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