Statistics
| Branch: | Revision:

root / tests / test-i386.c @ 6b2b6112

History | View | Annotate | Download (41.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
//#define TEST_P4_FLAGS
37

    
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

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

    
52
static void *call_start __init_call = NULL;
53

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

    
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

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

    
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

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

    
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

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

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

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

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

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

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

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

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

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

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

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

    
333

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

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

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

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

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

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

    
355
#undef CC_MASK
356
#ifdef TEST_P4_FLAGS
357
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
358
#else
359
#define CC_MASK (CC_O | CC_C)
360
#endif
361

    
362
#define OP mul
363
#include "test-i386-muldiv.h"
364

    
365
#define OP imul
366
#include "test-i386-muldiv.h"
367

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

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

    
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

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

    
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

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

    
506
/**********************************************/
507

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

    
529
void 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);
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
    }
551
}
552

    
553
void test_fcvt(double a)
554
{
555
    float fa;
556
    long double la;
557
    int16_t fpuc;
558
    int i;
559
    int64_t lla;
560
    int ia;
561
    int16_t wa;
562
    double ra;
563

    
564
    fa = a;
565
    la = a;
566
    printf("(float)%f = %f\n", a, fa);
567
    printf("(long double)%f = %Lf\n", a, la);
568
    printf("a=%016Lx\n", *(long long *)&a);
569
    printf("la=%016Lx %04x\n", *(long long *)&la, 
570
           *(unsigned short *)((char *)(&la) + 8));
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
    }
