From 80df40d8faa647e504d50a32034af6256e120fcc Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 7 Feb 2022 13:37:23 +0800 Subject: [PATCH] xous-rs: implement UpdateMemoryFlags syscall Implement this syscall and create a wrapper function `update_memory_flags()`. Signed-off-by: Sean Cross --- xous-rs/Cargo.toml | 2 +- xous-rs/src/syscall.rs | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/xous-rs/Cargo.toml b/xous-rs/Cargo.toml index db57dcaee..51d2f3127 100644 --- a/xous-rs/Cargo.toml +++ b/xous-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xous" -version = "0.8.3" +version = "0.9.7" authors = ["Sean Cross "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 72ecb9869..f62ad34d7 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -1,7 +1,7 @@ use crate::{ - pid_from_usize, CpuID, Error, MemoryAddress, MemoryFlags, MemoryMessage, - MemoryRange, MemorySize, MemoryType, Message, MessageEnvelope, MessageSender, ProcessArgs, - ProcessInit, Result, ScalarMessage, SysCallResult, ThreadInit, CID, PID, SID, TID, + pid_from_usize, CpuID, Error, MemoryAddress, MemoryFlags, MemoryMessage, MemoryRange, + MemorySize, MemoryType, Message, MessageEnvelope, MessageSender, ProcessArgs, ProcessInit, + Result, ScalarMessage, SysCallResult, ThreadInit, CID, PID, SID, TID, }; use core::convert::{TryFrom, TryInto}; /* https://github.com/betrusted-io/xous-core/issues/90 @@ -122,10 +122,9 @@ pub enum SysCall { /// * **MemoryInUse**: The given PID has already been started, and it is not /// legal to modify memory flags anymore. UpdateMemoryFlags( - MemoryAddress, /* virt */ - MemorySize, /* number of bytes */ - MemoryFlags, /* new flags */ - Option, /* if present, indicates the process to modify */ + MemoryRange, /* range of memory to update flags for */ + MemoryFlags, /* new flags */ + Option, /* if present, indicates the process to modify */ ), /// Pauses execution of the current thread and returns execution to the parent @@ -658,10 +657,10 @@ impl SysCall { ], SysCall::UpdateMemoryFlags(a1, a2, a3) => [ SysCallNumber::UpdateMemoryFlags as usize, - a1.get(), - *a2 as usize, - crate::get_bits(a3), - 0, + a1.as_mut_ptr() as usize, + a1.len(), + crate::get_bits(a2), + a3.map(|m| m.get() as usize).unwrap_or(0), 0, 0, 0, @@ -910,9 +909,9 @@ impl SysCall { ), SysCallNumber::DecreaseHeap => SysCall::DecreaseHeap(a1 as usize), SysCallNumber::UpdateMemoryFlags => SysCall::UpdateMemoryFlags( - MemoryAddress::new(a1).ok_or(Error::InvalidSyscall)?, - a2 as usize, + unsafe { MemoryRange::new(a1, a2) }?, crate::from_bits(a3).ok_or(Error::InvalidSyscall)?, + PID::new(a4 as _), ), SysCallNumber::SetMemRegion => SysCall::SetMemRegion( pid_from_usize(a1)?, @@ -1160,6 +1159,22 @@ pub fn unmap_memory(range: MemoryRange) -> core::result::Result<(), Error> { } } +/// Update the permissions on the given memory range. Note that permissions may +/// only be stripped here -- they may never be added. +pub fn update_memory_flags( + range: MemoryRange, + flags: MemoryFlags, +) -> core::result::Result { + let result = rsyscall(SysCall::UpdateMemoryFlags(range, flags, None))?; + if let Result::Ok = result { + Ok(Result::Ok) + } else if let Result::Error(e) = result { + Err(e) + } else { + Err(Error::InternalError) + } +} + /// Map the given physical address to the given virtual address. /// The `size` field must be page-aligned. pub fn return_memory(sender: MessageSender, mem: MemoryRange) -> core::result::Result<(), Error> {