Nothing to see here, move along
at main 91 lines 2.5 kB view raw
1#![no_std] 2 3pub mod fs; 4pub mod io; 5pub mod net; 6pub mod path; 7pub mod syscall; 8 9unsafe extern "C" { 10 fn lancer_main() -> !; 11} 12 13const PROC_NAME_CAP: usize = 32; 14static mut PROC_NAME_BUF: [u8; PROC_NAME_CAP] = [0u8; PROC_NAME_CAP]; 15static mut PROC_NAME_LEN: usize = 0; 16 17pub fn proc_name() -> &'static str { 18 let len = unsafe { PROC_NAME_LEN }; 19 match len { 20 0 => "?", 21 _ => unsafe { core::str::from_utf8_unchecked(&PROC_NAME_BUF[..len]) }, 22 } 23} 24 25#[unsafe(no_mangle)] 26#[allow(clippy::deref_addrof)] 27pub extern "C" fn _start(bootstrap: u64) -> ! { 28 match bootstrap { 29 0 | u64::MAX => {} 30 slot => { 31 crate::io::set_stdout_slot(slot); 32 crate::io::set_debug_print_enabled(false); 33 } 34 } 35 let name_len = syscall::get_proc_name(unsafe { &mut *(&raw mut PROC_NAME_BUF) }); 36 if name_len > 0 { 37 unsafe { PROC_NAME_LEN = (name_len as usize).min(PROC_NAME_CAP) }; 38 } 39 unsafe { lancer_main() } 40} 41 42fn fmt_u64_decimal(n: u64, buf: &mut [u8; 20]) -> usize { 43 match n { 44 0 => { 45 buf[19] = b'0'; 46 19 47 } 48 _ => { 49 fn go(v: u64, buf: &mut [u8; 20], pos: usize) -> usize { 50 match v { 51 0 => pos, 52 _ => { 53 let next = pos - 1; 54 buf[next] = b'0' + (v % 10) as u8; 55 go(v / 10, buf, next) 56 } 57 } 58 } 59 go(n, buf, buf.len()) 60 } 61 } 62} 63 64#[panic_handler] 65fn panic(info: &core::panic::PanicInfo) -> ! { 66 let pid = syscall::getpid(); 67 let mut pid_buf = [0u8; 20]; 68 let pid_start = fmt_u64_decimal(pid, &mut pid_buf); 69 70 syscall::debug_print("panic in userspace pid "); 71 if let Ok(s) = core::str::from_utf8(&pid_buf[pid_start..]) { 72 syscall::debug_print(s); 73 } 74 syscall::debug_print(": "); 75 match info.location() { 76 Some(loc) => { 77 syscall::debug_print(loc.file()); 78 syscall::debug_print(":"); 79 let mut line_buf = [0u8; 20]; 80 let line_start = fmt_u64_decimal(loc.line() as u64, &mut line_buf); 81 if let Ok(s) = core::str::from_utf8(&line_buf[line_start..]) { 82 syscall::debug_print(s); 83 } 84 } 85 None => { 86 syscall::debug_print("unknown location"); 87 } 88 } 89 syscall::debug_print("\n"); 90 syscall::exit() 91}