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