test-i386.c 66.8 KB
Newer Older
B
license  
bellard 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  x86 CPU test
 * 
 *  Copyright (c) 2003 Fabrice Bellard
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
20
#define _GNU_SOURCE
B
bellard 已提交
21 22
#include <stdlib.h>
#include <stdio.h>
B
bellard 已提交
23
#include <string.h>
B
bellard 已提交
24
#include <inttypes.h>
B
bellard 已提交
25
#include <math.h>
26 27
#include <signal.h>
#include <setjmp.h>
B
bellard 已提交
28
#include <errno.h>
29 30
#include <sys/ucontext.h>
#include <sys/mman.h>
B
bellard 已提交
31

B
bellard 已提交
32
#if !defined(__x86_64__)
33
#define TEST_VM86
B
bellard 已提交
34 35
#define TEST_SEGS
#endif
36
//#define LINUX_VM86_IOPL_FIX
37
//#define TEST_P4_FLAGS
B
bellard 已提交
38 39 40 41 42
#if defined(__x86_64__)
#define TEST_SSE
#define TEST_CMOV  1
#define TEST_FCOMI 1
#else
B
bellard 已提交
43
//#define TEST_SSE
B
bellard 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#define TEST_CMOV  0
#define TEST_FCOMI 0
#endif

#if defined(__x86_64__)
#define FMT64X "%016lx"
#define FMTLX "%016lx"
#define X86_64_ONLY(x) x
#else
#define FMT64X "%016llx"
#define FMTLX "%08lx"
#define X86_64_ONLY(x)
#endif

#ifdef TEST_VM86
#include <asm/vm86.h>
#endif
B
bellard 已提交
61

B
bellard 已提交
62 63 64 65 66 67 68 69 70 71 72 73
#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)
#define stringify(s)	tostring(s)
#define tostring(s)	#s

#define CC_C   	0x0001
#define CC_P 	0x0004
#define CC_A	0x0010
#define CC_Z	0x0040
#define CC_S    0x0080
#define CC_O    0x0800

B
bellard 已提交
74
#define __init_call	__attribute__ ((unused,__section__ ("initcall")))
B
bellard 已提交
75

B
bellard 已提交
76 77
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)

B
bellard 已提交
78 79 80 81 82 83 84 85 86 87 88 89
#if defined(__x86_64__)
static inline long i2l(long v)
{
    return v | ((v ^ 0xabcd) << 32);
}
#else
static inline long i2l(long v)
{
    return v;
}
#endif

B
bellard 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#define OP add
#include "test-i386.h"

#define OP sub
#include "test-i386.h"

#define OP xor
#include "test-i386.h"

#define OP and
#include "test-i386.h"

#define OP or
#include "test-i386.h"

#define OP cmp
#include "test-i386.h"

#define OP adc
#define OP_CC
#include "test-i386.h"

#define OP sbb
#define OP_CC
#include "test-i386.h"

#define OP inc
#define OP_CC
#define OP1
#include "test-i386.h"

#define OP dec
#define OP_CC
#define OP1
#include "test-i386.h"

#define OP neg
#define OP_CC
#define OP1
#include "test-i386.h"

#define OP not
#define OP_CC
#define OP1
#include "test-i386.h"

B
bellard 已提交
136 137 138
#undef CC_MASK
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O)

B
bellard 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#define OP shl
#include "test-i386-shift.h"

#define OP shr
#include "test-i386-shift.h"

#define OP sar
#include "test-i386-shift.h"

#define OP rol
#include "test-i386-shift.h"

#define OP ror
#include "test-i386-shift.h"

#define OP rcr
#define OP_CC
#include "test-i386-shift.h"

#define OP rcl
#define OP_CC
#include "test-i386-shift.h"

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
#define OP shld
#define OP_SHIFTD
#define OP_NOBYTE
#include "test-i386-shift.h"

#define OP shrd
#define OP_SHIFTD
#define OP_NOBYTE
#include "test-i386-shift.h"

/* XXX: should be more precise ? */
#undef CC_MASK
#define CC_MASK (CC_C)

#define OP bt
#define OP_NOBYTE
#include "test-i386-shift.h"

#define OP bts
#define OP_NOBYTE
#include "test-i386-shift.h"

#define OP btr
#define OP_NOBYTE
#include "test-i386-shift.h"

#define OP btc
#define OP_NOBYTE
#include "test-i386-shift.h"
B
bellard 已提交
191

B
bellard 已提交
192
/* lea test (modrm support) */
B
bellard 已提交
193 194 195 196 197 198 199 200
#define TEST_LEAQ(STR)\
{\
    asm("lea " STR ", %0"\
        : "=r" (res)\
        : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
    printf("lea %s = " FMTLX "\n", STR, res);\
}

B
bellard 已提交
201 202
#define TEST_LEA(STR)\
{\
B
bellard 已提交
203
    asm("lea " STR ", %0"\
B
bellard 已提交
204 205
        : "=r" (res)\
        : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
B
bellard 已提交
206
    printf("lea %s = " FMTLX "\n", STR, res);\
B
bellard 已提交
207 208 209 210 211 212 213
}

#define TEST_LEA16(STR)\
{\
    asm(".code16 ; .byte 0x67 ; leal " STR ", %0 ; .code32"\
        : "=wq" (res)\
        : "a" (eax), "b" (ebx), "c" (ecx), "d" (edx), "S" (esi), "D" (edi));\
B
bellard 已提交
214
    printf("lea %s = %08lx\n", STR, res);\
B
bellard 已提交
215 216 217 218 219
}


void test_lea(void)
{
B
bellard 已提交
220 221 222 223 224 225 226
    long eax, ebx, ecx, edx, esi, edi, res;
    eax = i2l(0x0001);
    ebx = i2l(0x0002);
    ecx = i2l(0x0004);
    edx = i2l(0x0008);
    esi = i2l(0x0010);
    edi = i2l(0x0020);
B
bellard 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    TEST_LEA("0x4000");

    TEST_LEA("(%%eax)");
    TEST_LEA("(%%ebx)");
    TEST_LEA("(%%ecx)");
    TEST_LEA("(%%edx)");
    TEST_LEA("(%%esi)");
    TEST_LEA("(%%edi)");

    TEST_LEA("0x40(%%eax)");
    TEST_LEA("0x40(%%ebx)");
    TEST_LEA("0x40(%%ecx)");
    TEST_LEA("0x40(%%edx)");
    TEST_LEA("0x40(%%esi)");
    TEST_LEA("0x40(%%edi)");

    TEST_LEA("0x4000(%%eax)");
    TEST_LEA("0x4000(%%ebx)");
    TEST_LEA("0x4000(%%ecx)");
    TEST_LEA("0x4000(%%edx)");
    TEST_LEA("0x4000(%%esi)");
    TEST_LEA("0x4000(%%edi)");

    TEST_LEA("(%%eax, %%ecx)");
    TEST_LEA("(%%ebx, %%edx)");
    TEST_LEA("(%%ecx, %%ecx)");
    TEST_LEA("(%%edx, %%ecx)");
    TEST_LEA("(%%esi, %%ecx)");
    TEST_LEA("(%%edi, %%ecx)");

    TEST_LEA("0x40(%%eax, %%ecx)");
    TEST_LEA("0x4000(%%ebx, %%edx)");

    TEST_LEA("(%%ecx, %%ecx, 2)");
    TEST_LEA("(%%edx, %%ecx, 4)");
    TEST_LEA("(%%esi, %%ecx, 8)");

    TEST_LEA("(,%%eax, 2)");
    TEST_LEA("(,%%ebx, 4)");
    TEST_LEA("(,%%ecx, 8)");

    TEST_LEA("0x40(,%%eax, 2)");
    TEST_LEA("0x40(,%%ebx, 4)");
    TEST_LEA("0x40(,%%ecx, 8)");


    TEST_LEA("-10(%%ecx, %%ecx, 2)");
    TEST_LEA("-10(%%edx, %%ecx, 4)");
    TEST_LEA("-10(%%esi, %%ecx, 8)");

    TEST_LEA("0x4000(%%ecx, %%ecx, 2)");
    TEST_LEA("0x4000(%%edx, %%ecx, 4)");
    TEST_LEA("0x4000(%%esi, %%ecx, 8)");

B
bellard 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
#if defined(__x86_64__)
    TEST_LEAQ("0x4000");
    TEST_LEAQ("0x4000(%%rip)");

    TEST_LEAQ("(%%rax)");
    TEST_LEAQ("(%%rbx)");
    TEST_LEAQ("(%%rcx)");
    TEST_LEAQ("(%%rdx)");
    TEST_LEAQ("(%%rsi)");
    TEST_LEAQ("(%%rdi)");

    TEST_LEAQ("0x40(%%rax)");
    TEST_LEAQ("0x40(%%rbx)");
    TEST_LEAQ("0x40(%%rcx)");
    TEST_LEAQ("0x40(%%rdx)");
    TEST_LEAQ("0x40(%%rsi)");
    TEST_LEAQ("0x40(%%rdi)");

    TEST_LEAQ("0x4000(%%rax)");
    TEST_LEAQ("0x4000(%%rbx)");
    TEST_LEAQ("0x4000(%%rcx)");
    TEST_LEAQ("0x4000(%%rdx)");
    TEST_LEAQ("0x4000(%%rsi)");
    TEST_LEAQ("0x4000(%%rdi)");

    TEST_LEAQ("(%%rax, %%rcx)");
    TEST_LEAQ("(%%rbx, %%rdx)");
    TEST_LEAQ("(%%rcx, %%rcx)");
    TEST_LEAQ("(%%rdx, %%rcx)");
    TEST_LEAQ("(%%rsi, %%rcx)");
    TEST_LEAQ("(%%rdi, %%rcx)");

    TEST_LEAQ("0x40(%%rax, %%rcx)");
    TEST_LEAQ("0x4000(%%rbx, %%rdx)");

    TEST_LEAQ("(%%rcx, %%rcx, 2)");
    TEST_LEAQ("(%%rdx, %%rcx, 4)");
    TEST_LEAQ("(%%rsi, %%rcx, 8)");

    TEST_LEAQ("(,%%rax, 2)");
    TEST_LEAQ("(,%%rbx, 4)");
    TEST_LEAQ("(,%%rcx, 8)");

    TEST_LEAQ("0x40(,%%rax, 2)");
    TEST_LEAQ("0x40(,%%rbx, 4)");
    TEST_LEAQ("0x40(,%%rcx, 8)");


    TEST_LEAQ("-10(%%rcx, %%rcx, 2)");
    TEST_LEAQ("-10(%%rdx, %%rcx, 4)");
    TEST_LEAQ("-10(%%rsi, %%rcx, 8)");

    TEST_LEAQ("0x4000(%%rcx, %%rcx, 2)");
    TEST_LEAQ("0x4000(%%rdx, %%rcx, 4)");
    TEST_LEAQ("0x4000(%%rsi, %%rcx, 8)");
#else
B
bellard 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    /* limited 16 bit addressing test */
    TEST_LEA16("0x4000");
    TEST_LEA16("(%%bx)");
    TEST_LEA16("(%%si)");
    TEST_LEA16("(%%di)");
    TEST_LEA16("0x40(%%bx)");
    TEST_LEA16("0x40(%%si)");
    TEST_LEA16("0x40(%%di)");
    TEST_LEA16("0x4000(%%bx)");
    TEST_LEA16("0x4000(%%si)");
    TEST_LEA16("(%%bx,%%si)");
    TEST_LEA16("(%%bx,%%di)");
    TEST_LEA16("0x40(%%bx,%%si)");
    TEST_LEA16("0x40(%%bx,%%di)");
    TEST_LEA16("0x4000(%%bx,%%si)");
    TEST_LEA16("0x4000(%%bx,%%di)");
B
bellard 已提交
354
#endif
B
bellard 已提交
355 356 357 358
}

#define TEST_JCC(JCC, v1, v2)\
{\
B
bellard 已提交
359
    int res;\
B
bellard 已提交
360 361
    asm("movl $1, %0\n\t"\
        "cmpl %2, %1\n\t"\
B
bellard 已提交
362
        "j" JCC " 1f\n\t"\
B
bellard 已提交
363 364 365 366
        "movl $0, %0\n\t"\
        "1:\n\t"\
        : "=r" (res)\
        : "r" (v1), "r" (v2));\
B
bellard 已提交
367 368 369 370 371 372 373 374 375
    printf("%-10s %d\n", "j" JCC, res);\
\
    asm("movl $0, %0\n\t"\
        "cmpl %2, %1\n\t"\
        "set" JCC " %b0\n\t"\
        : "=r" (res)\
        : "r" (v1), "r" (v2));\
    printf("%-10s %d\n", "set" JCC, res);\
 if (TEST_CMOV) {\
B
bellard 已提交
376 377 378 379 380
    long val = i2l(1);\
    long res = i2l(0x12345678);\
X86_64_ONLY(\
    asm("cmpl %2, %1\n\t"\
        "cmov" JCC "q %3, %0\n\t"\
B
bellard 已提交
381
        : "=r" (res)\
B
bellard 已提交
382
        : "r" (v1), "r" (v2), "m" (val), "0" (res));\
B
bellard 已提交
383
        printf("%-10s R=" FMTLX "\n", "cmov" JCC "q", res);)\
B
bellard 已提交
384 385 386 387 388 389
    asm("cmpl %2, %1\n\t"\
        "cmov" JCC "l %k3, %k0\n\t"\
        : "=r" (res)\
        : "r" (v1), "r" (v2), "m" (val), "0" (res));\
        printf("%-10s R=" FMTLX "\n", "cmov" JCC "l", res);\
    asm("cmpl %2, %1\n\t"\
B
bellard 已提交
390 391
        "cmov" JCC "w %w3, %w0\n\t"\
        : "=r" (res)\
B
bellard 已提交
392 393
        : "r" (v1), "r" (v2), "r" (1), "0" (res));\
        printf("%-10s R=" FMTLX "\n", "cmov" JCC "w", res);\
B
bellard 已提交
394
 } \
B
bellard 已提交
395 396 397 398 399
}

/* various jump tests */
void test_jcc(void)
{
B
bellard 已提交
400 401
    TEST_JCC("ne", 1, 1);
    TEST_JCC("ne", 1, 0);
B
bellard 已提交
402

B
bellard 已提交
403 404
    TEST_JCC("e", 1, 1);
    TEST_JCC("e", 1, 0);
B
bellard 已提交
405

B
bellard 已提交
406 407 408
    TEST_JCC("l", 1, 1);
    TEST_JCC("l", 1, 0);
    TEST_JCC("l", 1, -1);
B
bellard 已提交
409

B
bellard 已提交
410 411 412
    TEST_JCC("le", 1, 1);
    TEST_JCC("le", 1, 0);
    TEST_JCC("le", 1, -1);
B
bellard 已提交
413

B
bellard 已提交
414 415 416
    TEST_JCC("ge", 1, 1);
    TEST_JCC("ge", 1, 0);
    TEST_JCC("ge", -1, 1);
B
bellard 已提交
417

B
bellard 已提交
418 419 420
    TEST_JCC("g", 1, 1);
    TEST_JCC("g", 1, 0);
    TEST_JCC("g", 1, -1);
B
bellard 已提交
421

B
bellard 已提交
422 423 424
    TEST_JCC("b", 1, 1);
    TEST_JCC("b", 1, 0);
    TEST_JCC("b", 1, -1);
B
bellard 已提交
425

B
bellard 已提交
426 427 428
    TEST_JCC("be", 1, 1);
    TEST_JCC("be", 1, 0);
    TEST_JCC("be", 1, -1);
B
bellard 已提交
429

B
bellard 已提交
430 431 432
    TEST_JCC("ae", 1, 1);
    TEST_JCC("ae", 1, 0);
    TEST_JCC("ae", 1, -1);
B
bellard 已提交
433

B
bellard 已提交
434 435 436
    TEST_JCC("a", 1, 1);
    TEST_JCC("a", 1, 0);
    TEST_JCC("a", 1, -1);
B
bellard 已提交
437 438


B
bellard 已提交
439 440
    TEST_JCC("p", 1, 1);
    TEST_JCC("p", 1, 0);
B
bellard 已提交
441

B
bellard 已提交
442 443
    TEST_JCC("np", 1, 1);
    TEST_JCC("np", 1, 0);
B
bellard 已提交
444

B
bellard 已提交
445 446
    TEST_JCC("o", 0x7fffffff, 0);
    TEST_JCC("o", 0x7fffffff, -1);
B
bellard 已提交
447

B
bellard 已提交
448 449
    TEST_JCC("no", 0x7fffffff, 0);
    TEST_JCC("no", 0x7fffffff, -1);
B
bellard 已提交
450

B
bellard 已提交
451 452 453
    TEST_JCC("s", 0, 1);
    TEST_JCC("s", 0, -1);
    TEST_JCC("s", 0, 0);
B
bellard 已提交
454

B
bellard 已提交
455 456 457
    TEST_JCC("ns", 0, 1);
    TEST_JCC("ns", 0, -1);
    TEST_JCC("ns", 0, 0);
B
bellard 已提交
458 459
}

B
bellard 已提交
460
#undef CC_MASK
461 462 463
#ifdef TEST_P4_FLAGS
#define CC_MASK (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)
#else
B
bellard 已提交
464
#define CC_MASK (CC_O | CC_C)
465
#endif
B
bellard 已提交
466 467 468 469 470 471 472

#define OP mul
#include "test-i386-muldiv.h"

#define OP imul
#include "test-i386-muldiv.h"

B
bellard 已提交
473
void test_imulw2(long op0, long op1) 
B
bellard 已提交
474
{
B
bellard 已提交
475
    long res, s1, s0, flags;
B
bellard 已提交
476 477 478 479
    s0 = op0;
    s1 = op1;
    res = s0;
    flags = 0;
480
    asm volatile ("push %4\n\t"
B
bellard 已提交
481 482 483
         "popf\n\t"
         "imulw %w2, %w0\n\t" 
         "pushf\n\t"
B
bellard 已提交
484
         "pop %1\n\t"
B
bellard 已提交
485 486
         : "=q" (res), "=g" (flags)
         : "q" (s1), "0" (res), "1" (flags));
B
bellard 已提交
487
    printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
B
bellard 已提交
488 489 490
           "imulw", s0, s1, res, flags & CC_MASK);
}

B
bellard 已提交
491
void test_imull2(long op0, long op1) 
B
bellard 已提交
492
{
B
bellard 已提交
493
    long res, s1, s0, flags;
B
bellard 已提交
494 495 496 497
    s0 = op0;
    s1 = op1;
    res = s0;
    flags = 0;
498
    asm volatile ("push %4\n\t"
B
bellard 已提交
499
         "popf\n\t"
B
bellard 已提交
500
         "imull %k2, %k0\n\t" 
B
bellard 已提交
501
         "pushf\n\t"
B
bellard 已提交
502
         "pop %1\n\t"
B
bellard 已提交
503 504
         : "=q" (res), "=g" (flags)
         : "q" (s1), "0" (res), "1" (flags));
B
bellard 已提交
505
    printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
B
bellard 已提交
506 507 508
           "imull", s0, s1, res, flags & CC_MASK);
}

