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