Module Name:    src
Committed By:   rin
Date:           Sun Jun 21 00:40:00 UTC 2020

Modified Files:
        src/sys/arch/powerpc/include: mcontext.h ptrace.h types.h
        src/sys/arch/powerpc/powerpc: sig_machdep.c

Log Message:
Fix inconsistency b/w kernel and userland recognitions of TLS, as well as
inconsistency whether it is biased or not in kernel.

- Obsolete __lwp_settcb() in order to let kernel know new TLS address via
  _lwp_setprivate(2). Alternatively, we can call _lwp_setprivate(2) within
  __lwp_settcb() like mips, but it is just double handling; we adjust %r2
  appropriately in _lwp_setprivate(2) via cpu_lwp_setprivate().

- Make sure that, like other ports, l_private represents address of tcb,
  not biased one as in %r2. This guarantees that the returned values from
  _lwp_getprivate(2) and __lwp_getprivate_fast() are always same. Also,
  we can obsolete PTRACE_LWP_GETPRIVATE() macro.

Now, *_pl_private tests in tests/lib/libc/sys successfully pass, while
no other tests become newly falling.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/powerpc/include/mcontext.h
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/powerpc/include/ptrace.h
cvs rdiff -u -r1.62 -r1.63 src/sys/arch/powerpc/include/types.h
cvs rdiff -u -r1.49 -r1.50 src/sys/arch/powerpc/powerpc/sig_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/powerpc/include/mcontext.h
diff -u src/sys/arch/powerpc/include/mcontext.h:1.19 src/sys/arch/powerpc/include/mcontext.h:1.20
--- src/sys/arch/powerpc/include/mcontext.h:1.19	Sun Jun 21 00:00:27 2020
+++ src/sys/arch/powerpc/include/mcontext.h	Sun Jun 21 00:39:59 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: mcontext.h,v 1.19 2020/06/21 00:00:27 rin Exp $	*/
+/*	$NetBSD: mcontext.h,v 1.20 2020/06/21 00:39:59 rin Exp $	*/
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -141,7 +141,8 @@ typedef struct {
 
 #define	_UC_MACHINE_SET_PC(uc, pc)	_UC_MACHINE_PC(uc) = (pc)
 
-#if defined(_RTLD_SOURCE) || defined(_LIBC_SOURCE) || defined(__LIBPTHREAD_SOURCE__)
+#if defined(_KERNEL) || \
+defined(_RTLD_SOURCE) || defined(_LIBC_SOURCE) || defined(__LIBPTHREAD_SOURCE__)
 #include <sys/tls.h>
 
 /*
@@ -153,6 +154,7 @@ typedef struct {
 #define	TLS_DTV_OFFSET	0x8000
 __CTASSERT(TLS_TP_OFFSET + sizeof(struct tls_tcb) < 0x8000);
 
+#ifndef _KERNEL
 static __inline void *
 __lwp_gettcb_fast(void)
 {
@@ -165,16 +167,7 @@ __lwp_gettcb_fast(void)
 
 	return __tcb;
 }
-
-static __inline void
-__lwp_settcb(void *__tcb)
-{
-	__asm __volatile(
-		"addi %%r2,%[__tcb],%[__offset]"
-	    :
-	    :	[__tcb] "r" (__tcb),
-		[__offset] "n" (TLS_TP_OFFSET + sizeof(struct tls_tcb)));
-}
-#endif /* _RTLD_SOURCE || _LIBC_SOURCE || __LIBPTHREAD_SOURCE__ */
+#endif /* !_KERNEL */
+#endif /* _KERNEL || _RTLD_SOURCE || _LIBC_SOURCE || __LIBPTHREAD_SOURCE__ */
 
 #endif	/* !_POWERPC_MCONTEXT_H_ */

Index: src/sys/arch/powerpc/include/ptrace.h
diff -u src/sys/arch/powerpc/include/ptrace.h:1.16 src/sys/arch/powerpc/include/ptrace.h:1.17
--- src/sys/arch/powerpc/include/ptrace.h:1.16	Tue Dec 24 14:50:59 2019
+++ src/sys/arch/powerpc/include/ptrace.h	Sun Jun 21 00:39:59 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: ptrace.h,v 1.16 2019/12/24 14:50:59 kamil Exp $	*/
+/*	$NetBSD: ptrace.h,v 1.17 2020/06/21 00:39:59 rin Exp $	*/
 
 #ifndef _POWERPC_PTRACE_H
 #define	_POWERPC_PTRACE_H
@@ -77,8 +77,4 @@ int procfs_machdep_validvecregs(struct l
 #define PTRACE_BREAKPOINT_ASM	__asm __volatile("trap")
 #define PTRACE_BREAKPOINT_SIZE	4
 
-#ifdef _KERNEL
-#define PTRACE_LWP_GETPRIVATE(l) (l)->l_md.md_utf->tf_fixreg[_REG_R2]
-#endif
-
 #endif /* _POWERPC_PTRACE_H */

Index: src/sys/arch/powerpc/include/types.h
diff -u src/sys/arch/powerpc/include/types.h:1.62 src/sys/arch/powerpc/include/types.h:1.63
--- src/sys/arch/powerpc/include/types.h:1.62	Thu Apr 16 22:11:12 2020
+++ src/sys/arch/powerpc/include/types.h	Sun Jun 21 00:39:59 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: types.h,v 1.62 2020/04/16 22:11:12 rin Exp $	*/
+/*	$NetBSD: types.h,v 1.63 2020/06/21 00:39:59 rin Exp $	*/
 
 /*-
  * Copyright (C) 1995 Wolfgang Solfrank.
@@ -89,7 +89,6 @@ typedef __uint32_t tlb_asid_t;		/* for b
 #define	__HAVE_CPU_LWP_SETPRIVATE
 #define	__HAVE_COMMON___TLS_GET_ADDR
 #define	__HAVE___LWP_GETTCB_FAST
-#define	__HAVE___LWP_SETTCB
 #define	__HAVE_TLS_VARIANT_I
 
 #if defined(_KERNEL) || defined(_KMEMUSER)

Index: src/sys/arch/powerpc/powerpc/sig_machdep.c
diff -u src/sys/arch/powerpc/powerpc/sig_machdep.c:1.49 src/sys/arch/powerpc/powerpc/sig_machdep.c:1.50
--- src/sys/arch/powerpc/powerpc/sig_machdep.c:1.49	Sun Jun 21 00:00:27 2020
+++ src/sys/arch/powerpc/powerpc/sig_machdep.c	Sun Jun 21 00:40:00 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: sig_machdep.c,v 1.49 2020/06/21 00:00:27 rin Exp $	*/
+/*	$NetBSD: sig_machdep.c,v 1.50 2020/06/21 00:40:00 rin Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.49 2020/06/21 00:00:27 rin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.50 2020/06/21 00:40:00 rin Exp $");
 
 #include "opt_ppcarch.h"
 #include "opt_altivec.h"
@@ -243,8 +243,9 @@ cpu_setmcontext(struct lwp *l, const mco
 #endif
 	}
 
+#define TLS_BIAS (TLS_TP_OFFSET + sizeof(struct tls_tcb))
 	if (flags & _UC_TLSBASE)
-		lwp_setprivate(l, (void *)(uintptr_t)gr[_REG_R2]);
+		lwp_setprivate(l, (void *)((uintptr_t)gr[_REG_R2] - TLS_BIAS));
 
 #ifdef PPC_HAVE_FPU
 	/* Restore FPU context, if any. */
@@ -279,6 +280,8 @@ cpu_lwp_setprivate(lwp_t *l, void *addr)
 {
 	struct trapframe * const tf = l->l_md.md_utf;
 
-	tf->tf_fixreg[_REG_R2] = (register_t)addr;
+	tf->tf_fixreg[_REG_R2] = (register_t)addr + TLS_BIAS;
+#undef TLS_BIAS
+
 	return 0;
 }

Reply via email to