Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src setcontext() used to be incompatible with -lpthread since it...



details:   https://anonhg.NetBSD.org/src/rev/f306f12b08cb
branches:  trunk
changeset: 781513:f306f12b08cb
user:      manu <manu%NetBSD.org@localhost>
date:      Wed Sep 12 02:00:51 2012 +0000

description:
setcontext() used to be incompatible with -lpthread since it affected
the TLS pointer, therefore wrecking the pthread environement.

Some ports had _UC_TLSBASE flag or equivalent (_UC_UNIQUE on alpha)
that controlled whether setcontext() would change the TLS pointer.
This change let libpthread override setcontext() with its own version
that unsets _UC_TLSBASE, enabling safe usage of setcontext() with
-lpthread.

We also have the following required changes here:
- rename alpha's _UC_UNIQUE into _UC_TLSBASE
- add _UC_TLSBASE definition in header file for all ports
  (powerpc, sh3, sparc and sparc64 lack the implementation for now)
- introduce a libc stub that can be overriden for setcontext()
- modify MD libcs swapcontext() implementations so that they use the
  setcontext() libc stub instead of doing a plain system call.

While we are there:
- document various MD _UC_* flags in header file
- add libc and libpthread tests for swapcontext() behavior
  (hopefully helpful to spot MD problems introduced with this change)

Future work:
- Deciding whether kernel support or _UC_TLSBASE should be added for
  powerpc, sh3, sparc and sparc64 is left to portmasters
  sparc64

Approved by core@

diffstat:

 distrib/sets/lists/tests/mi               |    6 +-
 lib/libc/arch/hppa/gen/swapcontext.S      |   13 ++-
 lib/libc/arch/mips/gen/_resumecontext.S   |    6 +-
 lib/libc/arch/mips/gen/swapcontext.S      |    6 +-
 lib/libc/arch/powerpc64/gen/swapcontext.S |    8 +-
 lib/libc/arch/sparc/gen/swapcontext.S     |   19 ++-
 lib/libc/arch/sparc64/gen/swapcontext.S   |   23 +++-
 lib/libc/include/extern.h                 |    5 +-
 lib/libc/sys/Makefile.inc                 |    6 +-
 lib/libpthread/pthread_specific.c         |   24 ++++-
 sys/arch/alpha/alpha/machdep.c            |    8 +-
 sys/arch/alpha/include/mcontext.h         |    4 +-
 sys/arch/sh3/include/mcontext.h           |    3 +-
 sys/arch/sparc/include/mcontext.h         |    3 +-
 sys/arch/sparc64/include/mcontext.h       |    3 +-
 sys/sys/ucontext.h                        |   51 ++++++++--
 tests/lib/libc/sys/Makefile               |    3 +-
 tests/lib/libc/sys/t_swapcontext.c        |  133 ++++++++++++++++++++++++++++++
 tests/lib/libpthread/Makefile             |    3 +-
 tests/lib/libpthread/t_swapcontext.c      |  111 +++++++++++++++++++++++++
 20 files changed, 380 insertions(+), 58 deletions(-)

diffs (truncated from 802 to 300 lines):

diff -r 86a6973d7ef9 -r f306f12b08cb distrib/sets/lists/tests/mi
--- a/distrib/sets/lists/tests/mi       Wed Sep 12 00:50:30 2012 +0000
+++ b/distrib/sets/lists/tests/mi       Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.490 2012/08/26 23:03:21 jmmv Exp $
+# $NetBSD: mi,v 1.491 2012/09/12 02:00:51 manu Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 #
@@ -573,6 +573,7 @@
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_sigqueue.debug            tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_socketpair.debug          tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_stat.debug                        tests-lib-debug         debug,atf
+./usr/libdata/debug/usr/tests/lib/libc/sys/t_swapcontext.debug         tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_timer_create.debug                tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_truncate.debug            tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libc/sys/t_ucontext.debug            tests-lib-debug         debug,atf
@@ -674,6 +675,7 @@
 ./usr/libdata/debug/usr/tests/lib/libpthread/t_siglongjmp.debug                tests-lib-tests         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libpthread/t_sleep.debug             tests-lib-tests         debug,atf
 ./usr/libdata/debug/usr/tests/lib/libpthread/t_status.debug            tests-obsolete          obsolete
