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