diff --git a/kernel/src/arch/x86_64/interrupt/trap.asm b/kernel/src/arch/x86_64/interrupt/trap.asm index e89e247a..a1098860 100644 --- a/kernel/src/arch/x86_64/interrupt/trap.asm +++ b/kernel/src/arch/x86_64/interrupt/trap.asm @@ -20,11 +20,9 @@ __alltraps: push r14 push r15 -.att_syntax - subq $0x8, %rsp - stmxcsr 0(%rsp) + sub rsp, 8 + stmxcsr [rsp] -.intel_syntax noprefix # push fs.base xor rax, rax mov ecx, 0xC0000100 @@ -39,9 +37,7 @@ __alltraps: mov rax, rsp and rax, 0xFFFFFFFFFFFFFFF0 # fxsave (rax) - .byte 0x0f - .byte 0xae - .byte 0x00 + .byte 0x0f, 0xae, 0x00 mov rcx, rsp sub rcx, rax # push fp state offset @@ -65,9 +61,7 @@ trap_ret: add rax, 16 sub rax, rcx # fxrstor (rax) - .byte 0x0f - .byte 0xae - .byte 0x08 + .byte 0x0f, 0xae, 0x08 skip_fxrstor: add rsp, 16+512 @@ -78,11 +72,9 @@ skip_fxrstor: mov ecx, 0xC0000100 wrmsr # msr[ecx] <= edx:eax -.att_syntax - ldmxcsr 0(%rsp) - addq $0x8, %rsp + ldmxcsr [rsp] + add rsp, 8 -.intel_syntax noprefix pop r15 pop r14 pop r13 @@ -152,11 +144,9 @@ syscall_entry: push r14 push r15 -.att_syntax - subq $0x8, %rsp - stmxcsr 0(%rsp) + sub rsp, 8 + stmxcsr [rsp] -.intel_syntax noprefix # push fs.base xor rax, rax mov ecx, 0xC0000100 @@ -171,9 +161,7 @@ syscall_entry: mov rax, rsp and rax, 0xFFFFFFFFFFFFFFF0 # fxsave (rax) - .byte 0x0f - .byte 0xae - .byte 0x00 + .byte 0x0f, 0xae, 0x00 mov rcx, rsp sub rcx, rax # push fp state offset @@ -199,9 +187,7 @@ syscall_return: add rax, 16 sub rax, rcx # fxrstor (rax) - .byte 0x0f - .byte 0xae - .byte 0x08 + .byte 0x0f, 0xae, 0x08 skip_fxrstor1: add rsp, 16+512 @@ -212,11 +198,9 @@ skip_fxrstor1: mov ecx, 0xC0000100 wrmsr # msr[ecx] <= edx:eax -.att_syntax - ldmxcsr 0(%rsp) - addq $0x8, %rsp + ldmxcsr [rsp] + add rsp, 8 -.intel_syntax noprefix pop r15 pop r14 pop r13 diff --git a/kernel/src/arch/x86_64/interrupt/trapframe.rs b/kernel/src/arch/x86_64/interrupt/trapframe.rs index b63067a2..8eb8b5b2 100644 --- a/kernel/src/arch/x86_64/interrupt/trapframe.rs +++ b/kernel/src/arch/x86_64/interrupt/trapframe.rs @@ -1,12 +1,6 @@ -use crate::arch::get_sp; use crate::arch::signal::MachineContext; -use crate::process::{current_thread, thread_manager}; -use crate::signal::*; -use alloc::vec::Vec; use core::default::Default; use core::fmt; -use num::FromPrimitive; -use rcore_thread::std_thread::current; #[derive(Clone)] #[repr(C)] @@ -200,43 +194,27 @@ impl Context { push r13 push r14 push r15 - "::::"intel" "volatile"); - asm!(" // save page table mov r15, cr3 - push r15"::::"intel" "volatile" - ); + push r15 - asm!( - " // Switch stacks mov [rdi], rsp // rdi = from_rsp mov rsp, [rsi] // rsi = to_rsp - "::::"intel" "volatile"); - asm!( - " // restore page table pop r15 mov cr3, r15 - "::::"intel" "volatile"); - asm!(" // restore old callee-save registers pop r15 - ": : : : "intel" "volatile" - ); - asm!(" pop r14 pop r13 - pop r12": : : : "intel" "volatile"); - - asm!(" + pop r12 pop rbp pop rbx ": : : : "intel" "volatile"); - // info!("wang!"); } pub unsafe fn null() -> Self { diff --git a/kernel/src/consts.rs b/kernel/src/consts.rs index e561c46d..58281ae6 100644 --- a/kernel/src/consts.rs +++ b/kernel/src/consts.rs @@ -1,7 +1,6 @@ #![allow(dead_code)] pub use crate::arch::consts::*; -use alloc::string::String; pub const MAX_CPU_NUM: usize = 64; pub const MAX_PROCESS_NUM: usize = 512; diff --git a/kernel/src/fpe.S b/kernel/src/fpe.S deleted file mode 100644 index 024bfded..00000000 --- a/kernel/src/fpe.S +++ /dev/null @@ -1,12 +0,0 @@ -.global fpe -fpe: - movsd .LC0(%rip), %xmm0 - subsd %xmm0, %xmm0 - stmxcsr -4(%rsp) - mov %eax, -4(%rsp) - ret -.LC0: - .long 0 - .long 2146435072 - .long 0 - .long 0 diff --git a/kernel/src/fs/mod.rs b/kernel/src/fs/mod.rs index 73fdeb99..236bc425 100644 --- a/kernel/src/fs/mod.rs +++ b/kernel/src/fs/mod.rs @@ -97,8 +97,8 @@ lazy_static! { }); let devfs = dev.mount(devfs).expect("failed to mount DevFS"); - let shm = devfs.root_inode().find(true, "shm").expect("cannot find shm"); // mount RamFS at /dev/shm + let shm = devfs.root_inode().find(true, "shm").expect("cannot find shm"); let shmfs = RamFS::new(); shm.mount(shmfs).expect("failed to mount /dev/shm"); diff --git a/kernel/src/process/structs.rs b/kernel/src/process/structs.rs index 63e27100..0edc1b44 100644 --- a/kernel/src/process/structs.rs +++ b/kernel/src/process/structs.rs @@ -111,17 +111,11 @@ lazy_static! { /// return the process which thread tid is in pub fn process_of(tid: usize) -> Option>> { - PROCESSES.read().iter().find_map(|(pid, weak)| { - if let Some(process) = weak.upgrade() { - if process.lock().threads.contains(&tid) { - Some(process) - } else { - None - } - } else { - None - } - }) + PROCESSES + .read() + .iter() + .filter_map(|(_, weak)| weak.upgrade()) + .find(|proc| proc.lock().threads.contains(&tid)) } pub fn process(pid: usize) -> Option>> { @@ -132,17 +126,8 @@ pub fn process_group(pgid: i32) -> Vec>> { PROCESSES .read() .iter() - .filter_map(|(pid, proc)| { - if let Some(proc) = proc.upgrade() { - if proc.lock().pgid == pgid { - Some(proc) - } else { - None - } - } else { - None - } - }) + .filter_map(|(_, proc)| proc.upgrade()) + .filter(|proc| proc.lock().pgid == pgid) .collect::>() } diff --git a/kernel/src/signal/action.rs b/kernel/src/signal/action.rs index 1e7c7de3..a7357bc0 100644 --- a/kernel/src/signal/action.rs +++ b/kernel/src/signal/action.rs @@ -7,13 +7,13 @@ pub const SIG_ERR: usize = usize::max_value() - 1; pub const SIG_DFL: usize = 0; pub const SIG_IGN: usize = 1; -pub const SI_ASYNCNL: i32 = (-60); -pub const SI_TKILL: i32 = (-6); -pub const SI_SIGIO: i32 = (-5); -pub const SI_ASYNCIO: i32 = (-4); -pub const SI_MESGQ: i32 = (-3); -pub const SI_TIMER: i32 = (-2); -pub const SI_QUEUE: i32 = (-1); +pub const SI_ASYNCNL: i32 = -60; +pub const SI_TKILL: i32 = -6; +pub const SI_SIGIO: i32 = -5; +pub const SI_ASYNCIO: i32 = -4; +pub const SI_MESGQ: i32 = -3; +pub const SI_TIMER: i32 = -2; +pub const SI_QUEUE: i32 = -1; pub const SI_USER: i32 = 0; pub const SI_KERNEL: i32 = 128; diff --git a/kernel/src/signal/mod.rs b/kernel/src/signal/mod.rs index 661f40b7..b7909deb 100644 --- a/kernel/src/signal/mod.rs +++ b/kernel/src/signal/mod.rs @@ -10,10 +10,6 @@ pub use self::action::*; use crate::arch::interrupt::TrapFrame; use crate::arch::signal::MachineContext; use crate::arch::syscall::SYS_RT_SIGRETURN; -use crate::arch::{get_sp, set_sp}; -use crate::processor; -use crate::syscall::{SysError, SysResult}; -use alloc::vec::Vec; use rcore_thread::std_thread::{current, yield_now}; #[derive(Eq, PartialEq, FromPrimitive, Debug, Copy, Clone)] diff --git a/kernel/src/sync/semaphore.rs b/kernel/src/sync/semaphore.rs index ab7aae82..a8e46f0f 100644 --- a/kernel/src/sync/semaphore.rs +++ b/kernel/src/sync/semaphore.rs @@ -8,12 +8,6 @@ use crate::syscall::SysError; use core::cell::Cell; use core::ops::Deref; -struct SemaphoreInner { - pub count: isize, - pub pid: usize, - pub removed: bool, -} - /// A counting, blocking, semaphore. pub struct Semaphore { // value and removed @@ -21,6 +15,12 @@ pub struct Semaphore { cvar: Condvar, } +struct SemaphoreInner { + count: isize, + pid: usize, + removed: bool, +} + /// An RAII guard which will release a resource acquired from a semaphore when /// dropped. pub struct SemaphoreGuard<'a> { diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 370270ff..ef636494 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -383,7 +383,7 @@ impl Syscall<'_> { match file_like { FileLike::File(_file) => { &crate::fs::STDIN.pushed.register_epoll_list( - unsafe { current_thread() }.proc.clone(), + self.thread.proc.clone(), thread::current().id(), epfd, *fd, @@ -392,7 +392,7 @@ impl Syscall<'_> { } FileLike::Socket(_socket) => { &(*crate::drivers::SOCKET_ACTIVITY).register_epoll_list( - unsafe { current_thread() }.proc.clone(), + self.thread.proc.clone(), thread::current().id(), epfd, *fd, diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 8719703d..6bc365ed 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -531,8 +531,6 @@ impl Syscall<'_> { pub type SysResult = Result; -use num::FromPrimitive; - #[allow(dead_code)] #[repr(isize)] #[derive(Debug, FromPrimitive)] diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index a9557bf0..dcbcbdfa 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -9,7 +9,7 @@ use alloc::sync::Weak; impl Syscall<'_> { /// Fork the current process. Return the child's PID. pub fn sys_fork(&mut self) -> SysResult { - let new_thread = unsafe { current_thread() }.fork(self.tf); + let new_thread = self.thread.fork(self.tf); let pid = new_thread.proc.lock().pid.get(); let tid = thread_manager().add(new_thread); thread_manager().detach(tid); @@ -56,8 +56,9 @@ impl Syscall<'_> { let parent_tid_ref = unsafe { self.vm().check_write_ptr(parent_tid)? }; // child_tid buffer should not be set because CLONE_CHILD_SETTID flag is not specified in the current implementation // let child_tid_ref = unsafe { self.vm().check_write_ptr(child_tid)? }; - let mut new_thread = - unsafe { current_thread() }.clone(self.tf, newsp, newtls, child_tid as usize); + let mut new_thread = self + .thread + .clone(self.tf, newsp, newtls, child_tid as usize); if clone_flags.contains(CloneFlags::CHILD_CLEARTID) { new_thread.clear_child_tid = child_tid as usize; } @@ -201,8 +202,7 @@ impl Syscall<'_> { .files .iter() .filter_map(|(fd, file_like)| { - use crate::fs::FileLike::File; - if let File(file) = file_like { + if let FileLike::File(file) = file_like { if file.fd_cloexec { Some(*fd) } else { @@ -253,8 +253,8 @@ impl Syscall<'_> { let process_table = PROCESSES.read(); // let process_table: BTreeMap>> = BTreeMap::new(); let proc = process_table.get(&pid); - if (proc.is_some()) { - let lock = proc.unwrap().upgrade().unwrap(); + if let Some(proc) = proc { + let lock = proc.upgrade().unwrap(); let proc = lock.lock(); Ok(proc.pgid as usize) } else { @@ -269,9 +269,9 @@ impl Syscall<'_> { info!("setpgid: set pgid of process {} to {}", pid, pgid); let process_table = PROCESSES.read(); let proc = process_table.get(&pid); - if (proc.is_some()) { + if let Some(proc) = proc { // TODO: check process pid is the child of calling process - if let Some(proc) = proc.unwrap().upgrade() { + if let Some(proc) = proc.upgrade() { let mut proc = proc.lock(); proc.pgid = pgid as i32; } @@ -315,8 +315,7 @@ impl Syscall<'_> { // ref: http://man7.org/linux/man-pages/man2/set_tid_address.2.html // FIXME: do it in all possible ways a thread can exit // it has memory access so we can't move it to Thread::drop? - let thread = unsafe { current_thread() }; - let clear_child_tid = thread.clear_child_tid as *mut u32; + let clear_child_tid = self.thread.clear_child_tid as *mut u32; if !clear_child_tid.is_null() { info!("exit: futex {:#?} wake 1", clear_child_tid); if let Ok(clear_child_tid_ref) = unsafe { self.vm().check_write_ptr(clear_child_tid) } { @@ -365,7 +364,7 @@ impl Syscall<'_> { pub fn sys_set_tid_address(&mut self, tidptr: *mut u32) -> SysResult { info!("set_tid_address: {:?}", tidptr); - unsafe { current_thread() }.clear_child_tid = tidptr as usize; + self.thread.clear_child_tid = tidptr as usize; Ok(thread::current().id()) } } diff --git a/kernel/src/syscall/signal.rs b/kernel/src/syscall/signal.rs index 002b2eb7..8b0894a7 100644 --- a/kernel/src/syscall/signal.rs +++ b/kernel/src/syscall/signal.rs @@ -1,11 +1,8 @@ -use crate::arch::interrupt::TrapFrame; use crate::process::{current_thread, process_of, thread_manager, PROCESSES}; use crate::process::{process, process_group}; -use crate::signal::Signal::SIGINT; use crate::signal::*; use crate::syscall::SysError::{EINVAL, ENOMEM, EPERM, ESRCH}; use crate::syscall::{SysResult, Syscall}; -use crate::thread; use num::FromPrimitive; impl Syscall<'_> { @@ -52,7 +49,7 @@ impl Syscall<'_> { pub fn sys_rt_sigreturn(&mut self) -> SysResult { info!("rt_sigreturn"); // FIXME: adapt arch - let frame = unsafe { (&*((self.tf.get_sp() - 8) as *mut SignalFrame)) }; + let frame = unsafe { &*((self.tf.get_sp() - 8) as *mut SignalFrame) }; // frame.info.signo { let mut process = self.process(); diff --git a/kernel/src/trap.rs b/kernel/src/trap.rs index 0c06a18b..12160fc1 100644 --- a/kernel/src/trap.rs +++ b/kernel/src/trap.rs @@ -8,14 +8,6 @@ use rcore_thread::std_thread::current; pub static mut TICK: usize = 0; -#[cfg(target_arch = "x86_64")] -global_asm!(include_str!("fpe.S")); - -#[cfg(target_arch = "x86_64")] -extern "C" { - fn fpe(); -} - lazy_static! { pub static ref TICK_ACTIVITY: Condvar = Condvar::new(); } @@ -28,8 +20,6 @@ pub fn timer() { if cpu::id() == 0 { unsafe { TICK += 1; - #[cfg(target_arch = "x86_64")] - fpe(); if uptime_msec() % INFORM_PER_MSEC == 0 { TICK_ACTIVITY.notify_all(); }