From d1d310f1d1127dac75f25aa27ae1c88489ade775 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Wed, 1 May 2024 20:24:59 +0200 Subject: [PATCH] remove assembly code and replace it by helper crates --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ Cargo.toml | 13 +++++++------ src/arch/aarch64/processor.rs | 15 ++------------- src/arch/x86_64/processor.rs | 23 ++++++++++++++--------- src/main.rs | 4 ++-- 5 files changed, 57 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fa54d8e4..87b40813 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,7 +51,9 @@ dependencies = [ "bootloader", "hermit-sync", "qemu-exit", + "semihosting", "x86", + "x86_64", ] [[package]] @@ -156,12 +158,24 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "rustversion" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80af6f9131f277a45a3fba6ce8e2258037bb0477a67e610d3c1fe046ab31de47" + [[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "semihosting" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5260e8474680a4617e36cee7f4602655c89146bde012a5b6ba7cc080ec49dcd" + [[package]] name = "spinning_top" version = "0.3.0" @@ -171,6 +185,12 @@ dependencies = [ "lock_api", ] +[[package]] +name = "volatile" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793" + [[package]] name = "x86" version = "0.52.0" @@ -181,3 +201,15 @@ dependencies = [ "bitflags 1.3.2", "raw-cpuid", ] + +[[package]] +name = "x86_64" +version = "0.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bc79523af8abf92fb1a970c3e086c5a343f6bcc1a0eb890f575cbb3b45743df" +dependencies = [ + "bit_field", + "bitflags 2.5.0", + "rustversion", + "volatile", +] diff --git a/Cargo.toml b/Cargo.toml index a802f1b9..f9e6d29e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ build-command = ["build"] # The command invoked with the created bootimage (the "{}" will be replaced # with the path to the bootable disk image) # Applies to `bootimage run` and `bootimage runner` -run-command = ["qemu-system-x86_64", "-display", "none", "-smp", "1", "-m", "128M", "-serial", "stdio", "-cpu", "qemu64,apic,fsgsbase,rdtscp,xsave,xsaveopt,fxsr", "-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-drive", "format=raw,file={}"] +run-command = ["qemu-system-x86_64", "-display", "none", "-smp", "1", "-m", "256M", "-serial", "stdio", "-cpu", "qemu64,apic,fsgsbase,rdtscp,xsave,xsaveopt,fxsr", "-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-drive", "format=raw,file={}"] # Additional arguments passed to the run command for non-test executables # Applies to `bootimage run` and `bootimage runner` run-args = [] @@ -19,10 +19,11 @@ run-args = [] hermit-sync = "0.1.6" qemu-exit = "3.0" # Spinlocks. -[target.'cfg(target_arch = "x86_64")'.dependencies.bootloader] -version = "0.9.29" +[target.'cfg(target_arch = "aarch64")'.dependencies] +semihosting = "0.1.9" -[target.'cfg(target_arch = "x86_64")'.dependencies.x86] -version = "0.52" -default-features = false +[target.'cfg(target_arch = "x86_64")'.dependencies] +bootloader = "0.9.29" +x86 = { version = "0.52", default-features = false } +x86_64 = "0.15" diff --git a/src/arch/aarch64/processor.rs b/src/arch/aarch64/processor.rs index 503b0abb..98f50379 100644 --- a/src/arch/aarch64/processor.rs +++ b/src/arch/aarch64/processor.rs @@ -1,15 +1,4 @@ -#![allow(dead_code)] -use core::arch::asm; - #[no_mangle] -pub extern "C" fn shutdown() -> ! { - unsafe { - asm!( - "mov w0, 0x18", - "mov x1, #0x20000", - "add x1, x1, #0x26", - "hlt #0xF000", - options(nostack, nomem, noreturn), - ); - } +pub extern "C" fn shutdown(error_code: i32) -> ! { + semihosting::process::exit(error_code) } diff --git a/src/arch/x86_64/processor.rs b/src/arch/x86_64/processor.rs index 08b01f85..5223f7c7 100644 --- a/src/arch/x86_64/processor.rs +++ b/src/arch/x86_64/processor.rs @@ -1,20 +1,25 @@ -#![allow(dead_code)] - -use core::arch::asm; -use qemu_exit::QEMUExit; use x86::controlregs::*; +use x86_64::instructions::port::Port; pub fn halt() { unsafe { - asm!("hlt", options(nomem, nostack)); + x86::halt(); + } +} + +fn qemu_exit(success: bool) { + let code = if success { 3 >> 1 } else { 0 }; + unsafe { + Port::::new(0xf4).write(code); } } #[no_mangle] -pub extern "C" fn shutdown() -> ! { - // shutdown, works like Qemu's shutdown command - let qemu_exit_handle = qemu_exit::X86::new(0xf4, 5); - qemu_exit_handle.exit_success(); +pub extern "C" fn shutdown(error_code: i32) -> ! { + qemu_exit(error_code == 0); + loop { + halt(); + } } pub fn cpu_init() { diff --git a/src/main.rs b/src/main.rs index 49fc0170..badf975f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ pub extern "C" fn main() -> ! { println!("Hello world!"); // shutdown system - shutdown(); + shutdown(0); } /// This function is called on panic. @@ -36,5 +36,5 @@ pub fn panic(info: &PanicInfo) -> ! { print!("\n"); - loop {} + shutdown(1); }