translate.c 218.4 KB
Newer Older
B
bellard 已提交
1
/*
2
 *  PowerPC emulation for qemu: main translation routines.
3
 *
4
 *  Copyright (c) 2003-2007 Jocelyn Mayer
B
bellard 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
B
bellard 已提交
20 21 22 23 24 25
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

B
bellard 已提交
26
#include "cpu.h"
B
bellard 已提交
27
#include "exec-all.h"
B
bellard 已提交
28 29
#include "disas.h"

30
/* Include definitions for instructions classes and implementations flags */
B
bellard 已提交
31
//#define DO_SINGLE_STEP
32
//#define PPC_DEBUG_DISAS
33
//#define DEBUG_MEMORY_ACCESSES
34
//#define DO_PPC_STATISTICS
35
//#define OPTIMIZE_FPRF_UPDATE
B
bellard 已提交
36

37 38
/*****************************************************************************/
/* Code translation helpers                                                  */
39
#if defined(USE_DIRECT_JUMP)
B
bellard 已提交
40 41 42 43 44
#define TBPARAM(x)
#else
#define TBPARAM(x) (long)(x)
#endif

B
bellard 已提交
45 46 47 48 49 50 51 52 53
enum {
#define DEF(s, n, copy_size) INDEX_op_ ## s,
#include "opc.h"
#undef DEF
    NB_OPS,
};

static uint16_t *gen_opc_ptr;
static uint32_t *gen_opparam_ptr;
54 55 56 57
#if defined(OPTIMIZE_FPRF_UPDATE)
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
static uint16_t **gen_fprf_ptr;
#endif
B
bellard 已提交
58 59

#include "gen-op.h"
60

61
static always_inline void gen_set_T0 (target_ulong val)
62 63 64 65 66 67 68 69 70
{
#if defined(TARGET_PPC64)
    if (val >> 32)
        gen_op_set_T0_64(val >> 32, val);
    else
#endif
        gen_op_set_T0(val);
}

71
static always_inline void gen_set_T1 (target_ulong val)
72 73 74 75 76 77 78 79 80 81
{
#if defined(TARGET_PPC64)
    if (val >> 32)
        gen_op_set_T1_64(val >> 32, val);
    else
#endif
        gen_op_set_T1(val);
}

#define GEN8(func, NAME)                                                      \
82 83 84 85
static GenOpFunc *NAME ## _table [8] = {                                      \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
};                                                                            \
86
static always_inline void func (int n)                                        \
87 88 89 90 91 92 93 94 95 96 97
{                                                                             \
    NAME ## _table[n]();                                                      \
}

#define GEN16(func, NAME)                                                     \
static GenOpFunc *NAME ## _table [16] = {                                     \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
};                                                                            \
98
static always_inline void func (int n)                                        \
99 100
{                                                                             \
    NAME ## _table[n]();                                                      \
101 102
}

103
#define GEN32(func, NAME)                                                     \
104 105 106 107 108 109 110 111 112 113
static GenOpFunc *NAME ## _table [32] = {                                     \
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,                               \
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,                               \
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,                               \
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,                               \
};                                                                            \
114
static always_inline void func (int n)                                        \
115 116 117 118 119 120 121 122
{                                                                             \
    NAME ## _table[n]();                                                      \
}

/* Condition register moves */
GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);
GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);
GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);
123
#if 0 // Unused
124
GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);
125
#endif
126

127 128 129 130 131 132 133
/* General purpose registers moves */
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);

GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
134
#if 0 // unused
135
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
136
#endif
137

B
bellard 已提交
138 139 140 141 142 143
/* floating point registers moves */
GEN32(gen_op_load_fpr_FT0, gen_op_load_fpr_FT0_fpr);
GEN32(gen_op_load_fpr_FT1, gen_op_load_fpr_FT1_fpr);
GEN32(gen_op_load_fpr_FT2, gen_op_load_fpr_FT2_fpr);
GEN32(gen_op_store_FT0_fpr, gen_op_store_FT0_fpr_fpr);
GEN32(gen_op_store_FT1_fpr, gen_op_store_FT1_fpr_fpr);
144
#if 0 // unused
B
bellard 已提交
145
GEN32(gen_op_store_FT2_fpr, gen_op_store_FT2_fpr_fpr);
146
#endif
B
bellard 已提交
147 148 149 150

/* internal defines */
typedef struct DisasContext {
    struct TranslationBlock *tb;
B
bellard 已提交
151
    target_ulong nip;
B
bellard 已提交
152
    uint32_t opcode;
153
    uint32_t exception;
B
bellard 已提交
154 155 156
    /* Routine used to access memory */
    int mem_idx;
    /* Translation flags */
157
#if !defined(CONFIG_USER_ONLY)
B
bellard 已提交
158
    int supervisor;
159 160 161
#endif
#if defined(TARGET_PPC64)
    int sf_mode;
162
#endif
B
bellard 已提交
163
    int fpu_enabled;
164
    int altivec_enabled;
165
    int spe_enabled;
166
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
167
    int singlestep_enabled;
168
    int dcache_line_size;
B
bellard 已提交
169 170
} DisasContext;

171
struct opc_handler_t {
B
bellard 已提交
172 173
    /* invalid bits */
    uint32_t inval;
174
    /* instruction type */
175
    uint64_t type;
B
bellard 已提交
176 177
    /* handler */
    void (*handler)(DisasContext *ctx);
178
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
179
    const unsigned char *oname;
180 181
#endif
#if defined(DO_PPC_STATISTICS)
182 183
    uint64_t count;
#endif
184
};
B
bellard 已提交
185

186
static always_inline void gen_set_Rc0 (DisasContext *ctx)
187
{
188 189 190 191 192 193
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_cmpi_64(0);
    else
#endif
        gen_op_cmpi(0);
194 195 196
    gen_op_set_Rc0();
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
static always_inline void gen_reset_fpstatus (void)
{
#ifdef CONFIG_SOFTFLOAT
    gen_op_reset_fpstatus();
#endif
}

static always_inline void gen_compute_fprf (int set_fprf, int set_rc)
{
    if (set_fprf != 0) {
        /* This case might be optimized later */
#if defined(OPTIMIZE_FPRF_UPDATE)
        *gen_fprf_ptr++ = gen_opc_ptr;
#endif
        gen_op_compute_fprf(1);
        if (unlikely(set_rc))
            gen_op_store_T0_crf(1);
        gen_op_float_check_status();
    } else if (unlikely(set_rc)) {
        /* We always need to compute fpcc */
        gen_op_compute_fprf(0);
        gen_op_store_T0_crf(1);
        if (set_fprf)
            gen_op_float_check_status();
    }
}

static always_inline void gen_optimize_fprf (void)
{
#if defined(OPTIMIZE_FPRF_UPDATE)
    uint16_t **ptr;

    for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
        *ptr = INDEX_op_nop1;
    gen_fprf_ptr = gen_fprf_buf;
#endif
}

235
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
236 237 238 239 240 241 242 243 244
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_update_nip_64(nip >> 32, nip);
    else
#endif
        gen_op_update_nip(nip);
}

245
#define GEN_EXCP(ctx, excp, error)                                            \
B
bellard 已提交
246
do {                                                                          \
247
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
248
        gen_update_nip(ctx, (ctx)->nip);                                      \
249 250 251
    }                                                                         \
    gen_op_raise_exception_err((excp), (error));                              \
    ctx->exception = (excp);                                                  \
B
bellard 已提交
252 253
} while (0)

254 255 256
#define GEN_EXCP_INVAL(ctx)                                                   \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
257

258 259 260
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
261

262 263 264 265 266 267 268 269 270
#define GEN_EXCP_PRIVREG(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)

#define GEN_EXCP_NO_FP(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)

#define GEN_EXCP_NO_AP(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
271

272 273 274
#define GEN_EXCP_NO_VR(ctx)                                                   \
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)

275
/* Stop translation */
276
static always_inline void GEN_STOP (DisasContext *ctx)
277
{
278
    gen_update_nip(ctx, ctx->nip);
279
    ctx->exception = POWERPC_EXCP_STOP;
280 281
}

282
/* No need to update nip here, as execution flow will change */
283
static always_inline void GEN_SYNC (DisasContext *ctx)
284
{
285
    ctx->exception = POWERPC_EXCP_SYNC;
286 287
}

B
bellard 已提交
288 289 290 291 292
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
static void gen_##name (DisasContext *ctx)

293 294 295 296 297
#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
static void gen_##name (DisasContext *ctx)

B
bellard 已提交
298 299
typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
300 301 302 303 304
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
305
    opc_handler_t handler;
306
    const unsigned char *oname;
B
bellard 已提交
307 308
} opcode_t;

309
/*****************************************************************************/
B
bellard 已提交
310 311
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
312
static always_inline uint32_t name (uint32_t opcode)                          \
B
bellard 已提交
313 314 315 316 317
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
318
static always_inline int32_t name (uint32_t opcode)                           \
B
bellard 已提交
319
{                                                                             \
320
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
}

/* Opcode part 1 */
EXTRACT_HELPER(opc1, 26, 6);
/* Opcode part 2 */
EXTRACT_HELPER(opc2, 1, 5);
/* Opcode part 3 */
EXTRACT_HELPER(opc3, 6, 5);
/* Update Cr0 flags */
EXTRACT_HELPER(Rc, 0, 1);
/* Destination */
EXTRACT_HELPER(rD, 21, 5);
/* Source */
EXTRACT_HELPER(rS, 21, 5);
/* First operand */
EXTRACT_HELPER(rA, 16, 5);
/* Second operand */
EXTRACT_HELPER(rB, 11, 5);
/* Third operand */
EXTRACT_HELPER(rC, 6, 5);
/***                               Get CRn                                 ***/
EXTRACT_HELPER(crfD, 23, 3);
EXTRACT_HELPER(crfS, 18, 3);
EXTRACT_HELPER(crbD, 21, 5);
EXTRACT_HELPER(crbA, 16, 5);
EXTRACT_HELPER(crbB, 11, 5);
/* SPR / TBL */
348
EXTRACT_HELPER(_SPR, 11, 10);
349
static always_inline uint32_t SPR (uint32_t opcode)
350 351 352 353 354
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368
/***                              Get constants                            ***/
EXTRACT_HELPER(IMM, 12, 8);
/* 16 bits signed immediate value */
EXTRACT_SHELPER(SIMM, 0, 16);
/* 16 bits unsigned immediate value */
EXTRACT_HELPER(UIMM, 0, 16);
/* Bit count */
EXTRACT_HELPER(NB, 11, 5);
/* Shift count */
EXTRACT_HELPER(SH, 11, 5);
/* Mask start */
EXTRACT_HELPER(MB, 6, 5);
/* Mask end */
EXTRACT_HELPER(ME, 1, 5);
B
bellard 已提交
369 370
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
371 372 373 374

EXTRACT_HELPER(CRM, 12, 8);
EXTRACT_HELPER(FM, 17, 8);
EXTRACT_HELPER(SR, 16, 4);
B
bellard 已提交
375 376
EXTRACT_HELPER(FPIMM, 20, 4);

B
bellard 已提交
377 378 379 380
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
381
static always_inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
382 383 384 385
{
    return (opcode >> 0) & 0x03FFFFFC;
}

386
static always_inline uint32_t BD (uint32_t opcode)
B
bellard 已提交
387 388 389 390 391 392 393 394 395 396 397 398
{
    return (opcode >> 0) & 0xFFFC;
}

EXTRACT_HELPER(BO, 21, 5);
EXTRACT_HELPER(BI, 16, 5);
/* Absolute/relative address */
EXTRACT_HELPER(AA, 1, 1);
/* Link */
EXTRACT_HELPER(LK, 0, 1);

/* Create a mask between <start> and <end> bits */
399
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
400
{
401
    target_ulong ret;
B
bellard 已提交
402

403 404
#if defined(TARGET_PPC64)
    if (likely(start == 0)) {
405
        ret = UINT64_MAX << (63 - end);
406
    } else if (likely(end == 63)) {
407
        ret = UINT64_MAX >> start;
408 409 410
    }
#else
    if (likely(start == 0)) {
411
        ret = UINT32_MAX << (31  - end);
412
    } else if (likely(end == 31)) {
413
        ret = UINT32_MAX >> start;
414 415 416 417 418 419 420 421
    }
#endif
    else {
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
            (((target_ulong)(-1ULL) >> (end)) >> 1);
        if (unlikely(start > end))
            return ~ret;
    }
B
bellard 已提交
422 423 424 425

    return ret;
}

426 427 428
/*****************************************************************************/
/* PowerPC Instructions types definitions                                    */
enum {
429
    PPC_NONE           = 0x0000000000000000ULL,
430
    /* PowerPC base instructions set                                         */
431 432
    PPC_INSNS_BASE     = 0x0000000000000001ULL,
    /*   integer operations instructions                                     */
433
#define PPC_INTEGER PPC_INSNS_BASE
434
    /*   flow control instructions                                           */
435
#define PPC_FLOW    PPC_INSNS_BASE
436
    /*   virtual memory instructions                                         */
437
#define PPC_MEM     PPC_INSNS_BASE
438
    /*   ld/st with reservation instructions                                 */
439
#define PPC_RES     PPC_INSNS_BASE
440
    /*   spr/msr access instructions                                         */
441
#define PPC_MISC    PPC_INSNS_BASE
442 443
    /* Deprecated instruction sets                                           */
    /*   Original POWER instruction set                                      */
444
    PPC_POWER          = 0x0000000000000002ULL,
445
    /*   POWER2 instruction set extension                                    */
446
    PPC_POWER2         = 0x0000000000000004ULL,
447
    /*   Power RTC support                                                   */
448
    PPC_POWER_RTC      = 0x0000000000000008ULL,
449
    /*   Power-to-PowerPC bridge (601)                                       */
450
    PPC_POWER_BR       = 0x0000000000000010ULL,
451
    /* 64 bits PowerPC instruction set                                       */
452
    PPC_64B            = 0x0000000000000020ULL,
453
    /*   New 64 bits extensions (PowerPC 2.0x)                               */
454
    PPC_64BX           = 0x0000000000000040ULL,
455
    /*   64 bits hypervisor extensions                                       */
456
    PPC_64H            = 0x0000000000000080ULL,
457
    /*   New wait instruction (PowerPC 2.0x)                                 */
458
    PPC_WAIT           = 0x0000000000000100ULL,
459
    /*   Time base mftb instruction                                          */
460
    PPC_MFTB           = 0x0000000000000200ULL,
461 462 463

    /* Fixed-point unit extensions                                           */
    /*   PowerPC 602 specific                                                */
464
    PPC_602_SPEC       = 0x0000000000000400ULL,
465
    /*   PowerPC 2.03 specification extensions                               */
466
    PPC_203            = 0x0000000000000800ULL,
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

    /* Floating-point unit extensions                                        */
    /*   Optional floating point instructions                                */
    PPC_FLOAT          = 0x0000000000010000ULL,
    /* New floating-point extensions (PowerPC 2.0x)                          */
    PPC_FLOAT_EXT      = 0x0000000000020000ULL,
    PPC_FLOAT_FSQRT    = 0x0000000000040000ULL,
    PPC_FLOAT_FRES     = 0x0000000000080000ULL,
    PPC_FLOAT_FRSQRTE  = 0x0000000000100000ULL,
    PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
    PPC_FLOAT_FSEL     = 0x0000000000400000ULL,
    PPC_FLOAT_STFIWX   = 0x0000000000800000ULL,

    /* Vector/SIMD extensions                                                */
    /*   Altivec support                                                     */
    PPC_ALTIVEC        = 0x0000000001000000ULL,
    /*   e500 vector instructions                                            */
    PPC_E500_VECTOR    = 0x0000000002000000ULL,
    /*   PowerPC 2.03 SPE extension                                          */
    PPC_SPE            = 0x0000000004000000ULL,
    /*   PowerPC 2.03 SPE floating-point extension                           */
    PPC_SPEFPU         = 0x0000000008000000ULL,

490
    /* Optional memory control instructions                                  */
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    PPC_MEM_TLBIA      = 0x0000000010000000ULL,
    PPC_MEM_TLBIE      = 0x0000000020000000ULL,
    PPC_MEM_TLBSYNC    = 0x0000000040000000ULL,
    /*   sync instruction                                                    */
    PPC_MEM_SYNC       = 0x0000000080000000ULL,
    /*   eieio instruction                                                   */
    PPC_MEM_EIEIO      = 0x0000000100000000ULL,

    /* Cache control instructions                                            */
    PPC_CACHE          = 0x0000001000000000ULL,
    /*   icbi instruction                                                    */
    PPC_CACHE_ICBI     = 0x0000002000000000ULL,
    /*   dcbz instruction with fixed cache line size                         */
    PPC_CACHE_DCBZ     = 0x0000004000000000ULL,
    /*   dcbz instruction with tunable cache line size                       */
    PPC_CACHE_DCBZT    = 0x0000008000000000ULL,
    /*   dcba instruction                                                    */
    PPC_CACHE_DCBA     = 0x0000010000000000ULL,

    /* MMU related extensions                                                */
    /*   external control instructions                                       */
    PPC_EXTERN         = 0x0000100000000000ULL,
    /*   segment register access instructions                                */
    PPC_SEGMENT        = 0x0000200000000000ULL,
    /*   PowerPC 6xx TLB management instructions                             */
    PPC_6xx_TLB        = 0x0000400000000000ULL,
    /* PowerPC 74xx TLB management instructions                              */
    PPC_74xx_TLB       = 0x0000800000000000ULL,
    /*   PowerPC 40x TLB management instructions                             */
    PPC_40x_TLB        = 0x0001000000000000ULL,
    /*   segment register access instructions for PowerPC 64 "bridge"        */
    PPC_SEGMENT_64B    = 0x0002000000000000ULL,
    /*   SLB management                                                      */
    PPC_SLBI           = 0x0004000000000000ULL,

526
    /* Embedded PowerPC dedicated instructions                               */
527
    PPC_EMB_COMMON     = 0x0010000000000000ULL,
528
    /* PowerPC 40x exception model                                           */
529
    PPC_40x_EXCP       = 0x0020000000000000ULL,
530
    /* PowerPC 405 Mac instructions                                          */
531
    PPC_405_MAC        = 0x0040000000000000ULL,
532
    /* PowerPC 440 specific instructions                                     */
533
    PPC_440_SPEC       = 0x0080000000000000ULL,
534
    /* BookE (embedded) PowerPC specification                                */
535 536 537
    PPC_BOOKE          = 0x0100000000000000ULL,
    /* More BookE (embedded) instructions...                                 */
    PPC_BOOKE_EXT      = 0x0200000000000000ULL,
538
    /* PowerPC 4xx dedicated instructions                                    */
539
    PPC_4xx_COMMON     = 0x0400000000000000ULL,
540
    /* PowerPC 40x ibct instructions                                         */
541
    PPC_40x_ICBT       = 0x0800000000000000ULL,
542
    /* rfmci is not implemented in all BookE PowerPC                         */
543
    PPC_RFMCI          = 0x1000000000000000ULL,
544
    /* user-mode DCR access, implemented in PowerPC 460                      */
545
    PPC_DCRUX          = 0x2000000000000000ULL,
546 547 548 549
};

/*****************************************************************************/
/* PowerPC instructions table                                                */
550 551 552 553 554
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
555
#if defined(__APPLE__)
556
#define OPCODES_SECTION                                                       \
557
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
558
#else
559
#define OPCODES_SECTION                                                       \
560
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
561 562
#endif

563
#if defined(DO_PPC_STATISTICS)
B
bellard 已提交
564
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
565
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
566 567 568
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
569
    .pad  = { 0, },                                                           \
B
bellard 已提交
570 571
    .handler = {                                                              \
        .inval   = invl,                                                      \
572
        .type = _typ,                                                         \
B
bellard 已提交
573
        .handler = &gen_##name,                                               \
574
        .oname = stringify(name),                                             \
B
bellard 已提交
575
    },                                                                        \
576
    .oname = stringify(name),                                                 \
B
bellard 已提交
577
}
578 579 580 581 582 583 584 585 586 587 588 589 590 591
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
        .oname = onam,                                                        \
    },                                                                        \
    .oname = onam,                                                            \
}
592 593 594 595 596 597 598 599 600 601 602 603 604 605
#else
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
    },                                                                        \
    .oname = stringify(name),                                                 \
}
606 607 608 609 610 611 612 613 614 615 616 617 618
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
    },                                                                        \
    .oname = onam,                                                            \
}
619
#endif
B
bellard 已提交
620 621

#define GEN_OPCODE_MARK(name)                                                 \
622
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
623 624 625
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
626
    .pad  = { 0, },                                                           \
B
bellard 已提交
627 628
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
629
        .type = 0x00,                                                         \
B
bellard 已提交
630 631
        .handler = NULL,                                                      \
    },                                                                        \
632
    .oname = stringify(name),                                                 \
B
bellard 已提交
633 634 635 636 637 638
}

/* Start opcode list */
GEN_OPCODE_MARK(start);

/* Invalid instruction */
639 640
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
641
    GEN_EXCP_INVAL(ctx);
642 643
}

B
bellard 已提交
644 645
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
646
    .type    = PPC_NONE,
B
bellard 已提交
647 648 649 650
    .handler = gen_invalid,
};

/***                           Integer arithmetic                          ***/
651 652
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval, type)                 \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
653 654 655 656 657
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
658 659
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
660 661
}

662 663
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
664 665 666 667 668
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
669 670
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
671 672
}

673 674
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
675 676 677 678
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
679 680
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
681
}
682 683
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
684 685 686 687
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
688 689
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
690 691 692
}

