clone.cpp revision 6203e7b853a587fbd70cea2e58b63ae38a71a13e
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 2997cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner#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 3436d6188f8cd8b948fb797f11d9620d63d0c2215aElliott 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); 356203e7b853a587fbd70cea2e58b63ae38a71a13eElliott Hughesextern "C" __noreturn void __exit(int status); 3697cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner 3736d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes// Called from the __bionic_clone assembler to call the thread function then exit. 38954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughesextern "C" __LIBC_HIDDEN__ void __bionic_clone_entry(int (*fn)(void*), void* arg) { 39c4c6e192ac045c06f4aad3afc8e437baf67227b7Elliott Hughes int status = (*fn)(arg); 406b53c2349a5a3fc70a475de6a66131b615e88e48Elliott Hughes __exit(status); 4197cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner} 4297cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner 43c4c6e192ac045c06f4aad3afc8e437baf67227b7Elliott Hughesint clone(int (*fn)(void*), void* child_stack, int flags, void* arg, ...) { 4436d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes int* parent_tid = NULL; 4536d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes void* new_tls = NULL; 4636d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes int* child_tid = NULL; 4797cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner 4836d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes // Extract any optional parameters required by the flags. 4936d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes va_list args; 5036d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes va_start(args, arg); 5136d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes if ((flags & (CLONE_PARENT_SETTID|CLONE_SETTLS|CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) != 0) { 5236d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes parent_tid = va_arg(args, int*); 5336d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes } 5436d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes if ((flags & (CLONE_SETTLS|CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) != 0) { 5536d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes new_tls = va_arg(args, void*); 5636d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes } 5736d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes if ((flags & (CLONE_CHILD_SETTID|CLONE_CHILD_CLEARTID)) != 0) { 5836d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes child_tid = va_arg(args, int*); 5936d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes } 6036d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes va_end(args); 6197cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner 620d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes // Align 'child_stack' to 16 bytes. 630d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes uintptr_t child_stack_addr = reinterpret_cast<uintptr_t>(child_stack); 640d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes child_stack_addr &= ~0xf; 650d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes child_stack = reinterpret_cast<void*>(child_stack_addr); 660d236aa3f1e6d31b0c729448ae9d3ed1cad23fb4Elliott Hughes 6736d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes return __bionic_clone(flags, child_stack, parent_tid, new_tls, child_tid, fn, arg); 6897cf7f3394780d524038fc083e2c134031b54728David 'Digit' Turner} 69