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