]> git.proxmox.com Git - qemu.git/blame - tests/test-i386.c
imull fix (suggested by Robert J. Harley)
[qemu.git] / tests / test-i386.c
CommitLineData
78d6da97
FB
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 */
3a27ad0b 20#define _GNU_SOURCE
4d1135e4
FB
21#include <stdlib.h>
22#include <stdio.h>
e3e86d56 23#include <string.h>
6dbad63e 24#include <inttypes.h>
4d1135e4 25#include <math.h>
3a27ad0b
FB
26#include <signal.h>
27#include <setjmp.h>
070893f4 28#include <errno.h>
3a27ad0b
FB
29#include <sys/ucontext.h>
30#include <sys/mman.h>
31#include <asm/vm86.h>
4d1135e4 32
03bfca94
FB
33#define TEST_CMOV 0
34#define TEST_FCOMI 0
3ff0631e 35//#define LINUX_VM86_IOPL_FIX
5dd9488c 36
4d1135e4
FB
37#define xglue(x, y) x ## y
38#define glue(x, y) xglue(x, y)
39#define stringify(s) tostring(s)
40#define tostring(s) #s
41
42#define CC_C 0x0001
43#define CC_P 0x0004
44#define CC_A 0x0010
45#define CC_Z 0x0040
46#define CC_S 0x0080
47#define CC_O 0x0800
48
4d1135e4
FB
49#define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
50
51static void *call_start __init_call = NULL;
52
4b74fe1f
FB
53#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
54
4d1135e4
FB
55#define OP add
56#include "test-i386.h"
57
58#define OP sub
59#include "test-i386.h"
60
61#define OP xor
62#include "test-i386.h"
63
64#define OP and
65#include "test-i386.h"
66
67#define OP or
68#include "test-i386.h"
69
70#define OP cmp
71#include "test-i386.h"
72
73#define OP adc
74#define OP_CC
75#include "test-i386.h"
76
77#define OP sbb
78#define OP_CC
79#include "test-i386.h"
80
81#define OP inc
82#define OP_CC
83#define OP1
84#include "test-i386.h"
85
86#define OP dec
87#define OP_CC
88#define OP1
89#include "test-i386.h"
90
91#define OP neg
92#define OP_CC
93#define OP1
94#include "test-i386.h"
95
96#define OP not
97#define OP_CC
98#define OP1
99#include "test-i386.h"
100
4b74fe1f
FB
101#undef CC_MASK
102#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
103
379ca80d
FB
104#define OP shl
105#include "test-i386-shift.h"
106
107#define OP shr
108#include "test-i386-shift.h"
109
110#define OP sar
111#include "test-i386-shift.h"
112
113#define OP rol
114#include "test-i386-shift.h"
115
116#define OP ror
117#include "test-i386-shift.h"
118
119#define OP rcr
120#define OP_CC
121#include "test-i386-shift.h"
122
123#define OP rcl
124#define OP_CC
125#include "test-i386-shift.h"
126
d57c4e01
FB
127#define OP shld
128#define OP_SHIFTD
129#define OP_NOBYTE
130#include "test-i386-shift.h"
131
132#define OP shrd
133#define OP_SHIFTD
134#define OP_NOBYTE
135#include "test-i386-shift.h"
136
137/* XXX: should be more precise ? */
138#undef CC_MASK
139#define CC_MASK (CC_C)
140
141#define OP bt
142#define OP_NOBYTE
143#include "test-i386-shift.h"
144
145#define OP bts
146#define OP_NOBYTE
147#include "test-i386-shift.h"
148
149#define OP btr
150#define OP_NOBYTE
151#include "test-i386-shift.h"
152
153#define OP btc
154#define OP_NOBYTE
155#include "test-i386-shift.h"
379ca80d 156
4d1135e4
FB
157/* lea test (modrm support) */
158#define TEST_LEA(STR)\
159{\
160 asm("leal " STR ", %0"\
161 : "=r" (res)\
162 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
163 printf("lea %s = %08x\n", STR, res);\
164}
165
166#define TEST_LEA16(STR)\
167{\
168 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
169 : "=wq" (res)\
170 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
171 printf("lea %s = %08x\n", STR, res);\
172}
173
174
175void test_lea(void)
176{
177 int eax, ebx, ecx, edx, esi, edi, res;
178 eax = 0x0001;
179 ebx = 0x0002;
180 ecx = 0x0004;
181 edx = 0x0008;
182 esi = 0x0010;
183 edi = 0x0020;
184
185 TEST_LEA("0x4000");
186
187 TEST_LEA("(%%eax)");
188 TEST_LEA("(%%ebx)");
189 TEST_LEA("(%%ecx)");
190 TEST_LEA("(%%edx)");
191 TEST_LEA("(%%esi)");
192 TEST_LEA("(%%edi)");
193
194 TEST_LEA("0x40(%%eax)");
195 TEST_LEA("0x40(%%ebx)");
196 TEST_LEA("0x40(%%ecx)");
197 TEST_LEA("0x40(%%edx)");
198 TEST_LEA("0x40(%%esi)");
199 TEST_LEA("0x40(%%edi)");
200
201 TEST_LEA("0x4000(%%eax)");
202 TEST_LEA("0x4000(%%ebx)");
203 TEST_LEA("0x4000(%%ecx)");
204 TEST_LEA("0x4000(%%edx)");
205 TEST_LEA("0x4000(%%esi)");
206 TEST_LEA("0x4000(%%edi)");
207
208 TEST_LEA("(%%eax, %%ecx)");
209 TEST_LEA("(%%ebx, %%edx)");
210 TEST_LEA("(%%ecx, %%ecx)");
211 TEST_LEA("(%%edx, %%ecx)");
212 TEST_LEA("(%%esi, %%ecx)");
213 TEST_LEA("(%%edi, %%ecx)");
214
215 TEST_LEA("0x40(%%eax, %%ecx)");
216 TEST_LEA("0x4000(%%ebx, %%edx)");
217
218 TEST_LEA("(%%ecx, %%ecx, 2)");
219 TEST_LEA("(%%edx, %%ecx, 4)");
220 TEST_LEA("(%%esi, %%ecx, 8)");
221
222 TEST_LEA("(,%%eax, 2)");
223 TEST_LEA("(,%%ebx, 4)");
224 TEST_LEA("(,%%ecx, 8)");
225
226 TEST_LEA("0x40(,%%eax, 2)");
227 TEST_LEA("0x40(,%%ebx, 4)");
228 TEST_LEA("0x40(,%%ecx, 8)");
229
230
231 TEST_LEA("-10(%%ecx, %%ecx, 2)");
232 TEST_LEA("-10(%%edx, %%ecx, 4)");
233 TEST_LEA("-10(%%esi, %%ecx, 8)");
234
235 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
236 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
237 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
238
239 /* limited 16 bit addressing test */
240 TEST_LEA16("0x4000");
241 TEST_LEA16("(%%bx)");
242 TEST_LEA16("(%%si)");
243 TEST_LEA16("(%%di)");
244 TEST_LEA16("0x40(%%bx)");
245 TEST_LEA16("0x40(%%si)");
246 TEST_LEA16("0x40(%%di)");
247 TEST_LEA16("0x4000(%%bx)");
248 TEST_LEA16("0x4000(%%si)");
249 TEST_LEA16("(%%bx,%%si)");
250 TEST_LEA16("(%%bx,%%di)");
251 TEST_LEA16("0x40(%%bx,%%si)");
252 TEST_LEA16("0x40(%%bx,%%di)");
253 TEST_LEA16("0x4000(%%bx,%%si)");
254 TEST_LEA16("0x4000(%%bx,%%di)");
255}
256
257#define TEST_JCC(JCC, v1, v2)\
258{\
5dd9488c 259 int res;\
4d1135e4
FB
260 asm("movl $1, %0\n\t"\
261 "cmpl %2, %1\n\t"\
5dd9488c 262 "j" JCC " 1f\n\t"\
4d1135e4
FB
263 "movl $0, %0\n\t"\
264 "1:\n\t"\
265 : "=r" (res)\
266 : "r" (v1), "r" (v2));\
5dd9488c
FB
267 printf("%-10s %d\n", "j" JCC, res);\
268\
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);\
275 if (TEST_CMOV) {\
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" (1));\
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" (1));\
287 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
288 } \
4d1135e4
FB
289}
290
291/* various jump tests */
292void test_jcc(void)
293{
5dd9488c
FB
294 TEST_JCC("ne", 1, 1);
295 TEST_JCC("ne", 1, 0);
4d1135e4 296
5dd9488c
FB
297 TEST_JCC("e", 1, 1);
298 TEST_JCC("e", 1, 0);
4d1135e4 299
5dd9488c
FB
300 TEST_JCC("l", 1, 1);
301 TEST_JCC("l", 1, 0);
302 TEST_JCC("l", 1, -1);
4d1135e4 303
5dd9488c
FB
304 TEST_JCC("le", 1, 1);
305 TEST_JCC("le", 1, 0);
306 TEST_JCC("le", 1, -1);
4d1135e4 307
5dd9488c
FB
308 TEST_JCC("ge", 1, 1);
309 TEST_JCC("ge", 1, 0);
310 TEST_JCC("ge", -1, 1);
4d1135e4 311
5dd9488c
FB
312 TEST_JCC("g", 1, 1);
313 TEST_JCC("g", 1, 0);
314 TEST_JCC("g", 1, -1);
4d1135e4 315
5dd9488c
FB
316 TEST_JCC("b", 1, 1);
317 TEST_JCC("b", 1, 0);
318 TEST_JCC("b", 1, -1);
4d1135e4 319
5dd9488c
FB
320 TEST_JCC("be", 1, 1);
321 TEST_JCC("be", 1, 0);
322 TEST_JCC("be", 1, -1);
4d1135e4 323
5dd9488c
FB
324 TEST_JCC("ae", 1, 1);
325 TEST_JCC("ae", 1, 0);
326 TEST_JCC("ae", 1, -1);
4d1135e4 327
5dd9488c
FB
328 TEST_JCC("a", 1, 1);
329 TEST_JCC("a", 1, 0);
330 TEST_JCC("a", 1, -1);
4d1135e4
FB
331
332
5dd9488c
FB
333 TEST_JCC("p", 1, 1);
334 TEST_JCC("p", 1, 0);
4d1135e4 335
5dd9488c
FB
336 TEST_JCC("np", 1, 1);
337 TEST_JCC("np", 1, 0);
4d1135e4 338
5dd9488c
FB
339 TEST_JCC("o", 0x7fffffff, 0);
340 TEST_JCC("o", 0x7fffffff, -1);
4d1135e4 341
5dd9488c
FB
342 TEST_JCC("no", 0x7fffffff, 0);
343 TEST_JCC("no", 0x7fffffff, -1);
4d1135e4 344
5dd9488c
FB
345 TEST_JCC("s", 0, 1);
346 TEST_JCC("s", 0, -1);
347 TEST_JCC("s", 0, 0);
4d1135e4 348
5dd9488c
FB
349 TEST_JCC("ns", 0, 1);
350 TEST_JCC("ns", 0, -1);
351 TEST_JCC("ns", 0, 0);
4d1135e4
FB
352}
353
4b74fe1f
FB
354#undef CC_MASK
355#define CC_MASK (CC_O | CC_C)
356
357#define OP mul
358#include "test-i386-muldiv.h"
359
360#define OP imul
361#include "test-i386-muldiv.h"
362
363#undef CC_MASK
364#define CC_MASK (0)
365
366#define OP div
367#include "test-i386-muldiv.h"
368
369#define OP idiv
370#include "test-i386-muldiv.h"
371
372void test_imulw2(int op0, int op1)
373{
374 int res, s1, s0, flags;
375 s0 = op0;
376 s1 = op1;
377 res = s0;
378 flags = 0;
379 asm ("push %4\n\t"
380 "popf\n\t"
381 "imulw %w2, %w0\n\t"
382 "pushf\n\t"
383 "popl %1\n\t"
384 : "=q" (res), "=g" (flags)
385 : "q" (s1), "0" (res), "1" (flags));
386 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
387 "imulw", s0, s1, res, flags & CC_MASK);
388}
389
390void test_imull2(int op0, int op1)
391{
392 int res, s1, s0, flags;
393 s0 = op0;
394 s1 = op1;
395 res = s0;
396 flags = 0;
397 asm ("push %4\n\t"
398 "popf\n\t"
399 "imull %2, %0\n\t"
400 "pushf\n\t"
401 "popl %1\n\t"
402 : "=q" (res), "=g" (flags)
403 : "q" (s1), "0" (res), "1" (flags));
404 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
405 "imull", s0, s1, res, flags & CC_MASK);
406}
407
408void test_mul(void)
409{
410 test_imulb(0x1234561d, 4);
411 test_imulb(3, -4);
412 test_imulb(0x80, 0x80);
413 test_imulb(0x10, 0x10);
414
415 test_imulw(0, 0x1234001d, 45);
416 test_imulw(0, 23, -45);
417 test_imulw(0, 0x8000, 0x8000);
418 test_imulw(0, 0x100, 0x100);
419
420 test_imull(0, 0x1234001d, 45);
421 test_imull(0, 23, -45);
422 test_imull(0, 0x80000000, 0x80000000);
423 test_imull(0, 0x10000, 0x10000);
424
425 test_mulb(0x1234561d, 4);
426 test_mulb(3, -4);
427 test_mulb(0x80, 0x80);
428 test_mulb(0x10, 0x10);
429
430 test_mulw(0, 0x1234001d, 45);
431 test_mulw(0, 23, -45);
432 test_mulw(0, 0x8000, 0x8000);
433 test_mulw(0, 0x100, 0x100);
434
435 test_mull(0, 0x1234001d, 45);
436 test_mull(0, 23, -45);
437 test_mull(0, 0x80000000, 0x80000000);
438 test_mull(0, 0x10000, 0x10000);
439
440 test_imulw2(0x1234001d, 45);
441 test_imulw2(23, -45);
442 test_imulw2(0x8000, 0x8000);
443 test_imulw2(0x100, 0x100);
444
445 test_imull2(0x1234001d, 45);
446 test_imull2(23, -45);
447 test_imull2(0x80000000, 0x80000000);
448 test_imull2(0x10000, 0x10000);
449
450 test_idivb(0x12341678, 0x127e);
451 test_idivb(0x43210123, -5);
452 test_idivb(0x12340004, -1);
453
454 test_idivw(0, 0x12345678, 12347);
455 test_idivw(0, -23223, -45);
456 test_idivw(0, 0x12348000, -1);
457 test_idivw(0x12343, 0x12345678, 0x81238567);
458
459 test_idivl(0, 0x12345678, 12347);
460 test_idivl(0, -233223, -45);
461 test_idivl(0, 0x80000000, -1);
462 test_idivl(0x12343, 0x12345678, 0x81234567);
463
464 test_divb(0x12341678, 0x127e);
465 test_divb(0x43210123, -5);
466 test_divb(0x12340004, -1);
467
468 test_divw(0, 0x12345678, 12347);
469 test_divw(0, -23223, -45);
470 test_divw(0, 0x12348000, -1);
471 test_divw(0x12343, 0x12345678, 0x81238567);
472
473 test_divl(0, 0x12345678, 12347);
474 test_divl(0, -233223, -45);
475 test_divl(0, 0x80000000, -1);
476 test_divl(0x12343, 0x12345678, 0x81234567);
477}
478
9d8e9c09
FB
479#define TEST_BSX(op, size, op0)\
480{\
481 int res, val, resz;\
482 val = op0;\
483 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
484 : "=r" (res), "=q" (resz)\
485 : "g" (val));\
486 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
487}
488
489void test_bsx(void)
490{
491 TEST_BSX(bsrw, "w", 0);
492 TEST_BSX(bsrw, "w", 0x12340128);
493 TEST_BSX(bsrl, "", 0);
494 TEST_BSX(bsrl, "", 0x00340128);
495 TEST_BSX(bsfw, "w", 0);
496 TEST_BSX(bsfw, "w", 0x12340128);
497 TEST_BSX(bsfl, "", 0);
498 TEST_BSX(bsfl, "", 0x00340128);
499}
500
55480af8
FB
501/**********************************************/
502
9d8e9c09
FB
503void test_fops(double a, double b)
504{
505 printf("a=%f b=%f a+b=%f\n", a, b, a + b);
506 printf("a=%f b=%f a-b=%f\n", a, b, a - b);
507 printf("a=%f b=%f a*b=%f\n", a, b, a * b);
508 printf("a=%f b=%f a/b=%f\n", a, b, a / b);
509 printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
510 printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
511 printf("a=%f sin(a)=%f\n", a, sin(a));
512 printf("a=%f cos(a)=%f\n", a, cos(a));
513 printf("a=%f tan(a)=%f\n", a, tan(a));
514 printf("a=%f log(a)=%f\n", a, log(a));
515 printf("a=%f exp(a)=%f\n", a, exp(a));
516 printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
517 /* just to test some op combining */
518 printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
519 printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
520 printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
521
522}
523
524void test_fcmp(double a, double b)
525{
526 printf("(%f<%f)=%d\n",
527 a, b, a < b);
528 printf("(%f<=%f)=%d\n",
529 a, b, a <= b);
530 printf("(%f==%f)=%d\n",
531 a, b, a == b);
532 printf("(%f>%f)=%d\n",
533 a, b, a > b);
534 printf("(%f<=%f)=%d\n",
535 a, b, a >= b);
03bfca94
FB
536 if (TEST_FCOMI) {
537 unsigned int eflags;
538 /* test f(u)comi instruction */
539 asm("fcomi %2, %1\n"
540 "pushf\n"
541 "pop %0\n"
542 : "=r" (eflags)
543 : "t" (a), "u" (b));
544 printf("fcomi(%f %f)=%08x\n", a, b, eflags & (CC_Z | CC_P | CC_C));
545 }
9d8e9c09
FB
546}
547
548void test_fcvt(double a)
549{
550 float fa;
551 long double la;
ea768640
FB
552 int16_t fpuc;
553 int i;
554 int64_t lla;
555 int ia;
556 int16_t wa;
557 double ra;
9d8e9c09
FB
558
559 fa = a;
560 la = a;
561 printf("(float)%f = %f\n", a, fa);
562 printf("(long double)%f = %Lf\n", a, la);
c5e9815d
FB
563 printf("a=%016Lx\n", *(long long *)&a);
564 printf("la=%016Lx %04x\n", *(long long *)&la,
565 *(unsigned short *)((char *)(&la) + 8));
ea768640
FB
566
567 /* test all roundings */
568 asm volatile ("fstcw %0" : "=m" (fpuc));
569 for(i=0;i<4;i++) {
570 asm volatile ("fldcw %0" : : "m" ((fpuc & ~0x0c00) | (i << 10)));
571 asm volatile ("fist %0" : "=m" (wa) : "t" (a));
572 asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
573 asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
574 asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
575 asm volatile ("fldcw %0" : : "m" (fpuc));
576 printf("(short)a = %d\n", wa);
577 printf("(int)a = %d\n", ia);
578 printf("(int64_t)a = %Ld\n", lla);
579 printf("rint(a) = %f\n", ra);
580 }
9d8e9c09
FB
581}
582
583#define TEST(N) \
584 asm("fld" #N : "=t" (a)); \
585 printf("fld" #N "= %f\n", a);
586
587void test_fconst(void)
588{
589 double a;
590 TEST(1);
591 TEST(l2t);
592 TEST(l2e);
593 TEST(pi);
594 TEST(lg2);
595 TEST(ln2);
596 TEST(z);
597}
598
c5e9815d
FB
599void test_fbcd(double a)
600{
601 unsigned short bcd[5];
602 double b;
603
604 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
605 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
606 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
607 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
608}
609
03bfca94
FB
610#define TEST_ENV(env, prefix)\
611{\
612 memset((env), 0xaa, sizeof(*(env)));\
613 asm("fld1\n"\
614 prefix "fnstenv %1\n"\
615 prefix "fldenv %1\n"\
e3e86d56 616 : "=t" (res) : "m" (*(env)));\
03bfca94
FB
617 printf("res=%f\n", res);\
618 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
619 (env)->fpuc,\
620 (env)->fpus & 0xff00,\
621 (env)->fptag);\
622 memset((env), 0xaa, sizeof(*(env)));\
623 asm("fld1\n"\
624 prefix "fnsave %1\n"\
625 prefix "frstor %1\n"\
e3e86d56 626 : "=t" (res) : "m" (*(env)));\
03bfca94
FB
627 printf("res=%f\n", res);\
628 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
629 (env)->fpuc,\
630 (env)->fpus & 0xff00,\
631 (env)->fptag);\
632 printf("ST(0) = %Lf\n",\
633 (env)->fpregs[0]);\
634}
635
636void test_fenv(void)
637{
638 struct __attribute__((packed)) {
639 uint16_t fpuc;
640 uint16_t dummy1;
641 uint16_t fpus;
642 uint16_t dummy2;
643 uint16_t fptag;
644 uint16_t dummy3;
645 uint32_t ignored[4];
646 long double fpregs[8];
647 } float_env32;
648 struct __attribute__((packed)) {
649 uint16_t fpuc;
650 uint16_t fpus;
651 uint16_t fptag;
652 uint16_t ignored[4];
653 long double fpregs[8];
654 } float_env16;
655 double res;
656
657 TEST_ENV(&float_env16, "data16 ");
658 TEST_ENV(&float_env32, "");
659}
660
75175024
FB
661
662#define TEST_FCMOV(a, b, eflags, CC)\
663{\
664 double res;\
665 asm("push %3\n"\
666 "popf\n"\
667 "fcmov" CC " %2, %0\n"\
668 : "=t" (res)\
669 : "0" (a), "u" (b), "g" (eflags));\
670 printf("fcmov%s eflags=0x%04x-> %f\n", \
671 CC, eflags, res);\
672}
673
674void test_fcmov(void)
675{
676 double a, b;
677 int eflags, i;
678
679 a = 1.0;
680 b = 2.0;
681 for(i = 0; i < 4; i++) {
682 eflags = 0;
683 if (i & 1)
684 eflags |= CC_C;
685 if (i & 2)
686 eflags |= CC_Z;
687 TEST_FCMOV(a, b, eflags, "b");
688 TEST_FCMOV(a, b, eflags, "e");
689 TEST_FCMOV(a, b, eflags, "be");
690 TEST_FCMOV(a, b, eflags, "nb");
691 TEST_FCMOV(a, b, eflags, "ne");
692 TEST_FCMOV(a, b, eflags, "nbe");
693 }
694 TEST_FCMOV(a, b, 0, "u");
9cdf757f
FB
695 TEST_FCMOV(a, b, CC_P, "u");
696 TEST_FCMOV(a, b, 0, "nu");
75175024
FB
697 TEST_FCMOV(a, b, CC_P, "nu");
698}
699
9d8e9c09
FB
700void test_floats(void)
701{
702 test_fops(2, 3);
703 test_fops(1.4, -5);
704 test_fcmp(2, -1);
705 test_fcmp(2, 2);
706 test_fcmp(2, 3);
ea768640
FB
707 test_fcvt(0.5);
708 test_fcvt(-0.5);
9d8e9c09
FB
709 test_fcvt(1.0/7.0);
710 test_fcvt(-1.0/9.0);
ea768640
FB
711 test_fcvt(32768);
712 test_fcvt(-1e20);
9d8e9c09 713 test_fconst();
c5e9815d
FB
714 test_fbcd(1234567890123456);
715 test_fbcd(-123451234567890);
03bfca94 716 test_fenv();
75175024
FB
717 if (TEST_CMOV) {
718 test_fcmov();
719 }
9d8e9c09 720}
4b74fe1f 721
55480af8
FB
722/**********************************************/
723
724#define TEST_BCD(op, op0, cc_in, cc_mask)\
725{\
726 int res, flags;\
727 res = op0;\
728 flags = cc_in;\
729 asm ("push %3\n\t"\
730 "popf\n\t"\
731 #op "\n\t"\
732 "pushf\n\t"\
733 "popl %1\n\t"\
734 : "=a" (res), "=g" (flags)\
735 : "0" (res), "1" (flags));\
736 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
737 #op, op0, res, cc_in, flags & cc_mask);\
738}
739
740void test_bcd(void)
741{
742 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
743 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
744 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
745 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
746 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
747 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
748 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
749 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
750 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
751 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
752 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
753 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
754 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
755
756 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
757 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
758 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
759 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
760 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
761 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
762 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
763 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
764 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
765 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
766 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
767 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
768 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
769
770 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
771 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
772 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
773 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
774 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
775 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
776 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
777 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
778
779 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
780 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
781 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
782 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
783 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
784 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
785 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
786 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
787
788 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
789 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
790}
791
e5918247
FB
792#define TEST_XCHG(op, size, opconst)\
793{\
794 int op0, op1;\
795 op0 = 0x12345678;\
796 op1 = 0xfbca7654;\
797 asm(#op " %" size "0, %" size "1" \
798 : "=q" (op0), opconst (op1) \
799 : "0" (op0), "1" (op1));\
800 printf("%-10s A=%08x B=%08x\n",\
801 #op, op0, op1);\
802}
803
804#define TEST_CMPXCHG(op, size, opconst, eax)\
805{\
806 int op0, op1;\
807 op0 = 0x12345678;\
808 op1 = 0xfbca7654;\
809 asm(#op " %" size "0, %" size "1" \
810 : "=q" (op0), opconst (op1) \
811 : "0" (op0), "1" (op1), "a" (eax));\
812 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
813 #op, eax, op0, op1);\
814}
815
816void test_xchg(void)
817{
818 TEST_XCHG(xchgl, "", "=q");
819 TEST_XCHG(xchgw, "w", "=q");
820 TEST_XCHG(xchgb, "b", "=q");
821
822 TEST_XCHG(xchgl, "", "=m");
823 TEST_XCHG(xchgw, "w", "=m");
824 TEST_XCHG(xchgb, "b", "=m");
825
826 TEST_XCHG(xaddl, "", "=q");
827 TEST_XCHG(xaddw, "w", "=q");
828 TEST_XCHG(xaddb, "b", "=q");
829
830 TEST_XCHG(xaddl, "", "=m");
831 TEST_XCHG(xaddw, "w", "=m");
832 TEST_XCHG(xaddb, "b", "=m");
833
834 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
835 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
836 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
837
838 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
839 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
840 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
841
842 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
843 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
844 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
845
846 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
847 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
848 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
849}
850
6dbad63e
FB
851/**********************************************/
852/* segmentation tests */
853
854#include <asm/ldt.h>
855#include <linux/unistd.h>
856
857_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
858
859uint8_t seg_data1[4096];
860uint8_t seg_data2[4096];
861
e5918247 862#define MK_SEL(n) (((n) << 3) | 7)
6dbad63e 863
288426fe
FB
864#define TEST_LR(op, size, seg, mask)\
865{\
866 int res, res2;\
867 res = 0x12345678;\
868 asm (op " %" size "2, %" size "0\n" \
869 "movl $0, %1\n"\
870 "jnz 1f\n"\
871 "movl $1, %1\n"\
872 "1:\n"\
873 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
874 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
875}
876
6dbad63e
FB
877/* NOTE: we use Linux modify_ldt syscall */
878void test_segs(void)
879{
880 struct modify_ldt_ldt_s ldt;
881 long long ldt_table[3];
04369ff2 882 int res, res2;
6dbad63e 883 char tmp;
e1d4294a
FB
884 struct {
885 uint32_t offset;
886 uint16_t seg;
887 } __attribute__((packed)) segoff;
6dbad63e
FB
888
889 ldt.entry_number = 1;
890 ldt.base_addr = (unsigned long)&seg_data1;
891 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
892 ldt.seg_32bit = 1;
893 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
894 ldt.read_exec_only = 0;
895 ldt.limit_in_pages = 1;
896 ldt.seg_not_present = 0;
897 ldt.useable = 1;
898 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
899
900 ldt.entry_number = 2;
901 ldt.base_addr = (unsigned long)&seg_data2;
902 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
903 ldt.seg_32bit = 1;
904 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
905 ldt.read_exec_only = 0;
906 ldt.limit_in_pages = 1;
907 ldt.seg_not_present = 0;
908 ldt.useable = 1;
909 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
910
911 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
04369ff2
FB
912#if 0
913 {
914 int i;
915 for(i=0;i<3;i++)
916 printf("%d: %016Lx\n", i, ldt_table[i]);
917 }
918#endif
6dbad63e
FB
919 /* do some tests with fs or gs */
920 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
6dbad63e
FB
921
922 seg_data1[1] = 0xaa;
923 seg_data2[1] = 0x55;
924
925 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
926 printf("FS[1] = %02x\n", res);
927
070893f4
FB
928 asm volatile ("pushl %%gs\n"
929 "movl %1, %%gs\n"
930 "gs movzbl 0x1, %0\n"
931 "popl %%gs\n"
932 : "=r" (res)
933 : "r" (MK_SEL(2)));
6dbad63e
FB
934 printf("GS[1] = %02x\n", res);
935
936 /* tests with ds/ss (implicit segment case) */
937 tmp = 0xa5;
938 asm volatile ("pushl %%ebp\n\t"
939 "pushl %%ds\n\t"
940 "movl %2, %%ds\n\t"
941 "movl %3, %%ebp\n\t"
942 "movzbl 0x1, %0\n\t"
943 "movzbl (%%ebp), %1\n\t"
944 "popl %%ds\n\t"
945 "popl %%ebp\n\t"
946 : "=r" (res), "=r" (res2)
947 : "r" (MK_SEL(1)), "r" (&tmp));
948 printf("DS[1] = %02x\n", res);
949 printf("SS[tmp] = %02x\n", res2);
e1d4294a
FB
950
951 segoff.seg = MK_SEL(2);
952 segoff.offset = 0xabcdef12;
953 asm volatile("lfs %2, %0\n\t"
954 "movl %%fs, %1\n\t"
955 : "=r" (res), "=g" (res2)
956 : "m" (segoff));
957 printf("FS:reg = %04x:%08x\n", res2, res);
288426fe
FB
958
959 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
960 TEST_LR("larl", "", MK_SEL(2), 0x0100);
961 TEST_LR("lslw", "w", MK_SEL(2), 0);
962 TEST_LR("lsll", "", MK_SEL(2), 0);
963
964 TEST_LR("larw", "w", 0xfff8, 0);
965 TEST_LR("larl", "", 0xfff8, 0);
966 TEST_LR("lslw", "w", 0xfff8, 0);
967 TEST_LR("lsll", "", 0xfff8, 0);
6dbad63e 968}
55480af8 969
e5918247
FB
970/* 16 bit code test */
971extern char code16_start, code16_end;
972extern char code16_func1;
973extern char code16_func2;
974extern char code16_func3;
a300e691 975
e5918247 976void test_code16(void)
1a9353d2 977{
e5918247
FB
978 struct modify_ldt_ldt_s ldt;
979 int res, res2;
a300e691 980
e5918247
FB
981 /* build a code segment */
982 ldt.entry_number = 1;
983 ldt.base_addr = (unsigned long)&code16_start;
984 ldt.limit = &code16_end - &code16_start;
985 ldt.seg_32bit = 0;
986 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
987 ldt.read_exec_only = 0;
988 ldt.limit_in_pages = 0;
989 ldt.seg_not_present = 0;
990 ldt.useable = 1;
991 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
a300e691 992
e5918247
FB
993 /* call the first function */
994 asm volatile ("lcall %1, %2"
995 : "=a" (res)
996 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
997 printf("func1() = 0x%08x\n", res);
998 asm volatile ("lcall %2, %3"
999 : "=a" (res), "=c" (res2)
1000 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
1001 printf("func2() = 0x%08x spdec=%d\n", res, res2);
1002 asm volatile ("lcall %1, %2"
1003 : "=a" (res)
1004 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1005 printf("func3() = 0x%08x\n", res);
1a9353d2
FB
1006}
1007
dd3587f3
FB
1008extern char func_lret32;
1009extern char func_iret32;
1010
e1d4294a
FB
1011void test_misc(void)
1012{
1013 char table[256];
1014 int res, i;
1015
1016 for(i=0;i<256;i++) table[i] = 256 - i;
1017 res = 0x12345678;
1018 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
1019 printf("xlat: EAX=%08x\n", res);
dd3587f3
FB
1020
1021 asm volatile ("pushl %%cs ; call %1"
1022 : "=a" (res)
1023 : "m" (func_lret32): "memory", "cc");
1024 printf("func_lret32=%x\n", res);
1025
1026 asm volatile ("pushfl ; pushl %%cs ; call %1"
1027 : "=a" (res)
1028 : "m" (func_iret32): "memory", "cc");
1029 printf("func_iret32=%x\n", res);
1030
1031 /* specific popl test */
1032 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1033 : "=g" (res));
1034 printf("popl esp=%x\n", res);
b2b5fb22
FB
1035
1036 /* specific popw test */
1037 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1038 : "=g" (res));
1039 printf("popw esp=%x\n", res);
e1d4294a
FB
1040}
1041
1042uint8_t str_buffer[4096];
1043
1044#define TEST_STRING1(OP, size, DF, REP)\
1045{\
1046 int esi, edi, eax, ecx, eflags;\
1047\
1048 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1049 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1050 eax = 0x12345678;\
1051 ecx = 17;\
1052\
1053 asm volatile ("pushl $0\n\t"\
1054 "popf\n\t"\
1055 DF "\n\t"\
1056 REP #OP size "\n\t"\
1057 "cld\n\t"\
1058 "pushf\n\t"\
1059 "popl %4\n\t"\
1060 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1061 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1062 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1063 REP #OP size, esi, edi, eax, ecx,\
1064 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1065}
1066
1067#define TEST_STRING(OP, REP)\
1068 TEST_STRING1(OP, "b", "", REP);\
1069 TEST_STRING1(OP, "w", "", REP);\
1070 TEST_STRING1(OP, "l", "", REP);\
1071 TEST_STRING1(OP, "b", "std", REP);\
1072 TEST_STRING1(OP, "w", "std", REP);\
1073 TEST_STRING1(OP, "l", "std", REP)
1074
1075void test_string(void)
1076{
1077 int i;
1078 for(i = 0;i < sizeof(str_buffer); i++)
1079 str_buffer[i] = i + 0x56;
1080 TEST_STRING(stos, "");
1081 TEST_STRING(stos, "rep ");
1082 TEST_STRING(lods, ""); /* to verify stos */
1083 TEST_STRING(lods, "rep ");
1084 TEST_STRING(movs, "");
1085 TEST_STRING(movs, "rep ");
1086 TEST_STRING(lods, ""); /* to verify stos */
1087
1088 /* XXX: better tests */
1089 TEST_STRING(scas, "");
1090 TEST_STRING(scas, "repz ");
1091 TEST_STRING(scas, "repnz ");
1092 TEST_STRING(cmps, "");
1093 TEST_STRING(cmps, "repz ");
1094 TEST_STRING(cmps, "repnz ");
1095}
e5918247 1096
3a27ad0b
FB
1097/* VM86 test */
1098
1099static inline void set_bit(uint8_t *a, unsigned int bit)
1100{
1101 a[bit / 8] |= (1 << (bit % 8));
1102}
1103
1104static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1105{
1106 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1107}
1108
1109static inline void pushw(struct vm86_regs *r, int val)
1110{
1111 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1112 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1113}
1114
1115#undef __syscall_return
1116#define __syscall_return(type, res) \
1117do { \
1118 return (type) (res); \
1119} while (0)
1120
1121_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1122
1123extern char vm86_code_start;
1124extern char vm86_code_end;
1125
1126#define VM86_CODE_CS 0x100
1127#define VM86_CODE_IP 0x100
1128
1129void test_vm86(void)
1130{
1131 struct vm86plus_struct ctx;
1132 struct vm86_regs *r;
1133 uint8_t *vm86_mem;
1134 int seg, ret;
1135
1136 vm86_mem = mmap((void *)0x00000000, 0x110000,
1137 PROT_WRITE | PROT_READ | PROT_EXEC,
1138 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1139 if (vm86_mem == MAP_FAILED) {
1140 printf("ERROR: could not map vm86 memory");
1141 return;
1142 }
1143 memset(&ctx, 0, sizeof(ctx));
1144
1145 /* init basic registers */
1146 r = &ctx.regs;
1147 r->eip = VM86_CODE_IP;
1148 r->esp = 0xfffe;
1149 seg = VM86_CODE_CS;
1150 r->cs = seg;
1151 r->ss = seg;
1152 r->ds = seg;
1153 r->es = seg;
1154 r->fs = seg;
1155 r->gs = seg;
1156 r->eflags = VIF_MASK;
1157
1158 /* move code to proper address. We use the same layout as a .com
1159 dos program. */
1160 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1161 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1162
1163 /* mark int 0x21 as being emulated */
1164 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1165
1166 for(;;) {
1167 ret = vm86(VM86_ENTER, &ctx);
1168 switch(VM86_TYPE(ret)) {
1169 case VM86_INTx:
1170 {
3ff0631e 1171 int int_num, ah, v;
3a27ad0b
FB
1172
1173 int_num = VM86_ARG(ret);
1174 if (int_num != 0x21)
1175 goto unknown_int;
1176 ah = (r->eax >> 8) & 0xff;
1177 switch(ah) {
1178 case 0x00: /* exit */
1179 goto the_end;
1180 case 0x02: /* write char */
1181 {
1182 uint8_t c = r->edx;
1183 putchar(c);
1184 }
1185 break;
1186 case 0x09: /* write string */
1187 {
1188 uint8_t c, *ptr;
1189 ptr = seg_to_linear(r->ds, r->edx);
1190 for(;;) {
1191 c = *ptr++;
1192 if (c == '$')
1193 break;
1194 putchar(c);
1195 }
1196 r->eax = (r->eax & ~0xff) | '$';
1197 }
1198 break;
3ff0631e
FB
1199 case 0xff: /* extension: write eflags number in edx */
1200 v = (int)r->edx;
1201#ifndef LINUX_VM86_IOPL_FIX
1202 v &= ~0x3000;
1203#endif
1204 printf("%08x\n", v);
3a27ad0b
FB
1205 break;
1206 default:
1207 unknown_int:
1208 printf("unsupported int 0x%02x\n", int_num);
1209 goto the_end;
1210 }
1211 }
1212 break;
1213 case VM86_SIGNAL:
1214 /* a signal came, we just ignore that */
1215 break;
1216 case VM86_STI:
1217 break;
1218 default:
1219 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1220 goto the_end;
1221 }
1222 }
1223 the_end:
1224 printf("VM86 end\n");
1225 munmap(vm86_mem, 0x110000);
1226}
1227
1228/* exception tests */
1229#ifndef REG_EAX
1230#define REG_EAX EAX
1231#define REG_EBX EBX
1232#define REG_ECX ECX
1233#define REG_EDX EDX
1234#define REG_ESI ESI
1235#define REG_EDI EDI
1236#define REG_EBP EBP
1237#define REG_ESP ESP
1238#define REG_EIP EIP
1239#define REG_EFL EFL
1240#define REG_TRAPNO TRAPNO
1241#define REG_ERR ERR
1242#endif
1243
1244jmp_buf jmp_env;
3a27ad0b
FB
1245int v1;
1246int tab[2];
1247
1248void sig_handler(int sig, siginfo_t *info, void *puc)
1249{
1250 struct ucontext *uc = puc;
1251
1252 printf("si_signo=%d si_errno=%d si_code=%d",
1253 info->si_signo, info->si_errno, info->si_code);
e3b32540
FB
1254 printf(" si_addr=0x%08lx",
1255 (unsigned long)info->si_addr);
3a27ad0b
FB
1256 printf("\n");
1257
1258 printf("trapno=0x%02x err=0x%08x",
1259 uc->uc_mcontext.gregs[REG_TRAPNO],
1260 uc->uc_mcontext.gregs[REG_ERR]);
e3b32540 1261 printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
3a27ad0b
FB
1262 printf("\n");
1263 longjmp(jmp_env, 1);
1264}
1265
1266void test_exceptions(void)
1267{
e3b32540 1268 struct modify_ldt_ldt_s ldt;
3a27ad0b
FB
1269 struct sigaction act;
1270 volatile int val;
1271
1272 act.sa_sigaction = sig_handler;
1273 sigemptyset(&act.sa_mask);
1274 act.sa_flags = SA_SIGINFO;
1275 sigaction(SIGFPE, &act, NULL);
1276 sigaction(SIGILL, &act, NULL);
1277 sigaction(SIGSEGV, &act, NULL);
e3b32540 1278 sigaction(SIGBUS, &act, NULL);
3a27ad0b
FB
1279 sigaction(SIGTRAP, &act, NULL);
1280
1281 /* test division by zero reporting */
e3b32540 1282 printf("DIVZ exception:\n");
3a27ad0b
FB
1283 if (setjmp(jmp_env) == 0) {
1284 /* now divide by zero */
1285 v1 = 0;
1286 v1 = 2 / v1;
1287 }
1288
e3b32540 1289 printf("BOUND exception:\n");
3a27ad0b
FB
1290 if (setjmp(jmp_env) == 0) {
1291 /* bound exception */
1292 tab[0] = 1;
1293 tab[1] = 10;
1294 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1295 }
1296
e3b32540
FB
1297 printf("segment exceptions:\n");
1298 if (setjmp(jmp_env) == 0) {
1299 /* load an invalid segment */
1300 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1301 }
1302 if (setjmp(jmp_env) == 0) {
1303 /* null data segment is valid */
1304 asm volatile ("movl %0, %%fs" : : "r" (3));
1305 /* null stack segment */
1306 asm volatile ("movl %0, %%ss" : : "r" (3));
1307 }
1308
1309 ldt.entry_number = 1;
1310 ldt.base_addr = (unsigned long)&seg_data1;
1311 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1312 ldt.seg_32bit = 1;
1313 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1314 ldt.read_exec_only = 0;
1315 ldt.limit_in_pages = 1;
1316 ldt.seg_not_present = 1;
1317 ldt.useable = 1;
1318 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1319
1320 if (setjmp(jmp_env) == 0) {
1321 /* segment not present */
1322 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1323 }
1324
3a27ad0b 1325 /* test SEGV reporting */
e3b32540 1326 printf("PF exception:\n");
3a27ad0b 1327 if (setjmp(jmp_env) == 0) {
e3b32540 1328 val = 1;
ede28208
FB
1329 /* we add a nop to test a weird PC retrieval case */
1330 asm volatile ("nop");
3a27ad0b
FB
1331 /* now store in an invalid address */
1332 *(char *)0x1234 = 1;
1333 }
1334
1335 /* test SEGV reporting */
e3b32540 1336 printf("PF exception:\n");
3a27ad0b 1337 if (setjmp(jmp_env) == 0) {
e3b32540 1338 val = 1;
3a27ad0b
FB
1339 /* read from an invalid address */
1340 v1 = *(char *)0x1234;
1341 }
1342
3a27ad0b
FB
1343 /* test illegal instruction reporting */
1344 printf("UD2 exception:\n");
1345 if (setjmp(jmp_env) == 0) {
1346 /* now execute an invalid instruction */
1347 asm volatile("ud2");
1348 }
1349
1350 printf("INT exception:\n");
1351 if (setjmp(jmp_env) == 0) {
1352 asm volatile ("int $0xfd");
1353 }
e3b32540
FB
1354 if (setjmp(jmp_env) == 0) {
1355 asm volatile ("int $0x01");
1356 }
1357 if (setjmp(jmp_env) == 0) {
1358 asm volatile (".byte 0xcd, 0x03");
1359 }
1360 if (setjmp(jmp_env) == 0) {
1361 asm volatile ("int $0x04");
1362 }
1363 if (setjmp(jmp_env) == 0) {
1364 asm volatile ("int $0x05");
1365 }
3a27ad0b
FB
1366
1367 printf("INT3 exception:\n");
1368 if (setjmp(jmp_env) == 0) {
1369 asm volatile ("int3");
1370 }
1371
1372 printf("CLI exception:\n");
1373 if (setjmp(jmp_env) == 0) {
1374 asm volatile ("cli");
1375 }
1376
1377 printf("STI exception:\n");
1378 if (setjmp(jmp_env) == 0) {
1379 asm volatile ("cli");
1380 }
1381
1382 printf("INTO exception:\n");
1383 if (setjmp(jmp_env) == 0) {
1384 /* overflow exception */
1385 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1386 }
1387
1388 printf("OUTB exception:\n");
1389 if (setjmp(jmp_env) == 0) {
1390 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1391 }
1392
1393 printf("INB exception:\n");
1394 if (setjmp(jmp_env) == 0) {
1395 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1396 }
1397
1398 printf("REP OUTSB exception:\n");
1399 if (setjmp(jmp_env) == 0) {
1400 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1401 }
1402
1403 printf("REP INSB exception:\n");
1404 if (setjmp(jmp_env) == 0) {
1405 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1406 }
1407
1408 printf("HLT exception:\n");
1409 if (setjmp(jmp_env) == 0) {
1410 asm volatile ("hlt");
1411 }
1412
1413 printf("single step exception:\n");
1414 val = 0;
1415 if (setjmp(jmp_env) == 0) {
1416 asm volatile ("pushf\n"
1417 "orl $0x00100, (%%esp)\n"
1418 "popf\n"
1419 "movl $0xabcd, %0\n"
1420 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1421 }
1422 printf("val=0x%x\n", val);
1423}
1424
3ff0631e
FB
1425/* specific precise single step test */
1426void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1427{
1428 struct ucontext *uc = puc;
1429 printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1430}
1431
1432const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1433uint8_t sstep_buf2[4];
1434
1435void test_single_step(void)
1436{
1437 struct sigaction act;
1438 volatile int val;
1439 int i;
1440
1441 val = 0;
1442 act.sa_sigaction = sig_trap_handler;
1443 sigemptyset(&act.sa_mask);
1444 act.sa_flags = SA_SIGINFO;
1445 sigaction(SIGTRAP, &act, NULL);
1446 asm volatile ("pushf\n"
1447 "orl $0x00100, (%%esp)\n"
1448 "popf\n"
1449 "movl $0xabcd, %0\n"
1450
1451 /* jmp test */
1452 "movl $3, %%ecx\n"
1453 "1:\n"
1454 "addl $1, %0\n"
1455 "decl %%ecx\n"
1456 "jnz 1b\n"
1457
1458 /* movsb: the single step should stop at each movsb iteration */
1459 "movl $sstep_buf1, %%esi\n"
1460 "movl $sstep_buf2, %%edi\n"
1461 "movl $0, %%ecx\n"
1462 "rep movsb\n"
1463 "movl $3, %%ecx\n"
1464 "rep movsb\n"
1465 "movl $1, %%ecx\n"
1466 "rep movsb\n"
1467
1468 /* cmpsb: the single step should stop at each cmpsb iteration */
1469 "movl $sstep_buf1, %%esi\n"
1470 "movl $sstep_buf2, %%edi\n"
1471 "movl $0, %%ecx\n"
1472 "rep cmpsb\n"
1473 "movl $4, %%ecx\n"
1474 "rep cmpsb\n"
1475
1476 /* getpid() syscall: single step should skip one
1477 instruction */
1478 "movl $20, %%eax\n"
1479 "int $0x80\n"
1480 "movl $0, %%eax\n"
1481
1482 /* when modifying SS, trace is not done on the next
1483 instruction */
1484 "movl %%ss, %%ecx\n"
1485 "movl %%ecx, %%ss\n"
1486 "addl $1, %0\n"
1487 "movl $1, %%eax\n"
1488 "movl %%ecx, %%ss\n"
1489 "jmp 1f\n"
1490 "addl $1, %0\n"
1491 "1:\n"
1492 "movl $1, %%eax\n"
1493 "pushl %%ecx\n"
1494 "popl %%ss\n"
1495 "addl $1, %0\n"
1496 "movl $1, %%eax\n"
1497
1498 "pushf\n"
1499 "andl $~0x00100, (%%esp)\n"
1500 "popf\n"
1501 : "=m" (val)
1502 :
1503 : "cc", "memory", "eax", "ecx", "esi", "edi");
1504 printf("val=%d\n", val);
1505 for(i = 0; i < 4; i++)
1506 printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1507}
1508
3a27ad0b
FB
1509/* self modifying code test */
1510uint8_t code[] = {
1511 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1512 0xc3, /* ret */
1513};
1514
d1fe2b24
FB
1515typedef int FuncType(void);
1516
3a27ad0b
FB
1517void test_self_modifying_code(void)
1518{
d1fe2b24 1519 int i;
3a27ad0b 1520
3a27ad0b 1521 printf("self modifying code:\n");
d1fe2b24
FB
1522 printf("func1 = 0x%x\n", ((FuncType *)code)());
1523 for(i = 2; i <= 4; i++) {
1524 code[1] = i;
1525 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1526 }
3a27ad0b
FB
1527}
1528
4d1135e4
FB
1529static void *call_end __init_call = NULL;
1530
1531int main(int argc, char **argv)
1532{
1533 void **ptr;
1534 void (*func)(void);
4b74fe1f 1535
4d1135e4
FB
1536 ptr = &call_start + 1;
1537 while (*ptr != NULL) {
1538 func = *ptr++;
1539 func();
1540 }
9d8e9c09 1541 test_bsx();
d57c4e01 1542 test_mul();
4d1135e4 1543 test_jcc();
9d8e9c09 1544 test_floats();
55480af8 1545 test_bcd();
1a9353d2 1546 test_xchg();
e1d4294a
FB
1547 test_string();
1548 test_misc();
6dbad63e
FB
1549 test_lea();
1550 test_segs();
e5918247 1551 test_code16();
3a27ad0b
FB
1552 test_vm86();
1553 test_exceptions();
1554 test_self_modifying_code();
3ff0631e 1555 test_single_step();
4d1135e4
FB
1556 return 0;
1557}