]>
git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
9 #include <sys/ucontext.h>
16 #define xglue(x, y) x ## y
17 #define glue(x, y) xglue(x, y)
18 #define stringify(s) tostring(s)
19 #define tostring(s) #s
28 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
30 static void *call_start __init_call
= NULL
;
32 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
35 #include "test-i386.h"
38 #include "test-i386.h"
41 #include "test-i386.h"
44 #include "test-i386.h"
47 #include "test-i386.h"
50 #include "test-i386.h"
54 #include "test-i386.h"
58 #include "test-i386.h"
63 #include "test-i386.h"
68 #include "test-i386.h"
73 #include "test-i386.h"
78 #include "test-i386.h"
81 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
84 #include "test-i386-shift.h"
87 #include "test-i386-shift.h"
90 #include "test-i386-shift.h"
93 #include "test-i386-shift.h"
96 #include "test-i386-shift.h"
100 #include "test-i386-shift.h"
104 #include "test-i386-shift.h"
109 #include "test-i386-shift.h"
114 #include "test-i386-shift.h"
116 /* XXX: should be more precise ? */
118 #define CC_MASK (CC_C)
122 #include "test-i386-shift.h"
126 #include "test-i386-shift.h"
130 #include "test-i386-shift.h"
134 #include "test-i386-shift.h"
136 /* lea test (modrm support) */
137 #define TEST_LEA(STR)\
139 asm("leal " STR ", %0"\
141 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
142 printf("lea %s = %08x\n", STR, res);\
145 #define TEST_LEA16(STR)\
147 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
149 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
150 printf("lea %s = %08x\n", STR, res);\
156 int eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
173 TEST_LEA("0x40(%%eax)");
174 TEST_LEA("0x40(%%ebx)");
175 TEST_LEA("0x40(%%ecx)");
176 TEST_LEA("0x40(%%edx)");
177 TEST_LEA("0x40(%%esi)");
178 TEST_LEA("0x40(%%edi)");
180 TEST_LEA("0x4000(%%eax)");
181 TEST_LEA("0x4000(%%ebx)");
182 TEST_LEA("0x4000(%%ecx)");
183 TEST_LEA("0x4000(%%edx)");
184 TEST_LEA("0x4000(%%esi)");
185 TEST_LEA("0x4000(%%edi)");
187 TEST_LEA("(%%eax, %%ecx)");
188 TEST_LEA("(%%ebx, %%edx)");
189 TEST_LEA("(%%ecx, %%ecx)");
190 TEST_LEA("(%%edx, %%ecx)");
191 TEST_LEA("(%%esi, %%ecx)");
192 TEST_LEA("(%%edi, %%ecx)");
194 TEST_LEA("0x40(%%eax, %%ecx)");
195 TEST_LEA("0x4000(%%ebx, %%edx)");
197 TEST_LEA("(%%ecx, %%ecx, 2)");
198 TEST_LEA("(%%edx, %%ecx, 4)");
199 TEST_LEA("(%%esi, %%ecx, 8)");
201 TEST_LEA("(,%%eax, 2)");
202 TEST_LEA("(,%%ebx, 4)");
203 TEST_LEA("(,%%ecx, 8)");
205 TEST_LEA("0x40(,%%eax, 2)");
206 TEST_LEA("0x40(,%%ebx, 4)");
207 TEST_LEA("0x40(,%%ecx, 8)");
210 TEST_LEA("-10(%%ecx, %%ecx, 2)");
211 TEST_LEA("-10(%%edx, %%ecx, 4)");
212 TEST_LEA("-10(%%esi, %%ecx, 8)");
214 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
215 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
216 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
218 /* limited 16 bit addressing test */
219 TEST_LEA16("0x4000");
220 TEST_LEA16("(%%bx)");
221 TEST_LEA16("(%%si)");
222 TEST_LEA16("(%%di)");
223 TEST_LEA16("0x40(%%bx)");
224 TEST_LEA16("0x40(%%si)");
225 TEST_LEA16("0x40(%%di)");
226 TEST_LEA16("0x4000(%%bx)");
227 TEST_LEA16("0x4000(%%si)");
228 TEST_LEA16("(%%bx,%%si)");
229 TEST_LEA16("(%%bx,%%di)");
230 TEST_LEA16("0x40(%%bx,%%si)");
231 TEST_LEA16("0x40(%%bx,%%di)");
232 TEST_LEA16("0x4000(%%bx,%%si)");
233 TEST_LEA16("0x4000(%%bx,%%di)");
236 #define TEST_JCC(JCC, v1, v2)\
239 asm("movl $1, %0\n\t"\
245 : "r" (v1), "r" (v2));\
246 printf("%-10s %d\n", "j" JCC, res);\
248 asm("movl $0, %0\n\t"\
250 "set" JCC " %b0\n\t"\
252 : "r" (v1), "r" (v2));\
253 printf("%-10s %d\n", "set" JCC, res);\
255 asm("movl $0x12345678, %0\n\t"\
257 "cmov" JCC "l %3, %0\n\t"\
259 : "r" (v1), "r" (v2), "m" (1));\
260 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
261 asm("movl $0x12345678, %0\n\t"\
263 "cmov" JCC "w %w3, %w0\n\t"\
265 : "r" (v1), "r" (v2), "r" (1));\
266 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
270 /* various jump tests */
273 TEST_JCC("ne", 1, 1);
274 TEST_JCC("ne", 1, 0);
281 TEST_JCC("l", 1, -1);
283 TEST_JCC("le", 1, 1);
284 TEST_JCC("le", 1, 0);
285 TEST_JCC("le", 1, -1);
287 TEST_JCC("ge", 1, 1);
288 TEST_JCC("ge", 1, 0);
289 TEST_JCC("ge", -1, 1);
293 TEST_JCC("g", 1, -1);
297 TEST_JCC("b", 1, -1);
299 TEST_JCC("be", 1, 1);
300 TEST_JCC("be", 1, 0);
301 TEST_JCC("be", 1, -1);
303 TEST_JCC("ae", 1, 1);
304 TEST_JCC("ae", 1, 0);
305 TEST_JCC("ae", 1, -1);
309 TEST_JCC("a", 1, -1);
315 TEST_JCC("np", 1, 1);
316 TEST_JCC("np", 1, 0);
318 TEST_JCC("o", 0x7fffffff, 0);
319 TEST_JCC("o", 0x7fffffff, -1);
321 TEST_JCC("no", 0x7fffffff, 0);
322 TEST_JCC("no", 0x7fffffff, -1);
325 TEST_JCC("s", 0, -1);
328 TEST_JCC("ns", 0, 1);
329 TEST_JCC("ns", 0, -1);
330 TEST_JCC("ns", 0, 0);
334 #define CC_MASK (CC_O | CC_C)
337 #include "test-i386-muldiv.h"
340 #include "test-i386-muldiv.h"
346 #include "test-i386-muldiv.h"
349 #include "test-i386-muldiv.h"
351 void test_imulw2(int op0
, int op1
)
353 int res
, s1
, s0
, flags
;
363 : "=q" (res
), "=g" (flags
)
364 : "q" (s1
), "0" (res
), "1" (flags
));
365 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
366 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
369 void test_imull2(int op0
, int op1
)
371 int res
, s1
, s0
, flags
;
381 : "=q" (res
), "=g" (flags
)
382 : "q" (s1
), "0" (res
), "1" (flags
));
383 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
384 "imull", s0
, s1
, res
, flags
& CC_MASK
);
389 test_imulb(0x1234561d, 4);
391 test_imulb(0x80, 0x80);
392 test_imulb(0x10, 0x10);
394 test_imulw(0, 0x1234001d, 45);
395 test_imulw(0, 23, -45);
396 test_imulw(0, 0x8000, 0x8000);
397 test_imulw(0, 0x100, 0x100);
399 test_imull(0, 0x1234001d, 45);
400 test_imull(0, 23, -45);
401 test_imull(0, 0x80000000, 0x80000000);
402 test_imull(0, 0x10000, 0x10000);
404 test_mulb(0x1234561d, 4);
406 test_mulb(0x80, 0x80);
407 test_mulb(0x10, 0x10);
409 test_mulw(0, 0x1234001d, 45);
410 test_mulw(0, 23, -45);
411 test_mulw(0, 0x8000, 0x8000);
412 test_mulw(0, 0x100, 0x100);
414 test_mull(0, 0x1234001d, 45);
415 test_mull(0, 23, -45);
416 test_mull(0, 0x80000000, 0x80000000);
417 test_mull(0, 0x10000, 0x10000);
419 test_imulw2(0x1234001d, 45);
420 test_imulw2(23, -45);
421 test_imulw2(0x8000, 0x8000);
422 test_imulw2(0x100, 0x100);
424 test_imull2(0x1234001d, 45);
425 test_imull2(23, -45);
426 test_imull2(0x80000000, 0x80000000);
427 test_imull2(0x10000, 0x10000);
429 test_idivb(0x12341678, 0x127e);
430 test_idivb(0x43210123, -5);
431 test_idivb(0x12340004, -1);
433 test_idivw(0, 0x12345678, 12347);
434 test_idivw(0, -23223, -45);
435 test_idivw(0, 0x12348000, -1);
436 test_idivw(0x12343, 0x12345678, 0x81238567);
438 test_idivl(0, 0x12345678, 12347);
439 test_idivl(0, -233223, -45);
440 test_idivl(0, 0x80000000, -1);
441 test_idivl(0x12343, 0x12345678, 0x81234567);
443 test_divb(0x12341678, 0x127e);
444 test_divb(0x43210123, -5);
445 test_divb(0x12340004, -1);
447 test_divw(0, 0x12345678, 12347);
448 test_divw(0, -23223, -45);
449 test_divw(0, 0x12348000, -1);
450 test_divw(0x12343, 0x12345678, 0x81238567);
452 test_divl(0, 0x12345678, 12347);
453 test_divl(0, -233223, -45);
454 test_divl(0, 0x80000000, -1);
455 test_divl(0x12343, 0x12345678, 0x81234567);
458 #define TEST_BSX(op, size, op0)\
462 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
463 : "=r" (res), "=q" (resz)\
465 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
470 TEST_BSX(bsrw
, "w", 0);
471 TEST_BSX(bsrw
, "w", 0x12340128);
472 TEST_BSX(bsrl
, "", 0);
473 TEST_BSX(bsrl
, "", 0x00340128);
474 TEST_BSX(bsfw
, "w", 0);
475 TEST_BSX(bsfw
, "w", 0x12340128);
476 TEST_BSX(bsfl
, "", 0);
477 TEST_BSX(bsfl
, "", 0x00340128);
480 /**********************************************/
482 void test_fops(double a
, double 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 a/b=%f\n", a
, b
, a
/ b
);
488 printf("a=%f b=%f fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
489 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
490 printf("a=%f sin(a)=%f\n", a
, sin(a
));
491 printf("a=%f cos(a)=%f\n", a
, cos(a
));
492 printf("a=%f tan(a)=%f\n", a
, tan(a
));
493 printf("a=%f log(a)=%f\n", a
, log(a
));
494 printf("a=%f exp(a)=%f\n", a
, exp(a
));
495 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
496 /* just to test some op combining */
497 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
498 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
499 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
503 void test_fcmp(double a
, double b
)
505 printf("(%f<%f)=%d\n",
507 printf("(%f<=%f)=%d\n",
509 printf("(%f==%f)=%d\n",
511 printf("(%f>%f)=%d\n",
513 printf("(%f<=%f)=%d\n",
517 /* test f(u)comi instruction */
523 printf("fcomi(%f %f)=%08x\n", a
, b
, eflags
& (CC_Z
| CC_P
| CC_C
));
527 void test_fcvt(double a
)
540 printf("(float)%f = %f\n", a
, fa
);
541 printf("(long double)%f = %Lf\n", a
, la
);
542 printf("a=%016Lx\n", *(long long *)&a
);
543 printf("la=%016Lx %04x\n", *(long long *)&la
,
544 *(unsigned short *)((char *)(&la
) + 8));
546 /* test all roundings */
547 asm volatile ("fstcw %0" : "=m" (fpuc
));
549 asm volatile ("fldcw %0" : : "m" ((fpuc
& ~0x0c00) | (i
<< 10)));
550 asm volatile ("fist %0" : "=m" (wa
) : "t" (a
));
551 asm volatile ("fistl %0" : "=m" (ia
) : "t" (a
));
552 asm volatile ("fistpll %0" : "=m" (lla
) : "t" (a
) : "st");
553 asm volatile ("frndint ; fstl %0" : "=m" (ra
) : "t" (a
));
554 asm volatile ("fldcw %0" : : "m" (fpuc
));
555 printf("(short)a = %d\n", wa
);
556 printf("(int)a = %d\n", ia
);
557 printf("(int64_t)a = %Ld\n", lla
);
558 printf("rint(a) = %f\n", ra
);
563 asm("fld" #N : "=t" (a)); \
564 printf("fld" #N "= %f\n", a);
566 void test_fconst(void)
578 void test_fbcd(double a
)
580 unsigned short bcd
[5];
583 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
584 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
585 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
586 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
589 #define TEST_ENV(env, prefix)\
591 memset((env), 0xaa, sizeof(*(env)));\
593 prefix "fnstenv %1\n"\
594 prefix "fldenv %1\n"\
595 : "=t" (res) : "m" (*(env)));\
596 printf("res=%f\n", res);\
597 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
599 (env)->fpus & 0xff00,\
601 memset((env), 0xaa, sizeof(*(env)));\
603 prefix "fnsave %1\n"\
604 prefix "frstor %1\n"\
605 : "=t" (res) : "m" (*(env)));\
606 printf("res=%f\n", res);\
607 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
609 (env)->fpus & 0xff00,\
611 printf("ST(0) = %Lf\n",\
617 struct __attribute__((packed
)) {
625 long double fpregs
[8];
627 struct __attribute__((packed
)) {
632 long double fpregs
[8];
636 TEST_ENV(&float_env16
, "data16 ");
637 TEST_ENV(&float_env32
, "");
640 void test_floats(void)
654 test_fbcd(1234567890123456);
655 test_fbcd(-123451234567890);
659 /**********************************************/
661 #define TEST_BCD(op, op0, cc_in, cc_mask)\
671 : "=a" (res), "=g" (flags)\
672 : "0" (res), "1" (flags));\
673 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
674 #op, op0, res, cc_in, flags & cc_mask);\
679 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
680 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
681 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
682 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
683 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
684 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
685 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
686 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
687 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
688 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
689 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
690 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
691 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
693 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
694 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
695 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
696 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
697 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
698 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
699 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
700 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
701 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
702 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
703 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
704 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
705 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
707 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
708 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
709 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
710 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
711 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
712 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
713 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
714 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
716 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
717 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
718 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
719 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
720 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
721 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
722 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
723 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
725 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
726 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
729 #define TEST_XCHG(op, size, opconst)\
734 asm(#op " %" size "0, %" size "1" \
735 : "=q" (op0), opconst (op1) \
736 : "0" (op0), "1" (op1));\
737 printf("%-10s A=%08x B=%08x\n",\
741 #define TEST_CMPXCHG(op, size, opconst, eax)\
746 asm(#op " %" size "0, %" size "1" \
747 : "=q" (op0), opconst (op1) \
748 : "0" (op0), "1" (op1), "a" (eax));\
749 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
750 #op, eax, op0, op1);\
755 TEST_XCHG(xchgl
, "", "=q");
756 TEST_XCHG(xchgw
, "w", "=q");
757 TEST_XCHG(xchgb
, "b", "=q");
759 TEST_XCHG(xchgl
, "", "=m");
760 TEST_XCHG(xchgw
, "w", "=m");
761 TEST_XCHG(xchgb
, "b", "=m");
763 TEST_XCHG(xaddl
, "", "=q");
764 TEST_XCHG(xaddw
, "w", "=q");
765 TEST_XCHG(xaddb
, "b", "=q");
767 TEST_XCHG(xaddl
, "", "=m");
768 TEST_XCHG(xaddw
, "w", "=m");
769 TEST_XCHG(xaddb
, "b", "=m");
771 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfbca7654);
772 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfbca7654);
773 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfbca7654);
775 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfffefdfc);
776 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfffefdfc);
777 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfffefdfc);
779 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfbca7654);
780 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfbca7654);
781 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfbca7654);
783 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfffefdfc);
784 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfffefdfc);
785 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfffefdfc);
788 /**********************************************/
789 /* segmentation tests */
792 #include <linux/unistd.h>
794 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
796 uint8_t seg_data1
[4096];
797 uint8_t seg_data2
[4096];
799 #define MK_SEL(n) (((n) << 3) | 7)
801 #define TEST_LR(op, size, seg, mask)\
805 asm (op " %" size "2, %" size "0\n" \
810 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
811 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
814 /* NOTE: we use Linux modify_ldt syscall */
817 struct modify_ldt_ldt_s ldt
;
818 long long ldt_table
[3];
824 } __attribute__((packed
)) segoff
;
826 ldt
.entry_number
= 1;
827 ldt
.base_addr
= (unsigned long)&seg_data1
;
828 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
830 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
831 ldt
.read_exec_only
= 0;
832 ldt
.limit_in_pages
= 1;
833 ldt
.seg_not_present
= 0;
835 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
837 ldt
.entry_number
= 2;
838 ldt
.base_addr
= (unsigned long)&seg_data2
;
839 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
841 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
842 ldt
.read_exec_only
= 0;
843 ldt
.limit_in_pages
= 1;
844 ldt
.seg_not_present
= 0;
846 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
848 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
853 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
856 /* do some tests with fs or gs */
857 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
858 asm volatile ("movl %0, %%gs" : : "r" (MK_SEL(2)));
863 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
864 printf("FS[1] = %02x\n", res
);
866 asm volatile ("gs movzbl 0x1, %0" : "=r" (res
));
867 printf("GS[1] = %02x\n", res
);
869 /* tests with ds/ss (implicit segment case) */
871 asm volatile ("pushl %%ebp\n\t"
876 "movzbl (%%ebp), %1\n\t"
879 : "=r" (res
), "=r" (res2
)
880 : "r" (MK_SEL(1)), "r" (&tmp
));
881 printf("DS[1] = %02x\n", res
);
882 printf("SS[tmp] = %02x\n", res2
);
884 segoff
.seg
= MK_SEL(2);
885 segoff
.offset
= 0xabcdef12;
886 asm volatile("lfs %2, %0\n\t"
888 : "=r" (res
), "=g" (res2
)
890 printf("FS:reg = %04x:%08x\n", res2
, res
);
892 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
893 TEST_LR("larl", "", MK_SEL(2), 0x0100);
894 TEST_LR("lslw", "w", MK_SEL(2), 0);
895 TEST_LR("lsll", "", MK_SEL(2), 0);
897 TEST_LR("larw", "w", 0xfff8, 0);
898 TEST_LR("larl", "", 0xfff8, 0);
899 TEST_LR("lslw", "w", 0xfff8, 0);
900 TEST_LR("lsll", "", 0xfff8, 0);
903 /* 16 bit code test */
904 extern char code16_start
, code16_end
;
905 extern char code16_func1
;
906 extern char code16_func2
;
907 extern char code16_func3
;
909 void test_code16(void)
911 struct modify_ldt_ldt_s ldt
;
914 /* build a code segment */
915 ldt
.entry_number
= 1;
916 ldt
.base_addr
= (unsigned long)&code16_start
;
917 ldt
.limit
= &code16_end
- &code16_start
;
919 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
920 ldt
.read_exec_only
= 0;
921 ldt
.limit_in_pages
= 0;
922 ldt
.seg_not_present
= 0;
924 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
926 /* call the first function */
927 asm volatile ("lcall %1, %2"
929 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
930 printf("func1() = 0x%08x\n", res
);
931 asm volatile ("lcall %2, %3"
932 : "=a" (res
), "=c" (res2
)
933 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
934 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
935 asm volatile ("lcall %1, %2"
937 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
938 printf("func3() = 0x%08x\n", res
);
941 extern char func_lret32
;
942 extern char func_iret32
;
949 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
951 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
952 printf("xlat: EAX=%08x\n", res
);
954 asm volatile ("pushl %%cs ; call %1"
956 : "m" (func_lret32
): "memory", "cc");
957 printf("func_lret32=%x\n", res
);
959 asm volatile ("pushfl ; pushl %%cs ; call %1"
961 : "m" (func_iret32
): "memory", "cc");
962 printf("func_iret32=%x\n", res
);
964 /* specific popl test */
965 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
967 printf("popl esp=%x\n", res
);
970 uint8_t str_buffer
[4096];
972 #define TEST_STRING1(OP, size, DF, REP)\
974 int esi, edi, eax, ecx, eflags;\
976 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
977 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
981 asm volatile ("pushl $0\n\t"\
988 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
989 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
990 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
991 REP #OP size, esi, edi, eax, ecx,\
992 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
995 #define TEST_STRING(OP, REP)\
996 TEST_STRING1(OP, "b", "", REP);\
997 TEST_STRING1(OP, "w", "", REP);\
998 TEST_STRING1(OP, "l", "", REP);\
999 TEST_STRING1(OP, "b", "std", REP);\
1000 TEST_STRING1(OP, "w", "std", REP);\
1001 TEST_STRING1(OP, "l", "std", REP)
1003 void test_string(void)
1006 for(i
= 0;i
< sizeof(str_buffer
); i
++)
1007 str_buffer
[i
] = i
+ 0x56;
1008 TEST_STRING(stos
, "");
1009 TEST_STRING(stos
, "rep ");
1010 TEST_STRING(lods
, ""); /* to verify stos */
1011 TEST_STRING(lods
, "rep ");
1012 TEST_STRING(movs
, "");
1013 TEST_STRING(movs
, "rep ");
1014 TEST_STRING(lods
, ""); /* to verify stos */
1016 /* XXX: better tests */
1017 TEST_STRING(scas
, "");
1018 TEST_STRING(scas
, "repz ");
1019 TEST_STRING(scas
, "repnz ");
1020 TEST_STRING(cmps
, "");
1021 TEST_STRING(cmps
, "repz ");
1022 TEST_STRING(cmps
, "repnz ");
1027 static inline void set_bit(uint8_t *a
, unsigned int bit
)
1029 a
[bit
/ 8] |= (1 << (bit
% 8));
1032 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
1034 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
1037 static inline void pushw(struct vm86_regs
*r
, int val
)
1039 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
1040 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
1043 #undef __syscall_return
1044 #define __syscall_return(type, res) \
1046 return (type) (res); \
1049 _syscall2(int, vm86
, int, func
, struct vm86plus_struct
*, v86
)
1051 extern char vm86_code_start
;
1052 extern char vm86_code_end
;
1054 #define VM86_CODE_CS 0x100
1055 #define VM86_CODE_IP 0x100
1057 void test_vm86(void)
1059 struct vm86plus_struct ctx
;
1060 struct vm86_regs
*r
;
1064 vm86_mem
= mmap((void *)0x00000000, 0x110000,
1065 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
1066 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
1067 if (vm86_mem
== MAP_FAILED
) {
1068 printf("ERROR: could not map vm86 memory");
1071 memset(&ctx
, 0, sizeof(ctx
));
1073 /* init basic registers */
1075 r
->eip
= VM86_CODE_IP
;
1084 r
->eflags
= VIF_MASK
;
1086 /* move code to proper address. We use the same layout as a .com
1088 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
1089 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
1091 /* mark int 0x21 as being emulated */
1092 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
1095 ret
= vm86(VM86_ENTER
, &ctx
);
1096 switch(VM86_TYPE(ret
)) {
1101 int_num
= VM86_ARG(ret
);
1102 if (int_num
!= 0x21)
1104 ah
= (r
->eax
>> 8) & 0xff;
1106 case 0x00: /* exit */
1108 case 0x02: /* write char */
1114 case 0x09: /* write string */
1117 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1124 r
->eax
= (r
->eax
& ~0xff) | '$';
1127 case 0xff: /* extension: write hex number in edx */
1128 printf("%08x\n", (int)r
->edx
);
1132 printf("unsupported int 0x%02x\n", int_num
);
1138 /* a signal came, we just ignore that */
1143 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1148 printf("VM86 end\n");
1149 munmap(vm86_mem
, 0x110000);
1152 /* exception tests */
1164 #define REG_TRAPNO TRAPNO
1172 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1174 struct ucontext
*uc
= puc
;
1176 printf("si_signo=%d si_errno=%d si_code=%d",
1177 info
->si_signo
, info
->si_errno
, info
->si_code
);
1178 printf(" si_addr=0x%08lx",
1179 (unsigned long)info
->si_addr
);
1182 printf("trapno=0x%02x err=0x%08x",
1183 uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1184 uc
->uc_mcontext
.gregs
[REG_ERR
]);
1185 printf(" EIP=0x%08x", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1187 longjmp(jmp_env
, 1);
1190 void test_exceptions(void)
1192 struct modify_ldt_ldt_s ldt
;
1193 struct sigaction act
;
1196 act
.sa_sigaction
= sig_handler
;
1197 sigemptyset(&act
.sa_mask
);
1198 act
.sa_flags
= SA_SIGINFO
;
1199 sigaction(SIGFPE
, &act
, NULL
);
1200 sigaction(SIGILL
, &act
, NULL
);
1201 sigaction(SIGSEGV
, &act
, NULL
);
1202 sigaction(SIGBUS
, &act
, NULL
);
1203 sigaction(SIGTRAP
, &act
, NULL
);
1205 /* test division by zero reporting */
1206 printf("DIVZ exception:\n");
1207 if (setjmp(jmp_env
) == 0) {
1208 /* now divide by zero */
1213 printf("BOUND exception:\n");
1214 if (setjmp(jmp_env
) == 0) {
1215 /* bound exception */
1218 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
));
1221 printf("segment exceptions:\n");
1222 if (setjmp(jmp_env
) == 0) {
1223 /* load an invalid segment */
1224 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1226 if (setjmp(jmp_env
) == 0) {
1227 /* null data segment is valid */
1228 asm volatile ("movl %0, %%fs" : : "r" (3));
1229 /* null stack segment */
1230 asm volatile ("movl %0, %%ss" : : "r" (3));
1233 ldt
.entry_number
= 1;
1234 ldt
.base_addr
= (unsigned long)&seg_data1
;
1235 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1237 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1238 ldt
.read_exec_only
= 0;
1239 ldt
.limit_in_pages
= 1;
1240 ldt
.seg_not_present
= 1;
1242 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1244 if (setjmp(jmp_env
) == 0) {
1245 /* segment not present */
1246 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1249 /* test SEGV reporting */
1250 printf("PF exception:\n");
1251 if (setjmp(jmp_env
) == 0) {
1253 /* now store in an invalid address */
1254 *(char *)0x1234 = 1;
1257 /* test SEGV reporting */
1258 printf("PF exception:\n");
1259 if (setjmp(jmp_env
) == 0) {
1261 /* read from an invalid address */
1262 v1
= *(char *)0x1234;
1265 /* test illegal instruction reporting */
1266 printf("UD2 exception:\n");
1267 if (setjmp(jmp_env
) == 0) {
1268 /* now execute an invalid instruction */
1269 asm volatile("ud2");
1272 printf("INT exception:\n");
1273 if (setjmp(jmp_env
) == 0) {
1274 asm volatile ("int $0xfd");
1276 if (setjmp(jmp_env
) == 0) {
1277 asm volatile ("int $0x01");
1279 if (setjmp(jmp_env
) == 0) {
1280 asm volatile (".byte 0xcd, 0x03");
1282 if (setjmp(jmp_env
) == 0) {
1283 asm volatile ("int $0x04");
1285 if (setjmp(jmp_env
) == 0) {
1286 asm volatile ("int $0x05");
1289 printf("INT3 exception:\n");
1290 if (setjmp(jmp_env
) == 0) {
1291 asm volatile ("int3");
1294 printf("CLI exception:\n");
1295 if (setjmp(jmp_env
) == 0) {
1296 asm volatile ("cli");
1299 printf("STI exception:\n");
1300 if (setjmp(jmp_env
) == 0) {
1301 asm volatile ("cli");
1304 printf("INTO exception:\n");
1305 if (setjmp(jmp_env
) == 0) {
1306 /* overflow exception */
1307 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1310 printf("OUTB exception:\n");
1311 if (setjmp(jmp_env
) == 0) {
1312 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1315 printf("INB exception:\n");
1316 if (setjmp(jmp_env
) == 0) {
1317 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1320 printf("REP OUTSB exception:\n");
1321 if (setjmp(jmp_env
) == 0) {
1322 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1325 printf("REP INSB exception:\n");
1326 if (setjmp(jmp_env
) == 0) {
1327 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1330 printf("HLT exception:\n");
1331 if (setjmp(jmp_env
) == 0) {
1332 asm volatile ("hlt");
1335 printf("single step exception:\n");
1337 if (setjmp(jmp_env
) == 0) {
1338 asm volatile ("pushf\n"
1339 "orl $0x00100, (%%esp)\n"
1341 "movl $0xabcd, %0\n"
1342 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1344 printf("val=0x%x\n", val
);
1347 /* self modifying code test */
1349 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1353 typedef int FuncType(void);
1355 void test_self_modifying_code(void)
1359 printf("self modifying code:\n");
1360 printf("func1 = 0x%x\n", ((FuncType
*)code
)());
1361 for(i
= 2; i
<= 4; i
++) {
1363 printf("func%d = 0x%x\n", i
, ((FuncType
*)code
)());
1367 static void *call_end __init_call
= NULL
;
1369 int main(int argc
, char **argv
)
1374 ptr
= &call_start
+ 1;
1375 while (*ptr
!= NULL
) {
1392 test_self_modifying_code();