diff --git a/kernel/src/arch/x86_64/interrupt/handler.rs b/kernel/src/arch/x86_64/interrupt/handler.rs index 98e80a3a..39ec86ec 100644 --- a/kernel/src/arch/x86_64/interrupt/handler.rs +++ b/kernel/src/arch/x86_64/interrupt/handler.rs @@ -67,7 +67,6 @@ use super::consts::*; use super::TrapFrame; use crate::drivers::IRQ_MANAGER; -use crate::process::current_thread; use crate::signal::do_signal; use bitflags::*; use log::*; @@ -216,7 +215,7 @@ pub extern "C" fn syscall(tf: &mut TrapFrame) { trace!("\nInterupt: Syscall {:#x?}", tf.rax); //let ret = crate::syscall::syscall(tf.rax, [tf.rdi, tf.rsi, tf.rdx, tf.r10, tf.r8, tf.r9], tf); //tf.rax = ret as usize; - do_signal(tf); + //do_signal(tf); } fn syscall32(tf: &mut TrapFrame) { diff --git a/kernel/src/fs/devfs/fbdev.rs b/kernel/src/fs/devfs/fbdev.rs index 2deb08c7..e4650ac3 100755 --- a/kernel/src/fs/devfs/fbdev.rs +++ b/kernel/src/fs/devfs/fbdev.rs @@ -100,6 +100,7 @@ impl INode for Fbdev { if area.offset + area.end_vaddr - area.start_vaddr > fb.framebuffer_size() { return Err(FsError::NoDeviceSpace); } + /* let thread = unsafe { crate::process::current_thread() }; thread.vm.lock().push( area.start_vaddr, @@ -108,6 +109,7 @@ impl INode for Fbdev { Linear::new((fb.paddr() + area.offset - area.start_vaddr) as isize), "mmap_file", ); + */ Ok(()) } else { Err(FsError::NoDevice) diff --git a/kernel/src/fs/file.rs b/kernel/src/fs/file.rs index 6108b13c..9a9e3d52 100644 --- a/kernel/src/fs/file.rs +++ b/kernel/src/fs/file.rs @@ -1,7 +1,7 @@ //! File handle for process use crate::memory::GlobalFrameAlloc; -use crate::process::{current_thread, INodeForMap}; +use crate::process::INodeForMap; use crate::syscall::{MmapProt, SysResult, TimeSpec}; use alloc::{string::String, sync::Arc}; use core::fmt; @@ -233,6 +233,7 @@ impl FileHandle { match self.inode.metadata()?.type_ { FileType::File => { let prot = MmapProt::from_bits_truncate(area.prot); + /* let thread = unsafe { current_thread() }; thread.vm.lock().push( area.start_vaddr, @@ -247,6 +248,7 @@ impl FileHandle { }, "mmap_file", ); + */ Ok(()) } FileType::CharDevice => self.inode.mmap(area), diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 40be74e0..07cd8a30 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -8,6 +8,7 @@ #![feature(negative_impls)] #![feature(alloc_prelude)] #![feature(const_fn)] +#![feature(const_in_array_repeat_expressions)] #![deny(unused_must_use)] #![deny(stable_features)] #![deny(unused_unsafe)] diff --git a/kernel/src/memory.rs b/kernel/src/memory.rs index 5c84e56e..f15b8062 100644 --- a/kernel/src/memory.rs +++ b/kernel/src/memory.rs @@ -141,8 +141,9 @@ impl Drop for KernelStack { pub fn handle_page_fault(addr: usize) -> bool { debug!("page fault @ {:#x}", addr); - let thread = unsafe { current_thread() }; - thread.vm.lock().handle_page_fault(addr) + let thread = current_thread().unwrap(); + let mut lock = thread.vm.lock(); + lock.handle_page_fault(addr) } pub fn init_heap() { diff --git a/kernel/src/process/mod.rs b/kernel/src/process/mod.rs index c98b8c6e..94515750 100644 --- a/kernel/src/process/mod.rs +++ b/kernel/src/process/mod.rs @@ -38,15 +38,16 @@ pub fn init() { info!("process: init end"); } +static mut THREADS: [Option>; MAX_CPU_NUM] = [None; MAX_CPU_NUM]; + /// Get current thread /// /// `Thread` is a thread-local object. /// It is safe to call this once, and pass `&mut Thread` as a function argument. -pub unsafe fn current_thread() -> &'static mut Thread { - // trick: force downcast from trait object - //let (process, _): (&mut Thread, *const ()) = core::mem::transmute(processor().context()); - //process - todo!() +/// Should only be called in kernel trap handler +pub fn current_thread() -> Option> { + let cpu_id = cpu::id(); + unsafe { THREADS[cpu_id].clone() } } pub fn spawn(thread: Arc) { @@ -109,6 +110,10 @@ impl Future for PageTableSwitchWrapper { type Output = (); fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { // vmtoken might change upon sys_exec + let cpu_id = cpu::id(); + unsafe { + THREADS[cpu_id] = Some(self.thread.clone()); + } let vmtoken = self.thread.vm.lock().token(); unsafe { Cr3::write( @@ -116,6 +121,10 @@ impl Future for PageTableSwitchWrapper { Cr3Flags::empty(), ); } - self.inner.lock().as_mut().poll(cx) + let res = self.inner.lock().as_mut().poll(cx); + unsafe { + THREADS[cpu_id] = None; + } + res } } diff --git a/kernel/src/signal/mod.rs b/kernel/src/signal/mod.rs index d4a3a5ef..50b88817 100644 --- a/kernel/src/signal/mod.rs +++ b/kernel/src/signal/mod.rs @@ -1,4 +1,4 @@ -use crate::process::{current_thread, process, process_of, Process}; +use crate::process::{process, process_of, Process, Thread}; use crate::sync::{MutexGuard, SpinNoIrq, SpinNoIrqLock as Mutex}; use alloc::sync::Arc; use bitflags::*; @@ -132,9 +132,8 @@ pub struct SignalFrame { pub ret_code: [u8; 7], // call sys_sigreturn } -pub fn do_signal(tf: &mut TrapFrame) { - let thread = unsafe { current_thread() }; - let mut process = unsafe { current_thread().proc.lock() }; +pub fn do_signal(thread: &Arc, tf: &mut TrapFrame) { + let mut process = thread.proc.lock(); while let Some((idx, info)) = process .sig_queue