]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x86cpuid.pl
Copyright consolidation: perl files
[thirdparty/openssl.git] / crypto / x86cpuid.pl
1 #! /usr/bin/env perl
2 # Copyright 2004-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
9 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
10 push(@INC, "${dir}perlasm", "perlasm");
11 require "x86asm.pl";
12
13 $output = pop;
14 open OUT,">$output";
15 *STDOUT=*OUT;
16
17 &asm_init($ARGV[0],"x86cpuid");
18
19 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
20
21 &function_begin("OPENSSL_ia32_cpuid");
22 &xor ("edx","edx");
23 &pushf ();
24 &pop ("eax");
25 &mov ("ecx","eax");
26 &xor ("eax",1<<21);
27 &push ("eax");
28 &popf ();
29 &pushf ();
30 &pop ("eax");
31 &xor ("ecx","eax");
32 &xor ("eax","eax");
33 &bt ("ecx",21);
34 &jnc (&label("nocpuid"));
35 &mov ("esi",&wparam(0));
36 &mov (&DWP(8,"esi"),"eax"); # clear 3rd word
37 &cpuid ();
38 &mov ("edi","eax"); # max value for standard query level
39
40 &xor ("eax","eax");
41 &cmp ("ebx",0x756e6547); # "Genu"
42 &setne (&LB("eax"));
43 &mov ("ebp","eax");
44 &cmp ("edx",0x49656e69); # "ineI"
45 &setne (&LB("eax"));
46 &or ("ebp","eax");
47 &cmp ("ecx",0x6c65746e); # "ntel"
48 &setne (&LB("eax"));
49 &or ("ebp","eax"); # 0 indicates Intel CPU
50 &jz (&label("intel"));
51
52 &cmp ("ebx",0x68747541); # "Auth"
53 &setne (&LB("eax"));
54 &mov ("esi","eax");
55 &cmp ("edx",0x69746E65); # "enti"
56 &setne (&LB("eax"));
57 &or ("esi","eax");
58 &cmp ("ecx",0x444D4163); # "cAMD"
59 &setne (&LB("eax"));
60 &or ("esi","eax"); # 0 indicates AMD CPU
61 &jnz (&label("intel"));
62
63 # AMD specific
64 &mov ("eax",0x80000000);
65 &cpuid ();
66 &cmp ("eax",0x80000001);
67 &jb (&label("intel"));
68 &mov ("esi","eax");
69 &mov ("eax",0x80000001);
70 &cpuid ();
71 &or ("ebp","ecx");
72 &and ("ebp",1<<11|1); # isolate XOP bit
73 &cmp ("esi",0x80000008);
74 &jb (&label("intel"));
75
76 &mov ("eax",0x80000008);
77 &cpuid ();
78 &movz ("esi",&LB("ecx")); # number of cores - 1
79 &inc ("esi"); # number of cores
80
81 &mov ("eax",1);
82 &xor ("ecx","ecx");
83 &cpuid ();
84 &bt ("edx",28);
85 &jnc (&label("generic"));
86 &shr ("ebx",16);
87 &and ("ebx",0xff);
88 &cmp ("ebx","esi");
89 &ja (&label("generic"));
90 &and ("edx",0xefffffff); # clear hyper-threading bit
91 &jmp (&label("generic"));
92
93 &set_label("intel");
94 &cmp ("edi",7);
95 &jb (&label("cacheinfo"));
96
97 &mov ("esi",&wparam(0));
98 &mov ("eax",7);
99 &xor ("ecx","ecx");
100 &cpuid ();
101 &mov (&DWP(8,"esi"),"ebx");
102
103 &set_label("cacheinfo");
104 &cmp ("edi",4);
105 &mov ("edi",-1);
106 &jb (&label("nocacheinfo"));
107
108 &mov ("eax",4);
109 &mov ("ecx",0); # query L1D
110 &cpuid ();
111 &mov ("edi","eax");
112 &shr ("edi",14);
113 &and ("edi",0xfff); # number of cores -1 per L1D
114
115 &set_label("nocacheinfo");
116 &mov ("eax",1);
117 &xor ("ecx","ecx");
118 &cpuid ();
119 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
120 &cmp ("ebp",0);
121 &jne (&label("notintel"));
122 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
123 &and (&HB("eax"),15); # familiy ID
124 &cmp (&HB("eax"),15); # P4?
125 &jne (&label("notintel"));
126 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
127 &set_label("notintel");
128 &bt ("edx",28); # test hyper-threading bit
129 &jnc (&label("generic"));
130 &and ("edx",0xefffffff);
131 &cmp ("edi",0);
132 &je (&label("generic"));
133
134 &or ("edx",0x10000000);
135 &shr ("ebx",16);
136 &cmp (&LB("ebx"),1);
137 &ja (&label("generic"));
138 &and ("edx",0xefffffff); # clear hyper-threading bit if not
139
140 &set_label("generic");
141 &and ("ebp",1<<11); # isolate AMD XOP flag
142 &and ("ecx",0xfffff7ff); # force 11th bit to 0
143 &mov ("esi","edx");
144 &or ("ebp","ecx"); # merge AMD XOP flag
145
146 &bt ("ecx",27); # check OSXSAVE bit
147 &jnc (&label("clear_avx"));
148 &xor ("ecx","ecx");
149 &data_byte(0x0f,0x01,0xd0); # xgetbv
150 &and ("eax",6);
151 &cmp ("eax",6);
152 &je (&label("done"));
153 &cmp ("eax",2);
154 &je (&label("clear_avx"));
155 &set_label("clear_xmm");
156 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
157 &and ("esi",0xfeffffff); # clear FXSR
158 &set_label("clear_avx");
159 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
160 &mov ("edi",&wparam(0));
161 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
162 &set_label("done");
163 &mov ("eax","esi");
164 &mov ("edx","ebp");
165 &set_label("nocpuid");
166 &function_end("OPENSSL_ia32_cpuid");
167
168 &external_label("OPENSSL_ia32cap_P");
169
170 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
171 &xor ("eax","eax");
172 &xor ("edx","edx");
173 &picmeup("ecx","OPENSSL_ia32cap_P");
174 &bt (&DWP(0,"ecx"),4);
175 &jnc (&label("notsc"));
176 &rdtsc ();
177 &set_label("notsc");
178 &ret ();
179 &function_end_B("OPENSSL_rdtsc");
180
181 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
182 # but it's safe to call it on any [supported] 32-bit platform...
183 # Just check for [non-]zero return value...
184 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
185 &picmeup("ecx","OPENSSL_ia32cap_P");
186 &bt (&DWP(0,"ecx"),4);
187 &jnc (&label("nohalt")); # no TSC
188
189 &data_word(0x9058900e); # push %cs; pop %eax
190 &and ("eax",3);
191 &jnz (&label("nohalt")); # not enough privileges
192
193 &pushf ();
194 &pop ("eax");
195 &bt ("eax",9);
196 &jnc (&label("nohalt")); # interrupts are disabled
197
198 &rdtsc ();
199 &push ("edx");
200 &push ("eax");
201 &halt ();
202 &rdtsc ();
203
204 &sub ("eax",&DWP(0,"esp"));
205 &sbb ("edx",&DWP(4,"esp"));
206 &add ("esp",8);
207 &ret ();
208
209 &set_label("nohalt");
210 &xor ("eax","eax");
211 &xor ("edx","edx");
212 &ret ();
213 &function_end_B("OPENSSL_instrument_halt");
214
215 # Essentially there is only one use for this function. Under DJGPP:
216 #
217 # #include <go32.h>
218 # ...
219 # i=OPENSSL_far_spin(_dos_ds,0x46c);
220 # ...
221 # to obtain the number of spins till closest timer interrupt.
222
223 &function_begin_B("OPENSSL_far_spin");
224 &pushf ();
225 &pop ("eax");
226 &bt ("eax",9);
227 &jnc (&label("nospin")); # interrupts are disabled
228
229 &mov ("eax",&DWP(4,"esp"));
230 &mov ("ecx",&DWP(8,"esp"));
231 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
232 &xor ("eax","eax");
233 &mov ("edx",&DWP(0,"ecx"));
234 &jmp (&label("spin"));
235
236 &align (16);
237 &set_label("spin");
238 &inc ("eax");
239 &cmp ("edx",&DWP(0,"ecx"));
240 &je (&label("spin"));
241
242 &data_word (0x1f909090); # pop %ds
243 &ret ();
244
245 &set_label("nospin");
246 &xor ("eax","eax");
247 &xor ("edx","edx");
248 &ret ();
249 &function_end_B("OPENSSL_far_spin");
250
251 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
252 &xor ("eax","eax");
253 &xor ("edx","edx");
254 &picmeup("ecx","OPENSSL_ia32cap_P");
255 &mov ("ecx",&DWP(0,"ecx"));
256 &bt (&DWP(0,"ecx"),1);
257 &jnc (&label("no_x87"));
258 if ($sse2) {
259 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
260 &cmp ("ecx",1<<26|1<<24);
261 &jne (&label("no_sse2"));
262 &pxor ("xmm0","xmm0");
263 &pxor ("xmm1","xmm1");
264 &pxor ("xmm2","xmm2");
265 &pxor ("xmm3","xmm3");
266 &pxor ("xmm4","xmm4");
267 &pxor ("xmm5","xmm5");
268 &pxor ("xmm6","xmm6");
269 &pxor ("xmm7","xmm7");
270 &set_label("no_sse2");
271 }
272 # just a bunch of fldz to zap the fp/mm bank followed by finit...
273 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
274 &set_label("no_x87");
275 &lea ("eax",&DWP(4,"esp"));
276 &ret ();
277 &function_end_B("OPENSSL_wipe_cpu");
278
279 &function_begin_B("OPENSSL_atomic_add");
280 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
281 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
282 &push ("ebx");
283 &nop ();
284 &mov ("eax",&DWP(0,"edx"));
285 &set_label("spin");
286 &lea ("ebx",&DWP(0,"eax","ecx"));
287 &nop ();
288 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
289 &jne (&label("spin"));
290 &mov ("eax","ebx"); # OpenSSL expects the new value
291 &pop ("ebx");
292 &ret ();
293 &function_end_B("OPENSSL_atomic_add");
294
295 # This function can become handy under Win32 in situations when
296 # we don't know which calling convention, __stdcall or __cdecl(*),
297 # indirect callee is using. In C it can be deployed as
298 #
299 #ifdef OPENSSL_CPUID_OBJ
300 # type OPENSSL_indirect_call(void *f,...);
301 # ...
302 # OPENSSL_indirect_call(func,[up to $max arguments]);
303 #endif
304 #
305 # (*) it's designed to work even for __fastcall if number of
306 # arguments is 1 or 2!
307 &function_begin_B("OPENSSL_indirect_call");
308 {
309 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
310 # in order to preserve eventual
311 # stack alignment
312 &push ("ebp");
313 &mov ("ebp","esp");
314 &sub ("esp",$max*4);
315 &mov ("ecx",&DWP(12,"ebp"));
316 &mov (&DWP(0,"esp"),"ecx");
317 &mov ("edx",&DWP(16,"ebp"));
318 &mov (&DWP(4,"esp"),"edx");
319 for($i=2;$i<$max;$i++)
320 {
321 # Some copies will be redundant/bogus...
322 &mov ("eax",&DWP(12+$i*4,"ebp"));
323 &mov (&DWP(0+$i*4,"esp"),"eax");
324 }
325 &call_ptr (&DWP(8,"ebp"));# make the call...
326 &mov ("esp","ebp"); # ... and just restore the stack pointer
327 # without paying attention to what we called,
328 # (__cdecl *func) or (__stdcall *one).
329 &pop ("ebp");
330 &ret ();
331 }
332 &function_end_B("OPENSSL_indirect_call");
333
334 &function_begin_B("OPENSSL_cleanse");
335 &mov ("edx",&wparam(0));
336 &mov ("ecx",&wparam(1));
337 &xor ("eax","eax");
338 &cmp ("ecx",7);
339 &jae (&label("lot"));
340 &cmp ("ecx",0);
341 &je (&label("ret"));
342 &set_label("little");
343 &mov (&BP(0,"edx"),"al");
344 &sub ("ecx",1);
345 &lea ("edx",&DWP(1,"edx"));
346 &jnz (&label("little"));
347 &set_label("ret");
348 &ret ();
349
350 &set_label("lot",16);
351 &test ("edx",3);
352 &jz (&label("aligned"));
353 &mov (&BP(0,"edx"),"al");
354 &lea ("ecx",&DWP(-1,"ecx"));
355 &lea ("edx",&DWP(1,"edx"));
356 &jmp (&label("lot"));
357 &set_label("aligned");
358 &mov (&DWP(0,"edx"),"eax");
359 &lea ("ecx",&DWP(-4,"ecx"));
360 &test ("ecx",-4);
361 &lea ("edx",&DWP(4,"edx"));
362 &jnz (&label("aligned"));
363 &cmp ("ecx",0);
364 &jne (&label("little"));
365 &ret ();
366 &function_end_B("OPENSSL_cleanse");
367
368 {
369 my $lasttick = "esi";
370 my $lastdiff = "ebx";
371 my $out = "edi";
372 my $cnt = "ecx";
373 my $max = "ebp";
374
375 &function_begin("OPENSSL_instrument_bus");
376 &mov ("eax",0);
377 if ($sse2) {
378 &picmeup("edx","OPENSSL_ia32cap_P");
379 &bt (&DWP(0,"edx"),4);
380 &jnc (&label("nogo")); # no TSC
381 &bt (&DWP(0,"edx"),19);
382 &jnc (&label("nogo")); # no CLFLUSH
383
384 &mov ($out,&wparam(0)); # load arguments
385 &mov ($cnt,&wparam(1));
386
387 # collect 1st tick
388 &rdtsc ();
389 &mov ($lasttick,"eax"); # lasttick = tick
390 &mov ($lastdiff,0); # lastdiff = 0
391 &clflush(&DWP(0,$out));
392 &data_byte(0xf0); # lock
393 &add (&DWP(0,$out),$lastdiff);
394 &jmp (&label("loop"));
395
396 &set_label("loop",16);
397 &rdtsc ();
398 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
399 &sub ("eax",$lasttick); # diff
400 &mov ($lasttick,"edx"); # lasttick = tick
401 &mov ($lastdiff,"eax"); # lastdiff = diff
402 &clflush(&DWP(0,$out));
403 &data_byte(0xf0); # lock
404 &add (&DWP(0,$out),"eax"); # accumulate diff
405 &lea ($out,&DWP(4,$out)); # ++$out
406 &sub ($cnt,1); # --$cnt
407 &jnz (&label("loop"));
408
409 &mov ("eax",&wparam(1));
410 &set_label("nogo");
411 }
412 &function_end("OPENSSL_instrument_bus");
413
414 &function_begin("OPENSSL_instrument_bus2");
415 &mov ("eax",0);
416 if ($sse2) {
417 &picmeup("edx","OPENSSL_ia32cap_P");
418 &bt (&DWP(0,"edx"),4);
419 &jnc (&label("nogo")); # no TSC
420 &bt (&DWP(0,"edx"),19);
421 &jnc (&label("nogo")); # no CLFLUSH
422
423 &mov ($out,&wparam(0)); # load arguments
424 &mov ($cnt,&wparam(1));
425 &mov ($max,&wparam(2));
426
427 &rdtsc (); # collect 1st tick
428 &mov ($lasttick,"eax"); # lasttick = tick
429 &mov ($lastdiff,0); # lastdiff = 0
430
431 &clflush(&DWP(0,$out));
432 &data_byte(0xf0); # lock
433 &add (&DWP(0,$out),$lastdiff);
434
435 &rdtsc (); # collect 1st diff
436 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
437 &sub ("eax",$lasttick); # diff
438 &mov ($lasttick,"edx"); # lasttick = tick
439 &mov ($lastdiff,"eax"); # lastdiff = diff
440 &jmp (&label("loop2"));
441
442 &set_label("loop2",16);
443 &clflush(&DWP(0,$out));
444 &data_byte(0xf0); # lock
445 &add (&DWP(0,$out),"eax"); # accumulate diff
446
447 &sub ($max,1);
448 &jz (&label("done2"));
449
450 &rdtsc ();
451 &mov ("edx","eax"); # put aside tick (yes, I neglect edx)
452 &sub ("eax",$lasttick); # diff
453 &mov ($lasttick,"edx"); # lasttick = tick
454 &cmp ("eax",$lastdiff);
455 &mov ($lastdiff,"eax"); # lastdiff = diff
456 &mov ("edx",0);
457 &setne ("dl");
458 &sub ($cnt,"edx"); # conditional --$cnt
459 &lea ($out,&DWP(0,$out,"edx",4)); # conditional ++$out
460 &jnz (&label("loop2"));
461
462 &set_label("done2");
463 &mov ("eax",&wparam(1));
464 &sub ("eax",$cnt);
465 &set_label("nogo");
466 }
467 &function_end("OPENSSL_instrument_bus2");
468 }
469
470 &function_begin_B("OPENSSL_ia32_rdrand");
471 &mov ("ecx",8);
472 &set_label("loop");
473 &rdrand ("eax");
474 &jc (&label("break"));
475 &loop (&label("loop"));
476 &set_label("break");
477 &cmp ("eax",0);
478 &cmove ("eax","ecx");
479 &ret ();
480 &function_end_B("OPENSSL_ia32_rdrand");
481
482 &function_begin_B("OPENSSL_ia32_rdseed");
483 &mov ("ecx",8);
484 &set_label("loop");
485 &rdseed ("eax");
486 &jc (&label("break"));
487 &loop (&label("loop"));
488 &set_label("break");
489 &cmp ("eax",0);
490 &cmove ("eax","ecx");
491 &ret ();
492 &function_end_B("OPENSSL_ia32_rdseed");
493
494 &initseg("OPENSSL_cpuid_setup");
495
496 &hidden("OPENSSL_cpuid_setup");
497 &hidden("OPENSSL_ia32cap_P");
498
499 &asm_finish();
500
501 close STDOUT;