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