xref: /xnu-10063.141.1/libsyscall/custom/SYS.h (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions /*
2*d8b80295SApple OSS Distributions  * Copyright (c) 1999-2011 Apple Inc. All rights reserved.
3*d8b80295SApple OSS Distributions  *
4*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*d8b80295SApple OSS Distributions  *
6*d8b80295SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*d8b80295SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*d8b80295SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*d8b80295SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*d8b80295SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*d8b80295SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*d8b80295SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*d8b80295SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*d8b80295SApple OSS Distributions  *
15*d8b80295SApple OSS Distributions  * Please obtain a copy of the License at
16*d8b80295SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*d8b80295SApple OSS Distributions  *
18*d8b80295SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*d8b80295SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*d8b80295SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*d8b80295SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*d8b80295SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*d8b80295SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*d8b80295SApple OSS Distributions  * limitations under the License.
25*d8b80295SApple OSS Distributions  *
26*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*d8b80295SApple OSS Distributions  */
28*d8b80295SApple OSS Distributions /* Copyright (c) 1992 NeXT Computer, Inc.  All rights reserved.
29*d8b80295SApple OSS Distributions  *
30*d8b80295SApple OSS Distributions  *	File:	SYS.h
31*d8b80295SApple OSS Distributions  *
32*d8b80295SApple OSS Distributions  *	Definition of the user side of the UNIX system call interface
33*d8b80295SApple OSS Distributions  *	for M98K.
34*d8b80295SApple OSS Distributions  *
35*d8b80295SApple OSS Distributions  *	Errors are flagged by the location of the trap return (ie., which
36*d8b80295SApple OSS Distributions  *	instruction is executed upon rfi):
37*d8b80295SApple OSS Distributions  *
38*d8b80295SApple OSS Distributions  *		SC PC + 4:	Error (typically branch to cerror())
39*d8b80295SApple OSS Distributions  *		SC PC + 8:	Success
40*d8b80295SApple OSS Distributions  *
41*d8b80295SApple OSS Distributions  * HISTORY
42*d8b80295SApple OSS Distributions  * 18-Nov-92	Ben Fathi ([email protected])
43*d8b80295SApple OSS Distributions  *	Ported to m98k.
44*d8b80295SApple OSS Distributions  *
45*d8b80295SApple OSS Distributions  *  9-Jan-92	Peter King ([email protected])
46*d8b80295SApple OSS Distributions  *	Created.
47*d8b80295SApple OSS Distributions  */
48*d8b80295SApple OSS Distributions 
49*d8b80295SApple OSS Distributions #include <sys/syscall.h>
50*d8b80295SApple OSS Distributions 
51*d8b80295SApple OSS Distributions #if defined(__i386__)
52*d8b80295SApple OSS Distributions 
53*d8b80295SApple OSS Distributions #include <architecture/i386/asm_help.h>
54*d8b80295SApple OSS Distributions #include <mach/i386/syscall_sw.h>
55*d8b80295SApple OSS Distributions 
56*d8b80295SApple OSS Distributions /*
57*d8b80295SApple OSS Distributions  * We have two entry points. int's is used for syscalls which need to preserve
58*d8b80295SApple OSS Distributions  * %ecx across the call, or return a 64-bit value in %eax:%edx. sysenter is used
59*d8b80295SApple OSS Distributions  * for the majority of syscalls which just return a value in %eax.
60*d8b80295SApple OSS Distributions  */
61*d8b80295SApple OSS Distributions 
62*d8b80295SApple OSS Distributions #define UNIX_SYSCALL_SYSENTER		call __sysenter_trap
63*d8b80295SApple OSS Distributions #define UNIX_SYSCALL(name, nargs)			\
64*d8b80295SApple OSS Distributions 	.globl	tramp_cerror				;\
65*d8b80295SApple OSS Distributions LEAF(_##name, 0)					;\
66*d8b80295SApple OSS Distributions 	movl	$ SYS_##name, %eax			;\
67*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_SYSENTER				;\
68*d8b80295SApple OSS Distributions 	jnb	2f					;\
69*d8b80295SApple OSS Distributions 	BRANCH_EXTERN(tramp_cerror)  			;\
70*d8b80295SApple OSS Distributions 2:
71*d8b80295SApple OSS Distributions 
72*d8b80295SApple OSS Distributions #define UNIX_SYSCALL_INT(name, nargs)			\
73*d8b80295SApple OSS Distributions 	.globl	tramp_cerror				;\
74*d8b80295SApple OSS Distributions LEAF(_##name, 0)					;\
75*d8b80295SApple OSS Distributions 	movl	$ SYS_##name, %eax			;\
76*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_TRAP				;\
77*d8b80295SApple OSS Distributions 	jnb	2f					;\
78*d8b80295SApple OSS Distributions 	BRANCH_EXTERN(tramp_cerror)  			;\
79*d8b80295SApple OSS Distributions 2:
80*d8b80295SApple OSS Distributions 
81*d8b80295SApple OSS Distributions #if defined(__SYSCALL_32BIT_ARG_BYTES) && ((__SYSCALL_32BIT_ARG_BYTES >= 4) && (__SYSCALL_32BIT_ARG_BYTES <= 20))
82*d8b80295SApple OSS Distributions #define UNIX_SYSCALL_NONAME(name, nargs, cerror)			\
83*d8b80295SApple OSS Distributions 	movl	$(SYS_##name | (__SYSCALL_32BIT_ARG_BYTES << I386_SYSCALL_ARG_BYTES_SHIFT)), %eax		;\
84*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_SYSENTER					;\
85*d8b80295SApple OSS Distributions 	jnb	2f						;\
86*d8b80295SApple OSS Distributions 	BRANCH_EXTERN(tramp_##cerror)				;\
87*d8b80295SApple OSS Distributions 2:
88*d8b80295SApple OSS Distributions #else /* __SYSCALL_32BIT_ARG_BYTES < 4 || > 20 */
89*d8b80295SApple OSS Distributions #define UNIX_SYSCALL_NONAME(name, nargs, cerror)	\
90*d8b80295SApple OSS Distributions 	movl	$ SYS_##name, %eax			;\
91*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_SYSENTER				;\
92*d8b80295SApple OSS Distributions 	jnb	2f					;\
93*d8b80295SApple OSS Distributions 	BRANCH_EXTERN(tramp_##cerror)			;\
94*d8b80295SApple OSS Distributions 2:
95*d8b80295SApple OSS Distributions #endif
96*d8b80295SApple OSS Distributions 
97*d8b80295SApple OSS Distributions #define UNIX_SYSCALL_INT_NONAME(name, nargs)		\
98*d8b80295SApple OSS Distributions 	.globl	tramp_cerror_nocancel			;\
99*d8b80295SApple OSS Distributions 	movl	$ SYS_##name, %eax			;\
100*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_TRAP				;\
101*d8b80295SApple OSS Distributions 	jnb	2f					;\
102*d8b80295SApple OSS Distributions 	BRANCH_EXTERN(tramp_cerror_nocancel) 		;\
103*d8b80295SApple OSS Distributions 2:
104*d8b80295SApple OSS Distributions 
105*d8b80295SApple OSS Distributions #define PSEUDO(pseudo, name, nargs, cerror)	\
106*d8b80295SApple OSS Distributions LEAF(pseudo, 0)					;\
107*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_NONAME(name, nargs, cerror)
108*d8b80295SApple OSS Distributions 
109*d8b80295SApple OSS Distributions #define PSEUDO_INT(pseudo, name, nargs)		\
110*d8b80295SApple OSS Distributions LEAF(pseudo, 0)					;\
111*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_INT_NONAME(name, nargs)
112*d8b80295SApple OSS Distributions 
113*d8b80295SApple OSS Distributions #define __SYSCALL2(pseudo, name, nargs, cerror)			\
114*d8b80295SApple OSS Distributions 	PSEUDO(pseudo, name, nargs, cerror)			;\
115*d8b80295SApple OSS Distributions 	ret
116*d8b80295SApple OSS Distributions 
117*d8b80295SApple OSS Distributions #define __SYSCALL(pseudo, name, nargs)			\
118*d8b80295SApple OSS Distributions 	PSEUDO(pseudo, name, nargs, cerror)		;\
119*d8b80295SApple OSS Distributions 	ret
120*d8b80295SApple OSS Distributions 
121*d8b80295SApple OSS Distributions #define __SYSCALL_INT(pseudo, name, nargs)		\
122*d8b80295SApple OSS Distributions 	PSEUDO_INT(pseudo, name, nargs)			;\
123*d8b80295SApple OSS Distributions 	ret
124*d8b80295SApple OSS Distributions 
125*d8b80295SApple OSS Distributions #elif defined(__x86_64__)
126*d8b80295SApple OSS Distributions 
127*d8b80295SApple OSS Distributions #include <architecture/i386/asm_help.h>
128*d8b80295SApple OSS Distributions #include <mach/i386/syscall_sw.h>
129*d8b80295SApple OSS Distributions 
130*d8b80295SApple OSS Distributions #define UNIX_SYSCALL_SYSCALL	\
131*d8b80295SApple OSS Distributions 	movq	%rcx, %r10		;\
132*d8b80295SApple OSS Distributions 	syscall
133*d8b80295SApple OSS Distributions 
134*d8b80295SApple OSS Distributions #define UNIX_SYSCALL(name, nargs)						 \
135*d8b80295SApple OSS Distributions 	.globl	cerror								;\
136*d8b80295SApple OSS Distributions LEAF(_##name, 0)								;\
137*d8b80295SApple OSS Distributions 	movl	$ SYSCALL_CONSTRUCT_UNIX(SYS_##name), %eax			;\
138*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_SYSCALL							;\
139*d8b80295SApple OSS Distributions 	jnb		2f							;\
140*d8b80295SApple OSS Distributions 	movq	%rax, %rdi							;\
141*d8b80295SApple OSS Distributions 	BRANCH_EXTERN(_cerror)							;\
142*d8b80295SApple OSS Distributions 2:
143*d8b80295SApple OSS Distributions 
144*d8b80295SApple OSS Distributions #define UNIX_SYSCALL_NONAME(name, nargs, cerror)		 \
145*d8b80295SApple OSS Distributions 	.globl	cerror								;\
146*d8b80295SApple OSS Distributions 	movl	$ SYSCALL_CONSTRUCT_UNIX(SYS_##name), %eax			;\
147*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_SYSCALL							;\
148*d8b80295SApple OSS Distributions 	jnb		2f							;\
149*d8b80295SApple OSS Distributions 	movq	%rax, %rdi							;\
150*d8b80295SApple OSS Distributions 	BRANCH_EXTERN(_##cerror)						;\
151*d8b80295SApple OSS Distributions 2:
152*d8b80295SApple OSS Distributions 
153*d8b80295SApple OSS Distributions #define PSEUDO(pseudo, name, nargs, cerror)			\
154*d8b80295SApple OSS Distributions LEAF(pseudo, 0)					;\
155*d8b80295SApple OSS Distributions 	UNIX_SYSCALL_NONAME(name, nargs, cerror)
156*d8b80295SApple OSS Distributions 
157*d8b80295SApple OSS Distributions #define __SYSCALL2(pseudo, name, nargs, cerror) \
158*d8b80295SApple OSS Distributions 	PSEUDO(pseudo, name, nargs, cerror)			;\
159*d8b80295SApple OSS Distributions 	ret											;\
160*d8b80295SApple OSS Distributions 	UNWIND_EPILOGUE
161*d8b80295SApple OSS Distributions 
162*d8b80295SApple OSS Distributions #define __SYSCALL(pseudo, name, nargs)			\
163*d8b80295SApple OSS Distributions 	PSEUDO(pseudo, name, nargs, cerror)			;\
164*d8b80295SApple OSS Distributions 	ret											;\
165*d8b80295SApple OSS Distributions 	UNWIND_EPILOGUE
166*d8b80295SApple OSS Distributions 
167*d8b80295SApple OSS Distributions #elif defined(__arm__)
168*d8b80295SApple OSS Distributions 
169*d8b80295SApple OSS Distributions #include <architecture/arm/asm_help.h>
170*d8b80295SApple OSS Distributions #include <mach/arm/syscall_sw.h>
171*d8b80295SApple OSS Distributions 
172*d8b80295SApple OSS Distributions /*
173*d8b80295SApple OSS Distributions  * ARM system call interface:
174*d8b80295SApple OSS Distributions  *
175*d8b80295SApple OSS Distributions  * swi 0x80
176*d8b80295SApple OSS Distributions  * args: r0-r6
177*d8b80295SApple OSS Distributions  * return code: r0
178*d8b80295SApple OSS Distributions  * on error, carry bit is set in the psr, otherwise carry bit is cleared.
179*d8b80295SApple OSS Distributions  */
180*d8b80295SApple OSS Distributions 
181*d8b80295SApple OSS Distributions /*
182*d8b80295SApple OSS Distributions  * Macros.
183*d8b80295SApple OSS Distributions  */
184*d8b80295SApple OSS Distributions 
185*d8b80295SApple OSS Distributions /*
186*d8b80295SApple OSS Distributions  * until we update the architecture project, these live here
187*d8b80295SApple OSS Distributions  */
188*d8b80295SApple OSS Distributions 
189*d8b80295SApple OSS Distributions #if defined(__DYNAMIC__)
190*d8b80295SApple OSS Distributions #define MI_GET_ADDRESS(reg,var)  \
191*d8b80295SApple OSS Distributions 	ldr	reg, 4f					;\
192*d8b80295SApple OSS Distributions 3:	ldr	reg, [pc, reg]				;\
193*d8b80295SApple OSS Distributions 	b	5f					;\
194*d8b80295SApple OSS Distributions 4:	.long	6f - (3b + 8)				;\
195*d8b80295SApple OSS Distributions 5:							;\
196*d8b80295SApple OSS Distributions 	.non_lazy_symbol_pointer			;\
197*d8b80295SApple OSS Distributions 6:							;\
198*d8b80295SApple OSS Distributions 	.indirect_symbol var				;\
199*d8b80295SApple OSS Distributions 	.long 0						;\
200*d8b80295SApple OSS Distributions 	.text						;\
201*d8b80295SApple OSS Distributions 	.align 2
202*d8b80295SApple OSS Distributions #else
203*d8b80295SApple OSS Distributions #define MI_GET_ADDRESS(reg,var)  \
204*d8b80295SApple OSS Distributions 	ldr	reg, 3f	;\
205*d8b80295SApple OSS Distributions 	b	4f	;\
206*d8b80295SApple OSS Distributions 3:	.long var	;\
207*d8b80295SApple OSS Distributions 4:
208*d8b80295SApple OSS Distributions #endif
209*d8b80295SApple OSS Distributions 
210*d8b80295SApple OSS Distributions #if defined(__DYNAMIC__)
211*d8b80295SApple OSS Distributions #define MI_BRANCH_EXTERNAL(var)				\
212*d8b80295SApple OSS Distributions 	.globl	var								;\
213*d8b80295SApple OSS Distributions 	MI_GET_ADDRESS(ip, var)				;\
214*d8b80295SApple OSS Distributions  	bx	ip
215*d8b80295SApple OSS Distributions #else
216*d8b80295SApple OSS Distributions #define MI_BRANCH_EXTERNAL(var)				;\
217*d8b80295SApple OSS Distributions 	.globl	var								;\
218*d8b80295SApple OSS Distributions  	b	var
219*d8b80295SApple OSS Distributions #endif
220*d8b80295SApple OSS Distributions 
221*d8b80295SApple OSS Distributions #if defined(__DYNAMIC__)
222*d8b80295SApple OSS Distributions #define MI_CALL_EXTERNAL(var)    \
223*d8b80295SApple OSS Distributions 	.globl	var				;\
224*d8b80295SApple OSS Distributions 	MI_GET_ADDRESS(ip,var)	;\
225*d8b80295SApple OSS Distributions 	blx	ip
226*d8b80295SApple OSS Distributions #else
227*d8b80295SApple OSS Distributions #define MI_CALL_EXTERNAL(var)				\
228*d8b80295SApple OSS Distributions 	.globl	var								;\
229*d8b80295SApple OSS Distributions  	bl	var
230*d8b80295SApple OSS Distributions #endif
231*d8b80295SApple OSS Distributions 
232*d8b80295SApple OSS Distributions #define MI_ENTRY_POINT(name)				\
233*d8b80295SApple OSS Distributions 	.text									;\
234*d8b80295SApple OSS Distributions 	.align 2	;\
235*d8b80295SApple OSS Distributions 	.globl  name							;\
236*d8b80295SApple OSS Distributions name:
237*d8b80295SApple OSS Distributions 
238*d8b80295SApple OSS Distributions /* load the syscall number into r12 and trap */
239*d8b80295SApple OSS Distributions #define DO_SYSCALL(num)		\
240*d8b80295SApple OSS Distributions 	.if (((num) & 0xff) == (num)) 	       				;\
241*d8b80295SApple OSS Distributions 	mov		r12, #(num)		       			;\
242*d8b80295SApple OSS Distributions 	.elseif (((num) & 0x3fc) == (num))				;\
243*d8b80295SApple OSS Distributions 	mov		r12, #(num)					;\
244*d8b80295SApple OSS Distributions 	.else								;\
245*d8b80295SApple OSS Distributions 	mov		r12, #((num) & 0xffffff00)	/* top half of the syscall number */ ;\
246*d8b80295SApple OSS Distributions 	orr		r12, r12, #((num) & 0xff)	/* bottom half */ ;\
247*d8b80295SApple OSS Distributions 	.endif								;\
248*d8b80295SApple OSS Distributions 	swi		#SWI_SYSCALL
249*d8b80295SApple OSS Distributions 
250*d8b80295SApple OSS Distributions /* simple syscalls (0 to 4 args) */
251*d8b80295SApple OSS Distributions #define	SYSCALL_0to4(name, cerror)			\
252*d8b80295SApple OSS Distributions 	MI_ENTRY_POINT(_##name)					;\
253*d8b80295SApple OSS Distributions 	DO_SYSCALL(SYS_##name)					;\
254*d8b80295SApple OSS Distributions 	bxcc	lr								/* return if carry is clear (no error) */ ; \
255*d8b80295SApple OSS Distributions 1:	MI_BRANCH_EXTERNAL(_##cerror)
256*d8b80295SApple OSS Distributions 
257*d8b80295SApple OSS Distributions /* syscalls with 5 args is different, because of the single arg register load */
258*d8b80295SApple OSS Distributions #define	SYSCALL_5(name, cerror)				\
259*d8b80295SApple OSS Distributions 	MI_ENTRY_POINT(_##name)					;\
260*d8b80295SApple OSS Distributions 	mov		ip, sp							/* save a pointer to the args */ ; \
261*d8b80295SApple OSS Distributions 	stmfd	sp!, { r4-r5 }					/* save r4-r5 */ ;\
262*d8b80295SApple OSS Distributions 	ldr		r4, [ip]						/* load 5th arg */ ; \
263*d8b80295SApple OSS Distributions 	DO_SYSCALL(SYS_##name)					;\
264*d8b80295SApple OSS Distributions 	ldmfd	sp!, { r4-r5 }					/* restore r4-r5 */ ; \
265*d8b80295SApple OSS Distributions 	bxcc	lr								/* return if carry is clear (no error) */ ; \
266*d8b80295SApple OSS Distributions 1:	MI_BRANCH_EXTERNAL(_##cerror)
267*d8b80295SApple OSS Distributions 
268*d8b80295SApple OSS Distributions /* syscalls with 6 to 12 args. kernel may have to read from stack */
269*d8b80295SApple OSS Distributions #define SYSCALL_6to12(name, save_regs, arg_regs, cerror) \
270*d8b80295SApple OSS Distributions 	MI_ENTRY_POINT(_##name)					;\
271*d8b80295SApple OSS Distributions 	mov		ip, sp							/* save a pointer to the args */ ; \
272*d8b80295SApple OSS Distributions 	stmfd	sp!, { save_regs }				/* callee saved regs */ ;\
273*d8b80295SApple OSS Distributions 	ldmia	ip, { arg_regs }				/* load arg regs */ ; \
274*d8b80295SApple OSS Distributions 	DO_SYSCALL(SYS_##name)					;\
275*d8b80295SApple OSS Distributions 	ldmfd	sp!, { save_regs }				/* restore callee saved regs */ ; \
276*d8b80295SApple OSS Distributions 	bxcc	lr								/* return if carry is clear (no error) */ ; \
277*d8b80295SApple OSS Distributions 1:	MI_BRANCH_EXTERNAL(_##cerror)
278*d8b80295SApple OSS Distributions 
279*d8b80295SApple OSS Distributions #define COMMA ,
280*d8b80295SApple OSS Distributions 
281*d8b80295SApple OSS Distributions #if __BIGGEST_ALIGNMENT__ > 4
282*d8b80295SApple OSS Distributions 
283*d8b80295SApple OSS Distributions /* For the armv7k ABI, the alignment requirements may add padding. So we
284*d8b80295SApple OSS Distributions  * let the kernel figure it out and push extra on the stack to avoid un-needed
285*d8b80295SApple OSS Distributions  * copy-ins */
286*d8b80295SApple OSS Distributions 
287*d8b80295SApple OSS Distributions  /* We'll also use r8 for moving arguments */
288*d8b80295SApple OSS Distributions 
289*d8b80295SApple OSS Distributions #define SYSCALL_0(name)						SYSCALL_0to4(name)
290*d8b80295SApple OSS Distributions #define SYSCALL_1(name)						SYSCALL_0to4(name)
291*d8b80295SApple OSS Distributions #define SYSCALL_2(name)						SYSCALL_0to4(name)
292*d8b80295SApple OSS Distributions #define SYSCALL_3(name)						SYSCALL_0to4(name)
293*d8b80295SApple OSS Distributions #define SYSCALL_4(name)						SYSCALL_6to12(name, r4-r5, r4-r5)
294*d8b80295SApple OSS Distributions #undef SYSCALL_5
295*d8b80295SApple OSS Distributions #define SYSCALL_5(name)						SYSCALL_6to12(name, r4-r5, r4-r5)
296*d8b80295SApple OSS Distributions #define SYSCALL_6(name)						SYSCALL_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8)
297*d8b80295SApple OSS Distributions #define SYSCALL_7(name)						SYSCALL_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8)
298*d8b80295SApple OSS Distributions #define SYSCALL_8(name)						SYSCALL_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8)
299*d8b80295SApple OSS Distributions #define SYSCALL_12(name)					SYSCALL_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8)
300*d8b80295SApple OSS Distributions 
301*d8b80295SApple OSS Distributions #else // !(__BIGGEST_ALIGNMENT__ > 4) (the normal arm32 ABI case)
302*d8b80295SApple OSS Distributions 
303*d8b80295SApple OSS Distributions #define SYSCALL_0(name)						SYSCALL_0to4(name)
304*d8b80295SApple OSS Distributions #define SYSCALL_1(name)						SYSCALL_0to4(name)
305*d8b80295SApple OSS Distributions #define SYSCALL_2(name)						SYSCALL_0to4(name)
306*d8b80295SApple OSS Distributions #define SYSCALL_3(name)						SYSCALL_0to4(name)
307*d8b80295SApple OSS Distributions #define SYSCALL_4(name)						SYSCALL_0to4(name)
308*d8b80295SApple OSS Distributions /* SYSCALL_5 declared above */
309*d8b80295SApple OSS Distributions #define SYSCALL_6(name)						SYSCALL_6to12(name, r4-r5, r4-r5)
310*d8b80295SApple OSS Distributions #define SYSCALL_7(name)						SYSCALL_6to12(name, r4-r6 COMMA r8, r4-r6)
311*d8b80295SApple OSS Distributions #define SYSCALL_8(name)						SYSCALL_6to12(name, r4-r6 COMMA r8, r4-r6) /* 8th on stack */
312*d8b80295SApple OSS Distributions #define SYSCALL_12(name)					SYSCALL_6to12(name, r4-r6 COMMA r8, r4-r6) /* 8th-12th on stack */
313*d8b80295SApple OSS Distributions 
314*d8b80295SApple OSS Distributions #endif // __BIGGEST_ALIGNMENT__ > 4
315*d8b80295SApple OSS Distributions 
316*d8b80295SApple OSS Distributions /* select the appropriate syscall code, based on the number of arguments */
317*d8b80295SApple OSS Distributions #ifndef __SYSCALL_32BIT_ARG_BYTES
318*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_##nargs(name, cerror)
319*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_##nargs(name, cerror)
320*d8b80295SApple OSS Distributions #else
321*d8b80295SApple OSS Distributions #if __SYSCALL_32BIT_ARG_BYTES < 20
322*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_0to4(name, cerror)
323*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_0to4(name, cerror)
324*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 20
325*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_5(name, cerror)
326*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_5(name, cerror)
327*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 24
328*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_6(name, cerror)
329*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_6(name, cerror)
330*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 28
331*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_7(name, cerror)
332*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_7(name, cerror)
333*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 32
334*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_8(name, cerror)
335*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_8(name, cerror)
336*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 36
337*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_8(name, cerror)
338*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_8(name, cerror)
339*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 40
340*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_8(name, cerror)
341*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_8(name, cerror)
342*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 44
343*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_8(name, cerror)
344*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_8(name, cerror)
345*d8b80295SApple OSS Distributions #elif __SYSCALL_32BIT_ARG_BYTES == 48
346*d8b80295SApple OSS Distributions #define SYSCALL(name, nargs, cerror)		SYSCALL_12(name, cerror)
347*d8b80295SApple OSS Distributions #define SYSCALL_NONAME(name, nargs, cerror)	SYSCALL_NONAME_12(name, cerror)
348*d8b80295SApple OSS Distributions #endif
349*d8b80295SApple OSS Distributions #endif
350*d8b80295SApple OSS Distributions 
351*d8b80295SApple OSS Distributions #define	SYSCALL_NONAME_0to4(name, cerror)	\
352*d8b80295SApple OSS Distributions 	DO_SYSCALL(SYS_##name)					;\
353*d8b80295SApple OSS Distributions 	bcc		1f								/* branch if carry bit is clear (no error) */ ; \
354*d8b80295SApple OSS Distributions 	MI_BRANCH_EXTERNAL(_##cerror)			/* call cerror */ ; \
355*d8b80295SApple OSS Distributions 1:
356*d8b80295SApple OSS Distributions 
357*d8b80295SApple OSS Distributions #define	SYSCALL_NONAME_5(name, cerror)		\
358*d8b80295SApple OSS Distributions 	mov		ip, sp 							/* save a pointer to the args */ ; \
359*d8b80295SApple OSS Distributions 	stmfd	sp!, { r4-r5 }					/* save r4-r5 */ ;\
360*d8b80295SApple OSS Distributions 	ldr		r4, [ip]						/* load 5th arg */ ; \
361*d8b80295SApple OSS Distributions 	DO_SYSCALL(SYS_##name)					;\
362*d8b80295SApple OSS Distributions 	ldmfd	sp!, { r4-r5 }					/* restore r4-r7 */ ; \
363*d8b80295SApple OSS Distributions 	bcc		1f								/* branch if carry bit is clear (no error) */ ; \
364*d8b80295SApple OSS Distributions 	MI_BRANCH_EXTERNAL(_##cerror)			/* call cerror */ ; \
365*d8b80295SApple OSS Distributions 1:
366*d8b80295SApple OSS Distributions 
367*d8b80295SApple OSS Distributions #define	SYSCALL_NONAME_6to12(name, save_regs, arg_regs, cerror)	\
368*d8b80295SApple OSS Distributions 	mov		ip, sp 							/* save a pointer to the args */ ; \
369*d8b80295SApple OSS Distributions 	stmfd	sp!, { save_regs }				/* callee save regs */ ;\
370*d8b80295SApple OSS Distributions 	ldmia	ip, { arg_regs }				/* load arguments */ ; \
371*d8b80295SApple OSS Distributions 	DO_SYSCALL(SYS_##name)					;\
372*d8b80295SApple OSS Distributions 	ldmfd	sp!, { save_regs }				/* restore callee saved regs */ ; \
373*d8b80295SApple OSS Distributions 	bcc		1f								/* branch if carry bit is clear (no error) */ ; \
374*d8b80295SApple OSS Distributions 	MI_BRANCH_EXTERNAL(_##cerror)			/* call cerror */ ; \
375*d8b80295SApple OSS Distributions 1:
376*d8b80295SApple OSS Distributions 
377*d8b80295SApple OSS Distributions 
378*d8b80295SApple OSS Distributions #if __BIGGEST_ALIGNMENT__ > 4
379*d8b80295SApple OSS Distributions 
380*d8b80295SApple OSS Distributions /* For the armv7k ABI, the alignment requirements may add padding. So we
381*d8b80295SApple OSS Distributions  * let the kernel figure it out and push extra on the stack to avoid un-needed
382*d8b80295SApple OSS Distributions  * copy-ins. We are relying on arguments that aren't in registers starting
383*d8b80295SApple OSS Distributions  * 32 bytes from sp. We also use r8 like in the mach case. */
384*d8b80295SApple OSS Distributions 
385*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_0(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
386*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_1(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
387*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_2(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
388*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_3(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
389*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_4(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r5, r4-r5, cerror)
390*d8b80295SApple OSS Distributions #undef SYSCALL_NONAME_5
391*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_5(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r5, r4-r5, cerror)
392*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_6(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8, cerror)
393*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_7(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8, cerror)
394*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_8(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8, cerror)
395*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_12(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r6 COMMA r8, r4-r6 COMMA r8, cerror)
396*d8b80295SApple OSS Distributions 
397*d8b80295SApple OSS Distributions #else // !(__BIGGEST_ALIGNMENT__ > 4) (the normal arm32 ABI case)
398*d8b80295SApple OSS Distributions 
399*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_0(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
400*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_1(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
401*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_2(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
402*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_3(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
403*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_4(name, cerror)				SYSCALL_NONAME_0to4(name, cerror)
404*d8b80295SApple OSS Distributions /* SYSCALL_NONAME_5 declared above */
405*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_6(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r5, r4-r5, cerror)
406*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_7(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r6 COMMA r8, r4-r6, cerror)
407*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_8(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r6 COMMA r8, r4-r6, cerror)
408*d8b80295SApple OSS Distributions #define SYSCALL_NONAME_12(name, cerror)				SYSCALL_NONAME_6to12(name, r4-r6 COMMA r8, r4-r6, cerror)
409*d8b80295SApple OSS Distributions 
410*d8b80295SApple OSS Distributions #endif // __BIGGEST_ALIGNMENT__ > 4
411*d8b80295SApple OSS Distributions 
412*d8b80295SApple OSS Distributions #define	PSEUDO(pseudo, name, nargs, cerror)			\
413*d8b80295SApple OSS Distributions 	.globl pseudo						;\
414*d8b80295SApple OSS Distributions 	.text									;\
415*d8b80295SApple OSS Distributions 	.align  2								;\
416*d8b80295SApple OSS Distributions pseudo:									;\
417*d8b80295SApple OSS Distributions 	SYSCALL_NONAME(name, nargs, cerror)
418*d8b80295SApple OSS Distributions 
419*d8b80295SApple OSS Distributions #define __SYSCALL2(pseudo, name, nargs, cerror)		\
420*d8b80295SApple OSS Distributions 	PSEUDO(pseudo, name, nargs, cerror)				;\
421*d8b80295SApple OSS Distributions 	bx lr
422*d8b80295SApple OSS Distributions 
423*d8b80295SApple OSS Distributions #define __SYSCALL(pseudo, name, nargs)				\
424*d8b80295SApple OSS Distributions 	PSEUDO(pseudo, name, nargs, cerror)				;\
425*d8b80295SApple OSS Distributions 	bx lr
426*d8b80295SApple OSS Distributions 
427*d8b80295SApple OSS Distributions #elif defined(__arm64__)
428*d8b80295SApple OSS Distributions 
429*d8b80295SApple OSS Distributions #include <mach/arm/syscall_sw.h>
430*d8b80295SApple OSS Distributions #include <mach/arm/vm_param.h>
431*d8b80295SApple OSS Distributions #include <mach/arm64/asm.h>
432*d8b80295SApple OSS Distributions 
433*d8b80295SApple OSS Distributions #if defined(__arm64__) && !defined(__LP64__)
434*d8b80295SApple OSS Distributions #define ZERO_EXTEND(argnum) uxtw  x ## argnum, w ## argnum
435*d8b80295SApple OSS Distributions #else
436*d8b80295SApple OSS Distributions #define ZERO_EXTEND(argnum)
437*d8b80295SApple OSS Distributions #endif
438*d8b80295SApple OSS Distributions 
439*d8b80295SApple OSS Distributions #if defined(__arm64__) && !defined(__LP64__)
440*d8b80295SApple OSS Distributions #define SIGN_EXTEND(argnum) sxtw  x ## argnum, w ## argnum
441*d8b80295SApple OSS Distributions #else
442*d8b80295SApple OSS Distributions #define SIGN_EXTEND(argnum)
443*d8b80295SApple OSS Distributions #endif
444*d8b80295SApple OSS Distributions 
445*d8b80295SApple OSS Distributions /*
446*d8b80295SApple OSS Distributions  * ARM64 system call interface:
447*d8b80295SApple OSS Distributions  *
448*d8b80295SApple OSS Distributions  * TBD
449*d8b80295SApple OSS Distributions  */
450*d8b80295SApple OSS Distributions 
451*d8b80295SApple OSS Distributions #define DO_SYSCALL(num, cerror)                 \
452*d8b80295SApple OSS Distributions 	mov   x16, #(num)                     %%\
453*d8b80295SApple OSS Distributions 	svc   #SWI_SYSCALL                    %%\
454*d8b80295SApple OSS Distributions 	b.cc  2f                              %%\
455*d8b80295SApple OSS Distributions 	ARM64_STACK_PROLOG                    %%\
456*d8b80295SApple OSS Distributions 	PUSH_FRAME                            %%\
457*d8b80295SApple OSS Distributions 	bl    _##cerror                       %%\
458*d8b80295SApple OSS Distributions 	POP_FRAME                             %%\
459*d8b80295SApple OSS Distributions 	ARM64_STACK_EPILOG                    %%\
460*d8b80295SApple OSS Distributions 2:
461*d8b80295SApple OSS Distributions 
462*d8b80295SApple OSS Distributions #define MI_GET_ADDRESS(reg,var)  \
463*d8b80295SApple OSS Distributions    adrp	reg, var@page      %%\
464*d8b80295SApple OSS Distributions    add  reg, reg, var@pageoff   %%
465*d8b80295SApple OSS Distributions 
466*d8b80295SApple OSS Distributions #define MI_CALL_EXTERNAL(sym)	\
467*d8b80295SApple OSS Distributions    .globl sym                %% \
468*d8b80295SApple OSS Distributions    bl sym
469*d8b80295SApple OSS Distributions 
470*d8b80295SApple OSS Distributions #define	SYSCALL_NONAME(name, nargs, cerror)						\
471*d8b80295SApple OSS Distributions   DO_SYSCALL(SYS_##name, cerror)					%%	\
472*d8b80295SApple OSS Distributions 1:
473*d8b80295SApple OSS Distributions 
474*d8b80295SApple OSS Distributions #define MI_ENTRY_POINT(name)				\
475*d8b80295SApple OSS Distributions   .text					%% \
476*d8b80295SApple OSS Distributions   .align 2	            %% \
477*d8b80295SApple OSS Distributions   .globl  name			%%	\
478*d8b80295SApple OSS Distributions name:
479*d8b80295SApple OSS Distributions 
480*d8b80295SApple OSS Distributions #define	PSEUDO(pseudo, name, nargs, cerror)			\
481*d8b80295SApple OSS Distributions   .text									%% \
482*d8b80295SApple OSS Distributions   .align  2								%% \
483*d8b80295SApple OSS Distributions   .globl pseudo						%%		\
484*d8b80295SApple OSS Distributions   pseudo:									%% \
485*d8b80295SApple OSS Distributions 	SYSCALL_NONAME(name, nargs, cerror)
486*d8b80295SApple OSS Distributions 
487*d8b80295SApple OSS Distributions #define __SYSCALL(pseudo, name, nargs)		\
488*d8b80295SApple OSS Distributions   PSEUDO(pseudo, name, nargs, cerror)		%%	\
489*d8b80295SApple OSS Distributions   ret
490*d8b80295SApple OSS Distributions 
491*d8b80295SApple OSS Distributions #define __SYSCALL2(pseudo, name, nargs, cerror)		\
492*d8b80295SApple OSS Distributions   PSEUDO(pseudo, name, nargs, cerror)		%% \
493*d8b80295SApple OSS Distributions   ret
494*d8b80295SApple OSS Distributions 
495*d8b80295SApple OSS Distributions #else
496*d8b80295SApple OSS Distributions #error Unsupported architecture
497*d8b80295SApple OSS Distributions #endif
498*d8b80295SApple OSS Distributions 
499