Add print* macros; Better panic handler

This commit is contained in:
xqtc161 2024-06-18 13:29:42 +02:00
parent 7ac41887b3
commit 2e9489d700
2 changed files with 59 additions and 2 deletions

View file

@ -8,6 +8,8 @@ use multiboot2;
use spin::*; use spin::*;
use talc::*; use talc::*;
mod print;
static mut START_ARENA: [u8; 10000] = [0; 10000]; static mut START_ARENA: [u8; 10000] = [0; 10000];
#[global_allocator] #[global_allocator]
@ -15,7 +17,11 @@ static ALLOCATOR: Talck<spin::Mutex<()>, ClaimOnOom> =
Talc::new(unsafe { ClaimOnOom::new(Span::from_const_array(core::ptr::addr_of!(START_ARENA))) }) Talc::new(unsafe { ClaimOnOom::new(Span::from_const_array(core::ptr::addr_of!(START_ARENA))) })
.lock(); .lock();
fn main() {} #[no_mangle]
pub extern "C" fn main() -> u32 {
println!("{}!", "It works");
0
}
pub fn kernel_entry(mbi_magic: u32, mbi_ptr: u32) { pub fn kernel_entry(mbi_magic: u32, mbi_ptr: u32) {
if mbi_magic == multiboot2::MAGIC { if mbi_magic == multiboot2::MAGIC {
@ -23,7 +29,12 @@ pub fn kernel_entry(mbi_magic: u32, mbi_ptr: u32) {
}; };
} }
extern "C" {
fn exit(status: i32) -> !;
}
#[panic_handler] #[panic_handler]
fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! {
loop {} println!("{}", info);
unsafe { exit(1) }
} }

46
src/print.rs Normal file
View file

@ -0,0 +1,46 @@
use core::ffi::c_void;
use core::fmt;
extern "C" {
fn write(fildes: i32, buf: *const c_void, nbyte: usize);
}
pub fn write_string(s: &str) {
unsafe {
write(1, s.as_ptr() as *const c_void, s.len());
}
}
pub struct Writer;
impl fmt::Write for Writer {
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
write_string(s);
Ok(())
}
}
#[allow(unused)]
pub fn print(args: fmt::Arguments) {
use fmt::Write;
Writer {}.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => {{
$crate::print::print(format_args!($($arg)*));
}}
}
#[macro_export]
macro_rules! println {
() => {{
print!("\n");
}};
($($arg:tt)*) => {{
print!("{}\n", format_args!($($arg)*));
}}
}