| 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 32-bit |
| 7 |
# ARM. See 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 |
# AAPCS: http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf |
| 18 |
# iOS ARMv6: https://developer.apple.com/library/archive/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARMv6FunctionCallingConventions.html |
| 19 |
# iOS ARMv7: https://developer.apple.com/library/archive/documentation/Xcode/Conceptual/iPhoneOSABIReference/Articles/ARMv7FunctionCallingConventions.html |
| 20 |
# Linux: http://sourcery.mentor.com/sgpp/lite/arm/portal/kbattach142/arm_gnu_linux_%20abi.pdf |
| 21 |
|
| 22 |
use strict; |
| 23 |
|
| 24 |
my $flavour = shift; |
| 25 |
my $output = shift; |
| 26 |
|
| 27 |
$0 =~ m/(.*[\/\\])[^\/\\]+$/; |
| 28 |
my $dir = $1; |
| 29 |
my $xlate; |
| 30 |
( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or |
| 31 |
( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or |
| 32 |
die "can't locate arm-xlate.pl"; |
| 33 |
|
| 34 |
open OUT, "| \"$^X\" \"$xlate\" $flavour \"$output\""; |
| 35 |
*STDOUT = *OUT; |
| 36 |
|
| 37 |
my ($func, $state, $argv, $argc) = ("r0", "r1", "r2", "r3"); |
| 38 |
my $code = <<____; |
| 39 |
.syntax unified |
| 40 |
|
| 41 |
.arch armv7-a |
| 42 |
.fpu vfp |
| 43 |
|
| 44 |
.text |
| 45 |
|
| 46 |
@ abi_test_trampoline loads callee-saved registers from |state|, calls |func| |
| 47 |
@ with |argv|, then saves the callee-saved registers into |state|. It returns |
| 48 |
@ the result of |func|. The |unwind| argument is unused. |
| 49 |
@ uint32_t abi_test_trampoline(void (*func)(...), CallerState *state, |
| 50 |
@ const uint32_t *argv, size_t argc, |
| 51 |
@ int unwind); |
| 52 |
.type abi_test_trampoline, %function |
| 53 |
.globl abi_test_trampoline |
| 54 |
.align 4 |
| 55 |
abi_test_trampoline: |
| 56 |
@ Save parameters and all callee-saved registers. For convenience, we |
| 57 |
@ save r9 on iOS even though it's volatile. |
| 58 |
vstmdb sp!, {d8-d15} |
| 59 |
stmdb sp!, {r0-r11,lr} |
| 60 |
|
| 61 |
@ Reserve stack space for six (10-4) stack parameters, plus an extra 4 |
| 62 |
@ bytes to keep it 8-byte-aligned (see AAPCS, section 5.3). |
| 63 |
sub sp, sp, #28 |
| 64 |
|
| 65 |
@ Every register in AAPCS is either non-volatile or a parameter (except |
| 66 |
@ r9 on iOS), so this code, by the actual call, loses all its scratch |
| 67 |
@ registers. First fill in stack parameters while there are registers |
| 68 |
@ to spare. |
| 69 |
cmp $argc, #4 |
| 70 |
bls .Lstack_args_done |
| 71 |
mov r4, sp @ r4 is the output pointer. |
| 72 |
add r5, $argv, $argc, lsl #2 @ Set r5 to the end of argv. |
| 73 |
add $argv, $argv, #16 @ Skip four arguments. |
| 74 |
.Lstack_args_loop: |
| 75 |
ldr r6, [$argv], #4 |
| 76 |
cmp $argv, r5 |
| 77 |
str r6, [r4], #4 |
| 78 |
bne .Lstack_args_loop |
| 79 |
|
| 80 |
.Lstack_args_done: |
| 81 |
@ Load registers from |$state|. |
| 82 |
vldmia $state!, {d8-d15} |
| 83 |
#if defined(__APPLE__) |
| 84 |
@ r9 is not volatile on iOS. |
| 85 |
ldmia $state!, {r4-r8,r10-r11} |
| 86 |
#else |
| 87 |
ldmia $state!, {r4-r11} |
| 88 |
#endif |
| 89 |
|
| 90 |
@ Load register parameters. This uses up our remaining registers, so we |
| 91 |
@ repurpose lr as scratch space. |
| 92 |
ldr $argc, [sp, #40] @ Reload argc. |
| 93 |
ldr lr, [sp, #36] @ Load argv into lr. |
| 94 |
cmp $argc, #3 |
| 95 |
bhi .Larg_r3 |
| 96 |
beq .Larg_r2 |
| 97 |
cmp $argc, #1 |
| 98 |
bhi .Larg_r1 |
| 99 |
beq .Larg_r0 |
| 100 |
b .Largs_done |
| 101 |
|
| 102 |
.Larg_r3: |
| 103 |
ldr r3, [lr, #12] @ argv[3] |
| 104 |
.Larg_r2: |
| 105 |
ldr r2, [lr, #8] @ argv[2] |
| 106 |
.Larg_r1: |
| 107 |
ldr r1, [lr, #4] @ argv[1] |
| 108 |
.Larg_r0: |
| 109 |
ldr r0, [lr] @ argv[0] |
| 110 |
.Largs_done: |
| 111 |
|
| 112 |
@ With every other register in use, load the function pointer into lr |
| 113 |
@ and call the function. |
| 114 |
ldr lr, [sp, #28] |
| 115 |
blx lr |
| 116 |
|
| 117 |
@ r1-r3 are free for use again. The trampoline only supports |
| 118 |
@ single-return functions. Pass r4-r11 to the caller. |
| 119 |
ldr $state, [sp, #32] |
| 120 |
vstmia $state!, {d8-d15} |
| 121 |
#if defined(__APPLE__) |
| 122 |
@ r9 is not volatile on iOS. |
| 123 |
stmia $state!, {r4-r8,r10-r11} |
| 124 |
#else |
| 125 |
stmia $state!, {r4-r11} |
| 126 |
#endif |
| 127 |
|
| 128 |
@ Unwind the stack and restore registers. |
| 129 |
add sp, sp, #44 @ 44 = 28+16 |
| 130 |
ldmia sp!, {r4-r11,lr} @ Skip r0-r3 (see +16 above). |
| 131 |
vldmia sp!, {d8-d15} |
| 132 |
|
| 133 |
bx lr |
| 134 |
.size abi_test_trampoline,.-abi_test_trampoline |
| 135 |
____ |
| 136 |
|
| 137 |
# abi_test_clobber_* zeros the corresponding register. These are used to test |
| 138 |
# the ABI-testing framework. |
| 139 |
foreach (0..12) { |
| 140 |
# This loop skips r13 (sp), r14 (lr, implicitly clobbered by every call), and |
| 141 |
# r15 (pc). |
| 142 |
$code .= <<____; |
| 143 |
.type abi_test_clobber_r$_, %function |
| 144 |
.globl abi_test_clobber_r$_ |
| 145 |
.align 4 |
| 146 |
abi_test_clobber_r$_: |
| 147 |
mov r$_, #0 |
| 148 |
bx lr |
| 149 |
.size abi_test_clobber_r$_,.-abi_test_clobber_r$_ |
| 150 |
____ |
| 151 |
} |
| 152 |
|
| 153 |
foreach (0..15) { |
| 154 |
my $lo = "s".(2*$_); |
| 155 |
my $hi = "s".(2*$_+1); |
| 156 |
$code .= <<____; |
| 157 |
.type abi_test_clobber_d$_, %function |
| 158 |
.globl abi_test_clobber_d$_ |
| 159 |
.align 4 |
| 160 |
abi_test_clobber_d$_: |
| 161 |
mov r0, #0 |
| 162 |
vmov $lo, r0 |
| 163 |
vmov $hi, r0 |
| 164 |
bx lr |
| 165 |
.size abi_test_clobber_d$_,.-abi_test_clobber_d$_ |
| 166 |
____ |
| 167 |
} |
| 168 |
|
| 169 |
print $code; |
| 170 |
close STDOUT or die "error closing STDOUT: $!"; |