From d6ce30924713db3a1355e0c644adb9e845f1df4e Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Wed, 10 Apr 2019 00:38:31 +0800 Subject: [PATCH 01/11] Fix some review issues. --- kernel/src/arch/mipsel/compiler_rt.rs | 8 -------- kernel/src/arch/mipsel/context.rs | 4 ++-- kernel/src/arch/mipsel/cpu.rs | 3 ++- 3 files changed, 4 insertions(+), 11 deletions(-) delete mode 100644 kernel/src/arch/mipsel/compiler_rt.rs diff --git a/kernel/src/arch/mipsel/compiler_rt.rs b/kernel/src/arch/mipsel/compiler_rt.rs deleted file mode 100644 index b49a3ce..0000000 --- a/kernel/src/arch/mipsel/compiler_rt.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! Workaround for missing compiler-builtin symbols -//! -//! [atomic](http://llvm.org/docs/Atomics.html#libcalls-atomic) - -#[no_mangle] -pub extern fn abort() { - panic!("abort"); -} diff --git a/kernel/src/arch/mipsel/context.rs b/kernel/src/arch/mipsel/context.rs index 7e473ad..e0502ee 100644 --- a/kernel/src/arch/mipsel/context.rs +++ b/kernel/src/arch/mipsel/context.rs @@ -50,8 +50,8 @@ pub struct TrapFrame { pub sp: usize, pub fp: usize, pub ra: usize, - /// Reserve space for hartid - pub _hartid: usize, + /// Reserved + pub reserved: usize, } impl TrapFrame { diff --git a/kernel/src/arch/mipsel/cpu.rs b/kernel/src/arch/mipsel/cpu.rs index ea27d1f..764d472 100644 --- a/kernel/src/arch/mipsel/cpu.rs +++ b/kernel/src/arch/mipsel/cpu.rs @@ -1,4 +1,5 @@ use mips::registers::cp0; +use mips::instructions; use crate::consts::MAX_CPU_NUM; use core::ptr::{read_volatile, write_volatile}; @@ -21,7 +22,7 @@ pub unsafe fn start_others(hart_mask: usize) { } pub fn halt() { - /* nothing to do */ + unsafe { instructions::wait(); } } pub unsafe fn exit_in_qemu(error_code: u8) -> ! { From 657395784df6a3fc2a05ae246a9097c2445cd2ff Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Mon, 15 Apr 2019 13:02:06 +0800 Subject: [PATCH 02/11] Fix compile errors. --- kernel/src/arch/mipsel/cpu.rs | 1 - kernel/src/arch/mipsel/mod.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/kernel/src/arch/mipsel/cpu.rs b/kernel/src/arch/mipsel/cpu.rs index 0d21616..764d472 100644 --- a/kernel/src/arch/mipsel/cpu.rs +++ b/kernel/src/arch/mipsel/cpu.rs @@ -2,7 +2,6 @@ use mips::registers::cp0; use mips::instructions; use crate::consts::MAX_CPU_NUM; use core::ptr::{read_volatile, write_volatile}; -use mips::registers::cp0; static mut STARTED: [bool; MAX_CPU_NUM] = [false; MAX_CPU_NUM]; diff --git a/kernel/src/arch/mipsel/mod.rs b/kernel/src/arch/mipsel/mod.rs index 74103db..cf13942 100644 --- a/kernel/src/arch/mipsel/mod.rs +++ b/kernel/src/arch/mipsel/mod.rs @@ -1,4 +1,3 @@ -pub mod compiler_rt; pub mod consts; pub mod cpu; pub mod driver; From bfa4c9d47eb78cf0563eff3bfd20bcc0bf971b96 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Mon, 15 Apr 2019 14:14:07 +0800 Subject: [PATCH 03/11] Support TLS for MIPS. --- kernel/src/arch/mipsel/boot/context.S | 56 +++++++++++-------- kernel/src/arch/mipsel/boot/trap.S | 3 + kernel/src/arch/mipsel/context.rs | 23 ++++---- kernel/src/arch/mipsel/interrupt.rs | 80 +++++++++++++++++++++++++++ kernel/src/syscall/mod.rs | 28 ++++++++++ 5 files changed, 156 insertions(+), 34 deletions(-) diff --git a/kernel/src/arch/mipsel/boot/context.S b/kernel/src/arch/mipsel/boot/context.S index 534ede5..d81e961 100644 --- a/kernel/src/arch/mipsel/boot/context.S +++ b/kernel/src/arch/mipsel/boot/context.S @@ -7,22 +7,23 @@ .globl switch_context .extern _root_page_table_ptr .extern _cur_kstack_ptr +.extern _cur_tls switch_context: // save from's registers addi sp, sp, (-4*14) sw sp, 0(a0) sw ra, 0(sp) - sw s0, 2*4(sp) - sw s1, 3*4(sp) - sw s2, 4*4(sp) - sw s3, 5*4(sp) - sw s4, 6*4(sp) - sw s5, 7*4(sp) - sw s6, 8*4(sp) - sw s7, 9*4(sp) - sw s8, 10*4(sp) - sw gp, 11*4(sp) + sw s0, 4*4(sp) + sw s1, 5*4(sp) + sw s2, 6*4(sp) + sw s3, 7*4(sp) + sw s4, 8*4(sp) + sw s5, 9*4(sp) + sw s6, 10*4(sp) + sw s7, 11*4(sp) + sw s8, 12*4(sp) + sw gp, 13*4(sp) // sw ra, 12*4(sp) // sw sp, 13*4(sp) @@ -31,27 +32,34 @@ switch_context: lw s1, 0(s0) sw s1, 4(sp) + // save TLS + la s2, _cur_tls + lw s1, 0(s2) + sw s1, 2*4(sp) + // restore to's registers lw sp, 0(a1) + + // restore page table address lw s1, 4(sp) sw s1, 0(s0) - // restore kstack ptr - // la s0, _cur_kstack_ptr - // addi s1, sp, 4 * 14 - // sw s1, 0(s0) + // restore TLS + lw s1, 2*4(sp) + sw s1, 0(s2) + mtc0 s1, $4, 2 // cp0.user_local lw ra, 0(sp) - lw s0, 2*4(sp) - lw s1, 3*4(sp) - lw s2, 4*4(sp) - lw s3, 5*4(sp) - lw s4, 6*4(sp) - lw s5, 7*4(sp) - lw s6, 8*4(sp) - lw s7, 9*4(sp) - lw s8, 10*4(sp) - lw gp, 11*4(sp) + lw s0, 4*4(sp) + lw s1, 5*4(sp) + lw s2, 6*4(sp) + lw s3, 7*4(sp) + lw s4, 8*4(sp) + lw s5, 9*4(sp) + lw s6, 10*4(sp) + lw s7, 11*4(sp) + lw s8, 12*4(sp) + lw gp, 13*4(sp) addi sp, sp, (4*14) sw zero, 0(a1) diff --git a/kernel/src/arch/mipsel/boot/trap.S b/kernel/src/arch/mipsel/boot/trap.S index 2d40343..44d48d2 100644 --- a/kernel/src/arch/mipsel/boot/trap.S +++ b/kernel/src/arch/mipsel/boot/trap.S @@ -177,3 +177,6 @@ _root_page_table_ptr: .global _cur_kstack_ptr _cur_kstack_ptr: .space 4 # 4bytes + .global _cur_tls +_cur_tls: + .space 4 # 4bytes diff --git a/kernel/src/arch/mipsel/context.rs b/kernel/src/arch/mipsel/context.rs index e5e2053..eec9bde 100644 --- a/kernel/src/arch/mipsel/context.rs +++ b/kernel/src/arch/mipsel/context.rs @@ -130,17 +130,21 @@ struct ContextData { ra: usize, /// Page table token satp: usize, - /// Callee-saved registers + /// s[0] = TLS + /// s[1] = reserved + /// s[2..11] = Callee-saved registers s: [usize; 12], } impl ContextData { - fn new(satp: usize) -> Self { - ContextData { + fn new(satp: usize, tls: usize) -> Self { + let mut context = ContextData { ra: trap_return as usize, - satp, + satp: satp, ..ContextData::default() - } + }; + context.s[0] = tls; + context } } @@ -191,7 +195,7 @@ impl Context { ); InitStack { - context: ContextData::new(satp), + context: ContextData::new(satp, 0), tf: TrapFrame::new_kernel_thread(entry, arg, kstack_top), } .push_at(kstack_top) @@ -215,7 +219,7 @@ impl Context { ); InitStack { - context: ContextData::new(satp), + context: ContextData::new(satp, 0), tf: TrapFrame::new_user_thread(entry_addr, ustack_top), } .push_at(kstack_top) @@ -228,7 +232,7 @@ impl Context { /// All the other registers are same as the original. pub unsafe fn new_fork(tf: &TrapFrame, kstack_top: usize, satp: usize) -> Self { InitStack { - context: ContextData::new(satp), + context: ContextData::new(satp, 0), tf: { let mut tf = tf.clone(); // fork function's ret value, the new process is 0 @@ -254,11 +258,10 @@ impl Context { tls: usize, ) -> Self { InitStack { - context: ContextData::new(satp), + context: ContextData::new(satp, tls), tf: { let mut tf = tf.clone(); tf.sp = ustack_top; // sp - tf.v1 = tls; tf.v0 = 0; // return value tf }, diff --git a/kernel/src/arch/mipsel/interrupt.rs b/kernel/src/arch/mipsel/interrupt.rs index bc60547..5ae84dd 100644 --- a/kernel/src/arch/mipsel/interrupt.rs +++ b/kernel/src/arch/mipsel/interrupt.rs @@ -70,6 +70,14 @@ pub extern "C" fn rust_trap(tf: &mut TrapFrame) { E::TLBModification => page_fault(tf), E::TLBLoadMiss => page_fault(tf), E::TLBStoreMiss => page_fault(tf), + E::ReservedInstruction => { + if !reserved_inst(tf) { + error!("Unhandled Exception @ CPU{}: {:?} ", 0, tf.cause.cause()); + crate::trap::error(tf) + } else { + tf.epc = tf.epc + 4; + } + } _ => { error!("Unhandled Exception @ CPU{}: {:?} ", 0, tf.cause.cause()); crate::trap::error(tf) @@ -149,6 +157,78 @@ fn syscall(tf: &mut TrapFrame) { } } +fn set_trapframe_register(rt: usize, val: usize, tf: &mut TrapFrame) { + match rt { + 1 => tf.at = val, + 2 => tf.v0 = val, + 3 => tf.v1 = val, + 4 => tf.a0 = val, + 5 => tf.a1 = val, + 6 => tf.a2 = val, + 7 => tf.a3 = val, + 8 => tf.t0 = val, + 9 => tf.t1 = val, + 10 => tf.t2 = val, + 11 => tf.t3 = val, + 12 => tf.t4 = val, + 13 => tf.t5 = val, + 14 => tf.t6 = val, + 15 => tf.t7 = val, + 16 => tf.s0 = val, + 17 => tf.s1 = val, + 18 => tf.s2 = val, + 19 => tf.s3 = val, + 20 => tf.s4 = val, + 21 => tf.s5 = val, + 22 => tf.s6 = val, + 23 => tf.s7 = val, + 24 => tf.t8 = val, + 25 => tf.t9 = val, + 26 => tf.k0 = val, + 27 => tf.k1 = val, + 28 => tf.gp = val, + 29 => tf.sp = val, + 30 => tf.fp = val, + 31 => tf.ra = val, + _ => { + error!("Unknown register {:?} ", rt); + crate::trap::error(tf) + } + } +} + +fn reserved_inst(tf: &mut TrapFrame) -> bool { + let inst = unsafe { + *(tf.epc as *const usize) + }; + + let opcode = inst >> 26; + let rt = (inst >> 16) & 0b11111; + let rd = (inst >> 11) & 0b11111; + let sel = (inst >> 6) & 0b111; + let format = inst & 0b111111; + + if opcode == 0b011111 && format == 0b111011 { + // RDHWR + if rd == 29 && sel == 0 { + info!("Read TLS by rdhdr"); + extern "C" { + fn _cur_tls(); + } + + let tls = unsafe { + *(_cur_tls as *const usize) + }; + set_trapframe_register(rt, tls, tf); + return true; + } else { + return false; + } + } + + false +} + fn page_fault(tf: &mut TrapFrame) { // TODO: set access/dirty bit let addr = tf.vaddr; diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 0787d83..11558d3 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -288,8 +288,15 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { let x86_64_ret = x86_64_syscall(id, args, tf); #[cfg(not(target_arch = "x86_64"))] let x86_64_ret = None; + + #[cfg(target_arch = "mips")] + let mips_ret = mips_syscall(id, args, tf); + #[cfg(not(target_arch = "mips"))] + let mips_ret = None; if let Some(ret) = x86_64_ret { ret + } else if let Some(ret) = mips_ret { + ret } else { error!("unknown syscall id: {}, args: {:x?}", id, args); crate::trap::error(tf); @@ -309,6 +316,27 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { } } +#[cfg(target_arch = "mips")] +fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option { + let ret = match id { + SYS_SET_THREAD_AREA => { + extern "C" { + fn _cur_tls(); + } + + unsafe { + asm!("mtc0 $0, $$4, 2": :"r"(args[0])); + *(_cur_tls as *mut usize) = args[0]; + } + Ok(0) + } + _ => { + return None; + } + }; + Some(ret) +} + #[cfg(target_arch = "x86_64")] fn x86_64_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option { let ret = match id { From 9075a172ad726672eb7e1d6e43e4c9a435368f7a Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Mon, 15 Apr 2019 15:45:09 +0800 Subject: [PATCH 04/11] Fix iovec size for MIPSn32. --- kernel/src/syscall/fs.rs | 3 +++ kernel/src/syscall/mod.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index b16fe73..d94e8b5 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -1074,6 +1074,9 @@ pub struct IoVec { /// Starting address base: *mut u8, /// Number of bytes to transfer + #[cfg(target_arch = "mips")] + len: u32, + #[cfg(not(target_arch = "mips"))] len: u64, } diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 11558d3..961f794 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -319,6 +319,9 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { #[cfg(target_arch = "mips")] fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option { let ret = match id { + SYS_OPEN => sys_open(args[0] as *const u8, args[1], args[2]), + SYS_DUP2 => sys_dup2(args[0], args[1]), + SYS_MMAP2 => sys_mmap(args[0], args[1], args[2], args[3], args[4], args[5] * 4096), SYS_SET_THREAD_AREA => { extern "C" { fn _cur_tls(); From dc90cb5a8d97c537ffd0df97a0845fc8bb57f354 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Mon, 15 Apr 2019 18:09:48 +0800 Subject: [PATCH 05/11] Fix pgfault, fstat syscall convention. biscuit/ls works! --- kernel/src/arch/mipsel/interrupt.rs | 16 +++++++ kernel/src/syscall/fs.rs | 67 ++++++++++++++++++++++++++++- kernel/src/syscall/mem.rs | 15 +++++++ kernel/src/syscall/mod.rs | 6 +++ 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/kernel/src/arch/mipsel/interrupt.rs b/kernel/src/arch/mipsel/interrupt.rs index 5ae84dd..1c45132 100644 --- a/kernel/src/arch/mipsel/interrupt.rs +++ b/kernel/src/arch/mipsel/interrupt.rs @@ -78,6 +78,9 @@ pub extern "C" fn rust_trap(tf: &mut TrapFrame) { tf.epc = tf.epc + 4; } } + E::CoprocessorUnusable => { + tf.epc = tf.epc + 4; + } _ => { error!("Unhandled Exception @ CPU{}: {:?} ", 0, tf.cause.cause()); crate::trap::error(tf) @@ -244,6 +247,19 @@ fn page_fault(tf: &mut TrapFrame) { tlb_entry.entry_lo0.get_pfn() << 12, tlb_entry.entry_lo1.get_pfn() << 12 ); + + let tlb_valid = if virt_addr.page_number() & 1 == 0 { + tlb_entry.entry_lo0.valid() + } else { + tlb_entry.entry_lo1.valid() + }; + + if !tlb_valid { + if !crate::memory::handle_page_fault(addr) { + crate::trap::error(tf); + } + } + tlb::write_tlb_random(tlb_entry) } Err(()) => { diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index d94e8b5..7035b31 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -913,7 +913,48 @@ pub struct Stat { ctime: Timespec, } -#[cfg(not(target_arch = "x86_64"))] +#[cfg(target_arch = "mips")] +#[repr(C)] +#[derive(Debug)] +pub struct Stat { + /// ID of device containing file + dev: u64, + /// padding + __pad1: u64, + /// inode number + ino: u64, + /// file type and mode + mode: StatMode, + /// number of hard links + nlink: u32, + + /// user ID of owner + uid: u32, + /// group ID of owner + gid: u32, + /// device ID (if special file) + rdev: u64, + /// padding + __pad2: u64, + /// total size, in bytes + size: u64, + + /// last access time + atime: Timespec, + /// last modification time + mtime: Timespec, + /// last status change time + ctime: Timespec, + + /// blocksize for filesystem I/O + blksize: u32, + /// padding + __pad3: u32, + /// number of 512B blocks allocated + blocks: u64, +} + +#[cfg(not(any(target_arch = "x86_64", target_arch = "mips"))] #[repr(C)] #[derive(Debug)] pub struct Stat { @@ -1042,7 +1083,29 @@ impl From for Stat { } } - #[cfg(not(target_arch = "x86_64"))] + #[cfg(target_arch = "mips")] + fn from(info: Metadata) -> Self { + Stat { + dev: info.dev as u64, + ino: info.inode as u64, + mode: StatMode::from_type_mode(info.type_, info.mode as u16), + nlink: info.nlinks as u32, + uid: info.uid as u32, + gid: info.gid as u32, + rdev: 0, + size: info.size as u64, + blksize: info.blk_size as u32, + blocks: info.blocks as u64, + atime: info.atime, + mtime: info.mtime, + ctime: info.ctime, + __pad1: 0, + __pad2: 0, + __pad3: 0, + } + } + + #[cfg(not(any(target_arch = "x86_64", target_arch="mips")))] fn from(info: Metadata) -> Self { Stat { dev: info.dev as u64, diff --git a/kernel/src/syscall/mem.rs b/kernel/src/syscall/mem.rs index 1c5bee9..0b471c2 100644 --- a/kernel/src/syscall/mem.rs +++ b/kernel/src/syscall/mem.rs @@ -123,6 +123,21 @@ bitflags! { } } +#[cfg(target_arch = "mips")] +bitflags! { + pub struct MmapFlags: usize { + /// Changes are shared. + const SHARED = 1 << 0; + /// Changes are private. + const PRIVATE = 1 << 1; + /// Place the mapping at the exact address + const FIXED = 1 << 4; + /// The mapping is not backed by any file. (non-POSIX) + const ANONYMOUS = 0x800; + } +} + +#[cfg(not(target_arch = "mips"))] bitflags! { pub struct MmapFlags: usize { /// Changes are shared. diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 961f794..efc5c0f 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -322,6 +322,12 @@ fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option sys_open(args[0] as *const u8, args[1], args[2]), SYS_DUP2 => sys_dup2(args[0], args[1]), SYS_MMAP2 => sys_mmap(args[0], args[1], args[2], args[3], args[4], args[5] * 4096), + SYS_FSTAT64 => sys_fstat(args[0], args[1] as *mut Stat), + SYS_STAT64 => sys_stat(args[0] as *const u8, args[1] as *mut Stat), + SYS_FCNTL64 => { + warn!("sys_fcntl64 is unimplemented"); + Ok(0) + } SYS_SET_THREAD_AREA => { extern "C" { fn _cur_tls(); From 9532311c1e4915341ad655f5a556e89daae22ba7 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Tue, 16 Apr 2019 01:31:00 +0800 Subject: [PATCH 06/11] Copy TLS data in sys_fork. --- kernel/src/arch/mipsel/context.rs | 4 +++- kernel/src/arch/mipsel/interrupt.rs | 3 ++- kernel/src/syscall/mod.rs | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/src/arch/mipsel/context.rs b/kernel/src/arch/mipsel/context.rs index eec9bde..f84ae70 100644 --- a/kernel/src/arch/mipsel/context.rs +++ b/kernel/src/arch/mipsel/context.rs @@ -120,6 +120,7 @@ impl InitStack { extern "C" { fn trap_return(); + fn _cur_tls(); } /// Saved registers for kernel context switches. @@ -231,8 +232,9 @@ impl Context { /// The SATP register will be set to `satp`. /// All the other registers are same as the original. pub unsafe fn new_fork(tf: &TrapFrame, kstack_top: usize, satp: usize) -> Self { + let tls = unsafe { *(_cur_tls as *const usize) }; InitStack { - context: ContextData::new(satp, 0), + context: ContextData::new(satp, tls), tf: { let mut tf = tf.clone(); // fork function's ret value, the new process is 0 diff --git a/kernel/src/arch/mipsel/interrupt.rs b/kernel/src/arch/mipsel/interrupt.rs index 1c45132..1036ffb 100644 --- a/kernel/src/arch/mipsel/interrupt.rs +++ b/kernel/src/arch/mipsel/interrupt.rs @@ -214,7 +214,6 @@ fn reserved_inst(tf: &mut TrapFrame) -> bool { if opcode == 0b011111 && format == 0b111011 { // RDHWR if rd == 29 && sel == 0 { - info!("Read TLS by rdhdr"); extern "C" { fn _cur_tls(); } @@ -222,7 +221,9 @@ fn reserved_inst(tf: &mut TrapFrame) -> bool { let tls = unsafe { *(_cur_tls as *const usize) }; + set_trapframe_register(rt, tls, tf); + info!("Read TLS by rdhdr {:x} to register {:?}", tls, rt); return true; } else { return false; diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index efc5c0f..6082afc 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -319,6 +319,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { #[cfg(target_arch = "mips")] fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option { let ret = match id { + SYS_FORK => sys_fork(tf), SYS_OPEN => sys_open(args[0] as *const u8, args[1], args[2]), SYS_DUP2 => sys_dup2(args[0], args[1]), SYS_MMAP2 => sys_mmap(args[0], args[1], args[2], args[3], args[4], args[5] * 4096), @@ -329,6 +330,7 @@ fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option { + info!("set_thread_area: tls: 0x{:x}", args[0]); extern "C" { fn _cur_tls(); } From f5fe3ce4f3f34f8c7a3f29674d27fce879de4013 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Tue, 16 Apr 2019 02:10:36 +0800 Subject: [PATCH 07/11] Move userstack to 0x60000000. --- kernel/src/arch/mipsel/consts.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/src/arch/mipsel/consts.rs b/kernel/src/arch/mipsel/consts.rs index ff2ef0c..84ae103 100644 --- a/kernel/src/arch/mipsel/consts.rs +++ b/kernel/src/arch/mipsel/consts.rs @@ -6,8 +6,8 @@ pub const KERNEL_OFFSET: usize = 0x80100000; pub const MEMORY_OFFSET: usize = 0x8000_0000; -pub const USER_STACK_OFFSET: usize = 0x80000000 - USER_STACK_SIZE; +pub const USER_STACK_OFFSET: usize = 0x70000000 - USER_STACK_SIZE; pub const USER_STACK_SIZE: usize = 0x10000; -pub const USER32_STACK_OFFSET: usize = 0x80000000 - USER_STACK_SIZE; +pub const USER32_STACK_OFFSET: usize = 0x70000000 - USER_STACK_SIZE; pub const MAX_DTB_SIZE: usize = 0x2000; From 189c4aa5c6ba13380b49a8aa1a7b527dcc3a85fb Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Tue, 16 Apr 2019 02:42:44 +0800 Subject: [PATCH 08/11] Support sys_pipe for MIPS. --- kernel/src/syscall/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 6082afc..86666ba 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -325,10 +325,23 @@ fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option sys_mmap(args[0], args[1], args[2], args[3], args[4], args[5] * 4096), SYS_FSTAT64 => sys_fstat(args[0], args[1] as *mut Stat), SYS_STAT64 => sys_stat(args[0] as *const u8, args[1] as *mut Stat), + SYS_PIPE => { + let fd_ptr = args[0] as *mut u32; + match sys_pipe(fd_ptr) { + Ok(code) => { + unsafe { + tf.v0 = *fd_ptr as usize; + tf.v1 = *(fd_ptr.add(1)) as usize; + } + Ok(tf.v0) + }, + Err(err) => Err(err) + } + }, SYS_FCNTL64 => { warn!("sys_fcntl64 is unimplemented"); Ok(0) - } + }, SYS_SET_THREAD_AREA => { info!("set_thread_area: tls: 0x{:x}", args[0]); extern "C" { From 3e7dd81dec5121fda7f606d0912ad489b82cfae7 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Wed, 17 Apr 2019 17:37:28 +0800 Subject: [PATCH 09/11] Add lstat, fix timespec for musln32. --- kernel/src/syscall/fs.rs | 14 +++++++++++--- kernel/src/syscall/mod.rs | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index 7035b31..1017332 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -3,6 +3,7 @@ use core::cell::UnsafeCell; use core::cmp::min; use core::mem::size_of; +#[cfg(not(target_arch = "mips"))] use rcore_fs::vfs::Timespec; use crate::drivers::SOCKET_ACTIVITY; @@ -913,6 +914,13 @@ pub struct Stat { ctime: Timespec, } +#[cfg(target_arch = "mips")] +#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] +pub struct Timespec { + pub sec: i32, + pub nsec: i32, +} + #[cfg(target_arch = "mips")] #[repr(C)] #[derive(Debug)] @@ -1096,9 +1104,9 @@ impl From for Stat { size: info.size as u64, blksize: info.blk_size as u32, blocks: info.blocks as u64, - atime: info.atime, - mtime: info.mtime, - ctime: info.ctime, + atime: Timespec { sec: info.atime.sec as i32, nsec: info.atime.nsec }, + mtime: Timespec { sec: info.mtime.sec as i32, nsec: info.mtime.nsec }, + ctime: Timespec { sec: info.ctime.sec as i32, nsec: info.ctime.nsec }, __pad1: 0, __pad2: 0, __pad3: 0, diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 86666ba..49051ec 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -324,6 +324,7 @@ fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option sys_dup2(args[0], args[1]), SYS_MMAP2 => sys_mmap(args[0], args[1], args[2], args[3], args[4], args[5] * 4096), SYS_FSTAT64 => sys_fstat(args[0], args[1] as *mut Stat), + SYS_LSTAT64 => sys_lstat(args[0] as *const u8, args[1] as *mut Stat), SYS_STAT64 => sys_stat(args[0] as *const u8, args[1] as *mut Stat), SYS_PIPE => { let fd_ptr = args[0] as *mut u32; From d2f2fa3d9744f25418438f195513be94dedd1f17 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Wed, 17 Apr 2019 17:42:05 +0800 Subject: [PATCH 10/11] Two syscall for mips. --- kernel/src/syscall/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 49051ec..4f81361 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -339,6 +339,14 @@ fn mips_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option Err(err) } }, + SYS_GETPGID => { + warn!("sys_getpgid is unimplemented"); + Ok(0) + } + SYS_SETPGID => { + warn!("sys_setpgid is unimplemented"); + Ok(0) + } SYS_FCNTL64 => { warn!("sys_fcntl64 is unimplemented"); Ok(0) From 218bb29cf337e9db010369155bbd99602aec6054 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Sun, 21 Apr 2019 14:30:02 +0800 Subject: [PATCH 11/11] Fix TimeSpec type. --- kernel/src/syscall/time.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/src/syscall/time.rs b/kernel/src/syscall/time.rs index 2ae76d4..edea0f6 100644 --- a/kernel/src/syscall/time.rs +++ b/kernel/src/syscall/time.rs @@ -53,20 +53,20 @@ impl TimeVal { #[repr(C)] #[derive(Debug, Copy, Clone)] pub struct TimeSpec { - sec: u64, - nsec: u64, + sec: usize, + nsec: usize, } impl TimeSpec { pub fn to_duration(&self) -> Duration { - Duration::new(self.sec, self.nsec as u32) + Duration::new(self.sec as u64, self.nsec as u32) } pub fn get_epoch() -> Self { let usec = get_epoch_usec(); TimeSpec { - sec: usec / USEC_PER_SEC, - nsec: usec % USEC_PER_SEC * NSEC_PER_USEC, + sec: (usec / USEC_PER_SEC) as usize, + nsec: (usec % USEC_PER_SEC * NSEC_PER_USEC) as usize, } } }