]>
git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
95ec5d15099c8e424bec4f91cff54c847879c86e
6 #define xglue(x, y) x ## y
7 #define glue(x, y) xglue(x, y)
8 #define stringify(s) tostring(s)
18 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
20 static void *call_start __init_call
= NULL
;
22 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
25 #include "test-i386.h"
28 #include "test-i386.h"
31 #include "test-i386.h"
34 #include "test-i386.h"
37 #include "test-i386.h"
40 #include "test-i386.h"
44 #include "test-i386.h"
48 #include "test-i386.h"
53 #include "test-i386.h"
58 #include "test-i386.h"
63 #include "test-i386.h"
68 #include "test-i386.h"
71 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
74 #include "test-i386-shift.h"
77 #include "test-i386-shift.h"
80 #include "test-i386-shift.h"
83 #include "test-i386-shift.h"
86 #include "test-i386-shift.h"
90 #include "test-i386-shift.h"
94 #include "test-i386-shift.h"
99 #include "test-i386-shift.h"
104 #include "test-i386-shift.h"
106 /* XXX: should be more precise ? */
108 #define CC_MASK (CC_C)
112 #include "test-i386-shift.h"
116 #include "test-i386-shift.h"
120 #include "test-i386-shift.h"
124 #include "test-i386-shift.h"
126 /* lea test (modrm support) */
127 #define TEST_LEA(STR)\
129 asm("leal " STR ", %0"\
131 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
132 printf("lea %s = %08x\n", STR, res);\
135 #define TEST_LEA16(STR)\
137 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
139 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
140 printf("lea %s = %08x\n", STR, res);\
146 int eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
163 TEST_LEA("0x40(%%eax)");
164 TEST_LEA("0x40(%%ebx)");
165 TEST_LEA("0x40(%%ecx)");
166 TEST_LEA("0x40(%%edx)");
167 TEST_LEA("0x40(%%esi)");
168 TEST_LEA("0x40(%%edi)");
170 TEST_LEA("0x4000(%%eax)");
171 TEST_LEA("0x4000(%%ebx)");
172 TEST_LEA("0x4000(%%ecx)");
173 TEST_LEA("0x4000(%%edx)");
174 TEST_LEA("0x4000(%%esi)");
175 TEST_LEA("0x4000(%%edi)");
177 TEST_LEA("(%%eax, %%ecx)");
178 TEST_LEA("(%%ebx, %%edx)");
179 TEST_LEA("(%%ecx, %%ecx)");
180 TEST_LEA("(%%edx, %%ecx)");
181 TEST_LEA("(%%esi, %%ecx)");
182 TEST_LEA("(%%edi, %%ecx)");
184 TEST_LEA("0x40(%%eax, %%ecx)");
185 TEST_LEA("0x4000(%%ebx, %%edx)");
187 TEST_LEA("(%%ecx, %%ecx, 2)");
188 TEST_LEA("(%%edx, %%ecx, 4)");
189 TEST_LEA("(%%esi, %%ecx, 8)");
191 TEST_LEA("(,%%eax, 2)");
192 TEST_LEA("(,%%ebx, 4)");
193 TEST_LEA("(,%%ecx, 8)");
195 TEST_LEA("0x40(,%%eax, 2)");
196 TEST_LEA("0x40(,%%ebx, 4)");
197 TEST_LEA("0x40(,%%ecx, 8)");
200 TEST_LEA("-10(%%ecx, %%ecx, 2)");
201 TEST_LEA("-10(%%edx, %%ecx, 4)");
202 TEST_LEA("-10(%%esi, %%ecx, 8)");
204 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
205 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
206 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
208 /* limited 16 bit addressing test */
209 TEST_LEA16("0x4000");
210 TEST_LEA16("(%%bx)");
211 TEST_LEA16("(%%si)");
212 TEST_LEA16("(%%di)");
213 TEST_LEA16("0x40(%%bx)");
214 TEST_LEA16("0x40(%%si)");
215 TEST_LEA16("0x40(%%di)");
216 TEST_LEA16("0x4000(%%bx)");
217 TEST_LEA16("0x4000(%%si)");
218 TEST_LEA16("(%%bx,%%si)");
219 TEST_LEA16("(%%bx,%%di)");
220 TEST_LEA16("0x40(%%bx,%%si)");
221 TEST_LEA16("0x40(%%bx,%%di)");
222 TEST_LEA16("0x4000(%%bx,%%si)");
223 TEST_LEA16("0x4000(%%bx,%%di)");
226 #define TEST_JCC(JCC, v1, v2)\
228 asm("movl $1, %0\n\t"\
234 : "r" (v1), "r" (v2));\
235 printf("%-10s %d\n", JCC, res);\
238 /* various jump tests */
243 TEST_JCC("jne", 1, 1);
244 TEST_JCC("jne", 1, 0);
246 TEST_JCC("je", 1, 1);
247 TEST_JCC("je", 1, 0);
249 TEST_JCC("jl", 1, 1);
250 TEST_JCC("jl", 1, 0);
251 TEST_JCC("jl", 1, -1);
253 TEST_JCC("jle", 1, 1);
254 TEST_JCC("jle", 1, 0);
255 TEST_JCC("jle", 1, -1);
257 TEST_JCC("jge", 1, 1);
258 TEST_JCC("jge", 1, 0);
259 TEST_JCC("jge", -1, 1);
261 TEST_JCC("jg", 1, 1);
262 TEST_JCC("jg", 1, 0);
263 TEST_JCC("jg", 1, -1);
265 TEST_JCC("jb", 1, 1);
266 TEST_JCC("jb", 1, 0);
267 TEST_JCC("jb", 1, -1);
269 TEST_JCC("jbe", 1, 1);
270 TEST_JCC("jbe", 1, 0);
271 TEST_JCC("jbe", 1, -1);
273 TEST_JCC("jae", 1, 1);
274 TEST_JCC("jae", 1, 0);
275 TEST_JCC("jae", 1, -1);
277 TEST_JCC("ja", 1, 1);
278 TEST_JCC("ja", 1, 0);
279 TEST_JCC("ja", 1, -1);
282 TEST_JCC("jp", 1, 1);
283 TEST_JCC("jp", 1, 0);
285 TEST_JCC("jnp", 1, 1);
286 TEST_JCC("jnp", 1, 0);
288 TEST_JCC("jo", 0x7fffffff, 0);
289 TEST_JCC("jo", 0x7fffffff, -1);
291 TEST_JCC("jno", 0x7fffffff, 0);
292 TEST_JCC("jno", 0x7fffffff, -1);
294 TEST_JCC("js", 0, 1);
295 TEST_JCC("js", 0, -1);
296 TEST_JCC("js", 0, 0);
298 TEST_JCC("jns", 0, 1);
299 TEST_JCC("jns", 0, -1);
300 TEST_JCC("jns", 0, 0);
304 #define CC_MASK (CC_O | CC_C)
307 #include "test-i386-muldiv.h"
310 #include "test-i386-muldiv.h"
316 #include "test-i386-muldiv.h"
319 #include "test-i386-muldiv.h"
321 void test_imulw2(int op0
, int op1
)
323 int res
, s1
, s0
, flags
;
333 : "=q" (res
), "=g" (flags
)
334 : "q" (s1
), "0" (res
), "1" (flags
));
335 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
336 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
339 void test_imull2(int op0
, int op1
)
341 int res
, s1
, s0
, flags
;
351 : "=q" (res
), "=g" (flags
)
352 : "q" (s1
), "0" (res
), "1" (flags
));
353 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
354 "imull", s0
, s1
, res
, flags
& CC_MASK
);
359 test_imulb(0x1234561d, 4);
361 test_imulb(0x80, 0x80);
362 test_imulb(0x10, 0x10);
364 test_imulw(0, 0x1234001d, 45);
365 test_imulw(0, 23, -45);
366 test_imulw(0, 0x8000, 0x8000);
367 test_imulw(0, 0x100, 0x100);
369 test_imull(0, 0x1234001d, 45);
370 test_imull(0, 23, -45);
371 test_imull(0, 0x80000000, 0x80000000);
372 test_imull(0, 0x10000, 0x10000);
374 test_mulb(0x1234561d, 4);
376 test_mulb(0x80, 0x80);
377 test_mulb(0x10, 0x10);
379 test_mulw(0, 0x1234001d, 45);
380 test_mulw(0, 23, -45);
381 test_mulw(0, 0x8000, 0x8000);
382 test_mulw(0, 0x100, 0x100);
384 test_mull(0, 0x1234001d, 45);
385 test_mull(0, 23, -45);
386 test_mull(0, 0x80000000, 0x80000000);
387 test_mull(0, 0x10000, 0x10000);
389 test_imulw2(0x1234001d, 45);
390 test_imulw2(23, -45);
391 test_imulw2(0x8000, 0x8000);
392 test_imulw2(0x100, 0x100);
394 test_imull2(0x1234001d, 45);
395 test_imull2(23, -45);
396 test_imull2(0x80000000, 0x80000000);
397 test_imull2(0x10000, 0x10000);
399 test_idivb(0x12341678, 0x127e);
400 test_idivb(0x43210123, -5);
401 test_idivb(0x12340004, -1);
403 test_idivw(0, 0x12345678, 12347);
404 test_idivw(0, -23223, -45);
405 test_idivw(0, 0x12348000, -1);
406 test_idivw(0x12343, 0x12345678, 0x81238567);
408 test_idivl(0, 0x12345678, 12347);
409 test_idivl(0, -233223, -45);
410 test_idivl(0, 0x80000000, -1);
411 test_idivl(0x12343, 0x12345678, 0x81234567);
413 test_divb(0x12341678, 0x127e);
414 test_divb(0x43210123, -5);
415 test_divb(0x12340004, -1);
417 test_divw(0, 0x12345678, 12347);
418 test_divw(0, -23223, -45);
419 test_divw(0, 0x12348000, -1);
420 test_divw(0x12343, 0x12345678, 0x81238567);
422 test_divl(0, 0x12345678, 12347);
423 test_divl(0, -233223, -45);
424 test_divl(0, 0x80000000, -1);
425 test_divl(0x12343, 0x12345678, 0x81234567);
428 #define TEST_BSX(op, size, op0)\
432 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
433 : "=r" (res), "=q" (resz)\
435 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
440 TEST_BSX(bsrw
, "w", 0);
441 TEST_BSX(bsrw
, "w", 0x12340128);
442 TEST_BSX(bsrl
, "", 0);
443 TEST_BSX(bsrl
, "", 0x00340128);
444 TEST_BSX(bsfw
, "w", 0);
445 TEST_BSX(bsfw
, "w", 0x12340128);
446 TEST_BSX(bsfl
, "", 0);
447 TEST_BSX(bsfl
, "", 0x00340128);
450 /**********************************************/
452 void test_fops(double a
, double b
)
454 printf("a=%f b=%f a+b=%f\n", a
, b
, a
+ b
);
455 printf("a=%f b=%f a-b=%f\n", a
, b
, a
- b
);
456 printf("a=%f b=%f a*b=%f\n", a
, b
, a
* b
);
457 printf("a=%f b=%f a/b=%f\n", a
, b
, a
/ b
);
458 printf("a=%f b=%f fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
459 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
460 printf("a=%f sin(a)=%f\n", a
, sin(a
));
461 printf("a=%f cos(a)=%f\n", a
, cos(a
));
462 printf("a=%f tan(a)=%f\n", a
, tan(a
));
463 printf("a=%f log(a)=%f\n", a
, log(a
));
464 printf("a=%f exp(a)=%f\n", a
, exp(a
));
465 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
466 /* just to test some op combining */
467 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
468 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
469 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
473 void test_fcmp(double a
, double b
)
475 printf("(%f<%f)=%d\n",
477 printf("(%f<=%f)=%d\n",
479 printf("(%f==%f)=%d\n",
481 printf("(%f>%f)=%d\n",
483 printf("(%f<=%f)=%d\n",
487 void test_fcvt(double a
)
494 printf("(float)%f = %f\n", a
, fa
);
495 printf("(long double)%f = %Lf\n", a
, la
);
496 printf("a=%016Lx\n", *(long long *)&a
);
497 printf("la=%016Lx %04x\n", *(long long *)&la
,
498 *(unsigned short *)((char *)(&la
) + 8));
499 printf("a=%f floor(a)=%f\n", a
, floor(a
));
500 printf("a=%f ceil(a)=%f\n", a
, ceil(a
));
501 printf("a=%f rint(a)=%f\n", a
, rint(a
));
505 asm("fld" #N : "=t" (a)); \
506 printf("fld" #N "= %f\n", a);
508 void test_fconst(void)
520 void test_fbcd(double a
)
522 unsigned short bcd
[5];
525 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
526 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
527 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
528 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
531 void test_floats(void)
542 test_fbcd(1234567890123456);
543 test_fbcd(-123451234567890);
546 /**********************************************/
548 #define TEST_BCD(op, op0, cc_in, cc_mask)\
558 : "=a" (res), "=g" (flags)\
559 : "0" (res), "1" (flags));\
560 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
561 #op, op0, res, cc_in, flags & cc_mask);\
566 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
567 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
568 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
569 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
570 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
571 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
572 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
573 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
574 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
575 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
576 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
577 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
578 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
580 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
581 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
582 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
583 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
584 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
585 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
586 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
587 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
588 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
589 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
590 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
591 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
592 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
594 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
595 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
596 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
597 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
598 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
599 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
600 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
601 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
603 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
604 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
605 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
606 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
607 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
608 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
609 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
610 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
612 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
613 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
616 /**********************************************/
617 /* segmentation tests */
620 #include <linux/unistd.h>
622 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
624 uint8_t seg_data1
[4096];
625 uint8_t seg_data2
[4096];
627 #define MK_SEL(n) (((n) << 3) | 4)
629 /* NOTE: we use Linux modify_ldt syscall */
632 struct modify_ldt_ldt_s ldt
;
633 long long ldt_table
[3];
637 ldt
.entry_number
= 1;
638 ldt
.base_addr
= (unsigned long)&seg_data1
;
639 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
641 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
642 ldt
.read_exec_only
= 0;
643 ldt
.limit_in_pages
= 1;
644 ldt
.seg_not_present
= 0;
646 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
648 ldt
.entry_number
= 2;
649 ldt
.base_addr
= (unsigned long)&seg_data2
;
650 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
652 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
653 ldt
.read_exec_only
= 0;
654 ldt
.limit_in_pages
= 1;
655 ldt
.seg_not_present
= 0;
657 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
659 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
661 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
663 /* do some tests with fs or gs */
664 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
665 asm volatile ("movl %0, %%gs" : : "r" (MK_SEL(2)));
670 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
671 printf("FS[1] = %02x\n", res
);
673 asm volatile ("gs movzbl 0x1, %0" : "=r" (res
));
674 printf("GS[1] = %02x\n", res
);
676 /* tests with ds/ss (implicit segment case) */
678 asm volatile ("pushl %%ebp\n\t"
683 "movzbl (%%ebp), %1\n\t"
686 : "=r" (res
), "=r" (res2
)
687 : "r" (MK_SEL(1)), "r" (&tmp
));
688 printf("DS[1] = %02x\n", res
);
689 printf("SS[tmp] = %02x\n", res2
);
692 static void *call_end __init_call
= NULL
;
694 int main(int argc
, char **argv
)
699 ptr
= &call_start
+ 1;
700 while (*ptr
!= NULL
) {