syscalltbl.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. usage() {
  18. cat >&2 <<EOF
  19. usage: $0 <TOOLS DIRECTORY> <OUTFILE>
  20. <TOOLS DIRECTORY> path to kernel tools directory
  21. <OUTFILE> output header file
  22. EOF
  23. exit 1
  24. }
  25. if [ $# -ne 2 ]; then
  26. usage
  27. fi
  28. tools_dir=$1
  29. outfile=$2
  30. build_tables() {
  31. infile="$1"
  32. outfile="$2"
  33. abis=$(echo "($3)" | tr ',' '|')
  34. e_machine="$4"
  35. if [ ! -f "$infile" ]
  36. then
  37. echo "Missing file $infile"
  38. exit 1
  39. fi
  40. sorted_table=$(mktemp /tmp/syscalltbl.XXXXXX)
  41. grep -E "^[0-9]+[[:space:]]+$abis" "$infile" | sort -n > "$sorted_table"
  42. echo "static const char *const syscall_num_to_name_${e_machine}[] = {" >> "$outfile"
  43. # the params are: nr abi name entry compat
  44. # use _ for intentionally unused variables according to SC2034
  45. while read -r nr _ name _ _; do
  46. echo " [$nr] = \"$name\"," >> "$outfile"
  47. done < "$sorted_table"
  48. echo "};" >> "$outfile"
  49. echo "static const uint16_t syscall_sorted_names_${e_machine}[] = {" >> "$outfile"
  50. # When sorting by name, add a suffix of 0s upto 20 characters so that
  51. # system calls that differ with a numerical suffix don't sort before
  52. # those without. This default behavior of sort differs from that of
  53. # strcmp used at runtime. Use sed to strip the trailing 0s suffix
  54. # afterwards.
  55. 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"
  56. while read -r name nr; do
  57. echo " $nr, /* $name */" >> "$outfile"
  58. done < "$sorted_table"
  59. echo "};" >> "$outfile"
  60. rm -f "$sorted_table"
  61. }
  62. rm -f "$outfile"
  63. cat >> "$outfile" <<EOF
  64. #include <elf.h>
  65. #include <stdint.h>
  66. #include <asm/bitsperlong.h>
  67. #include <linux/kernel.h>
  68. struct syscalltbl {
  69. const char *const *num_to_name;
  70. const uint16_t *sorted_names;
  71. uint16_t e_machine;
  72. uint16_t num_to_name_len;
  73. uint16_t sorted_names_len;
  74. };
  75. #if defined(ALL_SYSCALLTBL) || defined(__alpha__)
  76. EOF
  77. build_tables "$tools_dir/perf/arch/alpha/entry/syscalls/syscall.tbl" "$outfile" common,64 EM_ALPHA
  78. cat >> "$outfile" <<EOF
  79. #endif // defined(ALL_SYSCALLTBL) || defined(__alpha__)
  80. #if defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
  81. EOF
  82. build_tables "$tools_dir/perf/arch/arm/entry/syscalls/syscall.tbl" "$outfile" common,32,oabi EM_ARM
  83. build_tables "$tools_dir/perf/arch/arm64/entry/syscalls/syscall_64.tbl" "$outfile" common,64,renameat,rlimit,memfd_secret EM_AARCH64
  84. cat >> "$outfile" <<EOF
  85. #endif // defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
  86. #if defined(ALL_SYSCALLTBL) || defined(__csky__)
  87. EOF
  88. build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,32,csky,time32,stat64,rlimit EM_CSKY
  89. cat >> "$outfile" <<EOF
  90. #endif // defined(ALL_SYSCALLTBL) || defined(__csky__)
  91. #if defined(ALL_SYSCALLTBL) || defined(__mips__)
  92. EOF
  93. build_tables "$tools_dir/perf/arch/mips/entry/syscalls/syscall_n64.tbl" "$outfile" common,64,n64 EM_MIPS
  94. cat >> "$outfile" <<EOF
  95. #endif // defined(ALL_SYSCALLTBL) || defined(__mips__)
  96. #if defined(ALL_SYSCALLTBL) || defined(__hppa__)
  97. #if __BITS_PER_LONG != 64
  98. EOF
  99. build_tables "$tools_dir/perf/arch/parisc/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_PARISC
  100. echo "#else" >> "$outfile"
  101. build_tables "$tools_dir/perf/arch/parisc/entry/syscalls/syscall.tbl" "$outfile" common,64 EM_PARISC
  102. cat >> "$outfile" <<EOF
  103. #endif //__BITS_PER_LONG != 64
  104. #endif // defined(ALL_SYSCALLTBL) || defined(__hppa__)
  105. #if defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
  106. EOF
  107. build_tables "$tools_dir/perf/arch/powerpc/entry/syscalls/syscall.tbl" "$outfile" common,32,nospu EM_PPC
  108. build_tables "$tools_dir/perf/arch/powerpc/entry/syscalls/syscall.tbl" "$outfile" common,64,nospu EM_PPC64
  109. cat >> "$outfile" <<EOF
  110. #endif // defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
  111. #if defined(ALL_SYSCALLTBL) || defined(__riscv)
  112. #if __BITS_PER_LONG != 64
  113. EOF
  114. build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,32,riscv,memfd_secret EM_RISCV
  115. echo "#else" >> "$outfile"
  116. build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,64,riscv,rlimit,memfd_secret EM_RISCV
  117. cat >> "$outfile" <<EOF
  118. #endif //__BITS_PER_LONG != 64
  119. #endif // defined(ALL_SYSCALLTBL) || defined(__riscv)
  120. #if defined(ALL_SYSCALLTBL) || defined(__s390x__)
  121. EOF
  122. build_tables "$tools_dir/perf/arch/s390/entry/syscalls/syscall.tbl" "$outfile" common,64,renameat,rlimit,memfd_secret EM_S390
  123. cat >> "$outfile" <<EOF
  124. #endif // defined(ALL_SYSCALLTBL) || defined(__s390x__)
  125. #if defined(ALL_SYSCALLTBL) || defined(__sh__)
  126. EOF
  127. build_tables "$tools_dir/perf/arch/sh/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_SH
  128. cat >> "$outfile" <<EOF
  129. #endif // defined(ALL_SYSCALLTBL) || defined(__sh__)
  130. #if defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
  131. #if __BITS_PER_LONG != 64
  132. EOF
  133. build_tables "$tools_dir/perf/arch/sparc/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_SPARC
  134. echo "#else" >> "$outfile"
  135. build_tables "$tools_dir/perf/arch/sparc/entry/syscalls/syscall.tbl" "$outfile" common,64 EM_SPARC
  136. cat >> "$outfile" <<EOF
  137. #endif //__BITS_PER_LONG != 64
  138. #endif // defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
  139. #if defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
  140. EOF
  141. build_tables "$tools_dir/perf/arch/x86/entry/syscalls/syscall_32.tbl" "$outfile" common,32,i386 EM_386
  142. build_tables "$tools_dir/perf/arch/x86/entry/syscalls/syscall_64.tbl" "$outfile" common,64 EM_X86_64
  143. cat >> "$outfile" <<EOF
  144. #endif // defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
  145. #if defined(ALL_SYSCALLTBL) || defined(__xtensa__)
  146. EOF
  147. build_tables "$tools_dir/perf/arch/xtensa/entry/syscalls/syscall.tbl" "$outfile" common,32 EM_XTENSA
  148. cat >> "$outfile" <<EOF
  149. #endif // defined(ALL_SYSCALLTBL) || defined(__xtensa__)
  150. #if __BITS_PER_LONG != 64
  151. EOF
  152. build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,32 EM_NONE
  153. echo "#else" >> "$outfile"
  154. build_tables "$tools_dir/scripts/syscall.tbl" "$outfile" common,64 EM_NONE
  155. echo "#endif //__BITS_PER_LONG != 64" >> "$outfile"
  156. build_outer_table() {
  157. e_machine=$1
  158. outfile="$2"
  159. cat >> "$outfile" <<EOF
  160. {
  161. .num_to_name = syscall_num_to_name_$e_machine,
  162. .sorted_names = syscall_sorted_names_$e_machine,
  163. .e_machine = $e_machine,
  164. .num_to_name_len = ARRAY_SIZE(syscall_num_to_name_$e_machine),
  165. .sorted_names_len = ARRAY_SIZE(syscall_sorted_names_$e_machine),
  166. },
  167. EOF
  168. }
  169. cat >> "$outfile" <<EOF
  170. static const struct syscalltbl syscalltbls[] = {
  171. #if defined(ALL_SYSCALLTBL) || defined(__alpha__)
  172. EOF
  173. build_outer_table EM_ALPHA "$outfile"
  174. cat >> "$outfile" <<EOF
  175. #endif // defined(ALL_SYSCALLTBL) || defined(__alpha__)
  176. #if defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
  177. EOF
  178. build_outer_table EM_ARM "$outfile"
  179. build_outer_table EM_AARCH64 "$outfile"
  180. cat >> "$outfile" <<EOF
  181. #endif // defined(ALL_SYSCALLTBL) || defined(__arm__) || defined(__aarch64__)
  182. #if defined(ALL_SYSCALLTBL) || defined(__csky__)
  183. EOF
  184. build_outer_table EM_CSKY "$outfile"
  185. cat >> "$outfile" <<EOF
  186. #endif // defined(ALL_SYSCALLTBL) || defined(__csky__)
  187. #if defined(ALL_SYSCALLTBL) || defined(__mips__)
  188. EOF
  189. build_outer_table EM_MIPS "$outfile"
  190. cat >> "$outfile" <<EOF
  191. #endif // defined(ALL_SYSCALLTBL) || defined(__mips__)
  192. #if defined(ALL_SYSCALLTBL) || defined(__hppa__)
  193. EOF
  194. build_outer_table EM_PARISC "$outfile"
  195. cat >> "$outfile" <<EOF
  196. #endif // defined(ALL_SYSCALLTBL) || defined(__hppa__)
  197. #if defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
  198. EOF
  199. build_outer_table EM_PPC "$outfile"
  200. build_outer_table EM_PPC64 "$outfile"
  201. cat >> "$outfile" <<EOF
  202. #endif // defined(ALL_SYSCALLTBL) || defined(__powerpc__) || defined(__powerpc64__)
  203. #if defined(ALL_SYSCALLTBL) || defined(__riscv)
  204. EOF
  205. build_outer_table EM_RISCV "$outfile"
  206. cat >> "$outfile" <<EOF
  207. #endif // defined(ALL_SYSCALLTBL) || defined(__riscv)
  208. #if defined(ALL_SYSCALLTBL) || defined(__s390x__)
  209. EOF
  210. build_outer_table EM_S390 "$outfile"
  211. cat >> "$outfile" <<EOF
  212. #endif // defined(ALL_SYSCALLTBL) || defined(__s390x__)
  213. #if defined(ALL_SYSCALLTBL) || defined(__sh__)
  214. EOF
  215. build_outer_table EM_SH "$outfile"
  216. cat >> "$outfile" <<EOF
  217. #endif // defined(ALL_SYSCALLTBL) || defined(__sh__)
  218. #if defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
  219. EOF
  220. build_outer_table EM_SPARC "$outfile"
  221. cat >> "$outfile" <<EOF
  222. #endif // defined(ALL_SYSCALLTBL) || defined(__sparc64__) || defined(__sparc__)
  223. #if defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
  224. EOF
  225. build_outer_table EM_386 "$outfile"
  226. build_outer_table EM_X86_64 "$outfile"
  227. cat >> "$outfile" <<EOF
  228. #endif // defined(ALL_SYSCALLTBL) || defined(__i386__) || defined(__x86_64__)
  229. #if defined(ALL_SYSCALLTBL) || defined(__xtensa__)
  230. EOF
  231. build_outer_table EM_XTENSA "$outfile"
  232. cat >> "$outfile" <<EOF
  233. #endif // defined(ALL_SYSCALLTBL) || defined(__xtensa__)
  234. EOF
  235. build_outer_table EM_NONE "$outfile"
  236. cat >> "$outfile" <<EOF
  237. };
  238. EOF