qemu/target/ppc/translate.c
<<
>>
Prefs
   1/*
   2 *  PowerPC emulation for qemu: main translation routines.
   3 *
   4 *  Copyright (c) 2003-2007 Jocelyn Mayer
   5 *  Copyright (C) 2011 Freescale Semiconductor, Inc.
   6 *
   7 * This library is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU Lesser General Public
   9 * License as published by the Free Software Foundation; either
  10 * version 2 of the License, or (at your option) any later version.
  11 *
  12 * This library is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * Lesser General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU Lesser General Public
  18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19 */
  20
  21#include "qemu/osdep.h"
  22#include "cpu.h"
  23#include "internal.h"
  24#include "disas/disas.h"
  25#include "exec/exec-all.h"
  26#include "tcg-op.h"
  27#include "tcg-op-gvec.h"
  28#include "qemu/host-utils.h"
  29#include "qemu/main-loop.h"
  30#include "exec/cpu_ldst.h"
  31
  32#include "exec/helper-proto.h"
  33#include "exec/helper-gen.h"
  34
  35#include "trace-tcg.h"
  36#include "exec/translator.h"
  37#include "exec/log.h"
  38#include "qemu/atomic128.h"
  39
  40
  41#define CPU_SINGLE_STEP 0x1
  42#define CPU_BRANCH_STEP 0x2
  43#define GDBSTUB_SINGLE_STEP 0x4
  44
  45/* Include definitions for instructions classes and implementations flags */
  46/* #define PPC_DEBUG_DISAS */
  47/* #define DO_PPC_STATISTICS */
  48
  49#ifdef PPC_DEBUG_DISAS
  50#  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
  51#else
  52#  define LOG_DISAS(...) do { } while (0)
  53#endif
  54/*****************************************************************************/
  55/* Code translation helpers                                                  */
  56
  57/* global register indexes */
  58static char cpu_reg_names[10 * 3 + 22 * 4   /* GPR */
  59                          + 10 * 4 + 22 * 5 /* SPE GPRh */
  60                          + 8 * 5           /* CRF */];
  61static TCGv cpu_gpr[32];
  62static TCGv cpu_gprh[32];
  63static TCGv_i32 cpu_crf[8];
  64static TCGv cpu_nip;
  65static TCGv cpu_msr;
  66static TCGv cpu_ctr;
  67static TCGv cpu_lr;
  68#if defined(TARGET_PPC64)
  69static TCGv cpu_cfar;
  70#endif
  71static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca, cpu_ov32, cpu_ca32;
  72static TCGv cpu_reserve;
  73static TCGv cpu_reserve_val;
  74static TCGv cpu_fpscr;
  75static TCGv_i32 cpu_access_type;
  76
  77#include "exec/gen-icount.h"
  78
  79void ppc_translate_init(void)
  80{
  81    int i;
  82    char *p;
  83    size_t cpu_reg_names_size;
  84
  85    p = cpu_reg_names;
  86    cpu_reg_names_size = sizeof(cpu_reg_names);
  87
  88    for (i = 0; i < 8; i++) {
  89        snprintf(p, cpu_reg_names_size, "crf%d", i);
  90        cpu_crf[i] = tcg_global_mem_new_i32(cpu_env,
  91                                            offsetof(CPUPPCState, crf[i]), p);
  92        p += 5;
  93        cpu_reg_names_size -= 5;
  94    }
  95
  96    for (i = 0; i < 32; i++) {
  97        snprintf(p, cpu_reg_names_size, "r%d", i);
  98        cpu_gpr[i] = tcg_global_mem_new(cpu_env,
  99                                        offsetof(CPUPPCState, gpr[i]), p);
 100        p += (i < 10) ? 3 : 4;
 101        cpu_reg_names_size -= (i < 10) ? 3 : 4;
 102        snprintf(p, cpu_reg_names_size, "r%dH", i);
 103        cpu_gprh[i] = tcg_global_mem_new(cpu_env,
 104                                         offsetof(CPUPPCState, gprh[i]), p);
 105        p += (i < 10) ? 4 : 5;
 106        cpu_reg_names_size -= (i < 10) ? 4 : 5;
 107    }
 108
 109    cpu_nip = tcg_global_mem_new(cpu_env,
 110                                 offsetof(CPUPPCState, nip), "nip");
 111
 112    cpu_msr = tcg_global_mem_new(cpu_env,
 113                                 offsetof(CPUPPCState, msr), "msr");
 114
 115    cpu_ctr = tcg_global_mem_new(cpu_env,
 116                                 offsetof(CPUPPCState, ctr), "ctr");
 117
 118    cpu_lr = tcg_global_mem_new(cpu_env,
 119                                offsetof(CPUPPCState, lr), "lr");
 120
 121#if defined(TARGET_PPC64)
 122    cpu_cfar = tcg_global_mem_new(cpu_env,
 123                                  offsetof(CPUPPCState, cfar), "cfar");
 124#endif
 125
 126    cpu_xer = tcg_global_mem_new(cpu_env,
 127                                 offsetof(CPUPPCState, xer), "xer");
 128    cpu_so = tcg_global_mem_new(cpu_env,
 129                                offsetof(CPUPPCState, so), "SO");
 130    cpu_ov = tcg_global_mem_new(cpu_env,
 131                                offsetof(CPUPPCState, ov), "OV");
 132    cpu_ca = tcg_global_mem_new(cpu_env,
 133                                offsetof(CPUPPCState, ca), "CA");
 134    cpu_ov32 = tcg_global_mem_new(cpu_env,
 135                                  offsetof(CPUPPCState, ov32), "OV32");
 136    cpu_ca32 = tcg_global_mem_new(cpu_env,
 137                                  offsetof(CPUPPCState, ca32), "CA32");
 138
 139    cpu_reserve = tcg_global_mem_new(cpu_env,
 140                                     offsetof(CPUPPCState, reserve_addr),
 141                                     "reserve_addr");
 142    cpu_reserve_val = tcg_global_mem_new(cpu_env,
 143                                     offsetof(CPUPPCState, reserve_val),
 144                                     "reserve_val");
 145
 146    cpu_fpscr = tcg_global_mem_new(cpu_env,
 147                                   offsetof(CPUPPCState, fpscr), "fpscr");
 148
 149    cpu_access_type = tcg_global_mem_new_i32(cpu_env,
 150                                             offsetof(CPUPPCState, access_type),
 151                                             "access_type");
 152}
 153
 154/* internal defines */
 155struct DisasContext {
 156    DisasContextBase base;
 157    uint32_t opcode;
 158    uint32_t exception;
 159    /* Routine used to access memory */
 160    bool pr, hv, dr, le_mode;
 161    bool lazy_tlb_flush;
 162    bool need_access_type;
 163    int mem_idx;
 164    int access_type;
 165    /* Translation flags */
 166    MemOp default_tcg_memop_mask;
 167#if defined(TARGET_PPC64)
 168    bool sf_mode;
 169    bool has_cfar;
 170#endif
 171    bool fpu_enabled;
 172    bool altivec_enabled;
 173    bool vsx_enabled;
 174    bool spe_enabled;
 175    bool tm_enabled;
 176    bool gtse;
 177    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
 178    int singlestep_enabled;
 179    uint32_t flags;
 180    uint64_t insns_flags;
 181    uint64_t insns_flags2;
 182};
 183
 184/* Return true iff byteswap is needed in a scalar memop */
 185static inline bool need_byteswap(const DisasContext *ctx)
 186{
 187#if defined(TARGET_WORDS_BIGENDIAN)
 188     return ctx->le_mode;
 189#else
 190     return !ctx->le_mode;
 191#endif
 192}
 193
 194/* True when active word size < size of target_long.  */
 195#ifdef TARGET_PPC64
 196# define NARROW_MODE(C)  (!(C)->sf_mode)
 197#else
 198# define NARROW_MODE(C)  0
 199#endif
 200
 201struct opc_handler_t {
 202    /* invalid bits for instruction 1 (Rc(opcode) == 0) */
 203    uint32_t inval1;
 204    /* invalid bits for instruction 2 (Rc(opcode) == 1) */
 205    uint32_t inval2;
 206    /* instruction type */
 207    uint64_t type;
 208    /* extended instruction type */
 209    uint64_t type2;
 210    /* handler */
 211    void (*handler)(DisasContext *ctx);
 212#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
 213    const char *oname;
 214#endif
 215#if defined(DO_PPC_STATISTICS)
 216    uint64_t count;
 217#endif
 218};
 219
 220/* SPR load/store helpers */
 221static inline void gen_load_spr(TCGv t, int reg)
 222{
 223    tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
 224}
 225
 226static inline void gen_store_spr(int reg, TCGv t)
 227{
 228    tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
 229}
 230
 231static inline void gen_set_access_type(DisasContext *ctx, int access_type)
 232{
 233    if (ctx->need_access_type && ctx->access_type != access_type) {
 234        tcg_gen_movi_i32(cpu_access_type, access_type);
 235        ctx->access_type = access_type;
 236    }
 237}
 238
 239static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
 240{
 241    if (NARROW_MODE(ctx)) {
 242        nip = (uint32_t)nip;
 243    }
 244    tcg_gen_movi_tl(cpu_nip, nip);
 245}
 246
 247static void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
 248{
 249    TCGv_i32 t0, t1;
 250
 251    /*
 252     * These are all synchronous exceptions, we set the PC back to the
 253     * faulting instruction
 254     */
 255    if (ctx->exception == POWERPC_EXCP_NONE) {
 256        gen_update_nip(ctx, ctx->base.pc_next - 4);
 257    }
 258    t0 = tcg_const_i32(excp);
 259    t1 = tcg_const_i32(error);
 260    gen_helper_raise_exception_err(cpu_env, t0, t1);
 261    tcg_temp_free_i32(t0);
 262    tcg_temp_free_i32(t1);
 263    ctx->exception = (excp);
 264}
 265
 266static void gen_exception(DisasContext *ctx, uint32_t excp)
 267{
 268    TCGv_i32 t0;
 269
 270    /*
 271     * These are all synchronous exceptions, we set the PC back to the
 272     * faulting instruction
 273     */
 274    if (ctx->exception == POWERPC_EXCP_NONE) {
 275        gen_update_nip(ctx, ctx->base.pc_next - 4);
 276    }
 277    t0 = tcg_const_i32(excp);
 278    gen_helper_raise_exception(cpu_env, t0);
 279    tcg_temp_free_i32(t0);
 280    ctx->exception = (excp);
 281}
 282
 283static void gen_exception_nip(DisasContext *ctx, uint32_t excp,
 284                              target_ulong nip)
 285{
 286    TCGv_i32 t0;
 287
 288    gen_update_nip(ctx, nip);
 289    t0 = tcg_const_i32(excp);
 290    gen_helper_raise_exception(cpu_env, t0);
 291    tcg_temp_free_i32(t0);
 292    ctx->exception = (excp);
 293}
 294
 295/*
 296 * Tells the caller what is the appropriate exception to generate and prepares
 297 * SPR registers for this exception.
 298 *
 299 * The exception can be either POWERPC_EXCP_TRACE (on most PowerPCs) or
 300 * POWERPC_EXCP_DEBUG (on BookE).
 301 */
 302static uint32_t gen_prep_dbgex(DisasContext *ctx)
 303{
 304    if (ctx->flags & POWERPC_FLAG_DE) {
 305        target_ulong dbsr = 0;
 306        if (ctx->singlestep_enabled & CPU_SINGLE_STEP) {
 307            dbsr = DBCR0_ICMP;
 308        } else {
 309            /* Must have been branch */
 310            dbsr = DBCR0_BRT;
 311        }
 312        TCGv t0 = tcg_temp_new();
 313        gen_load_spr(t0, SPR_BOOKE_DBSR);
 314        tcg_gen_ori_tl(t0, t0, dbsr);
 315        gen_store_spr(SPR_BOOKE_DBSR, t0);
 316        tcg_temp_free(t0);
 317        return POWERPC_EXCP_DEBUG;
 318    } else {
 319        return POWERPC_EXCP_TRACE;
 320    }
 321}
 322
 323static void gen_debug_exception(DisasContext *ctx)
 324{
 325    TCGv_i32 t0;
 326
 327    /*
 328     * These are all synchronous exceptions, we set the PC back to the
 329     * faulting instruction
 330     */
 331    if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
 332        (ctx->exception != POWERPC_EXCP_SYNC)) {
 333        gen_update_nip(ctx, ctx->base.pc_next);
 334    }
 335    t0 = tcg_const_i32(EXCP_DEBUG);
 336    gen_helper_raise_exception(cpu_env, t0);
 337    tcg_temp_free_i32(t0);
 338}
 339
 340static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
 341{
 342    /* Will be converted to program check if needed */
 343    gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_INVAL | error);
 344}
 345
 346static inline void gen_priv_exception(DisasContext *ctx, uint32_t error)
 347{
 348    gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_PRIV | error);
 349}
 350
 351static inline void gen_hvpriv_exception(DisasContext *ctx, uint32_t error)
 352{
 353    /* Will be converted to program check if needed */
 354    gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_PRIV | error);
 355}
 356
 357/* Stop translation */
 358static inline void gen_stop_exception(DisasContext *ctx)
 359{
 360    gen_update_nip(ctx, ctx->base.pc_next);
 361    ctx->exception = POWERPC_EXCP_STOP;
 362}
 363
 364#ifndef CONFIG_USER_ONLY
 365/* No need to update nip here, as execution flow will change */
 366static inline void gen_sync_exception(DisasContext *ctx)
 367{
 368    ctx->exception = POWERPC_EXCP_SYNC;
 369}
 370#endif
 371
 372#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
 373GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
 374
 375#define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2)             \
 376GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
 377
 378#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
 379GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
 380
 381#define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2)      \
 382GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
 383
 384#define GEN_HANDLER_E_2(name, opc1, opc2, opc3, opc4, inval, type, type2)     \
 385GEN_OPCODE3(name, opc1, opc2, opc3, opc4, inval, type, type2)
 386
 387#define GEN_HANDLER2_E_2(name, onam, opc1, opc2, opc3, opc4, inval, typ, typ2) \
 388GEN_OPCODE4(name, onam, opc1, opc2, opc3, opc4, inval, typ, typ2)
 389
 390typedef struct opcode_t {
 391    unsigned char opc1, opc2, opc3, opc4;
 392#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
 393    unsigned char pad[4];
 394#endif
 395    opc_handler_t handler;
 396    const char *oname;
 397} opcode_t;
 398
 399/* Helpers for priv. check */
 400#define GEN_PRIV                                                \
 401    do {                                                        \
 402        gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC); return; \
 403    } while (0)
 404
 405#if defined(CONFIG_USER_ONLY)
 406#define CHK_HV GEN_PRIV
 407#define CHK_SV GEN_PRIV
 408#define CHK_HVRM GEN_PRIV
 409#else
 410#define CHK_HV                                                          \
 411    do {                                                                \
 412        if (unlikely(ctx->pr || !ctx->hv)) {                            \
 413            GEN_PRIV;                                                   \
 414        }                                                               \
 415    } while (0)
 416#define CHK_SV                   \
 417    do {                         \
 418        if (unlikely(ctx->pr)) { \
 419            GEN_PRIV;            \
 420        }                        \
 421    } while (0)
 422#define CHK_HVRM                                            \
 423    do {                                                    \
 424        if (unlikely(ctx->pr || !ctx->hv || ctx->dr)) {     \
 425            GEN_PRIV;                                       \
 426        }                                                   \
 427    } while (0)
 428#endif
 429
 430#define CHK_NONE
 431
 432/*****************************************************************************/
 433/* PowerPC instructions table                                                */
 434
 435#if defined(DO_PPC_STATISTICS)
 436#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
 437{                                                                             \
 438    .opc1 = op1,                                                              \
 439    .opc2 = op2,                                                              \
 440    .opc3 = op3,                                                              \
 441    .opc4 = 0xff,                                                             \
 442    .handler = {                                                              \
 443        .inval1  = invl,                                                      \
 444        .type = _typ,                                                         \
 445        .type2 = _typ2,                                                       \
 446        .handler = &gen_##name,                                               \
 447        .oname = stringify(name),                                             \
 448    },                                                                        \
 449    .oname = stringify(name),                                                 \
 450}
 451#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
 452{                                                                             \
 453    .opc1 = op1,                                                              \
 454    .opc2 = op2,                                                              \
 455    .opc3 = op3,                                                              \
 456    .opc4 = 0xff,                                                             \
 457    .handler = {                                                              \
 458        .inval1  = invl1,                                                     \
 459        .inval2  = invl2,                                                     \
 460        .type = _typ,                                                         \
 461        .type2 = _typ2,                                                       \
 462        .handler = &gen_##name,                                               \
 463        .oname = stringify(name),                                             \
 464    },                                                                        \
 465    .oname = stringify(name),                                                 \
 466}
 467#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
 468{                                                                             \
 469    .opc1 = op1,                                                              \
 470    .opc2 = op2,                                                              \
 471    .opc3 = op3,                                                              \
 472    .opc4 = 0xff,                                                             \
 473    .handler = {                                                              \
 474        .inval1  = invl,                                                      \
 475        .type = _typ,                                                         \
 476        .type2 = _typ2,                                                       \
 477        .handler = &gen_##name,                                               \
 478        .oname = onam,                                                        \
 479    },                                                                        \
 480    .oname = onam,                                                            \
 481}
 482#define GEN_OPCODE3(name, op1, op2, op3, op4, invl, _typ, _typ2)              \
 483{                                                                             \
 484    .opc1 = op1,                                                              \
 485    .opc2 = op2,                                                              \
 486    .opc3 = op3,                                                              \
 487    .opc4 = op4,                                                              \
 488    .handler = {                                                              \
 489        .inval1  = invl,                                                      \
 490        .type = _typ,                                                         \
 491        .type2 = _typ2,                                                       \
 492        .handler = &gen_##name,                                               \
 493        .oname = stringify(name),                                             \
 494    },                                                                        \
 495    .oname = stringify(name),                                                 \
 496}
 497#define GEN_OPCODE4(name, onam, op1, op2, op3, op4, invl, _typ, _typ2)        \
 498{                                                                             \
 499    .opc1 = op1,                                                              \
 500    .opc2 = op2,                                                              \
 501    .opc3 = op3,                                                              \
 502    .opc4 = op4,                                                              \
 503    .handler = {                                                              \
 504        .inval1  = invl,                                                      \
 505        .type = _typ,                                                         \
 506        .type2 = _typ2,                                                       \
 507        .handler = &gen_##name,                                               \
 508        .oname = onam,                                                        \
 509    },                                                                        \
 510    .oname = onam,                                                            \
 511}
 512#else
 513#define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
 514{                                                                             \
 515    .opc1 = op1,                                                              \
 516    .opc2 = op2,                                                              \
 517    .opc3 = op3,                                                              \
 518    .opc4 = 0xff,                                                             \
 519    .handler = {                                                              \
 520        .inval1  = invl,                                                      \
 521        .type = _typ,                                                         \
 522        .type2 = _typ2,                                                       \
 523        .handler = &gen_##name,                                               \
 524    },                                                                        \
 525    .oname = stringify(name),                                                 \
 526}
 527#define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
 528{                                                                             \
 529    .opc1 = op1,                                                              \
 530    .opc2 = op2,                                                              \
 531    .opc3 = op3,                                                              \
 532    .opc4 = 0xff,                                                             \
 533    .handler = {                                                              \
 534        .inval1  = invl1,                                                     \
 535        .inval2  = invl2,                                                     \
 536        .type = _typ,                                                         \
 537        .type2 = _typ2,                                                       \
 538        .handler = &gen_##name,                                               \
 539    },                                                                        \
 540    .oname = stringify(name),                                                 \
 541}
 542#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
 543{                                                                             \
 544    .opc1 = op1,                                                              \
 545    .opc2 = op2,                                                              \
 546    .opc3 = op3,                                                              \
 547    .opc4 = 0xff,                                                             \
 548    .handler = {                                                              \
 549        .inval1  = invl,                                                      \
 550        .type = _typ,                                                         \
 551        .type2 = _typ2,                                                       \
 552        .handler = &gen_##name,                                               \
 553    },                                                                        \
 554    .oname = onam,                                                            \
 555}
 556#define GEN_OPCODE3(name, op1, op2, op3, op4, invl, _typ, _typ2)              \
 557{                                                                             \
 558    .opc1 = op1,                                                              \
 559    .opc2 = op2,                                                              \
 560    .opc3 = op3,                                                              \
 561    .opc4 = op4,                                                              \
 562    .handler = {                                                              \
 563        .inval1  = invl,                                                      \
 564        .type = _typ,                                                         \
 565        .type2 = _typ2,                                                       \
 566        .handler = &gen_##name,                                               \
 567    },                                                                        \
 568    .oname = stringify(name),                                                 \
 569}
 570#define GEN_OPCODE4(name, onam, op1, op2, op3, op4, invl, _typ, _typ2)        \
 571{                                                                             \
 572    .opc1 = op1,                                                              \
 573    .opc2 = op2,                                                              \
 574    .opc3 = op3,                                                              \
 575    .opc4 = op4,                                                              \
 576    .handler = {                                                              \
 577        .inval1  = invl,                                                      \
 578        .type = _typ,                                                         \
 579        .type2 = _typ2,                                                       \
 580        .handler = &gen_##name,                                               \
 581    },                                                                        \
 582    .oname = onam,                                                            \
 583}
 584#endif
 585
 586/* Invalid instruction */
 587static void gen_invalid(DisasContext *ctx)
 588{
 589    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
 590}
 591
 592static opc_handler_t invalid_handler = {
 593    .inval1  = 0xFFFFFFFF,
 594    .inval2  = 0xFFFFFFFF,
 595    .type    = PPC_NONE,
 596    .type2   = PPC_NONE,
 597    .handler = gen_invalid,
 598};
 599
 600/***                           Integer comparison                          ***/
 601
 602static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
 603{
 604    TCGv t0 = tcg_temp_new();
 605    TCGv t1 = tcg_temp_new();
 606    TCGv_i32 t = tcg_temp_new_i32();
 607
 608    tcg_gen_movi_tl(t0, CRF_EQ);
 609    tcg_gen_movi_tl(t1, CRF_LT);
 610    tcg_gen_movcond_tl((s ? TCG_COND_LT : TCG_COND_LTU),
 611                       t0, arg0, arg1, t1, t0);
 612    tcg_gen_movi_tl(t1, CRF_GT);
 613    tcg_gen_movcond_tl((s ? TCG_COND_GT : TCG_COND_GTU),
 614                       t0, arg0, arg1, t1, t0);
 615
 616    tcg_gen_trunc_tl_i32(t, t0);
 617    tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
 618    tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t);
 619
 620    tcg_temp_free(t0);
 621    tcg_temp_free(t1);
 622    tcg_temp_free_i32(t);
 623}
 624
 625static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
 626{
 627    TCGv t0 = tcg_const_tl(arg1);
 628    gen_op_cmp(arg0, t0, s, crf);
 629    tcg_temp_free(t0);
 630}
 631
 632static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
 633{
 634    TCGv t0, t1;
 635    t0 = tcg_temp_new();
 636    t1 = tcg_temp_new();
 637    if (s) {
 638        tcg_gen_ext32s_tl(t0, arg0);
 639        tcg_gen_ext32s_tl(t1, arg1);
 640    } else {
 641        tcg_gen_ext32u_tl(t0, arg0);
 642        tcg_gen_ext32u_tl(t1, arg1);
 643    }
 644    gen_op_cmp(t0, t1, s, crf);
 645    tcg_temp_free(t1);
 646    tcg_temp_free(t0);
 647}
 648
 649static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
 650{
 651    TCGv t0 = tcg_const_tl(arg1);
 652    gen_op_cmp32(arg0, t0, s, crf);
 653    tcg_temp_free(t0);
 654}
 655
 656static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 657{
 658    if (NARROW_MODE(ctx)) {
 659        gen_op_cmpi32(reg, 0, 1, 0);
 660    } else {
 661        gen_op_cmpi(reg, 0, 1, 0);
 662    }
 663}
 664
 665/* cmp */
 666static void gen_cmp(DisasContext *ctx)
 667{
 668    if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
 669        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
 670                   1, crfD(ctx->opcode));
 671    } else {
 672        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
 673                     1, crfD(ctx->opcode));
 674    }
 675}
 676
 677/* cmpi */
 678static void gen_cmpi(DisasContext *ctx)
 679{
 680    if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
 681        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
 682                    1, crfD(ctx->opcode));
 683    } else {
 684        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
 685                      1, crfD(ctx->opcode));
 686    }
 687}
 688
 689/* cmpl */
 690static void gen_cmpl(DisasContext *ctx)
 691{
 692    if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
 693        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
 694                   0, crfD(ctx->opcode));
 695    } else {
 696        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
 697                     0, crfD(ctx->opcode));
 698    }
 699}
 700
 701/* cmpli */
 702static void gen_cmpli(DisasContext *ctx)
 703{
 704    if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
 705        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
 706                    0, crfD(ctx->opcode));
 707    } else {
 708        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
 709                      0, crfD(ctx->opcode));
 710    }
 711}
 712
 713/* cmprb - range comparison: isupper, isaplha, islower*/
 714static void gen_cmprb(DisasContext *ctx)
 715{
 716    TCGv_i32 src1 = tcg_temp_new_i32();
 717    TCGv_i32 src2 = tcg_temp_new_i32();
 718    TCGv_i32 src2lo = tcg_temp_new_i32();
 719    TCGv_i32 src2hi = tcg_temp_new_i32();
 720    TCGv_i32 crf = cpu_crf[crfD(ctx->opcode)];
 721
 722    tcg_gen_trunc_tl_i32(src1, cpu_gpr[rA(ctx->opcode)]);
 723    tcg_gen_trunc_tl_i32(src2, cpu_gpr[rB(ctx->opcode)]);
 724
 725    tcg_gen_andi_i32(src1, src1, 0xFF);
 726    tcg_gen_ext8u_i32(src2lo, src2);
 727    tcg_gen_shri_i32(src2, src2, 8);
 728    tcg_gen_ext8u_i32(src2hi, src2);
 729
 730    tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
 731    tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
 732    tcg_gen_and_i32(crf, src2lo, src2hi);
 733
 734    if (ctx->opcode & 0x00200000) {
 735        tcg_gen_shri_i32(src2, src2, 8);
 736        tcg_gen_ext8u_i32(src2lo, src2);
 737        tcg_gen_shri_i32(src2, src2, 8);
 738        tcg_gen_ext8u_i32(src2hi, src2);
 739        tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
 740        tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
 741        tcg_gen_and_i32(src2lo, src2lo, src2hi);
 742        tcg_gen_or_i32(crf, crf, src2lo);
 743    }
 744    tcg_gen_shli_i32(crf, crf, CRF_GT_BIT);
 745    tcg_temp_free_i32(src1);
 746    tcg_temp_free_i32(src2);
 747    tcg_temp_free_i32(src2lo);
 748    tcg_temp_free_i32(src2hi);
 749}
 750
 751#if defined(TARGET_PPC64)
 752/* cmpeqb */
 753static void gen_cmpeqb(DisasContext *ctx)
 754{
 755    gen_helper_cmpeqb(cpu_crf[crfD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
 756                      cpu_gpr[rB(ctx->opcode)]);
 757}
 758#endif
 759
 760/* isel (PowerPC 2.03 specification) */
 761static void gen_isel(DisasContext *ctx)
 762{
 763    uint32_t bi = rC(ctx->opcode);
 764    uint32_t mask = 0x08 >> (bi & 0x03);
 765    TCGv t0 = tcg_temp_new();
 766    TCGv zr;
 767
 768    tcg_gen_extu_i32_tl(t0, cpu_crf[bi >> 2]);
 769    tcg_gen_andi_tl(t0, t0, mask);
 770
 771    zr = tcg_const_tl(0);
 772    tcg_gen_movcond_tl(TCG_COND_NE, cpu_gpr[rD(ctx->opcode)], t0, zr,
 773                       rA(ctx->opcode) ? cpu_gpr[rA(ctx->opcode)] : zr,
 774                       cpu_gpr[rB(ctx->opcode)]);
 775    tcg_temp_free(zr);
 776    tcg_temp_free(t0);
 777}
 778
 779/* cmpb: PowerPC 2.05 specification */
 780static void gen_cmpb(DisasContext *ctx)
 781{
 782    gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
 783                    cpu_gpr[rB(ctx->opcode)]);
 784}
 785
 786/***                           Integer arithmetic                          ***/
 787
 788static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
 789                                           TCGv arg1, TCGv arg2, int sub)
 790{
 791    TCGv t0 = tcg_temp_new();
 792
 793    tcg_gen_xor_tl(cpu_ov, arg0, arg2);
 794    tcg_gen_xor_tl(t0, arg1, arg2);
 795    if (sub) {
 796        tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
 797    } else {
 798        tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
 799    }
 800    tcg_temp_free(t0);
 801    if (NARROW_MODE(ctx)) {
 802        tcg_gen_extract_tl(cpu_ov, cpu_ov, 31, 1);
 803        if (is_isa300(ctx)) {
 804            tcg_gen_mov_tl(cpu_ov32, cpu_ov);
 805        }
 806    } else {
 807        if (is_isa300(ctx)) {
 808            tcg_gen_extract_tl(cpu_ov32, cpu_ov, 31, 1);
 809        }
 810        tcg_gen_extract_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1, 1);
 811    }
 812    tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
 813}
 814
 815static inline void gen_op_arith_compute_ca32(DisasContext *ctx,
 816                                             TCGv res, TCGv arg0, TCGv arg1,
 817                                             TCGv ca32, int sub)
 818{
 819    TCGv t0;
 820
 821    if (!is_isa300(ctx)) {
 822        return;
 823    }
 824
 825    t0 = tcg_temp_new();
 826    if (sub) {
 827        tcg_gen_eqv_tl(t0, arg0, arg1);
 828    } else {
 829        tcg_gen_xor_tl(t0, arg0, arg1);
 830    }
 831    tcg_gen_xor_tl(t0, t0, res);
 832    tcg_gen_extract_tl(ca32, t0, 32, 1);
 833    tcg_temp_free(t0);
 834}
 835
 836/* Common add function */
 837static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
 838                                    TCGv arg2, TCGv ca, TCGv ca32,
 839                                    bool add_ca, bool compute_ca,
 840                                    bool compute_ov, bool compute_rc0)
 841{
 842    TCGv t0 = ret;
 843
 844    if (compute_ca || compute_ov) {
 845        t0 = tcg_temp_new();
 846    }
 847
 848    if (compute_ca) {
 849        if (NARROW_MODE(ctx)) {
 850            /*
 851             * Caution: a non-obvious corner case of the spec is that
 852             * we must produce the *entire* 64-bit addition, but
 853             * produce the carry into bit 32.
 854             */
 855            TCGv t1 = tcg_temp_new();
 856            tcg_gen_xor_tl(t1, arg1, arg2);        /* add without carry */
 857            tcg_gen_add_tl(t0, arg1, arg2);
 858            if (add_ca) {
 859                tcg_gen_add_tl(t0, t0, ca);
 860            }
 861            tcg_gen_xor_tl(ca, t0, t1);        /* bits changed w/ carry */
 862            tcg_temp_free(t1);
 863            tcg_gen_extract_tl(ca, ca, 32, 1);
 864            if (is_isa300(ctx)) {
 865                tcg_gen_mov_tl(ca32, ca);
 866            }
 867        } else {
 868            TCGv zero = tcg_const_tl(0);
 869            if (add_ca) {
 870                tcg_gen_add2_tl(t0, ca, arg1, zero, ca, zero);
 871                tcg_gen_add2_tl(t0, ca, t0, ca, arg2, zero);
 872            } else {
 873                tcg_gen_add2_tl(t0, ca, arg1, zero, arg2, zero);
 874            }
 875            gen_op_arith_compute_ca32(ctx, t0, arg1, arg2, ca32, 0);
 876            tcg_temp_free(zero);
 877        }
 878    } else {
 879        tcg_gen_add_tl(t0, arg1, arg2);
 880        if (add_ca) {
 881            tcg_gen_add_tl(t0, t0, ca);
 882        }
 883    }
 884
 885    if (compute_ov) {
 886        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
 887    }
 888    if (unlikely(compute_rc0)) {
 889        gen_set_Rc0(ctx, t0);
 890    }
 891
 892    if (t0 != ret) {
 893        tcg_gen_mov_tl(ret, t0);
 894        tcg_temp_free(t0);
 895    }
 896}
 897/* Add functions with two operands */
 898#define GEN_INT_ARITH_ADD(name, opc3, ca, add_ca, compute_ca, compute_ov)     \
 899static void glue(gen_, name)(DisasContext *ctx)                               \
 900{                                                                             \
 901    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
 902                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
 903                     ca, glue(ca, 32),                                        \
 904                     add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
 905}
 906/* Add functions with one operand and one immediate */
 907#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, ca,                    \
 908                                add_ca, compute_ca, compute_ov)               \
 909static void glue(gen_, name)(DisasContext *ctx)                               \
 910{                                                                             \
 911    TCGv t0 = tcg_const_tl(const_val);                                        \
 912    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
 913                     cpu_gpr[rA(ctx->opcode)], t0,                            \
 914                     ca, glue(ca, 32),                                        \
 915                     add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
 916    tcg_temp_free(t0);                                                        \
 917}
 918
 919/* add  add.  addo  addo. */
 920GEN_INT_ARITH_ADD(add, 0x08, cpu_ca, 0, 0, 0)
 921GEN_INT_ARITH_ADD(addo, 0x18, cpu_ca, 0, 0, 1)
 922/* addc  addc.  addco  addco. */
 923GEN_INT_ARITH_ADD(addc, 0x00, cpu_ca, 0, 1, 0)
 924GEN_INT_ARITH_ADD(addco, 0x10, cpu_ca, 0, 1, 1)
 925/* adde  adde.  addeo  addeo. */
 926GEN_INT_ARITH_ADD(adde, 0x04, cpu_ca, 1, 1, 0)
 927GEN_INT_ARITH_ADD(addeo, 0x14, cpu_ca, 1, 1, 1)
 928/* addme  addme.  addmeo  addmeo.  */
 929GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, cpu_ca, 1, 1, 0)
 930GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, cpu_ca, 1, 1, 1)
 931/* addex */
 932GEN_INT_ARITH_ADD(addex, 0x05, cpu_ov, 1, 1, 0);
 933/* addze  addze.  addzeo  addzeo.*/
 934GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, cpu_ca, 1, 1, 0)
 935GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, cpu_ca, 1, 1, 1)
 936/* addi */
 937static void gen_addi(DisasContext *ctx)
 938{
 939    target_long simm = SIMM(ctx->opcode);
 940
 941    if (rA(ctx->opcode) == 0) {
 942        /* li case */
 943        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
 944    } else {
 945        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
 946                        cpu_gpr[rA(ctx->opcode)], simm);
 947    }
 948}
 949/* addic  addic.*/
 950static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
 951{
 952    TCGv c = tcg_const_tl(SIMM(ctx->opcode));
 953    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
 954                     c, cpu_ca, cpu_ca32, 0, 1, 0, compute_rc0);
 955    tcg_temp_free(c);
 956}
 957
 958static void gen_addic(DisasContext *ctx)
 959{
 960    gen_op_addic(ctx, 0);
 961}
 962
 963static void gen_addic_(DisasContext *ctx)
 964{
 965    gen_op_addic(ctx, 1);
 966}
 967
 968/* addis */
 969static void gen_addis(DisasContext *ctx)
 970{
 971    target_long simm = SIMM(ctx->opcode);
 972
 973    if (rA(ctx->opcode) == 0) {
 974        /* lis case */
 975        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
 976    } else {
 977        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
 978                        cpu_gpr[rA(ctx->opcode)], simm << 16);
 979    }
 980}
 981
 982/* addpcis */
 983static void gen_addpcis(DisasContext *ctx)
 984{
 985    target_long d = DX(ctx->opcode);
 986
 987    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], ctx->base.pc_next + (d << 16));
 988}
 989
 990static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
 991                                     TCGv arg2, int sign, int compute_ov)
 992{
 993    TCGv_i32 t0 = tcg_temp_new_i32();
 994    TCGv_i32 t1 = tcg_temp_new_i32();
 995    TCGv_i32 t2 = tcg_temp_new_i32();
 996    TCGv_i32 t3 = tcg_temp_new_i32();
 997
 998    tcg_gen_trunc_tl_i32(t0, arg1);
 999    tcg_gen_trunc_tl_i32(t1, arg2);
1000    if (sign) {
1001        tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1002        tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1003        tcg_gen_and_i32(t2, t2, t3);
1004        tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1005        tcg_gen_or_i32(t2, t2, t3);
1006        tcg_gen_movi_i32(t3, 0);
1007        tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1008        tcg_gen_div_i32(t3, t0, t1);
1009        tcg_gen_extu_i32_tl(ret, t3);
1010    } else {
1011        tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t1, 0);
1012        tcg_gen_movi_i32(t3, 0);
1013        tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1014        tcg_gen_divu_i32(t3, t0, t1);
1015        tcg_gen_extu_i32_tl(ret, t3);
1016    }
1017    if (compute_ov) {
1018        tcg_gen_extu_i32_tl(cpu_ov, t2);
1019        if (is_isa300(ctx)) {
1020            tcg_gen_extu_i32_tl(cpu_ov32, t2);
1021        }
1022        tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1023    }
1024    tcg_temp_free_i32(t0);
1025    tcg_temp_free_i32(t1);
1026    tcg_temp_free_i32(t2);
1027    tcg_temp_free_i32(t3);
1028
1029    if (unlikely(Rc(ctx->opcode) != 0)) {
1030        gen_set_Rc0(ctx, ret);
1031    }
1032}
1033/* Div functions */
1034#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
1035static void glue(gen_, name)(DisasContext *ctx)                               \
1036{                                                                             \
1037    gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1038                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
1039                     sign, compute_ov);                                       \
1040}
1041/* divwu  divwu.  divwuo  divwuo.   */
1042GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1043GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1044/* divw  divw.  divwo  divwo.   */
1045GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1046GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1047
1048/* div[wd]eu[o][.] */
1049#define GEN_DIVE(name, hlpr, compute_ov)                                      \
1050static void gen_##name(DisasContext *ctx)                                     \
1051{                                                                             \
1052    TCGv_i32 t0 = tcg_const_i32(compute_ov);                                  \
1053    gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
1054                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1055    tcg_temp_free_i32(t0);                                                    \
1056    if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
1057        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1058    }                                                                         \
1059}
1060
1061GEN_DIVE(divweu, divweu, 0);
1062GEN_DIVE(divweuo, divweu, 1);
1063GEN_DIVE(divwe, divwe, 0);
1064GEN_DIVE(divweo, divwe, 1);
1065
1066#if defined(TARGET_PPC64)
1067static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1068                                     TCGv arg2, int sign, int compute_ov)
1069{
1070    TCGv_i64 t0 = tcg_temp_new_i64();
1071    TCGv_i64 t1 = tcg_temp_new_i64();
1072    TCGv_i64 t2 = tcg_temp_new_i64();
1073    TCGv_i64 t3 = tcg_temp_new_i64();
1074
1075    tcg_gen_mov_i64(t0, arg1);
1076    tcg_gen_mov_i64(t1, arg2);
1077    if (sign) {
1078        tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t0, INT64_MIN);
1079        tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, -1);
1080        tcg_gen_and_i64(t2, t2, t3);
1081        tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, 0);
1082        tcg_gen_or_i64(t2, t2, t3);
1083        tcg_gen_movi_i64(t3, 0);
1084        tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1085        tcg_gen_div_i64(ret, t0, t1);
1086    } else {
1087        tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t1, 0);
1088        tcg_gen_movi_i64(t3, 0);
1089        tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1090        tcg_gen_divu_i64(ret, t0, t1);
1091    }
1092    if (compute_ov) {
1093        tcg_gen_mov_tl(cpu_ov, t2);
1094        if (is_isa300(ctx)) {
1095            tcg_gen_mov_tl(cpu_ov32, t2);
1096        }
1097        tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1098    }
1099    tcg_temp_free_i64(t0);
1100    tcg_temp_free_i64(t1);
1101    tcg_temp_free_i64(t2);
1102    tcg_temp_free_i64(t3);
1103
1104    if (unlikely(Rc(ctx->opcode) != 0)) {
1105        gen_set_Rc0(ctx, ret);
1106    }
1107}
1108
1109#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1110static void glue(gen_, name)(DisasContext *ctx)                               \
1111{                                                                             \
1112    gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1113                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1114                      sign, compute_ov);                                      \
1115}
1116/* divdu  divdu.  divduo  divduo.   */
1117GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1118GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1119/* divd  divd.  divdo  divdo.   */
1120GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1121GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1122
1123GEN_DIVE(divdeu, divdeu, 0);
1124GEN_DIVE(divdeuo, divdeu, 1);
1125GEN_DIVE(divde, divde, 0);
1126GEN_DIVE(divdeo, divde, 1);
1127#endif
1128
1129static inline void gen_op_arith_modw(DisasContext *ctx, TCGv ret, TCGv arg1,
1130                                     TCGv arg2, int sign)
1131{
1132    TCGv_i32 t0 = tcg_temp_new_i32();
1133    TCGv_i32 t1 = tcg_temp_new_i32();
1134
1135    tcg_gen_trunc_tl_i32(t0, arg1);
1136    tcg_gen_trunc_tl_i32(t1, arg2);
1137    if (sign) {
1138        TCGv_i32 t2 = tcg_temp_new_i32();
1139        TCGv_i32 t3 = tcg_temp_new_i32();
1140        tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1141        tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1142        tcg_gen_and_i32(t2, t2, t3);
1143        tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1144        tcg_gen_or_i32(t2, t2, t3);
1145        tcg_gen_movi_i32(t3, 0);
1146        tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1147        tcg_gen_rem_i32(t3, t0, t1);
1148        tcg_gen_ext_i32_tl(ret, t3);
1149        tcg_temp_free_i32(t2);
1150        tcg_temp_free_i32(t3);
1151    } else {
1152        TCGv_i32 t2 = tcg_const_i32(1);
1153        TCGv_i32 t3 = tcg_const_i32(0);
1154        tcg_gen_movcond_i32(TCG_COND_EQ, t1, t1, t3, t2, t1);
1155        tcg_gen_remu_i32(t3, t0, t1);
1156        tcg_gen_extu_i32_tl(ret, t3);
1157        tcg_temp_free_i32(t2);
1158        tcg_temp_free_i32(t3);
1159    }
1160    tcg_temp_free_i32(t0);
1161    tcg_temp_free_i32(t1);
1162}
1163
1164#define GEN_INT_ARITH_MODW(name, opc3, sign)                                \
1165static void glue(gen_, name)(DisasContext *ctx)                             \
1166{                                                                           \
1167    gen_op_arith_modw(ctx, cpu_gpr[rD(ctx->opcode)],                        \
1168                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],   \
1169                      sign);                                                \
1170}
1171
1172GEN_INT_ARITH_MODW(moduw, 0x08, 0);
1173GEN_INT_ARITH_MODW(modsw, 0x18, 1);
1174
1175#if defined(TARGET_PPC64)
1176static inline void gen_op_arith_modd(DisasContext *ctx, TCGv ret, TCGv arg1,
1177                                     TCGv arg2, int sign)
1178{
1179    TCGv_i64 t0 = tcg_temp_new_i64();
1180    TCGv_i64 t1 = tcg_temp_new_i64();
1181
1182    tcg_gen_mov_i64(t0, arg1);
1183    tcg_gen_mov_i64(t1, arg2);
1184    if (sign) {
1185        TCGv_i64 t2 = tcg_temp_new_i64();
1186        TCGv_i64 t3 = tcg_temp_new_i64();
1187        tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t0, INT64_MIN);
1188        tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, -1);
1189        tcg_gen_and_i64(t2, t2, t3);
1190        tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, 0);
1191        tcg_gen_or_i64(t2, t2, t3);
1192        tcg_gen_movi_i64(t3, 0);
1193        tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1194        tcg_gen_rem_i64(ret, t0, t1);
1195        tcg_temp_free_i64(t2);
1196        tcg_temp_free_i64(t3);
1197    } else {
1198        TCGv_i64 t2 = tcg_const_i64(1);
1199        TCGv_i64 t3 = tcg_const_i64(0);
1200        tcg_gen_movcond_i64(TCG_COND_EQ, t1, t1, t3, t2, t1);
1201        tcg_gen_remu_i64(ret, t0, t1);
1202        tcg_temp_free_i64(t2);
1203        tcg_temp_free_i64(t3);
1204    }
1205    tcg_temp_free_i64(t0);
1206    tcg_temp_free_i64(t1);
1207}
1208
1209#define GEN_INT_ARITH_MODD(name, opc3, sign)                            \
1210static void glue(gen_, name)(DisasContext *ctx)                           \
1211{                                                                         \
1212  gen_op_arith_modd(ctx, cpu_gpr[rD(ctx->opcode)],                        \
1213                    cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],   \
1214                    sign);                                                \
1215}
1216
1217GEN_INT_ARITH_MODD(modud, 0x08, 0);
1218GEN_INT_ARITH_MODD(modsd, 0x18, 1);
1219#endif
1220
1221/* mulhw  mulhw. */
1222static void gen_mulhw(DisasContext *ctx)
1223{
1224    TCGv_i32 t0 = tcg_temp_new_i32();
1225    TCGv_i32 t1 = tcg_temp_new_i32();
1226
1227    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1228    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1229    tcg_gen_muls2_i32(t0, t1, t0, t1);
1230    tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1231    tcg_temp_free_i32(t0);
1232    tcg_temp_free_i32(t1);
1233    if (unlikely(Rc(ctx->opcode) != 0)) {
1234        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1235    }
1236}
1237
1238/* mulhwu  mulhwu.  */
1239static void gen_mulhwu(DisasContext *ctx)
1240{
1241    TCGv_i32 t0 = tcg_temp_new_i32();
1242    TCGv_i32 t1 = tcg_temp_new_i32();
1243
1244    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1245    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1246    tcg_gen_mulu2_i32(t0, t1, t0, t1);
1247    tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1248    tcg_temp_free_i32(t0);
1249    tcg_temp_free_i32(t1);
1250    if (unlikely(Rc(ctx->opcode) != 0)) {
1251        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1252    }
1253}
1254
1255/* mullw  mullw. */
1256static void gen_mullw(DisasContext *ctx)
1257{
1258#if defined(TARGET_PPC64)
1259    TCGv_i64 t0, t1;
1260    t0 = tcg_temp_new_i64();
1261    t1 = tcg_temp_new_i64();
1262    tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1263    tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1264    tcg_gen_mul_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1265    tcg_temp_free(t0);
1266    tcg_temp_free(t1);
1267#else
1268    tcg_gen_mul_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1269                    cpu_gpr[rB(ctx->opcode)]);
1270#endif
1271    if (unlikely(Rc(ctx->opcode) != 0)) {
1272        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1273    }
1274}
1275
1276/* mullwo  mullwo. */
1277static void gen_mullwo(DisasContext *ctx)
1278{
1279    TCGv_i32 t0 = tcg_temp_new_i32();
1280    TCGv_i32 t1 = tcg_temp_new_i32();
1281
1282    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1283    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1284    tcg_gen_muls2_i32(t0, t1, t0, t1);
1285#if defined(TARGET_PPC64)
1286    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1287#else
1288    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], t0);
1289#endif
1290
1291    tcg_gen_sari_i32(t0, t0, 31);
1292    tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1293    tcg_gen_extu_i32_tl(cpu_ov, t0);
1294    if (is_isa300(ctx)) {
1295        tcg_gen_mov_tl(cpu_ov32, cpu_ov);
1296    }
1297    tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1298
1299    tcg_temp_free_i32(t0);
1300    tcg_temp_free_i32(t1);
1301    if (unlikely(Rc(ctx->opcode) != 0)) {
1302        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1303    }
1304}
1305
1306/* mulli */
1307static void gen_mulli(DisasContext *ctx)
1308{
1309    tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1310                    SIMM(ctx->opcode));
1311}
1312
1313#if defined(TARGET_PPC64)
1314/* mulhd  mulhd. */
1315static void gen_mulhd(DisasContext *ctx)
1316{
1317    TCGv lo = tcg_temp_new();
1318    tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1319                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1320    tcg_temp_free(lo);
1321    if (unlikely(Rc(ctx->opcode) != 0)) {
1322        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1323    }
1324}
1325
1326/* mulhdu  mulhdu. */
1327static void gen_mulhdu(DisasContext *ctx)
1328{
1329    TCGv lo = tcg_temp_new();
1330    tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1331                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1332    tcg_temp_free(lo);
1333    if (unlikely(Rc(ctx->opcode) != 0)) {
1334        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1335    }
1336}
1337
1338/* mulld  mulld. */
1339static void gen_mulld(DisasContext *ctx)
1340{
1341    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1342                   cpu_gpr[rB(ctx->opcode)]);
1343    if (unlikely(Rc(ctx->opcode) != 0)) {
1344        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1345    }
1346}
1347
1348/* mulldo  mulldo. */
1349static void gen_mulldo(DisasContext *ctx)
1350{
1351    TCGv_i64 t0 = tcg_temp_new_i64();
1352    TCGv_i64 t1 = tcg_temp_new_i64();
1353
1354    tcg_gen_muls2_i64(t0, t1, cpu_gpr[rA(ctx->opcode)],
1355                      cpu_gpr[rB(ctx->opcode)]);
1356    tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], t0);
1357
1358    tcg_gen_sari_i64(t0, t0, 63);
1359    tcg_gen_setcond_i64(TCG_COND_NE, cpu_ov, t0, t1);
1360    if (is_isa300(ctx)) {
1361        tcg_gen_mov_tl(cpu_ov32, cpu_ov);
1362    }
1363    tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1364
1365    tcg_temp_free_i64(t0);
1366    tcg_temp_free_i64(t1);
1367
1368    if (unlikely(Rc(ctx->opcode) != 0)) {
1369        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1370    }
1371}
1372#endif
1373
1374/* Common subf function */
1375static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
1376                                     TCGv arg2, bool add_ca, bool compute_ca,
1377                                     bool compute_ov, bool compute_rc0)
1378{
1379    TCGv t0 = ret;
1380
1381    if (compute_ca || compute_ov) {
1382        t0 = tcg_temp_new();
1383    }
1384
1385    if (compute_ca) {
1386        /* dest = ~arg1 + arg2 [+ ca].  */
1387        if (NARROW_MODE(ctx)) {
1388            /*
1389             * Caution: a non-obvious corner case of the spec is that
1390             * we must produce the *entire* 64-bit addition, but
1391             * produce the carry into bit 32.
1392             */
1393            TCGv inv1 = tcg_temp_new();
1394            TCGv t1 = tcg_temp_new();
1395            tcg_gen_not_tl(inv1, arg1);
1396            if (add_ca) {
1397                tcg_gen_add_tl(t0, arg2, cpu_ca);
1398            } else {
1399                tcg_gen_addi_tl(t0, arg2, 1);
1400            }
1401            tcg_gen_xor_tl(t1, arg2, inv1);         /* add without carry */
1402            tcg_gen_add_tl(t0, t0, inv1);
1403            tcg_temp_free(inv1);
1404            tcg_gen_xor_tl(cpu_ca, t0, t1);         /* bits changes w/ carry */
1405            tcg_temp_free(t1);
1406            tcg_gen_extract_tl(cpu_ca, cpu_ca, 32, 1);
1407            if (is_isa300(ctx)) {
1408                tcg_gen_mov_tl(cpu_ca32, cpu_ca);
1409            }
1410        } else if (add_ca) {
1411            TCGv zero, inv1 = tcg_temp_new();
1412            tcg_gen_not_tl(inv1, arg1);
1413            zero = tcg_const_tl(0);
1414            tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
1415            tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
1416            gen_op_arith_compute_ca32(ctx, t0, inv1, arg2, cpu_ca32, 0);
1417            tcg_temp_free(zero);
1418            tcg_temp_free(inv1);
1419        } else {
1420            tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
1421            tcg_gen_sub_tl(t0, arg2, arg1);
1422            gen_op_arith_compute_ca32(ctx, t0, arg1, arg2, cpu_ca32, 1);
1423        }
1424    } else if (add_ca) {
1425        /*
1426         * Since we're ignoring carry-out, we can simplify the
1427         * standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.
1428         */
1429        tcg_gen_sub_tl(t0, arg2, arg1);
1430        tcg_gen_add_tl(t0, t0, cpu_ca);
1431        tcg_gen_subi_tl(t0, t0, 1);
1432    } else {
1433        tcg_gen_sub_tl(t0, arg2, arg1);
1434    }
1435
1436    if (compute_ov) {
1437        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1438    }
1439    if (unlikely(compute_rc0)) {
1440        gen_set_Rc0(ctx, t0);
1441    }
1442
1443    if (t0 != ret) {
1444        tcg_gen_mov_tl(ret, t0);
1445        tcg_temp_free(t0);
1446    }
1447}
1448/* Sub functions with Two operands functions */
1449#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1450static void glue(gen_, name)(DisasContext *ctx)                               \
1451{                                                                             \
1452    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1453                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1454                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1455}
1456/* Sub functions with one operand and one immediate */
1457#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1458                                add_ca, compute_ca, compute_ov)               \
1459static void glue(gen_, name)(DisasContext *ctx)                               \
1460{                                                                             \
1461    TCGv t0 = tcg_const_tl(const_val);                                        \
1462    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1463                      cpu_gpr[rA(ctx->opcode)], t0,                           \
1464                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1465    tcg_temp_free(t0);                                                        \
1466}
1467/* subf  subf.  subfo  subfo. */
1468GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1469GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1470/* subfc  subfc.  subfco  subfco. */
1471GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1472GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1473/* subfe  subfe.  subfeo  subfo. */
1474GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1475GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1476/* subfme  subfme.  subfmeo  subfmeo.  */
1477GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1478GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1479/* subfze  subfze.  subfzeo  subfzeo.*/
1480GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1481GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1482
1483/* subfic */
1484static void gen_subfic(DisasContext *ctx)
1485{
1486    TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1487    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1488                      c, 0, 1, 0, 0);
1489    tcg_temp_free(c);
1490}
1491
1492/* neg neg. nego nego. */
1493static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1494{
1495    TCGv zero = tcg_const_tl(0);
1496    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1497                      zero, 0, 0, compute_ov, Rc(ctx->opcode));
1498    tcg_temp_free(zero);
1499}
1500
1501static void gen_neg(DisasContext *ctx)
1502{
1503    tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1504    if (unlikely(Rc(ctx->opcode))) {
1505        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1506    }
1507}
1508
1509static void gen_nego(DisasContext *ctx)
1510{
1511    gen_op_arith_neg(ctx, 1);
1512}
1513
1514/***                            Integer logical                            ***/
1515#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1516static void glue(gen_, name)(DisasContext *ctx)                               \
1517{                                                                             \
1518    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1519       cpu_gpr[rB(ctx->opcode)]);                                             \
1520    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1521        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1522}
1523
1524#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1525static void glue(gen_, name)(DisasContext *ctx)                               \
1526{                                                                             \
1527    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1528    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1529        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1530}
1531
1532/* and & and. */
1533GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1534/* andc & andc. */
1535GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1536
1537/* andi. */
1538static void gen_andi_(DisasContext *ctx)
1539{
1540    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1541                    UIMM(ctx->opcode));
1542    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1543}
1544
1545/* andis. */
1546static void gen_andis_(DisasContext *ctx)
1547{
1548    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1549                    UIMM(ctx->opcode) << 16);
1550    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1551}
1552
1553/* cntlzw */
1554static void gen_cntlzw(DisasContext *ctx)
1555{
1556    TCGv_i32 t = tcg_temp_new_i32();
1557
1558    tcg_gen_trunc_tl_i32(t, cpu_gpr[rS(ctx->opcode)]);
1559    tcg_gen_clzi_i32(t, t, 32);
1560    tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t);
1561    tcg_temp_free_i32(t);
1562
1563    if (unlikely(Rc(ctx->opcode) != 0)) {
1564        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1565    }
1566}
1567
1568/* cnttzw */
1569static void gen_cnttzw(DisasContext *ctx)
1570{
1571    TCGv_i32 t = tcg_temp_new_i32();
1572
1573    tcg_gen_trunc_tl_i32(t, cpu_gpr[rS(ctx->opcode)]);
1574    tcg_gen_ctzi_i32(t, t, 32);
1575    tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t);
1576    tcg_temp_free_i32(t);
1577
1578    if (unlikely(Rc(ctx->opcode) != 0)) {
1579        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1580    }
1581}
1582
1583/* eqv & eqv. */
1584GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1585/* extsb & extsb. */
1586GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1587/* extsh & extsh. */
1588GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1589/* nand & nand. */
1590GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1591/* nor & nor. */
1592GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1593
1594#if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
1595static void gen_pause(DisasContext *ctx)
1596{
1597    TCGv_i32 t0 = tcg_const_i32(0);
1598    tcg_gen_st_i32(t0, cpu_env,
1599                   -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
1600    tcg_temp_free_i32(t0);
1601
1602    /* Stop translation, this gives other CPUs a chance to run */
1603    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
1604}
1605#endif /* defined(TARGET_PPC64) */
1606
1607/* or & or. */
1608static void gen_or(DisasContext *ctx)
1609{
1610    int rs, ra, rb;
1611
1612    rs = rS(ctx->opcode);
1613    ra = rA(ctx->opcode);
1614    rb = rB(ctx->opcode);
1615    /* Optimisation for mr. ri case */
1616    if (rs != ra || rs != rb) {
1617        if (rs != rb) {
1618            tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1619        } else {
1620            tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1621        }
1622        if (unlikely(Rc(ctx->opcode) != 0)) {
1623            gen_set_Rc0(ctx, cpu_gpr[ra]);
1624        }
1625    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1626        gen_set_Rc0(ctx, cpu_gpr[rs]);
1627#if defined(TARGET_PPC64)
1628    } else if (rs != 0) { /* 0 is nop */
1629        int prio = 0;
1630
1631        switch (rs) {
1632        case 1:
1633            /* Set process priority to low */
1634            prio = 2;
1635            break;
1636        case 6:
1637            /* Set process priority to medium-low */
1638            prio = 3;
1639            break;
1640        case 2:
1641            /* Set process priority to normal */
1642            prio = 4;
1643            break;
1644#if !defined(CONFIG_USER_ONLY)
1645        case 31:
1646            if (!ctx->pr) {
1647                /* Set process priority to very low */
1648                prio = 1;
1649            }
1650            break;
1651        case 5:
1652            if (!ctx->pr) {
1653                /* Set process priority to medium-hight */
1654                prio = 5;
1655            }
1656            break;
1657        case 3:
1658            if (!ctx->pr) {
1659                /* Set process priority to high */
1660                prio = 6;
1661            }
1662            break;
1663        case 7:
1664            if (ctx->hv && !ctx->pr) {
1665                /* Set process priority to very high */
1666                prio = 7;
1667            }
1668            break;
1669#endif
1670        default:
1671            break;
1672        }
1673        if (prio) {
1674            TCGv t0 = tcg_temp_new();
1675            gen_load_spr(t0, SPR_PPR);
1676            tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1677            tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1678            gen_store_spr(SPR_PPR, t0);
1679            tcg_temp_free(t0);
1680        }
1681#if !defined(CONFIG_USER_ONLY)
1682        /*
1683         * Pause out of TCG otherwise spin loops with smt_low eat too
1684         * much CPU and the kernel hangs.  This applies to all
1685         * encodings other than no-op, e.g., miso(rs=26), yield(27),
1686         * mdoio(29), mdoom(30), and all currently undefined.
1687         */
1688        gen_pause(ctx);
1689#endif
1690#endif
1691    }
1692}
1693/* orc & orc. */
1694GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1695
1696/* xor & xor. */
1697static void gen_xor(DisasContext *ctx)
1698{
1699    /* Optimisation for "set to zero" case */
1700    if (rS(ctx->opcode) != rB(ctx->opcode)) {
1701        tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1702                       cpu_gpr[rB(ctx->opcode)]);
1703    } else {
1704        tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1705    }
1706    if (unlikely(Rc(ctx->opcode) != 0)) {
1707        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1708    }
1709}
1710
1711/* ori */
1712static void gen_ori(DisasContext *ctx)
1713{
1714    target_ulong uimm = UIMM(ctx->opcode);
1715
1716    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1717        return;
1718    }
1719    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1720}
1721
1722/* oris */
1723static void gen_oris(DisasContext *ctx)
1724{
1725    target_ulong uimm = UIMM(ctx->opcode);
1726
1727    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1728        /* NOP */
1729        return;
1730    }
1731    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1732                   uimm << 16);
1733}
1734
1735/* xori */
1736static void gen_xori(DisasContext *ctx)
1737{
1738    target_ulong uimm = UIMM(ctx->opcode);
1739
1740    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1741        /* NOP */
1742        return;
1743    }
1744    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1745}
1746
1747/* xoris */
1748static void gen_xoris(DisasContext *ctx)
1749{
1750    target_ulong uimm = UIMM(ctx->opcode);
1751
1752    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1753        /* NOP */
1754        return;
1755    }
1756    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1757                    uimm << 16);
1758}
1759
1760/* popcntb : PowerPC 2.03 specification */
1761static void gen_popcntb(DisasContext *ctx)
1762{
1763    gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1764}
1765
1766static void gen_popcntw(DisasContext *ctx)
1767{
1768#if defined(TARGET_PPC64)
1769    gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1770#else
1771    tcg_gen_ctpop_i32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1772#endif
1773}
1774
1775#if defined(TARGET_PPC64)
1776/* popcntd: PowerPC 2.06 specification */
1777static void gen_popcntd(DisasContext *ctx)
1778{
1779    tcg_gen_ctpop_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1780}
1781#endif
1782
1783/* prtyw: PowerPC 2.05 specification */
1784static void gen_prtyw(DisasContext *ctx)
1785{
1786    TCGv ra = cpu_gpr[rA(ctx->opcode)];
1787    TCGv rs = cpu_gpr[rS(ctx->opcode)];
1788    TCGv t0 = tcg_temp_new();
1789    tcg_gen_shri_tl(t0, rs, 16);
1790    tcg_gen_xor_tl(ra, rs, t0);
1791    tcg_gen_shri_tl(t0, ra, 8);
1792    tcg_gen_xor_tl(ra, ra, t0);
1793    tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1794    tcg_temp_free(t0);
1795}
1796
1797#if defined(TARGET_PPC64)
1798/* prtyd: PowerPC 2.05 specification */
1799static void gen_prtyd(DisasContext *ctx)
1800{
1801    TCGv ra = cpu_gpr[rA(ctx->opcode)];
1802    TCGv rs = cpu_gpr[rS(ctx->opcode)];
1803    TCGv t0 = tcg_temp_new();
1804    tcg_gen_shri_tl(t0, rs, 32);
1805    tcg_gen_xor_tl(ra, rs, t0);
1806    tcg_gen_shri_tl(t0, ra, 16);
1807    tcg_gen_xor_tl(ra, ra, t0);
1808    tcg_gen_shri_tl(t0, ra, 8);
1809    tcg_gen_xor_tl(ra, ra, t0);
1810    tcg_gen_andi_tl(ra, ra, 1);
1811    tcg_temp_free(t0);
1812}
1813#endif
1814
1815#if defined(TARGET_PPC64)
1816/* bpermd */
1817static void gen_bpermd(DisasContext *ctx)
1818{
1819    gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1820                      cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1821}
1822#endif
1823
1824#if defined(TARGET_PPC64)
1825/* extsw & extsw. */
1826GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1827
1828/* cntlzd */
1829static void gen_cntlzd(DisasContext *ctx)
1830{
1831    tcg_gen_clzi_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], 64);
1832    if (unlikely(Rc(ctx->opcode) != 0)) {
1833        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1834    }
1835}
1836
1837/* cnttzd */
1838static void gen_cnttzd(DisasContext *ctx)
1839{
1840    tcg_gen_ctzi_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], 64);
1841    if (unlikely(Rc(ctx->opcode) != 0)) {
1842        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1843    }
1844}
1845
1846/* darn */
1847static void gen_darn(DisasContext *ctx)
1848{
1849    int l = L(ctx->opcode);
1850
1851    if (l > 2) {
1852        tcg_gen_movi_i64(cpu_gpr[rD(ctx->opcode)], -1);
1853    } else {
1854        if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
1855            gen_io_start();
1856        }
1857        if (l == 0) {
1858            gen_helper_darn32(cpu_gpr[rD(ctx->opcode)]);
1859        } else {
1860            /* Return 64-bit random for both CRN and RRN */
1861            gen_helper_darn64(cpu_gpr[rD(ctx->opcode)]);
1862        }
1863        if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
1864            gen_stop_exception(ctx);
1865        }
1866    }
1867}
1868#endif
1869
1870/***                             Integer rotate                            ***/
1871
1872/* rlwimi & rlwimi. */
1873static void gen_rlwimi(DisasContext *ctx)
1874{
1875    TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1876    TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1877    uint32_t sh = SH(ctx->opcode);
1878    uint32_t mb = MB(ctx->opcode);
1879    uint32_t me = ME(ctx->opcode);
1880
1881    if (sh == (31 - me) && mb <= me) {
1882        tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
1883    } else {
1884        target_ulong mask;
1885        TCGv t1;
1886
1887#if defined(TARGET_PPC64)
1888        mb += 32;
1889        me += 32;
1890#endif
1891        mask = MASK(mb, me);
1892
1893        t1 = tcg_temp_new();
1894        if (mask <= 0xffffffffu) {
1895            TCGv_i32 t0 = tcg_temp_new_i32();
1896            tcg_gen_trunc_tl_i32(t0, t_rs);
1897            tcg_gen_rotli_i32(t0, t0, sh);
1898            tcg_gen_extu_i32_tl(t1, t0);
1899            tcg_temp_free_i32(t0);
1900        } else {
1901#if defined(TARGET_PPC64)
1902            tcg_gen_deposit_i64(t1, t_rs, t_rs, 32, 32);
1903            tcg_gen_rotli_i64(t1, t1, sh);
1904#else
1905            g_assert_not_reached();
1906#endif
1907        }
1908
1909        tcg_gen_andi_tl(t1, t1, mask);
1910        tcg_gen_andi_tl(t_ra, t_ra, ~mask);
1911        tcg_gen_or_tl(t_ra, t_ra, t1);
1912        tcg_temp_free(t1);
1913    }
1914    if (unlikely(Rc(ctx->opcode) != 0)) {
1915        gen_set_Rc0(ctx, t_ra);
1916    }
1917}
1918
1919/* rlwinm & rlwinm. */
1920static void gen_rlwinm(DisasContext *ctx)
1921{
1922    TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1923    TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1924    int sh = SH(ctx->opcode);
1925    int mb = MB(ctx->opcode);
1926    int me = ME(ctx->opcode);
1927    int len = me - mb + 1;
1928    int rsh = (32 - sh) & 31;
1929
1930    if (sh != 0 && len > 0 && me == (31 - sh)) {
1931        tcg_gen_deposit_z_tl(t_ra, t_rs, sh, len);
1932    } else if (me == 31 && rsh + len <= 32) {
1933        tcg_gen_extract_tl(t_ra, t_rs, rsh, len);
1934    } else {
1935        target_ulong mask;
1936#if defined(TARGET_PPC64)
1937        mb += 32;
1938        me += 32;
1939#endif
1940        mask = MASK(mb, me);
1941        if (mask <= 0xffffffffu) {
1942            if (sh == 0) {
1943                tcg_gen_andi_tl(t_ra, t_rs, mask);
1944            } else {
1945                TCGv_i32 t0 = tcg_temp_new_i32();
1946                tcg_gen_trunc_tl_i32(t0, t_rs);
1947                tcg_gen_rotli_i32(t0, t0, sh);
1948                tcg_gen_andi_i32(t0, t0, mask);
1949                tcg_gen_extu_i32_tl(t_ra, t0);
1950                tcg_temp_free_i32(t0);
1951            }
1952        } else {
1953#if defined(TARGET_PPC64)
1954            tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
1955            tcg_gen_rotli_i64(t_ra, t_ra, sh);
1956            tcg_gen_andi_i64(t_ra, t_ra, mask);
1957#else
1958            g_assert_not_reached();
1959#endif
1960        }
1961    }
1962    if (unlikely(Rc(ctx->opcode) != 0)) {
1963        gen_set_Rc0(ctx, t_ra);
1964    }
1965}
1966
1967/* rlwnm & rlwnm. */
1968static void gen_rlwnm(DisasContext *ctx)
1969{
1970    TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1971    TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1972    TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
1973    uint32_t mb = MB(ctx->opcode);
1974    uint32_t me = ME(ctx->opcode);
1975    target_ulong mask;
1976
1977#if defined(TARGET_PPC64)
1978    mb += 32;
1979    me += 32;
1980#endif
1981    mask = MASK(mb, me);
1982
1983    if (mask <= 0xffffffffu) {
1984        TCGv_i32 t0 = tcg_temp_new_i32();
1985        TCGv_i32 t1 = tcg_temp_new_i32();
1986        tcg_gen_trunc_tl_i32(t0, t_rb);
1987        tcg_gen_trunc_tl_i32(t1, t_rs);
1988        tcg_gen_andi_i32(t0, t0, 0x1f);
1989        tcg_gen_rotl_i32(t1, t1, t0);
1990        tcg_gen_extu_i32_tl(t_ra, t1);
1991        tcg_temp_free_i32(t0);
1992        tcg_temp_free_i32(t1);
1993    } else {
1994#if defined(TARGET_PPC64)
1995        TCGv_i64 t0 = tcg_temp_new_i64();
1996        tcg_gen_andi_i64(t0, t_rb, 0x1f);
1997        tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
1998        tcg_gen_rotl_i64(t_ra, t_ra, t0);
1999        tcg_temp_free_i64(t0);
2000#else
2001        g_assert_not_reached();
2002#endif
2003    }
2004
2005    tcg_gen_andi_tl(t_ra, t_ra, mask);
2006
2007    if (unlikely(Rc(ctx->opcode) != 0)) {
2008        gen_set_Rc0(ctx, t_ra);
2009    }
2010}
2011
2012#if defined(TARGET_PPC64)
2013#define GEN_PPC64_R2(name, opc1, opc2)                                        \
2014static void glue(gen_, name##0)(DisasContext *ctx)                            \
2015{                                                                             \
2016    gen_##name(ctx, 0);                                                       \
2017}                                                                             \
2018                                                                              \
2019static void glue(gen_, name##1)(DisasContext *ctx)                            \
2020{                                                                             \
2021    gen_##name(ctx, 1);                                                       \
2022}
2023#define GEN_PPC64_R4(name, opc1, opc2)                                        \
2024static void glue(gen_, name##0)(DisasContext *ctx)                            \
2025{                                                                             \
2026    gen_##name(ctx, 0, 0);                                                    \
2027}                                                                             \
2028                                                                              \
2029static void glue(gen_, name##1)(DisasContext *ctx)                            \
2030{                                                                             \
2031    gen_##name(ctx, 0, 1);                                                    \
2032}                                                                             \
2033                                                                              \
2034static void glue(gen_, name##2)(DisasContext *ctx)                            \
2035{                                                                             \
2036    gen_##name(ctx, 1, 0);                                                    \
2037}                                                                             \
2038                                                                              \
2039static void glue(gen_, name##3)(DisasContext *ctx)                            \
2040{                                                                             \
2041    gen_##name(ctx, 1, 1);                                                    \
2042}
2043
2044static void gen_rldinm(DisasContext *ctx, int mb, int me, int sh)
2045{
2046    TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2047    TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2048    int len = me - mb + 1;
2049    int rsh = (64 - sh) & 63;
2050
2051    if (sh != 0 && len > 0 && me == (63 - sh)) {
2052        tcg_gen_deposit_z_tl(t_ra, t_rs, sh, len);
2053    } else if (me == 63 && rsh + len <= 64) {
2054        tcg_gen_extract_tl(t_ra, t_rs, rsh, len);
2055    } else {
2056        tcg_gen_rotli_tl(t_ra, t_rs, sh);
2057        tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2058    }
2059    if (unlikely(Rc(ctx->opcode) != 0)) {
2060        gen_set_Rc0(ctx, t_ra);
2061    }
2062}
2063
2064/* rldicl - rldicl. */
2065static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
2066{
2067    uint32_t sh, mb;
2068
2069    sh = SH(ctx->opcode) | (shn << 5);
2070    mb = MB(ctx->opcode) | (mbn << 5);
2071    gen_rldinm(ctx, mb, 63, sh);
2072}
2073GEN_PPC64_R4(rldicl, 0x1E, 0x00);
2074
2075/* rldicr - rldicr. */
2076static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
2077{
2078    uint32_t sh, me;
2079
2080    sh = SH(ctx->opcode) | (shn << 5);
2081    me = MB(ctx->opcode) | (men << 5);
2082    gen_rldinm(ctx, 0, me, sh);
2083}
2084GEN_PPC64_R4(rldicr, 0x1E, 0x02);
2085
2086/* rldic - rldic. */
2087static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
2088{
2089    uint32_t sh, mb;
2090
2091    sh = SH(ctx->opcode) | (shn << 5);
2092    mb = MB(ctx->opcode) | (mbn << 5);
2093    gen_rldinm(ctx, mb, 63 - sh, sh);
2094}
2095GEN_PPC64_R4(rldic, 0x1E, 0x04);
2096
2097static void gen_rldnm(DisasContext *ctx, int mb, int me)
2098{
2099    TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2100    TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2101    TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
2102    TCGv t0;
2103
2104    t0 = tcg_temp_new();
2105    tcg_gen_andi_tl(t0, t_rb, 0x3f);
2106    tcg_gen_rotl_tl(t_ra, t_rs, t0);
2107    tcg_temp_free(t0);
2108
2109    tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2110    if (unlikely(Rc(ctx->opcode) != 0)) {
2111        gen_set_Rc0(ctx, t_ra);
2112    }
2113}
2114
2115/* rldcl - rldcl. */
2116static inline void gen_rldcl(DisasContext *ctx, int mbn)
2117{
2118    uint32_t mb;
2119
2120    mb = MB(ctx->opcode) | (mbn << 5);
2121    gen_rldnm(ctx, mb, 63);
2122}
2123GEN_PPC64_R2(rldcl, 0x1E, 0x08);
2124
2125/* rldcr - rldcr. */
2126static inline void gen_rldcr(DisasContext *ctx, int men)
2127{
2128    uint32_t me;
2129
2130    me = MB(ctx->opcode) | (men << 5);
2131    gen_rldnm(ctx, 0, me);
2132}
2133GEN_PPC64_R2(rldcr, 0x1E, 0x09);
2134
2135/* rldimi - rldimi. */
2136static void gen_rldimi(DisasContext *ctx, int mbn, int shn)
2137{
2138    TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2139    TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2140    uint32_t sh = SH(ctx->opcode) | (shn << 5);
2141    uint32_t mb = MB(ctx->opcode) | (mbn << 5);
2142    uint32_t me = 63 - sh;
2143
2144    if (mb <= me) {
2145        tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
2146    } else {
2147        target_ulong mask = MASK(mb, me);
2148        TCGv t1 = tcg_temp_new();
2149
2150        tcg_gen_rotli_tl(t1, t_rs, sh);
2151        tcg_gen_andi_tl(t1, t1, mask);
2152        tcg_gen_andi_tl(t_ra, t_ra, ~mask);
2153        tcg_gen_or_tl(t_ra, t_ra, t1);
2154        tcg_temp_free(t1);
2155    }
2156    if (unlikely(Rc(ctx->opcode) != 0)) {
2157        gen_set_Rc0(ctx, t_ra);
2158    }
2159}
2160GEN_PPC64_R4(rldimi, 0x1E, 0x06);
2161#endif
2162
2163/***                             Integer shift                             ***/
2164
2165/* slw & slw. */
2166static void gen_slw(DisasContext *ctx)
2167{
2168    TCGv t0, t1;
2169
2170    t0 = tcg_temp_new();
2171    /* AND rS with a mask that is 0 when rB >= 0x20 */
2172#if defined(TARGET_PPC64)
2173    tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2174    tcg_gen_sari_tl(t0, t0, 0x3f);
2175#else
2176    tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2177    tcg_gen_sari_tl(t0, t0, 0x1f);
2178#endif
2179    tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2180    t1 = tcg_temp_new();
2181    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2182    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2183    tcg_temp_free(t1);
2184    tcg_temp_free(t0);
2185    tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
2186    if (unlikely(Rc(ctx->opcode) != 0)) {
2187        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2188    }
2189}
2190
2191/* sraw & sraw. */
2192static void gen_sraw(DisasContext *ctx)
2193{
2194    gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
2195                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2196    if (unlikely(Rc(ctx->opcode) != 0)) {
2197        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2198    }
2199}
2200
2201/* srawi & srawi. */
2202static void gen_srawi(DisasContext *ctx)
2203{
2204    int sh = SH(ctx->opcode);
2205    TCGv dst = cpu_gpr[rA(ctx->opcode)];
2206    TCGv src = cpu_gpr[rS(ctx->opcode)];
2207    if (sh == 0) {
2208        tcg_gen_ext32s_tl(dst, src);
2209        tcg_gen_movi_tl(cpu_ca, 0);
2210        if (is_isa300(ctx)) {
2211            tcg_gen_movi_tl(cpu_ca32, 0);
2212        }
2213    } else {
2214        TCGv t0;
2215        tcg_gen_ext32s_tl(dst, src);
2216        tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
2217        t0 = tcg_temp_new();
2218        tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
2219        tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2220        tcg_temp_free(t0);
2221        tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2222        if (is_isa300(ctx)) {
2223            tcg_gen_mov_tl(cpu_ca32, cpu_ca);
2224        }
2225        tcg_gen_sari_tl(dst, dst, sh);
2226    }
2227    if (unlikely(Rc(ctx->opcode) != 0)) {
2228        gen_set_Rc0(ctx, dst);
2229    }
2230}
2231
2232/* srw & srw. */
2233static void gen_srw(DisasContext *ctx)
2234{
2235    TCGv t0, t1;
2236
2237    t0 = tcg_temp_new();
2238    /* AND rS with a mask that is 0 when rB >= 0x20 */
2239#if defined(TARGET_PPC64)
2240    tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2241    tcg_gen_sari_tl(t0, t0, 0x3f);
2242#else
2243    tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2244    tcg_gen_sari_tl(t0, t0, 0x1f);
2245#endif
2246    tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2247    tcg_gen_ext32u_tl(t0, t0);
2248    t1 = tcg_temp_new();
2249    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2250    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2251    tcg_temp_free(t1);
2252    tcg_temp_free(t0);
2253    if (unlikely(Rc(ctx->opcode) != 0)) {
2254        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2255    }
2256}
2257
2258#if defined(TARGET_PPC64)
2259/* sld & sld. */
2260static void gen_sld(DisasContext *ctx)
2261{
2262    TCGv t0, t1;
2263
2264    t0 = tcg_temp_new();
2265    /* AND rS with a mask that is 0 when rB >= 0x40 */
2266    tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2267    tcg_gen_sari_tl(t0, t0, 0x3f);
2268    tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2269    t1 = tcg_temp_new();
2270    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2271    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2272    tcg_temp_free(t1);
2273    tcg_temp_free(t0);
2274    if (unlikely(Rc(ctx->opcode) != 0)) {
2275        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2276    }
2277}
2278
2279/* srad & srad. */
2280static void gen_srad(DisasContext *ctx)
2281{
2282    gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
2283                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2284    if (unlikely(Rc(ctx->opcode) != 0)) {
2285        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2286    }
2287}
2288/* sradi & sradi. */
2289static inline void gen_sradi(DisasContext *ctx, int n)
2290{
2291    int sh = SH(ctx->opcode) + (n << 5);
2292    TCGv dst = cpu_gpr[rA(ctx->opcode)];
2293    TCGv src = cpu_gpr[rS(ctx->opcode)];
2294    if (sh == 0) {
2295        tcg_gen_mov_tl(dst, src);
2296        tcg_gen_movi_tl(cpu_ca, 0);
2297        if (is_isa300(ctx)) {
2298            tcg_gen_movi_tl(cpu_ca32, 0);
2299        }
2300    } else {
2301        TCGv t0;
2302        tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
2303        t0 = tcg_temp_new();
2304        tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
2305        tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2306        tcg_temp_free(t0);
2307        tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2308        if (is_isa300(ctx)) {
2309            tcg_gen_mov_tl(cpu_ca32, cpu_ca);
2310        }
2311        tcg_gen_sari_tl(dst, src, sh);
2312    }
2313    if (unlikely(Rc(ctx->opcode) != 0)) {
2314        gen_set_Rc0(ctx, dst);
2315    }
2316}
2317
2318static void gen_sradi0(DisasContext *ctx)
2319{
2320    gen_sradi(ctx, 0);
2321}
2322
2323static void gen_sradi1(DisasContext *ctx)
2324{
2325    gen_sradi(ctx, 1);
2326}
2327
2328/* extswsli & extswsli. */
2329static inline void gen_extswsli(DisasContext *ctx, int n)
2330{
2331    int sh = SH(ctx->opcode) + (n << 5);
2332    TCGv dst = cpu_gpr[rA(ctx->opcode)];
2333    TCGv src = cpu_gpr[rS(ctx->opcode)];
2334
2335    tcg_gen_ext32s_tl(dst, src);
2336    tcg_gen_shli_tl(dst, dst, sh);
2337    if (unlikely(Rc(ctx->opcode) != 0)) {
2338        gen_set_Rc0(ctx, dst);
2339    }
2340}
2341
2342static void gen_extswsli0(DisasContext *ctx)
2343{
2344    gen_extswsli(ctx, 0);
2345}
2346
2347static void gen_extswsli1(DisasContext *ctx)
2348{
2349    gen_extswsli(ctx, 1);
2350}
2351
2352/* srd & srd. */
2353static void gen_srd(DisasContext *ctx)
2354{
2355    TCGv t0, t1;
2356
2357    t0 = tcg_temp_new();
2358    /* AND rS with a mask that is 0 when rB >= 0x40 */
2359    tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2360    tcg_gen_sari_tl(t0, t0, 0x3f);
2361    tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2362    t1 = tcg_temp_new();
2363    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2364    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2365    tcg_temp_free(t1);
2366    tcg_temp_free(t0);
2367    if (unlikely(Rc(ctx->opcode) != 0)) {
2368        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2369    }
2370}
2371#endif
2372
2373/***                           Addressing modes                            ***/
2374/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2375static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2376                                      target_long maskl)
2377{
2378    target_long simm = SIMM(ctx->opcode);
2379
2380    simm &= ~maskl;
2381    if (rA(ctx->opcode) == 0) {
2382        if (NARROW_MODE(ctx)) {
2383            simm = (uint32_t)simm;
2384        }
2385        tcg_gen_movi_tl(EA, simm);
2386    } else if (likely(simm != 0)) {
2387        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2388        if (NARROW_MODE(ctx)) {
2389            tcg_gen_ext32u_tl(EA, EA);
2390        }
2391    } else {
2392        if (NARROW_MODE(ctx)) {
2393            tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2394        } else {
2395            tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2396        }
2397    }
2398}
2399
2400static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
2401{
2402    if (rA(ctx->opcode) == 0) {
2403        if (NARROW_MODE(ctx)) {
2404            tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2405        } else {
2406            tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2407        }
2408    } else {
2409        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2410        if (NARROW_MODE(ctx)) {
2411            tcg_gen_ext32u_tl(EA, EA);
2412        }
2413    }
2414}
2415
2416static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
2417{
2418    if (rA(ctx->opcode) == 0) {
2419        tcg_gen_movi_tl(EA, 0);
2420    } else if (NARROW_MODE(ctx)) {
2421        tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2422    } else {
2423        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2424    }
2425}
2426
2427static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2428                                target_long val)
2429{
2430    tcg_gen_addi_tl(ret, arg1, val);
2431    if (NARROW_MODE(ctx)) {
2432        tcg_gen_ext32u_tl(ret, ret);
2433    }
2434}
2435
2436static inline void gen_align_no_le(DisasContext *ctx)
2437{
2438    gen_exception_err(ctx, POWERPC_EXCP_ALIGN,
2439                      (ctx->opcode & 0x03FF0000) | POWERPC_EXCP_ALIGN_LE);
2440}
2441
2442/***                             Integer load                              ***/
2443#define DEF_MEMOP(op) ((op) | ctx->default_tcg_memop_mask)
2444#define BSWAP_MEMOP(op) ((op) | (ctx->default_tcg_memop_mask ^ MO_BSWAP))
2445
2446#define GEN_QEMU_LOAD_TL(ldop, op)                                      \
2447static void glue(gen_qemu_, ldop)(DisasContext *ctx,                    \
2448                                  TCGv val,                             \
2449                                  TCGv addr)                            \
2450{                                                                       \
2451    tcg_gen_qemu_ld_tl(val, addr, ctx->mem_idx, op);                    \
2452}
2453
2454GEN_QEMU_LOAD_TL(ld8u,  DEF_MEMOP(MO_UB))
2455GEN_QEMU_LOAD_TL(ld16u, DEF_MEMOP(MO_UW))
2456GEN_QEMU_LOAD_TL(ld16s, DEF_MEMOP(MO_SW))
2457GEN_QEMU_LOAD_TL(ld32u, DEF_MEMOP(MO_UL))
2458GEN_QEMU_LOAD_TL(ld32s, DEF_MEMOP(MO_SL))
2459
2460GEN_QEMU_LOAD_TL(ld16ur, BSWAP_MEMOP(MO_UW))
2461GEN_QEMU_LOAD_TL(ld32ur, BSWAP_MEMOP(MO_UL))
2462
2463#define GEN_QEMU_LOAD_64(ldop, op)                                  \
2464static void glue(gen_qemu_, glue(ldop, _i64))(DisasContext *ctx,    \
2465                                             TCGv_i64 val,          \
2466                                             TCGv addr)             \
2467{                                                                   \
2468    tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, op);               \
2469}
2470
2471GEN_QEMU_LOAD_64(ld8u,  DEF_MEMOP(MO_UB))
2472GEN_QEMU_LOAD_64(ld16u, DEF_MEMOP(MO_UW))
2473GEN_QEMU_LOAD_64(ld32u, DEF_MEMOP(MO_UL))
2474GEN_QEMU_LOAD_64(ld32s, DEF_MEMOP(MO_SL))
2475GEN_QEMU_LOAD_64(ld64,  DEF_MEMOP(MO_Q))
2476
2477#if defined(TARGET_PPC64)
2478GEN_QEMU_LOAD_64(ld64ur, BSWAP_MEMOP(MO_Q))
2479#endif
2480
2481#define GEN_QEMU_STORE_TL(stop, op)                                     \
2482static void glue(gen_qemu_, stop)(DisasContext *ctx,                    \
2483                                  TCGv val,                             \
2484                                  TCGv addr)                            \
2485{                                                                       \
2486    tcg_gen_qemu_st_tl(val, addr, ctx->mem_idx, op);                    \
2487}
2488
2489GEN_QEMU_STORE_TL(st8,  DEF_MEMOP(MO_UB))
2490GEN_QEMU_STORE_TL(st16, DEF_MEMOP(MO_UW))
2491GEN_QEMU_STORE_TL(st32, DEF_MEMOP(MO_UL))
2492
2493GEN_QEMU_STORE_TL(st16r, BSWAP_MEMOP(MO_UW))
2494GEN_QEMU_STORE_TL(st32r, BSWAP_MEMOP(MO_UL))
2495
2496#define GEN_QEMU_STORE_64(stop, op)                               \
2497static void glue(gen_qemu_, glue(stop, _i64))(DisasContext *ctx,  \
2498                                              TCGv_i64 val,       \
2499                                              TCGv addr)          \
2500{                                                                 \
2501    tcg_gen_qemu_st_i64(val, addr, ctx->mem_idx, op);             \
2502}
2503
2504GEN_QEMU_STORE_64(st8,  DEF_MEMOP(MO_UB))
2505GEN_QEMU_STORE_64(st16, DEF_MEMOP(MO_UW))
2506GEN_QEMU_STORE_64(st32, DEF_MEMOP(MO_UL))
2507GEN_QEMU_STORE_64(st64, DEF_MEMOP(MO_Q))
2508
2509#if defined(TARGET_PPC64)
2510GEN_QEMU_STORE_64(st64r, BSWAP_MEMOP(MO_Q))
2511#endif
2512
2513#define GEN_LD(name, ldop, opc, type)                                         \
2514static void glue(gen_, name)(DisasContext *ctx)                               \
2515{                                                                             \
2516    TCGv EA;                                                                  \
2517    gen_set_access_type(ctx, ACCESS_INT);                                     \
2518    EA = tcg_temp_new();                                                      \
2519    gen_addr_imm_index(ctx, EA, 0);                                           \
2520    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2521    tcg_temp_free(EA);                                                        \
2522}
2523
2524#define GEN_LDU(name, ldop, opc, type)                                        \
2525static void glue(gen_, name##u)(DisasContext *ctx)                            \
2526{                                                                             \
2527    TCGv EA;                                                                  \
2528    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2529                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2530        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2531        return;                                                               \
2532    }                                                                         \
2533    gen_set_access_type(ctx, ACCESS_INT);                                     \
2534    EA = tcg_temp_new();                                                      \
2535    if (type == PPC_64B)                                                      \
2536        gen_addr_imm_index(ctx, EA, 0x03);                                    \
2537    else                                                                      \
2538        gen_addr_imm_index(ctx, EA, 0);                                       \
2539    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2540    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2541    tcg_temp_free(EA);                                                        \
2542}
2543
2544#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2545static void glue(gen_, name##ux)(DisasContext *ctx)                           \
2546{                                                                             \
2547    TCGv EA;                                                                  \
2548    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2549                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2550        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2551        return;                                                               \
2552    }                                                                         \
2553    gen_set_access_type(ctx, ACCESS_INT);                                     \
2554    EA = tcg_temp_new();                                                      \
2555    gen_addr_reg_index(ctx, EA);                                              \
2556    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2557    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2558    tcg_temp_free(EA);                                                        \
2559}
2560
2561#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
2562static void glue(gen_, name##x)(DisasContext *ctx)                            \
2563{                                                                             \
2564    TCGv EA;                                                                  \
2565    chk;                                                                      \
2566    gen_set_access_type(ctx, ACCESS_INT);                                     \
2567    EA = tcg_temp_new();                                                      \
2568    gen_addr_reg_index(ctx, EA);                                              \
2569    gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2570    tcg_temp_free(EA);                                                        \
2571}
2572
2573#define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2574    GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_NONE)
2575
2576#define GEN_LDX_HVRM(name, ldop, opc2, opc3, type)                            \
2577    GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
2578
2579#define GEN_LDS(name, ldop, op, type)                                         \
2580GEN_LD(name, ldop, op | 0x20, type);                                          \
2581GEN_LDU(name, ldop, op | 0x21, type);                                         \
2582GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2583GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2584
2585/* lbz lbzu lbzux lbzx */
2586GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2587/* lha lhau lhaux lhax */
2588GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2589/* lhz lhzu lhzux lhzx */
2590GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2591/* lwz lwzu lwzux lwzx */
2592GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2593
2594#define GEN_LDEPX(name, ldop, opc2, opc3)                                     \
2595static void glue(gen_, name##epx)(DisasContext *ctx)                          \
2596{                                                                             \
2597    TCGv EA;                                                                  \
2598    CHK_SV;                                                                   \
2599    gen_set_access_type(ctx, ACCESS_INT);                                     \
2600    EA = tcg_temp_new();                                                      \
2601    gen_addr_reg_index(ctx, EA);                                              \
2602    tcg_gen_qemu_ld_tl(cpu_gpr[rD(ctx->opcode)], EA, PPC_TLB_EPID_LOAD, ldop);\
2603    tcg_temp_free(EA);                                                        \
2604}
2605
2606GEN_LDEPX(lb, DEF_MEMOP(MO_UB), 0x1F, 0x02)
2607GEN_LDEPX(lh, DEF_MEMOP(MO_UW), 0x1F, 0x08)
2608GEN_LDEPX(lw, DEF_MEMOP(MO_UL), 0x1F, 0x00)
2609#if defined(TARGET_PPC64)
2610GEN_LDEPX(ld, DEF_MEMOP(MO_Q), 0x1D, 0x00)
2611#endif
2612
2613#if defined(TARGET_PPC64)
2614/* lwaux */
2615GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2616/* lwax */
2617GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2618/* ldux */
2619GEN_LDUX(ld, ld64_i64, 0x15, 0x01, PPC_64B);
2620/* ldx */
2621GEN_LDX(ld, ld64_i64, 0x15, 0x00, PPC_64B);
2622
2623/* CI load/store variants */
2624GEN_LDX_HVRM(ldcix, ld64_i64, 0x15, 0x1b, PPC_CILDST)
2625GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x15, PPC_CILDST)
2626GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
2627GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
2628
2629static void gen_ld(DisasContext *ctx)
2630{
2631    TCGv EA;
2632    if (Rc(ctx->opcode)) {
2633        if (unlikely(rA(ctx->opcode) == 0 ||
2634                     rA(ctx->opcode) == rD(ctx->opcode))) {
2635            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2636            return;
2637        }
2638    }
2639    gen_set_access_type(ctx, ACCESS_INT);
2640    EA = tcg_temp_new();
2641    gen_addr_imm_index(ctx, EA, 0x03);
2642    if (ctx->opcode & 0x02) {
2643        /* lwa (lwau is undefined) */
2644        gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2645    } else {
2646        /* ld - ldu */
2647        gen_qemu_ld64_i64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2648    }
2649    if (Rc(ctx->opcode)) {
2650        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2651    }
2652    tcg_temp_free(EA);
2653}
2654
2655/* lq */
2656static void gen_lq(DisasContext *ctx)
2657{
2658    int ra, rd;
2659    TCGv EA, hi, lo;
2660
2661    /* lq is a legal user mode instruction starting in ISA 2.07 */
2662    bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2663    bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2664
2665    if (!legal_in_user_mode && ctx->pr) {
2666        gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2667        return;
2668    }
2669
2670    if (!le_is_supported && ctx->le_mode) {
2671        gen_align_no_le(ctx);
2672        return;
2673    }
2674    ra = rA(ctx->opcode);
2675    rd = rD(ctx->opcode);
2676    if (unlikely((rd & 1) || rd == ra)) {
2677        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2678        return;
2679    }
2680
2681    gen_set_access_type(ctx, ACCESS_INT);
2682    EA = tcg_temp_new();
2683    gen_addr_imm_index(ctx, EA, 0x0F);
2684
2685    /* Note that the low part is always in RD+1, even in LE mode.  */
2686    lo = cpu_gpr[rd + 1];
2687    hi = cpu_gpr[rd];
2688
2689    if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2690        if (HAVE_ATOMIC128) {
2691            TCGv_i32 oi = tcg_temp_new_i32();
2692            if (ctx->le_mode) {
2693                tcg_gen_movi_i32(oi, make_memop_idx(MO_LEQ, ctx->mem_idx));
2694                gen_helper_lq_le_parallel(lo, cpu_env, EA, oi);
2695            } else {
2696                tcg_gen_movi_i32(oi, make_memop_idx(MO_BEQ, ctx->mem_idx));
2697                gen_helper_lq_be_parallel(lo, cpu_env, EA, oi);
2698            }
2699            tcg_temp_free_i32(oi);
2700            tcg_gen_ld_i64(hi, cpu_env, offsetof(CPUPPCState, retxh));
2701        } else {
2702            /* Restart with exclusive lock.  */
2703            gen_helper_exit_atomic(cpu_env);
2704            ctx->base.is_jmp = DISAS_NORETURN;
2705        }
2706    } else if (ctx->le_mode) {
2707        tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_LEQ);
2708        gen_addr_add(ctx, EA, EA, 8);
2709        tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_LEQ);
2710    } else {
2711        tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_BEQ);
2712        gen_addr_add(ctx, EA, EA, 8);
2713        tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_BEQ);
2714    }
2715    tcg_temp_free(EA);
2716}
2717#endif
2718
2719/***                              Integer store                            ***/
2720#define GEN_ST(name, stop, opc, type)                                         \
2721static void glue(gen_, name)(DisasContext *ctx)                               \
2722{                                                                             \
2723    TCGv EA;                                                                  \
2724    gen_set_access_type(ctx, ACCESS_INT);                                     \
2725    EA = tcg_temp_new();                                                      \
2726    gen_addr_imm_index(ctx, EA, 0);                                           \
2727    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2728    tcg_temp_free(EA);                                                        \
2729}
2730
2731#define GEN_STU(name, stop, opc, type)                                        \
2732static void glue(gen_, stop##u)(DisasContext *ctx)                            \
2733{                                                                             \
2734    TCGv EA;                                                                  \
2735    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2736        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2737        return;                                                               \
2738    }                                                                         \
2739    gen_set_access_type(ctx, ACCESS_INT);                                     \
2740    EA = tcg_temp_new();                                                      \
2741    if (type == PPC_64B)                                                      \
2742        gen_addr_imm_index(ctx, EA, 0x03);                                    \
2743    else                                                                      \
2744        gen_addr_imm_index(ctx, EA, 0);                                       \
2745    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2746    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2747    tcg_temp_free(EA);                                                        \
2748}
2749
2750#define GEN_STUX(name, stop, opc2, opc3, type)                                \
2751static void glue(gen_, name##ux)(DisasContext *ctx)                           \
2752{                                                                             \
2753    TCGv EA;                                                                  \
2754    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2755        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2756        return;                                                               \
2757    }                                                                         \
2758    gen_set_access_type(ctx, ACCESS_INT);                                     \
2759    EA = tcg_temp_new();                                                      \
2760    gen_addr_reg_index(ctx, EA);                                              \
2761    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2762    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2763    tcg_temp_free(EA);                                                        \
2764}
2765
2766#define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
2767static void glue(gen_, name##x)(DisasContext *ctx)                            \
2768{                                                                             \
2769    TCGv EA;                                                                  \
2770    chk;                                                                      \
2771    gen_set_access_type(ctx, ACCESS_INT);                                     \
2772    EA = tcg_temp_new();                                                      \
2773    gen_addr_reg_index(ctx, EA);                                              \
2774    gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2775    tcg_temp_free(EA);                                                        \
2776}
2777#define GEN_STX(name, stop, opc2, opc3, type)                                 \
2778    GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_NONE)
2779
2780#define GEN_STX_HVRM(name, stop, opc2, opc3, type)                            \
2781    GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
2782
2783#define GEN_STS(name, stop, op, type)                                         \
2784GEN_ST(name, stop, op | 0x20, type);                                          \
2785GEN_STU(name, stop, op | 0x21, type);                                         \
2786GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2787GEN_STX(name, stop, 0x17, op | 0x00, type)
2788
2789/* stb stbu stbux stbx */
2790GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2791/* sth sthu sthux sthx */
2792GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2793/* stw stwu stwux stwx */
2794GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2795
2796#define GEN_STEPX(name, stop, opc2, opc3)                                     \
2797static void glue(gen_, name##epx)(DisasContext *ctx)                          \
2798{                                                                             \
2799    TCGv EA;                                                                  \
2800    CHK_SV;                                                                   \
2801    gen_set_access_type(ctx, ACCESS_INT);                                     \
2802    EA = tcg_temp_new();                                                      \
2803    gen_addr_reg_index(ctx, EA);                                              \
2804    tcg_gen_qemu_st_tl(                                                       \
2805        cpu_gpr[rD(ctx->opcode)], EA, PPC_TLB_EPID_STORE, stop);              \
2806    tcg_temp_free(EA);                                                        \
2807}
2808
2809GEN_STEPX(stb, DEF_MEMOP(MO_UB), 0x1F, 0x06)
2810GEN_STEPX(sth, DEF_MEMOP(MO_UW), 0x1F, 0x0C)
2811GEN_STEPX(stw, DEF_MEMOP(MO_UL), 0x1F, 0x04)
2812#if defined(TARGET_PPC64)
2813GEN_STEPX(std, DEF_MEMOP(MO_Q), 0x1d, 0x04)
2814#endif
2815
2816#if defined(TARGET_PPC64)
2817GEN_STUX(std, st64_i64, 0x15, 0x05, PPC_64B);
2818GEN_STX(std, st64_i64, 0x15, 0x04, PPC_64B);
2819GEN_STX_HVRM(stdcix, st64_i64, 0x15, 0x1f, PPC_CILDST)
2820GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
2821GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
2822GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
2823
2824static void gen_std(DisasContext *ctx)
2825{
2826    int rs;
2827    TCGv EA;
2828
2829    rs = rS(ctx->opcode);
2830    if ((ctx->opcode & 0x3) == 0x2) { /* stq */
2831        bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2832        bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2833        TCGv hi, lo;
2834
2835        if (!(ctx->insns_flags & PPC_64BX)) {
2836            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2837        }
2838
2839        if (!legal_in_user_mode && ctx->pr) {
2840            gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2841            return;
2842        }
2843
2844        if (!le_is_supported && ctx->le_mode) {
2845            gen_align_no_le(ctx);
2846            return;
2847        }
2848
2849        if (unlikely(rs & 1)) {
2850            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2851            return;
2852        }
2853        gen_set_access_type(ctx, ACCESS_INT);
2854        EA = tcg_temp_new();
2855        gen_addr_imm_index(ctx, EA, 0x03);
2856
2857        /* Note that the low part is always in RS+1, even in LE mode.  */
2858        lo = cpu_gpr[rs + 1];
2859        hi = cpu_gpr[rs];
2860
2861        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2862            if (HAVE_ATOMIC128) {
2863                TCGv_i32 oi = tcg_temp_new_i32();
2864                if (ctx->le_mode) {
2865                    tcg_gen_movi_i32(oi, make_memop_idx(MO_LEQ, ctx->mem_idx));
2866                    gen_helper_stq_le_parallel(cpu_env, EA, lo, hi, oi);
2867                } else {
2868                    tcg_gen_movi_i32(oi, make_memop_idx(MO_BEQ, ctx->mem_idx));
2869                    gen_helper_stq_be_parallel(cpu_env, EA, lo, hi, oi);
2870                }
2871                tcg_temp_free_i32(oi);
2872            } else {
2873                /* Restart with exclusive lock.  */
2874                gen_helper_exit_atomic(cpu_env);
2875                ctx->base.is_jmp = DISAS_NORETURN;
2876            }
2877        } else if (ctx->le_mode) {
2878            tcg_gen_qemu_st_i64(lo, EA, ctx->mem_idx, MO_LEQ);
2879            gen_addr_add(ctx, EA, EA, 8);
2880            tcg_gen_qemu_st_i64(hi, EA, ctx->mem_idx, MO_LEQ);
2881        } else {
2882            tcg_gen_qemu_st_i64(hi, EA, ctx->mem_idx, MO_BEQ);
2883            gen_addr_add(ctx, EA, EA, 8);
2884            tcg_gen_qemu_st_i64(lo, EA, ctx->mem_idx, MO_BEQ);
2885        }
2886        tcg_temp_free(EA);
2887    } else {
2888        /* std / stdu */
2889        if (Rc(ctx->opcode)) {
2890            if (unlikely(rA(ctx->opcode) == 0)) {
2891                gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2892                return;
2893            }
2894        }
2895        gen_set_access_type(ctx, ACCESS_INT);
2896        EA = tcg_temp_new();
2897        gen_addr_imm_index(ctx, EA, 0x03);
2898        gen_qemu_st64_i64(ctx, cpu_gpr[rs], EA);
2899        if (Rc(ctx->opcode)) {
2900            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2901        }
2902        tcg_temp_free(EA);
2903    }
2904}
2905#endif
2906/***                Integer load and store with byte reverse               ***/
2907
2908/* lhbrx */
2909GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
2910
2911/* lwbrx */
2912GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
2913
2914#if defined(TARGET_PPC64)
2915/* ldbrx */
2916GEN_LDX_E(ldbr, ld64ur_i64, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE);
2917/* stdbrx */
2918GEN_STX_E(stdbr, st64r_i64, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE);
2919#endif  /* TARGET_PPC64 */
2920
2921/* sthbrx */
2922GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
2923/* stwbrx */
2924GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
2925
2926/***                    Integer load and store multiple                    ***/
2927
2928/* lmw */
2929static void gen_lmw(DisasContext *ctx)
2930{
2931    TCGv t0;
2932    TCGv_i32 t1;
2933
2934    if (ctx->le_mode) {
2935        gen_align_no_le(ctx);
2936        return;
2937    }
2938    gen_set_access_type(ctx, ACCESS_INT);
2939    t0 = tcg_temp_new();
2940    t1 = tcg_const_i32(rD(ctx->opcode));
2941    gen_addr_imm_index(ctx, t0, 0);
2942    gen_helper_lmw(cpu_env, t0, t1);
2943    tcg_temp_free(t0);
2944    tcg_temp_free_i32(t1);
2945}
2946
2947/* stmw */
2948static void gen_stmw(DisasContext *ctx)
2949{
2950    TCGv t0;
2951    TCGv_i32 t1;
2952
2953    if (ctx->le_mode) {
2954        gen_align_no_le(ctx);
2955        return;
2956    }
2957    gen_set_access_type(ctx, ACCESS_INT);
2958    t0 = tcg_temp_new();
2959    t1 = tcg_const_i32(rS(ctx->opcode));
2960    gen_addr_imm_index(ctx, t0, 0);
2961    gen_helper_stmw(cpu_env, t0, t1);
2962    tcg_temp_free(t0);
2963    tcg_temp_free_i32(t1);
2964}
2965
2966/***                    Integer load and store strings                     ***/
2967
2968/* lswi */
2969/*
2970 * PowerPC32 specification says we must generate an exception if rA is
2971 * in the range of registers to be loaded.  In an other hand, IBM says
2972 * this is valid, but rA won't be loaded.  For now, I'll follow the
2973 * spec...
2974 */
2975static void gen_lswi(DisasContext *ctx)
2976{
2977    TCGv t0;
2978    TCGv_i32 t1, t2;
2979    int nb = NB(ctx->opcode);
2980    int start = rD(ctx->opcode);
2981    int ra = rA(ctx->opcode);
2982    int nr;
2983
2984    if (ctx->le_mode) {
2985        gen_align_no_le(ctx);
2986        return;
2987    }
2988    if (nb == 0) {
2989        nb = 32;
2990    }
2991    nr = DIV_ROUND_UP(nb, 4);
2992    if (unlikely(lsw_reg_in_range(start, nr, ra))) {
2993        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
2994        return;
2995    }
2996    gen_set_access_type(ctx, ACCESS_INT);
2997    t0 = tcg_temp_new();
2998    gen_addr_register(ctx, t0);
2999    t1 = tcg_const_i32(nb);
3000    t2 = tcg_const_i32(start);
3001    gen_helper_lsw(cpu_env, t0, t1, t2);
3002    tcg_temp_free(t0);
3003    tcg_temp_free_i32(t1);
3004    tcg_temp_free_i32(t2);
3005}
3006
3007/* lswx */
3008static void gen_lswx(DisasContext *ctx)
3009{
3010    TCGv t0;
3011    TCGv_i32 t1, t2, t3;
3012
3013    if (ctx->le_mode) {
3014        gen_align_no_le(ctx);
3015        return;
3016    }
3017    gen_set_access_type(ctx, ACCESS_INT);
3018    t0 = tcg_temp_new();
3019    gen_addr_reg_index(ctx, t0);
3020    t1 = tcg_const_i32(rD(ctx->opcode));
3021    t2 = tcg_const_i32(rA(ctx->opcode));
3022    t3 = tcg_const_i32(rB(ctx->opcode));
3023    gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3024    tcg_temp_free(t0);
3025    tcg_temp_free_i32(t1);
3026    tcg_temp_free_i32(t2);
3027    tcg_temp_free_i32(t3);
3028}
3029
3030/* stswi */
3031static void gen_stswi(DisasContext *ctx)
3032{
3033    TCGv t0;
3034    TCGv_i32 t1, t2;
3035    int nb = NB(ctx->opcode);
3036
3037    if (ctx->le_mode) {
3038        gen_align_no_le(ctx);
3039        return;
3040    }
3041    gen_set_access_type(ctx, ACCESS_INT);
3042    t0 = tcg_temp_new();
3043    gen_addr_register(ctx, t0);
3044    if (nb == 0) {
3045        nb = 32;
3046    }
3047    t1 = tcg_const_i32(nb);
3048    t2 = tcg_const_i32(rS(ctx->opcode));
3049    gen_helper_stsw(cpu_env, t0, t1, t2);
3050    tcg_temp_free(t0);
3051    tcg_temp_free_i32(t1);
3052    tcg_temp_free_i32(t2);
3053}
3054
3055/* stswx */
3056static void gen_stswx(DisasContext *ctx)
3057{
3058    TCGv t0;
3059    TCGv_i32 t1, t2;
3060
3061    if (ctx->le_mode) {
3062        gen_align_no_le(ctx);
3063        return;
3064    }
3065    gen_set_access_type(ctx, ACCESS_INT);
3066    t0 = tcg_temp_new();
3067    gen_addr_reg_index(ctx, t0);
3068    t1 = tcg_temp_new_i32();
3069    tcg_gen_trunc_tl_i32(t1, cpu_xer);
3070    tcg_gen_andi_i32(t1, t1, 0x7F);
3071    t2 = tcg_const_i32(rS(ctx->opcode));
3072    gen_helper_stsw(cpu_env, t0, t1, t2);
3073    tcg_temp_free(t0);
3074    tcg_temp_free_i32(t1);
3075    tcg_temp_free_i32(t2);
3076}
3077
3078/***                        Memory synchronisation                         ***/
3079/* eieio */
3080static void gen_eieio(DisasContext *ctx)
3081{
3082    TCGBar bar = TCG_MO_LD_ST;
3083
3084    /*
3085     * POWER9 has a eieio instruction variant using bit 6 as a hint to
3086     * tell the CPU it is a store-forwarding barrier.
3087     */
3088    if (ctx->opcode & 0x2000000) {
3089        /*
3090         * ISA says that "Reserved fields in instructions are ignored
3091         * by the processor". So ignore the bit 6 on non-POWER9 CPU but
3092         * as this is not an instruction software should be using,
3093         * complain to the user.
3094         */
3095        if (!(ctx->insns_flags2 & PPC2_ISA300)) {
3096            qemu_log_mask(LOG_GUEST_ERROR, "invalid eieio using bit 6 at @"
3097                          TARGET_FMT_lx "\n", ctx->base.pc_next - 4);
3098        } else {
3099            bar = TCG_MO_ST_LD;
3100        }
3101    }
3102
3103    tcg_gen_mb(bar | TCG_BAR_SC);
3104}
3105
3106#if !defined(CONFIG_USER_ONLY)
3107static inline void gen_check_tlb_flush(DisasContext *ctx, bool global)
3108{
3109    TCGv_i32 t;
3110    TCGLabel *l;
3111
3112    if (!ctx->lazy_tlb_flush) {
3113        return;
3114    }
3115    l = gen_new_label();
3116    t = tcg_temp_new_i32();
3117    tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
3118    tcg_gen_brcondi_i32(TCG_COND_EQ, t, 0, l);
3119    if (global) {
3120        gen_helper_check_tlb_flush_global(cpu_env);
3121    } else {
3122        gen_helper_check_tlb_flush_local(cpu_env);
3123    }
3124    gen_set_label(l);
3125    tcg_temp_free_i32(t);
3126}
3127#else
3128static inline void gen_check_tlb_flush(DisasContext *ctx, bool global) { }
3129#endif
3130
3131/* isync */
3132static void gen_isync(DisasContext *ctx)
3133{
3134    /*
3135     * We need to check for a pending TLB flush. This can only happen in
3136     * kernel mode however so check MSR_PR
3137     */
3138    if (!ctx->pr) {
3139        gen_check_tlb_flush(ctx, false);
3140    }
3141    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
3142    gen_stop_exception(ctx);
3143}
3144
3145#define MEMOP_GET_SIZE(x)  (1 << ((x) & MO_SIZE))
3146
3147static void gen_load_locked(DisasContext *ctx, MemOp memop)
3148{
3149    TCGv gpr = cpu_gpr[rD(ctx->opcode)];
3150    TCGv t0 = tcg_temp_new();
3151
3152    gen_set_access_type(ctx, ACCESS_RES);
3153    gen_addr_reg_index(ctx, t0);
3154    tcg_gen_qemu_ld_tl(gpr, t0, ctx->mem_idx, memop | MO_ALIGN);
3155    tcg_gen_mov_tl(cpu_reserve, t0);
3156    tcg_gen_mov_tl(cpu_reserve_val, gpr);
3157    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3158    tcg_temp_free(t0);
3159}
3160
3161#define LARX(name, memop)                  \
3162static void gen_##name(DisasContext *ctx)  \
3163{                                          \
3164    gen_load_locked(ctx, memop);           \
3165}
3166
3167/* lwarx */
3168LARX(lbarx, DEF_MEMOP(MO_UB))
3169LARX(lharx, DEF_MEMOP(MO_UW))
3170LARX(lwarx, DEF_MEMOP(MO_UL))
3171
3172static void gen_fetch_inc_conditional(DisasContext *ctx, MemOp memop,
3173                                      TCGv EA, TCGCond cond, int addend)
3174{
3175    TCGv t = tcg_temp_new();
3176    TCGv t2 = tcg_temp_new();
3177    TCGv u = tcg_temp_new();
3178
3179    tcg_gen_qemu_ld_tl(t, EA, ctx->mem_idx, memop);
3180    tcg_gen_addi_tl(t2, EA, MEMOP_GET_SIZE(memop));
3181    tcg_gen_qemu_ld_tl(t2, t2, ctx->mem_idx, memop);
3182    tcg_gen_addi_tl(u, t, addend);
3183
3184    /* E.g. for fetch and increment bounded... */
3185    /* mem(EA,s) = (t != t2 ? u = t + 1 : t) */
3186    tcg_gen_movcond_tl(cond, u, t, t2, u, t);
3187    tcg_gen_qemu_st_tl(u, EA, ctx->mem_idx, memop);
3188
3189    /* RT = (t != t2 ? t : u = 1<<(s*8-1)) */
3190    tcg_gen_movi_tl(u, 1 << (MEMOP_GET_SIZE(memop) * 8 - 1));
3191    tcg_gen_movcond_tl(cond, cpu_gpr[rD(ctx->opcode)], t, t2, t, u);
3192
3193    tcg_temp_free(t);
3194    tcg_temp_free(t2);
3195    tcg_temp_free(u);
3196}
3197
3198static void gen_ld_atomic(DisasContext *ctx, MemOp memop)
3199{
3200    uint32_t gpr_FC = FC(ctx->opcode);
3201    TCGv EA = tcg_temp_new();
3202    int rt = rD(ctx->opcode);
3203    bool need_serial;
3204    TCGv src, dst;
3205
3206    gen_addr_register(ctx, EA);
3207    dst = cpu_gpr[rt];
3208    src = cpu_gpr[(rt + 1) & 31];
3209
3210    need_serial = false;
3211    memop |= MO_ALIGN;
3212    switch (gpr_FC) {
3213    case 0: /* Fetch and add */
3214        tcg_gen_atomic_fetch_add_tl(dst, EA, src, ctx->mem_idx, memop);
3215        break;
3216    case 1: /* Fetch and xor */
3217        tcg_gen_atomic_fetch_xor_tl(dst, EA, src, ctx->mem_idx, memop);
3218        break;
3219    case 2: /* Fetch and or */
3220        tcg_gen_atomic_fetch_or_tl(dst, EA, src, ctx->mem_idx, memop);
3221        break;
3222    case 3: /* Fetch and 'and' */
3223        tcg_gen_atomic_fetch_and_tl(dst, EA, src, ctx->mem_idx, memop);
3224        break;
3225    case 4:  /* Fetch and max unsigned */
3226        tcg_gen_atomic_fetch_umax_tl(dst, EA, src, ctx->mem_idx, memop);
3227        break;
3228    case 5:  /* Fetch and max signed */
3229        tcg_gen_atomic_fetch_smax_tl(dst, EA, src, ctx->mem_idx, memop);
3230        break;
3231    case 6:  /* Fetch and min unsigned */
3232        tcg_gen_atomic_fetch_umin_tl(dst, EA, src, ctx->mem_idx, memop);
3233        break;
3234    case 7:  /* Fetch and min signed */
3235        tcg_gen_atomic_fetch_smin_tl(dst, EA, src, ctx->mem_idx, memop);
3236        break;
3237    case 8: /* Swap */
3238        tcg_gen_atomic_xchg_tl(dst, EA, src, ctx->mem_idx, memop);
3239        break;
3240
3241    case 16: /* Compare and swap not equal */
3242        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3243            need_serial = true;
3244        } else {
3245            TCGv t0 = tcg_temp_new();
3246            TCGv t1 = tcg_temp_new();
3247
3248            tcg_gen_qemu_ld_tl(t0, EA, ctx->mem_idx, memop);
3249            if ((memop & MO_SIZE) == MO_64 || TARGET_LONG_BITS == 32) {
3250                tcg_gen_mov_tl(t1, src);
3251            } else {
3252                tcg_gen_ext32u_tl(t1, src);
3253            }
3254            tcg_gen_movcond_tl(TCG_COND_NE, t1, t0, t1,
3255                               cpu_gpr[(rt + 2) & 31], t0);
3256            tcg_gen_qemu_st_tl(t1, EA, ctx->mem_idx, memop);
3257            tcg_gen_mov_tl(dst, t0);
3258
3259            tcg_temp_free(t0);
3260            tcg_temp_free(t1);
3261        }
3262        break;
3263
3264    case 24: /* Fetch and increment bounded */
3265        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3266            need_serial = true;
3267        } else {
3268            gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_NE, 1);
3269        }
3270        break;
3271    case 25: /* Fetch and increment equal */
3272        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3273            need_serial = true;
3274        } else {
3275            gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_EQ, 1);
3276        }
3277        break;
3278    case 28: /* Fetch and decrement bounded */
3279        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3280            need_serial = true;
3281        } else {
3282            gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_NE, -1);
3283        }
3284        break;
3285
3286    default:
3287        /* invoke data storage error handler */
3288        gen_exception_err(ctx, POWERPC_EXCP_DSI, POWERPC_EXCP_INVAL);
3289    }
3290    tcg_temp_free(EA);
3291
3292    if (need_serial) {
3293        /* Restart with exclusive lock.  */
3294        gen_helper_exit_atomic(cpu_env);
3295        ctx->base.is_jmp = DISAS_NORETURN;
3296    }
3297}
3298
3299static void gen_lwat(DisasContext *ctx)
3300{
3301    gen_ld_atomic(ctx, DEF_MEMOP(MO_UL));
3302}
3303
3304#ifdef TARGET_PPC64
3305static void gen_ldat(DisasContext *ctx)
3306{
3307    gen_ld_atomic(ctx, DEF_MEMOP(MO_Q));
3308}
3309#endif
3310
3311static void gen_st_atomic(DisasContext *ctx, MemOp memop)
3312{
3313    uint32_t gpr_FC = FC(ctx->opcode);
3314    TCGv EA = tcg_temp_new();
3315    TCGv src, discard;
3316
3317    gen_addr_register(ctx, EA);
3318    src = cpu_gpr[rD(ctx->opcode)];
3319    discard = tcg_temp_new();
3320
3321    memop |= MO_ALIGN;
3322    switch (gpr_FC) {
3323    case 0: /* add and Store */
3324        tcg_gen_atomic_add_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3325        break;
3326    case 1: /* xor and Store */
3327        tcg_gen_atomic_xor_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3328        break;
3329    case 2: /* Or and Store */
3330        tcg_gen_atomic_or_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3331        break;
3332    case 3: /* 'and' and Store */
3333        tcg_gen_atomic_and_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3334        break;
3335    case 4:  /* Store max unsigned */
3336        tcg_gen_atomic_umax_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3337        break;
3338    case 5:  /* Store max signed */
3339        tcg_gen_atomic_smax_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3340        break;
3341    case 6:  /* Store min unsigned */
3342        tcg_gen_atomic_umin_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3343        break;
3344    case 7:  /* Store min signed */
3345        tcg_gen_atomic_smin_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3346        break;
3347    case 24: /* Store twin  */
3348        if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3349            /* Restart with exclusive lock.  */
3350            gen_helper_exit_atomic(cpu_env);
3351            ctx->base.is_jmp = DISAS_NORETURN;
3352        } else {
3353            TCGv t = tcg_temp_new();
3354            TCGv t2 = tcg_temp_new();
3355            TCGv s = tcg_temp_new();
3356            TCGv s2 = tcg_temp_new();
3357            TCGv ea_plus_s = tcg_temp_new();
3358
3359            tcg_gen_qemu_ld_tl(t, EA, ctx->mem_idx, memop);
3360            tcg_gen_addi_tl(ea_plus_s, EA, MEMOP_GET_SIZE(memop));
3361            tcg_gen_qemu_ld_tl(t2, ea_plus_s, ctx->mem_idx, memop);
3362            tcg_gen_movcond_tl(TCG_COND_EQ, s, t, t2, src, t);
3363            tcg_gen_movcond_tl(TCG_COND_EQ, s2, t, t2, src, t2);
3364            tcg_gen_qemu_st_tl(s, EA, ctx->mem_idx, memop);
3365            tcg_gen_qemu_st_tl(s2, ea_plus_s, ctx->mem_idx, memop);
3366
3367            tcg_temp_free(ea_plus_s);
3368            tcg_temp_free(s2);
3369            tcg_temp_free(s);
3370            tcg_temp_free(t2);
3371            tcg_temp_free(t);
3372        }
3373        break;
3374    default:
3375        /* invoke data storage error handler */
3376        gen_exception_err(ctx, POWERPC_EXCP_DSI, POWERPC_EXCP_INVAL);
3377    }
3378    tcg_temp_free(discard);
3379    tcg_temp_free(EA);
3380}
3381
3382static void gen_stwat(DisasContext *ctx)
3383{
3384    gen_st_atomic(ctx, DEF_MEMOP(MO_UL));
3385}
3386
3387#ifdef TARGET_PPC64
3388static void gen_stdat(DisasContext *ctx)
3389{
3390    gen_st_atomic(ctx, DEF_MEMOP(MO_Q));
3391}
3392#endif
3393
3394static void gen_conditional_store(DisasContext *ctx, MemOp memop)
3395{
3396    TCGLabel *l1 = gen_new_label();
3397    TCGLabel *l2 = gen_new_label();
3398    TCGv t0 = tcg_temp_new();
3399    int reg = rS(ctx->opcode);
3400
3401    gen_set_access_type(ctx, ACCESS_RES);
3402    gen_addr_reg_index(ctx, t0);
3403    tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3404    tcg_temp_free(t0);
3405
3406    t0 = tcg_temp_new();
3407    tcg_gen_atomic_cmpxchg_tl(t0, cpu_reserve, cpu_reserve_val,
3408                              cpu_gpr[reg], ctx->mem_idx,
3409                              DEF_MEMOP(memop) | MO_ALIGN);
3410    tcg_gen_setcond_tl(TCG_COND_EQ, t0, t0, cpu_reserve_val);
3411    tcg_gen_shli_tl(t0, t0, CRF_EQ_BIT);
3412    tcg_gen_or_tl(t0, t0, cpu_so);
3413    tcg_gen_trunc_tl_i32(cpu_crf[0], t0);
3414    tcg_temp_free(t0);
3415    tcg_gen_br(l2);
3416
3417    gen_set_label(l1);
3418
3419    /*
3420     * Address mismatch implies failure.  But we still need to provide
3421     * the memory barrier semantics of the instruction.
3422     */
3423    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3424    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3425
3426    gen_set_label(l2);
3427    tcg_gen_movi_tl(cpu_reserve, -1);
3428}
3429
3430#define STCX(name, memop)                  \
3431static void gen_##name(DisasContext *ctx)  \
3432{                                          \
3433    gen_conditional_store(ctx, memop);     \
3434}
3435
3436STCX(stbcx_, DEF_MEMOP(MO_UB))
3437STCX(sthcx_, DEF_MEMOP(MO_UW))
3438STCX(stwcx_, DEF_MEMOP(MO_UL))
3439
3440#if defined(TARGET_PPC64)
3441/* ldarx */
3442LARX(ldarx, DEF_MEMOP(MO_Q))
3443/* stdcx. */
3444STCX(stdcx_, DEF_MEMOP(MO_Q))
3445
3446/* lqarx */
3447static void gen_lqarx(DisasContext *ctx)
3448{
3449    int rd = rD(ctx->opcode);
3450    TCGv EA, hi, lo;
3451
3452    if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3453                 (rd == rB(ctx->opcode)))) {
3454        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3455        return;
3456    }
3457
3458    gen_set_access_type(ctx, ACCESS_RES);
3459    EA = tcg_temp_new();
3460    gen_addr_reg_index(ctx, EA);
3461
3462    /* Note that the low part is always in RD+1, even in LE mode.  */
3463    lo = cpu_gpr[rd + 1];
3464    hi = cpu_gpr[rd];
3465
3466    if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3467        if (HAVE_ATOMIC128) {
3468            TCGv_i32 oi = tcg_temp_new_i32();
3469            if (ctx->le_mode) {
3470                tcg_gen_movi_i32(oi, make_memop_idx(MO_LEQ | MO_ALIGN_16,
3471                                                    ctx->mem_idx));
3472                gen_helper_lq_le_parallel(lo, cpu_env, EA, oi);
3473            } else {
3474                tcg_gen_movi_i32(oi, make_memop_idx(MO_BEQ | MO_ALIGN_16,
3475                                                    ctx->mem_idx));
3476                gen_helper_lq_be_parallel(lo, cpu_env, EA, oi);
3477            }
3478            tcg_temp_free_i32(oi);
3479            tcg_gen_ld_i64(hi, cpu_env, offsetof(CPUPPCState, retxh));
3480        } else {
3481            /* Restart with exclusive lock.  */
3482            gen_helper_exit_atomic(cpu_env);
3483            ctx->base.is_jmp = DISAS_NORETURN;
3484            tcg_temp_free(EA);
3485            return;
3486        }
3487    } else if (ctx->le_mode) {
3488        tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_LEQ | MO_ALIGN_16);
3489        tcg_gen_mov_tl(cpu_reserve, EA);
3490        gen_addr_add(ctx, EA, EA, 8);
3491        tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_LEQ);
3492    } else {
3493        tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_BEQ | MO_ALIGN_16);
3494        tcg_gen_mov_tl(cpu_reserve, EA);
3495        gen_addr_add(ctx, EA, EA, 8);
3496        tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_BEQ);
3497    }
3498    tcg_temp_free(EA);
3499
3500    tcg_gen_st_tl(hi, cpu_env, offsetof(CPUPPCState, reserve_val));
3501    tcg_gen_st_tl(lo, cpu_env, offsetof(CPUPPCState, reserve_val2));
3502}
3503
3504/* stqcx. */
3505static void gen_stqcx_(DisasContext *ctx)
3506{
3507    int rs = rS(ctx->opcode);
3508    TCGv EA, hi, lo;
3509
3510    if (unlikely(rs & 1)) {
3511        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3512        return;
3513    }
3514
3515    gen_set_access_type(ctx, ACCESS_RES);
3516    EA = tcg_temp_new();
3517    gen_addr_reg_index(ctx, EA);
3518
3519    /* Note that the low part is always in RS+1, even in LE mode.  */
3520    lo = cpu_gpr[rs + 1];
3521    hi = cpu_gpr[rs];
3522
3523    if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3524        if (HAVE_CMPXCHG128) {
3525            TCGv_i32 oi = tcg_const_i32(DEF_MEMOP(MO_Q) | MO_ALIGN_16);
3526            if (ctx->le_mode) {
3527                gen_helper_stqcx_le_parallel(cpu_crf[0], cpu_env,
3528                                             EA, lo, hi, oi);
3529            } else {
3530                gen_helper_stqcx_be_parallel(cpu_crf[0], cpu_env,
3531                                             EA, lo, hi, oi);
3532            }
3533            tcg_temp_free_i32(oi);
3534        } else {
3535            /* Restart with exclusive lock.  */
3536            gen_helper_exit_atomic(cpu_env);
3537            ctx->base.is_jmp = DISAS_NORETURN;
3538        }
3539        tcg_temp_free(EA);
3540    } else {
3541        TCGLabel *lab_fail = gen_new_label();
3542        TCGLabel *lab_over = gen_new_label();
3543        TCGv_i64 t0 = tcg_temp_new_i64();
3544        TCGv_i64 t1 = tcg_temp_new_i64();
3545
3546        tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, lab_fail);
3547        tcg_temp_free(EA);
3548
3549        gen_qemu_ld64_i64(ctx, t0, cpu_reserve);
3550        tcg_gen_ld_i64(t1, cpu_env, (ctx->le_mode
3551                                     ? offsetof(CPUPPCState, reserve_val2)
3552                                     : offsetof(CPUPPCState, reserve_val)));
3553        tcg_gen_brcond_i64(TCG_COND_NE, t0, t1, lab_fail);
3554
3555        tcg_gen_addi_i64(t0, cpu_reserve, 8);
3556        gen_qemu_ld64_i64(ctx, t0, t0);
3557        tcg_gen_ld_i64(t1, cpu_env, (ctx->le_mode
3558                                     ? offsetof(CPUPPCState, reserve_val)
3559                                     : offsetof(CPUPPCState, reserve_val2)));
3560        tcg_gen_brcond_i64(TCG_COND_NE, t0, t1, lab_fail);
3561
3562        /* Success */
3563        gen_qemu_st64_i64(ctx, ctx->le_mode ? lo : hi, cpu_reserve);
3564        tcg_gen_addi_i64(t0, cpu_reserve, 8);
3565        gen_qemu_st64_i64(ctx, ctx->le_mode ? hi : lo, t0);
3566
3567        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3568        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], CRF_EQ);
3569        tcg_gen_br(lab_over);
3570
3571        gen_set_label(lab_fail);
3572        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3573
3574        gen_set_label(lab_over);
3575        tcg_gen_movi_tl(cpu_reserve, -1);
3576        tcg_temp_free_i64(t0);
3577        tcg_temp_free_i64(t1);
3578    }
3579}
3580#endif /* defined(TARGET_PPC64) */
3581
3582/* sync */
3583static void gen_sync(DisasContext *ctx)
3584{
3585    uint32_t l = (ctx->opcode >> 21) & 3;
3586
3587    /*
3588     * We may need to check for a pending TLB flush.
3589     *
3590     * We do this on ptesync (l == 2) on ppc64 and any sync pn ppc32.
3591     *
3592     * Additionally, this can only happen in kernel mode however so
3593     * check MSR_PR as well.
3594     */
3595    if (((l == 2) || !(ctx->insns_flags & PPC_64B)) && !ctx->pr) {
3596        gen_check_tlb_flush(ctx, true);
3597    }
3598    tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
3599}
3600
3601/* wait */
3602static void gen_wait(DisasContext *ctx)
3603{
3604    TCGv_i32 t0 = tcg_const_i32(1);
3605    tcg_gen_st_i32(t0, cpu_env,
3606                   -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
3607    tcg_temp_free_i32(t0);
3608    /* Stop translation, as the CPU is supposed to sleep from now */
3609    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3610}
3611
3612#if defined(TARGET_PPC64)
3613static void gen_doze(DisasContext *ctx)
3614{
3615#if defined(CONFIG_USER_ONLY)
3616    GEN_PRIV;
3617#else
3618    TCGv_i32 t;
3619
3620    CHK_HV;
3621    t = tcg_const_i32(PPC_PM_DOZE);
3622    gen_helper_pminsn(cpu_env, t);
3623    tcg_temp_free_i32(t);
3624    /* Stop translation, as the CPU is supposed to sleep from now */
3625    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3626#endif /* defined(CONFIG_USER_ONLY) */
3627}
3628
3629static void gen_nap(DisasContext *ctx)
3630{
3631#if defined(CONFIG_USER_ONLY)
3632    GEN_PRIV;
3633#else
3634    TCGv_i32 t;
3635
3636    CHK_HV;
3637    t = tcg_const_i32(PPC_PM_NAP);
3638    gen_helper_pminsn(cpu_env, t);
3639    tcg_temp_free_i32(t);
3640    /* Stop translation, as the CPU is supposed to sleep from now */
3641    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3642#endif /* defined(CONFIG_USER_ONLY) */
3643}
3644
3645static void gen_stop(DisasContext *ctx)
3646{
3647#if defined(CONFIG_USER_ONLY)
3648    GEN_PRIV;
3649#else
3650    TCGv_i32 t;
3651
3652    CHK_HV;
3653    t = tcg_const_i32(PPC_PM_STOP);
3654    gen_helper_pminsn(cpu_env, t);
3655    tcg_temp_free_i32(t);
3656    /* Stop translation, as the CPU is supposed to sleep from now */
3657    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3658#endif /* defined(CONFIG_USER_ONLY) */
3659}
3660
3661static void gen_sleep(DisasContext *ctx)
3662{
3663#if defined(CONFIG_USER_ONLY)
3664    GEN_PRIV;
3665#else
3666    TCGv_i32 t;
3667
3668    CHK_HV;
3669    t = tcg_const_i32(PPC_PM_SLEEP);
3670    gen_helper_pminsn(cpu_env, t);
3671    tcg_temp_free_i32(t);
3672    /* Stop translation, as the CPU is supposed to sleep from now */
3673    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3674#endif /* defined(CONFIG_USER_ONLY) */
3675}
3676
3677static void gen_rvwinkle(DisasContext *ctx)
3678{
3679#if defined(CONFIG_USER_ONLY)
3680    GEN_PRIV;
3681#else
3682    TCGv_i32 t;
3683
3684    CHK_HV;
3685    t = tcg_const_i32(PPC_PM_RVWINKLE);
3686    gen_helper_pminsn(cpu_env, t);
3687    tcg_temp_free_i32(t);
3688    /* Stop translation, as the CPU is supposed to sleep from now */
3689    gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3690#endif /* defined(CONFIG_USER_ONLY) */
3691}
3692#endif /* #if defined(TARGET_PPC64) */
3693
3694static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3695{
3696#if defined(TARGET_PPC64)
3697    if (ctx->has_cfar) {
3698        tcg_gen_movi_tl(cpu_cfar, nip);
3699    }
3700#endif
3701}
3702
3703static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
3704{
3705    if (unlikely(ctx->singlestep_enabled)) {
3706        return false;
3707    }
3708
3709#ifndef CONFIG_USER_ONLY
3710    return (ctx->base.tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
3711#else
3712    return true;
3713#endif
3714}
3715
3716static void gen_lookup_and_goto_ptr(DisasContext *ctx)
3717{
3718    int sse = ctx->singlestep_enabled;
3719    if (unlikely(sse)) {
3720        if (sse & GDBSTUB_SINGLE_STEP) {
3721            gen_debug_exception(ctx);
3722        } else if (sse & (CPU_SINGLE_STEP | CPU_BRANCH_STEP)) {
3723            uint32_t excp = gen_prep_dbgex(ctx);
3724            gen_exception(ctx, excp);
3725        }
3726        tcg_gen_exit_tb(NULL, 0);
3727    } else {
3728        tcg_gen_lookup_and_goto_ptr();
3729    }
3730}
3731
3732/***                                Branch                                 ***/
3733static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
3734{
3735    if (NARROW_MODE(ctx)) {
3736        dest = (uint32_t) dest;
3737    }
3738    if (use_goto_tb(ctx, dest)) {
3739        tcg_gen_goto_tb(n);
3740        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3741        tcg_gen_exit_tb(ctx->base.tb, n);
3742    } else {
3743        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3744        gen_lookup_and_goto_ptr(ctx);
3745    }
3746}
3747
3748static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
3749{
3750    if (NARROW_MODE(ctx)) {
3751        nip = (uint32_t)nip;
3752    }
3753    tcg_gen_movi_tl(cpu_lr, nip);
3754}
3755
3756/* b ba bl bla */
3757static void gen_b(DisasContext *ctx)
3758{
3759    target_ulong li, target;
3760
3761    ctx->exception = POWERPC_EXCP_BRANCH;
3762    /* sign extend LI */
3763    li = LI(ctx->opcode);
3764    li = (li ^ 0x02000000) - 0x02000000;
3765    if (likely(AA(ctx->opcode) == 0)) {
3766        target = ctx->base.pc_next + li - 4;
3767    } else {
3768        target = li;
3769    }
3770    if (LK(ctx->opcode)) {
3771        gen_setlr(ctx, ctx->base.pc_next);
3772    }
3773    gen_update_cfar(ctx, ctx->base.pc_next - 4);
3774    gen_goto_tb(ctx, 0, target);
3775}
3776
3777#define BCOND_IM  0
3778#define BCOND_LR  1
3779#define BCOND_CTR 2
3780#define BCOND_TAR 3
3781
3782static void gen_bcond(DisasContext *ctx, int type)
3783{
3784    uint32_t bo = BO(ctx->opcode);
3785    TCGLabel *l1;
3786    TCGv target;
3787    ctx->exception = POWERPC_EXCP_BRANCH;
3788
3789    if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
3790        target = tcg_temp_local_new();
3791        if (type == BCOND_CTR) {
3792            tcg_gen_mov_tl(target, cpu_ctr);
3793        } else if (type == BCOND_TAR) {
3794            gen_load_spr(target, SPR_TAR);
3795        } else {
3796            tcg_gen_mov_tl(target, cpu_lr);
3797        }
3798    } else {
3799        target = NULL;
3800    }
3801    if (LK(ctx->opcode)) {
3802        gen_setlr(ctx, ctx->base.pc_next);
3803    }
3804    l1 = gen_new_label();
3805    if ((bo & 0x4) == 0) {
3806        /* Decrement and test CTR */
3807        TCGv temp = tcg_temp_new();
3808
3809        if (type == BCOND_CTR) {
3810            /*
3811             * All ISAs up to v3 describe this form of bcctr as invalid but
3812             * some processors, ie. 64-bit server processors compliant with
3813             * arch 2.x, do implement a "test and decrement" logic instead,
3814             * as described in their respective UMs. This logic involves CTR
3815             * to act as both the branch target and a counter, which makes
3816             * it basically useless and thus never used in real code.
3817             *
3818             * This form was hence chosen to trigger extra micro-architectural
3819             * side-effect on real HW needed for the Spectre v2 workaround.
3820             * It is up to guests that implement such workaround, ie. linux, to
3821             * use this form in a way it just triggers the side-effect without
3822             * doing anything else harmful.
3823             */
3824            if (unlikely(!is_book3s_arch2x(ctx))) {
3825                gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3826                tcg_temp_free(temp);
3827                tcg_temp_free(target);
3828                return;
3829            }
3830
3831            if (NARROW_MODE(ctx)) {
3832                tcg_gen_ext32u_tl(temp, cpu_ctr);
3833            } else {
3834                tcg_gen_mov_tl(temp, cpu_ctr);
3835            }
3836            if (bo & 0x2) {
3837                tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3838            } else {
3839                tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3840            }
3841            tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3842        } else {
3843            tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3844            if (NARROW_MODE(ctx)) {
3845                tcg_gen_ext32u_tl(temp, cpu_ctr);
3846            } else {
3847                tcg_gen_mov_tl(temp, cpu_ctr);
3848            }
3849            if (bo & 0x2) {
3850                tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3851            } else {
3852                tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3853            }
3854        }
3855        tcg_temp_free(temp);
3856    }
3857    if ((bo & 0x10) == 0) {
3858        /* Test CR */
3859        uint32_t bi = BI(ctx->opcode);
3860        uint32_t mask = 0x08 >> (bi & 0x03);
3861        TCGv_i32 temp = tcg_temp_new_i32();
3862
3863        if (bo & 0x8) {
3864            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3865            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3866        } else {
3867            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3868            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3869        }
3870        tcg_temp_free_i32(temp);
3871    }
3872    gen_update_cfar(ctx, ctx->base.pc_next - 4);
3873    if (type == BCOND_IM) {
3874        target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3875        if (likely(AA(ctx->opcode) == 0)) {
3876            gen_goto_tb(ctx, 0, ctx->base.pc_next + li - 4);
3877        } else {
3878            gen_goto_tb(ctx, 0, li);
3879        }
3880    } else {
3881        if (NARROW_MODE(ctx)) {
3882            tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3883        } else {
3884            tcg_gen_andi_tl(cpu_nip, target, ~3);
3885        }
3886        gen_lookup_and_goto_ptr(ctx);
3887        tcg_temp_free(target);
3888    }
3889    if ((bo & 0x14) != 0x14) {
3890        /* fallthrough case */
3891        gen_set_label(l1);
3892        gen_goto_tb(ctx, 1, ctx->base.pc_next);
3893    }
3894}
3895
3896static void gen_bc(DisasContext *ctx)
3897{
3898    gen_bcond(ctx, BCOND_IM);
3899}
3900
3901static void gen_bcctr(DisasContext *ctx)
3902{
3903    gen_bcond(ctx, BCOND_CTR);
3904}
3905
3906static void gen_bclr(DisasContext *ctx)
3907{
3908    gen_bcond(ctx, BCOND_LR);
3909}
3910
3911static void gen_bctar(DisasContext *ctx)
3912{
3913    gen_bcond(ctx, BCOND_TAR);
3914}
3915
3916/***                      Condition register logical                       ***/
3917#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3918static void glue(gen_, name)(DisasContext *ctx)                               \
3919{                                                                             \
3920    uint8_t bitmask;                                                          \
3921    int sh;                                                                   \
3922    TCGv_i32 t0, t1;                                                          \
3923    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3924    t0 = tcg_temp_new_i32();                                                  \
3925    if (sh > 0)                                                               \
3926        tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3927    else if (sh < 0)                                                          \
3928        tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3929    else                                                                      \
3930        tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3931    t1 = tcg_temp_new_i32();                                                  \
3932    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3933    if (sh > 0)                                                               \
3934        tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3935    else if (sh < 0)                                                          \
3936        tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3937    else                                                                      \
3938        tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3939    tcg_op(t0, t0, t1);                                                       \
3940    bitmask = 0x08 >> (crbD(ctx->opcode) & 0x03);                             \
3941    tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3942    tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3943    tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3944    tcg_temp_free_i32(t0);                                                    \
3945    tcg_temp_free_i32(t1);                                                    \
3946}
3947
3948/* crand */
3949GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3950/* crandc */
3951GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3952/* creqv */
3953GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3954/* crnand */
3955GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3956/* crnor */
3957GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3958/* cror */
3959GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3960/* crorc */
3961GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3962/* crxor */
3963GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3964
3965/* mcrf */
3966static void gen_mcrf(DisasContext *ctx)
3967{
3968    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3969}
3970
3971/***                           System linkage                              ***/
3972
3973/* rfi (supervisor only) */
3974static void gen_rfi(DisasContext *ctx)
3975{
3976#if defined(CONFIG_USER_ONLY)
3977    GEN_PRIV;
3978#else
3979    /*
3980     * This instruction doesn't exist anymore on 64-bit server
3981     * processors compliant with arch 2.x
3982     */
3983    if (is_book3s_arch2x(ctx)) {
3984        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3985        return;
3986    }
3987    /* Restore CPU state */
3988    CHK_SV;
3989    if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
3990        gen_io_start();
3991    }
3992    gen_update_cfar(ctx, ctx->base.pc_next - 4);
3993    gen_helper_rfi(cpu_env);
3994    gen_sync_exception(ctx);
3995#endif
3996}
3997
3998#if defined(TARGET_PPC64)
3999static void gen_rfid(DisasContext *ctx)
4000{
4001#if defined(CONFIG_USER_ONLY)
4002    GEN_PRIV;
4003#else
4004    /* Restore CPU state */
4005    CHK_SV;
4006    if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
4007        gen_io_start();
4008    }
4009    gen_update_cfar(ctx, ctx->base.pc_next - 4);
4010    gen_helper_rfid(cpu_env);
4011    gen_sync_exception(ctx);
4012#endif
4013}
4014
4015static void gen_hrfid(DisasContext *ctx)
4016{
4017#if defined(CONFIG_USER_ONLY)
4018    GEN_PRIV;
4019#else
4020    /* Restore CPU state */
4021    CHK_HV;
4022    gen_helper_hrfid(cpu_env);
4023    gen_sync_exception(ctx);
4024#endif
4025}
4026#endif
4027
4028/* sc */
4029#if defined(CONFIG_USER_ONLY)
4030#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4031#else
4032#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4033#endif
4034static void gen_sc(DisasContext *ctx)
4035{
4036    uint32_t lev;
4037
4038    lev = (ctx->opcode >> 5) & 0x7F;
4039    gen_exception_err(ctx, POWERPC_SYSCALL, lev);
4040}
4041
4042/***                                Trap                                   ***/
4043
4044/* Check for unconditional traps (always or never) */
4045static bool check_unconditional_trap(DisasContext *ctx)
4046{
4047    /* Trap never */
4048    if (TO(ctx->opcode) == 0) {
4049        return true;
4050    }
4051    /* Trap always */
4052    if (TO(ctx->opcode) == 31) {
4053        gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
4054        return true;
4055    }
4056    return false;
4057}
4058
4059/* tw */
4060static void gen_tw(DisasContext *ctx)
4061{
4062    TCGv_i32 t0;
4063
4064    if (check_unconditional_trap(ctx)) {
4065        return;
4066    }
4067    t0 = tcg_const_i32(TO(ctx->opcode));
4068    gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4069                  t0);
4070    tcg_temp_free_i32(t0);
4071}
4072
4073/* twi */
4074static void gen_twi(DisasContext *ctx)
4075{
4076    TCGv t0;
4077    TCGv_i32 t1;
4078
4079    if (check_unconditional_trap(ctx)) {
4080        return;
4081    }
4082    t0 = tcg_const_tl(SIMM(ctx->opcode));
4083    t1 = tcg_const_i32(TO(ctx->opcode));
4084    gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4085    tcg_temp_free(t0);
4086    tcg_temp_free_i32(t1);
4087}
4088
4089#if defined(TARGET_PPC64)
4090/* td */
4091static void gen_td(DisasContext *ctx)
4092{
4093    TCGv_i32 t0;
4094
4095    if (check_unconditional_trap(ctx)) {
4096        return;
4097    }
4098    t0 = tcg_const_i32(TO(ctx->opcode));
4099    gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4100                  t0);
4101    tcg_temp_free_i32(t0);
4102}
4103
4104/* tdi */
4105static void gen_tdi(DisasContext *ctx)
4106{
4107    TCGv t0;
4108    TCGv_i32 t1;
4109
4110    if (check_unconditional_trap(ctx)) {
4111        return;
4112    }
4113    t0 = tcg_const_tl(SIMM(ctx->opcode));
4114    t1 = tcg_const_i32(TO(ctx->opcode));
4115    gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4116    tcg_temp_free(t0);
4117    tcg_temp_free_i32(t1);
4118}
4119#endif
4120
4121/***                          Processor control                            ***/
4122
4123static void gen_read_xer(DisasContext *ctx, TCGv dst)
4124{
4125    TCGv t0 = tcg_temp_new();
4126    TCGv t1 = tcg_temp_new();
4127    TCGv t2 = tcg_temp_new();
4128    tcg_gen_mov_tl(dst, cpu_xer);
4129    tcg_gen_shli_tl(t0, cpu_so, XER_SO);
4130    tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
4131    tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
4132    tcg_gen_or_tl(t0, t0, t1);
4133    tcg_gen_or_tl(dst, dst, t2);
4134    tcg_gen_or_tl(dst, dst, t0);
4135    if (is_isa300(ctx)) {
4136        tcg_gen_shli_tl(t0, cpu_ov32, XER_OV32);
4137        tcg_gen_or_tl(dst, dst, t0);
4138        tcg_gen_shli_tl(t0, cpu_ca32, XER_CA32);
4139        tcg_gen_or_tl(dst, dst, t0);
4140    }
4141    tcg_temp_free(t0);
4142    tcg_temp_free(t1);
4143    tcg_temp_free(t2);
4144}
4145
4146static void gen_write_xer(TCGv src)
4147{
4148    /* Write all flags, while reading back check for isa300 */
4149    tcg_gen_andi_tl(cpu_xer, src,
4150                    ~((1u << XER_SO) |
4151                      (1u << XER_OV) | (1u << XER_OV32) |
4152                      (1u << XER_CA) | (1u << XER_CA32)));
4153    tcg_gen_extract_tl(cpu_ov32, src, XER_OV32, 1);
4154    tcg_gen_extract_tl(cpu_ca32, src, XER_CA32, 1);
4155    tcg_gen_extract_tl(cpu_so, src, XER_SO, 1);
4156    tcg_gen_extract_tl(cpu_ov, src, XER_OV, 1);
4157    tcg_gen_extract_tl(cpu_ca, src, XER_CA, 1);
4158}
4159
4160/* mcrxr */
4161static void gen_mcrxr(DisasContext *ctx)
4162{
4163    TCGv_i32 t0 = tcg_temp_new_i32();
4164    TCGv_i32 t1 = tcg_temp_new_i32();
4165    TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4166
4167    tcg_gen_trunc_tl_i32(t0, cpu_so);
4168    tcg_gen_trunc_tl_i32(t1, cpu_ov);
4169    tcg_gen_trunc_tl_i32(dst, cpu_ca);
4170    tcg_gen_shli_i32(t0, t0, 3);
4171    tcg_gen_shli_i32(t1, t1, 2);
4172    tcg_gen_shli_i32(dst, dst, 1);
4173    tcg_gen_or_i32(dst, dst, t0);
4174    tcg_gen_or_i32(dst, dst, t1);
4175    tcg_temp_free_i32(t0);
4176    tcg_temp_free_i32(t1);
4177
4178    tcg_gen_movi_tl(cpu_so, 0);
4179    tcg_gen_movi_tl(cpu_ov, 0);
4180    tcg_gen_movi_tl(cpu_ca, 0);
4181}
4182
4183#ifdef TARGET_PPC64
4184/* mcrxrx */
4185static void gen_mcrxrx(DisasContext *ctx)
4186{
4187    TCGv t0 = tcg_temp_new();
4188    TCGv t1 = tcg_temp_new();
4189    TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4190
4191    /* copy OV and OV32 */
4192    tcg_gen_shli_tl(t0, cpu_ov, 1);
4193    tcg_gen_or_tl(t0, t0, cpu_ov32);
4194    tcg_gen_shli_tl(t0, t0, 2);
4195    /* copy CA and CA32 */
4196    tcg_gen_shli_tl(t1, cpu_ca, 1);
4197    tcg_gen_or_tl(t1, t1, cpu_ca32);
4198    tcg_gen_or_tl(t0, t0, t1);
4199    tcg_gen_trunc_tl_i32(dst, t0);
4200    tcg_temp_free(t0);
4201    tcg_temp_free(t1);
4202}
4203#endif
4204
4205/* mfcr mfocrf */
4206static void gen_mfcr(DisasContext *ctx)
4207{
4208    uint32_t crm, crn;
4209
4210    if (likely(ctx->opcode & 0x00100000)) {
4211        crm = CRM(ctx->opcode);
4212        if (likely(crm && ((crm & (crm - 1)) == 0))) {
4213            crn = ctz32(crm);
4214            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
4215            tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4216                            cpu_gpr[rD(ctx->opcode)], crn * 4);
4217        }
4218    } else {
4219        TCGv_i32 t0 = tcg_temp_new_i32();
4220        tcg_gen_mov_i32(t0, cpu_crf[0]);
4221        tcg_gen_shli_i32(t0, t0, 4);
4222        tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4223        tcg_gen_shli_i32(t0, t0, 4);
4224        tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4225        tcg_gen_shli_i32(t0, t0, 4);
4226        tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4227        tcg_gen_shli_i32(t0, t0, 4);
4228        tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4229        tcg_gen_shli_i32(t0, t0, 4);
4230        tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4231        tcg_gen_shli_i32(t0, t0, 4);
4232        tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4233        tcg_gen_shli_i32(t0, t0, 4);
4234        tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4235        tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4236        tcg_temp_free_i32(t0);
4237    }
4238}
4239
4240/* mfmsr */
4241static void gen_mfmsr(DisasContext *ctx)
4242{
4243    CHK_SV;
4244    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4245}
4246
4247static void spr_noaccess(DisasContext *ctx, int gprn, int sprn)
4248{
4249#if 0
4250    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4251    printf("ERROR: try to access SPR %d !\n", sprn);
4252#endif
4253}
4254#define SPR_NOACCESS (&spr_noaccess)
4255
4256/* mfspr */
4257static inline void gen_op_mfspr(DisasContext *ctx)
4258{
4259    void (*read_cb)(DisasContext *ctx, int gprn, int sprn);
4260    uint32_t sprn = SPR(ctx->opcode);
4261
4262#if defined(CONFIG_USER_ONLY)
4263    read_cb = ctx->spr_cb[sprn].uea_read;
4264#else
4265    if (ctx->pr) {
4266        read_cb = ctx->spr_cb[sprn].uea_read;
4267    } else if (ctx->hv) {
4268        read_cb = ctx->spr_cb[sprn].hea_read;
4269    } else {
4270        read_cb = ctx->spr_cb[sprn].oea_read;
4271    }
4272#endif
4273    if (likely(read_cb != NULL)) {
4274        if (likely(read_cb != SPR_NOACCESS)) {
4275            (*read_cb)(ctx, rD(ctx->opcode), sprn);
4276        } else {
4277            /* Privilege exception */
4278            /*
4279             * This is a hack to avoid warnings when running Linux:
4280             * this OS breaks the PowerPC virtualisation model,
4281             * allowing userland application to read the PVR
4282             */
4283            if (sprn != SPR_PVR) {
4284                qemu_log_mask(LOG_GUEST_ERROR, "Trying to read privileged spr "
4285                              "%d (0x%03x) at " TARGET_FMT_lx "\n", sprn, sprn,
4286                              ctx->base.pc_next - 4);
4287            }
4288            gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4289        }
4290    } else {
4291        /* ISA 2.07 defines these as no-ops */
4292        if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4293            (sprn >= 808 && sprn <= 811)) {
4294            /* This is a nop */
4295            return;
4296        }
4297        /* Not defined */
4298        qemu_log_mask(LOG_GUEST_ERROR,
4299                      "Trying to read invalid spr %d (0x%03x) at "
4300                      TARGET_FMT_lx "\n", sprn, sprn, ctx->base.pc_next - 4);
4301
4302        /*
4303         * The behaviour depends on MSR:PR and SPR# bit 0x10, it can
4304         * generate a priv, a hv emu or a no-op
4305         */
4306        if (sprn & 0x10) {
4307            if (ctx->pr) {
4308                gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4309            }
4310        } else {
4311            if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {
4312                gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4313            }
4314        }
4315    }
4316}
4317
4318static void gen_mfspr(DisasContext *ctx)
4319{
4320    gen_op_mfspr(ctx);
4321}
4322
4323/* mftb */
4324static void gen_mftb(DisasContext *ctx)
4325{
4326    gen_op_mfspr(ctx);
4327}
4328
4329/* mtcrf mtocrf*/
4330static void gen_mtcrf(DisasContext *ctx)
4331{
4332    uint32_t crm, crn;
4333
4334    crm = CRM(ctx->opcode);
4335    if (likely((ctx->opcode & 0x00100000))) {
4336        if (crm && ((crm & (crm - 1)) == 0)) {
4337            TCGv_i32 temp = tcg_temp_new_i32();
4338            crn = ctz32(crm);
4339            tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4340            tcg_gen_shri_i32(temp, temp, crn * 4);
4341            tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4342            tcg_temp_free_i32(temp);
4343        }
4344    } else {
4345        TCGv_i32 temp = tcg_temp_new_i32();
4346        tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4347        for (crn = 0 ; crn < 8 ; crn++) {
4348            if (crm & (1 << crn)) {
4349                    tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4350                    tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4351            }
4352        }
4353        tcg_temp_free_i32(temp);
4354    }
4355}
4356
4357/* mtmsr */
4358#if defined(TARGET_PPC64)
4359static void gen_mtmsrd(DisasContext *ctx)
4360{
4361    CHK_SV;
4362
4363#if !defined(CONFIG_USER_ONLY)
4364    if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
4365        gen_io_start();
4366    }
4367    if (ctx->opcode & 0x00010000) {
4368        /* L=1 form only updates EE and RI */
4369        TCGv t0 = tcg_temp_new();
4370        TCGv t1 = tcg_temp_new();
4371        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)],
4372                        (1 << MSR_RI) | (1 << MSR_EE));
4373        tcg_gen_andi_tl(t1, cpu_msr,
4374                        ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
4375        tcg_gen_or_tl(t1, t1, t0);
4376
4377        gen_helper_store_msr(cpu_env, t1);
4378        tcg_temp_free(t0);
4379        tcg_temp_free(t1);
4380
4381    } else {
4382        /*
4383         * XXX: we need to update nip before the store if we enter
4384         *      power saving mode, we will exit the loop directly from
4385         *      ppc_store_msr
4386         */
4387        gen_update_nip(ctx, ctx->base.pc_next);
4388        gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
4389    }
4390    /* Must stop the translation as machine state (may have) changed */
4391    gen_stop_exception(ctx);
4392#endif /* !defined(CONFIG_USER_ONLY) */
4393}
4394#endif /* defined(TARGET_PPC64) */
4395
4396static void gen_mtmsr(DisasContext *ctx)
4397{
4398    CHK_SV;
4399
4400#if !defined(CONFIG_USER_ONLY)
4401    if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
4402        gen_io_start();
4403    }
4404    if (ctx->opcode & 0x00010000) {
4405        /* L=1 form only updates EE and RI */
4406        TCGv t0 = tcg_temp_new();
4407        TCGv t1 = tcg_temp_new();
4408        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)],
4409                        (1 << MSR_RI) | (1 << MSR_EE));
4410        tcg_gen_andi_tl(t1, cpu_msr,
4411                        ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
4412        tcg_gen_or_tl(t1, t1, t0);
4413
4414        gen_helper_store_msr(cpu_env, t1);
4415        tcg_temp_free(t0);
4416        tcg_temp_free(t1);
4417
4418    } else {
4419        TCGv msr = tcg_temp_new();
4420
4421        /*
4422         * XXX: we need to update nip before the store if we enter
4423         *      power saving mode, we will exit the loop directly from
4424         *      ppc_store_msr
4425         */
4426        gen_update_nip(ctx, ctx->base.pc_next);
4427#if defined(TARGET_PPC64)
4428        tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4429#else
4430        tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
4431#endif
4432        gen_helper_store_msr(cpu_env, msr);
4433        tcg_temp_free(msr);
4434    }
4435    /* Must stop the translation as machine state (may have) changed */
4436    gen_stop_exception(ctx);
4437#endif
4438}
4439
4440/* mtspr */
4441static void gen_mtspr(DisasContext *ctx)
4442{
4443    void (*write_cb)(DisasContext *ctx, int sprn, int gprn);
4444    uint32_t sprn = SPR(ctx->opcode);
4445
4446#if defined(CONFIG_USER_ONLY)
4447    write_cb = ctx->spr_cb[sprn].uea_write;
4448#else
4449    if (ctx->pr) {
4450        write_cb = ctx->spr_cb[sprn].uea_write;
4451    } else if (ctx->hv) {
4452        write_cb = ctx->spr_cb[sprn].hea_write;
4453    } else {
4454        write_cb = ctx->spr_cb[sprn].oea_write;
4455    }
4456#endif
4457    if (likely(write_cb != NULL)) {
4458        if (likely(write_cb != SPR_NOACCESS)) {
4459            (*write_cb)(ctx, sprn, rS(ctx->opcode));
4460        } else {
4461            /* Privilege exception */
4462            qemu_log_mask(LOG_GUEST_ERROR, "Trying to write privileged spr "
4463                          "%d (0x%03x) at " TARGET_FMT_lx "\n", sprn, sprn,
4464                          ctx->base.pc_next - 4);
4465            gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4466        }
4467    } else {
4468        /* ISA 2.07 defines these as no-ops */
4469        if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4470            (sprn >= 808 && sprn <= 811)) {
4471            /* This is a nop */
4472            return;
4473        }
4474
4475        /* Not defined */
4476        qemu_log_mask(LOG_GUEST_ERROR,
4477                      "Trying to write invalid spr %d (0x%03x) at "
4478                      TARGET_FMT_lx "\n", sprn, sprn, ctx->base.pc_next - 4);
4479
4480
4481        /*
4482         * The behaviour depends on MSR:PR and SPR# bit 0x10, it can
4483         * generate a priv, a hv emu or a no-op
4484         */
4485        if (sprn & 0x10) {
4486            if (ctx->pr) {
4487                gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4488            }
4489        } else {
4490            if (ctx->pr || sprn == 0) {
4491                gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4492            }
4493        }
4494    }
4495}
4496
4497#if defined(TARGET_PPC64)
4498/* setb */
4499static void gen_setb(DisasContext *ctx)
4500{
4501    TCGv_i32 t0 = tcg_temp_new_i32();
4502    TCGv_i32 t8 = tcg_temp_new_i32();
4503    TCGv_i32 tm1 = tcg_temp_new_i32();
4504    int crf = crfS(ctx->opcode);
4505
4506    tcg_gen_setcondi_i32(TCG_COND_GEU, t0, cpu_crf[crf], 4);
4507    tcg_gen_movi_i32(t8, 8);
4508    tcg_gen_movi_i32(tm1, -1);
4509    tcg_gen_movcond_i32(TCG_COND_GEU, t0, cpu_crf[crf], t8, tm1, t0);
4510    tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4511
4512    tcg_temp_free_i32(t0);
4513    tcg_temp_free_i32(t8);
4514    tcg_temp_free_i32(tm1);
4515}
4516#endif
4517
4518/***                         Cache management                              ***/
4519
4520/* dcbf */
4521static void gen_dcbf(DisasContext *ctx)
4522{
4523    /* XXX: specification says this is treated as a load by the MMU */
4524    TCGv t0;
4525    gen_set_access_type(ctx, ACCESS_CACHE);
4526    t0 = tcg_temp_new();
4527    gen_addr_reg_index(ctx, t0);
4528    gen_qemu_ld8u(ctx, t0, t0);
4529    tcg_temp_free(t0);
4530}
4531
4532/* dcbfep (external PID dcbf) */
4533static void gen_dcbfep(DisasContext *ctx)
4534{
4535    /* XXX: specification says this is treated as a load by the MMU */
4536    TCGv t0;
4537    CHK_SV;
4538    gen_set_access_type(ctx, ACCESS_CACHE);
4539    t0 = tcg_temp_new();
4540    gen_addr_reg_index(ctx, t0);
4541    tcg_gen_qemu_ld_tl(t0, t0, PPC_TLB_EPID_LOAD, DEF_MEMOP(MO_UB));
4542    tcg_temp_free(t0);
4543}
4544
4545/* dcbi (Supervisor only) */
4546static void gen_dcbi(DisasContext *ctx)
4547{
4548#if defined(CONFIG_USER_ONLY)
4549    GEN_PRIV;
4550#else
4551    TCGv EA, val;
4552
4553    CHK_SV;
4554    EA = tcg_temp_new();
4555    gen_set_access_type(ctx, ACCESS_CACHE);
4556    gen_addr_reg_index(ctx, EA);
4557    val = tcg_temp_new();
4558    /* XXX: specification says this should be treated as a store by the MMU */
4559    gen_qemu_ld8u(ctx, val, EA);
4560    gen_qemu_st8(ctx, val, EA);
4561    tcg_temp_free(val);
4562    tcg_temp_free(EA);
4563#endif /* defined(CONFIG_USER_ONLY) */
4564}
4565
4566/* dcdst */
4567static void gen_dcbst(DisasContext *ctx)
4568{
4569    /* XXX: specification say this is treated as a load by the MMU */
4570    TCGv t0;
4571    gen_set_access_type(ctx, ACCESS_CACHE);
4572    t0 = tcg_temp_new();
4573    gen_addr_reg_index(ctx, t0);
4574    gen_qemu_ld8u(ctx, t0, t0);
4575    tcg_temp_free(t0);
4576}
4577
4578/* dcbstep (dcbstep External PID version) */
4579static void gen_dcbstep(DisasContext *ctx)
4580{
4581    /* XXX: specification say this is treated as a load by the MMU */
4582    TCGv t0;
4583    gen_set_access_type(ctx, ACCESS_CACHE);
4584    t0 = tcg_temp_new();
4585    gen_addr_reg_index(ctx, t0);
4586    tcg_gen_qemu_ld_tl(t0, t0, PPC_TLB_EPID_LOAD, DEF_MEMOP(MO_UB));
4587    tcg_temp_free(t0);
4588}
4589
4590/* dcbt */
4591static void gen_dcbt(DisasContext *ctx)
4592{
4593    /*
4594     * interpreted as no-op
4595     * XXX: specification say this is treated as a load by the MMU but
4596     *      does not generate any exception
4597     */
4598}
4599
4600/* dcbtep */
4601static void gen_dcbtep(DisasContext *ctx)
4602{
4603    /*
4604     * interpreted as no-op
4605     * XXX: specification say this is treated as a load by the MMU but
4606     *      does not generate any exception
4607     */
4608}
4609
4610/* dcbtst */
4611static void gen_dcbtst(DisasContext *ctx)
4612{
4613    /*
4614     * interpreted as no-op
4615     * XXX: specification say this is treated as a load by the MMU but
4616     *      does not generate any exception
4617     */
4618}
4619
4620/* dcbtstep */
4621static void gen_dcbtstep(DisasContext *ctx)
4622{
4623    /*
4624     * interpreted as no-op
4625     * XXX: specification say this is treated as a load by the MMU but
4626     *      does not generate any exception
4627     */
4628}
4629
4630/* dcbtls */
4631static void gen_dcbtls(DisasContext *ctx)
4632{
4633    /* Always fails locking the cache */
4634    TCGv t0 = tcg_temp_new();
4635    gen_load_spr(t0, SPR_Exxx_L1CSR0);
4636    tcg_gen_ori_tl(t0, t0, L1CSR0_CUL);
4637    gen_store_spr(SPR_Exxx_L1CSR0, t0);
4638    tcg_temp_free(t0);
4639}
4640
4641/* dcbz */
4642static void gen_dcbz(DisasContext *ctx)
4643{
4644    TCGv tcgv_addr;
4645    TCGv_i32 tcgv_op;
4646
4647    gen_set_access_type(ctx, ACCESS_CACHE);
4648    tcgv_addr = tcg_temp_new();
4649    tcgv_op = tcg_const_i32(ctx->opcode & 0x03FF000);
4650    gen_addr_reg_index(ctx, tcgv_addr);
4651    gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_op);
4652    tcg_temp_free(tcgv_addr);
4653    tcg_temp_free_i32(tcgv_op);
4654}
4655
4656/* dcbzep */
4657static void gen_dcbzep(DisasContext *ctx)
4658{
4659    TCGv tcgv_addr;
4660    TCGv_i32 tcgv_op;
4661
4662    gen_set_access_type(ctx, ACCESS_CACHE);
4663    tcgv_addr = tcg_temp_new();
4664    tcgv_op = tcg_const_i32(ctx->opcode & 0x03FF000);
4665    gen_addr_reg_index(ctx, tcgv_addr);
4666    gen_helper_dcbzep(cpu_env, tcgv_addr, tcgv_op);
4667    tcg_temp_free(tcgv_addr);
4668    tcg_temp_free_i32(tcgv_op);
4669}
4670
4671/* dst / dstt */
4672static void gen_dst(DisasContext *ctx)
4673{
4674    if (rA(ctx->opcode) == 0) {
4675        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
4676    } else {
4677        /* interpreted as no-op */
4678    }
4679}
4680
4681/* dstst /dststt */
4682static void gen_dstst(DisasContext *ctx)
4683{
4684    if (rA(ctx->opcode) == 0) {
4685        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
4686    } else {
4687        /* interpreted as no-op */
4688    }
4689
4690}
4691
4692/* dss / dssall */
4693static void gen_dss(DisasContext *ctx)
4694{
4695    /* interpreted as no-op */
4696}
4697
4698/* icbi */
4699static void gen_icbi(DisasContext *ctx)
4700{
4701    TCGv t0;
4702    gen_set_access_type(ctx, ACCESS_CACHE);
4703    t0 = tcg_temp_new();
4704    gen_addr_reg_index(ctx, t0);
4705    gen_helper_icbi(cpu_env, t0);
4706    tcg_temp_free(t0);
4707}
4708
4709/* icbiep */
4710static void gen_icbiep(DisasContext *ctx)
4711{
4712    TCGv t0;
4713    gen_set_access_type(ctx, ACCESS_CACHE);
4714    t0 = tcg_temp_new();
4715    gen_addr_reg_index(ctx, t0);
4716    gen_helper_icbiep(cpu_env, t0);
4717    tcg_temp_free(t0);
4718}
4719
4720/* Optional: */
4721/* dcba */
4722static void gen_dcba(DisasContext *ctx)
4723{
4724    /*
4725     * interpreted as no-op
4726     * XXX: specification say this is treated as a store by the MMU
4727     *      but does not generate any exception
4728     */
4729}
4730
4731/***                    Segment register manipulation                      ***/
4732/* Supervisor only: */
4733
4734/* mfsr */
4735static void gen_mfsr(DisasContext *ctx)
4736{
4737#if defined(CONFIG_USER_ONLY)
4738    GEN_PRIV;
4739#else
4740    TCGv t0;
4741
4742    CHK_SV;
4743    t0 = tcg_const_tl(SR(ctx->opcode));
4744    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4745    tcg_temp_free(t0);
4746#endif /* defined(CONFIG_USER_ONLY) */
4747}
4748
4749/* mfsrin */
4750static void gen_mfsrin(DisasContext *ctx)
4751{
4752#if defined(CONFIG_USER_ONLY)
4753    GEN_PRIV;
4754#else
4755    TCGv t0;
4756
4757    CHK_SV;
4758    t0 = tcg_temp_new();
4759    tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4760    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4761    tcg_temp_free(t0);
4762#endif /* defined(CONFIG_USER_ONLY) */
4763}
4764
4765/* mtsr */
4766static void gen_mtsr(DisasContext *ctx)
4767{
4768#if defined(CONFIG_USER_ONLY)
4769    GEN_PRIV;
4770#else
4771    TCGv t0;
4772
4773    CHK_SV;
4774    t0 = tcg_const_tl(SR(ctx->opcode));
4775    gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4776    tcg_temp_free(t0);
4777#endif /* defined(CONFIG_USER_ONLY) */
4778}
4779
4780/* mtsrin */
4781static void gen_mtsrin(DisasContext *ctx)
4782{
4783#if defined(CONFIG_USER_ONLY)
4784    GEN_PRIV;
4785#else
4786    TCGv t0;
4787    CHK_SV;
4788
4789    t0 = tcg_temp_new();
4790    tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4791    gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
4792    tcg_temp_free(t0);
4793#endif /* defined(CONFIG_USER_ONLY) */
4794}
4795
4796#if defined(TARGET_PPC64)
4797/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4798
4799/* mfsr */
4800static void gen_mfsr_64b(DisasContext *ctx)
4801{
4802#if defined(CONFIG_USER_ONLY)
4803    GEN_PRIV;
4804#else
4805    TCGv t0;
4806
4807    CHK_SV;
4808    t0 = tcg_const_tl(SR(ctx->opcode));
4809    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4810    tcg_temp_free(t0);
4811#endif /* defined(CONFIG_USER_ONLY) */
4812}
4813
4814/* mfsrin */
4815static void gen_mfsrin_64b(DisasContext *ctx)
4816{
4817#if defined(CONFIG_USER_ONLY)
4818    GEN_PRIV;
4819#else
4820    TCGv t0;
4821
4822    CHK_SV;
4823    t0 = tcg_temp_new();
4824    tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4825    gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4826    tcg_temp_free(t0);
4827#endif /* defined(CONFIG_USER_ONLY) */
4828}
4829
4830/* mtsr */
4831static void gen_mtsr_64b(DisasContext *ctx)
4832{
4833#if defined(CONFIG_USER_ONLY)
4834    GEN_PRIV;
4835#else
4836    TCGv t0;
4837
4838    CHK_SV;
4839    t0 = tcg_const_tl(SR(ctx->opcode));
4840    gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4841    tcg_temp_free(t0);
4842#endif /* defined(CONFIG_USER_ONLY) */
4843}
4844
4845/* mtsrin */
4846static void gen_mtsrin_64b(DisasContext *ctx)
4847{
4848#if defined(CONFIG_USER_ONLY)
4849    GEN_PRIV;
4850#else
4851    TCGv t0;
4852
4853    CHK_SV;
4854    t0 = tcg_temp_new();
4855    tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4856    gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4857    tcg_temp_free(t0);
4858#endif /* defined(CONFIG_USER_ONLY) */
4859}
4860
4861/* slbmte */
4862static void gen_slbmte(DisasContext *ctx)
4863{
4864#if defined(CONFIG_USER_ONLY)
4865    GEN_PRIV;
4866#else
4867    CHK_SV;
4868
4869    gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4870                         cpu_gpr[rS(ctx->opcode)]);
4871#endif /* defined(CONFIG_USER_ONLY) */
4872}
4873
4874static void gen_slbmfee(DisasContext *ctx)
4875{
4876#if defined(CONFIG_USER_ONLY)
4877    GEN_PRIV;
4878#else
4879    CHK_SV;
4880
4881    gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4882                             cpu_gpr[rB(ctx->opcode)]);
4883#endif /* defined(CONFIG_USER_ONLY) */
4884}
4885
4886static void gen_slbmfev(DisasContext *ctx)
4887{
4888#if defined(CONFIG_USER_ONLY)
4889    GEN_PRIV;
4890#else
4891    CHK_SV;
4892
4893    gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4894                             cpu_gpr[rB(ctx->opcode)]);
4895#endif /* defined(CONFIG_USER_ONLY) */
4896}
4897
4898static void gen_slbfee_(DisasContext *ctx)
4899{
4900#if defined(CONFIG_USER_ONLY)
4901    gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4902#else
4903    TCGLabel *l1, *l2;
4904
4905    if (unlikely(ctx->pr)) {
4906        gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4907        return;
4908    }
4909    gen_helper_find_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4910                             cpu_gpr[rB(ctx->opcode)]);
4911    l1 = gen_new_label();
4912    l2 = gen_new_label();
4913    tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4914    tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rS(ctx->opcode)], -1, l1);
4915    tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], CRF_EQ);
4916    tcg_gen_br(l2);
4917    gen_set_label(l1);
4918    tcg_gen_movi_tl(cpu_gpr[rS(ctx->opcode)], 0);
4919    gen_set_label(l2);
4920#endif
4921}
4922#endif /* defined(TARGET_PPC64) */
4923
4924/***                      Lookaside buffer management                      ***/
4925/* Optional & supervisor only: */
4926
4927/* tlbia */
4928static void gen_tlbia(DisasContext *ctx)
4929{
4930#if defined(CONFIG_USER_ONLY)
4931    GEN_PRIV;
4932#else
4933    CHK_HV;
4934
4935    gen_helper_tlbia(cpu_env);
4936#endif  /* defined(CONFIG_USER_ONLY) */
4937}
4938
4939/* tlbiel */
4940static void gen_tlbiel(DisasContext *ctx)
4941{
4942#if defined(CONFIG_USER_ONLY)
4943    GEN_PRIV;
4944#else
4945    CHK_SV;
4946
4947    gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4948#endif /* defined(CONFIG_USER_ONLY) */
4949}
4950
4951/* tlbie */
4952static void gen_tlbie(DisasContext *ctx)
4953{
4954#if defined(CONFIG_USER_ONLY)
4955    GEN_PRIV;
4956#else
4957    TCGv_i32 t1;
4958
4959    if (ctx->gtse) {
4960        CHK_SV; /* If gtse is set then tlbie is supervisor privileged */
4961    } else {
4962        CHK_HV; /* Else hypervisor privileged */
4963    }
4964
4965    if (NARROW_MODE(ctx)) {
4966        TCGv t0 = tcg_temp_new();
4967        tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4968        gen_helper_tlbie(cpu_env, t0);
4969        tcg_temp_free(t0);
4970    } else {
4971        gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4972    }
4973    t1 = tcg_temp_new_i32();
4974    tcg_gen_ld_i32(t1, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
4975    tcg_gen_ori_i32(t1, t1, TLB_NEED_GLOBAL_FLUSH);
4976    tcg_gen_st_i32(t1, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
4977    tcg_temp_free_i32(t1);
4978#endif /* defined(CONFIG_USER_ONLY) */
4979}
4980
4981/* tlbsync */
4982static void gen_tlbsync(DisasContext *ctx)
4983{
4984#if defined(CONFIG_USER_ONLY)
4985    GEN_PRIV;
4986#else
4987
4988    if (ctx->gtse) {
4989        CHK_SV; /* If gtse is set then tlbsync is supervisor privileged */
4990    } else {
4991        CHK_HV; /* Else hypervisor privileged */
4992    }
4993
4994    /* BookS does both ptesync and tlbsync make tlbsync a nop for server */
4995    if (ctx->insns_flags & PPC_BOOKE) {
4996        gen_check_tlb_flush(ctx, true);
4997    }
4998#endif /* defined(CONFIG_USER_ONLY) */
4999}
5000
5001#if defined(TARGET_PPC64)
5002/* slbia */
5003static void gen_slbia(DisasContext *ctx)
5004{
5005#if defined(CONFIG_USER_ONLY)
5006    GEN_PRIV;
5007#else
5008    CHK_SV;
5009
5010    gen_helper_slbia(cpu_env);
5011#endif /* defined(CONFIG_USER_ONLY) */
5012}
5013
5014/* slbie */
5015static void gen_slbie(DisasContext *ctx)
5016{
5017#if defined(CONFIG_USER_ONLY)
5018    GEN_PRIV;
5019#else
5020    CHK_SV;
5021
5022    gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5023#endif /* defined(CONFIG_USER_ONLY) */
5024}
5025
5026/* slbieg */
5027static void gen_slbieg(DisasContext *ctx)
5028{
5029#if defined(CONFIG_USER_ONLY)
5030    GEN_PRIV;
5031#else
5032    CHK_SV;
5033
5034    gen_helper_slbieg(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5035#endif /* defined(CONFIG_USER_ONLY) */
5036}
5037
5038/* slbsync */
5039static void gen_slbsync(DisasContext *ctx)
5040{
5041#if defined(CONFIG_USER_ONLY)
5042    GEN_PRIV;
5043#else
5044    CHK_SV;
5045    gen_check_tlb_flush(ctx, true);
5046#endif /* defined(CONFIG_USER_ONLY) */
5047}
5048
5049#endif  /* defined(TARGET_PPC64) */
5050
5051/***                              External control                         ***/
5052/* Optional: */
5053
5054/* eciwx */
5055static void gen_eciwx(DisasContext *ctx)
5056{
5057    TCGv t0;
5058    /* Should check EAR[E] ! */
5059    gen_set_access_type(ctx, ACCESS_EXT);
5060    t0 = tcg_temp_new();
5061    gen_addr_reg_index(ctx, t0);
5062    tcg_gen_qemu_ld_tl(cpu_gpr[rD(ctx->opcode)], t0, ctx->mem_idx,
5063                       DEF_MEMOP(MO_UL | MO_ALIGN));
5064    tcg_temp_free(t0);
5065}
5066
5067/* ecowx */
5068static void gen_ecowx(DisasContext *ctx)
5069{
5070    TCGv t0;
5071    /* Should check EAR[E] ! */
5072    gen_set_access_type(ctx, ACCESS_EXT);
5073    t0 = tcg_temp_new();
5074    gen_addr_reg_index(ctx, t0);
5075    tcg_gen_qemu_st_tl(cpu_gpr[rD(ctx->opcode)], t0, ctx->mem_idx,
5076                       DEF_MEMOP(MO_UL | MO_ALIGN));
5077    tcg_temp_free(t0);
5078}
5079
5080/* PowerPC 601 specific instructions */
5081
5082/* abs - abs. */
5083static void gen_abs(DisasContext *ctx)
5084{
5085    TCGv d = cpu_gpr[rD(ctx->opcode)];
5086    TCGv a = cpu_gpr[rA(ctx->opcode)];
5087
5088    tcg_gen_abs_tl(d, a);
5089    if (unlikely(Rc(ctx->opcode) != 0)) {
5090        gen_set_Rc0(ctx, d);
5091    }
5092}
5093
5094/* abso - abso. */
5095static void gen_abso(DisasContext *ctx)
5096{
5097    TCGv d = cpu_gpr[rD(ctx->opcode)];
5098    TCGv a = cpu_gpr[rA(ctx->opcode)];
5099
5100    tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_ov, a, 0x80000000);
5101    tcg_gen_abs_tl(d, a);
5102    tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
5103    if (unlikely(Rc(ctx->opcode) != 0)) {
5104        gen_set_Rc0(ctx, d);
5105    }
5106}
5107
5108/* clcs */
5109static void gen_clcs(DisasContext *ctx)
5110{
5111    TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
5112    gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5113    tcg_temp_free_i32(t0);
5114    /* Rc=1 sets CR0 to an undefined state */
5115}
5116
5117/* div - div. */
5118static void gen_div(DisasContext *ctx)
5119{
5120    gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5121                   cpu_gpr[rB(ctx->opcode)]);
5122    if (unlikely(Rc(ctx->opcode) != 0)) {
5123        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5124    }
5125}
5126
5127/* divo - divo. */
5128static void gen_divo(DisasContext *ctx)
5129{
5130    gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5131                    cpu_gpr[rB(ctx->opcode)]);
5132    if (unlikely(Rc(ctx->opcode) != 0)) {
5133        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5134    }
5135}
5136
5137/* divs - divs. */
5138static void gen_divs(DisasContext *ctx)
5139{
5140    gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5141                    cpu_gpr[rB(ctx->opcode)]);
5142    if (unlikely(Rc(ctx->opcode) != 0)) {
5143        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5144    }
5145}
5146
5147/* divso - divso. */
5148static void gen_divso(DisasContext *ctx)
5149{
5150    gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
5151                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5152    if (unlikely(Rc(ctx->opcode) != 0)) {
5153        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5154    }
5155}
5156
5157/* doz - doz. */
5158static void gen_doz(DisasContext *ctx)
5159{
5160    TCGLabel *l1 = gen_new_label();
5161    TCGLabel *l2 = gen_new_label();
5162    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)],
5163                      cpu_gpr[rA(ctx->opcode)], l1);
5164    tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
5165                   cpu_gpr[rA(ctx->opcode)]);
5166    tcg_gen_br(l2);
5167    gen_set_label(l1);
5168    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5169    gen_set_label(l2);
5170    if (unlikely(Rc(ctx->opcode) != 0)) {
5171        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5172    }
5173}
5174
5175/* dozo - dozo. */
5176static void gen_dozo(DisasContext *ctx)
5177{
5178    TCGLabel *l1 = gen_new_label();
5179    TCGLabel *l2 = gen_new_label();
5180    TCGv t0 = tcg_temp_new();
5181    TCGv t1 = tcg_temp_new();
5182    TCGv t2 = tcg_temp_new();
5183    /* Start with XER OV disabled, the most likely case */
5184    tcg_gen_movi_tl(cpu_ov, 0);
5185    tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)],
5186                      cpu_gpr[rA(ctx->opcode)], l1);
5187    tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5188    tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5189    tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
5190    tcg_gen_andc_tl(t1, t1, t2);
5191    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
5192    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5193    tcg_gen_movi_tl(cpu_ov, 1);
5194    tcg_gen_movi_tl(cpu_so, 1);
5195    tcg_gen_br(l2);
5196    gen_set_label(l1);
5197    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5198    gen_set_label(l2);
5199    tcg_temp_free(t0);
5200    tcg_temp_free(t1);
5201    tcg_temp_free(t2);
5202    if (unlikely(Rc(ctx->opcode) != 0)) {
5203        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5204    }
5205}
5206
5207/* dozi */
5208static void gen_dozi(DisasContext *ctx)
5209{
5210    target_long simm = SIMM(ctx->opcode);
5211    TCGLabel *l1 = gen_new_label();
5212    TCGLabel *l2 = gen_new_label();
5213    tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
5214    tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
5215    tcg_gen_br(l2);
5216    gen_set_label(l1);
5217    tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5218    gen_set_label(l2);
5219    if (unlikely(Rc(ctx->opcode) != 0)) {
5220        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5221    }
5222}
5223
5224/* lscbx - lscbx. */
5225static void gen_lscbx(DisasContext *ctx)
5226{
5227    TCGv t0 = tcg_temp_new();
5228    TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
5229    TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
5230    TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
5231
5232    gen_addr_reg_index(ctx, t0);
5233    gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
5234    tcg_temp_free_i32(t1);
5235    tcg_temp_free_i32(t2);
5236    tcg_temp_free_i32(t3);
5237    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5238    tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
5239    if (unlikely(Rc(ctx->opcode) != 0)) {
5240        gen_set_Rc0(ctx, t0);
5241    }
5242    tcg_temp_free(t0);
5243}
5244
5245/* maskg - maskg. */
5246static void gen_maskg(DisasContext *ctx)
5247{
5248    TCGLabel *l1 = gen_new_label();
5249    TCGv t0 = tcg_temp_new();
5250    TCGv t1 = tcg_temp_new();
5251    TCGv t2 = tcg_temp_new();
5252    TCGv t3 = tcg_temp_new();
5253    tcg_gen_movi_tl(t3, 0xFFFFFFFF);
5254    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5255    tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
5256    tcg_gen_addi_tl(t2, t0, 1);
5257    tcg_gen_shr_tl(t2, t3, t2);
5258    tcg_gen_shr_tl(t3, t3, t1);
5259    tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
5260    tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
5261    tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5262    gen_set_label(l1);
5263    tcg_temp_free(t0);
5264    tcg_temp_free(t1);
5265    tcg_temp_free(t2);
5266    tcg_temp_free(t3);
5267    if (unlikely(Rc(ctx->opcode) != 0)) {
5268        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5269    }
5270}
5271
5272/* maskir - maskir. */
5273static void gen_maskir(DisasContext *ctx)
5274{
5275    TCGv t0 = tcg_temp_new();
5276    TCGv t1 = tcg_temp_new();
5277    tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5278    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5279    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5280    tcg_temp_free(t0);
5281    tcg_temp_free(t1);
5282    if (unlikely(Rc(ctx->opcode) != 0)) {
5283        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5284    }
5285}
5286
5287/* mul - mul. */
5288static void gen_mul(DisasContext *ctx)
5289{
5290    TCGv_i64 t0 = tcg_temp_new_i64();
5291    TCGv_i64 t1 = tcg_temp_new_i64();
5292    TCGv t2 = tcg_temp_new();
5293    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5294    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5295    tcg_gen_mul_i64(t0, t0, t1);
5296    tcg_gen_trunc_i64_tl(t2, t0);
5297    gen_store_spr(SPR_MQ, t2);
5298    tcg_gen_shri_i64(t1, t0, 32);
5299    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5300    tcg_temp_free_i64(t0);
5301    tcg_temp_free_i64(t1);
5302    tcg_temp_free(t2);
5303    if (unlikely(Rc(ctx->opcode) != 0)) {
5304        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5305    }
5306}
5307
5308/* mulo - mulo. */
5309static void gen_mulo(DisasContext *ctx)
5310{
5311    TCGLabel *l1 = gen_new_label();
5312    TCGv_i64 t0 = tcg_temp_new_i64();
5313    TCGv_i64 t1 = tcg_temp_new_i64();
5314    TCGv t2 = tcg_temp_new();
5315    /* Start with XER OV disabled, the most likely case */
5316    tcg_gen_movi_tl(cpu_ov, 0);
5317    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5318    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5319    tcg_gen_mul_i64(t0, t0, t1);
5320    tcg_gen_trunc_i64_tl(t2, t0);
5321    gen_store_spr(SPR_MQ, t2);
5322    tcg_gen_shri_i64(t1, t0, 32);
5323    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5324    tcg_gen_ext32s_i64(t1, t0);
5325    tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
5326    tcg_gen_movi_tl(cpu_ov, 1);
5327    tcg_gen_movi_tl(cpu_so, 1);
5328    gen_set_label(l1);
5329    tcg_temp_free_i64(t0);
5330    tcg_temp_free_i64(t1);
5331    tcg_temp_free(t2);
5332    if (unlikely(Rc(ctx->opcode) != 0)) {
5333        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5334    }
5335}
5336
5337/* nabs - nabs. */
5338static void gen_nabs(DisasContext *ctx)
5339{
5340    TCGv d = cpu_gpr[rD(ctx->opcode)];
5341    TCGv a = cpu_gpr[rA(ctx->opcode)];
5342
5343    tcg_gen_abs_tl(d, a);
5344    tcg_gen_neg_tl(d, d);
5345    if (unlikely(Rc(ctx->opcode) != 0)) {
5346        gen_set_Rc0(ctx, d);
5347    }
5348}
5349
5350/* nabso - nabso. */
5351static void gen_nabso(DisasContext *ctx)
5352{
5353    TCGv d = cpu_gpr[rD(ctx->opcode)];
5354    TCGv a = cpu_gpr[rA(ctx->opcode)];
5355
5356    tcg_gen_abs_tl(d, a);
5357    tcg_gen_neg_tl(d, d);
5358    /* nabs never overflows */
5359    tcg_gen_movi_tl(cpu_ov, 0);
5360    if (unlikely(Rc(ctx->opcode) != 0)) {
5361        gen_set_Rc0(ctx, d);
5362    }
5363}
5364
5365/* rlmi - rlmi. */
5366static void gen_rlmi(DisasContext *ctx)
5367{
5368    uint32_t mb = MB(ctx->opcode);
5369    uint32_t me = ME(ctx->opcode);
5370    TCGv t0 = tcg_temp_new();
5371    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5372    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5373    tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5374    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
5375                    ~MASK(mb, me));
5376    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5377    tcg_temp_free(t0);
5378    if (unlikely(Rc(ctx->opcode) != 0)) {
5379        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5380    }
5381}
5382
5383/* rrib - rrib. */
5384static void gen_rrib(DisasContext *ctx)
5385{
5386    TCGv t0 = tcg_temp_new();
5387    TCGv t1 = tcg_temp_new();
5388    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5389    tcg_gen_movi_tl(t1, 0x80000000);
5390    tcg_gen_shr_tl(t1, t1, t0);
5391    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5392    tcg_gen_and_tl(t0, t0, t1);
5393    tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5394    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5395    tcg_temp_free(t0);
5396    tcg_temp_free(t1);
5397    if (unlikely(Rc(ctx->opcode) != 0)) {
5398        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5399    }
5400}
5401
5402/* sle - sle. */
5403static void gen_sle(DisasContext *ctx)
5404{
5405    TCGv t0 = tcg_temp_new();
5406    TCGv t1 = tcg_temp_new();
5407    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5408    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5409    tcg_gen_subfi_tl(t1, 32, t1);
5410    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5411    tcg_gen_or_tl(t1, t0, t1);
5412    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5413    gen_store_spr(SPR_MQ, t1);
5414    tcg_temp_free(t0);
5415    tcg_temp_free(t1);
5416    if (unlikely(Rc(ctx->opcode) != 0)) {
5417        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5418    }
5419}
5420
5421/* sleq - sleq. */
5422static void gen_sleq(DisasContext *ctx)
5423{
5424    TCGv t0 = tcg_temp_new();
5425    TCGv t1 = tcg_temp_new();
5426    TCGv t2 = tcg_temp_new();
5427    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5428    tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5429    tcg_gen_shl_tl(t2, t2, t0);
5430    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5431    gen_load_spr(t1, SPR_MQ);
5432    gen_store_spr(SPR_MQ, t0);
5433    tcg_gen_and_tl(t0, t0, t2);
5434    tcg_gen_andc_tl(t1, t1, t2);
5435    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5436    tcg_temp_free(t0);
5437    tcg_temp_free(t1);
5438    tcg_temp_free(t2);
5439    if (unlikely(Rc(ctx->opcode) != 0)) {
5440        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5441    }
5442}
5443
5444/* sliq - sliq. */
5445static void gen_sliq(DisasContext *ctx)
5446{
5447    int sh = SH(ctx->opcode);
5448    TCGv t0 = tcg_temp_new();
5449    TCGv t1 = tcg_temp_new();
5450    tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5451    tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5452    tcg_gen_or_tl(t1, t0, t1);
5453    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5454    gen_store_spr(SPR_MQ, t1);
5455    tcg_temp_free(t0);
5456    tcg_temp_free(t1);
5457    if (unlikely(Rc(ctx->opcode) != 0)) {
5458        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5459    }
5460}
5461
5462/* slliq - slliq. */
5463static void gen_slliq(DisasContext *ctx)
5464{
5465    int sh = SH(ctx->opcode);
5466    TCGv t0 = tcg_temp_new();
5467    TCGv t1 = tcg_temp_new();
5468    tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5469    gen_load_spr(t1, SPR_MQ);
5470    gen_store_spr(SPR_MQ, t0);
5471    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
5472    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5473    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5474    tcg_temp_free(t0);
5475    tcg_temp_free(t1);
5476    if (unlikely(Rc(ctx->opcode) != 0)) {
5477        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5478    }
5479}
5480
5481/* sllq - sllq. */
5482static void gen_sllq(DisasContext *ctx)
5483{
5484    TCGLabel *l1 = gen_new_label();
5485    TCGLabel *l2 = gen_new_label();
5486    TCGv t0 = tcg_temp_local_new();
5487    TCGv t1 = tcg_temp_local_new();
5488    TCGv t2 = tcg_temp_local_new();
5489    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5490    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5491    tcg_gen_shl_tl(t1, t1, t2);
5492    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5493    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5494    gen_load_spr(t0, SPR_MQ);
5495    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5496    tcg_gen_br(l2);
5497    gen_set_label(l1);
5498    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5499    gen_load_spr(t2, SPR_MQ);
5500    tcg_gen_andc_tl(t1, t2, t1);
5501    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5502    gen_set_label(l2);
5503    tcg_temp_free(t0);
5504    tcg_temp_free(t1);
5505    tcg_temp_free(t2);
5506    if (unlikely(Rc(ctx->opcode) != 0)) {
5507        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5508    }
5509}
5510
5511/* slq - slq. */
5512static void gen_slq(DisasContext *ctx)
5513{
5514    TCGLabel *l1 = gen_new_label();
5515    TCGv t0 = tcg_temp_new();
5516    TCGv t1 = tcg_temp_new();
5517    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5518    tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5519    tcg_gen_subfi_tl(t1, 32, t1);
5520    tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5521    tcg_gen_or_tl(t1, t0, t1);
5522    gen_store_spr(SPR_MQ, t1);
5523    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5524    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5525    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5526    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5527    gen_set_label(l1);
5528    tcg_temp_free(t0);
5529    tcg_temp_free(t1);
5530    if (unlikely(Rc(ctx->opcode) != 0)) {
5531        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5532    }
5533}
5534
5535/* sraiq - sraiq. */
5536static void gen_sraiq(DisasContext *ctx)
5537{
5538    int sh = SH(ctx->opcode);
5539    TCGLabel *l1 = gen_new_label();
5540    TCGv t0 = tcg_temp_new();
5541    TCGv t1 = tcg_temp_new();
5542    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5543    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5544    tcg_gen_or_tl(t0, t0, t1);
5545    gen_store_spr(SPR_MQ, t0);
5546    tcg_gen_movi_tl(cpu_ca, 0);
5547    tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5548    tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
5549    tcg_gen_movi_tl(cpu_ca, 1);
5550    gen_set_label(l1);
5551    tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5552    tcg_temp_free(t0);
5553    tcg_temp_free(t1);
5554    if (unlikely(Rc(ctx->opcode) != 0)) {
5555        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5556    }
5557}
5558
5559/* sraq - sraq. */
5560static void gen_sraq(DisasContext *ctx)
5561{
5562    TCGLabel *l1 = gen_new_label();
5563    TCGLabel *l2 = gen_new_label();
5564    TCGv t0 = tcg_temp_new();
5565    TCGv t1 = tcg_temp_local_new();
5566    TCGv t2 = tcg_temp_local_new();
5567    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5568    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5569    tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5570    tcg_gen_subfi_tl(t2, 32, t2);
5571    tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5572    tcg_gen_or_tl(t0, t0, t2);
5573    gen_store_spr(SPR_MQ, t0);
5574    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5575    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5576    tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5577    tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5578    gen_set_label(l1);
5579    tcg_temp_free(t0);
5580    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
5581    tcg_gen_movi_tl(cpu_ca, 0);
5582    tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5583    tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
5584    tcg_gen_movi_tl(cpu_ca, 1);
5585    gen_set_label(l2);
5586    tcg_temp_free(t1);
5587    tcg_temp_free(t2);
5588    if (unlikely(Rc(ctx->opcode) != 0)) {
5589        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5590    }
5591}
5592
5593/* sre - sre. */
5594static void gen_sre(DisasContext *ctx)
5595{
5596    TCGv t0 = tcg_temp_new();
5597    TCGv t1 = tcg_temp_new();
5598    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5599    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5600    tcg_gen_subfi_tl(t1, 32, t1);
5601    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5602    tcg_gen_or_tl(t1, t0, t1);
5603    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5604    gen_store_spr(SPR_MQ, t1);
5605    tcg_temp_free(t0);
5606    tcg_temp_free(t1);
5607    if (unlikely(Rc(ctx->opcode) != 0)) {
5608        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5609    }
5610}
5611
5612/* srea - srea. */
5613static void gen_srea(DisasContext *ctx)
5614{
5615    TCGv t0 = tcg_temp_new();
5616    TCGv t1 = tcg_temp_new();
5617    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5618    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5619    gen_store_spr(SPR_MQ, t0);
5620    tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5621    tcg_temp_free(t0);
5622    tcg_temp_free(t1);
5623    if (unlikely(Rc(ctx->opcode) != 0)) {
5624        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5625    }
5626}
5627
5628/* sreq */
5629static void gen_sreq(DisasContext *ctx)
5630{
5631    TCGv t0 = tcg_temp_new();
5632    TCGv t1 = tcg_temp_new();
5633    TCGv t2 = tcg_temp_new();
5634    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5635    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5636    tcg_gen_shr_tl(t1, t1, t0);
5637    tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5638    gen_load_spr(t2, SPR_MQ);
5639    gen_store_spr(SPR_MQ, t0);
5640    tcg_gen_and_tl(t0, t0, t1);
5641    tcg_gen_andc_tl(t2, t2, t1);
5642    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5643    tcg_temp_free(t0);
5644    tcg_temp_free(t1);
5645    tcg_temp_free(t2);
5646    if (unlikely(Rc(ctx->opcode) != 0)) {
5647        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5648    }
5649}
5650
5651/* sriq */
5652static void gen_sriq(DisasContext *ctx)
5653{
5654    int sh = SH(ctx->opcode);
5655    TCGv t0 = tcg_temp_new();
5656    TCGv t1 = tcg_temp_new();
5657    tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5658    tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5659    tcg_gen_or_tl(t1, t0, t1);
5660    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5661    gen_store_spr(SPR_MQ, t1);
5662    tcg_temp_free(t0);
5663    tcg_temp_free(t1);
5664    if (unlikely(Rc(ctx->opcode) != 0)) {
5665        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5666    }
5667}
5668
5669/* srliq */
5670static void gen_srliq(DisasContext *ctx)
5671{
5672    int sh = SH(ctx->opcode);
5673    TCGv t0 = tcg_temp_new();
5674    TCGv t1 = tcg_temp_new();
5675    tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5676    gen_load_spr(t1, SPR_MQ);
5677    gen_store_spr(SPR_MQ, t0);
5678    tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5679    tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5680    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5681    tcg_temp_free(t0);
5682    tcg_temp_free(t1);
5683    if (unlikely(Rc(ctx->opcode) != 0)) {
5684        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5685    }
5686}
5687
5688/* srlq */
5689static void gen_srlq(DisasContext *ctx)
5690{
5691    TCGLabel *l1 = gen_new_label();
5692    TCGLabel *l2 = gen_new_label();
5693    TCGv t0 = tcg_temp_local_new();
5694    TCGv t1 = tcg_temp_local_new();
5695    TCGv t2 = tcg_temp_local_new();
5696    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5697    tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5698    tcg_gen_shr_tl(t2, t1, t2);
5699    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5700    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5701    gen_load_spr(t0, SPR_MQ);
5702    tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5703    tcg_gen_br(l2);
5704    gen_set_label(l1);
5705    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5706    tcg_gen_and_tl(t0, t0, t2);
5707    gen_load_spr(t1, SPR_MQ);
5708    tcg_gen_andc_tl(t1, t1, t2);
5709    tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5710    gen_set_label(l2);
5711    tcg_temp_free(t0);
5712    tcg_temp_free(t1);
5713    tcg_temp_free(t2);
5714    if (unlikely(Rc(ctx->opcode) != 0)) {
5715        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5716    }
5717}
5718
5719/* srq */
5720static void gen_srq(DisasContext *ctx)
5721{
5722    TCGLabel *l1 = gen_new_label();
5723    TCGv t0 = tcg_temp_new();
5724    TCGv t1 = tcg_temp_new();
5725    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5726    tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5727    tcg_gen_subfi_tl(t1, 32, t1);
5728    tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5729    tcg_gen_or_tl(t1, t0, t1);
5730    gen_store_spr(SPR_MQ, t1);
5731    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5732    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5733    tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5734    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5735    gen_set_label(l1);
5736    tcg_temp_free(t0);
5737    tcg_temp_free(t1);
5738    if (unlikely(Rc(ctx->opcode) != 0)) {
5739        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5740    }
5741}
5742
5743/* PowerPC 602 specific instructions */
5744
5745/* dsa  */
5746static void gen_dsa(DisasContext *ctx)
5747{
5748    /* XXX: TODO */
5749    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5750}
5751
5752/* esa */
5753static void gen_esa(DisasContext *ctx)
5754{
5755    /* XXX: TODO */
5756    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5757}
5758
5759/* mfrom */
5760static void gen_mfrom(DisasContext *ctx)
5761{
5762#if defined(CONFIG_USER_ONLY)
5763    GEN_PRIV;
5764#else
5765    CHK_SV;
5766    gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5767#endif /* defined(CONFIG_USER_ONLY) */
5768}
5769
5770/* 602 - 603 - G2 TLB management */
5771
5772/* tlbld */
5773static void gen_tlbld_6xx(DisasContext *ctx)
5774{
5775#if defined(CONFIG_USER_ONLY)
5776    GEN_PRIV;
5777#else
5778    CHK_SV;
5779    gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5780#endif /* defined(CONFIG_USER_ONLY) */
5781}
5782
5783/* tlbli */
5784static void gen_tlbli_6xx(DisasContext *ctx)
5785{
5786#if defined(CONFIG_USER_ONLY)
5787    GEN_PRIV;
5788#else
5789    CHK_SV;
5790    gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5791#endif /* defined(CONFIG_USER_ONLY) */
5792}
5793
5794/* 74xx TLB management */
5795
5796/* tlbld */
5797static void gen_tlbld_74xx(DisasContext *ctx)
5798{
5799#if defined(CONFIG_USER_ONLY)
5800    GEN_PRIV;
5801#else
5802    CHK_SV;
5803    gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5804#endif /* defined(CONFIG_USER_ONLY) */
5805}
5806
5807/* tlbli */
5808static void gen_tlbli_74xx(DisasContext *ctx)
5809{
5810#if defined(CONFIG_USER_ONLY)
5811    GEN_PRIV;
5812#else
5813    CHK_SV;
5814    gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5815#endif /* defined(CONFIG_USER_ONLY) */
5816}
5817
5818/* POWER instructions not in PowerPC 601 */
5819
5820/* clf */
5821static void gen_clf(DisasContext *ctx)
5822{
5823    /* Cache line flush: implemented as no-op */
5824}
5825
5826/* cli */
5827static void gen_cli(DisasContext *ctx)
5828{
5829#if defined(CONFIG_USER_ONLY)
5830    GEN_PRIV;
5831#else
5832    /* Cache line invalidate: privileged and treated as no-op */
5833    CHK_SV;
5834#endif /* defined(CONFIG_USER_ONLY) */
5835}
5836
5837/* dclst */
5838static void gen_dclst(DisasContext *ctx)
5839{
5840    /* Data cache line store: treated as no-op */
5841}
5842
5843static void gen_mfsri(DisasContext *ctx)
5844{
5845#if defined(CONFIG_USER_ONLY)
5846    GEN_PRIV;
5847#else
5848    int ra = rA(ctx->opcode);
5849    int rd = rD(ctx->opcode);
5850    TCGv t0;
5851
5852    CHK_SV;
5853    t0 = tcg_temp_new();
5854    gen_addr_reg_index(ctx, t0);
5855    tcg_gen_extract_tl(t0, t0, 28, 4);
5856    gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
5857    tcg_temp_free(t0);
5858    if (ra != 0 && ra != rd) {
5859        tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5860    }
5861#endif /* defined(CONFIG_USER_ONLY) */
5862}
5863
5864static void gen_rac(DisasContext *ctx)
5865{
5866#if defined(CONFIG_USER_ONLY)
5867    GEN_PRIV;
5868#else
5869    TCGv t0;
5870
5871    CHK_SV;
5872    t0 = tcg_temp_new();
5873    gen_addr_reg_index(ctx, t0);
5874    gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5875    tcg_temp_free(t0);
5876#endif /* defined(CONFIG_USER_ONLY) */
5877}
5878
5879static void gen_rfsvc(DisasContext *ctx)
5880{
5881#if defined(CONFIG_USER_ONLY)
5882    GEN_PRIV;
5883#else
5884    CHK_SV;
5885
5886    gen_helper_rfsvc(cpu_env);
5887    gen_sync_exception(ctx);
5888#endif /* defined(CONFIG_USER_ONLY) */
5889}
5890
5891/* svc is not implemented for now */
5892
5893/* BookE specific instructions */
5894
5895/* XXX: not implemented on 440 ? */
5896static void gen_mfapidi(DisasContext *ctx)
5897{
5898    /* XXX: TODO */
5899    gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5900}
5901
5902/* XXX: not implemented on 440 ? */
5903static void gen_tlbiva(DisasContext *ctx)
5904{
5905#if defined(CONFIG_USER_ONLY)
5906    GEN_PRIV;
5907#else
5908    TCGv t0;
5909
5910    CHK_SV;
5911    t0 = tcg_temp_new();
5912    gen_addr_reg_index(ctx, t0);
5913    gen_helper_tlbiva(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5914    tcg_temp_free(t0);
5915#endif /* defined(CONFIG_USER_ONLY) */
5916}
5917
5918/* All 405 MAC instructions are translated here */
5919static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5920                                        int ra, int rb, int rt, int Rc)
5921{
5922    TCGv t0, t1;
5923
5924    t0 = tcg_temp_local_new();
5925    t1 = tcg_temp_local_new();
5926
5927    switch (opc3 & 0x0D) {
5928    case 0x05:
5929        /* macchw    - macchw.    - macchwo   - macchwo.   */
5930        /* macchws   - macchws.   - macchwso  - macchwso.  */
5931        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5932        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5933        /* mulchw - mulchw. */
5934        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5935        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5936        tcg_gen_ext16s_tl(t1, t1);
5937        break;
5938    case 0x04:
5939        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5940        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5941        /* mulchwu - mulchwu. */
5942        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5943        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5944        tcg_gen_ext16u_tl(t1, t1);
5945        break;
5946    case 0x01:
5947        /* machhw    - machhw.    - machhwo   - machhwo.   */
5948        /* machhws   - machhws.   - machhwso  - machhwso.  */
5949        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5950        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5951        /* mulhhw - mulhhw. */
5952        tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5953        tcg_gen_ext16s_tl(t0, t0);
5954        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5955        tcg_gen_ext16s_tl(t1, t1);
5956        break;
5957    case 0x00:
5958        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5959        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5960        /* mulhhwu - mulhhwu. */
5961        tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5962        tcg_gen_ext16u_tl(t0, t0);
5963        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5964        tcg_gen_ext16u_tl(t1, t1);
5965        break;
5966    case 0x0D:
5967        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5968        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5969        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5970        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5971        /* mullhw - mullhw. */
5972        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5973        tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5974        break;
5975    case 0x0C:
5976        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5977        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5978        /* mullhwu - mullhwu. */
5979        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5980        tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5981        break;
5982    }
5983    if (opc2 & 0x04) {
5984        /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5985        tcg_gen_mul_tl(t1, t0, t1);
5986        if (opc2 & 0x02) {
5987            /* nmultiply-and-accumulate (0x0E) */
5988            tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5989        } else {
5990            /* multiply-and-accumulate (0x0C) */
5991            tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5992        }
5993
5994        if (opc3 & 0x12) {
5995            /* Check overflow and/or saturate */
5996            TCGLabel *l1 = gen_new_label();
5997
5998            if (opc3 & 0x10) {
5999                /* Start with XER OV disabled, the most likely case */
6000                tcg_gen_movi_tl(cpu_ov, 0);
6001            }
6002            if (opc3 & 0x01) {
6003                /* Signed */
6004                tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
6005                tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
6006                tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
6007                tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
6008                if (opc3 & 0x02) {
6009                    /* Saturate */
6010                    tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
6011                    tcg_gen_xori_tl(t0, t0, 0x7fffffff);
6012                }
6013            } else {
6014                /* Unsigned */
6015                tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
6016                if (opc3 & 0x02) {
6017                    /* Saturate */
6018                    tcg_gen_movi_tl(t0, UINT32_MAX);
6019                }
6020            }
6021            if (opc3 & 0x10) {
6022                /* Check overflow */
6023                tcg_gen_movi_tl(cpu_ov, 1);
6024                tcg_gen_movi_tl(cpu_so, 1);
6025            }
6026            gen_set_label(l1);
6027            tcg_gen_mov_tl(cpu_gpr[rt], t0);
6028        }
6029    } else {
6030        tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
6031    }
6032    tcg_temp_free(t0);
6033    tcg_temp_free(t1);
6034    if (unlikely(Rc) != 0) {
6035        /* Update Rc0 */
6036        gen_set_Rc0(ctx, cpu_gpr[rt]);
6037    }
6038}
6039
6040#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
6041static void glue(gen_, name)(DisasContext *ctx)                               \
6042{                                                                             \
6043    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
6044                         rD(ctx->opcode), Rc(ctx->opcode));                   \
6045}
6046
6047/* macchw    - macchw.    */
6048GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
6049/* macchwo   - macchwo.   */
6050GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
6051/* macchws   - macchws.   */
6052GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
6053/* macchwso  - macchwso.  */
6054GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
6055/* macchwsu  - macchwsu.  */
6056GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
6057/* macchwsuo - macchwsuo. */
6058GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
6059/* macchwu   - macchwu.   */
6060GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
6061/* macchwuo  - macchwuo.  */
6062GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
6063/* machhw    - machhw.    */
6064GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
6065/* machhwo   - machhwo.   */
6066GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
6067/* machhws   - machhws.   */
6068GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
6069/* machhwso  - machhwso.  */
6070GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
6071/* machhwsu  - machhwsu.  */
6072GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
6073/* machhwsuo - machhwsuo. */
6074GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
6075/* machhwu   - machhwu.   */
6076GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
6077/* machhwuo  - machhwuo.  */
6078GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
6079/* maclhw    - maclhw.    */
6080GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
6081/* maclhwo   - maclhwo.   */
6082GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
6083/* maclhws   - maclhws.   */
6084GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
6085/* maclhwso  - maclhwso.  */
6086GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
6087/* maclhwu   - maclhwu.   */
6088GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
6089/* maclhwuo  - maclhwuo.  */
6090GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
6091/* maclhwsu  - maclhwsu.  */
6092GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
6093/* maclhwsuo - maclhwsuo. */
6094GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
6095/* nmacchw   - nmacchw.   */
6096GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
6097/* nmacchwo  - nmacchwo.  */
6098GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
6099/* nmacchws  - nmacchws.  */
6100GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
6101/* nmacchwso - nmacchwso. */
6102GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
6103/* nmachhw   - nmachhw.   */
6104GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
6105/* nmachhwo  - nmachhwo.  */
6106GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
6107/* nmachhws  - nmachhws.  */
6108GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
6109/* nmachhwso - nmachhwso. */
6110GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
6111/* nmaclhw   - nmaclhw.   */
6112GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
6113/* nmaclhwo  - nmaclhwo.  */
6114GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
6115/* nmaclhws  - nmaclhws.  */
6116GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
6117/* nmaclhwso - nmaclhwso. */
6118GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
6119
6120/* mulchw  - mulchw.  */
6121GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
6122/* mulchwu - mulchwu. */
6123GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
6124/* mulhhw  - mulhhw.  */
6125GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
6126/* mulhhwu - mulhhwu. */
6127GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
6128/* mullhw  - mullhw.  */
6129GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
6130/* mullhwu - mullhwu. */
6131GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
6132
6133/* mfdcr */
6134static void gen_mfdcr(DisasContext *ctx)
6135{
6136#if defined(CONFIG_USER_ONLY)
6137    GEN_PRIV;
6138#else
6139    TCGv dcrn;
6140
6141    CHK_SV;
6142    dcrn = tcg_const_tl(SPR(ctx->opcode));
6143    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
6144    tcg_temp_free(dcrn);
6145#endif /* defined(CONFIG_USER_ONLY) */
6146}
6147
6148/* mtdcr */
6149static void gen_mtdcr(DisasContext *ctx)
6150{
6151#if defined(CONFIG_USER_ONLY)
6152    GEN_PRIV;
6153#else
6154    TCGv dcrn;
6155
6156    CHK_SV;
6157    dcrn = tcg_const_tl(SPR(ctx->opcode));
6158    gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
6159    tcg_temp_free(dcrn);
6160#endif /* defined(CONFIG_USER_ONLY) */
6161}
6162
6163/* mfdcrx */
6164/* XXX: not implemented on 440 ? */
6165static void gen_mfdcrx(DisasContext *ctx)
6166{
6167#if defined(CONFIG_USER_ONLY)
6168    GEN_PRIV;
6169#else
6170    CHK_SV;
6171    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6172                        cpu_gpr[rA(ctx->opcode)]);
6173    /* Note: Rc update flag set leads to undefined state of Rc0 */
6174#endif /* defined(CONFIG_USER_ONLY) */
6175}
6176
6177/* mtdcrx */
6178/* XXX: not implemented on 440 ? */
6179static void gen_mtdcrx(DisasContext *ctx)
6180{
6181#if defined(CONFIG_USER_ONLY)
6182    GEN_PRIV;
6183#else
6184    CHK_SV;
6185    gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6186                         cpu_gpr[rS(ctx->opcode)]);
6187    /* Note: Rc update flag set leads to undefined state of Rc0 */
6188#endif /* defined(CONFIG_USER_ONLY) */
6189}
6190
6191/* mfdcrux (PPC 460) : user-mode access to DCR */
6192static void gen_mfdcrux(DisasContext *ctx)
6193{
6194    gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6195                        cpu_gpr[rA(ctx->opcode)]);
6196    /* Note: Rc update flag set leads to undefined state of Rc0 */
6197}
6198
6199/* mtdcrux (PPC 460) : user-mode access to DCR */
6200static void gen_mtdcrux(DisasContext *ctx)
6201{
6202    gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6203                         cpu_gpr[rS(ctx->opcode)]);
6204    /* Note: Rc update flag set leads to undefined state of Rc0 */
6205}
6206
6207/* dccci */
6208static void gen_dccci(DisasContext *ctx)
6209{
6210    CHK_SV;
6211    /* interpreted as no-op */
6212}
6213
6214/* dcread */
6215static void gen_dcread(DisasContext *ctx)
6216{
6217#if defined(CONFIG_USER_ONLY)
6218    GEN_PRIV;
6219#else
6220    TCGv EA, val;
6221
6222    CHK_SV;
6223    gen_set_access_type(ctx, ACCESS_CACHE);
6224    EA = tcg_temp_new();
6225    gen_addr_reg_index(ctx, EA);
6226    val = tcg_temp_new();
6227    gen_qemu_ld32u(ctx, val, EA);
6228    tcg_temp_free(val);
6229    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6230    tcg_temp_free(EA);
6231#endif /* defined(CONFIG_USER_ONLY) */
6232}
6233
6234/* icbt */
6235static void gen_icbt_40x(DisasContext *ctx)
6236{
6237    /*
6238     * interpreted as no-op
6239     * XXX: specification say this is treated as a load by the MMU but
6240     *      does not generate any exception
6241     */
6242}
6243
6244/* iccci */
6245static void gen_iccci(DisasContext *ctx)
6246{
6247    CHK_SV;
6248    /* interpreted as no-op */
6249}
6250
6251/* icread */
6252static void gen_icread(DisasContext *ctx)
6253{
6254    CHK_SV;
6255    /* interpreted as no-op */
6256}
6257
6258/* rfci (supervisor only) */
6259static void gen_rfci_40x(DisasContext *ctx)
6260{
6261#if defined(CONFIG_USER_ONLY)
6262    GEN_PRIV;
6263#else
6264    CHK_SV;
6265    /* Restore CPU state */
6266    gen_helper_40x_rfci(cpu_env);
6267    gen_sync_exception(ctx);
6268#endif /* defined(CONFIG_USER_ONLY) */
6269}
6270
6271static void gen_rfci(DisasContext *ctx)
6272{
6273#if defined(CONFIG_USER_ONLY)
6274    GEN_PRIV;
6275#else
6276    CHK_SV;
6277    /* Restore CPU state */
6278    gen_helper_rfci(cpu_env);
6279    gen_sync_exception(ctx);
6280#endif /* defined(CONFIG_USER_ONLY) */
6281}
6282
6283/* BookE specific */
6284
6285/* XXX: not implemented on 440 ? */
6286static void gen_rfdi(DisasContext *ctx)
6287{
6288#if defined(CONFIG_USER_ONLY)
6289    GEN_PRIV;
6290#else
6291    CHK_SV;
6292    /* Restore CPU state */
6293    gen_helper_rfdi(cpu_env);
6294    gen_sync_exception(ctx);
6295#endif /* defined(CONFIG_USER_ONLY) */
6296}
6297
6298/* XXX: not implemented on 440 ? */
6299static void gen_rfmci(DisasContext *ctx)
6300{
6301#if defined(CONFIG_USER_ONLY)
6302    GEN_PRIV;
6303#else
6304    CHK_SV;
6305    /* Restore CPU state */
6306    gen_helper_rfmci(cpu_env);
6307    gen_sync_exception(ctx);
6308#endif /* defined(CONFIG_USER_ONLY) */
6309}
6310
6311/* TLB management - PowerPC 405 implementation */
6312
6313/* tlbre */
6314static void gen_tlbre_40x(DisasContext *ctx)
6315{
6316#if defined(CONFIG_USER_ONLY)
6317    GEN_PRIV;
6318#else
6319    CHK_SV;
6320    switch (rB(ctx->opcode)) {
6321    case 0:
6322        gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6323                                cpu_gpr[rA(ctx->opcode)]);
6324        break;
6325    case 1:
6326        gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6327                                cpu_gpr[rA(ctx->opcode)]);
6328        break;
6329    default:
6330        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6331        break;
6332    }
6333#endif /* defined(CONFIG_USER_ONLY) */
6334}
6335
6336/* tlbsx - tlbsx. */
6337static void gen_tlbsx_40x(DisasContext *ctx)
6338{
6339#if defined(CONFIG_USER_ONLY)
6340    GEN_PRIV;
6341#else
6342    TCGv t0;
6343
6344    CHK_SV;
6345    t0 = tcg_temp_new();
6346    gen_addr_reg_index(ctx, t0);
6347    gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6348    tcg_temp_free(t0);
6349    if (Rc(ctx->opcode)) {
6350        TCGLabel *l1 = gen_new_label();
6351        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6352        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6353        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6354        gen_set_label(l1);
6355    }
6356#endif /* defined(CONFIG_USER_ONLY) */
6357}
6358
6359/* tlbwe */
6360static void gen_tlbwe_40x(DisasContext *ctx)
6361{
6362#if defined(CONFIG_USER_ONLY)
6363    GEN_PRIV;
6364#else
6365    CHK_SV;
6366
6367    switch (rB(ctx->opcode)) {
6368    case 0:
6369        gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6370                                cpu_gpr[rS(ctx->opcode)]);
6371        break;
6372    case 1:
6373        gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6374                                cpu_gpr[rS(ctx->opcode)]);
6375        break;
6376    default:
6377        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6378        break;
6379    }
6380#endif /* defined(CONFIG_USER_ONLY) */
6381}
6382
6383/* TLB management - PowerPC 440 implementation */
6384
6385/* tlbre */
6386static void gen_tlbre_440(DisasContext *ctx)
6387{
6388#if defined(CONFIG_USER_ONLY)
6389    GEN_PRIV;
6390#else
6391    CHK_SV;
6392
6393    switch (rB(ctx->opcode)) {
6394    case 0:
6395    case 1:
6396    case 2:
6397        {
6398            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6399            gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6400                                 t0, cpu_gpr[rA(ctx->opcode)]);
6401            tcg_temp_free_i32(t0);
6402        }
6403        break;
6404    default:
6405        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6406        break;
6407    }
6408#endif /* defined(CONFIG_USER_ONLY) */
6409}
6410
6411/* tlbsx - tlbsx. */
6412static void gen_tlbsx_440(DisasContext *ctx)
6413{
6414#if defined(CONFIG_USER_ONLY)
6415    GEN_PRIV;
6416#else
6417    TCGv t0;
6418
6419    CHK_SV;
6420    t0 = tcg_temp_new();
6421    gen_addr_reg_index(ctx, t0);
6422    gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6423    tcg_temp_free(t0);
6424    if (Rc(ctx->opcode)) {
6425        TCGLabel *l1 = gen_new_label();
6426        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6427        tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6428        tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6429        gen_set_label(l1);
6430    }
6431#endif /* defined(CONFIG_USER_ONLY) */
6432}
6433
6434/* tlbwe */
6435static void gen_tlbwe_440(DisasContext *ctx)
6436{
6437#if defined(CONFIG_USER_ONLY)
6438    GEN_PRIV;
6439#else
6440    CHK_SV;
6441    switch (rB(ctx->opcode)) {
6442    case 0:
6443    case 1:
6444    case 2:
6445        {
6446            TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6447            gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6448                                 cpu_gpr[rS(ctx->opcode)]);
6449            tcg_temp_free_i32(t0);
6450        }
6451        break;
6452    default:
6453        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6454        break;
6455    }
6456#endif /* defined(CONFIG_USER_ONLY) */
6457}
6458
6459/* TLB management - PowerPC BookE 2.06 implementation */
6460
6461/* tlbre */
6462static void gen_tlbre_booke206(DisasContext *ctx)
6463{
6464 #if defined(CONFIG_USER_ONLY)
6465    GEN_PRIV;
6466#else
6467   CHK_SV;
6468    gen_helper_booke206_tlbre(cpu_env);
6469#endif /* defined(CONFIG_USER_ONLY) */
6470}
6471
6472/* tlbsx - tlbsx. */
6473static void gen_tlbsx_booke206(DisasContext *ctx)
6474{
6475#if defined(CONFIG_USER_ONLY)
6476    GEN_PRIV;
6477#else
6478    TCGv t0;
6479
6480    CHK_SV;
6481    if (rA(ctx->opcode)) {
6482        t0 = tcg_temp_new();
6483        tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6484    } else {
6485        t0 = tcg_const_tl(0);
6486    }
6487
6488    tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6489    gen_helper_booke206_tlbsx(cpu_env, t0);
6490    tcg_temp_free(t0);
6491#endif /* defined(CONFIG_USER_ONLY) */
6492}
6493
6494/* tlbwe */
6495static void gen_tlbwe_booke206(DisasContext *ctx)
6496{
6497#if defined(CONFIG_USER_ONLY)
6498    GEN_PRIV;
6499#else
6500    CHK_SV;
6501    gen_helper_booke206_tlbwe(cpu_env);
6502#endif /* defined(CONFIG_USER_ONLY) */
6503}
6504
6505static void gen_tlbivax_booke206(DisasContext *ctx)
6506{
6507#if defined(CONFIG_USER_ONLY)
6508    GEN_PRIV;
6509#else
6510    TCGv t0;
6511
6512    CHK_SV;
6513    t0 = tcg_temp_new();
6514    gen_addr_reg_index(ctx, t0);
6515    gen_helper_booke206_tlbivax(cpu_env, t0);
6516    tcg_temp_free(t0);
6517#endif /* defined(CONFIG_USER_ONLY) */
6518}
6519
6520static void gen_tlbilx_booke206(DisasContext *ctx)
6521{
6522#if defined(CONFIG_USER_ONLY)
6523    GEN_PRIV;
6524#else
6525    TCGv t0;
6526
6527    CHK_SV;
6528    t0 = tcg_temp_new();
6529    gen_addr_reg_index(ctx, t0);
6530
6531    switch ((ctx->opcode >> 21) & 0x3) {
6532    case 0:
6533        gen_helper_booke206_tlbilx0(cpu_env, t0);
6534        break;
6535    case 1:
6536        gen_helper_booke206_tlbilx1(cpu_env, t0);
6537        break;
6538    case 3:
6539        gen_helper_booke206_tlbilx3(cpu_env, t0);
6540        break;
6541    default:
6542        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6543        break;
6544    }
6545
6546    tcg_temp_free(t0);
6547#endif /* defined(CONFIG_USER_ONLY) */
6548}
6549
6550
6551/* wrtee */
6552static void gen_wrtee(DisasContext *ctx)
6553{
6554#if defined(CONFIG_USER_ONLY)
6555    GEN_PRIV;
6556#else
6557    TCGv t0;
6558
6559    CHK_SV;
6560    t0 = tcg_temp_new();
6561    tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6562    tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6563    tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6564    tcg_temp_free(t0);
6565    /*
6566     * Stop translation to have a chance to raise an exception if we
6567     * just set msr_ee to 1
6568     */
6569    gen_stop_exception(ctx);
6570#endif /* defined(CONFIG_USER_ONLY) */
6571}
6572
6573/* wrteei */
6574static void gen_wrteei(DisasContext *ctx)
6575{
6576#if defined(CONFIG_USER_ONLY)
6577    GEN_PRIV;
6578#else
6579    CHK_SV;
6580    if (ctx->opcode & 0x00008000) {
6581        tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6582        /* Stop translation to have a chance to raise an exception */
6583        gen_stop_exception(ctx);
6584    } else {
6585        tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6586    }
6587#endif /* defined(CONFIG_USER_ONLY) */
6588}
6589
6590/* PowerPC 440 specific instructions */
6591
6592/* dlmzb */
6593static void gen_dlmzb(DisasContext *ctx)
6594{
6595    TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6596    gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6597                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6598    tcg_temp_free_i32(t0);
6599}
6600
6601/* mbar replaces eieio on 440 */
6602static void gen_mbar(DisasContext *ctx)
6603{
6604    /* interpreted as no-op */
6605}
6606
6607/* msync replaces sync on 440 */
6608static void gen_msync_4xx(DisasContext *ctx)
6609{
6610    /* Only e500 seems to treat reserved bits as invalid */
6611    if ((ctx->insns_flags2 & PPC2_BOOKE206) &&
6612        (ctx->opcode & 0x03FFF801)) {
6613        gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6614    }
6615    /* otherwise interpreted as no-op */
6616}
6617
6618/* icbt */
6619static void gen_icbt_440(DisasContext *ctx)
6620{
6621    /*
6622     * interpreted as no-op
6623     * XXX: specification say this is treated as a load by the MMU but
6624     *      does not generate any exception
6625     */
6626}
6627
6628/* Embedded.Processor Control */
6629
6630static void gen_msgclr(DisasContext *ctx)
6631{
6632#if defined(CONFIG_USER_ONLY)
6633    GEN_PRIV;
6634#else
6635    CHK_HV;
6636    if (is_book3s_arch2x(ctx)) {
6637        gen_helper_book3s_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6638    } else {
6639        gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6640    }
6641#endif /* defined(CONFIG_USER_ONLY) */
6642}
6643
6644static void gen_msgsnd(DisasContext *ctx)
6645{
6646#if defined(CONFIG_USER_ONLY)
6647    GEN_PRIV;
6648#else
6649    CHK_HV;
6650    if (is_book3s_arch2x(ctx)) {
6651        gen_helper_book3s_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6652    } else {
6653        gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6654    }
6655#endif /* defined(CONFIG_USER_ONLY) */
6656}
6657
6658static void gen_msgsync(DisasContext *ctx)
6659{
6660#if defined(CONFIG_USER_ONLY)
6661    GEN_PRIV;
6662#else
6663    CHK_HV;
6664#endif /* defined(CONFIG_USER_ONLY) */
6665    /* interpreted as no-op */
6666}
6667
6668#if defined(TARGET_PPC64)
6669static void gen_maddld(DisasContext *ctx)
6670{
6671    TCGv_i64 t1 = tcg_temp_new_i64();
6672
6673    tcg_gen_mul_i64(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6674    tcg_gen_add_i64(cpu_gpr[rD(ctx->opcode)], t1, cpu_gpr[rC(ctx->opcode)]);
6675    tcg_temp_free_i64(t1);
6676}
6677
6678/* maddhd maddhdu */
6679static void gen_maddhd_maddhdu(DisasContext *ctx)
6680{
6681    TCGv_i64 lo = tcg_temp_new_i64();
6682    TCGv_i64 hi = tcg_temp_new_i64();
6683    TCGv_i64 t1 = tcg_temp_new_i64();
6684
6685    if (Rc(ctx->opcode)) {
6686        tcg_gen_mulu2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6687                          cpu_gpr[rB(ctx->opcode)]);
6688        tcg_gen_movi_i64(t1, 0);
6689    } else {
6690        tcg_gen_muls2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6691                          cpu_gpr[rB(ctx->opcode)]);
6692        tcg_gen_sari_i64(t1, cpu_gpr[rC(ctx->opcode)], 63);
6693    }
6694    tcg_gen_add2_i64(t1, cpu_gpr[rD(ctx->opcode)], lo, hi,
6695                     cpu_gpr[rC(ctx->opcode)], t1);
6696    tcg_temp_free_i64(lo);
6697    tcg_temp_free_i64(hi);
6698    tcg_temp_free_i64(t1);
6699}
6700#endif /* defined(TARGET_PPC64) */
6701
6702static void gen_tbegin(DisasContext *ctx)
6703{
6704    if (unlikely(!ctx->tm_enabled)) {
6705        gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6706        return;
6707    }
6708    gen_helper_tbegin(cpu_env);
6709}
6710
6711#define GEN_TM_NOOP(name)                                      \
6712static inline void gen_##name(DisasContext *ctx)               \
6713{                                                              \
6714    if (unlikely(!ctx->tm_enabled)) {                          \
6715        gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
6716        return;                                                \
6717    }                                                          \
6718    /*                                                         \
6719     * Because tbegin always fails in QEMU, these user         \
6720     * space instructions all have a simple implementation:    \
6721     *                                                         \
6722     *     CR[0] = 0b0 || MSR[TS] || 0b0                       \
6723     *           = 0b0 || 0b00    || 0b0                       \
6724     */                                                        \
6725    tcg_gen_movi_i32(cpu_crf[0], 0);                           \
6726}
6727
6728GEN_TM_NOOP(tend);
6729GEN_TM_NOOP(tabort);
6730GEN_TM_NOOP(tabortwc);
6731GEN_TM_NOOP(tabortwci);
6732GEN_TM_NOOP(tabortdc);
6733GEN_TM_NOOP(tabortdci);
6734GEN_TM_NOOP(tsr);
6735
6736static inline void gen_cp_abort(DisasContext *ctx)
6737{
6738    /* Do Nothing */
6739}
6740
6741#define GEN_CP_PASTE_NOOP(name)                           \
6742static inline void gen_##name(DisasContext *ctx)          \
6743{                                                         \
6744    /*                                                    \
6745     * Generate invalid exception until we have an        \
6746     * implementation of the copy paste facility          \
6747     */                                                   \
6748    gen_invalid(ctx);                                     \
6749}
6750
6751GEN_CP_PASTE_NOOP(copy)
6752GEN_CP_PASTE_NOOP(paste)
6753
6754static void gen_tcheck(DisasContext *ctx)
6755{
6756    if (unlikely(!ctx->tm_enabled)) {
6757        gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6758        return;
6759    }
6760    /*
6761     * Because tbegin always fails, the tcheck implementation is
6762     * simple:
6763     *
6764     * CR[CRF] = TDOOMED || MSR[TS] || 0b0
6765     *         = 0b1 || 0b00 || 0b0
6766     */
6767    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0x8);
6768}
6769
6770#if defined(CONFIG_USER_ONLY)
6771#define GEN_TM_PRIV_NOOP(name)                                 \
6772static inline void gen_##name(DisasContext *ctx)               \
6773{                                                              \
6774    gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);            \
6775}
6776
6777#else
6778
6779#define GEN_TM_PRIV_NOOP(name)                                 \
6780static inline void gen_##name(DisasContext *ctx)               \
6781{                                                              \
6782    CHK_SV;                                                    \
6783    if (unlikely(!ctx->tm_enabled)) {                          \
6784        gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
6785        return;                                                \
6786    }                                                          \
6787    /*                                                         \
6788     * Because tbegin always fails, the implementation is      \
6789     * simple:                                                 \
6790     *                                                         \
6791     *   CR[0] = 0b0 || MSR[TS] || 0b0                         \
6792     *         = 0b0 || 0b00 | 0b0                             \
6793     */                                                        \
6794    tcg_gen_movi_i32(cpu_crf[0], 0);                           \
6795}
6796
6797#endif
6798
6799GEN_TM_PRIV_NOOP(treclaim);
6800GEN_TM_PRIV_NOOP(trechkpt);
6801
6802static inline void get_fpr(TCGv_i64 dst, int regno)
6803{
6804    tcg_gen_ld_i64(dst, cpu_env, fpr_offset(regno));
6805}
6806
6807static inline void set_fpr(int regno, TCGv_i64 src)
6808{
6809    tcg_gen_st_i64(src, cpu_env, fpr_offset(regno));
6810}
6811
6812static inline void get_avr64(TCGv_i64 dst, int regno, bool high)
6813{
6814    tcg_gen_ld_i64(dst, cpu_env, avr64_offset(regno, high));
6815}
6816
6817static inline void set_avr64(int regno, TCGv_i64 src, bool high)
6818{
6819    tcg_gen_st_i64(src, cpu_env, avr64_offset(regno, high));
6820}
6821
6822#include "translate/fp-impl.inc.c"
6823
6824#include "translate/vmx-impl.inc.c"
6825
6826#include "translate/vsx-impl.inc.c"
6827
6828#include "translate/dfp-impl.inc.c"
6829
6830#include "translate/spe-impl.inc.c"
6831
6832/* Handles lfdp, lxsd, lxssp */
6833static void gen_dform39(DisasContext *ctx)
6834{
6835    switch (ctx->opcode & 0x3) {
6836    case 0: /* lfdp */
6837        if (ctx->insns_flags2 & PPC2_ISA205) {
6838            return gen_lfdp(ctx);
6839        }
6840        break;
6841    case 2: /* lxsd */
6842        if (ctx->insns_flags2 & PPC2_ISA300) {
6843            return gen_lxsd(ctx);
6844        }
6845        break;
6846    case 3: /* lxssp */
6847        if (ctx->insns_flags2 & PPC2_ISA300) {
6848            return gen_lxssp(ctx);
6849        }
6850        break;
6851    }
6852    return gen_invalid(ctx);
6853}
6854
6855/* handles stfdp, lxv, stxsd, stxssp lxvx */
6856static void gen_dform3D(DisasContext *ctx)
6857{
6858    if ((ctx->opcode & 3) == 1) { /* DQ-FORM */
6859        switch (ctx->opcode & 0x7) {
6860        case 1: /* lxv */
6861            if (ctx->insns_flags2 & PPC2_ISA300) {
6862                return gen_lxv(ctx);
6863            }
6864            break;
6865        case 5: /* stxv */
6866            if (ctx->insns_flags2 & PPC2_ISA300) {
6867                return gen_stxv(ctx);
6868            }
6869            break;
6870        }
6871    } else { /* DS-FORM */
6872        switch (ctx->opcode & 0x3) {
6873        case 0: /* stfdp */
6874            if (ctx->insns_flags2 & PPC2_ISA205) {
6875                return gen_stfdp(ctx);
6876            }
6877            break;
6878        case 2: /* stxsd */
6879            if (ctx->insns_flags2 & PPC2_ISA300) {
6880                return gen_stxsd(ctx);
6881            }
6882            break;
6883        case 3: /* stxssp */
6884            if (ctx->insns_flags2 & PPC2_ISA300) {
6885                return gen_stxssp(ctx);
6886            }
6887            break;
6888        }
6889    }
6890    return gen_invalid(ctx);
6891}
6892
6893static opcode_t opcodes[] = {
6894GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
6895GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
6896GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
6897GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400001, PPC_INTEGER),
6898GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
6899#if defined(TARGET_PPC64)
6900GEN_HANDLER_E(cmpeqb, 0x1F, 0x00, 0x07, 0x00600000, PPC_NONE, PPC2_ISA300),
6901#endif
6902GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
6903GEN_HANDLER_E(cmprb, 0x1F, 0x00, 0x06, 0x00400001, PPC_NONE, PPC2_ISA300),
6904GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
6905GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6906GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6907GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6908GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6909GEN_HANDLER_E(addpcis, 0x13, 0x2, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
6910GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
6911GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
6912GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
6913GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
6914GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6915#if defined(TARGET_PPC64)
6916GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
6917#endif
6918GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
6919GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
6920GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6921GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6922GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6923GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
6924GEN_HANDLER_E(cnttzw, 0x1F, 0x1A, 0x10, 0x00000000, PPC_NONE, PPC2_ISA300),
6925GEN_HANDLER_E(copy, 0x1F, 0x06, 0x18, 0x03C00001, PPC_NONE, PPC2_ISA300),
6926GEN_HANDLER_E(cp_abort, 0x1F, 0x06, 0x1A, 0x03FFF801, PPC_NONE, PPC2_ISA300),
6927GEN_HANDLER_E(paste, 0x1F, 0x06, 0x1C, 0x03C00000, PPC_NONE, PPC2_ISA300),
6928GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
6929GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
6930GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6931GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6932GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6933GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6934GEN_HANDLER(popcntb, 0x1F, 0x1A, 0x03, 0x0000F801, PPC_POPCNTB),
6935GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
6936GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
6937#if defined(TARGET_PPC64)
6938GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
6939GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
6940GEN_HANDLER_E(cnttzd, 0x1F, 0x1A, 0x11, 0x00000000, PPC_NONE, PPC2_ISA300),
6941GEN_HANDLER_E(darn, 0x1F, 0x13, 0x17, 0x001CF801, PPC_NONE, PPC2_ISA300),
6942GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
6943GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
6944#endif
6945GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6946GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6947GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6948GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
6949GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
6950GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
6951GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
6952#if defined(TARGET_PPC64)
6953GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
6954GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
6955GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
6956GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
6957GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
6958GEN_HANDLER2_E(extswsli0, "extswsli", 0x1F, 0x1A, 0x1B, 0x00000000,
6959               PPC_NONE, PPC2_ISA300),
6960GEN_HANDLER2_E(extswsli1, "extswsli", 0x1F, 0x1B, 0x1B, 0x00000000,
6961               PPC_NONE, PPC2_ISA300),
6962#endif
6963#if defined(TARGET_PPC64)
6964GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
6965GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
6966GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
6967#endif
6968/* handles lfdp, lxsd, lxssp */
6969GEN_HANDLER_E(dform39, 0x39, 0xFF, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA205),
6970/* handles stfdp, lxv, stxsd, stxssp, stxv */
6971GEN_HANDLER_E(dform3D, 0x3D, 0xFF, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA205),
6972GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6973GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6974GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
6975GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
6976GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
6977GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
6978GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x01FFF801, PPC_MEM_EIEIO),
6979GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
6980GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6981GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6982GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
6983GEN_HANDLER_E(lwat, 0x1F, 0x06, 0x12, 0x00000001, PPC_NONE, PPC2_ISA300),
6984GEN_HANDLER_E(stwat, 0x1F, 0x06, 0x16, 0x00000001, PPC_NONE, PPC2_ISA300),
6985GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6986GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6987GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
6988#if defined(TARGET_PPC64)
6989GEN_HANDLER_E(ldat, 0x1F, 0x06, 0x13, 0x00000001, PPC_NONE, PPC2_ISA300),
6990GEN_HANDLER_E(stdat, 0x1F, 0x06, 0x17, 0x00000001, PPC_NONE, PPC2_ISA300),
6991GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
6992GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
6993GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
6994GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
6995#endif
6996GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
6997GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
6998GEN_HANDLER_E(wait, 0x1F, 0x1E, 0x00, 0x039FF801, PPC_NONE, PPC2_ISA300),
6999GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
7000GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
7001GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
7002GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
7003GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0x0000E000, PPC_NONE, PPC2_BCTAR_ISA207),
7004GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
7005GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
7006#if defined(TARGET_PPC64)
7007GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
7008GEN_HANDLER_E(stop, 0x13, 0x12, 0x0b, 0x03FFF801, PPC_NONE, PPC2_ISA300),
7009GEN_HANDLER_E(doze, 0x13, 0x12, 0x0c, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7010GEN_HANDLER_E(nap, 0x13, 0x12, 0x0d, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7011GEN_HANDLER_E(sleep, 0x13, 0x12, 0x0e, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7012GEN_HANDLER_E(rvwinkle, 0x13, 0x12, 0x0f, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7013GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
7014#endif
7015GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
7016GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
7017GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
7018#if defined(TARGET_PPC64)
7019GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
7020GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
7021#endif
7022GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
7023GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
7024GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
7025GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
7026GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
7027GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
7028#if defined(TARGET_PPC64)
7029GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
7030GEN_HANDLER_E(setb, 0x1F, 0x00, 0x04, 0x0003F801, PPC_NONE, PPC2_ISA300),
7031GEN_HANDLER_E(mcrxrx, 0x1F, 0x00, 0x12, 0x007FF801, PPC_NONE, PPC2_ISA300),
7032#endif
7033GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001EF801, PPC_MISC),
7034GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000000, PPC_MISC),
7035GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
7036GEN_HANDLER_E(dcbfep, 0x1F, 0x1F, 0x03, 0x03C00001, PPC_NONE, PPC2_BOOKE206),
7037GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
7038GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
7039GEN_HANDLER_E(dcbstep, 0x1F, 0x1F, 0x01, 0x03E00001, PPC_NONE, PPC2_BOOKE206),
7040GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
7041GEN_HANDLER_E(dcbtep, 0x1F, 0x1F, 0x09, 0x00000001, PPC_NONE, PPC2_BOOKE206),
7042GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
7043GEN_HANDLER_E(dcbtstep, 0x1F, 0x1F, 0x07, 0x00000001, PPC_NONE, PPC2_BOOKE206),
7044GEN_HANDLER_E(dcbtls, 0x1F, 0x06, 0x05, 0x02000001, PPC_BOOKE, PPC2_BOOKE206),
7045GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
7046GEN_HANDLER_E(dcbzep, 0x1F, 0x1F, 0x1F, 0x03C00001, PPC_NONE, PPC2_BOOKE206),
7047GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
7048GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x01800001, PPC_ALTIVEC),
7049GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
7050GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
7051GEN_HANDLER_E(icbiep, 0x1F, 0x1F, 0x1E, 0x03E00001, PPC_NONE, PPC2_BOOKE206),
7052GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
7053GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
7054GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
7055GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
7056GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
7057#if defined(TARGET_PPC64)
7058GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
7059GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
7060             PPC_SEGMENT_64B),
7061GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
7062GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
7063             PPC_SEGMENT_64B),
7064GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
7065GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
7066GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
7067GEN_HANDLER2(slbfee_, "slbfee.", 0x1F, 0x13, 0x1E, 0x001F0000, PPC_SEGMENT_64B),
7068#endif
7069GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
7070/*
7071 * XXX Those instructions will need to be handled differently for
7072 * different ISA versions
7073 */
7074GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x001F0001, PPC_MEM_TLBIE),
7075GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x001F0001, PPC_MEM_TLBIE),
7076GEN_HANDLER_E(tlbiel, 0x1F, 0x12, 0x08, 0x00100001, PPC_NONE, PPC2_ISA300),
7077GEN_HANDLER_E(tlbie, 0x1F, 0x12, 0x09, 0x00100001, PPC_NONE, PPC2_ISA300),
7078GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
7079#if defined(TARGET_PPC64)
7080GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x031FFC01, PPC_SLBI),
7081GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
7082GEN_HANDLER_E(slbieg, 0x1F, 0x12, 0x0E, 0x001F0001, PPC_NONE, PPC2_ISA300),
7083GEN_HANDLER_E(slbsync, 0x1F, 0x12, 0x0A, 0x03FFF801, PPC_NONE, PPC2_ISA300),
7084#endif
7085GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
7086GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
7087GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
7088GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
7089GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
7090GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
7091GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
7092GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
7093GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
7094GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
7095GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
7096GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
7097GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
7098GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
7099GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
7100GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
7101GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
7102GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
7103GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
7104GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
7105GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
7106GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
7107GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
7108GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
7109GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
7110GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
7111GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
7112GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
7113GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
7114GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
7115GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
7116GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
7117GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
7118GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
7119GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
7120GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
7121GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
7122GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
7123GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
7124GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
7125GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
7126GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
7127GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
7128GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
7129GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
7130GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
7131GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
7132GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
7133GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
7134GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7135GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7136GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
7137GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
7138GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7139GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7140GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
7141GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
7142GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
7143GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
7144GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
7145GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
7146GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
7147GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
7148GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
7149GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
7150GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
7151GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
7152GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
7153GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
7154GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
7155GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
7156GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
7157GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
7158GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
7159GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
7160GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
7161GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
7162GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
7163GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
7164GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
7165GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
7166               PPC_NONE, PPC2_BOOKE206),
7167GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
7168               PPC_NONE, PPC2_BOOKE206),
7169GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
7170               PPC_NONE, PPC2_BOOKE206),
7171GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
7172               PPC_NONE, PPC2_BOOKE206),
7173GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
7174               PPC_NONE, PPC2_BOOKE206),
7175GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
7176               PPC_NONE, PPC2_PRCNTL),
7177GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
7178               PPC_NONE, PPC2_PRCNTL),
7179GEN_HANDLER2_E(msgsync, "msgsync", 0x1F, 0x16, 0x1B, 0x00000000,
7180               PPC_NONE, PPC2_PRCNTL),
7181GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
7182GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
7183GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
7184GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
7185              PPC_BOOKE, PPC2_BOOKE206),
7186GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x039FF801, PPC_BOOKE),
7187GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
7188               PPC_BOOKE, PPC2_BOOKE206),
7189GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x06, 0x08, 0x03E00001,
7190             PPC_440_SPEC),
7191GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
7192GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
7193GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
7194GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
7195GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
7196#if defined(TARGET_PPC64)
7197GEN_HANDLER_E(maddhd_maddhdu, 0x04, 0x18, 0xFF, 0x00000000, PPC_NONE,
7198              PPC2_ISA300),
7199GEN_HANDLER_E(maddld, 0x04, 0x19, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
7200#endif
7201
7202#undef GEN_INT_ARITH_ADD
7203#undef GEN_INT_ARITH_ADD_CONST
7204#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
7205GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
7206#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
7207                                add_ca, compute_ca, compute_ov)               \
7208GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
7209GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
7210GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
7211GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
7212GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
7213GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
7214GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
7215GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
7216GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
7217GEN_HANDLER_E(addex, 0x1F, 0x0A, 0x05, 0x00000000, PPC_NONE, PPC2_ISA300),
7218GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
7219GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
7220
7221#undef GEN_INT_ARITH_DIVW
7222#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
7223GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
7224GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
7225GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
7226GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
7227GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
7228GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7229GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7230GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7231GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7232GEN_HANDLER_E(modsw, 0x1F, 0x0B, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
7233GEN_HANDLER_E(moduw, 0x1F, 0x0B, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
7234
7235#if defined(TARGET_PPC64)
7236#undef GEN_INT_ARITH_DIVD
7237#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
7238GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
7239GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
7240GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
7241GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
7242GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
7243
7244GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7245GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7246GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7247GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7248GEN_HANDLER_E(modsd, 0x1F, 0x09, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
7249GEN_HANDLER_E(modud, 0x1F, 0x09, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
7250
7251#undef GEN_INT_ARITH_MUL_HELPER
7252#define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
7253GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
7254GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
7255GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
7256GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
7257#endif
7258
7259#undef GEN_INT_ARITH_SUBF
7260#undef GEN_INT_ARITH_SUBF_CONST
7261#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
7262GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
7263#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
7264                                add_ca, compute_ca, compute_ov)               \
7265GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
7266GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
7267GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
7268GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
7269GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
7270GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
7271GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
7272GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
7273GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
7274GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
7275GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
7276
7277#undef GEN_LOGICAL1
7278#undef GEN_LOGICAL2
7279#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
7280GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
7281#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
7282GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
7283GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
7284GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
7285GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
7286GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
7287GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
7288GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
7289GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
7290GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
7291#if defined(TARGET_PPC64)
7292GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
7293#endif
7294
7295#if defined(TARGET_PPC64)
7296#undef GEN_PPC64_R2
7297#undef GEN_PPC64_R4
7298#define GEN_PPC64_R2(name, opc1, opc2)                                        \
7299GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
7300GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
7301             PPC_64B)
7302#define GEN_PPC64_R4(name, opc1, opc2)                                        \
7303GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
7304GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
7305             PPC_64B),                                                        \
7306GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
7307             PPC_64B),                                                        \
7308GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
7309             PPC_64B)
7310GEN_PPC64_R4(rldicl, 0x1E, 0x00),
7311GEN_PPC64_R4(rldicr, 0x1E, 0x02),
7312GEN_PPC64_R4(rldic, 0x1E, 0x04),
7313GEN_PPC64_R2(rldcl, 0x1E, 0x08),
7314GEN_PPC64_R2(rldcr, 0x1E, 0x09),
7315GEN_PPC64_R4(rldimi, 0x1E, 0x06),
7316#endif
7317
7318#undef GEN_LD
7319#undef GEN_LDU
7320#undef GEN_LDUX
7321#undef GEN_LDX_E
7322#undef GEN_LDS
7323#define GEN_LD(name, ldop, opc, type)                                         \
7324GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
7325#define GEN_LDU(name, ldop, opc, type)                                        \
7326GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
7327#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
7328GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
7329#define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
7330GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
7331#define GEN_LDS(name, ldop, op, type)                                         \
7332GEN_LD(name, ldop, op | 0x20, type)                                           \
7333GEN_LDU(name, ldop, op | 0x21, type)                                          \
7334GEN_LDUX(name, ldop, 0x17, op | 0x01, type)                                   \
7335GEN_LDX(name, ldop, 0x17, op | 0x00, type)
7336
7337GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
7338GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
7339GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
7340GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
7341#if defined(TARGET_PPC64)
7342GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
7343GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
7344GEN_LDUX(ld, ld64_i64, 0x15, 0x01, PPC_64B)
7345GEN_LDX(ld, ld64_i64, 0x15, 0x00, PPC_64B)
7346GEN_LDX_E(ldbr, ld64ur_i64, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE)
7347
7348/* HV/P7 and later only */
7349GEN_LDX_HVRM(ldcix, ld64_i64, 0x15, 0x1b, PPC_CILDST)
7350GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x18, PPC_CILDST)
7351GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
7352GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
7353#endif
7354GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
7355GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
7356
7357/* External PID based load */
7358#undef GEN_LDEPX
7359#define GEN_LDEPX(name, ldop, opc2, opc3)                                     \
7360GEN_HANDLER_E(name##epx, 0x1F, opc2, opc3,                                    \
7361              0x00000001, PPC_NONE, PPC2_BOOKE206),
7362
7363GEN_LDEPX(lb, DEF_MEMOP(MO_UB), 0x1F, 0x02)
7364GEN_LDEPX(lh, DEF_MEMOP(MO_UW), 0x1F, 0x08)
7365GEN_LDEPX(lw, DEF_MEMOP(MO_UL), 0x1F, 0x00)
7366#if defined(TARGET_PPC64)
7367GEN_LDEPX(ld, DEF_MEMOP(MO_Q), 0x1D, 0x00)
7368#endif
7369
7370#undef GEN_ST
7371#undef GEN_STU
7372#undef GEN_STUX
7373#undef GEN_STX_E
7374#undef GEN_STS
7375#define GEN_ST(name, stop, opc, type)                                         \
7376GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
7377#define GEN_STU(name, stop, opc, type)                                        \
7378GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
7379#define GEN_STUX(name, stop, opc2, opc3, type)                                \
7380GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
7381#define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
7382GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000000, type, type2),
7383#define GEN_STS(name, stop, op, type)                                         \
7384GEN_ST(name, stop, op | 0x20, type)                                           \
7385GEN_STU(name, stop, op | 0x21, type)                                          \
7386GEN_STUX(name, stop, 0x17, op | 0x01, type)                                   \
7387GEN_STX(name, stop, 0x17, op | 0x00, type)
7388
7389GEN_STS(stb, st8, 0x06, PPC_INTEGER)
7390GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
7391GEN_STS(stw, st32, 0x04, PPC_INTEGER)
7392#if defined(TARGET_PPC64)
7393GEN_STUX(std, st64_i64, 0x15, 0x05, PPC_64B)
7394GEN_STX(std, st64_i64, 0x15, 0x04, PPC_64B)
7395GEN_STX_E(stdbr, st64r_i64, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE)
7396GEN_STX_HVRM(stdcix, st64_i64, 0x15, 0x1f, PPC_CILDST)
7397GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
7398GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
7399GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
7400#endif
7401GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
7402GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
7403
7404#undef GEN_STEPX
7405#define GEN_STEPX(name, ldop, opc2, opc3)                                     \
7406GEN_HANDLER_E(name##epx, 0x1F, opc2, opc3,                                    \
7407              0x00000001, PPC_NONE, PPC2_BOOKE206),
7408
7409GEN_STEPX(stb, DEF_MEMOP(MO_UB), 0x1F, 0x06)
7410GEN_STEPX(sth, DEF_MEMOP(MO_UW), 0x1F, 0x0C)
7411GEN_STEPX(stw, DEF_MEMOP(MO_UL), 0x1F, 0x04)
7412#if defined(TARGET_PPC64)
7413GEN_STEPX(std, DEF_MEMOP(MO_Q), 0x1D, 0x04)
7414#endif
7415
7416#undef GEN_CRLOGIC
7417#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
7418GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
7419GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
7420GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
7421GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
7422GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
7423GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
7424GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
7425GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
7426GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
7427
7428#undef GEN_MAC_HANDLER
7429#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
7430GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
7431GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
7432GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
7433GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
7434GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
7435GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
7436GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
7437GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
7438GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
7439GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
7440GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
7441GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
7442GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
7443GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
7444GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
7445GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
7446GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
7447GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
7448GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
7449GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
7450GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
7451GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
7452GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
7453GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
7454GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
7455GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
7456GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
7457GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
7458GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
7459GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
7460GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
7461GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
7462GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
7463GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
7464GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
7465GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
7466GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
7467GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
7468GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
7469GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
7470GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
7471GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
7472GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
7473
7474GEN_HANDLER2_E(tbegin, "tbegin", 0x1F, 0x0E, 0x14, 0x01DFF800, \
7475               PPC_NONE, PPC2_TM),
7476GEN_HANDLER2_E(tend,   "tend",   0x1F, 0x0E, 0x15, 0x01FFF800, \
7477               PPC_NONE, PPC2_TM),
7478GEN_HANDLER2_E(tabort, "tabort", 0x1F, 0x0E, 0x1C, 0x03E0F800, \
7479               PPC_NONE, PPC2_TM),
7480GEN_HANDLER2_E(tabortwc, "tabortwc", 0x1F, 0x0E, 0x18, 0x00000000, \
7481               PPC_NONE, PPC2_TM),
7482GEN_HANDLER2_E(tabortwci, "tabortwci", 0x1F, 0x0E, 0x1A, 0x00000000, \
7483               PPC_NONE, PPC2_TM),
7484GEN_HANDLER2_E(tabortdc, "tabortdc", 0x1F, 0x0E, 0x19, 0x00000000, \
7485               PPC_NONE, PPC2_TM),
7486GEN_HANDLER2_E(tabortdci, "tabortdci", 0x1F, 0x0E, 0x1B, 0x00000000, \
7487               PPC_NONE, PPC2_TM),
7488GEN_HANDLER2_E(tsr, "tsr", 0x1F, 0x0E, 0x17, 0x03DFF800, \
7489               PPC_NONE, PPC2_TM),
7490GEN_HANDLER2_E(tcheck, "tcheck", 0x1F, 0x0E, 0x16, 0x007FF800, \
7491               PPC_NONE, PPC2_TM),
7492GEN_HANDLER2_E(treclaim, "treclaim", 0x1F, 0x0E, 0x1D, 0x03E0F800, \
7493               PPC_NONE, PPC2_TM),
7494GEN_HANDLER2_E(trechkpt, "trechkpt", 0x1F, 0x0E, 0x1F, 0x03FFF800, \
7495               PPC_NONE, PPC2_TM),
7496
7497#include "translate/fp-ops.inc.c"
7498
7499#include "translate/vmx-ops.inc.c"
7500
7501#include "translate/vsx-ops.inc.c"
7502
7503#include "translate/dfp-ops.inc.c"
7504
7505#include "translate/spe-ops.inc.c"
7506};
7507
7508#include "helper_regs.h"
7509#include "translate_init.inc.c"
7510
7511/*****************************************************************************/
7512/* Misc PowerPC helpers */
7513void ppc_cpu_dump_state(CPUState *cs, FILE *f, int flags)
7514{
7515#define RGPL  4
7516#define RFPL  4
7517
7518    PowerPCCPU *cpu = POWERPC_CPU(cs);
7519    CPUPPCState *env = &cpu->env;
7520    int i;
7521
7522    qemu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
7523                 TARGET_FMT_lx " XER " TARGET_FMT_lx " CPU#%d\n",
7524                 env->nip, env->lr, env->ctr, cpu_read_xer(env),
7525                 cs->cpu_index);
7526    qemu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
7527                 TARGET_FMT_lx " iidx %d didx %d\n",
7528                 env->msr, env->spr[SPR_HID0],
7529                 env->hflags, env->immu_idx, env->dmmu_idx);
7530#if !defined(NO_TIMER_DUMP)
7531    qemu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
7532#if !defined(CONFIG_USER_ONLY)
7533                 " DECR " TARGET_FMT_lu
7534#endif
7535                 "\n",
7536                 cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7537#if !defined(CONFIG_USER_ONLY)
7538                 , cpu_ppc_load_decr(env)
7539#endif
7540        );
7541#endif
7542    for (i = 0; i < 32; i++) {
7543        if ((i & (RGPL - 1)) == 0) {
7544            qemu_fprintf(f, "GPR%02d", i);
7545        }
7546        qemu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
7547        if ((i & (RGPL - 1)) == (RGPL - 1)) {
7548            qemu_fprintf(f, "\n");
7549        }
7550    }
7551    qemu_fprintf(f, "CR ");
7552    for (i = 0; i < 8; i++)
7553        qemu_fprintf(f, "%01x", env->crf[i]);
7554    qemu_fprintf(f, "  [");
7555    for (i = 0; i < 8; i++) {
7556        char a = '-';
7557        if (env->crf[i] & 0x08) {
7558            a = 'L';
7559        } else if (env->crf[i] & 0x04) {
7560            a = 'G';
7561        } else if (env->crf[i] & 0x02) {
7562            a = 'E';
7563        }
7564        qemu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7565    }
7566    qemu_fprintf(f, " ]             RES " TARGET_FMT_lx "\n",
7567                 env->reserve_addr);
7568
7569    if (flags & CPU_DUMP_FPU) {
7570        for (i = 0; i < 32; i++) {
7571            if ((i & (RFPL - 1)) == 0) {
7572                qemu_fprintf(f, "FPR%02d", i);
7573            }
7574            qemu_fprintf(f, " %016" PRIx64, *cpu_fpr_ptr(env, i));
7575            if ((i & (RFPL - 1)) == (RFPL - 1)) {
7576                qemu_fprintf(f, "\n");
7577            }
7578        }
7579        qemu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
7580    }
7581
7582#if !defined(CONFIG_USER_ONLY)
7583    qemu_fprintf(f, " SRR0 " TARGET_FMT_lx "  SRR1 " TARGET_FMT_lx
7584                 "    PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
7585                 env->spr[SPR_SRR0], env->spr[SPR_SRR1],
7586                 env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
7587
7588    qemu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
7589                 "  SPRG2 " TARGET_FMT_lx "  SPRG3 " TARGET_FMT_lx "\n",
7590                 env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
7591                 env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
7592
7593    qemu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
7594                 "  SPRG6 " TARGET_FMT_lx "  SPRG7 " TARGET_FMT_lx "\n",
7595                 env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
7596                 env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
7597
7598#if defined(TARGET_PPC64)
7599    if (env->excp_model == POWERPC_EXCP_POWER7 ||
7600        env->excp_model == POWERPC_EXCP_POWER8 ||
7601        env->excp_model == POWERPC_EXCP_POWER9)  {
7602        qemu_fprintf(f, "HSRR0 " TARGET_FMT_lx " HSRR1 " TARGET_FMT_lx "\n",
7603                     env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
7604    }
7605#endif
7606    if (env->excp_model == POWERPC_EXCP_BOOKE) {
7607        qemu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
7608                     " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
7609                     env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
7610                     env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
7611
7612        qemu_fprintf(f, "  TCR " TARGET_FMT_lx "   TSR " TARGET_FMT_lx
7613                     "    ESR " TARGET_FMT_lx "   DEAR " TARGET_FMT_lx "\n",
7614                     env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
7615                     env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
7616
7617        qemu_fprintf(f, "  PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
7618                     "   IVPR " TARGET_FMT_lx "   EPCR " TARGET_FMT_lx "\n",
7619                     env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
7620                     env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
7621
7622        qemu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
7623                     "    EPR " TARGET_FMT_lx "\n",
7624                     env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
7625                     env->spr[SPR_BOOKE_EPR]);
7626
7627        /* FSL-specific */
7628        qemu_fprintf(f, " MCAR " TARGET_FMT_lx "  PID1 " TARGET_FMT_lx
7629                     "   PID2 " TARGET_FMT_lx "    SVR " TARGET_FMT_lx "\n",
7630                     env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
7631                     env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
7632
7633        /*
7634         * IVORs are left out as they are large and do not change often --
7635         * they can be read with "p $ivor0", "p $ivor1", etc.
7636         */
7637    }
7638
7639#if defined(TARGET_PPC64)
7640    if (env->flags & POWERPC_FLAG_CFAR) {
7641        qemu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
7642    }
7643#endif
7644
7645    if (env->spr_cb[SPR_LPCR].name) {
7646        qemu_fprintf(f, " LPCR " TARGET_FMT_lx "\n", env->spr[SPR_LPCR]);
7647    }
7648
7649    switch (env->mmu_model) {
7650    case POWERPC_MMU_32B:
7651    case POWERPC_MMU_601:
7652    case POWERPC_MMU_SOFT_6xx:
7653    case POWERPC_MMU_SOFT_74xx:
7654#if defined(TARGET_PPC64)
7655    case POWERPC_MMU_64B:
7656    case POWERPC_MMU_2_03:
7657    case POWERPC_MMU_2_06:
7658    case POWERPC_MMU_2_07:
7659    case POWERPC_MMU_3_00:
7660#endif
7661        if (env->spr_cb[SPR_SDR1].name) { /* SDR1 Exists */
7662            qemu_fprintf(f, " SDR1 " TARGET_FMT_lx " ", env->spr[SPR_SDR1]);
7663        }
7664        if (env->spr_cb[SPR_PTCR].name) { /* PTCR Exists */
7665            qemu_fprintf(f, " PTCR " TARGET_FMT_lx " ", env->spr[SPR_PTCR]);
7666        }
7667        qemu_fprintf(f, "  DAR " TARGET_FMT_lx "  DSISR " TARGET_FMT_lx "\n",
7668                     env->spr[SPR_DAR], env->spr[SPR_DSISR]);
7669        break;
7670    case POWERPC_MMU_BOOKE206:
7671        qemu_fprintf(f, " MAS0 " TARGET_FMT_lx "  MAS1 " TARGET_FMT_lx
7672                     "   MAS2 " TARGET_FMT_lx "   MAS3 " TARGET_FMT_lx "\n",
7673                     env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
7674                     env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
7675
7676        qemu_fprintf(f, " MAS4 " TARGET_FMT_lx "  MAS6 " TARGET_FMT_lx
7677                     "   MAS7 " TARGET_FMT_lx "    PID " TARGET_FMT_lx "\n",
7678                     env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
7679                     env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
7680
7681        qemu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
7682                     " TLB1CFG " TARGET_FMT_lx "\n",
7683                     env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
7684                     env->spr[SPR_BOOKE_TLB1CFG]);
7685        break;
7686    default:
7687        break;
7688    }
7689#endif
7690
7691#undef RGPL
7692#undef RFPL
7693}
7694
7695void ppc_cpu_dump_statistics(CPUState *cs, int flags)
7696{
7697#if defined(DO_PPC_STATISTICS)
7698    PowerPCCPU *cpu = POWERPC_CPU(cs);
7699    opc_handler_t **t1, **t2, **t3, *handler;
7700    int op1, op2, op3;
7701
7702    t1 = cpu->env.opcodes;
7703    for (op1 = 0; op1 < 64; op1++) {
7704        handler = t1[op1];
7705        if (is_indirect_opcode(handler)) {
7706            t2 = ind_table(handler);
7707            for (op2 = 0; op2 < 32; op2++) {
7708                handler = t2[op2];
7709                if (is_indirect_opcode(handler)) {
7710                    t3 = ind_table(handler);
7711                    for (op3 = 0; op3 < 32; op3++) {
7712                        handler = t3[op3];
7713                        if (handler->count == 0) {
7714                            continue;
7715                        }
7716                        qemu_printf("%02x %02x %02x (%02x %04d) %16s: "
7717                                    "%016" PRIx64 " %" PRId64 "\n",
7718                                    op1, op2, op3, op1, (op3 << 5) | op2,
7719                                    handler->oname,
7720                                    handler->count, handler->count);
7721                    }
7722                } else {
7723                    if (handler->count == 0) {
7724                        continue;
7725                    }
7726                    qemu_printf("%02x %02x    (%02x %04d) %16s: "
7727                                "%016" PRIx64 " %" PRId64 "\n",
7728                                op1, op2, op1, op2, handler->oname,
7729                                handler->count, handler->count);
7730                }
7731            }
7732        } else {
7733            if (handler->count == 0) {
7734                continue;
7735            }
7736            qemu_printf("%02x       (%02x     ) %16s: %016" PRIx64
7737                        " %" PRId64 "\n",
7738                        op1, op1, handler->oname,
7739                        handler->count, handler->count);
7740        }
7741    }
7742#endif
7743}
7744
7745static void ppc_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
7746{
7747    DisasContext *ctx = container_of(dcbase, DisasContext, base);
7748    CPUPPCState *env = cs->env_ptr;
7749    int bound;
7750
7751    ctx->exception = POWERPC_EXCP_NONE;
7752    ctx->spr_cb = env->spr_cb;
7753    ctx->pr = msr_pr;
7754    ctx->mem_idx = env->dmmu_idx;
7755    ctx->dr = msr_dr;
7756#if !defined(CONFIG_USER_ONLY)
7757    ctx->hv = msr_hv || !env->has_hv_mode;
7758#endif
7759    ctx->insns_flags = env->insns_flags;
7760    ctx->insns_flags2 = env->insns_flags2;
7761    ctx->access_type = -1;
7762    ctx->need_access_type = !(env->mmu_model & POWERPC_MMU_64B);
7763    ctx->le_mode = !!(env->hflags & (1 << MSR_LE));
7764    ctx->default_tcg_memop_mask = ctx->le_mode ? MO_LE : MO_BE;
7765    ctx->flags = env->flags;
7766#if defined(TARGET_PPC64)
7767    ctx->sf_mode = msr_is_64bit(env, env->msr);
7768    ctx->has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
7769#endif
7770    ctx->lazy_tlb_flush = env->mmu_model == POWERPC_MMU_32B
7771        || env->mmu_model == POWERPC_MMU_601
7772        || (env->mmu_model & POWERPC_MMU_64B);
7773
7774    ctx->fpu_enabled = !!msr_fp;
7775    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe) {
7776        ctx->spe_enabled = !!msr_spe;
7777    } else {
7778        ctx->spe_enabled = false;
7779    }
7780    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr) {
7781        ctx->altivec_enabled = !!msr_vr;
7782    } else {
7783        ctx->altivec_enabled = false;
7784    }
7785    if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
7786        ctx->vsx_enabled = !!msr_vsx;
7787    } else {
7788        ctx->vsx_enabled = false;
7789    }
7790#if defined(TARGET_PPC64)
7791    if ((env->flags & POWERPC_FLAG_TM) && msr_tm) {
7792        ctx->tm_enabled = !!msr_tm;
7793    } else {
7794        ctx->tm_enabled = false;
7795    }
7796#endif
7797    ctx->gtse = !!(env->spr[SPR_LPCR] & LPCR_GTSE);
7798    if ((env->flags & POWERPC_FLAG_SE) && msr_se) {
7799        ctx->singlestep_enabled = CPU_SINGLE_STEP;
7800    } else {
7801        ctx->singlestep_enabled = 0;
7802    }
7803    if ((env->flags & POWERPC_FLAG_BE) && msr_be) {
7804        ctx->singlestep_enabled |= CPU_BRANCH_STEP;
7805    }
7806    if ((env->flags & POWERPC_FLAG_DE) && msr_de) {
7807        ctx->singlestep_enabled = 0;
7808        target_ulong dbcr0 = env->spr[SPR_BOOKE_DBCR0];
7809        if (dbcr0 & DBCR0_ICMP) {
7810            ctx->singlestep_enabled |= CPU_SINGLE_STEP;
7811        }
7812        if (dbcr0 & DBCR0_BRT) {
7813            ctx->singlestep_enabled |= CPU_BRANCH_STEP;
7814        }
7815
7816    }
7817    if (unlikely(ctx->base.singlestep_enabled)) {
7818        ctx->singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7819    }
7820#if defined(DO_SINGLE_STEP) && 0
7821    /* Single step trace mode */
7822    msr_se = 1;
7823#endif
7824
7825    bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
7826    ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
7827}
7828
7829static void ppc_tr_tb_start(DisasContextBase *db, CPUState *cs)
7830{
7831}
7832
7833static void ppc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
7834{
7835    tcg_gen_insn_start(dcbase->pc_next);
7836}
7837
7838static bool ppc_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,
7839                                    const CPUBreakpoint *bp)
7840{
7841    DisasContext *ctx = container_of(dcbase, DisasContext, base);
7842
7843    gen_debug_exception(ctx);
7844    dcbase->is_jmp = DISAS_NORETURN;
7845    /*
7846     * The address covered by the breakpoint must be included in
7847     * [tb->pc, tb->pc + tb->size) in order to for it to be properly
7848     * cleared -- thus we increment the PC here so that the logic
7849     * setting tb->size below does the right thing.
7850     */
7851    ctx->base.pc_next += 4;
7852    return true;
7853}
7854
7855static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
7856{
7857    DisasContext *ctx = container_of(dcbase, DisasContext, base);
7858    PowerPCCPU *cpu = POWERPC_CPU(cs);
7859    CPUPPCState *env = cs->env_ptr;
7860    opc_handler_t **table, *handler;
7861
7862    LOG_DISAS("----------------\n");
7863    LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
7864              ctx->base.pc_next, ctx->mem_idx, (int)msr_ir);
7865
7866    ctx->opcode = translator_ldl_swap(env, ctx->base.pc_next,
7867                                      need_byteswap(ctx));
7868
7869    LOG_DISAS("translate opcode %08x (%02x %02x %02x %02x) (%s)\n",
7870              ctx->opcode, opc1(ctx->opcode), opc2(ctx->opcode),
7871              opc3(ctx->opcode), opc4(ctx->opcode),
7872              ctx->le_mode ? "little" : "big");
7873    ctx->base.pc_next += 4;
7874    table = cpu->opcodes;
7875    handler = table[opc1(ctx->opcode)];
7876    if (is_indirect_opcode(handler)) {
7877        table = ind_table(handler);
7878        handler = table[opc2(ctx->opcode)];
7879        if (is_indirect_opcode(handler)) {
7880            table = ind_table(handler);
7881            handler = table[opc3(ctx->opcode)];
7882            if (is_indirect_opcode(handler)) {
7883                table = ind_table(handler);
7884                handler = table[opc4(ctx->opcode)];
7885            }
7886        }
7887    }
7888    /* Is opcode *REALLY* valid ? */
7889    if (unlikely(handler->handler == &gen_invalid)) {
7890        qemu_log_mask(LOG_GUEST_ERROR, "invalid/unsupported opcode: "
7891                      "%02x - %02x - %02x - %02x (%08x) "
7892                      TARGET_FMT_lx " %d\n",
7893                      opc1(ctx->opcode), opc2(ctx->opcode),
7894                      opc3(ctx->opcode), opc4(ctx->opcode),
7895                      ctx->opcode, ctx->base.pc_next - 4, (int)msr_ir);
7896    } else {
7897        uint32_t inval;
7898
7899        if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE)
7900                     && Rc(ctx->opcode))) {
7901            inval = handler->inval2;
7902        } else {
7903            inval = handler->inval1;
7904        }
7905
7906        if (unlikely((ctx->opcode & inval) != 0)) {
7907            qemu_log_mask(LOG_GUEST_ERROR, "invalid bits: %08x for opcode: "
7908                          "%02x - %02x - %02x - %02x (%08x) "
7909                          TARGET_FMT_lx "\n", ctx->opcode & inval,
7910                          opc1(ctx->opcode), opc2(ctx->opcode),
7911                          opc3(ctx->opcode), opc4(ctx->opcode),
7912                          ctx->opcode, ctx->base.pc_next - 4);
7913            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
7914            ctx->base.is_jmp = DISAS_NORETURN;
7915            return;
7916        }
7917    }
7918    (*(handler->handler))(ctx);
7919#if defined(DO_PPC_STATISTICS)
7920    handler->count++;
7921#endif
7922    /* Check trace mode exceptions */
7923    if (unlikely(ctx->singlestep_enabled & CPU_SINGLE_STEP &&
7924                 (ctx->base.pc_next <= 0x100 || ctx->base.pc_next > 0xF00) &&
7925                 ctx->exception != POWERPC_SYSCALL &&
7926                 ctx->exception != POWERPC_EXCP_TRAP &&
7927                 ctx->exception != POWERPC_EXCP_BRANCH)) {
7928        uint32_t excp = gen_prep_dbgex(ctx);
7929        gen_exception_nip(ctx, excp, ctx->base.pc_next);
7930    }
7931
7932    if (tcg_check_temp_count()) {
7933        qemu_log("Opcode %02x %02x %02x %02x (%08x) leaked "
7934                 "temporaries\n", opc1(ctx->opcode), opc2(ctx->opcode),
7935                 opc3(ctx->opcode), opc4(ctx->opcode), ctx->opcode);
7936    }
7937
7938    ctx->base.is_jmp = ctx->exception == POWERPC_EXCP_NONE ?
7939        DISAS_NEXT : DISAS_NORETURN;
7940}
7941
7942static void ppc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
7943{
7944    DisasContext *ctx = container_of(dcbase, DisasContext, base);
7945
7946    if (ctx->exception == POWERPC_EXCP_NONE) {
7947        gen_goto_tb(ctx, 0, ctx->base.pc_next);
7948    } else if (ctx->exception != POWERPC_EXCP_BRANCH) {
7949        if (unlikely(ctx->base.singlestep_enabled)) {
7950            gen_debug_exception(ctx);
7951        }
7952        /* Generate the return instruction */
7953        tcg_gen_exit_tb(NULL, 0);
7954    }
7955}
7956
7957static void ppc_tr_disas_log(const DisasContextBase *dcbase, CPUState *cs)
7958{
7959    qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first));
7960    log_target_disas(cs, dcbase->pc_first, dcbase->tb->size);
7961}
7962
7963static const TranslatorOps ppc_tr_ops = {
7964    .init_disas_context = ppc_tr_init_disas_context,
7965    .tb_start           = ppc_tr_tb_start,
7966    .insn_start         = ppc_tr_insn_start,
7967    .breakpoint_check   = ppc_tr_breakpoint_check,
7968    .translate_insn     = ppc_tr_translate_insn,
7969    .tb_stop            = ppc_tr_tb_stop,
7970    .disas_log          = ppc_tr_disas_log,
7971};
7972
7973void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
7974{
7975    DisasContext ctx;
7976
7977    translator_loop(&ppc_tr_ops, &ctx.base, cs, tb, max_insns);
7978}
7979
7980void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb,
7981                          target_ulong *data)
7982{
7983    env->nip = data[0];
7984}
7985