XeOS/src/lib.rs

97 lines
2.0 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)]
2019-10-24 00:30:47 +00:00
#![feature(alloc_error_handler)]
2019-10-23 23:19:02 +00:00
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
2019-10-24 00:30:47 +00:00
extern crate alloc;
pub mod allocator;
2019-10-23 23:19:02 +00:00
pub mod gdt;
pub mod interrupts;
2019-10-24 00:30:47 +00:00
pub mod memory;
pub mod serial;
2019-10-23 23:19:02 +00:00
pub mod vga_buffer;
2019-10-24 13:43:00 +00:00
pub mod wasm;
2019-10-23 23:19:02 +00:00
2019-10-24 00:30:47 +00:00
use linked_list_allocator::LockedHeap;
#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
#[alloc_error_handler]
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
panic!("allocation error: {:?}", layout)
}
use bootloader::BootInfo;
2019-10-24 14:43:38 +00:00
use core::panic::PanicInfo;
2019-10-23 23:19:02 +00:00
2019-10-24 00:30:47 +00:00
pub fn init(boot_info: &'static BootInfo) {
2019-10-24 14:21:48 +00:00
println!("XeOS booting:");
2019-10-23 23:19:02 +00:00
gdt::init();
interrupts::init_idt();
2019-10-23 23:36:24 +00:00
unsafe { interrupts::PICS.lock().initialize() };
x86_64::instructions::interrupts::enable();
2019-10-24 00:30:47 +00:00
allocator::init(&boot_info);
2019-10-23 23:36:24 +00:00
}
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
}
2019-10-24 00:30:47 +00:00
#[cfg(test)]
use bootloader::entry_point;
#[cfg(test)]
entry_point!(test_kernel_main);
2019-10-23 23:19:02 +00:00
/// Entry point for `cargo xtest`
#[cfg(test)]
2019-10-24 00:30:47 +00:00
fn test_kernel_main(boot_info: &'static BootInfo) -> ! {
init(boot_info);
2019-10-23 23:19:02 +00:00
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);
}
}