Statistics
| Branch: | Revision:

root / tests / test-i386.c @ c321f673

History | View | Annotate | Download (39.5 kB)

1
/*
2
 *  x86 CPU test
3
 * 
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
 */
20
#define _GNU_SOURCE
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <inttypes.h>
25
#include <math.h>
26
#include <signal.h>
27
#include <setjmp.h>
28
#include <errno.h>
29
#include <sys/ucontext.h>
30
#include <sys/mman.h>
31
#include <asm/vm86.h>
32

    
33
#define TEST_CMOV  0
34
#define TEST_FCOMI 0
35
//#define LINUX_VM86_IOPL_FIX
36

    
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

    
49
#define __init_call        __attribute__ ((unused,__section__ (".initcall.init")))
50

    
51
static void *call_start __init_call = NULL;
52

    
53
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
54

    
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

    
101
#undef CC_MASK
102
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
103

    
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

    
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"
156

    
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

    
175
void 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
{\
259
    int res;\
260
    asm("movl $1, %0\n\t"\
261
        "cmpl %2, %1\n\t"\
262
        "j" JCC " 1f\n\t"\
263
        "movl $0, %0\n\t"\
264
        "1:\n\t"\
265
        : "=r" (res)\
266
        : "r" (v1), "r" (v2));\
267
    printf("%-10s %d\n", "j" JCC, res);\
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
 } \
289
}
290

    
291
/* various jump tests */
292
void test_jcc(void)
293
{
294
    TEST_JCC("ne", 1, 1);
295
    TEST_JCC("ne", 1, 0);
296

    
297
    TEST_JCC("e", 1, 1);
298
    TEST_JCC("e", 1, 0);
299

    
300
    TEST_JCC("l", 1, 1);
301
    TEST_JCC("l", 1, 0);
302
    TEST_JCC("l", 1, -1);
303

    
304
    TEST_JCC("le", 1, 1);
305
    TEST_JCC("le", 1, 0);
306
    TEST_JCC("le", 1, -1);
307

    
308
    TEST_JCC("ge", 1, 1);
309
    TEST_JCC("ge", 1, 0);
310
    TEST_JCC("ge", -1, 1);
311

    
312
    TEST_JCC("g", 1, 1);
313
    TEST_JCC("g", 1, 0);
314
    TEST_JCC("g", 1, -1);
315

    
316
    TEST_JCC("b", 1, 1);
317
    TEST_JCC("b", 1, 0);
318
    TEST_JCC("b", 1, -1);
319

    
320
    TEST_JCC("be", 1, 1);
321
    TEST_JCC("be", 1, 0);
322
    TEST_JCC("be", 1, -1);
323

    
324
    TEST_JCC("ae", 1, 1);
325
    TEST_JCC("ae", 1, 0);
326
    TEST_JCC("ae", 1, -1);
327

    
328
    TEST_JCC("a", 1, 1);
329
    TEST_JCC("a", 1, 0);
330
    TEST_JCC("a", 1, -1);
331

    
332

    
333
    TEST_JCC("p", 1, 1);
334
    TEST_JCC("p", 1, 0);
335

    
336
    TEST_JCC("np", 1, 1);
337
    TEST_JCC("np", 1, 0);
338

    
339
    TEST_JCC("o", 0x7fffffff, 0);
340
    TEST_JCC("o", 0x7fffffff, -1);
341

    
342
    TEST_JCC("no", 0x7fffffff, 0);
343
    TEST_JCC("no", 0x7fffffff, -1);
344

    
345
    TEST_JCC("s", 0, 1);
346
    TEST_JCC("s", 0, -1);
347
    TEST_JCC("s", 0, 0);
348

    
349
    TEST_JCC("ns", 0, 1);
350
    TEST_JCC("ns", 0, -1);
351
    TEST_JCC("ns", 0, 0);
352
}
353

    
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

    
372
void 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

    
390
void 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

    
408
void 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

    
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

    
489
void 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

    
501
/**********************************************/
502

    
503
void 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

    
524
void 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);
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
    }
