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