14906e5653c57d49f94940f28556009a88c42a583Elliott Hughes/* 24906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * Copyright (C) 2013 The Android Open Source Project 34906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * All rights reserved. 44906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * 54906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * Redistribution and use in source and binary forms, with or without 64906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * modification, are permitted provided that the following conditions 74906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * are met: 84906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * * Redistributions of source code must retain the above copyright 94906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * notice, this list of conditions and the following disclaimer. 104906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * * Redistributions in binary form must reproduce the above copyright 114906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * notice, this list of conditions and the following disclaimer in 124906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * the documentation and/or other materials provided with the 134906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * distribution. 144906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * 154906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 164906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 174906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 184906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 194906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 204906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 214906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 224906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 234906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 244906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 254906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 264906e5653c57d49f94940f28556009a88c42a583Elliott Hughes * SUCH DAMAGE. 274906e5653c57d49f94940f28556009a88c42a583Elliott Hughes */ 284906e5653c57d49f94940f28556009a88c42a583Elliott Hughes 29851e68a2402fa414544e66650e09dfdaac813e51Elliott Hughes#include <private/bionic_asm.h> 304906e5653c57d49f94940f28556009a88c42a583Elliott Hughes 3170b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes// pid_t __bionic_clone(int flags, void* child_stack, pid_t* parent_tid, void* tls, pid_t* child_tid, int (*fn)(void*), void* arg); 32beb879662470fb01f8062c173d9e6fc1b76988eeElliott HughesENTRY_PRIVATE(__bionic_clone) 3353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes # Copy 'fn' and 'arg' onto the child stack. 3453bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes movq %r9, -16(%rsi) # fn 3553bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes movq 8(%rsp), %rax # Read 'arg'. 3653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes movq %rax, -8(%rsi) # Write 'arg'. 374906e5653c57d49f94940f28556009a88c42a583Elliott Hughes 384906e5653c57d49f94940f28556009a88c42a583Elliott Hughes subq $16, %rsi 3936d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes 4036d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes # Translate to the kernel calling convention and swap the 'tls' and 'child_tid' arguments. 4136d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes # They're flipped for x86-64 compared to all our other architectures and __bionic_clone. 424906e5653c57d49f94940f28556009a88c42a583Elliott Hughes movq %r8, %r10 434906e5653c57d49f94940f28556009a88c42a583Elliott Hughes movq %rcx, %r8 4436d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes 4536d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes # Make the system call. 464906e5653c57d49f94940f28556009a88c42a583Elliott Hughes movl $__NR_clone, %eax 474906e5653c57d49f94940f28556009a88c42a583Elliott Hughes syscall 48aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes 49aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes # Check result. 50aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes testq %rax, %rax 51aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes jz .L_bc_child 52aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes jg .L_bc_parent 534906e5653c57d49f94940f28556009a88c42a583Elliott Hughes 5453bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes # An error occurred, set errno and return -1. 554906e5653c57d49f94940f28556009a88c42a583Elliott Hughes negl %eax 564906e5653c57d49f94940f28556009a88c42a583Elliott Hughes movl %eax, %edi 577efad83d430f4d824f2aaa75edea5106f6ff8aaeElliott Hughes call __set_errno_internal 58aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes ret 59aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes 60aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes.L_bc_child: 61aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes # We don't want anyone to unwind past this point. 62aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes .cfi_undefined %rip 63aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes .cfi_undefined %rbp 644906e5653c57d49f94940f28556009a88c42a583Elliott Hughes 65ebc8cd117a562f387c52ed4e1aeba0fb21f33194Elliott Hughes # We're in the child now, so call __start_thread 6653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes # with the arguments from the child stack moved into 6753bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes # the appropriate registers. 6853bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes popq %rdi # fn 6953bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes popq %rsi # arg 70ebc8cd117a562f387c52ed4e1aeba0fb21f33194Elliott Hughes call __start_thread 714906e5653c57d49f94940f28556009a88c42a583Elliott Hughes hlt 72aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes 73aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes.L_bc_parent: 74aeb3016f8132689d1b49d30056005b667e3d2d0eElliott Hughes # We're the parent; nothing to do. 754906e5653c57d49f94940f28556009a88c42a583Elliott Hughes ret 76507cfe2e10a6c4ad61b9638820ba10bfe881a18cChristopher FerrisEND(__bionic_clone) 77