Check that we were actually allocated BIGNUMs in dsa_builtin_paramgen2
[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         mov     %rbx,%r8                # save %rbx
67
68         xor     %eax,%eax
69         mov     %eax,8(%rdi)            # clear 3rd word
70         cpuid
71         mov     %eax,%r11d              # max value for standard query level
72
73         xor     %eax,%eax
74         cmp     \$0x756e6547,%ebx       # "Genu"
75         setne   %al
76         mov     %eax,%r9d
77         cmp     \$0x49656e69,%edx       # "ineI"
78         setne   %al
79         or      %eax,%r9d
80         cmp     \$0x6c65746e,%ecx       # "ntel"
81         setne   %al
82         or      %eax,%r9d               # 0 indicates Intel CPU
83         jz      .Lintel
84
85         cmp     \$0x68747541,%ebx       # "Auth"
86         setne   %al
87         mov     %eax,%r10d
88         cmp     \$0x69746E65,%edx       # "enti"
89         setne   %al
90         or      %eax,%r10d
91         cmp     \$0x444D4163,%ecx       # "cAMD"
92         setne   %al
93         or      %eax,%r10d              # 0 indicates AMD CPU
94         jnz     .Lintel
95
96         # AMD specific
97         mov     \$0x80000000,%eax
98         cpuid
99         cmp     \$0x80000001,%eax
100         jb      .Lintel
101         mov     %eax,%r10d
102         mov     \$0x80000001,%eax
103         cpuid
104         or      %ecx,%r9d
105         and     \$0x00000801,%r9d       # isolate AMD XOP bit, 1<<11
106
107         cmp     \$0x80000008,%r10d
108         jb      .Lintel
109
110         mov     \$0x80000008,%eax
111         cpuid
112         movzb   %cl,%r10                # number of cores - 1
113         inc     %r10                    # number of cores
114
115         mov     \$1,%eax
116         cpuid
117         bt      \$28,%edx               # test hyper-threading bit
118         jnc     .Lgeneric
119         shr     \$16,%ebx               # number of logical processors
120         cmp     %r10b,%bl
121         ja      .Lgeneric
122         and     \$0xefffffff,%edx       # ~(1<<28)
123         jmp     .Lgeneric
124
125 .Lintel:
126         cmp     \$4,%r11d
127         mov     \$-1,%r10d
128         jb      .Lnocacheinfo
129
130         mov     \$4,%eax
131         mov     \$0,%ecx                # query L1D
132         cpuid
133         mov     %eax,%r10d
134         shr     \$14,%r10d
135         and     \$0xfff,%r10d           # number of cores -1 per L1D
136
137         cmp     \$7,%r11d
138         jb      .Lnocacheinfo
139
140         mov     \$7,%eax
141         xor     %ecx,%ecx
142         cpuid
143         mov     %ebx,8(%rdi)
144
145 .Lnocacheinfo:
146         mov     \$1,%eax
147         cpuid
148         and     \$0xbfefffff,%edx       # force reserved bits to 0
149         cmp     \$0,%r9d
150         jne     .Lnotintel
151         or      \$0x40000000,%edx       # set reserved bit#30 on Intel CPUs
152         and     \$15,%ah
153         cmp     \$15,%ah                # examine Family ID
154         jne     .Lnotintel
155         or      \$0x00100000,%edx       # set reserved bit#20 to engage RC4_CHAR
156 .Lnotintel:
157         bt      \$28,%edx               # test hyper-threading bit
158         jnc     .Lgeneric
159         and     \$0xefffffff,%edx       # ~(1<<28)
160         cmp     \$0,%r10d
161         je      .Lgeneric
162
163         or      \$0x10000000,%edx       # 1<<28
164         shr     \$16,%ebx
165         cmp     \$1,%bl                 # see if cache is shared
166         ja      .Lgeneric
167         and     \$0xefffffff,%edx       # ~(1<<28)
168 .Lgeneric:
169         and     \$0x00000800,%r9d       # isolate AMD XOP flag
170         and     \$0xfffff7ff,%ecx
171         or      %ecx,%r9d               # merge AMD XOP flag
172
173         mov     %edx,%r10d              # %r9d:%r10d is copy of %ecx:%edx
174         bt      \$27,%r9d               # check OSXSAVE bit
175         jnc     .Lclear_avx
176         xor     %ecx,%ecx               # XCR0
177         .byte   0x0f,0x01,0xd0          # xgetbv
178         and     \$6,%eax                # isolate XMM and YMM state support
179         cmp     \$6,%eax
180         je      .Ldone
181 .Lclear_avx:
182         mov     \$0xefffe7ff,%eax       # ~(1<<28|1<<12|1<<11)
183         and     %eax,%r9d               # clear AVX, FMA and AMD XOP bits
184         andl    \$0xffffffdf,8(%rdi)    # cleax AVX2, ~(1<<5)
185 .Ldone:
186         shl     \$32,%r9
187         mov     %r10d,%eax
188         mov     %r8,%rbx                # restore %rbx
189         or      %r9,%rax
190         ret
191 .size   OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
192
193 .globl  OPENSSL_cleanse
194 .type   OPENSSL_cleanse,\@abi-omnipotent
195 .align  16
196 OPENSSL_cleanse:
197         xor     %rax,%rax
198         cmp     \$15,$arg2
199         jae     .Lot
200         cmp     \$0,$arg2
201         je      .Lret
202 .Little:
203         mov     %al,($arg1)
204         sub     \$1,$arg2
205         lea     1($arg1),$arg1
206         jnz     .Little
207 .Lret:
208         ret
209 .align  16
210 .Lot:
211         test    \$7,$arg1
212         jz      .Laligned
213         mov     %al,($arg1)
214         lea     -1($arg2),$arg2
215         lea     1($arg1),$arg1
216         jmp     .Lot
217 .Laligned:
218         mov     %rax,($arg1)
219         lea     -8($arg2),$arg2
220         test    \$-8,$arg2
221         lea     8($arg1),$arg1
222         jnz     .Laligned
223         cmp     \$0,$arg2
224         jne     .Little
225         ret
226 .size   OPENSSL_cleanse,.-OPENSSL_cleanse
227 ___
228
229 print<<___ if (!$win64);
230 .globl  OPENSSL_wipe_cpu
231 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
232 .align  16
233 OPENSSL_wipe_cpu:
234         pxor    %xmm0,%xmm0
235         pxor    %xmm1,%xmm1
236         pxor    %xmm2,%xmm2
237         pxor    %xmm3,%xmm3
238         pxor    %xmm4,%xmm4
239         pxor    %xmm5,%xmm5
240         pxor    %xmm6,%xmm6
241         pxor    %xmm7,%xmm7
242         pxor    %xmm8,%xmm8
243         pxor    %xmm9,%xmm9
244         pxor    %xmm10,%xmm10
245         pxor    %xmm11,%xmm11
246         pxor    %xmm12,%xmm12
247         pxor    %xmm13,%xmm13
248         pxor    %xmm14,%xmm14
249         pxor    %xmm15,%xmm15
250         xorq    %rcx,%rcx
251         xorq    %rdx,%rdx
252         xorq    %rsi,%rsi
253         xorq    %rdi,%rdi
254         xorq    %r8,%r8
255         xorq    %r9,%r9
256         xorq    %r10,%r10
257         xorq    %r11,%r11
258         leaq    8(%rsp),%rax
259         ret
260 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
261 ___
262 print<<___ if ($win64);
263 .globl  OPENSSL_wipe_cpu
264 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
265 .align  16
266 OPENSSL_wipe_cpu:
267         pxor    %xmm0,%xmm0
268         pxor    %xmm1,%xmm1
269         pxor    %xmm2,%xmm2
270         pxor    %xmm3,%xmm3
271         pxor    %xmm4,%xmm4
272         pxor    %xmm5,%xmm5
273         xorq    %rcx,%rcx
274         xorq    %rdx,%rdx
275         xorq    %r8,%r8
276         xorq    %r9,%r9
277         xorq    %r10,%r10
278         xorq    %r11,%r11
279         leaq    8(%rsp),%rax
280         ret
281 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
282 ___
283 {
284 my $out="%r10";
285 my $cnt="%rcx";
286 my $max="%r11";
287 my $lasttick="%r8d";
288 my $lastdiff="%r9d";
289 my $redzone=win64?8:-8;
290
291 print<<___;
292 .globl  OPENSSL_instrument_bus
293 .type   OPENSSL_instrument_bus,\@abi-omnipotent
294 .align  16
295 OPENSSL_instrument_bus:
296         mov     $arg1,$out      # tribute to Win64
297         mov     $arg2,$cnt
298         mov     $arg2,$max
299
300         rdtsc                   # collect 1st tick
301         mov     %eax,$lasttick  # lasttick = tick
302         mov     \$0,$lastdiff   # lastdiff = 0
303         clflush ($out)
304         .byte   0xf0            # lock
305         add     $lastdiff,($out)
306         jmp     .Loop
307 .align  16
308 .Loop:  rdtsc
309         mov     %eax,%edx
310         sub     $lasttick,%eax
311         mov     %edx,$lasttick
312         mov     %eax,$lastdiff
313         clflush ($out)
314         .byte   0xf0            # lock
315         add     %eax,($out)
316         lea     4($out),$out
317         sub     \$1,$cnt
318         jnz     .Loop
319
320         mov     $max,%rax
321         ret
322 .size   OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
323
324 .globl  OPENSSL_instrument_bus2
325 .type   OPENSSL_instrument_bus2,\@abi-omnipotent
326 .align  16
327 OPENSSL_instrument_bus2:
328         mov     $arg1,$out      # tribute to Win64
329         mov     $arg2,$cnt
330         mov     $arg3,$max
331         mov     $cnt,$redzone(%rsp)
332
333         rdtsc                   # collect 1st tick
334         mov     %eax,$lasttick  # lasttick = tick
335         mov     \$0,$lastdiff   # lastdiff = 0
336
337         clflush ($out)
338         .byte   0xf0            # lock
339         add     $lastdiff,($out)
340
341         rdtsc                   # collect 1st diff
342         mov     %eax,%edx
343         sub     $lasttick,%eax  # diff
344         mov     %edx,$lasttick  # lasttick = tick
345         mov     %eax,$lastdiff  # lastdiff = diff
346 .Loop2:
347         clflush ($out)
348         .byte   0xf0            # lock
349         add     %eax,($out)     # accumulate diff
350
351         sub     \$1,$max
352         jz      .Ldone2
353
354         rdtsc
355         mov     %eax,%edx
356         sub     $lasttick,%eax  # diff
357         mov     %edx,$lasttick  # lasttick = tick
358         cmp     $lastdiff,%eax
359         mov     %eax,$lastdiff  # lastdiff = diff
360         mov     \$0,%edx
361         setne   %dl
362         sub     %rdx,$cnt       # conditional --$cnt
363         lea     ($out,%rdx,4),$out      # conditional ++$out
364         jnz     .Loop2
365
366 .Ldone2:
367         mov     $redzone(%rsp),%rax
368         sub     $cnt,%rax
369         ret
370 .size   OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
371 ___
372 }
373
374 print<<___;
375 .globl  OPENSSL_ia32_rdrand
376 .type   OPENSSL_ia32_rdrand,\@abi-omnipotent
377 .align  16
378 OPENSSL_ia32_rdrand:
379         mov     \$8,%ecx
380 .Loop_rdrand:
381         rdrand  %rax
382         jc      .Lbreak_rdrand
383         loop    .Loop_rdrand
384 .Lbreak_rdrand:
385         cmp     \$0,%rax
386         cmove   %rcx,%rax
387         ret
388 .size   OPENSSL_ia32_rdrand,.-OPENSSL_ia32_rdrand
389
390 .globl  OPENSSL_ia32_rdseed
391 .type   OPENSSL_ia32_rdseed,\@abi-omnipotent
392 .align  16
393 OPENSSL_ia32_rdseed:
394         mov     \$8,%ecx
395 .Loop_rdseed:
396         rdseed  %rax
397         jc      .Lbreak_rdseed
398         loop    .Loop_rdseed
399 .Lbreak_rdseed:
400         cmp     \$0,%rax
401         cmove   %rcx,%rax
402         ret
403 .size   OPENSSL_ia32_rdseed,.-OPENSSL_ia32_rdseed
404 ___
405
406 close STDOUT;   # flush