The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 2 | * Copyright (C) 2008-2010 The Android Open Source Project |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | #include <sys/linux-syscalls.h> |
Kenny Root | 420878c | 2011-02-16 11:55:58 -0800 | [diff] [blame^] | 29 | #include <machine/asm.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | |
Kenny Root | 420878c | 2011-02-16 11:55:58 -0800 | [diff] [blame^] | 31 | ENTRY(__pthread_clone) |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 32 | @ insert the args onto the new stack |
| 33 | str r0, [r1, #-4] |
| 34 | str r3, [r1, #-8] |
| 35 | |
| 36 | @ do the system call |
| 37 | @ get flags |
| 38 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | mov r0, r2 |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 40 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | @ new sp is already in r1 |
| 42 | |
| 43 | #if __ARM_EABI__ |
| 44 | stmfd sp!, {r4, r7} |
| 45 | ldr r7, =__NR_clone |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 46 | swi #0 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | #else |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 48 | swi #__NR_clone |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 49 | #endif |
| 50 | |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 51 | movs r0, r0 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 52 | #if __ARM_EABI__ |
| 53 | ldmnefd sp!, {r4, r7} |
| 54 | #endif |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 55 | blt __error |
| 56 | bxne lr |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 | |
| 58 | |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 59 | @ pick the function arg and call address off the stack and jump |
| 60 | @ to the C __thread_entry function which does some setup and then |
| 61 | @ calls the thread's start function |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 62 | |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 63 | ldr r0, [sp, #-4] |
| 64 | ldr r1, [sp, #-8] |
| 65 | mov r2, sp @ __thread_entry needs the TLS pointer |
| 66 | b __thread_entry |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 67 | |
| 68 | __error: |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 69 | mov r0, #-1 |
| 70 | bx lr |
Kenny Root | 420878c | 2011-02-16 11:55:58 -0800 | [diff] [blame^] | 71 | END(__pthread_clone) |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 72 | |
| 73 | |
| 74 | # |
| 75 | # This function is defined as: |
| 76 | # |
| 77 | # pid_t __bionic_clone( int flags, void *child_stack, |
| 78 | # pid_t *pid, void *tls, pid_t *ctid, |
| 79 | # int (*fn)(void *), void* arg ); |
| 80 | # |
| 81 | # NOTE: This is not the same signature than the GLibc |
| 82 | # __clone function here !! Placing 'fn' and 'arg' |
| 83 | # at the end of the parameter list makes the |
| 84 | # implementation much simpler. |
| 85 | # |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 86 | |
Kenny Root | 420878c | 2011-02-16 11:55:58 -0800 | [diff] [blame^] | 87 | ENTRY(__bionic_clone) |
David 'Digit' Turner | 97cf7f3 | 2010-01-22 18:59:05 -0800 | [diff] [blame] | 88 | mov ip, sp |
| 89 | .save {r4, r5, r6, r7} |
| 90 | |
| 91 | # save registers to parent stack |
| 92 | stmfd sp!, {r4, r5, r6, r7} |
| 93 | |
| 94 | # load extra parameters |
| 95 | ldmfd ip, {r4, r5, r6} |
| 96 | |
| 97 | # store 'fn' and 'arg' to the child stack |
| 98 | str r5, [r1, #-4] |
| 99 | str r6, [r1, #-8] |
| 100 | |
| 101 | # system call |
| 102 | ldr r7, =__NR_clone |
| 103 | swi #0 |
| 104 | movs r0, r0 |
| 105 | beq 1f |
| 106 | |
| 107 | # in parent, reload saved registers |
| 108 | # then either exit or error |
| 109 | # |
| 110 | ldmfd sp!, {r4, r5, r6, r7} |
| 111 | bxne lr |
| 112 | b __set_syscall_errno |
| 113 | |
| 114 | 1: # in the child - pick arguments |
| 115 | ldr r0, [sp, #-4] |
| 116 | ldr r1, [sp, #-8] |
| 117 | b __bionic_clone_entry |
Kenny Root | 420878c | 2011-02-16 11:55:58 -0800 | [diff] [blame^] | 118 | END(__bionic_clone) |