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