summaryrefslogtreecommitdiff
path: root/kernel/src/lib.rs
blob: 8ec68e52b083849793263bf1c5316b2bf4872fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#![no_main]
#![feature(compiler_builtins_lib)]
#![feature(custom_test_frameworks)]
#![feature(abi_x86_interrupt)]
#![feature(panic_info_message)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
#![no_std]

mod interrupts;
mod io;

use core::fmt::Write;
use io::vga_text::OStream;
use io::{qemu, serial, vga_text};

#[cfg(test)]
pub fn test_runner(tests: &[&dyn Fn(&mut OStream)]) {
    let mut stdout = OStream::new();
    stdout.clear();
    write!(&mut stdout, "running {} tests\n", tests.len()).unwrap();
    for test in tests {
        test(&mut stdout)
    }
}

#[no_mangle]
pub extern "C" fn _start() -> ! {
    interrupts::table::init();
    if cfg!(test) {
        #[cfg(test)]
        test_main();
        qemu::exit_qemu(qemu::QemuExitCode::Success);
    }

    let mut stdout = OStream::new();
    stdout.clear();
    panic!("panic! at the disco");

    core::iter::successors(Some(0), |n| Some(n + 1))
        .for_each(|n| write!(&mut stdout, "hello world {}!", n).unwrap());

    x86_64::instructions::interrupts::int3();
    loop {}
}

#[panic_handler]
#[no_mangle]
pub extern "C" fn panic_handler(info: &core::panic::PanicInfo) -> ! {
    let mut stderr = OStream::new();
    stderr.set_state(vga_text::CharState::from_colors(
        vga_text::Color::LightRed,
        vga_text::Color::Red,
    ));
    stderr.clear();
    stderr.print(b"uff-os");
    stderr.set_row(10);
    stderr.set_state(vga_text::CharState::from_colors(
        vga_text::Color::White,
        vga_text::Color::Red,
    ));
    stderr.print_centered(b"<kernel panic>");
    stderr.set_row(14);
    stderr.set_state(vga_text::CharState::from_colors(
        vga_text::Color::Cyan,
        vga_text::Color::Red,
    ));
    stderr.set_centered(true);
    write!(
        &mut stderr,
        "{:?}",
        info.message()
            .unwrap_or(&format_args!("no panic information obtainable"))
    );
    //loop{}
    stderr.set_centered(false);
    if cfg!(test) {
        write!(serial::SerialStream::new(), "Testing failed\n").unwrap();
        qemu::exit_qemu(qemu::QemuExitCode::Failed);
    }
    loop {}
}