mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
Merge remote-tracking branch 'origin/hardware' into hardware
This commit is contained in:
commit
50b521266f
15 changed files with 641 additions and 60 deletions
|
|
@ -14,6 +14,7 @@ members = [
|
||||||
"services/fcc-agent",
|
"services/fcc-agent",
|
||||||
"services/xous-names",
|
"services/xous-names",
|
||||||
"services/keyboard",
|
"services/keyboard",
|
||||||
|
"services/kernel-test",
|
||||||
"services/trng",
|
"services/trng",
|
||||||
"services/gam",
|
"services/gam",
|
||||||
"services/benchmark",
|
"services/benchmark",
|
||||||
|
|
|
||||||
|
|
@ -593,6 +593,12 @@ pub fn virt_to_phys(virt: usize) -> Result<usize, xous_kernel::Error> {
|
||||||
|
|
||||||
// Ensure the entry hasn't already been mapped.
|
// Ensure the entry hasn't already been mapped.
|
||||||
if l0_pt.entries[vpn0] & MMUFlags::VALID.bits() == 0 {
|
if l0_pt.entries[vpn0] & MMUFlags::VALID.bits() == 0 {
|
||||||
|
// The memory has been reserved, but isn't pointing anywhere yet.
|
||||||
|
if l0_pt.entries[vpn0] != 0 {
|
||||||
|
return Err(xous_kernel::Error::MemoryInUse);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The address hasn't been allocated
|
||||||
return Err(xous_kernel::Error::BadAddress);
|
return Err(xous_kernel::Error::BadAddress);
|
||||||
}
|
}
|
||||||
Ok((l0_pt.entries[vpn0] >> 10) << 12)
|
Ok((l0_pt.entries[vpn0] >> 10) << 12)
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ use core::fmt::{Error, Write};
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
use utralib::generated::*;
|
use utralib::generated::*;
|
||||||
|
|
||||||
|
static mut INITIALIZED: bool = false;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
#[cfg(all(
|
#[cfg(all(
|
||||||
not(test),
|
not(test),
|
||||||
|
|
@ -67,12 +69,17 @@ pub struct Uart {
|
||||||
#[cfg(baremetal)]
|
#[cfg(baremetal)]
|
||||||
impl Uart {
|
impl Uart {
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn enable_rx(self) {
|
pub fn init(self) {
|
||||||
|
unsafe { INITIALIZED = true };
|
||||||
let mut uart_csr = CSR::new(0xffcf_0000 as *mut u32);
|
let mut uart_csr = CSR::new(0xffcf_0000 as *mut u32);
|
||||||
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
|
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn putc(&self, c: u8) {
|
pub fn putc(&self, c: u8) {
|
||||||
|
if unsafe { INITIALIZED != true } {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let mut uart_csr = CSR::new(0xffcf_0000 as *mut u32);
|
let mut uart_csr = CSR::new(0xffcf_0000 as *mut u32);
|
||||||
// Wait until TXFULL is `0`
|
// Wait until TXFULL is `0`
|
||||||
while uart_csr.r(utra::uart::TXFULL) != 0 {
|
while uart_csr.r(utra::uart::TXFULL) != 0 {
|
||||||
|
|
|
||||||
|
|
@ -76,8 +76,8 @@ pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offs
|
||||||
)
|
)
|
||||||
.expect("unable to map serial port")
|
.expect("unable to map serial port")
|
||||||
});
|
});
|
||||||
|
debug::Uart{}.init();
|
||||||
println!("KMAIN: Supervisor mode started...");
|
println!("KMAIN: Supervisor mode started...");
|
||||||
debug::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)
|
xous_kernel::claim_interrupt(utra::uart::UART_IRQ, debug::irq, 0 as *mut usize)
|
||||||
.expect("Couldn't claim debug interrupt");
|
.expect("Couldn't claim debug interrupt");
|
||||||
|
|
|
||||||
|
|
@ -290,7 +290,6 @@ impl MemoryManager {
|
||||||
|
|
||||||
// Look for a sequence of `size` pages that are free.
|
// Look for a sequence of `size` pages that are free.
|
||||||
for potential_start in (initial..end - size).step_by(PAGE_SIZE) {
|
for potential_start in (initial..end - size).step_by(PAGE_SIZE) {
|
||||||
// println!(" Checking {:08x}...", potential_start);
|
|
||||||
let mut all_free = true;
|
let mut all_free = true;
|
||||||
for check_page in (potential_start..potential_start + size).step_by(PAGE_SIZE) {
|
for check_page in (potential_start..potential_start + size).step_by(PAGE_SIZE) {
|
||||||
if !crate::arch::mem::address_available(check_page) {
|
if !crate::arch::mem::address_available(check_page) {
|
||||||
|
|
@ -313,7 +312,6 @@ impl MemoryManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
for potential_start in (start..initial).step_by(PAGE_SIZE) {
|
for potential_start in (start..initial).step_by(PAGE_SIZE) {
|
||||||
// println!(" Checking {:08x}...", potential_start);
|
|
||||||
let mut all_free = true;
|
let mut all_free = true;
|
||||||
for check_page in (potential_start..potential_start + size).step_by(PAGE_SIZE) {
|
for check_page in (potential_start..potential_start + size).step_by(PAGE_SIZE) {
|
||||||
if !crate::arch::mem::address_available(check_page) {
|
if !crate::arch::mem::address_available(check_page) {
|
||||||
|
|
@ -334,7 +332,6 @@ impl MemoryManager {
|
||||||
return Ok(potential_start as *mut u8);
|
return Ok(potential_start as *mut u8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// println!("Couldn't find virtual address");
|
|
||||||
Err(xous_kernel::Error::BadAddress)
|
Err(xous_kernel::Error::BadAddress)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ pub struct SystemServices {
|
||||||
_syscall_depth: usize,
|
_syscall_depth: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
pub enum ProcessState {
|
pub enum ProcessState {
|
||||||
/// This is an unallocated, free process
|
/// This is an unallocated, free process
|
||||||
Free,
|
Free,
|
||||||
|
|
@ -62,6 +62,20 @@ pub enum ProcessState {
|
||||||
Sleeping,
|
Sleeping,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Debug for ProcessState {
|
||||||
|
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
|
||||||
|
use ProcessState::*;
|
||||||
|
match *self {
|
||||||
|
Free => write!(fmt, "Free"),
|
||||||
|
Allocated => write!(fmt, "Allocated"),
|
||||||
|
Setup(ti) => write!(fmt, "Setup({:?})", ti),
|
||||||
|
Ready(rt) => write!(fmt, "Ready({:b})", rt),
|
||||||
|
Running(rt) => write!(fmt, "Running({:b})", rt),
|
||||||
|
Sleeping => write!(fmt, "Sleeping"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for ProcessState {
|
impl Default for ProcessState {
|
||||||
fn default() -> ProcessState {
|
fn default() -> ProcessState {
|
||||||
ProcessState::Free
|
ProcessState::Free
|
||||||
|
|
@ -541,7 +555,6 @@ impl SystemServices {
|
||||||
/// Mark the specified context as ready to run. If the thread is Sleeping, mark
|
/// Mark the specified context as ready to run. If the thread is Sleeping, mark
|
||||||
/// it as Ready.
|
/// it as Ready.
|
||||||
pub fn ready_thread(&mut self, pid: PID, tid: TID) -> Result<(), xous_kernel::Error> {
|
pub fn ready_thread(&mut self, pid: PID, tid: TID) -> Result<(), xous_kernel::Error> {
|
||||||
// assert!(tid == INITIAL_TID);
|
|
||||||
let process = self.get_process_mut(pid)?;
|
let process = self.get_process_mut(pid)?;
|
||||||
process.state = match process.state {
|
process.state = match process.state {
|
||||||
ProcessState::Free => {
|
ProcessState::Free => {
|
||||||
|
|
@ -684,10 +697,13 @@ impl SystemServices {
|
||||||
ProcessState::Running(new_mask)
|
ProcessState::Running(new_mask)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// println!(
|
// println!(
|
||||||
// "switch_to_thread({}:{:?}): New state is {:?}",
|
// "switch_to_thread({}:{:?}): New state is {:?} Thread is ",
|
||||||
// pid, tid, process.state
|
// pid, tid, process.state
|
||||||
// );
|
// );
|
||||||
|
// ArchProcess::with_current(|current| current.print_thread());
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -705,6 +721,8 @@ impl SystemServices {
|
||||||
// "switch_from_thread({}:{}): Old state was {:?}",
|
// "switch_from_thread({}:{}): Old state was {:?}",
|
||||||
// pid, tid, process.state
|
// pid, tid, process.state
|
||||||
// );
|
// );
|
||||||
|
// ArchProcess::with_current(|current| current.print_thread());
|
||||||
|
|
||||||
process.state = match process.state {
|
process.state = match process.state {
|
||||||
ProcessState::Running(x) if x & (1 << tid) != 0 => panic!(
|
ProcessState::Running(x) if x & (1 << tid) != 0 => panic!(
|
||||||
"PID {} thread {} was already queued for running when `switch_from_thread()` was called",
|
"PID {} thread {} was already queued for running when `switch_from_thread()` was called",
|
||||||
|
|
@ -798,6 +816,7 @@ impl SystemServices {
|
||||||
"\n\r KERNEL({},{}): Activating process {} thread {}",
|
"\n\r KERNEL({},{}): Activating process {} thread {}",
|
||||||
previous_pid, previous_tid, new_pid, new_tid
|
previous_pid, previous_tid, new_pid, new_tid
|
||||||
);
|
);
|
||||||
|
ArchProcess::with_current(|current| current.print_thread());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save state if the PID has changed. This will activate the new memory
|
// Save state if the PID has changed. This will activate the new memory
|
||||||
|
|
@ -823,11 +842,6 @@ impl SystemServices {
|
||||||
new.state
|
new.state
|
||||||
);
|
);
|
||||||
if new_tid == 0 {
|
if new_tid == 0 {
|
||||||
print!(
|
|
||||||
"PID {}: Looking for a valid context in the mask {:08b}",
|
|
||||||
new_pid, x,
|
|
||||||
);
|
|
||||||
//new_tid = 0;
|
|
||||||
new_tid = (new.current_thread + 1) as usize;
|
new_tid = (new.current_thread + 1) as usize;
|
||||||
while x & (1 << new_tid) == 0 {
|
while x & (1 << new_tid) == 0 {
|
||||||
new_tid += 1;
|
new_tid += 1;
|
||||||
|
|
@ -835,22 +849,22 @@ impl SystemServices {
|
||||||
new_tid = 0;
|
new_tid = 0;
|
||||||
} else if new_tid == (new.current_thread + 1) as usize {
|
} else if new_tid == (new.current_thread + 1) as usize {
|
||||||
// If we've looped around, return an error.
|
// If we've looped around, return an error.
|
||||||
println!("Looked through all contexts and couldn't find one that was ready");
|
// println!("Looked through all contexts and couldn't find one that was ready");
|
||||||
return Err(xous_kernel::Error::ProcessNotFound);
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
new.current_thread = new_tid as _;
|
new.current_thread = new_tid as _;
|
||||||
println!(" -- picked thread {}", new_tid);
|
// println!(" -- picked thread {}", new_tid);
|
||||||
} else if x & (1 << new_tid) == 0 {
|
} else if x & (1 << new_tid) == 0 {
|
||||||
println!(
|
// println!(
|
||||||
"thread is {:?}, which is not valid for new thread {}",
|
// "thread is {:?}, which is not valid for new thread {}",
|
||||||
new.state, new_tid
|
// new.state, new_tid
|
||||||
);
|
// );
|
||||||
return Err(xous_kernel::Error::ProcessNotFound);
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ProcessState::Sleeping => {
|
ProcessState::Sleeping => {
|
||||||
println!("PID {} was sleeping", new_pid);
|
// println!("PID {} was sleeping", new_pid);
|
||||||
return Err(xous_kernel::Error::ProcessNotFound);
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -928,50 +942,53 @@ impl SystemServices {
|
||||||
// previous_pid, previous.state, can_resume
|
// previous_pid, previous.state, can_resume
|
||||||
// );
|
// );
|
||||||
} else {
|
} else {
|
||||||
// if self.current_thread(previous_pid) == new_tid {
|
let new = self.get_process_mut(new_pid)?;
|
||||||
// if !can_resume {
|
|
||||||
// panic!("tried to switch to our own context without resume");
|
|
||||||
// }
|
|
||||||
// return Ok(new_tid);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// we can get here if we're activating a new thread in the same process
|
// If we wanted to switch to a "new" thread, and it's the same
|
||||||
//return Ok(new_tid);
|
// as the one we just switched from, do nothing.
|
||||||
|
if new.current_thread == new_tid {
|
||||||
let new = self.get_process_mut(new_pid)?; /*
|
if !can_resume {
|
||||||
new.state = match new.state {
|
panic!("tried to switch to our own context without resume");
|
||||||
ProcessState::Running(x) if (x & 1 << new_tid) == 0 => {
|
|
||||||
return Err(xous_kernel::Error::ProcessNotFound)
|
|
||||||
}
|
}
|
||||||
// ProcessState::Running(x) => {
|
return Ok(new_tid);
|
||||||
// if can_resume {
|
}
|
||||||
// ProcessState::Running((x | (1 << previous_tid)) & !(1 << new_tid))
|
|
||||||
// } else {
|
// Transition to the new state.
|
||||||
// ProcessState::Running(x | (1 << previous_tid))
|
new.state = if let ProcessState::Running(x) = new.state {
|
||||||
// }
|
let previous_tid = new.current_thread;
|
||||||
// }
|
// If no new thread is specified, take the previous
|
||||||
other => */
|
// thread. If that is not runnable, do a round-robin
|
||||||
|
// search for the next available thread.
|
||||||
|
if new_tid == 0 {
|
||||||
|
new_tid = (new.current_thread + 1) as usize;
|
||||||
|
while x & (1 << new_tid) == 0 {
|
||||||
|
new_tid += 1;
|
||||||
|
if new_tid > arch::process::MAX_THREAD {
|
||||||
|
new_tid = 0;
|
||||||
|
} else if new_tid == (new.current_thread + 1) as usize {
|
||||||
|
// If we've looped around, return an error.
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new.current_thread = new_tid as _;
|
||||||
|
} else if x & (1 << new_tid) == 0 {
|
||||||
|
return Err(xous_kernel::Error::ProcessNotFound);
|
||||||
|
}
|
||||||
|
// Mark the previous TID as being runnable, and remove the new TID
|
||||||
|
// from the list of threads that can be run.
|
||||||
|
ProcessState::Running(x & !(1 << new_tid) | if can_resume { 1 << previous_tid } else { 0 })
|
||||||
|
} else {
|
||||||
panic!(
|
panic!(
|
||||||
"PID {} invalid process state (not Running): {:?}",
|
"PID {} invalid process state (not Running): {:?}",
|
||||||
previous_pid, new.state
|
previous_pid, new.state
|
||||||
) /*,
|
)
|
||||||
}*/
|
};
|
||||||
;
|
|
||||||
// if advance_thread {
|
|
||||||
// new.current_thread += 1;
|
|
||||||
// if new.current_thread as TID > arch::process::MAX_CONTEXT {
|
|
||||||
// new.current_thread = 0;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
// self.pid = new_pid;
|
|
||||||
|
|
||||||
let mut process = crate::arch::process::Process::current();
|
let mut process = crate::arch::process::Process::current();
|
||||||
|
|
||||||
// Restore the previous context, if one exists.
|
// Restore the previous thread, if one exists.
|
||||||
process.set_thread(new_tid)?;
|
process.set_thread(new_tid)?;
|
||||||
// self.processes[new_pid.get() as usize - 1].current_thread = new_tid as u8;
|
|
||||||
// let _ctx = process.current_context();
|
|
||||||
|
|
||||||
Ok(new_tid)
|
Ok(new_tid)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -578,8 +578,6 @@ pub fn handle(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallResult
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallResult {
|
pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallResult {
|
||||||
// let pid = arch::current_pid();
|
|
||||||
|
|
||||||
match call {
|
match call {
|
||||||
SysCall::MapMemory(phys, virt, size, req_flags) => {
|
SysCall::MapMemory(phys, virt, size, req_flags) => {
|
||||||
MemoryManager::with_mut(|mm| {
|
MemoryManager::with_mut(|mm| {
|
||||||
|
|
|
||||||
16
services/kernel-test/Cargo.toml
Normal file
16
services/kernel-test/Cargo.toml
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
[package]
|
||||||
|
name = "kernel-test"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Sean Cross <sean@xobs.io>"]
|
||||||
|
edition = "2018"
|
||||||
|
description = "Program to test various aspects of the kernel"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
xous = { path = "../../xous-rs" }
|
||||||
|
|
||||||
|
[target.'cfg(not(any(windows,unix)))'.dependencies]
|
||||||
|
utralib = { path = "../../utralib"}
|
||||||
|
|
||||||
|
[features]
|
||||||
|
logging = [] # adding this allocates the UART for logging of Xous
|
||||||
|
default = ["logging"]
|
||||||
5
services/kernel-test/README.md
Normal file
5
services/kernel-test/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Kernel Test
|
||||||
|
|
||||||
|
Run various tests inside the kernel.
|
||||||
|
|
||||||
|
This is supposed to be the only program running.
|
||||||
17
services/kernel-test/build.rs
Normal file
17
services/kernel-test/build.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
// NOTE: Adapted from cortex-m/build.rs
|
||||||
|
use std::env;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let target = env::var("TARGET").unwrap();
|
||||||
|
|
||||||
|
let target_os = target.split('-').nth(2).unwrap_or("none");
|
||||||
|
|
||||||
|
// If we're not running on a desktop-class operating system, emit the "baremetal"
|
||||||
|
// config setting. This will enable software to do tasks such as
|
||||||
|
// managing memory.
|
||||||
|
if target_os == "none" {
|
||||||
|
println!("cargo:rustc-cfg=baremetal");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
}
|
||||||
111
services/kernel-test/src/debug.rs
Normal file
111
services/kernel-test/src/debug.rs
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
use core::fmt::{Error, Write};
|
||||||
|
|
||||||
|
use utralib::generated::*;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! print
|
||||||
|
{
|
||||||
|
($($args:tt)+) => ({
|
||||||
|
use core::fmt::Write;
|
||||||
|
let _ = write!(crate::debug::Uart {}, $($args)+);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! println
|
||||||
|
{
|
||||||
|
() => ({
|
||||||
|
print!("\r\n")
|
||||||
|
});
|
||||||
|
($fmt:expr) => ({
|
||||||
|
print!(concat!($fmt, "\r\n"))
|
||||||
|
});
|
||||||
|
($fmt:expr, $($args:tt)+) => ({
|
||||||
|
print!(concat!($fmt, "\r\n"), $($args)+)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_irq(irq_no: usize, arg: *mut usize) {
|
||||||
|
print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize);
|
||||||
|
|
||||||
|
while let Some(c) = { crate::debug::Uart {} }.getc() {
|
||||||
|
print!("{}", c as char);
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Uart {}
|
||||||
|
|
||||||
|
// this is a hack to bypass an explicit initialization/allocation step for the debug structure
|
||||||
|
pub static mut DEFAULT_UART_ADDR: *mut usize = 0x0000_0000 as *mut usize;
|
||||||
|
|
||||||
|
pub const DEFAULT: Uart = Uart {};
|
||||||
|
|
||||||
|
impl Uart {
|
||||||
|
fn map_uart(&self) {
|
||||||
|
/*
|
||||||
|
Note: the memory address and interrupt specified here needs to map to a unique hardware
|
||||||
|
UART resource. Modify in this function as necessary.
|
||||||
|
*/
|
||||||
|
let uart = xous::syscall::map_memory(
|
||||||
|
xous::MemoryAddress::new(utra::console::HW_CONSOLE_BASE),
|
||||||
|
None,
|
||||||
|
4096,
|
||||||
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||||
|
)
|
||||||
|
.expect("couldn't map debug uart");
|
||||||
|
unsafe {
|
||||||
|
DEFAULT_UART_ADDR = uart.as_mut_ptr() as _;
|
||||||
|
}
|
||||||
|
println!("Mapped UART @ {:08x}", uart.addr.get());
|
||||||
|
// core::mem::forget(uart);
|
||||||
|
|
||||||
|
println!("Allocating IRQ...");
|
||||||
|
xous::claim_interrupt(
|
||||||
|
utra::console::CONSOLE_IRQ,
|
||||||
|
handle_irq,
|
||||||
|
core::ptr::null_mut::<usize>(),
|
||||||
|
)
|
||||||
|
.expect("unable to allocate IRQ");
|
||||||
|
self.enable_rx();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn putc(&self, c: u8) {
|
||||||
|
if unsafe { DEFAULT_UART_ADDR } as usize == 0 {
|
||||||
|
self.map_uart();
|
||||||
|
}
|
||||||
|
let mut uart_csr = CSR::new(unsafe { DEFAULT_UART_ADDR as *mut u32 });
|
||||||
|
|
||||||
|
// Wait until TXFULL is `0`
|
||||||
|
while uart_csr.r(utra::uart::TXFULL) != 0 {}
|
||||||
|
uart_csr.wo(utra::uart::RXTX, c as u32);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn enable_rx(&self) {
|
||||||
|
let mut uart_csr = CSR::new(unsafe { DEFAULT_UART_ADDR as *mut u32 });
|
||||||
|
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getc(&self) -> Option<u8> {
|
||||||
|
if unsafe { DEFAULT_UART_ADDR } as usize == 0 {
|
||||||
|
self.map_uart();
|
||||||
|
}
|
||||||
|
let mut uart_csr = CSR::new(unsafe { DEFAULT_UART_ADDR as *mut u32 });
|
||||||
|
match uart_csr.rf(utra::uart::EV_PENDING_RX) {
|
||||||
|
0 => None,
|
||||||
|
ack => {
|
||||||
|
let c = Some(uart_csr.rf(utra::uart::RXTX_RXTX) as u8);
|
||||||
|
uart_csr.wfo(utra::uart::EV_PENDING_RX, ack);
|
||||||
|
c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for Uart {
|
||||||
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||||
|
for c in s.bytes() {
|
||||||
|
self.putc(c);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
57
services/kernel-test/src/log_string.rs
Normal file
57
services/kernel-test/src/log_string.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
use core::fmt;
|
||||||
|
use core::slice;
|
||||||
|
|
||||||
|
pub struct LogString<'a> {
|
||||||
|
raw_slice: &'a mut [u8],
|
||||||
|
pub s: &'a str,
|
||||||
|
pub len: u32,
|
||||||
|
msg_len: &'a mut Option<xous::MemorySize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> LogString<'a> {
|
||||||
|
pub fn from_message(message: &'a mut xous::MemoryMessage) -> LogString<'a> {
|
||||||
|
// println!("LOG: Message address is at {:08x} (whole message: {:?})", message.buf.addr.get(), message);
|
||||||
|
let raw_slice = unsafe { slice::from_raw_parts_mut(message.buf.as_ptr() as *mut u8, message.buf.len() as usize) };
|
||||||
|
let starting_length = message.valid.map(|x| x.get()).unwrap_or(0);
|
||||||
|
|
||||||
|
// print!("LOG: String @ {:08x}:", message.buf.as_ptr() as usize);
|
||||||
|
// for offset in 0..starting_length {
|
||||||
|
// print!(" {:02x}", raw_slice[offset]);
|
||||||
|
// }
|
||||||
|
// println!(" (length: {})", starting_length);
|
||||||
|
|
||||||
|
LogString {
|
||||||
|
s: unsafe {
|
||||||
|
core::str::from_utf8_unchecked(slice::from_raw_parts(
|
||||||
|
message.buf.as_ptr() as *mut u8,
|
||||||
|
starting_length,
|
||||||
|
))
|
||||||
|
},
|
||||||
|
len: starting_length as u32,
|
||||||
|
raw_slice,
|
||||||
|
msg_len: &mut message.valid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> fmt::Display for LogString<'a> {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> fmt::Write for LogString<'a> {
|
||||||
|
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
|
||||||
|
for c in s.bytes() {
|
||||||
|
if self.len as usize < self.raw_slice.len() {
|
||||||
|
self.raw_slice[self.len as usize] = c;
|
||||||
|
self.len += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.s = unsafe {
|
||||||
|
core::str::from_utf8_unchecked(slice::from_raw_parts(self.raw_slice.as_ptr(), self.len as usize))
|
||||||
|
};
|
||||||
|
*self.msg_len = Some(xous::MemorySize::new(self.len as usize).unwrap());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
336
services/kernel-test/src/main.rs
Normal file
336
services/kernel-test/src/main.rs
Normal file
|
|
@ -0,0 +1,336 @@
|
||||||
|
#![cfg_attr(target_os = "none", no_std)]
|
||||||
|
#![cfg_attr(target_os = "none", no_main)]
|
||||||
|
|
||||||
|
#[cfg(baremetal)]
|
||||||
|
#[macro_use]
|
||||||
|
mod debug;
|
||||||
|
|
||||||
|
use core::fmt::Write;
|
||||||
|
use xous::String;
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "none"))]
|
||||||
|
mod implementation {
|
||||||
|
use core::fmt::{Error, Write};
|
||||||
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||||
|
|
||||||
|
enum ControlMessage {
|
||||||
|
Text(String),
|
||||||
|
Byte(u8),
|
||||||
|
Exit,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Output {
|
||||||
|
tx: Sender<ControlMessage>,
|
||||||
|
rx: Receiver<ControlMessage>,
|
||||||
|
stdout: std::io::Stdout,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init() -> Output {
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
|
||||||
|
Output {
|
||||||
|
tx,
|
||||||
|
rx,
|
||||||
|
stdout: std::io::stdout(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Output {
|
||||||
|
pub fn run(&mut self) {
|
||||||
|
use std::io::Write;
|
||||||
|
loop {
|
||||||
|
match self.rx.recv_timeout(std::time::Duration::from_millis(50)) {
|
||||||
|
Ok(msg) => match msg {
|
||||||
|
ControlMessage::Exit => break,
|
||||||
|
ControlMessage::Text(s) => print!("{}", s),
|
||||||
|
ControlMessage::Byte(s) => {
|
||||||
|
let mut handle = self.stdout.lock();
|
||||||
|
handle.write_all(&[s]).unwrap();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
|
||||||
|
Err(e) => panic!("Error: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_writer(&self) -> OutputWriter {
|
||||||
|
OutputWriter {
|
||||||
|
tx: self.tx.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Output {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.tx.send(ControlMessage::Exit).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for Output {
|
||||||
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||||
|
// It would be nice if this worked with &str
|
||||||
|
self.tx.send(ControlMessage::Text(s.to_owned())).unwrap();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct OutputWriter {
|
||||||
|
tx: Sender<ControlMessage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OutputWriter {
|
||||||
|
pub fn putc(&self, c: u8) {
|
||||||
|
self.tx.send(ControlMessage::Byte(c)).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for OutputWriter {
|
||||||
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||||
|
// It would be nice if this worked with &str
|
||||||
|
self.tx.send(ControlMessage::Text(s.to_owned())).unwrap();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "none")]
|
||||||
|
mod implementation {
|
||||||
|
use core::fmt::{Error, Write};
|
||||||
|
use utralib::generated::*;
|
||||||
|
|
||||||
|
pub struct Output {}
|
||||||
|
|
||||||
|
pub fn init() -> Output {
|
||||||
|
if cfg!(feature = "logging") {
|
||||||
|
let uart = xous::syscall::map_memory(
|
||||||
|
xous::MemoryAddress::new(utra::console::HW_CONSOLE_BASE),
|
||||||
|
None,
|
||||||
|
4096,
|
||||||
|
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||||
|
)
|
||||||
|
.expect("couldn't map serial port");
|
||||||
|
unsafe { crate::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ };
|
||||||
|
println!("Mapped UART @ {:08x}", uart.addr.get());
|
||||||
|
|
||||||
|
println!("Process: map success!");
|
||||||
|
crate::debug::DEFAULT.enable_rx();
|
||||||
|
|
||||||
|
println!("Allocating IRQ...");
|
||||||
|
xous::syscall::claim_interrupt(
|
||||||
|
utra::console::CONSOLE_IRQ,
|
||||||
|
handle_irq,
|
||||||
|
core::ptr::null_mut::<usize>(),
|
||||||
|
)
|
||||||
|
.expect("couldn't claim interrupt");
|
||||||
|
println!("Claimed IRQ {}", utra::console::CONSOLE_IRQ);
|
||||||
|
}
|
||||||
|
Output {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Output {
|
||||||
|
pub fn get_writer(&self) -> OutputWriter {
|
||||||
|
OutputWriter {}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(&mut self) {
|
||||||
|
loop {
|
||||||
|
xous::wait_event();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_irq(irq_no: usize, arg: *mut usize) {
|
||||||
|
if cfg!(feature = "logging") {
|
||||||
|
print!("Handling IRQ {} (arg: {:08x}): ", irq_no, arg as usize);
|
||||||
|
|
||||||
|
while let Some(c) = crate::debug::DEFAULT.getc() {
|
||||||
|
print!("0x{:02x}", c);
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct OutputWriter {}
|
||||||
|
|
||||||
|
impl OutputWriter {
|
||||||
|
pub fn putc(&self, c: u8) {
|
||||||
|
if cfg!(feature = "logging") {
|
||||||
|
let mut uart_csr = CSR::new(unsafe { crate::debug::DEFAULT_UART_ADDR as *mut u32 });
|
||||||
|
|
||||||
|
// Wait until TXFULL is `0`
|
||||||
|
while uart_csr.r(utra::uart::TXFULL) != 0 {}
|
||||||
|
uart_csr.wo(utra::uart::RXTX, c as u32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Write for OutputWriter {
|
||||||
|
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||||
|
for c in s.bytes() {
|
||||||
|
self.putc(c);
|
||||||
|
if c == '\n' as u8 {
|
||||||
|
self.putc('\r' as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_scalar(
|
||||||
|
output: &mut implementation::OutputWriter,
|
||||||
|
sender: xous::MessageSender,
|
||||||
|
msg: &xous::ScalarMessage,
|
||||||
|
sender_pid: xous::PID,
|
||||||
|
) {
|
||||||
|
match msg.id {
|
||||||
|
1000 => writeln!(output, "PANIC from PID {} | {}", sender_pid, sender).unwrap(),
|
||||||
|
1100 => (),
|
||||||
|
1101..=1132 => {
|
||||||
|
let mut output_bfr = [0u8; core::mem::size_of::<usize>() * 4];
|
||||||
|
let output_iter = output_bfr.iter_mut();
|
||||||
|
|
||||||
|
// Combine the four arguments to form a single
|
||||||
|
// contiguous buffer. Note: The buffer size will change
|
||||||
|
// depending on the platfor's `usize` length.
|
||||||
|
let arg1_bytes = msg.arg1.to_le_bytes();
|
||||||
|
let arg2_bytes = msg.arg2.to_le_bytes();
|
||||||
|
let arg3_bytes = msg.arg3.to_le_bytes();
|
||||||
|
let arg4_bytes = msg.arg4.to_le_bytes();
|
||||||
|
let input_iter = arg1_bytes
|
||||||
|
.iter()
|
||||||
|
.chain(arg2_bytes.iter())
|
||||||
|
.chain(arg3_bytes.iter())
|
||||||
|
.chain(arg4_bytes.iter());
|
||||||
|
for (dest, src) in output_iter.zip(input_iter) {
|
||||||
|
*dest = *src;
|
||||||
|
}
|
||||||
|
let total_chars = msg.id - 1100;
|
||||||
|
for (idx, c) in output_bfr.iter().enumerate() {
|
||||||
|
if idx >= total_chars {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
output.putc(*c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1200 => writeln!(output, "Terminating process").unwrap(),
|
||||||
|
_ => writeln!(
|
||||||
|
output,
|
||||||
|
"Unrecognized scalar message from {}: {:#?}",
|
||||||
|
sender, msg
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reader_thread(mut output: implementation::OutputWriter) {
|
||||||
|
writeln!(output, "LOG: Xous Logging Server starting up...").unwrap();
|
||||||
|
|
||||||
|
writeln!(output, "LOG: ****************************************************************").unwrap();
|
||||||
|
// time stamp isn't actually the time stamp of the build, unfortunately. It's the time stamp of the
|
||||||
|
// last time you managed to force a rebuild that also causes log-server to be rebuilt, not necessarily
|
||||||
|
// capturing the build time of the very most recent change!
|
||||||
|
// writeln!(output, "LOG: *** Built: {:49} ***", env!("VERGEN_BUILD_TIMESTAMP")).unwrap();
|
||||||
|
writeln!(output, "LOG: ****************************************************************").unwrap();
|
||||||
|
let server_addr = xous::create_server_with_address(b"xous-log-server ").unwrap();
|
||||||
|
writeln!(output, "LOG: Server listening on address {:?}", server_addr).unwrap();
|
||||||
|
|
||||||
|
let mut counter: usize = 0;
|
||||||
|
loop {
|
||||||
|
if counter.trailing_zeros() >= 12 {
|
||||||
|
writeln!(output, "LOG: Counter tick: {}", counter).unwrap();
|
||||||
|
}
|
||||||
|
counter += 1;
|
||||||
|
// writeln!(output, "LOG: Waiting for an event...").unwrap();
|
||||||
|
let mut envelope =
|
||||||
|
xous::syscall::receive_message(server_addr).expect("couldn't get address");
|
||||||
|
let sender = envelope.sender;
|
||||||
|
// writeln!(output, "LOG: Got message envelope: {:?}", envelope).unwrap();
|
||||||
|
match &mut envelope.body {
|
||||||
|
xous::Message::Scalar(msg) => handle_scalar(&mut output, sender, msg, envelope.sender.pid().unwrap()),
|
||||||
|
xous::Message::BlockingScalar(msg) => {
|
||||||
|
writeln!(
|
||||||
|
output,
|
||||||
|
"LOG: BlockingScalar message from {}: {:?}",
|
||||||
|
envelope.sender, msg
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
xous::Message::Move(msg) => {
|
||||||
|
String::<4000>::from_message(msg)
|
||||||
|
.map(|log_entry: String<4000>| {
|
||||||
|
writeln!(
|
||||||
|
output,
|
||||||
|
"LOG: Moved log message from {}: {}",
|
||||||
|
sender, log_entry
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
.or_else(|e| {
|
||||||
|
writeln!(output, "LOG: unable to convert Move message to str: {}", e)
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
xous::Message::Borrow(msg) => {
|
||||||
|
String::<4000>::from_message(msg)
|
||||||
|
.map(|log_entry: String<4000>| writeln!(output, "{}", log_entry).unwrap())
|
||||||
|
.or_else(|e| {
|
||||||
|
writeln!(
|
||||||
|
output,
|
||||||
|
"LOG: unable to convert Borrow message to str: {}",
|
||||||
|
e
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
xous::Message::MutableBorrow(msg) => {
|
||||||
|
String::<4000>::from_message(msg)
|
||||||
|
.map(|mut log_entry: String<4000>| {
|
||||||
|
writeln!(
|
||||||
|
output,
|
||||||
|
"LOG: Mutable borrowed log message from {} len {}:\n\r {}\n\r",
|
||||||
|
sender,
|
||||||
|
log_entry.len(),
|
||||||
|
log_entry,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
writeln!(log_entry, " << HELLO FROM THE SERVER").unwrap();
|
||||||
|
})
|
||||||
|
.or_else(|e| {
|
||||||
|
writeln!(
|
||||||
|
output,
|
||||||
|
"LOG: unable to convert MutableBorrow message to str: {}",
|
||||||
|
e
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn writer_thread(_ignored: usize) {
|
||||||
|
let mut counter = 0;
|
||||||
|
loop {
|
||||||
|
let conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap();
|
||||||
|
let mut buffer = xous::String::<4000>::new();
|
||||||
|
write!(buffer, "Hello, myself! Loop: {}", counter).unwrap();
|
||||||
|
buffer.lend(conn).unwrap();
|
||||||
|
|
||||||
|
counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[xous::xous_main]
|
||||||
|
fn test_main() -> ! {
|
||||||
|
let mut output = implementation::init();
|
||||||
|
let writer = output.get_writer();
|
||||||
|
println!("LOG: my PID is {}", xous::process::id());
|
||||||
|
println!("LOG: Creating the reader thread");
|
||||||
|
xous::create_thread_simple(reader_thread, writer).unwrap();
|
||||||
|
xous::create_thread_simple(writer_thread, 0usize).unwrap();
|
||||||
|
println!("LOG: Running the output");
|
||||||
|
output.run();
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
@ -491,6 +491,17 @@ impl From<usize> for MemoryType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl core::fmt::Display for MemoryType {
|
||||||
|
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
match *self {
|
||||||
|
MemoryType::Default => write!(fmt, "Default"),
|
||||||
|
MemoryType::Heap => write!(fmt, "Heap"),
|
||||||
|
MemoryType::Stack => write!(fmt, "Stack"),
|
||||||
|
MemoryType::Messages => write!(fmt, "Messages"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum Result {
|
pub enum Result {
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,7 @@ fn try_main() -> Result<(), DynError> {
|
||||||
let task = env::args().nth(1);
|
let task = env::args().nth(1);
|
||||||
match task.as_deref() {
|
match task.as_deref() {
|
||||||
Some("renode-image") => renode_image(false, &hw_pkgs)?,
|
Some("renode-image") => renode_image(false, &hw_pkgs)?,
|
||||||
|
Some("renode-test") => renode_image(false, &["kernel-test"])?,
|
||||||
Some("renode-image-debug") => renode_image(true, &hw_pkgs)?,
|
Some("renode-image-debug") => renode_image(true, &hw_pkgs)?,
|
||||||
Some("run") => run(false)?,
|
Some("run") => run(false)?,
|
||||||
Some("hw-image") => build_hw_image(false, env::args().nth(2), &hw_pkgs)?,
|
Some("hw-image") => build_hw_image(false, env::args().nth(2), &hw_pkgs)?,
|
||||||
|
|
@ -87,7 +88,8 @@ fn try_main() -> Result<(), DynError> {
|
||||||
fn print_help() {
|
fn print_help() {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"Tasks:
|
"Tasks:
|
||||||
renode-image builds a test image for renode
|
renode-image builds a functional image for renode
|
||||||
|
renode-test builds a test image for renode
|
||||||
renode-image-debug builds a test image for renode in debug mode
|
renode-image-debug builds a test image for renode in debug mode
|
||||||
hw-image [soc.svd] builds an image for real hardware
|
hw-image [soc.svd] builds an image for real hardware
|
||||||
run runs a release build using a hosted environment
|
run runs a release build using a hosted environment
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue