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