diff --git a/emulation/betrusted.repl b/emulation/betrusted.repl index 7e0f3051e..cc0dcefd3 100644 --- a/emulation/betrusted.repl +++ b/emulation/betrusted.repl @@ -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; diff --git a/emulation/boot.resc b/emulation/boot.resc index 8eca04e1a..0b0483081 100644 --- a/emulation/boot.resc +++ b/emulation/boot.resc @@ -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 diff --git a/examples/shell/src/main.rs b/examples/shell/src/main.rs index 239d55e73..ecdb06615 100644 --- a/examples/shell/src/main.rs +++ b/examples/shell/src/main.rs @@ -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; diff --git a/examples/shell/src/timer.rs b/examples/shell/src/timer.rs index 84b7d4679..295e58ca9 100644 --- a/examples/shell/src/timer.rs +++ b/examples/shell/src/timer.rs @@ -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, diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index b856ba76f..e892478e7 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -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. diff --git a/kernel/src/syscall.rs b/kernel/src/syscall.rs index 22e7ba43c..7b8581d7f 100644 --- a/kernel/src/syscall.rs +++ b/kernel/src/syscall.rs @@ -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 { diff --git a/xous-rs/src/definitions.rs b/xous-rs/src/definitions.rs index 836c16a6a..eabf60d66 100644 --- a/xous-rs/src/definitions.rs +++ b/xous-rs/src/definitions.rs @@ -32,7 +32,8 @@ pub enum Error { ProcessNotChild = 11, ProcessTerminated = 12, Timeout = 13, - UnhandledSyscall = 14, + InternalError = 14, + UnhandledSyscall = 15, } #[repr(C)] diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index a55925d74..341e44477 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -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 { 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, + virt: Option, + size: usize, + flags: MemoryFlags, +) -> core::result::Result { + 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() };