Subject: QEMU fails to boot Alpine RISCV64?

From: Péter Szilágyi <[email-masked]>
Date: Fri, 6 Dec 2024 19:48:10 +0200

Hello,

I've used alpine-make-rootfs to create an initramfs to be able to boot
a stateless, ram-only alpine image. It works great on amd64 and arm64,
I can start them up in QEMU. On riscv64, it creates all the needed
files as before, but I simply cannot convince QEMU to actually start
it. It just hangs on bios startup. I've tried other test images and
they work fine, only the Alpine one hangs. Was wondering if I'm
missing something?

The way this tool works is that it's run in a docker container for the
required platform (amd64, arm64, riscv64) and installs the `linux-lts`
package, which among others contains `vmlinuz-lts`, the kernel needed
by QEMU. This file works well for most plafroms, but not for RISCV64.

https://pkgs.alpinelinux.org/contents?file=&path=%2Fboot&name=linux-lts&branch=edge&repo=main&arch=x86_64

Now, my guess is that I'm missing something super obvious, because
surely Alpine can run on riscv64 and qemu should surely be able to run
it... but I've spent an entire day looking at a hung riscv64 bios and
I'm just baffled at this point.

I'm trying to run it with:

qemu-system-riscv64 -M virt -cpu rv64 -m 4G -kernel
"images/$image/vmlinuz-lts" -initrd "images/$image/initramfs-lts"
-append "console=ttyS0" -nographic

And it just prints all the bios stuff (so that works) and then hangs
when trying to give control over to Alpine. Hepl?

OpenSBI v1.5.1

   ____                    _____ ____ _____
  / __ \                  / ____|  _ \_   _|
 | |  | |_ __   ___ _ __ | (___ | |_) || |
 | |  | | '_ \ / _ \ '_ \ \___ \|  _ < | |
 | |__| | |_) |  __/ | | |____) | |_) || |_
  \____/| .__/ \___|_| |_|_____/|____/_____|
        | |
        |_|

Platform Name             : riscv-virtio,qemu
Platform Features         : medeleg
Platform HART Count       : 1
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             : 327 KB
Firmware RW Offset        : 0x40000
Firmware RW Size          : 71 KB
Firmware Heap Offset      : 0x49000
Firmware Heap Size        : 35 KB (total), 2 KB (reserved), 11 KB
(used), 21 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*
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

---

From: Naomi Rennie-Waldock <[email-masked]>
Date: Sun, 29 Dec 2024 19:15:00 +0000

Péter Szilágyi <[email-masked]> 
<mailto:~alpine/[email-masked]?cc=P%C3%A9ter%20Szil%C3%A1gyi%20%3Cpeterke%40gmail.com%3E&in-reply-to=%3CCANBVciK7GL_fy_ipOFqyL0v-TyvvWJhQ1aZzVDtEvY4NWee%3D%3Dg%40mail.gmail.com%3E&subject=Re%3A%20QEMU%20fails%20to%20boot%20Alpine%20RISCV64%3F> 

 > when trying to give control over to Alpine. Hepl?

OpenSBI won't decompress the kernel, you'll need to do that yourself 
(eg: zcat vmlinuz-lts > vmlinux-lts ).
Some of the needed drivers are also not yet enabled for RISC-V, I've 
submitted a patch for that:
https://gitlab.alpinelinux.org/alpine/aports/-/merge_requests/77727