xref: /linux/tools/perf/util/syscalltbl.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * System call table mapper
4  *
5  * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
6  */
7 
8 #include "syscalltbl.h"
9 #include <stdlib.h>
10 #include <linux/compiler.h>
11 #include <linux/zalloc.h>
12 
13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT
14 #include <string.h>
15 #include "string2.h"
16 
17 #if defined(__x86_64__)
18 #include <asm/syscalls_64.c>
19 const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID;
20 static const char *const *syscalltbl_native = syscalltbl_x86_64;
21 #elif defined(__i386__)
22 #include <asm/syscalls_32.c>
23 const int syscalltbl_native_max_id = SYSCALLTBL_x86_MAX_ID;
24 static const char *const *syscalltbl_native = syscalltbl_x86;
25 #elif defined(__s390x__)
26 #include <asm/syscalls_64.c>
27 const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID;
28 static const char *const *syscalltbl_native = syscalltbl_s390_64;
29 #elif defined(__powerpc64__)
30 #include <asm/syscalls_64.c>
31 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID;
32 static const char *const *syscalltbl_native = syscalltbl_powerpc_64;
33 #elif defined(__powerpc__)
34 #include <asm/syscalls_32.c>
35 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID;
36 static const char *const *syscalltbl_native = syscalltbl_powerpc_32;
37 #elif defined(__aarch64__)
38 #include <asm/syscalls.c>
39 const int syscalltbl_native_max_id = SYSCALLTBL_ARM64_MAX_ID;
40 static const char *const *syscalltbl_native = syscalltbl_arm64;
41 #elif defined(__mips__)
42 #include <asm/syscalls_n64.c>
43 const int syscalltbl_native_max_id = SYSCALLTBL_MIPS_N64_MAX_ID;
44 static const char *const *syscalltbl_native = syscalltbl_mips_n64;
45 #elif defined(__loongarch__)
46 #include <asm/syscalls.c>
47 const int syscalltbl_native_max_id = SYSCALLTBL_LOONGARCH_MAX_ID;
48 static const char *const *syscalltbl_native = syscalltbl_loongarch;
49 #elif defined(__riscv)
50 #include <asm/syscalls.c>
51 const int syscalltbl_native_max_id = SYSCALLTBL_RISCV_MAX_ID;
52 static const char *const *syscalltbl_native = syscalltbl_riscv;
53 #else
54 const int syscalltbl_native_max_id = 0;
55 static const char *const syscalltbl_native[] = {
56 	[0] = "unknown",
57 };
58 #endif
59 
60 struct syscall {
61 	int id;
62 	const char *name;
63 };
64 
65 static int syscallcmpname(const void *vkey, const void *ventry)
66 {
67 	const char *key = vkey;
68 	const struct syscall *entry = ventry;
69 
70 	return strcmp(key, entry->name);
71 }
72 
73 static int syscallcmp(const void *va, const void *vb)
74 {
75 	const struct syscall *a = va, *b = vb;
76 
77 	return strcmp(a->name, b->name);
78 }
79 
80 static int syscalltbl__init_native(struct syscalltbl *tbl)
81 {
82 	int nr_entries = 0, i, j;
83 	struct syscall *entries;
84 
85 	for (i = 0; i <= syscalltbl_native_max_id; ++i)
86 		if (syscalltbl_native[i])
87 			++nr_entries;
88 
89 	entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
90 	if (tbl->syscalls.entries == NULL)
91 		return -1;
92 
93 	for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
94 		if (syscalltbl_native[i]) {
95 			entries[j].name = syscalltbl_native[i];
96 			entries[j].id = i;
97 			++j;
98 		}
99 	}
100 
101 	qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
102 	tbl->syscalls.nr_entries = nr_entries;
103 	tbl->syscalls.max_id	 = syscalltbl_native_max_id;
104 	return 0;
105 }
106 
107 struct syscalltbl *syscalltbl__new(void)
108 {
109 	struct syscalltbl *tbl = malloc(sizeof(*tbl));
110 	if (tbl) {
111 		if (syscalltbl__init_native(tbl)) {
112 			free(tbl);
113 			return NULL;
114 		}
115 	}
116 	return tbl;
117 }
118 
119 void syscalltbl__delete(struct syscalltbl *tbl)
120 {
121 	zfree(&tbl->syscalls.entries);
122 	free(tbl);
123 }
124 
125 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
126 {
127 	return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
128 }
129 
130 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
131 {
132 	struct syscall *sc = bsearch(name, tbl->syscalls.entries,
133 				     tbl->syscalls.nr_entries, sizeof(*sc),
134 				     syscallcmpname);
135 
136 	return sc ? sc->id : -1;
137 }
138 
139 int syscalltbl__id_at_idx(struct syscalltbl *tbl, int idx)
140 {
141 	struct syscall *syscalls = tbl->syscalls.entries;
142 
143 	return idx < tbl->syscalls.nr_entries ? syscalls[idx].id : -1;
144 }
145 
146 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
147 {
148 	int i;
149 	struct syscall *syscalls = tbl->syscalls.entries;
150 
151 	for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
152 		if (strglobmatch(syscalls[i].name, syscall_glob)) {
153 			*idx = i;
154 			return syscalls[i].id;
155 		}
156 	}
157 
158 	return -1;
159 }
160 
161 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
162 {
163 	*idx = -1;
164 	return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
165 }
166 
167 #else /* HAVE_SYSCALL_TABLE_SUPPORT */
168 
169 #include <libaudit.h>
170 
171 struct syscalltbl *syscalltbl__new(void)
172 {
173 	struct syscalltbl *tbl = zalloc(sizeof(*tbl));
174 	if (tbl)
175 		tbl->audit_machine = audit_detect_machine();
176 	return tbl;
177 }
178 
179 void syscalltbl__delete(struct syscalltbl *tbl)
180 {
181 	free(tbl);
182 }
183 
184 const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
185 {
186 	return audit_syscall_to_name(id, tbl->audit_machine);
187 }
188 
189 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
190 {
191 	return audit_name_to_syscall(name, tbl->audit_machine);
192 }
193 
194 int syscalltbl__id_at_idx(struct syscalltbl *tbl __maybe_unused, int idx)
195 {
196 	return idx;
197 }
198 
199 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
200 				  const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
201 {
202 	return -1;
203 }
204 
205 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
206 {
207 	return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
208 }
209 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */
210