8 #include <sys/ucontext.h>
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
27 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
29 static void *call_start __init_call = NULL;
31 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
34 #include "test-i386.h"
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"
53 #include "test-i386.h"
57 #include "test-i386.h"
62 #include "test-i386.h"
67 #include "test-i386.h"
72 #include "test-i386.h"
77 #include "test-i386.h"
80 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
83 #include "test-i386-shift.h"
86 #include "test-i386-shift.h"
89 #include "test-i386-shift.h"
92 #include "test-i386-shift.h"
95 #include "test-i386-shift.h"
99 #include "test-i386-shift.h"
103 #include "test-i386-shift.h"
108 #include "test-i386-shift.h"
113 #include "test-i386-shift.h"
115 /* XXX: should be more precise ? */
117 #define CC_MASK (CC_C)
121 #include "test-i386-shift.h"
125 #include "test-i386-shift.h"
129 #include "test-i386-shift.h"
133 #include "test-i386-shift.h"
135 /* lea test (modrm support) */
136 #define TEST_LEA(STR)\
138 asm("leal " STR ", %0"\
140 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
141 printf("lea %s = %08x\n", STR, res);\
144 #define TEST_LEA16(STR)\
146 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
148 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
149 printf("lea %s = %08x\n", STR, res);\
155 int eax, ebx, ecx, edx, esi, edi, res;
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)");
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)");
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)");
193 TEST_LEA("0x40(%%eax, %%ecx)");
194 TEST_LEA("0x4000(%%ebx, %%edx)");
196 TEST_LEA("(%%ecx, %%ecx, 2)");
197 TEST_LEA("(%%edx, %%ecx, 4)");
198 TEST_LEA("(%%esi, %%ecx, 8)");
200 TEST_LEA("(,%%eax, 2)");
201 TEST_LEA("(,%%ebx, 4)");
202 TEST_LEA("(,%%ecx, 8)");
204 TEST_LEA("0x40(,%%eax, 2)");
205 TEST_LEA("0x40(,%%ebx, 4)");
206 TEST_LEA("0x40(,%%ecx, 8)");
209 TEST_LEA("-10(%%ecx, %%ecx, 2)");
210 TEST_LEA("-10(%%edx, %%ecx, 4)");
211 TEST_LEA("-10(%%esi, %%ecx, 8)");
213 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
214 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
215 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
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)");
235 #define TEST_JCC(JCC, v1, v2)\
238 asm("movl $1, %0\n\t"\
244 : "r" (v1), "r" (v2));\
245 printf("%-10s %d\n", "j" JCC, res);\
247 asm("movl $0, %0\n\t"\
249 "set" JCC " %b0\n\t"\
251 : "r" (v1), "r" (v2));\
252 printf("%-10s %d\n", "set" JCC, res);\
254 asm("movl $0x12345678, %0\n\t"\
256 "cmov" JCC "l %3, %0\n\t"\
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"\
262 "cmov" JCC "w %w3, %w0\n\t"\
264 : "r" (v1), "r" (v2), "r" (1));\
265 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
269 /* various jump tests */
272 TEST_JCC("ne", 1, 1);
273 TEST_JCC("ne", 1, 0);
280 TEST_JCC("l", 1, -1);
282 TEST_JCC("le", 1, 1);
283 TEST_JCC("le", 1, 0);
284 TEST_JCC("le", 1, -1);
286 TEST_JCC("ge", 1, 1);
287 TEST_JCC("ge", 1, 0);
288 TEST_JCC("ge", -1, 1);
292 TEST_JCC("g", 1, -1);
296 TEST_JCC("b", 1, -1);
298 TEST_JCC("be", 1, 1);
299 TEST_JCC("be", 1, 0);
300 TEST_JCC("be", 1, -1);
302 TEST_JCC("ae", 1, 1);
303 TEST_JCC("ae", 1, 0);
304 TEST_JCC("ae", 1, -1);
308 TEST_JCC("a", 1, -1);
314 TEST_JCC("np", 1, 1);
315 TEST_JCC("np", 1, 0);
317 TEST_JCC("o", 0x7fffffff, 0);
318 TEST_JCC("o", 0x7fffffff, -1);
320 TEST_JCC("no", 0x7fffffff, 0);
321 TEST_JCC("no", 0x7fffffff, -1);
324 TEST_JCC("s", 0, -1);
327 TEST_JCC("ns", 0, 1);
328 TEST_JCC("ns", 0, -1);
329 TEST_JCC("ns", 0, 0);
333 #define CC_MASK (CC_O | CC_C)
336 #include "test-i386-muldiv.h"
339 #include "test-i386-muldiv.h"
345 #include "test-i386-muldiv.h"
348 #include "test-i386-muldiv.h"
350 void test_imulw2(int op0, int op1)
352 int res, s1, s0, flags;
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);
368 void test_imull2(int op0, int op1)
370 int res, s1, s0, flags;
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);
388 test_imulb(0x1234561d, 4);
390 test_imulb(0x80, 0x80);
391 test_imulb(0x10, 0x10);
393 test_imulw(0, 0x1234001d, 45);
394 test_imulw(0, 23, -45);
395 test_imulw(0, 0x8000, 0x8000);
396 test_imulw(0, 0x100, 0x100);
398 test_imull(0, 0x1234001d, 45);
399 test_imull(0, 23, -45);
400 test_imull(0, 0x80000000, 0x80000000);
401 test_imull(0, 0x10000, 0x10000);
403 test_mulb(0x1234561d, 4);
405 test_mulb(0x80, 0x80);
406 test_mulb(0x10, 0x10);
408 test_mulw(0, 0x1234001d, 45);
409 test_mulw(0, 23, -45);
410 test_mulw(0, 0x8000, 0x8000);
411 test_mulw(0, 0x100, 0x100);
413 test_mull(0, 0x1234001d, 45);
414 test_mull(0, 23, -45);
415 test_mull(0, 0x80000000, 0x80000000);
416 test_mull(0, 0x10000, 0x10000);
418 test_imulw2(0x1234001d, 45);
419 test_imulw2(23, -45);
420 test_imulw2(0x8000, 0x8000);
421 test_imulw2(0x100, 0x100);
423 test_imull2(0x1234001d, 45);
424 test_imull2(23, -45);
425 test_imull2(0x80000000, 0x80000000);
426 test_imull2(0x10000, 0x10000);
428 test_idivb(0x12341678, 0x127e);
429 test_idivb(0x43210123, -5);
430 test_idivb(0x12340004, -1);
432 test_idivw(0, 0x12345678, 12347);
433 test_idivw(0, -23223, -45);
434 test_idivw(0, 0x12348000, -1);
435 test_idivw(0x12343, 0x12345678, 0x81238567);
437 test_idivl(0, 0x12345678, 12347);
438 test_idivl(0, -233223, -45);
439 test_idivl(0, 0x80000000, -1);
440 test_idivl(0x12343, 0x12345678, 0x81234567);
442 test_divb(0x12341678, 0x127e);
443 test_divb(0x43210123, -5);
444 test_divb(0x12340004, -1);
446 test_divw(0, 0x12345678, 12347);
447 test_divw(0, -23223, -45);
448 test_divw(0, 0x12348000, -1);
449 test_divw(0x12343, 0x12345678, 0x81238567);
451 test_divl(0, 0x12345678, 12347);
452 test_divl(0, -233223, -45);
453 test_divl(0, 0x80000000, -1);
454 test_divl(0x12343, 0x12345678, 0x81234567);
457 #define TEST_BSX(op, size, op0)\
461 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
462 : "=r" (res), "=q" (resz)\
464 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
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);
479 /**********************************************/
481 void test_fops(double a, double b)
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)));
502 void test_fcmp(double a, double b)
504 printf("(%f<%f)=%d\n",
506 printf("(%f<=%f)=%d\n",
508 printf("(%f==%f)=%d\n",
510 printf("(%f>%f)=%d\n",
512 printf("(%f<=%f)=%d\n",
516 /* test f(u)comi instruction */
522 printf("fcomi(%f %f)=%08x\n", a, b, eflags & (CC_Z | CC_P | CC_C));
526 void test_fcvt(double a)
539 printf("(float)%f = %f\n", a, fa);
540 printf("(long double)%f = %Lf\n", a, la);
541 printf("a=%016Lx\n", *(long long *)&a);
542 printf("la=%016Lx %04x\n", *(long long *)&la,
543 *(unsigned short *)((char *)(&la) + 8));
545 /* test all roundings */
546 asm volatile ("fstcw %0" : "=m" (fpuc));
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);
562 asm("fld" #N : "=t" (a)); \
563 printf("fld" #N "= %f\n", a);
565 void test_fconst(void)
577 void test_fbcd(double a)
579 unsigned short bcd[5];
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);
588 #define TEST_ENV(env, prefix)\
590 memset((env), 0xaa, sizeof(*(env)));\
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",\
598 (env)->fpus & 0xff00,\
600 memset((env), 0xaa, sizeof(*(env)));\
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",\
608 (env)->fpus & 0xff00,\
610 printf("ST(0) = %Lf\n",\
616 struct __attribute__((packed)) {
624 long double fpregs[8];
626 struct __attribute__((packed)) {
631 long double fpregs[8];
635 TEST_ENV(&float_env16, "data16 ");
636 TEST_ENV(&float_env32, "");
639 void test_floats(void)
653 test_fbcd(1234567890123456);
654 test_fbcd(-123451234567890);
658 /**********************************************/
660 #define TEST_BCD(op, op0, cc_in, cc_mask)\
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);\
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));
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));
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));
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));
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));
728 #define TEST_XCHG(op, size, opconst)\
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",\
740 #define TEST_CMPXCHG(op, size, opconst, eax)\
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);\
754 TEST_XCHG(xchgl, "", "=q");
755 TEST_XCHG(xchgw, "w", "=q");
756 TEST_XCHG(xchgb, "b", "=q");
758 TEST_XCHG(xchgl, "", "=m");
759 TEST_XCHG(xchgw, "w", "=m");
760 TEST_XCHG(xchgb, "b", "=m");
762 TEST_XCHG(xaddl, "", "=q");
763 TEST_XCHG(xaddw, "w", "=q");
764 TEST_XCHG(xaddb, "b", "=q");
766 TEST_XCHG(xaddl, "", "=m");
767 TEST_XCHG(xaddw, "w", "=m");
768 TEST_XCHG(xaddb, "b", "=m");
770 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
771 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
772 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
774 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
775 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
776 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
778 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
779 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
780 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
782 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
783 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
784 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
787 /**********************************************/
788 /* segmentation tests */
791 #include <linux/unistd.h>
793 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
795 uint8_t seg_data1[4096];
796 uint8_t seg_data2[4096];
798 #define MK_SEL(n) (((n) << 3) | 7)
800 #define TEST_LR(op, size, seg, mask)\
804 asm (op " %" size "2, %" size "0\n" \
809 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
810 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
813 /* NOTE: we use Linux modify_ldt syscall */
816 struct modify_ldt_ldt_s ldt;
817 long long ldt_table[3];
823 } __attribute__((packed)) segoff;
825 ldt.entry_number = 1;
826 ldt.base_addr = (unsigned long)&seg_data1;
827 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
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;
834 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
836 ldt.entry_number = 2;
837 ldt.base_addr = (unsigned long)&seg_data2;
838 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
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;
845 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
847 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
852 printf("%d: %016Lx\n", i, ldt_table[i]);
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)));
862 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
863 printf("FS[1] = %02x\n", res);
865 asm volatile ("gs movzbl 0x1, %0" : "=r" (res));
866 printf("GS[1] = %02x\n", res);
868 /* tests with ds/ss (implicit segment case) */
870 asm volatile ("pushl %%ebp\n\t"
875 "movzbl (%%ebp), %1\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);
883 segoff.seg = MK_SEL(2);
884 segoff.offset = 0xabcdef12;
885 asm volatile("lfs %2, %0\n\t"
887 : "=r" (res), "=g" (res2)
889 printf("FS:reg = %04x:%08x\n", res2, res);
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);
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);
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;
908 void test_code16(void)
910 struct modify_ldt_ldt_s ldt;
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;
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;
923 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
925 /* call the first function */
926 asm volatile ("lcall %1, %2"
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"
936 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
937 printf("func3() = 0x%08x\n", res);
940 extern char func_lret32;
941 extern char func_iret32;
948 for(i=0;i<256;i++) table[i] = 256 - i;
950 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
951 printf("xlat: EAX=%08x\n", res);
953 asm volatile ("pushl %%cs ; call %1"
955 : "m" (func_lret32): "memory", "cc");
956 printf("func_lret32=%x\n", res);
958 asm volatile ("pushfl ; pushl %%cs ; call %1"
960 : "m" (func_iret32): "memory", "cc");
961 printf("func_iret32=%x\n", res);
963 /* specific popl test */
964 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
966 printf("popl esp=%x\n", res);
969 uint8_t str_buffer[4096];
971 #define TEST_STRING1(OP, size, DF, REP)\
973 int esi, edi, eax, ecx, eflags;\
975 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
976 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
980 asm volatile ("pushl $0\n\t"\
987 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
988 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
989 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
990 REP #OP size, esi, edi, eax, ecx,\
991 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
994 #define TEST_STRING(OP, REP)\
995 TEST_STRING1(OP, "b", "", REP);\
996 TEST_STRING1(OP, "w", "", REP);\
997 TEST_STRING1(OP, "l", "", REP);\
998 TEST_STRING1(OP, "b", "std", REP);\
999 TEST_STRING1(OP, "w", "std", REP);\
1000 TEST_STRING1(OP, "l", "std", REP)
1002 void test_string(void)
1005 for(i = 0;i < sizeof(str_buffer); i++)
1006 str_buffer[i] = i + 0x56;
1007 TEST_STRING(stos, "");
1008 TEST_STRING(stos, "rep ");
1009 TEST_STRING(lods, ""); /* to verify stos */
1010 TEST_STRING(lods, "rep ");
1011 TEST_STRING(movs, "");
1012 TEST_STRING(movs, "rep ");
1013 TEST_STRING(lods, ""); /* to verify stos */
1015 /* XXX: better tests */
1016 TEST_STRING(scas, "");
1017 TEST_STRING(scas, "repz ");
1018 TEST_STRING(scas, "repnz ");
1019 TEST_STRING(cmps, "");
1020 TEST_STRING(cmps, "repz ");
1021 TEST_STRING(cmps, "repnz ");
1026 static inline void set_bit(uint8_t *a, unsigned int bit)
1028 a[bit / 8] |= (1 << (bit % 8));
1031 static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1033 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1036 static inline void pushw(struct vm86_regs *r, int val)
1038 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1039 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1042 #undef __syscall_return
1043 #define __syscall_return(type, res) \
1045 return (type) (res); \
1048 _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1050 extern char vm86_code_start;
1051 extern char vm86_code_end;
1053 #define VM86_CODE_CS 0x100
1054 #define VM86_CODE_IP 0x100
1056 void test_vm86(void)
1058 struct vm86plus_struct ctx;
1059 struct vm86_regs *r;
1063 vm86_mem = mmap((void *)0x00000000, 0x110000,
1064 PROT_WRITE | PROT_READ | PROT_EXEC,
1065 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1066 if (vm86_mem == MAP_FAILED) {
1067 printf("ERROR: could not map vm86 memory");
1070 memset(&ctx, 0, sizeof(ctx));
1072 /* init basic registers */
1074 r->eip = VM86_CODE_IP;
1083 r->eflags = VIF_MASK;
1085 /* move code to proper address. We use the same layout as a .com
1087 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1088 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1090 /* mark int 0x21 as being emulated */
1091 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1094 ret = vm86(VM86_ENTER, &ctx);
1095 switch(VM86_TYPE(ret)) {
1100 int_num = VM86_ARG(ret);
1101 if (int_num != 0x21)
1103 ah = (r->eax >> 8) & 0xff;
1105 case 0x00: /* exit */
1107 case 0x02: /* write char */
1113 case 0x09: /* write string */
1116 ptr = seg_to_linear(r->ds, r->edx);
1123 r->eax = (r->eax & ~0xff) | '$';
1126 case 0xff: /* extension: write hex number in edx */
1127 printf("%08x\n", (int)r->edx);
1131 printf("unsupported int 0x%02x\n", int_num);
1137 /* a signal came, we just ignore that */
1142 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1147 printf("VM86 end\n");
1148 munmap(vm86_mem, 0x110000);
1151 /* exception tests */
1163 #define REG_TRAPNO TRAPNO
1171 void sig_handler(int sig, siginfo_t *info, void *puc)
1173 struct ucontext *uc = puc;
1175 printf("si_signo=%d si_errno=%d si_code=%d",
1176 info->si_signo, info->si_errno, info->si_code);
1177 printf(" si_addr=0x%08lx",
1178 (unsigned long)info->si_addr);
1181 printf("trapno=0x%02x err=0x%08x",
1182 uc->uc_mcontext.gregs[REG_TRAPNO],
1183 uc->uc_mcontext.gregs[REG_ERR]);
1184 printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
1186 longjmp(jmp_env, 1);
1189 void test_exceptions(void)
1191 struct modify_ldt_ldt_s ldt;
1192 struct sigaction act;
1195 act.sa_sigaction = sig_handler;
1196 sigemptyset(&act.sa_mask);
1197 act.sa_flags = SA_SIGINFO;
1198 sigaction(SIGFPE, &act, NULL);
1199 sigaction(SIGILL, &act, NULL);
1200 sigaction(SIGSEGV, &act, NULL);
1201 sigaction(SIGBUS, &act, NULL);
1202 sigaction(SIGTRAP, &act, NULL);
1204 /* test division by zero reporting */
1205 printf("DIVZ exception:\n");
1206 if (setjmp(jmp_env) == 0) {
1207 /* now divide by zero */
1212 printf("BOUND exception:\n");
1213 if (setjmp(jmp_env) == 0) {
1214 /* bound exception */
1217 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1220 printf("segment exceptions:\n");
1221 if (setjmp(jmp_env) == 0) {
1222 /* load an invalid segment */
1223 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1225 if (setjmp(jmp_env) == 0) {
1226 /* null data segment is valid */
1227 asm volatile ("movl %0, %%fs" : : "r" (3));
1228 /* null stack segment */
1229 asm volatile ("movl %0, %%ss" : : "r" (3));
1232 ldt.entry_number = 1;
1233 ldt.base_addr = (unsigned long)&seg_data1;
1234 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1236 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1237 ldt.read_exec_only = 0;
1238 ldt.limit_in_pages = 1;
1239 ldt.seg_not_present = 1;
1241 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1243 if (setjmp(jmp_env) == 0) {
1244 /* segment not present */
1245 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1248 /* test SEGV reporting */
1249 printf("PF exception:\n");
1250 if (setjmp(jmp_env) == 0) {
1252 /* now store in an invalid address */
1253 *(char *)0x1234 = 1;
1256 /* test SEGV reporting */
1257 printf("PF exception:\n");
1258 if (setjmp(jmp_env) == 0) {
1260 /* read from an invalid address */
1261 v1 = *(char *)0x1234;
1264 /* test illegal instruction reporting */
1265 printf("UD2 exception:\n");
1266 if (setjmp(jmp_env) == 0) {
1267 /* now execute an invalid instruction */
1268 asm volatile("ud2");
1271 printf("INT exception:\n");
1272 if (setjmp(jmp_env) == 0) {
1273 asm volatile ("int $0xfd");
1275 if (setjmp(jmp_env) == 0) {
1276 asm volatile ("int $0x01");
1278 if (setjmp(jmp_env) == 0) {
1279 asm volatile (".byte 0xcd, 0x03");
1281 if (setjmp(jmp_env) == 0) {
1282 asm volatile ("int $0x04");
1284 if (setjmp(jmp_env) == 0) {
1285 asm volatile ("int $0x05");
1288 printf("INT3 exception:\n");
1289 if (setjmp(jmp_env) == 0) {
1290 asm volatile ("int3");
1293 printf("CLI exception:\n");
1294 if (setjmp(jmp_env) == 0) {
1295 asm volatile ("cli");
1298 printf("STI exception:\n");
1299 if (setjmp(jmp_env) == 0) {
1300 asm volatile ("cli");
1303 printf("INTO exception:\n");
1304 if (setjmp(jmp_env) == 0) {
1305 /* overflow exception */
1306 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1309 printf("OUTB exception:\n");
1310 if (setjmp(jmp_env) == 0) {
1311 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1314 printf("INB exception:\n");
1315 if (setjmp(jmp_env) == 0) {
1316 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1319 printf("REP OUTSB exception:\n");
1320 if (setjmp(jmp_env) == 0) {
1321 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1324 printf("REP INSB exception:\n");
1325 if (setjmp(jmp_env) == 0) {
1326 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1329 printf("HLT exception:\n");
1330 if (setjmp(jmp_env) == 0) {
1331 asm volatile ("hlt");
1334 printf("single step exception:\n");
1336 if (setjmp(jmp_env) == 0) {
1337 asm volatile ("pushf\n"
1338 "orl $0x00100, (%%esp)\n"
1340 "movl $0xabcd, %0\n"
1341 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1343 printf("val=0x%x\n", val);
1346 /* self modifying code test */
1348 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1352 typedef int FuncType(void);
1354 void test_self_modifying_code(void)
1358 printf("self modifying code:\n");
1359 printf("func1 = 0x%x\n", ((FuncType *)code)());
1360 for(i = 2; i <= 4; i++) {
1362 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1366 static void *call_end __init_call = NULL;
1368 int main(int argc, char **argv)
1373 ptr = &call_start + 1;
1374 while (*ptr != NULL) {
1391 test_self_modifying_code();