xref: /xnu-12377.81.4/osfmk/arm64/asm.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2007 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34  * All Rights Reserved.
35  *
36  * Permission to use, copy, modify and distribute this software and its
37  * documentation is hereby granted, provided that both the copyright
38  * notice and this permission notice appear in all copies of the
39  * software, derivative works or modified versions, and any portions
40  * thereof, and that both notices appear in supporting documentation.
41  *
42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 
57 #ifndef	_ARM_ASM_H_
58 #define	_ARM_ASM_H_
59 
60 #ifdef __arm64__
61 
62 #include <arm/arch.h>
63 #if XNU_KERNEL_PRIVATE
64 #include <pexpert/arm64/board_config.h>
65 #endif /* XNU_KERNEL_PRIVATE */
66 
67 /* There is another definition of ALIGN for .c sources */
68 #ifdef __ASSEMBLER__
69 #define ALIGN 2
70 #endif /* ASSEMBLER */
71 
72 #ifndef FALIGN
73 #define FALIGN ALIGN
74 #endif
75 
76 #define LB(x,n) n
77 #if	__STDC__
78 #ifndef __NO_UNDERSCORES__
79 #define	LCL(x)	L ## x
80 #define EXT(x) _ ## x
81 #define LEXT(x) _ ## x ## :
82 #else
83 #define	LCL(x)	.L ## x
84 #define EXT(x) x
85 #define LEXT(x) x ## :
86 #endif
87 #define LBc(x,n) n ## :
88 #define LBb(x,n) n ## b
89 #define LBf(x,n) n ## f
90 #else /* __STDC__ */
91 #ifndef __NO_UNDERSCORES__
92 #define LCL(x) L/**/x
93 #define EXT(x) _/**/x
94 #define LEXT(x) _/**/x/**/:
95 #else /* __NO_UNDERSCORES__ */
96 #define	LCL(x)	.L/**/x
97 #define EXT(x) x
98 #define LEXT(x) x/**/:
99 #endif /* __NO_UNDERSCORES__ */
100 #define LBc(x,n) n/**/:
101 #define LBb(x,n) n/**/b
102 #define LBf(x,n) n/**/f
103 #endif /* __STDC__ */
104 
105 #define String	.asciz
106 #define Value	.word
107 #define Times(a,b) (a*b)
108 #define Divide(a,b) (a/b)
109 
110 #ifdef __ASSEMBLER__
111 #if	MACH_KDB
112 #include <ddb/stab.h>
113 /*
114  * This pseudo-assembler line is added so that there will be at least
115  *	one N_SO entry in the symbol stable to define the current file name.
116  */
117 #endif	/* MACH_KDB */
118 
119 /*
120  * Multiline macros must use .macro syntax for now,
121  * as there is no ARM64 statement separator.
122  */
123 .macro ENTRY
124 	.align FALIGN
125 	.globl _$0
126 	_$0 :
127 .endmacro
128 
129 .macro ENTRY2
130 	.align FALIGN
131 	.globl _$0
132 	.globl _$1
133 	_$0 :
134 	_$1 :
135 .endmacro
136 
137 .macro READ_THREAD
138 	mrs $0, TPIDR_EL1
139 .endmacro
140 
141 .macro BRANCH_EXTERN
142 	b _$0
143 .endmacro
144 
145 .macro CALL_EXTERN
146 	bl _$0
147 .endmacro
148 
149 .macro MOV64
150 	movk $0, #((($1) >> 48) & 0x000000000000FFFF), lsl #48
151 	movk $0, #((($1) >> 32) & 0x000000000000FFFF), lsl #32
152 	movk $0, #((($1) >> 16) & 0x000000000000FFFF), lsl #16
153 	movk $0, #((($1) >> 00) & 0x000000000000FFFF), lsl #00
154 .endmacro
155 
156 .macro MOV32
157 	movz $0, #((($1) >> 16) & 0x000000000000FFFF), lsl #16
158 	movk $0, #((($1) >> 00) & 0x000000000000FFFF), lsl #00
159 .endmacro
160 
161 .macro ARM64_STACK_PROLOG
162 #if __has_feature(ptrauth_returns)
163 	pacibsp
164 #endif
165 .endmacro
166 
167 .macro ARM64_STACK_EPILOG
168 #if __has_feature(ptrauth_returns)
169 	retab
170 #else
171 	ret
172 #endif
173 .endmacro
174 
175 
176 /**
177  * Push a stack frame.
178  *
179  * Most callers should invoke ARM64_STACK_PROLOG first, since otherwise this will
180  * push an unsigned return address onto the stack.
181  */
182 #define PUSH_FRAME			\
183 	stp fp, lr, [sp, #-16]!		%% \
184 	mov fp, sp			%%
185 
186 /**
187  * Pop the most recent stack frame.
188  *
189  * Note: if the complementary PUSH_FRAME was not preceded by ARM64_STACK_PROLOG,
190  * then this operation could load an attacker-controlled return address from
191  * memory!  Either add ARM64_STACK_PROLOG, or use POP_FRAME_WITHOUT_LR if there
192  * are no plans to ever use the popped LR.
193  */
194 #define POP_FRAME			\
195 	mov sp, fp			%% \
196 	ldp fp, lr, [sp], #16		%%
197 
198 /**
199  * Pop the most recent stack frame, but do not update LR.
200  *
201  * This macro is intended for situations like kernel entry, where the caller
202  * doesn't actually need to preserve LR, but wants to push a stack frame
203  * anyway for the benefit of unwinders.
204  */
205 #define POP_FRAME_WITHOUT_LR		\
206 	mov sp, fp			%% \
207 	ldp fp, xzr, [sp], #16		%%
208 
209 #define EXT(x) _ ## x
210 
211 #ifdef  XNU_KERNEL_PRIVATE
212 .macro PANIC_UNIMPLEMENTED
213 	bl EXT(panic_unimplemented)
214 .endmacro
215 
216 /**
217  * Marks the first instruction of an externally callable assembly routine.
218  *
219  * Callable functions which might be executed via indirect branches (i.e. any
220  * function which could be invoked from C/C++) MUST use either this macro or
221  * ARM64_STACK_PROLOG, depending on whether LR will be spilled to the stack.
222  * Failure to use one of these macros may cause unexpected runtime BTI
223  * exceptions when invoking a function.
224  */
225 .macro ARM64_PROLOG
226 #if BTI_ENFORCED
227     bti c
228 #endif /* BTI_ENFORCED */
229 .endmacro
230 
231 /**
232  * Marks the first instruction of an indirect branch target.
233  *
234  * This macro is not required if the location is only ever branched to by direct
235  * branches (as is most common). This macro is necessary for computed
236  * branches (i.e. switch cases) and other forms of dynamic dispatch.
237  */
238 .macro ARM64_JUMP_TARGET
239 #if BTI_ENFORCED
240     bti j
241 #endif /* BTI_ENFORCED */
242 .endmacro
243 #endif /* XNU_KERNEL_PRIVATE */
244 #else /* NOT __ASSEMBLER__ */
245 
246 /* These defines are here for .c files that wish to reference global symbols
247  * within __asm__ statements.
248  */
249 #ifndef __NO_UNDERSCORES__
250 #define CC_SYM_PREFIX "_"
251 #else
252 #define CC_SYM_PREFIX ""
253 #endif /* __NO_UNDERSCORES__ */
254 #endif /* __ASSEMBLER__ */
255 
256 #ifdef __ASSEMBLER__
257 
258 # define BRANCH_EXTERN(x)	b	EXT(x)
259 
260 #endif /* __ASSEMBLER__ */
261 
262 #endif /* __arm64__ */
263 
264 #endif /* _ARM_ASM_H_ */
265