-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
167 lines (141 loc) · 3.14 KB
/
lib.rs
File metadata and controls
167 lines (141 loc) · 3.14 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![no_std]
#![cfg_attr(test, no_main)]
#![feature(abi_x86_interrupt)]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
extern crate alloc;
use core::panic::PanicInfo;
pub mod allocator;
pub mod dat;
pub mod dev;
/// Initialize the kernel.
pub fn init() {
dev::framebuffer::fb0::init();
}
/// Halt the CPU.
pub fn hlt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
}
/// Prints INFO to serial and framebuffer terminals.
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {
serial_info!($($arg)*);
fb0_info!($($arg)*);
};
}
/// Prints INFO to serial and framebuffer terminals, followed by a newline.
#[macro_export]
macro_rules! info_ln {
($($arg:tt)*) => {
serial_info_ln!($($arg)*);
fb0_info_ln!($($arg)*);
};
}
/// Prints DEBUG to serial and framebuffer terminals.
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {
serial_debug!($($arg)*);
fb0_debug!($($arg)*);
};
}
/// Prints DEBUG to serial and framebuffer terminals, followed by a newline.
#[macro_export]
macro_rules! debug_ln {
($($arg:tt)*) => {
serial_debug_ln!($($arg)*);
fb0_debug_ln!($($arg)*);
};
}
/// Prints WARN to serial and framebuffer terminals.
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {
serial_warn!($($arg)*);
fb0_warn!($($arg)*);
};
}
/// Prints WARN to serial and framebuffer terminals, followed by a newline.
#[macro_export]
macro_rules! warn_ln {
($($arg:tt)*) => {
serial_warn_ln!($($arg)*);
fb0_warn_ln!($($arg)*);
};
}
/// Prints DANGER to serial and framebuffer terminals.
#[macro_export]
macro_rules! danger {
($($arg:tt)*) => {
serial_danger!($($arg)*);
fb0_danger!($($arg)*);
};
}
/// Prints DANGER to serial and framebuffer terminals, followed by a newline.
#[macro_export]
macro_rules! danger_ln {
($($arg:tt)*) => {
serial_danger_ln!($($arg)*);
fb0_danger_ln!($($arg)*);
};
}
pub trait Testable {
fn run(&self) -> ();
}
impl<T> Testable for T
where
T: Fn(),
{
fn run(&self) {
serial_print!("TEST: {}...\t", core::any::type_name::<T>());
self();
serial_print!("[ok]\n");
}
}
#[no_mangle]
pub fn test_runner(tests: &[&dyn Testable]) {
serial_print!("INFO: Running {} tests...\n", tests.len());
for test in tests {
test.run();
}
exit_qemu(QemuExitCode::Success);
}
pub fn test_panic_handler(info: &PanicInfo) -> ! {
serial_danger_ln!("[failed]\n");
serial_danger_ln!("Error: {}\n", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}
#[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);
}
}
#[cfg(test)]
#[no_mangle]
pub extern "C" fn _start() -> ! {
serial_info_ln!("!!! RUNNING LIBRARY TESTS !!!");
test_main();
loop {}
}
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
test_panic_handler(info)
}
#[test_case]
fn trivial_lib_assertion() {
assert_eq!(1, 1);
}