x86cpuid.pl: sync OPENSSL_ia32_cpuid with x86_64cpuid.pl.
[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("done"));
24         &xor    ("eax","eax");
25         &cpuid  ();
26         &xor    ("eax","eax");
27         &cmp    ("ebx",0x756e6547);     # "Genu"
28         &setne  (&LB("eax"));
29         &mov    ("ebp","eax");
30         &cmp    ("edx",0x49656e69);     # "ineI"
31         &setne  (&LB("eax"));
32         &or     ("ebp","eax");
33         &cmp    ("ecx",0x6c65746e);     # "ntel"
34         &setne  (&LB("eax"));
35         &or     ("ebp","eax");          # 0 indicates Intel CPU
36         &mov    ("esi",1);              # "number of [AMD] cores"
37         &jz     (&label("intel"));
38
39         &cmp    ("ebx",0x68747541);     # "Auth"
40         &setne  (&LB("eax"));
41         &mov    ("esi","eax");
42         &cmp    ("edx",0x69746E65);     # "enti"
43         &setne  (&LB("eax"));
44         &or     ("esi","eax");
45         &cmp    ("ecx",0x444D4163);     # "cAMD"
46         &setne  (&LB("eax"));
47         &or     ("esi","eax");          # 0 indicates AMD CPU
48         &jnz    (&label("intel"));
49
50         &mov    ("eax",0x80000000);
51         &cpuid  ();
52         &cmp    ("eax",0x80000008);
53         &mov    ("esi",1);              # "number of [AMD] cores"
54         &jb     (&label("intel"));
55
56         &mov    ("eax",0x80000008);
57         &cpuid  ();
58         &movz   ("esi",&LB("ecx"));     # number of cores - 1
59         &inc    ("esi");                # number of cores
60
61 &set_label("intel");
62         &mov    ("eax",1);
63         &cpuid  ();
64         &cmp    ("ebp",0);
65         &jne    (&label("notP4"));
66         &and    (&HB("eax"),15);        # familiy ID
67         &cmp    (&HB("eax"),15);        # P4?
68         &jne    (&label("notP4"));
69         &or     ("edx",1<<20);          # use reserved bit to engage RC4_CHAR
70 &set_label("notP4");
71         &bt     ("edx",28);             # test hyper-threading bit
72         &jnc    (&label("done"));
73         &shr    ("ebx",16);
74         &and    ("ebx",0xff);
75         &cmp    ("ebx","esi");          # see if cache is shared(*)
76         &ja     (&label("done"));
77         &and    ("edx",0xefffffff);     # clear hyper-threading bit if not
78 &set_label("done");
79         &mov    ("eax","edx");
80         &mov    ("edx","ecx");
81 &function_end("OPENSSL_ia32_cpuid");
82 # (*)   on Core2 this value is set to 2 denoting the fact that L2
83 #       cache is shared between cores.
84
85 &external_label("OPENSSL_ia32cap_P");
86
87 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
88         &xor    ("eax","eax");
89         &xor    ("edx","edx");
90         &picmeup("ecx","OPENSSL_ia32cap_P");
91         &bt     (&DWP(0,"ecx"),4);
92         &jnc    (&label("notsc"));
93         &rdtsc  ();
94 &set_label("notsc");
95         &ret    ();
96 &function_end_B("OPENSSL_rdtsc");
97
98 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
99 # but it's safe to call it on any [supported] 32-bit platform...
100 # Just check for [non-]zero return value...
101 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
102         &picmeup("ecx","OPENSSL_ia32cap_P");
103         &bt     (&DWP(0,"ecx"),4);
104         &jnc    (&label("nohalt"));     # no TSC
105
106         &data_word(0x9058900e);         # push %cs; pop %eax
107         &and    ("eax",3);
108         &jnz    (&label("nohalt"));     # not enough privileges
109
110         &pushf  ();
111         &pop    ("eax")
112         &bt     ("eax",9);
113         &jnc    (&label("nohalt"));     # interrupts are disabled
114
115         &rdtsc  ();
116         &push   ("edx");
117         &push   ("eax");
118         &halt   ();
119         &rdtsc  ();
120
121         &sub    ("eax",&DWP(0,"esp"));
122         &sbb    ("edx",&DWP(4,"esp"));
123         &add    ("esp",8);
124         &ret    ();
125
126 &set_label("nohalt");
127         &xor    ("eax","eax");
128         &xor    ("edx","edx");
129         &ret    ();
130 &function_end_B("OPENSSL_instrument_halt");
131
132 # Essentially there is only one use for this function. Under DJGPP:
133 #
134 #       #include <go32.h>
135 #       ...
136 #       i=OPENSSL_far_spin(_dos_ds,0x46c);
137 #       ...
138 # to obtain the number of spins till closest timer interrupt.
139
140 &function_begin_B("OPENSSL_far_spin");
141         &pushf  ();
142         &pop    ("eax")
143         &bt     ("eax",9);
144         &jnc    (&label("nospin"));     # interrupts are disabled
145
146         &mov    ("eax",&DWP(4,"esp"));
147         &mov    ("ecx",&DWP(8,"esp"));
148         &data_word (0x90d88e1e);        # push %ds, mov %eax,%ds
149         &xor    ("eax","eax");
150         &mov    ("edx",&DWP(0,"ecx"));
151         &jmp    (&label("spin"));
152
153         &align  (16);
154 &set_label("spin");
155         &inc    ("eax");
156         &cmp    ("edx",&DWP(0,"ecx"));
157         &je     (&label("spin"));
158
159         &data_word (0x1f909090);        # pop   %ds
160         &ret    ();
161
162 &set_label("nospin");
163         &xor    ("eax","eax");
164         &xor    ("edx","edx");
165         &ret    ();
166 &function_end_B("OPENSSL_far_spin");
167
168 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
169         &xor    ("eax","eax");
170         &xor    ("edx","edx");
171         &picmeup("ecx","OPENSSL_ia32cap_P");
172         &mov    ("ecx",&DWP(0,"ecx"));
173         &bt     (&DWP(0,"ecx"),1);
174         &jnc    (&label("no_x87"));
175         if ($sse2) {
176                 &bt     (&DWP(0,"ecx"),26);
177                 &jnc    (&label("no_sse2"));
178                 &pxor   ("xmm0","xmm0");
179                 &pxor   ("xmm1","xmm1");
180                 &pxor   ("xmm2","xmm2");
181                 &pxor   ("xmm3","xmm3");
182                 &pxor   ("xmm4","xmm4");
183                 &pxor   ("xmm5","xmm5");
184                 &pxor   ("xmm6","xmm6");
185                 &pxor   ("xmm7","xmm7");
186         &set_label("no_sse2");
187         }
188         # just a bunch of fldz to zap the fp/mm bank followed by finit...
189         &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
190 &set_label("no_x87");
191         &lea    ("eax",&DWP(4,"esp"));
192         &ret    ();
193 &function_end_B("OPENSSL_wipe_cpu");
194
195 &function_begin_B("OPENSSL_atomic_add");
196         &mov    ("edx",&DWP(4,"esp"));  # fetch the pointer, 1st arg
197         &mov    ("ecx",&DWP(8,"esp"));  # fetch the increment, 2nd arg
198         &push   ("ebx");
199         &nop    ();
200         &mov    ("eax",&DWP(0,"edx"));
201 &set_label("spin");
202         &lea    ("ebx",&DWP(0,"eax","ecx"));
203         &nop    ();
204         &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx)     # %eax is envolved and is always reloaded
205         &jne    (&label("spin"));
206         &mov    ("eax","ebx");  # OpenSSL expects the new value
207         &pop    ("ebx");
208         &ret    ();
209 &function_end_B("OPENSSL_atomic_add");
210
211 # This function can become handy under Win32 in situations when
212 # we don't know which calling convention, __stdcall or __cdecl(*),
213 # indirect callee is using. In C it can be deployed as
214 #
215 #ifdef OPENSSL_CPUID_OBJ
216 #       type OPENSSL_indirect_call(void *f,...);
217 #       ...
218 #       OPENSSL_indirect_call(func,[up to $max arguments]);
219 #endif
220 #
221 # (*)   it's designed to work even for __fastcall if number of
222 #       arguments is 1 or 2!
223 &function_begin_B("OPENSSL_indirect_call");
224         {
225         my $i,$max=7;           # $max has to be chosen as 4*n-1
226                                 # in order to preserve eventual
227                                 # stack alignment
228         &push   ("ebp");
229         &mov    ("ebp","esp");
230         &sub    ("esp",$max*4);
231         &mov    ("ecx",&DWP(12,"ebp"));
232         &mov    (&DWP(0,"esp"),"ecx");
233         &mov    ("edx",&DWP(16,"ebp"));
234         &mov    (&DWP(4,"esp"),"edx");
235         for($i=2;$i<$max;$i++)
236                 {
237                 # Some copies will be redundant/bogus...
238                 &mov    ("eax",&DWP(12+$i*4,"ebp"));
239                 &mov    (&DWP(0+$i*4,"esp"),"eax");
240                 }
241         &call_ptr       (&DWP(8,"ebp"));# make the call...
242         &mov    ("esp","ebp");  # ... and just restore the stack pointer
243                                 # without paying attention to what we called,
244                                 # (__cdecl *func) or (__stdcall *one).
245         &pop    ("ebp");
246         &ret    ();
247         }
248 &function_end_B("OPENSSL_indirect_call");
249
250 &function_begin_B("OPENSSL_cleanse");
251         &mov    ("edx",&wparam(0));
252         &mov    ("ecx",&wparam(1));
253         &xor    ("eax","eax");
254         &cmp    ("ecx",7);
255         &jae    (&label("lot"));
256 &set_label("little");
257         &mov    (&BP(0,"edx"),"al");
258         &sub    ("ecx",1);
259         &lea    ("edx",&DWP(1,"edx"));
260         &jnz    (&label("little"));
261         &ret    ();
262
263 &set_label("lot",16);
264         &test   ("edx",3);
265         &jz     (&label("aligned"));
266         &mov    (&BP(0,"edx"),"al");
267         &lea    ("ecx",&DWP(-1,"ecx"));
268         &lea    ("edx",&DWP(1,"edx"));
269         &jmp    (&label("lot"));
270 &set_label("aligned");
271         &mov    (&DWP(0,"edx"),"eax");
272         &lea    ("ecx",&DWP(-4,"ecx"));
273         &test   ("ecx",-4);
274         &lea    ("edx",&DWP(4,"edx"));
275         &jnz    (&label("aligned"));
276         &cmp    ("ecx",0);
277         &jne    (&label("little"));
278         &ret    ();
279 &function_end_B("OPENSSL_cleanse");
280
281 &initseg("OPENSSL_cpuid_setup");
282
283 &asm_finish();