Skip to content

Commit c610531

Browse files
ancientmodernfelicitiapicostove
authored andcommitted
compel: add riscv64 support
Co-authored-by: Yixue Zhao <[email protected]> Co-authored-by: stove <[email protected]> Signed-off-by: Haorong Lu <[email protected]> --- - rebased - added a membarrier() to syscall table (fix authored by Cryolitia PukNgae) Signed-off-by: PukNgae Cryolitia <[email protected]> Signed-off-by: Alexander Mikhalitsyn <[email protected]>
1 parent a0a0400 commit c610531

31 files changed

+1141
-3
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ endif
1919

2020
#
2121
# Supported Architectures
22-
ifneq ($(filter-out x86 arm aarch64 ppc64 s390 mips loongarch64,$(ARCH)),)
22+
ifneq ($(filter-out x86 arm aarch64 ppc64 s390 mips loongarch64 riscv64,$(ARCH)),)
2323
$(error "The architecture $(ARCH) isn't supported")
2424
endif
2525

@@ -84,6 +84,10 @@ ifeq ($(ARCH),loongarch64)
8484
DEFINES := -DCONFIG_LOONGARCH64
8585
endif
8686

87+
ifeq ($(ARCH),riscv64)
88+
DEFINES := -DCONFIG_RISCV64
89+
endif
90+
8791
#
8892
# CFLAGS_PIE:
8993
#

compel/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ ifeq ($(ARCH),x86)
3232
lib-y += arch/$(ARCH)/src/lib/thread_area.o
3333
endif
3434

