2 # Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
9 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
10 push(@INC, "${dir}perlasm", "perlasm");
17 &asm_init($ARGV[0],"x86cpuid");
19 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
21 &function_begin("OPENSSL_ia32_cpuid");
34 &jnc (&label("nocpuid"));
35 &mov ("esi",&wparam(0));
36 &mov (&DWP(8,"esi"),"eax"); # clear 3rd word
38 &mov ("edi","eax"); # max value for standard query level
41 &cmp ("ebx",0x756e6547); # "Genu"
44 &cmp ("edx",0x49656e69); # "ineI"
47 &cmp ("ecx",0x6c65746e); # "ntel"
49 &or ("ebp","eax"); # 0 indicates Intel CPU
50 &jz (&label("intel"));
52 &cmp ("ebx",0x68747541); # "Auth"
55 &cmp ("edx",0x69746E65); # "enti"
58 &cmp ("ecx",0x444D4163); # "cAMD"
60 &or ("esi","eax"); # 0 indicates AMD CPU
61 &jnz (&label("intel"));
64 &mov ("eax",0x80000000);
66 &cmp ("eax",0x80000001);
67 &jb (&label("intel"));
69 &mov ("eax",0x80000001);
72 &and ("ebp",1<<11|1); # isolate XOP bit
73 &cmp ("esi",0x80000008);
74 &jb (&label("intel"));
76 &mov ("eax",0x80000008);
78 &movz ("esi",&LB("ecx")); # number of cores - 1
79 &inc ("esi"); # number of cores
85 &jnc (&label("generic"));
89 &ja (&label("generic"));
90 &and ("edx",0xefffffff); # clear hyper-threading bit
91 &jmp (&label("generic"));
95 &jb (&label("cacheinfo"));
97 &mov ("esi",&wparam(0));
101 &mov (&DWP(8,"esi"),"ebx");
103 &set_label("cacheinfo");
106 &jb (&label("nocacheinfo"));
109 &mov ("ecx",0); # query L1D
113 &and ("edi",0xfff); # number of cores -1 per L1D
115 &set_label("nocacheinfo");
119 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
121 &jne (&label("notintel"));
122 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
123 &and (&HB("eax"),15); # familiy ID
124 &cmp (&HB("eax"),15); # P4?
125 &jne (&label("notintel"));
126 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
127 &set_label("notintel");
128 &bt ("edx",28); # test hyper-threading bit
129 &jnc (&label("generic"));
130 &and ("edx",0xefffffff);
132 &je (&label("generic"));
134 &or ("edx",0x10000000);
137 &ja (&label("generic"));
138 &and ("edx",0xefffffff); # clear hyper-threading bit if not
140 &set_label("generic");
141 &and ("ebp",1<<11); # isolate AMD XOP flag
142 &and ("ecx",0xfffff7ff); # force 11th bit to 0
144 &or ("ebp","ecx"); # merge AMD XOP flag
146 &bt ("ecx",27); # check OSXSAVE bit
147 &jnc (&label("clear_avx"));
149 &data_byte(0x0f,0x01,0xd0); # xgetbv
152 &je (&label("done"));
154 &je (&label("clear_avx"));
155 &set_label("clear_xmm");
156 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
157 &and ("esi",0xfeffffff); # clear FXSR
158 &set_label("clear_avx");
159 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
160 &mov ("edi",&wparam(0));
161 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
165 &set_label("nocpuid");
166 &function_end("OPENSSL_ia32_cpuid");
168 &external_label("OPENSSL_ia32cap_P");
170 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
173 &picmeup("ecx","OPENSSL_ia32cap_P");
174 &bt (&DWP(0,"ecx"),4);
175 &jnc (&label("notsc"));
179 &function_end_B("OPENSSL_rdtsc");
181 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
182 # but it's safe to call it on any [supported] 32-bit platform...
183 # Just check for [non-]zero return value...
184 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
185 &picmeup("ecx","OPENSSL_ia32cap_P");
186 &bt (&DWP(0,"ecx"),4);
187 &jnc (&label("nohalt")); # no TSC
189 &data_word(0x9058900e); # push %cs; pop %eax
191 &jnz (&label("nohalt")); # not enough privileges
196 &jnc (&label("nohalt")); # interrupts are disabled
204 &sub ("eax",&DWP(0,"esp"));
205 &sbb ("edx",&DWP(4,"esp"));
209 &set_label("nohalt");
213 &function_end_B("OPENSSL_instrument_halt");
215 # Essentially there is only one use for this function. Under DJGPP:
219 # i=OPENSSL_far_spin(_dos_ds,0x46c);
221 # to obtain the number of spins till closest timer interrupt.
223 &function_begin_B("OPENSSL_far_spin");
227 &jnc (&label("nospin")); # interrupts are disabled
229 &mov ("eax",&DWP(4,"esp"));
230 &mov ("ecx",&DWP(8,"esp"));
231 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
233 &mov ("edx",&DWP(0,"ecx"));
234 &jmp (&label("spin"));
239 &cmp ("edx",&DWP(0,"ecx"));
240 &je (&label("spin"));
242 &data_word (0x1f909090); # pop %ds
245 &set_label("nospin");
249 &function_end_B("OPENSSL_far_spin");
251 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
254 &picmeup("ecx","OPENSSL_ia32cap_P");
255 &mov ("ecx",&DWP(0,"ecx"));
256 &bt (&DWP(0,"ecx"),1);
257 &jnc (&label("no_x87"));
259 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
260 &cmp ("ecx",1<<26|1<<24);
261 &jne (&label("no_sse2"));
262 &pxor ("xmm0","xmm0");
263 &pxor ("xmm1","xmm1");
264 &pxor ("xmm2","xmm2");
265 &pxor ("xmm3","xmm3");
266 &pxor ("xmm4","xmm4");
267 &pxor ("xmm5","xmm5");
268 &pxor ("xmm6","xmm6");
269 &pxor ("xmm7","xmm7");
270 &set_label("no_sse2");
272 # just a bunch of fldz to zap the fp/mm bank followed by finit...
273 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
274 &set_label("no_x87");
275 &lea ("eax",&DWP(4,"esp"));
277 &function_end_B("OPENSSL_wipe_cpu");
279 &function_begin_B("OPENSSL_atomic_add");
280 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
281 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
284 &mov ("eax",&DWP(0,"edx"));
286 &lea ("ebx",&DWP(0,"eax","ecx"));
288 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
289 &jne (&label("spin"));
290 &mov ("eax","ebx"); # OpenSSL expects the new value
293 &function_end_B("OPENSSL_atomic_add");
295 # This function can become handy under Win32 in situations when
296 # we don't know which calling convention, __stdcall or __cdecl(*),
297 # indirect callee is using. In C it can be deployed as
299 #ifdef OPENSSL_CPUID_OBJ
300 # type OPENSSL_indirect_call(void *f,...);
302 # OPENSSL_indirect_call(func,[up to $max arguments]);
305 # (*) it's designed to work even for __fastcall if number of
306 # arguments is 1 or 2!
307 &function_begin_B("OPENSSL_indirect_call");
309 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
310 # in order to preserve eventual
315 &mov ("ecx",&DWP(12,"ebp"));
316 &mov (&DWP(0,"esp"),"ecx");
317 &mov ("edx",&DWP(16,"ebp"));
318 &mov (&DWP(4,"esp"),"edx");
319 for($i=2;$i<$max;$i++)
321 # Some copies will be redundant/bogus...
322 &mov ("eax",&DWP(12+$i*4,"ebp"));
323 &mov (&DWP(0+$i*4,"esp"),"eax");
325 &call_ptr (&DWP(8,"ebp"));# make the call...
326 &mov ("esp","ebp"); # ... and just restore the stack pointer
327 # without paying attention to what we called,
328 # (__cdecl *func) or (__stdcall *one).
332 &function_end_B("OPENSSL_indirect_call");
334 &function_begin_B("OPENSSL_cleanse");
335 &mov ("edx",&wparam(0));
336 &mov ("ecx",&wparam(1));
339 &jae (&label("lot"));
342 &set_label("little");
343 &mov (&BP(0,"edx"),"al");
345 &lea ("edx",&DWP(1,"edx"));
346 &jnz (&label("little"));
350 &set_label("lot",16);
352 &jz (&label("aligned"));
353 &mov (&BP(0,"edx"),"al");
354 &lea ("ecx",&DWP(-1,"ecx"));
355 &lea ("edx",&DWP(1,"edx"));
356 &jmp (&label("lot"));
357 &set_label("aligned");
358 &mov (&DWP(0,"edx"),"eax");
359 &lea ("ecx",&DWP(-4,"ecx"));
361 &lea ("edx",&DWP(4,"edx"));
362 &jnz (&label("aligned"));
364 &jne (&label("little"));
366 &function_end_B("OPENSSL_cleanse");
369 my $lasttick = "esi";
370 my $lastdiff = "ebx";
375 &function_begin("OPENSSL_instrument_bus");
378 &picmeup("edx","OPENSSL_ia32cap_P");
379 &bt (&DWP(0,"edx"),4);
380 &jnc (&label("nogo")); # no TSC
381 &bt (&DWP(0,"edx"),19);
382 &jnc (&label("nogo")); # no CLFLUSH
384 &mov ($out,&wparam(0)); # load arguments
385 &mov ($cnt,&wparam(1));
389 &mov ($lasttick,"eax"); # lasttick = tick
390 &mov ($lastdiff,0); # lastdiff = 0
391 &clflush(&DWP(0,$out));
392 &data_byte(0xf0); # lock
393 &add (&DWP(0,$out),$lastdiff);
394 &jmp (&label("loop"));
396 &set_label("loop",16);
398 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
399 &sub ("eax",$lasttick); # diff
400 &mov ($lasttick,"edx"); # lasttick = tick
401 &mov ($lastdiff,"eax"); # lastdiff = diff
402 &clflush(&DWP(0,$out));
403 &data_byte(0xf0); # lock
404 &add (&DWP(0,$out),"eax"); # accumulate diff
405 &lea ($out,&DWP(4,$out)); # ++$out
406 &sub ($cnt,1); # --$cnt
407 &jnz (&label("loop"));
409 &mov ("eax",&wparam(1));
412 &function_end("OPENSSL_instrument_bus");
414 &function_begin("OPENSSL_instrument_bus2");
417 &picmeup("edx","OPENSSL_ia32cap_P");
418 &bt (&DWP(0,"edx"),4);
419 &jnc (&label("nogo")); # no TSC
420 &bt (&DWP(0,"edx"),19);
421 &jnc (&label("nogo")); # no CLFLUSH
423 &mov ($out,&wparam(0)); # load arguments
424 &mov ($cnt,&wparam(1));
425 &mov ($max,&wparam(2));
427 &rdtsc (); # collect 1st tick
428 &mov ($lasttick,"eax"); # lasttick = tick
429 &mov ($lastdiff,0); # lastdiff = 0
431 &clflush(&DWP(0,$out));
432 &data_byte(0xf0); # lock
433 &add (&DWP(0,$out),$lastdiff);
435 &rdtsc (); # collect 1st diff
436 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
437 &sub ("eax",$lasttick); # diff
438 &mov ($lasttick,"edx"); # lasttick = tick
439 &mov ($lastdiff,"eax"); # lastdiff = diff
440 &jmp (&label("loop2"));
442 &set_label("loop2",16);
443 &clflush(&DWP(0,$out));
444 &data_byte(0xf0); # lock
445 &add (&DWP(0,$out),"eax"); # accumulate diff
448 &jz (&label("done2"));
451 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
452 &sub ("eax",$lasttick); # diff
453 &mov ($lasttick,"edx"); # lasttick = tick
454 &cmp ("eax",$lastdiff);
455 &mov ($lastdiff,"eax"); # lastdiff = diff
458 &sub ($cnt,"edx"); # conditional --$cnt
459 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
460 &jnz (&label("loop2"));
463 &mov ("eax",&wparam(1));
467 &function_end("OPENSSL_instrument_bus2");
470 &function_begin_B("OPENSSL_ia32_rdrand");
474 &jc (&label("break"));
475 &loop (&label("loop"));
478 &cmove ("eax","ecx");
480 &function_end_B("OPENSSL_ia32_rdrand");
482 &function_begin_B("OPENSSL_ia32_rdseed");
486 &jc (&label("break"));
487 &loop (&label("loop"));
490 &cmove ("eax","ecx");
492 &function_end_B("OPENSSL_ia32_rdseed");
494 &initseg("OPENSSL_cpuid_setup");
496 &hidden("OPENSSL_cpuid_setup");
497 &hidden("OPENSSL_ia32cap_P");