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