197cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner/*
297cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * Copyright (C) 2010 The Android Open Source Project
397cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * All rights reserved.
497cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *
597cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * Redistribution and use in source and binary forms, with or without
697cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * modification, are permitted provided that the following conditions
797cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * are met:
897cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *  * Redistributions of source code must retain the above copyright
997cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *    notice, this list of conditions and the following disclaimer.
1097cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *  * Redistributions in binary form must reproduce the above copyright
1197cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *    notice, this list of conditions and the following disclaimer in
1297cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *    the documentation and/or other materials provided with the
1397cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *    distribution.
1497cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner *
1597cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1697cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1797cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1897cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
1997cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2097cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2197cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2297cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2397cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2497cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2597cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2697cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner * SUCH DAMAGE.
2797cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner */
2836d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes
295f5cc45cf0e027f6ca503dc229a4890fc7164b66Elliott Hughes#define _GNU_SOURCE 1
3097cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner#include <sched.h>
3197cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner#include <stdlib.h>
3297cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner#include <stdarg.h>
3397cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner
34fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes#include "pthread_internal.h"
35fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes
3636d6188f8cd8b948fb797f11d9620d63d0c2215aElliott 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);
376203e7b853a587fbd70cea2e58b63ae38a71a13eElliott Hughesextern "C" __noreturn void __exit(int status);
3897cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner
3936d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes// Called from the __bionic_clone assembler to call the thread function then exit.
40ebc8cd117a562f387c52ed4e1aeba0fb21f33194Elliott Hughesextern "C" __LIBC_HIDDEN__ void __start_thread(int (*fn)(void*), void* arg) {
41c4c6e192ac045c06f4aad3afc8e437baf67227b7Elliott Hughes  int status = (*fn)(arg);
426b53c2349a5a3fc70a475de6a66131b615e88e48Elliott Hughes  __exit(status);
4397cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner}
4497cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner
45c4c6e192ac045c06f4aad3afc8e437baf67227b7Elliott Hughesint clone(int (*fn)(void*), void* child_stack, int flags, void* arg, ...) {
4636d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  int* parent_tid = NULL;
4736d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  void* new_tls = NULL;
4836d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  int* child_tid = NULL;
4997cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner
5036d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  // Extract any optional parameters required by the flags.
5136d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  va_list args;
5236d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  va_start(args, arg);
5336d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  if ((flags & (CLONE_PARENT_SETTID|CLONE_SETTLS|CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) != 0) {
5436d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes    parent_tid = va_arg(args, int*);
5536d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  }
5636d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  if ((flags & (CLONE_SETTLS|CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) != 0) {
5736d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes    new_tls = va_arg(args, void*);
5836d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  }
5936d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  if ((flags & (CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) != 0) {
6036d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes    child_tid = va_arg(args, int*);
6136d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  }
6236d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes  va_end(args);
6397cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner
640d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  // Align 'child_stack' to 16 bytes.
650d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  uintptr_t child_stack_addr = reinterpret_cast<uintptr_t>(child_stack);
660d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  child_stack_addr &= ~0xf;
670d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes  child_stack = reinterpret_cast<void*>(child_stack_addr);
680d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes
69fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  // Remember the parent pid and invalidate the cached value while we clone.
70fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  pthread_internal_t* self = __get_thread();
71fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  pid_t parent_pid = self->invalidate_cached_pid();
72fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes
73fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  // Actually do the clone.
74fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  int clone_result = __bionic_clone(flags, child_stack, parent_tid, new_tls, child_tid, fn, arg);
75fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes
76fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  // We're the parent, so put our known pid back in place.
77fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  // We leave the child without a cached pid, but:
78fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  // 1. pthread_create gives its children their own pthread_internal_t with the correct pid.
79fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  // 2. fork makes a clone system call directly.
80fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  // If any other cases become important, we could use a double trampoline like __pthread_start.
81fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  self->set_cached_pid(parent_pid);
82fa9e16efaf0e885f6044e725eb759ef6de10f7efElliott Hughes  return clone_result;
8397cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner}
84