Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/target/riscv/opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
86 changes: 78 additions & 8 deletions src/target/riscv/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,87 @@ 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 (!cheriot_variant_can_spill_registers(info)) {
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 (!cheriot_variant_can_spill_registers(info)) {
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 (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);
if (result != ERROR_OK)
return result;
continue;
}

int result = riscv_get_register(t, &saved_registers[i], i);
if (result != ERROR_OK)
return result;
Expand Down Expand Up @@ -83,8 +148,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 (cheriot_variant_can_spill_registers(info)) {
cheriot_restore_register(t, i);
continue;
}
riscv_set_register(t, i, saved_registers[i]);
}

return ERROR_OK;
}
Expand Down
26 changes: 14 additions & 12 deletions src/target/riscv/riscv-013.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -3360,6 +3353,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,
Expand All @@ -3373,7 +3370,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 (cheriot_variant_can_spill_registers(info) && cheriot_save_register(target, GDB_REGNO_S0) != ERROR_OK)
goto restore_mstatus;
if (!cheriot_variant_can_spill_registers(info) && register_read(target, &s0, GDB_REGNO_S0) != ERROR_OK)
goto restore_mstatus;

/* Write the program (load, increment) */
Expand Down Expand Up @@ -3423,7 +3423,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 (!cheriot_variant_can_spill_registers(info) && riscv_set_register(target, GDB_REGNO_S0, s0) != ERROR_OK)
result = ERROR_FAIL;
if (cheriot_variant_can_spill_registers(info) && cheriot_restore_register(target, GDB_REGNO_S0) != ERROR_OK)
result = ERROR_FAIL;

restore_mstatus:
Expand Down
72 changes: 68 additions & 4 deletions src/target/riscv/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down
16 changes: 14 additions & 2 deletions src/target/riscv/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Loading