yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* Wrapper around clone system call. |
| 2 | Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #include <features.h> |
| 21 | #define _ERRNO_H 1 |
| 22 | #include <bits/errno.h> |
| 23 | #include <sysdep.h> |
| 24 | |
| 25 | #define CLONE_VM 0x00000100 |
| 26 | #define CLONE_THREAD 0x00010000 |
| 27 | |
| 28 | /* This is the only really unusual system call in PPC linux, but not |
| 29 | because of any weirdness in the system call itself; because of |
| 30 | all the freaky stuff we have to do to make the call useful. */ |
| 31 | |
| 32 | /* int [r3] clone(int (*fn)(void *arg) [r3], void *child_stack [r4], |
| 33 | int flags [r5], void *arg [r6], void *parent_tid [r7], |
| 34 | void *tls [r8], void *child_tid [r9]); */ |
| 35 | |
| 36 | #ifdef __NR_clone |
| 37 | .globl __clone |
| 38 | .type __clone,@function |
| 39 | .align 2 |
| 40 | |
| 41 | __clone: |
| 42 | /* Check for child_stack == NULL || fn == NULL. */ |
| 43 | cmpwi cr0,r4,0 |
| 44 | cmpwi cr1,r3,0 |
| 45 | cror cr0*4+eq,cr1*4+eq,cr0*4+eq |
| 46 | beq- cr0,.Lbadargs |
| 47 | |
| 48 | /* Set up stack frame for parent. */ |
| 49 | stwu r1,-32(r1) |
| 50 | cfi_adjust_cfa_offset (32) |
| 51 | #ifdef RESET_PID |
| 52 | stmw r28,16(r1) |
| 53 | #else |
| 54 | # ifndef __ASSUME_FIXED_CLONE_SYSCALL |
| 55 | stmw r29,16(r1) |
| 56 | # else |
| 57 | stmw r30,16(r1) |
| 58 | # endif |
| 59 | #endif |
| 60 | |
| 61 | /* Set up stack frame for child. */ |
| 62 | clrrwi r4,r4,4 |
| 63 | li r0,0 |
| 64 | stwu r0,-16(r4) |
| 65 | |
| 66 | /* Save fn, args, stack across syscall. */ |
| 67 | mr r30,r3 /* Function in r30. */ |
| 68 | #ifndef __ASSUME_FIXED_CLONE_SYSCALL |
| 69 | mr r29,r4 /* Stack pointer in r29. */ |
| 70 | #endif |
| 71 | #ifdef RESET_PID |
| 72 | mr r28,r5 |
| 73 | #endif |
| 74 | mr r31,r6 /* Argument in r31. */ |
| 75 | |
| 76 | /* 'flags' argument is first parameter to clone syscall. (The other |
| 77 | argument is the stack pointer, already in r4.) */ |
| 78 | mr r3,r5 |
| 79 | |
| 80 | /* Move the parent_tid, child_tid and tls arguments. */ |
| 81 | mr r5,r7 |
| 82 | mr r6,r8 |
| 83 | mr r7,r9 |
| 84 | |
| 85 | /* End FDE now, because in the child the unwind info will be wrong. */ |
| 86 | cfi_endproc |
| 87 | |
| 88 | /* Do the call. */ |
| 89 | li 0, __NR_clone |
| 90 | sc |
| 91 | |
| 92 | /* Check for child process. */ |
| 93 | cmpwi cr1,r3,0 |
| 94 | crandc cr1*4+eq,cr1*4+eq,cr0*4+so |
| 95 | bne- cr1,.Lparent /* The '-' is to minimise the race. */ |
| 96 | |
| 97 | #ifndef __ASSUME_FIXED_CLONE_SYSCALL |
| 98 | /* On at least mklinux DR3a5, clone() doesn't actually change |
| 99 | the stack pointer. I'm pretty sure this is a bug, because |
| 100 | it adds a race condition if a signal is sent to a thread |
| 101 | just after it is created (in the previous three instructions). */ |
| 102 | mr r1,r29 |
| 103 | #endif |
| 104 | |
| 105 | #ifdef RESET_PID |
| 106 | andis. r0,r28,CLONE_THREAD>>16 |
| 107 | bne+ r0,.Loldpid |
| 108 | andi. r0,r28,CLONE_VM |
| 109 | li r3,-1 |
| 110 | bne- r0,.Lnomoregetpid |
| 111 | .Lnomoregetpid: |
| 112 | stw r3,TID(r2) |
| 113 | stw r3,PID(r2) |
| 114 | .Loldpid: |
| 115 | #endif |
| 116 | /* Call procedure. */ |
| 117 | mtctr r30 |
| 118 | mr r3,r31 |
| 119 | bctrl |
| 120 | /* Call _exit with result from procedure. */ |
| 121 | b HIDDEN_JUMPTARGET(_exit) |
| 122 | |
| 123 | .Lparent: |
| 124 | /* Parent. Restore registers & return. */ |
| 125 | #ifdef RESET_PID |
| 126 | lmw r28,16(r1) |
| 127 | #else |
| 128 | # ifndef __ASSUME_FIXED_CLONE_SYSCALL |
| 129 | lmw r29,16(r1) |
| 130 | # else |
| 131 | lmw r30,16(r1) |
| 132 | # endif |
| 133 | #endif |
| 134 | addi r1,r1,32 |
| 135 | bnslr+ |
| 136 | b __syscall_error |
| 137 | |
| 138 | .Lbadargs: |
| 139 | li r3,EINVAL |
| 140 | b __syscall_error |
| 141 | |
| 142 | cfi_startproc |
| 143 | .size __clone,.-__clone |
| 144 | weak_alias(__clone, clone) |
| 145 | #endif |