Skip to content

Commit

Permalink
Add script to automatically generate syscalls
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Cervesato <[email protected]>
  • Loading branch information
acerv committed Sep 23, 2024
1 parent 09489a8 commit 0f0d586
Showing 1 changed file with 178 additions and 0 deletions.
178 changes: 178 additions & 0 deletions include/lapi/syscalls/generate_syscalls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#!/bin/sh
#
# Script to generate the list of syscalls file for all architectures.
# Based on https://github.com/hrw/syscalls-table/
#
# Author: Andrea Cervesato <[email protected]>

if [ -z ${TMPDIR} ]; then
TMPDIR="/tmp"
fi

LINUX_SRC="$1"
SYSCALLS_FILE="$2"

SUPPORTED_ARCHS="${PWD}/supported-arch.txt"
LINUX_HEADERS="${TMPDIR}/headers"
SYSCALLS_NAMES="${TMPDIR}/syscalls-names.txt"
GENERATOR_BIN="${TMPDIR}/list-syscalls"
GENERATOR_SRC="${GENERATOR_BIN}.c"

if [ -z ${LINUX_SRC} ]; then
echo "Give the path of Linux kernel sources:"
echo ""
echo "$0 linux/kernel/sources"
echo ""
exit 1
fi

if [ ! -e ${LINUX_SRC}/Makefile ]; then
echo "No Makefile in ${LINUX_SRC} directory!"
exit 1
fi

build_headers() {
arch="$1"

echo "Building linux headers..."

make -s -C ${LINUX_SRC} \
ARCH=${arch} \
O=${LINUX_HEADERS} \
headers_install >/dev/null
}

extract_syscalls() {
arch="$1"
flags="$2"
uppercase_arch=$(echo "$arch" | tr '[:lower:]' '[:upper:]')

echo "Extracting syscalls names..."

grep -E -h "^#define __NR_" \
${LINUX_HEADERS}/usr/include/asm/unistd*.h \
${LINUX_HEADERS}/usr/include/asm-generic/unistd.h > \
${TMPDIR}/syscalls-names.tosort

grep -E -v "(unistd.h|NR3264|__NR_syscall|__SC_COMP|__NR_.*Linux|__NR_FAST)" \
${TMPDIR}/syscalls-names.tosort |
grep -E -vi "(not implemented|available|unused|reserved|xtensa|spill)" |
grep -E -v "(__SYSCALL|SYSCALL_BASE|SYSCALL_MASK)" |
sed -e "s/#define\s*__NR_//g" -e "s/\s.*//g" >${SYSCALLS_NAMES}

echo "
#include <stdio.h>
#include <asm/unistd.h>
int main(void)
{
" > ${GENERATOR_SRC}

while IFS= read -r syscall
do
echo "
#ifdef __NR_$syscall
printf(\"$syscall\\t%d\\n\", __NR_$syscall);
#else
printf(\"$syscall\\n\");
#endif
" >> ${GENERATOR_SRC}
done <${SYSCALLS_NAMES}

echo " return 0;
}
" >> ${GENERATOR_SRC}

gcc ${GENERATOR_SRC} -U__LP64__ -U__ILP32__ -U__i386__ \
-D${uppercase_arch} -D__${arch}__ ${flags} \
-I ${LINUX_HEADERS}/usr/include/ \
-o ${GENERATOR_BIN} &>/dev/null


echo "Generating ${arch}.in ..."

${GENERATOR_BIN} > ${arch}.in
}

generate_syscalls() {
case $1 in
aarch64)
build_headers "arm64"
extract_syscalls "$1" "-D__ARM_EABI__"
;;
arc)
build_headers "arc"
extract_syscalls "$1" "-D__BITS_PER_LONG=32"
;;
arm)
build_headers "arm"
extract_syscalls "$1" "-D__BITS_PER_LONG=32"
;;
i386)
build_headers "x86"
extract_syscalls "$1" "-D__BITS_PER_LONG=32"
;;
loongarch)
build_headers "loongarch"
extract_syscalls "$1" "-D_LOONGARCH_SZLONG=64"
;;
mips_n32)
build_headers "mips"
extract_syscalls "$1" "-D_MIPS_SIM=_MIPS_SIM_NABI32"
;;
mips_n64)
build_headers "mips"
extract_syscalls "$1" "-D_MIPS_SIM=_MIPS_SIM_ABI64"
;;
mips_o32)
build_headers "mips"
extract_syscalls "$1" "-D_MIPS_SIM=_MIPS_SIM_ABI32"
;;
powerpc)
build_headers "powerpc"
extract_syscalls "$1" "-D__BITS_PER_LONG=32"
;;
powerpc64)
build_headers "powerpc"
extract_syscalls "$1" ""
;;
s390)
build_headers "s390"
extract_syscalls "$1" "-D__BITS_PER_LONG=32"
;;
s390x)
build_headers "s390"
extract_syscalls "$1" ""
;;
sh)
build_headers "sh"
extract_syscalls "$1" "-D__BITS_PER_LONG=32"
;;
sparc)
build_headers "sparc"
extract_syscalls "$1" "-D__32bit_syscall_numbers__ -D__BITS_PER_LONG=32"
;;
sparc64)
build_headers "sparc64"
extract_syscalls "$1" "-D__arch64__"
;;
x86_64)
build_headers "x86_64"
extract_syscalls "$1" "-D__LP64__"
;;
hppa | ia64)
echo "'$1' architecture is not supported"
echo 1
;;
*)
echo "Can't find '$1' architecture"
exit 1
;;
esac
}

while IFS= read -r arch
do
echo "Preparing syscalls for ${arch} architecture..."
generate_syscalls "${arch}"
done <${SUPPORTED_ARCHS}

0 comments on commit 0f0d586

Please sign in to comment.