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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/ucontext.h>
32 #if !defined(__x86_64__)
36 //#define LINUX_VM86_IOPL_FIX
37 //#define TEST_P4_FLAGS
38 #if defined(__x86_64__)
47 #if defined(__x86_64__)
48 #define FMT64X "%016lx"
49 #define FMTLX "%016lx"
50 #define X86_64_ONLY(x) x
52 #define FMT64X "%016llx"
54 #define X86_64_ONLY(x)
61 #define xglue(x, y) x ## y
62 #define glue(x, y) xglue(x, y)
63 #define stringify(s) tostring(s)
64 #define tostring(s) #s
73 #define __init_call __attribute__ ((unused,__section__ ("initcall")))
75 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
77 #if defined(__x86_64__)
78 static inline long i2l(long v)
80 return v | ((v ^ 0xabcd) << 32);
83 static inline long i2l(long v)
90 #include "test-i386.h"
93 #include "test-i386.h"
96 #include "test-i386.h"
99 #include "test-i386.h"
102 #include "test-i386.h"
105 #include "test-i386.h"
109 #include "test-i386.h"
113 #include "test-i386.h"
118 #include "test-i386.h"
123 #include "test-i386.h"
128 #include "test-i386.h"
133 #include "test-i386.h"
136 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
139 #include "test-i386-shift.h"
142 #include "test-i386-shift.h"
145 #include "test-i386-shift.h"
148 #include "test-i386-shift.h"
151 #include "test-i386-shift.h"
155 #include "test-i386-shift.h"
159 #include "test-i386-shift.h"
164 #include "test-i386-shift.h"
169 #include "test-i386-shift.h"
171 /* XXX: should be more precise ? */
173 #define CC_MASK (CC_C)
177 #include "test-i386-shift.h"
181 #include "test-i386-shift.h"
185 #include "test-i386-shift.h"
189 #include "test-i386-shift.h"
191 /* lea test (modrm support) */
192 #define TEST_LEAQ(STR)\
194 asm("lea " STR ", %0"\
196 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
197 printf("lea %s = " FMTLX "\n", STR, res);\
200 #define TEST_LEA(STR)\
202 asm("lea " STR ", %0"\
204 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
205 printf("lea %s = " FMTLX "\n", STR, res);\
208 #define TEST_LEA16(STR)\
210 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
212 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
213 printf("lea %s = %08lx\n", STR, res);\
219 long eax, ebx, ecx, edx, esi, edi, res;
236 TEST_LEA("0x40(%%eax)");
237 TEST_LEA("0x40(%%ebx)");
238 TEST_LEA("0x40(%%ecx)");
239 TEST_LEA("0x40(%%edx)");
240 TEST_LEA("0x40(%%esi)");
241 TEST_LEA("0x40(%%edi)");
243 TEST_LEA("0x4000(%%eax)");
244 TEST_LEA("0x4000(%%ebx)");
245 TEST_LEA("0x4000(%%ecx)");
246 TEST_LEA("0x4000(%%edx)");
247 TEST_LEA("0x4000(%%esi)");
248 TEST_LEA("0x4000(%%edi)");
250 TEST_LEA("(%%eax, %%ecx)");
251 TEST_LEA("(%%ebx, %%edx)");
252 TEST_LEA("(%%ecx, %%ecx)");
253 TEST_LEA("(%%edx, %%ecx)");
254 TEST_LEA("(%%esi, %%ecx)");
255 TEST_LEA("(%%edi, %%ecx)");
257 TEST_LEA("0x40(%%eax, %%ecx)");
258 TEST_LEA("0x4000(%%ebx, %%edx)");
260 TEST_LEA("(%%ecx, %%ecx, 2)");
261 TEST_LEA("(%%edx, %%ecx, 4)");
262 TEST_LEA("(%%esi, %%ecx, 8)");
264 TEST_LEA("(,%%eax, 2)");
265 TEST_LEA("(,%%ebx, 4)");
266 TEST_LEA("(,%%ecx, 8)");
268 TEST_LEA("0x40(,%%eax, 2)");
269 TEST_LEA("0x40(,%%ebx, 4)");
270 TEST_LEA("0x40(,%%ecx, 8)");
273 TEST_LEA("-10(%%ecx, %%ecx, 2)");
274 TEST_LEA("-10(%%edx, %%ecx, 4)");
275 TEST_LEA("-10(%%esi, %%ecx, 8)");
277 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
278 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
279 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
281 #if defined(__x86_64__)
283 TEST_LEAQ("0x4000(%%rip)");
285 TEST_LEAQ("(%%rax)");
286 TEST_LEAQ("(%%rbx)");
287 TEST_LEAQ("(%%rcx)");
288 TEST_LEAQ("(%%rdx)");
289 TEST_LEAQ("(%%rsi)");
290 TEST_LEAQ("(%%rdi)");
292 TEST_LEAQ("0x40(%%rax)");
293 TEST_LEAQ("0x40(%%rbx)");
294 TEST_LEAQ("0x40(%%rcx)");
295 TEST_LEAQ("0x40(%%rdx)");
296 TEST_LEAQ("0x40(%%rsi)");
297 TEST_LEAQ("0x40(%%rdi)");
299 TEST_LEAQ("0x4000(%%rax)");
300 TEST_LEAQ("0x4000(%%rbx)");
301 TEST_LEAQ("0x4000(%%rcx)");
302 TEST_LEAQ("0x4000(%%rdx)");
303 TEST_LEAQ("0x4000(%%rsi)");
304 TEST_LEAQ("0x4000(%%rdi)");
306 TEST_LEAQ("(%%rax, %%rcx)");
307 TEST_LEAQ("(%%rbx, %%rdx)");
308 TEST_LEAQ("(%%rcx, %%rcx)");
309 TEST_LEAQ("(%%rdx, %%rcx)");
310 TEST_LEAQ("(%%rsi, %%rcx)");
311 TEST_LEAQ("(%%rdi, %%rcx)");
313 TEST_LEAQ("0x40(%%rax, %%rcx)");
314 TEST_LEAQ("0x4000(%%rbx, %%rdx)");
316 TEST_LEAQ("(%%rcx, %%rcx, 2)");
317 TEST_LEAQ("(%%rdx, %%rcx, 4)");
318 TEST_LEAQ("(%%rsi, %%rcx, 8)");
320 TEST_LEAQ("(,%%rax, 2)");
321 TEST_LEAQ("(,%%rbx, 4)");
322 TEST_LEAQ("(,%%rcx, 8)");
324 TEST_LEAQ("0x40(,%%rax, 2)");
325 TEST_LEAQ("0x40(,%%rbx, 4)");
326 TEST_LEAQ("0x40(,%%rcx, 8)");
329 TEST_LEAQ("-10(%%rcx, %%rcx, 2)");
330 TEST_LEAQ("-10(%%rdx, %%rcx, 4)");
331 TEST_LEAQ("-10(%%rsi, %%rcx, 8)");
333 TEST_LEAQ("0x4000(%%rcx, %%rcx, 2)");
334 TEST_LEAQ("0x4000(%%rdx, %%rcx, 4)");
335 TEST_LEAQ("0x4000(%%rsi, %%rcx, 8)");
337 /* limited 16 bit addressing test */
338 TEST_LEA16("0x4000");
339 TEST_LEA16("(%%bx)");
340 TEST_LEA16("(%%si)");
341 TEST_LEA16("(%%di)");
342 TEST_LEA16("0x40(%%bx)");
343 TEST_LEA16("0x40(%%si)");
344 TEST_LEA16("0x40(%%di)");
345 TEST_LEA16("0x4000(%%bx)");
346 TEST_LEA16("0x4000(%%si)");
347 TEST_LEA16("(%%bx,%%si)");
348 TEST_LEA16("(%%bx,%%di)");
349 TEST_LEA16("0x40(%%bx,%%si)");
350 TEST_LEA16("0x40(%%bx,%%di)");
351 TEST_LEA16("0x4000(%%bx,%%si)");
352 TEST_LEA16("0x4000(%%bx,%%di)");
356 #define TEST_JCC(JCC, v1, v2)\
359 asm("movl $1, %0\n\t"\
365 : "r" (v1), "r" (v2));\
366 printf("%-10s %d\n", "j" JCC, res);\
368 asm("movl $0, %0\n\t"\
370 "set" JCC " %b0\n\t"\
372 : "r" (v1), "r" (v2));\
373 printf("%-10s %d\n", "set" JCC, res);\
376 long res = i2l(0x12345678);\
378 asm("cmpl %2, %1\n\t"\
379 "cmov" JCC "q %3, %0\n\t"\
381 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
382 printf("%-10s R=" FMTLX "\n", "cmov" JCC "l", res);)\
383 asm("cmpl %2, %1\n\t"\
384 "cmov" JCC "l %k3, %k0\n\t"\
386 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
387 printf("%-10s R=" FMTLX "\n", "cmov" JCC "l", res);\
388 asm("cmpl %2, %1\n\t"\
389 "cmov" JCC "w %w3, %w0\n\t"\
391 : "r" (v1), "r" (v2), "r" (1), "0" (res));\
392 printf("%-10s R=" FMTLX "\n", "cmov" JCC "w", res);\
396 /* various jump tests */
399 TEST_JCC("ne", 1, 1);
400 TEST_JCC("ne", 1, 0);
407 TEST_JCC("l", 1, -1);
409 TEST_JCC("le", 1, 1);
410 TEST_JCC("le", 1, 0);
411 TEST_JCC("le", 1, -1);
413 TEST_JCC("ge", 1, 1);
414 TEST_JCC("ge", 1, 0);
415 TEST_JCC("ge", -1, 1);
419 TEST_JCC("g", 1, -1);
423 TEST_JCC("b", 1, -1);
425 TEST_JCC("be", 1, 1);
426 TEST_JCC("be", 1, 0);
427 TEST_JCC("be", 1, -1);
429 TEST_JCC("ae", 1, 1);
430 TEST_JCC("ae", 1, 0);
431 TEST_JCC("ae", 1, -1);
435 TEST_JCC("a", 1, -1);
441 TEST_JCC("np", 1, 1);
442 TEST_JCC("np", 1, 0);
444 TEST_JCC("o", 0x7fffffff, 0);
445 TEST_JCC("o", 0x7fffffff, -1);
447 TEST_JCC("no", 0x7fffffff, 0);
448 TEST_JCC("no", 0x7fffffff, -1);
451 TEST_JCC("s", 0, -1);
454 TEST_JCC("ns", 0, 1);
455 TEST_JCC("ns", 0, -1);
456 TEST_JCC("ns", 0, 0);
461 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
463 #define CC_MASK (CC_O | CC_C)
467 #include "test-i386-muldiv.h"
470 #include "test-i386-muldiv.h"
472 void test_imulw2(long op0, long op1)
474 long res, s1, s0, flags;
479 asm volatile ("push %4\n\t"
484 : "=q" (res), "=g" (flags)
485 : "q" (s1), "0" (res), "1" (flags));
486 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
487 "imulw", s0, s1, res, flags & CC_MASK);
490 void test_imull2(long op0, long op1)
492 long res, s1, s0, flags;
497 asm volatile ("push %4\n\t"
502 : "=q" (res), "=g" (flags)
503 : "q" (s1), "0" (res), "1" (flags));
504 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
505 "imull", s0, s1, res, flags & CC_MASK);
508 #if defined(__x86_64__)
509 void test_imulq2(long op0, long op1)
511 long res, s1, s0, flags;
516 asm volatile ("push %4\n\t"
521 : "=q" (res), "=g" (flags)
522 : "q" (s1), "0" (res), "1" (flags));
523 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
524 "imulq", s0, s1, res, flags & CC_MASK);
528 #define TEST_IMUL_IM(size, rsize, op0, op1)\
530 long res, flags, s1;\
534 asm volatile ("push %3\n\t"\
536 "imul" size " $" #op0 ", %" rsize "2, %" rsize "0\n\t" \
539 : "=r" (res), "=g" (flags)\
540 : "r" (s1), "1" (flags), "0" (res));\
541 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",\
542 "imul" size " im", (long)op0, (long)op1, res, flags & CC_MASK);\
550 #include "test-i386-muldiv.h"
553 #include "test-i386-muldiv.h"
557 test_imulb(0x1234561d, 4);
559 test_imulb(0x80, 0x80);
560 test_imulb(0x10, 0x10);
562 test_imulw(0, 0x1234001d, 45);
563 test_imulw(0, 23, -45);
564 test_imulw(0, 0x8000, 0x8000);
565 test_imulw(0, 0x100, 0x100);
567 test_imull(0, 0x1234001d, 45);
568 test_imull(0, 23, -45);
569 test_imull(0, 0x80000000, 0x80000000);
570 test_imull(0, 0x10000, 0x10000);
572 test_mulb(0x1234561d, 4);
574 test_mulb(0x80, 0x80);
575 test_mulb(0x10, 0x10);
577 test_mulw(0, 0x1234001d, 45);
578 test_mulw(0, 23, -45);
579 test_mulw(0, 0x8000, 0x8000);
580 test_mulw(0, 0x100, 0x100);
582 test_mull(0, 0x1234001d, 45);
583 test_mull(0, 23, -45);
584 test_mull(0, 0x80000000, 0x80000000);
585 test_mull(0, 0x10000, 0x10000);
587 test_imulw2(0x1234001d, 45);
588 test_imulw2(23, -45);
589 test_imulw2(0x8000, 0x8000);
590 test_imulw2(0x100, 0x100);
592 test_imull2(0x1234001d, 45);
593 test_imull2(23, -45);
594 test_imull2(0x80000000, 0x80000000);
595 test_imull2(0x10000, 0x10000);
597 TEST_IMUL_IM("w", "w", 45, 0x1234);
598 TEST_IMUL_IM("w", "w", -45, 23);
599 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
600 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
602 TEST_IMUL_IM("l", "k", 45, 0x1234);
603 TEST_IMUL_IM("l", "k", -45, 23);
604 TEST_IMUL_IM("l", "k", 0x8000, 0x80000000);
605 TEST_IMUL_IM("l", "k", 0x7fff, 0x1000);
607 test_idivb(0x12341678, 0x127e);
608 test_idivb(0x43210123, -5);
609 test_idivb(0x12340004, -1);
611 test_idivw(0, 0x12345678, 12347);
612 test_idivw(0, -23223, -45);
613 test_idivw(0, 0x12348000, -1);
614 test_idivw(0x12343, 0x12345678, 0x81238567);
616 test_idivl(0, 0x12345678, 12347);
617 test_idivl(0, -233223, -45);
618 test_idivl(0, 0x80000000, -1);
619 test_idivl(0x12343, 0x12345678, 0x81234567);
621 test_divb(0x12341678, 0x127e);
622 test_divb(0x43210123, -5);
623 test_divb(0x12340004, -1);
625 test_divw(0, 0x12345678, 12347);
626 test_divw(0, -23223, -45);
627 test_divw(0, 0x12348000, -1);
628 test_divw(0x12343, 0x12345678, 0x81238567);
630 test_divl(0, 0x12345678, 12347);
631 test_divl(0, -233223, -45);
632 test_divl(0, 0x80000000, -1);
633 test_divl(0x12343, 0x12345678, 0x81234567);
635 #if defined(__x86_64__)
636 test_imulq(0, 0x1234001d1234001d, 45);
637 test_imulq(0, 23, -45);
638 test_imulq(0, 0x8000000000000000, 0x8000000000000000);
639 test_imulq(0, 0x100000000, 0x100000000);
641 test_mulq(0, 0x1234001d1234001d, 45);
642 test_mulq(0, 23, -45);
643 test_mulq(0, 0x8000000000000000, 0x8000000000000000);
644 test_mulq(0, 0x100000000, 0x100000000);
646 test_imulq2(0x1234001d1234001d, 45);
647 test_imulq2(23, -45);
648 test_imulq2(0x8000000000000000, 0x8000000000000000);
649 test_imulq2(0x100000000, 0x100000000);
651 TEST_IMUL_IM("q", "", 45, 0x12341234);
652 TEST_IMUL_IM("q", "", -45, 23);
653 TEST_IMUL_IM("q", "", 0x8000, 0x8000000000000000);
654 TEST_IMUL_IM("q", "", 0x7fff, 0x10000000);
656 test_idivq(0, 0x12345678abcdef, 12347);
657 test_idivq(0, -233223, -45);
658 test_idivq(0, 0x8000000000000000, -1);
659 test_idivq(0x12343, 0x12345678, 0x81234567);
661 test_divq(0, 0x12345678abcdef, 12347);
662 test_divq(0, -233223, -45);
663 test_divq(0, 0x8000000000000000, -1);
664 test_divq(0x12343, 0x12345678, 0x81234567);
668 #define TEST_BSX(op, size, op0)\
670 long res, val, resz;\
673 "mov $0x12345678, %0\n"\
674 #op " %" size "2, %" size "0 ; setz %b1" \
675 : "=r" (res), "=q" (resz)\
677 printf("%-10s A=" FMTLX " R=" FMTLX " %ld\n", #op, val, res, resz);\
682 TEST_BSX(bsrw, "w", 0);
683 TEST_BSX(bsrw, "w", 0x12340128);
684 TEST_BSX(bsfw, "w", 0);
685 TEST_BSX(bsfw, "w", 0x12340128);
686 TEST_BSX(bsrl, "k", 0);
687 TEST_BSX(bsrl, "k", 0x00340128);
688 TEST_BSX(bsfl, "k", 0);
689 TEST_BSX(bsfl, "k", 0x00340128);
690 #if defined(__x86_64__)
691 TEST_BSX(bsrq, "", 0);
692 TEST_BSX(bsrq, "", 0x003401281234);
693 TEST_BSX(bsfq, "", 0);
694 TEST_BSX(bsfq, "", 0x003401281234);
698 /**********************************************/
700 void test_fops(double a, double b)
702 printf("a=%f b=%f a+b=%f\n", a, b, a + b);
703 printf("a=%f b=%f a-b=%f\n", a, b, a - b);
704 printf("a=%f b=%f a*b=%f\n", a, b, a * b);
705 printf("a=%f b=%f a/b=%f\n", a, b, a / b);
706 printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
707 printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
708 printf("a=%f sin(a)=%f\n", a, sin(a));
709 printf("a=%f cos(a)=%f\n", a, cos(a));
710 printf("a=%f tan(a)=%f\n", a, tan(a));
711 printf("a=%f log(a)=%f\n", a, log(a));
712 printf("a=%f exp(a)=%f\n", a, exp(a));
713 printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
714 /* just to test some op combining */
715 printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
716 printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
717 printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
721 void test_fcmp(double a, double b)
723 printf("(%f<%f)=%d\n",
725 printf("(%f<=%f)=%d\n",
727 printf("(%f==%f)=%d\n",
729 printf("(%f>%f)=%d\n",
731 printf("(%f<=%f)=%d\n",
735 /* test f(u)comi instruction */
741 printf("fcomi(%f %f)=%08lx\n", a, b, eflags & (CC_Z | CC_P | CC_C));
745 void test_fcvt(double a)
758 printf("(float)%f = %f\n", a, fa);
759 printf("(long double)%f = %Lf\n", a, la);
760 printf("a=" FMT64X "\n", *(uint64_t *)&a);
761 printf("la=" FMT64X " %04x\n", *(uint64_t *)&la,
762 *(unsigned short *)((char *)(&la) + 8));
764 /* test all roundings */
765 asm volatile ("fstcw %0" : "=m" (fpuc));
767 asm volatile ("fldcw %0" : : "m" ((fpuc & ~0x0c00) | (i << 10)));
768 asm volatile ("fist %0" : "=m" (wa) : "t" (a));
769 asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
770 asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
771 asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
772 asm volatile ("fldcw %0" : : "m" (fpuc));
773 printf("(short)a = %d\n", wa);
774 printf("(int)a = %d\n", ia);
775 printf("(int64_t)a = " FMT64X "\n", lla);
776 printf("rint(a) = %f\n", ra);
781 asm("fld" #N : "=t" (a)); \
782 printf("fld" #N "= %f\n", a);
784 void test_fconst(void)
796 void test_fbcd(double a)
798 unsigned short bcd[5];
801 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
802 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
803 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
804 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
807 #define TEST_ENV(env, save, restore)\
809 memset((env), 0xaa, sizeof(*(env)));\
811 asm volatile ("fldl %0" : : "m" (dtab[i]));\
812 asm volatile (save " %0\n" : : "m" (*(env)));\
813 asm volatile (restore " %0\n": : "m" (*(env)));\
815 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
817 printf("res[%d]=%f\n", i, rtab[i]);\
818 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
820 (env)->fpus & 0xff00,\
826 struct __attribute__((packed)) {
834 long double fpregs[8];
836 struct __attribute__((packed)) {
841 long double fpregs[8];
850 TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
851 TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
852 TEST_ENV(&float_env32, "fnstenv", "fldenv");
853 TEST_ENV(&float_env32, "fnsave", "frstor");
857 asm volatile ("fldl %0" : : "m" (dtab[i]));
858 asm volatile("ffree %st(2)");
859 asm volatile ("fnstenv %0\n" : : "m" (float_env32));
860 asm volatile ("fninit");
861 printf("fptag=%04x\n", float_env32.fptag);
865 #define TEST_FCMOV(a, b, eflags, CC)\
870 "fcmov" CC " %2, %0\n"\
872 : "0" (a), "u" (b), "g" (eflags));\
873 printf("fcmov%s eflags=0x%04lx-> %f\n", \
874 CC, (long)eflags, res);\
877 void test_fcmov(void)
884 for(i = 0; i < 4; i++) {
890 TEST_FCMOV(a, b, eflags, "b");
891 TEST_FCMOV(a, b, eflags, "e");
892 TEST_FCMOV(a, b, eflags, "be");
893 TEST_FCMOV(a, b, eflags, "nb");
894 TEST_FCMOV(a, b, eflags, "ne");
895 TEST_FCMOV(a, b, eflags, "nbe");
897 TEST_FCMOV(a, b, 0, "u");
898 TEST_FCMOV(a, b, CC_P, "u");
899 TEST_FCMOV(a, b, 0, "nu");
900 TEST_FCMOV(a, b, CC_P, "nu");
903 void test_floats(void)
917 test_fbcd(1234567890123456);
918 test_fbcd(-123451234567890);
925 /**********************************************/
926 #if !defined(__x86_64__)
928 #define TEST_BCD(op, op0, cc_in, cc_mask)\
938 : "=a" (res), "=g" (flags)\
939 : "0" (res), "1" (flags));\
940 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
941 #op, op0, res, cc_in, flags & cc_mask);\
946 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
947 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
948 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
949 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
950 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
951 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
952 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
953 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
954 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
955 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
956 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
957 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
958 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
960 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
961 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
962 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
963 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
964 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
965 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
966 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
967 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
968 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
969 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
970 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
971 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
972 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
974 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
975 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
976 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
977 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
978 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
979 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
980 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
981 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
983 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
984 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
985 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
986 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
987 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
988 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
989 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
990 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
992 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
993 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
997 #define TEST_XCHG(op, size, opconst)\
1000 op0 = i2l(0x12345678);\
1001 op1 = i2l(0xfbca7654);\
1002 asm(#op " %" size "0, %" size "1" \
1003 : "=q" (op0), opconst (op1) \
1004 : "0" (op0), "1" (op1));\
1005 printf("%-10s A=" FMTLX " B=" FMTLX "\n",\
1009 #define TEST_CMPXCHG(op, size, opconst, eax)\
1011 long op0, op1, op2;\
1012 op0 = i2l(0x12345678);\
1013 op1 = i2l(0xfbca7654);\
1015 asm(#op " %" size "0, %" size "1" \
1016 : "=q" (op0), opconst (op1) \
1017 : "0" (op0), "1" (op1), "a" (op2));\
1018 printf("%-10s EAX=" FMTLX " A=" FMTLX " C=" FMTLX "\n",\
1019 #op, op2, op0, op1);\
1022 void test_xchg(void)
1024 #if defined(__x86_64__)
1025 TEST_XCHG(xchgq, "", "=q");
1027 TEST_XCHG(xchgl, "k", "=q");
1028 TEST_XCHG(xchgw, "w", "=q");
1029 TEST_XCHG(xchgb, "b", "=q");
1031 #if defined(__x86_64__)
1032 TEST_XCHG(xchgq, "", "=m");
1034 TEST_XCHG(xchgl, "k", "=m");
1035 TEST_XCHG(xchgw, "w", "=m");
1036 TEST_XCHG(xchgb, "b", "=m");
1038 #if defined(__x86_64__)
1039 TEST_XCHG(xaddq, "", "=q");
1041 TEST_XCHG(xaddl, "k", "=q");
1042 TEST_XCHG(xaddw, "w", "=q");
1043 TEST_XCHG(xaddb, "b", "=q");
1048 asm("xaddl %1, %0" : "=r" (res) : "0" (res));
1049 printf("xaddl same res=%08x\n", res);
1052 #if defined(__x86_64__)
1053 TEST_XCHG(xaddq, "", "=m");
1055 TEST_XCHG(xaddl, "k", "=m");
1056 TEST_XCHG(xaddw, "w", "=m");
1057 TEST_XCHG(xaddb, "b", "=m");
1059 #if defined(__x86_64__)
1060 TEST_CMPXCHG(cmpxchgq, "", "=q", 0xfbca7654);
1062 TEST_CMPXCHG(cmpxchgl, "k", "=q", 0xfbca7654);
1063 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
1064 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
1066 #if defined(__x86_64__)
1067 TEST_CMPXCHG(cmpxchgq, "", "=q", 0xfffefdfc);
1069 TEST_CMPXCHG(cmpxchgl, "k", "=q", 0xfffefdfc);
1070 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
1071 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
1073 #if defined(__x86_64__)
1074 TEST_CMPXCHG(cmpxchgq, "", "=m", 0xfbca7654);
1076 TEST_CMPXCHG(cmpxchgl, "k", "=m", 0xfbca7654);
1077 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
1078 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
1080 #if defined(__x86_64__)
1081 TEST_CMPXCHG(cmpxchgq, "", "=m", 0xfffefdfc);
1083 TEST_CMPXCHG(cmpxchgl, "k", "=m", 0xfffefdfc);
1084 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
1085 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
1088 uint64_t op0, op1, op2;
1091 for(i = 0; i < 2; i++) {
1092 op0 = 0x123456789abcd;
1094 op1 = 0xfbca765423456;
1097 op2 = 0x6532432432434;
1098 asm("cmpxchg8b %1\n"
1101 : "=A" (op0), "=m" (op1), "=g" (eflags)
1102 : "0" (op0), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
1103 printf("cmpxchg8b: op0=" FMT64X " op1=" FMT64X " CC=%02lx\n",
1104 op0, op1, eflags & CC_Z);
1110 /**********************************************/
1111 /* segmentation tests */
1113 #include <asm/ldt.h>
1114 #include <linux/unistd.h>
1115 #include <linux/version.h>
1117 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
1119 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
1120 #define modify_ldt_ldt_s user_desc
1123 #define MK_SEL(n) (((n) << 3) | 7)
1125 uint8_t seg_data1[4096];
1126 uint8_t seg_data2[4096];
1128 #define TEST_LR(op, size, seg, mask)\
1132 asm (op " %" size "2, %" size "0\n" \
1137 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
1138 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
1141 /* NOTE: we use Linux modify_ldt syscall */
1142 void test_segs(void)
1144 struct modify_ldt_ldt_s ldt;
1145 long long ldt_table[3];
1151 } __attribute__((packed)) segoff;
1153 ldt.entry_number = 1;
1154 ldt.base_addr = (unsigned long)&seg_data1;
1155 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1157 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1158 ldt.read_exec_only = 0;
1159 ldt.limit_in_pages = 1;
1160 ldt.seg_not_present = 0;
1162 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1164 ldt.entry_number = 2;
1165 ldt.base_addr = (unsigned long)&seg_data2;
1166 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
1168 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1169 ldt.read_exec_only = 0;
1170 ldt.limit_in_pages = 1;
1171 ldt.seg_not_present = 0;
1173 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1175 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
1180 printf("%d: %016Lx\n", i, ldt_table[i]);
1183 /* do some tests with fs or gs */
1184 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1186 seg_data1[1] = 0xaa;
1187 seg_data2[1] = 0x55;
1189 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
1190 printf("FS[1] = %02x\n", res);
1192 asm volatile ("pushl %%gs\n"
1194 "gs movzbl 0x1, %0\n"
1198 printf("GS[1] = %02x\n", res);
1200 /* tests with ds/ss (implicit segment case) */
1202 asm volatile ("pushl %%ebp\n\t"
1205 "movl %3, %%ebp\n\t"
1206 "movzbl 0x1, %0\n\t"
1207 "movzbl (%%ebp), %1\n\t"
1210 : "=r" (res), "=r" (res2)
1211 : "r" (MK_SEL(1)), "r" (&tmp));
1212 printf("DS[1] = %02x\n", res);
1213 printf("SS[tmp] = %02x\n", res2);
1215 segoff.seg = MK_SEL(2);
1216 segoff.offset = 0xabcdef12;
1217 asm volatile("lfs %2, %0\n\t"
1219 : "=r" (res), "=g" (res2)
1221 printf("FS:reg = %04x:%08x\n", res2, res);
1223 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1224 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1225 TEST_LR("lslw", "w", MK_SEL(2), 0);
1226 TEST_LR("lsll", "", MK_SEL(2), 0);
1228 TEST_LR("larw", "w", 0xfff8, 0);
1229 TEST_LR("larl", "", 0xfff8, 0);
1230 TEST_LR("lslw", "w", 0xfff8, 0);
1231 TEST_LR("lsll", "", 0xfff8, 0);
1234 /* 16 bit code test */
1235 extern char code16_start, code16_end;
1236 extern char code16_func1;
1237 extern char code16_func2;
1238 extern char code16_func3;
1240 void test_code16(void)
1242 struct modify_ldt_ldt_s ldt;
1245 /* build a code segment */
1246 ldt.entry_number = 1;
1247 ldt.base_addr = (unsigned long)&code16_start;
1248 ldt.limit = &code16_end - &code16_start;
1250 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
1251 ldt.read_exec_only = 0;
1252 ldt.limit_in_pages = 0;
1253 ldt.seg_not_present = 0;
1255 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1257 /* call the first function */
1258 asm volatile ("lcall %1, %2"
1260 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
1261 printf("func1() = 0x%08x\n", res);
1262 asm volatile ("lcall %2, %3"
1263 : "=a" (res), "=c" (res2)
1264 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
1265 printf("func2() = 0x%08x spdec=%d\n", res, res2);
1266 asm volatile ("lcall %1, %2"
1268 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1269 printf("func3() = 0x%08x\n", res);
1273 extern char func_lret32;
1274 extern char func_iret32;
1276 void test_misc(void)
1281 for(i=0;i<256;i++) table[i] = 256 - i;
1283 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
1284 printf("xlat: EAX=" FMTLX "\n", res);
1286 #if !defined(__x86_64__)
1287 asm volatile ("push %%cs ; call %1"
1289 : "m" (func_lret32): "memory", "cc");
1290 printf("func_lret32=" FMTLX "\n", res);
1292 asm volatile ("pushf ; push %%cs ; call %1"
1294 : "m" (func_iret32): "memory", "cc");
1295 printf("func_iret32=" FMTLX "\n", res);
1298 #if defined(__x86_64__)
1299 /* specific popl test */
1300 asm volatile ("push $12345432 ; push $0x9abcdef ; pop (%%rsp) ; pop %0"
1302 printf("popl esp=" FMTLX "\n", res);
1304 /* specific popl test */
1305 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1307 printf("popl esp=" FMTLX "\n", res);
1309 /* specific popw test */
1310 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1312 printf("popw esp=" FMTLX "\n", res);
1316 uint8_t str_buffer[4096];
1318 #define TEST_STRING1(OP, size, DF, REP)\
1320 long esi, edi, eax, ecx, eflags;\
1322 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1323 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1324 eax = i2l(0x12345678);\
1327 asm volatile ("push $0\n\t"\
1330 REP #OP size "\n\t"\
1334 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1335 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1336 printf("%-10s ESI=" FMTLX " EDI=" FMTLX " EAX=" FMTLX " ECX=" FMTLX " EFL=%04x\n",\
1337 REP #OP size, esi, edi, eax, ecx,\
1338 (int)(eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)));\
1341 #define TEST_STRING(OP, REP)\
1342 TEST_STRING1(OP, "b", "", REP);\
1343 TEST_STRING1(OP, "w", "", REP);\
1344 TEST_STRING1(OP, "l", "", REP);\
1345 X86_64_ONLY(TEST_STRING1(OP, "q", "", REP));\
1346 TEST_STRING1(OP, "b", "std", REP);\
1347 TEST_STRING1(OP, "w", "std", REP);\
1348 TEST_STRING1(OP, "l", "std", REP);\
1349 X86_64_ONLY(TEST_STRING1(OP, "q", "std", REP))
1351 void test_string(void)
1354 for(i = 0;i < sizeof(str_buffer); i++)
1355 str_buffer[i] = i + 0x56;
1356 TEST_STRING(stos, "");
1357 TEST_STRING(stos, "rep ");
1358 TEST_STRING(lods, ""); /* to verify stos */
1359 TEST_STRING(lods, "rep ");
1360 TEST_STRING(movs, "");
1361 TEST_STRING(movs, "rep ");
1362 TEST_STRING(lods, ""); /* to verify stos */
1364 /* XXX: better tests */
1365 TEST_STRING(scas, "");
1366 TEST_STRING(scas, "repz ");
1367 TEST_STRING(scas, "repnz ");
1368 TEST_STRING(cmps, "");
1369 TEST_STRING(cmps, "repz ");
1370 TEST_STRING(cmps, "repnz ");
1376 static inline void set_bit(uint8_t *a, unsigned int bit)
1378 a[bit / 8] |= (1 << (bit % 8));
1381 static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1383 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1386 static inline void pushw(struct vm86_regs *r, int val)
1388 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1389 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1392 #undef __syscall_return
1393 #define __syscall_return(type, res) \
1395 return (type) (res); \
1398 _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1400 extern char vm86_code_start;
1401 extern char vm86_code_end;
1403 #define VM86_CODE_CS 0x100
1404 #define VM86_CODE_IP 0x100
1406 void test_vm86(void)
1408 struct vm86plus_struct ctx;
1409 struct vm86_regs *r;
1413 vm86_mem = mmap((void *)0x00000000, 0x110000,
1414 PROT_WRITE | PROT_READ | PROT_EXEC,
1415 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1416 if (vm86_mem == MAP_FAILED) {
1417 printf("ERROR: could not map vm86 memory");
1420 memset(&ctx, 0, sizeof(ctx));
1422 /* init basic registers */
1424 r->eip = VM86_CODE_IP;
1433 r->eflags = VIF_MASK;
1435 /* move code to proper address. We use the same layout as a .com
1437 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1438 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1440 /* mark int 0x21 as being emulated */
1441 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1444 ret = vm86(VM86_ENTER, &ctx);
1445 switch(VM86_TYPE(ret)) {
1450 int_num = VM86_ARG(ret);
1451 if (int_num != 0x21)
1453 ah = (r->eax >> 8) & 0xff;
1455 case 0x00: /* exit */
1457 case 0x02: /* write char */
1463 case 0x09: /* write string */
1466 ptr = seg_to_linear(r->ds, r->edx);
1473 r->eax = (r->eax & ~0xff) | '$';
1476 case 0xff: /* extension: write eflags number in edx */
1478 #ifndef LINUX_VM86_IOPL_FIX
1481 printf("%08x\n", v);
1485 printf("unsupported int 0x%02x\n", int_num);
1491 /* a signal came, we just ignore that */
1496 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1501 printf("VM86 end\n");
1502 munmap(vm86_mem, 0x110000);
1506 /* exception tests */
1507 #if defined(__i386__) && !defined(REG_EAX)
1518 #define REG_TRAPNO TRAPNO
1522 #if defined(__x86_64__)
1523 #define REG_EIP REG_RIP
1530 void sig_handler(int sig, siginfo_t *info, void *puc)
1532 struct ucontext *uc = puc;
1534 printf("si_signo=%d si_errno=%d si_code=%d",
1535 info->si_signo, info->si_errno, info->si_code);
1536 printf(" si_addr=0x%08lx",
1537 (unsigned long)info->si_addr);
1540 printf("trapno=" FMTLX " err=" FMTLX,
1541 (long)uc->uc_mcontext.gregs[REG_TRAPNO],
1542 (long)uc->uc_mcontext.gregs[REG_ERR]);
1543 printf(" EIP=" FMTLX, (long)uc->uc_mcontext.gregs[REG_EIP]);
1545 longjmp(jmp_env, 1);
1548 void test_exceptions(void)
1550 struct sigaction act;
1553 act.sa_sigaction = sig_handler;
1554 sigemptyset(&act.sa_mask);
1555 act.sa_flags = SA_SIGINFO | SA_NODEFER;
1556 sigaction(SIGFPE, &act, NULL);
1557 sigaction(SIGILL, &act, NULL);
1558 sigaction(SIGSEGV, &act, NULL);
1559 sigaction(SIGBUS, &act, NULL);
1560 sigaction(SIGTRAP, &act, NULL);
1562 /* test division by zero reporting */
1563 printf("DIVZ exception:\n");
1564 if (setjmp(jmp_env) == 0) {
1565 /* now divide by zero */
1570 #if !defined(__x86_64__)
1571 printf("BOUND exception:\n");
1572 if (setjmp(jmp_env) == 0) {
1573 /* bound exception */
1576 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab[0]));
1581 printf("segment exceptions:\n");
1582 if (setjmp(jmp_env) == 0) {
1583 /* load an invalid segment */
1584 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1586 if (setjmp(jmp_env) == 0) {
1587 /* null data segment is valid */
1588 asm volatile ("movl %0, %%fs" : : "r" (3));
1589 /* null stack segment */
1590 asm volatile ("movl %0, %%ss" : : "r" (3));
1594 struct modify_ldt_ldt_s ldt;
1595 ldt.entry_number = 1;
1596 ldt.base_addr = (unsigned long)&seg_data1;
1597 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1599 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1600 ldt.read_exec_only = 0;
1601 ldt.limit_in_pages = 1;
1602 ldt.seg_not_present = 1;
1604 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1606 if (setjmp(jmp_env) == 0) {
1607 /* segment not present */
1608 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1613 /* test SEGV reporting */
1614 printf("PF exception:\n");
1615 if (setjmp(jmp_env) == 0) {
1617 /* we add a nop to test a weird PC retrieval case */
1618 asm volatile ("nop");
1619 /* now store in an invalid address */
1620 *(char *)0x1234 = 1;
1623 /* test SEGV reporting */
1624 printf("PF exception:\n");
1625 if (setjmp(jmp_env) == 0) {
1627 /* read from an invalid address */
1628 v1 = *(char *)0x1234;
1631 /* test illegal instruction reporting */
1632 printf("UD2 exception:\n");
1633 if (setjmp(jmp_env) == 0) {
1634 /* now execute an invalid instruction */
1635 asm volatile("ud2");
1637 printf("lock nop exception:\n");
1638 if (setjmp(jmp_env) == 0) {
1639 /* now execute an invalid instruction */
1640 asm volatile("lock nop");
1643 printf("INT exception:\n");
1644 if (setjmp(jmp_env) == 0) {
1645 asm volatile ("int $0xfd");
1647 if (setjmp(jmp_env) == 0) {
1648 asm volatile ("int $0x01");
1650 if (setjmp(jmp_env) == 0) {
1651 asm volatile (".byte 0xcd, 0x03");
1653 if (setjmp(jmp_env) == 0) {
1654 asm volatile ("int $0x04");
1656 if (setjmp(jmp_env) == 0) {
1657 asm volatile ("int $0x05");
1660 printf("INT3 exception:\n");
1661 if (setjmp(jmp_env) == 0) {
1662 asm volatile ("int3");
1665 printf("CLI exception:\n");
1666 if (setjmp(jmp_env) == 0) {
1667 asm volatile ("cli");
1670 printf("STI exception:\n");
1671 if (setjmp(jmp_env) == 0) {
1672 asm volatile ("cli");
1675 #if !defined(__x86_64__)
1676 printf("INTO exception:\n");
1677 if (setjmp(jmp_env) == 0) {
1678 /* overflow exception */
1679 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1683 printf("OUTB exception:\n");
1684 if (setjmp(jmp_env) == 0) {
1685 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1688 printf("INB exception:\n");
1689 if (setjmp(jmp_env) == 0) {
1690 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1693 printf("REP OUTSB exception:\n");
1694 if (setjmp(jmp_env) == 0) {
1695 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1698 printf("REP INSB exception:\n");
1699 if (setjmp(jmp_env) == 0) {
1700 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1703 printf("HLT exception:\n");
1704 if (setjmp(jmp_env) == 0) {
1705 asm volatile ("hlt");
1708 printf("single step exception:\n");
1710 if (setjmp(jmp_env) == 0) {
1711 asm volatile ("pushf\n"
1712 "orl $0x00100, (%%esp)\n"
1714 "movl $0xabcd, %0\n"
1715 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1717 printf("val=0x%x\n", val);
1720 #if !defined(__x86_64__)
1721 /* specific precise single step test */
1722 void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1724 struct ucontext *uc = puc;
1725 printf("EIP=" FMTLX "\n", (long)uc->uc_mcontext.gregs[REG_EIP]);
1728 const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1729 uint8_t sstep_buf2[4];
1731 void test_single_step(void)
1733 struct sigaction act;
1738 act.sa_sigaction = sig_trap_handler;
1739 sigemptyset(&act.sa_mask);
1740 act.sa_flags = SA_SIGINFO;
1741 sigaction(SIGTRAP, &act, NULL);
1742 asm volatile ("pushf\n"
1743 "orl $0x00100, (%%esp)\n"
1745 "movl $0xabcd, %0\n"
1754 /* movsb: the single step should stop at each movsb iteration */
1755 "movl $sstep_buf1, %%esi\n"
1756 "movl $sstep_buf2, %%edi\n"
1764 /* cmpsb: the single step should stop at each cmpsb iteration */
1765 "movl $sstep_buf1, %%esi\n"
1766 "movl $sstep_buf2, %%edi\n"
1772 /* getpid() syscall: single step should skip one
1778 /* when modifying SS, trace is not done on the next
1780 "movl %%ss, %%ecx\n"
1781 "movl %%ecx, %%ss\n"
1784 "movl %%ecx, %%ss\n"
1795 "andl $~0x00100, (%%esp)\n"
1799 : "cc", "memory", "eax", "ecx", "esi", "edi");
1800 printf("val=%d\n", val);
1801 for(i = 0; i < 4; i++)
1802 printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1805 /* self modifying code test */
1807 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1812 "movl 4(%esp), %eax\n"
1813 "movl %eax, smc_patch_addr2 + 1\n"
1822 "smc_patch_addr2:\n"
1826 typedef int FuncType(void);
1827 extern int smc_code2(int);
1828 void test_self_modifying_code(void)
1832 printf("self modifying code:\n");
1833 printf("func1 = 0x%x\n", ((FuncType *)code)());
1834 for(i = 2; i <= 4; i++) {
1836 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1839 /* more difficult test : the modified code is just after the
1840 modifying instruction. It is forbidden in Intel specs, but it
1841 is used by old DOS programs */
1842 for(i = 2; i <= 4; i++) {
1843 printf("smc_code2(%d) = %d\n", i, smc_code2(i));
1848 long enter_stack[4096];
1850 #if defined(__x86_64__)
1858 #define TEST_ENTER(size, stack_type, level)\
1860 long esp_save, esp_val, ebp_val, ebp_save, i;\
1861 stack_type *ptr, *stack_end, *stack_ptr;\
1862 memset(enter_stack, 0, sizeof(enter_stack));\
1863 stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
1864 ebp_val = (long)stack_ptr;\
1867 esp_val = (long)stack_ptr;\
1868 asm("mov " RSP ", %[esp_save]\n"\
1869 "mov " RBP ", %[ebp_save]\n"\
1870 "mov %[esp_val], " RSP "\n"\
1871 "mov %[ebp_val], " RBP "\n"\
1872 "enter" size " $8, $" #level "\n"\
1873 "mov " RSP ", %[esp_val]\n"\
1874 "mov " RBP ", %[ebp_val]\n"\
1875 "mov %[esp_save], " RSP "\n"\
1876 "mov %[ebp_save], " RBP "\n"\
1877 : [esp_save] "=r" (esp_save),\
1878 [ebp_save] "=r" (ebp_save),\
1879 [esp_val] "=r" (esp_val),\
1880 [ebp_val] "=r" (ebp_val)\
1881 : "[esp_val]" (esp_val),\
1882 "[ebp_val]" (ebp_val));\
1883 printf("level=%d:\n", level);\
1884 printf("esp_val=" FMTLX "\n", esp_val - (long)stack_end);\
1885 printf("ebp_val=" FMTLX "\n", ebp_val - (long)stack_end);\
1886 for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
1887 printf(FMTLX "\n", (long)ptr[0]);\
1890 static void test_enter(void)
1892 #if defined(__x86_64__)
1893 TEST_ENTER("q", uint64_t, 0);
1894 TEST_ENTER("q", uint64_t, 1);
1895 TEST_ENTER("q", uint64_t, 2);
1896 TEST_ENTER("q", uint64_t, 31);
1898 TEST_ENTER("l", uint32_t, 0);
1899 TEST_ENTER("l", uint32_t, 1);
1900 TEST_ENTER("l", uint32_t, 2);
1901 TEST_ENTER("l", uint32_t, 31);
1904 TEST_ENTER("w", uint16_t, 0);
1905 TEST_ENTER("w", uint16_t, 1);
1906 TEST_ENTER("w", uint16_t, 2);
1907 TEST_ENTER("w", uint16_t, 31);
1912 typedef int __m64 __attribute__ ((__mode__ (__V2SI__)));
1913 typedef int __m128 __attribute__ ((__mode__(__V4SF__)));
1923 static uint64_t __attribute__((aligned(16))) test_values[4][2] = {
1924 { 0x456723c698694873, 0xdc515cff944a58ec },
1925 { 0x1f297ccd58bad7ab, 0x41f21efba9e3e146 },
1926 { 0x007c62c2085427f8, 0x231be9e8cde7438d },
1927 { 0x0f76255a085427f8, 0xc233e9e8c4c9439a },
1932 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
1933 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
1940 #define SSE_OP2(op)\
1944 a.q[0] = test_values[2*i][0];\
1945 a.q[1] = test_values[2*i][1];\
1946 b.q[0] = test_values[2*i+1][0];\
1947 b.q[1] = test_values[2*i+1][1];\
1952 #define MMX_OP2(op)\
1956 a.q[0] = test_values[2*i][0];\
1957 b.q[0] = test_values[2*i+1][0];\
1958 asm volatile (#op " %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0]));\
1959 printf("%-9s: a=" FMT64X " b=" FMT64X " r=" FMT64X "\n",\
1968 #define SHUF_OP(op, ib)\
1970 a.q[0] = test_values[0][0];\
1971 a.q[1] = test_values[0][1];\
1972 b.q[0] = test_values[1][0];\
1973 b.q[1] = test_values[1][1];\
1974 asm volatile (#op " $" #ib ", %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
1975 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
1983 #define PSHUF_OP(op, ib)\
1987 a.q[0] = test_values[2*i][0];\
1988 a.q[1] = test_values[2*i][1];\
1989 asm volatile (#op " $" #ib ", %1, %0" : "=x" (r.dq) : "x" (a.dq));\
1990 printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
1998 #define SHIFT_IM(op, ib)\
2002 a.q[0] = test_values[2*i][0];\
2003 a.q[1] = test_values[2*i][1];\
2004 asm volatile (#op " $" #ib ", %0" : "=x" (r.dq) : "0" (a.dq));\
2005 printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
2013 #define SHIFT_OP(op, ib)\
2018 a.q[0] = test_values[2*i][0];\
2019 a.q[1] = test_values[2*i][1];\
2022 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
2023 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
2035 a.q[0] = test_values[2*i][0];\
2036 a.q[1] = test_values[2*i][1];\
2037 asm volatile (#op " %1, %0" : "=r" (reg) : "x" (a.dq));\
2038 printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
2045 #define SSE_OPS(a) \
2049 #define SSE_OPD(a) \
2053 #define SSE_COMI(op, field)\
2055 unsigned int eflags;\
2059 asm volatile (#op " %2, %1\n"\
2063 : "x" (a.dq), "x" (b.dq));\
2064 printf("%-9s: a=%f b=%f cc=%04x\n",\
2066 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
2069 void test_sse_comi(double a1, double b1)
2071 SSE_COMI(ucomiss, s);
2072 SSE_COMI(ucomisd, d);
2073 SSE_COMI(comiss, s);
2074 SSE_COMI(comisd, d);
2077 #define CVT_OP_XMM(op)\
2079 asm volatile (#op " %1, %0" : "=x" (r.dq) : "x" (a.dq));\
2080 printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
2086 #define CVT_OP_XMM2MMX(op)\
2088 asm volatile (#op " %1, %0" : "=y" (r.q[0]) : "x" (a.dq));\
2089 printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "\n",\
2095 #define CVT_OP_MMX2XMM(op)\
2097 asm volatile (#op " %1, %0" : "=x" (r.dq) : "y" (a.q[0]));\
2098 printf("%-9s: a=" FMT64X " r=" FMT64X "" FMT64X "\n",\
2104 #define CVT_OP_REG2XMM(op)\
2106 asm volatile (#op " %1, %0" : "=x" (r.dq) : "r" (a.l[0]));\
2107 printf("%-9s: a=%08x r=" FMT64X "" FMT64X "\n",\
2113 #define CVT_OP_XMM2REG(op)\
2115 asm volatile (#op " %1, %0" : "=r" (r.l[0]) : "x" (a.dq));\
2116 printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
2134 uint32_t mxcsr_mask;
2135 uint8_t fpregs1[8 * 16];
2136 uint8_t xmm_regs[8 * 16];
2137 uint8_t dummy2[224];
2140 static struct fpxstate fpx_state __attribute__((aligned(16)));
2141 static struct fpxstate fpx_state2 __attribute__((aligned(16)));
2143 void test_fxsave(void)
2145 struct fpxstate *fp = &fpx_state;
2146 struct fpxstate *fp2 = &fpx_state2;
2149 a.q[0] = test_values[0][0];
2150 a.q[1] = test_values[0][1];
2151 b.q[0] = test_values[1][0];
2152 b.q[1] = test_values[1][1];
2154 asm("movdqa %2, %%xmm0\n"
2155 "movdqa %3, %%xmm7\n"
2156 #if defined(__x86_64__)
2157 "movdqa %2, %%xmm15\n"
2166 : "=m" (*(uint32_t *)fp2), "=m" (*(uint32_t *)fp)
2167 : "m" (a), "m" (b));
2168 printf("fpuc=%04x\n", fp->fpuc);
2169 printf("fpus=%04x\n", fp->fpus);
2170 printf("fptag=%04x\n", fp->fptag);
2171 for(i = 0; i < 3; i++) {
2172 printf("ST%d: " FMT64X " %04x\n",
2174 *(uint64_t *)&fp->fpregs1[i * 16],
2175 *(uint16_t *)&fp->fpregs1[i * 16 + 8]);
2177 printf("mxcsr=%08x\n", fp->mxcsr & 0x1f80);
2178 #if defined(__x86_64__)
2183 for(i = 0; i < nb_xmm; i++) {
2184 printf("xmm%d: " FMT64X "" FMT64X "\n",
2186 *(uint64_t *)&fp->xmm_regs[i * 16],
2187 *(uint64_t *)&fp->xmm_regs[i * 16 + 8]);
2247 asm volatile ("pinsrw $1, %1, %0" : "=y" (r.q[0]) : "r" (0x12345678));
2248 printf("%-9s: r=" FMT64X "\n", "pinsrw", r.q[0]);
2250 asm volatile ("pinsrw $5, %1, %0" : "=x" (r.dq) : "r" (0x12345678));
2251 printf("%-9s: r=" FMT64X "" FMT64X "\n", "pinsrw", r.q[1], r.q[0]);
2253 a.q[0] = test_values[0][0];
2254 a.q[1] = test_values[0][1];
2255 asm volatile ("pextrw $1, %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
2256 printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
2258 asm volatile ("pextrw $5, %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
2259 printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
2261 asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
2262 printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
2264 asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
2265 printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
2271 a.q[0] = test_values[0][0];
2272 a.q[1] = test_values[0][1];
2273 b.q[0] = test_values[1][0];
2274 b.q[1] = test_values[1][1];
2275 asm volatile("maskmovq %1, %0" :
2276 : "y" (a.q[0]), "y" (b.q[0]), "D" (&r)
2278 printf("%-9s: r=" FMT64X " a=" FMT64X " b=" FMT64X "\n",
2283 asm volatile("maskmovdqu %1, %0" :
2284 : "x" (a.dq), "x" (b.dq), "D" (&r)
2286 printf("%-9s: r=" FMT64X "" FMT64X " a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X "\n",
2293 asm volatile ("emms");
2295 SSE_OP2(punpcklqdq);
2296 SSE_OP2(punpckhqdq);
2311 SHUF_OP(shufps, 0x78);
2312 SHUF_OP(shufpd, 0x02);
2314 PSHUF_OP(pshufd, 0x78);
2315 PSHUF_OP(pshuflw, 0x78);
2316 PSHUF_OP(pshufhw, 0x78);
2319 SHIFT_OP(psrlw, 16);
2321 SHIFT_OP(psraw, 16);
2323 SHIFT_OP(psllw, 16);
2326 SHIFT_OP(psrld, 32);
2328 SHIFT_OP(psrad, 32);
2330 SHIFT_OP(pslld, 32);
2333 SHIFT_OP(psrlq, 32);
2335 SHIFT_OP(psllq, 32);
2337 SHIFT_IM(psrldq, 16);
2338 SHIFT_IM(psrldq, 7);
2339 SHIFT_IM(pslldq, 16);
2340 SHIFT_IM(pslldq, 7);
2345 /* FPU specific ops */
2349 asm volatile("stmxcsr %0" : "=m" (mxcsr));
2350 printf("mxcsr=%08x\n", mxcsr & 0x1f80);
2351 asm volatile("ldmxcsr %0" : : "m" (mxcsr));
2354 test_sse_comi(2, -1);
2355 test_sse_comi(2, 2);
2356 test_sse_comi(2, 3);
2402 /* float to float/int */
2407 CVT_OP_XMM(cvtps2pd);
2408 CVT_OP_XMM(cvtss2sd);
2409 CVT_OP_XMM2MMX(cvtps2pi);
2410 CVT_OP_XMM2MMX(cvttps2pi);
2411 CVT_OP_XMM2REG(cvtss2si);
2412 CVT_OP_XMM2REG(cvttss2si);
2413 CVT_OP_XMM(cvtps2dq);
2414 CVT_OP_XMM(cvttps2dq);
2418 CVT_OP_XMM(cvtpd2ps);
2419 CVT_OP_XMM(cvtsd2ss);
2420 CVT_OP_XMM2MMX(cvtpd2pi);
2421 CVT_OP_XMM2MMX(cvttpd2pi);
2422 CVT_OP_XMM2REG(cvtsd2si);
2423 CVT_OP_XMM2REG(cvttsd2si);
2424 CVT_OP_XMM(cvtpd2dq);
2425 CVT_OP_XMM(cvttpd2dq);
2432 CVT_OP_MMX2XMM(cvtpi2ps);
2433 CVT_OP_MMX2XMM(cvtpi2pd);
2434 CVT_OP_REG2XMM(cvtsi2ss);
2435 CVT_OP_REG2XMM(cvtsi2sd);
2436 CVT_OP_XMM(cvtdq2ps);
2437 CVT_OP_XMM(cvtdq2pd);
2439 /* XXX: test PNI insns */
2443 asm volatile ("emms");
2448 extern void *__start_initcall;
2449 extern void *__stop_initcall;
2452 int main(int argc, char **argv)
2457 ptr = &__start_initcall;
2458 while (ptr != &__stop_initcall) {
2466 #if !defined(__x86_64__)
2481 #if !defined(__x86_64__)
2482 test_self_modifying_code();