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"));
84 &jb
(&label
("nocacheinfo"));
87 &mov
("ecx",0); # query L1D
91 &and ("edi",0xfff); # number of cores -1 per L1D
93 &set_label
("nocacheinfo");
97 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
99 &jne
(&label
("notintel"));
100 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
101 &and (&HB
("eax"),15); # familiy ID
102 &cmp (&HB
("eax"),15); # P4?
103 &jne
(&label
("notintel"));
104 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
105 &set_label
("notintel");
106 &bt
("edx",28); # test hyper-threading bit
107 &jnc
(&label
("generic"));
108 &and ("edx",0xefffffff);
110 &je
(&label
("generic"));
112 &or ("edx",0x10000000);
115 &ja
(&label
("generic"));
116 &and ("edx",0xefffffff); # clear hyper-threading bit if not
118 &set_label
("generic");
119 &and ("ebp",1<<11); # isolate AMD XOP flag
120 &and ("ecx",0xfffff7ff); # force 11th bit to 0
122 &or ("ebp","ecx"); # merge AMD XOP flag
124 &bt
("ecx",27); # check OSXSAVE bit
125 &jnc
(&label
("clear_avx"));
127 &data_byte
(0x0f,0x01,0xd0); # xgetbv
130 &je
(&label
("done"));
132 &je
(&label
("clear_avx"));
133 &set_label
("clear_xmm");
134 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
135 &and ("esi",0xfeffffff); # clear FXSR
136 &set_label
("clear_avx");
137 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
141 &set_label
("nocpuid");
142 &function_end
("OPENSSL_ia32_cpuid");
144 &external_label
("OPENSSL_ia32cap_P");
146 &function_begin_B
("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
149 &picmeup
("ecx","OPENSSL_ia32cap_P");
150 &bt
(&DWP
(0,"ecx"),4);
151 &jnc
(&label
("notsc"));
155 &function_end_B
("OPENSSL_rdtsc");
157 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
158 # but it's safe to call it on any [supported] 32-bit platform...
159 # Just check for [non-]zero return value...
160 &function_begin_B
("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
161 &picmeup
("ecx","OPENSSL_ia32cap_P");
162 &bt
(&DWP
(0,"ecx"),4);
163 &jnc
(&label
("nohalt")); # no TSC
165 &data_word
(0x9058900e); # push %cs; pop %eax
167 &jnz
(&label
("nohalt")); # not enough privileges
172 &jnc
(&label
("nohalt")); # interrupts are disabled
180 &sub ("eax",&DWP
(0,"esp"));
181 &sbb
("edx",&DWP
(4,"esp"));
185 &set_label
("nohalt");
189 &function_end_B
("OPENSSL_instrument_halt");
191 # Essentially there is only one use for this function. Under DJGPP:
195 # i=OPENSSL_far_spin(_dos_ds,0x46c);
197 # to obtain the number of spins till closest timer interrupt.
199 &function_begin_B
("OPENSSL_far_spin");
203 &jnc
(&label
("nospin")); # interrupts are disabled
205 &mov
("eax",&DWP
(4,"esp"));
206 &mov
("ecx",&DWP
(8,"esp"));
207 &data_word
(0x90d88e1e); # push %ds, mov %eax,%ds
209 &mov
("edx",&DWP
(0,"ecx"));
210 &jmp
(&label
("spin"));
215 &cmp ("edx",&DWP
(0,"ecx"));
216 &je
(&label
("spin"));
218 &data_word
(0x1f909090); # pop %ds
221 &set_label
("nospin");
225 &function_end_B
("OPENSSL_far_spin");
227 &function_begin_B
("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
230 &picmeup
("ecx","OPENSSL_ia32cap_P");
231 &mov
("ecx",&DWP
(0,"ecx"));
232 &bt
(&DWP
(0,"ecx"),1);
233 &jnc
(&label
("no_x87"));
235 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
236 &cmp ("ecx",1<<26|1<<24);
237 &jne
(&label
("no_sse2"));
238 &pxor
("xmm0","xmm0");
239 &pxor
("xmm1","xmm1");
240 &pxor
("xmm2","xmm2");
241 &pxor
("xmm3","xmm3");
242 &pxor
("xmm4","xmm4");
243 &pxor
("xmm5","xmm5");
244 &pxor
("xmm6","xmm6");
245 &pxor
("xmm7","xmm7");
246 &set_label
("no_sse2");
248 # just a bunch of fldz to zap the fp/mm bank followed by finit...
249 &data_word
(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
250 &set_label
("no_x87");
251 &lea
("eax",&DWP
(4,"esp"));
253 &function_end_B
("OPENSSL_wipe_cpu");
255 &function_begin_B
("OPENSSL_atomic_add");
256 &mov
("edx",&DWP
(4,"esp")); # fetch the pointer, 1st arg
257 &mov
("ecx",&DWP
(8,"esp")); # fetch the increment, 2nd arg
260 &mov
("eax",&DWP
(0,"edx"));
262 &lea
("ebx",&DWP
(0,"eax","ecx"));
264 &data_word
(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
265 &jne
(&label
("spin"));
266 &mov
("eax","ebx"); # OpenSSL expects the new value
269 &function_end_B
("OPENSSL_atomic_add");
271 # This function can become handy under Win32 in situations when
272 # we don't know which calling convention, __stdcall or __cdecl(*),
273 # indirect callee is using. In C it can be deployed as
275 #ifdef OPENSSL_CPUID_OBJ
276 # type OPENSSL_indirect_call(void *f,...);
278 # OPENSSL_indirect_call(func,[up to $max arguments]);
281 # (*) it's designed to work even for __fastcall if number of
282 # arguments is 1 or 2!
283 &function_begin_B
("OPENSSL_indirect_call");
285 my ($max,$i)=(7,); # $max has to be chosen as 4*n-1
286 # in order to preserve eventual
291 &mov
("ecx",&DWP
(12,"ebp"));
292 &mov
(&DWP
(0,"esp"),"ecx");
293 &mov
("edx",&DWP
(16,"ebp"));
294 &mov
(&DWP
(4,"esp"),"edx");
295 for($i=2;$i<$max;$i++)
297 # Some copies will be redundant/bogus...
298 &mov
("eax",&DWP
(12+$i*4,"ebp"));
299 &mov
(&DWP
(0+$i*4,"esp"),"eax");
301 &call_ptr
(&DWP
(8,"ebp"));# make the call...
302 &mov
("esp","ebp"); # ... and just restore the stack pointer
303 # without paying attention to what we called,
304 # (__cdecl *func) or (__stdcall *one).
308 &function_end_B
("OPENSSL_indirect_call");
310 &function_begin_B
("OPENSSL_cleanse");
311 &mov
("edx",&wparam
(0));
312 &mov
("ecx",&wparam
(1));
315 &jae
(&label
("lot"));
318 &set_label
("little");
319 &mov
(&BP
(0,"edx"),"al");
321 &lea
("edx",&DWP
(1,"edx"));
322 &jnz
(&label
("little"));
326 &set_label
("lot",16);
328 &jz
(&label
("aligned"));
329 &mov
(&BP
(0,"edx"),"al");
330 &lea
("ecx",&DWP
(-1,"ecx"));
331 &lea
("edx",&DWP
(1,"edx"));
332 &jmp
(&label
("lot"));
333 &set_label
("aligned");
334 &mov
(&DWP
(0,"edx"),"eax");
335 &lea
("ecx",&DWP
(-4,"ecx"));
337 &lea
("edx",&DWP
(4,"edx"));
338 &jnz
(&label
("aligned"));
340 &jne
(&label
("little"));
342 &function_end_B
("OPENSSL_cleanse");
344 &function_begin_B
("OPENSSL_ia32_rdrand");
348 &jc
(&label
("break"));
349 &loop (&label
("loop"));
352 &cmove
("eax","ecx");
354 &function_end_B
("OPENSSL_ia32_rdrand");
356 &initseg
("OPENSSL_cpuid_setup");