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