2 # Copyright 2005-2016 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
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
66 mov %rbx,%r8 # save %rbx
69 mov %eax,8(%rdi) # clear 3rd word
71 mov %eax,%r11d # max value for standard query level
84 cmp \$0x756e6547,%ebx # "Genu"
87 cmp \$0x49656e69,%edx # "ineI"
90 cmp \$0x6c65746e,%ecx # "ntel"
92 or %eax,%r9d # 0 indicates Intel CPU
95 cmp \$0x68747541,%ebx # "Auth"
98 cmp \$0x69746E65,%edx # "enti"
101 cmp \$0x444D4163,%ecx # "cAMD"
103 or %eax,%r10d # 0 indicates AMD CPU
107 mov \$0x80000000,%eax
109 cmp \$0x80000001,%eax
112 mov \$0x80000001,%eax
115 and \$0x00000801,%r9d # isolate AMD XOP bit, 1<<11
117 cmp \$0x80000008,%r10d
120 mov \$0x80000008,%eax
122 movzb %cl,%r10 # number of cores - 1
123 inc %r10 # number of cores
127 bt \$28,%edx # test hyper-threading bit
129 shr \$16,%ebx # number of logical processors
132 and \$0xefffffff,%edx # ~(1<<28)
141 mov \$0,%ecx # query L1D
145 and \$0xfff,%r10d # number of cores -1 per L1D
150 and \$0xbfefffff,%edx # force reserved bits to 0
153 or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
155 cmp \$15,%ah # examine Family ID
157 or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
159 bt \$28,%edx # test hyper-threading bit
161 and \$0xefffffff,%edx # ~(1<<28)
165 or \$0x10000000,%edx # 1<<28
167 cmp \$1,%bl # see if cache is shared
169 and \$0xefffffff,%edx # ~(1<<28)
171 and \$0x00000800,%r9d # isolate AMD XOP flag
172 and \$0xfffff7ff,%ecx
173 or %ecx,%r9d # merge AMD XOP flag
175 mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
176 bt \$27,%r9d # check OSXSAVE bit
179 .byte 0x0f,0x01,0xd0 # xgetbv
180 and \$6,%eax # isolate XMM and YMM state support
184 mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
185 and %eax,%r9d # clear AVX, FMA and AMD XOP bits
186 andl \$0xffffffdf,8(%rdi) # cleax AVX2, ~(1<<5)
190 mov %r8,%rbx # restore %rbx
193 .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
195 .globl OPENSSL_cleanse
196 .type OPENSSL_cleanse,\@abi-omnipotent
228 .size OPENSSL_cleanse,.-OPENSSL_cleanse
231 .type CRYPTO_memcmp,\@abi-omnipotent
250 .size CRYPTO_memcmp,.-CRYPTO_memcmp
253 print<<___ if (!$win64);
254 .globl OPENSSL_wipe_cpu
255 .type OPENSSL_wipe_cpu,\@abi-omnipotent
284 .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
286 print<<___ if ($win64);
287 .globl OPENSSL_wipe_cpu
288 .type OPENSSL_wipe_cpu,\@abi-omnipotent
305 .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
313 my $redzone=win64?8:-8;
316 .globl OPENSSL_instrument_bus
317 .type OPENSSL_instrument_bus,\@abi-omnipotent
319 OPENSSL_instrument_bus:
320 mov $arg1,$out # tribute to Win64
324 rdtsc # collect 1st tick
325 mov %eax,$lasttick # lasttick = tick
326 mov \$0,$lastdiff # lastdiff = 0
346 .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
348 .globl OPENSSL_instrument_bus2
349 .type OPENSSL_instrument_bus2,\@abi-omnipotent
351 OPENSSL_instrument_bus2:
352 mov $arg1,$out # tribute to Win64
355 mov $cnt,$redzone(%rsp)
357 rdtsc # collect 1st tick
358 mov %eax,$lasttick # lasttick = tick
359 mov \$0,$lastdiff # lastdiff = 0
365 rdtsc # collect 1st diff
367 sub $lasttick,%eax # diff
368 mov %edx,$lasttick # lasttick = tick
369 mov %eax,$lastdiff # lastdiff = diff
373 add %eax,($out) # accumulate diff
380 sub $lasttick,%eax # diff
381 mov %edx,$lasttick # lasttick = tick
383 mov %eax,$lastdiff # lastdiff = diff
386 sub %rdx,$cnt # conditional --$cnt
387 lea ($out,%rdx,4),$out # conditional ++$out
391 mov $redzone(%rsp),%rax
394 .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
401 .globl OPENSSL_ia32_${rdop}
402 .type OPENSSL_ia32_${rdop},\@abi-omnipotent
404 OPENSSL_ia32_${rdop}:
414 .size OPENSSL_ia32_${rdop},.-OPENSSL_ia32_${rdop}
416 .globl OPENSSL_ia32_${rdop}_bytes
417 .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
419 OPENSSL_ia32_${rdop}_bytes:
420 xor %rax, %rax # return value
422 je .Ldone_${rdop}_bytes
427 jc .Lbreak_${rdop}_bytes
429 jnz .Loop_${rdop}_bytes
430 jmp .Ldone_${rdop}_bytes
433 .Lbreak_${rdop}_bytes:
435 jb .Ltail_${rdop}_bytes
440 jz .Ldone_${rdop}_bytes
442 jmp .Loop_${rdop}_bytes
445 .Ltail_${rdop}_bytes:
451 jnz .Ltail_${rdop}_bytes
453 .Ldone_${rdop}_bytes:
455 .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
458 gen_random("rdrand");
459 gen_random("rdseed");
461 close STDOUT; # flush