[LTP] [PATCH v2 2/5] Add script to generate arch(s) dependant syscalls

Andrea Cervesato andrea.cervesato@suse.de
Fri Sep 27 11:49:22 CEST 2024


From: Andrea Cervesato <andrea.cervesato@suse.com>

Add generate_arch.sh script which can be used to generate arch(s)
dependant syscalls file. The way it works is pretty simple: for each
architecture defined into supported-arch.txt, compile kernel headers,
extract the list of syscalls and generate a .in file containing all of
them, associated with their own syscall's number.
The way syscalls files are generated, passes through a C application
which is automatically checking the availability of the syscalls in
the user space environment.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 include/lapi/syscalls/blacklist-syscalls.txt |   6 +
 include/lapi/syscalls/generate_arch.sh       | 182 +++++++++++++++++++++++++++
 include/lapi/syscalls/generate_syscalls.sh   |   4 +-
 3 files changed, 190 insertions(+), 2 deletions(-)

diff --git a/include/lapi/syscalls/blacklist-syscalls.txt b/include/lapi/syscalls/blacklist-syscalls.txt
new file mode 100644
index 000000000..e1ae5f76f
--- /dev/null
+++ b/include/lapi/syscalls/blacklist-syscalls.txt
@@ -0,0 +1,6 @@
+arch_specific_syscall
+available
+ni_syscall
+reserved
+SYSCALL_MASK
+unused
diff --git a/include/lapi/syscalls/generate_arch.sh b/include/lapi/syscalls/generate_arch.sh
new file mode 100755
index 000000000..c3338d82c
--- /dev/null
+++ b/include/lapi/syscalls/generate_arch.sh
@@ -0,0 +1,182 @@
+#!/bin/sh
+#
+# Generate arch dependant syscalls files.
+# Based on https://github.com/hrw/syscalls-table/
+#
+# Author: Andrea Cervesato <andrea.cervesato@suse.com>
+
+TEMP=$(mktemp -d)
+LINUX_SRC="$1"
+
+if [ -z "${LINUX_SRC}" ]; then
+	echo "Please give the path of Linux kernel sources:"
+	echo ""
+	echo "$0 path/of/linux/sources"
+	echo ""
+	exit 1
+fi
+
+if [ ! -e "${LINUX_SRC}/Makefile" ]; then
+	echo "No Makefile in ${LINUX_SRC} directory!"
+	exit 1
+fi
+
+export LC_ALL=C
+
+SCRIPT_DIR="$(realpath $(dirname "$0"))"
+SUPPORTED_ARCH="${SCRIPT_DIR}/supported-arch.txt"
+SYSCALLS_BLACKLIST="${SCRIPT_DIR}/blacklist-syscalls.txt"
+LINUX_HEADERS="${TEMP}/headers"
+
+build_headers() {
+	local arch="$1"
+
+	echo "Building linux headers..."
+
+	make -s -C ${LINUX_SRC} \
+		ARCH=${arch} \
+		O=${LINUX_HEADERS} \
+		headers_install >/dev/null
+}
+
+extract_syscalls() {
+	local arch="$1"
+	local flags="$2"
+	local syscalls_tosort="${TEMP}/syscalls-names.tosort"
+	local syscalls_names="${TEMP}/syscalls-names.txt"
+	local generator_bin="${TEMP}/list-syscalls"
+	local generator_src="${generator_bin}.c"
+
+	echo "Extracting syscalls names..."
+
+	grep -E -h "^#define __NR_" \
+		${LINUX_HEADERS}/usr/include/asm/unistd*.h \
+		${LINUX_HEADERS}/usr/include/asm-generic/unistd.h > \
+		${syscalls_tosort}
+
+	grep -E -v "(unistd.h|NR3264|__NR_syscall|__SC_COMP|__NR_.*Linux|__NR_FAST)" \
+		${syscalls_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" |
+		sort -u >${syscalls_names}
+
+	grep -w -v -f ${SYSCALLS_BLACKLIST} ${syscalls_names} |
+		sort -u >${syscalls_names}
+
+	(
+		echo
+		echo "
+		#include <stdio.h>
+		#include <asm/unistd.h>
+
+		int main(void) {"
+		while IFS= read -r syscall; do
+			echo "
+		#ifdef __NR_$syscall
+			printf(\"$syscall\\t%d\\n\", __NR_$syscall);
+		#endif"
+		done < ${syscalls_names}
+		echo "return 0; }"
+	) >> ${generator_src}
+
+	local uppercase_arch=$(echo "$arch" | tr '[:lower:]' '[:upper:]')
+
+	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} > "${SCRIPT_DIR}/${arch}.in"
+}
+
+generate_syscalls() {
+	while IFS= read -r arch; do
+		echo "Preparing syscalls for ${arch} architecture..."
+
+		case ${arch} in
+		aarch64)
+			build_headers "arm64"
+			extract_syscalls "${arch}" "-D__ARM_EABI__"
+			;;
+		arc)
+			build_headers "arc"
+			extract_syscalls "${arch}" "-D__BITS_PER_LONG=32"
+			;;
+		arm)
+			build_headers "arm"
+			extract_syscalls "${arch}" "-D__BITS_PER_LONG=32"
+			;;
+		hppa)
+			build_headers "parisc"
+			extract_syscalls "${arch}" ""
+			;;
+		i386)
+			build_headers "x86"
+			extract_syscalls "${arch}" "-D__BITS_PER_LONG=32"
+			;;
+		ia64)
+			# ia64 has been removed from the kernel
+			;;
+		loongarch)
+			build_headers "loongarch"
+			extract_syscalls "${arch}" "-D_LOONGARCH_SZLONG=64"
+			;;
+		mips_n32)
+			build_headers "mips"
+			extract_syscalls "${arch}" "-D_MIPS_SIM=_MIPS_SIM_NABI32"
+			;;
+		mips_n64)
+			build_headers "mips"
+			extract_syscalls "${arch}" "-D_MIPS_SIM=_MIPS_SIM_ABI64"
+			;;
+		mips_o32)
+			build_headers "mips"
+			extract_syscalls "${arch}" "-D_MIPS_SIM=_MIPS_SIM_ABI32"
+			;;
+		powerpc)
+			build_headers "powerpc"
+			extract_syscalls "${arch}" "-D__BITS_PER_LONG=32"
+			;;
+		powerpc64)
+			build_headers "powerpc"
+			extract_syscalls "${arch}" ""
+			;;
+		s390)
+			build_headers "s390"
+			extract_syscalls "${arch}" "-D__BITS_PER_LONG=32"
+			;;
+		s390x)
+			build_headers "s390"
+			extract_syscalls "${arch}" ""
+			;;
+		sh)
+			build_headers "sh"
+			extract_syscalls "${arch}" "-D__BITS_PER_LONG=32"
+			;;
+		sparc)
+			build_headers "sparc"
+			extract_syscalls "${arch}" "-D__32bit_syscall_numbers__ -D__BITS_PER_LONG=32"
+			;;
+		sparc64)
+			build_headers "sparc64"
+			extract_syscalls "${arch}" "-D__arch64__"
+			;;
+		x86_64)
+			build_headers "x86_64"
+			extract_syscalls "${arch}" "-D__LP64__"
+			;;
+		*)
+			echo "Can't find '${arch}' architecture"
+			exit 1
+			;;
+		esac
+	done < ${SUPPORTED_ARCH}
+}
+
+echo "Temporary folder: ${TEMP}"
+
+generate_syscalls
+
diff --git a/include/lapi/syscalls/generate_syscalls.sh b/include/lapi/syscalls/generate_syscalls.sh
index 52e605900..a75d3ea38 100755
--- a/include/lapi/syscalls/generate_syscalls.sh
+++ b/include/lapi/syscalls/generate_syscalls.sh
@@ -88,7 +88,7 @@ tst_ret; \
 				echo "# ifndef ${syscall_nr}"
 				echo "#  define ${syscall_nr} $*"
 				echo "# endif"
-			done <"${arch}.in"
+			done <"${SCRIPT_DIR}/${arch}.in"
 			echo "#endif"
 			echo
 		) >>${SYSCALLS_FILE}
@@ -106,7 +106,7 @@ tst_ret; \
 				echo "# ifndef ${syscall_nr}"
 				echo "#  define ${syscall_nr} __LTP__NR_INVALID_SYSCALL"
 				echo "# endif"
-			done <"${arch}.in"
+			done <"${SCRIPT_DIR}/${arch}.in"
 		done <${SUPPORTED_ARCH}
 		echo "#endif"
 	) >>${SYSCALLS_FILE}

-- 
2.43.0



More information about the ltp mailing list