Skip to content

Commit 6d127a0

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 5822e50 commit 6d127a0

32 files changed

Lines changed: 208 additions & 7 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
Luca Bruno <lucab@debian.org>

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@
275275
#define __PNR_ppoll -10241
276276
#define __PNR_renameat -10242
277277
#define __PNR_riscv_flush_icache -10243
278+
#define __PNR_fstat -10244
279+
#define __PNR_futex -10245
280+
#define __PNR_nanosleep -10246
281+
#define __PNR_lseek -10247
282+
#define __PNR_clock_gettime -10248
283+
#define __PNR_clock_nanosleep -10249
284+
#define __PNR_gettimeofday -10250
278285

279286
/*
280287
* libseccomp syscall definitions
@@ -442,15 +449,23 @@
442449
#define __SNR_clock_getres_time64 __PNR_clock_getres_time64
443450
#endif
444451

452+
#ifdef __NR_clock_gettime
445453
#define __SNR_clock_gettime __NR_clock_gettime
454+
#else
455+
#define __SNR_clock_gettime __PNR_clock_gettime
456+
#endif
446457

447458
#ifdef __NR_clock_gettime64
448459
#define __SNR_clock_gettime64 __NR_clock_gettime64
449460
#else
450461
#define __SNR_clock_gettime64 __PNR_clock_gettime64
451462
#endif
452463

464+
#ifdef __NR_clock_nanosleep
453465
#define __SNR_clock_nanosleep __NR_clock_nanosleep
466+
#else
467+
#define __SNR_clock_nanosleep __PNR_clock_nanosleep
468+
#endif
454469

455470
#ifdef __NR_clock_nanosleep_time64
456471
#define __SNR_clock_nanosleep_time64 __NR_clock_nanosleep_time64
@@ -710,7 +725,11 @@
710725
#define __SNR_ftruncate64 __PNR_ftruncate64
711726
#endif
712727

728+
#ifdef __NR_futex
713729
#define __SNR_futex __NR_futex
730+
#else
731+
#define __SNR_futex __PNR_futex
732+
#endif
714733

715734
#ifdef __NR_futex_time64
716735
#define __SNR_futex_time64 __NR_futex_time64
@@ -896,7 +915,11 @@
896915

897916
#define __SNR_gettid __NR_gettid
898917

918+
#ifdef __NR_gettimeofday
899919
#define __SNR_gettimeofday __NR_gettimeofday
920+
#else
921+
#define __SNR_gettimeofday __PNR_gettimeofday
922+
#endif
900923

901924
#ifdef __NR_getuid
902925
#define __SNR_getuid __NR_getuid
@@ -1046,7 +1069,11 @@
10461069

10471070
#define __SNR_lremovexattr __NR_lremovexattr
10481071

1072+
#ifdef __NR_lseek
10491073
#define __SNR_lseek __NR_lseek
1074+
#else
1075+
#define __SNR_lseek __PNR_lseek
1076+
#endif
10501077

10511078
#define __SNR_lsetxattr __NR_lsetxattr
10521079

@@ -1218,7 +1245,11 @@
12181245

12191246
#define __SNR_name_to_handle_at __NR_name_to_handle_at
12201247

1248+
#ifdef __NR_nanosleep
12211249
#define __SNR_nanosleep __NR_nanosleep
1250+
#else
1251+
#define __SNR_nanosleep __PNR_nanosleep
1252+
#endif
12221253

12231254
#ifdef __NR_newfstatat
12241255
#define __SNR_newfstatat __NR_newfstatat

include/seccomp.h.in

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

219228
/**
220229
* The SuperH architecture tokens

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ SOURCES_ALL = \
4444
arch-ppc.h arch-ppc.c \
4545
arch-ppc64.h arch-ppc64.c \
4646
arch-riscv64.h arch-riscv64.c \
47+
arch-riscv32.h arch-riscv32.c \
4748
arch-s390.h arch-s390.c \
4849
arch-s390x.h arch-s390x.c \
4950
arch-sh.h arch-sh.c \

src/arch-riscv32.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
22+
const struct arch_def arch_def_riscv32 = {
23+
.token = SCMP_ARCH_RISCV32,
24+
.token_bpf = AUDIT_ARCH_RISCV32,
25+
.size = ARCH_SIZE_32,
26+
.endian = ARCH_ENDIAN_LITTLE,
27+
.syscall_resolve_name = riscv32_syscall_resolve_name,
28+
.syscall_resolve_num = riscv32_syscall_resolve_num,
29+
.syscall_rewrite = NULL,
30+
.rule_add = NULL,
31+
};

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)