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
10 # $output is the last argument if it looks like a file (it has an extension)
11 # $flavour is the first argument if it doesn't look like a file
12 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
13 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
15 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
17 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
18 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
19 ( $xlate="${dir}perlasm/x86_64-xlate.pl" and -f $xlate) or
20 die "can't locate x86_64-xlate.pl";
22 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
23 or die "can't call $xlate: $!";
26 ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
27 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
30 .extern OPENSSL_cpuid_setup
31 .hidden OPENSSL_cpuid_setup
33 call OPENSSL_cpuid_setup
35 .hidden OPENSSL_ia32cap_P
36 .comm OPENSSL_ia32cap_P,16,4
40 .globl OPENSSL_atomic_add
41 .type OPENSSL_atomic_add,\@abi-omnipotent
47 .Lspin: leaq ($arg2,%rax),%r8
52 .byte 0x48,0x98 # cltq/cdqe
55 .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
58 .type OPENSSL_rdtsc,\@abi-omnipotent
68 .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
70 .globl OPENSSL_ia32_cpuid
71 .type OPENSSL_ia32_cpuid,\@function,1
76 mov %rbx,%r8 # save %rbx
77 .cfi_register %rbx,%r8
80 mov %rax,8(%rdi) # clear extended feature flags
82 mov %eax,%r11d # max value for standard query level
85 cmp \$0x756e6547,%ebx # "Genu"
88 cmp \$0x49656e69,%edx # "ineI"
91 cmp \$0x6c65746e,%ecx # "ntel"
93 or %eax,%r9d # 0 indicates Intel CPU
96 cmp \$0x68747541,%ebx # "Auth"
99 cmp \$0x69746E65,%edx # "enti"
102 cmp \$0x444D4163,%ecx # "cAMD"
104 or %eax,%r10d # 0 indicates AMD CPU
108 mov \$0x80000000,%eax
110 cmp \$0x80000001,%eax
113 mov \$0x80000001,%eax
116 and \$0x00000801,%r9d # isolate AMD XOP bit, 1<<11
118 cmp \$0x80000008,%r10d
121 mov \$0x80000008,%eax
123 movzb %cl,%r10 # number of cores - 1
124 inc %r10 # number of cores
128 bt \$28,%edx # test hyper-threading bit
130 shr \$16,%ebx # number of logical processors
133 and \$0xefffffff,%edx # ~(1<<28)
142 mov \$0,%ecx # query L1D
146 and \$0xfff,%r10d # number of cores -1 per L1D
151 movd %eax,%xmm0 # put aside processor id
152 and \$0xbfefffff,%edx # force reserved bits to 0
155 or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
157 cmp \$15,%ah # examine Family ID
159 or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
163 and \$0x0fff0ff0,%eax
164 cmp \$0x00050670,%eax # Knights Landing
166 cmp \$0x00080650,%eax # Knights Mill (according to sde)
169 and \$0xfbffffff,%ecx # clear XSAVE flag to mimic Silvermont
172 bt \$28,%edx # test hyper-threading bit
174 and \$0xefffffff,%edx # ~(1<<28)
178 or \$0x10000000,%edx # 1<<28
180 cmp \$1,%bl # see if cache is shared
182 and \$0xefffffff,%edx # ~(1<<28)
184 and \$0x00000800,%r9d # isolate AMD XOP flag
185 and \$0xfffff7ff,%ecx
186 or %ecx,%r9d # merge AMD XOP flag
188 mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
191 jb .Lno_extended_info
195 bt \$26,%r9d # check XSAVE bit, cleared on Knights
197 and \$0xfff7ffff,%ebx # clear ADCX/ADOX flag
199 movd %xmm0,%eax # restore processor id
200 and \$0x0fff0ff0,%eax
201 cmp \$0x00050650,%eax # Skylake-X
203 and \$0xfffeffff,%ebx # ~(1<<16)
204 # suppress AVX512F flag on Skylake-X
206 mov %ebx,8(%rdi) # save extended feature flags
210 bt \$27,%r9d # check OSXSAVE bit
213 .byte 0x0f,0x01,0xd0 # xgetbv
214 and \$0xe6,%eax # isolate XMM, YMM and ZMM state support
217 andl \$0x3fdeffff,8(%rdi) # ~(1<<31|1<<30|1<<21|1<<16)
218 # clear AVX512F+BW+VL+FIMA, all of
219 # them are EVEX-encoded, which requires
220 # ZMM state support even if one uses
221 # only XMM and YMM :-(
222 and \$6,%eax # isolate XMM and YMM state support
226 mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
227 and %eax,%r9d # clear AVX, FMA and AMD XOP bits
228 mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
229 and %eax,8(%rdi) # clear AVX2 and AVX512* bits
233 mov %r8,%rbx # restore %rbx
238 .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
240 .globl OPENSSL_cleanse
241 .type OPENSSL_cleanse,\@abi-omnipotent
276 .size OPENSSL_cleanse,.-OPENSSL_cleanse
279 .type CRYPTO_memcmp,\@abi-omnipotent
313 .size CRYPTO_memcmp,.-CRYPTO_memcmp
316 print<<___ if (!$win64);
317 .globl OPENSSL_wipe_cpu
318 .type OPENSSL_wipe_cpu,\@abi-omnipotent
350 .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
352 print<<___ if ($win64);
353 .globl OPENSSL_wipe_cpu
354 .type OPENSSL_wipe_cpu,\@abi-omnipotent
371 .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
379 my $redzone=win64?8:-8;
382 .globl OPENSSL_instrument_bus
383 .type OPENSSL_instrument_bus,\@abi-omnipotent
385 OPENSSL_instrument_bus:
388 mov $arg1,$out # tribute to Win64
392 rdtsc # collect 1st tick
393 mov %eax,$lasttick # lasttick = tick
394 mov \$0,$lastdiff # lastdiff = 0
415 .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
417 .globl OPENSSL_instrument_bus2
418 .type OPENSSL_instrument_bus2,\@abi-omnipotent
420 OPENSSL_instrument_bus2:
423 mov $arg1,$out # tribute to Win64
426 mov $cnt,$redzone(%rsp)
428 rdtsc # collect 1st tick
429 mov %eax,$lasttick # lasttick = tick
430 mov \$0,$lastdiff # lastdiff = 0
436 rdtsc # collect 1st diff
438 sub $lasttick,%eax # diff
439 mov %edx,$lasttick # lasttick = tick
440 mov %eax,$lastdiff # lastdiff = diff
444 add %eax,($out) # accumulate diff
451 sub $lasttick,%eax # diff
452 mov %edx,$lasttick # lasttick = tick
454 mov %eax,$lastdiff # lastdiff = diff
457 sub %rdx,$cnt # conditional --$cnt
458 lea ($out,%rdx,4),$out # conditional ++$out
462 mov $redzone(%rsp),%rax
466 .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
473 .globl OPENSSL_ia32_${rdop}_bytes
474 .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
476 OPENSSL_ia32_${rdop}_bytes:
479 xor %rax, %rax # return value
481 je .Ldone_${rdop}_bytes
486 jc .Lbreak_${rdop}_bytes
488 jnz .Loop_${rdop}_bytes
489 jmp .Ldone_${rdop}_bytes
492 .Lbreak_${rdop}_bytes:
494 jb .Ltail_${rdop}_bytes
499 jz .Ldone_${rdop}_bytes
501 jmp .Loop_${rdop}_bytes
504 .Ltail_${rdop}_bytes:
510 jnz .Ltail_${rdop}_bytes
512 .Ldone_${rdop}_bytes:
513 xor %r10,%r10 # Clear sensitive data from register
516 .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
519 gen_random("rdrand");
520 gen_random("rdseed");
522 close STDOUT or die "error closing STDOUT: $!"; # flush