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)
533 printf("(float)%f = %f\n", a, fa);
534 printf("(long double)%f = %Lf\n", a, la);
535 printf("a=%016Lx\n", *(long long *)&a);
536 printf("la=%016Lx %04x\n", *(long long *)&la,
537 *(unsigned short *)((char *)(&la) + 8));
538 printf("a=%f floor(a)=%f\n", a, floor(a));
539 printf("a=%f ceil(a)=%f\n", a, ceil(a));
540 printf("a=%f rint(a)=%f\n", a, rint(a));
544 asm("fld" #N : "=t" (a)); \
545 printf("fld" #N "= %f\n", a);
547 void test_fconst(void)
559 void test_fbcd(double a)
561 unsigned short bcd[5];
564 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
565 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
566 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
567 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
570 #define TEST_ENV(env, prefix)\
572 memset((env), 0xaa, sizeof(*(env)));\
574 prefix "fnstenv %1\n"\
575 prefix "fldenv %1\n"\
576 : "=t" (res) : "m" (*(env)) : "st");\
577 printf("res=%f\n", res);\
578 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
580 (env)->fpus & 0xff00,\
582 memset((env), 0xaa, sizeof(*(env)));\
584 prefix "fnsave %1\n"\
585 prefix "frstor %1\n"\
586 : "=t" (res) : "m" (*(env)) : "st");\
587 printf("res=%f\n", res);\
588 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
590 (env)->fpus & 0xff00,\
592 printf("ST(0) = %Lf\n",\
598 struct __attribute__((packed)) {
606 long double fpregs[8];
608 struct __attribute__((packed)) {
613 long double fpregs[8];
617 TEST_ENV(&float_env16, "data16 ");
618 TEST_ENV(&float_env32, "");
621 void test_floats(void)
632 test_fbcd(1234567890123456);
633 test_fbcd(-123451234567890);
637 /**********************************************/
639 #define TEST_BCD(op, op0, cc_in, cc_mask)\
649 : "=a" (res), "=g" (flags)\
650 : "0" (res), "1" (flags));\
651 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
652 #op, op0, res, cc_in, flags & cc_mask);\
657 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
658 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
659 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
660 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
661 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
662 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
663 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
664 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
665 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
666 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
667 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
668 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
669 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
671 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
672 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
673 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
674 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
675 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
676 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
677 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
678 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
679 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
680 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
681 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
682 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
683 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
685 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
686 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
687 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
688 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
689 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
690 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
691 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
692 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
694 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
695 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
696 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
697 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
698 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
699 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
700 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
701 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
703 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
704 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
707 #define TEST_XCHG(op, size, opconst)\
712 asm(#op " %" size "0, %" size "1" \
713 : "=q" (op0), opconst (op1) \
714 : "0" (op0), "1" (op1));\
715 printf("%-10s A=%08x B=%08x\n",\
719 #define TEST_CMPXCHG(op, size, opconst, eax)\
724 asm(#op " %" size "0, %" size "1" \
725 : "=q" (op0), opconst (op1) \
726 : "0" (op0), "1" (op1), "a" (eax));\
727 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
728 #op, eax, op0, op1);\
733 TEST_XCHG(xchgl, "", "=q");
734 TEST_XCHG(xchgw, "w", "=q");
735 TEST_XCHG(xchgb, "b", "=q");
737 TEST_XCHG(xchgl, "", "=m");
738 TEST_XCHG(xchgw, "w", "=m");
739 TEST_XCHG(xchgb, "b", "=m");
741 TEST_XCHG(xaddl, "", "=q");
742 TEST_XCHG(xaddw, "w", "=q");
743 TEST_XCHG(xaddb, "b", "=q");
745 TEST_XCHG(xaddl, "", "=m");
746 TEST_XCHG(xaddw, "w", "=m");
747 TEST_XCHG(xaddb, "b", "=m");
749 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
750 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
751 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
753 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
754 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
755 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
757 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
758 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
759 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
761 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
762 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
763 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
766 /**********************************************/
767 /* segmentation tests */
770 #include <linux/unistd.h>
772 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
774 uint8_t seg_data1[4096];
775 uint8_t seg_data2[4096];
777 #define MK_SEL(n) (((n) << 3) | 7)
779 #define TEST_LR(op, size, seg, mask)\
783 asm (op " %" size "2, %" size "0\n" \
788 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
789 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
792 /* NOTE: we use Linux modify_ldt syscall */
795 struct modify_ldt_ldt_s ldt;
796 long long ldt_table[3];
802 } __attribute__((packed)) segoff;
804 ldt.entry_number = 1;
805 ldt.base_addr = (unsigned long)&seg_data1;
806 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
808 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
809 ldt.read_exec_only = 0;
810 ldt.limit_in_pages = 1;
811 ldt.seg_not_present = 0;
813 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
815 ldt.entry_number = 2;
816 ldt.base_addr = (unsigned long)&seg_data2;
817 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
819 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
820 ldt.read_exec_only = 0;
821 ldt.limit_in_pages = 1;
822 ldt.seg_not_present = 0;
824 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
826 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
831 printf("%d: %016Lx\n", i, ldt_table[i]);
834 /* do some tests with fs or gs */
835 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
836 asm volatile ("movl %0, %%gs" : : "r" (MK_SEL(2)));
841 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
842 printf("FS[1] = %02x\n", res);
844 asm volatile ("gs movzbl 0x1, %0" : "=r" (res));
845 printf("GS[1] = %02x\n", res);
847 /* tests with ds/ss (implicit segment case) */
849 asm volatile ("pushl %%ebp\n\t"
854 "movzbl (%%ebp), %1\n\t"
857 : "=r" (res), "=r" (res2)
858 : "r" (MK_SEL(1)), "r" (&tmp));
859 printf("DS[1] = %02x\n", res);
860 printf("SS[tmp] = %02x\n", res2);
862 segoff.seg = MK_SEL(2);
863 segoff.offset = 0xabcdef12;
864 asm volatile("lfs %2, %0\n\t"
866 : "=r" (res), "=g" (res2)
868 printf("FS:reg = %04x:%08x\n", res2, res);
870 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
871 TEST_LR("larl", "", MK_SEL(2), 0x0100);
872 TEST_LR("lslw", "w", MK_SEL(2), 0);
873 TEST_LR("lsll", "", MK_SEL(2), 0);
875 TEST_LR("larw", "w", 0xfff8, 0);
876 TEST_LR("larl", "", 0xfff8, 0);
877 TEST_LR("lslw", "w", 0xfff8, 0);
878 TEST_LR("lsll", "", 0xfff8, 0);
881 /* 16 bit code test */
882 extern char code16_start, code16_end;
883 extern char code16_func1;
884 extern char code16_func2;
885 extern char code16_func3;
887 void test_code16(void)
889 struct modify_ldt_ldt_s ldt;
892 /* build a code segment */
893 ldt.entry_number = 1;
894 ldt.base_addr = (unsigned long)&code16_start;
895 ldt.limit = &code16_end - &code16_start;
897 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
898 ldt.read_exec_only = 0;
899 ldt.limit_in_pages = 0;
900 ldt.seg_not_present = 0;
902 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
904 /* call the first function */
905 asm volatile ("lcall %1, %2"
907 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
908 printf("func1() = 0x%08x\n", res);
909 asm volatile ("lcall %2, %3"
910 : "=a" (res), "=c" (res2)
911 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
912 printf("func2() = 0x%08x spdec=%d\n", res, res2);
913 asm volatile ("lcall %1, %2"
915 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
916 printf("func3() = 0x%08x\n", res);
924 for(i=0;i<256;i++) table[i] = 256 - i;
926 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
927 printf("xlat: EAX=%08x\n", res);
930 uint8_t str_buffer[4096];
932 #define TEST_STRING1(OP, size, DF, REP)\
934 int esi, edi, eax, ecx, eflags;\
936 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
937 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
941 asm volatile ("pushl $0\n\t"\
948 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
949 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
950 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
951 REP #OP size, esi, edi, eax, ecx,\
952 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
955 #define TEST_STRING(OP, REP)\
956 TEST_STRING1(OP, "b", "", REP);\
957 TEST_STRING1(OP, "w", "", REP);\
958 TEST_STRING1(OP, "l", "", REP);\
959 TEST_STRING1(OP, "b", "std", REP);\
960 TEST_STRING1(OP, "w", "std", REP);\
961 TEST_STRING1(OP, "l", "std", REP)
963 void test_string(void)
966 for(i = 0;i < sizeof(str_buffer); i++)
967 str_buffer[i] = i + 0x56;
968 TEST_STRING(stos, "");
969 TEST_STRING(stos, "rep ");
970 TEST_STRING(lods, ""); /* to verify stos */
971 TEST_STRING(lods, "rep ");
972 TEST_STRING(movs, "");
973 TEST_STRING(movs, "rep ");
974 TEST_STRING(lods, ""); /* to verify stos */
976 /* XXX: better tests */
977 TEST_STRING(scas, "");
978 TEST_STRING(scas, "repz ");
979 TEST_STRING(scas, "repnz ");
980 TEST_STRING(cmps, "");
981 TEST_STRING(cmps, "repz ");
982 TEST_STRING(cmps, "repnz ");
987 static inline void set_bit(uint8_t *a, unsigned int bit)
989 a[bit / 8] |= (1 << (bit % 8));
992 static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
994 return (uint8_t *)((seg << 4) + (reg & 0xffff));
997 static inline void pushw(struct vm86_regs *r, int val)
999 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1000 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1003 #undef __syscall_return
1004 #define __syscall_return(type, res) \
1006 return (type) (res); \
1009 _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1011 extern char vm86_code_start;
1012 extern char vm86_code_end;
1014 #define VM86_CODE_CS 0x100
1015 #define VM86_CODE_IP 0x100
1017 void test_vm86(void)
1019 struct vm86plus_struct ctx;
1020 struct vm86_regs *r;
1024 vm86_mem = mmap((void *)0x00000000, 0x110000,
1025 PROT_WRITE | PROT_READ | PROT_EXEC,
1026 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1027 if (vm86_mem == MAP_FAILED) {
1028 printf("ERROR: could not map vm86 memory");
1031 memset(&ctx, 0, sizeof(ctx));
1033 /* init basic registers */
1035 r->eip = VM86_CODE_IP;
1044 r->eflags = VIF_MASK;
1046 /* move code to proper address. We use the same layout as a .com
1048 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1049 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1051 /* mark int 0x21 as being emulated */
1052 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1055 ret = vm86(VM86_ENTER, &ctx);
1056 switch(VM86_TYPE(ret)) {
1061 int_num = VM86_ARG(ret);
1062 if (int_num != 0x21)
1064 ah = (r->eax >> 8) & 0xff;
1066 case 0x00: /* exit */
1068 case 0x02: /* write char */
1074 case 0x09: /* write string */
1077 ptr = seg_to_linear(r->ds, r->edx);
1084 r->eax = (r->eax & ~0xff) | '$';
1087 case 0xff: /* extension: write hex number in edx */
1088 printf("%08x\n", (int)r->edx);
1092 printf("unsupported int 0x%02x\n", int_num);
1098 /* a signal came, we just ignore that */
1103 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1108 printf("VM86 end\n");
1109 munmap(vm86_mem, 0x110000);
1112 /* exception tests */
1124 #define REG_TRAPNO TRAPNO
1132 void sig_handler(int sig, siginfo_t *info, void *puc)
1134 struct ucontext *uc = puc;
1136 printf("si_signo=%d si_errno=%d si_code=%d",
1137 info->si_signo, info->si_errno, info->si_code);
1138 printf(" si_addr=0x%08lx",
1139 (unsigned long)info->si_addr);
1142 printf("trapno=0x%02x err=0x%08x",
1143 uc->uc_mcontext.gregs[REG_TRAPNO],
1144 uc->uc_mcontext.gregs[REG_ERR]);
1145 printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
1147 longjmp(jmp_env, 1);
1150 void test_exceptions(void)
1152 struct modify_ldt_ldt_s ldt;
1153 struct sigaction act;
1156 act.sa_sigaction = sig_handler;
1157 sigemptyset(&act.sa_mask);
1158 act.sa_flags = SA_SIGINFO;
1159 sigaction(SIGFPE, &act, NULL);
1160 sigaction(SIGILL, &act, NULL);
1161 sigaction(SIGSEGV, &act, NULL);
1162 sigaction(SIGBUS, &act, NULL);
1163 sigaction(SIGTRAP, &act, NULL);
1165 /* test division by zero reporting */
1166 printf("DIVZ exception:\n");
1167 if (setjmp(jmp_env) == 0) {
1168 /* now divide by zero */
1173 printf("BOUND exception:\n");
1174 if (setjmp(jmp_env) == 0) {
1175 /* bound exception */
1178 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1181 printf("segment exceptions:\n");
1182 if (setjmp(jmp_env) == 0) {
1183 /* load an invalid segment */
1184 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1186 if (setjmp(jmp_env) == 0) {
1187 /* null data segment is valid */
1188 asm volatile ("movl %0, %%fs" : : "r" (3));
1189 /* null stack segment */
1190 asm volatile ("movl %0, %%ss" : : "r" (3));
1193 ldt.entry_number = 1;
1194 ldt.base_addr = (unsigned long)&seg_data1;
1195 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1197 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1198 ldt.read_exec_only = 0;
1199 ldt.limit_in_pages = 1;
1200 ldt.seg_not_present = 1;
1202 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1204 if (setjmp(jmp_env) == 0) {
1205 /* segment not present */
1206 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1209 /* test SEGV reporting */
1210 printf("PF exception:\n");
1211 if (setjmp(jmp_env) == 0) {
1213 /* now store in an invalid address */
1214 *(char *)0x1234 = 1;
1217 /* test SEGV reporting */
1218 printf("PF exception:\n");
1219 if (setjmp(jmp_env) == 0) {
1221 /* read from an invalid address */
1222 v1 = *(char *)0x1234;
1225 /* test illegal instruction reporting */
1226 printf("UD2 exception:\n");
1227 if (setjmp(jmp_env) == 0) {
1228 /* now execute an invalid instruction */
1229 asm volatile("ud2");
1232 printf("INT exception:\n");
1233 if (setjmp(jmp_env) == 0) {
1234 asm volatile ("int $0xfd");
1236 if (setjmp(jmp_env) == 0) {
1237 asm volatile ("int $0x01");
1239 if (setjmp(jmp_env) == 0) {
1240 asm volatile (".byte 0xcd, 0x03");
1242 if (setjmp(jmp_env) == 0) {
1243 asm volatile ("int $0x04");
1245 if (setjmp(jmp_env) == 0) {
1246 asm volatile ("int $0x05");
1249 printf("INT3 exception:\n");
1250 if (setjmp(jmp_env) == 0) {
1251 asm volatile ("int3");
1254 printf("CLI exception:\n");
1255 if (setjmp(jmp_env) == 0) {
1256 asm volatile ("cli");
1259 printf("STI exception:\n");
1260 if (setjmp(jmp_env) == 0) {
1261 asm volatile ("cli");
1264 printf("INTO exception:\n");
1265 if (setjmp(jmp_env) == 0) {
1266 /* overflow exception */
1267 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1270 printf("OUTB exception:\n");
1271 if (setjmp(jmp_env) == 0) {
1272 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1275 printf("INB exception:\n");
1276 if (setjmp(jmp_env) == 0) {
1277 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1280 printf("REP OUTSB exception:\n");
1281 if (setjmp(jmp_env) == 0) {
1282 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1285 printf("REP INSB exception:\n");
1286 if (setjmp(jmp_env) == 0) {
1287 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1290 printf("HLT exception:\n");
1291 if (setjmp(jmp_env) == 0) {
1292 asm volatile ("hlt");
1295 printf("single step exception:\n");
1297 if (setjmp(jmp_env) == 0) {
1298 asm volatile ("pushf\n"
1299 "orl $0x00100, (%%esp)\n"
1301 "movl $0xabcd, %0\n"
1302 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1304 printf("val=0x%x\n", val);
1307 /* self modifying code test */
1309 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1313 typedef int FuncType(void);
1315 void test_self_modifying_code(void)
1319 printf("self modifying code:\n");
1320 printf("func1 = 0x%x\n", ((FuncType *)code)());
1321 for(i = 2; i <= 4; i++) {
1323 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1327 static void *call_end __init_call = NULL;
1329 int main(int argc, char **argv)
1334 ptr = &call_start + 1;
1335 while (*ptr != NULL) {
1352 test_self_modifying_code();