feat(adapter-riscv): boot on QEMU virt with UART hello world
This commit is contained in:
61
crates/adapter-riscv/src/main.rs
Normal file
61
crates/adapter-riscv/src/main.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
mod uart;
|
||||
|
||||
use core::arch::global_asm;
|
||||
use uart::Uart;
|
||||
|
||||
global_asm!(
|
||||
r#"
|
||||
.section .text.entry
|
||||
.globl _start
|
||||
_start:
|
||||
la sp, _stack_top
|
||||
call kmain
|
||||
spin:
|
||||
wfi
|
||||
j spin
|
||||
"#
|
||||
);
|
||||
|
||||
unsafe extern "C" {
|
||||
static _bss_start: u8;
|
||||
static _bss_end: u8;
|
||||
}
|
||||
|
||||
fn zero_bss() {
|
||||
unsafe {
|
||||
let start = &_bss_start as *const u8 as *mut u8;
|
||||
let end = &_bss_end as *const u8 as *mut u8;
|
||||
let len = end as usize - start as usize;
|
||||
core::ptr::write_bytes(start, 0, len);
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn kmain() -> ! {
|
||||
zero_bss();
|
||||
|
||||
let uart = Uart::new();
|
||||
uart.write_str("k-os: booted on RISC-V\n");
|
||||
|
||||
loop {
|
||||
unsafe { core::arch::asm!("wfi") };
|
||||
}
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||
let uart = Uart::new();
|
||||
uart.write_str("PANIC: ");
|
||||
if let Some(location) = info.location() {
|
||||
uart.write_str(location.file());
|
||||
uart.put_char(b':');
|
||||
uart.write_dec(location.line() as usize);
|
||||
}
|
||||
uart.put_char(b'\n');
|
||||
loop {
|
||||
unsafe { core::arch::asm!("wfi") };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user