]>
git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
09c6626330ed9a5523a7e32e7b44f39e6da8c5a9
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>
35 //#define LINUX_VM86_IOPL_FIX
37 #define xglue(x, y) x ## y
38 #define glue(x, y) xglue(x, y)
39 #define stringify(s) tostring(s)
40 #define tostring(s) #s
49 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
51 static void *call_start __init_call
= NULL
;
53 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
56 #include "test-i386.h"
59 #include "test-i386.h"
62 #include "test-i386.h"
65 #include "test-i386.h"
68 #include "test-i386.h"
71 #include "test-i386.h"
75 #include "test-i386.h"
79 #include "test-i386.h"
84 #include "test-i386.h"
89 #include "test-i386.h"
94 #include "test-i386.h"
99 #include "test-i386.h"
102 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
105 #include "test-i386-shift.h"
108 #include "test-i386-shift.h"
111 #include "test-i386-shift.h"
114 #include "test-i386-shift.h"
117 #include "test-i386-shift.h"
121 #include "test-i386-shift.h"
125 #include "test-i386-shift.h"
130 #include "test-i386-shift.h"
135 #include "test-i386-shift.h"
137 /* XXX: should be more precise ? */
139 #define CC_MASK (CC_C)
143 #include "test-i386-shift.h"
147 #include "test-i386-shift.h"
151 #include "test-i386-shift.h"
155 #include "test-i386-shift.h"
157 /* lea test (modrm support) */
158 #define TEST_LEA(STR)\
160 asm("leal " STR ", %0"\
162 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
163 printf("lea %s = %08x\n", STR, res);\
166 #define TEST_LEA16(STR)\
168 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
170 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
171 printf("lea %s = %08x\n", STR, res);\
177 int eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
194 TEST_LEA("0x40(%%eax)");
195 TEST_LEA("0x40(%%ebx)");
196 TEST_LEA("0x40(%%ecx)");
197 TEST_LEA("0x40(%%edx)");
198 TEST_LEA("0x40(%%esi)");
199 TEST_LEA("0x40(%%edi)");
201 TEST_LEA("0x4000(%%eax)");
202 TEST_LEA("0x4000(%%ebx)");
203 TEST_LEA("0x4000(%%ecx)");
204 TEST_LEA("0x4000(%%edx)");
205 TEST_LEA("0x4000(%%esi)");
206 TEST_LEA("0x4000(%%edi)");
208 TEST_LEA("(%%eax, %%ecx)");
209 TEST_LEA("(%%ebx, %%edx)");
210 TEST_LEA("(%%ecx, %%ecx)");
211 TEST_LEA("(%%edx, %%ecx)");
212 TEST_LEA("(%%esi, %%ecx)");
213 TEST_LEA("(%%edi, %%ecx)");
215 TEST_LEA("0x40(%%eax, %%ecx)");
216 TEST_LEA("0x4000(%%ebx, %%edx)");
218 TEST_LEA("(%%ecx, %%ecx, 2)");
219 TEST_LEA("(%%edx, %%ecx, 4)");
220 TEST_LEA("(%%esi, %%ecx, 8)");
222 TEST_LEA("(,%%eax, 2)");
223 TEST_LEA("(,%%ebx, 4)");
224 TEST_LEA("(,%%ecx, 8)");
226 TEST_LEA("0x40(,%%eax, 2)");
227 TEST_LEA("0x40(,%%ebx, 4)");
228 TEST_LEA("0x40(,%%ecx, 8)");
231 TEST_LEA("-10(%%ecx, %%ecx, 2)");
232 TEST_LEA("-10(%%edx, %%ecx, 4)");
233 TEST_LEA("-10(%%esi, %%ecx, 8)");
235 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
236 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
237 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
239 /* limited 16 bit addressing test */
240 TEST_LEA16("0x4000");
241 TEST_LEA16("(%%bx)");
242 TEST_LEA16("(%%si)");
243 TEST_LEA16("(%%di)");
244 TEST_LEA16("0x40(%%bx)");
245 TEST_LEA16("0x40(%%si)");
246 TEST_LEA16("0x40(%%di)");
247 TEST_LEA16("0x4000(%%bx)");
248 TEST_LEA16("0x4000(%%si)");
249 TEST_LEA16("(%%bx,%%si)");
250 TEST_LEA16("(%%bx,%%di)");
251 TEST_LEA16("0x40(%%bx,%%si)");
252 TEST_LEA16("0x40(%%bx,%%di)");
253 TEST_LEA16("0x4000(%%bx,%%si)");
254 TEST_LEA16("0x4000(%%bx,%%di)");
257 #define TEST_JCC(JCC, v1, v2)\
260 asm("movl $1, %0\n\t"\
266 : "r" (v1), "r" (v2));\
267 printf("%-10s %d\n", "j" JCC, res);\
269 asm("movl $0, %0\n\t"\
271 "set" JCC " %b0\n\t"\
273 : "r" (v1), "r" (v2));\
274 printf("%-10s %d\n", "set" JCC, res);\
276 asm("movl $0x12345678, %0\n\t"\
278 "cmov" JCC "l %3, %0\n\t"\
280 : "r" (v1), "r" (v2), "m" (1));\
281 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
282 asm("movl $0x12345678, %0\n\t"\
284 "cmov" JCC "w %w3, %w0\n\t"\
286 : "r" (v1), "r" (v2), "r" (1));\
287 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
291 /* various jump tests */
294 TEST_JCC("ne", 1, 1);
295 TEST_JCC("ne", 1, 0);
302 TEST_JCC("l", 1, -1);
304 TEST_JCC("le", 1, 1);
305 TEST_JCC("le", 1, 0);
306 TEST_JCC("le", 1, -1);
308 TEST_JCC("ge", 1, 1);
309 TEST_JCC("ge", 1, 0);
310 TEST_JCC("ge", -1, 1);
314 TEST_JCC("g", 1, -1);
318 TEST_JCC("b", 1, -1);
320 TEST_JCC("be", 1, 1);
321 TEST_JCC("be", 1, 0);
322 TEST_JCC("be", 1, -1);
324 TEST_JCC("ae", 1, 1);
325 TEST_JCC("ae", 1, 0);
326 TEST_JCC("ae", 1, -1);
330 TEST_JCC("a", 1, -1);
336 TEST_JCC("np", 1, 1);
337 TEST_JCC("np", 1, 0);
339 TEST_JCC("o", 0x7fffffff, 0);
340 TEST_JCC("o", 0x7fffffff, -1);
342 TEST_JCC("no", 0x7fffffff, 0);
343 TEST_JCC("no", 0x7fffffff, -1);
346 TEST_JCC("s", 0, -1);
349 TEST_JCC("ns", 0, 1);
350 TEST_JCC("ns", 0, -1);
351 TEST_JCC("ns", 0, 0);
355 #define CC_MASK (CC_O | CC_C)
358 #include "test-i386-muldiv.h"
361 #include "test-i386-muldiv.h"
367 #include "test-i386-muldiv.h"
370 #include "test-i386-muldiv.h"
372 void test_imulw2(int op0
, int op1
)
374 int res
, s1
, s0
, flags
;
384 : "=q" (res
), "=g" (flags
)
385 : "q" (s1
), "0" (res
), "1" (flags
));
386 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
387 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
390 void test_imull2(int op0
, int op1
)
392 int res
, s1
, s0
, flags
;
402 : "=q" (res
), "=g" (flags
)
403 : "q" (s1
), "0" (res
), "1" (flags
));
404 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
405 "imull", s0
, s1
, res
, flags
& CC_MASK
);
410 test_imulb(0x1234561d, 4);
412 test_imulb(0x80, 0x80);
413 test_imulb(0x10, 0x10);
415 test_imulw(0, 0x1234001d, 45);
416 test_imulw(0, 23, -45);
417 test_imulw(0, 0x8000, 0x8000);
418 test_imulw(0, 0x100, 0x100);
420 test_imull(0, 0x1234001d, 45);
421 test_imull(0, 23, -45);
422 test_imull(0, 0x80000000, 0x80000000);
423 test_imull(0, 0x10000, 0x10000);
425 test_mulb(0x1234561d, 4);
427 test_mulb(0x80, 0x80);
428 test_mulb(0x10, 0x10);
430 test_mulw(0, 0x1234001d, 45);
431 test_mulw(0, 23, -45);
432 test_mulw(0, 0x8000, 0x8000);
433 test_mulw(0, 0x100, 0x100);
435 test_mull(0, 0x1234001d, 45);
436 test_mull(0, 23, -45);
437 test_mull(0, 0x80000000, 0x80000000);
438 test_mull(0, 0x10000, 0x10000);
440 test_imulw2(0x1234001d, 45);
441 test_imulw2(23, -45);
442 test_imulw2(0x8000, 0x8000);
443 test_imulw2(0x100, 0x100);
445 test_imull2(0x1234001d, 45);
446 test_imull2(23, -45);
447 test_imull2(0x80000000, 0x80000000);
448 test_imull2(0x10000, 0x10000);
450 test_idivb(0x12341678, 0x127e);
451 test_idivb(0x43210123, -5);
452 test_idivb(0x12340004, -1);
454 test_idivw(0, 0x12345678, 12347);
455 test_idivw(0, -23223, -45);
456 test_idivw(0, 0x12348000, -1);
457 test_idivw(0x12343, 0x12345678, 0x81238567);
459 test_idivl(0, 0x12345678, 12347);
460 test_idivl(0, -233223, -45);
461 test_idivl(0, 0x80000000, -1);
462 test_idivl(0x12343, 0x12345678, 0x81234567);
464 test_divb(0x12341678, 0x127e);
465 test_divb(0x43210123, -5);
466 test_divb(0x12340004, -1);
468 test_divw(0, 0x12345678, 12347);
469 test_divw(0, -23223, -45);
470 test_divw(0, 0x12348000, -1);
471 test_divw(0x12343, 0x12345678, 0x81238567);
473 test_divl(0, 0x12345678, 12347);
474 test_divl(0, -233223, -45);
475 test_divl(0, 0x80000000, -1);
476 test_divl(0x12343, 0x12345678, 0x81234567);
479 #define TEST_BSX(op, size, op0)\
483 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
484 : "=r" (res), "=q" (resz)\
486 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
491 TEST_BSX(bsrw
, "w", 0);
492 TEST_BSX(bsrw
, "w", 0x12340128);
493 TEST_BSX(bsrl
, "", 0);
494 TEST_BSX(bsrl
, "", 0x00340128);
495 TEST_BSX(bsfw
, "w", 0);
496 TEST_BSX(bsfw
, "w", 0x12340128);
497 TEST_BSX(bsfl
, "", 0);
498 TEST_BSX(bsfl
, "", 0x00340128);
501 /**********************************************/
503 void test_fops(double a
, double b
)
505 printf("a=%f b=%f a+b=%f\n", a
, b
, a
+ b
);
506 printf("a=%f b=%f a-b=%f\n", a
, b
, a
- b
);
507 printf("a=%f b=%f a*b=%f\n", a
, b
, a
* b
);
508 printf("a=%f b=%f a/b=%f\n", a
, b
, a
/ b
);
509 printf("a=%f b=%f fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
510 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
511 printf("a=%f sin(a)=%f\n", a
, sin(a
));
512 printf("a=%f cos(a)=%f\n", a
, cos(a
));
513 printf("a=%f tan(a)=%f\n", a
, tan(a
));
514 printf("a=%f log(a)=%f\n", a
, log(a
));
515 printf("a=%f exp(a)=%f\n", a
, exp(a
));
516 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
517 /* just to test some op combining */
518 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
519 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
520 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
524 void test_fcmp(double a
, double b
)
526 printf("(%f<%f)=%d\n",
528 printf("(%f<=%f)=%d\n",
530 printf("(%f==%f)=%d\n",
532 printf("(%f>%f)=%d\n",
534 printf("(%f<=%f)=%d\n",
538 /* test f(u)comi instruction */
544 printf("fcomi(%f %f)=%08x\n", a
, b
, eflags
& (CC_Z
| CC_P
| CC_C
));
548 void test_fcvt(double a
)
561 printf("(float)%f = %f\n", a
, fa
);
562 printf("(long double)%f = %Lf\n", a
, la
);
563 printf("a=%016Lx\n", *(long long *)&a
);
564 printf("la=%016Lx %04x\n", *(long long *)&la
,
565 *(unsigned short *)((char *)(&la
) + 8));
567 /* test all roundings */
568 asm volatile ("fstcw %0" : "=m" (fpuc
));
570 asm volatile ("fldcw %0" : : "m" ((fpuc
& ~0x0c00) | (i
<< 10)));
571 asm volatile ("fist %0" : "=m" (wa
) : "t" (a
));
572 asm volatile ("fistl %0" : "=m" (ia
) : "t" (a
));
573 asm volatile ("fistpll %0" : "=m" (lla
) : "t" (a
) : "st");
574 asm volatile ("frndint ; fstl %0" : "=m" (ra
) : "t" (a
));
575 asm volatile ("fldcw %0" : : "m" (fpuc
));
576 printf("(short)a = %d\n", wa
);
577 printf("(int)a = %d\n", ia
);
578 printf("(int64_t)a = %Ld\n", lla
);
579 printf("rint(a) = %f\n", ra
);
584 asm("fld" #N : "=t" (a)); \
585 printf("fld" #N "= %f\n", a);
587 void test_fconst(void)
599 void test_fbcd(double a
)
601 unsigned short bcd
[5];
604 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
605 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
606 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
607 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
610 #define TEST_ENV(env, prefix)\
612 memset((env), 0xaa, sizeof(*(env)));\
614 prefix "fnstenv %1\n"\
615 prefix "fldenv %1\n"\
616 : "=t" (res) : "m" (*(env)));\
617 printf("res=%f\n", res);\
618 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
620 (env)->fpus & 0xff00,\
622 memset((env), 0xaa, sizeof(*(env)));\
624 prefix "fnsave %1\n"\
625 prefix "frstor %1\n"\
626 : "=t" (res) : "m" (*(env)));\
627 printf("res=%f\n", res);\
628 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
630 (env)->fpus & 0xff00,\
632 printf("ST(0) = %Lf\n",\
638 struct __attribute__((packed
)) {
646 long double fpregs
[8];
648 struct __attribute__((packed
)) {
653 long double fpregs
[8];
657 TEST_ENV(&float_env16
, "data16 ");
658 TEST_ENV(&float_env32
, "");
661 void test_floats(void)
675 test_fbcd(1234567890123456);
676 test_fbcd(-123451234567890);
680 /**********************************************/
682 #define TEST_BCD(op, op0, cc_in, cc_mask)\
692 : "=a" (res), "=g" (flags)\
693 : "0" (res), "1" (flags));\
694 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
695 #op, op0, res, cc_in, flags & cc_mask);\
700 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
701 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
702 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
703 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
704 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
705 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
706 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
707 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
708 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
709 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
710 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
711 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
712 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
714 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
715 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
716 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
717 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
718 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
719 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
720 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
721 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
722 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
723 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
724 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
725 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
726 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
728 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
729 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
730 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
731 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
732 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
733 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
734 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
735 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
737 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
738 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
739 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
740 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
741 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
742 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
743 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
744 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
746 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
747 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
750 #define TEST_XCHG(op, size, opconst)\
755 asm(#op " %" size "0, %" size "1" \
756 : "=q" (op0), opconst (op1) \
757 : "0" (op0), "1" (op1));\
758 printf("%-10s A=%08x B=%08x\n",\
762 #define TEST_CMPXCHG(op, size, opconst, eax)\
767 asm(#op " %" size "0, %" size "1" \
768 : "=q" (op0), opconst (op1) \
769 : "0" (op0), "1" (op1), "a" (eax));\
770 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
771 #op, eax, op0, op1);\
776 TEST_XCHG(xchgl
, "", "=q");
777 TEST_XCHG(xchgw
, "w", "=q");
778 TEST_XCHG(xchgb
, "b", "=q");
780 TEST_XCHG(xchgl
, "", "=m");
781 TEST_XCHG(xchgw
, "w", "=m");
782 TEST_XCHG(xchgb
, "b", "=m");
784 TEST_XCHG(xaddl
, "", "=q");
785 TEST_XCHG(xaddw
, "w", "=q");
786 TEST_XCHG(xaddb
, "b", "=q");
788 TEST_XCHG(xaddl
, "", "=m");
789 TEST_XCHG(xaddw
, "w", "=m");
790 TEST_XCHG(xaddb
, "b", "=m");
792 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfbca7654);
793 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfbca7654);
794 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfbca7654);
796 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfffefdfc);
797 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfffefdfc);
798 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfffefdfc);
800 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfbca7654);
801 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfbca7654);
802 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfbca7654);
804 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfffefdfc);
805 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfffefdfc);
806 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfffefdfc);
809 /**********************************************/
810 /* segmentation tests */
813 #include <linux/unistd.h>
815 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
817 uint8_t seg_data1
[4096];
818 uint8_t seg_data2
[4096];
820 #define MK_SEL(n) (((n) << 3) | 7)
822 #define TEST_LR(op, size, seg, mask)\
826 asm (op " %" size "2, %" size "0\n" \
831 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
832 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
835 /* NOTE: we use Linux modify_ldt syscall */
838 struct modify_ldt_ldt_s ldt
;
839 long long ldt_table
[3];
845 } __attribute__((packed
)) segoff
;
847 ldt
.entry_number
= 1;
848 ldt
.base_addr
= (unsigned long)&seg_data1
;
849 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
851 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
852 ldt
.read_exec_only
= 0;
853 ldt
.limit_in_pages
= 1;
854 ldt
.seg_not_present
= 0;
856 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
858 ldt
.entry_number
= 2;
859 ldt
.base_addr
= (unsigned long)&seg_data2
;
860 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
862 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
863 ldt
.read_exec_only
= 0;
864 ldt
.limit_in_pages
= 1;
865 ldt
.seg_not_present
= 0;
867 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
869 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
874 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
877 /* do some tests with fs or gs */
878 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
883 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
884 printf("FS[1] = %02x\n", res
);
886 asm volatile ("pushl %%gs\n"
888 "gs movzbl 0x1, %0\n"
892 printf("GS[1] = %02x\n", res
);
894 /* tests with ds/ss (implicit segment case) */
896 asm volatile ("pushl %%ebp\n\t"
901 "movzbl (%%ebp), %1\n\t"
904 : "=r" (res
), "=r" (res2
)
905 : "r" (MK_SEL(1)), "r" (&tmp
));
906 printf("DS[1] = %02x\n", res
);
907 printf("SS[tmp] = %02x\n", res2
);
909 segoff
.seg
= MK_SEL(2);
910 segoff
.offset
= 0xabcdef12;
911 asm volatile("lfs %2, %0\n\t"
913 : "=r" (res
), "=g" (res2
)
915 printf("FS:reg = %04x:%08x\n", res2
, res
);
917 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
918 TEST_LR("larl", "", MK_SEL(2), 0x0100);
919 TEST_LR("lslw", "w", MK_SEL(2), 0);
920 TEST_LR("lsll", "", MK_SEL(2), 0);
922 TEST_LR("larw", "w", 0xfff8, 0);
923 TEST_LR("larl", "", 0xfff8, 0);
924 TEST_LR("lslw", "w", 0xfff8, 0);
925 TEST_LR("lsll", "", 0xfff8, 0);
928 /* 16 bit code test */
929 extern char code16_start
, code16_end
;
930 extern char code16_func1
;
931 extern char code16_func2
;
932 extern char code16_func3
;
934 void test_code16(void)
936 struct modify_ldt_ldt_s ldt
;
939 /* build a code segment */
940 ldt
.entry_number
= 1;
941 ldt
.base_addr
= (unsigned long)&code16_start
;
942 ldt
.limit
= &code16_end
- &code16_start
;
944 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
945 ldt
.read_exec_only
= 0;
946 ldt
.limit_in_pages
= 0;
947 ldt
.seg_not_present
= 0;
949 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
951 /* call the first function */
952 asm volatile ("lcall %1, %2"
954 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
955 printf("func1() = 0x%08x\n", res
);
956 asm volatile ("lcall %2, %3"
957 : "=a" (res
), "=c" (res2
)
958 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
959 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
960 asm volatile ("lcall %1, %2"
962 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
963 printf("func3() = 0x%08x\n", res
);
966 extern char func_lret32
;
967 extern char func_iret32
;
974 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
976 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
977 printf("xlat: EAX=%08x\n", res
);
979 asm volatile ("pushl %%cs ; call %1"
981 : "m" (func_lret32
): "memory", "cc");
982 printf("func_lret32=%x\n", res
);
984 asm volatile ("pushfl ; pushl %%cs ; call %1"
986 : "m" (func_iret32
): "memory", "cc");
987 printf("func_iret32=%x\n", res
);
989 /* specific popl test */
990 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
992 printf("popl esp=%x\n", res
);
994 /* specific popw test */
995 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
997 printf("popw esp=%x\n", res
);
1000 uint8_t str_buffer
[4096];
1002 #define TEST_STRING1(OP, size, DF, REP)\
1004 int esi, edi, eax, ecx, eflags;\
1006 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1007 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1011 asm volatile ("pushl $0\n\t"\
1014 REP #OP size "\n\t"\
1018 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1019 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1020 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1021 REP #OP size, esi, edi, eax, ecx,\
1022 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1025 #define TEST_STRING(OP, REP)\
1026 TEST_STRING1(OP, "b", "", REP);\
1027 TEST_STRING1(OP, "w", "", REP);\
1028 TEST_STRING1(OP, "l", "", REP);\
1029 TEST_STRING1(OP, "b", "std", REP);\
1030 TEST_STRING1(OP, "w", "std", REP);\
1031 TEST_STRING1(OP, "l", "std", REP)
1033 void test_string(void)
1036 for(i
= 0;i
< sizeof(str_buffer
); i
++)
1037 str_buffer
[i
] = i
+ 0x56;
1038 TEST_STRING(stos
, "");
1039 TEST_STRING(stos
, "rep ");
1040 TEST_STRING(lods
, ""); /* to verify stos */
1041 TEST_STRING(lods
, "rep ");
1042 TEST_STRING(movs
, "");
1043 TEST_STRING(movs
, "rep ");
1044 TEST_STRING(lods
, ""); /* to verify stos */
1046 /* XXX: better tests */
1047 TEST_STRING(scas
, "");
1048 TEST_STRING(scas
, "repz ");
1049 TEST_STRING(scas
, "repnz ");
1050 TEST_STRING(cmps
, "");
1051 TEST_STRING(cmps
, "repz ");
1052 TEST_STRING(cmps
, "repnz ");
1057 static inline void set_bit(uint8_t *a
, unsigned int bit
)
1059 a
[bit
/ 8] |= (1 << (bit
% 8));
1062 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
1064 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
1067 static inline void pushw(struct vm86_regs
*r
, int val
)
1069 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
1070 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
1073 #undef __syscall_return
1074 #define __syscall_return(type, res) \
1076 return (type) (res); \
1079 _syscall2(int, vm86
, int, func
, struct vm86plus_struct
*, v86
)
1081 extern char vm86_code_start
;
1082 extern char vm86_code_end
;
1084 #define VM86_CODE_CS 0x100
1085 #define VM86_CODE_IP 0x100
1087 void test_vm86(void)
1089 struct vm86plus_struct ctx
;
1090 struct vm86_regs
*r
;
1094 vm86_mem
= mmap((void *)0x00000000, 0x110000,
1095 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
1096 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
1097 if (vm86_mem
== MAP_FAILED
) {
1098 printf("ERROR: could not map vm86 memory");
1101 memset(&ctx
, 0, sizeof(ctx
));
1103 /* init basic registers */
1105 r
->eip
= VM86_CODE_IP
;
1114 r
->eflags
= VIF_MASK
;
1116 /* move code to proper address. We use the same layout as a .com
1118 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
1119 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
1121 /* mark int 0x21 as being emulated */
1122 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
1125 ret
= vm86(VM86_ENTER
, &ctx
);
1126 switch(VM86_TYPE(ret
)) {
1131 int_num
= VM86_ARG(ret
);
1132 if (int_num
!= 0x21)
1134 ah
= (r
->eax
>> 8) & 0xff;
1136 case 0x00: /* exit */
1138 case 0x02: /* write char */
1144 case 0x09: /* write string */
1147 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1154 r
->eax
= (r
->eax
& ~0xff) | '$';
1157 case 0xff: /* extension: write eflags number in edx */
1159 #ifndef LINUX_VM86_IOPL_FIX
1162 printf("%08x\n", v
);
1166 printf("unsupported int 0x%02x\n", int_num
);
1172 /* a signal came, we just ignore that */
1177 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1182 printf("VM86 end\n");
1183 munmap(vm86_mem
, 0x110000);
1186 /* exception tests */
1198 #define REG_TRAPNO TRAPNO
1206 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1208 struct ucontext
*uc
= puc
;
1210 printf("si_signo=%d si_errno=%d si_code=%d",
1211 info
->si_signo
, info
->si_errno
, info
->si_code
);
1212 printf(" si_addr=0x%08lx",
1213 (unsigned long)info
->si_addr
);
1216 printf("trapno=0x%02x err=0x%08x",
1217 uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1218 uc
->uc_mcontext
.gregs
[REG_ERR
]);
1219 printf(" EIP=0x%08x", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1221 longjmp(jmp_env
, 1);
1224 void test_exceptions(void)
1226 struct modify_ldt_ldt_s ldt
;
1227 struct sigaction act
;
1230 act
.sa_sigaction
= sig_handler
;
1231 sigemptyset(&act
.sa_mask
);
1232 act
.sa_flags
= SA_SIGINFO
;
1233 sigaction(SIGFPE
, &act
, NULL
);
1234 sigaction(SIGILL
, &act
, NULL
);
1235 sigaction(SIGSEGV
, &act
, NULL
);
1236 sigaction(SIGBUS
, &act
, NULL
);
1237 sigaction(SIGTRAP
, &act
, NULL
);
1239 /* test division by zero reporting */
1240 printf("DIVZ exception:\n");
1241 if (setjmp(jmp_env
) == 0) {
1242 /* now divide by zero */
1247 printf("BOUND exception:\n");
1248 if (setjmp(jmp_env
) == 0) {
1249 /* bound exception */
1252 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
));
1255 printf("segment exceptions:\n");
1256 if (setjmp(jmp_env
) == 0) {
1257 /* load an invalid segment */
1258 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1260 if (setjmp(jmp_env
) == 0) {
1261 /* null data segment is valid */
1262 asm volatile ("movl %0, %%fs" : : "r" (3));
1263 /* null stack segment */
1264 asm volatile ("movl %0, %%ss" : : "r" (3));
1267 ldt
.entry_number
= 1;
1268 ldt
.base_addr
= (unsigned long)&seg_data1
;
1269 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1271 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1272 ldt
.read_exec_only
= 0;
1273 ldt
.limit_in_pages
= 1;
1274 ldt
.seg_not_present
= 1;
1276 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1278 if (setjmp(jmp_env
) == 0) {
1279 /* segment not present */
1280 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1283 /* test SEGV reporting */
1284 printf("PF exception:\n");
1285 if (setjmp(jmp_env
) == 0) {
1287 /* we add a nop to test a weird PC retrieval case */
1288 asm volatile ("nop");
1289 /* now store in an invalid address */
1290 *(char *)0x1234 = 1;
1293 /* test SEGV reporting */
1294 printf("PF exception:\n");
1295 if (setjmp(jmp_env
) == 0) {
1297 /* read from an invalid address */
1298 v1
= *(char *)0x1234;
1301 /* test illegal instruction reporting */
1302 printf("UD2 exception:\n");
1303 if (setjmp(jmp_env
) == 0) {
1304 /* now execute an invalid instruction */
1305 asm volatile("ud2");
1308 printf("INT exception:\n");
1309 if (setjmp(jmp_env
) == 0) {
1310 asm volatile ("int $0xfd");
1312 if (setjmp(jmp_env
) == 0) {
1313 asm volatile ("int $0x01");
1315 if (setjmp(jmp_env
) == 0) {
1316 asm volatile (".byte 0xcd, 0x03");
1318 if (setjmp(jmp_env
) == 0) {
1319 asm volatile ("int $0x04");
1321 if (setjmp(jmp_env
) == 0) {
1322 asm volatile ("int $0x05");
1325 printf("INT3 exception:\n");
1326 if (setjmp(jmp_env
) == 0) {
1327 asm volatile ("int3");
1330 printf("CLI exception:\n");
1331 if (setjmp(jmp_env
) == 0) {
1332 asm volatile ("cli");
1335 printf("STI exception:\n");
1336 if (setjmp(jmp_env
) == 0) {
1337 asm volatile ("cli");
1340 printf("INTO exception:\n");
1341 if (setjmp(jmp_env
) == 0) {
1342 /* overflow exception */
1343 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1346 printf("OUTB exception:\n");
1347 if (setjmp(jmp_env
) == 0) {
1348 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1351 printf("INB exception:\n");
1352 if (setjmp(jmp_env
) == 0) {
1353 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1356 printf("REP OUTSB exception:\n");
1357 if (setjmp(jmp_env
) == 0) {
1358 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1361 printf("REP INSB exception:\n");
1362 if (setjmp(jmp_env
) == 0) {
1363 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1366 printf("HLT exception:\n");
1367 if (setjmp(jmp_env
) == 0) {
1368 asm volatile ("hlt");
1371 printf("single step exception:\n");
1373 if (setjmp(jmp_env
) == 0) {
1374 asm volatile ("pushf\n"
1375 "orl $0x00100, (%%esp)\n"
1377 "movl $0xabcd, %0\n"
1378 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1380 printf("val=0x%x\n", val
);
1383 /* specific precise single step test */
1384 void sig_trap_handler(int sig
, siginfo_t
*info
, void *puc
)
1386 struct ucontext
*uc
= puc
;
1387 printf("EIP=0x%08x\n", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1390 const uint8_t sstep_buf1
[4] = { 1, 2, 3, 4};
1391 uint8_t sstep_buf2
[4];
1393 void test_single_step(void)
1395 struct sigaction act
;
1400 act
.sa_sigaction
= sig_trap_handler
;
1401 sigemptyset(&act
.sa_mask
);
1402 act
.sa_flags
= SA_SIGINFO
;
1403 sigaction(SIGTRAP
, &act
, NULL
);
1404 asm volatile ("pushf\n"
1405 "orl $0x00100, (%%esp)\n"
1407 "movl $0xabcd, %0\n"
1416 /* movsb: the single step should stop at each movsb iteration */
1417 "movl $sstep_buf1, %%esi\n"
1418 "movl $sstep_buf2, %%edi\n"
1426 /* cmpsb: the single step should stop at each cmpsb iteration */
1427 "movl $sstep_buf1, %%esi\n"
1428 "movl $sstep_buf2, %%edi\n"
1434 /* getpid() syscall: single step should skip one
1440 /* when modifying SS, trace is not done on the next
1442 "movl %%ss, %%ecx\n"
1443 "movl %%ecx, %%ss\n"
1446 "movl %%ecx, %%ss\n"
1457 "andl $~0x00100, (%%esp)\n"
1461 : "cc", "memory", "eax", "ecx", "esi", "edi");
1462 printf("val=%d\n", val
);
1463 for(i
= 0; i
< 4; i
++)
1464 printf("sstep_buf2[%d] = %d\n", i
, sstep_buf2
[i
]);
1467 /* self modifying code test */
1469 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1473 typedef int FuncType(void);
1475 void test_self_modifying_code(void)
1479 printf("self modifying code:\n");
1480 printf("func1 = 0x%x\n", ((FuncType
*)code
)());
1481 for(i
= 2; i
<= 4; i
++) {
1483 printf("func%d = 0x%x\n", i
, ((FuncType
*)code
)());
1487 static void *call_end __init_call
= NULL
;
1489 int main(int argc
, char **argv
)
1494 ptr
= &call_start
+ 1;
1495 while (*ptr
!= NULL
) {
1512 test_self_modifying_code();