B
bellard 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
#if defined(__x86_64__)
void test_imulq2(long op0, long op1) 
{
    long res, s1, s0, flags;
    s0 = op0;
    s1 = op1;
    res = s0;
    flags = 0;
    asm volatile ("push %4\n\t"
         "popf\n\t"
         "imulq %2, %0\n\t" 
         "pushf\n\t"
         "pop %1\n\t"
         : "=q" (res), "=g" (flags)
         : "q" (s1), "0" (res), "1" (flags));
    printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",
           "imulq", s0, s1, res, flags & CC_MASK);
}
#endif

#define TEST_IMUL_IM(size, rsize, op0, op1)\
B
bellard 已提交
530
{\
B
bellard 已提交
531
    long res, flags, s1;\
B
bellard 已提交
532 533
    flags = 0;\
    res = 0;\
B
bellard 已提交
534
    s1 = op1;\
535
    asm volatile ("push %3\n\t"\
B
bellard 已提交
536
         "popf\n\t"\
B
bellard 已提交
537
         "imul" size " $" #op0 ", %" rsize "2, %" rsize "0\n\t" \
B
bellard 已提交
538
         "pushf\n\t"\
B
bellard 已提交
539
         "pop %1\n\t"\
B
bellard 已提交
540
         : "=r" (res), "=g" (flags)\
B
bellard 已提交
541 542 543
         : "r" (s1), "1" (flags), "0" (res));\
    printf("%-10s A=" FMTLX " B=" FMTLX " R=" FMTLX " CC=%04lx\n",\
           "imul" size " im", (long)op0, (long)op1, res, flags & CC_MASK);\
B
bellard 已提交
544 545 546
}


547 548 549 550 551 552 553 554 555
#undef CC_MASK
#define CC_MASK (0)

#define OP div
#include "test-i386-muldiv.h"

#define OP idiv
#include "test-i386-muldiv.h"

B
bellard 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
void test_mul(void)
{
    test_imulb(0x1234561d, 4);
    test_imulb(3, -4);
    test_imulb(0x80, 0x80);
    test_imulb(0x10, 0x10);

    test_imulw(0, 0x1234001d, 45);
    test_imulw(0, 23, -45);
    test_imulw(0, 0x8000, 0x8000);
    test_imulw(0, 0x100, 0x100);

    test_imull(0, 0x1234001d, 45);
    test_imull(0, 23, -45);
    test_imull(0, 0x80000000, 0x80000000);
    test_imull(0, 0x10000, 0x10000);

    test_mulb(0x1234561d, 4);
    test_mulb(3, -4);
    test_mulb(0x80, 0x80);
    test_mulb(0x10, 0x10);

    test_mulw(0, 0x1234001d, 45);
    test_mulw(0, 23, -45);
    test_mulw(0, 0x8000, 0x8000);
    test_mulw(0, 0x100, 0x100);

    test_mull(0, 0x1234001d, 45);
    test_mull(0, 23, -45);
    test_mull(0, 0x80000000, 0x80000000);
    test_mull(0, 0x10000, 0x10000);

    test_imulw2(0x1234001d, 45);
    test_imulw2(23, -45);
    test_imulw2(0x8000, 0x8000);
    test_imulw2(0x100, 0x100);

    test_imull2(0x1234001d, 45);
    test_imull2(23, -45);
    test_imull2(0x80000000, 0x80000000);
    test_imull2(0x10000, 0x10000);

B
bellard 已提交
598 599 600 601 602
    TEST_IMUL_IM("w", "w", 45, 0x1234);
    TEST_IMUL_IM("w", "w", -45, 23);
    TEST_IMUL_IM("w", "w", 0x8000, 0x80000000);
    TEST_IMUL_IM("w", "w", 0x7fff, 0x1000);

B
bellard 已提交
603 604 605 606
    TEST_IMUL_IM("l", "k", 45, 0x1234);
    TEST_IMUL_IM("l", "k", -45, 23);
    TEST_IMUL_IM("l", "k", 0x8000, 0x80000000);
    TEST_IMUL_IM("l", "k", 0x7fff, 0x1000);
B
bellard 已提交
607

B
bellard 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    test_idivb(0x12341678, 0x127e);
    test_idivb(0x43210123, -5);
    test_idivb(0x12340004, -1);

    test_idivw(0, 0x12345678, 12347);
    test_idivw(0, -23223, -45);
    test_idivw(0, 0x12348000, -1);
    test_idivw(0x12343, 0x12345678, 0x81238567);

    test_idivl(0, 0x12345678, 12347);
    test_idivl(0, -233223, -45);
    test_idivl(0, 0x80000000, -1);
    test_idivl(0x12343, 0x12345678, 0x81234567);

    test_divb(0x12341678, 0x127e);
    test_divb(0x43210123, -5);
    test_divb(0x12340004, -1);

    test_divw(0, 0x12345678, 12347);
    test_divw(0, -23223, -45);
    test_divw(0, 0x12348000, -1);
    test_divw(0x12343, 0x12345678, 0x81238567);

    test_divl(0, 0x12345678, 12347);
    test_divl(0, -233223, -45);
    test_divl(0, 0x80000000, -1);
    test_divl(0x12343, 0x12345678, 0x81234567);
B
bellard 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

#if defined(__x86_64__)
    test_imulq(0, 0x1234001d1234001d, 45);
    test_imulq(0, 23, -45);
    test_imulq(0, 0x8000000000000000, 0x8000000000000000);
    test_imulq(0, 0x100000000, 0x100000000);

    test_mulq(0, 0x1234001d1234001d, 45);
    test_mulq(0, 23, -45);
    test_mulq(0, 0x8000000000000000, 0x8000000000000000);
    test_mulq(0, 0x100000000, 0x100000000);

    test_imulq2(0x1234001d1234001d, 45);
    test_imulq2(23, -45);
    test_imulq2(0x8000000000000000, 0x8000000000000000);
    test_imulq2(0x100000000, 0x100000000);

    TEST_IMUL_IM("q", "", 45, 0x12341234);
    TEST_IMUL_IM("q", "", -45, 23);
    TEST_IMUL_IM("q", "", 0x8000, 0x8000000000000000);
    TEST_IMUL_IM("q", "", 0x7fff, 0x10000000);

    test_idivq(0, 0x12345678abcdef, 12347);
    test_idivq(0, -233223, -45);
    test_idivq(0, 0x8000000000000000, -1);
    test_idivq(0x12343, 0x12345678, 0x81234567);

    test_divq(0, 0x12345678abcdef, 12347);
    test_divq(0, -233223, -45);
    test_divq(0, 0x8000000000000000, -1);
    test_divq(0x12343, 0x12345678, 0x81234567);
#endif
B
bellard 已提交
667 668
}

