[PATCH] arch: Add 64-bit LoongArch support
authorXiaotian Wu <wuxiaotian@loongson.cn>
Tue, 9 Feb 2021 07:39:13 +0000 (15:39 +0800)
committerFelix Geyer <fgeyer@debian.org>
Sun, 24 Nov 2024 20:54:16 +0000 (21:54 +0100)
Signed-off-by: Xiaotian Wu <wuxiaotian@loongson.cn>
Signed-off-by: WANG Xuerui <git@xen0n.name>
Acked-by: Tom Hromatka <tom.hromatka@oracle.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
Gbp-Pq: Name arch_Add_64-bit_LoongArch_support.patch

17 files changed:
include/seccomp.h.in
src/Makefile.am
src/arch-loongarch64.c [new file with mode: 0644]
src/arch-loongarch64.h [new file with mode: 0644]
src/arch-syscall-dump.c
src/arch-syscall-validate
src/arch.c
src/gen_pfc.c
src/python/libseccomp.pxd
src/python/seccomp.pyx
src/syscalls.h
src/system.c
tools/scmp_arch_detect.c
tools/scmp_bpf_disasm.c
tools/scmp_bpf_sim.c
tools/util.c
tools/util.h

index ef4c6e4d09c5fd945ffa798cbad049c9db5a2745..0a3ffae10a9c83f509453be851a79ad1bc5fc3a4 100644 (file)
@@ -150,6 +150,18 @@ struct scmp_arg_cmp {
 #endif /* AUDIT_ARCH_AARCH64 */
 #define SCMP_ARCH_AARCH64      AUDIT_ARCH_AARCH64
 
+/**
+ * The LoongArch architecture tokens
+ */
+/* 64-bit LoongArch audit support is upstream as of 5.19-rc1 */
+#ifndef AUDIT_ARCH_LOONGARCH64
+#ifndef EM_LOONGARCH
+#define EM_LOONGARCH           258
+#endif /* EM_LOONGARCH */
+#define AUDIT_ARCH_LOONGARCH64 (EM_LOONGARCH|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
+#endif /* AUDIT_ARCH_LOONGARCH64 */
+#define SCMP_ARCH_LOONGARCH64  AUDIT_ARCH_LOONGARCH64
+
 /**
  * The MIPS architecture tokens
  */
index 1e5c092bc2c829ef5bcb985cbb4b8ee1732ced0e..cc1652dd67bb654b7224a42a133e1fbdd94aeea0 100644 (file)
@@ -32,6 +32,7 @@ SOURCES_ALL = \
        arch-x32.h arch-x32.c \
        arch-arm.h arch-arm.c \
        arch-aarch64.h arch-aarch64.c \
+       arch-loongarch64.h arch-loongarch64.c \
        arch-mips.h arch-mips.c \
        arch-mips64.h arch-mips64.c \
        arch-mips64n32.h arch-mips64n32.c \
diff --git a/src/arch-loongarch64.c b/src/arch-loongarch64.c
new file mode 100644 (file)
index 0000000..87a7d9d
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * Enhanced Seccomp 64-bit LoongArch Syscall Table
+ *
+ * Copyright (c) 2021 Xiaotian Wu <wuxiaotian@loongson.cn>
+ */
+
+/*
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <linux/audit.h>
+
+#include "arch.h"
+#include "arch-loongarch64.h"
+#include "syscalls.h"
+
+ARCH_DEF(loongarch64)
+
+const struct arch_def arch_def_loongarch64 = {
+       .token = SCMP_ARCH_LOONGARCH64,
+       .token_bpf = AUDIT_ARCH_LOONGARCH64,
+       .size = ARCH_SIZE_64,
+       .endian = ARCH_ENDIAN_LITTLE,
+       .syscall_resolve_name_raw = loongarch64_syscall_resolve_name,
+       .syscall_resolve_num_raw = loongarch64_syscall_resolve_num,
+       .syscall_rewrite = NULL,
+       .rule_add = NULL,
+};
diff --git a/src/arch-loongarch64.h b/src/arch-loongarch64.h
new file mode 100644 (file)
index 0000000..c3c06af
--- /dev/null
@@ -0,0 +1,28 @@
+/**
+ * Enhanced Seccomp 64-bit LoongArch Syscall Table
+ *
+ * Copyright (c) 2021 Xiaotian Wu <wuxiaotian@loongson.cn>
+ */
+
+/*
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of version 2.1 of the GNU Lesser General Public License as
+ * published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses>.
+ */
+
+#ifndef _ARCH_LOONGARCH64_H
+#define _ARCH_LOONGARCH64_H
+
+#include "arch.h"
+
+ARCH_DECL(loongarch64)
+
+#endif
index 2055d34221ab10c86d04c7d852125205587a823a..cc76e2c05e5228e30ad86503126fbcf649a4b5df 100644 (file)
@@ -34,6 +34,7 @@
 #include "arch-x86_64.h"
 #include "arch-x32.h"
 #include "arch-arm.h"
+#include "arch-loongarch64.h"
 #include "arch-mips.h"
 #include "arch-mips64.h"
 #include "arch-mips64n32.h"
@@ -106,6 +107,9 @@ int main(int argc, char *argv[])
                case SCMP_ARCH_AARCH64:
                        sys = aarch64_syscall_iterate(iter);
                        break;
+               case SCMP_ARCH_LOONGARCH64:
+                       sys = loongarch64_syscall_iterate(iter);
+                       break;
                case SCMP_ARCH_MIPS:
                case SCMP_ARCH_MIPSEL:
                        sys = mips_syscall_iterate(iter);
index 3b69e9b27451096d8cbe51a3e30d58ed35df3d9d..efbf2216644578965328b3478ff9515da43c58e2 100755 (executable)
@@ -305,6 +305,48 @@ function dump_lib_aarch64() {
        dump_lib_arch aarch64 | mangle_lib_syscall aarch64
 }
 
+#
+# Dump the loongarch64 syscall table
+#
+# Arguments:
+#     1    path to the kernel source
+#
+#  Dump the architecture's syscall table to stdout.
+#
+function dump_sys_loongarch64() {
+       local sed_filter=""
+
+       sed_filter+='s/__NR3264_fadvise64/223/;'
+       sed_filter+='s/__NR3264_fcntl/25/;'
+       sed_filter+='s/__NR3264_fstatfs/44/;'
+       sed_filter+='s/__NR3264_ftruncate/46/;'
+       sed_filter+='s/__NR3264_lseek/62/;'
+       sed_filter+='s/__NR3264_mmap/222/;'
+       sed_filter+='s/__NR3264_sendfile/71/;'
+       sed_filter+='s/__NR3264_statfs/43/;'
+       sed_filter+='s/__NR3264_truncate/45/;'
+
+       gcc -E -dM -I$1/include/uapi \
+               -D__BITS_PER_LONG=64 \
+               -D__ARCH_WANT_SYS_CLONE \
+               -D__ARCH_WANT_SYS_CLONE3 \
+               $1/arch/loongarch/include/uapi/asm/unistd.h | \
+               grep "^#define __NR_" | \
+               sed '/__NR_syscalls/d' | \
+               sed '/__NR_arch_specific_syscall/d' | \
+               sed 's/#define[ \t]\+__NR_\([^ \t]\+\)[ \t]\+\(.*\)/\1,\2/' | \
+               sed $sed_filter | sort
+}
+
+#
+# Dump the loongarch64 library syscall table
+#
+#  Dump the library's syscall table to stdout.
+#
+function dump_lib_loongarch64() {
+       dump_lib_arch loongarch64 | mangle_lib_syscall loongarch64
+}
+
 #
 # Dump the mips system syscall table
 #
@@ -593,6 +635,9 @@ function dump_sys() {
        aarch64)
                dump_sys_aarch64 "$2"
                ;;
+       loongarch64)
+               dump_sys_loongarch64 "$2"
+               ;;
        mips)
                dump_sys_mips "$2"
                ;;
