non-linux regtest: update cmdline expecteds
[valgrind.git] / VEX / test / test-amd64.c
blob9cb8c3dbe01581a52a446d7e5aac9bbc5b773145
2 /* To build:
4 gcc -g -o test-amd64 test-amd64.c -lm
6 */
8 /* Contrary to what the next comment says, this is now an amd64 CPU
9 test. */
12 * x86 CPU test
14 * Copyright (c) 2003 Fabrice Bellard
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
29 #define _GNU_SOURCE
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <inttypes.h>
34 #include <math.h>
35 #include <signal.h>
36 #include <setjmp.h>
37 #include <errno.h>
38 #include <sys/ucontext.h>
39 #include <sys/mman.h>
41 /* Setting this to 1 creates a very comprehensive test of
42 integer condition codes. */
43 #define TEST_INTEGER_VERBOSE 1
45 typedef long long int int64;
47 //#define LINUX_VM86_IOPL_FIX
48 //#define TEST_P4_FLAGS
50 #define xglue(x, y) x ## y
51 #define glue(x, y) xglue(x, y)
52 #define stringify(s) tostring(s)
53 #define tostring(s) #s
55 #define CC_C 0x0001
56 #define CC_P 0x0004
57 #define CC_A 0x0010
58 #define CC_Z 0x0040
59 #define CC_S 0x0080
60 #define CC_O 0x0800
62 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
64 static void *call_start __init_call = NULL;
66 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
68 #define OP add
69 #include "test-amd64.h"
71 #define OP sub
72 #include "test-amd64.h"
74 #define OP xor
75 #include "test-amd64.h"
77 #define OP and
78 #include "test-amd64.h"
80 #define OP or
81 #include "test-amd64.h"
83 #define OP cmp
84 #include "test-amd64.h"
86 #define OP adc
87 #define OP_CC
88 #include "test-amd64.h"
90 #define OP sbb
91 #define OP_CC
92 #include "test-amd64.h"
94 #define OP inc
95 #define OP_CC
96 #define OP1
97 #include "test-amd64.h"
99 #define OP dec
100 #define OP_CC
101 #define OP1
102 #include "test-amd64.h"
104 #define OP neg
105 #define OP_CC
106 #define OP1
107 #include "test-amd64.h"
109 #define OP not
110 #define OP_CC
111 #define OP1
112 #include "test-amd64.h"
114 #undef CC_MASK
115 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
117 #define OP shl
118 #include "test-amd64-shift.h"
120 #define OP shr
121 #include "test-amd64-shift.h"
123 #define OP sar
124 #include "test-amd64-shift.h"
126 #define OP rol
127 #include "test-amd64-shift.h"
129 #define OP ror
130 #include "test-amd64-shift.h"
132 #define OP rcr
133 #define OP_CC
134 #include "test-amd64-shift.h"
136 #define OP rcl
137 #define OP_CC
138 #include "test-amd64-shift.h"
140 #if 0
141 #define OP shld
142 #define OP_SHIFTD
143 #define OP_NOBYTE
144 #include "test-amd64-shift.h"
146 #define OP shrd
147 #define OP_SHIFTD
148 #define OP_NOBYTE
149 #include "test-amd64-shift.h"
150 #endif
152 /* XXX: should be more precise ? */
153 #undef CC_MASK
154 #define CC_MASK (CC_C)
156 #if 0
157 #define OP bt
158 #define OP_NOBYTE
159 #include "test-amd64-shift.h"
161 #define OP bts
162 #define OP_NOBYTE
163 #include "test-amd64-shift.h"
165 #define OP btr
166 #define OP_NOBYTE
167 #include "test-amd64-shift.h"
169 #define OP btc
170 #define OP_NOBYTE
171 #include "test-amd64-shift.h"
172 #endif
174 /* lea test (modrm support) */
175 #define TEST_LEA(STR)\
177 asm("leaq " STR ", %0"\
178 : "=r" (res)\
179 : "a" (rax), "b" (rbx), "c" (rcx), "d" (rdx), "S" (rsi), "D" (rdi));\
180 printf("lea %s = %016llx\n", STR, res);\
183 #define TEST_LEA16(STR)\
185 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
186 : "=wq" (res)\
187 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
188 printf("lea %s = %08x\n", STR, res);\
192 void test_lea(void)
194 int64 rax, rbx, rcx, rdx, rsi, rdi, res;
195 rax = 0x0001;
196 rbx = 0x0002;
197 rcx = 0x0004;
198 rdx = 0x0008;
199 rsi = 0x0010;
200 rdi = 0x0020;
202 TEST_LEA("0x4000");
204 TEST_LEA("(%%rax)");
205 TEST_LEA("(%%rbx)");
206 TEST_LEA("(%%rcx)");
207 TEST_LEA("(%%rdx)");
208 TEST_LEA("(%%rsi)");
209 TEST_LEA("(%%rdi)");
211 TEST_LEA("0x40(%%rax)");
212 TEST_LEA("0x40(%%rbx)");
213 TEST_LEA("0x40(%%rcx)");
214 TEST_LEA("0x40(%%rdx)");
215 TEST_LEA("0x40(%%rsi)");
216 TEST_LEA("0x40(%%rdi)");
218 TEST_LEA("0x4000(%%rax)");
219 TEST_LEA("0x4000(%%rbx)");
220 TEST_LEA("0x4000(%%rcx)");
221 TEST_LEA("0x4000(%%rdx)");
222 TEST_LEA("0x4000(%%rsi)");
223 TEST_LEA("0x4000(%%rdi)");
225 TEST_LEA("(%%rax, %%rcx)");
226 TEST_LEA("(%%rbx, %%rdx)");
227 TEST_LEA("(%%rcx, %%rcx)");
228 TEST_LEA("(%%rdx, %%rcx)");
229 TEST_LEA("(%%rsi, %%rcx)");
230 TEST_LEA("(%%rdi, %%rcx)");
232 TEST_LEA("0x40(%%rax, %%rcx)");
233 TEST_LEA("0x4000(%%rbx, %%rdx)");
235 TEST_LEA("(%%rcx, %%rcx, 2)");
236 TEST_LEA("(%%rdx, %%rcx, 4)");
237 TEST_LEA("(%%rsi, %%rcx, 8)");
239 TEST_LEA("(,%%rax, 2)");
240 TEST_LEA("(,%%rbx, 4)");
241 TEST_LEA("(,%%rcx, 8)");
243 TEST_LEA("0x40(,%%rax, 2)");
244 TEST_LEA("0x40(,%%rbx, 4)");
245 TEST_LEA("0x40(,%%rcx, 8)");
248 TEST_LEA("-10(%%rcx, %%rcx, 2)");
249 TEST_LEA("-10(%%rdx, %%rcx, 4)");
250 TEST_LEA("-10(%%rsi, %%rcx, 8)");
252 TEST_LEA("0x4000(%%rcx, %%rcx, 2)");
253 TEST_LEA("0x4000(%%rdx, %%rcx, 4)");
254 TEST_LEA("0x4000(%%rsi, %%rcx, 8)");
257 #define TEST_JCC(JCC, v1, v2)\
258 { int one = 1; \
259 int res;\
260 asm("movl $1, %0\n\t"\
261 "cmpl %2, %1\n\t"\
262 "j" JCC " 1f\n\t"\
263 "movl $0, %0\n\t"\
264 "1:\n\t"\
265 : "=r" (res)\
266 : "r" (v1), "r" (v2));\
267 printf("%-10s %d\n", "j" JCC, res);\
269 asm("movl $0, %0\n\t"\
270 "cmpl %2, %1\n\t"\
271 "set" JCC " %b0\n\t"\
272 : "=r" (res)\
273 : "r" (v1), "r" (v2));\
274 printf("%-10s %d\n", "set" JCC, res);\
276 asm("movl $0x12345678, %0\n\t"\
277 "cmpl %2, %1\n\t"\
278 "cmov" JCC "l %3, %0\n\t"\
279 : "=r" (res)\
280 : "r" (v1), "r" (v2), "m" (one));\
281 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
282 asm("movl $0x12345678, %0\n\t"\
283 "cmpl %2, %1\n\t"\
284 "cmov" JCC "w %w3, %w0\n\t"\
285 : "=r" (res)\
286 : "r" (v1), "r" (v2), "r" (one));\
287 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
291 /* various jump tests */
292 void test_jcc(void)
294 TEST_JCC("ne", 1, 1);
295 TEST_JCC("ne", 1, 0);
297 TEST_JCC("e", 1, 1);
298 TEST_JCC("e", 1, 0);
300 TEST_JCC("l", 1, 1);
301 TEST_JCC("l", 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);
312 TEST_JCC("g", 1, 1);
313 TEST_JCC("g", 1, 0);
314 TEST_JCC("g", 1, -1);
316 TEST_JCC("b", 1, 1);
317 TEST_JCC("b", 1, 0);
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);
328 TEST_JCC("a", 1, 1);
329 TEST_JCC("a", 1, 0);
330 TEST_JCC("a", 1, -1);
333 TEST_JCC("p", 1, 1);
334 TEST_JCC("p", 1, 0);
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);
345 TEST_JCC("s", 0, 1);
346 TEST_JCC("s", 0, -1);
347 TEST_JCC("s", 0, 0);
349 TEST_JCC("ns", 0, 1);
350 TEST_JCC("ns", 0, -1);
351 TEST_JCC("ns", 0, 0);
354 #undef CC_MASK
355 #ifdef TEST_P4_FLAGS
356 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
357 #else
358 #define CC_MASK (CC_O | CC_C)
359 #endif
361 #define OP mul
362 #include "test-amd64-muldiv.h"
364 #define OP imul
365 #include "test-amd64-muldiv.h"
367 void test_imulw2(int64 op0, int64 op1)
369 int64 res, s1, s0, flags;
370 s0 = op0;
371 s1 = op1;
372 res = s0;
373 flags = 0;
374 asm ("pushq %4\n\t"
375 "popfq\n\t"
376 "imulw %w2, %w0\n\t"
377 "pushfq\n\t"
378 "popq %1\n\t"
379 : "=q" (res), "=g" (flags)
380 : "q" (s1), "0" (res), "1" (flags));
381 printf("%-10s A=%016llx B=%016llx R=%016llx CC=%04llx\n",
382 "imulw", s0, s1, res, flags & CC_MASK);
385 void test_imull2(int64 op0, int64 op1)
387 int res, s1;
388 int64 s0, flags;
389 s0 = op0;
390 s1 = op1;
391 res = s0;
392 flags = 0;
393 asm ("pushq %4\n\t"
394 "popfq\n\t"
395 "imull %2, %0\n\t"
396 "pushfq\n\t"
397 "popq %1\n\t"
398 : "=q" (res), "=g" (flags)
399 : "q" (s1), "0" (res), "1" (flags));
400 printf("%-10s A=%016llx B=%08x R=%08x CC=%04llx\n",
401 "imull", s0, s1, res, flags & CC_MASK);
404 #define TEST_IMUL_IM(size, size1, op0, op1)\
406 int64 res, flags;\
407 flags = 0;\
408 res = 0;\
409 asm ("pushq %3\n\t"\
410 "popfq\n\t"\
411 "imul" size " $" #op0 ", %" size1 "2, %" size1 "0\n\t" \
412 "pushfq\n\t"\
413 "popq %1\n\t"\
414 : "=r" (res), "=g" (flags)\
415 : "r" (op1), "1" (flags), "0" (res));\
416 printf("%-10s A=%08x B=%08x R=%016llx CC=%04llx\n",\
417 "imul" size, op0, op1, res, flags & CC_MASK);\
420 #define TEST_IMUL_IM_L(op0, op1)\
422 int64 flags = 0;\
423 int res = 0;\
424 int res64 = 0;\
425 asm ("pushq %3\n\t"\
426 "popfq\n\t"\
427 "imul $" #op0 ", %2, %0\n\t" \
428 "pushfq\n\t"\
429 "popq %1\n\t"\
430 : "=r" (res64), "=g" (flags)\
431 : "r" (op1), "1" (flags), "0" (res));\
432 printf("%-10s A=%08x B=%08x R=%08x CC=%04llx\n",\
433 "imull", op0, op1, res, flags & CC_MASK);\
437 #undef CC_MASK
438 #define CC_MASK (0)
440 #define OP div
441 #include "test-amd64-muldiv.h"
443 #define OP idiv
444 #include "test-amd64-muldiv.h"
446 void test_mul(void)
448 test_imulb(0x1234561d, 4);
449 test_imulb(3, -4);
450 test_imulb(0x80, 0x80);
451 test_imulb(0x10, 0x10);
453 test_imulw(0, 0, 0);
454 test_imulw(0, 0xFF, 0xFF);
455 test_imulw(0, 0xFF, 0x100);
456 test_imulw(0, 0x1234001d, 45);
457 test_imulw(0, 23, -45);
458 test_imulw(0, 0x8000, 0x8000);
459 test_imulw(0, 0x100, 0x100);
461 test_imull(0, 0, 0);
462 test_imull(0, 0xFFFF, 0xFFFF);
463 test_imull(0, 0xFFFF, 0x10000);
464 test_imull(0, 0x1234001d, 45);
465 test_imull(0, 23, -45);
466 test_imull(0, 0x80000000, 0x80000000);
467 test_imull(0, 0x10000, 0x10000);
469 test_mulb(0x1234561d, 4);
470 test_mulb(3, -4);
471 test_mulb(0x80, 0x80);
472 test_mulb(0x10, 0x10);
474 test_mulw(0, 0x1234001d, 45);
475 test_mulw(0, 23, -45);
476 test_mulw(0, 0x8000, 0x8000);
477 test_mulw(0, 0x100, 0x100);
479 test_mull(0, 0x1234001d, 45);
480 test_mull(0, 23, -45);
481 test_mull(0, 0x80000000, 0x80000000);
482 test_mull(0, 0x10000, 0x10000);
484 test_imulw2(0x1234001d, 45);
485 test_imulw2(23, -45);
486 test_imulw2(0x8000, 0x8000);
487 test_imulw2(0x100, 0x100);
489 test_imull2(0x1234001d, 45);
490 test_imull2(23, -45);
491 test_imull2(0x80000000, 0x80000000);
492 test_imull2(0x10000, 0x10000);
494 TEST_IMUL_IM("w", "w", 45, 0x1234);
495 TEST_IMUL_IM("w", "w", -45, 23);
496 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
497 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
499 TEST_IMUL_IM_L(45, 0x1234);
500 TEST_IMUL_IM_L(-45, 23);
501 TEST_IMUL_IM_L(0x8000, 0x80000000);
502 TEST_IMUL_IM_L(0x7fff, 0x1000);
504 test_idivb(0x12341678, 0x127e);
505 test_idivb(0x43210123, -5);
506 test_idivb(0x12340004, -1);
508 test_idivw(0, 0x12345678, 12347);
509 test_idivw(0, -23223, -45);
510 test_idivw(0, 0x12348000, -1);
511 test_idivw(0x12343, 0x12345678, 0x81238567);
513 test_idivl(0, 0x12345678, 12347);
514 test_idivl(0, -233223, -45);
515 test_idivl(0, 0x80000000, -1);
516 test_idivl(0x12343, 0x12345678, 0x81234567);
518 test_idivq(0, 0x12345678, 12347);
519 test_idivq(0, -233223, -45);
520 test_idivq(0, 0x80000000, -1);
521 test_idivq(0x12343, 0x12345678, 0x81234567);
523 test_divb(0x12341678, 0x127e);
524 test_divb(0x43210123, -5);
525 test_divb(0x12340004, -1);
527 test_divw(0, 0x12345678, 12347);
528 test_divw(0, -23223, -45);
529 test_divw(0, 0x12348000, -1);
530 test_divw(0x12343, 0x12345678, 0x81238567);
532 test_divl(0, 0x12345678, 12347);
533 test_divl(0, -233223, -45);
534 test_divl(0, 0x80000000, -1);
535 test_divl(0x12343, 0x12345678, 0x81234567);
537 test_divq(0, 0x12345678, 12347);
538 test_divq(0, -233223, -45);
539 test_divq(0, 0x80000000, -1);
540 test_divq(0x12343, 0x12345678, 0x81234567);
543 #define TEST_BSX(op, size, op0)\
545 int res, val, resz;\
546 val = op0;\
547 asm("xorl %1, %1\n"\
548 "movl $0x12345678, %0\n"\
549 #op " %" size "2, %" size "0 ; setz %b1" \
550 : "=r" (res), "=q" (resz)\
551 : "r" (val));\
552 printf("%-10s A=%08x R=%08x %d\n", #op, val, res, resz);\
555 void test_bsx(void)
557 TEST_BSX(bsrw, "w", 0);
558 TEST_BSX(bsrw, "w", 0x12340128);
559 TEST_BSX(bsrl, "", 0);
560 TEST_BSX(bsrl, "", 0x00340128);
561 TEST_BSX(bsfw, "w", 0);
562 TEST_BSX(bsfw, "w", 0x12340128);
563 TEST_BSX(bsfl, "", 0);
564 TEST_BSX(bsfl, "", 0x00340128);
567 /**********************************************/
569 void test_fops(double a, double b)
571 printf("a=%f b=%f a+b=%f\n", a, b, a + b);
572 printf("a=%f b=%f a-b=%f\n", a, b, a - b);
573 printf("a=%f b=%f a*b=%f\n", a, b, a * b);
574 printf("a=%f b=%f a/b=%f\n", a, b, a / b);
575 printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
576 printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
577 printf("a=%f sin(a)=%f\n", a, sin(a));
578 printf("a=%f cos(a)=%f\n", a, cos(a));
579 printf("a=%f tan(a)=%f\n", a, tan(a));
580 printf("a=%f log(a)=%f\n", a, log(a));
581 printf("a=%f exp(a)=%f\n", a, exp(a));
582 printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
583 /* just to test some op combining */
584 printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
585 printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
586 printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
589 void test_fcmp(double a, double b)
591 printf("(%f<%f)=%d\n",
592 a, b, a < b);
593 printf("(%f<=%f)=%d\n",
594 a, b, a <= b);
595 printf("(%f==%f)=%d\n",
596 a, b, a == b);
597 printf("(%f>%f)=%d\n",
598 a, b, a > b);
599 printf("(%f<=%f)=%d\n",
600 a, b, a >= b);
602 unsigned long long int rflags;
603 /* test f(u)comi instruction */
604 asm("fcomi %2, %1\n"
605 "pushfq\n"
606 "popq %0\n"
607 : "=r" (rflags)
608 : "t" (a), "u" (b));
609 printf("fcomi(%f %f)=%016llx\n", a, b, rflags & (CC_Z | CC_P | CC_C));
613 void test_fcvt(double a)
615 float fa;
616 long double la;
617 int16_t fpuc;
618 int i;
619 int64 lla;
620 int ia;
621 int16_t wa;
622 double ra;
624 fa = a;
625 la = a;
626 printf("(float)%f = %f\n", a, fa);
627 printf("(long double)%f = %Lf\n", a, la);
628 printf("a=%016Lx\n", *(long long *)&a);
629 printf("la=%016Lx %04x\n", *(long long *)&la,
630 *(unsigned short *)((char *)(&la) + 8));
632 /* test all roundings */
633 asm volatile ("fstcw %0" : "=m" (fpuc));
634 for(i=0;i<4;i++) {
635 short zz = (fpuc & ~0x0c00) | (i << 10);
636 asm volatile ("fldcw %0" : : "m" (zz));
637 asm volatile ("fist %0" : "=m" (wa) : "t" (a));
638 asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
639 asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
640 asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
641 asm volatile ("fldcw %0" : : "m" (fpuc));
642 printf("(short)a = %d\n", wa);
643 printf("(int)a = %d\n", ia);
644 printf("(int64_t)a = %Ld\n", lla);
645 printf("rint(a) = %f\n", ra);
649 #define TEST(N) \
650 asm("fld" #N : "=t" (a)); \
651 printf("fld" #N "= %f\n", a);
653 void test_fconst(void)
655 double a;
656 TEST(1);
657 TEST(l2t);
658 TEST(l2e);
659 TEST(pi);
660 TEST(lg2);
661 TEST(ln2);
662 TEST(z);
665 void test_fbcd(double a)
667 unsigned short bcd[5];
668 double b;
670 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
671 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
672 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
673 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
676 #define TEST_ENV(env, save, restore)\
678 memset((env), 0xaa, sizeof(*(env)));\
679 for(i=0;i<5;i++)\
680 asm volatile ("fldl %0" : : "m" (dtab[i]));\
681 asm(save " %0\n" : : "m" (*(env)));\
682 asm(restore " %0\n": : "m" (*(env)));\
683 for(i=0;i<5;i++)\
684 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
685 for(i=0;i<5;i++)\
686 printf("res[%d]=%f\n", i, rtab[i]);\
687 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
688 (env)->fpuc,\
689 (env)->fpus & 0xff00,\
690 (env)->fptag);\
693 void test_fenv(void)
695 struct __attribute__((packed)) {
696 uint16_t fpuc;
697 uint16_t dummy1;
698 uint16_t fpus;
699 uint16_t dummy2;
700 uint16_t fptag;
701 uint16_t dummy3;
702 uint32_t ignored[4];
703 long double fpregs[8];
704 } float_env32;
705 struct __attribute__((packed)) {
706 uint16_t fpuc;
707 uint16_t fpus;
708 uint16_t fptag;
709 uint16_t ignored[4];
710 long double fpregs[8];
711 } float_env16;
712 double dtab[8];
713 double rtab[8];
714 int i;
716 for(i=0;i<8;i++)
717 dtab[i] = i + 1;
719 TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
720 TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
721 TEST_ENV(&float_env32, "fnstenv", "fldenv");
722 TEST_ENV(&float_env32, "fnsave", "frstor");
724 /* test for ffree */
725 for(i=0;i<5;i++)
726 asm volatile ("fldl %0" : : "m" (dtab[i]));
727 asm volatile("ffree %st(2)");
728 asm volatile ("fnstenv %0\n" : : "m" (float_env32));
729 asm volatile ("fninit");
730 printf("fptag=%04x\n", float_env32.fptag);
734 #define TEST_FCMOV(a, b, rflags, CC)\
736 double res;\
737 asm("pushq %3\n"\
738 "popfq\n"\
739 "fcmov" CC " %2, %0\n"\
740 : "=t" (res)\
741 : "0" (a), "u" (b), "g" (rflags));\
742 printf("fcmov%s rflags=0x%04llx-> %f\n", \
743 CC, rflags, res);\
746 void test_fcmov(void)
748 double a, b;
749 int64 rflags, i;
751 a = 1.0;
752 b = 2.0;
753 for(i = 0; i < 4; i++) {
754 rflags = 0;
755 if (i & 1)
756 rflags |= CC_C;
757 if (i & 2)
758 rflags |= CC_Z;
759 TEST_FCMOV(a, b, rflags, "b");
760 TEST_FCMOV(a, b, rflags, "e");
761 TEST_FCMOV(a, b, rflags, "be");
762 TEST_FCMOV(a, b, rflags, "nb");
763 TEST_FCMOV(a, b, rflags, "ne");
764 TEST_FCMOV(a, b, rflags, "nbe");
766 TEST_FCMOV(a, b, (int64)0, "u");
767 TEST_FCMOV(a, b, (int64)CC_P, "u");
768 TEST_FCMOV(a, b, (int64)0, "nu");
769 TEST_FCMOV(a, b, (int64)CC_P, "nu");
772 void test_floats(void)
774 test_fops(2, 3);
775 test_fops(1.4, -5);
776 test_fcmp(2, -1);
777 test_fcmp(2, 2);
778 test_fcmp(2, 3);
779 test_fcvt(0.5);
780 test_fcvt(-0.5);
781 test_fcvt(1.0/7.0);
782 test_fcvt(-1.0/9.0);
783 test_fcvt(32768);
784 test_fcvt(-1e20);
785 test_fconst();
786 // REINSTATE (maybe): test_fbcd(1234567890123456);
787 // REINSTATE (maybe): test_fbcd(-123451234567890);
788 // REINSTATE: test_fenv();
789 // REINSTATE: test_fcmov();
792 /**********************************************/
793 #if 0
795 #define TEST_BCD(op, op0, cc_in, cc_mask)\
797 int res, flags;\
798 res = op0;\
799 flags = cc_in;\
800 asm ("push %3\n\t"\
801 "popf\n\t"\
802 #op "\n\t"\
803 "pushf\n\t"\
804 "popl %1\n\t"\
805 : "=a" (res), "=g" (flags)\
806 : "0" (res), "1" (flags));\
807 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
808 #op, op0, res, cc_in, flags & cc_mask);\
811 void test_bcd(void)
813 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
814 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
815 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
816 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
817 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
818 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
819 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
820 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
821 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
822 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
823 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
824 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
825 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
827 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
828 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
829 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
830 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
831 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
832 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
833 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
834 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
835 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
836 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
837 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
838 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
839 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
841 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
842 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
843 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
844 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
845 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
846 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
847 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
848 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
850 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
851 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
852 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
853 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
854 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
855 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
856 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
857 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
859 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
860 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
862 #endif /* 0 */
864 #define TEST_XCHG(op, size, opconst)\
866 int op0, op1;\
867 op0 = 0x12345678;\
868 op1 = 0xfbca7654;\
869 asm(#op " %" size "0, %" size "1" \
870 : "=q" (op0), opconst (op1) \
871 : "0" (op0), "1" (op1));\
872 printf("%-10s A=%08x B=%08x\n",\
873 #op, op0, op1);\
876 #define TEST_CMPXCHG(op, size, opconst, eax)\
878 int op0, op1;\
879 op0 = 0x12345678;\
880 op1 = 0xfbca7654;\
881 asm(#op " %" size "0, %" size "1" \
882 : "=q" (op0), opconst (op1) \
883 : "0" (op0), "1" (op1), "a" (eax));\
884 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
885 #op, eax, op0, op1);\
888 void test_xchg(void)
890 TEST_XCHG(xchgl, "", "=q");
891 TEST_XCHG(xchgw, "w", "=q");
892 TEST_XCHG(xchgb, "b", "=q");
894 TEST_XCHG(xchgl, "", "=m");
895 TEST_XCHG(xchgw, "w", "=m");
896 TEST_XCHG(xchgb, "b", "=m");
898 #if 0
899 TEST_XCHG(xaddl, "", "=q");
900 TEST_XCHG(xaddw, "w", "=q");
901 TEST_XCHG(xaddb, "b", "=q");
904 int res;
905 res = 0x12345678;
906 asm("xaddl %1, %0" : "=r" (res) : "0" (res));
907 printf("xaddl same res=%08x\n", res);
910 TEST_XCHG(xaddl, "", "=m");
911 TEST_XCHG(xaddw, "w", "=m");
912 TEST_XCHG(xaddb, "b", "=m");
913 #endif
914 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
915 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
916 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
918 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
919 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
920 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
922 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
923 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
924 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
926 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
927 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
928 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
929 #if 0
931 uint64_t op0, op1, op2;
932 int i, eflags;
934 for(i = 0; i < 2; i++) {
935 op0 = 0x123456789abcd;
936 if (i == 0)
937 op1 = 0xfbca765423456;
938 else
939 op1 = op0;
940 op2 = 0x6532432432434;
941 asm("cmpxchg8b %1\n"
942 "pushf\n"
943 "popl %2\n"
944 : "=A" (op0), "=m" (op1), "=g" (eflags)
945 : "0" (op0), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
946 printf("cmpxchg8b: op0=%016llx op1=%016llx CC=%02x\n",
947 op0, op1, eflags & CC_Z);
950 #endif
953 /**********************************************/
954 /* segmentation tests */
955 #if 0
956 #include <asm/ldt.h>
957 #include <linux/unistd.h>
958 #include <linux/version.h>
960 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
962 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
963 #define modify_ldt_ldt_s user_desc
964 #endif
966 uint8_t seg_data1[4096];
967 uint8_t seg_data2[4096];
969 #define MK_SEL(n) (((n) << 3) | 7)
971 #define TEST_LR(op, size, seg, mask)\
973 int res, res2;\
974 res = 0x12345678;\
975 asm (op " %" size "2, %" size "0\n" \
976 "movl $0, %1\n"\
977 "jnz 1f\n"\
978 "movl $1, %1\n"\
979 "1:\n"\
980 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
981 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
984 /* NOTE: we use Linux modify_ldt syscall */
985 void test_segs(void)
987 struct modify_ldt_ldt_s ldt;
988 long long ldt_table[3];
989 int res, res2;
990 char tmp;
991 struct {
992 uint32_t offset;
993 uint16_t seg;
994 } __attribute__((packed)) segoff;
996 ldt.entry_number = 1;
997 ldt.base_addr = (unsigned long)&seg_data1;
998 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
999 ldt.seg_32bit = 1;
1000 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1001 ldt.read_exec_only = 0;
1002 ldt.limit_in_pages = 1;
1003 ldt.seg_not_present = 0;
1004 ldt.useable = 1;
1005 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1007 ldt.entry_number = 2;
1008 ldt.base_addr = (unsigned long)&seg_data2;
1009 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
1010 ldt.seg_32bit = 1;
1011 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1012 ldt.read_exec_only = 0;
1013 ldt.limit_in_pages = 1;
1014 ldt.seg_not_present = 0;
1015 ldt.useable = 1;
1016 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1018 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
1019 #if 0
1021 int i;
1022 for(i=0;i<3;i++)
1023 printf("%d: %016Lx\n", i, ldt_table[i]);
1025 #endif
1026 /* do some tests with fs or gs */
1027 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1029 seg_data1[1] = 0xaa;
1030 seg_data2[1] = 0x55;
1032 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
1033 printf("FS[1] = %02x\n", res);
1035 asm volatile ("pushl %%gs\n"
1036 "movl %1, %%gs\n"
1037 "gs movzbl 0x1, %0\n"
1038 "popl %%gs\n"
1039 : "=r" (res)
1040 : "r" (MK_SEL(2)));
1041 printf("GS[1] = %02x\n", res);
1043 /* tests with ds/ss (implicit segment case) */
1044 tmp = 0xa5;
1045 asm volatile ("pushl %%ebp\n\t"
1046 "pushl %%ds\n\t"
1047 "movl %2, %%ds\n\t"
1048 "movl %3, %%ebp\n\t"
1049 "movzbl 0x1, %0\n\t"
1050 "movzbl (%%ebp), %1\n\t"
1051 "popl %%ds\n\t"
1052 "popl %%ebp\n\t"
1053 : "=r" (res), "=r" (res2)
1054 : "r" (MK_SEL(1)), "r" (&tmp));
1055 printf("DS[1] = %02x\n", res);
1056 printf("SS[tmp] = %02x\n", res2);
1058 segoff.seg = MK_SEL(2);
1059 segoff.offset = 0xabcdef12;
1060 asm volatile("lfs %2, %0\n\t"
1061 "movl %%fs, %1\n\t"
1062 : "=r" (res), "=g" (res2)
1063 : "m" (segoff));
1064 printf("FS:reg = %04x:%08x\n", res2, res);
1066 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1067 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1068 TEST_LR("lslw", "w", MK_SEL(2), 0);
1069 TEST_LR("lsll", "", MK_SEL(2), 0);
1071 TEST_LR("larw", "w", 0xfff8, 0);
1072 TEST_LR("larl", "", 0xfff8, 0);
1073 TEST_LR("lslw", "w", 0xfff8, 0);
1074 TEST_LR("lsll", "", 0xfff8, 0);
1076 #endif
1078 #if 0
1079 /* 16 bit code test */
1080 extern char code16_start, code16_end;
1081 extern char code16_func1;
1082 extern char code16_func2;
1083 extern char code16_func3;
1085 void test_code16(void)
1087 struct modify_ldt_ldt_s ldt;
1088 int res, res2;
1090 /* build a code segment */
1091 ldt.entry_number = 1;
1092 ldt.base_addr = (unsigned long)&code16_start;
1093 ldt.limit = &code16_end - &code16_start;
1094 ldt.seg_32bit = 0;
1095 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
1096 ldt.read_exec_only = 0;
1097 ldt.limit_in_pages = 0;
1098 ldt.seg_not_present = 0;
1099 ldt.useable = 1;
1100 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1102 /* call the first function */
1103 asm volatile ("lcall %1, %2"
1104 : "=a" (res)
1105 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
1106 printf("func1() = 0x%08x\n", res);
1107 asm volatile ("lcall %2, %3"
1108 : "=a" (res), "=c" (res2)
1109 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
1110 printf("func2() = 0x%08x spdec=%d\n", res, res2);
1111 asm volatile ("lcall %1, %2"
1112 : "=a" (res)
1113 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1114 printf("func3() = 0x%08x\n", res);
1116 #endif
1118 extern char func_lret32;
1119 extern char func_iret32;
1121 void test_misc(void)
1123 // char table[256];
1124 // int res, i;
1126 #if 0
1127 // REINSTATE
1128 for(i=0;i<256;i++) table[i] = 256 - i;
1129 res = 0x12345678;
1130 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
1131 printf("xlat: EAX=%08x\n", res);
1132 #endif
1133 #if 0
1134 // REINSTATE
1135 asm volatile ("pushl %%cs ; call %1"
1136 : "=a" (res)
1137 : "m" (func_lret32): "memory", "cc");
1138 printf("func_lret32=%x\n", res);
1140 asm volatile ("pushfl ; pushl %%cs ; call %1"
1141 : "=a" (res)
1142 : "m" (func_iret32): "memory", "cc");
1143 printf("func_iret32=%x\n", res);
1144 #endif
1145 #if 0
1146 /* specific popl test */
1147 asm volatile ("pushq $0x9abcdef12345678 ; popl (%%rsp) ; addq $4,%%rsp"
1148 : "=g" (res));
1149 printf("popl esp=%x\n", res);
1150 #endif
1151 #if 0
1152 // REINSTATE
1153 /* specific popw test */
1154 asm volatile ("pushq $12345432 ; pushq $0x9abcdef ; popw (%%rsp) ; addl $2, %%rsp ; popq %0"
1155 : "=g" (res));
1156 printf("popw rsp=%x\n", res);
1157 #endif
1160 uint8_t str_buffer[4096];
1162 #define TEST_STRING1(OP, size, DF, REP)\
1164 int64 rsi, rdi, rax, rcx, rflags;\
1166 rsi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1167 rdi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1168 rax = 0x12345678;\
1169 rcx = 17;\
1171 asm volatile ("pushq $0\n\t"\
1172 "popfq\n\t"\
1173 DF "\n\t"\
1174 REP #OP size "\n\t"\
1175 "cld\n\t"\
1176 "pushfq\n\t"\
1177 "popq %4\n\t"\
1178 : "=S" (rsi), "=D" (rdi), "=a" (rax), "=c" (rcx), "=g" (rflags)\
1179 : "0" (rsi), "1" (rdi), "2" (rax), "3" (rcx));\
1180 printf("%-10s ESI=%016llx EDI=%016llx EAX=%016llx ECX=%016llx EFL=%04llx\n",\
1181 REP #OP size, rsi, rdi, rax, rcx,\
1182 rflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1185 #define TEST_STRING(OP, REP)\
1186 TEST_STRING1(OP, "b", "", REP);\
1187 TEST_STRING1(OP, "w", "", REP);\
1188 TEST_STRING1(OP, "l", "", REP);\
1189 TEST_STRING1(OP, "b", "std", REP);\
1190 TEST_STRING1(OP, "w", "std", REP);\
1191 TEST_STRING1(OP, "l", "std", REP)
1193 void test_string(void)
1195 int64 i;
1196 for(i = 0;i < sizeof(str_buffer); i++)
1197 str_buffer[i] = i + 0x56;
1198 TEST_STRING(stos, "");
1199 TEST_STRING(stos, "rep ");
1200 // REINSTATE: TEST_STRING(lods, ""); /* to verify stos */
1201 // REINSTATE: TEST_STRING(lods, "rep ");
1202 TEST_STRING(movs, "");
1203 TEST_STRING(movs, "rep ");
1204 // REINSTATE: TEST_STRING(lods, ""); /* to verify stos */
1206 /* XXX: better tests */
1207 TEST_STRING(scas, "");
1208 // REINSTATE: TEST_STRING(scas, "repz ");
1209 TEST_STRING(scas, "repnz ");
1210 // REINSTATE: TEST_STRING(cmps, "");
1211 TEST_STRING(cmps, "repz ");
1212 // REINSTATE: TEST_STRING(cmps, "repnz ");
1215 /* VM86 test */
1216 #if 0
1217 static inline void set_bit(uint8_t *a, unsigned int bit)
1219 a[bit / 8] |= (1 << (bit % 8));
1222 static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1224 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1227 static inline void pushw(struct vm86_regs *r, int val)
1229 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1230 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1233 #undef __syscall_return
1234 #define __syscall_return(type, res) \
1235 do { \
1236 return (type) (res); \
1237 } while (0)
1239 _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1241 extern char vm86_code_start;
1242 extern char vm86_code_end;
1244 #define VM86_CODE_CS 0x100
1245 #define VM86_CODE_IP 0x100
1247 void test_vm86(void)
1249 struct vm86plus_struct ctx;
1250 struct vm86_regs *r;
1251 uint8_t *vm86_mem;
1252 int seg, ret;
1254 vm86_mem = mmap((void *)0x00000000, 0x110000,
1255 PROT_WRITE | PROT_READ | PROT_EXEC,
1256 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1257 if (vm86_mem == MAP_FAILED) {
1258 printf("ERROR: could not map vm86 memory");
1259 return;
1261 memset(&ctx, 0, sizeof(ctx));
1263 /* init basic registers */
1264 r = &ctx.regs;
1265 r->eip = VM86_CODE_IP;
1266 r->esp = 0xfffe;
1267 seg = VM86_CODE_CS;
1268 r->cs = seg;
1269 r->ss = seg;
1270 r->ds = seg;
1271 r->es = seg;
1272 r->fs = seg;
1273 r->gs = seg;
1274 r->eflags = VIF_MASK;
1276 /* move code to proper address. We use the same layout as a .com
1277 dos program. */
1278 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1279 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1281 /* mark int 0x21 as being emulated */
1282 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1284 for(;;) {
1285 ret = vm86(VM86_ENTER, &ctx);
1286 switch(VM86_TYPE(ret)) {
1287 case VM86_INTx:
1289 int int_num, ah, v;
1291 int_num = VM86_ARG(ret);
1292 if (int_num != 0x21)
1293 goto unknown_int;
1294 ah = (r->eax >> 8) & 0xff;
1295 switch(ah) {
1296 case 0x00: /* exit */
1297 goto the_end;
1298 case 0x02: /* write char */
1300 uint8_t c = r->edx;
1301 putchar(c);
1303 break;
1304 case 0x09: /* write string */
1306 uint8_t c, *ptr;
1307 ptr = seg_to_linear(r->ds, r->edx);
1308 for(;;) {
1309 c = *ptr++;
1310 if (c == '$')
1311 break;
1312 putchar(c);
1314 r->eax = (r->eax & ~0xff) | '$';
1316 break;
1317 case 0xff: /* extension: write eflags number in edx */
1318 v = (int)r->edx;
1319 #ifndef LINUX_VM86_IOPL_FIX
1320 v &= ~0x3000;
1321 #endif
1322 printf("%08x\n", v);
1323 break;
1324 default:
1325 unknown_int:
1326 printf("unsupported int 0x%02x\n", int_num);
1327 goto the_end;
1330 break;
1331 case VM86_SIGNAL:
1332 /* a signal came, we just ignore that */
1333 break;
1334 case VM86_STI:
1335 break;
1336 default:
1337 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1338 goto the_end;
1341 the_end:
1342 printf("VM86 end\n");
1343 munmap(vm86_mem, 0x110000);
1345 #endif
1347 /* exception tests */
1348 #if 0
1349 #ifndef REG_EAX
1350 #define REG_EAX EAX
1351 #define REG_EBX EBX
1352 #define REG_ECX ECX
1353 #define REG_EDX EDX
1354 #define REG_ESI ESI
1355 #define REG_EDI EDI
1356 #define REG_EBP EBP
1357 #define REG_ESP ESP
1358 #define REG_EIP EIP
1359 #define REG_EFL EFL
1360 #define REG_TRAPNO TRAPNO
1361 #define REG_ERR ERR
1362 #endif
1364 jmp_buf jmp_env;
1365 int v1;
1366 int tab[2];
1368 void sig_handler(int sig, siginfo_t *info, void *puc)
1370 struct ucontext *uc = puc;
1372 printf("si_signo=%d si_errno=%d si_code=%d",
1373 info->si_signo, info->si_errno, info->si_code);
1374 printf(" si_addr=0x%08lx",
1375 (unsigned long)info->si_addr);
1376 printf("\n");
1378 printf("trapno=0x%02x err=0x%08x",
1379 uc->uc_mcontext.gregs[REG_TRAPNO],
1380 uc->uc_mcontext.gregs[REG_ERR]);
1381 printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
1382 printf("\n");
1383 longjmp(jmp_env, 1);
1386 void test_exceptions(void)
1388 struct modify_ldt_ldt_s ldt;
1389 struct sigaction act;
1390 volatile int val;
1392 act.sa_sigaction = sig_handler;
1393 sigemptyset(&act.sa_mask);
1394 act.sa_flags = SA_SIGINFO;
1395 sigaction(SIGFPE, &act, NULL);
1396 sigaction(SIGILL, &act, NULL);
1397 sigaction(SIGSEGV, &act, NULL);
1398 sigaction(SIGBUS, &act, NULL);
1399 sigaction(SIGTRAP, &act, NULL);
1401 /* test division by zero reporting */
1402 printf("DIVZ exception:\n");
1403 if (setjmp(jmp_env) == 0) {
1404 /* now divide by zero */
1405 v1 = 0;
1406 v1 = 2 / v1;
1409 printf("BOUND exception:\n");
1410 if (setjmp(jmp_env) == 0) {
1411 /* bound exception */
1412 tab[0] = 1;
1413 tab[1] = 10;
1414 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1417 printf("segment exceptions:\n");
1418 if (setjmp(jmp_env) == 0) {
1419 /* load an invalid segment */
1420 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1422 if (setjmp(jmp_env) == 0) {
1423 /* null data segment is valid */
1424 asm volatile ("movl %0, %%fs" : : "r" (3));
1425 /* null stack segment */
1426 asm volatile ("movl %0, %%ss" : : "r" (3));
1429 ldt.entry_number = 1;
1430 ldt.base_addr = (unsigned long)&seg_data1;
1431 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1432 ldt.seg_32bit = 1;
1433 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1434 ldt.read_exec_only = 0;
1435 ldt.limit_in_pages = 1;
1436 ldt.seg_not_present = 1;
1437 ldt.useable = 1;
1438 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1440 if (setjmp(jmp_env) == 0) {
1441 /* segment not present */
1442 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1445 /* test SEGV reporting */
1446 printf("PF exception:\n");
1447 if (setjmp(jmp_env) == 0) {
1448 val = 1;
1449 /* we add a nop to test a weird PC retrieval case */
1450 asm volatile ("nop");
1451 /* now store in an invalid address */
1452 *(char *)0x1234 = 1;
1455 /* test SEGV reporting */
1456 printf("PF exception:\n");
1457 if (setjmp(jmp_env) == 0) {
1458 val = 1;
1459 /* read from an invalid address */
1460 v1 = *(char *)0x1234;
1463 /* test illegal instruction reporting */
1464 printf("UD2 exception:\n");
1465 if (setjmp(jmp_env) == 0) {
1466 /* now execute an invalid instruction */
1467 asm volatile("ud2");
1469 printf("lock nop exception:\n");
1470 if (setjmp(jmp_env) == 0) {
1471 /* now execute an invalid instruction */
1472 asm volatile("lock nop");
1475 printf("INT exception:\n");
1476 if (setjmp(jmp_env) == 0) {
1477 asm volatile ("int $0xfd");
1479 if (setjmp(jmp_env) == 0) {
1480 asm volatile ("int $0x01");
1482 if (setjmp(jmp_env) == 0) {
1483 asm volatile (".byte 0xcd, 0x03");
1485 if (setjmp(jmp_env) == 0) {
1486 asm volatile ("int $0x04");
1488 if (setjmp(jmp_env) == 0) {
1489 asm volatile ("int $0x05");
1492 printf("INT3 exception:\n");
1493 if (setjmp(jmp_env) == 0) {
1494 asm volatile ("int3");
1497 printf("CLI exception:\n");
1498 if (setjmp(jmp_env) == 0) {
1499 asm volatile ("cli");
1502 printf("STI exception:\n");
1503 if (setjmp(jmp_env) == 0) {
1504 asm volatile ("cli");
1507 printf("INTO exception:\n");
1508 if (setjmp(jmp_env) == 0) {
1509 /* overflow exception */
1510 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1513 printf("OUTB exception:\n");
1514 if (setjmp(jmp_env) == 0) {
1515 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1518 printf("INB exception:\n");
1519 if (setjmp(jmp_env) == 0) {
1520 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1523 printf("REP OUTSB exception:\n");
1524 if (setjmp(jmp_env) == 0) {
1525 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1528 printf("REP INSB exception:\n");
1529 if (setjmp(jmp_env) == 0) {
1530 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1533 printf("HLT exception:\n");
1534 if (setjmp(jmp_env) == 0) {
1535 asm volatile ("hlt");
1538 printf("single step exception:\n");
1539 val = 0;
1540 if (setjmp(jmp_env) == 0) {
1541 asm volatile ("pushf\n"
1542 "orl $0x00100, (%%esp)\n"
1543 "popf\n"
1544 "movl $0xabcd, %0\n"
1545 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1547 printf("val=0x%x\n", val);
1550 /* specific precise single step test */
1551 void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1553 struct ucontext *uc = puc;
1554 printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1557 const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1558 uint8_t sstep_buf2[4];
1560 void test_single_step(void)
1562 struct sigaction act;
1563 volatile int val;
1564 int i;
1566 val = 0;
1567 act.sa_sigaction = sig_trap_handler;
1568 sigemptyset(&act.sa_mask);
1569 act.sa_flags = SA_SIGINFO;
1570 sigaction(SIGTRAP, &act, NULL);
1571 asm volatile ("pushf\n"
1572 "orl $0x00100, (%%esp)\n"
1573 "popf\n"
1574 "movl $0xabcd, %0\n"
1576 /* jmp test */
1577 "movl $3, %%ecx\n"
1578 "1:\n"
1579 "addl $1, %0\n"
1580 "decl %%ecx\n"
1581 "jnz 1b\n"
1583 /* movsb: the single step should stop at each movsb iteration */
1584 "movl $sstep_buf1, %%esi\n"
1585 "movl $sstep_buf2, %%edi\n"
1586 "movl $0, %%ecx\n"
1587 "rep movsb\n"
1588 "movl $3, %%ecx\n"
1589 "rep movsb\n"
1590 "movl $1, %%ecx\n"
1591 "rep movsb\n"
1593 /* cmpsb: the single step should stop at each cmpsb iteration */
1594 "movl $sstep_buf1, %%esi\n"
1595 "movl $sstep_buf2, %%edi\n"
1596 "movl $0, %%ecx\n"
1597 "rep cmpsb\n"
1598 "movl $4, %%ecx\n"
1599 "rep cmpsb\n"
1601 /* getpid() syscall: single step should skip one
1602 instruction */
1603 "movl $20, %%eax\n"
1604 "int $0x80\n"
1605 "movl $0, %%eax\n"
1607 /* when modifying SS, trace is not done on the next
1608 instruction */
1609 "movl %%ss, %%ecx\n"
1610 "movl %%ecx, %%ss\n"
1611 "addl $1, %0\n"
1612 "movl $1, %%eax\n"
1613 "movl %%ecx, %%ss\n"
1614 "jmp 1f\n"
1615 "addl $1, %0\n"
1616 "1:\n"
1617 "movl $1, %%eax\n"
1618 "pushl %%ecx\n"
1619 "popl %%ss\n"
1620 "addl $1, %0\n"
1621 "movl $1, %%eax\n"
1623 "pushf\n"
1624 "andl $~0x00100, (%%esp)\n"
1625 "popf\n"
1626 : "=m" (val)
1628 : "cc", "memory", "eax", "ecx", "esi", "edi");
1629 printf("val=%d\n", val);
1630 for(i = 0; i < 4; i++)
1631 printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1634 /* self modifying code test */
1635 uint8_t code[] = {
1636 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1637 0xc3, /* ret */
1640 asm("smc_code2:\n"
1641 "movl 4(%esp), %eax\n"
1642 "movl %eax, smc_patch_addr2 + 1\n"
1643 "nop\n"
1644 "nop\n"
1645 "nop\n"
1646 "nop\n"
1647 "nop\n"
1648 "nop\n"
1649 "nop\n"
1650 "nop\n"
1651 "smc_patch_addr2:\n"
1652 "movl $1, %eax\n"
1653 "ret\n");
1655 typedef int FuncType(void);
1656 extern int smc_code2(int);
1657 void test_self_modifying_code(void)
1659 int i;
1661 printf("self modifying code:\n");
1662 printf("func1 = 0x%x\n", ((FuncType *)code)());
1663 for(i = 2; i <= 4; i++) {
1664 code[1] = i;
1665 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1668 /* more difficult test : the modified code is just after the
1669 modifying instruction. It is forbidden in Intel specs, but it
1670 is used by old DOS programs */
1671 for(i = 2; i <= 4; i++) {
1672 printf("smc_code2(%d) = %d\n", i, smc_code2(i));
1676 static void *call_end __init_call = NULL;
1677 #endif
1679 int main(int argc, char **argv)
1681 void **ptr;
1682 void (*func)(void);
1684 #if 1
1685 ptr = &call_start + 1;
1686 while (*ptr != NULL) {
1687 func = *ptr++;
1688 func();
1690 #endif
1691 test_bsx(); //REINSTATE64
1692 test_mul();
1693 test_jcc();
1694 // test_floats(); REINSTATE64
1695 //test_bcd();
1696 //test_xchg(); REINSTATE64
1697 test_string();
1698 //test_misc(); // REINSTATE
1699 test_lea();
1700 // test_segs();
1701 //test_code16();
1702 //test_vm86();
1703 //test_exceptions();
1704 //test_self_modifying_code();
1705 //test_single_step();
1706 printf("bye\n");
1707 return 0;