B
bellard 已提交
669 670
#define TEST_BSX(op, size, op0)\
{\
B
bellard 已提交
671
    long res, val, resz;\
B
bellard 已提交
672
    val = op0;\
B
bellard 已提交
673 674
    asm("xor %1, %1\n"\
        "mov $0x12345678, %0\n"\
B
bellard 已提交
675
        #op " %" size "2, %" size "0 ; setz %b1" \
B
bellard 已提交
676 677
        : "=r" (res), "=q" (resz)\
        : "g" (val));\
B
bellard 已提交
678
    printf("%-10s A=" FMTLX " R=" FMTLX " %ld\n", #op, val, res, resz);\
B
bellard 已提交
679 680 681 682 683 684 685 686
}

void test_bsx(void)
{
    TEST_BSX(bsrw, "w", 0);
    TEST_BSX(bsrw, "w", 0x12340128);
    TEST_BSX(bsfw, "w", 0);
    TEST_BSX(bsfw, "w", 0x12340128);
B
bellard 已提交
687 688 689 690 691 692 693 694 695 696
    TEST_BSX(bsrl, "k", 0);
    TEST_BSX(bsrl, "k", 0x00340128);
    TEST_BSX(bsfl, "k", 0);
    TEST_BSX(bsfl, "k", 0x00340128);
#if defined(__x86_64__)
    TEST_BSX(bsrq, "", 0);
    TEST_BSX(bsrq, "", 0x003401281234);
    TEST_BSX(bsfq, "", 0);
    TEST_BSX(bsfq, "", 0x003401281234);
#endif
B
bellard 已提交
697 698
}

B
bellard 已提交
699 700
/**********************************************/

B
bellard 已提交
701 702 703 704 705 706 707 708
union float64u {
    double d;
    uint64_t l;
};

union float64u q_nan = { .l = 0xFFF8000000000000 };
union float64u s_nan = { .l = 0xFFF0000000000000 };

B
bellard 已提交
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
void test_fops(double a, double b)
{
    printf("a=%f b=%f a+b=%f\n", a, b, a + b);
    printf("a=%f b=%f a-b=%f\n", a, b, a - b);
    printf("a=%f b=%f a*b=%f\n", a, b, a * b);
    printf("a=%f b=%f a/b=%f\n", a, b, a / b);
    printf("a=%f b=%f fmod(a, b)=%f\n", a, b, fmod(a, b));
    printf("a=%f sqrt(a)=%f\n", a, sqrt(a));
    printf("a=%f sin(a)=%f\n", a, sin(a));
    printf("a=%f cos(a)=%f\n", a, cos(a));
    printf("a=%f tan(a)=%f\n", a, tan(a));
    printf("a=%f log(a)=%f\n", a, log(a));
    printf("a=%f exp(a)=%f\n", a, exp(a));
    printf("a=%f b=%f atan2(a, b)=%f\n", a, b, atan2(a, b));
    /* just to test some op combining */
    printf("a=%f asin(sin(a))=%f\n", a, asin(sin(a)));
    printf("a=%f acos(cos(a))=%f\n", a, acos(cos(a)));
    printf("a=%f atan(tan(a))=%f\n", a, atan(tan(a)));

}

B
bellard 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
void fpu_clear_exceptions(void)
{
    struct __attribute__((packed)) {
        uint16_t fpuc;
        uint16_t dummy1;
        uint16_t fpus;
        uint16_t dummy2;
        uint16_t fptag;
        uint16_t dummy3;
        uint32_t ignored[4];
        long double fpregs[8];
    } float_env32;
    
    asm volatile ("fnstenv %0\n" : : "m" (float_env32));
    float_env32.fpus &= ~0x7f;
    asm volatile ("fldenv %0\n" : : "m" (float_env32));
}

/* XXX: display exception bits when supported */
#define FPUS_EMASK 0x0000
//#define FPUS_EMASK 0x007f

B
bellard 已提交
752 753
void test_fcmp(double a, double b)
{
B
bellard 已提交
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    long eflags, fpus;

    fpu_clear_exceptions();
    asm("fcom %2\n"
        "fstsw %%ax\n"
        : "=a" (fpus)
        : "t" (a), "u" (b));
    printf("fcom(%f %f)=%04lx \n", 
           a, b, fpus & (0x4500 | FPUS_EMASK));
    fpu_clear_exceptions();
    asm("fucom %2\n"
        "fstsw %%ax\n"
        : "=a" (fpus)
        : "t" (a), "u" (b));
    printf("fucom(%f %f)=%04lx\n", 
           a, b, fpus & (0x4500 | FPUS_EMASK));
B
bellard 已提交
770 771
    if (TEST_FCOMI) {
        /* test f(u)comi instruction */
B
bellard 已提交
772 773 774
        fpu_clear_exceptions();
        asm("fcomi %3, %2\n"
            "fstsw %%ax\n"
B
bellard 已提交
775 776
            "pushf\n"
            "pop %0\n"
B
bellard 已提交
777
            : "=r" (eflags), "=a" (fpus)
B
bellard 已提交
778
            : "t" (a), "u" (b));
B
bellard 已提交
779 780 781 782 783 784 785 786 787 788 789
        printf("fcomi(%f %f)=%04lx %02lx\n", 
               a, b, fpus & FPUS_EMASK, eflags & (CC_Z | CC_P | CC_C));
        fpu_clear_exceptions();
        asm("fucomi %3, %2\n"
            "fstsw %%ax\n"
            "pushf\n"
            "pop %0\n"
            : "=r" (eflags), "=a" (fpus)
            : "t" (a), "u" (b));
        printf("fucomi(%f %f)=%04lx %02lx\n", 
               a, b, fpus & FPUS_EMASK, eflags & (CC_Z | CC_P | CC_C));
B
bellard 已提交
790
    }
B
bellard 已提交
791
    fpu_clear_exceptions();
B
bellard 已提交
792 793 794 795 796 797
    asm volatile("fxam\n"
                 "fstsw %%ax\n"
                 : "=a" (fpus)
                 : "t" (a));
    printf("fxam(%f)=%04lx\n", a, fpus & 0x4700);
    fpu_clear_exceptions();
B
bellard 已提交
798 799 800 801 802 803
}

void test_fcvt(double a)
{
    float fa;
    long double la;
B
bellard 已提交
804 805 806 807 808 809
    int16_t fpuc;
    int i;
    int64_t lla;
    int ia;
    int16_t wa;
    double ra;
B
bellard 已提交
810 811 812 813 814

    fa = a;
    la = a;
    printf("(float)%f = %f\n", a, fa);
    printf("(long double)%f = %Lf\n", a, la);
B
bellard 已提交
815 816
    printf("a=" FMT64X "\n", *(uint64_t *)&a);
    printf("la=" FMT64X " %04x\n", *(uint64_t *)&la, 
B
bellard 已提交
817
           *(unsigned short *)((char *)(&la) + 8));
B
bellard 已提交
818 819 820 821 822 823 824 825 826 827 828 829

    /* test all roundings */
    asm volatile ("fstcw %0" : "=m" (fpuc));
    for(i=0;i<4;i++) {
        asm volatile ("fldcw %0" : : "m" ((fpuc & ~0x0c00) | (i << 10)));
        asm volatile ("fist %0" : "=m" (wa) : "t" (a));
        asm volatile ("fistl %0" : "=m" (ia) : "t" (a));
        asm volatile ("fistpll %0" : "=m" (lla) : "t" (a) : "st");
        asm volatile ("frndint ; fstl %0" : "=m" (ra) : "t" (a));
        asm volatile ("fldcw %0" : : "m" (fpuc));
        printf("(short)a = %d\n", wa);
        printf("(int)a = %d\n", ia);
B
bellard 已提交
830
        printf("(int64_t)a = " FMT64X "\n", lla);
B
bellard 已提交
831 832
        printf("rint(a) = %f\n", ra);
    }
B
bellard 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
}

#define TEST(N) \
    asm("fld" #N : "=t" (a)); \
    printf("fld" #N "= %f\n", a);

void test_fconst(void)
{
    double a;
    TEST(1);
    TEST(l2t);
    TEST(l2e);
    TEST(pi);
    TEST(lg2);
    TEST(ln2);
    TEST(z);
}

B
bellard 已提交
851 852 853 854 855 856 857 858 859 860 861
void test_fbcd(double a)
{
    unsigned short bcd[5];
    double b;

    asm("fbstp %0" : "=m" (bcd[0]) : "t" (a) : "st");
    asm("fbld %1" : "=t" (b) : "m" (bcd[0]));
    printf("a=%f bcd=%04x%04x%04x%04x%04x b=%f\n", 
           a, bcd[4], bcd[3], bcd[2], bcd[1], bcd[0], b);
}

B
bellard 已提交
862
#define TEST_ENV(env, save, restore)\
B
bellard 已提交
863 864
{\
    memset((env), 0xaa, sizeof(*(env)));\
B
bellard 已提交
865 866
    for(i=0;i<5;i++)\
        asm volatile ("fldl %0" : : "m" (dtab[i]));\
B
bellard 已提交
867 868
    asm volatile (save " %0\n" : : "m" (*(env)));\
    asm volatile (restore " %0\n": : "m" (*(env)));\
B
bellard 已提交
869 870 871 872
    for(i=0;i<5;i++)\
        asm volatile ("fstpl %0" : "=m" (rtab[i]));\
    for(i=0;i<5;i++)\
        printf("res[%d]=%f\n", i, rtab[i]);\
B
bellard 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
    printf("fpuc=%04x fpus=%04x fptag=%04x\n",\
           (env)->fpuc,\
           (env)->fpus & 0xff00,\
           (env)->fptag);\
}

void test_fenv(void)
{
    struct __attribute__((packed)) {
        uint16_t fpuc;
        uint16_t dummy1;
        uint16_t fpus;
        uint16_t dummy2;
        uint16_t fptag;
        uint16_t dummy3;
        uint32_t ignored[4];
        long double fpregs[8];
    } float_env32;
    struct __attribute__((packed)) {
        uint16_t fpuc;
        uint16_t fpus;
        uint16_t fptag;
        uint16_t ignored[4];
        long double fpregs[8];
    } float_env16;
B
bellard 已提交
898 899 900 901 902 903
    double dtab[8];
    double rtab[8];
    int i;

    for(i=0;i<8;i++)
        dtab[i] = i + 1;
B
bellard 已提交
904

B
bellard 已提交
905 906 907 908
    TEST_ENV(&float_env16, "data16 fnstenv", "data16 fldenv");
    TEST_ENV(&float_env16, "data16 fnsave", "data16 frstor");
    TEST_ENV(&float_env32, "fnstenv", "fldenv");
    TEST_ENV(&float_env32, "fnsave", "frstor");
B
bellard 已提交
909 910 911 912 913 914 915 916

    /* test for ffree */
    for(i=0;i<5;i++)
        asm volatile ("fldl %0" : : "m" (dtab[i]));
    asm volatile("ffree %st(2)");
    asm volatile ("fnstenv %0\n" : : "m" (float_env32));
    asm volatile ("fninit");
    printf("fptag=%04x\n", float_env32.fptag);
B
bellard 已提交
917 918
}

B
bellard 已提交
919 920 921 922 923 924 925 926 927

#define TEST_FCMOV(a, b, eflags, CC)\
{\
    double res;\
    asm("push %3\n"\
        "popf\n"\
        "fcmov" CC " %2, %0\n"\
        : "=t" (res)\
        : "0" (a), "u" (b), "g" (eflags));\
B
bellard 已提交
928 929
    printf("fcmov%s eflags=0x%04lx-> %f\n", \
           CC, (long)eflags, res);\
B
bellard 已提交
930 931 932 933 934
}

void test_fcmov(void)
{
    double a, b;
B
bellard 已提交
935
    long eflags, i;
B
bellard 已提交
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

    a = 1.0;
    b = 2.0;
    for(i = 0; i < 4; i++) {
        eflags = 0;
        if (i & 1)
            eflags |= CC_C;
        if (i & 2)
            eflags |= CC_Z;
        TEST_FCMOV(a, b, eflags, "b");
        TEST_FCMOV(a, b, eflags, "e");
        TEST_FCMOV(a, b, eflags, "be");
        TEST_FCMOV(a, b, eflags, "nb");
        TEST_FCMOV(a, b, eflags, "ne");
        TEST_FCMOV(a, b, eflags, "nbe");
    }
    TEST_FCMOV(a, b, 0, "u");
B
bellard 已提交
953 954
    TEST_FCMOV(a, b, CC_P, "u");
    TEST_FCMOV(a, b, 0, "nu");
B
bellard 已提交
955 956 957
    TEST_FCMOV(a, b, CC_P, "nu");
}

B
bellard 已提交
958 959 960 961 962 963 964
void test_floats(void)
{
    test_fops(2, 3);
    test_fops(1.4, -5);
    test_fcmp(2, -1);
    test_fcmp(2, 2);
    test_fcmp(2, 3);
B
bellard 已提交
965 966
    test_fcmp(2, q_nan.d);
    test_fcmp(q_nan.d, -1);
B
bellard 已提交
967 968
    test_fcmp(-1.0/0.0, -1);
    test_fcmp(1.0/0.0, -1);
B
bellard 已提交
969 970
    test_fcvt(0.5);
    test_fcvt(-0.5);
B
bellard 已提交
971 972
    test_fcvt(1.0/7.0);
    test_fcvt(-1.0/9.0);
B
bellard 已提交
973 974
    test_fcvt(32768);
    test_fcvt(-1e20);
B
bellard 已提交
975 976 977
    test_fcvt(-1.0/0.0);
    test_fcvt(1.0/0.0);
    test_fcvt(q_nan.d);
B
bellard 已提交
978
    test_fconst();
B
bellard 已提交
979 980
    test_fbcd(1234567890123456);
    test_fbcd(-123451234567890);
B
bellard 已提交
981
    test_fenv();
B
bellard 已提交
982 983 984
    if (TEST_CMOV) {
        test_fcmov();
    }
B
bellard 已提交
985
}
B
bellard 已提交
986

B
bellard 已提交
987
/**********************************************/
B
bellard 已提交
988
#if !defined(__x86_64__)
B
bellard 已提交
989 990 991 992 993 994 995 996 997 998

#define TEST_BCD(op, op0, cc_in, cc_mask)\
{\
    int res, flags;\
    res = op0;\
    flags = cc_in;\
    asm ("push %3\n\t"\
         "popf\n\t"\
         #op "\n\t"\
         "pushf\n\t"\
B
bellard 已提交
999
         "pop %1\n\t"\
B
bellard 已提交
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
        : "=a" (res), "=g" (flags)\
        : "0" (res), "1" (flags));\
    printf("%-10s A=%08x R=%08x CCIN=%04x CC=%04x\n",\
           #op, op0, res, cc_in, flags & cc_mask);\
}

void test_bcd(void)
{
    TEST_BCD(daa, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(daa, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));

    TEST_BCD(das, 0x12340503, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340506, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340507, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340559, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340560, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x1234059f, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x123405a0, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340503, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340506, 0, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340503, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340506, CC_C, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340503, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));
    TEST_BCD(das, 0x12340506, CC_C | CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_A));

    TEST_BCD(aaa, 0x12340205, CC_A, (CC_C | CC_A));
    TEST_BCD(aaa, 0x12340306, CC_A, (CC_C | CC_A));
    TEST_BCD(aaa, 0x1234040a, CC_A, (CC_C | CC_A));
    TEST_BCD(aaa, 0x123405fa, CC_A, (CC_C | CC_A));
    TEST_BCD(aaa, 0x12340205, 0, (CC_C | CC_A));
    TEST_BCD(aaa, 0x12340306, 0, (CC_C | CC_A));
    TEST_BCD(aaa, 0x1234040a, 0, (CC_C | CC_A));
    TEST_BCD(aaa, 0x123405fa, 0, (CC_C | CC_A));
    
    TEST_BCD(aas, 0x12340205, CC_A, (CC_C | CC_A));
    TEST_BCD(aas, 0x12340306, CC_A, (CC_C | CC_A));
    TEST_BCD(aas, 0x1234040a, CC_A, (CC_C | CC_A));
    TEST_BCD(aas, 0x123405fa, CC_A, (CC_C | CC_A));
    TEST_BCD(aas, 0x12340205, 0, (CC_C | CC_A));
    TEST_BCD(aas, 0x12340306, 0, (CC_C | CC_A));
    TEST_BCD(aas, 0x1234040a, 0, (CC_C | CC_A));
    TEST_BCD(aas, 0x123405fa, 0, (CC_C | CC_A));

    TEST_BCD(aam, 0x12340547, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
    TEST_BCD(aad, 0x12340407, CC_A, (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));
}
B
bellard 已提交
1057
#endif
B
bellard 已提交
1058

B
bellard 已提交
1059 1060
#define TEST_XCHG(op, size, opconst)\
{\
B
bellard 已提交
1061 1062 1063
    long op0, op1;\
    op0 = i2l(0x12345678);\
    op1 = i2l(0xfbca7654);\
B
bellard 已提交
1064 1065 1066
    asm(#op " %" size "0, %" size "1" \
        : "=q" (op0), opconst (op1) \
        : "0" (op0), "1" (op1));\
B
bellard 已提交
1067
    printf("%-10s A=" FMTLX " B=" FMTLX "\n",\
B
bellard 已提交
1068 1069 1070 1071 1072
           #op, op0, op1);\
}

#define TEST_CMPXCHG(op, size, opconst, eax)\
{\
B
bellard 已提交
1073 1074 1075 1076
    long op0, op1, op2;\
    op0 = i2l(0x12345678);\
    op1 = i2l(0xfbca7654);\
    op2 = i2l(eax);\
B
bellard 已提交
1077 1078
    asm(#op " %" size "0, %" size "1" \
        : "=q" (op0), opconst (op1) \
B
bellard 已提交
1079 1080 1081
        : "0" (op0), "1" (op1), "a" (op2));\
    printf("%-10s EAX=" FMTLX " A=" FMTLX " C=" FMTLX "\n",\
           #op, op2, op0, op1);\
B
bellard 已提交
1082 1083 1084 1085
}

void test_xchg(void)
{
B
bellard 已提交
1086 1087 1088 1089
#if defined(__x86_64__)
    TEST_XCHG(xchgq, "", "=q");
#endif
    TEST_XCHG(xchgl, "k", "=q");
B
bellard 已提交
1090 1091 1092
    TEST_XCHG(xchgw, "w", "=q");
    TEST_XCHG(xchgb, "b", "=q");

B
bellard 已提交
1093 1094 1095 1096
#if defined(__x86_64__)
    TEST_XCHG(xchgq, "", "=m");
#endif
    TEST_XCHG(xchgl, "k", "=m");
B
bellard 已提交
1097 1098 1099
    TEST_XCHG(xchgw, "w", "=m");
    TEST_XCHG(xchgb, "b", "=m");

B
bellard 已提交
1100 1101 1102 1103
#if defined(__x86_64__)
    TEST_XCHG(xaddq, "", "=q");
#endif
    TEST_XCHG(xaddl, "k", "=q");
B
bellard 已提交
1104 1105 1106
    TEST_XCHG(xaddw, "w", "=q");
    TEST_XCHG(xaddb, "b", "=q");

B
bellard 已提交
1107 1108 1109 1110 1111 1112 1113
    {
        int res;
        res = 0x12345678;
        asm("xaddl %1, %0" : "=r" (res) : "0" (res));
        printf("xaddl same res=%08x\n", res);
    }

B
bellard 已提交
1114 1115 1116 1117
#if defined(__x86_64__)
    TEST_XCHG(xaddq, "", "=m");
#endif
    TEST_XCHG(xaddl, "k", "=m");
B
bellard 已提交
1118 1119 1120
    TEST_XCHG(xaddw, "w", "=m");
    TEST_XCHG(xaddb, "b", "=m");

B
bellard 已提交
1121 1122 1123 1124
#if defined(__x86_64__)
    TEST_CMPXCHG(cmpxchgq, "", "=q", 0xfbca7654);
#endif
    TEST_CMPXCHG(cmpxchgl, "k", "=q", 0xfbca7654);
B
bellard 已提交
1125 1126 1127
    TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfbca7654);
    TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfbca7654);

B
bellard 已提交
1128 1129 1130 1131
#if defined(__x86_64__)
    TEST_CMPXCHG(cmpxchgq, "", "=q", 0xfffefdfc);
#endif
    TEST_CMPXCHG(cmpxchgl, "k", "=q", 0xfffefdfc);
B
bellard 已提交
1132 1133 1134
    TEST_CMPXCHG(cmpxchgw, "w", "=q", 0xfffefdfc);
    TEST_CMPXCHG(cmpxchgb, "b", "=q", 0xfffefdfc);

B
bellard 已提交
1135 1136 1137 1138
#if defined(__x86_64__)
    TEST_CMPXCHG(cmpxchgq, "", "=m", 0xfbca7654);
#endif
    TEST_CMPXCHG(cmpxchgl, "k", "=m", 0xfbca7654);
B
bellard 已提交
1139 1140 1141
    TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfbca7654);
    TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfbca7654);

B
bellard 已提交
1142 1143 1144 1145
#if defined(__x86_64__)
    TEST_CMPXCHG(cmpxchgq, "", "=m", 0xfffefdfc);
#endif
    TEST_CMPXCHG(cmpxchgl, "k", "=m", 0xfffefdfc);
B
bellard 已提交
1146 1147
    TEST_CMPXCHG(cmpxchgw, "w", "=m", 0xfffefdfc);
    TEST_CMPXCHG(cmpxchgb, "b", "=m", 0xfffefdfc);
B
bellard 已提交
1148 1149 1150

    {
        uint64_t op0, op1, op2;
B
bellard 已提交
1151
        long i, eflags;
B
bellard 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161

        for(i = 0; i < 2; i++) {
            op0 = 0x123456789abcd;
            if (i == 0)
                op1 = 0xfbca765423456;
            else
                op1 = op0;
            op2 = 0x6532432432434;
            asm("cmpxchg8b %1\n" 
                "pushf\n"
B
bellard 已提交
1162
                "pop %2\n"
B
bellard 已提交
1163 1164
                : "=A" (op0), "=m" (op1), "=g" (eflags)
                : "0" (op0), "m" (op1), "b" ((int)op2), "c" ((int)(op2 >> 32)));
B
bellard 已提交
1165
            printf("cmpxchg8b: op0=" FMT64X " op1=" FMT64X " CC=%02lx\n", 
B
bellard 已提交
1166 1167 1168
                    op0, op1, eflags & CC_Z);
        }
    }
B
bellard 已提交
1169 1170
}

B
bellard 已提交
1171
#ifdef TEST_SEGS
B
bellard 已提交
1172 1173 1174 1175 1176
/**********************************************/
/* segmentation tests */

#include <asm/ldt.h>
#include <linux/unistd.h>
B
bellard 已提交
1177
#include <linux/version.h>
B
bellard 已提交
1178 1179 1180

_syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)

