-
-
Notifications
You must be signed in to change notification settings - Fork 775
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
riscv: add flashstub framework and helper functions (riscv32 only) #1764
base: main
Are you sure you want to change the base?
Conversation
We'll dive in and review this tomorrow, but as for the stack thing - the stub can use stack, it just has to set one up itself - see the RP2040 stub in #1609 which does this to improve code density and size. It's not really viable for the stub runner to do this itself, and with the |
Hi, This looks ugly and a bit error prone. Not sure what is the best way to deal with that. |
Given that going through the target API there is just a convoluted way to access riscv_csr_read/riscv_csr_write - we would suggest that the logic use these functions directly, solving the need to deal with the GDB offset and specify the register width that way (you can use |
Indeed, thank you for the pointer, that's way better. |
b0b8e42
to
da769a0
Compare
Updated accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good, though there are some items to address and talk about before this can be considered ready for merge. Most of the items are about formatting and code style though.
src/target/riscv32.c
Outdated
It returns true on success, false on error | ||
There is a built-in timeout of 10 seconds | ||
*/ | ||
bool riscv32_run_stub(target_s *t, uint32_t codeexec, uint32_t param1, uint32_t param2, uint32_t param3, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use target
for the target_s *
parameter name, we'd like not to introduce more clang-tidy lints when we've been trying to fix these.
Perhaps use loadaddr
rather than codeexec
, or at least codeaddr
to more clearly indicate what this parameter is?
src/target/riscv32.c
Outdated
uint32_t param4) | ||
{ | ||
bool ret = false; | ||
uint32_t pc, mie, zero = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please define each of these separately and with initialisation values. This is a clang-tidy
lint.
src/target/riscv32.c
Outdated
reason = t->halt_poll(t, NULL); | ||
} | ||
|
||
if (reason == TARGET_HALT_ERROR) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition and the next look merge-able, and more importantly, look to be able to be simplified such that it removes the gotos (we'd really like not to introduce any, they make for horrible codegen and a horrid time understanding code flow.
For example:
if (reason == TARGET_HALT_REQUEST)
ret = true;
target->halt_request(target);
Now, we have questions about ret
as well and its (re)use here as that logic doesn't make sense right now, but that should also simplify the while loop too so as to just need a break;
statement from that check for timeout expiry.
Please write comments detailing your intent with this code - it's very difficult to tell from the plain text of this code what you are intending to have happen here and why, vs what this actually does.
src/target/riscv32.c
Outdated
t->reg_write(t, RISCV_REG_PC, &pc, 4); | ||
return ret; | ||
} | ||
//---- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this trailing comment.
src/target/riscv32.c
Outdated
Small helper function to translate target to hart and simplify parameters | ||
We assume it is 32 bits | ||
*/ | ||
static bool riscv32_target_csr_write(target_s *target, const uint16_t reg, uint32_t val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given how this gets used and how small a function this is, but with it not marked inline, we expect this to be contributing a significant amount to the codegen for riscv32_run_stub() - if you grab the hart at the top of that function, you can then call the underlying read and write routines directly, which should result in leaner code generation. Same with the read function.
src/target/riscv32.c
Outdated
#define RISCV_REG_A0 10 | ||
#define RISCV_REG_A1 11 | ||
#define RISCV_REG_A2 12 | ||
#define RISCV_REG_A3 13 | ||
#define RISCV_REG_PC 32 | ||
#define RISCV_REG_SP 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need suffixing with U
to make them unsigned. It looks like these should really belong in the riscv_debug.h header so all RISC-V debug code can benefit from them.
src/target/riscv32.c
Outdated
while (reason == TARGET_HALT_RUNNING) | ||
{ | ||
if (platform_timeout_is_expired(&timeout)) | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The braces here can be (should be) removed. Please also run clang-format as this is not formatted properly for this code base.
1ef411c
to
f0d6406
Compare
I think i've taken all or about all of the comments into account. I've left the two helper functions as inline to have a simpler code. I guess they might be helpful later on. Similarily i've let the error handling a bit detailed as to allow further more detailed use cases. It should not cause binary bloat. ( On a side note, clang-tidy 17 does not like much the .clang-tidy due to varying indentation and after fixing it complains really a lot ) |
Detailed description
Hi
This is a temptative patch to add some flashstub helper functions for the rv32 targets.
It tries to be very similar to the cortexm version. i.e.
Call riscv32_run_stub( target, loadaddr, param1, param2, param3, param4)
and the flashstub must end up with riscv_stub_exit(errorcode)
The main difference in behavior is the code saves and restore PC & MIE to avoid letting things dangling in ram.
Not completely sure this is needed, just to be on the safe side.
I've tested it with a CH32V307 board using the upcoming board & rvswd protocol support with a ~2x speedup.
As a sidenote and for the record, my original version was providing a temporary stack for the stub to avoid strict compiler rules about not using stack. Dont know if it is a valid idea. Nonetheless, this MR is assuming the stack is not used at all by the stub.
Thank you.
Your checklist for this pull request
Tested with a BMP derivative using CH32V3xx chips. Cant test (yet!) with vanilla bmp