2 # Copyright 2005-2020 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
12 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
14 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
16 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
17 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
18 ( $xlate="${dir}perlasm/x86_64-xlate.pl" and -f $xlate) or
19 die "can't locate x86_64-xlate.pl";
21 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
24 ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
25 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
28 .extern OPENSSL_cpuid_setup
29 .hidden OPENSSL_cpuid_setup
31 call OPENSSL_cpuid_setup
33 .hidden OPENSSL_ia32cap_P
34 .comm OPENSSL_ia32cap_P,16,4
38 .globl OPENSSL_atomic_add
39 .type OPENSSL_atomic_add,\@abi-omnipotent
44 .Lspin: leaq ($arg2,%rax),%r8
49 .byte 0x48,0x98 # cltq/cdqe
52 .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
55 .type OPENSSL_rdtsc,\@abi-omnipotent
64 .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
66 .globl OPENSSL_ia32_cpuid
67 .type OPENSSL_ia32_cpuid,\@function,1
71 mov %rbx,%r8 # save %rbx
72 .cfi_register %rbx,%r8
75 mov %rax,8(%rdi) # clear extended feature flags
77 mov %eax,%r11d # max value for standard query level
80 cmp \$0x756e6547,%ebx # "Genu"
83 cmp \$0x49656e69,%edx # "ineI"
86 cmp \$0x6c65746e,%ecx # "ntel"
88 or %eax,%r9d # 0 indicates Intel CPU
91 cmp \$0x68747541,%ebx # "Auth"
94 cmp \$0x69746E65,%edx # "enti"
97 cmp \$0x444D4163,%ecx # "cAMD"
99 or %eax,%r10d # 0 indicates AMD CPU
103 mov \$0x80000000,%eax
105 cmp \$0x80000001,%eax
108 mov \$0x80000001,%eax
111 and \$0x00000801,%r9d # isolate AMD XOP bit, 1<<11
113 cmp \$0x80000008,%r10d
116 mov \$0x80000008,%eax
118 movzb %cl,%r10 # number of cores - 1
119 inc %r10 # number of cores
123 bt \$28,%edx # test hyper-threading bit
125 shr \$16,%ebx # number of logical processors
128 and \$0xefffffff,%edx # ~(1<<28)
137 mov \$0,%ecx # query L1D
141 and \$0xfff,%r10d # number of cores -1 per L1D
146 movd %eax,%xmm0 # put aside processor id
147 and \$0xbfefffff,%edx # force reserved bits to 0
150 or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
152 cmp \$15,%ah # examine Family ID
154 or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
158 and \$0x0fff0ff0,%eax
159 cmp \$0x00050670,%eax # Knights Landing
161 cmp \$0x00080650,%eax # Knights Mill (according to sde)
164 and \$0xfbffffff,%ecx # clear XSAVE flag to mimic Silvermont
167 bt \$28,%edx # test hyper-threading bit
169 and \$0xefffffff,%edx # ~(1<<28)
173 or \$0x10000000,%edx # 1<<28
175 cmp \$1,%bl # see if cache is shared
177 and \$0xefffffff,%edx # ~(1<<28)
179 and \$0x00000800,%r9d # isolate AMD XOP flag
180 and \$0xfffff7ff,%ecx
181 or %ecx,%r9d # merge AMD XOP flag
183 mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
186 jb .Lno_extended_info
190 bt \$26,%r9d # check XSAVE bit, cleared on Knights
192 and \$0xfff7ffff,%ebx # clear ADCX/ADOX flag
194 movd %xmm0,%eax # restore processor id
195 and \$0x0fff0ff0,%eax
196 cmp \$0x00050650,%eax # Skylake-X
198 and \$0xfffeffff,%ebx # ~(1<<16)
199 # suppress AVX512F flag on Skylake-X
201 mov %ebx,8(%rdi) # save extended feature flags
205 bt \$27,%r9d # check OSXSAVE bit
208 .byte 0x0f,0x01,0xd0 # xgetbv
209 and \$0xe6,%eax # isolate XMM, YMM and ZMM state support
212 andl \$0x3fdeffff,8(%rdi) # ~(1<<31|1<<30|1<<21|1<<16)
213 # clear AVX512F+BW+VL+FIMA, all of
214 # them are EVEX-encoded, which requires
215 # ZMM state support even if one uses
216 # only XMM and YMM :-(
217 and \$6,%eax # isolate XMM and YMM state support
221 mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
222 and %eax,%r9d # clear AVX, FMA and AMD XOP bits
223 mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
224 and %eax,8(%rdi) # clear AVX2 and AVX512* bits
228 mov %r8,%rbx # restore %rbx
233 .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
235 .globl OPENSSL_cleanse
236 .type OPENSSL_cleanse,\@abi-omnipotent
270 .size OPENSSL_cleanse,.-OPENSSL_cleanse
273 .type CRYPTO_memcmp,\@abi-omnipotent
306 .size CRYPTO_memcmp,.-CRYPTO_memcmp
309 print<<___ if (!$win64);
310 .globl OPENSSL_wipe_cpu
311 .type OPENSSL_wipe_cpu,\@abi-omnipotent
342 .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
344 print<<___ if ($win64);
345 .globl OPENSSL_wipe_cpu
346 .type OPENSSL_wipe_cpu,\@abi-omnipotent
363 .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
371 my $redzone=win64?8:-8;
374 .globl OPENSSL_instrument_bus
375 .type OPENSSL_instrument_bus,\@abi-omnipotent
377 OPENSSL_instrument_bus:
379 mov $arg1,$out # tribute to Win64
383 rdtsc # collect 1st tick
384 mov %eax,$lasttick # lasttick = tick
385 mov \$0,$lastdiff # lastdiff = 0
406 .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
408 .globl OPENSSL_instrument_bus2
409 .type OPENSSL_instrument_bus2,\@abi-omnipotent
411 OPENSSL_instrument_bus2:
413 mov $arg1,$out # tribute to Win64
416 mov $cnt,$redzone(%rsp)
418 rdtsc # collect 1st tick
419 mov %eax,$lasttick # lasttick = tick
420 mov \$0,$lastdiff # lastdiff = 0
426 rdtsc # collect 1st diff
428 sub $lasttick,%eax # diff
429 mov %edx,$lasttick # lasttick = tick
430 mov %eax,$lastdiff # lastdiff = diff
434 add %eax,($out) # accumulate diff
441 sub $lasttick,%eax # diff
442 mov %edx,$lasttick # lasttick = tick
444 mov %eax,$lastdiff # lastdiff = diff
447 sub %rdx,$cnt # conditional --$cnt
448 lea ($out,%rdx,4),$out # conditional ++$out
452 mov $redzone(%rsp),%rax
456 .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
463 .globl OPENSSL_ia32_${rdop}_bytes
464 .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
466 OPENSSL_ia32_${rdop}_bytes:
468 xor %rax, %rax # return value
470 je .Ldone_${rdop}_bytes
475 jc .Lbreak_${rdop}_bytes
477 jnz .Loop_${rdop}_bytes
478 jmp .Ldone_${rdop}_bytes
481 .Lbreak_${rdop}_bytes:
483 jb .Ltail_${rdop}_bytes
488 jz .Ldone_${rdop}_bytes
490 jmp .Loop_${rdop}_bytes
493 .Ltail_${rdop}_bytes:
499 jnz .Ltail_${rdop}_bytes
501 .Ldone_${rdop}_bytes:
502 xor %r10,%r10 # Clear sensitive data from register
505 .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
508 gen_random("rdrand");
509 gen_random("rdseed");
511 close STDOUT or die "error closing STDOUT: $!"; # flush