/* Two operands arithmetic functions */
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
#define GEN_INT_ARITH2(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000, type)                    \
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)

/* Two operands arithmetic functions with no overflow allowed */
#define GEN_INT_ARITHN(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000400, type)

/* One operand arithmetic functions */
#define GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                                \
__GEN_INT_ARITH1_O(name##o, opc1, opc2, opc3 | 0x10, type)

#if defined(TARGET_PPC64)
#define __GEN_INT_ARITH2_64(name, opc1, opc2, opc3, inval, type)              \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

#define __GEN_INT_ARITH2_O_64(name, opc1, opc2, opc3, inval, type)            \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

#define __GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                     \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}
#define __GEN_INT_ARITH1_O_64(name, opc1, opc2, opc3, type)                   \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    if (ctx->sf_mode)                                                         \
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
}

/* Two operands arithmetic functions */
#define GEN_INT_ARITH2_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000000, type)                 \
__GEN_INT_ARITH2_O_64(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
B
bellard 已提交
764 765

/* Two operands arithmetic functions with no overflow allowed */
766 767
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
B
bellard 已提交
768 769

/* One operand arithmetic functions */
770 771 772 773 774 775 776 777
#define GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                             \
__GEN_INT_ARITH1_O_64(name##o, opc1, opc2, opc3 | 0x10, type)
#else
#define GEN_INT_ARITH2_64 GEN_INT_ARITH2
#define GEN_INT_ARITHN_64 GEN_INT_ARITHN
#define GEN_INT_ARITH1_64 GEN_INT_ARITH1
#endif
B
bellard 已提交
778 779

/* add    add.    addo    addo.    */
780
static always_inline void gen_op_addo (void)
781 782 783 784 785 786 787
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
#define gen_op_add_64 gen_op_add
788
static always_inline void gen_op_addo_64 (void)
789 790 791 792 793 794 795
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (add,    0x1F, 0x0A, 0x08, PPC_INTEGER);
B
bellard 已提交
796
/* addc   addc.   addco   addco.   */
797
static always_inline void gen_op_addc (void)
798 799 800 801 802
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
}
803
static always_inline void gen_op_addco (void)
804 805 806 807 808 809 810
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
811
static always_inline void gen_op_addc_64 (void)
812 813 814 815 816
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc_64();
}
817
static always_inline void gen_op_addco_64 (void)
818 819 820 821 822 823 824 825
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (addc,   0x1F, 0x0A, 0x00, PPC_INTEGER);
B
bellard 已提交
826
/* adde   adde.   addeo   addeo.   */
827
static always_inline void gen_op_addeo (void)
828 829 830 831 832 833
{
    gen_op_move_T2_T0();
    gen_op_adde();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
834
static always_inline void gen_op_addeo_64 (void)
835 836 837 838 839 840 841
{
    gen_op_move_T2_T0();
    gen_op_adde_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (adde,   0x1F, 0x0A, 0x04, PPC_INTEGER);
B
bellard 已提交
842
/* addme  addme.  addmeo  addmeo.  */
843
static always_inline void gen_op_addme (void)
844 845 846 847 848
{
    gen_op_move_T1_T0();
    gen_op_add_me();
}
#if defined(TARGET_PPC64)
849
static always_inline void gen_op_addme_64 (void)
850 851 852 853 854 855
{
    gen_op_move_T1_T0();
    gen_op_add_me_64();
}
#endif
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
B
bellard 已提交
856
/* addze  addze.  addzeo  addzeo.  */
857
static always_inline void gen_op_addze (void)
858 859 860 861 862
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
}
863
static always_inline void gen_op_addzeo (void)
864 865 866 867 868 869 870
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
871
static always_inline void gen_op_addze_64 (void)
872 873 874 875 876
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc_64();
}
877
static always_inline void gen_op_addzeo_64 (void)
878 879 880 881 882 883 884 885
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH1_64 (addze,  0x1F, 0x0A, 0x06, PPC_INTEGER);
B
bellard 已提交
886
/* divw   divw.   divwo   divwo.   */
887
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
B
bellard 已提交
888
/* divwu  divwu.  divwuo  divwuo.  */
889
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
B
bellard 已提交
890
/* mulhw  mulhw.                   */
891
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
B
bellard 已提交
892
/* mulhwu mulhwu.                  */
893
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
B
bellard 已提交
894
/* mullw  mullw.  mullwo  mullwo.  */
895
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
B
bellard 已提交
896
/* neg    neg.    nego    nego.    */
897
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
B
bellard 已提交
898
/* subf   subf.   subfo   subfo.   */
899
static always_inline void gen_op_subfo (void)
900
{
901
    gen_op_moven_T2_T0();
902
    gen_op_subf();
903
    gen_op_check_addo();
904 905 906
}
#if defined(TARGET_PPC64)
#define gen_op_subf_64 gen_op_subf
907
static always_inline void gen_op_subfo_64 (void)
908
{
909
    gen_op_moven_T2_T0();
910
    gen_op_subf();
911
    gen_op_check_addo_64();
912 913 914
}
#endif
GEN_INT_ARITH2_64 (subf,   0x1F, 0x08, 0x01, PPC_INTEGER);
B
bellard 已提交
915
/* subfc  subfc.  subfco  subfco.  */
916
static always_inline void gen_op_subfc (void)
917 918 919 920
{
    gen_op_subf();
    gen_op_check_subfc();
}
921
static always_inline void gen_op_subfco (void)
922
{
923
    gen_op_moven_T2_T0();
924 925
    gen_op_subf();
    gen_op_check_subfc();
926
    gen_op_check_addo();
927 928
}
#if defined(TARGET_PPC64)
929
static always_inline void gen_op_subfc_64 (void)
930 931 932 933
{
    gen_op_subf();
    gen_op_check_subfc_64();
}
934
static always_inline void gen_op_subfco_64 (void)
935
{
936
    gen_op_moven_T2_T0();
937 938
    gen_op_subf();
    gen_op_check_subfc_64();
939
    gen_op_check_addo_64();
940 941 942
}
#endif
GEN_INT_ARITH2_64 (subfc,  0x1F, 0x08, 0x00, PPC_INTEGER);
B
bellard 已提交
943
/* subfe  subfe.  subfeo  subfeo.  */
944
static always_inline void gen_op_subfeo (void)
945
{
946
    gen_op_moven_T2_T0();
947
    gen_op_subfe();
948
    gen_op_check_addo();
949 950 951
}
#if defined(TARGET_PPC64)
#define gen_op_subfe_64 gen_op_subfe
952
static always_inline void gen_op_subfeo_64 (void)
953
{
954
    gen_op_moven_T2_T0();
955
    gen_op_subfe_64();
956
    gen_op_check_addo_64();
957 958 959
}
#endif
GEN_INT_ARITH2_64 (subfe,  0x1F, 0x08, 0x04, PPC_INTEGER);
B
bellard 已提交
960
/* subfme subfme. subfmeo subfmeo. */
961
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
B
bellard 已提交
962
/* subfze subfze. subfzeo subfzeo. */
963
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
B
bellard 已提交
964 965 966
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
967
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
968 969

    if (rA(ctx->opcode) == 0) {
970
        /* li case */
971
        gen_set_T0(simm);
B
bellard 已提交
972 973
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
974 975
        if (likely(simm != 0))
            gen_op_addi(simm);
B
bellard 已提交
976 977 978 979 980 981
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic */
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
982 983
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
984
    gen_op_load_gpr_T0(rA(ctx->opcode));
985 986 987 988 989 990 991 992 993
    if (likely(simm != 0)) {
        gen_op_move_T2_T0();
        gen_op_addi(simm);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
994 995
    } else {
        gen_op_clear_xer_ca();
996
    }
B
bellard 已提交
997 998 999
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic. */
1000
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1001
{
1002 1003
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
1004
    gen_op_load_gpr_T0(rA(ctx->opcode));
1005 1006 1007 1008 1009 1010 1011 1012 1013
    if (likely(simm != 0)) {
        gen_op_move_T2_T0();
        gen_op_addi(simm);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
1014 1015
    } else {
        gen_op_clear_xer_ca();
1016
    }
B
bellard 已提交
1017
    gen_op_store_T0_gpr(rD(ctx->opcode));
1018
    gen_set_Rc0(ctx);
B
bellard 已提交
1019 1020 1021 1022
}
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1023
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
1024 1025

    if (rA(ctx->opcode) == 0) {
1026
        /* lis case */
1027
        gen_set_T0(simm << 16);
B
bellard 已提交
1028 1029
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
1030 1031
        if (likely(simm != 0))
            gen_op_addi(simm << 16);
B
bellard 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_mulli(SIMM(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1046 1047 1048 1049 1050 1051
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_subfic_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_subfic(SIMM(ctx->opcode));
B
bellard 已提交
1052 1053 1054
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

1055 1056
#if defined(TARGET_PPC64)
/* mulhd  mulhd.                   */
1057
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
1058
/* mulhdu mulhdu.                  */
1059
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
1060
/* mulld  mulld.  mulldo  mulldo.  */
1061
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
1062
/* divd   divd.   divdo   divdo.   */
1063
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
1064
/* divdu  divdu.  divduo  divduo.  */
1065
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
1066 1067
#endif

B
bellard 已提交
1068
/***                           Integer comparison                          ***/
1069 1070 1071 1072 1073 1074
#if defined(TARGET_PPC64)
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1075
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))                           \
1076 1077 1078 1079 1080 1081 1082 1083
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}
#else
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
B
bellard 已提交
1084 1085 1086 1087 1088 1089
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}
1090
#endif
B
bellard 已提交
1091 1092

/* cmp */
1093
GEN_CMP(cmp, 0x00, PPC_INTEGER);
B
bellard 已提交
1094 1095 1096 1097
/* cmpi */
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1098
#if defined(TARGET_PPC64)
1099
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1100 1101 1102 1103
        gen_op_cmpi_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_cmpi(SIMM(ctx->opcode));
B
bellard 已提交
1104 1105 1106
    gen_op_store_T0_crf(crfD(ctx->opcode));
}
/* cmpl */
1107
GEN_CMP(cmpl, 0x01, PPC_INTEGER);
B
bellard 已提交
1108 1109 1110 1111
/* cmpli */
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1112
#if defined(TARGET_PPC64)
1113
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1114 1115 1116 1117
        gen_op_cmpli_64(UIMM(ctx->opcode));
    else
#endif
        gen_op_cmpli(UIMM(ctx->opcode));
B
bellard 已提交
1118 1119 1120
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
/* isel (PowerPC 2.03 specification) */
GEN_HANDLER(isel, 0x1F, 0x0F, 0x00, 0x00000001, PPC_203)
{
    uint32_t bi = rC(ctx->opcode);
    uint32_t mask;

    if (rA(ctx->opcode) == 0) {
        gen_set_T0(0);
    } else {
        gen_op_load_gpr_T1(rA(ctx->opcode));
    }
    gen_op_load_gpr_T2(rB(ctx->opcode));
    mask = 1 << (3 - (bi & 0x03));
    gen_op_load_crf_T0(bi >> 2);
    gen_op_test_true(mask);
    gen_op_isel();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

B
bellard 已提交
1140
/***                            Integer logical                            ***/
1141 1142
#define __GEN_LOGICAL2(name, opc2, opc3, type)                                \
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, type)                         \
B
bellard 已提交
1143 1144 1145 1146 1147
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1148 1149
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1150
}
1151 1152
#define GEN_LOGICAL2(name, opc, type)                                         \
__GEN_LOGICAL2(name, 0x1C, opc, type)
B
bellard 已提交
1153

1154 1155
#define GEN_LOGICAL1(name, opc, type)                                         \
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1156 1157 1158 1159
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1160 1161
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1162 1163 1164
}

/* and & and. */
1165
GEN_LOGICAL2(and, 0x00, PPC_INTEGER);
B
bellard 已提交
1166
/* andc & andc. */
1167
GEN_LOGICAL2(andc, 0x01, PPC_INTEGER);
B
bellard 已提交
1168
/* andi. */
1169
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1170 1171
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1172
    gen_op_andi_T0(UIMM(ctx->opcode));
B
bellard 已提交
1173
    gen_op_store_T0_gpr(rA(ctx->opcode));
1174
    gen_set_Rc0(ctx);
B
bellard 已提交
1175 1176
}
/* andis. */
1177
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
B
bellard 已提交
1178 1179
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1180
    gen_op_andi_T0(UIMM(ctx->opcode) << 16);
B
bellard 已提交
1181
    gen_op_store_T0_gpr(rA(ctx->opcode));
1182
    gen_set_Rc0(ctx);
B
bellard 已提交
1183 1184 1185
}

/* cntlzw */
1186
GEN_LOGICAL1(cntlzw, 0x00, PPC_INTEGER);
B
bellard 已提交
1187
/* eqv & eqv. */
1188
GEN_LOGICAL2(eqv, 0x08, PPC_INTEGER);
B
bellard 已提交
1189
/* extsb & extsb. */
1190
GEN_LOGICAL1(extsb, 0x1D, PPC_INTEGER);
B
bellard 已提交
1191
/* extsh & extsh. */
1192
GEN_LOGICAL1(extsh, 0x1C, PPC_INTEGER);
B
bellard 已提交
1193
/* nand & nand. */
1194
GEN_LOGICAL2(nand, 0x0E, PPC_INTEGER);
B
bellard 已提交
1195
/* nor & nor. */
1196
GEN_LOGICAL2(nor, 0x03, PPC_INTEGER);
1197

B
bellard 已提交
1198
/* or & or. */
1199 1200
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    int rs, ra, rb;

    rs = rS(ctx->opcode);
    ra = rA(ctx->opcode);
    rb = rB(ctx->opcode);
    /* Optimisation for mr. ri case */
    if (rs != ra || rs != rb) {
        gen_op_load_gpr_T0(rs);
        if (rs != rb) {
            gen_op_load_gpr_T1(rb);
            gen_op_or();
        }
        gen_op_store_T0_gpr(ra);
        if (unlikely(Rc(ctx->opcode) != 0))
            gen_set_Rc0(ctx);
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_gpr_T0(rs);
        gen_set_Rc0(ctx);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
#if defined(TARGET_PPC64)
    } else {
        switch (rs) {
        case 1:
            /* Set process priority to low */
            gen_op_store_pri(2);
            break;
        case 6:
            /* Set process priority to medium-low */
            gen_op_store_pri(3);
            break;
        case 2:
            /* Set process priority to normal */
            gen_op_store_pri(4);
            break;
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
#if !defined(CONFIG_USER_ONLY)
        case 31:
            if (ctx->supervisor > 0) {
                /* Set process priority to very low */
                gen_op_store_pri(1);
            }
            break;
        case 5:
            if (ctx->supervisor > 0) {
                /* Set process priority to medium-hight */
                gen_op_store_pri(5);
            }
            break;
        case 3:
            if (ctx->supervisor > 0) {
                /* Set process priority to high */
                gen_op_store_pri(6);
            }
            break;
        case 7:
            if (ctx->supervisor > 1) {
                /* Set process priority to very high */
                gen_op_store_pri(7);
            }
            break;
#endif
1260 1261 1262 1263 1264
        default:
            /* nop */
            break;
        }
#endif
1265 1266 1267
    }
}

B
bellard 已提交
1268
/* orc & orc. */
1269
GEN_LOGICAL2(orc, 0x0C, PPC_INTEGER);
B
bellard 已提交
1270
/* xor & xor. */
1271 1272 1273 1274 1275 1276 1277 1278
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    /* Optimisation for "set to zero" case */
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_xor();
    } else {
1279
        gen_op_reset_T0();
1280 1281
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1282 1283
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1284
}
B
bellard 已提交
1285 1286 1287
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1288
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1289

1290 1291
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1292
        /* XXX: should handle special NOPs for POWER series */
1293
        return;
1294 1295 1296
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1297
        gen_op_ori(uimm);
1298
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1299 1300 1301 1302
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1303
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1304

1305 1306 1307
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1308 1309 1310
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1311
        gen_op_ori(uimm << 16);
1312
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1313 1314 1315 1316
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1317
    target_ulong uimm = UIMM(ctx->opcode);
1318 1319 1320 1321 1322

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1323
    gen_op_load_gpr_T0(rS(ctx->opcode));
1324 1325
    if (likely(uimm != 0))
        gen_op_xori(uimm);
B
bellard 已提交
1326 1327 1328 1329 1330 1331
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1332
    target_ulong uimm = UIMM(ctx->opcode);
1333 1334 1335 1336 1337

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1338
    gen_op_load_gpr_T0(rS(ctx->opcode));
1339 1340
    if (likely(uimm != 0))
        gen_op_xori(uimm << 16);
B
bellard 已提交
1341 1342 1343
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
/* popcntb : PowerPC 2.03 specification */
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_203)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_popcntb_64();
    else
#endif
        gen_op_popcntb();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

#if defined(TARGET_PPC64)
/* extsw & extsw. */
GEN_LOGICAL1(extsw, 0x1E, PPC_64B);
/* cntlzd */
GEN_LOGICAL1(cntlzd, 0x01, PPC_64B);
#endif

B
bellard 已提交
1364 1365 1366 1367
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1368 1369
    target_ulong mask;
    uint32_t mb, me, sh;
B
bellard 已提交
1370 1371 1372

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
    sh = SH(ctx->opcode);
    if (likely(sh == 0)) {
        if (likely(mb == 0 && me == 31)) {
            gen_op_load_gpr_T0(rS(ctx->opcode));
            goto do_store;
        } else if (likely(mb == 31 && me == 0)) {
            gen_op_load_gpr_T0(rA(ctx->opcode));
            goto do_store;
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
        gen_op_load_gpr_T1(rA(ctx->opcode));
        goto do_mask;
    }
B
bellard 已提交
1386
    gen_op_load_gpr_T0(rS(ctx->opcode));
B
bellard 已提交
1387
    gen_op_load_gpr_T1(rA(ctx->opcode));
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    gen_op_rotli32_T0(SH(ctx->opcode));
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
    mask = MASK(mb, me);
    gen_op_andi_T0(mask);
    gen_op_andi_T1(~mask);
    gen_op_or();
 do_store:
B
bellard 已提交
1399
    gen_op_store_T0_gpr(rA(ctx->opcode));
1400 1401
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1402 1403 1404 1405 1406
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1407

B
bellard 已提交
1408 1409 1410 1411
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 31)) {
            gen_op_rotli32_T0(sh);
            goto do_store;
        } else if (likely(me == (31 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
B
bellard 已提交
1422
        }
1423 1424 1425 1426
    } else if (likely(me == 31)) {
        if (likely(sh == (32 - mb))) {
            gen_op_srli_T0(mb);
            goto do_store;
B
bellard 已提交
1427 1428
        }
    }
1429 1430 1431 1432 1433 1434 1435 1436
    gen_op_rotli32_T0(sh);
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
    gen_op_andi_T0(MASK(mb, me));
 do_store:
B
bellard 已提交
1437
    gen_op_store_T0_gpr(rA(ctx->opcode));
1438 1439
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
}
/* rlwnm & rlwnm. */
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
1450 1451 1452 1453 1454 1455 1456
    gen_op_rotl32_T0_T1();
    if (unlikely(mb != 0 || me != 31)) {
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
        gen_op_andi_T0(MASK(mb, me));
B
bellard 已提交
1457 1458
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1459 1460
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1461 1462
}

1463 1464
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1465
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1466 1467 1468
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
1469 1470
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1471 1472 1473 1474
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1475
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1476 1477 1478
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
1479 1480
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1481 1482 1483
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
1484 1485
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1486 1487 1488
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
1489 1490
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
             PPC_64B)                                                         \
1491 1492 1493
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1494

1495
static always_inline void gen_andi_T0_64 (DisasContext *ctx, uint64_t mask)
1496 1497 1498 1499 1500 1501 1502
{
    if (mask >> 32)
        gen_op_andi_T0_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T0(mask);
}

1503
static always_inline void gen_andi_T1_64 (DisasContext *ctx, uint64_t mask)
1504 1505 1506 1507 1508 1509 1510
{
    if (mask >> 32)
        gen_op_andi_T1_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T1(mask);
}

1511 1512
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
                                      uint32_t me, uint32_t sh)
