]>
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: | |
b9064221 | 66 | mov %rbx,%r8 # save %rbx |
9babf392 AP |
67 | |
68 | xor %eax,%eax | |
c5cd28bd | 69 | mov %eax,8(%rdi) # clear 3rd word |
9babf392 | 70 | cpuid |
761393bb AP |
71 | mov %eax,%r11d # max value for standard query level |
72 | ||
9babf392 AP |
73 | xor %eax,%eax |
74 | cmp \$0x756e6547,%ebx # "Genu" | |
75 | setne %al | |
76 | mov %eax,%r9d | |
77 | cmp \$0x49656e69,%edx # "ineI" | |
78 | setne %al | |
79 | or %eax,%r9d | |
80 | cmp \$0x6c65746e,%ecx # "ntel" | |
81 | setne %al | |
5cd91b50 | 82 | or %eax,%r9d # 0 indicates Intel CPU |
5cd91b50 AP |
83 | jz .Lintel |
84 | ||
85 | cmp \$0x68747541,%ebx # "Auth" | |
86 | setne %al | |
87 | mov %eax,%r10d | |
88 | cmp \$0x69746E65,%edx # "enti" | |
89 | setne %al | |
90 | or %eax,%r10d | |
91 | cmp \$0x444D4163,%ecx # "cAMD" | |
92 | setne %al | |
93 | or %eax,%r10d # 0 indicates AMD CPU | |
94 | jnz .Lintel | |
95 | ||
761393bb | 96 | # AMD specific |
5cd91b50 AP |
97 | mov \$0x80000000,%eax |
98 | cpuid | |
b9064221 AP |
99 | cmp \$0x80000001,%eax |
100 | jb .Lintel | |
101 | mov %eax,%r10d | |
102 | mov \$0x80000001,%eax | |
103 | cpuid | |
104 | or %ecx,%r9d | |
105 | and \$0x00000801,%r9d # isolate AMD XOP bit, 1<<11 | |
106 | ||
107 | cmp \$0x80000008,%r10d | |
5cd91b50 AP |
108 | jb .Lintel |
109 | ||
110 | mov \$0x80000008,%eax | |
111 | cpuid | |
112 | movzb %cl,%r10 # number of cores - 1 | |
113 | inc %r10 # number of cores | |
9babf392 | 114 | |
761393bb AP |
115 | mov \$1,%eax |
116 | cpuid | |
117 | bt \$28,%edx # test hyper-threading bit | |
b9064221 | 118 | jnc .Lgeneric |
761393bb AP |
119 | shr \$16,%ebx # number of logical processors |
120 | cmp %r10b,%bl | |
b9064221 | 121 | ja .Lgeneric |
761393bb | 122 | and \$0xefffffff,%edx # ~(1<<28) |
b9064221 | 123 | jmp .Lgeneric |
761393bb | 124 | |
5cd91b50 | 125 | .Lintel: |
761393bb AP |
126 | cmp \$4,%r11d |
127 | mov \$-1,%r10d | |
128 | jb .Lnocacheinfo | |
129 | ||
130 | mov \$4,%eax | |
131 | mov \$0,%ecx # query L1D | |
132 | cpuid | |
133 | mov %eax,%r10d | |
134 | shr \$14,%r10d | |
135 | and \$0xfff,%r10d # number of cores -1 per L1D | |
136 | ||
c5cd28bd AP |
137 | cmp \$7,%r11d |
138 | jb .Lnocacheinfo | |
139 | ||
140 | mov \$7,%eax | |
141 | xor %ecx,%ecx | |
142 | cpuid | |
143 | mov %ebx,8(%rdi) | |
144 | ||
761393bb | 145 | .Lnocacheinfo: |
932cc129 | 146 | mov \$1,%eax |
376729e1 | 147 | cpuid |
4bb90087 | 148 | and \$0xbfefffff,%edx # force reserved bits to 0 |
932cc129 | 149 | cmp \$0,%r9d |
9babf392 | 150 | jne .Lnotintel |
4bb90087 | 151 | or \$0x40000000,%edx # set reserved bit#30 on Intel CPUs |
9babf392 AP |
152 | and \$15,%ah |
153 | cmp \$15,%ah # examine Family ID | |
4bb90087 AP |
154 | jne .Lnotintel |
155 | or \$0x00100000,%edx # set reserved bit#20 to engage RC4_CHAR | |
9babf392 | 156 | .Lnotintel: |
3df2eff4 | 157 | bt \$28,%edx # test hyper-threading bit |
b9064221 | 158 | jnc .Lgeneric |
761393bb AP |
159 | and \$0xefffffff,%edx # ~(1<<28) |
160 | cmp \$0,%r10d | |
b9064221 | 161 | je .Lgeneric |
761393bb AP |
162 | |
163 | or \$0x10000000,%edx # 1<<28 | |
9babf392 | 164 | shr \$16,%ebx |
761393bb | 165 | cmp \$1,%bl # see if cache is shared |
b9064221 | 166 | ja .Lgeneric |
932cc129 | 167 | and \$0xefffffff,%edx # ~(1<<28) |
b9064221 AP |
168 | .Lgeneric: |
169 | and \$0x00000800,%r9d # isolate AMD XOP flag | |
170 | and \$0xfffff7ff,%ecx | |
2bc3ad28 | 171 | or %ecx,%r9d # merge AMD XOP flag |
b9064221 | 172 | |
2bc3ad28 AP |
173 | mov %edx,%r10d # %r9d:%r10d is copy of %ecx:%edx |
174 | bt \$27,%r9d # check OSXSAVE bit | |
b9064221 AP |
175 | jnc .Lclear_avx |
176 | xor %ecx,%ecx # XCR0 | |
177 | .byte 0x0f,0x01,0xd0 # xgetbv | |
66bee01c AP |
178 | and \$0xe6,%eax # isolate XMM, YMM and ZMM state support |
179 | cmp \$0xe6,%eax | |
180 | je .Ldone | |
181 | andl \$0xfffeffff,8(%rdi) # clear AVX512F, ~(1<<16) | |
182 | # note that we don't touch other AVX512 | |
183 | # extensions, because they can be used | |
184 | # with YMM (without opmasking though) | |
b9064221 AP |
185 | and \$6,%eax # isolate XMM and YMM state support |
186 | cmp \$6,%eax | |
187 | je .Ldone | |
188 | .Lclear_avx: | |
189 | mov \$0xefffe7ff,%eax # ~(1<<28|1<<12|1<<11) | |
2bc3ad28 | 190 | and %eax,%r9d # clear AVX, FMA and AMD XOP bits |
66bee01c AP |
191 | mov \$0x3fdeffdf,%eax # ~(1<<31|1<<30|1<<21|1<<16|1<<5) |
192 | and %eax,8(%rdi) # cleax AVX2 and AVX512* bits | |
b9064221 | 193 | .Ldone: |
2bc3ad28 AP |
194 | shl \$32,%r9 |
195 | mov %r10d,%eax | |
b9064221 | 196 | mov %r8,%rbx # restore %rbx |
2bc3ad28 | 197 | or %r9,%rax |
376729e1 AP |
198 | ret |
199 | .size OPENSSL_ia32_cpuid,.-OPENSSL_ia32_cpuid | |
b2dba9bf AP |
200 | |
201 | .globl OPENSSL_cleanse | |
aa8f38e4 | 202 | .type OPENSSL_cleanse,\@abi-omnipotent |
b2dba9bf AP |
203 | .align 16 |
204 | OPENSSL_cleanse: | |
205 | xor %rax,%rax | |
aa8f38e4 | 206 | cmp \$15,$arg2 |
b2dba9bf | 207 | jae .Lot |
7676eebf AP |
208 | cmp \$0,$arg2 |
209 | je .Lret | |
b2dba9bf | 210 | .Little: |
aa8f38e4 AP |
211 | mov %al,($arg1) |
212 | sub \$1,$arg2 | |
213 | lea 1($arg1),$arg1 | |
b2dba9bf | 214 | jnz .Little |
1fd79f66 AP |
215 | .Lret: |
216 | ret | |
b2dba9bf AP |
217 | .align 16 |
218 | .Lot: | |
aa8f38e4 | 219 | test \$7,$arg1 |
b2dba9bf | 220 | jz .Laligned |
aa8f38e4 AP |
221 | mov %al,($arg1) |
222 | lea -1($arg2),$arg2 | |
223 | lea 1($arg1),$arg1 | |
b2dba9bf AP |
224 | jmp .Lot |
225 | .Laligned: | |
aa8f38e4 AP |
226 | mov %rax,($arg1) |
227 | lea -8($arg2),$arg2 | |
228 | test \$-8,$arg2 | |
229 | lea 8($arg1),$arg1 | |
b2dba9bf | 230 | jnz .Laligned |
aa8f38e4 | 231 | cmp \$0,$arg2 |
b2dba9bf AP |
232 | jne .Little |
233 | ret | |
234 | .size OPENSSL_cleanse,.-OPENSSL_cleanse | |
e33826f0 AP |
235 | |
236 | .globl CRYPTO_memcmp | |
237 | .type CRYPTO_memcmp,\@abi-omnipotent | |
238 | .align 16 | |
239 | CRYPTO_memcmp: | |
240 | xor %rax,%rax | |
241 | xor %r10,%r10 | |
242 | cmp \$0,$arg3 | |
243 | je .Lno_data | |
244 | .Loop_cmp: | |
245 | mov ($arg1),%r10b | |
246 | lea 1($arg1),$arg1 | |
247 | xor ($arg2),%r10b | |
248 | lea 1($arg2),$arg2 | |
249 | or %r10b,%al | |
250 | dec $arg3 | |
251 | jnz .Loop_cmp | |
252 | neg %rax | |
253 | shr \$63,%rax | |
254 | .Lno_data: | |
255 | ret | |
256 | .size CRYPTO_memcmp,.-CRYPTO_memcmp | |
14e21f86 | 257 | ___ |
aa8f38e4 AP |
258 | |
259 | print<<___ if (!$win64); | |
260 | .globl OPENSSL_wipe_cpu | |
261 | .type OPENSSL_wipe_cpu,\@abi-omnipotent | |
262 | .align 16 | |
263 | OPENSSL_wipe_cpu: | |
264 | pxor %xmm0,%xmm0 | |
265 | pxor %xmm1,%xmm1 | |
266 | pxor %xmm2,%xmm2 | |
267 | pxor %xmm3,%xmm3 | |
268 | pxor %xmm4,%xmm4 | |
269 | pxor %xmm5,%xmm5 | |
270 | pxor %xmm6,%xmm6 | |
271 | pxor %xmm7,%xmm7 | |
272 | pxor %xmm8,%xmm8 | |
273 | pxor %xmm9,%xmm9 | |
274 | pxor %xmm10,%xmm10 | |
275 | pxor %xmm11,%xmm11 | |
276 | pxor %xmm12,%xmm12 | |
277 | pxor %xmm13,%xmm13 | |
278 | pxor %xmm14,%xmm14 | |
279 | pxor %xmm15,%xmm15 | |
280 | xorq %rcx,%rcx | |
281 | xorq %rdx,%rdx | |
282 | xorq %rsi,%rsi | |
283 | xorq %rdi,%rdi | |
284 | xorq %r8,%r8 | |
285 | xorq %r9,%r9 | |
286 | xorq %r10,%r10 | |
287 | xorq %r11,%r11 | |
288 | leaq 8(%rsp),%rax | |
289 | ret | |
290 | .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu | |
291 | ___ | |
292 | print<<___ if ($win64); | |
293 | .globl OPENSSL_wipe_cpu | |
294 | .type OPENSSL_wipe_cpu,\@abi-omnipotent | |
295 | .align 16 | |
296 | OPENSSL_wipe_cpu: | |
297 | pxor %xmm0,%xmm0 | |
298 | pxor %xmm1,%xmm1 | |
299 | pxor %xmm2,%xmm2 | |
300 | pxor %xmm3,%xmm3 | |
301 | pxor %xmm4,%xmm4 | |
302 | pxor %xmm5,%xmm5 | |
303 | xorq %rcx,%rcx | |
304 | xorq %rdx,%rdx | |
305 | xorq %r8,%r8 | |
306 | xorq %r9,%r9 | |
307 | xorq %r10,%r10 | |
308 | xorq %r11,%r11 | |
309 | leaq 8(%rsp),%rax | |
310 | ret | |
311 | .size OPENSSL_wipe_cpu,.-OPENSSL_wipe_cpu | |
312 | ___ | |
5fabb88a AP |
313 | { |
314 | my $out="%r10"; | |
315 | my $cnt="%rcx"; | |
316 | my $max="%r11"; | |
317 | my $lasttick="%r8d"; | |
318 | my $lastdiff="%r9d"; | |
319 | my $redzone=win64?8:-8; | |
320 | ||
321 | print<<___; | |
322 | .globl OPENSSL_instrument_bus | |
323 | .type OPENSSL_instrument_bus,\@abi-omnipotent | |
324 | .align 16 | |
325 | OPENSSL_instrument_bus: | |
326 | mov $arg1,$out # tribute to Win64 | |
327 | mov $arg2,$cnt | |
328 | mov $arg2,$max | |
329 | ||
330 | rdtsc # collect 1st tick | |
331 | mov %eax,$lasttick # lasttick = tick | |
332 | mov \$0,$lastdiff # lastdiff = 0 | |
333 | clflush ($out) | |
b9064221 | 334 | .byte 0xf0 # lock |
5fabb88a AP |
335 | add $lastdiff,($out) |
336 | jmp .Loop | |
337 | .align 16 | |
338 | .Loop: rdtsc | |
339 | mov %eax,%edx | |
340 | sub $lasttick,%eax | |
341 | mov %edx,$lasttick | |
342 | mov %eax,$lastdiff | |
343 | clflush ($out) | |
b9064221 | 344 | .byte 0xf0 # lock |
5fabb88a AP |
345 | add %eax,($out) |
346 | lea 4($out),$out | |
347 | sub \$1,$cnt | |
348 | jnz .Loop | |
349 | ||
350 | mov $max,%rax | |
351 | ret | |
352 | .size OPENSSL_instrument_bus,.-OPENSSL_instrument_bus | |
353 | ||
354 | .globl OPENSSL_instrument_bus2 | |
355 | .type OPENSSL_instrument_bus2,\@abi-omnipotent | |
356 | .align 16 | |
357 | OPENSSL_instrument_bus2: | |
358 | mov $arg1,$out # tribute to Win64 | |
359 | mov $arg2,$cnt | |
360 | mov $arg3,$max | |
361 | mov $cnt,$redzone(%rsp) | |
362 | ||
363 | rdtsc # collect 1st tick | |
364 | mov %eax,$lasttick # lasttick = tick | |
365 | mov \$0,$lastdiff # lastdiff = 0 | |
366 | ||
367 | clflush ($out) | |
b9064221 | 368 | .byte 0xf0 # lock |
5fabb88a AP |
369 | add $lastdiff,($out) |
370 | ||
371 | rdtsc # collect 1st diff | |
372 | mov %eax,%edx | |
373 | sub $lasttick,%eax # diff | |
374 | mov %edx,$lasttick # lasttick = tick | |
375 | mov %eax,$lastdiff # lastdiff = diff | |
376 | .Loop2: | |
377 | clflush ($out) | |
b9064221 | 378 | .byte 0xf0 # lock |
5fabb88a AP |
379 | add %eax,($out) # accumulate diff |
380 | ||
381 | sub \$1,$max | |
382 | jz .Ldone2 | |
383 | ||
384 | rdtsc | |
385 | mov %eax,%edx | |
386 | sub $lasttick,%eax # diff | |
387 | mov %edx,$lasttick # lasttick = tick | |
388 | cmp $lastdiff,%eax | |
389 | mov %eax,$lastdiff # lastdiff = diff | |
390 | mov \$0,%edx | |
391 | setne %dl | |
392 | sub %rdx,$cnt # conditional --$cnt | |
393 | lea ($out,%rdx,4),$out # conditional ++$out | |
394 | jnz .Loop2 | |
395 | ||
396 | .Ldone2: | |
397 | mov $redzone(%rsp),%rax | |
398 | sub $cnt,%rax | |
399 | ret | |
400 | .size OPENSSL_instrument_bus2,.-OPENSSL_instrument_bus2 | |
401 | ___ | |
402 | } | |
aa8f38e4 | 403 | |
9c940446 AP |
404 | sub gen_random { |
405 | my $rdop = shift; | |
301799b8 | 406 | print<<___; |
9c940446 AP |
407 | .globl OPENSSL_ia32_${rdop} |
408 | .type OPENSSL_ia32_${rdop},\@abi-omnipotent | |
301799b8 | 409 | .align 16 |
9c940446 | 410 | OPENSSL_ia32_${rdop}: |
301799b8 | 411 | mov \$8,%ecx |
9c940446 AP |
412 | .Loop_${rdop}: |
413 | ${rdop} %rax | |
414 | jc .Lbreak_${rdop} | |
415 | loop .Loop_${rdop} | |
416 | .Lbreak_${rdop}: | |
301799b8 AP |
417 | cmp \$0,%rax |
418 | cmove %rcx,%rax | |
419 | ret | |
9c940446 | 420 | .size OPENSSL_ia32_${rdop},.-OPENSSL_ia32_${rdop} |
f4d45640 | 421 | |
9c940446 AP |
422 | .globl OPENSSL_ia32_${rdop}_bytes |
423 | .type OPENSSL_ia32_${rdop}_bytes,\@abi-omnipotent | |
f4d45640 | 424 | .align 16 |
9c940446 AP |
425 | OPENSSL_ia32_${rdop}_bytes: |
426 | xor %rax, %rax # return value | |
427 | cmp \$0,$arg2 | |
428 | je .Ldone_${rdop}_bytes | |
429 | ||
430 | mov \$8,%r11 | |
431 | .Loop_${rdop}_bytes: | |
432 | ${rdop} %r10 | |
433 | jc .Lbreak_${rdop}_bytes | |
434 | dec %r11 | |
435 | jnz .Loop_${rdop}_bytes | |
436 | jmp .Ldone_${rdop}_bytes | |
437 | ||
438 | .align 16 | |
439 | .Lbreak_${rdop}_bytes: | |
440 | cmp \$8,$arg2 | |
441 | jb .Ltail_${rdop}_bytes | |
442 | mov %r10,($arg1) | |
443 | lea 8($arg1),$arg1 | |
444 | add \$8,%rax | |
445 | sub \$8,$arg2 | |
446 | jz .Ldone_${rdop}_bytes | |
447 | mov \$8,%r11 | |
448 | jmp .Loop_${rdop}_bytes | |
449 | ||
450 | .align 16 | |
451 | .Ltail_${rdop}_bytes: | |
452 | mov %r10b,($arg1) | |
453 | lea 1($arg1),$arg1 | |
454 | inc %rax | |
455 | shr \$8,%r8 | |
456 | dec $arg2 | |
457 | jnz .Ltail_${rdop}_bytes | |
458 | ||
459 | .Ldone_${rdop}_bytes: | |
f4d45640 | 460 | ret |
9c940446 | 461 | .size OPENSSL_ia32_${rdop}_bytes,.-OPENSSL_ia32_${rdop}_bytes |
301799b8 | 462 | ___ |
9c940446 AP |
463 | } |
464 | gen_random("rdrand"); | |
465 | gen_random("rdseed"); | |
301799b8 | 466 | |
5d863367 | 467 | close STDOUT; # flush |