B
bellard 已提交
1181 1182 1183 1184
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 66)
#define modify_ldt_ldt_s user_desc
#endif

B
bellard 已提交
1185 1186
#define MK_SEL(n) (((n) << 3) | 7)

B
bellard 已提交
1187 1188 1189
uint8_t seg_data1[4096];
uint8_t seg_data2[4096];

B
bellard 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
#define TEST_LR(op, size, seg, mask)\
{\
    int res, res2;\
    res = 0x12345678;\
    asm (op " %" size "2, %" size "0\n" \
         "movl $0, %1\n"\
         "jnz 1f\n"\
         "movl $1, %1\n"\
         "1:\n"\
         : "=r" (res), "=r" (res2) : "m" (seg), "0" (res));\
    printf(op ": Z=%d %08x\n", res2, res & ~(mask));\
}

B
bellard 已提交
1203 1204 1205 1206 1207
/* NOTE: we use Linux modify_ldt syscall */
void test_segs(void)
{
    struct modify_ldt_ldt_s ldt;
    long long ldt_table[3];
B
bellard 已提交
1208
    int res, res2;
B
bellard 已提交
1209
    char tmp;
B
bellard 已提交
1210 1211 1212 1213
    struct {
        uint32_t offset;
        uint16_t seg;
    } __attribute__((packed)) segoff;
B
bellard 已提交
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237

    ldt.entry_number = 1;
    ldt.base_addr = (unsigned long)&seg_data1;
    ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
    ldt.seg_32bit = 1;
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
    ldt.read_exec_only = 0;
    ldt.limit_in_pages = 1;
    ldt.seg_not_present = 0;
    ldt.useable = 1;
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */

    ldt.entry_number = 2;
    ldt.base_addr = (unsigned long)&seg_data2;
    ldt.limit = (sizeof(seg_data2) + 0xfff) >> 12;
    ldt.seg_32bit = 1;
    ldt.contents = MODIFY_LDT_CONTENTS_DATA;
    ldt.read_exec_only = 0;
    ldt.limit_in_pages = 1;
    ldt.seg_not_present = 0;
    ldt.useable = 1;
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */

    modify_ldt(0, &ldt_table, sizeof(ldt_table)); /* read ldt entries */
B
bellard 已提交
1238 1239 1240 1241 1242 1243 1244
#if 0
    {
        int i;
        for(i=0;i<3;i++)
            printf("%d: %016Lx\n", i, ldt_table[i]);
    }
#endif
B
bellard 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253
    /* do some tests with fs or gs */
    asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));

    seg_data1[1] = 0xaa;
    seg_data2[1] = 0x55;

    asm volatile ("fs movzbl 0x1, %0" : "=r" (res));
    printf("FS[1] = %02x\n", res);

B
bellard 已提交
1254 1255 1256 1257 1258 1259
    asm volatile ("pushl %%gs\n"
                  "movl %1, %%gs\n"
                  "gs movzbl 0x1, %0\n"
                  "popl %%gs\n"
                  : "=r" (res)
                  : "r" (MK_SEL(2)));
