x86gas.pl update.
[openssl.git] / crypto / x86cpuid.pl
1 #!/usr/bin/env perl
2
3 push(@INC,"perlasm");
4 require "x86asm.pl";
5
6 &asm_init($ARGV[0],"x86cpuid");
7
8 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
9
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);
22         &jnc    (&label("done"));
23         &xor    ("eax","eax");
24         &cpuid  ();
25         &xor    ("eax","eax");
26         &cmp    ("ebx",0x756e6547);     # "Genu"
27         &setne  (&LB("eax"));
28         &mov    ("ebp","eax");
29         &cmp    ("edx",0x49656e69);     # "ineI"
30         &setne  (&LB("eax"));
31         &or     ("ebp","eax");
32         &cmp    ("ecx",0x6c65746e);     # "ntel"
33         &setne  (&LB("eax"));
34         &or     ("ebp","eax");
35         &mov    ("eax",1);
36         &cpuid  ();
37         &cmp    ("ebp",0);
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
43 &set_label("notP4");
44         &bt     ("edx",28);             # test hyper-threading bit
45         &jnc    (&label("done"));
46         &shr    ("ebx",16);
47         &cmp    (&LB("ebx"),1);         # see if cache is shared(*)
48         &ja     (&label("done"));
49         &and    ("edx",0xefffffff);     # clear hyper-threading bit if not
50 &set_label("done");
51         &mov    ("eax","edx");
52         &mov    ("edx","ecx");
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.
56
57 &external_label("OPENSSL_ia32cap_P");
58
59 &function_begin_B("OPENSSL_rdtsc","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
60         &xor    ("eax","eax");
61         &xor    ("edx","edx");
62         &picmeup("ecx","OPENSSL_ia32cap_P");
63         &bt     (&DWP(0,"ecx"),4);
64         &jnc    (&label("notsc"));
65         &rdtsc  ();
66 &set_label("notsc");
67         &ret    ();
68 &function_end_B("OPENSSL_rdtsc");
69
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
77
78         &data_word(0x9058900e);         # push %cs; pop %eax
79         &and    ("eax",3);
80         &jnz    (&label("nohalt"));     # not enough privileges
81
82         &pushf  ();
83         &pop    ("eax")
84         &bt     ("eax",9);
85         &jnc    (&label("nohalt"));     # interrupts are disabled
86
87         &rdtsc  ();
88         &push   ("edx");
89         &push   ("eax");
90         &halt   ();
91         &rdtsc  ();
92
93         &sub    ("eax",&DWP(0,"esp"));
94         &sbb    ("edx",&DWP(4,"esp"));
95         &add    ("esp",8);
96         &ret    ();
97
98 &set_label("nohalt");
99         &xor    ("eax","eax");
100         &xor    ("edx","edx");
101         &ret    ();
102 &function_end_B("OPENSSL_instrument_halt");
103
104 # Essentially there is only one use for this function. Under DJGPP:
105 #
106 #       #include <go32.h>
107 #       ...
108 #       i=OPENSSL_far_spin(_dos_ds,0x46c);
109 #       ...
110 # to obtain the number of spins till closest timer interrupt.
111
112 &function_begin_B("OPENSSL_far_spin");
113         &pushf  ();
114         &pop    ("eax")
115         &bt     ("eax",9);
116         &jnc    (&label("nospin"));     # interrupts are disabled
117
118         &mov    ("eax",&DWP(4,"esp"));
119         &mov    ("ecx",&DWP(8,"esp"));
120         &data_word (0x90d88e1e);        # push %ds, mov %eax,%ds
121         &xor    ("eax","eax");
122         &mov    ("edx",&DWP(0,"ecx"));
123         &jmp    (&label("spin"));
124
125         &align  (16);
126 &set_label("spin");
127         &inc    ("eax");
128         &cmp    ("edx",&DWP(0,"ecx"));
129         &je     (&label("spin"));
130
131         &data_word (0x1f909090);        # pop   %ds
132         &ret    ();
133
134 &set_label("nospin");
135         &xor    ("eax","eax");
136         &xor    ("edx","edx");
137         &ret    ();
138 &function_end_B("OPENSSL_far_spin");
139
140 &function_begin_B("OPENSSL_wipe_cpu","EXTRN\t_OPENSSL_ia32cap_P:DWORD");
141         &xor    ("eax","eax");
142         &xor    ("edx","edx");
143         &picmeup("ecx","OPENSSL_ia32cap_P");
144         &mov    ("ecx",&DWP(0,"ecx"));
145         &bt     (&DWP(0,"ecx"),1);
146         &jnc    (&label("no_x87"));
147         if ($sse2) {
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");
159         }
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"));
164         &ret    ();
165 &function_end_B("OPENSSL_wipe_cpu");
166
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
170         &push   ("ebx");
171         &nop    ();
172         &mov    ("eax",&DWP(0,"edx"));
173 &set_label("spin");
174         &lea    ("ebx",&DWP(0,"eax","ecx"));
175         &nop    ();
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
179         &pop    ("ebx");
180         &ret    ();
181 &function_end_B("OPENSSL_atomic_add");
182
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
186 #
187 #ifdef OPENSSL_CPUID_OBJ
188 #       type OPENSSL_indirect_call(void *f,...);
189 #       ...
190 #       OPENSSL_indirect_call(func,[up to $max arguments]);
191 #endif
192 #
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");
196         {
197         my $i,$max=7;           # $max has to be chosen as 4*n-1
198                                 # in order to preserve eventual
199                                 # stack alignment
200         &push   ("ebp");
201         &mov    ("ebp","esp");
202         &sub    ("esp",$max*4);
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++)
208                 {
209                 # Some copies will be redundant/bogus...
210                 &mov    ("eax",&DWP(12+$i*4,"ebp"));
211                 &mov    (&DWP(0+$i*4,"esp"),"eax");
212                 }
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).
217         &pop    ("ebp");
218         &ret    ();
219         }
220 &function_end_B("OPENSSL_indirect_call");
221
222 &function_begin_B("OPENSSL_cleanse");
223         &mov    ("edx",&wparam(0));
224         &mov    ("ecx",&wparam(1));
225         &xor    ("eax","eax");
226         &cmp    ("ecx",7);
227         &jae    (&label("lot"));
228 &set_label("little");
229         &mov    (&BP(0,"edx"),"al");
230         &sub    ("ecx",1);
231         &lea    ("edx",&DWP(1,"edx"));
232         &jnz    (&label("little"));
233         &ret    ();
234
235 &set_label("lot",16);
236         &test   ("edx",3);
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"));
245         &test   ("ecx",-4);
246         &lea    ("edx",&DWP(4,"edx"));
247         &jnz    (&label("aligned"));
248         &cmp    ("ecx",0);
249         &jne    (&label("little"));
250         &ret    ();
251 &function_end_B("OPENSSL_cleanse");
252
253 &initseg("OPENSSL_cpuid_setup");
254
255 &asm_finish();