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