Skip to content

Commit

Permalink
target/riscv: use cacheable read/write function to handle DCSR
Browse files Browse the repository at this point in the history
Signed-off-by: liangzhen <[email protected]>
  • Loading branch information
lz-bro committed Sep 25, 2023
1 parent 2427f58 commit d38efb7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
40 changes: 5 additions & 35 deletions src/target/riscv/riscv-013.c
Original file line number Diff line number Diff line change
Expand Up @@ -4698,25 +4698,12 @@ static int riscv013_get_register(struct target *target,
if (dm013_select_target(target) != ERROR_OK)
return ERROR_FAIL;

int result = ERROR_OK;
if (rid == GDB_REGNO_PC) {
/* TODO: move this into riscv.c. */
result = register_read_direct(target, value, GDB_REGNO_DPC);
LOG_TARGET_DEBUG(target, "read PC from DPC: 0x%" PRIx64, *value);
} else if (rid == GDB_REGNO_PRIV) {
uint64_t dcsr;
/* TODO: move this into riscv.c. */
if (register_read_direct(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
return ERROR_FAIL;
*value = set_field(0, VIRT_PRIV_V, get_field(dcsr, CSR_DCSR_V));
*value = set_field(*value, VIRT_PRIV_PRV, get_field(dcsr, CSR_DCSR_PRV));
} else {
result = register_read_direct(target, value, rid);
if (result != ERROR_OK)
*value = -1;
if (register_read_direct(target, value, rid) != ERROR_OK) {
*value = -1;
return ERROR_FAIL;
}

return result;
return ERROR_OK;
}

static int riscv013_set_register(struct target *target, enum gdb_regno rid,
Expand All @@ -4728,24 +4715,7 @@ static int riscv013_set_register(struct target *target, enum gdb_regno rid,
if (dm013_select_target(target) != ERROR_OK)
return ERROR_FAIL;

if (rid <= GDB_REGNO_XPR31) {
return register_write_direct(target, rid, value);
} else if (rid == GDB_REGNO_PC) {
LOG_TARGET_DEBUG(target, "writing PC to DPC: 0x%" PRIx64, value);
return register_write_direct(target, GDB_REGNO_DPC, value);
} else if (rid == GDB_REGNO_PRIV) {
riscv_reg_t dcsr;

if (register_read_direct(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
return ERROR_FAIL;
dcsr = set_field(dcsr, CSR_DCSR_PRV, get_field(value, VIRT_PRIV_PRV));
dcsr = set_field(dcsr, CSR_DCSR_V, get_field(value, VIRT_PRIV_V));
return register_write_direct(target, GDB_REGNO_DCSR, dcsr);
} else {
return register_write_direct(target, rid, value);
}

return ERROR_OK;
return register_write_direct(target, rid, value);
}

static int dm013_select_hart(struct target *target, int hart_index)
Expand Down
24 changes: 24 additions & 0 deletions src/target/riscv/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -4822,6 +4822,19 @@ static int riscv_set_or_write_register(struct target *target,

keep_alive();

if (rid == GDB_REGNO_PC) {
LOG_TARGET_DEBUG(target, "writing PC to DPC: 0x%" PRIx64, value);
return riscv_set_or_write_register(target, GDB_REGNO_DPC, value, write_through);
} else if (rid == GDB_REGNO_PRIV) {
riscv_reg_t dcsr;

if (riscv_get_register(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
return ERROR_FAIL;
dcsr = set_field(dcsr, CSR_DCSR_PRV, get_field(value, VIRT_PRIV_PRV));
dcsr = set_field(dcsr, CSR_DCSR_V, get_field(value, VIRT_PRIV_V));
return riscv_set_or_write_register(target, GDB_REGNO_DCSR, dcsr, write_through);
}

if (!target->reg_cache) {
assert(!target_was_examined(target));
LOG_TARGET_DEBUG(target,
Expand Down Expand Up @@ -4907,6 +4920,17 @@ int riscv_get_register(struct target *target, riscv_reg_t *value,

keep_alive();

if (rid == GDB_REGNO_PC) {
return riscv_get_register(target, value, GDB_REGNO_DPC);
} else if (rid == GDB_REGNO_PRIV) {
uint64_t dcsr;
if (riscv_get_register(target, &dcsr, GDB_REGNO_DCSR) != ERROR_OK)
return ERROR_FAIL;
*value = set_field(0, VIRT_PRIV_V, get_field(dcsr, CSR_DCSR_V));
*value = set_field(*value, VIRT_PRIV_PRV, get_field(dcsr, CSR_DCSR_PRV));
return ERROR_OK;
}

if (!target->reg_cache) {
assert(!target_was_examined(target));
LOG_TARGET_DEBUG(target, "No cache, reading %s from target",
Expand Down

0 comments on commit d38efb7

Please sign in to comment.