4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <sys/ucontext.h>
31 #if !defined(__x86_64__)
35 //#define LINUX_VM86_IOPL_FIX
36 //#define TEST_P4_FLAGS
40 #if defined(__x86_64__)
41 #define FMT64X "%016lx"
42 #define FMTLX "%016lx"
43 #define X86_64_ONLY(x) x
45 #define FMT64X "%016" PRIx64
47 #define X86_64_ONLY(x)
54 #define xglue(x, y) x ## y
55 #define glue(x, y) xglue(x, y)
56 #define stringify(s) tostring(s)
57 #define tostring(s) #s
66 #define __init_call __attribute__ ((unused,__section__ ("initcall")))
68 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
70 #if defined(__x86_64__)
71 static inline long i2l(long v
)
73 return v
| ((v
^ 0xabcd) << 32);
76 static inline long i2l(long v
)
83 #include "test-i386.h"
86 #include "test-i386.h"
89 #include "test-i386.h"
92 #include "test-i386.h"
95 #include "test-i386.h"
98 #include "test-i386.h"
102 #include "test-i386.h"
106 #include "test-i386.h"
111 #include "test-i386.h"
116 #include "test-i386.h"
121 #include "test-i386.h"
126 #include "test-i386.h"
129 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
132 #include "test-i386-shift.h"
135 #include "test-i386-shift.h"
138 #include "test-i386-shift.h"
141 #include "test-i386-shift.h"
144 #include "test-i386-shift.h"
148 #include "test-i386-shift.h"
152 #include "test-i386-shift.h"
157 #include "test-i386-shift.h"
162 #include "test-i386-shift.h"
164 /* XXX: should be more precise ? */
166 #define CC_MASK (CC_C)
170 #include "test-i386-shift.h"
174 #include "test-i386-shift.h"
178 #include "test-i386-shift.h"
182 #include "test-i386-shift.h"
184 /* lea test (modrm support) */
185 #define TEST_LEAQ(STR)\
187 asm("lea " STR ", %0"\
189 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
190 printf("lea %s = " FMTLX "\n", STR, res);\
193 #define TEST_LEA(STR)\
195 asm("lea " STR ", %0"\
197 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
198 printf("lea %s = " FMTLX "\n", STR, res);\
201 #define TEST_LEA16(STR)\
203 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
205 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
206 printf("lea %s = %08lx\n", STR, res);\
212 long eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
229 TEST_LEA("0x40(%%eax)");
230 TEST_LEA("0x40(%%ebx)");
231 TEST_LEA("0x40(%%ecx)");
232 TEST_LEA("0x40(%%edx)");
233 TEST_LEA("0x40(%%esi)");
234 TEST_LEA("0x40(%%edi)");
236 TEST_LEA("0x4000(%%eax)");
237 TEST_LEA("0x4000(%%ebx)");
238 TEST_LEA("0x4000(%%ecx)");
239 TEST_LEA("0x4000(%%edx)");
240 TEST_LEA("0x4000(%%esi)");
241 TEST_LEA("0x4000(%%edi)");
243 TEST_LEA("(%%eax, %%ecx)");
244 TEST_LEA("(%%ebx, %%edx)");
245 TEST_LEA("(%%ecx, %%ecx)");
246 TEST_LEA("(%%edx, %%ecx)");
247 TEST_LEA("(%%esi, %%ecx)");
248 TEST_LEA("(%%edi, %%ecx)");
250 TEST_LEA("0x40(%%eax, %%ecx)");
251 TEST_LEA("0x4000(%%ebx, %%edx)");
253 TEST_LEA("(%%ecx, %%ecx, 2)");
254 TEST_LEA("(%%edx, %%ecx, 4)");
255 TEST_LEA("(%%esi, %%ecx, 8)");
257 TEST_LEA("(,%%eax, 2)");
258 TEST_LEA("(,%%ebx, 4)");
259 TEST_LEA("(,%%ecx, 8)");
261 TEST_LEA("0x40(,%%eax, 2)");
262 TEST_LEA("0x40(,%%ebx, 4)");
263 TEST_LEA("0x40(,%%ecx, 8)");
266 TEST_LEA("-10(%%ecx, %%ecx, 2)");
267 TEST_LEA("-10(%%edx, %%ecx, 4)");
268 TEST_LEA("-10(%%esi, %%ecx, 8)");
270 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
271 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
272 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
274 #if defined(__x86_64__)
276 TEST_LEAQ("0x4000(%%rip)");
278 TEST_LEAQ("(%%rax)");
279 TEST_LEAQ("(%%rbx)");
280 TEST_LEAQ("(%%rcx)");
281 TEST_LEAQ("(%%rdx)");
282 TEST_LEAQ("(%%rsi)");
283 TEST_LEAQ("(%%rdi)");
285 TEST_LEAQ("0x40(%%rax)");
286 TEST_LEAQ("0x40(%%rbx)");
287 TEST_LEAQ("0x40(%%rcx)");
288 TEST_LEAQ("0x40(%%rdx)");
289 TEST_LEAQ("0x40(%%rsi)");
290 TEST_LEAQ("0x40(%%rdi)");
292 TEST_LEAQ("0x4000(%%rax)");
293 TEST_LEAQ("0x4000(%%rbx)");
294 TEST_LEAQ("0x4000(%%rcx)");
295 TEST_LEAQ("0x4000(%%rdx)");
296 TEST_LEAQ("0x4000(%%rsi)");
297 TEST_LEAQ("0x4000(%%rdi)");
299 TEST_LEAQ("(%%rax, %%rcx)");
300 TEST_LEAQ("(%%rbx, %%rdx)");
301 TEST_LEAQ("(%%rcx, %%rcx)");
302 TEST_LEAQ("(%%rdx, %%rcx)");
303 TEST_LEAQ("(%%rsi, %%rcx)");
304 TEST_LEAQ("(%%rdi, %%rcx)");
306 TEST_LEAQ("0x40(%%rax, %%rcx)");
307 TEST_LEAQ("0x4000(%%rbx, %%rdx)");
309 TEST_LEAQ("(%%rcx, %%rcx, 2)");
310 TEST_LEAQ("(%%rdx, %%rcx, 4)");
311 TEST_LEAQ("(%%rsi, %%rcx, 8)");
313 TEST_LEAQ("(,%%rax, 2)");
314 TEST_LEAQ("(,%%rbx, 4)");
315 TEST_LEAQ("(,%%rcx, 8)");
317 TEST_LEAQ("0x40(,%%rax, 2)");
318 TEST_LEAQ("0x40(,%%rbx, 4)");
319 TEST_LEAQ("0x40(,%%rcx, 8)");
322 TEST_LEAQ("-10(%%rcx, %%rcx, 2)");
323 TEST_LEAQ("-10(%%rdx, %%rcx, 4)");
324 TEST_LEAQ("-10(%%rsi, %%rcx, 8)");
326 TEST_LEAQ("0x4000(%%rcx, %%rcx, 2)");
327 TEST_LEAQ("0x4000(%%rdx, %%rcx, 4)");
328 TEST_LEAQ("0x4000(%%rsi, %%rcx, 8)");
330 /* limited 16 bit addressing test */
331 TEST_LEA16("0x4000");
332 TEST_LEA16("(%%bx)");
333 TEST_LEA16("(%%si)");
334 TEST_LEA16("(%%di)");
335 TEST_LEA16("0x40(%%bx)");
336 TEST_LEA16("0x40(%%si)");
337 TEST_LEA16("0x40(%%di)");
338 TEST_LEA16("0x4000(%%bx)");
339 TEST_LEA16("0x4000(%%si)");
340 TEST_LEA16("(%%bx,%%si)");
341 TEST_LEA16("(%%bx,%%di)");
342 TEST_LEA16("0x40(%%bx,%%si)");
343 TEST_LEA16("0x40(%%bx,%%di)");
344 TEST_LEA16("0x4000(%%bx,%%si)");
345 TEST_LEA16("0x4000(%%bx,%%di)");
349 #define TEST_JCC(JCC, v1, v2)\
352 asm("movl $1, %0\n\t"\
358 : "r" (v1), "r" (v2));\
359 printf("%-10s %d\n", "j" JCC, res);\
361 asm("movl $0, %0\n\t"\
363 "set" JCC " %b0\n\t"\
365 : "r" (v1), "r" (v2));\
366 printf("%-10s %d\n", "set" JCC, res);\
369 long res = i2l(0x12345678);\
371 asm("cmpl %2, %1\n\t"\
372 "cmov" JCC "q %3, %0\n\t"\
374 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
375 printf("%-10s R=" FMTLX "\n", "cmov" JCC "q", res);)\
376 asm("cmpl %2, %1\n\t"\
377 "cmov" JCC "l %k3, %k0\n\t"\
379 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
380 printf("%-10s R=" FMTLX "\n", "cmov" JCC "l", res);\
381 asm("cmpl %2, %1\n\t"\
382 "cmov" JCC "w %w3, %w0\n\t"\
384 : "r" (v1), "r" (v2), "r" (1), "0" (res));\
385 printf("%-10s R=" FMTLX "\n", "cmov" JCC "w", res);\
389 /* various jump tests */
392 TEST_JCC("ne", 1, 1);
393 TEST_JCC("ne", 1, 0);
400 TEST_JCC("l", 1, -1);
402 TEST_JCC("le", 1, 1);
403 TEST_JCC("le", 1, 0);
404 TEST_JCC("le", 1, -1);
406 TEST_JCC("ge", 1, 1);
407 TEST_JCC("ge", 1, 0);
408 TEST_JCC("ge", -1, 1);
412 TEST_JCC("g", 1, -1);
416 TEST_JCC("b", 1, -1);
418 TEST_JCC("be", 1, 1);
419 TEST_JCC("be", 1, 0);
420 TEST_JCC("be", 1, -1);
422 TEST_JCC("ae", 1, 1);
423 TEST_JCC("ae", 1, 0);
424 TEST_JCC("ae", 1, -1);
428 TEST_JCC("a", 1, -1);
434 TEST_JCC("np", 1, 1);
435 TEST_JCC("np", 1, 0);
437 TEST_JCC("o", 0x7fffffff, 0);
438 TEST_JCC("o", 0x7fffffff, -1);
440 TEST_JCC("no", 0x7fffffff, 0);
441 TEST_JCC("no", 0x7fffffff, -1);
444 TEST_JCC("s", 0, -1);
447 TEST_JCC("ns", 0, 1);
448 TEST_JCC("ns", 0, -1);
449 TEST_JCC("ns", 0, 0);
452 #define TEST_LOOP(insn) \
454 for(i = 0; i < sizeof(ecx_vals) / sizeof(long); i++) {\
456 for(zf = 0; zf < 2; zf++) {\
457 asm("test %2, %2\n\t"\
463 : "c" (ecx), "b" (!zf)); \
464 printf("%-10s ECX=" FMTLX " ZF=%ld r=%d\n", insn, ecx, zf, res); \
472 const long ecx_vals
[] = {
477 #if defined(__x86_64__)
484 #if !defined(__x86_64__)
488 TEST_LOOP("loopnzw");
494 TEST_LOOP("loopnzl");
499 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
501 #define CC_MASK (CC_O | CC_C)
505 #include "test-i386-muldiv.h"
508 #include "test-i386-muldiv.h"
510 void test_imulw2(long op0
, long op1
)
512 long res
, s1
, s0
, flags
;
517 asm volatile ("push %4\n\t"
522 : "=q" (res
), "=g" (flags
)
523 : "q" (s1
), "0" (res
), "1" (flags
));
524 printf("%-10s A=" FMTLX
" B=" FMTLX
" R=" FMTLX
" CC=%04lx\n",
525 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
528 void test_imull2(long op0
, long op1
)
530 long res
, s1
, s0
, flags
;
535 asm volatile ("push %4\n\t"
540 : "=q" (res
), "=g" (flags
)
541 : "q" (s1
), "0" (res
), "1" (flags
));
542 printf("%-10s A=" FMTLX
" B=" FMTLX
" R=" FMTLX
" CC=%04lx\n",
543 "imull", s0
, s1
, res
, flags
& CC_MASK
);
546 #if defined(__x86_64__)
547 void test_imulq2(long op0
, long op1
)
549 long res
, s1
, s0
, flags
;
554 asm volatile ("push %4\n\t"
559 : "=q" (res
), "=g" (flags
)
560 : "q" (s1
), "0" (res
), "1" (flags
));
561 printf("%-10s A=" FMTLX
" B=" FMTLX
" R=" FMTLX
" CC=%04lx\n",
562 "imulq", s0
, s1
, res
, flags
& CC_MASK
);
566 #define TEST_IMUL_IM(size, rsize, op0, op1)\
568 long res, flags, s1;\
572 asm volatile ("push %3\n\t"\
574 "imul" size " $" #op0 ", %" rsize "2, %" rsize "0\n\t" \
577 : "=r" (res), "=g" (flags)\
578 : "r" (s1), "1" (flags), "0" (res));\
579 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",\
580 "imul" size " im", (long)op0, (long)op1, res, flags & CC_MASK);\
588 #include "test-i386-muldiv.h"
591 #include "test-i386-muldiv.h"
595 test_imulb(0x1234561d, 4);
597 test_imulb(0x80, 0x80);
598 test_imulb(0x10, 0x10);
600 test_imulw(0, 0x1234001d, 45);
601 test_imulw(0, 23, -45);
602 test_imulw(0, 0x8000, 0x8000);
603 test_imulw(0, 0x100, 0x100);
605 test_imull(0, 0x1234001d, 45);
606 test_imull(0, 23, -45);
607 test_imull(0, 0x80000000, 0x80000000);
608 test_imull(0, 0x10000, 0x10000);
610 test_mulb(0x1234561d, 4);
612 test_mulb(0x80, 0x80);
613 test_mulb(0x10, 0x10);
615 test_mulw(0, 0x1234001d, 45);
616 test_mulw(0, 23, -45);
617 test_mulw(0, 0x8000, 0x8000);
618 test_mulw(0, 0x100, 0x100);
620 test_mull(0, 0x1234001d, 45);
621 test_mull(0, 23, -45);
622 test_mull(0, 0x80000000, 0x80000000);
623 test_mull(0, 0x10000, 0x10000);
625 test_imulw2(0x1234001d, 45);
626 test_imulw2(23, -45);
627 test_imulw2(0x8000, 0x8000);
628 test_imulw2(0x100, 0x100);
630 test_imull2(0x1234001d, 45);
631 test_imull2(23, -45);
632 test_imull2(0x80000000, 0x80000000);
633 test_imull2(0x10000, 0x10000);
635 TEST_IMUL_IM("w", "w", 45, 0x1234);
636 TEST_IMUL_IM("w", "w", -45, 23);
637 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
638 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
640 TEST_IMUL_IM("l", "k", 45, 0x1234);
641 TEST_IMUL_IM("l", "k", -45, 23);
642 TEST_IMUL_IM("l", "k", 0x8000, 0x80000000);
643 TEST_IMUL_IM("l", "k", 0x7fff, 0x1000);
645 test_idivb(0x12341678, 0x127e);
646 test_idivb(0x43210123, -5);
647 test_idivb(0x12340004, -1);
649 test_idivw(0, 0x12345678, 12347);
650 test_idivw(0, -23223, -45);
651 test_idivw(0, 0x12348000, -1);
652 test_idivw(0x12343, 0x12345678, 0x81238567);
654 test_idivl(0, 0x12345678, 12347);
655 test_idivl(0, -233223, -45);
656 test_idivl(0, 0x80000000, -1);
657 test_idivl(0x12343, 0x12345678, 0x81234567);
659 test_divb(0x12341678, 0x127e);
660 test_divb(0x43210123, -5);
661 test_divb(0x12340004, -1);
663 test_divw(0, 0x12345678, 12347);
664 test_divw(0, -23223, -45);
665 test_divw(0, 0x12348000, -1);
666 test_divw(0x12343, 0x12345678, 0x81238567);
668 test_divl(0, 0x12345678, 12347);
669 test_divl(0, -233223, -45);
670 test_divl(0, 0x80000000, -1);
671 test_divl(0x12343, 0x12345678, 0x81234567);
673 #if defined(__x86_64__)
674 test_imulq(0, 0x1234001d1234001d, 45);
675 test_imulq(0, 23, -45);
676 test_imulq(0, 0x8000000000000000, 0x8000000000000000);
677 test_imulq(0, 0x100000000, 0x100000000);
679 test_mulq(0, 0x1234001d1234001d, 45);
680 test_mulq(0, 23, -45);
681 test_mulq(0, 0x8000000000000000, 0x8000000000000000);
682 test_mulq(0, 0x100000000, 0x100000000);
684 test_imulq2(0x1234001d1234001d, 45);
685 test_imulq2(23, -45);
686 test_imulq2(0x8000000000000000, 0x8000000000000000);
687 test_imulq2(0x100000000, 0x100000000);
689 TEST_IMUL_IM("q", "", 45, 0x12341234);
690 TEST_IMUL_IM("q", "", -45, 23);
691 TEST_IMUL_IM("q", "", 0x8000, 0x8000000000000000);
692 TEST_IMUL_IM("q", "", 0x7fff, 0x10000000);
694 test_idivq(0, 0x12345678abcdef, 12347);
695 test_idivq(0, -233223, -45);
696 test_idivq(0, 0x8000000000000000, -1);
697 test_idivq(0x12343, 0x12345678, 0x81234567);
699 test_divq(0, 0x12345678abcdef, 12347);
700 test_divq(0, -233223, -45);
701 test_divq(0, 0x8000000000000000, -1);
702 test_divq(0x12343, 0x12345678, 0x81234567);
706 #define TEST_BSX(op, size, op0)\
708 long res, val, resz;\
711 "mov $0x12345678, %0\n"\
712 #op " %" size "2, %" size "0 ; setz %b1" \
713 : "=&r" (res), "=&q" (resz)\
715 printf("%-10s A=" FMTLX " R=" FMTLX " %ld\n", #op, val, res, resz);\
720 TEST_BSX(bsrw
, "w", 0);
721 TEST_BSX(bsrw
, "w", 0x12340128);
722 TEST_BSX(bsfw
, "w", 0);
723 TEST_BSX(bsfw
, "w", 0x12340128);
724 TEST_BSX(bsrl
, "k", 0);
725 TEST_BSX(bsrl
, "k", 0x00340128);
726 TEST_BSX(bsfl
, "k", 0);
727 TEST_BSX(bsfl
, "k", 0x00340128);
728 #if defined(__x86_64__)
729 TEST_BSX(bsrq
, "", 0);
730 TEST_BSX(bsrq
, "", 0x003401281234);
731 TEST_BSX(bsfq
, "", 0);
732 TEST_BSX(bsfq
, "", 0x003401281234);
736 /**********************************************/
743 union float64u q_nan
= { .l
= 0xFFF8000000000000LL
};
744 union float64u s_nan
= { .l
= 0xFFF0000000000000LL
};
746 void test_fops(double a
, double b
)
748 printf("a=%f b=%f a+b=%f\n", a
, b
, a
+ b
);
749 printf("a=%f b=%f a-b=%f\n", a
, b
, a
- b
);
750 printf("a=%f b=%f a*b=%f\n", a
, b
, a
* b
);
751 printf("a=%f b=%f a/b=%f\n", a
, b
, a
/ b
);
752 printf("a=%f b=%f fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
753 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
754 printf("a=%f sin(a)=%f\n", a
, sin(a
));
755 printf("a=%f cos(a)=%f\n", a
, cos(a
));
756 printf("a=%f tan(a)=%f\n", a
, tan(a
));
757 printf("a=%f log(a)=%f\n", a
, log(a
));
758 printf("a=%f exp(a)=%f\n", a
, exp(a
));
759 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
760 /* just to test some op combining */
761 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
762 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
763 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
767 void fpu_clear_exceptions(void)
777 long double fpregs
[8];
780 asm volatile ("fnstenv %0\n" : "=m" (float_env32
));
781 float_env32
.fpus
&= ~0x7f;
782 asm volatile ("fldenv %0\n" : : "m" (float_env32
));
785 /* XXX: display exception bits when supported */
786 #define FPUS_EMASK 0x0000
787 //#define FPUS_EMASK 0x007f
789 void test_fcmp(double a
, double b
)
793 fpu_clear_exceptions();
798 printf("fcom(%f %f)=%04lx\n",
799 a
, b
, fpus
& (0x4500 | FPUS_EMASK
));
800 fpu_clear_exceptions();
805 printf("fucom(%f %f)=%04lx\n",
806 a
, b
, fpus
& (0x4500 | FPUS_EMASK
));
808 /* test f(u)comi instruction */
809 fpu_clear_exceptions();
814 : "=r" (eflags
), "=a" (fpus
)
816 printf("fcomi(%f %f)=%04lx %02lx\n",
817 a
, b
, fpus
& FPUS_EMASK
, eflags
& (CC_Z
| CC_P
| CC_C
));
818 fpu_clear_exceptions();
819 asm("fucomi %3, %2\n"
823 : "=r" (eflags
), "=a" (fpus
)
825 printf("fucomi(%f %f)=%04lx %02lx\n",
826 a
, b
, fpus
& FPUS_EMASK
, eflags
& (CC_Z
| CC_P
| CC_C
));
828 fpu_clear_exceptions();
829 asm volatile("fxam\n"
833 printf("fxam(%f)=%04lx\n", a
, fpus
& 0x4700);
834 fpu_clear_exceptions();
837 void test_fcvt(double a
)
850 printf("(float)%f = %f\n", a
, fa
);
851 printf("(long double)%f = %Lf\n", a
, la
);
852 printf("a=" FMT64X
"\n", *(uint64_t *)&a
);
853 printf("la=" FMT64X
" %04x\n", *(uint64_t *)&la
,
854 *(unsigned short *)((char *)(&la
) + 8));
856 /* test all roundings */
857 asm volatile ("fstcw %0" : "=m" (fpuc
));
860 val16
= (fpuc
& ~0x0c00) | (i
<< 10);
861 asm volatile ("fldcw %0" : : "m" (val16
));
862 asm volatile ("fists %0" : "=m" (wa
) : "t" (a
));
863 asm volatile ("fistl %0" : "=m" (ia
) : "t" (a
));
864 asm volatile ("fistpll %0" : "=m" (lla
) : "t" (a
) : "st");
865 asm volatile ("frndint ; fstl %0" : "=m" (ra
) : "t" (a
));
866 asm volatile ("fldcw %0" : : "m" (fpuc
));
867 printf("(short)a = %d\n", wa
);
868 printf("(int)a = %d\n", ia
);
869 printf("(int64_t)a = " FMT64X
"\n", lla
);
870 printf("rint(a) = %f\n", ra
);
875 asm("fld" #N : "=t" (a)); \
876 printf("fld" #N "= %f\n", a);
878 void test_fconst(void)
890 void test_fbcd(double a
)
892 unsigned short bcd
[5];
895 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
896 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
897 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
898 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
901 #define TEST_ENV(env, save, restore)\
903 memset((env), 0xaa, sizeof(*(env)));\
905 asm volatile ("fldl %0" : : "m" (dtab[i]));\
906 asm volatile (save " %0\n" : : "m" (*(env)));\
907 asm volatile (restore " %0\n": : "m" (*(env)));\
909 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
911 printf("res[%d]=%f\n", i, rtab[i]);\
912 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
914 (env)->fpus & 0xff00,\
920 struct __attribute__((__packed__
)) {
928 long double fpregs
[8];
930 struct __attribute__((__packed__
)) {
935 long double fpregs
[8];
944 TEST_ENV(&float_env16
, "data16 fnstenv", "data16 fldenv");
945 TEST_ENV(&float_env16
, "data16 fnsave", "data16 frstor");
946 TEST_ENV(&float_env32
, "fnstenv", "fldenv");
947 TEST_ENV(&float_env32
, "fnsave", "frstor");
951 asm volatile ("fldl %0" : : "m" (dtab
[i
]));
952 asm volatile("ffree %st(2)");
953 asm volatile ("fnstenv %0\n" : : "m" (float_env32
));
954 asm volatile ("fninit");
955 printf("fptag=%04x\n", float_env32
.fptag
);
959 #define TEST_FCMOV(a, b, eflags, CC)\
964 "fcmov" CC " %2, %0\n"\
966 : "0" (a), "u" (b), "g" (eflags));\
967 printf("fcmov%s eflags=0x%04lx-> %f\n", \
968 CC, (long)eflags, res);\
971 void test_fcmov(void)
978 for(i
= 0; i
< 4; i
++) {
984 TEST_FCMOV(a
, b
, eflags
, "b");
985 TEST_FCMOV(a
, b
, eflags
, "e");
986 TEST_FCMOV(a
, b
, eflags
, "be");
987 TEST_FCMOV(a
, b
, eflags
, "nb");
988 TEST_FCMOV(a
, b
, eflags
, "ne");
989 TEST_FCMOV(a
, b
, eflags
, "nbe");
991 TEST_FCMOV(a
, b
, 0, "u");
992 TEST_FCMOV(a
, b
, CC_P
, "u");
993 TEST_FCMOV(a
, b
, 0, "nu");
994 TEST_FCMOV(a
, b
, CC_P
, "nu");
997 void test_floats(void)
1004 test_fcmp(2, q_nan
.d
);
1005 test_fcmp(q_nan
.d
, -1);
1006 test_fcmp(-1.0/0.0, -1);
1007 test_fcmp(1.0/0.0, -1);
1011 test_fcvt(-1.0/9.0);
1014 test_fcvt(-1.0/0.0);
1018 test_fbcd(1234567890123456.0);
1019 test_fbcd(-123451234567890.0);
1026 /**********************************************/
1027 #if !defined(__x86_64__)
1029 #define TEST_BCD(op, op0, cc_in, cc_mask)\
1039 : "=a" (res), "=g" (flags)\
1040 : "0" (res), "1" (flags));\
1041 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
1042 #op, op0, res, cc_in, flags & cc_mask);\
1047 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1048 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1049 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1050 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1051 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1052 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1053 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1054 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1055 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1056 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1057 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1058 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1059 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1061 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1062 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1063 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1064 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1065 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1066 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1067 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1068 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1069 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1070 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1071 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1072 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1073 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1075 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
1076 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
1077 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
1078 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
1079 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
1080 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
1081 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
1082 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
1084 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
1085 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
1086 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
1087 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
1088 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
1089 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
1090 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
1091 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
1093 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
1094 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
1098 #define TEST_XCHG(op, size, opconst)\
1101 op0 = i2l(0x12345678);\
1102 op1 = i2l(0xfbca7654);\
1103 asm(#op " %" size "0, %" size "1" \
1104 : "=q" (op0), opconst (op1) \
1106 printf("%-10s A=" FMTLX " B=" FMTLX "\n",\
1110 #define TEST_CMPXCHG(op, size, opconst, eax)\
1112 long op0, op1, op2;\
1113 op0 = i2l(0x12345678);\
1114 op1 = i2l(0xfbca7654);\
1116 asm(#op " %" size "0, %" size "1" \
1117 : "=q" (op0), opconst (op1) \
1118 : "0" (op0), "a" (op2));\
1119 printf("%-10s EAX=" FMTLX " A=" FMTLX " C=" FMTLX "\n",\
1120 #op, op2, op0, op1);\
1123 void test_xchg(void)
1125 #if defined(__x86_64__)
1126 TEST_XCHG(xchgq
, "", "+q");
1128 TEST_XCHG(xchgl
, "k", "+q");
1129 TEST_XCHG(xchgw
, "w", "+q");
1130 TEST_XCHG(xchgb
, "b", "+q");
1132 #if defined(__x86_64__)
1133 TEST_XCHG(xchgq
, "", "+m");
1135 TEST_XCHG(xchgl
, "k", "+m");
1136 TEST_XCHG(xchgw
, "w", "+m");
1137 TEST_XCHG(xchgb
, "b", "+m");
1139 #if defined(__x86_64__)
1140 TEST_XCHG(xaddq
, "", "+q");
1142 TEST_XCHG(xaddl
, "k", "+q");
1143 TEST_XCHG(xaddw
, "w", "+q");
1144 TEST_XCHG(xaddb
, "b", "+q");
1149 asm("xaddl %1, %0" : "=r" (res
) : "0" (res
));
1150 printf("xaddl same res=%08x\n", res
);
1153 #if defined(__x86_64__)
1154 TEST_XCHG(xaddq
, "", "+m");
1156 TEST_XCHG(xaddl
, "k", "+m");
1157 TEST_XCHG(xaddw
, "w", "+m");
1158 TEST_XCHG(xaddb
, "b", "+m");
1160 #if defined(__x86_64__)
1161 TEST_CMPXCHG(cmpxchgq
, "", "+q", 0xfbca7654);
1163 TEST_CMPXCHG(cmpxchgl
, "k", "+q", 0xfbca7654);
1164 TEST_CMPXCHG(cmpxchgw
, "w", "+q", 0xfbca7654);
1165 TEST_CMPXCHG(cmpxchgb
, "b", "+q", 0xfbca7654);
1167 #if defined(__x86_64__)
1168 TEST_CMPXCHG(cmpxchgq
, "", "+q", 0xfffefdfc);
1170 TEST_CMPXCHG(cmpxchgl
, "k", "+q", 0xfffefdfc);
1171 TEST_CMPXCHG(cmpxchgw
, "w", "+q", 0xfffefdfc);
1172 TEST_CMPXCHG(cmpxchgb
, "b", "+q", 0xfffefdfc);
1174 #if defined(__x86_64__)
1175 TEST_CMPXCHG(cmpxchgq
, "", "+m", 0xfbca7654);
1177 TEST_CMPXCHG(cmpxchgl
, "k", "+m", 0xfbca7654);
1178 TEST_CMPXCHG(cmpxchgw
, "w", "+m", 0xfbca7654);
1179 TEST_CMPXCHG(cmpxchgb
, "b", "+m", 0xfbca7654);
1181 #if defined(__x86_64__)
1182 TEST_CMPXCHG(cmpxchgq
, "", "+m", 0xfffefdfc);
1184 TEST_CMPXCHG(cmpxchgl
, "k", "+m", 0xfffefdfc);
1185 TEST_CMPXCHG(cmpxchgw
, "w", "+m", 0xfffefdfc);
1186 TEST_CMPXCHG(cmpxchgb
, "b", "+m", 0xfffefdfc);
1189 uint64_t op0
, op1
, op2
;
1193 for(i
= 0; i
< 2; i
++) {
1194 op0
= 0x123456789abcdLL
;
1195 eax
= i2l(op0
& 0xffffffff);
1196 edx
= i2l(op0
>> 32);
1198 op1
= 0xfbca765423456LL
;
1201 op2
= 0x6532432432434LL
;
1202 asm("cmpxchg8b %2\n"
1205 : "=a" (eax
), "=d" (edx
), "=m" (op1
), "=g" (eflags
)
1206 : "0" (eax
), "1" (edx
), "m" (op1
), "b" ((int)op2
), "c" ((int)(op2
>> 32)));
1207 printf("cmpxchg8b: eax=" FMTLX
" edx=" FMTLX
" op1=" FMT64X
" CC=%02lx\n",
1208 eax
, edx
, op1
, eflags
& CC_Z
);
1214 /**********************************************/
1215 /* segmentation tests */
1217 #include <sys/syscall.h>
1219 #include <asm/ldt.h>
1220 #include <linux/version.h>
1222 static inline int modify_ldt(int func
, void * ptr
, unsigned long bytecount
)
1224 return syscall(__NR_modify_ldt
, func
, ptr
, bytecount
);
1227 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
1228 #define modify_ldt_ldt_s user_desc
1231 #define MK_SEL(n) (((n) << 3) | 7)
1233 uint8_t seg_data1
[4096];
1234 uint8_t seg_data2
[4096];
1236 #define TEST_LR(op, size, seg, mask)\
1239 uint16_t mseg = seg;\
1241 asm (op " %" size "2, %" size "0\n" \
1246 : "=r" (res), "=r" (res2) : "m" (mseg), "0" (res));\
1247 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
1250 #define TEST_ARPL(op, size, op1, op2)\
1255 asm volatile(op " %" size "3, %" size "0\n"\
1260 : "=r" (a), "=r" (c) : "0" (a), "r" (b)); \
1261 printf(op size " A=" FMTLX " B=" FMTLX " R=" FMTLX " z=%ld\n",\
1262 (long)(op1), (long)(op2), a, c);\
1265 /* NOTE: we use Linux modify_ldt syscall */
1266 void test_segs(void)
1268 struct modify_ldt_ldt_s ldt
;
1269 long long ldt_table
[3];
1275 } __attribute__((__packed__
)) segoff
;
1277 ldt
.entry_number
= 1;
1278 ldt
.base_addr
= (unsigned long)&seg_data1
;
1279 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1281 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1282 ldt
.read_exec_only
= 0;
1283 ldt
.limit_in_pages
= 1;
1284 ldt
.seg_not_present
= 0;
1286 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1288 ldt
.entry_number
= 2;
1289 ldt
.base_addr
= (unsigned long)&seg_data2
;
1290 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
1292 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1293 ldt
.read_exec_only
= 0;
1294 ldt
.limit_in_pages
= 1;
1295 ldt
.seg_not_present
= 0;
1297 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1299 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
1304 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
1307 /* do some tests with fs or gs */
1308 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1310 seg_data1
[1] = 0xaa;
1311 seg_data2
[1] = 0x55;
1313 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
1314 printf("FS[1] = %02x\n", res
);
1316 asm volatile ("pushl %%gs\n"
1318 "gs movzbl 0x1, %0\n"
1322 printf("GS[1] = %02x\n", res
);
1324 /* tests with ds/ss (implicit segment case) */
1326 asm volatile ("pushl %%ebp\n\t"
1329 "movl %3, %%ebp\n\t"
1330 "movzbl 0x1, %0\n\t"
1331 "movzbl (%%ebp), %1\n\t"
1334 : "=r" (res
), "=r" (res2
)
1335 : "r" (MK_SEL(1)), "r" (&tmp
));
1336 printf("DS[1] = %02x\n", res
);
1337 printf("SS[tmp] = %02x\n", res2
);
1339 segoff
.seg
= MK_SEL(2);
1340 segoff
.offset
= 0xabcdef12;
1341 asm volatile("lfs %2, %0\n\t"
1343 : "=r" (res
), "=g" (res2
)
1345 printf("FS:reg = %04x:%08x\n", res2
, res
);
1347 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1348 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1349 TEST_LR("lslw", "w", MK_SEL(2), 0);
1350 TEST_LR("lsll", "", MK_SEL(2), 0);
1352 TEST_LR("larw", "w", 0xfff8, 0);
1353 TEST_LR("larl", "", 0xfff8, 0);
1354 TEST_LR("lslw", "w", 0xfff8, 0);
1355 TEST_LR("lsll", "", 0xfff8, 0);
1357 TEST_ARPL("arpl", "w", 0x12345678 | 3, 0x762123c | 1);
1358 TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 3);
1359 TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 1);
1362 /* 16 bit code test */
1363 extern char code16_start
, code16_end
;
1364 extern char code16_func1
;
1365 extern char code16_func2
;
1366 extern char code16_func3
;
1368 void test_code16(void)
1370 struct modify_ldt_ldt_s ldt
;
1373 /* build a code segment */
1374 ldt
.entry_number
= 1;
1375 ldt
.base_addr
= (unsigned long)&code16_start
;
1376 ldt
.limit
= &code16_end
- &code16_start
;
1378 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
1379 ldt
.read_exec_only
= 0;
1380 ldt
.limit_in_pages
= 0;
1381 ldt
.seg_not_present
= 0;
1383 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1385 /* call the first function */
1386 asm volatile ("lcall %1, %2"
1388 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
1389 printf("func1() = 0x%08x\n", res
);
1390 asm volatile ("lcall %2, %3"
1391 : "=a" (res
), "=c" (res2
)
1392 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
1393 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
1394 asm volatile ("lcall %1, %2"
1396 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
1397 printf("func3() = 0x%08x\n", res
);
1401 #if defined(__x86_64__)
1402 asm(".globl func_lret\n"
1404 "movl $0x87654641, %eax\n"
1407 asm(".globl func_lret\n"
1409 "movl $0x87654321, %eax\n"
1412 ".globl func_iret\n"
1414 "movl $0xabcd4321, %eax\n"
1418 extern char func_lret
;
1419 extern char func_iret
;
1421 void test_misc(void)
1426 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
1428 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
1429 printf("xlat: EAX=" FMTLX
"\n", res
);
1431 #if defined(__x86_64__)
1434 /* XXX: see if Intel Core2 and AMD64 behavior really
1435 differ. Here we implemented the Intel way which is not
1436 compatible yet with QEMU. */
1437 static struct QEMU_PACKED
{
1443 asm volatile ("mov %%cs, %0" : "=r" (cs_sel
));
1445 asm volatile ("push %1\n"
1448 : "r" (cs_sel
) : "memory", "cc");
1449 printf("func_lret=" FMTLX
"\n", res
);
1451 desc
.offset
= (long)&func_lret
;
1454 asm volatile ("xor %%rax, %%rax\n"
1455 "rex64 lcall *(%%rcx)\n"
1459 printf("func_lret2=" FMTLX
"\n", res
);
1461 asm volatile ("push %2\n"
1464 "rex64 ljmp *(%%rcx)\n"
1467 : "c" (&desc
), "b" (cs_sel
)
1469 printf("func_lret3=" FMTLX
"\n", res
);
1473 asm volatile ("push %%cs ; call %1"
1475 : "m" (func_lret
): "memory", "cc");
1476 printf("func_lret=" FMTLX
"\n", res
);
1478 asm volatile ("pushf ; push %%cs ; call %1"
1480 : "m" (func_iret
): "memory", "cc");
1481 printf("func_iret=" FMTLX
"\n", res
);
1484 #if defined(__x86_64__)
1485 /* specific popl test */
1486 asm volatile ("push $12345432 ; push $0x9abcdef ; pop (%%rsp) ; pop %0"
1488 printf("popl esp=" FMTLX
"\n", res
);
1490 /* specific popl test */
1491 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1493 printf("popl esp=" FMTLX
"\n", res
);
1495 /* specific popw test */
1496 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1498 printf("popw esp=" FMTLX
"\n", res
);
1502 uint8_t str_buffer
[4096];
1504 #define TEST_STRING1(OP, size, DF, REP)\
1506 long esi, edi, eax, ecx, eflags;\
1508 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1509 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1510 eax = i2l(0x12345678);\
1513 asm volatile ("push $0\n\t"\
1516 REP #OP size "\n\t"\
1520 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1521 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1522 printf("%-10s ESI=" FMTLX " EDI=" FMTLX " EAX=" FMTLX " ECX=" FMTLX " EFL=%04x\n",\
1523 REP #OP size, esi, edi, eax, ecx,\
1524 (int)(eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)));\
1527 #define TEST_STRING(OP, REP)\
1528 TEST_STRING1(OP, "b", "", REP);\
1529 TEST_STRING1(OP, "w", "", REP);\
1530 TEST_STRING1(OP, "l", "", REP);\
1531 X86_64_ONLY(TEST_STRING1(OP, "q", "", REP));\
1532 TEST_STRING1(OP, "b", "std", REP);\
1533 TEST_STRING1(OP, "w", "std", REP);\
1534 TEST_STRING1(OP, "l", "std", REP);\
1535 X86_64_ONLY(TEST_STRING1(OP, "q", "std", REP))
1537 void test_string(void)
1540 for(i
= 0;i
< sizeof(str_buffer
); i
++)
1541 str_buffer
[i
] = i
+ 0x56;
1542 TEST_STRING(stos
, "");
1543 TEST_STRING(stos
, "rep ");
1544 TEST_STRING(lods
, ""); /* to verify stos */
1545 TEST_STRING(lods
, "rep ");
1546 TEST_STRING(movs
, "");
1547 TEST_STRING(movs
, "rep ");
1548 TEST_STRING(lods
, ""); /* to verify stos */
1550 /* XXX: better tests */
1551 TEST_STRING(scas
, "");
1552 TEST_STRING(scas
, "repz ");
1553 TEST_STRING(scas
, "repnz ");
1554 TEST_STRING(cmps
, "");
1555 TEST_STRING(cmps
, "repz ");
1556 TEST_STRING(cmps
, "repnz ");
1562 static inline void set_bit(uint8_t *a
, unsigned int bit
)
1564 a
[bit
/ 8] |= (1 << (bit
% 8));
1567 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
1569 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
1572 static inline void pushw(struct vm86_regs
*r
, int val
)
1574 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
1575 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
1578 static inline int vm86(int func
, struct vm86plus_struct
*v86
)
1580 return syscall(__NR_vm86
, func
, v86
);
1583 extern char vm86_code_start
;
1584 extern char vm86_code_end
;
1586 #define VM86_CODE_CS 0x100
1587 #define VM86_CODE_IP 0x100
1589 void test_vm86(void)
1591 struct vm86plus_struct ctx
;
1592 struct vm86_regs
*r
;
1596 vm86_mem
= mmap((void *)0x00000000, 0x110000,
1597 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
1598 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
1599 if (vm86_mem
== MAP_FAILED
) {
1600 printf("ERROR: could not map vm86 memory");
1603 memset(&ctx
, 0, sizeof(ctx
));
1605 /* init basic registers */
1607 r
->eip
= VM86_CODE_IP
;
1616 r
->eflags
= VIF_MASK
;
1618 /* move code to proper address. We use the same layout as a .com
1620 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
1621 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
1623 /* mark int 0x21 as being emulated */
1624 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
1627 ret
= vm86(VM86_ENTER
, &ctx
);
1628 switch(VM86_TYPE(ret
)) {
1633 int_num
= VM86_ARG(ret
);
1634 if (int_num
!= 0x21)
1636 ah
= (r
->eax
>> 8) & 0xff;
1638 case 0x00: /* exit */
1640 case 0x02: /* write char */
1646 case 0x09: /* write string */
1649 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1656 r
->eax
= (r
->eax
& ~0xff) | '$';
1659 case 0xff: /* extension: write eflags number in edx */
1661 #ifndef LINUX_VM86_IOPL_FIX
1664 printf("%08x\n", v
);
1668 printf("unsupported int 0x%02x\n", int_num
);
1674 /* a signal came, we just ignore that */
1679 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1684 printf("VM86 end\n");
1685 munmap(vm86_mem
, 0x110000);
1689 /* exception tests */
1690 #if defined(__i386__) && !defined(REG_EAX)
1701 #define REG_TRAPNO TRAPNO
1705 #if defined(__x86_64__)
1706 #define REG_EIP REG_RIP
1713 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1715 ucontext_t
*uc
= puc
;
1717 printf("si_signo=%d si_errno=%d si_code=%d",
1718 info
->si_signo
, info
->si_errno
, info
->si_code
);
1719 printf(" si_addr=0x%08lx",
1720 (unsigned long)info
->si_addr
);
1723 printf("trapno=" FMTLX
" err=" FMTLX
,
1724 (long)uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1725 (long)uc
->uc_mcontext
.gregs
[REG_ERR
]);
1726 printf(" EIP=" FMTLX
, (long)uc
->uc_mcontext
.gregs
[REG_EIP
]);
1728 longjmp(jmp_env
, 1);
1731 void test_exceptions(void)
1733 struct sigaction act
;
1736 act
.sa_sigaction
= sig_handler
;
1737 sigemptyset(&act
.sa_mask
);
1738 act
.sa_flags
= SA_SIGINFO
| SA_NODEFER
;
1739 sigaction(SIGFPE
, &act
, NULL
);
1740 sigaction(SIGILL
, &act
, NULL
);
1741 sigaction(SIGSEGV
, &act
, NULL
);
1742 sigaction(SIGBUS
, &act
, NULL
);
1743 sigaction(SIGTRAP
, &act
, NULL
);
1745 /* test division by zero reporting */
1746 printf("DIVZ exception:\n");
1747 if (setjmp(jmp_env
) == 0) {
1748 /* now divide by zero */
1753 #if !defined(__x86_64__)
1754 printf("BOUND exception:\n");
1755 if (setjmp(jmp_env
) == 0) {
1756 /* bound exception */
1759 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
[0]));
1764 printf("segment exceptions:\n");
1765 if (setjmp(jmp_env
) == 0) {
1766 /* load an invalid segment */
1767 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1769 if (setjmp(jmp_env
) == 0) {
1770 /* null data segment is valid */
1771 asm volatile ("movl %0, %%fs" : : "r" (3));
1772 /* null stack segment */
1773 asm volatile ("movl %0, %%ss" : : "r" (3));
1777 struct modify_ldt_ldt_s ldt
;
1778 ldt
.entry_number
= 1;
1779 ldt
.base_addr
= (unsigned long)&seg_data1
;
1780 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1782 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1783 ldt
.read_exec_only
= 0;
1784 ldt
.limit_in_pages
= 1;
1785 ldt
.seg_not_present
= 1;
1787 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1789 if (setjmp(jmp_env
) == 0) {
1790 /* segment not present */
1791 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1796 /* test SEGV reporting */
1797 printf("PF exception:\n");
1798 if (setjmp(jmp_env
) == 0) {
1800 /* we add a nop to test a weird PC retrieval case */
1801 asm volatile ("nop");
1802 /* now store in an invalid address */
1803 *(char *)0x1234 = 1;
1806 /* test SEGV reporting */
1807 printf("PF exception:\n");
1808 if (setjmp(jmp_env
) == 0) {
1810 /* read from an invalid address */
1811 v1
= *(char *)0x1234;
1814 /* test illegal instruction reporting */
1815 printf("UD2 exception:\n");
1816 if (setjmp(jmp_env
) == 0) {
1817 /* now execute an invalid instruction */
1818 asm volatile("ud2");
1820 printf("lock nop exception:\n");
1821 if (setjmp(jmp_env
) == 0) {
1822 /* now execute an invalid instruction */
1823 asm volatile(".byte 0xf0, 0x90");
1826 printf("INT exception:\n");
1827 if (setjmp(jmp_env
) == 0) {
1828 asm volatile ("int $0xfd");
1830 if (setjmp(jmp_env
) == 0) {
1831 asm volatile ("int $0x01");
1833 if (setjmp(jmp_env
) == 0) {
1834 asm volatile (".byte 0xcd, 0x03");
1836 if (setjmp(jmp_env
) == 0) {
1837 asm volatile ("int $0x04");
1839 if (setjmp(jmp_env
) == 0) {
1840 asm volatile ("int $0x05");
1843 printf("INT3 exception:\n");
1844 if (setjmp(jmp_env
) == 0) {
1845 asm volatile ("int3");
1848 printf("CLI exception:\n");
1849 if (setjmp(jmp_env
) == 0) {
1850 asm volatile ("cli");
1853 printf("STI exception:\n");
1854 if (setjmp(jmp_env
) == 0) {
1855 asm volatile ("cli");
1858 #if !defined(__x86_64__)
1859 printf("INTO exception:\n");
1860 if (setjmp(jmp_env
) == 0) {
1861 /* overflow exception */
1862 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1866 printf("OUTB exception:\n");
1867 if (setjmp(jmp_env
) == 0) {
1868 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1871 printf("INB exception:\n");
1872 if (setjmp(jmp_env
) == 0) {
1873 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1876 printf("REP OUTSB exception:\n");
1877 if (setjmp(jmp_env
) == 0) {
1878 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1881 printf("REP INSB exception:\n");
1882 if (setjmp(jmp_env
) == 0) {
1883 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1886 printf("HLT exception:\n");
1887 if (setjmp(jmp_env
) == 0) {
1888 asm volatile ("hlt");
1891 printf("single step exception:\n");
1893 if (setjmp(jmp_env
) == 0) {
1894 asm volatile ("pushf\n"
1895 "orl $0x00100, (%%esp)\n"
1897 "movl $0xabcd, %0\n"
1898 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1900 printf("val=0x%x\n", val
);
1903 #if !defined(__x86_64__)
1904 /* specific precise single step test */
1905 void sig_trap_handler(int sig
, siginfo_t
*info
, void *puc
)
1907 ucontext_t
*uc
= puc
;
1908 printf("EIP=" FMTLX
"\n", (long)uc
->uc_mcontext
.gregs
[REG_EIP
]);
1911 const uint8_t sstep_buf1
[4] = { 1, 2, 3, 4};
1912 uint8_t sstep_buf2
[4];
1914 void test_single_step(void)
1916 struct sigaction act
;
1921 act
.sa_sigaction
= sig_trap_handler
;
1922 sigemptyset(&act
.sa_mask
);
1923 act
.sa_flags
= SA_SIGINFO
;
1924 sigaction(SIGTRAP
, &act
, NULL
);
1925 asm volatile ("pushf\n"
1926 "orl $0x00100, (%%esp)\n"
1928 "movl $0xabcd, %0\n"
1937 /* movsb: the single step should stop at each movsb iteration */
1938 "movl $sstep_buf1, %%esi\n"
1939 "movl $sstep_buf2, %%edi\n"
1947 /* cmpsb: the single step should stop at each cmpsb iteration */
1948 "movl $sstep_buf1, %%esi\n"
1949 "movl $sstep_buf2, %%edi\n"
1955 /* getpid() syscall: single step should skip one
1961 /* when modifying SS, trace is not done on the next
1963 "movl %%ss, %%ecx\n"
1964 "movl %%ecx, %%ss\n"
1967 "movl %%ecx, %%ss\n"
1978 "andl $~0x00100, (%%esp)\n"
1982 : "cc", "memory", "eax", "ecx", "esi", "edi");
1983 printf("val=%d\n", val
);
1984 for(i
= 0; i
< 4; i
++)
1985 printf("sstep_buf2[%d] = %d\n", i
, sstep_buf2
[i
]);
1988 /* self modifying code test */
1990 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1994 asm(".section \".data_x\",\"awx\"\n"
1996 "movl 4(%esp), %eax\n"
1997 "movl %eax, smc_patch_addr2 + 1\n"
2006 "smc_patch_addr2:\n"
2012 typedef int FuncType(void);
2013 extern int smc_code2(int);
2014 void test_self_modifying_code(void)
2017 printf("self modifying code:\n");
2018 printf("func1 = 0x%x\n", ((FuncType
*)code
)());
2019 for(i
= 2; i
<= 4; i
++) {
2021 printf("func%d = 0x%x\n", i
, ((FuncType
*)code
)());
2024 /* more difficult test : the modified code is just after the
2025 modifying instruction. It is forbidden in Intel specs, but it
2026 is used by old DOS programs */
2027 for(i
= 2; i
<= 4; i
++) {
2028 printf("smc_code2(%d) = %d\n", i
, smc_code2(i
));
2033 long enter_stack
[4096];
2035 #if defined(__x86_64__)
2043 #if !defined(__x86_64__)
2044 /* causes an infinite loop, disable it for now. */
2045 #define TEST_ENTER(size, stack_type, level)
2047 #define TEST_ENTER(size, stack_type, level)\
2049 long esp_save, esp_val, ebp_val, ebp_save, i;\
2050 stack_type *ptr, *stack_end, *stack_ptr;\
2051 memset(enter_stack, 0, sizeof(enter_stack));\
2052 stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
2053 ebp_val = (long)stack_ptr;\
2056 esp_val = (long)stack_ptr;\
2057 asm("mov " RSP ", %[esp_save]\n"\
2058 "mov " RBP ", %[ebp_save]\n"\
2059 "mov %[esp_val], " RSP "\n"\
2060 "mov %[ebp_val], " RBP "\n"\
2061 "enter" size " $8, $" #level "\n"\
2062 "mov " RSP ", %[esp_val]\n"\
2063 "mov " RBP ", %[ebp_val]\n"\
2064 "mov %[esp_save], " RSP "\n"\
2065 "mov %[ebp_save], " RBP "\n"\
2066 : [esp_save] "=r" (esp_save),\
2067 [ebp_save] "=r" (ebp_save),\
2068 [esp_val] "=r" (esp_val),\
2069 [ebp_val] "=r" (ebp_val)\
2070 : "[esp_val]" (esp_val),\
2071 "[ebp_val]" (ebp_val));\
2072 printf("level=%d:\n", level);\
2073 printf("esp_val=" FMTLX "\n", esp_val - (long)stack_end);\
2074 printf("ebp_val=" FMTLX "\n", ebp_val - (long)stack_end);\
2075 for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
2076 printf(FMTLX "\n", (long)ptr[0]);\
2080 static void test_enter(void)
2082 #if defined(__x86_64__)
2083 TEST_ENTER("q", uint64_t, 0);
2084 TEST_ENTER("q", uint64_t, 1);
2085 TEST_ENTER("q", uint64_t, 2);
2086 TEST_ENTER("q", uint64_t, 31);
2088 TEST_ENTER("l", uint32_t, 0);
2089 TEST_ENTER("l", uint32_t, 1);
2090 TEST_ENTER("l", uint32_t, 2);
2091 TEST_ENTER("l", uint32_t, 31);
2094 TEST_ENTER("w", uint16_t, 0);
2095 TEST_ENTER("w", uint16_t, 1);
2096 TEST_ENTER("w", uint16_t, 2);
2097 TEST_ENTER("w", uint16_t, 31);
2100 #define TEST_CONV_RAX(op)\
2102 unsigned long a, r;\
2103 a = i2l(0x8234a6f8);\
2105 asm volatile(#op : "=a" (r) : "0" (r));\
2106 printf("%-10s A=" FMTLX " R=" FMTLX "\n", #op, a, r);\
2109 #define TEST_CONV_RAX_RDX(op)\
2111 unsigned long a, d, r, rh; \
2112 a = i2l(0x8234a6f8);\
2113 d = i2l(0x8345a1f2);\
2116 asm volatile(#op : "=a" (r), "=d" (rh) : "0" (r), "1" (rh)); \
2117 printf("%-10s A=" FMTLX " R=" FMTLX ":" FMTLX "\n", #op, a, r, rh); \
2120 void test_conv(void)
2123 TEST_CONV_RAX(cwde
);
2124 #if defined(__x86_64__)
2125 TEST_CONV_RAX(cdqe
);
2128 TEST_CONV_RAX_RDX(cwd
);
2129 TEST_CONV_RAX_RDX(cdq
);
2130 #if defined(__x86_64__)
2131 TEST_CONV_RAX_RDX(cqo
);
2136 a
= i2l(0x12345678);
2137 asm volatile("bswapl %k0" : "=r" (r
) : "0" (a
));
2138 printf("%-10s: A=" FMTLX
" R=" FMTLX
"\n", "bswapl", a
, r
);
2140 #if defined(__x86_64__)
2143 a
= i2l(0x12345678);
2144 asm volatile("bswapq %0" : "=r" (r
) : "0" (a
));
2145 printf("%-10s: A=" FMTLX
" R=" FMTLX
"\n", "bswapq", a
, r
);
2150 extern void *__start_initcall
;
2151 extern void *__stop_initcall
;
2154 int main(int argc
, char **argv
)
2159 ptr
= &__start_initcall
;
2160 while (ptr
!= &__stop_initcall
) {
2169 #if !defined(__x86_64__)
2183 #if !defined(__x86_64__)
2185 test_self_modifying_code();