1*c54f35caSApple OSS Distributions/* 2*c54f35caSApple OSS Distributions * Copyright (c) 1999-2007 Apple Inc. All rights reserved. 3*c54f35caSApple OSS Distributions * 4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*c54f35caSApple OSS Distributions * 6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*c54f35caSApple OSS Distributions * 15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at 16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*c54f35caSApple OSS Distributions * 18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are 19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and 24*c54f35caSApple OSS Distributions * limitations under the License. 25*c54f35caSApple OSS Distributions * 26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*c54f35caSApple OSS Distributions */ 28*c54f35caSApple OSS Distributions 29*c54f35caSApple OSS Distributions#include "SYS.h" 30*c54f35caSApple OSS Distributions 31*c54f35caSApple OSS Distributions#if defined(__i386__) 32*c54f35caSApple OSS Distributions 33*c54f35caSApple OSS Distributions .data 34*c54f35caSApple OSS Distributions .private_extern __current_pid 35*c54f35caSApple OSS DistributionsL__current_pid_addr: 36*c54f35caSApple OSS Distributions __current_pid: 37*c54f35caSApple OSS Distributions .long 0 38*c54f35caSApple OSS Distributions 39*c54f35caSApple OSS Distributions#if defined(__DYNAMIC__) 40*c54f35caSApple OSS Distributions#define GET_CURRENT_PID \ 41*c54f35caSApple OSS Distributions call 0f ; \ 42*c54f35caSApple OSS Distributions0: ; \ 43*c54f35caSApple OSS Distributions popl %ecx ; \ 44*c54f35caSApple OSS Distributions leal L__current_pid_addr-0b(%ecx), %ecx 45*c54f35caSApple OSS Distributions 46*c54f35caSApple OSS Distributions#define __current_pid (%ecx) 47*c54f35caSApple OSS Distributions 48*c54f35caSApple OSS Distributions#else 49*c54f35caSApple OSS Distributions#define GET_CURRENT_PID 50*c54f35caSApple OSS Distributions#endif 51*c54f35caSApple OSS Distributions 52*c54f35caSApple OSS Distributions/* 53*c54f35caSApple OSS Distributions * If __current_pid is > 0, return it, else make syscall. 54*c54f35caSApple OSS Distributions * If __current_pid is 0, cache result of syscall. 55*c54f35caSApple OSS Distributions */ 56*c54f35caSApple OSS DistributionsTEXT 57*c54f35caSApple OSS DistributionsLEAF(___getpid, 0) 58*c54f35caSApple OSS Distributions GET_CURRENT_PID 59*c54f35caSApple OSS Distributions movl __current_pid, %eax 60*c54f35caSApple OSS Distributions testl %eax, %eax 61*c54f35caSApple OSS Distributions jle 1f 62*c54f35caSApple OSS Distributions ret 63*c54f35caSApple OSS Distributions1: 64*c54f35caSApple OSS Distributions UNIX_SYSCALL_NONAME(getpid, 0, cerror_nocancel) 65*c54f35caSApple OSS Distributions movl %eax, %edx 66*c54f35caSApple OSS Distributions xorl %eax, %eax 67*c54f35caSApple OSS Distributions GET_CURRENT_PID 68*c54f35caSApple OSS Distributions lock 69*c54f35caSApple OSS Distributions cmpxchgl %edx, __current_pid 70*c54f35caSApple OSS Distributions movl %edx, %eax 71*c54f35caSApple OSS Distributions ret 72*c54f35caSApple OSS Distributions 73*c54f35caSApple OSS Distributions#elif defined(__x86_64__) 74*c54f35caSApple OSS Distributions 75*c54f35caSApple OSS Distributions .data 76*c54f35caSApple OSS Distributions .private_extern __current_pid 77*c54f35caSApple OSS Distributions__current_pid: 78*c54f35caSApple OSS Distributions .long 0 79*c54f35caSApple OSS Distributions 80*c54f35caSApple OSS Distributions/* 81*c54f35caSApple OSS Distributions * If __current_pid is > 0, return it, else make syscall. 82*c54f35caSApple OSS Distributions * If __current_pid is 0, cache result of syscall. 83*c54f35caSApple OSS Distributions */ 84*c54f35caSApple OSS DistributionsTEXT 85*c54f35caSApple OSS DistributionsLEAF(___getpid, 0) 86*c54f35caSApple OSS Distributions movl __current_pid(%rip), %eax 87*c54f35caSApple OSS Distributions testl %eax, %eax 88*c54f35caSApple OSS Distributions jle 1f 89*c54f35caSApple OSS Distributions ret 90*c54f35caSApple OSS Distributions1: 91*c54f35caSApple OSS Distributions UNIX_SYSCALL_NONAME(getpid, 0, cerror_nocancel) 92*c54f35caSApple OSS Distributions movl %eax, %edx 93*c54f35caSApple OSS Distributions xorl %eax, %eax 94*c54f35caSApple OSS Distributions leaq __current_pid(%rip), %rcx 95*c54f35caSApple OSS Distributions lock 96*c54f35caSApple OSS Distributions cmpxchgl %edx, (%rcx) 97*c54f35caSApple OSS Distributions movl %edx, %eax 98*c54f35caSApple OSS Distributions ret 99*c54f35caSApple OSS Distributions UNWIND_EPILOGUE 100*c54f35caSApple OSS Distributions 101*c54f35caSApple OSS Distributions#elif defined(__arm__) 102*c54f35caSApple OSS Distributions 103*c54f35caSApple OSS Distributions#include <arm/arch.h> 104*c54f35caSApple OSS Distributions 105*c54f35caSApple OSS Distributions .data 106*c54f35caSApple OSS Distributions .globl __current_pid 107*c54f35caSApple OSS Distributions .align 2 108*c54f35caSApple OSS Distributions__current_pid: 109*c54f35caSApple OSS Distributions /* Cached pid. Possible values: 110*c54f35caSApple OSS Distributions * 0: no value cached 111*c54f35caSApple OSS Distributions * > 0: cached PID of current process 112*c54f35caSApple OSS Distributions * < 0: negative number of vforks in progress 113*c54f35caSApple OSS Distributions * INT_MIN: for pre-ARMv6, "looking" value (0x80000000) 114*c54f35caSApple OSS Distributions */ 115*c54f35caSApple OSS Distributions .long 0 116*c54f35caSApple OSS Distributions 117*c54f35caSApple OSS DistributionsMI_ENTRY_POINT(___getpid) 118*c54f35caSApple OSS Distributions ldr r3, L__current_pid 119*c54f35caSApple OSS DistributionsL1: add r3, pc, r3 // r3 = &__current_pid 120*c54f35caSApple OSS Distributions ldr r0, [r3] // get the cached pid 121*c54f35caSApple OSS Distributions cmp r0, #0 122*c54f35caSApple OSS Distributions bxgt lr // if positive, return it 123*c54f35caSApple OSS Distributions 124*c54f35caSApple OSS Distributions SYSCALL_NONAME(getpid, 0, cerror_nocancel) 125*c54f35caSApple OSS Distributions 126*c54f35caSApple OSS Distributions#ifdef _ARM_ARCH_6 127*c54f35caSApple OSS Distributions ldrex r2, [r3] // see if we can cache it 128*c54f35caSApple OSS Distributions cmp r2, #0 // we can't if there are any... 129*c54f35caSApple OSS Distributions bxlt lr // ...vforks in progress 130*c54f35caSApple OSS Distributions strex r2, r0, [r3] // ignore conflicts 131*c54f35caSApple OSS Distributions#else 132*c54f35caSApple OSS Distributions mov r1, #0x80000000 // load "looking" value 133*c54f35caSApple OSS Distributions swp r2, r1, [r3] // look at the value, lock others out 134*c54f35caSApple OSS Distributions cmp r2, r1 // anyone else trying to look? 135*c54f35caSApple OSS Distributions bxeq lr // yes, so return immediately/ 136*c54f35caSApple OSS Distributions cmp r2, #0 // see if we can cache it 137*c54f35caSApple OSS Distributions streq r0, [r3] // if zero, we can 138*c54f35caSApple OSS Distributions strne r2, [r3] // otherwise restore previous value 139*c54f35caSApple OSS Distributions#endif 140*c54f35caSApple OSS Distributions 141*c54f35caSApple OSS Distributions bx lr 142*c54f35caSApple OSS Distributions 143*c54f35caSApple OSS DistributionsL__current_pid: 144*c54f35caSApple OSS Distributions .long __current_pid - (L1+8) 145*c54f35caSApple OSS Distributions 146*c54f35caSApple OSS Distributions#elif defined(__arm64__) 147*c54f35caSApple OSS Distributions .data 148*c54f35caSApple OSS Distributions .globl __current_pid 149*c54f35caSApple OSS Distributions .align 2 150*c54f35caSApple OSS Distributions__current_pid: 151*c54f35caSApple OSS Distributions /* cached pid. possible values: 152*c54f35caSApple OSS Distributions * 0: no value cached 153*c54f35caSApple OSS Distributions * > 0: cached pid of current process 154*c54f35caSApple OSS Distributions * < 0: negative number of vforks in progress 155*c54f35caSApple OSS Distributions * int_min: for pre-armv6, "looking" value (0x80000000) 156*c54f35caSApple OSS Distributions */ 157*c54f35caSApple OSS Distributions .long 0 158*c54f35caSApple OSS Distributions 159*c54f35caSApple OSS DistributionsMI_ENTRY_POINT(___getpid) 160*c54f35caSApple OSS Distributions MI_GET_ADDRESS(x9, __current_pid) // Get address of cached value 161*c54f35caSApple OSS Distributions ldr w0, [x9] // Load it 162*c54f35caSApple OSS Distributions cmp w0, #0 // See if there's a cached value 163*c54f35caSApple OSS Distributions b.le L_notcached // If not, make syscall 164*c54f35caSApple OSS Distributions ret // Else, we're done 165*c54f35caSApple OSS DistributionsL_notcached: 166*c54f35caSApple OSS Distributions SYSCALL_NONAME(getpid, 0, cerror_nocancel) 167*c54f35caSApple OSS Distributions ldxr w10, [x9] // Exclusive load 168*c54f35caSApple OSS Distributions cbnz w10, L_done // Unless unset, don't even try 169*c54f35caSApple OSS Distributions stxr wzr, w0, [x9] // Try to store, but don't care if we fail (someone will win, or not) 170*c54f35caSApple OSS DistributionsL_done: 171*c54f35caSApple OSS Distributions ret // Done 172*c54f35caSApple OSS Distributions#else 173*c54f35caSApple OSS Distributions#error Unsupported architecture 174*c54f35caSApple OSS Distributions#endif 175