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

riscv: add flashstub framework and helper functions (riscv32 only) #1764

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mean00
Copy link

@mean00 mean00 commented Feb 8, 2024

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

  • I've read the Code of Conduct
  • I've read the guidelines for contributing to this repository
  • [Builds till linking then out of flash ] It builds for hardware native (see Building the firmware)
  • It builds as BMDA (see Building the BMDA)
  • I've tested it to the best of my ability
    Tested with a BMP derivative using CH32V3xx chips. Cant test (yet!) with vanilla bmp
  • My commit messages provide a useful short description of what the commits do

@dragonmux dragonmux added this to the v2.0 release milestone Feb 8, 2024
@dragonmux dragonmux added the Enhancement General project improvement label Feb 8, 2024
@dragonmux
Copy link
Member

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 lui instruction in RISC-V, it would be trivial to load a 4KiB-aligned stack pointer in the stub.

@mean00
Copy link
Author

mean00 commented Feb 11, 2024

Hi,
This MR exhibits an issue related to the CSR register management introduced earlier (my bad) because it manipulates some CSR directly.
The CSR registers are offset by RV_CSR_GDB_OFFSET to present them as regular register to gdb
It means that also when used internally, something like
t->reg_write(t, RV_CSR_MIE, &zero, 4);
should actually be
t->reg_write(t, RV_CSR_MIE+RV_CSR_GDB_OFFSET, &zero, 4);

This looks ugly and a bit error prone. Not sure what is the best way to deal with that.

@dragonmux
Copy link
Member

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 RV_CSR_FORCE_32_BIT with RV_CSR_MIE to ensure the right width access will always happen, or just assume because it's a RISC-V 32 part that it's the right width)

@mean00
Copy link
Author

mean00 commented Feb 11, 2024

Indeed, thank you for the pointer, that's way better.

@mean00
Copy link
Author

mean00 commented Feb 12, 2024

Updated accordingly.
Thank you.

Copy link
Member

@dragonmux dragonmux left a 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/flashstub/riscv32_flashstub.h Outdated Show resolved Hide resolved
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,
Copy link
Member

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 Show resolved Hide resolved
uint32_t param4)
{
bool ret = false;
uint32_t pc, mie, zero = 0;
Copy link
Member

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.

reason = t->halt_poll(t, NULL);
}

if (reason == TARGET_HALT_ERROR)
Copy link
Member

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.

t->reg_write(t, RISCV_REG_PC, &pc, 4);
return ret;
}
//----
Copy link
Member

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/riscv_debug.h Show resolved Hide resolved
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)
Copy link
Member

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.

Comment on lines 670 to 675
#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
Copy link
Member

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.

while (reason == TARGET_HALT_RUNNING)
{
if (platform_timeout_is_expired(&timeout))
{
Copy link
Member

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.

@mean00 mean00 marked this pull request as draft February 18, 2024 16:00
@mean00
Copy link
Author

mean00 commented Feb 18, 2024

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.
The generated codesize should be the same.

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 )

@mean00 mean00 marked this pull request as ready for review February 18, 2024 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement General project improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants