1*0f4c859eSApple OSS Distributions/* 2*0f4c859eSApple OSS Distributions * Copyright (c) 1999-2007 Apple Inc. All rights reserved. 3*0f4c859eSApple OSS Distributions * 4*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*0f4c859eSApple OSS Distributions * 6*0f4c859eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*0f4c859eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*0f4c859eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*0f4c859eSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*0f4c859eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*0f4c859eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*0f4c859eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*0f4c859eSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*0f4c859eSApple OSS Distributions * 15*0f4c859eSApple OSS Distributions * Please obtain a copy of the License at 16*0f4c859eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*0f4c859eSApple OSS Distributions * 18*0f4c859eSApple OSS Distributions * The Original Code and all software distributed under the License are 19*0f4c859eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*0f4c859eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*0f4c859eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*0f4c859eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*0f4c859eSApple OSS Distributions * Please see the License for the specific language governing rights and 24*0f4c859eSApple OSS Distributions * limitations under the License. 25*0f4c859eSApple OSS Distributions * 26*0f4c859eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*0f4c859eSApple OSS Distributions */ 28*0f4c859eSApple OSS Distributions/* Copyright (c) 1998 Apple Computer, Inc. All rights reserved. 29*0f4c859eSApple OSS Distributions * 30*0f4c859eSApple OSS Distributions * File: libc/ppc/sys/vfork.s 31*0f4c859eSApple OSS Distributions * 32*0f4c859eSApple OSS Distributions * HISTORY 33*0f4c859eSApple OSS Distributions * 23-Jun-1998 Umesh Vaishampayan ([email protected]) 34*0f4c859eSApple OSS Distributions * Created from fork.s 35*0f4c859eSApple OSS Distributions * 36*0f4c859eSApple OSS Distributions */ 37*0f4c859eSApple OSS Distributions 38*0f4c859eSApple OSS Distributions#include "SYS.h" 39*0f4c859eSApple OSS Distributions 40*0f4c859eSApple OSS Distributions#if defined(__i386__) 41*0f4c859eSApple OSS Distributions 42*0f4c859eSApple OSS Distributions#if defined(__DYNAMIC__) 43*0f4c859eSApple OSS Distributions#define GET_CURRENT_PID PICIFY(__current_pid) 44*0f4c859eSApple OSS Distributions 45*0f4c859eSApple OSS Distributions NON_LAZY_STUB(__current_pid) 46*0f4c859eSApple OSS Distributions#define __current_pid (%edx) 47*0f4c859eSApple OSS Distributions#else 48*0f4c859eSApple OSS Distributions#define GET_CURRENT_PID 49*0f4c859eSApple OSS Distributions#endif 50*0f4c859eSApple OSS Distributions 51*0f4c859eSApple OSS Distributions/* 52*0f4c859eSApple OSS Distributions * If __current_pid >= 0, we want to put a -1 in there 53*0f4c859eSApple OSS Distributions * otherwise we just decrement it 54*0f4c859eSApple OSS Distributions */ 55*0f4c859eSApple OSS Distributions 56*0f4c859eSApple OSS DistributionsLEAF(___vfork, 0) 57*0f4c859eSApple OSS Distributions GET_CURRENT_PID 58*0f4c859eSApple OSS Distributions movl __current_pid, %eax 59*0f4c859eSApple OSS Distributions0: 60*0f4c859eSApple OSS Distributions xorl %ecx, %ecx 61*0f4c859eSApple OSS Distributions testl %eax, %eax 62*0f4c859eSApple OSS Distributions cmovs %eax, %ecx 63*0f4c859eSApple OSS Distributions decl %ecx 64*0f4c859eSApple OSS Distributions lock 65*0f4c859eSApple OSS Distributions cmpxchgl %ecx, __current_pid 66*0f4c859eSApple OSS Distributions jne 0b 67*0f4c859eSApple OSS Distributions popl %ecx 68*0f4c859eSApple OSS Distributions movl $(SYS_vfork), %eax // code for vfork -> eax 69*0f4c859eSApple OSS Distributions UNIX_SYSCALL_TRAP // do the system call 70*0f4c859eSApple OSS Distributions jnb L1 // jump if CF==0 71*0f4c859eSApple OSS Distributions GET_CURRENT_PID 72*0f4c859eSApple OSS Distributions lock 73*0f4c859eSApple OSS Distributions incl __current_pid 74*0f4c859eSApple OSS Distributions pushl %ecx 75*0f4c859eSApple OSS Distributions BRANCH_EXTERN(tramp_cerror) 76*0f4c859eSApple OSS Distributions 77*0f4c859eSApple OSS DistributionsL1: 78*0f4c859eSApple OSS Distributions testl %edx, %edx // CF=OF=0, ZF set if zero result 79*0f4c859eSApple OSS Distributions jz L2 // parent, since r1 == 0 in parent, 1 in child 80*0f4c859eSApple OSS Distributions xorl %eax, %eax // zero eax 81*0f4c859eSApple OSS Distributions jmp *%ecx 82*0f4c859eSApple OSS Distributions 83*0f4c859eSApple OSS DistributionsL2: 84*0f4c859eSApple OSS Distributions GET_CURRENT_PID 85*0f4c859eSApple OSS Distributions lock 86*0f4c859eSApple OSS Distributions incl __current_pid 87*0f4c859eSApple OSS Distributions jmp *%ecx 88*0f4c859eSApple OSS Distributions 89*0f4c859eSApple OSS Distributions#elif defined(__x86_64__) 90*0f4c859eSApple OSS Distributions 91*0f4c859eSApple OSS Distributions/* 92*0f4c859eSApple OSS Distributions * If __current_pid >= 0, we want to put a -1 in there 93*0f4c859eSApple OSS Distributions * otherwise we just decrement it 94*0f4c859eSApple OSS Distributions */ 95*0f4c859eSApple OSS Distributions 96*0f4c859eSApple OSS DistributionsLEAF(___vfork, 0) 97*0f4c859eSApple OSS Distributions movq __current_pid@GOTPCREL(%rip), %rax 98*0f4c859eSApple OSS Distributions movl (%rax), %eax 99*0f4c859eSApple OSS Distributions0: 100*0f4c859eSApple OSS Distributions xorl %ecx, %ecx 101*0f4c859eSApple OSS Distributions testl %eax, %eax 102*0f4c859eSApple OSS Distributions cmovs %eax, %ecx 103*0f4c859eSApple OSS Distributions subl $1, %ecx 104*0f4c859eSApple OSS Distributions movq __current_pid@GOTPCREL(%rip), %rdx 105*0f4c859eSApple OSS Distributions lock 106*0f4c859eSApple OSS Distributions cmpxchgl %ecx, (%rdx) 107*0f4c859eSApple OSS Distributions jne 0b 108*0f4c859eSApple OSS Distributions popq %rdi // return address in %rdi 109*0f4c859eSApple OSS Distributions movq $ SYSCALL_CONSTRUCT_UNIX(SYS_vfork), %rax // code for vfork -> rax 110*0f4c859eSApple OSS Distributions UNIX_SYSCALL_TRAP // do the system call 111*0f4c859eSApple OSS Distributions jnb L1 // jump if CF==0 112*0f4c859eSApple OSS Distributions pushq %rdi // put return address back on stack for cerror 113*0f4c859eSApple OSS Distributions movq __current_pid@GOTPCREL(%rip), %rcx 114*0f4c859eSApple OSS Distributions lock 115*0f4c859eSApple OSS Distributions addl $1, (%rcx) 116*0f4c859eSApple OSS Distributions movq %rax, %rdi 117*0f4c859eSApple OSS Distributions BRANCH_EXTERN(_cerror) 118*0f4c859eSApple OSS Distributions 119*0f4c859eSApple OSS DistributionsL1: 120*0f4c859eSApple OSS Distributions testl %edx, %edx // CF=OF=0, ZF set if zero result 121*0f4c859eSApple OSS Distributions jz L2 // parent, since r1 == 0 in parent, 1 in child 122*0f4c859eSApple OSS Distributions xorq %rax, %rax // zero rax 123*0f4c859eSApple OSS Distributions jmp *%rdi 124*0f4c859eSApple OSS Distributions 125*0f4c859eSApple OSS DistributionsL2: 126*0f4c859eSApple OSS Distributions movq __current_pid@GOTPCREL(%rip), %rdx 127*0f4c859eSApple OSS Distributions lock 128*0f4c859eSApple OSS Distributions addl $1, (%rdx) 129*0f4c859eSApple OSS Distributions jmp *%rdi 130*0f4c859eSApple OSS Distributions UNWIND_EPILOGUE 131*0f4c859eSApple OSS Distributions 132*0f4c859eSApple OSS Distributions#elif defined(__arm__) 133*0f4c859eSApple OSS Distributions 134*0f4c859eSApple OSS Distributions#include <arm/arch.h> 135*0f4c859eSApple OSS Distributions 136*0f4c859eSApple OSS Distributions .globl cerror 137*0f4c859eSApple OSS Distributions MI_ENTRY_POINT(___vfork) 138*0f4c859eSApple OSS Distributions 139*0f4c859eSApple OSS Distributions MI_GET_ADDRESS(r3, __current_pid) // get address of __current_pid 140*0f4c859eSApple OSS Distributions#ifdef _ARM_ARCH_6 141*0f4c859eSApple OSS DistributionsL0: 142*0f4c859eSApple OSS Distributions ldrex r1, [r3] 143*0f4c859eSApple OSS Distributions subs r1, r1, #1 // if __current_pid <= 0, decrement it 144*0f4c859eSApple OSS Distributions movpl r1, #-1 // otherwise put -1 in there 145*0f4c859eSApple OSS Distributions strex r2, r1, [r3] 146*0f4c859eSApple OSS Distributions cmp r2, #0 147*0f4c859eSApple OSS Distributions bne L0 148*0f4c859eSApple OSS Distributions#else 149*0f4c859eSApple OSS Distributions mov r2, #0x80000000 // load "looking" value 150*0f4c859eSApple OSS DistributionsL0: 151*0f4c859eSApple OSS Distributions swp r1, r2, [r3] // look at the value, lock others out 152*0f4c859eSApple OSS Distributions cmp r1, r2 // anyone else trying to look? 153*0f4c859eSApple OSS Distributions beq L0 // yes, so wait our turn 154*0f4c859eSApple OSS Distributions subs r1, r1, #1 // if __current_pid <= 0, decrement it 155*0f4c859eSApple OSS Distributions movpl r1, #-1 // otherwise put -1 in there 156*0f4c859eSApple OSS Distributions str r1, [r3] 157*0f4c859eSApple OSS Distributions#endif 158*0f4c859eSApple OSS Distributions 159*0f4c859eSApple OSS Distributions mov r1, #1 // prime results 160*0f4c859eSApple OSS Distributions mov r12, #SYS_vfork 161*0f4c859eSApple OSS Distributions swi #SWI_SYSCALL // make the syscall 162*0f4c859eSApple OSS Distributions bcs Lbotch // error? 163*0f4c859eSApple OSS Distributions cmp r1, #0 // parent (r1=0) or child(r1=1) 164*0f4c859eSApple OSS Distributions beq Lparent 165*0f4c859eSApple OSS Distributions 166*0f4c859eSApple OSS Distributions //child here... 167*0f4c859eSApple OSS Distributions mov r0, #0 168*0f4c859eSApple OSS Distributions bx lr // return 169*0f4c859eSApple OSS Distributions 170*0f4c859eSApple OSS DistributionsLbotch: 171*0f4c859eSApple OSS Distributions stmfd sp!, {lr} 172*0f4c859eSApple OSS Distributions MI_CALL_EXTERNAL(_cerror) // jump here on error 173*0f4c859eSApple OSS Distributions mov r0,#-1 // set the error 174*0f4c859eSApple OSS Distributions // reload values clobbered by cerror (so we can treat them as live in Lparent) 175*0f4c859eSApple OSS Distributions MI_GET_ADDRESS(r3, __current_pid) // get address of __current_pid 176*0f4c859eSApple OSS Distributions ldmfd sp!, {lr} 177*0f4c859eSApple OSS Distributions#ifndef _ARM_ARCH_6 178*0f4c859eSApple OSS Distributions mov r2, #0x80000000 // load "looking" value 179*0f4c859eSApple OSS Distributions#endif 180*0f4c859eSApple OSS Distributions // fall thru 181*0f4c859eSApple OSS Distributions 182*0f4c859eSApple OSS DistributionsLparent: 183*0f4c859eSApple OSS Distributions#ifdef _ARM_ARCH_6 184*0f4c859eSApple OSS Distributions ldrex r1, [r3] 185*0f4c859eSApple OSS Distributions add r1, r1, #1 // we're back, decrement vfork count 186*0f4c859eSApple OSS Distributions strex r2, r1, [r3] 187*0f4c859eSApple OSS Distributions cmp r2, #0 188*0f4c859eSApple OSS Distributions bne Lparent 189*0f4c859eSApple OSS Distributions#else 190*0f4c859eSApple OSS Distributions swp r1, r2, [r3] // look at the value, lock others out 191*0f4c859eSApple OSS Distributions cmp r1, r2 // anyone else trying to look? 192*0f4c859eSApple OSS Distributions beq Lparent // yes, so wait our turn 193*0f4c859eSApple OSS Distributions add r1, r1, #1 // we're back, decrement vfork count 194*0f4c859eSApple OSS Distributions str r1, [r3] 195*0f4c859eSApple OSS Distributions#endif 196*0f4c859eSApple OSS Distributions 197*0f4c859eSApple OSS Distributions bx lr // return 198*0f4c859eSApple OSS Distributions 199*0f4c859eSApple OSS Distributions#elif defined(__arm64__) 200*0f4c859eSApple OSS Distributions 201*0f4c859eSApple OSS Distributions MI_ENTRY_POINT(___vfork) 202*0f4c859eSApple OSS Distributions ARM64_STACK_PROLOG 203*0f4c859eSApple OSS Distributions 204*0f4c859eSApple OSS Distributions MI_GET_ADDRESS(x9, __current_pid) 205*0f4c859eSApple OSS DistributionsLtry_set_vfork: 206*0f4c859eSApple OSS Distributions ldxr w10, [x9] // Get old current pid value (exclusive) 207*0f4c859eSApple OSS Distributions mov w11, #-1 // Will be -1 if current value is positive 208*0f4c859eSApple OSS Distributions subs w10, w10, #1 // Subtract one 209*0f4c859eSApple OSS Distributions csel w12, w11, w10, pl // If >= 0, set to -1, else set to (current - 1) 210*0f4c859eSApple OSS Distributions stxr w13, w12, [x9] // Attempt exclusive store to current pid 211*0f4c859eSApple OSS Distributions cbnz w13, Ltry_set_vfork // If store failed, retry 212*0f4c859eSApple OSS Distributions 213*0f4c859eSApple OSS Distributions // ARM sets r1 to 1 here. I don't see why. 214*0f4c859eSApple OSS Distributions mov w16, #SYS_vfork // Set syscall code 215*0f4c859eSApple OSS Distributions svc #SWI_SYSCALL 216*0f4c859eSApple OSS Distributions b.cs Lbotch 217*0f4c859eSApple OSS Distributions cbz w1, Lparent 218*0f4c859eSApple OSS Distributions 219*0f4c859eSApple OSS Distributions // Child 220*0f4c859eSApple OSS Distributions mov w0, #0 221*0f4c859eSApple OSS Distributions ARM64_STACK_EPILOG 222*0f4c859eSApple OSS Distributions 223*0f4c859eSApple OSS Distributions // Error case 224*0f4c859eSApple OSS DistributionsLbotch: 225*0f4c859eSApple OSS Distributions PUSH_FRAME 226*0f4c859eSApple OSS Distributions bl _cerror // Update errno 227*0f4c859eSApple OSS Distributions mov w0, #-1 // Set return value 228*0f4c859eSApple OSS Distributions MI_GET_ADDRESS(x9, __current_pid) // Reload current pid address 229*0f4c859eSApple OSS Distributions POP_FRAME 230*0f4c859eSApple OSS Distributions // Fall through 231*0f4c859eSApple OSS DistributionsLparent: 232*0f4c859eSApple OSS Distributions ldxr w10, [x9] // Exclusive load current pid value 233*0f4c859eSApple OSS Distributions add w10, w10, #1 // Increment (i.e. decrement vfork count) 234*0f4c859eSApple OSS Distributions stxr w11, w10, [x9] // Attempt exclusive store of updated vfork count 235*0f4c859eSApple OSS Distributions cbnz w11, Lparent // If exclusive store failed, retry 236*0f4c859eSApple OSS Distributions ARM64_STACK_EPILOG // Done, return 237*0f4c859eSApple OSS Distributions 238*0f4c859eSApple OSS Distributions#else 239*0f4c859eSApple OSS Distributions#error Unsupported architecture 240*0f4c859eSApple OSS Distributions#endif 241