From 6f86f3b115b4c0bf5b6973581e0c114fee08ae3c Mon Sep 17 00:00:00 2001 From: Vihaan Jim Date: Tue, 4 Jul 2023 15:27:38 -0400 Subject: [PATCH] Allow logging from spawn This allows the spawn (i.e. new process) to use log. Firstly, xous-api-log was changed to allow no_std when the target os is xous (this is done using a feature). Then, in spawn itself, an AtomicU32 is moved into memory as a replacement for XOUS_LOGGER_CONNECTION, which is not in .text and is thus not in the stub file. --- Cargo.toml | 4 ++-- api/xous-api-log/Cargo.toml | 1 + api/xous-api-log/src/lib.rs | 6 ++++-- services/app-loader/build.rs | 22 ---------------------- services/app-loader/spawn/Cargo.toml | 2 ++ services/app-loader/spawn/src/main.rs | 17 +++++++++++++++++ 6 files changed, 26 insertions(+), 26 deletions(-) delete mode 100644 services/app-loader/build.rs diff --git a/Cargo.toml b/Cargo.toml index 92da63194..d20b01cb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -179,7 +179,7 @@ rev="4296c2e7904898766cf7d8d589759a129794783b" # use the commitref because we're # path = "./api/xous-api-names" # [patch.crates-io.xous-api-susres] # path = "./api/xous-api-susres" -# [patch.crates-io.xous-api-log] -# path = "./api/xous-api-log" +[patch.crates-io.xous-api-log] +path = "./api/xous-api-log" # [patch.crates-io.xous-api-ticktimer] # path = "./api/xous-api-ticktimer" diff --git a/api/xous-api-log/Cargo.toml b/api/xous-api-log/Cargo.toml index 98a646c63..a6fc8109d 100644 --- a/api/xous-api-log/Cargo.toml +++ b/api/xous-api-log/Cargo.toml @@ -19,4 +19,5 @@ num-traits = {version = "0.2.14", default-features = false} [features] debugprint = [] # adding this allocates the UART for debugging the logger logging = [] # adding this allocates the hardware UART for console interactions +nostd = [] # adding this causes the program to be built without std default = ["logging"] diff --git a/api/xous-api-log/src/lib.rs b/api/xous-api-log/src/lib.rs index 7748c54b0..282c5d0f6 100644 --- a/api/xous-api-log/src/lib.rs +++ b/api/xous-api-log/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(any(target_os = "none", feature = "nostd"), no_std)] use core::fmt::Write; use core::sync::atomic::{AtomicU32, Ordering}; use num_traits::ToPrimitive; @@ -96,8 +96,10 @@ pub fn init() -> Result<(), LogError> { } pub fn init_wait() -> Result<(), ()> { + let sid = xous::SID::from_bytes(b"xous-log-server ").unwrap(); + let cid = xous::connect(sid).or(Err(()))?; XOUS_LOGGER_CONNECTION.store( - xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).or(Err(()))?, + cid, Ordering::Relaxed, ); log::set_logger(&XOUS_LOGGER).or(Err(()))?; diff --git a/services/app-loader/build.rs b/services/app-loader/build.rs deleted file mode 100644 index 9754b6449..000000000 --- a/services/app-loader/build.rs +++ /dev/null @@ -1,22 +0,0 @@ -use std::process::Command; - -fn main() { - // this should be changed at some point to support other platforms - let target = "riscv32imac-unknown-xous-elf"; - - // compile the spawn package - let status = Command::new("cargo").args(&["build", "--package", "spawn", "--target", target]) - .status().unwrap(); - if !status.success() { - panic!(); - } - // turn it into a stub - let status = Command::new("cargo").args(&["run", "--package", "tools", "--bin", "copy-object", "--"]) - .arg(&format!("../../target/{}/debug/spawn", target)) - .status().unwrap(); - if !status.success() { - panic!(); - } - - println!("cargo:rerun-if-changed=spawn"); -} diff --git a/services/app-loader/spawn/Cargo.toml b/services/app-loader/spawn/Cargo.toml index 5cb521483..1a13d63bf 100644 --- a/services/app-loader/spawn/Cargo.toml +++ b/services/app-loader/spawn/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] xous = "0.9.42" +log-server = { package = "xous-api-log", version = "0.1.35", features = ["nostd"] } +log = "0.4.17" diff --git a/services/app-loader/spawn/src/main.rs b/services/app-loader/spawn/src/main.rs index 76cfad5a2..f7ca6afa1 100644 --- a/services/app-loader/spawn/src/main.rs +++ b/services/app-loader/spawn/src/main.rs @@ -31,6 +31,23 @@ fn handle_panic(_arg: &core::panic::PanicInfo) -> ! { #[no_mangle] pub extern "C" fn init(server1: u32, server2: u32, server3: u32, server4: u32) -> ! { let server = xous::SID::from_u32(server1, server2, server3, server4); + + // recreate the extra sections that were cut out of the stub + let mut memory = xous::map_memory( + None, + core::num::NonZeroUsize::new(0x40000000), + 0x1000, + xous::MemoryFlags::R | xous::MemoryFlags::W + ).unwrap(); + let connection = core::sync::atomic::AtomicU32::new(0); + let slice = unsafe { core::slice::from_raw_parts(&connection as *const _ as *const u8, core::mem::size_of::()) }; + for (dest, src) in memory.as_slice_mut::().iter_mut().skip(8).zip(slice) { + *dest = *src; + } + + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); loop { if let Ok(xous::Result::MessageEnvelope(envelope)) = xous::rsyscall(xous::SysCall::ReceiveMessage(server))