From patchwork Fri Jun 6 22:58:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roland McGrath X-Patchwork-Id: 1364 Received: (qmail 2087 invoked by alias); 6 Jun 2014 22:58:17 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 2074 invoked by uid 89); 6 Jun 2014 22:58:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00 autolearn=ham version=3.3.2 X-HELO: topped-with-meat.com MIME-Version: 1.0 From: Roland McGrath To: "GNU C. Library" Subject: [PATCH roland/tls-init] Start cleaning up TLS initial value for pthread_create. Message-Id: <20140606225811.36E5F2C39EF@topped-with-meat.com> Date: Fri, 6 Jun 2014 15:58:11 -0700 (PDT) X-CMAE-Score: 0 X-CMAE-Analysis: v=2.1 cv=SvUDtp+0 c=1 sm=1 tr=0 a=WkljmVdYkabdwxfqvArNOQ==:117 a=14OXPxybAAAA:8 a=uOJx6WEgtsUA:10 a=Z6MIti7PxpgA:10 a=kj9zAlcOel0A:10 a=hOe2yjtxAAAA:8 a=20KFwNOVAAAA:8 a=mDV3o1hIAAAA:8 a=WSmTLRYD8rlKQtl5kEUA:9 a=mNbTRksBuNVGZapB:21 a=MRhG7modk-Gq5enb:21 a=CjuIK1q_8ugA:10 a=jEp0ucaQiEUA:10 This replaces the TLS_VALUE macro in createthread.c with a new macro TLS_DEFINE_INIT_TP, defined in tls.h. This consolidates the knowledge of machine-specific TLS details into tls.h where it belongs. It also gets rid of the need for any per-machine createthread.c files. Verified on x86_64-linux-gnu and i686-linux-gnu that there are no significant code changes in libpthread.so. If nobody objects, then I'll commit this on Monday and follow up with TLS_DEFINE_INIT_TP definitions for the other machines. Thanks, Roland 2014-06-06 Roland McGrath * nptl/sysdeps/pthread/createthread.c (TLS_DEFINE_INIT_TP): New macro, if not already defined. (do_clone): Use that in place of PREPARE_CREATE and TLS_VALUE. * nptl/sysdeps/x86_64/tls.h (TLS_DEFINE_INIT_TP): New macro. * nptl/sysdeps/i386/tls.h (tls_fill_user_desc): New function. (TLS_INIT_TP): Use it. (TLS_DEFINE_INIT_TP): New macro. * sysdeps/unix/sysv/linux/i386/createthread.c: File removed. --- a/nptl/sysdeps/i386/tls.h +++ b/nptl/sysdeps/i386/tls.h @@ -192,6 +192,26 @@ union user_desc_init # endif #endif +static inline void __attribute__ ((unused, always_inline)) +tls_fill_user_desc (union user_desc_init *desc, + unsigned int entry_number, + void *pd) +{ + desc->vals[0] = entry_number; + /* The 'base_addr' field. Pointer to the TCB. */ + desc->vals[1] = (unsigned long int) pd; + /* The 'limit' field. We use 4GB which is 0xfffff pages. */ + desc->vals[2] = 0xfffff; + /* Collapsed value of the bitfield: + .seg_32bit = 1 + .contents = 0 + .read_exec_only = 0 + .limit_in_pages = 1 + .seg_not_present = 0 + .useable = 1 */ + desc->vals[3] = 0x51; +} + /* Code to initially initialize the thread pointer. This might need special attention since 'errno' is not yet available and if the operation can cause a failure 'errno' must not be touched. */ @@ -207,20 +227,8 @@ union user_desc_init /* New syscall handling support. */ \ INIT_SYSINFO; \ \ - /* The 'entry_number' field. Let the kernel pick a value. */ \ - _segdescr.vals[0] = -1; \ - /* The 'base_addr' field. Pointer to the TCB. */ \ - _segdescr.vals[1] = (unsigned long int) _thrdescr; \ - /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \ - _segdescr.vals[2] = 0xfffff; \ - /* Collapsed value of the bitfield: \ - .seg_32bit = 1 \ - .contents = 0 \ - .read_exec_only = 0 \ - .limit_in_pages = 1 \ - .seg_not_present = 0 \ - .useable = 1 */ \ - _segdescr.vals[3] = 0x51; \ + /* Let the kernel pick a value for the 'entry_number' field. */ \ + tls_fill_user_desc (&_segdescr, -1, _thrdescr); \ \ /* Install the TLS. */ \ asm volatile (TLS_LOAD_EBX \ @@ -244,6 +252,15 @@ union user_desc_init _result == 0 ? NULL \ : "set_thread_area failed when setting up thread-local storage\n"; }) +# define TLS_DEFINE_INIT_TP(tp, pd) \ + union user_desc_init _segdescr; \ + /* Find the 'entry_number' field that the kernel selected in TLS_INIT_TP. \ + The first three bits of the segment register value select the GDT, \ + ignore them. We get the index from the value of the %gs register in \ + the current thread. */ \ + tls_fill_user_desc (&_segdescr, TLS_GET_GS () >> 3, pd); \ + const struct user_desc *tp = &_segdescr.desc + /* Return the address of the dtv for the current thread. */ # define THREAD_DTV() \ --- a/nptl/sysdeps/pthread/createthread.c +++ b/nptl/sysdeps/pthread/createthread.c @@ -30,10 +30,18 @@ #define CLONE_SIGNAL (CLONE_SIGHAND | CLONE_THREAD) -/* Unless otherwise specified, the thread "register" is going to be - initialized with a pointer to the TCB. */ -#ifndef TLS_VALUE -# define TLS_VALUE pd +/* The header should define the macro TLS_DEFINE_INIT_TP such that: + TLS_DEFINE_INIT_TP (VAR, PD); + Declares and initializes a variable VAR with the value that should + be passed to the OS thread creation function (e.g. clone) to initialize + its TLS state for the 'struct pthread *' PD. */ +#ifndef TLS_DEFINE_INIT_TP +/* For a transitional period while all the implementations are + getting updated, we define it using the old TLS_VALUE macro. */ +# define TLS_DEFINE_INIT_TP(tp, pd) void *tp = TLS_VALUE +# ifndef TLS_VALUE +# define TLS_VALUE pd +# endif #endif #ifndef ARCH_CLONE @@ -52,9 +60,7 @@ do_clone (struct pthread *pd, const struct pthread_attr *attr, int clone_flags, int (*fct) (void *), STACK_VARIABLES_PARMS, int stopped) { -#ifdef PREPARE_CREATE - PREPARE_CREATE; -#endif + TLS_DEFINE_INIT_TP (tp, pd); if (__glibc_unlikely (stopped != 0)) /* We make sure the thread does not run far by forcing it to get a @@ -73,7 +79,7 @@ do_clone (struct pthread *pd, const struct pthread_attr *attr, atomic_increment (&__nptl_nthreads); int rc = ARCH_CLONE (fct, STACK_VARIABLES_ARGS, clone_flags, - pd, &pd->tid, TLS_VALUE, &pd->tid); + pd, &pd->tid, tp, &pd->tid); if (__glibc_unlikely (rc == -1)) { --- a/nptl/sysdeps/x86_64/tls.h +++ b/nptl/sysdeps/x86_64/tls.h @@ -164,6 +164,8 @@ typedef struct _result ? "cannot set %fs base address for thread-local storage" : 0; \ }) +# define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + /* Return the address of the dtv for the current thread. */ # define THREAD_DTV() \ --- a/sysdeps/unix/sysv/linux/i386/createthread.c +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright (C) 2002-2014 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -/* The "thread register" gets initialized from a segment descriptor. - Initialize such a descriptor first. */ -#define PREPARE_CREATE \ - union user_desc_init desc; \ - \ - /* Describe the thread-local storage segment. */ \ - \ - /* The 'entry_number' field. The first three bits of the segment \ - register value select the GDT, ignore them. We get the index \ - from the value of the %gs register in the current thread. */ \ - desc.vals[0] = TLS_GET_GS () >> 3; \ - /* The 'base_addr' field. Pointer to the TCB. */ \ - desc.vals[1] = (unsigned long int) pd; \ - /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \ - desc.vals[2] = 0xfffff; \ - /* Collapsed value of the bitfield: \ - .seg_32bit = 1 \ - .contents = 0 \ - .read_exec_only = 0 \ - .limit_in_pages = 1 \ - .seg_not_present = 0 \ - .useable = 1 */ \ - desc.vals[3] = 0x51 - -/* Value passed to 'clone' for initialization of the thread register. */ -#define TLS_VALUE &desc.desc - - -/* Get the real implementation. */ -#include