__bionic_clone.S revision 851e68a2402fa414544e66650e09dfdaac813e51
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);
324906e5653c57d49f94940f28556009a88c42a583Elliott HughesENTRY(__bionic_clone)
3353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        # Enforce 16-byte alignment for child stack.
344906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        andq    $~15, %rsi
3553bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
3653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        # Copy 'fn' and 'arg' onto the child stack.
3753bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        movq    %r9, -16(%rsi)  # fn
3853bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        movq    8(%rsp), %rax   # Read 'arg'.
3953bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        movq    %rax, -8(%rsi)  # Write 'arg'.
404906e5653c57d49f94940f28556009a88c42a583Elliott Hughes
414906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        subq    $16, %rsi
4236d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes
4336d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes        # Translate to the kernel calling convention and swap the 'tls' and 'child_tid' arguments.
4436d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes        # They're flipped for x86-64 compared to all our other architectures and __bionic_clone.
454906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        movq    %r8, %r10
464906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        movq    %rcx, %r8
4736d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes
4836d6188f8cd8b948fb797f11d9620d63d0c2215aElliott Hughes        # Make the system call.
494906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        movl    $__NR_clone, %eax
504906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        syscall
514906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        testl   %eax, %eax
524906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        jns     1f
534906e5653c57d49f94940f28556009a88c42a583Elliott Hughes
5453bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        # An error occurred, set errno and return -1.
554906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        negl    %eax
564906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        movl    %eax, %edi
574906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        call    __set_errno
584906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        orl     $-1, %eax
594906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        jmp     2f
604906e5653c57d49f94940f28556009a88c42a583Elliott Hughes1:
614906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        jnz     2f
624906e5653c57d49f94940f28556009a88c42a583Elliott Hughes
6353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        # We're in the child now, so call __bionic_clone_entry
6453bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        # with the arguments from the child stack moved into
6553bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        # the appropriate registers.
6653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        popq    %rdi  # fn
6753bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes        popq    %rsi  # arg
684906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        call    __bionic_clone_entry
694906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        hlt
704906e5653c57d49f94940f28556009a88c42a583Elliott Hughes2:
714906e5653c57d49f94940f28556009a88c42a583Elliott Hughes        ret
72507cfe2e10a6c4ad61b9638820ba10bfe881a18cChristopher FerrisEND(__bionic_clone)
73