How to bring up the Linux Kernel on a new platform
Bringing up the Linux Kernel on a new platform can be a complex task, but it is essential for running embedded systems. The most critical requirement for Linux to run is the hardware, which in this case is a minimal, emulated RISC-V CPU. The implementation involves 2000 lines of C++ code, including a simple RAM peripheral, SBI interface for timers, and a UART peripheral based on the WD8250 chip.
Although the process of creating the emulator is beyond the scope of this article, the full implementation can be found at WerWolv/riscv-emulator. In a real system, the emulator would not be necessary, as the hardware would already be present. Instead, the CPU's address space would be mapped with the peripheral devices, and the kernel, device tree, and initramfs would be loaded using a bootloader like u-boot.
To get Linux running, one must configure and compile the Linux Kernel for the target platform, populate the LinuxKernel, DeviceTreeBlob, and InitRamFs arrays with the appropriate data, and compile the toolchain. The toolchain is essential for building applications that run on the target platform. The crosstool-ng Project simplifies the process of creating a toolchain for 32-bit RISC-V Linux by handling all the necessary configurations and compilations.
After compiling the toolchain, one can then compile the Linux Kernel using the crosstool-ng toolchain. The compilation process can take up to an hour or more, depending on the machine's performance. Once the compilation is complete, the arch/riscv/boot/Image file contains the compiled kernel, which can now be executed. Additionally, a simple init program can be compiled as a statically linked binary and included in the initramfs, which is an Initial RAM File System containing files and directories available after the system has booted.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.