3 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
4 push(@INC, "${dir}perlasm", "perlasm");
7 &asm_init($ARGV[0],"x86cpuid");
9 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
11 &function_begin("OPENSSL_ia32_cpuid");
24 &jnc (&label("nocpuid"));
26 &mov ("edi","eax"); # max value for standard query level
29 &cmp ("ebx",0x756e6547); # "Genu"
32 &cmp ("edx",0x49656e69); # "ineI"
35 &cmp ("ecx",0x6c65746e); # "ntel"
37 &or ("ebp","eax"); # 0 indicates Intel CPU
38 &jz (&label("intel"));
40 &cmp ("ebx",0x68747541); # "Auth"
43 &cmp ("edx",0x69746E65); # "enti"
46 &cmp ("ecx",0x444D4163); # "cAMD"
48 &or ("esi","eax"); # 0 indicates AMD CPU
49 &jnz (&label("intel"));
52 &mov ("eax",0x80000000);
54 &cmp ("eax",0x80000001);
55 &jb (&label("intel"));
57 &mov ("eax",0x80000001);
60 &and ("ebp",1<<11|1); # isolate XOP bit
61 &cmp ("esi",0x80000008);
62 &jb (&label("intel"));
64 &mov ("eax",0x80000008);
66 &movz ("esi",&LB("ecx")); # number of cores - 1
67 &inc ("esi"); # number of cores
73 &jnc (&label("generic"));
77 &ja (&label("generic"));
78 &and ("edx",0xefffffff); # clear hyper-threading bit
79 &jmp (&label("generic"));
83 &jb (&label("cacheinfo"));
85 &mov ("esi",&wparam(0));
89 &mov (&DWP(8,"esi"),"ebx");
91 &set_label("cacheinfo");
94 &jb (&label("nocacheinfo"));
97 &mov ("ecx",0); # query L1D
101 &and ("edi",0xfff); # number of cores -1 per L1D
103 &set_label("nocacheinfo");
107 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
109 &jne (&label("notintel"));
110 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
111 &and (&HB("eax"),15); # familiy ID
112 &cmp (&HB("eax"),15); # P4?
113 &jne (&label("notintel"));
114 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
115 &set_label("notintel");
116 &bt ("edx",28); # test hyper-threading bit
117 &jnc (&label("generic"));
118 &and ("edx",0xefffffff);
120 &je (&label("generic"));
122 &or ("edx",0x10000000);
125 &ja (&label("generic"));
126 &and ("edx",0xefffffff); # clear hyper-threading bit if not
128 &set_label("generic");
129 &and ("ebp",1<<11); # isolate AMD XOP flag
130 &and ("ecx",0xfffff7ff); # force 11th bit to 0
132 &or ("ebp","ecx"); # merge AMD XOP flag
134 &bt ("ecx",27); # check OSXSAVE bit
135 &jnc (&label("clear_avx"));
137 &data_byte(0x0f,0x01,0xd0); # xgetbv
140 &je (&label("done"));
142 &je (&label("clear_avx"));
143 &set_label("clear_xmm");
144 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
145 &and ("esi",0xfeffffff); # clear FXSR
146 &set_label("clear_avx");
147 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
151 &set_label("nocpuid");
152 &function_end("OPENSSL_ia32_cpuid");
154 &external_label("OPENSSL_ia32cap_P");
156 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
159 &picmeup("ecx","OPENSSL_ia32cap_P");
160 &bt (&DWP(0,"ecx"),4);
161 &jnc (&label("notsc"));
165 &function_end_B("OPENSSL_rdtsc");
167 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
168 # but it's safe to call it on any [supported] 32-bit platform...
169 # Just check for [non-]zero return value...
170 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
171 &picmeup("ecx","OPENSSL_ia32cap_P");
172 &bt (&DWP(0,"ecx"),4);
173 &jnc (&label("nohalt")); # no TSC
175 &data_word(0x9058900e); # push %cs; pop %eax
177 &jnz (&label("nohalt")); # not enough privileges
182 &jnc (&label("nohalt")); # interrupts are disabled
190 &sub ("eax",&DWP(0,"esp"));
191 &sbb ("edx",&DWP(4,"esp"));
195 &set_label("nohalt");
199 &function_end_B("OPENSSL_instrument_halt");
201 # Essentially there is only one use for this function. Under DJGPP:
205 # i=OPENSSL_far_spin(_dos_ds,0x46c);
207 # to obtain the number of spins till closest timer interrupt.
209 &function_begin_B("OPENSSL_far_spin");
213 &jnc (&label("nospin")); # interrupts are disabled
215 &mov ("eax",&DWP(4,"esp"));
216 &mov ("ecx",&DWP(8,"esp"));
217 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
219 &mov ("edx",&DWP(0,"ecx"));
220 &jmp (&label("spin"));
225 &cmp ("edx",&DWP(0,"ecx"));
226 &je (&label("spin"));
228 &data_word (0x1f909090); # pop %ds
231 &set_label("nospin");
235 &function_end_B("OPENSSL_far_spin");
237 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
240 &picmeup("ecx","OPENSSL_ia32cap_P");
241 &mov ("ecx",&DWP(0,"ecx"));
242 &bt (&DWP(0,"ecx"),1);
243 &jnc (&label("no_x87"));
245 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
246 &cmp ("ecx",1<<26|1<<24);
247 &jne (&label("no_sse2"));
248 &pxor ("xmm0","xmm0");
249 &pxor ("xmm1","xmm1");
250 &pxor ("xmm2","xmm2");
251 &pxor ("xmm3","xmm3");
252 &pxor ("xmm4","xmm4");
253 &pxor ("xmm5","xmm5");
254 &pxor ("xmm6","xmm6");
255 &pxor ("xmm7","xmm7");
256 &set_label("no_sse2");
258 # just a bunch of fldz to zap the fp/mm bank followed by finit...
259 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
260 &set_label("no_x87");
261 &lea ("eax",&DWP(4,"esp"));
263 &function_end_B("OPENSSL_wipe_cpu");
265 &function_begin_B("OPENSSL_atomic_add");
266 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
267 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
270 &mov ("eax",&DWP(0,"edx"));
272 &lea ("ebx",&DWP(0,"eax","ecx"));
274 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
275 &jne (&label("spin"));
276 &mov ("eax","ebx"); # OpenSSL expects the new value
279 &function_end_B("OPENSSL_atomic_add");
281 # This function can become handy under Win32 in situations when
282 # we don't know which calling convention, __stdcall or __cdecl(*),
283 # indirect callee is using. In C it can be deployed as
285 #ifdef OPENSSL_CPUID_OBJ
286 # type OPENSSL_indirect_call(void *f,...);
288 # OPENSSL_indirect_call(func,[up to $max arguments]);
291 # (*) it's designed to work even for __fastcall if number of
292 # arguments is 1 or 2!
293 &function_begin_B("OPENSSL_indirect_call");
295 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
296 # in order to preserve eventual
301 &mov ("ecx",&DWP(12,"ebp"));
302 &mov (&DWP(0,"esp"),"ecx");
303 &mov ("edx",&DWP(16,"ebp"));
304 &mov (&DWP(4,"esp"),"edx");
305 for($i=2;$i<$max;$i++)
307 # Some copies will be redundant/bogus...
308 &mov ("eax",&DWP(12+$i*4,"ebp"));
309 &mov (&DWP(0+$i*4,"esp"),"eax");
311 &call_ptr (&DWP(8,"ebp"));# make the call...
312 &mov ("esp","ebp"); # ... and just restore the stack pointer
313 # without paying attention to what we called,
314 # (__cdecl *func) or (__stdcall *one).
318 &function_end_B("OPENSSL_indirect_call");
320 &function_begin_B("OPENSSL_cleanse");
321 &mov ("edx",&wparam(0));
322 &mov ("ecx",&wparam(1));
325 &jae (&label("lot"));
328 &set_label("little");
329 &mov (&BP(0,"edx"),"al");
331 &lea ("edx",&DWP(1,"edx"));
332 &jnz (&label("little"));
336 &set_label("lot",16);
338 &jz (&label("aligned"));
339 &mov (&BP(0,"edx"),"al");
340 &lea ("ecx",&DWP(-1,"ecx"));
341 &lea ("edx",&DWP(1,"edx"));
342 &jmp (&label("lot"));
343 &set_label("aligned");
344 &mov (&DWP(0,"edx"),"eax");
345 &lea ("ecx",&DWP(-4,"ecx"));
347 &lea ("edx",&DWP(4,"edx"));
348 &jnz (&label("aligned"));
350 &jne (&label("little"));
352 &function_end_B("OPENSSL_cleanse");
354 &function_begin_B("OPENSSL_ia32_rdrand");
358 &jc (&label("break"));
359 &loop (&label("loop"));
362 &cmove ("eax","ecx");
364 &function_end_B("OPENSSL_ia32_rdrand");
366 &initseg("OPENSSL_cpuid_setup");
368 &hidden("OPENSSL_cpuid_setup");
369 &hidden("OPENSSL_ia32cap_P");