Skip to content

Commit 0fb641f

Browse files
captain5050namhyung
authored andcommitted
perf trace beauty: Add syscalltbl.sh generating all system call tables
Rather than generating individual syscall header files generate a single trace/beauty/generated/syscalltbl.c. In a syscalltbls array have references to each architectures tables along with the corresponding e_machine. When the 32-bit or 64-bit table is ambiguous, match the perf binary's type. For ARM32 don't use the arm64 32-bit table which is smaller. EM_NONE is present for is no machine matches. Conditionally compile the tables, only having the appropriate 32 and 64-bit table. If ALL_SYSCALLTBL is defined all tables can be compiled. Add comment for noreturn column suggested by Arnd Bergmann: https://lore.kernel.org/lkml/d47c35dd-9c52-48e7-a00d-135572f11fbb@app.fastmail.com/ and added in commit 9142be9 ("x86/syscall: Mark exit[_group] syscall handlers __noreturn"). Signed-off-by: Ian Rogers <irogers@google.com> Reviewed-by: Howard Chu <howardchu95@gmail.com> Reviewed-by: Charlie Jenkins <charlie@rivosinc.com> Reviewed-by: Namhyung Kim <namhyung@kernel.org> Acked-by: Arnaldo Carvalho de Melo <acme@kernel.org> Link: https://lore.kernel.org/r/20250319050741.269828-9-irogers@google.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 7035102 commit 0fb641f

2 files changed

Lines changed: 283 additions & 0 deletions

File tree

tools/perf/Makefile.perf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,14 @@ beauty_ioctl_outdir := $(beauty_outdir)/ioctl
559559
# Create output directory if not already present
560560
$(shell [ -d '$(beauty_ioctl_outdir)' ] || mkdir -p '$(beauty_ioctl_outdir)')
561561