+./usr/libdata/debug/usr/tests/lib/libpthread/t_swapcontext.debug       tests-lib-tests         debug,atf
 ./usr/libdata/debug/usr/tests/lib/librt                                        tests-lib-debug
 ./usr/libdata/debug/usr/tests/lib/librt/t_sched.debug                  tests-lib-debug         debug,atf
 ./usr/libdata/debug/usr/tests/lib/librt/t_sem.debug                    tests-lib-debug         debug,atf
@@ -2486,6 +2488,7 @@
 ./usr/tests/lib/libc/sys/t_sigqueue            tests-lib-tests         atf
 ./usr/tests/lib/libc/sys/t_socketpair          tests-lib-tests         atf
 ./usr/tests/lib/libc/sys/t_stat                        tests-lib-tests         atf
+./usr/tests/lib/libc/sys/t_swapcontext         tests-lib-tests         atf
 ./usr/tests/lib/libc/sys/t_timer_create                tests-lib-tests         atf
 ./usr/tests/lib/libc/sys/t_truncate            tests-lib-tests         atf
 ./usr/tests/lib/libc/sys/t_ucontext            tests-lib-tests         atf
@@ -2714,6 +2717,7 @@
 ./usr/tests/lib/libpthread/t_siglongjmp                tests-lib-tests         atf
 ./usr/tests/lib/libpthread/t_sleep             tests-lib-tests         atf
 ./usr/tests/lib/libpthread/t_status            tests-obsolete          obsolete
+./usr/tests/lib/libpthread/t_swapcontext       tests-lib-tests         atf
 ./usr/tests/lib/librt                          tests-lib-tests         atf
 ./usr/tests/lib/librt/Atffile                  tests-lib-tests         atf
 ./usr/tests/lib/librt/t_sched                  tests-lib-tests         atf
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/arch/hppa/gen/swapcontext.S
--- a/lib/libc/arch/hppa/gen/swapcontext.S      Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/arch/hppa/gen/swapcontext.S      Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: swapcontext.S,v 1.4 2008/04/28 20:22:56 martin Exp $   */
+/*     $NetBSD: swapcontext.S,v 1.5 2012/09/12 02:00:52 manu Exp $     */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #include <machine/mcontext.h>
 
 #if defined(SYSLIBC_SCCS) && !defined(lint)
-       RCSID("$NetBSD: swapcontext.S,v 1.4 2008/04/28 20:22:56 martin Exp $")
+       RCSID("$NetBSD: swapcontext.S,v 1.5 2012/09/12 02:00:52 manu Exp $")
 #endif /* SYSLIBC_SCCS && !lint */
 
 LEAF_ENTRY(swapcontext)
@@ -44,10 +44,15 @@
 
        ldw     HPPA_FRAME_ARG(0)(%sp), %arg1
        ldw     HPPA_FRAME_ARG(1)(%sp), %arg0
-       stw     %r0, (_OFFSETOF_UC_GREGS + _REG_RET0 * SZREG)(%arg1)
+
+       ldw     HPPA_FRAME_CRP(%sp), %rp
        ldo     4(%rp), %r1
        stw     %r1, (_OFFSETOF_UC_GREGS + _REG_PCOQT * SZREG)(%arg1)
        stw     %rp, (_OFFSETOF_UC_GREGS + _REG_PCOQH * SZREG)(%arg1)
-       SYSCALL(setcontext)
+       bl      setcontext, %rp
+       stw     %r0, (_OFFSETOF_UC_GREGS + _REG_RET0 * SZREG)(%arg1)
+
+       ldw     HPPA_FRAME_CRP(%sp), %rp
+       bv,n    %r0(%rp)
 EXIT(swapcontext)
        .end
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/arch/mips/gen/_resumecontext.S
--- a/lib/libc/arch/mips/gen/_resumecontext.S   Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/arch/mips/gen/_resumecontext.S   Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: _resumecontext.S,v 1.6 2009/12/14 01:07:42 matt Exp $  */
+/*     $NetBSD: _resumecontext.S,v 1.7 2012/09/12 02:00:52 manu Exp $  */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
 #include "assym.h"
 
 #if defined(SYSLIBC_SCCS) && !defined(lint)
-       RCSID("$NetBSD: _resumecontext.S,v 1.6 2009/12/14 01:07:42 matt Exp $")
+       RCSID("$NetBSD: _resumecontext.S,v 1.7 2012/09/12 02:00:52 manu Exp $")
 #endif /* SYSLIBC_SCCS && !lint */
 
        .set    reorder
@@ -53,7 +53,7 @@
        PTR_L           a0, _OFFSETOF_UC_LINK(a0)       # linked context?
        beq             a0, zero, 1f                    #   nope, exit process
        nop
