]>
Commit | Line | Data |
---|---|---|
14e21f86 AP |
1 | #!/usr/bin/env perl |
2 | ||
3 | push(@INC,"perlasm"); | |
4 | require "x86asm.pl"; | |
5 | ||
6 | &asm_init($ARGV[0],"x86cpuid"); | |
7 | ||
ca3e6837 AP |
8 | for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); } |
9 | ||
14e21f86 AP |
10 | &function_begin("OPENSSL_ia32_cpuid"); |
11 | &xor ("edx","edx"); | |
12 | &pushf (); | |
13 | &pop ("eax"); | |
14 | &mov ("ecx","eax"); | |
15 | &xor ("eax",1<<21); | |
16 | &push ("eax"); | |
17 | &popf (); | |
18 | &pushf (); | |
19 | &pop ("eax"); | |
20 | &xor ("ecx","eax"); | |
21 | &bt ("ecx",21); | |
095db720 AP |
22 | &jnc (&label("done")); |
23 | &xor ("eax","eax"); | |
24 | &cpuid (); | |
25 | &xor ("eax","eax"); | |
26 | &cmp ("ebx",0x756e6547); # "Genu" | |
27 | &data_byte(0x0f,0x95,0xc0); #&setne (&LB("eax")); | |
28 | &mov ("ebp","eax"); | |
29 | &cmp ("edx",0x49656e69); # "ineI" | |
30 | &data_byte(0x0f,0x95,0xc0); #&setne (&LB("eax")); | |
31 | &or ("ebp","eax"); | |
32 | &cmp ("ecx",0x6c65746e); # "ntel" | |
33 | &data_byte(0x0f,0x95,0xc0); #&setne (&LB("eax")); | |
34 | &or ("ebp","eax"); | |
14e21f86 | 35 | &mov ("eax",1); |
01de6e21 | 36 | &xor ("ecx","ecx"); |
14e21f86 | 37 | &cpuid (); |
095db720 AP |
38 | &cmp ("ebp",0); |
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 | |
44 | &set_label("notP4"); | |
45 | &bt ("edx",28); # test hyper-threading bit | |
46 | &jnc (&label("done")); | |
47 | &shr ("ebx",16); | |
48 | &and ("ebx",0xff); | |
49 | &cmp ("ebx",1); # see if cache is shared(*) | |
50 | &ja (&label("done")); | |
51 | &and ("edx",0xefffffff); # clear hyper-threading bit if not | |
52 | &set_label("done"); | |
14e21f86 AP |
53 | &mov ("eax","edx"); |
54 | &mov ("edx","ecx"); | |
55 | &function_end("OPENSSL_ia32_cpuid"); | |
095db720 AP |
56 | # (*) on Core2 this value is set to 2 denoting the fact that L2 |
57 | # cache is shared between cores. | |
14e21f86 | 58 | |
2b247cf8 | 59 | &external_label("OPENSSL_ia32cap_P"); |
14e21f86 | 60 | |
2b247cf8 | 61 | &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD"); |
14e21f86 AP |
62 | &xor ("eax","eax"); |
63 | &xor ("edx","edx"); | |
2b247cf8 | 64 | &picmeup("ecx","OPENSSL_ia32cap_P"); |
14e21f86 AP |
65 | &bt (&DWP(0,"ecx"),4); |
66 | &jnc (&label("notsc")); | |
67 | &rdtsc (); | |
68 | &set_label("notsc"); | |
69 | &ret (); | |
70 | &function_end_B("OPENSSL_rdtsc"); | |
71 | ||
c85c5c40 AP |
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 | |
79 | ||
80 | &data_word(0x9058900e); # push %cs; pop %eax | |
81 | &and ("eax",3); | |
82 | &jnz (&label("nohalt")); # not enough privileges | |
83 | ||
84 | &pushf (); | |
85 | &pop ("eax") | |
86 | &bt ("eax",9); | |
87 | &jnc (&label("nohalt")); # interrupts are disabled | |
88 | ||
89 | &rdtsc (); | |
90 | &push ("edx"); | |
91 | &push ("eax"); | |
92 | &halt (); | |
93 | &rdtsc (); | |
94 | ||
95 | &sub ("eax",&DWP(0,"esp")); | |
96 | &sbb ("edx",&DWP(4,"esp")); | |
97 | &add ("esp",8); | |
98 | &ret (); | |
99 | ||
100 | &set_label("nohalt"); | |
101 | &xor ("eax","eax"); | |
102 | &xor ("edx","edx"); | |
103 | &ret (); | |
104 | &function_end_B("OPENSSL_instrument_halt"); | |
105 | ||
cee73df3 AP |
106 | # Essentially there is only one use for this function. Under DJGPP: |
107 | # | |
108 | # #include <go32.h> | |
109 | # ... | |
110 | # i=OPENSSL_far_spin(_dos_ds,0x46c); | |
111 | # ... | |
112 | # to obtain the number of spins till closest timer interrupt. | |
113 | ||
114 | &function_begin_B("OPENSSL_far_spin"); | |
115 | &pushf (); | |
116 | &pop ("eax") | |
117 | &bt ("eax",9); | |
118 | &jnc (&label("nospin")); # interrupts are disabled | |
119 | ||
120 | &mov ("eax",&DWP(4,"esp")); | |
121 | &mov ("ecx",&DWP(8,"esp")); | |
122 | &data_word (0x90d88e1e); # push %ds, mov %eax,%ds | |
123 | &xor ("eax","eax"); | |
124 | &mov ("edx",&DWP(0,"ecx")); | |
125 | &jmp (&label("spin")); | |
126 | ||
127 | &align (16); | |
128 | &set_label("spin"); | |
129 | &inc ("eax"); | |
130 | &cmp ("edx",&DWP(0,"ecx")); | |
131 | &je (&label("spin")); | |
132 | ||
133 | &data_word (0x1f909090); # pop %ds | |
134 | &ret (); | |
135 | ||
136 | &set_label("nospin"); | |
137 | &xor ("eax","eax"); | |
138 | &xor ("edx","edx"); | |
139 | &ret (); | |
140 | &function_end_B("OPENSSL_far_spin"); | |
141 | ||
142 | &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD"); | |
143 | &xor ("eax","eax"); | |
144 | &xor ("edx","edx"); | |
145 | &picmeup("ecx","OPENSSL_ia32cap_P"); | |
146 | &mov ("ecx",&DWP(0,"ecx")); | |
147 | &bt (&DWP(0,"ecx"),1); | |
148 | &jnc (&label("no_x87")); | |
ca3e6837 AP |
149 | if ($sse2) { |
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"); | |
161 | } | |
ff0bfe64 AP |
162 | # just a bunch of fldz to zap the fp/mm bank followed by finit... |
163 | &data_word(0xeed9eed9,0xeed9eed9,0xeed9eed9,0xeed9eed9,0x90e3db9b); | |
cee73df3 AP |
164 | &set_label("no_x87"); |
165 | &lea ("eax",&DWP(4,"esp")); | |
166 | &ret (); | |
167 | &function_end_B("OPENSSL_wipe_cpu"); | |
168 | ||
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 | |
172 | &push ("ebx"); | |
173 | &nop (); | |
174 | &mov ("eax",&DWP(0,"edx")); | |
175 | &set_label("spin"); | |
176 | &lea ("ebx",&DWP(0,"eax","ecx")); | |
177 | &nop (); | |
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 | |
181 | &pop ("ebx"); | |
182 | &ret (); | |
183 | &function_end_B("OPENSSL_atomic_add"); | |
184 | ||
6104c49f AP |
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 | |
188 | # | |
189 | #ifdef OPENSSL_CPUID_OBJ | |
190 | # type OPENSSL_indirect_call(void *f,...); | |
191 | # ... | |
192 | # OPENSSL_indirect_call(func,[up to $max arguments]); | |
193 | #endif | |
194 | # | |
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"); | |
198 | { | |
199 | my $i,$max=7; # $max has to be chosen as 4*n-1 | |
200 | # in order to preserve eventual | |
201 | # stack alignment | |
202 | &push ("ebp"); | |
203 | &mov ("ebp","esp"); | |
204 | &sub ("esp",$max*4); | |
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++) | |
210 | { | |
211 | # Some copies will be redundant/bogus... | |
212 | &mov ("eax",&DWP(12+$i*4,"ebp")); | |
213 | &mov (&DWP(0+$i*4,"esp"),"eax"); | |
214 | } | |
05decf36 | 215 | &call_ptr (&DWP(8,"ebp"));# make the call... |
6104c49f AP |
216 | &mov ("esp","ebp"); # ... and just restore the stack pointer |
217 | # without paying attention to what we called, | |
218 | # (__cdecl *func) or (__stdcall *one). | |
219 | &pop ("ebp"); | |
220 | &ret (); | |
221 | } | |
222 | &function_end_B("OPENSSL_indirect_call"); | |
223 | ||
2b247cf8 | 224 | &initseg("OPENSSL_cpuid_setup"); |
14e21f86 AP |
225 | |
226 | &asm_finish(); |