From 9532311c1e4915341ad655f5a556e89daae22ba7 Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Tue, 16 Apr 2019 01:31:00 +0800 Subject: [PATCH] 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(); }