]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x86_64cpuid.pl
Fix no-cmac
[thirdparty/openssl.git] / crypto / x86_64cpuid.pl
CommitLineData
e0a65194 1#! /usr/bin/env perl
082193ef 2# Copyright 2005-2018 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
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
9a708bf9
AP
274 cmp \$16,$arg3
275 jne .Loop_cmp
276 mov ($arg1),%r10
277 mov 8($arg1),%r11
278 mov \$1,$arg3
279 xor ($arg2),%r10
280 xor 8($arg2),%r11
281 or %r11,%r10
282 cmovnz $arg3,%rax
283 ret
284
285.align 16
e33826f0
AP
286.Loop_cmp:
287 mov ($arg1),%r10b
288 lea 1($arg1),$arg1
289 xor ($arg2),%r10b
290 lea 1($arg2),$arg2
291 or %r10b,%al
292 dec $arg3
293 jnz .Loop_cmp
294 neg %rax
295 shr \$63,%rax
296.Lno_data:
297 ret
298.size CRYPTO_memcmp,.-CRYPTO_memcmp
14e21f86 299___
aa8f38e4
AP
300
301print<<___ if (!$win64);
302.globl OPENSSL_wipe_cpu
303.type OPENSSL_wipe_cpu,\@abi-omnipotent
304.align 16
305OPENSSL_wipe_cpu:
306 pxor %xmm0,%xmm0
307 pxor %xmm1,%xmm1
308 pxor %xmm2,%xmm2
309 pxor %xmm3,%xmm3
310 pxor %xmm4,%xmm4
311 pxor %xmm5,%xmm5
312 pxor %xmm6,%xmm6
313 pxor %xmm7,%xmm7
314 pxor %xmm8,%xmm8
315 pxor %xmm9,%xmm9
316 pxor %xmm10,%xmm10
317 pxor %xmm11,%xmm11
318 pxor %xmm12,%xmm12
319 pxor %xmm13,%xmm13
320 pxor %xmm14,%xmm14
321 pxor %xmm15,%xmm15
322 xorq %rcx,%rcx
323 xorq %rdx,%rdx
324 xorq %rsi,%rsi
325 xorq %rdi,%rdi
326 xorq %r8,%r8
327 xorq %r9,%r9
328 xorq %r10,%r10
329 xorq %r11,%r11
330 leaq 8(%rsp),%rax
331 ret
332.size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
333___
334print<<___ if ($win64);
335.globl OPENSSL_wipe_cpu
336.type OPENSSL_wipe_cpu,\@abi-omnipotent
337.align 16
338OPENSSL_wipe_cpu:
339 pxor %xmm0,%xmm0
340 pxor %xmm1,%xmm1
341 pxor %xmm2,%xmm2
342 pxor %xmm3,%xmm3
343 pxor %xmm4,%xmm4
344 pxor %xmm5,%xmm5
345 xorq %rcx,%rcx
346 xorq %rdx,%rdx
347 xorq %r8,%r8
348 xorq %r9,%r9
349 xorq %r10,%r10
350 xorq %r11,%r11
351 leaq 8(%rsp),%rax
352 ret
353.size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu
354___
5fabb88a
AP
355{
356my $out="%r10";
357my $cnt="%rcx";
358my $max="%r11";
359my $lasttick="%r8d";
360my $lastdiff="%r9d";
361my $redzone=win64?8:-8;
362
363print<<___;
364.globl OPENSSL_instrument_bus
365.type OPENSSL_instrument_bus,\@abi-omnipotent
366.align 16
367OPENSSL_instrument_bus:
368 mov $arg1,$out # tribute to Win64
369 mov $arg2,$cnt
370 mov $arg2,$max
371
372 rdtsc # collect 1st tick
373 mov %eax,$lasttick # lasttick = tick
374 mov \$0,$lastdiff # lastdiff = 0
375 clflush ($out)
b9064221 376 .byte 0xf0 # lock
5fabb88a
AP
377 add $lastdiff,($out)
378 jmp .Loop
379.align 16
380.Loop: rdtsc
381 mov %eax,%edx
382 sub $lasttick,%eax
383 mov %edx,$lasttick
384 mov %eax,$lastdiff
385 clflush ($out)
b9064221 386 .byte 0xf0 # lock
5fabb88a
AP
387 add %eax,($out)
388 lea 4($out),$out
389 sub \$1,$cnt
390 jnz .Loop
391
392 mov $max,%rax
393 ret
394.size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
395
396.globl OPENSSL_instrument_bus2
397.type OPENSSL_instrument_bus2,\@abi-omnipotent
398.align 16
399OPENSSL_instrument_bus2:
400 mov $arg1,$out # tribute to Win64
401 mov $arg2,$cnt
402 mov $arg3,$max
403 mov $cnt,$redzone(%rsp)
404
405 rdtsc # collect 1st tick
406 mov %eax,$lasttick # lasttick = tick
407 mov \$0,$lastdiff # lastdiff = 0
408
409 clflush ($out)
b9064221 410 .byte 0xf0 # lock
5fabb88a
AP
411 add $lastdiff,($out)
412
413 rdtsc # collect 1st diff
414 mov %eax,%edx
415 sub $lasttick,%eax # diff
416 mov %edx,$lasttick # lasttick = tick
417 mov %eax,$lastdiff # lastdiff = diff
418.Loop2:
419 clflush ($out)
b9064221 420 .byte 0xf0 # lock
5fabb88a
AP
421 add %eax,($out) # accumulate diff
422
423 sub \$1,$max
424 jz .Ldone2
425
426 rdtsc
427 mov %eax,%edx
428 sub $lasttick,%eax # diff
429 mov %edx,$lasttick # lasttick = tick
430 cmp $lastdiff,%eax
431 mov %eax,$lastdiff # lastdiff = diff
432 mov \$0,%edx
433 setne %dl
434 sub %rdx,$cnt # conditional --$cnt
435 lea ($out,%rdx,4),$out # conditional ++$out
436 jnz .Loop2
437
438.Ldone2:
439 mov $redzone(%rsp),%rax
440 sub $cnt,%rax
441 ret
442.size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
443___
444}
aa8f38e4 445
9c940446
AP
446sub gen_random {
447my $rdop = shift;
301799b8 448print<<___;
9c940446
AP
449.globl OPENSSL_ia32_${rdop}_bytes
450.type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
f4d45640 451.align 16
9c940446
AP
452OPENSSL_ia32_${rdop}_bytes:
453 xor %rax, %rax # return value
454 cmp \$0,$arg2
455 je .Ldone_${rdop}_bytes
456
457 mov \$8,%r11
458.Loop_${rdop}_bytes:
459 ${rdop} %r10
460 jc .Lbreak_${rdop}_bytes
461 dec %r11
462 jnz .Loop_${rdop}_bytes
463 jmp .Ldone_${rdop}_bytes
464
465.align 16
466.Lbreak_${rdop}_bytes:
467 cmp \$8,$arg2
468 jb .Ltail_${rdop}_bytes
469 mov %r10,($arg1)
470 lea 8($arg1),$arg1
471 add \$8,%rax
472 sub \$8,$arg2
473 jz .Ldone_${rdop}_bytes
474 mov \$8,%r11
475 jmp .Loop_${rdop}_bytes
476
477.align 16
478.Ltail_${rdop}_bytes:
479 mov %r10b,($arg1)
480 lea 1($arg1),$arg1
481 inc %rax
082193ef 482 shr \$8,%r10
9c940446
AP
483 dec $arg2
484 jnz .Ltail_${rdop}_bytes
485
486.Ldone_${rdop}_bytes:
082193ef 487 xor %r10,%r10 # Clear sensitive data from register
f4d45640 488 ret
9c940446 489.size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
301799b8 490___
9c940446
AP
491}
492gen_random("rdrand");
493gen_random("rdseed");
301799b8 494
5d863367 495close STDOUT; # flush