Daniel Schwierzeck | e4ccb47 | 2020-07-12 01:46:18 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Copyright (C) 2004, 2007 Maciej W. Rozycki |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General Public |
| 6 | * License. See the file "COPYING" in the main directory of this archive |
| 7 | * for more details. |
| 8 | */ |
| 9 | #ifndef _ASM_COMPILER_H |
| 10 | #define _ASM_COMPILER_H |
| 11 | |
| 12 | /* |
| 13 | * With GCC 4.5 onwards we can use __builtin_unreachable to indicate to the |
| 14 | * compiler that a particular code path will never be hit. This allows it to be |
| 15 | * optimised out of the generated binary. |
| 16 | * |
| 17 | * Unfortunately at least GCC 4.6.3 through 7.3.0 inclusive suffer from a bug |
| 18 | * that can lead to instructions from beyond an unreachable statement being |
| 19 | * incorrectly reordered into earlier delay slots if the unreachable statement |
| 20 | * is the only content of a case in a switch statement. This can lead to |
| 21 | * seemingly random behaviour, such as invalid memory accesses from incorrectly |
| 22 | * reordered loads or stores. See this potential GCC fix for details: |
| 23 | * |
| 24 | * https://gcc.gnu.org/ml/gcc-patches/2015-09/msg00360.html |
| 25 | * |
| 26 | * It is unclear whether GCC 8 onwards suffer from the same issue - nothing |
| 27 | * relevant is mentioned in GCC 8 release notes and nothing obviously relevant |
| 28 | * stands out in GCC commit logs, but these newer GCC versions generate very |
| 29 | * different code for the testcase which doesn't exhibit the bug. |
| 30 | * |
| 31 | * GCC also handles stack allocation suboptimally when calling noreturn |
| 32 | * functions or calling __builtin_unreachable(): |
| 33 | * |
| 34 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82365 |
| 35 | * |
| 36 | * We work around both of these issues by placing a volatile asm statement, |
| 37 | * which GCC is prevented from reordering past, prior to __builtin_unreachable |
| 38 | * calls. |
| 39 | * |
| 40 | * The .insn statement is required to ensure that any branches to the |
| 41 | * statement, which sadly must be kept due to the asm statement, are known to |
| 42 | * be branches to code and satisfy linker requirements for microMIPS kernels. |
| 43 | */ |
| 44 | #undef barrier_before_unreachable |
| 45 | #define barrier_before_unreachable() asm volatile(".insn") |
| 46 | |
| 47 | #if !defined(CONFIG_CC_IS_GCC) || \ |
| 48 | (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) |
| 49 | # define GCC_OFF_SMALL_ASM() "ZC" |
| 50 | #elif defined(CONFIG_CPU_MICROMIPS) |
| 51 | # error "microMIPS compilation unsupported with GCC older than 4.9" |
| 52 | #else |
| 53 | # define GCC_OFF_SMALL_ASM() "R" |
| 54 | #endif |
| 55 | |
| 56 | #ifdef CONFIG_CPU_MIPSR6 |
| 57 | #define MIPS_ISA_LEVEL "mips64r6" |
| 58 | #define MIPS_ISA_ARCH_LEVEL MIPS_ISA_LEVEL |
| 59 | #define MIPS_ISA_LEVEL_RAW mips64r6 |
| 60 | #define MIPS_ISA_ARCH_LEVEL_RAW MIPS_ISA_LEVEL_RAW |
| 61 | #else |
| 62 | /* MIPS64 is a superset of MIPS32 */ |
| 63 | #define MIPS_ISA_LEVEL "mips64r2" |
| 64 | #define MIPS_ISA_ARCH_LEVEL "arch=r4000" |
| 65 | #define MIPS_ISA_LEVEL_RAW mips64r2 |
| 66 | #define MIPS_ISA_ARCH_LEVEL_RAW MIPS_ISA_LEVEL_RAW |
| 67 | #endif /* CONFIG_CPU_MIPSR6 */ |
| 68 | |
| 69 | #endif /* _ASM_COMPILER_H */ |