8 #include <sys/ucontext.h>
14 #define xglue(x, y) x ## y
15 #define glue(x, y) xglue(x, y)
16 #define stringify(s) tostring(s)
17 #define tostring(s) #s
26 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
28 static void *call_start __init_call
= NULL
;
30 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
33 #include "test-i386.h"
36 #include "test-i386.h"
39 #include "test-i386.h"
42 #include "test-i386.h"
45 #include "test-i386.h"
48 #include "test-i386.h"
52 #include "test-i386.h"
56 #include "test-i386.h"
61 #include "test-i386.h"
66 #include "test-i386.h"
71 #include "test-i386.h"
76 #include "test-i386.h"
79 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
82 #include "test-i386-shift.h"
85 #include "test-i386-shift.h"
88 #include "test-i386-shift.h"
91 #include "test-i386-shift.h"
94 #include "test-i386-shift.h"
98 #include "test-i386-shift.h"
102 #include "test-i386-shift.h"
107 #include "test-i386-shift.h"
112 #include "test-i386-shift.h"
114 /* XXX: should be more precise ? */
116 #define CC_MASK (CC_C)
120 #include "test-i386-shift.h"
124 #include "test-i386-shift.h"
128 #include "test-i386-shift.h"
132 #include "test-i386-shift.h"
134 /* lea test (modrm support) */
135 #define TEST_LEA(STR)\
137 asm("leal " STR ", %0"\
139 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
140 printf("lea %s = %08x\n", STR, res);\
143 #define TEST_LEA16(STR)\
145 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
147 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
148 printf("lea %s = %08x\n", STR, res);\
154 int eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
171 TEST_LEA("0x40(%%eax)");
172 TEST_LEA("0x40(%%ebx)");
173 TEST_LEA("0x40(%%ecx)");
174 TEST_LEA("0x40(%%edx)");
175 TEST_LEA("0x40(%%esi)");
176 TEST_LEA("0x40(%%edi)");
178 TEST_LEA("0x4000(%%eax)");
179 TEST_LEA("0x4000(%%ebx)");
180 TEST_LEA("0x4000(%%ecx)");
181 TEST_LEA("0x4000(%%edx)");
182 TEST_LEA("0x4000(%%esi)");
183 TEST_LEA("0x4000(%%edi)");
185 TEST_LEA("(%%eax, %%ecx)");
186 TEST_LEA("(%%ebx, %%edx)");
187 TEST_LEA("(%%ecx, %%ecx)");
188 TEST_LEA("(%%edx, %%ecx)");
189 TEST_LEA("(%%esi, %%ecx)");
190 TEST_LEA("(%%edi, %%ecx)");
192 TEST_LEA("0x40(%%eax, %%ecx)");
193 TEST_LEA("0x4000(%%ebx, %%edx)");
195 TEST_LEA("(%%ecx, %%ecx, 2)");
196 TEST_LEA("(%%edx, %%ecx, 4)");
197 TEST_LEA("(%%esi, %%ecx, 8)");
199 TEST_LEA("(,%%eax, 2)");
200 TEST_LEA("(,%%ebx, 4)");
201 TEST_LEA("(,%%ecx, 8)");
203 TEST_LEA("0x40(,%%eax, 2)");
204 TEST_LEA("0x40(,%%ebx, 4)");
205 TEST_LEA("0x40(,%%ecx, 8)");
208 TEST_LEA("-10(%%ecx, %%ecx, 2)");
209 TEST_LEA("-10(%%edx, %%ecx, 4)");
210 TEST_LEA("-10(%%esi, %%ecx, 8)");
212 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
213 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
214 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
216 /* limited 16 bit addressing test */
217 TEST_LEA16("0x4000");
218 TEST_LEA16("(%%bx)");
219 TEST_LEA16("(%%si)");
220 TEST_LEA16("(%%di)");
221 TEST_LEA16("0x40(%%bx)");
222 TEST_LEA16("0x40(%%si)");
223 TEST_LEA16("0x40(%%di)");
224 TEST_LEA16("0x4000(%%bx)");
225 TEST_LEA16("0x4000(%%si)");
226 TEST_LEA16("(%%bx,%%si)");
227 TEST_LEA16("(%%bx,%%di)");
228 TEST_LEA16("0x40(%%bx,%%si)");
229 TEST_LEA16("0x40(%%bx,%%di)");
230 TEST_LEA16("0x4000(%%bx,%%si)");
231 TEST_LEA16("0x4000(%%bx,%%di)");
234 #define TEST_JCC(JCC, v1, v2)\
237 asm("movl $1, %0\n\t"\
243 : "r" (v1), "r" (v2));\
244 printf("%-10s %d\n", "j" JCC, res);\
246 asm("movl $0, %0\n\t"\
248 "set" JCC " %b0\n\t"\
250 : "r" (v1), "r" (v2));\
251 printf("%-10s %d\n", "set" JCC, res);\
253 asm("movl $0x12345678, %0\n\t"\
255 "cmov" JCC "l %3, %0\n\t"\
257 : "r" (v1), "r" (v2), "m" (1));\
258 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
259 asm("movl $0x12345678, %0\n\t"\
261 "cmov" JCC "w %w3, %w0\n\t"\
263 : "r" (v1), "r" (v2), "r" (1));\
264 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
268 /* various jump tests */
271 TEST_JCC("ne", 1, 1);
272 TEST_JCC("ne", 1, 0);
279 TEST_JCC("l", 1, -1);
281 TEST_JCC("le", 1, 1);
282 TEST_JCC("le", 1, 0);
283 TEST_JCC("le", 1, -1);
285 TEST_JCC("ge", 1, 1);
286 TEST_JCC("ge", 1, 0);
287 TEST_JCC("ge", -1, 1);
291 TEST_JCC("g", 1, -1);
295 TEST_JCC("b", 1, -1);
297 TEST_JCC("be", 1, 1);
298 TEST_JCC("be", 1, 0);
299 TEST_JCC("be", 1, -1);
301 TEST_JCC("ae", 1, 1);
302 TEST_JCC("ae", 1, 0);
303 TEST_JCC("ae", 1, -1);
307 TEST_JCC("a", 1, -1);
313 TEST_JCC("np", 1, 1);
314 TEST_JCC("np", 1, 0);
316 TEST_JCC("o", 0x7fffffff, 0);
317 TEST_JCC("o", 0x7fffffff, -1);
319 TEST_JCC("no", 0x7fffffff, 0);
320 TEST_JCC("no", 0x7fffffff, -1);
323 TEST_JCC("s", 0, -1);
326 TEST_JCC("ns", 0, 1);
327 TEST_JCC("ns", 0, -1);
328 TEST_JCC("ns", 0, 0);
332 #define CC_MASK (CC_O | CC_C)
335 #include "test-i386-muldiv.h"
338 #include "test-i386-muldiv.h"
344 #include "test-i386-muldiv.h"
347 #include "test-i386-muldiv.h"
349 void test_imulw2(int op0
, int op1
)
351 int res
, s1
, s0
, flags
;
361 : "=q" (res
), "=g" (flags
)
362 : "q" (s1
), "0" (res
), "1" (flags
));
363 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
364 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
367 void test_imull2(int op0
, int op1
)
369 int res
, s1
, s0
, flags
;
379 : "=q" (res
), "=g" (flags
)
380 : "q" (s1
), "0" (res
), "1" (flags
));
381 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
382 "imull", s0
, s1
, res
, flags
& CC_MASK
);
387 test_imulb(0x1234561d, 4);
389 test_imulb(0x80, 0x80);
390 test_imulb(0x10, 0x10);
392 test_imulw(0, 0x1234001d, 45);
393 test_imulw(0, 23, -45);
394 test_imulw(0, 0x8000, 0x8000);
395 test_imulw(0, 0x100, 0x100);
397 test_imull(0, 0x1234001d, 45);
398 test_imull(0, 23, -45);
399 test_imull(0, 0x80000000, 0x80000000);
400 test_imull(0, 0x10000, 0x10000);
402 test_mulb(0x1234561d, 4);
404 test_mulb(0x80, 0x80);
405 test_mulb(0x10, 0x10);
407 test_mulw(0, 0x1234001d, 45);
408 test_mulw(0, 23, -45);
409 test_mulw(0, 0x8000, 0x8000);
410 test_mulw(0, 0x100, 0x100);
412 test_mull(0, 0x1234001d, 45);
413 test_mull(0, 23, -45);
414 test_mull(0, 0x80000000, 0x80000000);
415 test_mull(0, 0x10000, 0x10000);
417 test_imulw2(0x1234001d, 45);
418 test_imulw2(23, -45);
419 test_imulw2(0x8000, 0x8000);
420 test_imulw2(0x100, 0x100);
422 test_imull2(0x1234001d, 45);
423 test_imull2(23, -45);
424 test_imull2(0x80000000, 0x80000000);
425 test_imull2(0x10000, 0x10000);
427 test_idivb(0x12341678, 0x127e);
428 test_idivb(0x43210123, -5);
429 test_idivb(0x12340004, -1);
431 test_idivw(0, 0x12345678, 12347);
432 test_idivw(0, -23223, -45);
433 test_idivw(0, 0x12348000, -1);
434 test_idivw(0x12343, 0x12345678, 0x81238567);
436 test_idivl(0, 0x12345678, 12347);
437 test_idivl(0, -233223, -45);
438 test_idivl(0, 0x80000000, -1);
439 test_idivl(0x12343, 0x12345678, 0x81234567);
441 test_divb(0x12341678, 0x127e);
442 test_divb(0x43210123, -5);
443 test_divb(0x12340004, -1);
445 test_divw(0, 0x12345678, 12347);
446 test_divw(0, -23223, -45);
447 test_divw(0, 0x12348000, -1);
448 test_divw(0x12343, 0x12345678, 0x81238567);
450 test_divl(0, 0x12345678, 12347);
451 test_divl(0, -233223, -45);
452 test_divl(0, 0x80000000, -1);
453 test_divl(0x12343, 0x12345678, 0x81234567);
456 #define TEST_BSX(op, size, op0)\
460 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
461 : "=r" (res), "=q" (resz)\
463 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
468 TEST_BSX(bsrw
, "w", 0);
469 TEST_BSX(bsrw
, "w", 0x12340128);
470 TEST_BSX(bsrl
, "", 0);
471 TEST_BSX(bsrl
, "", 0x00340128);
472 TEST_BSX(bsfw
, "w", 0);
473 TEST_BSX(bsfw
, "w", 0x12340128);
474 TEST_BSX(bsfl
, "", 0);
475 TEST_BSX(bsfl
, "", 0x00340128);
478 /**********************************************/
480 void test_fops(double a
, double b
)
482 printf("a=%f b=%f a+b=%f\n", a
, b
, a
+ 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 fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
487 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
488 printf("a=%f sin(a)=%f\n", a
, sin(a
));
489 printf("a=%f cos(a)=%f\n", a
, cos(a
));
490 printf("a=%f tan(a)=%f\n", a
, tan(a
));
491 printf("a=%f log(a)=%f\n", a
, log(a
));
492 printf("a=%f exp(a)=%f\n", a
, exp(a
));
493 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
494 /* just to test some op combining */
495 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
496 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
497 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
501 void test_fcmp(double a
, double b
)
503 printf("(%f<%f)=%d\n",
505 printf("(%f<=%f)=%d\n",
507 printf("(%f==%f)=%d\n",
509 printf("(%f>%f)=%d\n",
511 printf("(%f<=%f)=%d\n",
515 void test_fcvt(double a
)
522 printf("(float)%f = %f\n", a
, fa
);
523 printf("(long double)%f = %Lf\n", a
, la
);
524 printf("a=%016Lx\n", *(long long *)&a
);
525 printf("la=%016Lx %04x\n", *(long long *)&la
,
526 *(unsigned short *)((char *)(&la
) + 8));
527 printf("a=%f floor(a)=%f\n", a
, floor(a
));
528 printf("a=%f ceil(a)=%f\n", a
, ceil(a
));
529 printf("a=%f rint(a)=%f\n", a
, rint(a
));
533 asm("fld" #N : "=t" (a)); \
534 printf("fld" #N "= %f\n", a);
536 void test_fconst(void)
548 void test_fbcd(double a
)
550 unsigned short bcd
[5];
553 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
554 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
555 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
556 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
559 void test_floats(void)
570 test_fbcd(1234567890123456);
571 test_fbcd(-123451234567890);
574 /**********************************************/
576 #define TEST_BCD(op, op0, cc_in, cc_mask)\
586 : "=a" (res), "=g" (flags)\
587 : "0" (res), "1" (flags));\
588 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
589 #op, op0, res, cc_in, flags & cc_mask);\
594 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
595 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
596 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
597 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
598 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
599 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
600 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
601 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
602 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
603 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
604 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
605 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
606 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
608 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
609 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
610 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
611 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
612 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
613 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
614 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
615 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
616 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
617 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
618 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
619 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
620 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
622 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
623 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
624 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
625 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
626 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
627 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
628 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
629 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
631 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
632 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
633 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
634 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
635 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
636 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
637 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
638 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
640 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
641 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
644 #define TEST_XCHG(op, size, opconst)\
649 asm(#op " %" size "0, %" size "1" \
650 : "=q" (op0), opconst (op1) \
651 : "0" (op0), "1" (op1));\
652 printf("%-10s A=%08x B=%08x\n",\
656 #define TEST_CMPXCHG(op, size, opconst, eax)\
661 asm(#op " %" size "0, %" size "1" \
662 : "=q" (op0), opconst (op1) \
663 : "0" (op0), "1" (op1), "a" (eax));\
664 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
665 #op, eax, op0, op1);\
670 TEST_XCHG(xchgl
, "", "=q");
671 TEST_XCHG(xchgw
, "w", "=q");
672 TEST_XCHG(xchgb
, "b", "=q");
674 TEST_XCHG(xchgl
, "", "=m");
675 TEST_XCHG(xchgw
, "w", "=m");
676 TEST_XCHG(xchgb
, "b", "=m");
678 TEST_XCHG(xaddl
, "", "=q");
679 TEST_XCHG(xaddw
, "w", "=q");
680 TEST_XCHG(xaddb
, "b", "=q");
682 TEST_XCHG(xaddl
, "", "=m");
683 TEST_XCHG(xaddw
, "w", "=m");
684 TEST_XCHG(xaddb
, "b", "=m");
686 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfbca7654);
687 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfbca7654);
688 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfbca7654);
690 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfffefdfc);
691 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfffefdfc);
692 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfffefdfc);
694 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfbca7654);
695 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfbca7654);
696 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfbca7654);
698 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfffefdfc);
699 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfffefdfc);
700 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfffefdfc);
703 /**********************************************/
704 /* segmentation tests */
707 #include <linux/unistd.h>
709 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
711 uint8_t seg_data1
[4096];
712 uint8_t seg_data2
[4096];
714 #define MK_SEL(n) (((n) << 3) | 7)
716 #define TEST_LR(op, size, seg, mask)\
720 asm (op " %" size "2, %" size "0\n" \
725 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
726 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
729 /* NOTE: we use Linux modify_ldt syscall */
732 struct modify_ldt_ldt_s ldt
;
733 long long ldt_table
[3];
739 } __attribute__((packed
)) segoff
;
741 ldt
.entry_number
= 1;
742 ldt
.base_addr
= (unsigned long)&seg_data1
;
743 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
745 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
746 ldt
.read_exec_only
= 0;
747 ldt
.limit_in_pages
= 1;
748 ldt
.seg_not_present
= 0;
750 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
752 ldt
.entry_number
= 2;
753 ldt
.base_addr
= (unsigned long)&seg_data2
;
754 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
756 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
757 ldt
.read_exec_only
= 0;
758 ldt
.limit_in_pages
= 1;
759 ldt
.seg_not_present
= 0;
761 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
763 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
768 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
771 /* do some tests with fs or gs */
772 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
773 asm volatile ("movl %0, %%gs" : : "r" (MK_SEL(2)));
778 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
779 printf("FS[1] = %02x\n", res
);
781 asm volatile ("gs movzbl 0x1, %0" : "=r" (res
));
782 printf("GS[1] = %02x\n", res
);
784 /* tests with ds/ss (implicit segment case) */
786 asm volatile ("pushl %%ebp\n\t"
791 "movzbl (%%ebp), %1\n\t"
794 : "=r" (res
), "=r" (res2
)
795 : "r" (MK_SEL(1)), "r" (&tmp
));
796 printf("DS[1] = %02x\n", res
);
797 printf("SS[tmp] = %02x\n", res2
);
799 segoff
.seg
= MK_SEL(2);
800 segoff
.offset
= 0xabcdef12;
801 asm volatile("lfs %2, %0\n\t"
803 : "=r" (res
), "=g" (res2
)
805 printf("FS:reg = %04x:%08x\n", res2
, res
);
807 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
808 TEST_LR("larl", "", MK_SEL(2), 0x0100);
809 TEST_LR("lslw", "w", MK_SEL(2), 0);
810 TEST_LR("lsll", "", MK_SEL(2), 0);
812 TEST_LR("larw", "w", 0xfff8, 0);
813 TEST_LR("larl", "", 0xfff8, 0);
814 TEST_LR("lslw", "w", 0xfff8, 0);
815 TEST_LR("lsll", "", 0xfff8, 0);
818 /* 16 bit code test */
819 extern char code16_start
, code16_end
;
820 extern char code16_func1
;
821 extern char code16_func2
;
822 extern char code16_func3
;
824 void test_code16(void)
826 struct modify_ldt_ldt_s ldt
;
829 /* build a code segment */
830 ldt
.entry_number
= 1;
831 ldt
.base_addr
= (unsigned long)&code16_start
;
832 ldt
.limit
= &code16_end
- &code16_start
;
834 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
835 ldt
.read_exec_only
= 0;
836 ldt
.limit_in_pages
= 0;
837 ldt
.seg_not_present
= 0;
839 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
841 /* call the first function */
842 asm volatile ("lcall %1, %2"
844 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
845 printf("func1() = 0x%08x\n", res
);
846 asm volatile ("lcall %2, %3"
847 : "=a" (res
), "=c" (res2
)
848 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
849 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
850 asm volatile ("lcall %1, %2"
852 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
853 printf("func3() = 0x%08x\n", res
);
861 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
863 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
864 printf("xlat: EAX=%08x\n", res
);
867 uint8_t str_buffer
[4096];
869 #define TEST_STRING1(OP, size, DF, REP)\
871 int esi, edi, eax, ecx, eflags;\
873 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
874 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
878 asm volatile ("pushl $0\n\t"\
885 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
886 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
887 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
888 REP #OP size, esi, edi, eax, ecx,\
889 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
892 #define TEST_STRING(OP, REP)\
893 TEST_STRING1(OP, "b", "", REP);\
894 TEST_STRING1(OP, "w", "", REP);\
895 TEST_STRING1(OP, "l", "", REP);\
896 TEST_STRING1(OP, "b", "std", REP);\
897 TEST_STRING1(OP, "w", "std", REP);\
898 TEST_STRING1(OP, "l", "std", REP)
900 void test_string(void)
903 for(i
= 0;i
< sizeof(str_buffer
); i
++)
904 str_buffer
[i
] = i
+ 0x56;
905 TEST_STRING(stos
, "");
906 TEST_STRING(stos
, "rep ");
907 TEST_STRING(lods
, ""); /* to verify stos */
908 TEST_STRING(lods
, "rep ");
909 TEST_STRING(movs
, "");
910 TEST_STRING(movs
, "rep ");
911 TEST_STRING(lods
, ""); /* to verify stos */
913 /* XXX: better tests */
914 TEST_STRING(scas
, "");
915 TEST_STRING(scas
, "repz ");
916 TEST_STRING(scas
, "repnz ");
917 TEST_STRING(cmps
, "");
918 TEST_STRING(cmps
, "repz ");
919 TEST_STRING(cmps
, "repnz ");
924 static inline void set_bit(uint8_t *a
, unsigned int bit
)
926 a
[bit
/ 8] |= (1 << (bit
% 8));
929 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
931 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
934 static inline void pushw(struct vm86_regs
*r
, int val
)
936 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
937 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
940 #undef __syscall_return
941 #define __syscall_return(type, res) \
943 return (type) (res); \
946 _syscall2(int, vm86
, int, func
, struct vm86plus_struct
*, v86
)
948 extern char vm86_code_start
;
949 extern char vm86_code_end
;
951 #define VM86_CODE_CS 0x100
952 #define VM86_CODE_IP 0x100
956 struct vm86plus_struct ctx
;
961 vm86_mem
= mmap((void *)0x00000000, 0x110000,
962 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
963 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
964 if (vm86_mem
== MAP_FAILED
) {
965 printf("ERROR: could not map vm86 memory");
968 memset(&ctx
, 0, sizeof(ctx
));
970 /* init basic registers */
972 r
->eip
= VM86_CODE_IP
;
981 r
->eflags
= VIF_MASK
;
983 /* move code to proper address. We use the same layout as a .com
985 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
986 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
988 /* mark int 0x21 as being emulated */
989 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
992 ret
= vm86(VM86_ENTER
, &ctx
);
993 switch(VM86_TYPE(ret
)) {
998 int_num
= VM86_ARG(ret
);
1001 ah
= (r
->eax
>> 8) & 0xff;
1003 case 0x00: /* exit */
1005 case 0x02: /* write char */
1011 case 0x09: /* write string */
1014 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1021 r
->eax
= (r
->eax
& ~0xff) | '$';
1024 case 0xff: /* extension: write hex number in edx */
1025 printf("%08x\n", (int)r
->edx
);
1029 printf("unsupported int 0x%02x\n", int_num
);
1035 /* a signal came, we just ignore that */
1040 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1045 printf("VM86 end\n");
1046 munmap(vm86_mem
, 0x110000);
1049 /* exception tests */
1061 #define REG_TRAPNO TRAPNO
1071 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1073 struct ucontext
*uc
= puc
;
1075 printf("si_signo=%d si_errno=%d si_code=%d",
1076 info
->si_signo
, info
->si_errno
, info
->si_code
);
1078 printf(" si_addr=0x%08lx",
1079 (unsigned long)info
->si_addr
);
1083 printf("trapno=0x%02x err=0x%08x",
1084 uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1085 uc
->uc_mcontext
.gregs
[REG_ERR
]);
1087 printf(" EIP=0x%08x", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1089 longjmp(jmp_env
, 1);
1092 void test_exceptions(void)
1094 struct sigaction act
;
1097 act
.sa_sigaction
= sig_handler
;
1098 sigemptyset(&act
.sa_mask
);
1099 act
.sa_flags
= SA_SIGINFO
;
1100 sigaction(SIGFPE
, &act
, NULL
);
1101 sigaction(SIGILL
, &act
, NULL
);
1102 sigaction(SIGSEGV
, &act
, NULL
);
1103 sigaction(SIGTRAP
, &act
, NULL
);
1105 /* test division by zero reporting */
1108 printf("DIVZ exception (currently imprecise):\n");
1109 if (setjmp(jmp_env
) == 0) {
1110 /* now divide by zero */
1116 printf("BOUND exception (currently imprecise):\n");
1117 if (setjmp(jmp_env
) == 0) {
1118 /* bound exception */
1121 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
));
1124 /* test SEGV reporting */
1125 printf("PF exception (currently imprecise):\n");
1126 if (setjmp(jmp_env
) == 0) {
1127 /* now store in an invalid address */
1128 *(char *)0x1234 = 1;
1131 /* test SEGV reporting */
1132 printf("PF exception (currently imprecise):\n");
1133 if (setjmp(jmp_env
) == 0) {
1134 /* read from an invalid address */
1135 v1
= *(char *)0x1234;
1138 printf("segment GPF exception (currently imprecise):\n");
1139 if (setjmp(jmp_env
) == 0) {
1140 /* load an invalid segment */
1141 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 0));
1145 /* test illegal instruction reporting */
1146 printf("UD2 exception:\n");
1147 if (setjmp(jmp_env
) == 0) {
1148 /* now execute an invalid instruction */
1149 asm volatile("ud2");
1152 printf("INT exception:\n");
1153 if (setjmp(jmp_env
) == 0) {
1154 asm volatile ("int $0xfd");
1157 printf("INT3 exception:\n");
1158 if (setjmp(jmp_env
) == 0) {
1159 asm volatile ("int3");
1162 printf("CLI exception:\n");
1163 if (setjmp(jmp_env
) == 0) {
1164 asm volatile ("cli");
1167 printf("STI exception:\n");
1168 if (setjmp(jmp_env
) == 0) {
1169 asm volatile ("cli");
1172 printf("INTO exception:\n");
1173 if (setjmp(jmp_env
) == 0) {
1174 /* overflow exception */
1175 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1178 printf("OUTB exception:\n");
1179 if (setjmp(jmp_env
) == 0) {
1180 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1183 printf("INB exception:\n");
1184 if (setjmp(jmp_env
) == 0) {
1185 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1188 printf("REP OUTSB exception:\n");
1189 if (setjmp(jmp_env
) == 0) {
1190 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1193 printf("REP INSB exception:\n");
1194 if (setjmp(jmp_env
) == 0) {
1195 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1198 printf("HLT exception:\n");
1199 if (setjmp(jmp_env
) == 0) {
1200 asm volatile ("hlt");
1203 printf("single step exception:\n");
1205 if (setjmp(jmp_env
) == 0) {
1206 asm volatile ("pushf\n"
1207 "orl $0x00100, (%%esp)\n"
1209 "movl $0xabcd, %0\n"
1210 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1212 printf("val=0x%x\n", val
);
1215 /* self modifying code test */
1217 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1221 void test_self_modifying_code(void)
1225 func
= (void *)code
;
1226 printf("self modifying code:\n");
1227 printf("func1 = 0x%x\n", func());
1229 printf("func1 = 0x%x\n", func());
1232 static void *call_end __init_call
= NULL
;
1234 int main(int argc
, char **argv
)
1239 ptr
= &call_start
+ 1;
1240 while (*ptr
!= NULL
) {
1257 test_self_modifying_code();