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