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"
27 &data_byte(0x0f,0x95,0xc0); #&setne (&LB("eax"));
29 &cmp ("edx",0x49656e69); # "ineI"
30 &data_byte(0x0f,0x95,0xc0); #&setne (&LB("eax"));
32 &cmp ("ecx",0x6c65746e); # "ntel"
33 &data_byte(0x0f,0x95,0xc0); #&setne (&LB("eax"));
39 &jne (&label("notP4"));
40 &and ("eax",15<<8); # familiy ID
41 &cmp ("eax",15<<8); # P4?
42 &jne (&label("notP4"));
43 &or ("edx",1<<20); # use reserved bit to engage RC4_CHAR
45 &bt ("edx",28); # test hyper-threading bit
46 &jnc (&label("done"));
49 &cmp ("ebx",1); # see if cache is shared(*)
51 &and ("edx",0xefffffff); # clear hyper-threading bit if not
55 &function_end("OPENSSL_ia32_cpuid");
56 # (*) on Core2 this value is set to 2 denoting the fact that L2
57 # cache is shared between cores.
59 &external_label("OPENSSL_ia32cap_P");
61 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
64 &picmeup("ecx","OPENSSL_ia32cap_P");
65 &bt (&DWP(0,"ecx"),4);
66 &jnc (&label("notsc"));
70 &function_end_B("OPENSSL_rdtsc");
72 # This works in Ring 0 only [read DJGPP+MS-DOS+privileged DPMI host],
73 # but it's safe to call it on any [supported] 32-bit platform...
74 # Just check for [non-]zero return value...
75 &function_begin_B("OPENSSL_instrument_halt","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
76 &picmeup("ecx","OPENSSL_ia32cap_P");
77 &bt (&DWP(0,"ecx"),4);
78 &jnc (&label("nohalt")); # no TSC
80 &data_word(0x9058900e); # push %cs; pop %eax
82 &jnz (&label("nohalt")); # not enough privileges
87 &jnc (&label("nohalt")); # interrupts are disabled
95 &sub ("eax",&DWP(0,"esp"));
96 &sbb ("edx",&DWP(4,"esp"));
100 &set_label("nohalt");
104 &function_end_B("OPENSSL_instrument_halt");
106 # Essentially there is only one use for this function. Under DJGPP:
110 # i=OPENSSL_far_spin(_dos_ds,0x46c);
112 # to obtain the number of spins till closest timer interrupt.
114 &function_begin_B("OPENSSL_far_spin");
118 &jnc (&label("nospin")); # interrupts are disabled
120 &mov ("eax",&DWP(4,"esp"));
121 &mov ("ecx",&DWP(8,"esp"));
122 &data_word (0x90d88e1e); # push %ds, mov %eax,%ds
124 &mov ("edx",&DWP(0,"ecx"));
125 &jmp (&label("spin"));
130 &cmp ("edx",&DWP(0,"ecx"));
131 &je (&label("spin"));
133 &data_word (0x1f909090); # pop %ds
136 &set_label("nospin");
140 &function_end_B("OPENSSL_far_spin");
142 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
145 &picmeup("ecx","OPENSSL_ia32cap_P");
146 &mov ("ecx",&DWP(0,"ecx"));
147 &bt (&DWP(0,"ecx"),1);
148 &jnc (&label("no_x87"));
150 &bt (&DWP(0,"ecx"),26);
151 &jnc (&label("no_sse2"));
152 &pxor ("xmm0","xmm0");
153 &pxor ("xmm1","xmm1");
154 &pxor ("xmm2","xmm2");
155 &pxor ("xmm3","xmm3");
156 &pxor ("xmm4","xmm4");
157 &pxor ("xmm5","xmm5");
158 &pxor ("xmm6","xmm6");
159 &pxor ("xmm7","xmm7");
160 &set_label("no_sse2");
162 # just a bunch of fldz to zap the fp/mm bank followed by finit...
163 &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b);
164 &set_label("no_x87");
165 &lea ("eax",&DWP(4,"esp"));
167 &function_end_B("OPENSSL_wipe_cpu");
169 &function_begin_B("OPENSSL_atomic_add");
170 &mov ("edx",&DWP(4,"esp")); # fetch the pointer, 1st arg
171 &mov ("ecx",&DWP(8,"esp")); # fetch the increment, 2nd arg
174 &mov ("eax",&DWP(0,"edx"));
176 &lea ("ebx",&DWP(0,"eax","ecx"));
178 &data_word(0x1ab10ff0); # lock; cmpxchg %ebx,(%edx) # %eax is envolved and is always reloaded
179 &jne (&label("spin"));
180 &mov ("eax","ebx"); # OpenSSL expects the new value
183 &function_end_B("OPENSSL_atomic_add");
185 # This function can become handy under Win32 in situations when
186 # we don't know which calling convention, __stdcall or __cdecl(*),
187 # indirect callee is using. In C it can be deployed as
189 #ifdef OPENSSL_CPUID_OBJ
190 # type OPENSSL_indirect_call(void *f,...);
192 # OPENSSL_indirect_call(func,[up to $max arguments]);
195 # (*) it's designed to work even for __fastcall if number of
196 # arguments is 1 or 2!
197 &function_begin_B("OPENSSL_indirect_call");
199 my $i,$max=7; # $max has to be chosen as 4*n-1
200 # in order to preserve eventual
205 &mov ("ecx",&DWP(12,"ebp"));
206 &mov (&DWP(0,"esp"),"ecx");
207 &mov ("edx",&DWP(16,"ebp"));
208 &mov (&DWP(4,"esp"),"edx");
209 for($i=2;$i<$max;$i++)
211 # Some copies will be redundant/bogus...
212 &mov ("eax",&DWP(12+$i*4,"ebp"));
213 &mov (&DWP(0+$i*4,"esp"),"eax");
215 &call_ptr (&DWP(8,"ebp"));# make the call...
216 &mov ("esp","ebp"); # ... and just restore the stack pointer
217 # without paying attention to what we called,
218 # (__cdecl *func) or (__stdcall *one).
222 &function_end_B("OPENSSL_indirect_call");
224 &initseg("OPENSSL_cpuid_setup");