From 6d9848c239bdda46a5d89cc9c5f65d533486ab55 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 14 Sep 2022 18:09:13 +0800 Subject: [PATCH 1/3] add virt_to_phys syscall this will help speed up performance profiling as we can resolve the physical address of the performance buffer allocated in RAM and use a USB command to read it out, instead of streaming it over serial. --- kernel/src/syscall.rs | 7 +++++++ xous-rs/src/syscall.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 675001e2d..85046bca1 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -936,6 +936,13 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR }), _ => Err(xous_kernel::Error::InvalidLimit), }, + SysCall::VirtToPhys(vaddr) => { + let phys_addr = crate::arch::mem::virt_to_phys(vaddr as usize); + match phys_addr { + Ok(pa) => Ok(xous_kernel::Result::Scalar1(pa)), + Err(_) => Err(xous_kernel::Error::BadAddress), + } + } /* https://github.com/betrusted-io/xous-core/issues/90 SysCall::SetExceptionHandler(pc, sp) => SystemServices::with_mut(|ss| { ss.set_exception_handler(pid, pc, sp) diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 364d3ce00..299a30a66 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -457,6 +457,20 @@ pub enum SysCall { usize, /* proposed new limit */ ), + /// Returns the physical address corresponding to a virtual address, if such a mapping exists. + /// + /// ## Arguments + /// * **vaddr**: The virtual address to inspect + /// + /// ## Returns + /// Returns a Scalar1 containing the physical address + /// + /// ## Errors + /// * **BadAddress**: The mapping does not exist + VirtToPhys( + usize, /* virtual address */ + ), + /// This syscall does not exist. It captures all possible /// arguments so detailed analysis can be performed. Invalid(usize, usize, usize, usize, usize, usize, usize), @@ -501,6 +515,7 @@ pub enum SysCallNumber { JoinThread = 36, SetExceptionHandler = 37, AdjustProcessLimit = 38, + VirtToPhys = 39, Invalid, } @@ -545,6 +560,7 @@ impl SysCallNumber { 36 => JoinThread, 37 => SetExceptionHandler, 38 => AdjustProcessLimit, + 39 => VirtToPhys, _ => Invalid, } } @@ -909,6 +925,16 @@ impl SysCall { 0, 0, ], + SysCall::VirtToPhys(vaddr) => [ + SysCallNumber::VirtToPhys as usize, + *vaddr, + 0, + 0, + 0, + 0, + 0, + 0, + ], SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7) => [ SysCallNumber::Invalid as usize, *a1, @@ -1073,6 +1099,7 @@ impl SysCall { SysCallNumber::JoinThread => SysCall::JoinThread(a1 as _), SysCallNumber::SetExceptionHandler => SysCall::SetExceptionHandler(a1 as _, a2 as _), SysCallNumber::AdjustProcessLimit => SysCall::AdjustProcessLimit(a1, a2, a3), + SysCallNumber::VirtToPhys => SysCall::VirtToPhys(a1 as _), SysCallNumber::Invalid => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), }) } @@ -1839,6 +1866,17 @@ pub fn set_exception_handler( } */ +/// Translate a virtual address to a physical address +pub fn virt_to_phys(va: usize) -> core::result::Result { + rsyscall(SysCall::VirtToPhys(va)).and_then(|result| { + if let Result::Scalar1(pa) = result { + Ok(pa) + } else { + Err(Error::BadAddress) + } + }) +} + /// Perform a raw syscall and return the result. This will transform /// `xous::Result::Error(e)` into an `Err(e)`. pub fn rsyscall(call: SysCall) -> SysCallResult { From ea37ce2ea172355341829ce4c02f54adb3b555fe Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 15 Sep 2022 14:06:07 +0800 Subject: [PATCH 2/3] add feature gate on v2p per xobs' request --- kernel/Cargo.toml | 1 + kernel/src/syscall.rs | 1 + xous-rs/Cargo.toml | 2 ++ xous-rs/src/syscall.rs | 6 ++++++ xtask/src/main.rs | 42 ++++++++++++++++++++++++------------------ 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 564b4b2b3..3f637cf50 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -35,6 +35,7 @@ gdbserver = ["gdbstub", "gdbstub_arch"] print-panics = [] report-memory = ["stats_alloc"] wrap-print = [] +v2p = [] # default = ["print-panics", "debug-print", "wrap-print"] default = ["print-panics", "gdbserver"] diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 85046bca1..2288da465 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -936,6 +936,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR }), _ => Err(xous_kernel::Error::InvalidLimit), }, + #[cfg(feature="v2p")] SysCall::VirtToPhys(vaddr) => { let phys_addr = crate::arch::mem::virt_to_phys(vaddr as usize); match phys_addr { diff --git a/xous-rs/Cargo.toml b/xous-rs/Cargo.toml index 679cfe0e7..856b513f8 100644 --- a/xous-rs/Cargo.toml +++ b/xous-rs/Cargo.toml @@ -13,6 +13,8 @@ core = { version = '1.0.0', optional = true, package = 'rustc-std-workspace-core compiler_builtins = { version = '0.1.0', optional = true } [features] +v2p = [] +default = [] # If this is set, then the "Drop" feature of MemoryMessage structs # will not be implemented. This should only be set by the kernel. diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 299a30a66..492211913 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -467,6 +467,7 @@ pub enum SysCall { /// /// ## Errors /// * **BadAddress**: The mapping does not exist + #[cfg(feature="v2p")] VirtToPhys( usize, /* virtual address */ ), @@ -515,6 +516,7 @@ pub enum SysCallNumber { JoinThread = 36, SetExceptionHandler = 37, AdjustProcessLimit = 38, + #[cfg(feature="v2p")] VirtToPhys = 39, Invalid, } @@ -560,6 +562,7 @@ impl SysCallNumber { 36 => JoinThread, 37 => SetExceptionHandler, 38 => AdjustProcessLimit, + #[cfg(feature="v2p")] 39 => VirtToPhys, _ => Invalid, } @@ -925,6 +928,7 @@ impl SysCall { 0, 0, ], + #[cfg(feature="v2p")] SysCall::VirtToPhys(vaddr) => [ SysCallNumber::VirtToPhys as usize, *vaddr, @@ -1099,6 +1103,7 @@ impl SysCall { SysCallNumber::JoinThread => SysCall::JoinThread(a1 as _), SysCallNumber::SetExceptionHandler => SysCall::SetExceptionHandler(a1 as _, a2 as _), SysCallNumber::AdjustProcessLimit => SysCall::AdjustProcessLimit(a1, a2, a3), + #[cfg(feature="v2p")] SysCallNumber::VirtToPhys => SysCall::VirtToPhys(a1 as _), SysCallNumber::Invalid => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), }) @@ -1867,6 +1872,7 @@ pub fn set_exception_handler( */ /// Translate a virtual address to a physical address +#[cfg(feature="v2p")] pub fn virt_to_phys(va: usize) -> core::result::Result { rsyscall(SysCall::VirtToPhys(va)).and_then(|result| { if let Result::Scalar1(pa) = result { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 1af13c329..20b6cd5f5 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -311,7 +311,7 @@ fn main() -> Result<(), Box> { None, Some(&["--features", "tts", "--features", "braille"]), &[&tts_exec_string], - None, + None, None, )?; let mut locale_revert = OpenOptions::new() .read(true) @@ -345,7 +345,7 @@ fn main() -> Result<(), Box> { None, None, None, - extra_packages.as_slice(), None)?; + extra_packages.as_slice(), None, None)?; } } Some("libstd-test") => { @@ -385,6 +385,7 @@ fn main() -> Result<(), Box> { None, &[], Some(&["--features", "renode-bypass"]), + None, )? //renode_image(false, &pkgs, extra_packages.as_slice(), //None, Some(&["--features", "renode-bypass"]))?; @@ -535,7 +536,7 @@ fn main() -> Result<(), Box> { kkey, None, &[], - None, + None, None, )? } Some("perf-image") => { @@ -554,10 +555,10 @@ fn main() -> Result<(), Box> { lkey, kkey, Some(&[ - "--features", "perfcounter" + "--features", "perfcounter", ]), &[], - None, + None, None, )? } Some("utra") => { @@ -584,6 +585,9 @@ fn main() -> Result<(), Box> { std::env::set_var("XOUS_SVD_FILE", path.canonicalize().unwrap()); println!("XOUS_SVD_FILE: {}", path.canonicalize().unwrap().display()); } + let generated_path = std::path::Path::new("utralib/src/generated.rs"); + // blank the contents so that the build command runs + std::fs::write(generated_path, "")?; let status = Command::new(cargo()) .current_dir(project_root()) .args(&[ @@ -615,7 +619,7 @@ fn main() -> Result<(), Box> { "--features", "ditherpunk", ]), &[], - None, + None, None, )? } Some("hw-image") => { @@ -639,7 +643,7 @@ fn main() -> Result<(), Box> { kkey, None, &[], - None, + None, None, )? } Some("gfx-dev") => run( @@ -656,7 +660,7 @@ fn main() -> Result<(), Box> { kkey, None, &[], - None, + None, None, )?, Some("pddb-hosted") => run(false, &pddb_dev_pkgs, None, false)?, Some("benchmark") => build_hw_image( @@ -667,7 +671,7 @@ fn main() -> Result<(), Box> { kkey, None, &[], - None, + None, None, )?, Some("minimal") => build_hw_image( false, @@ -677,7 +681,7 @@ fn main() -> Result<(), Box> { kkey, None, &[], - None, + None, None, )?, Some("cbtest") => build_hw_image( false, @@ -687,7 +691,7 @@ fn main() -> Result<(), Box> { kkey, None, &[], - None, + None, None, )?, Some("trng-test") => { generate_app_menus(&Vec::::new()); @@ -699,7 +703,7 @@ fn main() -> Result<(), Box> { kkey, Some(&["--features", "urandomtest"]), &[], - None, + None, None, )? } Some("ro-test") => { @@ -712,7 +716,7 @@ fn main() -> Result<(), Box> { kkey, Some(&["--features", "ringosctest"]), &[], - None, + None, None, )? } Some("av-test") => { @@ -725,7 +729,7 @@ fn main() -> Result<(), Box> { kkey, Some(&["--features", "avalanchetest"]), &[], - None, + None, None, )? } Some("sr-test") => build_hw_image( @@ -736,7 +740,7 @@ fn main() -> Result<(), Box> { kkey, None, &[], - None, + None, None, )?, Some("burn-kernel") => update_usb(true, false, false, false)?, Some("burn-loader") => update_usb(false, true, false, false)?, @@ -952,6 +956,7 @@ fn build_hw_image( extra_args: Option<&[&str]>, extra_packages: &[&str], loader_features: Option<&[&str]>, + kernel_features: Option<&[&str]>, ) -> Result<(), DynError> { let svd_file = match svd { Some(s) => s, @@ -972,7 +977,7 @@ fn build_hw_image( let loaderkey_file = lkey.unwrap_or_else(|| "devkey/dev.key".into()); let kernelkey_file = kkey.unwrap_or_else(|| "devkey/dev.key".into()); - let kernel = build_kernel(debug)?; + let kernel = build_kernel(debug, kernel_features)?; let mut init = vec![]; let base_path = build( packages, @@ -1170,6 +1175,7 @@ fn renode_image( xous_features, extra_packages, loader_features, + None, ) } @@ -1234,8 +1240,8 @@ fn run( Ok(()) } -fn build_kernel(debug: bool) -> Result { - let mut path = build(&["kernel"], debug, Some(KERNEL_TARGET), None, None, None)?; +fn build_kernel(debug: bool, features: Option<&[&str]>) -> Result { + let mut path = build(&["kernel"], debug, Some(KERNEL_TARGET), None, None, features)?; path.push("kernel"); Ok(path) } From 57673052bf86e43d1d7c5a3e647090f0bbcc0162 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 15 Sep 2022 19:17:00 +0800 Subject: [PATCH 3/3] pass the v2p dependency onto xous-rs when it's activated in the kernel --- kernel/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 3f637cf50..b7d99def1 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -35,7 +35,7 @@ gdbserver = ["gdbstub", "gdbstub_arch"] print-panics = [] report-memory = ["stats_alloc"] wrap-print = [] -v2p = [] +v2p = ["xous-kernel/v2p"] # default = ["print-panics", "debug-print", "wrap-print"] default = ["print-panics", "gdbserver"]