Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavutil / aarch64 / asm.S
1 /*
2  * Copyright (c) 2008 Mans Rullgard <mans@mansr.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "config.h"
22
23 #ifdef __ELF__
24 #   define ELF
25 #else
26 #   define ELF #
27 #endif
28
29 #if HAVE_AS_FUNC
30 #   define FUNC
31 #else
32 #   define FUNC #
33 #endif
34
35 #ifndef __has_feature
36 #   define __has_feature(x) 0
37 #endif
38
39 #if HAVE_AS_ARCH_DIRECTIVE
40         .arch           AS_ARCH_LEVEL
41 #endif
42
43 #if HAVE_AS_ARCHEXT_DOTPROD_DIRECTIVE
44 #define ENABLE_DOTPROD  .arch_extension dotprod
45 #define DISABLE_DOTPROD .arch_extension nodotprod
46 #else
47 #define ENABLE_DOTPROD
48 #define DISABLE_DOTPROD
49 #endif
50
51 #if HAVE_AS_ARCHEXT_I8MM_DIRECTIVE
52 #define ENABLE_I8MM  .arch_extension i8mm
53 #define DISABLE_I8MM .arch_extension noi8mm
54 #else
55 #define ENABLE_I8MM
56 #define DISABLE_I8MM
57 #endif
58
59 DISABLE_DOTPROD
60 DISABLE_I8MM
61
62
63 /* Support macros for
64  *   - Armv8.3-A Pointer Authentication and
65  *   - Armv8.5-A Branch Target Identification
66  * features which require emitting a .note.gnu.property section with the
67  * appropriate architecture-dependent feature bits set.
68  *
69  * |AARCH64_SIGN_LINK_REGISTER| and |AARCH64_VALIDATE_LINK_REGISTER| expand to
70  * PACIxSP and AUTIxSP, respectively. |AARCH64_SIGN_LINK_REGISTER| should be
71  * used immediately before saving the LR register (x30) to the stack.
72  * |AARCH64_VALIDATE_LINK_REGISTER| should be used immediately after restoring
73  * it. Note |AARCH64_SIGN_LINK_REGISTER|'s modifications to LR must be undone
74  * with |AARCH64_VALIDATE_LINK_REGISTER| before RET. The SP register must also
75  * have the same value at the two points. For example:
76  *
77  *   .global f
78  *   f:
79  *     AARCH64_SIGN_LINK_REGISTER
80  *     stp x29, x30, [sp, #-96]!
81  *     mov x29, sp
82  *     ...
83  *     ldp x29, x30, [sp], #96
84  *     AARCH64_VALIDATE_LINK_REGISTER
85  *     ret
86  *
87  * |AARCH64_VALID_CALL_TARGET| expands to BTI 'c'. Either it, or
88  * |AARCH64_SIGN_LINK_REGISTER|, must be used at every point that may be an
89  * indirect call target. In particular, all symbols exported from a file must
90  * begin with one of these macros. For example, a leaf function that does not
91  * save LR can instead use |AARCH64_VALID_CALL_TARGET|:
92  *
93  *   .globl return_zero
94  *   return_zero:
95  *     AARCH64_VALID_CALL_TARGET
96  *     mov x0, #0
97  *     ret
98  *
99  * A non-leaf function which does not immediately save LR may need both macros
100  * because |AARCH64_SIGN_LINK_REGISTER| appears late. For example, the function
101  * may jump to an alternate implementation before setting up the stack:
102  *
103  *   .globl with_early_jump
104  *   with_early_jump:
105  *     AARCH64_VALID_CALL_TARGET
106  *     cmp x0, #128
107  *     b.lt .Lwith_early_jump_128
108  *     AARCH64_SIGN_LINK_REGISTER
109  *     stp x29, x30, [sp, #-96]!
110  *     mov x29, sp
111  *     ...
112  *     ldp x29, x30, [sp], #96
113  *     AARCH64_VALIDATE_LINK_REGISTER
114  *     ret
115  *
116  *  .Lwith_early_jump_128:
117  *     ...
118  *     ret
119  *
120  * These annotations are only required with indirect calls. Private symbols that
121  * are only the target of direct calls do not require annotations. Also note
122  * that |AARCH64_VALID_CALL_TARGET| is only valid for indirect calls (BLR), not
123  * indirect jumps (BR). Indirect jumps in assembly are supported through
124  * |AARCH64_VALID_JUMP_TARGET|. Landing Pads which shall serve for jumps and
125  * calls can be created using |AARCH64_VALID_JUMP_CALL_TARGET|.
126  *
127  * Although not necessary, it is safe to use these macros in 32-bit ARM
128  * assembly. This may be used to simplify dual 32-bit and 64-bit files.
129  *
130  * References:
131  * - "ELF for the ArmĀ® 64-bit Architecture"
132  *   https: *github.com/ARM-software/abi-aa/blob/master/aaelf64/aaelf64.rst
133  * - "Providing protection for complex software"
134  *   https://developer.arm.com/architectures/learn-the-architecture/providing-protection-for-complex-software
135  */
136 #if defined(__ARM_FEATURE_BTI_DEFAULT) && (__ARM_FEATURE_BTI_DEFAULT == 1)
137 #   define GNU_PROPERTY_AARCH64_BTI (1 << 0)   // Has BTI
138 #   define AARCH64_VALID_CALL_TARGET hint #34  // BTI 'c'
139 #   define AARCH64_VALID_JUMP_TARGET hint #38  // BTI 'j'
140 #else
141 #   define GNU_PROPERTY_AARCH64_BTI 0          // No BTI
142 #   define AARCH64_VALID_CALL_TARGET
143 #   define AARCH64_VALID_JUMP_TARGET
144 #endif
145
146 #if defined(__ARM_FEATURE_PAC_DEFAULT)
147 #   if ((__ARM_FEATURE_PAC_DEFAULT & (1 << 0)) != 0) // authentication using key A
148 #       define AARCH64_SIGN_LINK_REGISTER      paciasp
149 #       define AARCH64_VALIDATE_LINK_REGISTER  autiasp
150 #   elif ((__ARM_FEATURE_PAC_DEFAULT & (1 << 1)) != 0) // authentication using key B
151 #       define AARCH64_SIGN_LINK_REGISTER      pacibsp
152 #       define AARCH64_VALIDATE_LINK_REGISTER  autibsp
153 #   else
154 #       error Pointer authentication defines no valid key!
155 #   endif
156 #   if ((__ARM_FEATURE_PAC_DEFAULT & (1 << 2)) != 0)
157 #       error Authentication of leaf functions is enabled but not supported in FFmpeg!
158 #   endif
159 #   define GNU_PROPERTY_AARCH64_PAC (1 << 1)
160 #else
161 #   define GNU_PROPERTY_AARCH64_PAC 0
162 #   define AARCH64_SIGN_LINK_REGISTER
163 #   define AARCH64_VALIDATE_LINK_REGISTER
164 #endif
165
166
167 #if (GNU_PROPERTY_AARCH64_BTI != 0 || GNU_PROPERTY_AARCH64_PAC != 0) && defined(__ELF__)
168         .pushsection .note.gnu.property, "a"
169         .balign 8
170         .long 4
171         .long 0x10
172         .long 0x5
173         .asciz "GNU"
174         .long 0xc0000000 /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */
175         .long 4
176         .long (GNU_PROPERTY_AARCH64_BTI | GNU_PROPERTY_AARCH64_PAC)
177         .long 0
178         .popsection
179 #endif
180
181 .macro  function name, export=0, align=2
182     .macro endfunc
183 ELF     .size   \name, . - \name
184 FUNC    .endfunc
185         .purgem endfunc
186     .endm
187         .text
188         .align          \align
189     .if \export
190         .global EXTERN_ASM\name
191 ELF     .type   EXTERN_ASM\name, %function
192 FUNC    .func   EXTERN_ASM\name
193 EXTERN_ASM\name:
194         AARCH64_VALID_CALL_TARGET
195     .else
196 ELF     .type   \name, %function
197 FUNC    .func   \name
198 \name:
199     .endif
200 .endm
201
202 .macro  const   name, align=2, relocate=0
203     .macro endconst
204 ELF     .size   \name, . - \name
205         .purgem endconst
206     .endm
207 #if HAVE_SECTION_DATA_REL_RO
208 .if \relocate
209         .section        .data.rel.ro
210 .else
211         .section        .rodata
212 .endif
213 #elif defined(_WIN32)
214         .section        .rdata
215 #elif !defined(__MACH__)
216         .section        .rodata
217 #else
218         .const_data
219 #endif
220         .align          \align
221 \name:
222 .endm
223
224 .macro  movrel rd, val, offset=0
225 #if CONFIG_PIC && defined(__APPLE__)
226     .if \offset < 0
227         adrp            \rd, \val@PAGE
228         add             \rd, \rd, \val@PAGEOFF
229         sub             \rd, \rd, -(\offset)
230     .else
231         adrp            \rd, \val+(\offset)@PAGE
232         add             \rd, \rd, \val+(\offset)@PAGEOFF
233     .endif
234 #elif CONFIG_PIC && defined(_WIN32)
235     .if \offset < 0
236         adrp            \rd, \val
237         add             \rd, \rd, :lo12:\val
238         sub             \rd, \rd, -(\offset)
239     .else
240         adrp            \rd, \val+(\offset)
241         add             \rd, \rd, :lo12:\val+(\offset)
242     .endif
243 #elif CONFIG_PIC
244 #   if __has_feature(hwaddress_sanitizer)
245         adrp            \rd, :pg_hi21_nc:\val+(\offset)
246 #   else
247         adrp            \rd, \val+(\offset)
248 #   endif
249         add             \rd, \rd, :lo12:\val+(\offset)
250 #else
251         ldr             \rd, =\val+\offset
252 #endif
253 .endm
254
255 #define GLUE(a, b) a ## b
256 #define JOIN(a, b) GLUE(a, b)
257 #define X(s) JOIN(EXTERN_ASM, s)
258
259 #define x18 do_not_use_x18
260 #define w18 do_not_use_w18