3 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
4 push(@INC, "${dir}perlasm", "perlasm");
7 &asm_init($ARGV[0],"x86cpuid");
9 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
11 &function_begin("OPENSSL_ia32_cpuid");
24 &jnc (&label("nocpuid"));
25 &mov ("esi",&wparam(0));
26 &mov (&DWP(8,"esi"),"eax"); # clear 3rd word
28 &mov ("edi","eax"); # max value for standard query level
31 &cmp ("ebx",0x756e6547); # "Genu"
34 &cmp ("edx",0x49656e69); # "ineI"
37 &cmp ("ecx",0x6c65746e); # "ntel"
39 &or ("ebp","eax"); # 0 indicates Intel CPU
40 &jz (&label("intel"));
42 &cmp ("ebx",0x68747541); # "Auth"
45 &cmp ("edx",0x69746E65); # "enti"
48 &cmp ("ecx",0x444D4163); # "cAMD"
50 &or ("esi","eax"); # 0 indicates AMD CPU
51 &jnz (&label("intel"));
54 &mov ("eax",0x80000000);
56 &cmp ("eax",0x80000001);
57 &jb (&label("intel"));
59 &mov ("eax",0x80000001);
62 &and ("ebp",1<<11|1); # isolate XOP bit
63 &cmp ("esi",0x80000008);
64 &jb (&label("intel"));
66 &mov ("eax",0x80000008);
68 &movz ("esi",&LB("ecx")); # number of cores - 1
69 &inc ("esi"); # number of cores
74 &jnc (&label("generic"));
78 &ja (&label("generic"));
79 &and ("edx",0xefffffff); # clear hyper-threading bit
80 &jmp (&label("generic"));
85 &jb (&label("nocacheinfo"));
88 &mov ("ecx",0); # query L1D
92 &and ("edi",0xfff); # number of cores -1 per L1D
95 &jb (&label("nocacheinfo"));
97 &mov ("esi",&wparam(0));
101 &mov (&DWP(8,"esi"),"ebx");
103 &set_label("nocacheinfo");
106 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
108 &jne (&label("notintel"));
109 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
110 &and (&HB("eax"),15); # familiy ID
111 &cmp (&HB("eax"),15); # P4?
112 &jne (&label("notintel"));
113 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
114 &set_label("notintel");
115 &bt ("edx",28); # test hyper-threading bit
116 &jnc (&label("generic"));
117 &and ("edx",0xefffffff);
119 &je (&label("generic"));
121 &or ("edx",0x10000000);
124 &ja (&label("generic"));
125 &and ("edx",0xefffffff); # clear hyper-threading bit if not
127 &set_label("generic");
128 &and ("ebp",1<<11); # isolate AMD XOP flag
129 &and ("ecx",0xfffff7ff); # force 11th bit to 0
131 &or ("ebp","ecx"); # merge AMD XOP flag
133 &bt ("ecx",27); # check OSXSAVE bit
134 &jnc (&label("clear_avx"));
136 &data_byte(0x0f,0x01,0xd0); # xgetbv
139 &je (&label("done"));
141 &je (&label("clear_avx"));
142 &set_label("clear_xmm");
143 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
144 &and ("esi",0xfeffffff); # clear FXSR
145 &set_label("clear_avx");
146 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
147 &mov ("edi",&wparam(0));
148 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
152 &set_label("nocpuid");
153 &function_end("OPENSSL_ia32_cpuid");
155 &external_label("OPENSSL_ia32cap_P");
157 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
160 &picmeup("ecx","OPENSSL_ia32cap_P");
161 &bt (&DWP(0,"ecx"),4);
162 &jnc (&label("notsc"));
166 &function_end_B("OPENSSL_rdtsc");
168 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
169 # but it's safe to call it on any [supported] 32-bit platform...
170 # Just check for [non-]zero return value...
171 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
172 &picmeup("ecx","OPENSSL_ia32cap_P");
173 &bt (&DWP(0,"ecx"),4);
174 &jnc (&label("nohalt")); # no TSC
176 &data_word(0x9058900e); # push %cs; pop %eax
178 &jnz (&label("nohalt")); # not enough privileges
183 &jnc (&label("nohalt")); # interrupts are disabled
191 &sub ("eax",&DWP(0,"esp"));
192 &sbb ("edx",&DWP(4,"esp"));
196 &set_label("nohalt");
200 &function_end_B("OPENSSL_instrument_halt");
202 # Essentially there is only one use for this function. Under DJGPP:
206 # i=OPENSSL_far_spin(_dos_ds,0x46c);
208 # to obtain the number of spins till closest timer interrupt.
210 &function_begin_B("OPENSSL_far_spin");
214 &jnc (&label("nospin")); # interrupts are disabled
216 &mov ("eax",&DWP(4,"esp"));
217 &mov ("ecx",&DWP(8,"esp"));
218 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
220 &mov ("edx",&DWP(0,"ecx"));
221 &jmp (&label("spin"));
226 &cmp ("edx",&DWP(0,"ecx"));
227 &je (&label("spin"));
229 &data_word (0x1f909090); # pop %ds
232 &set_label("nospin");
236 &function_end_B("OPENSSL_far_spin");
238 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
241 &picmeup("ecx","OPENSSL_ia32cap_P");
242 &mov ("ecx",&DWP(0,"ecx"));
243 &bt (&DWP(0,"ecx"),1);
244 &jnc (&label("no_x87"));
246 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
247 &cmp ("ecx",1<<26|1<<24);
248 &jne (&label("no_sse2"));
249 &pxor ("xmm0","xmm0");
250 &pxor ("xmm1","xmm1");
251 &pxor ("xmm2","xmm2");
252 &pxor ("xmm3","xmm3");
253 &pxor ("xmm4","xmm4");
254 &pxor ("xmm5","xmm5");
255 &pxor ("xmm6","xmm6");
256 &pxor ("xmm7","xmm7");
257 &set_label("no_sse2");
259 # just a bunch of fldz to zap the fp/mm bank followed by finit...
260 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
261 &set_label("no_x87");
262 &lea ("eax",&DWP(4,"esp"));
264 &function_end_B("OPENSSL_wipe_cpu");
266 &function_begin_B("OPENSSL_atomic_add");
267 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
268 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
271 &mov ("eax",&DWP(0,"edx"));
273 &lea ("ebx",&DWP(0,"eax","ecx"));
275 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
276 &jne (&label("spin"));
277 &mov ("eax","ebx"); # OpenSSL expects the new value
280 &function_end_B("OPENSSL_atomic_add");
282 # This function can become handy under Win32 in situations when
283 # we don't know which calling convention, __stdcall or __cdecl(*),
284 # indirect callee is using. In C it can be deployed as
286 #ifdef OPENSSL_CPUID_OBJ
287 # type OPENSSL_indirect_call(void *f,...);
289 # OPENSSL_indirect_call(func,[up to $max arguments]);
292 # (*) it's designed to work even for __fastcall if number of
293 # arguments is 1 or 2!
294 &function_begin_B("OPENSSL_indirect_call");
296 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
297 # in order to preserve eventual
302 &mov ("ecx",&DWP(12,"ebp"));
303 &mov (&DWP(0,"esp"),"ecx");
304 &mov ("edx",&DWP(16,"ebp"));
305 &mov (&DWP(4,"esp"),"edx");
306 for($i=2;$i<$max;$i++)
308 # Some copies will be redundant/bogus...
309 &mov ("eax",&DWP(12+$i*4,"ebp"));
310 &mov (&DWP(0+$i*4,"esp"),"eax");
312 &call_ptr (&DWP(8,"ebp"));# make the call...
313 &mov ("esp","ebp"); # ... and just restore the stack pointer
314 # without paying attention to what we called,
315 # (__cdecl *func) or (__stdcall *one).
319 &function_end_B("OPENSSL_indirect_call");
321 &function_begin_B("OPENSSL_cleanse");
322 &mov ("edx",&wparam(0));
323 &mov ("ecx",&wparam(1));
326 &jae (&label("lot"));
329 &set_label("little");
330 &mov (&BP(0,"edx"),"al");
332 &lea ("edx",&DWP(1,"edx"));
333 &jnz (&label("little"));
337 &set_label("lot",16);
339 &jz (&label("aligned"));
340 &mov (&BP(0,"edx"),"al");
341 &lea ("ecx",&DWP(-1,"ecx"));
342 &lea ("edx",&DWP(1,"edx"));
343 &jmp (&label("lot"));
344 &set_label("aligned");
345 &mov (&DWP(0,"edx"),"eax");
346 &lea ("ecx",&DWP(-4,"ecx"));
348 &lea ("edx",&DWP(4,"edx"));
349 &jnz (&label("aligned"));
351 &jne (&label("little"));
353 &function_end_B("OPENSSL_cleanse");
356 my $lasttick = "esi";
357 my $lastdiff = "ebx";
362 &function_begin("OPENSSL_instrument_bus");
365 &picmeup("edx","OPENSSL_ia32cap_P");
366 &bt (&DWP(0,"edx"),4);
367 &jnc (&label("nogo")); # no TSC
368 &bt (&DWP(0,"edx"),19);
369 &jnc (&label("nogo")); # no CLFLUSH
371 &mov ($out,&wparam(0)); # load arguments
372 &mov ($cnt,&wparam(1));
376 &mov ($lasttick,"eax"); # lasttick = tick
377 &mov ($lastdiff,0); # lastdiff = 0
378 &clflush(&DWP(0,$out));
379 &data_byte(0xf0); # lock
380 &add (&DWP(0,$out),$lastdiff);
381 &jmp (&label("loop"));
383 &set_label("loop",16);
385 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
386 &sub ("eax",$lasttick); # diff
387 &mov ($lasttick,"edx"); # lasttick = tick
388 &mov ($lastdiff,"eax"); # lastdiff = diff
389 &clflush(&DWP(0,$out));
390 &data_byte(0xf0); # lock
391 &add (&DWP(0,$out),"eax"); # accumulate diff
392 &lea ($out,&DWP(4,$out)); # ++$out
393 &sub ($cnt,1); # --$cnt
394 &jnz (&label("loop"));
396 &mov ("eax",&wparam(1));
399 &function_end("OPENSSL_instrument_bus");
401 &function_begin("OPENSSL_instrument_bus2");
404 &picmeup("edx","OPENSSL_ia32cap_P");
405 &bt (&DWP(0,"edx"),4);
406 &jnc (&label("nogo")); # no TSC
407 &bt (&DWP(0,"edx"),19);
408 &jnc (&label("nogo")); # no CLFLUSH
410 &mov ($out,&wparam(0)); # load arguments
411 &mov ($cnt,&wparam(1));
412 &mov ($max,&wparam(2));
414 &rdtsc (); # collect 1st tick
415 &mov ($lasttick,"eax"); # lasttick = tick
416 &mov ($lastdiff,0); # lastdiff = 0
418 &clflush(&DWP(0,$out));
419 &data_byte(0xf0); # lock
420 &add (&DWP(0,$out),$lastdiff);
422 &rdtsc (); # collect 1st diff
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 &jmp (&label("loop2"));
429 &set_label("loop2",16);
430 &clflush(&DWP(0,$out));
431 &data_byte(0xf0); # lock
432 &add (&DWP(0,$out),"eax"); # accumulate diff
435 &jz (&label("done2"));
438 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
439 &sub ("eax",$lasttick); # diff
440 &mov ($lasttick,"edx"); # lasttick = tick
441 &cmp ("eax",$lastdiff);
442 &mov ($lastdiff,"eax"); # lastdiff = diff
445 &sub ($cnt,"edx"); # conditional --$cnt
446 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
447 &jnz (&label("loop2"));
450 &mov ("eax",&wparam(1));
454 &function_end("OPENSSL_instrument_bus2");
457 &function_begin_B("OPENSSL_ia32_rdrand");
461 &jc (&label("break"));
462 &loop (&label("loop"));
465 &cmove ("eax","ecx");
467 &function_end_B("OPENSSL_ia32_rdrand");
469 &initseg("OPENSSL_cpuid_setup");
471 &hidden("OPENSSL_cpuid_setup");
472 &hidden("OPENSSL_ia32cap_P");