Remove some commented out code in libcrypto
[openssl.git] / crypto / x86_64cpuid.pl
1 #! /usr/bin/env perl
2 # Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
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
8
9
10 $flavour = shift;
11 $output  = shift;
12 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
13
14 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
15
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";
20
21 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
22 *STDOUT=*OUT;
23
24 ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
25                                  ("%rdi","%rsi","%rdx","%rcx"); # Unix order
26
27 print<<___;
28 .extern         OPENSSL_cpuid_setup
29 .hidden         OPENSSL_cpuid_setup
30 .section        .init
31         call    OPENSSL_cpuid_setup
32
33 .hidden OPENSSL_ia32cap_P
34 .comm   OPENSSL_ia32cap_P,16,4
35
36 .text
37
38 .globl  OPENSSL_atomic_add
39 .type   OPENSSL_atomic_add,\@abi-omnipotent
40 .align  16
41 OPENSSL_atomic_add:
42         movl    ($arg1),%eax
43 .Lspin: leaq    ($arg2,%rax),%r8
44         .byte   0xf0            # lock
45         cmpxchgl        %r8d,($arg1)
46         jne     .Lspin
47         movl    %r8d,%eax
48         .byte   0x48,0x98       # cltq/cdqe
49         ret
50 .size   OPENSSL_atomic_add,.-OPENSSL_atomic_add
51
52 .globl  OPENSSL_rdtsc
53 .type   OPENSSL_rdtsc,\@abi-omnipotent
54 .align  16
55 OPENSSL_rdtsc:
56         rdtsc
57         shl     \$32,%rdx
58         or      %rdx,%rax
59         ret
60 .size   OPENSSL_rdtsc,.-OPENSSL_rdtsc
61
62 .globl  OPENSSL_ia32_cpuid
63 .type   OPENSSL_ia32_cpuid,\@function,1
64 .align  16
65 OPENSSL_ia32_cpuid:
66 .cfi_startproc
67         mov     %rbx,%r8                # save %rbx
68 .cfi_register   %rbx,%r8
69
70         xor     %eax,%eax
71         mov     %eax,8(%rdi)            # clear 3rd word
72         cpuid
73         mov     %eax,%r11d              # max value for standard query level
74
75         xor     %eax,%eax
76         cmp     \$0x756e6547,%ebx       # "Genu"
77         setne   %al
78         mov     %eax,%r9d
79         cmp     \$0x49656e69,%edx       # "ineI"
80         setne   %al
81         or      %eax,%r9d
82         cmp     \$0x6c65746e,%ecx       # "ntel"
83         setne   %al
84         or      %eax,%r9d               # 0 indicates Intel CPU
85         jz      .Lintel
86
87         cmp     \$0x68747541,%ebx       # "Auth"
88         setne   %al
89         mov     %eax,%r10d
90         cmp     \$0x69746E65,%edx       # "enti"
91         setne   %al
92         or      %eax,%r10d
93         cmp     \$0x444D4163,%ecx       # "cAMD"
94         setne   %al
95         or      %eax,%r10d              # 0 indicates AMD CPU
96         jnz     .Lintel
97
98         # AMD specific
99         mov     \$0x80000000,%eax
100         cpuid
101         cmp     \$0x80000001,%eax
102         jb      .Lintel
103         mov     %eax,%r10d
104         mov     \$0x80000001,%eax
105         cpuid
106         or      %ecx,%r9d
107         and     \$0x00000801,%r9d       # isolate AMD XOP bit, 1<<11
108
109         cmp     \$0x80000008,%r10d
110         jb      .Lintel
111
112         mov     \$0x80000008,%eax
113         cpuid
114         movzb   %cl,%r10                # number of cores - 1
115         inc     %r10                    # number of cores
116
117         mov     \$1,%eax
118         cpuid
119         bt      \$28,%edx               # test hyper-threading bit
120         jnc     .Lgeneric
121         shr     \$16,%ebx               # number of logical processors
122         cmp     %r10b,%bl
123         ja      .Lgeneric
124         and     \$0xefffffff,%edx       # ~(1<<28)
125         jmp     .Lgeneric
126
127 .Lintel:
128         cmp     \$4,%r11d
129         mov     \$-1,%r10d
130         jb      .Lnocacheinfo
131
132         mov     \$4,%eax
133         mov     \$0,%ecx                # query L1D
134         cpuid
135         mov     %eax,%r10d
136         shr     \$14,%r10d
137         and     \$0xfff,%r10d           # number of cores -1 per L1D
138
139         cmp     \$7,%r11d
140         jb      .Lnocacheinfo
141
142         mov     \$7,%eax
143         xor     %ecx,%ecx
144         cpuid
145         mov     %ebx,8(%rdi)
146
147 .Lnocacheinfo:
148         mov     \$1,%eax
149         cpuid
150         and     \$0xbfefffff,%edx       # force reserved bits to 0
151         cmp     \$0,%r9d
152         jne     .Lnotintel
153         or      \$0x40000000,%edx       # set reserved bit#30 on Intel CPUs
154         and     \$15,%ah
155         cmp     \$15,%ah                # examine Family ID
156         jne     .Lnotintel
157         or      \$0x00100000,%edx       # set reserved bit#20 to engage RC4_CHAR
158 .Lnotintel:
159         bt      \$28,%edx               # test hyper-threading bit
160         jnc     .Lgeneric
161         and     \$0xefffffff,%edx       # ~(1<<28)
162         cmp     \$0,%r10d
163         je      .Lgeneric
164
165         or      \$0x10000000,%edx       # 1<<28
166         shr     \$16,%ebx
167         cmp     \$1,%bl                 # see if cache is shared
168         ja      .Lgeneric
169         and     \$0xefffffff,%edx       # ~(1<<28)
170 .Lgeneric:
171         and     \$0x00000800,%r9d       # isolate AMD XOP flag
172         and     \$0xfffff7ff,%ecx
173         or      %ecx,%r9d               # merge AMD XOP flag
174
175         mov     %edx,%r10d              # %r9d:%r10d is copy of %ecx:%edx
176         bt      \$27,%r9d               # check OSXSAVE bit
177         jnc     .Lclear_avx
178         xor     %ecx,%ecx               # XCR0
179         .byte   0x0f,0x01,0xd0          # xgetbv
180         and     \$0xe6,%eax             # isolate XMM, YMM and ZMM state support
181         cmp     \$0xe6,%eax
182         je      .Ldone
183         andl    \$0xfffeffff,8(%rdi)    # clear AVX512F, ~(1<<16)
184                                         # note that we don't touch other AVX512
185                                         # extensions, because they can be used
186                                         # with YMM (without opmasking though)
187         and     \$6,%eax                # isolate XMM and YMM state support
188         cmp     \$6,%eax
189         je      .Ldone
190 .Lclear_avx:
191         mov     \$0xefffe7ff,%eax       # ~(1<<28|1<<12|1<<11)
192         and     %eax,%r9d               # clear AVX, FMA and AMD XOP bits
193         mov     \$0x3fdeffdf,%eax       # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
194         and     %eax,8(%rdi)            # cleax AVX2 and AVX512* bits
195 .Ldone:
196         shl     \$32,%r9
197         mov     %r10d,%eax
198         mov     %r8,%rbx                # restore %rbx
199 .cfi_restore    %rbx
200         or      %r9,%rax
201         ret
202 .cfi_endproc
203 .size   OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
204
205 .globl  OPENSSL_cleanse
206 .type   OPENSSL_cleanse,\@abi-omnipotent
207 .align  16
208 OPENSSL_cleanse:
209         xor     %rax,%rax
210         cmp     \$15,$arg2
211         jae     .Lot
212         cmp     \$0,$arg2
213         je      .Lret
214 .Little:
215         mov     %al,($arg1)
216         sub     \$1,$arg2
217         lea     1($arg1),$arg1
218         jnz     .Little
219 .Lret:
220         ret
221 .align  16
222 .Lot:
223         test    \$7,$arg1
224         jz      .Laligned
225         mov     %al,($arg1)
226         lea     -1($arg2),$arg2
227         lea     1($arg1),$arg1
228         jmp     .Lot
229 .Laligned:
230         mov     %rax,($arg1)
231         lea     -8($arg2),$arg2
232         test    \$-8,$arg2
233         lea     8($arg1),$arg1
234         jnz     .Laligned
235         cmp     \$0,$arg2
236         jne     .Little
237         ret
238 .size   OPENSSL_cleanse,.-OPENSSL_cleanse
239
240 .globl  CRYPTO_memcmp
241 .type   CRYPTO_memcmp,\@abi-omnipotent
242 .align  16
243 CRYPTO_memcmp:
244         xor     %rax,%rax
245         xor     %r10,%r10
246         cmp     \$0,$arg3
247         je      .Lno_data
248 .Loop_cmp:
249         mov     ($arg1),%r10b
250         lea     1($arg1),$arg1
251         xor     ($arg2),%r10b
252         lea     1($arg2),$arg2
253         or      %r10b,%al
254         dec     $arg3
255         jnz     .Loop_cmp
256         neg     %rax
257         shr     \$63,%rax
258 .Lno_data:
259         ret
260 .size   CRYPTO_memcmp,.-CRYPTO_memcmp
261 ___
262
263 print<<___ if (!$win64);
264 .globl  OPENSSL_wipe_cpu
265 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
266 .align  16
267 OPENSSL_wipe_cpu:
268         pxor    %xmm0,%xmm0
269         pxor    %xmm1,%xmm1
270         pxor    %xmm2,%xmm2
271         pxor    %xmm3,%xmm3
272         pxor    %xmm4,%xmm4
273         pxor    %xmm5,%xmm5
274         pxor    %xmm6,%xmm6
275         pxor    %xmm7,%xmm7
276         pxor    %xmm8,%xmm8
277         pxor    %xmm9,%xmm9
278         pxor    %xmm10,%xmm10
279         pxor    %xmm11,%xmm11
280         pxor    %xmm12,%xmm12
281         pxor    %xmm13,%xmm13
282         pxor    %xmm14,%xmm14
283         pxor    %xmm15,%xmm15
284         xorq    %rcx,%rcx
285         xorq    %rdx,%rdx
286         xorq    %rsi,%rsi
287         xorq    %rdi,%rdi
288         xorq    %r8,%r8
289         xorq    %r9,%r9
290         xorq    %r10,%r10
291         xorq    %r11,%r11
292         leaq    8(%rsp),%rax
293         ret
294 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
295 ___
296 print<<___ if ($win64);
297 .globl  OPENSSL_wipe_cpu
298 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
299 .align  16
300 OPENSSL_wipe_cpu:
301         pxor    %xmm0,%xmm0
302         pxor    %xmm1,%xmm1
303         pxor    %xmm2,%xmm2
304         pxor    %xmm3,%xmm3
305         pxor    %xmm4,%xmm4
306         pxor    %xmm5,%xmm5
307         xorq    %rcx,%rcx
308         xorq    %rdx,%rdx
309         xorq    %r8,%r8
310         xorq    %r9,%r9
311         xorq    %r10,%r10
312         xorq    %r11,%r11
313         leaq    8(%rsp),%rax
314         ret
315 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
316 ___
317 {
318 my $out="%r10";
319 my $cnt="%rcx";
320 my $max="%r11";
321 my $lasttick="%r8d";
322 my $lastdiff="%r9d";
323 my $redzone=win64?8:-8;
324
325 print<<___;
326 .globl  OPENSSL_instrument_bus
327 .type   OPENSSL_instrument_bus,\@abi-omnipotent
328 .align  16
329 OPENSSL_instrument_bus:
330         mov     $arg1,$out      # tribute to Win64
331         mov     $arg2,$cnt
332         mov     $arg2,$max
333
334         rdtsc                   # collect 1st tick
335         mov     %eax,$lasttick  # lasttick = tick
336         mov     \$0,$lastdiff   # lastdiff = 0
337         clflush ($out)
338         .byte   0xf0            # lock
339         add     $lastdiff,($out)
340         jmp     .Loop
341 .align  16
342 .Loop:  rdtsc
343         mov     %eax,%edx
344         sub     $lasttick,%eax
345         mov     %edx,$lasttick
346         mov     %eax,$lastdiff
347         clflush ($out)
348         .byte   0xf0            # lock
349         add     %eax,($out)
350         lea     4($out),$out
351         sub     \$1,$cnt
352         jnz     .Loop
353
354         mov     $max,%rax
355         ret
356 .size   OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
357
358 .globl  OPENSSL_instrument_bus2
359 .type   OPENSSL_instrument_bus2,\@abi-omnipotent
360 .align  16
361 OPENSSL_instrument_bus2:
362         mov     $arg1,$out      # tribute to Win64
363         mov     $arg2,$cnt
364         mov     $arg3,$max
365         mov     $cnt,$redzone(%rsp)
366
367         rdtsc                   # collect 1st tick
368         mov     %eax,$lasttick  # lasttick = tick
369         mov     \$0,$lastdiff   # lastdiff = 0
370
371         clflush ($out)
372         .byte   0xf0            # lock
373         add     $lastdiff,($out)
374
375         rdtsc                   # collect 1st diff
376         mov     %eax,%edx
377         sub     $lasttick,%eax  # diff
378         mov     %edx,$lasttick  # lasttick = tick
379         mov     %eax,$lastdiff  # lastdiff = diff
380 .Loop2:
381         clflush ($out)
382         .byte   0xf0            # lock
383         add     %eax,($out)     # accumulate diff
384
385         sub     \$1,$max
386         jz      .Ldone2
387
388         rdtsc
389         mov     %eax,%edx
390         sub     $lasttick,%eax  # diff
391         mov     %edx,$lasttick  # lasttick = tick
392         cmp     $lastdiff,%eax
393         mov     %eax,$lastdiff  # lastdiff = diff
394         mov     \$0,%edx
395         setne   %dl
396         sub     %rdx,$cnt       # conditional --$cnt
397         lea     ($out,%rdx,4),$out      # conditional ++$out
398         jnz     .Loop2
399
400 .Ldone2:
401         mov     $redzone(%rsp),%rax
402         sub     $cnt,%rax
403         ret
404 .size   OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
405 ___
406 }
407
408 sub gen_random {
409 my $rdop = shift;
410 print<<___;
411 .globl  OPENSSL_ia32_${rdop}
412 .type   OPENSSL_ia32_${rdop},\@abi-omnipotent
413 .align  16
414 OPENSSL_ia32_${rdop}:
415         mov     \$8,%ecx
416 .Loop_${rdop}:
417         ${rdop} %rax
418         jc      .Lbreak_${rdop}
419         loop    .Loop_${rdop}
420 .Lbreak_${rdop}:
421         cmp     \$0,%rax
422         cmove   %rcx,%rax
423         ret
424 .size   OPENSSL_ia32_${rdop},.-OPENSSL_ia32_${rdop}
425
426 .globl  OPENSSL_ia32_${rdop}_bytes
427 .type   OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
428 .align  16
429 OPENSSL_ia32_${rdop}_bytes:
430         xor     %rax, %rax      # return value
431         cmp     \$0,$arg2
432         je      .Ldone_${rdop}_bytes
433
434         mov     \$8,%r11
435 .Loop_${rdop}_bytes:
436         ${rdop} %r10
437         jc      .Lbreak_${rdop}_bytes
438         dec     %r11
439         jnz     .Loop_${rdop}_bytes
440         jmp     .Ldone_${rdop}_bytes
441
442 .align  16
443 .Lbreak_${rdop}_bytes:
444         cmp     \$8,$arg2
445         jb      .Ltail_${rdop}_bytes
446         mov     %r10,($arg1)
447         lea     8($arg1),$arg1
448         add     \$8,%rax
449         sub     \$8,$arg2
450         jz      .Ldone_${rdop}_bytes
451         mov     \$8,%r11
452         jmp     .Loop_${rdop}_bytes
453
454 .align  16
455 .Ltail_${rdop}_bytes:
456         mov     %r10b,($arg1)
457         lea     1($arg1),$arg1
458         inc     %rax
459         shr     \$8,%r8
460         dec     $arg2
461         jnz     .Ltail_${rdop}_bytes
462
463 .Ldone_${rdop}_bytes:
464         ret
465 .size   OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
466 ___
467 }
468 gen_random("rdrand");
469 gen_random("rdseed");
470
471 close STDOUT;   # flush