J
j_mayer 已提交
1513 1514 1515 1516 1517 1518 1519
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 63)) {
1520
            gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1521 1522 1523 1524 1525 1526 1527
            goto do_store;
        } else if (likely(me == (63 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
        }
    } else if (likely(me == 63)) {
        if (likely(sh == (64 - mb))) {
1528
            gen_op_srli_T0_64(mb);
J
j_mayer 已提交
1529 1530 1531 1532 1533
            goto do_store;
        }
    }
    gen_op_rotli64_T0(sh);
 do_mask:
1534
    gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1535 1536 1537 1538 1539
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
1540
/* rldicl - rldicl. */
1541
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1542
{
J
j_mayer 已提交
1543
    uint32_t sh, mb;
1544

J
j_mayer 已提交
1545 1546
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1547
    gen_rldinm(ctx, mb, 63, sh);
1548
}
J
j_mayer 已提交
1549
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1550
/* rldicr - rldicr. */
1551
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1552
{
J
j_mayer 已提交
1553
    uint32_t sh, me;
1554

J
j_mayer 已提交
1555 1556
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1557
    gen_rldinm(ctx, 0, me, sh);
1558
}
J
j_mayer 已提交
1559
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1560
/* rldic - rldic. */
1561
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1562
{
J
j_mayer 已提交
1563
    uint32_t sh, mb;
1564

J
j_mayer 已提交
1565 1566
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1567 1568 1569 1570
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

1571 1572
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
                                     uint32_t me)
J
j_mayer 已提交
1573 1574 1575 1576 1577
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_rotl64_T0_T1();
    if (unlikely(mb != 0 || me != 63)) {
1578
        gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1579 1580 1581 1582
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1583
}
J
j_mayer 已提交
1584

1585
/* rldcl - rldcl. */
1586
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1587
{
J
j_mayer 已提交
1588
    uint32_t mb;
1589

J
j_mayer 已提交
1590
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1591
    gen_rldnm(ctx, mb, 63);
1592
}
1593
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1594
/* rldcr - rldcr. */
1595
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1596
{
J
j_mayer 已提交
1597
    uint32_t me;
1598

J
j_mayer 已提交
1599
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1600
    gen_rldnm(ctx, 0, me);
1601
}
1602
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1603
/* rldimi - rldimi. */
1604
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1605
{
J
j_mayer 已提交
1606
    uint64_t mask;
1607
    uint32_t sh, mb, me;
1608

J
j_mayer 已提交
1609 1610
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
1611
    me = 63 - sh;
J
j_mayer 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
    if (likely(sh == 0)) {
        if (likely(mb == 0)) {
            gen_op_load_gpr_T0(rS(ctx->opcode));
            goto do_store;
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
        gen_op_load_gpr_T1(rA(ctx->opcode));
        goto do_mask;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rA(ctx->opcode));
1623
    gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1624
 do_mask:
1625
    mask = MASK(mb, me);
1626 1627
    gen_andi_T0_64(ctx, mask);
    gen_andi_T1_64(ctx, ~mask);
J
j_mayer 已提交
1628 1629 1630 1631 1632
    gen_op_or();
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1633
}
1634
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1635 1636
#endif

B
bellard 已提交
1637 1638
/***                             Integer shift                             ***/
/* slw & slw. */
1639
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
B
bellard 已提交
1640
/* sraw & sraw. */
1641
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
B
bellard 已提交
1642 1643 1644
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
1645
    int mb, me;
B
bellard 已提交
1646
    gen_op_load_gpr_T0(rS(ctx->opcode));
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
    if (SH(ctx->opcode) != 0) {
        gen_op_move_T1_T0();
        mb = 32 - SH(ctx->opcode);
        me = 31;
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
        gen_op_srawi(SH(ctx->opcode), MASK(mb, me));
    }
B
bellard 已提交
1657
    gen_op_store_T0_gpr(rA(ctx->opcode));
1658 1659
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1660 1661
}
/* srw & srw. */
1662 1663 1664 1665 1666 1667 1668 1669
__GEN_LOGICAL2(srw, 0x18, 0x10, PPC_INTEGER);

#if defined(TARGET_PPC64)
/* sld & sld. */
__GEN_LOGICAL2(sld, 0x1B, 0x00, PPC_64B);
/* srad & srad. */
__GEN_LOGICAL2(srad, 0x1A, 0x18, PPC_64B);
/* sradi & sradi. */
1670
static always_inline void gen_sradi (DisasContext *ctx, int n)
1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
{
    uint64_t mask;
    int sh, mb, me;

    gen_op_load_gpr_T0(rS(ctx->opcode));
    sh = SH(ctx->opcode) + (n << 5);
    if (sh != 0) {
        gen_op_move_T1_T0();
        mb = 64 - SH(ctx->opcode);
        me = 63;
        mask = MASK(mb, me);
        gen_op_sradi(sh, mask >> 32, mask);
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
1688
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
1689 1690 1691
{
    gen_sradi(ctx, 0);
}
1692
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
1693 1694 1695 1696 1697 1698
{
    gen_sradi(ctx, 1);
}
/* srd & srd. */
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
#endif
B
bellard 已提交
1699 1700

/***                       Floating-Point arithmetic                       ***/
1701
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
1702
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1703
{                                                                             \
1704
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1705
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1706 1707
        return;                                                               \
    }                                                                         \
1708 1709 1710
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
1711
    gen_reset_fpstatus();                                                     \
1712 1713 1714 1715
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1716
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1717
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1718 1719
}

1720 1721 1722
#define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
1723

1724 1725
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1726
{                                                                             \
1727
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1728
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1729 1730
        return;                                                               \
    }                                                                         \
1731 1732
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
1733
    gen_reset_fpstatus();                                                     \
1734 1735 1736 1737
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1738
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1739
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1740
}
1741 1742 1743
#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
1744

1745 1746
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1747
{                                                                             \
1748
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1749
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1750 1751
        return;                                                               \
    }                                                                         \
1752 1753
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
1754
    gen_reset_fpstatus();                                                     \
1755 1756 1757 1758
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1759
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1760
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1761
}
1762 1763 1764
#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
1765

1766
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
1767
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
1768
{                                                                             \
1769
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1770
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1771 1772
        return;                                                               \
    }                                                                         \
1773
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
1774
    gen_reset_fpstatus();                                                     \
1775 1776
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1777
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
1778 1779
}

1780
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
1781
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
1782
{                                                                             \
1783
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1784
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1785 1786
        return;                                                               \
    }                                                                         \
1787
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
1788
    gen_reset_fpstatus();                                                     \
1789 1790
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1791
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
B
bellard 已提交
1792 1793
}

1794
/* fadd - fadds */
1795
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
1796
/* fdiv - fdivs */
1797
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
1798
/* fmul - fmuls */
1799
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
B
bellard 已提交
1800

1801
/* fre */
1802
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
1803

1804
/* fres */
1805
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
B
bellard 已提交
1806

1807
/* frsqrte */
1808 1809 1810 1811 1812 1813 1814 1815
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);

/* frsqrtes */
static always_inline void gen_op_frsqrtes (void)
{
    gen_op_frsqrte();
    gen_op_frsp();
}
1816
GEN_FLOAT_BS(rsqrtes, 0x3B, 0x1A, 1, PPC_FLOAT_FRSQRTES);
B
bellard 已提交
1817

1818
/* fsel */
1819
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
1820
/* fsub - fsubs */
1821
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
B
bellard 已提交
1822 1823
/* Optional: */
/* fsqrt */
1824
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1825
{
1826
    if (unlikely(!ctx->fpu_enabled)) {
1827
        GEN_EXCP_NO_FP(ctx);
1828 1829 1830
        return;
    }
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1831
    gen_reset_fpstatus();
1832 1833
    gen_op_fsqrt();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1834
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
1835
}
B
bellard 已提交
1836

1837
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
1838
{
1839
    if (unlikely(!ctx->fpu_enabled)) {
1840
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1841 1842
        return;
    }
1843
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1844
    gen_reset_fpstatus();
1845 1846
    gen_op_fsqrt();
    gen_op_frsp();
1847
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1848
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
B
bellard 已提交
1849 1850 1851
}

/***                     Floating-Point multiply-and-add                   ***/
1852
/* fmadd - fmadds */
1853
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
1854
/* fmsub - fmsubs */
1855
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
1856
/* fnmadd - fnmadds */
1857
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
1858
/* fnmsub - fnmsubs */
1859
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
B
bellard 已提交
1860 1861 1862

/***                     Floating-Point round & convert                    ***/
/* fctiw */
1863
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
1864
/* fctiwz */
1865
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
B
bellard 已提交
1866
/* frsp */
1867
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
J
j_mayer 已提交
1868 1869
#if defined(TARGET_PPC64)
/* fcfid */
1870
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
J
j_mayer 已提交
1871
/* fctid */
1872
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
J
j_mayer 已提交
1873
/* fctidz */
1874
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
J
j_mayer 已提交
1875
#endif
B
bellard 已提交
1876

1877
/* frin */
1878
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
1879
/* friz */
1880
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
1881
/* frip */
1882
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
1883
/* frim */
1884
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
1885

B
bellard 已提交
1886 1887
/***                         Floating-Point compare                        ***/
/* fcmpo */
1888
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1889
{
1890
    if (unlikely(!ctx->fpu_enabled)) {
1891
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1892 1893
        return;
    }
1894 1895
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
1896
    gen_reset_fpstatus();
1897 1898
    gen_op_fcmpo();
    gen_op_store_T0_crf(crfD(ctx->opcode));
1899
    gen_op_float_check_status();
B
bellard 已提交
1900 1901 1902
}

/* fcmpu */
1903
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1904
{
1905
    if (unlikely(!ctx->fpu_enabled)) {
1906
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1907 1908
        return;
    }
1909 1910
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
1911
    gen_reset_fpstatus();
1912 1913
    gen_op_fcmpu();
    gen_op_store_T0_crf(crfD(ctx->opcode));
1914
    gen_op_float_check_status();
B
bellard 已提交
1915 1916
}

1917 1918
/***                         Floating-point move                           ***/
/* fabs */
1919 1920
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
1921 1922

/* fmr  - fmr. */
1923
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
1924 1925
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
1926
    if (unlikely(!ctx->fpu_enabled)) {
1927
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1928 1929
        return;
    }
1930 1931
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1932
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
1933 1934 1935
}

/* fnabs */
1936 1937
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
1938
/* fneg */
1939 1940
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
1941

B
bellard 已提交
1942 1943 1944 1945
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
1946 1947
    int bfa;

1948
    if (unlikely(!ctx->fpu_enabled)) {
1949
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1950 1951
        return;
    }
1952 1953 1954
    gen_optimize_fprf();
    bfa = 4 * (7 - crfS(ctx->opcode));
    gen_op_load_fpscr_T0(bfa);
B
bellard 已提交
1955
    gen_op_store_T0_crf(crfD(ctx->opcode));
1956
    gen_op_fpscr_resetbit(~(0xF << bfa));
B
bellard 已提交
1957 1958 1959 1960 1961
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
1962
    if (unlikely(!ctx->fpu_enabled)) {
1963
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1964 1965
        return;
    }
1966 1967 1968
    gen_optimize_fprf();
    gen_reset_fpstatus();
    gen_op_load_fpscr_FT0();
B
bellard 已提交
1969
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1970
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
B
bellard 已提交
1971 1972 1973 1974 1975
}

/* mtfsb0 */
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1976
    uint8_t crb;
1977

1978
    if (unlikely(!ctx->fpu_enabled)) {
1979
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1980 1981
        return;
    }
1982 1983 1984 1985 1986 1987 1988 1989 1990
    crb = 32 - (crbD(ctx->opcode) >> 2);
    gen_optimize_fprf();
    gen_reset_fpstatus();
    if (likely(crb != 30 && crb != 29))
        gen_op_fpscr_resetbit(~(1 << crb));
    if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
B
bellard 已提交
1991 1992 1993 1994 1995
}

/* mtfsb1 */
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
{
B
bellard 已提交
1996
    uint8_t crb;
1997

1998
    if (unlikely(!ctx->fpu_enabled)) {
1999
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2000 2001
        return;
    }
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
    crb = 32 - (crbD(ctx->opcode) >> 2);
    gen_optimize_fprf();
    gen_reset_fpstatus();
    /* XXX: we pretend we can only do IEEE floating-point computations */
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI))
        gen_op_fpscr_setbit(crb);
    if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2014 2015 2016 2017 2018
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
2019
    if (unlikely(!ctx->fpu_enabled)) {
2020
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2021 2022
        return;
    }
2023
    gen_optimize_fprf();
B
bellard 已提交
2024
    gen_op_load_fpr_FT0(rB(ctx->opcode));
2025
    gen_reset_fpstatus();
2026
    gen_op_store_fpscr(FM(ctx->opcode));
2027 2028 2029 2030 2031 2032
    if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2033 2034 2035 2036 2037
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
2038 2039
    int bf, sh;

2040
    if (unlikely(!ctx->fpu_enabled)) {
2041
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
2042 2043
        return;
    }
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055
    bf = crbD(ctx->opcode) >> 2;
    sh = 7 - bf;
    gen_optimize_fprf();
    gen_op_set_FT0(FPIMM(ctx->opcode) << (4 * sh));
    gen_reset_fpstatus();
    gen_op_store_fpscr(1 << sh);
    if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_fpcc();
        gen_op_set_Rc0();
    }
    /* We can raise a differed exception */
    gen_op_float_check_status();
B
bellard 已提交
2056 2057
}

2058 2059
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2060 2061
static always_inline void gen_addr_imm_index (DisasContext *ctx,
                                              target_long maskl)
2062 2063 2064
{
    target_long simm = SIMM(ctx->opcode);

2065
    simm &= ~maskl;
2066
    if (rA(ctx->opcode) == 0) {
2067
        gen_set_T0(simm);
2068 2069 2070 2071 2072
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        if (likely(simm != 0))
            gen_op_addi(simm);
    }
2073 2074 2075
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
2076 2077
}

2078
static always_inline void gen_addr_reg_index (DisasContext *ctx)
2079 2080 2081 2082 2083 2084 2085 2086
{
    if (rA(ctx->opcode) == 0) {
        gen_op_load_gpr_T0(rB(ctx->opcode));
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_add();
    }
2087 2088 2089
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
2090 2091
}

2092
static always_inline void gen_addr_register (DisasContext *ctx)
2093 2094 2095 2096 2097 2098
{
    if (rA(ctx->opcode) == 0) {
        gen_op_reset_T0();
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
    }
2099 2100 2101
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
2102 2103
}

2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
#if defined(TARGET_PPC64)
#define _GEN_MEM_FUNCS(name, mode)                                            \
    &gen_op_##name##_##mode,                                                  \
    &gen_op_##name##_le_##mode,                                               \
    &gen_op_##name##_64_##mode,                                               \
    &gen_op_##name##_le_64_##mode
#else
#define _GEN_MEM_FUNCS(name, mode)                                            \
    &gen_op_##name##_##mode,                                                  \
    &gen_op_##name##_le_##mode
#endif
2115
#if defined(CONFIG_USER_ONLY)
2116
#if defined(TARGET_PPC64)
2117
#define NB_MEM_FUNCS 4
2118
#else
2119
#define NB_MEM_FUNCS 2
2120
#endif
2121 2122
#define GEN_MEM_FUNCS(name)                                                   \
    _GEN_MEM_FUNCS(name, raw)
2123
#else
2124
#if defined(TARGET_PPC64)
2125
#define NB_MEM_FUNCS 12
2126
#else
2127
#define NB_MEM_FUNCS 6
2128
#endif
2129 2130 2131 2132 2133 2134 2135 2136
#define GEN_MEM_FUNCS(name)                                                   \
    _GEN_MEM_FUNCS(name, user),                                               \
    _GEN_MEM_FUNCS(name, kernel),                                             \
    _GEN_MEM_FUNCS(name, hypv)
#endif

/***                             Integer load                              ***/
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
2137
/* Byte access routine are endian safe */
2138 2139 2140 2141 2142
#define gen_op_lbz_le_raw       gen_op_lbz_raw
#define gen_op_lbz_le_user      gen_op_lbz_user
#define gen_op_lbz_le_kernel    gen_op_lbz_kernel
#define gen_op_lbz_le_hypv      gen_op_lbz_hypv
#define gen_op_lbz_le_64_raw    gen_op_lbz_64_raw
2143
#define gen_op_lbz_le_64_user   gen_op_lbz_64_user
2144
#define gen_op_lbz_le_64_kernel gen_op_lbz_64_kernel
2145 2146 2147 2148 2149 2150 2151 2152 2153
#define gen_op_lbz_le_64_hypv   gen_op_lbz_64_hypv
#define gen_op_stb_le_raw       gen_op_stb_raw
#define gen_op_stb_le_user      gen_op_stb_user
#define gen_op_stb_le_kernel    gen_op_stb_kernel
#define gen_op_stb_le_hypv      gen_op_stb_hypv
#define gen_op_stb_le_64_raw    gen_op_stb_64_raw
#define gen_op_stb_le_64_user   gen_op_stb_64_user
#define gen_op_stb_le_64_kernel gen_op_stb_64_kernel
#define gen_op_stb_le_64_hypv   gen_op_stb_64_hypv
2154
#define OP_LD_TABLE(width)                                                    \
2155 2156
static GenOpFunc *gen_op_l##width[NB_MEM_FUNCS] = {                           \
    GEN_MEM_FUNCS(l##width),                                                  \
2157 2158
};
#define OP_ST_TABLE(width)                                                    \
2159 2160
static GenOpFunc *gen_op_st##width[NB_MEM_FUNCS] = {                          \
    GEN_MEM_FUNCS(st##width),                                                 \
2161
};
2162

2163 2164
#define GEN_LD(width, opc, type)                                              \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2165
{                                                                             \
J
j_mayer 已提交
2166
    gen_addr_imm_index(ctx, 0);                                               \
2167
    op_ldst(l##width);                                                        \
B
bellard 已提交
2168 2169 2170
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2171 2172
#define GEN_LDU(width, opc, type)                                             \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2173
{                                                                             \
2174 2175
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2176
        GEN_EXCP_INVAL(ctx);                                                  \
2177
        return;                                                               \
2178
    }                                                                         \
J
j_mayer 已提交
2179
    if (type == PPC_64B)                                                      \
2180
        gen_addr_imm_index(ctx, 0x03);                                        \
J
j_mayer 已提交
2181 2182
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
2183
    op_ldst(l##width);                                                        \
B
bellard 已提交
2184 2185 2186 2187
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2188 2189
#define GEN_LDUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2190
{                                                                             \
2191 2192
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2193
        GEN_EXCP_INVAL(ctx);                                                  \
2194
        return;                                                               \
2195
    }                                                                         \
2196
    gen_addr_reg_index(ctx);                                                  \
2197
    op_ldst(l##width);                                                        \
B
bellard 已提交
2198 2199 2200 2201
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2202 2203
#define GEN_LDX(width, opc2, opc3, type)                                      \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2204
{                                                                             \
2205
    gen_addr_reg_index(ctx);                                                  \
2206
    op_ldst(l##width);                                                        \
B
bellard 已提交
2207 2208 2209
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2210
#define GEN_LDS(width, op, type)                                              \
2211
OP_LD_TABLE(width);                                                           \
2212 2213 2214 2215
GEN_LD(width, op | 0x20, type);                                               \
GEN_LDU(width, op | 0x21, type);                                              \
GEN_LDUX(width, 0x17, op | 0x01, type);                                       \
GEN_LDX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2216 2217

/* lbz lbzu lbzux lbzx */
2218
GEN_LDS(bz, 0x02, PPC_INTEGER);
B
bellard 已提交
2219
/* lha lhau lhaux lhax */
2220
GEN_LDS(ha, 0x0A, PPC_INTEGER);
B
bellard 已提交
2221
/* lhz lhzu lhzux lhzx */
2222
GEN_LDS(hz, 0x08, PPC_INTEGER);
B
bellard 已提交
2223
/* lwz lwzu lwzux lwzx */
2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
GEN_LDS(wz, 0x00, PPC_INTEGER);
#if defined(TARGET_PPC64)
OP_LD_TABLE(wa);
OP_LD_TABLE(d);
/* lwaux */
GEN_LDUX(wa, 0x15, 0x0B, PPC_64B);
/* lwax */
GEN_LDX(wa, 0x15, 0x0A, PPC_64B);
/* ldux */
GEN_LDUX(d, 0x15, 0x01, PPC_64B);
/* ldx */
GEN_LDX(d, 0x15, 0x00, PPC_64B);
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
    if (Rc(ctx->opcode)) {
        if (unlikely(rA(ctx->opcode) == 0 ||
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2241
            GEN_EXCP_INVAL(ctx);
2242 2243 2244
            return;
        }
    }
2245
    gen_addr_imm_index(ctx, 0x03);
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
    if (ctx->opcode & 0x02) {
        /* lwa (lwau is undefined) */
        op_ldst(lwa);
    } else {
        /* ld - ldu */
        op_ldst(ld);
    }
    gen_op_store_T1_gpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_store_T0_gpr(rA(ctx->opcode));
}
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
/* lq */
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    int ra, rd;

    /* Restore CPU state */
    if (unlikely(ctx->supervisor == 0)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    ra = rA(ctx->opcode);
    rd = rD(ctx->opcode);
    if (unlikely((rd & 1) || rd == ra)) {
        GEN_EXCP_INVAL(ctx);
        return;
    }
    if (unlikely(ctx->mem_idx & 1)) {
        /* Little-endian mode is not handled */
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
        return;
    }
    gen_addr_imm_index(ctx, 0x0F);
    op_ldst(ld);
    gen_op_store_T1_gpr(rd);
    gen_op_addi(8);
    op_ldst(ld);
    gen_op_store_T1_gpr(rd + 1);
#endif
}
2289
#endif
B
bellard 已提交
2290 2291

/***                              Integer store                            ***/
2292 2293
#define GEN_ST(width, opc, type)                                              \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2294
{                                                                             \
J
j_mayer 已提交
2295
    gen_addr_imm_index(ctx, 0);                                               \
2296 2297
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2298 2299
}

2300 2301
#define GEN_STU(width, opc, type)                                             \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2302
{                                                                             \
2303
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2304
        GEN_EXCP_INVAL(ctx);                                                  \
2305
        return;                                                               \
2306
    }                                                                         \
J
j_mayer 已提交
2307
    if (type == PPC_64B)                                                      \
2308
        gen_addr_imm_index(ctx, 0x03);                                        \
J
j_mayer 已提交
2309 2310
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
B
bellard 已提交
2311
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2312
    op_ldst(st##width);                                                       \
B
bellard 已提交
2313 2314 2315
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2316 2317
#define GEN_STUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
B
bellard 已提交
2318
{                                                                             \
2319
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2320
        GEN_EXCP_INVAL(ctx);                                                  \
2321
        return;                                                               \
2322
    }                                                                         \
2323
    gen_addr_reg_index(ctx);                                                  \
2324 2325
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2326 2327 2328
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2329 2330
#define GEN_STX(width, opc2, opc3, type)                                      \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2331
{                                                                             \
2332
    gen_addr_reg_index(ctx);                                                  \
2333 2334
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2335 2336
}

2337
#define GEN_STS(width, op, type)                                              \
2338
OP_ST_TABLE(width);                                                           \
2339 2340 2341 2342
GEN_ST(width, op | 0x20, type);                                               \
GEN_STU(width, op | 0x21, type);                                              \
GEN_STUX(width, 0x17, op | 0x01, type);                                       \
GEN_STX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2343 2344

/* stb stbu stbux stbx */
2345
GEN_STS(b, 0x06, PPC_INTEGER);
B
bellard 已提交
2346
/* sth sthu sthux sthx */
2347
GEN_STS(h, 0x0C, PPC_INTEGER);
B
bellard 已提交
2348
/* stw stwu stwux stwx */
2349 2350 2351
GEN_STS(w, 0x04, PPC_INTEGER);
#if defined(TARGET_PPC64)
OP_ST_TABLE(d);
J
j_mayer 已提交
2352 2353
GEN_STUX(d, 0x15, 0x05, PPC_64B);
GEN_STX(d, 0x15, 0x04, PPC_64B);
2354
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2355
{
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
    int rs;

    rs = rS(ctx->opcode);
    if ((ctx->opcode & 0x3) == 0x2) {
#if defined(CONFIG_USER_ONLY)
        GEN_EXCP_PRIVOPC(ctx);
#else
        /* stq */
        if (unlikely(ctx->supervisor == 0)) {
            GEN_EXCP_PRIVOPC(ctx);
            return;
        }
        if (unlikely(rs & 1)) {
2369
            GEN_EXCP_INVAL(ctx);
2370 2371
            return;
        }
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
        if (unlikely(ctx->mem_idx & 1)) {
            /* Little-endian mode is not handled */
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
            return;
        }
        gen_addr_imm_index(ctx, 0x03);
        gen_op_load_gpr_T1(rs);
        op_ldst(std);
        gen_op_addi(8);
        gen_op_load_gpr_T1(rs + 1);
        op_ldst(std);
#endif
    } else {
        /* std / stdu */
        if (Rc(ctx->opcode)) {
            if (unlikely(rA(ctx->opcode) == 0)) {
                GEN_EXCP_INVAL(ctx);
                return;
            }
        }
        gen_addr_imm_index(ctx, 0x03);
        gen_op_load_gpr_T1(rs);
        op_ldst(std);
        if (Rc(ctx->opcode))
            gen_op_store_T0_gpr(rA(ctx->opcode));
2397 2398 2399
    }
}
#endif
B
bellard 已提交
2400 2401
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
2402
OP_LD_TABLE(hbr);
2403
GEN_LDX(hbr, 0x16, 0x18, PPC_INTEGER);
B
bellard 已提交
2404
/* lwbrx */
2405
OP_LD_TABLE(wbr);
2406
GEN_LDX(wbr, 0x16, 0x10, PPC_INTEGER);
B
bellard 已提交
2407
/* sthbrx */
2408
OP_ST_TABLE(hbr);
2409
GEN_STX(hbr, 0x16, 0x1C, PPC_INTEGER);
B
bellard 已提交
2410
/* stwbrx */
2411
OP_ST_TABLE(wbr);
2412
GEN_STX(wbr, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
2413 2414

/***                    Integer load and store multiple                    ***/
2415
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2416 2417
static GenOpFunc1 *gen_op_lmw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lmw),
2418
};
2419 2420
static GenOpFunc1 *gen_op_stmw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stmw),
2421
};
2422

B
bellard 已提交
2423 2424 2425
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2426
    /* NIP cannot be restored if the memory exception comes from an helper */
2427
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2428
    gen_addr_imm_index(ctx, 0);
2429
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
2430 2431 2432 2433 2434
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2435
    /* NIP cannot be restored if the memory exception comes from an helper */
2436
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2437
    gen_addr_imm_index(ctx, 0);
2438
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
2439 2440 2441
}

/***                    Integer load and store strings                     ***/
2442 2443
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
2444 2445
static GenOpFunc1 *gen_op_lswi[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lswi),
2446
};
2447 2448
static GenOpFunc3 *gen_op_lswx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lswx),
2449
};
2450 2451
static GenOpFunc1 *gen_op_stsw[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stsw),
2452 2453
};

