syscallhdr.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # Generate a syscall number header.
  5. #
  6. # Each line of the syscall table should have the following format:
  7. #
  8. # NR ABI NAME [NATIVE] [COMPAT]
  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. set -e
  16. usage() {
  17. echo >&2 "usage: $0 [--abis ABIS] [--emit-nr] [--offset OFFSET] [--prefix PREFIX] INFILE OUTFILE" >&2
  18. echo >&2
  19. echo >&2 " INFILE input syscall table"
  20. echo >&2 " OUTFILE output header file"
  21. echo >&2
  22. echo >&2 "options:"
  23. echo >&2 " --abis ABIS ABI(s) to handle (By default, all lines are handled)"
  24. echo >&2 " --emit-nr Emit the macro of the number of syscalls (__NR_syscalls)"
  25. echo >&2 " --offset OFFSET The offset of syscall numbers"
  26. echo >&2 " --prefix PREFIX The prefix to the macro like __NR_<PREFIX><NAME>"
  27. exit 1
  28. }
  29. # default unless specified by options
  30. abis=
  31. emit_nr=
  32. offset=
  33. prefix=
  34. while [ $# -gt 0 ]
  35. do
  36. case $1 in
  37. --abis)
  38. abis=$(echo "($2)" | tr ',' '|')
  39. shift 2;;
  40. --emit-nr)
  41. emit_nr=1
  42. shift 1;;
  43. --offset)
  44. offset=$2
  45. shift 2;;
  46. --prefix)
  47. prefix=$2
  48. shift 2;;
  49. -*)
  50. echo "$1: unknown option" >&2
  51. usage;;
  52. *)
  53. break;;
  54. esac
  55. done
  56. if [ $# -ne 2 ]; then
  57. usage
  58. fi
  59. infile="$1"
  60. outfile="$2"
  61. guard=_UAPI_ASM_$(basename "$outfile" |
  62. sed -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' \
  63. -e 's/[^A-Z0-9_]/_/g' -e 's/__/_/g')
  64. grep -E "^[0-9A-Fa-fXx]+[[:space:]]+$abis" "$infile" | {
  65. echo "#ifndef $guard"
  66. echo "#define $guard"
  67. echo
  68. max=0
  69. while read nr abi name native compat ; do
  70. max=$nr
  71. if [ -n "$offset" ]; then
  72. nr="($offset + $nr)"
  73. fi
  74. echo "#define __NR_$prefix$name $nr"
  75. done
  76. if [ -n "$emit_nr" ]; then
  77. echo
  78. echo "#ifdef __KERNEL__"
  79. echo "#define __NR_${prefix}syscalls $(($max + 1))"
  80. echo "#endif"
  81. fi
  82. echo
  83. echo "#endif /* $guard */"
  84. } > "$outfile"