B
bellard 已提交
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
    printf("GS[1] = %02x\n", res);

    /* tests with ds/ss (implicit segment case) */
    tmp = 0xa5;
    asm volatile ("pushl %%ebp\n\t"
                  "pushl %%ds\n\t"
                  "movl %2, %%ds\n\t"
                  "movl %3, %%ebp\n\t"
                  "movzbl 0x1, %0\n\t"
                  "movzbl (%%ebp), %1\n\t"
                  "popl %%ds\n\t"
                  "popl %%ebp\n\t"
                  : "=r" (res), "=r" (res2)
                  : "r" (MK_SEL(1)), "r" (&tmp));
    printf("DS[1] = %02x\n", res);
    printf("SS[tmp] = %02x\n", res2);
B
bellard 已提交
1276 1277 1278 1279 1280 1281 1282 1283

    segoff.seg = MK_SEL(2);
    segoff.offset = 0xabcdef12;
    asm volatile("lfs %2, %0\n\t" 
                 "movl %%fs, %1\n\t"
                 : "=r" (res), "=g" (res2) 
                 : "m" (segoff));
    printf("FS:reg = %04x:%08x\n", res2, res);
B
bellard 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293

    TEST_LR("larw", "w", MK_SEL(2), 0x0100);
    TEST_LR("larl", "", MK_SEL(2), 0x0100);
    TEST_LR("lslw", "w", MK_SEL(2), 0);
    TEST_LR("lsll", "", MK_SEL(2), 0);

    TEST_LR("larw", "w", 0xfff8, 0);
    TEST_LR("larl", "", 0xfff8, 0);
    TEST_LR("lslw", "w", 0xfff8, 0);
    TEST_LR("lsll", "", 0xfff8, 0);
B
bellard 已提交
1294
}
B
bellard 已提交
1295

B
bellard 已提交
1296 1297 1298 1299 1300
/* 16 bit code test */
extern char code16_start, code16_end;
extern char code16_func1;
extern char code16_func2;
extern char code16_func3;
B
bellard 已提交
1301

B
bellard 已提交
1302
void test_code16(void)
B
bellard 已提交
1303
{
B
bellard 已提交
1304 1305
    struct modify_ldt_ldt_s ldt;
    int res, res2;
B
bellard 已提交
1306

B
bellard 已提交
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    /* build a code segment */
    ldt.entry_number = 1;
    ldt.base_addr = (unsigned long)&code16_start;
    ldt.limit = &code16_end - &code16_start;
    ldt.seg_32bit = 0;
    ldt.contents = MODIFY_LDT_CONTENTS_CODE;
    ldt.read_exec_only = 0;
    ldt.limit_in_pages = 0;
    ldt.seg_not_present = 0;
    ldt.useable = 1;
    modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
B
bellard 已提交
1318

B
bellard 已提交
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
    /* call the first function */
    asm volatile ("lcall %1, %2" 
                  : "=a" (res)
                  : "i" (MK_SEL(1)), "i" (&code16_func1): "memory", "cc");
    printf("func1() = 0x%08x\n", res);
    asm volatile ("lcall %2, %3" 
                  : "=a" (res), "=c" (res2)
                  : "i" (MK_SEL(1)), "i" (&code16_func2): "memory", "cc");
    printf("func2() = 0x%08x spdec=%d\n", res, res2);
    asm volatile ("lcall %1, %2" 
                  : "=a" (res)
                  : "i" (MK_SEL(1)), "i" (&code16_func3): "memory", "cc");
    printf("func3() = 0x%08x\n", res);
B
bellard 已提交
1332
}
B
bellard 已提交
1333
#endif
B
bellard 已提交
1334

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
#if defined(__x86_64__)
asm(".globl func_lret\n"
    "func_lret:\n"
    "movl $0x87654641, %eax\n"
    "lretq\n");
#else
asm(".globl func_lret\n"
    "func_lret:\n"
    "movl $0x87654321, %eax\n"
    "lret\n"

    ".globl func_iret\n"
    "func_iret:\n"
    "movl $0xabcd4321, %eax\n"
    "iret\n");
#endif

extern char func_lret;
extern char func_iret;
B
bellard 已提交
1354

B
bellard 已提交
1355 1356 1357
void test_misc(void)
{
    char table[256];
B
bellard 已提交
1358
    long res, i;
B
bellard 已提交
1359 1360 1361 1362

    for(i=0;i<256;i++) table[i] = 256 - i;
    res = 0x12345678;
    asm ("xlat" : "=a" (res) : "b" (table), "0" (res));
B
bellard 已提交
1363
    printf("xlat: EAX=" FMTLX "\n", res);
B
bellard 已提交
1364

1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
#if defined(__x86_64__)
    {
        static struct __attribute__((packed)) {
            uint32_t offset;
            uint16_t seg;
        } desc;
        long cs_sel;

        asm volatile ("mov %%cs, %0" : "=r" (cs_sel));

        asm volatile ("push %1\n"
                      "call func_lret\n" 
                      : "=a" (res)
                      : "r" (cs_sel) : "memory", "cc");
        printf("func_lret=" FMTLX "\n", res);

        /* NOTE: we assume that &func_lret < 4GB */
        desc.offset = (long)&func_lret;
        desc.seg = cs_sel;
        
        asm volatile ("xor %%rax, %%rax\n"
                      "rex64 lcall %1\n"
                      : "=a" (res)
                      : "m" (desc) 
                      : "memory", "cc");
        printf("func_lret2=" FMTLX "\n", res);

        asm volatile ("push %2\n"
                      "mov $ 1f, %%rax\n"
                      "push %%rax\n"
                      "ljmp %1\n"
                      "1:\n"
                      : "=a" (res)
                      : "m" (desc), "b" (cs_sel)
                      : "memory", "cc");
        printf("func_lret3=" FMTLX "\n", res);
    }
#else
B
bellard 已提交
1403
    asm volatile ("push %%cs ; call %1" 
B
bellard 已提交
1404
                  : "=a" (res)
1405 1406
                  : "m" (func_lret): "memory", "cc");
    printf("func_lret=" FMTLX "\n", res);
B
bellard 已提交
1407

B
bellard 已提交
1408
    asm volatile ("pushf ; push %%cs ; call %1" 
B
bellard 已提交
1409
                  : "=a" (res)
1410 1411
                  : "m" (func_iret): "memory", "cc");
    printf("func_iret=" FMTLX "\n", res);
B
bellard 已提交
1412
#endif
B
bellard 已提交
1413

B
bellard 已提交
1414 1415 1416 1417 1418 1419
#if defined(__x86_64__)
    /* specific popl test */
    asm volatile ("push $12345432 ; push $0x9abcdef ; pop (%%rsp) ; pop %0"
                  : "=g" (res));
    printf("popl esp=" FMTLX "\n", res);
#else
B
bellard 已提交
1420 1421 1422
    /* specific popl test */
    asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popl (%%esp) ; popl %0"
                  : "=g" (res));
B
bellard 已提交
1423
    printf("popl esp=" FMTLX "\n", res);
B
bellard 已提交
1424 1425 1426 1427

    /* specific popw test */
    asm volatile ("pushl $12345432 ; pushl $0x9abcdef ; popw (%%esp) ; addl $2, %%esp ; popl %0"
                  : "=g" (res));
B
bellard 已提交
1428 1429
    printf("popw esp=" FMTLX "\n", res);
#endif
B
bellard 已提交
1430 1431 1432 1433 1434 1435
}

uint8_t str_buffer[4096];

#define TEST_STRING1(OP, size, DF, REP)\
{\
B
bellard 已提交
1436
    long esi, edi, eax, ecx, eflags;\
B
bellard 已提交
1437 1438 1439
\
    esi = (long)(str_buffer + sizeof(str_buffer) / 2);\
    edi = (long)(str_buffer + sizeof(str_buffer) / 2) + 16;\
B
bellard 已提交
1440
    eax = i2l(0x12345678);\
B
bellard 已提交
1441 1442
    ecx = 17;\
\
B
bellard 已提交
1443
    asm volatile ("push $0\n\t"\
B
bellard 已提交
1444 1445 1446 1447 1448
                  "popf\n\t"\
                  DF "\n\t"\
                  REP #OP size "\n\t"\
                  "cld\n\t"\
                  "pushf\n\t"\
B
bellard 已提交
1449
                  "pop %4\n\t"\
B
bellard 已提交
1450 1451
                  : "=S" (esi), "=D" (edi), "=a" (eax), "=c" (ecx), "=g" (eflags)\
                  : "0" (esi), "1" (edi), "2" (eax), "3" (ecx));\
B
bellard 已提交
1452
    printf("%-10s ESI=" FMTLX " EDI=" FMTLX " EAX=" FMTLX " ECX=" FMTLX " EFL=%04x\n",\
B
bellard 已提交
1453
           REP #OP size, esi, edi, eax, ecx,\
B
bellard 已提交
1454
           (int)(eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A)));\
B
bellard 已提交
1455 1456 1457 1458 1459 1460
}

#define TEST_STRING(OP, REP)\
    TEST_STRING1(OP, "b", "", REP);\
    TEST_STRING1(OP, "w", "", REP);\
    TEST_STRING1(OP, "l", "", REP);\
B
bellard 已提交
1461
    X86_64_ONLY(TEST_STRING1(OP, "q", "", REP));\
B
bellard 已提交
1462 1463
    TEST_STRING1(OP, "b", "std", REP);\
    TEST_STRING1(OP, "w", "std", REP);\
B
bellard 已提交
1464 1465
    TEST_STRING1(OP, "l", "std", REP);\
    X86_64_ONLY(TEST_STRING1(OP, "q", "std", REP))
B
bellard 已提交
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487

void test_string(void)
{
    int i;
    for(i = 0;i < sizeof(str_buffer); i++)
        str_buffer[i] = i + 0x56;
   TEST_STRING(stos, "");
   TEST_STRING(stos, "rep ");
   TEST_STRING(lods, ""); /* to verify stos */
   TEST_STRING(lods, "rep "); 
   TEST_STRING(movs, "");
   TEST_STRING(movs, "rep ");
   TEST_STRING(lods, ""); /* to verify stos */

   /* XXX: better tests */
   TEST_STRING(scas, "");
   TEST_STRING(scas, "repz ");
   TEST_STRING(scas, "repnz ");
   TEST_STRING(cmps, "");
   TEST_STRING(cmps, "repz ");
   TEST_STRING(cmps, "repnz ");
}
B
bellard 已提交
1488

B
bellard 已提交
1489
#ifdef TEST_VM86
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
/* VM86 test */

static inline void set_bit(uint8_t *a, unsigned int bit)
{
    a[bit / 8] |= (1 << (bit % 8));
}

static inline uint8_t *seg_to_linear(unsigned int seg, unsigned int reg)
{
    return (uint8_t *)((seg << 4) + (reg & 0xffff));
}

static inline void pushw(struct vm86_regs *r, int val)
{
    r->esp = (r->esp & ~0xffff) | ((r->esp - 2) & 0xffff);
    *(uint16_t *)seg_to_linear(r->ss, r->esp) = val;
}

#undef __syscall_return
#define __syscall_return(type, res) \
do { \
	return (type) (res); \
} while (0)

_syscall2(int, vm86, int, func, struct vm86plus_struct *, v86)

extern char vm86_code_start;
extern char vm86_code_end;

#define VM86_CODE_CS 0x100
#define VM86_CODE_IP 0x100

