xous: remove all instances of raw pointers

We now use MemoryAddress and MemorySize wrappers where applicable.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-06-03 15:40:15 +08:00
parent 6ad48ab062
commit 322e2e7804
2 changed files with 72 additions and 53 deletions

View file

@ -287,7 +287,7 @@ impl From<usize> for MemoryType {
pub enum Result { pub enum Result {
Ok, Ok,
Error(Error), Error(Error),
MemoryAddress(*mut u8), MemoryAddress(MemoryAddress),
MemoryRange(MemoryRange), MemoryRange(MemoryRange),
ReadyContexts( ReadyContexts(
usize, /* count */ usize, /* count */
@ -312,7 +312,10 @@ impl Result {
match src[0] { match src[0] {
0 => Result::Ok, 0 => Result::Ok,
1 => Result::Error(Error::from_usize(src[1])), 1 => Result::Error(Error::from_usize(src[1])),
2 => Result::MemoryAddress(src[1] as *mut u8), 2 => match MemoryAddress::new(src[1]) {
None => Result::Error(Error::InternalError),
Some(s) => Result::MemoryAddress(s),
}
3 => { 3 => {
let addr = match MemoryAddress::new(src[1]) { let addr = match MemoryAddress::new(src[1]) {
None => return Result::Error(Error::InternalError), None => return Result::Error(Error::InternalError),

View file

@ -22,17 +22,20 @@ pub enum SysCall {
/// * **OutOfMemory**: A contiguous chunk of memory couldn't be found, or /// * **OutOfMemory**: A contiguous chunk of memory couldn't be found, or
/// the system's memory size has been exceeded. /// the system's memory size has been exceeded.
MapMemory( MapMemory(
*mut usize, /* phys */ Option<MemoryAddress>, /* phys */
*mut usize, /* virt */ Option<MemoryAddress>, /* virt */
usize, /* region size */ MemorySize, /* region size */
MemoryFlags, /* flags */ MemoryFlags, /* flags */
), ),
/// Release the memory back to the operating system. /// Release the memory back to the operating system.
/// ///
/// # Errors /// # Errors
/// ///
UnmapMemory(*mut usize /* virt */, usize /* region size */), UnmapMemory(
MemoryAddress, /* virt */
MemorySize, /* region size */
),
/// Sets the offset and size of a given memory region. This call may only /// Sets the offset and size of a given memory region. This call may only
/// be made by processes that have not yet started, or processes that have a /// be made by processes that have not yet started, or processes that have a
@ -47,10 +50,10 @@ pub enum SysCall {
/// page width. /// page width.
/// * **BadAddress**: The address conflicts with the kernel /// * **BadAddress**: The address conflicts with the kernel
SetMemRegion( SetMemRegion(
PID, /* pid */ PID, /* pid */
MemoryType, /* region type */ MemoryType, /* region type */
*mut usize, /* region address */ MemoryAddress, /* region address */
usize, /* region size */ usize, /* region size */
), ),
/// Add the given number of bytes to the heap. The number of bytes must be /// Add the given number of bytes to the heap. The number of bytes must be
@ -99,9 +102,9 @@ pub enum SysCall {
/// * **MemoryInUse**: The given PID has already been started, and it is not /// * **MemoryInUse**: The given PID has already been started, and it is not
/// legal to modify memory flags anymore. /// legal to modify memory flags anymore.
UpdateMemoryFlags( UpdateMemoryFlags(
*mut usize, /* virt */ MemoryAddress, /* virt */
usize, /* number of pages */ usize, /* number of pages */
MemoryFlags, /* new flags */ MemoryFlags, /* new flags */
), ),
/// Pauses execution of the current thread and returns execution to the parent /// Pauses execution of the current thread and returns execution to the parent
@ -136,9 +139,9 @@ pub enum SysCall {
/// system /// system
/// * **InterruptInUse**: The specified interrupt has already been claimed /// * **InterruptInUse**: The specified interrupt has already been claimed
ClaimInterrupt( ClaimInterrupt(
usize, /* IRQ number */ usize, /* IRQ number */
*mut usize, /* function pointer */ MemoryAddress, /* function pointer */
*mut usize, /* argument */ Option<MemoryAddress>, /* argument */
), ),
/// Returns the interrupt back to the operating system and masks it again. /// Returns the interrupt back to the operating system and masks it again.
@ -236,12 +239,13 @@ pub enum SysCall {
/// Spawn a new thread /// Spawn a new thread
SpawnThread( SpawnThread(
*mut usize, /* entrypoint */ MemoryAddress, /* entrypoint */
*mut usize, /* stack pointer */ MemoryAddress, /* stack pointer */
*mut usize, /* argument */ Option<MemoryAddress>, /* argument */
), ),
/// This syscall does not exist /// This syscall does not exist. It captures all possible
/// arguments so detailed analysis can be performed.
Invalid(usize, usize, usize, usize, usize, usize, usize), Invalid(usize, usize, usize, usize, usize, usize, usize),
} }
@ -270,6 +274,8 @@ enum SysCallNumber {
} }
impl SysCall { impl SysCall {
/// Convert the SysCall into an array of eight `usize` elements,
/// suitable for passing to the kernel.
pub fn as_args(&self) -> [usize; 8] { pub fn as_args(&self) -> [usize; 8] {
use core::mem; use core::mem;
assert!( assert!(
@ -279,9 +285,9 @@ impl SysCall {
match self { match self {
SysCall::MapMemory(a1, a2, a3, a4) => [ SysCall::MapMemory(a1, a2, a3, a4) => [
SysCallNumber::MapMemory as usize, SysCallNumber::MapMemory as usize,
*a1 as usize, a1.map(|x| x.get()).unwrap_or_default(),
*a2 as usize, a2.map(|x| x.get()).unwrap_or_default(),
*a3, a3.get(),
a4.bits(), a4.bits(),
0, 0,
0, 0,
@ -289,8 +295,8 @@ impl SysCall {
], ],
SysCall::UnmapMemory(a1, a2) => [ SysCall::UnmapMemory(a1, a2) => [
SysCallNumber::UnmapMemory as usize, SysCallNumber::UnmapMemory as usize,
*a1 as usize, a1.get(),
*a2, a2.get(),
0, 0,
0, 0,
0, 0,
@ -322,8 +328,8 @@ impl SysCall {
SysCall::ClaimInterrupt(a1, a2, a3) => [ SysCall::ClaimInterrupt(a1, a2, a3) => [
SysCallNumber::ClaimInterrupt as usize, SysCallNumber::ClaimInterrupt as usize,
*a1, *a1,
*a2 as usize, a2.get(),
*a3 as usize, a3.map(|x| x.get()).unwrap_or_default(),
0, 0,
0, 0,
0, 0,
@ -374,7 +380,7 @@ impl SysCall {
], ],
SysCall::UpdateMemoryFlags(a1, a2, a3) => [ SysCall::UpdateMemoryFlags(a1, a2, a3) => [
SysCallNumber::UpdateMemoryFlags as usize, SysCallNumber::UpdateMemoryFlags as usize,
*a1 as usize, a1.get(),
*a2 as usize, *a2 as usize,
a3.bits(), a3.bits(),
0, 0,
@ -386,7 +392,7 @@ impl SysCall {
SysCallNumber::SetMemRegion as usize, SysCallNumber::SetMemRegion as usize,
*a1 as usize, *a1 as usize,
*a2 as usize, *a2 as usize,
*a3 as usize, a3.get(),
*a4, *a4,
0, 0,
0, 0,
@ -460,9 +466,9 @@ impl SysCall {
], ],
SysCall::SpawnThread(a1, a2, a3) => [ SysCall::SpawnThread(a1, a2, a3) => [
SysCallNumber::SpawnThread as usize, SysCallNumber::SpawnThread as usize,
*a1 as usize, a1.get(),
*a2 as usize, a2.get(),
*a3 as usize, a3.map(|x| x.get()).unwrap_or_default(),
0, 0,
0, 0,
0, 0,
@ -494,19 +500,24 @@ impl SysCall {
) -> core::result::Result<Self, Error> { ) -> core::result::Result<Self, Error> {
Ok(match FromPrimitive::from_usize(a0) { Ok(match FromPrimitive::from_usize(a0) {
Some(SysCallNumber::MapMemory) => SysCall::MapMemory( Some(SysCallNumber::MapMemory) => SysCall::MapMemory(
a1 as *mut usize, MemoryAddress::new(a1),
a2 as *mut usize, MemoryAddress::new(a2),
a3, MemoryAddress::new(a3).ok_or(Error::InvalidSyscall)?,
MemoryFlags::from_bits(a4).ok_or(Error::InvalidSyscall)?, MemoryFlags::from_bits(a4).ok_or(Error::InvalidSyscall)?,
), ),
Some(SysCallNumber::UnmapMemory) => SysCall::UnmapMemory(a1 as *mut usize, a2), Some(SysCallNumber::UnmapMemory) => SysCall::UnmapMemory(
MemoryAddress::new(a1).ok_or(Error::InvalidSyscall)?,
MemorySize::new(a2).ok_or(Error::InvalidSyscall)?,
),
Some(SysCallNumber::Yield) => SysCall::Yield, Some(SysCallNumber::Yield) => SysCall::Yield,
Some(SysCallNumber::WaitEvent) => SysCall::WaitEvent, Some(SysCallNumber::WaitEvent) => SysCall::WaitEvent,
Some(SysCallNumber::ReceiveMessage) => SysCall::ReceiveMessage((a1, a2, a3, a4)), Some(SysCallNumber::ReceiveMessage) => SysCall::ReceiveMessage((a1, a2, a3, a4)),
Some(SysCallNumber::ReturnToParentI) => SysCall::ReturnToParentI(a1 as PID, a2), Some(SysCallNumber::ReturnToParentI) => SysCall::ReturnToParentI(a1 as PID, a2),
Some(SysCallNumber::ClaimInterrupt) => { Some(SysCallNumber::ClaimInterrupt) => SysCall::ClaimInterrupt(
SysCall::ClaimInterrupt(a1, a2 as *mut usize, a3 as *mut usize) a1,
} MemoryAddress::new(a2).ok_or(Error::InvalidSyscall)?,
MemoryAddress::new(a3),
),
Some(SysCallNumber::FreeInterrupt) => SysCall::FreeInterrupt(a1), Some(SysCallNumber::FreeInterrupt) => SysCall::FreeInterrupt(a1),
Some(SysCallNumber::SwitchTo) => SysCall::SwitchTo(a1 as PID, a2 as usize), Some(SysCallNumber::SwitchTo) => SysCall::SwitchTo(a1 as PID, a2 as usize),
Some(SysCallNumber::ReadyContexts) => SysCall::ReadyContexts(a1 as u8), Some(SysCallNumber::ReadyContexts) => SysCall::ReadyContexts(a1 as u8),
@ -516,13 +527,16 @@ impl SysCall {
), ),
Some(SysCallNumber::DecreaseHeap) => SysCall::DecreaseHeap(a1 as usize), Some(SysCallNumber::DecreaseHeap) => SysCall::DecreaseHeap(a1 as usize),
Some(SysCallNumber::UpdateMemoryFlags) => SysCall::UpdateMemoryFlags( Some(SysCallNumber::UpdateMemoryFlags) => SysCall::UpdateMemoryFlags(
a1 as *mut usize, MemoryAddress::new(a1).ok_or(Error::InvalidSyscall)?,
a2 as usize, a2 as usize,
MemoryFlags::from_bits(a3).ok_or(Error::InvalidSyscall)?, MemoryFlags::from_bits(a3).ok_or(Error::InvalidSyscall)?,
), ),
Some(SysCallNumber::SetMemRegion) => { Some(SysCallNumber::SetMemRegion) => SysCall::SetMemRegion(
SysCall::SetMemRegion(a1 as PID, MemoryType::from(a2), a3 as *mut usize, a4) a1 as PID,
} MemoryType::from(a2),
MemoryAddress::new(a3).ok_or(Error::InvalidSyscall)?,
a4,
),
Some(SysCallNumber::CreateServer) => SysCall::CreateServer(a1), Some(SysCallNumber::CreateServer) => SysCall::CreateServer(a1),
Some(SysCallNumber::Connect) => SysCall::Connect((a1, a2, a3, a4)), Some(SysCallNumber::Connect) => SysCall::Connect((a1, a2, a3, a4)),
Some(SysCallNumber::SendMessage) => match a2 { Some(SysCallNumber::SendMessage) => match a2 {
@ -566,9 +580,11 @@ impl SysCall {
_ => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), _ => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7),
}, },
Some(SysCallNumber::ReturnMemory) => SysCall::ReturnMemory(a1, a2, a3), Some(SysCallNumber::ReturnMemory) => SysCall::ReturnMemory(a1, a2, a3),
Some(SysCallNumber::SpawnThread) => { Some(SysCallNumber::SpawnThread) => SysCall::SpawnThread(
SysCall::SpawnThread(a1 as *mut usize, a2 as *mut usize, a3 as *mut usize) MemoryAddress::new(a1).ok_or(Error::InvalidSyscall)?,
} MemoryAddress::new(a2).ok_or(Error::InvalidSyscall)?,
MemoryAddress::new(a3),
),
Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), Some(SysCallNumber::Invalid) => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7),
None => return Err(Error::InvalidSyscall), None => return Err(Error::InvalidSyscall),
}) })
@ -609,9 +625,9 @@ pub fn map_memory(
flags: MemoryFlags, flags: MemoryFlags,
) -> core::result::Result<MemoryRange, Error> { ) -> core::result::Result<MemoryRange, Error> {
let result = rsyscall(SysCall::MapMemory( let result = rsyscall(SysCall::MapMemory(
phys.map(|x| x.get()).unwrap_or(0) as *mut usize, phys,
virt.map(|x| x.get()).unwrap_or(0) as *mut usize, virt,
size, MemorySize::new(size).ok_or(Error::InvalidSyscall)?,
flags, flags,
))?; ))?;
if let Result::MemoryRange(range) = result { if let Result::MemoryRange(range) = result {
@ -626,7 +642,7 @@ pub fn map_memory(
/// Map the given physical address to the given virtual address. /// Map the given physical address to the given virtual address.
/// The `size` field must be page-aligned. /// The `size` field must be page-aligned.
pub fn unmap_memory(virt: MemoryAddress, size: MemorySize) -> core::result::Result<(), Error> { pub fn unmap_memory(virt: MemoryAddress, size: MemorySize) -> core::result::Result<(), Error> {
let result = rsyscall(SysCall::UnmapMemory(virt.get() as *mut usize, size.get()))?; let result = rsyscall(SysCall::UnmapMemory(virt, size))?;
if let Result::Ok = result { if let Result::Ok = result {
Ok(()) Ok(())
} else if let Result::Error(e) = result { } else if let Result::Error(e) = result {
@ -661,8 +677,8 @@ pub fn claim_interrupt(
) -> core::result::Result<(), Error> { ) -> core::result::Result<(), Error> {
let result = rsyscall(SysCall::ClaimInterrupt( let result = rsyscall(SysCall::ClaimInterrupt(
irq_no, irq_no,
callback as *mut usize, MemoryAddress::new(callback as *mut usize as usize).ok_or(Error::InvalidSyscall)?,
arg as *mut usize, MemoryAddress::new(arg as *mut usize as usize),
))?; ))?;
if let Result::Ok = result { if let Result::Ok = result {
Ok(()) Ok(())