rust-operating-system/src/main.rs

89 lines
1.6 KiB
Rust
Raw Normal View History

2022-02-19 00:54:44 -08:00
// #![feature(abi_x86_interrupt, asm)]
2019-07-09 09:42:41 -07:00
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
2019-07-11 01:46:28 -07:00
#[macro_use]
2019-07-10 02:25:12 -07:00
mod vga_buffer;
#[macro_use]
mod serial;
2019-07-10 02:25:12 -07:00
2019-07-09 09:42:41 -07:00
use core::panic::PanicInfo;
2022-02-20 02:14:49 -08:00
#[cfg(not(test))]
2019-07-09 09:42:41 -07:00
#[panic_handler]
2022-02-19 01:01:32 -08:00
fn panic(info: &PanicInfo) -> ! {
println!("{info}");
loop {}
2019-07-09 09:42:41 -07:00
}
2022-02-20 02:14:49 -08:00
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}
2019-07-09 09:42:41 -07:00
#[no_mangle]
pub extern "C" fn _start() -> ! {
#[cfg(test)]
test_main();
2022-02-19 00:54:44 -08:00
println!("Gamarjoba, munde!");
loop {}
2019-07-09 02:02:08 -07:00
}
#[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); // isa-debug-exit port, see Cargo.toml
port.write(exit_code as u32);
}
}
2022-02-20 02:14:49 -08:00
pub trait TestFunction {
fn run(&self) -> ();
}
impl<T> TestFunction for T
where
T: Fn(),
{
fn run(&self) {
serial_print!("{}...\t", core::any::type_name::<T>());
self();
serial_println!("[ok]");
}
}
#[cfg(test)]
2022-02-20 02:14:49 -08:00
fn test_runner(tests: &[&dyn TestFunction]) {
serial_println!("Running {} test(s)", tests.len());
for test in tests {
2022-02-20 02:14:49 -08:00
test.run();
}
exit_qemu(QemuExitCode::Success);
}
#[test_case]
fn basic_test() {
assert_eq!(5, 5);
}
2022-02-20 02:14:49 -08:00
#[test_case]
fn basic_test2() {
assert_eq!(4, 4);
}