syscall: implement map_memory wrapper

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2020-03-13 20:33:08 +08:00
parent 5ce653882e
commit c2008e5fe8
8 changed files with 76 additions and 38 deletions

View file

@ -15,6 +15,12 @@ uartkernel: UART.LiteX_UART @ {
} }
-> cpu@1003 -> cpu@1003
uartlog: UART.LiteX_UART @ {
sysbus 0x60004000;
sysbus 0xF0004000 // shadow
}
-> cpu@1004
timer0: Timers.LiteX_Timer_32 @ { timer0: Timers.LiteX_Timer_32 @ {
sysbus 0x60003000; sysbus 0x60003000;
sysbus 0xF0003000 // shadow sysbus 0xF0003000 // shadow
@ -22,10 +28,10 @@ timer0: Timers.LiteX_Timer_32 @ {
frequency: 12000000 frequency: 12000000
-> cpu@1001 -> cpu@1001
spi: SPI.PicoRV_SPI @ { //spi: SPI.PicoRV_SPI @ {
sysbus 0x60004000; // sysbus 0x60004000;
sysbus 0xe0004000 // shadow // sysbus 0xe0004000 // shadow
} //}
rom: Memory.MappedMemory @ { rom: Memory.MappedMemory @ {
sysbus 0x00000000; sysbus 0x00000000;
@ -44,8 +50,8 @@ cpu_timer: Timers.LiteX_CPUTimer @ sysbus 0xf0000800
// IRQ #100 is Machine Timer Interrupt // IRQ #100 is Machine Timer Interrupt
-> cpu@100 -> cpu@100
flash: SPI.Micron_MT25Q @ spi //flash: SPI.Micron_MT25Q @ spi
underlyingMemory: flash_mem // underlyingMemory: flash_mem
flash_mem: Memory.MappedMemory @ { flash_mem: Memory.MappedMemory @ {
sysbus 0x20000000; sysbus 0x20000000;

View file

@ -17,6 +17,7 @@ machine LoadPlatformDescription @betrusted.repl
machine StartGdbServer 3333 true machine StartGdbServer 3333 true
showAnalyzer uartkernel showAnalyzer uartkernel
showAnalyzer uart showAnalyzer uart
showAnalyzer uartlog
# The macro `reset` gets called implicitly when running `machine Reset` # The macro `reset` gets called implicitly when running `machine Reset`
macro reset macro reset

View file

@ -27,28 +27,26 @@ fn handle_irq(irq_no: usize, arg: *mut usize) {
#[no_mangle] #[no_mangle]
fn main() { fn main() {
let uart = xous::rsyscall(xous::SysCall::MapPhysical( let uart = xous::syscall::map_memory(
0xF0001000 as *mut usize, xous::MemoryAddress::new(0xf0001000),
0 as *mut usize, None,
4096, 4096,
xous::MemoryFlags::R | xous::MemoryFlags::W, xous::MemoryFlags::R | xous::MemoryFlags::W,
)) )
.expect("couldn't map address"); .expect("couldn't map uart");
let uart = match uart { unsafe { debug::DEFAULT_UART_ADDR = uart.base as *mut usize };
xous::Result::MemoryRange(base, _size) => base as *mut usize, println!("Mapped UART @ {:08x}", uart.base as usize);
x => panic!("Unexpected result: {:?}", x),
}; xous::syscall::map_memory(
unsafe { debug::DEFAULT_UART_ADDR = uart }; xous::MemoryAddress::new(0xf0002000),
println!("Mapped UART @ {:08x}", uart as usize); None,
xous::rsyscall(xous::SysCall::MapPhysical(
0xF0002000 as *mut usize,
0xF0002000 as *mut usize,
4096, 4096,
xous::MemoryFlags::R | xous::MemoryFlags::W, xous::MemoryFlags::R | xous::MemoryFlags::W,
)) )
.map(|_| println!("!!!WARNING: managed to steal kernel's memory")) .map(|_| println!("!!!WARNING: managed to steal kernel's memory"))
.ok(); .ok();
println!("Process: map success!"); println!("Process: map success!");
debug::DEFAULT.enable_rx(); debug::DEFAULT.enable_rx();
println!("Allocating IRQ..."); println!("Allocating IRQ...");
xous::rsyscall(xous::SysCall::ClaimInterrupt( xous::rsyscall(xous::SysCall::ClaimInterrupt(
@ -69,14 +67,14 @@ fn main() {
xous::MemoryFlags::R | xous::MemoryFlags::W, xous::MemoryFlags::R | xous::MemoryFlags::W,
)) ))
.expect("couldn't increase heap"); .expect("couldn't increase heap");
if let xous::Result::MemoryRange(start, len) = heap { if let xous::Result::MemoryRange(range) = heap {
println!( println!(
"Heap goes from {:08x} - {:08x}", "Heap goes from {:08x} - {:08x}",
start as usize, range.base as usize,
start as usize + len range.base as usize + range.size
); );
use core::slice; use core::slice;
let mem_range = unsafe { slice::from_raw_parts_mut(start, len) }; let mem_range = unsafe { slice::from_raw_parts_mut(range.base, range.size) };
println!("Filling with bytes..."); println!("Filling with bytes...");
for word in mem_range.iter_mut() { for word in mem_range.iter_mut() {
*word = 42; *word = 42;

View file

@ -11,7 +11,7 @@ fn timer_tick(_irq_no: usize, _arg: *mut usize) {
pub fn init() { pub fn init() {
println!("Allocating timer..."); println!("Allocating timer...");
xous::rsyscall(xous::SysCall::MapPhysical( xous::rsyscall(xous::SysCall::MapMemory(
TIMER_BASE as *mut usize, TIMER_BASE as *mut usize,
TIMER_BASE as *mut usize, TIMER_BASE as *mut usize,
4096, 4096,

View file

@ -337,7 +337,10 @@ impl MemoryManager {
// FIXME: Un-reserve addresses if we encounter an error here // FIXME: Un-reserve addresses if we encounter an error here
mm.reserve_address(self, virt, flags)?; mm.reserve_address(self, virt, flags)?;
} }
Ok(xous::Result::MemoryRange(virt_ptr as *mut u8, size)) Ok(xous::Result::MemoryRange(xous::MemoryRange {
base: virt_ptr as *mut u8,
size,
}))
} }
/// Attempt to map the given physical address into the virtual address space /// Attempt to map the given physical address into the virtual address space
@ -394,7 +397,10 @@ impl MemoryManager {
} }
} }
Ok(xous::Result::MemoryRange(virt as *mut u8, size)) Ok(xous::Result::MemoryRange(xous::MemoryRange {
base: virt as *mut u8,
size,
}))
} }
// /// Map a range of physical addresses into the current memory space. // /// Map a range of physical addresses into the current memory space.

View file

@ -136,7 +136,7 @@ pub fn handle(call: SysCall) -> xous::Result {
println!("PID{} Syscall: {:?}", pid, call); println!("PID{} Syscall: {:?}", pid, call);
match call { match call {
SysCall::MapPhysical(phys, virt, size, req_flags) => { SysCall::MapMemory(phys, virt, size, req_flags) => {
let mut mm = MemoryManagerHandle::get(); let mut mm = MemoryManagerHandle::get();
// Don't let the address exceed the user area (unless it's PID 1) // Don't let the address exceed the user area (unless it's PID 1)
if pid != 1 && (virt as usize) != 0 && (virt as usize) >= arch::mem::USER_AREA_END { if pid != 1 && (virt as usize) != 0 && (virt as usize) >= arch::mem::USER_AREA_END {

View file

@ -32,7 +32,8 @@ pub enum Error {
ProcessNotChild = 11, ProcessNotChild = 11,
ProcessTerminated = 12, ProcessTerminated = 12,
Timeout = 13, Timeout = 13,
UnhandledSyscall = 14, InternalError = 14,
UnhandledSyscall = 15,
} }
#[repr(C)] #[repr(C)]

View file

@ -1,4 +1,4 @@
use crate::{CpuID, Error, PID}; use crate::{CpuID, Error, MemoryAddress, PID};
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
@ -69,7 +69,7 @@ pub enum SysCall {
/// or the size isn't a multiple of the page width. /// or the size isn't a multiple of the page width.
/// * **OutOfMemory**: A contiguous chunk of memory couldn't be found, or the system's /// * **OutOfMemory**: A contiguous chunk of memory couldn't be found, or the system's
/// memory size has been exceeded. /// memory size has been exceeded.
MapPhysical( MapMemory(
*mut usize, /* phys */ *mut usize, /* phys */
*mut usize, /* virt */ *mut usize, /* virt */
usize, /* region size */ usize, /* region size */
@ -87,7 +87,7 @@ pub enum SysCall {
/// or the size isn't a multiple of the page width. /// or the size isn't a multiple of the 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 */ *mut usize, /* region address */
usize, /* region size */ usize, /* region size */
@ -224,7 +224,7 @@ pub enum SysCall {
#[derive(FromPrimitive)] #[derive(FromPrimitive)]
enum SysCallNumber { enum SysCallNumber {
MapPhysical = 2, MapMemory = 2,
Yield = 3, Yield = 3,
Suspend = 4, Suspend = 4,
ClaimInterrupt = 5, ClaimInterrupt = 5,
@ -249,8 +249,8 @@ impl SysCall {
"SysCall is not the expected size" "SysCall is not the expected size"
); );
match *self { match *self {
SysCall::MapPhysical(a1, a2, a3, a4) => [ SysCall::MapMemory(a1, a2, a3, a4) => [
SysCallNumber::MapPhysical as usize, SysCallNumber::MapMemory as usize,
a1 as usize, a1 as usize,
a2 as usize, a2 as usize,
a3, a3,
@ -351,7 +351,7 @@ impl SysCall {
a7: usize, a7: usize,
) -> core::result::Result<Self, InvalidSyscall> { ) -> core::result::Result<Self, InvalidSyscall> {
Ok(match FromPrimitive::from_usize(a0) { Ok(match FromPrimitive::from_usize(a0) {
Some(SysCallNumber::MapPhysical) => SysCall::MapPhysical( Some(SysCallNumber::MapMemory) => SysCall::MapMemory(
a1 as *mut usize, a1 as *mut usize,
a2 as *mut usize, a2 as *mut usize,
a3, a3,
@ -384,13 +384,19 @@ impl SysCall {
} }
} }
#[derive(Debug, PartialEq)]
pub struct MemoryRange {
pub base: *mut u8,
pub size: usize,
}
#[repr(C)] #[repr(C)]
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Result { pub enum Result {
ReturnResult, ReturnResult,
Error(Error), Error(Error),
MemoryAddress(*mut u8), MemoryAddress(*mut u8),
MemoryRange(*mut u8 /* base */, usize /* size */), MemoryRange(MemoryRange),
ResumeResult(usize, usize, usize, usize, usize, usize), ResumeResult(usize, usize, usize, usize, usize, usize),
ResumeProcess, ResumeProcess,
UnknownResult(usize, usize, usize, usize, usize, usize, usize), UnknownResult(usize, usize, usize, usize, usize, usize, usize),
@ -429,6 +435,26 @@ extern "Rust" {
); );
} }
/// Map the given physical address to the given virtual address.
/// The `size` field must be page-aligned.
pub fn map_memory(
phys: Option<MemoryAddress>,
virt: Option<MemoryAddress>,
size: usize,
flags: MemoryFlags,
) -> core::result::Result<crate::MemoryRange, crate::Error> {
let result = rsyscall(SysCall::MapMemory(
phys.map(|x| x.get()).unwrap_or(0) as *mut usize,
virt.map(|x| x.get()).unwrap_or(0) as *mut usize,
size,
flags,
))?;
if let Result::MemoryRange(range) = result {
return Ok(range);
}
Err(crate::Error::InternalError)
}
pub fn rsyscall(call: SysCall) -> SyscallResult { pub fn rsyscall(call: SysCall) -> SyscallResult {
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
let mut ret = unsafe { MaybeUninit::uninit().assume_init() }; let mut ret = unsafe { MaybeUninit::uninit().assume_init() };