6 &asm_init($ARGV[0],"x86cpuid");
8 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
10 &function_begin("OPENSSL_ia32_cpuid");
22 &jnc (&label("done"));
26 &cmp ("ebx",0x756e6547); # "Genu"
29 &cmp ("edx",0x49656e69); # "ineI"
32 &cmp ("ecx",0x6c65746e); # "ntel"
38 &jne (&label("notP4"));
39 &and (&HB("eax"),15); # familiy ID
40 &cmp (&HB("eax"),15); # P4?
41 &jne (&label("notP4"));
42 &or ("edx",1<<20); # use reserved bit to engage RC4_CHAR
44 &bt ("edx",28); # test hyper-threading bit
45 &jnc (&label("done"));
47 &cmp (&LB("ebx"),1); # see if cache is shared(*)
49 &and ("edx",0xefffffff); # clear hyper-threading bit if not
53 &function_end("OPENSSL_ia32_cpuid");
54 # (*) on Core2 this value is set to 2 denoting the fact that L2
55 # cache is shared between cores.
57 &external_label("OPENSSL_ia32cap_P");
59 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
62 &picmeup("ecx","OPENSSL_ia32cap_P");
63 &bt (&DWP(0,"ecx"),4);
64 &jnc (&label("notsc"));
68 &function_end_B("OPENSSL_rdtsc");
70 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
71 # but it's safe to call it on any [supported] 32-bit platform...
72 # Just check for [non-]zero return value...
73 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
74 &picmeup("ecx","OPENSSL_ia32cap_P");
75 &bt (&DWP(0,"ecx"),4);
76 &jnc (&label("nohalt")); # no TSC
78 &data_word(0x9058900e); # push %cs; pop %eax
80 &jnz (&label("nohalt")); # not enough privileges
85 &jnc (&label("nohalt")); # interrupts are disabled
93 &sub ("eax",&DWP(0,"esp"));
94 &sbb ("edx",&DWP(4,"esp"));
102 &function_end_B("OPENSSL_instrument_halt");
104 # Essentially there is only one use for this function. Under DJGPP:
108 # i=OPENSSL_far_spin(_dos_ds,0x46c);
110 # to obtain the number of spins till closest timer interrupt.
112 &function_begin_B("OPENSSL_far_spin");
116 &jnc (&label("nospin")); # interrupts are disabled
118 &mov ("eax",&DWP(4,"esp"));
119 &mov ("ecx",&DWP(8,"esp"));
120 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
122 &mov ("edx",&DWP(0,"ecx"));
123 &jmp (&label("spin"));
128 &cmp ("edx",&DWP(0,"ecx"));
129 &je (&label("spin"));
131 &data_word (0x1f909090); # pop %ds
134 &set_label("nospin");
138 &function_end_B("OPENSSL_far_spin");
140 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
143 &picmeup("ecx","OPENSSL_ia32cap_P");
144 &mov ("ecx",&DWP(0,"ecx"));
145 &bt (&DWP(0,"ecx"),1);
146 &jnc (&label("no_x87"));
148 &bt (&DWP(0,"ecx"),26);
149 &jnc (&label("no_sse2"));
150 &pxor ("xmm0","xmm0");
151 &pxor ("xmm1","xmm1");
152 &pxor ("xmm2","xmm2");
153 &pxor ("xmm3","xmm3");
154 &pxor ("xmm4","xmm4");
155 &pxor ("xmm5","xmm5");
156 &pxor ("xmm6","xmm6");
157 &pxor ("xmm7","xmm7");
158 &set_label("no_sse2");
160 # just a bunch of fldz to zap the fp/mm bank followed by finit...
161 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
162 &set_label("no_x87");
163 &lea ("eax",&DWP(4,"esp"));
165 &function_end_B("OPENSSL_wipe_cpu");
167 &function_begin_B("OPENSSL_atomic_add");
168 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
169 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
172 &mov ("eax",&DWP(0,"edx"));
174 &lea ("ebx",&DWP(0,"eax","ecx"));
176 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
177 &jne (&label("spin"));
178 &mov ("eax","ebx"); # OpenSSL expects the new value
181 &function_end_B("OPENSSL_atomic_add");
183 # This function can become handy under Win32 in situations when
184 # we don't know which calling convention, __stdcall or __cdecl(*),
185 # indirect callee is using. In C it can be deployed as
187 #ifdef OPENSSL_CPUID_OBJ
188 # type OPENSSL_indirect_call(void *f,...);
190 # OPENSSL_indirect_call(func,[up to $max arguments]);
193 # (*) it's designed to work even for __fastcall if number of
194 # arguments is 1 or 2!
195 &function_begin_B("OPENSSL_indirect_call");
197 my $i,$max=7; # $max has to be chosen as 4*n-1
198 # in order to preserve eventual
203 &mov ("ecx",&DWP(12,"ebp"));
204 &mov (&DWP(0,"esp"),"ecx");
205 &mov ("edx",&DWP(16,"ebp"));
206 &mov (&DWP(4,"esp"),"edx");
207 for($i=2;$i<$max;$i++)
209 # Some copies will be redundant/bogus...
210 &mov ("eax",&DWP(12+$i*4,"ebp"));
211 &mov (&DWP(0+$i*4,"esp"),"eax");
213 &call_ptr (&DWP(8,"ebp"));# make the call...
214 &mov ("esp","ebp"); # ... and just restore the stack pointer
215 # without paying attention to what we called,
216 # (__cdecl *func) or (__stdcall *one).
220 &function_end_B("OPENSSL_indirect_call");
222 &function_begin_B("OPENSSL_cleanse");
223 &mov ("edx",&wparam(0));
224 &mov ("ecx",&wparam(1));
227 &jae (&label("lot"));
228 &set_label("little");
229 &mov (&BP(0,"edx"),"al");
231 &lea ("edx",&DWP(1,"edx"));
232 &jnz (&label("little"));
235 &set_label("lot",16);
237 &jz (&label("aligned"));
238 &mov (&BP(0,"edx"),"al");
239 &lea ("ecx",&DWP(-1,"ecx"));
240 &lea ("edx",&DWP(1,"edx"));
241 &jmp (&label("lot"));
242 &set_label("aligned");
243 &mov (&DWP(0,"edx"),"eax");
244 &lea ("ecx",&DWP(-4,"ecx"));
246 &lea ("edx",&DWP(4,"edx"));
247 &jnz (&label("aligned"));
249 &jne (&label("little"));
251 &function_end_B("OPENSSL_cleanse");
253 &initseg("OPENSSL_cpuid_setup");