DEV Community

Kanak Shilledar
Kanak Shilledar

Posted on

Hello OpenSBI!

In this post we will discuss on how we can get a Hello World! printed on the OpenSBI console. Let's dive into it

What is OpenSBI?

OpenSBI (Open Source Supervisor Binary Interface) is a critical software layer in the RISC-V ecosystem. SBI is an interface between the Supervisor Execution Environment (SEE) and the supervisor. It allows the supervisor to execute some privileged operations by using the ecall instruction. Examples of SEE and supervisor are M-Mode and S-Mode on Unix-class platforms, where SBI si the only interface between them, as well as the Hypervisor extended-supervisor (HS) and the Virtualized Supervisor (VS). OpenSBI is an open-source reference implementation of the RISCV SBI specification and acts as a minimal runtime environment that executes in Machine Mode (M-Mode), the most privileged mode of execution in RISC-V. The primary purpost of OpenSBI is to offer platform services to operating systems running in Supervisor Mode (S-Mode).

Why do we need OpenSBI?

The RISC-V spec is designed to be minimal and modular, which means the ISA doesn't dictate how th efirmware works beyond the most essential pieces. The kernel (S-Mode) doesn't have the authority to directly interact with the hardware's critical parts like memory protection, CPU control, or I/O resources as it is all handled in M-Mode. OpenSBI tries to fill this gap by implementing all the necessary runtime services for your kernel. This way, your OS can call any SBI function and offload privileged tasks like setting up traps or powering off the system to M-Mode. This results in portability as the OS doesn't have to care about how the things are working internally. This is the reason why same kernel can work on different RISC-V hardware.

Two words on RISC-V

RISC-V is an open and extensible instruction set. It is governed by the RISC-V International. RISC-V is split into three privilege levels.

  • M-Mode (machine mode) Highest and most privileged
  • S-Mode (supervisor mode) Used by kernels for regular OS-level operations.
  • U-Mode (user mode) user applications

OpenSBI changes the environment from M-Mode to S-Mode to make the environment suitable for the kernel to boot.

Let's print Hello world!

We will be printing hello world in RISC-V assembly.

You can view the source code on github: https://github.com/kanakshilledar/hello-opensbi

_start:
  li  a0, 'H'
  li  a7, 0x01
  ecall
  li  a0, 'e'
  li  a7, 0x01
  ecall
  li  a0, 'l'
  li  a7, 0x01
  ecall
  li  a0, 'l'
  li  a7, 0x01
  ecall
  li  a0, 'o'
  li  a7, 0x01
  ecall
  li  a0, ' '
  li  a7, 0x01
  ecall
  li  a0, 'W'
  li  a7, 0x01
  ecall
  li  a0, 'o'
  li  a7, 0x01
  ecall
  li  a0, 'r'
  li  a7, 0x01
  ecall
  li  a0, 'l'
  li  a7, 0x01
  ecall
  li  a0, 'd'
  li  a7, 0x01
  ecall
  li  a0, '!'
  li  a7, 0x01
  ecall
  li  a0, '\n'
  li  a7, 0x01
  ecall
  li  a0, '\r'
  li  a7, 0x01
  ecall
  wfi
Enter fullscreen mode Exit fullscreen mode

To compile this you will need the riscv64 toolchain which you can easily get from distribution's package manager.

Before we run it lets take a closer look at the code.

  • _start: This a lable which marks as the entry point for our program.
  • li a0, 'H': li is a opcode which stands for load immediate which loads the value H into the register a0.
  • a7 is a systemcall number register in RISC-V and 0x01 corresponds to the write() systemcall.
  • ecall: This instruction triggers an environment call. It results in printing the character stored in the a0 register to the console.
  • wfi: Wait for interrupt opcode tells the CPU to halt until any external event occurs.

Running the code

You can run the code via the included Makefile which will assemble the code and run on qemu target.

make run
Enter fullscreen mode Exit fullscreen mode

You should get the following output.
OpenSBI Output

To exit out of qemu you can press CTRL + ALT + X

Conclusion

You must be having a question that we have successfully printed hello world but what's next? Why even bother printing it? The main purpose of this activity is to be able to print to the console a lot of things which are required in the early boot stages of the RISC-V system. The understanding of how to interact with M Mode through OpenSBI opens up a lot of potential for optimizing system-level operations, developing custom platform code, etc. OpenSBI is foundational and knwoing your way around it is crucial if you're serious about RISC-V development.

Thanks for reading!

Top comments (0)