2 # Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
9 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
10 push(@INC, "${dir}perlasm", "perlasm");
17 &asm_init($ARGV[0],"x86cpuid");
19 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
21 &function_begin("OPENSSL_ia32_cpuid");
34 &jnc (&label("nocpuid"));
35 &mov ("esi",&wparam(0));
36 &mov (&DWP(8,"esi"),"eax"); # clear 3rd word
38 &mov ("edi","eax"); # max value for standard query level
41 &cmp ("ebx",0x756e6547); # "Genu"
44 &cmp ("edx",0x49656e69); # "ineI"
47 &cmp ("ecx",0x6c65746e); # "ntel"
49 &or ("ebp","eax"); # 0 indicates Intel CPU
50 &jz (&label("intel"));
52 &cmp ("ebx",0x68747541); # "Auth"
55 &cmp ("edx",0x69746E65); # "enti"
58 &cmp ("ecx",0x444D4163); # "cAMD"
60 &or ("esi","eax"); # 0 indicates AMD CPU
61 &jnz (&label("intel"));
64 &mov ("eax",0x80000000);
66 &cmp ("eax",0x80000001);
67 &jb (&label("intel"));
69 &mov ("eax",0x80000001);
72 &and ("ebp",1<<11|1); # isolate XOP bit
73 &cmp ("esi",0x80000008);
74 &jb (&label("intel"));
76 &mov ("eax",0x80000008);
78 &movz ("esi",&LB("ecx")); # number of cores - 1
79 &inc ("esi"); # number of cores
85 &jnc (&label("generic"));
89 &ja (&label("generic"));
90 &and ("edx",0xefffffff); # clear hyper-threading bit
91 &jmp (&label("generic"));
95 &jb (&label("cacheinfo"));
97 &mov ("esi",&wparam(0));
101 &mov (&DWP(8,"esi"),"ebx");
103 &set_label("cacheinfo");
106 &jb (&label("nocacheinfo"));
109 &mov ("ecx",0); # query L1D
113 &and ("edi",0xfff); # number of cores -1 per L1D
115 &set_label("nocacheinfo");
119 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
121 &jne (&label("notintel"));
122 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
123 &and (&HB("eax"),15); # familiy ID
124 &cmp (&HB("eax"),15); # P4?
125 &jne (&label("notintel"));
126 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
127 &set_label("notintel");
128 &bt ("edx",28); # test hyper-threading bit
129 &jnc (&label("generic"));
130 &and ("edx",0xefffffff);
132 &je (&label("generic"));
134 &or ("edx",0x10000000);
137 &ja (&label("generic"));
138 &and ("edx",0xefffffff); # clear hyper-threading bit if not
140 &set_label("generic");
141 &and ("ebp",1<<11); # isolate AMD XOP flag
142 &and ("ecx",0xfffff7ff); # force 11th bit to 0
144 &or ("ebp","ecx"); # merge AMD XOP flag
146 &bt ("ecx",27); # check OSXSAVE bit
147 &jnc (&label("clear_avx"));
149 &data_byte(0x0f,0x01,0xd0); # xgetbv
152 &je (&label("done"));
154 &je (&label("clear_avx"));
155 &set_label("clear_xmm");
156 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
157 &and ("esi",0xfeffffff); # clear FXSR
158 &set_label("clear_avx");
159 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
160 &mov ("edi",&wparam(0));
161 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
165 &set_label("nocpuid");
166 &function_end("OPENSSL_ia32_cpuid");
168 &external_label("OPENSSL_ia32cap_P");
170 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
173 &picmeup("ecx","OPENSSL_ia32cap_P");
174 &bt (&DWP(0,"ecx"),4);
175 &jnc (&label("notsc"));
179 &function_end_B("OPENSSL_rdtsc");
181 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
182 # but it's safe to call it on any [supported] 32-bit platform...
183 # Just check for [non-]zero return value...
184 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
185 &picmeup("ecx","OPENSSL_ia32cap_P");
186 &bt (&DWP(0,"ecx"),4);
187 &jnc (&label("nohalt")); # no TSC
189 &data_word(0x9058900e); # push %cs; pop %eax
191 &jnz (&label("nohalt")); # not enough privileges
196 &jnc (&label("nohalt")); # interrupts are disabled
204 &sub ("eax",&DWP(0,"esp"));
205 &sbb ("edx",&DWP(4,"esp"));
209 &set_label("nohalt");
213 &function_end_B("OPENSSL_instrument_halt");
215 # Essentially there is only one use for this function. Under DJGPP:
219 # i=OPENSSL_far_spin(_dos_ds,0x46c);
221 # to obtain the number of spins till closest timer interrupt.
223 &function_begin_B("OPENSSL_far_spin");
227 &jnc (&label("nospin")); # interrupts are disabled
229 &mov ("eax",&DWP(4,"esp"));
230 &mov ("ecx",&DWP(8,"esp"));
231 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
233 &mov ("edx",&DWP(0,"ecx"));
234 &jmp (&label("spin"));
239 &cmp ("edx",&DWP(0,"ecx"));
240 &je (&label("spin"));
242 &data_word (0x1f909090); # pop %ds
245 &set_label("nospin");
249 &function_end_B("OPENSSL_far_spin");
251 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
254 &picmeup("ecx","OPENSSL_ia32cap_P");
255 &mov ("ecx",&DWP(0,"ecx"));
256 &bt (&DWP(0,"ecx"),1);
257 &jnc (&label("no_x87"));
259 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
260 &cmp ("ecx",1<<26|1<<24);
261 &jne (&label("no_sse2"));
262 &pxor ("xmm0","xmm0");
263 &pxor ("xmm1","xmm1");
264 &pxor ("xmm2","xmm2");
265 &pxor ("xmm3","xmm3");
266 &pxor ("xmm4","xmm4");
267 &pxor ("xmm5","xmm5");
268 &pxor ("xmm6","xmm6");
269 &pxor ("xmm7","xmm7");
270 &set_label("no_sse2");
272 # just a bunch of fldz to zap the fp/mm bank followed by finit...
273 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
274 &set_label("no_x87");
275 &lea ("eax",&DWP(4,"esp"));
277 &function_end_B("OPENSSL_wipe_cpu");
279 &function_begin_B("OPENSSL_atomic_add");
280 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
281 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
284 &mov ("eax",&DWP(0,"edx"));
286 &lea ("ebx",&DWP(0,"eax","ecx"));
288 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
289 &jne (&label("spin"));
290 &mov ("eax","ebx"); # OpenSSL expects the new value
293 &function_end_B("OPENSSL_atomic_add");
295 # This function can become handy under Win32 in situations when
296 # we don't know which calling convention, __stdcall or __cdecl(*),
297 # indirect callee is using. In C it can be deployed as
299 #ifdef OPENSSL_CPUID_OBJ
300 # type OPENSSL_indirect_call(void *f,...);
302 # OPENSSL_indirect_call(func,[up to $max arguments]);
305 # (*) it's designed to work even for __fastcall if number of
306 # arguments is 1 or 2!
307 &function_begin_B("OPENSSL_indirect_call");
309 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
310 # in order to preserve eventual
315 &mov ("ecx",&DWP(12,"ebp"));
316 &mov (&DWP(0,"esp"),"ecx");
317 &mov ("edx",&DWP(16,"ebp"));
318 &mov (&DWP(4,"esp"),"edx");
319 for($i=2;$i<$max;$i++)
321 # Some copies will be redundant/bogus...
322 &mov ("eax",&DWP(12+$i*4,"ebp"));
323 &mov (&DWP(0+$i*4,"esp"),"eax");
325 &call_ptr (&DWP(8,"ebp"));# make the call...
326 &mov ("esp","ebp"); # ... and just restore the stack pointer
327 # without paying attention to what we called,
328 # (__cdecl *func) or (__stdcall *one).
332 &function_end_B("OPENSSL_indirect_call");
334 &function_begin_B("OPENSSL_cleanse");
335 &mov ("edx",&wparam(0));
336 &mov ("ecx",&wparam(1));
339 &jae (&label("lot"));
342 &set_label("little");
343 &mov (&BP(0,"edx"),"al");
345 &lea ("edx",&DWP(1,"edx"));
346 &jnz (&label("little"));
350 &set_label("lot",16);
352 &jz (&label("aligned"));
353 &mov (&BP(0,"edx"),"al");
354 &lea ("ecx",&DWP(-1,"ecx"));
355 &lea ("edx",&DWP(1,"edx"));
356 &jmp (&label("lot"));
357 &set_label("aligned");
358 &mov (&DWP(0,"edx"),"eax");
359 &lea ("ecx",&DWP(-4,"ecx"));
361 &lea ("edx",&DWP(4,"edx"));
362 &jnz (&label("aligned"));
364 &jne (&label("little"));
366 &function_end_B("OPENSSL_cleanse");
368 &function_begin_B("CRYPTO_memcmp");
371 &mov ("esi",&wparam(0));
372 &mov ("edi",&wparam(1));
373 &mov ("ecx",&wparam(2));
377 &je (&label("no_data"));
379 &mov ("dl",&BP(0,"esi"));
380 &lea ("esi",&DWP(1,"esi"));
381 &xor ("dl",&BP(0,"edi"));
382 &lea ("edi",&DWP(1,"edi"));
385 &jnz (&label("loop"));
388 &set_label("no_data");
392 &function_end_B("CRYPTO_memcmp");
394 my $lasttick = "esi";
395 my $lastdiff = "ebx";
400 &function_begin("OPENSSL_instrument_bus");
403 &picmeup("edx","OPENSSL_ia32cap_P");
404 &bt (&DWP(0,"edx"),4);
405 &jnc (&label("nogo")); # no TSC
406 &bt (&DWP(0,"edx"),19);
407 &jnc (&label("nogo")); # no CLFLUSH
409 &mov ($out,&wparam(0)); # load arguments
410 &mov ($cnt,&wparam(1));
414 &mov ($lasttick,"eax"); # lasttick = tick
415 &mov ($lastdiff,0); # lastdiff = 0
416 &clflush(&DWP(0,$out));
417 &data_byte(0xf0); # lock
418 &add (&DWP(0,$out),$lastdiff);
419 &jmp (&label("loop"));
421 &set_label("loop",16);
423 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
424 &sub ("eax",$lasttick); # diff
425 &mov ($lasttick,"edx"); # lasttick = tick
426 &mov ($lastdiff,"eax"); # lastdiff = diff
427 &clflush(&DWP(0,$out));
428 &data_byte(0xf0); # lock
429 &add (&DWP(0,$out),"eax"); # accumulate diff
430 &lea ($out,&DWP(4,$out)); # ++$out
431 &sub ($cnt,1); # --$cnt
432 &jnz (&label("loop"));
434 &mov ("eax",&wparam(1));
437 &function_end("OPENSSL_instrument_bus");
439 &function_begin("OPENSSL_instrument_bus2");
442 &picmeup("edx","OPENSSL_ia32cap_P");
443 &bt (&DWP(0,"edx"),4);
444 &jnc (&label("nogo")); # no TSC
445 &bt (&DWP(0,"edx"),19);
446 &jnc (&label("nogo")); # no CLFLUSH
448 &mov ($out,&wparam(0)); # load arguments
449 &mov ($cnt,&wparam(1));
450 &mov ($max,&wparam(2));
452 &rdtsc (); # collect 1st tick
453 &mov ($lasttick,"eax"); # lasttick = tick
454 &mov ($lastdiff,0); # lastdiff = 0
456 &clflush(&DWP(0,$out));
457 &data_byte(0xf0); # lock
458 &add (&DWP(0,$out),$lastdiff);
460 &rdtsc (); # collect 1st diff
461 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
462 &sub ("eax",$lasttick); # diff
463 &mov ($lasttick,"edx"); # lasttick = tick
464 &mov ($lastdiff,"eax"); # lastdiff = diff
465 &jmp (&label("loop2"));
467 &set_label("loop2",16);
468 &clflush(&DWP(0,$out));
469 &data_byte(0xf0); # lock
470 &add (&DWP(0,$out),"eax"); # accumulate diff
473 &jz (&label("done2"));
476 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
477 &sub ("eax",$lasttick); # diff
478 &mov ($lasttick,"edx"); # lasttick = tick
479 &cmp ("eax",$lastdiff);
480 &mov ($lastdiff,"eax"); # lastdiff = diff
483 &sub ($cnt,"edx"); # conditional --$cnt
484 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
485 &jnz (&label("loop2"));
488 &mov ("eax",&wparam(1));
492 &function_end("OPENSSL_instrument_bus2");
497 &function_begin_B("OPENSSL_ia32_${rdop}");
501 &jc (&label("break"));
502 &loop (&label("loop"));
505 &cmove ("eax","ecx");
507 &function_end_B("OPENSSL_ia32_${rdop}");
509 &function_begin_B("OPENSSL_ia32_${rdop}_bytes");
512 &xor ("eax","eax"); # return value
513 &mov ("edi",&wparam(0));
514 &mov ("ebx",&wparam(1));
517 &je (&label("done"));
522 &jc (&label("break"));
523 &loop (&label("loop"));
524 &jmp (&label("done"));
526 &set_label("break",16);
528 &jb (&label("tail"));
529 &mov (&DWP(0,"edi"),"edx");
530 &lea ("edi",&DWP(4,"edi"));
533 &jz (&label("done"));
535 &jmp (&label("loop"));
537 &set_label("tail",16);
538 &mov (&BP(0,"edi"),"dl");
539 &lea ("edi",&DWP(1,"edi"));
543 &jnz (&label("tail"));
549 &function_end_B("OPENSSL_ia32_${rdop}_bytes");
551 &gen_random("rdrand");
552 &gen_random("rdseed");
554 &initseg("OPENSSL_cpuid_setup");
556 &hidden("OPENSSL_cpuid_setup");
557 &hidden("OPENSSL_ia32cap_P");