xref: /xnu-11215.61.5/osfmk/arm/asm.h (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1*4f1223e8SApple OSS Distributions /*
2*4f1223e8SApple OSS Distributions  * Copyright (c) 2007 Apple Inc. All rights reserved.
3*4f1223e8SApple OSS Distributions  *
4*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*4f1223e8SApple OSS Distributions  *
6*4f1223e8SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*4f1223e8SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*4f1223e8SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*4f1223e8SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*4f1223e8SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*4f1223e8SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*4f1223e8SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*4f1223e8SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*4f1223e8SApple OSS Distributions  *
15*4f1223e8SApple OSS Distributions  * Please obtain a copy of the License at
16*4f1223e8SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*4f1223e8SApple OSS Distributions  *
18*4f1223e8SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*4f1223e8SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*4f1223e8SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*4f1223e8SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*4f1223e8SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*4f1223e8SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*4f1223e8SApple OSS Distributions  * limitations under the License.
25*4f1223e8SApple OSS Distributions  *
26*4f1223e8SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*4f1223e8SApple OSS Distributions  */
28*4f1223e8SApple OSS Distributions /*
29*4f1223e8SApple OSS Distributions  * @OSF_COPYRIGHT@
30*4f1223e8SApple OSS Distributions  */
31*4f1223e8SApple OSS Distributions /*
32*4f1223e8SApple OSS Distributions  * Mach Operating System
33*4f1223e8SApple OSS Distributions  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34*4f1223e8SApple OSS Distributions  * All Rights Reserved.
35*4f1223e8SApple OSS Distributions  *
36*4f1223e8SApple OSS Distributions  * Permission to use, copy, modify and distribute this software and its
37*4f1223e8SApple OSS Distributions  * documentation is hereby granted, provided that both the copyright
38*4f1223e8SApple OSS Distributions  * notice and this permission notice appear in all copies of the
39*4f1223e8SApple OSS Distributions  * software, derivative works or modified versions, and any portions
40*4f1223e8SApple OSS Distributions  * thereof, and that both notices appear in supporting documentation.
41*4f1223e8SApple OSS Distributions  *
42*4f1223e8SApple OSS Distributions  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43*4f1223e8SApple OSS Distributions  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44*4f1223e8SApple OSS Distributions  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45*4f1223e8SApple OSS Distributions  *
46*4f1223e8SApple OSS Distributions  * Carnegie Mellon requests users of this software to return to
47*4f1223e8SApple OSS Distributions  *
48*4f1223e8SApple OSS Distributions  *  Software Distribution Coordinator  or  [email protected]
49*4f1223e8SApple OSS Distributions  *  School of Computer Science
50*4f1223e8SApple OSS Distributions  *  Carnegie Mellon University
51*4f1223e8SApple OSS Distributions  *  Pittsburgh PA 15213-3890
52*4f1223e8SApple OSS Distributions  *
53*4f1223e8SApple OSS Distributions  * any improvements or extensions that they make and grant Carnegie Mellon
54*4f1223e8SApple OSS Distributions  * the rights to redistribute these changes.
55*4f1223e8SApple OSS Distributions  */
56*4f1223e8SApple OSS Distributions 
57*4f1223e8SApple OSS Distributions #ifndef	_ARM_ASM_H_
58*4f1223e8SApple OSS Distributions #define	_ARM_ASM_H_
59*4f1223e8SApple OSS Distributions 
60*4f1223e8SApple OSS Distributions #if defined (__arm__) || defined (__arm64__)
61*4f1223e8SApple OSS Distributions 
62*4f1223e8SApple OSS Distributions #include <arm/arch.h>
63*4f1223e8SApple OSS Distributions 
64*4f1223e8SApple OSS Distributions #define FRAME	pushl %ebp; movl %esp, %ebp
65*4f1223e8SApple OSS Distributions #define EMARF	leave
66*4f1223e8SApple OSS Distributions 
67*4f1223e8SApple OSS Distributions 
68*4f1223e8SApple OSS Distributions /* There is another definition of ALIGN for .c sources */
69*4f1223e8SApple OSS Distributions #ifdef ASSEMBLER
70*4f1223e8SApple OSS Distributions #define ALIGN 2
71*4f1223e8SApple OSS Distributions #endif /* ASSEMBLER */
72*4f1223e8SApple OSS Distributions 
73*4f1223e8SApple OSS Distributions #ifndef FALIGN
74*4f1223e8SApple OSS Distributions #define FALIGN ALIGN
75*4f1223e8SApple OSS Distributions #endif
76*4f1223e8SApple OSS Distributions 
77*4f1223e8SApple OSS Distributions #define LB(x,n) n
78*4f1223e8SApple OSS Distributions #if	__STDC__
79*4f1223e8SApple OSS Distributions #ifndef __NO_UNDERSCORES__
80*4f1223e8SApple OSS Distributions #define	LCL(x)	L ## x
81*4f1223e8SApple OSS Distributions #define EXT(x) _ ## x
82*4f1223e8SApple OSS Distributions #define LEXT(x) _ ## x ## :
83*4f1223e8SApple OSS Distributions #else
84*4f1223e8SApple OSS Distributions #define	LCL(x)	.L ## x
85*4f1223e8SApple OSS Distributions #define EXT(x) x
86*4f1223e8SApple OSS Distributions #define LEXT(x) x ## :
87*4f1223e8SApple OSS Distributions #endif
88*4f1223e8SApple OSS Distributions #define LBc(x,n) n ## :
89*4f1223e8SApple OSS Distributions #define LBb(x,n) n ## b
90*4f1223e8SApple OSS Distributions #define LBf(x,n) n ## f
91*4f1223e8SApple OSS Distributions #else /* __STDC__ */
92*4f1223e8SApple OSS Distributions #ifndef __NO_UNDERSCORES__
93*4f1223e8SApple OSS Distributions #define LCL(x) L/**/x
94*4f1223e8SApple OSS Distributions #define EXT(x) _/**/x
95*4f1223e8SApple OSS Distributions #define LEXT(x) _/**/x/**/:
96*4f1223e8SApple OSS Distributions #else /* __NO_UNDERSCORES__ */
97*4f1223e8SApple OSS Distributions #define	LCL(x)	.L/**/x
98*4f1223e8SApple OSS Distributions #define EXT(x) x
99*4f1223e8SApple OSS Distributions #define LEXT(x) x/**/:
100*4f1223e8SApple OSS Distributions #endif /* __NO_UNDERSCORES__ */
101*4f1223e8SApple OSS Distributions #define LBc(x,n) n/**/:
102*4f1223e8SApple OSS Distributions #define LBb(x,n) n/**/b
103*4f1223e8SApple OSS Distributions #define LBf(x,n) n/**/f
104*4f1223e8SApple OSS Distributions #endif /* __STDC__ */
105*4f1223e8SApple OSS Distributions 
106*4f1223e8SApple OSS Distributions #define String	.asciz
107*4f1223e8SApple OSS Distributions #define Value	.word
108*4f1223e8SApple OSS Distributions #define Times(a,b) (a*b)
109*4f1223e8SApple OSS Distributions #define Divide(a,b) (a/b)
110*4f1223e8SApple OSS Distributions 
111*4f1223e8SApple OSS Distributions #if 0 /* TOTOJK */
112*4f1223e8SApple OSS Distributions #ifdef __ELF__
113*4f1223e8SApple OSS Distributions #define ELF_FUNC(x)	.type x,@function
114*4f1223e8SApple OSS Distributions #define ELF_DATA(x)	.type x,@object
115*4f1223e8SApple OSS Distributions #define ELF_SIZE(x,s)	.size x,s
116*4f1223e8SApple OSS Distributions #else
117*4f1223e8SApple OSS Distributions #define ELF_FUNC(x)
118*4f1223e8SApple OSS Distributions #define ELF_DATA(x)
119*4f1223e8SApple OSS Distributions #define ELF_SIZE(x,s)
120*4f1223e8SApple OSS Distributions #endif
121*4f1223e8SApple OSS Distributions #else
122*4f1223e8SApple OSS Distributions #define ELF_FUNC(x)
123*4f1223e8SApple OSS Distributions #define ELF_DATA(x)
124*4f1223e8SApple OSS Distributions #define ELF_SIZE(x,s)
125*4f1223e8SApple OSS Distributions #endif /* TODOJK */
126*4f1223e8SApple OSS Distributions 
127*4f1223e8SApple OSS Distributions #define	Entry(x)	.globl EXT(x); ELF_FUNC(EXT(x)); .align FALIGN; LEXT(x)
128*4f1223e8SApple OSS Distributions #define	ENTRY(x)	Entry(x) MCOUNT
129*4f1223e8SApple OSS Distributions #define	ENTRY2(x,y)	.globl EXT(x); .globl EXT(y); \
130*4f1223e8SApple OSS Distributions 			ELF_FUNC(EXT(x)); ELF_FUNC(EXT(y)); \
131*4f1223e8SApple OSS Distributions 			.align FALIGN; LEXT(x); LEXT(y) \
132*4f1223e8SApple OSS Distributions 			MCOUNT
133*4f1223e8SApple OSS Distributions #if __STDC__
134*4f1223e8SApple OSS Distributions #define	ASENTRY(x) 	.globl x; .align FALIGN; x ## : ELF_FUNC(x) MCOUNT
135*4f1223e8SApple OSS Distributions #else
136*4f1223e8SApple OSS Distributions #define	ASENTRY(x) 	.globl x; .align FALIGN; x: ELF_FUNC(x) MCOUNT
137*4f1223e8SApple OSS Distributions #endif /* __STDC__ */
138*4f1223e8SApple OSS Distributions 
139*4f1223e8SApple OSS Distributions #define	DATA(x)		.globl EXT(x); ELF_DATA(EXT(x)); .align ALIGN; LEXT(x)
140*4f1223e8SApple OSS Distributions 
141*4f1223e8SApple OSS Distributions #define End(x)		ELF_SIZE(x,.-x)
142*4f1223e8SApple OSS Distributions #define END(x)		End(EXT(x))
143*4f1223e8SApple OSS Distributions #define ENDDATA(x)	END(x)
144*4f1223e8SApple OSS Distributions #define Enddata(x)	End(x)
145*4f1223e8SApple OSS Distributions 
146*4f1223e8SApple OSS Distributions #ifdef ASSEMBLER
147*4f1223e8SApple OSS Distributions 
148*4f1223e8SApple OSS Distributions #define MCOUNT
149*4f1223e8SApple OSS Distributions 
150*4f1223e8SApple OSS Distributions #else /* NOT ASSEMBLER */
151*4f1223e8SApple OSS Distributions 
152*4f1223e8SApple OSS Distributions /* These defines are here for .c files that wish to reference global symbols
153*4f1223e8SApple OSS Distributions  * within __asm__ statements.
154*4f1223e8SApple OSS Distributions  */
155*4f1223e8SApple OSS Distributions #ifndef __NO_UNDERSCORES__
156*4f1223e8SApple OSS Distributions #define CC_SYM_PREFIX "_"
157*4f1223e8SApple OSS Distributions #else
158*4f1223e8SApple OSS Distributions #define CC_SYM_PREFIX ""
159*4f1223e8SApple OSS Distributions #endif /* __NO_UNDERSCORES__ */
160*4f1223e8SApple OSS Distributions #endif /* ASSEMBLER */
161*4f1223e8SApple OSS Distributions 
162*4f1223e8SApple OSS Distributions #ifdef ASSEMBLER
163*4f1223e8SApple OSS Distributions 
164*4f1223e8SApple OSS Distributions #if defined (_ARM_ARCH_4T)
165*4f1223e8SApple OSS Distributions # define RET    bx      lr
166*4f1223e8SApple OSS Distributions # define RETeq  bxeq    lr
167*4f1223e8SApple OSS Distributions # define RETne  bxne    lr
168*4f1223e8SApple OSS Distributions # ifdef __STDC__
169*4f1223e8SApple OSS Distributions #  define RETc(c) bx##c lr
170*4f1223e8SApple OSS Distributions # else
171*4f1223e8SApple OSS Distributions #  define RETc(c) bx/**/c       lr
172*4f1223e8SApple OSS Distributions # endif
173*4f1223e8SApple OSS Distributions #else
174*4f1223e8SApple OSS Distributions # define RET    mov     pc, lr
175*4f1223e8SApple OSS Distributions # define RETeq  moveq   pc, lr
176*4f1223e8SApple OSS Distributions # define RETne  movne   pc, lr
177*4f1223e8SApple OSS Distributions # ifdef __STDC__
178*4f1223e8SApple OSS Distributions #  define RETc(c) mov##c        pc, lr
179*4f1223e8SApple OSS Distributions # else
180*4f1223e8SApple OSS Distributions #  define RETc(c) mov/**/c      pc, lr
181*4f1223e8SApple OSS Distributions # endif
182*4f1223e8SApple OSS Distributions #endif
183*4f1223e8SApple OSS Distributions 
184*4f1223e8SApple OSS Distributions #if defined (__thumb__)
185*4f1223e8SApple OSS Distributions /* Provide a PI mechanism for thumb branching. */
186*4f1223e8SApple OSS Distributions # define BRANCH_EXTERN(x)	ldr	pc, [pc, #-4] ;	\
187*4f1223e8SApple OSS Distributions 				.long	EXT(x)
188*4f1223e8SApple OSS Distributions #else
189*4f1223e8SApple OSS Distributions # define BRANCH_EXTERN(x)	b	EXT(x)
190*4f1223e8SApple OSS Distributions #endif
191*4f1223e8SApple OSS Distributions 
192*4f1223e8SApple OSS Distributions /*
193*4f1223e8SApple OSS Distributions  * arg0: Register for thread pointer
194*4f1223e8SApple OSS Distributions  */
195*4f1223e8SApple OSS Distributions .macro READ_THREAD
196*4f1223e8SApple OSS Distributions 	mrc p15, 0, $0, c13, c0, 4  /* Read TPIDRPRW */
197*4f1223e8SApple OSS Distributions .endmacro
198*4f1223e8SApple OSS Distributions 
199*4f1223e8SApple OSS Distributions 
200*4f1223e8SApple OSS Distributions /* Macros for loading up addresses that are external to the .s file.
201*4f1223e8SApple OSS Distributions  * LOAD_ADDR:  loads the address for (label) into (reg). Not safe for
202*4f1223e8SApple OSS Distributions  *   loading to the PC.
203*4f1223e8SApple OSS Distributions  * LOAD_ADDR_PC:  Variant for loading to the PC; load the address of (label)
204*4f1223e8SApple OSS Distributions  *   into the pc.
205*4f1223e8SApple OSS Distributions  * LOAD_ADDR_GEN_DEF:  The general definition needed to support loading
206*4f1223e8SApple OSS Distributions  *   a label address.
207*4f1223e8SApple OSS Distributions  *
208*4f1223e8SApple OSS Distributions  * Usage:  For any label accessed, we require one (and only one) instance
209*4f1223e8SApple OSS Distributions  *   of LOAD_ADDR_GEN_DEF(label).
210*4f1223e8SApple OSS Distributions  *
211*4f1223e8SApple OSS Distributions  * Example:
212*4f1223e8SApple OSS Distributions  *   LOAD_ADDR(r0, arm_init)
213*4f1223e8SApple OSS Distributions  *   LOAD_ADDR(lr, arm_init_cpu)
214*4f1223e8SApple OSS Distributions  *   LOAD_ADDR_PC(arm_init)
215*4f1223e8SApple OSS Distributions  *   ...
216*4f1223e8SApple OSS Distributions  *
217*4f1223e8SApple OSS Distributions  *   LOAD_ADDR_GEN_DEF(arm_init)
218*4f1223e8SApple OSS Distributions  *   LOAD_ADDR_GEN_DEF(arm_init_cpu)
219*4f1223e8SApple OSS Distributions  */
220*4f1223e8SApple OSS Distributions 
221*4f1223e8SApple OSS Distributions /* Definitions for a position dependent kernel using non-lazy pointers.
222*4f1223e8SApple OSS Distributions  */
223*4f1223e8SApple OSS Distributions 
224*4f1223e8SApple OSS Distributions /* TODO: Make this work with thumb .s files. */
225*4f1223e8SApple OSS Distributions #define PC_INC	0x8
226*4f1223e8SApple OSS Distributions 
227*4f1223e8SApple OSS Distributions /* We need wrapper macros in order to ensure that __LINE__ is expanded.
228*4f1223e8SApple OSS Distributions  *
229*4f1223e8SApple OSS Distributions  * There is some small potential for duplicate labels here, but because
230*4f1223e8SApple OSS Distributions  *   we do not export the generated labels, it should not be an issue.
231*4f1223e8SApple OSS Distributions  */
232*4f1223e8SApple OSS Distributions 
233*4f1223e8SApple OSS Distributions #define GLUE_LABEL_GUTS(label, tag) L_##label##_##tag##_glue
234*4f1223e8SApple OSS Distributions #define GLUE_LABEL(label, tag) GLUE_LABEL_GUTS(label, tag)
235*4f1223e8SApple OSS Distributions 
236*4f1223e8SApple OSS Distributions #define LOAD_ADDR(reg, label)                                                                   \
237*4f1223e8SApple OSS Distributions 	movw	reg, :lower16:(label##$non_lazy_ptr - (GLUE_LABEL(label, __LINE__) + PC_INC)) ; \
238*4f1223e8SApple OSS Distributions 	movt	reg, :upper16:(label##$non_lazy_ptr - (GLUE_LABEL(label, __LINE__) + PC_INC)) ; \
239*4f1223e8SApple OSS Distributions GLUE_LABEL(label, __LINE__): ;                                                                  \
240*4f1223e8SApple OSS Distributions 	ldr	reg, [pc, reg]
241*4f1223e8SApple OSS Distributions 
242*4f1223e8SApple OSS Distributions /* Designed with the understanding that directly branching to thumb code
243*4f1223e8SApple OSS Distributions  *   is unreliable; this should allow for dealing with __thumb__ in
244*4f1223e8SApple OSS Distributions  *   assembly; the non-thumb variant still needs to provide the glue label
245*4f1223e8SApple OSS Distributions  *   to avoid failing to build on undefined symbols.
246*4f1223e8SApple OSS Distributions  *
247*4f1223e8SApple OSS Distributions  * TODO: Make this actually use a scratch register; this macro is convenient
248*4f1223e8SApple OSS Distributions  *   for translating (ldr pc, [?]) to a slidable format without the risk of
249*4f1223e8SApple OSS Distributions  *   clobbering registers, but it is also wasteful.
250*4f1223e8SApple OSS Distributions  */
251*4f1223e8SApple OSS Distributions #if defined(__thumb__)
252*4f1223e8SApple OSS Distributions #define LOAD_ADDR_PC(label)    \
253*4f1223e8SApple OSS Distributions 	stmfd	sp!, { r0 } ;  \
254*4f1223e8SApple OSS Distributions 	stmfd	sp!, { r0 } ;  \
255*4f1223e8SApple OSS Distributions 	LOAD_ADDR(r0, label) ; \
256*4f1223e8SApple OSS Distributions 	str	r0, [sp, #4] ; \
257*4f1223e8SApple OSS Distributions 	ldmfd	sp!, { r0 } ;  \
258*4f1223e8SApple OSS Distributions 	ldmfd	sp!, { pc }
259*4f1223e8SApple OSS Distributions #else
260*4f1223e8SApple OSS Distributions #define LOAD_ADDR_PC(label) \
261*4f1223e8SApple OSS Distributions 	b	EXT(label)
262*4f1223e8SApple OSS Distributions #endif
263*4f1223e8SApple OSS Distributions 
264*4f1223e8SApple OSS Distributions #define LOAD_ADDR_GEN_DEF(label)                                   \
265*4f1223e8SApple OSS Distributions 	.section __DATA,__nl_symbol_ptr,non_lazy_symbol_pointers ; \
266*4f1223e8SApple OSS Distributions 	.align 2 ;                                                 \
267*4f1223e8SApple OSS Distributions label##$non_lazy_ptr: ;                                            \
268*4f1223e8SApple OSS Distributions 	.indirect_symbol	EXT(label) ;                       \
269*4f1223e8SApple OSS Distributions 	.long			0
270*4f1223e8SApple OSS Distributions 
271*4f1223e8SApple OSS Distributions /* The linker can deal with branching from ARM to thumb in unconditional
272*4f1223e8SApple OSS Distributions  *   branches, but not in conditional branches.  To support this in our
273*4f1223e8SApple OSS Distributions  *   assembly (which allows us to build xnu without -mno-thumb), use the
274*4f1223e8SApple OSS Distributions  *   following macros for branching conditionally to external symbols.
275*4f1223e8SApple OSS Distributions  *   These macros are used just like the corresponding conditional branch
276*4f1223e8SApple OSS Distributions  *   instructions.
277*4f1223e8SApple OSS Distributions  */
278*4f1223e8SApple OSS Distributions 
279*4f1223e8SApple OSS Distributions #define SHIM_LABEL_GUTS(line_num) L_cond_extern_##line_num##_shim
280*4f1223e8SApple OSS Distributions #define SHIM_LABEL(line_num) SHIM_LABEL_GUTS(line_num)
281*4f1223e8SApple OSS Distributions 
282*4f1223e8SApple OSS Distributions #define COND_EXTERN_BEQ(label)         \
283*4f1223e8SApple OSS Distributions 	bne	SHIM_LABEL(__LINE__) ; \
284*4f1223e8SApple OSS Distributions 	b	EXT(label) ;           \
285*4f1223e8SApple OSS Distributions SHIM_LABEL(__LINE__):
286*4f1223e8SApple OSS Distributions 
287*4f1223e8SApple OSS Distributions #define COND_EXTERN_BLNE(label)        \
288*4f1223e8SApple OSS Distributions 	beq	SHIM_LABEL(__LINE__) ; \
289*4f1223e8SApple OSS Distributions 	bl	EXT(label) ;           \
290*4f1223e8SApple OSS Distributions SHIM_LABEL(__LINE__):
291*4f1223e8SApple OSS Distributions 
292*4f1223e8SApple OSS Distributions #define COND_EXTERN_BLGT(label)        \
293*4f1223e8SApple OSS Distributions 	ble	SHIM_LABEL(__LINE__) ; \
294*4f1223e8SApple OSS Distributions 	bl	EXT(label) ;           \
295*4f1223e8SApple OSS Distributions SHIM_LABEL(__LINE__):
296*4f1223e8SApple OSS Distributions 
297*4f1223e8SApple OSS Distributions #endif /* ASSEMBLER */
298*4f1223e8SApple OSS Distributions 
299*4f1223e8SApple OSS Distributions #endif /* defined (__arm__) || defined (__arm64__) */
300*4f1223e8SApple OSS Distributions 
301*4f1223e8SApple OSS Distributions #endif /* _ARM_ASM_H_ */
302