mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +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)
|
f(&mut process)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn current_tid(&self) -> TID {
|
pub fn current_tid(&self) -> TID {
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
@ -290,7 +291,7 @@ impl Process {
|
||||||
|
|
||||||
if let Some(mem) = result.memory() {
|
if let Some(mem) = result.memory() {
|
||||||
let s = unsafe { core::slice::from_raw_parts(mem.as_ptr(), mem.len()) };
|
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);
|
response.extend_from_slice(&s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -299,11 +300,12 @@ impl Process {
|
||||||
if result.memory().is_some() {
|
if result.memory().is_some() {
|
||||||
panic!("Result has memory and we're also returning memory!");
|
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);
|
response.extend_from_slice(&buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
print!(" [setting thread return value to {} bytes]", response.len());
|
klog!("setting thread return value to {} bytes", response.len());
|
||||||
process
|
process
|
||||||
.conn
|
.conn
|
||||||
.as_mut()
|
.as_mut()
|
||||||
|
|
|
||||||
|
|
@ -507,12 +507,12 @@ pub fn lend_page_inner(
|
||||||
} else {
|
} else {
|
||||||
MMUFlags::NONE
|
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
|
// If the current entry is writable, clear that bit and set the "P" flag
|
||||||
*entry = (*entry & !(MMUFlags::W.bits())) | (previous_flag | MMUFlags::S).bits();
|
*entry = (*entry & !(MMUFlags::W.bits())) | (previous_flag | MMUFlags::S).bits();
|
||||||
print!(
|
klog!(
|
||||||
" [additionally, mapping {:08x} into PID {:08x} @ {:08x}]",
|
"additionally, mapping {:08x} into PID {:08x} @ {:08x}",
|
||||||
phys, dest_pid, dest_addr as usize
|
phys, dest_pid, dest_addr as usize
|
||||||
);
|
);
|
||||||
unsafe { flush_mmu() };
|
unsafe { flush_mmu() };
|
||||||
|
|
|
||||||
|
|
@ -291,7 +291,6 @@ impl Process {
|
||||||
*thread = Default::default();
|
*thread = Default::default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let pid = pid.get();
|
|
||||||
let process = unsafe { &mut *PROCESS };
|
let process = unsafe { &mut *PROCESS };
|
||||||
let mut thread = &mut process.threads[tid];
|
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();
|
thread.registers[9] = thread_init.arg.map(|x| x.get()).unwrap_or_default();
|
||||||
|
|
||||||
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||||
if pid != 1 {
|
{
|
||||||
print!(
|
let pid = pid.get();
|
||||||
" [initializing PID {} thread {} with entrypoint {:08x}, stack @ {:08x}, arg {:08x}]",
|
if pid != 1 {
|
||||||
pid, tid, thread.sepc, thread.registers[1], thread.registers[9],
|
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();
|
process.inner = Default::default();
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
|
#[cfg(baremetal)]
|
||||||
use core::fmt::{Error, Write};
|
use core::fmt::{Error, Write};
|
||||||
|
#[cfg(baremetal)]
|
||||||
use utralib::generated::*;
|
use utralib::generated::*;
|
||||||
|
|
||||||
#[macro_use]
|
#[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 {
|
pub mod debug_print_hardware {
|
||||||
use crate::debug::*;
|
use crate::debug::*;
|
||||||
pub const SUPERVISOR_UART: Uart = Uart {
|
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;
|
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_export]
|
||||||
macro_rules! print {
|
macro_rules! print {
|
||||||
($($args:tt)+) => {{
|
($($args:tt)+) => {{
|
||||||
|
|
@ -29,6 +31,7 @@ macro_rules! print {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(baremetal)]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! println
|
macro_rules! println
|
||||||
{
|
{
|
||||||
|
|
@ -43,10 +46,12 @@ macro_rules! println
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(baremetal)]
|
||||||
pub struct Uart {
|
pub struct Uart {
|
||||||
pub base: *mut usize,
|
pub base: *mut usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(baremetal)]
|
||||||
impl Uart {
|
impl Uart {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn enable_rx(self) {
|
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) {
|
pub fn irq(_irq_number: usize, _arg: *mut usize) {
|
||||||
println!(
|
println!(
|
||||||
"Interrupt {}: Key pressed: {}",
|
"Interrupt {}: Key pressed: {}",
|
||||||
|
|
@ -90,6 +95,7 @@ pub fn irq(_irq_number: usize, _arg: *mut usize) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(baremetal)]
|
||||||
impl Write for Uart {
|
impl Write for Uart {
|
||||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||||
for c in s.bytes() {
|
for c in s.bytes() {
|
||||||
|
|
@ -98,3 +104,27 @@ impl Write for Uart {
|
||||||
Ok(())
|
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]
|
#[macro_use]
|
||||||
extern crate bitflags;
|
extern crate bitflags;
|
||||||
|
|
||||||
#[cfg(baremetal)]
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod debug;
|
mod debug;
|
||||||
|
|
||||||
|
|
@ -26,9 +25,6 @@ mod syscall;
|
||||||
use services::SystemServices;
|
use services::SystemServices;
|
||||||
use xous_kernel::*;
|
use xous_kernel::*;
|
||||||
|
|
||||||
#[cfg(baremetal)]
|
|
||||||
use utralib::generated::*;
|
|
||||||
|
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
use core::panic::PanicInfo;
|
use core::panic::PanicInfo;
|
||||||
#[cfg(baremetal)]
|
#[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.
|
// handler then directly map it.
|
||||||
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
#[cfg(any(feature = "debug-print", feature = "print-panics"))]
|
||||||
{
|
{
|
||||||
|
use utralib::generated::*;
|
||||||
// Map the serial port so println!() works as expected.
|
// Map the serial port so println!() works as expected.
|
||||||
mem::MemoryManager::with_mut(|memory_manager| {
|
mem::MemoryManager::with_mut(|memory_manager| {
|
||||||
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...");
|
println!("KMAIN: Supervisor mode started...");
|
||||||
debug::SUPERVISOR_UART.enable_rx();
|
debug::SUPERVISOR_UART.enable_rx();
|
||||||
println!("Claiming IRQ {} via syscall...", utra::uart::UART_IRQ);
|
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!("}} ");
|
||||||
|
|
||||||
// Print the processed kernel arguments
|
// Print the processed kernel arguments
|
||||||
|
|
@ -154,8 +152,9 @@ pub extern "C" fn kmain() {
|
||||||
match pid {
|
match pid {
|
||||||
Some(pid) => {
|
Some(pid) => {
|
||||||
// #[cfg(feature = "debug-print")]
|
// #[cfg(feature = "debug-print")]
|
||||||
// print!(" [switching to pid {}]", pid);
|
// klog!("switching to pid {}", pid);
|
||||||
xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0)).expect("couldn't switch to pid");
|
xous_kernel::rsyscall(xous_kernel::SysCall::SwitchTo(pid, 0))
|
||||||
|
.expect("couldn't switch to pid");
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
|
|
|
||||||
|
|
@ -337,7 +337,7 @@ impl Server {
|
||||||
// println!("KERNEL: index exceeds queue length");
|
// println!("KERNEL: index exceeds queue length");
|
||||||
return Err(xous_kernel::Error::BadAddress);
|
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] {
|
let (pid, tid, server_addr, client_addr, len, forget, is_memory) = match self.queue[idx] {
|
||||||
QueuedMessage::WaitingReturnMemory(pid, tid, server_addr, client_addr, len) => {
|
QueuedMessage::WaitingReturnMemory(pid, tid, server_addr, client_addr, len) => {
|
||||||
(pid, tid, server_addr, client_addr, len, false, true)
|
(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
|
// 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 {
|
if !is_memory {
|
||||||
return Ok(WaitingMessage::ScalarMessage(
|
return Ok(WaitingMessage::ScalarMessage(
|
||||||
|
|
@ -753,7 +753,7 @@ impl Server {
|
||||||
}
|
}
|
||||||
let mut test_thread_mask = 1;
|
let mut test_thread_mask = 1;
|
||||||
let mut thread_number = 0;
|
let mut thread_number = 0;
|
||||||
print!(" [ready threads: 0b{:08b}]", self.ready_threads);
|
klog!("ready threads: 0b{:08b}", self.ready_threads);
|
||||||
loop {
|
loop {
|
||||||
// If the context mask matches this context number, remove it
|
// If the context mask matches this context number, remove it
|
||||||
// and return the index.
|
// and return the index.
|
||||||
|
|
@ -789,9 +789,9 @@ impl Server {
|
||||||
|
|
||||||
/// Add the given context to the list of ready and waiting contexts.
|
/// Add the given context to the list of ready and waiting contexts.
|
||||||
pub fn park_thread(&mut self, tid: TID) {
|
pub fn park_thread(&mut self, tid: TID) {
|
||||||
print!(" [parking thread {}]", tid);
|
klog!("parking thread {}", tid);
|
||||||
assert!(self.ready_threads & (1 << tid) == 0);
|
assert!(self.ready_threads & (1 << tid) == 0);
|
||||||
self.ready_threads |= 1 << tid;
|
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")
|
.expect("server couldn't be located")
|
||||||
.take_available_thread()
|
.take_available_thread()
|
||||||
{
|
{
|
||||||
print!(
|
klog!(
|
||||||
" [there are contexts available to handle this message -- marking PID {} as Ready]",
|
"there are contexts available to handle this message -- marking PID {} as Ready",
|
||||||
server_pid
|
server_pid
|
||||||
);
|
);
|
||||||
let sender_idx = if message.is_blocking() {
|
let sender_idx = if message.is_blocking() {
|
||||||
|
|
@ -145,9 +145,11 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
||||||
sidx,
|
sidx,
|
||||||
idx: sender_idx,
|
idx: sender_idx,
|
||||||
};
|
};
|
||||||
print!(
|
klog!(
|
||||||
" [server connection data: sidx: {}, idx: {}, server pid: {}]",
|
"server connection data: sidx: {}, idx: {}, server pid: {}",
|
||||||
sidx, sender_idx, server_pid
|
sidx,
|
||||||
|
sender_idx,
|
||||||
|
server_pid
|
||||||
);
|
);
|
||||||
let envelope = MessageEnvelope {
|
let envelope = MessageEnvelope {
|
||||||
sender: sender.into(),
|
sender: sender.into(),
|
||||||
|
|
@ -187,8 +189,8 @@ fn send_message(pid: PID, thread: TID, cid: CID, message: Message) -> SysCallRes
|
||||||
)
|
)
|
||||||
.map(|_| xous_kernel::Result::Ok)
|
.map(|_| xous_kernel::Result::Ok)
|
||||||
} else {
|
} else {
|
||||||
print!(
|
klog!(
|
||||||
" [setting the return value of the Server to {:?} and returning to Client]",
|
"setting the return value of the Server to {:?} and returning to Client",
|
||||||
envelope
|
envelope
|
||||||
);
|
);
|
||||||
// "Switch to" the server PID when not running on bare metal. This ensures
|
// "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)
|
.map(|_| xous_kernel::Result::Ok)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print!(
|
klog!(
|
||||||
" [no threads available in PID {} to handle this message, so blocking]",
|
"no threads available in PID {} to handle this message, so blocking",
|
||||||
server_pid
|
server_pid
|
||||||
);
|
);
|
||||||
// Add this message to the queue. If the queue is full, this
|
// 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);
|
return Err(xous_kernel::Error::ServerNotFound);
|
||||||
}
|
}
|
||||||
let result = server.take_waiting_message(sender.idx, Some(&buf))?;
|
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 {
|
let (client_pid, client_tid, server_addr, client_addr, len) = match result {
|
||||||
WaitingMessage::BorrowedMemory(
|
WaitingMessage::BorrowedMemory(
|
||||||
client_pid,
|
client_pid,
|
||||||
|
|
@ -268,7 +270,7 @@ fn return_memory(
|
||||||
let virt = range.addr.get();
|
let virt = range.addr.get();
|
||||||
let size = range.size.get();
|
let size = range.size.get();
|
||||||
if cfg!(baremetal) && virt & 0xfff != 0 {
|
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);
|
return Err(xous_kernel::Error::BadAlignment);
|
||||||
}
|
}
|
||||||
for addr in (virt..(virt + size)).step_by(PAGE_SIZE) {
|
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 there is a pending message, return it immediately.
|
||||||
if let Some(msg) = server.take_next_message(sidx) {
|
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));
|
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
|
// process and mark ourselves as awaiting an event. When a message
|
||||||
// arrives, our return value will already be set to the
|
// arrives, our return value will already be set to the
|
||||||
// MessageEnvelope of the incoming message.
|
// MessageEnvelope of the incoming message.
|
||||||
print!(
|
klog!(
|
||||||
" [did not have any waiting messages -- parking thread {}]",
|
"did not have any waiting messages -- parking thread {}",
|
||||||
tid
|
tid
|
||||||
);
|
);
|
||||||
server.park_thread(tid);
|
server.park_thread(tid);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue