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
72 &jnc
(&label
("generic"));
76 &ja
(&label
("generic"));
77 &and ("edx",0xefffffff); # clear hyper-threading bit
78 &jmp
(&label
("generic"));
83 &jb
(&label
("nocacheinfo"));
86 &mov
("ecx",0); # query L1D
90 &and ("edi",0xfff); # number of cores -1 per L1D
92 &set_label
("nocacheinfo");
95 &and ("edx",0xbfefffff); # force reserved bits #20, #30 to 0
97 &jne
(&label
("notintel"));
98 &or ("edx",1<<30); # set reserved bit#30 on Intel CPUs
99 &and (&HB
("eax"),15); # familiy ID
100 &cmp (&HB
("eax"),15); # P4?
101 &jne
(&label
("notintel"));
102 &or ("edx",1<<20); # set reserved bit#20 to engage RC4_CHAR
103 &set_label
("notintel");
104 &bt
("edx",28); # test hyper-threading bit
105 &jnc
(&label
("generic"));
106 &and ("edx",0xefffffff);
108 &je
(&label
("generic"));
110 &or ("edx",0x10000000);
113 &ja
(&label
("generic"));
114 &and ("edx",0xefffffff); # clear hyper-threading bit if not
116 &set_label
("generic");
117 &and ("ebp",1<<11); # isolate AMD XOP flag
118 &and ("ecx",0xfffff7ff); # force 11th bit to 0
120 &or ("ebp","ecx"); # merge AMD XOP flag
122 &bt
("ecx",27); # check OSXSAVE bit
123 &jnc
(&label
("clear_avx"));
125 &data_byte
(0x0f,0x01,0xd0); # xgetbv
128 &je
(&label
("done"));
130 &je
(&label
("clear_avx"));
131 &set_label
("clear_xmm");
132 &and ("ebp",0xfdfffffd); # clear AESNI and PCLMULQDQ bits
133 &and ("esi",0xfeffffff); # clear FXSR
134 &set_label
("clear_avx");
135 &and ("ebp",0xefffe7ff); # clear AVX, FMA and AMD XOP bits
139 &set_label
("nocpuid");
140 &function_end
("OPENSSL_ia32_cpuid");
142 &external_label
("OPENSSL_ia32cap_P");
144 &function_begin_B
("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
147 &picmeup
("ecx","OPENSSL_ia32cap_P");
148 &bt
(&DWP
(0,"ecx"),4);
149 &jnc
(&label
("notsc"));
153 &function_end_B
("OPENSSL_rdtsc");
155 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
156 # but it's safe to call it on any [supported] 32-bit platform...
157 # Just check for [non-]zero return value...
158 &function_begin_B
("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
159 &picmeup
("ecx","OPENSSL_ia32cap_P");
160 &bt
(&DWP
(0,"ecx"),4);
161 &jnc
(&label
("nohalt")); # no TSC
163 &data_word
(0x9058900e); # push %cs; pop %eax
165 &jnz
(&label
("nohalt")); # not enough privileges
170 &jnc
(&label
("nohalt")); # interrupts are disabled
178 &sub ("eax",&DWP
(0,"esp"));
179 &sbb
("edx",&DWP
(4,"esp"));
183 &set_label
("nohalt");
187 &function_end_B
("OPENSSL_instrument_halt");
189 # Essentially there is only one use for this function. Under DJGPP:
193 # i=OPENSSL_far_spin(_dos_ds,0x46c);
195 # to obtain the number of spins till closest timer interrupt.
197 &function_begin_B
("OPENSSL_far_spin");
201 &jnc
(&label
("nospin")); # interrupts are disabled
203 &mov
("eax",&DWP
(4,"esp"));
204 &mov
("ecx",&DWP
(8,"esp"));
205 &data_word
(0x90d88e1e); # push %ds, mov %eax,%ds
207 &mov
("edx",&DWP
(0,"ecx"));
208 &jmp
(&label
("spin"));
213 &cmp ("edx",&DWP
(0,"ecx"));
214 &je
(&label
("spin"));
216 &data_word
(0x1f909090); # pop %ds
219 &set_label
("nospin");
223 &function_end_B
("OPENSSL_far_spin");
225 &function_begin_B
("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
228 &picmeup
("ecx","OPENSSL_ia32cap_P");
229 &mov
("ecx",&DWP
(0,"ecx"));
230 &bt
(&DWP
(0,"ecx"),1);
231 &jnc
(&label
("no_x87"));
233 &and ("ecx",1<<26|1<<24); # check SSE2 and FXSR bits
234 &cmp ("ecx",1<<26|1<<24);
235 &jne
(&label
("no_sse2"));
236 &pxor
("xmm0","xmm0");
237 &pxor
("xmm1","xmm1");
238 &pxor
("xmm2","xmm2");
239 &pxor
("xmm3","xmm3");
240 &pxor
("xmm4","xmm4");
241 &pxor
("xmm5","xmm5");
242 &pxor
("xmm6","xmm6");
243 &pxor
("xmm7","xmm7");
244 &set_label
("no_sse2");
246 # just a bunch of fldz to zap the fp/mm bank followed by finit...
247 &data_word
(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
248 &set_label
("no_x87");
249 &lea
("eax",&DWP
(4,"esp"));
251 &function_end_B
("OPENSSL_wipe_cpu");
253 &function_begin_B
("OPENSSL_atomic_add");
254 &mov
("edx",&DWP
(4,"esp")); # fetch the pointer, 1st arg
255 &mov
("ecx",&DWP
(8,"esp")); # fetch the increment, 2nd arg
258 &mov
("eax",&DWP
(0,"edx"));
260 &lea
("ebx",&DWP
(0,"eax","ecx"));
262 &data_word
(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
263 &jne
(&label
("spin"));
264 &mov
("eax","ebx"); # OpenSSL expects the new value
267 &function_end_B
("OPENSSL_atomic_add");
269 # This function can become handy under Win32 in situations when
270 # we don't know which calling convention, __stdcall or __cdecl(*),
271 # indirect callee is using. In C it can be deployed as
273 #ifdef OPENSSL_CPUID_OBJ
274 # type OPENSSL_indirect_call(void *f,...);
276 # OPENSSL_indirect_call(func,[up to $max arguments]);
279 # (*) it's designed to work even for __fastcall if number of
280 # arguments is 1 or 2!
281 &function_begin_B
("OPENSSL_indirect_call");
283 my $i,$max=7; # $max has to be chosen as 4*n-1
284 # in order to preserve eventual
289 &mov
("ecx",&DWP
(12,"ebp"));
290 &mov
(&DWP
(0,"esp"),"ecx");
291 &mov
("edx",&DWP
(16,"ebp"));
292 &mov
(&DWP
(4,"esp"),"edx");
293 for($i=2;$i<$max;$i++)
295 # Some copies will be redundant/bogus...
296 &mov
("eax",&DWP
(12+$i*4,"ebp"));
297 &mov
(&DWP
(0+$i*4,"esp"),"eax");
299 &call_ptr
(&DWP
(8,"ebp"));# make the call...
300 &mov
("esp","ebp"); # ... and just restore the stack pointer
301 # without paying attention to what we called,
302 # (__cdecl *func) or (__stdcall *one).
306 &function_end_B
("OPENSSL_indirect_call");
308 &function_begin_B
("OPENSSL_cleanse");
309 &mov
("edx",&wparam
(0));
310 &mov
("ecx",&wparam
(1));
313 &jae
(&label
("lot"));
316 &set_label
("little");
317 &mov
(&BP
(0,"edx"),"al");
319 &lea
("edx",&DWP
(1,"edx"));
320 &jnz
(&label
("little"));
324 &set_label
("lot",16);
326 &jz
(&label
("aligned"));
327 &mov
(&BP
(0,"edx"),"al");
328 &lea
("ecx",&DWP
(-1,"ecx"));
329 &lea
("edx",&DWP
(1,"edx"));
330 &jmp
(&label
("lot"));
331 &set_label
("aligned");
332 &mov
(&DWP
(0,"edx"),"eax");
333 &lea
("ecx",&DWP
(-4,"ecx"));
335 &lea
("edx",&DWP
(4,"edx"));
336 &jnz
(&label
("aligned"));
338 &jne
(&label
("little"));
340 &function_end_B
("OPENSSL_cleanse");
342 &function_begin_B
("OPENSSL_ia32_rdrand");
346 &jc
(&label
("break"));
347 &loop (&label
("loop"));
350 &cmove
("eax","ecx");
352 &function_end_B
("OPENSSL_ia32_rdrand");
354 &initseg
("OPENSSL_cpuid_setup");