35-
# handle_elf() has no support of ELF relocations on ARM (yet?)
36-
ifneq ($(filter arm aarch64 loongarch64,$(ARCH)),)
35+
# handle_elf() has no support of ELF relocations on ARM and RISCV64 (yet?)
36+
ifneq ($(filter arm aarch64 loongarch64 riscv64,$(ARCH)),)
3737
CFLAGS += -DNO_RELOCS
3838
HOSTCFLAGS += -DNO_RELOCS
3939
endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef __ASM_PROLOGUE_H__
2+
#define __ASM_PROLOGUE_H__
3+
4+
#ifndef __ASSEMBLY__
5+
6+
#include <sys/types.h>
7+
#include <sys/socket.h>
8+
#include <sys/un.h>
9+
10+
#include <errno.h>
11+
12+
#define sys_recv(sockfd, ubuf, size, flags) sys_recvfrom(sockfd, ubuf, size, flags, NULL, NULL)
13+
14+
typedef struct prologue_init_args {
15+
struct sockaddr_un ctl_sock_addr;
16+
unsigned int ctl_sock_addr_len;
17+
18+
unsigned int arg_s;
19+
void *arg_p;
20+
21+
void *sigframe;
22+
} prologue_init_args_t;
23+
24+
#endif /* __ASSEMBLY__ */
25+
26+
/*
27+
* Reserve enough space for sigframe.
28+
*
29+
* FIXME It is rather should be taken from sigframe header.
30+
*/
31+
#define PROLOGUE_SGFRAME_SIZE 4096
32+
33+
#define PROLOGUE_INIT_ARGS_SIZE 1024
34+
35+
#endif /* __ASM_PROLOGUE_H__ */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef COMPEL_ARCH_SYSCALL_TYPES_H__
2+
#define COMPEL_ARCH_SYSCALL_TYPES_H__
3+
4+
#define SA_RESTORER 0x04000000
5+
6+
typedef void rt_signalfn_t(int, siginfo_t *, void *);
7+
typedef rt_signalfn_t *rt_sighandler_t;
8+
9+
typedef void rt_restorefn_t(void);
10+
typedef rt_restorefn_t *rt_sigrestore_t;
11+
12+
#define _KNSIG 64 // number of signals
13+
#define _NSIG_BPW 64 // number of signals per word
14+
15+
#define _KNSIG_WORDS (_KNSIG / _NSIG_BPW)
16+
17+
typedef struct {
18+
unsigned long sig[_KNSIG_WORDS];
19+
} k_rtsigset_t;
20+
21+
typedef struct {
22+
rt_sighandler_t rt_sa_handler;
23+
unsigned long rt_sa_flags;
24+
rt_sigrestore_t rt_sa_restorer;
25+
k_rtsigset_t rt_sa_mask;
26+
} rt_sigaction_t;
27+
28+
#endif /* COMPEL_ARCH_SYSCALL_TYPES_H__ */
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef __COMPEL_ARCH_FEATURES_H
2+
#define __COMPEL_ARCH_FEATURES_H
3+
4+
#endif /* __COMPEL_ARCH_FEATURES_H */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "common/asm/linkage.h"
2+
3+
.section .head.text, "ax"
4+
ENTRY(__export_parasite_head_start)
5+
jal parasite_service
6+
ebreak
7+
END(__export_parasite_head_start)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
ccflags-y += -iquote $(PLUGIN_ARCH_DIR)/std/syscalls/
2+
asflags-y += -iquote $(PLUGIN_ARCH_DIR)/std/syscalls/
3+
4+
sys-types := $(obj)/include/uapi/std/syscall-types.h
5+
sys-codes := $(obj)/include/uapi/std/syscall-codes.h
6+
sys-proto := $(obj)/include/uapi/std/syscall.h
7+
8+
sys-def := $(PLUGIN_ARCH_DIR)/std/syscalls/syscall.def
9+
sys-asm-common-name := std/syscalls/syscall-common.S
10+
sys-asm-common := $(PLUGIN_ARCH_DIR)/$(sys-asm-common-name)
11+
sys-asm-types := $(obj)/include/uapi/std/asm/syscall-types.h
12+
sys-exec-tbl = $(PLUGIN_ARCH_DIR)/std/sys-exec-tbl.c
13+
14+
sys-gen := $(PLUGIN_ARCH_DIR)/std/syscalls/gen-syscalls.pl
15+
sys-gen-tbl := $(PLUGIN_ARCH_DIR)/std/syscalls/gen-sys-exec-tbl.pl
16+
17+
sys-asm := ./$(PLUGIN_ARCH_DIR)/std/syscalls/syscalls.S
18+
std-lib-y += $(sys-asm:.S=).o
19+
20+
ifeq ($(ARCH),arm)
21+
arch_bits := 32
22+
else
23+
arch_bits := 64
24+
endif
25+
26+
sys-exec-tbl := sys-exec-tbl.c
27+
28+
$(sys-asm) $(sys-types) $(sys-codes) $(sys-proto): $(sys-gen) $(sys-def) $(sys-asm-common) $(sys-asm-types)
29+
$(E) " GEN " $@
30+
$(Q) perl \
31+
$(sys-gen) \
32+
$(sys-def) \
33+
$(sys-codes) \
34+
$(sys-proto) \
35+
$(sys-asm) \
36+
$(sys-asm-common-name) \
37+
$(sys-types) \
38+
$(arch_bits)
39+
40+
$(sys-asm:.S=).o: $(sys-asm)
41+
42+
$(sys-exec-tbl): $(sys-gen-tbl) $(sys-def)
43+
$(E) " GEN " $@
44+
$(Q) perl \
45+
$(sys-gen-tbl) \
46+
$(sys-def) \
47+
$(sys-exec-tbl) \
48+
$(arch_bits)
49+
50+
$(sys-asm-types): $(PLUGIN_ARCH_DIR)/include/asm/syscall-types.h
51+
$(call msg-gen, $@)
52+
$(Q) ln -s ../../../../../../$(PLUGIN_ARCH_DIR)/include/asm/syscall-types.h $(sys-asm-types)
53+
$(Q) ln -s ../../../../../$(PLUGIN_ARCH_DIR)/std/syscalls/syscall-aux.S $(obj)/include/uapi/std/syscall-aux.S
54+
$(Q) ln -s ../../../../../$(PLUGIN_ARCH_DIR)/std/syscalls/syscall-aux.h $(obj)/include/uapi/std/syscall-aux.h
55+
56+
std-headers-deps += $(sys-asm) $(sys-codes) $(sys-proto) $(sys-asm-types) $(sys-codes)
57+
mrproper-y += $(std-headers-deps)
58+
mrproper-y += $(obj)/include/uapi/std/syscall-aux.S
59+
mrproper-y += $(obj)/include/uapi/std/syscall-aux.h
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
my $in = $ARGV[0];
7+
my $tblout = $ARGV[1];
8+
my $bits = $ARGV[2];
9+
10+
my $code = "code$bits";
11+
12+
open TBLOUT, ">", $tblout or die $!;
13+
open IN, "<", $in or die $!;
14+
15+
print TBLOUT "/* Autogenerated, don't edit */\n";
16+
print TBLOUT "static struct syscall_exec_desc sc_exec_table[] = {\n";
17+
18+
for (<IN>) {
19+
if ($_ =~ /\#/) {
20+
next;
21+
}
22+
23+
my $sys_name;
24+
my $sys_num;
25+
26+
if (/(?<name>\S+)\s+(?<alias>\S+)\s+(?<code64>\d+|\!)\s+(?<code32>(?:\d+|\!))\s+\((?<args>.+)\)/) {
27+
$sys_name = $+{alias};
28+
} elsif (/(?<name>\S+)\s+(?<code64>\d+|\!)\s+(?<code32>(?:\d+|\!))\s+\((?<args>.+)\)/) {
29+
$sys_name = $+{name};
30+
} else {
31+
unlink $tblout;
32+
die "Invalid syscall definition file: invalid entry $_\n";
33+
}
34+
35+
$sys_num = $+{$code};
36+
37+
if ($sys_num ne "!") {
38+
print TBLOUT "SYSCALL($sys_name, $sys_num)\n";
39+
}
40+
}
41+
42+
print TBLOUT " { }, /* terminator */";
43+
print TBLOUT "};"
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
use warnings;
5+
6+
my $in = $ARGV[0];
7+
my $codesout = $ARGV[1];
8+
my $codes = $ARGV[1];
9+
$codes =~ s/.*include\/uapi\//compel\/plugins\//g;
10+
my $protosout = $ARGV[2];
11+
my $protos = $ARGV[2];
12+
$protos =~ s/.*include\/uapi\//compel\/plugins\//g;
13+
my $asmout = $ARGV[3];
14+
my $asmcommon = $ARGV[4];
15+
my $prototypes = $ARGV[5];
16+
$prototypes =~ s/.*include\/uapi\//compel\/plugins\//g;
17+
my $bits = $ARGV[6];
18+
19+
my $codesdef = $codes;
20+
$codesdef =~ tr/.\-\//_/;
21+
my $protosdef = $protos;
22+
$protosdef =~ tr/.\-\//_/;
23+
my $code = "code$bits";
24+
my $need_aux = 0;
25+
26+
unlink $codesout;
27+
unlink $protosout;
28+
unlink $asmout;
29+
30+
open CODESOUT, ">", $codesout or die $!;
31+
open PROTOSOUT, ">", $protosout or die $!;
32+
open ASMOUT, ">", $asmout or die $!;
33+
open IN, "<", $in or die $!;
34+
35+
print CODESOUT <<"END";
36+
/* Autogenerated, don't edit */
37+
#ifndef $codesdef
38+
#define $codesdef
39+
END
40+
41+
print PROTOSOUT <<"END";
42+
/* Autogenerated, don't edit */
43+
#ifndef $protosdef
44+
#define $protosdef
45+
#include <$prototypes>
46+
#include <$codes>
47+
END
48+
49+
print ASMOUT <<"END";
50+
/* Autogenerated, don't edit */
51+
#include <$codes>
52+
#include "$asmcommon"
53+
END
54+
55+
56+
for (<IN>) {
57+
if ($_ =~ /\#/) {
58+
next;
59+
}
60+
61+
my $code_macro;
62+
my $sys_macro;
63+
my $sys_name;
64+
65+
if (/(?<name>\S+)\s+(?<alias>\S+)\s+(?<code64>\d+|\!)\s+(?<code32>(?:\d+|\!))\s+\((?<args>.+)\)/) {
66+
$code_macro = "__NR_$+{name}";
67+
$sys_macro = "SYS_$+{name}";
68+
$sys_name = "sys_$+{alias}";
69+
} elsif (/(?<name>\S+)\s+(?<code64>\d+|\!)\s+(?<code32>(?:\d+|\!))\s+\((?<args>.+)\)/) {
70+
$code_macro = "__NR_$+{name}";
71+
$sys_macro = "SYS_$+{name}";
72+
$sys_name = "sys_$+{name}";
73+
} else {
74+
unlink $codesout;
75+
unlink $protosout;
76+
unlink $asmout;
77+
78+
die "Invalid syscall definition file: invalid entry $_\n";
79+
}
80+
81+
if ($+{$code} ne "!") {
82+
print CODESOUT "#ifndef $code_macro\n#define $code_macro $+{$code}\n#endif\n";
83+
print CODESOUT "#ifndef $sys_macro\n#define $sys_macro $code_macro\n#endif\n";
84+
print ASMOUT "syscall $sys_name, $code_macro\n";
85+
86+
} else {
87+
$need_aux = 1;
88+
}
89+
90+
print PROTOSOUT "extern long $sys_name($+{args});\n";
91+
}
92+
93+
if ($need_aux == 1) {
94+
print ASMOUT "#include <compel/plugins/std/syscall-aux.S>\n";
95+
print CODESOUT "#include <compel/plugins/std/syscall-aux.h>\n";
96+
}
97+
98+
print CODESOUT "#endif /* $codesdef */";
99+
print PROTOSOUT "#endif /* $protosdef */";
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* This source contains emulation of syscalls
3+
* that are not implemented in the riscv64 Linux kernel
4+
*/
5+
6+
ENTRY(sys_open)
7+
add a3, x0, a2
8+
add a2, x0, a1
9+
add a1, x0, a0
10+
addi a0, x0, -100
11+
j sys_openat
12+
END(sys_open)
13+
14+
15+
ENTRY(sys_mkdir)
16+
add a3,x0, a2
17+
add a2, x0, a1
18+
add a1, x0, a0
19+
addi a0, x0, -100
20+
j sys_mkdirat
21+
END(sys_mkdir)
22+
23+
24+
ENTRY(sys_rmdir)
25+
addi a2, x0, 0x200 // flags = AT_REMOVEDIR
26+
add a1, x0, a0
27+
addi a0, x0, -100
28+
j sys_unlinkat
29+
END(sys_rmdir)
30+
31+
32+
ENTRY(sys_unlink)
33+
addi a2, x0, 0 // flags = 0
34+
add a1, x0, a0
35+
addi a0, x0, -100
36+
j sys_unlinkat
37+
END(sys_unlink)

0 commit comments

Comments
 (0)