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");
23 &mov ("esi",&wparam(0));
24 &mov (&DWP(8,"esi"),"eax"); # clear extended feature flags
26 &jnc (&label("nocpuid"));
28 &mov ("edi","eax"); # max value for standard query level
31 &cmp ("ebx",0x756e6547); # "Genu"
34 &cmp ("edx",0x49656e69); # "ineI"
37 &cmp ("ecx",0x6c65746e); # "ntel"
39 &or ("ebp","eax"); # 0 indicates Intel CPU
40 &jz (&label("intel"));
42 &cmp ("ebx",0x68747541); # "Auth"
45 &cmp ("edx",0x69746E65); # "enti"
48 &cmp ("ecx",0x444D4163); # "cAMD"
50 &or ("esi","eax"); # 0 indicates AMD CPU
51 &jnz (&label("intel"));
54 &mov ("eax",0x80000000);
56 &cmp ("eax",0x80000001);
57 &jb (&label("intel"));
59 &mov ("eax",0x80000001);
62 &and ("ebp",1<<11|1); # isolate XOP bit
63 &cmp ("esi",0x80000008);
64 &jb (&label("intel"));
66 &mov ("eax",0x80000008);
68 &movz ("esi",&LB("ecx")); # number of cores - 1
69 &inc ("esi"); # number of cores
75 &jnc (&label("generic"));
79 &ja (&label("generic"));
80 &and ("edx",0xefffffff); # clear hyper-threading bit
81 &jmp (&label("generic"));
86 &jb (&label("nocacheinfo"));
89 &mov ("ecx",0); # query L1D
93 &and ("esi",0xfff); # number of cores -1 per L1D
95 &set_label("nocacheinfo");
99 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
101 &jne (&label("notintel"));
102 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
103 &and (&HB("eax"),15); # familiy ID
104 &cmp (&HB("eax"),15); # P4?
105 &jne (&label("notintel"));
106 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
107 &set_label("notintel");
108 &bt ("edx",28); # test hyper-threading bit
109 &jnc (&label("generic"));
110 &and ("edx",0xefffffff);
112 &je (&label("generic"));
114 &or ("edx",0x10000000);
117 &ja (&label("generic"));
118 &and ("edx",0xefffffff); # clear hyper-threading bit if not
120 &set_label("generic");
121 &and ("ebp",1<<11); # isolate AMD XOP flag
122 &and ("ecx",0xfffff7ff); # force 11th bit to 0
123 &mov ("esi","edx"); # %ebp:%esi is copy of %ecx:%edx
124 &or ("ebp","ecx"); # merge AMD XOP flag
127 &mov ("edi",&wparam(0));
128 &jb (&label("no_extended_info"));
132 &mov (&DWP(8,"edi"),"ebx"); # save extended feature flag
133 &set_label("no_extended_info");
135 &bt ("ebp",27); # check OSXSAVE bit
136 &jnc (&label("clear_avx"));
138 &data_byte(0x0f,0x01,0xd0); # xgetbv
141 &je (&label("done"));
143 &je (&label("clear_avx"));
144 &set_label("clear_xmm");
145 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
146 &and ("esi",0xfeffffff); # clear FXSR
147 &set_label("clear_avx");
148 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
149 &and (&DWP(8,"edi"),0xffffffdf); # clear AVX2
153 &set_label("nocpuid");
154 &function_end("OPENSSL_ia32_cpuid");
156 &external_label("OPENSSL_ia32cap_P");
158 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
161 &picmeup("ecx","OPENSSL_ia32cap_P");
162 &bt (&DWP(0,"ecx"),4);
163 &jnc (&label("notsc"));
167 &function_end_B("OPENSSL_rdtsc");
169 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
170 # but it's safe to call it on any [supported] 32-bit platform...
171 # Just check for [non-]zero return value...
172 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
173 &picmeup("ecx","OPENSSL_ia32cap_P");
174 &bt (&DWP(0,"ecx"),4);
175 &jnc (&label("nohalt")); # no TSC
177 &data_word(0x9058900e); # push %cs; pop %eax
179 &jnz (&label("nohalt")); # not enough privileges
184 &jnc (&label("nohalt")); # interrupts are disabled
192 &sub ("eax",&DWP(0,"esp"));
193 &sbb ("edx",&DWP(4,"esp"));
197 &set_label("nohalt");
201 &function_end_B("OPENSSL_instrument_halt");
203 # Essentially there is only one use for this function. Under DJGPP:
207 # i=OPENSSL_far_spin(_dos_ds,0x46c);
209 # to obtain the number of spins till closest timer interrupt.
211 &function_begin_B("OPENSSL_far_spin");
215 &jnc (&label("nospin")); # interrupts are disabled
217 &mov ("eax",&DWP(4,"esp"));
218 &mov ("ecx",&DWP(8,"esp"));
219 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
221 &mov ("edx",&DWP(0,"ecx"));
222 &jmp (&label("spin"));
227 &cmp ("edx",&DWP(0,"ecx"));
228 &je (&label("spin"));
230 &data_word (0x1f909090); # pop %ds
233 &set_label("nospin");
237 &function_end_B("OPENSSL_far_spin");
239 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
242 &picmeup("ecx","OPENSSL_ia32cap_P");
243 &mov ("ecx",&DWP(0,"ecx"));
244 &bt (&DWP(0,"ecx"),1);
245 &jnc (&label("no_x87"));
247 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
248 &cmp ("ecx",1<<26|1<<24);
249 &jne (&label("no_sse2"));
250 &pxor ("xmm0","xmm0");
251 &pxor ("xmm1","xmm1");
252 &pxor ("xmm2","xmm2");
253 &pxor ("xmm3","xmm3");
254 &pxor ("xmm4","xmm4");
255 &pxor ("xmm5","xmm5");
256 &pxor ("xmm6","xmm6");
257 &pxor ("xmm7","xmm7");
258 &set_label("no_sse2");
260 # just a bunch of fldz to zap the fp/mm bank followed by finit...
261 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
262 &set_label("no_x87");
263 &lea ("eax",&DWP(4,"esp"));
265 &function_end_B("OPENSSL_wipe_cpu");
267 &function_begin_B("OPENSSL_atomic_add");
268 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
269 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
272 &mov ("eax",&DWP(0,"edx"));
274 &lea ("ebx",&DWP(0,"eax","ecx"));
276 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
277 &jne (&label("spin"));
278 &mov ("eax","ebx"); # OpenSSL expects the new value
281 &function_end_B("OPENSSL_atomic_add");
283 # This function can become handy under Win32 in situations when
284 # we don't know which calling convention, __stdcall or __cdecl(*),
285 # indirect callee is using. In C it can be deployed as
287 #ifdef OPENSSL_CPUID_OBJ
288 # type OPENSSL_indirect_call(void *f,...);
290 # OPENSSL_indirect_call(func,[up to $max arguments]);
293 # (*) it's designed to work even for __fastcall if number of
294 # arguments is 1 or 2!
295 &function_begin_B("OPENSSL_indirect_call");
297 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
298 # in order to preserve eventual
303 &mov ("ecx",&DWP(12,"ebp"));
304 &mov (&DWP(0,"esp"),"ecx");
305 &mov ("edx",&DWP(16,"ebp"));
306 &mov (&DWP(4,"esp"),"edx");
307 for($i=2;$i<$max;$i++)
309 # Some copies will be redundant/bogus...
310 &mov ("eax",&DWP(12+$i*4,"ebp"));
311 &mov (&DWP(0+$i*4,"esp"),"eax");
313 &call_ptr (&DWP(8,"ebp"));# make the call...
314 &mov ("esp","ebp"); # ... and just restore the stack pointer
315 # without paying attention to what we called,
316 # (__cdecl *func) or (__stdcall *one).
320 &function_end_B("OPENSSL_indirect_call");
322 &function_begin_B("OPENSSL_cleanse");
323 &mov ("edx",&wparam(0));
324 &mov ("ecx",&wparam(1));
327 &jae (&label("lot"));
330 &set_label("little");
331 &mov (&BP(0,"edx"),"al");
333 &lea ("edx",&DWP(1,"edx"));
334 &jnz (&label("little"));
338 &set_label("lot",16);
340 &jz (&label("aligned"));
341 &mov (&BP(0,"edx"),"al");
342 &lea ("ecx",&DWP(-1,"ecx"));
343 &lea ("edx",&DWP(1,"edx"));
344 &jmp (&label("lot"));
345 &set_label("aligned");
346 &mov (&DWP(0,"edx"),"eax");
347 &lea ("ecx",&DWP(-4,"ecx"));
349 &lea ("edx",&DWP(4,"edx"));
350 &jnz (&label("aligned"));
352 &jne (&label("little"));
354 &function_end_B("OPENSSL_cleanse");
356 &function_begin_B("OPENSSL_ia32_rdrand");
360 &jc (&label("break"));
361 &loop (&label("loop"));
364 &cmove ("eax","ecx");
366 &function_end_B("OPENSSL_ia32_rdrand");
368 &function_begin_B("OPENSSL_ia32_rdseed");
372 &jc (&label("break"));
373 &loop (&label("loop"));
376 &cmove ("eax","ecx");
378 &function_end_B("OPENSSL_ia32_rdseed");
380 &initseg("OPENSSL_cpuid_setup");
382 &hidden("OPENSSL_cpuid_setup");
383 &hidden("OPENSSL_ia32cap_P");