562+
syscall_array := $(beauty_outdir)/syscalltbl.c
563+
syscall_tbl := $(srctree)/tools/perf/trace/beauty/syscalltbl.sh
564+
syscall_tbl_data := $(srctree)/tools/scripts/syscall.tbl \
565+
$(wildcard $(srctree)/tools/perf/arch/*/entry/syscalls/syscall*.tbl)
566+
567+
$(syscall_array): $(syscall_tbl) $(syscall_tbl_data)
568+
$(Q)$(SHELL) '$(syscall_tbl)' $(srctree)/tools $@
569+
562570
fs_at_flags_array := $(beauty_outdir)/fs_at_flags_array.c
563571
fs_at_flags_tbl := $(srctree)/tools/perf/trace/beauty/fs_at_flags.sh
564572

@@ -878,6 +886,7 @@ build-dir = $(or $(__build-dir),.)
878886

879887
prepare: $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)common-cmds.h archheaders \
880888
arm64-sysreg-defs \
889+
$(syscall_array) \
881890
$(fs_at_flags_array) \
882891
$(clone_flags_array) \
883892
$(drm_ioctl_array) \
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
#
4+
# Generate all syscall tables.
5+
#
6+
# Each line of the syscall table should have the following format:
7+
#
8+
# NR ABI NAME [NATIVE] [COMPAT [noreturn]]
9+
#
10+
# NR syscall number
11+
# ABI ABI name
12+
# NAME syscall name
13+
# NATIVE native entry point (optional)
14+
# COMPAT compat entry point (optional)
15+
# noreturn system call doesn't return (optional)
16+
set -e
17+
18+
usage() {
19+
cat >&2 <<EOF
20+
usage: $0 <TOOLS DIRECTORY> <OUTFILE>
21+
22+
<TOOLS DIRECTORY> path to kernel tools directory
23+
<OUTFILE> output header file
24+
EOF
25+
exit 1
26+
}
27+
28+
if [ $# -ne 2 ]; then
29+
usage
30+
fi
31+
tools_dir=$1
32+
outfile=$2
33+
34+
build_tables() {
35+
infile="$1"
36+
outfile="$2"
37+
abis=$(echo "($3)" | tr ',' '|')
38+
e_machine="$4"
39+
40+
if [ ! -f "$infile" ]
41+
then
42+
echo "Missing file $infile"
43+
exit 1
44+
fi
45+
sorted_table=$(mktemp /tmp/syscalltbl.XXXXXX)
46+
grep -E "^[0-9]+[[:space:]]+$abis" "$infile" | sort -n > "$sorted_table"
47+
48+
echo "static const char *const syscall_num_to_name_${e_machine}[] = {" >> "$outfile"
49+
# the params are: nr abi name entry compat
50+
# use _ for intentionally unused variables according to SC2034
51+
while read -r nr _ name _ _; do
52+
echo " [$nr] = \"$name\"," >> "$outfile"
53+
done < "$sorted_table"
54+
echo "};" >> "$outfile"
55+
56+
echo "static const uint16_t syscall_sorted_names_${e_machine}[] = {" >> "$outfile"
57+
58+
# When sorting by name, add a suffix of 0s upto 20 characters so that
59+
# system calls that differ with a numerical suffix don't sort before
60+
# those without. This default behavior of sort differs from that of
61+
# strcmp used at runtime. Use sed to strip the trailing 0s suffix
62+
# afterwards.
63+
grep -E "^[0-9]+[[:space:]]+$abis" "$infile" | awk '{printf $3; for (i = length($3); i < 20; i++) { printf "0"; }; print " " $1}'| sort | sed 's/\([a-zA-Z1-9]\+\)0\+ \([0-9]\+\)/\1 \2/' > "$sorted_table"
64+
while read -r name nr; do
65+
echo " $nr, /* $name */" >> "$outfile"
66+
done < "$sorted_table"
67+
echo "};" >> "$outfile"
68+
69+
rm -f "$sorted_table"
70+
}
71+
72+
rm -f "$outfile"
73+
cat >> "$outfile" <<EOF
74+
#include <elf.h>
75+
#include <stdint.h>
76+
#include <asm/bitsperlong.h>
77+
#include <linux/kernel.h>
78+
79+
struct syscalltbl {
80+
const char *const *num_to_name;
81+
const uint16_t *sorted_names;
82+
uint16_t e_machine;
83+
uint16_t num_to_name_len;
84+
uint16_t sorted_names_len;
85+
};
86+
87+
#if defined(ALL_SYSCALLTBL) || defined(__alpha__)
88+
EOF
89+
build_tables "$tools_dir/perf/arch/alpha/entry/syscalls/syscall.tbl" "$outfile" common,64 EM_ALPHA
90+
cat >> "$outfile" <<EOF
91+
#endif // defined(ALL_SYSCALLTBL) || defined(__alpha__)
92+
93+
#if defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
94+
EOF
95+
build_tables "$tools_dir/perf/arch/arm/entry/syscalls/syscall.tbl" "$outfile" common,32,oabi EM_ARM
96+
build_tables "$tools_dir/perf/arch/arm64/entry/syscalls/syscall_64.tbl" "$outfile" common,64,renameat,rlimit,memfd_secret EM_AARCH64
97+
cat >> "$outfile" <<EOF
98+
#endif // defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
99+
100+
#if defined(ALL_SYSCALLTBL) || defined(__csky__)
101+
EOF
102+
build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,32,csky,time32,stat64,rlimit EM_CSKY
103+
cat >> "$outfile" <<EOF
104+
#endif // defined(ALL_SYSCALLTBL) || defined(__csky__)
105+
106+
#if defined(ALL_SYSCALLTBL) || defined(__mips__)
107+
EOF
108+
build_tables "$tools_dir/perf/arch/mips/entry/syscalls/syscall_n64.tbl" "$outfile" common,64,n64 EM_MIPS
109+
cat >> "$outfile" <<EOF
110+
#endif // defined(ALL_SYSCALLTBL) || defined(__mips__)
111+
112+
#if defined(ALL_SYSCALLTBL) || defined(__hppa__)
113+
#if __BITS_PER_LONG != 64
114+
EOF
115+
build_tables "$tools_dir/perf/arch/parisc/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_PARISC
116+
echo "#else" >> "$outfile"
117+
build_tables "$tools_dir/perf/arch/parisc/entry/syscalls/syscall.tbl" "$outfile" common,64 EM_PARISC
118+
cat >> "$outfile" <<EOF
119+
#endif //__BITS_PER_LONG != 64
120+
#endif // defined(ALL_SYSCALLTBL) || defined(__hppa__)
121+
122+
#if defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
123+
EOF
124+
build_tables "$tools_dir/perf/arch/powerpc/entry/syscalls/syscall.tbl" "$outfile" common,32,nospu EM_PPC
125+
build_tables "$tools_dir/perf/arch/powerpc/entry/syscalls/syscall.tbl" "$outfile" common,64,nospu EM_PPC64
126+
cat >> "$outfile" <<EOF
127+
#endif // defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
128+
129+
#if defined(ALL_SYSCALLTBL) || defined(__riscv)
130+
#if __BITS_PER_LONG != 64
131+
EOF
132+
build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,32,riscv,memfd_secret EM_RISCV
133+
echo "#else" >> "$outfile"
134+
build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,64,riscv,rlimit,memfd_secret EM_RISCV
135+
cat >> "$outfile" <<EOF
136+
#endif //__BITS_PER_LONG != 64
137+
#endif // defined(ALL_SYSCALLTBL) || defined(__riscv)
138+
#if defined(ALL_SYSCALLTBL) || defined(__s390x__)
139+
EOF
140+
build_tables "$tools_dir/perf/arch/s390/entry/syscalls/syscall.tbl" "$outfile" common,64,renameat,rlimit,memfd_secret EM_S390
141+
cat >> "$outfile" <<EOF
142+
#endif // defined(ALL_SYSCALLTBL) || defined(__s390x__)
143+
144+
#if defined(ALL_SYSCALLTBL) || defined(__sh__)
145+
EOF
146+
build_tables "$tools_dir/perf/arch/sh/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_SH
147+
cat >> "$outfile" <<EOF
148+
#endif // defined(ALL_SYSCALLTBL) || defined(__sh__)
149+
150+
#if defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
151+
#if __BITS_PER_LONG != 64
152+
EOF
153+
build_tables "$tools_dir/perf/arch/sparc/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_SPARC
154+
echo "#else" >> "$outfile"
155+
build_tables "$tools_dir/perf/arch/sparc/entry/syscalls/syscall.tbl" "$outfile" common,64 EM_SPARC
156+
cat >> "$outfile" <<EOF
157+
#endif //__BITS_PER_LONG != 64
158+
#endif // defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
159+
160+
#if defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
161+
EOF
162+
build_tables "$tools_dir/perf/arch/x86/entry/syscalls/syscall_32.tbl" "$outfile" common,32,i386 EM_386
163+
build_tables "$tools_dir/perf/arch/x86/entry/syscalls/syscall_64.tbl" "$outfile" common,64 EM_X86_64
164+
cat >> "$outfile" <<EOF
165+
#endif // defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
166+
167+
#if defined(ALL_SYSCALLTBL) || defined(__xtensa__)
168+
EOF
169+
build_tables "$tools_dir/perf/arch/xtensa/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_XTENSA
170+
cat >> "$outfile" <<EOF
171+
#endif // defined(ALL_SYSCALLTBL) || defined(__xtensa__)
172+
173+
#if __BITS_PER_LONG != 64
174+
EOF
175+
build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,32 EM_NONE
176+
echo "#else" >> "$outfile"
177+
build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,64 EM_NONE
178+
echo "#endif //__BITS_PER_LONG != 64" >> "$outfile"
179+
180+
build_outer_table() {
181+
e_machine=$1
182+
outfile="$2"
183+
cat >> "$outfile" <<EOF
184+
{
185+
.num_to_name = syscall_num_to_name_$e_machine,
186+
.sorted_names = syscall_sorted_names_$e_machine,
187+
.e_machine = $e_machine,
188+
.num_to_name_len = ARRAY_SIZE(syscall_num_to_name_$e_machine),
189+
.sorted_names_len = ARRAY_SIZE(syscall_sorted_names_$e_machine),
190+
},
191+
EOF
192+
}
193+
194+
cat >> "$outfile" <<EOF
195+
static const struct syscalltbl syscalltbls[] = {
196+
#if defined(ALL_SYSCALLTBL) || defined(__alpha__)
197+
EOF
198+
build_outer_table EM_ALPHA "$outfile"
199+
cat >> "$outfile" <<EOF
200+
#endif // defined(ALL_SYSCALLTBL) || defined(__alpha__)
201+
202+
#if defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
203+
EOF
204+
build_outer_table EM_ARM "$outfile"
205+
build_outer_table EM_AARCH64 "$outfile"
206+
cat >> "$outfile" <<EOF
207+
#endif // defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
208+
209+
#if defined(ALL_SYSCALLTBL) || defined(__csky__)
210+
EOF
211+
build_outer_table EM_CSKY "$outfile"
212+
cat >> "$outfile" <<EOF
213+
#endif // defined(ALL_SYSCALLTBL) || defined(__csky__)
214+
215+
#if defined(ALL_SYSCALLTBL) || defined(__mips__)
216+
EOF
217+
build_outer_table EM_MIPS "$outfile"
218+
cat >> "$outfile" <<EOF
219+
#endif // defined(ALL_SYSCALLTBL) || defined(__mips__)
220+
221+
#if defined(ALL_SYSCALLTBL) || defined(__hppa__)
222+
EOF
223+
build_outer_table EM_PARISC "$outfile"
224+
cat >> "$outfile" <<EOF
225+
#endif // defined(ALL_SYSCALLTBL) || defined(__hppa__)
226+
227+
#if defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
228+
EOF
229+
build_outer_table EM_PPC "$outfile"
230+
build_outer_table EM_PPC64 "$outfile"
231+
cat >> "$outfile" <<EOF
232+
#endif // defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
233+
234+
#if defined(ALL_SYSCALLTBL) || defined(__riscv)
235+
EOF
236+
build_outer_table EM_RISCV "$outfile"
237+
cat >> "$outfile" <<EOF
238+
#endif // defined(ALL_SYSCALLTBL) || defined(__riscv)
239+
240+
#if defined(ALL_SYSCALLTBL) || defined(__s390x__)
241+
EOF
242+
build_outer_table EM_S390 "$outfile"
243+
cat >> "$outfile" <<EOF
244+
#endif // defined(ALL_SYSCALLTBL) || defined(__s390x__)
245+
246+
#if defined(ALL_SYSCALLTBL) || defined(__sh__)
247+
EOF
248+
build_outer_table EM_SH "$outfile"
249+
cat >> "$outfile" <<EOF
250+
#endif // defined(ALL_SYSCALLTBL) || defined(__sh__)
251+
252+
#if defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
253+
EOF
254+
build_outer_table EM_SPARC "$outfile"
255+
cat >> "$outfile" <<EOF
256+
#endif // defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
257+
258+
#if defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
259+
EOF
260+
build_outer_table EM_386 "$outfile"
261+
build_outer_table EM_X86_64 "$outfile"
262+
cat >> "$outfile" <<EOF
263+
#endif // defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
264+
265+
#if defined(ALL_SYSCALLTBL) || defined(__xtensa__)
266+
EOF
267+
build_outer_table EM_XTENSA "$outfile"
268+
cat >> "$outfile" <<EOF
269+
#endif // defined(ALL_SYSCALLTBL) || defined(__xtensa__)
270+
EOF
271+
build_outer_table EM_NONE "$outfile"
272+
cat >> "$outfile" <<EOF
273+
};
274+
EOF

0 commit comments

Comments
 (0)