@@ -26,8 +26,26 @@ struct CompartmentInfo {
2626
2727 static int unwindIfAtBoundary (R ®isters) {
2828#ifdef _LIBUNWIND_HAS_CHERI_LIB_C18N
29- #ifdef _LIBUNWIND_TARGET_AARCH64
3029 struct dl_c18n_compart_state state;
30+
31+ #ifdef _LIBUNWIND_TARGET_AARCH64
32+ static constexpr int fp_num = UNW_AARCH64_FP;
33+ static constexpr int callee_saved[] = {
34+ UNW_ARM64_C19, UNW_ARM64_C20, UNW_ARM64_C21, UNW_ARM64_C22, UNW_ARM64_C23,
35+ UNW_ARM64_C24, UNW_ARM64_C26, UNW_ARM64_C27, UNW_ARM64_C28, UNW_ARM64_C29
36+ };
37+ #elif defined(_LIBUNWIND_TARGET_RISCV)
38+ static constexpr int fp_num = UNW_RISCV_X8;
39+ static constexpr int callee_saved[] = {
40+ UNW_RISCV_X9, UNW_RISCV_X18, UNW_RISCV_X19, UNW_RISCV_X20, UNW_RISCV_X21,
41+ UNW_RISCV_X22, UNW_RISCV_X23, UNW_RISCV_X24, UNW_RISCV_X25, UNW_RISCV_X26,
42+ UNW_RISCV_X27
43+ };
44+ #endif
45+ static_assert (sizeof (callee_saved) / sizeof (*callee_saved) ==
46+ sizeof (state.regs ) / sizeof (*state.regs ),
47+ " unexpected number of saved registers" );
48+
3149 pint_t pc = registers.getIP ();
3250 pint_t tf = registers.getTrustedStack ();
3351
@@ -41,7 +59,7 @@ struct CompartmentInfo {
4159 registers.setTrustedStack (tf);
4260 CHERI_DBG (" C18N: SET TRUSTED STACK %#p\n " , (void *)tf);
4361
44- registers.setFP ( (pint_t )state.fp );
62+ registers.setCapabilityRegister (fp_num, (pint_t )state.fp );
4563 CHERI_DBG (" C18N: SET FP %#p\n " , state.fp );
4664
4765 registers.setSP ((pint_t )state.sp );
@@ -51,13 +69,12 @@ struct CompartmentInfo {
5169 CHERI_DBG (" C18N: SET IP: %#p\n " , state.pc );
5270
5371 for (size_t i = 0 ; i < sizeof (state.regs ) / sizeof (*state.regs ); ++i) {
54- registers.setCapabilityRegister (UNW_ARM64_C19 + i , (pint_t )state.regs [i]);
55- CHERI_DBG (" C18N: SET REGISTER: %lu (%s): %#p\n " ,
56- UNW_ARM64_C19 + i ,
57- registers.getRegisterName (UNW_ARM64_C19 + i ),
72+ registers.setCapabilityRegister (callee_saved[i] , (pint_t )state.regs [i]);
73+ CHERI_DBG (" C18N: SET REGISTER: %d (%s): %#p\n " ,
74+ callee_saved[i] ,
75+ registers.getRegisterName (callee_saved[i] ),
5876 state.regs [i]);
5977 }
60- #endif
6178#endif // _LIBUNWIND_HAS_CHERI_LIB_C18N
6279 return UNW_STEP_SUCCESS;
6380 }
0 commit comments