14b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes/*
24b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Copyright (C) 2008 The Android Open Source Project
34b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * All rights reserved.
44b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *
54b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Redistribution and use in source and binary forms, with or without
64b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * modification, are permitted provided that the following conditions
74b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * are met:
84b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *  * Redistributions of source code must retain the above copyright
94b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    notice, this list of conditions and the following disclaimer.
104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *  * Redistributions in binary form must reproduce the above copyright
114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    notice, this list of conditions and the following disclaimer in
124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    the documentation and/or other materials provided with the
134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *    distribution.
144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *
154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * SUCH DAMAGE.
274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes */
284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <pthread.h>
304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <errno.h>
324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <sys/mman.h>
337086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes#include <unistd.h>
344b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "pthread_internal.h"
364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
3703eebcb6e8762e668a0d3af6bb303cccb88c5b81Christopher Ferris#include "private/bionic_macros.h"
384b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_ssp.h"
394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_tls.h"
408f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include "private/libc_logging.h"
414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ErrnoRestorer.h"
424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ScopedPthreadMutexLocker.h"
434b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
440d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes// x86 uses segment descriptors rather than a direct pointer to TLS.
450d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#if __i386__
460d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#include <asm/ldt.h>
470d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughesextern "C" __LIBC_HIDDEN__ void __init_user_desc(struct user_desc*, int, void*);
480d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes#endif
490d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes
50f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughesextern "C" int __isthreaded;
51f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes
5270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes// This code is used both by each new pthread and the code that initializes the main thread.
53af8aebebb52d73ea38c604525a6a5857618861cfElliott Hughesvoid __init_tls(pthread_internal_t* thread) {
5411bf8a3025a7b5aee891c521255a7db1860e5b12Elliott Hughes  if (thread->user_allocated_stack()) {
5511bf8a3025a7b5aee891c521255a7db1860e5b12Elliott Hughes    // We don't know where the user got their stack, so assume the worst and zero the TLS area.
5611bf8a3025a7b5aee891c521255a7db1860e5b12Elliott Hughes    memset(&thread->tls[0], 0, BIONIC_TLS_SLOTS * sizeof(void*));
574b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
584b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0.
6040eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_SELF] = thread->tls;
6140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_THREAD_ID] = thread;
624b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // GCC looks in the TLS for the stack guard on x86, so copy it there from our global.
6340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_STACK_GUARD] = (void*) __stack_chk_guard;
6470b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes}
654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
6670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughesvoid __init_alternate_signal_stack(pthread_internal_t* thread) {
6784114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // Create and set an alternate signal stack.
6884114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  stack_t ss;
6911bf8a3025a7b5aee891c521255a7db1860e5b12Elliott Hughes  ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
7084114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  if (ss.ss_sp != MAP_FAILED) {
7184114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    ss.ss_size = SIGSTKSZ;
7284114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    ss.ss_flags = 0;
7384114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    sigaltstack(&ss, NULL);
7484114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    thread->alternate_signal_stack = ss.ss_sp;
7584114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  }
764b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
774b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
78cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughesint __init_thread(pthread_internal_t* thread, bool add_to_thread_list) {
794b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int error = 0;
804b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Set the scheduling policy/priority of the thread.
824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread->attr.sched_policy != SCHED_NORMAL) {
83c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    sched_param param;
844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    param.sched_priority = thread->attr.sched_priority;
8540eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    if (sched_setscheduler(thread->tid, thread->attr.sched_policy, &param) == -1) {
8698624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#if __LP64__
8798624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      // For backwards compatibility reasons, we only report failures on 64-bit devices.
8898624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      error = errno;
8998624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#endif
90b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes      __libc_format_log(ANDROID_LOG_WARN, "libc",
91b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                        "pthread_create sched_setscheduler call failed: %s", strerror(errno));
924b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
934b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  thread->cleanup_stack = NULL;
964b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
974b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (add_to_thread_list) {
984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    _pthread_internal_add(thread);
994b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1014b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return error;
1024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
104b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* __create_thread_stack(pthread_internal_t* thread) {
1054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Create a new private anonymous map.
1064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int prot = PROT_READ | PROT_WRITE;
1074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
108b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* stack = mmap(NULL, thread->attr.stack_size, prot, flags, -1, 0);
1094b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (stack == MAP_FAILED) {
110b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN,
111b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "libc",
112b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "pthread_create failed: couldn't allocate %zd-byte stack: %s",
113b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      thread->attr.stack_size, strerror(errno));
1144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Set the guard region at the end of the stack to PROT_NONE.
118b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  if (mprotect(stack, thread->attr.guard_size, PROT_NONE) == -1) {
119b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc",
120b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "pthread_create failed: couldn't mprotect PROT_NONE %zd-byte stack guard region: %s",
121b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      thread->attr.guard_size, strerror(errno));
122b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    munmap(stack, thread->attr.stack_size);
1234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return stack;
1274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
129e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughesstatic int __pthread_start(void* arg) {
130e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(arg);
131e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
132e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // Wait for our creating thread to release us. This lets it have time to
133e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // notify gdb about this thread before we start doing anything.
134e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // This also provides the memory barrier needed to ensure that all memory
135e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // accesses previously made by the creating thread are visible to us.
136b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_lock(&thread->startup_handshake_mutex);
137b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_destroy(&thread->startup_handshake_mutex);
138e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
139e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  __init_alternate_signal_stack(thread);
140e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
141e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  void* result = thread->start_routine(thread->start_routine_arg);
142e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_exit(result);
143e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
144e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  return 0;
145e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes}
146e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
147cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// A dummy start routine for pthread_create failures where we've created a thread but aren't
148cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// going to run user code on it. We swap out the user's start routine for this and take advantage
149cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// of the regular thread teardown to free up resources.
150cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughesstatic void* __do_nothing(void*) {
151cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes  return NULL;
152cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes}
153cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes
1544b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesint pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
1554b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                   void* (*start_routine)(void*), void* arg) {
1564b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  ErrnoRestorer errno_restorer;
1574b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
158f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes  // Inform the rest of the C library that at least one thread was created.
1594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  __isthreaded = 1;
1604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(calloc(sizeof(*thread), 1));
1624b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread == NULL) {
163cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: couldn't allocate thread");
1644b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return EAGAIN;
1654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1674b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (attr == NULL) {
1684b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    pthread_attr_init(&thread->attr);
1694b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  } else {
1704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    thread->attr = *attr;
1714b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    attr = NULL; // Prevent misuse below.
1724b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
174b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Make sure the stack size and guard size are multiples of PAGE_SIZE.
17503eebcb6e8762e668a0d3af6bb303cccb88c5b81Christopher Ferris  thread->attr.stack_size = BIONIC_ALIGN(thread->attr.stack_size, PAGE_SIZE);
17603eebcb6e8762e668a0d3af6bb303cccb88c5b81Christopher Ferris  thread->attr.guard_size = BIONIC_ALIGN(thread->attr.guard_size, PAGE_SIZE);
1774b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1784b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread->attr.stack_base == NULL) {
1794b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    // The caller didn't provide a stack, so allocate one.
180b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    thread->attr.stack_base = __create_thread_stack(thread);
1814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (thread->attr.stack_base == NULL) {
1824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes      free(thread);
1834b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes      return EAGAIN;
1844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1854b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  } else {
1864b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    // The caller did provide a stack, so remember we're not supposed to free it.
1872b6e43e00ece68b3aba26d8f95f07cd9d9294ab4Elliott Hughes    thread->attr.flags |= PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK;
1884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1894b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
19084114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // Make room for the TLS area.
19184114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // The child stack is the same address, just growing in the opposite direction.
19284114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // At offsets >= 0, we have the TLS slots.
19384114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // At offsets < 0, we have the child stack.
194b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  thread->tls = reinterpret_cast<void**>(reinterpret_cast<uint8_t*>(thread->attr.stack_base) +
195b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes                                         thread->attr.stack_size - BIONIC_TLS_SLOTS * sizeof(void*));
19670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  void* child_stack = thread->tls;
1970d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  __init_tls(thread);
1984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
19970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // Create a mutex for the thread in TLS to wait on once it starts so we can keep
2004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // it from doing anything until after we notify the debugger about it
2014b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  //
2024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // This also provides the memory barrier we need to ensure that all
2034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // memory accesses previously performed by this thread are visible to
2044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // the new thread.
205b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_init(&thread->startup_handshake_mutex, NULL);
206b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_lock(&thread->startup_handshake_mutex);
2074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
20870b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine = start_routine;
20970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine_arg = arg;
2104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2117086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes  thread->set_cached_pid(getpid());
2127086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes
213877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  int flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM |
214877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes      CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID;
2150d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  void* tls = thread->tls;
21680906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#if defined(__i386__)
21780906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  // On x86 (but not x86-64), CLONE_SETTLS takes a pointer to a struct user_desc rather than
2180d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  // a pointer to the TLS itself.
2190d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  user_desc tls_descriptor;
2200d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  __init_user_desc(&tls_descriptor, false, tls);
2210d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  tls = &tls_descriptor;
22280906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#endif
2230d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  int rc = clone(__pthread_start, child_stack, flags, thread, &(thread->tid), tls, &(thread->tid));
224877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  if (rc == -1) {
2254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    int clone_errno = errno;
226877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // We don't have to unlock the mutex at all because clone(2) failed so there's no child waiting to
227877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // be unblocked, but we're about to unmap the memory the mutex is stored in, so this serves as a
228877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // reminder that you can't rewrite this function to use a ScopedPthreadMutexLocker.
229b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes    pthread_mutex_unlock(&thread->startup_handshake_mutex);
23011bf8a3025a7b5aee891c521255a7db1860e5b12Elliott Hughes    if (!thread->user_allocated_stack()) {
231b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes      munmap(thread->attr.stack_base, thread->attr.stack_size);
2324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
2334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    free(thread);
234cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s", strerror(errno));
2354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return clone_errno;
2364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
238cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes  int init_errno = __init_thread(thread, true);
2394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (init_errno != 0) {
240cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Mark the thread detached and replace its start_routine with a no-op.
241cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Letting the thread run is the easiest way to clean up its resources.
2424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
243cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    thread->start_routine = __do_nothing;
244b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes    pthread_mutex_unlock(&thread->startup_handshake_mutex);
2454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return init_errno;
2464b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2474b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
248877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  // Publish the pthread_t and unlock the mutex to let the new thread start running.
249877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  *thread_out = reinterpret_cast<pthread_t>(thread);
250b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes  pthread_mutex_unlock(&thread->startup_handshake_mutex);
2514b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2524b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return 0;
2534b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
254