Skip to content

Instantly share code, notes, and snippets.

@cyring
Last active March 12, 2025 06:17
Show Gist options
  • Save cyring/adb32ec352cffcecba278e1b1c73b32b to your computer and use it in GitHub Desktop.
Save cyring/adb32ec352cffcecba278e1b1c73b32b to your computer and use it in GitHub Desktop.
RISC-V Ready
@cyring
Copy link
Author

cyring commented Feb 19, 2025

  1. Download one of the Ubuntu image for RISC-V: < ubuntu-24.10-live-server-riscv64.img >
  2. Refer to the documentation < Install Ubuntu on QEMU (RISC-V) >
  3. Mount the image to retreive the installation kernel and initrd
sudo losetup -P -f ubuntu-24.10-live-server-riscv64.img
sudo mount /dev/loop0p2 /mnt
cp -v /mnt/casper/{vmlinux,initrd} .
sudo umount /mnt
  1. Create your Ubuntu disk
qemu-img create -f qcow2 ubuntu_rv64.img 25G
  1. Boot the QEMU / RISC-V / Ubuntu
qemu-system-riscv64 -kernel vmlinux -initrd initrd -append "root=/dev/vda nokaslr audit=0" \
-machine virt -nographic -m 2048 -smp 4 \
-drive file=ubuntu-24.10-live-server-riscv64.img,format=raw,if=virtio \
-drive file=ubuntu_rv64.img \
-device virtio-net-device,netdev=eth0 -netdev user,id=eth0 \
-device virtio-rng-pci

2025-02-19-121231_642x410_scrot

2025-02-19-121538_642x410_scrot

2025-02-19-121249_642x410_scrot

2025-02-19-121308_642x410_scrot

2025-02-19-121408_642x410_scrot

2025-02-19-122937_642x410_scrot

2025-02-19-122958_642x410_scrot

2025-02-19-123101_642x410_scrot

@cyring
Copy link
Author

cyring commented Feb 21, 2025

2025-02-21-100909_642x410_scrot

2025-02-21-101210_642x410_scrot

2025-02-21-101238_642x410_scrot

2025-02-21-101444_642x410_scrot

@cyring
Copy link
Author

cyring commented Feb 21, 2025

2025-02-21-101555_642x410_scrot

2025-02-21-101636_642x410_scrot

2025-02-21-102553_642x410_scrot

@cyring
Copy link
Author

cyring commented Feb 22, 2025

sudo modprobe nbd max_part=8
sudo qemu-nbd --connect=/dev/nbd0 ubuntu_rv64.img
sudo mount /dev/nbd0p2 /mnt/

