From 818eaecaddceeb9db271bb3f0f48af4b3a3bba2e Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Mon, 19 Oct 2026 01:00:33 +0200 Subject: [PATCH] refactor: replace manual debug_putchar with libkos::println! Init, storage server, and hello app now use println!/print! macros instead of manual byte-by-byte UART output. --- crates/apps/hello/src/main.rs | 2 +- crates/init/src/lib.rs | 58 ++++++------------------ crates/servers/storage-server/src/lib.rs | 25 +++++----- 3 files changed, 27 insertions(+), 58 deletions(-) diff --git a/crates/apps/hello/src/main.rs b/crates/apps/hello/src/main.rs index 584a5b2..e7245e2 100644 --- a/crates/apps/hello/src/main.rs +++ b/crates/apps/hello/src/main.rs @@ -5,5 +5,5 @@ extern crate libkos; #[unsafe(no_mangle)] extern "C" fn main() { - // The runtime handles _start, exit, and panic. + libkos::println!("Hello from k-os!"); } diff --git a/crates/init/src/lib.rs b/crates/init/src/lib.rs index e5f4719..2c0b0fb 100644 --- a/crates/init/src/lib.rs +++ b/crates/init/src/lib.rs @@ -1,5 +1,7 @@ #![no_std] +extern crate alloc; + use types::address::{EntryPoint, StackPointer, VirtualAddress}; use types::bounded::{CapSlot, PageCount}; use types::permission::{Permission, PermissionSet}; @@ -16,32 +18,6 @@ const STORAGE_EP: CapSlot = CapSlot::const_new(2); const DISPLAY_EP: CapSlot = CapSlot::const_new(3); const INPUT_EP: CapSlot = CapSlot::const_new(5); -fn debug_print(s: &[u8]) { - for &b in s { - syscall::debug_putchar(b); - } -} - -fn debug_hex(mut val: usize) { - debug_print(b"0x"); - if val == 0 { - syscall::debug_putchar(b'0'); - return; - } - let mut buf = [0u8; 16]; - let mut i = 0; - while val > 0 { - let d = (val & 0xF) as u8; - buf[i] = if d < 10 { b'0' + d } else { b'a' + d - 10 }; - val >>= 4; - i += 1; - } - while i > 0 { - i -= 1; - syscall::debug_putchar(buf[i]); - } -} - fn request_file(shared_buf: *mut u8, filename: &[u8]) -> usize { unsafe { for (i, &b) in filename.iter().enumerate() { @@ -122,35 +98,31 @@ fn load_binary(shared_buf: *mut u8, filename: &[u8]) -> Option { struct ServerInfo { filename: &'static [u8], - name: &'static [u8], + name: &'static str, } const SERVERS: &[ServerInfo] = &[ - ServerInfo { filename: b"log.elf", name: b"log" }, - ServerInfo { filename: b"permis.elf", name: b"permis" }, - ServerInfo { filename: b"session.elf", name: b"session" }, + ServerInfo { filename: b"log.elf", name: "log" }, + ServerInfo { filename: b"permis.elf", name: "permis" }, + ServerInfo { filename: b"session.elf", name: "session" }, ]; pub fn run(shared_buf_va: usize) -> ! { let shared_buf = shared_buf_va as *mut u8; - debug_print(b"init: started (pid="); - debug_hex(syscall::get_process_id().raw() as usize); - debug_print(b")\n"); + libkos::println!("init: started (pid={:#x})", syscall::get_process_id().raw()); let ipc_perms = PermissionSet::empty() .with(Permission::Read) .with(Permission::Execute); for server in SERVERS { - debug_print(b"init: loading "); - debug_print(server.name); - debug_print(b"... "); + libkos::print!("init: loading {}... ", server.name); let loaded = match load_binary(shared_buf, server.filename) { Some(l) => l, None => { - debug_print(b"SKIP\n"); + libkos::println!("SKIP"); continue; } }; @@ -160,22 +132,20 @@ pub fn run(shared_buf_va: usize) -> ! { syscall::spawn_thread_in(loaded.process_cap, loaded.entry_point, USER_STACK_TOP); } - debug_print(b"ok (entry="); - debug_hex(loaded.entry_point.raw()); - debug_print(b")\n"); + libkos::println!("ok (entry={:#x})", loaded.entry_point.raw()); } - debug_print(b"init: loading snake... "); + libkos::print!("init: loading snake... "); if let Some(snake) = load_binary(shared_buf, b"snake.elf") { syscall::delegate(INPUT_EP, snake.process_cap, ipc_perms); syscall::delegate(DISPLAY_EP, snake.process_cap, ipc_perms); syscall::spawn_thread_in(snake.process_cap, snake.entry_point, USER_STACK_TOP); - debug_print(b"ok\n"); + libkos::println!("ok"); } else { - debug_print(b"SKIP\n"); + libkos::println!("SKIP"); } - debug_print(b"init: all loaded, supervising\n"); + libkos::println!("init: all loaded, supervising"); loop { syscall::yield_now(); diff --git a/crates/servers/storage-server/src/lib.rs b/crates/servers/storage-server/src/lib.rs index 2289c11..f4dcbf6 100644 --- a/crates/servers/storage-server/src/lib.rs +++ b/crates/servers/storage-server/src/lib.rs @@ -1,5 +1,7 @@ #![no_std] +extern crate alloc; + pub mod adapters; pub mod filesystem; pub mod fs; @@ -20,23 +22,17 @@ fn shared_buf() -> &'static mut [u8] { unsafe { core::slice::from_raw_parts_mut(SHARED_BUF_VA as *mut u8, SHARED_BUF_SIZE) } } -fn debug_print(s: &[u8]) { - for &b in s { - syscall::debug_putchar(b); - } -} - pub fn run() -> ! { - debug_print(b"storage: started\n"); + libkos::println!("storage: started"); let device = driver_block_virtio::VirtioBlockDevice::new(); let fat16 = fs::fat16::Fat16::mount(device); if fat16.is_none() { - debug_print(b"storage: FAT16 mount failed\n"); + libkos::println!("storage: FAT16 mount failed"); loop { syscall::yield_now(); } } let mut fat16 = fat16.unwrap(); - debug_print(b"storage: FAT16 mounted\n"); + libkos::println!("storage: FAT16 mounted"); let ep = types::bounded::CapSlot::new(0).unwrap(); @@ -53,13 +49,16 @@ pub fn run() -> ! { let name_len = filename_len.min(32); name_buf[..name_len].copy_from_slice(&buf[..name_len]); - debug_print(b"storage: read "); - debug_print(&name_buf[..name_len]); - debug_print(b"..."); + let name = core::str::from_utf8(&name_buf[..name_len]).unwrap_or("?"); + libkos::print!("storage: read {}...", name); let file_size = read_entire_file(&mut fat16, &name_buf[..name_len], buf); - debug_print(if file_size > 0 { b"ok\n" } else { b"NOT FOUND\n" }); + if file_size > 0 { + libkos::println!("ok"); + } else { + libkos::println!("NOT FOUND"); + } syscall::send(ep, file_size, 0, 0, 0); }