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"));
85 &jb (&label("cacheinfo"));
87 &mov ("esi",&wparam(0));
91 &mov (&DWP(8,"esi"),"ebx");
93 &set_label("cacheinfo");
96 &jb (&label("nocacheinfo"));
99 &mov ("ecx",0); # query L1D
103 &and ("edi",0xfff); # number of cores -1 per L1D
105 &set_label("nocacheinfo");
109 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
111 &jne (&label("notintel"));
112 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
113 &and (&HB("eax"),15); # familiy ID
114 &cmp (&HB("eax"),15); # P4?
115 &jne (&label("notintel"));
116 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
117 &set_label("notintel");
118 &bt ("edx",28); # test hyper-threading bit
119 &jnc (&label("generic"));
120 &and ("edx",0xefffffff);
122 &je (&label("generic"));
124 &or ("edx",0x10000000);
127 &ja (&label("generic"));
128 &and ("edx",0xefffffff); # clear hyper-threading bit if not
130 &set_label("generic");
131 &and ("ebp",1<<11); # isolate AMD XOP flag
132 &and ("ecx",0xfffff7ff); # force 11th bit to 0
134 &or ("ebp","ecx"); # merge AMD XOP flag
136 &bt ("ecx",27); # check OSXSAVE bit
137 &jnc (&label("clear_avx"));
139 &data_byte(0x0f,0x01,0xd0); # xgetbv
142 &je (&label("done"));
144 &je (&label("clear_avx"));
145 &set_label("clear_xmm");
146 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
147 &and ("esi",0xfeffffff); # clear FXSR
148 &set_label("clear_avx");
149 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
150 &mov ("edi",&wparam(0));
151 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
155 &set_label("nocpuid");
156 &function_end("OPENSSL_ia32_cpuid");
158 &external_label("OPENSSL_ia32cap_P");
160 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
163 &picmeup("ecx","OPENSSL_ia32cap_P");
164 &bt (&DWP(0,"ecx"),4);
165 &jnc (&label("notsc"));
169 &function_end_B("OPENSSL_rdtsc");
171 # This works in Ring 0 only [read MS-DOS+privileged DPMI host],
172 # but it's safe to call it on any [supported] 32-bit platform...
173 # Just check for [non-]zero return value...
174 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
175 &picmeup("ecx","OPENSSL_ia32cap_P");
176 &bt (&DWP(0,"ecx"),4);
177 &jnc (&label("nohalt")); # no TSC
179 &data_word(0x9058900e); # push %cs; pop %eax
181 &jnz (&label("nohalt")); # not enough privileges
186 &jnc (&label("nohalt")); # interrupts are disabled
194 &sub ("eax",&DWP(0,"esp"));
195 &sbb ("edx",&DWP(4,"esp"));
199 &set_label("nohalt");
203 &function_end_B("OPENSSL_instrument_halt");
206 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
209 &picmeup("ecx","OPENSSL_ia32cap_P");
210 &mov ("ecx",&DWP(0,"ecx"));
211 &bt (&DWP(0,"ecx"),1);
212 &jnc (&label("no_x87"));
214 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
215 &cmp ("ecx",1<<26|1<<24);
216 &jne (&label("no_sse2"));
217 &pxor ("xmm0","xmm0");
218 &pxor ("xmm1","xmm1");
219 &pxor ("xmm2","xmm2");
220 &pxor ("xmm3","xmm3");
221 &pxor ("xmm4","xmm4");
222 &pxor ("xmm5","xmm5");
223 &pxor ("xmm6","xmm6");
224 &pxor ("xmm7","xmm7");
225 &set_label("no_sse2");
227 # just a bunch of fldz to zap the fp/mm bank followed by finit...
228 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
229 &set_label("no_x87");
230 &lea ("eax",&DWP(4,"esp"));
232 &function_end_B("OPENSSL_wipe_cpu");
234 &function_begin_B("OPENSSL_atomic_add");
235 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
236 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
239 &mov ("eax",&DWP(0,"edx"));
241 &lea ("ebx",&DWP(0,"eax","ecx"));
243 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
244 &jne (&label("spin"));
245 &mov ("eax","ebx"); # OpenSSL expects the new value
248 &function_end_B("OPENSSL_atomic_add");
250 # This function can become handy under Win32 in situations when
251 # we don't know which calling convention, __stdcall or __cdecl(*),
252 # indirect callee is using. In C it can be deployed as
254 #ifdef OPENSSL_CPUID_OBJ
255 # type OPENSSL_indirect_call(void *f,...);
257 # OPENSSL_indirect_call(func,[up to $max arguments]);
260 # (*) it's designed to work even for __fastcall if number of
261 # arguments is 1 or 2!
262 &function_begin_B("OPENSSL_indirect_call");
264 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
265 # in order to preserve eventual
270 &mov ("ecx",&DWP(12,"ebp"));
271 &mov (&DWP(0,"esp"),"ecx");
272 &mov ("edx",&DWP(16,"ebp"));
273 &mov (&DWP(4,"esp"),"edx");
274 for($i=2;$i<$max;$i++)
276 # Some copies will be redundant/bogus...
277 &mov ("eax",&DWP(12+$i*4,"ebp"));
278 &mov (&DWP(0+$i*4,"esp"),"eax");
280 &call_ptr (&DWP(8,"ebp"));# make the call...
281 &mov ("esp","ebp"); # ... and just restore the stack pointer
282 # without paying attention to what we called,
283 # (__cdecl *func) or (__stdcall *one).
287 &function_end_B("OPENSSL_indirect_call");
289 &function_begin_B("OPENSSL_cleanse");
290 &mov ("edx",&wparam(0));
291 &mov ("ecx",&wparam(1));
294 &jae (&label("lot"));
297 &set_label("little");
298 &mov (&BP(0,"edx"),"al");
300 &lea ("edx",&DWP(1,"edx"));
301 &jnz (&label("little"));
305 &set_label("lot",16);
307 &jz (&label("aligned"));
308 &mov (&BP(0,"edx"),"al");
309 &lea ("ecx",&DWP(-1,"ecx"));
310 &lea ("edx",&DWP(1,"edx"));
311 &jmp (&label("lot"));
312 &set_label("aligned");
313 &mov (&DWP(0,"edx"),"eax");
314 &lea ("ecx",&DWP(-4,"ecx"));
316 &lea ("edx",&DWP(4,"edx"));
317 &jnz (&label("aligned"));
319 &jne (&label("little"));
321 &function_end_B("OPENSSL_cleanse");
324 my $lasttick = "esi";
325 my $lastdiff = "ebx";
330 &function_begin("OPENSSL_instrument_bus");
333 &picmeup("edx","OPENSSL_ia32cap_P");
334 &bt (&DWP(0,"edx"),4);
335 &jnc (&label("nogo")); # no TSC
336 &bt (&DWP(0,"edx"),19);
337 &jnc (&label("nogo")); # no CLFLUSH
339 &mov ($out,&wparam(0)); # load arguments
340 &mov ($cnt,&wparam(1));
344 &mov ($lasttick,"eax"); # lasttick = tick
345 &mov ($lastdiff,0); # lastdiff = 0
346 &clflush(&DWP(0,$out));
347 &data_byte(0xf0); # lock
348 &add (&DWP(0,$out),$lastdiff);
349 &jmp (&label("loop"));
351 &set_label("loop",16);
353 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
354 &sub ("eax",$lasttick); # diff
355 &mov ($lasttick,"edx"); # lasttick = tick
356 &mov ($lastdiff,"eax"); # lastdiff = diff
357 &clflush(&DWP(0,$out));
358 &data_byte(0xf0); # lock
359 &add (&DWP(0,$out),"eax"); # accumulate diff
360 &lea ($out,&DWP(4,$out)); # ++$out
361 &sub ($cnt,1); # --$cnt
362 &jnz (&label("loop"));
364 &mov ("eax",&wparam(1));
367 &function_end("OPENSSL_instrument_bus");
369 &function_begin("OPENSSL_instrument_bus2");
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));
380 &mov ($max,&wparam(2));
382 &rdtsc (); # collect 1st tick
383 &mov ($lasttick,"eax"); # lasttick = tick
384 &mov ($lastdiff,0); # lastdiff = 0
386 &clflush(&DWP(0,$out));
387 &data_byte(0xf0); # lock
388 &add (&DWP(0,$out),$lastdiff);
390 &rdtsc (); # collect 1st diff
391 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
392 &sub ("eax",$lasttick); # diff
393 &mov ($lasttick,"edx"); # lasttick = tick
394 &mov ($lastdiff,"eax"); # lastdiff = diff
395 &jmp (&label("loop2"));
397 &set_label("loop2",16);
398 &clflush(&DWP(0,$out));
399 &data_byte(0xf0); # lock
400 &add (&DWP(0,$out),"eax"); # accumulate diff
403 &jz (&label("done2"));
406 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
407 &sub ("eax",$lasttick); # diff
408 &mov ($lasttick,"edx"); # lasttick = tick
409 &cmp ("eax",$lastdiff);
410 &mov ($lastdiff,"eax"); # lastdiff = diff
413 &sub ($cnt,"edx"); # conditional --$cnt
414 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
415 &jnz (&label("loop2"));
418 &mov ("eax",&wparam(1));
422 &function_end("OPENSSL_instrument_bus2");
425 &function_begin_B("OPENSSL_ia32_rdrand");
429 &jc (&label("break"));
430 &loop (&label("loop"));
433 &cmove ("eax","ecx");
435 &function_end_B("OPENSSL_ia32_rdrand");
437 &function_begin_B("OPENSSL_ia32_rdseed");
441 &jc (&label("break"));
442 &loop (&label("loop"));
445 &cmove ("eax","ecx");
447 &function_end_B("OPENSSL_ia32_rdseed");
449 &initseg("OPENSSL_cpuid_setup");
451 &hidden("OPENSSL_cpuid_setup");
452 &hidden("OPENSSL_ia32cap_P");