Feature: RISC-V semihosting support #2115
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Detailed description
cortexm_hostio_request()into riscv_debug.c and hooking it toriscv_halt_poll()in much the same fashion. Entire semihosting.c can be reused because https://github.com/riscv-non-isa/riscv-semihosting follows https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst w.r.t. syscall numbers and calling convention. Thanks to maintainers for refactoring and splitting out the common code of BMD implementation.Additional code for stepping over XLEN-wide EBREAK helps me avoid doing $pc manipulation from gdb console, although I'm not sure how correct is it to do so.
Tested on Pico 2 board running basically https://github.com/raspberrypi/pico-examples/blob/master/hello_world/serial/hello_serial.c on the first Hazard3/RISC-V core, but using
PICO_STDIO_SEMIHOSTINGnotPICO_STDIO_UART. Debugger isblackpill-f411ce, running BMF v2.0 with full newlib (not nano because of rvhuimac bug) and/or BMDA. Not tested on RV64. There were 2-byte compressed instructions in disassembly AFAICS, like 0x9002 short ebreak elsewhere, but BMD memory access layer works regardless.Disassembly snippet of semihosting trampoline
The same helloworld runs on Cortex-M33 (ARM Secure) when recompiled to, and both arches exhibit a bug where the emit Fwrite packets to file descriptor 0 (subtracted 1 from callsite https://github.com/raspberrypi/pico-sdk/blob/2.1.1/src/rp2_common/pico_stdio_semihosting/stdio_semihosting.c#L24), and I'm not sure whose bug this is. Previously Cortex-M semihosting worked from --rdimon.specs as well as fw-test (on random cortexm targets I mean), so I think Pico C SDK should be fixed, which is what I did locally. Porting blackmagic-test-fw-archive to RISC-V could help test the other calls, I only checked 0x5 SYS_WRITE to fd 2 (stdout) which works, without fd=Fopen(":tt"), both in GDB and in BMF redirect_stdout, as well as
DEBUG_INFO("syscall ")diagnostic logs.This branch is based on #2113 but I could rebase it away because Hazard3/ice40 is not strictly required, it's just another target applicable to testing.
Some refactoring may be needed, like extracting macros for preamble/postamble slli/srai instructions, and that's for review process to deal with.
Scope of support (and possible testing) is obviously RP2350, ESP32-C3/C6 (with manual assembly, no ESP-IDF), GD32VF103 (with -lrdimon etc.), maybe CH32V205/V307 once RVSWD transport is merged, maybe Milk-V Duo and other chips with no flash driver support merged.
Your checklist for this pull request
Closing issues