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

View file

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

View file

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

View file

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

View file

@ -337,7 +337,10 @@ impl MemoryManager {
// FIXME: Un-reserve addresses if we encounter an error here
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
@ -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.

View file

@ -136,7 +136,7 @@ pub fn handle(call: SysCall) -> xous::Result {
println!("PID{} Syscall: {:?}", pid, call);
match call {
SysCall::MapPhysical(phys, virt, size, req_flags) => {
SysCall::MapMemory(phys, virt, size, req_flags) => {
let mut mm = MemoryManagerHandle::get();
// 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 {

View file

@ -32,7 +32,8 @@ pub enum Error {
ProcessNotChild = 11,
ProcessTerminated = 12,
Timeout = 13,
UnhandledSyscall = 14,
InternalError = 14,
UnhandledSyscall = 15,
}
#[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_traits::FromPrimitive;
@ -69,7 +69,7 @@ pub enum SysCall {
/// 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
/// memory size has been exceeded.
MapPhysical(
MapMemory(
*mut usize, /* phys */
*mut usize, /* virt */
usize, /* region size */
@ -87,7 +87,7 @@ pub enum SysCall {
/// or the size isn't a multiple of the page width.
/// * **BadAddress**: The address conflicts with the kernel
SetMemRegion(
PID, /* pid */
PID, /* pid */
MemoryType, /* region type */
*mut usize, /* region address */
usize, /* region size */
@ -224,7 +224,7 @@ pub enum SysCall {
#[derive(FromPrimitive)]
enum SysCallNumber {
MapPhysical = 2,
MapMemory = 2,
Yield = 3,
Suspend = 4,
ClaimInterrupt = 5,
@ -249,8 +249,8 @@ impl SysCall {
"SysCall is not the expected size"
);
match *self {
SysCall::MapPhysical(a1, a2, a3, a4) => [
SysCallNumber::MapPhysical as usize,
SysCall::MapMemory(a1, a2, a3, a4) => [
SysCallNumber::MapMemory as usize,
a1 as usize,
a2 as usize,
a3,
@ -351,7 +351,7 @@ impl SysCall {
a7: usize,
) -> core::result::Result<Self, InvalidSyscall> {
Ok(match FromPrimitive::from_usize(a0) {
Some(SysCallNumber::MapPhysical) => SysCall::MapPhysical(
Some(SysCallNumber::MapMemory) => SysCall::MapMemory(
a1 as *mut usize,
a2 as *mut usize,
a3,
@ -384,13 +384,19 @@ impl SysCall {
}
}
#[derive(Debug, PartialEq)]
pub struct MemoryRange {
pub base: *mut u8,
pub size: usize,
}
#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum Result {
ReturnResult,
Error(Error),
MemoryAddress(*mut u8),
MemoryRange(*mut u8 /* base */, usize /* size */),
MemoryRange(MemoryRange),
ResumeResult(usize, usize, usize, usize, usize, usize),
ResumeProcess,
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 {
use core::mem::MaybeUninit;
let mut ret = unsafe { MaybeUninit::uninit().assume_init() };