crypto/x86[_64]cpuid.pl: add OPENSSL_ia32_rd[rand|seed]_bytes.
[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 # This function can become handy under Win32 in situations when
296 # we don't know which calling convention, __stdcall or __cdecl(*),
297 # indirect callee is using. In C it can be deployed as
298 #
299 #ifdef OPENSSL_CPUID_OBJ
300 #       type OPENSSL_indirect_call(void *f,...);
301 #       ...
302 #       OPENSSL_indirect_call(func,[up to $max arguments]);
303 #endif
304 #
305 # (*)   it's designed to work even for __fastcall if number of
306 #       arguments is 1 or 2!
307 &function_begin_B("OPENSSL_indirect_call");
308         {
309         my ($max,$i)=(7,);      # $max has to be chosen as 4*n-1
310                                 # in order to preserve eventual
311                                 # stack alignment
312         &push   ("ebp");
313         &mov    ("ebp","esp");
314         &sub    ("esp",$max*4);
315         &mov    ("ecx",&DWP(12,"ebp"));
316         &mov    (&DWP(0,"esp"),"ecx");
317         &mov    ("edx",&DWP(16,"ebp"));
318         &mov    (&DWP(4,"esp"),"edx");
319         for($i=2;$i<$max;$i++)
320                 {
321                 # Some copies will be redundant/bogus...
322                 &mov    ("eax",&DWP(12+$i*4,"ebp"));
323                 &mov    (&DWP(0+$i*4,"esp"),"eax");
324                 }
325         &call_ptr       (&DWP(8,"ebp"));# make the call...
326         &mov    ("esp","ebp");  # ... and just restore the stack pointer
327                                 # without paying attention to what we called,
328                                 # (__cdecl *func) or (__stdcall *one).
329         &pop    ("ebp");
330         &ret    ();
331         }
332 &function_end_B("OPENSSL_indirect_call");
333
334 &function_begin_B("OPENSSL_cleanse");
335         &mov    ("edx",&wparam(0));
336         &mov    ("ecx",&wparam(1));
337         &xor    ("eax","eax");
338         &cmp    ("ecx",7);
339         &jae    (&label("lot"));
340         &cmp    ("ecx",0);
341         &je     (&label("ret"));
342 &set_label("little");
343         &mov    (&BP(0,"edx"),"al");
344         &sub    ("ecx",1);
345         &lea    ("edx",&DWP(1,"edx"));
346         &jnz    (&label("little"));
347 &set_label("ret");
348         &ret    ();
349
350 &set_label("lot",16);
351         &test   ("edx",3);
352         &jz     (&label("aligned"));
353         &mov    (&BP(0,"edx"),"al");
354         &lea    ("ecx",&DWP(-1,"ecx"));
355         &lea    ("edx",&DWP(1,"edx"));
356         &jmp    (&label("lot"));
357 &set_label("aligned");
358         &mov    (&DWP(0,"edx"),"eax");
359         &lea    ("ecx",&DWP(-4,"ecx"));
360         &test   ("ecx",-4);
361         &lea    ("edx",&DWP(4,"edx"));
362         &jnz    (&label("aligned"));
363         &cmp    ("ecx",0);
364         &jne    (&label("little"));
365         &ret    ();
366 &function_end_B("OPENSSL_cleanse");
367
368 &function_begin_B("CRYPTO_memcmp");
369         &push   ("esi");
370         &push   ("edi");
371         &mov    ("esi",&wparam(0));
372         &mov    ("edi",&wparam(1));
373         &mov    ("ecx",&wparam(2));
374         &xor    ("eax","eax");
375         &xor    ("edx","edx");
376         &cmp    ("ecx",0);
377         &je     (&label("no_data"));
378 &set_label("loop");
379         &mov    ("dl",&BP(0,"esi"));
380         &lea    ("esi",&DWP(1,"esi"));
381         &xor    ("dl",&BP(0,"edi"));
382         &lea    ("edi",&DWP(1,"edi"));
383         &or     ("al","dl");
384         &dec    ("ecx");
385         &jnz    (&label("loop"));
386         &neg    ("eax");
387         &shr    ("eax",31);
388 &set_label("no_data");
389         &pop    ("edi");
390         &pop    ("esi");
391         &ret    ();
392 &function_end_B("CRYPTO_memcmp");
393 {
394 my $lasttick = "esi";
395 my $lastdiff = "ebx";
396 my $out = "edi";
397 my $cnt = "ecx";
398 my $max = "ebp";
399
400 &function_begin("OPENSSL_instrument_bus");
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
412         # collect 1st tick
413         &rdtsc  ();
414         &mov    ($lasttick,"eax");      # lasttick = tick
415         &mov    ($lastdiff,0);          # lastdiff = 0
416         &clflush(&DWP(0,$out));
417         &data_byte(0xf0);               # lock
418         &add    (&DWP(0,$out),$lastdiff);
419         &jmp    (&label("loop"));
420
421 &set_label("loop",16);
422         &rdtsc  ();
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         &clflush(&DWP(0,$out));
428         &data_byte(0xf0);               # lock
429         &add    (&DWP(0,$out),"eax");   # accumulate diff
430         &lea    ($out,&DWP(4,$out));    # ++$out
431         &sub    ($cnt,1);               # --$cnt
432         &jnz    (&label("loop"));
433
434         &mov    ("eax",&wparam(1));
435 &set_label("nogo");
436     }
437 &function_end("OPENSSL_instrument_bus");
438
439 &function_begin("OPENSSL_instrument_bus2");
440     &mov        ("eax",0);
441     if ($sse2) {
442         &picmeup("edx","OPENSSL_ia32cap_P");
443         &bt     (&DWP(0,"edx"),4);
444         &jnc    (&label("nogo"));       # no TSC
445         &bt     (&DWP(0,"edx"),19);
446         &jnc    (&label("nogo"));       # no CLFLUSH
447
448         &mov    ($out,&wparam(0));      # load arguments
449         &mov    ($cnt,&wparam(1));
450         &mov    ($max,&wparam(2));
451
452         &rdtsc  ();                     # collect 1st tick
453         &mov    ($lasttick,"eax");      # lasttick = tick
454         &mov    ($lastdiff,0);          # lastdiff = 0
455
456         &clflush(&DWP(0,$out));
457         &data_byte(0xf0);               # lock
458         &add    (&DWP(0,$out),$lastdiff);
459
460         &rdtsc  ();                     # collect 1st diff
461         &mov    ("edx","eax");          # put aside tick (yes, I neglect edx)
462         &sub    ("eax",$lasttick);      # diff
463         &mov    ($lasttick,"edx");      # lasttick = tick
464         &mov    ($lastdiff,"eax");      # lastdiff = diff
465         &jmp    (&label("loop2"));
466
467 &set_label("loop2",16);
468         &clflush(&DWP(0,$out));
469         &data_byte(0xf0);               # lock
470         &add    (&DWP(0,$out),"eax");   # accumulate diff
471
472         &sub    ($max,1);
473         &jz     (&label("done2"));
474
475         &rdtsc  ();
476         &mov    ("edx","eax");          # put aside tick (yes, I neglect edx)
477         &sub    ("eax",$lasttick);      # diff
478         &mov    ($lasttick,"edx");      # lasttick = tick
479         &cmp    ("eax",$lastdiff);
480         &mov    ($lastdiff,"eax");      # lastdiff = diff
481         &mov    ("edx",0);
482         &setne  ("dl");
483         &sub    ($cnt,"edx");           # conditional --$cnt
484         &lea    ($out,&DWP(0,$out,"edx",4));    # conditional ++$out
485         &jnz    (&label("loop2"));
486
487 &set_label("done2");
488         &mov    ("eax",&wparam(1));
489         &sub    ("eax",$cnt);
490 &set_label("nogo");
491     }
492 &function_end("OPENSSL_instrument_bus2");
493 }
494
495 sub gen_random {
496 my $rdop = shift;
497 &function_begin_B("OPENSSL_ia32_${rdop}");
498         &mov    ("ecx",8);
499 &set_label("loop");
500         &${rdop}("eax");
501         &jc     (&label("break"));
502         &loop   (&label("loop"));
503 &set_label("break");
504         &cmp    ("eax",0);
505         &cmove  ("eax","ecx");
506         &ret    ();
507 &function_end_B("OPENSSL_ia32_${rdop}");
508
509 &function_begin_B("OPENSSL_ia32_${rdop}_bytes");
510         &push   ("edi");
511         &push   ("ebx");
512         &xor    ("eax","eax");          # return value
513         &mov    ("edi",&wparam(0));
514         &mov    ("ebx",&wparam(1));
515
516         &cmp    ("ebx",0);
517         &je     (&label("done"));
518
519         &mov    ("ecx",8);
520 &set_label("loop");
521         &${rdop}("edx");
522         &jc     (&label("break"));
523         &loop   (&label("loop"));
524         &jmp    (&label("done"));
525
526 &set_label("break",16);
527         &cmp    ("ebx",4);
528         &jb     (&label("tail"));
529         &mov    (&DWP(0,"edi"),"edx");
530         &lea    ("edi",&DWP(4,"edi"));
531         &add    ("eax",4);
532         &sub    ("ebx",4);
533         &jz     (&label("done"));
534         &mov    ("ecx",8);
535         &jmp    (&label("loop"));
536
537 &set_label("tail",16);
538         &mov    (&BP(0,"edi"),"dl");
539         &lea    ("edi",&DWP(1,"edi"));
540         &inc    ("eax");
541         &shr    ("edx",8);
542         &dec    ("ebx");
543         &jnz    (&label("tail"));
544
545 &set_label("done");
546         &pop    ("ebx");
547         &pop    ("edi");
548         &ret    ();
549 &function_end_B("OPENSSL_ia32_${rdop}_bytes");
550 }
551 &gen_random("rdrand");
552 &gen_random("rdseed");
553
554 &initseg("OPENSSL_cpuid_setup");
555
556 &hidden("OPENSSL_cpuid_setup");
557 &hidden("OPENSSL_ia32cap_P");
558
559 &asm_finish();
560
561 close STDOUT;