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) |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 34 | # Save tls. |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 35 | movq %rsi, %r11 |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 36 | # 16-byte alignment for child stack. |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 37 | andq $~15, %rsi |
| 38 | |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 39 | # Copy arguments onto the child stack. |
| 40 | movq %rdi, -32(%rsi) # fn |
| 41 | movq %rcx, -24(%rsi) # arg |
| 42 | movq %r11, -16(%rsi) # tls |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 43 | subq $32, %rsi |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 44 | |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 45 | movq %rdx, %rdi |
| 46 | movl $__NR_clone, %eax |
| 47 | syscall |
| 48 | testl %eax, %eax |
| 49 | jns 1f |
| 50 | |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 51 | # An error occurred, set errno and return -1. |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 52 | negl %eax |
| 53 | movl %eax, %edi |
| 54 | call __set_errno |
| 55 | orl $-1, %eax |
| 56 | jmp 2f |
| 57 | 1: |
| 58 | jnz 2f |
| 59 | |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 60 | # We're in the child thread now, call __thread_entry |
| 61 | # with the arguments from the child stack moved into |
| 62 | # the appropriate registers. |
| 63 | popq %rdi |
| 64 | popq %rsi |
| 65 | popq %rdx |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 66 | call __thread_entry |
| 67 | hlt |
| 68 | 2: |
| 69 | ret |
| 70 | |
| 71 | /* |
| 72 | * int __bionic_clone(unsigned long clone_flags, |
| 73 | * void* newsp, |
| 74 | * int *parent_tidptr, |
| 75 | * void *new_tls, |
| 76 | * int *child_tidptr, |
| 77 | * int (*fn)(void *), |
| 78 | * void *arg); |
| 79 | */ |
| 80 | ENTRY(__bionic_clone) |
| 81 | # insert arguments onto the child stack |
| 82 | andq $~15, %rsi |
| 83 | movq %r9, -16(%rsi) |
| 84 | # 7th argument (arg) goes through stack |
| 85 | movq 8(%rsp), %rax |
| 86 | movq %rax, -8(%rsi) |
| 87 | |
| 88 | subq $16, %rsi |
| 89 | movq %r8, %r10 |
| 90 | movq %rcx, %r8 |
| 91 | movl $__NR_clone, %eax |
| 92 | syscall |
| 93 | testl %eax, %eax |
| 94 | jns 1f |
| 95 | |
| 96 | # an error occurred, set errno and return -1 |
| 97 | negl %eax |
| 98 | movl %eax, %edi |
| 99 | call __set_errno |
| 100 | orl $-1, %eax |
| 101 | jmp 2f |
| 102 | |
| 103 | 1: |
| 104 | jnz 2f |
| 105 | |
| 106 | # we're in the child now, call __bionic_clone_entry |
| 107 | # with the appropriate arguments on the child stack |
| 108 | # we already placed most of them |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 109 | # TODO: write a test for __bionic_clone and then fix this too (see above). |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 110 | call __bionic_clone_entry |
| 111 | hlt |
| 112 | |
| 113 | 2: |
| 114 | ret |
| 115 | |