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