-       SYSTRAP(setcontext)                             #   yes, become it.
+       PIC_TAILCALL(setcontext)                        #   yes, become it.
        /* NOTREACHED (in theory) */
        li              a0, -1                          # failure, 
 1:
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/arch/mips/gen/swapcontext.S
--- a/lib/libc/arch/mips/gen/swapcontext.S      Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/arch/mips/gen/swapcontext.S      Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: swapcontext.S,v 1.4 2009/12/14 01:07:42 matt Exp $     */
+/*     $NetBSD: swapcontext.S,v 1.5 2012/09/12 02:00:52 manu Exp $     */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #include "assym.h"
 
 #if defined(SYSLIBC_SCCS) && !defined(lint)
-       RCSID("$NetBSD: swapcontext.S,v 1.4 2009/12/14 01:07:42 matt Exp $")
+       RCSID("$NetBSD: swapcontext.S,v 1.5 2012/09/12 02:00:52 manu Exp $")
 #endif /* SYSLIBC_SCCS && !lint */
 
        .set    reorder
@@ -57,7 +57,7 @@
        REG_S           v0, _OFFSETOF_UC_GREGS_SP(v1)
        REG_EPILOGUE
 
-       SYSTRAP(setcontext)
+       PIC_TAILCALL(setcontext)
        /* NOTREACHED */
 1:
        PTR_ADDU        sp, sp, CALLFRAME_SIZ
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/arch/powerpc64/gen/swapcontext.S
--- a/lib/libc/arch/powerpc64/gen/swapcontext.S Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/arch/powerpc64/gen/swapcontext.S Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $   */
+/*     $NetBSD: swapcontext.S,v 1.4 2012/09/12 02:00:52 manu Exp $     */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 
 #if defined(LIBC_SCCS) && !defined(lint)
        .text
-       .asciz "$NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $"
+       .asciz "$NetBSD: swapcontext.S,v 1.4 2012/09/12 02:00:52 manu Exp $"
 #endif /* LIBC_SCCS && !lint */
 
 ENTRY(swapcontext)
@@ -52,8 +52,8 @@
        std     %r0,mc_off+34*8(%r11)   #       pc <- lr
        la      %r0,16(%r1)
        std     %r0,mc_off+1*8(%r11)            # adjust sp
-       ld      %r3,SF_PARAM+8(%r1)                     # load ucp
-       bl      .setcontext                     # setcontext(ucp)
+       ld      %r3,SF_PARAM+8(%r1)             # load ucp
+       bl      PIC_PLT(_C_LABEL(setcontext))   # setcontext(ucp)
        nop
 1:
        ld      %r0,SF_SZ+SF_LR(%r1)
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/arch/sparc/gen/swapcontext.S
--- a/lib/libc/arch/sparc/gen/swapcontext.S     Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/arch/sparc/gen/swapcontext.S     Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $   */
+/*     $NetBSD: swapcontext.S,v 1.4 2012/09/12 02:00:53 manu Exp $     */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 #include "SYS.h"
 
 #if defined(LIBC_SCCS) && !defined(lint)
-       RCSID("$NetBSD: swapcontext.S,v 1.3 2008/04/28 20:22:57 martin Exp $")
+       RCSID("$NetBSD: swapcontext.S,v 1.4 2012/09/12 02:00:53 manu Exp $")
 #endif /* LIBC_SCCS && !lint */
 
 ENTRY(swapcontext)
@@ -49,8 +49,13 @@
        add     %o7, 8, %o1
        st      %o1, [%o2 + 40 + 1 * 4]         ! gr[_REG_PC] = retaddr
 
