1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-22 08:06:17 +04:00

clean and refactor code

This commit is contained in:
Runji Wang 2020-06-15 12:31:12 +08:00
parent 22dbe7f515
commit eaa3b42f5d
14 changed files with 49 additions and 135 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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;

View File

@ -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

View File

@ -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");

View File

@ -111,17 +111,11 @@ lazy_static! {
/// return the process which thread tid is in
pub fn process_of(tid: usize) -> Option<Arc<Mutex<Process>>> {
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<Arc<Mutex<Process>>> {
@ -132,17 +126,8 @@ pub fn process_group(pgid: i32) -> Vec<Arc<Mutex<Process>>> {
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::<Vec<_>>()
}

View File

@ -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;

View File

@ -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)]

View File

@ -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> {

View File

@ -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,

View File

@ -531,8 +531,6 @@ impl Syscall<'_> {
pub type SysResult = Result<usize, SysError>;
use num::FromPrimitive;
#[allow(dead_code)]
#[repr(isize)]
#[derive(Debug, FromPrimitive)]

View File

@ -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<usize, Weak<Mutex<Process>>> = 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())
}
}

View File

@ -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();

View File

@ -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();
}