| 1 |
dnl x86 fat binary entrypoints. |
| 2 |
|
| 3 |
dnl Contributed to the GNU project by Kevin Ryde (original x86_32 code) and |
| 4 |
dnl Torbjorn Granlund (port to x86_64) |
| 5 |
|
| 6 |
dnl Copyright 2003, 2009, 2011-2014 Free Software Foundation, Inc. |
| 7 |
|
| 8 |
dnl This file is part of the GNU MP Library. |
| 9 |
dnl |
| 10 |
dnl The GNU MP Library is free software; you can redistribute it and/or modify |
| 11 |
dnl it under the terms of either: |
| 12 |
dnl |
| 13 |
dnl * the GNU Lesser General Public License as published by the Free |
| 14 |
dnl Software Foundation; either version 3 of the License, or (at your |
| 15 |
dnl option) any later version. |
| 16 |
dnl |
| 17 |
dnl or |
| 18 |
dnl |
| 19 |
dnl * the GNU General Public License as published by the Free Software |
| 20 |
dnl Foundation; either version 2 of the License, or (at your option) any |
| 21 |
dnl later version. |
| 22 |
dnl |
| 23 |
dnl or both in parallel, as here. |
| 24 |
dnl |
| 25 |
dnl The GNU MP Library is distributed in the hope that it will be useful, but |
| 26 |
dnl WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 27 |
dnl or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 28 |
dnl for more details. |
| 29 |
dnl |
| 30 |
dnl You should have received copies of the GNU General Public License and the |
| 31 |
dnl GNU Lesser General Public License along with the GNU MP Library. If not, |
| 32 |
dnl see https://www.gnu.org/licenses/. |
| 33 |
|
| 34 |
include(`../config.m4') |
| 35 |
|
| 36 |
|
| 37 |
dnl Forcibly disable profiling. |
| 38 |
dnl |
| 39 |
dnl The entrypoints and inits are small enough not to worry about, the real |
| 40 |
dnl routines arrived at will have any profiling. Also, the way the code |
| 41 |
dnl here ends with a jump means we won't work properly with the |
| 42 |
dnl "instrument" profiling scheme anyway. |
| 43 |
|
| 44 |
define(`WANT_PROFILING',no) |
| 45 |
|
| 46 |
|
| 47 |
dnl We define PRETEND_PIC as a helper symbol, the use it for suppressing |
| 48 |
dnl normal, fast call code, since that triggers problems on Darwin and |
| 49 |
dnl OpenBSD. |
| 50 |
|
| 51 |
ifdef(`DARWIN', |
| 52 |
`define(`PRETEND_PIC')') |
| 53 |
ifdef(`OPENBSD', |
| 54 |
`define(`PRETEND_PIC')') |
| 55 |
ifdef(`PIC', |
| 56 |
`define(`PRETEND_PIC')') |
| 57 |
|
| 58 |
ABI_SUPPORT(DOS64) |
| 59 |
ABI_SUPPORT(STD64) |
| 60 |
|
| 61 |
TEXT |
| 62 |
|
| 63 |
dnl Usage: FAT_ENTRY(name, offset) |
| 64 |
dnl |
| 65 |
dnl Emit a fat binary entrypoint function of the given name. This is the |
| 66 |
dnl normal entry for applications, eg. __gmpn_add_n. |
| 67 |
dnl |
| 68 |
dnl The code simply jumps through the function pointer in __gmpn_cpuvec at |
| 69 |
dnl the given "offset" (in bytes). |
| 70 |
dnl |
| 71 |
dnl For non-PIC, the jumps are 5 bytes each, aligning them to 8 should be |
| 72 |
dnl fine for all x86s. |
| 73 |
dnl |
| 74 |
dnl For ELF/DARWIN PIC, the jumps are 20 bytes each, and are best aligned to |
| 75 |
dnl 16 to ensure at least the first two instructions don't cross a cache line |
| 76 |
dnl boundary. |
| 77 |
dnl |
| 78 |
dnl For DOS64, the jumps are 6 bytes. The same form works also for GNU/Linux |
| 79 |
dnl (at least with certain assembler/linkers) but FreeBSD 8.2 crashes. Not |
| 80 |
dnl tested on Darwin, Slowaris, NetBSD, etc. |
| 81 |
dnl |
| 82 |
dnl Note the extra `' ahead of PROLOGUE obscures it from the HAVE_NATIVE |
| 83 |
dnl grepping in configure, stopping that code trying to eval something with |
| 84 |
dnl $1 in it. |
| 85 |
|
| 86 |
define(FAT_ENTRY, |
| 87 |
m4_assert_numargs(2) |
| 88 |
`ifdef(`HOST_DOS64', |
| 89 |
` ALIGN(8) |
| 90 |
`'PROLOGUE($1) |
| 91 |
jmp *$2+GSYM_PREFIX`'__gmpn_cpuvec(%rip) |
| 92 |
EPILOGUE() |
| 93 |
', |
| 94 |
` ALIGN(ifdef(`PIC',16,8)) |
| 95 |
`'PROLOGUE($1) |
| 96 |
ifdef(`PRETEND_PIC', |
| 97 |
` LEA( GSYM_PREFIX`'__gmpn_cpuvec, %rax) |
| 98 |
jmp *$2(%rax) |
| 99 |
',`dnl non-PIC |
| 100 |
jmp *GSYM_PREFIX`'__gmpn_cpuvec+$2 |
| 101 |
') |
| 102 |
EPILOGUE() |
| 103 |
')') |
| 104 |
|
| 105 |
|
| 106 |
dnl FAT_ENTRY for each CPUVEC_FUNCS_LIST |
| 107 |
dnl |
| 108 |
|
| 109 |
define(`CPUVEC_offset',0) |
| 110 |
foreach(i, |
| 111 |
`FAT_ENTRY(MPN(i),CPUVEC_offset) |
| 112 |
define(`CPUVEC_offset',eval(CPUVEC_offset + 8))', |
| 113 |
CPUVEC_FUNCS_LIST) |
| 114 |
|
| 115 |
|
| 116 |
dnl Usage: FAT_INIT(name, offset) |
| 117 |
dnl |
| 118 |
dnl Emit a fat binary initializer function of the given name. These |
| 119 |
dnl functions are the initial values for the pointers in __gmpn_cpuvec. |
| 120 |
dnl |
| 121 |
dnl The code simply calls __gmpn_cpuvec_init, and then jumps back through |
| 122 |
dnl the __gmpn_cpuvec pointer, at the given "offset" (in bytes). |
| 123 |
dnl __gmpn_cpuvec_init will have stored the address of the selected |
| 124 |
dnl implementation there. |
| 125 |
dnl |
| 126 |
dnl Only one of these routines will be executed, and only once, since after |
| 127 |
dnl that all the __gmpn_cpuvec pointers go to real routines. So there's no |
| 128 |
dnl need for anything special here, just something small and simple. To |
| 129 |
dnl keep code size down, "fat_init" is a shared bit of code, arrived at |
| 130 |
dnl with the offset in %al. %al is used since the movb instruction is 2 |
| 131 |
dnl bytes where %eax would be 4. |
| 132 |
dnl |
| 133 |
dnl Note having `PROLOGUE in FAT_INIT obscures that PROLOGUE from the |
| 134 |
dnl HAVE_NATIVE grepping in configure, preventing that code trying to eval |
| 135 |
dnl something with $1 in it. |
| 136 |
dnl |
| 137 |
dnl We need to preserve parameter registers over the __gmpn_cpuvec_init call |
| 138 |
|
| 139 |
define(FAT_INIT, |
| 140 |
m4_assert_numargs(2) |
| 141 |
`PROLOGUE($1) |
| 142 |
mov $`'$2, %al |
| 143 |
jmp L(fat_init) |
| 144 |
EPILOGUE() |
| 145 |
') |
| 146 |
|
| 147 |
dnl FAT_INIT for each CPUVEC_FUNCS_LIST |
| 148 |
dnl |
| 149 |
|
| 150 |
define(`CPUVEC_offset',0) |
| 151 |
foreach(i, |
| 152 |
`FAT_INIT(MPN(i`'_init),CPUVEC_offset) |
| 153 |
define(`CPUVEC_offset',eval(CPUVEC_offset + 1))', |
| 154 |
CPUVEC_FUNCS_LIST) |
| 155 |
|
| 156 |
L(fat_init): |
| 157 |
C al __gmpn_cpuvec byte offset |
| 158 |
|
| 159 |
movzbl %al, %eax |
| 160 |
IFSTD(` push %rdi ') |
| 161 |
IFSTD(` push %rsi ') |
| 162 |
push %rdx |
| 163 |
push %rcx |
| 164 |
push %r8 |
| 165 |
push %r9 |
| 166 |
push %rax |
| 167 |
CALL( __gmpn_cpuvec_init) |
| 168 |
pop %rax |
| 169 |
pop %r9 |
| 170 |
pop %r8 |
| 171 |
pop %rcx |
| 172 |
pop %rdx |
| 173 |
IFSTD(` pop %rsi ') |
| 174 |
IFSTD(` pop %rdi ') |
| 175 |
ifdef(`PRETEND_PIC',` |
| 176 |
LEA( GSYM_PREFIX`'__gmpn_cpuvec, %r10) |
| 177 |
jmp *(%r10,%rax,8) |
| 178 |
',`dnl non-PIC |
| 179 |
jmp *GSYM_PREFIX`'__gmpn_cpuvec(,%rax,8) |
| 180 |
') |
| 181 |
|
| 182 |
|
| 183 |
C long __gmpn_cpuid (char dst[12], int id); |
| 184 |
C |
| 185 |
C This is called only 3 times, so just something simple and compact is fine. |
| 186 |
C |
| 187 |
C The rcx/ecx zeroing here is needed for the BMI2 check. |
| 188 |
|
| 189 |
define(`rp', `%rdi') |
| 190 |
define(`idx', `%rsi') |
| 191 |
|
| 192 |
PROLOGUE(__gmpn_cpuid) |
| 193 |
FUNC_ENTRY(2) |
| 194 |
mov %rbx, %r8 |
| 195 |
mov R32(idx), R32(%rax) |
| 196 |
xor %ecx, %ecx |
| 197 |
cpuid |
| 198 |
mov %ebx, (rp) |
| 199 |
mov %edx, 4(rp) |
| 200 |
mov %ecx, 8(rp) |
| 201 |
mov %r8, %rbx |
| 202 |
FUNC_EXIT() |
| 203 |
ret |
| 204 |
EPILOGUE() |