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"));
27 &cmp ("ebx",0x756e6547); # "Genu"
30 &cmp ("edx",0x49656e69); # "ineI"
33 &cmp ("ecx",0x6c65746e); # "ntel"
35 &or ("ebp","eax"); # 0 indicates Intel CPU
36 &mov ("esi",1); # "number of [AMD] cores"
37 &jz (&label("intel"));
39 &cmp ("ebx",0x68747541); # "Auth"
42 &cmp ("edx",0x69746E65); # "enti"
45 &cmp ("ecx",0x444D4163); # "cAMD"
47 &or ("esi","eax"); # 0 indicates AMD CPU
48 &jnz (&label("intel"));
50 &mov ("eax",0x80000000);
52 &cmp ("eax",0x80000008);
53 &mov ("esi",1); # "number of [AMD] cores"
54 &jb (&label("intel"));
56 &mov ("eax",0x80000008);
58 &movz ("esi",&LB("ecx")); # number of cores - 1
59 &inc ("esi"); # number of cores
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
71 &bt ("edx",28); # test hyper-threading bit
72 &jnc (&label("done"));
75 &cmp ("ebx","esi"); # see if cache is shared(*)
77 &and ("edx",0xefffffff); # clear hyper-threading bit if not
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.
85 &external_label("OPENSSL_ia32cap_P");
87 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
90 &picmeup("ecx","OPENSSL_ia32cap_P");
91 &bt (&DWP(0,"ecx"),4);
92 &jnc (&label("notsc"));
96 &function_end_B("OPENSSL_rdtsc");
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
106 &data_word(0x9058900e); # push %cs; pop %eax
108 &jnz (&label("nohalt")); # not enough privileges
113 &jnc (&label("nohalt")); # interrupts are disabled
121 &sub ("eax",&DWP(0,"esp"));
122 &sbb ("edx",&DWP(4,"esp"));
126 &set_label("nohalt");
130 &function_end_B("OPENSSL_instrument_halt");
132 # Essentially there is only one use for this function. Under DJGPP:
136 # i=OPENSSL_far_spin(_dos_ds,0x46c);
138 # to obtain the number of spins till closest timer interrupt.
140 &function_begin_B("OPENSSL_far_spin");
144 &jnc (&label("nospin")); # interrupts are disabled
146 &mov ("eax",&DWP(4,"esp"));
147 &mov ("ecx",&DWP(8,"esp"));
148 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
150 &mov ("edx",&DWP(0,"ecx"));
151 &jmp (&label("spin"));
156 &cmp ("edx",&DWP(0,"ecx"));
157 &je (&label("spin"));
159 &data_word (0x1f909090); # pop %ds
162 &set_label("nospin");
166 &function_end_B("OPENSSL_far_spin");
168 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
171 &picmeup("ecx","OPENSSL_ia32cap_P");
172 &mov ("ecx",&DWP(0,"ecx"));
173 &bt (&DWP(0,"ecx"),1);
174 &jnc (&label("no_x87"));
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");
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"));
193 &function_end_B("OPENSSL_wipe_cpu");
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
200 &mov ("eax",&DWP(0,"edx"));
202 &lea ("ebx",&DWP(0,"eax","ecx"));
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
209 &function_end_B("OPENSSL_atomic_add");
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
215 #ifdef OPENSSL_CPUID_OBJ
216 # type OPENSSL_indirect_call(void *f,...);
218 # OPENSSL_indirect_call(func,[up to $max arguments]);
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");
225 my $i,$max=7; # $max has to be chosen as 4*n-1
226 # in order to preserve eventual
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++)
237 # Some copies will be redundant/bogus...
238 &mov ("eax",&DWP(12+$i*4,"ebp"));
239 &mov (&DWP(0+$i*4,"esp"),"eax");
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).
248 &function_end_B("OPENSSL_indirect_call");
250 &function_begin_B("OPENSSL_cleanse");
251 &mov ("edx",&wparam(0));
252 &mov ("ecx",&wparam(1));
255 &jae (&label("lot"));
256 &set_label("little");
257 &mov (&BP(0,"edx"),"al");
259 &lea ("edx",&DWP(1,"edx"));
260 &jnz (&label("little"));
263 &set_label("lot",16);
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"));
274 &lea ("edx",&DWP(4,"edx"));
275 &jnz (&label("aligned"));
277 &jne (&label("little"));
279 &function_end_B("OPENSSL_cleanse");
281 &initseg("OPENSSL_cpuid_setup");