1 // SPDX-License-Identifier: GPL-2.0-only
3 * System call table mapper
5 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
8 #include "syscalltbl.h"
10 #include <linux/compiler.h>
11 #include <linux/zalloc.h>
13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT
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
;
54 const int syscalltbl_native_max_id
= 0;
55 static const char *const syscalltbl_native
[] = {
65 static int syscallcmpname(const void *vkey
, const void *ventry
)
67 const char *key
= vkey
;
68 const struct syscall
*entry
= ventry
;
70 return strcmp(key
, entry
->name
);
73 static int syscallcmp(const void *va
, const void *vb
)
75 const struct syscall
*a
= va
, *b
= vb
;
77 return strcmp(a
->name
, b
->name
);
80 static int syscalltbl__init_native(struct syscalltbl
*tbl
)
82 int nr_entries
= 0, i
, j
;
83 struct syscall
*entries
;
85 for (i
= 0; i
<= syscalltbl_native_max_id
; ++i
)
86 if (syscalltbl_native
[i
])
89 entries
= tbl
->syscalls
.entries
= malloc(sizeof(struct syscall
) * nr_entries
);
90 if (tbl
->syscalls
.entries
== NULL
)
93 for (i
= 0, j
= 0; i
<= syscalltbl_native_max_id
; ++i
) {
94 if (syscalltbl_native
[i
]) {
95 entries
[j
].name
= syscalltbl_native
[i
];
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
;
107 struct syscalltbl
*syscalltbl__new(void)
109 struct syscalltbl
*tbl
= malloc(sizeof(*tbl
));
111 if (syscalltbl__init_native(tbl
)) {
119 void syscalltbl__delete(struct syscalltbl
*tbl
)
121 zfree(&tbl
->syscalls
.entries
);
125 const char *syscalltbl__name(const struct syscalltbl
*tbl __maybe_unused
, int id
)
127 return id
<= syscalltbl_native_max_id
? syscalltbl_native
[id
]: NULL
;
130 int syscalltbl__id(struct syscalltbl
*tbl
, const char *name
)
132 struct syscall
*sc
= bsearch(name
, tbl
->syscalls
.entries
,
133 tbl
->syscalls
.nr_entries
, sizeof(*sc
),
136 return sc
? sc
->id
: -1;
139 int syscalltbl__id_at_idx(struct syscalltbl
*tbl
, int idx
)
141 struct syscall
*syscalls
= tbl
->syscalls
.entries
;
143 return idx
< tbl
->syscalls
.nr_entries
? syscalls
[idx
].id
: -1;
146 int syscalltbl__strglobmatch_next(struct syscalltbl
*tbl
, const char *syscall_glob
, int *idx
)
149 struct syscall
*syscalls
= tbl
->syscalls
.entries
;
151 for (i
= *idx
+ 1; i
< tbl
->syscalls
.nr_entries
; ++i
) {
152 if (strglobmatch(syscalls
[i
].name
, syscall_glob
)) {
154 return syscalls
[i
].id
;
161 int syscalltbl__strglobmatch_first(struct syscalltbl
*tbl
, const char *syscall_glob
, int *idx
)
164 return syscalltbl__strglobmatch_next(tbl
, syscall_glob
, idx
);
167 #else /* HAVE_SYSCALL_TABLE_SUPPORT */
169 #include <libaudit.h>
171 struct syscalltbl
*syscalltbl__new(void)
173 struct syscalltbl
*tbl
= zalloc(sizeof(*tbl
));
175 tbl
->audit_machine
= audit_detect_machine();
179 void syscalltbl__delete(struct syscalltbl
*tbl
)
184 const char *syscalltbl__name(const struct syscalltbl
*tbl
, int id
)
186 return audit_syscall_to_name(id
, tbl
->audit_machine
);
189 int syscalltbl__id(struct syscalltbl
*tbl
, const char *name
)
191 return audit_name_to_syscall(name
, tbl
->audit_machine
);
194 int syscalltbl__id_at_idx(struct syscalltbl
*tbl __maybe_unused
, int idx
)
199 int syscalltbl__strglobmatch_next(struct syscalltbl
*tbl __maybe_unused
,
200 const char *syscall_glob __maybe_unused
, int *idx __maybe_unused
)
205 int syscalltbl__strglobmatch_first(struct syscalltbl
*tbl
, const char *syscall_glob
, int *idx
)
207 return syscalltbl__strglobmatch_next(tbl
, syscall_glob
, idx
);
209 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */