xref: /openbmc/qemu/scripts/gensyscalls.sh (revision 38472890)
1#!/bin/sh
2#
3# Update syscall_nr.h files from linux headers asm-generic/unistd.h
4#
5# This code is licensed under the GPL version 2 or later.  See
6# the COPYING file in the top-level directory.
7#
8
9linux="$1"
10output="$2"
11
12TMP=$(mktemp -d)
13
14if [ "$linux" = "" ] ; then
15    echo "Needs path to linux source tree" 1>&2
16    exit 1
17fi
18
19if [ "$output" = "" ] ; then
20    output="$PWD"
21fi
22
23upper()
24{
25    echo "$1" | tr "[:lower:]" "[:upper:]" | tr "[:punct:]" "_"
26}
27
28qemu_arch()
29{
30    case "$1" in
31    arm64)
32        echo "aarch64"
33        ;;
34    *)
35        echo "$1"
36        ;;
37    esac
38}
39
40read_includes()
41{
42    arch=$1
43    bits=$2
44
45     cpp -P -nostdinc -fdirectives-only \
46        -D_UAPI_ASM_$(upper ${arch})_BITSPERLONG_H \
47        -D__BITS_PER_LONG=${bits} \
48        -I${linux}/arch/${arch}/include/uapi/ \
49        -I${linux}/include/uapi \
50        -I${TMP} \
51        "${linux}/arch/${arch}/include/uapi/asm/unistd.h"
52}
53
54filter_defines()
55{
56    grep -e "#define __NR_" -e "#define __NR3264"
57}
58
59rename_defines()
60{
61    sed "s/ __NR_/ TARGET_NR_/g;s/(__NR_/(TARGET_NR_/g"
62}
63
64evaluate_values()
65{
66    sed "s/#define TARGET_NR_/QEMU TARGET_NR_/" | \
67    cpp -P -nostdinc | \
68    sed "s/^QEMU /#define /"
69}
70
71generate_syscall_nr()
72{
73    arch=$1
74    bits=$2
75    file="$3"
76    guard="$(upper LINUX_USER_$(qemu_arch $arch)_$(basename "$file"))"
77
78    (echo "/*"
79    echo " * This file contains the system call numbers."
80    echo " * Do not modify."
81    echo " * This file is generated by scripts/gensyscalls.sh"
82    echo " */"
83    echo "#ifndef ${guard}"
84    echo "#define ${guard}"
85    echo
86    read_includes $arch $bits | filter_defines | rename_defines | \
87                                evaluate_values | sort -n -k 3
88    echo
89    echo "#endif /* ${guard} */"
90    echo) > "$file"
91}
92
93mkdir "$TMP/asm"
94> "$TMP/asm/bitsperlong.h"
95
96generate_syscall_nr arm64 64 "$output/linux-user/aarch64/syscall_nr.h"
97generate_syscall_nr nios2 32 "$output/linux-user/nios2/syscall_nr.h"
98generate_syscall_nr openrisc 32 "$output/linux-user/openrisc/syscall_nr.h"
99
100generate_syscall_nr riscv 32 "$output/linux-user/riscv/syscall32_nr.h"
101generate_syscall_nr riscv 64 "$output/linux-user/riscv/syscall64_nr.h"
102rm -fr "$TMP"
103