void test_vm86(void)
{
    struct vm86plus_struct ctx;
    struct vm86_regs *r;
    uint8_t *vm86_mem;
    int seg, ret;

    vm86_mem = mmap((void *)0x00000000, 0x110000, 
                    PROT_WRITE | PROT_READ | PROT_EXEC, 
                    MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
    if (vm86_mem == MAP_FAILED) {
        printf("ERROR: could not map vm86 memory");
        return;
    }
    memset(&ctx, 0, sizeof(ctx));

    /* init basic registers */
    r = &ctx.regs;
    r->eip = VM86_CODE_IP;
    r->esp = 0xfffe;
    seg = VM86_CODE_CS;
    r->cs = seg;
    r->ss = seg;
    r->ds = seg;
    r->es = seg;
    r->fs = seg;
    r->gs = seg;
    r->eflags = VIF_MASK;

    /* move code to proper address. We use the same layout as a .com
       dos program. */
    memcpy(vm86_mem + (VM86_CODE_CS << 4) + VM86_CODE_IP, 
           &vm86_code_start, &vm86_code_end - &vm86_code_start);

    /* mark int 0x21 as being emulated */
    set_bit((uint8_t *)&ctx.int_revectored, 0x21);

    for(;;) {
        ret = vm86(VM86_ENTER, &ctx);
        switch(VM86_TYPE(ret)) {
        case VM86_INTx:
            {
1564
                int int_num, ah, v;
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
                
                int_num = VM86_ARG(ret);
                if (int_num != 0x21)
                    goto unknown_int;
                ah = (r->eax >> 8) & 0xff;
                switch(ah) {
                case 0x00: /* exit */
                    goto the_end;
                case 0x02: /* write char */
                    {
                        uint8_t c = r->edx;
                        putchar(c);
                    }
                    break;
                case 0x09: /* write string */
                    {
                        uint8_t c, *ptr;
                        ptr = seg_to_linear(r->ds, r->edx);
                        for(;;) {
                            c = *ptr++;
                            if (c == '$')
                                break;
                            putchar(c);
                        }
                        r->eax = (r->eax & ~0xff) | '$';
                    }
                    break;
1592 1593 1594 1595 1596 1597
                case 0xff: /* extension: write eflags number in edx */
                    v = (int)r->edx;
#ifndef LINUX_VM86_IOPL_FIX
                    v &= ~0x3000;
#endif
                    printf("%08x\n", v);
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
                    break;
                default:
                unknown_int:
                    printf("unsupported int 0x%02x\n", int_num);
                    goto the_end;
                }
            }
            break;
        case VM86_SIGNAL:
            /* a signal came, we just ignore that */
            break;
        case VM86_STI:
            break;
        default:
            printf("ERROR: unhandled vm86 return code (0x%x)\n", ret);
            goto the_end;
        }
    }
 the_end:
    printf("VM86 end\n");
    munmap(vm86_mem, 0x110000);
}
B
bellard 已提交
1620
#endif
1621 1622

/* exception tests */
B
bellard 已提交
1623
#if defined(__i386__) && !defined(REG_EAX)
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
#define REG_EAX EAX
#define REG_EBX EBX
#define REG_ECX ECX
#define REG_EDX EDX
#define REG_ESI ESI
#define REG_EDI EDI
#define REG_EBP EBP
#define REG_ESP ESP
#define REG_EIP EIP
#define REG_EFL EFL
#define REG_TRAPNO TRAPNO
#define REG_ERR ERR
#endif

B
bellard 已提交
1638 1639 1640 1641
#if defined(__x86_64__)
#define REG_EIP REG_RIP
#endif

1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
jmp_buf jmp_env;
int v1;
int tab[2];

void sig_handler(int sig, siginfo_t *info, void *puc)
{
    struct ucontext *uc = puc;

    printf("si_signo=%d si_errno=%d si_code=%d",
           info->si_signo, info->si_errno, info->si_code);
1652 1653
    printf(" si_addr=0x%08lx",
           (unsigned long)info->si_addr);
1654 1655
    printf("\n");

B
bellard 已提交
1656 1657 1658 1659
    printf("trapno=" FMTLX " err=" FMTLX,
           (long)uc->uc_mcontext.gregs[REG_TRAPNO],
           (long)uc->uc_mcontext.gregs[REG_ERR]);
    printf(" EIP=" FMTLX, (long)uc->uc_mcontext.gregs[REG_EIP]);
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
    printf("\n");
    longjmp(jmp_env, 1);
}

void test_exceptions(void)
{
    struct sigaction act;
    volatile int val;
    
    act.sa_sigaction = sig_handler;
    sigemptyset(&act.sa_mask);
B
bellard 已提交
1671
    act.sa_flags = SA_SIGINFO | SA_NODEFER;
1672 1673 1674
    sigaction(SIGFPE, &act, NULL);
    sigaction(SIGILL, &act, NULL);
    sigaction(SIGSEGV, &act, NULL);
1675
    sigaction(SIGBUS, &act, NULL);
1676 1677 1678
    sigaction(SIGTRAP, &act, NULL);

    /* test division by zero reporting */
1679
    printf("DIVZ exception:\n");
1680 1681 1682 1683 1684 1685
    if (setjmp(jmp_env) == 0) {
        /* now divide by zero */
        v1 = 0;
        v1 = 2 / v1;
    }

B
bellard 已提交
1686
#if !defined(__x86_64__)
1687
    printf("BOUND exception:\n");
1688 1689 1690 1691
    if (setjmp(jmp_env) == 0) {
        /* bound exception */
        tab[0] = 1;
        tab[1] = 10;
B
bellard 已提交
1692
        asm volatile ("bound %0, %1" : : "r" (11), "m" (tab[0]));
1693
    }
B
bellard 已提交
1694
#endif
1695

B
bellard 已提交
1696
#ifdef TEST_SEGS
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708
    printf("segment exceptions:\n");
    if (setjmp(jmp_env) == 0) {
        /* load an invalid segment */
        asm volatile ("movl %0, %%fs" : : "r" ((0x1234 << 3) | 1));
    }
    if (setjmp(jmp_env) == 0) {
        /* null data segment is valid */
        asm volatile ("movl %0, %%fs" : : "r" (3));
        /* null stack segment */
        asm volatile ("movl %0, %%ss" : : "r" (3));
    }

B
bellard 已提交
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
    {
        struct modify_ldt_ldt_s ldt;
        ldt.entry_number = 1;
        ldt.base_addr = (unsigned long)&seg_data1;
        ldt.limit = (sizeof(seg_data1) + 0xfff) >> 12;
        ldt.seg_32bit = 1;
        ldt.contents = MODIFY_LDT_CONTENTS_DATA;
        ldt.read_exec_only = 0;
        ldt.limit_in_pages = 1;
        ldt.seg_not_present = 1;
        ldt.useable = 1;
        modify_ldt(1, &ldt, sizeof(ldt)); /* write ldt entry */
        
        if (setjmp(jmp_env) == 0) {
            /* segment not present */
            asm volatile ("movl %0, %%fs" : : "r" (MK_SEL(1)));
        }
1726
    }
B
bellard 已提交
1727
#endif
1728

1729
    /* test SEGV reporting */
1730
    printf("PF exception:\n");
1731
    if (setjmp(jmp_env) == 0) {
1732
        val = 1;
B
bellard 已提交
1733 1734
        /* we add a nop to test a weird PC retrieval case */
        asm volatile ("nop");
1735 1736 1737 1738 1739
        /* now store in an invalid address */
        *(char *)0x1234 = 1;
    }

    /* test SEGV reporting */
1740
    printf("PF exception:\n");
1741
    if (setjmp(jmp_env) == 0) {
1742
        val = 1;
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752
        /* read from an invalid address */
        v1 = *(char *)0x1234;
    }
    
    /* test illegal instruction reporting */
    printf("UD2 exception:\n");
    if (setjmp(jmp_env) == 0) {
        /* now execute an invalid instruction */
        asm volatile("ud2");
    }
B
bellard 已提交
1753 1754 1755 1756 1757
    printf("lock nop exception:\n");
    if (setjmp(jmp_env) == 0) {
        /* now execute an invalid instruction */
        asm volatile("lock nop");
    }
1758 1759 1760 1761 1762
    
    printf("INT exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("int $0xfd");
    }
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
    if (setjmp(jmp_env) == 0) {
        asm volatile ("int $0x01");
    }
    if (setjmp(jmp_env) == 0) {
        asm volatile (".byte 0xcd, 0x03");
    }
    if (setjmp(jmp_env) == 0) {
        asm volatile ("int $0x04");
    }
    if (setjmp(jmp_env) == 0) {
        asm volatile ("int $0x05");
    }
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790

    printf("INT3 exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("int3");
    }

    printf("CLI exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("cli");
    }

    printf("STI exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("cli");
    }

B
bellard 已提交
1791
#if !defined(__x86_64__)
1792 1793 1794 1795 1796
    printf("INTO exception:\n");
    if (setjmp(jmp_env) == 0) {
        /* overflow exception */
        asm volatile ("addl $1, %0 ; into" : : "r" (0x7fffffff));
    }
B
bellard 已提交
1797
#endif
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835

    printf("OUTB exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("outb %%al, %%dx" : : "d" (0x4321), "a" (0));
    }

    printf("INB exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("inb %%dx, %%al" : "=a" (val) : "d" (0x4321));
    }

    printf("REP OUTSB exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("rep outsb" : : "d" (0x4321), "S" (tab), "c" (1));
    }

    printf("REP INSB exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("rep insb" : : "d" (0x4321), "D" (tab), "c" (1));
    }

    printf("HLT exception:\n");
    if (setjmp(jmp_env) == 0) {
        asm volatile ("hlt");
    }

    printf("single step exception:\n");
    val = 0;
    if (setjmp(jmp_env) == 0) {
        asm volatile ("pushf\n"
                      "orl $0x00100, (%%esp)\n"
                      "popf\n"
                      "movl $0xabcd, %0\n" 
                      "movl $0x0, %0\n" : "=m" (val) : : "cc", "memory");
    }
    printf("val=0x%x\n", val);
}

B
bellard 已提交
1836
#if !defined(__x86_64__)
1837 1838 1839 1840
/* specific precise single step test */
void sig_trap_handler(int sig, siginfo_t *info, void *puc)
{
    struct ucontext *uc = puc;
B
bellard 已提交
1841
    printf("EIP=" FMTLX "\n", (long)uc->uc_mcontext.gregs[REG_EIP]);
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
}

const uint8_t sstep_buf1[4] = { 1, 2, 3, 4};
uint8_t sstep_buf2[4];

void test_single_step(void)
{
    struct sigaction act;
    volatile int val;
    int i;

    val = 0;
    act.sa_sigaction = sig_trap_handler;
    sigemptyset(&act.sa_mask);
    act.sa_flags = SA_SIGINFO;
    sigaction(SIGTRAP, &act, NULL);
    asm volatile ("pushf\n"
                  "orl $0x00100, (%%esp)\n"
                  "popf\n"
                  "movl $0xabcd, %0\n" 

                  /* jmp test */
                  "movl $3, %%ecx\n"
                  "1:\n"
                  "addl $1, %0\n"
                  "decl %%ecx\n"
                  "jnz 1b\n"

                  /* movsb: the single step should stop at each movsb iteration */
                  "movl $sstep_buf1, %%esi\n"
                  "movl $sstep_buf2, %%edi\n"
                  "movl $0, %%ecx\n"
                  "rep movsb\n"
                  "movl $3, %%ecx\n"
                  "rep movsb\n"
                  "movl $1, %%ecx\n"
                  "rep movsb\n"

                  /* cmpsb: the single step should stop at each cmpsb iteration */
                  "movl $sstep_buf1, %%esi\n"
                  "movl $sstep_buf2, %%edi\n"
                  "movl $0, %%ecx\n"
                  "rep cmpsb\n"
                  "movl $4, %%ecx\n"
                  "rep cmpsb\n"
                  
                  /* getpid() syscall: single step should skip one
                     instruction */
                  "movl $20, %%eax\n"
                  "int $0x80\n"
                  "movl $0, %%eax\n"
                  
                  /* when modifying SS, trace is not done on the next
                     instruction */
                  "movl %%ss, %%ecx\n"
                  "movl %%ecx, %%ss\n"
                  "addl $1, %0\n"
                  "movl $1, %%eax\n"
                  "movl %%ecx, %%ss\n"
                  "jmp 1f\n"
                  "addl $1, %0\n"
                  "1:\n"
                  "movl $1, %%eax\n"
                  "pushl %%ecx\n"
                  "popl %%ss\n"
                  "addl $1, %0\n"
                  "movl $1, %%eax\n"
                  
                  "pushf\n"
                  "andl $~0x00100, (%%esp)\n"
                  "popf\n"
                  : "=m" (val) 
                  : 
                  : "cc", "memory", "eax", "ecx", "esi", "edi");
    printf("val=%d\n", val);
    for(i = 0; i < 4; i++)
        printf("sstep_buf2[%d] = %d\n", i, sstep_buf2[i]);
}

1921 1922 1923 1924 1925 1926
/* self modifying code test */
uint8_t code[] = {
    0xb8, 0x1, 0x00, 0x00, 0x00, /* movl $1, %eax */
    0xc3, /* ret */
};

B
bellard 已提交
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
asm("smc_code2:\n"
    "movl 4(%esp), %eax\n"
    "movl %eax, smc_patch_addr2 + 1\n"
    "nop\n"
    "nop\n"
    "nop\n"
    "nop\n"
    "nop\n"
    "nop\n"
    "nop\n"
    "nop\n"
    "smc_patch_addr2:\n"
    "movl $1, %eax\n"
    "ret\n");
1941

B
bellard 已提交
1942 1943
typedef int FuncType(void);
extern int smc_code2(int);
1944 1945
void test_self_modifying_code(void)
{
1946
    int i;
1947 1948

    printf("self modifying code:\n");
1949 1950 1951 1952 1953
    printf("func1 = 0x%x\n", ((FuncType *)code)());
    for(i = 2; i <= 4; i++) {
        code[1] = i;
        printf("func%d = 0x%x\n", i, ((FuncType *)code)());
    }
B
bellard 已提交
1954 1955 1956 1957 1958 1959 1960

    /* more difficult test : the modified code is just after the
       modifying instruction. It is forbidden in Intel specs, but it
       is used by old DOS programs */
    for(i = 2; i <= 4; i++) {
        printf("smc_code2(%d) = %d\n", i, smc_code2(i));
    }
1961
}
B
bellard 已提交
1962
#endif
B
bellard 已提交
1963

B
bellard 已提交
1964 1965 1966 1967 1968 1969 1970 1971 1972
long enter_stack[4096];

#if defined(__x86_64__)
#define RSP "%%rsp"
#define RBP "%%rbp"
#else
#define RSP "%%esp"
#define RBP "%%ebp"
#endif
B
bellard 已提交
1973 1974 1975

#define TEST_ENTER(size, stack_type, level)\
{\
B
bellard 已提交
1976
    long esp_save, esp_val, ebp_val, ebp_save, i;\
B
bellard 已提交
1977 1978 1979 1980 1981 1982 1983
    stack_type *ptr, *stack_end, *stack_ptr;\
    memset(enter_stack, 0, sizeof(enter_stack));\
    stack_end = stack_ptr = (stack_type *)(enter_stack + 4096);\
    ebp_val = (long)stack_ptr;\
    for(i=1;i<=32;i++)\
       *--stack_ptr = i;\
    esp_val = (long)stack_ptr;\
B
bellard 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992
    asm("mov " RSP ", %[esp_save]\n"\
        "mov " RBP ", %[ebp_save]\n"\
        "mov %[esp_val], " RSP "\n"\
        "mov %[ebp_val], " RBP "\n"\
        "enter" size " $8, $" #level "\n"\
        "mov " RSP ", %[esp_val]\n"\
        "mov " RBP ", %[ebp_val]\n"\
        "mov %[esp_save], " RSP "\n"\
        "mov %[ebp_save], " RBP "\n"\
B
bellard 已提交
1993 1994 1995 1996 1997 1998 1999
        : [esp_save] "=r" (esp_save),\
        [ebp_save] "=r" (ebp_save),\
        [esp_val] "=r" (esp_val),\
        [ebp_val] "=r" (ebp_val)\
        :  "[esp_val]" (esp_val),\
        "[ebp_val]" (ebp_val));\
    printf("level=%d:\n", level);\
B
bellard 已提交
2000 2001
    printf("esp_val=" FMTLX "\n", esp_val - (long)stack_end);\
    printf("ebp_val=" FMTLX "\n", ebp_val - (long)stack_end);\
B
bellard 已提交
2002
    for(ptr = (stack_type *)esp_val; ptr < stack_end; ptr++)\
B
bellard 已提交
2003
        printf(FMTLX "\n", (long)ptr[0]);\
B
bellard 已提交
2004 2005 2006 2007
}

static void test_enter(void)
{
B
bellard 已提交
2008 2009 2010 2011 2012 2013
#if defined(__x86_64__)
    TEST_ENTER("q", uint64_t, 0);
    TEST_ENTER("q", uint64_t, 1);
    TEST_ENTER("q", uint64_t, 2);
    TEST_ENTER("q", uint64_t, 31);
#else
B
bellard 已提交
2014 2015 2016 2017
    TEST_ENTER("l", uint32_t, 0);
    TEST_ENTER("l", uint32_t, 1);
    TEST_ENTER("l", uint32_t, 2);
    TEST_ENTER("l", uint32_t, 31);
B
bellard 已提交
2018
#endif
B
bellard 已提交
2019 2020 2021 2022 2023 2024 2025

    TEST_ENTER("w", uint16_t, 0);
    TEST_ENTER("w", uint16_t, 1);
    TEST_ENTER("w", uint16_t, 2);
    TEST_ENTER("w", uint16_t, 31);
}

B
bellard 已提交
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
#ifdef TEST_SSE

typedef int __m64 __attribute__ ((__mode__ (__V2SI__)));
typedef int __m128 __attribute__ ((__mode__(__V4SF__)));

typedef union {
    double d[2];
    float s[4];
    uint32_t l[4];
    uint64_t q[2];
    __m128 dq;
} XMMReg;

static uint64_t __attribute__((aligned(16))) test_values[4][2] = {
    { 0x456723c698694873, 0xdc515cff944a58ec },
    { 0x1f297ccd58bad7ab, 0x41f21efba9e3e146 },
    { 0x007c62c2085427f8, 0x231be9e8cde7438d },
    { 0x0f76255a085427f8, 0xc233e9e8c4c9439a },
};

#define SSE_OP(op)\
{\
    asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
B
bellard 已提交
2049
    printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
B
bellard 已提交
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
           #op,\
           a.q[1], a.q[0],\
           b.q[1], b.q[0],\
           r.q[1], r.q[0]);\
}

