Skip to content

Commit e3baf2f

Browse files
committed
arch: Add riscv32 architecture support
Support for rv32 was upstreamed into 5.4+ kernel Signed-off-by: Khem Raj <raj.khem@gmail.com>
1 parent a792221 commit e3baf2f

30 files changed

Lines changed: 210 additions & 6 deletions

CREDITS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
3333
Jonah Petri <jonah@petri.us>
3434
Justin Cormack <justin.cormack@docker.com>
3535
Kees Cook <keescook@chromium.org>
36+
Khem Raj <raj.khem@gmail.com>
3637
Kyle R. Conway <kyle.r.conway@gmail.com>
3738
Kenta Tada <Kenta.Tada@sony.com>
3839
Kir Kolyshkin <kolyshkin@gmail.com>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The libseccomp library currently supports the architectures listed below:
5454
* 32-bit s390 (s390)
5555
* 64-bit s390x (s390x)
5656
* 64-bit RISC-V (riscv64)
57+
* 32-bit RISC-V (riscv32)
5758
* 32-bit SuperH big endian (sheb)
5859
* 32-bit SuperH (sh)
5960

doc/man/man1/scmp_sys_resolver.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The architecture to use for resolving the system call. Valid
3636
.I ARCH
3737
values are "x86", "x86_64", "x32", "arm", "aarch64", "mips", "mipsel", "mips64",
3838
"mipsel64", "mips64n32", "mipsel64n32", "parisc", "parisc64", "ppc", "ppc64",
39-
"ppc64le", "s390", "s390x", "sheb" and "sh".
39+
"ppc64le", "riscv32", "s390", "s390x", "sheb" and "sh".
4040
.TP
4141
.B \-t
4242
If necessary, translate the system call name to the proper system call number,

doc/man/man3/seccomp_arch_add.3

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ seccomp_arch_add, seccomp_arch_remove, seccomp_arch_exist, seccomp_arch_native \
3030
.B #define SCMP_ARCH_S390X
3131
.B #define SCMP_ARCH_PARISC
3232
.B #define SCMP_ARCH_PARISC64
33+
.B #define SCMP_ARCH_RISCV32
3334
.B #define SCMP_ARCH_RISCV64
3435
.sp
3536
.BI "uint32_t seccomp_arch_resolve_name(const char *" arch_name ");"

include/seccomp-syscalls.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,14 @@
276276
#define __PNR_renameat -10242
277277
#define __PNR_riscv_flush_icache -10243
278278
#define __PNR_memfd_secret -10244
279+
#define __PNR_fstat -10245
280+
#define __PNR_futex -10246
281+
#define __PNR_nanosleep -10247
282+
#define __PNR_lseek -10248
283+
#define __PNR_clock_gettime -10249
284+
#define __PNR_clock_nanosleep -10250
285+
#define __PNR_gettimeofday -10251
286+
#define __PNR_fcntl -10252
279287

