xref: /rk3399_ARM-atf/include/common/asm_macros_common.S (revision 9fc59639e649f614318f78ae2ca103fe102405ec)
1738b1fd7SSoby Mathew/*
2*9fc59639SAlexei Fedorov * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
3738b1fd7SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5738b1fd7SSoby Mathew */
6c3cf06f1SAntonio Nino Diaz#ifndef ASM_MACROS_COMMON_S
7c3cf06f1SAntonio Nino Diaz#define ASM_MACROS_COMMON_S
8738b1fd7SSoby Mathew
9738b1fd7SSoby Mathew	/*
10738b1fd7SSoby Mathew	 * This macro is used to create a function label and place the
11738b1fd7SSoby Mathew	 * code into a separate text section based on the function name
12b91d935fSDouglas Raillard	 * to enable elimination of unused code during linking. It also adds
13b91d935fSDouglas Raillard	 * basic debug information to enable call stack printing most of the
1464726e6dSJulius Werner	 * time. The optional _align parameter can be used to force a
15fed18b3aSMasahiro Yamada	 * non-standard alignment (indicated in powers of 2). The default is
16fed18b3aSMasahiro Yamada	 * _align=2 because both Aarch32 and Aarch64 instructions must be
17fed18b3aSMasahiro Yamada	 * word aligned. Do *not* try to use a raw .align directive. Since func
18fed18b3aSMasahiro Yamada	 * switches to a new section, this would not have the desired effect.
19738b1fd7SSoby Mathew	 */
20fed18b3aSMasahiro Yamada	.macro func _name, _align=2
21b91d935fSDouglas Raillard	/*
22b91d935fSDouglas Raillard	 * Add Call Frame Information entry in the .debug_frame section for
23b91d935fSDouglas Raillard	 * debugger consumption. This enables callstack printing in debuggers.
24b91d935fSDouglas Raillard	 * This does not use any space in the final loaded binary, only in the
25b91d935fSDouglas Raillard	 * ELF file.
26b91d935fSDouglas Raillard	 * Note that a function manipulating the CFA pointer location (i.e. the
27b91d935fSDouglas Raillard	 * x29 frame pointer on AArch64) should declare it using the
28b91d935fSDouglas Raillard	 * appropriate .cfi* directives, or be prepared to have a degraded
29b91d935fSDouglas Raillard	 * debugging experience.
30b91d935fSDouglas Raillard	 */
31b91d935fSDouglas Raillard	.cfi_sections .debug_frame
32d1f7292eSRoberto Vargas	.section .text.asm.\_name, "ax"
33738b1fd7SSoby Mathew	.type \_name, %function
34b91d935fSDouglas Raillard	/*
35b91d935fSDouglas Raillard	 * .cfi_startproc and .cfi_endproc are needed to output entries in
36b91d935fSDouglas Raillard	 * .debug_frame
37b91d935fSDouglas Raillard	 */
38b91d935fSDouglas Raillard	.cfi_startproc
3964726e6dSJulius Werner	.align \_align
40738b1fd7SSoby Mathew	\_name:
41*9fc59639SAlexei Fedorov#if ENABLE_BTI
42*9fc59639SAlexei Fedorov	/* When Branch Target Identification is enabled, insert "bti jc"
43*9fc59639SAlexei Fedorov	 * instruction to enable indirect calls and branches
44*9fc59639SAlexei Fedorov	 */
45*9fc59639SAlexei Fedorov	 bti	jc
46*9fc59639SAlexei Fedorov#endif
47738b1fd7SSoby Mathew	.endm
48738b1fd7SSoby Mathew
49738b1fd7SSoby Mathew	/*
50738b1fd7SSoby Mathew	 * This macro is used to mark the end of a function.
51738b1fd7SSoby Mathew	 */
52738b1fd7SSoby Mathew	.macro endfunc _name
53b91d935fSDouglas Raillard	.cfi_endproc
54738b1fd7SSoby Mathew	.size \_name, . - \_name
55738b1fd7SSoby Mathew	.endm
56738b1fd7SSoby Mathew
57738b1fd7SSoby Mathew	/*
58738b1fd7SSoby Mathew	 * Theses macros are used to create function labels for deprecated
59738b1fd7SSoby Mathew	 * APIs. If ERROR_DEPRECATED is non zero, the callers of these APIs
60738b1fd7SSoby Mathew	 * will fail to link and cause build failure.
61738b1fd7SSoby Mathew	 */
62738b1fd7SSoby Mathew#if ERROR_DEPRECATED
63738b1fd7SSoby Mathew	.macro func_deprecated _name
64738b1fd7SSoby Mathew	func deprecated\_name
65738b1fd7SSoby Mathew	.endm
66738b1fd7SSoby Mathew
67738b1fd7SSoby Mathew	.macro endfunc_deprecated _name
68738b1fd7SSoby Mathew	endfunc deprecated\_name
69738b1fd7SSoby Mathew	.endm
70738b1fd7SSoby Mathew#else
71738b1fd7SSoby Mathew	.macro func_deprecated _name
72738b1fd7SSoby Mathew	func \_name
73738b1fd7SSoby Mathew	.endm
74738b1fd7SSoby Mathew
75738b1fd7SSoby Mathew	.macro endfunc_deprecated _name
76738b1fd7SSoby Mathew	endfunc \_name
77738b1fd7SSoby Mathew	.endm
78738b1fd7SSoby Mathew#endif
79738b1fd7SSoby Mathew
80738b1fd7SSoby Mathew	/*
81738b1fd7SSoby Mathew	 * Helper assembler macro to count trailing zeros. The output is
82738b1fd7SSoby Mathew	 * populated in the `TZ_COUNT` symbol.
83738b1fd7SSoby Mathew	 */
84738b1fd7SSoby Mathew	.macro count_tz _value, _tz_count
85738b1fd7SSoby Mathew	.if \_value
86738b1fd7SSoby Mathew	  count_tz "(\_value >> 1)", "(\_tz_count + 1)"
87738b1fd7SSoby Mathew	.else
88738b1fd7SSoby Mathew	  .equ TZ_COUNT, (\_tz_count - 1)
89738b1fd7SSoby Mathew	.endif
90738b1fd7SSoby Mathew	.endm
91738b1fd7SSoby Mathew
92738b1fd7SSoby Mathew	/*
93738b1fd7SSoby Mathew	 * This macro declares an array of 1 or more stacks, properly
94738b1fd7SSoby Mathew	 * aligned and in the requested section
95738b1fd7SSoby Mathew	 */
96738b1fd7SSoby Mathew#define DEFAULT_STACK_ALIGN	(1 << 6)   /* In case the caller doesnt provide alignment */
97738b1fd7SSoby Mathew
98738b1fd7SSoby Mathew	.macro declare_stack _name, _section, _size, _count, _align=DEFAULT_STACK_ALIGN
99738b1fd7SSoby Mathew	count_tz \_align, 0
100738b1fd7SSoby Mathew	.if (\_align - (1 << TZ_COUNT))
101738b1fd7SSoby Mathew	  .error "Incorrect stack alignment specified (Must be a power of 2)."
102738b1fd7SSoby Mathew	.endif
103738b1fd7SSoby Mathew	.if ((\_size & ((1 << TZ_COUNT) - 1)) <> 0)
104738b1fd7SSoby Mathew	  .error "Stack size not correctly aligned"
105738b1fd7SSoby Mathew	.endif
106738b1fd7SSoby Mathew	.section    \_section, "aw", %nobits
107738b1fd7SSoby Mathew	.align TZ_COUNT
108738b1fd7SSoby Mathew	\_name:
109738b1fd7SSoby Mathew	.space ((\_count) * (\_size)), 0
110738b1fd7SSoby Mathew	.endm
111738b1fd7SSoby Mathew
112738b1fd7SSoby Mathew
113c3cf06f1SAntonio Nino Diaz#endif /* ASM_MACROS_COMMON_S */
114