Notes

Linux Kernel for RISC-V Build Commands on macOS

· 1min

Quick reference for the workflow walked through in How I Compiled and Ran a Linux Kernel for RISC-V on My Apple Silicon Mac.

Generate a Starting Config

cd /Volumes/linuxkernel/linux
gmake ARCH=riscv LLVM=1 defconfig

Toggle Config Options

./scripts/config --enable NONPORTABLE
./scripts/config --enable HVC_RISCV_SBI
gmake ARCH=riscv LLVM=1 olddefconfig

Verify a Config Option

grep -E "^CONFIG_HVC_RISCV_SBI" .config

Build the Kernel

gmake ARCH=riscv LLVM=1 -j$(nproc) \
  HOSTCFLAGS="-Iscripts/macos-include -I$(brew --prefix libelf)/include -Wno-incompatible-pointer-types -D_UUID_T"

Build the Init Binary

clang --target=riscv64-linux-gnu -static -nostdlib -fuse-ld=lld \
  -o /Volumes/linuxkernel/initramfs/init \
  /Volumes/linuxkernel/initramfs/init.c

Repack the Initramfs

/Volumes/linuxkernel/linux/usr/gen_init_cpio /Volumes/linuxkernel/initramfs.txt \
  | gzip > /Volumes/linuxkernel/initramfs.cpio.gz

Boot in QEMU

qemu-system-riscv64 -M virt -nographic \
  -kernel /Volumes/linuxkernel/linux/arch/riscv/boot/Image \
  -initrd /Volumes/linuxkernel/initramfs.cpio.gz \
  -append "earlycon=sbi console=hvc0"

Boot for Debugging

CPU paused, gdb stub on port 1234.

qemu-system-riscv64 -M virt -nographic \
  -kernel /Volumes/linuxkernel/linux/arch/riscv/boot/Image \
  -initrd /Volumes/linuxkernel/initramfs.cpio.gz \
  -append "earlycon=sbi console=hvc0" \
  -s -S

Attach LLDB

Run in another terminal while QEMU is paused.

lldb /Volumes/linuxkernel/linux/vmlinux
# Then inside lldb:
#   gdb-remote localhost:1234
#   breakpoint set --name start_kernel
#   continue



Comments