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",26); # check XSAVE bit
123 &jnc (&label("done"));
124 &bt ("ecx",27); # check OSXSAVE bit
125 &jnc (&label("clear_xmm"));
127 &data_byte(0x0f,0x01,0xd0); # xgetbv
130 &je (&label("done"));
132 &je (&label("clear_avx"));
133 &set_label("clear_xmm");
134 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
135 &and ("esi",0xfeffffff); # clear FXSR
136 &set_label("clear_avx");
137 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
141 &function_end("OPENSSL_ia32_cpuid");
143 &external_label("OPENSSL_ia32cap_P");
145 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
148 &picmeup("ecx","OPENSSL_ia32cap_P");
149 &bt (&DWP(0,"ecx"),4);
150 &jnc (&label("notsc"));
154 &function_end_B("OPENSSL_rdtsc");
156 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
157 # but it's safe to call it on any [supported] 32-bit platform...
158 # Just check for [non-]zero return value...
159 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
160 &picmeup("ecx","OPENSSL_ia32cap_P");
161 &bt (&DWP(0,"ecx"),4);
162 &jnc (&label("nohalt")); # no TSC
164 &data_word(0x9058900e); # push %cs; pop %eax
166 &jnz (&label("nohalt")); # not enough privileges
171 &jnc (&label("nohalt")); # interrupts are disabled
179 &sub ("eax",&DWP(0,"esp"));
180 &sbb ("edx",&DWP(4,"esp"));
184 &set_label("nohalt");
188 &function_end_B("OPENSSL_instrument_halt");
190 # Essentially there is only one use for this function. Under DJGPP:
194 # i=OPENSSL_far_spin(_dos_ds,0x46c);
196 # to obtain the number of spins till closest timer interrupt.
198 &function_begin_B("OPENSSL_far_spin");
202 &jnc (&label("nospin")); # interrupts are disabled
204 &mov ("eax",&DWP(4,"esp"));
205 &mov ("ecx",&DWP(8,"esp"));
206 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
208 &mov ("edx",&DWP(0,"ecx"));
209 &jmp (&label("spin"));
214 &cmp ("edx",&DWP(0,"ecx"));
215 &je (&label("spin"));
217 &data_word (0x1f909090); # pop %ds
220 &set_label("nospin");
224 &function_end_B("OPENSSL_far_spin");
226 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
229 &picmeup("ecx","OPENSSL_ia32cap_P");
230 &mov ("ecx",&DWP(0,"ecx"));
231 &bt (&DWP(0,"ecx"),1);
232 &jnc (&label("no_x87"));
234 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
235 &cmp ("ecx",1<<26|1<<24);
236 &jne (&label("no_sse2"));
237 &pxor ("xmm0","xmm0");
238 &pxor ("xmm1","xmm1");
239 &pxor ("xmm2","xmm2");
240 &pxor ("xmm3","xmm3");
241 &pxor ("xmm4","xmm4");
242 &pxor ("xmm5","xmm5");
243 &pxor ("xmm6","xmm6");
244 &pxor ("xmm7","xmm7");
245 &set_label("no_sse2");
247 # just a bunch of fldz to zap the fp/mm bank followed by finit...
248 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
249 &set_label("no_x87");
250 &lea ("eax",&DWP(4,"esp"));
252 &function_end_B("OPENSSL_wipe_cpu");
254 &function_begin_B("OPENSSL_atomic_add");
255 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
256 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
259 &mov ("eax",&DWP(0,"edx"));
261 &lea ("ebx",&DWP(0,"eax","ecx"));
263 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
264 &jne (&label("spin"));
265 &mov ("eax","ebx"); # OpenSSL expects the new value
268 &function_end_B("OPENSSL_atomic_add");
270 # This function can become handy under Win32 in situations when
271 # we don't know which calling convention, __stdcall or __cdecl(*),
272 # indirect callee is using. In C it can be deployed as
274 #ifdef OPENSSL_CPUID_OBJ
275 # type OPENSSL_indirect_call(void *f,...);
277 # OPENSSL_indirect_call(func,[up to $max arguments]);
280 # (*) it's designed to work even for __fastcall if number of
281 # arguments is 1 or 2!
282 &function_begin_B("OPENSSL_indirect_call");
284 my $i,$max=7; # $max has to be chosen as 4*n-1
285 # in order to preserve eventual
290 &mov ("ecx",&DWP(12,"ebp"));
291 &mov (&DWP(0,"esp"),"ecx");
292 &mov ("edx",&DWP(16,"ebp"));
293 &mov (&DWP(4,"esp"),"edx");
294 for($i=2;$i<$max;$i++)
296 # Some copies will be redundant/bogus...
297 &mov ("eax",&DWP(12+$i*4,"ebp"));
298 &mov (&DWP(0+$i*4,"esp"),"eax");
300 &call_ptr (&DWP(8,"ebp"));# make the call...
301 &mov ("esp","ebp"); # ... and just restore the stack pointer
302 # without paying attention to what we called,
303 # (__cdecl *func) or (__stdcall *one).
307 &function_end_B("OPENSSL_indirect_call");
309 &function_begin_B("OPENSSL_cleanse");
310 &mov ("edx",&wparam(0));
311 &mov ("ecx",&wparam(1));
314 &jae (&label("lot"));
317 &set_label("little");
318 &mov (&BP(0,"edx"),"al");
320 &lea ("edx",&DWP(1,"edx"));
321 &jnz (&label("little"));
325 &set_label("lot",16);
327 &jz (&label("aligned"));
328 &mov (&BP(0,"edx"),"al");
329 &lea ("ecx",&DWP(-1,"ecx"));
330 &lea ("edx",&DWP(1,"edx"));
331 &jmp (&label("lot"));
332 &set_label("aligned");
333 &mov (&DWP(0,"edx"),"eax");
334 &lea ("ecx",&DWP(-4,"ecx"));
336 &lea ("edx",&DWP(4,"edx"));
337 &jnz (&label("aligned"));
339 &jne (&label("little"));
341 &function_end_B("OPENSSL_cleanse");
344 my $lasttick = "esi";
345 my $lastdiff = "ebx";
350 &function_begin("OPENSSL_instrument_bus");
353 &picmeup("edx","OPENSSL_ia32cap_P");
354 &bt (&DWP(0,"edx"),4);
355 &jnc (&label("nogo")); # no TSC
356 &bt (&DWP(0,"edx"),19);
357 &jnc (&label("nogo")); # no CLFLUSH
359 &mov ($out,&wparam(0)); # load arguments
360 &mov ($cnt,&wparam(1));
364 &mov ($lasttick,"eax"); # lasttick = tick
365 &mov ($lastdiff,0); # lastdiff = 0
366 &clflush(&DWP(0,$out));
367 &data_byte(0xf0); # lock
368 &add (&DWP(0,$out),$lastdiff);
369 &jmp (&label("loop"));
371 &set_label("loop",16);
373 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
374 &sub ("eax",$lasttick); # diff
375 &mov ($lasttick,"edx"); # lasttick = tick
376 &mov ($lastdiff,"eax"); # lastdiff = diff
377 &clflush(&DWP(0,$out));
378 &data_byte(0xf0); # lock
379 &add (&DWP(0,$out),"eax"); # accumulate diff
380 &lea ($out,&DWP(4,$out)); # ++$out
381 &sub ($cnt,1); # --$cnt
382 &jnz (&label("loop"));
384 &mov ("eax",&wparam(1));
387 &function_end("OPENSSL_instrument_bus");
389 &function_begin("OPENSSL_instrument_bus2");
392 &picmeup("edx","OPENSSL_ia32cap_P");
393 &bt (&DWP(0,"edx"),4);
394 &jnc (&label("nogo")); # no TSC
395 &bt (&DWP(0,"edx"),19);
396 &jnc (&label("nogo")); # no CLFLUSH
398 &mov ($out,&wparam(0)); # load arguments
399 &mov ($cnt,&wparam(1));
400 &mov ($max,&wparam(2));
402 &rdtsc (); # collect 1st tick
403 &mov ($lasttick,"eax"); # lasttick = tick
404 &mov ($lastdiff,0); # lastdiff = 0
406 &clflush(&DWP(0,$out));
407 &data_byte(0xf0); # lock
408 &add (&DWP(0,$out),$lastdiff);
410 &rdtsc (); # collect 1st diff
411 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
412 &sub ("eax",$lasttick); # diff
413 &mov ($lasttick,"edx"); # lasttick = tick
414 &mov ($lastdiff,"eax"); # lastdiff = diff
415 &jmp (&label("loop2"));
417 &set_label("loop2",16);
418 &clflush(&DWP(0,$out));
419 &data_byte(0xf0); # lock
420 &add (&DWP(0,$out),"eax"); # accumulate diff
423 &jz (&label("done2"));
426 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
427 &sub ("eax",$lasttick); # diff
428 &mov ($lasttick,"edx"); # lasttick = tick
429 &cmp ("eax",$lastdiff);
430 &mov ($lastdiff,"eax"); # lastdiff = diff
433 &sub ($cnt,"edx"); # conditional --$cnt
434 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
435 &jnz (&label("loop2"));
438 &mov ("eax",&wparam(1));
442 &function_end("OPENSSL_instrument_bus2");
445 &function_begin_B("OPENSSL_ia32_rdrand");
449 &jc (&label("break"));
450 &loop (&label("loop"));
453 &cmove ("eax","ecx");
455 &function_end_B("OPENSSL_ia32_rdrand");
457 &initseg("OPENSSL_cpuid_setup");