diff --git a/kernel/src/arch/arm/mem.rs b/kernel/src/arch/arm/mem.rs index 9a125cd67..4fb243a56 100644 --- a/kernel/src/arch/arm/mem.rs +++ b/kernel/src/arch/arm/mem.rs @@ -3,6 +3,8 @@ use crate::arch::arm::process::InitialProcess; use crate::mem::MemoryManager; +use crate::SystemServices; + use core::num::NonZeroU8; pub use armv7::structures::paging::{ @@ -629,6 +631,35 @@ pub fn virt_to_phys(virt: usize) -> Result { Err(xous_kernel::Error::BadAddress) } +/// Translates the virtual address from a mapping of the specific process to a physical address. +#[allow(dead_code)] +pub fn virt_to_phys_pid(pid: PID, virt: usize) -> Result { + let current_pid = SystemServices::with(|ss| { + ss.current_pid() + }); + + /// Ensures switching back to the source memory space whenever this function returns + /// in successful and error cases + struct SwitchBackGuard(PID); + impl Drop for SwitchBackGuard { + fn drop(&mut self) { + SystemServices::with(|ss| { + let p = ss.get_process(self.0).expect("current process"); + p.mapping.activate().ok(); + }); + } + } + + let _guard = SwitchBackGuard(current_pid); + + SystemServices::with(|ss| { + let target_process = ss.get_process(pid).or_else(|_| Err(xous_kernel::Error::InvalidPID))?; + target_process.mapping.activate().or_else(|_| Err(xous_kernel::Error::InvalidPID))?; + + virt_to_phys(virt) + }) +} + fn get_s_flag_from_tex_bits(tex_bits: u32) -> bool { // Reuse TEX[1] bit as an S flag since TRE is enabled and TEX[2:1] bits are customizable tex_bits & 0b010 != 0 diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 1b2024049..c08f9b14c 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -1142,6 +1142,14 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR Err(_) => Err(xous_kernel::Error::BadAddress), } } + #[cfg(feature = "v2p")] + SysCall::VirtToPhysPid(pid, vaddr) => { + let phys_addr = crate::arch::mem::virt_to_phys_pid(pid, 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 d556e3c7b..13d3f03e2 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -517,6 +517,20 @@ pub enum SysCall { usize, /* how many args are valid (BlockingScalar) or usize::MAX (MemoryMessge) */ ), + /// Returns the physical address corresponding to a virtual address for a given process, if such a mapping exists. + /// + /// ## Arguments + /// * **pid**: The PID + /// * **vaddr**: The virtual address to inspect + /// + /// ## Returns + /// Returns a Scalar1 containing the physical address + /// + /// ## Errors + /// * **BadAddress**: The mapping does not exist + #[cfg(feature = "v2p")] + VirtToPhysPid(PID /* Process ID */, 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), @@ -565,6 +579,8 @@ pub enum SysCallNumber { VirtToPhys = 39, ReturnScalar5 = 40, ReplyAndReceiveNext = 41, + #[cfg(feature = "v2p")] + VirtToPhysPid = 42, Invalid, } @@ -613,6 +629,8 @@ impl SysCallNumber { 39 => VirtToPhys, 40 => ReturnScalar5, 41 => ReplyAndReceiveNext, + #[cfg(feature = "v2p")] + 42 => VirtToPhysPid, _ => Invalid, } } @@ -1002,6 +1020,10 @@ impl SysCall { *arg5, 0, ], + #[cfg(feature = "v2p")] + SysCall::VirtToPhysPid(pid, vaddr) => { + [SysCallNumber::VirtToPhysPid as usize, pid.get() as usize, *vaddr, 0, 0, 0, 0, 0] + } SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7) => [ SysCallNumber::Invalid as usize, *a1, @@ -1171,6 +1193,8 @@ impl SysCall { SysCallNumber::AdjustProcessLimit => SysCall::AdjustProcessLimit(a1, a2, a3), #[cfg(feature = "v2p")] SysCallNumber::VirtToPhys => SysCall::VirtToPhys(a1 as _), + #[cfg(feature = "v2p")] + SysCallNumber::VirtToPhysPid => SysCall::VirtToPhysPid(pid_from_usize(a1)?, a2 as _), SysCallNumber::ReturnScalar5 => { SysCall::ReturnScalar5(MessageSender::from_usize(a1), a2, a3, a4, a5, a6) } @@ -2055,6 +2079,18 @@ pub fn virt_to_phys(va: usize) -> core::result::Result { }) } +/// Translate a virtual address to a physical address for a given process +#[cfg(feature = "v2p")] +pub fn virt_to_phys_pid(pid: PID, va: usize) -> core::result::Result { + rsyscall(SysCall::VirtToPhysPid(pid, va)).and_then(|result| { + if let Result::Scalar1(pa) = result { + Ok(pa) + } else { + Err(Error::BadAddress) + } + }) +} + pub fn increase_heap(bytes: usize, flags: MemoryFlags) -> core::result::Result { let res = crate::arch::syscall(SysCall::IncreaseHeap(bytes, flags)); if let Ok(Result::MemoryRange(range)) = res