Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <asm/unistd.h> |
| 30 | #include <machine/asm.h> |
| 31 | |
| 32 | // int __pthread_clone(void* (*fn)(void*), void* tls, int flags, void* arg); |
| 33 | ENTRY(__pthread_clone) |
| 34 | # save tls |
| 35 | movq %rsi, %r11 |
| 36 | # 16-byte alignment on child stack |
| 37 | andq $~15, %rsi |
| 38 | |
| 39 | # insert arguments onto the child stack |
| 40 | movq %rdi, -32(%rsi) |
| 41 | movq %rcx, -24(%rsi) |
| 42 | movq %r11, -16(%rsi) |
| 43 | |
| 44 | subq $32, %rsi |
| 45 | movq %rdx, %rdi |
| 46 | movl $__NR_clone, %eax |
| 47 | syscall |
| 48 | testl %eax, %eax |
| 49 | jns 1f |
| 50 | |
| 51 | # an error occurred, set errno and return -1 |
| 52 | negl %eax |
| 53 | movl %eax, %edi |
| 54 | call __set_errno |
| 55 | orl $-1, %eax |
| 56 | jmp 2f |
| 57 | 1: |
| 58 | jnz 2f |
| 59 | |
| 60 | # we're in the child thread now, call __thread_entry |
| 61 | # with the appropriate arguments on the child stack |
| 62 | # we already placed most of them |
| 63 | call __thread_entry |
| 64 | hlt |
| 65 | 2: |
| 66 | ret |
| 67 | |
| 68 | /* |
| 69 | * int __bionic_clone(unsigned long clone_flags, |
| 70 | * void* newsp, |
| 71 | * int *parent_tidptr, |
| 72 | * void *new_tls, |
| 73 | * int *child_tidptr, |
| 74 | * int (*fn)(void *), |
| 75 | * void *arg); |
| 76 | */ |
| 77 | ENTRY(__bionic_clone) |
| 78 | # insert arguments onto the child stack |
| 79 | andq $~15, %rsi |
| 80 | movq %r9, -16(%rsi) |
| 81 | # 7th argument (arg) goes through stack |
| 82 | movq 8(%rsp), %rax |
| 83 | movq %rax, -8(%rsi) |
| 84 | |
| 85 | subq $16, %rsi |
| 86 | movq %r8, %r10 |
| 87 | movq %rcx, %r8 |
| 88 | movl $__NR_clone, %eax |
| 89 | syscall |
| 90 | testl %eax, %eax |
| 91 | jns 1f |
| 92 | |
| 93 | # an error occurred, set errno and return -1 |
| 94 | negl %eax |
| 95 | movl %eax, %edi |
| 96 | call __set_errno |
| 97 | orl $-1, %eax |
| 98 | jmp 2f |
| 99 | |
| 100 | 1: |
| 101 | jnz 2f |
| 102 | |
| 103 | # we're in the child now, call __bionic_clone_entry |
| 104 | # with the appropriate arguments on the child stack |
| 105 | # we already placed most of them |
| 106 | call __bionic_clone_entry |
| 107 | hlt |
| 108 | |
| 109 | 2: |
| 110 | ret |
| 111 | |