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