B
bellard 已提交
2454
/* lswi */
2455
/* PowerPC32 specification says we must generate an exception if
2456 2457 2458 2459
 * rA is in the range of registers to be loaded.
 * In an other hand, IBM says this is valid, but rA won't be loaded.
 * For now, I'll follow the spec...
 */
B
bellard 已提交
2460 2461 2462 2463
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
{
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
2464
    int ra = rA(ctx->opcode);
B
bellard 已提交
2465 2466 2467 2468 2469
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
2470 2471 2472
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
2473 2474
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
2475
        return;
B
bellard 已提交
2476
    }
2477
    /* NIP cannot be restored if the memory exception comes from an helper */
2478
    gen_update_nip(ctx, ctx->nip - 4);
2479 2480
    gen_addr_register(ctx);
    gen_op_set_T1(nb);
2481
    op_ldsts(lswi, start);
B
bellard 已提交
2482 2483 2484 2485 2486
}

/* lswx */
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
{
2487 2488 2489
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

2490
    /* NIP cannot be restored if the memory exception comes from an helper */
2491
    gen_update_nip(ctx, ctx->nip - 4);
2492
    gen_addr_reg_index(ctx);
2493 2494
    if (ra == 0) {
        ra = rb;
B
bellard 已提交
2495
    }
2496 2497
    gen_op_load_xer_bc();
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
B
bellard 已提交
2498 2499 2500 2501 2502
}

/* stswi */
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
{
B
bellard 已提交
2503 2504
    int nb = NB(ctx->opcode);

2505
    /* NIP cannot be restored if the memory exception comes from an helper */
2506
    gen_update_nip(ctx, ctx->nip - 4);
2507
    gen_addr_register(ctx);
B
bellard 已提交
2508 2509 2510
    if (nb == 0)
        nb = 32;
    gen_op_set_T1(nb);
2511
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2512 2513 2514 2515 2516
}

/* stswx */
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
{
2517
    /* NIP cannot be restored if the memory exception comes from an helper */
2518
    gen_update_nip(ctx, ctx->nip - 4);
2519 2520
    gen_addr_reg_index(ctx);
    gen_op_load_xer_bc();
2521
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2522 2523 2524 2525
}

/***                        Memory synchronisation                         ***/
/* eieio */
2526
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
2527 2528 2529 2530
{
}

/* isync */
2531
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
2532
{
2533
    GEN_STOP(ctx);
B
bellard 已提交
2534 2535
}

2536 2537
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
2538 2539
static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(lwarx),
2540
};
2541 2542
static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stwcx),
B
bellard 已提交
2543
};
2544

2545
/* lwarx */
2546
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
2547
{
2548 2549
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2550
    gen_addr_reg_index(ctx);
B
bellard 已提交
2551
    op_lwarx();
B
bellard 已提交
2552 2553 2554 2555
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stwcx. */
2556
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
2557
{
2558 2559
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2560
    gen_addr_reg_index(ctx);
2561 2562
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stwcx();
B
bellard 已提交
2563 2564
}

J
j_mayer 已提交
2565 2566 2567
#if defined(TARGET_PPC64)
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
2568 2569
static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(ldarx),
J
j_mayer 已提交
2570
};
2571 2572
static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(stdcx),
J
j_mayer 已提交
2573 2574 2575
};

/* ldarx */
2576
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
2577
{
2578 2579
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2580 2581 2582 2583 2584 2585
    gen_addr_reg_index(ctx);
    op_ldarx();
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stdcx. */
2586
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
2587
{
2588 2589
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2590 2591 2592 2593 2594 2595
    gen_addr_reg_index(ctx);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stdcx();
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
2596
/* sync */
2597
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
2598 2599 2600
{
}

2601 2602 2603 2604
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
    /* Stop translation, as the CPU is supposed to sleep from now */
2605 2606
    gen_op_wait();
    GEN_EXCP(ctx, EXCP_HLT, 1);
2607 2608
}

B
bellard 已提交
2609
/***                         Floating-point load                           ***/
2610 2611
#define GEN_LDF(width, opc, type)                                             \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2612
{                                                                             \
2613
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2614
        GEN_EXCP_NO_FP(ctx);                                                  \
2615 2616
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2617
    gen_addr_imm_index(ctx, 0);                                               \
2618
    op_ldst(l##width);                                                        \
2619
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2620 2621
}

2622 2623
#define GEN_LDUF(width, opc, type)                                            \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2624
{                                                                             \
2625
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2626
        GEN_EXCP_NO_FP(ctx);                                                  \
2627 2628
        return;                                                               \
    }                                                                         \
2629
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2630
        GEN_EXCP_INVAL(ctx);                                                  \
2631
        return;                                                               \
2632
    }                                                                         \
J
j_mayer 已提交
2633
    gen_addr_imm_index(ctx, 0);                                               \
2634
    op_ldst(l##width);                                                        \
2635
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2636 2637 2638
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2639 2640
#define GEN_LDUXF(width, opc, type)                                           \
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
B
bellard 已提交
2641
{                                                                             \
2642
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2643
        GEN_EXCP_NO_FP(ctx);                                                  \
2644 2645
        return;                                                               \
    }                                                                         \
2646
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2647
        GEN_EXCP_INVAL(ctx);                                                  \
2648
        return;                                                               \
2649
    }                                                                         \
2650
    gen_addr_reg_index(ctx);                                                  \
2651
    op_ldst(l##width);                                                        \
2652
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2653 2654 2655
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2656 2657
#define GEN_LDXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2658
{                                                                             \
2659
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2660
        GEN_EXCP_NO_FP(ctx);                                                  \
2661 2662
        return;                                                               \
    }                                                                         \
2663
    gen_addr_reg_index(ctx);                                                  \
2664
    op_ldst(l##width);                                                        \
2665
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2666 2667
}

2668
#define GEN_LDFS(width, op, type)                                             \
2669
OP_LD_TABLE(width);                                                           \
2670 2671 2672 2673
GEN_LDF(width, op | 0x20, type);                                              \
GEN_LDUF(width, op | 0x21, type);                                             \
GEN_LDUXF(width, op | 0x01, type);                                            \
GEN_LDXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
2674 2675

/* lfd lfdu lfdux lfdx */
2676
GEN_LDFS(fd, 0x12, PPC_FLOAT);
B
bellard 已提交
2677
/* lfs lfsu lfsux lfsx */
2678
GEN_LDFS(fs, 0x10, PPC_FLOAT);
B
bellard 已提交
2679 2680

/***                         Floating-point store                          ***/
2681 2682
#define GEN_STF(width, opc, type)                                             \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2683
{                                                                             \
2684
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2685
        GEN_EXCP_NO_FP(ctx);                                                  \
2686 2687
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2688
    gen_addr_imm_index(ctx, 0);                                               \
2689
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2690
    op_ldst(st##width);                                                       \
B
bellard 已提交
2691 2692
}

2693 2694
#define GEN_STUF(width, opc, type)                                            \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2695
{                                                                             \
2696
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2697
        GEN_EXCP_NO_FP(ctx);                                                  \
2698 2699
        return;                                                               \
    }                                                                         \
2700
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2701
        GEN_EXCP_INVAL(ctx);                                                  \
2702
        return;                                                               \
2703
    }                                                                         \
J
j_mayer 已提交
2704
    gen_addr_imm_index(ctx, 0);                                               \
2705
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2706
    op_ldst(st##width);                                                       \
B
bellard 已提交
2707 2708 2709
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2710 2711
#define GEN_STUXF(width, opc, type)                                           \
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
B
bellard 已提交
2712
{                                                                             \
2713
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2714
        GEN_EXCP_NO_FP(ctx);                                                  \
2715 2716
        return;                                                               \
    }                                                                         \
2717
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2718
        GEN_EXCP_INVAL(ctx);                                                  \
2719
        return;                                                               \
2720
    }                                                                         \
2721 2722
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2723
    op_ldst(st##width);                                                       \
B
bellard 已提交
2724 2725 2726
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2727 2728
#define GEN_STXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2729
{                                                                             \
2730
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2731
        GEN_EXCP_NO_FP(ctx);                                                  \
2732 2733
        return;                                                               \
    }                                                                         \
2734 2735
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2736
    op_ldst(st##width);                                                       \
B
bellard 已提交
2737 2738
}

2739
#define GEN_STFS(width, op, type)                                             \
2740
OP_ST_TABLE(width);                                                           \
2741 2742 2743 2744
GEN_STF(width, op | 0x20, type);                                              \
GEN_STUF(width, op | 0x21, type);                                             \
GEN_STUXF(width, op | 0x01, type);                                            \
GEN_STXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
2745 2746

/* stfd stfdu stfdux stfdx */
2747
GEN_STFS(fd, 0x16, PPC_FLOAT);
B
bellard 已提交
2748
/* stfs stfsu stfsux stfsx */
2749
GEN_STFS(fs, 0x14, PPC_FLOAT);
B
bellard 已提交
2750 2751 2752

/* Optional: */
/* stfiwx */
2753 2754
OP_ST_TABLE(fiwx);
GEN_STXF(fiwx, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
2755 2756

/***                                Branch                                 ***/
2757 2758
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
                                       target_ulong dest)
2759 2760 2761 2762 2763 2764 2765 2766
{
    TranslationBlock *tb;
    tb = ctx->tb;
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
        if (n == 0)
            gen_op_goto_tb0(TBPARAM(tb));
        else
            gen_op_goto_tb1(TBPARAM(tb));
2767 2768 2769 2770 2771 2772 2773
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
2774
        gen_op_set_T0((long)tb + n);
2775 2776
        if (ctx->singlestep_enabled)
            gen_op_debug();
2777 2778
        gen_op_exit_tb();
    } else {
2779 2780 2781 2782 2783 2784 2785
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
2786
        gen_op_reset_T0();
2787 2788
        if (ctx->singlestep_enabled)
            gen_op_debug();
2789 2790
        gen_op_exit_tb();
    }
B
bellard 已提交
2791 2792
}

2793
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
2794 2795 2796 2797 2798 2799 2800 2801 2802
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode != 0 && (nip >> 32))
        gen_op_setlr_64(ctx->nip >> 32, ctx->nip);
    else
#endif
        gen_op_setlr(ctx->nip);
}

B
bellard 已提交
2803 2804 2805
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
2806
    target_ulong li, target;
B
bellard 已提交
2807 2808

    /* sign extend LI */
2809
#if defined(TARGET_PPC64)
2810 2811 2812
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
2813
#endif
2814
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
2815
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
2816
        target = ctx->nip + li - 4;
B
bellard 已提交
2817
    else
2818
        target = li;
2819
#if defined(TARGET_PPC64)
2820 2821
    if (!ctx->sf_mode)
        target = (uint32_t)target;
2822
#endif
2823 2824
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
2825
    gen_goto_tb(ctx, 0, target);
2826
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
2827 2828
}

2829 2830 2831 2832
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

2833
static always_inline void gen_bcond (DisasContext *ctx, int type)
2834
{
2835 2836
    target_ulong target = 0;
    target_ulong li;
2837 2838 2839
    uint32_t bo = BO(ctx->opcode);
    uint32_t bi = BI(ctx->opcode);
    uint32_t mask;
2840 2841

    if ((bo & 0x4) == 0)
2842
        gen_op_dec_ctr();
2843 2844
    switch(type) {
    case BCOND_IM:
2845 2846
        li = (target_long)((int16_t)(BD(ctx->opcode)));
        if (likely(AA(ctx->opcode) == 0)) {
B
bellard 已提交
2847
            target = ctx->nip + li - 4;
2848 2849 2850
        } else {
            target = li;
        }
2851 2852 2853 2854
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode)
            target = (uint32_t)target;
#endif
2855 2856 2857 2858 2859 2860 2861 2862 2863
        break;
    case BCOND_CTR:
        gen_op_movl_T1_ctr();
        break;
    default:
    case BCOND_LR:
        gen_op_movl_T1_lr();
        break;
    }
2864 2865
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
2866
    if (bo & 0x10) {
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
        /* No CR condition */
        switch (bo & 0x6) {
        case 0:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctr_64();
            else
#endif
                gen_op_test_ctr();
            break;
        case 2:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctrz_64();
            else
#endif
                gen_op_test_ctrz();
2884 2885
            break;
        default:
2886 2887
        case 4:
        case 6:
2888
            if (type == BCOND_IM) {
2889
                gen_goto_tb(ctx, 0, target);
2890
                goto out;
2891
            } else {
2892 2893 2894 2895 2896 2897
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_b_T1_64();
                else
#endif
                    gen_op_b_T1();
2898
                gen_op_reset_T0();
2899
                goto no_test;
2900
            }
2901
            break;
2902
        }
2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
    } else {
        mask = 1 << (3 - (bi & 0x03));
        gen_op_load_crf_T0(bi >> 2);
        if (bo & 0x8) {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_true_64(mask);
                else
#endif
                    gen_op_test_ctr_true(mask);
                break;
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_true_64(mask);
                else
#endif
                    gen_op_test_ctrz_true(mask);
                break;
            default:
            case 4:
            case 6:
2927
                gen_op_test_true(mask);
2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
                break;
            }
        } else {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_false_64(mask);
                else
#endif
                    gen_op_test_ctr_false(mask);
2939
                break;
2940 2941 2942 2943 2944 2945 2946 2947
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_false_64(mask);
                else
#endif
                    gen_op_test_ctrz_false(mask);
                break;
2948
            default:
2949 2950
            case 4:
            case 6:
2951
                gen_op_test_false(mask);
2952 2953 2954 2955
                break;
            }
        }
    }
2956
    if (type == BCOND_IM) {
B
bellard 已提交
2957 2958
        int l1 = gen_new_label();
        gen_op_jz_T0(l1);
2959
        gen_goto_tb(ctx, 0, target);
B
bellard 已提交
2960
        gen_set_label(l1);
2961
        gen_goto_tb(ctx, 1, ctx->nip);
2962
    } else {
2963 2964 2965 2966 2967 2968
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_btest_T1_64(ctx->nip >> 32, ctx->nip);
        else
#endif
            gen_op_btest_T1(ctx->nip);
2969
        gen_op_reset_T0();
2970
    no_test:
J
j_mayer 已提交
2971 2972 2973 2974
        if (ctx->singlestep_enabled)
            gen_op_debug();
        gen_op_exit_tb();
    }
2975
 out:
2976
    ctx->exception = POWERPC_EXCP_BRANCH;
