From 92831234d6320e9cc3eb8d15dbbdbbc22fe14dea Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Tue, 23 Jan 2024 20:37:24 +0000 Subject: [PATCH] using crate semihosting on aarch64 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This crate provides access to semihosting, a mechanism for programs running on the real or virtual (e.g., QEMU) target to communicate with I/O facilities on the host system. Co-authored-by: Martin Kröning --- Cargo.lock | 7 +++++++ Cargo.toml | 5 ++++- src/arch/aarch64/kernel/processor.rs | 19 ++++++++++++++++--- src/arch/riscv64/kernel/processor.rs | 11 +++++++++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 48eb435ddf..e2757cf4be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -519,6 +519,7 @@ dependencies = [ "rand_chacha", "riscv", "sbi", + "semihosting", "shell-words", "smallvec", "smoltcp", @@ -1075,6 +1076,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "semihosting" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab7a0be8d9e9893dfb5ce313aa0324396936d8bf788f5ef493c9f122ad84fd8" + [[package]] name = "shell-words" version = "1.1.0" diff --git a/Cargo.toml b/Cargo.toml index d3d41d3c91..efa9e8796c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,7 @@ udp = ["smoltcp", "smoltcp/socket-udp"] trace = [] vga = [] common-os = [] +semihosting = ["dep:semihosting"] [dependencies] ahash = { version = "0.8", default-features = false } @@ -85,7 +86,6 @@ num-traits = { version = "0.2", default-features = false } pci-ids = { version = "0.2", optional = true } pci_types = { version = "0.6" } pflock = "0.2" -qemu-exit = "3.0" rand_chacha = { version = "0.3", default-features = false } shell-words = { version = "1.1", default-features = false } smallvec = { version = "1", features = ["const_new"] } @@ -122,17 +122,20 @@ multiboot = "0.8" uart_16550 = "0.3" x86 = { version = "0.52", default-features = false } x86_64 = "0.14" +qemu-exit = "3.0" [target.'cfg(target_arch = "aarch64")'.dependencies] aarch64 = { version = "0.0", default-features = false } arm-gic = { version = "0.1" } hermit-dtb = { version = "0.1" } +semihosting = { version = "0.1", optional = true } [target.'cfg(target_arch = "riscv64")'.dependencies] fdt = "0.1" riscv = "0.11" sbi = "0.2" trapframe = "0.9" +semihosting = { version = "0.1", optional = true } [dev-dependencies] float-cmp = "0.9" diff --git a/src/arch/aarch64/kernel/processor.rs b/src/arch/aarch64/kernel/processor.rs index d204614f6f..9d025ddbf4 100644 --- a/src/arch/aarch64/kernel/processor.rs +++ b/src/arch/aarch64/kernel/processor.rs @@ -4,7 +4,6 @@ use core::{fmt, str}; use aarch64::regs::{Readable, CNTFRQ_EL0}; use hermit_dtb::Dtb; use hermit_sync::{without_interrupts, Lazy}; -use qemu_exit::QEMUExit; use crate::arch::aarch64::kernel::boot_info; use crate::env; @@ -119,8 +118,22 @@ pub fn halt() { pub fn shutdown() -> ! { info!("Shutting down system"); - let exit_handler = qemu_exit::AArch64::new(); - exit_handler.exit_success(); + cfg_if::cfg_if! { + if #[cfg(feature = "semihosting")] { + semihosting::process::exit(0) + } else { + unsafe { + const PSCI_SYSTEM_OFF: u64 = 0x84000008; + // call hypervisor to shut down the system + asm!("hvc #0", in("x0") PSCI_SYSTEM_OFF, options(nomem, nostack)); + + // we should never reach this point + loop { + asm!("wfe", options(nomem, nostack)); + } + } + } + } } #[inline] diff --git a/src/arch/riscv64/kernel/processor.rs b/src/arch/riscv64/kernel/processor.rs index 56ac9a3317..399e87e69d 100644 --- a/src/arch/riscv64/kernel/processor.rs +++ b/src/arch/riscv64/kernel/processor.rs @@ -229,8 +229,15 @@ pub fn halt() { /// Shutdown the system pub fn shutdown() -> ! { info!("Shutting down system"); - //SBI shutdown - sbi::legacy::shutdown() + + cfg_if::cfg_if! { + if #[cfg(feature = "semihosting")] { + semihosting::process::exit(0) + } else { + // use SBI shutdown + sbi::legacy::shutdown() + } + } } pub fn get_timer_ticks() -> u64 {