diff --git a/kernel/defs.h b/kernel/defs.h index 1a23e54..14cb5d1 100644 --- a/kernel/defs.h +++ b/kernel/defs.h @@ -90,7 +90,6 @@ void userinit(void); void wakeup(void *chan); int either_copyout(int user_dst, uint64 dst, void *src, uint64 len); int either_copyin(void *dst, int user_src, uint64 src, uint64 len); -void procdump(void); // swtch.S void swtch(struct context *, struct context *); diff --git a/kernel/proc.c b/kernel/proc.c index df82ef3..f7a7973 100644 --- a/kernel/proc.c +++ b/kernel/proc.c @@ -157,35 +157,3 @@ either_copyin(void *dst, int user_src, uint64 src, uint64 len) return 0; } } - -// Print a process listing to console. For debugging. -// Runs when user types ^P on console. -// No lock to avoid wedging a stuck machine further. -void procdump(void) -{ - static char *states[] = { - [UNUSED] "unused", - [USED] "used", - [SLEEPING] "sleep ", - [RUNNABLE] "runble", - [RUNNING] "run ", - [ZOMBIE] "zombie" - }; - struct proc *p; - char *state; - - printstr("\n"); - for(p = proc; p < &proc[NPROC]; p++){ - if(p->state == UNUSED) - continue; - if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) - state = states[p->state]; - else - state = "???"; - - printint(p->pid); - printstr(" "); - printstr(state); - printstr("\n"); - } -} diff --git a/kernel/rustkernel/src/console/printf.rs b/kernel/rustkernel/src/console/printf.rs index 7ff972e..eb25650 100644 --- a/kernel/rustkernel/src/console/printf.rs +++ b/kernel/rustkernel/src/console/printf.rs @@ -5,7 +5,7 @@ pub use crate::panic; pub static PRINT_LOCK: Lock = Lock::new(); -/// Print out formatted text to the UART. +/// Print out formatted text to the console. /// Spins to acquire the lock. macro_rules! print { ($($arg:tt)*) => {{ @@ -28,6 +28,34 @@ macro_rules! println { } pub(crate) use println; +/// Print out formatted text to the UART. +/// Does not use any locks. +macro_rules! uprint { + ($($arg:tt)*) => {{ + use $crate::console::uart::Uart; + use core::fmt::Write; + + // Do some casts to get a mutable reference. + // Safe because Uart's core::fmt::Write implementation + // only uses the &mut reference immutably. + let uart: *const Uart = &$crate::console::uart::UART0 as *const Uart; + let uart: &mut Uart = unsafe { &mut *uart.cast_mut() }; + + let _ = core::write!(uart, $($arg)*); + }}; +} +pub(crate) use uprint; + +macro_rules! uprintln { + ($($arg:tt)*) => {{ + use $crate::console::printf::uprint; + uprint!($($arg)*); + uprint!("\n"); + }}; +} +pub(crate) use uprintln; + + #[no_mangle] pub extern "C" fn printint(n: i32) { print!("{}", n); diff --git a/kernel/rustkernel/src/console/uart.rs b/kernel/rustkernel/src/console/uart.rs index 6f79ee7..6880a95 100644 --- a/kernel/rustkernel/src/console/uart.rs +++ b/kernel/rustkernel/src/console/uart.rs @@ -187,3 +187,9 @@ impl Uart { } } } +impl core::fmt::Write for Uart { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + self.write_slice(s.as_bytes()); + core::fmt::Result::Ok(()) + } +} diff --git a/kernel/rustkernel/src/lib.rs b/kernel/rustkernel/src/lib.rs index 15920ce..fca33ad 100644 --- a/kernel/rustkernel/src/lib.rs +++ b/kernel/rustkernel/src/lib.rs @@ -24,7 +24,7 @@ mod trap; use crate::{proc::cpu::Cpu, sync::mutex::Mutex}; use core::ffi::{c_char, CStr}; -pub(crate) use crate::console::printf::{print, println}; +pub(crate) use crate::console::printf::{print, println, uprint, uprintln}; pub static mut STARTED: bool = false; pub static PANICKED: Mutex = Mutex::new(false); diff --git a/kernel/rustkernel/src/proc/process.rs b/kernel/rustkernel/src/proc/process.rs index 492885c..da28e1e 100644 --- a/kernel/rustkernel/src/proc/process.rs +++ b/kernel/rustkernel/src/proc/process.rs @@ -23,8 +23,8 @@ use crate::{ copyout, mappages, uvmalloc, uvmcopy, uvmcreate, uvmdealloc, uvmfree, uvmunmap, }, }, - string::safestrcpy, sync::spinlock::Spinlock, + uprintln, }; use core::{ ffi::{c_char, c_void}, @@ -51,7 +51,7 @@ extern "C" { // pub fn fork() -> i32; // pub fn exit(status: i32) -> !; pub fn wait(addr: u64) -> i32; - pub fn procdump(); + // pub fn procdump(); pub fn proc_mapstacks(kpgtbl: Pagetable); } @@ -504,3 +504,15 @@ pub unsafe extern "C" fn proc_pagetable(p: *mut Process) -> Pagetable { pub unsafe extern "C" fn proc_freepagetable(pagetable: Pagetable, size: u64) { Process::free_pagetable(pagetable, size as usize) } + +/// Print a process listing to console for debugging. +/// Runs when a user types ^P on console. +/// No lock to avoid wedging a stuck machine further. +pub unsafe fn procdump() { + uprintln!("\nprocdump:"); + for p in &proc { + if p.state != ProcessState::Unused { + uprintln!(" {}: {:?}", p.pid, p.state); + } + } +}