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