From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 126675 invoked by alias); 13 Nov 2017 15:46:23 -0000 Mailing-List: contact newlib-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-owner@sourceware.org Received: (qmail 126646 invoked by uid 89); 13 Nov 2017 15:46:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=vital, hands, sk:corinna, *foo X-HELO: drew.franken.de Received: from mail-n.franken.de (HELO drew.franken.de) (193.175.24.27) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 13 Nov 2017 15:46:20 +0000 Received: from aqua.hirmke.de (aquarius.franken.de [193.175.24.89]) (Authenticated sender: aquarius) by mail-n.franken.de (Postfix) with ESMTPSA id 2447872106C16 for ; Mon, 13 Nov 2017 16:46:17 +0100 (CET) Received: from calimero.vinschen.de (calimero.vinschen.de [192.168.129.6]) by aqua.hirmke.de (Postfix) with ESMTP id 5D0F35E08A0 for ; Mon, 13 Nov 2017 16:46:16 +0100 (CET) Received: by calimero.vinschen.de (Postfix, from userid 500) id 5794DA806CB; Mon, 13 Nov 2017 16:46:16 +0100 (CET) From: Corinna Vinschen To: newlib@sourceware.org Subject: [PATCH 4/6] Cygwin: expose CYGTLS_PADSIZE as __CYGTLS_PADSIZE in userspace Date: Mon, 13 Nov 2017 15:52:00 -0000 Message-Id: <20171113154616.25189-5-vinschen@redhat.com> In-Reply-To: <20171113154616.25189-1-vinschen@redhat.com> References: <20171113154616.25189-1-vinschen@redhat.com> X-IsSubscribed: yes X-SW-Source: 2017/txt/msg01112.txt.bz2 From: Corinna Vinschen This definition will be fixed from now on so we can use it as offset operator in userspace. The definition gets exposed in a file cygwin/_cygtls_padsize.h which in turn is included by cygwin/config.h. Signed-off-by: Corinna Vinschen --- winsup/cygwin/cygtls.cc | 2 +- winsup/cygwin/cygtls.h | 4 +--- winsup/cygwin/dcrt0.cc | 2 +- winsup/cygwin/external.cc | 2 +- winsup/cygwin/gentls_offsets | 4 ++-- winsup/cygwin/how-cygtls-works.txt | 10 +++++----- .../{cygtls_padsize.h => include/cygwin/_cygtls_padsize.h} | 5 ++--- winsup/cygwin/init.cc | 2 +- winsup/cygwin/miscfuncs.cc | 10 +++++----- winsup/utils/strace.cc | 4 ++-- 10 files changed, 21 insertions(+), 24 deletions(-) rename winsup/cygwin/{cygtls_padsize.h => include/cygwin/_cygtls_padsize.h} (60%) diff --git a/winsup/cygwin/cygtls.cc b/winsup/cygwin/cygtls.cc index 7c5936a109a5..207ebf89c6a1 100644 --- a/winsup/cygwin/cygtls.cc +++ b/winsup/cygwin/cygtls.cc @@ -20,7 +20,7 @@ details. */ void _cygtls::call (DWORD (*func) (void *, void *), void *arg) { - char buf[CYGTLS_PADSIZE]; + char buf[__CYGTLS_PADSIZE]; /* Initialize this thread's ability to respond to things like SIGSEGV or SIGFPE. */ exception protect; diff --git a/winsup/cygwin/cygtls.h b/winsup/cygwin/cygtls.h index 39dba1380ce1..18d9378f88ec 100644 --- a/winsup/cygwin/cygtls.h +++ b/winsup/cygwin/cygtls.h @@ -276,14 +276,12 @@ private: }; #pragma pack(pop) -#include "cygtls_padsize.h" - /*gentls_offsets*/ #include "cygerrno.h" #include "ntdll.h" -#define _my_tls (*((_cygtls *) ((PBYTE) NtCurrentTeb()->Tib.StackBase - CYGTLS_PADSIZE))) +#define _my_tls (*((_cygtls *) ((PBYTE) NtCurrentTeb()->Tib.StackBase - __CYGTLS_PADSIZE))) extern _cygtls *_main_tls; extern _cygtls *_sig_tls; diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index 70198e27453d..cbfab7daf993 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -1094,7 +1094,7 @@ dll_crt0 (per_process *uptr) } /* This must be called by anyone who uses LoadLibrary to load cygwin1.dll. - You must have CYGTLS_PADSIZE bytes reserved at the bottom of the stack + You must have __CYGTLS_PADSIZE bytes reserved at the bottom of the stack calling this function, and that storage must not be overwritten until you unload cygwin1.dll, as it is used for _my_tls. It is best to load cygwin1.dll before spawning any additional threads in your process. diff --git a/winsup/cygwin/external.cc b/winsup/cygwin/external.cc index 6aae32aea472..30ebb4eb24f5 100644 --- a/winsup/cygwin/external.cc +++ b/winsup/cygwin/external.cc @@ -432,7 +432,7 @@ cygwin_internal (cygwin_getinfo_types t, ...) res = 0; break; case CW_CYGTLS_PADSIZE: - res = CYGTLS_PADSIZE; + res = __CYGTLS_PADSIZE; break; case CW_SET_DOS_FILE_WARNING: { diff --git a/winsup/cygwin/gentls_offsets b/winsup/cygwin/gentls_offsets index 745ea27a035c..be778f5cf0ed 100755 --- a/winsup/cygwin/gentls_offsets +++ b/winsup/cygwin/gentls_offsets @@ -66,11 +66,11 @@ main(int argc, char **argv) { $struct *foo; # define foo_beg ((char *) foo) -# define offset(f) ((unsigned)((int) (((char *) &(foo->f)) - foo_beg) - CYGTLS_PADSIZE)) +# define offset(f) ((unsigned)((int) (((char *) &(foo->f)) - foo_beg) - __CYGTLS_PADSIZE)) # define poffset(f) ((unsigned)(((char *) &(foo->f)) - ((char *) foo))) EOF print TMP 'puts ("//;# autogenerated: Do not edit.\n");', "\n\n"; - print TMP "printf (\"//; \$tls::start_offset = -%d;\\n\", CYGTLS_PADSIZE);\n"; + print TMP "printf (\"//; \$tls::start_offset = -%d;\\n\", __CYGTLS_PADSIZE);\n"; for my $f (@fields) { print TMP ' printf ("//; $tls::', $f, ' = %d;\n", ', "offset($f));\n"; print TMP ' printf ("//; $tls::p', $f, ' = %d;\n", ', "poffset($f));\n"; diff --git a/winsup/cygwin/how-cygtls-works.txt b/winsup/cygwin/how-cygtls-works.txt index 8bba0a7a0043..2f5e0ccf67e9 100644 --- a/winsup/cygwin/how-cygtls-works.txt +++ b/winsup/cygwin/how-cygtls-works.txt @@ -1,7 +1,7 @@ Contributed by Max Kaehn All cygwin threads have separate context in an object of class _cygtls. The -storage for this object is kept on the stack in the bottom CYGTLS_PADSIZE +storage for this object is kept on the stack in the bottom __CYGTLS_PADSIZE bytes. Each thread references the storage via the Thread Environment Block (aka Thread Information Block), which Windows maintains for each user thread in the system, with the address in the FS segment register. The memory @@ -34,13 +34,13 @@ And accesses cygtls like this: Initialization always goes through _cygtls::init_thread(). It works in the following ways: -* In the main thread, _dll_crt0() provides CYGTLS_PADSIZE bytes on the stack +* In the main thread, _dll_crt0() provides __CYGTLS_PADSIZE bytes on the stack and passes them to initialize_main_tls(), which calls _cygtls::init_thread(). It then calls dll_crt0_1(), which terminates with cygwin_exit() rather than by returning, so the storage never goes out of scope. If you load cygwin1.dll dynamically from a non-cygwin application, it is - vital that the bottom CYGTLS_PADSIZE bytes of the stack are not in use + vital that the bottom __CYGTLS_PADSIZE bytes of the stack are not in use before you call cygwin_dll_init(). See winsup/testsuite/cygload for more information. @@ -49,7 +49,7 @@ in the following ways: - dll_entry() calls munge_threadfunc(), which grabs the function pointer for the thread from the stack frame and substitutes threadfunc_fe(), - which then passes the original function pointer to _cygtls::call(), - - which then allocates CYGTLS_PADSIZE bytes on the stack and hands them + - which then allocates __CYGTLS_PADSIZE bytes on the stack and hands them to call2(), - which allocates an exception_list object on the stack and hands it to init_exceptions() (in exceptions.cc), which attaches it to the end of @@ -60,7 +60,7 @@ in the following ways: Note that the padding isn't necessarily going to be just where the _cygtls structure lives; it just makes sure there's enough room on the stack when the -CYGTLS_PADSIZE bytes down from there are overwritten. +__CYGTLS_PADSIZE bytes down from there are overwritten. Debugging diff --git a/winsup/cygwin/cygtls_padsize.h b/winsup/cygwin/include/cygwin/_cygtls_padsize.h similarity index 60% rename from winsup/cygwin/cygtls_padsize.h rename to winsup/cygwin/include/cygwin/_cygtls_padsize.h index d3f024ad341a..d011d76ea36a 100644 --- a/winsup/cygwin/cygtls_padsize.h +++ b/winsup/cygwin/include/cygwin/_cygtls_padsize.h @@ -4,9 +4,8 @@ This software is a copyrighted work licensed under the terms of the Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */ -/* FIXME: Find some way to autogenerate this value */ #ifdef __x86_64__ -const int CYGTLS_PADSIZE = 12800; /* Must be 16-byte aligned */ +#define __CYGTLS_PADSIZE 12800 #else -const int CYGTLS_PADSIZE = 12700; +#define __CYGTLS_PADSIZE 12700 #endif diff --git a/winsup/cygwin/init.cc b/winsup/cygwin/init.cc index aeeeac7725fc..db7ed11e2c8e 100644 --- a/winsup/cygwin/init.cc +++ b/winsup/cygwin/init.cc @@ -99,7 +99,7 @@ dll_entry (HANDLE h, DWORD reason, void *static_load) initialized to NULL, so subsequent calls to locale-specific functions will always fall back to __global_locale, rather then crash due to _REENT->_locale having an arbitrary value. */ - alloca_dummy = alloca (CYGTLS_PADSIZE); + alloca_dummy = alloca (__CYGTLS_PADSIZE); memcpy (_REENT, _GLOBAL_REENT, sizeof (struct _reent)); dll_crt0_0 (); diff --git a/winsup/cygwin/miscfuncs.cc b/winsup/cygwin/miscfuncs.cc index e2ab7a0809fd..9760f3951d2f 100644 --- a/winsup/cygwin/miscfuncs.cc +++ b/winsup/cygwin/miscfuncs.cc @@ -397,7 +397,7 @@ pthread_wrapper (PVOID arg) SetThreadStackGuarantee (&wrapper_arg.guardsize); } /* Initialize new _cygtls. */ - _my_tls.init_thread (wrapper_arg.stackbase - CYGTLS_PADSIZE, + _my_tls.init_thread (wrapper_arg.stackbase - __CYGTLS_PADSIZE, (DWORD (*)(void*, void*)) wrapper_arg.func); #ifndef __x86_64__ /* Copy exception list over to new stack. I'm not quite sure how the @@ -440,7 +440,7 @@ pthread_wrapper (PVOID arg) movq 8(%%rbx), %%r13 # Load thread arg into r13 \n\ movq 16(%%rbx), %%rcx # Load stackaddr into rcx \n\ movq 24(%%rbx), %%rsp # Load stackbase into rsp \n\ - subq %[CYGTLS], %%rsp # Subtract CYGTLS_PADSIZE \n\ + subq %[CYGTLS], %%rsp # Subtract __CYGTLS_PADSIZE \n\ # (here we are 16 bytes aligned)\n\ subq $32, %%rsp # Subtract another 32 bytes \n\ # (shadow space for arg regs) \n\ @@ -456,7 +456,7 @@ pthread_wrapper (PVOID arg) movq %%r13, %%rcx # Move thread arg to 1st arg reg\n\ call *%%r12 # Call thread func \n" : : [WRAPPER_ARG] "o" (wrapper_arg), - [CYGTLS] "i" (CYGTLS_PADSIZE)); + [CYGTLS] "i" (__CYGTLS_PADSIZE)); #else __asm__ ("\n\ leal %[WRAPPER_ARG], %%ebx # Load &wrapper_arg into ebx \n\ @@ -464,7 +464,7 @@ pthread_wrapper (PVOID arg) movl 4(%%ebx), %%ecx # Load thread arg into ecx \n\ movl 8(%%ebx), %%edx # Load stackaddr into edx \n\ movl 12(%%ebx), %%ebx # Load stackbase into ebx \n\ - subl %[CYGTLS], %%ebx # Subtract CYGTLS_PADSIZE \n\ + subl %[CYGTLS], %%ebx # Subtract __CYGTLS_PADSIZE \n\ subl $4, %%ebx # Subtract another 4 bytes \n\ movl %%ebx, %%esp # Set esp \n\ xorl %%ebp, %%ebp # Set ebp to 0 \n\ @@ -487,7 +487,7 @@ pthread_wrapper (PVOID arg) popl %%eax # Pop thread_func address \n\ call *%%eax # Call thread func \n" : : [WRAPPER_ARG] "o" (wrapper_arg), - [CYGTLS] "i" (CYGTLS_PADSIZE)); + [CYGTLS] "i" (__CYGTLS_PADSIZE)); #endif /* pthread::thread_init_wrapper calls pthread::exit, which in turn calls ExitThread, so we should never arrive here. */ diff --git a/winsup/utils/strace.cc b/winsup/utils/strace.cc index ae62cdc5f2ba..1e1985eaf95a 100644 --- a/winsup/utils/strace.cc +++ b/winsup/utils/strace.cc @@ -24,7 +24,7 @@ details. */ #include "../cygwin/include/sys/strace.h" #include "../cygwin/include/sys/cygwin.h" #include "../cygwin/include/cygwin/version.h" -#include "../cygwin/cygtls_padsize.h" +#include "../cygwin/include/cygwin/_cygtls_padsize.h" #include "path.h" #undef cygwin_internal #include "loadlib.h" @@ -1189,7 +1189,7 @@ main (int argc, char **argv) This is required to make sure cygwin_internal calls into Cygwin work reliably. This problem has been noticed under AllocationPreference registry setting to 0x100000 (TOP_DOWN). */ - char buf[CYGTLS_PADSIZE]; + char buf[__CYGTLS_PADSIZE]; RtlSecureZeroMemory (buf, sizeof (buf)); exit (main2 (argc, argv)); -- 2.9.5