arm-trusted-firmware/include/common/asm_macros_common.S

114 lines
3.1 KiB
ArmAsm
Raw Normal View History

/*
* Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef ASM_MACROS_COMMON_S
#define ASM_MACROS_COMMON_S
/*
* This macro is used to create a function label and place the
* code into a separate text section based on the function name
* to enable elimination of unused code during linking. It also adds
* basic debug information to enable call stack printing most of the
Add new alignment parameter to func assembler macro Assembler programmers are used to being able to define functions with a specific aligment with a pattern like this: .align X myfunction: However, this pattern is subtly broken when instead of a direct label like 'myfunction:', you use the 'func myfunction' macro that's standard in Trusted Firmware. Since the func macro declares a new section for the function, the .align directive written above it actually applies to the *previous* section in the assembly file, and the function it was supposed to apply to is linked with default alignment. An extreme case can be seen in Rockchip's plat_helpers.S which contains this code: [...] endfunc plat_crash_console_putc .align 16 func platform_cpu_warmboot [...] This assembles into the following plat_helpers.o: Sections: Idx Name Size [...] Algn 9 .text.plat_crash_console_putc 00010000 [...] 2**16 10 .text.platform_cpu_warmboot 00000080 [...] 2**3 As can be seen, the *previous* function actually got the alignment constraint, and it is also 64KB big even though it contains only two instructions, because the .align directive at the end of its section forces the assembler to insert a giant sled of NOPs. The function we actually wanted to align has the default constraint. This code only works at all because the linker just happens to put the two functions right behind each other when linking the final image, and since the end of plat_crash_console_putc is aligned the start of platform_cpu_warmboot will also be. But it still wastes almost 64KB of image space unnecessarily, and it will break under certain circumstances (e.g. if the plat_crash_console_putc function becomes unused and its section gets garbage-collected out). There's no real way to fix this with the existing func macro. Code like func myfunc .align X happens to do the right thing, but is still not really correct code (because the function label is inserted before the .align directive, so the assembler is technically allowed to insert padding at the beginning of the function which would then get executed as instructions if the function was called). Therefore, this patch adds a new parameter with a default value to the func macro that allows overriding its alignment. Also fix up all existing instances of this dangerous antipattern. Change-Id: I5696a07e2fde896f21e0e83644c95b7b6ac79a10 Signed-off-by: Julius Werner <jwerner@chromium.org>
2017-08-01 23:16:36 +01:00
* time. The optional _align parameter can be used to force a
asm_macros: set the default assembly code alignment to 4 byte Assembly routines are usually defined by using "func" and "endfunc": func foo ... endfunc foo Currently, the "func" macro does not specify ".align" directive by default. It causes unaligned instruction under some circumstances. As far as I tested, this problem happens for GCC 5 or older. It did not happen for GCC 6 or newer. Taking into account that GCC 4.x / 5.x is still used, make sure that assembly code is at least 4 byte aligned. [ How to reproduce the problem ] For example, use GCC 5.3 downloaded from Linaro: http://releases.linaro.org/components/toolchain/binaries/5.3-2016.05/ aarch64-linux-gnu/gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu.tar.xz Expand mbedtls-2.4.2 to the current directory. Try the following: $ git log --oneline -1 77544ef Merge pull request #1071 from jeenu-arm/syntax-fix $ aarch64-linux-gnu-gcc --version | head -1 aarch64-linux-gnu-gcc (Linaro GCC 5.3-2016.05) 5.3.1 20160412 $ make CROSS_COMPILE=aarch64-linux-gnu- PLAT=uniphier \ TRUSTED_BOARD_BOOT=1 MBEDTLS_DIR=mbedtls-2.4.2 ( snip build log ) $ aarch64-linux-gnu-nm build/uniphier/release/bl1/bl1.elf | grep handler 00000000800088f4 T bl1_fwu_smc_handler 00000000800084c8 T bl1_smc_handler 000000008000a6e0 t _panic_handler 000000008000a8e0 W plat_error_handler 000000008000a8e8 W plat_panic_handler 000000008000a8d8 W plat_reset_handler 000000008000a39f T reset_handler 000000008000a367 t smc_handler 000000008000a2ef t smc_handler64 You will notice "smc_handler64", "reset_handler", etc. are not properly aligned. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
2017-08-31 06:29:34 +01:00
* non-standard alignment (indicated in powers of 2). The default is
* _align=2 because both Aarch32 and Aarch64 instructions must be
* word aligned. Do *not* try to use a raw .align directive. Since func
* switches to a new section, this would not have the desired effect.
*/
asm_macros: set the default assembly code alignment to 4 byte Assembly routines are usually defined by using "func" and "endfunc": func foo ... endfunc foo Currently, the "func" macro does not specify ".align" directive by default. It causes unaligned instruction under some circumstances. As far as I tested, this problem happens for GCC 5 or older. It did not happen for GCC 6 or newer. Taking into account that GCC 4.x / 5.x is still used, make sure that assembly code is at least 4 byte aligned. [ How to reproduce the problem ] For example, use GCC 5.3 downloaded from Linaro: http://releases.linaro.org/components/toolchain/binaries/5.3-2016.05/ aarch64-linux-gnu/gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu.tar.xz Expand mbedtls-2.4.2 to the current directory. Try the following: $ git log --oneline -1 77544ef Merge pull request #1071 from jeenu-arm/syntax-fix $ aarch64-linux-gnu-gcc --version | head -1 aarch64-linux-gnu-gcc (Linaro GCC 5.3-2016.05) 5.3.1 20160412 $ make CROSS_COMPILE=aarch64-linux-gnu- PLAT=uniphier \ TRUSTED_BOARD_BOOT=1 MBEDTLS_DIR=mbedtls-2.4.2 ( snip build log ) $ aarch64-linux-gnu-nm build/uniphier/release/bl1/bl1.elf | grep handler 00000000800088f4 T bl1_fwu_smc_handler 00000000800084c8 T bl1_smc_handler 000000008000a6e0 t _panic_handler 000000008000a8e0 W plat_error_handler 000000008000a8e8 W plat_panic_handler 000000008000a8d8 W plat_reset_handler 000000008000a39f T reset_handler 000000008000a367 t smc_handler 000000008000a2ef t smc_handler64 You will notice "smc_handler64", "reset_handler", etc. are not properly aligned. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
2017-08-31 06:29:34 +01:00
.macro func _name, _align=2
/*
* Add Call Frame Information entry in the .debug_frame section for
* debugger consumption. This enables callstack printing in debuggers.
* This does not use any space in the final loaded binary, only in the
* ELF file.
* Note that a function manipulating the CFA pointer location (i.e. the
* x29 frame pointer on AArch64) should declare it using the
* appropriate .cfi* directives, or be prepared to have a degraded
* debugging experience.
*/
.cfi_sections .debug_frame
.section .text.asm.\_name, "ax"
.type \_name, %function
/*
* .cfi_startproc and .cfi_endproc are needed to output entries in
* .debug_frame
*/
.cfi_startproc
asm_macros: set the default assembly code alignment to 4 byte Assembly routines are usually defined by using "func" and "endfunc": func foo ... endfunc foo Currently, the "func" macro does not specify ".align" directive by default. It causes unaligned instruction under some circumstances. As far as I tested, this problem happens for GCC 5 or older. It did not happen for GCC 6 or newer. Taking into account that GCC 4.x / 5.x is still used, make sure that assembly code is at least 4 byte aligned. [ How to reproduce the problem ] For example, use GCC 5.3 downloaded from Linaro: http://releases.linaro.org/components/toolchain/binaries/5.3-2016.05/ aarch64-linux-gnu/gcc-linaro-5.3.1-2016.05-x86_64_aarch64-linux-gnu.tar.xz Expand mbedtls-2.4.2 to the current directory. Try the following: $ git log --oneline -1 77544ef Merge pull request #1071 from jeenu-arm/syntax-fix $ aarch64-linux-gnu-gcc --version | head -1 aarch64-linux-gnu-gcc (Linaro GCC 5.3-2016.05) 5.3.1 20160412 $ make CROSS_COMPILE=aarch64-linux-gnu- PLAT=uniphier \ TRUSTED_BOARD_BOOT=1 MBEDTLS_DIR=mbedtls-2.4.2 ( snip build log ) $ aarch64-linux-gnu-nm build/uniphier/release/bl1/bl1.elf | grep handler 00000000800088f4 T bl1_fwu_smc_handler 00000000800084c8 T bl1_smc_handler 000000008000a6e0 t _panic_handler 000000008000a8e0 W plat_error_handler 000000008000a8e8 W plat_panic_handler 000000008000a8d8 W plat_reset_handler 000000008000a39f T reset_handler 000000008000a367 t smc_handler 000000008000a2ef t smc_handler64 You will notice "smc_handler64", "reset_handler", etc. are not properly aligned. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
2017-08-31 06:29:34 +01:00
.align \_align
\_name:
#if ENABLE_BTI
/* When Branch Target Identification is enabled, insert "bti jc"
* instruction to enable indirect calls and branches
*/
bti jc
#endif
.endm
/*
* This macro is used to mark the end of a function.
*/
.macro endfunc _name
.cfi_endproc
.size \_name, . - \_name
.endm
/*
* Theses macros are used to create function labels for deprecated
* APIs. If ERROR_DEPRECATED is non zero, the callers of these APIs
* will fail to link and cause build failure.
*/
#if ERROR_DEPRECATED
.macro func_deprecated _name
func deprecated\_name
.endm
.macro endfunc_deprecated _name
endfunc deprecated\_name
.endm
#else
.macro func_deprecated _name
func \_name
.endm
.macro endfunc_deprecated _name
endfunc \_name
.endm
#endif
/*
* Helper assembler macro to count trailing zeros. The output is
* populated in the `TZ_COUNT` symbol.
*/
.macro count_tz _value, _tz_count
.if \_value
count_tz "(\_value >> 1)", "(\_tz_count + 1)"
.else
.equ TZ_COUNT, (\_tz_count - 1)
.endif
.endm
/*
* This macro declares an array of 1 or more stacks, properly
* aligned and in the requested section
*/
#define DEFAULT_STACK_ALIGN (1 << 6) /* In case the caller doesnt provide alignment */
.macro declare_stack _name, _section, _size, _count, _align=DEFAULT_STACK_ALIGN
count_tz \_align, 0
.if (\_align - (1 << TZ_COUNT))
.error "Incorrect stack alignment specified (Must be a power of 2)."
.endif
.if ((\_size & ((1 << TZ_COUNT) - 1)) <> 0)
.error "Stack size not correctly aligned"
.endif
.section \_section, "aw", %nobits
.align TZ_COUNT
\_name:
.space ((\_count) * (\_size)), 0
.endm
#endif /* ASM_MACROS_COMMON_S */