2 # Copyright 2005-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
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
43 .Lspin
: leaq
($arg2,%rax),%r8
48 .byte
0x48,0x98 # cltq/cdqe
50 .size OPENSSL_atomic_add
,.-OPENSSL_atomic_add
53 .type OPENSSL_rdtsc
,\
@abi-omnipotent
60 .size OPENSSL_rdtsc
,.-OPENSSL_rdtsc
62 .globl OPENSSL_ia32_cpuid
63 .type OPENSSL_ia32_cpuid
,\
@function,1
67 mov
%rbx,%r8 # save %rbx
68 .cfi_register
%rbx,%r8
71 mov
%rax,8(%rdi) # clear extended feature flags
73 mov
%eax,%r11d # max value for standard query level
76 cmp \
$0x756e6547,%ebx # "Genu"
79 cmp \
$0x49656e69,%edx # "ineI"
82 cmp \
$0x6c65746e,%ecx # "ntel"
84 or %eax,%r9d # 0 indicates Intel CPU
87 cmp \
$0x68747541,%ebx # "Auth"
90 cmp \
$0x69746E65,%edx # "enti"
93 cmp \
$0x444D4163,%ecx # "cAMD"
95 or %eax,%r10d # 0 indicates AMD CPU
101 cmp \
$0x80000001,%eax
104 mov \
$0x80000001,%eax
107 and \
$0x00000801,%r9d # isolate AMD XOP bit, 1<<11
109 cmp \
$0x80000008,%r10d
112 mov \
$0x80000008,%eax
114 movzb
%cl,%r10 # number of cores - 1
115 inc
%r10 # number of cores
119 bt \
$28,%edx # test hyper-threading bit
121 shr \
$16,%ebx # number of logical processors
124 and \
$0xefffffff,%edx # ~(1<<28)
133 mov \
$0,%ecx # query L1D
137 and \
$0xfff,%r10d # number of cores -1 per L1D
142 movd
%eax,%xmm0 # put aside processor id
143 and \
$0xbfefffff,%edx # force reserved bits to 0
146 or \
$0x40000000,%edx # set reserved bit#30 on Intel CPUs
148 cmp \
$15,%ah # examine Family ID
150 or \
$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
154 and \
$0x0fff0ff0,%eax
155 cmp \
$0x00050670,%eax # Knights Landing
157 cmp \
$0x00080650,%eax # Knights Mill (according to sde)
160 and \
$0xfbffffff,%ecx # clear XSAVE flag to mimic Silvermont
163 bt \
$28,%edx # test hyper-threading bit
165 and \
$0xefffffff,%edx # ~(1<<28)
169 or \
$0x10000000,%edx # 1<<28
171 cmp \
$1,%bl # see if cache is shared
173 and \
$0xefffffff,%edx # ~(1<<28)
175 and \
$0x00000800,%r9d # isolate AMD XOP flag
176 and \
$0xfffff7ff,%ecx
177 or %ecx,%r9d # merge AMD XOP flag
179 mov
%edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
182 jb
.Lno_extended_info
186 bt \
$26,%r9d # check XSAVE bit, cleared on Knights
188 and \
$0xfff7ffff,%ebx # clear ADCX/ADOX flag
190 movd
%xmm0,%eax # restore processor id
191 and \
$0x0fff0ff0,%eax
192 cmp \
$0x00050650,%eax # Skylake-X
194 and \
$0xfffeffff,%ebx # ~(1<<16)
195 # suppress AVX512F flag on Skylake-X
197 mov
%ebx,8(%rdi) # save extended feature flags
201 bt \
$27,%r9d # check OSXSAVE bit
204 .byte
0x0f,0x01,0xd0 # xgetbv
205 and \
$0xe6,%eax # isolate XMM, YMM and ZMM state support
208 andl \
$0x3fdeffff,8(%rdi) # ~(1<<31|1<<30|1<<21|1<<16)
209 # clear AVX512F+BW+VL+FIMA, all of
210 # them are EVEX-encoded, which requires
211 # ZMM state support even if one uses
212 # only XMM and YMM :-(
213 and \
$6,%eax # isolate XMM and YMM state support
217 mov \
$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
218 and %eax,%r9d # clear AVX, FMA and AMD XOP bits
219 mov \
$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
220 and %eax,8(%rdi) # clear AVX2 and AVX512* bits
224 mov
%r8,%rbx # restore %rbx
229 .size OPENSSL_ia32_cpuid
,.-OPENSSL_ia32_cpuid
231 .globl OPENSSL_cleanse
232 .type OPENSSL_cleanse
,\
@abi-omnipotent
264 .size OPENSSL_cleanse
,.-OPENSSL_cleanse
267 .type CRYPTO_memcmp
,\
@abi-omnipotent
298 .size CRYPTO_memcmp
,.-CRYPTO_memcmp
301 print<<___
if (!$win64);
302 .globl OPENSSL_wipe_cpu
303 .type OPENSSL_wipe_cpu
,\
@abi-omnipotent
332 .size OPENSSL_wipe_cpu
,.-OPENSSL_wipe_cpu
334 print<<___
if ($win64);
335 .globl OPENSSL_wipe_cpu
336 .type OPENSSL_wipe_cpu
,\
@abi-omnipotent
353 .size OPENSSL_wipe_cpu
,.-OPENSSL_wipe_cpu
361 my $redzone=win64?
8:-8;
364 .globl OPENSSL_instrument_bus
365 .type OPENSSL_instrument_bus
,\
@abi-omnipotent
367 OPENSSL_instrument_bus
:
368 mov
$arg1,$out # tribute to Win64
372 rdtsc
# collect 1st tick
373 mov
%eax,$lasttick # lasttick = tick
374 mov \
$0,$lastdiff # lastdiff = 0
394 .size OPENSSL_instrument_bus
,.-OPENSSL_instrument_bus
396 .globl OPENSSL_instrument_bus2
397 .type OPENSSL_instrument_bus2
,\
@abi-omnipotent
399 OPENSSL_instrument_bus2
:
400 mov
$arg1,$out # tribute to Win64
403 mov
$cnt,$redzone(%rsp)
405 rdtsc
# collect 1st tick
406 mov
%eax,$lasttick # lasttick = tick
407 mov \
$0,$lastdiff # lastdiff = 0
413 rdtsc
# collect 1st diff
415 sub $lasttick,%eax # diff
416 mov
%edx,$lasttick # lasttick = tick
417 mov
%eax,$lastdiff # lastdiff = diff
421 add
%eax,($out) # accumulate diff
428 sub $lasttick,%eax # diff
429 mov
%edx,$lasttick # lasttick = tick
431 mov
%eax,$lastdiff # lastdiff = diff
434 sub %rdx,$cnt # conditional --$cnt
435 lea
($out,%rdx,4),$out # conditional ++$out
439 mov
$redzone(%rsp),%rax
442 .size OPENSSL_instrument_bus2
,.-OPENSSL_instrument_bus2
449 .globl OPENSSL_ia32_
${rdop
}_bytes
450 .type OPENSSL_ia32_
${rdop
}_bytes
,\
@abi-omnipotent
452 OPENSSL_ia32_
${rdop
}_bytes
:
453 xor %rax, %rax # return value
455 je
.Ldone_
${rdop
}_bytes
460 jc
.Lbreak_
${rdop
}_bytes
462 jnz
.Loop_
${rdop
}_bytes
463 jmp
.Ldone_
${rdop
}_bytes
466 .Lbreak_
${rdop
}_bytes
:
468 jb
.Ltail_
${rdop
}_bytes
473 jz
.Ldone_
${rdop
}_bytes
475 jmp
.Loop_
${rdop
}_bytes
478 .Ltail_
${rdop
}_bytes
:
484 jnz
.Ltail_
${rdop
}_bytes
486 .Ldone_
${rdop
}_bytes
:
487 xor %r10,%r10 # Clear sensitive data from register
489 .size OPENSSL_ia32_
${rdop
}_bytes
,.-OPENSSL_ia32_
${rdop
}_bytes
492 gen_random
("rdrand");
493 gen_random
("rdseed");
495 close STDOUT
; # flush