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