2977 2978 2979
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
2980
{
2981 2982 2983 2984
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
2985
{
2986 2987 2988 2989
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
2990
{
2991 2992
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
2993 2994 2995 2996 2997

/***                      Condition register logical                       ***/
#define GEN_CRLOGIC(op, opc)                                                  \
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
{                                                                             \
2998 2999
    uint8_t bitmask;                                                          \
    int sh;                                                                   \
B
bellard 已提交
3000
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
3001 3002 3003 3004 3005
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
        gen_op_srli_T0(sh);                                                   \
    else if (sh < 0)                                                          \
        gen_op_sli_T0(-sh);                                                   \
B
bellard 已提交
3006
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
3007 3008 3009 3010 3011
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
    if (sh > 0)                                                               \
        gen_op_srli_T1(sh);                                                   \
    else if (sh < 0)                                                          \
        gen_op_sli_T1(-sh);                                                   \
B
bellard 已提交
3012
    gen_op_##op();                                                            \
3013 3014
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
    gen_op_andi_T0(bitmask);                                                  \
B
bellard 已提交
3015
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
3016 3017 3018
    gen_op_andi_T1(~bitmask);                                                 \
    gen_op_or();                                                              \
    gen_op_store_T0_crf(crbD(ctx->opcode) >> 2);                              \
B
bellard 已提交
3019 3020 3021
}

/* crand */
3022
GEN_CRLOGIC(and, 0x08);
B
bellard 已提交
3023
/* crandc */
3024
GEN_CRLOGIC(andc, 0x04);
B
bellard 已提交
3025
/* creqv */
3026
GEN_CRLOGIC(eqv, 0x09);
B
bellard 已提交
3027
/* crnand */
3028
GEN_CRLOGIC(nand, 0x07);
B
bellard 已提交
3029
/* crnor */
3030
GEN_CRLOGIC(nor, 0x01);
B
bellard 已提交
3031
/* cror */
3032
GEN_CRLOGIC(or, 0x0E);
B
bellard 已提交
3033
/* crorc */
3034
GEN_CRLOGIC(orc, 0x0D);
B
bellard 已提交
3035
/* crxor */
3036
GEN_CRLOGIC(xor, 0x06);
B
bellard 已提交
3037 3038 3039 3040 3041 3042 3043 3044 3045
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
    gen_op_load_crf_T0(crfS(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

/***                           System linkage                              ***/
/* rfi (supervisor only) */
3046
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3047
{
3048
#if defined(CONFIG_USER_ONLY)
3049
    GEN_EXCP_PRIVOPC(ctx);
3050 3051
#else
    /* Restore CPU state */
3052
    if (unlikely(!ctx->supervisor)) {
3053
        GEN_EXCP_PRIVOPC(ctx);
3054
        return;
3055
    }
3056
    gen_op_rfi();
3057
    GEN_SYNC(ctx);
3058
#endif
B
bellard 已提交
3059 3060
}

J
j_mayer 已提交
3061
#if defined(TARGET_PPC64)
3062
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3063 3064
{
#if defined(CONFIG_USER_ONLY)
3065
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3066 3067 3068
#else
    /* Restore CPU state */
    if (unlikely(!ctx->supervisor)) {
3069
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3070 3071
        return;
    }
3072
    gen_op_rfid();
3073
    GEN_SYNC(ctx);
J
j_mayer 已提交
3074 3075 3076
#endif
}

3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64B)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    /* Restore CPU state */
    if (unlikely(ctx->supervisor <= 1)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_hrfid();
    GEN_SYNC(ctx);
#endif
}
#endif

B
bellard 已提交
3093
/* sc */
3094 3095 3096 3097 3098
#if defined(CONFIG_USER_ONLY)
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
#else
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
#endif
3099
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3100
{
3101 3102 3103
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
3104
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
B
bellard 已提交
3105 3106 3107 3108
}

/***                                Trap                                   ***/
/* tw */
3109
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3110
{
3111 3112
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3113
    /* Update the nip since this might generate a trap exception */
3114
    gen_update_nip(ctx, ctx->nip);
3115
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3116 3117 3118 3119 3120
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3121
    gen_op_load_gpr_T0(rA(ctx->opcode));
3122 3123 3124
    gen_set_T1(SIMM(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3125
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3126 3127
}

3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
    gen_op_td(TO(ctx->opcode));
}

/* tdi */
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_set_T1(SIMM(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
    gen_op_td(TO(ctx->opcode));
}
#endif

B
bellard 已提交
3150 3151 3152 3153 3154 3155
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
    gen_op_load_xer_cr();
    gen_op_store_T0_crf(crfD(ctx->opcode));
J
j_mayer 已提交
3156 3157
    gen_op_clear_xer_ov();
    gen_op_clear_xer_ca();
B
bellard 已提交
3158 3159 3160
}

/* mfcr */
3161
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3162
{
3163
    uint32_t crm, crn;
3164

3165 3166 3167 3168 3169 3170
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
            gen_op_load_cro(7 - crn);
        }
3171 3172 3173
    } else {
        gen_op_load_cr();
    }
B
bellard 已提交
3174 3175 3176 3177 3178 3179
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3180
#if defined(CONFIG_USER_ONLY)
3181
    GEN_EXCP_PRIVREG(ctx);
3182
#else
3183
    if (unlikely(!ctx->supervisor)) {
3184
        GEN_EXCP_PRIVREG(ctx);
3185
        return;
3186
    }
B
bellard 已提交
3187 3188
    gen_op_load_msr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
3189
#endif
B
bellard 已提交
3190 3191
}

J
j_mayer 已提交
3192
#if 1
3193
#define SPR_NOACCESS ((void *)(-1UL))
3194 3195 3196 3197 3198 3199 3200 3201 3202
#else
static void spr_noaccess (void *opaque, int sprn)
{
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
    printf("ERROR: try to access SPR %d !\n", sprn);
}
#define SPR_NOACCESS (&spr_noaccess)
#endif

B
bellard 已提交
3203
/* mfspr */
3204
static always_inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3205
{
3206
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
3207 3208
    uint32_t sprn = SPR(ctx->opcode);

3209
#if !defined(CONFIG_USER_ONLY)
3210 3211
    if (ctx->supervisor == 2)
        read_cb = ctx->spr_cb[sprn].hea_read;
3212
    else if (ctx->supervisor)
3213 3214
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3215
#endif
3216
        read_cb = ctx->spr_cb[sprn].uea_read;
3217 3218
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
3219 3220 3221 3222
            (*read_cb)(ctx, sprn);
            gen_op_store_T0_gpr(rD(ctx->opcode));
        } else {
            /* Privilege exception */
3223 3224 3225 3226 3227 3228
            /* This is a hack to avoid warnings when running Linux:
             * this OS breaks the PowerPC virtualisation model,
             * allowing userland application to read the PVR
             */
            if (sprn != SPR_PVR) {
                if (loglevel != 0) {
J
j_mayer 已提交
3229 3230
                    fprintf(logfile, "Trying to read privileged spr %d %03x at"
                            ADDRX "\n", sprn, sprn, ctx->nip);
3231
                }
J
j_mayer 已提交
3232 3233
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
                       sprn, sprn, ctx->nip);
3234
            }
3235
            GEN_EXCP_PRIVREG(ctx);
B
bellard 已提交
3236
        }
3237 3238
    } else {
        /* Not defined */
J
j_mayer 已提交
3239
        if (loglevel != 0) {
J
j_mayer 已提交
3240 3241
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
3242
        }
J
j_mayer 已提交
3243 3244
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
3245 3246
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3247 3248 3249
    }
}

3250
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3251
{
3252
    gen_op_mfspr(ctx);
3253
}
3254 3255

/* mftb */
3256
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3257 3258
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3259 3260 3261
}

/* mtcrf */
3262
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3263
{
3264
    uint32_t crm, crn;
3265

B
bellard 已提交
3266
    gen_op_load_gpr_T0(rS(ctx->opcode));
3267 3268 3269 3270 3271 3272 3273 3274 3275
    crm = CRM(ctx->opcode);
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
        crn = ffs(crm);
        gen_op_srli_T0(crn * 4);
        gen_op_andi_T0(0xF);
        gen_op_store_cro(7 - crn);
    } else {
        gen_op_store_cr(crm);
    }
B
bellard 已提交
3276 3277 3278
}

/* mtmsr */
J
j_mayer 已提交
3279
#if defined(TARGET_PPC64)
3280
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3281 3282
{
#if defined(CONFIG_USER_ONLY)
3283
    GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3284 3285
#else
    if (unlikely(!ctx->supervisor)) {
3286
        GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3287 3288 3289
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
3290 3291 3292 3293
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3294 3295 3296 3297
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
3298 3299 3300 3301
        gen_update_nip(ctx, ctx->nip);
        gen_op_store_msr();
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
3302
        ctx->exception = POWERPC_EXCP_STOP;
3303
    }
J
j_mayer 已提交
3304 3305 3306 3307
#endif
}
#endif

B
bellard 已提交
3308 3309
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3310
#if defined(CONFIG_USER_ONLY)
3311
    GEN_EXCP_PRIVREG(ctx);
3312
#else
3313
    if (unlikely(!ctx->supervisor)) {
3314
        GEN_EXCP_PRIVREG(ctx);
3315
        return;
3316
    }
B
bellard 已提交
3317
    gen_op_load_gpr_T0(rS(ctx->opcode));
3318 3319 3320 3321
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3322 3323 3324 3325
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
3326
        gen_update_nip(ctx, ctx->nip);
3327
#if defined(TARGET_PPC64)
3328 3329 3330
        if (!ctx->sf_mode)
            gen_op_store_msr_32();
        else
3331
#endif
3332 3333 3334
            gen_op_store_msr();
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsrd is not always defined as context-synchronizing */
3335
        ctx->exception = POWERPC_EXCP_STOP;
3336
    }
3337
#endif
B
bellard 已提交
3338 3339 3340 3341 3342
}

/* mtspr */
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
{
3343
    void (*write_cb)(void *opaque, int sprn);
B
bellard 已提交
3344 3345
    uint32_t sprn = SPR(ctx->opcode);

3346
#if !defined(CONFIG_USER_ONLY)
3347 3348
    if (ctx->supervisor == 2)
        write_cb = ctx->spr_cb[sprn].hea_write;
3349
    else if (ctx->supervisor)
3350 3351
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
3352
#endif
3353
        write_cb = ctx->spr_cb[sprn].uea_write;
3354 3355
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
3356 3357 3358 3359
            gen_op_load_gpr_T0(rS(ctx->opcode));
            (*write_cb)(ctx, sprn);
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3360
            if (loglevel != 0) {
J
j_mayer 已提交
3361 3362
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
                        ADDRX "\n", sprn, sprn, ctx->nip);
3363
            }
J
j_mayer 已提交
3364 3365
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
                   sprn, sprn, ctx->nip);
3366
            GEN_EXCP_PRIVREG(ctx);
3367
        }
3368 3369
    } else {
        /* Not defined */
J
j_mayer 已提交
3370
        if (loglevel != 0) {
J
j_mayer 已提交
3371 3372
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
                    ADDRX "\n", sprn, sprn, ctx->nip);
3373
        }
J
j_mayer 已提交
3374 3375
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
               sprn, sprn, ctx->nip);
3376 3377
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3378 3379 3380 3381 3382
    }
}

/***                         Cache management                              ***/
/* dcbf */
3383
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
3384
{
J
j_mayer 已提交
3385
    /* XXX: specification says this is treated as a load by the MMU */
3386
    gen_addr_reg_index(ctx);
3387
    op_ldst(lbz);
B
bellard 已提交
3388 3389 3390
}

/* dcbi (Supervisor only) */
3391
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3392
{
3393
#if defined(CONFIG_USER_ONLY)
3394
    GEN_EXCP_PRIVOPC(ctx);
3395
#else
3396
    if (unlikely(!ctx->supervisor)) {
3397
        GEN_EXCP_PRIVOPC(ctx);
3398
        return;
3399
    }
3400 3401
    gen_addr_reg_index(ctx);
    /* XXX: specification says this should be treated as a store by the MMU */
J
j_mayer 已提交
3402
    op_ldst(lbz);
3403 3404
    op_ldst(stb);
#endif
B
bellard 已提交
3405 3406 3407
}

/* dcdst */
3408
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3409
{
3410 3411
    /* XXX: specification say this is treated as a load by the MMU */
    gen_addr_reg_index(ctx);
3412
    op_ldst(lbz);
B
bellard 已提交
3413 3414 3415
}

/* dcbt */
3416
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
3417
{
3418
    /* interpreted as no-op */
3419 3420 3421
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3422 3423 3424
}

/* dcbtst */
3425
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
B
bellard 已提交
3426
{
3427
    /* interpreted as no-op */
3428 3429 3430
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3431 3432 3433
}

/* dcbz */
3434
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
3435 3436
static GenOpFunc *gen_op_dcbz[4][NB_MEM_FUNCS] = {
    /* 32 bytes cache line size */
3437
    {
3438 3439 3440 3441 3442 3443 3444 3445 3446
#define gen_op_dcbz_l32_le_raw        gen_op_dcbz_l32_raw
#define gen_op_dcbz_l32_le_user       gen_op_dcbz_l32_user
#define gen_op_dcbz_l32_le_kernel     gen_op_dcbz_l32_kernel
#define gen_op_dcbz_l32_le_hypv       gen_op_dcbz_l32_hypv
#define gen_op_dcbz_l32_le_64_raw     gen_op_dcbz_l32_64_raw
#define gen_op_dcbz_l32_le_64_user    gen_op_dcbz_l32_64_user
#define gen_op_dcbz_l32_le_64_kernel  gen_op_dcbz_l32_64_kernel
#define gen_op_dcbz_l32_le_64_hypv    gen_op_dcbz_l32_64_hypv
        GEN_MEM_FUNCS(dcbz_l32),
3447
    },
3448
    /* 64 bytes cache line size */
3449
    {
3450 3451 3452 3453 3454 3455 3456 3457 3458
#define gen_op_dcbz_l64_le_raw        gen_op_dcbz_l64_raw
#define gen_op_dcbz_l64_le_user       gen_op_dcbz_l64_user
#define gen_op_dcbz_l64_le_kernel     gen_op_dcbz_l64_kernel
#define gen_op_dcbz_l64_le_hypv       gen_op_dcbz_l64_hypv
#define gen_op_dcbz_l64_le_64_raw     gen_op_dcbz_l64_64_raw
#define gen_op_dcbz_l64_le_64_user    gen_op_dcbz_l64_64_user
#define gen_op_dcbz_l64_le_64_kernel  gen_op_dcbz_l64_64_kernel
#define gen_op_dcbz_l64_le_64_hypv    gen_op_dcbz_l64_64_hypv
        GEN_MEM_FUNCS(dcbz_l64),
3459
    },
3460
    /* 128 bytes cache line size */
3461
    {
3462 3463 3464 3465 3466 3467 3468 3469 3470
#define gen_op_dcbz_l128_le_raw       gen_op_dcbz_l128_raw
#define gen_op_dcbz_l128_le_user      gen_op_dcbz_l128_user
#define gen_op_dcbz_l128_le_kernel    gen_op_dcbz_l128_kernel
#define gen_op_dcbz_l128_le_hypv      gen_op_dcbz_l128_hypv
#define gen_op_dcbz_l128_le_64_raw    gen_op_dcbz_l128_64_raw
#define gen_op_dcbz_l128_le_64_user   gen_op_dcbz_l128_64_user
#define gen_op_dcbz_l128_le_64_kernel gen_op_dcbz_l128_64_kernel
#define gen_op_dcbz_l128_le_64_hypv   gen_op_dcbz_l128_64_hypv
        GEN_MEM_FUNCS(dcbz_l128),
3471
    },
3472
    /* tunable cache line size */
3473
    {
3474 3475 3476 3477 3478 3479 3480 3481 3482
#define gen_op_dcbz_le_raw            gen_op_dcbz_raw
#define gen_op_dcbz_le_user           gen_op_dcbz_user
#define gen_op_dcbz_le_kernel         gen_op_dcbz_kernel
#define gen_op_dcbz_le_hypv           gen_op_dcbz_hypv
#define gen_op_dcbz_le_64_raw         gen_op_dcbz_64_raw
#define gen_op_dcbz_le_64_user        gen_op_dcbz_64_user
#define gen_op_dcbz_le_64_kernel      gen_op_dcbz_64_kernel
#define gen_op_dcbz_le_64_hypv        gen_op_dcbz_64_hypv
        GEN_MEM_FUNCS(dcbz),
3483
    },
3484
};
3485

3486 3487
static always_inline void handler_dcbz (DisasContext *ctx,
                                        int dcache_line_size)
3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
{
    int n;

    switch (dcache_line_size) {
    case 32:
        n = 0;
        break;
    case 64:
        n = 1;
        break;
    case 128:
        n = 2;
        break;
    default:
        n = 3;
        break;
    }
    op_dcbz(n);
}

GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
B
bellard 已提交
3509
{
3510
    gen_addr_reg_index(ctx);
3511 3512 3513 3514
    handler_dcbz(ctx, ctx->dcache_line_size);
    gen_op_check_reservation();
}

3515
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
3516 3517 3518 3519 3520 3521
{
    gen_addr_reg_index(ctx);
    if (ctx->opcode & 0x00200000)
        handler_dcbz(ctx, ctx->dcache_line_size);
    else
        handler_dcbz(ctx, -1);
B
bellard 已提交
3522
    gen_op_check_reservation();
B
bellard 已提交
3523 3524 3525
}

/* icbi */
3526
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
3527 3528 3529 3530 3531 3532 3533 3534 3535 3536
#define gen_op_icbi_le_raw       gen_op_icbi_raw
#define gen_op_icbi_le_user      gen_op_icbi_user
#define gen_op_icbi_le_kernel    gen_op_icbi_kernel
#define gen_op_icbi_le_hypv      gen_op_icbi_hypv
#define gen_op_icbi_le_64_raw    gen_op_icbi_64_raw
#define gen_op_icbi_le_64_user   gen_op_icbi_64_user
#define gen_op_icbi_le_64_kernel gen_op_icbi_64_kernel
#define gen_op_icbi_le_64_hypv   gen_op_icbi_64_hypv
static GenOpFunc *gen_op_icbi[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(icbi),
3537
};
3538

3539
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
B
bellard 已提交
3540
{
3541 3542
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3543
    gen_addr_reg_index(ctx);
3544
    op_icbi();
B
bellard 已提交
3545 3546 3547 3548
}

/* Optional: */
/* dcba */
3549
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
3550
{
3551 3552 3553 3554
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3555 3556 3557 3558 3559 3560 3561
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
3562
#if defined(CONFIG_USER_ONLY)
3563
    GEN_EXCP_PRIVREG(ctx);
3564
#else
3565
    if (unlikely(!ctx->supervisor)) {
3566
        GEN_EXCP_PRIVREG(ctx);
3567
        return;
3568
    }
3569 3570
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_load_sr();
3571 3572
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3573 3574 3575
}

/* mfsrin */
3576
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3577
{
3578
#if defined(CONFIG_USER_ONLY)
3579
    GEN_EXCP_PRIVREG(ctx);
3580
#else
3581
    if (unlikely(!ctx->supervisor)) {
3582
        GEN_EXCP_PRIVREG(ctx);
3583
        return;
3584 3585
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
3586 3587
    gen_op_srli_T1(28);
    gen_op_load_sr();
3588 3589
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3590 3591 3592
}

/* mtsr */
B
bellard 已提交
3593
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
3594
{
3595
#if defined(CONFIG_USER_ONLY)
3596
    GEN_EXCP_PRIVREG(ctx);
3597
#else
3598
    if (unlikely(!ctx->supervisor)) {
3599
        GEN_EXCP_PRIVREG(ctx);
3600
        return;
3601 3602
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
3603 3604
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_store_sr();
3605
#endif
B
bellard 已提交
3606 3607 3608
}

/* mtsrin */
3609
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3610
{
3611
#if defined(CONFIG_USER_ONLY)
3612
    GEN_EXCP_PRIVREG(ctx);
3613
#else
3614
    if (unlikely(!ctx->supervisor)) {
3615
        GEN_EXCP_PRIVREG(ctx);
3616
        return;
3617 3618 3619
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3620 3621
    gen_op_srli_T1(28);
    gen_op_store_sr();
3622
#endif
B
bellard 已提交
3623 3624
}

3625 3626 3627
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
3628
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_load_slb();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mfsrin */
3644 3645
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
             PPC_SEGMENT_64B)
3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_srli_T1(28);
    gen_op_load_slb();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mtsr */
3662
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_store_slb();
#endif
}

/* mtsrin */
3678 3679
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
             PPC_SEGMENT_64B)
3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_srli_T1(28);
    gen_op_store_slb();
#endif
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
3696 3697 3698
/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
3699
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
3700
{
3701
#if defined(CONFIG_USER_ONLY)
3702
    GEN_EXCP_PRIVOPC(ctx);
3703
#else
3704
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
3705
        if (loglevel != 0)
3706
            fprintf(logfile, "%s: ! supervisor\n", __func__);
3707
        GEN_EXCP_PRIVOPC(ctx);
3708
        return;
3709 3710 3711
    }
    gen_op_tlbia();
#endif
B
bellard 已提交
3712 3713 3714
}

/* tlbie */
3715
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
3716
{
3717
#if defined(CONFIG_USER_ONLY)
3718
    GEN_EXCP_PRIVOPC(ctx);
3719
#else
3720
    if (unlikely(!ctx->supervisor)) {
3721
        GEN_EXCP_PRIVOPC(ctx);
3722
        return;
3723 3724
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
3725 3726 3727 3728 3729 3730
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
3731
#endif
B
bellard 已提交
3732 3733 3734
}

/* tlbsync */
3735
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
3736
{
3737
#if defined(CONFIG_USER_ONLY)
3738
    GEN_EXCP_PRIVOPC(ctx);
3739
#else
3740
    if (unlikely(!ctx->supervisor)) {
3741
        GEN_EXCP_PRIVOPC(ctx);
3742
        return;
3743 3744 3745 3746
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
3747
    GEN_STOP(ctx);
3748
#endif
B
bellard 已提交
3749 3750
}

J
j_mayer 已提交
3751 3752 3753 3754 3755
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
3756
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3757 3758
#else
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
3759
        if (loglevel != 0)
J
j_mayer 已提交
3760
            fprintf(logfile, "%s: ! supervisor\n", __func__);
3761
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3762 3763 3764 3765 3766 3767 3768 3769 3770 3771
        return;
    }
    gen_op_slbia();
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
3772
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3773 3774
#else
    if (unlikely(!ctx->supervisor)) {
3775
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3776 3777 3778 3779 3780 3781 3782 3783
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_slbie();
#endif
}
#endif

B
bellard 已提交
3784 3785
/***                              External control                         ***/
/* Optional: */
3786 3787
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
3788 3789
static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(eciwx),
3790
};
3791 3792
static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(ecowx),
3793
};
3794

3795
/* eciwx */
B
bellard 已提交
3796 3797
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
3798
    /* Should check EAR[E] & alignment ! */
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834
    gen_addr_reg_index(ctx);
    op_eciwx();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* ecowx */
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
{
    /* Should check EAR[E] & alignment ! */
    gen_addr_reg_index(ctx);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_ecowx();
}

/* PowerPC 601 specific instructions */
/* abs - abs. */
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_abs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* abso - abso. */
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_abso();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* clcs */
3835
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
3836 3837 3838
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_clcs();
3839
    /* Rc=1 sets CR0 to an undefined state */
3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* div - div. */
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_div();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divo - divo. */
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_divo();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divs - divs. */
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_divs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* divso - divso. */
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_divso();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* doz - doz. */
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_doz();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* dozo - dozo. */
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_dozo();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* dozi */
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_set_T1(SIMM(ctx->opcode));
    gen_op_POWER_doz();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

3918 3919 3920
/* As lscbx load from memory byte after byte, it's always endian safe.
 * Original POWER is 32 bits only, define 64 bits ops as 32 bits ones
 */
