3 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
4 push(@INC, "${dir}perlasm", "perlasm");
11 &asm_init($ARGV[0],"x86cpuid");
13 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
15 &function_begin("OPENSSL_ia32_cpuid");
28 &jnc (&label("nocpuid"));
29 &mov ("esi",&wparam(0));
30 &mov (&DWP(8,"esi"),"eax"); # clear 3rd word
32 &mov ("edi","eax"); # max value for standard query level
35 &cmp ("ebx",0x756e6547); # "Genu"
38 &cmp ("edx",0x49656e69); # "ineI"
41 &cmp ("ecx",0x6c65746e); # "ntel"
43 &or ("ebp","eax"); # 0 indicates Intel CPU
44 &jz (&label("intel"));
46 &cmp ("ebx",0x68747541); # "Auth"
49 &cmp ("edx",0x69746E65); # "enti"
52 &cmp ("ecx",0x444D4163); # "cAMD"
54 &or ("esi","eax"); # 0 indicates AMD CPU
55 &jnz (&label("intel"));
58 &mov ("eax",0x80000000);
60 &cmp ("eax",0x80000001);
61 &jb (&label("intel"));
63 &mov ("eax",0x80000001);
66 &and ("ebp",1<<11|1); # isolate XOP bit
67 &cmp ("esi",0x80000008);
68 &jb (&label("intel"));
70 &mov ("eax",0x80000008);
72 &movz ("esi",&LB("ecx")); # number of cores - 1
73 &inc ("esi"); # number of cores
79 &jnc (&label("generic"));
83 &ja (&label("generic"));
84 &and ("edx",0xefffffff); # clear hyper-threading bit
85 &jmp (&label("generic"));
89 &jb (&label("cacheinfo"));
91 &mov ("esi",&wparam(0));
95 &mov (&DWP(8,"esi"),"ebx");
97 &set_label("cacheinfo");
100 &jb (&label("nocacheinfo"));
103 &mov ("ecx",0); # query L1D
107 &and ("edi",0xfff); # number of cores -1 per L1D
109 &set_label("nocacheinfo");
113 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
115 &jne (&label("notintel"));
116 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
117 &and (&HB("eax"),15); # familiy ID
118 &cmp (&HB("eax"),15); # P4?
119 &jne (&label("notintel"));
120 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
121 &set_label("notintel");
122 &bt ("edx",28); # test hyper-threading bit
123 &jnc (&label("generic"));
124 &and ("edx",0xefffffff);
126 &je (&label("generic"));
128 &or ("edx",0x10000000);
131 &ja (&label("generic"));
132 &and ("edx",0xefffffff); # clear hyper-threading bit if not
134 &set_label("generic");
135 &and ("ebp",1<<11); # isolate AMD XOP flag
136 &and ("ecx",0xfffff7ff); # force 11th bit to 0
138 &or ("ebp","ecx"); # merge AMD XOP flag
140 &bt ("ecx",27); # check OSXSAVE bit
141 &jnc (&label("clear_avx"));
143 &data_byte(0x0f,0x01,0xd0); # xgetbv
146 &je (&label("done"));
148 &je (&label("clear_avx"));
149 &set_label("clear_xmm");
150 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
151 &and ("esi",0xfeffffff); # clear FXSR
152 &set_label("clear_avx");
153 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
154 &mov ("edi",&wparam(0));
155 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
159 &set_label("nocpuid");
160 &function_end("OPENSSL_ia32_cpuid");
162 &external_label("OPENSSL_ia32cap_P");
164 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
167 &picmeup("ecx","OPENSSL_ia32cap_P");
168 &bt (&DWP(0,"ecx"),4);
169 &jnc (&label("notsc"));
173 &function_end_B("OPENSSL_rdtsc");
175 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
176 # but it's safe to call it on any [supported] 32-bit platform...
177 # Just check for [non-]zero return value...
178 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
179 &picmeup("ecx","OPENSSL_ia32cap_P");
180 &bt (&DWP(0,"ecx"),4);
181 &jnc (&label("nohalt")); # no TSC
183 &data_word(0x9058900e); # push %cs; pop %eax
185 &jnz (&label("nohalt")); # not enough privileges
190 &jnc (&label("nohalt")); # interrupts are disabled
198 &sub ("eax",&DWP(0,"esp"));
199 &sbb ("edx",&DWP(4,"esp"));
203 &set_label("nohalt");
207 &function_end_B("OPENSSL_instrument_halt");
209 # Essentially there is only one use for this function. Under DJGPP:
213 # i=OPENSSL_far_spin(_dos_ds,0x46c);
215 # to obtain the number of spins till closest timer interrupt.
217 &function_begin_B("OPENSSL_far_spin");
221 &jnc (&label("nospin")); # interrupts are disabled
223 &mov ("eax",&DWP(4,"esp"));
224 &mov ("ecx",&DWP(8,"esp"));
225 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
227 &mov ("edx",&DWP(0,"ecx"));
228 &jmp (&label("spin"));
233 &cmp ("edx",&DWP(0,"ecx"));
234 &je (&label("spin"));
236 &data_word (0x1f909090); # pop %ds
239 &set_label("nospin");
243 &function_end_B("OPENSSL_far_spin");
245 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
248 &picmeup("ecx","OPENSSL_ia32cap_P");
249 &mov ("ecx",&DWP(0,"ecx"));
250 &bt (&DWP(0,"ecx"),1);
251 &jnc (&label("no_x87"));
253 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
254 &cmp ("ecx",1<<26|1<<24);
255 &jne (&label("no_sse2"));
256 &pxor ("xmm0","xmm0");
257 &pxor ("xmm1","xmm1");
258 &pxor ("xmm2","xmm2");
259 &pxor ("xmm3","xmm3");
260 &pxor ("xmm4","xmm4");
261 &pxor ("xmm5","xmm5");
262 &pxor ("xmm6","xmm6");
263 &pxor ("xmm7","xmm7");
264 &set_label("no_sse2");
266 # just a bunch of fldz to zap the fp/mm bank followed by finit...
267 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
268 &set_label("no_x87");
269 &lea ("eax",&DWP(4,"esp"));
271 &function_end_B("OPENSSL_wipe_cpu");
273 &function_begin_B("OPENSSL_atomic_add");
274 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
275 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
278 &mov ("eax",&DWP(0,"edx"));
280 &lea ("ebx",&DWP(0,"eax","ecx"));
282 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
283 &jne (&label("spin"));
284 &mov ("eax","ebx"); # OpenSSL expects the new value
287 &function_end_B("OPENSSL_atomic_add");
289 # This function can become handy under Win32 in situations when
290 # we don't know which calling convention, __stdcall or __cdecl(*),
291 # indirect callee is using. In C it can be deployed as
293 #ifdef OPENSSL_CPUID_OBJ
294 # type OPENSSL_indirect_call(void *f,...);
296 # OPENSSL_indirect_call(func,[up to $max arguments]);
299 # (*) it's designed to work even for __fastcall if number of
300 # arguments is 1 or 2!
301 &function_begin_B("OPENSSL_indirect_call");
303 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
304 # in order to preserve eventual
309 &mov ("ecx",&DWP(12,"ebp"));
310 &mov (&DWP(0,"esp"),"ecx");
311 &mov ("edx",&DWP(16,"ebp"));
312 &mov (&DWP(4,"esp"),"edx");
313 for($i=2;$i<$max;$i++)
315 # Some copies will be redundant/bogus...
316 &mov ("eax",&DWP(12+$i*4,"ebp"));
317 &mov (&DWP(0+$i*4,"esp"),"eax");
319 &call_ptr (&DWP(8,"ebp"));# make the call...
320 &mov ("esp","ebp"); # ... and just restore the stack pointer
321 # without paying attention to what we called,
322 # (__cdecl *func) or (__stdcall *one).
326 &function_end_B("OPENSSL_indirect_call");
328 &function_begin_B("OPENSSL_cleanse");
329 &mov ("edx",&wparam(0));
330 &mov ("ecx",&wparam(1));
333 &jae (&label("lot"));
336 &set_label("little");
337 &mov (&BP(0,"edx"),"al");
339 &lea ("edx",&DWP(1,"edx"));
340 &jnz (&label("little"));
344 &set_label("lot",16);
346 &jz (&label("aligned"));
347 &mov (&BP(0,"edx"),"al");
348 &lea ("ecx",&DWP(-1,"ecx"));
349 &lea ("edx",&DWP(1,"edx"));
350 &jmp (&label("lot"));
351 &set_label("aligned");
352 &mov (&DWP(0,"edx"),"eax");
353 &lea ("ecx",&DWP(-4,"ecx"));
355 &lea ("edx",&DWP(4,"edx"));
356 &jnz (&label("aligned"));
358 &jne (&label("little"));
360 &function_end_B("OPENSSL_cleanse");
363 my $lasttick = "esi";
364 my $lastdiff = "ebx";
369 &function_begin("OPENSSL_instrument_bus");
372 &picmeup("edx","OPENSSL_ia32cap_P");
373 &bt (&DWP(0,"edx"),4);
374 &jnc (&label("nogo")); # no TSC
375 &bt (&DWP(0,"edx"),19);
376 &jnc (&label("nogo")); # no CLFLUSH
378 &mov ($out,&wparam(0)); # load arguments
379 &mov ($cnt,&wparam(1));
383 &mov ($lasttick,"eax"); # lasttick = tick
384 &mov ($lastdiff,0); # lastdiff = 0
385 &clflush(&DWP(0,$out));
386 &data_byte(0xf0); # lock
387 &add (&DWP(0,$out),$lastdiff);
388 &jmp (&label("loop"));
390 &set_label("loop",16);
392 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
393 &sub ("eax",$lasttick); # diff
394 &mov ($lasttick,"edx"); # lasttick = tick
395 &mov ($lastdiff,"eax"); # lastdiff = diff
396 &clflush(&DWP(0,$out));
397 &data_byte(0xf0); # lock
398 &add (&DWP(0,$out),"eax"); # accumulate diff
399 &lea ($out,&DWP(4,$out)); # ++$out
400 &sub ($cnt,1); # --$cnt
401 &jnz (&label("loop"));
403 &mov ("eax",&wparam(1));
406 &function_end("OPENSSL_instrument_bus");
408 &function_begin("OPENSSL_instrument_bus2");
411 &picmeup("edx","OPENSSL_ia32cap_P");
412 &bt (&DWP(0,"edx"),4);
413 &jnc (&label("nogo")); # no TSC
414 &bt (&DWP(0,"edx"),19);
415 &jnc (&label("nogo")); # no CLFLUSH
417 &mov ($out,&wparam(0)); # load arguments
418 &mov ($cnt,&wparam(1));
419 &mov ($max,&wparam(2));
421 &rdtsc (); # collect 1st tick
422 &mov ($lasttick,"eax"); # lasttick = tick
423 &mov ($lastdiff,0); # lastdiff = 0
425 &clflush(&DWP(0,$out));
426 &data_byte(0xf0); # lock
427 &add (&DWP(0,$out),$lastdiff);
429 &rdtsc (); # collect 1st diff
430 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
431 &sub ("eax",$lasttick); # diff
432 &mov ($lasttick,"edx"); # lasttick = tick
433 &mov ($lastdiff,"eax"); # lastdiff = diff
434 &jmp (&label("loop2"));
436 &set_label("loop2",16);
437 &clflush(&DWP(0,$out));
438 &data_byte(0xf0); # lock
439 &add (&DWP(0,$out),"eax"); # accumulate diff
442 &jz (&label("done2"));
445 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
446 &sub ("eax",$lasttick); # diff
447 &mov ($lasttick,"edx"); # lasttick = tick
448 &cmp ("eax",$lastdiff);
449 &mov ($lastdiff,"eax"); # lastdiff = diff
452 &sub ($cnt,"edx"); # conditional --$cnt
453 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
454 &jnz (&label("loop2"));
457 &mov ("eax",&wparam(1));
461 &function_end("OPENSSL_instrument_bus2");
464 &function_begin_B("OPENSSL_ia32_rdrand");
468 &jc (&label("break"));
469 &loop (&label("loop"));
472 &cmove ("eax","ecx");
474 &function_end_B("OPENSSL_ia32_rdrand");
476 &function_begin_B("OPENSSL_ia32_rdseed");
480 &jc (&label("break"));
481 &loop (&label("loop"));
484 &cmove ("eax","ecx");
486 &function_end_B("OPENSSL_ia32_rdseed");
488 &initseg("OPENSSL_cpuid_setup");
490 &hidden("OPENSSL_cpuid_setup");
491 &hidden("OPENSSL_ia32cap_P");