diff --git a/lib/Makefile b/lib/Makefile index 2eac42a8b96a..ad9a768817ed 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -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 diff --git a/lib/libc/riscv/gen/_setjmp.S b/lib/libc/riscv/gen/_setjmp.S index 050113cb0e26..0d22ae268167 100644 --- a/lib/libc/riscv/gen/_setjmp.S +++ b/lib/libc/riscv/gen/_setjmp.S @@ -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 @@ -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 */ @@ -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) diff --git a/lib/libc/riscv/gen/setjmp.S b/lib/libc/riscv/gen/setjmp.S index 672f7f50482b..7421f2cf99ea 100644 --- a/lib/libc/riscv/gen/setjmp.S +++ b/lib/libc/riscv/gen/setjmp.S @@ -35,6 +35,11 @@ #include #include +#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) @@ -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) @@ -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 */ @@ -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) diff --git a/lib/libsys/riscv/Makefile.sys b/lib/libsys/riscv/Makefile.sys index 2ff84735f484..5cfd48643944 100644 --- a/lib/libsys/riscv/Makefile.sys +++ b/lib/libsys/riscv/Makefile.sys @@ -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