mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
kernel: implement and use klog!() macro
This replaces the majority of println!() and print!() calls, and works on baremetal and hosted modes. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
eecd9c2367
commit
65c8c6eef5
7 changed files with 77 additions and 42 deletions
|
|
@ -180,6 +180,7 @@ impl Process {
|
|||
f(&mut process)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn current_tid(&self) -> TID {
|
||||
1
|
||||
}
|
||||
|
|
@ -290,7 +291,7 @@ impl Process {
|
|||
|
||||
if let Some(mem) = result.memory() {
|
||||
let s = unsafe { core::slice::from_raw_parts(mem.as_ptr(), mem.len()) };
|
||||
print!(" [adding {} additional bytes from result]", s.len());
|
||||
klog!("adding {} additional bytes from result", s.len());
|
||||
response.extend_from_slice(&s);
|
||||
}
|
||||
|
||||
|
|
@ -299,11 +300,12 @@ impl Process {
|
|||
if result.memory().is_some() {
|
||||
panic!("Result has memory and we're also returning memory!");
|
||||
}
|
||||
print!(" [adding {} additional bytes from memory being returned]", buf.len());
|
||||
klog!("adding {} additional bytes from memory being returned", buf.len());
|
||||
klog!("data: {:?}", buf);
|
||||
response.extend_from_slice(&buf);
|
||||
}
|
||||
|
||||
print!(" [setting thread return value to {} bytes]", response.len());
|
||||
klog!("setting thread return value to {} bytes", response.len());
|
||||
process
|
||||
.conn
|
||||
.as_mut()
|
||||
|
|
|
|||
|
|
@ -507,12 +507,12 @@ pub fn lend_page_inner(
|
|||
} else {
|
||||
MMUFlags::NONE
|
||||
};
|
||||
print!(" [clearing `W` bit from mapping of page {:08x}]", phys);
|
||||
klog!("clearing `W` bit from mapping of page {:08x}", phys);
|
||||
|
||||
// If the current entry is writable, clear that bit and set the "P" flag
|
||||
*entry = (*entry & !(MMUFlags::W.bits())) | (previous_flag | MMUFlags::S).bits();
|
||||
print!(
|
||||
" [additionally, mapping {:08x} into PID {:08x} @ {:08x}]",
|
||||
klog!(
|
||||
"additionally, mapping {:08x} into PID {:08x} @ {:08x}",
|
||||
phys, dest_pid, dest_addr as usize
|
||||
);
|
||||
unsafe { flush_mmu() };
|
||||
|
|
|
|||
|
|
@ -291,7 +291,6 @@ impl Process {
|
|||
*thread = Default::default();
|
||||
}
|
||||
|
||||
let pid = pid.get();
|
||||
let process = unsafe { &mut *PROCESS };
|
||||
let mut thread = &mut process.threads[tid];
|
||||
|
||||
|
|
@ -300,11 +299,14 @@ impl Process {
|
|||
thread.registers[9] = thread_init.arg.map(|x| x.get()).unwrap_or_default();
|
||||
|
||||
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||
if pid != 1 {
|
||||
print!(
|
||||
" [initializing PID {} thread {} with entrypoint {:08x}, stack @ {:08x}, arg {:08x}]",
|
||||
pid, tid, thread.sepc, thread.registers[1], thread.registers[9],
|
||||
);
|
||||
{
|
||||
let pid = pid.get();
|
||||
if pid != 1 {
|
||||
klog!(
|
||||
"initializing PID {} thread {} with entrypoint {:08x}, stack @ {:08x}, arg {:08x}",
|
||||
pid, tid, thread.sepc, thread.registers[1], thread.registers[9],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
process.inner = Default::default();
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
#[cfg(baremetal)]
|
||||
use core::fmt::{Error, Write};
|
||||
#[cfg(baremetal)]
|
||||
use utralib::generated::*;
|
||||
|
||||
#[macro_use]
|
||||
#[cfg(all(not(test), any(feature = "debug-print", feature = "print-panics")))]
|
||||
#[cfg(all(not(test), baremetal, any(feature = "debug-print", feature = "print-panics")))]
|
||||
pub mod debug_print_hardware {
|
||||
use crate::debug::*;
|
||||
pub const SUPERVISOR_UART: Uart = Uart {
|
||||
|
|
@ -18,10 +20,10 @@ pub mod debug_print_hardware {
|
|||
});
|
||||
}
|
||||
}
|
||||
#[cfg(all(not(test), any(feature = "debug-print", feature = "print-panics")))]
|
||||
#[cfg(all(not(test), baremetal, any(feature = "debug-print", feature = "print-panics")))]
|
||||
pub use crate::debug::debug_print_hardware::SUPERVISOR_UART;
|
||||
|
||||
#[cfg(all(not(test), not(any(feature = "debug-print", feature = "print-panics"))))]
|
||||
#[cfg(all(not(test), baremetal, not(any(feature = "debug-print", feature = "print-panics"))))]
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($args:tt)+) => {{
|
||||
|
|
@ -29,6 +31,7 @@ macro_rules! print {
|
|||
}};
|
||||
}
|
||||
|
||||
#[cfg(baremetal)]
|
||||
#[macro_export]
|
||||
macro_rules! println
|
||||
{
|
||||
|
|
@ -43,10 +46,12 @@ macro_rules! println
|
|||
});
|
||||
}
|
||||
|
||||
#[cfg(baremetal)]
|
||||
pub struct Uart {
|
||||
pub base: *mut usize,
|
||||
}
|
||||
|
||||
#[cfg(baremetal)]
|
||||
impl Uart {
|
||||
#[allow(dead_code)]
|
||||
pub fn enable_rx(self) {
|
||||
|
|
@ -79,7 +84,7 @@ impl Uart {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(all(not(test), any(feature = "debug-print", feature = "print-panics")))]
|
||||
#[cfg(all(not(test), baremetal, any(feature = "debug-print", feature = "print-panics")))]
|
||||
pub fn irq(_irq_number: usize, _arg: *mut usize) {
|
||||
println!(
|
||||
"Interrupt {}: Key pressed: {}",
|
||||
|
|
@ -90,6 +95,7 @@ pub fn irq(_irq_number: usize, _arg: *mut usize) {
|
|||
);
|
||||
}
|
||||
|
||||
#[cfg(baremetal)]
|
||||
impl Write for Uart {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||
for c in s.bytes() {
|
||||
|
|
@ -98,3 +104,27 @@ impl Write for Uart {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "debug-print")]
|
||||
#[macro_export]
|
||||
macro_rules! klog
|
||||
{
|
||||
() => ({
|
||||
print!(" [{}:{}]", file!(), line!())
|
||||
});
|
||||
($fmt:expr) => ({
|
||||
print!(concat!(" [{}:{} ", $fmt, "]"), file!(), line!())
|
||||
});
|
||||
($fmt:expr, $($args:tt)+) => ({
|
||||
print!(concat!(" [{}:{} ", $fmt, "]"), file!(), line!(), $($args)+)
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "debug-print"))]
|
||||
#[macro_export]
|
||||
macro_rules! klog
|
||||
{
|
||||
($($args:tt)+) => {{
|
||||
()
|
||||
}};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@
|
|||
#[macro_use]
|
||||
extern crate bitflags;
|
||||
|
||||
#[cfg(baremetal)]
|
||||
#[macro_use]
|
||||
mod debug;
|
||||
|
||||
|
|
@ -26,9 +25,6 @@ mod syscall;
|
|||
use services::SystemServices;
|
||||
use xous_kernel::*;
|
||||
|
||||
#[cfg(baremetal)]
|
||||
use utralib::generated::*;
|
||||
|
||||
#[cfg(baremetal)]
|
||||
use core::panic::PanicInfo;
|
||||
#[cfg(baremetal)]
|
||||
|
|
@ -63,6 +59,7 @@ pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offs
|
|||
// handler then directly map it.
|
||||
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||
{
|
||||
use utralib::generated::*;
|
||||
// Map the serial port so println!() works as expected.
|
||||
mem::MemoryManager::with_mut(|memory_manager| {
|
||||
memory_manager
|
||||
|
|
@ -79,7 +76,8 @@ pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offs
|
|||
println!("KMAIN: Supervisor mode started...");
|
||||
debug::SUPERVISOR_UART.enable_rx();
|
||||
println!("Claiming IRQ {} via syscall...", utra::uart::UART_IRQ);
|
||||
xous_kernel::claim_interrupt(utra::uart::UART_IRQ, debug::irq, 0 as *mut usize).expect("Couldn't claim debug interrupt");
|
||||
xous_kernel::claim_interrupt(utra::uart::UART_IRQ, debug::irq, 0 as *mut usize)
|
||||
.expect("Couldn't claim debug interrupt");
|
||||
print!("}} ");
|
||||
|
||||
// Print the processed kernel arguments
|
||||
|
|
@ -154,8 +152,9 @@ pub extern "C" fn kmain() {
|
|||
match pid {
|
||||
Some(pid) => {
|
||||
// #[cfg(feature = "debug-print")]
|
||||
// print!(" [switching to pid {}]", pid);
|
||||
xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0)).expect("couldn't switch to pid");
|
||||
// klog!("switching to pid {}", pid);
|
||||
xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0))
|
||||
.expect("couldn't switch to pid");
|
||||
}
|
||||
None => {
|
||||
#[cfg(feature = "debug-print")]
|
||||
|
|
|
|||
|
|
@ -337,7 +337,7 @@ impl Server {
|
|||
// println!("KERNEL: index exceeds queue length");
|
||||
return Err(xous_kernel::Error::BadAddress);
|
||||
}
|
||||
print!(" [memory in queue[{}]: {:?}]", idx, self.queue[idx]);
|
||||
klog!("memory in queue[{}]: {:?}", idx, self.queue[idx]);
|
||||
let (pid, tid, server_addr, client_addr, len, forget, is_memory) = match self.queue[idx] {
|
||||
QueuedMessage::WaitingReturnMemory(pid, tid, server_addr, client_addr, len) => {
|
||||
(pid, tid, server_addr, client_addr, len, false, true)
|
||||
|
|
@ -367,7 +367,7 @@ impl Server {
|
|||
}
|
||||
|
||||
// Destructure the PID and context ID from the `pid_tid` field
|
||||
print!(" [taking waiting message and returning to pid: {} tid: {}", pid, tid);
|
||||
klog!("taking waiting message and returning to pid: {} tid: {}", pid, tid);
|
||||
|
||||
if !is_memory {
|
||||
return Ok(WaitingMessage::ScalarMessage(
|
||||
|
|
@ -753,7 +753,7 @@ impl Server {
|
|||
}
|
||||
let mut test_thread_mask = 1;
|
||||
let mut thread_number = 0;
|
||||
print!(" [ready threads: 0b{:08b}]", self.ready_threads);
|
||||
klog!("ready threads: 0b{:08b}", self.ready_threads);
|
||||
loop {
|
||||
// If the context mask matches this context number, remove it
|
||||
// and return the index.
|
||||
|
|
@ -789,9 +789,9 @@ impl Server {
|
|||
|
||||
/// Add the given context to the list of ready and waiting contexts.
|
||||
pub fn park_thread(&mut self, tid: TID) {
|
||||
print!(" [parking thread {}]", tid);
|
||||
klog!("parking thread {}", tid);
|
||||
assert!(self.ready_threads & (1 << tid) == 0);
|
||||
self.ready_threads |= 1 << tid;
|
||||
print!(" [ready threads now: {:08b}]", self.ready_threads);
|
||||
klog!("ready threads now: {:08b}", self.ready_threads);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,8 +126,8 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
|||
.expect("server couldn't be located")
|
||||
.take_available_thread()
|
||||
{
|
||||
print!(
|
||||
" [there are contexts available to handle this message -- marking PID {} as Ready]",
|
||||
klog!(
|
||||
"there are contexts available to handle this message -- marking PID {} as Ready",
|
||||
server_pid
|
||||
);
|
||||
let sender_idx = if message.is_blocking() {
|
||||
|
|
@ -145,9 +145,11 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
|||
sidx,
|
||||
idx: sender_idx,
|
||||
};
|
||||
print!(
|
||||
" [server connection data: sidx: {}, idx: {}, server pid: {}]",
|
||||
sidx, sender_idx, server_pid
|
||||
klog!(
|
||||
"server connection data: sidx: {}, idx: {}, server pid: {}",
|
||||
sidx,
|
||||
sender_idx,
|
||||
server_pid
|
||||
);
|
||||
let envelope = MessageEnvelope {
|
||||
sender: sender.into(),
|
||||
|
|
@ -187,8 +189,8 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
|||
)
|
||||
.map(|_| xous_kernel::Result::Ok)
|
||||
} else {
|
||||
print!(
|
||||
" [setting the return value of the Server to {:?} and returning to Client]",
|
||||
klog!(
|
||||
"setting the return value of the Server to {:?} and returning to Client",
|
||||
envelope
|
||||
);
|
||||
// "Switch to" the server PID when not running on bare metal. This ensures
|
||||
|
|
@ -202,8 +204,8 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
|||
.map(|_| xous_kernel::Result::Ok)
|
||||
}
|
||||
} else {
|
||||
print!(
|
||||
" [no threads available in PID {} to handle this message, so blocking]",
|
||||
klog!(
|
||||
"no threads available in PID {} to handle this message, so blocking",
|
||||
server_pid
|
||||
);
|
||||
// Add this message to the queue. If the queue is full, this
|
||||
|
|
@ -250,7 +252,7 @@ fn return_memory(
|
|||
return Err(xous_kernel::Error::ServerNotFound);
|
||||
}
|
||||
let result = server.take_waiting_message(sender.idx, Some(&buf))?;
|
||||
print!(" [waiting message was: {:?}]", result);
|
||||
klog!("waiting message was: {:?}", result);
|
||||
let (client_pid, client_tid, server_addr, client_addr, len) = match result {
|
||||
WaitingMessage::BorrowedMemory(
|
||||
client_pid,
|
||||
|
|
@ -268,7 +270,7 @@ fn return_memory(
|
|||
let virt = range.addr.get();
|
||||
let size = range.size.get();
|
||||
if cfg!(baremetal) && virt & 0xfff != 0 {
|
||||
print!(" [VIRT NOT DIVISIBLE BY 4: {:08x}]", virt);
|
||||
klog!("VIRT NOT DIVISIBLE BY 4: {:08x}", virt);
|
||||
return Err(xous_kernel::Error::BadAlignment);
|
||||
}
|
||||
for addr in (virt..(virt + size)).step_by(PAGE_SIZE) {
|
||||
|
|
@ -500,7 +502,7 @@ fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult {
|
|||
|
||||
// If there is a pending message, return it immediately.
|
||||
if let Some(msg) = server.take_next_message(sidx) {
|
||||
print!(" [waiting messages found -- returning {:?}]", msg);
|
||||
klog!("waiting messages found -- returning {:?}", msg);
|
||||
return Ok(xous_kernel::Result::Message(msg));
|
||||
}
|
||||
|
||||
|
|
@ -508,8 +510,8 @@ fn receive_message(pid: PID, tid: TID, sid: SID) -> SysCallResult {
|
|||
// process and mark ourselves as awaiting an event. When a message
|
||||
// arrives, our return value will already be set to the
|
||||
// MessageEnvelope of the incoming message.
|
||||
print!(
|
||||
" [did not have any waiting messages -- parking thread {}]",
|
||||
klog!(
|
||||
"did not have any waiting messages -- parking thread {}",
|
||||
tid
|
||||
);
|
||||
server.park_thread(tid);
|
||||
|
|
|
|||
Loading…
Reference in a new issue