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