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: use batch interface in read_memory_bus_v1() #1081

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

en-sc
Copy link
Collaborator

@en-sc en-sc commented Jun 4, 2024

Fixes #1080

@en-sc en-sc changed the title target/riscv: use batch interface in read_memory_bus_v1() target/riscv: use batch interface in read_memory_bus_v1() Jun 4, 2024
@en-sc en-sc self-assigned this Jun 4, 2024
@en-sc
Copy link
Collaborator Author

en-sc commented Jun 4, 2024

This PR is based on #1073

@en-sc en-sc force-pushed the en-sc/sb_read_v1 branch 2 times, most recently from b97f814 to 9c69123 Compare June 5, 2024 13:59
@en-sc en-sc marked this pull request as draft June 7, 2024 12:27
@en-sc en-sc marked this pull request as ready for review June 18, 2024 10:06
Copy link
Collaborator

@JanMatCodasip JanMatCodasip left a comment

Choose a reason for hiding this comment

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

Overall this change looks all right - thanks. I like how the use of the batch API simplifies the code in read_memory_bus_v1() and makes it easier to reason about.

Two things:

  • I have not really grasped where exactly in the original code was the problem causing read_memory_bus_v1() is broken #1080, and what had to happen for it to occur. Could you please explain it.
  • I believe the order of sbdata0 reads should be different - please see my comments.

src/target/riscv/riscv-013.c Outdated Show resolved Hide resolved
src/target/riscv/riscv-013.c Outdated Show resolved Hide resolved
src/target/riscv/riscv-013.c Outdated Show resolved Hide resolved
@en-sc en-sc force-pushed the en-sc/sb_read_v1 branch 4 times, most recently from 49e7dd9 to e546940 Compare July 1, 2024 08:26
@en-sc
Copy link
Collaborator Author

en-sc commented Jul 1, 2024

I have not really grasped where exactly in the original code was the problem causing read_memory_bus_v1() is broken #1080, and what had to happen for it to occur. Could you please explain it.

How the issue occurs?

Debug: 647 10 riscv-013.c:3379 log_memory_access64(): M[0x1000] reads 0x00000297
  • continue transfers control to the start of the outermost loop:

    while (next_address < end_address) {
  • On this iteration, the loop that reads an element is not entered, since next_address - address == 4, i = (next_address - address) / size == 1, i < (count - 1 == 1) is false.:
    for (uint32_t i = (next_address - address) / size; i < count - 1; i++) {
  • However, DMI NOP is executed outside the loop, since the count is greater than 1:
    if (count > 1) {
    unsigned attempt = 0;
    while (1) {
    if (attempt++ > 100) {
    LOG_TARGET_ERROR(target, "DMI keeps being busy in while reading memory"
    " just past " TARGET_ADDR_FMT, next_read);
    return ERROR_FAIL;
    }
    dmi_status_t status = dmi_scan(target, NULL, &sbvalue[0], DMI_OP_NOP, 0, 0, false);
    if (status == DMI_STATUS_BUSY)
    increase_dmi_busy_delay(target);
    else if (status == DMI_STATUS_SUCCESS)
    break;
    else
    return ERROR_FAIL;
    }
  • This NOP returns some garbage (the previous DMI OP was a NOP) and this garbage is assumed to be the result of the read (and overrides the previous correct result):
Debug: 661 11 riscv-013.c:3379 log_memory_access64(): M[0xfff] reads 0x00000000

TL;DR:

The condition

if (count > 1) {

Shoudl guarantee that the body of the loop
for (uint32_t i = (next_address - address) / size; i < count - 1; i++) {

is executed at least once for the read value to be correct.

How the commit fixes the issue?

The execution of the NOP is moved into the loop (the last operation in the batch).

JanMatCodasip
JanMatCodasip previously approved these changes Jul 2, 2024
Copy link
Collaborator

@JanMatCodasip JanMatCodasip left a comment

Choose a reason for hiding this comment

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

@en-sc Thank you for explaining it in detail 👍. I have understood the original problem now.

LGTM!

Fixes riscv-collab#1080

Change-Id: Ifc1a48fcd0b28f7cdb1e5ad3cbd20d53ea3560a5
Signed-off-by: Evgeniy Naydanov <evgeniy.naydanov@syntacore.com>
@en-sc
Copy link
Collaborator Author

en-sc commented Jul 3, 2024

@JanMatCodasip, please, take a look once again. There was a conflict with aa9a3fa.

@en-sc en-sc merged commit f34e531 into riscv-collab:riscv Jul 9, 2024
4 checks passed
@en-sc en-sc deleted the en-sc/sb_read_v1 branch July 9, 2024 11:28
for (size_t k = 0; k <= last_key; ++k) {
sbvalue[k] = riscv_batch_get_dmi_read_data(batch,
last_key - k);
buf_set_u32(buffer + i * size + k * 4, 0, 8 * size, sbvalue[k]);
Copy link
Collaborator

@aap-sc aap-sc Aug 3, 2024

Choose a reason for hiding this comment

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

@en-sc you have an error here. size can be 8 here.

#7  0x000055555609d04c in buf_set_u32 (_buffer=0x5555569ca720 "\277", first=0, num=64, value=191)                           
    at /home/nikita/projects/openocd/src/helper/binarybuffer.h:43 
#8  0x00005555560b1d13 in read_memory_bus_v1 (target=0x5555569e5390, address=2147491969, size=8, count=2, 
    buffer=0x5555569ca720 "\277", increment=8) at ../openocd/src/target/riscv/riscv-013.c:3410
#9  0x00005555560b58c0 in read_memory (target=0x5555569e5390, address=2147491969, size=8, count=2, 
    buffer=0x5555569ca720 "\277", increment=8) at ../openocd/src/target/riscv/riscv-013.c:4397
#10 0x0000555555c9b5c8 in riscv_read_memory (target=0x5555569e5390, address=2147491969, size=8, count=2, 
    buffer=0x5555569ca720 "\277") at ../openocd/src/target/riscv/riscv.c:3088
#11 0x0000555555c2daee in target_read_memory (target=0x5555569e5390, address=2147491969, size=8, count=2, 
    buffer=0x5555569ca720 "\277") at ../openocd/src/target/target.c:1245

Copy link
Collaborator

Choose a reason for hiding this comment

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

(For reference, in case anyone wonders.)

This is being fixed in #1109

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.

read_memory_bus_v1() is broken
3 participants