blob: 53fe3d5085178742331441f227750b6df6795b51 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Elliott Hughes2a0b8732013-10-08 18:50:24 -070028
Ryan Prichard45d13492019-01-03 02:51:30 -080029#pragma once
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030
Josh Gao5e2285d2017-02-22 12:19:05 -080031#include <locale.h>
32#include <mntent.h>
33#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <sys/cdefs.h>
Josh Gao5e2285d2017-02-22 12:19:05 -080035#include <sys/param.h>
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080036
Christopher Ferrisc5d3a432019-09-25 17:50:36 -070037#include <platform/bionic/tls.h>
38
Josh Gao4956c372019-12-19 16:35:51 -080039#include "platform/bionic/macros.h"
Josh Gao5e2285d2017-02-22 12:19:05 -080040#include "grp_pwd.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042/** WARNING WARNING WARNING
43 **
Ryan Prichard45d13492019-01-03 02:51:30 -080044 ** This header file is *NOT* part of the public Bionic ABI/API and should not
45 ** be used/included by user-serviceable parts of the system (e.g.
46 ** applications).
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047 **/
48
Ryan Prichard45d13492019-01-03 02:51:30 -080049class pthread_internal_t;
Ryan Prichard37754cd2018-12-07 01:47:00 -080050
Ryan Prichard45d13492019-01-03 02:51:30 -080051// This struct is small, so the linker can allocate a temporary copy on its
52// stack. It can't be combined with pthread_internal_t because:
53// - native bridge requires pthread_internal_t to have the same layout across
54// architectures, and
55// - On x86, this struct would have to be placed at the front of
56// pthread_internal_t, moving fields like `tid`.
57// - We'd like to avoid having a temporary pthread_internal_t object that
58// needs to be transferred once the final size of static TLS is known.
59struct bionic_tcb {
60 void* raw_slots_storage[BIONIC_TLS_SLOTS];
Elliott Hughes3e898472013-02-12 16:40:24 +000061
Ryan Prichard45d13492019-01-03 02:51:30 -080062 // Return a reference to a slot given its TP-relative TLS_SLOT_xxx index.
63 // The thread pointer (i.e. __get_tls()) points at &tls_slot(0).
64 void*& tls_slot(size_t tpindex) {
65 return raw_slots_storage[tpindex - MIN_TLS_SLOT];
66 }
Elliott Hughes3e898472013-02-12 16:40:24 +000067
Ryan Prichard45d13492019-01-03 02:51:30 -080068 // Initialize the main thread's final object using its bootstrap object.
69 void copy_from_bootstrap(const bionic_tcb* boot) {
70 // Copy everything. Problematic slots will be reinitialized.
71 *this = *boot;
72 }
Elliott Hughes34583c12018-11-13 15:30:07 -080073
Ryan Prichard45d13492019-01-03 02:51:30 -080074 pthread_internal_t* thread() {
75 return static_cast<pthread_internal_t*>(tls_slot(TLS_SLOT_THREAD_ID));
76 }
Elliott Hughes44b53ad2013-02-11 20:18:47 +000077};
Elliott Hughes5419b942012-10-16 15:54:46 -070078
Elliott Hughes3e898472013-02-12 16:40:24 +000079/*
Yabin Cui5e2bd712015-02-20 16:15:33 -080080 * Bionic uses some pthread keys internally. All pthread keys used internally
Elliott Hughes61706932015-03-31 10:56:58 -070081 * should be created in constructors, except for keys that may be used in or
82 * before constructors.
83 *
Yabin Cui5e2bd712015-02-20 16:15:33 -080084 * We need to manually maintain the count of pthread keys used internally, but
85 * pthread_test should fail if we forget.
Elliott Hughes61706932015-03-31 10:56:58 -070086 *
87 * These are the pthread keys currently used internally by libc:
Elliott Hughes61706932015-03-31 10:56:58 -070088 * _res_key libc (constructor in BSD code)
Yabin Cui5e2bd712015-02-20 16:15:33 -080089 */
90
Josh Gao5e2285d2017-02-22 12:19:05 -080091#define LIBC_PTHREAD_KEY_RESERVED_COUNT 1
Yabin Cui5e2bd712015-02-20 16:15:33 -080092
Christopher Ferrisc0f89282015-04-15 16:34:57 -070093/* Internally, jemalloc uses a single key for per thread data. */
94#define JEMALLOC_PTHREAD_KEY_RESERVED_COUNT 1
Yabin Cui5e2bd712015-02-20 16:15:33 -080095#define BIONIC_PTHREAD_KEY_RESERVED_COUNT (LIBC_PTHREAD_KEY_RESERVED_COUNT + JEMALLOC_PTHREAD_KEY_RESERVED_COUNT)
Christopher Ferris72bbd422014-05-08 11:14:03 -070096
Elliott Hughes18876212013-12-12 11:02:41 -080097/*
Yabin Cui5e2bd712015-02-20 16:15:33 -080098 * Maximum number of pthread keys allocated.
99 * This includes pthread keys used internally and externally.
Elliott Hughes18876212013-12-12 11:02:41 -0800100 */
Yabin Cui5e2bd712015-02-20 16:15:33 -0800101#define BIONIC_PTHREAD_KEY_COUNT (BIONIC_PTHREAD_KEY_RESERVED_COUNT + PTHREAD_KEYS_MAX)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800102
Ryan Prichard45d13492019-01-03 02:51:30 -0800103class pthread_key_data_t {
104 public:
105 uintptr_t seq; // Use uintptr_t just for alignment, as we use pointer below.
106 void* data;
107};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108
Ryan Prichard45d13492019-01-03 02:51:30 -0800109// ~3 pages. This struct is allocated as static TLS memory (i.e. at a fixed
110// offset from the thread pointer).
111struct bionic_tls {
112 pthread_key_data_t key_data[BIONIC_PTHREAD_KEY_COUNT];
113
114 locale_t locale;
115
116 char basename_buf[MAXPATHLEN];
117 char dirname_buf[MAXPATHLEN];
118
119 mntent mntent_buf;
120 char mntent_strings[BUFSIZ];
121
122 char ptsname_buf[32];
123 char ttyname_buf[64];
124
125 char strerror_buf[NL_TEXTMAX];
126 char strsignal_buf[NL_TEXTMAX];
127
128 group_state_t group;
129 passwd_state_t passwd;
130
Josh Gao97271922019-11-06 13:15:00 -0800131 char fdtrack_disabled;
Daniele Di Proiettof5f04b12022-01-25 18:52:04 +0000132 char bionic_systrace_disabled;
133 char padding[2];
Josh Gao97271922019-11-06 13:15:00 -0800134
Ryan Prichard45d13492019-01-03 02:51:30 -0800135 // Initialize the main thread's final object using its bootstrap object.
136 void copy_from_bootstrap(const bionic_tls* boot __attribute__((unused))) {
137 // Nothing in bionic_tls needs to be preserved in the transition to the
138 // final TLS objects, so don't copy anything.
139 }
140};
141
Bernhard Rosenkraenzeredad1e12013-09-18 23:37:00 +0200142class KernelArgumentBlock;
Ryan Prichard45d13492019-01-03 02:51:30 -0800143extern "C" void __libc_init_main_thread_early(const KernelArgumentBlock& args, bionic_tcb* temp_tcb);
144extern "C" void __libc_init_main_thread_late();
145extern "C" void __libc_init_main_thread_final();