
Recall first from Calling the SBI from S-mode
- An S-mode kernel executes ecall to request an SBI service from OpenSBI.
- Legacy console_putchar puts EID 0x01 in a7 and the byte in a0. OpenSBI drives the UART on QEMU virt.
- SBI v0.2+ uses a7 for the extension ID and a6 for the function ID, then returns an error in a0 and a value in a1.
The physical addresses on QEMU virt
4 min readThe last lesson showed how to call OpenSBI. Before writing more code you need the fixed addresses QEMU virt guarantees, and the qemu-system-riscv64 command that launches against them.
A kernel can build successfully and still fail before it prints a character. The file can contain valid RISC-V instructions while the address where those instructions are loaded differs from the address where the firmware starts executing. The Linux RISC-V boot documentation specifies kernel placement and incoming register state because the loader and kernel have to agree on both. In this lesson, the QEMU virt memory map tells you which physical addresses are available, and the kernel ELF tells QEMU where to load its bytes. RISC-V boot requirements
A an address in the guest machine's memory map as seen by its hardware can select RAM or a device. QEMU interprets that address according to the machine model you choose. The Ubuntu host runs QEMU as a process, and the kernel running inside QEMU uses the emulated machine's addresses. For this lesson's virt configuration, use the following map.
| Guest physical address | What QEMU puts there |
|---|---|
0x80000000 |
Start of RAM |
0x80200000 |
Start of the course kernel image with the normal linker script |
0x10000000 |
UART0, an NS16550A serial controller |
0x10001000 through 0x10008000 |
Eight virtio MMIO transport bases, spaced 0x1000 bytes apart |
0x02000000 |
CLINT, the core-local interrupt controller |
0x0c000000 |
PLIC, the platform-level interrupt controller |
A RAM access reads or writes stored bytes. With MMIO, where a load or store at a device address accesses a device register, the same kind of CPU instruction can have a device-specific effect. The UART base tells you where its register block begins. Each virtio address in the table identifies the start of a transport slot. The machine can expose those slots even when the command line attaches no virtio device. The CLINT provides local timer and software interrupt facilities, and the PLIC handles external interrupt sources. For the first boot, you only need to recognize those regions in the map.
The memory size and the RAM base answer different questions. -m 128M gives the guest 128 MiB of RAM starting at 0x80000000. The first address beyond that RAM is 0x88000000. The course's kernel start, 0x80200000, is 2 MiB above the RAM base and lies within that range. Default OpenSBI firmware occupies memory near the start of RAM, so placing the kernel at the RAM base risks overlapping the firmware. The later memory-management lessons will account for the regions already in use before assigning RAM to the kernel allocator.
Going deeperExplore the mechanism
QEMU constructs a flattened device tree, or FDT, that describes the configured machine. Its data includes the RAM range and device information such as register ranges and interrupt connections. OpenSBI passes the physical address of this blob to the supervisor kernel in register a1. The register holds a pointer to the blob, so the kernel would read the blob from memory to get those descriptions. In the real run above, OpenSBI printed Domain0 Next Arg1 : 0x0000000087e00000: QEMU put the device tree blob near the top of the 128 MiB of RAM, and that is the address your kernel receives in a1.
The first course kernel boots with a known RAM configuration and a known machine model. It can use the fixed UART base if it needs direct serial access, and it can leave the FDT unread for a first banner. A kernel that needs to discover the RAM size or devices on different configurations can read the FDT supplied on each boot. RISC-V boot requirements
Launching the kernel ELF
2 min readRun the command in the Ubuntu shell. Set elf to the filename of the kernel ELF you built, using its relative or absolute path. ./kernel.elf is an example path. Quoting "$elf" lets the shell pass the path as a single argument.
elf=./kernel.elf
qemu-system-riscv64 -machine virt -m 128M \
-nographic -bios default -kernel "$elf"
qemu-system-riscv64 runs a whole 64-bit RISC-V machine. -machine virt chooses the board model and therefore its physical address map. -m 128M sets the guest RAM capacity. -bios default selects the default OpenSBI firmware that QEMU supplies for this machine. -kernel "$elf" supplies your kernel as the next image. The usual launch starts the guest CPUs immediately, so OpenSBI runs and can print its startup information before your kernel executes.
-nographic disables graphical output and, with the default serial and monitor configuration, multiplexes the guest serial console and QEMU monitor onto the terminal. The serial console carries guest output. The monitor accepts commands for inspecting or controlling QEMU, which lets you examine the guest before it has a working console of its own. To request that terminal routing explicitly, use -serial mon:stdio with -nographic.
qemu-system-riscv64 -machine virt -m 128M \
-nographic -serial mon:stdio -bios default -kernel "$elf"
In the multiplexed session, press Ctrl-a then c to switch between the serial console and QEMU monitor. At the monitor, quit exits QEMU. You can also press Ctrl-a then x to exit directly. An OpenSBI banner confirms that the firmware ran and that its console output reached the terminal. The course kernel may be silent at this stage. The banner alone gives you evidence about the firmware, so use the handoff address and guest memory when you need to check the kernel image.
For a launch intended for GDB, add both -s and -S.
qemu-system-riscv64 -machine virt -m 128M \
-nographic -serial mon:stdio -bios default -kernel "$elf" -s -S
Lowercase -s enables QEMU's GDB server on TCP port 1234. Uppercase -S leaves the guest CPUs stopped when QEMU starts. The options have independent effects. With -s alone the guest begins running while the debugger is disconnected. With -S the terminal can stay quiet because even OpenSBI has yet to execute. The QEMU monitor remains available in this state. Its cont command resumes the guest if you want to observe the firmware handoff without attaching GDB. The later GDB lesson covers attaching and inspecting execution.
Checking where the ELF lands
4 min readThe ordinary course build links the kernel at 0x80200000. The linker script sets the address of the first loadable code region and places the entry code there. To see the result, inspect the built ELF before launching QEMU. The first command displays the ELF header, including the entry point. The second displays the program headers, including the LOAD records and their physical addresses.
readelf -h "$elf"
readelf -lW "$elf"
For this build, look for the LOAD segment that contains the entry code and compare its addresses with the entry point in the header. The course layout uses matching virtual and physical addresses there and puts the entry at the start of the loaded image. Keep that layout for this exercise. An ELF can represent the entry point and segment addresses separately, so inspecting the header and segment records gives you two pieces of evidence. elf(5)
QEMU can load the file before any guest instruction runs. With an ELF recognized by -kernel, QEMU takes the load locations from its loadable program headers. 0x80200000 is the expected placement for the course ELF because of how it is linked. If you relink the same image with a different origin, the resulting program headers can describe a different placement. The exercise keeps the entry code at the image start so the intended entry and the address reported for the firmware handoff can be compared directly.
For a baseline observation, start QEMU with -S and switch to the monitor. The command below examines four bytes of guest physical memory. With the normal course layout, those bytes are at the start of the kernel's entry code.
xp /4bx 0x80200000
For an exact byte comparison, locate the entry segment's file Offset in readelf -lW. With the entry at the start of that segment and at least four bytes in its file image, that offset is where the same four bytes begin in the ELF. In the following shell command, replace 0xOFFSET with the actual hexadecimal value from readelf. Bash arithmetic converts it to the byte offset accepted by od.
offset=0xOFFSET
od -An -tx1 -j "$((offset))" -N 4 "$elf"
Here is a real run with a minimal kernel that only prints K through the SBI (QEMU 8.2.2, OpenSBI v1.3). Its entry code is the first instruction of the image, auipc sp,0x10, whose encoding is 0x00010117:
$ readelf -lW kernel.elf | grep LOAD
LOAD 0x001000 0x0000000080200000 0x0000000080200000 0x00002a 0x00002a R E 0x1000
$ od -An -tx1 -j $((0x1000)) -N 4 kernel.elf
17 01 01 00
(qemu) xp /4bx 0x80200000
0000000080200000: 0x17 0x01 0x01 0x00
The file bytes at Offset 0x1000 and the guest bytes at 0x80200000 are the same four bytes, little-endian. Your own kernel will show different bytes, but the two lines must match each other.
The actual bytes depend on the build. Matching them ties the file you inspected to the address QEMU populated. Then enter cont at the monitor and switch back to the serial console to see the OpenSBI startup report. Look for Domain0 Next Address, which reports where the firmware will hand off to the next image. That field records the intended destination. Evidence that instructions are present there comes from the memory comparison, and evidence that the kernel itself has executed can come from its own output.
To test placement, change only the linker origin from 0x80200000 to 0x80400000, rebuild, and point elf at the rebuilt file. The new origin stays aligned to 2 MiB and remains inside the same 128 MiB of guest RAM. Keep the same QEMU options and use the same entry-at-start layout. Check the header and load records again, inspect physical memory at 0x80400000 with the CPUs stopped, then compare the firmware's reported next address after continuing. The comparison across both runs lets you attribute the placement change to the rebuilt ELF while holding the machine configuration constant. Restore the normal linker origin after collecting your observations.
Here is what happened when I did this on the same minimal kernel. readelf -lW then showed VirtAddr and PhysAddr 0x80400000, and the OpenSBI report printed Domain0 Next Address : 0x0000000080400000 and Domain0 Next Mode : S-mode, followed by the kernel's K. QEMU took the load address from the ELF, and the firmware followed it. One trap: when the linker script is passed with -C link-arg=-Tlinker.ld in .cargo/config.toml, Cargo does not treat the script as an input. After editing it I had to run touch src/main.rs before cargo build; otherwise Cargo printed Finished and the ELF was unchanged. A build.rs that prints cargo:rerun-if-changed=linker.ld removes the problem.
In The first printed banner, you will use the same launch command and terminal to make the kernel itself produce visible output after the OpenSBI handoff.
Key takeaways
- On the course's QEMU virt machine, RAM begins at 0x80000000 and UART0 is at 0x10000000.
- The course ELF links at 0x80200000, and QEMU uses the ELF loadable segments to place the image.
- OpenSBI passes the kernel an FDT pointer in a1.
- -s opens a GDB connection on TCP port 1234 and -S leaves the guest CPUs stopped at startup.
Reflect
- Explain to a colleague, in two sentences: locate RAM, UART0, the virtio MMIO slots, the CLINT, and the PLIC in the QEMU virt physical address map.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Kernel documentationRISC-V boot requirements: register state, kernel placement, and hardware description
- Man pageelf(5): ELF headers and loadable program segments
Official documentation first. Links checked when the lesson was published.