| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 |
| 16 | # This file defines helper functions for crypto/test/abi_test.h on aarch64. See |
| 17 | # that header for details on how to use this. |
| 18 | # |
| 19 | # For convenience, this file is linked into libcrypto, where consuming builds |
| 20 | # already support architecture-specific sources. The static linker should drop |
| 21 | # this code in non-test binaries. This includes a shared library build of |
| 22 | # libcrypto, provided --gc-sections (ELF), -dead_strip (iOS), or equivalent is |
| 23 | # used. |
| 24 | # |
| 25 | # References: |
| 26 | # |
| 27 | # AAPCS64: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0055b/IHI0055B_aapcs64.pdf |
| 28 | # iOS ARM64: https://developer.apple.com/library/archive/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARM64FunctionCallingConventions.html |
| 29 |
| 30 | use strict; |
| 31 |
| 32 | my $flavour = shift; |
| 33 | my $output = shift; |
| 34 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } |
| 35 |
| 36 | $0 =~ m/(.*[\/\\])[^\/\\]+$/; |
| 37 | my $dir = $1; |
| 38 | my $xlate; |
| 39 | ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or |
| 40 | ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or |
| 41 | die "can't locate arm-xlate.pl"; |
| 42 |
| 43 | open OUT, "| \"$^X\" \"$xlate\" $flavour \"$output\""; |
| 44 | *STDOUT = *OUT; |
| 45 |
| 46 | my ($func, $state, $argv, $argc) = ("x0", "x1", "x2", "x3"); |
| 47 | my $code = <<____; |
| 48 | .text |
| 49 |
| 50 | // abi_test_trampoline loads callee-saved registers from |state|, calls |func| |
| 51 | // with |argv|, then saves the callee-saved registers into |state|. It returns |
| 52 | // the result of |func|. The |unwind| argument is unused. |
| 53 | // uint64_t abi_test_trampoline(void (*func)(...), CallerState *state, |
| 54 | // const uint64_t *argv, size_t argc, |
| 55 | // uint64_t unwind); |
| 56 | .type abi_test_trampoline, %function |
| 57 | .globl abi_test_trampoline |
| 58 | .align 4 |
| 59 | abi_test_trampoline: |
| 60 | .Labi_test_trampoline_begin: |
| 61 | AARCH64_SIGN_LINK_REGISTER |
| 62 | // Stack layout (low to high addresses) |
| 63 | // x29,x30 (16 bytes) |
| 64 | // d8-d15 (64 bytes) |
| 65 | // x19-x28 (80 bytes) |
| 66 | // $state (8 bytes) |
| 67 | // padding (8 bytes) |
| 68 | stp x29, x30, [sp, #-176]! |
| 69 | mov x29, sp |
| 70 |
| 71 | // Saved callee-saved registers and |state|. |
| 72 | stp d8, d9, [sp, #16] |
| 73 | stp d10, d11, [sp, #32] |
| 74 | stp d12, d13, [sp, #48] |
| 75 | stp d14, d15, [sp, #64] |
| 76 | stp x19, x20, [sp, #80] |
| 77 | stp x21, x22, [sp, #96] |
| 78 | stp x23, x24, [sp, #112] |
| 79 | stp x25, x26, [sp, #128] |
| 80 | stp x27, x28, [sp, #144] |
| 81 | str $state, [sp, #160] |
| 82 |
| 83 | // Load registers from |state|, with the exception of x29. x29 is the |
| 84 | // frame pointer and also callee-saved, but AAPCS64 allows platforms to |
| 85 | // mandate that x29 always point to a frame. iOS64 does so, which means |
| 86 | // we cannot fill x29 with entropy without violating ABI rules |
| 87 | // ourselves. x29 is tested separately below. |
| 88 | ldp d8, d9, [$state], #16 |
| 89 | ldp d10, d11, [$state], #16 |
| 90 | ldp d12, d13, [$state], #16 |
| 91 | ldp d14, d15, [$state], #16 |
| 92 | ldp x19, x20, [$state], #16 |
| 93 | ldp x21, x22, [$state], #16 |
| 94 | ldp x23, x24, [$state], #16 |
| 95 | ldp x25, x26, [$state], #16 |
| 96 | ldp x27, x28, [$state], #16 |
| 97 |
| 98 | // Move parameters into temporary registers. |
| 99 | mov x9, $func |
| 100 | mov x10, $argv |
| 101 | mov x11, $argc |
| 102 |
| 103 | // Load parameters into registers. |
| 104 | cbz x11, .Largs_done |
| 105 | ldr x0, [x10], #8 |
| 106 | subs x11, x11, #1 |
| 107 | b.eq .Largs_done |
| 108 | ldr x1, [x10], #8 |
| 109 | subs x11, x11, #1 |
| 110 | b.eq .Largs_done |
| 111 | ldr x2, [x10], #8 |
| 112 | subs x11, x11, #1 |
| 113 | b.eq .Largs_done |
| 114 | ldr x3, [x10], #8 |
| 115 | subs x11, x11, #1 |
| 116 | b.eq .Largs_done |
| 117 | ldr x4, [x10], #8 |
| 118 | subs x11, x11, #1 |
| 119 | b.eq .Largs_done |
| 120 | ldr x5, [x10], #8 |
| 121 | subs x11, x11, #1 |
| 122 | b.eq .Largs_done |
| 123 | ldr x6, [x10], #8 |
| 124 | subs x11, x11, #1 |
| 125 | b.eq .Largs_done |
| 126 | ldr x7, [x10], #8 |
| 127 |
| 128 | .Largs_done: |
| 129 | blr x9 |
| 130 |
| 131 | // Reload |state| and store registers. |
| 132 | ldr $state, [sp, #160] |
| 133 | stp d8, d9, [$state], #16 |
| 134 | stp d10, d11, [$state], #16 |
| 135 | stp d12, d13, [$state], #16 |
| 136 | stp d14, d15, [$state], #16 |
| 137 | stp x19, x20, [$state], #16 |
| 138 | stp x21, x22, [$state], #16 |
| 139 | stp x23, x24, [$state], #16 |
| 140 | stp x25, x26, [$state], #16 |
| 141 | stp x27, x28, [$state], #16 |
| 142 |
| 143 | // |func| is required to preserve x29, the frame pointer. We cannot load |
| 144 | // random values into x29 (see comment above), so compare it against the |
| 145 | // expected value and zero the field of |state| if corrupted. |
| 146 | mov x9, sp |
| 147 | cmp x29, x9 |
| 148 | b.eq .Lx29_ok |
| 149 | str xzr, [$state] |
| 150 |
| 151 | .Lx29_ok: |
| 152 | // Restore callee-saved registers. |
| 153 | ldp d8, d9, [sp, #16] |
| 154 | ldp d10, d11, [sp, #32] |
| 155 | ldp d12, d13, [sp, #48] |
| 156 | ldp d14, d15, [sp, #64] |
| 157 | ldp x19, x20, [sp, #80] |
| 158 | ldp x21, x22, [sp, #96] |
| 159 | ldp x23, x24, [sp, #112] |
| 160 | ldp x25, x26, [sp, #128] |
| 161 | ldp x27, x28, [sp, #144] |
| 162 |
| 163 | ldp x29, x30, [sp], #176 |
| 164 | AARCH64_VALIDATE_LINK_REGISTER |
| 165 | ret |
| 166 | .size abi_test_trampoline,.-abi_test_trampoline |
| 167 | ____ |
| 168 |
| 169 | # abi_test_clobber_* zeros the corresponding register. These are used to test |
| 170 | # the ABI-testing framework. |
| 171 | foreach (0..29) { |
| 172 | # x18 is the platform register and off limits. |
| 173 | next if ($_ == 18); |
| 174 | $code .= <<____; |
| 175 | .type abi_test_clobber_x$_, %function |
| 176 | .globl abi_test_clobber_x$_ |
| 177 | .align 4 |
| 178 | abi_test_clobber_x$_: |
| 179 | AARCH64_VALID_CALL_TARGET |
| 180 | mov x$_, xzr |
| 181 | ret |
| 182 | .size abi_test_clobber_x$_,.-abi_test_clobber_x$_ |
| 183 | ____ |
| 184 | } |
| 185 | foreach (0..31) { |
| 186 | $code .= <<____; |
| 187 | .type abi_test_clobber_d$_, %function |
| 188 | .globl abi_test_clobber_d$_ |
| 189 | .align 4 |
| 190 | abi_test_clobber_d$_: |
| 191 | AARCH64_VALID_CALL_TARGET |
| 192 | fmov d$_, xzr |
| 193 | ret |
| 194 | .size abi_test_clobber_d$_,.-abi_test_clobber_d$_ |
| 195 | ____ |
| 196 | } |
| 197 |
| 198 | # abi_test_clobber_v*_upper clobbers only the upper half of v*. AAPCS64 only |
| 199 | # requires the lower half (d*) be preserved. |
| 200 | foreach (8..15) { |
| 201 | $code .= <<____; |
| 202 | .type abi_test_clobber_v${_}_upper, %function |
| 203 | .globl abi_test_clobber_v${_}_upper |
| 204 | .align 4 |
| 205 | abi_test_clobber_v${_}_upper: |
| 206 | AARCH64_VALID_CALL_TARGET |
| 207 | fmov v${_}.d[1], xzr |
| 208 | ret |
| 209 | .size abi_test_clobber_v${_}_upper,.-abi_test_clobber_v${_}_upper |
| 210 | ____ |
| 211 | } |
| 212 |
| 213 | print $code; |
| 214 | close STDOUT or die "error closing STDOUT: $!"; |
| 215 |