cachepc-qemu

Fork of AMDESE/qemu with changes for cachepc side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-qemu
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

translate.c (274027B)


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