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
75 &jnc (&label("generic"));
79 &ja (&label("generic"));
80 &and ("edx",0xefffffff); # clear hyper-threading bit
81 &jmp (&label("generic"));
86 &jb (&label("nocacheinfo"));
89 &mov ("ecx",0); # query L1D
93 &and ("edi",0xfff); # number of cores -1 per L1D
96 &jb (&label("nocacheinfo"));
98 &mov ("esi",&wparam(0));
102 &mov (&DWP(8,"esi"),"ebx");
104 &set_label("nocacheinfo");
108 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
110 &jne (&label("notintel"));
111 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
112 &and (&HB("eax"),15); # familiy ID
113 &cmp (&HB("eax"),15); # P4?
114 &jne (&label("notintel"));
115 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
116 &set_label("notintel");
117 &bt ("edx",28); # test hyper-threading bit
118 &jnc (&label("generic"));
119 &and ("edx",0xefffffff);
121 &je (&label("generic"));
123 &or ("edx",0x10000000);
126 &ja (&label("generic"));
127 &and ("edx",0xefffffff); # clear hyper-threading bit if not
129 &set_label("generic");
130 &and ("ebp",1<<11); # isolate AMD XOP flag
131 &and ("ecx",0xfffff7ff); # force 11th bit to 0
133 &or ("ebp","ecx"); # merge AMD XOP flag
135 &bt ("ecx",27); # check OSXSAVE bit
136 &jnc (&label("clear_avx"));
138 &data_byte(0x0f,0x01,0xd0); # xgetbv
141 &je (&label("done"));
143 &je (&label("clear_avx"));
144 &set_label("clear_xmm");
145 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
146 &and ("esi",0xfeffffff); # clear FXSR
147 &set_label("clear_avx");
148 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
149 &mov ("edi",&wparam(0));
150 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
154 &set_label("nocpuid");
155 &function_end("OPENSSL_ia32_cpuid");
157 &external_label("OPENSSL_ia32cap_P");
159 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
162 &picmeup("ecx","OPENSSL_ia32cap_P");
163 &bt (&DWP(0,"ecx"),4);
164 &jnc (&label("notsc"));
168 &function_end_B("OPENSSL_rdtsc");
170 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
171 # but it's safe to call it on any [supported] 32-bit platform...
172 # Just check for [non-]zero return value...
173 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
174 &picmeup("ecx","OPENSSL_ia32cap_P");
175 &bt (&DWP(0,"ecx"),4);
176 &jnc (&label("nohalt")); # no TSC
178 &data_word(0x9058900e); # push %cs; pop %eax
180 &jnz (&label("nohalt")); # not enough privileges
185 &jnc (&label("nohalt")); # interrupts are disabled
193 &sub ("eax",&DWP(0,"esp"));
194 &sbb ("edx",&DWP(4,"esp"));
198 &set_label("nohalt");
202 &function_end_B("OPENSSL_instrument_halt");
204 # Essentially there is only one use for this function. Under DJGPP:
208 # i=OPENSSL_far_spin(_dos_ds,0x46c);
210 # to obtain the number of spins till closest timer interrupt.
212 &function_begin_B("OPENSSL_far_spin");
216 &jnc (&label("nospin")); # interrupts are disabled
218 &mov ("eax",&DWP(4,"esp"));
219 &mov ("ecx",&DWP(8,"esp"));
220 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
222 &mov ("edx",&DWP(0,"ecx"));
223 &jmp (&label("spin"));
228 &cmp ("edx",&DWP(0,"ecx"));
229 &je (&label("spin"));
231 &data_word (0x1f909090); # pop %ds
234 &set_label("nospin");
238 &function_end_B("OPENSSL_far_spin");
240 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
243 &picmeup("ecx","OPENSSL_ia32cap_P");
244 &mov ("ecx",&DWP(0,"ecx"));
245 &bt (&DWP(0,"ecx"),1);
246 &jnc (&label("no_x87"));
248 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
249 &cmp ("ecx",1<<26|1<<24);
250 &jne (&label("no_sse2"));
251 &pxor ("xmm0","xmm0");
252 &pxor ("xmm1","xmm1");
253 &pxor ("xmm2","xmm2");
254 &pxor ("xmm3","xmm3");
255 &pxor ("xmm4","xmm4");
256 &pxor ("xmm5","xmm5");
257 &pxor ("xmm6","xmm6");
258 &pxor ("xmm7","xmm7");
259 &set_label("no_sse2");
261 # just a bunch of fldz to zap the fp/mm bank followed by finit...
262 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
263 &set_label("no_x87");
264 &lea ("eax",&DWP(4,"esp"));
266 &function_end_B("OPENSSL_wipe_cpu");
268 &function_begin_B("OPENSSL_atomic_add");
269 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
270 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
273 &mov ("eax",&DWP(0,"edx"));
275 &lea ("ebx",&DWP(0,"eax","ecx"));
277 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
278 &jne (&label("spin"));
279 &mov ("eax","ebx"); # OpenSSL expects the new value
282 &function_end_B("OPENSSL_atomic_add");
284 # This function can become handy under Win32 in situations when
285 # we don't know which calling convention, __stdcall or __cdecl(*),
286 # indirect callee is using. In C it can be deployed as
288 #ifdef OPENSSL_CPUID_OBJ
289 # type OPENSSL_indirect_call(void *f,...);
291 # OPENSSL_indirect_call(func,[up to $max arguments]);
294 # (*) it's designed to work even for __fastcall if number of
295 # arguments is 1 or 2!
296 &function_begin_B("OPENSSL_indirect_call");
298 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
299 # in order to preserve eventual
304 &mov ("ecx",&DWP(12,"ebp"));
305 &mov (&DWP(0,"esp"),"ecx");
306 &mov ("edx",&DWP(16,"ebp"));
307 &mov (&DWP(4,"esp"),"edx");
308 for($i=2;$i<$max;$i++)
310 # Some copies will be redundant/bogus...
311 &mov ("eax",&DWP(12+$i*4,"ebp"));
312 &mov (&DWP(0+$i*4,"esp"),"eax");
314 &call_ptr (&DWP(8,"ebp"));# make the call...
315 &mov ("esp","ebp"); # ... and just restore the stack pointer
316 # without paying attention to what we called,
317 # (__cdecl *func) or (__stdcall *one).
321 &function_end_B("OPENSSL_indirect_call");
323 &function_begin_B("OPENSSL_cleanse");
324 &mov ("edx",&wparam(0));
325 &mov ("ecx",&wparam(1));
328 &jae (&label("lot"));
331 &set_label("little");
332 &mov (&BP(0,"edx"),"al");
334 &lea ("edx",&DWP(1,"edx"));
335 &jnz (&label("little"));
339 &set_label("lot",16);
341 &jz (&label("aligned"));
342 &mov (&BP(0,"edx"),"al");
343 &lea ("ecx",&DWP(-1,"ecx"));
344 &lea ("edx",&DWP(1,"edx"));
345 &jmp (&label("lot"));
346 &set_label("aligned");
347 &mov (&DWP(0,"edx"),"eax");
348 &lea ("ecx",&DWP(-4,"ecx"));
350 &lea ("edx",&DWP(4,"edx"));
351 &jnz (&label("aligned"));
353 &jne (&label("little"));
355 &function_end_B("OPENSSL_cleanse");
358 my $lasttick = "esi";
359 my $lastdiff = "ebx";
364 &function_begin("OPENSSL_instrument_bus");
367 &picmeup("edx","OPENSSL_ia32cap_P");
368 &bt (&DWP(0,"edx"),4);
369 &jnc (&label("nogo")); # no TSC
370 &bt (&DWP(0,"edx"),19);
371 &jnc (&label("nogo")); # no CLFLUSH
373 &mov ($out,&wparam(0)); # load arguments
374 &mov ($cnt,&wparam(1));
378 &mov ($lasttick,"eax"); # lasttick = tick
379 &mov ($lastdiff,0); # lastdiff = 0
380 &clflush(&DWP(0,$out));
381 &data_byte(0xf0); # lock
382 &add (&DWP(0,$out),$lastdiff);
383 &jmp (&label("loop"));
385 &set_label("loop",16);
387 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
388 &sub ("eax",$lasttick); # diff
389 &mov ($lasttick,"edx"); # lasttick = tick
390 &mov ($lastdiff,"eax"); # lastdiff = diff
391 &clflush(&DWP(0,$out));
392 &data_byte(0xf0); # lock
393 &add (&DWP(0,$out),"eax"); # accumulate diff
394 &lea ($out,&DWP(4,$out)); # ++$out
395 &sub ($cnt,1); # --$cnt
396 &jnz (&label("loop"));
398 &mov ("eax",&wparam(1));
401 &function_end("OPENSSL_instrument_bus");
403 &function_begin("OPENSSL_instrument_bus2");
406 &picmeup("edx","OPENSSL_ia32cap_P");
407 &bt (&DWP(0,"edx"),4);
408 &jnc (&label("nogo")); # no TSC
409 &bt (&DWP(0,"edx"),19);
410 &jnc (&label("nogo")); # no CLFLUSH
412 &mov ($out,&wparam(0)); # load arguments
413 &mov ($cnt,&wparam(1));
414 &mov ($max,&wparam(2));
416 &rdtsc (); # collect 1st tick
417 &mov ($lasttick,"eax"); # lasttick = tick
418 &mov ($lastdiff,0); # lastdiff = 0
420 &clflush(&DWP(0,$out));
421 &data_byte(0xf0); # lock
422 &add (&DWP(0,$out),$lastdiff);
424 &rdtsc (); # collect 1st diff
425 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
426 &sub ("eax",$lasttick); # diff
427 &mov ($lasttick,"edx"); # lasttick = tick
428 &mov ($lastdiff,"eax"); # lastdiff = diff
429 &jmp (&label("loop2"));
431 &set_label("loop2",16);
432 &clflush(&DWP(0,$out));
433 &data_byte(0xf0); # lock
434 &add (&DWP(0,$out),"eax"); # accumulate diff
437 &jz (&label("done2"));
440 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
441 &sub ("eax",$lasttick); # diff
442 &mov ($lasttick,"edx"); # lasttick = tick
443 &cmp ("eax",$lastdiff);
444 &mov ($lastdiff,"eax"); # lastdiff = diff
447 &sub ($cnt,"edx"); # conditional --$cnt
448 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
449 &jnz (&label("loop2"));
452 &mov ("eax",&wparam(1));
456 &function_end("OPENSSL_instrument_bus2");
459 &function_begin_B("OPENSSL_ia32_rdrand");
463 &jc (&label("break"));
464 &loop (&label("loop"));
467 &cmove ("eax","ecx");
469 &function_end_B("OPENSSL_ia32_rdrand");
471 &initseg("OPENSSL_cpuid_setup");
473 &hidden("OPENSSL_cpuid_setup");
474 &hidden("OPENSSL_ia32cap_P");