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