From 10ecacd136f3cf0270e87cf1d1c2e79076d6f4b9 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 9 Aug 2020 23:08:54 +0800 Subject: [PATCH] kernel: native: create process for initial processes These all need to bootstrap themselves. Signed-off-by: Sean Cross --- emulation/boot.resc | 5 ++--- kernel/src/arch/riscv/process.rs | 17 +++++++++++------ kernel/src/main.rs | 24 ++++++++++++------------ kernel/src/mem.rs | 4 ++-- kernel/src/services.rs | 8 +++++--- tools/src/elf.rs | 8 ++++---- xous-rs/src/arch/native.rs | 8 ++++---- xous-rs/src/syscall.rs | 26 ++++++++++++-------------- 8 files changed, 52 insertions(+), 48 deletions(-) diff --git a/emulation/boot.resc b/emulation/boot.resc index 8f25c13e5..4cd313764 100644 --- a/emulation/boot.resc +++ b/emulation/boot.resc @@ -27,9 +27,8 @@ logLevel -1 kbd # The macro `reset` gets called implicitly when running `machine Reset` macro reset """ - # sysbus LoadELF @../target/riscv32imac-unknown-none-elf/release/loader - sysbus LoadELF @../target/riscv32imac-unknown-none-elf/debug/loader - sysbus LoadBinary @../target/args.bin 0x40800000 + sysbus LoadELF @../loader/target/riscv32imac-unknown-none-elf/debug/loader + sysbus LoadBinary @../tools/args.bin 0x40800000 # Set $a0 to point at the args binary cpu SetRegisterUnsafe 10 0x40800000 # cpu PC 0x20500000 diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index 2edd1c512..d7a5a3a93 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -1,7 +1,7 @@ use core::mem; static mut PROCESS: *mut ProcessImpl = 0xff80_1000 as *mut ProcessImpl; pub const MAX_THREAD: TID = 31; -pub const INITIAL_TID: TID = 1; +pub const INITIAL_TID: TID = 2; use crate::arch::mem::PAGE_SIZE; use crate::services::ProcessInner; use xous::{ProcessInit, ThreadInit, PID, TID}; @@ -29,10 +29,12 @@ struct ProcessImpl { /// This enables the kernel to keep track of threads in the /// target process, and know which threads are ready to /// receive messages. - threads: [Thread; MAX_THREAD + 1], + threads: [Thread; MAX_THREAD], /// The currently-active thread for this process current_thread: TID, + + _padding: [u32; 14], } /// Singleton process table. Each process in the system gets allocated from this table. @@ -204,10 +206,13 @@ impl Process { pub fn init(&mut self, entrypoint: usize, stack: usize, thread: usize) { let mut process = unsafe { &mut *PROCESS }; assert!( - mem::size_of::() == PAGE_SIZE, - "Process size is {}, not PAGE_SIZE ({})", - mem::size_of::(), - PAGE_SIZE + mem::size_of::() == PAGE_SIZE, + "Process size is {}, not PAGE_SIZE ({}) (Thread size: {}, array: {}, Inner: {})", + mem::size_of::(), + PAGE_SIZE, + mem::size_of::(), + mem::size_of::<[Thread; MAX_THREAD + 1]>(), + mem::size_of::(), ); assert!( thread + 1 < process.threads.len(), diff --git a/kernel/src/main.rs b/kernel/src/main.rs index fdcd14e4f..da2618ee8 100644 --- a/kernel/src/main.rs +++ b/kernel/src/main.rs @@ -62,18 +62,18 @@ pub extern "C" fn init(arg_offset: *const u32, init_offset: *const u32, rpt_offs #[cfg(feature = "debug-print")] { // Map the serial port so println!() works as expected. - use mem::MemoryManagerHandle; - let mut memory_manager = MemoryManagerHandle::get(); - memory_manager - .map_range( - 0xF0002000 as *mut usize, - ((debug::SUPERVISOR_UART.base as u32) & !4095) as *mut usize, - 4096, - 1, - MemoryFlags::R | MemoryFlags::W, - MemoryType::Default, - ) - .expect("unable to map serial port"); + mem::MemoryManager::with_mut(|memory_manager| { + memory_manager + .map_range( + 0xF0002000 as *mut u8, + ((debug::SUPERVISOR_UART.base as u32) & !4095) as *mut u8, + 4096, + PID::new(1).unwrap(), + MemoryFlags::R | MemoryFlags::W, + MemoryType::Default, + ) + .expect("unable to map serial port") + }); println!("KMAIN: Supervisor mode started..."); debug::SUPERVISOR_UART.enable_rx(); println!("Claiming IRQ 3 via syscall..."); diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index 9225adcc6..4311b993d 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -162,9 +162,9 @@ impl MemoryManager { Ok(()) } - #[cfg(baremetal)] + #[cfg(all(baremetal, feature = "print-debug"))] pub fn print_ownership(&self) { - println!("Ownership ({} bytes in all):", self.allocations.len()); + println!("Ownership ({} bytes in all):", unsafe { MEMORY_ALLOCATIONS.len() }); let mut offset = 0; unsafe { diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 5f85ad8d3..1ddd2b805 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -131,7 +131,8 @@ pub struct ProcessInner { /// A copy of this process' ID pub pid: PID, - pub _reserved: [u8; 27], + /// Some reserved data to pad this out to a multiple of 32 bytes. + pub _reserved: [u8; 1], } impl Default for ProcessInner { @@ -146,7 +147,7 @@ impl Default for ProcessInner { mem_heap_max: 524_288, connection_map: [0; 32], pid: unsafe { PID::new_unchecked(1) }, - _reserved: [0; 27], + _reserved: [0; 1], } } } @@ -311,7 +312,7 @@ impl SystemServices { unsafe { core::mem::transmute::(init.entrypoint) }, MemoryRange::new(init.sp, crate::arch::process::DEFAULT_STACK_SIZE).unwrap(), None, - [0u8; 16], + [0u8; 12], )); } } @@ -319,6 +320,7 @@ impl SystemServices { // Set up our handle with a bogus sp and pc. These will get updated // once a context switch _away_ from the kernel occurs, however we need // to make sure other fields such as "thread number" are all valid. + ArchProcess::with_current_mut(|process| process.init(0, 0, INITIAL_TID)); // ProcessHandle::get().init(0, 0, INITIAL_TID); // self.processes[0].current_thread = INITIAL_TID as u8; } diff --git a/tools/src/elf.rs b/tools/src/elf.rs index 47fc2cd55..8b29260e1 100644 --- a/tools/src/elf.rs +++ b/tools/src/elf.rs @@ -174,10 +174,10 @@ pub fn read_program>(filename: P) -> Result usize, pub stack: MemoryRange, pub arg: Option, - pub name: [u8; 16], + pub name: [u8; 12], } impl ThreadInit { @@ -22,7 +22,7 @@ impl ThreadInit { call: fn(data: *const usize) -> usize, stack: MemoryRange, arg: Option, - name: [u8; 16], + name: [u8; 12], ) -> Self { ThreadInit { call, @@ -34,9 +34,9 @@ impl ThreadInit { } #[derive(Copy, Clone, Debug, PartialEq)] -pub struct ProcessKey([u8; 16]); +pub struct ProcessKey([u8; 8]); impl ProcessKey { - pub fn new(key: [u8; 16]) -> ProcessKey { + pub fn new(key: [u8; 8]) -> ProcessKey { ProcessKey(key) } } diff --git a/xous-rs/src/syscall.rs b/xous-rs/src/syscall.rs index 25112f1f7..2fe1ed8f3 100644 --- a/xous-rs/src/syscall.rs +++ b/xous-rs/src/syscall.rs @@ -36,9 +36,7 @@ pub enum SysCall { /// /// # Errors /// - UnmapMemory( - MemoryRange - ), + UnmapMemory(MemoryRange), /// 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 @@ -323,7 +321,9 @@ impl SysCall { use core::mem; assert!( mem::size_of::() == mem::size_of::() * 8, - "SysCall is not the expected size" + "SysCall is not the expected size (expected {}, got {})", + mem::size_of::() * 8, + mem::size_of::() ); match self { SysCall::MapMemory(a1, a2, a3, a4) => [ @@ -571,17 +571,15 @@ impl SysCall { MemoryAddress::new(a3).ok_or(Error::InvalidSyscall)?, MemoryFlags::from_bits(a4).ok_or(Error::InvalidSyscall)?, ), - SysCallNumber::UnmapMemory => SysCall::UnmapMemory( - MemoryRange::new(a1, a2).or(Err(Error::InvalidSyscall))?, - ), + SysCallNumber::UnmapMemory => { + SysCall::UnmapMemory(MemoryRange::new(a1, a2).or(Err(Error::InvalidSyscall))?) + } SysCallNumber::Yield => SysCall::Yield, SysCallNumber::WaitEvent => SysCall::WaitEvent, SysCallNumber::ReceiveMessage => { SysCall::ReceiveMessage(SID::from_u32(a1 as _, a2 as _, a3 as _, a4 as _)) } - SysCallNumber::ReturnToParentI => { - SysCall::ReturnToParentI(pid_from_usize(a1)?, a2) - } + SysCallNumber::ReturnToParentI => SysCall::ReturnToParentI(pid_from_usize(a1)?, a2), SysCallNumber::ClaimInterrupt => SysCall::ClaimInterrupt( a1, MemoryAddress::new(a2).ok_or(Error::InvalidSyscall)?, @@ -652,9 +650,7 @@ impl SysCall { ), _ => SysCall::Invalid(a1, a2, a3, a4, a5, a6, a7), }, - SysCallNumber::ReturnMemory => { - SysCall::ReturnMemory(a1, MemoryRange::new(a2, a3)?) - } + SysCallNumber::ReturnMemory => SysCall::ReturnMemory(a1, MemoryRange::new(a2, a3)?), SysCallNumber::CreateThread => { SysCall::CreateThread(crate::arch::args_to_thread(a1, a2, a3, a4, a5, a6, a7)?) } @@ -709,7 +705,9 @@ pub fn map_memory( flags, ))?; if let Result::MemoryRange(range) = result { - Ok(crate::arch::map_memory_post(phys, virt, size, flags, range)?) + Ok(crate::arch::map_memory_post( + phys, virt, size, flags, range, + )?) } else if let Result::Error(e) = result { Err(e) } else {