Skip to content

target/riscv: warn about truncating register values #1268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: riscv
Choose a base branch
from
Open
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
31 changes: 24 additions & 7 deletions src/target/riscv/riscv-013.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,14 +1370,31 @@ static int register_read_progbuf(struct target *target, uint64_t *value,
{
assert(target->state == TARGET_HALTED);

if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31)
return fpr_read_progbuf(target, value, number);
else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095)
return csr_read_progbuf(target, value, number);
int res;
if (number >= GDB_REGNO_FPR0 && number <= GDB_REGNO_FPR31) {
res = fpr_read_progbuf(target, value, number);
} else if (number >= GDB_REGNO_CSR0 && number <= GDB_REGNO_CSR4095) {
res = csr_read_progbuf(target, value, number);
} else {
LOG_TARGET_ERROR(target, "Unexpected read of %s via program buffer.",
riscv_reg_gdb_regno_name(target, number));
return ERROR_FAIL;
}
if (res != ERROR_OK)
return res;

LOG_TARGET_ERROR(target, "Unexpected read of %s via program buffer.",
riscv_reg_gdb_regno_name(target, number));
return ERROR_FAIL;
unsigned int size_bits = register_size(target, number);
unsigned int value_bits = sizeof(*value) * CHAR_BIT;
assert(size_bits <= value_bits);
if (size_bits == value_bits || *value >> size_bits == 0)
return ERROR_OK;

LOG_TARGET_WARNING(target, "Value read for register %s of %" PRIx64
" does not fit into the size of the register (%u bits)."
" This is a HW bug. Truncating the value to fit into the register.",
riscv_reg_gdb_regno_name(target, number), *value, size_bits);
Comment on lines +1392 to +1395
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy of #1233 (comment) by @JanMatCodasip:

If this HW problem occurs, it is a serious thing, and we better not trust the returned value at all.

Instead of truncating the value, I would recommend to change the message level to ERROR and end the operation with an error code.

Few additional details:

  • Added prefix 0x in front of the hex number
  • I'd suggest minor change of word order for readability
  • does not fit into the size --> exceeds the size (shorter and more formal)
Suggested change
LOG_TARGET_WARNING(target, "Value read for register %s of %" PRIx64
" does not fit into the size of the register (%u bits)."
" This is a HW bug. Truncating the value to fit into the register.",
riscv_reg_gdb_regno_name(target, number), *value, size_bits);
LOG_TARGET_ERROR(target, "Value 0x%" PRIx64 " read from register %s"
" exceeds the size of the register (%u bits). This is a HW bug. "
*value, riscv_reg_gdb_regno_name(target, number), size_bits);
return ERROR_FAIL;

Otherwise this change looks good.

*value &= GENMASK_ULL(size_bits - 1U, 0U);
return ERROR_OK;
}

/**
Expand Down
Loading