diff --git a/src/main.rs b/src/main.rs index 25caceb..3c16553 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use multiboot2; use spin::*; use talc::*; +mod print; + static mut START_ARENA: [u8; 10000] = [0; 10000]; #[global_allocator] @@ -15,7 +17,11 @@ static ALLOCATOR: Talck, ClaimOnOom> = Talc::new(unsafe { ClaimOnOom::new(Span::from_const_array(core::ptr::addr_of!(START_ARENA))) }) .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) { 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] fn panic(info: &PanicInfo) -> ! { - loop {} + println!("{}", info); + unsafe { exit(1) } } diff --git a/src/print.rs b/src/print.rs new file mode 100644 index 0000000..7e57eb2 --- /dev/null +++ b/src/print.rs @@ -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)*)); + }} +} +