mc-manual.xml fix new/delete expresions -> expression
[valgrind.git] / VEX / test / test-i386.c
bloba948519e1529ce89e8f22ebba35d06bc158bd958
1 /*
2 * x86 CPU test
3 *
4 * Copyright (c) 2003 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #define _GNU_SOURCE
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <inttypes.h>
24 #include <math.h>
25 #include <signal.h>
26 #include <setjmp.h>
27 #include <errno.h>
28 #include <sys/ucontext.h>
29 #include <sys/mman.h>
30 #include <asm/vm86.h>
32 /* Setting this to 1 creates a very comprehensive test of
33 integer condition codes. */
34 #define TEST_INTEGER_VERBOSE 1
37 //#define LINUX_VM86_IOPL_FIX
38 //#define TEST_P4_FLAGS
40 #define xglue(x, y) x ## y
41 #define glue(x, y) xglue(x, y)
42 #define stringify(s) tostring(s)
43 #define tostring(s) #s
45 #define CC_C 0x0001
46 #define CC_P 0x0004
47 #define CC_A 0x0010
48 #define CC_Z 0x0040
49 #define CC_S 0x0080
50 #define CC_O 0x0800
52 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
54 static void *call_start __init_call = NULL;
56 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
58 #define OP add
59 #include "test-i386.h"
61 #define OP sub
62 #include "test-i386.h"
64 #define OP xor
65 #include "test-i386.h"
67 #define OP and
68 #include "test-i386.h"
70 #define OP or
71 #include "test-i386.h"
73 #define OP cmp
74 #include "test-i386.h"
76 #define OP adc
77 #define OP_CC
78 #include "test-i386.h"
80 #define OP sbb
81 #define OP_CC
82 #include "test-i386.h"
84 #define OP inc
85 #define OP_CC
86 #define OP1
87 #include "test-i386.h"
89 #define OP dec
90 #define OP_CC
91 #define OP1
92 #include "test-i386.h"
94 #define OP neg
95 #define OP_CC
96 #define OP1
97 #include "test-i386.h"
99 #define OP not
100 #define OP_CC
101 #define OP1
102 #include "test-i386.h"
104 #undef CC_MASK
105 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
107 #define OP shl
108 #include "test-i386-shift.h"
110 #define OP shr
111 #include "test-i386-shift.h"
113 #define OP sar
114 #include "test-i386-shift.h"
116 #define OP rol
117 #include "test-i386-shift.h"
119 #define OP ror
120 #include "test-i386-shift.h"
122 #define OP rcr
123 #define OP_CC
124 #include "test-i386-shift.h"
126 #define OP rcl
127 #define OP_CC
128 #include "test-i386-shift.h"
130 #define OP shld
131 #define OP_SHIFTD
132 #define OP_NOBYTE
133 #include "test-i386-shift.h"
135 #define OP shrd
136 #define OP_SHIFTD
137 #define OP_NOBYTE
138 #include "test-i386-shift.h"
141 /* XXX: should be more precise ? */
142 #undef CC_MASK
143 #define CC_MASK (CC_C)
145 #define OP bt
146 #define OP_NOBYTE
147 #include "test-i386-shift.h"
149 #define OP bts
150 #define OP_NOBYTE
151 #include "test-i386-shift.h"
153 #define OP btr
154 #define OP_NOBYTE
155 #include "test-i386-shift.h"
157 #define OP btc
158 #define OP_NOBYTE
159 #include "test-i386-shift.h"
162 /* lea test (modrm support) */
163 #define TEST_LEA(STR)\
165 asm("leal " STR ", %0"\
166 : "=r" (res)\
167 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
168 printf("lea %s = %08x\n", STR, res);\
171 #define TEST_LEA16(STR)\
173 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
174 : "=wq" (res)\
175 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
176 printf("lea %s = %08x\n", STR, res);\
180 void test_lea(void)
182 int eax, ebx, ecx, edx, esi, edi, res;
183 eax = 0x0001;
184 ebx = 0x0002;
185 ecx = 0x0004;
186 edx = 0x0008;
187 esi = 0x0010;
188 edi = 0x0020;
190 TEST_LEA("0x4000");
192 TEST_LEA("(%%eax)");
193 TEST_LEA("(%%ebx)");
194 TEST_LEA("(%%ecx)");
195 TEST_LEA("(%%edx)");
196 TEST_LEA("(%%esi)");
197 TEST_LEA("(%%edi)");
199 TEST_LEA("0x40(%%eax)");
200 TEST_LEA("0x40(%%ebx)");
201 TEST_LEA("0x40(%%ecx)");
202 TEST_LEA("0x40(%%edx)");
203 TEST_LEA("0x40(%%esi)");
204 TEST_LEA("0x40(%%edi)");
206 TEST_LEA("0x4000(%%eax)");
207 TEST_LEA("0x4000(%%ebx)");
208 TEST_LEA("0x4000(%%ecx)");
209 TEST_LEA("0x4000(%%edx)");
210 TEST_LEA("0x4000(%%esi)");
211 TEST_LEA("0x4000(%%edi)");
213 TEST_LEA("(%%eax, %%ecx)");
214 TEST_LEA("(%%ebx, %%edx)");
215 TEST_LEA("(%%ecx, %%ecx)");
216 TEST_LEA("(%%edx, %%ecx)");
217 TEST_LEA("(%%esi, %%ecx)");
218 TEST_LEA("(%%edi, %%ecx)");
220 TEST_LEA("0x40(%%eax, %%ecx)");
221 TEST_LEA("0x4000(%%ebx, %%edx)");
223 TEST_LEA("(%%ecx, %%ecx, 2)");
224 TEST_LEA("(%%edx, %%ecx, 4)");
225 TEST_LEA("(%%esi, %%ecx, 8)");
227 TEST_LEA("(,%%eax, 2)");
228 TEST_LEA("(,%%ebx, 4)");
229 TEST_LEA("(,%%ecx, 8)");
231 TEST_LEA("0x40(,%%eax, 2)");
232 TEST_LEA("0x40(,%%ebx, 4)");
233 TEST_LEA("0x40(,%%ecx, 8)");
236 TEST_LEA("-10(%%ecx, %%ecx, 2)");
237 TEST_LEA("-10(%%edx, %%ecx, 4)");
238 TEST_LEA("-10(%%esi, %%ecx, 8)");
240 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
241 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
242 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
245 #define TEST_JCC(JCC, v1, v2)\
247 int res;\
248 asm("movl $1, %0\n\t"\
249 "cmpl %2, %1\n\t"\
250 "j" JCC " 1f\n\t"\
251 "movl $0, %0\n\t"\
252 "1:\n\t"\
253 : "=r" (res)\
254 : "r" (v1), "r" (v2));\
255 printf("%-10s %d\n", "j" JCC, res);\
257 asm("movl $0, %0\n\t"\
258 "cmpl %2, %1\n\t"\
259 "set" JCC " %b0\n\t"\
260 : "=r" (res)\
261 : "r" (v1), "r" (v2));\
262 printf("%-10s %d\n", "set" JCC, res);\
263 { int one = 1; \
264 asm("movl $0x12345678, %0\n\t"\
265 "cmpl %2, %1\n\t"\
266 "cmov" JCC "l %3, %0\n\t"\
267 : "=r" (res)\
268 : "r" (v1), "r" (v2), "m" (one));\
269 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
270 asm("movl $0x12345678, %0\n\t"\
271 "cmpl %2, %1\n\t"\
272 "cmov" JCC "w %w3, %w0\n\t"\
273 : "=r" (res)\
274 : "r" (v1), "r" (v2), "r" (1));\
275 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
279 /* various jump tests */
280 void test_jcc(void)
282 TEST_JCC("ne", 1, 1);
283 TEST_JCC("ne", 1, 0);
285 TEST_JCC("e", 1, 1);
286 TEST_JCC("e", 1, 0);
288 TEST_JCC("l", 1, 1);
289 TEST_JCC("l", 1, 0);
290 TEST_JCC("l", 1, -1);
292 TEST_JCC("le", 1, 1);
293 TEST_JCC("le", 1, 0);
294 TEST_JCC("le", 1, -1);
296 TEST_JCC("ge", 1, 1);
297 TEST_JCC("ge", 1, 0);
298 TEST_JCC("ge", -1, 1);
300 TEST_JCC("g", 1, 1);
301 TEST_JCC("g", 1, 0);
302 TEST_JCC("g", 1, -1);
304 TEST_JCC("b", 1, 1);
305 TEST_JCC("b", 1, 0);
306 TEST_JCC("b", 1, -1);
308 TEST_JCC("be", 1, 1);
309 TEST_JCC("be", 1, 0);
310 TEST_JCC("be", 1, -1);
312 TEST_JCC("ae", 1, 1);
313 TEST_JCC("ae", 1, 0);
314 TEST_JCC("ae", 1, -1);
316 TEST_JCC("a", 1, 1);
317 TEST_JCC("a", 1, 0);
318 TEST_JCC("a", 1, -1);
321 TEST_JCC("p", 1, 1);
322 TEST_JCC("p", 1, 0);
324 TEST_JCC("np", 1, 1);
325 TEST_JCC("np", 1, 0);
327 TEST_JCC("o", 0x7fffffff, 0);
328 TEST_JCC("o", 0x7fffffff, -1);
330 TEST_JCC("no", 0x7fffffff, 0);
331 TEST_JCC("no", 0x7fffffff, -1);
333 TEST_JCC("s", 0, 1);
334 TEST_JCC("s", 0, -1);
335 TEST_JCC("s", 0, 0);
337 TEST_JCC("ns", 0, 1);
338 TEST_JCC("ns", 0, -1);
339 TEST_JCC("ns", 0, 0);
342 #undef CC_MASK
343 #ifdef TEST_P4_FLAGS
344 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
345 #else
346 #define CC_MASK (CC_O | CC_C)
347 #endif
349 #define OP mul
350 #include "test-i386-muldiv.h"
352 #define OP imul
353 #include "test-i386-muldiv.h"
355 void test_imulw2(int op0, int op1)
357 int res, s1, s0, flags;
358 s0 = op0;
359 s1 = op1;
360 res = s0;
361 flags = 0;
362 asm ("push %4\n\t"
363 "popf\n\t"
364 "imulw %w2, %w0\n\t"
365 "pushf\n\t"
366 "popl %1\n\t"
367 : "=q" (res), "=g" (flags)
368 : "q" (s1), "0" (res), "1" (flags));
369 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
370 "imulw", s0, s1, res, flags & CC_MASK);
373 void test_imull2(int op0, int op1)
375 int res, s1, s0, flags;
376 s0 = op0;
377 s1 = op1;
378 res = s0;
379 flags = 0;
380 asm ("push %4\n\t"
381 "popf\n\t"
382 "imull %2, %0\n\t"
383 "pushf\n\t"
384 "popl %1\n\t"
385 : "=q" (res), "=g" (flags)
386 : "q" (s1), "0" (res), "1" (flags));
387 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
388 "imull", s0, s1, res, flags & CC_MASK);
391 #define TEST_IMUL_IM(size, size1, op0, op1)\
393 int res, flags;\
394 flags = 0;\
395 res = 0;\
396 asm ("push %3\n\t"\
397 "popf\n\t"\
398 "imul" size " $" #op0 ", %" size1 "2, %" size1 "0\n\t" \
399 "pushf\n\t"\
400 "popl %1\n\t"\
401 : "=r" (res), "=g" (flags)\
402 : "r" (op1), "1" (flags), "0" (res));\
403 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",\
404 "imul" size, op0, op1, res, flags & CC_MASK);\
408 #undef CC_MASK
409 #define CC_MASK (0)
411 #define OP div
412 #include "test-i386-muldiv.h"
414 #define OP idiv
415 #include "test-i386-muldiv.h"
417 void test_mul(void)
419 test_imulb(0x1234561d, 4);
420 test_imulb(3, -4);
421 test_imulb(0x80, 0x80);
422 test_imulb(0x10, 0x10);
424 test_imulw(0, 0, 0);
425 test_imulw(0, 0xFF, 0xFF);
426 test_imulw(0, 0xFF, 0x100);
427 test_imulw(0, 0x1234001d, 45);
428 test_imulw(0, 23, -45);
429 test_imulw(0, 0x8000, 0x8000);
430 test_imulw(0, 0x100, 0x100);
432 test_imull(0, 0, 0);
433 test_imull(0, 0xFFFF, 0xFFFF);
434 test_imull(0, 0xFFFF, 0x10000);
435 test_imull(0, 0x1234001d, 45);
436 test_imull(0, 23, -45);
437 test_imull(0, 0x80000000, 0x80000000);
438 test_imull(0, 0x10000, 0x10000);
440 test_mulb(0x1234561d, 4);
441 test_mulb(3, -4);
442 test_mulb(0x80, 0x80);
443 test_mulb(0x10, 0x10);
445 test_mulw(0, 0x1234001d, 45);
446 test_mulw(0, 23, -45);
447 test_mulw(0, 0x8000, 0x8000);
448 test_mulw(0, 0x100, 0x100);
450 test_mull(0, 0x1234001d, 45);
451 test_mull(0, 23, -45);
452 test_mull(0, 0x80000000, 0x80000000);
453 test_mull(0, 0x10000, 0x10000);
455 test_imulw2(0x1234001d, 45);
456 test_imulw2(23, -45);
457 test_imulw2(0x8000, 0x8000);
458 test_imulw2(0x100, 0x100);
460 test_imull2(0x1234001d, 45);
461 test_imull2(23, -45);
462 test_imull2(0x80000000, 0x80000000);
463 test_imull2(0x10000, 0x10000);
465 TEST_IMUL_IM("w", "w", 45, 0x1234);
466 TEST_IMUL_IM("w", "w", -45, 23);
467 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
468 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
470 TEST_IMUL_IM("l", "", 45, 0x1234);
471 TEST_IMUL_IM("l", "", -45, 23);
472 TEST_IMUL_IM("l", "", 0x8000, 0x80000000);
473 TEST_IMUL_IM("l", "", 0x7fff, 0x1000);
475 test_idivb(0x12341678, 0x127e);
476 test_idivb(0x43210123, -5);
477 test_idivb(0x12340004, -1);
479 test_idivw(0, 0x12345678, 12347);
480 test_idivw(0, -23223, -45);
481 test_idivw(0, 0x12348000, -1);
482 test_idivw(0x12343, 0x12345678, 0x81238567);
484 test_idivl(0, 0x12345678, 12347);
485 test_idivl(0, -233223, -45);
486 test_idivl(0, 0x80000000, -1);
487 test_idivl(0x12343, 0x12345678, 0x81234567);
489 test_divb(0x12341678, 0x127e);
490 test_divb(0x43210123, -5);
491 test_divb(0x12340004, -1);
493 test_divw(0, 0x12345678, 12347);
494 test_divw(0, -23223, -45);
495 test_divw(0, 0x12348000, -1);
496 test_divw(0x12343, 0x12345678, 0x81238567);
498 test_divl(0, 0x12345678, 12347);
499 test_divl(0, -233223, -45);
500 test_divl(0, 0x80000000, -1);
501 test_divl(0x12343, 0x12345678, 0x81234567);
504 #define TEST_BSX(op, size, op0)\
506 int res, val, resz;\
507 val = op0;\
508 asm("xorl %1, %1\n\t"\
509 "movl $0x12345678, %0\n\t"\
510 #op " %" size "2, %" size "0\n\t" \
511 "setz %b1" \
512 : "=r" (res), "=q" (resz)\
513 : "r" (val));\
514 printf("%-10s A=%08x R=%08x %d\n", #op, val, res, resz);\
517 void test_bsx(void)
519 TEST_BSX(bsrw, "w", 0);
520 TEST_BSX(bsrw, "w", 0x12340128);
521 TEST_BSX(bsrl, "", 0);
522 TEST_BSX(bsrl, "", 0x00340128);
523 TEST_BSX(bsfw, "w", 0);
524 TEST_BSX(bsfw, "w", 0x12340128);
525 TEST_BSX(bsfl, "", 0);
526 TEST_BSX(bsfl, "", 0x00340128);
529 /**********************************************/
531 void test_fops(double a, double b)
533 printf("a=%f b=%f a+b=%f\n", a, b, a + b);
534 printf("a=%f b=%f a-b=%f\n", a, b, a - b);
535 printf("a=%f b=%f a*b=%f\n", a, b, a * b);
536 printf("a=%f b=%f a/b=%f\n", a, b, a / b);
537 printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
538 printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
539 printf("a=%f sin(a)=%f\n", a, sin(a));
540 printf("a=%f cos(a)=%f\n", a, cos(a));
541 printf("a=%f tan(a)=%f\n", a, tan(a));
542 printf("a=%f log(a)=%f\n", a, log(a));
543 printf("a=%f exp(a)=%f\n", a, exp(a));
544 printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
545 /* just to test some op combining */
546 printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
547 printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
548 printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
551 void test_fcmp(double a, double b)
553 printf("(%f<%f)=%d\n",
554 a, b, a < b);
555 printf("(%f<=%f)=%d\n",
556 a, b, a <= b);
557 printf("(%f==%f)=%d\n",
558 a, b, a == b);
559 printf("(%f>%f)=%d\n",
560 a, b, a > b);
561 printf("(%f<=%f)=%d\n",
562 a, b, a >= b);
564 unsigned int eflags;
565 /* test f(u)comi instruction */
566 asm("fcomi %2, %1\n"
567 "pushf\n"
568 "pop %0\n"
569 : "=r" (eflags)
570 : "t" (a), "u" (b));
571 printf("fcomi(%f %f)=%08x\n", a, b, eflags & (CC_Z | CC_P | CC_C));
575 void test_fcvt(double a)
577 float fa;
578 long double la;
579 int16_t fpuc;
580 int i;
581 int64_t lla;
582 int ia;
583 int16_t wa;
584 double ra;
586 fa = a;
587 la = a;
588 printf("(float)%f = %f\n", a, fa);
589 printf("(long double)%f = %Lf\n", a, la);
590 printf("a=%016Lx\n", *(long long *)&a);
591 printf("la=%016Lx %04x\n", *(long long *)&la,
592 *(unsigned short *)((char *)(&la) + 8));
594 /* test all roundings */
595 asm volatile ("fstcw %0" : "=m" (fpuc));
596 for(i=0;i<4;i++) {
597 int16_t tmp = (fpuc & ~0x0c00) | (i << 10);
598 asm volatile ("fldcw %0" : : "m" (tmp));
599 asm volatile ("fist %0" : "=m" (wa) : "t" (a));
600 asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
601 asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
602 asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
603 asm volatile ("fldcw %0" : : "m" (fpuc));
604 printf("(short)a = %d\n", wa);
605 printf("(int)a = %d\n", ia);
606 printf("(int64_t)a = %Ld\n", lla);
607 printf("rint(a) = %f\n", ra);
611 #define TEST(N) \
612 asm("fld" #N : "=t" (a)); \
613 printf("fld" #N "= %f\n", a);
615 void test_fconst(void)
617 double a;
618 TEST(1);
619 TEST(l2t);
620 TEST(l2e);
621 TEST(pi);
622 TEST(lg2);
623 TEST(ln2);
624 TEST(z);
627 void test_fbcd(double a)
629 unsigned short bcd[5];
630 double b;
632 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
633 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
634 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
635 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
638 #define TEST_ENV(env, save, restore)\
640 memset((env), 0xaa, sizeof(*(env)));\
641 for(i=0;i<5;i++)\
642 asm volatile ("fldl %0" : : "m" (dtab[i]));\
643 asm(save " %0\n" : : "m" (*(env)));\
644 asm(restore " %0\n": : "m" (*(env)));\
645 for(i=0;i<5;i++)\
646 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
647 for(i=0;i<5;i++)\
648 printf("res[%d]=%f\n", i, rtab[i]);\
649 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
650 (env)->fpuc,\
651 (env)->fpus & 0xff00,\
652 (env)->fptag);\
655 void test_fenv(void)
657 struct __attribute__((packed)) {
658 uint16_t fpuc;
659 uint16_t dummy1;
660 uint16_t fpus;
661 uint16_t dummy2;
662 uint16_t fptag;
663 uint16_t dummy3;
664 uint32_t ignored[4];
665 long double fpregs[8];
666 } float_env32;
667 struct __attribute__((packed)) {
668 uint16_t fpuc;
669 uint16_t fpus;
670 uint16_t fptag;
671 uint16_t ignored[4];
672 long double fpregs[8];
673 } float_env16;
674 double dtab[8];
675 double rtab[8];
676 int i;
678 for(i=0;i<8;i++)
679 dtab[i] = i + 1;
681 TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
682 TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
683 TEST_ENV(&float_env32, "fnstenv", "fldenv");
684 TEST_ENV(&float_env32, "fnsave", "frstor");
686 /* test for ffree */
687 for(i=0;i<5;i++)
688 asm volatile ("fldl %0" : : "m" (dtab[i]));
689 asm volatile("ffree %st(2)");
690 asm volatile ("fnstenv %0\n" : : "m" (float_env32));
691 asm volatile ("fninit");
692 printf("fptag=%04x\n", float_env32.fptag);
696 #define TEST_FCMOV(a, b, eflags, CC)\
698 double res;\
699 asm("push %3\n"\
700 "popf\n"\
701 "fcmov" CC " %2, %0\n"\
702 : "=t" (res)\
703 : "0" (a), "u" (b), "g" (eflags));\
704 printf("fcmov%s eflags=0x%04x-> %f\n", \
705 CC, eflags, res);\
708 void test_fcmov(void)
710 double a, b;
711 int eflags, i;
713 a = 1.0;
714 b = 2.0;
715 for(i = 0; i < 4; i++) {
716 eflags = 0;
717 if (i & 1)
718 eflags |= CC_C;
719 if (i & 2)
720 eflags |= CC_Z;
721 TEST_FCMOV(a, b, eflags, "b");
722 TEST_FCMOV(a, b, eflags, "e");
723 TEST_FCMOV(a, b, eflags, "be");
724 TEST_FCMOV(a, b, eflags, "nb");
725 TEST_FCMOV(a, b, eflags, "ne");
726 TEST_FCMOV(a, b, eflags, "nbe");
728 TEST_FCMOV(a, b, 0, "u");
729 TEST_FCMOV(a, b, CC_P, "u");
730 TEST_FCMOV(a, b, 0, "nu");
731 TEST_FCMOV(a, b, CC_P, "nu");
734 void test_floats(void)
736 test_fops(2, 3);
737 test_fops(1.4, -5);
738 test_fcmp(2, -1);
739 test_fcmp(2, 2);
740 test_fcmp(2, 3);
741 test_fcvt(0.5);
742 test_fcvt(-0.5);
743 test_fcvt(1.0/7.0);
744 test_fcvt(-1.0/9.0);
745 test_fcvt(32768);
746 test_fcvt(-1e20);
747 test_fconst();
748 // REINSTATE (maybe): test_fbcd(1234567890123456);
749 // REINSTATE (maybe): test_fbcd(-123451234567890);
750 // REINSTATE: test_fenv();
751 // REINSTATE: test_fcmov();
754 /**********************************************/
755 #if 0
757 #define TEST_BCD(op, op0, cc_in, cc_mask)\
759 int res, flags;\
760 res = op0;\
761 flags = cc_in;\
762 asm ("push %3\n\t"\
763 "popf\n\t"\
764 #op "\n\t"\
765 "pushf\n\t"\
766 "popl %1\n\t"\
767 : "=a" (res), "=g" (flags)\
768 : "0" (res), "1" (flags));\
769 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
770 #op, op0, res, cc_in, flags & cc_mask);\
773 void test_bcd(void)
775 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
776 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
777 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
778 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
779 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
780 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
781 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
782 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
783 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
784 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
785 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
786 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
787 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
789 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
790 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
791 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
792 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
793 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
794 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
795 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
796 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
797 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
798 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
799 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
800 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
801 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
803 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
804 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
805 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
806 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
807 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
808 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
809 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
810 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
812 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
813 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
814 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
815 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
816 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
817 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
818 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
819 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
821 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
822 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
824 #endif /* 0 */
826 #define TEST_XCHG(op, size, opconst)\
828 int op0, op1;\
829 op0 = 0x12345678;\
830 op1 = 0xfbca7654;\
831 asm(#op " %" size "0, %" size "1" \
832 : "=q" (op0), opconst (op1) \
833 : "0" (op0), "1" (op1));\
834 printf("%-10s A=%08x B=%08x\n",\
835 #op, op0, op1);\
838 #define TEST_CMPXCHG(op, size, opconst, eax)\
840 int op0, op1;\
841 op0 = 0x12345678;\
842 op1 = 0xfbca7654;\
843 asm(#op " %" size "0, %" size "1" \
844 : "=q" (op0), opconst (op1) \
845 : "0" (op0), "1" (op1), "a" (eax));\
846 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
847 #op, eax, op0, op1);\
850 void test_xchg(void)
852 TEST_XCHG(xchgl, "", "=q");
853 TEST_XCHG(xchgw, "w", "=q");
854 TEST_XCHG(xchgb, "b", "=q");
856 TEST_XCHG(xchgl, "", "=m");
857 TEST_XCHG(xchgw, "w", "=m");
858 TEST_XCHG(xchgb, "b", "=m");
860 #if 0
861 TEST_XCHG(xaddl, "", "=q");
862 TEST_XCHG(xaddw, "w", "=q");
863 TEST_XCHG(xaddb, "b", "=q");
866 int res;
867 res = 0x12345678;
868 asm("xaddl %1, %0" : "=r" (res) : "0" (res));
869 printf("xaddl same res=%08x\n", res);
872 TEST_XCHG(xaddl, "", "=m");
873 TEST_XCHG(xaddw, "w", "=m");
874 TEST_XCHG(xaddb, "b", "=m");
875 #endif
876 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
877 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
878 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
880 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
881 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
882 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
884 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
885 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
886 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
888 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
889 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
890 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
891 #if 0
893 uint64_t op0, op1, op2;
894 int i, eflags;
896 for(i = 0; i < 2; i++) {
897 op0 = 0x123456789abcd;
898 if (i == 0)
899 op1 = 0xfbca765423456;
900 else
901 op1 = op0;
902 op2 = 0x6532432432434;
903 asm("cmpxchg8b %1\n"
904 "pushf\n"
905 "popl %2\n"
906 : "=A" (op0), "=m" (op1), "=g" (eflags)
907 : "0" (op0), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
908 printf("cmpxchg8b: op0=%016llx op1=%016llx CC=%02x\n",
909 op0, op1, eflags & CC_Z);
912 #endif
915 /**********************************************/
916 /* segmentation tests */
917 #if 0
918 #include <asm/ldt.h>
919 #include <linux/unistd.h>
920 #include <linux/version.h>
922 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
924 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
925 #define modify_ldt_ldt_s user_desc
926 #endif
928 uint8_t seg_data1[4096];
929 uint8_t seg_data2[4096];
931 #define MK_SEL(n) (((n) << 3) | 7)
933 #define TEST_LR(op, size, seg, mask)\
935 int res, res2;\
936 res = 0x12345678;\
937 asm (op " %" size "2, %" size "0\n" \
938 "movl $0, %1\n"\
939 "jnz 1f\n"\
940 "movl $1, %1\n"\
941 "1:\n"\
942 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
943 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
946 /* NOTE: we use Linux modify_ldt syscall */
947 void test_segs(void)
949 struct modify_ldt_ldt_s ldt;
950 long long ldt_table[3];
951 int res, res2;
952 char tmp;
953 struct {
954 uint32_t offset;
955 uint16_t seg;
956 } __attribute__((packed)) segoff;
958 ldt.entry_number = 1;
959 ldt.base_addr = (unsigned long)&seg_data1;
960 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
961 ldt.seg_32bit = 1;
962 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
963 ldt.read_exec_only = 0;
964 ldt.limit_in_pages = 1;
965 ldt.seg_not_present = 0;
966 ldt.useable = 1;
967 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
969 ldt.entry_number = 2;
970 ldt.base_addr = (unsigned long)&seg_data2;
971 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
972 ldt.seg_32bit = 1;
973 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
974 ldt.read_exec_only = 0;
975 ldt.limit_in_pages = 1;
976 ldt.seg_not_present = 0;
977 ldt.useable = 1;
978 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
980 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
981 #if 0
983 int i;
984 for(i=0;i<3;i++)
985 printf("%d: %016Lx\n", i, ldt_table[i]);
987 #endif
988 /* do some tests with fs or gs */
989 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
991 seg_data1[1] = 0xaa;
992 seg_data2[1] = 0x55;
994 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
995 printf("FS[1] = %02x\n", res);
997 asm volatile ("pushl %%gs\n"
998 "movl %1, %%gs\n"
999 "gs movzbl 0x1, %0\n"
1000 "popl %%gs\n"
1001 : "=r" (res)
1002 : "r" (MK_SEL(2)));
1003 printf("GS[1] = %02x\n", res);
1005 /* tests with ds/ss (implicit segment case) */
1006 tmp = 0xa5;
1007 asm volatile ("pushl %%ebp\n\t"
1008 "pushl %%ds\n\t"
1009 "movl %2, %%ds\n\t"
1010 "movl %3, %%ebp\n\t"
1011 "movzbl 0x1, %0\n\t"
1012 "movzbl (%%ebp), %1\n\t"
1013 "popl %%ds\n\t"
1014 "popl %%ebp\n\t"
1015 : "=r" (res), "=r" (res2)
1016 : "r" (MK_SEL(1)), "r" (&tmp));
1017 printf("DS[1] = %02x\n", res);
1018 printf("SS[tmp] = %02x\n", res2);
1020 segoff.seg = MK_SEL(2);
1021 segoff.offset = 0xabcdef12;
1022 asm volatile("lfs %2, %0\n\t"
1023 "movl %%fs, %1\n\t"
1024 : "=r" (res), "=g" (res2)
1025 : "m" (segoff));
1026 printf("FS:reg = %04x:%08x\n", res2, res);
1028 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1029 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1030 TEST_LR("lslw", "w", MK_SEL(2), 0);
1031 TEST_LR("lsll", "", MK_SEL(2), 0);
1033 TEST_LR("larw", "w", 0xfff8, 0);
1034 TEST_LR("larl", "", 0xfff8, 0);
1035 TEST_LR("lslw", "w", 0xfff8, 0);
1036 TEST_LR("lsll", "", 0xfff8, 0);
1038 #endif
1040 #if 0
1041 /* 16 bit code test */
1042 extern char code16_start, code16_end;
1043 extern char code16_func1;
1044 extern char code16_func2;
1045 extern char code16_func3;
1047 void test_code16(void)
1049 struct modify_ldt_ldt_s ldt;
1050 int res, res2;
1052 /* build a code segment */
1053 ldt.entry_number = 1;
1054 ldt.base_addr = (unsigned long)&code16_start;
1055 ldt.limit = &code16_end - &code16_start;
1056 ldt.seg_32bit = 0;
1057 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
1058 ldt.read_exec_only = 0;
1059 ldt.limit_in_pages = 0;
1060 ldt.seg_not_present = 0;
1061 ldt.useable = 1;
1062 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1064 /* call the first function */
1065 asm volatile ("lcall %1, %2"
1066 : "=a" (res)
1067 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
1068 printf("func1() = 0x%08x\n", res);
1069 asm volatile ("lcall %2, %3"
1070 : "=a" (res), "=c" (res2)
1071 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
1072 printf("func2() = 0x%08x spdec=%d\n", res, res2);
1073 asm volatile ("lcall %1, %2"
1074 : "=a" (res)
1075 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1076 printf("func3() = 0x%08x\n", res);
1078 #endif
1080 extern char func_lret32;
1081 extern char func_iret32;
1083 void test_misc(void)
1085 char table[256];
1086 int res, i;
1088 #if 0
1089 // REINSTATE
1090 for(i=0;i<256;i++) table[i] = 256 - i;
1091 res = 0x12345678;
1092 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
1093 printf("xlat: EAX=%08x\n", res);
1094 #endif
1095 #if 0
1096 // REINSTATE
1097 asm volatile ("pushl %%cs ; call %1"
1098 : "=a" (res)
1099 : "m" (func_lret32): "memory", "cc");
1100 printf("func_lret32=%x\n", res);
1102 asm volatile ("pushfl ; pushl %%cs ; call %1"
1103 : "=a" (res)
1104 : "m" (func_iret32): "memory", "cc");
1105 printf("func_iret32=%x\n", res);
1106 #endif
1107 /* specific popl test */
1108 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1109 : "=g" (res));
1110 printf("popl esp=%x\n", res);
1111 #if 0
1112 // REINSTATE
1113 /* specific popw test */
1114 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1115 : "=g" (res));
1116 printf("popw esp=%x\n", res);
1117 #endif
1120 uint8_t str_buffer[4096];
1122 #define TEST_STRING1(OP, size, DF, REP)\
1124 int esi, edi, eax, ecx, eflags;\
1126 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1127 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1128 eax = 0x12345678;\
1129 ecx = 17;\
1131 asm volatile ("pushl $0\n\t"\
1132 "popf\n\t"\
1133 DF "\n\t"\
1134 REP #OP size "\n\t"\
1135 "cld\n\t"\
1136 "pushf\n\t"\
1137 "popl %4\n\t"\
1138 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1139 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1140 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1141 REP #OP size, esi, edi, eax, ecx,\
1142 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1145 #define TEST_STRING(OP, REP)\
1146 TEST_STRING1(OP, "b", "", REP);\
1147 TEST_STRING1(OP, "w", "", REP);\
1148 TEST_STRING1(OP, "l", "", REP);\
1149 TEST_STRING1(OP, "b", "std", REP);\
1150 TEST_STRING1(OP, "w", "std", REP);\
1151 TEST_STRING1(OP, "l", "std", REP)
1153 void test_string(void)
1155 int i;
1156 for(i = 0;i < sizeof(str_buffer); i++)
1157 str_buffer[i] = i + 0x56;
1158 TEST_STRING(stos, "");
1159 TEST_STRING(stos, "rep ");
1160 // REINSTATE: TEST_STRING(lods, ""); /* to verify stos */
1161 // REINSTATE: TEST_STRING(lods, "rep ");
1162 TEST_STRING(movs, "");
1163 TEST_STRING(movs, "rep ");
1164 // REINSTATE: TEST_STRING(lods, ""); /* to verify stos */
1166 /* XXX: better tests */
1167 TEST_STRING(scas, "");
1168 // REINSTATE: TEST_STRING(scas, "repz ");
1169 TEST_STRING(scas, "repnz ");
1170 // REINSTATE: TEST_STRING(cmps, "");
1171 TEST_STRING(cmps, "repz ");
1172 // REINSTATE: TEST_STRING(cmps, "repnz ");
1175 /* VM86 test */
1176 #if 0
1177 static inline void set_bit(uint8_t *a, unsigned int bit)
1179 a[bit / 8] |= (1 << (bit % 8));
1182 static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1184 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1187 static inline void pushw(struct vm86_regs *r, int val)
1189 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1190 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1193 #undef __syscall_return
1194 #define __syscall_return(type, res) \
1195 do { \
1196 return (type) (res); \
1197 } while (0)
1199 _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1201 extern char vm86_code_start;
1202 extern char vm86_code_end;
1204 #define VM86_CODE_CS 0x100
1205 #define VM86_CODE_IP 0x100
1207 void test_vm86(void)
1209 struct vm86plus_struct ctx;
1210 struct vm86_regs *r;
1211 uint8_t *vm86_mem;
1212 int seg, ret;
1214 vm86_mem = mmap((void *)0x00000000, 0x110000,
1215 PROT_WRITE | PROT_READ | PROT_EXEC,
1216 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1217 if (vm86_mem == MAP_FAILED) {
1218 printf("ERROR: could not map vm86 memory");
1219 return;
1221 memset(&ctx, 0, sizeof(ctx));
1223 /* init basic registers */
1224 r = &ctx.regs;
1225 r->eip = VM86_CODE_IP;
1226 r->esp = 0xfffe;
1227 seg = VM86_CODE_CS;
1228 r->cs = seg;
1229 r->ss = seg;
1230 r->ds = seg;
1231 r->es = seg;
1232 r->fs = seg;
1233 r->gs = seg;
1234 r->eflags = VIF_MASK;
1236 /* move code to proper address. We use the same layout as a .com
1237 dos program. */
1238 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1239 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1241 /* mark int 0x21 as being emulated */
1242 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1244 for(;;) {
1245 ret = vm86(VM86_ENTER, &ctx);
1246 switch(VM86_TYPE(ret)) {
1247 case VM86_INTx:
1249 int int_num, ah, v;
1251 int_num = VM86_ARG(ret);
1252 if (int_num != 0x21)
1253 goto unknown_int;
1254 ah = (r->eax >> 8) & 0xff;
1255 switch(ah) {
1256 case 0x00: /* exit */
1257 goto the_end;
1258 case 0x02: /* write char */
1260 uint8_t c = r->edx;
1261 putchar(c);
1263 break;
1264 case 0x09: /* write string */
1266 uint8_t c, *ptr;
1267 ptr = seg_to_linear(r->ds, r->edx);
1268 for(;;) {
1269 c = *ptr++;
1270 if (c == '$')
1271 break;
1272 putchar(c);
1274 r->eax = (r->eax & ~0xff) | '$';
1276 break;
1277 case 0xff: /* extension: write eflags number in edx */
1278 v = (int)r->edx;
1279 #ifndef LINUX_VM86_IOPL_FIX
1280 v &= ~0x3000;
1281 #endif
1282 printf("%08x\n", v);
1283 break;
1284 default:
1285 unknown_int:
1286 printf("unsupported int 0x%02x\n", int_num);
1287 goto the_end;
1290 break;
1291 case VM86_SIGNAL:
1292 /* a signal came, we just ignore that */
1293 break;
1294 case VM86_STI:
1295 break;
1296 default:
1297 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1298 goto the_end;
1301 the_end:
1302 printf("VM86 end\n");
1303 munmap(vm86_mem, 0x110000);
1305 #endif
1307 /* exception tests */
1308 #if 0
1309 #ifndef REG_EAX
1310 #define REG_EAX EAX
1311 #define REG_EBX EBX
1312 #define REG_ECX ECX
1313 #define REG_EDX EDX
1314 #define REG_ESI ESI
1315 #define REG_EDI EDI
1316 #define REG_EBP EBP
1317 #define REG_ESP ESP
1318 #define REG_EIP EIP
1319 #define REG_EFL EFL
1320 #define REG_TRAPNO TRAPNO
1321 #define REG_ERR ERR
1322 #endif
1324 jmp_buf jmp_env;
1325 int v1;
1326 int tab[2];
1328 void sig_handler(int sig, siginfo_t *info, void *puc)
1330 struct ucontext *uc = puc;
1332 printf("si_signo=%d si_errno=%d si_code=%d",
1333 info->si_signo, info->si_errno, info->si_code);
1334 printf(" si_addr=0x%08lx",
1335 (unsigned long)info->si_addr);
1336 printf("\n");
1338 printf("trapno=0x%02x err=0x%08x",
1339 uc->uc_mcontext.gregs[REG_TRAPNO],
1340 uc->uc_mcontext.gregs[REG_ERR]);
1341 printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
1342 printf("\n");
1343 longjmp(jmp_env, 1);
1346 void test_exceptions(void)
1348 struct modify_ldt_ldt_s ldt;
1349 struct sigaction act;
1350 volatile int val;
1352 act.sa_sigaction = sig_handler;
1353 sigemptyset(&act.sa_mask);
1354 act.sa_flags = SA_SIGINFO;
1355 sigaction(SIGFPE, &act, NULL);
1356 sigaction(SIGILL, &act, NULL);
1357 sigaction(SIGSEGV, &act, NULL);
1358 sigaction(SIGBUS, &act, NULL);
1359 sigaction(SIGTRAP, &act, NULL);
1361 /* test division by zero reporting */
1362 printf("DIVZ exception:\n");
1363 if (setjmp(jmp_env) == 0) {
1364 /* now divide by zero */
1365 v1 = 0;
1366 v1 = 2 / v1;
1369 printf("BOUND exception:\n");
1370 if (setjmp(jmp_env) == 0) {
1371 /* bound exception */
1372 tab[0] = 1;
1373 tab[1] = 10;
1374 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1377 printf("segment exceptions:\n");
1378 if (setjmp(jmp_env) == 0) {
1379 /* load an invalid segment */
1380 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1382 if (setjmp(jmp_env) == 0) {
1383 /* null data segment is valid */
1384 asm volatile ("movl %0, %%fs" : : "r" (3));
1385 /* null stack segment */
1386 asm volatile ("movl %0, %%ss" : : "r" (3));
1389 ldt.entry_number = 1;
1390 ldt.base_addr = (unsigned long)&seg_data1;
1391 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1392 ldt.seg_32bit = 1;
1393 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1394 ldt.read_exec_only = 0;
1395 ldt.limit_in_pages = 1;
1396 ldt.seg_not_present = 1;
1397 ldt.useable = 1;
1398 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1400 if (setjmp(jmp_env) == 0) {
1401 /* segment not present */
1402 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1405 /* test SEGV reporting */
1406 printf("PF exception:\n");
1407 if (setjmp(jmp_env) == 0) {
1408 val = 1;
1409 /* we add a nop to test a weird PC retrieval case */
1410 asm volatile ("nop");
1411 /* now store in an invalid address */
1412 *(char *)0x1234 = 1;
1415 /* test SEGV reporting */
1416 printf("PF exception:\n");
1417 if (setjmp(jmp_env) == 0) {
1418 val = 1;
1419 /* read from an invalid address */
1420 v1 = *(char *)0x1234;
1423 /* test illegal instruction reporting */
1424 printf("UD2 exception:\n");
1425 if (setjmp(jmp_env) == 0) {
1426 /* now execute an invalid instruction */
1427 asm volatile("ud2");
1429 printf("lock nop exception:\n");
1430 if (setjmp(jmp_env) == 0) {
1431 /* now execute an invalid instruction */
1432 asm volatile("lock nop");
1435 printf("INT exception:\n");
1436 if (setjmp(jmp_env) == 0) {
1437 asm volatile ("int $0xfd");
1439 if (setjmp(jmp_env) == 0) {
1440 asm volatile ("int $0x01");
1442 if (setjmp(jmp_env) == 0) {
1443 asm volatile (".byte 0xcd, 0x03");
1445 if (setjmp(jmp_env) == 0) {
1446 asm volatile ("int $0x04");
1448 if (setjmp(jmp_env) == 0) {
1449 asm volatile ("int $0x05");
1452 printf("INT3 exception:\n");
1453 if (setjmp(jmp_env) == 0) {
1454 asm volatile ("int3");
1457 printf("CLI exception:\n");
1458 if (setjmp(jmp_env) == 0) {
1459 asm volatile ("cli");
1462 printf("STI exception:\n");
1463 if (setjmp(jmp_env) == 0) {
1464 asm volatile ("cli");
1467 printf("INTO exception:\n");
1468 if (setjmp(jmp_env) == 0) {
1469 /* overflow exception */
1470 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1473 printf("OUTB exception:\n");
1474 if (setjmp(jmp_env) == 0) {
1475 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1478 printf("INB exception:\n");
1479 if (setjmp(jmp_env) == 0) {
1480 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1483 printf("REP OUTSB exception:\n");
1484 if (setjmp(jmp_env) == 0) {
1485 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1488 printf("REP INSB exception:\n");
1489 if (setjmp(jmp_env) == 0) {
1490 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1493 printf("HLT exception:\n");
1494 if (setjmp(jmp_env) == 0) {
1495 asm volatile ("hlt");
1498 printf("single step exception:\n");
1499 val = 0;
1500 if (setjmp(jmp_env) == 0) {
1501 asm volatile ("pushf\n"
1502 "orl $0x00100, (%%esp)\n"
1503 "popf\n"
1504 "movl $0xabcd, %0\n"
1505 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1507 printf("val=0x%x\n", val);
1510 /* specific precise single step test */
1511 void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1513 struct ucontext *uc = puc;
1514 printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1517 const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1518 uint8_t sstep_buf2[4];
1520 void test_single_step(void)
1522 struct sigaction act;
1523 volatile int val;
1524 int i;
1526 val = 0;
1527 act.sa_sigaction = sig_trap_handler;
1528 sigemptyset(&act.sa_mask);
1529 act.sa_flags = SA_SIGINFO;
1530 sigaction(SIGTRAP, &act, NULL);
1531 asm volatile ("pushf\n"
1532 "orl $0x00100, (%%esp)\n"
1533 "popf\n"
1534 "movl $0xabcd, %0\n"
1536 /* jmp test */
1537 "movl $3, %%ecx\n"
1538 "1:\n"
1539 "addl $1, %0\n"
1540 "decl %%ecx\n"
1541 "jnz 1b\n"
1543 /* movsb: the single step should stop at each movsb iteration */
1544 "movl $sstep_buf1, %%esi\n"
1545 "movl $sstep_buf2, %%edi\n"
1546 "movl $0, %%ecx\n"
1547 "rep movsb\n"
1548 "movl $3, %%ecx\n"
1549 "rep movsb\n"
1550 "movl $1, %%ecx\n"
1551 "rep movsb\n"
1553 /* cmpsb: the single step should stop at each cmpsb iteration */
1554 "movl $sstep_buf1, %%esi\n"
1555 "movl $sstep_buf2, %%edi\n"
1556 "movl $0, %%ecx\n"
1557 "rep cmpsb\n"
1558 "movl $4, %%ecx\n"
1559 "rep cmpsb\n"
1561 /* getpid() syscall: single step should skip one
1562 instruction */
1563 "movl $20, %%eax\n"
1564 "int $0x80\n"
1565 "movl $0, %%eax\n"
1567 /* when modifying SS, trace is not done on the next
1568 instruction */
1569 "movl %%ss, %%ecx\n"
1570 "movl %%ecx, %%ss\n"
1571 "addl $1, %0\n"
1572 "movl $1, %%eax\n"
1573 "movl %%ecx, %%ss\n"
1574 "jmp 1f\n"
1575 "addl $1, %0\n"
1576 "1:\n"
1577 "movl $1, %%eax\n"
1578 "pushl %%ecx\n"
1579 "popl %%ss\n"
1580 "addl $1, %0\n"
1581 "movl $1, %%eax\n"
1583 "pushf\n"
1584 "andl $~0x00100, (%%esp)\n"
1585 "popf\n"
1586 : "=m" (val)
1588 : "cc", "memory", "eax", "ecx", "esi", "edi");
1589 printf("val=%d\n", val);
1590 for(i = 0; i < 4; i++)
1591 printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1594 /* self modifying code test */
1595 uint8_t code[] = {
1596 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1597 0xc3, /* ret */
1600 asm("smc_code2:\n"
1601 "movl 4(%esp), %eax\n"
1602 "movl %eax, smc_patch_addr2 + 1\n"
1603 "nop\n"
1604 "nop\n"
1605 "nop\n"
1606 "nop\n"
1607 "nop\n"
1608 "nop\n"
1609 "nop\n"
1610 "nop\n"
1611 "smc_patch_addr2:\n"
1612 "movl $1, %eax\n"
1613 "ret\n");
1615 typedef int FuncType(void);
1616 extern int smc_code2(int);
1617 void test_self_modifying_code(void)
1619 int i;
1621 printf("self modifying code:\n");
1622 printf("func1 = 0x%x\n", ((FuncType *)code)());
1623 for(i = 2; i <= 4; i++) {
1624 code[1] = i;
1625 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1628 /* more difficult test : the modified code is just after the
1629 modifying instruction. It is forbidden in Intel specs, but it
1630 is used by old DOS programs */
1631 for(i = 2; i <= 4; i++) {
1632 printf("smc_code2(%d) = %d\n", i, smc_code2(i));
1636 static void *call_end __init_call = NULL;
1637 #endif
1639 int main(int argc, char **argv)
1641 void **ptr;
1642 void (*func)(void);
1644 #if 1
1645 ptr = &call_start + 1;
1646 while (*ptr != NULL) {
1647 func = *ptr++;
1648 func();
1650 #endif
1651 test_bsx();
1652 test_mul();
1653 test_jcc();
1654 test_floats();
1655 //test_bcd();
1656 test_xchg();
1657 test_string();
1658 test_misc(); // REINSTATE
1659 test_lea();
1660 // test_segs();
1661 //test_code16();
1662 //test_vm86();
1663 //test_exceptions();
1664 //test_self_modifying_code();
1665 //test_single_step();
1666 return 0;