-       mov     %o3, %o0
-       mov     %o1, %g2                        ! optimize `return'
-       mov     SYS_setcontext|SYSCALL_G2RFLAG, %g1
-       t       ST_SYSCALL
-       ERROR()
+#ifdef PIC
+       PIC_PROLOGUE(%g1, %g2)                  ! %g1 = _GLOBAL_OFFSET_TABLE
+       set     _C_LABEL(setcontext), %g2
+       ld      [%g1 + %g2], %g1
+       jmp     %g1
+        mov    %o3, %o0
+#else
+       jmp     _C_LABEL(setcontext)
+        mov    %o3, %o0
+#endif
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/arch/sparc64/gen/swapcontext.S
--- a/lib/libc/arch/sparc64/gen/swapcontext.S   Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/arch/sparc64/gen/swapcontext.S   Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: swapcontext.S,v 1.5 2011/03/28 11:19:13 martin Exp $   */
+/*     $NetBSD: swapcontext.S,v 1.6 2012/09/12 02:00:53 manu Exp $     */
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
 #include "SYS.h"
 
 #if defined(LIBC_SCCS) && !defined(lint)
-       RCSID("$NetBSD: swapcontext.S,v 1.5 2011/03/28 11:19:13 martin Exp $")
+       RCSID("$NetBSD: swapcontext.S,v 1.6 2012/09/12 02:00:53 manu Exp $")
 #endif /* LIBC_SCCS && !lint */
 
 
@@ -50,8 +50,17 @@
        add     %o7, 8, %o1
        stx     %o1, [%o2 + 64 + 1 * 8]         /* gr[_REG_PC] = retaddr */
 
-       mov     %o3, %o0
-       mov     %o1, %g5                        /* optimize `return' */
-       mov     SYS_setcontext|SYSCALL_G5RFLAG, %g1
-       t       ST_SYSCALL
-       ERROR()
+#ifdef PIC
+       PIC_PROLOGUE(%g5,%o4)                   /* %g5 = _GLOBAL_OFFSET_TABLE */
+#ifdef BIGPIC
+       set     _C_LABEL(setcontext), %o6
+       ldx     [%g5+%o6], %o5
+#else
+       ldx     [%g5+_C_LABEL(setcontext)], %o5
+#endif
+       jmp     %o5
+        mov    %o3, %o0
+#else
+       ba,a    _C_LABEL(setcontext)
+        mov    %o3, %o0
+#endif
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/include/extern.h
--- a/lib/libc/include/extern.h Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/include/extern.h Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.19 2011/09/19 21:24:58 joerg Exp $        */
+/*     $NetBSD: extern.h,v 1.20 2012/09/12 02:00:53 manu Exp $ */
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -25,6 +25,7 @@
  */
 
 #include <stdarg.h>
+#include <ucontext.h>
 
 __BEGIN_DECLS
 extern char *__minbrk;
@@ -67,4 +68,6 @@
 void   _malloc_prefork(void);
 void   _malloc_postfork(void);
 
+int    _sys_setcontext(const ucontext_t *);
+
 __END_DECLS
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libc/sys/Makefile.inc
--- a/lib/libc/sys/Makefile.inc Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libc/sys/Makefile.inc Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.inc,v 1.215 2012/06/22 18:28:38 christos Exp $
+#      $NetBSD: Makefile.inc,v 1.216 2012/09/12 02:00:53 manu Exp $
 #      @(#)Makefile.inc        8.3 (Berkeley) 10/24/94
 
 # sys sources
@@ -120,7 +120,7 @@
        rasctl.S readlinkat.S reboot.S recvfrom.S recvmmsg.S recvmsg.S rename.S \
                renameat.S revoke.S rmdir.S \
        semconfig.S semget.S semop.S \
-               sendmmsg.S sendmsg.S sendto.S setegid.S setcontext.S seteuid.S \
+               sendmmsg.S sendmsg.S sendto.S setegid.S seteuid.S \
                setgid.S setgroups.S __setitimer50.S __setlogin.S setpgid.S \
                setpriority.S \
                setregid.S setreuid.S setrlimit.S setsid.S setsockopt.S \
@@ -156,7 +156,7 @@
        __pollts50.S __pselect50.S read.S readlink.S \
        readv.S _sched_setparam.S _sched_getparam.S _sched_setaffinity.S \
        _sched_getaffinity.S sched_yield.S \
-       __select50.S __sigprocmask14.S __sigsuspend14.S sysarch.S \
+       __select50.S setcontext.S __sigprocmask14.S __sigsuspend14.S sysarch.S \
        __wait450.S write.S writev.S
 
 NOERR= getegid.S geteuid.S getgid.S getpid.S getppid.S getuid.S \
diff -r 86a6973d7ef9 -r f306f12b08cb lib/libpthread/pthread_specific.c
--- a/lib/libpthread/pthread_specific.c Wed Sep 12 00:50:30 2012 +0000
+++ b/lib/libpthread/pthread_specific.c Wed Sep 12 02:00:51 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_specific.c,v 1.21 2008/06/23 10:38:39 ad Exp $ */
+/*     $NetBSD: pthread_specific.c,v 1.22 2012/09/12 02:00:53 manu Exp $       */
 
 /*-
  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
@@ -30,19 +30,26 @@
  */



Home | Main Index | Thread Index | Old Index