]> git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
fixed imul im test - added TEST_VM86 define for x86_64 tests
[qemu.git] / tests / test-i386.c
1 /*
2 * x86 CPU test
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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.
19 */
20 #define _GNU_SOURCE
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <math.h>
26 #include <signal.h>
27 #include <setjmp.h>
28 #include <errno.h>
29 #include <sys/ucontext.h>
30 #include <sys/mman.h>
31 #include <asm/vm86.h>
32
33 #define TEST_CMOV 0
34 #define TEST_FCOMI 0
35 #define TEST_VM86
36 //#define LINUX_VM86_IOPL_FIX
37 //#define TEST_P4_FLAGS
38
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
43
44 #define CC_C 0x0001
45 #define CC_P 0x0004
46 #define CC_A 0x0010
47 #define CC_Z 0x0040
48 #define CC_S 0x0080
49 #define CC_O 0x0800
50
51 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
52
53 static void *call_start __init_call = NULL;
54
55 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
56
57 #define OP add
58 #include "test-i386.h"
59
60 #define OP sub
61 #include "test-i386.h"
62
63 #define OP xor
64 #include "test-i386.h"
65
66 #define OP and
67 #include "test-i386.h"
68
69 #define OP or
70 #include "test-i386.h"
71
72 #define OP cmp
73 #include "test-i386.h"
74
75 #define OP adc
76 #define OP_CC
77 #include "test-i386.h"
78
79 #define OP sbb
80 #define OP_CC
81 #include "test-i386.h"
82
83 #define OP inc
84 #define OP_CC
85 #define OP1
86 #include "test-i386.h"
87
88 #define OP dec
89 #define OP_CC
90 #define OP1
91 #include "test-i386.h"
92
93 #define OP neg
94 #define OP_CC
95 #define OP1
96 #include "test-i386.h"
97
98 #define OP not
99 #define OP_CC
100 #define OP1
101 #include "test-i386.h"
102
103 #undef CC_MASK
104 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
105
106 #define OP shl
107 #include "test-i386-shift.h"
108
109 #define OP shr
110 #include "test-i386-shift.h"
111
112 #define OP sar
113 #include "test-i386-shift.h"
114
115 #define OP rol
116 #include "test-i386-shift.h"
117
118 #define OP ror
119 #include "test-i386-shift.h"
120
121 #define OP rcr
122 #define OP_CC
123 #include "test-i386-shift.h"
124
125 #define OP rcl
126 #define OP_CC
127 #include "test-i386-shift.h"
128
129 #define OP shld
130 #define OP_SHIFTD
131 #define OP_NOBYTE
132 #include "test-i386-shift.h"
133
134 #define OP shrd
135 #define OP_SHIFTD
136 #define OP_NOBYTE
137 #include "test-i386-shift.h"
138
139 /* XXX: should be more precise ? */
140 #undef CC_MASK
141 #define CC_MASK (CC_C)
142
143 #define OP bt
144 #define OP_NOBYTE
145 #include "test-i386-shift.h"
146
147 #define OP bts
148 #define OP_NOBYTE
149 #include "test-i386-shift.h"
150
151 #define OP btr
152 #define OP_NOBYTE
153 #include "test-i386-shift.h"
154
155 #define OP btc
156 #define OP_NOBYTE
157 #include "test-i386-shift.h"
158
159 /* lea test (modrm support) */
160 #define TEST_LEA(STR)\
161 {\
162 asm("leal " STR ", %0"\
163 : "=r" (res)\
164 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
165 printf("lea %s = %08x\n", STR, res);\
166 }
167
168 #define TEST_LEA16(STR)\
169 {\
170 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
171 : "=wq" (res)\
172 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
173 printf("lea %s = %08x\n", STR, res);\
174 }
175
176
177 void test_lea(void)
178 {
179 int eax, ebx, ecx, edx, esi, edi, res;
180 eax = 0x0001;
181 ebx = 0x0002;
182 ecx = 0x0004;
183 edx = 0x0008;
184 esi = 0x0010;
185 edi = 0x0020;
186
187 TEST_LEA("0x4000");
188
189 TEST_LEA("(%%eax)");
190 TEST_LEA("(%%ebx)");
191 TEST_LEA("(%%ecx)");
192 TEST_LEA("(%%edx)");
193 TEST_LEA("(%%esi)");
194 TEST_LEA("(%%edi)");
195
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)");
202
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)");
209
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)");
216
217 TEST_LEA("0x40(%%eax, %%ecx)");
218 TEST_LEA("0x4000(%%ebx, %%edx)");
219
220 TEST_LEA("(%%ecx, %%ecx, 2)");
221 TEST_LEA("(%%edx, %%ecx, 4)");
222 TEST_LEA("(%%esi, %%ecx, 8)");
223
224 TEST_LEA("(,%%eax, 2)");
225 TEST_LEA("(,%%ebx, 4)");
226 TEST_LEA("(,%%ecx, 8)");
227
228 TEST_LEA("0x40(,%%eax, 2)");
229 TEST_LEA("0x40(,%%ebx, 4)");
230 TEST_LEA("0x40(,%%ecx, 8)");
231
232
233 TEST_LEA("-10(%%ecx, %%ecx, 2)");
234 TEST_LEA("-10(%%edx, %%ecx, 4)");
235 TEST_LEA("-10(%%esi, %%ecx, 8)");
236
237 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
238 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
239 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
240
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)");
257 }
258
259 #define TEST_JCC(JCC, v1, v2)\
260 {\
261 int res;\
262 asm("movl $1, %0\n\t"\
263 "cmpl %2, %1\n\t"\
264 "j" JCC " 1f\n\t"\
265 "movl $0, %0\n\t"\
266 "1:\n\t"\
267 : "=r" (res)\
268 : "r" (v1), "r" (v2));\
269 printf("%-10s %d\n", "j" JCC, res);\
270 \
271 asm("movl $0, %0\n\t"\
272 "cmpl %2, %1\n\t"\
273 "set" JCC " %b0\n\t"\
274 : "=r" (res)\
275 : "r" (v1), "r" (v2));\
276 printf("%-10s %d\n", "set" JCC, res);\
277 if (TEST_CMOV) {\
278 asm("movl $0x12345678, %0\n\t"\
279 "cmpl %2, %1\n\t"\
280 "cmov" JCC "l %3, %0\n\t"\
281 : "=r" (res)\
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"\
285 "cmpl %2, %1\n\t"\
286 "cmov" JCC "w %w3, %w0\n\t"\
287 : "=r" (res)\
288 : "r" (v1), "r" (v2), "r" (1));\
289 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
290 } \
291 }
292
293 /* various jump tests */
294 void test_jcc(void)
295 {
296 TEST_JCC("ne", 1, 1);
297 TEST_JCC("ne", 1, 0);
298
299 TEST_JCC("e", 1, 1);
300 TEST_JCC("e", 1, 0);
301
302 TEST_JCC("l", 1, 1);
303 TEST_JCC("l", 1, 0);
304 TEST_JCC("l", 1, -1);
305
306 TEST_JCC("le", 1, 1);
307 TEST_JCC("le", 1, 0);
308 TEST_JCC("le", 1, -1);
309
310 TEST_JCC("ge", 1, 1);
311 TEST_JCC("ge", 1, 0);
312 TEST_JCC("ge", -1, 1);
313
314 TEST_JCC("g", 1, 1);
315 TEST_JCC("g", 1, 0);
316 TEST_JCC("g", 1, -1);
317
318 TEST_JCC("b", 1, 1);
319 TEST_JCC("b", 1, 0);
320 TEST_JCC("b", 1, -1);
321
322 TEST_JCC("be", 1, 1);
323 TEST_JCC("be", 1, 0);
324 TEST_JCC("be", 1, -1);
325
326 TEST_JCC("ae", 1, 1);
327 TEST_JCC("ae", 1, 0);
328 TEST_JCC("ae", 1, -1);
329
330 TEST_JCC("a", 1, 1);
331 TEST_JCC("a", 1, 0);
332 TEST_JCC("a", 1, -1);
333
334
335 TEST_JCC("p", 1, 1);
336 TEST_JCC("p", 1, 0);
337
338 TEST_JCC("np", 1, 1);
339 TEST_JCC("np", 1, 0);
340
341 TEST_JCC("o", 0x7fffffff, 0);
342 TEST_JCC("o", 0x7fffffff, -1);
343
344 TEST_JCC("no", 0x7fffffff, 0);
345 TEST_JCC("no", 0x7fffffff, -1);
346
347 TEST_JCC("s", 0, 1);
348 TEST_JCC("s", 0, -1);
349 TEST_JCC("s", 0, 0);
350
351 TEST_JCC("ns", 0, 1);
352 TEST_JCC("ns", 0, -1);
353 TEST_JCC("ns", 0, 0);
354 }
355
356 #undef CC_MASK
357 #ifdef TEST_P4_FLAGS
358 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
359 #else
360 #define CC_MASK (CC_O | CC_C)
361 #endif
362
363 #define OP mul
364 #include "test-i386-muldiv.h"
365
366 #define OP imul
367 #include "test-i386-muldiv.h"
368
369 void test_imulw2(int op0, int op1)
370 {
371 int res, s1, s0, flags;
372 s0 = op0;
373 s1 = op1;
374 res = s0;
375 flags = 0;
376 asm volatile ("push %4\n\t"
377 "popf\n\t"
378 "imulw %w2, %w0\n\t"
379 "pushf\n\t"
380 "popl %1\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);
385 }
386
387 void test_imull2(int op0, int op1)
388 {
389 int res, s1, s0, flags;
390 s0 = op0;
391 s1 = op1;
392 res = s0;
393 flags = 0;
394 asm volatile ("push %4\n\t"
395 "popf\n\t"
396 "imull %2, %0\n\t"
397 "pushf\n\t"
398 "popl %1\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);
403 }
404
405 #define TEST_IMUL_IM(size, size1, op0, op1)\
406 {\
407 int res, flags;\
408 flags = 0;\
409 res = 0;\
410 asm volatile ("push %3\n\t"\
411 "popf\n\t"\
412 "imul" size " $" #op0 ", %" size1 "2, %" size1 "0\n\t" \
413 "pushf\n\t"\
414 "popl %1\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);\
419 }
420
421
422 #undef CC_MASK
423 #define CC_MASK (0)
424
425 #define OP div
426 #include "test-i386-muldiv.h"
427
428 #define OP idiv
429 #include "test-i386-muldiv.h"
430
431 void test_mul(void)
432 {
433 test_imulb(0x1234561d, 4);
434 test_imulb(3, -4);
435 test_imulb(0x80, 0x80);
436 test_imulb(0x10, 0x10);
437
438 test_imulw(0, 0x1234001d, 45);
439 test_imulw(0, 23, -45);
440 test_imulw(0, 0x8000, 0x8000);
441 test_imulw(0, 0x100, 0x100);
442
443 test_imull(0, 0x1234001d, 45);
444 test_imull(0, 23, -45);
445 test_imull(0, 0x80000000, 0x80000000);
446 test_imull(0, 0x10000, 0x10000);
447
448 test_mulb(0x1234561d, 4);
449 test_mulb(3, -4);
450 test_mulb(0x80, 0x80);
451 test_mulb(0x10, 0x10);
452
453 test_mulw(0, 0x1234001d, 45);
454 test_mulw(0, 23, -45);
455 test_mulw(0, 0x8000, 0x8000);
456 test_mulw(0, 0x100, 0x100);
457
458 test_mull(0, 0x1234001d, 45);
459 test_mull(0, 23, -45);
460 test_mull(0, 0x80000000, 0x80000000);
461 test_mull(0, 0x10000, 0x10000);
462
463 test_imulw2(0x1234001d, 45);
464 test_imulw2(23, -45);
465 test_imulw2(0x8000, 0x8000);
466 test_imulw2(0x100, 0x100);
467
468 test_imull2(0x1234001d, 45);
469 test_imull2(23, -45);
470 test_imull2(0x80000000, 0x80000000);
471 test_imull2(0x10000, 0x10000);
472
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);
477
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);
482
483 test_idivb(0x12341678, 0x127e);
484 test_idivb(0x43210123, -5);
485 test_idivb(0x12340004, -1);
486
487 test_idivw(0, 0x12345678, 12347);
488 test_idivw(0, -23223, -45);
489 test_idivw(0, 0x12348000, -1);
490 test_idivw(0x12343, 0x12345678, 0x81238567);
491
492 test_idivl(0, 0x12345678, 12347);
493 test_idivl(0, -233223, -45);
494 test_idivl(0, 0x80000000, -1);
495 test_idivl(0x12343, 0x12345678, 0x81234567);
496
497 test_divb(0x12341678, 0x127e);
498 test_divb(0x43210123, -5);
499 test_divb(0x12340004, -1);
500
501 test_divw(0, 0x12345678, 12347);
502 test_divw(0, -23223, -45);
503 test_divw(0, 0x12348000, -1);
504 test_divw(0x12343, 0x12345678, 0x81238567);
505
506 test_divl(0, 0x12345678, 12347);
507 test_divl(0, -233223, -45);
508 test_divl(0, 0x80000000, -1);
509 test_divl(0x12343, 0x12345678, 0x81234567);
510 }
511
512 #define TEST_BSX(op, size, op0)\
513 {\
514 int res, val, resz;\
515 val = op0;\
516 asm("xorl %1, %1\n"\
517 "movl $0x12345678, %0\n"\
518 #op " %" size "2, %" size "0 ; setz %b1" \
519 : "=r" (res), "=q" (resz)\
520 : "g" (val));\
521 printf("%-10s A=%08x R=%08x %d\n", #op, val, res, resz);\
522 }
523
524 void test_bsx(void)
525 {
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);
534 }
535
536 /**********************************************/
537
538 void test_fops(double a, double b)
539 {
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)));
556
557 }
558
559 void test_fcmp(double a, double b)
560 {
561 printf("(%f<%f)=%d\n",
562 a, b, a < b);
563 printf("(%f<=%f)=%d\n",
564 a, b, a <= b);
565 printf("(%f==%f)=%d\n",
566 a, b, a == b);
567 printf("(%f>%f)=%d\n",
568 a, b, a > b);
569 printf("(%f<=%f)=%d\n",
570 a, b, a >= b);
571 if (TEST_FCOMI) {
572 unsigned int eflags;
573 /* test f(u)comi instruction */
574 asm("fcomi %2, %1\n"
575 "pushf\n"
576 "pop %0\n"
577 : "=r" (eflags)
578 : "t" (a), "u" (b));
579 printf("fcomi(%f %f)=%08x\n", a, b, eflags & (CC_Z | CC_P | CC_C));
580 }
581 }
582
583 void test_fcvt(double a)
584 {
585 float fa;
586 long double la;
587 int16_t fpuc;
588 int i;
589 int64_t lla;
590 int ia;
591 int16_t wa;
592 double ra;
593
594 fa = a;
595 la = 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));
601
602 /* test all roundings */
603 asm volatile ("fstcw %0" : "=m" (fpuc));
604 for(i=0;i<4;i++) {
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);
615 }
616 }
617
618 #define TEST(N) \
619 asm("fld" #N : "=t" (a)); \
620 printf("fld" #N "= %f\n", a);
621
622 void test_fconst(void)
623 {
624 double a;
625 TEST(1);
626 TEST(l2t);
627 TEST(l2e);
628 TEST(pi);
629 TEST(lg2);
630 TEST(ln2);
631 TEST(z);
632 }
633
634 void test_fbcd(double a)
635 {
636 unsigned short bcd[5];
637 double b;
638
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);
643 }
644
645 #define TEST_ENV(env, save, restore)\
646 {\
647 memset((env), 0xaa, sizeof(*(env)));\
648 for(i=0;i<5;i++)\
649 asm volatile ("fldl %0" : : "m" (dtab[i]));\
650 asm(save " %0\n" : : "m" (*(env)));\
651 asm(restore " %0\n": : "m" (*(env)));\
652 for(i=0;i<5;i++)\
653 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
654 for(i=0;i<5;i++)\
655 printf("res[%d]=%f\n", i, rtab[i]);\
656 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
657 (env)->fpuc,\
658 (env)->fpus & 0xff00,\
659 (env)->fptag);\
660 }
661
662 void test_fenv(void)
663 {
664 struct __attribute__((packed)) {
665 uint16_t fpuc;
666 uint16_t dummy1;
667 uint16_t fpus;
668 uint16_t dummy2;
669 uint16_t fptag;
670 uint16_t dummy3;
671 uint32_t ignored[4];
672 long double fpregs[8];
673 } float_env32;
674 struct __attribute__((packed)) {
675 uint16_t fpuc;
676 uint16_t fpus;
677 uint16_t fptag;
678 uint16_t ignored[4];
679 long double fpregs[8];
680 } float_env16;
681 double dtab[8];
682 double rtab[8];
683 int i;
684
685 for(i=0;i<8;i++)
686 dtab[i] = i + 1;
687
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");
692
693 /* test for ffree */
694 for(i=0;i<5;i++)
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);
700 }
701
702
703 #define TEST_FCMOV(a, b, eflags, CC)\
704 {\
705 double res;\
706 asm("push %3\n"\
707 "popf\n"\
708 "fcmov" CC " %2, %0\n"\
709 : "=t" (res)\
710 : "0" (a), "u" (b), "g" (eflags));\
711 printf("fcmov%s eflags=0x%04x-> %f\n", \
712 CC, eflags, res);\
713 }
714
715 void test_fcmov(void)
716 {
717 double a, b;
718 int eflags, i;
719
720 a = 1.0;
721 b = 2.0;
722 for(i = 0; i < 4; i++) {
723 eflags = 0;
724 if (i & 1)
725 eflags |= CC_C;
726 if (i & 2)
727 eflags |= CC_Z;
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");
734 }
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");
739 }
740
741 void test_floats(void)
742 {
743 test_fops(2, 3);
744 test_fops(1.4, -5);
745 test_fcmp(2, -1);
746 test_fcmp(2, 2);
747 test_fcmp(2, 3);
748 test_fcvt(0.5);
749 test_fcvt(-0.5);
750 test_fcvt(1.0/7.0);
751 test_fcvt(-1.0/9.0);
752 test_fcvt(32768);
753 test_fcvt(-1e20);
754 test_fconst();
755 test_fbcd(1234567890123456);
756 test_fbcd(-123451234567890);
757 test_fenv();
758 if (TEST_CMOV) {
759 test_fcmov();
760 }
761 }
762
763 /**********************************************/
764
765 #define TEST_BCD(op, op0, cc_in, cc_mask)\
766 {\
767 int res, flags;\
768 res = op0;\
769 flags = cc_in;\
770 asm ("push %3\n\t"\
771 "popf\n\t"\
772 #op "\n\t"\
773 "pushf\n\t"\
774 "popl %1\n\t"\
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);\
779 }
780
781 void test_bcd(void)
782 {
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));
796
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));
810
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));
819
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));
828
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));
831 }
832
833 #define TEST_XCHG(op, size, opconst)\
834 {\
835 int op0, op1;\
836 op0 = 0x12345678;\
837 op1 = 0xfbca7654;\
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",\
842 #op, op0, op1);\
843 }
844
845 #define TEST_CMPXCHG(op, size, opconst, eax)\
846 {\
847 int op0, op1;\
848 op0 = 0x12345678;\
849 op1 = 0xfbca7654;\
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);\
855 }
856
857 void test_xchg(void)
858 {
859 TEST_XCHG(xchgl, "", "=q");
860 TEST_XCHG(xchgw, "w", "=q");
861 TEST_XCHG(xchgb, "b", "=q");
862
863 TEST_XCHG(xchgl, "", "=m");
864 TEST_XCHG(xchgw, "w", "=m");
865 TEST_XCHG(xchgb, "b", "=m");
866
867 TEST_XCHG(xaddl, "", "=q");
868 TEST_XCHG(xaddw, "w", "=q");
869 TEST_XCHG(xaddb, "b", "=q");
870
871 {
872 int res;
873 res = 0x12345678;
874 asm("xaddl %1, %0" : "=r" (res) : "0" (res));
875 printf("xaddl same res=%08x\n", res);
876 }
877
878 TEST_XCHG(xaddl, "", "=m");
879 TEST_XCHG(xaddw, "w", "=m");
880 TEST_XCHG(xaddb, "b", "=m");
881
882 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
883 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
884 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
885
886 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
887 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
888 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
889
890 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
891 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
892 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
893
894 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
895 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
896 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
897
898 {
899 uint64_t op0, op1, op2;
900 int i, eflags;
901
902 for(i = 0; i < 2; i++) {
903 op0 = 0x123456789abcd;
904 if (i == 0)
905 op1 = 0xfbca765423456;
906 else
907 op1 = op0;
908 op2 = 0x6532432432434;
909 asm("cmpxchg8b %1\n"
910 "pushf\n"
911 "popl %2\n"
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);
916 }
917 }
918 }
919
920 /**********************************************/
921 /* segmentation tests */
922
923 #include <asm/ldt.h>
924 #include <linux/unistd.h>
925 #include <linux/version.h>
926
927 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
928
929 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
930 #define modify_ldt_ldt_s user_desc
931 #endif
932
933 uint8_t seg_data1[4096];
934 uint8_t seg_data2[4096];
935
936 #define MK_SEL(n) (((n) << 3) | 7)
937
938 #define TEST_LR(op, size, seg, mask)\
939 {\
940 int res, res2;\
941 res = 0x12345678;\
942 asm (op " %" size "2, %" size "0\n" \
943 "movl $0, %1\n"\
944 "jnz 1f\n"\
945 "movl $1, %1\n"\
946 "1:\n"\
947 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
948 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
949 }
950
951 /* NOTE: we use Linux modify_ldt syscall */
952 void test_segs(void)
953 {
954 struct modify_ldt_ldt_s ldt;
955 long long ldt_table[3];
956 int res, res2;
957 char tmp;
958 struct {
959 uint32_t offset;
960 uint16_t seg;
961 } __attribute__((packed)) segoff;
962
963 ldt.entry_number = 1;
964 ldt.base_addr = (unsigned long)&seg_data1;
965 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
966 ldt.seg_32bit = 1;
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;
971 ldt.useable = 1;
972 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
973
974 ldt.entry_number = 2;
975 ldt.base_addr = (unsigned long)&seg_data2;
976 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
977 ldt.seg_32bit = 1;
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;
982 ldt.useable = 1;
983 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
984
985 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
986 #if 0
987 {
988 int i;
989 for(i=0;i<3;i++)
990 printf("%d: %016Lx\n", i, ldt_table[i]);
991 }
992 #endif
993 /* do some tests with fs or gs */
994 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
995
996 seg_data1[1] = 0xaa;
997 seg_data2[1] = 0x55;
998
999 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
1000 printf("FS[1] = %02x\n", res);
1001
1002 asm volatile ("pushl %%gs\n"
1003 "movl %1, %%gs\n"
1004 "gs movzbl 0x1, %0\n"
1005 "popl %%gs\n"
1006 : "=r" (res)
1007 : "r" (MK_SEL(2)));
1008 printf("GS[1] = %02x\n", res);
1009
1010 /* tests with ds/ss (implicit segment case) */
1011 tmp = 0xa5;
1012 asm volatile ("pushl %%ebp\n\t"
1013 "pushl %%ds\n\t"
1014 "movl %2, %%ds\n\t"
1015 "movl %3, %%ebp\n\t"
1016 "movzbl 0x1, %0\n\t"
1017 "movzbl (%%ebp), %1\n\t"
1018 "popl %%ds\n\t"
1019 "popl %%ebp\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);
1024
1025 segoff.seg = MK_SEL(2);
1026 segoff.offset = 0xabcdef12;
1027 asm volatile("lfs %2, %0\n\t"
1028 "movl %%fs, %1\n\t"
1029 : "=r" (res), "=g" (res2)
1030 : "m" (segoff));
1031 printf("FS:reg = %04x:%08x\n", res2, res);
1032
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);
1037
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);
1042 }
1043
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;
1049
1050 void test_code16(void)
1051 {
1052 struct modify_ldt_ldt_s ldt;
1053 int res, res2;
1054
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;
1059 ldt.seg_32bit = 0;
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;
1064 ldt.useable = 1;
1065 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1066
1067 /* call the first function */
1068 asm volatile ("lcall %1, %2"
1069 : "=a" (res)
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"
1077 : "=a" (res)
1078 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1079 printf("func3() = 0x%08x\n", res);
1080 }
1081
1082 extern char func_lret32;
1083 extern char func_iret32;
1084
1085 void test_misc(void)
1086 {
1087 char table[256];
1088 int res, i;
1089
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
1095 asm volatile ("pushl %%cs ; call %1"
1096 : "=a" (res)
1097 : "m" (func_lret32): "memory", "cc");
1098 printf("func_lret32=%x\n", res);
1099
1100 asm volatile ("pushfl ; pushl %%cs ; call %1"
1101 : "=a" (res)
1102 : "m" (func_iret32): "memory", "cc");
1103 printf("func_iret32=%x\n", res);
1104
1105 /* specific popl test */
1106 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1107 : "=g" (res));
1108 printf("popl esp=%x\n", res);
1109
1110 /* specific popw test */
1111 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1112 : "=g" (res));
1113 printf("popw esp=%x\n", res);
1114 }
1115
1116 uint8_t str_buffer[4096];
1117
1118 #define TEST_STRING1(OP, size, DF, REP)\
1119 {\
1120 int esi, edi, eax, ecx, eflags;\
1121 \
1122 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1123 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1124 eax = 0x12345678;\
1125 ecx = 17;\
1126 \
1127 asm volatile ("pushl $0\n\t"\
1128 "popf\n\t"\
1129 DF "\n\t"\
1130 REP #OP size "\n\t"\
1131 "cld\n\t"\
1132 "pushf\n\t"\
1133 "popl %4\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));\
1139 }
1140
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)
1148
1149 void test_string(void)
1150 {
1151 int i;
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 */
1161
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 ");
1169 }
1170
1171 /* VM86 test */
1172
1173 static inline void set_bit(uint8_t *a, unsigned int bit)
1174 {
1175 a[bit / 8] |= (1 << (bit % 8));
1176 }
1177
1178 static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1179 {
1180 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1181 }
1182
1183 static inline void pushw(struct vm86_regs *r, int val)
1184 {
1185 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1186 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1187 }
1188
1189 #undef __syscall_return
1190 #define __syscall_return(type, res) \
1191 do { \
1192 return (type) (res); \
1193 } while (0)
1194
1195 _syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1196
1197 extern char vm86_code_start;
1198 extern char vm86_code_end;
1199
1200 #define VM86_CODE_CS 0x100
1201 #define VM86_CODE_IP 0x100
1202
1203 void test_vm86(void)
1204 {
1205 struct vm86plus_struct ctx;
1206 struct vm86_regs *r;
1207 uint8_t *vm86_mem;
1208 int seg, ret;
1209
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");
1215 return;
1216 }
1217 memset(&ctx, 0, sizeof(ctx));
1218
1219 /* init basic registers */
1220 r = &ctx.regs;
1221 r->eip = VM86_CODE_IP;
1222 r->esp = 0xfffe;
1223 seg = VM86_CODE_CS;
1224 r->cs = seg;
1225 r->ss = seg;
1226 r->ds = seg;
1227 r->es = seg;
1228 r->fs = seg;
1229 r->gs = seg;
1230 r->eflags = VIF_MASK;
1231
1232 /* move code to proper address. We use the same layout as a .com
1233 dos program. */
1234 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1235 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1236
1237 /* mark int 0x21 as being emulated */
1238 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1239
1240 for(;;) {
1241 ret = vm86(VM86_ENTER, &ctx);
1242 switch(VM86_TYPE(ret)) {
1243 case VM86_INTx:
1244 {
1245 int int_num, ah, v;
1246
1247 int_num = VM86_ARG(ret);
1248 if (int_num != 0x21)
1249 goto unknown_int;
1250 ah = (r->eax >> 8) & 0xff;
1251 switch(ah) {
1252 case 0x00: /* exit */
1253 goto the_end;
1254 case 0x02: /* write char */
1255 {
1256 uint8_t c = r->edx;
1257 putchar(c);
1258 }
1259 break;
1260 case 0x09: /* write string */
1261 {
1262 uint8_t c, *ptr;
1263 ptr = seg_to_linear(r->ds, r->edx);
1264 for(;;) {
1265 c = *ptr++;
1266 if (c == '$')
1267 break;
1268 putchar(c);
1269 }
1270 r->eax = (r->eax & ~0xff) | '$';
1271 }
1272 break;
1273 case 0xff: /* extension: write eflags number in edx */
1274 v = (int)r->edx;
1275 #ifndef LINUX_VM86_IOPL_FIX
1276 v &= ~0x3000;
1277 #endif
1278 printf("%08x\n", v);
1279 break;
1280 default:
1281 unknown_int:
1282 printf("unsupported int 0x%02x\n", int_num);
1283 goto the_end;
1284 }
1285 }
1286 break;
1287 case VM86_SIGNAL:
1288 /* a signal came, we just ignore that */
1289 break;
1290 case VM86_STI:
1291 break;
1292 default:
1293 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1294 goto the_end;
1295 }
1296 }
1297 the_end:
1298 printf("VM86 end\n");
1299 munmap(vm86_mem, 0x110000);
1300 }
1301
1302 /* exception tests */
1303 #ifndef REG_EAX
1304 #define REG_EAX EAX
1305 #define REG_EBX EBX
1306 #define REG_ECX ECX
1307 #define REG_EDX EDX
1308 #define REG_ESI ESI
1309 #define REG_EDI EDI
1310 #define REG_EBP EBP
1311 #define REG_ESP ESP
1312 #define REG_EIP EIP
1313 #define REG_EFL EFL
1314 #define REG_TRAPNO TRAPNO
1315 #define REG_ERR ERR
1316 #endif
1317
1318 jmp_buf jmp_env;
1319 int v1;
1320 int tab[2];
1321
1322 void sig_handler(int sig, siginfo_t *info, void *puc)
1323 {
1324 struct ucontext *uc = puc;
1325
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);
1330 printf("\n");
1331
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]);
1336 printf("\n");
1337 longjmp(jmp_env, 1);
1338 }
1339
1340 void test_exceptions(void)
1341 {
1342 struct modify_ldt_ldt_s ldt;
1343 struct sigaction act;
1344 volatile int val;
1345
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);
1354
1355 /* test division by zero reporting */
1356 printf("DIVZ exception:\n");
1357 if (setjmp(jmp_env) == 0) {
1358 /* now divide by zero */
1359 v1 = 0;
1360 v1 = 2 / v1;
1361 }
1362
1363 printf("BOUND exception:\n");
1364 if (setjmp(jmp_env) == 0) {
1365 /* bound exception */
1366 tab[0] = 1;
1367 tab[1] = 10;
1368 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab[0]));
1369 }
1370
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));
1375 }
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));
1381 }
1382
1383 ldt.entry_number = 1;
1384 ldt.base_addr = (unsigned long)&seg_data1;
1385 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1386 ldt.seg_32bit = 1;
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;
1391 ldt.useable = 1;
1392 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1393
1394 if (setjmp(jmp_env) == 0) {
1395 /* segment not present */
1396 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1397 }
1398
1399 /* test SEGV reporting */
1400 printf("PF exception:\n");
1401 if (setjmp(jmp_env) == 0) {
1402 val = 1;
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;
1407 }
1408
1409 /* test SEGV reporting */
1410 printf("PF exception:\n");
1411 if (setjmp(jmp_env) == 0) {
1412 val = 1;
1413 /* read from an invalid address */
1414 v1 = *(char *)0x1234;
1415 }
1416
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");
1422 }
1423 printf("lock nop exception:\n");
1424 if (setjmp(jmp_env) == 0) {
1425 /* now execute an invalid instruction */
1426 asm volatile("lock nop");
1427 }
1428
1429 printf("INT exception:\n");
1430 if (setjmp(jmp_env) == 0) {
1431 asm volatile ("int $0xfd");
1432 }
1433 if (setjmp(jmp_env) == 0) {
1434 asm volatile ("int $0x01");
1435 }
1436 if (setjmp(jmp_env) == 0) {
1437 asm volatile (".byte 0xcd, 0x03");
1438 }
1439 if (setjmp(jmp_env) == 0) {
1440 asm volatile ("int $0x04");
1441 }
1442 if (setjmp(jmp_env) == 0) {
1443 asm volatile ("int $0x05");
1444 }
1445
1446 printf("INT3 exception:\n");
1447 if (setjmp(jmp_env) == 0) {
1448 asm volatile ("int3");
1449 }
1450
1451 printf("CLI exception:\n");
1452 if (setjmp(jmp_env) == 0) {
1453 asm volatile ("cli");
1454 }
1455
1456 printf("STI exception:\n");
1457 if (setjmp(jmp_env) == 0) {
1458 asm volatile ("cli");
1459 }
1460
1461 printf("INTO exception:\n");
1462 if (setjmp(jmp_env) == 0) {
1463 /* overflow exception */
1464 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1465 }
1466
1467 printf("OUTB exception:\n");
1468 if (setjmp(jmp_env) == 0) {
1469 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1470 }
1471
1472 printf("INB exception:\n");
1473 if (setjmp(jmp_env) == 0) {
1474 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1475 }
1476
1477 printf("REP OUTSB exception:\n");
1478 if (setjmp(jmp_env) == 0) {
1479 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1480 }
1481
1482 printf("REP INSB exception:\n");
1483 if (setjmp(jmp_env) == 0) {
1484 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1485 }
1486
1487 printf("HLT exception:\n");
1488 if (setjmp(jmp_env) == 0) {
1489 asm volatile ("hlt");
1490 }
1491
1492 printf("single step exception:\n");
1493 val = 0;
1494 if (setjmp(jmp_env) == 0) {
1495 asm volatile ("pushf\n"
1496 "orl $0x00100, (%%esp)\n"
1497 "popf\n"
1498 "movl $0xabcd, %0\n"
1499 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1500 }
1501 printf("val=0x%x\n", val);
1502 }
1503
1504 /* specific precise single step test */
1505 void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1506 {
1507 struct ucontext *uc = puc;
1508 printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1509 }
1510
1511 const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1512 uint8_t sstep_buf2[4];
1513
1514 void test_single_step(void)
1515 {
1516 struct sigaction act;
1517 volatile int val;
1518 int i;
1519
1520 val = 0;
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"
1527 "popf\n"
1528 "movl $0xabcd, %0\n"
1529
1530 /* jmp test */
1531 "movl $3, %%ecx\n"
1532 "1:\n"
1533 "addl $1, %0\n"
1534 "decl %%ecx\n"
1535 "jnz 1b\n"
1536
1537 /* movsb: the single step should stop at each movsb iteration */
1538 "movl $sstep_buf1, %%esi\n"
1539 "movl $sstep_buf2, %%edi\n"
1540 "movl $0, %%ecx\n"
1541 "rep movsb\n"
1542 "movl $3, %%ecx\n"
1543 "rep movsb\n"
1544 "movl $1, %%ecx\n"
1545 "rep movsb\n"
1546
1547 /* cmpsb: the single step should stop at each cmpsb iteration */
1548 "movl $sstep_buf1, %%esi\n"
1549 "movl $sstep_buf2, %%edi\n"
1550 "movl $0, %%ecx\n"
1551 "rep cmpsb\n"
1552 "movl $4, %%ecx\n"
1553 "rep cmpsb\n"
1554
1555 /* getpid() syscall: single step should skip one
1556 instruction */
1557 "movl $20, %%eax\n"
1558 "int $0x80\n"
1559 "movl $0, %%eax\n"
1560
1561 /* when modifying SS, trace is not done on the next
1562 instruction */
1563 "movl %%ss, %%ecx\n"
1564 "movl %%ecx, %%ss\n"
1565 "addl $1, %0\n"
1566 "movl $1, %%eax\n"
1567 "movl %%ecx, %%ss\n"
1568 "jmp 1f\n"
1569 "addl $1, %0\n"
1570 "1:\n"
1571 "movl $1, %%eax\n"
1572 "pushl %%ecx\n"
1573 "popl %%ss\n"
1574 "addl $1, %0\n"
1575 "movl $1, %%eax\n"
1576
1577 "pushf\n"
1578 "andl $~0x00100, (%%esp)\n"
1579 "popf\n"
1580 : "=m" (val)
1581 :
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]);
1586 }
1587
1588 /* self modifying code test */
1589 uint8_t code[] = {
1590 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1591 0xc3, /* ret */
1592 };
1593
1594 asm("smc_code2:\n"
1595 "movl 4(%esp), %eax\n"
1596 "movl %eax, smc_patch_addr2 + 1\n"
1597 "nop\n"
1598 "nop\n"
1599 "nop\n"
1600 "nop\n"
1601 "nop\n"
1602 "nop\n"
1603 "nop\n"
1604 "nop\n"
1605 "smc_patch_addr2:\n"
1606 "movl $1, %eax\n"
1607 "ret\n");
1608
1609 typedef int FuncType(void);
1610 extern int smc_code2(int);
1611 void test_self_modifying_code(void)
1612 {
1613 int i;
1614
1615 printf("self modifying code:\n");
1616 printf("func1 = 0x%x\n", ((FuncType *)code)());
1617 for(i = 2; i <= 4; i++) {
1618 code[1] = i;
1619 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1620 }
1621
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));
1627 }
1628 }
1629
1630 int enter_stack[4096];
1631
1632 #define TEST_ENTER(size, stack_type, level)\
1633 {\
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;\
1639 for(i=1;i<=32;i++)\
1640 *--stack_ptr = i;\
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]);\
1662 }
1663
1664 static void test_enter(void)
1665 {
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);
1670
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);
1675 }
1676
1677 static void *call_end __init_call = NULL;
1678
1679 int main(int argc, char **argv)
1680 {
1681 void **ptr;
1682 void (*func)(void);
1683
1684 ptr = &call_start + 1;
1685 while (*ptr != NULL) {
1686 func = *ptr++;
1687 func();
1688 }
1689 test_bsx();
1690 test_mul();
1691 test_jcc();
1692 test_floats();
1693 test_bcd();
1694 test_xchg();
1695 test_string();
1696 test_misc();
1697 test_lea();
1698 test_segs();
1699 test_code16();
1700 #ifdef TEST_VM86
1701 test_vm86();
1702 #endif
1703 test_exceptions();
1704 test_self_modifying_code();
1705 test_single_step();
1706 test_enter();
1707 return 0;
1708 }