Skip to content
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

target/riscv: warn about truncating register values #1233

Open
wants to merge 1 commit into
base: riscv
Choose a base branch
from

Conversation

en-sc
Copy link
Collaborator

@en-sc en-sc commented Feb 27, 2025

When reading a register via Program Buffer the actual value is read from a GPR. The GPR size (XLEN) can be greater than the length of the target register (e.g. dcsr is always 32-bit wide). Due to some HW issue GPR read may return a value that needs to be truncated to fit into the target register. Warn the user about it.

Moreover, the value in cache was and is truncated, but the riscv_reg_t that is filled by riscv_reg_get() didn't use to be. This may lead to an assertion failure in abstract_data_write_fill_batch(): Link:

assert(value <= UINT32_MAX || size_in_words > 1);

Change-Id: I4d8a5ba2451fc5a60f51b9143b6b140dfe3b73b8

When reading a register via Program Buffer the actual value is read from
a GPR. The GPR size (XLEN) can be greater than the length of the target
register (e.g. `dcsr` is always 32-bit wide). Due to some HW issue GPR
read may return a value that needs to be truncated to fit into the
target register. Warn the user about it.

Moreover, the value in cache was and is truncated, but the `riscv_reg_t`
that is filled by `riscv_reg_get()` didn't use to be. This may lead to
an assertion failure in `abstract_data_write_fill_batch()`:
Link: https://github.com/riscv-collab/riscv-openocd/blob/fa7e2351c834d63016db65923f25ec599bb7ceac/src/target/riscv/riscv-013.c#L757

Change-Id: I4d8a5ba2451fc5a60f51b9143b6b140dfe3b73b8
Signed-off-by: Evgeniy Naydanov <[email protected]>
Comment on lines +1392 to +1395
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);
Copy link
Collaborator

@JanMatCodasip JanMatCodasip Feb 28, 2025

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants