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