@@ -657,6 +702,9 @@ function dump_lib() {
        aarch64)
                dump_lib_aarch64
                ;;
+       loongarch64)
+               dump_lib_loongarch64
+               ;;
        mips)
                dump_lib_mips
                ;;
@@ -717,6 +765,7 @@ function gen_csv() {
        abi_list=""
        abi_list+=" x86 x86_64 x32"
        abi_list+=" arm aarch64"
+       abi_list+=" loongarch64"
        abi_list+=" mips mips64 mips64n32"
        abi_list+=" parisc parisc64"
        abi_list+=" ppc ppc64"
@@ -806,6 +855,7 @@ if [[ $opt_arches == "" ]]; then
        opt_arches=" \
                x86 x86_64 x32 \
                arm aarch64 \
+               loongarch64 \
                mips mips64 mips64n32 \
                parisc parisc64 \
                ppc ppc64 \
index 8ef77b1a6b04b11cb2489dd7b0ab24ca9cb77953..8c5363a8193b30a904807cb2518d7fd5d01a4f8f 100644 (file)
@@ -35,6 +35,7 @@
 #include "arch-x32.h"
 #include "arch-arm.h"
 #include "arch-aarch64.h"
+#include "arch-loongarch64.h"
 #include "arch-mips.h"
 #include "arch-mips64.h"
 #include "arch-mips64n32.h"
@@ -62,6 +63,8 @@ const struct arch_def *arch_def_native = &arch_def_x86_64;
 const struct arch_def *arch_def_native = &arch_def_arm;
 #elif __aarch64__
 const struct arch_def *arch_def_native = &arch_def_aarch64;
+#elif __loongarch64
+const struct arch_def *arch_def_native = &arch_def_loongarch64;
 #elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI32
 #if __MIPSEB__
 const struct arch_def *arch_def_native = &arch_def_mips;
@@ -134,6 +137,8 @@ const struct arch_def *arch_def_lookup(uint32_t token)
                return &arch_def_arm;
        case SCMP_ARCH_AARCH64:
                return &arch_def_aarch64;
+       case SCMP_ARCH_LOONGARCH64:
+               return &arch_def_loongarch64;
        case SCMP_ARCH_MIPS:
                return &arch_def_mips;
        case SCMP_ARCH_MIPSEL:
@@ -186,6 +191,8 @@ const struct arch_def *arch_def_lookup_name(const char *arch_name)
                return &arch_def_arm;
        else if (strcmp(arch_name, "aarch64") == 0)
                return &arch_def_aarch64;
+       else if (strcmp(arch_name, "loongarch64") == 0)
+               return &arch_def_loongarch64;
        else if (strcmp(arch_name, "mips") == 0)
                return &arch_def_mips;
        else if (strcmp(arch_name, "mipsel") == 0)
index d58f586fa7b5cb1905778f2424152999bf6a6433..f7ae4e459fd731cdcf11dfd4803544c76b889654 100644 (file)
@@ -61,6 +61,8 @@ static const char *_pfc_arch(const struct arch_def *arch)
                return "arm";
        case SCMP_ARCH_AARCH64:
                return "aarch64";
+       case SCMP_ARCH_LOONGARCH64:
+               return "loongarch64";
        case SCMP_ARCH_MIPS:
                return "mips";
        case SCMP_ARCH_MIPSEL:
index 0629bf1ea10b3b3420cd4d19aa7542fd65bab576..280143ffd5ad1280e83cbf73baeb75c82c7706de 100644 (file)
@@ -38,6 +38,7 @@ cdef extern from "seccomp.h":
         SCMP_ARCH_X32
         SCMP_ARCH_ARM
         SCMP_ARCH_AARCH64
+        SCMP_ARCH_LOONGARCH64
         SCMP_ARCH_MIPS
         SCMP_ARCH_MIPS64
         SCMP_ARCH_MIPS64N32
index 2eeabc17b5f6a8b8ff732ab8aedc20b2d403b693..9e08cfd3644b6e4e509eb0e9f936dc2f2c631419 100644 (file)
@@ -204,6 +204,7 @@ cdef class Arch:
     X32 - 64-bit x86 using the x32 ABI
     ARM - ARM
     AARCH64 - 64-bit ARM
+    LOONGARCH64 - 64-bit LoongArch
     MIPS - MIPS O32 ABI
     MIPS64 - MIPS 64-bit ABI
     MIPS64N32 - MIPS N32 ABI
@@ -225,6 +226,7 @@ cdef class Arch:
     X32 = libseccomp.SCMP_ARCH_X32
     ARM = libseccomp.SCMP_ARCH_ARM
     AARCH64 = libseccomp.SCMP_ARCH_AARCH64
+    LOONGARCH64 = libseccomp.SCMP_ARCH_LOONGARCH64
     MIPS = libseccomp.SCMP_ARCH_MIPS
     MIPS64 = libseccomp.SCMP_ARCH_MIPS64
     MIPS64N32 = libseccomp.SCMP_ARCH_MIPS64N32
@@ -262,6 +264,8 @@ cdef class Arch:
                 self._token = libseccomp.SCMP_ARCH_ARM
             elif arch == libseccomp.SCMP_ARCH_AARCH64:
                 self._token = libseccomp.SCMP_ARCH_AARCH64
+            elif arch == libseccomp.SCMP_ARCH_LOONGARCH64:
+                self._token = libseccomp.SCMP_ARCH_LOONGARCH64
             elif arch == libseccomp.SCMP_ARCH_MIPS:
                 self._token = libseccomp.SCMP_ARCH_MIPS
             elif arch == libseccomp.SCMP_ARCH_MIPS64:
index af468a10f424008000fd25106774dd3f40cf00dd..2617563fdd1da62d3dad396a3228ef06b6e3e6a6 100644 (file)
@@ -14,6 +14,7 @@
 #include "arch-aarch64.h"
 #include "arch-arm.h"
 #include "arch.h"
+#include "arch-loongarch64.h"
 #include "arch-mips64.h"
 #include "arch-mips64n32.h"
 #include "arch-mips.h"
@@ -41,6 +42,7 @@ struct arch_syscall_table {
        int x32;
        int arm;
        int aarch64;
+       int loongarch64;
        int mips;
        int mips64;
        int mips64n32;
index ae445bf58dba9e8be3536bee0e0f2e1a5ebb7305..94e9be7cbe1a5ec751e7696de25b747d4e7b41d7 100644 (file)
@@ -126,6 +126,7 @@ int sys_chk_seccomp_syscall(void)
        case SCMP_ARCH_X86_64:
        case SCMP_ARCH_ARM:
        case SCMP_ARCH_AARCH64:
+       case SCMP_ARCH_LOONGARCH64:
        case SCMP_ARCH_PPC64:
        case SCMP_ARCH_PPC64LE:
        case SCMP_ARCH_S390:
index b844a68205f47d518208a5b15a391ebaecf59201..320b1afae0115e8696a4376dcff242be9679aac9 100644 (file)
@@ -81,6 +81,9 @@ int main(int argc, char *argv[])
                case SCMP_ARCH_AARCH64:
                        printf("aarch64\n");
                        break;
+               case SCMP_ARCH_LOONGARCH64:
+                       printf("loongarch64\n");
+                       break;
                case SCMP_ARCH_MIPS:
                        printf("mips\n");
                        break;
index b682de73f53e39c2d2d88146344bb996d8f3f425..45726591772692c2235500e7f40f03da9be6eda3 100644 (file)
@@ -484,6 +484,8 @@ int main(int argc, char *argv[])
                                arch = AUDIT_ARCH_ARM;
                        else if (strcmp(optarg, "aarch64") == 0)
                                arch = AUDIT_ARCH_AARCH64;
+                       else if (strcmp(optarg, "loongarch64") == 0)
+                               arch = AUDIT_ARCH_LOONGARCH64;
                        else if (strcmp(optarg, "mips") == 0)
                                arch = AUDIT_ARCH_MIPS;
                        else if (strcmp(optarg, "mipsel") == 0)
index a381314346d9d676e843cbc94f97e8aba9bf50fd..a2b31f17025ae74fa0f7ccbba165b9e377191be0 100644 (file)
@@ -259,6 +259,8 @@ int main(int argc, char *argv[])
                                arch = AUDIT_ARCH_ARM;
                        else if (strcmp(optarg, "aarch64") == 0)
                                arch = AUDIT_ARCH_AARCH64;
+                       else if (strcmp(optarg, "loongarch64") == 0)
+                               arch = AUDIT_ARCH_LOONGARCH64;
                        else if (strcmp(optarg, "mips") == 0)
                                arch = AUDIT_ARCH_MIPS;
                        else if (strcmp(optarg, "mipsel") == 0)
index 5687b30f66e48be18a4cf3d65e435080112a98b2..433bbfa4c4d0b3dc40fcd5cbea5d49f75925872b 100644 (file)
@@ -44,6 +44,8 @@
 #define ARCH_NATIVE            AUDIT_ARCH_ARM
 #elif __aarch64__
 #define ARCH_NATIVE            AUDIT_ARCH_AARCH64
+#elif __loongarch64
+#define ARCH_NATIVE            AUDIT_ARCH_LOONGARCH64
 #elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI32
 #if __MIPSEB__
 #define ARCH_NATIVE            AUDIT_ARCH_MIPS
index 6c2ca33db81a8244b79d0b0ce6f344f8f6cafcf6..24b9ed6a97c3e027f4fcd5771bcb01bacbd3e362 100644 (file)
 #define AUDIT_ARCH_AARCH64     (EM_AARCH64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
 #endif /* AUDIT_ARCH_AARCH64 */
 
+/**
+ * The 64-bit LoongArch architecture tokens
+ */
+/* 64-bit LoongArch audit support is upstream as of 5.19-rc1 */
+#ifndef AUDIT_ARCH_LOONGARCH64
+#ifndef EM_LOONGARCH
+#define EM_LOONGARCH           258
+#endif /* EM_LOONGARCH */
+#define AUDIT_ARCH_LOONGARCH64 (EM_LOONGARCH|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
+#endif /* AUDIT_ARCH_LOONGARCH64 */
+
 /**
  * The MIPS architecture tokens
  */