]>
Commit | Line | Data |
---|---|---|
3a27ad0b | 1 | #define _GNU_SOURCE |
4d1135e4 FB |
2 | #include <stdlib.h> |
3 | #include <stdio.h> | |
6dbad63e | 4 | #include <inttypes.h> |
4d1135e4 | 5 | #include <math.h> |
3a27ad0b FB |
6 | #include <signal.h> |
7 | #include <setjmp.h> | |
8 | #include <sys/ucontext.h> | |
9 | #include <sys/mman.h> | |
10 | #include <asm/vm86.h> | |
4d1135e4 | 11 | |
03bfca94 FB |
12 | #define TEST_CMOV 0 |
13 | #define TEST_FCOMI 0 | |
5dd9488c | 14 | |
4d1135e4 FB |
15 | #define xglue(x, y) x ## y |
16 | #define glue(x, y) xglue(x, y) | |
17 | #define stringify(s) tostring(s) | |
18 | #define tostring(s) #s | |
19 | ||
20 | #define CC_C 0x0001 | |
21 | #define CC_P 0x0004 | |
22 | #define CC_A 0x0010 | |
23 | #define CC_Z 0x0040 | |
24 | #define CC_S 0x0080 | |
25 | #define CC_O 0x0800 | |
26 | ||
4d1135e4 FB |
27 | #define __init_call __attribute__ ((unused,__section__ (".initcall.init"))) |
28 | ||
29 | static void *call_start __init_call = NULL; | |
30 | ||
4b74fe1f FB |
31 | #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A) |
32 | ||
4d1135e4 FB |
33 | #define OP add |
34 | #include "test-i386.h" | |
35 | ||
36 | #define OP sub | |
37 | #include "test-i386.h" | |
38 | ||
39 | #define OP xor | |
40 | #include "test-i386.h" | |
41 | ||
42 | #define OP and | |
43 | #include "test-i386.h" | |
44 | ||
45 | #define OP or | |
46 | #include "test-i386.h" | |
47 | ||
48 | #define OP cmp | |
49 | #include "test-i386.h" | |
50 | ||
51 | #define OP adc | |
52 | #define OP_CC | |
53 | #include "test-i386.h" | |
54 | ||
55 | #define OP sbb | |
56 | #define OP_CC | |
57 | #include "test-i386.h" | |
58 | ||
59 | #define OP inc | |
60 | #define OP_CC | |
61 | #define OP1 | |
62 | #include "test-i386.h" | |
63 | ||
64 | #define OP dec | |
65 | #define OP_CC | |
66 | #define OP1 | |
67 | #include "test-i386.h" | |
68 | ||
69 | #define OP neg | |
70 | #define OP_CC | |
71 | #define OP1 | |
72 | #include "test-i386.h" | |
73 | ||
74 | #define OP not | |
75 | #define OP_CC | |
76 | #define OP1 | |
77 | #include "test-i386.h" | |
78 | ||
4b74fe1f FB |
79 | #undef CC_MASK |
80 | #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O) | |
81 | ||
379ca80d FB |
82 | #define OP shl |
83 | #include "test-i386-shift.h" | |
84 | ||
85 | #define OP shr | |
86 | #include "test-i386-shift.h" | |
87 | ||
88 | #define OP sar | |
89 | #include "test-i386-shift.h" | |
90 | ||
91 | #define OP rol | |
92 | #include "test-i386-shift.h" | |
93 | ||
94 | #define OP ror | |
95 | #include "test-i386-shift.h" | |
96 | ||
97 | #define OP rcr | |
98 | #define OP_CC | |
99 | #include "test-i386-shift.h" | |
100 | ||
101 | #define OP rcl | |
102 | #define OP_CC | |
103 | #include "test-i386-shift.h" | |
104 | ||
d57c4e01 FB |
105 | #define OP shld |
106 | #define OP_SHIFTD | |
107 | #define OP_NOBYTE | |
108 | #include "test-i386-shift.h" | |
109 | ||
110 | #define OP shrd | |
111 | #define OP_SHIFTD | |
112 | #define OP_NOBYTE | |
113 | #include "test-i386-shift.h" | |
114 | ||
115 | /* XXX: should be more precise ? */ | |
116 | #undef CC_MASK | |
117 | #define CC_MASK (CC_C) | |
118 | ||
119 | #define OP bt | |
120 | #define OP_NOBYTE | |
121 | #include "test-i386-shift.h" | |
122 | ||
123 | #define OP bts | |
124 | #define OP_NOBYTE | |
125 | #include "test-i386-shift.h" | |
126 | ||
127 | #define OP btr | |
128 | #define OP_NOBYTE | |
129 | #include "test-i386-shift.h" | |
130 | ||
131 | #define OP btc | |
132 | #define OP_NOBYTE | |
133 | #include "test-i386-shift.h" | |
379ca80d | 134 | |
4d1135e4 FB |
135 | /* lea test (modrm support) */ |
136 | #define TEST_LEA(STR)\ | |
137 | {\ | |
138 | asm("leal " STR ", %0"\ | |
139 | : "=r" (res)\ | |
140 | : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\ | |
141 | printf("lea %s = %08x\n", STR, res);\ | |
142 | } | |
143 | ||
144 | #define TEST_LEA16(STR)\ | |
145 | {\ | |
146 | asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\ | |
147 | : "=wq" (res)\ | |
148 | : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\ | |
149 | printf("lea %s = %08x\n", STR, res);\ | |
150 | } | |
151 | ||
152 | ||
153 | void test_lea(void) | |
154 | { | |
155 | int eax, ebx, ecx, edx, esi, edi, res; | |
156 | eax = 0x0001; | |
157 | ebx = 0x0002; | |
158 | ecx = 0x0004; | |
159 | edx = 0x0008; | |
160 | esi = 0x0010; | |
161 | edi = 0x0020; | |
162 | ||
163 | TEST_LEA("0x4000"); | |
164 | ||
165 | TEST_LEA("(%%eax)"); | |
166 | TEST_LEA("(%%ebx)"); | |
167 | TEST_LEA("(%%ecx)"); | |
168 | TEST_LEA("(%%edx)"); | |
169 | TEST_LEA("(%%esi)"); | |
170 | TEST_LEA("(%%edi)"); | |
171 | ||
172 | TEST_LEA("0x40(%%eax)"); | |
173 | TEST_LEA("0x40(%%ebx)"); | |
174 | TEST_LEA("0x40(%%ecx)"); | |
175 | TEST_LEA("0x40(%%edx)"); | |
176 | TEST_LEA("0x40(%%esi)"); | |
177 | TEST_LEA("0x40(%%edi)"); | |
178 | ||
179 | TEST_LEA("0x4000(%%eax)"); | |
180 | TEST_LEA("0x4000(%%ebx)"); | |
181 | TEST_LEA("0x4000(%%ecx)"); | |
182 | TEST_LEA("0x4000(%%edx)"); | |
183 | TEST_LEA("0x4000(%%esi)"); | |
184 | TEST_LEA("0x4000(%%edi)"); | |
185 | ||
186 | TEST_LEA("(%%eax, %%ecx)"); | |
187 | TEST_LEA("(%%ebx, %%edx)"); | |
188 | TEST_LEA("(%%ecx, %%ecx)"); | |
189 | TEST_LEA("(%%edx, %%ecx)"); | |
190 | TEST_LEA("(%%esi, %%ecx)"); | |
191 | TEST_LEA("(%%edi, %%ecx)"); | |
192 | ||
193 | TEST_LEA("0x40(%%eax, %%ecx)"); | |
194 | TEST_LEA("0x4000(%%ebx, %%edx)"); | |
195 | ||
196 | TEST_LEA("(%%ecx, %%ecx, 2)"); | |
197 | TEST_LEA("(%%edx, %%ecx, 4)"); | |
198 | TEST_LEA("(%%esi, %%ecx, 8)"); | |
199 | ||
200 | TEST_LEA("(,%%eax, 2)"); | |
201 | TEST_LEA("(,%%ebx, 4)"); | |
202 | TEST_LEA("(,%%ecx, 8)"); | |
203 | ||
204 | TEST_LEA("0x40(,%%eax, 2)"); | |
205 | TEST_LEA("0x40(,%%ebx, 4)"); | |
206 | TEST_LEA("0x40(,%%ecx, 8)"); | |
207 | ||
208 | ||
209 | TEST_LEA("-10(%%ecx, %%ecx, 2)"); | |
210 | TEST_LEA("-10(%%edx, %%ecx, 4)"); | |
211 | TEST_LEA("-10(%%esi, %%ecx, 8)"); | |
212 | ||
213 | TEST_LEA("0x4000(%%ecx, %%ecx, 2)"); | |
214 | TEST_LEA("0x4000(%%edx, %%ecx, 4)"); | |
215 | TEST_LEA("0x4000(%%esi, %%ecx, 8)"); | |
216 | ||
217 | /* limited 16 bit addressing test */ | |
218 | TEST_LEA16("0x4000"); | |
219 | TEST_LEA16("(%%bx)"); | |
220 | TEST_LEA16("(%%si)"); | |
221 | TEST_LEA16("(%%di)"); | |
222 | TEST_LEA16("0x40(%%bx)"); | |
223 | TEST_LEA16("0x40(%%si)"); | |
224 | TEST_LEA16("0x40(%%di)"); | |
225 | TEST_LEA16("0x4000(%%bx)"); | |
226 | TEST_LEA16("0x4000(%%si)"); | |
227 | TEST_LEA16("(%%bx,%%si)"); | |
228 | TEST_LEA16("(%%bx,%%di)"); | |
229 | TEST_LEA16("0x40(%%bx,%%si)"); | |
230 | TEST_LEA16("0x40(%%bx,%%di)"); | |
231 | TEST_LEA16("0x4000(%%bx,%%si)"); | |
232 | TEST_LEA16("0x4000(%%bx,%%di)"); | |
233 | } | |
234 | ||
235 | #define TEST_JCC(JCC, v1, v2)\ | |
236 | {\ | |
5dd9488c | 237 | int res;\ |
4d1135e4 FB |
238 | asm("movl $1, %0\n\t"\ |
239 | "cmpl %2, %1\n\t"\ | |
5dd9488c | 240 | "j" JCC " 1f\n\t"\ |
4d1135e4 FB |
241 | "movl $0, %0\n\t"\ |
242 | "1:\n\t"\ | |
243 | : "=r" (res)\ | |
244 | : "r" (v1), "r" (v2));\ | |
5dd9488c FB |
245 | printf("%-10s %d\n", "j" JCC, res);\ |
246 | \ | |
247 | asm("movl $0, %0\n\t"\ | |
248 | "cmpl %2, %1\n\t"\ | |
249 | "set" JCC " %b0\n\t"\ | |
250 | : "=r" (res)\ | |
251 | : "r" (v1), "r" (v2));\ | |
252 | printf("%-10s %d\n", "set" JCC, res);\ | |
253 | if (TEST_CMOV) {\ | |
254 | asm("movl $0x12345678, %0\n\t"\ | |
255 | "cmpl %2, %1\n\t"\ | |
256 | "cmov" JCC "l %3, %0\n\t"\ | |
257 | : "=r" (res)\ | |
258 | : "r" (v1), "r" (v2), "m" (1));\ | |
259 | printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\ | |
260 | asm("movl $0x12345678, %0\n\t"\ | |
261 | "cmpl %2, %1\n\t"\ | |
262 | "cmov" JCC "w %w3, %w0\n\t"\ | |
263 | : "=r" (res)\ | |
264 | : "r" (v1), "r" (v2), "r" (1));\ | |
265 | printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\ | |
266 | } \ | |
4d1135e4 FB |
267 | } |
268 | ||
269 | /* various jump tests */ | |
270 | void test_jcc(void) | |
271 | { | |
5dd9488c FB |
272 | TEST_JCC("ne", 1, 1); |
273 | TEST_JCC("ne", 1, 0); | |
4d1135e4 | 274 | |
5dd9488c FB |
275 | TEST_JCC("e", 1, 1); |
276 | TEST_JCC("e", 1, 0); | |
4d1135e4 | 277 | |
5dd9488c FB |
278 | TEST_JCC("l", 1, 1); |
279 | TEST_JCC("l", 1, 0); | |
280 | TEST_JCC("l", 1, -1); | |
4d1135e4 | 281 | |
5dd9488c FB |
282 | TEST_JCC("le", 1, 1); |
283 | TEST_JCC("le", 1, 0); | |
284 | TEST_JCC("le", 1, -1); | |
4d1135e4 | 285 | |
5dd9488c FB |
286 | TEST_JCC("ge", 1, 1); |
287 | TEST_JCC("ge", 1, 0); | |
288 | TEST_JCC("ge", -1, 1); | |
4d1135e4 | 289 | |
5dd9488c FB |
290 | TEST_JCC("g", 1, 1); |
291 | TEST_JCC("g", 1, 0); | |
292 | TEST_JCC("g", 1, -1); | |
4d1135e4 | 293 | |
5dd9488c FB |
294 | TEST_JCC("b", 1, 1); |
295 | TEST_JCC("b", 1, 0); | |
296 | TEST_JCC("b", 1, -1); | |
4d1135e4 | 297 | |
5dd9488c FB |
298 | TEST_JCC("be", 1, 1); |
299 | TEST_JCC("be", 1, 0); | |
300 | TEST_JCC("be", 1, -1); | |
4d1135e4 | 301 | |
5dd9488c FB |
302 | TEST_JCC("ae", 1, 1); |
303 | TEST_JCC("ae", 1, 0); | |
304 | TEST_JCC("ae", 1, -1); | |
4d1135e4 | 305 | |
5dd9488c FB |
306 | TEST_JCC("a", 1, 1); |
307 | TEST_JCC("a", 1, 0); | |
308 | TEST_JCC("a", 1, -1); | |
4d1135e4 FB |
309 | |
310 | ||
5dd9488c FB |
311 | TEST_JCC("p", 1, 1); |
312 | TEST_JCC("p", 1, 0); | |
4d1135e4 | 313 | |
5dd9488c FB |
314 | TEST_JCC("np", 1, 1); |
315 | TEST_JCC("np", 1, 0); | |
4d1135e4 | 316 | |
5dd9488c FB |
317 | TEST_JCC("o", 0x7fffffff, 0); |
318 | TEST_JCC("o", 0x7fffffff, -1); | |
4d1135e4 | 319 | |
5dd9488c FB |
320 | TEST_JCC("no", 0x7fffffff, 0); |
321 | TEST_JCC("no", 0x7fffffff, -1); | |
4d1135e4 | 322 | |
5dd9488c FB |
323 | TEST_JCC("s", 0, 1); |
324 | TEST_JCC("s", 0, -1); | |
325 | TEST_JCC("s", 0, 0); | |
4d1135e4 | 326 | |
5dd9488c FB |
327 | TEST_JCC("ns", 0, 1); |
328 | TEST_JCC("ns", 0, -1); | |
329 | TEST_JCC("ns", 0, 0); | |
4d1135e4 FB |
330 | } |
331 | ||
4b74fe1f FB |
332 | #undef CC_MASK |
333 | #define CC_MASK (CC_O | CC_C) | |
334 | ||
335 | #define OP mul | |
336 | #include "test-i386-muldiv.h" | |
337 | ||
338 | #define OP imul | |
339 | #include "test-i386-muldiv.h" | |
340 | ||
341 | #undef CC_MASK | |
342 | #define CC_MASK (0) | |
343 | ||
344 | #define OP div | |
345 | #include "test-i386-muldiv.h" | |
346 | ||
347 | #define OP idiv | |
348 | #include "test-i386-muldiv.h" | |
349 | ||
350 | void test_imulw2(int op0, int op1) | |
351 | { | |
352 | int res, s1, s0, flags; | |
353 | s0 = op0; | |
354 | s1 = op1; | |
355 | res = s0; | |
356 | flags = 0; | |
357 | asm ("push %4\n\t" | |
358 | "popf\n\t" | |
359 | "imulw %w2, %w0\n\t" | |
360 | "pushf\n\t" | |
361 | "popl %1\n\t" | |
362 | : "=q" (res), "=g" (flags) | |
363 | : "q" (s1), "0" (res), "1" (flags)); | |
364 | printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n", | |
365 | "imulw", s0, s1, res, flags & CC_MASK); | |
366 | } | |
367 | ||
368 | void test_imull2(int op0, int op1) | |
369 | { | |
370 | int res, s1, s0, flags; | |
371 | s0 = op0; | |
372 | s1 = op1; | |
373 | res = s0; | |
374 | flags = 0; | |
375 | asm ("push %4\n\t" | |
376 | "popf\n\t" | |
377 | "imull %2, %0\n\t" | |
378 | "pushf\n\t" | |
379 | "popl %1\n\t" | |
380 | : "=q" (res), "=g" (flags) | |
381 | : "q" (s1), "0" (res), "1" (flags)); | |
382 | printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n", | |
383 | "imull", s0, s1, res, flags & CC_MASK); | |
384 | } | |
385 | ||
386 | void test_mul(void) | |
387 | { | |
388 | test_imulb(0x1234561d, 4); | |
389 | test_imulb(3, -4); | |
390 | test_imulb(0x80, 0x80); | |
391 | test_imulb(0x10, 0x10); | |
392 | ||
393 | test_imulw(0, 0x1234001d, 45); | |
394 | test_imulw(0, 23, -45); | |
395 | test_imulw(0, 0x8000, 0x8000); | |
396 | test_imulw(0, 0x100, 0x100); | |
397 | ||
398 | test_imull(0, 0x1234001d, 45); | |
399 | test_imull(0, 23, -45); | |
400 | test_imull(0, 0x80000000, 0x80000000); | |
401 | test_imull(0, 0x10000, 0x10000); | |
402 | ||
403 | test_mulb(0x1234561d, 4); | |
404 | test_mulb(3, -4); | |
405 | test_mulb(0x80, 0x80); | |
406 | test_mulb(0x10, 0x10); | |
407 | ||
408 | test_mulw(0, 0x1234001d, 45); | |
409 | test_mulw(0, 23, -45); | |
410 | test_mulw(0, 0x8000, 0x8000); | |
411 | test_mulw(0, 0x100, 0x100); | |
412 | ||
413 | test_mull(0, 0x1234001d, 45); | |
414 | test_mull(0, 23, -45); | |
415 | test_mull(0, 0x80000000, 0x80000000); | |
416 | test_mull(0, 0x10000, 0x10000); | |
417 | ||
418 | test_imulw2(0x1234001d, 45); | |
419 | test_imulw2(23, -45); | |
420 | test_imulw2(0x8000, 0x8000); | |
421 | test_imulw2(0x100, 0x100); | |
422 | ||
423 | test_imull2(0x1234001d, 45); | |
424 | test_imull2(23, -45); | |
425 | test_imull2(0x80000000, 0x80000000); | |
426 | test_imull2(0x10000, 0x10000); | |
427 | ||
428 | test_idivb(0x12341678, 0x127e); | |
429 | test_idivb(0x43210123, -5); | |
430 | test_idivb(0x12340004, -1); | |
431 | ||
432 | test_idivw(0, 0x12345678, 12347); | |
433 | test_idivw(0, -23223, -45); | |
434 | test_idivw(0, 0x12348000, -1); | |
435 | test_idivw(0x12343, 0x12345678, 0x81238567); | |
436 | ||
437 | test_idivl(0, 0x12345678, 12347); | |
438 | test_idivl(0, -233223, -45); | |
439 | test_idivl(0, 0x80000000, -1); | |
440 | test_idivl(0x12343, 0x12345678, 0x81234567); | |
441 | ||
442 | test_divb(0x12341678, 0x127e); | |
443 | test_divb(0x43210123, -5); | |
444 | test_divb(0x12340004, -1); | |
445 | ||
446 | test_divw(0, 0x12345678, 12347); | |
447 | test_divw(0, -23223, -45); | |
448 | test_divw(0, 0x12348000, -1); | |
449 | test_divw(0x12343, 0x12345678, 0x81238567); | |
450 | ||
451 | test_divl(0, 0x12345678, 12347); | |
452 | test_divl(0, -233223, -45); | |
453 | test_divl(0, 0x80000000, -1); | |
454 | test_divl(0x12343, 0x12345678, 0x81234567); | |
455 | } | |
456 | ||
9d8e9c09 FB |
457 | #define TEST_BSX(op, size, op0)\ |
458 | {\ | |
459 | int res, val, resz;\ | |
460 | val = op0;\ | |
461 | asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \ | |
462 | : "=r" (res), "=q" (resz)\ | |
463 | : "g" (val));\ | |
464 | printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\ | |
465 | } | |
466 | ||
467 | void test_bsx(void) | |
468 | { | |
469 | TEST_BSX(bsrw, "w", 0); | |
470 | TEST_BSX(bsrw, "w", 0x12340128); | |
471 | TEST_BSX(bsrl, "", 0); | |
472 | TEST_BSX(bsrl, "", 0x00340128); | |
473 | TEST_BSX(bsfw, "w", 0); | |
474 | TEST_BSX(bsfw, "w", 0x12340128); | |
475 | TEST_BSX(bsfl, "", 0); | |
476 | TEST_BSX(bsfl, "", 0x00340128); | |
477 | } | |
478 | ||
55480af8 FB |
479 | /**********************************************/ |
480 | ||
9d8e9c09 FB |
481 | void test_fops(double a, double b) |
482 | { | |
483 | printf("a=%f b=%f a+b=%f\n", a, b, a + b); | |
484 | printf("a=%f b=%f a-b=%f\n", a, b, a - b); | |
485 | printf("a=%f b=%f a*b=%f\n", a, b, a * b); | |
486 | printf("a=%f b=%f a/b=%f\n", a, b, a / b); | |
487 | printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b)); | |
488 | printf("a=%f sqrt(a)=%f\n", a, sqrt(a)); | |
489 | printf("a=%f sin(a)=%f\n", a, sin(a)); | |
490 | printf("a=%f cos(a)=%f\n", a, cos(a)); | |
491 | printf("a=%f tan(a)=%f\n", a, tan(a)); | |
492 | printf("a=%f log(a)=%f\n", a, log(a)); | |
493 | printf("a=%f exp(a)=%f\n", a, exp(a)); | |
494 | printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b)); | |
495 | /* just to test some op combining */ | |
496 | printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a))); | |
497 | printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a))); | |
498 | printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a))); | |
499 | ||
500 | } | |
501 | ||
502 | void test_fcmp(double a, double b) | |
503 | { | |
504 | printf("(%f<%f)=%d\n", | |
505 | a, b, a < b); | |
506 | printf("(%f<=%f)=%d\n", | |
507 | a, b, a <= b); | |
508 | printf("(%f==%f)=%d\n", | |
509 | a, b, a == b); | |
510 | printf("(%f>%f)=%d\n", | |
511 | a, b, a > b); | |
512 | printf("(%f<=%f)=%d\n", | |
513 | a, b, a >= b); | |
03bfca94 FB |
514 | if (TEST_FCOMI) { |
515 | unsigned int eflags; | |
516 | /* test f(u)comi instruction */ | |
517 | asm("fcomi %2, %1\n" | |
518 | "pushf\n" | |
519 | "pop %0\n" | |
520 | : "=r" (eflags) | |
521 | : "t" (a), "u" (b)); | |
522 | printf("fcomi(%f %f)=%08x\n", a, b, eflags & (CC_Z | CC_P | CC_C)); | |
523 | } | |
9d8e9c09 FB |
524 | } |
525 | ||
526 | void test_fcvt(double a) | |
527 | { | |
528 | float fa; | |
529 | long double la; | |
ea768640 FB |
530 | int16_t fpuc; |
531 | int i; | |
532 | int64_t lla; | |
533 | int ia; | |
534 | int16_t wa; | |
535 | double ra; | |
9d8e9c09 FB |
536 | |
537 | fa = a; | |
538 | la = a; | |
539 | printf("(float)%f = %f\n", a, fa); | |
540 | printf("(long double)%f = %Lf\n", a, la); | |
c5e9815d FB |
541 | printf("a=%016Lx\n", *(long long *)&a); |
542 | printf("la=%016Lx %04x\n", *(long long *)&la, | |
543 | *(unsigned short *)((char *)(&la) + 8)); | |
ea768640 FB |
544 | |
545 | /* test all roundings */ | |
546 | asm volatile ("fstcw %0" : "=m" (fpuc)); | |
547 | for(i=0;i<4;i++) { | |
548 | asm volatile ("fldcw %0" : : "m" ((fpuc & ~0x0c00) | (i << 10))); | |
549 | asm volatile ("fist %0" : "=m" (wa) : "t" (a)); | |
550 | asm volatile ("fistl %0" : "=m" (ia) : "t" (a)); | |
551 | asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st"); | |
552 | asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a)); | |
553 | asm volatile ("fldcw %0" : : "m" (fpuc)); | |
554 | printf("(short)a = %d\n", wa); | |
555 | printf("(int)a = %d\n", ia); | |
556 | printf("(int64_t)a = %Ld\n", lla); | |
557 | printf("rint(a) = %f\n", ra); | |
558 | } | |
9d8e9c09 FB |
559 | } |
560 | ||
561 | #define TEST(N) \ | |
562 | asm("fld" #N : "=t" (a)); \ | |
563 | printf("fld" #N "= %f\n", a); | |
564 | ||
565 | void test_fconst(void) | |
566 | { | |
567 | double a; | |
568 | TEST(1); | |
569 | TEST(l2t); | |
570 | TEST(l2e); | |
571 | TEST(pi); | |
572 | TEST(lg2); | |
573 | TEST(ln2); | |
574 | TEST(z); | |
575 | } | |
576 | ||
c5e9815d FB |
577 | void test_fbcd(double a) |
578 | { | |
579 | unsigned short bcd[5]; | |
580 | double b; | |
581 | ||
582 | asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st"); | |
583 | asm("fbld %1" : "=t" (b) : "m" (bcd[0])); | |
584 | printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n", | |
585 | a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b); | |
586 | } | |
587 | ||
03bfca94 FB |
588 | #define TEST_ENV(env, prefix)\ |
589 | {\ | |
590 | memset((env), 0xaa, sizeof(*(env)));\ | |
591 | asm("fld1\n"\ | |
592 | prefix "fnstenv %1\n"\ | |
593 | prefix "fldenv %1\n"\ | |
594 | : "=t" (res) : "m" (*(env)) : "st");\ | |
595 | printf("res=%f\n", res);\ | |
596 | printf("fpuc=%04x fpus=%04x fptag=%04x\n",\ | |
597 | (env)->fpuc,\ | |
598 | (env)->fpus & 0xff00,\ | |
599 | (env)->fptag);\ | |
600 | memset((env), 0xaa, sizeof(*(env)));\ | |
601 | asm("fld1\n"\ | |
602 | prefix "fnsave %1\n"\ | |
603 | prefix "frstor %1\n"\ | |
604 | : "=t" (res) : "m" (*(env)) : "st");\ | |
605 | printf("res=%f\n", res);\ | |
606 | printf("fpuc=%04x fpus=%04x fptag=%04x\n",\ | |
607 | (env)->fpuc,\ | |
608 | (env)->fpus & 0xff00,\ | |
609 | (env)->fptag);\ | |
610 | printf("ST(0) = %Lf\n",\ | |
611 | (env)->fpregs[0]);\ | |
612 | } | |
613 | ||
614 | void test_fenv(void) | |
615 | { | |
616 | struct __attribute__((packed)) { | |
617 | uint16_t fpuc; | |
618 | uint16_t dummy1; | |
619 | uint16_t fpus; | |
620 | uint16_t dummy2; | |
621 | uint16_t fptag; | |
622 | uint16_t dummy3; | |
623 | uint32_t ignored[4]; | |
624 | long double fpregs[8]; | |
625 | } float_env32; | |
626 | struct __attribute__((packed)) { | |
627 | uint16_t fpuc; | |
628 | uint16_t fpus; | |
629 | uint16_t fptag; | |
630 | uint16_t ignored[4]; | |
631 | long double fpregs[8]; | |
632 | } float_env16; | |
633 | double res; | |
634 | ||
635 | TEST_ENV(&float_env16, "data16 "); | |
636 | TEST_ENV(&float_env32, ""); | |
637 | } | |
638 | ||
9d8e9c09 FB |
639 | void test_floats(void) |
640 | { | |
641 | test_fops(2, 3); | |
642 | test_fops(1.4, -5); | |
643 | test_fcmp(2, -1); | |
644 | test_fcmp(2, 2); | |
645 | test_fcmp(2, 3); | |
ea768640 FB |
646 | test_fcvt(0.5); |
647 | test_fcvt(-0.5); | |
9d8e9c09 FB |
648 | test_fcvt(1.0/7.0); |
649 | test_fcvt(-1.0/9.0); | |
ea768640 FB |
650 | test_fcvt(32768); |
651 | test_fcvt(-1e20); | |
9d8e9c09 | 652 | test_fconst(); |
c5e9815d FB |
653 | test_fbcd(1234567890123456); |
654 | test_fbcd(-123451234567890); | |
03bfca94 | 655 | test_fenv(); |
9d8e9c09 | 656 | } |
4b74fe1f | 657 | |
55480af8 FB |
658 | /**********************************************/ |
659 | ||
660 | #define TEST_BCD(op, op0, cc_in, cc_mask)\ | |
661 | {\ | |
662 | int res, flags;\ | |
663 | res = op0;\ | |
664 | flags = cc_in;\ | |
665 | asm ("push %3\n\t"\ | |
666 | "popf\n\t"\ | |
667 | #op "\n\t"\ | |
668 | "pushf\n\t"\ | |
669 | "popl %1\n\t"\ | |
670 | : "=a" (res), "=g" (flags)\ | |
671 | : "0" (res), "1" (flags));\ | |
672 | printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\ | |
673 | #op, op0, res, cc_in, flags & cc_mask);\ | |
674 | } | |
675 | ||
676 | void test_bcd(void) | |
677 | { | |
678 | TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
679 | TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
680 | TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
681 | TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
682 | TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
683 | TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
684 | TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
685 | TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
686 | TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
687 | TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
688 | TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
689 | TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
690 | TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
691 | ||
692 | TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
693 | TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
694 | TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
695 | TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
696 | TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
697 | TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
698 | TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
699 | TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
700 | TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
701 | TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
702 | TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
703 | TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
704 | TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A)); | |
705 | ||
706 | TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A)); | |
707 | TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A)); | |
708 | TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A)); | |
709 | TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A)); | |
710 | TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A)); | |
711 | TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A)); | |
712 | TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A)); | |
713 | TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A)); | |
714 | ||
715 | TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A)); | |
716 | TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A)); | |
717 | TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A)); | |
718 | TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A)); | |
719 | TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A)); | |
720 | TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A)); | |
721 | TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A)); | |
722 | TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A)); | |
723 | ||
724 | TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)); | |
725 | TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)); | |
726 | } | |
727 | ||
e5918247 FB |
728 | #define TEST_XCHG(op, size, opconst)\ |
729 | {\ | |
730 | int op0, op1;\ | |
731 | op0 = 0x12345678;\ | |
732 | op1 = 0xfbca7654;\ | |
733 | asm(#op " %" size "0, %" size "1" \ | |
734 | : "=q" (op0), opconst (op1) \ | |
735 | : "0" (op0), "1" (op1));\ | |
736 | printf("%-10s A=%08x B=%08x\n",\ | |
737 | #op, op0, op1);\ | |
738 | } | |
739 | ||
740 | #define TEST_CMPXCHG(op, size, opconst, eax)\ | |
741 | {\ | |
742 | int op0, op1;\ | |
743 | op0 = 0x12345678;\ | |
744 | op1 = 0xfbca7654;\ | |
745 | asm(#op " %" size "0, %" size "1" \ | |
746 | : "=q" (op0), opconst (op1) \ | |
747 | : "0" (op0), "1" (op1), "a" (eax));\ | |
748 | printf("%-10s EAX=%08x A=%08x C=%08x\n",\ | |
749 | #op, eax, op0, op1);\ | |
750 | } | |
751 | ||
752 | void test_xchg(void) | |
753 | { | |
754 | TEST_XCHG(xchgl, "", "=q"); | |
755 | TEST_XCHG(xchgw, "w", "=q"); | |
756 | TEST_XCHG(xchgb, "b", "=q"); | |
757 | ||
758 | TEST_XCHG(xchgl, "", "=m"); | |
759 | TEST_XCHG(xchgw, "w", "=m"); | |
760 | TEST_XCHG(xchgb, "b", "=m"); | |
761 | ||
762 | TEST_XCHG(xaddl, "", "=q"); | |
763 | TEST_XCHG(xaddw, "w", "=q"); | |
764 | TEST_XCHG(xaddb, "b", "=q"); | |
765 | ||
766 | TEST_XCHG(xaddl, "", "=m"); | |
767 | TEST_XCHG(xaddw, "w", "=m"); | |
768 | TEST_XCHG(xaddb, "b", "=m"); | |
769 | ||
770 | TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654); | |
771 | TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654); | |
772 | TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654); | |
773 | ||
774 | TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc); | |
775 | TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc); | |
776 | TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc); | |
777 | ||
778 | TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654); | |
779 | TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654); | |
780 | TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654); | |
781 | ||
782 | TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc); | |
783 | TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc); | |
784 | TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc); | |
785 | } | |
786 | ||
6dbad63e FB |
787 | /**********************************************/ |
788 | /* segmentation tests */ | |
789 | ||
790 | #include <asm/ldt.h> | |
791 | #include <linux/unistd.h> | |
792 | ||
793 | _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount) | |
794 | ||
795 | uint8_t seg_data1[4096]; | |
796 | uint8_t seg_data2[4096]; | |
797 | ||
e5918247 | 798 | #define MK_SEL(n) (((n) << 3) | 7) |
6dbad63e | 799 | |
288426fe FB |
800 | #define TEST_LR(op, size, seg, mask)\ |
801 | {\ | |
802 | int res, res2;\ | |
803 | res = 0x12345678;\ | |
804 | asm (op " %" size "2, %" size "0\n" \ | |
805 | "movl $0, %1\n"\ | |
806 | "jnz 1f\n"\ | |
807 | "movl $1, %1\n"\ | |
808 | "1:\n"\ | |
809 | : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\ | |
810 | printf(op ": Z=%d %08x\n", res2, res & ~(mask));\ | |
811 | } | |
812 | ||
6dbad63e FB |
813 | /* NOTE: we use Linux modify_ldt syscall */ |
814 | void test_segs(void) | |
815 | { | |
816 | struct modify_ldt_ldt_s ldt; | |
817 | long long ldt_table[3]; | |
04369ff2 | 818 | int res, res2; |
6dbad63e | 819 | char tmp; |
e1d4294a FB |
820 | struct { |
821 | uint32_t offset; | |
822 | uint16_t seg; | |
823 | } __attribute__((packed)) segoff; | |
6dbad63e FB |
824 | |
825 | ldt.entry_number = 1; | |
826 | ldt.base_addr = (unsigned long)&seg_data1; | |
827 | ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12; | |
828 | ldt.seg_32bit = 1; | |
829 | ldt.contents = MODIFY_LDT_CONTENTS_DATA; | |
830 | ldt.read_exec_only = 0; | |
831 | ldt.limit_in_pages = 1; | |
832 | ldt.seg_not_present = 0; | |
833 | ldt.useable = 1; | |
834 | modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ | |
835 | ||
836 | ldt.entry_number = 2; | |
837 | ldt.base_addr = (unsigned long)&seg_data2; | |
838 | ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12; | |
839 | ldt.seg_32bit = 1; | |
840 | ldt.contents = MODIFY_LDT_CONTENTS_DATA; | |
841 | ldt.read_exec_only = 0; | |
842 | ldt.limit_in_pages = 1; | |
843 | ldt.seg_not_present = 0; | |
844 | ldt.useable = 1; | |
845 | modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ | |
846 | ||
847 | modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */ | |
04369ff2 FB |
848 | #if 0 |
849 | { | |
850 | int i; | |
851 | for(i=0;i<3;i++) | |
852 | printf("%d: %016Lx\n", i, ldt_table[i]); | |
853 | } | |
854 | #endif | |
6dbad63e FB |
855 | /* do some tests with fs or gs */ |
856 | asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1))); | |
857 | asm volatile ("movl %0, %%gs" : : "r" (MK_SEL(2))); | |
858 | ||
859 | seg_data1[1] = 0xaa; | |
860 | seg_data2[1] = 0x55; | |
861 | ||
862 | asm volatile ("fs movzbl 0x1, %0" : "=r" (res)); | |
863 | printf("FS[1] = %02x\n", res); | |
864 | ||
865 | asm volatile ("gs movzbl 0x1, %0" : "=r" (res)); | |
866 | printf("GS[1] = %02x\n", res); | |
867 | ||
868 | /* tests with ds/ss (implicit segment case) */ | |
869 | tmp = 0xa5; | |
870 | asm volatile ("pushl %%ebp\n\t" | |
871 | "pushl %%ds\n\t" | |
872 | "movl %2, %%ds\n\t" | |
873 | "movl %3, %%ebp\n\t" | |
874 | "movzbl 0x1, %0\n\t" | |
875 | "movzbl (%%ebp), %1\n\t" | |
876 | "popl %%ds\n\t" | |
877 | "popl %%ebp\n\t" | |
878 | : "=r" (res), "=r" (res2) | |
879 | : "r" (MK_SEL(1)), "r" (&tmp)); | |
880 | printf("DS[1] = %02x\n", res); | |
881 | printf("SS[tmp] = %02x\n", res2); | |
e1d4294a FB |
882 | |
883 | segoff.seg = MK_SEL(2); | |
884 | segoff.offset = 0xabcdef12; | |
885 | asm volatile("lfs %2, %0\n\t" | |
886 | "movl %%fs, %1\n\t" | |
887 | : "=r" (res), "=g" (res2) | |
888 | : "m" (segoff)); | |
889 | printf("FS:reg = %04x:%08x\n", res2, res); | |
288426fe FB |
890 | |
891 | TEST_LR("larw", "w", MK_SEL(2), 0x0100); | |
892 | TEST_LR("larl", "", MK_SEL(2), 0x0100); | |
893 | TEST_LR("lslw", "w", MK_SEL(2), 0); | |
894 | TEST_LR("lsll", "", MK_SEL(2), 0); | |
895 | ||
896 | TEST_LR("larw", "w", 0xfff8, 0); | |
897 | TEST_LR("larl", "", 0xfff8, 0); | |
898 | TEST_LR("lslw", "w", 0xfff8, 0); | |
899 | TEST_LR("lsll", "", 0xfff8, 0); | |
6dbad63e | 900 | } |
55480af8 | 901 | |
e5918247 FB |
902 | /* 16 bit code test */ |
903 | extern char code16_start, code16_end; | |
904 | extern char code16_func1; | |
905 | extern char code16_func2; | |
906 | extern char code16_func3; | |
a300e691 | 907 | |
e5918247 | 908 | void test_code16(void) |
1a9353d2 | 909 | { |
e5918247 FB |
910 | struct modify_ldt_ldt_s ldt; |
911 | int res, res2; | |
a300e691 | 912 | |
e5918247 FB |
913 | /* build a code segment */ |
914 | ldt.entry_number = 1; | |
915 | ldt.base_addr = (unsigned long)&code16_start; | |
916 | ldt.limit = &code16_end - &code16_start; | |
917 | ldt.seg_32bit = 0; | |
918 | ldt.contents = MODIFY_LDT_CONTENTS_CODE; | |
919 | ldt.read_exec_only = 0; | |
920 | ldt.limit_in_pages = 0; | |
921 | ldt.seg_not_present = 0; | |
922 | ldt.useable = 1; | |
923 | modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ | |
a300e691 | 924 | |
e5918247 FB |
925 | /* call the first function */ |
926 | asm volatile ("lcall %1, %2" | |
927 | : "=a" (res) | |
928 | : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc"); | |
929 | printf("func1() = 0x%08x\n", res); | |
930 | asm volatile ("lcall %2, %3" | |
931 | : "=a" (res), "=c" (res2) | |
932 | : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc"); | |
933 | printf("func2() = 0x%08x spdec=%d\n", res, res2); | |
934 | asm volatile ("lcall %1, %2" | |
935 | : "=a" (res) | |
936 | : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc"); | |
937 | printf("func3() = 0x%08x\n", res); | |
1a9353d2 FB |
938 | } |
939 | ||
e1d4294a FB |
940 | void test_misc(void) |
941 | { | |
942 | char table[256]; | |
943 | int res, i; | |
944 | ||
945 | for(i=0;i<256;i++) table[i] = 256 - i; | |
946 | res = 0x12345678; | |
947 | asm ("xlat" : "=a" (res) : "b" (table), "0" (res)); | |
948 | printf("xlat: EAX=%08x\n", res); | |
949 | } | |
950 | ||
951 | uint8_t str_buffer[4096]; | |
952 | ||
953 | #define TEST_STRING1(OP, size, DF, REP)\ | |
954 | {\ | |
955 | int esi, edi, eax, ecx, eflags;\ | |
956 | \ | |
957 | esi = (long)(str_buffer + sizeof(str_buffer) / 2);\ | |
958 | edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\ | |
959 | eax = 0x12345678;\ | |
960 | ecx = 17;\ | |
961 | \ | |
962 | asm volatile ("pushl $0\n\t"\ | |
963 | "popf\n\t"\ | |
964 | DF "\n\t"\ | |
965 | REP #OP size "\n\t"\ | |
966 | "cld\n\t"\ | |
967 | "pushf\n\t"\ | |
968 | "popl %4\n\t"\ | |
969 | : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\ | |
970 | : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\ | |
971 | printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\ | |
972 | REP #OP size, esi, edi, eax, ecx,\ | |
973 | eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\ | |
974 | } | |
975 | ||
976 | #define TEST_STRING(OP, REP)\ | |
977 | TEST_STRING1(OP, "b", "", REP);\ | |
978 | TEST_STRING1(OP, "w", "", REP);\ | |
979 | TEST_STRING1(OP, "l", "", REP);\ | |
980 | TEST_STRING1(OP, "b", "std", REP);\ | |
981 | TEST_STRING1(OP, "w", "std", REP);\ | |
982 | TEST_STRING1(OP, "l", "std", REP) | |
983 | ||
984 | void test_string(void) | |
985 | { | |
986 | int i; | |
987 | for(i = 0;i < sizeof(str_buffer); i++) | |
988 | str_buffer[i] = i + 0x56; | |
989 | TEST_STRING(stos, ""); | |
990 | TEST_STRING(stos, "rep "); | |
991 | TEST_STRING(lods, ""); /* to verify stos */ | |
992 | TEST_STRING(lods, "rep "); | |
993 | TEST_STRING(movs, ""); | |
994 | TEST_STRING(movs, "rep "); | |
995 | TEST_STRING(lods, ""); /* to verify stos */ | |
996 | ||
997 | /* XXX: better tests */ | |
998 | TEST_STRING(scas, ""); | |
999 | TEST_STRING(scas, "repz "); | |
1000 | TEST_STRING(scas, "repnz "); | |
1001 | TEST_STRING(cmps, ""); | |
1002 | TEST_STRING(cmps, "repz "); | |
1003 | TEST_STRING(cmps, "repnz "); | |
1004 | } | |
e5918247 | 1005 | |
3a27ad0b FB |
1006 | /* VM86 test */ |
1007 | ||
1008 | static inline void set_bit(uint8_t *a, unsigned int bit) | |
1009 | { | |
1010 | a[bit / 8] |= (1 << (bit % 8)); | |
1011 | } | |
1012 | ||
1013 | static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg) | |
1014 | { | |
1015 | return (uint8_t *)((seg << 4) + (reg & 0xffff)); | |
1016 | } | |
1017 | ||
1018 | static inline void pushw(struct vm86_regs *r, int val) | |
1019 | { | |
1020 | r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff); | |
1021 | *(uint16_t *)seg_to_linear(r->ss, r->esp) = val; | |
1022 | } | |
1023 | ||
1024 | #undef __syscall_return | |
1025 | #define __syscall_return(type, res) \ | |
1026 | do { \ | |
1027 | return (type) (res); \ | |
1028 | } while (0) | |
1029 | ||
1030 | _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86) | |
1031 | ||
1032 | extern char vm86_code_start; | |
1033 | extern char vm86_code_end; | |
1034 | ||
1035 | #define VM86_CODE_CS 0x100 | |
1036 | #define VM86_CODE_IP 0x100 | |
1037 | ||
1038 | void test_vm86(void) | |
1039 | { | |
1040 | struct vm86plus_struct ctx; | |
1041 | struct vm86_regs *r; | |
1042 | uint8_t *vm86_mem; | |
1043 | int seg, ret; | |
1044 | ||
1045 | vm86_mem = mmap((void *)0x00000000, 0x110000, | |
1046 | PROT_WRITE | PROT_READ | PROT_EXEC, | |
1047 | MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); | |
1048 | if (vm86_mem == MAP_FAILED) { | |
1049 | printf("ERROR: could not map vm86 memory"); | |
1050 | return; | |
1051 | } | |
1052 | memset(&ctx, 0, sizeof(ctx)); | |
1053 | ||
1054 | /* init basic registers */ | |
1055 | r = &ctx.regs; | |
1056 | r->eip = VM86_CODE_IP; | |
1057 | r->esp = 0xfffe; | |
1058 | seg = VM86_CODE_CS; | |
1059 | r->cs = seg; | |
1060 | r->ss = seg; | |
1061 | r->ds = seg; | |
1062 | r->es = seg; | |
1063 | r->fs = seg; | |
1064 | r->gs = seg; | |
1065 | r->eflags = VIF_MASK; | |
1066 | ||
1067 | /* move code to proper address. We use the same layout as a .com | |
1068 | dos program. */ | |
1069 | memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP, | |
1070 | &vm86_code_start, &vm86_code_end - &vm86_code_start); | |
1071 | ||
1072 | /* mark int 0x21 as being emulated */ | |
1073 | set_bit((uint8_t *)&ctx.int_revectored, 0x21); | |
1074 | ||
1075 | for(;;) { | |
1076 | ret = vm86(VM86_ENTER, &ctx); | |
1077 | switch(VM86_TYPE(ret)) { | |
1078 | case VM86_INTx: | |
1079 | { | |
1080 | int int_num, ah; | |
1081 | ||
1082 | int_num = VM86_ARG(ret); | |
1083 | if (int_num != 0x21) | |
1084 | goto unknown_int; | |
1085 | ah = (r->eax >> 8) & 0xff; | |
1086 | switch(ah) { | |
1087 | case 0x00: /* exit */ | |
1088 | goto the_end; | |
1089 | case 0x02: /* write char */ | |
1090 | { | |
1091 | uint8_t c = r->edx; | |
1092 | putchar(c); | |
1093 | } | |
1094 | break; | |
1095 | case 0x09: /* write string */ | |
1096 | { | |
1097 | uint8_t c, *ptr; | |
1098 | ptr = seg_to_linear(r->ds, r->edx); | |
1099 | for(;;) { | |
1100 | c = *ptr++; | |
1101 | if (c == '$') | |
1102 | break; | |
1103 | putchar(c); | |
1104 | } | |
1105 | r->eax = (r->eax & ~0xff) | '$'; | |
1106 | } | |
1107 | break; | |
1108 | case 0xff: /* extension: write hex number in edx */ | |
1109 | printf("%08x\n", (int)r->edx); | |
1110 | break; | |
1111 | default: | |
1112 | unknown_int: | |
1113 | printf("unsupported int 0x%02x\n", int_num); | |
1114 | goto the_end; | |
1115 | } | |
1116 | } | |
1117 | break; | |
1118 | case VM86_SIGNAL: | |
1119 | /* a signal came, we just ignore that */ | |
1120 | break; | |
1121 | case VM86_STI: | |
1122 | break; | |
1123 | default: | |
1124 | printf("ERROR: unhandled vm86 return code (0x%x)\n", ret); | |
1125 | goto the_end; | |
1126 | } | |
1127 | } | |
1128 | the_end: | |
1129 | printf("VM86 end\n"); | |
1130 | munmap(vm86_mem, 0x110000); | |
1131 | } | |
1132 | ||
1133 | /* exception tests */ | |
1134 | #ifndef REG_EAX | |
1135 | #define REG_EAX EAX | |
1136 | #define REG_EBX EBX | |
1137 | #define REG_ECX ECX | |
1138 | #define REG_EDX EDX | |
1139 | #define REG_ESI ESI | |
1140 | #define REG_EDI EDI | |
1141 | #define REG_EBP EBP | |
1142 | #define REG_ESP ESP | |
1143 | #define REG_EIP EIP | |
1144 | #define REG_EFL EFL | |
1145 | #define REG_TRAPNO TRAPNO | |
1146 | #define REG_ERR ERR | |
1147 | #endif | |
1148 | ||
1149 | jmp_buf jmp_env; | |
3a27ad0b FB |
1150 | int v1; |
1151 | int tab[2]; | |
1152 | ||
1153 | void sig_handler(int sig, siginfo_t *info, void *puc) | |
1154 | { | |
1155 | struct ucontext *uc = puc; | |
1156 | ||
1157 | printf("si_signo=%d si_errno=%d si_code=%d", | |
1158 | info->si_signo, info->si_errno, info->si_code); | |
e3b32540 FB |
1159 | printf(" si_addr=0x%08lx", |
1160 | (unsigned long)info->si_addr); | |
3a27ad0b FB |
1161 | printf("\n"); |
1162 | ||
1163 | printf("trapno=0x%02x err=0x%08x", | |
1164 | uc->uc_mcontext.gregs[REG_TRAPNO], | |
1165 | uc->uc_mcontext.gregs[REG_ERR]); | |
e3b32540 | 1166 | printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]); |
3a27ad0b FB |
1167 | printf("\n"); |
1168 | longjmp(jmp_env, 1); | |
1169 | } | |
1170 | ||
1171 | void test_exceptions(void) | |
1172 | { | |
e3b32540 | 1173 | struct modify_ldt_ldt_s ldt; |
3a27ad0b FB |
1174 | struct sigaction act; |
1175 | volatile int val; | |
1176 | ||
1177 | act.sa_sigaction = sig_handler; | |
1178 | sigemptyset(&act.sa_mask); | |
1179 | act.sa_flags = SA_SIGINFO; | |
1180 | sigaction(SIGFPE, &act, NULL); | |
1181 | sigaction(SIGILL, &act, NULL); | |
1182 | sigaction(SIGSEGV, &act, NULL); | |
e3b32540 | 1183 | sigaction(SIGBUS, &act, NULL); |
3a27ad0b FB |
1184 | sigaction(SIGTRAP, &act, NULL); |
1185 | ||
1186 | /* test division by zero reporting */ | |
e3b32540 | 1187 | printf("DIVZ exception:\n"); |
3a27ad0b FB |
1188 | if (setjmp(jmp_env) == 0) { |
1189 | /* now divide by zero */ | |
1190 | v1 = 0; | |
1191 | v1 = 2 / v1; | |
1192 | } | |
1193 | ||
e3b32540 | 1194 | printf("BOUND exception:\n"); |
3a27ad0b FB |
1195 | if (setjmp(jmp_env) == 0) { |
1196 | /* bound exception */ | |
1197 | tab[0] = 1; | |
1198 | tab[1] = 10; | |
1199 | asm volatile ("bound %0, %1" : : "r" (11), "m" (tab)); | |
1200 | } | |
1201 | ||
e3b32540 FB |
1202 | printf("segment exceptions:\n"); |
1203 | if (setjmp(jmp_env) == 0) { | |
1204 | /* load an invalid segment */ | |
1205 | asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1)); | |
1206 | } | |
1207 | if (setjmp(jmp_env) == 0) { | |
1208 | /* null data segment is valid */ | |
1209 | asm volatile ("movl %0, %%fs" : : "r" (3)); | |
1210 | /* null stack segment */ | |
1211 | asm volatile ("movl %0, %%ss" : : "r" (3)); | |
1212 | } | |
1213 | ||
1214 | ldt.entry_number = 1; | |
1215 | ldt.base_addr = (unsigned long)&seg_data1; | |
1216 | ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12; | |
1217 | ldt.seg_32bit = 1; | |
1218 | ldt.contents = MODIFY_LDT_CONTENTS_DATA; | |
1219 | ldt.read_exec_only = 0; | |
1220 | ldt.limit_in_pages = 1; | |
1221 | ldt.seg_not_present = 1; | |
1222 | ldt.useable = 1; | |
1223 | modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */ | |
1224 | ||
1225 | if (setjmp(jmp_env) == 0) { | |
1226 | /* segment not present */ | |
1227 | asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1))); | |
1228 | } | |
1229 | ||
3a27ad0b | 1230 | /* test SEGV reporting */ |
e3b32540 | 1231 | printf("PF exception:\n"); |
3a27ad0b | 1232 | if (setjmp(jmp_env) == 0) { |
e3b32540 | 1233 | val = 1; |
3a27ad0b FB |
1234 | /* now store in an invalid address */ |
1235 | *(char *)0x1234 = 1; | |
1236 | } | |
1237 | ||
1238 | /* test SEGV reporting */ | |
e3b32540 | 1239 | printf("PF exception:\n"); |
3a27ad0b | 1240 | if (setjmp(jmp_env) == 0) { |
e3b32540 | 1241 | val = 1; |
3a27ad0b FB |
1242 | /* read from an invalid address */ |
1243 | v1 = *(char *)0x1234; | |
1244 | } | |
1245 | ||
3a27ad0b FB |
1246 | /* test illegal instruction reporting */ |
1247 | printf("UD2 exception:\n"); | |
1248 | if (setjmp(jmp_env) == 0) { | |
1249 | /* now execute an invalid instruction */ | |
1250 | asm volatile("ud2"); | |
1251 | } | |
1252 | ||
1253 | printf("INT exception:\n"); | |
1254 | if (setjmp(jmp_env) == 0) { | |
1255 | asm volatile ("int $0xfd"); | |
1256 | } | |
e3b32540 FB |
1257 | if (setjmp(jmp_env) == 0) { |
1258 | asm volatile ("int $0x01"); | |
1259 | } | |
1260 | if (setjmp(jmp_env) == 0) { | |
1261 | asm volatile (".byte 0xcd, 0x03"); | |
1262 | } | |
1263 | if (setjmp(jmp_env) == 0) { | |
1264 | asm volatile ("int $0x04"); | |
1265 | } | |
1266 | if (setjmp(jmp_env) == 0) { | |
1267 | asm volatile ("int $0x05"); | |
1268 | } | |
3a27ad0b FB |
1269 | |
1270 | printf("INT3 exception:\n"); | |
1271 | if (setjmp(jmp_env) == 0) { | |
1272 | asm volatile ("int3"); | |
1273 | } | |
1274 | ||
1275 | printf("CLI exception:\n"); | |
1276 | if (setjmp(jmp_env) == 0) { | |
1277 | asm volatile ("cli"); | |
1278 | } | |
1279 | ||
1280 | printf("STI exception:\n"); | |
1281 | if (setjmp(jmp_env) == 0) { | |
1282 | asm volatile ("cli"); | |
1283 | } | |
1284 | ||
1285 | printf("INTO exception:\n"); | |
1286 | if (setjmp(jmp_env) == 0) { | |
1287 | /* overflow exception */ | |
1288 | asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff)); | |
1289 | } | |
1290 | ||
1291 | printf("OUTB exception:\n"); | |
1292 | if (setjmp(jmp_env) == 0) { | |
1293 | asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0)); | |
1294 | } | |
1295 | ||
1296 | printf("INB exception:\n"); | |
1297 | if (setjmp(jmp_env) == 0) { | |
1298 | asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321)); | |
1299 | } | |
1300 | ||
1301 | printf("REP OUTSB exception:\n"); | |
1302 | if (setjmp(jmp_env) == 0) { | |
1303 | asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1)); | |
1304 | } | |
1305 | ||
1306 | printf("REP INSB exception:\n"); | |
1307 | if (setjmp(jmp_env) == 0) { | |
1308 | asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1)); | |
1309 | } | |
1310 | ||
1311 | printf("HLT exception:\n"); | |
1312 | if (setjmp(jmp_env) == 0) { | |
1313 | asm volatile ("hlt"); | |
1314 | } | |
1315 | ||
1316 | printf("single step exception:\n"); | |
1317 | val = 0; | |
1318 | if (setjmp(jmp_env) == 0) { | |
1319 | asm volatile ("pushf\n" | |
1320 | "orl $0x00100, (%%esp)\n" | |
1321 | "popf\n" | |
1322 | "movl $0xabcd, %0\n" | |
1323 | "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory"); | |
1324 | } | |
1325 | printf("val=0x%x\n", val); | |
1326 | } | |
1327 | ||
1328 | /* self modifying code test */ | |
1329 | uint8_t code[] = { | |
1330 | 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */ | |
1331 | 0xc3, /* ret */ | |
1332 | }; | |
1333 | ||
d1fe2b24 FB |
1334 | typedef int FuncType(void); |
1335 | ||
3a27ad0b FB |
1336 | void test_self_modifying_code(void) |
1337 | { | |
d1fe2b24 | 1338 | int i; |
3a27ad0b | 1339 | |
3a27ad0b | 1340 | printf("self modifying code:\n"); |
d1fe2b24 FB |
1341 | printf("func1 = 0x%x\n", ((FuncType *)code)()); |
1342 | for(i = 2; i <= 4; i++) { | |
1343 | code[1] = i; | |
1344 | printf("func%d = 0x%x\n", i, ((FuncType *)code)()); | |
1345 | } | |
3a27ad0b FB |
1346 | } |
1347 | ||
4d1135e4 FB |
1348 | static void *call_end __init_call = NULL; |
1349 | ||
1350 | int main(int argc, char **argv) | |
1351 | { | |
1352 | void **ptr; | |
1353 | void (*func)(void); | |
4b74fe1f | 1354 | |
4d1135e4 FB |
1355 | ptr = &call_start + 1; |
1356 | while (*ptr != NULL) { | |
1357 | func = *ptr++; | |
1358 | func(); | |
1359 | } | |
9d8e9c09 | 1360 | test_bsx(); |
d57c4e01 | 1361 | test_mul(); |
4d1135e4 | 1362 | test_jcc(); |
9d8e9c09 | 1363 | test_floats(); |
55480af8 | 1364 | test_bcd(); |
1a9353d2 | 1365 | test_xchg(); |
e1d4294a FB |
1366 | test_string(); |
1367 | test_misc(); | |
6dbad63e FB |
1368 | test_lea(); |
1369 | test_segs(); | |
e5918247 | 1370 | test_code16(); |
3a27ad0b FB |
1371 | test_vm86(); |
1372 | test_exceptions(); | |
1373 | test_self_modifying_code(); | |
4d1135e4 FB |
1374 | return 0; |
1375 | } |