Author: br
Date: Thu Aug  2 12:08:52 2018
New Revision: 337125
URL: https://svnweb.freebsd.org/changeset/base/337125

Log:
  o Correctly set user tls base: consider TP_OFFSET.
  o Ensure tp (thread pointer) saved before copying the pcb.
  
  Sponsored by: DARPA, AFRL

Modified:
  head/sys/riscv/riscv/vm_machdep.c

Modified: head/sys/riscv/riscv/vm_machdep.c
==============================================================================
--- head/sys/riscv/riscv/vm_machdep.c   Thu Aug  2 11:55:16 2018        
(r337124)
+++ head/sys/riscv/riscv/vm_machdep.c   Thu Aug  2 12:08:52 2018        
(r337125)
@@ -55,6 +55,10 @@ __FBSDID("$FreeBSD$");
 #include <machine/frame.h>
 #include <machine/sbi.h>
 
+#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 t
 {
        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);
 }
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to