280288
/*
281289
* libseccomp syscall definitions
@@ -443,15 +451,23 @@
443451
#define __SNR_clock_getres_time64 __PNR_clock_getres_time64
444452
#endif
445453

454+
#ifdef __NR_clock_gettime
446455
#define __SNR_clock_gettime __NR_clock_gettime
456+
#else
457+
#define __SNR_clock_gettime __PNR_clock_gettime
458+
#endif
447459

448460
#ifdef __NR_clock_gettime64
449461
#define __SNR_clock_gettime64 __NR_clock_gettime64
450462
#else
451463
#define __SNR_clock_gettime64 __PNR_clock_gettime64
452464
#endif
453465

466+
#ifdef __NR_clock_nanosleep
454467
#define __SNR_clock_nanosleep __NR_clock_nanosleep
468+
#else
469+
#define __SNR_clock_nanosleep __PNR_clock_nanosleep
470+
#endif
455471

456472
#ifdef __NR_clock_nanosleep_time64
457473
#define __SNR_clock_nanosleep_time64 __NR_clock_nanosleep_time64
@@ -715,7 +731,11 @@
715731
#define __SNR_ftruncate64 __PNR_ftruncate64
716732
#endif
717733

734+
#ifdef __NR_futex
718735
#define __SNR_futex __NR_futex
736+
#else
737+
#define __SNR_futex __PNR_futex
738+
#endif
719739

720740
#ifdef __NR_futex_time64
721741
#define __SNR_futex_time64 __NR_futex_time64
@@ -901,7 +921,11 @@
901921

902922
#define __SNR_gettid __NR_gettid
903923

924+
#ifdef __NR_gettimeofday
904925
#define __SNR_gettimeofday __NR_gettimeofday
926+
#else
927+
#define __SNR_gettimeofday __PNR_gettimeofday
928+
#endif
905929

906930
#ifdef __NR_getuid
907931
#define __SNR_getuid __NR_getuid
@@ -1055,7 +1079,11 @@
10551079

10561080
#define __SNR_lremovexattr __NR_lremovexattr
10571081

1082+
#ifdef __NR_lseek
10581083
#define __SNR_lseek __NR_lseek
1084+
#else
1085+
#define __SNR_lseek __PNR_lseek
1086+
#endif
10591087

10601088
#define __SNR_lsetxattr __NR_lsetxattr
10611089

@@ -1235,7 +1263,11 @@
12351263

12361264
#define __SNR_name_to_handle_at __NR_name_to_handle_at
12371265

1266+
#ifdef __NR_nanosleep
12381267
#define __SNR_nanosleep __NR_nanosleep
1268+
#else
1269+
#define __SNR_nanosleep __PNR_nanosleep
1270+
#endif
12391271

12401272
#ifdef __NR_newfstatat
12411273
#define __SNR_newfstatat __NR_newfstatat

include/seccomp.h.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,16 @@ struct scmp_arg_cmp {
215215
#endif /* EM_RISCV */
216216
#define AUDIT_ARCH_RISCV64 (EM_RISCV|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
217217
#endif /* AUDIT_ARCH_RISCV64 */
218+
219+
#ifndef AUDIT_ARCH_RISCV32
220+
#ifndef EM_RISCV
221+
#define EM_RISCV 243
222+
#endif /* EM_RISCV */
223+
#define AUDIT_ARCH_RISCV32 (EM_RISCV|__AUDIT_ARCH_LE)
224+
#endif /* AUDIT_ARCH_RISCV32 */
225+
218226
#define SCMP_ARCH_RISCV64 AUDIT_ARCH_RISCV64
227+
#define SCMP_ARCH_RISCV32 AUDIT_ARCH_RISCV32
219228

220229
/**
221230
* The SuperH architecture tokens

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ SOURCES_ALL = \
4040
arch-ppc.h arch-ppc.c \
4141
arch-ppc64.h arch-ppc64.c \
4242
arch-riscv64.h arch-riscv64.c \
43+
arch-riscv32.h arch-riscv32.c \
4344
arch-s390.h arch-s390.c \
4445
arch-s390x.h arch-s390x.c \
4546
arch-sh.h arch-sh.c \

src/arch-riscv32.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This library is free software; you can redistribute it and/or modify it
3+
* under the terms of version 2.1 of the GNU Lesser General Public License as
4+
* published by the Free Software Foundation.
5+
*
6+
* This library is distributed in the hope that it will be useful, but WITHOUT
7+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9+
* for more details.
10+
*
11+
* You should have received a copy of the GNU Lesser General Public License
12+
* along with this library; if not, see <http://www.gnu.org/licenses>.
13+
*/
14+
15+
#include <stdlib.h>
16+
#include <errno.h>
17+
#include <linux/audit.h>
18+
19+
#include "arch.h"
20+
#include "arch-riscv32.h"
21+
#include "syscalls.h"
22+
23+
ARCH_DEF(riscv32)
24+
25+
const struct arch_def arch_def_riscv32 = {
26+
.token = SCMP_ARCH_RISCV32,
27+
.token_bpf = AUDIT_ARCH_RISCV32,
28+
.size = ARCH_SIZE_32,
29+
.endian = ARCH_ENDIAN_LITTLE,
30+
.syscall_resolve_name_raw = riscv32_syscall_resolve_name,
31+
.syscall_resolve_num_raw = riscv32_syscall_resolve_num,
32+
.syscall_rewrite = NULL,
33+
.rule_add = NULL,
34+
};

src/arch-riscv32.h

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

src/arch-syscall-dump.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "arch-ppc.h"
4444
#include "arch-ppc64.h"
4545
#include "arch-riscv64.h"
46+
#include "arch-riscv32.h"
4647
#include "arch-s390.h"
4748
#include "arch-s390x.h"
4849
#include "arch-sh.h"
@@ -135,6 +136,9 @@ int main(int argc, char *argv[])
135136
case SCMP_ARCH_RISCV64:
136137
sys = riscv64_syscall_iterate(iter);
137138
break;
139+
case SCMP_ARCH_RISCV32:
140+
sys = riscv32_syscall_iterate(iter);
141+
break;
138142
case SCMP_ARCH_S390:
139143
sys = s390_syscall_iterate(iter);
140144
break;

0 commit comments

Comments
 (0)