1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <async_safe/log.h>
18
19 #include "bionic/pthread_internal.h"
20 #include "native_bridge_support/linker/static_tls_config.h"
21 #include "private/KernelArgumentBlock.h"
22 #include "private/bionic_arc4random.h"
23 #include "private/bionic_elf_tls.h"
24 #include "private/bionic_globals.h"
25 #include "private/bionic_ssp.h"
26 #include "private/bionic_tls.h"
27
28 // Once the loader has calculated the size of static TLS, report the information
29 // to the host so it can allocate the static TLS of future threads.
30 extern "C" void __native_bridge_config_static_tls(const NativeBridgeStaticTlsConfig* config);
31
32 // Get the current thread's host pthread_internal_t.
33 extern "C" pthread_t __native_bridge_get_host_pthread();
34
35 // The host has already initialized the thread and created its
36 // pthread_internal_t object. The guest needs to initialize its globals and the
37 // main thread's guest static TLS memory.
__libc_init_main_thread_early(const KernelArgumentBlock & args,bionic_tcb * temp_tcb)38 extern "C" void __libc_init_main_thread_early(const KernelArgumentBlock& args,
39 bionic_tcb* temp_tcb) {
40 __libc_shared_globals()->auxv = args.auxv;
41 #if defined(__i386__)
42 __libc_init_sysinfo();
43 #endif
44 // TCB layout is different between host and guest, so initialize the guest
45 // TCB. Reuse the host's pthread_internal_t and bionic_tls objects.
46 auto host_thread = reinterpret_cast<pthread_internal_t*>(__native_bridge_get_host_pthread());
47 __init_tcb(temp_tcb, host_thread);
48 __set_tls(&temp_tcb->tls_slot(0));
49 }
50
__libc_init_main_thread_late()51 extern "C" void __libc_init_main_thread_late() {
52 // Reuse the host's bionic_tls structure.
53 __get_tls()[TLS_SLOT_BIONIC_TLS] = __get_thread()->bionic_tls;
54
55 // There's currently no way for the guest to query the host's stack guard
56 // cookie, so just generate a new one.
57 __libc_safe_arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard));
58 __init_tcb_stack_guard(__get_bionic_tcb());
59 }
60
__libc_init_main_thread_final()61 extern "C" void __libc_init_main_thread_final() {
62 const StaticTlsLayout& layout = __libc_shared_globals()->static_tls_layout;
63
64 // Prepare the initialization image for the host.
65 char* init_img = new char[layout.size()]{};
66 __init_static_tls(init_img);
67 bionic_tcb img_tcb = {};
68 __init_tcb_dtv(&img_tcb);
69 __init_tcb_stack_guard(&img_tcb);
70 memcpy(init_img + layout.offset_bionic_tcb(), &img_tcb, sizeof(img_tcb));
71
72 // Configure the host to create guest static TLS memory for new threads. The
73 // host will replace the guest main thread's static TLS with memory it
74 // allocates.
75 NativeBridgeStaticTlsConfig config{};
76 config.size = layout.size();
77 config.tpoff = layout.offset_thread_pointer();
78 config.tls_slot_thread_id = TLS_SLOT_THREAD_ID;
79 config.tls_slot_bionic_tls = TLS_SLOT_BIONIC_TLS;
80 config.init_img = init_img;
81 __native_bridge_config_static_tls(&config);
82 }
83