Skip to content

Commit

Permalink
qemu: riscv64: switch from u-boot to EDK2
Browse files Browse the repository at this point in the history
u-boot no longer needs to be specified as the "kernel" image,
as EDK2 (`edk2-riscv-code.fd`) is available since QEMU v9.1.

ACPI is disabled, as suggested in
https://github.com/tianocore/edk2/blob/edk2-stable202408/OvmfPkg/RiscVVirt/README.md#test

Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Sep 11, 2024
1 parent ac1fc4f commit 44e269d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
11 changes: 2 additions & 9 deletions examples/experimental/riscv64.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
# This template requires Lima v0.11.0 or later.
# This template requires Lima v1.0 or later, with QEMU v9.1 or later.
# Lima v0.23 users should use https://raw.githubusercontent.com/lima-vm/lima/v0.23.2/examples/experimental/riscv64.yaml

arch: "riscv64"
images:
- location: "https://cloud-images.ubuntu.com/releases/24.04/release-20240821/ubuntu-24.04-server-cloudimg-riscv64.img"
arch: "riscv64"
digest: "sha256:f5886ad4e405e689585dfef0e96c31b06478e0cb12bc7f3fae965759a32d729e"
kernel:
# Extracted from http://http.us.debian.org/debian/pool/main/u/u-boot/u-boot-qemu_2023.07+dfsg-1_all.deb (GPL-2.0)
location: "https://github.com/lima-vm/u-boot-qemu-mirror/releases/download/2023.07%2Bdfsg-7/qemu-riscv64_smode_uboot.elf"
digest: "sha256:d4b3a10c3ef04219641802a586dca905e768805f5a5164fb68520887df54f33c"
# Fallback to the latest release image.
# Hint: run `limactl prune` to invalidate the cache
- location: "https://cloud-images.ubuntu.com/releases/24.04/release/ubuntu-24.04-server-cloudimg-riscv64.img"
arch: "riscv64"
kernel:
# Extracted from http://http.us.debian.org/debian/pool/main/u/u-boot/u-boot-qemu_2023.07+dfsg-1_all.deb (GPL-2.0)
location: "https://github.com/lima-vm/u-boot-qemu-mirror/releases/download/2023.07%2Bdfsg-1/qemu-riscv64_smode_uboot.elf"
digest: "sha256:d4b3a10c3ef04219641802a586dca905e768805f5a5164fb68520887df54f33c"

mounts:
- location: "~"
Expand Down
2 changes: 0 additions & 2 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ func Validate(y *LimaYAML, warn bool) error {
if f.Kernel.Arch != f.Arch {
return fmt.Errorf("images[%d].kernel has unexpected architecture %q, must be %q", i, f.Kernel.Arch, f.Arch)
}
} else if f.Arch == RISCV64 {
return errors.New("riscv64 needs the kernel (e.g., \"uboot.elf\") to be specified")
}
if f.Initrd != nil {
if err := validateFileObject(*f.Initrd, fmt.Sprintf("images[%d].initrd", i)); err != nil {
Expand Down
23 changes: 19 additions & 4 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,12 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er
}
args = appendArgsIfNoConflict(args, "-machine", machine)
case limayaml.RISCV64:
machine := "virt,accel=" + accel
// https://github.com/tianocore/edk2/blob/edk2-stable202408/OvmfPkg/RiscVVirt/README.md#test
// > Note: the `acpi=off` machine property is specified because Linux guest
// > support for ACPI (that is, the ACPI consumer side) is a work in progress.
// > Currently, `acpi=off` is recommended unless you are developing ACPI support
// > yourself.
machine := "virt,acpi=off,accel=" + accel
args = appendArgsIfNoConflict(args, "-machine", machine)
case limayaml.ARMV7L:
machine := "virt,accel=" + accel
Expand Down Expand Up @@ -610,7 +615,7 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er
firmware = downloadedFirmware
logrus.Infof("Using existing firmware (%q)", firmware)
}
if firmware == "" && *y.Arch != limayaml.RISCV64 {
if firmware == "" {
firmware, err = getFirmware(exe, *y.Arch)
if err != nil {
return "", nil, err
Expand Down Expand Up @@ -1028,6 +1033,14 @@ func qemuArch(arch limayaml.Arch) string {
return arch
}

// qemuEdk2 returns the arch string used by `/usr/local/share/qemu/edk2-*-code.fd`.
func qemuEdk2Arch(arch limayaml.Arch) string {
if arch == limayaml.RISCV64 {
return "riscv"
}
return qemuArch(arch)
}

func Exe(arch limayaml.Arch) (exe string, args []string, err error) {
exeBase := "qemu-system-" + qemuArch(arch)
envK := "QEMU_SYSTEM_" + strings.ToUpper(qemuArch(arch))
Expand Down Expand Up @@ -1091,7 +1104,7 @@ func getQemuVersion(qemuExe string) (*semver.Version, error) {

func getFirmware(qemuExe string, arch limayaml.Arch) (string, error) {
switch arch {
case limayaml.X8664, limayaml.AARCH64, limayaml.ARMV7L:
case limayaml.X8664, limayaml.AARCH64, limayaml.ARMV7L, limayaml.RISCV64:
default:
return "", fmt.Errorf("unexpected architecture: %q", arch)
}
Expand All @@ -1105,7 +1118,7 @@ func getFirmware(qemuExe string, arch limayaml.Arch) (string, error) {
localDir := filepath.Dir(binDir) // "/usr/local"
userLocalDir := filepath.Join(currentUser.HomeDir, ".local") // "$HOME/.local"

relativePath := fmt.Sprintf("share/qemu/edk2-%s-code.fd", qemuArch(arch))
relativePath := fmt.Sprintf("share/qemu/edk2-%s-code.fd", qemuEdk2Arch(arch))
candidates := []string{
filepath.Join(userLocalDir, relativePath), // XDG-like
filepath.Join(localDir, relativePath), // macOS (homebrew)
Expand All @@ -1132,6 +1145,8 @@ func getFirmware(qemuExe string, arch limayaml.Arch) (string, error) {
// Debian package "qemu-efi-arm"
// Fedora package "edk2-arm"
candidates = append(candidates, "/usr/share/AAVMF/AAVMF32_CODE.fd")
case limayaml.RISCV64:
// NOP, as EDK2 for RISCV64 is not packaged yet in well-known distros.
}

logrus.Debugf("firmware candidates = %v", candidates)
Expand Down
3 changes: 2 additions & 1 deletion pkg/qemu/qemu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ func (l *LimaQemuDriver) killVhosts() error {
}

func (l *LimaQemuDriver) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error {
logrus.Info("Shutting down QEMU with ACPI")
// "power button" refers to ACPI on the most archs, except RISC-V
logrus.Info("Shutting down QEMU with the power button")
if usernetIndex := limayaml.FirstUsernetIndex(l.Yaml); usernetIndex != -1 {
client := usernet.NewClientByName(l.Yaml.Networks[usernetIndex].Lima)
err := client.UnExposeSSH(l.SSHLocalPort)
Expand Down

0 comments on commit 44e269d

Please sign in to comment.