| 1 |
/* |
| 2 |
* Copyright © 2018, VideoLAN and dav1d authors |
| 3 |
* Copyright © 2018, Janne Grunau |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright notice, this |
| 10 |
* list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 |
* this list of conditions and the following disclaimer in the documentation |
| 14 |
* and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 20 |
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef DAV1D_SRC_ARM_ASM_S |
| 29 |
#define DAV1D_SRC_ARM_ASM_S |
| 30 |
|
| 31 |
#include "config.h" |
| 32 |
|
| 33 |
#if ARCH_AARCH64 |
| 34 |
#define x18 do_not_use_x18 |
| 35 |
#define w18 do_not_use_w18 |
| 36 |
|
| 37 |
/* Support macros for |
| 38 |
* - Armv8.3-A Pointer Authentication and |
| 39 |
* - Armv8.5-A Branch Target Identification |
| 40 |
* features which require emitting a .note.gnu.property section with the |
| 41 |
* appropriate architecture-dependent feature bits set. |
| 42 |
* |
| 43 |
* |AARCH64_SIGN_LINK_REGISTER| and |AARCH64_VALIDATE_LINK_REGISTER| expand to |
| 44 |
* PACIxSP and AUTIxSP, respectively. |AARCH64_SIGN_LINK_REGISTER| should be |
| 45 |
* used immediately before saving the LR register (x30) to the stack. |
| 46 |
* |AARCH64_VALIDATE_LINK_REGISTER| should be used immediately after restoring |
| 47 |
* it. Note |AARCH64_SIGN_LINK_REGISTER|'s modifications to LR must be undone |
| 48 |
* with |AARCH64_VALIDATE_LINK_REGISTER| before RET. The SP register must also |
| 49 |
* have the same value at the two points. For example: |
| 50 |
* |
| 51 |
* .global f |
| 52 |
* f: |
| 53 |
* AARCH64_SIGN_LINK_REGISTER |
| 54 |
* stp x29, x30, [sp, #-96]! |
| 55 |
* mov x29, sp |
| 56 |
* ... |
| 57 |
* ldp x29, x30, [sp], #96 |
| 58 |
* AARCH64_VALIDATE_LINK_REGISTER |
| 59 |
* ret |
| 60 |
* |
| 61 |
* |AARCH64_VALID_CALL_TARGET| expands to BTI 'c'. Either it, or |
| 62 |
* |AARCH64_SIGN_LINK_REGISTER|, must be used at every point that may be an |
| 63 |
* indirect call target. In particular, all symbols exported from a file must |
| 64 |
* begin with one of these macros. For example, a leaf function that does not |
| 65 |
* save LR can instead use |AARCH64_VALID_CALL_TARGET|: |
| 66 |
* |
| 67 |
* .globl return_zero |
| 68 |
* return_zero: |
| 69 |
* AARCH64_VALID_CALL_TARGET |
| 70 |
* mov x0, #0 |
| 71 |
* ret |
| 72 |
* |
| 73 |
* A non-leaf function which does not immediately save LR may need both macros |
| 74 |
* because |AARCH64_SIGN_LINK_REGISTER| appears late. For example, the function |
| 75 |
* may jump to an alternate implementation before setting up the stack: |
| 76 |
* |
| 77 |
* .globl with_early_jump |
| 78 |
* with_early_jump: |
| 79 |
* AARCH64_VALID_CALL_TARGET |
| 80 |
* cmp x0, #128 |
| 81 |
* b.lt .Lwith_early_jump_128 |
| 82 |
* AARCH64_SIGN_LINK_REGISTER |
| 83 |
* stp x29, x30, [sp, #-96]! |
| 84 |
* mov x29, sp |
| 85 |
* ... |
| 86 |
* ldp x29, x30, [sp], #96 |
| 87 |
* AARCH64_VALIDATE_LINK_REGISTER |
| 88 |
* ret |
| 89 |
* |
| 90 |
* .Lwith_early_jump_128: |
| 91 |
* ... |
| 92 |
* ret |
| 93 |
* |
| 94 |
* These annotations are only required with indirect calls. Private symbols that |
| 95 |
* are only the target of direct calls do not require annotations. Also note |
| 96 |
* that |AARCH64_VALID_CALL_TARGET| is only valid for indirect calls (BLR), not |
| 97 |
* indirect jumps (BR). Indirect jumps in assembly are supported through |
| 98 |
* |AARCH64_VALID_JUMP_TARGET|. Landing Pads which shall serve for jumps and |
| 99 |
* calls can be created using |AARCH64_VALID_JUMP_CALL_TARGET|. |
| 100 |
* |
| 101 |
* Although not necessary, it is safe to use these macros in 32-bit ARM |
| 102 |
* assembly. This may be used to simplify dual 32-bit and 64-bit files. |
| 103 |
* |
| 104 |
* References: |
| 105 |
* - "ELF for the Arm® 64-bit Architecture" |
| 106 |
* https: *github.com/ARM-software/abi-aa/blob/master/aaelf64/aaelf64.rst |
| 107 |
* - "Providing protection for complex software" |
| 108 |
* https://developer.arm.com/architectures/learn-the-architecture/providing-protection-for-complex-software |
| 109 |
*/ |
| 110 |
#if defined(__ARM_FEATURE_BTI_DEFAULT) && (__ARM_FEATURE_BTI_DEFAULT == 1) |
| 111 |
#define GNU_PROPERTY_AARCH64_BTI (1 << 0) // Has Branch Target Identification |
| 112 |
#define AARCH64_VALID_JUMP_CALL_TARGET hint #38 // BTI 'jc' |
| 113 |
#define AARCH64_VALID_CALL_TARGET hint #34 // BTI 'c' |
| 114 |
#define AARCH64_VALID_JUMP_TARGET hint #36 // BTI 'j' |
| 115 |
#else |
| 116 |
#define GNU_PROPERTY_AARCH64_BTI 0 // No Branch Target Identification |
| 117 |
#define AARCH64_VALID_JUMP_CALL_TARGET |
| 118 |
#define AARCH64_VALID_CALL_TARGET |
| 119 |
#define AARCH64_VALID_JUMP_TARGET |
| 120 |
#endif |
| 121 |
|
| 122 |
#if defined(__ARM_FEATURE_PAC_DEFAULT) |
| 123 |
|
| 124 |
#if ((__ARM_FEATURE_PAC_DEFAULT & (1 << 0)) != 0) // authentication using key A |
| 125 |
#define AARCH64_SIGN_LINK_REGISTER paciasp |
| 126 |
#define AARCH64_VALIDATE_LINK_REGISTER autiasp |
| 127 |
#elif ((__ARM_FEATURE_PAC_DEFAULT & (1 << 1)) != 0) // authentication using key B |
| 128 |
#define AARCH64_SIGN_LINK_REGISTER pacibsp |
| 129 |
#define AARCH64_VALIDATE_LINK_REGISTER autibsp |
| 130 |
#else |
| 131 |
#error Pointer authentication defines no valid key! |
| 132 |
#endif |
| 133 |
#if ((__ARM_FEATURE_PAC_DEFAULT & (1 << 2)) != 0) // authentication of leaf functions |
| 134 |
#error Authentication of leaf functions is enabled but not supported in dav1d! |
| 135 |
#endif |
| 136 |
#define GNU_PROPERTY_AARCH64_PAC (1 << 1) |
| 137 |
|
| 138 |
#elif defined(__APPLE__) && defined(__arm64e__) |
| 139 |
|
| 140 |
#define GNU_PROPERTY_AARCH64_PAC 0 |
| 141 |
#define AARCH64_SIGN_LINK_REGISTER pacibsp |
| 142 |
#define AARCH64_VALIDATE_LINK_REGISTER autibsp |
| 143 |
|
| 144 |
#else /* __ARM_FEATURE_PAC_DEFAULT */ |
| 145 |
|
| 146 |
#define GNU_PROPERTY_AARCH64_PAC 0 |
| 147 |
#define AARCH64_SIGN_LINK_REGISTER |
| 148 |
#define AARCH64_VALIDATE_LINK_REGISTER |
| 149 |
|
| 150 |
#endif /* !__ARM_FEATURE_PAC_DEFAULT */ |
| 151 |
|
| 152 |
|
| 153 |
#if (GNU_PROPERTY_AARCH64_BTI != 0 || GNU_PROPERTY_AARCH64_PAC != 0) && defined(__ELF__) |
| 154 |
.pushsection .note.gnu.property, "a" |
| 155 |
.balign 8 |
| 156 |
.long 4 |
| 157 |
.long 0x10 |
| 158 |
.long 0x5 |
| 159 |
.asciz "GNU" |
| 160 |
.long 0xc0000000 /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */ |
| 161 |
.long 4 |
| 162 |
.long (GNU_PROPERTY_AARCH64_BTI | GNU_PROPERTY_AARCH64_PAC) |
| 163 |
.long 0 |
| 164 |
.popsection |
| 165 |
#endif /* (GNU_PROPERTY_AARCH64_BTI != 0 || GNU_PROPERTY_AARCH64_PAC != 0) && defined(__ELF__) */ |
| 166 |
#endif /* ARCH_AARCH64 */ |
| 167 |
|
| 168 |
#if ARCH_ARM |
| 169 |
.syntax unified |
| 170 |
#ifdef __ELF__ |
| 171 |
.arch armv7-a |
| 172 |
.fpu neon |
| 173 |
.eabi_attribute 10, 0 // suppress Tag_FP_arch |
| 174 |
.eabi_attribute 12, 0 // suppress Tag_Advanced_SIMD_arch |
| 175 |
.section .note.GNU-stack,"",%progbits // Mark stack as non-executable |
| 176 |
#endif /* __ELF__ */ |
| 177 |
|
| 178 |
#ifdef _WIN32 |
| 179 |
#define CONFIG_THUMB 1 |
| 180 |
#else |
| 181 |
#define CONFIG_THUMB 0 |
| 182 |
#endif |
| 183 |
|
| 184 |
#if CONFIG_THUMB |
| 185 |
.thumb |
| 186 |
#define A @ |
| 187 |
#define T |
| 188 |
#else |
| 189 |
#define A |
| 190 |
#define T @ |
| 191 |
#endif /* CONFIG_THUMB */ |
| 192 |
#endif /* ARCH_ARM */ |
| 193 |
|
| 194 |
#if !defined(PIC) |
| 195 |
#if defined(__PIC__) |
| 196 |
#define PIC __PIC__ |
| 197 |
#elif defined(__pic__) |
| 198 |
#define PIC __pic__ |
| 199 |
#endif |
| 200 |
#endif |
| 201 |
|
| 202 |
#ifndef PRIVATE_PREFIX |
| 203 |
#define PRIVATE_PREFIX dav1d_ |
| 204 |
#endif |
| 205 |
|
| 206 |
#define PASTE(a,b) a ## b |
| 207 |
#define CONCAT(a,b) PASTE(a,b) |
| 208 |
|
| 209 |
#ifdef PREFIX |
| 210 |
#define EXTERN CONCAT(_,PRIVATE_PREFIX) |
| 211 |
#else |
| 212 |
#define EXTERN PRIVATE_PREFIX |
| 213 |
#endif |
| 214 |
|
| 215 |
.macro function name, export=0, align=2 |
| 216 |
.macro endfunc |
| 217 |
#ifdef __ELF__ |
| 218 |
.size \name, . - \name |
| 219 |
#endif |
| 220 |
#if HAVE_AS_FUNC |
| 221 |
.endfunc |
| 222 |
#endif |
| 223 |
.purgem endfunc |
| 224 |
.endm |
| 225 |
.text |
| 226 |
.align \align |
| 227 |
.if \export |
| 228 |
.global EXTERN\name |
| 229 |
#ifdef __ELF__ |
| 230 |
.type EXTERN\name, %function |
| 231 |
.hidden EXTERN\name |
| 232 |
#elif defined(__MACH__) |
| 233 |
.private_extern EXTERN\name |
| 234 |
#endif |
| 235 |
#if HAVE_AS_FUNC |
| 236 |
.func EXTERN\name |
| 237 |
#endif |
| 238 |
EXTERN\name: |
| 239 |
.else |
| 240 |
#ifdef __ELF__ |
| 241 |
.type \name, %function |
| 242 |
#endif |
| 243 |
#if HAVE_AS_FUNC |
| 244 |
.func \name |
| 245 |
#endif |
| 246 |
.endif |
| 247 |
\name: |
| 248 |
#if ARCH_AARCH64 |
| 249 |
.if \export |
| 250 |
AARCH64_VALID_CALL_TARGET |
| 251 |
.endif |
| 252 |
#endif |
| 253 |
.endm |
| 254 |
|
| 255 |
.macro const name, export=0, align=2 |
| 256 |
.macro endconst |
| 257 |
#ifdef __ELF__ |
| 258 |
.size \name, . - \name |
| 259 |
#endif |
| 260 |
.purgem endconst |
| 261 |
.endm |
| 262 |
#if defined(_WIN32) |
| 263 |
.section .rdata |
| 264 |
#elif !defined(__MACH__) |
| 265 |
.section .rodata |
| 266 |
#else |
| 267 |
.const_data |
| 268 |
#endif |
| 269 |
.align \align |
| 270 |
.if \export |
| 271 |
.global EXTERN\name |
| 272 |
#ifdef __ELF__ |
| 273 |
.hidden EXTERN\name |
| 274 |
#elif defined(__MACH__) |
| 275 |
.private_extern EXTERN\name |
| 276 |
#endif |
| 277 |
EXTERN\name: |
| 278 |
.endif |
| 279 |
\name: |
| 280 |
.endm |
| 281 |
|
| 282 |
#ifdef __APPLE__ |
| 283 |
#define L(x) L ## x |
| 284 |
#else |
| 285 |
#define L(x) .L ## x |
| 286 |
#endif |
| 287 |
|
| 288 |
#define X(x) CONCAT(EXTERN, x) |
| 289 |
|
| 290 |
|
| 291 |
#endif /* DAV1D_SRC_ARM_ASM_S */ |