blob: 6dc730d882fd853946bac3701d17dd08c4bf7f41 [file] [log] [blame]
bellard78d6da92003-10-27 23:55:20 +00001/*
2 * x86 CPU test
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard78d6da92003-10-27 23:55:20 +00004 * 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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard78d6da92003-10-27 23:55:20 +000018 */
bellard3a27ad02003-05-16 13:43:31 +000019#define _GNU_SOURCE
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010020#include "qemu/compiler.h"
bellard4d1135e2003-02-24 20:14:06 +000021#include <stdlib.h>
22#include <stdio.h>
bellarde3e86d52003-06-25 16:21:11 +000023#include <string.h>
bellard6dbad632003-03-16 18:05:05 +000024#include <inttypes.h>
bellard4d1135e2003-02-24 20:14:06 +000025#include <math.h>
bellard3a27ad02003-05-16 13:43:31 +000026#include <signal.h>
27#include <setjmp.h>
bellard070893f2003-07-13 17:27:19 +000028#include <errno.h>
bellard3a27ad02003-05-16 13:43:31 +000029#include <sys/ucontext.h>
30#include <sys/mman.h>
bellard4d1135e2003-02-24 20:14:06 +000031
bellard776f2222005-03-02 22:19:12 +000032#if !defined(__x86_64__)
bellarda5973fb2008-05-28 12:34:49 +000033//#define TEST_VM86
bellard776f2222005-03-02 22:19:12 +000034#define TEST_SEGS
35#endif
bellard3ff06312003-09-17 22:49:51 +000036//#define LINUX_VM86_IOPL_FIX
bellard791c22612003-12-02 21:55:34 +000037//#define TEST_P4_FLAGS
bellarda5973fb2008-05-28 12:34:49 +000038#ifdef __SSE__
bellard776f2222005-03-02 22:19:12 +000039#define TEST_SSE
40#define TEST_CMOV 1
41#define TEST_FCOMI 1
42#else
bellarda5973fb2008-05-28 12:34:49 +000043#undef TEST_SSE
44#define TEST_CMOV 1
45#define TEST_FCOMI 1
bellard776f2222005-03-02 22:19:12 +000046#endif
47
48#if defined(__x86_64__)
49#define FMT64X "%016lx"
50#define FMTLX "%016lx"
51#define X86_64_ONLY(x) x
52#else
bellard26a76462006-06-25 18:15:32 +000053#define FMT64X "%016" PRIx64
bellard776f2222005-03-02 22:19:12 +000054#define FMTLX "%08lx"
55#define X86_64_ONLY(x)
56#endif
57
58#ifdef TEST_VM86
59#include <asm/vm86.h>
60#endif
bellard5dd94882003-03-16 22:54:06 +000061
bellard4d1135e2003-02-24 20:14:06 +000062#define xglue(x, y) x ## y
63#define glue(x, y) xglue(x, y)
64#define stringify(s) tostring(s)
65#define tostring(s) #s
66
67#define CC_C 0x0001
68#define CC_P 0x0004
69#define CC_A 0x0010
70#define CC_Z 0x0040
71#define CC_S 0x0080
72#define CC_O 0x0800
73
bellard776f2222005-03-02 22:19:12 +000074#define __init_call __attribute__ ((unused,__section__ ("initcall")))
bellard4d1135e2003-02-24 20:14:06 +000075
bellard4b74fe12003-03-03 23:23:09 +000076#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
77
bellard776f2222005-03-02 22:19:12 +000078#if defined(__x86_64__)
79static inline long i2l(long v)
80{
81 return v | ((v ^ 0xabcd) << 32);
82}
83#else
84static inline long i2l(long v)
85{
86 return v;
87}
88#endif
89
bellard4d1135e2003-02-24 20:14:06 +000090#define OP add
91#include "test-i386.h"
92
93#define OP sub
94#include "test-i386.h"
95
96#define OP xor
97#include "test-i386.h"
98
99#define OP and
100#include "test-i386.h"
101
102#define OP or
103#include "test-i386.h"
104
105#define OP cmp
106#include "test-i386.h"
107
108#define OP adc
109#define OP_CC
110#include "test-i386.h"
111
112#define OP sbb
113#define OP_CC
114#include "test-i386.h"
115
116#define OP inc
117#define OP_CC
118#define OP1
119#include "test-i386.h"
120
121#define OP dec
122#define OP_CC
123#define OP1
124#include "test-i386.h"
125
126#define OP neg
127#define OP_CC
128#define OP1
129#include "test-i386.h"
130
131#define OP not
132#define OP_CC
133#define OP1
134#include "test-i386.h"
135
bellard4b74fe12003-03-03 23:23:09 +0000136#undef CC_MASK
137#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)
138
bellard379ca802003-02-24 23:43:02 +0000139#define OP shl
140#include "test-i386-shift.h"
141
142#define OP shr
143#include "test-i386-shift.h"
144
145#define OP sar
146#include "test-i386-shift.h"
147
148#define OP rol
149#include "test-i386-shift.h"
150
151#define OP ror
152#include "test-i386-shift.h"
153
154#define OP rcr
155#define OP_CC
156#include "test-i386-shift.h"
157
158#define OP rcl
159#define OP_CC
160#include "test-i386-shift.h"
161
bellardd57c4e02003-03-04 01:14:13 +0000162#define OP shld
163#define OP_SHIFTD
164#define OP_NOBYTE
165#include "test-i386-shift.h"
166
167#define OP shrd
168#define OP_SHIFTD
169#define OP_NOBYTE
170#include "test-i386-shift.h"
171
172/* XXX: should be more precise ? */
173#undef CC_MASK
174#define CC_MASK (CC_C)
175
176#define OP bt
177#define OP_NOBYTE
178#include "test-i386-shift.h"
179
180#define OP bts
181#define OP_NOBYTE
182#include "test-i386-shift.h"
183
184#define OP btr
185#define OP_NOBYTE
186#include "test-i386-shift.h"
187
188#define OP btc
189#define OP_NOBYTE
190#include "test-i386-shift.h"
bellard379ca802003-02-24 23:43:02 +0000191
bellard4d1135e2003-02-24 20:14:06 +0000192/* lea test (modrm support) */
bellard776f2222005-03-02 22:19:12 +0000193#define TEST_LEAQ(STR)\
bellard4d1135e2003-02-24 20:14:06 +0000194{\
bellard776f2222005-03-02 22:19:12 +0000195 asm("lea " STR ", %0"\
bellard4d1135e2003-02-24 20:14:06 +0000196 : "=r" (res)\
197 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
bellard776f2222005-03-02 22:19:12 +0000198 printf("lea %s = " FMTLX "\n", STR, res);\
199}
200
201#define TEST_LEA(STR)\
202{\
203 asm("lea " STR ", %0"\
204 : "=r" (res)\
205 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
206 printf("lea %s = " FMTLX "\n", STR, res);\
bellard4d1135e2003-02-24 20:14:06 +0000207}
208
209#define TEST_LEA16(STR)\
210{\
211 asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
212 : "=wq" (res)\
213 : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
bellard776f2222005-03-02 22:19:12 +0000214 printf("lea %s = %08lx\n", STR, res);\
bellard4d1135e2003-02-24 20:14:06 +0000215}
216
217
218void test_lea(void)
219{
bellard776f2222005-03-02 22:19:12 +0000220 long eax, ebx, ecx, edx, esi, edi, res;
221 eax = i2l(0x0001);
222 ebx = i2l(0x0002);
223 ecx = i2l(0x0004);
224 edx = i2l(0x0008);
225 esi = i2l(0x0010);
226 edi = i2l(0x0020);
bellard4d1135e2003-02-24 20:14:06 +0000227
228 TEST_LEA("0x4000");
229
230 TEST_LEA("(%%eax)");
231 TEST_LEA("(%%ebx)");
232 TEST_LEA("(%%ecx)");
233 TEST_LEA("(%%edx)");
234 TEST_LEA("(%%esi)");
235 TEST_LEA("(%%edi)");
236
237 TEST_LEA("0x40(%%eax)");
238 TEST_LEA("0x40(%%ebx)");
239 TEST_LEA("0x40(%%ecx)");
240 TEST_LEA("0x40(%%edx)");
241 TEST_LEA("0x40(%%esi)");
242 TEST_LEA("0x40(%%edi)");
243
244 TEST_LEA("0x4000(%%eax)");
245 TEST_LEA("0x4000(%%ebx)");
246 TEST_LEA("0x4000(%%ecx)");
247 TEST_LEA("0x4000(%%edx)");
248 TEST_LEA("0x4000(%%esi)");
249 TEST_LEA("0x4000(%%edi)");
250
251 TEST_LEA("(%%eax, %%ecx)");
252 TEST_LEA("(%%ebx, %%edx)");
253 TEST_LEA("(%%ecx, %%ecx)");
254 TEST_LEA("(%%edx, %%ecx)");
255 TEST_LEA("(%%esi, %%ecx)");
256 TEST_LEA("(%%edi, %%ecx)");
257
258 TEST_LEA("0x40(%%eax, %%ecx)");
259 TEST_LEA("0x4000(%%ebx, %%edx)");
260
261 TEST_LEA("(%%ecx, %%ecx, 2)");
262 TEST_LEA("(%%edx, %%ecx, 4)");
263 TEST_LEA("(%%esi, %%ecx, 8)");
264
265 TEST_LEA("(,%%eax, 2)");
266 TEST_LEA("(,%%ebx, 4)");
267 TEST_LEA("(,%%ecx, 8)");
268
269 TEST_LEA("0x40(,%%eax, 2)");
270 TEST_LEA("0x40(,%%ebx, 4)");
271 TEST_LEA("0x40(,%%ecx, 8)");
272
273
274 TEST_LEA("-10(%%ecx, %%ecx, 2)");
275 TEST_LEA("-10(%%edx, %%ecx, 4)");
276 TEST_LEA("-10(%%esi, %%ecx, 8)");
277
278 TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
279 TEST_LEA("0x4000(%%edx, %%ecx, 4)");
280 TEST_LEA("0x4000(%%esi, %%ecx, 8)");
281
bellard776f2222005-03-02 22:19:12 +0000282#if defined(__x86_64__)
283 TEST_LEAQ("0x4000");
284 TEST_LEAQ("0x4000(%%rip)");
285
286 TEST_LEAQ("(%%rax)");
287 TEST_LEAQ("(%%rbx)");
288 TEST_LEAQ("(%%rcx)");
289 TEST_LEAQ("(%%rdx)");
290 TEST_LEAQ("(%%rsi)");
291 TEST_LEAQ("(%%rdi)");
292
293 TEST_LEAQ("0x40(%%rax)");
294 TEST_LEAQ("0x40(%%rbx)");
295 TEST_LEAQ("0x40(%%rcx)");
296 TEST_LEAQ("0x40(%%rdx)");
297 TEST_LEAQ("0x40(%%rsi)");
298 TEST_LEAQ("0x40(%%rdi)");
299
300 TEST_LEAQ("0x4000(%%rax)");
301 TEST_LEAQ("0x4000(%%rbx)");
302 TEST_LEAQ("0x4000(%%rcx)");
303 TEST_LEAQ("0x4000(%%rdx)");
304 TEST_LEAQ("0x4000(%%rsi)");
305 TEST_LEAQ("0x4000(%%rdi)");
306
307 TEST_LEAQ("(%%rax, %%rcx)");
308 TEST_LEAQ("(%%rbx, %%rdx)");
309 TEST_LEAQ("(%%rcx, %%rcx)");
310 TEST_LEAQ("(%%rdx, %%rcx)");
311 TEST_LEAQ("(%%rsi, %%rcx)");
312 TEST_LEAQ("(%%rdi, %%rcx)");
313
314 TEST_LEAQ("0x40(%%rax, %%rcx)");
315 TEST_LEAQ("0x4000(%%rbx, %%rdx)");
316
317 TEST_LEAQ("(%%rcx, %%rcx, 2)");
318 TEST_LEAQ("(%%rdx, %%rcx, 4)");
319 TEST_LEAQ("(%%rsi, %%rcx, 8)");
320
321 TEST_LEAQ("(,%%rax, 2)");
322 TEST_LEAQ("(,%%rbx, 4)");
323 TEST_LEAQ("(,%%rcx, 8)");
324
325 TEST_LEAQ("0x40(,%%rax, 2)");
326 TEST_LEAQ("0x40(,%%rbx, 4)");
327 TEST_LEAQ("0x40(,%%rcx, 8)");
328
329
330 TEST_LEAQ("-10(%%rcx, %%rcx, 2)");
331 TEST_LEAQ("-10(%%rdx, %%rcx, 4)");
332 TEST_LEAQ("-10(%%rsi, %%rcx, 8)");
333
334 TEST_LEAQ("0x4000(%%rcx, %%rcx, 2)");
335 TEST_LEAQ("0x4000(%%rdx, %%rcx, 4)");
336 TEST_LEAQ("0x4000(%%rsi, %%rcx, 8)");
337#else
bellard4d1135e2003-02-24 20:14:06 +0000338 /* limited 16 bit addressing test */
339 TEST_LEA16("0x4000");
340 TEST_LEA16("(%%bx)");
341 TEST_LEA16("(%%si)");
342 TEST_LEA16("(%%di)");
343 TEST_LEA16("0x40(%%bx)");
344 TEST_LEA16("0x40(%%si)");
345 TEST_LEA16("0x40(%%di)");
346 TEST_LEA16("0x4000(%%bx)");
347 TEST_LEA16("0x4000(%%si)");
348 TEST_LEA16("(%%bx,%%si)");
349 TEST_LEA16("(%%bx,%%di)");
350 TEST_LEA16("0x40(%%bx,%%si)");
351 TEST_LEA16("0x40(%%bx,%%di)");
352 TEST_LEA16("0x4000(%%bx,%%si)");
353 TEST_LEA16("0x4000(%%bx,%%di)");
bellard776f2222005-03-02 22:19:12 +0000354#endif
bellard4d1135e2003-02-24 20:14:06 +0000355}
356
357#define TEST_JCC(JCC, v1, v2)\
358{\
bellard5dd94882003-03-16 22:54:06 +0000359 int res;\
bellard4d1135e2003-02-24 20:14:06 +0000360 asm("movl $1, %0\n\t"\
361 "cmpl %2, %1\n\t"\
bellard5dd94882003-03-16 22:54:06 +0000362 "j" JCC " 1f\n\t"\
bellard4d1135e2003-02-24 20:14:06 +0000363 "movl $0, %0\n\t"\
364 "1:\n\t"\
365 : "=r" (res)\
366 : "r" (v1), "r" (v2));\
bellard5dd94882003-03-16 22:54:06 +0000367 printf("%-10s %d\n", "j" JCC, res);\
368\
369 asm("movl $0, %0\n\t"\
370 "cmpl %2, %1\n\t"\
371 "set" JCC " %b0\n\t"\
372 : "=r" (res)\
373 : "r" (v1), "r" (v2));\
374 printf("%-10s %d\n", "set" JCC, res);\
375 if (TEST_CMOV) {\
bellard776f2222005-03-02 22:19:12 +0000376 long val = i2l(1);\
377 long res = i2l(0x12345678);\
378X86_64_ONLY(\
379 asm("cmpl %2, %1\n\t"\
380 "cmov" JCC "q %3, %0\n\t"\
bellard5dd94882003-03-16 22:54:06 +0000381 : "=r" (res)\
bellard776f2222005-03-02 22:19:12 +0000382 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
bellardd0570992005-03-03 01:13:19 +0000383 printf("%-10s R=" FMTLX "\n", "cmov" JCC "q", res);)\
bellard776f2222005-03-02 22:19:12 +0000384 asm("cmpl %2, %1\n\t"\
385 "cmov" JCC "l %k3, %k0\n\t"\
386 : "=r" (res)\
387 : "r" (v1), "r" (v2), "m" (val), "0" (res));\
388 printf("%-10s R=" FMTLX "\n", "cmov" JCC "l", res);\
389 asm("cmpl %2, %1\n\t"\
bellard5dd94882003-03-16 22:54:06 +0000390 "cmov" JCC "w %w3, %w0\n\t"\
391 : "=r" (res)\
bellard776f2222005-03-02 22:19:12 +0000392 : "r" (v1), "r" (v2), "r" (1), "0" (res));\
393 printf("%-10s R=" FMTLX "\n", "cmov" JCC "w", res);\
bellard5dd94882003-03-16 22:54:06 +0000394 } \
bellard4d1135e2003-02-24 20:14:06 +0000395}
396
397/* various jump tests */
398void test_jcc(void)
399{
bellard5dd94882003-03-16 22:54:06 +0000400 TEST_JCC("ne", 1, 1);
401 TEST_JCC("ne", 1, 0);
bellard4d1135e2003-02-24 20:14:06 +0000402
bellard5dd94882003-03-16 22:54:06 +0000403 TEST_JCC("e", 1, 1);
404 TEST_JCC("e", 1, 0);
bellard4d1135e2003-02-24 20:14:06 +0000405
bellard5dd94882003-03-16 22:54:06 +0000406 TEST_JCC("l", 1, 1);
407 TEST_JCC("l", 1, 0);
408 TEST_JCC("l", 1, -1);
bellard4d1135e2003-02-24 20:14:06 +0000409
bellard5dd94882003-03-16 22:54:06 +0000410 TEST_JCC("le", 1, 1);
411 TEST_JCC("le", 1, 0);
412 TEST_JCC("le", 1, -1);
bellard4d1135e2003-02-24 20:14:06 +0000413
bellard5dd94882003-03-16 22:54:06 +0000414 TEST_JCC("ge", 1, 1);
415 TEST_JCC("ge", 1, 0);
416 TEST_JCC("ge", -1, 1);
bellard4d1135e2003-02-24 20:14:06 +0000417
bellard5dd94882003-03-16 22:54:06 +0000418 TEST_JCC("g", 1, 1);
419 TEST_JCC("g", 1, 0);
420 TEST_JCC("g", 1, -1);
bellard4d1135e2003-02-24 20:14:06 +0000421
bellard5dd94882003-03-16 22:54:06 +0000422 TEST_JCC("b", 1, 1);
423 TEST_JCC("b", 1, 0);
424 TEST_JCC("b", 1, -1);
bellard4d1135e2003-02-24 20:14:06 +0000425
bellard5dd94882003-03-16 22:54:06 +0000426 TEST_JCC("be", 1, 1);
427 TEST_JCC("be", 1, 0);
428 TEST_JCC("be", 1, -1);
bellard4d1135e2003-02-24 20:14:06 +0000429
bellard5dd94882003-03-16 22:54:06 +0000430 TEST_JCC("ae", 1, 1);
431 TEST_JCC("ae", 1, 0);
432 TEST_JCC("ae", 1, -1);
bellard4d1135e2003-02-24 20:14:06 +0000433
bellard5dd94882003-03-16 22:54:06 +0000434 TEST_JCC("a", 1, 1);
435 TEST_JCC("a", 1, 0);
436 TEST_JCC("a", 1, -1);
bellard4d1135e2003-02-24 20:14:06 +0000437
438
bellard5dd94882003-03-16 22:54:06 +0000439 TEST_JCC("p", 1, 1);
440 TEST_JCC("p", 1, 0);
bellard4d1135e2003-02-24 20:14:06 +0000441
bellard5dd94882003-03-16 22:54:06 +0000442 TEST_JCC("np", 1, 1);
443 TEST_JCC("np", 1, 0);
bellard4d1135e2003-02-24 20:14:06 +0000444
bellard5dd94882003-03-16 22:54:06 +0000445 TEST_JCC("o", 0x7fffffff, 0);
446 TEST_JCC("o", 0x7fffffff, -1);
bellard4d1135e2003-02-24 20:14:06 +0000447
bellard5dd94882003-03-16 22:54:06 +0000448 TEST_JCC("no", 0x7fffffff, 0);
449 TEST_JCC("no", 0x7fffffff, -1);
bellard4d1135e2003-02-24 20:14:06 +0000450
bellard5dd94882003-03-16 22:54:06 +0000451 TEST_JCC("s", 0, 1);
452 TEST_JCC("s", 0, -1);
453 TEST_JCC("s", 0, 0);
bellard4d1135e2003-02-24 20:14:06 +0000454
bellard5dd94882003-03-16 22:54:06 +0000455 TEST_JCC("ns", 0, 1);
456 TEST_JCC("ns", 0, -1);
457 TEST_JCC("ns", 0, 0);
bellard4d1135e2003-02-24 20:14:06 +0000458}
459
bellarda5973fb2008-05-28 12:34:49 +0000460#define TEST_LOOP(insn) \
461{\
462 for(i = 0; i < sizeof(ecx_vals) / sizeof(long); i++) {\
463 ecx = ecx_vals[i];\
464 for(zf = 0; zf < 2; zf++) {\
465 asm("test %2, %2\n\t"\
466 "movl $1, %0\n\t"\
467 insn " 1f\n\t" \
468 "movl $0, %0\n\t"\
469 "1:\n\t"\
470 : "=a" (res)\
471 : "c" (ecx), "b" (!zf)); \
472 printf("%-10s ECX=" FMTLX " ZF=%ld r=%d\n", insn, ecx, zf, res); \
473 }\
474 }\
475}
476
477void test_loop(void)
478{
479 long ecx, zf;
480 const long ecx_vals[] = {
481 0,
482 1,
483 0x10000,
484 0x10001,
485#if defined(__x86_64__)
486 0x100000000L,
487 0x100000001L,
488#endif
489 };
490 int i, res;
491
balrog872ea0c2008-09-21 02:31:19 +0000492#if !defined(__x86_64__)
bellarda5973fb2008-05-28 12:34:49 +0000493 TEST_LOOP("jcxz");
494 TEST_LOOP("loopw");
495 TEST_LOOP("loopzw");
496 TEST_LOOP("loopnzw");
balrog872ea0c2008-09-21 02:31:19 +0000497#endif
bellarda5973fb2008-05-28 12:34:49 +0000498
499 TEST_LOOP("jecxz");
500 TEST_LOOP("loopl");
501 TEST_LOOP("loopzl");
502 TEST_LOOP("loopnzl");
503}
504
bellard4b74fe12003-03-03 23:23:09 +0000505#undef CC_MASK
bellard791c22612003-12-02 21:55:34 +0000506#ifdef TEST_P4_FLAGS
507#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
508#else
bellard4b74fe12003-03-03 23:23:09 +0000509#define CC_MASK (CC_O | CC_C)
bellard791c22612003-12-02 21:55:34 +0000510#endif
bellard4b74fe12003-03-03 23:23:09 +0000511
512#define OP mul
513#include "test-i386-muldiv.h"
514
515#define OP imul
516#include "test-i386-muldiv.h"
517
ths5fafdf22007-09-16 21:08:06 +0000518void test_imulw2(long op0, long op1)
bellard4b74fe12003-03-03 23:23:09 +0000519{
bellard776f2222005-03-02 22:19:12 +0000520 long res, s1, s0, flags;
bellard4b74fe12003-03-03 23:23:09 +0000521 s0 = op0;
522 s1 = op1;
523 res = s0;
524 flags = 0;
bellardacae4682005-01-03 23:25:56 +0000525 asm volatile ("push %4\n\t"
bellard4b74fe12003-03-03 23:23:09 +0000526 "popf\n\t"
ths5fafdf22007-09-16 21:08:06 +0000527 "imulw %w2, %w0\n\t"
bellard4b74fe12003-03-03 23:23:09 +0000528 "pushf\n\t"
bellard776f2222005-03-02 22:19:12 +0000529 "pop %1\n\t"
bellard4b74fe12003-03-03 23:23:09 +0000530 : "=q" (res), "=g" (flags)
531 : "q" (s1), "0" (res), "1" (flags));
bellard776f2222005-03-02 22:19:12 +0000532 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
bellard4b74fe12003-03-03 23:23:09 +0000533 "imulw", s0, s1, res, flags & CC_MASK);
534}
535
ths5fafdf22007-09-16 21:08:06 +0000536void test_imull2(long op0, long op1)
bellard4b74fe12003-03-03 23:23:09 +0000537{
bellard776f2222005-03-02 22:19:12 +0000538 long res, s1, s0, flags;
bellard4b74fe12003-03-03 23:23:09 +0000539 s0 = op0;
540 s1 = op1;
541 res = s0;
542 flags = 0;
bellardacae4682005-01-03 23:25:56 +0000543 asm volatile ("push %4\n\t"
bellard4b74fe12003-03-03 23:23:09 +0000544 "popf\n\t"
ths5fafdf22007-09-16 21:08:06 +0000545 "imull %k2, %k0\n\t"
bellard4b74fe12003-03-03 23:23:09 +0000546 "pushf\n\t"
bellard776f2222005-03-02 22:19:12 +0000547 "pop %1\n\t"
bellard4b74fe12003-03-03 23:23:09 +0000548 : "=q" (res), "=g" (flags)
549 : "q" (s1), "0" (res), "1" (flags));
bellard776f2222005-03-02 22:19:12 +0000550 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
bellard4b74fe12003-03-03 23:23:09 +0000551 "imull", s0, s1, res, flags & CC_MASK);
552}
553
bellard776f2222005-03-02 22:19:12 +0000554#if defined(__x86_64__)
ths5fafdf22007-09-16 21:08:06 +0000555void test_imulq2(long op0, long op1)
bellard776f2222005-03-02 22:19:12 +0000556{
557 long res, s1, s0, flags;
558 s0 = op0;
559 s1 = op1;
560 res = s0;
561 flags = 0;
562 asm volatile ("push %4\n\t"
563 "popf\n\t"
ths5fafdf22007-09-16 21:08:06 +0000564 "imulq %2, %0\n\t"
bellard776f2222005-03-02 22:19:12 +0000565 "pushf\n\t"
566 "pop %1\n\t"
567 : "=q" (res), "=g" (flags)
568 : "q" (s1), "0" (res), "1" (flags));
569 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
570 "imulq", s0, s1, res, flags & CC_MASK);
571}
572#endif
573
574#define TEST_IMUL_IM(size, rsize, op0, op1)\
bellardb5075d22004-04-22 21:37:55 +0000575{\
bellard776f2222005-03-02 22:19:12 +0000576 long res, flags, s1;\
bellardb5075d22004-04-22 21:37:55 +0000577 flags = 0;\
578 res = 0;\
bellard776f2222005-03-02 22:19:12 +0000579 s1 = op1;\
bellardacae4682005-01-03 23:25:56 +0000580 asm volatile ("push %3\n\t"\
bellardb5075d22004-04-22 21:37:55 +0000581 "popf\n\t"\
bellard776f2222005-03-02 22:19:12 +0000582 "imul" size " $" #op0 ", %" rsize "2, %" rsize "0\n\t" \
bellardb5075d22004-04-22 21:37:55 +0000583 "pushf\n\t"\
bellard776f2222005-03-02 22:19:12 +0000584 "pop %1\n\t"\
bellardb5075d22004-04-22 21:37:55 +0000585 : "=r" (res), "=g" (flags)\
bellard776f2222005-03-02 22:19:12 +0000586 : "r" (s1), "1" (flags), "0" (res));\
587 printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",\
588 "imul" size " im", (long)op0, (long)op1, res, flags & CC_MASK);\
bellardb5075d22004-04-22 21:37:55 +0000589}
590
591
bellard791c22612003-12-02 21:55:34 +0000592#undef CC_MASK
593#define CC_MASK (0)
594
595#define OP div
596#include "test-i386-muldiv.h"
597
598#define OP idiv
599#include "test-i386-muldiv.h"
600
bellard4b74fe12003-03-03 23:23:09 +0000601void test_mul(void)
602{
603 test_imulb(0x1234561d, 4);
604 test_imulb(3, -4);
605 test_imulb(0x80, 0x80);
606 test_imulb(0x10, 0x10);
607
608 test_imulw(0, 0x1234001d, 45);
609 test_imulw(0, 23, -45);
610 test_imulw(0, 0x8000, 0x8000);
611 test_imulw(0, 0x100, 0x100);
612
613 test_imull(0, 0x1234001d, 45);
614 test_imull(0, 23, -45);
615 test_imull(0, 0x80000000, 0x80000000);
616 test_imull(0, 0x10000, 0x10000);
617
618 test_mulb(0x1234561d, 4);
619 test_mulb(3, -4);
620 test_mulb(0x80, 0x80);
621 test_mulb(0x10, 0x10);
622
623 test_mulw(0, 0x1234001d, 45);
624 test_mulw(0, 23, -45);
625 test_mulw(0, 0x8000, 0x8000);
626 test_mulw(0, 0x100, 0x100);
627
628 test_mull(0, 0x1234001d, 45);
629 test_mull(0, 23, -45);
630 test_mull(0, 0x80000000, 0x80000000);
631 test_mull(0, 0x10000, 0x10000);
632
633 test_imulw2(0x1234001d, 45);
634 test_imulw2(23, -45);
635 test_imulw2(0x8000, 0x8000);
636 test_imulw2(0x100, 0x100);
637
638 test_imull2(0x1234001d, 45);
639 test_imull2(23, -45);
640 test_imull2(0x80000000, 0x80000000);
641 test_imull2(0x10000, 0x10000);
642
bellardb5075d22004-04-22 21:37:55 +0000643 TEST_IMUL_IM("w", "w", 45, 0x1234);
644 TEST_IMUL_IM("w", "w", -45, 23);
645 TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
646 TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);
647
bellard776f2222005-03-02 22:19:12 +0000648 TEST_IMUL_IM("l", "k", 45, 0x1234);
649 TEST_IMUL_IM("l", "k", -45, 23);
650 TEST_IMUL_IM("l", "k", 0x8000, 0x80000000);
651 TEST_IMUL_IM("l", "k", 0x7fff, 0x1000);
bellardb5075d22004-04-22 21:37:55 +0000652
bellard4b74fe12003-03-03 23:23:09 +0000653 test_idivb(0x12341678, 0x127e);
654 test_idivb(0x43210123, -5);
655 test_idivb(0x12340004, -1);
656
657 test_idivw(0, 0x12345678, 12347);
658 test_idivw(0, -23223, -45);
659 test_idivw(0, 0x12348000, -1);
660 test_idivw(0x12343, 0x12345678, 0x81238567);
661
662 test_idivl(0, 0x12345678, 12347);
663 test_idivl(0, -233223, -45);
664 test_idivl(0, 0x80000000, -1);
665 test_idivl(0x12343, 0x12345678, 0x81234567);
666
667 test_divb(0x12341678, 0x127e);
668 test_divb(0x43210123, -5);
669 test_divb(0x12340004, -1);
670
671 test_divw(0, 0x12345678, 12347);
672 test_divw(0, -23223, -45);
673 test_divw(0, 0x12348000, -1);
674 test_divw(0x12343, 0x12345678, 0x81238567);
675
676 test_divl(0, 0x12345678, 12347);
677 test_divl(0, -233223, -45);
678 test_divl(0, 0x80000000, -1);
679 test_divl(0x12343, 0x12345678, 0x81234567);
bellard776f2222005-03-02 22:19:12 +0000680
681#if defined(__x86_64__)
682 test_imulq(0, 0x1234001d1234001d, 45);
683 test_imulq(0, 23, -45);
684 test_imulq(0, 0x8000000000000000, 0x8000000000000000);
685 test_imulq(0, 0x100000000, 0x100000000);
686
687 test_mulq(0, 0x1234001d1234001d, 45);
688 test_mulq(0, 23, -45);
689 test_mulq(0, 0x8000000000000000, 0x8000000000000000);
690 test_mulq(0, 0x100000000, 0x100000000);
691
692 test_imulq2(0x1234001d1234001d, 45);
693 test_imulq2(23, -45);
694 test_imulq2(0x8000000000000000, 0x8000000000000000);
695 test_imulq2(0x100000000, 0x100000000);
696
697 TEST_IMUL_IM("q", "", 45, 0x12341234);
698 TEST_IMUL_IM("q", "", -45, 23);
699 TEST_IMUL_IM("q", "", 0x8000, 0x8000000000000000);
700 TEST_IMUL_IM("q", "", 0x7fff, 0x10000000);
701
702 test_idivq(0, 0x12345678abcdef, 12347);
703 test_idivq(0, -233223, -45);
704 test_idivq(0, 0x8000000000000000, -1);
705 test_idivq(0x12343, 0x12345678, 0x81234567);
706
707 test_divq(0, 0x12345678abcdef, 12347);
708 test_divq(0, -233223, -45);
709 test_divq(0, 0x8000000000000000, -1);
710 test_divq(0x12343, 0x12345678, 0x81234567);
711#endif
bellard4b74fe12003-03-03 23:23:09 +0000712}
713
bellard9d8e9c02003-03-05 20:57:02 +0000714#define TEST_BSX(op, size, op0)\
715{\
bellard776f2222005-03-02 22:19:12 +0000716 long res, val, resz;\
bellard9d8e9c02003-03-05 20:57:02 +0000717 val = op0;\
bellard776f2222005-03-02 22:19:12 +0000718 asm("xor %1, %1\n"\
719 "mov $0x12345678, %0\n"\
bellard7f5e1452004-05-16 16:02:40 +0000720 #op " %" size "2, %" size "0 ; setz %b1" \
balrog7058bfa2008-09-21 02:34:50 +0000721 : "=&r" (res), "=&q" (resz)\
722 : "r" (val));\
bellard776f2222005-03-02 22:19:12 +0000723 printf("%-10s A=" FMTLX " R=" FMTLX " %ld\n", #op, val, res, resz);\
bellard9d8e9c02003-03-05 20:57:02 +0000724}
725
726void test_bsx(void)
727{
728 TEST_BSX(bsrw, "w", 0);
729 TEST_BSX(bsrw, "w", 0x12340128);
bellard9d8e9c02003-03-05 20:57:02 +0000730 TEST_BSX(bsfw, "w", 0);
731 TEST_BSX(bsfw, "w", 0x12340128);
bellard776f2222005-03-02 22:19:12 +0000732 TEST_BSX(bsrl, "k", 0);
733 TEST_BSX(bsrl, "k", 0x00340128);
734 TEST_BSX(bsfl, "k", 0);
735 TEST_BSX(bsfl, "k", 0x00340128);
736#if defined(__x86_64__)
737 TEST_BSX(bsrq, "", 0);
738 TEST_BSX(bsrq, "", 0x003401281234);
739 TEST_BSX(bsfq, "", 0);
740 TEST_BSX(bsfq, "", 0x003401281234);
741#endif
bellard9d8e9c02003-03-05 20:57:02 +0000742}
743
bellard55480af2003-03-16 11:29:17 +0000744/**********************************************/
745
bellard86bd2ca2005-03-20 10:40:15 +0000746union float64u {
747 double d;
748 uint64_t l;
749};
750
bellardbe98f1f2007-11-11 14:43:13 +0000751union float64u q_nan = { .l = 0xFFF8000000000000LL };
752union float64u s_nan = { .l = 0xFFF0000000000000LL };
bellard86bd2ca2005-03-20 10:40:15 +0000753
bellard9d8e9c02003-03-05 20:57:02 +0000754void test_fops(double a, double b)
755{
756 printf("a=%f b=%f a+b=%f\n", a, b, a + b);
757 printf("a=%f b=%f a-b=%f\n", a, b, a - b);
758 printf("a=%f b=%f a*b=%f\n", a, b, a * b);
759 printf("a=%f b=%f a/b=%f\n", a, b, a / b);
760 printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
761 printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
762 printf("a=%f sin(a)=%f\n", a, sin(a));
763 printf("a=%f cos(a)=%f\n", a, cos(a));
764 printf("a=%f tan(a)=%f\n", a, tan(a));
765 printf("a=%f log(a)=%f\n", a, log(a));
766 printf("a=%f exp(a)=%f\n", a, exp(a));
767 printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
768 /* just to test some op combining */
769 printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
770 printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
771 printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));
772
773}
774
bellard86bd2ca2005-03-20 10:40:15 +0000775void fpu_clear_exceptions(void)
776{
Stefan Weil541dc0d2011-08-31 12:38:01 +0200777 struct QEMU_PACKED {
bellard86bd2ca2005-03-20 10:40:15 +0000778 uint16_t fpuc;
779 uint16_t dummy1;
780 uint16_t fpus;
781 uint16_t dummy2;
782 uint16_t fptag;
783 uint16_t dummy3;
784 uint32_t ignored[4];
785 long double fpregs[8];
786 } float_env32;
ths3b46e622007-09-17 08:09:54 +0000787
Catalin Patulea49cdaea2012-10-22 19:18:35 -0400788 asm volatile ("fnstenv %0\n" : "=m" (float_env32));
bellard86bd2ca2005-03-20 10:40:15 +0000789 float_env32.fpus &= ~0x7f;
790 asm volatile ("fldenv %0\n" : : "m" (float_env32));
791}
792
793/* XXX: display exception bits when supported */
794#define FPUS_EMASK 0x0000
795//#define FPUS_EMASK 0x007f
796
bellard9d8e9c02003-03-05 20:57:02 +0000797void test_fcmp(double a, double b)
798{
bellard86bd2ca2005-03-20 10:40:15 +0000799 long eflags, fpus;
800
801 fpu_clear_exceptions();
802 asm("fcom %2\n"
803 "fstsw %%ax\n"
804 : "=a" (fpus)
805 : "t" (a), "u" (b));
Stefan Weilb2bedb22011-09-12 22:33:01 +0200806 printf("fcom(%f %f)=%04lx\n",
bellard86bd2ca2005-03-20 10:40:15 +0000807 a, b, fpus & (0x4500 | FPUS_EMASK));
808 fpu_clear_exceptions();
809 asm("fucom %2\n"
810 "fstsw %%ax\n"
811 : "=a" (fpus)
812 : "t" (a), "u" (b));
ths5fafdf22007-09-16 21:08:06 +0000813 printf("fucom(%f %f)=%04lx\n",
bellard86bd2ca2005-03-20 10:40:15 +0000814 a, b, fpus & (0x4500 | FPUS_EMASK));
bellard03bfca92003-05-29 20:06:57 +0000815 if (TEST_FCOMI) {
bellard03bfca92003-05-29 20:06:57 +0000816 /* test f(u)comi instruction */
bellard86bd2ca2005-03-20 10:40:15 +0000817 fpu_clear_exceptions();
818 asm("fcomi %3, %2\n"
819 "fstsw %%ax\n"
bellard03bfca92003-05-29 20:06:57 +0000820 "pushf\n"
821 "pop %0\n"
bellard86bd2ca2005-03-20 10:40:15 +0000822 : "=r" (eflags), "=a" (fpus)
bellard03bfca92003-05-29 20:06:57 +0000823 : "t" (a), "u" (b));
ths5fafdf22007-09-16 21:08:06 +0000824 printf("fcomi(%f %f)=%04lx %02lx\n",
bellard86bd2ca2005-03-20 10:40:15 +0000825 a, b, fpus & FPUS_EMASK, eflags & (CC_Z | CC_P | CC_C));
826 fpu_clear_exceptions();
827 asm("fucomi %3, %2\n"
828 "fstsw %%ax\n"
829 "pushf\n"
830 "pop %0\n"
831 : "=r" (eflags), "=a" (fpus)
832 : "t" (a), "u" (b));
ths5fafdf22007-09-16 21:08:06 +0000833 printf("fucomi(%f %f)=%04lx %02lx\n",
bellard86bd2ca2005-03-20 10:40:15 +0000834 a, b, fpus & FPUS_EMASK, eflags & (CC_Z | CC_P | CC_C));
bellard03bfca92003-05-29 20:06:57 +0000835 }
bellard86bd2ca2005-03-20 10:40:15 +0000836 fpu_clear_exceptions();
bellardb2a8e592006-06-19 22:42:57 +0000837 asm volatile("fxam\n"
838 "fstsw %%ax\n"
839 : "=a" (fpus)
840 : "t" (a));
841 printf("fxam(%f)=%04lx\n", a, fpus & 0x4700);
842 fpu_clear_exceptions();
bellard9d8e9c02003-03-05 20:57:02 +0000843}
844
845void test_fcvt(double a)
846{
847 float fa;
848 long double la;
bellardea768642003-06-15 19:36:33 +0000849 int16_t fpuc;
850 int i;
851 int64_t lla;
852 int ia;
853 int16_t wa;
854 double ra;
bellard9d8e9c02003-03-05 20:57:02 +0000855
856 fa = a;
857 la = a;
858 printf("(float)%f = %f\n", a, fa);
859 printf("(long double)%f = %Lf\n", a, la);
bellard776f2222005-03-02 22:19:12 +0000860 printf("a=" FMT64X "\n", *(uint64_t *)&a);
ths5fafdf22007-09-16 21:08:06 +0000861 printf("la=" FMT64X " %04x\n", *(uint64_t *)&la,
bellardc5e98152003-03-05 22:24:26 +0000862 *(unsigned short *)((char *)(&la) + 8));
bellardea768642003-06-15 19:36:33 +0000863
864 /* test all roundings */
865 asm volatile ("fstcw %0" : "=m" (fpuc));
866 for(i=0;i<4;i++) {
bellardbe98f1f2007-11-11 14:43:13 +0000867 uint16_t val16;
868 val16 = (fpuc & ~0x0c00) | (i << 10);
869 asm volatile ("fldcw %0" : : "m" (val16));
bellardea768642003-06-15 19:36:33 +0000870 asm volatile ("fist %0" : "=m" (wa) : "t" (a));
871 asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
872 asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
873 asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
874 asm volatile ("fldcw %0" : : "m" (fpuc));
875 printf("(short)a = %d\n", wa);
876 printf("(int)a = %d\n", ia);
bellard776f2222005-03-02 22:19:12 +0000877 printf("(int64_t)a = " FMT64X "\n", lla);
bellardea768642003-06-15 19:36:33 +0000878 printf("rint(a) = %f\n", ra);
879 }
bellard9d8e9c02003-03-05 20:57:02 +0000880}
881
882#define TEST(N) \
883 asm("fld" #N : "=t" (a)); \
884 printf("fld" #N "= %f\n", a);
885
886void test_fconst(void)
887{
888 double a;
889 TEST(1);
890 TEST(l2t);
891 TEST(l2e);
892 TEST(pi);
893 TEST(lg2);
894 TEST(ln2);
895 TEST(z);
896}
897
bellardc5e98152003-03-05 22:24:26 +0000898void test_fbcd(double a)
899{
900 unsigned short bcd[5];
901 double b;
902
903 asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
904 asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
ths5fafdf22007-09-16 21:08:06 +0000905 printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n",
bellardc5e98152003-03-05 22:24:26 +0000906 a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
907}
908
bellard6b2b6112004-02-25 23:34:07 +0000909#define TEST_ENV(env, save, restore)\
bellard03bfca92003-05-29 20:06:57 +0000910{\
911 memset((env), 0xaa, sizeof(*(env)));\
bellard6b2b6112004-02-25 23:34:07 +0000912 for(i=0;i<5;i++)\
913 asm volatile ("fldl %0" : : "m" (dtab[i]));\
bellard085339a2005-01-08 18:54:41 +0000914 asm volatile (save " %0\n" : : "m" (*(env)));\
915 asm volatile (restore " %0\n": : "m" (*(env)));\
bellard6b2b6112004-02-25 23:34:07 +0000916 for(i=0;i<5;i++)\
917 asm volatile ("fstpl %0" : "=m" (rtab[i]));\
918 for(i=0;i<5;i++)\
919 printf("res[%d]=%f\n", i, rtab[i]);\
bellard03bfca92003-05-29 20:06:57 +0000920 printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
921 (env)->fpuc,\
922 (env)->fpus & 0xff00,\
923 (env)->fptag);\
bellard03bfca92003-05-29 20:06:57 +0000924}
925
926void test_fenv(void)
927{
Stefan Weil541dc0d2011-08-31 12:38:01 +0200928 struct QEMU_PACKED {
bellard03bfca92003-05-29 20:06:57 +0000929 uint16_t fpuc;
930 uint16_t dummy1;
931 uint16_t fpus;
932 uint16_t dummy2;
933 uint16_t fptag;
934 uint16_t dummy3;
935 uint32_t ignored[4];
936 long double fpregs[8];
937 } float_env32;
Stefan Weil541dc0d2011-08-31 12:38:01 +0200938 struct QEMU_PACKED {
bellard03bfca92003-05-29 20:06:57 +0000939 uint16_t fpuc;
940 uint16_t fpus;
941 uint16_t fptag;
942 uint16_t ignored[4];
943 long double fpregs[8];
944 } float_env16;
bellard6b2b6112004-02-25 23:34:07 +0000945 double dtab[8];
946 double rtab[8];
947 int i;
bellard03bfca92003-05-29 20:06:57 +0000948
bellard6b2b6112004-02-25 23:34:07 +0000949 for(i=0;i<8;i++)
950 dtab[i] = i + 1;
951
952 TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
953 TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
954 TEST_ENV(&float_env32, "fnstenv", "fldenv");
955 TEST_ENV(&float_env32, "fnsave", "frstor");
bellard665656a2004-06-12 11:38:00 +0000956
957 /* test for ffree */
958 for(i=0;i<5;i++)
959 asm volatile ("fldl %0" : : "m" (dtab[i]));
960 asm volatile("ffree %st(2)");
961 asm volatile ("fnstenv %0\n" : : "m" (float_env32));
962 asm volatile ("fninit");
963 printf("fptag=%04x\n", float_env32.fptag);
bellard03bfca92003-05-29 20:06:57 +0000964}
965
bellard75175022003-11-16 20:18:52 +0000966
967#define TEST_FCMOV(a, b, eflags, CC)\
968{\
969 double res;\
970 asm("push %3\n"\
971 "popf\n"\
972 "fcmov" CC " %2, %0\n"\
973 : "=t" (res)\
974 : "0" (a), "u" (b), "g" (eflags));\
bellard776f2222005-03-02 22:19:12 +0000975 printf("fcmov%s eflags=0x%04lx-> %f\n", \
976 CC, (long)eflags, res);\
bellard75175022003-11-16 20:18:52 +0000977}
978
979void test_fcmov(void)
980{
981 double a, b;
bellard776f2222005-03-02 22:19:12 +0000982 long eflags, i;
bellard75175022003-11-16 20:18:52 +0000983
984 a = 1.0;
985 b = 2.0;
986 for(i = 0; i < 4; i++) {
987 eflags = 0;
988 if (i & 1)
989 eflags |= CC_C;
990 if (i & 2)
991 eflags |= CC_Z;
992 TEST_FCMOV(a, b, eflags, "b");
993 TEST_FCMOV(a, b, eflags, "e");
994 TEST_FCMOV(a, b, eflags, "be");
995 TEST_FCMOV(a, b, eflags, "nb");
996 TEST_FCMOV(a, b, eflags, "ne");
997 TEST_FCMOV(a, b, eflags, "nbe");
998 }
999 TEST_FCMOV(a, b, 0, "u");
bellard9cdf7572003-11-19 22:12:47 +00001000 TEST_FCMOV(a, b, CC_P, "u");
1001 TEST_FCMOV(a, b, 0, "nu");
bellard75175022003-11-16 20:18:52 +00001002 TEST_FCMOV(a, b, CC_P, "nu");
1003}
1004
bellard9d8e9c02003-03-05 20:57:02 +00001005void test_floats(void)
1006{
1007 test_fops(2, 3);
1008 test_fops(1.4, -5);
1009 test_fcmp(2, -1);
1010 test_fcmp(2, 2);
1011 test_fcmp(2, 3);
bellard86bd2ca2005-03-20 10:40:15 +00001012 test_fcmp(2, q_nan.d);
1013 test_fcmp(q_nan.d, -1);
bellardb2a8e592006-06-19 22:42:57 +00001014 test_fcmp(-1.0/0.0, -1);
1015 test_fcmp(1.0/0.0, -1);
bellardea768642003-06-15 19:36:33 +00001016 test_fcvt(0.5);
1017 test_fcvt(-0.5);
bellard9d8e9c02003-03-05 20:57:02 +00001018 test_fcvt(1.0/7.0);
1019 test_fcvt(-1.0/9.0);
bellardea768642003-06-15 19:36:33 +00001020 test_fcvt(32768);
1021 test_fcvt(-1e20);
bellardb2a8e592006-06-19 22:42:57 +00001022 test_fcvt(-1.0/0.0);
1023 test_fcvt(1.0/0.0);
1024 test_fcvt(q_nan.d);
bellard9d8e9c02003-03-05 20:57:02 +00001025 test_fconst();
bellardbe98f1f2007-11-11 14:43:13 +00001026 test_fbcd(1234567890123456.0);
1027 test_fbcd(-123451234567890.0);
bellard03bfca92003-05-29 20:06:57 +00001028 test_fenv();
bellard75175022003-11-16 20:18:52 +00001029 if (TEST_CMOV) {
1030 test_fcmov();
1031 }
bellard9d8e9c02003-03-05 20:57:02 +00001032}
bellard4b74fe12003-03-03 23:23:09 +00001033
bellard55480af2003-03-16 11:29:17 +00001034/**********************************************/
bellard776f2222005-03-02 22:19:12 +00001035#if !defined(__x86_64__)
bellard55480af2003-03-16 11:29:17 +00001036
1037#define TEST_BCD(op, op0, cc_in, cc_mask)\
1038{\
1039 int res, flags;\
1040 res = op0;\
1041 flags = cc_in;\
1042 asm ("push %3\n\t"\
1043 "popf\n\t"\
1044 #op "\n\t"\
1045 "pushf\n\t"\
bellard776f2222005-03-02 22:19:12 +00001046 "pop %1\n\t"\
bellard55480af2003-03-16 11:29:17 +00001047 : "=a" (res), "=g" (flags)\
1048 : "0" (res), "1" (flags));\
1049 printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
1050 #op, op0, res, cc_in, flags & cc_mask);\
1051}
1052
1053void test_bcd(void)
1054{
1055 TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1056 TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1057 TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1058 TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1059 TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1060 TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1061 TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1062 TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1063 TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1064 TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1065 TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1066 TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1067 TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1068
1069 TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1070 TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1071 TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1072 TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1073 TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1074 TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1075 TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1076 TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1077 TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1078 TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1079 TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1080 TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1081 TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
1082
1083 TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
1084 TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
1085 TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
1086 TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
1087 TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
1088 TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
1089 TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
1090 TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
ths3b46e622007-09-17 08:09:54 +00001091
bellard55480af2003-03-16 11:29:17 +00001092 TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
1093 TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
1094 TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
1095 TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
1096 TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
1097 TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
1098 TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
1099 TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));
1100
1101 TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
1102 TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
1103}
bellard776f2222005-03-02 22:19:12 +00001104#endif
bellard55480af2003-03-16 11:29:17 +00001105
bellarde5918242003-03-22 15:20:50 +00001106#define TEST_XCHG(op, size, opconst)\
1107{\
bellard776f2222005-03-02 22:19:12 +00001108 long op0, op1;\
1109 op0 = i2l(0x12345678);\
1110 op1 = i2l(0xfbca7654);\
bellarde5918242003-03-22 15:20:50 +00001111 asm(#op " %" size "0, %" size "1" \
1112 : "=q" (op0), opconst (op1) \
bellard8aadfbf2008-01-31 14:56:10 +00001113 : "0" (op0));\
bellard776f2222005-03-02 22:19:12 +00001114 printf("%-10s A=" FMTLX " B=" FMTLX "\n",\
bellarde5918242003-03-22 15:20:50 +00001115 #op, op0, op1);\
1116}
1117
1118#define TEST_CMPXCHG(op, size, opconst, eax)\
1119{\
bellard776f2222005-03-02 22:19:12 +00001120 long op0, op1, op2;\
1121 op0 = i2l(0x12345678);\
1122 op1 = i2l(0xfbca7654);\
1123 op2 = i2l(eax);\
bellarde5918242003-03-22 15:20:50 +00001124 asm(#op " %" size "0, %" size "1" \
1125 : "=q" (op0), opconst (op1) \
bellard8aadfbf2008-01-31 14:56:10 +00001126 : "0" (op0), "a" (op2));\
bellard776f2222005-03-02 22:19:12 +00001127 printf("%-10s EAX=" FMTLX " A=" FMTLX " C=" FMTLX "\n",\
1128 #op, op2, op0, op1);\
bellarde5918242003-03-22 15:20:50 +00001129}
1130
1131void test_xchg(void)
1132{
bellard776f2222005-03-02 22:19:12 +00001133#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001134 TEST_XCHG(xchgq, "", "+q");
bellard776f2222005-03-02 22:19:12 +00001135#endif
bellard8aadfbf2008-01-31 14:56:10 +00001136 TEST_XCHG(xchgl, "k", "+q");
1137 TEST_XCHG(xchgw, "w", "+q");
1138 TEST_XCHG(xchgb, "b", "+q");
bellarde5918242003-03-22 15:20:50 +00001139
bellard776f2222005-03-02 22:19:12 +00001140#if defined(__x86_64__)
1141 TEST_XCHG(xchgq, "", "=m");
1142#endif
bellard8aadfbf2008-01-31 14:56:10 +00001143 TEST_XCHG(xchgl, "k", "+m");
1144 TEST_XCHG(xchgw, "w", "+m");
1145 TEST_XCHG(xchgb, "b", "+m");
bellarde5918242003-03-22 15:20:50 +00001146
bellard776f2222005-03-02 22:19:12 +00001147#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001148 TEST_XCHG(xaddq, "", "+q");
bellard776f2222005-03-02 22:19:12 +00001149#endif
bellard8aadfbf2008-01-31 14:56:10 +00001150 TEST_XCHG(xaddl, "k", "+q");
1151 TEST_XCHG(xaddw, "w", "+q");
1152 TEST_XCHG(xaddb, "b", "+q");
bellarde5918242003-03-22 15:20:50 +00001153
bellardd575b782004-01-18 21:57:29 +00001154 {
1155 int res;
1156 res = 0x12345678;
1157 asm("xaddl %1, %0" : "=r" (res) : "0" (res));
1158 printf("xaddl same res=%08x\n", res);
1159 }
1160
bellard776f2222005-03-02 22:19:12 +00001161#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001162 TEST_XCHG(xaddq, "", "+m");
bellard776f2222005-03-02 22:19:12 +00001163#endif
bellard8aadfbf2008-01-31 14:56:10 +00001164 TEST_XCHG(xaddl, "k", "+m");
1165 TEST_XCHG(xaddw, "w", "+m");
1166 TEST_XCHG(xaddb, "b", "+m");
bellarde5918242003-03-22 15:20:50 +00001167
bellard776f2222005-03-02 22:19:12 +00001168#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001169 TEST_CMPXCHG(cmpxchgq, "", "+q", 0xfbca7654);
bellard776f2222005-03-02 22:19:12 +00001170#endif
bellard8aadfbf2008-01-31 14:56:10 +00001171 TEST_CMPXCHG(cmpxchgl, "k", "+q", 0xfbca7654);
1172 TEST_CMPXCHG(cmpxchgw, "w", "+q", 0xfbca7654);
1173 TEST_CMPXCHG(cmpxchgb, "b", "+q", 0xfbca7654);
bellarde5918242003-03-22 15:20:50 +00001174
bellard776f2222005-03-02 22:19:12 +00001175#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001176 TEST_CMPXCHG(cmpxchgq, "", "+q", 0xfffefdfc);
bellard776f2222005-03-02 22:19:12 +00001177#endif
bellard8aadfbf2008-01-31 14:56:10 +00001178 TEST_CMPXCHG(cmpxchgl, "k", "+q", 0xfffefdfc);
1179 TEST_CMPXCHG(cmpxchgw, "w", "+q", 0xfffefdfc);
1180 TEST_CMPXCHG(cmpxchgb, "b", "+q", 0xfffefdfc);
bellarde5918242003-03-22 15:20:50 +00001181
bellard776f2222005-03-02 22:19:12 +00001182#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001183 TEST_CMPXCHG(cmpxchgq, "", "+m", 0xfbca7654);
bellard776f2222005-03-02 22:19:12 +00001184#endif
bellard8aadfbf2008-01-31 14:56:10 +00001185 TEST_CMPXCHG(cmpxchgl, "k", "+m", 0xfbca7654);
1186 TEST_CMPXCHG(cmpxchgw, "w", "+m", 0xfbca7654);
1187 TEST_CMPXCHG(cmpxchgb, "b", "+m", 0xfbca7654);
bellarde5918242003-03-22 15:20:50 +00001188
bellard776f2222005-03-02 22:19:12 +00001189#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001190 TEST_CMPXCHG(cmpxchgq, "", "+m", 0xfffefdfc);
bellard776f2222005-03-02 22:19:12 +00001191#endif
bellard8aadfbf2008-01-31 14:56:10 +00001192 TEST_CMPXCHG(cmpxchgl, "k", "+m", 0xfffefdfc);
1193 TEST_CMPXCHG(cmpxchgw, "w", "+m", 0xfffefdfc);
1194 TEST_CMPXCHG(cmpxchgb, "b", "+m", 0xfffefdfc);
bellardd575b782004-01-18 21:57:29 +00001195
1196 {
1197 uint64_t op0, op1, op2;
bellarda5973fb2008-05-28 12:34:49 +00001198 long eax, edx;
bellard776f2222005-03-02 22:19:12 +00001199 long i, eflags;
bellardd575b782004-01-18 21:57:29 +00001200
1201 for(i = 0; i < 2; i++) {
bellardbe98f1f2007-11-11 14:43:13 +00001202 op0 = 0x123456789abcdLL;
bellarda5973fb2008-05-28 12:34:49 +00001203 eax = i2l(op0 & 0xffffffff);
1204 edx = i2l(op0 >> 32);
bellardd575b782004-01-18 21:57:29 +00001205 if (i == 0)
bellardbe98f1f2007-11-11 14:43:13 +00001206 op1 = 0xfbca765423456LL;
bellardd575b782004-01-18 21:57:29 +00001207 else
1208 op1 = op0;
bellardbe98f1f2007-11-11 14:43:13 +00001209 op2 = 0x6532432432434LL;
bellarda5973fb2008-05-28 12:34:49 +00001210 asm("cmpxchg8b %2\n"
bellardd575b782004-01-18 21:57:29 +00001211 "pushf\n"
bellarda5973fb2008-05-28 12:34:49 +00001212 "pop %3\n"
1213 : "=a" (eax), "=d" (edx), "=m" (op1), "=g" (eflags)
1214 : "0" (eax), "1" (edx), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
1215 printf("cmpxchg8b: eax=" FMTLX " edx=" FMTLX " op1=" FMT64X " CC=%02lx\n",
1216 eax, edx, op1, eflags & CC_Z);
bellardd575b782004-01-18 21:57:29 +00001217 }
1218 }
bellarde5918242003-03-22 15:20:50 +00001219}
1220
bellard776f2222005-03-02 22:19:12 +00001221#ifdef TEST_SEGS
bellard6dbad632003-03-16 18:05:05 +00001222/**********************************************/
1223/* segmentation tests */
1224
bellardbe98f1f2007-11-11 14:43:13 +00001225#include <sys/syscall.h>
1226#include <unistd.h>
bellard6dbad632003-03-16 18:05:05 +00001227#include <asm/ldt.h>
bellard73bdea12004-03-04 22:50:52 +00001228#include <linux/version.h>
bellard6dbad632003-03-16 18:05:05 +00001229
bellardbe98f1f2007-11-11 14:43:13 +00001230static inline int modify_ldt(int func, void * ptr, unsigned long bytecount)
1231{
1232 return syscall(__NR_modify_ldt, func, ptr, bytecount);
1233}
bellard6dbad632003-03-16 18:05:05 +00001234
bellard73bdea12004-03-04 22:50:52 +00001235#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
1236#define modify_ldt_ldt_s user_desc
1237#endif
1238
bellard776f2222005-03-02 22:19:12 +00001239#define MK_SEL(n) (((n) << 3) | 7)
1240
bellard6dbad632003-03-16 18:05:05 +00001241uint8_t seg_data1[4096];
1242uint8_t seg_data2[4096];
1243
bellard288426f2003-05-10 21:39:12 +00001244#define TEST_LR(op, size, seg, mask)\
1245{\
1246 int res, res2;\
bellarda5973fb2008-05-28 12:34:49 +00001247 uint16_t mseg = seg;\
bellard288426f2003-05-10 21:39:12 +00001248 res = 0x12345678;\
1249 asm (op " %" size "2, %" size "0\n" \
1250 "movl $0, %1\n"\
1251 "jnz 1f\n"\
1252 "movl $1, %1\n"\
1253 "1:\n"\
bellarda5973fb2008-05-28 12:34:49 +00001254 : "=r" (res), "=r" (res2) : "m" (mseg), "0" (res));\
bellard288426f2003-05-10 21:39:12 +00001255 printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
1256}
1257
bellarda5973fb2008-05-28 12:34:49 +00001258#define TEST_ARPL(op, size, op1, op2)\
1259{\
1260 long a, b, c; \
1261 a = (op1); \
1262 b = (op2); \
1263 asm volatile(op " %" size "3, %" size "0\n"\
1264 "movl $0,%1\n"\
1265 "jnz 1f\n"\
1266 "movl $1,%1\n"\
1267 "1:\n"\
1268 : "=r" (a), "=r" (c) : "0" (a), "r" (b)); \
1269 printf(op size " A=" FMTLX " B=" FMTLX " R=" FMTLX " z=%ld\n",\
1270 (long)(op1), (long)(op2), a, c);\
1271}
1272
bellard6dbad632003-03-16 18:05:05 +00001273/* NOTE: we use Linux modify_ldt syscall */
1274void test_segs(void)
1275{
1276 struct modify_ldt_ldt_s ldt;
1277 long long ldt_table[3];
bellard04369ff2003-03-20 22:33:23 +00001278 int res, res2;
bellard6dbad632003-03-16 18:05:05 +00001279 char tmp;
bellarde1d42942003-03-29 16:45:07 +00001280 struct {
1281 uint32_t offset;
1282 uint16_t seg;
Stefan Weil541dc0d2011-08-31 12:38:01 +02001283 } QEMU_PACKED segoff;
bellard6dbad632003-03-16 18:05:05 +00001284
1285 ldt.entry_number = 1;
1286 ldt.base_addr = (unsigned long)&seg_data1;
1287 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1288 ldt.seg_32bit = 1;
1289 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1290 ldt.read_exec_only = 0;
1291 ldt.limit_in_pages = 1;
1292 ldt.seg_not_present = 0;
1293 ldt.useable = 1;
1294 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1295
1296 ldt.entry_number = 2;
1297 ldt.base_addr = (unsigned long)&seg_data2;
1298 ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
1299 ldt.seg_32bit = 1;
1300 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1301 ldt.read_exec_only = 0;
1302 ldt.limit_in_pages = 1;
1303 ldt.seg_not_present = 0;
1304 ldt.useable = 1;
1305 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
1306
1307 modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
bellard04369ff2003-03-20 22:33:23 +00001308#if 0
1309 {
1310 int i;
1311 for(i=0;i<3;i++)
1312 printf("%d: %016Lx\n", i, ldt_table[i]);
1313 }
1314#endif
bellard6dbad632003-03-16 18:05:05 +00001315 /* do some tests with fs or gs */
1316 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
bellard6dbad632003-03-16 18:05:05 +00001317
1318 seg_data1[1] = 0xaa;
1319 seg_data2[1] = 0x55;
1320
1321 asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
1322 printf("FS[1] = %02x\n", res);
1323
bellard070893f2003-07-13 17:27:19 +00001324 asm volatile ("pushl %%gs\n"
1325 "movl %1, %%gs\n"
1326 "gs movzbl 0x1, %0\n"
1327 "popl %%gs\n"
1328 : "=r" (res)
1329 : "r" (MK_SEL(2)));
bellard6dbad632003-03-16 18:05:05 +00001330 printf("GS[1] = %02x\n", res);
1331
1332 /* tests with ds/ss (implicit segment case) */
1333 tmp = 0xa5;
1334 asm volatile ("pushl %%ebp\n\t"
1335 "pushl %%ds\n\t"
1336 "movl %2, %%ds\n\t"
1337 "movl %3, %%ebp\n\t"
1338 "movzbl 0x1, %0\n\t"
1339 "movzbl (%%ebp), %1\n\t"
1340 "popl %%ds\n\t"
1341 "popl %%ebp\n\t"
1342 : "=r" (res), "=r" (res2)
1343 : "r" (MK_SEL(1)), "r" (&tmp));
1344 printf("DS[1] = %02x\n", res);
1345 printf("SS[tmp] = %02x\n", res2);
bellarde1d42942003-03-29 16:45:07 +00001346
1347 segoff.seg = MK_SEL(2);
1348 segoff.offset = 0xabcdef12;
ths5fafdf22007-09-16 21:08:06 +00001349 asm volatile("lfs %2, %0\n\t"
bellarde1d42942003-03-29 16:45:07 +00001350 "movl %%fs, %1\n\t"
ths5fafdf22007-09-16 21:08:06 +00001351 : "=r" (res), "=g" (res2)
bellarde1d42942003-03-29 16:45:07 +00001352 : "m" (segoff));
1353 printf("FS:reg = %04x:%08x\n", res2, res);
bellard288426f2003-05-10 21:39:12 +00001354
1355 TEST_LR("larw", "w", MK_SEL(2), 0x0100);
1356 TEST_LR("larl", "", MK_SEL(2), 0x0100);
1357 TEST_LR("lslw", "w", MK_SEL(2), 0);
1358 TEST_LR("lsll", "", MK_SEL(2), 0);
1359
1360 TEST_LR("larw", "w", 0xfff8, 0);
1361 TEST_LR("larl", "", 0xfff8, 0);
1362 TEST_LR("lslw", "w", 0xfff8, 0);
1363 TEST_LR("lsll", "", 0xfff8, 0);
bellarda5973fb2008-05-28 12:34:49 +00001364
1365 TEST_ARPL("arpl", "w", 0x12345678 | 3, 0x762123c | 1);
1366 TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 3);
1367 TEST_ARPL("arpl", "w", 0x12345678 | 1, 0x762123c | 1);
bellard6dbad632003-03-16 18:05:05 +00001368}
bellard55480af2003-03-16 11:29:17 +00001369
bellarde5918242003-03-22 15:20:50 +00001370/* 16 bit code test */
1371extern char code16_start, code16_end;
1372extern char code16_func1;
1373extern char code16_func2;
1374extern char code16_func3;
bellard1a9353d2003-03-16 20:28:50 +00001375
bellarde5918242003-03-22 15:20:50 +00001376void test_code16(void)
bellard1a9353d2003-03-16 20:28:50 +00001377{
bellarde5918242003-03-22 15:20:50 +00001378 struct modify_ldt_ldt_s ldt;
1379 int res, res2;
bellard1a9353d2003-03-16 20:28:50 +00001380
bellarde5918242003-03-22 15:20:50 +00001381 /* build a code segment */
1382 ldt.entry_number = 1;
1383 ldt.base_addr = (unsigned long)&code16_start;
1384 ldt.limit = &code16_end - &code16_start;
1385 ldt.seg_32bit = 0;
1386 ldt.contents = MODIFY_LDT_CONTENTS_CODE;
1387 ldt.read_exec_only = 0;
1388 ldt.limit_in_pages = 0;
1389 ldt.seg_not_present = 0;
1390 ldt.useable = 1;
1391 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
bellard1a9353d2003-03-16 20:28:50 +00001392
bellarde5918242003-03-22 15:20:50 +00001393 /* call the first function */
ths5fafdf22007-09-16 21:08:06 +00001394 asm volatile ("lcall %1, %2"
bellarde5918242003-03-22 15:20:50 +00001395 : "=a" (res)
1396 : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
1397 printf("func1() = 0x%08x\n", res);
ths5fafdf22007-09-16 21:08:06 +00001398 asm volatile ("lcall %2, %3"
bellarde5918242003-03-22 15:20:50 +00001399 : "=a" (res), "=c" (res2)
1400 : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
1401 printf("func2() = 0x%08x spdec=%d\n", res, res2);
ths5fafdf22007-09-16 21:08:06 +00001402 asm volatile ("lcall %1, %2"
bellarde5918242003-03-22 15:20:50 +00001403 : "=a" (res)
1404 : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
1405 printf("func3() = 0x%08x\n", res);
bellard1a9353d2003-03-16 20:28:50 +00001406}
bellard776f2222005-03-02 22:19:12 +00001407#endif
bellard1a9353d2003-03-16 20:28:50 +00001408
bellarde06e5252005-04-23 17:54:59 +00001409#if defined(__x86_64__)
1410asm(".globl func_lret\n"
1411 "func_lret:\n"
1412 "movl $0x87654641, %eax\n"
1413 "lretq\n");
1414#else
1415asm(".globl func_lret\n"
1416 "func_lret:\n"
1417 "movl $0x87654321, %eax\n"
1418 "lret\n"
1419
1420 ".globl func_iret\n"
1421 "func_iret:\n"
1422 "movl $0xabcd4321, %eax\n"
1423 "iret\n");
1424#endif
1425
1426extern char func_lret;
1427extern char func_iret;
bellarddd3587f2003-06-24 13:29:40 +00001428
bellarde1d42942003-03-29 16:45:07 +00001429void test_misc(void)
1430{
1431 char table[256];
bellard776f2222005-03-02 22:19:12 +00001432 long res, i;
bellarde1d42942003-03-29 16:45:07 +00001433
1434 for(i=0;i<256;i++) table[i] = 256 - i;
1435 res = 0x12345678;
1436 asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
bellard776f2222005-03-02 22:19:12 +00001437 printf("xlat: EAX=" FMTLX "\n", res);
bellarddd3587f2003-06-24 13:29:40 +00001438
bellarde06e5252005-04-23 17:54:59 +00001439#if defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00001440#if 0
bellarde06e5252005-04-23 17:54:59 +00001441 {
bellard8aadfbf2008-01-31 14:56:10 +00001442 /* XXX: see if Intel Core2 and AMD64 behavior really
1443 differ. Here we implemented the Intel way which is not
1444 compatible yet with QEMU. */
Stefan Weil541dc0d2011-08-31 12:38:01 +02001445 static struct QEMU_PACKED {
bellard8aadfbf2008-01-31 14:56:10 +00001446 uint64_t offset;
bellarde06e5252005-04-23 17:54:59 +00001447 uint16_t seg;
1448 } desc;
1449 long cs_sel;
1450
1451 asm volatile ("mov %%cs, %0" : "=r" (cs_sel));
1452
1453 asm volatile ("push %1\n"
ths5fafdf22007-09-16 21:08:06 +00001454 "call func_lret\n"
bellarde06e5252005-04-23 17:54:59 +00001455 : "=a" (res)
1456 : "r" (cs_sel) : "memory", "cc");
1457 printf("func_lret=" FMTLX "\n", res);
1458
bellarde06e5252005-04-23 17:54:59 +00001459 desc.offset = (long)&func_lret;
1460 desc.seg = cs_sel;
ths3b46e622007-09-17 08:09:54 +00001461
bellarde06e5252005-04-23 17:54:59 +00001462 asm volatile ("xor %%rax, %%rax\n"
bellard8aadfbf2008-01-31 14:56:10 +00001463 "rex64 lcall *(%%rcx)\n"
bellarde06e5252005-04-23 17:54:59 +00001464 : "=a" (res)
bellard8aadfbf2008-01-31 14:56:10 +00001465 : "c" (&desc)
bellarde06e5252005-04-23 17:54:59 +00001466 : "memory", "cc");
1467 printf("func_lret2=" FMTLX "\n", res);
1468
1469 asm volatile ("push %2\n"
1470 "mov $ 1f, %%rax\n"
1471 "push %%rax\n"
bellard8aadfbf2008-01-31 14:56:10 +00001472 "rex64 ljmp *(%%rcx)\n"
bellarde06e5252005-04-23 17:54:59 +00001473 "1:\n"
1474 : "=a" (res)
bellard8aadfbf2008-01-31 14:56:10 +00001475 : "c" (&desc), "b" (cs_sel)
bellarde06e5252005-04-23 17:54:59 +00001476 : "memory", "cc");
1477 printf("func_lret3=" FMTLX "\n", res);
1478 }
bellard8aadfbf2008-01-31 14:56:10 +00001479#endif
bellarde06e5252005-04-23 17:54:59 +00001480#else
ths5fafdf22007-09-16 21:08:06 +00001481 asm volatile ("push %%cs ; call %1"
bellarddd3587f2003-06-24 13:29:40 +00001482 : "=a" (res)
bellarde06e5252005-04-23 17:54:59 +00001483 : "m" (func_lret): "memory", "cc");
1484 printf("func_lret=" FMTLX "\n", res);
bellarddd3587f2003-06-24 13:29:40 +00001485
ths5fafdf22007-09-16 21:08:06 +00001486 asm volatile ("pushf ; push %%cs ; call %1"
bellarddd3587f2003-06-24 13:29:40 +00001487 : "=a" (res)
bellarde06e5252005-04-23 17:54:59 +00001488 : "m" (func_iret): "memory", "cc");
1489 printf("func_iret=" FMTLX "\n", res);
bellard776f2222005-03-02 22:19:12 +00001490#endif
bellarddd3587f2003-06-24 13:29:40 +00001491
bellard776f2222005-03-02 22:19:12 +00001492#if defined(__x86_64__)
1493 /* specific popl test */
1494 asm volatile ("push $12345432 ; push $0x9abcdef ; pop (%%rsp) ; pop %0"
1495 : "=g" (res));
1496 printf("popl esp=" FMTLX "\n", res);
1497#else
bellarddd3587f2003-06-24 13:29:40 +00001498 /* specific popl test */
1499 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
1500 : "=g" (res));
bellard776f2222005-03-02 22:19:12 +00001501 printf("popl esp=" FMTLX "\n", res);
bellardb2b5fb22003-07-26 18:00:58 +00001502
1503 /* specific popw test */
1504 asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
1505 : "=g" (res));
bellard776f2222005-03-02 22:19:12 +00001506 printf("popw esp=" FMTLX "\n", res);
1507#endif
bellarde1d42942003-03-29 16:45:07 +00001508}
1509
1510uint8_t str_buffer[4096];
1511
1512#define TEST_STRING1(OP, size, DF, REP)\
1513{\
bellard776f2222005-03-02 22:19:12 +00001514 long esi, edi, eax, ecx, eflags;\
bellarde1d42942003-03-29 16:45:07 +00001515\
1516 esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
1517 edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
bellard776f2222005-03-02 22:19:12 +00001518 eax = i2l(0x12345678);\
bellarde1d42942003-03-29 16:45:07 +00001519 ecx = 17;\
1520\
bellard776f2222005-03-02 22:19:12 +00001521 asm volatile ("push $0\n\t"\
bellarde1d42942003-03-29 16:45:07 +00001522 "popf\n\t"\
1523 DF "\n\t"\
1524 REP #OP size "\n\t"\
1525 "cld\n\t"\
1526 "pushf\n\t"\
bellard776f2222005-03-02 22:19:12 +00001527 "pop %4\n\t"\
bellarde1d42942003-03-29 16:45:07 +00001528 : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
1529 : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
bellard776f2222005-03-02 22:19:12 +00001530 printf("%-10s ESI=" FMTLX " EDI=" FMTLX " EAX=" FMTLX " ECX=" FMTLX " EFL=%04x\n",\
bellarde1d42942003-03-29 16:45:07 +00001531 REP #OP size, esi, edi, eax, ecx,\
bellard776f2222005-03-02 22:19:12 +00001532 (int)(eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)));\
bellarde1d42942003-03-29 16:45:07 +00001533}
1534
1535#define TEST_STRING(OP, REP)\
1536 TEST_STRING1(OP, "b", "", REP);\
1537 TEST_STRING1(OP, "w", "", REP);\
1538 TEST_STRING1(OP, "l", "", REP);\
bellard776f2222005-03-02 22:19:12 +00001539 X86_64_ONLY(TEST_STRING1(OP, "q", "", REP));\
bellarde1d42942003-03-29 16:45:07 +00001540 TEST_STRING1(OP, "b", "std", REP);\
1541 TEST_STRING1(OP, "w", "std", REP);\
bellard776f2222005-03-02 22:19:12 +00001542 TEST_STRING1(OP, "l", "std", REP);\
1543 X86_64_ONLY(TEST_STRING1(OP, "q", "std", REP))
bellarde1d42942003-03-29 16:45:07 +00001544
1545void test_string(void)
1546{
1547 int i;
1548 for(i = 0;i < sizeof(str_buffer); i++)
1549 str_buffer[i] = i + 0x56;
1550 TEST_STRING(stos, "");
1551 TEST_STRING(stos, "rep ");
1552 TEST_STRING(lods, ""); /* to verify stos */
ths5fafdf22007-09-16 21:08:06 +00001553 TEST_STRING(lods, "rep ");
bellarde1d42942003-03-29 16:45:07 +00001554 TEST_STRING(movs, "");
1555 TEST_STRING(movs, "rep ");
1556 TEST_STRING(lods, ""); /* to verify stos */
1557
1558 /* XXX: better tests */
1559 TEST_STRING(scas, "");
1560 TEST_STRING(scas, "repz ");
1561 TEST_STRING(scas, "repnz ");
1562 TEST_STRING(cmps, "");
1563 TEST_STRING(cmps, "repz ");
1564 TEST_STRING(cmps, "repnz ");
1565}
bellarde5918242003-03-22 15:20:50 +00001566
bellard776f2222005-03-02 22:19:12 +00001567#ifdef TEST_VM86
bellard3a27ad02003-05-16 13:43:31 +00001568/* VM86 test */
1569
1570static inline void set_bit(uint8_t *a, unsigned int bit)
1571{
1572 a[bit / 8] |= (1 << (bit % 8));
1573}
1574
1575static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
1576{
1577 return (uint8_t *)((seg << 4) + (reg & 0xffff));
1578}
1579
1580static inline void pushw(struct vm86_regs *r, int val)
1581{
1582 r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
1583 *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
1584}
1585
bellardbe98f1f2007-11-11 14:43:13 +00001586static inline int vm86(int func, struct vm86plus_struct *v86)
1587{
1588 return syscall(__NR_vm86, func, v86);
1589}
bellard3a27ad02003-05-16 13:43:31 +00001590
1591extern char vm86_code_start;
1592extern char vm86_code_end;
1593
1594#define VM86_CODE_CS 0x100
1595#define VM86_CODE_IP 0x100
1596
1597void test_vm86(void)
1598{
1599 struct vm86plus_struct ctx;
1600 struct vm86_regs *r;
1601 uint8_t *vm86_mem;
1602 int seg, ret;
1603
ths5fafdf22007-09-16 21:08:06 +00001604 vm86_mem = mmap((void *)0x00000000, 0x110000,
1605 PROT_WRITE | PROT_READ | PROT_EXEC,
bellard3a27ad02003-05-16 13:43:31 +00001606 MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
1607 if (vm86_mem == MAP_FAILED) {
1608 printf("ERROR: could not map vm86 memory");
1609 return;
1610 }
1611 memset(&ctx, 0, sizeof(ctx));
1612
1613 /* init basic registers */
1614 r = &ctx.regs;
1615 r->eip = VM86_CODE_IP;
1616 r->esp = 0xfffe;
1617 seg = VM86_CODE_CS;
1618 r->cs = seg;
1619 r->ss = seg;
1620 r->ds = seg;
1621 r->es = seg;
1622 r->fs = seg;
1623 r->gs = seg;
1624 r->eflags = VIF_MASK;
1625
1626 /* move code to proper address. We use the same layout as a .com
1627 dos program. */
ths5fafdf22007-09-16 21:08:06 +00001628 memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP,
bellard3a27ad02003-05-16 13:43:31 +00001629 &vm86_code_start, &vm86_code_end - &vm86_code_start);
1630
1631 /* mark int 0x21 as being emulated */
1632 set_bit((uint8_t *)&ctx.int_revectored, 0x21);
1633
1634 for(;;) {
1635 ret = vm86(VM86_ENTER, &ctx);
1636 switch(VM86_TYPE(ret)) {
1637 case VM86_INTx:
1638 {
bellard3ff06312003-09-17 22:49:51 +00001639 int int_num, ah, v;
ths3b46e622007-09-17 08:09:54 +00001640
bellard3a27ad02003-05-16 13:43:31 +00001641 int_num = VM86_ARG(ret);
1642 if (int_num != 0x21)
1643 goto unknown_int;
1644 ah = (r->eax >> 8) & 0xff;
1645 switch(ah) {
1646 case 0x00: /* exit */
1647 goto the_end;
1648 case 0x02: /* write char */
1649 {
1650 uint8_t c = r->edx;
1651 putchar(c);
1652 }
1653 break;
1654 case 0x09: /* write string */
1655 {
1656 uint8_t c, *ptr;
1657 ptr = seg_to_linear(r->ds, r->edx);
1658 for(;;) {
1659 c = *ptr++;
1660 if (c == '$')
1661 break;
1662 putchar(c);
1663 }
1664 r->eax = (r->eax & ~0xff) | '$';
1665 }
1666 break;
bellard3ff06312003-09-17 22:49:51 +00001667 case 0xff: /* extension: write eflags number in edx */
1668 v = (int)r->edx;
1669#ifndef LINUX_VM86_IOPL_FIX
1670 v &= ~0x3000;
1671#endif
1672 printf("%08x\n", v);
bellard3a27ad02003-05-16 13:43:31 +00001673 break;
1674 default:
1675 unknown_int:
1676 printf("unsupported int 0x%02x\n", int_num);
1677 goto the_end;
1678 }
1679 }
1680 break;
1681 case VM86_SIGNAL:
1682 /* a signal came, we just ignore that */
1683 break;
1684 case VM86_STI:
1685 break;
1686 default:
1687 printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
1688 goto the_end;
1689 }
1690 }
1691 the_end:
1692 printf("VM86 end\n");
1693 munmap(vm86_mem, 0x110000);
1694}
bellard776f2222005-03-02 22:19:12 +00001695#endif
bellard3a27ad02003-05-16 13:43:31 +00001696
1697/* exception tests */
bellard776f2222005-03-02 22:19:12 +00001698#if defined(__i386__) && !defined(REG_EAX)
bellard3a27ad02003-05-16 13:43:31 +00001699#define REG_EAX EAX
1700#define REG_EBX EBX
1701#define REG_ECX ECX
1702#define REG_EDX EDX
1703#define REG_ESI ESI
1704#define REG_EDI EDI
1705#define REG_EBP EBP
1706#define REG_ESP ESP
1707#define REG_EIP EIP
1708#define REG_EFL EFL
1709#define REG_TRAPNO TRAPNO
1710#define REG_ERR ERR
1711#endif
1712
bellard776f2222005-03-02 22:19:12 +00001713#if defined(__x86_64__)
1714#define REG_EIP REG_RIP
1715#endif
1716
bellard3a27ad02003-05-16 13:43:31 +00001717jmp_buf jmp_env;
bellard3a27ad02003-05-16 13:43:31 +00001718int v1;
1719int tab[2];
1720
1721void sig_handler(int sig, siginfo_t *info, void *puc)
1722{
1723 struct ucontext *uc = puc;
1724
1725 printf("si_signo=%d si_errno=%d si_code=%d",
1726 info->si_signo, info->si_errno, info->si_code);
bellarde3b32542003-05-27 23:23:22 +00001727 printf(" si_addr=0x%08lx",
1728 (unsigned long)info->si_addr);
bellard3a27ad02003-05-16 13:43:31 +00001729 printf("\n");
1730
bellard776f2222005-03-02 22:19:12 +00001731 printf("trapno=" FMTLX " err=" FMTLX,
1732 (long)uc->uc_mcontext.gregs[REG_TRAPNO],
1733 (long)uc->uc_mcontext.gregs[REG_ERR]);
1734 printf(" EIP=" FMTLX, (long)uc->uc_mcontext.gregs[REG_EIP]);
bellard3a27ad02003-05-16 13:43:31 +00001735 printf("\n");
1736 longjmp(jmp_env, 1);
1737}
1738
1739void test_exceptions(void)
1740{
1741 struct sigaction act;
1742 volatile int val;
ths3b46e622007-09-17 08:09:54 +00001743
bellard3a27ad02003-05-16 13:43:31 +00001744 act.sa_sigaction = sig_handler;
1745 sigemptyset(&act.sa_mask);
bellard776f2222005-03-02 22:19:12 +00001746 act.sa_flags = SA_SIGINFO | SA_NODEFER;
bellard3a27ad02003-05-16 13:43:31 +00001747 sigaction(SIGFPE, &act, NULL);
1748 sigaction(SIGILL, &act, NULL);
1749 sigaction(SIGSEGV, &act, NULL);
bellarde3b32542003-05-27 23:23:22 +00001750 sigaction(SIGBUS, &act, NULL);
bellard3a27ad02003-05-16 13:43:31 +00001751 sigaction(SIGTRAP, &act, NULL);
1752
1753 /* test division by zero reporting */
bellarde3b32542003-05-27 23:23:22 +00001754 printf("DIVZ exception:\n");
bellard3a27ad02003-05-16 13:43:31 +00001755 if (setjmp(jmp_env) == 0) {
1756 /* now divide by zero */
1757 v1 = 0;
1758 v1 = 2 / v1;
1759 }
1760
bellard776f2222005-03-02 22:19:12 +00001761#if !defined(__x86_64__)
bellarde3b32542003-05-27 23:23:22 +00001762 printf("BOUND exception:\n");
bellard3a27ad02003-05-16 13:43:31 +00001763 if (setjmp(jmp_env) == 0) {
1764 /* bound exception */
1765 tab[0] = 1;
1766 tab[1] = 10;
bellarde82d8ad2004-08-15 14:47:30 +00001767 asm volatile ("bound %0, %1" : : "r" (11), "m" (tab[0]));
bellard3a27ad02003-05-16 13:43:31 +00001768 }
bellard776f2222005-03-02 22:19:12 +00001769#endif
bellard3a27ad02003-05-16 13:43:31 +00001770
bellard776f2222005-03-02 22:19:12 +00001771#ifdef TEST_SEGS
bellarde3b32542003-05-27 23:23:22 +00001772 printf("segment exceptions:\n");
bellard3a27ad02003-05-16 13:43:31 +00001773 if (setjmp(jmp_env) == 0) {
bellarde3b32542003-05-27 23:23:22 +00001774 /* load an invalid segment */
1775 asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
1776 }
1777 if (setjmp(jmp_env) == 0) {
1778 /* null data segment is valid */
1779 asm volatile ("movl %0, %%fs" : : "r" (3));
1780 /* null stack segment */
1781 asm volatile ("movl %0, %%ss" : : "r" (3));
1782 }
1783
bellard776f2222005-03-02 22:19:12 +00001784 {
1785 struct modify_ldt_ldt_s ldt;
1786 ldt.entry_number = 1;
1787 ldt.base_addr = (unsigned long)&seg_data1;
1788 ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
1789 ldt.seg_32bit = 1;
1790 ldt.contents = MODIFY_LDT_CONTENTS_DATA;
1791 ldt.read_exec_only = 0;
1792 ldt.limit_in_pages = 1;
1793 ldt.seg_not_present = 1;
1794 ldt.useable = 1;
1795 modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
ths3b46e622007-09-17 08:09:54 +00001796
bellard776f2222005-03-02 22:19:12 +00001797 if (setjmp(jmp_env) == 0) {
1798 /* segment not present */
1799 asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
1800 }
bellarde3b32542003-05-27 23:23:22 +00001801 }
bellard776f2222005-03-02 22:19:12 +00001802#endif
bellarde3b32542003-05-27 23:23:22 +00001803
1804 /* test SEGV reporting */
1805 printf("PF exception:\n");
1806 if (setjmp(jmp_env) == 0) {
1807 val = 1;
bellardede28202003-07-11 14:49:58 +00001808 /* we add a nop to test a weird PC retrieval case */
1809 asm volatile ("nop");
bellard3a27ad02003-05-16 13:43:31 +00001810 /* now store in an invalid address */
1811 *(char *)0x1234 = 1;
1812 }
1813
1814 /* test SEGV reporting */
bellarde3b32542003-05-27 23:23:22 +00001815 printf("PF exception:\n");
bellard3a27ad02003-05-16 13:43:31 +00001816 if (setjmp(jmp_env) == 0) {
bellarde3b32542003-05-27 23:23:22 +00001817 val = 1;
bellard3a27ad02003-05-16 13:43:31 +00001818 /* read from an invalid address */
1819 v1 = *(char *)0x1234;
1820 }
ths3b46e622007-09-17 08:09:54 +00001821
bellard3a27ad02003-05-16 13:43:31 +00001822 /* test illegal instruction reporting */
1823 printf("UD2 exception:\n");
1824 if (setjmp(jmp_env) == 0) {
1825 /* now execute an invalid instruction */
1826 asm volatile("ud2");
1827 }
bellard4120b612004-01-19 20:29:34 +00001828 printf("lock nop exception:\n");
1829 if (setjmp(jmp_env) == 0) {
1830 /* now execute an invalid instruction */
Catalin Patuleaf62cb1b2012-10-16 16:00:23 -04001831 asm volatile(".byte 0xf0, 0x90"); /* lock nop */
bellard4120b612004-01-19 20:29:34 +00001832 }
ths3b46e622007-09-17 08:09:54 +00001833
bellard3a27ad02003-05-16 13:43:31 +00001834 printf("INT exception:\n");
1835 if (setjmp(jmp_env) == 0) {
1836 asm volatile ("int $0xfd");
1837 }
bellarde3b32542003-05-27 23:23:22 +00001838 if (setjmp(jmp_env) == 0) {
1839 asm volatile ("int $0x01");
1840 }
1841 if (setjmp(jmp_env) == 0) {
1842 asm volatile (".byte 0xcd, 0x03");
1843 }
1844 if (setjmp(jmp_env) == 0) {
1845 asm volatile ("int $0x04");
1846 }
1847 if (setjmp(jmp_env) == 0) {
1848 asm volatile ("int $0x05");
1849 }
bellard3a27ad02003-05-16 13:43:31 +00001850
1851 printf("INT3 exception:\n");
1852 if (setjmp(jmp_env) == 0) {
1853 asm volatile ("int3");
1854 }
1855
1856 printf("CLI exception:\n");
1857 if (setjmp(jmp_env) == 0) {
1858 asm volatile ("cli");
1859 }
1860
1861 printf("STI exception:\n");
1862 if (setjmp(jmp_env) == 0) {
1863 asm volatile ("cli");
1864 }
1865
bellard776f2222005-03-02 22:19:12 +00001866#if !defined(__x86_64__)
bellard3a27ad02003-05-16 13:43:31 +00001867 printf("INTO exception:\n");
1868 if (setjmp(jmp_env) == 0) {
1869 /* overflow exception */
1870 asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
1871 }
bellard776f2222005-03-02 22:19:12 +00001872#endif
bellard3a27ad02003-05-16 13:43:31 +00001873
1874 printf("OUTB exception:\n");
1875 if (setjmp(jmp_env) == 0) {
1876 asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
1877 }
1878
1879 printf("INB exception:\n");
1880 if (setjmp(jmp_env) == 0) {
1881 asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
1882 }
1883
1884 printf("REP OUTSB exception:\n");
1885 if (setjmp(jmp_env) == 0) {
1886 asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
1887 }
1888
1889 printf("REP INSB exception:\n");
1890 if (setjmp(jmp_env) == 0) {
1891 asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
1892 }
1893
1894 printf("HLT exception:\n");
1895 if (setjmp(jmp_env) == 0) {
1896 asm volatile ("hlt");
1897 }
1898
1899 printf("single step exception:\n");
1900 val = 0;
1901 if (setjmp(jmp_env) == 0) {
1902 asm volatile ("pushf\n"
1903 "orl $0x00100, (%%esp)\n"
1904 "popf\n"
ths5fafdf22007-09-16 21:08:06 +00001905 "movl $0xabcd, %0\n"
bellard3a27ad02003-05-16 13:43:31 +00001906 "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
1907 }
1908 printf("val=0x%x\n", val);
1909}
1910
bellard776f2222005-03-02 22:19:12 +00001911#if !defined(__x86_64__)
bellard3ff06312003-09-17 22:49:51 +00001912/* specific precise single step test */
1913void sig_trap_handler(int sig, siginfo_t *info, void *puc)
1914{
1915 struct ucontext *uc = puc;
bellard776f2222005-03-02 22:19:12 +00001916 printf("EIP=" FMTLX "\n", (long)uc->uc_mcontext.gregs[REG_EIP]);
bellard3ff06312003-09-17 22:49:51 +00001917}
1918
1919const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
1920uint8_t sstep_buf2[4];
1921
1922void test_single_step(void)
1923{
1924 struct sigaction act;
1925 volatile int val;
1926 int i;
1927
1928 val = 0;
1929 act.sa_sigaction = sig_trap_handler;
1930 sigemptyset(&act.sa_mask);
1931 act.sa_flags = SA_SIGINFO;
1932 sigaction(SIGTRAP, &act, NULL);
1933 asm volatile ("pushf\n"
1934 "orl $0x00100, (%%esp)\n"
1935 "popf\n"
ths5fafdf22007-09-16 21:08:06 +00001936 "movl $0xabcd, %0\n"
bellard3ff06312003-09-17 22:49:51 +00001937
1938 /* jmp test */
1939 "movl $3, %%ecx\n"
1940 "1:\n"
1941 "addl $1, %0\n"
1942 "decl %%ecx\n"
1943 "jnz 1b\n"
1944
1945 /* movsb: the single step should stop at each movsb iteration */
1946 "movl $sstep_buf1, %%esi\n"
1947 "movl $sstep_buf2, %%edi\n"
1948 "movl $0, %%ecx\n"
1949 "rep movsb\n"
1950 "movl $3, %%ecx\n"
1951 "rep movsb\n"
1952 "movl $1, %%ecx\n"
1953 "rep movsb\n"
1954
1955 /* cmpsb: the single step should stop at each cmpsb iteration */
1956 "movl $sstep_buf1, %%esi\n"
1957 "movl $sstep_buf2, %%edi\n"
1958 "movl $0, %%ecx\n"
1959 "rep cmpsb\n"
1960 "movl $4, %%ecx\n"
1961 "rep cmpsb\n"
ths3b46e622007-09-17 08:09:54 +00001962
bellard3ff06312003-09-17 22:49:51 +00001963 /* getpid() syscall: single step should skip one
1964 instruction */
1965 "movl $20, %%eax\n"
1966 "int $0x80\n"
1967 "movl $0, %%eax\n"
ths3b46e622007-09-17 08:09:54 +00001968
bellard3ff06312003-09-17 22:49:51 +00001969 /* when modifying SS, trace is not done on the next
1970 instruction */
1971 "movl %%ss, %%ecx\n"
1972 "movl %%ecx, %%ss\n"
1973 "addl $1, %0\n"
1974 "movl $1, %%eax\n"
1975 "movl %%ecx, %%ss\n"
1976 "jmp 1f\n"
1977 "addl $1, %0\n"
1978 "1:\n"
1979 "movl $1, %%eax\n"
1980 "pushl %%ecx\n"
1981 "popl %%ss\n"
1982 "addl $1, %0\n"
1983 "movl $1, %%eax\n"
ths3b46e622007-09-17 08:09:54 +00001984
bellard3ff06312003-09-17 22:49:51 +00001985 "pushf\n"
1986 "andl $~0x00100, (%%esp)\n"
1987 "popf\n"
ths5fafdf22007-09-16 21:08:06 +00001988 : "=m" (val)
1989 :
bellard3ff06312003-09-17 22:49:51 +00001990 : "cc", "memory", "eax", "ecx", "esi", "edi");
1991 printf("val=%d\n", val);
1992 for(i = 0; i < 4; i++)
1993 printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
1994}
1995
bellard3a27ad02003-05-16 13:43:31 +00001996/* self modifying code test */
1997uint8_t code[] = {
1998 0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
1999 0xc3, /* ret */
2000};
2001
bellarda5973fb2008-05-28 12:34:49 +00002002asm(".section \".data\"\n"
2003 "smc_code2:\n"
bellard11909352004-04-25 17:54:32 +00002004 "movl 4(%esp), %eax\n"
2005 "movl %eax, smc_patch_addr2 + 1\n"
2006 "nop\n"
2007 "nop\n"
2008 "nop\n"
2009 "nop\n"
2010 "nop\n"
2011 "nop\n"
2012 "nop\n"
2013 "nop\n"
2014 "smc_patch_addr2:\n"
2015 "movl $1, %eax\n"
bellarda5973fb2008-05-28 12:34:49 +00002016 "ret\n"
2017 ".previous\n"
2018 );
bellardd1fe2b22003-05-25 16:47:16 +00002019
bellard11909352004-04-25 17:54:32 +00002020typedef int FuncType(void);
2021extern int smc_code2(int);
bellard3a27ad02003-05-16 13:43:31 +00002022void test_self_modifying_code(void)
2023{
bellardd1fe2b22003-05-25 16:47:16 +00002024 int i;
bellard3a27ad02003-05-16 13:43:31 +00002025 printf("self modifying code:\n");
bellardd1fe2b22003-05-25 16:47:16 +00002026 printf("func1 = 0x%x\n", ((FuncType *)code)());
2027 for(i = 2; i <= 4; i++) {
2028 code[1] = i;
2029 printf("func%d = 0x%x\n", i, ((FuncType *)code)());
2030 }
bellard11909352004-04-25 17:54:32 +00002031
2032 /* more difficult test : the modified code is just after the
2033 modifying instruction. It is forbidden in Intel specs, but it
2034 is used by old DOS programs */
2035 for(i = 2; i <= 4; i++) {
2036 printf("smc_code2(%d) = %d\n", i, smc_code2(i));
2037 }
bellard3a27ad02003-05-16 13:43:31 +00002038}
bellard776f2222005-03-02 22:19:12 +00002039#endif
bellard61a8c4e2004-11-14 15:39:16 +00002040
bellard776f2222005-03-02 22:19:12 +00002041long enter_stack[4096];
2042
2043#if defined(__x86_64__)
2044#define RSP "%%rsp"
2045#define RBP "%%rbp"
2046#else
2047#define RSP "%%esp"
2048#define RBP "%%ebp"
2049#endif
bellard61a8c4e2004-11-14 15:39:16 +00002050
Paolo Bonzinif34f1fe2010-10-21 10:18:37 +02002051#if !defined(__x86_64__)
2052/* causes an infinite loop, disable it for now. */
2053#define TEST_ENTER(size, stack_type, level)
2054#else
bellard61a8c4e2004-11-14 15:39:16 +00002055#define TEST_ENTER(size, stack_type, level)\
2056{\
bellard776f2222005-03-02 22:19:12 +00002057 long esp_save, esp_val, ebp_val, ebp_save, i;\
bellard61a8c4e2004-11-14 15:39:16 +00002058 stack_type *ptr, *stack_end, *stack_ptr;\
2059 memset(enter_stack, 0, sizeof(enter_stack));\
2060 stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
2061 ebp_val = (long)stack_ptr;\
2062 for(i=1;i<=32;i++)\
2063 *--stack_ptr = i;\
2064 esp_val = (long)stack_ptr;\
bellard776f2222005-03-02 22:19:12 +00002065 asm("mov " RSP ", %[esp_save]\n"\
2066 "mov " RBP ", %[ebp_save]\n"\
2067 "mov %[esp_val], " RSP "\n"\
2068 "mov %[ebp_val], " RBP "\n"\
2069 "enter" size " $8, $" #level "\n"\
2070 "mov " RSP ", %[esp_val]\n"\
2071 "mov " RBP ", %[ebp_val]\n"\
2072 "mov %[esp_save], " RSP "\n"\
2073 "mov %[ebp_save], " RBP "\n"\
bellard61a8c4e2004-11-14 15:39:16 +00002074 : [esp_save] "=r" (esp_save),\
2075 [ebp_save] "=r" (ebp_save),\
2076 [esp_val] "=r" (esp_val),\
2077 [ebp_val] "=r" (ebp_val)\
2078 : "[esp_val]" (esp_val),\
2079 "[ebp_val]" (ebp_val));\
2080 printf("level=%d:\n", level);\
bellard776f2222005-03-02 22:19:12 +00002081 printf("esp_val=" FMTLX "\n", esp_val - (long)stack_end);\
2082 printf("ebp_val=" FMTLX "\n", ebp_val - (long)stack_end);\
bellard61a8c4e2004-11-14 15:39:16 +00002083 for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
bellard776f2222005-03-02 22:19:12 +00002084 printf(FMTLX "\n", (long)ptr[0]);\
bellard61a8c4e2004-11-14 15:39:16 +00002085}
Paolo Bonzinif34f1fe2010-10-21 10:18:37 +02002086#endif
bellard61a8c4e2004-11-14 15:39:16 +00002087
2088static void test_enter(void)
2089{
bellard776f2222005-03-02 22:19:12 +00002090#if defined(__x86_64__)
2091 TEST_ENTER("q", uint64_t, 0);
2092 TEST_ENTER("q", uint64_t, 1);
2093 TEST_ENTER("q", uint64_t, 2);
2094 TEST_ENTER("q", uint64_t, 31);
2095#else
bellard61a8c4e2004-11-14 15:39:16 +00002096 TEST_ENTER("l", uint32_t, 0);
2097 TEST_ENTER("l", uint32_t, 1);
2098 TEST_ENTER("l", uint32_t, 2);
2099 TEST_ENTER("l", uint32_t, 31);
bellard776f2222005-03-02 22:19:12 +00002100#endif
bellard61a8c4e2004-11-14 15:39:16 +00002101
2102 TEST_ENTER("w", uint16_t, 0);
2103 TEST_ENTER("w", uint16_t, 1);
2104 TEST_ENTER("w", uint16_t, 2);
2105 TEST_ENTER("w", uint16_t, 31);
2106}
2107
bellard085339a2005-01-08 18:54:41 +00002108#ifdef TEST_SSE
2109
2110typedef int __m64 __attribute__ ((__mode__ (__V2SI__)));
bellard8aadfbf2008-01-31 14:56:10 +00002111typedef float __m128 __attribute__ ((__mode__(__V4SF__)));
bellard085339a2005-01-08 18:54:41 +00002112
2113typedef union {
2114 double d[2];
2115 float s[4];
2116 uint32_t l[4];
2117 uint64_t q[2];
2118 __m128 dq;
2119} XMMReg;
2120
2121static uint64_t __attribute__((aligned(16))) test_values[4][2] = {
2122 { 0x456723c698694873, 0xdc515cff944a58ec },
2123 { 0x1f297ccd58bad7ab, 0x41f21efba9e3e146 },
2124 { 0x007c62c2085427f8, 0x231be9e8cde7438d },
2125 { 0x0f76255a085427f8, 0xc233e9e8c4c9439a },
2126};
2127
2128#define SSE_OP(op)\
2129{\
2130 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
bellard776f2222005-03-02 22:19:12 +00002131 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002132 #op,\
2133 a.q[1], a.q[0],\
2134 b.q[1], b.q[0],\
2135 r.q[1], r.q[0]);\
2136}
2137
2138#define SSE_OP2(op)\
2139{\
2140 int i;\
2141 for(i=0;i<2;i++) {\
2142 a.q[0] = test_values[2*i][0];\
2143 a.q[1] = test_values[2*i][1];\
2144 b.q[0] = test_values[2*i+1][0];\
2145 b.q[1] = test_values[2*i+1][1];\
2146 SSE_OP(op);\
2147 }\
2148}
2149
2150#define MMX_OP2(op)\
2151{\
2152 int i;\
2153 for(i=0;i<2;i++) {\
2154 a.q[0] = test_values[2*i][0];\
2155 b.q[0] = test_values[2*i+1][0];\
2156 asm volatile (#op " %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0]));\
bellard776f2222005-03-02 22:19:12 +00002157 printf("%-9s: a=" FMT64X " b=" FMT64X " r=" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002158 #op,\
2159 a.q[0],\
2160 b.q[0],\
2161 r.q[0]);\
2162 }\
2163 SSE_OP2(op);\
2164}
2165
bellard97ed14a2005-01-16 01:09:01 +00002166#define SHUF_OP(op, ib)\
2167{\
bellard97ed14a2005-01-16 01:09:01 +00002168 a.q[0] = test_values[0][0];\
2169 a.q[1] = test_values[0][1];\
2170 b.q[0] = test_values[1][0];\
2171 b.q[1] = test_values[1][1];\
2172 asm volatile (#op " $" #ib ", %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
bellard776f2222005-03-02 22:19:12 +00002173 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
bellard97ed14a2005-01-16 01:09:01 +00002174 #op,\
2175 a.q[1], a.q[0],\
2176 b.q[1], b.q[0],\
2177 ib,\
2178 r.q[1], r.q[0]);\
2179}
2180
bellard085339a2005-01-08 18:54:41 +00002181#define PSHUF_OP(op, ib)\
2182{\
2183 int i;\
2184 for(i=0;i<2;i++) {\
2185 a.q[0] = test_values[2*i][0];\
2186 a.q[1] = test_values[2*i][1];\
2187 asm volatile (#op " $" #ib ", %1, %0" : "=x" (r.dq) : "x" (a.dq));\
bellard776f2222005-03-02 22:19:12 +00002188 printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002189 #op,\
2190 a.q[1], a.q[0],\
2191 ib,\
2192 r.q[1], r.q[0]);\
2193 }\
2194}
2195
2196#define SHIFT_IM(op, ib)\
2197{\
2198 int i;\
2199 for(i=0;i<2;i++) {\
2200 a.q[0] = test_values[2*i][0];\
2201 a.q[1] = test_values[2*i][1];\
2202 asm volatile (#op " $" #ib ", %0" : "=x" (r.dq) : "0" (a.dq));\
bellard776f2222005-03-02 22:19:12 +00002203 printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002204 #op,\
2205 a.q[1], a.q[0],\
2206 ib,\
2207 r.q[1], r.q[0]);\
2208 }\
2209}
2210
2211#define SHIFT_OP(op, ib)\
2212{\
2213 int i;\
2214 SHIFT_IM(op, ib);\
2215 for(i=0;i<2;i++) {\
2216 a.q[0] = test_values[2*i][0];\
2217 a.q[1] = test_values[2*i][1];\
2218 b.q[0] = ib;\
2219 b.q[1] = 0;\
2220 asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
bellard776f2222005-03-02 22:19:12 +00002221 printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002222 #op,\
2223 a.q[1], a.q[0],\
2224 b.q[1], b.q[0],\
2225 r.q[1], r.q[0]);\
2226 }\
2227}
2228
2229#define MOVMSK(op)\
2230{\
2231 int i, reg;\
2232 for(i=0;i<2;i++) {\
2233 a.q[0] = test_values[2*i][0];\
2234 a.q[1] = test_values[2*i][1];\
2235 asm volatile (#op " %1, %0" : "=r" (reg) : "x" (a.dq));\
bellard776f2222005-03-02 22:19:12 +00002236 printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
bellard085339a2005-01-08 18:54:41 +00002237 #op,\
2238 a.q[1], a.q[0],\
2239 reg);\
2240 }\
2241}
2242
2243#define SSE_OPS(a) \
2244SSE_OP(a ## ps);\
2245SSE_OP(a ## ss);
2246
2247#define SSE_OPD(a) \
2248SSE_OP(a ## pd);\
2249SSE_OP(a ## sd);
2250
2251#define SSE_COMI(op, field)\
2252{\
2253 unsigned int eflags;\
2254 XMMReg a, b;\
2255 a.field[0] = a1;\
2256 b.field[0] = b1;\
2257 asm volatile (#op " %2, %1\n"\
2258 "pushf\n"\
2259 "pop %0\n"\
2260 : "=m" (eflags)\
2261 : "x" (a.dq), "x" (b.dq));\
2262 printf("%-9s: a=%f b=%f cc=%04x\n",\
2263 #op, a1, b1,\
2264 eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
2265}
2266
2267void test_sse_comi(double a1, double b1)
2268{
2269 SSE_COMI(ucomiss, s);
2270 SSE_COMI(ucomisd, d);
2271 SSE_COMI(comiss, s);
2272 SSE_COMI(comisd, d);
2273}
2274
2275#define CVT_OP_XMM(op)\
2276{\
2277 asm volatile (#op " %1, %0" : "=x" (r.dq) : "x" (a.dq));\
bellard776f2222005-03-02 22:19:12 +00002278 printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002279 #op,\
2280 a.q[1], a.q[0],\
2281 r.q[1], r.q[0]);\
2282}
2283
bellard80e7d522006-06-24 14:01:32 +00002284/* Force %xmm0 usage to avoid the case where both register index are 0
Stefan Weil5ba18542011-05-07 22:20:03 +02002285 to test instruction decoding more extensively */
bellard085339a2005-01-08 18:54:41 +00002286#define CVT_OP_XMM2MMX(op)\
2287{\
bellard80e7d522006-06-24 14:01:32 +00002288 asm volatile (#op " %1, %0" : "=y" (r.q[0]) : "x" (a.dq) \
bellarda5973fb2008-05-28 12:34:49 +00002289 : "%xmm0"); \
2290 asm volatile("emms\n"); \
bellard776f2222005-03-02 22:19:12 +00002291 printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002292 #op,\
2293 a.q[1], a.q[0],\
2294 r.q[0]);\
2295}
2296
2297#define CVT_OP_MMX2XMM(op)\
2298{\
2299 asm volatile (#op " %1, %0" : "=x" (r.dq) : "y" (a.q[0]));\
bellarda5973fb2008-05-28 12:34:49 +00002300 asm volatile("emms\n"); \
bellard776f2222005-03-02 22:19:12 +00002301 printf("%-9s: a=" FMT64X " r=" FMT64X "" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002302 #op,\
2303 a.q[0],\
2304 r.q[1], r.q[0]);\
2305}
2306
2307#define CVT_OP_REG2XMM(op)\
2308{\
2309 asm volatile (#op " %1, %0" : "=x" (r.dq) : "r" (a.l[0]));\
bellard776f2222005-03-02 22:19:12 +00002310 printf("%-9s: a=%08x r=" FMT64X "" FMT64X "\n",\
bellard085339a2005-01-08 18:54:41 +00002311 #op,\
2312 a.l[0],\
2313 r.q[1], r.q[0]);\
2314}
2315
2316#define CVT_OP_XMM2REG(op)\
2317{\
2318 asm volatile (#op " %1, %0" : "=r" (r.l[0]) : "x" (a.dq));\
bellard776f2222005-03-02 22:19:12 +00002319 printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
bellard085339a2005-01-08 18:54:41 +00002320 #op,\
2321 a.q[1], a.q[0],\
2322 r.l[0]);\
2323}
2324
bellarda4682cc2005-01-12 22:33:30 +00002325struct fpxstate {
2326 uint16_t fpuc;
2327 uint16_t fpus;
2328 uint16_t fptag;
2329 uint16_t fop;
2330 uint32_t fpuip;
2331 uint16_t cs_sel;
2332 uint16_t dummy0;
2333 uint32_t fpudp;
2334 uint16_t ds_sel;
2335 uint16_t dummy1;
2336 uint32_t mxcsr;
2337 uint32_t mxcsr_mask;
2338 uint8_t fpregs1[8 * 16];
2339 uint8_t xmm_regs[8 * 16];
2340 uint8_t dummy2[224];
2341};
2342
2343static struct fpxstate fpx_state __attribute__((aligned(16)));
2344static struct fpxstate fpx_state2 __attribute__((aligned(16)));
2345
2346void test_fxsave(void)
2347{
2348 struct fpxstate *fp = &fpx_state;
2349 struct fpxstate *fp2 = &fpx_state2;
bellard776f2222005-03-02 22:19:12 +00002350 int i, nb_xmm;
bellarda4682cc2005-01-12 22:33:30 +00002351 XMMReg a, b;
2352 a.q[0] = test_values[0][0];
2353 a.q[1] = test_values[0][1];
2354 b.q[0] = test_values[1][0];
2355 b.q[1] = test_values[1][1];
2356
2357 asm("movdqa %2, %%xmm0\n"
2358 "movdqa %3, %%xmm7\n"
bellard776f2222005-03-02 22:19:12 +00002359#if defined(__x86_64__)
2360 "movdqa %2, %%xmm15\n"
2361#endif
bellarda4682cc2005-01-12 22:33:30 +00002362 " fld1\n"
2363 " fldpi\n"
2364 " fldln2\n"
2365 " fxsave %0\n"
2366 " fxrstor %0\n"
2367 " fxsave %1\n"
2368 " fninit\n"
ths5fafdf22007-09-16 21:08:06 +00002369 : "=m" (*(uint32_t *)fp2), "=m" (*(uint32_t *)fp)
bellarda4682cc2005-01-12 22:33:30 +00002370 : "m" (a), "m" (b));
2371 printf("fpuc=%04x\n", fp->fpuc);
2372 printf("fpus=%04x\n", fp->fpus);
2373 printf("fptag=%04x\n", fp->fptag);
2374 for(i = 0; i < 3; i++) {
bellard776f2222005-03-02 22:19:12 +00002375 printf("ST%d: " FMT64X " %04x\n",
ths5fafdf22007-09-16 21:08:06 +00002376 i,
bellarda4682cc2005-01-12 22:33:30 +00002377 *(uint64_t *)&fp->fpregs1[i * 16],
2378 *(uint16_t *)&fp->fpregs1[i * 16 + 8]);
2379 }
2380 printf("mxcsr=%08x\n", fp->mxcsr & 0x1f80);
bellard776f2222005-03-02 22:19:12 +00002381#if defined(__x86_64__)
2382 nb_xmm = 16;
2383#else
2384 nb_xmm = 8;
2385#endif
2386 for(i = 0; i < nb_xmm; i++) {
2387 printf("xmm%d: " FMT64X "" FMT64X "\n",
ths5fafdf22007-09-16 21:08:06 +00002388 i,
bellarda4682cc2005-01-12 22:33:30 +00002389 *(uint64_t *)&fp->xmm_regs[i * 16],
2390 *(uint64_t *)&fp->xmm_regs[i * 16 + 8]);
2391 }
2392}
2393
bellard085339a2005-01-08 18:54:41 +00002394void test_sse(void)
2395{
2396 XMMReg r, a, b;
bellard86bd2ca2005-03-20 10:40:15 +00002397 int i;
bellard085339a2005-01-08 18:54:41 +00002398
2399 MMX_OP2(punpcklbw);
2400 MMX_OP2(punpcklwd);
2401 MMX_OP2(punpckldq);
2402 MMX_OP2(packsswb);
2403 MMX_OP2(pcmpgtb);
2404 MMX_OP2(pcmpgtw);
2405 MMX_OP2(pcmpgtd);
2406 MMX_OP2(packuswb);
2407 MMX_OP2(punpckhbw);
2408 MMX_OP2(punpckhwd);
2409 MMX_OP2(punpckhdq);
2410 MMX_OP2(packssdw);
2411 MMX_OP2(pcmpeqb);
2412 MMX_OP2(pcmpeqw);
2413 MMX_OP2(pcmpeqd);
2414
2415 MMX_OP2(paddq);
2416 MMX_OP2(pmullw);
2417 MMX_OP2(psubusb);
2418 MMX_OP2(psubusw);
2419 MMX_OP2(pminub);
2420 MMX_OP2(pand);
2421 MMX_OP2(paddusb);
2422 MMX_OP2(paddusw);
2423 MMX_OP2(pmaxub);
2424 MMX_OP2(pandn);
2425
2426 MMX_OP2(pmulhuw);
2427 MMX_OP2(pmulhw);
ths3b46e622007-09-17 08:09:54 +00002428
bellard085339a2005-01-08 18:54:41 +00002429 MMX_OP2(psubsb);
2430 MMX_OP2(psubsw);
2431 MMX_OP2(pminsw);
2432 MMX_OP2(por);
2433 MMX_OP2(paddsb);
2434 MMX_OP2(paddsw);
2435 MMX_OP2(pmaxsw);
2436 MMX_OP2(pxor);
2437 MMX_OP2(pmuludq);
2438 MMX_OP2(pmaddwd);
2439 MMX_OP2(psadbw);
2440 MMX_OP2(psubb);
2441 MMX_OP2(psubw);
2442 MMX_OP2(psubd);
2443 MMX_OP2(psubq);
2444 MMX_OP2(paddb);
2445 MMX_OP2(paddw);
2446 MMX_OP2(paddd);
2447
2448 MMX_OP2(pavgb);
2449 MMX_OP2(pavgw);
2450
2451 asm volatile ("pinsrw $1, %1, %0" : "=y" (r.q[0]) : "r" (0x12345678));
bellard776f2222005-03-02 22:19:12 +00002452 printf("%-9s: r=" FMT64X "\n", "pinsrw", r.q[0]);
bellard085339a2005-01-08 18:54:41 +00002453
2454 asm volatile ("pinsrw $5, %1, %0" : "=x" (r.dq) : "r" (0x12345678));
bellard776f2222005-03-02 22:19:12 +00002455 printf("%-9s: r=" FMT64X "" FMT64X "\n", "pinsrw", r.q[1], r.q[0]);
bellard085339a2005-01-08 18:54:41 +00002456
2457 a.q[0] = test_values[0][0];
2458 a.q[1] = test_values[0][1];
2459 asm volatile ("pextrw $1, %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
2460 printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
2461
2462 asm volatile ("pextrw $5, %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
2463 printf("%-9s: r=%08x\n", "pextrw", r.l[0]);
2464
2465 asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
2466 printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
ths3b46e622007-09-17 08:09:54 +00002467
bellard085339a2005-01-08 18:54:41 +00002468 asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
2469 printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
2470
bellard97ed14a2005-01-16 01:09:01 +00002471 {
2472 r.q[0] = -1;
2473 r.q[1] = -1;
2474
2475 a.q[0] = test_values[0][0];
2476 a.q[1] = test_values[0][1];
2477 b.q[0] = test_values[1][0];
2478 b.q[1] = test_values[1][1];
ths5fafdf22007-09-16 21:08:06 +00002479 asm volatile("maskmovq %1, %0" :
bellard97ed14a2005-01-16 01:09:01 +00002480 : "y" (a.q[0]), "y" (b.q[0]), "D" (&r)
ths5fafdf22007-09-16 21:08:06 +00002481 : "memory");
2482 printf("%-9s: r=" FMT64X " a=" FMT64X " b=" FMT64X "\n",
2483 "maskmov",
2484 r.q[0],
2485 a.q[0],
bellard97ed14a2005-01-16 01:09:01 +00002486 b.q[0]);
ths5fafdf22007-09-16 21:08:06 +00002487 asm volatile("maskmovdqu %1, %0" :
bellard97ed14a2005-01-16 01:09:01 +00002488 : "x" (a.dq), "x" (b.dq), "D" (&r)
ths5fafdf22007-09-16 21:08:06 +00002489 : "memory");
2490 printf("%-9s: r=" FMT64X "" FMT64X " a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X "\n",
2491 "maskmov",
2492 r.q[1], r.q[0],
2493 a.q[1], a.q[0],
bellard97ed14a2005-01-16 01:09:01 +00002494 b.q[1], b.q[0]);
2495 }
2496
bellard085339a2005-01-08 18:54:41 +00002497 asm volatile ("emms");
2498
2499 SSE_OP2(punpcklqdq);
2500 SSE_OP2(punpckhqdq);
2501 SSE_OP2(andps);
2502 SSE_OP2(andpd);
2503 SSE_OP2(andnps);
2504 SSE_OP2(andnpd);
2505 SSE_OP2(orps);
2506 SSE_OP2(orpd);
2507 SSE_OP2(xorps);
2508 SSE_OP2(xorpd);
2509
2510 SSE_OP2(unpcklps);
2511 SSE_OP2(unpcklpd);
2512 SSE_OP2(unpckhps);
2513 SSE_OP2(unpckhpd);
2514
bellard97ed14a2005-01-16 01:09:01 +00002515 SHUF_OP(shufps, 0x78);
2516 SHUF_OP(shufpd, 0x02);
bellard085339a2005-01-08 18:54:41 +00002517
2518 PSHUF_OP(pshufd, 0x78);
2519 PSHUF_OP(pshuflw, 0x78);
2520 PSHUF_OP(pshufhw, 0x78);
2521
2522 SHIFT_OP(psrlw, 7);
2523 SHIFT_OP(psrlw, 16);
2524 SHIFT_OP(psraw, 7);
2525 SHIFT_OP(psraw, 16);
2526 SHIFT_OP(psllw, 7);
2527 SHIFT_OP(psllw, 16);
2528
2529 SHIFT_OP(psrld, 7);
2530 SHIFT_OP(psrld, 32);
2531 SHIFT_OP(psrad, 7);
2532 SHIFT_OP(psrad, 32);
2533 SHIFT_OP(pslld, 7);
2534 SHIFT_OP(pslld, 32);
2535
2536 SHIFT_OP(psrlq, 7);
2537 SHIFT_OP(psrlq, 32);
2538 SHIFT_OP(psllq, 7);
2539 SHIFT_OP(psllq, 32);
2540
2541 SHIFT_IM(psrldq, 16);
2542 SHIFT_IM(psrldq, 7);
2543 SHIFT_IM(pslldq, 16);
2544 SHIFT_IM(pslldq, 7);
2545
2546 MOVMSK(movmskps);
2547 MOVMSK(movmskpd);
2548
2549 /* FPU specific ops */
2550
2551 {
2552 uint32_t mxcsr;
2553 asm volatile("stmxcsr %0" : "=m" (mxcsr));
2554 printf("mxcsr=%08x\n", mxcsr & 0x1f80);
2555 asm volatile("ldmxcsr %0" : : "m" (mxcsr));
2556 }
2557
2558 test_sse_comi(2, -1);
2559 test_sse_comi(2, 2);
2560 test_sse_comi(2, 3);
bellard86bd2ca2005-03-20 10:40:15 +00002561 test_sse_comi(2, q_nan.d);
2562 test_sse_comi(q_nan.d, -1);
bellard085339a2005-01-08 18:54:41 +00002563
bellard86bd2ca2005-03-20 10:40:15 +00002564 for(i = 0; i < 2; i++) {
2565 a.s[0] = 2.7;
2566 a.s[1] = 3.4;
2567 a.s[2] = 4;
2568 a.s[3] = -6.3;
2569 b.s[0] = 45.7;
2570 b.s[1] = 353.4;
2571 b.s[2] = 4;
2572 b.s[3] = 56.3;
2573 if (i == 1) {
2574 a.s[0] = q_nan.d;
2575 b.s[3] = q_nan.d;
2576 }
bellard085339a2005-01-08 18:54:41 +00002577
bellard86bd2ca2005-03-20 10:40:15 +00002578 SSE_OPS(add);
2579 SSE_OPS(mul);
2580 SSE_OPS(sub);
2581 SSE_OPS(min);
2582 SSE_OPS(div);
2583 SSE_OPS(max);
2584 SSE_OPS(sqrt);
2585 SSE_OPS(cmpeq);
2586 SSE_OPS(cmplt);
2587 SSE_OPS(cmple);
2588 SSE_OPS(cmpunord);
2589 SSE_OPS(cmpneq);
2590 SSE_OPS(cmpnlt);
2591 SSE_OPS(cmpnle);
2592 SSE_OPS(cmpord);
ths3b46e622007-09-17 08:09:54 +00002593
2594
bellard86bd2ca2005-03-20 10:40:15 +00002595 a.d[0] = 2.7;
2596 a.d[1] = -3.4;
2597 b.d[0] = 45.7;
2598 b.d[1] = -53.4;
2599 if (i == 1) {
2600 a.d[0] = q_nan.d;
2601 b.d[1] = q_nan.d;
2602 }
2603 SSE_OPD(add);
2604 SSE_OPD(mul);
2605 SSE_OPD(sub);
2606 SSE_OPD(min);
2607 SSE_OPD(div);
2608 SSE_OPD(max);
2609 SSE_OPD(sqrt);
2610 SSE_OPD(cmpeq);
2611 SSE_OPD(cmplt);
2612 SSE_OPD(cmple);
2613 SSE_OPD(cmpunord);
2614 SSE_OPD(cmpneq);
2615 SSE_OPD(cmpnlt);
2616 SSE_OPD(cmpnle);
2617 SSE_OPD(cmpord);
2618 }
2619
bellard085339a2005-01-08 18:54:41 +00002620 /* float to float/int */
2621 a.s[0] = 2.7;
2622 a.s[1] = 3.4;
2623 a.s[2] = 4;
2624 a.s[3] = -6.3;
2625 CVT_OP_XMM(cvtps2pd);
2626 CVT_OP_XMM(cvtss2sd);
2627 CVT_OP_XMM2MMX(cvtps2pi);
2628 CVT_OP_XMM2MMX(cvttps2pi);
2629 CVT_OP_XMM2REG(cvtss2si);
2630 CVT_OP_XMM2REG(cvttss2si);
2631 CVT_OP_XMM(cvtps2dq);
2632 CVT_OP_XMM(cvttps2dq);
2633
2634 a.d[0] = 2.6;
2635 a.d[1] = -3.4;
2636 CVT_OP_XMM(cvtpd2ps);
2637 CVT_OP_XMM(cvtsd2ss);
2638 CVT_OP_XMM2MMX(cvtpd2pi);
2639 CVT_OP_XMM2MMX(cvttpd2pi);
2640 CVT_OP_XMM2REG(cvtsd2si);
2641 CVT_OP_XMM2REG(cvttsd2si);
2642 CVT_OP_XMM(cvtpd2dq);
2643 CVT_OP_XMM(cvttpd2dq);
2644
bellard80e7d522006-06-24 14:01:32 +00002645 /* sse/mmx moves */
2646 CVT_OP_XMM2MMX(movdq2q);
2647 CVT_OP_MMX2XMM(movq2dq);
2648
bellard085339a2005-01-08 18:54:41 +00002649 /* int to float */
2650 a.l[0] = -6;
2651 a.l[1] = 2;
2652 a.l[2] = 100;
2653 a.l[3] = -60000;
2654 CVT_OP_MMX2XMM(cvtpi2ps);
2655 CVT_OP_MMX2XMM(cvtpi2pd);
2656 CVT_OP_REG2XMM(cvtsi2ss);
2657 CVT_OP_REG2XMM(cvtsi2sd);
2658 CVT_OP_XMM(cvtdq2ps);
2659 CVT_OP_XMM(cvtdq2pd);
2660
2661 /* XXX: test PNI insns */
2662#if 0
2663 SSE_OP2(movshdup);
2664#endif
bellarda4682cc2005-01-12 22:33:30 +00002665 asm volatile ("emms");
bellard085339a2005-01-08 18:54:41 +00002666}
2667
2668#endif
2669
bellarddf517ce2007-02-05 20:52:39 +00002670#define TEST_CONV_RAX(op)\
2671{\
2672 unsigned long a, r;\
2673 a = i2l(0x8234a6f8);\
2674 r = a;\
2675 asm volatile(#op : "=a" (r) : "0" (r));\
2676 printf("%-10s A=" FMTLX " R=" FMTLX "\n", #op, a, r);\
2677}
2678
2679#define TEST_CONV_RAX_RDX(op)\
2680{\
2681 unsigned long a, d, r, rh; \
2682 a = i2l(0x8234a6f8);\
2683 d = i2l(0x8345a1f2);\
2684 r = a;\
2685 rh = d;\
2686 asm volatile(#op : "=a" (r), "=d" (rh) : "0" (r), "1" (rh)); \
2687 printf("%-10s A=" FMTLX " R=" FMTLX ":" FMTLX "\n", #op, a, r, rh); \
2688}
2689
2690void test_conv(void)
2691{
2692 TEST_CONV_RAX(cbw);
2693 TEST_CONV_RAX(cwde);
2694#if defined(__x86_64__)
2695 TEST_CONV_RAX(cdqe);
2696#endif
2697
2698 TEST_CONV_RAX_RDX(cwd);
2699 TEST_CONV_RAX_RDX(cdq);
2700#if defined(__x86_64__)
2701 TEST_CONV_RAX_RDX(cqo);
2702#endif
bellard8aadfbf2008-01-31 14:56:10 +00002703
2704 {
2705 unsigned long a, r;
2706 a = i2l(0x12345678);
2707 asm volatile("bswapl %k0" : "=r" (r) : "0" (a));
2708 printf("%-10s: A=" FMTLX " R=" FMTLX "\n", "bswapl", a, r);
2709 }
2710#if defined(__x86_64__)
2711 {
2712 unsigned long a, r;
2713 a = i2l(0x12345678);
2714 asm volatile("bswapq %0" : "=r" (r) : "0" (a));
2715 printf("%-10s: A=" FMTLX " R=" FMTLX "\n", "bswapq", a, r);
2716 }
2717#endif
bellarddf517ce2007-02-05 20:52:39 +00002718}
2719
bellard776f2222005-03-02 22:19:12 +00002720extern void *__start_initcall;
2721extern void *__stop_initcall;
2722
bellard4d1135e2003-02-24 20:14:06 +00002723
2724int main(int argc, char **argv)
2725{
2726 void **ptr;
2727 void (*func)(void);
bellard4b74fe12003-03-03 23:23:09 +00002728
bellard776f2222005-03-02 22:19:12 +00002729 ptr = &__start_initcall;
2730 while (ptr != &__stop_initcall) {
bellard4d1135e2003-02-24 20:14:06 +00002731 func = *ptr++;
2732 func();
2733 }
bellard9d8e9c02003-03-05 20:57:02 +00002734 test_bsx();
bellardd57c4e02003-03-04 01:14:13 +00002735 test_mul();
bellard4d1135e2003-02-24 20:14:06 +00002736 test_jcc();
bellarda5973fb2008-05-28 12:34:49 +00002737 test_loop();
bellard9d8e9c02003-03-05 20:57:02 +00002738 test_floats();
bellard776f2222005-03-02 22:19:12 +00002739#if !defined(__x86_64__)
bellard55480af2003-03-16 11:29:17 +00002740 test_bcd();
bellard776f2222005-03-02 22:19:12 +00002741#endif
bellard1a9353d2003-03-16 20:28:50 +00002742 test_xchg();
bellarde1d42942003-03-29 16:45:07 +00002743 test_string();
2744 test_misc();
bellard6dbad632003-03-16 18:05:05 +00002745 test_lea();
bellard776f2222005-03-02 22:19:12 +00002746#ifdef TEST_SEGS
bellard6dbad632003-03-16 18:05:05 +00002747 test_segs();
bellarde5918242003-03-22 15:20:50 +00002748 test_code16();
bellard776f2222005-03-02 22:19:12 +00002749#endif
bellardacae4682005-01-03 23:25:56 +00002750#ifdef TEST_VM86
bellard3a27ad02003-05-16 13:43:31 +00002751 test_vm86();
bellardacae4682005-01-03 23:25:56 +00002752#endif
bellard776f2222005-03-02 22:19:12 +00002753#if !defined(__x86_64__)
bellard8aadfbf2008-01-31 14:56:10 +00002754 test_exceptions();
bellard3a27ad02003-05-16 13:43:31 +00002755 test_self_modifying_code();
bellard3ff06312003-09-17 22:49:51 +00002756 test_single_step();
bellard776f2222005-03-02 22:19:12 +00002757#endif
bellard61a8c4e2004-11-14 15:39:16 +00002758 test_enter();
bellarddf517ce2007-02-05 20:52:39 +00002759 test_conv();
bellard085339a2005-01-08 18:54:41 +00002760#ifdef TEST_SSE
2761 test_sse();
bellarda4682cc2005-01-12 22:33:30 +00002762 test_fxsave();
bellard085339a2005-01-08 18:54:41 +00002763#endif
bellard4d1135e2003-02-24 20:14:06 +00002764 return 0;
2765}