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