586
}
587

    
588
#define TEST(N) \
589
    asm("fld" #N : "=t" (a)); \
590
    printf("fld" #N "= %f\n", a);
591

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

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

    
615
#define TEST_ENV(env, save, restore)\
616
{\
617
    memset((env), 0xaa, sizeof(*(env)));\
618
    for(i=0;i<5;i++)\
619
        asm volatile ("fldl %0" : : "m" (dtab[i]));\
620
    asm(save " %0\n" : : "m" (*(env)));\
621
    asm(restore " %0\n": : "m" (*(env)));\
622
    for(i=0;i<5;i++)\
623
        asm volatile ("fstpl %0" : "=m" (rtab[i]));\
624
    for(i=0;i<5;i++)\
625
        printf("res[%d]=%f\n", i, rtab[i]);\
626
    printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
627
           (env)->fpuc,\
628
           (env)->fpus & 0xff00,\
629
           (env)->fptag);\
630
}
631

    
632
void test_fenv(void)
633
{
634
    struct __attribute__((packed)) {
635
        uint16_t fpuc;
636
        uint16_t dummy1;
637
        uint16_t fpus;
638
        uint16_t dummy2;
639
        uint16_t fptag;
640
        uint16_t dummy3;
641
        uint32_t ignored[4];
642
        long double fpregs[8];
643
    } float_env32;
644
    struct __attribute__((packed)) {
645
        uint16_t fpuc;
646
        uint16_t fpus;
647
        uint16_t fptag;
648
        uint16_t ignored[4];
649
        long double fpregs[8];
650
    } float_env16;
651
    double dtab[8];
652
    double rtab[8];
653
    int i;
654

    
655
    for(i=0;i<8;i++)
656
        dtab[i] = i + 1;
657

    
658
    TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
659
    TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
660
    TEST_ENV(&float_env32, "fnstenv", "fldenv");
661
    TEST_ENV(&float_env32, "fnsave", "frstor");
662
}
663

    
664

    
665
#define TEST_FCMOV(a, b, eflags, CC)\
666
{\
667
    double res;\
668
    asm("push %3\n"\
669
        "popf\n"\
670
        "fcmov" CC " %2, %0\n"\
671
        : "=t" (res)\
672
        : "0" (a), "u" (b), "g" (eflags));\
673
    printf("fcmov%s eflags=0x%04x-> %f\n", \
674
           CC, eflags, res);\
675
}
676

    
677
void test_fcmov(void)
678
{
679
    double a, b;
680
    int eflags, i;
681

    
682
    a = 1.0;
683
    b = 2.0;
684
    for(i = 0; i < 4; i++) {
685
        eflags = 0;
686
        if (i & 1)
687
            eflags |= CC_C;
688
        if (i & 2)
689
            eflags |= CC_Z;
690
        TEST_FCMOV(a, b, eflags, "b");
691
        TEST_FCMOV(a, b, eflags, "e");
692
        TEST_FCMOV(a, b, eflags, "be");
693
        TEST_FCMOV(a, b, eflags, "nb");
694
        TEST_FCMOV(a, b, eflags, "ne");
695
        TEST_FCMOV(a, b, eflags, "nbe");
696
    }
697
    TEST_FCMOV(a, b, 0, "u");
698
    TEST_FCMOV(a, b, CC_P, "u");
699
    TEST_FCMOV(a, b, 0, "nu");
700
    TEST_FCMOV(a, b, CC_P, "nu");
701
}
702

    
703
void test_floats(void)
704
{
705
    test_fops(2, 3);
706
    test_fops(1.4, -5);
707
    test_fcmp(2, -1);
708
    test_fcmp(2, 2);
709
    test_fcmp(2, 3);
710
    test_fcvt(0.5);
711
    test_fcvt(-0.5);
712
    test_fcvt(1.0/7.0);
713
    test_fcvt(-1.0/9.0);
714
    test_fcvt(32768);
715
    test_fcvt(-1e20);
716
    test_fconst();
717
    test_fbcd(1234567890123456);
718
    test_fbcd(-123451234567890);
719
    test_fenv();
720
    if (TEST_CMOV) {
721
        test_fcmov();
722
    }
723
}
724

    
725
/**********************************************/
726

    
727
#define TEST_BCD(op, op0, cc_in, cc_mask)\
728
{\
729
    int res, flags;\
730
    res = op0;\
731
    flags = cc_in;\
732
    asm ("push %3\n\t"\
733
         "popf\n\t"\
734
         #op "\n\t"\
735
         "pushf\n\t"\
736
         "popl %1\n\t"\
737
        : "=a" (res), "=g" (flags)\
738
        : "0" (res), "1" (flags));\
739
    printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
740
           #op, op0, res, cc_in, flags & cc_mask);\
741
}
742

    
743
void test_bcd(void)
744
{
745
    TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
746
    TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
747
    TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
748
    TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
749
    TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
750
    TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
751
    TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
752
    TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
753
    TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
754
    TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
755
    TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
756
    TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
757
    TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
758

    
759
    TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
760
    TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
761
    TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
762
    TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
763
    TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
764
    TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
765
    TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
766
    TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
767
    TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
768
    TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
769
    TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
770
    TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
771
    TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
772

    
773
    TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
774
    TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
775
    TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
776
    TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
777
    TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
778
    TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
779
    TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
780
    TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
781
    
782
    TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
783
    TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
784
    TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
785
    TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
786
    TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
787
    TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
788
    TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
789
    TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
790

    
791
    TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
792
    TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
793
}
794

    
795
#define TEST_XCHG(op, size, opconst)\
796
{\
797
    int op0, op1;\
798
    op0 = 0x12345678;\
799
    op1 = 0xfbca7654;\
800
    asm(#op " %" size "0, %" size "1" \
801
        : "=q" (op0), opconst (op1) \
802
        : "0" (op0), "1" (op1));\
803
    printf("%-10s A=%08x B=%08x\n",\
804
           #op, op0, op1);\
805
}
806

    
807
#define TEST_CMPXCHG(op, size, opconst, eax)\
808
{\
809
    int op0, op1;\
810
    op0 = 0x12345678;\
811
    op1 = 0xfbca7654;\
812
    asm(#op " %" size "0, %" size "1" \
813
        : "=q" (op0), opconst (op1) \
814
        : "0" (op0), "1" (op1), "a" (eax));\
815
    printf("%-10s EAX=%08x A=%08x C=%08x\n",\
816
           #op, eax, op0, op1);\
817
}
818

    
819
void test_xchg(void)
820
{
821
    TEST_XCHG(xchgl, "", "=q");
822
    TEST_XCHG(xchgw, "w", "=q");
823
    TEST_XCHG(xchgb, "b", "=q");
824

    
825
    TEST_XCHG(xchgl, "", "=m");
826
    TEST_XCHG(xchgw, "w", "=m");
827
    TEST_XCHG(xchgb, "b", "=m");
828

    
829
    TEST_XCHG(xaddl, "", "=q");
830
    TEST_XCHG(xaddw, "w", "=q");
831
    TEST_XCHG(xaddb, "b", "=q");
832

    
833
    {
834
        int res;
835
        res = 0x12345678;
836
        asm("xaddl %1, %0" : "=r" (res) : "0" (res));
837
        printf("xaddl same res=%08x\n", res);
838
    }
839

    
840
    TEST_XCHG(xaddl, "", "=m");
841
    TEST_XCHG(xaddw, "w", "=m");
842
    TEST_XCHG(xaddb, "b", "=m");
843

    
844
    TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
845
    TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
846
    TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
847

    
848
    TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
849
    TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
850
    TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
851

    
852
    TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
853
    TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
854
    TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
855

    
856
    TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
857
    TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
858
    TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
859

    
860
    {
861
        uint64_t op0, op1, op2;
862
        int i, eflags;
863

    
864
        for(i = 0; i < 2; i++) {
865
            op0 = 0x123456789abcd;
866
            if (i == 0)
867
                op1 = 0xfbca765423456;
868
            else
869
                op1 = op0;
870
            op2 = 0x6532432432434;
871
            asm("cmpxchg8b %1\n" 
872
                "pushf\n"
873
                "popl %2\n"
874
                : "=A" (op0), "=m" (op1), "=g" (eflags)
875
                : "0" (op0), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
876
            printf("cmpxchg8b: op0=%016llx op1=%016llx CC=%02x\n", 
877
                    op0, op1, eflags & CC_Z);
878
        }
879
    }
880
}
881

    
882
/**********************************************/
883
/* segmentation tests */
884

    
885
#include <asm/ldt.h>
886
#include <linux/unistd.h>
887

    
888
_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
889

    
890
uint8_t seg_data1[4096];
891
uint8_t seg_data2[4096];
892

    
893
#define MK_SEL(n) (((n) << 3) | 7)
894

    
895
#define TEST_LR(op, size, seg, mask)\
896
{\
897
    int res, res2;\
898
    res = 0x12345678;\
899
    asm (op " %" size "2, %" size "0\n" \
900
         "movl $0, %1\n"\
901
         "jnz 1f\n"\
902
         "movl $1, %1\n"\
903
         "1:\n"\
904
         : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
905
    printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
906
}
907

    
908
/* NOTE: we use Linux modify_ldt syscall */
909
void test_segs(void)
910
{
911
    struct modify_ldt_ldt_s ldt;
912
    long long ldt_table[3];
913
    int res, res2;
914
    char tmp;
915
    struct {
916
        uint32_t offset;
917
        uint16_t seg;
918
    } __attribute__((packed)) segoff;
919

    
920
    ldt.entry_number = 1;
921
    ldt.base_addr = (unsigned long)&seg_data1;
922
    ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
923
    ldt.seg_32bit = 1;
924
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
925
    ldt.read_exec_only = 0;
926
    ldt.limit_in_pages = 1;
927
    ldt.seg_not_present = 0;
928
    ldt.useable = 1;
929
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
930

    
931
    ldt.entry_number = 2;
932
    ldt.base_addr = (unsigned long)&seg_data2;
933
    ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
934
    ldt.seg_32bit = 1;
935
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
936
    ldt.read_exec_only = 0;
937
    ldt.limit_in_pages = 1;
938
    ldt.seg_not_present = 0;
939
    ldt.useable = 1;
940
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
941

    
942
    modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
943
#if 0
944
    {
945
        int i;
946
        for(i=0;i<3;i++)
947
            printf("%d: %016Lx\n", i, ldt_table[i]);
948
    }
949
#endif
950
    /* do some tests with fs or gs */
951
    asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
952

    
953
    seg_data1[1] = 0xaa;
954
    seg_data2[1] = 0x55;
955

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

    
959
    asm volatile ("pushl %%gs\n"
960
                  "movl %1, %%gs\n"
961
                  "gs movzbl 0x1, %0\n"
962
                  "popl %%gs\n"
963
                  : "=r" (res)
964
                  : "r" (MK_SEL(2)));
965
    printf("GS[1] = %02x\n", res);
966

    
967
    /* tests with ds/ss (implicit segment case) */
968
    tmp = 0xa5;
969
    asm volatile ("pushl %%ebp\n\t"
970
                  "pushl %%ds\n\t"
971
                  "movl %2, %%ds\n\t"
972
                  "movl %3, %%ebp\n\t"
973
                  "movzbl 0x1, %0\n\t"
974
                  "movzbl (%%ebp), %1\n\t"
975
                  "popl %%ds\n\t"
976
                  "popl %%ebp\n\t"
977
                  : "=r" (res), "=r" (res2)
978
                  : "r" (MK_SEL(1)), "r" (&tmp));
979
    printf("DS[1] = %02x\n", res);
980
    printf("SS[tmp] = %02x\n", res2);
981

    
982
    segoff.seg = MK_SEL(2);
983
    segoff.offset = 0xabcdef12;
984
    asm volatile("lfs %2, %0\n\t" 
985
                 "movl %%fs, %1\n\t"
986
                 : "=r" (res), "=g" (res2) 
987
                 : "m" (segoff));
988
    printf("FS:reg = %04x:%08x\n", res2, res);
989

    
990
    TEST_LR("larw", "w", MK_SEL(2), 0x0100);
991
    TEST_LR("larl", "", MK_SEL(2), 0x0100);
992
    TEST_LR("lslw", "w", MK_SEL(2), 0);
993
    TEST_LR("lsll", "", MK_SEL(2), 0);
994

    
995
    TEST_LR("larw", "w", 0xfff8, 0);
996
    TEST_LR("larl", "", 0xfff8, 0);
997
    TEST_LR("lslw", "w", 0xfff8, 0);
998
    TEST_LR("lsll", "", 0xfff8, 0);
999
}
1000

    
1001
/* 16 bit code test */
1002
extern char code16_start, code16_end;
1003
extern char code16_func1;
1004
extern char code16_func2;
1005
extern char code16_func3;
1006

    
1007
void test_code16(void)
1008
{
1009
    struct modify_ldt_ldt_s ldt;
1010
    int res, res2;
1011

    
1012
    /* build a code segment */
1013
    ldt.entry_number = 1;
1014
    ldt.base_addr = (unsigned long)&code16_start;
1015
    ldt.limit = &code16_end - &code16_start;
1016
    ldt.seg_32bit = 0;
1017
    ldt.contents = MODIFY_LDT_CONTENTS_CODE;
1018
    ldt.read_exec_only = 0;
1019
    ldt.limit_in_pages = 0;
1020
    ldt.seg_not_present = 0;
1021
    ldt.useable = 1;
1022
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1023

    
1024
    /* call the first function */
1025
    asm volatile ("lcall %1, %2" 
1026
                  : "=a" (res)
1027
                  : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
1028
    printf("func1() = 0x%08x\n", res);
1029
    asm volatile ("lcall %2, %3" 
1030
                  : "=a" (res), "=c" (res2)
1031
                  : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
1032
    printf("func2() = 0x%08x spdec=%d\n", res, res2);
1033
    asm volatile ("lcall %1, %2" 
1034
                  : "=a" (res)
1035
                  : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1036
    printf("func3() = 0x%08x\n", res);
1037
}
1038

    
1039
extern char func_lret32;
1040
extern char func_iret32;
1041

    
1042
void test_misc(void)
1043
{
1044
    char table[256];
1045
    int res, i;
1046

    
1047
    for(i=0;i<256;i++) table[i] = 256 - i;
1048
    res = 0x12345678;
1049
    asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
1050
    printf("xlat: EAX=%08x\n", res);
1051

    
1052
    asm volatile ("pushl %%cs ; call %1" 
1053
                  : "=a" (res)
1054
                  : "m" (func_lret32): "memory", "cc");
1055
    printf("func_lret32=%x\n", res);
1056

    
1057
    asm volatile ("pushfl ; pushl %%cs ; call %1" 
1058
                  : "=a" (res)
1059
                  : "m" (func_iret32): "memory", "cc");
1060
    printf("func_iret32=%x\n", res);
1061

    
1062
    /* specific popl test */
1063
    asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1064
                  : "=g" (res));
1065
    printf("popl esp=%x\n", res);
1066

    
1067
    /* specific popw test */
1068
    asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1069
                  : "=g" (res));
1070
    printf("popw esp=%x\n", res);
1071
}
1072

    
1073
uint8_t str_buffer[4096];
1074

    
1075
#define TEST_STRING1(OP, size, DF, REP)\
1076
{\
1077
    int esi, edi, eax, ecx, eflags;\
1078
\
1079
    esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1080
    edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1081
    eax = 0x12345678;\
1082
    ecx = 17;\
1083
\
1084
    asm volatile ("pushl $0\n\t"\
1085
                  "popf\n\t"\
1086
                  DF "\n\t"\
1087
                  REP #OP size "\n\t"\
1088
                  "cld\n\t"\
1089
                  "pushf\n\t"\
1090
                  "popl %4\n\t"\
1091
                  : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1092
                  : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1093
    printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1094
           REP #OP size, esi, edi, eax, ecx,\
1095
           eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1096
}
1097

    
1098
#define TEST_STRING(OP, REP)\
1099
    TEST_STRING1(OP, "b", "", REP);\
1100
    TEST_STRING1(OP, "w", "", REP);\
1101
    TEST_STRING1(OP, "l", "", REP);\
1102
    TEST_STRING1(OP, "b", "std", REP);\
1103
    TEST_STRING1(OP, "w", "std", REP);\
1104
    TEST_STRING1(OP, "l", "std", REP)
1105

    
1106
void test_string(void)
1107
{
1108
    int i;
1109
    for(i = 0;i < sizeof(str_buffer); i++)
1110
        str_buffer[i] = i + 0x56;
1111
   TEST_STRING(stos, "");
1112
   TEST_STRING(stos, "rep ");
1113
   TEST_STRING(lods, ""); /* to verify stos */
1114
   TEST_STRING(lods, "rep "); 
1115
   TEST_STRING(movs, "");
1116
   TEST_STRING(movs, "rep ");
1117
   TEST_STRING(lods, ""); /* to verify stos */
1118

    
1119
   /* XXX: better tests */
1120
   TEST_STRING(scas, "");
1121
   TEST_STRING(scas, "repz ");
1122
   TEST_STRING(scas, "repnz ");
1123
   TEST_STRING(cmps, "");
1124
   TEST_STRING(cmps, "repz ");
1125
   TEST_STRING(cmps, "repnz ");
1126
}
1127

    
1128
/* VM86 test */
1129

    
1130
static inline void set_bit(uint8_t *a, unsigned int bit)
1131
{
1132
    a[bit / 8] |= (1 << (bit % 8));
1133
}
1134

    
1135
static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1136
{
1137
    return (uint8_t *)((seg << 4) + (reg & 0xffff));
1138
}
1139

    
1140
static inline void pushw(struct vm86_regs *r, int val)
1141
{
1142
    r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1143
    *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1144
}
1145

    
1146
#undef __syscall_return
1147
#define __syscall_return(type, res) \
1148
do { \
1149
        return (type) (res); \
1150
} while (0)
1151

    
1152
_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1153

    
1154
extern char vm86_code_start;
1155
extern char vm86_code_end;
1156

    
1157
#define VM86_CODE_CS 0x100
1158
#define VM86_CODE_IP 0x100
1159

    
1160
void test_vm86(void)
1161
{
1162
    struct vm86plus_struct ctx;
1163
    struct vm86_regs *r;
1164
    uint8_t *vm86_mem;
1165
    int seg, ret;
1166

    
1167
    vm86_mem = mmap((void *)0x00000000, 0x110000, 
1168
                    PROT_WRITE | PROT_READ | PROT_EXEC, 
1169
                    MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1170
    if (vm86_mem == MAP_FAILED) {
1171
        printf("ERROR: could not map vm86 memory");
1172
        return;
1173
    }
1174
    memset(&ctx, 0, sizeof(ctx));
1175

    
1176
    /* init basic registers */
1177
    r = &ctx.regs;
1178
    r->eip = VM86_CODE_IP;
1179
    r->esp = 0xfffe;
1180
    seg = VM86_CODE_CS;
1181
    r->cs = seg;
1182
    r->ss = seg;
1183
    r->ds = seg;
1184
    r->es = seg;
1185
    r->fs = seg;
1186
    r->gs = seg;
1187
    r->eflags = VIF_MASK;
1188

    
1189
    /* move code to proper address. We use the same layout as a .com
1190
       dos program. */
1191
    memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP, 
1192
           &vm86_code_start, &vm86_code_end - &vm86_code_start);
1193

    
1194
    /* mark int 0x21 as being emulated */
1195
    set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1196

    
1197
    for(;;) {
1198
        ret = vm86(VM86_ENTER, &ctx);
1199
        switch(VM86_TYPE(ret)) {
1200
        case VM86_INTx:
1201
            {
1202
                int int_num, ah, v;
1203
                
1204
                int_num = VM86_ARG(ret);
1205
                if (int_num != 0x21)
1206
                    goto unknown_int;
1207
                ah = (r->eax >> 8) & 0xff;
1208
                switch(ah) {
1209
                case 0x00: /* exit */
1210
                    goto the_end;
1211
                case 0x02: /* write char */
1212
                    {
1213
                        uint8_t c = r->edx;
1214
                        putchar(c);
1215
                    }
1216
                    break;
1217
                case 0x09: /* write string */
1218
                    {
1219
                        uint8_t c, *ptr;
1220
                        ptr = seg_to_linear(r->ds, r->edx);
1221
                        for(;;) {
1222
                            c = *ptr++;
1223
                            if (c == '$')
1224
                                break;
1225
                            putchar(c);
1226
                        }
1227
                        r->eax = (r->eax & ~0xff) | '$';
1228
                    }
1229
                    break;
1230
                case 0xff: /* extension: write eflags number in edx */
1231
                    v = (int)r->edx;
1232
#ifndef LINUX_VM86_IOPL_FIX
1233
                    v &= ~0x3000;
1234
#endif
1235
                    printf("%08x\n", v);
1236
                    break;
1237
                default:
1238
                unknown_int:
1239
                    printf("unsupported int 0x%02x\n", int_num);
1240
                    goto the_end;
1241
                }
1242
            }
1243
            break;
1244
        case VM86_SIGNAL:
1245
            /* a signal came, we just ignore that */
1246
            break;
1247
        case VM86_STI:
1248
            break;
1249
        default:
1250
            printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1251
            goto the_end;
1252
        }
1253
    }
1254
 the_end:
1255
    printf("VM86 end\n");
1256
    munmap(vm86_mem, 0x110000);
1257
}
1258

    
1259
/* exception tests */
1260
#ifndef REG_EAX
1261
#define REG_EAX EAX
1262
#define REG_EBX EBX
1263
#define REG_ECX ECX
1264
#define REG_EDX EDX
1265
#define REG_ESI ESI
1266
#define REG_EDI EDI
1267
#define REG_EBP EBP
1268
#define REG_ESP ESP
1269
#define REG_EIP EIP
1270
#define REG_EFL EFL
1271
#define REG_TRAPNO TRAPNO
1272
#define REG_ERR ERR
1273
#endif
1274

    
1275
jmp_buf jmp_env;
1276
int v1;
1277
int tab[2];
1278

    
1279
void sig_handler(int sig, siginfo_t *info, void *puc)
1280
{
1281
    struct ucontext *uc = puc;
1282

    
1283
    printf("si_signo=%d si_errno=%d si_code=%d",
1284
           info->si_signo, info->si_errno, info->si_code);
1285
    printf(" si_addr=0x%08lx",
1286
           (unsigned long)info->si_addr);
1287
    printf("\n");
1288

    
1289
    printf("trapno=0x%02x err=0x%08x",
1290
           uc->uc_mcontext.gregs[REG_TRAPNO],
1291
           uc->uc_mcontext.gregs[REG_ERR]);
1292
    printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
1293
    printf("\n");
1294
    longjmp(jmp_env, 1);
1295
}
1296

    
1297
void test_exceptions(void)
1298
{
1299
    struct modify_ldt_ldt_s ldt;
1300
    struct sigaction act;
1301
    volatile int val;
1302
    
1303
    act.sa_sigaction = sig_handler;
1304
    sigemptyset(&act.sa_mask);
1305
    act.sa_flags = SA_SIGINFO;
1306
    sigaction(SIGFPE, &act, NULL);
1307
    sigaction(SIGILL, &act, NULL);
1308
    sigaction(SIGSEGV, &act, NULL);
1309
    sigaction(SIGBUS, &act, NULL);
1310
    sigaction(SIGTRAP, &act, NULL);
1311

    
1312
    /* test division by zero reporting */
1313
    printf("DIVZ exception:\n");
1314
    if (setjmp(jmp_env) == 0) {
1315
        /* now divide by zero */
1316
        v1 = 0;
1317
        v1 = 2 / v1;
1318
    }
1319

    
1320
    printf("BOUND exception:\n");
1321
    if (setjmp(jmp_env) == 0) {
1322
        /* bound exception */
1323
        tab[0] = 1;
1324
        tab[1] = 10;
1325
        asm volatile ("bound %0, %1" : : "r" (11), "m" (tab));
1326
    }
1327

    
1328
    printf("segment exceptions:\n");
1329
    if (setjmp(jmp_env) == 0) {
1330
        /* load an invalid segment */
1331
        asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1332
    }
1333
    if (setjmp(jmp_env) == 0) {
1334
        /* null data segment is valid */
1335
        asm volatile ("movl %0, %%fs" : : "r" (3));
1336
        /* null stack segment */
1337
        asm volatile ("movl %0, %%ss" : : "r" (3));
1338
    }
1339

    
1340
    ldt.entry_number = 1;
1341
    ldt.base_addr = (unsigned long)&seg_data1;
1342
    ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1343
    ldt.seg_32bit = 1;
1344
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1345
    ldt.read_exec_only = 0;
1346
    ldt.limit_in_pages = 1;
1347
    ldt.seg_not_present = 1;
1348
    ldt.useable = 1;
1349
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1350

    
1351
    if (setjmp(jmp_env) == 0) {
1352
        /* segment not present */
1353
        asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1354
    }
1355

    
1356
    /* test SEGV reporting */
1357
    printf("PF exception:\n");
1358
    if (setjmp(jmp_env) == 0) {
1359
        val = 1;
1360
        /* we add a nop to test a weird PC retrieval case */
1361
        asm volatile ("nop");
1362
        /* now store in an invalid address */
1363
        *(char *)0x1234 = 1;
1364
    }
1365

    
1366
    /* test SEGV reporting */
1367
    printf("PF exception:\n");
1368
    if (setjmp(jmp_env) == 0) {
1369
        val = 1;
1370
        /* read from an invalid address */
1371
        v1 = *(char *)0x1234;
1372
    }
1373
    
1374
    /* test illegal instruction reporting */
1375
    printf("UD2 exception:\n");
1376
    if (setjmp(jmp_env) == 0) {
1377
        /* now execute an invalid instruction */
1378
        asm volatile("ud2");
1379
    }
1380
    printf("lock nop exception:\n");
1381
    if (setjmp(jmp_env) == 0) {
1382
        /* now execute an invalid instruction */
1383
        asm volatile("lock nop");
1384
    }
1385
    
1386
    printf("INT exception:\n");
1387
    if (setjmp(jmp_env) == 0) {
1388
        asm volatile ("int $0xfd");
1389
    }
1390
    if (setjmp(jmp_env) == 0) {
1391
        asm volatile ("int $0x01");
1392
    }
1393
    if (setjmp(jmp_env) == 0) {
1394
        asm volatile (".byte 0xcd, 0x03");
1395
    }
1396
    if (setjmp(jmp_env) == 0) {
1397
        asm volatile ("int $0x04");
1398
    }
1399
    if (setjmp(jmp_env) == 0) {
1400
        asm volatile ("int $0x05");
1401
    }
1402

    
1403
    printf("INT3 exception:\n");
1404
    if (setjmp(jmp_env) == 0) {
1405
        asm volatile ("int3");
1406
    }
1407

    
1408
    printf("CLI exception:\n");
1409
    if (setjmp(jmp_env) == 0) {
1410
        asm volatile ("cli");
1411
    }
1412

    
1413
    printf("STI exception:\n");
1414
    if (setjmp(jmp_env) == 0) {
1415
        asm volatile ("cli");
1416
    }
1417

    
1418
    printf("INTO exception:\n");
1419
    if (setjmp(jmp_env) == 0) {
1420
        /* overflow exception */
1421
        asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1422
    }
1423

    
1424
    printf("OUTB exception:\n");
1425
    if (setjmp(jmp_env) == 0) {
1426
        asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1427
    }
1428

    
1429
    printf("INB exception:\n");
1430
    if (setjmp(jmp_env) == 0) {
1431
        asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1432
    }
1433

    
1434
    printf("REP OUTSB exception:\n");
1435
    if (setjmp(jmp_env) == 0) {
1436
        asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1437
    }
1438

    
1439
    printf("REP INSB exception:\n");
1440
    if (setjmp(jmp_env) == 0) {
1441
        asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1442
    }
1443

    
1444
    printf("HLT exception:\n");
1445
    if (setjmp(jmp_env) == 0) {
1446
        asm volatile ("hlt");
1447
    }
1448

    
1449
    printf("single step exception:\n");
1450
    val = 0;
1451
    if (setjmp(jmp_env) == 0) {
1452
        asm volatile ("pushf\n"
1453
                      "orl $0x00100, (%%esp)\n"
1454
                      "popf\n"
1455
                      "movl $0xabcd, %0\n" 
1456
                      "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1457
    }
1458
    printf("val=0x%x\n", val);
1459
}
1460

    
1461
/* specific precise single step test */
1462
void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1463
{
1464
    struct ucontext *uc = puc;
1465
    printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1466
}
1467

    
1468
const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1469
uint8_t sstep_buf2[4];
1470

    
1471
void test_single_step(void)
1472
{
1473
    struct sigaction act;
1474
    volatile int val;
1475
    int i;
1476

    
1477
    val = 0;
1478
    act.sa_sigaction = sig_trap_handler;
1479
    sigemptyset(&act.sa_mask);
1480
    act.sa_flags = SA_SIGINFO;
1481
    sigaction(SIGTRAP, &act, NULL);
1482
    asm volatile ("pushf\n"
1483
                  "orl $0x00100, (%%esp)\n"
1484
                  "popf\n"
1485
                  "movl $0xabcd, %0\n" 
1486

    
1487
                  /* jmp test */
1488
                  "movl $3, %%ecx\n"
1489
                  "1:\n"
1490
                  "addl $1, %0\n"
1491
                  "decl %%ecx\n"
1492
                  "jnz 1b\n"
1493

    
1494
                  /* movsb: the single step should stop at each movsb iteration */
1495
                  "movl $sstep_buf1, %%esi\n"
1496
                  "movl $sstep_buf2, %%edi\n"
1497
                  "movl $0, %%ecx\n"
1498
                  "rep movsb\n"
1499
                  "movl $3, %%ecx\n"
1500
                  "rep movsb\n"
1501
                  "movl $1, %%ecx\n"
1502
                  "rep movsb\n"
1503

    
1504
                  /* cmpsb: the single step should stop at each cmpsb iteration */
1505
                  "movl $sstep_buf1, %%esi\n"
1506
                  "movl $sstep_buf2, %%edi\n"
1507
                  "movl $0, %%ecx\n"
1508
                  "rep cmpsb\n"
1509
                  "movl $4, %%ecx\n"
1510
                  "rep cmpsb\n"
1511
                  
1512
                  /* getpid() syscall: single step should skip one
1513
                     instruction */
1514
                  "movl $20, %%eax\n"
1515
                  "int $0x80\n"
1516
                  "movl $0, %%eax\n"
1517
                  
1518
                  /* when modifying SS, trace is not done on the next
1519
                     instruction */
1520
                  "movl %%ss, %%ecx\n"
1521
                  "movl %%ecx, %%ss\n"
1522
                  "addl $1, %0\n"
1523
                  "movl $1, %%eax\n"
1524
                  "movl %%ecx, %%ss\n"
1525
                  "jmp 1f\n"
1526
                  "addl $1, %0\n"
1527
                  "1:\n"
1528
                  "movl $1, %%eax\n"
1529
                  "pushl %%ecx\n"
1530
                  "popl %%ss\n"
1531
                  "addl $1, %0\n"
1532
                  "movl $1, %%eax\n"
1533
                  
1534
                  "pushf\n"
1535
                  "andl $~0x00100, (%%esp)\n"
1536
                  "popf\n"
1537
                  : "=m" (val) 
1538
                  : 
1539
                  : "cc", "memory", "eax", "ecx", "esi", "edi");
1540
    printf("val=%d\n", val);
1541
    for(i = 0; i < 4; i++)
1542
        printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1543
}
1544

    
1545
/* self modifying code test */
1546
uint8_t code[] = {
1547
    0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1548
    0xc3, /* ret */
1549
};
1550

    
1551
typedef int FuncType(void);
1552

    
1553
void test_self_modifying_code(void)
1554
{
1555
    int i;
1556

    
1557
    printf("self modifying code:\n");
1558
    printf("func1 = 0x%x\n", ((FuncType *)code)());
1559
    for(i = 2; i <= 4; i++) {
1560
        code[1] = i;
1561
        printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1562
    }
1563
}
1564
    
1565
static void *call_end __init_call = NULL;
1566

    
1567
int main(int argc, char **argv)
1568
{
1569
    void **ptr;
1570
    void (*func)(void);
1571

    
1572
    ptr = &call_start + 1;
1573
    while (*ptr != NULL) {
1574
        func = *ptr++;
1575
        func();
1576
    }
1577
    test_bsx();
1578
    test_mul();
1579
    test_jcc();
1580
    test_floats();
1581
    test_bcd();
1582
    test_xchg();
1583
    test_string();
1584
    test_misc();
1585
    test_lea();
1586
    test_segs();
1587
    test_code16();
1588
    test_vm86();
1589
    test_exceptions();
1590
    test_self_modifying_code();
1591
    test_single_step();
1592
    return 0;
1593
}