XeOS/src/lib.rs

73 lines
1.4 KiB
Rust
Raw Normal View History

2019-10-23 23:19:02 +00:00
#![no_std]
#![cfg_attr(test, no_main)]
#![feature(abi_x86_interrupt)]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
pub mod gdt;
pub mod serial;
pub mod interrupts;
pub mod vga_buffer;
use core::panic::PanicInfo;
pub fn init() {
gdt::init();
interrupts::init_idt();
2019-10-23 23:36:24 +00:00
unsafe { interrupts::PICS.lock().initialize() };
x86_64::instructions::interrupts::enable();
}
pub fn hlt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
2019-10-23 23:19:02 +00:00
}
pub fn test_runner(tests: &[&dyn Fn()]) {
serial_println!("Running {} tests", 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);
2019-10-23 23:36:24 +00:00
hlt_loop();
2019-10-23 23:19:02 +00:00
}
/// Entry point for `cargo xtest`
#[cfg(test)]
#[no_mangle]
pub extern "C" fn _start() -> ! {
init();
test_main();
2019-10-23 23:36:24 +00:00
hlt_loop();
2019-10-23 23:19:02 +00:00
}
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
test_panic_handler(info)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum QemuExitCode {
Success = 0x10,
Failed = 0x11,
}
pub fn exit_qemu(exit_code: QemuExitCode) {
use x86_64::instructions::port::Port;
unsafe {
let mut port = Port::new(0xf4);
port.write(exit_code as u32);
}
}