blob: 9357e3f791f0b4136b0059deb569b04d7662f524 [file] [log] [blame]
xiaohu.huang89700ad2022-11-06 23:57:08 +08001/*
2 * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#ifndef _ASMDEFS_H
8#define _ASMDEFS_H
9
10#if defined (__arm__)
11
12#define ARM_FNSTART .fnstart
13#if defined (IS_LEAF)
14#define ARM_FNEND \
15 .cantunwind \
16 .fnend
17#else
18#define ARM_FNEND .fnend
19# endif
20#else
21#define ARM_FNSTART
22#define ARM_FNEND
23
24/* Check whether leaf function PAC signing has been requested in the
25 -mbranch-protect compile-time option. */
26#define LEAF_PROTECT_BIT 2
27
28#ifdef __ARM_FEATURE_PAC_DEFAULT
29# define HAVE_PAC_LEAF \
30 ((__ARM_FEATURE_PAC_DEFAULT & (1 << LEAF_PROTECT_BIT)) && 1)
31#else
32# define HAVE_PAC_LEAF 0
33#endif
34
35/* Provide default parameters for PAC-code handling in leaf-functions. */
36#if HAVE_PAC_LEAF
37# ifndef PAC_LEAF_PUSH_IP
38# define PAC_LEAF_PUSH_IP 1
39# endif
40#else /* !HAVE_PAC_LEAF */
41# undef PAC_LEAF_PUSH_IP
42# define PAC_LEAF_PUSH_IP 0
43#endif /* HAVE_PAC_LEAF */
44
45#define STACK_ALIGN_ENFORCE 0
46
47/******************************************************************************
48* Implementation of the prologue and epilogue assembler macros and their
49* associated helper functions.
50*
51* These functions add support for the following:
52*
53* - M-profile branch target identification (BTI) landing-pads when compiled
54* with `-mbranch-protection=bti'.
55* - PAC-signing and verification instructions, depending on hardware support
56* and whether the PAC-signing of leaf functions has been requested via the
57* `-mbranch-protection=pac-ret+leaf' compiler argument.
58* - 8-byte stack alignment preservation at function entry, defaulting to the
59* value of STACK_ALIGN_ENFORCE.
60*
61* Notes:
62* - Prologue stack alignment is implemented by detecting a push with an odd
63* number of registers and prepending a dummy register to the list.
64* - If alignment is attempted on a list containing r0, compilation will result
65* in an error.
66* - If alignment is attempted in a list containing r1, r0 will be prepended to
67* the register list and r0 will be restored prior to function return. for
68* functions with non-void return types, this will result in the corruption of
69* the result register.
70* - Stack alignment is enforced via the following helper macro call-chain:
71*
72* {prologue|epilogue} ->_align8 -> _preprocess_reglist ->
73* _preprocess_reglist1 -> {_prologue|_epilogue}
74*
75* - Debug CFI directives are automatically added to prologues and epilogues,
76* assisted by `cfisavelist' and `cfirestorelist', respectively.
77*
78* Arguments:
79* prologue
80* --------
81* - first - If `last' specified, this serves as start of general-purpose
82* register (GPR) range to push onto stack, otherwise represents
83* single GPR to push onto stack. If omitted, no GPRs pushed
84* onto stack at prologue.
85* - last - If given, specifies inclusive upper-bound of GPR range.
86* - push_ip - Determines whether IP register is to be pushed to stack at
87* prologue. When pac-signing is requested, this holds the
88* the pac-key. Either 1 or 0 to push or not push, respectively.
89* Default behavior: Set to value of PAC_LEAF_PUSH_IP macro.
90* - push_lr - Determines whether to push lr to the stack on function entry.
91* Either 1 or 0 to push or not push, respectively.
92* - align8 - Whether to enforce alignment. Either 1 or 0, with 1 requesting
93* alignment.
94*
95* epilogue
96* --------
97* The epilogue should be called passing the same arguments as those passed to
98* the prologue to ensure the stack is not corrupted on function return.
99*
100* Usage examples:
101*
102* prologue push_ip=1 -> push {ip}
103* epilogue push_ip=1, align8=1 -> pop {r2, ip}
104* prologue push_ip=1, push_lr=1 -> push {ip, lr}
105* epilogue 1 -> pop {r1}
106* prologue 1, align8=1 -> push {r0, r1}
107* epilogue 1, push_ip=1 -> pop {r1, ip}
108* prologue 1, 4 -> push {r1-r4}
109* epilogue 1, 4 push_ip=1 -> pop {r1-r4, ip}
110*
111******************************************************************************/
112
113/* Emit .cfi_restore directives for a consecutive sequence of registers. */
114 .macro cfirestorelist first, last
115 .cfi_restore \last
116 .if \last-\first
117 cfirestorelist \first, \last-1
118 .endif
119 .endm
120
121/* Emit .cfi_offset directives for a consecutive sequence of registers. */
122 .macro cfisavelist first, last, index=1
123 .cfi_offset \last, -4*(\index)
124 .if \last-\first
125 cfisavelist \first, \last-1, \index+1
126 .endif
127 .endm
128
129.macro _prologue first=-1, last=-1, push_ip=PAC_LEAF_PUSH_IP, push_lr=0
130 .if \push_ip & 1 != \push_ip
131 .error "push_ip may be either 0 or 1"
132 .endif
133 .if \push_lr & 1 != \push_lr
134 .error "push_lr may be either 0 or 1"
135 .endif
136 .if \first != -1
137 .if \last == -1
138 /* Upper-bound not provided: Set upper = lower. */
139 _prologue \first, \first, \push_ip, \push_lr
140 .exitm
141 .endif
142 .endif
143#if HAVE_PAC_LEAF
144#if __ARM_FEATURE_BTI_DEFAULT
145 pacbti ip, lr, sp
146#else
147 pac ip, lr, sp
148#endif /* __ARM_FEATURE_BTI_DEFAULT */
149 .cfi_register 143, 12
150#else
151#if __ARM_FEATURE_BTI_DEFAULT
152 bti
153#endif /* __ARM_FEATURE_BTI_DEFAULT */
154#endif /* HAVE_PAC_LEAF */
155 .if \first != -1
156 .if \last != \first
157 .if \last >= 13
158 .error "SP cannot be in the save list"
159 .endif
160 .if \push_ip
161 .if \push_lr
162 /* Case 1: push register range, ip and lr registers. */
163 push {r\first-r\last, ip, lr}
164 .cfi_adjust_cfa_offset ((\last-\first)+3)*4
165 .cfi_offset 14, -4
166 .cfi_offset 143, -8
167 cfisavelist \first, \last, 3
168 .else // !\push_lr
169 /* Case 2: push register range and ip register. */
170 push {r\first-r\last, ip}
171 .cfi_adjust_cfa_offset ((\last-\first)+2)*4
172 .cfi_offset 143, -4
173 cfisavelist \first, \last, 2
174 .endif
175 .else // !\push_ip
176 .if \push_lr
177 /* Case 3: push register range and lr register. */
178 push {r\first-r\last, lr}
179 .cfi_adjust_cfa_offset ((\last-\first)+2)*4
180 .cfi_offset 14, -4
181 cfisavelist \first, \last, 2
182 .else // !\push_lr
183 /* Case 4: push register range. */
184 push {r\first-r\last}
185 .cfi_adjust_cfa_offset ((\last-\first)+1)*4
186 cfisavelist \first, \last, 1
187 .endif
188 .endif
189 .else // \last == \first
190 .if \push_ip
191 .if \push_lr
192 /* Case 5: push single GP register plus ip and lr registers. */
193 push {r\first, ip, lr}
194 .cfi_adjust_cfa_offset 12
195 .cfi_offset 14, -4
196 .cfi_offset 143, -8
197 cfisavelist \first, \first, 3
198 .else // !\push_lr
199 /* Case 6: push single GP register plus ip register. */
200 push {r\first, ip}
201 .cfi_adjust_cfa_offset 8
202 .cfi_offset 143, -4
203 cfisavelist \first, \first, 2
204 .endif
205 .else // !\push_ip
206 .if \push_lr
207 /* Case 7: push single GP register plus lr register. */
208 push {r\first, lr}
209 .cfi_adjust_cfa_offset 8
210 .cfi_offset 14, -4
211 cfisavelist \first, \first, 2
212 .else // !\push_lr
213 /* Case 8: push single GP register. */
214 push {r\first}
215 .cfi_adjust_cfa_offset 4
216 cfisavelist \first, \first, 1
217 .endif
218 .endif
219 .endif
220 .else // \first == -1
221 .if \push_ip
222 .if \push_lr
223 /* Case 9: push ip and lr registers. */
224 push {ip, lr}
225 .cfi_adjust_cfa_offset 8
226 .cfi_offset 14, -4
227 .cfi_offset 143, -8
228 .else // !\push_lr
229 /* Case 10: push ip register. */
230 push {ip}
231 .cfi_adjust_cfa_offset 4
232 .cfi_offset 143, -4
233 .endif
234 .else // !\push_ip
235 .if \push_lr
236 /* Case 11: push lr register. */
237 push {lr}
238 .cfi_adjust_cfa_offset 4
239 .cfi_offset 14, -4
240 .endif
241 .endif
242 .endif
243.endm
244
245.macro _epilogue first=-1, last=-1, push_ip=PAC_LEAF_PUSH_IP, push_lr=0
246 .if \push_ip & 1 != \push_ip
247 .error "push_ip may be either 0 or 1"
248 .endif
249 .if \push_lr & 1 != \push_lr
250 .error "push_lr may be either 0 or 1"
251 .endif
252 .if \first != -1
253 .if \last == -1
254 /* Upper-bound not provided: Set upper = lower. */
255 _epilogue \first, \first, \push_ip, \push_lr
256 .exitm
257 .endif
258 .if \last != \first
259 .if \last >= 13
260 .error "SP cannot be in the save list"
261 .endif
262 .if \push_ip
263 .if \push_lr
264 /* Case 1: pop register range, ip and lr registers. */
265 pop {r\first-r\last, ip, lr}
266 .cfi_restore 14
267 .cfi_register 143, 12
268 cfirestorelist \first, \last
269 .else // !\push_lr
270 /* Case 2: pop register range and ip register. */
271 pop {r\first-r\last, ip}
272 .cfi_register 143, 12
273 cfirestorelist \first, \last
274 .endif
275 .else // !\push_ip
276 .if \push_lr
277 /* Case 3: pop register range and lr register. */
278 pop {r\first-r\last, lr}
279 .cfi_restore 14
280 cfirestorelist \first, \last
281 .else // !\push_lr
282 /* Case 4: pop register range. */
283 pop {r\first-r\last}
284 cfirestorelist \first, \last
285 .endif
286 .endif
287 .else // \last == \first
288 .if \push_ip
289 .if \push_lr
290 /* Case 5: pop single GP register plus ip and lr registers. */
291 pop {r\first, ip, lr}
292 .cfi_restore 14
293 .cfi_register 143, 12
294 cfirestorelist \first, \first
295 .else // !\push_lr
296 /* Case 6: pop single GP register plus ip register. */
297 pop {r\first, ip}
298 .cfi_register 143, 12
299 cfirestorelist \first, \first
300 .endif
301 .else // !\push_ip
302 .if \push_lr
303 /* Case 7: pop single GP register plus lr register. */
304 pop {r\first, lr}
305 .cfi_restore 14
306 cfirestorelist \first, \first
307 .else // !\push_lr
308 /* Case 8: pop single GP register. */
309 pop {r\first}
310 cfirestorelist \first, \first
311 .endif
312 .endif
313 .endif
314 .else // \first == -1
315 .if \push_ip
316 .if \push_lr
317 /* Case 9: pop ip and lr registers. */
318 pop {ip, lr}
319 .cfi_restore 14
320 .cfi_register 143, 12
321 .else // !\push_lr
322 /* Case 10: pop ip register. */
323 pop {ip}
324 .cfi_register 143, 12
325 .endif
326 .else // !\push_ip
327 .if \push_lr
328 /* Case 11: pop lr register. */
329 pop {lr}
330 .cfi_restore 14
331 .endif
332 .endif
333 .endif
334#if HAVE_PAC_LEAF
335 aut ip, lr, sp
336#endif /* HAVE_PAC_LEAF */
337 bx lr
338.endm
339
340# clean up expressions in 'last'
341.macro _preprocess_reglist1 first:req, last:req, push_ip:req, push_lr:req, reglist_op:req
342 .if \last == 0
343 \reglist_op \first, 0, \push_ip, \push_lr
344 .elseif \last == 1
345 \reglist_op \first, 1, \push_ip, \push_lr
346 .elseif \last == 2
347 \reglist_op \first, 2, \push_ip, \push_lr
348 .elseif \last == 3
349 \reglist_op \first, 3, \push_ip, \push_lr
350 .elseif \last == 4
351 \reglist_op \first, 4, \push_ip, \push_lr
352 .elseif \last == 5
353 \reglist_op \first, 5, \push_ip, \push_lr
354 .elseif \last == 6
355 \reglist_op \first, 6, \push_ip, \push_lr
356 .elseif \last == 7
357 \reglist_op \first, 7, \push_ip, \push_lr
358 .elseif \last == 8
359 \reglist_op \first, 8, \push_ip, \push_lr
360 .elseif \last == 9
361 \reglist_op \first, 9, \push_ip, \push_lr
362 .elseif \last == 10
363 \reglist_op \first, 10, \push_ip, \push_lr
364 .elseif \last == 11
365 \reglist_op \first, 11, \push_ip, \push_lr
366 .else
367 .error "last (\last) out of range"
368 .endif
369.endm
370
371# clean up expressions in 'first'
372.macro _preprocess_reglist first:req, last, push_ip=0, push_lr=0, reglist_op:req
373 .ifb \last
374 _preprocess_reglist \first \first \push_ip \push_lr
375 .else
376 .if \first > \last
377 .error "last (\last) must be at least as great as first (\first)"
378 .endif
379 .if \first == 0
380 _preprocess_reglist1 0, \last, \push_ip, \push_lr, \reglist_op
381 .elseif \first == 1
382 _preprocess_reglist1 1, \last, \push_ip, \push_lr, \reglist_op
383 .elseif \first == 2
384 _preprocess_reglist1 2, \last, \push_ip, \push_lr, \reglist_op
385 .elseif \first == 3
386 _preprocess_reglist1 3, \last, \push_ip, \push_lr, \reglist_op
387 .elseif \first == 4
388 _preprocess_reglist1 4, \last, \push_ip, \push_lr, \reglist_op
389 .elseif \first == 5
390 _preprocess_reglist1 5, \last, \push_ip, \push_lr, \reglist_op
391 .elseif \first == 6
392 _preprocess_reglist1 6, \last, \push_ip, \push_lr, \reglist_op
393 .elseif \first == 7
394 _preprocess_reglist1 7, \last, \push_ip, \push_lr, \reglist_op
395 .elseif \first == 8
396 _preprocess_reglist1 8, \last, \push_ip, \push_lr, \reglist_op
397 .elseif \first == 9
398 _preprocess_reglist1 9, \last, \push_ip, \push_lr, \reglist_op
399 .elseif \first == 10
400 _preprocess_reglist1 10, \last, \push_ip, \push_lr, \reglist_op
401 .elseif \first == 11
402 _preprocess_reglist1 11, \last, \push_ip, \push_lr, \reglist_op
403 .else
404 .error "first (\first) out of range"
405 .endif
406 .endif
407.endm
408
409.macro _align8 first, last, push_ip=0, push_lr=0, reglist_op=_prologue
410 .ifb \first
411 .ifnb \last
412 .error "can't have last (\last) without specifying first"
413 .else // \last not blank
414 .if ((\push_ip + \push_lr) % 2) == 0
415 \reglist_op first=-1, last=-1, push_ip=\push_ip, push_lr=\push_lr
416 .exitm
417 .else // ((\push_ip + \push_lr) % 2) odd
418 _align8 2, 2, \push_ip, \push_lr, \reglist_op
419 .exitm
420 .endif // ((\push_ip + \push_lr) % 2) == 0
421 .endif // .ifnb \last
422 .endif // .ifb \first
423
424 .ifb \last
425 _align8 \first, \first, \push_ip, \push_lr, \reglist_op
426 .else
427 .if \push_ip & 1 <> \push_ip
428 .error "push_ip may be 0 or 1"
429 .endif
430 .if \push_lr & 1 <> \push_lr
431 .error "push_lr may be 0 or 1"
432 .endif
433 .ifeq (\last - \first + \push_ip + \push_lr) % 2
434 .if \first == 0
435 .error "Alignment required and first register is r0"
436 .exitm
437 .endif
438 _preprocess_reglist \first-1, \last, \push_ip, \push_lr, \reglist_op
439 .else
440 _preprocess_reglist \first \last, \push_ip, \push_lr, \reglist_op
441 .endif
442 .endif
443.endm
444
445.macro prologue first, last, push_ip=PAC_LEAF_PUSH_IP, push_lr=0, align8=STACK_ALIGN_ENFORCE
446 .if \align8
447 _align8 \first, \last, \push_ip, \push_lr, _prologue
448 .else
449 _prologue first=\first, last=\last, push_ip=\push_ip, push_lr=\push_lr
450 .endif
451.endm
452
453.macro epilogue first, last, push_ip=PAC_LEAF_PUSH_IP, push_lr=0, align8=STACK_ALIGN_ENFORCE
454 .if \align8
455 _align8 \first, \last, \push_ip, \push_lr, reglist_op=_epilogue
456 .else
457 _epilogue first=\first, last=\last, push_ip=\push_ip, push_lr=\push_lr
458 .endif
459.endm
460
461#endif
462
463#if defined(__aarch64__)
464
465/* Branch Target Identitication support. */
466#define BTI_C hint 34
467#define BTI_J hint 36
468/* Return address signing support (pac-ret). */
469#define PACIASP hint 25; .cfi_window_save
470#define AUTIASP hint 29; .cfi_window_save
471
472/* GNU_PROPERTY_AARCH64_* macros from elf.h. */
473#define FEATURE_1_AND 0xc0000000
474#define FEATURE_1_BTI 1
475#define FEATURE_1_PAC 2
476
477/* Add a NT_GNU_PROPERTY_TYPE_0 note. */
478#define GNU_PROPERTY(type, value) \
479 .section .note.gnu.property, "a"; \
480 .p2align 3; \
481 .word 4; \
482 .word 16; \
483 .word 5; \
484 .asciz "GNU"; \
485 .word type; \
486 .word 4; \
487 .word value; \
488 .word 0; \
489 .text
490
491/* If set then the GNU Property Note section will be added to
492 mark objects to support BTI and PAC-RET. */
493#ifndef WANT_GNU_PROPERTY
494#define WANT_GNU_PROPERTY 1
495#endif
496
497#if WANT_GNU_PROPERTY
498/* Add property note with supported features to all asm files. */
499GNU_PROPERTY (FEATURE_1_AND, FEATURE_1_BTI|FEATURE_1_PAC)
500#endif
501
502#define ENTRY_ALIGN(name, alignment) \
503 .global name; \
504 .type name,%function; \
505 .align alignment; \
506 name: \
507 ARM_FNSTART; \
508 .cfi_startproc; \
509 BTI_C;
510
511#else
512
513#define END_FILE
514
515#define ENTRY_ALIGN(name, alignment) \
516 .global name; \
517 .type name,%function; \
518 .align alignment; \
519 name: \
520 ARM_FNSTART; \
521 .cfi_startproc;
522
523#endif
524
525#define ENTRY(name) ENTRY_ALIGN(name, 6)
526
527#define ENTRY_ALIAS(name) \
528 .global name; \
529 .type name,%function; \
530 name:
531
532#define END(name) \
533 .cfi_endproc; \
534 ARM_FNEND; \
535 .size name, .-name;
536
537#define L(l) .L ## l
538
539#ifdef __ILP32__
540 /* Sanitize padding bits of pointer arguments as per aapcs64 */
541#define PTR_ARG(n) mov w##n, w##n
542#else
543#define PTR_ARG(n)
544#endif
545
546#ifdef __ILP32__
547 /* Sanitize padding bits of size arguments as per aapcs64 */
548#define SIZE_ARG(n) mov w##n, w##n
549#else
550#define SIZE_ARG(n)
551#endif
552
553#endif