]> git.proxmox.com Git - qemu.git/blob - tests/test-i386.c
86aa94915a393b50831e4fcb4cb8f49eeb184eaa
[qemu.git] / tests / test-i386.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <inttypes.h>
4 #include <math.h>
5
6 #define TEST_CMOV 0
7
8 #define xglue(x, y) x ## y
9 #define glue(x, y) xglue(x, y)
10 #define stringify(s) tostring(s)
11 #define tostring(s) #s
12
13 #define CC_C 0x0001
14 #define CC_P 0x0004
15 #define CC_A 0x0010
16 #define CC_Z 0x0040
17 #define CC_S 0x0080
18 #define CC_O 0x0800
19
20 #define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
21
22 static void *call_start __init_call = NULL;
23
24 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
25
26 #define OP add
27 #include "test-i386.h"
28
29 #define OP sub
30 #include "test-i386.h"
31
32 #define OP xor
33 #include "test-i386.h"
34
35 #define OP and
36 #include "test-i386.h"
37
38 #define OP or
39 #include "test-i386.h"
40
41 #define OP cmp
42 #include "test-i386.h"
43
44 #define OP adc
45 #define OP_CC
46 #include "test-i386.h"
47
48 #define OP sbb
49 #define OP_CC
50 #include "test-i386.h"
51
52 #define OP inc
53 #define OP_CC
54 #define OP1
55 #include "test-i386.h"
56
57 #define OP dec
58 #define OP_CC
59 #define OP1
60 #include "test-i386.h"
61
62 #define OP neg
63 #define OP_CC
64 #define OP1
65 #include "test-i386.h"
66
67 #define OP not
68 #define OP_CC
69 #define OP1
70 #include "test-i386.h"
71
72 #undef CC_MASK
73 #define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
74
75 #define OP shl
76 #include "test-i386-shift.h"
77
78 #define OP shr
79 #include "test-i386-shift.h"
80
81 #define OP sar
82 #include "test-i386-shift.h"
83
84 #define OP rol
85 #include "test-i386-shift.h"
86
87 #define OP ror
88 #include "test-i386-shift.h"
89
90 #define OP rcr
91 #define OP_CC
92 #include "test-i386-shift.h"
93
94 #define OP rcl
95 #define OP_CC
96 #include "test-i386-shift.h"
97
98 #define OP shld
99 #define OP_SHIFTD
100 #define OP_NOBYTE
101 #include "test-i386-shift.h"
102
103 #define OP shrd
104 #define OP_SHIFTD
105 #define OP_NOBYTE
106 #include "test-i386-shift.h"
107
108 /* XXX: should be more precise ? */
109 #undef CC_MASK
110 #define CC_MASK (CC_C)
111
112 #define OP bt
113 #define OP_NOBYTE
114 #include "test-i386-shift.h"
115
116 #define OP bts
117 #define OP_NOBYTE
118 #include "test-i386-shift.h"
119
120 #define OP btr
121 #define OP_NOBYTE
122 #include "test-i386-shift.h"
123
124 #define OP btc
125 #define OP_NOBYTE
126 #include "test-i386-shift.h"
127
128 /* lea test (modrm support) */
129 #define TEST_LEA(STR)\
130 {\
131 asm("leal " STR ", %0"\
132 : "=r" (res)\
133 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
134 printf("lea %s = %08x\n", STR, res);\
135 }
136
137 #define TEST_LEA16(STR)\
138 {\
139 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
140 : "=wq" (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
146 void test_lea(void)
147 {
148 int eax, ebx, ecx, edx, esi, edi, res;
149 eax = 0x0001;
150 ebx = 0x0002;
151 ecx = 0x0004;
152 edx = 0x0008;
153 esi = 0x0010;
154 edi = 0x0020;
155
156 TEST_LEA("0x4000");
157
158 TEST_LEA("(%%eax)");
159 TEST_LEA("(%%ebx)");
160 TEST_LEA("(%%ecx)");
161 TEST_LEA("(%%edx)");
162 TEST_LEA("(%%esi)");
163 TEST_LEA("(%%edi)");
164
165 TEST_LEA("0x40(%%eax)");
166 TEST_LEA("0x40(%%ebx)");
167 TEST_LEA("0x40(%%ecx)");
168 TEST_LEA("0x40(%%edx)");
169 TEST_LEA("0x40(%%esi)");
170 TEST_LEA("0x40(%%edi)");
171
172 TEST_LEA("0x4000(%%eax)");
173 TEST_LEA("0x4000(%%ebx)");
174 TEST_LEA("0x4000(%%ecx)");
175 TEST_LEA("0x4000(%%edx)");
176 TEST_LEA("0x4000(%%esi)");
177 TEST_LEA("0x4000(%%edi)");
178
179 TEST_LEA("(%%eax, %%ecx)");
180 TEST_LEA("(%%ebx, %%edx)");
181 TEST_LEA("(%%ecx, %%ecx)");
182 TEST_LEA("(%%edx, %%ecx)");
183 TEST_LEA("(%%esi, %%ecx)");
184 TEST_LEA("(%%edi, %%ecx)");
185
186 TEST_LEA("0x40(%%eax, %%ecx)");
187 TEST_LEA("0x4000(%%ebx, %%edx)");
188
189 TEST_LEA("(%%ecx, %%ecx, 2)");
190 TEST_LEA("(%%edx, %%ecx, 4)");
191 TEST_LEA("(%%esi, %%ecx, 8)");
192
193 TEST_LEA("(,%%eax, 2)");
194 TEST_LEA("(,%%ebx, 4)");
195 TEST_LEA("(,%%ecx, 8)");
196
197 TEST_LEA("0x40(,%%eax, 2)");
198 TEST_LEA("0x40(,%%ebx, 4)");
199 TEST_LEA("0x40(,%%ecx, 8)");
200
201
202 TEST_LEA("-10(%%ecx, %%ecx, 2)");
203 TEST_LEA("-10(%%edx, %%ecx, 4)");
204 TEST_LEA("-10(%%esi, %%ecx, 8)");
205
206 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
207 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
208 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
209
210 /* limited 16 bit addressing test */
211 TEST_LEA16("0x4000");
212 TEST_LEA16("(%%bx)");
213 TEST_LEA16("(%%si)");
214 TEST_LEA16("(%%di)");
215 TEST_LEA16("0x40(%%bx)");
216 TEST_LEA16("0x40(%%si)");
217 TEST_LEA16("0x40(%%di)");
218 TEST_LEA16("0x4000(%%bx)");
219 TEST_LEA16("0x4000(%%si)");
220 TEST_LEA16("(%%bx,%%si)");
221 TEST_LEA16("(%%bx,%%di)");
222 TEST_LEA16("0x40(%%bx,%%si)");
223 TEST_LEA16("0x40(%%bx,%%di)");
224 TEST_LEA16("0x4000(%%bx,%%si)");
225 TEST_LEA16("0x4000(%%bx,%%di)");
226 }
227
228 #define TEST_JCC(JCC, v1, v2)\
229 {\
230 int res;\
231 asm("movl $1, %0\n\t"\
232 "cmpl %2, %1\n\t"\
233 "j" JCC " 1f\n\t"\
234 "movl $0, %0\n\t"\
235 "1:\n\t"\
236 : "=r" (res)\
237 : "r" (v1), "r" (v2));\
238 printf("%-10s %d\n", "j" JCC, res);\
239 \
240 asm("movl $0, %0\n\t"\
241 "cmpl %2, %1\n\t"\
242 "set" JCC " %b0\n\t"\
243 : "=r" (res)\
244 : "r" (v1), "r" (v2));\
245 printf("%-10s %d\n", "set" JCC, res);\
246 if (TEST_CMOV) {\
247 asm("movl $0x12345678, %0\n\t"\
248 "cmpl %2, %1\n\t"\
249 "cmov" JCC "l %3, %0\n\t"\
250 : "=r" (res)\
251 : "r" (v1), "r" (v2), "m" (1));\
252 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
253 asm("movl $0x12345678, %0\n\t"\
254 "cmpl %2, %1\n\t"\
255 "cmov" JCC "w %w3, %w0\n\t"\
256 : "=r" (res)\
257 : "r" (v1), "r" (v2), "r" (1));\
258 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
259 } \
260 }
261
262 /* various jump tests */
263 void test_jcc(void)
264 {
265 TEST_JCC("ne", 1, 1);
266 TEST_JCC("ne", 1, 0);
267
268 TEST_JCC("e", 1, 1);
269 TEST_JCC("e", 1, 0);
270
271 TEST_JCC("l", 1, 1);
272 TEST_JCC("l", 1, 0);
273 TEST_JCC("l", 1, -1);
274
275 TEST_JCC("le", 1, 1);
276 TEST_JCC("le", 1, 0);
277 TEST_JCC("le", 1, -1);
278
279 TEST_JCC("ge", 1, 1);
280 TEST_JCC("ge", 1, 0);
281 TEST_JCC("ge", -1, 1);
282
283 TEST_JCC("g", 1, 1);
284 TEST_JCC("g", 1, 0);
285 TEST_JCC("g", 1, -1);
286
287 TEST_JCC("b", 1, 1);
288 TEST_JCC("b", 1, 0);
289 TEST_JCC("b", 1, -1);
290
291 TEST_JCC("be", 1, 1);
292 TEST_JCC("be", 1, 0);
293 TEST_JCC("be", 1, -1);
294
295 TEST_JCC("ae", 1, 1);
296 TEST_JCC("ae", 1, 0);
297 TEST_JCC("ae", 1, -1);
298
299 TEST_JCC("a", 1, 1);
300 TEST_JCC("a", 1, 0);
301 TEST_JCC("a", 1, -1);
302
303
304 TEST_JCC("p", 1, 1);
305 TEST_JCC("p", 1, 0);
306
307 TEST_JCC("np", 1, 1);
308 TEST_JCC("np", 1, 0);
309
310 TEST_JCC("o", 0x7fffffff, 0);
311 TEST_JCC("o", 0x7fffffff, -1);
312
313 TEST_JCC("no", 0x7fffffff, 0);
314 TEST_JCC("no", 0x7fffffff, -1);
315
316 TEST_JCC("s", 0, 1);
317 TEST_JCC("s", 0, -1);
318 TEST_JCC("s", 0, 0);
319
320 TEST_JCC("ns", 0, 1);
321 TEST_JCC("ns", 0, -1);
322 TEST_JCC("ns", 0, 0);
323 }
324
325 #undef CC_MASK
326 #define CC_MASK (CC_O | CC_C)
327
328 #define OP mul
329 #include "test-i386-muldiv.h"
330
331 #define OP imul
332 #include "test-i386-muldiv.h"
333
334 #undef CC_MASK
335 #define CC_MASK (0)
336
337 #define OP div
338 #include "test-i386-muldiv.h"
339
340 #define OP idiv
341 #include "test-i386-muldiv.h"
342
343 void test_imulw2(int op0, int op1)
344 {
345 int res, s1, s0, flags;
346 s0 = op0;
347 s1 = op1;
348 res = s0;
349 flags = 0;
350 asm ("push %4\n\t"
351 "popf\n\t"
352 "imulw %w2, %w0\n\t"
353 "pushf\n\t"
354 "popl %1\n\t"
355 : "=q" (res), "=g" (flags)
356 : "q" (s1), "0" (res), "1" (flags));
357 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
358 "imulw", s0, s1, res, flags & CC_MASK);
359 }
360
361 void test_imull2(int op0, int op1)
362 {
363 int res, s1, s0, flags;
364 s0 = op0;
365 s1 = op1;
366 res = s0;
367 flags = 0;
368 asm ("push %4\n\t"
369 "popf\n\t"
370 "imull %2, %0\n\t"
371 "pushf\n\t"
372 "popl %1\n\t"
373 : "=q" (res), "=g" (flags)
374 : "q" (s1), "0" (res), "1" (flags));
375 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
376 "imull", s0, s1, res, flags & CC_MASK);
377 }
378
379 void test_mul(void)
380 {
381 test_imulb(0x1234561d, 4);
382 test_imulb(3, -4);
383 test_imulb(0x80, 0x80);
384 test_imulb(0x10, 0x10);
385
386 test_imulw(0, 0x1234001d, 45);
387 test_imulw(0, 23, -45);
388 test_imulw(0, 0x8000, 0x8000);
389 test_imulw(0, 0x100, 0x100);
390
391 test_imull(0, 0x1234001d, 45);
392 test_imull(0, 23, -45);
393 test_imull(0, 0x80000000, 0x80000000);
394 test_imull(0, 0x10000, 0x10000);
395
396 test_mulb(0x1234561d, 4);
397 test_mulb(3, -4);
398 test_mulb(0x80, 0x80);
399 test_mulb(0x10, 0x10);
400
401 test_mulw(0, 0x1234001d, 45);
402 test_mulw(0, 23, -45);
403 test_mulw(0, 0x8000, 0x8000);
404 test_mulw(0, 0x100, 0x100);
405
406 test_mull(0, 0x1234001d, 45);
407 test_mull(0, 23, -45);
408 test_mull(0, 0x80000000, 0x80000000);
409 test_mull(0, 0x10000, 0x10000);
410
411 test_imulw2(0x1234001d, 45);
412 test_imulw2(23, -45);
413 test_imulw2(0x8000, 0x8000);
414 test_imulw2(0x100, 0x100);
415
416 test_imull2(0x1234001d, 45);
417 test_imull2(23, -45);
418 test_imull2(0x80000000, 0x80000000);
419 test_imull2(0x10000, 0x10000);
420
421 test_idivb(0x12341678, 0x127e);
422 test_idivb(0x43210123, -5);
423 test_idivb(0x12340004, -1);
424
425 test_idivw(0, 0x12345678, 12347);
426 test_idivw(0, -23223, -45);
427 test_idivw(0, 0x12348000, -1);
428 test_idivw(0x12343, 0x12345678, 0x81238567);
429
430 test_idivl(0, 0x12345678, 12347);
431 test_idivl(0, -233223, -45);
432 test_idivl(0, 0x80000000, -1);
433 test_idivl(0x12343, 0x12345678, 0x81234567);
434
435 test_divb(0x12341678, 0x127e);
436 test_divb(0x43210123, -5);
437 test_divb(0x12340004, -1);
438
439 test_divw(0, 0x12345678, 12347);
440 test_divw(0, -23223, -45);
441 test_divw(0, 0x12348000, -1);
442 test_divw(0x12343, 0x12345678, 0x81238567);
443
444 test_divl(0, 0x12345678, 12347);
445 test_divl(0, -233223, -45);
446 test_divl(0, 0x80000000, -1);
447 test_divl(0x12343, 0x12345678, 0x81234567);
448 }
449
450 #define TEST_BSX(op, size, op0)\
451 {\
452 int res, val, resz;\
453 val = op0;\
454 asm("xorl %1, %1 ; " #op " %" size "2, %" size "0 ; setz %b1" \
455 : "=r" (res), "=q" (resz)\
456 : "g" (val));\
457 printf("%-10s A=%08x R=%08x %d\n", #op, val, resz ? 0 : res, resz);\
458 }
459
460 void test_bsx(void)
461 {
462 TEST_BSX(bsrw, "w", 0);
463 TEST_BSX(bsrw, "w", 0x12340128);
464 TEST_BSX(bsrl, "", 0);
465 TEST_BSX(bsrl, "", 0x00340128);
466 TEST_BSX(bsfw, "w", 0);
467 TEST_BSX(bsfw, "w", 0x12340128);
468 TEST_BSX(bsfl, "", 0);
469 TEST_BSX(bsfl, "", 0x00340128);
470 }
471
472 /**********************************************/
473
474 void test_fops(double a, double b)
475 {
476 printf("a=%f b=%f a+b=%f\n", a, b, a + b);
477 printf("a=%f b=%f a-b=%f\n", a, b, a - b);
478 printf("a=%f b=%f a*b=%f\n", a, b, a * b);
479 printf("a=%f b=%f a/b=%f\n", a, b, a / b);
480 printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
481 printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
482 printf("a=%f sin(a)=%f\n", a, sin(a));
483 printf("a=%f cos(a)=%f\n", a, cos(a));
484 printf("a=%f tan(a)=%f\n", a, tan(a));
485 printf("a=%f log(a)=%f\n", a, log(a));
486 printf("a=%f exp(a)=%f\n", a, exp(a));
487 printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
488 /* just to test some op combining */
489 printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
490 printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
491 printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
492
493 }
494
495 void test_fcmp(double a, double b)
496 {
497 printf("(%f<%f)=%d\n",
498 a, b, a < b);
499 printf("(%f<=%f)=%d\n",
500 a, b, a <= b);
501 printf("(%f==%f)=%d\n",
502 a, b, a == b);
503 printf("(%f>%f)=%d\n",
504 a, b, a > b);
505 printf("(%f<=%f)=%d\n",
506 a, b, a >= b);
507 }
508
509 void test_fcvt(double a)
510 {
511 float fa;
512 long double la;
513
514 fa = a;
515 la = a;
516 printf("(float)%f = %f\n", a, fa);
517 printf("(long double)%f = %Lf\n", a, la);
518 printf("a=%016Lx\n", *(long long *)&a);
519 printf("la=%016Lx %04x\n", *(long long *)&la,
520 *(unsigned short *)((char *)(&la) + 8));
521 printf("a=%f floor(a)=%f\n", a, floor(a));
522 printf("a=%f ceil(a)=%f\n", a, ceil(a));
523 printf("a=%f rint(a)=%f\n", a, rint(a));
524 }
525
526 #define TEST(N) \
527 asm("fld" #N : "=t" (a)); \
528 printf("fld" #N "= %f\n", a);
529
530 void test_fconst(void)
531 {
532 double a;
533 TEST(1);
534 TEST(l2t);
535 TEST(l2e);
536 TEST(pi);
537 TEST(lg2);
538 TEST(ln2);
539 TEST(z);
540 }
541
542 void test_fbcd(double a)
543 {
544 unsigned short bcd[5];
545 double b;
546
547 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
548 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
549 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
550 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
551 }
552
553 void test_floats(void)
554 {
555 test_fops(2, 3);
556 test_fops(1.4, -5);
557 test_fcmp(2, -1);
558 test_fcmp(2, 2);
559 test_fcmp(2, 3);
560 test_fcvt(1.0/7.0);
561 test_fcvt(-1.0/9.0);
562 test_fcvt(1e30);
563 test_fconst();
564 test_fbcd(1234567890123456);
565 test_fbcd(-123451234567890);
566 }
567
568 /**********************************************/
569
570 #define TEST_BCD(op, op0, cc_in, cc_mask)\
571 {\
572 int res, flags;\
573 res = op0;\
574 flags = cc_in;\
575 asm ("push %3\n\t"\
576 "popf\n\t"\
577 #op "\n\t"\
578 "pushf\n\t"\
579 "popl %1\n\t"\
580 : "=a" (res), "=g" (flags)\
581 : "0" (res), "1" (flags));\
582 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
583 #op, op0, res, cc_in, flags & cc_mask);\
584 }
585
586 void test_bcd(void)
587 {
588 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
589 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
590 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
591 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
592 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
593 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
594 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
595 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
596 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
597 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
598 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
599 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
600 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
601
602 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
603 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
604 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
605 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
606 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
607 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
608 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
609 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
610 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
611 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
612 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
613 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
614 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
615
616 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
617 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
618 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
619 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
620 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
621 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
622 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
623 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
624
625 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
626 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
627 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
628 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
629 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
630 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
631 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
632 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
633
634 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
635 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
636 }
637
638 /**********************************************/
639 /* segmentation tests */
640
641 #include <asm/ldt.h>
642 #include <linux/unistd.h>
643
644 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
645
646 uint8_t seg_data1[4096];
647 uint8_t seg_data2[4096];
648
649 #define MK_SEL(n) (((n) << 3) | 4)
650
651 /* NOTE: we use Linux modify_ldt syscall */
652 void test_segs(void)
653 {
654 struct modify_ldt_ldt_s ldt;
655 long long ldt_table[3];
656 int i, res, res2;
657 char tmp;
658
659 ldt.entry_number = 1;
660 ldt.base_addr = (unsigned long)&seg_data1;
661 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
662 ldt.seg_32bit = 1;
663 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
664 ldt.read_exec_only = 0;
665 ldt.limit_in_pages = 1;
666 ldt.seg_not_present = 0;
667 ldt.useable = 1;
668 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
669
670 ldt.entry_number = 2;
671 ldt.base_addr = (unsigned long)&seg_data2;
672 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
673 ldt.seg_32bit = 1;
674 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
675 ldt.read_exec_only = 0;
676 ldt.limit_in_pages = 1;
677 ldt.seg_not_present = 0;
678 ldt.useable = 1;
679 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
680
681 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
682 for(i=0;i<3;i++)
683 printf("%d: %016Lx\n", i, ldt_table[i]);
684
685 /* do some tests with fs or gs */
686 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
687 asm volatile ("movl %0, %%gs" : : "r" (MK_SEL(2)));
688
689 seg_data1[1] = 0xaa;
690 seg_data2[1] = 0x55;
691
692 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
693 printf("FS[1] = %02x\n", res);
694
695 asm volatile ("gs movzbl 0x1, %0" : "=r" (res));
696 printf("GS[1] = %02x\n", res);
697
698 /* tests with ds/ss (implicit segment case) */
699 tmp = 0xa5;
700 asm volatile ("pushl %%ebp\n\t"
701 "pushl %%ds\n\t"
702 "movl %2, %%ds\n\t"
703 "movl %3, %%ebp\n\t"
704 "movzbl 0x1, %0\n\t"
705 "movzbl (%%ebp), %1\n\t"
706 "popl %%ds\n\t"
707 "popl %%ebp\n\t"
708 : "=r" (res), "=r" (res2)
709 : "r" (MK_SEL(1)), "r" (&tmp));
710 printf("DS[1] = %02x\n", res);
711 printf("SS[tmp] = %02x\n", res2);
712 }
713
714 #define TEST_XCHG(op, size, opconst)\
715 {\
716 int op0, op1;\
717 op0 = 0x12345678;\
718 op1 = 0xfbca7654;\
719 asm(#op " %" size "0, %" size "1" \
720 : "=q" (op0), opconst (op1) \
721 : "0" (op0), "1" (op1));\
722 printf("%-10s A=%08x B=%08x\n",\
723 #op, op0, op1);\
724 }
725
726 #define TEST_CMPXCHG(op, size, opconst, eax)\
727 {\
728 int op0, op1;\
729 op0 = 0x12345678;\
730 op1 = 0xfbca7654;\
731 asm(#op " %" size "0, %" size "1" \
732 : "=q" (op0), opconst (op1) \
733 : "0" (op0), "1" (op1), "a" (eax));\
734 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
735 #op, eax, op0, op1);\
736 }
737
738 void test_xchg(void)
739 {
740 TEST_XCHG(xchgl, "", "=q");
741 TEST_XCHG(xchgw, "w", "=q");
742 TEST_XCHG(xchgb, "b", "=q");
743
744 TEST_XCHG(xchgl, "", "=m");
745 TEST_XCHG(xchgw, "w", "=m");
746 TEST_XCHG(xchgb, "b", "=m");
747
748 TEST_XCHG(xaddl, "", "=q");
749 TEST_XCHG(xaddw, "w", "=q");
750 TEST_XCHG(xaddb, "b", "=q");
751
752 TEST_XCHG(xaddl, "", "=m");
753 TEST_XCHG(xaddw, "w", "=m");
754 TEST_XCHG(xaddb, "b", "=m");
755
756 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
757 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
758 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
759
760 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
761 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
762 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
763
764 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
765 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
766 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
767
768 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
769 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
770 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
771 }
772
773 static void *call_end __init_call = NULL;
774
775 int main(int argc, char **argv)
776 {
777 void **ptr;
778 void (*func)(void);
779
780 ptr = &call_start + 1;
781 while (*ptr != NULL) {
782 func = *ptr++;
783 func();
784 }
785 test_bsx();
786 test_mul();
787 test_jcc();
788 test_floats();
789 test_bcd();
790 test_xchg();
791 test_lea();
792 test_segs();
793 return 0;
794 }