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 | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 36 | # Enforce 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 | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 39 | # Copy 'fn', 'arg', and 'tls' 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 | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 60 | # We're in the child now, so call __thread_entry |
Elliott Hughes | 938f38d | 2013-10-17 22:22:31 -0700 | [diff] [blame] | 61 | # with the arguments from the child stack moved into |
| 62 | # the appropriate registers. |
Elliott Hughes | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 63 | popq %rdi # fn |
| 64 | popq %rsi # arg |
| 65 | popq %rdx # tls |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 66 | call __thread_entry |
| 67 | hlt |
| 68 | 2: |
| 69 | ret |
| 70 | |
Elliott Hughes | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 71 | // int __bionic_clone(unsigned long clone_flags, |
| 72 | // void* new_sp, |
| 73 | // int* parent_tid_ptr, |
| 74 | // void* new_tls, |
| 75 | // int* child_tid_ptr, |
| 76 | // int (*fn)(void*), |
| 77 | // void* arg); |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 78 | ENTRY(__bionic_clone) |
Elliott Hughes | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 79 | # Enforce 16-byte alignment for child stack. |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 80 | andq $~15, %rsi |
Elliott Hughes | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 81 | |
| 82 | # Copy 'fn' and 'arg' onto the child stack. |
| 83 | movq %r9, -16(%rsi) # fn |
| 84 | movq 8(%rsp), %rax # Read 'arg'. |
| 85 | movq %rax, -8(%rsi) # Write 'arg'. |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 86 | |
| 87 | subq $16, %rsi |
| 88 | movq %r8, %r10 |
| 89 | movq %rcx, %r8 |
| 90 | movl $__NR_clone, %eax |
| 91 | syscall |
| 92 | testl %eax, %eax |
| 93 | jns 1f |
| 94 | |
Elliott Hughes | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 95 | # An error occurred, set errno and return -1. |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 96 | negl %eax |
| 97 | movl %eax, %edi |
| 98 | call __set_errno |
| 99 | orl $-1, %eax |
| 100 | jmp 2f |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 101 | 1: |
| 102 | jnz 2f |
| 103 | |
Elliott Hughes | 53bfdae | 2013-10-18 19:39:09 -0700 | [diff] [blame^] | 104 | # We're in the child now, so call __bionic_clone_entry |
| 105 | # with the arguments from the child stack moved into |
| 106 | # the appropriate registers. |
| 107 | popq %rdi # fn |
| 108 | popq %rsi # arg |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 109 | call __bionic_clone_entry |
| 110 | hlt |
Elliott Hughes | 4906e56 | 2013-10-04 14:55:30 -0700 | [diff] [blame] | 111 | 2: |
| 112 | ret |