]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x86_64cpuid.pl
Allocate the three shared DRBGs on the secure heap
[thirdparty/openssl.git] / crypto / x86_64cpuid.pl
CommitLineData
e0a65194
RS
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
14e21f86 9
aa8f38e4
AP
10$flavour = shift;
11$output = shift;
12if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
14e21f86 13
aa8f38e4 14$win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
376729e1 15
aa8f38e4 16$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
301799b8
AP
17( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
18( $xlate="${dir}perlasm/x86_64-xlate.pl" and -f $xlate) or
19die "can't locate x86_64-xlate.pl";
20
cfe1d992 21open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
46bf83f0 22*STDOUT=*OUT;
376729e1 23
5fabb88a
AP
24($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
25 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
26
aa8f38e4
AP
27print<<___;
28.extern OPENSSL_cpuid_setup
ddc20d4d 29.hidden OPENSSL_cpuid_setup
aa8f38e4
AP
30.section .init
31 call OPENSSL_cpuid_setup
932cc129 32
ddc20d4d 33.hidden OPENSSL_ia32cap_P
c5cd28bd 34.comm OPENSSL_ia32cap_P,16,4
ddc20d4d 35
14e21f86 36.text
376729e1
AP
37
38.globl OPENSSL_atomic_add
aa8f38e4 39.type OPENSSL_atomic_add,\@abi-omnipotent
376729e1
AP
40.align 16
41OPENSSL_atomic_add:
aa8f38e4
AP
42 movl ($arg1),%eax
43.Lspin: leaq ($arg2,%rax),%r8
44 .byte 0xf0 # lock
45 cmpxchgl %r8d,($arg1)
376729e1 46 jne .Lspin
e442c362 47 movl %r8d,%eax
aa8f38e4 48 .byte 0x48,0x98 # cltq/cdqe
376729e1
AP
49 ret
50.size OPENSSL_atomic_add,.-OPENSSL_atomic_add
51
932cc129
AP
52.globl OPENSSL_rdtsc
53.type OPENSSL_rdtsc,\@abi-omnipotent
54.align 16
55OPENSSL_rdtsc:
56 rdtsc
57 shl \$32,%rdx
58 or %rdx,%rax
59 ret
60.size OPENSSL_rdtsc,.-OPENSSL_rdtsc
61
376729e1 62.globl OPENSSL_ia32_cpuid
c5cd28bd 63.type OPENSSL_ia32_cpuid,\@function,1
376729e1
AP
64.align 16
65OPENSSL_ia32_cpuid:
5e32cfb2 66.cfi_startproc
b9064221 67 mov %rbx,%r8 # save %rbx
5e32cfb2 68.cfi_register %rbx,%r8
9babf392
AP
69
70 xor %eax,%eax
d6ee8f3d 71 mov %rax,8(%rdi) # clear extended feature flags
9babf392 72 cpuid
761393bb
AP
73 mov %eax,%r11d # max value for standard query level
74
9babf392
AP
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
5cd91b50 84 or %eax,%r9d # 0 indicates Intel CPU
5cd91b50
AP
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
761393bb 98 # AMD specific
5cd91b50
AP
99 mov \$0x80000000,%eax
100 cpuid
b9064221
AP
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
5cd91b50
AP
110 jb .Lintel
111
112 mov \$0x80000008,%eax
113 cpuid
114 movzb %cl,%r10 # number of cores - 1
115 inc %r10 # number of cores
9babf392 116
761393bb
AP
117 mov \$1,%eax
118 cpuid
119 bt \$28,%edx # test hyper-threading bit
b9064221 120 jnc .Lgeneric
761393bb
AP
121 shr \$16,%ebx # number of logical processors
122 cmp %r10b,%bl
b9064221 123 ja .Lgeneric
761393bb 124 and \$0xefffffff,%edx # ~(1<<28)
b9064221 125 jmp .Lgeneric
761393bb 126
5cd91b50 127.Lintel:
761393bb
AP
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:
932cc129 140 mov \$1,%eax
376729e1 141 cpuid
79337628 142 movd %eax,%xmm0 # put aside processor id
4bb90087 143 and \$0xbfefffff,%edx # force reserved bits to 0
932cc129 144 cmp \$0,%r9d
9babf392 145 jne .Lnotintel
4bb90087 146 or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
9babf392
AP
147 and \$15,%ah
148 cmp \$15,%ah # examine Family ID
64d92d74 149 jne .LnotP4
4bb90087 150 or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
64d92d74
AP
151.LnotP4:
152 cmp \$6,%ah
153 jne .Lnotintel
d84df594 154 and \$0x0fff0ff0,%eax
64d92d74
AP
155 cmp \$0x00050670,%eax # Knights Landing
156 je .Lknights
157 cmp \$0x00080650,%eax # Knights Mill (according to sde)
158 jne .Lnotintel
159.Lknights:
160 and \$0xfbffffff,%ecx # clear XSAVE flag to mimic Silvermont
161
9babf392 162.Lnotintel:
3df2eff4 163 bt \$28,%edx # test hyper-threading bit
b9064221 164 jnc .Lgeneric
761393bb
AP
165 and \$0xefffffff,%edx # ~(1<<28)
166 cmp \$0,%r10d
b9064221 167 je .Lgeneric
761393bb
AP
168
169 or \$0x10000000,%edx # 1<<28
9babf392 170 shr \$16,%ebx
761393bb 171 cmp \$1,%bl # see if cache is shared
b9064221 172 ja .Lgeneric
932cc129 173 and \$0xefffffff,%edx # ~(1<<28)
b9064221
AP
174.Lgeneric:
175 and \$0x00000800,%r9d # isolate AMD XOP flag
176 and \$0xfffff7ff,%ecx
2bc3ad28 177 or %ecx,%r9d # merge AMD XOP flag
b9064221 178
2bc3ad28 179 mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
1aed5e1a
AP
180
181 cmp \$7,%r11d
182 jb .Lno_extended_info
183 mov \$7,%eax
184 xor %ecx,%ecx
185 cpuid
64d92d74
AP
186 bt \$26,%r9d # check XSAVE bit, cleared on Knights
187 jc .Lnotknights
188 and \$0xfff7ffff,%ebx # clear ADCX/ADOX flag
189.Lnotknights:
79337628
AP
190 movd %xmm0,%eax # restore processor id
191 and \$0x0fff0ff0,%eax
192 cmp \$0x00050650,%eax # Skylake-X
193 jne .Lnotskylakex
194 and \$0xfffeffff,%ebx # ~(1<<16)
195 # suppress AVX512F flag on Skylake-X
196.Lnotskylakex:
1aed5e1a 197 mov %ebx,8(%rdi) # save extended feature flags
d6ee8f3d 198 mov %ecx,12(%rdi)
1aed5e1a
AP
199.Lno_extended_info:
200
2bc3ad28 201 bt \$27,%r9d # check OSXSAVE bit
b9064221
AP
202 jnc .Lclear_avx
203 xor %ecx,%ecx # XCR0
204 .byte 0x0f,0x01,0xd0 # xgetbv
66bee01c
AP
205 and \$0xe6,%eax # isolate XMM, YMM and ZMM state support
206 cmp \$0xe6,%eax
207 je .Ldone
88ac224c
AP
208 andl \$0x3fdeffff,8(%rdi) # ~(1<<31|1<<30|1<<21|1<<16)
209 # clear AVX512F+BW+VL+FIMA, all of
210 # them are EVEX-encoded, which requires
211 # ZMM state support even if one uses
212 # only XMM and YMM :-(
b9064221
AP
213 and \$6,%eax # isolate XMM and YMM state support
214 cmp \$6,%eax
215 je .Ldone
216.Lclear_avx:
217 mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
2bc3ad28 218 and %eax,%r9d # clear AVX, FMA and AMD XOP bits
66bee01c 219 mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
d67e7554 220 and %eax,8(%rdi) # clear AVX2 and AVX512* bits
b9064221 221.Ldone:
2bc3ad28
AP
222 shl \$32,%r9
223 mov %r10d,%eax
b9064221 224 mov %r8,%rbx # restore %rbx
5e32cfb2 225.cfi_restore %rbx
2bc3ad28 226 or %r9,%rax
376729e1 227 ret
5e32cfb2 228.cfi_endproc
376729e1 229.size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
b2dba9bf
AP
230
231.globl OPENSSL_cleanse
aa8f38e4 232.type OPENSSL_cleanse,\@abi-omnipotent
b2dba9bf
AP
233.align 16
234OPENSSL_cleanse:
235 xor %rax,%rax
aa8f38e4 236 cmp \$15,$arg2
b2dba9bf 237 jae .Lot
7676eebf
AP
238 cmp \$0,$arg2
239 je .Lret
b2dba9bf 240.Little:
aa8f38e4
AP
241 mov %al,($arg1)
242 sub \$1,$arg2
243 lea 1($arg1),$arg1
b2dba9bf 244 jnz .Little
1fd79f66
AP
245.Lret:
246 ret
b2dba9bf
AP
247.align 16
248.Lot:
aa8f38e4 249 test \$7,$arg1
b2dba9bf 250 jz .Laligned
aa8f38e4
AP
251 mov %al,($arg1)
252 lea -1($arg2),$arg2
253 lea 1($arg1),$arg1
b2dba9bf
AP
254 jmp .Lot
255.Laligned:
aa8f38e4
AP
256 mov %rax,($arg1)
257 lea -8($arg2),$arg2
258 test \$-8,$arg2
259 lea 8($arg1),$arg1
b2dba9bf 260 jnz .Laligned
aa8f38e4 261 cmp \$0,$arg2
b2dba9bf
AP
262 jne .Little
263 ret
264.size OPENSSL_cleanse,.-OPENSSL_cleanse
e33826f0
AP
265
266.globl CRYPTO_memcmp
267.type CRYPTO_memcmp,\@abi-omnipotent
268.align 16
269CRYPTO_memcmp:
270 xor %rax,%rax
271 xor %r10,%r10
272 cmp \$0,$arg3
273 je .Lno_data
274.Loop_cmp:
275 mov ($arg1),%r10b
276 lea 1($arg1),$arg1
277 xor ($arg2),%r10b
278 lea 1($arg2),$arg2
279 or %r10b,%al
280 dec $arg3
281 jnz .Loop_cmp
282 neg %rax
283 shr \$63,%rax
284.Lno_data:
285 ret
286.size CRYPTO_memcmp,.-CRYPTO_memcmp
14e21f86 287___
aa8f38e4
AP
288
289print<<___ if (!$win64);
290.globl OPENSSL_wipe_cpu
291.type OPENSSL_wipe_cpu,\@abi-omnipotent
292.align 16
293OPENSSL_wipe_cpu:
294 pxor %xmm0,%xmm0
295 pxor %xmm1,%xmm1
296 pxor %xmm2,%xmm2
297 pxor %xmm3,%xmm3
298 pxor %xmm4,%xmm4
299 pxor %xmm5,%xmm5
300 pxor %xmm6,%xmm6
301 pxor %xmm7,%xmm7
302 pxor %xmm8,%xmm8
303 pxor %xmm9,%xmm9
304 pxor %xmm10,%xmm10
305 pxor %xmm11,%xmm11
306 pxor %xmm12,%xmm12
307 pxor %xmm13,%xmm13
308 pxor %xmm14,%xmm14
309 pxor %xmm15,%xmm15
310 xorq %rcx,%rcx
311 xorq %rdx,%rdx
312 xorq %rsi,%rsi
313 xorq %rdi,%rdi
314 xorq %r8,%r8
315 xorq %r9,%r9
316 xorq %r10,%r10
317 xorq %r11,%r11
318 leaq 8(%rsp),%rax
319 ret
320.size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
321___
322print<<___ if ($win64);
323.globl OPENSSL_wipe_cpu
324.type OPENSSL_wipe_cpu,\@abi-omnipotent
325.align 16
326OPENSSL_wipe_cpu:
327 pxor %xmm0,%xmm0
328 pxor %xmm1,%xmm1
329 pxor %xmm2,%xmm2
330 pxor %xmm3,%xmm3
331 pxor %xmm4,%xmm4
332 pxor %xmm5,%xmm5
333 xorq %rcx,%rcx
334 xorq %rdx,%rdx
335 xorq %r8,%r8
336 xorq %r9,%r9
337 xorq %r10,%r10
338 xorq %r11,%r11
339 leaq 8(%rsp),%rax
340 ret
341.size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
342___
5fabb88a
AP
343{
344my $out="%r10";
345my $cnt="%rcx";
346my $max="%r11";
347my $lasttick="%r8d";
348my $lastdiff="%r9d";
349my $redzone=win64?8:-8;
350
351print<<___;
352.globl OPENSSL_instrument_bus
353.type OPENSSL_instrument_bus,\@abi-omnipotent
354.align 16
355OPENSSL_instrument_bus:
356 mov $arg1,$out # tribute to Win64
357 mov $arg2,$cnt
358 mov $arg2,$max
359
360 rdtsc # collect 1st tick
361 mov %eax,$lasttick # lasttick = tick
362 mov \$0,$lastdiff # lastdiff = 0
363 clflush ($out)
b9064221 364 .byte 0xf0 # lock
5fabb88a
AP
365 add $lastdiff,($out)
366 jmp .Loop
367.align 16
368.Loop: rdtsc
369 mov %eax,%edx
370 sub $lasttick,%eax
371 mov %edx,$lasttick
372 mov %eax,$lastdiff
373 clflush ($out)
b9064221 374 .byte 0xf0 # lock
5fabb88a
AP
375 add %eax,($out)
376 lea 4($out),$out
377 sub \$1,$cnt
378 jnz .Loop
379
380 mov $max,%rax
381 ret
382.size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
383
384.globl OPENSSL_instrument_bus2
385.type OPENSSL_instrument_bus2,\@abi-omnipotent
386.align 16
387OPENSSL_instrument_bus2:
388 mov $arg1,$out # tribute to Win64
389 mov $arg2,$cnt
390 mov $arg3,$max
391 mov $cnt,$redzone(%rsp)
392
393 rdtsc # collect 1st tick
394 mov %eax,$lasttick # lasttick = tick
395 mov \$0,$lastdiff # lastdiff = 0
396
397 clflush ($out)
b9064221 398 .byte 0xf0 # lock
5fabb88a
AP
399 add $lastdiff,($out)
400
401 rdtsc # collect 1st diff
402 mov %eax,%edx
403 sub $lasttick,%eax # diff
404 mov %edx,$lasttick # lasttick = tick
405 mov %eax,$lastdiff # lastdiff = diff
406.Loop2:
407 clflush ($out)
b9064221 408 .byte 0xf0 # lock
5fabb88a
AP
409 add %eax,($out) # accumulate diff
410
411 sub \$1,$max
412 jz .Ldone2
413
414 rdtsc
415 mov %eax,%edx
416 sub $lasttick,%eax # diff
417 mov %edx,$lasttick # lasttick = tick
418 cmp $lastdiff,%eax
419 mov %eax,$lastdiff # lastdiff = diff
420 mov \$0,%edx
421 setne %dl
422 sub %rdx,$cnt # conditional --$cnt
423 lea ($out,%rdx,4),$out # conditional ++$out
424 jnz .Loop2
425
426.Ldone2:
427 mov $redzone(%rsp),%rax
428 sub $cnt,%rax
429 ret
430.size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
431___
432}
aa8f38e4 433
9c940446
AP
434sub gen_random {
435my $rdop = shift;
301799b8 436print<<___;
9c940446
AP
437.globl OPENSSL_ia32_${rdop}
438.type OPENSSL_ia32_${rdop},\@abi-omnipotent
301799b8 439.align 16
9c940446 440OPENSSL_ia32_${rdop}:
301799b8 441 mov \$8,%ecx
9c940446
AP
442.Loop_${rdop}:
443 ${rdop} %rax
444 jc .Lbreak_${rdop}
445 loop .Loop_${rdop}
446.Lbreak_${rdop}:
301799b8
AP
447 cmp \$0,%rax
448 cmove %rcx,%rax
449 ret
9c940446 450.size OPENSSL_ia32_${rdop},.-OPENSSL_ia32_${rdop}
f4d45640 451
9c940446
AP
452.globl OPENSSL_ia32_${rdop}_bytes
453.type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
f4d45640 454.align 16
9c940446
AP
455OPENSSL_ia32_${rdop}_bytes:
456 xor %rax, %rax # return value
457 cmp \$0,$arg2
458 je .Ldone_${rdop}_bytes
459
460 mov \$8,%r11
461.Loop_${rdop}_bytes:
462 ${rdop} %r10
463 jc .Lbreak_${rdop}_bytes
464 dec %r11
465 jnz .Loop_${rdop}_bytes
466 jmp .Ldone_${rdop}_bytes
467
468.align 16
469.Lbreak_${rdop}_bytes:
470 cmp \$8,$arg2
471 jb .Ltail_${rdop}_bytes
472 mov %r10,($arg1)
473 lea 8($arg1),$arg1
474 add \$8,%rax
475 sub \$8,$arg2
476 jz .Ldone_${rdop}_bytes
477 mov \$8,%r11
478 jmp .Loop_${rdop}_bytes
479
480.align 16
481.Ltail_${rdop}_bytes:
482 mov %r10b,($arg1)
483 lea 1($arg1),$arg1
484 inc %rax
485 shr \$8,%r8
486 dec $arg2
487 jnz .Ltail_${rdop}_bytes
488
489.Ldone_${rdop}_bytes:
f4d45640 490 ret
9c940446 491.size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
301799b8 492___
9c940446
AP
493}
494gen_random("rdrand");
495gen_random("rdseed");
301799b8 496
5d863367 497close STDOUT; # flush