]>
git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
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>
36 //#define LINUX_VM86_IOPL_FIX
37 //#define TEST_P4_FLAGS
39 #define xglue(x, y) x ## y
40 #define glue(x, y) xglue(x, y)
41 #define stringify(s) tostring(s)
42 #define tostring(s) #s
51 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
53 static void *call_start __init_call
= NULL
;
55 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
58 #include "test-i386.h"
61 #include "test-i386.h"
64 #include "test-i386.h"
67 #include "test-i386.h"
70 #include "test-i386.h"
73 #include "test-i386.h"
77 #include "test-i386.h"
81 #include "test-i386.h"
86 #include "test-i386.h"
91 #include "test-i386.h"
96 #include "test-i386.h"
101 #include "test-i386.h"
104 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
107 #include "test-i386-shift.h"
110 #include "test-i386-shift.h"
113 #include "test-i386-shift.h"
116 #include "test-i386-shift.h"
119 #include "test-i386-shift.h"
123 #include "test-i386-shift.h"
127 #include "test-i386-shift.h"
132 #include "test-i386-shift.h"
137 #include "test-i386-shift.h"
139 /* XXX: should be more precise ? */
141 #define CC_MASK (CC_C)
145 #include "test-i386-shift.h"
149 #include "test-i386-shift.h"
153 #include "test-i386-shift.h"
157 #include "test-i386-shift.h"
159 /* lea test (modrm support) */
160 #define TEST_LEA(STR)\
162 asm("leal " STR ", %0"\
164 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
165 printf("lea %s = %08x\n", STR, res);\
168 #define TEST_LEA16(STR)\
170 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
172 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
173 printf("lea %s = %08x\n", STR, res);\
179 int eax
, ebx
, ecx
, edx
, esi
, edi
, res
;
196 TEST_LEA("0x40(%%eax)");
197 TEST_LEA("0x40(%%ebx)");
198 TEST_LEA("0x40(%%ecx)");
199 TEST_LEA("0x40(%%edx)");
200 TEST_LEA("0x40(%%esi)");
201 TEST_LEA("0x40(%%edi)");
203 TEST_LEA("0x4000(%%eax)");
204 TEST_LEA("0x4000(%%ebx)");
205 TEST_LEA("0x4000(%%ecx)");
206 TEST_LEA("0x4000(%%edx)");
207 TEST_LEA("0x4000(%%esi)");
208 TEST_LEA("0x4000(%%edi)");
210 TEST_LEA("(%%eax, %%ecx)");
211 TEST_LEA("(%%ebx, %%edx)");
212 TEST_LEA("(%%ecx, %%ecx)");
213 TEST_LEA("(%%edx, %%ecx)");
214 TEST_LEA("(%%esi, %%ecx)");
215 TEST_LEA("(%%edi, %%ecx)");
217 TEST_LEA("0x40(%%eax, %%ecx)");
218 TEST_LEA("0x4000(%%ebx, %%edx)");
220 TEST_LEA("(%%ecx, %%ecx, 2)");
221 TEST_LEA("(%%edx, %%ecx, 4)");
222 TEST_LEA("(%%esi, %%ecx, 8)");
224 TEST_LEA("(,%%eax, 2)");
225 TEST_LEA("(,%%ebx, 4)");
226 TEST_LEA("(,%%ecx, 8)");
228 TEST_LEA("0x40(,%%eax, 2)");
229 TEST_LEA("0x40(,%%ebx, 4)");
230 TEST_LEA("0x40(,%%ecx, 8)");
233 TEST_LEA("-10(%%ecx, %%ecx, 2)");
234 TEST_LEA("-10(%%edx, %%ecx, 4)");
235 TEST_LEA("-10(%%esi, %%ecx, 8)");
237 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
238 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
239 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
241 /* limited 16 bit addressing test */
242 TEST_LEA16("0x4000");
243 TEST_LEA16("(%%bx)");
244 TEST_LEA16("(%%si)");
245 TEST_LEA16("(%%di)");
246 TEST_LEA16("0x40(%%bx)");
247 TEST_LEA16("0x40(%%si)");
248 TEST_LEA16("0x40(%%di)");
249 TEST_LEA16("0x4000(%%bx)");
250 TEST_LEA16("0x4000(%%si)");
251 TEST_LEA16("(%%bx,%%si)");
252 TEST_LEA16("(%%bx,%%di)");
253 TEST_LEA16("0x40(%%bx,%%si)");
254 TEST_LEA16("0x40(%%bx,%%di)");
255 TEST_LEA16("0x4000(%%bx,%%si)");
256 TEST_LEA16("0x4000(%%bx,%%di)");
259 #define TEST_JCC(JCC, v1, v2)\
262 asm("movl $1, %0\n\t"\
268 : "r" (v1), "r" (v2));\
269 printf("%-10s %d\n", "j" JCC, res);\
271 asm("movl $0, %0\n\t"\
273 "set" JCC " %b0\n\t"\
275 : "r" (v1), "r" (v2));\
276 printf("%-10s %d\n", "set" JCC, res);\
278 asm("movl $0x12345678, %0\n\t"\
280 "cmov" JCC "l %3, %0\n\t"\
282 : "r" (v1), "r" (v2), "m" (1));\
283 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
284 asm("movl $0x12345678, %0\n\t"\
286 "cmov" JCC "w %w3, %w0\n\t"\
288 : "r" (v1), "r" (v2), "r" (1));\
289 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
293 /* various jump tests */
296 TEST_JCC("ne", 1, 1);
297 TEST_JCC("ne", 1, 0);
304 TEST_JCC("l", 1, -1);
306 TEST_JCC("le", 1, 1);
307 TEST_JCC("le", 1, 0);
308 TEST_JCC("le", 1, -1);
310 TEST_JCC("ge", 1, 1);
311 TEST_JCC("ge", 1, 0);
312 TEST_JCC("ge", -1, 1);
316 TEST_JCC("g", 1, -1);
320 TEST_JCC("b", 1, -1);
322 TEST_JCC("be", 1, 1);
323 TEST_JCC("be", 1, 0);
324 TEST_JCC("be", 1, -1);
326 TEST_JCC("ae", 1, 1);
327 TEST_JCC("ae", 1, 0);
328 TEST_JCC("ae", 1, -1);
332 TEST_JCC("a", 1, -1);
338 TEST_JCC("np", 1, 1);
339 TEST_JCC("np", 1, 0);
341 TEST_JCC("o", 0x7fffffff, 0);
342 TEST_JCC("o", 0x7fffffff, -1);
344 TEST_JCC("no", 0x7fffffff, 0);
345 TEST_JCC("no", 0x7fffffff, -1);
348 TEST_JCC("s", 0, -1);
351 TEST_JCC("ns", 0, 1);
352 TEST_JCC("ns", 0, -1);
353 TEST_JCC("ns", 0, 0);
358 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
360 #define CC_MASK (CC_O | CC_C)
364 #include "test-i386-muldiv.h"
367 #include "test-i386-muldiv.h"
369 void test_imulw2(int op0
, int op1
)
371 int res
, s1
, s0
, flags
;
376 asm volatile ("push %4\n\t"
381 : "=q" (res
), "=g" (flags
)
382 : "q" (s1
), "0" (res
), "1" (flags
));
383 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
384 "imulw", s0
, s1
, res
, flags
& CC_MASK
);
387 void test_imull2(int op0
, int op1
)
389 int res
, s1
, s0
, flags
;
394 asm volatile ("push %4\n\t"
399 : "=q" (res
), "=g" (flags
)
400 : "q" (s1
), "0" (res
), "1" (flags
));
401 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
402 "imull", s0
, s1
, res
, flags
& CC_MASK
);
405 #define TEST_IMUL_IM(size, size1, op0, op1)\
410 asm volatile ("push %3\n\t"\
412 "imul" size " $" #op0 ", %" size1 "2, %" size1 "0\n\t" \
415 : "=r" (res), "=g" (flags)\
416 : "r" (op1), "1" (flags), "0" (res));\
417 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",\
418 "imul" size " im", op0, op1, res, flags & CC_MASK);\
426 #include "test-i386-muldiv.h"
429 #include "test-i386-muldiv.h"
433 test_imulb(0x1234561d, 4);
435 test_imulb(0x80, 0x80);
436 test_imulb(0x10, 0x10);
438 test_imulw(0, 0x1234001d, 45);
439 test_imulw(0, 23, -45);
440 test_imulw(0, 0x8000, 0x8000);
441 test_imulw(0, 0x100, 0x100);
443 test_imull(0, 0x1234001d, 45);
444 test_imull(0, 23, -45);
445 test_imull(0, 0x80000000, 0x80000000);
446 test_imull(0, 0x10000, 0x10000);
448 test_mulb(0x1234561d, 4);
450 test_mulb(0x80, 0x80);
451 test_mulb(0x10, 0x10);
453 test_mulw(0, 0x1234001d, 45);
454 test_mulw(0, 23, -45);
455 test_mulw(0, 0x8000, 0x8000);
456 test_mulw(0, 0x100, 0x100);
458 test_mull(0, 0x1234001d, 45);
459 test_mull(0, 23, -45);
460 test_mull(0, 0x80000000, 0x80000000);
461 test_mull(0, 0x10000, 0x10000);
463 test_imulw2(0x1234001d, 45);
464 test_imulw2(23, -45);
465 test_imulw2(0x8000, 0x8000);
466 test_imulw2(0x100, 0x100);
468 test_imull2(0x1234001d, 45);
469 test_imull2(23, -45);
470 test_imull2(0x80000000, 0x80000000);
471 test_imull2(0x10000, 0x10000);
473 TEST_IMUL_IM("w", "w", 45, 0x1234);
474 TEST_IMUL_IM("w", "w", -45, 23);
475 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
476 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
478 TEST_IMUL_IM("l", "", 45, 0x1234);
479 TEST_IMUL_IM("l", "", -45, 23);
480 TEST_IMUL_IM("l", "", 0x8000, 0x80000000);
481 TEST_IMUL_IM("l", "", 0x7fff, 0x1000);
483 test_idivb(0x12341678, 0x127e);
484 test_idivb(0x43210123, -5);
485 test_idivb(0x12340004, -1);
487 test_idivw(0, 0x12345678, 12347);
488 test_idivw(0, -23223, -45);
489 test_idivw(0, 0x12348000, -1);
490 test_idivw(0x12343, 0x12345678, 0x81238567);
492 test_idivl(0, 0x12345678, 12347);
493 test_idivl(0, -233223, -45);
494 test_idivl(0, 0x80000000, -1);
495 test_idivl(0x12343, 0x12345678, 0x81234567);
497 test_divb(0x12341678, 0x127e);
498 test_divb(0x43210123, -5);
499 test_divb(0x12340004, -1);
501 test_divw(0, 0x12345678, 12347);
502 test_divw(0, -23223, -45);
503 test_divw(0, 0x12348000, -1);
504 test_divw(0x12343, 0x12345678, 0x81238567);
506 test_divl(0, 0x12345678, 12347);
507 test_divl(0, -233223, -45);
508 test_divl(0, 0x80000000, -1);
509 test_divl(0x12343, 0x12345678, 0x81234567);
512 #define TEST_BSX(op, size, op0)\
517 "movl $0x12345678, %0\n"\
518 #op " %" size "2, %" size "0 ; setz %b1" \
519 : "=r" (res), "=q" (resz)\
521 printf("%-10s A=%08x R=%08x %d\n", #op, val, res, resz);\
526 TEST_BSX(bsrw
, "w", 0);
527 TEST_BSX(bsrw
, "w", 0x12340128);
528 TEST_BSX(bsrl
, "", 0);
529 TEST_BSX(bsrl
, "", 0x00340128);
530 TEST_BSX(bsfw
, "w", 0);
531 TEST_BSX(bsfw
, "w", 0x12340128);
532 TEST_BSX(bsfl
, "", 0);
533 TEST_BSX(bsfl
, "", 0x00340128);
536 /**********************************************/
538 void test_fops(double a
, double b
)
540 printf("a=%f b=%f a+b=%f\n", a
, b
, a
+ b
);
541 printf("a=%f b=%f a-b=%f\n", a
, b
, a
- b
);
542 printf("a=%f b=%f a*b=%f\n", a
, b
, a
* b
);
543 printf("a=%f b=%f a/b=%f\n", a
, b
, a
/ b
);
544 printf("a=%f b=%f fmod(a, b)=%f\n", a
, b
, fmod(a
, b
));
545 printf("a=%f sqrt(a)=%f\n", a
, sqrt(a
));
546 printf("a=%f sin(a)=%f\n", a
, sin(a
));
547 printf("a=%f cos(a)=%f\n", a
, cos(a
));
548 printf("a=%f tan(a)=%f\n", a
, tan(a
));
549 printf("a=%f log(a)=%f\n", a
, log(a
));
550 printf("a=%f exp(a)=%f\n", a
, exp(a
));
551 printf("a=%f b=%f atan2(a, b)=%f\n", a
, b
, atan2(a
, b
));
552 /* just to test some op combining */
553 printf("a=%f asin(sin(a))=%f\n", a
, asin(sin(a
)));
554 printf("a=%f acos(cos(a))=%f\n", a
, acos(cos(a
)));
555 printf("a=%f atan(tan(a))=%f\n", a
, atan(tan(a
)));
559 void test_fcmp(double a
, double b
)
561 printf("(%f<%f)=%d\n",
563 printf("(%f<=%f)=%d\n",
565 printf("(%f==%f)=%d\n",
567 printf("(%f>%f)=%d\n",
569 printf("(%f<=%f)=%d\n",
573 /* test f(u)comi instruction */
579 printf("fcomi(%f %f)=%08x\n", a
, b
, eflags
& (CC_Z
| CC_P
| CC_C
));
583 void test_fcvt(double a
)
596 printf("(float)%f = %f\n", a
, fa
);
597 printf("(long double)%f = %Lf\n", a
, la
);
598 printf("a=%016Lx\n", *(long long *)&a
);
599 printf("la=%016Lx %04x\n", *(long long *)&la
,
600 *(unsigned short *)((char *)(&la
) + 8));
602 /* test all roundings */
603 asm volatile ("fstcw %0" : "=m" (fpuc
));
605 asm volatile ("fldcw %0" : : "m" ((fpuc
& ~0x0c00) | (i
<< 10)));
606 asm volatile ("fist %0" : "=m" (wa
) : "t" (a
));
607 asm volatile ("fistl %0" : "=m" (ia
) : "t" (a
));
608 asm volatile ("fistpll %0" : "=m" (lla
) : "t" (a
) : "st");
609 asm volatile ("frndint ; fstl %0" : "=m" (ra
) : "t" (a
));
610 asm volatile ("fldcw %0" : : "m" (fpuc
));
611 printf("(short)a = %d\n", wa
);
612 printf("(int)a = %d\n", ia
);
613 printf("(int64_t)a = %Ld\n", lla
);
614 printf("rint(a) = %f\n", ra
);
619 asm("fld" #N : "=t" (a)); \
620 printf("fld" #N "= %f\n", a);
622 void test_fconst(void)
634 void test_fbcd(double a
)
636 unsigned short bcd
[5];
639 asm("fbstp %0" : "=m" (bcd
[0]) : "t" (a
) : "st");
640 asm("fbld %1" : "=t" (b
) : "m" (bcd
[0]));
641 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
642 a
, bcd
[4], bcd
[3], bcd
[2], bcd
[1], bcd
[0], b
);
645 #define TEST_ENV(env, save, restore)\
647 memset((env), 0xaa, sizeof(*(env)));\
649 asm volatile ("fldl %0" : : "m" (dtab[i]));\
650 asm(save " %0\n" : : "m" (*(env)));\
651 asm(restore " %0\n": : "m" (*(env)));\
653 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
655 printf("res[%d]=%f\n", i, rtab[i]);\
656 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
658 (env)->fpus & 0xff00,\
664 struct __attribute__((packed
)) {
672 long double fpregs
[8];
674 struct __attribute__((packed
)) {
679 long double fpregs
[8];
688 TEST_ENV(&float_env16
, "data16 fnstenv", "data16 fldenv");
689 TEST_ENV(&float_env16
, "data16 fnsave", "data16 frstor");
690 TEST_ENV(&float_env32
, "fnstenv", "fldenv");
691 TEST_ENV(&float_env32
, "fnsave", "frstor");
695 asm volatile ("fldl %0" : : "m" (dtab
[i
]));
696 asm volatile("ffree %st(2)");
697 asm volatile ("fnstenv %0\n" : : "m" (float_env32
));
698 asm volatile ("fninit");
699 printf("fptag=%04x\n", float_env32
.fptag
);
703 #define TEST_FCMOV(a, b, eflags, CC)\
708 "fcmov" CC " %2, %0\n"\
710 : "0" (a), "u" (b), "g" (eflags));\
711 printf("fcmov%s eflags=0x%04x-> %f\n", \
715 void test_fcmov(void)
722 for(i
= 0; i
< 4; i
++) {
728 TEST_FCMOV(a
, b
, eflags
, "b");
729 TEST_FCMOV(a
, b
, eflags
, "e");
730 TEST_FCMOV(a
, b
, eflags
, "be");
731 TEST_FCMOV(a
, b
, eflags
, "nb");
732 TEST_FCMOV(a
, b
, eflags
, "ne");
733 TEST_FCMOV(a
, b
, eflags
, "nbe");
735 TEST_FCMOV(a
, b
, 0, "u");
736 TEST_FCMOV(a
, b
, CC_P
, "u");
737 TEST_FCMOV(a
, b
, 0, "nu");
738 TEST_FCMOV(a
, b
, CC_P
, "nu");
741 void test_floats(void)
755 test_fbcd(1234567890123456);
756 test_fbcd(-123451234567890);
763 /**********************************************/
765 #define TEST_BCD(op, op0, cc_in, cc_mask)\
775 : "=a" (res), "=g" (flags)\
776 : "0" (res), "1" (flags));\
777 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
778 #op, op0, res, cc_in, flags & cc_mask);\
783 TEST_BCD(daa
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
784 TEST_BCD(daa
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
785 TEST_BCD(daa
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
786 TEST_BCD(daa
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
787 TEST_BCD(daa
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
788 TEST_BCD(daa
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
789 TEST_BCD(daa
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
790 TEST_BCD(daa
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
791 TEST_BCD(daa
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
792 TEST_BCD(daa
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
793 TEST_BCD(daa
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
794 TEST_BCD(daa
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
795 TEST_BCD(daa
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
797 TEST_BCD(das
, 0x12340503, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
798 TEST_BCD(das
, 0x12340506, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
799 TEST_BCD(das
, 0x12340507, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
800 TEST_BCD(das
, 0x12340559, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
801 TEST_BCD(das
, 0x12340560, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
802 TEST_BCD(das
, 0x1234059f, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
803 TEST_BCD(das
, 0x123405a0, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
804 TEST_BCD(das
, 0x12340503, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
805 TEST_BCD(das
, 0x12340506, 0, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
806 TEST_BCD(das
, 0x12340503, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
807 TEST_BCD(das
, 0x12340506, CC_C
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
808 TEST_BCD(das
, 0x12340503, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
809 TEST_BCD(das
, 0x12340506, CC_C
| CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_A
));
811 TEST_BCD(aaa
, 0x12340205, CC_A
, (CC_C
| CC_A
));
812 TEST_BCD(aaa
, 0x12340306, CC_A
, (CC_C
| CC_A
));
813 TEST_BCD(aaa
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
814 TEST_BCD(aaa
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
815 TEST_BCD(aaa
, 0x12340205, 0, (CC_C
| CC_A
));
816 TEST_BCD(aaa
, 0x12340306, 0, (CC_C
| CC_A
));
817 TEST_BCD(aaa
, 0x1234040a, 0, (CC_C
| CC_A
));
818 TEST_BCD(aaa
, 0x123405fa, 0, (CC_C
| CC_A
));
820 TEST_BCD(aas
, 0x12340205, CC_A
, (CC_C
| CC_A
));
821 TEST_BCD(aas
, 0x12340306, CC_A
, (CC_C
| CC_A
));
822 TEST_BCD(aas
, 0x1234040a, CC_A
, (CC_C
| CC_A
));
823 TEST_BCD(aas
, 0x123405fa, CC_A
, (CC_C
| CC_A
));
824 TEST_BCD(aas
, 0x12340205, 0, (CC_C
| CC_A
));
825 TEST_BCD(aas
, 0x12340306, 0, (CC_C
| CC_A
));
826 TEST_BCD(aas
, 0x1234040a, 0, (CC_C
| CC_A
));
827 TEST_BCD(aas
, 0x123405fa, 0, (CC_C
| CC_A
));
829 TEST_BCD(aam
, 0x12340547, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
830 TEST_BCD(aad
, 0x12340407, CC_A
, (CC_C
| CC_P
| CC_Z
| CC_S
| CC_O
| CC_A
));
833 #define TEST_XCHG(op, size, opconst)\
838 asm(#op " %" size "0, %" size "1" \
839 : "=q" (op0), opconst (op1) \
840 : "0" (op0), "1" (op1));\
841 printf("%-10s A=%08x B=%08x\n",\
845 #define TEST_CMPXCHG(op, size, opconst, eax)\
850 asm(#op " %" size "0, %" size "1" \
851 : "=q" (op0), opconst (op1) \
852 : "0" (op0), "1" (op1), "a" (eax));\
853 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
854 #op, eax, op0, op1);\
859 TEST_XCHG(xchgl
, "", "=q");
860 TEST_XCHG(xchgw
, "w", "=q");
861 TEST_XCHG(xchgb
, "b", "=q");
863 TEST_XCHG(xchgl
, "", "=m");
864 TEST_XCHG(xchgw
, "w", "=m");
865 TEST_XCHG(xchgb
, "b", "=m");
867 TEST_XCHG(xaddl
, "", "=q");
868 TEST_XCHG(xaddw
, "w", "=q");
869 TEST_XCHG(xaddb
, "b", "=q");
874 asm("xaddl %1, %0" : "=r" (res
) : "0" (res
));
875 printf("xaddl same res=%08x\n", res
);
878 TEST_XCHG(xaddl
, "", "=m");
879 TEST_XCHG(xaddw
, "w", "=m");
880 TEST_XCHG(xaddb
, "b", "=m");
882 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfbca7654);
883 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfbca7654);
884 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfbca7654);
886 TEST_CMPXCHG(cmpxchgl
, "", "=q", 0xfffefdfc);
887 TEST_CMPXCHG(cmpxchgw
, "w", "=q", 0xfffefdfc);
888 TEST_CMPXCHG(cmpxchgb
, "b", "=q", 0xfffefdfc);
890 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfbca7654);
891 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfbca7654);
892 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfbca7654);
894 TEST_CMPXCHG(cmpxchgl
, "", "=m", 0xfffefdfc);
895 TEST_CMPXCHG(cmpxchgw
, "w", "=m", 0xfffefdfc);
896 TEST_CMPXCHG(cmpxchgb
, "b", "=m", 0xfffefdfc);
899 uint64_t op0
, op1
, op2
;
902 for(i
= 0; i
< 2; i
++) {
903 op0
= 0x123456789abcd;
905 op1
= 0xfbca765423456;
908 op2
= 0x6532432432434;
912 : "=A" (op0
), "=m" (op1
), "=g" (eflags
)
913 : "0" (op0
), "m" (op1
), "b" ((int)op2
), "c" ((int)(op2
>> 32)));
914 printf("cmpxchg8b: op0=%016llx op1=%016llx CC=%02x\n",
915 op0
, op1
, eflags
& CC_Z
);
920 /**********************************************/
921 /* segmentation tests */
924 #include <linux/unistd.h>
925 #include <linux/version.h>
927 _syscall3(int, modify_ldt
, int, func
, void *, ptr
, unsigned long, bytecount
)
929 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
930 #define modify_ldt_ldt_s user_desc
933 uint8_t seg_data1
[4096];
934 uint8_t seg_data2
[4096];
936 #define MK_SEL(n) (((n) << 3) | 7)
938 #define TEST_LR(op, size, seg, mask)\
942 asm (op " %" size "2, %" size "0\n" \
947 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
948 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
951 /* NOTE: we use Linux modify_ldt syscall */
954 struct modify_ldt_ldt_s ldt
;
955 long long ldt_table
[3];
961 } __attribute__((packed
)) segoff
;
963 ldt
.entry_number
= 1;
964 ldt
.base_addr
= (unsigned long)&seg_data1
;
965 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
967 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
968 ldt
.read_exec_only
= 0;
969 ldt
.limit_in_pages
= 1;
970 ldt
.seg_not_present
= 0;
972 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
974 ldt
.entry_number
= 2;
975 ldt
.base_addr
= (unsigned long)&seg_data2
;
976 ldt
.limit
= (sizeof(seg_data2
) + 0xfff) >> 12;
978 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
979 ldt
.read_exec_only
= 0;
980 ldt
.limit_in_pages
= 1;
981 ldt
.seg_not_present
= 0;
983 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
985 modify_ldt(0, &ldt_table
, sizeof(ldt_table
)); /* read ldt entries */
990 printf("%d: %016Lx\n", i
, ldt_table
[i
]);
993 /* do some tests with fs or gs */
994 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
999 asm volatile ("fs movzbl 0x1, %0" : "=r" (res
));
1000 printf("FS[1] = %02x\n", res
);
1002 asm volatile ("pushl %%gs\n"
1004 "gs movzbl 0x1, %0\n"
1008 printf("GS[1] = %02x\n", res
);
1010 /* tests with ds/ss (implicit segment case) */
1012 asm volatile ("pushl %%ebp\n\t"
1015 "movl %3, %%ebp\n\t"
1016 "movzbl 0x1, %0\n\t"
1017 "movzbl (%%ebp), %1\n\t"
1020 : "=r" (res
), "=r" (res2
)
1021 : "r" (MK_SEL(1)), "r" (&tmp
));
1022 printf("DS[1] = %02x\n", res
);
1023 printf("SS[tmp] = %02x\n", res2
);
1025 segoff
.seg
= MK_SEL(2);
1026 segoff
.offset
= 0xabcdef12;
1027 asm volatile("lfs %2, %0\n\t"
1029 : "=r" (res
), "=g" (res2
)
1031 printf("FS:reg = %04x:%08x\n", res2
, res
);
1033 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1034 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1035 TEST_LR("lslw", "w", MK_SEL(2), 0);
1036 TEST_LR("lsll", "", MK_SEL(2), 0);
1038 TEST_LR("larw", "w", 0xfff8, 0);
1039 TEST_LR("larl", "", 0xfff8, 0);
1040 TEST_LR("lslw", "w", 0xfff8, 0);
1041 TEST_LR("lsll", "", 0xfff8, 0);
1044 /* 16 bit code test */
1045 extern char code16_start
, code16_end
;
1046 extern char code16_func1
;
1047 extern char code16_func2
;
1048 extern char code16_func3
;
1050 void test_code16(void)
1052 struct modify_ldt_ldt_s ldt
;
1055 /* build a code segment */
1056 ldt
.entry_number
= 1;
1057 ldt
.base_addr
= (unsigned long)&code16_start
;
1058 ldt
.limit
= &code16_end
- &code16_start
;
1060 ldt
.contents
= MODIFY_LDT_CONTENTS_CODE
;
1061 ldt
.read_exec_only
= 0;
1062 ldt
.limit_in_pages
= 0;
1063 ldt
.seg_not_present
= 0;
1065 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1067 /* call the first function */
1068 asm volatile ("lcall %1, %2"
1070 : "i" (MK_SEL(1)), "i" (&code16_func1
): "memory", "cc");
1071 printf("func1() = 0x%08x\n", res
);
1072 asm volatile ("lcall %2, %3"
1073 : "=a" (res
), "=c" (res2
)
1074 : "i" (MK_SEL(1)), "i" (&code16_func2
): "memory", "cc");
1075 printf("func2() = 0x%08x spdec=%d\n", res
, res2
);
1076 asm volatile ("lcall %1, %2"
1078 : "i" (MK_SEL(1)), "i" (&code16_func3
): "memory", "cc");
1079 printf("func3() = 0x%08x\n", res
);
1082 extern char func_lret32
;
1083 extern char func_iret32
;
1085 void test_misc(void)
1090 for(i
=0;i
<256;i
++) table
[i
] = 256 - i
;
1092 asm ("xlat" : "=a" (res
) : "b" (table
), "0" (res
));
1093 printf("xlat: EAX=%08x\n", res
);
1095 asm volatile ("pushl %%cs ; call %1"
1097 : "m" (func_lret32
): "memory", "cc");
1098 printf("func_lret32=%x\n", res
);
1100 asm volatile ("pushfl ; pushl %%cs ; call %1"
1102 : "m" (func_iret32
): "memory", "cc");
1103 printf("func_iret32=%x\n", res
);
1105 /* specific popl test */
1106 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1108 printf("popl esp=%x\n", res
);
1110 /* specific popw test */
1111 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1113 printf("popw esp=%x\n", res
);
1116 uint8_t str_buffer
[4096];
1118 #define TEST_STRING1(OP, size, DF, REP)\
1120 int esi, edi, eax, ecx, eflags;\
1122 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1123 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1127 asm volatile ("pushl $0\n\t"\
1130 REP #OP size "\n\t"\
1134 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1135 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1136 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1137 REP #OP size, esi, edi, eax, ecx,\
1138 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1141 #define TEST_STRING(OP, REP)\
1142 TEST_STRING1(OP, "b", "", REP);\
1143 TEST_STRING1(OP, "w", "", REP);\
1144 TEST_STRING1(OP, "l", "", REP);\
1145 TEST_STRING1(OP, "b", "std", REP);\
1146 TEST_STRING1(OP, "w", "std", REP);\
1147 TEST_STRING1(OP, "l", "std", REP)
1149 void test_string(void)
1152 for(i
= 0;i
< sizeof(str_buffer
); i
++)
1153 str_buffer
[i
] = i
+ 0x56;
1154 TEST_STRING(stos
, "");
1155 TEST_STRING(stos
, "rep ");
1156 TEST_STRING(lods
, ""); /* to verify stos */
1157 TEST_STRING(lods
, "rep ");
1158 TEST_STRING(movs
, "");
1159 TEST_STRING(movs
, "rep ");
1160 TEST_STRING(lods
, ""); /* to verify stos */
1162 /* XXX: better tests */
1163 TEST_STRING(scas
, "");
1164 TEST_STRING(scas
, "repz ");
1165 TEST_STRING(scas
, "repnz ");
1166 TEST_STRING(cmps
, "");
1167 TEST_STRING(cmps
, "repz ");
1168 TEST_STRING(cmps
, "repnz ");
1173 static inline void set_bit(uint8_t *a
, unsigned int bit
)
1175 a
[bit
/ 8] |= (1 << (bit
% 8));
1178 static inline uint8_t *seg_to_linear(unsigned int seg
, unsigned int reg
)
1180 return (uint8_t *)((seg
<< 4) + (reg
& 0xffff));
1183 static inline void pushw(struct vm86_regs
*r
, int val
)
1185 r
->esp
= (r
->esp
& ~0xffff) | ((r
->esp
- 2) & 0xffff);
1186 *(uint16_t *)seg_to_linear(r
->ss
, r
->esp
) = val
;
1189 #undef __syscall_return
1190 #define __syscall_return(type, res) \
1192 return (type) (res); \
1195 _syscall2(int, vm86
, int, func
, struct vm86plus_struct
*, v86
)
1197 extern char vm86_code_start
;
1198 extern char vm86_code_end
;
1200 #define VM86_CODE_CS 0x100
1201 #define VM86_CODE_IP 0x100
1203 void test_vm86(void)
1205 struct vm86plus_struct ctx
;
1206 struct vm86_regs
*r
;
1210 vm86_mem
= mmap((void *)0x00000000, 0x110000,
1211 PROT_WRITE
| PROT_READ
| PROT_EXEC
,
1212 MAP_FIXED
| MAP_ANON
| MAP_PRIVATE
, -1, 0);
1213 if (vm86_mem
== MAP_FAILED
) {
1214 printf("ERROR: could not map vm86 memory");
1217 memset(&ctx
, 0, sizeof(ctx
));
1219 /* init basic registers */
1221 r
->eip
= VM86_CODE_IP
;
1230 r
->eflags
= VIF_MASK
;
1232 /* move code to proper address. We use the same layout as a .com
1234 memcpy(vm86_mem
+ (VM86_CODE_CS
<< 4) + VM86_CODE_IP
,
1235 &vm86_code_start
, &vm86_code_end
- &vm86_code_start
);
1237 /* mark int 0x21 as being emulated */
1238 set_bit((uint8_t *)&ctx
.int_revectored
, 0x21);
1241 ret
= vm86(VM86_ENTER
, &ctx
);
1242 switch(VM86_TYPE(ret
)) {
1247 int_num
= VM86_ARG(ret
);
1248 if (int_num
!= 0x21)
1250 ah
= (r
->eax
>> 8) & 0xff;
1252 case 0x00: /* exit */
1254 case 0x02: /* write char */
1260 case 0x09: /* write string */
1263 ptr
= seg_to_linear(r
->ds
, r
->edx
);
1270 r
->eax
= (r
->eax
& ~0xff) | '$';
1273 case 0xff: /* extension: write eflags number in edx */
1275 #ifndef LINUX_VM86_IOPL_FIX
1278 printf("%08x\n", v
);
1282 printf("unsupported int 0x%02x\n", int_num
);
1288 /* a signal came, we just ignore that */
1293 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret
);
1298 printf("VM86 end\n");
1299 munmap(vm86_mem
, 0x110000);
1302 /* exception tests */
1314 #define REG_TRAPNO TRAPNO
1322 void sig_handler(int sig
, siginfo_t
*info
, void *puc
)
1324 struct ucontext
*uc
= puc
;
1326 printf("si_signo=%d si_errno=%d si_code=%d",
1327 info
->si_signo
, info
->si_errno
, info
->si_code
);
1328 printf(" si_addr=0x%08lx",
1329 (unsigned long)info
->si_addr
);
1332 printf("trapno=0x%02x err=0x%08x",
1333 uc
->uc_mcontext
.gregs
[REG_TRAPNO
],
1334 uc
->uc_mcontext
.gregs
[REG_ERR
]);
1335 printf(" EIP=0x%08x", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1337 longjmp(jmp_env
, 1);
1340 void test_exceptions(void)
1342 struct modify_ldt_ldt_s ldt
;
1343 struct sigaction act
;
1346 act
.sa_sigaction
= sig_handler
;
1347 sigemptyset(&act
.sa_mask
);
1348 act
.sa_flags
= SA_SIGINFO
;
1349 sigaction(SIGFPE
, &act
, NULL
);
1350 sigaction(SIGILL
, &act
, NULL
);
1351 sigaction(SIGSEGV
, &act
, NULL
);
1352 sigaction(SIGBUS
, &act
, NULL
);
1353 sigaction(SIGTRAP
, &act
, NULL
);
1355 /* test division by zero reporting */
1356 printf("DIVZ exception:\n");
1357 if (setjmp(jmp_env
) == 0) {
1358 /* now divide by zero */
1363 printf("BOUND exception:\n");
1364 if (setjmp(jmp_env
) == 0) {
1365 /* bound exception */
1368 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab
[0]));
1371 printf("segment exceptions:\n");
1372 if (setjmp(jmp_env
) == 0) {
1373 /* load an invalid segment */
1374 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1376 if (setjmp(jmp_env
) == 0) {
1377 /* null data segment is valid */
1378 asm volatile ("movl %0, %%fs" : : "r" (3));
1379 /* null stack segment */
1380 asm volatile ("movl %0, %%ss" : : "r" (3));
1383 ldt
.entry_number
= 1;
1384 ldt
.base_addr
= (unsigned long)&seg_data1
;
1385 ldt
.limit
= (sizeof(seg_data1
) + 0xfff) >> 12;
1387 ldt
.contents
= MODIFY_LDT_CONTENTS_DATA
;
1388 ldt
.read_exec_only
= 0;
1389 ldt
.limit_in_pages
= 1;
1390 ldt
.seg_not_present
= 1;
1392 modify_ldt(1, &ldt
, sizeof(ldt
)); /* write ldt entry */
1394 if (setjmp(jmp_env
) == 0) {
1395 /* segment not present */
1396 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1399 /* test SEGV reporting */
1400 printf("PF exception:\n");
1401 if (setjmp(jmp_env
) == 0) {
1403 /* we add a nop to test a weird PC retrieval case */
1404 asm volatile ("nop");
1405 /* now store in an invalid address */
1406 *(char *)0x1234 = 1;
1409 /* test SEGV reporting */
1410 printf("PF exception:\n");
1411 if (setjmp(jmp_env
) == 0) {
1413 /* read from an invalid address */
1414 v1
= *(char *)0x1234;
1417 /* test illegal instruction reporting */
1418 printf("UD2 exception:\n");
1419 if (setjmp(jmp_env
) == 0) {
1420 /* now execute an invalid instruction */
1421 asm volatile("ud2");
1423 printf("lock nop exception:\n");
1424 if (setjmp(jmp_env
) == 0) {
1425 /* now execute an invalid instruction */
1426 asm volatile("lock nop");
1429 printf("INT exception:\n");
1430 if (setjmp(jmp_env
) == 0) {
1431 asm volatile ("int $0xfd");
1433 if (setjmp(jmp_env
) == 0) {
1434 asm volatile ("int $0x01");
1436 if (setjmp(jmp_env
) == 0) {
1437 asm volatile (".byte 0xcd, 0x03");
1439 if (setjmp(jmp_env
) == 0) {
1440 asm volatile ("int $0x04");
1442 if (setjmp(jmp_env
) == 0) {
1443 asm volatile ("int $0x05");
1446 printf("INT3 exception:\n");
1447 if (setjmp(jmp_env
) == 0) {
1448 asm volatile ("int3");
1451 printf("CLI exception:\n");
1452 if (setjmp(jmp_env
) == 0) {
1453 asm volatile ("cli");
1456 printf("STI exception:\n");
1457 if (setjmp(jmp_env
) == 0) {
1458 asm volatile ("cli");
1461 printf("INTO exception:\n");
1462 if (setjmp(jmp_env
) == 0) {
1463 /* overflow exception */
1464 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1467 printf("OUTB exception:\n");
1468 if (setjmp(jmp_env
) == 0) {
1469 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1472 printf("INB exception:\n");
1473 if (setjmp(jmp_env
) == 0) {
1474 asm volatile ("inb %%dx, %%al" : "=a" (val
) : "d" (0x4321));
1477 printf("REP OUTSB exception:\n");
1478 if (setjmp(jmp_env
) == 0) {
1479 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab
), "c" (1));
1482 printf("REP INSB exception:\n");
1483 if (setjmp(jmp_env
) == 0) {
1484 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab
), "c" (1));
1487 printf("HLT exception:\n");
1488 if (setjmp(jmp_env
) == 0) {
1489 asm volatile ("hlt");
1492 printf("single step exception:\n");
1494 if (setjmp(jmp_env
) == 0) {
1495 asm volatile ("pushf\n"
1496 "orl $0x00100, (%%esp)\n"
1498 "movl $0xabcd, %0\n"
1499 "movl $0x0, %0\n" : "=m" (val
) : : "cc", "memory");
1501 printf("val=0x%x\n", val
);
1504 /* specific precise single step test */
1505 void sig_trap_handler(int sig
, siginfo_t
*info
, void *puc
)
1507 struct ucontext
*uc
= puc
;
1508 printf("EIP=0x%08x\n", uc
->uc_mcontext
.gregs
[REG_EIP
]);
1511 const uint8_t sstep_buf1
[4] = { 1, 2, 3, 4};
1512 uint8_t sstep_buf2
[4];
1514 void test_single_step(void)
1516 struct sigaction act
;
1521 act
.sa_sigaction
= sig_trap_handler
;
1522 sigemptyset(&act
.sa_mask
);
1523 act
.sa_flags
= SA_SIGINFO
;
1524 sigaction(SIGTRAP
, &act
, NULL
);
1525 asm volatile ("pushf\n"
1526 "orl $0x00100, (%%esp)\n"
1528 "movl $0xabcd, %0\n"
1537 /* movsb: the single step should stop at each movsb iteration */
1538 "movl $sstep_buf1, %%esi\n"
1539 "movl $sstep_buf2, %%edi\n"
1547 /* cmpsb: the single step should stop at each cmpsb iteration */
1548 "movl $sstep_buf1, %%esi\n"
1549 "movl $sstep_buf2, %%edi\n"
1555 /* getpid() syscall: single step should skip one
1561 /* when modifying SS, trace is not done on the next
1563 "movl %%ss, %%ecx\n"
1564 "movl %%ecx, %%ss\n"
1567 "movl %%ecx, %%ss\n"
1578 "andl $~0x00100, (%%esp)\n"
1582 : "cc", "memory", "eax", "ecx", "esi", "edi");
1583 printf("val=%d\n", val
);
1584 for(i
= 0; i
< 4; i
++)
1585 printf("sstep_buf2[%d] = %d\n", i
, sstep_buf2
[i
]);
1588 /* self modifying code test */
1590 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1595 "movl 4(%esp), %eax\n"
1596 "movl %eax, smc_patch_addr2 + 1\n"
1605 "smc_patch_addr2:\n"
1609 typedef int FuncType(void);
1610 extern int smc_code2(int);
1611 void test_self_modifying_code(void)
1615 printf("self modifying code:\n");
1616 printf("func1 = 0x%x\n", ((FuncType
*)code
)());
1617 for(i
= 2; i
<= 4; i
++) {
1619 printf("func%d = 0x%x\n", i
, ((FuncType
*)code
)());
1622 /* more difficult test : the modified code is just after the
1623 modifying instruction. It is forbidden in Intel specs, but it
1624 is used by old DOS programs */
1625 for(i
= 2; i
<= 4; i
++) {
1626 printf("smc_code2(%d) = %d\n", i
, smc_code2(i
));
1630 int enter_stack
[4096];
1632 #define TEST_ENTER(size, stack_type, level)\
1634 int esp_save, esp_val, ebp_val, ebp_save, i;\
1635 stack_type *ptr, *stack_end, *stack_ptr;\
1636 memset(enter_stack, 0, sizeof(enter_stack));\
1637 stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
1638 ebp_val = (long)stack_ptr;\
1641 esp_val = (long)stack_ptr;\
1642 asm("movl %%esp, %[esp_save]\n"\
1643 "movl %%ebp, %[ebp_save]\n"\
1644 "movl %[esp_val], %%esp\n"\
1645 "movl %[ebp_val], %%ebp\n"\
1646 "enter" size " $12, $" #level "\n"\
1647 "movl %%esp, %[esp_val]\n"\
1648 "movl %%ebp, %[ebp_val]\n"\
1649 "movl %[esp_save], %%esp\n"\
1650 "movl %[ebp_save], %%ebp\n"\
1651 : [esp_save] "=r" (esp_save),\
1652 [ebp_save] "=r" (ebp_save),\
1653 [esp_val] "=r" (esp_val),\
1654 [ebp_val] "=r" (ebp_val)\
1655 : "[esp_val]" (esp_val),\
1656 "[ebp_val]" (ebp_val));\
1657 printf("level=%d:\n", level);\
1658 printf("esp_val=0x%08lx\n", esp_val - (long)stack_end);\
1659 printf("ebp_val=0x%08lx\n", ebp_val - (long)stack_end);\
1660 for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
1661 printf("%08x\n", ptr[0]);\
1664 static void test_enter(void)
1666 TEST_ENTER("l", uint32_t, 0);
1667 TEST_ENTER("l", uint32_t, 1);
1668 TEST_ENTER("l", uint32_t, 2);
1669 TEST_ENTER("l", uint32_t, 31);
1671 TEST_ENTER("w", uint16_t, 0);
1672 TEST_ENTER("w", uint16_t, 1);
1673 TEST_ENTER("w", uint16_t, 2);
1674 TEST_ENTER("w", uint16_t, 31);
1677 static void *call_end __init_call
= NULL
;
1679 int main(int argc
, char **argv
)
1684 ptr
= &call_start
+ 1;
1685 while (*ptr
!= NULL
) {
1704 test_self_modifying_code();