546
}
547

    
548
void test_fcvt(double a)
549
{
550
    float fa;
551
    long double la;
552
    int16_t fpuc;
553
    int i;
554
    int64_t lla;
555
    int ia;
556
    int16_t wa;
557
    double ra;
558

    
559
    fa = a;
560
    la = a;
561
    printf("(float)%f = %f\n", a, fa);
562
    printf("(long double)%f = %Lf\n", a, la);
563
    printf("a=%016Lx\n", *(long long *)&a);
564
    printf("la=%016Lx %04x\n", *(long long *)&la, 
565
           *(unsigned short *)((char *)(&la) + 8));
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
    }
581
}
582

    
583
#define TEST(N) \
584
    asm("fld" #N : "=t" (a)); \
585
    printf("fld" #N "= %f\n", a);
586

    
587
void 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

    
599
void 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

    
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"\
616
        : "=t" (res) : "m" (*(env)));\
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"\
626
        : "=t" (res) : "m" (*(env)));\
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

    
636
void 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

    
661
void test_floats(void)
662
{
663
    test_fops(2, 3);
664
    test_fops(1.4, -5);
665
    test_fcmp(2, -1);
666
    test_fcmp(2, 2);
667
    test_fcmp(2, 3);
668
    test_fcvt(0.5);
669
    test_fcvt(-0.5);
670
    test_fcvt(1.0/7.0);
671
    test_fcvt(-1.0/9.0);
672
    test_fcvt(32768);
673
    test_fcvt(-1e20);
674
    test_fconst();
675
    test_fbcd(1234567890123456);
676
    test_fbcd(-123451234567890);
677
    test_fenv();
678
}
679

    
680
/**********************************************/
681

    
682
#define TEST_BCD(op, op0, cc_in, cc_mask)\
683
{\
684
    int res, flags;\
685
    res = op0;\
686
    flags = cc_in;\
687
    asm ("push %3\n\t"\
688
         "popf\n\t"\
689
         #op "\n\t"\
690
         "pushf\n\t"\
691
         "popl %1\n\t"\
692
        : "=a" (res), "=g" (flags)\
693
        : "0" (res), "1" (flags));\
694
    printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
695
           #op, op0, res, cc_in, flags & cc_mask);\
696
}
697

    
698
void test_bcd(void)
699
{
700
    TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
701
    TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
702
    TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
703
    TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
704
    TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
705
    TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
706
    TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
707
    TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
708
    TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
709
    TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
710
    TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
711
    TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
712
    TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
713

    
714
    TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
715
    TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
716
    TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
717
    TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
718
    TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
719
    TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
720
    TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
721
    TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
722
    TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
723
    TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
724
    TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
725
    TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
726
    TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
727

    
728
    TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
729
    TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
730
    TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
731
    TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
732
    TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
733
    TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
734
    TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
735
    TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
736
    
737
    TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
738
    TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
739
    TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
740
    TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
741
    TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
742
    TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
743
    TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
744
    TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
745

    
746
    TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
747
    TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
748
}
749

    
750
#define TEST_XCHG(op, size, opconst)\
751
{\
752
    int op0, op1;\
753
    op0 = 0x12345678;\
754
    op1 = 0xfbca7654;\
755
    asm(#op " %" size "0, %" size "1" \
756
        : "=q" (op0), opconst (op1) \
757
        : "0" (op0), "1" (op1));\
758
    printf("%-10s A=%08x B=%08x\n",\
759
           #op, op0, op1);\
760
}
761

    
762
#define TEST_CMPXCHG(op, size, opconst, eax)\
763
{\
764
    int op0, op1;\
765
    op0 = 0x12345678;\
766
    op1 = 0xfbca7654;\
767
    asm(#op " %" size "0, %" size "1" \
768
        : "=q" (op0), opconst (op1) \
769
        : "0" (op0), "1" (op1), "a" (eax));\
770
    printf("%-10s EAX=%08x A=%08x C=%08x\n",\
771
           #op, eax, op0, op1);\
772
}
773

    
774
void test_xchg(void)
775
{
776
    TEST_XCHG(xchgl, "", "=q");
777
    TEST_XCHG(xchgw, "w", "=q");
778
    TEST_XCHG(xchgb, "b", "=q");
779

    
780
    TEST_XCHG(xchgl, "", "=m");
781
    TEST_XCHG(xchgw, "w", "=m");
782
    TEST_XCHG(xchgb, "b", "=m");
783

    
784
    TEST_XCHG(xaddl, "", "=q");
785
    TEST_XCHG(xaddw, "w", "=q");
786
    TEST_XCHG(xaddb, "b", "=q");
787

    
788
    TEST_XCHG(xaddl, "", "=m");
789
    TEST_XCHG(xaddw, "w", "=m");
790
    TEST_XCHG(xaddb, "b", "=m");
791

    
792
    TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
793
    TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
794
    TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
795

    
796
    TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
797
    TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
798
    TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
799

    
800
    TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
801
    TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
802
    TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
803

    
804
    TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
805
    TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
806
    TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
807
}
808

    
809
/**********************************************/
810
/* segmentation tests */
811

    
812
#include <asm/ldt.h>
813
#include <linux/unistd.h>
814

    
815
_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
816

    
817
uint8_t seg_data1[4096];
818
uint8_t seg_data2[4096];
819

    
820
#define MK_SEL(n) (((n) << 3) | 7)
821

    
822
#define TEST_LR(op, size, seg, mask)\
823
{\
824
    int res, res2;\
825
    res = 0x12345678;\
826
    asm (op " %" size "2, %" size "0\n" \
827
         "movl $0, %1\n"\
828
         "jnz 1f\n"\
829
         "movl $1, %1\n"\
830
         "1:\n"\
831
         : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
832
    printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
833
}
834

    
835
/* NOTE: we use Linux modify_ldt syscall */
836
void test_segs(void)
837
{
838
    struct modify_ldt_ldt_s ldt;
839
    long long ldt_table[3];
840
    int res, res2;
841
    char tmp;
842
    struct {
843
        uint32_t offset;
844
        uint16_t seg;
845
    } __attribute__((packed)) segoff;
846

    
847
    ldt.entry_number = 1;
848
    ldt.base_addr = (unsigned long)&seg_data1;
849
    ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
850
    ldt.seg_32bit = 1;
851
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
852
    ldt.read_exec_only = 0;
853
    ldt.limit_in_pages = 1;
854
    ldt.seg_not_present = 0;
855
    ldt.useable = 1;
856
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
857

    
858
    ldt.entry_number = 2;
859
    ldt.base_addr = (unsigned long)&seg_data2;
860
    ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
861
    ldt.seg_32bit = 1;
862
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
863
    ldt.read_exec_only = 0;
864
    ldt.limit_in_pages = 1;
865
    ldt.seg_not_present = 0;
866
    ldt.useable = 1;
867
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
868

    
869
    modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
870
#if 0
871
    {
872
        int i;
873
        for(i=0;i<3;i++)
874
            printf("%d: %016Lx\n", i, ldt_table[i]);
875
    }
876
#endif
877
    /* do some tests with fs or gs */
878
    asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
879

    
880
    seg_data1[1] = 0xaa;
881
    seg_data2[1] = 0x55;
882

    
883
    asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
884
    printf("FS[1] = %02x\n", res);
885

    
886
    asm volatile ("pushl %%gs\n"
887
                  "movl %1, %%gs\n"
888
                  "gs movzbl 0x1, %0\n"
889
                  "popl %%gs\n"
890
                  : "=r" (res)
891
                  : "r" (MK_SEL(2)));
892
    printf("GS[1] = %02x\n", res);
893

    
894
    /* tests with ds/ss (implicit segment case) */
895
    tmp = 0xa5;
896
    asm volatile ("pushl %%ebp\n\t"
897
                  "pushl %%ds\n\t"
898
                  "movl %2, %%ds\n\t"
899
                  "movl %3, %%ebp\n\t"
900
                  "movzbl 0x1, %0\n\t"
901
                  "movzbl (%%ebp), %1\n\t"
902
                  "popl %%ds\n\t"
903
                  "popl %%ebp\n\t"
904
                  : "=r" (res), "=r" (res2)
905
                  : "r" (MK_SEL(1)), "r" (&tmp));
906
    printf("DS[1] = %02x\n", res);
907
    printf("SS[tmp] = %02x\n", res2);
908

    
909
    segoff.seg = MK_SEL(2);
910
    segoff.offset = 0xabcdef12;
911
    asm volatile("lfs %2, %0\n\t" 
912
                 "movl %%fs, %1\n\t"
913
                 : "=r" (res), "=g" (res2) 
914
                 : "m" (segoff));
915
    printf("FS:reg = %04x:%08x\n", res2, res);
916

    
917
    TEST_LR("larw", "w", MK_SEL(2), 0x0100);
918
    TEST_LR("larl", "", MK_SEL(2), 0x0100);
919
    TEST_LR("lslw", "w", MK_SEL(2), 0);
920
    TEST_LR("lsll", "", MK_SEL(2), 0);
921

    
922
    TEST_LR("larw", "w", 0xfff8, 0);
923
    TEST_LR("larl", "", 0xfff8, 0);
924
    TEST_LR("lslw", "w", 0xfff8, 0);
925
    TEST_LR("lsll", "", 0xfff8, 0);
926
}
927

    
928
/* 16 bit code test */
929
extern char code16_start, code16_end;
930
extern char code16_func1;
931
extern char code16_func2;
932
extern char code16_func3;
933

    
934
void test_code16(void)
935
{
936
    struct modify_ldt_ldt_s ldt;
937
    int res, res2;
938

    
939
    /* build a code segment */
940
    ldt.entry_number = 1;
941
    ldt.base_addr = (unsigned long)&code16_start;
942
    ldt.limit = &code16_end - &code16_start;
943
    ldt.seg_32bit = 0;
944
    ldt.contents = MODIFY_LDT_CONTENTS_CODE;
945
    ldt.read_exec_only = 0;
946
    ldt.limit_in_pages = 0;
947
    ldt.seg_not_present = 0;
948
    ldt.useable = 1;
949
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
950

    
951
    /* call the first function */
952
    asm volatile ("lcall %1, %2" 
953
                  : "=a" (res)
954
                  : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
955
    printf("func1() = 0x%08x\n", res);
956
    asm volatile ("lcall %2, %3" 
957
                  : "=a" (res), "=c" (res2)
958
                  : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
959
    printf("func2() = 0x%08x spdec=%d\n", res, res2);
960
    asm volatile ("lcall %1, %2" 
961
                  : "=a" (res)
962
                  : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
963
    printf("func3() = 0x%08x\n", res);
964
}
965

    
966
extern char func_lret32;
967
extern char func_iret32;
968

    
969
void test_misc(void)
970
{
971
    char table[256];
972
    int res, i;
973

    
974
    for(i=0;i<256;i++) table[i] = 256 - i;
975
    res = 0x12345678;
976
    asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
977
    printf("xlat: EAX=%08x\n", res);
978

    
979
    asm volatile ("pushl %%cs ; call %1" 
980
                  : "=a" (res)
981
                  : "m" (func_lret32): "memory", "cc");
982
    printf("func_lret32=%x\n", res);
983

    
984
    asm volatile ("pushfl ; pushl %%cs ; call %1" 
985
                  : "=a" (res)
986
                  : "m" (func_iret32): "memory", "cc");
987
    printf("func_iret32=%x\n", res);
988

    
989
    /* specific popl test */
990
    asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
991
                  : "=g" (res));
992
    printf("popl esp=%x\n", res);
993

    
994
    /* specific popw test */
995
    asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
996
                  : "=g" (res));
997
    printf("popw esp=%x\n", res);
998
}
999

    
1000
uint8_t str_buffer[4096];
1001

    
1002
#define TEST_STRING1(OP, size, DF, REP)\
1003
{\
1004
    int esi, edi, eax, ecx, eflags;\
1005
\
1006
    esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1007
    edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1008
    eax = 0x12345678;\
1009
    ecx = 17;\
1010
\
1011
    asm volatile ("pushl $0\n\t"\
1012
                  "popf\n\t"\
1013
                  DF "\n\t"\
1014
                  REP #OP size "\n\t"\
1015
                  "cld\n\t"\
1016
                  "pushf\n\t"\
1017
                  "popl %4\n\t"\
1018
                  : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1019
                  : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1020
    printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1021
           REP #OP size, esi, edi, eax, ecx,\
1022
           eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1023
}
1024

    
1025
#define TEST_STRING(OP, REP)\
1026
    TEST_STRING1(OP, "b", "", REP);\
1027
    TEST_STRING1(OP, "w", "", REP);\
1028
    TEST_STRING1(OP, "l", "", REP);\
1029
    TEST_STRING1(OP, "b", "std", REP);\
1030
    TEST_STRING1(OP, "w", "std", REP);\
1031
    TEST_STRING1(OP, "l", "std", REP)
1032

    
1033
void test_string(void)
1034
{
1035
    int i;
1036
    for(i = 0;i < sizeof(str_buffer); i++)
1037
        str_buffer[i] = i + 0x56;
1038
   TEST_STRING(stos, "");
1039
   TEST_STRING(stos, "rep ");
1040
   TEST_STRING(lods, ""); /* to verify stos */
1041
   TEST_STRING(lods, "rep "); 
1042
   TEST_STRING(movs, "");
1043
   TEST_STRING(movs, "rep ");
1044
   TEST_STRING(lods, ""); /* to verify stos */
1045

    
1046
   /* XXX: better tests */
1047
   TEST_STRING(scas, "");
1048
   TEST_STRING(scas, "repz ");
1049
   TEST_STRING(scas, "repnz ");
1050
   TEST_STRING(cmps, "");
1051
   TEST_STRING(cmps, "repz ");
1052
   TEST_STRING(cmps, "repnz ");
1053
}
1054

    
1055
/* VM86 test */
1056

    
1057
static inline void set_bit(uint8_t *a, unsigned int bit)
1058
{
1059
    a[bit / 8] |= (1 << (bit % 8));
1060
}
1061

    
1062
static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1063
{
1064
    return (uint8_t *)((seg << 4) + (reg & 0xffff));
1065
}
1066

    
1067
static inline void pushw(struct vm86_regs *r, int val)
1068
{
1069
    r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1070
    *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1071
}
1072

    
1073
#undef __syscall_return
1074
#define __syscall_return(type, res) \
1075
do { \
1076
        return (type) (res); \
1077
} while (0)
1078

    
1079
_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1080

    
1081
extern char vm86_code_start;
1082
extern char vm86_code_end;
1083

    
1084
#define VM86_CODE_CS 0x100
1085
#define VM86_CODE_IP 0x100
1086

    
1087
void test_vm86(void)
1088
{
1089
    struct vm86plus_struct ctx;
1090
    struct vm86_regs *r;
1091
    uint8_t *vm86_mem;
1092
    int seg, ret;
1093

    
1094
    vm86_mem = mmap((void *)0x00000000, 0x110000, 
1095
                    PROT_WRITE | PROT_READ | PROT_EXEC, 
1096
                    MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1097
    if (vm86_mem == MAP_FAILED) {
1098
        printf("ERROR: could not map vm86 memory");
1099
        return;
1100
    }
1101
    memset(&ctx, 0, sizeof(ctx));
1102

    
1103
    /* init basic registers */
1104
    r = &ctx.regs;
1105
    r->eip = VM86_CODE_IP;
1106
    r->esp = 0xfffe;
1107
    seg = VM86_CODE_CS;
1108
    r->cs = seg;
1109
    r->ss = seg;
1110
    r->ds = seg;
1111
    r->es = seg;
1112
    r->fs = seg;
1113
    r->gs = seg;
1114
    r->eflags = VIF_MASK;
1115

    
1116
    /* move code to proper address. We use the same layout as a .com
1117
       dos program. */
1118
    memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP, 
1119
           &vm86_code_start, &vm86_code_end - &vm86_code_start);
1120

    
1121
    /* mark int 0x21 as being emulated */
1122
    set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1123

    
1124
    for(;;) {
1125
        ret = vm86(VM86_ENTER, &ctx);
1126
        switch(VM86_TYPE(ret)) {
1127
        case VM86_INTx:
1128
            {
1129
                int int_num, ah, v;
1130
                
1131
                int_num = VM86_ARG(ret);
1132
                if (int_num != 0x21)
1133
                    goto unknown_int;
1134
                ah = (r->eax >> 8) & 0xff;
1135
                switch(ah) {
1136
                case 0x00: /* exit */
1137
                    goto the_end;
1138
                case 0x02: /* write char */
1139
                    {
1140
                        uint8_t c = r->edx;
1141
                        putchar(c);
1142
                    }
1143
                    break;
1144
                case 0x09: /* write string */
1145
                    {
1146
                        uint8_t c, *ptr;
1147
                        ptr = seg_to_linear(r->ds, r->edx);
1148
                        for(;;) {
1149
                            c = *ptr++;
1150
                            if (c == '$')
1151
                                break;
1152
                            putchar(c);
1153
                        }
1154
                        r->eax = (r->eax & ~0xff) | '$';
1155
                    }
1156
                    break;
1157
                case 0xff: /* extension: write eflags number in edx */
1158
                    v = (int)r->edx;
1159
#ifndef LINUX_VM86_IOPL_FIX
1160
                    v &= ~0x3000;
1161
#endif
1162
                    printf("%08x\n", v);
1163
                    break;
1164
                default:
1165
                unknown_int:
1166
                    printf("unsupported int 0x%02x\n", int_num);
1167
                    goto the_end;
1168
                }
1169
            }
1170
            break;
1171
        case VM86_SIGNAL:
1172
            /* a signal came, we just ignore that */
1173
            break;
1174
        case VM86_STI:
1175
            break;
1176
        default:
1177
            printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1178
            goto the_end;
1179
        }
1180
    }
1181
 the_end:
1182
    printf("VM86 end\n");
1183
    munmap(vm86_mem, 0x110000);
1184
}
1185

    
1186
/* exception tests */
1187
#ifndef REG_EAX
1188
#define REG_EAX EAX
1189
#define REG_EBX EBX
1190
#define REG_ECX ECX
1191
#define REG_EDX EDX
1192
#define REG_ESI ESI
1193
#define REG_EDI EDI
1194
#define REG_EBP EBP
1195
#define REG_ESP ESP
1196
#define REG_EIP EIP
1197
#define REG_EFL EFL
1198
#define REG_TRAPNO TRAPNO
1199
#define REG_ERR ERR
1200
#endif
1201

    
1202
jmp_buf jmp_env;
1203
int v1;
1204
int tab[2];
1205

    
1206
void sig_handler(int sig, siginfo_t *info, void *puc)
1207
{
1208
    struct ucontext *uc = puc;
1209

    
1210
    printf("si_signo=%d si_errno=%d si_code=%d",
1211
           info->si_signo, info->si_errno, info->si_code);
1212
    printf(" si_addr=0x%08lx",
1213
           (unsigned long)info->si_addr);
1214
    printf("\n");
1215

    
1216
    printf("trapno=0x%02x err=0x%08x",
1217
           uc->uc_mcontext.gregs[REG_TRAPNO],
1218
           uc->uc_mcontext.gregs[REG_ERR]);
1219
    printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
1220
    printf("\n");
1221
    longjmp(jmp_env, 1);
1222
}
1223

    
1224
void test_exceptions(void)
1225
{
1226
    struct modify_ldt_ldt_s ldt;
1227
    struct sigaction act;
1228
    volatile int val;
1229
    
1230
    act.sa_sigaction = sig_handler;
1231
    sigemptyset(&act.sa_mask);
1232
    act.sa_flags = SA_SIGINFO;
1233
    sigaction(SIGFPE, &act, NULL);
1234
    sigaction(SIGILL, &act, NULL);
1235
    sigaction(SIGSEGV, &act, NULL);
1236
    sigaction(SIGBUS, &act, NULL);
1237
    sigaction(SIGTRAP, &act, NULL);
1238

    
1239
    /* test division by zero reporting */
1240
    printf("DIVZ exception:\n");
1241
    if (setjmp(jmp_env) == 0) {
1242
        /* now divide by zero */
1243
        v1 = 0;
1244
        v1 = 2 / v1;
1245
    }
1246

    
1247
    printf("BOUND exception:\n");
1248
    if (setjmp(jmp_env) == 0) {
1249
        /* bound exception */
1250
        tab[0] = 1;
1251
        tab[1] = 10;
1252
        asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1253
    }
1254

    
1255
    printf("segment exceptions:\n");
1256
    if (setjmp(jmp_env) == 0) {
1257
        /* load an invalid segment */
1258
        asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1259
    }
1260
    if (setjmp(jmp_env) == 0) {
1261
        /* null data segment is valid */
1262
        asm volatile ("movl %0, %%fs" : : "r" (3));
1263
        /* null stack segment */
1264
        asm volatile ("movl %0, %%ss" : : "r" (3));
1265
    }
1266

    
1267
    ldt.entry_number = 1;
1268
    ldt.base_addr = (unsigned long)&seg_data1;
1269
    ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1270
    ldt.seg_32bit = 1;
1271
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1272
    ldt.read_exec_only = 0;
1273
    ldt.limit_in_pages = 1;
1274
    ldt.seg_not_present = 1;
1275
    ldt.useable = 1;
1276
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1277

    
1278
    if (setjmp(jmp_env) == 0) {
1279
        /* segment not present */
1280
        asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1281
    }
1282

    
1283
    /* test SEGV reporting */
1284
    printf("PF exception:\n");
1285
    if (setjmp(jmp_env) == 0) {
1286
        val = 1;
1287
        /* we add a nop to test a weird PC retrieval case */
1288
        asm volatile ("nop");
1289
        /* now store in an invalid address */
1290
        *(char *)0x1234 = 1;
1291
    }
1292

    
1293
    /* test SEGV reporting */
1294
    printf("PF exception:\n");
1295
    if (setjmp(jmp_env) == 0) {
1296
        val = 1;
1297
        /* read from an invalid address */
1298
        v1 = *(char *)0x1234;
1299
    }
1300
    
1301
    /* test illegal instruction reporting */
1302
    printf("UD2 exception:\n");
1303
    if (setjmp(jmp_env) == 0) {
1304
        /* now execute an invalid instruction */
1305
        asm volatile("ud2");
1306
    }
1307
    
1308
    printf("INT exception:\n");
1309
    if (setjmp(jmp_env) == 0) {
1310
        asm volatile ("int $0xfd");
1311
    }
1312
    if (setjmp(jmp_env) == 0) {
1313
        asm volatile ("int $0x01");
1314
    }
1315
    if (setjmp(jmp_env) == 0) {
1316
        asm volatile (".byte 0xcd, 0x03");
1317
    }
1318
    if (setjmp(jmp_env) == 0) {
1319
        asm volatile ("int $0x04");
1320
    }
1321
    if (setjmp(jmp_env) == 0) {
1322
        asm volatile ("int $0x05");
1323
    }
1324

    
1325
    printf("INT3 exception:\n");
1326
    if (setjmp(jmp_env) == 0) {
1327
        asm volatile ("int3");
1328
    }
1329

    
1330
    printf("CLI exception:\n");
1331
    if (setjmp(jmp_env) == 0) {
1332
        asm volatile ("cli");
1333
    }
1334

    
1335
    printf("STI exception:\n");
1336
    if (setjmp(jmp_env) == 0) {
1337
        asm volatile ("cli");
1338
    }
1339

    
1340
    printf("INTO exception:\n");
1341
    if (setjmp(jmp_env) == 0) {
1342
        /* overflow exception */
1343
        asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1344
    }
1345

    
1346
    printf("OUTB exception:\n");
1347
    if (setjmp(jmp_env) == 0) {
1348
        asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1349
    }
1350

    
1351
    printf("INB exception:\n");
1352
    if (setjmp(jmp_env) == 0) {
1353
        asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1354
    }
1355

    
1356
    printf("REP OUTSB exception:\n");
1357
    if (setjmp(jmp_env) == 0) {
1358
        asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1359
    }
1360

    
1361
    printf("REP INSB exception:\n");
1362
    if (setjmp(jmp_env) == 0) {
1363
        asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1364
    }
1365

    
1366
    printf("HLT exception:\n");
1367
    if (setjmp(jmp_env) == 0) {
1368
        asm volatile ("hlt");
1369
    }
1370

    
1371
    printf("single step exception:\n");
1372
    val = 0;
1373
    if (setjmp(jmp_env) == 0) {
1374
        asm volatile ("pushf\n"
1375
                      "orl $0x00100, (%%esp)\n"
1376
                      "popf\n"
1377
                      "movl $0xabcd, %0\n" 
1378
                      "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1379
    }
1380
    printf("val=0x%x\n", val);
1381
}
1382

    
1383
/* specific precise single step test */
1384
void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1385
{
1386
    struct ucontext *uc = puc;
1387
    printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1388
}
1389

    
1390
const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1391
uint8_t sstep_buf2[4];
1392

    
1393
void test_single_step(void)
1394
{
1395
    struct sigaction act;
1396
    volatile int val;
1397
    int i;
1398

    
1399
    val = 0;
1400
    act.sa_sigaction = sig_trap_handler;
1401
    sigemptyset(&act.sa_mask);
1402
    act.sa_flags = SA_SIGINFO;
1403
    sigaction(SIGTRAP, &act, NULL);
1404
    asm volatile ("pushf\n"
1405
                  "orl $0x00100, (%%esp)\n"
1406
                  "popf\n"
1407
                  "movl $0xabcd, %0\n" 
1408

    
1409
                  /* jmp test */
1410
                  "movl $3, %%ecx\n"
1411
                  "1:\n"
1412
                  "addl $1, %0\n"
1413
                  "decl %%ecx\n"
1414
                  "jnz 1b\n"
1415

    
1416
                  /* movsb: the single step should stop at each movsb iteration */
1417
                  "movl $sstep_buf1, %%esi\n"
1418
                  "movl $sstep_buf2, %%edi\n"
1419
                  "movl $0, %%ecx\n"
1420
                  "rep movsb\n"
1421
                  "movl $3, %%ecx\n"
1422
                  "rep movsb\n"
1423
                  "movl $1, %%ecx\n"
1424
                  "rep movsb\n"
1425

    
1426
                  /* cmpsb: the single step should stop at each cmpsb iteration */
1427
                  "movl $sstep_buf1, %%esi\n"
1428
                  "movl $sstep_buf2, %%edi\n"
1429
                  "movl $0, %%ecx\n"
1430
                  "rep cmpsb\n"
1431
                  "movl $4, %%ecx\n"
1432
                  "rep cmpsb\n"
1433
                  
1434
                  /* getpid() syscall: single step should skip one
1435
                     instruction */
1436
                  "movl $20, %%eax\n"
1437
                  "int $0x80\n"
1438
                  "movl $0, %%eax\n"
1439
                  
1440
                  /* when modifying SS, trace is not done on the next
1441
                     instruction */
1442
                  "movl %%ss, %%ecx\n"
1443
                  "movl %%ecx, %%ss\n"
1444
                  "addl $1, %0\n"
1445
                  "movl $1, %%eax\n"
1446
                  "movl %%ecx, %%ss\n"
1447
                  "jmp 1f\n"
1448
                  "addl $1, %0\n"
1449
                  "1:\n"
1450
                  "movl $1, %%eax\n"
1451
                  "pushl %%ecx\n"
1452
                  "popl %%ss\n"
1453
                  "addl $1, %0\n"
1454
                  "movl $1, %%eax\n"
1455
                  
1456
                  "pushf\n"
1457
                  "andl $~0x00100, (%%esp)\n"
1458
                  "popf\n"
1459
                  : "=m" (val) 
1460
                  : 
1461
                  : "cc", "memory", "eax", "ecx", "esi", "edi");
1462
    printf("val=%d\n", val);
1463
    for(i = 0; i < 4; i++)
1464
        printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1465
}
1466

    
1467
/* self modifying code test */
1468
uint8_t code[] = {
1469
    0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1470
    0xc3, /* ret */
1471
};
1472

    
1473
typedef int FuncType(void);
1474

    
1475
void test_self_modifying_code(void)
1476
{
1477
    int i;
1478

    
1479
    printf("self modifying code:\n");
1480
    printf("func1 = 0x%x\n", ((FuncType *)code)());
1481
    for(i = 2; i <= 4; i++) {
1482
        code[1] = i;
1483
        printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1484
    }
1485
}
1486
    
1487
static void *call_end __init_call = NULL;
1488

    
1489
int main(int argc, char **argv)
1490
{
1491
    void **ptr;
1492
    void (*func)(void);
1493

    
1494
    ptr = &call_start + 1;
1495
    while (*ptr != NULL) {
1496
        func = *ptr++;
1497
        func();
1498
    }
1499
    test_bsx();
1500
    test_mul();
1501
    test_jcc();
1502
    test_floats();
1503
    test_bcd();
1504
    test_xchg();
1505
    test_string();
1506
    test_misc();
1507
    test_lea();
1508
    test_segs();
1509
    test_code16();
1510
    test_vm86();
1511
    test_exceptions();
1512
    test_self_modifying_code();
1513
    test_single_step();
1514
    return 0;
1515
}