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)]
|
2020-05-29 16:26:53 +02:00
|
|
|
#![feature(llvm_asm)]
|
2019-11-14 14:26:37 +01:00
|
|
|
#![feature(alloc_error_handler)]
|
|
|
|
extern crate alloc;
|
2019-10-21 13:10:53 +02:00
|
|
|
|
2019-11-14 14:26:37 +01:00
|
|
|
pub mod allocator;
|
|
|
|
pub mod gdt;
|
|
|
|
pub mod interrupts;
|
|
|
|
pub mod memory;
|
2019-11-13 14:26:39 +01:00
|
|
|
|
2019-11-14 14:26:37 +01:00
|
|
|
use core::panic::PanicInfo;
|
|
|
|
use linked_list_allocator::LockedHeap;
|
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;
|
|
|
|
|
2019-10-21 13:10:53 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[repr(u32)]
|
|
|
|
pub enum QemuExitCode {
|
|
|
|
Success = 0x10,
|
|
|
|
Failed = 0x11,
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:26:37 +01:00
|
|
|
#[global_allocator]
|
|
|
|
static ALLOCATOR: LockedHeap = LockedHeap::empty();
|
2019-10-21 13:10:53 +02:00
|
|
|
|
2019-11-14 14:26:37 +01:00
|
|
|
#[alloc_error_handler]
|
|
|
|
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
|
|
|
|
panic!("allocation error: {:?}", layout)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom panic handler, required for freestanding program
|
2019-10-21 13:10:53 +02:00
|
|
|
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-11-14 14:26:37 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
use vga_buffer::print;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
use polling_serial::serial_print;
|
|
|
|
|
2019-11-13 14:12:28 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
use bootloader::{entry_point, BootInfo};
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
entry_point!(test_kernel_main);
|
|
|
|
|
|
|
|
/// Entry point for `cargo xtest`
|
2019-10-21 13:10:53 +02:00
|
|
|
#[cfg(test)]
|
2019-11-13 14:12:28 +01:00
|
|
|
fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
|
|
|
|
init();
|
2019-10-21 13:10:53 +02:00
|
|
|
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]");
|
|
|
|
}
|