1738b1fd7SSoby Mathew/* 29fc59639SAlexei 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 /* 10*bdac600bSAndre Przywara * Provide a wrapper for the "bti" instructions using the more 11*bdac600bSAndre Przywara * compatible "hint" encoding, otherwise older toolchains would reject 12*bdac600bSAndre Przywara * this when not compiled for a BTI capable machine (-march=armv8.5-a). 13*bdac600bSAndre Przywara */ 14*bdac600bSAndre Przywara .macro BTI _targets 15*bdac600bSAndre Przywara .ifc \_targets, j 16*bdac600bSAndre Przywara hint #36 17*bdac600bSAndre Przywara .endif 18*bdac600bSAndre Przywara .ifc \_targets, jc 19*bdac600bSAndre Przywara hint #38 20*bdac600bSAndre Przywara .endif 21*bdac600bSAndre Przywara .endm 22*bdac600bSAndre Przywara 23*bdac600bSAndre Przywara /* 24738b1fd7SSoby Mathew * This macro is used to create a function label and place the 25738b1fd7SSoby Mathew * code into a separate text section based on the function name 26b91d935fSDouglas Raillard * to enable elimination of unused code during linking. It also adds 27b91d935fSDouglas Raillard * basic debug information to enable call stack printing most of the 2864726e6dSJulius Werner * time. The optional _align parameter can be used to force a 29fed18b3aSMasahiro Yamada * non-standard alignment (indicated in powers of 2). The default is 30fed18b3aSMasahiro Yamada * _align=2 because both Aarch32 and Aarch64 instructions must be 31fed18b3aSMasahiro Yamada * word aligned. Do *not* try to use a raw .align directive. Since func 32fed18b3aSMasahiro Yamada * switches to a new section, this would not have the desired effect. 33738b1fd7SSoby Mathew */ 34fed18b3aSMasahiro Yamada .macro func _name, _align=2 35b91d935fSDouglas Raillard /* 36b91d935fSDouglas Raillard * Add Call Frame Information entry in the .debug_frame section for 37b91d935fSDouglas Raillard * debugger consumption. This enables callstack printing in debuggers. 38b91d935fSDouglas Raillard * This does not use any space in the final loaded binary, only in the 39b91d935fSDouglas Raillard * ELF file. 40b91d935fSDouglas Raillard * Note that a function manipulating the CFA pointer location (i.e. the 41b91d935fSDouglas Raillard * x29 frame pointer on AArch64) should declare it using the 42b91d935fSDouglas Raillard * appropriate .cfi* directives, or be prepared to have a degraded 43b91d935fSDouglas Raillard * debugging experience. 44b91d935fSDouglas Raillard */ 45b91d935fSDouglas Raillard .cfi_sections .debug_frame 46d1f7292eSRoberto Vargas .section .text.asm.\_name, "ax" 47738b1fd7SSoby Mathew .type \_name, %function 48b91d935fSDouglas Raillard /* 49b91d935fSDouglas Raillard * .cfi_startproc and .cfi_endproc are needed to output entries in 50b91d935fSDouglas Raillard * .debug_frame 51b91d935fSDouglas Raillard */ 52b91d935fSDouglas Raillard .cfi_startproc 5364726e6dSJulius Werner .align \_align 54738b1fd7SSoby Mathew \_name: 559fc59639SAlexei Fedorov#if ENABLE_BTI 569fc59639SAlexei Fedorov /* When Branch Target Identification is enabled, insert "bti jc" 579fc59639SAlexei Fedorov * instruction to enable indirect calls and branches 589fc59639SAlexei Fedorov */ 59*bdac600bSAndre Przywara BTI jc 609fc59639SAlexei Fedorov#endif 61738b1fd7SSoby Mathew .endm 62738b1fd7SSoby Mathew 63738b1fd7SSoby Mathew /* 64738b1fd7SSoby Mathew * This macro is used to mark the end of a function. 65738b1fd7SSoby Mathew */ 66738b1fd7SSoby Mathew .macro endfunc _name 67b91d935fSDouglas Raillard .cfi_endproc 68738b1fd7SSoby Mathew .size \_name, . - \_name 69738b1fd7SSoby Mathew .endm 70738b1fd7SSoby Mathew 71738b1fd7SSoby Mathew /* 72738b1fd7SSoby Mathew * Theses macros are used to create function labels for deprecated 73738b1fd7SSoby Mathew * APIs. If ERROR_DEPRECATED is non zero, the callers of these APIs 74738b1fd7SSoby Mathew * will fail to link and cause build failure. 75738b1fd7SSoby Mathew */ 76738b1fd7SSoby Mathew#if ERROR_DEPRECATED 77738b1fd7SSoby Mathew .macro func_deprecated _name 78738b1fd7SSoby Mathew func deprecated\_name 79738b1fd7SSoby Mathew .endm 80738b1fd7SSoby Mathew 81738b1fd7SSoby Mathew .macro endfunc_deprecated _name 82738b1fd7SSoby Mathew endfunc deprecated\_name 83738b1fd7SSoby Mathew .endm 84738b1fd7SSoby Mathew#else 85738b1fd7SSoby Mathew .macro func_deprecated _name 86738b1fd7SSoby Mathew func \_name 87738b1fd7SSoby Mathew .endm 88738b1fd7SSoby Mathew 89738b1fd7SSoby Mathew .macro endfunc_deprecated _name 90738b1fd7SSoby Mathew endfunc \_name 91738b1fd7SSoby Mathew .endm 92738b1fd7SSoby Mathew#endif 93738b1fd7SSoby Mathew 94738b1fd7SSoby Mathew /* 95738b1fd7SSoby Mathew * Helper assembler macro to count trailing zeros. The output is 96738b1fd7SSoby Mathew * populated in the `TZ_COUNT` symbol. 97738b1fd7SSoby Mathew */ 98738b1fd7SSoby Mathew .macro count_tz _value, _tz_count 99738b1fd7SSoby Mathew .if \_value 100738b1fd7SSoby Mathew count_tz "(\_value >> 1)", "(\_tz_count + 1)" 101738b1fd7SSoby Mathew .else 102738b1fd7SSoby Mathew .equ TZ_COUNT, (\_tz_count - 1) 103738b1fd7SSoby Mathew .endif 104738b1fd7SSoby Mathew .endm 105738b1fd7SSoby Mathew 106738b1fd7SSoby Mathew /* 107738b1fd7SSoby Mathew * This macro declares an array of 1 or more stacks, properly 108738b1fd7SSoby Mathew * aligned and in the requested section 109738b1fd7SSoby Mathew */ 110738b1fd7SSoby Mathew#define DEFAULT_STACK_ALIGN (1 << 6) /* In case the caller doesnt provide alignment */ 111738b1fd7SSoby Mathew 112738b1fd7SSoby Mathew .macro declare_stack _name, _section, _size, _count, _align=DEFAULT_STACK_ALIGN 113738b1fd7SSoby Mathew count_tz \_align, 0 114738b1fd7SSoby Mathew .if (\_align - (1 << TZ_COUNT)) 115738b1fd7SSoby Mathew .error "Incorrect stack alignment specified (Must be a power of 2)." 116738b1fd7SSoby Mathew .endif 117738b1fd7SSoby Mathew .if ((\_size & ((1 << TZ_COUNT) - 1)) <> 0) 118738b1fd7SSoby Mathew .error "Stack size not correctly aligned" 119738b1fd7SSoby Mathew .endif 120738b1fd7SSoby Mathew .section \_section, "aw", %nobits 121738b1fd7SSoby Mathew .align TZ_COUNT 122738b1fd7SSoby Mathew \_name: 123738b1fd7SSoby Mathew .space ((\_count) * (\_size)), 0 124738b1fd7SSoby Mathew .endm 125738b1fd7SSoby Mathew 126738b1fd7SSoby Mathew 127c3cf06f1SAntonio Nino Diaz#endif /* ASM_MACROS_COMMON_S */ 128