From 6d9848c239bdda46a5d89cc9c5f65d533486ab55 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 14 Sep 2022 18:09:13 +0800 Subject: [PATCH] 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 {