blob: 9172b55b5c9022a05fabb78b47cde275343465ea [file] [log] [blame]
Soby Mathew738b1fd2016-07-08 15:26:35 +01001/*
Alexei Fedorov9fc59632019-05-24 12:17:09 +01002 * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
Soby Mathew738b1fd2016-07-08 15:26:35 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew738b1fd2016-07-08 15:26:35 +01005 */
Antonio Nino Diazc3cf06f2018-11-08 10:20:19 +00006#ifndef ASM_MACROS_COMMON_S
7#define ASM_MACROS_COMMON_S
Soby Mathew738b1fd2016-07-08 15:26:35 +01008
Soby Mathew738b1fd2016-07-08 15:26:35 +01009 /*
Andre Przywarabdac6002025-04-15 13:24:40 +010010 * Provide a wrapper for the "bti" instructions using the more
11 * compatible "hint" encoding, otherwise older toolchains would reject
12 * this when not compiled for a BTI capable machine (-march=armv8.5-a).
13 */
14 .macro BTI _targets
15 .ifc \_targets, j
16 hint #36
17 .endif
18 .ifc \_targets, jc
19 hint #38
20 .endif
21 .endm
22
23 /*
Soby Mathew738b1fd2016-07-08 15:26:35 +010024 * This macro is used to create a function label and place the
25 * code into a separate text section based on the function name
Douglas Raillardb91d9352016-11-21 14:12:32 +000026 * to enable elimination of unused code during linking. It also adds
27 * basic debug information to enable call stack printing most of the
Julius Werner64726e62017-08-01 15:16:36 -070028 * time. The optional _align parameter can be used to force a
Masahiro Yamadafed18b32017-08-31 14:29:34 +090029 * non-standard alignment (indicated in powers of 2). The default is
30 * _align=2 because both Aarch32 and Aarch64 instructions must be
31 * word aligned. Do *not* try to use a raw .align directive. Since func
32 * switches to a new section, this would not have the desired effect.
Soby Mathew738b1fd2016-07-08 15:26:35 +010033 */
Masahiro Yamadafed18b32017-08-31 14:29:34 +090034 .macro func _name, _align=2
Douglas Raillardb91d9352016-11-21 14:12:32 +000035 /*
36 * Add Call Frame Information entry in the .debug_frame section for
37 * debugger consumption. This enables callstack printing in debuggers.
38 * This does not use any space in the final loaded binary, only in the
39 * ELF file.
40 * Note that a function manipulating the CFA pointer location (i.e. the
41 * x29 frame pointer on AArch64) should declare it using the
42 * appropriate .cfi* directives, or be prepared to have a degraded
43 * debugging experience.
44 */
45 .cfi_sections .debug_frame
Roberto Vargasd1f72922017-11-02 16:36:51 +000046 .section .text.asm.\_name, "ax"
Soby Mathew738b1fd2016-07-08 15:26:35 +010047 .type \_name, %function
Douglas Raillardb91d9352016-11-21 14:12:32 +000048 /*
49 * .cfi_startproc and .cfi_endproc are needed to output entries in
50 * .debug_frame
51 */
52 .cfi_startproc
Masahiro Yamadafed18b32017-08-31 14:29:34 +090053 .align \_align
Soby Mathew738b1fd2016-07-08 15:26:35 +010054 \_name:
Alexei Fedorov9fc59632019-05-24 12:17:09 +010055#if ENABLE_BTI
56 /* When Branch Target Identification is enabled, insert "bti jc"
57 * instruction to enable indirect calls and branches
58 */
Andre Przywarabdac6002025-04-15 13:24:40 +010059 BTI jc
Alexei Fedorov9fc59632019-05-24 12:17:09 +010060#endif
Soby Mathew738b1fd2016-07-08 15:26:35 +010061 .endm
62
63 /*
64 * This macro is used to mark the end of a function.
65 */
66 .macro endfunc _name
Douglas Raillardb91d9352016-11-21 14:12:32 +000067 .cfi_endproc
Soby Mathew738b1fd2016-07-08 15:26:35 +010068 .size \_name, . - \_name
69 .endm
70
71 /*
72 * Theses macros are used to create function labels for deprecated
73 * APIs. If ERROR_DEPRECATED is non zero, the callers of these APIs
74 * will fail to link and cause build failure.
75 */
76#if ERROR_DEPRECATED
77 .macro func_deprecated _name
78 func deprecated\_name
79 .endm
80
81 .macro endfunc_deprecated _name
82 endfunc deprecated\_name
83 .endm
84#else
85 .macro func_deprecated _name
86 func \_name
87 .endm
88
89 .macro endfunc_deprecated _name
90 endfunc \_name
91 .endm
92#endif
93
94 /*
95 * Helper assembler macro to count trailing zeros. The output is
96 * populated in the `TZ_COUNT` symbol.
97 */
98 .macro count_tz _value, _tz_count
99 .if \_value
100 count_tz "(\_value >> 1)", "(\_tz_count + 1)"
101 .else
102 .equ TZ_COUNT, (\_tz_count - 1)
103 .endif
104 .endm
105
106 /*
107 * This macro declares an array of 1 or more stacks, properly
108 * aligned and in the requested section
109 */
110#define DEFAULT_STACK_ALIGN (1 << 6) /* In case the caller doesnt provide alignment */
111
112 .macro declare_stack _name, _section, _size, _count, _align=DEFAULT_STACK_ALIGN
113 count_tz \_align, 0
114 .if (\_align - (1 << TZ_COUNT))
115 .error "Incorrect stack alignment specified (Must be a power of 2)."
116 .endif
117 .if ((\_size & ((1 << TZ_COUNT) - 1)) <> 0)
118 .error "Stack size not correctly aligned"
119 .endif
120 .section \_section, "aw", %nobits
121 .align TZ_COUNT
122 \_name:
123 .space ((\_count) * (\_size)), 0
124 .endm
125
126
Antonio Nino Diazc3cf06f2018-11-08 10:20:19 +0000127#endif /* ASM_MACROS_COMMON_S */