Skip to content

Commit 6966ec7

Browse files
yetistpcmoore
authored andcommitted
arch: Add 64-bit LoongArch support
Signed-off-by: Xiaotian Wu <[email protected]> Signed-off-by: WANG Xuerui <[email protected]> Acked-by: Tom Hromatka <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent 315f7b9 commit 6966ec7

17 files changed

+176
-0
lines changed

include/seccomp.h.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ struct scmp_arg_cmp {
152152
#endif /* AUDIT_ARCH_AARCH64 */
153153
#define SCMP_ARCH_AARCH64 AUDIT_ARCH_AARCH64
154154

155+
/**
156+
* The LoongArch architecture tokens
157+
*/
158+
/* 64-bit LoongArch audit support is upstream as of 5.19-rc1 */
159+
#ifndef AUDIT_ARCH_LOONGARCH64
160+
#ifndef EM_LOONGARCH
161+
#define EM_LOONGARCH 258
162+
#endif /* EM_LOONGARCH */
163+
#define AUDIT_ARCH_LOONGARCH64 (EM_LOONGARCH|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
164+
#endif /* AUDIT_ARCH_LOONGARCH64 */
165+
#define SCMP_ARCH_LOONGARCH64 AUDIT_ARCH_LOONGARCH64
166+
155167
/**
156168
* The MIPS architecture tokens
157169
*/

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SOURCES_ALL = \
3232
arch-x32.h arch-x32.c \
3333
arch-arm.h arch-arm.c \
3434
arch-aarch64.h arch-aarch64.c \
35+
arch-loongarch64.h arch-loongarch64.c \
3536
arch-mips.h arch-mips.c \
3637
arch-mips64.h arch-mips64.c \
3738
arch-mips64n32.h arch-mips64n32.c \

src/arch-loongarch64.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Enhanced Seccomp 64-bit LoongArch Syscall Table
3+
*
4+
* Copyright (c) 2021 Xiaotian Wu <[email protected]>
5+
*/
6+
7+
/*
8+
* This library is free software; you can redistribute it and/or modify it
9+
* under the terms of version 2.1 of the GNU Lesser General Public License as
10+
* published by the Free Software Foundation.
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15+
* for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, see <http://www.gnu.org/licenses>.
19+
*/
20+
21+
#include <stdlib.h>
22+
#include <errno.h>
23+
#include <linux/audit.h>
24+
25+
#include "arch.h"
26+
#include "arch-loongarch64.h"
27+
#include "syscalls.h"
28+
29+
ARCH_DEF(loongarch64)
30+
31+
const struct arch_def arch_def_loongarch64 = {
32+
.token = SCMP_ARCH_LOONGARCH64,
33+
.token_bpf = AUDIT_ARCH_LOONGARCH64,
34+
.size = ARCH_SIZE_64,
35+
.endian = ARCH_ENDIAN_LITTLE,
36+
.syscall_resolve_name_raw = loongarch64_syscall_resolve_name,
37+
.syscall_resolve_num_raw = loongarch64_syscall_resolve_num,
38+
.syscall_rewrite = NULL,
39+
.rule_add = NULL,
40+
.syscall_name_kver = loongarch64_syscall_name_kver,
41+
.syscall_num_kver = loongarch64_syscall_num_kver,
42+
};

src/arch-loongarch64.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Enhanced Seccomp 64-bit LoongArch Syscall Table
3+
*
4+
* Copyright (c) 2021 Xiaotian Wu <[email protected]>
5+
*/
6+
7+
/*
8+
* This library is free software; you can redistribute it and/or modify it
9+
* under the terms of version 2.1 of the GNU Lesser General Public License as
10+
* published by the Free Software Foundation.
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15+
* for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, see <http://www.gnu.org/licenses>.
19+
*/
20+
21+
#ifndef _ARCH_LOONGARCH64_H
22+
#define _ARCH_LOONGARCH64_H
23+
24+
#include "arch.h"
25+
26+
ARCH_DECL(loongarch64)
27+
28+
#endif

src/arch-syscall-dump.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "arch-x86_64.h"
3535
#include "arch-x32.h"
3636
#include "arch-arm.h"
37+
#include "arch-loongarch64.h"
3738
#include "arch-mips.h"
3839
#include "arch-mips64.h"
3940
#include "arch-mips64n32.h"
@@ -107,6 +108,9 @@ int main(int argc, char *argv[])
107108
case SCMP_ARCH_AARCH64:
108109
sys = aarch64_syscall_iterate(iter);
109110
break;
111+
case SCMP_ARCH_LOONGARCH64:
112+
sys = loongarch64_syscall_iterate(iter);
113+
break;
110114
case SCMP_ARCH_MIPS:
111115
case SCMP_ARCH_MIPSEL:
112116
sys = mips_syscall_iterate(iter);

src/arch-syscall-validate

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,48 @@ function dump_lib_aarch64() {
306306
dump_lib_arch aarch64 | mangle_lib_syscall aarch64
307307
}
308308

309+
#
310+
# Dump the loongarch64 syscall table
311+
#
312+
# Arguments:
313+
# 1 path to the kernel source
314+
#
315+
# Dump the architecture's syscall table to stdout.
316+
#
317+
function dump_sys_loongarch64() {
318+
local sed_filter=""
319+
320+
sed_filter+='s/__NR3264_fadvise64/223/;'
321+
sed_filter+='s/__NR3264_fcntl/25/;'
322+
sed_filter+='s/__NR3264_fstatfs/44/;'
323+
sed_filter+='s/__NR3264_ftruncate/46/;'
324+
sed_filter+='s/__NR3264_lseek/62/;'
325+
sed_filter+='s/__NR3264_mmap/222/;'
326+
sed_filter+='s/__NR3264_sendfile/71/;'
327+
sed_filter+='s/__NR3264_statfs/43/;'
328+
sed_filter+='s/__NR3264_truncate/45/;'
329+
330+
gcc -E -dM -I$1/include/uapi \
331+
-D__BITS_PER_LONG=64 \
332+
-D__ARCH_WANT_SYS_CLONE \
333+
-D__ARCH_WANT_SYS_CLONE3 \
334+
$1/arch/loongarch/include/uapi/asm/unistd.h | \
335+
grep "^#define __NR_" | \
336+
sed '/__NR_syscalls/d' | \
337+
sed '/__NR_arch_specific_syscall/d' | \
338+
sed 's/#define[ \t]\+__NR_\([^ \t]\+\)[ \t]\+\(.*\)/\1,\2/' | \
339+
sed $sed_filter | sort
340+
}
341+
342+
#
343+
# Dump the loongarch64 library syscall table
344+
#
345+
# Dump the library's syscall table to stdout.
346+
#
347+
function dump_lib_loongarch64() {
348+
dump_lib_arch loongarch64 | mangle_lib_syscall loongarch64
349+
}
350+
309351
#
310352
# Dump the mips system syscall table
311353
#
@@ -619,6 +661,9 @@ function dump_sys() {
619661
aarch64)
620662
dump_sys_aarch64 "$2"
621663
;;
664+
loongarch64)
665+
dump_sys_loongarch64 "$2"
666+
;;
622667
mips)
623668
dump_sys_mips "$2"
624669
;;
@@ -686,6 +731,9 @@ function dump_lib() {
686731
aarch64)
687732
dump_lib_aarch64
688733
;;
734+
loongarch64)
735+
dump_lib_loongarch64
736+
;;
689737
mips)
690738
dump_lib_mips
691739
;;
@@ -750,6 +798,7 @@ function gen_csv() {
750798
abi_list=""
751799
abi_list+=" x86 x86_64 x32"
752800
abi_list+=" arm aarch64"
801+
abi_list+=" loongarch64"
753802
abi_list+=" mips mips64 mips64n32"
754803
abi_list+=" parisc parisc64"
755804
abi_list+=" ppc ppc64"
@@ -861,6 +910,7 @@ if [[ $opt_arches == "" ]]; then
861910
opt_arches=" \
862911
x86 x86_64 x32 \
863912
arm aarch64 \
913+
loongarch64 \
864914
mips mips64 mips64n32 \
865915
parisc parisc64 \
866916
ppc ppc64 \

src/arch.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "arch-x32.h"
3636
#include "arch-arm.h"
3737
#include "arch-aarch64.h"
38+
#include "arch-loongarch64.h"
3839
#include "arch-mips.h"
3940
#include "arch-mips64.h"
4041
#include "arch-mips64n32.h"
@@ -63,6 +64,8 @@ const struct arch_def *arch_def_native = &arch_def_x86_64;
6364
const struct arch_def *arch_def_native = &arch_def_arm;
6465
#elif __aarch64__
6566
const struct arch_def *arch_def_native = &arch_def_aarch64;
67+
#elif __loongarch64
68+
const struct arch_def *arch_def_native = &arch_def_loongarch64;
6669
#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI32
6770
#if __MIPSEB__
6871
const struct arch_def *arch_def_native = &arch_def_mips;
@@ -141,6 +144,8 @@ const struct arch_def *arch_def_lookup(uint32_t token)
141144
return &arch_def_arm;
142145
case SCMP_ARCH_AARCH64:
143146
return &arch_def_aarch64;
147+
case SCMP_ARCH_LOONGARCH64:
148+
return &arch_def_loongarch64;
144149
case SCMP_ARCH_MIPS:
145150
return &arch_def_mips;
146151
case SCMP_ARCH_MIPSEL:
@@ -197,6 +202,8 @@ const struct arch_def *arch_def_lookup_name(const char *arch_name)
197202
return &arch_def_arm;
198203
else if (strcmp(arch_name, "aarch64") == 0)
199204
return &arch_def_aarch64;
205+
else if (strcmp(arch_name, "loongarch64") == 0)
206+
return &arch_def_loongarch64;
200207
else if (strcmp(arch_name, "mips") == 0)
201208
return &arch_def_mips;
202209
else if (strcmp(arch_name, "mipsel") == 0)

src/gen_pfc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ static const char *_pfc_arch(const struct arch_def *arch)
6161
return "arm";
6262
case SCMP_ARCH_AARCH64:
6363
return "aarch64";
64+
case SCMP_ARCH_LOONGARCH64:
65+
return "loongarch64";
6466
case SCMP_ARCH_MIPS:
6567
return "mips";
6668
case SCMP_ARCH_MIPSEL:

src/python/libseccomp.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ cdef extern from "seccomp.h":
3838
SCMP_ARCH_X32
3939
SCMP_ARCH_ARM
4040
SCMP_ARCH_AARCH64
41+
SCMP_ARCH_LOONGARCH64
4142
SCMP_ARCH_MIPS
4243
SCMP_ARCH_MIPS64
4344
SCMP_ARCH_MIPS64N32

src/python/seccomp.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ cdef class Arch:
206206
X32 - 64-bit x86 using the x32 ABI
207207
ARM - ARM
208208
AARCH64 - 64-bit ARM
209+
LOONGARCH64 - 64-bit LoongArch
209210
MIPS - MIPS O32 ABI
210211
MIPS64 - MIPS 64-bit ABI
211212
MIPS64N32 - MIPS N32 ABI
@@ -227,6 +228,7 @@ cdef class Arch:
227228
X32 = libseccomp.SCMP_ARCH_X32
228229
ARM = libseccomp.SCMP_ARCH_ARM
229230
AARCH64 = libseccomp.SCMP_ARCH_AARCH64
231+
LOONGARCH64 = libseccomp.SCMP_ARCH_LOONGARCH64
230232
MIPS = libseccomp.SCMP_ARCH_MIPS
231233
MIPS64 = libseccomp.SCMP_ARCH_MIPS64
232234
MIPS64N32 = libseccomp.SCMP_ARCH_MIPS64N32
@@ -264,6 +266,8 @@ cdef class Arch:
264266
self._token = libseccomp.SCMP_ARCH_ARM
265267
elif arch == libseccomp.SCMP_ARCH_AARCH64:
266268
self._token = libseccomp.SCMP_ARCH_AARCH64
269+
elif arch == libseccomp.SCMP_ARCH_LOONGARCH64:
270+
self._token = libseccomp.SCMP_ARCH_LOONGARCH64
267271
elif arch == libseccomp.SCMP_ARCH_MIPS:
268272
self._token = libseccomp.SCMP_ARCH_MIPS
269273
elif arch == libseccomp.SCMP_ARCH_MIPS64:

0 commit comments

Comments
 (0)