]> git.proxmox.com Git - qemu.git/blame - tests/test-i386.c
sse fix
[qemu.git] / tests / test-i386.c
CommitLineData
78d6da97
FB
1/*
2 * x86 CPU test
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
3a27ad0b 20#define _GNU_SOURCE
4d1135e4
FB
21#include <stdlib.h>
22#include <stdio.h>
e3e86d56 23#include <string.h>
6dbad63e 24#include <inttypes.h>
4d1135e4 25#include <math.h>
3a27ad0b
FB
26#include <signal.h>
27#include <setjmp.h>
070893f4 28#include <errno.h>
3a27ad0b
FB
29#include <sys/ucontext.h>
30#include <sys/mman.h>
31#include <asm/vm86.h>
4d1135e4 32
03bfca94
FB
33#define TEST_CMOV 0
34#define TEST_FCOMI 0
acae4681 35#define TEST_VM86
3ff0631e 36//#define LINUX_VM86_IOPL_FIX
791c2261 37//#define TEST_P4_FLAGS
085339a1 38//#define TEST_SSE
5dd9488c 39
4d1135e4
FB
40#define xglue(x, y) x ## y
41#define glue(x, y) xglue(x, y)
42#define stringify(s) tostring(s)
43#define tostring(s) #s
44
45#define CC_C 0x0001
46#define CC_P 0x0004
47#define CC_A 0x0010
48#define CC_Z 0x0040
49#define CC_S 0x0080
50#define CC_O 0x0800
51
4d1135e4
FB
52#define __init_call __attribute__ ((unused,__section__ (".initcall.init")))
53
54static void *call_start __init_call = NULL;
55
4b74fe1f
FB
56#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
57
4d1135e4
FB
58#define OP add
59#include "test-i386.h"
60
61#define OP sub
62#include "test-i386.h"
63
64#define OP xor
65#include "test-i386.h"
66
67#define OP and
68#include "test-i386.h"
69
70#define OP or
71#include "test-i386.h"
72
73#define OP cmp
74#include "test-i386.h"
75
76#define OP adc
77#define OP_CC
78#include "test-i386.h"
79
80#define OP sbb
81#define OP_CC
82#include "test-i386.h"
83
84#define OP inc
85#define OP_CC
86#define OP1
87#include "test-i386.h"
88
89#define OP dec
90#define OP_CC
91#define OP1
92#include "test-i386.h"
93
94#define OP neg
95#define OP_CC
96#define OP1
97#include "test-i386.h"
98
99#define OP not
100#define OP_CC
101#define OP1
102#include "test-i386.h"
103
4b74fe1f
FB
104#undef CC_MASK
105#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
106
379ca80d
FB
107#define OP shl
108#include "test-i386-shift.h"
109
110#define OP shr
111#include "test-i386-shift.h"
112
113#define OP sar
114#include "test-i386-shift.h"
115
116#define OP rol
117#include "test-i386-shift.h"
118
119#define OP ror
120#include "test-i386-shift.h"
121
122#define OP rcr
123#define OP_CC
124#include "test-i386-shift.h"
125
126#define OP rcl
127#define OP_CC
128#include "test-i386-shift.h"
129
d57c4e01
FB
130#define OP shld
131#define OP_SHIFTD
132#define OP_NOBYTE
133#include "test-i386-shift.h"
134
135#define OP shrd
136#define OP_SHIFTD
137#define OP_NOBYTE
138#include "test-i386-shift.h"
139
140/* XXX: should be more precise ? */
141#undef CC_MASK
142#define CC_MASK (CC_C)
143
144#define OP bt
145#define OP_NOBYTE
146#include "test-i386-shift.h"
147
148#define OP bts
149#define OP_NOBYTE
150#include "test-i386-shift.h"
151
152#define OP btr
153#define OP_NOBYTE
154#include "test-i386-shift.h"
155
156#define OP btc
157#define OP_NOBYTE
158#include "test-i386-shift.h"
379ca80d 159
4d1135e4
FB
160/* lea test (modrm support) */
161#define TEST_LEA(STR)\
162{\
163 asm("leal " STR ", %0"\
164 : "=r" (res)\
165 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
166 printf("lea %s = %08x\n", STR, res);\
167}
168
169#define TEST_LEA16(STR)\
170{\
171 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
172 : "=wq" (res)\
173 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
174 printf("lea %s = %08x\n", STR, res);\
175}
176
177
178void test_lea(void)
179{
180 int eax, ebx, ecx, edx, esi, edi, res;
181 eax = 0x0001;
182 ebx = 0x0002;
183 ecx = 0x0004;
184 edx = 0x0008;
185 esi = 0x0010;
186 edi = 0x0020;
187
188 TEST_LEA("0x4000");
189
190 TEST_LEA("(%%eax)");
191 TEST_LEA("(%%ebx)");
192 TEST_LEA("(%%ecx)");
193 TEST_LEA("(%%edx)");
194 TEST_LEA("(%%esi)");
195 TEST_LEA("(%%edi)");
196
197 TEST_LEA("0x40(%%eax)");
198 TEST_LEA("0x40(%%ebx)");
199 TEST_LEA("0x40(%%ecx)");
200 TEST_LEA("0x40(%%edx)");
201 TEST_LEA("0x40(%%esi)");
202 TEST_LEA("0x40(%%edi)");
203
204 TEST_LEA("0x4000(%%eax)");
205 TEST_LEA("0x4000(%%ebx)");
206 TEST_LEA("0x4000(%%ecx)");
207 TEST_LEA("0x4000(%%edx)");
208 TEST_LEA("0x4000(%%esi)");
209 TEST_LEA("0x4000(%%edi)");
210
211 TEST_LEA("(%%eax, %%ecx)");
212 TEST_LEA("(%%ebx, %%edx)");
213 TEST_LEA("(%%ecx, %%ecx)");
214 TEST_LEA("(%%edx, %%ecx)");
215 TEST_LEA("(%%esi, %%ecx)");
216 TEST_LEA("(%%edi, %%ecx)");
217
218 TEST_LEA("0x40(%%eax, %%ecx)");
219 TEST_LEA("0x4000(%%ebx, %%edx)");
220
221 TEST_LEA("(%%ecx, %%ecx, 2)");
222 TEST_LEA("(%%edx, %%ecx, 4)");
223 TEST_LEA("(%%esi, %%ecx, 8)");
224
225 TEST_LEA("(,%%eax, 2)");
226 TEST_LEA("(,%%ebx, 4)");
227 TEST_LEA("(,%%ecx, 8)");
228
229 TEST_LEA("0x40(,%%eax, 2)");
230 TEST_LEA("0x40(,%%ebx, 4)");
231 TEST_LEA("0x40(,%%ecx, 8)");
232
233
234 TEST_LEA("-10(%%ecx, %%ecx, 2)");
235 TEST_LEA("-10(%%edx, %%ecx, 4)");
236 TEST_LEA("-10(%%esi, %%ecx, 8)");
237
238 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
239 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
240 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
241
242 /* limited 16 bit addressing test */
243 TEST_LEA16("0x4000");
244 TEST_LEA16("(%%bx)");
245 TEST_LEA16("(%%si)");
246 TEST_LEA16("(%%di)");
247 TEST_LEA16("0x40(%%bx)");
248 TEST_LEA16("0x40(%%si)");
249 TEST_LEA16("0x40(%%di)");
250 TEST_LEA16("0x4000(%%bx)");
251 TEST_LEA16("0x4000(%%si)");
252 TEST_LEA16("(%%bx,%%si)");
253 TEST_LEA16("(%%bx,%%di)");
254 TEST_LEA16("0x40(%%bx,%%si)");
255 TEST_LEA16("0x40(%%bx,%%di)");
256 TEST_LEA16("0x4000(%%bx,%%si)");
257 TEST_LEA16("0x4000(%%bx,%%di)");
258}
259
260#define TEST_JCC(JCC, v1, v2)\
261{\
5dd9488c 262 int res;\
4d1135e4
FB
263 asm("movl $1, %0\n\t"\
264 "cmpl %2, %1\n\t"\
5dd9488c 265 "j" JCC " 1f\n\t"\
4d1135e4
FB
266 "movl $0, %0\n\t"\
267 "1:\n\t"\
268 : "=r" (res)\
269 : "r" (v1), "r" (v2));\
5dd9488c
FB
270 printf("%-10s %d\n", "j" JCC, res);\
271\
272 asm("movl $0, %0\n\t"\
273 "cmpl %2, %1\n\t"\
274 "set" JCC " %b0\n\t"\
275 : "=r" (res)\
276 : "r" (v1), "r" (v2));\
277 printf("%-10s %d\n", "set" JCC, res);\
278 if (TEST_CMOV) {\
279 asm("movl $0x12345678, %0\n\t"\
280 "cmpl %2, %1\n\t"\
281 "cmov" JCC "l %3, %0\n\t"\
282 : "=r" (res)\
283 : "r" (v1), "r" (v2), "m" (1));\
284 printf("%-10s R=0x%08x\n", "cmov" JCC "l", res);\
285 asm("movl $0x12345678, %0\n\t"\
286 "cmpl %2, %1\n\t"\
287 "cmov" JCC "w %w3, %w0\n\t"\
288 : "=r" (res)\
289 : "r" (v1), "r" (v2), "r" (1));\
290 printf("%-10s R=0x%08x\n", "cmov" JCC "w", res);\
291 } \
4d1135e4
FB
292}
293
294/* various jump tests */
295void test_jcc(void)
296{
5dd9488c
FB
297 TEST_JCC("ne", 1, 1);
298 TEST_JCC("ne", 1, 0);
4d1135e4 299
5dd9488c
FB
300 TEST_JCC("e", 1, 1);
301 TEST_JCC("e", 1, 0);
4d1135e4 302
5dd9488c
FB
303 TEST_JCC("l", 1, 1);
304 TEST_JCC("l", 1, 0);
305 TEST_JCC("l", 1, -1);
4d1135e4 306
5dd9488c
FB
307 TEST_JCC("le", 1, 1);
308 TEST_JCC("le", 1, 0);
309 TEST_JCC("le", 1, -1);
4d1135e4 310
5dd9488c
FB
311 TEST_JCC("ge", 1, 1);
312 TEST_JCC("ge", 1, 0);
313 TEST_JCC("ge", -1, 1);
4d1135e4 314
5dd9488c
FB
315 TEST_JCC("g", 1, 1);
316 TEST_JCC("g", 1, 0);
317 TEST_JCC("g", 1, -1);
4d1135e4 318
5dd9488c
FB
319 TEST_JCC("b", 1, 1);
320 TEST_JCC("b", 1, 0);
321 TEST_JCC("b", 1, -1);
4d1135e4 322
5dd9488c
FB
323 TEST_JCC("be", 1, 1);
324 TEST_JCC("be", 1, 0);
325 TEST_JCC("be", 1, -1);
4d1135e4 326
5dd9488c
FB
327 TEST_JCC("ae", 1, 1);
328 TEST_JCC("ae", 1, 0);
329 TEST_JCC("ae", 1, -1);
4d1135e4 330
5dd9488c
FB
331 TEST_JCC("a", 1, 1);
332 TEST_JCC("a", 1, 0);
333 TEST_JCC("a", 1, -1);
4d1135e4
FB
334
335
5dd9488c
FB
336 TEST_JCC("p", 1, 1);
337 TEST_JCC("p", 1, 0);
4d1135e4 338
5dd9488c
FB
339 TEST_JCC("np", 1, 1);
340 TEST_JCC("np", 1, 0);
4d1135e4 341
5dd9488c
FB
342 TEST_JCC("o", 0x7fffffff, 0);
343 TEST_JCC("o", 0x7fffffff, -1);
4d1135e4 344
5dd9488c
FB
345 TEST_JCC("no", 0x7fffffff, 0);
346 TEST_JCC("no", 0x7fffffff, -1);
4d1135e4 347
5dd9488c
FB
348 TEST_JCC("s", 0, 1);
349 TEST_JCC("s", 0, -1);
350 TEST_JCC("s", 0, 0);
4d1135e4 351
5dd9488c
FB
352 TEST_JCC("ns", 0, 1);
353 TEST_JCC("ns", 0, -1);
354 TEST_JCC("ns", 0, 0);
4d1135e4
FB
355}
356
4b74fe1f 357#undef CC_MASK
791c2261
FB
358#ifdef TEST_P4_FLAGS
359#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
360#else
4b74fe1f 361#define CC_MASK (CC_O | CC_C)
791c2261 362#endif
4b74fe1f
FB
363
364#define OP mul
365#include "test-i386-muldiv.h"
366
367#define OP imul
368#include "test-i386-muldiv.h"
369
4b74fe1f
FB
370void test_imulw2(int op0, int op1)
371{
372 int res, s1, s0, flags;
373 s0 = op0;
374 s1 = op1;
375 res = s0;
376 flags = 0;
acae4681 377 asm volatile ("push %4\n\t"
4b74fe1f
FB
378 "popf\n\t"
379 "imulw %w2, %w0\n\t"
380 "pushf\n\t"
381 "popl %1\n\t"
382 : "=q" (res), "=g" (flags)
383 : "q" (s1), "0" (res), "1" (flags));
384 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
385 "imulw", s0, s1, res, flags & CC_MASK);
386}
387
388void test_imull2(int op0, int op1)
389{
390 int res, s1, s0, flags;
391 s0 = op0;
392 s1 = op1;
393 res = s0;
394 flags = 0;
acae4681 395 asm volatile ("push %4\n\t"
4b74fe1f
FB
396 "popf\n\t"
397 "imull %2, %0\n\t"
398 "pushf\n\t"
399 "popl %1\n\t"
400 : "=q" (res), "=g" (flags)
401 : "q" (s1), "0" (res), "1" (flags));
402 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",
403 "imull", s0, s1, res, flags & CC_MASK);
404}
405
b5075d29
FB
406#define TEST_IMUL_IM(size, size1, op0, op1)\
407{\
408 int res, flags;\
409 flags = 0;\
410 res = 0;\
acae4681 411 asm volatile ("push %3\n\t"\
b5075d29
FB
412 "popf\n\t"\
413 "imul" size " $" #op0 ", %" size1 "2, %" size1 "0\n\t" \
414 "pushf\n\t"\
415 "popl %1\n\t"\
416 : "=r" (res), "=g" (flags)\
417 : "r" (op1), "1" (flags), "0" (res));\
418 printf("%-10s A=%08x B=%08x R=%08x CC=%04x\n",\
acae4681 419 "imul" size " im", op0, op1, res, flags & CC_MASK);\
b5075d29
FB
420}
421
422
791c2261
FB
423#undef CC_MASK
424#define CC_MASK (0)
425
426#define OP div
427#include "test-i386-muldiv.h"
428
429#define OP idiv
430#include "test-i386-muldiv.h"
431
4b74fe1f
FB
432void test_mul(void)
433{
434 test_imulb(0x1234561d, 4);
435 test_imulb(3, -4);
436 test_imulb(0x80, 0x80);
437 test_imulb(0x10, 0x10);
438
439 test_imulw(0, 0x1234001d, 45);
440 test_imulw(0, 23, -45);
441 test_imulw(0, 0x8000, 0x8000);
442 test_imulw(0, 0x100, 0x100);
443
444 test_imull(0, 0x1234001d, 45);
445 test_imull(0, 23, -45);
446 test_imull(0, 0x80000000, 0x80000000);
447 test_imull(0, 0x10000, 0x10000);
448
449 test_mulb(0x1234561d, 4);
450 test_mulb(3, -4);
451 test_mulb(0x80, 0x80);
452 test_mulb(0x10, 0x10);
453
454 test_mulw(0, 0x1234001d, 45);
455 test_mulw(0, 23, -45);
456 test_mulw(0, 0x8000, 0x8000);
457 test_mulw(0, 0x100, 0x100);
458
459 test_mull(0, 0x1234001d, 45);
460 test_mull(0, 23, -45);
461 test_mull(0, 0x80000000, 0x80000000);
462 test_mull(0, 0x10000, 0x10000);
463
464 test_imulw2(0x1234001d, 45);
465 test_imulw2(23, -45);
466 test_imulw2(0x8000, 0x8000);
467 test_imulw2(0x100, 0x100);
468
469 test_imull2(0x1234001d, 45);
470 test_imull2(23, -45);
471 test_imull2(0x80000000, 0x80000000);
472 test_imull2(0x10000, 0x10000);
473
b5075d29
FB
474 TEST_IMUL_IM("w", "w", 45, 0x1234);
475 TEST_IMUL_IM("w", "w", -45, 23);
476 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
477 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
478
479 TEST_IMUL_IM("l", "", 45, 0x1234);
480 TEST_IMUL_IM("l", "", -45, 23);
481 TEST_IMUL_IM("l", "", 0x8000, 0x80000000);
482 TEST_IMUL_IM("l", "", 0x7fff, 0x1000);
483
4b74fe1f
FB
484 test_idivb(0x12341678, 0x127e);
485 test_idivb(0x43210123, -5);
486 test_idivb(0x12340004, -1);
487
488 test_idivw(0, 0x12345678, 12347);
489 test_idivw(0, -23223, -45);
490 test_idivw(0, 0x12348000, -1);
491 test_idivw(0x12343, 0x12345678, 0x81238567);
492
493 test_idivl(0, 0x12345678, 12347);
494 test_idivl(0, -233223, -45);
495 test_idivl(0, 0x80000000, -1);
496 test_idivl(0x12343, 0x12345678, 0x81234567);
497
498 test_divb(0x12341678, 0x127e);
499 test_divb(0x43210123, -5);
500 test_divb(0x12340004, -1);
501
502 test_divw(0, 0x12345678, 12347);
503 test_divw(0, -23223, -45);
504 test_divw(0, 0x12348000, -1);
505 test_divw(0x12343, 0x12345678, 0x81238567);
506
507 test_divl(0, 0x12345678, 12347);
508 test_divl(0, -233223, -45);
509 test_divl(0, 0x80000000, -1);
510 test_divl(0x12343, 0x12345678, 0x81234567);
511}
512
9d8e9c09
FB
513#define TEST_BSX(op, size, op0)\
514{\
515 int res, val, resz;\
516 val = op0;\
7f5e1452
FB
517 asm("xorl %1, %1\n"\
518 "movl $0x12345678, %0\n"\
519 #op " %" size "2, %" size "0 ; setz %b1" \
9d8e9c09
FB
520 : "=r" (res), "=q" (resz)\
521 : "g" (val));\
7f5e1452 522 printf("%-10s A=%08x R=%08x %d\n", #op, val, res, resz);\
9d8e9c09
FB
523}
524
525void test_bsx(void)
526{
527 TEST_BSX(bsrw, "w", 0);
528 TEST_BSX(bsrw, "w", 0x12340128);
529 TEST_BSX(bsrl, "", 0);
530 TEST_BSX(bsrl, "", 0x00340128);
531 TEST_BSX(bsfw, "w", 0);
532 TEST_BSX(bsfw, "w", 0x12340128);
533 TEST_BSX(bsfl, "", 0);
534 TEST_BSX(bsfl, "", 0x00340128);
535}
536
55480af8
FB
537/**********************************************/
538
9d8e9c09
FB
539void test_fops(double a, double b)
540{
541 printf("a=%f b=%f a+b=%f\n", a, b, a + b);
542 printf("a=%f b=%f a-b=%f\n", a, b, a - b);
543 printf("a=%f b=%f a*b=%f\n", a, b, a * b);
544 printf("a=%f b=%f a/b=%f\n", a, b, a / b);
545 printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
546 printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
547 printf("a=%f sin(a)=%f\n", a, sin(a));
548 printf("a=%f cos(a)=%f\n", a, cos(a));
549 printf("a=%f tan(a)=%f\n", a, tan(a));
550 printf("a=%f log(a)=%f\n", a, log(a));
551 printf("a=%f exp(a)=%f\n", a, exp(a));
552 printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
553 /* just to test some op combining */
554 printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
555 printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
556 printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
557
558}
559
560void test_fcmp(double a, double b)
561{
562 printf("(%f<%f)=%d\n",
563 a, b, a < b);
564 printf("(%f<=%f)=%d\n",
565 a, b, a <= b);
566 printf("(%f==%f)=%d\n",
567 a, b, a == b);
568 printf("(%f>%f)=%d\n",
569 a, b, a > b);
570 printf("(%f<=%f)=%d\n",
571 a, b, a >= b);
03bfca94
FB
572 if (TEST_FCOMI) {
573 unsigned int eflags;
574 /* test f(u)comi instruction */
575 asm("fcomi %2, %1\n"
576 "pushf\n"
577 "pop %0\n"
578 : "=r" (eflags)
579 : "t" (a), "u" (b));
580 printf("fcomi(%f %f)=%08x\n", a, b, eflags & (CC_Z | CC_P | CC_C));
581 }
9d8e9c09
FB
582}
583
584void test_fcvt(double a)
585{
586 float fa;
587 long double la;
ea768640
FB
588 int16_t fpuc;
589 int i;
590 int64_t lla;
591 int ia;
592 int16_t wa;
593 double ra;
9d8e9c09
FB
594
595 fa = a;
596 la = a;
597 printf("(float)%f = %f\n", a, fa);
598 printf("(long double)%f = %Lf\n", a, la);
c5e9815d
FB
599 printf("a=%016Lx\n", *(long long *)&a);
600 printf("la=%016Lx %04x\n", *(long long *)&la,
601 *(unsigned short *)((char *)(&la) + 8));
ea768640
FB
602
603 /* test all roundings */
604 asm volatile ("fstcw %0" : "=m" (fpuc));
605 for(i=0;i<4;i++) {
606 asm volatile ("fldcw %0" : : "m" ((fpuc & ~0x0c00) | (i << 10)));
607 asm volatile ("fist %0" : "=m" (wa) : "t" (a));
608 asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
609 asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
610 asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
611 asm volatile ("fldcw %0" : : "m" (fpuc));
612 printf("(short)a = %d\n", wa);
613 printf("(int)a = %d\n", ia);
614 printf("(int64_t)a = %Ld\n", lla);
615 printf("rint(a) = %f\n", ra);
616 }
9d8e9c09
FB
617}
618
619#define TEST(N) \
620 asm("fld" #N : "=t" (a)); \
621 printf("fld" #N "= %f\n", a);
622
623void test_fconst(void)
624{
625 double a;
626 TEST(1);
627 TEST(l2t);
628 TEST(l2e);
629 TEST(pi);
630 TEST(lg2);
631 TEST(ln2);
632 TEST(z);
633}
634
c5e9815d
FB
635void test_fbcd(double a)
636{
637 unsigned short bcd[5];
638 double b;
639
640 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
641 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
642 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
643 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
644}
645
6b2b6112 646#define TEST_ENV(env, save, restore)\
03bfca94
FB
647{\
648 memset((env), 0xaa, sizeof(*(env)));\
6b2b6112
FB
649 for(i=0;i<5;i++)\
650 asm volatile ("fldl %0" : : "m" (dtab[i]));\
085339a1
FB
651 asm volatile (save " %0\n" : : "m" (*(env)));\
652 asm volatile (restore " %0\n": : "m" (*(env)));\
6b2b6112
FB
653 for(i=0;i<5;i++)\
654 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
655 for(i=0;i<5;i++)\
656 printf("res[%d]=%f\n", i, rtab[i]);\
03bfca94
FB
657 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
658 (env)->fpuc,\
659 (env)->fpus & 0xff00,\
660 (env)->fptag);\
03bfca94
FB
661}
662
663void test_fenv(void)
664{
665 struct __attribute__((packed)) {
666 uint16_t fpuc;
667 uint16_t dummy1;
668 uint16_t fpus;
669 uint16_t dummy2;
670 uint16_t fptag;
671 uint16_t dummy3;
672 uint32_t ignored[4];
673 long double fpregs[8];
674 } float_env32;
675 struct __attribute__((packed)) {
676 uint16_t fpuc;
677 uint16_t fpus;
678 uint16_t fptag;
679 uint16_t ignored[4];
680 long double fpregs[8];
681 } float_env16;
6b2b6112
FB
682 double dtab[8];
683 double rtab[8];
684 int i;
685
686 for(i=0;i<8;i++)
687 dtab[i] = i + 1;
03bfca94 688
6b2b6112
FB
689 TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
690 TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
691 TEST_ENV(&float_env32, "fnstenv", "fldenv");
692 TEST_ENV(&float_env32, "fnsave", "frstor");
665656a9
FB
693
694 /* test for ffree */
695 for(i=0;i<5;i++)
696 asm volatile ("fldl %0" : : "m" (dtab[i]));
697 asm volatile("ffree %st(2)");
698 asm volatile ("fnstenv %0\n" : : "m" (float_env32));
699 asm volatile ("fninit");
700 printf("fptag=%04x\n", float_env32.fptag);
03bfca94
FB
701}
702
75175024
FB
703
704#define TEST_FCMOV(a, b, eflags, CC)\
705{\
706 double res;\
707 asm("push %3\n"\
708 "popf\n"\
709 "fcmov" CC " %2, %0\n"\
710 : "=t" (res)\
711 : "0" (a), "u" (b), "g" (eflags));\
712 printf("fcmov%s eflags=0x%04x-> %f\n", \
713 CC, eflags, res);\
714}
715
716void test_fcmov(void)
717{
718 double a, b;
719 int eflags, i;
720
721 a = 1.0;
722 b = 2.0;
723 for(i = 0; i < 4; i++) {
724 eflags = 0;
725 if (i & 1)
726 eflags |= CC_C;
727 if (i & 2)
728 eflags |= CC_Z;
729 TEST_FCMOV(a, b, eflags, "b");
730 TEST_FCMOV(a, b, eflags, "e");
731 TEST_FCMOV(a, b, eflags, "be");
732 TEST_FCMOV(a, b, eflags, "nb");
733 TEST_FCMOV(a, b, eflags, "ne");
734 TEST_FCMOV(a, b, eflags, "nbe");
735 }
736 TEST_FCMOV(a, b, 0, "u");
9cdf757f
FB
737 TEST_FCMOV(a, b, CC_P, "u");
738 TEST_FCMOV(a, b, 0, "nu");
75175024
FB
739 TEST_FCMOV(a, b, CC_P, "nu");
740}
741
9d8e9c09
FB
742void test_floats(void)
743{
744 test_fops(2, 3);
745 test_fops(1.4, -5);
746 test_fcmp(2, -1);
747 test_fcmp(2, 2);
748 test_fcmp(2, 3);
ea768640
FB
749 test_fcvt(0.5);
750 test_fcvt(-0.5);
9d8e9c09
FB
751 test_fcvt(1.0/7.0);
752 test_fcvt(-1.0/9.0);
ea768640
FB
753 test_fcvt(32768);
754 test_fcvt(-1e20);
9d8e9c09 755 test_fconst();
c5e9815d
FB
756 test_fbcd(1234567890123456);
757 test_fbcd(-123451234567890);
03bfca94 758 test_fenv();
75175024
FB
759 if (TEST_CMOV) {
760 test_fcmov();
761 }
9d8e9c09 762}
4b74fe1f 763
55480af8
FB
764/**********************************************/
765
766#define TEST_BCD(op, op0, cc_in, cc_mask)\
767{\
768 int res, flags;\
769 res = op0;\
770 flags = cc_in;\
771 asm ("push %3\n\t"\
772 "popf\n\t"\
773 #op "\n\t"\
774 "pushf\n\t"\
775 "popl %1\n\t"\
776 : "=a" (res), "=g" (flags)\
777 : "0" (res), "1" (flags));\
778 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
779 #op, op0, res, cc_in, flags & cc_mask);\
780}
781
782void test_bcd(void)
783{
784 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
785 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
786 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
787 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
788 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
789 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
790 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
791 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
792 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
793 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
794 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
795 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
796 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
797
798 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
799 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
800 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
801 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
802 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
803 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
804 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
805 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
806 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
807 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
808 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
809 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
810 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
811
812 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
813 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
814 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
815 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
816 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
817 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
818 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
819 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
820
821 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
822 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
823 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
824 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
825 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
826 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
827 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
828 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
829
830 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
831 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
832}
833
e5918247
FB
834#define TEST_XCHG(op, size, opconst)\
835{\
836 int op0, op1;\
837 op0 = 0x12345678;\
838 op1 = 0xfbca7654;\
839 asm(#op " %" size "0, %" size "1" \
840 : "=q" (op0), opconst (op1) \
841 : "0" (op0), "1" (op1));\
842 printf("%-10s A=%08x B=%08x\n",\
843 #op, op0, op1);\
844}
845
846#define TEST_CMPXCHG(op, size, opconst, eax)\
847{\
848 int op0, op1;\
849 op0 = 0x12345678;\
850 op1 = 0xfbca7654;\
851 asm(#op " %" size "0, %" size "1" \
852 : "=q" (op0), opconst (op1) \
853 : "0" (op0), "1" (op1), "a" (eax));\
854 printf("%-10s EAX=%08x A=%08x C=%08x\n",\
855 #op, eax, op0, op1);\
856}
857
858void test_xchg(void)
859{
860 TEST_XCHG(xchgl, "", "=q");
861 TEST_XCHG(xchgw, "w", "=q");
862 TEST_XCHG(xchgb, "b", "=q");
863
864 TEST_XCHG(xchgl, "", "=m");
865 TEST_XCHG(xchgw, "w", "=m");
866 TEST_XCHG(xchgb, "b", "=m");
867
868 TEST_XCHG(xaddl, "", "=q");
869 TEST_XCHG(xaddw, "w", "=q");
870 TEST_XCHG(xaddb, "b", "=q");
871
d575b78a
FB
872 {
873 int res;
874 res = 0x12345678;
875 asm("xaddl %1, %0" : "=r" (res) : "0" (res));
876 printf("xaddl same res=%08x\n", res);
877 }
878
e5918247
FB
879 TEST_XCHG(xaddl, "", "=m");
880 TEST_XCHG(xaddw, "w", "=m");
881 TEST_XCHG(xaddb, "b", "=m");
882
883 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfbca7654);
884 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
885 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);
886
887 TEST_CMPXCHG(cmpxchgl, "", "=q", 0xfffefdfc);
888 TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
889 TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);
890
891 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfbca7654);
892 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
893 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);
894
895 TEST_CMPXCHG(cmpxchgl, "", "=m", 0xfffefdfc);
896 TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
897 TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
d575b78a
FB
898
899 {
900 uint64_t op0, op1, op2;
901 int i, eflags;
902
903 for(i = 0; i < 2; i++) {
904 op0 = 0x123456789abcd;
905 if (i == 0)
906 op1 = 0xfbca765423456;
907 else
908 op1 = op0;
909 op2 = 0x6532432432434;
910 asm("cmpxchg8b %1\n"
911 "pushf\n"
912 "popl %2\n"
913 : "=A" (op0), "=m" (op1), "=g" (eflags)
914 : "0" (op0), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
915 printf("cmpxchg8b: op0=%016llx op1=%016llx CC=%02x\n",
916 op0, op1, eflags & CC_Z);
917 }
918 }
e5918247
FB
919}
920
6dbad63e
FB
921/**********************************************/
922/* segmentation tests */
923
924#include <asm/ldt.h>
925#include <linux/unistd.h>
73bdea19 926#include <linux/version.h>
6dbad63e
FB
927
928_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
929
73bdea19
FB
930#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
931#define modify_ldt_ldt_s user_desc
932#endif
933
6dbad63e
FB
934uint8_t seg_data1[4096];
935uint8_t seg_data2[4096];
936
e5918247 937#define MK_SEL(n) (((n) << 3) | 7)
6dbad63e 938
288426fe
FB
939#define TEST_LR(op, size, seg, mask)\
940{\
941 int res, res2;\
942 res = 0x12345678;\
943 asm (op " %" size "2, %" size "0\n" \
944 "movl $0, %1\n"\
945 "jnz 1f\n"\
946 "movl $1, %1\n"\
947 "1:\n"\
948 : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
949 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
950}
951
6dbad63e
FB
952/* NOTE: we use Linux modify_ldt syscall */
953void test_segs(void)
954{
955 struct modify_ldt_ldt_s ldt;
956 long long ldt_table[3];
04369ff2 957 int res, res2;
6dbad63e 958 char tmp;
e1d4294a
FB
959 struct {
960 uint32_t offset;
961 uint16_t seg;
962 } __attribute__((packed)) segoff;
6dbad63e
FB
963
964 ldt.entry_number = 1;
965 ldt.base_addr = (unsigned long)&seg_data1;
966 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
967 ldt.seg_32bit = 1;
968 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
969 ldt.read_exec_only = 0;
970 ldt.limit_in_pages = 1;
971 ldt.seg_not_present = 0;
972 ldt.useable = 1;
973 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
974
975 ldt.entry_number = 2;
976 ldt.base_addr = (unsigned long)&seg_data2;
977 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
978 ldt.seg_32bit = 1;
979 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
980 ldt.read_exec_only = 0;
981 ldt.limit_in_pages = 1;
982 ldt.seg_not_present = 0;
983 ldt.useable = 1;
984 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
985
986 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
04369ff2
FB
987#if 0
988 {
989 int i;
990 for(i=0;i<3;i++)
991 printf("%d: %016Lx\n", i, ldt_table[i]);
992 }
993#endif
6dbad63e
FB
994 /* do some tests with fs or gs */
995 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
6dbad63e
FB
996
997 seg_data1[1] = 0xaa;
998 seg_data2[1] = 0x55;
999
1000 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
1001 printf("FS[1] = %02x\n", res);
1002
070893f4
FB
1003 asm volatile ("pushl %%gs\n"
1004 "movl %1, %%gs\n"
1005 "gs movzbl 0x1, %0\n"
1006 "popl %%gs\n"
1007 : "=r" (res)
1008 : "r" (MK_SEL(2)));
6dbad63e
FB
1009 printf("GS[1] = %02x\n", res);
1010
1011 /* tests with ds/ss (implicit segment case) */
1012 tmp = 0xa5;
1013 asm volatile ("pushl %%ebp\n\t"
1014 "pushl %%ds\n\t"
1015 "movl %2, %%ds\n\t"
1016 "movl %3, %%ebp\n\t"
1017 "movzbl 0x1, %0\n\t"
1018 "movzbl (%%ebp), %1\n\t"
1019 "popl %%ds\n\t"
1020 "popl %%ebp\n\t"
1021 : "=r" (res), "=r" (res2)
1022 : "r" (MK_SEL(1)), "r" (&tmp));
1023 printf("DS[1] = %02x\n", res);
1024 printf("SS[tmp] = %02x\n", res2);
e1d4294a
FB
1025
1026 segoff.seg = MK_SEL(2);
1027 segoff.offset = 0xabcdef12;
1028 asm volatile("lfs %2, %0\n\t"
1029 "movl %%fs, %1\n\t"
1030 : "=r" (res), "=g" (res2)
1031 : "m" (segoff));
1032 printf("FS:reg = %04x:%08x\n", res2, res);
288426fe
FB
1033
1034 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1035 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1036 TEST_LR("lslw", "w", MK_SEL(2), 0);
1037 TEST_LR("lsll", "", MK_SEL(2), 0);
1038
1039 TEST_LR("larw", "w", 0xfff8, 0);
1040 TEST_LR("larl", "", 0xfff8, 0);
1041 TEST_LR("lslw", "w", 0xfff8, 0);
1042 TEST_LR("lsll", "", 0xfff8, 0);
6dbad63e 1043}
55480af8 1044
e5918247
FB
1045/* 16 bit code test */
1046extern char code16_start, code16_end;
1047extern char code16_func1;
1048extern char code16_func2;
1049extern char code16_func3;
a300e691 1050
e5918247 1051void test_code16(void)
1a9353d2 1052{
e5918247
FB
1053 struct modify_ldt_ldt_s ldt;
1054 int res, res2;
a300e691 1055
e5918247
FB
1056 /* build a code segment */
1057 ldt.entry_number = 1;
1058 ldt.base_addr = (unsigned long)&code16_start;
1059 ldt.limit = &code16_end - &code16_start;
1060 ldt.seg_32bit = 0;
1061 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
1062 ldt.read_exec_only = 0;
1063 ldt.limit_in_pages = 0;
1064 ldt.seg_not_present = 0;
1065 ldt.useable = 1;
1066 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
a300e691 1067
e5918247
FB
1068 /* call the first function */
1069 asm volatile ("lcall %1, %2"
1070 : "=a" (res)
1071 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
1072 printf("func1() = 0x%08x\n", res);
1073 asm volatile ("lcall %2, %3"
1074 : "=a" (res), "=c" (res2)
1075 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
1076 printf("func2() = 0x%08x spdec=%d\n", res, res2);
1077 asm volatile ("lcall %1, %2"
1078 : "=a" (res)
1079 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1080 printf("func3() = 0x%08x\n", res);
1a9353d2
FB
1081}
1082
dd3587f3
FB
1083extern char func_lret32;
1084extern char func_iret32;
1085
e1d4294a
FB
1086void test_misc(void)
1087{
1088 char table[256];
1089 int res, i;
1090
1091 for(i=0;i<256;i++) table[i] = 256 - i;
1092 res = 0x12345678;
1093 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
1094 printf("xlat: EAX=%08x\n", res);
dd3587f3
FB
1095
1096 asm volatile ("pushl %%cs ; call %1"
1097 : "=a" (res)
1098 : "m" (func_lret32): "memory", "cc");
1099 printf("func_lret32=%x\n", res);
1100
1101 asm volatile ("pushfl ; pushl %%cs ; call %1"
1102 : "=a" (res)
1103 : "m" (func_iret32): "memory", "cc");
1104 printf("func_iret32=%x\n", res);
1105
1106 /* specific popl test */
1107 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1108 : "=g" (res));
1109 printf("popl esp=%x\n", res);
b2b5fb22
FB
1110
1111 /* specific popw test */
1112 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1113 : "=g" (res));
1114 printf("popw esp=%x\n", res);
e1d4294a
FB
1115}
1116
1117uint8_t str_buffer[4096];
1118
1119#define TEST_STRING1(OP, size, DF, REP)\
1120{\
1121 int esi, edi, eax, ecx, eflags;\
1122\
1123 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1124 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
1125 eax = 0x12345678;\
1126 ecx = 17;\
1127\
1128 asm volatile ("pushl $0\n\t"\
1129 "popf\n\t"\
1130 DF "\n\t"\
1131 REP #OP size "\n\t"\
1132 "cld\n\t"\
1133 "pushf\n\t"\
1134 "popl %4\n\t"\
1135 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1136 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
1137 printf("%-10s ESI=%08x EDI=%08x EAX=%08x ECX=%08x EFL=%04x\n",\
1138 REP #OP size, esi, edi, eax, ecx,\
1139 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1140}
1141
1142#define TEST_STRING(OP, REP)\
1143 TEST_STRING1(OP, "b", "", REP);\
1144 TEST_STRING1(OP, "w", "", REP);\
1145 TEST_STRING1(OP, "l", "", REP);\
1146 TEST_STRING1(OP, "b", "std", REP);\
1147 TEST_STRING1(OP, "w", "std", REP);\
1148 TEST_STRING1(OP, "l", "std", REP)
1149
1150void test_string(void)
1151{
1152 int i;
1153 for(i = 0;i < sizeof(str_buffer); i++)
1154 str_buffer[i] = i + 0x56;
1155 TEST_STRING(stos, "");
1156 TEST_STRING(stos, "rep ");
1157 TEST_STRING(lods, ""); /* to verify stos */
1158 TEST_STRING(lods, "rep ");
1159 TEST_STRING(movs, "");
1160 TEST_STRING(movs, "rep ");
1161 TEST_STRING(lods, ""); /* to verify stos */
1162
1163 /* XXX: better tests */
1164 TEST_STRING(scas, "");
1165 TEST_STRING(scas, "repz ");
1166 TEST_STRING(scas, "repnz ");
1167 TEST_STRING(cmps, "");
1168 TEST_STRING(cmps, "repz ");
1169 TEST_STRING(cmps, "repnz ");
1170}
e5918247 1171
3a27ad0b
FB
1172/* VM86 test */
1173
1174static inline void set_bit(uint8_t *a, unsigned int bit)
1175{
1176 a[bit / 8] |= (1 << (bit % 8));
1177}
1178
1179static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1180{
1181 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1182}
1183
1184static inline void pushw(struct vm86_regs *r, int val)
1185{
1186 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1187 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1188}
1189
1190#undef __syscall_return
1191#define __syscall_return(type, res) \
1192do { \
1193 return (type) (res); \
1194} while (0)
1195
1196_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)
1197
1198extern char vm86_code_start;
1199extern char vm86_code_end;
1200
1201#define VM86_CODE_CS 0x100
1202#define VM86_CODE_IP 0x100
1203
1204void test_vm86(void)
1205{
1206 struct vm86plus_struct ctx;
1207 struct vm86_regs *r;
1208 uint8_t *vm86_mem;
1209 int seg, ret;
1210
1211 vm86_mem = mmap((void *)0x00000000, 0x110000,
1212 PROT_WRITE | PROT_READ | PROT_EXEC,
1213 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1214 if (vm86_mem == MAP_FAILED) {
1215 printf("ERROR: could not map vm86 memory");
1216 return;
1217 }
1218 memset(&ctx, 0, sizeof(ctx));
1219
1220 /* init basic registers */
1221 r = &ctx.regs;
1222 r->eip = VM86_CODE_IP;
1223 r->esp = 0xfffe;
1224 seg = VM86_CODE_CS;
1225 r->cs = seg;
1226 r->ss = seg;
1227 r->ds = seg;
1228 r->es = seg;
1229 r->fs = seg;
1230 r->gs = seg;
1231 r->eflags = VIF_MASK;
1232
1233 /* move code to proper address. We use the same layout as a .com
1234 dos program. */
1235 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
1236 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1237
1238 /* mark int 0x21 as being emulated */
1239 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1240
1241 for(;;) {
1242 ret = vm86(VM86_ENTER, &ctx);
1243 switch(VM86_TYPE(ret)) {
1244 case VM86_INTx:
1245 {
3ff0631e 1246 int int_num, ah, v;
3a27ad0b
FB
1247
1248 int_num = VM86_ARG(ret);
1249 if (int_num != 0x21)
1250 goto unknown_int;
1251 ah = (r->eax >> 8) & 0xff;
1252 switch(ah) {
1253 case 0x00: /* exit */
1254 goto the_end;
1255 case 0x02: /* write char */
1256 {
1257 uint8_t c = r->edx;
1258 putchar(c);
1259 }
1260 break;
1261 case 0x09: /* write string */
1262 {
1263 uint8_t c, *ptr;
1264 ptr = seg_to_linear(r->ds, r->edx);
1265 for(;;) {
1266 c = *ptr++;
1267 if (c == '$')
1268 break;
1269 putchar(c);
1270 }
1271 r->eax = (r->eax & ~0xff) | '$';
1272 }
1273 break;
3ff0631e
FB
1274 case 0xff: /* extension: write eflags number in edx */
1275 v = (int)r->edx;
1276#ifndef LINUX_VM86_IOPL_FIX
1277 v &= ~0x3000;
1278#endif
1279 printf("%08x\n", v);
3a27ad0b
FB
1280 break;
1281 default:
1282 unknown_int:
1283 printf("unsupported int 0x%02x\n", int_num);
1284 goto the_end;
1285 }
1286 }
1287 break;
1288 case VM86_SIGNAL:
1289 /* a signal came, we just ignore that */
1290 break;
1291 case VM86_STI:
1292 break;
1293 default:
1294 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1295 goto the_end;
1296 }
1297 }
1298 the_end:
1299 printf("VM86 end\n");
1300 munmap(vm86_mem, 0x110000);
1301}
1302
1303/* exception tests */
1304#ifndef REG_EAX
1305#define REG_EAX EAX
1306#define REG_EBX EBX
1307#define REG_ECX ECX
1308#define REG_EDX EDX
1309#define REG_ESI ESI
1310#define REG_EDI EDI
1311#define REG_EBP EBP
1312#define REG_ESP ESP
1313#define REG_EIP EIP
1314#define REG_EFL EFL
1315#define REG_TRAPNO TRAPNO
1316#define REG_ERR ERR
1317#endif
1318
1319jmp_buf jmp_env;
3a27ad0b
FB
1320int v1;
1321int tab[2];
1322
1323void sig_handler(int sig, siginfo_t *info, void *puc)
1324{
1325 struct ucontext *uc = puc;
1326
1327 printf("si_signo=%d si_errno=%d si_code=%d",
1328 info->si_signo, info->si_errno, info->si_code);
e3b32540
FB
1329 printf(" si_addr=0x%08lx",
1330 (unsigned long)info->si_addr);
3a27ad0b
FB
1331 printf("\n");
1332
1333 printf("trapno=0x%02x err=0x%08x",
1334 uc->uc_mcontext.gregs[REG_TRAPNO],
1335 uc->uc_mcontext.gregs[REG_ERR]);
e3b32540 1336 printf(" EIP=0x%08x", uc->uc_mcontext.gregs[REG_EIP]);
3a27ad0b
FB
1337 printf("\n");
1338 longjmp(jmp_env, 1);
1339}
1340
1341void test_exceptions(void)
1342{
e3b32540 1343 struct modify_ldt_ldt_s ldt;
3a27ad0b
FB
1344 struct sigaction act;
1345 volatile int val;
1346
1347 act.sa_sigaction = sig_handler;
1348 sigemptyset(&act.sa_mask);
1349 act.sa_flags = SA_SIGINFO;
1350 sigaction(SIGFPE, &act, NULL);
1351 sigaction(SIGILL, &act, NULL);
1352 sigaction(SIGSEGV, &act, NULL);
e3b32540 1353 sigaction(SIGBUS, &act, NULL);
3a27ad0b
FB
1354 sigaction(SIGTRAP, &act, NULL);
1355
1356 /* test division by zero reporting */
e3b32540 1357 printf("DIVZ exception:\n");
3a27ad0b
FB
1358 if (setjmp(jmp_env) == 0) {
1359 /* now divide by zero */
1360 v1 = 0;
1361 v1 = 2 / v1;
1362 }
1363
e3b32540 1364 printf("BOUND exception:\n");
3a27ad0b
FB
1365 if (setjmp(jmp_env) == 0) {
1366 /* bound exception */
1367 tab[0] = 1;
1368 tab[1] = 10;
e82d8ade 1369 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab[0]));
3a27ad0b
FB
1370 }
1371
e3b32540
FB
1372 printf("segment exceptions:\n");
1373 if (setjmp(jmp_env) == 0) {
1374 /* load an invalid segment */
1375 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1376 }
1377 if (setjmp(jmp_env) == 0) {
1378 /* null data segment is valid */
1379 asm volatile ("movl %0, %%fs" : : "r" (3));
1380 /* null stack segment */
1381 asm volatile ("movl %0, %%ss" : : "r" (3));
1382 }
1383
1384 ldt.entry_number = 1;
1385 ldt.base_addr = (unsigned long)&seg_data1;
1386 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1387 ldt.seg_32bit = 1;
1388 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1389 ldt.read_exec_only = 0;
1390 ldt.limit_in_pages = 1;
1391 ldt.seg_not_present = 1;
1392 ldt.useable = 1;
1393 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1394
1395 if (setjmp(jmp_env) == 0) {
1396 /* segment not present */
1397 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1398 }
1399
3a27ad0b 1400 /* test SEGV reporting */
e3b32540 1401 printf("PF exception:\n");
3a27ad0b 1402 if (setjmp(jmp_env) == 0) {
e3b32540 1403 val = 1;
ede28208
FB
1404 /* we add a nop to test a weird PC retrieval case */
1405 asm volatile ("nop");
3a27ad0b
FB
1406 /* now store in an invalid address */
1407 *(char *)0x1234 = 1;
1408 }
1409
1410 /* test SEGV reporting */
e3b32540 1411 printf("PF exception:\n");
3a27ad0b 1412 if (setjmp(jmp_env) == 0) {
e3b32540 1413 val = 1;
3a27ad0b
FB
1414 /* read from an invalid address */
1415 v1 = *(char *)0x1234;
1416 }
1417
3a27ad0b
FB
1418 /* test illegal instruction reporting */
1419 printf("UD2 exception:\n");
1420 if (setjmp(jmp_env) == 0) {
1421 /* now execute an invalid instruction */
1422 asm volatile("ud2");
1423 }
4120b61d
FB
1424 printf("lock nop exception:\n");
1425 if (setjmp(jmp_env) == 0) {
1426 /* now execute an invalid instruction */
1427 asm volatile("lock nop");
1428 }
3a27ad0b
FB
1429
1430 printf("INT exception:\n");
1431 if (setjmp(jmp_env) == 0) {
1432 asm volatile ("int $0xfd");
1433 }
e3b32540
FB
1434 if (setjmp(jmp_env) == 0) {
1435 asm volatile ("int $0x01");
1436 }
1437 if (setjmp(jmp_env) == 0) {
1438 asm volatile (".byte 0xcd, 0x03");
1439 }
1440 if (setjmp(jmp_env) == 0) {
1441 asm volatile ("int $0x04");
1442 }
1443 if (setjmp(jmp_env) == 0) {
1444 asm volatile ("int $0x05");
1445 }
3a27ad0b
FB
1446
1447 printf("INT3 exception:\n");
1448 if (setjmp(jmp_env) == 0) {
1449 asm volatile ("int3");
1450 }
1451
1452 printf("CLI exception:\n");
1453 if (setjmp(jmp_env) == 0) {
1454 asm volatile ("cli");
1455 }
1456
1457 printf("STI exception:\n");
1458 if (setjmp(jmp_env) == 0) {
1459 asm volatile ("cli");
1460 }
1461
1462 printf("INTO exception:\n");
1463 if (setjmp(jmp_env) == 0) {
1464 /* overflow exception */
1465 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1466 }
1467
1468 printf("OUTB exception:\n");
1469 if (setjmp(jmp_env) == 0) {
1470 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1471 }
1472
1473 printf("INB exception:\n");
1474 if (setjmp(jmp_env) == 0) {
1475 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1476 }
1477
1478 printf("REP OUTSB exception:\n");
1479 if (setjmp(jmp_env) == 0) {
1480 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1481 }
1482
1483 printf("REP INSB exception:\n");
1484 if (setjmp(jmp_env) == 0) {
1485 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1486 }
1487
1488 printf("HLT exception:\n");
1489 if (setjmp(jmp_env) == 0) {
1490 asm volatile ("hlt");
1491 }
1492
1493 printf("single step exception:\n");
1494 val = 0;
1495 if (setjmp(jmp_env) == 0) {
1496 asm volatile ("pushf\n"
1497 "orl $0x00100, (%%esp)\n"
1498 "popf\n"
1499 "movl $0xabcd, %0\n"
1500 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1501 }
1502 printf("val=0x%x\n", val);
1503}
1504
3ff0631e
FB
1505/* specific precise single step test */
1506void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1507{
1508 struct ucontext *uc = puc;
1509 printf("EIP=0x%08x\n", uc->uc_mcontext.gregs[REG_EIP]);
1510}
1511
1512const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1513uint8_t sstep_buf2[4];
1514
1515void test_single_step(void)
1516{
1517 struct sigaction act;
1518 volatile int val;
1519 int i;
1520
1521 val = 0;
1522 act.sa_sigaction = sig_trap_handler;
1523 sigemptyset(&act.sa_mask);
1524 act.sa_flags = SA_SIGINFO;
1525 sigaction(SIGTRAP, &act, NULL);
1526 asm volatile ("pushf\n"
1527 "orl $0x00100, (%%esp)\n"
1528 "popf\n"
1529 "movl $0xabcd, %0\n"
1530
1531 /* jmp test */
1532 "movl $3, %%ecx\n"
1533 "1:\n"
1534 "addl $1, %0\n"
1535 "decl %%ecx\n"
1536 "jnz 1b\n"
1537
1538 /* movsb: the single step should stop at each movsb iteration */
1539 "movl $sstep_buf1, %%esi\n"
1540 "movl $sstep_buf2, %%edi\n"
1541 "movl $0, %%ecx\n"
1542 "rep movsb\n"
1543 "movl $3, %%ecx\n"
1544 "rep movsb\n"
1545 "movl $1, %%ecx\n"
1546 "rep movsb\n"
1547
1548 /* cmpsb: the single step should stop at each cmpsb iteration */
1549 "movl $sstep_buf1, %%esi\n"
1550 "movl $sstep_buf2, %%edi\n"
1551 "movl $0, %%ecx\n"
1552 "rep cmpsb\n"
1553 "movl $4, %%ecx\n"
1554 "rep cmpsb\n"
1555
1556 /* getpid() syscall: single step should skip one
1557 instruction */
1558 "movl $20, %%eax\n"
1559 "int $0x80\n"
1560 "movl $0, %%eax\n"
1561
1562 /* when modifying SS, trace is not done on the next
1563 instruction */
1564 "movl %%ss, %%ecx\n"
1565 "movl %%ecx, %%ss\n"
1566 "addl $1, %0\n"
1567 "movl $1, %%eax\n"
1568 "movl %%ecx, %%ss\n"
1569 "jmp 1f\n"
1570 "addl $1, %0\n"
1571 "1:\n"
1572 "movl $1, %%eax\n"
1573 "pushl %%ecx\n"
1574 "popl %%ss\n"
1575 "addl $1, %0\n"
1576 "movl $1, %%eax\n"
1577
1578 "pushf\n"
1579 "andl $~0x00100, (%%esp)\n"
1580 "popf\n"
1581 : "=m" (val)
1582 :
1583 : "cc", "memory", "eax", "ecx", "esi", "edi");
1584 printf("val=%d\n", val);
1585 for(i = 0; i < 4; i++)
1586 printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1587}
1588
3a27ad0b
FB
1589/* self modifying code test */
1590uint8_t code[] = {
1591 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1592 0xc3, /* ret */
1593};
1594
1190935d
FB
1595asm("smc_code2:\n"
1596 "movl 4(%esp), %eax\n"
1597 "movl %eax, smc_patch_addr2 + 1\n"
1598 "nop\n"
1599 "nop\n"
1600 "nop\n"
1601 "nop\n"
1602 "nop\n"
1603 "nop\n"
1604 "nop\n"
1605 "nop\n"
1606 "smc_patch_addr2:\n"
1607 "movl $1, %eax\n"
1608 "ret\n");
d1fe2b24 1609
1190935d
FB
1610typedef int FuncType(void);
1611extern int smc_code2(int);
3a27ad0b
FB
1612void test_self_modifying_code(void)
1613{
d1fe2b24 1614 int i;
3a27ad0b 1615
3a27ad0b 1616 printf("self modifying code:\n");
d1fe2b24
FB
1617 printf("func1 = 0x%x\n", ((FuncType *)code)());
1618 for(i = 2; i <= 4; i++) {
1619 code[1] = i;
1620 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
1621 }
1190935d
FB
1622
1623 /* more difficult test : the modified code is just after the
1624 modifying instruction. It is forbidden in Intel specs, but it
1625 is used by old DOS programs */
1626 for(i = 2; i <= 4; i++) {
1627 printf("smc_code2(%d) = %d\n", i, smc_code2(i));
1628 }
3a27ad0b 1629}
61a8c4ec
FB
1630
1631int enter_stack[4096];
1632
1633#define TEST_ENTER(size, stack_type, level)\
1634{\
1635 int esp_save, esp_val, ebp_val, ebp_save, i;\
1636 stack_type *ptr, *stack_end, *stack_ptr;\
1637 memset(enter_stack, 0, sizeof(enter_stack));\
1638 stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
1639 ebp_val = (long)stack_ptr;\
1640 for(i=1;i<=32;i++)\
1641 *--stack_ptr = i;\
1642 esp_val = (long)stack_ptr;\
1643 asm("movl %%esp, %[esp_save]\n"\
1644 "movl %%ebp, %[ebp_save]\n"\
1645 "movl %[esp_val], %%esp\n"\
1646 "movl %[ebp_val], %%ebp\n"\
1647 "enter" size " $12, $" #level "\n"\
1648 "movl %%esp, %[esp_val]\n"\
1649 "movl %%ebp, %[ebp_val]\n"\
1650 "movl %[esp_save], %%esp\n"\
1651 "movl %[ebp_save], %%ebp\n"\
1652 : [esp_save] "=r" (esp_save),\
1653 [ebp_save] "=r" (ebp_save),\
1654 [esp_val] "=r" (esp_val),\
1655 [ebp_val] "=r" (ebp_val)\
1656 : "[esp_val]" (esp_val),\
1657 "[ebp_val]" (ebp_val));\
1658 printf("level=%d:\n", level);\
1659 printf("esp_val=0x%08lx\n", esp_val - (long)stack_end);\
1660 printf("ebp_val=0x%08lx\n", ebp_val - (long)stack_end);\
1661 for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
1662 printf("%08x\n", ptr[0]);\
1663}
1664
1665static void test_enter(void)
1666{
1667 TEST_ENTER("l", uint32_t, 0);
1668 TEST_ENTER("l", uint32_t, 1);
1669 TEST_ENTER("l", uint32_t, 2);
1670 TEST_ENTER("l", uint32_t, 31);
1671
1672 TEST_ENTER("w", uint16_t, 0);
1673 TEST_ENTER("w", uint16_t, 1);
1674 TEST_ENTER("w", uint16_t, 2);
1675 TEST_ENTER("w", uint16_t, 31);
1676}
1677
085339a1
FB
1678#ifdef TEST_SSE
1679
1680typedef int __m64 __attribute__ ((__mode__ (__V2SI__)));
1681typedef int __m128 __attribute__ ((__mode__(__V4SF__)));
1682
1683typedef union {
1684 double d[2];
1685 float s[4];
1686 uint32_t l[4];
1687 uint64_t q[2];
1688 __m128 dq;
1689} XMMReg;
1690
1691static uint64_t __attribute__((aligned(16))) test_values[4][2] = {
1692 { 0x456723c698694873, 0xdc515cff944a58ec },
1693 { 0x1f297ccd58bad7ab, 0x41f21efba9e3e146 },
1694 { 0x007c62c2085427f8, 0x231be9e8cde7438d },
1695 { 0x0f76255a085427f8, 0xc233e9e8c4c9439a },
1696};
1697
1698#define SSE_OP(op)\
1699{\
1700 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
1701 printf("%-9s: a=%016llx%016llx b=%016llx%016llx r=%016llx%016llx\n",\
1702 #op,\
1703 a.q[1], a.q[0],\
1704 b.q[1], b.q[0],\
1705 r.q[1], r.q[0]);\
1706}
1707
1708#define SSE_OP2(op)\
1709{\
1710 int i;\
1711 for(i=0;i<2;i++) {\
1712 a.q[0] = test_values[2*i][0];\
1713 a.q[1] = test_values[2*i][1];\
1714 b.q[0] = test_values[2*i+1][0];\
1715 b.q[1] = test_values[2*i+1][1];\
1716 SSE_OP(op);\
1717 }\
1718}
1719
1720#define MMX_OP2(op)\
1721{\
1722 int i;\
1723 for(i=0;i<2;i++) {\
1724 a.q[0] = test_values[2*i][0];\
1725 b.q[0] = test_values[2*i+1][0];\
1726 asm volatile (#op " %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0]));\
1727 printf("%-9s: a=%016llx b=%016llx r=%016llx\n",\
1728 #op,\
1729 a.q[0],\
1730 b.q[0],\
1731 r.q[0]);\
1732 }\
1733 SSE_OP2(op);\
1734}
1735
1736#define PSHUF_OP(op, ib)\
1737{\
1738 int i;\
1739 for(i=0;i<2;i++) {\
1740 a.q[0] = test_values[2*i][0];\
1741 a.q[1] = test_values[2*i][1];\
1742 asm volatile (#op " $" #ib ", %1, %0" : "=x" (r.dq) : "x" (a.dq));\
1743 printf("%-9s: a=%016llx%016llx ib=%02x r=%016llx%016llx\n",\
1744 #op,\
1745 a.q[1], a.q[0],\
1746 ib,\
1747 r.q[1], r.q[0]);\
1748 }\
1749}
1750
1751#define SHIFT_IM(op, ib)\
1752{\
1753 int i;\
1754 for(i=0;i<2;i++) {\
1755 a.q[0] = test_values[2*i][0];\
1756 a.q[1] = test_values[2*i][1];\
1757 asm volatile (#op " $" #ib ", %0" : "=x" (r.dq) : "0" (a.dq));\
1758 printf("%-9s: a=%016llx%016llx ib=%02x r=%016llx%016llx\n",\
1759 #op,\
1760 a.q[1], a.q[0],\
1761 ib,\
1762 r.q[1], r.q[0]);\
1763 }\
1764}
1765
1766#define SHIFT_OP(op, ib)\
1767{\
1768 int i;\
1769 SHIFT_IM(op, ib);\
1770 for(i=0;i<2;i++) {\
1771 a.q[0] = test_values[2*i][0];\
1772 a.q[1] = test_values[2*i][1];\
1773 b.q[0] = ib;\
1774 b.q[1] = 0;\
1775 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
1776 printf("%-9s: a=%016llx%016llx b=%016llx%016llx r=%016llx%016llx\n",\
1777 #op,\
1778 a.q[1], a.q[0],\
1779 b.q[1], b.q[0],\
1780 r.q[1], r.q[0]);\
1781 }\
1782}
1783
1784#define MOVMSK(op)\
1785{\
1786 int i, reg;\
1787 for(i=0;i<2;i++) {\
1788 a.q[0] = test_values[2*i][0];\
1789 a.q[1] = test_values[2*i][1];\
1790 asm volatile (#op " %1, %0" : "=r" (reg) : "x" (a.dq));\
1791 printf("%-9s: a=%016llx%016llx r=%08x\n",\
1792 #op,\
1793 a.q[1], a.q[0],\
1794 reg);\
1795 }\
1796}
1797
1798#define SSE_OPS(a) \
1799SSE_OP(a ## ps);\
1800SSE_OP(a ## ss);
1801
1802#define SSE_OPD(a) \
1803SSE_OP(a ## pd);\
1804SSE_OP(a ## sd);
1805
1806#define SSE_COMI(op, field)\
1807{\
1808 unsigned int eflags;\
1809 XMMReg a, b;\
1810 a.field[0] = a1;\
1811 b.field[0] = b1;\
1812 asm volatile (#op " %2, %1\n"\
1813 "pushf\n"\
1814 "pop %0\n"\
1815 : "=m" (eflags)\
1816 : "x" (a.dq), "x" (b.dq));\
1817 printf("%-9s: a=%f b=%f cc=%04x\n",\
1818 #op, a1, b1,\
1819 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
1820}
1821
1822void test_sse_comi(double a1, double b1)
1823{
1824 SSE_COMI(ucomiss, s);
1825 SSE_COMI(ucomisd, d);
1826 SSE_COMI(comiss, s);
1827 SSE_COMI(comisd, d);
1828}
1829
1830#define CVT_OP_XMM(op)\
1831{\
1832 asm volatile (#op " %1, %0" : "=x" (r.dq) : "x" (a.dq));\
1833 printf("%-9s: a=%016llx%016llx r=%016llx%016llx\n",\
1834 #op,\
1835 a.q[1], a.q[0],\
1836 r.q[1], r.q[0]);\
1837}
1838
1839#define CVT_OP_XMM2MMX(op)\
1840{\
1841 asm volatile (#op " %1, %0" : "=y" (r.q[0]) : "x" (a.dq));\
1842 printf("%-9s: a=%016llx%016llx r=%016llx\n",\
1843 #op,\
1844 a.q[1], a.q[0],\
1845 r.q[0]);\
1846}
1847
1848#define CVT_OP_MMX2XMM(op)\
1849{\
1850 asm volatile (#op " %1, %0" : "=x" (r.dq) : "y" (a.q[0]));\
1851 printf("%-9s: a=%016llx r=%016llx%016llx\n",\
1852 #op,\
1853 a.q[0],\
1854 r.q[1], r.q[0]);\
1855}
1856
1857#define CVT_OP_REG2XMM(op)\
1858{\
1859 asm volatile (#op " %1, %0" : "=x" (r.dq) : "r" (a.l[0]));\
1860 printf("%-9s: a=%08x r=%016llx%016llx\n",\
1861 #op,\
1862 a.l[0],\
1863 r.q[1], r.q[0]);\
1864}
1865
1866#define CVT_OP_XMM2REG(op)\
1867{\
1868 asm volatile (#op " %1, %0" : "=r" (r.l[0]) : "x" (a.dq));\
1869 printf("%-9s: a=%016llx%016llx r=%08x\n",\
1870 #op,\
1871 a.q[1], a.q[0],\
1872 r.l[0]);\
1873}
1874
a4682cc2
FB
1875struct fpxstate {
1876 uint16_t fpuc;
1877 uint16_t fpus;
1878 uint16_t fptag;
1879 uint16_t fop;
1880 uint32_t fpuip;
1881 uint16_t cs_sel;
1882 uint16_t dummy0;
1883 uint32_t fpudp;
1884 uint16_t ds_sel;
1885 uint16_t dummy1;
1886 uint32_t mxcsr;
1887 uint32_t mxcsr_mask;
1888 uint8_t fpregs1[8 * 16];
1889 uint8_t xmm_regs[8 * 16];
1890 uint8_t dummy2[224];
1891};
1892
1893static struct fpxstate fpx_state __attribute__((aligned(16)));
1894static struct fpxstate fpx_state2 __attribute__((aligned(16)));
1895
1896void test_fxsave(void)
1897{
1898 struct fpxstate *fp = &fpx_state;
1899 struct fpxstate *fp2 = &fpx_state2;
1900 int i;
1901 XMMReg a, b;
1902 a.q[0] = test_values[0][0];
1903 a.q[1] = test_values[0][1];
1904 b.q[0] = test_values[1][0];
1905 b.q[1] = test_values[1][1];
1906
1907 asm("movdqa %2, %%xmm0\n"
1908 "movdqa %3, %%xmm7\n"
1909 " fld1\n"
1910 " fldpi\n"
1911 " fldln2\n"
1912 " fxsave %0\n"
1913 " fxrstor %0\n"
1914 " fxsave %1\n"
1915 " fninit\n"
1916 : "=m" (*(uint32_t *)fp2), "=m" (*(uint32_t *)fp)
1917 : "m" (a), "m" (b));
1918 printf("fpuc=%04x\n", fp->fpuc);
1919 printf("fpus=%04x\n", fp->fpus);
1920 printf("fptag=%04x\n", fp->fptag);
1921 for(i = 0; i < 3; i++) {
1922 printf("ST%d: %016llx %04x\n",
1923 i,
1924 *(uint64_t *)&fp->fpregs1[i * 16],
1925 *(uint16_t *)&fp->fpregs1[i * 16 + 8]);
1926 }
1927 printf("mxcsr=%08x\n", fp->mxcsr & 0x1f80);
1928 for(i = 0; i < 8; i++) {
1929 printf("xmm%d: %016llx%016llx\n",
1930 i,
1931 *(uint64_t *)&fp->xmm_regs[i * 16],
1932 *(uint64_t *)&fp->xmm_regs[i * 16 + 8]);
1933 }
1934}
1935
085339a1
FB
1936void test_sse(void)
1937{
1938 XMMReg r, a, b;
1939
1940 MMX_OP2(punpcklbw);
1941 MMX_OP2(punpcklwd);
1942 MMX_OP2(punpckldq);
1943 MMX_OP2(packsswb);
1944 MMX_OP2(pcmpgtb);
1945 MMX_OP2(pcmpgtw);
1946 MMX_OP2(pcmpgtd);
1947 MMX_OP2(packuswb);
1948 MMX_OP2(punpckhbw);
1949 MMX_OP2(punpckhwd);
1950 MMX_OP2(punpckhdq);
1951 MMX_OP2(packssdw);
1952 MMX_OP2(pcmpeqb);
1953 MMX_OP2(pcmpeqw);
1954 MMX_OP2(pcmpeqd);
1955
1956 MMX_OP2(paddq);
1957 MMX_OP2(pmullw);
1958 MMX_OP2(psubusb);
1959 MMX_OP2(psubusw);
1960 MMX_OP2(pminub);
1961 MMX_OP2(pand);
1962 MMX_OP2(paddusb);
1963 MMX_OP2(paddusw);
1964 MMX_OP2(pmaxub);
1965 MMX_OP2(pandn);
1966
1967 MMX_OP2(pmulhuw);
1968 MMX_OP2(pmulhw);
1969
1970 MMX_OP2(psubsb);
1971 MMX_OP2(psubsw);
1972 MMX_OP2(pminsw);
1973 MMX_OP2(por);
1974 MMX_OP2(paddsb);
1975 MMX_OP2(paddsw);
1976 MMX_OP2(pmaxsw);
1977 MMX_OP2(pxor);
1978 MMX_OP2(pmuludq);
1979 MMX_OP2(pmaddwd);
1980 MMX_OP2(psadbw);
1981 MMX_OP2(psubb);
1982 MMX_OP2(psubw);
1983 MMX_OP2(psubd);
1984 MMX_OP2(psubq);
1985 MMX_OP2(paddb);
1986 MMX_OP2(paddw);
1987 MMX_OP2(paddd);
1988
1989 MMX_OP2(pavgb);
1990 MMX_OP2(pavgw);
1991
1992 asm volatile ("pinsrw $1, %1, %0" : "=y" (r.q[0]) : "r" (0x12345678));
1993 printf("%-9s: r=%016llx\n", "pinsrw", r.q[0]);
1994
1995 asm volatile ("pinsrw $5, %1, %0" : "=x" (r.dq) : "r" (0x12345678));
1996 printf("%-9s: r=%016llx%016llx\n", "pinsrw", r.q[1], r.q[0]);
1997
1998 a.q[0] = test_values[0][0];
1999 a.q[1] = test_values[0][1];
2000 asm volatile ("pextrw $1, %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
2001 printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
2002
2003 asm volatile ("pextrw $5, %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
2004 printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
2005
2006 asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
2007 printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
2008
2009 asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
2010 printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
2011
2012 asm volatile ("emms");
2013
2014 SSE_OP2(punpcklqdq);
2015 SSE_OP2(punpckhqdq);
2016 SSE_OP2(andps);
2017 SSE_OP2(andpd);
2018 SSE_OP2(andnps);
2019 SSE_OP2(andnpd);
2020 SSE_OP2(orps);
2021 SSE_OP2(orpd);
2022 SSE_OP2(xorps);
2023 SSE_OP2(xorpd);
2024
2025 SSE_OP2(unpcklps);
2026 SSE_OP2(unpcklpd);
2027 SSE_OP2(unpckhps);
2028 SSE_OP2(unpckhpd);
2029
2030 PSHUF_OP(shufps, 0x78);
2031 PSHUF_OP(shufpd, 0x02);
2032
2033 PSHUF_OP(pshufd, 0x78);
2034 PSHUF_OP(pshuflw, 0x78);
2035 PSHUF_OP(pshufhw, 0x78);
2036
2037 SHIFT_OP(psrlw, 7);
2038 SHIFT_OP(psrlw, 16);
2039 SHIFT_OP(psraw, 7);
2040 SHIFT_OP(psraw, 16);
2041 SHIFT_OP(psllw, 7);
2042 SHIFT_OP(psllw, 16);
2043
2044 SHIFT_OP(psrld, 7);
2045 SHIFT_OP(psrld, 32);
2046 SHIFT_OP(psrad, 7);
2047 SHIFT_OP(psrad, 32);
2048 SHIFT_OP(pslld, 7);
2049 SHIFT_OP(pslld, 32);
2050
2051 SHIFT_OP(psrlq, 7);
2052 SHIFT_OP(psrlq, 32);
2053 SHIFT_OP(psllq, 7);
2054 SHIFT_OP(psllq, 32);
2055
2056 SHIFT_IM(psrldq, 16);
2057 SHIFT_IM(psrldq, 7);
2058 SHIFT_IM(pslldq, 16);
2059 SHIFT_IM(pslldq, 7);
2060
2061 MOVMSK(movmskps);
2062 MOVMSK(movmskpd);
2063
2064 /* FPU specific ops */
2065
2066 {
2067 uint32_t mxcsr;
2068 asm volatile("stmxcsr %0" : "=m" (mxcsr));
2069 printf("mxcsr=%08x\n", mxcsr & 0x1f80);
2070 asm volatile("ldmxcsr %0" : : "m" (mxcsr));
2071 }
2072
2073 test_sse_comi(2, -1);
2074 test_sse_comi(2, 2);
2075 test_sse_comi(2, 3);
2076
2077 a.s[0] = 2.7;
2078 a.s[1] = 3.4;
2079 a.s[2] = 4;
2080 a.s[3] = -6.3;
2081 b.s[0] = 45.7;
2082 b.s[1] = 353.4;
2083 b.s[2] = 4;
2084 b.s[3] = 56.3;
2085 SSE_OPS(add);
2086 SSE_OPS(mul);
2087 SSE_OPS(sub);
2088 SSE_OPS(min);
2089 SSE_OPS(div);
2090 SSE_OPS(max);
2091 SSE_OPS(sqrt);
2092 SSE_OPS(cmpeq);
2093 SSE_OPS(cmplt);
2094 SSE_OPS(cmple);
2095 SSE_OPS(cmpunord);
2096 SSE_OPS(cmpneq);
2097 SSE_OPS(cmpnlt);
2098 SSE_OPS(cmpnle);
2099 SSE_OPS(cmpord);
2100
2101 a.d[0] = 2.7;
2102 a.d[1] = -3.4;
2103 b.d[0] = 45.7;
2104 b.d[1] = -53.4;
2105 SSE_OPD(add);
2106 SSE_OPD(mul);
2107 SSE_OPD(sub);
2108 SSE_OPD(min);
2109 SSE_OPD(div);
2110 SSE_OPD(max);
2111 SSE_OPD(sqrt);
2112 SSE_OPD(cmpeq);
2113 SSE_OPD(cmplt);
2114 SSE_OPD(cmple);
2115 SSE_OPD(cmpunord);
2116 SSE_OPD(cmpneq);
2117 SSE_OPD(cmpnlt);
2118 SSE_OPD(cmpnle);
2119 SSE_OPD(cmpord);
2120
2121 /* float to float/int */
2122 a.s[0] = 2.7;
2123 a.s[1] = 3.4;
2124 a.s[2] = 4;
2125 a.s[3] = -6.3;
2126 CVT_OP_XMM(cvtps2pd);
2127 CVT_OP_XMM(cvtss2sd);
2128 CVT_OP_XMM2MMX(cvtps2pi);
2129 CVT_OP_XMM2MMX(cvttps2pi);
2130 CVT_OP_XMM2REG(cvtss2si);
2131 CVT_OP_XMM2REG(cvttss2si);
2132 CVT_OP_XMM(cvtps2dq);
2133 CVT_OP_XMM(cvttps2dq);
2134
2135 a.d[0] = 2.6;
2136 a.d[1] = -3.4;
2137 CVT_OP_XMM(cvtpd2ps);
2138 CVT_OP_XMM(cvtsd2ss);
2139 CVT_OP_XMM2MMX(cvtpd2pi);
2140 CVT_OP_XMM2MMX(cvttpd2pi);
2141 CVT_OP_XMM2REG(cvtsd2si);
2142 CVT_OP_XMM2REG(cvttsd2si);
2143 CVT_OP_XMM(cvtpd2dq);
2144 CVT_OP_XMM(cvttpd2dq);
2145
2146 /* int to float */
2147 a.l[0] = -6;
2148 a.l[1] = 2;
2149 a.l[2] = 100;
2150 a.l[3] = -60000;
2151 CVT_OP_MMX2XMM(cvtpi2ps);
2152 CVT_OP_MMX2XMM(cvtpi2pd);
2153 CVT_OP_REG2XMM(cvtsi2ss);
2154 CVT_OP_REG2XMM(cvtsi2sd);
2155 CVT_OP_XMM(cvtdq2ps);
2156 CVT_OP_XMM(cvtdq2pd);
2157
2158 /* XXX: test PNI insns */
2159#if 0
2160 SSE_OP2(movshdup);
2161#endif
a4682cc2 2162 asm volatile ("emms");
085339a1
FB
2163}
2164
2165#endif
2166
4d1135e4
FB
2167static void *call_end __init_call = NULL;
2168
2169int main(int argc, char **argv)
2170{
2171 void **ptr;
2172 void (*func)(void);
4b74fe1f 2173
4d1135e4
FB
2174 ptr = &call_start + 1;
2175 while (*ptr != NULL) {
2176 func = *ptr++;
2177 func();
2178 }
9d8e9c09 2179 test_bsx();
d57c4e01 2180 test_mul();
4d1135e4 2181 test_jcc();
9d8e9c09 2182 test_floats();
55480af8 2183 test_bcd();
1a9353d2 2184 test_xchg();
e1d4294a
FB
2185 test_string();
2186 test_misc();
6dbad63e
FB
2187 test_lea();
2188 test_segs();
e5918247 2189 test_code16();
acae4681 2190#ifdef TEST_VM86
3a27ad0b 2191 test_vm86();
acae4681 2192#endif
3a27ad0b
FB
2193 test_exceptions();
2194 test_self_modifying_code();
3ff0631e 2195 test_single_step();
61a8c4ec 2196 test_enter();
085339a1
FB
2197#ifdef TEST_SSE
2198 test_sse();
a4682cc2 2199 test_fxsave();
085339a1 2200#endif
4d1135e4
FB
2201 return 0;
2202}