Skip to content

Commit

Permalink
remove assembly code and replace it by helper crates
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed May 1, 2024
1 parent 027ebd1 commit d1d310f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 30 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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"

15 changes: 2 additions & 13 deletions src/arch/aarch64/processor.rs
Original file line number Diff line number Diff line change
@@ -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)
}
23 changes: 14 additions & 9 deletions src/arch/x86_64/processor.rs
Original file line number Diff line number Diff line change
@@ -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::<u32>::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() {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub extern "C" fn main() -> ! {
println!("Hello world!");

// shutdown system
shutdown();
shutdown(0);
}

/// This function is called on panic.
Expand All @@ -36,5 +36,5 @@ pub fn panic(info: &PanicInfo) -> ! {

print!("\n");

loop {}
shutdown(1);
}

0 comments on commit d1d310f

Please sign in to comment.