]> git.proxmox.com Git - qemu.git/blame - tests/test-i386.c
fixed TB linking in case of code invalidation (fixes random segfaults)
[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");
695 TEST_FCMOV(a, b, CC_P, "nu");
696}
697
9d8e9c09
FB
698void test_floats(void)
699{
700 test_fops(2, 3);
701 test_fops(1.4, -5);
702 test_fcmp(2, -1);
703 test_fcmp(2, 2);
704 test_fcmp(2, 3);
ea768640
FB
705 test_fcvt(0.5);
706 test_fcvt(-0.5);
9d8e9c09
FB
707 test_fcvt(1.0/7.0);
708 test_fcvt(-1.0/9.0);
ea768640
FB
709 test_fcvt(32768);
710 test_fcvt(-1e20);
9d8e9c09 711 test_fconst();
c5e9815d
FB
712 test_fbcd(1234567890123456);
713 test_fbcd(-123451234567890);
03bfca94 714 test_fenv();
75175024
FB
715 if (TEST_CMOV) {
716 test_fcmov();
717 }
9d8e9c09 718}
4b74fe1f 719
55480af8
FB
720/**********************************************/
721
722#define TEST_BCD(op, op0, cc_in, cc_mask)\
723{\
724 int res, flags;\
725 res = op0;\
726 flags = cc_in;\
727 asm ("push %3\n\t"\
728 "popf\n\t"\
729 #op "\n\t"\
730 "pushf\n\t"\
731 "popl %1\n\t"\
732 : "=a" (res), "=g" (flags)\
733 : "0" (res), "1" (flags));\
734 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
735 #op, op0, res, cc_in, flags & cc_mask);\
736}
737
738void test_bcd(void)
739{
740 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
741 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
742 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
743 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
744 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
745 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
746 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
747 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
748 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
749 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
750 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
751 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
752 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
753
754 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
755 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
756 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
757 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
758 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
759 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
760 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
761 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
762 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
763 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
764 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
765 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
766 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
767
768 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
769 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
770 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
771 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
772 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
773 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
774 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
775 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
776
777 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
778 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
779 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
780 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
781 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
782 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
783 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
784 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
785
786 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
787 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
788}
789
e5918247
FB
790#define TEST_XCHG(op, size, opconst)\
791{\
792 int op0, op1;\
793 op0 = 0x12345678;\
794 op1 = 0xfbca7654;\
795 asm(#op " %" size "0, %" size "1" \
796 : "=q" (op0), opconst (op1) \
797 : "0" (op0), "1" (op1));\
798 printf("%-10s A=%08x B=%08x\n",\
799 #op, op0, op1);\
800}
801
802#define TEST_CMPXCHG(op, size, opconst, eax)\
803{\
804 int op0, op1;\
805 op0 = 0x12345678;\
806 op1 = 0xfbca7654;\
807 asm(#op " %" size "0, %" size "1" \
808 : "=q" (op0), opconst (op1) \
809 : "0" (op0), "1" (op1), "a" (eax));\
810 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
811 #op, eax, op0, op1);\
812}
813
814void test_xchg(void)
815{
816 TEST_XCHG(xchgl, "", "=q");
817 TEST_XCHG(xchgw, "w", "=q");
818 TEST_XCHG(xchgb, "b", "=q");
819
820 TEST_XCHG(xchgl, "", "=m");
821 TEST_XCHG(xchgw, "w", "=m");
822 TEST_XCHG(xchgb, "b", "=m");
823
824 TEST_XCHG(xaddl, "", "=q");
825 TEST_XCHG(xaddw, "w", "=q");
826 TEST_XCHG(xaddb, "b", "=q");
827
828 TEST_XCHG(xaddl, "", "=m");
829 TEST_XCHG(xaddw, "w", "=m");
830 TEST_XCHG(xaddb, "b", "=m");
831
832 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
833 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
834 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
835
836 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
837 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
838 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
839
840 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
841 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
842 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
843
844 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
845 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
846 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
847}
848
6dbad63e
FB
849/**********************************************/
850/* segmentation tests */
851
852#include <asm/ldt.h>
853#include <linux/unistd.h>
854
855_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
856
857uint8_t seg_data1[4096];
858uint8_t seg_data2[4096];
859
e5918247 860#define MK_SEL(n) (((n) << 3) | 7)
6dbad63e 861
288426fe
FB
862#define TEST_LR(op, size, seg, mask)\
863{\
864 int res, res2;\
865 res = 0x12345678;\
866 asm (op " %" size "2, %" size "0\n" \
867 "movl $0, %1\n"\
868 "jnz 1f\n"\
869 "movl $1, %1\n"\
870 "1:\n"\
871 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
872 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
873}
874
6dbad63e
FB
875/* NOTE: we use Linux modify_ldt syscall */
876void test_segs(void)
877{
878 struct modify_ldt_ldt_s ldt;
879 long long ldt_table[3];
04369ff2 880 int res, res2;
6dbad63e 881 char tmp;
e1d4294a
FB
882 struct {
883 uint32_t offset;
884 uint16_t seg;
885 } __attribute__((packed)) segoff;
6dbad63e
FB
886
887 ldt.entry_number = 1;
888 ldt.base_addr = (unsigned long)&seg_data1;
889 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
890 ldt.seg_32bit = 1;
891 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
892 ldt.read_exec_only = 0;
893 ldt.limit_in_pages = 1;
894 ldt.seg_not_present = 0;
895 ldt.useable = 1;
896 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
897
898 ldt.entry_number = 2;
899 ldt.base_addr = (unsigned long)&seg_data2;
900 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
901 ldt.seg_32bit = 1;
902 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
903 ldt.read_exec_only = 0;
904 ldt.limit_in_pages = 1;
905 ldt.seg_not_present = 0;
906 ldt.useable = 1;
907 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
908
909 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
04369ff2
FB
910#if 0
911 {
912 int i;
913 for(i=0;i<3;i++)
914 printf("%d: %016Lx\n", i, ldt_table[i]);
915 }
916#endif
6dbad63e
FB
917 /* do some tests with fs or gs */
918 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
6dbad63e
FB
919
920 seg_data1[1] = 0xaa;
921 seg_data2[1] = 0x55;
922
923 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
924 printf("FS[1] = %02x\n", res);
925
070893f4
FB
926 asm volatile ("pushl %%gs\n"
927 "movl %1, %%gs\n"
928 "gs movzbl 0x1, %0\n"
929 "popl %%gs\n"
930 : "=r" (res)
931 : "r" (MK_SEL(2)));
6dbad63e
FB
932 printf("GS[1] = %02x\n", res);
933
934 /* tests with ds/ss (implicit segment case) */
935 tmp = 0xa5;
936 asm volatile ("pushl %%ebp\n\t"
937 "pushl %%ds\n\t"
938 "movl %2, %%ds\n\t"
939 "movl %3, %%ebp\n\t"
940 "movzbl 0x1, %0\n\t"
941 "movzbl (%%ebp), %1\n\t"
942 "popl %%ds\n\t"
943 "popl %%ebp\n\t"
944 : "=r" (res), "=r" (res2)
945 : "r" (MK_SEL(1)), "r" (&tmp));
946 printf("DS[1] = %02x\n", res);
947 printf("SS[tmp] = %02x\n", res2);
e1d4294a
FB
948
949 segoff.seg = MK_SEL(2);
950 segoff.offset = 0xabcdef12;
951 asm volatile("lfs %2, %0\n\t"
952 "movl %%fs, %1\n\t"
953 : "=r" (res), "=g" (res2)
954 : "m" (segoff));
955 printf("FS:reg = %04x:%08x\n", res2, res);
288426fe
FB
956
957 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
958 TEST_LR("larl", "", MK_SEL(2), 0x0100);
959 TEST_LR("lslw", "w", MK_SEL(2), 0);
960 TEST_LR("lsll", "", MK_SEL(2), 0);
961
962 TEST_LR("larw", "w", 0xfff8, 0);
963 TEST_LR("larl", "", 0xfff8, 0);
964 TEST_LR("lslw", "w", 0xfff8, 0);
965 TEST_LR("lsll", "", 0xfff8, 0);
6dbad63e 966}
55480af8 967
e5918247
FB
968/* 16 bit code test */
969extern char code16_start, code16_end;
970extern char code16_func1;
971extern char code16_func2;
972extern char code16_func3;
a300e691 973
e5918247 974void test_code16(void)
1a9353d2 975{
e5918247
FB
976 struct modify_ldt_ldt_s ldt;
977 int res, res2;
a300e691 978
e5918247
FB
979 /* build a code segment */
980 ldt.entry_number = 1;
981 ldt.base_addr = (unsigned long)&code16_start;
982 ldt.limit = &code16_end - &code16_start;
983 ldt.seg_32bit = 0;
984 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
985 ldt.read_exec_only = 0;
986 ldt.limit_in_pages = 0;
987 ldt.seg_not_present = 0;
988 ldt.useable = 1;
989 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
a300e691 990
e5918247
FB
991 /* call the first function */
992 asm volatile ("lcall %1, %2"
993 : "=a" (res)
994 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
995 printf("func1() = 0x%08x\n", res);
996 asm volatile ("lcall %2, %3"
997 : "=a" (res), "=c" (res2)
998 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
999 printf("func2() = 0x%08x spdec=%d\n", res, res2);
1000 asm volatile ("lcall %1, %2"
1001 : "=a" (res)
1002 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1003 printf("func3() = 0x%08x\n", res);
1a9353d2
FB
1004}
1005
dd3587f3
FB
1006extern char func_lret32;
1007extern char func_iret32;
1008
e1d4294a
FB
1009void test_misc(void)
1010{
1011 char table[256];
1012 int res, i;
1013
1014 for(i=0;i<256;i++) table[i] = 256 - i;
1015 res = 0x12345678;
1016 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
1017 printf("xlat: EAX=%08x\n", res);
dd3587f3
FB
1018
1019 asm volatile ("pushl %%cs ; call %1"
1020 : "=a" (res)
1021 : "m" (func_lret32): "memory", "cc");
1022 printf("func_lret32=%x\n", res);
1023
1024 asm volatile ("pushfl ; pushl %%cs ; call %1"
1025 : "=a" (res)
1026 : "m" (func_iret32): "memory", "cc");
1027 printf("func_iret32=%x\n", res);
1028
1029 /* specific popl test */
1030 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1031 : "=g" (res));
1032 printf("popl esp=%x\n", res);
b2b5fb22
FB
1033
1034 /* specific popw test */
1035 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1036 : "=g" (res));
1037 printf("popw esp=%x\n", res);
e1d4294a
FB
1038}
1039
1040uint8_t str_buffer[4096];
1041
1042#define TEST_STRING1(OP, size, DF, REP)\
1043{\
1044 int esi, edi, eax, ecx, eflags;\
1045\
1046 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1047 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1048 eax = 0x12345678;\
1049 ecx = 17;\
1050\
1051 asm volatile ("pushl $0\n\t"\
1052 "popf\n\t"\
1053 DF "\n\t"\
1054 REP #OP size "\n\t"\
1055 "cld\n\t"\
1056 "pushf\n\t"\
1057 "popl %4\n\t"\
1058 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1059 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1060 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1061 REP #OP size, esi, edi, eax, ecx,\
1062 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1063}
1064
1065#define TEST_STRING(OP, REP)\
1066 TEST_STRING1(OP, "b", "", REP);\
1067 TEST_STRING1(OP, "w", "", REP);\
1068 TEST_STRING1(OP, "l", "", REP);\
1069 TEST_STRING1(OP, "b", "std", REP);\
1070 TEST_STRING1(OP, "w", "std", REP);\
1071 TEST_STRING1(OP, "l", "std", REP)
1072
1073void test_string(void)
1074{
1075 int i;
1076 for(i = 0;i < sizeof(str_buffer); i++)
1077 str_buffer[i] = i + 0x56;
1078 TEST_STRING(stos, "");
1079 TEST_STRING(stos, "rep ");
1080 TEST_STRING(lods, ""); /* to verify stos */
1081 TEST_STRING(lods, "rep ");
1082 TEST_STRING(movs, "");
1083 TEST_STRING(movs, "rep ");
1084 TEST_STRING(lods, ""); /* to verify stos */
1085
1086 /* XXX: better tests */
1087 TEST_STRING(scas, "");
1088 TEST_STRING(scas, "repz ");
1089 TEST_STRING(scas, "repnz ");
1090 TEST_STRING(cmps, "");
1091 TEST_STRING(cmps, "repz ");
1092 TEST_STRING(cmps, "repnz ");
1093}
e5918247 1094
3a27ad0b
FB
1095/* VM86 test */
1096
1097static inline void set_bit(uint8_t *a, unsigned int bit)
1098{
1099 a[bit / 8] |= (1 << (bit % 8));
1100}
1101
1102static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1103{
1104 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1105}
1106
1107static inline void pushw(struct vm86_regs *r, int val)
1108{
1109 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1110 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1111}
1112
1113#undef __syscall_return
1114#define __syscall_return(type, res) \
1115do { \
1116 return (type) (res); \
1117} while (0)
1118
1119_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1120
1121extern char vm86_code_start;
1122extern char vm86_code_end;
1123
1124#define VM86_CODE_CS 0x100
1125#define VM86_CODE_IP 0x100
1126
1127void test_vm86(void)
1128{
1129 struct vm86plus_struct ctx;
1130 struct vm86_regs *r;
1131 uint8_t *vm86_mem;
1132 int seg, ret;
1133
1134 vm86_mem = mmap((void *)0x00000000, 0x110000,
1135 PROT_WRITE | PROT_READ | PROT_EXEC,
1136 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1137 if (vm86_mem == MAP_FAILED) {
1138 printf("ERROR: could not map vm86 memory");
1139 return;
1140 }
1141 memset(&ctx, 0, sizeof(ctx));
1142
1143 /* init basic registers */
1144 r = &ctx.regs;
1145 r->eip = VM86_CODE_IP;
1146 r->esp = 0xfffe;
1147 seg = VM86_CODE_CS;
1148 r->cs = seg;
1149 r->ss = seg;
1150 r->ds = seg;
1151 r->es = seg;
1152 r->fs = seg;
1153 r->gs = seg;
1154 r->eflags = VIF_MASK;
1155
1156 /* move code to proper address. We use the same layout as a .com
1157 dos program. */
1158 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1159 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1160
1161 /* mark int 0x21 as being emulated */
1162 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1163
1164 for(;;) {
1165 ret = vm86(VM86_ENTER, &ctx);
1166 switch(VM86_TYPE(ret)) {
1167 case VM86_INTx:
1168 {
3ff0631e 1169 int int_num, ah, v;
3a27ad0b
FB
1170
1171 int_num = VM86_ARG(ret);
1172 if (int_num != 0x21)
1173 goto unknown_int;
1174 ah = (r->eax >> 8) & 0xff;
1175 switch(ah) {
1176 case 0x00: /* exit */
1177 goto the_end;
1178 case 0x02: /* write char */
1179 {
1180 uint8_t c = r->edx;
1181 putchar(c);
1182 }
1183 break;
1184 case 0x09: /* write string */
1185 {
1186 uint8_t c, *ptr;
1187 ptr = seg_to_linear(r->ds, r->edx);
1188 for(;;) {
1189 c = *ptr++;
1190 if (c == '$')
1191 break;
1192 putchar(c);
1193 }
1194 r->eax = (r->eax & ~0xff) | '$';
1195 }
1196 break;
3ff0631e
FB
1197 case 0xff: /* extension: write eflags number in edx */
1198 v = (int)r->edx;
1199#ifndef LINUX_VM86_IOPL_FIX
1200 v &= ~0x3000;
1201#endif
1202 printf("%08x\n", v);
3a27ad0b
FB
1203 break;
1204 default:
1205 unknown_int:
1206 printf("unsupported int 0x%02x\n", int_num);
1207 goto the_end;
1208 }
1209 }
1210 break;
1211 case VM86_SIGNAL:
1212 /* a signal came, we just ignore that */
1213 break;
1214 case VM86_STI:
1215 break;
1216 default:
1217 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1218 goto the_end;
1219 }
1220 }
1221 the_end:
1222 printf("VM86 end\n");
1223 munmap(vm86_mem, 0x110000);
1224}
1225
1226/* exception tests */
1227#ifndef REG_EAX
1228#define REG_EAX EAX
1229#define REG_EBX EBX
1230#define REG_ECX ECX
1231#define REG_EDX EDX
1232#define REG_ESI ESI
1233#define REG_EDI EDI
1234#define REG_EBP EBP
1235#define REG_ESP ESP
1236#define REG_EIP EIP
1237#define REG_EFL EFL
1238#define REG_TRAPNO TRAPNO
1239#define REG_ERR ERR
1240#endif
1241
1242jmp_buf jmp_env;
3a27ad0b
FB
1243int v1;
1244int tab[2];
1245
1246void sig_handler(int sig, siginfo_t *info, void *puc)
1247{
1248 struct ucontext *uc = puc;
1249
1250 printf("si_signo=%d si_errno=%d si_code=%d",
1251 info->si_signo, info->si_errno, info->si_code);
e3b32540
FB
1252 printf(" si_addr=0x%08lx",
1253 (unsigned long)info->si_addr);
3a27ad0b
FB
1254 printf("\n");
1255
1256 printf("trapno=0x%02x err=0x%08x",
1257 uc->uc_mcontext.gregs[REG_TRAPNO],
1258 uc->uc_mcontext.gregs[REG_ERR]);
e3b32540 1259 printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
3a27ad0b
FB
1260 printf("\n");
1261 longjmp(jmp_env, 1);
1262}
1263
1264void test_exceptions(void)
1265{
e3b32540 1266 struct modify_ldt_ldt_s ldt;
3a27ad0b
FB
1267 struct sigaction act;
1268 volatile int val;
1269
1270 act.sa_sigaction = sig_handler;
1271 sigemptyset(&act.sa_mask);
1272 act.sa_flags = SA_SIGINFO;
1273 sigaction(SIGFPE, &act, NULL);
1274 sigaction(SIGILL, &act, NULL);
1275 sigaction(SIGSEGV, &act, NULL);
e3b32540 1276 sigaction(SIGBUS, &act, NULL);
3a27ad0b
FB
1277 sigaction(SIGTRAP, &act, NULL);
1278
1279 /* test division by zero reporting */
e3b32540 1280 printf("DIVZ exception:\n");
3a27ad0b
FB
1281 if (setjmp(jmp_env) == 0) {
1282 /* now divide by zero */
1283 v1 = 0;
1284 v1 = 2 / v1;
1285 }
1286
e3b32540 1287 printf("BOUND exception:\n");
3a27ad0b
FB
1288 if (setjmp(jmp_env) == 0) {
1289 /* bound exception */
1290 tab[0] = 1;
1291 tab[1] = 10;
1292 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1293 }
1294
e3b32540
FB
1295 printf("segment exceptions:\n");
1296 if (setjmp(jmp_env) == 0) {
1297 /* load an invalid segment */
1298 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1299 }
1300 if (setjmp(jmp_env) == 0) {
1301 /* null data segment is valid */
1302 asm volatile ("movl %0, %%fs" : : "r" (3));
1303 /* null stack segment */
1304 asm volatile ("movl %0, %%ss" : : "r" (3));
1305 }
1306
1307 ldt.entry_number = 1;
1308 ldt.base_addr = (unsigned long)&seg_data1;
1309 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1310 ldt.seg_32bit = 1;
1311 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1312 ldt.read_exec_only = 0;
1313 ldt.limit_in_pages = 1;
1314 ldt.seg_not_present = 1;
1315 ldt.useable = 1;
1316 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1317
1318 if (setjmp(jmp_env) == 0) {
1319 /* segment not present */
1320 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1321 }
1322
3a27ad0b 1323 /* test SEGV reporting */
e3b32540 1324 printf("PF exception:\n");
3a27ad0b 1325 if (setjmp(jmp_env) == 0) {
e3b32540 1326 val = 1;
ede28208
FB
1327 /* we add a nop to test a weird PC retrieval case */
1328 asm volatile ("nop");
3a27ad0b
FB
1329 /* now store in an invalid address */
1330 *(char *)0x1234 = 1;
1331 }
1332
1333 /* test SEGV reporting */
e3b32540 1334 printf("PF exception:\n");
3a27ad0b 1335 if (setjmp(jmp_env) == 0) {
e3b32540 1336 val = 1;
3a27ad0b
FB
1337 /* read from an invalid address */
1338 v1 = *(char *)0x1234;
1339 }
1340
3a27ad0b
FB
1341 /* test illegal instruction reporting */
1342 printf("UD2 exception:\n");
1343 if (setjmp(jmp_env) == 0) {
1344 /* now execute an invalid instruction */
1345 asm volatile("ud2");
1346 }
1347
1348 printf("INT exception:\n");
1349 if (setjmp(jmp_env) == 0) {
1350 asm volatile ("int $0xfd");
1351 }
e3b32540
FB
1352 if (setjmp(jmp_env) == 0) {
1353 asm volatile ("int $0x01");
1354 }
1355 if (setjmp(jmp_env) == 0) {
1356 asm volatile (".byte 0xcd, 0x03");
1357 }
1358 if (setjmp(jmp_env) == 0) {
1359 asm volatile ("int $0x04");
1360 }
1361 if (setjmp(jmp_env) == 0) {
1362 asm volatile ("int $0x05");
1363 }
3a27ad0b
FB
1364
1365 printf("INT3 exception:\n");
1366 if (setjmp(jmp_env) == 0) {
1367 asm volatile ("int3");
1368 }
1369
1370 printf("CLI exception:\n");
1371 if (setjmp(jmp_env) == 0) {
1372 asm volatile ("cli");
1373 }
1374
1375 printf("STI exception:\n");
1376 if (setjmp(jmp_env) == 0) {
1377 asm volatile ("cli");
1378 }
1379
1380 printf("INTO exception:\n");
1381 if (setjmp(jmp_env) == 0) {
1382 /* overflow exception */
1383 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1384 }
1385
1386 printf("OUTB exception:\n");
1387 if (setjmp(jmp_env) == 0) {
1388 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1389 }
1390
1391 printf("INB exception:\n");
1392 if (setjmp(jmp_env) == 0) {
1393 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1394 }
1395
1396 printf("REP OUTSB exception:\n");
1397 if (setjmp(jmp_env) == 0) {
1398 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1399 }
1400
1401 printf("REP INSB exception:\n");
1402 if (setjmp(jmp_env) == 0) {
1403 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1404 }
1405
1406 printf("HLT exception:\n");
1407 if (setjmp(jmp_env) == 0) {
1408 asm volatile ("hlt");
1409 }
1410
1411 printf("single step exception:\n");
1412 val = 0;
1413 if (setjmp(jmp_env) == 0) {
1414 asm volatile ("pushf\n"
1415 "orl $0x00100, (%%esp)\n"
1416 "popf\n"
1417 "movl $0xabcd, %0\n"
1418 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1419 }
1420 printf("val=0x%x\n", val);
1421}
1422
3ff0631e
FB
1423/* specific precise single step test */
1424void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1425{
1426 struct ucontext *uc = puc;
1427 printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1428}
1429
1430const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1431uint8_t sstep_buf2[4];
1432
1433void test_single_step(void)
1434{
1435 struct sigaction act;
1436 volatile int val;
1437 int i;
1438
1439 val = 0;
1440 act.sa_sigaction = sig_trap_handler;
1441 sigemptyset(&act.sa_mask);
1442 act.sa_flags = SA_SIGINFO;
1443 sigaction(SIGTRAP, &act, NULL);
1444 asm volatile ("pushf\n"
1445 "orl $0x00100, (%%esp)\n"
1446 "popf\n"
1447 "movl $0xabcd, %0\n"
1448
1449 /* jmp test */
1450 "movl $3, %%ecx\n"
1451 "1:\n"
1452 "addl $1, %0\n"
1453 "decl %%ecx\n"
1454 "jnz 1b\n"
1455
1456 /* movsb: the single step should stop at each movsb iteration */
1457 "movl $sstep_buf1, %%esi\n"
1458 "movl $sstep_buf2, %%edi\n"
1459 "movl $0, %%ecx\n"
1460 "rep movsb\n"
1461 "movl $3, %%ecx\n"
1462 "rep movsb\n"
1463 "movl $1, %%ecx\n"
1464 "rep movsb\n"
1465
1466 /* cmpsb: the single step should stop at each cmpsb iteration */
1467 "movl $sstep_buf1, %%esi\n"
1468 "movl $sstep_buf2, %%edi\n"
1469 "movl $0, %%ecx\n"
1470 "rep cmpsb\n"
1471 "movl $4, %%ecx\n"
1472 "rep cmpsb\n"
1473
1474 /* getpid() syscall: single step should skip one
1475 instruction */
1476 "movl $20, %%eax\n"
1477 "int $0x80\n"
1478 "movl $0, %%eax\n"
1479
1480 /* when modifying SS, trace is not done on the next
1481 instruction */
1482 "movl %%ss, %%ecx\n"
1483 "movl %%ecx, %%ss\n"
1484 "addl $1, %0\n"
1485 "movl $1, %%eax\n"
1486 "movl %%ecx, %%ss\n"
1487 "jmp 1f\n"
1488 "addl $1, %0\n"
1489 "1:\n"
1490 "movl $1, %%eax\n"
1491 "pushl %%ecx\n"
1492 "popl %%ss\n"
1493 "addl $1, %0\n"
1494 "movl $1, %%eax\n"
1495
1496 "pushf\n"
1497 "andl $~0x00100, (%%esp)\n"
1498 "popf\n"
1499 : "=m" (val)
1500 :
1501 : "cc", "memory", "eax", "ecx", "esi", "edi");
1502 printf("val=%d\n", val);
1503 for(i = 0; i < 4; i++)
1504 printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1505}
1506
3a27ad0b
FB
1507/* self modifying code test */
1508uint8_t code[] = {
1509 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1510 0xc3, /* ret */
1511};
1512
d1fe2b24
FB
1513typedef int FuncType(void);
1514
3a27ad0b
FB
1515void test_self_modifying_code(void)
1516{
d1fe2b24 1517 int i;
3a27ad0b 1518
3a27ad0b 1519 printf("self modifying code:\n");
d1fe2b24
FB
1520 printf("func1 = 0x%x\n", ((FuncType *)code)());
1521 for(i = 2; i <= 4; i++) {
1522 code[1] = i;
1523 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1524 }
3a27ad0b
FB
1525}
1526
4d1135e4
FB
1527static void *call_end __init_call = NULL;
1528
1529int main(int argc, char **argv)
1530{
1531 void **ptr;
1532 void (*func)(void);
4b74fe1f 1533
4d1135e4
FB
1534 ptr = &call_start + 1;
1535 while (*ptr != NULL) {
1536 func = *ptr++;
1537 func();
1538 }
9d8e9c09 1539 test_bsx();
d57c4e01 1540 test_mul();
4d1135e4 1541 test_jcc();
9d8e9c09 1542 test_floats();
55480af8 1543 test_bcd();
1a9353d2 1544 test_xchg();
e1d4294a
FB
1545 test_string();
1546 test_misc();
6dbad63e
FB
1547 test_lea();
1548 test_segs();
e5918247 1549 test_code16();
3a27ad0b
FB
1550 test_vm86();
1551 test_exceptions();
1552 test_self_modifying_code();
3ff0631e 1553 test_single_step();
4d1135e4
FB
1554 return 0;
1555}