xref: /arm-trusted-firmware/include/common/asm_macros_common.S (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu/*
2*91f16700Schasinglulu * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu *
4*91f16700Schasinglulu * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu */
6*91f16700Schasinglulu#ifndef ASM_MACROS_COMMON_S
7*91f16700Schasinglulu#define ASM_MACROS_COMMON_S
8*91f16700Schasinglulu
9*91f16700Schasinglulu	/*
10*91f16700Schasinglulu	 * This macro is used to create a function label and place the
11*91f16700Schasinglulu	 * code into a separate text section based on the function name
12*91f16700Schasinglulu	 * to enable elimination of unused code during linking. It also adds
13*91f16700Schasinglulu	 * basic debug information to enable call stack printing most of the
14*91f16700Schasinglulu	 * time. The optional _align parameter can be used to force a
15*91f16700Schasinglulu	 * non-standard alignment (indicated in powers of 2). The default is
16*91f16700Schasinglulu	 * _align=2 because both Aarch32 and Aarch64 instructions must be
17*91f16700Schasinglulu	 * word aligned. Do *not* try to use a raw .align directive. Since func
18*91f16700Schasinglulu	 * switches to a new section, this would not have the desired effect.
19*91f16700Schasinglulu	 */
20*91f16700Schasinglulu	.macro func _name, _align=2
21*91f16700Schasinglulu	/*
22*91f16700Schasinglulu	 * Add Call Frame Information entry in the .debug_frame section for
23*91f16700Schasinglulu	 * debugger consumption. This enables callstack printing in debuggers.
24*91f16700Schasinglulu	 * This does not use any space in the final loaded binary, only in the
25*91f16700Schasinglulu	 * ELF file.
26*91f16700Schasinglulu	 * Note that a function manipulating the CFA pointer location (i.e. the
27*91f16700Schasinglulu	 * x29 frame pointer on AArch64) should declare it using the
28*91f16700Schasinglulu	 * appropriate .cfi* directives, or be prepared to have a degraded
29*91f16700Schasinglulu	 * debugging experience.
30*91f16700Schasinglulu	 */
31*91f16700Schasinglulu	.cfi_sections .debug_frame
32*91f16700Schasinglulu	.section .text.asm.\_name, "ax"
33*91f16700Schasinglulu	.type \_name, %function
34*91f16700Schasinglulu	/*
35*91f16700Schasinglulu	 * .cfi_startproc and .cfi_endproc are needed to output entries in
36*91f16700Schasinglulu	 * .debug_frame
37*91f16700Schasinglulu	 */
38*91f16700Schasinglulu	.cfi_startproc
39*91f16700Schasinglulu	.align \_align
40*91f16700Schasinglulu	\_name:
41*91f16700Schasinglulu#if ENABLE_BTI
42*91f16700Schasinglulu	/* When Branch Target Identification is enabled, insert "bti jc"
43*91f16700Schasinglulu	 * instruction to enable indirect calls and branches
44*91f16700Schasinglulu	 */
45*91f16700Schasinglulu	 bti	jc
46*91f16700Schasinglulu#endif
47*91f16700Schasinglulu	.endm
48*91f16700Schasinglulu
49*91f16700Schasinglulu	/*
50*91f16700Schasinglulu	 * This macro is used to mark the end of a function.
51*91f16700Schasinglulu	 */
52*91f16700Schasinglulu	.macro endfunc _name
53*91f16700Schasinglulu	.cfi_endproc
54*91f16700Schasinglulu	.size \_name, . - \_name
55*91f16700Schasinglulu	.endm
56*91f16700Schasinglulu
57*91f16700Schasinglulu	/*
58*91f16700Schasinglulu	 * Theses macros are used to create function labels for deprecated
59*91f16700Schasinglulu	 * APIs. If ERROR_DEPRECATED is non zero, the callers of these APIs
60*91f16700Schasinglulu	 * will fail to link and cause build failure.
61*91f16700Schasinglulu	 */
62*91f16700Schasinglulu#if ERROR_DEPRECATED
63*91f16700Schasinglulu	.macro func_deprecated _name
64*91f16700Schasinglulu	func deprecated\_name
65*91f16700Schasinglulu	.endm
66*91f16700Schasinglulu
67*91f16700Schasinglulu	.macro endfunc_deprecated _name
68*91f16700Schasinglulu	endfunc deprecated\_name
69*91f16700Schasinglulu	.endm
70*91f16700Schasinglulu#else
71*91f16700Schasinglulu	.macro func_deprecated _name
72*91f16700Schasinglulu	func \_name
73*91f16700Schasinglulu	.endm
74*91f16700Schasinglulu
75*91f16700Schasinglulu	.macro endfunc_deprecated _name
76*91f16700Schasinglulu	endfunc \_name
77*91f16700Schasinglulu	.endm
78*91f16700Schasinglulu#endif
79*91f16700Schasinglulu
80*91f16700Schasinglulu	/*
81*91f16700Schasinglulu	 * Helper assembler macro to count trailing zeros. The output is
82*91f16700Schasinglulu	 * populated in the `TZ_COUNT` symbol.
83*91f16700Schasinglulu	 */
84*91f16700Schasinglulu	.macro count_tz _value, _tz_count
85*91f16700Schasinglulu	.if \_value
86*91f16700Schasinglulu	  count_tz "(\_value >> 1)", "(\_tz_count + 1)"
87*91f16700Schasinglulu	.else
88*91f16700Schasinglulu	  .equ TZ_COUNT, (\_tz_count - 1)
89*91f16700Schasinglulu	.endif
90*91f16700Schasinglulu	.endm
91*91f16700Schasinglulu
92*91f16700Schasinglulu	/*
93*91f16700Schasinglulu	 * This macro declares an array of 1 or more stacks, properly
94*91f16700Schasinglulu	 * aligned and in the requested section
95*91f16700Schasinglulu	 */
96*91f16700Schasinglulu#define DEFAULT_STACK_ALIGN	(1 << 6)   /* In case the caller doesnt provide alignment */
97*91f16700Schasinglulu
98*91f16700Schasinglulu	.macro declare_stack _name, _section, _size, _count, _align=DEFAULT_STACK_ALIGN
99*91f16700Schasinglulu	count_tz \_align, 0
100*91f16700Schasinglulu	.if (\_align - (1 << TZ_COUNT))
101*91f16700Schasinglulu	  .error "Incorrect stack alignment specified (Must be a power of 2)."
102*91f16700Schasinglulu	.endif
103*91f16700Schasinglulu	.if ((\_size & ((1 << TZ_COUNT) - 1)) <> 0)
104*91f16700Schasinglulu	  .error "Stack size not correctly aligned"
105*91f16700Schasinglulu	.endif
106*91f16700Schasinglulu	.section    \_section, "aw", %nobits
107*91f16700Schasinglulu	.align TZ_COUNT
108*91f16700Schasinglulu	\_name:
109*91f16700Schasinglulu	.space ((\_count) * (\_size)), 0
110*91f16700Schasinglulu	.endm
111*91f16700Schasinglulu
112*91f16700Schasinglulu
113*91f16700Schasinglulu#endif /* ASM_MACROS_COMMON_S */
114