pthread_create.cpp revision f2cea021ab2c6d7d7feeb40cca098aa132605876
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>
334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
344b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "pthread_internal.h"
354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_ssp.h"
374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_tls.h"
388f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include "private/libc_logging.h"
394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/thread_private.h"
404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ErrnoRestorer.h"
414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ScopedPthreadMutexLocker.h"
424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
4370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughesextern "C" pid_t __bionic_clone(uint32_t flags, void* child_stack, int* parent_tid, void* tls, int* child_tid, int (*fn)(void*), void* arg);
4480906141f79be8be63fc915bfab467029b442ca1Elliott Hughesextern "C" int __set_tls(void*);
454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
46af8aebebb52d73ea38c604525a6a5857618861cfElliott Hughes// Used by gdb to track thread creation. See libthread_db.
474b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifdef __i386__
48af8aebebb52d73ea38c604525a6a5857618861cfElliott Hughesextern "C" __attribute__((noinline)) __attribute__((fastcall)) void _thread_created_hook(pid_t) {}
494b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#else
50af8aebebb52d73ea38c604525a6a5857618861cfElliott Hughesextern "C" __attribute__((noinline)) void _thread_created_hook(pid_t) {}
514b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif
524b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
534b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic pthread_mutex_t gPthreadStackCreationLock = PTHREAD_MUTEX_INITIALIZER;
544b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
554b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic pthread_mutex_t gDebuggerNotificationLock = PTHREAD_MUTEX_INITIALIZER;
564b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
57f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughesextern "C" int __isthreaded;
58f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes
5970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes// This code is used both by each new pthread and the code that initializes the main thread.
60af8aebebb52d73ea38c604525a6a5857618861cfElliott Hughesvoid __init_tls(pthread_internal_t* thread) {
6170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // Zero-initialize all the slots after TLS_SLOT_SELF and TLS_SLOT_THREAD_ID.
6270b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  for (size_t i = TLS_SLOT_ERRNO; i < BIONIC_TLS_SLOTS; ++i) {
6340eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    thread->tls[i] = NULL;
644b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
6680906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#if defined(__i386__)
6780906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  __set_tls(thread->tls);
6880906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#endif
6980906141f79be8be63fc915bfab467029b442ca1Elliott Hughes
704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0.
7140eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_SELF] = thread->tls;
7240eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_THREAD_ID] = thread;
734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // GCC looks in the TLS for the stack guard on x86, so copy it there from our global.
7440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes  thread->tls[TLS_SLOT_STACK_GUARD] = (void*) __stack_chk_guard;
7570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes}
764b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
7770b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughesvoid __init_alternate_signal_stack(pthread_internal_t* thread) {
7884114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // Create and set an alternate signal stack.
7984114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  stack_t ss;
8084114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  ss.ss_sp = mmap(NULL, SIGSTKSZ, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
8184114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  if (ss.ss_sp != MAP_FAILED) {
8284114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    ss.ss_size = SIGSTKSZ;
8384114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    ss.ss_flags = 0;
8484114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    sigaltstack(&ss, NULL);
8584114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes    thread->alternate_signal_stack = ss.ss_sp;
8684114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  }
874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
89cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughesint __init_thread(pthread_internal_t* thread, bool add_to_thread_list) {
904b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int error = 0;
914b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
924b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Set the scheduling policy/priority of the thread.
934b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread->attr.sched_policy != SCHED_NORMAL) {
94c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes    sched_param param;
954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    param.sched_priority = thread->attr.sched_priority;
9640eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    if (sched_setscheduler(thread->tid, thread->attr.sched_policy, &param) == -1) {
9798624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#if __LP64__
9898624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      // For backwards compatibility reasons, we only report failures on 64-bit devices.
9998624c374646a050556bdc402b55b792fefa7e55Elliott Hughes      error = errno;
10098624c374646a050556bdc402b55b792fefa7e55Elliott Hughes#endif
101b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes      __libc_format_log(ANDROID_LOG_WARN, "libc",
102b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                        "pthread_create sched_setscheduler call failed: %s", strerror(errno));
1034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
1044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  thread->cleanup_stack = NULL;
1074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1084b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (add_to_thread_list) {
1094b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    _pthread_internal_add(thread);
1104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return error;
1134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
115b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughesstatic void* __create_thread_stack(pthread_internal_t* thread) {
1164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  ScopedPthreadMutexLocker lock(&gPthreadStackCreationLock);
1174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Create a new private anonymous map.
1194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int prot = PROT_READ | PROT_WRITE;
1204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
121b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  void* stack = mmap(NULL, thread->attr.stack_size, prot, flags, -1, 0);
1224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (stack == MAP_FAILED) {
123b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN,
124b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "libc",
125b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "pthread_create failed: couldn't allocate %zd-byte stack: %s",
126b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      thread->attr.stack_size, strerror(errno));
1274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Set the guard region at the end of the stack to PROT_NONE.
131b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  if (mprotect(stack, thread->attr.guard_size, PROT_NONE) == -1) {
132b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc",
133b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      "pthread_create failed: couldn't mprotect PROT_NONE %zd-byte stack guard region: %s",
134b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes                      thread->attr.guard_size, strerror(errno));
135b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    munmap(stack, thread->attr.stack_size);
1364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return NULL;
1374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1384b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return stack;
1404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
1414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
142e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughesstatic int __pthread_start(void* arg) {
143e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(arg);
144e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
145e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // Wait for our creating thread to release us. This lets it have time to
146e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // notify gdb about this thread before we start doing anything.
147e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // This also provides the memory barrier needed to ensure that all memory
148e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  // accesses previously made by the creating thread are visible to us.
149e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_mutex_t* start_mutex = (pthread_mutex_t*) &thread->tls[TLS_SLOT_START_MUTEX];
150e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_mutex_lock(start_mutex);
151e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_mutex_destroy(start_mutex);
152e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
153e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  __init_tls(thread);
154e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
155e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  __init_alternate_signal_stack(thread);
156e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
157e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  void* result = thread->start_routine(thread->start_routine_arg);
158e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  pthread_exit(result);
159e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
160e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes  return 0;
161e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes}
162e48b68570d872ef7ece1d873c0ea298ea76393f3Elliott Hughes
163cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// A dummy start routine for pthread_create failures where we've created a thread but aren't
164cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// going to run user code on it. We swap out the user's start routine for this and take advantage
165cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes// of the regular thread teardown to free up resources.
166cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughesstatic void* __do_nothing(void*) {
167cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes  return NULL;
168cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes}
169cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes
1704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesint pthread_create(pthread_t* thread_out, pthread_attr_t const* attr,
1714b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes                   void* (*start_routine)(void*), void* arg) {
1724b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  ErrnoRestorer errno_restorer;
1734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
174f2cea021ab2c6d7d7feeb40cca098aa132605876Elliott Hughes  // Inform the rest of the C library that at least one thread was created.
1754b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  __isthreaded = 1;
1764b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1774b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(calloc(sizeof(*thread), 1));
1784b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread == NULL) {
179cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: couldn't allocate thread");
1804b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return EAGAIN;
1814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1834b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (attr == NULL) {
1844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    pthread_attr_init(&thread->attr);
1854b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  } else {
1864b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    thread->attr = *attr;
1874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    attr = NULL; // Prevent misuse below.
1884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
1894b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
190b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  // Make sure the stack size and guard size are multiples of PAGE_SIZE.
191b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  thread->attr.stack_size = (thread->attr.stack_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
192b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes  thread->attr.guard_size = (thread->attr.guard_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
1934b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
1944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (thread->attr.stack_base == NULL) {
1954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    // The caller didn't provide a stack, so allocate one.
196b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes    thread->attr.stack_base = __create_thread_stack(thread);
1974b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    if (thread->attr.stack_base == NULL) {
1984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes      free(thread);
1994b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes      return EAGAIN;
2004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
2014b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  } else {
2024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    // The caller did provide a stack, so remember we're not supposed to free it.
2032b6e43e00ece68b3aba26d8f95f07cd9d9294ab4Elliott Hughes    thread->attr.flags |= PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK;
2044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
20684114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // Make room for the TLS area.
20784114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // The child stack is the same address, just growing in the opposite direction.
20884114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // At offsets >= 0, we have the TLS slots.
20984114c8dd5b17efecf7988f263ce431208d7be5aElliott Hughes  // At offsets < 0, we have the child stack.
21070b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->tls = (void**)((uint8_t*)(thread->attr.stack_base) + thread->attr.stack_size - BIONIC_TLS_SLOTS * sizeof(void*));
21170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  void* child_stack = thread->tls;
2124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
21370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  // Create a mutex for the thread in TLS to wait on once it starts so we can keep
2144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // it from doing anything until after we notify the debugger about it
2154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  //
2164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // This also provides the memory barrier we need to ensure that all
2174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // memory accesses previously performed by this thread are visible to
2184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // the new thread.
21970b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  pthread_mutex_t* start_mutex = (pthread_mutex_t*) &thread->tls[TLS_SLOT_START_MUTEX];
2204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  pthread_mutex_init(start_mutex, NULL);
221877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_mutex_lock(start_mutex);
2224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
22370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->tls[TLS_SLOT_THREAD_ID] = thread;
2244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
22570b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine = start_routine;
22670b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes  thread->start_routine_arg = arg;
2274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
228877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  int flags = CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM |
229877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes      CLONE_SETTLS | CLONE_PARENT_SETTID | CLONE_CHILD_CLEARTID;
23080906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#if defined(__i386__)
23180906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  // On x86 (but not x86-64), CLONE_SETTLS takes a pointer to a struct user_desc rather than
23280906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  // a pointer to the TLS itself. Rather than try to deal with that here, we just let x86 set
23380906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  // the TLS manually in __init_tls, like all architectures used to.
23480906141f79be8be63fc915bfab467029b442ca1Elliott Hughes  flags &= ~CLONE_SETTLS;
23580906141f79be8be63fc915bfab467029b442ca1Elliott Hughes#endif
236877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  int rc = __bionic_clone(flags, child_stack, &(thread->tid), thread->tls, &(thread->tid), __pthread_start, thread);
237877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  if (rc == -1) {
2384b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    int clone_errno = errno;
239877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // We don't have to unlock the mutex at all because clone(2) failed so there's no child waiting to
240877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // be unblocked, but we're about to unmap the memory the mutex is stored in, so this serves as a
241877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    // reminder that you can't rewrite this function to use a ScopedPthreadMutexLocker.
242877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes    pthread_mutex_unlock(start_mutex);
2432b6e43e00ece68b3aba26d8f95f07cd9d9294ab4Elliott Hughes    if ((thread->attr.flags & PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK) == 0) {
244b95cf0d23a1db3b7c37bd98b0c86196796c9b029Elliott Hughes      munmap(thread->attr.stack_base, thread->attr.stack_size);
2454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    }
2464b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    free(thread);
247cfa089df23ff50fcd5ed3854c54991d30be5fc7eElliott Hughes    __libc_format_log(ANDROID_LOG_WARN, "libc", "pthread_create failed: clone failed: %s", strerror(errno));
2484b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return clone_errno;
2494b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2504b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
251cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes  int init_errno = __init_thread(thread, true);
2524b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  if (init_errno != 0) {
253cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Mark the thread detached and replace its start_routine with a no-op.
254cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    // Letting the thread run is the easiest way to clean up its resources.
2554b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
256cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    thread->start_routine = __do_nothing;
257cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes    pthread_mutex_unlock(start_mutex);
2584b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    return init_errno;
2594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  // Notify any debuggers about the new thread.
2624b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  {
2634b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes    ScopedPthreadMutexLocker debugger_locker(&gDebuggerNotificationLock);
26440eabe24e4e3ae8ebe437f1f4e43cf39cbba2e9eElliott Hughes    _thread_created_hook(thread->tid);
2654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  }
2664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
267877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  // Publish the pthread_t and unlock the mutex to let the new thread start running.
268877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  *thread_out = reinterpret_cast<pthread_t>(thread);
269877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes  pthread_mutex_unlock(start_mutex);
2704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes
2714b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes  return 0;
2724b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes}
273