Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | |
Dr. Stephen Henson | 4d1f3f7 | 2008-01-05 22:28:38 +0000 | [diff] [blame] | 3 | $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; |
| 4 | push(@INC, "${dir}perlasm", "perlasm"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 5 | require "x86asm.pl"; |
| 6 | |
| 7 | &asm_init($ARGV[0],"x86cpuid"); |
| 8 | |
Andy Polyakov | c502265 | 2005-05-18 08:42:08 +0000 | [diff] [blame] | 9 | for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); } |
| 10 | |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 11 | &function_begin("OPENSSL_ia32_cpuid"); |
| 12 | &xor ("edx","edx"); |
| 13 | &pushf (); |
| 14 | &pop ("eax"); |
| 15 | &mov ("ecx","eax"); |
| 16 | &xor ("eax",1<<21); |
| 17 | &push ("eax"); |
| 18 | &popf (); |
| 19 | &pushf (); |
| 20 | &pop ("eax"); |
| 21 | &xor ("ecx","eax"); |
Andy Polyakov | 162f677 | 2007-04-01 17:28:08 +0000 | [diff] [blame] | 22 | &xor ("eax","eax"); |
Andy Polyakov | 0f2ece8 | 2012-02-28 14:20:21 +0000 | [diff] [blame] | 23 | &bt ("ecx",21); |
| 24 | &jnc (&label("nocpuid")); |
Andy Polyakov | c5cd28b | 2012-11-17 19:04:15 +0000 | [diff] [blame] | 25 | &mov ("esi",&wparam(0)); |
| 26 | &mov (&DWP(8,"esi"),"eax"); # clear 3rd word |
Andy Polyakov | 162f677 | 2007-04-01 17:28:08 +0000 | [diff] [blame] | 27 | &cpuid (); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 28 | &mov ("edi","eax"); # max value for standard query level |
| 29 | |
Andy Polyakov | 162f677 | 2007-04-01 17:28:08 +0000 | [diff] [blame] | 30 | &xor ("eax","eax"); |
| 31 | &cmp ("ebx",0x756e6547); # "Genu" |
| 32 | &setne (&LB("eax")); |
| 33 | &mov ("ebp","eax"); |
| 34 | &cmp ("edx",0x49656e69); # "ineI" |
| 35 | &setne (&LB("eax")); |
| 36 | &or ("ebp","eax"); |
| 37 | &cmp ("ecx",0x6c65746e); # "ntel" |
| 38 | &setne (&LB("eax")); |
Andy Polyakov | 5867231 | 2009-05-12 21:19:41 +0000 | [diff] [blame] | 39 | &or ("ebp","eax"); # 0 indicates Intel CPU |
Andy Polyakov | 5867231 | 2009-05-12 21:19:41 +0000 | [diff] [blame] | 40 | &jz (&label("intel")); |
| 41 | |
| 42 | &cmp ("ebx",0x68747541); # "Auth" |
| 43 | &setne (&LB("eax")); |
| 44 | &mov ("esi","eax"); |
| 45 | &cmp ("edx",0x69746E65); # "enti" |
| 46 | &setne (&LB("eax")); |
| 47 | &or ("esi","eax"); |
| 48 | &cmp ("ecx",0x444D4163); # "cAMD" |
| 49 | &setne (&LB("eax")); |
| 50 | &or ("esi","eax"); # 0 indicates AMD CPU |
| 51 | &jnz (&label("intel")); |
| 52 | |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 53 | # AMD specific |
Andy Polyakov | 5867231 | 2009-05-12 21:19:41 +0000 | [diff] [blame] | 54 | &mov ("eax",0x80000000); |
| 55 | &cpuid (); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 56 | &cmp ("eax",0x80000001); |
| 57 | &jb (&label("intel")); |
| 58 | &mov ("esi","eax"); |
| 59 | &mov ("eax",0x80000001); |
| 60 | &cpuid (); |
| 61 | &or ("ebp","ecx"); |
| 62 | &and ("ebp",1<<11|1); # isolate XOP bit |
| 63 | &cmp ("esi",0x80000008); |
Andy Polyakov | 5867231 | 2009-05-12 21:19:41 +0000 | [diff] [blame] | 64 | &jb (&label("intel")); |
| 65 | |
| 66 | &mov ("eax",0x80000008); |
| 67 | &cpuid (); |
| 68 | &movz ("esi",&LB("ecx")); # number of cores - 1 |
| 69 | &inc ("esi"); # number of cores |
| 70 | |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 71 | &mov ("eax",1); |
Andy Polyakov | 5702e96 | 2013-03-04 20:05:04 +0100 | [diff] [blame] | 72 | &xor ("ecx","ecx"); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 73 | &cpuid (); |
| 74 | &bt ("edx",28); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 75 | &jnc (&label("generic")); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 76 | &shr ("ebx",16); |
| 77 | &and ("ebx",0xff); |
| 78 | &cmp ("ebx","esi"); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 79 | &ja (&label("generic")); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 80 | &and ("edx",0xefffffff); # clear hyper-threading bit |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 81 | &jmp (&label("generic")); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 82 | |
Andy Polyakov | 5867231 | 2009-05-12 21:19:41 +0000 | [diff] [blame] | 83 | &set_label("intel"); |
Andy Polyakov | 1bc0b68 | 2013-06-10 22:20:46 +0200 | [diff] [blame] | 84 | &cmp ("edi",7); |
| 85 | &jb (&label("cacheinfo")); |
| 86 | |
| 87 | &mov ("esi",&wparam(0)); |
| 88 | &mov ("eax",7); |
| 89 | &xor ("ecx","ecx"); |
| 90 | &cpuid (); |
| 91 | &mov (&DWP(8,"esi"),"ebx"); |
| 92 | |
| 93 | &set_label("cacheinfo"); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 94 | &cmp ("edi",4); |
| 95 | &mov ("edi",-1); |
| 96 | &jb (&label("nocacheinfo")); |
| 97 | |
| 98 | &mov ("eax",4); |
| 99 | &mov ("ecx",0); # query L1D |
| 100 | &cpuid (); |
| 101 | &mov ("edi","eax"); |
| 102 | &shr ("edi",14); |
| 103 | &and ("edi",0xfff); # number of cores -1 per L1D |
| 104 | |
| 105 | &set_label("nocacheinfo"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 106 | &mov ("eax",1); |
Andy Polyakov | 5702e96 | 2013-03-04 20:05:04 +0100 | [diff] [blame] | 107 | &xor ("ecx","ecx"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 108 | &cpuid (); |
Andy Polyakov | 18f5603 | 2011-05-29 12:50:02 +0000 | [diff] [blame] | 109 | &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0 |
Andy Polyakov | 162f677 | 2007-04-01 17:28:08 +0000 | [diff] [blame] | 110 | &cmp ("ebp",0); |
Andy Polyakov | 4bb9008 | 2011-05-27 15:32:43 +0000 | [diff] [blame] | 111 | &jne (&label("notintel")); |
| 112 | &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs |
Andy Polyakov | 3df2eff | 2007-07-21 14:46:27 +0000 | [diff] [blame] | 113 | &and (&HB("eax"),15); # familiy ID |
| 114 | &cmp (&HB("eax"),15); # P4? |
Andy Polyakov | 4bb9008 | 2011-05-27 15:32:43 +0000 | [diff] [blame] | 115 | &jne (&label("notintel")); |
| 116 | &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR |
| 117 | &set_label("notintel"); |
Andy Polyakov | 3df2eff | 2007-07-21 14:46:27 +0000 | [diff] [blame] | 118 | &bt ("edx",28); # test hyper-threading bit |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 119 | &jnc (&label("generic")); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 120 | &and ("edx",0xefffffff); |
| 121 | &cmp ("edi",0); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 122 | &je (&label("generic")); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 123 | |
| 124 | &or ("edx",0x10000000); |
Andy Polyakov | 162f677 | 2007-04-01 17:28:08 +0000 | [diff] [blame] | 125 | &shr ("ebx",16); |
Andy Polyakov | 761393b | 2009-05-14 18:17:26 +0000 | [diff] [blame] | 126 | &cmp (&LB("ebx"),1); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 127 | &ja (&label("generic")); |
Andy Polyakov | b1e8b4e | 2007-05-19 17:52:51 +0000 | [diff] [blame] | 128 | &and ("edx",0xefffffff); # clear hyper-threading bit if not |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 129 | |
| 130 | &set_label("generic"); |
| 131 | &and ("ebp",1<<11); # isolate AMD XOP flag |
Andy Polyakov | 18f5603 | 2011-05-29 12:50:02 +0000 | [diff] [blame] | 132 | &and ("ecx",0xfffff7ff); # force 11th bit to 0 |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 133 | &mov ("esi","edx"); |
| 134 | &or ("ebp","ecx"); # merge AMD XOP flag |
| 135 | |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 136 | &bt ("ecx",27); # check OSXSAVE bit |
Andy Polyakov | e879dd4 | 2011-11-05 10:44:12 +0000 | [diff] [blame] | 137 | &jnc (&label("clear_avx")); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 138 | &xor ("ecx","ecx"); |
| 139 | &data_byte(0x0f,0x01,0xd0); # xgetbv |
| 140 | &and ("eax",6); |
| 141 | &cmp ("eax",6); |
| 142 | &je (&label("done")); |
| 143 | &cmp ("eax",2); |
| 144 | &je (&label("clear_avx")); |
| 145 | &set_label("clear_xmm"); |
Andy Polyakov | 18f5603 | 2011-05-29 12:50:02 +0000 | [diff] [blame] | 146 | &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits |
| 147 | &and ("esi",0xfeffffff); # clear FXSR |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 148 | &set_label("clear_avx"); |
Andy Polyakov | 18f5603 | 2011-05-29 12:50:02 +0000 | [diff] [blame] | 149 | &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits |
Andy Polyakov | c5cd28b | 2012-11-17 19:04:15 +0000 | [diff] [blame] | 150 | &mov ("edi",&wparam(0)); |
| 151 | &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2 |
Andy Polyakov | 3df2eff | 2007-07-21 14:46:27 +0000 | [diff] [blame] | 152 | &set_label("done"); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 153 | &mov ("eax","esi"); |
| 154 | &mov ("edx","ebp"); |
Andy Polyakov | 0f2ece8 | 2012-02-28 14:20:21 +0000 | [diff] [blame] | 155 | &set_label("nocpuid"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 156 | &function_end("OPENSSL_ia32_cpuid"); |
| 157 | |
Andy Polyakov | 2b247cf | 2004-08-29 16:36:05 +0000 | [diff] [blame] | 158 | &external_label("OPENSSL_ia32cap_P"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 159 | |
Andy Polyakov | 2b247cf | 2004-08-29 16:36:05 +0000 | [diff] [blame] | 160 | &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 161 | &xor ("eax","eax"); |
| 162 | &xor ("edx","edx"); |
Andy Polyakov | 2b247cf | 2004-08-29 16:36:05 +0000 | [diff] [blame] | 163 | &picmeup("ecx","OPENSSL_ia32cap_P"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 164 | &bt (&DWP(0,"ecx"),4); |
| 165 | &jnc (&label("notsc")); |
| 166 | &rdtsc (); |
| 167 | &set_label("notsc"); |
| 168 | &ret (); |
| 169 | &function_end_B("OPENSSL_rdtsc"); |
| 170 | |
Rich Salz | f642ebc | 2014-08-09 08:02:20 -0400 | [diff] [blame] | 171 | # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host], |
Andy Polyakov | c85c5c4 | 2004-09-09 14:50:32 +0000 | [diff] [blame] | 172 | # but it's safe to call it on any [supported] 32-bit platform... |
| 173 | # Just check for [non-]zero return value... |
| 174 | &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD"); |
| 175 | &picmeup("ecx","OPENSSL_ia32cap_P"); |
| 176 | &bt (&DWP(0,"ecx"),4); |
| 177 | &jnc (&label("nohalt")); # no TSC |
| 178 | |
| 179 | &data_word(0x9058900e); # push %cs; pop %eax |
| 180 | &and ("eax",3); |
| 181 | &jnz (&label("nohalt")); # not enough privileges |
| 182 | |
| 183 | &pushf (); |
Andy Polyakov | f9c5e5d | 2012-04-28 10:36:58 +0000 | [diff] [blame] | 184 | &pop ("eax"); |
Andy Polyakov | c85c5c4 | 2004-09-09 14:50:32 +0000 | [diff] [blame] | 185 | &bt ("eax",9); |
| 186 | &jnc (&label("nohalt")); # interrupts are disabled |
| 187 | |
| 188 | &rdtsc (); |
| 189 | &push ("edx"); |
| 190 | &push ("eax"); |
| 191 | &halt (); |
| 192 | &rdtsc (); |
| 193 | |
| 194 | &sub ("eax",&DWP(0,"esp")); |
| 195 | &sbb ("edx",&DWP(4,"esp")); |
| 196 | &add ("esp",8); |
| 197 | &ret (); |
| 198 | |
| 199 | &set_label("nohalt"); |
| 200 | &xor ("eax","eax"); |
| 201 | &xor ("edx","edx"); |
| 202 | &ret (); |
| 203 | &function_end_B("OPENSSL_instrument_halt"); |
| 204 | |
Rich Salz | f642ebc | 2014-08-09 08:02:20 -0400 | [diff] [blame] | 205 | # Essentially there is only one use for this function. Under DJGPP: |
| 206 | # |
| 207 | # #include <go32.h> |
| 208 | # ... |
| 209 | # i=OPENSSL_far_spin(_dos_ds,0x46c); |
| 210 | # ... |
| 211 | # to obtain the number of spins till closest timer interrupt. |
| 212 | |
| 213 | &function_begin_B("OPENSSL_far_spin"); |
| 214 | &pushf (); |
| 215 | &pop ("eax"); |
| 216 | &bt ("eax",9); |
| 217 | &jnc (&label("nospin")); # interrupts are disabled |
| 218 | |
| 219 | &mov ("eax",&DWP(4,"esp")); |
| 220 | &mov ("ecx",&DWP(8,"esp")); |
| 221 | &data_word (0x90d88e1e); # push %ds, mov %eax,%ds |
| 222 | &xor ("eax","eax"); |
| 223 | &mov ("edx",&DWP(0,"ecx")); |
| 224 | &jmp (&label("spin")); |
| 225 | |
| 226 | &align (16); |
| 227 | &set_label("spin"); |
| 228 | &inc ("eax"); |
| 229 | &cmp ("edx",&DWP(0,"ecx")); |
| 230 | &je (&label("spin")); |
| 231 | |
| 232 | &data_word (0x1f909090); # pop %ds |
| 233 | &ret (); |
| 234 | |
| 235 | &set_label("nospin"); |
| 236 | &xor ("eax","eax"); |
| 237 | &xor ("edx","edx"); |
| 238 | &ret (); |
| 239 | &function_end_B("OPENSSL_far_spin"); |
Andy Polyakov | cee73df | 2005-05-03 21:05:06 +0000 | [diff] [blame] | 240 | |
| 241 | &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD"); |
| 242 | &xor ("eax","eax"); |
| 243 | &xor ("edx","edx"); |
| 244 | &picmeup("ecx","OPENSSL_ia32cap_P"); |
| 245 | &mov ("ecx",&DWP(0,"ecx")); |
| 246 | &bt (&DWP(0,"ecx"),1); |
| 247 | &jnc (&label("no_x87")); |
Andy Polyakov | c502265 | 2005-05-18 08:42:08 +0000 | [diff] [blame] | 248 | if ($sse2) { |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 249 | &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits |
| 250 | &cmp ("ecx",1<<26|1<<24); |
| 251 | &jne (&label("no_sse2")); |
Andy Polyakov | c502265 | 2005-05-18 08:42:08 +0000 | [diff] [blame] | 252 | &pxor ("xmm0","xmm0"); |
| 253 | &pxor ("xmm1","xmm1"); |
| 254 | &pxor ("xmm2","xmm2"); |
| 255 | &pxor ("xmm3","xmm3"); |
| 256 | &pxor ("xmm4","xmm4"); |
| 257 | &pxor ("xmm5","xmm5"); |
| 258 | &pxor ("xmm6","xmm6"); |
| 259 | &pxor ("xmm7","xmm7"); |
| 260 | &set_label("no_sse2"); |
| 261 | } |
Andy Polyakov | c49a0aa | 2005-06-24 16:32:10 +0000 | [diff] [blame] | 262 | # just a bunch of fldz to zap the fp/mm bank followed by finit... |
| 263 | &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b); |
Andy Polyakov | cee73df | 2005-05-03 21:05:06 +0000 | [diff] [blame] | 264 | &set_label("no_x87"); |
| 265 | &lea ("eax",&DWP(4,"esp")); |
| 266 | &ret (); |
| 267 | &function_end_B("OPENSSL_wipe_cpu"); |
| 268 | |
| 269 | &function_begin_B("OPENSSL_atomic_add"); |
| 270 | &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg |
| 271 | &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg |
| 272 | &push ("ebx"); |
| 273 | &nop (); |
| 274 | &mov ("eax",&DWP(0,"edx")); |
| 275 | &set_label("spin"); |
| 276 | &lea ("ebx",&DWP(0,"eax","ecx")); |
| 277 | &nop (); |
| 278 | &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded |
| 279 | &jne (&label("spin")); |
| 280 | &mov ("eax","ebx"); # OpenSSL expects the new value |
| 281 | &pop ("ebx"); |
| 282 | &ret (); |
| 283 | &function_end_B("OPENSSL_atomic_add"); |
| 284 | |
Andy Polyakov | 061bebc | 2005-12-03 11:56:55 +0000 | [diff] [blame] | 285 | # This function can become handy under Win32 in situations when |
| 286 | # we don't know which calling convention, __stdcall or __cdecl(*), |
| 287 | # indirect callee is using. In C it can be deployed as |
| 288 | # |
| 289 | #ifdef OPENSSL_CPUID_OBJ |
| 290 | # type OPENSSL_indirect_call(void *f,...); |
| 291 | # ... |
| 292 | # OPENSSL_indirect_call(func,[up to $max arguments]); |
| 293 | #endif |
| 294 | # |
| 295 | # (*) it's designed to work even for __fastcall if number of |
| 296 | # arguments is 1 or 2! |
| 297 | &function_begin_B("OPENSSL_indirect_call"); |
| 298 | { |
Andy Polyakov | f9c5e5d | 2012-04-28 10:36:58 +0000 | [diff] [blame] | 299 | my ($max,$i)=(7,); # $max has to be chosen as 4*n-1 |
Andy Polyakov | 061bebc | 2005-12-03 11:56:55 +0000 | [diff] [blame] | 300 | # in order to preserve eventual |
| 301 | # stack alignment |
| 302 | &push ("ebp"); |
| 303 | &mov ("ebp","esp"); |
| 304 | &sub ("esp",$max*4); |
| 305 | &mov ("ecx",&DWP(12,"ebp")); |
| 306 | &mov (&DWP(0,"esp"),"ecx"); |
| 307 | &mov ("edx",&DWP(16,"ebp")); |
| 308 | &mov (&DWP(4,"esp"),"edx"); |
| 309 | for($i=2;$i<$max;$i++) |
| 310 | { |
| 311 | # Some copies will be redundant/bogus... |
| 312 | &mov ("eax",&DWP(12+$i*4,"ebp")); |
| 313 | &mov (&DWP(0+$i*4,"esp"),"eax"); |
| 314 | } |
Andy Polyakov | 3ebf898 | 2005-12-06 18:43:59 +0000 | [diff] [blame] | 315 | &call_ptr (&DWP(8,"ebp"));# make the call... |
Andy Polyakov | 061bebc | 2005-12-03 11:56:55 +0000 | [diff] [blame] | 316 | &mov ("esp","ebp"); # ... and just restore the stack pointer |
| 317 | # without paying attention to what we called, |
| 318 | # (__cdecl *func) or (__stdcall *one). |
| 319 | &pop ("ebp"); |
| 320 | &ret (); |
| 321 | } |
| 322 | &function_end_B("OPENSSL_indirect_call"); |
| 323 | |
Andy Polyakov | b2dba9b | 2007-05-14 21:35:25 +0000 | [diff] [blame] | 324 | &function_begin_B("OPENSSL_cleanse"); |
| 325 | &mov ("edx",&wparam(0)); |
| 326 | &mov ("ecx",&wparam(1)); |
| 327 | &xor ("eax","eax"); |
| 328 | &cmp ("ecx",7); |
| 329 | &jae (&label("lot")); |
Andy Polyakov | 7676eeb | 2010-01-24 14:54:24 +0000 | [diff] [blame] | 330 | &cmp ("ecx",0); |
| 331 | &je (&label("ret")); |
Andy Polyakov | b2dba9b | 2007-05-14 21:35:25 +0000 | [diff] [blame] | 332 | &set_label("little"); |
| 333 | &mov (&BP(0,"edx"),"al"); |
| 334 | &sub ("ecx",1); |
| 335 | &lea ("edx",&DWP(1,"edx")); |
| 336 | &jnz (&label("little")); |
Andy Polyakov | 7676eeb | 2010-01-24 14:54:24 +0000 | [diff] [blame] | 337 | &set_label("ret"); |
Andy Polyakov | b2dba9b | 2007-05-14 21:35:25 +0000 | [diff] [blame] | 338 | &ret (); |
| 339 | |
| 340 | &set_label("lot",16); |
| 341 | &test ("edx",3); |
| 342 | &jz (&label("aligned")); |
| 343 | &mov (&BP(0,"edx"),"al"); |
| 344 | &lea ("ecx",&DWP(-1,"ecx")); |
| 345 | &lea ("edx",&DWP(1,"edx")); |
| 346 | &jmp (&label("lot")); |
| 347 | &set_label("aligned"); |
| 348 | &mov (&DWP(0,"edx"),"eax"); |
| 349 | &lea ("ecx",&DWP(-4,"ecx")); |
| 350 | &test ("ecx",-4); |
| 351 | &lea ("edx",&DWP(4,"edx")); |
| 352 | &jnz (&label("aligned")); |
| 353 | &cmp ("ecx",0); |
| 354 | &jne (&label("little")); |
| 355 | &ret (); |
| 356 | &function_end_B("OPENSSL_cleanse"); |
| 357 | |
Andy Polyakov | 5fabb88 | 2011-04-17 12:46:00 +0000 | [diff] [blame] | 358 | { |
| 359 | my $lasttick = "esi"; |
| 360 | my $lastdiff = "ebx"; |
| 361 | my $out = "edi"; |
| 362 | my $cnt = "ecx"; |
| 363 | my $max = "ebp"; |
| 364 | |
| 365 | &function_begin("OPENSSL_instrument_bus"); |
| 366 | &mov ("eax",0); |
| 367 | if ($sse2) { |
| 368 | &picmeup("edx","OPENSSL_ia32cap_P"); |
| 369 | &bt (&DWP(0,"edx"),4); |
| 370 | &jnc (&label("nogo")); # no TSC |
| 371 | &bt (&DWP(0,"edx"),19); |
| 372 | &jnc (&label("nogo")); # no CLFLUSH |
| 373 | |
| 374 | &mov ($out,&wparam(0)); # load arguments |
| 375 | &mov ($cnt,&wparam(1)); |
| 376 | |
| 377 | # collect 1st tick |
| 378 | &rdtsc (); |
| 379 | &mov ($lasttick,"eax"); # lasttick = tick |
| 380 | &mov ($lastdiff,0); # lastdiff = 0 |
| 381 | &clflush(&DWP(0,$out)); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 382 | &data_byte(0xf0); # lock |
Andy Polyakov | 5fabb88 | 2011-04-17 12:46:00 +0000 | [diff] [blame] | 383 | &add (&DWP(0,$out),$lastdiff); |
| 384 | &jmp (&label("loop")); |
| 385 | |
| 386 | &set_label("loop",16); |
| 387 | &rdtsc (); |
| 388 | &mov ("edx","eax"); # put aside tick (yes, I neglect edx) |
| 389 | &sub ("eax",$lasttick); # diff |
| 390 | &mov ($lasttick,"edx"); # lasttick = tick |
| 391 | &mov ($lastdiff,"eax"); # lastdiff = diff |
| 392 | &clflush(&DWP(0,$out)); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 393 | &data_byte(0xf0); # lock |
Andy Polyakov | 5fabb88 | 2011-04-17 12:46:00 +0000 | [diff] [blame] | 394 | &add (&DWP(0,$out),"eax"); # accumulate diff |
| 395 | &lea ($out,&DWP(4,$out)); # ++$out |
| 396 | &sub ($cnt,1); # --$cnt |
| 397 | &jnz (&label("loop")); |
| 398 | |
| 399 | &mov ("eax",&wparam(1)); |
| 400 | &set_label("nogo"); |
| 401 | } |
| 402 | &function_end("OPENSSL_instrument_bus"); |
| 403 | |
| 404 | &function_begin("OPENSSL_instrument_bus2"); |
| 405 | &mov ("eax",0); |
| 406 | if ($sse2) { |
| 407 | &picmeup("edx","OPENSSL_ia32cap_P"); |
| 408 | &bt (&DWP(0,"edx"),4); |
| 409 | &jnc (&label("nogo")); # no TSC |
| 410 | &bt (&DWP(0,"edx"),19); |
| 411 | &jnc (&label("nogo")); # no CLFLUSH |
| 412 | |
| 413 | &mov ($out,&wparam(0)); # load arguments |
| 414 | &mov ($cnt,&wparam(1)); |
| 415 | &mov ($max,&wparam(2)); |
| 416 | |
| 417 | &rdtsc (); # collect 1st tick |
| 418 | &mov ($lasttick,"eax"); # lasttick = tick |
| 419 | &mov ($lastdiff,0); # lastdiff = 0 |
| 420 | |
| 421 | &clflush(&DWP(0,$out)); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 422 | &data_byte(0xf0); # lock |
Andy Polyakov | 5fabb88 | 2011-04-17 12:46:00 +0000 | [diff] [blame] | 423 | &add (&DWP(0,$out),$lastdiff); |
| 424 | |
| 425 | &rdtsc (); # collect 1st diff |
| 426 | &mov ("edx","eax"); # put aside tick (yes, I neglect edx) |
| 427 | &sub ("eax",$lasttick); # diff |
| 428 | &mov ($lasttick,"edx"); # lasttick = tick |
| 429 | &mov ($lastdiff,"eax"); # lastdiff = diff |
| 430 | &jmp (&label("loop2")); |
| 431 | |
| 432 | &set_label("loop2",16); |
| 433 | &clflush(&DWP(0,$out)); |
Andy Polyakov | b906422 | 2011-05-16 20:35:11 +0000 | [diff] [blame] | 434 | &data_byte(0xf0); # lock |
Andy Polyakov | 5fabb88 | 2011-04-17 12:46:00 +0000 | [diff] [blame] | 435 | &add (&DWP(0,$out),"eax"); # accumulate diff |
| 436 | |
| 437 | &sub ($max,1); |
| 438 | &jz (&label("done2")); |
| 439 | |
| 440 | &rdtsc (); |
| 441 | &mov ("edx","eax"); # put aside tick (yes, I neglect edx) |
| 442 | &sub ("eax",$lasttick); # diff |
| 443 | &mov ($lasttick,"edx"); # lasttick = tick |
| 444 | &cmp ("eax",$lastdiff); |
| 445 | &mov ($lastdiff,"eax"); # lastdiff = diff |
| 446 | &mov ("edx",0); |
| 447 | &setne ("dl"); |
| 448 | &sub ($cnt,"edx"); # conditional --$cnt |
| 449 | &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out |
| 450 | &jnz (&label("loop2")); |
| 451 | |
| 452 | &set_label("done2"); |
| 453 | &mov ("eax",&wparam(1)); |
| 454 | &sub ("eax",$cnt); |
| 455 | &set_label("nogo"); |
| 456 | } |
| 457 | &function_end("OPENSSL_instrument_bus2"); |
| 458 | } |
| 459 | |
Andy Polyakov | 301799b | 2011-06-04 12:20:45 +0000 | [diff] [blame] | 460 | &function_begin_B("OPENSSL_ia32_rdrand"); |
| 461 | &mov ("ecx",8); |
| 462 | &set_label("loop"); |
| 463 | &rdrand ("eax"); |
| 464 | &jc (&label("break")); |
| 465 | &loop (&label("loop")); |
| 466 | &set_label("break"); |
| 467 | &cmp ("eax",0); |
| 468 | &cmove ("eax","ecx"); |
| 469 | &ret (); |
| 470 | &function_end_B("OPENSSL_ia32_rdrand"); |
| 471 | |
Andy Polyakov | f4d4564 | 2014-02-14 17:24:12 +0100 | [diff] [blame] | 472 | &function_begin_B("OPENSSL_ia32_rdseed"); |
| 473 | &mov ("ecx",8); |
| 474 | &set_label("loop"); |
| 475 | &rdseed ("eax"); |
| 476 | &jc (&label("break")); |
| 477 | &loop (&label("loop")); |
| 478 | &set_label("break"); |
| 479 | &cmp ("eax",0); |
| 480 | &cmove ("eax","ecx"); |
| 481 | &ret (); |
| 482 | &function_end_B("OPENSSL_ia32_rdseed"); |
| 483 | |
Andy Polyakov | 2b247cf | 2004-08-29 16:36:05 +0000 | [diff] [blame] | 484 | &initseg("OPENSSL_cpuid_setup"); |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 485 | |
Andy Polyakov | 6206682 | 2012-08-29 14:19:59 +0000 | [diff] [blame] | 486 | &hidden("OPENSSL_cpuid_setup"); |
| 487 | &hidden("OPENSSL_ia32cap_P"); |
| 488 | |
Andy Polyakov | 14e21f8 | 2004-07-26 20:18:55 +0000 | [diff] [blame] | 489 | &asm_finish(); |