Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 2 Aug 2018 12:08:52 +0000 (UTC)
From:      Ruslan Bukin <br@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r337125 - head/sys/riscv/riscv
Message-ID:  <201808021208.w72C8qZR060202@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808021208.w72C8qZR060202>