| 1 |
#!/usr/bin/env perl |
| 2 |
# Copyright (c) 2018, Google Inc. |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: ISC |
| 5 |
|
| 6 |
# This file defines helper functions for crypto/test/abi_test.h on x86. 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 (Mac), or equivalent is |
| 13 |
# used. |
| 14 |
# |
| 15 |
# References: |
| 16 |
# |
| 17 |
# SysV ABI: https://uclibc.org/docs/psABI-i386.pdf |
| 18 |
# Win32 ABI: https://docs.microsoft.com/en-us/cpp/cpp/argument-passing-and-naming-conventions?view=vs-2017 |
| 19 |
|
| 20 |
use strict; |
| 21 |
|
| 22 |
$0 =~ m/(.*[\/\\])[^\/\\]+$/; |
| 23 |
my $dir = $1; |
| 24 |
push(@INC, "${dir}", "${dir}../../perlasm"); |
| 25 |
require "x86asm.pl"; |
| 26 |
|
| 27 |
my $output = $ARGV[1]; |
| 28 |
open STDOUT, ">$output"; |
| 29 |
|
| 30 |
&asm_init($ARGV[0]); |
| 31 |
|
| 32 |
# abi_test_trampoline loads callee-saved registers from |state|, calls |func| |
| 33 |
# with |argv|, then saves the callee-saved registers into |state|. It returns |
| 34 |
# the result of |func|. |unwind| is ignored. |
| 35 |
# uint32_t abi_test_trampoline(void (*func)(...), CallerState *state, |
| 36 |
# const uint32_t *argv, size_t argc, |
| 37 |
# int unwind); |
| 38 |
&function_begin("abi_test_trampoline") |
| 39 |
# Load registers from |state|. Note |function_begin| (as opposed to |
| 40 |
# |function_begin_B|) automatically saves all callee-saved registers, so we |
| 41 |
# may freely clobber them. |
| 42 |
&mov("ecx", &wparam(1)); |
| 43 |
&mov("esi", &DWP(4*0, "ecx")); |
| 44 |
&mov("edi", &DWP(4*1, "ecx")); |
| 45 |
&mov("ebx", &DWP(4*2, "ecx")); |
| 46 |
&mov("ebp", &DWP(4*3, "ecx")); |
| 47 |
|
| 48 |
# Use a fixed stack allocation so |wparam| continues to work. abi_test.h |
| 49 |
# supports at most 10 arguments. The SysV ABI requires a 16-byte-aligned |
| 50 |
# stack on process entry, so round up to 3 (mod 4). |
| 51 |
&stack_push(11); |
| 52 |
|
| 53 |
# Copy parameters to stack. |
| 54 |
&mov("eax", &wparam(2)); |
| 55 |
&xor("ecx", "ecx"); |
| 56 |
&set_label("loop"); |
| 57 |
&cmp("ecx", &wparam(3)); |
| 58 |
&jae(&label("loop_done")); |
| 59 |
&mov("edx", &DWP(0, "eax", "ecx", 4)); |
| 60 |
&mov(&DWP(0, "esp", "ecx", 4), "edx"); |
| 61 |
&add("ecx", 1); |
| 62 |
&jmp(&label("loop")); |
| 63 |
|
| 64 |
&set_label("loop_done"); |
| 65 |
&call_ptr(&wparam(0)); |
| 66 |
|
| 67 |
&stack_pop(11); |
| 68 |
|
| 69 |
# Save registers back into |state|. |
| 70 |
&mov("ecx", &wparam(1)); |
| 71 |
&mov(&DWP(4*0, "ecx"), "esi"); |
| 72 |
&mov(&DWP(4*1, "ecx"), "edi"); |
| 73 |
&mov(&DWP(4*2, "ecx"), "ebx"); |
| 74 |
&mov(&DWP(4*3, "ecx"), "ebp"); |
| 75 |
&function_end("abi_test_trampoline") |
| 76 |
|
| 77 |
# abi_test_get_and_clear_direction_flag clears the direction flag. If the flag |
| 78 |
# was previously set, it returns one. Otherwise, it returns zero. |
| 79 |
# int abi_test_get_and_clear_direction_flag(void); |
| 80 |
&function_begin_B("abi_test_get_and_clear_direction_flag"); |
| 81 |
&pushf(); |
| 82 |
&pop("eax"); |
| 83 |
&and("eax", 0x400); |
| 84 |
&shr("eax", 10); |
| 85 |
&cld(); |
| 86 |
&ret(); |
| 87 |
&function_end_B("abi_test_get_and_clear_direction_flag"); |
| 88 |
|
| 89 |
# abi_test_set_direction_flag sets the direction flag. |
| 90 |
# void abi_test_set_direction_flag(void); |
| 91 |
&function_begin_B("abi_test_set_direction_flag"); |
| 92 |
&std(); |
| 93 |
&ret(); |
| 94 |
&function_end_B("abi_test_set_direction_flag"); |
| 95 |
|
| 96 |
# abi_test_clobber_* zeros the corresponding register. These are used to test |
| 97 |
# the ABI-testing framework. |
| 98 |
foreach ("eax", "ebx", "ecx", "edx", "edi", "esi", "ebp") { |
| 99 |
&function_begin_B("abi_test_clobber_$_"); |
| 100 |
&xor($_, $_); |
| 101 |
&ret(); |
| 102 |
&function_end_B("abi_test_clobber_$_"); |
| 103 |
} |
| 104 |
foreach (0..7) { |
| 105 |
&function_begin_B("abi_test_clobber_xmm$_"); |
| 106 |
&pxor("xmm$_", "xmm$_"); |
| 107 |
&ret(); |
| 108 |
&function_end_B("abi_test_clobber_xmm$_"); |
| 109 |
} |
| 110 |
|
| 111 |
&asm_finish(); |
| 112 |
|
| 113 |
close STDOUT or die "error closing STDOUT: $!"; |