Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/x86_64cpuid.pl

raw
1 #! /usr/bin/env perl
2 # Copyright 2005-2026 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (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 # $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;
14
15 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
16
17 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
18 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
19 ( $xlate="${dir}perlasm/x86_64-xlate.pl" and -f $xlate) or
20 die "can't locate x86_64-xlate.pl";
21
22 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
23 or die "can't call $xlate: $!";
24 *STDOUT=*OUT;
25
26 ($arg1,$arg2,$arg3,$arg4)=$win64?("%rcx","%rdx","%r8", "%r9") : # Win64 order
27 ("%rdi","%rsi","%rdx","%rcx"); # Unix order
28
29 print<<___;
30 #include crypto/cryptlib.h
31 .extern OPENSSL_cpuid_setup
32 .hidden OPENSSL_cpuid_setup
33 .section .init
34 call OPENSSL_cpuid_setup
35
36 .hidden OPENSSL_ia32cap_P
37 .comm OPENSSL_ia32cap_P,40,4 # <--Should match with internal/cryptlib.h OPENSSL_IA32CAP_P_MAX_INDEXES
38 .text
39
40 .globl OPENSSL_atomic_add
41 .type OPENSSL_atomic_add,\@abi-omnipotent
42 .align 16
43 OPENSSL_atomic_add:
44 .cfi_startproc
45 endbranch
46 movl ($arg1),%eax
47 .Lspin: leaq ($arg2,%rax),%r8
48 .byte 0xf0 # lock
49 cmpxchgl %r8d,($arg1)
50 jne .Lspin
51 movl %r8d,%eax
52 .byte 0x48,0x98 # cltq/cdqe
53 ret
54 .cfi_endproc
55 .size OPENSSL_atomic_add,.-OPENSSL_atomic_add
56
57 .globl OPENSSL_rdtsc
58 .type OPENSSL_rdtsc,\@abi-omnipotent
59 .align 16
60 OPENSSL_rdtsc:
61 .cfi_startproc
62 endbranch
63 rdtsc
64 shl \$32,%rdx
65 or %rdx,%rax
66 ret
67 .cfi_endproc
68 .size OPENSSL_rdtsc,.-OPENSSL_rdtsc
69
70 .globl OPENSSL_ia32_cpuid
71 .type OPENSSL_ia32_cpuid,\@function,1
72 .align 16
73 OPENSSL_ia32_cpuid:
74 .cfi_startproc
75 endbranch
76 mov %rbx,%r8 # save %rbx
77 .cfi_register %rbx,%r8
78
79 xor %eax,%eax
80 mov %rax,8(%rdi) # clear extended feature flags
81 cpuid
82 mov %eax,%r11d # max value for standard query level
83
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
93 or %eax,%r9d # 0 indicates Intel CPU
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
107 # AMD specific
108 mov \$0x80000000,%eax
109 cpuid
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
119 jb .Lintel
120
121 mov \$0x80000008,%eax
122 cpuid
123 movzb %cl,%r10 # number of cores - 1
124 inc %r10 # number of cores
125
126 mov \$1,%eax
127 cpuid
128 bt \$28,%edx # test hyper-threading bit
129 jnc .Lgeneric
130 shr \$16,%ebx # number of logical processors
131 cmp %r10b,%bl
132 ja .Lgeneric
133 and \$0xefffffff,%edx # ~(1<<28)
134 jmp .Lgeneric
135
136 .Lintel:
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:
149 mov \$1,%eax
150 cpuid
151 movd %eax,%xmm0 # put aside processor id
152 and \$0xbfefffff,%edx # force reserved bits to 0
153 cmp \$0,%r9d
154 jne .Lnotintel
155 or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs
156 and \$15,%ah
157 cmp \$15,%ah # examine Family ID
158 jne .LnotP4
159 or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR
160 .LnotP4:
161 cmp \$6,%ah
162 jne .Lnotintel
163 and \$0x0fff0ff0,%eax
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
171 .Lnotintel:
172 bt \$28,%edx # test hyper-threading bit
173 jnc .Lgeneric
174 and \$0xefffffff,%edx # ~(1<<28)
175 cmp \$0,%r10d
176 je .Lgeneric
177
178 or \$0x10000000,%edx # 1<<28
179 shr \$16,%ebx
180 cmp \$1,%bl # see if cache is shared
181 ja .Lgeneric
182 and \$0xefffffff,%edx # ~(1<<28)
183 .Lgeneric:
184 and \$0x00000800,%r9d # isolate AMD XOP flag
185 and \$0xfffff7ff,%ecx
186 or %ecx,%r9d # merge AMD XOP flag
187
188 mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx
189
190 cmp \$7,%r11d
191 jb .Lno_extended_info
192 mov \$7,%eax
193 xor %ecx,%ecx
194 cpuid
195 movd %eax,%xmm1 # put aside leaf 07H Max Sub-leaves
196 bt \$26,%r9d # check XSAVE bit, cleared on Knights
197 jc .Lnotknights
198 and \$0xfff7ffff,%ebx # clear ADCX/ADOX flag
199 .Lnotknights:
200 movd %xmm0,%eax # restore processor id
201 and \$0x0fff0ff0,%eax
202 cmp \$0x00050650,%eax # Skylake-X
203 jne .Lnotskylakex
204 and \$0xfffeffff,%ebx # ~(1<<16)
205 # suppress AVX512F flag on Skylake-X
206
207 .Lnotskylakex: # save extended feature flags
208 mov %ebx,8(%rdi) # save cpuid(EAX=0x7, ECX=0x0).EBX to OPENSSL_ia32cap_P[2]
209 mov %ecx,12(%rdi) # save cpuid(EAX=0x7, ECX=0x0).ECX to OPENSSL_ia32cap_P[3]
210 mov %edx,16(%rdi) # save cpuid(EAX=0x7, ECX=0x0).EDX to OPENSSL_ia32cap_P[4]
211
212 movd %xmm1,%eax # Restore leaf 07H Max Sub-leaves
213 cmp \$0x1,%eax # Do we have cpuid(EAX=0x7, ECX=0x1)?
214 jb .Lno_extended_info
215 mov \$0x7,%eax
216 mov \$0x1,%ecx
217 cpuid # cpuid(EAX=0x7, ECX=0x1)
218 mov %eax,20(%rdi) # save cpuid(EAX=0x7, ECX=0x1).EAX to OPENSSL_ia32cap_P[5]
219 mov %edx,24(%rdi) # save cpuid(EAX=0x7, ECX=0x1).EDX to OPENSSL_ia32cap_P[6]
220 mov %ebx,28(%rdi) # save cpuid(EAX=0x7, ECX=0x1).EBX to OPENSSL_ia32cap_P[7]
221 mov %ecx,32(%rdi) # save cpuid(EAX=0x7, ECX=0x1).ECX to OPENSSL_ia32cap_P[8]
222
223 and \$0x80000,%edx # Mask cpuid(EAX=0x7, ECX=0x1).EDX bit 19 to detect AVX10 support
224 cmp \$0x0,%edx
225 je .Lno_extended_info
226 mov \$0x24,%eax # Have AVX10 Support, query for details
227 mov \$0x0,%ecx
228 cpuid # cpuid(EAX=0x24, ECX=0x0) AVX10 Leaf
229 mov %ebx,36(%rdi) # save cpuid(EAX=0x24, ECX=0x0).EBX to OPENSSL_ia32cap_P[9]
230
231 .Lno_extended_info:
232
233 bt \$27,%r9d # check OSXSAVE bit
234 jnc .Lclear_avx
235 xor %ecx,%ecx # XCR0
236 .byte 0x0f,0x01,0xd0 # xgetbv
237 and \$0xe6,%eax # isolate XMM, YMM and ZMM state support
238 cmp \$0xe6,%eax
239 je .Ldone
240 andl \$0x3fdeffff,8(%rdi) # ~(1<<31|1<<30|1<<21|1<<16)
241 # clear AVX512F+BW+VL+IFMA, all of
242 # them are EVEX-encoded, which requires
243 # ZMM state support even if one uses
244 # only XMM and YMM :-(
245 and \$6,%eax # isolate XMM and YMM state support
246 cmp \$6,%eax
247 je .Ldone
248 .Lclear_avx:
249 andl \$0xff7fffff,20(%rdi) # ~(1<<23)
250 # clear AVXIFMA, which is VEX-encoded
251 # and requires YMM state support
252 mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11)
253 and %eax,%r9d # clear AVX, FMA and AMD XOP bits
254 mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5)
255 and %eax,8(%rdi) # clear AVX2 and AVX512* bits
256 .Ldone:
257 shl \$32,%r9
258 mov %r10d,%eax
259 mov %r8,%rbx # restore %rbx
260 .cfi_restore %rbx
261 or %r9,%rax
262 ret
263 .cfi_endproc
264 .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid
265
266 .globl OPENSSL_cleanse
267 .type OPENSSL_cleanse,\@abi-omnipotent
268 .align 16
269 OPENSSL_cleanse:
270 .cfi_startproc
271 endbranch
272 xor %rax,%rax
273 cmp \$15,$arg2
274 jae .Lot
275 cmp \$0,$arg2
276 je .Lret
277 .Little:
278 mov %al,($arg1)
279 sub \$1,$arg2
280 lea 1($arg1),$arg1
281 jnz .Little
282 .Lret:
283 ret
284 .align 16
285 .Lot:
286 test \$7,$arg1
287 jz .Laligned
288 mov %al,($arg1)
289 lea -1($arg2),$arg2
290 lea 1($arg1),$arg1
291 jmp .Lot
292 .Laligned:
293 mov %rax,($arg1)
294 lea -8($arg2),$arg2
295 test \$-8,$arg2
296 lea 8($arg1),$arg1
297 jnz .Laligned
298 cmp \$0,$arg2
299 jne .Little
300 ret
301 .cfi_endproc
302 .size OPENSSL_cleanse,.-OPENSSL_cleanse
303
304 .globl CRYPTO_memcmp
305 .type CRYPTO_memcmp,\@abi-omnipotent
306 .align 16
307 CRYPTO_memcmp:
308 .cfi_startproc
309 endbranch
310 xor %rax,%rax
311 xor %r10,%r10
312 cmp \$0,$arg3
313 je .Lno_data
314 cmp \$16,$arg3
315 jne .Loop_cmp
316 mov ($arg1),%r10
317 mov 8($arg1),%r11
318 mov \$1,$arg3
319 xor ($arg2),%r10
320 xor 8($arg2),%r11
321 or %r11,%r10
322 cmovnz $arg3,%rax
323 ret
324
325 .align 16
326 .Loop_cmp:
327 mov ($arg1),%r10b
328 lea 1($arg1),$arg1
329 xor ($arg2),%r10b
330 lea 1($arg2),$arg2
331 or %r10b,%al
332 dec $arg3
333 jnz .Loop_cmp
334 neg %rax
335 shr \$63,%rax
336 .Lno_data:
337 ret
338 .cfi_endproc
339 .size CRYPTO_memcmp,.-CRYPTO_memcmp
340 ___
341
342 {
343 my $out="%r10";
344 my $cnt="%rcx";
345 my $max="%r11";
346 my $lasttick="%r8d";
347 my $lastdiff="%r9d";
348 my $redzone=win64?8:-8;
349
350 print<<___;
351 .globl OPENSSL_instrument_bus
352 .type OPENSSL_instrument_bus,\@abi-omnipotent
353 .align 16
354 OPENSSL_instrument_bus:
355 .cfi_startproc
356 endbranch
357 mov $arg1,$out # tribute to Win64
358 mov $arg2,$cnt
359 mov $arg2,$max
360
361 rdtsc # collect 1st tick
362 mov %eax,$lasttick # lasttick = tick
363 mov \$0,$lastdiff # lastdiff = 0
364 clflush ($out)
365 .byte 0xf0 # lock
366 add $lastdiff,($out)
367 jmp .Loop
368 .align 16
369 .Loop: rdtsc
370 mov %eax,%edx
371 sub $lasttick,%eax
372 mov %edx,$lasttick
373 mov %eax,$lastdiff
374 clflush ($out)
375 .byte 0xf0 # lock
376 add %eax,($out)
377 lea 4($out),$out
378 sub \$1,$cnt
379 jnz .Loop
380
381 mov $max,%rax
382 ret
383 .cfi_endproc
384 .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus
385
386 .globl OPENSSL_instrument_bus2
387 .type OPENSSL_instrument_bus2,\@abi-omnipotent
388 .align 16
389 OPENSSL_instrument_bus2:
390 .cfi_startproc
391 endbranch
392 mov $arg1,$out # tribute to Win64
393 mov $arg2,$cnt
394 mov $arg3,$max
395 mov $cnt,$redzone(%rsp)
396
397 rdtsc # collect 1st tick
398 mov %eax,$lasttick # lasttick = tick
399 mov \$0,$lastdiff # lastdiff = 0
400
401 clflush ($out)
402 .byte 0xf0 # lock
403 add $lastdiff,($out)
404
405 rdtsc # collect 1st diff
406 mov %eax,%edx
407 sub $lasttick,%eax # diff
408 mov %edx,$lasttick # lasttick = tick
409 mov %eax,$lastdiff # lastdiff = diff
410 .Loop2:
411 clflush ($out)
412 .byte 0xf0 # lock
413 add %eax,($out) # accumulate diff
414
415 sub \$1,$max
416 jz .Ldone2
417
418 rdtsc
419 mov %eax,%edx
420 sub $lasttick,%eax # diff
421 mov %edx,$lasttick # lasttick = tick
422 cmp $lastdiff,%eax
423 mov %eax,$lastdiff # lastdiff = diff
424 mov \$0,%edx
425 setne %dl
426 sub %rdx,$cnt # conditional --$cnt
427 lea ($out,%rdx,4),$out # conditional ++$out
428 jnz .Loop2
429
430 .Ldone2:
431 mov $redzone(%rsp),%rax
432 sub $cnt,%rax
433 ret
434 .cfi_endproc
435 .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2
436 ___
437 }
438
439 sub gen_random {
440 my $rdop = shift;
441 print<<___;
442 .globl OPENSSL_ia32_${rdop}_bytes
443 .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent
444 .align 16
445 OPENSSL_ia32_${rdop}_bytes:
446 .cfi_startproc
447 endbranch
448 xor %rax, %rax # return value
449 cmp \$0,$arg2
450 je .Ldone_${rdop}_bytes
451
452 mov \$8,%r11
453 .Loop_${rdop}_bytes:
454 ${rdop} %r10
455 jc .Lbreak_${rdop}_bytes
456 dec %r11
457 jnz .Loop_${rdop}_bytes
458 jmp .Ldone_${rdop}_bytes
459
460 .align 16
461 .Lbreak_${rdop}_bytes:
462 cmp \$8,$arg2
463 jb .Ltail_${rdop}_bytes
464 mov %r10,($arg1)
465 lea 8($arg1),$arg1
466 add \$8,%rax
467 sub \$8,$arg2
468 jz .Ldone_${rdop}_bytes
469 mov \$8,%r11
470 jmp .Loop_${rdop}_bytes
471
472 .align 16
473 .Ltail_${rdop}_bytes:
474 mov %r10b,($arg1)
475 lea 1($arg1),$arg1
476 inc %rax
477 shr \$8,%r10
478 dec $arg2
479 jnz .Ltail_${rdop}_bytes
480
481 .Ldone_${rdop}_bytes:
482 xor %r10,%r10 # Clear sensitive data from register
483 ret
484 .cfi_endproc
485 .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes
486 ___
487 }
488 gen_random("rdrand");
489 gen_random("rdseed");
490
491 close STDOUT or die "error closing STDOUT: $!"; # flush