2019-10-21 13:10:53 +02:00
|
|
|
#![no_std]
|
|
|
|
#![cfg_attr(test, no_main)]
|
|
|
|
#![feature(custom_test_frameworks)]
|
|
|
|
#![test_runner(crate::test_runner)]
|
|
|
|
#![reexport_test_harness_main = "test_main"]
|
2019-11-04 13:54:43 +01:00
|
|
|
#![feature(abi_x86_interrupt)]
|
|
|
|
#![feature(asm)]
|
2019-10-21 13:10:53 +02:00
|
|
|
|
|
|
|
use core::panic::PanicInfo;
|
|
|
|
|
2019-11-13 14:12:07 +01:00
|
|
|
use polling_serial::serial_println;
|
|
|
|
use vga_buffer::println;
|
2019-11-04 13:54:43 +01:00
|
|
|
use x86_64::instructions::bochs_breakpoint;
|
|
|
|
|
|
|
|
pub mod gdt;
|
|
|
|
pub mod interrupts;
|
2019-10-21 13:10:53 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[repr(u32)]
|
|
|
|
pub enum QemuExitCode {
|
|
|
|
Success = 0x10,
|
|
|
|
Failed = 0x11,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom panic handler, required for freestanding program
|
|
|
|
|
|
|
|
pub fn test_panic_handler(info: &PanicInfo) -> ! {
|
|
|
|
serial_println!("[failed]\n");
|
|
|
|
serial_println!("Error: {}\n", info);
|
|
|
|
exit_qemu(QemuExitCode::Failed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes isa-debug-device at 0xf4, of size 4
|
|
|
|
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);
|
|
|
|
}
|
2019-11-13 14:12:07 +01:00
|
|
|
loop {
|
|
|
|
bochs_breakpoint();
|
|
|
|
}
|
2019-10-21 13:10:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn test_runner(tests: &[&dyn Fn()]) {
|
|
|
|
println!("Running {} tests", tests.len());
|
|
|
|
serial_println!("Running {} tests", tests.len());
|
|
|
|
for test in tests {
|
|
|
|
test();
|
|
|
|
}
|
|
|
|
|
|
|
|
exit_qemu(QemuExitCode::Success);
|
|
|
|
}
|
|
|
|
|
2019-11-04 13:54:43 +01:00
|
|
|
pub fn init() {
|
|
|
|
gdt::init();
|
|
|
|
interrupts::init_idt();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hlt_loop() -> ! {
|
|
|
|
loop {
|
|
|
|
bochs_breakpoint();
|
|
|
|
x86_64::instructions::hlt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 13:10:53 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn _start() -> ! {
|
|
|
|
test_main();
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
#[panic_handler]
|
|
|
|
fn panic(info: &PanicInfo) -> ! {
|
|
|
|
test_panic_handler(info)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_case]
|
|
|
|
fn trivial_assertion() {
|
|
|
|
print!("trivial assertion... ");
|
|
|
|
serial_print!("trivial assertion... ");
|
|
|
|
assert_eq!(1, 1);
|
|
|
|
println!("[ok]");
|
|
|
|
serial_println!("[ok]");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test_case]
|
|
|
|
fn printf_test() {
|
|
|
|
serial_print!("Testing VGA print/println... ");
|
|
|
|
println!("Are frogs blue?");
|
2019-11-13 14:12:07 +01:00
|
|
|
println!("Yes");
|
2019-10-21 13:10:53 +02:00
|
|
|
serial_println!("[ok]");
|
|
|
|
}
|