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…


This content originally appeared on DEV Community and was authored by Kanak Shilledar

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

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

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!


This content originally appeared on DEV Community and was authored by Kanak Shilledar


Print Share Comment Cite Upload Translate Updates
APA

Kanak Shilledar | Sciencx (2024-09-07T05:56:02+00:00) Hello OpenSBI!. Retrieved from https://www.scien.cx/2024/09/07/hello-opensbi/

MLA
" » Hello OpenSBI!." Kanak Shilledar | Sciencx - Saturday September 7, 2024, https://www.scien.cx/2024/09/07/hello-opensbi/
HARVARD
Kanak Shilledar | Sciencx Saturday September 7, 2024 » Hello OpenSBI!., viewed ,<https://www.scien.cx/2024/09/07/hello-opensbi/>
VANCOUVER
Kanak Shilledar | Sciencx - » Hello OpenSBI!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/07/hello-opensbi/
CHICAGO
" » Hello OpenSBI!." Kanak Shilledar | Sciencx - Accessed . https://www.scien.cx/2024/09/07/hello-opensbi/
IEEE
" » Hello OpenSBI!." Kanak Shilledar | Sciencx [Online]. Available: https://www.scien.cx/2024/09/07/hello-opensbi/. [Accessed: ]
rf:citation
» Hello OpenSBI! | Kanak Shilledar | Sciencx | https://www.scien.cx/2024/09/07/hello-opensbi/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.