-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.rs
More file actions
62 lines (49 loc) · 1.47 KB
/
Copy pathmain.rs
File metadata and controls
62 lines (49 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![no_main]
#![no_std]
core::arch::global_asm!(include_str!("start.S"));
use core::alloc::{GlobalAlloc, Layout};
use core::fmt::Write;
use core::panic::PanicInfo;
use core::ptr::NonNull;
use qemu_exit::QEMUExit;
use uart_16550::{Config, Uart16550Tty};
struct DummyGlobalAlloc;
#[global_allocator]
static GLOBAL_ALLOCATOR: DummyGlobalAlloc = DummyGlobalAlloc;
unsafe impl GlobalAlloc for DummyGlobalAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
panic!("unsupported! layout={layout:?}");
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
panic!("unsupported! ptr={ptr:?}, layout={layout:?}");
}
}
const UART_BASE: usize = 0x1000_0000;
const UART_STRIDE: u8 = 1;
const SIFIVE_TEST_BASE: u64 = 0x0010_0000;
#[unsafe(no_mangle)]
extern "C" fn rust_entry() -> ! {
main()
}
fn exit_qemu(success: bool) -> ! {
let exit = qemu_exit::RISCV64::new(SIFIVE_TEST_BASE);
if success {
exit.exit_success()
} else {
exit.exit_failure()
}
}
fn main() -> ! {
let uart_base = NonNull::new(UART_BASE as *mut u8).expect("valid MMIO base");
unsafe {
let mut uart = Uart16550Tty::new_mmio(uart_base, UART_STRIDE, Config::default())
.expect("MMIO UART should initialize");
uart.write_str("hello from serial via riscv mmio")
.expect("serial write should succeed");
}
exit_qemu(true)
}
#[panic_handler]
fn panic_handler(_info: &PanicInfo) -> ! {
exit_qemu(false);
}