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