From 7bb4a84ad3e1ed116f7e46e47a3aa77e6b4740f3 Mon Sep 17 00:00:00 2001 From: Ruslan Bukin Date: Thu, 2 Aug 2018 12:08:52 +0000 Subject: o Correctly set user tls base: consider TP_OFFSET. o Ensure tp (thread pointer) saved before copying the pcb. Sponsored by: DARPA, AFRL --- sys/riscv/riscv/vm_machdep.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/sys/riscv/riscv/vm_machdep.c b/sys/riscv/riscv/vm_machdep.c index 73bb5aae7b6cc..9ba560d43a493 100644 --- a/sys/riscv/riscv/vm_machdep.c +++ b/sys/riscv/riscv/vm_machdep.c @@ -55,6 +55,10 @@ __FBSDID("$FreeBSD$"); #include #include +#if __riscv_xlen == 64 +#define TP_OFFSET 16 /* sizeof(struct tcb) */ +#endif + /* * Finish a fork operation, with process p2 nearly set up. * Copy and update the pcb, set up the stack so that the child @@ -65,10 +69,23 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) { struct pcb *pcb2; struct trapframe *tf; + register_t val; if ((flags & RFPROC) == 0) return; + if (td1 == curthread) { + /* + * Save the tp. These normally happen in cpu_switch, + * but if userland changes this then forks this may + * not have happened. + */ + __asm __volatile("mv %0, tp" : "=&r"(val)); + td1->td_pcb->pcb_tp = val; + + /* RISCVTODO: save the FPU state here */ + } + pcb2 = (struct pcb *)(td2->td_kstack + td2->td_kstack_pages * PAGE_SIZE) - 1; @@ -198,7 +215,9 @@ cpu_set_user_tls(struct thread *td, void *tls_base) return (EINVAL); pcb = td->td_pcb; - pcb->pcb_tp = (register_t)tls_base; + pcb->pcb_tp = (register_t)tls_base + TP_OFFSET; + if (td == curthread) + __asm __volatile("mv tp, %0" :: "r"(pcb->pcb_tp)); return (0); } -- cgit v1.3