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/>.
20 #include "qemu/compiler.h"
29 #include <sys/ucontext.h>
32 #if !defined(__x86_64__)
36 //#define LINUX_VM86_IOPL_FIX
37 //#define TEST_P4_FLAGS
48 #if defined(__x86_64__)
49 #define FMT64X "%016lx"
50 #define FMTLX "%016lx"
51 #define X86_64_ONLY(x) x
53 #define FMT64X "%016" PRIx64
55 #define X86_64_ONLY(x)
62 #define xglue(x, y) x ## y
63 #define glue(x, y) xglue(x, y)
64 #define stringify(s) tostring(s)
65 #define tostring(s) #s
74 #define __init_call __attribute__ ((unused,__section__ ("initcall")))
76 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
78 #if defined(__x86_64__)
79 static inline long i2l(long v
)
81 return v
| ((v
^ 0xabcd) << 32);
84 static inline long i2l(long v
)
91 #include "test-i386.h"
94 #include "test-i386.h"
97 #include "test-i386.h"
100 #include "test-i386.h"
103 #include "test-i386.h"
106 #include "test-i386.h"
110 #include "test-i386.h"
114 #include "test-i386.h"
119 #include "test-i386.h"
124 #include "test-i386.h"
129 #include "test-i386.h"
134 #include "test-i386.h"
137 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
140 #include "test-i386-shift.h"
143 #include "test-i386-shift.h"
146 #include "test-i386-shift.h"
149 #include "test-i386-shift.h"
152 #include "test-i386-shift.h"
156 #include "test-i386-shift.h"
160 #include "test-i386-shift.h"
165 #include "test-i386-shift.h"
170 #include "test-i386-shift.h"
172 /* XXX: should be more precise ? */
174 #define CC_MASK (CC_C)
178 #include "test-i386-shift.h"
182 #include "test-i386-shift.h"
186 #include "test-i386-shift.h"
190 #include "test-i386-shift.h"
192 /* lea test (modrm support) */
193 #define TEST_LEAQ(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_LEA(STR)\
203 asm("lea " STR ", %0"\
205 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
206 printf("lea %s = " FMTLX "\n", STR, res);\
209 #define TEST_LEA16(STR)\
211 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
213 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
214 printf("lea %s = %08lx\n", STR, res);\
220 long eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
237 TEST_LEA("0x40(%%eax)");
238 TEST_LEA("0x40(%%ebx)");
239 TEST_LEA("0x40(%%ecx)");
240 TEST_LEA("0x40(%%edx)");
241 TEST_LEA("0x40(%%esi)");
242 TEST_LEA("0x40(%%edi)");
244 TEST_LEA("0x4000(%%eax)");
245 TEST_LEA("0x4000(%%ebx)");
246 TEST_LEA("0x4000(%%ecx)");
247 TEST_LEA("0x4000(%%edx)");
248 TEST_LEA("0x4000(%%esi)");
249 TEST_LEA("0x4000(%%edi)");
251 TEST_LEA("(%%eax, %%ecx)");
252 TEST_LEA("(%%ebx, %%edx)");
253 TEST_LEA("(%%ecx, %%ecx)");
254 TEST_LEA("(%%edx, %%ecx)");
255 TEST_LEA("(%%esi, %%ecx)");
256 TEST_LEA("(%%edi, %%ecx)");
258 TEST_LEA("0x40(%%eax, %%ecx)");
259 TEST_LEA("0x4000(%%ebx, %%edx)");
261 TEST_LEA("(%%ecx, %%ecx, 2)");
262 TEST_LEA("(%%edx, %%ecx, 4)");
263 TEST_LEA("(%%esi, %%ecx, 8)");
265 TEST_LEA("(,%%eax, 2)");
266 TEST_LEA("(,%%ebx, 4)");
267 TEST_LEA("(,%%ecx, 8)");
269 TEST_LEA("0x40(,%%eax, 2)");
270 TEST_LEA("0x40(,%%ebx, 4)");
271 TEST_LEA("0x40(,%%ecx, 8)");
274 TEST_LEA("-10(%%ecx, %%ecx, 2)");
275 TEST_LEA("-10(%%edx, %%ecx, 4)");
276 TEST_LEA("-10(%%esi, %%ecx, 8)");
278 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
279 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
280 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
282 #if defined(__x86_64__)
284 TEST_LEAQ("0x4000(%%rip)");
286 TEST_LEAQ("(%%rax)");
287 TEST_LEAQ("(%%rbx)");
288 TEST_LEAQ("(%%rcx)");
289 TEST_LEAQ("(%%rdx)");
290 TEST_LEAQ("(%%rsi)");
291 TEST_LEAQ("(%%rdi)");
293 TEST_LEAQ("0x40(%%rax)");
294 TEST_LEAQ("0x40(%%rbx)");
295 TEST_LEAQ("0x40(%%rcx)");
296 TEST_LEAQ("0x40(%%rdx)");
297 TEST_LEAQ("0x40(%%rsi)");
298 TEST_LEAQ("0x40(%%rdi)");
300 TEST_LEAQ("0x4000(%%rax)");
301 TEST_LEAQ("0x4000(%%rbx)");
302 TEST_LEAQ("0x4000(%%rcx)");
303 TEST_LEAQ("0x4000(%%rdx)");
304 TEST_LEAQ("0x4000(%%rsi)");
305 TEST_LEAQ("0x4000(%%rdi)");
307 TEST_LEAQ("(%%rax, %%rcx)");
308 TEST_LEAQ("(%%rbx, %%rdx)");
309 TEST_LEAQ("(%%rcx, %%rcx)");
310 TEST_LEAQ("(%%rdx, %%rcx)");
311 TEST_LEAQ("(%%rsi, %%rcx)");
312 TEST_LEAQ("(%%rdi, %%rcx)");
314 TEST_LEAQ("0x40(%%rax, %%rcx)");
315 TEST_LEAQ("0x4000(%%rbx, %%rdx)");
317 TEST_LEAQ("(%%rcx, %%rcx, 2)");
318 TEST_LEAQ("(%%rdx, %%rcx, 4)");
319 TEST_LEAQ("(%%rsi, %%rcx, 8)");
321 TEST_LEAQ("(,%%rax, 2)");
322 TEST_LEAQ("(,%%rbx, 4)");
323 TEST_LEAQ("(,%%rcx, 8)");
325 TEST_LEAQ("0x40(,%%rax, 2)");
326 TEST_LEAQ("0x40(,%%rbx, 4)");
327 TEST_LEAQ("0x40(,%%rcx, 8)");
330 TEST_LEAQ("-10(%%rcx, %%rcx, 2)");
331 TEST_LEAQ("-10(%%rdx, %%rcx, 4)");
332 TEST_LEAQ("-10(%%rsi, %%rcx, 8)");
334 TEST_LEAQ("0x4000(%%rcx, %%rcx, 2)");
335 TEST_LEAQ("0x4000(%%rdx, %%rcx, 4)");
336 TEST_LEAQ("0x4000(%%rsi, %%rcx, 8)");
338 /* limited 16 bit addressing test */
339 TEST_LEA16("0x4000");
340 TEST_LEA16("(%%bx)");
341 TEST_LEA16("(%%si)");
342 TEST_LEA16("(%%di)");
343 TEST_LEA16("0x40(%%bx)");
344 TEST_LEA16("0x40(%%si)");
345 TEST_LEA16("0x40(%%di)");
346 TEST_LEA16("0x4000(%%bx)");
347 TEST_LEA16("0x4000(%%si)");
348 TEST_LEA16("(%%bx,%%si)");
349 TEST_LEA16("(%%bx,%%di)");
350 TEST_LEA16("0x40(%%bx,%%si)");
351 TEST_LEA16("0x40(%%bx,%%di)");
352 TEST_LEA16("0x4000(%%bx,%%si)");
353 TEST_LEA16("0x4000(%%bx,%%di)");
357 #define TEST_JCC(JCC, v1, v2)\
360 asm("movl $1, %0\n\t"\
366 : "r" (v1), "r" (v2));\
367 printf("%-10s %d\n", "j" JCC, res);\
369 asm("movl $0, %0\n\t"\
371 "set" JCC " %b0\n\t"\
373 : "r" (v1), "r" (v2));\
374 printf("%-10s %d\n", "set" JCC, res);\
377 long res = i2l(0x12345678);\
379 asm("cmpl %2, %1\n\t"\
380 "cmov" JCC "q %3, %0\n\t"\
382 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
383 printf("%-10s R=" FMTLX "\n", "cmov" JCC "q", res);)\
384 asm("cmpl %2, %1\n\t"\
385 "cmov" JCC "l %k3, %k0\n\t"\
387 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
388 printf("%-10s R=" FMTLX "\n", "cmov" JCC "l", res);\
389 asm("cmpl %2, %1\n\t"\
390 "cmov" JCC "w %w3, %w0\n\t"\
392 : "r" (v1), "r" (v2), "r" (1), "0" (res));\
393 printf("%-10s R=" FMTLX "\n", "cmov" JCC "w", res);\
397 /* various jump tests */
400 TEST_JCC("ne", 1, 1);
401 TEST_JCC("ne", 1, 0);
408 TEST_JCC("l", 1, -1);
410 TEST_JCC("le", 1, 1);
411 TEST_JCC("le", 1, 0);
412 TEST_JCC("le", 1, -1);
414 TEST_JCC("ge", 1, 1);
415 TEST_JCC("ge", 1, 0);
416 TEST_JCC("ge", -1, 1);
420 TEST_JCC("g", 1, -1);
424 TEST_JCC("b", 1, -1);
426 TEST_JCC("be", 1, 1);
427 TEST_JCC("be", 1, 0);
428 TEST_JCC("be", 1, -1);
430 TEST_JCC("ae", 1, 1);
431 TEST_JCC("ae", 1, 0);
432 TEST_JCC("ae", 1, -1);
436 TEST_JCC("a", 1, -1);
442 TEST_JCC("np", 1, 1);
443 TEST_JCC("np", 1, 0);
445 TEST_JCC("o", 0x7fffffff, 0);
446 TEST_JCC("o", 0x7fffffff, -1);
448 TEST_JCC("no", 0x7fffffff, 0);
449 TEST_JCC("no", 0x7fffffff, -1);
452 TEST_JCC("s", 0, -1);
455 TEST_JCC("ns", 0, 1);
456 TEST_JCC("ns", 0, -1);
457 TEST_JCC("ns", 0, 0);
460 #define TEST_LOOP(insn) \
462 for(i = 0; i < sizeof(ecx_vals) / sizeof(long); i++) {\
464 for(zf = 0; zf < 2; zf++) {\
465 asm("test %2, %2\n\t"\
471 : "c" (ecx), "b" (!zf)); \
472 printf("%-10s ECX=" FMTLX " ZF=%ld r=%d\n", insn, ecx, zf, res); \
480 const long ecx_vals
[] = {
485 #if defined(__x86_64__)
492 #if !defined(__x86_64__)
496 TEST_LOOP("loopnzw");
502 TEST_LOOP("loopnzl");
507 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
509 #define CC_MASK (CC_O | CC_C)
513 #include "test-i386-muldiv.h"
516 #include "test-i386-muldiv.h"
518 void test_imulw2(long op0
, long op1
)
520 long res
, s1
, s0
, flags
;
525 asm volatile ("push %4\n\t"
530 : "=q" (res
), "=g" (flags
)
531 : "q" (s1
), "0" (res
), "1" (flags
));
532 printf("%-10s A=" FMTLX
" B=" FMTLX
" R=" FMTLX
" CC=%04lx\n",
533 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
536 void test_imull2(long op0
, long op1
)
538 long res
, s1
, s0
, flags
;
543 asm volatile ("push %4\n\t"
548 : "=q" (res
), "=g" (flags
)
549 : "q" (s1
), "0" (res
), "1" (flags
));
550 printf("%-10s A=" FMTLX
" B=" FMTLX
" R=" FMTLX
" CC=%04lx\n",
551 "imull", s0
, s1
, res
, flags
& CC_MASK
);
554 #if defined(__x86_64__)
555 void test_imulq2(long op0
, long op1
)
557 long res
, s1
, s0
, flags
;
562 asm volatile ("push %4\n\t"
567 : "=q" (res
), "=g" (flags
)
568 : "q" (s1
), "0" (res
), "1" (flags
));
569 printf("%-10s A=" FMTLX
" B=" FMTLX
" R=" FMTLX
" CC=%04lx\n",
570 "imulq", s0
, s1
, res
, flags
& CC_MASK
);
574 #define TEST_IMUL_IM(size, rsize, op0, op1)\
576 long res, flags, s1;\
580 asm volatile ("push %3\n\t"\
582 "imul" size " $" #op0 ", %" rsize "2, %" rsize "0\n\t" \
585 : "=r" (res), "=g" (flags)\
586 : "r" (s1), "1" (flags), "0" (res));\
587 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",\
588 "imul" size " im", (long)op0, (long)op1, res, flags & CC_MASK);\
596 #include "test-i386-muldiv.h"
599 #include "test-i386-muldiv.h"
603 test_imulb(0x1234561d, 4);
605 test_imulb(0x80, 0x80);
606 test_imulb(0x10, 0x10);
608 test_imulw(0, 0x1234001d, 45);
609 test_imulw(0, 23, -45);
610 test_imulw(0, 0x8000, 0x8000);
611 test_imulw(0, 0x100, 0x100);
613 test_imull(0, 0x1234001d, 45);
614 test_imull(0, 23, -45);
615 test_imull(0, 0x80000000, 0x80000000);
616 test_imull(0, 0x10000, 0x10000);
618 test_mulb(0x1234561d, 4);
620 test_mulb(0x80, 0x80);
621 test_mulb(0x10, 0x10);
623 test_mulw(0, 0x1234001d, 45);
624 test_mulw(0, 23, -45);
625 test_mulw(0, 0x8000, 0x8000);
626 test_mulw(0, 0x100, 0x100);
628 test_mull(0, 0x1234001d, 45);
629 test_mull(0, 23, -45);
630 test_mull(0, 0x80000000, 0x80000000);
631 test_mull(0, 0x10000, 0x10000);
633 test_imulw2(0x1234001d, 45);
634 test_imulw2(23, -45);
635 test_imulw2(0x8000, 0x8000);
636 test_imulw2(0x100, 0x100);
638 test_imull2(0x1234001d, 45);
639 test_imull2(23, -45);
640 test_imull2(0x80000000, 0x80000000);
641 test_imull2(0x10000, 0x10000);
643 TEST_IMUL_IM("w", "w", 45, 0x1234);
644 TEST_IMUL_IM("w", "w", -45, 23);
645 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
646 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
648 TEST_IMUL_IM("l", "k", 45, 0x1234);
649 TEST_IMUL_IM("l", "k", -45, 23);
650 TEST_IMUL_IM("l", "k", 0x8000, 0x80000000);
651 TEST_IMUL_IM("l", "k", 0x7fff, 0x1000);
653 test_idivb(0x12341678, 0x127e);
654 test_idivb(0x43210123, -5);
655 test_idivb(0x12340004, -1);
657 test_idivw(0, 0x12345678, 12347);
658 test_idivw(0, -23223, -45);
659 test_idivw(0, 0x12348000, -1);
660 test_idivw(0x12343, 0x12345678, 0x81238567);
662 test_idivl(0, 0x12345678, 12347);
663 test_idivl(0, -233223, -45);
664 test_idivl(0, 0x80000000, -1);
665 test_idivl(0x12343, 0x12345678, 0x81234567);
667 test_divb(0x12341678, 0x127e);
668 test_divb(0x43210123, -5);
669 test_divb(0x12340004, -1);
671 test_divw(0, 0x12345678, 12347);
672 test_divw(0, -23223, -45);
673 test_divw(0, 0x12348000, -1);
674 test_divw(0x12343, 0x12345678, 0x81238567);
676 test_divl(0, 0x12345678, 12347);
677 test_divl(0, -233223, -45);
678 test_divl(0, 0x80000000, -1);
679 test_divl(0x12343, 0x12345678, 0x81234567);
681 #if defined(__x86_64__)
682 test_imulq(0, 0x1234001d1234001d, 45);
683 test_imulq(0, 23, -45);
684 test_imulq(0, 0x8000000000000000, 0x8000000000000000);
685 test_imulq(0, 0x100000000, 0x100000000);
687 test_mulq(0, 0x1234001d1234001d, 45);
688 test_mulq(0, 23, -45);
689 test_mulq(0, 0x8000000000000000, 0x8000000000000000);
690 test_mulq(0, 0x100000000, 0x100000000);
692 test_imulq2(0x1234001d1234001d, 45);
693 test_imulq2(23, -45);
694 test_imulq2(0x8000000000000000, 0x8000000000000000);
695 test_imulq2(0x100000000, 0x100000000);
697 TEST_IMUL_IM("q", "", 45, 0x12341234);
698 TEST_IMUL_IM("q", "", -45, 23);
699 TEST_IMUL_IM("q", "", 0x8000, 0x8000000000000000);
700 TEST_IMUL_IM("q", "", 0x7fff, 0x10000000);
702 test_idivq(0, 0x12345678abcdef, 12347);
703 test_idivq(0, -233223, -45);
704 test_idivq(0, 0x8000000000000000, -1);
705 test_idivq(0x12343, 0x12345678, 0x81234567);
707 test_divq(0, 0x12345678abcdef, 12347);
708 test_divq(0, -233223, -45);
709 test_divq(0, 0x8000000000000000, -1);
710 test_divq(0x12343, 0x12345678, 0x81234567);
714 #define TEST_BSX(op, size, op0)\
716 long res, val, resz;\
719 "mov $0x12345678, %0\n"\
720 #op " %" size "2, %" size "0 ; setz %b1" \
721 : "=&r" (res), "=&q" (resz)\
723 printf("%-10s A=" FMTLX " R=" FMTLX " %ld\n", #op, val, res, resz);\
728 TEST_BSX(bsrw
, "w", 0);
729 TEST_BSX(bsrw
, "w", 0x12340128);
730 TEST_BSX(bsfw
, "w", 0);
731 TEST_BSX(bsfw
, "w", 0x12340128);
732 TEST_BSX(bsrl
, "k", 0);
733 TEST_BSX(bsrl
, "k", 0x00340128);
734 TEST_BSX(bsfl
, "k", 0);
735 TEST_BSX(bsfl
, "k", 0x00340128);
736 #if defined(__x86_64__)
737 TEST_BSX(bsrq
, "", 0);
738 TEST_BSX(bsrq
, "", 0x003401281234);
739 TEST_BSX(bsfq
, "", 0);
740 TEST_BSX(bsfq
, "", 0x003401281234);
744 /**********************************************/
751 union float64u q_nan
= { .l
= 0xFFF8000000000000LL
};
752 union float64u s_nan
= { .l
= 0xFFF0000000000000LL
};
754 void test_fops(double a
, double b
)
756 printf("a=%f b=%f a+b=%f\n", a
, b
, a
+ b
);
757 printf("a=%f b=%f a-b=%f\n", a
, b
, a
- b
);
758 printf("a=%f b=%f a*b=%f\n", a
, b
, a
* b
);
759 printf("a=%f b=%f a/b=%f\n", a
, b
, a
/ b
);
760 printf("a=%f b=%f fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
761 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
762 printf("a=%f sin(a)=%f\n", a
, sin(a
));
763 printf("a=%f cos(a)=%f\n", a
, cos(a
));
764 printf("a=%f tan(a)=%f\n", a
, tan(a
));
765 printf("a=%f log(a)=%f\n", a
, log(a
));
766 printf("a=%f exp(a)=%f\n", a
, exp(a
));
767 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
768 /* just to test some op combining */
769 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
770 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
771 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
775 void fpu_clear_exceptions(void)
785 long double fpregs
[8];
788 asm volatile ("fnstenv %0\n" : "=m" (float_env32
));
789 float_env32
.fpus
&= ~0x7f;
790 asm volatile ("fldenv %0\n" : : "m" (float_env32
));
793 /* XXX: display exception bits when supported */
794 #define FPUS_EMASK 0x0000
795 //#define FPUS_EMASK 0x007f
797 void test_fcmp(double a
, double b
)
801 fpu_clear_exceptions();
806 printf("fcom(%f %f)=%04lx\n",
807 a
, b
, fpus
& (0x4500 | FPUS_EMASK
));
808 fpu_clear_exceptions();
813 printf("fucom(%f %f)=%04lx\n",
814 a
, b
, fpus
& (0x4500 | FPUS_EMASK
));
816 /* test f(u)comi instruction */
817 fpu_clear_exceptions();
822 : "=r" (eflags
), "=a" (fpus
)
824 printf("fcomi(%f %f)=%04lx %02lx\n",
825 a
, b
, fpus
& FPUS_EMASK
, eflags
& (CC_Z
| CC_P
| CC_C
));
826 fpu_clear_exceptions();
827 asm("fucomi %3, %2\n"
831 : "=r" (eflags
), "=a" (fpus
)
833 printf("fucomi(%f %f)=%04lx %02lx\n",
834 a
, b
, fpus
& FPUS_EMASK
, eflags
& (CC_Z
| CC_P
| CC_C
));
836 fpu_clear_exceptions();
837 asm volatile("fxam\n"
841 printf("fxam(%f)=%04lx\n", a
, fpus
& 0x4700);
842 fpu_clear_exceptions();
845 void test_fcvt(double a
)
858 printf("(float)%f = %f\n", a
, fa
);
859 printf("(long double)%f = %Lf\n", a
, la
);
860 printf("a=" FMT64X
"\n", *(uint64_t *)&a
);
861 printf("la=" FMT64X
" %04x\n", *(uint64_t *)&la
,
862 *(unsigned short *)((char *)(&la
) + 8));
864 /* test all roundings */
865 asm volatile ("fstcw %0" : "=m" (fpuc
));
868 val16
= (fpuc
& ~0x0c00) | (i
<< 10);
869 asm volatile ("fldcw %0" : : "m" (val16
));
870 asm volatile ("fist %0" : "=m" (wa
) : "t" (a
));
871 asm volatile ("fistl %0" : "=m" (ia
) : "t" (a
));
872 asm volatile ("fistpll %0" : "=m" (lla
) : "t" (a
) : "st");
873 asm volatile ("frndint ; fstl %0" : "=m" (ra
) : "t" (a
));
874 asm volatile ("fldcw %0" : : "m" (fpuc
));
875 printf("(short)a = %d\n", wa
);
876 printf("(int)a = %d\n", ia
);
877 printf("(int64_t)a = " FMT64X
"\n", lla
);
878 printf("rint(a) = %f\n", ra
);
883 asm("fld" #N : "=t" (a)); \
884 printf("fld" #N "= %f\n", a);
886 void test_fconst(void)
898 void test_fbcd(double a
)
900 unsigned short bcd
[5];
903 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
904 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
905 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
906 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
909 #define TEST_ENV(env, save, restore)\
911 memset((env), 0xaa, sizeof(*(env)));\
913 asm volatile ("fldl %0" : : "m" (dtab[i]));\
914 asm volatile (save " %0\n" : : "m" (*(env)));\
915 asm volatile (restore " %0\n": : "m" (*(env)));\
917 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
919 printf("res[%d]=%f\n", i, rtab[i]);\
920 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
922 (env)->fpus & 0xff00,\
928 struct __attribute__((__packed__
)) {
936 long double fpregs
[8];
938 struct __attribute__((__packed__
)) {
943 long double fpregs
[8];
952 TEST_ENV(&float_env16
, "data16 fnstenv", "data16 fldenv");
953 TEST_ENV(&float_env16
, "data16 fnsave", "data16 frstor");
954 TEST_ENV(&float_env32
, "fnstenv", "fldenv");
955 TEST_ENV(&float_env32
, "fnsave", "frstor");
959 asm volatile ("fldl %0" : : "m" (dtab
[i
]));
960 asm volatile("ffree %st(2)");
961 asm volatile ("fnstenv %0\n" : : "m" (float_env32
));
962 asm volatile ("fninit");
963 printf("fptag=%04x\n", float_env32
.fptag
);
967 #define TEST_FCMOV(a, b, eflags, CC)\
972 "fcmov" CC " %2, %0\n"\
974 : "0" (a), "u" (b), "g" (eflags));\
975 printf("fcmov%s eflags=0x%04lx-> %f\n", \
976 CC, (long)eflags, res);\
979 void test_fcmov(void)
986 for(i
= 0; i
< 4; i
++) {
992 TEST_FCMOV(a
, b
, eflags
, "b");
993 TEST_FCMOV(a
, b
, eflags
, "e");
994 TEST_FCMOV(a
, b
, eflags
, "be");
995 TEST_FCMOV(a
, b
, eflags
, "nb");
996 TEST_FCMOV(a
, b
, eflags
, "ne");
997 TEST_FCMOV(a
, b
, eflags
, "nbe");
999 TEST_FCMOV(a
, b
, 0, "u");
1000 TEST_FCMOV(a
, b
, CC_P
, "u");
1001 TEST_FCMOV(a
, b
, 0, "nu");
1002 TEST_FCMOV(a
, b
, CC_P
, "nu");
1005 void test_floats(void)
1012 test_fcmp(2, q_nan
.d
);
1013 test_fcmp(q_nan
.d
, -1);
1014 test_fcmp(-1.0/0.0, -1);
1015 test_fcmp(1.0/0.0, -1);
1019 test_fcvt(-1.0/9.0);
1022 test_fcvt(-1.0/0.0);
1026 test_fbcd(1234567890123456.0);
1027 test_fbcd(-123451234567890.0);
1034 /**********************************************/
1035 #if !defined(__x86_64__)
1037 #define TEST_BCD(op, op0, cc_in, cc_mask)\
1047 : "=a" (res), "=g" (flags)\
1048 : "0" (res), "1" (flags));\
1049 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
1050 #op, op0, res, cc_in, flags & cc_mask);\
1055 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1056 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1057 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1058 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1059 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1060 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1061 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1062 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1063 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1064 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1065 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1066 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1067 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1069 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1070 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1071 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1072 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1073 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1074 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1075 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1076 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1077 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1078 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1079 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1080 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1081 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
1083 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
1084 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
1085 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
1086 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
1087 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
1088 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
1089 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
1090 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
1092 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
1093 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
1094 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
1095 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
1096 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
1097 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
1098 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
1099 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
1101 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
1102 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
1106 #define TEST_XCHG(op, size, opconst)\
1109 op0 = i2l(0x12345678);\
1110 op1 = i2l(0xfbca7654);\
1111 asm(#op " %" size "0, %" size "1" \
1112 : "=q" (op0), opconst (op1) \
1114 printf("%-10s A=" FMTLX " B=" FMTLX "\n",\
1118 #define TEST_CMPXCHG(op, size, opconst, eax)\
1120 long op0, op1, op2;\
1121 op0 = i2l(0x12345678);\
1122 op1 = i2l(0xfbca7654);\
1124 asm(#op " %" size "0, %" size "1" \
1125 : "=q" (op0), opconst (op1) \
1126 : "0" (op0), "a" (op2));\
1127 printf("%-10s EAX=" FMTLX " A=" FMTLX " C=" FMTLX "\n",\
1128 #op, op2, op0, op1);\
1131 void test_xchg(void)
1133 #if defined(__x86_64__)
1134 TEST_XCHG(xchgq
, "", "+q");
1136 TEST_XCHG(xchgl
, "k", "+q");
1137 TEST_XCHG(xchgw
, "w", "+q");
1138 TEST_XCHG(xchgb
, "b", "+q");
1140 #if defined(__x86_64__)
1141 TEST_XCHG(xchgq
, "", "=m");
1143 TEST_XCHG(xchgl
, "k", "+m");
1144 TEST_XCHG(xchgw
, "w", "+m");
1145 TEST_XCHG(xchgb
, "b", "+m");
1147 #if defined(__x86_64__)
1148 TEST_XCHG(xaddq
, "", "+q");
1150 TEST_XCHG(xaddl
, "k", "+q");
1151 TEST_XCHG(xaddw
, "w", "+q");
1152 TEST_XCHG(xaddb
, "b", "+q");
1157 asm("xaddl %1, %0" : "=r" (res
) : "0" (res
));
1158 printf("xaddl same res=%08x\n", res
);
1161 #if defined(__x86_64__)
1162 TEST_XCHG(xaddq
, "", "+m");
1164 TEST_XCHG(xaddl
, "k", "+m");
1165 TEST_XCHG(xaddw
, "w", "+m");
1166 TEST_XCHG(xaddb
, "b", "+m");
1168 #if defined(__x86_64__)
1169 TEST_CMPXCHG(cmpxchgq
, "", "+q", 0xfbca7654);
1171 TEST_CMPXCHG(cmpxchgl
, "k", "+q", 0xfbca7654);
1172 TEST_CMPXCHG(cmpxchgw
, "w", "+q", 0xfbca7654);
1173 TEST_CMPXCHG(cmpxchgb
, "b", "+q", 0xfbca7654);
1175 #if defined(__x86_64__)
1176 TEST_CMPXCHG(cmpxchgq
, "", "+q", 0xfffefdfc);
1178 TEST_CMPXCHG(cmpxchgl
, "k", "+q", 0xfffefdfc);
1179 TEST_CMPXCHG(cmpxchgw
, "w", "+q", 0xfffefdfc);
1180 TEST_CMPXCHG(cmpxchgb
, "b", "+q", 0xfffefdfc);
1182 #if defined(__x86_64__)
1183 TEST_CMPXCHG(cmpxchgq
, "", "+m", 0xfbca7654);
1185 TEST_CMPXCHG(cmpxchgl
, "k", "+m", 0xfbca7654);
1186 TEST_CMPXCHG(cmpxchgw
, "w", "+m", 0xfbca7654);
1187 TEST_CMPXCHG(cmpxchgb
, "b", "+m", 0xfbca7654);
1189 #if defined(__x86_64__)
1190 TEST_CMPXCHG(cmpxchgq
, "", "+m", 0xfffefdfc);
1192 TEST_CMPXCHG(cmpxchgl
, "k", "+m", 0xfffefdfc);
1193 TEST_CMPXCHG(cmpxchgw
, "w", "+m", 0xfffefdfc);
1194 TEST_CMPXCHG(cmpxchgb
, "b", "+m", 0xfffefdfc);
1197 uint64_t op0
, op1
, op2
;
1201 for(i
= 0; i
< 2; i
++) {
1202 op0
= 0x123456789abcdLL
;
1203 eax
= i2l(op0
& 0xffffffff);
1204 edx
= i2l(op0
>> 32);
1206 op1
= 0xfbca765423456LL
;
1209 op2
= 0x6532432432434LL
;
1210 asm("cmpxchg8b %2\n"
1213 : "=a" (eax
), "=d" (edx
), "=m" (op1
), "=g" (eflags
)
1214 : "0" (eax
), "1" (edx
), "m" (op1
), "b" ((int)op2
), "c" ((int)(op2
>> 32)));
1215 printf("cmpxchg8b: eax=" FMTLX
" edx=" FMTLX
" op1=" FMT64X
" CC=%02lx\n",
1216 eax
, edx
, op1
, eflags
& CC_Z
);
1222 /**********************************************/
1223 /* segmentation tests */
1225 #include <sys/syscall.h>
1227 #include <asm/ldt.h>
1228 #include <linux/version.h>
1230 static inline int modify_ldt(int func
, void * ptr
, unsigned long bytecount
)
1232 return syscall(__NR_modify_ldt
, func
, ptr
, bytecount
);
1235 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
1236 #define modify_ldt_ldt_s user_desc
1239 #define MK_SEL(n) (((n) << 3) | 7)
1241 uint8_t seg_data1
[4096];
1242 uint8_t seg_data2
[4096];
1244 #define TEST_LR(op, size, seg, mask)\
1247 uint16_t mseg = seg;\
1249 asm (op " %" size "2, %" size "0\n" \
1254 : "=r" (res), "=r" (res2) : "m" (mseg), "0" (res));\
1255 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
1258 #define TEST_ARPL(op, size, op1, op2)\
1263 asm volatile(op " %" size "3, %" size "0\n"\
1268 : "=r" (a), "=r" (c) : "0" (a), "r" (b)); \
1269 printf(op size " A=" FMTLX " B=" FMTLX " R=" FMTLX " z=%ld\n",\
1270 (long)(op1), (long)(op2), a, c);\
1273 /* NOTE: we use Linux modify_ldt syscall */
1274 void test_segs(void)
1276 struct modify_ldt_ldt_s ldt
;
1277 long long ldt_table
[3];
1283 } __attribute__((__packed__
)) segoff
;
1285 ldt
.entry_number
= 1;
1286 ldt
.base_addr
= (unsigned long)&seg_data1
;
1287 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1289 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1290 ldt
.read_exec_only
= 0;
1291 ldt
.limit_in_pages
= 1;
1292 ldt
.seg_not_present
= 0;
1294 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1296 ldt
.entry_number
= 2;
1297 ldt
.base_addr
= (unsigned long)&seg_data2
;
1298 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
1300 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1301 ldt
.read_exec_only
= 0;
1302 ldt
.limit_in_pages
= 1;
1303 ldt
.seg_not_present
= 0;
1305 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1307 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
1312 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
1315 /* do some tests with fs or gs */
1316 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1318 seg_data1
[1] = 0xaa;
1319 seg_data2
[1] = 0x55;
1321 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
1322 printf("FS[1] = %02x\n", res
);
1324 asm volatile ("pushl %%gs\n"
1326 "gs movzbl 0x1, %0\n"
1330 printf("GS[1] = %02x\n", res
);
1332 /* tests with ds/ss (implicit segment case) */
1334 asm volatile ("pushl %%ebp\n\t"
1337 "movl %3, %%ebp\n\t"
1338 "movzbl 0x1, %0\n\t"
1339 "movzbl (%%ebp), %1\n\t"
1342 : "=r" (res
), "=r" (res2
)
1343 : "r" (MK_SEL(1)), "r" (&tmp
));
1344 printf("DS[1] = %02x\n", res
);
1345 printf("SS[tmp] = %02x\n", res2
);
1347 segoff
.seg
= MK_SEL(2);
1348 segoff
.offset
= 0xabcdef12;
1349 asm volatile("lfs %2, %0\n\t"
1351 : "=r" (res
), "=g" (res2
)
1353 printf("FS:reg = %04x:%08x\n", res2
, res
);
1355 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1356 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1357 TEST_LR("lslw", "w", MK_SEL(2), 0);
1358 TEST_LR("lsll", "", MK_SEL(2), 0);
1360 TEST_LR("larw", "w", 0xfff8, 0);
1361 TEST_LR("larl", "", 0xfff8, 0);
1362 TEST_LR("lslw", "w", 0xfff8, 0);
1363 TEST_LR("lsll", "", 0xfff8, 0);
1365 TEST_ARPL("arpl", "w", 0x12345678 | 3, 0x762123c | 1);
1366 TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 3);
1367 TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 1);
1370 /* 16 bit code test */
1371 extern char code16_start
, code16_end
;
1372 extern char code16_func1
;
1373 extern char code16_func2
;
1374 extern char code16_func3
;
1376 void test_code16(void)
1378 struct modify_ldt_ldt_s ldt
;
1381 /* build a code segment */
1382 ldt
.entry_number
= 1;
1383 ldt
.base_addr
= (unsigned long)&code16_start
;
1384 ldt
.limit
= &code16_end
- &code16_start
;
1386 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
1387 ldt
.read_exec_only
= 0;
1388 ldt
.limit_in_pages
= 0;
1389 ldt
.seg_not_present
= 0;
1391 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1393 /* call the first function */
1394 asm volatile ("lcall %1, %2"
1396 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
1397 printf("func1() = 0x%08x\n", res
);
1398 asm volatile ("lcall %2, %3"
1399 : "=a" (res
), "=c" (res2
)
1400 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
1401 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
1402 asm volatile ("lcall %1, %2"
1404 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
1405 printf("func3() = 0x%08x\n", res
);
1409 #if defined(__x86_64__)
1410 asm(".globl func_lret\n"
1412 "movl $0x87654641, %eax\n"
1415 asm(".globl func_lret\n"
1417 "movl $0x87654321, %eax\n"
1420 ".globl func_iret\n"
1422 "movl $0xabcd4321, %eax\n"
1426 extern char func_lret
;
1427 extern char func_iret
;
1429 void test_misc(void)
1434 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
1436 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
1437 printf("xlat: EAX=" FMTLX
"\n", res
);
1439 #if defined(__x86_64__)
1442 /* XXX: see if Intel Core2 and AMD64 behavior really
1443 differ. Here we implemented the Intel way which is not
1444 compatible yet with QEMU. */
1445 static struct QEMU_PACKED
{
1451 asm volatile ("mov %%cs, %0" : "=r" (cs_sel
));
1453 asm volatile ("push %1\n"
1456 : "r" (cs_sel
) : "memory", "cc");
1457 printf("func_lret=" FMTLX
"\n", res
);
1459 desc
.offset
= (long)&func_lret
;
1462 asm volatile ("xor %%rax, %%rax\n"
1463 "rex64 lcall *(%%rcx)\n"
1467 printf("func_lret2=" FMTLX
"\n", res
);
1469 asm volatile ("push %2\n"
1472 "rex64 ljmp *(%%rcx)\n"
1475 : "c" (&desc
), "b" (cs_sel
)
1477 printf("func_lret3=" FMTLX
"\n", res
);
1481 asm volatile ("push %%cs ; call %1"
1483 : "m" (func_lret
): "memory", "cc");
1484 printf("func_lret=" FMTLX
"\n", res
);
1486 asm volatile ("pushf ; push %%cs ; call %1"
1488 : "m" (func_iret
): "memory", "cc");
1489 printf("func_iret=" FMTLX
"\n", res
);
1492 #if defined(__x86_64__)
1493 /* specific popl test */
1494 asm volatile ("push $12345432 ; push $0x9abcdef ; pop (%%rsp) ; pop %0"
1496 printf("popl esp=" FMTLX
"\n", res
);
1498 /* specific popl test */
1499 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1501 printf("popl esp=" FMTLX
"\n", res
);
1503 /* specific popw test */
1504 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1506 printf("popw esp=" FMTLX
"\n", res
);
1510 uint8_t str_buffer
[4096];
1512 #define TEST_STRING1(OP, size, DF, REP)\
1514 long esi, edi, eax, ecx, eflags;\
1516 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1517 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1518 eax = i2l(0x12345678);\
1521 asm volatile ("push $0\n\t"\
1524 REP #OP size "\n\t"\
1528 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1529 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1530 printf("%-10s ESI=" FMTLX " EDI=" FMTLX " EAX=" FMTLX " ECX=" FMTLX " EFL=%04x\n",\
1531 REP #OP size, esi, edi, eax, ecx,\
1532 (int)(eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)));\
1535 #define TEST_STRING(OP, REP)\
1536 TEST_STRING1(OP, "b", "", REP);\
1537 TEST_STRING1(OP, "w", "", REP);\
1538 TEST_STRING1(OP, "l", "", REP);\
1539 X86_64_ONLY(TEST_STRING1(OP, "q", "", REP));\
1540 TEST_STRING1(OP, "b", "std", REP);\
1541 TEST_STRING1(OP, "w", "std", REP);\
1542 TEST_STRING1(OP, "l", "std", REP);\
1543 X86_64_ONLY(TEST_STRING1(OP, "q", "std", REP))
1545 void test_string(void)
1548 for(i
= 0;i
< sizeof(str_buffer
); i
++)
1549 str_buffer
[i
] = i
+ 0x56;
1550 TEST_STRING(stos
, "");
1551 TEST_STRING(stos
, "rep ");
1552 TEST_STRING(lods
, ""); /* to verify stos */
1553 TEST_STRING(lods
, "rep ");
1554 TEST_STRING(movs
, "");
1555 TEST_STRING(movs
, "rep ");
1556 TEST_STRING(lods
, ""); /* to verify stos */
1558 /* XXX: better tests */
1559 TEST_STRING(scas
, "");
1560 TEST_STRING(scas
, "repz ");
1561 TEST_STRING(scas
, "repnz ");
1562 TEST_STRING(cmps
, "");
1563 TEST_STRING(cmps
, "repz ");
1564 TEST_STRING(cmps
, "repnz ");
1570 static inline void set_bit(uint8_t *a
, unsigned int bit
)
1572 a
[bit
/ 8] |= (1 << (bit
% 8));
1575 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
1577 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
1580 static inline void pushw(struct vm86_regs
*r
, int val
)
1582 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
1583 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
1586 static inline int vm86(int func
, struct vm86plus_struct
*v86
)
1588 return syscall(__NR_vm86
, func
, v86
);
1591 extern char vm86_code_start
;
1592 extern char vm86_code_end
;
1594 #define VM86_CODE_CS 0x100
1595 #define VM86_CODE_IP 0x100
1597 void test_vm86(void)
1599 struct vm86plus_struct ctx
;
1600 struct vm86_regs
*r
;
1604 vm86_mem
= mmap((void *)0x00000000, 0x110000,
1605 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
1606 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
1607 if (vm86_mem
== MAP_FAILED
) {
1608 printf("ERROR: could not map vm86 memory");
1611 memset(&ctx
, 0, sizeof(ctx
));
1613 /* init basic registers */
1615 r
->eip
= VM86_CODE_IP
;
1624 r
->eflags
= VIF_MASK
;
1626 /* move code to proper address. We use the same layout as a .com
1628 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
1629 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
1631 /* mark int 0x21 as being emulated */
1632 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
1635 ret
= vm86(VM86_ENTER
, &ctx
);
1636 switch(VM86_TYPE(ret
)) {
1641 int_num
= VM86_ARG(ret
);
1642 if (int_num
!= 0x21)
1644 ah
= (r
->eax
>> 8) & 0xff;
1646 case 0x00: /* exit */
1648 case 0x02: /* write char */
1654 case 0x09: /* write string */
1657 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1664 r
->eax
= (r
->eax
& ~0xff) | '$';
1667 case 0xff: /* extension: write eflags number in edx */
1669 #ifndef LINUX_VM86_IOPL_FIX
1672 printf("%08x\n", v
);
1676 printf("unsupported int 0x%02x\n", int_num
);
1682 /* a signal came, we just ignore that */
1687 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1692 printf("VM86 end\n");
1693 munmap(vm86_mem
, 0x110000);
1697 /* exception tests */
1698 #if defined(__i386__) && !defined(REG_EAX)
1709 #define REG_TRAPNO TRAPNO
1713 #if defined(__x86_64__)
1714 #define REG_EIP REG_RIP
1721 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1723 struct ucontext
*uc
= puc
;
1725 printf("si_signo=%d si_errno=%d si_code=%d",
1726 info
->si_signo
, info
->si_errno
, info
->si_code
);
1727 printf(" si_addr=0x%08lx",
1728 (unsigned long)info
->si_addr
);
1731 printf("trapno=" FMTLX
" err=" FMTLX
,
1732 (long)uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1733 (long)uc
->uc_mcontext
.gregs
[REG_ERR
]);
1734 printf(" EIP=" FMTLX
, (long)uc
->uc_mcontext
.gregs
[REG_EIP
]);
1736 longjmp(jmp_env
, 1);
1739 void test_exceptions(void)
1741 struct sigaction act
;
1744 act
.sa_sigaction
= sig_handler
;
1745 sigemptyset(&act
.sa_mask
);
1746 act
.sa_flags
= SA_SIGINFO
| SA_NODEFER
;
1747 sigaction(SIGFPE
, &act
, NULL
);
1748 sigaction(SIGILL
, &act
, NULL
);
1749 sigaction(SIGSEGV
, &act
, NULL
);
1750 sigaction(SIGBUS
, &act
, NULL
);
1751 sigaction(SIGTRAP
, &act
, NULL
);
1753 /* test division by zero reporting */
1754 printf("DIVZ exception:\n");
1755 if (setjmp(jmp_env
) == 0) {
1756 /* now divide by zero */
1761 #if !defined(__x86_64__)
1762 printf("BOUND exception:\n");
1763 if (setjmp(jmp_env
) == 0) {
1764 /* bound exception */
1767 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
[0]));
1772 printf("segment exceptions:\n");
1773 if (setjmp(jmp_env
) == 0) {
1774 /* load an invalid segment */
1775 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1777 if (setjmp(jmp_env
) == 0) {
1778 /* null data segment is valid */
1779 asm volatile ("movl %0, %%fs" : : "r" (3));
1780 /* null stack segment */
1781 asm volatile ("movl %0, %%ss" : : "r" (3));
1785 struct modify_ldt_ldt_s ldt
;
1786 ldt
.entry_number
= 1;
1787 ldt
.base_addr
= (unsigned long)&seg_data1
;
1788 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1790 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1791 ldt
.read_exec_only
= 0;
1792 ldt
.limit_in_pages
= 1;
1793 ldt
.seg_not_present
= 1;
1795 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1797 if (setjmp(jmp_env
) == 0) {
1798 /* segment not present */
1799 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1804 /* test SEGV reporting */
1805 printf("PF exception:\n");
1806 if (setjmp(jmp_env
) == 0) {
1808 /* we add a nop to test a weird PC retrieval case */
1809 asm volatile ("nop");
1810 /* now store in an invalid address */
1811 *(char *)0x1234 = 1;
1814 /* test SEGV reporting */
1815 printf("PF exception:\n");
1816 if (setjmp(jmp_env
) == 0) {
1818 /* read from an invalid address */
1819 v1
= *(char *)0x1234;
1822 /* test illegal instruction reporting */
1823 printf("UD2 exception:\n");
1824 if (setjmp(jmp_env
) == 0) {
1825 /* now execute an invalid instruction */
1826 asm volatile("ud2");
1828 printf("lock nop exception:\n");
1829 if (setjmp(jmp_env
) == 0) {
1830 /* now execute an invalid instruction */
1831 asm volatile(".byte 0xf0, 0x90");
1834 printf("INT exception:\n");
1835 if (setjmp(jmp_env
) == 0) {
1836 asm volatile ("int $0xfd");
1838 if (setjmp(jmp_env
) == 0) {
1839 asm volatile ("int $0x01");
1841 if (setjmp(jmp_env
) == 0) {
1842 asm volatile (".byte 0xcd, 0x03");
1844 if (setjmp(jmp_env
) == 0) {
1845 asm volatile ("int $0x04");
1847 if (setjmp(jmp_env
) == 0) {
1848 asm volatile ("int $0x05");
1851 printf("INT3 exception:\n");
1852 if (setjmp(jmp_env
) == 0) {
1853 asm volatile ("int3");
1856 printf("CLI exception:\n");
1857 if (setjmp(jmp_env
) == 0) {
1858 asm volatile ("cli");
1861 printf("STI exception:\n");
1862 if (setjmp(jmp_env
) == 0) {
1863 asm volatile ("cli");
1866 #if !defined(__x86_64__)
1867 printf("INTO exception:\n");
1868 if (setjmp(jmp_env
) == 0) {
1869 /* overflow exception */
1870 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1874 printf("OUTB exception:\n");
1875 if (setjmp(jmp_env
) == 0) {
1876 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1879 printf("INB exception:\n");
1880 if (setjmp(jmp_env
) == 0) {
1881 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1884 printf("REP OUTSB exception:\n");
1885 if (setjmp(jmp_env
) == 0) {
1886 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1889 printf("REP INSB exception:\n");
1890 if (setjmp(jmp_env
) == 0) {
1891 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1894 printf("HLT exception:\n");
1895 if (setjmp(jmp_env
) == 0) {
1896 asm volatile ("hlt");
1899 printf("single step exception:\n");
1901 if (setjmp(jmp_env
) == 0) {
1902 asm volatile ("pushf\n"
1903 "orl $0x00100, (%%esp)\n"
1905 "movl $0xabcd, %0\n"
1906 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1908 printf("val=0x%x\n", val
);
1911 #if !defined(__x86_64__)
1912 /* specific precise single step test */
1913 void sig_trap_handler(int sig
, siginfo_t
*info
, void *puc
)
1915 struct ucontext
*uc
= puc
;
1916 printf("EIP=" FMTLX
"\n", (long)uc
->uc_mcontext
.gregs
[REG_EIP
]);
1919 const uint8_t sstep_buf1
[4] = { 1, 2, 3, 4};
1920 uint8_t sstep_buf2
[4];
1922 void test_single_step(void)
1924 struct sigaction act
;
1929 act
.sa_sigaction
= sig_trap_handler
;
1930 sigemptyset(&act
.sa_mask
);
1931 act
.sa_flags
= SA_SIGINFO
;
1932 sigaction(SIGTRAP
, &act
, NULL
);
1933 asm volatile ("pushf\n"
1934 "orl $0x00100, (%%esp)\n"
1936 "movl $0xabcd, %0\n"
1945 /* movsb: the single step should stop at each movsb iteration */
1946 "movl $sstep_buf1, %%esi\n"
1947 "movl $sstep_buf2, %%edi\n"
1955 /* cmpsb: the single step should stop at each cmpsb iteration */
1956 "movl $sstep_buf1, %%esi\n"
1957 "movl $sstep_buf2, %%edi\n"
1963 /* getpid() syscall: single step should skip one
1969 /* when modifying SS, trace is not done on the next
1971 "movl %%ss, %%ecx\n"
1972 "movl %%ecx, %%ss\n"
1975 "movl %%ecx, %%ss\n"
1986 "andl $~0x00100, (%%esp)\n"
1990 : "cc", "memory", "eax", "ecx", "esi", "edi");
1991 printf("val=%d\n", val
);
1992 for(i
= 0; i
< 4; i
++)
1993 printf("sstep_buf2[%d] = %d\n", i
, sstep_buf2
[i
]);
1996 /* self modifying code test */
1998 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
2002 asm(".section \".data\"\n"
2004 "movl 4(%esp), %eax\n"
2005 "movl %eax, smc_patch_addr2 + 1\n"
2014 "smc_patch_addr2:\n"
2020 typedef int FuncType(void);
2021 extern int smc_code2(int);
2022 void test_self_modifying_code(void)
2025 printf("self modifying code:\n");
2026 printf("func1 = 0x%x\n", ((FuncType
*)code
)());
2027 for(i
= 2; i
<= 4; i
++) {
2029 printf("func%d = 0x%x\n", i
, ((FuncType
*)code
)());
2032 /* more difficult test : the modified code is just after the
2033 modifying instruction. It is forbidden in Intel specs, but it
2034 is used by old DOS programs */
2035 for(i
= 2; i
<= 4; i
++) {
2036 printf("smc_code2(%d) = %d\n", i
, smc_code2(i
));
2041 long enter_stack
[4096];
2043 #if defined(__x86_64__)
2051 #if !defined(__x86_64__)
2052 /* causes an infinite loop, disable it for now. */
2053 #define TEST_ENTER(size, stack_type, level)
2055 #define TEST_ENTER(size, stack_type, level)\
2057 long esp_save, esp_val, ebp_val, ebp_save, i;\
2058 stack_type *ptr, *stack_end, *stack_ptr;\
2059 memset(enter_stack, 0, sizeof(enter_stack));\
2060 stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
2061 ebp_val = (long)stack_ptr;\
2064 esp_val = (long)stack_ptr;\
2065 asm("mov " RSP ", %[esp_save]\n"\
2066 "mov " RBP ", %[ebp_save]\n"\
2067 "mov %[esp_val], " RSP "\n"\
2068 "mov %[ebp_val], " RBP "\n"\
2069 "enter" size " $8, $" #level "\n"\
2070 "mov " RSP ", %[esp_val]\n"\
2071 "mov " RBP ", %[ebp_val]\n"\
2072 "mov %[esp_save], " RSP "\n"\
2073 "mov %[ebp_save], " RBP "\n"\
2074 : [esp_save] "=r" (esp_save),\
2075 [ebp_save] "=r" (ebp_save),\
2076 [esp_val] "=r" (esp_val),\
2077 [ebp_val] "=r" (ebp_val)\
2078 : "[esp_val]" (esp_val),\
2079 "[ebp_val]" (ebp_val));\
2080 printf("level=%d:\n", level);\
2081 printf("esp_val=" FMTLX "\n", esp_val - (long)stack_end);\
2082 printf("ebp_val=" FMTLX "\n", ebp_val - (long)stack_end);\
2083 for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
2084 printf(FMTLX "\n", (long)ptr[0]);\
2088 static void test_enter(void)
2090 #if defined(__x86_64__)
2091 TEST_ENTER("q", uint64_t, 0);
2092 TEST_ENTER("q", uint64_t, 1);
2093 TEST_ENTER("q", uint64_t, 2);
2094 TEST_ENTER("q", uint64_t, 31);
2096 TEST_ENTER("l", uint32_t, 0);
2097 TEST_ENTER("l", uint32_t, 1);
2098 TEST_ENTER("l", uint32_t, 2);
2099 TEST_ENTER("l", uint32_t, 31);
2102 TEST_ENTER("w", uint16_t, 0);
2103 TEST_ENTER("w", uint16_t, 1);
2104 TEST_ENTER("w", uint16_t, 2);
2105 TEST_ENTER("w", uint16_t, 31);
2110 typedef int __m64
__attribute__ ((__mode__ (__V2SI__
)));
2111 typedef float __m128
__attribute__ ((__mode__(__V4SF__
)));
2121 static uint64_t __attribute__((aligned(16))) test_values
[4][2] = {
2122 { 0x456723c698694873, 0xdc515cff944a58ec },
2123 { 0x1f297ccd58bad7ab, 0x41f21efba9e3e146 },
2124 { 0x007c62c2085427f8, 0x231be9e8cde7438d },
2125 { 0x0f76255a085427f8, 0xc233e9e8c4c9439a },
2130 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
2131 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
2138 #define SSE_OP2(op)\
2142 a.q[0] = test_values[2*i][0];\
2143 a.q[1] = test_values[2*i][1];\
2144 b.q[0] = test_values[2*i+1][0];\
2145 b.q[1] = test_values[2*i+1][1];\
2150 #define MMX_OP2(op)\
2154 a.q[0] = test_values[2*i][0];\
2155 b.q[0] = test_values[2*i+1][0];\
2156 asm volatile (#op " %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0]));\
2157 printf("%-9s: a=" FMT64X " b=" FMT64X " r=" FMT64X "\n",\
2166 #define SHUF_OP(op, ib)\
2168 a.q[0] = test_values[0][0];\
2169 a.q[1] = test_values[0][1];\
2170 b.q[0] = test_values[1][0];\
2171 b.q[1] = test_values[1][1];\
2172 asm volatile (#op " $" #ib ", %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
2173 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
2181 #define PSHUF_OP(op, ib)\
2185 a.q[0] = test_values[2*i][0];\
2186 a.q[1] = test_values[2*i][1];\
2187 asm volatile (#op " $" #ib ", %1, %0" : "=x" (r.dq) : "x" (a.dq));\
2188 printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
2196 #define SHIFT_IM(op, ib)\
2200 a.q[0] = test_values[2*i][0];\
2201 a.q[1] = test_values[2*i][1];\
2202 asm volatile (#op " $" #ib ", %0" : "=x" (r.dq) : "0" (a.dq));\
2203 printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
2211 #define SHIFT_OP(op, ib)\
2216 a.q[0] = test_values[2*i][0];\
2217 a.q[1] = test_values[2*i][1];\
2220 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
2221 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
2233 a.q[0] = test_values[2*i][0];\
2234 a.q[1] = test_values[2*i][1];\
2235 asm volatile (#op " %1, %0" : "=r" (reg) : "x" (a.dq));\
2236 printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
2243 #define SSE_OPS(a) \
2247 #define SSE_OPD(a) \
2251 #define SSE_COMI(op, field)\
2253 unsigned long eflags;\
2257 asm volatile (#op " %2, %1\n"\
2261 : "x" (a.dq), "x" (b.dq));\
2262 printf("%-9s: a=%f b=%f cc=%04x\n",\
2264 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
2267 void test_sse_comi(double a1
, double b1
)
2269 SSE_COMI(ucomiss
, s
);
2270 SSE_COMI(ucomisd
, d
);
2271 SSE_COMI(comiss
, s
);
2272 SSE_COMI(comisd
, d
);
2275 #define CVT_OP_XMM(op)\
2277 asm volatile (#op " %1, %0" : "=x" (r.dq) : "x" (a.dq));\
2278 printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
2284 /* Force %xmm0 usage to avoid the case where both register index are 0
2285 to test instruction decoding more extensively */
2286 #define CVT_OP_XMM2MMX(op)\
2288 asm volatile (#op " %1, %0" : "=y" (r.q[0]) : "x" (a.dq) \
2290 asm volatile("emms\n"); \
2291 printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "\n",\
2297 #define CVT_OP_MMX2XMM(op)\
2299 asm volatile (#op " %1, %0" : "=x" (r.dq) : "y" (a.q[0]));\
2300 asm volatile("emms\n"); \
2301 printf("%-9s: a=" FMT64X " r=" FMT64X "" FMT64X "\n",\
2307 #define CVT_OP_REG2XMM(op)\
2309 asm volatile (#op " %1, %0" : "=x" (r.dq) : "r" (a.l[0]));\
2310 printf("%-9s: a=%08x r=" FMT64X "" FMT64X "\n",\
2316 #define CVT_OP_XMM2REG(op)\
2318 asm volatile (#op " %1, %0" : "=r" (r.l[0]) : "x" (a.dq));\
2319 printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
2337 uint32_t mxcsr_mask
;
2338 uint8_t fpregs1
[8 * 16];
2339 uint8_t xmm_regs
[8 * 16];
2340 uint8_t dummy2
[224];
2343 static struct fpxstate fpx_state
__attribute__((aligned(16)));
2344 static struct fpxstate fpx_state2
__attribute__((aligned(16)));
2346 void test_fxsave(void)
2348 struct fpxstate
*fp
= &fpx_state
;
2349 struct fpxstate
*fp2
= &fpx_state2
;
2352 a
.q
[0] = test_values
[0][0];
2353 a
.q
[1] = test_values
[0][1];
2354 b
.q
[0] = test_values
[1][0];
2355 b
.q
[1] = test_values
[1][1];
2357 asm("movdqa %2, %%xmm0\n"
2358 "movdqa %3, %%xmm7\n"
2359 #if defined(__x86_64__)
2360 "movdqa %2, %%xmm15\n"
2369 : "=m" (*(uint32_t *)fp2
), "=m" (*(uint32_t *)fp
)
2370 : "m" (a
), "m" (b
));
2371 printf("fpuc=%04x\n", fp
->fpuc
);
2372 printf("fpus=%04x\n", fp
->fpus
);
2373 printf("fptag=%04x\n", fp
->fptag
);
2374 for(i
= 0; i
< 3; i
++) {
2375 printf("ST%d: " FMT64X
" %04x\n",
2377 *(uint64_t *)&fp
->fpregs1
[i
* 16],
2378 *(uint16_t *)&fp
->fpregs1
[i
* 16 + 8]);
2380 printf("mxcsr=%08x\n", fp
->mxcsr
& 0x1f80);
2381 #if defined(__x86_64__)
2386 for(i
= 0; i
< nb_xmm
; i
++) {
2387 printf("xmm%d: " FMT64X
"" FMT64X
"\n",
2389 *(uint64_t *)&fp
->xmm_regs
[i
* 16],
2390 *(uint64_t *)&fp
->xmm_regs
[i
* 16 + 8]);
2451 asm volatile ("pinsrw $1, %1, %0" : "=y" (r
.q
[0]) : "r" (0x12345678));
2452 printf("%-9s: r=" FMT64X
"\n", "pinsrw", r
.q
[0]);
2454 asm volatile ("pinsrw $5, %1, %0" : "=x" (r
.dq
) : "r" (0x12345678));
2455 printf("%-9s: r=" FMT64X
"" FMT64X
"\n", "pinsrw", r
.q
[1], r
.q
[0]);
2457 a
.q
[0] = test_values
[0][0];
2458 a
.q
[1] = test_values
[0][1];
2459 asm volatile ("pextrw $1, %1, %0" : "=r" (r
.l
[0]) : "y" (a
.q
[0]));
2460 printf("%-9s: r=%08x\n", "pextrw", r
.l
[0]);
2462 asm volatile ("pextrw $5, %1, %0" : "=r" (r
.l
[0]) : "x" (a
.dq
));
2463 printf("%-9s: r=%08x\n", "pextrw", r
.l
[0]);
2465 asm volatile ("pmovmskb %1, %0" : "=r" (r
.l
[0]) : "y" (a
.q
[0]));
2466 printf("%-9s: r=%08x\n", "pmovmskb", r
.l
[0]);
2468 asm volatile ("pmovmskb %1, %0" : "=r" (r
.l
[0]) : "x" (a
.dq
));
2469 printf("%-9s: r=%08x\n", "pmovmskb", r
.l
[0]);
2475 a
.q
[0] = test_values
[0][0];
2476 a
.q
[1] = test_values
[0][1];
2477 b
.q
[0] = test_values
[1][0];
2478 b
.q
[1] = test_values
[1][1];
2479 asm volatile("maskmovq %1, %0" :
2480 : "y" (a
.q
[0]), "y" (b
.q
[0]), "D" (&r
)
2482 printf("%-9s: r=" FMT64X
" a=" FMT64X
" b=" FMT64X
"\n",
2487 asm volatile("maskmovdqu %1, %0" :
2488 : "x" (a
.dq
), "x" (b
.dq
), "D" (&r
)
2490 printf("%-9s: r=" FMT64X
"" FMT64X
" a=" FMT64X
"" FMT64X
" b=" FMT64X
"" FMT64X
"\n",
2497 asm volatile ("emms");
2499 SSE_OP2(punpcklqdq
);
2500 SSE_OP2(punpckhqdq
);
2515 SHUF_OP(shufps
, 0x78);
2516 SHUF_OP(shufpd
, 0x02);
2518 PSHUF_OP(pshufd
, 0x78);
2519 PSHUF_OP(pshuflw
, 0x78);
2520 PSHUF_OP(pshufhw
, 0x78);
2523 SHIFT_OP(psrlw
, 16);
2525 SHIFT_OP(psraw
, 16);
2527 SHIFT_OP(psllw
, 16);
2530 SHIFT_OP(psrld
, 32);
2532 SHIFT_OP(psrad
, 32);
2534 SHIFT_OP(pslld
, 32);
2537 SHIFT_OP(psrlq
, 32);
2539 SHIFT_OP(psllq
, 32);
2541 SHIFT_IM(psrldq
, 16);
2542 SHIFT_IM(psrldq
, 7);
2543 SHIFT_IM(pslldq
, 16);
2544 SHIFT_IM(pslldq
, 7);
2549 /* FPU specific ops */
2553 asm volatile("stmxcsr %0" : "=m" (mxcsr
));
2554 printf("mxcsr=%08x\n", mxcsr
& 0x1f80);
2555 asm volatile("ldmxcsr %0" : : "m" (mxcsr
));
2558 test_sse_comi(2, -1);
2559 test_sse_comi(2, 2);
2560 test_sse_comi(2, 3);
2561 test_sse_comi(2, q_nan
.d
);
2562 test_sse_comi(q_nan
.d
, -1);
2564 for(i
= 0; i
< 2; i
++) {
2620 /* float to float/int */
2625 CVT_OP_XMM(cvtps2pd
);
2626 CVT_OP_XMM(cvtss2sd
);
2627 CVT_OP_XMM2MMX(cvtps2pi
);
2628 CVT_OP_XMM2MMX(cvttps2pi
);
2629 CVT_OP_XMM2REG(cvtss2si
);
2630 CVT_OP_XMM2REG(cvttss2si
);
2631 CVT_OP_XMM(cvtps2dq
);
2632 CVT_OP_XMM(cvttps2dq
);
2636 CVT_OP_XMM(cvtpd2ps
);
2637 CVT_OP_XMM(cvtsd2ss
);
2638 CVT_OP_XMM2MMX(cvtpd2pi
);
2639 CVT_OP_XMM2MMX(cvttpd2pi
);
2640 CVT_OP_XMM2REG(cvtsd2si
);
2641 CVT_OP_XMM2REG(cvttsd2si
);
2642 CVT_OP_XMM(cvtpd2dq
);
2643 CVT_OP_XMM(cvttpd2dq
);
2646 CVT_OP_XMM2MMX(movdq2q
);
2647 CVT_OP_MMX2XMM(movq2dq
);
2654 CVT_OP_MMX2XMM(cvtpi2ps
);
2655 CVT_OP_MMX2XMM(cvtpi2pd
);
2656 CVT_OP_REG2XMM(cvtsi2ss
);
2657 CVT_OP_REG2XMM(cvtsi2sd
);
2658 CVT_OP_XMM(cvtdq2ps
);
2659 CVT_OP_XMM(cvtdq2pd
);
2661 /* XXX: test PNI insns */
2665 asm volatile ("emms");
2670 #define TEST_CONV_RAX(op)\
2672 unsigned long a, r;\
2673 a = i2l(0x8234a6f8);\
2675 asm volatile(#op : "=a" (r) : "0" (r));\
2676 printf("%-10s A=" FMTLX " R=" FMTLX "\n", #op, a, r);\
2679 #define TEST_CONV_RAX_RDX(op)\
2681 unsigned long a, d, r, rh; \
2682 a = i2l(0x8234a6f8);\
2683 d = i2l(0x8345a1f2);\
2686 asm volatile(#op : "=a" (r), "=d" (rh) : "0" (r), "1" (rh)); \
2687 printf("%-10s A=" FMTLX " R=" FMTLX ":" FMTLX "\n", #op, a, r, rh); \
2690 void test_conv(void)
2693 TEST_CONV_RAX(cwde
);
2694 #if defined(__x86_64__)
2695 TEST_CONV_RAX(cdqe
);
2698 TEST_CONV_RAX_RDX(cwd
);
2699 TEST_CONV_RAX_RDX(cdq
);
2700 #if defined(__x86_64__)
2701 TEST_CONV_RAX_RDX(cqo
);
2706 a
= i2l(0x12345678);
2707 asm volatile("bswapl %k0" : "=r" (r
) : "0" (a
));
2708 printf("%-10s: A=" FMTLX
" R=" FMTLX
"\n", "bswapl", a
, r
);
2710 #if defined(__x86_64__)
2713 a
= i2l(0x12345678);
2714 asm volatile("bswapq %0" : "=r" (r
) : "0" (a
));
2715 printf("%-10s: A=" FMTLX
" R=" FMTLX
"\n", "bswapq", a
, r
);
2720 extern void *__start_initcall
;
2721 extern void *__stop_initcall
;
2724 int main(int argc
, char **argv
)
2729 ptr
= &__start_initcall
;
2730 while (ptr
!= &__stop_initcall
) {
2739 #if !defined(__x86_64__)
2753 #if !defined(__x86_64__)
2755 test_self_modifying_code();