]>
Commit | Line | Data |
---|---|---|
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; | |
12 | if ($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 | |
19 | die "can't locate x86_64-xlate.pl"; | |
20 | ||
cfe1d992 | 21 | open 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 |
27 | print<<___; |
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 |
41 | OPENSSL_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 | |
55 | OPENSSL_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 |
65 | OPENSSL_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 | |
1aed5e1a | 71 | mov %eax,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 |
4bb90087 | 142 | and \$0xbfefffff,%edx # force reserved bits to 0 |
932cc129 | 143 | cmp \$0,%r9d |
9babf392 | 144 | jne .Lnotintel |
4bb90087 | 145 | or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs |
9babf392 AP |
146 | and \$15,%ah |
147 | cmp \$15,%ah # examine Family ID | |
64d92d74 | 148 | jne .LnotP4 |
4bb90087 | 149 | or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR |
64d92d74 AP |
150 | .LnotP4: |
151 | cmp \$6,%ah | |
152 | jne .Lnotintel | |
d84df594 | 153 | and \$0x0fff0ff0,%eax |
64d92d74 AP |
154 | cmp \$0x00050670,%eax # Knights Landing |
155 | je .Lknights | |
156 | cmp \$0x00080650,%eax # Knights Mill (according to sde) | |
157 | jne .Lnotintel | |
158 | .Lknights: | |
159 | and \$0xfbffffff,%ecx # clear XSAVE flag to mimic Silvermont | |
160 | ||
9babf392 | 161 | .Lnotintel: |
3df2eff4 | 162 | bt \$28,%edx # test hyper-threading bit |
b9064221 | 163 | jnc .Lgeneric |
761393bb AP |
164 | and \$0xefffffff,%edx # ~(1<<28) |
165 | cmp \$0,%r10d | |
b9064221 | 166 | je .Lgeneric |
761393bb AP |
167 | |
168 | or \$0x10000000,%edx # 1<<28 | |
9babf392 | 169 | shr \$16,%ebx |
761393bb | 170 | cmp \$1,%bl # see if cache is shared |
b9064221 | 171 | ja .Lgeneric |
932cc129 | 172 | and \$0xefffffff,%edx # ~(1<<28) |
b9064221 AP |
173 | .Lgeneric: |
174 | and \$0x00000800,%r9d # isolate AMD XOP flag | |
175 | and \$0xfffff7ff,%ecx | |
2bc3ad28 | 176 | or %ecx,%r9d # merge AMD XOP flag |
b9064221 | 177 | |
2bc3ad28 | 178 | mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx |
1aed5e1a AP |
179 | |
180 | cmp \$7,%r11d | |
181 | jb .Lno_extended_info | |
182 | mov \$7,%eax | |
183 | xor %ecx,%ecx | |
184 | cpuid | |
64d92d74 AP |
185 | bt \$26,%r9d # check XSAVE bit, cleared on Knights |
186 | jc .Lnotknights | |
187 | and \$0xfff7ffff,%ebx # clear ADCX/ADOX flag | |
188 | .Lnotknights: | |
1aed5e1a AP |
189 | mov %ebx,8(%rdi) # save extended feature flags |
190 | .Lno_extended_info: | |
191 | ||
2bc3ad28 | 192 | bt \$27,%r9d # check OSXSAVE bit |
b9064221 AP |
193 | jnc .Lclear_avx |
194 | xor %ecx,%ecx # XCR0 | |
195 | .byte 0x0f,0x01,0xd0 # xgetbv | |
66bee01c AP |
196 | and \$0xe6,%eax # isolate XMM, YMM and ZMM state support |
197 | cmp \$0xe6,%eax | |
198 | je .Ldone | |
199 | andl \$0xfffeffff,8(%rdi) # clear AVX512F, ~(1<<16) | |
200 | # note that we don't touch other AVX512 | |
201 | # extensions, because they can be used | |
202 | # with YMM (without opmasking though) | |
b9064221 AP |
203 | and \$6,%eax # isolate XMM and YMM state support |
204 | cmp \$6,%eax | |
205 | je .Ldone | |
206 | .Lclear_avx: | |
207 | mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11) | |
2bc3ad28 | 208 | and %eax,%r9d # clear AVX, FMA and AMD XOP bits |
66bee01c | 209 | mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5) |
d67e7554 | 210 | and %eax,8(%rdi) # clear AVX2 and AVX512* bits |
b9064221 | 211 | .Ldone: |
2bc3ad28 AP |
212 | shl \$32,%r9 |
213 | mov %r10d,%eax | |
b9064221 | 214 | mov %r8,%rbx # restore %rbx |
5e32cfb2 | 215 | .cfi_restore %rbx |
2bc3ad28 | 216 | or %r9,%rax |
376729e1 | 217 | ret |
5e32cfb2 | 218 | .cfi_endproc |
376729e1 | 219 | .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid |
b2dba9bf AP |
220 | |
221 | .globl OPENSSL_cleanse | |
aa8f38e4 | 222 | .type OPENSSL_cleanse,\@abi-omnipotent |
b2dba9bf AP |
223 | .align 16 |
224 | OPENSSL_cleanse: | |
225 | xor %rax,%rax | |
aa8f38e4 | 226 | cmp \$15,$arg2 |
b2dba9bf | 227 | jae .Lot |
7676eebf AP |
228 | cmp \$0,$arg2 |
229 | je .Lret | |
b2dba9bf | 230 | .Little: |
aa8f38e4 AP |
231 | mov %al,($arg1) |
232 | sub \$1,$arg2 | |
233 | lea 1($arg1),$arg1 | |
b2dba9bf | 234 | jnz .Little |
1fd79f66 AP |
235 | .Lret: |
236 | ret | |
b2dba9bf AP |
237 | .align 16 |
238 | .Lot: | |
aa8f38e4 | 239 | test \$7,$arg1 |
b2dba9bf | 240 | jz .Laligned |
aa8f38e4 AP |
241 | mov %al,($arg1) |
242 | lea -1($arg2),$arg2 | |
243 | lea 1($arg1),$arg1 | |
b2dba9bf AP |
244 | jmp .Lot |
245 | .Laligned: | |
aa8f38e4 AP |
246 | mov %rax,($arg1) |
247 | lea -8($arg2),$arg2 | |
248 | test \$-8,$arg2 | |
249 | lea 8($arg1),$arg1 | |
b2dba9bf | 250 | jnz .Laligned |
aa8f38e4 | 251 | cmp \$0,$arg2 |
b2dba9bf AP |
252 | jne .Little |
253 | ret | |
254 | .size OPENSSL_cleanse,.-OPENSSL_cleanse | |
e33826f0 AP |
255 | |
256 | .globl CRYPTO_memcmp | |
257 | .type CRYPTO_memcmp,\@abi-omnipotent | |
258 | .align 16 | |
259 | CRYPTO_memcmp: | |
260 | xor %rax,%rax | |
261 | xor %r10,%r10 | |
262 | cmp \$0,$arg3 | |
263 | je .Lno_data | |
264 | .Loop_cmp: | |
265 | mov ($arg1),%r10b | |
266 | lea 1($arg1),$arg1 | |
267 | xor ($arg2),%r10b | |
268 | lea 1($arg2),$arg2 | |
269 | or %r10b,%al | |
270 | dec $arg3 | |
271 | jnz .Loop_cmp | |
272 | neg %rax | |
273 | shr \$63,%rax | |
274 | .Lno_data: | |
275 | ret | |
276 | .size CRYPTO_memcmp,.-CRYPTO_memcmp | |
14e21f86 | 277 | ___ |
aa8f38e4 AP |
278 | |
279 | print<<___ if (!$win64); | |
280 | .globl OPENSSL_wipe_cpu | |
281 | .type OPENSSL_wipe_cpu,\@abi-omnipotent | |
282 | .align 16 | |
283 | OPENSSL_wipe_cpu: | |
284 | pxor %xmm0,%xmm0 | |
285 | pxor %xmm1,%xmm1 | |
286 | pxor %xmm2,%xmm2 | |
287 | pxor %xmm3,%xmm3 | |
288 | pxor %xmm4,%xmm4 | |
289 | pxor %xmm5,%xmm5 | |
290 | pxor %xmm6,%xmm6 | |
291 | pxor %xmm7,%xmm7 | |
292 | pxor %xmm8,%xmm8 | |
293 | pxor %xmm9,%xmm9 | |
294 | pxor %xmm10,%xmm10 | |
295 | pxor %xmm11,%xmm11 | |
296 | pxor %xmm12,%xmm12 | |
297 | pxor %xmm13,%xmm13 | |
298 | pxor %xmm14,%xmm14 | |
299 | pxor %xmm15,%xmm15 | |
300 | xorq %rcx,%rcx | |
301 | xorq %rdx,%rdx | |
302 | xorq %rsi,%rsi | |
303 | xorq %rdi,%rdi | |
304 | xorq %r8,%r8 | |
305 | xorq %r9,%r9 | |
306 | xorq %r10,%r10 | |
307 | xorq %r11,%r11 | |
308 | leaq 8(%rsp),%rax | |
309 | ret | |
310 | .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu | |
311 | ___ | |
312 | print<<___ if ($win64); | |
313 | .globl OPENSSL_wipe_cpu | |
314 | .type OPENSSL_wipe_cpu,\@abi-omnipotent | |
315 | .align 16 | |
316 | OPENSSL_wipe_cpu: | |
317 | pxor %xmm0,%xmm0 | |
318 | pxor %xmm1,%xmm1 | |
319 | pxor %xmm2,%xmm2 | |
320 | pxor %xmm3,%xmm3 | |
321 | pxor %xmm4,%xmm4 | |
322 | pxor %xmm5,%xmm5 | |
323 | xorq %rcx,%rcx | |
324 | xorq %rdx,%rdx | |
325 | xorq %r8,%r8 | |
326 | xorq %r9,%r9 | |
327 | xorq %r10,%r10 | |
328 | xorq %r11,%r11 | |
329 | leaq 8(%rsp),%rax | |
330 | ret | |
331 | .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu | |
332 | ___ | |
5fabb88a AP |
333 | { |
334 | my $out="%r10"; | |
335 | my $cnt="%rcx"; | |
336 | my $max="%r11"; | |
337 | my $lasttick="%r8d"; | |
338 | my $lastdiff="%r9d"; | |
339 | my $redzone=win64?8:-8; | |
340 | ||
341 | print<<___; | |
342 | .globl OPENSSL_instrument_bus | |
343 | .type OPENSSL_instrument_bus,\@abi-omnipotent | |
344 | .align 16 | |
345 | OPENSSL_instrument_bus: | |
346 | mov $arg1,$out # tribute to Win64 | |
347 | mov $arg2,$cnt | |
348 | mov $arg2,$max | |
349 | ||
350 | rdtsc # collect 1st tick | |
351 | mov %eax,$lasttick # lasttick = tick | |
352 | mov \$0,$lastdiff # lastdiff = 0 | |
353 | clflush ($out) | |
b9064221 | 354 | .byte 0xf0 # lock |
5fabb88a AP |
355 | add $lastdiff,($out) |
356 | jmp .Loop | |
357 | .align 16 | |
358 | .Loop: rdtsc | |
359 | mov %eax,%edx | |
360 | sub $lasttick,%eax | |
361 | mov %edx,$lasttick | |
362 | mov %eax,$lastdiff | |
363 | clflush ($out) | |
b9064221 | 364 | .byte 0xf0 # lock |
5fabb88a AP |
365 | add %eax,($out) |
366 | lea 4($out),$out | |
367 | sub \$1,$cnt | |
368 | jnz .Loop | |
369 | ||
370 | mov $max,%rax | |
371 | ret | |
372 | .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus | |
373 | ||
374 | .globl OPENSSL_instrument_bus2 | |
375 | .type OPENSSL_instrument_bus2,\@abi-omnipotent | |
376 | .align 16 | |
377 | OPENSSL_instrument_bus2: | |
378 | mov $arg1,$out # tribute to Win64 | |
379 | mov $arg2,$cnt | |
380 | mov $arg3,$max | |
381 | mov $cnt,$redzone(%rsp) | |
382 | ||
383 | rdtsc # collect 1st tick | |
384 | mov %eax,$lasttick # lasttick = tick | |
385 | mov \$0,$lastdiff # lastdiff = 0 | |
386 | ||
387 | clflush ($out) | |
b9064221 | 388 | .byte 0xf0 # lock |
5fabb88a AP |
389 | add $lastdiff,($out) |
390 | ||
391 | rdtsc # collect 1st diff | |
392 | mov %eax,%edx | |
393 | sub $lasttick,%eax # diff | |
394 | mov %edx,$lasttick # lasttick = tick | |
395 | mov %eax,$lastdiff # lastdiff = diff | |
396 | .Loop2: | |
397 | clflush ($out) | |
b9064221 | 398 | .byte 0xf0 # lock |
5fabb88a AP |
399 | add %eax,($out) # accumulate diff |
400 | ||
401 | sub \$1,$max | |
402 | jz .Ldone2 | |
403 | ||
404 | rdtsc | |
405 | mov %eax,%edx | |
406 | sub $lasttick,%eax # diff | |
407 | mov %edx,$lasttick # lasttick = tick | |
408 | cmp $lastdiff,%eax | |
409 | mov %eax,$lastdiff # lastdiff = diff | |
410 | mov \$0,%edx | |
411 | setne %dl | |
412 | sub %rdx,$cnt # conditional --$cnt | |
413 | lea ($out,%rdx,4),$out # conditional ++$out | |
414 | jnz .Loop2 | |
415 | ||
416 | .Ldone2: | |
417 | mov $redzone(%rsp),%rax | |
418 | sub $cnt,%rax | |
419 | ret | |
420 | .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2 | |
421 | ___ | |
422 | } | |
aa8f38e4 | 423 | |
9c940446 AP |
424 | sub gen_random { |
425 | my $rdop = shift; | |
301799b8 | 426 | print<<___; |
9c940446 AP |
427 | .globl OPENSSL_ia32_${rdop} |
428 | .type OPENSSL_ia32_${rdop},\@abi-omnipotent | |
301799b8 | 429 | .align 16 |
9c940446 | 430 | OPENSSL_ia32_${rdop}: |
301799b8 | 431 | mov \$8,%ecx |
9c940446 AP |
432 | .Loop_${rdop}: |
433 | ${rdop} %rax | |
434 | jc .Lbreak_${rdop} | |
435 | loop .Loop_${rdop} | |
436 | .Lbreak_${rdop}: | |
301799b8 AP |
437 | cmp \$0,%rax |
438 | cmove %rcx,%rax | |
439 | ret | |
9c940446 | 440 | .size OPENSSL_ia32_${rdop},.-OPENSSL_ia32_${rdop} |
f4d45640 | 441 | |
9c940446 AP |
442 | .globl OPENSSL_ia32_${rdop}_bytes |
443 | .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent | |
f4d45640 | 444 | .align 16 |
9c940446 AP |
445 | OPENSSL_ia32_${rdop}_bytes: |
446 | xor %rax, %rax # return value | |
447 | cmp \$0,$arg2 | |
448 | je .Ldone_${rdop}_bytes | |
449 | ||
450 | mov \$8,%r11 | |
451 | .Loop_${rdop}_bytes: | |
452 | ${rdop} %r10 | |
453 | jc .Lbreak_${rdop}_bytes | |
454 | dec %r11 | |
455 | jnz .Loop_${rdop}_bytes | |
456 | jmp .Ldone_${rdop}_bytes | |
457 | ||
458 | .align 16 | |
459 | .Lbreak_${rdop}_bytes: | |
460 | cmp \$8,$arg2 | |
461 | jb .Ltail_${rdop}_bytes | |
462 | mov %r10,($arg1) | |
463 | lea 8($arg1),$arg1 | |
464 | add \$8,%rax | |
465 | sub \$8,$arg2 | |
466 | jz .Ldone_${rdop}_bytes | |
467 | mov \$8,%r11 | |
468 | jmp .Loop_${rdop}_bytes | |
469 | ||
470 | .align 16 | |
471 | .Ltail_${rdop}_bytes: | |
472 | mov %r10b,($arg1) | |
473 | lea 1($arg1),$arg1 | |
474 | inc %rax | |
475 | shr \$8,%r8 | |
476 | dec $arg2 | |
477 | jnz .Ltail_${rdop}_bytes | |
478 | ||
479 | .Ldone_${rdop}_bytes: | |
f4d45640 | 480 | ret |
9c940446 | 481 | .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes |
301799b8 | 482 | ___ |
9c940446 AP |
483 | } |
484 | gen_random("rdrand"); | |
485 | gen_random("rdseed"); | |
301799b8 | 486 | |
5d863367 | 487 | close STDOUT; # flush |