From d2a35efb49c4c679c7128935af9c58dcb28f515d Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Thu, 16 Apr 2026 10:36:34 +0200 Subject: [PATCH 1/2] [CHERIoT] Add support for save/restore of registers to dspill CSRs --- src/target/riscv/opcodes.h | 12 ++++++ src/target/riscv/program.c | 81 ++++++++++++++++++++++++++++++++---- src/target/riscv/riscv-013.c | 13 +++++- 3 files changed, 96 insertions(+), 10 deletions(-) diff --git a/src/target/riscv/opcodes.h b/src/target/riscv/opcodes.h index 7d17e6eea0..7fd04d8967 100644 --- a/src/target/riscv/opcodes.h +++ b/src/target/riscv/opcodes.h @@ -332,3 +332,15 @@ static uint32_t vslide1down_vx(unsigned int vd, unsigned int vs2, return ((vm & 1) << 25) | inst_rs2(vs2) | inst_rs1(rs1) | inst_rd(vd) | MATCH_VSLIDE1DOWN_VX; } +static uint32_t ct_cspecialw(unsigned int scr, unsigned int cs1) __attribute__((unused)); +static uint32_t ct_cspecialw(unsigned int scr, unsigned int cs1) +{ + return (0x1 << 25) | ((scr & 0x1F) << 20) | inst_rs1(cs1) | 0x5B; +} + + +static uint32_t ct_cspecialr(unsigned int cd, unsigned int scr) __attribute__((unused)); +static uint32_t ct_cspecialr(unsigned int cd, unsigned int scr) +{ + return (0x1 << 25) | ((scr & 0x1F) << 20) | inst_rd(cd) | 0x5B; +} diff --git a/src/target/riscv/program.c b/src/target/riscv/program.c index 5551a845b9..957b760d8a 100644 --- a/src/target/riscv/program.c +++ b/src/target/riscv/program.c @@ -40,22 +40,82 @@ int riscv_program_write(struct riscv_program *program) return ERROR_OK; } -/** Add ebreak and execute the program. */ -int riscv_program_exec(struct riscv_program *p, struct target *t) -{ - // Disable program buffer execution on CHERIOT until we have a way - // to save/restore capabilities. - struct riscv_info *info = t->arch_info; - if (info->cheriot) { +static unsigned int cheriot_get_saverestore_csr(enum gdb_regno regid) { + unsigned offset = regid - (GDB_REGNO_ZERO + 1); + return offset + 9; +} + +int cheriot_save_register(struct target *target, enum gdb_regno regid) { + struct riscv_info *info = target->arch_info; + if (!info->cheriot) { + return ERROR_FAIL; + } + + if (regid < GDB_REGNO_ZERO + 1 || regid > GDB_REGNO_XPR15) { + return ERROR_FAIL; + } + + unsigned scr = cheriot_get_saverestore_csr(regid); + + struct riscv_program program; + riscv_program_init(&program, target); + riscv_program_insert(&program, ct_cspecialw(scr, regid - GDB_REGNO_ZERO)); + + if (riscv_program_exec(&program, target) != ERROR_OK) { + return ERROR_FAIL; + } + + return ERROR_OK; +} + +int cheriot_restore_register(struct target *target, enum gdb_regno regid) { + struct riscv_info *info = target->arch_info; + if (!info->cheriot) { return ERROR_FAIL; } + if (regid < GDB_REGNO_ZERO + 1 || regid > GDB_REGNO_XPR15) { + return ERROR_FAIL; + } + + unsigned scr = cheriot_get_saverestore_csr(regid); + + struct riscv_program program; + riscv_program_init(&program, target); + riscv_program_insert(&program, ct_cspecialr(regid - GDB_REGNO_ZERO, scr)); + // NOTE: Intentionally do not set writes_xreg[i], even though we do, + // because we don't want to recursively trigger another save/restore. + + if (riscv_program_exec(&program, target) != ERROR_OK) { + return ERROR_FAIL; + } + + // Invalidate the register cache + // FIXME: This could be done fine-grained. + register_cache_invalidate(target->reg_cache); + + return ERROR_OK; +} + +/** Add ebreak and execute the program. */ +int riscv_program_exec(struct riscv_program *p, struct target *t) +{ keep_alive(); + struct riscv_info *info = t->arch_info; riscv_reg_t saved_registers[GDB_REGNO_XPR31 + 1]; for (size_t i = GDB_REGNO_ZERO + 1; i <= GDB_REGNO_XPR31; ++i) { if (p->writes_xreg[i]) { LOG_DEBUG("Saving register %d as used by program", (int)i); + if (info->cheriot) { + // CHERIoT capabilities cannot be saved to host. + // Use an on-device store for them instead. + int result = cheriot_save_register(t, i); + if (result != ERROR_OK) + return result; + continue; + } + int result = riscv_get_register(t, &saved_registers[i], i); if (result != ERROR_OK) return result; @@ -83,8 +143,13 @@ int riscv_program_exec(struct riscv_program *p, struct target *t) p->debug_buffer[i] = riscv_read_debug_buffer(t, i); for (size_t i = GDB_REGNO_ZERO; i <= GDB_REGNO_XPR31; ++i) - if (p->writes_xreg[i]) + if (p->writes_xreg[i]) { + if (info->cheriot) { + cheriot_restore_register(t, i); + continue; + } riscv_set_register(t, i, saved_registers[i]); + } return ERROR_OK; } diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 9f94eb4b1c..847f3f7963 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -3360,6 +3360,10 @@ static int read_memory_progbuf_inner(struct target *target, target_addr_t addres return result; } +int cheriot_save_register(struct target *target, enum gdb_regno regid); +int cheriot_restore_register(struct target *target, enum gdb_regno regid); + + /* Only need to save/restore one GPR to read a single word, and the progbuf * program doesn't need to increment. */ static int read_memory_progbuf_one(struct target *target, target_addr_t address, @@ -3373,7 +3377,10 @@ static int read_memory_progbuf_one(struct target *target, target_addr_t address, uint64_t s0; int result = ERROR_FAIL; - if (register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK) + RISCV_INFO(info); + if (info->cheriot && cheriot_save_register(target, GDB_REGNO_S0) != ERROR_OK) + goto restore_mstatus; + if (!info->cheriot && register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK) goto restore_mstatus; /* Write the program (load, increment) */ @@ -3423,7 +3430,9 @@ static int read_memory_progbuf_one(struct target *target, target_addr_t address, result = ERROR_OK; restore_s0: - if (riscv_set_register(target, GDB_REGNO_S0, s0) != ERROR_OK) + if (!info->cheriot && riscv_set_register(target, GDB_REGNO_S0, s0) != ERROR_OK) + result = ERROR_FAIL; + if (info->cheriot && cheriot_restore_register(target, GDB_REGNO_S0) != ERROR_OK) result = ERROR_FAIL; restore_mstatus: From acd65b06398c1e3d0ad314a6ac3c8df367493238 Mon Sep 17 00:00:00 2001 From: Aurore Poirier Date: Tue, 9 Jun 2026 16:57:46 +0200 Subject: [PATCH 2/2] riscv/cheriot: Added cheriot hint tcl command `riscv_info.cheriot` is now an enum instead of a boolean that can be filled with the `riscv enable_cheriot [variant]` tcl command --- src/target/riscv/program.c | 13 +++++-- src/target/riscv/riscv-013.c | 21 ++++------- src/target/riscv/riscv.c | 72 ++++++++++++++++++++++++++++++++++-- src/target/riscv/riscv.h | 16 +++++++- 4 files changed, 98 insertions(+), 24 deletions(-) diff --git a/src/target/riscv/program.c b/src/target/riscv/program.c index 957b760d8a..9f109ec57e 100644 --- a/src/target/riscv/program.c +++ b/src/target/riscv/program.c @@ -47,7 +47,7 @@ static unsigned int cheriot_get_saverestore_csr(enum gdb_regno regid) { int cheriot_save_register(struct target *target, enum gdb_regno regid) { struct riscv_info *info = target->arch_info; - if (!info->cheriot) { + if (!cheriot_variant_can_spill_registers(info)) { return ERROR_FAIL; } @@ -70,7 +70,7 @@ int cheriot_save_register(struct target *target, enum gdb_regno regid) { int cheriot_restore_register(struct target *target, enum gdb_regno regid) { struct riscv_info *info = target->arch_info; - if (!info->cheriot) { + if (!cheriot_variant_can_spill_registers(info)) { return ERROR_FAIL; } @@ -107,7 +107,12 @@ int riscv_program_exec(struct riscv_program *p, struct target *t) for (size_t i = GDB_REGNO_ZERO + 1; i <= GDB_REGNO_XPR31; ++i) { if (p->writes_xreg[i]) { LOG_DEBUG("Saving register %d as used by program", (int)i); - if (info->cheriot) { + if (cheriot_variant_is_cheriot(info)) { + if (!cheriot_variant_can_spill_registers(info)) { + LOG_ERROR("Cannot save register on early cheriot versions"); + // CHERIoT registers cannot be saved in earliest board versions + return ERROR_FAIL; + } // CHERIoT capabilities cannot be saved to host. // Use an on-device store for them instead. int result = cheriot_save_register(t, i); @@ -144,7 +149,7 @@ int riscv_program_exec(struct riscv_program *p, struct target *t) for (size_t i = GDB_REGNO_ZERO; i <= GDB_REGNO_XPR31; ++i) if (p->writes_xreg[i]) { - if (info->cheriot) { + if (cheriot_variant_can_spill_registers(info)) { cheriot_restore_register(t, i); continue; } diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 847f3f7963..105aa3ab1d 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -1742,18 +1742,11 @@ static int examine(struct target *target) else r->xlen = 32; - - /* Probe for CHERIOT by checking for marchid == 0xce1 - Perhaps this should check mvendorid as well? */ - uint64_t marchid = 0; - result = register_read_abstract(target, &marchid, CSR_MARCHID+GDB_REGNO_CSR0, 32); - if (result == ERROR_OK && marchid == 0xCE1) { - LOG_INFO("CHERIOT detected"); - r->cheriot = true; - } + // FIXME: First versions on CHERIOT chips use placeholders. With later + // ones, we should probe for CHERIOT mvendorid - marchid pairs /* Force XLEN to 32 on CHERIOT. It will normally be detected as 64, due to cap registers being 64-bits. */ - if (r->cheriot) { + if (cheriot_variant_is_cheriot(r)) { r->xlen = 32; } @@ -3378,9 +3371,9 @@ static int read_memory_progbuf_one(struct target *target, target_addr_t address, int result = ERROR_FAIL; RISCV_INFO(info); - if (info->cheriot && cheriot_save_register(target, GDB_REGNO_S0) != ERROR_OK) + if (cheriot_variant_can_spill_registers(info) && cheriot_save_register(target, GDB_REGNO_S0) != ERROR_OK) goto restore_mstatus; - if (!info->cheriot && register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK) + if (!cheriot_variant_can_spill_registers(info) && register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK) goto restore_mstatus; /* Write the program (load, increment) */ @@ -3430,9 +3423,9 @@ static int read_memory_progbuf_one(struct target *target, target_addr_t address, result = ERROR_OK; restore_s0: - if (!info->cheriot && riscv_set_register(target, GDB_REGNO_S0, s0) != ERROR_OK) + if (!cheriot_variant_can_spill_registers(info) && riscv_set_register(target, GDB_REGNO_S0, s0) != ERROR_OK) result = ERROR_FAIL; - if (info->cheriot && cheriot_restore_register(target, GDB_REGNO_S0) != ERROR_OK) + if (cheriot_variant_can_spill_registers(info) && cheriot_restore_register(target, GDB_REGNO_S0) != ERROR_OK) result = ERROR_FAIL; restore_mstatus: diff --git a/src/target/riscv/riscv.c b/src/target/riscv/riscv.c index 62cf2cddeb..5b106a89f4 100644 --- a/src/target/riscv/riscv.c +++ b/src/target/riscv/riscv.c @@ -2329,6 +2329,26 @@ int riscv_openocd_step(struct target *target, bool current, return out; } +/*** CHERIOT Helper Functions ***/ +int cheriot_variant_is_cheriot(struct riscv_info * info) { + return info->cheriot >= CHERIOT_MPW_1; +} + +int cheriot_variant_can_access_cap_gpr(struct riscv_info * info) { + return info->cheriot >= CHERIOT_MPW_1; +} + +int cheriot_variant_can_spill_registers(struct riscv_info * info) { + return info->cheriot >= CHERIOT_MPW_2; +} + +int riscv_can_access_mtvec_mepcc(struct riscv_info * info) { + /* MTVEC and MEPC are inaccessible on CHERIOT. They must be access via + the special cap registers MTCC and MEPCC respectively. */ + // TODO: check on Sonata One + return info->cheriot == CHERIOT_NONE; +} + /* Command Handlers */ COMMAND_HANDLER(riscv_set_command_timeout_sec) { @@ -2859,6 +2879,44 @@ COMMAND_HANDLER(handle_info) return 0; } +COMMAND_HANDLER(riscv_enable_cheriot) +{ + struct target *target = get_current_target(CMD_CTX); + struct enable_cheriot_mapping { + char* key; + enum cheriot_variant value; + }; + static const struct enable_cheriot_mapping mappings[] = { + {"mpw1", CHERIOT_MPW_1}, + {"mpw2", CHERIOT_MPW_2}, + {"iceni", CHERIOT_MPW_2}, + {"sonata", CHERIOT_SONATA}, + {NULL, CHERIOT_NONE}, + }; + + RISCV_INFO(r); + if (CMD_ARGC < 1) { + LOG_WARNING("cheriot enabled, but no cheriot target given, defaulting to iceni"); + r->cheriot = CHERIOT_MPW_2; + return ERROR_OK; + } + if (CMD_ARGC > 1) { + command_print(CMD, "Command takes at most 1 parameter"); + return ERROR_COMMAND_ARGUMENT_INVALID; + } + for(struct enable_cheriot_mapping *ecm = mappings; ecm->key != NULL; ecm++) { + if (strcmp(ecm->key, CMD_ARGV[0]) == 0) { + r->cheriot = ecm->value; + return ERROR_OK; + } + } + + LOG_ERROR("Unknown argument '%s'. " + "Must be one of: 'MPW1', 'MPW2' (or 'iceni') or 'sonata'.", CMD_ARGV[0]); + return ERROR_COMMAND_SYNTAX_ERROR; +} + + static const struct command_registration riscv_exec_command_handlers[] = { { .name = "info", @@ -3015,6 +3073,14 @@ static const struct command_registration riscv_exec_command_handlers[] = { .help = "Control dcsr.ebreaku. When off, U-mode ebreak instructions " "don't trap to OpenOCD. Defaults to on." }, + { + .name = "enable_cheriot", + .handler = riscv_enable_cheriot, + .mode = COMMAND_ANY, + .usage = "[mpw1|mpw2|iceni|sonata]", + .help = "Enable cheriot and set the variant" + }, + COMMAND_REGISTRATION_DONE }; @@ -4095,7 +4161,7 @@ int riscv_init_registers(struct target *target) r->feature = &feature_cpu; // GPRs are capability-sized on CHERIOT. - if (info->cheriot) { + if (cheriot_variant_can_access_cap_gpr(info)) { r->size = 64; r->reg_data_type = &type_cheriot_cap; } @@ -4346,9 +4412,7 @@ int riscv_init_registers(struct target *target) break; case CSR_MTVEC: case CSR_MEPC: - /* MTVEC and MEPC are inaccessible on CHERIOT. They must be access via - the special cap registers MTCC and MEPCC respectively. */ - r->exist = !info->cheriot; + r->exist = riscv_can_access_mtvec_mepcc(info); } if (!r->exist && !list_empty(&info->expose_csr)) { diff --git a/src/target/riscv/riscv.h b/src/target/riscv/riscv.h index ff4affafa5..4e0ecb51c9 100644 --- a/src/target/riscv/riscv.h +++ b/src/target/riscv/riscv.h @@ -59,6 +59,13 @@ enum riscv_halt_reason { RISCV_HALT_ERROR }; +enum cheriot_variant { + CHERIOT_NONE = 0, // We want to ensure this is falsely + CHERIOT_SONATA, + CHERIOT_MPW_1, + CHERIOT_MPW_2, +}; + typedef struct { struct target *target; unsigned int custom_number; @@ -109,8 +116,8 @@ struct riscv_info { int xlen; riscv_reg_t misa; - /* Whether the hart implements CHERIOT or not*/ - bool cheriot; + /* Whether the hart implements CHERIOT or not, and which version */ + enum cheriot_variant cheriot; /* Cached value of vlenb. 0 if vlenb is not readable for some reason. */ unsigned int vlenb; @@ -368,4 +375,9 @@ void riscv_add_bscan_tunneled_scan(struct target *target, struct scan_field *fie int riscv_read_by_any_size(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer); int riscv_write_by_any_size(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer); +// CHERIOT Helper functions +int riscv_can_access_mtvec_mepcc(struct riscv_info * info); +int cheriot_variant_is_cheriot(struct riscv_info * info); +int cheriot_variant_can_access_cap_gpr(struct riscv_info * info); +int cheriot_variant_can_spill_registers(struct riscv_info * info); #endif