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