RISC-V Meaty Skeleton with QEMU virt board

From OSDev.wiki
Jump to navigation Jump to search

This tutorial assumes you have completed RISC-V Bare Bones on the QEMU virt board, or alternatively, HiFive-1 Bare Bones. If not, you should complete them first for an overview of how to boot your own operating system on RISC-V. This tutorial is deliberately brief on concepts that have already been covered in the bare bones tutorials and their transitive prerequisites.

The bare bones tutorials provide minimal examples that are not structured to enable sustainable mid- to long-term development of the codebase. This tutorial attempts to rectify that by providing a well-structured project that should serve you well through your OSDev journey with the following features:

  • Hierarchical project structure with make build system for sustainable mid- to long-term development
  • Includes debug target for debugging with GDB (requires cross-debugger targeting riscv64-elf)
  • Basic console output through NS16550A UART
  • Convenience wrappers for powering off and rebooting the device
  • Working kprintf supporting base format specifiers (no floating point support; no sub-specifiers; no n specifier) to facilitate printf debugging
  • panic function for kernel panics

RISC-V Bare Bones

Main article: RISC-V Bare Bones

It is assumed you have completed RISC-V Bare Bones or another comparable bare bones tutorial. Though not a strict requirement, it is useful for confirming that your development environment works and explaining a few basic things.

We won't be reusing any code from those tutorials though, so throw it away (or save it to an archive) and we'll start over again.

Building a Cross-Compiler

Main article: GCC Cross-Compiler, Why do I need a Cross Compiler?

You must use a GCC Cross-Compiler in this tutorial as in the RISC-V Bare Bones tutorial, with riscv64-elf as the target.

You must configure your cross-binutils with the --with-sysroot option, otherwise linking will mysteriously fail with the this linker was not configured to use sysroots error message. If you forgot to configure your cross-binutils with that option, you'll have to rebuild it, but you can keep your cross-gcc.

Building a Cross-Debugger (optional)

Main article: GDB

If you wish to debug your kernel with GDB, you'll need to build it separately with riscv64-elf as the target, since it's not included by default with GCC and Binutils. Otherwise, if printf debugging is your style, you may safely skip this section.

The process is similar to building a cross-GCC or cross-binutils, and you may refer to the GDB page in BLFS for most details sans cross-debugging support, but we'll go through the process in detail here anyway.

Fetch the latest version of GDB through https://ftp.gnu.org/gnu/gdb/. The latest version at the time of writing is 12.1:

export GDB_VERSION="12.1"
wget https://ftp.gnu.org/gnu/gdb/gdb-${GDB_VERSION}.tar.xz

Unpack the archive:

tar xvf gdb-${GDB_VERSION}.tar.xz

Now move into the source directory:

pushd gdb-${GDB_VERSION}/

Create a build directory and move into it:

mkdir build
pushd build/

Now export a few variables as with building a cross-GCC or cross-binutils:

export PREFIX="$HOME/opt/cross"
export TARGET=riscv64-elf
export PATH="$PREFIX/bin:$PATH"

Configuration options are mostly the same as with building cross-GCC or cross-binutils. In particular, you may wish to enable the following features:

  • --enable-tui=yes: Enables TUI mode for debugging. Requires development headers for Ncurses to be installed on the build host
  • --with-expat: Build with Expat, a library for XML parsing
../configure --target=$TARGET \
  --prefix="$PREFIX" \
  --with-sysroot \
  --disable-nls \
  --disable-werror \
  --enable-languages=c,c++ \
  --without-headers \
  --with-python=/usr/bin/python3 \
  --enable-tui=yes \
  --with-expat

Now build the source code:

make -j$(nproc)