Merge pull request #388 from Foundation-Devices/syscall-virt-to-phys-pid

Add virt_to_phys_pid syscall
This commit is contained in:
bunnie 2023-05-28 15:52:57 +08:00 committed by GitHub
commit a244b204cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 0 deletions

View file

@ -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<usize, xous_kernel::Error> {
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<usize, xous_kernel::Error> {
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

View file

@ -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)

View file

@ -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<usize, Error> {
})
}
/// 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<usize, Error> {
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<MemoryRange, ()> {
let res = crate::arch::syscall(SysCall::IncreaseHeap(bytes, flags));
if let Ok(Result::MemoryRange(range)) = res