1*8d67ca89SAndroid Build Coastguard Worker /* 2*8d67ca89SAndroid Build Coastguard Worker * Copyright (C) 2008 The Android Open Source Project 3*8d67ca89SAndroid Build Coastguard Worker * All rights reserved. 4*8d67ca89SAndroid Build Coastguard Worker * 5*8d67ca89SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 6*8d67ca89SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions 7*8d67ca89SAndroid Build Coastguard Worker * are met: 8*8d67ca89SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright 9*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer. 10*8d67ca89SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright 11*8d67ca89SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in 12*8d67ca89SAndroid Build Coastguard Worker * the documentation and/or other materials provided with the 13*8d67ca89SAndroid Build Coastguard Worker * distribution. 14*8d67ca89SAndroid Build Coastguard Worker * 15*8d67ca89SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16*8d67ca89SAndroid Build Coastguard Worker * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17*8d67ca89SAndroid Build Coastguard Worker * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18*8d67ca89SAndroid Build Coastguard Worker * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19*8d67ca89SAndroid Build Coastguard Worker * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20*8d67ca89SAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21*8d67ca89SAndroid Build Coastguard Worker * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22*8d67ca89SAndroid Build Coastguard Worker * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23*8d67ca89SAndroid Build Coastguard Worker * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24*8d67ca89SAndroid Build Coastguard Worker * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25*8d67ca89SAndroid Build Coastguard Worker * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*8d67ca89SAndroid Build Coastguard Worker * SUCH DAMAGE. 27*8d67ca89SAndroid Build Coastguard Worker */ 28*8d67ca89SAndroid Build Coastguard Worker 29*8d67ca89SAndroid Build Coastguard Worker #pragma once 30*8d67ca89SAndroid Build Coastguard Worker 31*8d67ca89SAndroid Build Coastguard Worker #include <locale.h> 32*8d67ca89SAndroid Build Coastguard Worker #include <mntent.h> 33*8d67ca89SAndroid Build Coastguard Worker #include <stdio.h> 34*8d67ca89SAndroid Build Coastguard Worker #include <sys/cdefs.h> 35*8d67ca89SAndroid Build Coastguard Worker #include <sys/param.h> 36*8d67ca89SAndroid Build Coastguard Worker 37*8d67ca89SAndroid Build Coastguard Worker #include <platform/bionic/tls.h> 38*8d67ca89SAndroid Build Coastguard Worker 39*8d67ca89SAndroid Build Coastguard Worker #include "platform/bionic/macros.h" 40*8d67ca89SAndroid Build Coastguard Worker #include "grp_pwd.h" 41*8d67ca89SAndroid Build Coastguard Worker 42*8d67ca89SAndroid Build Coastguard Worker /** WARNING WARNING WARNING 43*8d67ca89SAndroid Build Coastguard Worker ** 44*8d67ca89SAndroid Build Coastguard Worker ** This header file is *NOT* part of the public Bionic ABI/API and should not 45*8d67ca89SAndroid Build Coastguard Worker ** be used/included by user-serviceable parts of the system (e.g. 46*8d67ca89SAndroid Build Coastguard Worker ** applications). 47*8d67ca89SAndroid Build Coastguard Worker **/ 48*8d67ca89SAndroid Build Coastguard Worker 49*8d67ca89SAndroid Build Coastguard Worker class pthread_internal_t; 50*8d67ca89SAndroid Build Coastguard Worker 51*8d67ca89SAndroid Build Coastguard Worker // This struct is small, so the linker can allocate a temporary copy on its 52*8d67ca89SAndroid Build Coastguard Worker // stack. It can't be combined with pthread_internal_t because: 53*8d67ca89SAndroid Build Coastguard Worker // - native bridge requires pthread_internal_t to have the same layout across 54*8d67ca89SAndroid Build Coastguard Worker // architectures, and 55*8d67ca89SAndroid Build Coastguard Worker // - On x86, this struct would have to be placed at the front of 56*8d67ca89SAndroid Build Coastguard Worker // pthread_internal_t, moving fields like `tid`. 57*8d67ca89SAndroid Build Coastguard Worker // - We'd like to avoid having a temporary pthread_internal_t object that 58*8d67ca89SAndroid Build Coastguard Worker // needs to be transferred once the final size of static TLS is known. 59*8d67ca89SAndroid Build Coastguard Worker struct bionic_tcb { 60*8d67ca89SAndroid Build Coastguard Worker void* raw_slots_storage[BIONIC_TLS_SLOTS]; 61*8d67ca89SAndroid Build Coastguard Worker 62*8d67ca89SAndroid Build Coastguard Worker // Return a reference to a slot given its TP-relative TLS_SLOT_xxx index. 63*8d67ca89SAndroid Build Coastguard Worker // The thread pointer (i.e. __get_tls()) points at &tls_slot(0). tls_slotbionic_tcb64*8d67ca89SAndroid Build Coastguard Worker void*& tls_slot(size_t tpindex) { 65*8d67ca89SAndroid Build Coastguard Worker return raw_slots_storage[tpindex - MIN_TLS_SLOT]; 66*8d67ca89SAndroid Build Coastguard Worker } 67*8d67ca89SAndroid Build Coastguard Worker 68*8d67ca89SAndroid Build Coastguard Worker // Initialize the main thread's final object using its bootstrap object. copy_from_bootstrapbionic_tcb69*8d67ca89SAndroid Build Coastguard Worker void copy_from_bootstrap(const bionic_tcb* boot) { 70*8d67ca89SAndroid Build Coastguard Worker // Copy everything. Problematic slots will be reinitialized. 71*8d67ca89SAndroid Build Coastguard Worker *this = *boot; 72*8d67ca89SAndroid Build Coastguard Worker } 73*8d67ca89SAndroid Build Coastguard Worker threadbionic_tcb74*8d67ca89SAndroid Build Coastguard Worker pthread_internal_t* thread() { 75*8d67ca89SAndroid Build Coastguard Worker return static_cast<pthread_internal_t*>(tls_slot(TLS_SLOT_THREAD_ID)); 76*8d67ca89SAndroid Build Coastguard Worker } 77*8d67ca89SAndroid Build Coastguard Worker }; 78*8d67ca89SAndroid Build Coastguard Worker 79*8d67ca89SAndroid Build Coastguard Worker /* 80*8d67ca89SAndroid Build Coastguard Worker * Bionic uses some pthread keys internally. All pthread keys used internally 81*8d67ca89SAndroid Build Coastguard Worker * should be created in constructors, except for keys that may be used in or 82*8d67ca89SAndroid Build Coastguard Worker * before constructors. 83*8d67ca89SAndroid Build Coastguard Worker * 84*8d67ca89SAndroid Build Coastguard Worker * We need to manually maintain the count of pthread keys used internally, but 85*8d67ca89SAndroid Build Coastguard Worker * pthread_test should fail if we forget. 86*8d67ca89SAndroid Build Coastguard Worker * 87*8d67ca89SAndroid Build Coastguard Worker * These are the pthread keys currently used internally by libc: 88*8d67ca89SAndroid Build Coastguard Worker * _res_key libc (constructor in BSD code) 89*8d67ca89SAndroid Build Coastguard Worker */ 90*8d67ca89SAndroid Build Coastguard Worker 91*8d67ca89SAndroid Build Coastguard Worker #define LIBC_PTHREAD_KEY_RESERVED_COUNT 1 92*8d67ca89SAndroid Build Coastguard Worker 93*8d67ca89SAndroid Build Coastguard Worker /* Internally, jemalloc uses a single key for per thread data. */ 94*8d67ca89SAndroid Build Coastguard Worker #define JEMALLOC_PTHREAD_KEY_RESERVED_COUNT 1 95*8d67ca89SAndroid Build Coastguard Worker #define BIONIC_PTHREAD_KEY_RESERVED_COUNT (LIBC_PTHREAD_KEY_RESERVED_COUNT + JEMALLOC_PTHREAD_KEY_RESERVED_COUNT) 96*8d67ca89SAndroid Build Coastguard Worker 97*8d67ca89SAndroid Build Coastguard Worker /* 98*8d67ca89SAndroid Build Coastguard Worker * Maximum number of pthread keys allocated. 99*8d67ca89SAndroid Build Coastguard Worker * This includes pthread keys used internally and externally. 100*8d67ca89SAndroid Build Coastguard Worker */ 101*8d67ca89SAndroid Build Coastguard Worker #define BIONIC_PTHREAD_KEY_COUNT (BIONIC_PTHREAD_KEY_RESERVED_COUNT + PTHREAD_KEYS_MAX) 102*8d67ca89SAndroid Build Coastguard Worker 103*8d67ca89SAndroid Build Coastguard Worker class pthread_key_data_t { 104*8d67ca89SAndroid Build Coastguard Worker public: 105*8d67ca89SAndroid Build Coastguard Worker uintptr_t seq; // Use uintptr_t just for alignment, as we use pointer below. 106*8d67ca89SAndroid Build Coastguard Worker void* data; 107*8d67ca89SAndroid Build Coastguard Worker }; 108*8d67ca89SAndroid Build Coastguard Worker 109*8d67ca89SAndroid Build Coastguard Worker // ~3 pages. This struct is allocated as static TLS memory (i.e. at a fixed 110*8d67ca89SAndroid Build Coastguard Worker // offset from the thread pointer). 111*8d67ca89SAndroid Build Coastguard Worker struct bionic_tls { 112*8d67ca89SAndroid Build Coastguard Worker pthread_key_data_t key_data[BIONIC_PTHREAD_KEY_COUNT]; 113*8d67ca89SAndroid Build Coastguard Worker 114*8d67ca89SAndroid Build Coastguard Worker locale_t locale; 115*8d67ca89SAndroid Build Coastguard Worker 116*8d67ca89SAndroid Build Coastguard Worker char basename_buf[MAXPATHLEN]; 117*8d67ca89SAndroid Build Coastguard Worker char dirname_buf[MAXPATHLEN]; 118*8d67ca89SAndroid Build Coastguard Worker 119*8d67ca89SAndroid Build Coastguard Worker mntent mntent_buf; 120*8d67ca89SAndroid Build Coastguard Worker char mntent_strings[BUFSIZ]; 121*8d67ca89SAndroid Build Coastguard Worker 122*8d67ca89SAndroid Build Coastguard Worker char ptsname_buf[32]; 123*8d67ca89SAndroid Build Coastguard Worker char ttyname_buf[64]; 124*8d67ca89SAndroid Build Coastguard Worker 125*8d67ca89SAndroid Build Coastguard Worker char strerror_buf[NL_TEXTMAX]; 126*8d67ca89SAndroid Build Coastguard Worker char strsignal_buf[NL_TEXTMAX]; 127*8d67ca89SAndroid Build Coastguard Worker 128*8d67ca89SAndroid Build Coastguard Worker group_state_t group; 129*8d67ca89SAndroid Build Coastguard Worker passwd_state_t passwd; 130*8d67ca89SAndroid Build Coastguard Worker 131*8d67ca89SAndroid Build Coastguard Worker char fdtrack_disabled; 132*8d67ca89SAndroid Build Coastguard Worker char bionic_systrace_disabled; 133*8d67ca89SAndroid Build Coastguard Worker char padding[2]; 134*8d67ca89SAndroid Build Coastguard Worker 135*8d67ca89SAndroid Build Coastguard Worker // Initialize the main thread's final object using its bootstrap object. copy_from_bootstrapbionic_tls136*8d67ca89SAndroid Build Coastguard Worker void copy_from_bootstrap(const bionic_tls* boot __attribute__((unused))) { 137*8d67ca89SAndroid Build Coastguard Worker // Nothing in bionic_tls needs to be preserved in the transition to the 138*8d67ca89SAndroid Build Coastguard Worker // final TLS objects, so don't copy anything. 139*8d67ca89SAndroid Build Coastguard Worker } 140*8d67ca89SAndroid Build Coastguard Worker }; 141*8d67ca89SAndroid Build Coastguard Worker 142*8d67ca89SAndroid Build Coastguard Worker class KernelArgumentBlock; 143*8d67ca89SAndroid Build Coastguard Worker extern "C" void __libc_init_main_thread_early(const KernelArgumentBlock& args, bionic_tcb* temp_tcb); 144*8d67ca89SAndroid Build Coastguard Worker extern "C" void __libc_init_main_thread_late(); 145*8d67ca89SAndroid Build Coastguard Worker extern "C" void __libc_init_main_thread_final(); 146