]>
git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
10 #include <sys/ucontext.h>
16 //#define LINUX_VM86_IOPL_FIX
18 #define xglue(x, y) x ## y
19 #define glue(x, y) xglue(x, y)
20 #define stringify(s) tostring(s)
21 #define tostring(s) #s
30 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
32 static void *call_start __init_call
= NULL
;
34 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
37 #include "test-i386.h"
40 #include "test-i386.h"
43 #include "test-i386.h"
46 #include "test-i386.h"
49 #include "test-i386.h"
52 #include "test-i386.h"
56 #include "test-i386.h"
60 #include "test-i386.h"
65 #include "test-i386.h"
70 #include "test-i386.h"
75 #include "test-i386.h"
80 #include "test-i386.h"
83 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
86 #include "test-i386-shift.h"
89 #include "test-i386-shift.h"
92 #include "test-i386-shift.h"
95 #include "test-i386-shift.h"
98 #include "test-i386-shift.h"
102 #include "test-i386-shift.h"
106 #include "test-i386-shift.h"
111 #include "test-i386-shift.h"
116 #include "test-i386-shift.h"
118 /* XXX: should be more precise ? */
120 #define CC_MASK (CC_C)
124 #include "test-i386-shift.h"
128 #include "test-i386-shift.h"
132 #include "test-i386-shift.h"
136 #include "test-i386-shift.h"
138 /* lea test (modrm support) */
139 #define TEST_LEA(STR)\
141 asm("leal " STR ", %0"\
143 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
144 printf("lea %s = %08x\n", STR, res);\
147 #define TEST_LEA16(STR)\
149 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
151 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
152 printf("lea %s = %08x\n", STR, res);\
158 int eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
175 TEST_LEA("0x40(%%eax)");
176 TEST_LEA("0x40(%%ebx)");
177 TEST_LEA("0x40(%%ecx)");
178 TEST_LEA("0x40(%%edx)");
179 TEST_LEA("0x40(%%esi)");
180 TEST_LEA("0x40(%%edi)");
182 TEST_LEA("0x4000(%%eax)");
183 TEST_LEA("0x4000(%%ebx)");
184 TEST_LEA("0x4000(%%ecx)");
185 TEST_LEA("0x4000(%%edx)");
186 TEST_LEA("0x4000(%%esi)");
187 TEST_LEA("0x4000(%%edi)");
189 TEST_LEA("(%%eax, %%ecx)");
190 TEST_LEA("(%%ebx, %%edx)");
191 TEST_LEA("(%%ecx, %%ecx)");
192 TEST_LEA("(%%edx, %%ecx)");
193 TEST_LEA("(%%esi, %%ecx)");
194 TEST_LEA("(%%edi, %%ecx)");
196 TEST_LEA("0x40(%%eax, %%ecx)");
197 TEST_LEA("0x4000(%%ebx, %%edx)");
199 TEST_LEA("(%%ecx, %%ecx, 2)");
200 TEST_LEA("(%%edx, %%ecx, 4)");
201 TEST_LEA("(%%esi, %%ecx, 8)");
203 TEST_LEA("(,%%eax, 2)");
204 TEST_LEA("(,%%ebx, 4)");
205 TEST_LEA("(,%%ecx, 8)");
207 TEST_LEA("0x40(,%%eax, 2)");
208 TEST_LEA("0x40(,%%ebx, 4)");
209 TEST_LEA("0x40(,%%ecx, 8)");
212 TEST_LEA("-10(%%ecx, %%ecx, 2)");
213 TEST_LEA("-10(%%edx, %%ecx, 4)");
214 TEST_LEA("-10(%%esi, %%ecx, 8)");
216 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
217 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
218 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
220 /* limited 16 bit addressing test */
221 TEST_LEA16("0x4000");
222 TEST_LEA16("(%%bx)");
223 TEST_LEA16("(%%si)");
224 TEST_LEA16("(%%di)");
225 TEST_LEA16("0x40(%%bx)");
226 TEST_LEA16("0x40(%%si)");
227 TEST_LEA16("0x40(%%di)");
228 TEST_LEA16("0x4000(%%bx)");
229 TEST_LEA16("0x4000(%%si)");
230 TEST_LEA16("(%%bx,%%si)");
231 TEST_LEA16("(%%bx,%%di)");
232 TEST_LEA16("0x40(%%bx,%%si)");
233 TEST_LEA16("0x40(%%bx,%%di)");
234 TEST_LEA16("0x4000(%%bx,%%si)");
235 TEST_LEA16("0x4000(%%bx,%%di)");
238 #define TEST_JCC(JCC, v1, v2)\
241 asm("movl $1, %0\n\t"\
247 : "r" (v1), "r" (v2));\
248 printf("%-10s %d\n", "j" JCC, res);\
250 asm("movl $0, %0\n\t"\
252 "set" JCC " %b0\n\t"\
254 : "r" (v1), "r" (v2));\
255 printf("%-10s %d\n", "set" JCC, res);\
257 asm("movl $0x12345678, %0\n\t"\
259 "cmov" JCC "l %3, %0\n\t"\
261 : "r" (v1), "r" (v2), "m" (1));\
262 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
263 asm("movl $0x12345678, %0\n\t"\
265 "cmov" JCC "w %w3, %w0\n\t"\
267 : "r" (v1), "r" (v2), "r" (1));\
268 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
272 /* various jump tests */
275 TEST_JCC("ne", 1, 1);
276 TEST_JCC("ne", 1, 0);
283 TEST_JCC("l", 1, -1);
285 TEST_JCC("le", 1, 1);
286 TEST_JCC("le", 1, 0);
287 TEST_JCC("le", 1, -1);
289 TEST_JCC("ge", 1, 1);
290 TEST_JCC("ge", 1, 0);
291 TEST_JCC("ge", -1, 1);
295 TEST_JCC("g", 1, -1);
299 TEST_JCC("b", 1, -1);
301 TEST_JCC("be", 1, 1);
302 TEST_JCC("be", 1, 0);
303 TEST_JCC("be", 1, -1);
305 TEST_JCC("ae", 1, 1);
306 TEST_JCC("ae", 1, 0);
307 TEST_JCC("ae", 1, -1);
311 TEST_JCC("a", 1, -1);
317 TEST_JCC("np", 1, 1);
318 TEST_JCC("np", 1, 0);
320 TEST_JCC("o", 0x7fffffff, 0);
321 TEST_JCC("o", 0x7fffffff, -1);
323 TEST_JCC("no", 0x7fffffff, 0);
324 TEST_JCC("no", 0x7fffffff, -1);
327 TEST_JCC("s", 0, -1);
330 TEST_JCC("ns", 0, 1);
331 TEST_JCC("ns", 0, -1);
332 TEST_JCC("ns", 0, 0);
336 #define CC_MASK (CC_O | CC_C)
339 #include "test-i386-muldiv.h"
342 #include "test-i386-muldiv.h"
348 #include "test-i386-muldiv.h"
351 #include "test-i386-muldiv.h"
353 void test_imulw2(int op0
, int op1
)
355 int res
, s1
, s0
, flags
;
365 : "=q" (res
), "=g" (flags
)
366 : "q" (s1
), "0" (res
), "1" (flags
));
367 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
368 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
371 void test_imull2(int op0
, int op1
)
373 int res
, s1
, s0
, flags
;
383 : "=q" (res
), "=g" (flags
)
384 : "q" (s1
), "0" (res
), "1" (flags
));
385 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
386 "imull", s0
, s1
, res
, flags
& CC_MASK
);
391 test_imulb(0x1234561d, 4);
393 test_imulb(0x80, 0x80);
394 test_imulb(0x10, 0x10);
396 test_imulw(0, 0x1234001d, 45);
397 test_imulw(0, 23, -45);
398 test_imulw(0, 0x8000, 0x8000);
399 test_imulw(0, 0x100, 0x100);
401 test_imull(0, 0x1234001d, 45);
402 test_imull(0, 23, -45);
403 test_imull(0, 0x80000000, 0x80000000);
404 test_imull(0, 0x10000, 0x10000);
406 test_mulb(0x1234561d, 4);
408 test_mulb(0x80, 0x80);
409 test_mulb(0x10, 0x10);
411 test_mulw(0, 0x1234001d, 45);
412 test_mulw(0, 23, -45);
413 test_mulw(0, 0x8000, 0x8000);
414 test_mulw(0, 0x100, 0x100);
416 test_mull(0, 0x1234001d, 45);
417 test_mull(0, 23, -45);
418 test_mull(0, 0x80000000, 0x80000000);
419 test_mull(0, 0x10000, 0x10000);
421 test_imulw2(0x1234001d, 45);
422 test_imulw2(23, -45);
423 test_imulw2(0x8000, 0x8000);
424 test_imulw2(0x100, 0x100);
426 test_imull2(0x1234001d, 45);
427 test_imull2(23, -45);
428 test_imull2(0x80000000, 0x80000000);
429 test_imull2(0x10000, 0x10000);
431 test_idivb(0x12341678, 0x127e);
432 test_idivb(0x43210123, -5);
433 test_idivb(0x12340004, -1);
435 test_idivw(0, 0x12345678, 12347);
436 test_idivw(0, -23223, -45);
437 test_idivw(0, 0x12348000, -1);
438 test_idivw(0x12343, 0x12345678, 0x81238567);
440 test_idivl(0, 0x12345678, 12347);
441 test_idivl(0, -233223, -45);
442 test_idivl(0, 0x80000000, -1);
443 test_idivl(0x12343, 0x12345678, 0x81234567);
445 test_divb(0x12341678, 0x127e);
446 test_divb(0x43210123, -5);
447 test_divb(0x12340004, -1);
449 test_divw(0, 0x12345678, 12347);
450 test_divw(0, -23223, -45);
451 test_divw(0, 0x12348000, -1);
452 test_divw(0x12343, 0x12345678, 0x81238567);
454 test_divl(0, 0x12345678, 12347);
455 test_divl(0, -233223, -45);
456 test_divl(0, 0x80000000, -1);
457 test_divl(0x12343, 0x12345678, 0x81234567);
460 #define TEST_BSX(op, size, op0)\
464 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
465 : "=r" (res), "=q" (resz)\
467 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
472 TEST_BSX(bsrw
, "w", 0);
473 TEST_BSX(bsrw
, "w", 0x12340128);
474 TEST_BSX(bsrl
, "", 0);
475 TEST_BSX(bsrl
, "", 0x00340128);
476 TEST_BSX(bsfw
, "w", 0);
477 TEST_BSX(bsfw
, "w", 0x12340128);
478 TEST_BSX(bsfl
, "", 0);
479 TEST_BSX(bsfl
, "", 0x00340128);
482 /**********************************************/
484 void test_fops(double a
, double 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 a*b=%f\n", a
, b
, a
* b
);
489 printf("a=%f b=%f a/b=%f\n", a
, b
, a
/ b
);
490 printf("a=%f b=%f fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
491 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
492 printf("a=%f sin(a)=%f\n", a
, sin(a
));
493 printf("a=%f cos(a)=%f\n", a
, cos(a
));
494 printf("a=%f tan(a)=%f\n", a
, tan(a
));
495 printf("a=%f log(a)=%f\n", a
, log(a
));
496 printf("a=%f exp(a)=%f\n", a
, exp(a
));
497 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
498 /* just to test some op combining */
499 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
500 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
501 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
505 void test_fcmp(double a
, double b
)
507 printf("(%f<%f)=%d\n",
509 printf("(%f<=%f)=%d\n",
511 printf("(%f==%f)=%d\n",
513 printf("(%f>%f)=%d\n",
515 printf("(%f<=%f)=%d\n",
519 /* test f(u)comi instruction */
525 printf("fcomi(%f %f)=%08x\n", a
, b
, eflags
& (CC_Z
| CC_P
| CC_C
));
529 void test_fcvt(double a
)
542 printf("(float)%f = %f\n", a
, fa
);
543 printf("(long double)%f = %Lf\n", a
, la
);
544 printf("a=%016Lx\n", *(long long *)&a
);
545 printf("la=%016Lx %04x\n", *(long long *)&la
,
546 *(unsigned short *)((char *)(&la
) + 8));
548 /* test all roundings */
549 asm volatile ("fstcw %0" : "=m" (fpuc
));
551 asm volatile ("fldcw %0" : : "m" ((fpuc
& ~0x0c00) | (i
<< 10)));
552 asm volatile ("fist %0" : "=m" (wa
) : "t" (a
));
553 asm volatile ("fistl %0" : "=m" (ia
) : "t" (a
));
554 asm volatile ("fistpll %0" : "=m" (lla
) : "t" (a
) : "st");
555 asm volatile ("frndint ; fstl %0" : "=m" (ra
) : "t" (a
));
556 asm volatile ("fldcw %0" : : "m" (fpuc
));
557 printf("(short)a = %d\n", wa
);
558 printf("(int)a = %d\n", ia
);
559 printf("(int64_t)a = %Ld\n", lla
);
560 printf("rint(a) = %f\n", ra
);
565 asm("fld" #N : "=t" (a)); \
566 printf("fld" #N "= %f\n", a);
568 void test_fconst(void)
580 void test_fbcd(double a
)
582 unsigned short bcd
[5];
585 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
586 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
587 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
588 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
591 #define TEST_ENV(env, prefix)\
593 memset((env), 0xaa, sizeof(*(env)));\
595 prefix "fnstenv %1\n"\
596 prefix "fldenv %1\n"\
597 : "=t" (res) : "m" (*(env)));\
598 printf("res=%f\n", res);\
599 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
601 (env)->fpus & 0xff00,\
603 memset((env), 0xaa, sizeof(*(env)));\
605 prefix "fnsave %1\n"\
606 prefix "frstor %1\n"\
607 : "=t" (res) : "m" (*(env)));\
608 printf("res=%f\n", res);\
609 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
611 (env)->fpus & 0xff00,\
613 printf("ST(0) = %Lf\n",\
619 struct __attribute__((packed
)) {
627 long double fpregs
[8];
629 struct __attribute__((packed
)) {
634 long double fpregs
[8];
638 TEST_ENV(&float_env16
, "data16 ");
639 TEST_ENV(&float_env32
, "");
642 void test_floats(void)
656 test_fbcd(1234567890123456);
657 test_fbcd(-123451234567890);
661 /**********************************************/
663 #define TEST_BCD(op, op0, cc_in, cc_mask)\
673 : "=a" (res), "=g" (flags)\
674 : "0" (res), "1" (flags));\
675 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
676 #op, op0, res, cc_in, flags & cc_mask);\
681 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
682 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
683 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
684 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
685 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
686 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
687 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
688 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
689 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
690 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
691 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
692 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
693 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
695 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
696 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
697 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
698 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
699 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
700 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
701 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
702 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
703 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
704 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
705 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
706 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
707 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
709 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
710 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
711 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
712 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
713 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
714 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
715 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
716 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
718 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
719 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
720 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
721 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
722 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
723 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
724 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
725 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
727 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
728 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
731 #define TEST_XCHG(op, size, opconst)\
736 asm(#op " %" size "0, %" size "1" \
737 : "=q" (op0), opconst (op1) \
738 : "0" (op0), "1" (op1));\
739 printf("%-10s A=%08x B=%08x\n",\
743 #define TEST_CMPXCHG(op, size, opconst, eax)\
748 asm(#op " %" size "0, %" size "1" \
749 : "=q" (op0), opconst (op1) \
750 : "0" (op0), "1" (op1), "a" (eax));\
751 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
752 #op, eax, op0, op1);\
757 TEST_XCHG(xchgl
, "", "=q");
758 TEST_XCHG(xchgw
, "w", "=q");
759 TEST_XCHG(xchgb
, "b", "=q");
761 TEST_XCHG(xchgl
, "", "=m");
762 TEST_XCHG(xchgw
, "w", "=m");
763 TEST_XCHG(xchgb
, "b", "=m");
765 TEST_XCHG(xaddl
, "", "=q");
766 TEST_XCHG(xaddw
, "w", "=q");
767 TEST_XCHG(xaddb
, "b", "=q");
769 TEST_XCHG(xaddl
, "", "=m");
770 TEST_XCHG(xaddw
, "w", "=m");
771 TEST_XCHG(xaddb
, "b", "=m");
773 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfbca7654);
774 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfbca7654);
775 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfbca7654);
777 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfffefdfc);
778 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfffefdfc);
779 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfffefdfc);
781 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfbca7654);
782 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfbca7654);
783 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfbca7654);
785 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfffefdfc);
786 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfffefdfc);
787 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfffefdfc);
790 /**********************************************/
791 /* segmentation tests */
794 #include <linux/unistd.h>
796 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
798 uint8_t seg_data1
[4096];
799 uint8_t seg_data2
[4096];
801 #define MK_SEL(n) (((n) << 3) | 7)
803 #define TEST_LR(op, size, seg, mask)\
807 asm (op " %" size "2, %" size "0\n" \
812 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
813 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
816 /* NOTE: we use Linux modify_ldt syscall */
819 struct modify_ldt_ldt_s ldt
;
820 long long ldt_table
[3];
826 } __attribute__((packed
)) segoff
;
828 ldt
.entry_number
= 1;
829 ldt
.base_addr
= (unsigned long)&seg_data1
;
830 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
832 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
833 ldt
.read_exec_only
= 0;
834 ldt
.limit_in_pages
= 1;
835 ldt
.seg_not_present
= 0;
837 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
839 ldt
.entry_number
= 2;
840 ldt
.base_addr
= (unsigned long)&seg_data2
;
841 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
843 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
844 ldt
.read_exec_only
= 0;
845 ldt
.limit_in_pages
= 1;
846 ldt
.seg_not_present
= 0;
848 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
850 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
855 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
858 /* do some tests with fs or gs */
859 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
864 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
865 printf("FS[1] = %02x\n", res
);
867 asm volatile ("pushl %%gs\n"
869 "gs movzbl 0x1, %0\n"
873 printf("GS[1] = %02x\n", res
);
875 /* tests with ds/ss (implicit segment case) */
877 asm volatile ("pushl %%ebp\n\t"
882 "movzbl (%%ebp), %1\n\t"
885 : "=r" (res
), "=r" (res2
)
886 : "r" (MK_SEL(1)), "r" (&tmp
));
887 printf("DS[1] = %02x\n", res
);
888 printf("SS[tmp] = %02x\n", res2
);
890 segoff
.seg
= MK_SEL(2);
891 segoff
.offset
= 0xabcdef12;
892 asm volatile("lfs %2, %0\n\t"
894 : "=r" (res
), "=g" (res2
)
896 printf("FS:reg = %04x:%08x\n", res2
, res
);
898 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
899 TEST_LR("larl", "", MK_SEL(2), 0x0100);
900 TEST_LR("lslw", "w", MK_SEL(2), 0);
901 TEST_LR("lsll", "", MK_SEL(2), 0);
903 TEST_LR("larw", "w", 0xfff8, 0);
904 TEST_LR("larl", "", 0xfff8, 0);
905 TEST_LR("lslw", "w", 0xfff8, 0);
906 TEST_LR("lsll", "", 0xfff8, 0);
909 /* 16 bit code test */
910 extern char code16_start
, code16_end
;
911 extern char code16_func1
;
912 extern char code16_func2
;
913 extern char code16_func3
;
915 void test_code16(void)
917 struct modify_ldt_ldt_s ldt
;
920 /* build a code segment */
921 ldt
.entry_number
= 1;
922 ldt
.base_addr
= (unsigned long)&code16_start
;
923 ldt
.limit
= &code16_end
- &code16_start
;
925 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
926 ldt
.read_exec_only
= 0;
927 ldt
.limit_in_pages
= 0;
928 ldt
.seg_not_present
= 0;
930 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
932 /* call the first function */
933 asm volatile ("lcall %1, %2"
935 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
936 printf("func1() = 0x%08x\n", res
);
937 asm volatile ("lcall %2, %3"
938 : "=a" (res
), "=c" (res2
)
939 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
940 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
941 asm volatile ("lcall %1, %2"
943 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
944 printf("func3() = 0x%08x\n", res
);
947 extern char func_lret32
;
948 extern char func_iret32
;
955 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
957 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
958 printf("xlat: EAX=%08x\n", res
);
960 asm volatile ("pushl %%cs ; call %1"
962 : "m" (func_lret32
): "memory", "cc");
963 printf("func_lret32=%x\n", res
);
965 asm volatile ("pushfl ; pushl %%cs ; call %1"
967 : "m" (func_iret32
): "memory", "cc");
968 printf("func_iret32=%x\n", res
);
970 /* specific popl test */
971 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
973 printf("popl esp=%x\n", res
);
975 /* specific popw test */
976 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
978 printf("popw esp=%x\n", res
);
981 uint8_t str_buffer
[4096];
983 #define TEST_STRING1(OP, size, DF, REP)\
985 int esi, edi, eax, ecx, eflags;\
987 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
988 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
992 asm volatile ("pushl $0\n\t"\
999 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1000 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1001 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1002 REP #OP size, esi, edi, eax, ecx,\
1003 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1006 #define TEST_STRING(OP, REP)\
1007 TEST_STRING1(OP, "b", "", REP);\
1008 TEST_STRING1(OP, "w", "", REP);\
1009 TEST_STRING1(OP, "l", "", REP);\
1010 TEST_STRING1(OP, "b", "std", REP);\
1011 TEST_STRING1(OP, "w", "std", REP);\
1012 TEST_STRING1(OP, "l", "std", REP)
1014 void test_string(void)
1017 for(i
= 0;i
< sizeof(str_buffer
); i
++)
1018 str_buffer
[i
] = i
+ 0x56;
1019 TEST_STRING(stos
, "");
1020 TEST_STRING(stos
, "rep ");
1021 TEST_STRING(lods
, ""); /* to verify stos */
1022 TEST_STRING(lods
, "rep ");
1023 TEST_STRING(movs
, "");
1024 TEST_STRING(movs
, "rep ");
1025 TEST_STRING(lods
, ""); /* to verify stos */
1027 /* XXX: better tests */
1028 TEST_STRING(scas
, "");
1029 TEST_STRING(scas
, "repz ");
1030 TEST_STRING(scas
, "repnz ");
1031 TEST_STRING(cmps
, "");
1032 TEST_STRING(cmps
, "repz ");
1033 TEST_STRING(cmps
, "repnz ");
1038 static inline void set_bit(uint8_t *a
, unsigned int bit
)
1040 a
[bit
/ 8] |= (1 << (bit
% 8));
1043 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
1045 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
1048 static inline void pushw(struct vm86_regs
*r
, int val
)
1050 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
1051 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
1054 #undef __syscall_return
1055 #define __syscall_return(type, res) \
1057 return (type) (res); \
1060 _syscall2(int, vm86
, int, func
, struct vm86plus_struct
*, v86
)
1062 extern char vm86_code_start
;
1063 extern char vm86_code_end
;
1065 #define VM86_CODE_CS 0x100
1066 #define VM86_CODE_IP 0x100
1068 void test_vm86(void)
1070 struct vm86plus_struct ctx
;
1071 struct vm86_regs
*r
;
1075 vm86_mem
= mmap((void *)0x00000000, 0x110000,
1076 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
1077 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
1078 if (vm86_mem
== MAP_FAILED
) {
1079 printf("ERROR: could not map vm86 memory");
1082 memset(&ctx
, 0, sizeof(ctx
));
1084 /* init basic registers */
1086 r
->eip
= VM86_CODE_IP
;
1095 r
->eflags
= VIF_MASK
;
1097 /* move code to proper address. We use the same layout as a .com
1099 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
1100 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
1102 /* mark int 0x21 as being emulated */
1103 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
1106 ret
= vm86(VM86_ENTER
, &ctx
);
1107 switch(VM86_TYPE(ret
)) {
1112 int_num
= VM86_ARG(ret
);
1113 if (int_num
!= 0x21)
1115 ah
= (r
->eax
>> 8) & 0xff;
1117 case 0x00: /* exit */
1119 case 0x02: /* write char */
1125 case 0x09: /* write string */
1128 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1135 r
->eax
= (r
->eax
& ~0xff) | '$';
1138 case 0xff: /* extension: write eflags number in edx */
1140 #ifndef LINUX_VM86_IOPL_FIX
1143 printf("%08x\n", v
);
1147 printf("unsupported int 0x%02x\n", int_num
);
1153 /* a signal came, we just ignore that */
1158 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1163 printf("VM86 end\n");
1164 munmap(vm86_mem
, 0x110000);
1167 /* exception tests */
1179 #define REG_TRAPNO TRAPNO
1187 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1189 struct ucontext
*uc
= puc
;
1191 printf("si_signo=%d si_errno=%d si_code=%d",
1192 info
->si_signo
, info
->si_errno
, info
->si_code
);
1193 printf(" si_addr=0x%08lx",
1194 (unsigned long)info
->si_addr
);
1197 printf("trapno=0x%02x err=0x%08x",
1198 uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1199 uc
->uc_mcontext
.gregs
[REG_ERR
]);
1200 printf(" EIP=0x%08x", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1202 longjmp(jmp_env
, 1);
1205 void test_exceptions(void)
1207 struct modify_ldt_ldt_s ldt
;
1208 struct sigaction act
;
1211 act
.sa_sigaction
= sig_handler
;
1212 sigemptyset(&act
.sa_mask
);
1213 act
.sa_flags
= SA_SIGINFO
;
1214 sigaction(SIGFPE
, &act
, NULL
);
1215 sigaction(SIGILL
, &act
, NULL
);
1216 sigaction(SIGSEGV
, &act
, NULL
);
1217 sigaction(SIGBUS
, &act
, NULL
);
1218 sigaction(SIGTRAP
, &act
, NULL
);
1220 /* test division by zero reporting */
1221 printf("DIVZ exception:\n");
1222 if (setjmp(jmp_env
) == 0) {
1223 /* now divide by zero */
1228 printf("BOUND exception:\n");
1229 if (setjmp(jmp_env
) == 0) {
1230 /* bound exception */
1233 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
));
1236 printf("segment exceptions:\n");
1237 if (setjmp(jmp_env
) == 0) {
1238 /* load an invalid segment */
1239 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1241 if (setjmp(jmp_env
) == 0) {
1242 /* null data segment is valid */
1243 asm volatile ("movl %0, %%fs" : : "r" (3));
1244 /* null stack segment */
1245 asm volatile ("movl %0, %%ss" : : "r" (3));
1248 ldt
.entry_number
= 1;
1249 ldt
.base_addr
= (unsigned long)&seg_data1
;
1250 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1252 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1253 ldt
.read_exec_only
= 0;
1254 ldt
.limit_in_pages
= 1;
1255 ldt
.seg_not_present
= 1;
1257 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1259 if (setjmp(jmp_env
) == 0) {
1260 /* segment not present */
1261 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1264 /* test SEGV reporting */
1265 printf("PF exception:\n");
1266 if (setjmp(jmp_env
) == 0) {
1268 /* we add a nop to test a weird PC retrieval case */
1269 asm volatile ("nop");
1270 /* now store in an invalid address */
1271 *(char *)0x1234 = 1;
1274 /* test SEGV reporting */
1275 printf("PF exception:\n");
1276 if (setjmp(jmp_env
) == 0) {
1278 /* read from an invalid address */
1279 v1
= *(char *)0x1234;
1282 /* test illegal instruction reporting */
1283 printf("UD2 exception:\n");
1284 if (setjmp(jmp_env
) == 0) {
1285 /* now execute an invalid instruction */
1286 asm volatile("ud2");
1289 printf("INT exception:\n");
1290 if (setjmp(jmp_env
) == 0) {
1291 asm volatile ("int $0xfd");
1293 if (setjmp(jmp_env
) == 0) {
1294 asm volatile ("int $0x01");
1296 if (setjmp(jmp_env
) == 0) {
1297 asm volatile (".byte 0xcd, 0x03");
1299 if (setjmp(jmp_env
) == 0) {
1300 asm volatile ("int $0x04");
1302 if (setjmp(jmp_env
) == 0) {
1303 asm volatile ("int $0x05");
1306 printf("INT3 exception:\n");
1307 if (setjmp(jmp_env
) == 0) {
1308 asm volatile ("int3");
1311 printf("CLI exception:\n");
1312 if (setjmp(jmp_env
) == 0) {
1313 asm volatile ("cli");
1316 printf("STI exception:\n");
1317 if (setjmp(jmp_env
) == 0) {
1318 asm volatile ("cli");
1321 printf("INTO exception:\n");
1322 if (setjmp(jmp_env
) == 0) {
1323 /* overflow exception */
1324 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1327 printf("OUTB exception:\n");
1328 if (setjmp(jmp_env
) == 0) {
1329 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1332 printf("INB exception:\n");
1333 if (setjmp(jmp_env
) == 0) {
1334 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1337 printf("REP OUTSB exception:\n");
1338 if (setjmp(jmp_env
) == 0) {
1339 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1342 printf("REP INSB exception:\n");
1343 if (setjmp(jmp_env
) == 0) {
1344 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1347 printf("HLT exception:\n");
1348 if (setjmp(jmp_env
) == 0) {
1349 asm volatile ("hlt");
1352 printf("single step exception:\n");
1354 if (setjmp(jmp_env
) == 0) {
1355 asm volatile ("pushf\n"
1356 "orl $0x00100, (%%esp)\n"
1358 "movl $0xabcd, %0\n"
1359 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1361 printf("val=0x%x\n", val
);
1364 /* specific precise single step test */
1365 void sig_trap_handler(int sig
, siginfo_t
*info
, void *puc
)
1367 struct ucontext
*uc
= puc
;
1368 printf("EIP=0x%08x\n", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1371 const uint8_t sstep_buf1
[4] = { 1, 2, 3, 4};
1372 uint8_t sstep_buf2
[4];
1374 void test_single_step(void)
1376 struct sigaction act
;
1381 act
.sa_sigaction
= sig_trap_handler
;
1382 sigemptyset(&act
.sa_mask
);
1383 act
.sa_flags
= SA_SIGINFO
;
1384 sigaction(SIGTRAP
, &act
, NULL
);
1385 asm volatile ("pushf\n"
1386 "orl $0x00100, (%%esp)\n"
1388 "movl $0xabcd, %0\n"
1397 /* movsb: the single step should stop at each movsb iteration */
1398 "movl $sstep_buf1, %%esi\n"
1399 "movl $sstep_buf2, %%edi\n"
1407 /* cmpsb: the single step should stop at each cmpsb iteration */
1408 "movl $sstep_buf1, %%esi\n"
1409 "movl $sstep_buf2, %%edi\n"
1415 /* getpid() syscall: single step should skip one
1421 /* when modifying SS, trace is not done on the next
1423 "movl %%ss, %%ecx\n"
1424 "movl %%ecx, %%ss\n"
1427 "movl %%ecx, %%ss\n"
1438 "andl $~0x00100, (%%esp)\n"
1442 : "cc", "memory", "eax", "ecx", "esi", "edi");
1443 printf("val=%d\n", val
);
1444 for(i
= 0; i
< 4; i
++)
1445 printf("sstep_buf2[%d] = %d\n", i
, sstep_buf2
[i
]);
1448 /* self modifying code test */
1450 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1454 typedef int FuncType(void);
1456 void test_self_modifying_code(void)
1460 printf("self modifying code:\n");
1461 printf("func1 = 0x%x\n", ((FuncType
*)code
)());
1462 for(i
= 2; i
<= 4; i
++) {
1464 printf("func%d = 0x%x\n", i
, ((FuncType
*)code
)());
1468 static void *call_end __init_call
= NULL
;
1470 int main(int argc
, char **argv
)
1475 ptr
= &call_start
+ 1;
1476 while (*ptr
!= NULL
) {
1493 test_self_modifying_code();