mkdir -p target/boot
sudo cp -v /mnt/boot/vmlinuz-6.11.0-17-generic target/boot/
sudo cp -v /mnt/boot/initrd.img-6.11.0-17-generic target/boot/
sudo chown cyril:users target/boot/*

sudo umount /dev/nbd0p2
sudo qemu-nbd --disconnect /dev/nbd0
sudo modprobe -r nbd
qemu-system-riscv64 -kernel target/boot/vmlinuz-6.11.0-17-generic \
-initrd target/boot/initrd.img-6.11.0-17-generic \
-append "root=/dev/vda2 nokaslr audit=0" \
-machine virt -nographic -m 2048 -smp 4 \
-drive file=ubuntu_rv64.img \
-device virtio-net-device,netdev=eth0 \
-netdev user,id=eth0 \
-device virtio-rng-pci
OpenSBI v1.5.1
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : medeleg
Platform HART Count       : 4
Platform IPI Device       : aclint-mswi
Platform Timer Device     : aclint-mtimer @ 10000000Hz
Platform Console Device   : uart8250
Platform HSM Device       : ---
Platform PMU Device       : ---
Platform Reboot Device    : syscon-reboot
Platform Shutdown Device  : syscon-poweroff
Platform Suspend Device   : ---
Platform CPPC Device      : ---
Firmware Base             : 0x80000000
Firmware Size             : 357 KB
Firmware RW Offset        : 0x40000
Firmware RW Size          : 101 KB
Firmware Heap Offset      : 0x4f000
Firmware Heap Size        : 41 KB (total), 2 KB (reserved), 11 KB (used), 27 KB (free)
Firmware Scratch Size     : 4096 B (total), 416 B (used), 3680 B (free)
Runtime SBI Version       : 2.0

Domain0 Name              : root
Domain0 Boot HART         : 0
Domain0 HARTs             : 0*,1*,2*,3*
Domain0 Region00          : 0x0000000000100000-0x0000000000100fff M: (I,R,W) S/U: (R,W)
Domain0 Region01          : 0x0000000010000000-0x0000000010000fff M: (I,R,W) S/U: (R,W)
Domain0 Region02          : 0x0000000002000000-0x000000000200ffff M: (I,R,W) S/U: ()
Domain0 Region03          : 0x0000000080040000-0x000000008005ffff M: (R,W) S/U: ()
Domain0 Region04          : 0x0000000080000000-0x000000008003ffff M: (R,X) S/U: ()
Domain0 Region05          : 0x000000000c400000-0x000000000c5fffff M: (I,R,W) S/U: (R,W)
Domain0 Region06          : 0x000000000c000000-0x000000000c3fffff M: (I,R,W) S/U: (R,W)
Domain0 Region07          : 0x0000000000000000-0xffffffffffffffff M: () S/U: (R,W,X)
Domain0 Next Address      : 0x0000000080200000
Domain0 Next Arg1         : 0x00000000bfe00000
Domain0 Next Mode         : S-mode
Domain0 SysReset          : yes
Domain0 SysSuspend        : yes

Boot HART ID              : 0
Boot HART Domain          : root
Boot HART Priv Version    : v1.12
Boot HART Base ISA        : rv64imafdch
Boot HART ISA Extensions  : sstc,zicntr,zihpm,zicboz,zicbom,sdtrig,svadu
Boot HART PMP Count       : 16
Boot HART PMP Granularity : 2 bits
Boot HART PMP Address Bits: 54
Boot HART MHPM Info       : 16 (0x0007fff8)
Boot HART Debug Triggers  : 2 triggers
Boot HART MIDELEG         : 0x0000000000001666
Boot HART MEDELEG         : 0x0000000000f0b509
[    0.000000] Linux version 6.11.0-17-generic (buildd@bos03-riscv64-050) (riscv64-linux-gnu-gcc-14 (Ubuntu 14.2.0-4ubuntu2) 14.2.0, GNU ld (GNU Binutils for Ubuntu) 2.43.1) #17.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jan 29 02:24:01 UTC 2025 (Ubuntu 6.11.0-17.17.1-generic 6.11.11)
...
[    9.279836] systemd[1]: systemd 256.5-2ubuntu3 running in system mode (+PAM +AUDIT +SELINUX +APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBCRYPTSETUP_PLUGINS +LIBFDISK +PCRE2 +PWQUALITY +P11KIT +QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +BPF_FRAMEWORK -XKBCOMMON +UTMP +SYSVINIT +LIBARCHIVE)
[    9.281028] systemd[1]: Detected virtualization qemu.
[    9.281464] systemd[1]: Detected architecture riscv64.

Welcome to Ubuntu 24.10!

[    9.293420] systemd[1]: Hostname set to <localhost.localdomain>.
...
[  OK  ] Finished cloud-config.service - Cloud-init: Config Stage.

Ubuntu 24.10 localhost ttyS0

localhost login: 

@cyring
Copy link
Author

cyring commented Feb 22, 2025

Recover Password

qemu-system-riscv64 -kernel target/boot/vmlinuz-6.11.0-17-generic \
-initrd target/boot/initrd.img-6.11.0-17-generic \
-append "root=/dev/vda2 nokaslr audit=0 init=/bin/sh" \
-machine virt -nographic -m 2048 -smp 4 \
-drive file=ubuntu_rv64.img -device virtio-net-device,netdev=eth0 -netdev user,id=eth0 -device virtio-rng-pci
mount -o remount,rw /
passwd
passwd: password updated successfully
usermod -l cyril ubuntu
groupmod -n cyril ubuntu
usermod cyril -c Cyril
usermod cyril -d /home/cyril
mv -v /home/ubuntu /home/cyril
mount -o remount,ro /
poweroff -f

Boot nominal

qemu-system-riscv64 -kernel target/boot/vmlinuz-6.11.0-17-generic \
-initrd target/boot/initrd.img-6.11.0-17-generic \
-append "root=/dev/vda2 nokaslr audit=0 quiet" \
-machine virt -nographic -m 2048 -smp 4 \
-drive file=ubuntu_rv64.img -device virtio-net-device,netdev=eth0 -netdev user,id=eth0 -device virtio-rng-pci

2025-02-22-065230_642x563_scrot

root@localhost:~# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda    253:0    0  25G  0 disk 
├─vda1 253:1    0   1M  0 part 
└─vda2 253:2    0  25G  0 part /
root@localhost:~# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2        25G  4.8G   19G  21% /
root@localhost:~# free -m
               total        used        free      shared  buff/cache   available
Mem:            1955         209        1639           0         182        1746
Swap:           2047           0        2047
root@localhost:~# uname -a
Linux localhost 6.11.0-17-generic #17.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Jan 29 02:24:01 UTC 2025 riscv64 riscv64 riscv64 GNU/Linux
root@localhost:~# lspci
00:00.0 Host bridge: Red Hat, Inc. QEMU PCIe Host bridge
00:01.0 Unclassified device [00ff]: Red Hat, Inc. Virtio RNG
00:02.0 SCSI storage controller: Red Hat, Inc. Virtio block device

@cyring
Copy link
Author

cyring commented Feb 22, 2025

Ubuntu 24.10 localhost ttyS0

localhost login: cyril

## you should already have git and the kernel headers
sudo apt install make gcc

mkdir ~/src && cd ~/src
git clone -b riscv64 https://github.com/cyring/CoreFreq.git
cd CoreFreq 
make -j

@cyring
Copy link
Author

cyring commented Mar 6, 2025

OpenSBI

git clone linux.git
git clone opensbi.git

cd linux
make -j8 ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- Image

cd opensbi
CROSS_COMPILE=riscv64-unknown-linux-gnu- make PLATFORM=generic FW_PAYLOAD_PATH=../linux/arch/riscv/boot/Image

qemu-system-riscv64 -M virt -m 256M -nographic -bios build/platform/generic/firmware/fw_payload.elf
OpenSBI v0.8-85-g0d49c3b
   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : timer,mfdeleg
Platform HART Count       : 2
Firmware Base             : 0x80000000
Firmware Size             : 124 KB
Runtime SBI Version       : 0.2
...
[    0.000000] Linux version 6.3.0-rc3-00021-ga1effab7a3a3 ([email protected]) (riscv64-unknown-linux-gnu-gcc (g5964b5cd727) 11.1.0, GNU ld (GNU Binutils) 2.38) #1 SMP Thu Mar  6 15:02:11 CET 2025
[    0.000000] OF: fdt: Ignoring memory range 0x80000000 - 0x80200000
[    0.000000] Machine model: riscv-virtio,qemu
[    0.000000] efi: UEFI not found.
[    0.000000] OF: reserved mem: 0x0000000080000000..0x000000008001ffff (128 KiB) map non-reusable mmode_resv0@80000000
[    0.000000] Zone ranges:
[    0.000000]   DMA32    [mem 0x0000000080200000-0x000000008fffffff]
[    0.000000]   Normal   empty
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0: [mem 0x0000000080200000-0x000000008fffffff]
[    0.000000] Initmem setup node 0 [mem 0x0000000080200000-0x000000008fffffff]
[    0.000000] On node 0, zone DMA32: 512 pages in unavailable ranges
[    0.000000] SBI specification v0.2 detected
[    0.000000] SBI implementation ID=0x1 Version=0x8
[    0.000000] SBI TIME extension detected
[    0.000000] SBI IPI extension detected
[    0.000000] SBI RFENCE extension detected
[    0.000000] SBI HSM extension detected
[    0.000000] riscv: base ISA extensions acdfim
[    0.000000] riscv: ELF capabilities acdfim
[    0.000000] percpu: Embedded 19 pages/cpu s39992 r8192 d29640 u77824
[    0.000000] Built 1 zonelists, mobility grouping on.  Total pages: 64008
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment