Skip to content

Commit

Permalink
c18n: Port {libc,libthr}_c18n to RISC-V
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgao committed Jul 25, 2024
1 parent 57f570a commit 1987963
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ SUBDIR= ${SUBDIR_BOOTSTRAP} \
ncurses \
nss_tacplus

.if ${MACHINE_ABI:Mpurecap} && ${MACHINE_CPUARCH} == "aarch64"
.if ${MACHINE_ABI:Mpurecap} && (${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "riscv")
.if !defined(COMPAT_LIBCOMPAT) || ${COMPAT_LIBCOMPAT} == "64CB"
SUBDIR+= c18n
.endif
Expand Down
27 changes: 27 additions & 0 deletions lib/libc/riscv/gen/_setjmp.S
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,19 @@ ENTRY(_setjmp)
#endif

/* Return value */
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
cmove ca1, ca0
#endif
li a0, 0
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
/*
* Tail-call to save trusted stack state
*/
clgc ct0, _C_LABEL(c18n_get_trusted_stk)
cjr ct0
#else
cret
#endif
#else
/* Store the magic value and stack pointer */
ld t0, .Lmagic
Expand Down Expand Up @@ -140,7 +151,11 @@ ENTRY(_longjmp)
bne t0, t1, botch

/* Restore the stack pointer */
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
clc ca3, 16(ca0)
#else
clc csp, 16(ca0)
#endif
cincoffset ca0, ca0, (2 * 16)

/* Restore the general purpose registers and ra */
Expand Down Expand Up @@ -179,8 +194,20 @@ ENTRY(_longjmp)
#endif

/* Load the return value */
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
clc ca2, 0(ca0)
#endif
mv a0, a1
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
cmove ca1, ca3
/*
* Tail-call to restore trusted stack state
*/
clgc ct0, _C_LABEL(c18n_unwind_trusted_stk)
cjr ct0
#else
cret
#endif
#else
/* Check the magic value */
ld t0, 0(a0)
Expand Down
32 changes: 32 additions & 0 deletions lib/libc/riscv/gen/setjmp.S
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#include <machine/asm.h>
#include <machine/setjmp.h>

#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
.weak _rtld_setjmp
.weak _rtld_longjmp
#endif

ENTRY(setjmp)
#ifdef __CHERI_PURE_CAPABILITY__
cincoffset csp, csp, -(2 * 16)
Expand Down Expand Up @@ -95,8 +100,19 @@ ENTRY(setjmp)
#endif

/* Return value */
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
cmove ca1, ca0
#endif
li a0, 0
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
/*
* Tail-call to save trusted stack state
*/
clgc ct0, _C_LABEL(c18n_get_trusted_stk)
cjr ct0
#else
cret
#endif
#else
addi sp, sp, -(2 * 8)
sd a0, 0(sp)
Expand Down Expand Up @@ -186,7 +202,11 @@ ENTRY(longjmp)
cincoffset csp, csp, (3 * 16)

/* Restore the stack pointer */
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
clc ca3, 16(ca0)
#else
clc csp, 16(ca0)
#endif
cincoffset ca0, ca0, (2 * 16)

/* Restore the general purpose registers and ra */
Expand Down Expand Up @@ -225,8 +245,20 @@ ENTRY(longjmp)
#endif

/* Load the return value */
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
clc ca2, 0(ca0)
#endif
mv a0, a1
#if defined(__CHERI_PURE_CAPABILITY__) && defined(CHERI_LIB_C18N)
cmove ca1, ca3
/*
* Tail-call to restore trusted stack state
*/
clgc ct0, _C_LABEL(c18n_unwind_trusted_stk)
cjr ct0
#else
cret
#endif

botch:
clgc ct0, _C_LABEL(longjmperror)
Expand Down
7 changes: 5 additions & 2 deletions lib/libsys/riscv/Makefile.sys
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
SRCS+= __vdso_gettc.c \
sched_getcpu_gen.c

MDASM= cerror.S \
vfork.S
MDASM= cerror.S

.ifndef CHERI_LIB_C18N
MDASM+= vfork.S
.endif

0 comments on commit 1987963

Please sign in to comment.