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 &jnc (&label("done"));
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",0x80000008);
55 &jb (&label("intel"));
57 &mov ("eax",0x80000008);
59 &movz ("esi",&LB("ecx")); # number of cores - 1
60 &inc ("esi"); # number of cores
65 &jnc (&label("done"));
70 &and ("edx",0xefffffff); # clear hyper-threading bit
71 &jmp (&label("done"));
76 &jb (&label("nocacheinfo"));
79 &mov ("ecx",0); # query L1D
83 &and ("edi",0xfff); # number of cores -1 per L1D
85 &set_label("nocacheinfo");
89 &jne (&label("notP4"));
90 &and (&HB("eax"),15); # familiy ID
91 &cmp (&HB("eax"),15); # P4?
92 &jne (&label("notP4"));
93 &or ("edx",1<<20); # use reserved bit to engage RC4_CHAR
95 &bt ("edx",28); # test hyper-threading bit
96 &jnc (&label("done"));
97 &and ("edx",0xefffffff);
101 &or ("edx",0x10000000);
104 &ja (&label("done"));
105 &and ("edx",0xefffffff); # clear hyper-threading bit if not
109 &function_end("OPENSSL_ia32_cpuid");
111 &external_label("OPENSSL_ia32cap_P");
113 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
116 &picmeup("ecx","OPENSSL_ia32cap_P");
117 &bt (&DWP(0,"ecx"),4);
118 &jnc (&label("notsc"));
122 &function_end_B("OPENSSL_rdtsc");
124 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
125 # but it's safe to call it on any [supported] 32-bit platform...
126 # Just check for [non-]zero return value...
127 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
128 &picmeup("ecx","OPENSSL_ia32cap_P");
129 &bt (&DWP(0,"ecx"),4);
130 &jnc (&label("nohalt")); # no TSC
132 &data_word(0x9058900e); # push %cs; pop %eax
134 &jnz (&label("nohalt")); # not enough privileges
139 &jnc (&label("nohalt")); # interrupts are disabled
147 &sub ("eax",&DWP(0,"esp"));
148 &sbb ("edx",&DWP(4,"esp"));
152 &set_label("nohalt");
156 &function_end_B("OPENSSL_instrument_halt");
158 # Essentially there is only one use for this function. Under DJGPP:
162 # i=OPENSSL_far_spin(_dos_ds,0x46c);
164 # to obtain the number of spins till closest timer interrupt.
166 &function_begin_B("OPENSSL_far_spin");
170 &jnc (&label("nospin")); # interrupts are disabled
172 &mov ("eax",&DWP(4,"esp"));
173 &mov ("ecx",&DWP(8,"esp"));
174 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
176 &mov ("edx",&DWP(0,"ecx"));
177 &jmp (&label("spin"));
182 &cmp ("edx",&DWP(0,"ecx"));
183 &je (&label("spin"));
185 &data_word (0x1f909090); # pop %ds
188 &set_label("nospin");
192 &function_end_B("OPENSSL_far_spin");
194 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
197 &picmeup("ecx","OPENSSL_ia32cap_P");
198 &mov ("ecx",&DWP(0,"ecx"));
199 &bt (&DWP(0,"ecx"),1);
200 &jnc (&label("no_x87"));
202 &bt (&DWP(0,"ecx"),26);
203 &jnc (&label("no_sse2"));
204 &pxor ("xmm0","xmm0");
205 &pxor ("xmm1","xmm1");
206 &pxor ("xmm2","xmm2");
207 &pxor ("xmm3","xmm3");
208 &pxor ("xmm4","xmm4");
209 &pxor ("xmm5","xmm5");
210 &pxor ("xmm6","xmm6");
211 &pxor ("xmm7","xmm7");
212 &set_label("no_sse2");
214 # just a bunch of fldz to zap the fp/mm bank followed by finit...
215 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
216 &set_label("no_x87");
217 &lea ("eax",&DWP(4,"esp"));
219 &function_end_B("OPENSSL_wipe_cpu");
221 &function_begin_B("OPENSSL_atomic_add");
222 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
223 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
226 &mov ("eax",&DWP(0,"edx"));
228 &lea ("ebx",&DWP(0,"eax","ecx"));
230 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
231 &jne (&label("spin"));
232 &mov ("eax","ebx"); # OpenSSL expects the new value
235 &function_end_B("OPENSSL_atomic_add");
237 # This function can become handy under Win32 in situations when
238 # we don't know which calling convention, __stdcall or __cdecl(*),
239 # indirect callee is using. In C it can be deployed as
241 #ifdef OPENSSL_CPUID_OBJ
242 # type OPENSSL_indirect_call(void *f,...);
244 # OPENSSL_indirect_call(func,[up to $max arguments]);
247 # (*) it's designed to work even for __fastcall if number of
248 # arguments is 1 or 2!
249 &function_begin_B("OPENSSL_indirect_call");
251 my $i,$max=7; # $max has to be chosen as 4*n-1
252 # in order to preserve eventual
257 &mov ("ecx",&DWP(12,"ebp"));
258 &mov (&DWP(0,"esp"),"ecx");
259 &mov ("edx",&DWP(16,"ebp"));
260 &mov (&DWP(4,"esp"),"edx");
261 for($i=2;$i<$max;$i++)
263 # Some copies will be redundant/bogus...
264 &mov ("eax",&DWP(12+$i*4,"ebp"));
265 &mov (&DWP(0+$i*4,"esp"),"eax");
267 &call_ptr (&DWP(8,"ebp"));# make the call...
268 &mov ("esp","ebp"); # ... and just restore the stack pointer
269 # without paying attention to what we called,
270 # (__cdecl *func) or (__stdcall *one).
274 &function_end_B("OPENSSL_indirect_call");
276 &function_begin_B("OPENSSL_cleanse");
277 &mov ("edx",&wparam(0));
278 &mov ("ecx",&wparam(1));
281 &jae (&label("lot"));
284 &set_label("little");
285 &mov (&BP(0,"edx"),"al");
287 &lea ("edx",&DWP(1,"edx"));
288 &jnz (&label("little"));
292 &set_label("lot",16);
294 &jz (&label("aligned"));
295 &mov (&BP(0,"edx"),"al");
296 &lea ("ecx",&DWP(-1,"ecx"));
297 &lea ("edx",&DWP(1,"edx"));
298 &jmp (&label("lot"));
299 &set_label("aligned");
300 &mov (&DWP(0,"edx"),"eax");
301 &lea ("ecx",&DWP(-4,"ecx"));
303 &lea ("edx",&DWP(4,"edx"));
304 &jnz (&label("aligned"));
306 &jne (&label("little"));
308 &function_end_B("OPENSSL_cleanse");
310 &initseg("OPENSSL_cpuid_setup");