3921
#define op_POWER_lscbx(start, ra, rb)                                         \
3922
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936
#define gen_op_POWER_lscbx_64_raw       gen_op_POWER_lscbx_raw
#define gen_op_POWER_lscbx_64_user      gen_op_POWER_lscbx_user
#define gen_op_POWER_lscbx_64_kernel    gen_op_POWER_lscbx_kernel
#define gen_op_POWER_lscbx_64_hypv      gen_op_POWER_lscbx_hypv
#define gen_op_POWER_lscbx_le_raw       gen_op_POWER_lscbx_raw
#define gen_op_POWER_lscbx_le_user      gen_op_POWER_lscbx_user
#define gen_op_POWER_lscbx_le_kernel    gen_op_POWER_lscbx_kernel
#define gen_op_POWER_lscbx_le_hypv      gen_op_POWER_lscbx_hypv
#define gen_op_POWER_lscbx_le_64_raw    gen_op_POWER_lscbx_raw
#define gen_op_POWER_lscbx_le_64_user   gen_op_POWER_lscbx_user
#define gen_op_POWER_lscbx_le_64_kernel gen_op_POWER_lscbx_kernel
#define gen_op_POWER_lscbx_le_64_hypv   gen_op_POWER_lscbx_hypv
static GenOpFunc3 *gen_op_POWER_lscbx[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(POWER_lscbx),
3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949
};

/* lscbx - lscbx. */
GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR)
{
    int ra = rA(ctx->opcode);
    int rb = rB(ctx->opcode);

    gen_addr_reg_index(ctx);
    if (ra == 0) {
        ra = rb;
    }
    /* NIP cannot be restored if the memory exception comes from an helper */
3950
    gen_update_nip(ctx, ctx->nip - 4);
3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117
    gen_op_load_xer_bc();
    gen_op_load_xer_cmp();
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
    gen_op_store_xer_bc();
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* maskg - maskg. */
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_maskg();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* maskir - maskir. */
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_load_gpr_T2(rB(ctx->opcode));
    gen_op_POWER_maskir();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* mul - mul. */
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_mul();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* mulo - mulo. */
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_mulo();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* nabs - nabs. */
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_nabs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* nabso - nabso. */
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_nabso();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* rlmi - rlmi. */
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
{
    uint32_t mb, me;

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rA(ctx->opcode));
    gen_op_load_gpr_T2(rB(ctx->opcode));
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* rrib - rrib. */
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rA(ctx->opcode));
    gen_op_load_gpr_T2(rB(ctx->opcode));
    gen_op_POWER_rrib();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sle - sle. */
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sle();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sleq - sleq. */
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sleq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sliq - sliq. */
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_sle();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* slliq - slliq. */
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_sleq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sllq - sllq. */
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sllq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* slq - slq. */
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_slq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

4118
/* sraiq - sraiq. */
4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_sraq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sraq - sraq. */
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sraq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sre - sre. */
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sre();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srea - srea. */
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_srea();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sreq */
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_sreq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* sriq */
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_srq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srliq */
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_srlq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srlq */
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_srlq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* srq */
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_POWER_srq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4223
    GEN_EXCP_INVAL(ctx);
4224 4225 4226 4227 4228 4229
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4230
    GEN_EXCP_INVAL(ctx);
4231 4232 4233 4234 4235 4236
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
4237
    GEN_EXCP_PRIVOPC(ctx);
4238 4239
#else
    if (unlikely(!ctx->supervisor)) {
4240
        GEN_EXCP_PRIVOPC(ctx);
4241 4242 4243 4244 4245 4246 4247 4248 4249 4250
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_602_mfrom();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
4251
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4252 4253
{
#if defined(CONFIG_USER_ONLY)
4254
    GEN_EXCP_PRIVOPC(ctx);
4255 4256
#else
    if (unlikely(!ctx->supervisor)) {
4257
        GEN_EXCP_PRIVOPC(ctx);
4258 4259 4260 4261 4262 4263 4264 4265
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbld();
#endif
}

/* tlbli */
4266
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4267 4268
{
#if defined(CONFIG_USER_ONLY)
4269
    GEN_EXCP_PRIVOPC(ctx);
4270 4271
#else
    if (unlikely(!ctx->supervisor)) {
4272
        GEN_EXCP_PRIVOPC(ctx);
4273 4274 4275 4276 4277 4278 4279
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbli();
#endif
}

4280 4281
/* 74xx TLB management */
/* tlbld */
4282
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_74xx_tlbld();
#endif
}

/* tlbli */
4297
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_74xx_tlbli();
#endif
}

4311 4312 4313 4314 4315 4316 4317 4318 4319 4320
/* POWER instructions not in PowerPC 601 */
/* clf */
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
{
    /* Cache line flush: implemented as no-op */
}

/* cli */
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
{
B
blueswir1 已提交
4321
    /* Cache line invalidate: privileged and treated as no-op */
4322
#if defined(CONFIG_USER_ONLY)
4323
    GEN_EXCP_PRIVOPC(ctx);
4324 4325
#else
    if (unlikely(!ctx->supervisor)) {
4326
        GEN_EXCP_PRIVOPC(ctx);
4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340
        return;
    }
#endif
}

/* dclst */
GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER)
{
    /* Data cache line store: treated as no-op */
}

GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4341
    GEN_EXCP_PRIVOPC(ctx);
4342 4343
#else
    if (unlikely(!ctx->supervisor)) {
4344
        GEN_EXCP_PRIVOPC(ctx);
4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360
        return;
    }
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);

    gen_addr_reg_index(ctx);
    gen_op_POWER_mfsri();
    gen_op_store_T0_gpr(rd);
    if (ra != 0 && ra != rd)
        gen_op_store_T1_gpr(ra);
#endif
}

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4361
    GEN_EXCP_PRIVOPC(ctx);
4362 4363
#else
    if (unlikely(!ctx->supervisor)) {
4364
        GEN_EXCP_PRIVOPC(ctx);
4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375
        return;
    }
    gen_addr_reg_index(ctx);
    gen_op_POWER_rac();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4376
    GEN_EXCP_PRIVOPC(ctx);
4377 4378
#else
    if (unlikely(!ctx->supervisor)) {
4379
        GEN_EXCP_PRIVOPC(ctx);
4380 4381 4382
        return;
    }
    gen_op_POWER_rfsvc();
4383
    GEN_SYNC(ctx);
4384 4385 4386 4387 4388 4389 4390
#endif
}

/* svc is not implemented for now */

/* POWER2 specific instructions */
/* Quad manipulation (load/store two floats at a time) */
4391
/* Original POWER2 is 32 bits only, define 64 bits ops as 32 bits ones */
4392 4393
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411
#define gen_op_POWER2_lfq_64_raw        gen_op_POWER2_lfq_raw
#define gen_op_POWER2_lfq_64_user       gen_op_POWER2_lfq_user
#define gen_op_POWER2_lfq_64_kernel     gen_op_POWER2_lfq_kernel
#define gen_op_POWER2_lfq_64_hypv       gen_op_POWER2_lfq_hypv
#define gen_op_POWER2_lfq_le_64_raw     gen_op_POWER2_lfq_le_raw
#define gen_op_POWER2_lfq_le_64_user    gen_op_POWER2_lfq_le_user
#define gen_op_POWER2_lfq_le_64_kernel  gen_op_POWER2_lfq_le_kernel
#define gen_op_POWER2_lfq_le_64_hypv    gen_op_POWER2_lfq_le_hypv
#define gen_op_POWER2_stfq_64_raw       gen_op_POWER2_stfq_raw
#define gen_op_POWER2_stfq_64_user      gen_op_POWER2_stfq_user
#define gen_op_POWER2_stfq_64_kernel    gen_op_POWER2_stfq_kernel
#define gen_op_POWER2_stfq_64_hypv      gen_op_POWER2_stfq_hypv
#define gen_op_POWER2_stfq_le_64_raw    gen_op_POWER2_stfq_le_raw
#define gen_op_POWER2_stfq_le_64_user   gen_op_POWER2_stfq_le_user
#define gen_op_POWER2_stfq_le_64_kernel gen_op_POWER2_stfq_le_kernel
#define gen_op_POWER2_stfq_le_64_hypv   gen_op_POWER2_stfq_le_hypv
static GenOpFunc *gen_op_POWER2_lfq[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(POWER2_lfq),
4412
};
4413 4414
static GenOpFunc *gen_op_POWER2_stfq[NB_MEM_FUNCS] = {
    GEN_MEM_FUNCS(POWER2_stfq),
4415 4416 4417 4418 4419 4420
};

/* lfq */
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4421
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4422
    gen_addr_imm_index(ctx, 0);
4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
}

/* lfqu */
GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4434
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4435
    gen_addr_imm_index(ctx, 0);
4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* lfqux */
GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4449
    gen_update_nip(ctx, ctx->nip - 4);
4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461
    gen_addr_reg_index(ctx);
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4462
    gen_update_nip(ctx, ctx->nip - 4);
4463 4464 4465 4466 4467 4468 4469 4470 4471 4472
    gen_addr_reg_index(ctx);
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4473
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4474
    gen_addr_imm_index(ctx, 0);
4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
}

/* stfqu */
GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4486
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4487
    gen_addr_imm_index(ctx, 0);
4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* stfqux */
GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2)
{
    int ra = rA(ctx->opcode);

    /* NIP cannot be restored if the memory exception comes from an helper */
4501
    gen_update_nip(ctx, ctx->nip - 4);
4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513
    gen_addr_reg_index(ctx);
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4514
    gen_update_nip(ctx, ctx->nip - 4);
4515 4516 4517 4518 4519 4520 4521
    gen_addr_reg_index(ctx);
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
}

/* BookE specific instructions */
4522
/* XXX: not implemented on 440 ? */
4523
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_BOOKE_EXT)
4524 4525
{
    /* XXX: TODO */
4526
    GEN_EXCP_INVAL(ctx);
4527 4528
}

4529
/* XXX: not implemented on 440 ? */
4530
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_BOOKE_EXT)
4531 4532
{
#if defined(CONFIG_USER_ONLY)
4533
    GEN_EXCP_PRIVOPC(ctx);
4534 4535
#else
    if (unlikely(!ctx->supervisor)) {
4536
        GEN_EXCP_PRIVOPC(ctx);
4537 4538 4539 4540
        return;
    }
    gen_addr_reg_index(ctx);
    /* Use the same micro-ops as for tlbie */
4541 4542 4543 4544 4545 4546
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4547 4548 4549 4550
#endif
}

/* All 405 MAC instructions are translated here */
4551 4552 4553
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
                                                int opc2, int opc3,
                                                int ra, int rb, int rt, int Rc)
4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613
{
    gen_op_load_gpr_T0(ra);
    gen_op_load_gpr_T1(rb);
    switch (opc3 & 0x0D) {
    case 0x05:
        /* macchw    - macchw.    - macchwo   - macchwo.   */
        /* macchws   - macchws.   - macchwso  - macchwso.  */
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
        /* mulchw - mulchw. */
        gen_op_405_mulchw();
        break;
    case 0x04:
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
        /* mulchwu - mulchwu. */
        gen_op_405_mulchwu();
        break;
    case 0x01:
        /* machhw    - machhw.    - machhwo   - machhwo.   */
        /* machhws   - machhws.   - machhwso  - machhwso.  */
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
        /* mulhhw - mulhhw. */
        gen_op_405_mulhhw();
        break;
    case 0x00:
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
        /* mulhhwu - mulhhwu. */
        gen_op_405_mulhhwu();
        break;
    case 0x0D:
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
        /* mullhw - mullhw. */
        gen_op_405_mullhw();
        break;
    case 0x0C:
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
        /* mullhwu - mullhwu. */
        gen_op_405_mullhwu();
        break;
    }
    if (opc2 & 0x02) {
        /* nmultiply-and-accumulate (0x0E) */
        gen_op_neg();
    }
    if (opc2 & 0x04) {
        /* (n)multiply-and-accumulate (0x0C - 0x0E) */
        gen_op_load_gpr_T2(rt);
        gen_op_move_T1_T0();
        gen_op_405_add_T0_T2();
    }
    if (opc3 & 0x10) {
        /* Check overflow */
        if (opc3 & 0x01)
4614
            gen_op_check_addo();
4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631
        else
            gen_op_405_check_ovu();
    }
    if (opc3 & 0x02) {
        /* Saturate */
        if (opc3 & 0x01)
            gen_op_405_check_sat();
        else
            gen_op_405_check_satu();
    }
    gen_op_store_T0_gpr(rt);
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
        gen_set_Rc0(ctx);
    }
}

4632 4633
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
4634 4635 4636 4637 4638 4639
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
4640
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
4641
/* macchwo   - macchwo.   */
4642
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
4643
/* macchws   - macchws.   */
4644
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
4645
/* macchwso  - macchwso.  */
4646
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
4647
/* macchwsu  - macchwsu.  */
4648
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
4649
/* macchwsuo - macchwsuo. */
4650
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
4651
/* macchwu   - macchwu.   */
4652
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
4653
/* macchwuo  - macchwuo.  */
4654
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
4655
/* machhw    - machhw.    */
4656
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
4657
/* machhwo   - machhwo.   */
4658
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
4659
/* machhws   - machhws.   */
4660
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
4661
/* machhwso  - machhwso.  */
4662
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
4663
/* machhwsu  - machhwsu.  */
4664
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
4665
/* machhwsuo - machhwsuo. */
4666
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
4667
/* machhwu   - machhwu.   */
4668
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
4669
/* machhwuo  - machhwuo.  */
4670
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
4671
/* maclhw    - maclhw.    */
4672
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
4673
/* maclhwo   - maclhwo.   */
4674
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
4675
/* maclhws   - maclhws.   */
4676
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
4677
/* maclhwso  - maclhwso.  */
4678
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
4679
/* maclhwu   - maclhwu.   */
4680
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
4681
/* maclhwuo  - maclhwuo.  */
4682
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
4683
/* maclhwsu  - maclhwsu.  */
4684
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
4685
/* maclhwsuo - maclhwsuo. */
4686
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
4687
/* nmacchw   - nmacchw.   */
4688
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
4689
/* nmacchwo  - nmacchwo.  */
4690
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
4691
/* nmacchws  - nmacchws.  */
4692
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
4693
/* nmacchwso - nmacchwso. */
4694
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
4695
/* nmachhw   - nmachhw.   */
4696
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
4697
/* nmachhwo  - nmachhwo.  */
4698
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
4699
/* nmachhws  - nmachhws.  */
4700
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
4701
/* nmachhwso - nmachhwso. */
4702
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
4703
/* nmaclhw   - nmaclhw.   */
4704
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
4705
/* nmaclhwo  - nmaclhwo.  */
4706
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
4707
/* nmaclhws  - nmaclhws.  */
4708
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
4709
/* nmaclhwso - nmaclhwso. */
4710
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
4711 4712

/* mulchw  - mulchw.  */
4713
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
4714
/* mulchwu - mulchwu. */
4715
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
4716
/* mulhhw  - mulhhw.  */
4717
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
4718
/* mulhhwu - mulhhwu. */
4719
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
4720
/* mullhw  - mullhw.  */
4721
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
4722
/* mullhwu - mullhwu. */
4723
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
4724 4725 4726 4727 4728

/* mfdcr */
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4729
    GEN_EXCP_PRIVREG(ctx);
4730 4731 4732 4733
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
4734
        GEN_EXCP_PRIVREG(ctx);
4735 4736
        return;
    }
4737 4738
    gen_op_set_T0(dcrn);
    gen_op_load_dcr();
4739 4740 4741 4742 4743 4744 4745 4746
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mtdcr */
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4747
    GEN_EXCP_PRIVREG(ctx);
4748 4749 4750 4751
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
4752
        GEN_EXCP_PRIVREG(ctx);
4753 4754
        return;
    }
4755 4756 4757 4758 4759 4760 4761
    gen_op_set_T0(dcrn);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
#endif
}

/* mfdcrx */
4762
/* XXX: not implemented on 440 ? */
4763
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_BOOKE_EXT)
4764 4765
{
#if defined(CONFIG_USER_ONLY)
4766
    GEN_EXCP_PRIVREG(ctx);
4767 4768
#else
    if (unlikely(!ctx->supervisor)) {
4769
        GEN_EXCP_PRIVREG(ctx);
4770 4771 4772 4773 4774
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_dcr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
4775
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4776 4777 4778 4779
#endif
}

/* mtdcrx */
4780
/* XXX: not implemented on 440 ? */
4781
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_BOOKE_EXT)
4782 4783
{
#if defined(CONFIG_USER_ONLY)
4784
    GEN_EXCP_PRIVREG(ctx);
4785 4786
#else
    if (unlikely(!ctx->supervisor)) {
4787
        GEN_EXCP_PRIVREG(ctx);
4788 4789 4790 4791 4792
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
4793
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4794 4795 4796
#endif
}

4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_dcr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

/* mtdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

4815 4816 4817 4818
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4819
    GEN_EXCP_PRIVOPC(ctx);
4820 4821
#else
    if (unlikely(!ctx->supervisor)) {
4822
        GEN_EXCP_PRIVOPC(ctx);
4823 4824 4825 4826 4827 4828 4829 4830 4831 4832
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4833
    GEN_EXCP_PRIVOPC(ctx);
4834 4835
#else
    if (unlikely(!ctx->supervisor)) {
4836
        GEN_EXCP_PRIVOPC(ctx);
4837 4838 4839 4840 4841 4842 4843 4844 4845
        return;
    }
    gen_addr_reg_index(ctx);
    op_ldst(lwz);
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* icbt */
4846
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
}

/* iccci */
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4858
    GEN_EXCP_PRIVOPC(ctx);
4859 4860
#else
    if (unlikely(!ctx->supervisor)) {
4861
        GEN_EXCP_PRIVOPC(ctx);
4862 4863 4864 4865 4866 4867 4868 4869 4870 4871
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
4872
    GEN_EXCP_PRIVOPC(ctx);
4873 4874
#else
    if (unlikely(!ctx->supervisor)) {
4875
        GEN_EXCP_PRIVOPC(ctx);
4876 4877 4878 4879 4880 4881 4882
        return;
    }
    /* interpreted as no-op */
#endif
}

/* rfci (supervisor only) */
4883
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
4884 4885
{
#if defined(CONFIG_USER_ONLY)
4886
    GEN_EXCP_PRIVOPC(ctx);
4887 4888
#else
    if (unlikely(!ctx->supervisor)) {
4889
        GEN_EXCP_PRIVOPC(ctx);
4890 4891 4892 4893
        return;
    }
    /* Restore CPU state */
    gen_op_40x_rfci();
4894
    GEN_SYNC(ctx);
4895 4896 4897 4898 4899 4900
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
4901
    GEN_EXCP_PRIVOPC(ctx);
4902 4903
#else
    if (unlikely(!ctx->supervisor)) {
4904
        GEN_EXCP_PRIVOPC(ctx);
4905 4906 4907 4908
        return;
    }
    /* Restore CPU state */
    gen_op_rfci();
4909
    GEN_SYNC(ctx);
4910 4911 4912 4913
#endif
}

/* BookE specific */
4914
/* XXX: not implemented on 440 ? */
4915
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_BOOKE_EXT)
4916 4917
{
#if defined(CONFIG_USER_ONLY)
4918
    GEN_EXCP_PRIVOPC(ctx);
4919 4920
#else
    if (unlikely(!ctx->supervisor)) {
4921
        GEN_EXCP_PRIVOPC(ctx);
4922 4923 4924
        return;
    }
    /* Restore CPU state */
4925
    gen_op_rfdi();
4926
    GEN_SYNC(ctx);
4927 4928 4929
#endif
}

4930
/* XXX: not implemented on 440 ? */
4931
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
4932 4933
{
#if defined(CONFIG_USER_ONLY)
4934
    GEN_EXCP_PRIVOPC(ctx);
4935 4936
#else
    if (unlikely(!ctx->supervisor)) {
4937
        GEN_EXCP_PRIVOPC(ctx);
4938 4939 4940 4941
        return;
    }
    /* Restore CPU state */
    gen_op_rfmci();
4942
    GEN_SYNC(ctx);
4943 4944
#endif
}
4945

4946
/* TLB management - PowerPC 405 implementation */
4947
/* tlbre */
4948
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
4949 4950
{
#if defined(CONFIG_USER_ONLY)
4951
    GEN_EXCP_PRIVOPC(ctx);
4952 4953
#else
    if (unlikely(!ctx->supervisor)) {
4954
        GEN_EXCP_PRIVOPC(ctx);
4955 4956 4957 4958
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
4959
        gen_op_load_gpr_T0(rA(ctx->opcode));
4960 4961 4962 4963 4964 4965 4966 4967 4968
        gen_op_4xx_tlbre_hi();
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    case 1:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_4xx_tlbre_lo();
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    default:
4969
        GEN_EXCP_INVAL(ctx);
4970
        break;
4971
    }
4972 4973 4974
#endif
}

4975
/* tlbsx - tlbsx. */
4976
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
4977 4978
{
#if defined(CONFIG_USER_ONLY)
4979
    GEN_EXCP_PRIVOPC(ctx);
4980 4981
#else
    if (unlikely(!ctx->supervisor)) {
4982
        GEN_EXCP_PRIVOPC(ctx);
4983 4984 4985
        return;
    }
    gen_addr_reg_index(ctx);
4986
    gen_op_4xx_tlbsx();
4987
    if (Rc(ctx->opcode))
4988
        gen_op_4xx_tlbsx_check();
4989
    gen_op_store_T0_gpr(rD(ctx->opcode));
4990
#endif
B
bellard 已提交
4991 4992
}

