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