60 lines
1.2 KiB
Rust
60 lines
1.2 KiB
Rust
#![no_std]
|
|
#![cfg_attr(test, no_main)]
|
|
#![feature(custom_test_frameworks)]
|
|
#![test_runner(crate::test_runner)]
|
|
#![reexport_test_harness_main = "test_main"]
|
|
|
|
#[macro_use]
|
|
extern crate lazy_static;
|
|
|
|
pub mod serial;
|
|
pub mod vga_buffer;
|
|
|
|
use core::panic::PanicInfo;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
#[repr(u32)]
|
|
// u32 because iosize is set to 4 bytes
|
|
pub enum QemuExitCode {
|
|
Success = 0x10,
|
|
Failed = 0x11,
|
|
}
|
|
|
|
pub fn exit_qemu(exit_code: QemuExitCode) {
|
|
use x86_64::instructions::port::Port;
|
|
|
|
unsafe {
|
|
// iobase address
|
|
let mut port = Port::new(0xf4);
|
|
port.write(exit_code as u32);
|
|
}
|
|
}
|
|
|
|
pub fn test_runner(tests: &[&dyn Fn()]) {
|
|
serial_println!("Running {} test", tests.len());
|
|
for test in tests {
|
|
test();
|
|
}
|
|
exit_qemu(QemuExitCode::Success);
|
|
}
|
|
|
|
pub fn test_panic_handler(info: &PanicInfo) -> ! {
|
|
serial_println!("[failed]\n");
|
|
serial_println!("Error: {}\n", info);
|
|
exit_qemu(QemuExitCode::Failed);
|
|
loop {}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[no_mangle]
|
|
pub extern "C" fn _start() -> ! {
|
|
test_main();
|
|
loop {}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[panic_handler]
|
|
fn panic(info: &PanicInfo) -> ! {
|
|
test_panic_handler(info)
|
|
}
|