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.
This commit is contained in:
2026-10-19 01:00:33 +02:00
parent 052d7c4a64
commit 818eaecadd
3 changed files with 27 additions and 58 deletions

View File

@@ -5,5 +5,5 @@ extern crate libkos;
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
extern "C" fn main() { extern "C" fn main() {
// The runtime handles _start, exit, and panic. libkos::println!("Hello from k-os!");
} }

View File

@@ -1,5 +1,7 @@
#![no_std] #![no_std]
extern crate alloc;
use types::address::{EntryPoint, StackPointer, VirtualAddress}; use types::address::{EntryPoint, StackPointer, VirtualAddress};
use types::bounded::{CapSlot, PageCount}; use types::bounded::{CapSlot, PageCount};
use types::permission::{Permission, PermissionSet}; 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 DISPLAY_EP: CapSlot = CapSlot::const_new(3);
const INPUT_EP: CapSlot = CapSlot::const_new(5); 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 { fn request_file(shared_buf: *mut u8, filename: &[u8]) -> usize {
unsafe { unsafe {
for (i, &b) in filename.iter().enumerate() { for (i, &b) in filename.iter().enumerate() {
@@ -122,35 +98,31 @@ fn load_binary(shared_buf: *mut u8, filename: &[u8]) -> Option<LoadedProcess> {
struct ServerInfo { struct ServerInfo {
filename: &'static [u8], filename: &'static [u8],
name: &'static [u8], name: &'static str,
} }
const SERVERS: &[ServerInfo] = &[ const SERVERS: &[ServerInfo] = &[
ServerInfo { filename: b"log.elf", name: b"log" }, ServerInfo { filename: b"log.elf", name: "log" },
ServerInfo { filename: b"permis.elf", name: b"permis" }, ServerInfo { filename: b"permis.elf", name: "permis" },
ServerInfo { filename: b"session.elf", name: b"session" }, ServerInfo { filename: b"session.elf", name: "session" },
]; ];
pub fn run(shared_buf_va: usize) -> ! { pub fn run(shared_buf_va: usize) -> ! {
let shared_buf = shared_buf_va as *mut u8; let shared_buf = shared_buf_va as *mut u8;
debug_print(b"init: started (pid="); libkos::println!("init: started (pid={:#x})", syscall::get_process_id().raw());
debug_hex(syscall::get_process_id().raw() as usize);
debug_print(b")\n");
let ipc_perms = PermissionSet::empty() let ipc_perms = PermissionSet::empty()
.with(Permission::Read) .with(Permission::Read)
.with(Permission::Execute); .with(Permission::Execute);
for server in SERVERS { for server in SERVERS {
debug_print(b"init: loading "); libkos::print!("init: loading {}... ", server.name);
debug_print(server.name);
debug_print(b"... ");
let loaded = match load_binary(shared_buf, server.filename) { let loaded = match load_binary(shared_buf, server.filename) {
Some(l) => l, Some(l) => l,
None => { None => {
debug_print(b"SKIP\n"); libkos::println!("SKIP");
continue; 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); syscall::spawn_thread_in(loaded.process_cap, loaded.entry_point, USER_STACK_TOP);
} }
debug_print(b"ok (entry="); libkos::println!("ok (entry={:#x})", loaded.entry_point.raw());
debug_hex(loaded.entry_point.raw());
debug_print(b")\n");
} }
debug_print(b"init: loading snake... "); libkos::print!("init: loading snake... ");
if let Some(snake) = load_binary(shared_buf, b"snake.elf") { if let Some(snake) = load_binary(shared_buf, b"snake.elf") {
syscall::delegate(INPUT_EP, snake.process_cap, ipc_perms); syscall::delegate(INPUT_EP, snake.process_cap, ipc_perms);
syscall::delegate(DISPLAY_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); syscall::spawn_thread_in(snake.process_cap, snake.entry_point, USER_STACK_TOP);
debug_print(b"ok\n"); libkos::println!("ok");
} else { } else {
debug_print(b"SKIP\n"); libkos::println!("SKIP");
} }
debug_print(b"init: all loaded, supervising\n"); libkos::println!("init: all loaded, supervising");
loop { loop {
syscall::yield_now(); syscall::yield_now();

View File

@@ -1,5 +1,7 @@
#![no_std] #![no_std]
extern crate alloc;
pub mod adapters; pub mod adapters;
pub mod filesystem; pub mod filesystem;
pub mod fs; 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) } 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() -> ! { pub fn run() -> ! {
debug_print(b"storage: started\n"); libkos::println!("storage: started");
let device = driver_block_virtio::VirtioBlockDevice::new(); let device = driver_block_virtio::VirtioBlockDevice::new();
let fat16 = fs::fat16::Fat16::mount(device); let fat16 = fs::fat16::Fat16::mount(device);
if fat16.is_none() { if fat16.is_none() {
debug_print(b"storage: FAT16 mount failed\n"); libkos::println!("storage: FAT16 mount failed");
loop { syscall::yield_now(); } loop { syscall::yield_now(); }
} }
let mut fat16 = fat16.unwrap(); 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(); let ep = types::bounded::CapSlot::new(0).unwrap();
@@ -53,13 +49,16 @@ pub fn run() -> ! {
let name_len = filename_len.min(32); let name_len = filename_len.min(32);
name_buf[..name_len].copy_from_slice(&buf[..name_len]); name_buf[..name_len].copy_from_slice(&buf[..name_len]);
debug_print(b"storage: read "); let name = core::str::from_utf8(&name_buf[..name_len]).unwrap_or("?");
debug_print(&name_buf[..name_len]); libkos::print!("storage: read {}...", name);
debug_print(b"...");
let file_size = read_entire_file(&mut fat16, &name_buf[..name_len], buf); 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); syscall::send(ep, file_size, 0, 0, 0);
} }