4993
/* tlbwe */
4994
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
4995
{
4996
#if defined(CONFIG_USER_ONLY)
4997
    GEN_EXCP_PRIVOPC(ctx);
4998 4999
#else
    if (unlikely(!ctx->supervisor)) {
5000
        GEN_EXCP_PRIVOPC(ctx);
5001 5002 5003 5004
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5005
        gen_op_load_gpr_T0(rA(ctx->opcode));
5006 5007 5008 5009 5010 5011 5012 5013 5014
        gen_op_load_gpr_T1(rS(ctx->opcode));
        gen_op_4xx_tlbwe_hi();
        break;
    case 1:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rS(ctx->opcode));
        gen_op_4xx_tlbwe_lo();
        break;
    default:
5015
        GEN_EXCP_INVAL(ctx);
5016
        break;
5017
    }
5018 5019 5020
#endif
}

5021
/* TLB management - PowerPC 440 implementation */
5022
/* tlbre */
5023
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5024 5025
{
#if defined(CONFIG_USER_ONLY)
5026
    GEN_EXCP_PRIVOPC(ctx);
5027 5028
#else
    if (unlikely(!ctx->supervisor)) {
5029
        GEN_EXCP_PRIVOPC(ctx);
5030 5031 5032 5033 5034 5035 5036
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
        gen_op_load_gpr_T0(rA(ctx->opcode));
5037
        gen_op_440_tlbre(rB(ctx->opcode));
5038 5039 5040
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    default:
5041
        GEN_EXCP_INVAL(ctx);
5042 5043 5044 5045 5046 5047
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5048
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5049 5050
{
#if defined(CONFIG_USER_ONLY)
5051
    GEN_EXCP_PRIVOPC(ctx);
5052 5053
#else
    if (unlikely(!ctx->supervisor)) {
5054
        GEN_EXCP_PRIVOPC(ctx);
5055 5056 5057
        return;
    }
    gen_addr_reg_index(ctx);
5058
    gen_op_440_tlbsx();
5059
    if (Rc(ctx->opcode))
5060
        gen_op_4xx_tlbsx_check();
5061 5062 5063 5064 5065
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* tlbwe */
5066
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5067 5068
{
#if defined(CONFIG_USER_ONLY)
5069
    GEN_EXCP_PRIVOPC(ctx);
5070 5071
#else
    if (unlikely(!ctx->supervisor)) {
5072
        GEN_EXCP_PRIVOPC(ctx);
5073 5074 5075 5076 5077 5078 5079 5080
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rS(ctx->opcode));
5081
        gen_op_440_tlbwe(rB(ctx->opcode));
5082 5083
        break;
    default:
5084
        GEN_EXCP_INVAL(ctx);
5085 5086 5087 5088 5089
        break;
    }
#endif
}

5090 5091 5092 5093
/* wrtee */
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5094
    GEN_EXCP_PRIVOPC(ctx);
5095 5096
#else
    if (unlikely(!ctx->supervisor)) {
5097
        GEN_EXCP_PRIVOPC(ctx);
5098 5099 5100
        return;
    }
    gen_op_load_gpr_T0(rD(ctx->opcode));
5101
    gen_op_wrte();
J
j_mayer 已提交
5102 5103 5104
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5105
    GEN_STOP(ctx);
5106 5107 5108 5109 5110 5111 5112
#endif
}

/* wrteei */
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5113
    GEN_EXCP_PRIVOPC(ctx);
5114 5115
#else
    if (unlikely(!ctx->supervisor)) {
5116
        GEN_EXCP_PRIVOPC(ctx);
5117 5118 5119
        return;
    }
    gen_op_set_T0(ctx->opcode & 0x00010000);
5120
    gen_op_wrte();
J
j_mayer 已提交
5121 5122 5123
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5124
    GEN_STOP(ctx);
5125 5126 5127
#endif
}

J
j_mayer 已提交
5128
/* PowerPC 440 specific instructions */
5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_440_dlmzb();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    gen_op_store_xer_bc();
    if (Rc(ctx->opcode)) {
        gen_op_440_dlmzb_update_Rc();
        gen_op_store_T0_crf(0);
    }
}

/* mbar replaces eieio on 440 */
GEN_HANDLER(mbar, 0x1F, 0x16, 0x13, 0x001FF801, PPC_BOOKE)
{
    /* interpreted as no-op */
}

/* msync replaces sync on 440 */
5150
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5151 5152 5153 5154 5155
{
    /* interpreted as no-op */
}

/* icbt */
5156
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5157 5158 5159 5160 5161
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
5162 5163
}

5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177
/***                      Altivec vector extension                         ***/
/* Altivec registers moves */
GEN32(gen_op_load_avr_A0, gen_op_load_avr_A0_avr);
GEN32(gen_op_load_avr_A1, gen_op_load_avr_A1_avr);
GEN32(gen_op_load_avr_A2, gen_op_load_avr_A2_avr);

GEN32(gen_op_store_A0_avr, gen_op_store_A0_avr_avr);
GEN32(gen_op_store_A1_avr, gen_op_store_A1_avr_avr);
#if 0 // unused
GEN32(gen_op_store_A2_avr, gen_op_store_A2_avr_avr);
#endif

#define op_vr_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#define OP_VR_LD_TABLE(name)                                                  \
5178 5179
static GenOpFunc *gen_op_vr_l##name[NB_MEM_FUNCS] = {                         \
    GEN_MEM_FUNCS(vr_l##name),                                                \
5180 5181
};
#define OP_VR_ST_TABLE(name)                                                  \
5182 5183
static GenOpFunc *gen_op_vr_st##name[NB_MEM_FUNCS] = {                        \
    GEN_MEM_FUNCS(vr_st##name),                                               \
5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221
};

#define GEN_VR_LDX(name, opc2, opc3)                                          \
GEN_HANDLER(l##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)               \
{                                                                             \
    if (unlikely(!ctx->altivec_enabled)) {                                    \
        GEN_EXCP_NO_VR(ctx);                                                  \
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    op_vr_ldst(vr_l##name);                                                   \
    gen_op_store_A0_avr(rD(ctx->opcode));                                     \
}

#define GEN_VR_STX(name, opc2, opc3)                                          \
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
{                                                                             \
    if (unlikely(!ctx->altivec_enabled)) {                                    \
        GEN_EXCP_NO_VR(ctx);                                                  \
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_avr_A0(rS(ctx->opcode));                                      \
    op_vr_ldst(vr_st##name);                                                  \
}

OP_VR_LD_TABLE(vx);
GEN_VR_LDX(vx, 0x07, 0x03);
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
#define gen_op_vr_lvxl gen_op_vr_lvx
GEN_VR_LDX(vxl, 0x07, 0x0B);

OP_VR_ST_TABLE(vx);
GEN_VR_STX(vx, 0x07, 0x07);
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
#define gen_op_vr_stvxl gen_op_vr_stvx
GEN_VR_STX(vxl, 0x07, 0x0F);

5222 5223
/***                           SPE extension                               ***/
/* Register moves */
5224
#if !defined(TARGET_PPC64)
5225

5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237
GEN32(gen_op_load_gpr64_T0, gen_op_load_gpr64_T0_gpr);
GEN32(gen_op_load_gpr64_T1, gen_op_load_gpr64_T1_gpr);
#if 0 // unused
GEN32(gen_op_load_gpr64_T2, gen_op_load_gpr64_T2_gpr);
#endif

GEN32(gen_op_store_T0_gpr64, gen_op_store_T0_gpr64_gpr);
GEN32(gen_op_store_T1_gpr64, gen_op_store_T1_gpr64_gpr);
#if 0 // unused
GEN32(gen_op_store_T2_gpr64, gen_op_store_T2_gpr64_gpr);
#endif

5238
#else /* !defined(TARGET_PPC64) */
5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252

/* No specific load/store functions: GPRs are already 64 bits */
#define gen_op_load_gpr64_T0 gen_op_load_gpr_T0
#define gen_op_load_gpr64_T1 gen_op_load_gpr_T1
#if 0 // unused
#define gen_op_load_gpr64_T2 gen_op_load_gpr_T2
#endif

#define gen_op_store_T0_gpr64 gen_op_store_T0_gpr
#define gen_op_store_T1_gpr64 gen_op_store_T1_gpr
#if 0 // unused
#define gen_op_store_T2_gpr64 gen_op_store_T2_gpr
#endif

5253
#endif /* !defined(TARGET_PPC64) */
5254

5255 5256 5257 5258 5259 5260 5261 5262 5263 5264
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
{                                                                             \
    if (Rc(ctx->opcode))                                                      \
        gen_##name1(ctx);                                                     \
    else                                                                      \
        gen_##name0(ctx);                                                     \
}

/* Handler for undefined SPE opcodes */
5265
static always_inline void gen_speundef (DisasContext *ctx)
5266
{
5267
    GEN_EXCP_INVAL(ctx);
5268 5269 5270
}

/* SPE load and stores */
5271
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, int sh)
5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285
{
    target_long simm = rB(ctx->opcode);

    if (rA(ctx->opcode) == 0) {
        gen_set_T0(simm << sh);
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        if (likely(simm != 0))
            gen_op_addi(simm << sh);
    }
}

#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#define OP_SPE_LD_TABLE(name)                                                 \
5286 5287
static GenOpFunc *gen_op_spe_l##name[NB_MEM_FUNCS] = {                        \
    GEN_MEM_FUNCS(spe_l##name),                                               \
5288 5289
};
#define OP_SPE_ST_TABLE(name)                                                 \
5290 5291
static GenOpFunc *gen_op_spe_st##name[NB_MEM_FUNCS] = {                       \
    GEN_MEM_FUNCS(spe_st##name),                                              \
5292
};
5293 5294

#define GEN_SPE_LD(name, sh)                                                  \
5295
static always_inline void gen_evl##name (DisasContext *ctx)                   \
5296 5297
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5298
        GEN_EXCP_NO_AP(ctx);                                                  \
5299 5300 5301 5302 5303 5304 5305 5306
        return;                                                               \
    }                                                                         \
    gen_addr_spe_imm_index(ctx, sh);                                          \
    op_spe_ldst(spe_l##name);                                                 \
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPE_LDX(name)                                                     \
5307
static always_inline void gen_evl##name##x (DisasContext *ctx)                \
5308 5309
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5310
        GEN_EXCP_NO_AP(ctx);                                                  \
5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    op_spe_ldst(spe_l##name);                                                 \
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_LD(name, sh)                                                \
OP_SPE_LD_TABLE(name);                                                        \
GEN_SPE_LD(name, sh);                                                         \
GEN_SPE_LDX(name)

#define GEN_SPE_ST(name, sh)                                                  \
5324
static always_inline void gen_evst##name (DisasContext *ctx)                  \
5325 5326
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5327
        GEN_EXCP_NO_AP(ctx);                                                  \
5328 5329 5330 5331 5332 5333 5334 5335
        return;                                                               \
    }                                                                         \
    gen_addr_spe_imm_index(ctx, sh);                                          \
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
    op_spe_ldst(spe_st##name);                                                \
}

#define GEN_SPE_STX(name)                                                     \
5336
static always_inline void gen_evst##name##x (DisasContext *ctx)               \
5337 5338
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5339
        GEN_EXCP_NO_AP(ctx);                                                  \
5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
    op_spe_ldst(spe_st##name);                                                \
}

#define GEN_SPEOP_ST(name, sh)                                                \
OP_SPE_ST_TABLE(name);                                                        \
GEN_SPE_ST(name, sh);                                                         \
GEN_SPE_STX(name)

#define GEN_SPEOP_LDST(name, sh)                                              \
GEN_SPEOP_LD(name, sh);                                                       \
GEN_SPEOP_ST(name, sh)

/* SPE arithmetic and logic */
#define GEN_SPEOP_ARITH2(name)                                                \
5358
static always_inline void gen_##name (DisasContext *ctx)                      \
5359 5360
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5361
        GEN_EXCP_NO_AP(ctx);                                                  \
5362 5363 5364 5365 5366 5367 5368 5369 5370
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_ARITH1(name)                                                \
5371
static always_inline void gen_##name (DisasContext *ctx)                      \
5372 5373
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5374
        GEN_EXCP_NO_AP(ctx);                                                  \
5375 5376 5377 5378 5379 5380 5381 5382
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_COMP(name)                                                  \
5383
static always_inline void gen_##name (DisasContext *ctx)                      \
5384 5385
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5386
        GEN_EXCP_NO_AP(ctx);                                                  \
5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}

/* Logical */
GEN_SPEOP_ARITH2(evand);
GEN_SPEOP_ARITH2(evandc);
GEN_SPEOP_ARITH2(evxor);
GEN_SPEOP_ARITH2(evor);
GEN_SPEOP_ARITH2(evnor);
GEN_SPEOP_ARITH2(eveqv);
GEN_SPEOP_ARITH2(evorc);
GEN_SPEOP_ARITH2(evnand);
GEN_SPEOP_ARITH2(evsrwu);
GEN_SPEOP_ARITH2(evsrws);
GEN_SPEOP_ARITH2(evslw);
GEN_SPEOP_ARITH2(evrlw);
GEN_SPEOP_ARITH2(evmergehi);
GEN_SPEOP_ARITH2(evmergelo);
GEN_SPEOP_ARITH2(evmergehilo);
GEN_SPEOP_ARITH2(evmergelohi);

/* Arithmetic */
GEN_SPEOP_ARITH2(evaddw);
GEN_SPEOP_ARITH2(evsubfw);
GEN_SPEOP_ARITH1(evabs);
GEN_SPEOP_ARITH1(evneg);
GEN_SPEOP_ARITH1(evextsb);
GEN_SPEOP_ARITH1(evextsh);
GEN_SPEOP_ARITH1(evrndw);
GEN_SPEOP_ARITH1(evcntlzw);
GEN_SPEOP_ARITH1(evcntlsw);
5423
static always_inline void gen_brinc (DisasContext *ctx)
5424 5425
{
    /* Note: brinc is usable even if SPE is disabled */
5426 5427
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
5428
    gen_op_brinc();
5429
    gen_op_store_T0_gpr(rD(ctx->opcode));
5430 5431 5432
}

#define GEN_SPEOP_ARITH_IMM2(name)                                            \
5433
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5434 5435
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5436
        GEN_EXCP_NO_AP(ctx);                                                  \
5437 5438 5439 5440 5441 5442 5443 5444 5445
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_LOGIC_IMM2(name)                                            \
5446
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5447 5448
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5449
        GEN_EXCP_NO_AP(ctx);                                                  \
5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_splatwi_T1_64(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

GEN_SPEOP_ARITH_IMM2(evaddw);
#define gen_evaddiw gen_evaddwi
GEN_SPEOP_ARITH_IMM2(evsubfw);
#define gen_evsubifw gen_evsubfwi
GEN_SPEOP_LOGIC_IMM2(evslw);
GEN_SPEOP_LOGIC_IMM2(evsrwu);
#define gen_evsrwis gen_evsrwsi
GEN_SPEOP_LOGIC_IMM2(evsrws);
#define gen_evsrwiu gen_evsrwui
GEN_SPEOP_LOGIC_IMM2(evrlw);

5469
static always_inline void gen_evsplati (DisasContext *ctx)
5470 5471 5472 5473 5474 5475 5476
{
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;

    gen_op_splatwi_T0_64(imm);
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

5477
static always_inline void gen_evsplatfi (DisasContext *ctx)
5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517
{
    uint32_t imm = rA(ctx->opcode) << 27;

    gen_op_splatwi_T0_64(imm);
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

/* Comparison */
GEN_SPEOP_COMP(evcmpgtu);
GEN_SPEOP_COMP(evcmpgts);
GEN_SPEOP_COMP(evcmpltu);
GEN_SPEOP_COMP(evcmplts);
GEN_SPEOP_COMP(evcmpeq);

GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////

5518
static always_inline void gen_evsel (DisasContext *ctx)
5519 5520
{
    if (unlikely(!ctx->spe_enabled)) {
5521
        GEN_EXCP_NO_AP(ctx);
5522 5523 5524 5525 5526 5527 5528 5529 5530
        return;
    }
    gen_op_load_crf_T0(ctx->opcode & 0x7);
    gen_op_load_gpr64_T0(rA(ctx->opcode));
    gen_op_load_gpr64_T1(rB(ctx->opcode));
    gen_op_evsel();
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

5531
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
5532 5533 5534
{
    gen_evsel(ctx);
}
5535
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
5536 5537 5538
{
    gen_evsel(ctx);
}
5539
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
5540 5541 5542
{
    gen_evsel(ctx);
}
5543
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
5544 5545 5546 5547 5548 5549 5550 5551 5552
{
    gen_evsel(ctx);
}

/* Load and stores */
#if defined(TARGET_PPC64)
/* In that case, we already have 64 bits load & stores
 * so, spe_ldd is equivalent to ld and spe_std is equivalent to std
 */
5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584
#define gen_op_spe_ldd_raw           gen_op_ld_raw
#define gen_op_spe_ldd_user          gen_op_ld_user
#define gen_op_spe_ldd_kernel        gen_op_ld_kernel
#define gen_op_spe_ldd_hypv          gen_op_ld_hypv
#define gen_op_spe_ldd_64_raw        gen_op_ld_64_raw
#define gen_op_spe_ldd_64_user       gen_op_ld_64_user
#define gen_op_spe_ldd_64_kernel     gen_op_ld_64_kernel
#define gen_op_spe_ldd_64_hypv       gen_op_ld_64_hypv
#define gen_op_spe_ldd_le_raw        gen_op_ld_le_raw
#define gen_op_spe_ldd_le_user       gen_op_ld_le_user
#define gen_op_spe_ldd_le_kernel     gen_op_ld_le_kernel
#define gen_op_spe_ldd_le_hypv       gen_op_ld_le_hypv
#define gen_op_spe_ldd_le_64_raw     gen_op_ld_le_64_raw
#define gen_op_spe_ldd_le_64_user    gen_op_ld_le_64_user
#define gen_op_spe_ldd_le_64_kernel  gen_op_ld_le_64_kernel
#define gen_op_spe_ldd_le_64_hypv    gen_op_ld_le_64_hypv
#define gen_op_spe_stdd_raw          gen_op_std_raw
#define gen_op_spe_stdd_user         gen_op_std_user
#define gen_op_spe_stdd_kernel       gen_op_std_kernel
#define gen_op_spe_stdd_hypv         gen_op_std_hypv
#define gen_op_spe_stdd_64_raw       gen_op_std_64_raw
#define gen_op_spe_stdd_64_user      gen_op_std_64_user
#define gen_op_spe_stdd_64_kernel    gen_op_std_64_kernel
#define gen_op_spe_stdd_64_hypv      gen_op_std_64_hypv
#define gen_op_spe_stdd_le_raw       gen_op_std_le_raw
#define gen_op_spe_stdd_le_user      gen_op_std_le_user
#define gen_op_spe_stdd_le_kernel    gen_op_std_le_kernel
#define gen_op_spe_stdd_le_hypv      gen_op_std_le_hypv
#define gen_op_spe_stdd_le_64_raw    gen_op_std_le_64_raw
#define gen_op_spe_stdd_le_64_user   gen_op_std_le_64_user
#define gen_op_spe_stdd_le_64_kernel gen_op_std_le_64_kernel
#define gen_op_spe_stdd_le_64_hypv   gen_op_std_le_64_hypv
5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595
#endif /* defined(TARGET_PPC64) */
GEN_SPEOP_LDST(dd, 3);
GEN_SPEOP_LDST(dw, 3);
GEN_SPEOP_LDST(dh, 3);
GEN_SPEOP_LDST(whe, 2);
GEN_SPEOP_LD(whou, 2);
GEN_SPEOP_LD(whos, 2);
GEN_SPEOP_ST(who, 2);

#if defined(TARGET_PPC64)
/* In that case, spe_stwwo is equivalent to stw */
5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609
#define gen_op_spe_stwwo_raw          gen_op_stw_raw
#define gen_op_spe_stwwo_user         gen_op_stw_user
#define gen_op_spe_stwwo_kernel       gen_op_stw_kernel
#define gen_op_spe_stwwo_hypv         gen_op_stw_hypv
#define gen_op_spe_stwwo_le_raw       gen_op_stw_le_raw
#define gen_op_spe_stwwo_le_user      gen_op_stw_le_user
#define gen_op_spe_stwwo_le_kernel    gen_op_stw_le_kernel
#define gen_op_spe_stwwo_le_hypv      gen_op_stw_le_hypv
#define gen_op_spe_stwwo_64_raw       gen_op_stw_64_raw
#define gen_op_spe_stwwo_64_user      gen_op_stw_64_user
#define gen_op_spe_stwwo_64_kernel    gen_op_stw_64_kernel
#define gen_op_spe_stwwo_64_hypv      gen_op_stw_64_hypv
#define gen_op_spe_stwwo_le_64_raw    gen_op_stw_le_64_raw
#define gen_op_spe_stwwo_le_64_user   gen_op_stw_le_64_user
5610
#define gen_op_spe_stwwo_le_64_kernel gen_op_stw_le_64_kernel
5611
#define gen_op_spe_stwwo_le_64_hypv   gen_op_stw_le_64_hypv
5612 5613
#endif
#define _GEN_OP_SPE_STWWE(suffix)                                             \
5614
static always_inline void gen_op_spe_stwwe_##suffix (void)                    \
5615 5616 5617 5618 5619
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_##suffix();                                              \
}
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
5620
static always_inline void gen_op_spe_stwwe_le_##suffix (void)                 \
5621 5622 5623 5624 5625 5626 5627 5628
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_##suffix();                                           \
}
#if defined(TARGET_PPC64)
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
5629
static always_inline void gen_op_spe_stwwe_64_##suffix (void)                 \
5630 5631 5632 5633
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_64_##suffix();                                           \
}                                                                             \
5634
static always_inline void gen_op_spe_stwwe_le_64_##suffix (void)              \
5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_64_##suffix();                                        \
}
#else
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix)
#endif
#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_STWWE(raw);
#else /* defined(CONFIG_USER_ONLY) */
GEN_OP_SPE_STWWE(user);
5648 5649
GEN_OP_SPE_STWWE(kernel);
GEN_OP_SPE_STWWE(hypv);
5650 5651 5652 5653 5654
#endif /* defined(CONFIG_USER_ONLY) */
GEN_SPEOP_ST(wwe, 2);
GEN_SPEOP_ST(wwo, 2);

#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
5655
static always_inline void gen_op_spe_l##name##_##suffix (void)                \
5656 5657 5658 5659 5660 5661
{                                                                             \
    gen_op_##op##_##suffix();                                                 \
    gen_op_splatw_T1_64();                                                    \
}

#define GEN_OP_SPE_LHE(suffix)                                                \
5662
static always_inline void gen_op_spe_lhe_##suffix (void)                      \
5663 5664 5665 5666 5667 5668
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_sli16_T1_64();                                                     \
}

#define GEN_OP_SPE_LHX(suffix)                                                \
5669
static always_inline void gen_op_spe_lhx_##suffix (void)                      \
5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_extsh_T1_64();                                                     \
}

