1*0f4c859eSApple OSS Distributions/* 2*0f4c859eSApple OSS Distributions * Copyright (c) 1999-2010 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) 1992 NeXT Computer, Inc. All rights reserved. 29*0f4c859eSApple OSS Distributions * 30*0f4c859eSApple OSS Distributions * File: libc/ppc/sys/fork.s 31*0f4c859eSApple OSS Distributions * 32*0f4c859eSApple OSS Distributions * HISTORY 33*0f4c859eSApple OSS Distributions * 18-Nov-92 Ben Fathi ([email protected]) 34*0f4c859eSApple OSS Distributions * Created from M88K sources 35*0f4c859eSApple OSS Distributions * 36*0f4c859eSApple OSS Distributions * 11-Jan-92 Peter King ([email protected]) 37*0f4c859eSApple OSS Distributions * Created from M68K sources 38*0f4c859eSApple OSS Distributions */ 39*0f4c859eSApple OSS Distributions 40*0f4c859eSApple OSS Distributions/* 41*0f4c859eSApple OSS Distributions * All of the asm stubs in this file have been adjusted so the pre/post 42*0f4c859eSApple OSS Distributions * fork handlers and dyld fixup are done in C inside Libc. As such, Libc 43*0f4c859eSApple OSS Distributions * expects the __fork asm to fix up the return code to be -1, 0 or pid 44*0f4c859eSApple OSS Distributions * and errno if needed. 45*0f4c859eSApple OSS Distributions */ 46*0f4c859eSApple OSS Distributions 47*0f4c859eSApple OSS Distributions#include "SYS.h" 48*0f4c859eSApple OSS Distributions 49*0f4c859eSApple OSS Distributions#if defined(__i386__) 50*0f4c859eSApple OSS Distributions 51*0f4c859eSApple OSS DistributionsLEAF(___fork, 0) 52*0f4c859eSApple OSS Distributions subl $28, %esp // Align the stack, with 16 bytes of extra padding that we'll need 53*0f4c859eSApple OSS Distributions 54*0f4c859eSApple OSS Distributions movl $ SYS_fork,%eax; // code for fork -> eax 55*0f4c859eSApple OSS Distributions UNIX_SYSCALL_TRAP // do the system call 56*0f4c859eSApple OSS Distributions jnc L1 // jump if CF==0 57*0f4c859eSApple OSS Distributions 58*0f4c859eSApple OSS Distributions CALL_EXTERN(tramp_cerror) 59*0f4c859eSApple OSS Distributions movl $-1,%eax 60*0f4c859eSApple OSS Distributions addl $28, %esp // restore the stack 61*0f4c859eSApple OSS Distributions ret 62*0f4c859eSApple OSS Distributions 63*0f4c859eSApple OSS DistributionsL1: 64*0f4c859eSApple OSS Distributions orl %edx,%edx // CF=OF=0, ZF set if zero result 65*0f4c859eSApple OSS Distributions jz L2 // parent, since r1 == 0 in parent, 1 in child 66*0f4c859eSApple OSS Distributions 67*0f4c859eSApple OSS Distributions //child here... 68*0f4c859eSApple OSS Distributions xorl %eax,%eax // zero eax 69*0f4c859eSApple OSS Distributions REG_TO_EXTERN(%eax, __current_pid); 70*0f4c859eSApple OSS DistributionsL2: 71*0f4c859eSApple OSS Distributions addl $28, %esp // restore the stack 72*0f4c859eSApple OSS Distributions // parent ends up here skipping child portion 73*0f4c859eSApple OSS Distributions ret 74*0f4c859eSApple OSS Distributions 75*0f4c859eSApple OSS Distributions#elif defined(__x86_64__) 76*0f4c859eSApple OSS Distributions 77*0f4c859eSApple OSS DistributionsLEAF(___fork, 0) 78*0f4c859eSApple OSS Distributions subq $24, %rsp // Align the stack, plus room for local storage 79*0f4c859eSApple OSS Distributions 80*0f4c859eSApple OSS Distributions movl $ SYSCALL_CONSTRUCT_UNIX(SYS_fork),%eax; // code for fork -> rax 81*0f4c859eSApple OSS Distributions UNIX_SYSCALL_TRAP // do the system call 82*0f4c859eSApple OSS Distributions jnc L1 // jump if CF==0 83*0f4c859eSApple OSS Distributions 84*0f4c859eSApple OSS Distributions movq %rax, %rdi 85*0f4c859eSApple OSS Distributions CALL_EXTERN(_cerror) 86*0f4c859eSApple OSS Distributions movq $-1, %rax 87*0f4c859eSApple OSS Distributions addq $24, %rsp // restore the stack 88*0f4c859eSApple OSS Distributions ret 89*0f4c859eSApple OSS Distributions 90*0f4c859eSApple OSS DistributionsL1: 91*0f4c859eSApple OSS Distributions orl %edx,%edx // CF=OF=0, ZF set if zero result 92*0f4c859eSApple OSS Distributions jz L2 // parent, since r1 == 0 in parent, 1 in child 93*0f4c859eSApple OSS Distributions 94*0f4c859eSApple OSS Distributions //child here... 95*0f4c859eSApple OSS Distributions xorq %rax, %rax 96*0f4c859eSApple OSS Distributions PICIFY(__current_pid) 97*0f4c859eSApple OSS Distributions movl %eax,(%r11) 98*0f4c859eSApple OSS DistributionsL2: 99*0f4c859eSApple OSS Distributions // parent ends up here skipping child portion 100*0f4c859eSApple OSS Distributions addq $24, %rsp // restore the stack 101*0f4c859eSApple OSS Distributions ret 102*0f4c859eSApple OSS Distributions UNWIND_EPILOGUE 103*0f4c859eSApple OSS Distributions 104*0f4c859eSApple OSS Distributions#elif defined(__arm__) 105*0f4c859eSApple OSS Distributions 106*0f4c859eSApple OSS DistributionsMI_ENTRY_POINT(___fork) 107*0f4c859eSApple OSS Distributions stmfd sp!, {r4, r7, lr} 108*0f4c859eSApple OSS Distributions add r7, sp, #4 109*0f4c859eSApple OSS Distributions 110*0f4c859eSApple OSS Distributions mov r1, #1 // prime results 111*0f4c859eSApple OSS Distributions mov r12, #SYS_fork 112*0f4c859eSApple OSS Distributions swi #SWI_SYSCALL // make the syscall 113*0f4c859eSApple OSS Distributions bcs Lbotch // error? 114*0f4c859eSApple OSS Distributions 115*0f4c859eSApple OSS Distributions cmp r1, #0 // parent (r1=0) or child(r1=1) 116*0f4c859eSApple OSS Distributions beq Lparent 117*0f4c859eSApple OSS Distributions 118*0f4c859eSApple OSS Distributions //child here... 119*0f4c859eSApple OSS Distributions MI_GET_ADDRESS(r3, __current_pid) 120*0f4c859eSApple OSS Distributions mov r0, #0 121*0f4c859eSApple OSS Distributions str r0, [r3] // clear cached pid in child 122*0f4c859eSApple OSS Distributions ldmfd sp!, {r4, r7, pc} 123*0f4c859eSApple OSS Distributions 124*0f4c859eSApple OSS DistributionsLbotch: 125*0f4c859eSApple OSS Distributions MI_CALL_EXTERNAL(_cerror) // jump here on error 126*0f4c859eSApple OSS Distributions mov r0,#-1 // set the error 127*0f4c859eSApple OSS Distributions // fall thru 128*0f4c859eSApple OSS DistributionsLparent: 129*0f4c859eSApple OSS Distributions ldmfd sp!, {r4, r7, pc} // pop and return 130*0f4c859eSApple OSS Distributions 131*0f4c859eSApple OSS Distributions#elif defined(__arm64__) 132*0f4c859eSApple OSS Distributions 133*0f4c859eSApple OSS Distributions#include <mach/arm64/asm.h> 134*0f4c859eSApple OSS Distributions 135*0f4c859eSApple OSS DistributionsMI_ENTRY_POINT(___fork) 136*0f4c859eSApple OSS Distributions ARM64_STACK_PROLOG 137*0f4c859eSApple OSS Distributions PUSH_FRAME 138*0f4c859eSApple OSS Distributions // ARM moves a 1 in to r1 here, but I can't see why. 139*0f4c859eSApple OSS Distributions mov x16, #SYS_fork // Syscall code 140*0f4c859eSApple OSS Distributions svc #SWI_SYSCALL // Trap to kernel 141*0f4c859eSApple OSS Distributions b.cs Lbotch // Carry bit indicates failure 142*0f4c859eSApple OSS Distributions cbz x1, Lparent // x1 == 0 indicates that we are the parent 143*0f4c859eSApple OSS Distributions 144*0f4c859eSApple OSS Distributions // Child 145*0f4c859eSApple OSS Distributions MI_GET_ADDRESS(x9, __current_pid) // Get address of cached "current pid" 146*0f4c859eSApple OSS Distributions mov w0, #0 147*0f4c859eSApple OSS Distributions str w0, [x9] // Clear cached current pid 148*0f4c859eSApple OSS Distributions POP_FRAME // And done 149*0f4c859eSApple OSS Distributions ARM64_STACK_EPILOG 150*0f4c859eSApple OSS Distributions 151*0f4c859eSApple OSS DistributionsLbotch: 152*0f4c859eSApple OSS Distributions MI_CALL_EXTERNAL(_cerror) // Handle error 153*0f4c859eSApple OSS Distributions mov w0, #-1 // Return value is -1 154*0f4c859eSApple OSS DistributionsLparent: 155*0f4c859eSApple OSS Distributions POP_FRAME // Return 156*0f4c859eSApple OSS Distributions ARM64_STACK_EPILOG 157*0f4c859eSApple OSS Distributions 158*0f4c859eSApple OSS Distributions#else 159*0f4c859eSApple OSS Distributions#error Unsupported architecture 160*0f4c859eSApple OSS Distributions#endif 161