#define SSE_OP2(op)\
{\
    int i;\
    for(i=0;i<2;i++) {\
    a.q[0] = test_values[2*i][0];\
    a.q[1] = test_values[2*i][1];\
    b.q[0] = test_values[2*i+1][0];\
    b.q[1] = test_values[2*i+1][1];\
    SSE_OP(op);\
    }\
}

#define MMX_OP2(op)\
{\
    int i;\
    for(i=0;i<2;i++) {\
    a.q[0] = test_values[2*i][0];\
    b.q[0] = test_values[2*i+1][0];\
    asm volatile (#op " %2, %0" : "=y" (r.q[0]) : "0" (a.q[0]), "y" (b.q[0]));\
B
bellard 已提交
2075
    printf("%-9s: a=" FMT64X " b=" FMT64X " r=" FMT64X "\n",\
B
bellard 已提交
2076 2077 2078 2079 2080 2081 2082 2083
           #op,\
           a.q[0],\
           b.q[0],\
           r.q[0]);\
    }\
    SSE_OP2(op);\
}

2084 2085 2086 2087 2088 2089 2090
#define SHUF_OP(op, ib)\
{\
    a.q[0] = test_values[0][0];\
    a.q[1] = test_values[0][1];\
    b.q[0] = test_values[1][0];\
    b.q[1] = test_values[1][1];\
    asm volatile (#op " $" #ib ", %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
B
bellard 已提交
2091
    printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
2092 2093 2094 2095 2096 2097 2098
           #op,\
           a.q[1], a.q[0],\
           b.q[1], b.q[0],\
           ib,\
           r.q[1], r.q[0]);\
}

B
bellard 已提交
2099 2100 2101 2102 2103 2104 2105
#define PSHUF_OP(op, ib)\
{\
    int i;\
    for(i=0;i<2;i++) {\
    a.q[0] = test_values[2*i][0];\
    a.q[1] = test_values[2*i][1];\
    asm volatile (#op " $" #ib ", %1, %0" : "=x" (r.dq) : "x" (a.dq));\
B
bellard 已提交
2106
    printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
B
bellard 已提交
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
           #op,\
           a.q[1], a.q[0],\
           ib,\
           r.q[1], r.q[0]);\
    }\
}

#define SHIFT_IM(op, ib)\
{\
    int i;\
    for(i=0;i<2;i++) {\
    a.q[0] = test_values[2*i][0];\
    a.q[1] = test_values[2*i][1];\
    asm volatile (#op " $" #ib ", %0" : "=x" (r.dq) : "0" (a.dq));\
B
bellard 已提交
2121
    printf("%-9s: a=" FMT64X "" FMT64X " ib=%02x r=" FMT64X "" FMT64X "\n",\
B
bellard 已提交
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
           #op,\
           a.q[1], a.q[0],\
           ib,\
           r.q[1], r.q[0]);\
    }\
}

#define SHIFT_OP(op, ib)\
{\
    int i;\
    SHIFT_IM(op, ib);\
    for(i=0;i<2;i++) {\
    a.q[0] = test_values[2*i][0];\
    a.q[1] = test_values[2*i][1];\
    b.q[0] = ib;\
    b.q[1] = 0;\
    asm volatile (#op " %2, %0" : "=x" (r.dq) : "0" (a.dq), "x" (b.dq));\
B
bellard 已提交
2139
    printf("%-9s: a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
B
bellard 已提交
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
           #op,\
           a.q[1], a.q[0],\
           b.q[1], b.q[0],\
           r.q[1], r.q[0]);\
    }\
}

#define MOVMSK(op)\
{\
    int i, reg;\
    for(i=0;i<2;i++) {\
    a.q[0] = test_values[2*i][0];\
    a.q[1] = test_values[2*i][1];\
    asm volatile (#op " %1, %0" : "=r" (reg) : "x" (a.dq));\
B
bellard 已提交
2154
    printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
B
bellard 已提交
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195
           #op,\
           a.q[1], a.q[0],\
           reg);\
    }\
}

#define SSE_OPS(a) \
SSE_OP(a ## ps);\
SSE_OP(a ## ss);

#define SSE_OPD(a) \
SSE_OP(a ## pd);\
SSE_OP(a ## sd);

#define SSE_COMI(op, field)\
{\
    unsigned int eflags;\
    XMMReg a, b;\
    a.field[0] = a1;\
    b.field[0] = b1;\
    asm volatile (#op " %2, %1\n"\
        "pushf\n"\
        "pop %0\n"\
        : "=m" (eflags)\
        : "x" (a.dq), "x" (b.dq));\
    printf("%-9s: a=%f b=%f cc=%04x\n",\
           #op, a1, b1,\
           eflags & (CC_C | CC_P | CC_Z | CC_S | CC_O | CC_A));\
}

void test_sse_comi(double a1, double b1)
{
    SSE_COMI(ucomiss, s);
    SSE_COMI(ucomisd, d);
    SSE_COMI(comiss, s);
    SSE_COMI(comisd, d);
}

#define CVT_OP_XMM(op)\
{\
    asm volatile (#op " %1, %0" : "=x" (r.dq) : "x" (a.dq));\
B
bellard 已提交
2196
    printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "" FMT64X "\n",\
B
bellard 已提交
2197 2198 2199 2200 2201 2202 2203 2204
           #op,\
           a.q[1], a.q[0],\
           r.q[1], r.q[0]);\
}

#define CVT_OP_XMM2MMX(op)\
{\
    asm volatile (#op " %1, %0" : "=y" (r.q[0]) : "x" (a.dq));\
B
bellard 已提交
2205
    printf("%-9s: a=" FMT64X "" FMT64X " r=" FMT64X "\n",\
B
bellard 已提交
2206 2207 2208 2209 2210 2211 2212 2213
           #op,\
           a.q[1], a.q[0],\
           r.q[0]);\
}

#define CVT_OP_MMX2XMM(op)\
{\
    asm volatile (#op " %1, %0" : "=x" (r.dq) : "y" (a.q[0]));\
B
bellard 已提交
2214
    printf("%-9s: a=" FMT64X " r=" FMT64X "" FMT64X "\n",\
B
bellard 已提交
2215 2216 2217 2218 2219 2220 2221 2222
           #op,\
           a.q[0],\
           r.q[1], r.q[0]);\
}

#define CVT_OP_REG2XMM(op)\
{\
    asm volatile (#op " %1, %0" : "=x" (r.dq) : "r" (a.l[0]));\
B
bellard 已提交
2223
    printf("%-9s: a=%08x r=" FMT64X "" FMT64X "\n",\
B
bellard 已提交
2224 2225 2226 2227 2228 2229 2230 2231
           #op,\
           a.l[0],\
           r.q[1], r.q[0]);\
}

#define CVT_OP_XMM2REG(op)\
{\
    asm volatile (#op " %1, %0" : "=r" (r.l[0]) : "x" (a.dq));\
B
bellard 已提交
2232
    printf("%-9s: a=" FMT64X "" FMT64X " r=%08x\n",\
B
bellard 已提交
2233 2234 2235 2236 2237
           #op,\
           a.q[1], a.q[0],\
           r.l[0]);\
}

B
bellard 已提交
2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
struct fpxstate {
    uint16_t fpuc;
    uint16_t fpus;
    uint16_t fptag;
    uint16_t fop;
    uint32_t fpuip;
    uint16_t cs_sel;
    uint16_t dummy0;
    uint32_t fpudp;
    uint16_t ds_sel;
    uint16_t dummy1;
    uint32_t mxcsr;
    uint32_t mxcsr_mask;
    uint8_t fpregs1[8 * 16];
    uint8_t xmm_regs[8 * 16];
    uint8_t dummy2[224];
};

static struct fpxstate fpx_state __attribute__((aligned(16)));
static struct fpxstate fpx_state2 __attribute__((aligned(16)));

void test_fxsave(void)
{
    struct fpxstate *fp = &fpx_state;
    struct fpxstate *fp2 = &fpx_state2;
B
bellard 已提交
2263
    int i, nb_xmm;
B
bellard 已提交
2264 2265 2266 2267 2268 2269 2270 2271
    XMMReg a, b;
    a.q[0] = test_values[0][0];
    a.q[1] = test_values[0][1];
    b.q[0] = test_values[1][0];
    b.q[1] = test_values[1][1];

    asm("movdqa %2, %%xmm0\n"
        "movdqa %3, %%xmm7\n"
B
bellard 已提交
2272 2273 2274
#if defined(__x86_64__)
        "movdqa %2, %%xmm15\n"
#endif
B
bellard 已提交
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
        " fld1\n"
        " fldpi\n"
        " fldln2\n"
        " fxsave %0\n"
        " fxrstor %0\n"
        " fxsave %1\n"
        " fninit\n"
        : "=m" (*(uint32_t *)fp2), "=m" (*(uint32_t *)fp) 
        : "m" (a), "m" (b));
    printf("fpuc=%04x\n", fp->fpuc);
    printf("fpus=%04x\n", fp->fpus);
    printf("fptag=%04x\n", fp->fptag);
    for(i = 0; i < 3; i++) {
B
bellard 已提交
2288
        printf("ST%d: " FMT64X " %04x\n",
B
bellard 已提交
2289 2290 2291 2292 2293
               i, 
               *(uint64_t *)&fp->fpregs1[i * 16],
               *(uint16_t *)&fp->fpregs1[i * 16 + 8]);
    }
    printf("mxcsr=%08x\n", fp->mxcsr & 0x1f80);
B
bellard 已提交
2294 2295 2296 2297 2298 2299 2300
#if defined(__x86_64__)
    nb_xmm = 16;
#else
    nb_xmm = 8;
#endif
    for(i = 0; i < nb_xmm; i++) {
        printf("xmm%d: " FMT64X "" FMT64X "\n",
B
bellard 已提交
2301 2302 2303 2304 2305 2306
               i, 
               *(uint64_t *)&fp->xmm_regs[i * 16],
               *(uint64_t *)&fp->xmm_regs[i * 16 + 8]);
    }
}

B
bellard 已提交
2307 2308 2309
void test_sse(void)
{
    XMMReg r, a, b;
B
bellard 已提交
2310
    int i;
B
bellard 已提交
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

    MMX_OP2(punpcklbw);
    MMX_OP2(punpcklwd);
    MMX_OP2(punpckldq);
    MMX_OP2(packsswb);
    MMX_OP2(pcmpgtb);
    MMX_OP2(pcmpgtw);
    MMX_OP2(pcmpgtd);
    MMX_OP2(packuswb);
    MMX_OP2(punpckhbw);
    MMX_OP2(punpckhwd);
    MMX_OP2(punpckhdq);
    MMX_OP2(packssdw);
    MMX_OP2(pcmpeqb);
    MMX_OP2(pcmpeqw);
    MMX_OP2(pcmpeqd);

    MMX_OP2(paddq);
    MMX_OP2(pmullw);
    MMX_OP2(psubusb);
    MMX_OP2(psubusw);
    MMX_OP2(pminub);
    MMX_OP2(pand);
    MMX_OP2(paddusb);
    MMX_OP2(paddusw);
    MMX_OP2(pmaxub);
    MMX_OP2(pandn);

    MMX_OP2(pmulhuw);
    MMX_OP2(pmulhw);
    
    MMX_OP2(psubsb);
    MMX_OP2(psubsw);
    MMX_OP2(pminsw);
    MMX_OP2(por);
    MMX_OP2(paddsb);
    MMX_OP2(paddsw);
    MMX_OP2(pmaxsw);
    MMX_OP2(pxor);
    MMX_OP2(pmuludq);
    MMX_OP2(pmaddwd);
    MMX_OP2(psadbw);
    MMX_OP2(psubb);
    MMX_OP2(psubw);
    MMX_OP2(psubd);
    MMX_OP2(psubq);
    MMX_OP2(paddb);
    MMX_OP2(paddw);
    MMX_OP2(paddd);

    MMX_OP2(pavgb);
    MMX_OP2(pavgw);

    asm volatile ("pinsrw $1, %1, %0" : "=y" (r.q[0]) : "r" (0x12345678));
B
bellard 已提交
2365
    printf("%-9s: r=" FMT64X "\n", "pinsrw", r.q[0]);
B
bellard 已提交
2366 2367

    asm volatile ("pinsrw $5, %1, %0" : "=x" (r.dq) : "r" (0x12345678));
B
bellard 已提交
2368
    printf("%-9s: r=" FMT64X "" FMT64X "\n", "pinsrw", r.q[1], r.q[0]);
B
bellard 已提交
2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383

    a.q[0] = test_values[0][0];
    a.q[1] = test_values[0][1];
    asm volatile ("pextrw $1, %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
    printf("%-9s: r=%08x\n", "pextrw", r.l[0]);

    asm volatile ("pextrw $5, %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
    printf("%-9s: r=%08x\n", "pextrw", r.l[0]);

    asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "y" (a.q[0]));
    printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);
    
    asm volatile ("pmovmskb %1, %0" : "=r" (r.l[0]) : "x" (a.dq));
    printf("%-9s: r=%08x\n", "pmovmskb", r.l[0]);

2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394
    {
        r.q[0] = -1;
        r.q[1] = -1;

        a.q[0] = test_values[0][0];
        a.q[1] = test_values[0][1];
        b.q[0] = test_values[1][0];
        b.q[1] = test_values[1][1];
        asm volatile("maskmovq %1, %0" : 
                     : "y" (a.q[0]), "y" (b.q[0]), "D" (&r)
                     : "memory"); 
B
bellard 已提交
2395
        printf("%-9s: r=" FMT64X " a=" FMT64X " b=" FMT64X "\n", 
2396 2397 2398 2399 2400 2401 2402
               "maskmov", 
               r.q[0], 
               a.q[0], 
               b.q[0]);
        asm volatile("maskmovdqu %1, %0" : 
                     : "x" (a.dq), "x" (b.dq), "D" (&r)
                     : "memory"); 
B
bellard 已提交
2403
        printf("%-9s: r=" FMT64X "" FMT64X " a=" FMT64X "" FMT64X " b=" FMT64X "" FMT64X "\n", 
2404 2405 2406 2407 2408 2409
               "maskmov", 
               r.q[1], r.q[0], 
               a.q[1], a.q[0], 
               b.q[1], b.q[0]);
    }

B
bellard 已提交
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
    asm volatile ("emms");

    SSE_OP2(punpcklqdq);
    SSE_OP2(punpckhqdq);
    SSE_OP2(andps);
    SSE_OP2(andpd);
    SSE_OP2(andnps);
    SSE_OP2(andnpd);
    SSE_OP2(orps);
    SSE_OP2(orpd);
    SSE_OP2(xorps);
    SSE_OP2(xorpd);

    SSE_OP2(unpcklps);
    SSE_OP2(unpcklpd);
    SSE_OP2(unpckhps);
    SSE_OP2(unpckhpd);

2428 2429
    SHUF_OP(shufps, 0x78);
    SHUF_OP(shufpd, 0x02);
B
bellard 已提交
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473

    PSHUF_OP(pshufd, 0x78);
    PSHUF_OP(pshuflw, 0x78);
    PSHUF_OP(pshufhw, 0x78);

    SHIFT_OP(psrlw, 7);
    SHIFT_OP(psrlw, 16);
    SHIFT_OP(psraw, 7);
    SHIFT_OP(psraw, 16);
    SHIFT_OP(psllw, 7);
    SHIFT_OP(psllw, 16);

    SHIFT_OP(psrld, 7);
    SHIFT_OP(psrld, 32);
    SHIFT_OP(psrad, 7);
    SHIFT_OP(psrad, 32);
    SHIFT_OP(pslld, 7);
    SHIFT_OP(pslld, 32);

    SHIFT_OP(psrlq, 7);
    SHIFT_OP(psrlq, 32);
    SHIFT_OP(psllq, 7);
    SHIFT_OP(psllq, 32);

    SHIFT_IM(psrldq, 16);
    SHIFT_IM(psrldq, 7);
    SHIFT_IM(pslldq, 16);
    SHIFT_IM(pslldq, 7);

    MOVMSK(movmskps);
    MOVMSK(movmskpd);

    /* FPU specific ops */

    {
        uint32_t mxcsr;
        asm volatile("stmxcsr %0" : "=m" (mxcsr));
        printf("mxcsr=%08x\n", mxcsr & 0x1f80);
        asm volatile("ldmxcsr %0" : : "m" (mxcsr));
    }

    test_sse_comi(2, -1);
    test_sse_comi(2, 2);
    test_sse_comi(2, 3);
B
bellard 已提交
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531
    test_sse_comi(2, q_nan.d);
    test_sse_comi(q_nan.d, -1);

    for(i = 0; i < 2; i++) {
        a.s[0] = 2.7;
        a.s[1] = 3.4;
        a.s[2] = 4;
        a.s[3] = -6.3;
        b.s[0] = 45.7;
        b.s[1] = 353.4;
        b.s[2] = 4;
        b.s[3] = 56.3;
        if (i == 1) {
            a.s[0] = q_nan.d;
            b.s[3] = q_nan.d;
        }

        SSE_OPS(add);
        SSE_OPS(mul);
        SSE_OPS(sub);
        SSE_OPS(min);
        SSE_OPS(div);
        SSE_OPS(max);
        SSE_OPS(sqrt);
        SSE_OPS(cmpeq);
        SSE_OPS(cmplt);
        SSE_OPS(cmple);
        SSE_OPS(cmpunord);
        SSE_OPS(cmpneq);
        SSE_OPS(cmpnlt);
        SSE_OPS(cmpnle);
        SSE_OPS(cmpord);
        
        
        a.d[0] = 2.7;
        a.d[1] = -3.4;
        b.d[0] = 45.7;
        b.d[1] = -53.4;
        if (i == 1) {
            a.d[0] = q_nan.d;
            b.d[1] = q_nan.d;
        }
        SSE_OPD(add);
        SSE_OPD(mul);
        SSE_OPD(sub);
        SSE_OPD(min);
        SSE_OPD(div);
        SSE_OPD(max);
        SSE_OPD(sqrt);
        SSE_OPD(cmpeq);
        SSE_OPD(cmplt);
        SSE_OPD(cmple);
        SSE_OPD(cmpunord);
        SSE_OPD(cmpneq);
        SSE_OPD(cmpnlt);
        SSE_OPD(cmpnle);
        SSE_OPD(cmpord);
    }
B
bellard 已提交
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573

    /* float to float/int */
    a.s[0] = 2.7;
    a.s[1] = 3.4;
    a.s[2] = 4;
    a.s[3] = -6.3;
    CVT_OP_XMM(cvtps2pd);
    CVT_OP_XMM(cvtss2sd);
    CVT_OP_XMM2MMX(cvtps2pi);
    CVT_OP_XMM2MMX(cvttps2pi);
    CVT_OP_XMM2REG(cvtss2si);
    CVT_OP_XMM2REG(cvttss2si);
    CVT_OP_XMM(cvtps2dq);
    CVT_OP_XMM(cvttps2dq);

    a.d[0] = 2.6;
    a.d[1] = -3.4;
    CVT_OP_XMM(cvtpd2ps);
    CVT_OP_XMM(cvtsd2ss);
    CVT_OP_XMM2MMX(cvtpd2pi);
    CVT_OP_XMM2MMX(cvttpd2pi);
    CVT_OP_XMM2REG(cvtsd2si);
    CVT_OP_XMM2REG(cvttsd2si);
    CVT_OP_XMM(cvtpd2dq);
    CVT_OP_XMM(cvttpd2dq);

    /* int to float */
    a.l[0] = -6;
    a.l[1] = 2;
    a.l[2] = 100;
    a.l[3] = -60000;
    CVT_OP_MMX2XMM(cvtpi2ps);
    CVT_OP_MMX2XMM(cvtpi2pd);
    CVT_OP_REG2XMM(cvtsi2ss);
    CVT_OP_REG2XMM(cvtsi2sd);
    CVT_OP_XMM(cvtdq2ps);
    CVT_OP_XMM(cvtdq2pd);

    /* XXX: test PNI insns */
#if 0
    SSE_OP2(movshdup);
#endif
B
bellard 已提交
2574
    asm volatile ("emms");
B
bellard 已提交
2575 2576 2577 2578
}

#endif

B
bellard 已提交
2579 2580 2581
extern void *__start_initcall;
extern void *__stop_initcall;

B
bellard 已提交
2582 2583 2584 2585 2586

int main(int argc, char **argv)
{
    void **ptr;
    void (*func)(void);
B
bellard 已提交
2587

B
bellard 已提交
2588 2589
    ptr = &__start_initcall;
    while (ptr != &__stop_initcall) {
B
bellard 已提交
2590 2591 2592
        func = *ptr++;
        func();
    }
B
bellard 已提交
2593
    test_bsx();
2594
    test_mul();
B
bellard 已提交
2595
    test_jcc();
B
bellard 已提交
2596
    test_floats();
B
bellard 已提交
2597
#if !defined(__x86_64__)
B
bellard 已提交
2598
    test_bcd();
B
bellard 已提交
2599
#endif
B
bellard 已提交
2600
    test_xchg();
B
bellard 已提交
2601 2602
    test_string();
    test_misc();
B
bellard 已提交
2603
    test_lea();
B
bellard 已提交
2604
#ifdef TEST_SEGS
B
bellard 已提交
2605
    test_segs();
B
bellard 已提交
2606
    test_code16();
B
bellard 已提交
2607
#endif
2608
#ifdef TEST_VM86
2609
    test_vm86();
2610
#endif
2611
    test_exceptions();
B
bellard 已提交
2612
#if !defined(__x86_64__)
2613
    test_self_modifying_code();
2614
    test_single_step();
B
bellard 已提交
2615
#endif
B
bellard 已提交
2616
    test_enter();
B
bellard 已提交
2617 2618
#ifdef TEST_SSE
    test_sse();
B
bellard 已提交
2619
    test_fxsave();
B
bellard 已提交
2620
#endif
B
bellard 已提交
2621 2622
    return 0;
}