#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_LHE(raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
GEN_OP_SPE_LHE(le_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
GEN_OP_SPE_LHX(raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
GEN_OP_SPE_LHX(le_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
GEN_OP_SPE_LHE(le_64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
GEN_OP_SPE_LHX(64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
GEN_OP_SPE_LHX(le_64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
#endif
#else
GEN_OP_SPE_LHE(user);
5700 5701
GEN_OP_SPE_LHE(kernel);
GEN_OP_SPE_LHE(hypv);
5702
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
5703 5704
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, hypv);
5705
GEN_OP_SPE_LHE(le_user);
5706 5707
GEN_OP_SPE_LHE(le_kernel);
GEN_OP_SPE_LHE(le_hypv);
5708
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
5709 5710
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_hypv);
5711
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
5712 5713
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, hypv);
5714
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
5715 5716
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_hypv);
5717
GEN_OP_SPE_LHX(user);
5718 5719
GEN_OP_SPE_LHX(kernel);
GEN_OP_SPE_LHX(hypv);
5720
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
5721 5722
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, hypv);
5723
GEN_OP_SPE_LHX(le_user);
5724 5725
GEN_OP_SPE_LHX(le_kernel);
GEN_OP_SPE_LHX(le_hypv);
5726
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
5727 5728
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_hypv);
5729 5730
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_user);
5731 5732
GEN_OP_SPE_LHE(64_kernel);
GEN_OP_SPE_LHE(64_hypv);
5733
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
5734 5735
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_hypv);
5736
GEN_OP_SPE_LHE(le_64_user);
5737 5738
GEN_OP_SPE_LHE(le_64_kernel);
GEN_OP_SPE_LHE(le_64_hypv);
5739
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
5740 5741
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_hypv);
5742
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
5743 5744
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_hypv);
5745
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
5746 5747
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_hypv);
5748
GEN_OP_SPE_LHX(64_user);
5749 5750
GEN_OP_SPE_LHX(64_kernel);
GEN_OP_SPE_LHX(64_hypv);
5751
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
5752 5753
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_hypv);
5754
GEN_OP_SPE_LHX(le_64_user);
5755 5756
GEN_OP_SPE_LHX(le_64_kernel);
GEN_OP_SPE_LHX(le_64_hypv);
5757
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
5758 5759
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_hypv);
5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864
#endif
#endif
GEN_SPEOP_LD(hhesplat, 1);
GEN_SPEOP_LD(hhousplat, 1);
GEN_SPEOP_LD(hhossplat, 1);
GEN_SPEOP_LD(wwsplat, 2);
GEN_SPEOP_LD(whsplat, 2);

GEN_SPE(evlddx,         evldd,         0x00, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evldwx,         evldw,         0x01, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evldhx,         evldh,         0x02, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlhhesplatx,   evlhhesplat,   0x04, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlhhousplatx,  evlhhousplat,  0x06, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlhhossplatx,  evlhhossplat,  0x07, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhex,        evlwhe,        0x08, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhoux,       evlwhou,       0x0A, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhosx,       evlwhos,       0x0B, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwwsplatx,    evlwwsplat,    0x0C, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evlwhsplatx,    evlwhsplat,    0x0E, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstddx,        evstdd,        0x10, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstdwx,        evstdw,        0x11, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstdhx,        evstdh,        0x12, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwhex,       evstwhe,       0x18, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwhox,       evstwho,       0x1A, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwwex,       evstwwe,       0x1C, 0x0C, 0x00000000, PPC_SPE); //
GEN_SPE(evstwwox,       evstwwo,       0x1E, 0x0C, 0x00000000, PPC_SPE); //

/* Multiply and add - TODO */
#if 0
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);

GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);

GEN_SPE(evadduiaaw,     evaddsiaaw,    0x00, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evsubfusiaaw,   evsubfssiaaw,  0x01, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evaddumiaaw,    evaddsmiaaw,   0x04, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evsubfumiaaw,   evsubfsmiaaw,  0x05, 0x13, 0x0000F800, PPC_SPE);
GEN_SPE(evdivws,        evdivwu,       0x06, 0x13, 0x00000000, PPC_SPE);
GEN_SPE(evmra,          speundef,      0x07, 0x13, 0x0000F800, PPC_SPE);

GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);

GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);

GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);

GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
#endif

/***                      SPE floating-point extension                     ***/
#define GEN_SPEFPUOP_CONV(name)                                               \
5865
static always_inline void gen_##name (DisasContext *ctx)                      \
5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011
{                                                                             \
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

/* Single precision floating-point vectors operations */
/* Arithmetic */
GEN_SPEOP_ARITH2(evfsadd);
GEN_SPEOP_ARITH2(evfssub);
GEN_SPEOP_ARITH2(evfsmul);
GEN_SPEOP_ARITH2(evfsdiv);
GEN_SPEOP_ARITH1(evfsabs);
GEN_SPEOP_ARITH1(evfsnabs);
GEN_SPEOP_ARITH1(evfsneg);
/* Conversion */
GEN_SPEFPUOP_CONV(evfscfui);
GEN_SPEFPUOP_CONV(evfscfsi);
GEN_SPEFPUOP_CONV(evfscfuf);
GEN_SPEFPUOP_CONV(evfscfsf);
GEN_SPEFPUOP_CONV(evfsctui);
GEN_SPEFPUOP_CONV(evfsctsi);
GEN_SPEFPUOP_CONV(evfsctuf);
GEN_SPEFPUOP_CONV(evfsctsf);
GEN_SPEFPUOP_CONV(evfsctuiz);
GEN_SPEFPUOP_CONV(evfsctsiz);
/* Comparison */
GEN_SPEOP_COMP(evfscmpgt);
GEN_SPEOP_COMP(evfscmplt);
GEN_SPEOP_COMP(evfscmpeq);
GEN_SPEOP_COMP(evfststgt);
GEN_SPEOP_COMP(evfststlt);
GEN_SPEOP_COMP(evfststeq);

/* Opcodes definitions */
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //

/* Single precision floating-point operations */
/* Arithmetic */
GEN_SPEOP_ARITH2(efsadd);
GEN_SPEOP_ARITH2(efssub);
GEN_SPEOP_ARITH2(efsmul);
GEN_SPEOP_ARITH2(efsdiv);
GEN_SPEOP_ARITH1(efsabs);
GEN_SPEOP_ARITH1(efsnabs);
GEN_SPEOP_ARITH1(efsneg);
/* Conversion */
GEN_SPEFPUOP_CONV(efscfui);
GEN_SPEFPUOP_CONV(efscfsi);
GEN_SPEFPUOP_CONV(efscfuf);
GEN_SPEFPUOP_CONV(efscfsf);
GEN_SPEFPUOP_CONV(efsctui);
GEN_SPEFPUOP_CONV(efsctsi);
GEN_SPEFPUOP_CONV(efsctuf);
GEN_SPEFPUOP_CONV(efsctsf);
GEN_SPEFPUOP_CONV(efsctuiz);
GEN_SPEFPUOP_CONV(efsctsiz);
GEN_SPEFPUOP_CONV(efscfd);
/* Comparison */
GEN_SPEOP_COMP(efscmpgt);
GEN_SPEOP_COMP(efscmplt);
GEN_SPEOP_COMP(efscmpeq);
GEN_SPEOP_COMP(efststgt);
GEN_SPEOP_COMP(efststlt);
GEN_SPEOP_COMP(efststeq);

/* Opcodes definitions */
GEN_SPE(efsadd,         efssub,        0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efsctuiz,       efsctsiz,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //

/* Double precision floating-point operations */
/* Arithmetic */
GEN_SPEOP_ARITH2(efdadd);
GEN_SPEOP_ARITH2(efdsub);
GEN_SPEOP_ARITH2(efdmul);
GEN_SPEOP_ARITH2(efddiv);
GEN_SPEOP_ARITH1(efdabs);
GEN_SPEOP_ARITH1(efdnabs);
GEN_SPEOP_ARITH1(efdneg);
/* Conversion */

GEN_SPEFPUOP_CONV(efdcfui);
GEN_SPEFPUOP_CONV(efdcfsi);
GEN_SPEFPUOP_CONV(efdcfuf);
GEN_SPEFPUOP_CONV(efdcfsf);
GEN_SPEFPUOP_CONV(efdctui);
GEN_SPEFPUOP_CONV(efdctsi);
GEN_SPEFPUOP_CONV(efdctuf);
GEN_SPEFPUOP_CONV(efdctsf);
GEN_SPEFPUOP_CONV(efdctuiz);
GEN_SPEFPUOP_CONV(efdctsiz);
GEN_SPEFPUOP_CONV(efdcfs);
GEN_SPEFPUOP_CONV(efdcfuid);
GEN_SPEFPUOP_CONV(efdcfsid);
GEN_SPEFPUOP_CONV(efdctuidz);
GEN_SPEFPUOP_CONV(efdctsidz);
/* Comparison */
GEN_SPEOP_COMP(efdcmpgt);
GEN_SPEOP_COMP(efdcmplt);
GEN_SPEOP_COMP(efdcmpeq);
GEN_SPEOP_COMP(efdtstgt);
GEN_SPEOP_COMP(efdtstlt);
GEN_SPEOP_COMP(efdtsteq);

/* Opcodes definitions */
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //

B
bellard 已提交
6012 6013 6014
/* End opcode list */
GEN_OPCODE_MARK(end);

6015
#include "translate_init.c"
6016
#include "helper_regs.h"
B
bellard 已提交
6017

6018
/*****************************************************************************/
6019
/* Misc PowerPC helpers */
6020 6021 6022
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
6023
{
6024 6025 6026 6027 6028 6029 6030 6031 6032 6033
#if defined(TARGET_PPC64) || 1
#define FILL ""
#define RGPL  4
#define RFPL  4
#else
#define FILL "        "
#define RGPL  8
#define RFPL  4
#endif

B
bellard 已提交
6034 6035
    int i;

J
j_mayer 已提交
6036 6037 6038 6039 6040
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
                env->nip, env->lr, env->ctr, hreg_load_xer(env));
    cpu_fprintf(f, "MSR " REGX FILL " HID0 " REGX FILL "  HF " REGX FILL
                " idx %d\n",
                env->msr, env->hflags, env->spr[SPR_HID0], env->mmu_idx);
6041
#if !defined(NO_TIMER_DUMP)
J
j_mayer 已提交
6042
    cpu_fprintf(f, "TB %08x %08x "
6043 6044 6045 6046
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
#endif
                "\n",
J
j_mayer 已提交
6047
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6048 6049 6050 6051
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
#endif
                );
J
j_mayer 已提交
6052
#endif
6053
    for (i = 0; i < 32; i++) {
6054 6055
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
6056
        cpu_fprintf(f, " " REGX, (target_ulong)env->gpr[i]);
6057
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
6058
            cpu_fprintf(f, "\n");
6059
    }
6060
    cpu_fprintf(f, "CR ");
6061
    for (i = 0; i < 8; i++)
B
bellard 已提交
6062 6063
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
6064 6065 6066 6067 6068 6069 6070 6071
    for (i = 0; i < 8; i++) {
        char a = '-';
        if (env->crf[i] & 0x08)
            a = 'L';
        else if (env->crf[i] & 0x04)
            a = 'G';
        else if (env->crf[i] & 0x02)
            a = 'E';
B
bellard 已提交
6072
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6073
    }
6074 6075 6076 6077
    cpu_fprintf(f, " ]             " FILL "RES " REGX "\n", env->reserve);
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
6078
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6079
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
6080
            cpu_fprintf(f, "\n");
B
bellard 已提交
6081
    }
6082
#if !defined(CONFIG_USER_ONLY)
J
j_mayer 已提交
6083
    cpu_fprintf(f, "SRR0 " REGX " SRR1 " REGX " SDR1 " REGX "\n",
6084
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
6085
#endif
B
bellard 已提交
6086

6087 6088 6089
#undef RGPL
#undef RFPL
#undef FILL
B
bellard 已提交
6090 6091
}

6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138
void cpu_dump_statistics (CPUState *env, FILE*f,
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                          int flags)
{
#if defined(DO_PPC_STATISTICS)
    opc_handler_t **t1, **t2, **t3, *handler;
    int op1, op2, op3;

    t1 = env->opcodes;
    for (op1 = 0; op1 < 64; op1++) {
        handler = t1[op1];
        if (is_indirect_opcode(handler)) {
            t2 = ind_table(handler);
            for (op2 = 0; op2 < 32; op2++) {
                handler = t2[op2];
                if (is_indirect_opcode(handler)) {
                    t3 = ind_table(handler);
                    for (op3 = 0; op3 < 32; op3++) {
                        handler = t3[op3];
                        if (handler->count == 0)
                            continue;
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
                                    "%016llx %lld\n",
                                    op1, op2, op3, op1, (op3 << 5) | op2,
                                    handler->oname,
                                    handler->count, handler->count);
                    }
                } else {
                    if (handler->count == 0)
                        continue;
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
                                "%016llx %lld\n",
                                op1, op2, op1, op2, handler->oname,
                                handler->count, handler->count);
                }
            }
        } else {
            if (handler->count == 0)
                continue;
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
                        op1, op1, handler->oname,
                        handler->count, handler->count);
        }
    }
#endif
}

6139
/*****************************************************************************/
6140 6141 6142
static always_inline int gen_intermediate_code_internal (CPUState *env,
                                                         TranslationBlock *tb,
                                                         int search_pc)
B
bellard 已提交
6143
{
6144
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
6145
    opc_handler_t **table, *handler;
B
bellard 已提交
6146
    target_ulong pc_start;
B
bellard 已提交
6147
    uint16_t *gen_opc_end;
6148
    int supervisor, little_endian;
6149
    int single_step, branch_step;
B
bellard 已提交
6150 6151 6152 6153 6154 6155
    int j, lj = -1;

    pc_start = tb->pc;
    gen_opc_ptr = gen_opc_buf;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
    gen_opparam_ptr = gen_opparam_buf;
6156 6157 6158
#if defined(OPTIMIZE_FPRF_UPDATE)
    gen_fprf_ptr = gen_fprf_buf;
#endif
B
bellard 已提交
6159
    nb_gen_labels = 0;
B
bellard 已提交
6160
    ctx.nip = pc_start;
B
bellard 已提交
6161
    ctx.tb = tb;
6162
    ctx.exception = POWERPC_EXCP_NONE;
6163
    ctx.spr_cb = env->spr_cb;
6164 6165
    supervisor = env->mmu_idx;
#if !defined(CONFIG_USER_ONLY)
6166
    ctx.supervisor = supervisor;
6167
#endif
6168
    little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
6169 6170
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
6171
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
6172
#else
6173
    ctx.mem_idx = (supervisor << 1) | little_endian;
6174
#endif
6175
    ctx.dcache_line_size = env->dcache_line_size;
B
bellard 已提交
6176
    ctx.fpu_enabled = msr_fp;
6177
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
6178 6179 6180
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
6181 6182 6183 6184
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
        ctx.altivec_enabled = msr_vr;
    else
        ctx.altivec_enabled = 0;
6185 6186 6187 6188 6189 6190 6191 6192
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
        single_step = 1;
    else
        single_step = 0;
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
        branch_step = 1;
    else
        branch_step = 0;
J
j_mayer 已提交
6193
    ctx.singlestep_enabled = env->singlestep_enabled || single_step == 1;
6194
#if defined (DO_SINGLE_STEP) && 0
6195 6196 6197 6198
    /* Single step trace mode */
    msr_se = 1;
#endif
    /* Set env in case of segfault during code fetch */
6199
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6200 6201
        if (unlikely(env->nb_breakpoints > 0)) {
            for (j = 0; j < env->nb_breakpoints; j++) {
6202
                if (env->breakpoints[j] == ctx.nip) {
6203
                    gen_update_nip(&ctx, ctx.nip);
6204 6205 6206 6207 6208
                    gen_op_debug();
                    break;
                }
            }
        }
6209
        if (unlikely(search_pc)) {
B
bellard 已提交
6210 6211 6212 6213 6214
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
6215
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
6216 6217 6218
                gen_opc_instr_start[lj] = 1;
            }
        }
6219 6220
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
6221
            fprintf(logfile, "----------------\n");
6222
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6223
                    ctx.nip, supervisor, (int)msr_ir);
6224 6225
        }
#endif
6226 6227 6228 6229
        if (unlikely(little_endian)) {
            ctx.opcode = bswap32(ldl_code(ctx.nip));
        } else {
            ctx.opcode = ldl_code(ctx.nip);
6230
        }
6231 6232
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6233
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6234
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6235
                    opc3(ctx.opcode), little_endian ? "little" : "big");
B
bellard 已提交
6236 6237
        }
#endif
B
bellard 已提交
6238
        ctx.nip += 4;
6239
        table = env->opcodes;
B
bellard 已提交
6240 6241 6242 6243 6244 6245 6246 6247 6248 6249
        handler = table[opc1(ctx.opcode)];
        if (is_indirect_opcode(handler)) {
            table = ind_table(handler);
            handler = table[opc2(ctx.opcode)];
            if (is_indirect_opcode(handler)) {
                table = ind_table(handler);
                handler = table[opc3(ctx.opcode)];
            }
        }
        /* Is opcode *REALLY* valid ? */
6250
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
6251
            if (loglevel != 0) {
6252
                fprintf(logfile, "invalid/unsupported opcode: "
6253
                        "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
6254
                        opc1(ctx.opcode), opc2(ctx.opcode),
6255
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6256 6257
            } else {
                printf("invalid/unsupported opcode: "
6258
                       "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
B
bellard 已提交
6259
                       opc1(ctx.opcode), opc2(ctx.opcode),
6260
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
B
bellard 已提交
6261
            }
6262 6263
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
6264
                if (loglevel != 0) {
B
bellard 已提交
6265
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6266
                            "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
B
bellard 已提交
6267 6268
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6269
                            ctx.opcode, ctx.nip - 4);
6270 6271
                } else {
                    printf("invalid bits: %08x for opcode: "
6272
                           "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
6273 6274
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6275
                           ctx.opcode, ctx.nip - 4);
6276
                }
6277
                GEN_EXCP_INVAL(ctxp);
B
bellard 已提交
6278
                break;
B
bellard 已提交
6279 6280
            }
        }
B
bellard 已提交
6281
        (*(handler->handler))(&ctx);
6282 6283 6284
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
6285
        /* Check trace mode exceptions */
6286 6287 6288 6289 6290 6291
        if (unlikely(branch_step != 0 &&
                     ctx.exception == POWERPC_EXCP_BRANCH)) {
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
        } else if (unlikely(single_step != 0 &&
                            (ctx.nip <= 0x100 || ctx.nip > 0xF00 ||
                             (ctx.nip & 0xFC) != 0x04) &&
6292
                            ctx.exception != POWERPC_SYSCALL &&
6293
                            ctx.exception != POWERPC_EXCP_TRAP)) {
6294
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6295 6296 6297 6298 6299
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
                            (env->singlestep_enabled))) {
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
6300
            break;
6301
        }
6302 6303 6304 6305
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
6306
    if (ctx.exception == POWERPC_EXCP_NONE) {
6307
        gen_goto_tb(&ctx, 0, ctx.nip);
6308
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6309 6310 6311
        gen_op_reset_T0();
        /* Generate the return instruction */
        gen_op_exit_tb();
6312
    }
B
bellard 已提交
6313
    *gen_opc_ptr = INDEX_op_end;
6314
    if (unlikely(search_pc)) {
6315 6316 6317 6318 6319
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
6320
        tb->size = ctx.nip - pc_start;
6321
    }
6322
#if defined(DEBUG_DISAS)
6323
    if (loglevel & CPU_LOG_TB_CPU) {
6324
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
6325
        cpu_dump_state(env, logfile, fprintf, 0);
6326 6327
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6328
        int flags;
6329
        flags = env->bfd_mach;
6330
        flags |= little_endian << 16;
B
bellard 已提交
6331
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6332
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
6333
        fprintf(logfile, "\n");
6334 6335
    }
    if (loglevel & CPU_LOG_TB_OP) {
B
bellard 已提交
6336 6337 6338 6339 6340 6341 6342 6343
        fprintf(logfile, "OP:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
#endif
    return 0;
}

6344
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6345 6346 6347 6348
{
    return gen_intermediate_code_internal(env, tb, 0);
}

6349
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6350 6351 6352
{
    return gen_intermediate_code_internal(env, tb, 1);
}