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