2 # Copyright 2004-2020 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");
13 $output = pop and open STDOUT,">$output";
17 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
19 &function_begin("OPENSSL_ia32_cpuid");
31 &mov ("esi",&wparam(0));
32 &mov (&DWP(8,"esi"),"eax"); # clear extended feature flags
34 &jnc (&label("nocpuid"));
36 &mov ("edi","eax"); # max value for standard query level
39 &cmp ("ebx",0x756e6547); # "Genu"
42 &cmp ("edx",0x49656e69); # "ineI"
45 &cmp ("ecx",0x6c65746e); # "ntel"
47 &or ("ebp","eax"); # 0 indicates Intel CPU
48 &jz (&label("intel"));
50 &cmp ("ebx",0x68747541); # "Auth"
53 &cmp ("edx",0x69746E65); # "enti"
56 &cmp ("ecx",0x444D4163); # "cAMD"
58 &or ("esi","eax"); # 0 indicates AMD CPU
59 &jnz (&label("intel"));
62 &mov ("eax",0x80000000);
64 &cmp ("eax",0x80000001);
65 &jb (&label("intel"));
67 &mov ("eax",0x80000001);
70 &and ("ebp",1<<11|1); # isolate XOP bit
71 &cmp ("esi",0x80000008);
72 &jb (&label("intel"));
74 &mov ("eax",0x80000008);
76 &movz ("esi",&LB("ecx")); # number of cores - 1
77 &inc ("esi"); # number of cores
83 &jnc (&label("generic"));
87 &ja (&label("generic"));
88 &and ("edx",0xefffffff); # clear hyper-threading bit
89 &jmp (&label("generic"));
94 &jb (&label("nocacheinfo"));
97 &mov ("ecx",0); # query L1D
101 &and ("esi",0xfff); # number of cores -1 per L1D
103 &set_label("nocacheinfo");
107 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
109 &jne (&label("notintel"));
110 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
111 &and (&HB("eax"),15); # family ID
112 &cmp (&HB("eax"),15); # P4?
113 &jne (&label("notintel"));
114 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
115 &set_label("notintel");
116 &bt ("edx",28); # test hyper-threading bit
117 &jnc (&label("generic"));
118 &and ("edx",0xefffffff);
120 &je (&label("generic"));
122 &or ("edx",0x10000000);
125 &ja (&label("generic"));
126 &and ("edx",0xefffffff); # clear hyper-threading bit if not
128 &set_label("generic");
129 &and ("ebp",1<<11); # isolate AMD XOP flag
130 &and ("ecx",0xfffff7ff); # force 11th bit to 0
131 &mov ("esi","edx"); # %ebp:%esi is copy of %ecx:%edx
132 &or ("ebp","ecx"); # merge AMD XOP flag
135 &mov ("edi",&wparam(0));
136 &jb (&label("no_extended_info"));
140 &mov (&DWP(8,"edi"),"ebx"); # save extended feature flag
141 &set_label("no_extended_info");
143 &bt ("ebp",27); # check OSXSAVE bit
144 &jnc (&label("clear_avx"));
146 &data_byte(0x0f,0x01,0xd0); # xgetbv
149 &je (&label("done"));
151 &je (&label("clear_avx"));
152 &set_label("clear_xmm");
153 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
154 &and ("esi",0xfeffffff); # clear FXSR
155 &set_label("clear_avx");
156 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
157 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
161 &set_label("nocpuid");
162 &function_end("OPENSSL_ia32_cpuid");
164 &external_label("OPENSSL_ia32cap_P");
166 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
169 &picmeup("ecx","OPENSSL_ia32cap_P");
170 &bt (&DWP(0,"ecx"),4);
171 &jnc (&label("notsc"));
175 &function_end_B("OPENSSL_rdtsc");
177 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
178 # but it's safe to call it on any [supported] 32-bit platform...
179 # Just check for [non-]zero return value...
180 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
181 &picmeup("ecx","OPENSSL_ia32cap_P");
182 &bt (&DWP(0,"ecx"),4);
183 &jnc (&label("nohalt")); # no TSC
185 &data_word(0x9058900e); # push %cs; pop %eax
187 &jnz (&label("nohalt")); # not enough privileges
192 &jnc (&label("nohalt")); # interrupts are disabled
200 &sub ("eax",&DWP(0,"esp"));
201 &sbb ("edx",&DWP(4,"esp"));
205 &set_label("nohalt");
209 &function_end_B("OPENSSL_instrument_halt");
211 # Essentially there is only one use for this function. Under DJGPP:
215 # i=OPENSSL_far_spin(_dos_ds,0x46c);
217 # to obtain the number of spins till closest timer interrupt.
219 &function_begin_B("OPENSSL_far_spin");
223 &jnc (&label("nospin")); # interrupts are disabled
225 &mov ("eax",&DWP(4,"esp"));
226 &mov ("ecx",&DWP(8,"esp"));
227 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
229 &mov ("edx",&DWP(0,"ecx"));
230 &jmp (&label("spin"));
235 &cmp ("edx",&DWP(0,"ecx"));
236 &je (&label("spin"));
238 &data_word (0x1f909090); # pop %ds
241 &set_label("nospin");
245 &function_end_B("OPENSSL_far_spin");
247 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
250 &picmeup("ecx","OPENSSL_ia32cap_P");
251 &mov ("ecx",&DWP(0,"ecx"));
252 &bt (&DWP(0,"ecx"),1);
253 &jnc (&label("no_x87"));
255 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
256 &cmp ("ecx",1<<26|1<<24);
257 &jne (&label("no_sse2"));
258 &pxor ("xmm0","xmm0");
259 &pxor ("xmm1","xmm1");
260 &pxor ("xmm2","xmm2");
261 &pxor ("xmm3","xmm3");
262 &pxor ("xmm4","xmm4");
263 &pxor ("xmm5","xmm5");
264 &pxor ("xmm6","xmm6");
265 &pxor ("xmm7","xmm7");
266 &set_label("no_sse2");
268 # just a bunch of fldz to zap the fp/mm bank followed by finit...
269 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
270 &set_label("no_x87");
271 &lea ("eax",&DWP(4,"esp"));
273 &function_end_B("OPENSSL_wipe_cpu");
275 &function_begin_B("OPENSSL_atomic_add");
276 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
277 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
280 &mov ("eax",&DWP(0,"edx"));
282 &lea ("ebx",&DWP(0,"eax","ecx"));
284 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is involved and is always reloaded
285 &jne (&label("spin"));
286 &mov ("eax","ebx"); # OpenSSL expects the new value
289 &function_end_B("OPENSSL_atomic_add");
291 &function_begin_B("OPENSSL_cleanse");
292 &mov ("edx",&wparam(0));
293 &mov ("ecx",&wparam(1));
296 &jae (&label("lot"));
299 &set_label("little");
300 &mov (&BP(0,"edx"),"al");
302 &lea ("edx",&DWP(1,"edx"));
303 &jnz (&label("little"));
307 &set_label("lot",16);
309 &jz (&label("aligned"));
310 &mov (&BP(0,"edx"),"al");
311 &lea ("ecx",&DWP(-1,"ecx"));
312 &lea ("edx",&DWP(1,"edx"));
313 &jmp (&label("lot"));
314 &set_label("aligned");
315 &mov (&DWP(0,"edx"),"eax");
316 &lea ("ecx",&DWP(-4,"ecx"));
318 &lea ("edx",&DWP(4,"edx"));
319 &jnz (&label("aligned"));
321 &jne (&label("little"));
323 &function_end_B("OPENSSL_cleanse");
325 &function_begin_B("CRYPTO_memcmp");
328 &mov ("esi",&wparam(0));
329 &mov ("edi",&wparam(1));
330 &mov ("ecx",&wparam(2));
334 &je (&label("no_data"));
336 &mov ("dl",&BP(0,"esi"));
337 &lea ("esi",&DWP(1,"esi"));
338 &xor ("dl",&BP(0,"edi"));
339 &lea ("edi",&DWP(1,"edi"));
342 &jnz (&label("loop"));
345 &set_label("no_data");
349 &function_end_B("CRYPTO_memcmp");
351 my $lasttick = "esi";
352 my $lastdiff = "ebx";
357 &function_begin("OPENSSL_instrument_bus");
360 &picmeup("edx","OPENSSL_ia32cap_P");
361 &bt (&DWP(0,"edx"),4);
362 &jnc (&label("nogo")); # no TSC
363 &bt (&DWP(0,"edx"),19);
364 &jnc (&label("nogo")); # no CLFLUSH
366 &mov ($out,&wparam(0)); # load arguments
367 &mov ($cnt,&wparam(1));
371 &mov ($lasttick,"eax"); # lasttick = tick
372 &mov ($lastdiff,0); # lastdiff = 0
373 &clflush(&DWP(0,$out));
374 &data_byte(0xf0); # lock
375 &add (&DWP(0,$out),$lastdiff);
376 &jmp (&label("loop"));
378 &set_label("loop",16);
380 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
381 &sub ("eax",$lasttick); # diff
382 &mov ($lasttick,"edx"); # lasttick = tick
383 &mov ($lastdiff,"eax"); # lastdiff = diff
384 &clflush(&DWP(0,$out));
385 &data_byte(0xf0); # lock
386 &add (&DWP(0,$out),"eax"); # accumulate diff
387 &lea ($out,&DWP(4,$out)); # ++$out
388 &sub ($cnt,1); # --$cnt
389 &jnz (&label("loop"));
391 &mov ("eax",&wparam(1));
394 &function_end("OPENSSL_instrument_bus");
396 &function_begin("OPENSSL_instrument_bus2");
399 &picmeup("edx","OPENSSL_ia32cap_P");
400 &bt (&DWP(0,"edx"),4);
401 &jnc (&label("nogo")); # no TSC
402 &bt (&DWP(0,"edx"),19);
403 &jnc (&label("nogo")); # no CLFLUSH
405 &mov ($out,&wparam(0)); # load arguments
406 &mov ($cnt,&wparam(1));
407 &mov ($max,&wparam(2));
409 &rdtsc (); # collect 1st tick
410 &mov ($lasttick,"eax"); # lasttick = tick
411 &mov ($lastdiff,0); # lastdiff = 0
413 &clflush(&DWP(0,$out));
414 &data_byte(0xf0); # lock
415 &add (&DWP(0,$out),$lastdiff);
417 &rdtsc (); # collect 1st diff
418 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
419 &sub ("eax",$lasttick); # diff
420 &mov ($lasttick,"edx"); # lasttick = tick
421 &mov ($lastdiff,"eax"); # lastdiff = diff
422 &jmp (&label("loop2"));
424 &set_label("loop2",16);
425 &clflush(&DWP(0,$out));
426 &data_byte(0xf0); # lock
427 &add (&DWP(0,$out),"eax"); # accumulate diff
430 &jz (&label("done2"));
433 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
434 &sub ("eax",$lasttick); # diff
435 &mov ($lasttick,"edx"); # lasttick = tick
436 &cmp ("eax",$lastdiff);
437 &mov ($lastdiff,"eax"); # lastdiff = diff
440 &sub ($cnt,"edx"); # conditional --$cnt
441 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
442 &jnz (&label("loop2"));
445 &mov ("eax",&wparam(1));
449 &function_end("OPENSSL_instrument_bus2");
454 &function_begin_B("OPENSSL_ia32_${rdop}_bytes");
457 &xor ("eax","eax"); # return value
458 &mov ("edi",&wparam(0));
459 &mov ("ebx",&wparam(1));
462 &je (&label("done"));
467 &jc (&label("break"));
468 &loop (&label("loop"));
469 &jmp (&label("done"));
471 &set_label("break",16);
473 &jb (&label("tail"));
474 &mov (&DWP(0,"edi"),"edx");
475 &lea ("edi",&DWP(4,"edi"));
478 &jz (&label("done"));
480 &jmp (&label("loop"));
482 &set_label("tail",16);
483 &mov (&BP(0,"edi"),"dl");
484 &lea ("edi",&DWP(1,"edi"));
488 &jnz (&label("tail"));
491 &xor ("edx","edx"); # Clear random value from registers
495 &function_end_B("OPENSSL_ia32_${rdop}_bytes");
497 &gen_random("rdrand");
498 &gen_random("rdseed");
500 &initseg("OPENSSL_cpuid_setup");
502 &hidden("OPENSSL_cpuid_setup");
503 &hidden("OPENSSL_ia32cap_P");
507 close STDOUT or die "error closing STDOUT: $!";