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