From ba30b4919de98a54c9624ffd7b0957c5527233c1 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sun, 9 Aug 2020 21:52:28 +0800 Subject: [PATCH] kernel: remove more warnings Still haven't run it on real hardware, but this removes more warnings from the system. Signed-off-by: Sean Cross --- kernel/.vscode/settings.json | 7 +++++- kernel/src/arch/riscv/mem.rs | 14 ++++++------ kernel/src/arch/riscv/process.rs | 36 ++++++++++++++++++++---------- kernel/src/arch/riscv/syscall.rs | 16 +++++++------- kernel/src/mem.rs | 24 ++++++++------------ kernel/src/server.rs | 2 +- kernel/src/services.rs | 38 ++------------------------------ xous-rs/src/arch/native.rs | 8 +++---- 8 files changed, 61 insertions(+), 84 deletions(-) diff --git a/kernel/.vscode/settings.json b/kernel/.vscode/settings.json index bc21aa68f..40c592ff3 100644 --- a/kernel/.vscode/settings.json +++ b/kernel/.vscode/settings.json @@ -37,5 +37,10 @@ "vmim", "vmip", "xous" + ], + "rust-analyzer.checkOnSave.allTargets": false, + "rust-analyzer.checkOnSave.extraArgs": [ + "--target", + "riscv32imac-unknown-none-elf" ] -} \ No newline at end of file +} diff --git a/kernel/src/arch/riscv/mem.rs b/kernel/src/arch/riscv/mem.rs index 84813c422..9895cf1bc 100644 --- a/kernel/src/arch/riscv/mem.rs +++ b/kernel/src/arch/riscv/mem.rs @@ -436,14 +436,14 @@ pub fn move_page_inner( *entry = 0; unsafe { flush_mmu() }; - dest_space.activate(); + dest_space.activate()?; let phys = previous_entry >> 10 << 12; let flags = untranslate_flags(previous_entry); let result = map_page_inner(mm, dest_pid, phys, dest_addr as usize, flags, dest_pid.get() != 1); // Switch back to the original address space and return - src_space.activate(); + src_space.activate().unwrap(); result } @@ -487,7 +487,7 @@ pub fn lend_page_inner( *entry = (*entry & !MMUFlags::VALID.bits()) | MMUFlags::S.bits(); unsafe { flush_mmu() }; - dest_space.activate(); + dest_space.activate()?; map_page_inner( mm, dest_pid, @@ -514,7 +514,7 @@ pub fn lend_page_inner( ); unsafe { flush_mmu() }; - dest_space.activate(); + dest_space.activate()?; map_page_inner( mm, dest_pid, @@ -526,7 +526,7 @@ pub fn lend_page_inner( }; unsafe { flush_mmu() }; - src_space.activate(); + src_space.activate().unwrap(); result.map(|_| phys) } @@ -549,7 +549,7 @@ pub fn return_page_inner( *src_entry = 0; unsafe { flush_mmu() }; - dest_space.activate(); + dest_space.activate()?; let dest_entry = pagetable_entry(dest_addr as usize).expect("page wasn't lent in destination space"); if *dest_entry & MMUFlags::S.bits() == 0 { @@ -571,7 +571,7 @@ pub fn return_page_inner( } unsafe { flush_mmu() }; - src_space.activate(); + src_space.activate().unwrap(); Ok(phys) } diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index dd202d922..2edd1c512 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -6,7 +6,7 @@ use crate::arch::mem::PAGE_SIZE; use crate::services::ProcessInner; use xous::{ProcessInit, ThreadInit, PID, TID}; -use crate::args::KernelArguments; +// use crate::args::KernelArguments; pub const DEFAULT_STACK_SIZE: usize = 131072; pub const MAX_PROCESS_COUNT: usize = 32; // pub use crate::arch::mem::DEFAULT_STACK_TOP; @@ -15,7 +15,7 @@ pub const MAX_PROCESS_COUNT: usize = 32; pub const RETURN_FROM_ISR: usize = 0xff80_2000; /// This is the address a thread will return to when it exits. -pub const EXIT_THREAD: usize = 0xff80_3000; +const EXIT_THREAD: usize = 0xff80_3000; pub const IRQ_CONTEXT: usize = 1; #[derive(Debug, Copy, Clone)] @@ -117,14 +117,14 @@ impl Process { // }) } - /// Calls the provided function with the current inner process state. - pub fn with_current(f: F) -> R - where - F: FnOnce(&Process) -> R, - { - let process = Self::current(); - f(&process) - } + // /// Calls the provided function with the current inner process state. + // pub fn with_current(f: F) -> R + // where + // F: FnOnce(&Process) -> R, + // { + // let process = Self::current(); + // f(&process) + // } /// Calls the provided function with the current inner process state. pub fn with_current_mut(f: F) -> R @@ -235,8 +235,20 @@ impl Process { process.inner = Default::default(); } - pub fn setup_thread(&mut self, thread: TID, _setup: ThreadInit) -> Result<(), xous::Error> { - todo!(); + pub fn setup_thread(&mut self, new_tid: TID, setup: ThreadInit) -> Result<(), xous::Error> { + let entrypoint = unsafe { core::mem::transmute::<_, usize>(setup.call) }; + // Create the new context and set it to run in the new address space. + let pid = self.pid.get(); + let thread = self.thread(new_tid); + crate::arch::syscall::invoke( + thread, + pid == 1, + entrypoint, + setup.stack.as_ptr() as usize, + EXIT_THREAD, + &[setup.arg.map(|x| x.get() as usize).unwrap_or_default()], + ); + Ok(()) } pub fn create(pid: PID, init_data: ProcessInit) -> PID { diff --git a/kernel/src/arch/riscv/syscall.rs b/kernel/src/arch/riscv/syscall.rs index dd99a5a86..ea49b913c 100644 --- a/kernel/src/arch/riscv/syscall.rs +++ b/kernel/src/arch/riscv/syscall.rs @@ -6,7 +6,7 @@ extern "C" { } pub fn invoke( - Thread: &mut Thread, + thread: &mut Thread, supervisor: bool, pc: usize, sp: usize, @@ -14,13 +14,13 @@ pub fn invoke( args: &[usize], ) { set_supervisor(supervisor); - Thread.registers[0] = ret_addr; - Thread.registers[1] = sp; + thread.registers[0] = ret_addr; + thread.registers[1] = sp; assert!(args.len() <= 8, "too many arguments to invoke()"); for (idx, arg) in args.iter().enumerate() { - Thread.registers[9 + idx] = *arg; + thread.registers[9 + idx] = *arg; } - Thread.sepc = pc; + thread.sepc = pc; } fn set_supervisor(supervisor: bool) { @@ -31,8 +31,8 @@ fn set_supervisor(supervisor: bool) { } } -pub fn resume(supervisor: bool, Thread: &Thread) -> ! { - sepc::write(Thread.sepc); +pub fn resume(supervisor: bool, thread: &Thread) -> ! { + sepc::write(thread.sepc); // Return to the appropriate CPU mode set_supervisor(supervisor); @@ -43,5 +43,5 @@ pub fn resume(supervisor: bool, Thread: &Thread) -> ! { // Thread.registers[1], // Thread.sepc, // ); - unsafe { _xous_resume_context(Thread.registers.as_ptr()) }; + unsafe { _xous_resume_context(thread.registers.as_ptr()) }; } diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index aeecf0b0a..e943cc03d 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -1,5 +1,4 @@ use crate::args::KernelArguments; -use core::cell::RefCell; use core::fmt; use core::mem; use core::slice; @@ -10,9 +9,6 @@ use crate::arch::process::Process; use xous::{MemoryFlags, MemoryRange, PID}; -#[cfg(not(baremetal))] -use std::thread_local; - #[derive(Debug)] enum ClaimOrRelease { Claim, @@ -63,7 +59,7 @@ impl Default for MemoryManager { } #[cfg(not(baremetal))] -thread_local!(static MEMORY_MANAGER: RefCell = RefCell::new(MemoryManager::default())); +std::thread_local!(static MEMORY_MANAGER: core::cell::RefCell = core::cell::RefCell::new(MemoryManager::default())); #[cfg(baremetal)] static mut MEMORY_MANAGER: MemoryManager = MemoryManager::default_hack(); @@ -189,11 +185,11 @@ impl MemoryManager { }; for o in 0..self.ram_size / PAGE_SIZE { unsafe { - if let Some(allocation) = MEMORY_ALLOCATIONS[offset + o] { + if let Some(_allocation) = MEMORY_ALLOCATIONS[offset + o] { println!( " {:08x} => {}", self.ram_size + o * PAGE_SIZE, - allocation.get() + _allocation.get() ); } } @@ -207,14 +203,12 @@ impl MemoryManager { for region in EXTRA_REGIONS { println!(" Region {}:", region); for o in 0..(region.mem_size as usize) / PAGE_SIZE { - unsafe { - if let Some(allocation) = MEMORY_ALLOCATIONS[offset + o] { - println!( - " {:08x} => {}", - (region.mem_start as usize) + o * PAGE_SIZE, - allocation.get() - ) - } + if let Some(_allocation) = MEMORY_ALLOCATIONS[offset + o] { + println!( + " {:08x} => {}", + (region.mem_start as usize) + o * PAGE_SIZE, + _allocation.get() + ) } } offset += region.mem_size as usize / PAGE_SIZE; diff --git a/kernel/src/server.rs b/kernel/src/server.rs index f49191bb9..b81214e0c 100644 --- a/kernel/src/server.rs +++ b/kernel/src/server.rs @@ -175,7 +175,7 @@ impl Server { } #[cfg(baremetal)] - let mut queue = unsafe { + let queue = unsafe { core::slice::from_raw_parts_mut( _backing.as_mut_ptr() as *mut QueuedMessage, _backing.len() / mem::size_of::(), diff --git a/kernel/src/services.rs b/kernel/src/services.rs index d6f78328f..2e30f27e4 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -5,30 +5,6 @@ pub use crate::arch::process::Thread; use crate::mem::MemoryManager; use xous::MemoryRange; -use core::cell::RefCell; - -// Fake macro to implement "thread local" on baremetal as a simple static variable. -// The kernel is single-threaded so it's really just one thread. -#[cfg(baremetal)] -macro_rules! thread_local { - // empty (base case for the recursion) - () => {}; - - // process multiple declarations - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => ( - $(#[$attr])* $vis static $name: $t = $init; - $crate::services::thread_local!($($rest)*); - ); - - // handle a single declaration - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => ( - $(#[$attr])* $vis static $name: $t = $init; - ); -} - -#[cfg(not(baremetal))] -use std::thread_local; - use crate::filled_array; use crate::server::Server; // use core::mem; @@ -216,7 +192,7 @@ impl Process { } #[cfg(not(baremetal))] -thread_local!(static SYSTEM_SERVICES: RefCell = RefCell::new(SystemServices { +std::thread_local!(static SYSTEM_SERVICES: core::cell::RefCell = core::cell::RefCell::new(SystemServices { processes: [Process { state: ProcessState::Free, ppid: unsafe { PID::new_unchecked(1) }, @@ -473,6 +449,7 @@ impl SystemServices { _irq_no: usize, _arg: *mut usize, ) -> Result<(), xous::Error> { + todo!(); Err(xous::Error::UnhandledSyscall) // // Get the current process (which was just interrupted) and mark it as // // "ready to run". If this function is called when the current process @@ -1249,17 +1226,6 @@ impl SystemServices { arch_process.setup_thread(new_tid, thread_init)?; - // // Create the new context and set it to run in the new address space. - // let context = process.context(new_context_nr); - // arch::syscall::invoke( - // context, - // self.pid == 1, - // entrypoint.get() as usize, - // stack_pointer.get() as usize, - // EXIT_THREAD, - // &[arg.map(|x| x.get()).unwrap_or_default() as usize], - // ); - // println!("KERNEL({}): Created new context {}", pid, new_context_nr); // Queue the thread to run diff --git a/xous-rs/src/arch/native.rs b/xous-rs/src/arch/native.rs index 42a60f481..d15cafb1b 100644 --- a/xous-rs/src/arch/native.rs +++ b/xous-rs/src/arch/native.rs @@ -11,10 +11,10 @@ pub struct ProcessArgs { #[derive(Debug, Clone, Copy, PartialEq)] pub struct ThreadInit { - call: fn(data: *const usize) -> usize, - stack: MemoryRange, - arg: Option, - name: [u8; 16], + pub call: fn(data: *const usize) -> usize, + pub stack: MemoryRange, + pub arg: Option, + pub name: [u8; 16], } impl ThreadInit {