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