first parameter is difference in days, not years
[openssl.git] / crypto / x86_64cpuid.pl
1 #!/usr/bin/env perl
2
3 $flavour = shift;
4 $output  = shift;
5 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
6
7 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
8
9 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
10 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
11 ( $xlate="${dir}perlasm/x86_64-xlate.pl" and -f $xlate) or
12 die "can't locate x86_64-xlate.pl";
13
14 open STDOUT,"| \"$^X\" $xlate $flavour $output";
15
16 ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
17                                  ("%rdi","%rsi","%rdx","%rcx"); # Unix order
18
19 print<<___;
20 .extern         OPENSSL_cpuid_setup
21 .hidden         OPENSSL_cpuid_setup
22 .section        .init
23         call    OPENSSL_cpuid_setup
24
25 .hidden OPENSSL_ia32cap_P
26 .comm   OPENSSL_ia32cap_P,16,4
27
28 .text
29
30 .globl  OPENSSL_atomic_add
31 .type   OPENSSL_atomic_add,\@abi-omnipotent
32 .align  16
33 OPENSSL_atomic_add:
34         movl    ($arg1),%eax
35 .Lspin: leaq    ($arg2,%rax),%r8
36         .byte   0xf0            # lock
37         cmpxchgl        %r8d,($arg1)
38         jne     .Lspin
39         movl    %r8d,%eax
40         .byte   0x48,0x98       # cltq/cdqe
41         ret
42 .size   OPENSSL_atomic_add,.-OPENSSL_atomic_add
43
44 .globl  OPENSSL_rdtsc
45 .type   OPENSSL_rdtsc,\@abi-omnipotent
46 .align  16
47 OPENSSL_rdtsc:
48         rdtsc
49         shl     \$32,%rdx
50         or      %rdx,%rax
51         ret
52 .size   OPENSSL_rdtsc,.-OPENSSL_rdtsc
53
54 .globl  OPENSSL_ia32_cpuid
55 .type   OPENSSL_ia32_cpuid,\@function,1
56 .align  16
57 OPENSSL_ia32_cpuid:
58         mov     %rbx,%r8                # save %rbx
59
60         xor     %eax,%eax
61         mov     %eax,8(%rdi)            # clear 3rd word
62         cpuid
63         mov     %eax,%r11d              # max value for standard query level
64
65         xor     %eax,%eax
66         cmp     \$0x756e6547,%ebx       # "Genu"
67         setne   %al
68         mov     %eax,%r9d
69         cmp     \$0x49656e69,%edx       # "ineI"
70         setne   %al
71         or      %eax,%r9d
72         cmp     \$0x6c65746e,%ecx       # "ntel"
73         setne   %al
74         or      %eax,%r9d               # 0 indicates Intel CPU
75         jz      .Lintel
76
77         cmp     \$0x68747541,%ebx       # "Auth"
78         setne   %al
79         mov     %eax,%r10d
80         cmp     \$0x69746E65,%edx       # "enti"
81         setne   %al
82         or      %eax,%r10d
83         cmp     \$0x444D4163,%ecx       # "cAMD"
84         setne   %al
85         or      %eax,%r10d              # 0 indicates AMD CPU
86         jnz     .Lintel
87
88         # AMD specific
89         mov     \$0x80000000,%eax
90         cpuid
91         cmp     \$0x80000001,%eax
92         jb      .Lintel
93         mov     %eax,%r10d
94         mov     \$0x80000001,%eax
95         cpuid
96         or      %ecx,%r9d
97         and     \$0x00000801,%r9d       # isolate AMD XOP bit, 1<<11
98
99         cmp     \$0x80000008,%r10d
100         jb      .Lintel
101
102         mov     \$0x80000008,%eax
103         cpuid
104         movzb   %cl,%r10                # number of cores - 1
105         inc     %r10                    # number of cores
106
107         mov     \$1,%eax
108         cpuid
109         bt      \$28,%edx               # test hyper-threading bit
110         jnc     .Lgeneric
111         shr     \$16,%ebx               # number of logical processors
112         cmp     %r10b,%bl
113         ja      .Lgeneric
114         and     \$0xefffffff,%edx       # ~(1<<28)
115         jmp     .Lgeneric
116
117 .Lintel:
118         cmp     \$4,%r11d
119         mov     \$-1,%r10d
120         jb      .Lnocacheinfo
121
122         mov     \$4,%eax
123         mov     \$0,%ecx                # query L1D
124         cpuid
125         mov     %eax,%r10d
126         shr     \$14,%r10d
127         and     \$0xfff,%r10d           # number of cores -1 per L1D
128
129         cmp     \$7,%r11d
130         jb      .Lnocacheinfo
131
132         mov     \$7,%eax
133         xor     %ecx,%ecx
134         cpuid
135         mov     %ebx,8(%rdi)
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         bt      \$27,%r9d               # check OSXSAVE bit
167         jnc     .Lclear_avx
168         xor     %ecx,%ecx               # XCR0
169         .byte   0x0f,0x01,0xd0          # xgetbv
170         and     \$6,%eax                # isolate XMM and YMM state support
171         cmp     \$6,%eax
172         je      .Ldone
173 .Lclear_avx:
174         mov     \$0xefffe7ff,%eax       # ~(1<<28|1<<12|1<<11)
175         and     %eax,%r9d               # clear AVX, FMA and AMD XOP bits
176         andl    \$0xffffffdf,8(%rdi)    # cleax AVX2, ~(1<<5)
177 .Ldone:
178         shl     \$32,%r9
179         mov     %r10d,%eax
180         mov     %r8,%rbx                # restore %rbx
181         or      %r9,%rax
182         ret
183 .size   OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
184
185 .globl  OPENSSL_cleanse
186 .type   OPENSSL_cleanse,\@abi-omnipotent
187 .align  16
188 OPENSSL_cleanse:
189         xor     %rax,%rax
190         cmp     \$15,$arg2
191         jae     .Lot
192         cmp     \$0,$arg2
193         je      .Lret
194 .Little:
195         mov     %al,($arg1)
196         sub     \$1,$arg2
197         lea     1($arg1),$arg1
198         jnz     .Little
199 .Lret:
200         ret
201 .align  16
202 .Lot:
203         test    \$7,$arg1
204         jz      .Laligned
205         mov     %al,($arg1)
206         lea     -1($arg2),$arg2
207         lea     1($arg1),$arg1
208         jmp     .Lot
209 .Laligned:
210         mov     %rax,($arg1)
211         lea     -8($arg2),$arg2
212         test    \$-8,$arg2
213         lea     8($arg1),$arg1
214         jnz     .Laligned
215         cmp     \$0,$arg2
216         jne     .Little
217         ret
218 .size   OPENSSL_cleanse,.-OPENSSL_cleanse
219 ___
220
221 print<<___ if (!$win64);
222 .globl  OPENSSL_wipe_cpu
223 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
224 .align  16
225 OPENSSL_wipe_cpu:
226         pxor    %xmm0,%xmm0
227         pxor    %xmm1,%xmm1
228         pxor    %xmm2,%xmm2
229         pxor    %xmm3,%xmm3
230         pxor    %xmm4,%xmm4
231         pxor    %xmm5,%xmm5
232         pxor    %xmm6,%xmm6
233         pxor    %xmm7,%xmm7
234         pxor    %xmm8,%xmm8
235         pxor    %xmm9,%xmm9
236         pxor    %xmm10,%xmm10
237         pxor    %xmm11,%xmm11
238         pxor    %xmm12,%xmm12
239         pxor    %xmm13,%xmm13
240         pxor    %xmm14,%xmm14
241         pxor    %xmm15,%xmm15
242         xorq    %rcx,%rcx
243         xorq    %rdx,%rdx
244         xorq    %rsi,%rsi
245         xorq    %rdi,%rdi
246         xorq    %r8,%r8
247         xorq    %r9,%r9
248         xorq    %r10,%r10
249         xorq    %r11,%r11
250         leaq    8(%rsp),%rax
251         ret
252 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
253 ___
254 print<<___ if ($win64);
255 .globl  OPENSSL_wipe_cpu
256 .type   OPENSSL_wipe_cpu,\@abi-omnipotent
257 .align  16
258 OPENSSL_wipe_cpu:
259         pxor    %xmm0,%xmm0
260         pxor    %xmm1,%xmm1
261         pxor    %xmm2,%xmm2
262         pxor    %xmm3,%xmm3
263         pxor    %xmm4,%xmm4
264         pxor    %xmm5,%xmm5
265         xorq    %rcx,%rcx
266         xorq    %rdx,%rdx
267         xorq    %r8,%r8
268         xorq    %r9,%r9
269         xorq    %r10,%r10
270         xorq    %r11,%r11
271         leaq    8(%rsp),%rax
272         ret
273 .size   OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
274 ___
275 {
276 my $out="%r10";
277 my $cnt="%rcx";
278 my $max="%r11";
279 my $lasttick="%r8d";
280 my $lastdiff="%r9d";
281 my $redzone=win64?8:-8;
282
283 print<<___;
284 .globl  OPENSSL_instrument_bus
285 .type   OPENSSL_instrument_bus,\@abi-omnipotent
286 .align  16
287 OPENSSL_instrument_bus:
288         mov     $arg1,$out      # tribute to Win64
289         mov     $arg2,$cnt
290         mov     $arg2,$max
291
292         rdtsc                   # collect 1st tick
293         mov     %eax,$lasttick  # lasttick = tick
294         mov     \$0,$lastdiff   # lastdiff = 0
295         clflush ($out)
296         .byte   0xf0            # lock
297         add     $lastdiff,($out)
298         jmp     .Loop
299 .align  16
300 .Loop:  rdtsc
301         mov     %eax,%edx
302         sub     $lasttick,%eax
303         mov     %edx,$lasttick
304         mov     %eax,$lastdiff
305         clflush ($out)
306         .byte   0xf0            # lock
307         add     %eax,($out)
308         lea     4($out),$out
309         sub     \$1,$cnt
310         jnz     .Loop
311
312         mov     $max,%rax
313         ret
314 .size   OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
315
316 .globl  OPENSSL_instrument_bus2
317 .type   OPENSSL_instrument_bus2,\@abi-omnipotent
318 .align  16
319 OPENSSL_instrument_bus2:
320         mov     $arg1,$out      # tribute to Win64
321         mov     $arg2,$cnt
322         mov     $arg3,$max
323         mov     $cnt,$redzone(%rsp)
324
325         rdtsc                   # collect 1st tick
326         mov     %eax,$lasttick  # lasttick = tick
327         mov     \$0,$lastdiff   # lastdiff = 0
328
329         clflush ($out)
330         .byte   0xf0            # lock
331         add     $lastdiff,($out)
332
333         rdtsc                   # collect 1st diff
334         mov     %eax,%edx
335         sub     $lasttick,%eax  # diff
336         mov     %edx,$lasttick  # lasttick = tick
337         mov     %eax,$lastdiff  # lastdiff = diff
338 .Loop2:
339         clflush ($out)
340         .byte   0xf0            # lock
341         add     %eax,($out)     # accumulate diff
342
343         sub     \$1,$max
344         jz      .Ldone2
345
346         rdtsc
347         mov     %eax,%edx
348         sub     $lasttick,%eax  # diff
349         mov     %edx,$lasttick  # lasttick = tick
350         cmp     $lastdiff,%eax
351         mov     %eax,$lastdiff  # lastdiff = diff
352         mov     \$0,%edx
353         setne   %dl
354         sub     %rdx,$cnt       # conditional --$cnt
355         lea     ($out,%rdx,4),$out      # conditional ++$out
356         jnz     .Loop2
357
358 .Ldone2:
359         mov     $redzone(%rsp),%rax
360         sub     $cnt,%rax
361         ret
362 .size   OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
363 ___
364 }
365
366 print<<___;
367 .globl  OPENSSL_ia32_rdrand
368 .type   OPENSSL_ia32_rdrand,\@abi-omnipotent
369 .align  16
370 OPENSSL_ia32_rdrand:
371         mov     \$8,%ecx
372 .Loop_rdrand:
373         rdrand  %rax
374         jc      .Lbreak_rdrand
375         loop    .Loop_rdrand
376 .Lbreak_rdrand:
377         cmp     \$0,%rax
378         cmove   %rcx,%rax
379         ret
380 .size   OPENSSL_ia32_rdrand,.-OPENSSL_ia32_rdrand
381 ___
382
383 close STDOUT;   # flush