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");
23 &jnc (&label("generic"));
26 &mov ("edi","eax"); # max value for standard query level
29 &cmp ("ebx",0x756e6547); # "Genu"
32 &cmp ("edx",0x49656e69); # "ineI"
35 &cmp ("ecx",0x6c65746e); # "ntel"
37 &or ("ebp","eax"); # 0 indicates Intel CPU
38 &jz (&label("intel"));
40 &cmp ("ebx",0x68747541); # "Auth"
43 &cmp ("edx",0x69746E65); # "enti"
46 &cmp ("ecx",0x444D4163); # "cAMD"
48 &or ("esi","eax"); # 0 indicates AMD CPU
49 &jnz (&label("intel"));
52 &mov ("eax",0x80000000);
54 &cmp ("eax",0x80000001);
55 &jb (&label("intel"));
57 &mov ("eax",0x80000001);
60 &and ("ebp",1<<11|1); # isolate XOP bit
61 &cmp ("esi",0x80000008);
62 &jb (&label("intel"));
64 &mov ("eax",0x80000008);
66 &movz ("esi",&LB("ecx")); # number of cores - 1
67 &inc ("esi"); # number of cores
72 &jnc (&label("generic"));
76 &ja (&label("generic"));
77 &and ("edx",0xefffffff); # clear hyper-threading bit
78 &jmp (&label("generic"));
83 &jb (&label("nocacheinfo"));
86 &mov ("ecx",0); # query L1D
90 &and ("edi",0xfff); # number of cores -1 per L1D
92 &set_label("nocacheinfo");
95 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
97 &jne (&label("notintel"));
98 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
99 &and (&HB("eax"),15); # familiy ID
100 &cmp (&HB("eax"),15); # P4?
101 &jne (&label("notintel"));
102 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
103 &set_label("notintel");
104 &bt ("edx",28); # test hyper-threading bit
105 &jnc (&label("generic"));
106 &and ("edx",0xefffffff);
108 &je (&label("generic"));
110 &or ("edx",0x10000000);
113 &ja (&label("generic"));
114 &and ("edx",0xefffffff); # clear hyper-threading bit if not
116 &set_label("generic");
117 &and ("ebp",1<<11); # isolate AMD XOP flag
118 &and ("ecx",0xfffff7ff); # force 11th bit to 0
120 &or ("ebp","ecx"); # merge AMD XOP flag
122 &bt ("ecx",27); # check OSXSAVE bit
123 &jnc (&label("clear_avx"));
125 &data_byte(0x0f,0x01,0xd0); # xgetbv
128 &je (&label("done"));
130 &je (&label("clear_avx"));
131 &set_label("clear_xmm");
132 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
133 &and ("esi",0xfeffffff); # clear FXSR
134 &set_label("clear_avx");
135 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
139 &function_end("OPENSSL_ia32_cpuid");
141 &external_label("OPENSSL_ia32cap_P");
143 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
146 &picmeup("ecx","OPENSSL_ia32cap_P");
147 &bt (&DWP(0,"ecx"),4);
148 &jnc (&label("notsc"));
152 &function_end_B("OPENSSL_rdtsc");
154 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
155 # but it's safe to call it on any [supported] 32-bit platform...
156 # Just check for [non-]zero return value...
157 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
158 &picmeup("ecx","OPENSSL_ia32cap_P");
159 &bt (&DWP(0,"ecx"),4);
160 &jnc (&label("nohalt")); # no TSC
162 &data_word(0x9058900e); # push %cs; pop %eax
164 &jnz (&label("nohalt")); # not enough privileges
169 &jnc (&label("nohalt")); # interrupts are disabled
177 &sub ("eax",&DWP(0,"esp"));
178 &sbb ("edx",&DWP(4,"esp"));
182 &set_label("nohalt");
186 &function_end_B("OPENSSL_instrument_halt");
188 # Essentially there is only one use for this function. Under DJGPP:
192 # i=OPENSSL_far_spin(_dos_ds,0x46c);
194 # to obtain the number of spins till closest timer interrupt.
196 &function_begin_B("OPENSSL_far_spin");
200 &jnc (&label("nospin")); # interrupts are disabled
202 &mov ("eax",&DWP(4,"esp"));
203 &mov ("ecx",&DWP(8,"esp"));
204 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
206 &mov ("edx",&DWP(0,"ecx"));
207 &jmp (&label("spin"));
212 &cmp ("edx",&DWP(0,"ecx"));
213 &je (&label("spin"));
215 &data_word (0x1f909090); # pop %ds
218 &set_label("nospin");
222 &function_end_B("OPENSSL_far_spin");
224 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
227 &picmeup("ecx","OPENSSL_ia32cap_P");
228 &mov ("ecx",&DWP(0,"ecx"));
229 &bt (&DWP(0,"ecx"),1);
230 &jnc (&label("no_x87"));
232 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
233 &cmp ("ecx",1<<26|1<<24);
234 &jne (&label("no_sse2"));
235 &pxor ("xmm0","xmm0");
236 &pxor ("xmm1","xmm1");
237 &pxor ("xmm2","xmm2");
238 &pxor ("xmm3","xmm3");
239 &pxor ("xmm4","xmm4");
240 &pxor ("xmm5","xmm5");
241 &pxor ("xmm6","xmm6");
242 &pxor ("xmm7","xmm7");
243 &set_label("no_sse2");
245 # just a bunch of fldz to zap the fp/mm bank followed by finit...
246 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
247 &set_label("no_x87");
248 &lea ("eax",&DWP(4,"esp"));
250 &function_end_B("OPENSSL_wipe_cpu");
252 &function_begin_B("OPENSSL_atomic_add");
253 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
254 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
257 &mov ("eax",&DWP(0,"edx"));
259 &lea ("ebx",&DWP(0,"eax","ecx"));
261 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
262 &jne (&label("spin"));
263 &mov ("eax","ebx"); # OpenSSL expects the new value
266 &function_end_B("OPENSSL_atomic_add");
268 # This function can become handy under Win32 in situations when
269 # we don't know which calling convention, __stdcall or __cdecl(*),
270 # indirect callee is using. In C it can be deployed as
272 #ifdef OPENSSL_CPUID_OBJ
273 # type OPENSSL_indirect_call(void *f,...);
275 # OPENSSL_indirect_call(func,[up to $max arguments]);
278 # (*) it's designed to work even for __fastcall if number of
279 # arguments is 1 or 2!
280 &function_begin_B("OPENSSL_indirect_call");
282 my $i,$max=7; # $max has to be chosen as 4*n-1
283 # in order to preserve eventual
288 &mov ("ecx",&DWP(12,"ebp"));
289 &mov (&DWP(0,"esp"),"ecx");
290 &mov ("edx",&DWP(16,"ebp"));
291 &mov (&DWP(4,"esp"),"edx");
292 for($i=2;$i<$max;$i++)
294 # Some copies will be redundant/bogus...
295 &mov ("eax",&DWP(12+$i*4,"ebp"));
296 &mov (&DWP(0+$i*4,"esp"),"eax");
298 &call_ptr (&DWP(8,"ebp"));# make the call...
299 &mov ("esp","ebp"); # ... and just restore the stack pointer
300 # without paying attention to what we called,
301 # (__cdecl *func) or (__stdcall *one).
305 &function_end_B("OPENSSL_indirect_call");
307 &function_begin_B("OPENSSL_cleanse");
308 &mov ("edx",&wparam(0));
309 &mov ("ecx",&wparam(1));
312 &jae (&label("lot"));
315 &set_label("little");
316 &mov (&BP(0,"edx"),"al");
318 &lea ("edx",&DWP(1,"edx"));
319 &jnz (&label("little"));
323 &set_label("lot",16);
325 &jz (&label("aligned"));
326 &mov (&BP(0,"edx"),"al");
327 &lea ("ecx",&DWP(-1,"ecx"));
328 &lea ("edx",&DWP(1,"edx"));
329 &jmp (&label("lot"));
330 &set_label("aligned");
331 &mov (&DWP(0,"edx"),"eax");
332 &lea ("ecx",&DWP(-4,"ecx"));
334 &lea ("edx",&DWP(4,"edx"));
335 &jnz (&label("aligned"));
337 &jne (&label("little"));
339 &function_end_B("OPENSSL_cleanse");
342 my $lasttick = "esi";
343 my $lastdiff = "ebx";
348 &function_begin("OPENSSL_instrument_bus");
351 &picmeup("edx","OPENSSL_ia32cap_P");
352 &bt (&DWP(0,"edx"),4);
353 &jnc (&label("nogo")); # no TSC
354 &bt (&DWP(0,"edx"),19);
355 &jnc (&label("nogo")); # no CLFLUSH
357 &mov ($out,&wparam(0)); # load arguments
358 &mov ($cnt,&wparam(1));
362 &mov ($lasttick,"eax"); # lasttick = tick
363 &mov ($lastdiff,0); # lastdiff = 0
364 &clflush(&DWP(0,$out));
365 &data_byte(0xf0); # lock
366 &add (&DWP(0,$out),$lastdiff);
367 &jmp (&label("loop"));
369 &set_label("loop",16);
371 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
372 &sub ("eax",$lasttick); # diff
373 &mov ($lasttick,"edx"); # lasttick = tick
374 &mov ($lastdiff,"eax"); # lastdiff = diff
375 &clflush(&DWP(0,$out));
376 &data_byte(0xf0); # lock
377 &add (&DWP(0,$out),"eax"); # accumulate diff
378 &lea ($out,&DWP(4,$out)); # ++$out
379 &sub ($cnt,1); # --$cnt
380 &jnz (&label("loop"));
382 &mov ("eax",&wparam(1));
385 &function_end("OPENSSL_instrument_bus");
387 &function_begin("OPENSSL_instrument_bus2");
390 &picmeup("edx","OPENSSL_ia32cap_P");
391 &bt (&DWP(0,"edx"),4);
392 &jnc (&label("nogo")); # no TSC
393 &bt (&DWP(0,"edx"),19);
394 &jnc (&label("nogo")); # no CLFLUSH
396 &mov ($out,&wparam(0)); # load arguments
397 &mov ($cnt,&wparam(1));
398 &mov ($max,&wparam(2));
400 &rdtsc (); # collect 1st tick
401 &mov ($lasttick,"eax"); # lasttick = tick
402 &mov ($lastdiff,0); # lastdiff = 0
404 &clflush(&DWP(0,$out));
405 &data_byte(0xf0); # lock
406 &add (&DWP(0,$out),$lastdiff);
408 &rdtsc (); # collect 1st diff
409 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
410 &sub ("eax",$lasttick); # diff
411 &mov ($lasttick,"edx"); # lasttick = tick
412 &mov ($lastdiff,"eax"); # lastdiff = diff
413 &jmp (&label("loop2"));
415 &set_label("loop2",16);
416 &clflush(&DWP(0,$out));
417 &data_byte(0xf0); # lock
418 &add (&DWP(0,$out),"eax"); # accumulate diff
421 &jz (&label("done2"));
424 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
425 &sub ("eax",$lasttick); # diff
426 &mov ($lasttick,"edx"); # lasttick = tick
427 &cmp ("eax",$lastdiff);
428 &mov ($lastdiff,"eax"); # lastdiff = diff
431 &sub ($cnt,"edx"); # conditional --$cnt
432 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
433 &jnz (&label("loop2"));
436 &mov ("eax",&wparam(1));
440 &function_end("OPENSSL_instrument_bus2");
443 &function_begin_B("OPENSSL_ia32_rdrand");
447 &jc (&label("break"));
448 &loop (&label("loop"));
451 &cmove ("eax","ecx");
453 &function_end_B("OPENSSL_ia32_rdrand");
455 &initseg("OPENSSL_cpuid_setup");