2 # Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (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");
19 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
21 &function_begin("OPENSSL_ia32_cpuid");
33 &mov ("esi",&wparam(0));
34 &mov (&DWP(8,"esi"),"eax"); # clear extended feature flags
36 &jnc (&label("nocpuid"));
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"));
96 &jb (&label("nocacheinfo"));
99 &mov ("ecx",0); # query L1D
103 &and ("esi",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); # family 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
133 &mov ("esi","edx"); # %ebp:%esi is copy of %ecx:%edx
134 &or ("ebp","ecx"); # merge AMD XOP flag
137 &mov ("edi",&wparam(0));
138 &jb (&label("no_extended_info"));
142 &mov (&DWP(8,"edi"),"ebx"); # save extended feature flag
143 &set_label("no_extended_info");
145 &bt ("ebp",27); # check OSXSAVE bit
146 &jnc (&label("clear_avx"));
148 &data_byte(0x0f,0x01,0xd0); # xgetbv
151 &je (&label("done"));
153 &je (&label("clear_avx"));
154 &set_label("clear_xmm");
155 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
156 &and ("esi",0xfeffffff); # clear FXSR
157 &set_label("clear_avx");
158 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
159 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
163 &set_label("nocpuid");
164 &function_end("OPENSSL_ia32_cpuid");
166 &external_label("OPENSSL_ia32cap_P");
168 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
171 &picmeup("ecx","OPENSSL_ia32cap_P");
172 &bt (&DWP(0,"ecx"),4);
173 &jnc (&label("notsc"));
177 &function_end_B("OPENSSL_rdtsc");
179 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
180 # but it's safe to call it on any [supported] 32-bit platform...
181 # Just check for [non-]zero return value...
182 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
183 &picmeup("ecx","OPENSSL_ia32cap_P");
184 &bt (&DWP(0,"ecx"),4);
185 &jnc (&label("nohalt")); # no TSC
187 &data_word(0x9058900e); # push %cs; pop %eax
189 &jnz (&label("nohalt")); # not enough privileges
194 &jnc (&label("nohalt")); # interrupts are disabled
202 &sub ("eax",&DWP(0,"esp"));
203 &sbb ("edx",&DWP(4,"esp"));
207 &set_label("nohalt");
211 &function_end_B("OPENSSL_instrument_halt");
213 # Essentially there is only one use for this function. Under DJGPP:
217 # i=OPENSSL_far_spin(_dos_ds,0x46c);
219 # to obtain the number of spins till closest timer interrupt.
221 &function_begin_B("OPENSSL_far_spin");
225 &jnc (&label("nospin")); # interrupts are disabled
227 &mov ("eax",&DWP(4,"esp"));
228 &mov ("ecx",&DWP(8,"esp"));
229 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
231 &mov ("edx",&DWP(0,"ecx"));
232 &jmp (&label("spin"));
237 &cmp ("edx",&DWP(0,"ecx"));
238 &je (&label("spin"));
240 &data_word (0x1f909090); # pop %ds
243 &set_label("nospin");
247 &function_end_B("OPENSSL_far_spin");
249 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
252 &picmeup("ecx","OPENSSL_ia32cap_P");
253 &mov ("ecx",&DWP(0,"ecx"));
254 &bt (&DWP(0,"ecx"),1);
255 &jnc (&label("no_x87"));
257 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
258 &cmp ("ecx",1<<26|1<<24);
259 &jne (&label("no_sse2"));
260 &pxor ("xmm0","xmm0");
261 &pxor ("xmm1","xmm1");
262 &pxor ("xmm2","xmm2");
263 &pxor ("xmm3","xmm3");
264 &pxor ("xmm4","xmm4");
265 &pxor ("xmm5","xmm5");
266 &pxor ("xmm6","xmm6");
267 &pxor ("xmm7","xmm7");
268 &set_label("no_sse2");
270 # just a bunch of fldz to zap the fp/mm bank followed by finit...
271 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
272 &set_label("no_x87");
273 &lea ("eax",&DWP(4,"esp"));
275 &function_end_B("OPENSSL_wipe_cpu");
277 &function_begin_B("OPENSSL_atomic_add");
278 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
279 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
282 &mov ("eax",&DWP(0,"edx"));
284 &lea ("ebx",&DWP(0,"eax","ecx"));
286 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is involved and is always reloaded
287 &jne (&label("spin"));
288 &mov ("eax","ebx"); # OpenSSL expects the new value
291 &function_end_B("OPENSSL_atomic_add");
293 &function_begin_B("OPENSSL_cleanse");
294 &mov ("edx",&wparam(0));
295 &mov ("ecx",&wparam(1));
298 &jae (&label("lot"));
301 &set_label("little");
302 &mov (&BP(0,"edx"),"al");
304 &lea ("edx",&DWP(1,"edx"));
305 &jnz (&label("little"));
309 &set_label("lot",16);
311 &jz (&label("aligned"));
312 &mov (&BP(0,"edx"),"al");
313 &lea ("ecx",&DWP(-1,"ecx"));
314 &lea ("edx",&DWP(1,"edx"));
315 &jmp (&label("lot"));
316 &set_label("aligned");
317 &mov (&DWP(0,"edx"),"eax");
318 &lea ("ecx",&DWP(-4,"ecx"));
320 &lea ("edx",&DWP(4,"edx"));
321 &jnz (&label("aligned"));
323 &jne (&label("little"));
325 &function_end_B("OPENSSL_cleanse");
327 &function_begin_B("CRYPTO_memcmp");
330 &mov ("esi",&wparam(0));
331 &mov ("edi",&wparam(1));
332 &mov ("ecx",&wparam(2));
336 &je (&label("no_data"));
338 &mov ("dl",&BP(0,"esi"));
339 &lea ("esi",&DWP(1,"esi"));
340 &xor ("dl",&BP(0,"edi"));
341 &lea ("edi",&DWP(1,"edi"));
344 &jnz (&label("loop"));
347 &set_label("no_data");
351 &function_end_B("CRYPTO_memcmp");
353 my $lasttick = "esi";
354 my $lastdiff = "ebx";
359 &function_begin("OPENSSL_instrument_bus");
362 &picmeup("edx","OPENSSL_ia32cap_P");
363 &bt (&DWP(0,"edx"),4);
364 &jnc (&label("nogo")); # no TSC
365 &bt (&DWP(0,"edx"),19);
366 &jnc (&label("nogo")); # no CLFLUSH
368 &mov ($out,&wparam(0)); # load arguments
369 &mov ($cnt,&wparam(1));
373 &mov ($lasttick,"eax"); # lasttick = tick
374 &mov ($lastdiff,0); # lastdiff = 0
375 &clflush(&DWP(0,$out));
376 &data_byte(0xf0); # lock
377 &add (&DWP(0,$out),$lastdiff);
378 &jmp (&label("loop"));
380 &set_label("loop",16);
382 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
383 &sub ("eax",$lasttick); # diff
384 &mov ($lasttick,"edx"); # lasttick = tick
385 &mov ($lastdiff,"eax"); # lastdiff = diff
386 &clflush(&DWP(0,$out));
387 &data_byte(0xf0); # lock
388 &add (&DWP(0,$out),"eax"); # accumulate diff
389 &lea ($out,&DWP(4,$out)); # ++$out
390 &sub ($cnt,1); # --$cnt
391 &jnz (&label("loop"));
393 &mov ("eax",&wparam(1));
396 &function_end("OPENSSL_instrument_bus");
398 &function_begin("OPENSSL_instrument_bus2");
401 &picmeup("edx","OPENSSL_ia32cap_P");
402 &bt (&DWP(0,"edx"),4);
403 &jnc (&label("nogo")); # no TSC
404 &bt (&DWP(0,"edx"),19);
405 &jnc (&label("nogo")); # no CLFLUSH
407 &mov ($out,&wparam(0)); # load arguments
408 &mov ($cnt,&wparam(1));
409 &mov ($max,&wparam(2));
411 &rdtsc (); # collect 1st tick
412 &mov ($lasttick,"eax"); # lasttick = tick
413 &mov ($lastdiff,0); # lastdiff = 0
415 &clflush(&DWP(0,$out));
416 &data_byte(0xf0); # lock
417 &add (&DWP(0,$out),$lastdiff);
419 &rdtsc (); # collect 1st diff
420 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
421 &sub ("eax",$lasttick); # diff
422 &mov ($lasttick,"edx"); # lasttick = tick
423 &mov ($lastdiff,"eax"); # lastdiff = diff
424 &jmp (&label("loop2"));
426 &set_label("loop2",16);
427 &clflush(&DWP(0,$out));
428 &data_byte(0xf0); # lock
429 &add (&DWP(0,$out),"eax"); # accumulate diff
432 &jz (&label("done2"));
435 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
436 &sub ("eax",$lasttick); # diff
437 &mov ($lasttick,"edx"); # lasttick = tick
438 &cmp ("eax",$lastdiff);
439 &mov ($lastdiff,"eax"); # lastdiff = diff
442 &sub ($cnt,"edx"); # conditional --$cnt
443 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
444 &jnz (&label("loop2"));
447 &mov ("eax",&wparam(1));
451 &function_end("OPENSSL_instrument_bus2");
456 &function_begin_B("OPENSSL_ia32_${rdop}_bytes");
459 &xor ("eax","eax"); # return value
460 &mov ("edi",&wparam(0));
461 &mov ("ebx",&wparam(1));
464 &je (&label("done"));
469 &jc (&label("break"));
470 &loop (&label("loop"));
471 &jmp (&label("done"));
473 &set_label("break",16);
475 &jb (&label("tail"));
476 &mov (&DWP(0,"edi"),"edx");
477 &lea ("edi",&DWP(4,"edi"));
480 &jz (&label("done"));
482 &jmp (&label("loop"));
484 &set_label("tail",16);
485 &mov (&BP(0,"edi"),"dl");
486 &lea ("edi",&DWP(1,"edi"));
490 &jnz (&label("tail"));
493 &xor ("edx","edx"); # Clear random value from registers
497 &function_end_B("OPENSSL_ia32_${rdop}_bytes");
499 &gen_random("rdrand");
500 &gen_random("rdseed");
502 &initseg("OPENSSL_cpuid_setup");
504 &hidden("OPENSSL_cpuid_setup");
505 &hidden("OPENSSL_ia32cap_P");