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

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

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

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

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

static uint16_t *gen_opc_ptr;
static uint32_t *gen_opparam_ptr;

#include "gen-op.h"
55

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static inline void gen_set_T0 (target_ulong val)
{
#if defined(TARGET_PPC64)
    if (val >> 32)
        gen_op_set_T0_64(val >> 32, val);
    else
#endif
        gen_op_set_T0(val);
}

static inline void gen_set_T1 (target_ulong val)
{
#if defined(TARGET_PPC64)
    if (val >> 32)
        gen_op_set_T1_64(val >> 32, val);
    else
#endif
        gen_op_set_T1(val);
}

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

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

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

/* Condition register moves */
GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);
GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);
GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);
GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);
119

B
bellard 已提交
120 121 122 123
/* Floating point condition and status register moves */
GEN8(gen_op_load_fpscr_T0, gen_op_load_fpscr_T0_fpscr);
GEN8(gen_op_store_T0_fpscr, gen_op_store_T0_fpscr_fpscr);
GEN8(gen_op_clear_fpscr, gen_op_clear_fpscr_fpscr);
124
static inline void gen_op_store_T0_fpscri (int n, uint8_t param)
B
bellard 已提交
125
{
126 127
    gen_op_set_T0(param);
    gen_op_store_T0_fpscr(n);
B
bellard 已提交
128 129
}

130 131 132 133 134 135 136
/* General purpose registers moves */
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);

GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
137
#if 0 // unused
138
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
139
#endif
140

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

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

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

190 191
static inline void gen_set_Rc0 (DisasContext *ctx)
{
192 193 194 195 196 197
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_cmpi_64(0);
    else
#endif
        gen_op_cmpi(0);
198 199 200
    gen_op_set_Rc0();
}

201 202 203 204 205 206 207 208 209 210
static inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_update_nip_64(nip >> 32, nip);
    else
#endif
        gen_op_update_nip(nip);
}

211
#define GEN_EXCP(ctx, excp, error)                                            \
B
bellard 已提交
212
do {                                                                          \
213
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
214
        gen_update_nip(ctx, (ctx)->nip);                                      \
215 216 217
    }                                                                         \
    gen_op_raise_exception_err((excp), (error));                              \
    ctx->exception = (excp);                                                  \
B
bellard 已提交
218 219
} while (0)

220 221 222
#define GEN_EXCP_INVAL(ctx)                                                   \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
223

224 225 226
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
227

228 229 230 231 232 233 234 235 236
#define GEN_EXCP_PRIVREG(ctx)                                                 \
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)

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

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

238
/* Stop translation */
239
static inline void GEN_STOP (DisasContext *ctx)
240
{
241
    gen_update_nip(ctx, ctx->nip);
242
    ctx->exception = POWERPC_EXCP_STOP;
243 244
}

245
/* No need to update nip here, as execution flow will change */
246
static inline void GEN_SYNC (DisasContext *ctx)
247
{
248
    ctx->exception = POWERPC_EXCP_SYNC;
249 250
}

B
bellard 已提交
251 252 253 254 255 256 257
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
static void gen_##name (DisasContext *ctx);                                   \
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
static void gen_##name (DisasContext *ctx)

typedef struct opcode_t {
    unsigned char opc1, opc2, opc3;
258 259 260 261 262
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
    unsigned char pad[5];
#else
    unsigned char pad[1];
#endif
B
bellard 已提交
263
    opc_handler_t handler;
264
    const unsigned char *oname;
B
bellard 已提交
265 266
} opcode_t;

267
/*****************************************************************************/
B
bellard 已提交
268 269
/***                           Instruction decoding                        ***/
#define EXTRACT_HELPER(name, shift, nb)                                       \
270
static inline uint32_t name (uint32_t opcode)                                 \
B
bellard 已提交
271 272 273 274 275
{                                                                             \
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
}

#define EXTRACT_SHELPER(name, shift, nb)                                      \
276
static inline int32_t name (uint32_t opcode)                                  \
B
bellard 已提交
277
{                                                                             \
278
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
B
bellard 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

/* Opcode part 1 */
EXTRACT_HELPER(opc1, 26, 6);
/* Opcode part 2 */
EXTRACT_HELPER(opc2, 1, 5);
/* Opcode part 3 */
EXTRACT_HELPER(opc3, 6, 5);
/* Update Cr0 flags */
EXTRACT_HELPER(Rc, 0, 1);
/* Destination */
EXTRACT_HELPER(rD, 21, 5);
/* Source */
EXTRACT_HELPER(rS, 21, 5);
/* First operand */
EXTRACT_HELPER(rA, 16, 5);
/* Second operand */
EXTRACT_HELPER(rB, 11, 5);
/* Third operand */
EXTRACT_HELPER(rC, 6, 5);
/***                               Get CRn                                 ***/
EXTRACT_HELPER(crfD, 23, 3);
EXTRACT_HELPER(crfS, 18, 3);
EXTRACT_HELPER(crbD, 21, 5);
EXTRACT_HELPER(crbA, 16, 5);
EXTRACT_HELPER(crbB, 11, 5);
/* SPR / TBL */
306 307 308 309 310 311 312
EXTRACT_HELPER(_SPR, 11, 10);
static inline uint32_t SPR (uint32_t opcode)
{
    uint32_t sprn = _SPR(opcode);

    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
}
B
bellard 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
/***                              Get constants                            ***/
EXTRACT_HELPER(IMM, 12, 8);
/* 16 bits signed immediate value */
EXTRACT_SHELPER(SIMM, 0, 16);
/* 16 bits unsigned immediate value */
EXTRACT_HELPER(UIMM, 0, 16);
/* Bit count */
EXTRACT_HELPER(NB, 11, 5);
/* Shift count */
EXTRACT_HELPER(SH, 11, 5);
/* Mask start */
EXTRACT_HELPER(MB, 6, 5);
/* Mask end */
EXTRACT_HELPER(ME, 1, 5);
B
bellard 已提交
327 328
/* Trap operand */
EXTRACT_HELPER(TO, 21, 5);
B
bellard 已提交
329 330 331 332

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

B
bellard 已提交
335 336 337 338
/***                            Jump target decoding                       ***/
/* Displacement */
EXTRACT_SHELPER(d, 0, 16);
/* Immediate address */
339
static inline target_ulong LI (uint32_t opcode)
B
bellard 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
{
    return (opcode >> 0) & 0x03FFFFFC;
}

static inline uint32_t BD (uint32_t opcode)
{
    return (opcode >> 0) & 0xFFFC;
}

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

/* Create a mask between <start> and <end> bits */
357
static inline target_ulong MASK (uint32_t start, uint32_t end)
B
bellard 已提交
358
{
359
    target_ulong ret;
B
bellard 已提交
360

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
#if defined(TARGET_PPC64)
    if (likely(start == 0)) {
        ret = (uint64_t)(-1ULL) << (63 - end);
    } else if (likely(end == 63)) {
        ret = (uint64_t)(-1ULL) >> start;
    }
#else
    if (likely(start == 0)) {
        ret = (uint32_t)(-1ULL) << (31  - end);
    } else if (likely(end == 31)) {
        ret = (uint32_t)(-1ULL) >> start;
    }
#endif
    else {
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
            (((target_ulong)(-1ULL) >> (end)) >> 1);
        if (unlikely(start > end))
            return ~ret;
    }
B
bellard 已提交
380 381 382 383

    return ret;
}

384 385 386 387
/*****************************************************************************/
/* PowerPC Instructions types definitions                                    */
enum {
    PPC_NONE          = 0x0000000000000000ULL,
388
    /* PowerPC base instructions set                                         */
389
    PPC_INSNS_BASE    = 0x0000000000000001ULL,
390
    /* integer operations instructions                                       */
391
#define PPC_INTEGER PPC_INSNS_BASE
392
    /* flow control instructions                                             */
393
#define PPC_FLOW    PPC_INSNS_BASE
394
    /* virtual memory instructions                                           */
395
#define PPC_MEM     PPC_INSNS_BASE
396
    /* ld/st with reservation instructions                                   */
397
#define PPC_RES     PPC_INSNS_BASE
398
    /* cache control instructions                                            */
399
#define PPC_CACHE   PPC_INSNS_BASE
400
    /* spr/msr access instructions                                           */
401
#define PPC_MISC    PPC_INSNS_BASE
402
    /* Optional floating point instructions                                  */
403 404 405 406 407 408
    PPC_FLOAT         = 0x0000000000000002ULL,
    PPC_FLOAT_FSQRT   = 0x0000000000000004ULL,
    PPC_FLOAT_FRES    = 0x0000000000000008ULL,
    PPC_FLOAT_FRSQRTE = 0x0000000000000010ULL,
    PPC_FLOAT_FSEL    = 0x0000000000000020ULL,
    PPC_FLOAT_STFIWX  = 0x0000000000000040ULL,
409
    /* external control instructions                                         */
410
    PPC_EXTERN        = 0x0000000000000080ULL,
411
    /* segment register access instructions                                  */
412
    PPC_SEGMENT       = 0x0000000000000100ULL,
413
    /* Optional cache control instruction                                    */
414
    PPC_CACHE_DCBA    = 0x0000000000000200ULL,
415
    /* Optional memory control instructions                                  */
416 417 418
    PPC_MEM_TLBIA     = 0x0000000000000400ULL,
    PPC_MEM_TLBIE     = 0x0000000000000800ULL,
    PPC_MEM_TLBSYNC   = 0x0000000000001000ULL,
419
    /* eieio & sync                                                          */
420
    PPC_MEM_SYNC      = 0x0000000000002000ULL,
421
    /* PowerPC 6xx TLB management instructions                               */
422
    PPC_6xx_TLB       = 0x0000000000004000ULL,
423
    /* Altivec support                                                       */
424
    PPC_ALTIVEC       = 0x0000000000008000ULL,
425
    /* Time base mftb instruction                                            */
426
    PPC_MFTB          = 0x0000000000010000ULL,
427
    /* Embedded PowerPC dedicated instructions                               */
428
    PPC_EMB_COMMON    = 0x0000000000020000ULL,
429
    /* PowerPC 40x exception model                                           */
430
    PPC_40x_EXCP      = 0x0000000000040000ULL,
431
    /* PowerPC 40x TLB management instructions                               */
432
    PPC_40x_TLB       = 0x0000000000080000ULL,
433
    /* PowerPC 405 Mac instructions                                          */
434
    PPC_405_MAC       = 0x0000000000100000ULL,
435
    /* PowerPC 440 specific instructions                                     */
436
    PPC_440_SPEC      = 0x0000000000200000ULL,
437
    /* Power-to-PowerPC bridge (601)                                         */
438
    PPC_POWER_BR      = 0x0000000000400000ULL,
439
    /* PowerPC 602 specific                                                  */
440
    PPC_602_SPEC      = 0x0000000000800000ULL,
441 442
    /* Deprecated instructions                                               */
    /* Original POWER instruction set                                        */
443
    PPC_POWER         = 0x0000000001000000ULL,
444
    /* POWER2 instruction set extension                                      */
445
    PPC_POWER2        = 0x0000000002000000ULL,
446
    /* Power RTC support                                                     */
447
    PPC_POWER_RTC     = 0x0000000004000000ULL,
448
    /* 64 bits PowerPC instruction set                                       */
449
    PPC_64B           = 0x0000000008000000ULL,
450
    /* 64 bits hypervisor extensions                                         */
451
    PPC_64H           = 0x0000000010000000ULL,
452 453 454
    /* segment register access instructions for PowerPC 64 "bridge"          */
    PPC_SEGMENT_64B   = 0x0000000020000000ULL,
    /* BookE (embedded) PowerPC specification                                */
455
    PPC_BOOKE         = 0x0000000040000000ULL,
456
    /* eieio                                                                 */
457
    PPC_MEM_EIEIO     = 0x0000000080000000ULL,
458
    /* e500 vector instructions                                              */
459
    PPC_E500_VECTOR   = 0x0000000100000000ULL,
460
    /* PowerPC 4xx dedicated instructions                                    */
461
    PPC_4xx_COMMON    = 0x0000000200000000ULL,
462
    /* PowerPC 2.03 specification extensions                                 */
463
    PPC_203           = 0x0000000400000000ULL,
464
    /* PowerPC 2.03 SPE extension                                            */
465
    PPC_SPE           = 0x0000000800000000ULL,
466
    /* PowerPC 2.03 SPE floating-point extension                             */
467
    PPC_SPEFPU        = 0x0000001000000000ULL,
468
    /* SLB management                                                        */
469
    PPC_SLBI          = 0x0000002000000000ULL,
470
    /* PowerPC 40x ibct instructions                                         */
471
    PPC_40x_ICBT      = 0x0000004000000000ULL,
472
    /* PowerPC 74xx TLB management instructions                              */
473
    PPC_74xx_TLB      = 0x0000008000000000ULL,
474
    /* More BookE (embedded) instructions...                                 */
475
    PPC_BOOKE_EXT     = 0x0000010000000000ULL,
476
    /* rfmci is not implemented in all BookE PowerPC                         */
477
    PPC_RFMCI         = 0x0000020000000000ULL,
478
    /* user-mode DCR access, implemented in PowerPC 460                      */
479
    PPC_DCRUX         = 0x0000040000000000ULL,
480
    /* New floating-point extensions (PowerPC 2.0x)                          */
481
    PPC_FLOAT_EXT     = 0x0000080000000000ULL,
482
    /* New wait instruction (PowerPC 2.0x)                                   */
483
    PPC_WAIT          = 0x0000100000000000ULL,
484
    /* New 64 bits extensions (PowerPC 2.0x)                                 */
485
    PPC_64BX          = 0x0000200000000000ULL,
486
    /* dcbz instruction with fixed cache line size                           */
487
    PPC_CACHE_DCBZ    = 0x0000400000000000ULL,
488
    /* dcbz instruction with tunable cache line size                         */
489
    PPC_CACHE_DCBZT   = 0x0000800000000000ULL,
490 491 492 493
};

/*****************************************************************************/
/* PowerPC instructions table                                                */
494 495 496 497 498
#if HOST_LONG_BITS == 64
#define OPC_ALIGN 8
#else
#define OPC_ALIGN 4
#endif
B
bellard 已提交
499
#if defined(__APPLE__)
500
#define OPCODES_SECTION                                                       \
501
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
502
#else
503
#define OPCODES_SECTION                                                       \
504
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
B
bellard 已提交
505 506
#endif

507
#if defined(DO_PPC_STATISTICS)
B
bellard 已提交
508
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
509
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
510 511 512
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
513
    .pad  = { 0, },                                                           \
B
bellard 已提交
514 515
    .handler = {                                                              \
        .inval   = invl,                                                      \
516
        .type = _typ,                                                         \
B
bellard 已提交
517
        .handler = &gen_##name,                                               \
518
        .oname = stringify(name),                                             \
B
bellard 已提交
519
    },                                                                        \
520
    .oname = stringify(name),                                                 \
B
bellard 已提交
521
}
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
#else
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
OPCODES_SECTION opcode_t opc_##name = {                                       \
    .opc1 = op1,                                                              \
    .opc2 = op2,                                                              \
    .opc3 = op3,                                                              \
    .pad  = { 0, },                                                           \
    .handler = {                                                              \
        .inval   = invl,                                                      \
        .type = _typ,                                                         \
        .handler = &gen_##name,                                               \
    },                                                                        \
    .oname = stringify(name),                                                 \
}
#endif
B
bellard 已提交
537 538

#define GEN_OPCODE_MARK(name)                                                 \
539
OPCODES_SECTION opcode_t opc_##name = {                                       \
B
bellard 已提交
540 541 542
    .opc1 = 0xFF,                                                             \
    .opc2 = 0xFF,                                                             \
    .opc3 = 0xFF,                                                             \
543
    .pad  = { 0, },                                                           \
B
bellard 已提交
544 545
    .handler = {                                                              \
        .inval   = 0x00000000,                                                \
546
        .type = 0x00,                                                         \
B
bellard 已提交
547 548
        .handler = NULL,                                                      \
    },                                                                        \
549
    .oname = stringify(name),                                                 \
B
bellard 已提交
550 551 552 553 554 555
}

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

/* Invalid instruction */
556 557
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
{
558
    GEN_EXCP_INVAL(ctx);
559 560
}

B
bellard 已提交
561 562
static opc_handler_t invalid_handler = {
    .inval   = 0xFFFFFFFF,
563
    .type    = PPC_NONE,
B
bellard 已提交
564 565 566 567
    .handler = gen_invalid,
};

/***                           Integer arithmetic                          ***/
568 569
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval, type)                 \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
570 571 572 573 574
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
575 576
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
577 578
}

579 580
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
B
bellard 已提交
581 582 583 584 585
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
586 587
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
588 589
}

590 591
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
592 593 594 595
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
596 597
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
598
}
599 600
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
B
bellard 已提交
601 602 603 604
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
605 606
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
607 608 609
}

/* Two operands arithmetic functions */
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
#define GEN_INT_ARITH2(name, opc1, opc2, opc3, type)                          \
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000, type)                    \
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)

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

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

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

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

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

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

/* Two operands arithmetic functions with no overflow allowed */
683 684
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
B
bellard 已提交
685 686

/* One operand arithmetic functions */
687 688 689 690 691 692 693 694
#define GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                       \
__GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                             \
__GEN_INT_ARITH1_O_64(name##o, opc1, opc2, opc3 | 0x10, type)
#else
#define GEN_INT_ARITH2_64 GEN_INT_ARITH2
#define GEN_INT_ARITHN_64 GEN_INT_ARITHN
#define GEN_INT_ARITH1_64 GEN_INT_ARITH1
#endif
B
bellard 已提交
695 696

/* add    add.    addo    addo.    */
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
static inline void gen_op_addo (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
#define gen_op_add_64 gen_op_add
static inline void gen_op_addo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (add,    0x1F, 0x0A, 0x08, PPC_INTEGER);
B
bellard 已提交
713
/* addc   addc.   addco   addco.   */
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
static inline void gen_op_addc (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
}
static inline void gen_op_addco (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addc_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc_64();
}
static inline void gen_op_addco_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add();
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (addc,   0x1F, 0x0A, 0x00, PPC_INTEGER);
B
bellard 已提交
743
/* adde   adde.   addeo   addeo.   */
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
static inline void gen_op_addeo (void)
{
    gen_op_move_T2_T0();
    gen_op_adde();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addeo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_adde_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH2_64 (adde,   0x1F, 0x0A, 0x04, PPC_INTEGER);
B
bellard 已提交
759
/* addme  addme.  addmeo  addmeo.  */
760 761 762 763 764 765 766 767 768 769 770 771 772
static inline void gen_op_addme (void)
{
    gen_op_move_T1_T0();
    gen_op_add_me();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addme_64 (void)
{
    gen_op_move_T1_T0();
    gen_op_add_me_64();
}
#endif
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
B
bellard 已提交
773
/* addze  addze.  addzeo  addzeo.  */
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
static inline void gen_op_addze (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
}
static inline void gen_op_addzeo (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc();
    gen_op_check_addo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_addze_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc_64();
}
static inline void gen_op_addzeo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_add_ze();
    gen_op_check_addc_64();
    gen_op_check_addo_64();
}
#endif
GEN_INT_ARITH1_64 (addze,  0x1F, 0x0A, 0x06, PPC_INTEGER);
B
bellard 已提交
803
/* divw   divw.   divwo   divwo.   */
804
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
B
bellard 已提交
805
/* divwu  divwu.  divwuo  divwuo.  */
806
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
B
bellard 已提交
807
/* mulhw  mulhw.                   */
808
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
B
bellard 已提交
809
/* mulhwu mulhwu.                  */
810
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
B
bellard 已提交
811
/* mullw  mullw.  mullwo  mullwo.  */
812
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
B
bellard 已提交
813
/* neg    neg.    nego    nego.    */
814
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
B
bellard 已提交
815
/* subf   subf.   subfo   subfo.   */
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
static inline void gen_op_subfo (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
#define gen_op_subf_64 gen_op_subf
static inline void gen_op_subfo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfo_64();
}
#endif
GEN_INT_ARITH2_64 (subf,   0x1F, 0x08, 0x01, PPC_INTEGER);
B
bellard 已提交
832
/* subfc  subfc.  subfco  subfco.  */
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
static inline void gen_op_subfc (void)
{
    gen_op_subf();
    gen_op_check_subfc();
}
static inline void gen_op_subfco (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfc();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
static inline void gen_op_subfc_64 (void)
{
    gen_op_subf();
    gen_op_check_subfc_64();
}
static inline void gen_op_subfco_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_subf();
    gen_op_check_subfc_64();
    gen_op_check_subfo_64();
}
#endif
GEN_INT_ARITH2_64 (subfc,  0x1F, 0x08, 0x00, PPC_INTEGER);
B
bellard 已提交
860
/* subfe  subfe.  subfeo  subfeo.  */
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
static inline void gen_op_subfeo (void)
{
    gen_op_move_T2_T0();
    gen_op_subfe();
    gen_op_check_subfo();
}
#if defined(TARGET_PPC64)
#define gen_op_subfe_64 gen_op_subfe
static inline void gen_op_subfeo_64 (void)
{
    gen_op_move_T2_T0();
    gen_op_subfe_64();
    gen_op_check_subfo_64();
}
#endif
GEN_INT_ARITH2_64 (subfe,  0x1F, 0x08, 0x04, PPC_INTEGER);
B
bellard 已提交
877
/* subfme subfme. subfmeo subfmeo. */
878
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
B
bellard 已提交
879
/* subfze subfze. subfzeo subfzeo. */
880
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
B
bellard 已提交
881 882 883
/* addi */
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
884
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
885 886

    if (rA(ctx->opcode) == 0) {
887
        /* li case */
888
        gen_set_T0(simm);
B
bellard 已提交
889 890
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
891 892
        if (likely(simm != 0))
            gen_op_addi(simm);
B
bellard 已提交
893 894 895 896 897 898
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic */
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
899 900
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
901
    gen_op_load_gpr_T0(rA(ctx->opcode));
902 903 904 905 906 907 908 909 910
    if (likely(simm != 0)) {
        gen_op_move_T2_T0();
        gen_op_addi(simm);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
911 912
    } else {
        gen_op_clear_xer_ca();
913
    }
B
bellard 已提交
914 915 916 917 918
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* addic. */
GEN_HANDLER(addic_, 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
919 920
    target_long simm = SIMM(ctx->opcode);

B
bellard 已提交
921
    gen_op_load_gpr_T0(rA(ctx->opcode));
922 923 924 925 926 927 928 929 930
    if (likely(simm != 0)) {
        gen_op_move_T2_T0();
        gen_op_addi(simm);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_check_addc_64();
        else
#endif
            gen_op_check_addc();
J
j_mayer 已提交
931 932
    } else {
        gen_op_clear_xer_ca();
933
    }
B
bellard 已提交
934
    gen_op_store_T0_gpr(rD(ctx->opcode));
935
    gen_set_Rc0(ctx);
B
bellard 已提交
936 937 938 939
}
/* addis */
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
940
    target_long simm = SIMM(ctx->opcode);
B
bellard 已提交
941 942

    if (rA(ctx->opcode) == 0) {
943
        /* lis case */
944
        gen_set_T0(simm << 16);
B
bellard 已提交
945 946
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
947 948
        if (likely(simm != 0))
            gen_op_addi(simm << 16);
B
bellard 已提交
949 950 951 952 953 954 955 956 957 958 959 960 961 962
    }
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* mulli */
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_mulli(SIMM(ctx->opcode));
    gen_op_store_T0_gpr(rD(ctx->opcode));
}
/* subfic */
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
963 964 965 966 967 968
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_subfic_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_subfic(SIMM(ctx->opcode));
B
bellard 已提交
969 970 971
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

972 973
#if defined(TARGET_PPC64)
/* mulhd  mulhd.                   */
974
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
975
/* mulhdu mulhdu.                  */
976
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
977
/* mulld  mulld.  mulldo  mulldo.  */
978
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
979
/* divd   divd.   divdo   divdo.   */
980
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
981
/* divdu  divdu.  divduo  divduo.  */
982
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
983 984
#endif

B
bellard 已提交
985
/***                           Integer comparison                          ***/
986 987 988 989 990 991
#if defined(TARGET_PPC64)
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
992
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))                           \
993 994 995 996 997 998 999 1000
        gen_op_##name##_64();                                                 \
    else                                                                      \
        gen_op_##name();                                                      \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}
#else
#define GEN_CMP(name, opc, type)                                              \
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
B
bellard 已提交
1001 1002 1003 1004 1005 1006
{                                                                             \
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}
1007
#endif
B
bellard 已提交
1008 1009

/* cmp */
1010
GEN_CMP(cmp, 0x00, PPC_INTEGER);
B
bellard 已提交
1011 1012 1013 1014
/* cmpi */
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1015
#if defined(TARGET_PPC64)
1016
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1017 1018 1019 1020
        gen_op_cmpi_64(SIMM(ctx->opcode));
    else
#endif
        gen_op_cmpi(SIMM(ctx->opcode));
B
bellard 已提交
1021 1022 1023
    gen_op_store_T0_crf(crfD(ctx->opcode));
}
/* cmpl */
1024
GEN_CMP(cmpl, 0x01, PPC_INTEGER);
B
bellard 已提交
1025 1026 1027 1028
/* cmpli */
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
1029
#if defined(TARGET_PPC64)
1030
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1031 1032 1033 1034
        gen_op_cmpli_64(UIMM(ctx->opcode));
    else
#endif
        gen_op_cmpli(UIMM(ctx->opcode));
B
bellard 已提交
1035 1036 1037
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
/* isel (PowerPC 2.03 specification) */
GEN_HANDLER(isel, 0x1F, 0x0F, 0x00, 0x00000001, PPC_203)
{
    uint32_t bi = rC(ctx->opcode);
    uint32_t mask;

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

B
bellard 已提交
1057
/***                            Integer logical                            ***/
1058 1059
#define __GEN_LOGICAL2(name, opc2, opc3, type)                                \
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, type)                         \
B
bellard 已提交
1060 1061 1062 1063 1064
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1065 1066
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1067
}
1068 1069
#define GEN_LOGICAL2(name, opc, type)                                         \
__GEN_LOGICAL2(name, 0x1C, opc, type)
B
bellard 已提交
1070

1071 1072
#define GEN_LOGICAL1(name, opc, type)                                         \
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
B
bellard 已提交
1073 1074 1075 1076
{                                                                             \
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1077 1078
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
        gen_set_Rc0(ctx);                                                     \
B
bellard 已提交
1079 1080 1081
}

/* and & and. */
1082
GEN_LOGICAL2(and, 0x00, PPC_INTEGER);
B
bellard 已提交
1083
/* andc & andc. */
1084
GEN_LOGICAL2(andc, 0x01, PPC_INTEGER);
B
bellard 已提交
1085 1086 1087 1088
/* andi. */
GEN_HANDLER(andi_, 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1089
    gen_op_andi_T0(UIMM(ctx->opcode));
B
bellard 已提交
1090
    gen_op_store_T0_gpr(rA(ctx->opcode));
1091
    gen_set_Rc0(ctx);
B
bellard 已提交
1092 1093 1094 1095 1096
}
/* andis. */
GEN_HANDLER(andis_, 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
1097
    gen_op_andi_T0(UIMM(ctx->opcode) << 16);
B
bellard 已提交
1098
    gen_op_store_T0_gpr(rA(ctx->opcode));
1099
    gen_set_Rc0(ctx);
B
bellard 已提交
1100 1101 1102
}

/* cntlzw */
1103
GEN_LOGICAL1(cntlzw, 0x00, PPC_INTEGER);
B
bellard 已提交
1104
/* eqv & eqv. */
1105
GEN_LOGICAL2(eqv, 0x08, PPC_INTEGER);
B
bellard 已提交
1106
/* extsb & extsb. */
1107
GEN_LOGICAL1(extsb, 0x1D, PPC_INTEGER);
B
bellard 已提交
1108
/* extsh & extsh. */
1109
GEN_LOGICAL1(extsh, 0x1C, PPC_INTEGER);
B
bellard 已提交
1110
/* nand & nand. */
1111
GEN_LOGICAL2(nand, 0x0E, PPC_INTEGER);
B
bellard 已提交
1112
/* nor & nor. */
1113
GEN_LOGICAL2(nor, 0x03, PPC_INTEGER);
1114

B
bellard 已提交
1115
/* or & or. */
1116 1117
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
{
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
    int rs, ra, rb;

    rs = rS(ctx->opcode);
    ra = rA(ctx->opcode);
    rb = rB(ctx->opcode);
    /* Optimisation for mr. ri case */
    if (rs != ra || rs != rb) {
        gen_op_load_gpr_T0(rs);
        if (rs != rb) {
            gen_op_load_gpr_T1(rb);
            gen_op_or();
        }
        gen_op_store_T0_gpr(ra);
        if (unlikely(Rc(ctx->opcode) != 0))
            gen_set_Rc0(ctx);
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
        gen_op_load_gpr_T0(rs);
        gen_set_Rc0(ctx);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
#if defined(TARGET_PPC64)
    } else {
        switch (rs) {
        case 1:
            /* Set process priority to low */
            gen_op_store_pri(2);
            break;
        case 6:
            /* Set process priority to medium-low */
            gen_op_store_pri(3);
            break;
        case 2:
            /* Set process priority to normal */
            gen_op_store_pri(4);
            break;
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
#if !defined(CONFIG_USER_ONLY)
        case 31:
            if (ctx->supervisor > 0) {
                /* Set process priority to very low */
                gen_op_store_pri(1);
            }
            break;
        case 5:
            if (ctx->supervisor > 0) {
                /* Set process priority to medium-hight */
                gen_op_store_pri(5);
            }
            break;
        case 3:
            if (ctx->supervisor > 0) {
                /* Set process priority to high */
                gen_op_store_pri(6);
            }
            break;
#if defined(TARGET_PPC64H)
        case 7:
            if (ctx->supervisor > 1) {
                /* Set process priority to very high */
                gen_op_store_pri(7);
            }
            break;
#endif
#endif
1179 1180 1181 1182 1183
        default:
            /* nop */
            break;
        }
#endif
1184 1185 1186
    }
}

B
bellard 已提交
1187
/* orc & orc. */
1188
GEN_LOGICAL2(orc, 0x0C, PPC_INTEGER);
B
bellard 已提交
1189
/* xor & xor. */
1190 1191 1192 1193 1194 1195 1196 1197
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    /* Optimisation for "set to zero" case */
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_xor();
    } else {
1198
        gen_op_reset_T0();
1199 1200
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1201 1202
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1203
}
B
bellard 已提交
1204 1205 1206
/* ori */
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1207
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1208

1209 1210
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
1211
        /* XXX: should handle special NOPs for POWER series */
1212
        return;
1213 1214 1215
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1216
        gen_op_ori(uimm);
1217
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1218 1219 1220 1221
}
/* oris */
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1222
    target_ulong uimm = UIMM(ctx->opcode);
B
bellard 已提交
1223

1224 1225 1226
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
1227 1228 1229
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(uimm != 0))
B
bellard 已提交
1230
        gen_op_ori(uimm << 16);
1231
    gen_op_store_T0_gpr(rA(ctx->opcode));
B
bellard 已提交
1232 1233 1234 1235
}
/* xori */
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1236
    target_ulong uimm = UIMM(ctx->opcode);
1237 1238 1239 1240 1241

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1242
    gen_op_load_gpr_T0(rS(ctx->opcode));
1243 1244
    if (likely(uimm != 0))
        gen_op_xori(uimm);
B
bellard 已提交
1245 1246 1247 1248 1249 1250
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

/* xoris */
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1251
    target_ulong uimm = UIMM(ctx->opcode);
1252 1253 1254 1255 1256

    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
        /* NOP */
        return;
    }
B
bellard 已提交
1257
    gen_op_load_gpr_T0(rS(ctx->opcode));
1258 1259
    if (likely(uimm != 0))
        gen_op_xori(uimm << 16);
B
bellard 已提交
1260 1261 1262
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
/* popcntb : PowerPC 2.03 specification */
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_203)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_popcntb_64();
    else
#endif
        gen_op_popcntb();
    gen_op_store_T0_gpr(rA(ctx->opcode));
}

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

B
bellard 已提交
1283 1284 1285 1286
/***                             Integer rotate                            ***/
/* rlwimi & rlwimi. */
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
1287 1288
    target_ulong mask;
    uint32_t mb, me, sh;
B
bellard 已提交
1289 1290 1291

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
    sh = SH(ctx->opcode);
    if (likely(sh == 0)) {
        if (likely(mb == 0 && me == 31)) {
            gen_op_load_gpr_T0(rS(ctx->opcode));
            goto do_store;
        } else if (likely(mb == 31 && me == 0)) {
            gen_op_load_gpr_T0(rA(ctx->opcode));
            goto do_store;
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
        gen_op_load_gpr_T1(rA(ctx->opcode));
        goto do_mask;
    }
B
bellard 已提交
1305
    gen_op_load_gpr_T0(rS(ctx->opcode));
B
bellard 已提交
1306
    gen_op_load_gpr_T1(rA(ctx->opcode));
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
    gen_op_rotli32_T0(SH(ctx->opcode));
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
    mask = MASK(mb, me);
    gen_op_andi_T0(mask);
    gen_op_andi_T1(~mask);
    gen_op_or();
 do_store:
B
bellard 已提交
1318
    gen_op_store_T0_gpr(rA(ctx->opcode));
1319 1320
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1321 1322 1323 1324 1325
}
/* rlwinm & rlwinm. */
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me, sh;
1326

B
bellard 已提交
1327 1328 1329 1330
    sh = SH(ctx->opcode);
    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 31)) {
            gen_op_rotli32_T0(sh);
            goto do_store;
        } else if (likely(me == (31 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
B
bellard 已提交
1341
        }
1342 1343 1344 1345
    } else if (likely(me == 31)) {
        if (likely(sh == (32 - mb))) {
            gen_op_srli_T0(mb);
            goto do_store;
B
bellard 已提交
1346 1347
        }
    }
1348 1349 1350 1351 1352 1353 1354 1355
    gen_op_rotli32_T0(sh);
 do_mask:
#if defined(TARGET_PPC64)
    mb += 32;
    me += 32;
#endif
    gen_op_andi_T0(MASK(mb, me));
 do_store:
B
bellard 已提交
1356
    gen_op_store_T0_gpr(rA(ctx->opcode));
1357 1358
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
}
/* rlwnm & rlwnm. */
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
    uint32_t mb, me;

    mb = MB(ctx->opcode);
    me = ME(ctx->opcode);
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
1369 1370 1371 1372 1373 1374 1375
    gen_op_rotl32_T0_T1();
    if (unlikely(mb != 0 || me != 31)) {
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
        gen_op_andi_T0(MASK(mb, me));
B
bellard 已提交
1376 1377
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
1378 1379
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1380 1381
}

1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
#if defined(TARGET_PPC64)
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
GEN_HANDLER(name##0, opc1, opc2, 0xFF, 0x00000000, PPC_64B)                   \
{                                                                             \
    gen_##name(ctx, 0);                                                       \
}                                                                             \
GEN_HANDLER(name##1, opc1, opc2 | 0x10, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 1);                                                       \
}
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
GEN_HANDLER(name##0, opc1, opc2, 0xFF, 0x00000000, PPC_64B)                   \
{                                                                             \
    gen_##name(ctx, 0, 0);                                                    \
}                                                                             \
GEN_HANDLER(name##1, opc1, opc2 | 0x01, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 0, 1);                                                    \
}                                                                             \
GEN_HANDLER(name##2, opc1, opc2 | 0x10, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 1, 0);                                                    \
}                                                                             \
GEN_HANDLER(name##3, opc1, opc2 | 0x11, 0xFF, 0x00000000, PPC_64B)            \
{                                                                             \
    gen_##name(ctx, 1, 1);                                                    \
}
J
j_mayer 已提交
1409

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
static inline void gen_andi_T0_64 (DisasContext *ctx, uint64_t mask)
{
    if (mask >> 32)
        gen_op_andi_T0_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T0(mask);
}

static inline void gen_andi_T1_64 (DisasContext *ctx, uint64_t mask)
{
    if (mask >> 32)
        gen_op_andi_T1_64(mask >> 32, mask & 0xFFFFFFFF);
    else
        gen_op_andi_T1(mask);
}

J
j_mayer 已提交
1426 1427 1428 1429 1430 1431 1432 1433 1434
static inline void gen_rldinm (DisasContext *ctx, uint32_t mb, uint32_t me,
                               uint32_t sh)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    if (likely(sh == 0)) {
        goto do_mask;
    }
    if (likely(mb == 0)) {
        if (likely(me == 63)) {
1435
            gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1436 1437 1438 1439 1440 1441 1442
            goto do_store;
        } else if (likely(me == (63 - sh))) {
            gen_op_sli_T0(sh);
            goto do_store;
        }
    } else if (likely(me == 63)) {
        if (likely(sh == (64 - mb))) {
1443
            gen_op_srli_T0_64(mb);
J
j_mayer 已提交
1444 1445 1446 1447 1448
            goto do_store;
        }
    }
    gen_op_rotli64_T0(sh);
 do_mask:
1449
    gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1450 1451 1452 1453 1454
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
1455 1456 1457
/* rldicl - rldicl. */
static inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
{
J
j_mayer 已提交
1458
    uint32_t sh, mb;
1459

J
j_mayer 已提交
1460 1461
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1462
    gen_rldinm(ctx, mb, 63, sh);
1463
}
J
j_mayer 已提交
1464
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1465 1466 1467
/* rldicr - rldicr. */
static inline void gen_rldicr (DisasContext *ctx, int men, int shn)
{
J
j_mayer 已提交
1468
    uint32_t sh, me;
1469

J
j_mayer 已提交
1470 1471
    sh = SH(ctx->opcode) | (shn << 5);
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1472
    gen_rldinm(ctx, 0, me, sh);
1473
}
J
j_mayer 已提交
1474
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1475 1476 1477
/* rldic - rldic. */
static inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
{
J
j_mayer 已提交
1478
    uint32_t sh, mb;
1479

J
j_mayer 已提交
1480 1481
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    gen_rldinm(ctx, mb, 63 - sh, sh);
}
GEN_PPC64_R4(rldic, 0x1E, 0x04);

static inline void gen_rldnm (DisasContext *ctx, uint32_t mb, uint32_t me)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_rotl64_T0_T1();
    if (unlikely(mb != 0 || me != 63)) {
1492
        gen_andi_T0_64(ctx, MASK(mb, me));
J
j_mayer 已提交
1493 1494 1495 1496
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1497
}
J
j_mayer 已提交
1498

1499 1500 1501
/* rldcl - rldcl. */
static inline void gen_rldcl (DisasContext *ctx, int mbn)
{
J
j_mayer 已提交
1502
    uint32_t mb;
1503

J
j_mayer 已提交
1504
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1505
    gen_rldnm(ctx, mb, 63);
1506
}
1507
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1508 1509 1510
/* rldcr - rldcr. */
static inline void gen_rldcr (DisasContext *ctx, int men)
{
J
j_mayer 已提交
1511
    uint32_t me;
1512

J
j_mayer 已提交
1513
    me = MB(ctx->opcode) | (men << 5);
J
j_mayer 已提交
1514
    gen_rldnm(ctx, 0, me);
1515
}
1516
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1517 1518 1519
/* rldimi - rldimi. */
static inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
{
J
j_mayer 已提交
1520 1521
    uint64_t mask;
    uint32_t sh, mb;
1522

J
j_mayer 已提交
1523 1524
    sh = SH(ctx->opcode) | (shn << 5);
    mb = MB(ctx->opcode) | (mbn << 5);
J
j_mayer 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    if (likely(sh == 0)) {
        if (likely(mb == 0)) {
            gen_op_load_gpr_T0(rS(ctx->opcode));
            goto do_store;
        } else if (likely(mb == 63)) {
            gen_op_load_gpr_T0(rA(ctx->opcode));
            goto do_store;
        }
        gen_op_load_gpr_T0(rS(ctx->opcode));
        gen_op_load_gpr_T1(rA(ctx->opcode));
        goto do_mask;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rA(ctx->opcode));
1539
    gen_op_rotli64_T0(sh);
J
j_mayer 已提交
1540 1541
 do_mask:
    mask = MASK(mb, 63 - sh);
1542 1543
    gen_andi_T0_64(ctx, mask);
    gen_andi_T1_64(ctx, ~mask);
J
j_mayer 已提交
1544 1545 1546 1547 1548
    gen_op_or();
 do_store:
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
1549
}
1550
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1551 1552
#endif

B
bellard 已提交
1553 1554
/***                             Integer shift                             ***/
/* slw & slw. */
1555
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
B
bellard 已提交
1556
/* sraw & sraw. */
1557
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
B
bellard 已提交
1558 1559 1560
/* srawi & srawi. */
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
{
1561
    int mb, me;
B
bellard 已提交
1562
    gen_op_load_gpr_T0(rS(ctx->opcode));
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
    if (SH(ctx->opcode) != 0) {
        gen_op_move_T1_T0();
        mb = 32 - SH(ctx->opcode);
        me = 31;
#if defined(TARGET_PPC64)
        mb += 32;
        me += 32;
#endif
        gen_op_srawi(SH(ctx->opcode), MASK(mb, me));
    }
B
bellard 已提交
1573
    gen_op_store_T0_gpr(rA(ctx->opcode));
1574 1575
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
B
bellard 已提交
1576 1577
}
/* srw & srw. */
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
__GEN_LOGICAL2(srw, 0x18, 0x10, PPC_INTEGER);

#if defined(TARGET_PPC64)
/* sld & sld. */
__GEN_LOGICAL2(sld, 0x1B, 0x00, PPC_64B);
/* srad & srad. */
__GEN_LOGICAL2(srad, 0x1A, 0x18, PPC_64B);
/* sradi & sradi. */
static inline void gen_sradi (DisasContext *ctx, int n)
{
    uint64_t mask;
    int sh, mb, me;

    gen_op_load_gpr_T0(rS(ctx->opcode));
    sh = SH(ctx->opcode) + (n << 5);
    if (sh != 0) {
        gen_op_move_T1_T0();
        mb = 64 - SH(ctx->opcode);
        me = 63;
        mask = MASK(mb, me);
        gen_op_sradi(sh, mask >> 32, mask);
    }
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}
GEN_HANDLER(sradi0, 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
{
    gen_sradi(ctx, 0);
}
GEN_HANDLER(sradi1, 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
{
    gen_sradi(ctx, 1);
}
/* srd & srd. */
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
#endif
B
bellard 已提交
1615 1616

/***                       Floating-Point arithmetic                       ***/
1617 1618
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, type)                     \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1619
{                                                                             \
1620
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1621
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1622 1623
        return;                                                               \
    }                                                                         \
1624 1625 1626 1627
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
1628 1629 1630 1631
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1632
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1633
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1634 1635 1636
        gen_op_set_Rc1();                                                     \
}

1637 1638 1639
#define GEN_FLOAT_ACB(name, op2, type)                                        \
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, type);                               \
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, type);
1640

1641
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat)                     \
1642 1643
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
1644
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1645
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1646 1647
        return;                                                               \
    }                                                                         \
1648 1649 1650
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
1651 1652 1653 1654
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1655
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1656
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1657 1658 1659
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AB(name, op2, inval)                                        \
1660 1661
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1);
1662

1663
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat)                     \
1664 1665
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
{                                                                             \
1666
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1667
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1668 1669
        return;                                                               \
    }                                                                         \
1670 1671 1672
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
1673 1674 1675 1676
    gen_op_f##op();                                                           \
    if (isfloat) {                                                            \
        gen_op_frsp();                                                        \
    }                                                                         \
1677
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1678
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1679 1680 1681
        gen_op_set_Rc1();                                                     \
}
#define GEN_FLOAT_AC(name, op2, inval)                                        \
1682 1683
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0);                               \
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1);
1684

1685 1686
#define GEN_FLOAT_B(name, op2, op3, type)                                     \
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
1687
{                                                                             \
1688
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1689
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1690 1691
        return;                                                               \
    }                                                                         \
1692 1693 1694 1695
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1696
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1697
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
1698 1699
}

1700 1701
#define GEN_FLOAT_BS(name, op1, op2, type)                                    \
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
1702
{                                                                             \
1703
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1704
        GEN_EXCP_NO_FP(ctx);                                                  \
B
bellard 已提交
1705 1706
        return;                                                               \
    }                                                                         \
1707 1708 1709 1710
    gen_op_reset_scrfx();                                                     \
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
    gen_op_f##name();                                                         \
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1711
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1712
        gen_op_set_Rc1();                                                     \
B
bellard 已提交
1713 1714
}

1715 1716
/* fadd - fadds */
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
1717
/* fdiv - fdivs */
1718
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
1719
/* fmul - fmuls */
1720
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
B
bellard 已提交
1721

1722 1723 1724
/* fre */
GEN_FLOAT_BS(re, 0x3F, 0x18, PPC_FLOAT_EXT);

1725 1726
/* fres */
GEN_FLOAT_BS(res, 0x3B, 0x18, PPC_FLOAT_FRES);
B
bellard 已提交
1727

1728 1729
/* frsqrte */
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, PPC_FLOAT_FRSQRTE);
B
bellard 已提交
1730

1731 1732
/* fsel */
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, PPC_FLOAT_FSEL);
1733
/* fsub - fsubs */
1734
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
B
bellard 已提交
1735 1736
/* Optional: */
/* fsqrt */
1737
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1738
{
1739
    if (unlikely(!ctx->fpu_enabled)) {
1740
        GEN_EXCP_NO_FP(ctx);
1741 1742 1743 1744 1745 1746
        return;
    }
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_fsqrt();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1747
    if (unlikely(Rc(ctx->opcode) != 0))
1748 1749
        gen_op_set_Rc1();
}
B
bellard 已提交
1750

1751
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
B
bellard 已提交
1752
{
1753
    if (unlikely(!ctx->fpu_enabled)) {
1754
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1755 1756
        return;
    }
1757 1758
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1759 1760
    gen_op_fsqrt();
    gen_op_frsp();
1761
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1762
    if (unlikely(Rc(ctx->opcode) != 0))
1763
        gen_op_set_Rc1();
B
bellard 已提交
1764 1765 1766
}

/***                     Floating-Point multiply-and-add                   ***/
1767
/* fmadd - fmadds */
1768
GEN_FLOAT_ACB(madd, 0x1D, PPC_FLOAT);
1769
/* fmsub - fmsubs */
1770
GEN_FLOAT_ACB(msub, 0x1C, PPC_FLOAT);
1771
/* fnmadd - fnmadds */
1772
GEN_FLOAT_ACB(nmadd, 0x1F, PPC_FLOAT);
1773
/* fnmsub - fnmsubs */
1774
GEN_FLOAT_ACB(nmsub, 0x1E, PPC_FLOAT);
B
bellard 已提交
1775 1776 1777

/***                     Floating-Point round & convert                    ***/
/* fctiw */
1778
GEN_FLOAT_B(ctiw, 0x0E, 0x00, PPC_FLOAT);
B
bellard 已提交
1779
/* fctiwz */
1780
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, PPC_FLOAT);
B
bellard 已提交
1781
/* frsp */
1782
GEN_FLOAT_B(rsp, 0x0C, 0x00, PPC_FLOAT);
J
j_mayer 已提交
1783 1784
#if defined(TARGET_PPC64)
/* fcfid */
1785
GEN_FLOAT_B(cfid, 0x0E, 0x1A, PPC_64B);
J
j_mayer 已提交
1786
/* fctid */
1787
GEN_FLOAT_B(ctid, 0x0E, 0x19, PPC_64B);
J
j_mayer 已提交
1788
/* fctidz */
1789
GEN_FLOAT_B(ctidz, 0x0F, 0x19, PPC_64B);
J
j_mayer 已提交
1790
#endif
B
bellard 已提交
1791

1792 1793 1794 1795 1796 1797 1798 1799 1800
/* frin */
GEN_FLOAT_B(rin, 0x08, 0x0C, PPC_FLOAT_EXT);
/* friz */
GEN_FLOAT_B(riz, 0x08, 0x0D, PPC_FLOAT_EXT);
/* frip */
GEN_FLOAT_B(rip, 0x08, 0x0E, PPC_FLOAT_EXT);
/* frim */
GEN_FLOAT_B(rim, 0x08, 0x0F, PPC_FLOAT_EXT);

B
bellard 已提交
1801 1802
/***                         Floating-Point compare                        ***/
/* fcmpo */
1803
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1804
{
1805
    if (unlikely(!ctx->fpu_enabled)) {
1806
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1807 1808
        return;
    }
1809 1810 1811 1812 1813
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
    gen_op_fcmpo();
    gen_op_store_T0_crf(crfD(ctx->opcode));
B
bellard 已提交
1814 1815 1816
}

/* fcmpu */
1817
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
B
bellard 已提交
1818
{
1819
    if (unlikely(!ctx->fpu_enabled)) {
1820
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1821 1822
        return;
    }
1823 1824 1825 1826 1827
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rA(ctx->opcode));
    gen_op_load_fpr_FT1(rB(ctx->opcode));
    gen_op_fcmpu();
    gen_op_store_T0_crf(crfD(ctx->opcode));
B
bellard 已提交
1828 1829
}

1830 1831
/***                         Floating-point move                           ***/
/* fabs */
1832
GEN_FLOAT_B(abs, 0x08, 0x08, PPC_FLOAT);
1833 1834 1835 1836

/* fmr  - fmr. */
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
{
1837
    if (unlikely(!ctx->fpu_enabled)) {
1838
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1839 1840
        return;
    }
1841 1842 1843
    gen_op_reset_scrfx();
    gen_op_load_fpr_FT0(rB(ctx->opcode));
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1844
    if (unlikely(Rc(ctx->opcode) != 0))
1845 1846 1847 1848
        gen_op_set_Rc1();
}

/* fnabs */
1849
GEN_FLOAT_B(nabs, 0x08, 0x04, PPC_FLOAT);
1850
/* fneg */
1851
GEN_FLOAT_B(neg, 0x08, 0x01, PPC_FLOAT);
1852

B
bellard 已提交
1853 1854 1855 1856
/***                  Floating-Point status & ctrl register                ***/
/* mcrfs */
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
{
1857
    if (unlikely(!ctx->fpu_enabled)) {
1858
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1859 1860
        return;
    }
B
bellard 已提交
1861 1862 1863
    gen_op_load_fpscr_T0(crfS(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
    gen_op_clear_fpscr(crfS(ctx->opcode));
B
bellard 已提交
1864 1865 1866 1867 1868
}

/* mffs */
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
{
1869
    if (unlikely(!ctx->fpu_enabled)) {
1870
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1871 1872
        return;
    }
1873
    gen_op_load_fpscr();
B
bellard 已提交
1874
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1875
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1876
        gen_op_set_Rc1();
B
bellard 已提交
1877 1878 1879 1880 1881
}

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

1884
    if (unlikely(!ctx->fpu_enabled)) {
1885
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1886 1887
        return;
    }
B
bellard 已提交
1888 1889
    crb = crbD(ctx->opcode) >> 2;
    gen_op_load_fpscr_T0(crb);
1890
    gen_op_andi_T0(~(1 << (crbD(ctx->opcode) & 0x03)));
B
bellard 已提交
1891
    gen_op_store_T0_fpscr(crb);
1892
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1893
        gen_op_set_Rc1();
B
bellard 已提交
1894 1895 1896 1897 1898
}

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

1901
    if (unlikely(!ctx->fpu_enabled)) {
1902
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1903 1904
        return;
    }
B
bellard 已提交
1905 1906 1907 1908
    crb = crbD(ctx->opcode) >> 2;
    gen_op_load_fpscr_T0(crb);
    gen_op_ori(1 << (crbD(ctx->opcode) & 0x03));
    gen_op_store_T0_fpscr(crb);
1909
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1910
        gen_op_set_Rc1();
B
bellard 已提交
1911 1912 1913 1914 1915
}

/* mtfsf */
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
{
1916
    if (unlikely(!ctx->fpu_enabled)) {
1917
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1918 1919
        return;
    }
B
bellard 已提交
1920
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1921
    gen_op_store_fpscr(FM(ctx->opcode));
1922
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1923
        gen_op_set_Rc1();
B
bellard 已提交
1924 1925 1926 1927 1928
}

/* mtfsfi */
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
{
1929
    if (unlikely(!ctx->fpu_enabled)) {
1930
        GEN_EXCP_NO_FP(ctx);
B
bellard 已提交
1931 1932
        return;
    }
B
bellard 已提交
1933
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
1934
    if (unlikely(Rc(ctx->opcode) != 0))
B
bellard 已提交
1935
        gen_op_set_Rc1();
B
bellard 已提交
1936 1937
}

1938 1939
/***                           Addressing modes                            ***/
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
1940
static inline void gen_addr_imm_index (DisasContext *ctx, target_long maskl)
1941 1942 1943
{
    target_long simm = SIMM(ctx->opcode);

1944
    simm &= ~maskl;
1945
    if (rA(ctx->opcode) == 0) {
1946
        gen_set_T0(simm);
1947 1948 1949 1950 1951
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        if (likely(simm != 0))
            gen_op_addi(simm);
    }
1952 1953 1954
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
}

static inline void gen_addr_reg_index (DisasContext *ctx)
{
    if (rA(ctx->opcode) == 0) {
        gen_op_load_gpr_T0(rB(ctx->opcode));
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rB(ctx->opcode));
        gen_op_add();
    }
1966 1967 1968
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
1969 1970 1971 1972 1973 1974 1975 1976 1977
}

static inline void gen_addr_register (DisasContext *ctx)
{
    if (rA(ctx->opcode) == 0) {
        gen_op_reset_T0();
    } else {
        gen_op_load_gpr_T0(rA(ctx->opcode));
    }
1978 1979 1980
#ifdef DEBUG_MEMORY_ACCESSES
    gen_op_print_mem_EA();
#endif
1981 1982
}

B
bellard 已提交
1983
/***                             Integer load                              ***/
1984
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
1985
#if defined(CONFIG_USER_ONLY)
1986
#if defined(TARGET_PPC64)
1987
/* User mode only - 64 bits */
1988 1989 1990 1991
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_raw,                                                   \
    &gen_op_l##width##_le_raw,                                                \
1992 1993
    &gen_op_l##width##_64_raw,                                                \
    &gen_op_l##width##_le_64_raw,                                             \
1994 1995 1996 1997 1998
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_raw,                                                  \
    &gen_op_st##width##_le_raw,                                               \
1999 2000
    &gen_op_st##width##_64_raw,                                               \
    &gen_op_st##width##_le_64_raw,                                            \
2001 2002
};
/* Byte access routine are endian safe */
2003 2004 2005
#define gen_op_stb_le_64_raw gen_op_stb_64_raw
#define gen_op_lbz_le_64_raw gen_op_lbz_64_raw
#else
2006
/* User mode only - 32 bits */
2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_raw,                                                   \
    &gen_op_l##width##_le_raw,                                                \
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_raw,                                                  \
    &gen_op_st##width##_le_raw,                                               \
};
#endif
/* Byte access routine are endian safe */
2019 2020
#define gen_op_stb_le_raw gen_op_stb_raw
#define gen_op_lbz_le_raw gen_op_lbz_raw
2021
#else
2022
#if defined(TARGET_PPC64)
2023 2024
#if defined(TARGET_PPC64H)
/* Full system - 64 bits with hypervisor mode */
2025 2026 2027
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
2028
    &gen_op_l##width##_le_user,                                               \
2029 2030
    &gen_op_l##width##_64_user,                                               \
    &gen_op_l##width##_le_64_user,                                            \
2031 2032
    &gen_op_l##width##_kernel,                                                \
    &gen_op_l##width##_le_kernel,                                             \
2033 2034
    &gen_op_l##width##_64_kernel,                                             \
    &gen_op_l##width##_le_64_kernel,                                          \
2035 2036 2037 2038
    &gen_op_l##width##_hypv,                                                  \
    &gen_op_l##width##_le_hypv,                                               \
    &gen_op_l##width##_64_hypv,                                               \
    &gen_op_l##width##_le_64_hypv,                                            \
2039
};
2040 2041 2042
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
2043
    &gen_op_st##width##_le_user,                                              \
2044 2045
    &gen_op_st##width##_64_user,                                              \
    &gen_op_st##width##_le_64_user,                                           \
2046
    &gen_op_st##width##_kernel,                                               \
2047
    &gen_op_st##width##_le_kernel,                                            \
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
    &gen_op_st##width##_64_kernel,                                            \
    &gen_op_st##width##_le_64_kernel,                                         \
    &gen_op_st##width##_hypv,                                                 \
    &gen_op_st##width##_le_hypv,                                              \
    &gen_op_st##width##_64_hypv,                                              \
    &gen_op_st##width##_le_64_hypv,                                           \
};
/* Byte access routine are endian safe */
#define gen_op_stb_le_hypv      gen_op_stb_64_hypv
#define gen_op_lbz_le_hypv      gen_op_lbz_64_hypv
#define gen_op_stb_le_64_hypv   gen_op_stb_64_hypv
#define gen_op_lbz_le_64_hypv   gen_op_lbz_64_hypv
#else
/* Full system - 64 bits */
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
    &gen_op_l##width##_le_user,                                               \
    &gen_op_l##width##_64_user,                                               \
    &gen_op_l##width##_le_64_user,                                            \
    &gen_op_l##width##_kernel,                                                \
    &gen_op_l##width##_le_kernel,                                             \
    &gen_op_l##width##_64_kernel,                                             \
    &gen_op_l##width##_le_64_kernel,                                          \
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
    &gen_op_st##width##_le_user,                                              \
2077 2078
    &gen_op_st##width##_64_user,                                              \
    &gen_op_st##width##_le_64_user,                                           \
2079 2080
    &gen_op_st##width##_kernel,                                               \
    &gen_op_st##width##_le_kernel,                                            \
2081 2082
    &gen_op_st##width##_64_kernel,                                            \
    &gen_op_st##width##_le_64_kernel,                                         \
2083
};
2084
#endif
2085
/* Byte access routine are endian safe */
2086 2087
#define gen_op_stb_le_64_user   gen_op_stb_64_user
#define gen_op_lbz_le_64_user   gen_op_lbz_64_user
2088 2089 2090
#define gen_op_stb_le_64_kernel gen_op_stb_64_kernel
#define gen_op_lbz_le_64_kernel gen_op_lbz_64_kernel
#else
2091
/* Full system - 32 bits */
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107
#define OP_LD_TABLE(width)                                                    \
static GenOpFunc *gen_op_l##width[] = {                                       \
    &gen_op_l##width##_user,                                                  \
    &gen_op_l##width##_le_user,                                               \
    &gen_op_l##width##_kernel,                                                \
    &gen_op_l##width##_le_kernel,                                             \
};
#define OP_ST_TABLE(width)                                                    \
static GenOpFunc *gen_op_st##width[] = {                                      \
    &gen_op_st##width##_user,                                                 \
    &gen_op_st##width##_le_user,                                              \
    &gen_op_st##width##_kernel,                                               \
    &gen_op_st##width##_le_kernel,                                            \
};
#endif
/* Byte access routine are endian safe */
2108 2109
#define gen_op_stb_le_user   gen_op_stb_user
#define gen_op_lbz_le_user   gen_op_lbz_user
2110 2111
#define gen_op_stb_le_kernel gen_op_stb_kernel
#define gen_op_lbz_le_kernel gen_op_lbz_kernel
2112 2113
#endif

2114 2115
#define GEN_LD(width, opc, type)                                              \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2116
{                                                                             \
J
j_mayer 已提交
2117
    gen_addr_imm_index(ctx, 0);                                               \
2118
    op_ldst(l##width);                                                        \
B
bellard 已提交
2119 2120 2121
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2122 2123
#define GEN_LDU(width, opc, type)                                             \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2124
{                                                                             \
2125 2126
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2127
        GEN_EXCP_INVAL(ctx);                                                  \
2128
        return;                                                               \
2129
    }                                                                         \
J
j_mayer 已提交
2130
    if (type == PPC_64B)                                                      \
2131
        gen_addr_imm_index(ctx, 0x03);                                        \
J
j_mayer 已提交
2132 2133
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
2134
    op_ldst(l##width);                                                        \
B
bellard 已提交
2135 2136 2137 2138
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2139 2140
#define GEN_LDUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2141
{                                                                             \
2142 2143
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2144
        GEN_EXCP_INVAL(ctx);                                                  \
2145
        return;                                                               \
2146
    }                                                                         \
2147
    gen_addr_reg_index(ctx);                                                  \
2148
    op_ldst(l##width);                                                        \
B
bellard 已提交
2149 2150 2151 2152
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2153 2154
#define GEN_LDX(width, opc2, opc3, type)                                      \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2155
{                                                                             \
2156
    gen_addr_reg_index(ctx);                                                  \
2157
    op_ldst(l##width);                                                        \
B
bellard 已提交
2158 2159 2160
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
}

2161
#define GEN_LDS(width, op, type)                                              \
2162
OP_LD_TABLE(width);                                                           \
2163 2164 2165 2166
GEN_LD(width, op | 0x20, type);                                               \
GEN_LDU(width, op | 0x21, type);                                              \
GEN_LDUX(width, 0x17, op | 0x01, type);                                       \
GEN_LDX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2167 2168

/* lbz lbzu lbzux lbzx */
2169
GEN_LDS(bz, 0x02, PPC_INTEGER);
B
bellard 已提交
2170
/* lha lhau lhaux lhax */
2171
GEN_LDS(ha, 0x0A, PPC_INTEGER);
B
bellard 已提交
2172
/* lhz lhzu lhzux lhzx */
2173
GEN_LDS(hz, 0x08, PPC_INTEGER);
B
bellard 已提交
2174
/* lwz lwzu lwzux lwzx */
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
GEN_LDS(wz, 0x00, PPC_INTEGER);
#if defined(TARGET_PPC64)
OP_LD_TABLE(wa);
OP_LD_TABLE(d);
/* lwaux */
GEN_LDUX(wa, 0x15, 0x0B, PPC_64B);
/* lwax */
GEN_LDX(wa, 0x15, 0x0A, PPC_64B);
/* ldux */
GEN_LDUX(d, 0x15, 0x01, PPC_64B);
/* ldx */
GEN_LDX(d, 0x15, 0x00, PPC_64B);
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
{
    if (Rc(ctx->opcode)) {
        if (unlikely(rA(ctx->opcode) == 0 ||
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2192
            GEN_EXCP_INVAL(ctx);
2193 2194 2195
            return;
        }
    }
2196
    gen_addr_imm_index(ctx, 0x03);
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
    if (ctx->opcode & 0x02) {
        /* lwa (lwau is undefined) */
        op_ldst(lwa);
    } else {
        /* ld - ldu */
        op_ldst(ld);
    }
    gen_op_store_T1_gpr(rD(ctx->opcode));
    if (Rc(ctx->opcode))
        gen_op_store_T0_gpr(rA(ctx->opcode));
}
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239
/* lq */
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    int ra, rd;

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

/***                              Integer store                            ***/
2243 2244
#define GEN_ST(width, opc, type)                                              \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2245
{                                                                             \
J
j_mayer 已提交
2246
    gen_addr_imm_index(ctx, 0);                                               \
2247 2248
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2249 2250
}

2251 2252
#define GEN_STU(width, opc, type)                                             \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2253
{                                                                             \
2254
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2255
        GEN_EXCP_INVAL(ctx);                                                  \
2256
        return;                                                               \
2257
    }                                                                         \
J
j_mayer 已提交
2258
    if (type == PPC_64B)                                                      \
2259
        gen_addr_imm_index(ctx, 0x03);                                        \
J
j_mayer 已提交
2260 2261
    else                                                                      \
        gen_addr_imm_index(ctx, 0);                                           \
B
bellard 已提交
2262
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2263
    op_ldst(st##width);                                                       \
B
bellard 已提交
2264 2265 2266
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2267 2268
#define GEN_STUX(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
B
bellard 已提交
2269
{                                                                             \
2270
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2271
        GEN_EXCP_INVAL(ctx);                                                  \
2272
        return;                                                               \
2273
    }                                                                         \
2274
    gen_addr_reg_index(ctx);                                                  \
2275 2276
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2277 2278 2279
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2280 2281
#define GEN_STX(width, opc2, opc3, type)                                      \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2282
{                                                                             \
2283
    gen_addr_reg_index(ctx);                                                  \
2284 2285
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
    op_ldst(st##width);                                                       \
B
bellard 已提交
2286 2287
}

2288
#define GEN_STS(width, op, type)                                              \
2289
OP_ST_TABLE(width);                                                           \
2290 2291 2292 2293
GEN_ST(width, op | 0x20, type);                                               \
GEN_STU(width, op | 0x21, type);                                              \
GEN_STUX(width, 0x17, op | 0x01, type);                                       \
GEN_STX(width, 0x17, op | 0x00, type)
B
bellard 已提交
2294 2295

/* stb stbu stbux stbx */
2296
GEN_STS(b, 0x06, PPC_INTEGER);
B
bellard 已提交
2297
/* sth sthu sthux sthx */
2298
GEN_STS(h, 0x0C, PPC_INTEGER);
B
bellard 已提交
2299
/* stw stwu stwux stwx */
2300 2301 2302
GEN_STS(w, 0x04, PPC_INTEGER);
#if defined(TARGET_PPC64)
OP_ST_TABLE(d);
J
j_mayer 已提交
2303 2304
GEN_STUX(d, 0x15, 0x05, PPC_64B);
GEN_STX(d, 0x15, 0x04, PPC_64B);
2305
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2306
{
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
    int rs;

    rs = rS(ctx->opcode);
    if ((ctx->opcode & 0x3) == 0x2) {
#if defined(CONFIG_USER_ONLY)
        GEN_EXCP_PRIVOPC(ctx);
#else
        /* stq */
        if (unlikely(ctx->supervisor == 0)) {
            GEN_EXCP_PRIVOPC(ctx);
            return;
        }
        if (unlikely(rs & 1)) {
2320
            GEN_EXCP_INVAL(ctx);
2321 2322
            return;
        }
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
        if (unlikely(ctx->mem_idx & 1)) {
            /* Little-endian mode is not handled */
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
            return;
        }
        gen_addr_imm_index(ctx, 0x03);
        gen_op_load_gpr_T1(rs);
        op_ldst(std);
        gen_op_addi(8);
        gen_op_load_gpr_T1(rs + 1);
        op_ldst(std);
#endif
    } else {
        /* std / stdu */
        if (Rc(ctx->opcode)) {
            if (unlikely(rA(ctx->opcode) == 0)) {
                GEN_EXCP_INVAL(ctx);
                return;
            }
        }
        gen_addr_imm_index(ctx, 0x03);
        gen_op_load_gpr_T1(rs);
        op_ldst(std);
        if (Rc(ctx->opcode))
            gen_op_store_T0_gpr(rA(ctx->opcode));
2348 2349 2350
    }
}
#endif
B
bellard 已提交
2351 2352
/***                Integer load and store with byte reverse               ***/
/* lhbrx */
2353
OP_LD_TABLE(hbr);
2354
GEN_LDX(hbr, 0x16, 0x18, PPC_INTEGER);
B
bellard 已提交
2355
/* lwbrx */
2356
OP_LD_TABLE(wbr);
2357
GEN_LDX(wbr, 0x16, 0x10, PPC_INTEGER);
B
bellard 已提交
2358
/* sthbrx */
2359
OP_ST_TABLE(hbr);
2360
GEN_STX(hbr, 0x16, 0x1C, PPC_INTEGER);
B
bellard 已提交
2361
/* stwbrx */
2362
OP_ST_TABLE(wbr);
2363
GEN_STX(wbr, 0x16, 0x14, PPC_INTEGER);
B
bellard 已提交
2364 2365

/***                    Integer load and store multiple                    ***/
2366
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2367
#if defined(CONFIG_USER_ONLY)
2368
/* User-mode only */
2369 2370 2371
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_raw,
    &gen_op_lmw_le_raw,
2372
#if defined(TARGET_PPC64)
2373 2374
    &gen_op_lmw_64_raw,
    &gen_op_lmw_le_64_raw,
2375
#endif
2376 2377
};
static GenOpFunc1 *gen_op_stmw[] = {
2378 2379 2380
    &gen_op_stmw_raw,
    &gen_op_stmw_le_raw,
#if defined(TARGET_PPC64)
2381 2382
    &gen_op_stmw_64_raw,
    &gen_op_stmw_le_64_raw,
2383
#endif
2384 2385
};
#else
2386 2387
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
2388 2389 2390 2391 2392
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
    &gen_op_lmw_le_user,
    &gen_op_lmw_64_user,
    &gen_op_lmw_le_64_user,
2393 2394
    &gen_op_lmw_kernel,
    &gen_op_lmw_le_kernel,
2395 2396
    &gen_op_lmw_64_kernel,
    &gen_op_lmw_le_64_kernel,
2397 2398 2399 2400 2401 2402
#if defined(TARGET_PPC64H)
    &gen_op_lmw_hypv,
    &gen_op_lmw_le_hypv,
    &gen_op_lmw_64_hypv,
    &gen_op_lmw_le_64_hypv,
#endif
2403 2404 2405 2406 2407 2408
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
    &gen_op_stmw_le_user,
    &gen_op_stmw_64_user,
    &gen_op_stmw_le_64_user,
2409 2410
    &gen_op_stmw_kernel,
    &gen_op_stmw_le_kernel,
2411 2412
    &gen_op_stmw_64_kernel,
    &gen_op_stmw_le_64_kernel,
2413 2414 2415 2416 2417
#if defined(TARGET_PPC64H)
    &gen_op_stmw_hypv,
    &gen_op_stmw_le_hypv,
    &gen_op_stmw_64_hypv,
    &gen_op_stmw_le_64_hypv,
2418
#endif
2419
};
2420
#else
2421
/* Full system - 32 bits mode */
2422 2423
static GenOpFunc1 *gen_op_lmw[] = {
    &gen_op_lmw_user,
2424
    &gen_op_lmw_le_user,
2425
    &gen_op_lmw_kernel,
2426
    &gen_op_lmw_le_kernel,
2427 2428 2429
};
static GenOpFunc1 *gen_op_stmw[] = {
    &gen_op_stmw_user,
2430
    &gen_op_stmw_le_user,
2431
    &gen_op_stmw_kernel,
2432
    &gen_op_stmw_le_kernel,
2433 2434
};
#endif
2435
#endif
2436

B
bellard 已提交
2437 2438 2439
/* lmw */
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2440
    /* NIP cannot be restored if the memory exception comes from an helper */
2441
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2442
    gen_addr_imm_index(ctx, 0);
2443
    op_ldstm(lmw, rD(ctx->opcode));
B
bellard 已提交
2444 2445 2446 2447 2448
}

/* stmw */
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
{
2449
    /* NIP cannot be restored if the memory exception comes from an helper */
2450
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2451
    gen_addr_imm_index(ctx, 0);
2452
    op_ldstm(stmw, rS(ctx->opcode));
B
bellard 已提交
2453 2454 2455
}

/***                    Integer load and store strings                     ***/
2456 2457
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
2458
#if defined(CONFIG_USER_ONLY)
2459
/* User-mode only */
2460 2461 2462
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_raw,
    &gen_op_lswi_le_raw,
2463
#if defined(TARGET_PPC64)
2464 2465
    &gen_op_lswi_64_raw,
    &gen_op_lswi_le_64_raw,
2466
#endif
2467 2468 2469 2470
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_raw,
    &gen_op_lswx_le_raw,
2471
#if defined(TARGET_PPC64)
2472 2473
    &gen_op_lswx_64_raw,
    &gen_op_lswx_le_64_raw,
2474
#endif
2475 2476 2477 2478
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_raw,
    &gen_op_stsw_le_raw,
2479
#if defined(TARGET_PPC64)
2480 2481
    &gen_op_stsw_64_raw,
    &gen_op_stsw_le_64_raw,
2482
#endif
2483 2484
};
#else
2485 2486
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
2487 2488 2489 2490 2491
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
    &gen_op_lswi_le_user,
    &gen_op_lswi_64_user,
    &gen_op_lswi_le_64_user,
2492 2493
    &gen_op_lswi_kernel,
    &gen_op_lswi_le_kernel,
2494 2495
    &gen_op_lswi_64_kernel,
    &gen_op_lswi_le_64_kernel,
2496 2497 2498 2499 2500 2501
#if defined(TARGET_PPC64H)
    &gen_op_lswi_hypv,
    &gen_op_lswi_le_hypv,
    &gen_op_lswi_64_hypv,
    &gen_op_lswi_le_64_hypv,
#endif
2502 2503 2504 2505 2506 2507
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
    &gen_op_lswx_le_user,
    &gen_op_lswx_64_user,
    &gen_op_lswx_le_64_user,
2508 2509
    &gen_op_lswx_kernel,
    &gen_op_lswx_le_kernel,
2510 2511
    &gen_op_lswx_64_kernel,
    &gen_op_lswx_le_64_kernel,
2512 2513 2514 2515 2516 2517
#if defined(TARGET_PPC64H)
    &gen_op_lswx_hypv,
    &gen_op_lswx_le_hypv,
    &gen_op_lswx_64_hypv,
    &gen_op_lswx_le_64_hypv,
#endif
2518 2519 2520 2521 2522 2523
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
    &gen_op_stsw_le_user,
    &gen_op_stsw_64_user,
    &gen_op_stsw_le_64_user,
2524 2525
    &gen_op_stsw_kernel,
    &gen_op_stsw_le_kernel,
2526 2527
    &gen_op_stsw_64_kernel,
    &gen_op_stsw_le_64_kernel,
2528 2529 2530 2531 2532
#if defined(TARGET_PPC64H)
    &gen_op_stsw_hypv,
    &gen_op_stsw_le_hypv,
    &gen_op_stsw_64_hypv,
    &gen_op_stsw_le_64_hypv,
2533
#endif
2534 2535
};
#else
2536
/* Full system - 32 bits mode */
2537 2538
static GenOpFunc1 *gen_op_lswi[] = {
    &gen_op_lswi_user,
2539
    &gen_op_lswi_le_user,
2540
    &gen_op_lswi_kernel,
2541
    &gen_op_lswi_le_kernel,
2542 2543 2544
};
static GenOpFunc3 *gen_op_lswx[] = {
    &gen_op_lswx_user,
2545
    &gen_op_lswx_le_user,
2546
    &gen_op_lswx_kernel,
2547
    &gen_op_lswx_le_kernel,
2548 2549 2550
};
static GenOpFunc1 *gen_op_stsw[] = {
    &gen_op_stsw_user,
2551
    &gen_op_stsw_le_user,
2552
    &gen_op_stsw_kernel,
2553
    &gen_op_stsw_le_kernel,
2554 2555
};
#endif
2556
#endif
2557

B
bellard 已提交
2558
/* lswi */
2559
/* PowerPC32 specification says we must generate an exception if
2560 2561 2562 2563
 * rA is in the range of registers to be loaded.
 * In an other hand, IBM says this is valid, but rA won't be loaded.
 * For now, I'll follow the spec...
 */
B
bellard 已提交
2564 2565 2566 2567
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
{
    int nb = NB(ctx->opcode);
    int start = rD(ctx->opcode);
2568
    int ra = rA(ctx->opcode);
B
bellard 已提交
2569 2570 2571 2572 2573
    int nr;

    if (nb == 0)
        nb = 32;
    nr = nb / 4;
2574 2575 2576
    if (unlikely(((start + nr) > 32  &&
                  start <= ra && (start + nr - 32) > ra) ||
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
2577 2578
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
2579
        return;
B
bellard 已提交
2580
    }
2581
    /* NIP cannot be restored if the memory exception comes from an helper */
2582
    gen_update_nip(ctx, ctx->nip - 4);
2583 2584
    gen_addr_register(ctx);
    gen_op_set_T1(nb);
2585
    op_ldsts(lswi, start);
B
bellard 已提交
2586 2587 2588 2589 2590
}

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

2594
    /* NIP cannot be restored if the memory exception comes from an helper */
2595
    gen_update_nip(ctx, ctx->nip - 4);
2596
    gen_addr_reg_index(ctx);
2597 2598
    if (ra == 0) {
        ra = rb;
B
bellard 已提交
2599
    }
2600 2601
    gen_op_load_xer_bc();
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
B
bellard 已提交
2602 2603 2604 2605 2606
}

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

2609
    /* NIP cannot be restored if the memory exception comes from an helper */
2610
    gen_update_nip(ctx, ctx->nip - 4);
2611
    gen_addr_register(ctx);
B
bellard 已提交
2612 2613 2614
    if (nb == 0)
        nb = 32;
    gen_op_set_T1(nb);
2615
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2616 2617 2618 2619 2620
}

/* stswx */
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
{
2621
    /* NIP cannot be restored if the memory exception comes from an helper */
2622
    gen_update_nip(ctx, ctx->nip - 4);
2623 2624
    gen_addr_reg_index(ctx);
    gen_op_load_xer_bc();
2625
    op_ldsts(stsw, rS(ctx->opcode));
B
bellard 已提交
2626 2627 2628 2629
}

/***                        Memory synchronisation                         ***/
/* eieio */
2630
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
B
bellard 已提交
2631 2632 2633 2634
{
}

/* isync */
2635
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
B
bellard 已提交
2636
{
2637
    GEN_STOP(ctx);
B
bellard 已提交
2638 2639
}

2640 2641
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
2642
#if defined(CONFIG_USER_ONLY)
2643
/* User-mode only */
2644 2645 2646
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_raw,
    &gen_op_lwarx_le_raw,
2647
#if defined(TARGET_PPC64)
2648 2649
    &gen_op_lwarx_64_raw,
    &gen_op_lwarx_le_64_raw,
2650
#endif
2651 2652 2653 2654
};
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_raw,
    &gen_op_stwcx_le_raw,
2655
#if defined(TARGET_PPC64)
2656 2657
    &gen_op_stwcx_64_raw,
    &gen_op_stwcx_le_64_raw,
2658
#endif
2659
};
2660
#else
2661 2662
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
B
bellard 已提交
2663 2664
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_user,
2665
    &gen_op_lwarx_le_user,
2666 2667
    &gen_op_lwarx_64_user,
    &gen_op_lwarx_le_64_user,
2668 2669
    &gen_op_lwarx_kernel,
    &gen_op_lwarx_le_kernel,
2670 2671
    &gen_op_lwarx_64_kernel,
    &gen_op_lwarx_le_64_kernel,
2672 2673 2674 2675 2676 2677
#if defined(TARGET_PPC64H)
    &gen_op_lwarx_hypv,
    &gen_op_lwarx_le_hypv,
    &gen_op_lwarx_64_hypv,
    &gen_op_lwarx_le_64_hypv,
#endif
B
bellard 已提交
2678
};
2679 2680
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_user,
2681
    &gen_op_stwcx_le_user,
2682 2683
    &gen_op_stwcx_64_user,
    &gen_op_stwcx_le_64_user,
2684 2685
    &gen_op_stwcx_kernel,
    &gen_op_stwcx_le_kernel,
2686 2687
    &gen_op_stwcx_64_kernel,
    &gen_op_stwcx_le_64_kernel,
2688 2689 2690 2691 2692
#if defined(TARGET_PPC64H)
    &gen_op_stwcx_hypv,
    &gen_op_stwcx_le_hypv,
    &gen_op_stwcx_64_hypv,
    &gen_op_stwcx_le_64_hypv,
2693
#endif
2694 2695
};
#else
2696
/* Full system - 32 bits mode */
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710
static GenOpFunc *gen_op_lwarx[] = {
    &gen_op_lwarx_user,
    &gen_op_lwarx_le_user,
    &gen_op_lwarx_kernel,
    &gen_op_lwarx_le_kernel,
};
static GenOpFunc *gen_op_stwcx[] = {
    &gen_op_stwcx_user,
    &gen_op_stwcx_le_user,
    &gen_op_stwcx_kernel,
    &gen_op_stwcx_le_kernel,
};
#endif
#endif
2711

2712
/* lwarx */
2713
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
B
bellard 已提交
2714
{
2715 2716
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2717
    gen_addr_reg_index(ctx);
B
bellard 已提交
2718
    op_lwarx();
B
bellard 已提交
2719 2720 2721 2722
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stwcx. */
2723
GEN_HANDLER(stwcx_, 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
B
bellard 已提交
2724
{
2725 2726
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
2727
    gen_addr_reg_index(ctx);
2728 2729
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stwcx();
B
bellard 已提交
2730 2731
}

J
j_mayer 已提交
2732 2733 2734 2735
#if defined(TARGET_PPC64)
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
2736
/* User-mode only */
J
j_mayer 已提交
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
static GenOpFunc *gen_op_ldarx[] = {
    &gen_op_ldarx_raw,
    &gen_op_ldarx_le_raw,
    &gen_op_ldarx_64_raw,
    &gen_op_ldarx_le_64_raw,
};
static GenOpFunc *gen_op_stdcx[] = {
    &gen_op_stdcx_raw,
    &gen_op_stdcx_le_raw,
    &gen_op_stdcx_64_raw,
    &gen_op_stdcx_le_64_raw,
};
#else
2750
/* Full system */
J
j_mayer 已提交
2751 2752 2753 2754 2755
static GenOpFunc *gen_op_ldarx[] = {
    &gen_op_ldarx_user,
    &gen_op_ldarx_le_user,
    &gen_op_ldarx_64_user,
    &gen_op_ldarx_le_64_user,
2756 2757
    &gen_op_ldarx_kernel,
    &gen_op_ldarx_le_kernel,
J
j_mayer 已提交
2758 2759
    &gen_op_ldarx_64_kernel,
    &gen_op_ldarx_le_64_kernel,
2760 2761 2762 2763 2764 2765
#if defined(TARGET_PPC64H)
    &gen_op_ldarx_hypv,
    &gen_op_ldarx_le_hypv,
    &gen_op_ldarx_64_hypv,
    &gen_op_ldarx_le_64_hypv,
#endif
J
j_mayer 已提交
2766 2767 2768 2769 2770 2771
};
static GenOpFunc *gen_op_stdcx[] = {
    &gen_op_stdcx_user,
    &gen_op_stdcx_le_user,
    &gen_op_stdcx_64_user,
    &gen_op_stdcx_le_64_user,
2772 2773
    &gen_op_stdcx_kernel,
    &gen_op_stdcx_le_kernel,
J
j_mayer 已提交
2774 2775
    &gen_op_stdcx_64_kernel,
    &gen_op_stdcx_le_64_kernel,
2776 2777 2778 2779 2780 2781
#if defined(TARGET_PPC64H)
    &gen_op_stdcx_hypv,
    &gen_op_stdcx_le_hypv,
    &gen_op_stdcx_64_hypv,
    &gen_op_stdcx_le_64_hypv,
#endif
J
j_mayer 已提交
2782 2783 2784 2785
};
#endif

/* ldarx */
2786
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
J
j_mayer 已提交
2787
{
2788 2789
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2790 2791 2792 2793 2794 2795
    gen_addr_reg_index(ctx);
    op_ldarx();
    gen_op_store_T1_gpr(rD(ctx->opcode));
}

/* stdcx. */
2796
GEN_HANDLER(stdcx_, 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
J
j_mayer 已提交
2797
{
2798 2799
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
2800 2801 2802 2803 2804 2805
    gen_addr_reg_index(ctx);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    op_stdcx();
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
2806
/* sync */
2807
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
B
bellard 已提交
2808 2809 2810
{
}

2811 2812 2813 2814
/* wait */
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
{
    /* Stop translation, as the CPU is supposed to sleep from now */
2815 2816
    gen_op_wait();
    GEN_EXCP(ctx, EXCP_HLT, 1);
2817 2818
}

B
bellard 已提交
2819
/***                         Floating-point load                           ***/
2820 2821
#define GEN_LDF(width, opc, type)                                             \
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
B
bellard 已提交
2822
{                                                                             \
2823
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2824
        GEN_EXCP_NO_FP(ctx);                                                  \
2825 2826
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2827
    gen_addr_imm_index(ctx, 0);                                               \
2828
    op_ldst(l##width);                                                        \
2829
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2830 2831
}

2832 2833
#define GEN_LDUF(width, opc, type)                                            \
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
B
bellard 已提交
2834
{                                                                             \
2835
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2836
        GEN_EXCP_NO_FP(ctx);                                                  \
2837 2838
        return;                                                               \
    }                                                                         \
2839
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2840
        GEN_EXCP_INVAL(ctx);                                                  \
2841
        return;                                                               \
2842
    }                                                                         \
J
j_mayer 已提交
2843
    gen_addr_imm_index(ctx, 0);                                               \
2844
    op_ldst(l##width);                                                        \
2845
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2846 2847 2848
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2849 2850
#define GEN_LDUXF(width, opc, type)                                           \
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
B
bellard 已提交
2851
{                                                                             \
2852
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2853
        GEN_EXCP_NO_FP(ctx);                                                  \
2854 2855
        return;                                                               \
    }                                                                         \
2856
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2857
        GEN_EXCP_INVAL(ctx);                                                  \
2858
        return;                                                               \
2859
    }                                                                         \
2860
    gen_addr_reg_index(ctx);                                                  \
2861
    op_ldst(l##width);                                                        \
2862
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2863 2864 2865
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2866 2867
#define GEN_LDXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
B
bellard 已提交
2868
{                                                                             \
2869
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2870
        GEN_EXCP_NO_FP(ctx);                                                  \
2871 2872
        return;                                                               \
    }                                                                         \
2873
    gen_addr_reg_index(ctx);                                                  \
2874
    op_ldst(l##width);                                                        \
2875
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
B
bellard 已提交
2876 2877
}

2878
#define GEN_LDFS(width, op, type)                                             \
2879
OP_LD_TABLE(width);                                                           \
2880 2881 2882 2883
GEN_LDF(width, op | 0x20, type);                                              \
GEN_LDUF(width, op | 0x21, type);                                             \
GEN_LDUXF(width, op | 0x01, type);                                            \
GEN_LDXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
2884 2885

/* lfd lfdu lfdux lfdx */
2886
GEN_LDFS(fd, 0x12, PPC_FLOAT);
B
bellard 已提交
2887
/* lfs lfsu lfsux lfsx */
2888
GEN_LDFS(fs, 0x10, PPC_FLOAT);
B
bellard 已提交
2889 2890

/***                         Floating-point store                          ***/
2891 2892
#define GEN_STF(width, opc, type)                                             \
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
B
bellard 已提交
2893
{                                                                             \
2894
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2895
        GEN_EXCP_NO_FP(ctx);                                                  \
2896 2897
        return;                                                               \
    }                                                                         \
J
j_mayer 已提交
2898
    gen_addr_imm_index(ctx, 0);                                               \
2899
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2900
    op_ldst(st##width);                                                       \
B
bellard 已提交
2901 2902
}

2903 2904
#define GEN_STUF(width, opc, type)                                            \
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
B
bellard 已提交
2905
{                                                                             \
2906
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2907
        GEN_EXCP_NO_FP(ctx);                                                  \
2908 2909
        return;                                                               \
    }                                                                         \
2910
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2911
        GEN_EXCP_INVAL(ctx);                                                  \
2912
        return;                                                               \
2913
    }                                                                         \
J
j_mayer 已提交
2914
    gen_addr_imm_index(ctx, 0);                                               \
2915
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2916
    op_ldst(st##width);                                                       \
B
bellard 已提交
2917 2918 2919
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2920 2921
#define GEN_STUXF(width, opc, type)                                           \
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
B
bellard 已提交
2922
{                                                                             \
2923
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2924
        GEN_EXCP_NO_FP(ctx);                                                  \
2925 2926
        return;                                                               \
    }                                                                         \
2927
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2928
        GEN_EXCP_INVAL(ctx);                                                  \
2929
        return;                                                               \
2930
    }                                                                         \
2931 2932
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2933
    op_ldst(st##width);                                                       \
B
bellard 已提交
2934 2935 2936
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
}

2937 2938
#define GEN_STXF(width, opc2, opc3, type)                                     \
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
B
bellard 已提交
2939
{                                                                             \
2940
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2941
        GEN_EXCP_NO_FP(ctx);                                                  \
2942 2943
        return;                                                               \
    }                                                                         \
2944 2945
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
2946
    op_ldst(st##width);                                                       \
B
bellard 已提交
2947 2948
}

2949
#define GEN_STFS(width, op, type)                                             \
2950
OP_ST_TABLE(width);                                                           \
2951 2952 2953 2954
GEN_STF(width, op | 0x20, type);                                              \
GEN_STUF(width, op | 0x21, type);                                             \
GEN_STUXF(width, op | 0x01, type);                                            \
GEN_STXF(width, 0x17, op | 0x00, type)
B
bellard 已提交
2955 2956

/* stfd stfdu stfdux stfdx */
2957
GEN_STFS(fd, 0x16, PPC_FLOAT);
B
bellard 已提交
2958
/* stfs stfsu stfsux stfsx */
2959
GEN_STFS(fs, 0x14, PPC_FLOAT);
B
bellard 已提交
2960 2961 2962

/* Optional: */
/* stfiwx */
2963 2964
OP_ST_TABLE(fiwx);
GEN_STXF(fiwx, 0x17, 0x1E, PPC_FLOAT_STFIWX);
B
bellard 已提交
2965 2966

/***                                Branch                                 ***/
2967
static inline void gen_goto_tb (DisasContext *ctx, int n, target_ulong dest)
2968 2969 2970 2971 2972 2973 2974 2975
{
    TranslationBlock *tb;
    tb = ctx->tb;
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
        if (n == 0)
            gen_op_goto_tb0(TBPARAM(tb));
        else
            gen_op_goto_tb1(TBPARAM(tb));
2976 2977 2978 2979 2980 2981 2982
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
2983
        gen_op_set_T0((long)tb + n);
2984 2985
        if (ctx->singlestep_enabled)
            gen_op_debug();
2986 2987
        gen_op_exit_tb();
    } else {
2988 2989 2990 2991 2992 2993 2994
        gen_set_T1(dest);
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_b_T1_64();
        else
#endif
            gen_op_b_T1();
2995
        gen_op_reset_T0();
2996 2997
        if (ctx->singlestep_enabled)
            gen_op_debug();
2998 2999
        gen_op_exit_tb();
    }
B
bellard 已提交
3000 3001
}

3002 3003 3004 3005 3006 3007 3008 3009 3010 3011
static inline void gen_setlr (DisasContext *ctx, target_ulong nip)
{
#if defined(TARGET_PPC64)
    if (ctx->sf_mode != 0 && (nip >> 32))
        gen_op_setlr_64(ctx->nip >> 32, ctx->nip);
    else
#endif
        gen_op_setlr(ctx->nip);
}

B
bellard 已提交
3012 3013 3014
/* b ba bl bla */
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3015
    target_ulong li, target;
B
bellard 已提交
3016 3017

    /* sign extend LI */
3018
#if defined(TARGET_PPC64)
3019 3020 3021
    if (ctx->sf_mode)
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
    else
3022
#endif
3023
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3024
    if (likely(AA(ctx->opcode) == 0))
B
bellard 已提交
3025
        target = ctx->nip + li - 4;
B
bellard 已提交
3026
    else
3027
        target = li;
3028
#if defined(TARGET_PPC64)
3029 3030
    if (!ctx->sf_mode)
        target = (uint32_t)target;
3031
#endif
3032 3033
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3034
    gen_goto_tb(ctx, 0, target);
3035
    ctx->exception = POWERPC_EXCP_BRANCH;
B
bellard 已提交
3036 3037
}

3038 3039 3040 3041
#define BCOND_IM  0
#define BCOND_LR  1
#define BCOND_CTR 2

3042
static inline void gen_bcond (DisasContext *ctx, int type)
3043
{
3044 3045
    target_ulong target = 0;
    target_ulong li;
3046 3047 3048
    uint32_t bo = BO(ctx->opcode);
    uint32_t bi = BI(ctx->opcode);
    uint32_t mask;
3049 3050

    if ((bo & 0x4) == 0)
3051
        gen_op_dec_ctr();
3052 3053
    switch(type) {
    case BCOND_IM:
3054 3055
        li = (target_long)((int16_t)(BD(ctx->opcode)));
        if (likely(AA(ctx->opcode) == 0)) {
B
bellard 已提交
3056
            target = ctx->nip + li - 4;
3057 3058 3059
        } else {
            target = li;
        }
3060 3061 3062 3063
#if defined(TARGET_PPC64)
        if (!ctx->sf_mode)
            target = (uint32_t)target;
#endif
3064 3065 3066 3067 3068 3069 3070 3071 3072
        break;
    case BCOND_CTR:
        gen_op_movl_T1_ctr();
        break;
    default:
    case BCOND_LR:
        gen_op_movl_T1_lr();
        break;
    }
3073 3074
    if (LK(ctx->opcode))
        gen_setlr(ctx, ctx->nip);
3075
    if (bo & 0x10) {
3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092
        /* No CR condition */
        switch (bo & 0x6) {
        case 0:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctr_64();
            else
#endif
                gen_op_test_ctr();
            break;
        case 2:
#if defined(TARGET_PPC64)
            if (ctx->sf_mode)
                gen_op_test_ctrz_64();
            else
#endif
                gen_op_test_ctrz();
3093 3094
            break;
        default:
3095 3096
        case 4:
        case 6:
3097
            if (type == BCOND_IM) {
3098
                gen_goto_tb(ctx, 0, target);
3099
                goto out;
3100
            } else {
3101 3102 3103 3104 3105 3106
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_b_T1_64();
                else
#endif
                    gen_op_b_T1();
3107
                gen_op_reset_T0();
3108
                goto no_test;
3109
            }
3110
            break;
3111
        }
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135
    } else {
        mask = 1 << (3 - (bi & 0x03));
        gen_op_load_crf_T0(bi >> 2);
        if (bo & 0x8) {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_true_64(mask);
                else
#endif
                    gen_op_test_ctr_true(mask);
                break;
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_true_64(mask);
                else
#endif
                    gen_op_test_ctrz_true(mask);
                break;
            default:
            case 4:
            case 6:
3136
                gen_op_test_true(mask);
3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
                break;
            }
        } else {
            switch (bo & 0x6) {
            case 0:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctr_false_64(mask);
                else
#endif
                    gen_op_test_ctr_false(mask);
3148
                break;
3149 3150 3151 3152 3153 3154 3155 3156
            case 2:
#if defined(TARGET_PPC64)
                if (ctx->sf_mode)
                    gen_op_test_ctrz_false_64(mask);
                else
#endif
                    gen_op_test_ctrz_false(mask);
                break;
3157
            default:
3158 3159
            case 4:
            case 6:
3160
                gen_op_test_false(mask);
3161 3162 3163 3164
                break;
            }
        }
    }
3165
    if (type == BCOND_IM) {
B
bellard 已提交
3166 3167
        int l1 = gen_new_label();
        gen_op_jz_T0(l1);
3168
        gen_goto_tb(ctx, 0, target);
B
bellard 已提交
3169
        gen_set_label(l1);
3170
        gen_goto_tb(ctx, 1, ctx->nip);
3171
    } else {
3172 3173 3174 3175 3176 3177
#if defined(TARGET_PPC64)
        if (ctx->sf_mode)
            gen_op_btest_T1_64(ctx->nip >> 32, ctx->nip);
        else
#endif
            gen_op_btest_T1(ctx->nip);
3178
        gen_op_reset_T0();
3179
    no_test:
J
j_mayer 已提交
3180 3181 3182 3183
        if (ctx->singlestep_enabled)
            gen_op_debug();
        gen_op_exit_tb();
    }
3184
 out:
3185
    ctx->exception = POWERPC_EXCP_BRANCH;
3186 3187 3188
}

GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3189
{
3190 3191 3192 3193
    gen_bcond(ctx, BCOND_IM);
}

GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3194
{
3195 3196 3197 3198
    gen_bcond(ctx, BCOND_CTR);
}

GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3199
{
3200 3201
    gen_bcond(ctx, BCOND_LR);
}
B
bellard 已提交
3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218

/***                      Condition register logical                       ***/
#define GEN_CRLOGIC(op, opc)                                                  \
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
{                                                                             \
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
    gen_op_getbit_T0(3 - (crbA(ctx->opcode) & 0x03));                         \
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
    gen_op_getbit_T1(3 - (crbB(ctx->opcode) & 0x03));                         \
    gen_op_##op();                                                            \
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
    gen_op_setcrfbit(~(1 << (3 - (crbD(ctx->opcode) & 0x03))),                \
                     3 - (crbD(ctx->opcode) & 0x03));                         \
    gen_op_store_T1_crf(crbD(ctx->opcode) >> 2);                              \
}

/* crand */
3219
GEN_CRLOGIC(and, 0x08);
B
bellard 已提交
3220
/* crandc */
3221
GEN_CRLOGIC(andc, 0x04);
B
bellard 已提交
3222
/* creqv */
3223
GEN_CRLOGIC(eqv, 0x09);
B
bellard 已提交
3224
/* crnand */
3225
GEN_CRLOGIC(nand, 0x07);
B
bellard 已提交
3226
/* crnor */
3227
GEN_CRLOGIC(nor, 0x01);
B
bellard 已提交
3228
/* cror */
3229
GEN_CRLOGIC(or, 0x0E);
B
bellard 已提交
3230
/* crorc */
3231
GEN_CRLOGIC(orc, 0x0D);
B
bellard 已提交
3232
/* crxor */
3233
GEN_CRLOGIC(xor, 0x06);
B
bellard 已提交
3234 3235 3236 3237 3238 3239 3240 3241 3242
/* mcrf */
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
{
    gen_op_load_crf_T0(crfS(ctx->opcode));
    gen_op_store_T0_crf(crfD(ctx->opcode));
}

/***                           System linkage                              ***/
/* rfi (supervisor only) */
3243
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
B
bellard 已提交
3244
{
3245
#if defined(CONFIG_USER_ONLY)
3246
    GEN_EXCP_PRIVOPC(ctx);
3247 3248
#else
    /* Restore CPU state */
3249
    if (unlikely(!ctx->supervisor)) {
3250
        GEN_EXCP_PRIVOPC(ctx);
3251
        return;
3252
    }
3253
    gen_op_rfi();
3254
    GEN_SYNC(ctx);
3255
#endif
B
bellard 已提交
3256 3257
}

J
j_mayer 已提交
3258
#if defined(TARGET_PPC64)
3259
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
J
j_mayer 已提交
3260 3261
{
#if defined(CONFIG_USER_ONLY)
3262
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3263 3264 3265
#else
    /* Restore CPU state */
    if (unlikely(!ctx->supervisor)) {
3266
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
3267 3268
        return;
    }
3269
    gen_op_rfid();
3270
    GEN_SYNC(ctx);
J
j_mayer 已提交
3271 3272 3273 3274
#endif
}
#endif

3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291
#if defined(TARGET_PPC64H)
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64B)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    /* Restore CPU state */
    if (unlikely(ctx->supervisor <= 1)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_hrfid();
    GEN_SYNC(ctx);
#endif
}
#endif

B
bellard 已提交
3292
/* sc */
3293
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
B
bellard 已提交
3294
{
3295 3296 3297
    uint32_t lev;

    lev = (ctx->opcode >> 5) & 0x7F;
3298
#if defined(CONFIG_USER_ONLY)
3299
    GEN_EXCP(ctx, POWERPC_EXCP_SYSCALL_USER, lev);
3300
#else
3301
    GEN_EXCP(ctx, POWERPC_EXCP_SYSCALL, lev);
3302
#endif
B
bellard 已提交
3303 3304 3305 3306
}

/***                                Trap                                   ***/
/* tw */
3307
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
B
bellard 已提交
3308
{
3309 3310
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3311
    /* Update the nip since this might generate a trap exception */
3312
    gen_update_nip(ctx, ctx->nip);
3313
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3314 3315 3316 3317 3318
}

/* twi */
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
{
3319
    gen_op_load_gpr_T0(rA(ctx->opcode));
3320 3321 3322
    gen_set_T1(SIMM(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
3323
    gen_op_tw(TO(ctx->opcode));
B
bellard 已提交
3324 3325
}

3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347
#if defined(TARGET_PPC64)
/* td */
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    /* Update the nip since this might generate a trap exception */
    gen_update_nip(ctx, ctx->nip);
    gen_op_td(TO(ctx->opcode));
}

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

B
bellard 已提交
3348 3349 3350 3351 3352 3353
/***                          Processor control                            ***/
/* mcrxr */
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
{
    gen_op_load_xer_cr();
    gen_op_store_T0_crf(crfD(ctx->opcode));
J
j_mayer 已提交
3354 3355
    gen_op_clear_xer_ov();
    gen_op_clear_xer_ca();
B
bellard 已提交
3356 3357 3358
}

/* mfcr */
3359
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
B
bellard 已提交
3360
{
3361
    uint32_t crm, crn;
3362

3363 3364 3365 3366 3367 3368
    if (likely(ctx->opcode & 0x00100000)) {
        crm = CRM(ctx->opcode);
        if (likely((crm ^ (crm - 1)) == 0)) {
            crn = ffs(crm);
            gen_op_load_cro(7 - crn);
        }
3369 3370 3371
    } else {
        gen_op_load_cr();
    }
B
bellard 已提交
3372 3373 3374 3375 3376 3377
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

/* mfmsr */
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
{
3378
#if defined(CONFIG_USER_ONLY)
3379
    GEN_EXCP_PRIVREG(ctx);
3380
#else
3381
    if (unlikely(!ctx->supervisor)) {
3382
        GEN_EXCP_PRIVREG(ctx);
3383
        return;
3384
    }
B
bellard 已提交
3385 3386
    gen_op_load_msr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
3387
#endif
B
bellard 已提交
3388 3389
}

3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
#if 0
#define SPR_NOACCESS ((void *)(-1))
#else
static void spr_noaccess (void *opaque, int sprn)
{
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
    printf("ERROR: try to access SPR %d !\n", sprn);
}
#define SPR_NOACCESS (&spr_noaccess)
#endif

B
bellard 已提交
3401
/* mfspr */
3402
static inline void gen_op_mfspr (DisasContext *ctx)
B
bellard 已提交
3403
{
3404
    void (*read_cb)(void *opaque, int sprn);
B
bellard 已提交
3405 3406
    uint32_t sprn = SPR(ctx->opcode);

3407
#if !defined(CONFIG_USER_ONLY)
3408 3409 3410 3411 3412
#if defined(TARGET_PPC64H)
    if (ctx->supervisor == 2)
        read_cb = ctx->spr_cb[sprn].hea_read;
    else
#endif
3413 3414 3415
    if (ctx->supervisor)
        read_cb = ctx->spr_cb[sprn].oea_read;
    else
3416
#endif
3417
        read_cb = ctx->spr_cb[sprn].uea_read;
3418 3419
    if (likely(read_cb != NULL)) {
        if (likely(read_cb != SPR_NOACCESS)) {
3420 3421 3422 3423
            (*read_cb)(ctx, sprn);
            gen_op_store_T0_gpr(rD(ctx->opcode));
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3424
            if (loglevel != 0) {
B
blueswir1 已提交
3425
                fprintf(logfile, "Trying to read privileged spr %d %03x\n",
3426 3427
                        sprn, sprn);
            }
B
blueswir1 已提交
3428
            printf("Trying to read privileged spr %d %03x\n", sprn, sprn);
3429
            GEN_EXCP_PRIVREG(ctx);
B
bellard 已提交
3430
        }
3431 3432
    } else {
        /* Not defined */
J
j_mayer 已提交
3433
        if (loglevel != 0) {
3434 3435 3436
            fprintf(logfile, "Trying to read invalid spr %d %03x\n",
                    sprn, sprn);
        }
3437
        printf("Trying to read invalid spr %d %03x\n", sprn, sprn);
3438 3439
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3440 3441 3442
    }
}

3443
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
B
bellard 已提交
3444
{
3445
    gen_op_mfspr(ctx);
3446
}
3447 3448

/* mftb */
3449
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3450 3451
{
    gen_op_mfspr(ctx);
B
bellard 已提交
3452 3453 3454
}

/* mtcrf */
3455
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
B
bellard 已提交
3456
{
3457
    uint32_t crm, crn;
3458

B
bellard 已提交
3459
    gen_op_load_gpr_T0(rS(ctx->opcode));
3460 3461 3462 3463 3464 3465 3466 3467 3468
    crm = CRM(ctx->opcode);
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
        crn = ffs(crm);
        gen_op_srli_T0(crn * 4);
        gen_op_andi_T0(0xF);
        gen_op_store_cro(7 - crn);
    } else {
        gen_op_store_cr(crm);
    }
B
bellard 已提交
3469 3470 3471
}

/* mtmsr */
J
j_mayer 已提交
3472
#if defined(TARGET_PPC64)
3473
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
J
j_mayer 已提交
3474 3475
{
#if defined(CONFIG_USER_ONLY)
3476
    GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3477 3478
#else
    if (unlikely(!ctx->supervisor)) {
3479
        GEN_EXCP_PRIVREG(ctx);
J
j_mayer 已提交
3480 3481 3482
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
3483 3484 3485 3486
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3487 3488 3489 3490
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
3491 3492 3493 3494
        gen_update_nip(ctx, ctx->nip);
        gen_op_store_msr();
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsr is not always defined as context-synchronizing */
3495
        ctx->exception = POWERPC_EXCP_STOP;
3496
    }
J
j_mayer 已提交
3497 3498 3499 3500
#endif
}
#endif

B
bellard 已提交
3501 3502
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
{
3503
#if defined(CONFIG_USER_ONLY)
3504
    GEN_EXCP_PRIVREG(ctx);
3505
#else
3506
    if (unlikely(!ctx->supervisor)) {
3507
        GEN_EXCP_PRIVREG(ctx);
3508
        return;
3509
    }
B
bellard 已提交
3510
    gen_op_load_gpr_T0(rS(ctx->opcode));
3511 3512 3513 3514
    if (ctx->opcode & 0x00010000) {
        /* Special form that does not need any synchronisation */
        gen_op_update_riee();
    } else {
3515 3516 3517 3518
        /* XXX: we need to update nip before the store
         *      if we enter power saving mode, we will exit the loop
         *      directly from ppc_store_msr
         */
3519
        gen_update_nip(ctx, ctx->nip);
3520
#if defined(TARGET_PPC64)
3521 3522 3523
        if (!ctx->sf_mode)
            gen_op_store_msr_32();
        else
3524
#endif
3525 3526 3527
            gen_op_store_msr();
        /* Must stop the translation as machine state (may have) changed */
        /* Note that mtmsrd is not always defined as context-synchronizing */
3528
        ctx->exception = POWERPC_EXCP_STOP;
3529
    }
3530
#endif
B
bellard 已提交
3531 3532 3533 3534 3535
}

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

3539
#if !defined(CONFIG_USER_ONLY)
3540 3541 3542 3543 3544
#if defined(TARGET_PPC64H)
    if (ctx->supervisor == 2)
        write_cb = ctx->spr_cb[sprn].hea_write;
    else
#endif
3545 3546 3547
    if (ctx->supervisor)
        write_cb = ctx->spr_cb[sprn].oea_write;
    else
3548
#endif
3549
        write_cb = ctx->spr_cb[sprn].uea_write;
3550 3551
    if (likely(write_cb != NULL)) {
        if (likely(write_cb != SPR_NOACCESS)) {
3552 3553 3554 3555
            gen_op_load_gpr_T0(rS(ctx->opcode));
            (*write_cb)(ctx, sprn);
        } else {
            /* Privilege exception */
J
j_mayer 已提交
3556
            if (loglevel != 0) {
B
blueswir1 已提交
3557
                fprintf(logfile, "Trying to write privileged spr %d %03x\n",
3558 3559
                        sprn, sprn);
            }
B
blueswir1 已提交
3560
            printf("Trying to write privileged spr %d %03x\n", sprn, sprn);
3561
            GEN_EXCP_PRIVREG(ctx);
3562
        }
3563 3564
    } else {
        /* Not defined */
J
j_mayer 已提交
3565
        if (loglevel != 0) {
3566 3567 3568
            fprintf(logfile, "Trying to write invalid spr %d %03x\n",
                    sprn, sprn);
        }
3569
        printf("Trying to write invalid spr %d %03x\n", sprn, sprn);
3570 3571
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
B
bellard 已提交
3572 3573 3574 3575 3576 3577
    }
}

/***                         Cache management                              ***/
/* For now, all those will be implemented as nop:
 * this is valid, regarding the PowerPC specs...
3578
 * We just have to flush tb while invalidating instruction cache lines...
B
bellard 已提交
3579 3580
 */
/* dcbf */
3581
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
B
bellard 已提交
3582
{
3583
    gen_addr_reg_index(ctx);
3584
    op_ldst(lbz);
B
bellard 已提交
3585 3586 3587
}

/* dcbi (Supervisor only) */
3588
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3589
{
3590
#if defined(CONFIG_USER_ONLY)
3591
    GEN_EXCP_PRIVOPC(ctx);
3592
#else
3593
    if (unlikely(!ctx->supervisor)) {
3594
        GEN_EXCP_PRIVOPC(ctx);
3595
        return;
3596
    }
3597 3598 3599
    gen_addr_reg_index(ctx);
    /* XXX: specification says this should be treated as a store by the MMU */
    //op_ldst(lbz);
3600 3601
    op_ldst(stb);
#endif
B
bellard 已提交
3602 3603 3604
}

/* dcdst */
3605
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3606
{
3607 3608
    /* XXX: specification say this is treated as a load by the MMU */
    gen_addr_reg_index(ctx);
3609
    op_ldst(lbz);
B
bellard 已提交
3610 3611 3612
}

/* dcbt */
3613
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
B
bellard 已提交
3614
{
3615
    /* interpreted as no-op */
3616 3617 3618
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3619 3620 3621
}

/* dcbtst */
3622
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
B
bellard 已提交
3623
{
3624
    /* interpreted as no-op */
3625 3626 3627
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3628 3629 3630
}

/* dcbz */
3631
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
3632
#if defined(CONFIG_USER_ONLY)
3633
/* User-mode only */
3634 3635 3636 3637
static GenOpFunc *gen_op_dcbz[4][4] = {
    {
        &gen_op_dcbz_l32_raw,
        &gen_op_dcbz_l32_raw,
3638
#if defined(TARGET_PPC64)
3639 3640
        &gen_op_dcbz_l32_64_raw,
        &gen_op_dcbz_l32_64_raw,
3641
#endif
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666
    },
    {
        &gen_op_dcbz_l64_raw,
        &gen_op_dcbz_l64_raw,
#if defined(TARGET_PPC64)
        &gen_op_dcbz_l64_64_raw,
        &gen_op_dcbz_l64_64_raw,
#endif
    },
    {
        &gen_op_dcbz_l128_raw,
        &gen_op_dcbz_l128_raw,
#if defined(TARGET_PPC64)
        &gen_op_dcbz_l128_64_raw,
        &gen_op_dcbz_l128_64_raw,
#endif
    },
    {
        &gen_op_dcbz_raw,
        &gen_op_dcbz_raw,
#if defined(TARGET_PPC64)
        &gen_op_dcbz_64_raw,
        &gen_op_dcbz_64_raw,
#endif
    },
3667 3668
};
#else
3669 3670
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
static GenOpFunc *gen_op_dcbz[4][12] = {
    {
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_64_user,
        &gen_op_dcbz_l32_64_user,
        &gen_op_dcbz_l32_kernel,
        &gen_op_dcbz_l32_kernel,
        &gen_op_dcbz_l32_64_kernel,
        &gen_op_dcbz_l32_64_kernel,
#if defined(TARGET_PPC64H)
        &gen_op_dcbz_l32_hypv,
        &gen_op_dcbz_l32_hypv,
        &gen_op_dcbz_l32_64_hypv,
        &gen_op_dcbz_l32_64_hypv,
#endif
    },
    {
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_64_user,
        &gen_op_dcbz_l64_64_user,
        &gen_op_dcbz_l64_kernel,
        &gen_op_dcbz_l64_kernel,
        &gen_op_dcbz_l64_64_kernel,
        &gen_op_dcbz_l64_64_kernel,
3697
#if defined(TARGET_PPC64H)
3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733
        &gen_op_dcbz_l64_hypv,
        &gen_op_dcbz_l64_hypv,
        &gen_op_dcbz_l64_64_hypv,
        &gen_op_dcbz_l64_64_hypv,
#endif
    },
    {
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_64_user,
        &gen_op_dcbz_l128_64_user,
        &gen_op_dcbz_l128_kernel,
        &gen_op_dcbz_l128_kernel,
        &gen_op_dcbz_l128_64_kernel,
        &gen_op_dcbz_l128_64_kernel,
#if defined(TARGET_PPC64H)
        &gen_op_dcbz_l128_hypv,
        &gen_op_dcbz_l128_hypv,
        &gen_op_dcbz_l128_64_hypv,
        &gen_op_dcbz_l128_64_hypv,
#endif
    },
    {
        &gen_op_dcbz_user,
        &gen_op_dcbz_user,
        &gen_op_dcbz_64_user,
        &gen_op_dcbz_64_user,
        &gen_op_dcbz_kernel,
        &gen_op_dcbz_kernel,
        &gen_op_dcbz_64_kernel,
        &gen_op_dcbz_64_kernel,
#if defined(TARGET_PPC64H)
        &gen_op_dcbz_hypv,
        &gen_op_dcbz_hypv,
        &gen_op_dcbz_64_hypv,
        &gen_op_dcbz_64_hypv,
3734
#endif
3735
    },
3736
};
3737
#else
3738
/* Full system - 32 bits mode */
3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763
static GenOpFunc *gen_op_dcbz[4][4] = {
    {
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_user,
        &gen_op_dcbz_l32_kernel,
        &gen_op_dcbz_l32_kernel,
    },
    {
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_user,
        &gen_op_dcbz_l64_kernel,
        &gen_op_dcbz_l64_kernel,
    },
    {
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_user,
        &gen_op_dcbz_l128_kernel,
        &gen_op_dcbz_l128_kernel,
    },
    {
        &gen_op_dcbz_user,
        &gen_op_dcbz_user,
        &gen_op_dcbz_kernel,
        &gen_op_dcbz_kernel,
    },
3764 3765
};
#endif
3766
#endif
3767

3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789
static inline void handler_dcbz (DisasContext *ctx, int dcache_line_size)
{
    int n;

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

GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
B
bellard 已提交
3790
{
3791
    gen_addr_reg_index(ctx);
3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802
    handler_dcbz(ctx, ctx->dcache_line_size);
    gen_op_check_reservation();
}

GEN_HANDLER(dcbz_970, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
{
    gen_addr_reg_index(ctx);
    if (ctx->opcode & 0x00200000)
        handler_dcbz(ctx, ctx->dcache_line_size);
    else
        handler_dcbz(ctx, -1);
B
bellard 已提交
3803
    gen_op_check_reservation();
B
bellard 已提交
3804 3805 3806
}

/* icbi */
3807 3808
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
3809
/* User-mode only */
3810 3811 3812
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_raw,
    &gen_op_icbi_raw,
3813
#if defined(TARGET_PPC64)
3814 3815
    &gen_op_icbi_64_raw,
    &gen_op_icbi_64_raw,
3816
#endif
3817 3818
};
#else
3819 3820
/* Full system - 64 bits mode */
#if defined(TARGET_PPC64)
3821 3822 3823 3824 3825
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_user,
    &gen_op_icbi_user,
    &gen_op_icbi_64_user,
    &gen_op_icbi_64_user,
3826 3827
    &gen_op_icbi_kernel,
    &gen_op_icbi_kernel,
3828 3829
    &gen_op_icbi_64_kernel,
    &gen_op_icbi_64_kernel,
3830 3831 3832 3833 3834
#if defined(TARGET_PPC64H)
    &gen_op_icbi_hypv,
    &gen_op_icbi_hypv,
    &gen_op_icbi_64_hypv,
    &gen_op_icbi_64_hypv,
3835 3836 3837
#endif
};
#else
3838
/* Full system - 32 bits mode */
3839 3840 3841 3842 3843 3844 3845 3846
static GenOpFunc *gen_op_icbi[] = {
    &gen_op_icbi_user,
    &gen_op_icbi_user,
    &gen_op_icbi_kernel,
    &gen_op_icbi_kernel,
};
#endif
#endif
3847

3848
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
B
bellard 已提交
3849
{
3850 3851
    /* NIP cannot be restored if the memory exception comes from an helper */
    gen_update_nip(ctx, ctx->nip - 4);
3852
    gen_addr_reg_index(ctx);
3853
    op_icbi();
B
bellard 已提交
3854 3855 3856 3857
}

/* Optional: */
/* dcba */
3858
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
B
bellard 已提交
3859
{
3860 3861 3862 3863
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a store by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
3864 3865 3866 3867 3868 3869 3870
}

/***                    Segment register manipulation                      ***/
/* Supervisor only: */
/* mfsr */
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
{
3871
#if defined(CONFIG_USER_ONLY)
3872
    GEN_EXCP_PRIVREG(ctx);
3873
#else
3874
    if (unlikely(!ctx->supervisor)) {
3875
        GEN_EXCP_PRIVREG(ctx);
3876
        return;
3877
    }
3878 3879
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_load_sr();
3880 3881
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3882 3883 3884
}

/* mfsrin */
3885
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3886
{
3887
#if defined(CONFIG_USER_ONLY)
3888
    GEN_EXCP_PRIVREG(ctx);
3889
#else
3890
    if (unlikely(!ctx->supervisor)) {
3891
        GEN_EXCP_PRIVREG(ctx);
3892
        return;
3893 3894
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
3895 3896
    gen_op_srli_T1(28);
    gen_op_load_sr();
3897 3898
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
B
bellard 已提交
3899 3900 3901
}

/* mtsr */
B
bellard 已提交
3902
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
B
bellard 已提交
3903
{
3904
#if defined(CONFIG_USER_ONLY)
3905
    GEN_EXCP_PRIVREG(ctx);
3906
#else
3907
    if (unlikely(!ctx->supervisor)) {
3908
        GEN_EXCP_PRIVREG(ctx);
3909
        return;
3910 3911
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
3912 3913
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_store_sr();
3914
#endif
B
bellard 已提交
3915 3916 3917
}

/* mtsrin */
3918
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
B
bellard 已提交
3919
{
3920
#if defined(CONFIG_USER_ONLY)
3921
    GEN_EXCP_PRIVREG(ctx);
3922
#else
3923
    if (unlikely(!ctx->supervisor)) {
3924
        GEN_EXCP_PRIVREG(ctx);
3925
        return;
3926 3927 3928
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
3929 3930
    gen_op_srli_T1(28);
    gen_op_store_sr();
3931
#endif
B
bellard 已提交
3932 3933
}

3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002
#if defined(TARGET_PPC64)
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
/* mfsr */
GEN_HANDLER(mfsr_64b, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_load_slb();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mfsrin */
GEN_HANDLER(mfsrin_64b, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT_64B)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_srli_T1(28);
    gen_op_load_slb();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mtsr */
GEN_HANDLER(mtsr_64b, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SR(ctx->opcode));
    gen_op_store_slb();
#endif
}

/* mtsrin */
GEN_HANDLER(mtsrin_64b, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT_64B)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVREG(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVREG(ctx);
        return;
    }
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_srli_T1(28);
    gen_op_store_slb();
#endif
}
#endif /* defined(TARGET_PPC64) */

B
bellard 已提交
4003 4004 4005
/***                      Lookaside buffer management                      ***/
/* Optional & supervisor only: */
/* tlbia */
4006
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
B
bellard 已提交
4007
{
4008
#if defined(CONFIG_USER_ONLY)
4009
    GEN_EXCP_PRIVOPC(ctx);
4010
#else
4011
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
4012
        if (loglevel != 0)
4013
            fprintf(logfile, "%s: ! supervisor\n", __func__);
4014
        GEN_EXCP_PRIVOPC(ctx);
4015
        return;
4016 4017 4018
    }
    gen_op_tlbia();
#endif
B
bellard 已提交
4019 4020 4021
}

/* tlbie */
4022
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
B
bellard 已提交
4023
{
4024
#if defined(CONFIG_USER_ONLY)
4025
    GEN_EXCP_PRIVOPC(ctx);
4026
#else
4027
    if (unlikely(!ctx->supervisor)) {
4028
        GEN_EXCP_PRIVOPC(ctx);
4029
        return;
4030 4031
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
4032 4033 4034 4035 4036 4037
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4038
#endif
B
bellard 已提交
4039 4040 4041
}

/* tlbsync */
4042
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
B
bellard 已提交
4043
{
4044
#if defined(CONFIG_USER_ONLY)
4045
    GEN_EXCP_PRIVOPC(ctx);
4046
#else
4047
    if (unlikely(!ctx->supervisor)) {
4048
        GEN_EXCP_PRIVOPC(ctx);
4049
        return;
4050 4051 4052 4053
    }
    /* This has no effect: it should ensure that all previous
     * tlbie have completed
     */
4054
    GEN_STOP(ctx);
4055
#endif
B
bellard 已提交
4056 4057
}

J
j_mayer 已提交
4058 4059 4060 4061 4062
#if defined(TARGET_PPC64)
/* slbia */
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4063
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4064 4065
#else
    if (unlikely(!ctx->supervisor)) {
J
j_mayer 已提交
4066
        if (loglevel != 0)
J
j_mayer 已提交
4067
            fprintf(logfile, "%s: ! supervisor\n", __func__);
4068
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4069 4070 4071 4072 4073 4074 4075 4076 4077 4078
        return;
    }
    gen_op_slbia();
#endif
}

/* slbie */
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
{
#if defined(CONFIG_USER_ONLY)
4079
    GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4080 4081
#else
    if (unlikely(!ctx->supervisor)) {
4082
        GEN_EXCP_PRIVOPC(ctx);
J
j_mayer 已提交
4083 4084 4085 4086 4087 4088 4089 4090
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_slbie();
#endif
}
#endif

B
bellard 已提交
4091 4092
/***                              External control                         ***/
/* Optional: */
4093 4094
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4095
#if defined(CONFIG_USER_ONLY)
4096
/* User-mode only */
4097 4098 4099
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_raw,
    &gen_op_eciwx_le_raw,
4100
#if defined(TARGET_PPC64)
4101 4102
    &gen_op_eciwx_64_raw,
    &gen_op_eciwx_le_64_raw,
4103
#endif
4104 4105 4106 4107
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_raw,
    &gen_op_ecowx_le_raw,
4108
#if defined(TARGET_PPC64)
4109 4110
    &gen_op_ecowx_64_raw,
    &gen_op_ecowx_le_64_raw,
4111
#endif
4112 4113
};
#else
4114 4115
#if defined(TARGET_PPC64)
/* Full system - 64 bits mode */
4116 4117
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_user,
4118
    &gen_op_eciwx_le_user,
4119 4120
    &gen_op_eciwx_64_user,
    &gen_op_eciwx_le_64_user,
4121 4122
    &gen_op_eciwx_kernel,
    &gen_op_eciwx_le_kernel,
4123 4124
    &gen_op_eciwx_64_kernel,
    &gen_op_eciwx_le_64_kernel,
4125 4126 4127 4128 4129 4130
#if defined(TARGET_PPC64H)
    &gen_op_eciwx_hypv,
    &gen_op_eciwx_le_hypv,
    &gen_op_eciwx_64_hypv,
    &gen_op_eciwx_le_64_hypv,
#endif
4131 4132 4133
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_user,
4134
    &gen_op_ecowx_le_user,
4135 4136
    &gen_op_ecowx_64_user,
    &gen_op_ecowx_le_64_user,
4137 4138
    &gen_op_ecowx_kernel,
    &gen_op_ecowx_le_kernel,
4139 4140
    &gen_op_ecowx_64_kernel,
    &gen_op_ecowx_le_64_kernel,
4141 4142 4143 4144 4145
#if defined(TARGET_PPC64H)
    &gen_op_ecowx_hypv,
    &gen_op_ecowx_le_hypv,
    &gen_op_ecowx_64_hypv,
    &gen_op_ecowx_le_64_hypv,
4146
#endif
4147 4148
};
#else
4149
/* Full system - 32 bits mode */
4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163
static GenOpFunc *gen_op_eciwx[] = {
    &gen_op_eciwx_user,
    &gen_op_eciwx_le_user,
    &gen_op_eciwx_kernel,
    &gen_op_eciwx_le_kernel,
};
static GenOpFunc *gen_op_ecowx[] = {
    &gen_op_ecowx_user,
    &gen_op_ecowx_le_user,
    &gen_op_ecowx_kernel,
    &gen_op_ecowx_le_kernel,
};
#endif
#endif
4164

4165
/* eciwx */
B
bellard 已提交
4166 4167
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
{
4168
    /* Should check EAR[E] & alignment ! */
4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204
    gen_addr_reg_index(ctx);
    op_eciwx();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

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

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

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

/* clcs */
4205
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_POWER_clcs();
    gen_op_store_T0_gpr(rD(ctx->opcode));
}

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

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

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

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

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

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

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

/* As lscbx load from memory byte after byte, it's always endian safe */
4288
#define op_POWER_lscbx(start, ra, rb)                                         \
4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
#if defined(CONFIG_USER_ONLY)
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
    &gen_op_POWER_lscbx_raw,
    &gen_op_POWER_lscbx_raw,
};
#else
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
    &gen_op_POWER_lscbx_user,
    &gen_op_POWER_lscbx_user,
    &gen_op_POWER_lscbx_kernel,
    &gen_op_POWER_lscbx_kernel,
};
#endif

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

    gen_addr_reg_index(ctx);
    if (ra == 0) {
        ra = rb;
    }
    /* NIP cannot be restored if the memory exception comes from an helper */
4315
    gen_update_nip(ctx, ctx->nip - 4);
4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482
    gen_op_load_xer_bc();
    gen_op_load_xer_cmp();
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
    gen_op_store_xer_bc();
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4483
/* sraiq - sraiq. */
4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_set_T1(SH(ctx->opcode));
    gen_op_POWER_sraq();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    if (unlikely(Rc(ctx->opcode) != 0))
        gen_set_Rc0(ctx);
}

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

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

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

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

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

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

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

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

/* PowerPC 602 specific instructions */
/* dsa  */
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4588
    GEN_EXCP_INVAL(ctx);
4589 4590 4591 4592 4593 4594
}

/* esa */
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
{
    /* XXX: TODO */
4595
    GEN_EXCP_INVAL(ctx);
4596 4597 4598 4599 4600 4601
}

/* mfrom */
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
{
#if defined(CONFIG_USER_ONLY)
4602
    GEN_EXCP_PRIVOPC(ctx);
4603 4604
#else
    if (unlikely(!ctx->supervisor)) {
4605
        GEN_EXCP_PRIVOPC(ctx);
4606 4607 4608 4609 4610 4611 4612 4613 4614 4615
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_602_mfrom();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* 602 - 603 - G2 TLB management */
/* tlbld */
4616
GEN_HANDLER(tlbld_6xx, 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4617 4618
{
#if defined(CONFIG_USER_ONLY)
4619
    GEN_EXCP_PRIVOPC(ctx);
4620 4621
#else
    if (unlikely(!ctx->supervisor)) {
4622
        GEN_EXCP_PRIVOPC(ctx);
4623 4624 4625 4626 4627 4628 4629 4630
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbld();
#endif
}

/* tlbli */
4631
GEN_HANDLER(tlbli_6xx, 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4632 4633
{
#if defined(CONFIG_USER_ONLY)
4634
    GEN_EXCP_PRIVOPC(ctx);
4635 4636
#else
    if (unlikely(!ctx->supervisor)) {
4637
        GEN_EXCP_PRIVOPC(ctx);
4638 4639 4640 4641 4642 4643 4644
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_6xx_tlbli();
#endif
}

4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675
/* 74xx TLB management */
/* tlbld */
GEN_HANDLER(tlbld_74xx, 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_74xx_tlbld();
#endif
}

/* tlbli */
GEN_HANDLER(tlbli_74xx, 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
{
#if defined(CONFIG_USER_ONLY)
    GEN_EXCP_PRIVOPC(ctx);
#else
    if (unlikely(!ctx->supervisor)) {
        GEN_EXCP_PRIVOPC(ctx);
        return;
    }
    gen_op_load_gpr_T0(rB(ctx->opcode));
    gen_op_74xx_tlbli();
#endif
}

4676 4677 4678 4679 4680 4681 4682 4683 4684 4685
/* POWER instructions not in PowerPC 601 */
/* clf */
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
{
    /* Cache line flush: implemented as no-op */
}

/* cli */
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
{
B
blueswir1 已提交
4686
    /* Cache line invalidate: privileged and treated as no-op */
4687
#if defined(CONFIG_USER_ONLY)
4688
    GEN_EXCP_PRIVOPC(ctx);
4689 4690
#else
    if (unlikely(!ctx->supervisor)) {
4691
        GEN_EXCP_PRIVOPC(ctx);
4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705
        return;
    }
#endif
}

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

GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4706
    GEN_EXCP_PRIVOPC(ctx);
4707 4708
#else
    if (unlikely(!ctx->supervisor)) {
4709
        GEN_EXCP_PRIVOPC(ctx);
4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725
        return;
    }
    int ra = rA(ctx->opcode);
    int rd = rD(ctx->opcode);

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

GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4726
    GEN_EXCP_PRIVOPC(ctx);
4727 4728
#else
    if (unlikely(!ctx->supervisor)) {
4729
        GEN_EXCP_PRIVOPC(ctx);
4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740
        return;
    }
    gen_addr_reg_index(ctx);
    gen_op_POWER_rac();
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
{
#if defined(CONFIG_USER_ONLY)
4741
    GEN_EXCP_PRIVOPC(ctx);
4742 4743
#else
    if (unlikely(!ctx->supervisor)) {
4744
        GEN_EXCP_PRIVOPC(ctx);
4745 4746 4747
        return;
    }
    gen_op_POWER_rfsvc();
4748
    GEN_SYNC(ctx);
4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785
#endif
}

/* svc is not implemented for now */

/* POWER2 specific instructions */
/* Quad manipulation (load/store two floats at a time) */
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
static GenOpFunc *gen_op_POWER2_lfq[] = {
    &gen_op_POWER2_lfq_le_raw,
    &gen_op_POWER2_lfq_raw,
};
static GenOpFunc *gen_op_POWER2_stfq[] = {
    &gen_op_POWER2_stfq_le_raw,
    &gen_op_POWER2_stfq_raw,
};
#else
static GenOpFunc *gen_op_POWER2_lfq[] = {
    &gen_op_POWER2_lfq_le_user,
    &gen_op_POWER2_lfq_user,
    &gen_op_POWER2_lfq_le_kernel,
    &gen_op_POWER2_lfq_kernel,
};
static GenOpFunc *gen_op_POWER2_stfq[] = {
    &gen_op_POWER2_stfq_le_user,
    &gen_op_POWER2_stfq_user,
    &gen_op_POWER2_stfq_le_kernel,
    &gen_op_POWER2_stfq_kernel,
};
#endif

/* lfq */
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4786
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4787
    gen_addr_imm_index(ctx, 0);
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4799
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4800
    gen_addr_imm_index(ctx, 0);
4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4814
    gen_update_nip(ctx, ctx->nip - 4);
4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826
    gen_addr_reg_index(ctx);
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* lfqx */
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4827
    gen_update_nip(ctx, ctx->nip - 4);
4828 4829 4830 4831 4832 4833 4834 4835 4836 4837
    gen_addr_reg_index(ctx);
    op_POWER2_lfq();
    gen_op_store_FT0_fpr(rD(ctx->opcode));
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
}

/* stfq */
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4838
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4839
    gen_addr_imm_index(ctx, 0);
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4851
    gen_update_nip(ctx, ctx->nip - 4);
J
j_mayer 已提交
4852
    gen_addr_imm_index(ctx, 0);
4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

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

    /* NIP cannot be restored if the memory exception comes from an helper */
4866
    gen_update_nip(ctx, ctx->nip - 4);
4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878
    gen_addr_reg_index(ctx);
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
    if (ra != 0)
        gen_op_store_T0_gpr(ra);
}

/* stfqx */
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
{
    /* NIP cannot be restored if the memory exception comes from an helper */
4879
    gen_update_nip(ctx, ctx->nip - 4);
4880 4881 4882 4883 4884 4885 4886
    gen_addr_reg_index(ctx);
    gen_op_load_fpr_FT0(rS(ctx->opcode));
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
    op_POWER2_stfq();
}

/* BookE specific instructions */
4887
/* XXX: not implemented on 440 ? */
4888
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_BOOKE_EXT)
4889 4890
{
    /* XXX: TODO */
4891
    GEN_EXCP_INVAL(ctx);
4892 4893
}

4894
/* XXX: not implemented on 440 ? */
4895
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_BOOKE_EXT)
4896 4897
{
#if defined(CONFIG_USER_ONLY)
4898
    GEN_EXCP_PRIVOPC(ctx);
4899 4900
#else
    if (unlikely(!ctx->supervisor)) {
4901
        GEN_EXCP_PRIVOPC(ctx);
4902 4903 4904 4905
        return;
    }
    gen_addr_reg_index(ctx);
    /* Use the same micro-ops as for tlbie */
4906 4907 4908 4909 4910 4911
#if defined(TARGET_PPC64)
    if (ctx->sf_mode)
        gen_op_tlbie_64();
    else
#endif
        gen_op_tlbie();
4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995
#endif
}

/* All 405 MAC instructions are translated here */
static inline void gen_405_mulladd_insn (DisasContext *ctx, int opc2, int opc3,
                                         int ra, int rb, int rt, int Rc)
{
    gen_op_load_gpr_T0(ra);
    gen_op_load_gpr_T1(rb);
    switch (opc3 & 0x0D) {
    case 0x05:
        /* macchw    - macchw.    - macchwo   - macchwo.   */
        /* macchws   - macchws.   - macchwso  - macchwso.  */
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
        /* mulchw - mulchw. */
        gen_op_405_mulchw();
        break;
    case 0x04:
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
        /* mulchwu - mulchwu. */
        gen_op_405_mulchwu();
        break;
    case 0x01:
        /* machhw    - machhw.    - machhwo   - machhwo.   */
        /* machhws   - machhws.   - machhwso  - machhwso.  */
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
        /* mulhhw - mulhhw. */
        gen_op_405_mulhhw();
        break;
    case 0x00:
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
        /* mulhhwu - mulhhwu. */
        gen_op_405_mulhhwu();
        break;
    case 0x0D:
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
        /* mullhw - mullhw. */
        gen_op_405_mullhw();
        break;
    case 0x0C:
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
        /* mullhwu - mullhwu. */
        gen_op_405_mullhwu();
        break;
    }
    if (opc2 & 0x02) {
        /* nmultiply-and-accumulate (0x0E) */
        gen_op_neg();
    }
    if (opc2 & 0x04) {
        /* (n)multiply-and-accumulate (0x0C - 0x0E) */
        gen_op_load_gpr_T2(rt);
        gen_op_move_T1_T0();
        gen_op_405_add_T0_T2();
    }
    if (opc3 & 0x10) {
        /* Check overflow */
        if (opc3 & 0x01)
            gen_op_405_check_ov();
        else
            gen_op_405_check_ovu();
    }
    if (opc3 & 0x02) {
        /* Saturate */
        if (opc3 & 0x01)
            gen_op_405_check_sat();
        else
            gen_op_405_check_satu();
    }
    gen_op_store_T0_gpr(rt);
    if (unlikely(Rc) != 0) {
        /* Update Rc0 */
        gen_set_Rc0(ctx);
    }
}

4996 4997
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
4998 4999 5000 5001 5002 5003
{                                                                             \
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
}

/* macchw    - macchw.    */
5004
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5005
/* macchwo   - macchwo.   */
5006
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5007
/* macchws   - macchws.   */
5008
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5009
/* macchwso  - macchwso.  */
5010
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5011
/* macchwsu  - macchwsu.  */
5012
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5013
/* macchwsuo - macchwsuo. */
5014
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5015
/* macchwu   - macchwu.   */
5016
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5017
/* macchwuo  - macchwuo.  */
5018
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5019
/* machhw    - machhw.    */
5020
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5021
/* machhwo   - machhwo.   */
5022
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5023
/* machhws   - machhws.   */
5024
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5025
/* machhwso  - machhwso.  */
5026
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5027
/* machhwsu  - machhwsu.  */
5028
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5029
/* machhwsuo - machhwsuo. */
5030
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5031
/* machhwu   - machhwu.   */
5032
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5033
/* machhwuo  - machhwuo.  */
5034
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5035
/* maclhw    - maclhw.    */
5036
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5037
/* maclhwo   - maclhwo.   */
5038
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5039
/* maclhws   - maclhws.   */
5040
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5041
/* maclhwso  - maclhwso.  */
5042
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5043
/* maclhwu   - maclhwu.   */
5044
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5045
/* maclhwuo  - maclhwuo.  */
5046
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5047
/* maclhwsu  - maclhwsu.  */
5048
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5049
/* maclhwsuo - maclhwsuo. */
5050
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5051
/* nmacchw   - nmacchw.   */
5052
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5053
/* nmacchwo  - nmacchwo.  */
5054
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5055
/* nmacchws  - nmacchws.  */
5056
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5057
/* nmacchwso - nmacchwso. */
5058
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5059
/* nmachhw   - nmachhw.   */
5060
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5061
/* nmachhwo  - nmachhwo.  */
5062
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5063
/* nmachhws  - nmachhws.  */
5064
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5065
/* nmachhwso - nmachhwso. */
5066
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5067
/* nmaclhw   - nmaclhw.   */
5068
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5069
/* nmaclhwo  - nmaclhwo.  */
5070
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5071
/* nmaclhws  - nmaclhws.  */
5072
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5073
/* nmaclhwso - nmaclhwso. */
5074
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5075 5076

/* mulchw  - mulchw.  */
5077
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5078
/* mulchwu - mulchwu. */
5079
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5080
/* mulhhw  - mulhhw.  */
5081
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5082
/* mulhhwu - mulhhwu. */
5083
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5084
/* mullhw  - mullhw.  */
5085
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5086
/* mullhwu - mullhwu. */
5087
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5088 5089 5090 5091 5092

/* mfdcr */
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5093
    GEN_EXCP_PRIVREG(ctx);
5094 5095 5096 5097
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5098
        GEN_EXCP_PRIVREG(ctx);
5099 5100
        return;
    }
5101 5102
    gen_op_set_T0(dcrn);
    gen_op_load_dcr();
5103 5104 5105 5106 5107 5108 5109 5110
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* mtdcr */
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5111
    GEN_EXCP_PRIVREG(ctx);
5112 5113 5114 5115
#else
    uint32_t dcrn = SPR(ctx->opcode);

    if (unlikely(!ctx->supervisor)) {
5116
        GEN_EXCP_PRIVREG(ctx);
5117 5118
        return;
    }
5119 5120 5121 5122 5123 5124 5125
    gen_op_set_T0(dcrn);
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
#endif
}

/* mfdcrx */
5126
/* XXX: not implemented on 440 ? */
5127
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_BOOKE_EXT)
5128 5129
{
#if defined(CONFIG_USER_ONLY)
5130
    GEN_EXCP_PRIVREG(ctx);
5131 5132
#else
    if (unlikely(!ctx->supervisor)) {
5133
        GEN_EXCP_PRIVREG(ctx);
5134 5135 5136 5137 5138
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_dcr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
5139
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5140 5141 5142 5143
#endif
}

/* mtdcrx */
5144
/* XXX: not implemented on 440 ? */
5145
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_BOOKE_EXT)
5146 5147
{
#if defined(CONFIG_USER_ONLY)
5148
    GEN_EXCP_PRIVREG(ctx);
5149 5150
#else
    if (unlikely(!ctx->supervisor)) {
5151
        GEN_EXCP_PRIVREG(ctx);
5152 5153 5154 5155 5156
        return;
    }
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_gpr_T1(rS(ctx->opcode));
    gen_op_store_dcr();
5157
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5158 5159 5160
#endif
}

5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178
/* mfdcrux (PPC 460) : user-mode access to DCR */
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
{
    gen_op_load_gpr_T0(rA(ctx->opcode));
    gen_op_load_dcr();
    gen_op_store_T0_gpr(rD(ctx->opcode));
    /* Note: Rc update flag set leads to undefined state of Rc0 */
}

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

5179 5180 5181 5182
/* dccci */
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5183
    GEN_EXCP_PRIVOPC(ctx);
5184 5185
#else
    if (unlikely(!ctx->supervisor)) {
5186
        GEN_EXCP_PRIVOPC(ctx);
5187 5188 5189 5190 5191 5192 5193 5194 5195 5196
        return;
    }
    /* interpreted as no-op */
#endif
}

/* dcread */
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5197
    GEN_EXCP_PRIVOPC(ctx);
5198 5199
#else
    if (unlikely(!ctx->supervisor)) {
5200
        GEN_EXCP_PRIVOPC(ctx);
5201 5202 5203 5204 5205 5206 5207 5208 5209
        return;
    }
    gen_addr_reg_index(ctx);
    op_ldst(lwz);
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* icbt */
5210
GEN_HANDLER(icbt_40x, 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
}

/* iccci */
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5222
    GEN_EXCP_PRIVOPC(ctx);
5223 5224
#else
    if (unlikely(!ctx->supervisor)) {
5225
        GEN_EXCP_PRIVOPC(ctx);
5226 5227 5228 5229 5230 5231 5232 5233 5234 5235
        return;
    }
    /* interpreted as no-op */
#endif
}

/* icread */
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5236
    GEN_EXCP_PRIVOPC(ctx);
5237 5238
#else
    if (unlikely(!ctx->supervisor)) {
5239
        GEN_EXCP_PRIVOPC(ctx);
5240 5241 5242 5243 5244 5245 5246
        return;
    }
    /* interpreted as no-op */
#endif
}

/* rfci (supervisor only) */
5247 5248 5249
GEN_HANDLER(rfci_40x, 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
{
#if defined(CONFIG_USER_ONLY)
5250
    GEN_EXCP_PRIVOPC(ctx);
5251 5252
#else
    if (unlikely(!ctx->supervisor)) {
5253
        GEN_EXCP_PRIVOPC(ctx);
5254 5255 5256 5257
        return;
    }
    /* Restore CPU state */
    gen_op_40x_rfci();
5258
    GEN_SYNC(ctx);
5259 5260 5261 5262 5263 5264
#endif
}

GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
{
#if defined(CONFIG_USER_ONLY)
5265
    GEN_EXCP_PRIVOPC(ctx);
5266 5267
#else
    if (unlikely(!ctx->supervisor)) {
5268
        GEN_EXCP_PRIVOPC(ctx);
5269 5270 5271 5272
        return;
    }
    /* Restore CPU state */
    gen_op_rfci();
5273
    GEN_SYNC(ctx);
5274 5275 5276 5277
#endif
}

/* BookE specific */
5278
/* XXX: not implemented on 440 ? */
5279
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_BOOKE_EXT)
5280 5281
{
#if defined(CONFIG_USER_ONLY)
5282
    GEN_EXCP_PRIVOPC(ctx);
5283 5284
#else
    if (unlikely(!ctx->supervisor)) {
5285
        GEN_EXCP_PRIVOPC(ctx);
5286 5287 5288
        return;
    }
    /* Restore CPU state */
5289
    gen_op_rfdi();
5290
    GEN_SYNC(ctx);
5291 5292 5293
#endif
}

5294
/* XXX: not implemented on 440 ? */
5295
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5296 5297
{
#if defined(CONFIG_USER_ONLY)
5298
    GEN_EXCP_PRIVOPC(ctx);
5299 5300
#else
    if (unlikely(!ctx->supervisor)) {
5301
        GEN_EXCP_PRIVOPC(ctx);
5302 5303 5304 5305
        return;
    }
    /* Restore CPU state */
    gen_op_rfmci();
5306
    GEN_SYNC(ctx);
5307 5308
#endif
}
5309

5310
/* TLB management - PowerPC 405 implementation */
5311
/* tlbre */
5312
GEN_HANDLER(tlbre_40x, 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5313 5314
{
#if defined(CONFIG_USER_ONLY)
5315
    GEN_EXCP_PRIVOPC(ctx);
5316 5317
#else
    if (unlikely(!ctx->supervisor)) {
5318
        GEN_EXCP_PRIVOPC(ctx);
5319 5320 5321 5322
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5323
        gen_op_load_gpr_T0(rA(ctx->opcode));
5324 5325 5326 5327 5328 5329 5330 5331 5332
        gen_op_4xx_tlbre_hi();
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    case 1:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_4xx_tlbre_lo();
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    default:
5333
        GEN_EXCP_INVAL(ctx);
5334
        break;
5335
    }
5336 5337 5338
#endif
}

5339
/* tlbsx - tlbsx. */
5340
GEN_HANDLER(tlbsx_40x, 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5341 5342
{
#if defined(CONFIG_USER_ONLY)
5343
    GEN_EXCP_PRIVOPC(ctx);
5344 5345
#else
    if (unlikely(!ctx->supervisor)) {
5346
        GEN_EXCP_PRIVOPC(ctx);
5347 5348 5349
        return;
    }
    gen_addr_reg_index(ctx);
5350
    gen_op_4xx_tlbsx();
5351
    if (Rc(ctx->opcode))
5352
        gen_op_4xx_tlbsx_check();
5353
    gen_op_store_T0_gpr(rD(ctx->opcode));
5354
#endif
B
bellard 已提交
5355 5356
}

5357
/* tlbwe */
5358
GEN_HANDLER(tlbwe_40x, 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
B
bellard 已提交
5359
{
5360
#if defined(CONFIG_USER_ONLY)
5361
    GEN_EXCP_PRIVOPC(ctx);
5362 5363
#else
    if (unlikely(!ctx->supervisor)) {
5364
        GEN_EXCP_PRIVOPC(ctx);
5365 5366 5367 5368
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
5369
        gen_op_load_gpr_T0(rA(ctx->opcode));
5370 5371 5372 5373 5374 5375 5376 5377 5378
        gen_op_load_gpr_T1(rS(ctx->opcode));
        gen_op_4xx_tlbwe_hi();
        break;
    case 1:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rS(ctx->opcode));
        gen_op_4xx_tlbwe_lo();
        break;
    default:
5379
        GEN_EXCP_INVAL(ctx);
5380
        break;
5381
    }
5382 5383 5384
#endif
}

5385
/* TLB management - PowerPC 440 implementation */
5386
/* tlbre */
5387
GEN_HANDLER(tlbre_440, 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5388 5389
{
#if defined(CONFIG_USER_ONLY)
5390
    GEN_EXCP_PRIVOPC(ctx);
5391 5392
#else
    if (unlikely(!ctx->supervisor)) {
5393
        GEN_EXCP_PRIVOPC(ctx);
5394 5395 5396 5397 5398 5399 5400
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
        gen_op_load_gpr_T0(rA(ctx->opcode));
5401
        gen_op_440_tlbre(rB(ctx->opcode));
5402 5403 5404
        gen_op_store_T0_gpr(rD(ctx->opcode));
        break;
    default:
5405
        GEN_EXCP_INVAL(ctx);
5406 5407 5408 5409 5410 5411
        break;
    }
#endif
}

/* tlbsx - tlbsx. */
5412
GEN_HANDLER(tlbsx_440, 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5413 5414
{
#if defined(CONFIG_USER_ONLY)
5415
    GEN_EXCP_PRIVOPC(ctx);
5416 5417
#else
    if (unlikely(!ctx->supervisor)) {
5418
        GEN_EXCP_PRIVOPC(ctx);
5419 5420 5421
        return;
    }
    gen_addr_reg_index(ctx);
5422
    gen_op_440_tlbsx();
5423
    if (Rc(ctx->opcode))
5424
        gen_op_4xx_tlbsx_check();
5425 5426 5427 5428 5429
    gen_op_store_T0_gpr(rD(ctx->opcode));
#endif
}

/* tlbwe */
5430
GEN_HANDLER(tlbwe_440, 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5431 5432
{
#if defined(CONFIG_USER_ONLY)
5433
    GEN_EXCP_PRIVOPC(ctx);
5434 5435
#else
    if (unlikely(!ctx->supervisor)) {
5436
        GEN_EXCP_PRIVOPC(ctx);
5437 5438 5439 5440 5441 5442 5443 5444
        return;
    }
    switch (rB(ctx->opcode)) {
    case 0:
    case 1:
    case 2:
        gen_op_load_gpr_T0(rA(ctx->opcode));
        gen_op_load_gpr_T1(rS(ctx->opcode));
5445
        gen_op_440_tlbwe(rB(ctx->opcode));
5446 5447
        break;
    default:
5448
        GEN_EXCP_INVAL(ctx);
5449 5450 5451 5452 5453
        break;
    }
#endif
}

5454 5455 5456 5457
/* wrtee */
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5458
    GEN_EXCP_PRIVOPC(ctx);
5459 5460
#else
    if (unlikely(!ctx->supervisor)) {
5461
        GEN_EXCP_PRIVOPC(ctx);
5462 5463 5464
        return;
    }
    gen_op_load_gpr_T0(rD(ctx->opcode));
5465
    gen_op_wrte();
J
j_mayer 已提交
5466 5467 5468
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5469
    GEN_STOP(ctx);
5470 5471 5472 5473 5474 5475 5476
#endif
}

/* wrteei */
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_EMB_COMMON)
{
#if defined(CONFIG_USER_ONLY)
5477
    GEN_EXCP_PRIVOPC(ctx);
5478 5479
#else
    if (unlikely(!ctx->supervisor)) {
5480
        GEN_EXCP_PRIVOPC(ctx);
5481 5482 5483
        return;
    }
    gen_op_set_T0(ctx->opcode & 0x00010000);
5484
    gen_op_wrte();
J
j_mayer 已提交
5485 5486 5487
    /* Stop translation to have a chance to raise an exception
     * if we just set msr_ee to 1
     */
5488
    GEN_STOP(ctx);
5489 5490 5491
#endif
}

J
j_mayer 已提交
5492
/* PowerPC 440 specific instructions */
5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513
/* dlmzb */
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
{
    gen_op_load_gpr_T0(rS(ctx->opcode));
    gen_op_load_gpr_T1(rB(ctx->opcode));
    gen_op_440_dlmzb();
    gen_op_store_T0_gpr(rA(ctx->opcode));
    gen_op_store_xer_bc();
    if (Rc(ctx->opcode)) {
        gen_op_440_dlmzb_update_Rc();
        gen_op_store_T0_crf(0);
    }
}

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

/* msync replaces sync on 440 */
5514
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525
{
    /* interpreted as no-op */
}

/* icbt */
GEN_HANDLER(icbt_440, 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
{
    /* interpreted as no-op */
    /* XXX: specification say this is treated as a load by the MMU
     *      but does not generate any exception
     */
B
bellard 已提交
5526 5527
}

5528
#if defined(TARGET_PPCEMB)
5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555
/***                           SPE extension                               ***/

/* Register moves */
GEN32(gen_op_load_gpr64_T0, gen_op_load_gpr64_T0_gpr);
GEN32(gen_op_load_gpr64_T1, gen_op_load_gpr64_T1_gpr);
#if 0 // unused
GEN32(gen_op_load_gpr64_T2, gen_op_load_gpr64_T2_gpr);
#endif

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

#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
{                                                                             \
    if (Rc(ctx->opcode))                                                      \
        gen_##name1(ctx);                                                     \
    else                                                                      \
        gen_##name0(ctx);                                                     \
}

/* Handler for undefined SPE opcodes */
static inline void gen_speundef (DisasContext *ctx)
{
5556
    GEN_EXCP_INVAL(ctx);
5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575
}

/* SPE load and stores */
static inline void gen_addr_spe_imm_index (DisasContext *ctx, int sh)
{
    target_long simm = rB(ctx->opcode);

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

#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
#if defined(CONFIG_USER_ONLY)
#if defined(TARGET_PPC64)
5576
/* User-mode only - 64 bits mode */
5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_raw,                                                \
    &gen_op_spe_l##name##_le_raw,                                             \
    &gen_op_spe_l##name##_64_raw,                                             \
    &gen_op_spe_l##name##_le_64_raw,                                          \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_raw,                                               \
    &gen_op_spe_st##name##_le_raw,                                            \
    &gen_op_spe_st##name##_64_raw,                                            \
    &gen_op_spe_st##name##_le_64_raw,                                         \
};
#else /* defined(TARGET_PPC64) */
5592
/* User-mode only - 32 bits mode */
5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_raw,                                                \
    &gen_op_spe_l##name##_le_raw,                                             \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_raw,                                               \
    &gen_op_spe_st##name##_le_raw,                                            \
};
#endif /* defined(TARGET_PPC64) */
#else /* defined(CONFIG_USER_ONLY) */
5605 5606
#if defined(TARGET_PPC64H)
/* Full system with hypervisor mode */
5607 5608 5609 5610 5611 5612
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_user,                                               \
    &gen_op_spe_l##name##_le_user,                                            \
    &gen_op_spe_l##name##_64_user,                                            \
    &gen_op_spe_l##name##_le_64_user,                                         \
5613 5614
    &gen_op_spe_l##name##_kernel,                                             \
    &gen_op_spe_l##name##_le_kernel,                                          \
5615 5616
    &gen_op_spe_l##name##_64_kernel,                                          \
    &gen_op_spe_l##name##_le_64_kernel,                                       \
5617 5618 5619 5620
    &gen_op_spe_l##name##_hypv,                                               \
    &gen_op_spe_l##name##_le_hypv,                                            \
    &gen_op_spe_l##name##_64_hypv,                                            \
    &gen_op_spe_l##name##_le_64_hypv,                                         \
5621 5622 5623 5624 5625
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_user,                                              \
    &gen_op_spe_st##name##_le_user,                                           \
5626 5627
    &gen_op_spe_st##name##_64_user,                                           \
    &gen_op_spe_st##name##_le_64_user,                                        \
5628 5629
    &gen_op_spe_st##name##_kernel,                                            \
    &gen_op_spe_st##name##_le_kernel,                                         \
5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653
    &gen_op_spe_st##name##_64_kernel,                                         \
    &gen_op_spe_st##name##_le_64_kernel,                                      \
    &gen_op_spe_st##name##_hypv,                                              \
    &gen_op_spe_st##name##_le_hypv,                                           \
    &gen_op_spe_st##name##_64_hypv,                                           \
    &gen_op_spe_st##name##_le_64_hypv,                                        \
};
#elif defined(TARGET_PPC64)
/* Full system - 64 bits mode */
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_user,                                               \
    &gen_op_spe_l##name##_le_user,                                            \
    &gen_op_spe_l##name##_64_user,                                            \
    &gen_op_spe_l##name##_le_64_user,                                         \
    &gen_op_spe_l##name##_kernel,                                             \
    &gen_op_spe_l##name##_le_kernel,                                          \
    &gen_op_spe_l##name##_64_kernel,                                          \
    &gen_op_spe_l##name##_le_64_kernel,                                       \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_user,                                              \
    &gen_op_spe_st##name##_le_user,                                           \
5654 5655
    &gen_op_spe_st##name##_64_user,                                           \
    &gen_op_spe_st##name##_le_64_user,                                        \
5656 5657
    &gen_op_spe_st##name##_kernel,                                            \
    &gen_op_spe_st##name##_le_kernel,                                         \
5658 5659 5660 5661
    &gen_op_spe_st##name##_64_kernel,                                         \
    &gen_op_spe_st##name##_le_64_kernel,                                      \
};
#else /* defined(TARGET_PPC64) */
5662
/* Full system - 32 bits mode */
5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683
#define OP_SPE_LD_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
    &gen_op_spe_l##name##_user,                                               \
    &gen_op_spe_l##name##_le_user,                                            \
    &gen_op_spe_l##name##_kernel,                                             \
    &gen_op_spe_l##name##_le_kernel,                                          \
};
#define OP_SPE_ST_TABLE(name)                                                 \
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
    &gen_op_spe_st##name##_user,                                              \
    &gen_op_spe_st##name##_le_user,                                           \
    &gen_op_spe_st##name##_kernel,                                            \
    &gen_op_spe_st##name##_le_kernel,                                         \
};
#endif /* defined(TARGET_PPC64) */
#endif /* defined(CONFIG_USER_ONLY) */

#define GEN_SPE_LD(name, sh)                                                  \
static inline void gen_evl##name (DisasContext *ctx)                          \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5684
        GEN_EXCP_NO_AP(ctx);                                                  \
5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695
        return;                                                               \
    }                                                                         \
    gen_addr_spe_imm_index(ctx, sh);                                          \
    op_spe_ldst(spe_l##name);                                                 \
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPE_LDX(name)                                                     \
static inline void gen_evl##name##x (DisasContext *ctx)                       \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5696
        GEN_EXCP_NO_AP(ctx);                                                  \
5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    op_spe_ldst(spe_l##name);                                                 \
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
}

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

#define GEN_SPE_ST(name, sh)                                                  \
static inline void gen_evst##name (DisasContext *ctx)                         \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5713
        GEN_EXCP_NO_AP(ctx);                                                  \
5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724
        return;                                                               \
    }                                                                         \
    gen_addr_spe_imm_index(ctx, sh);                                          \
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
    op_spe_ldst(spe_st##name);                                                \
}

#define GEN_SPE_STX(name)                                                     \
static inline void gen_evst##name##x (DisasContext *ctx)                      \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5725
        GEN_EXCP_NO_AP(ctx);                                                  \
5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746
        return;                                                               \
    }                                                                         \
    gen_addr_reg_index(ctx);                                                  \
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
    op_spe_ldst(spe_st##name);                                                \
}

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

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

/* SPE arithmetic and logic */
#define GEN_SPEOP_ARITH2(name)                                                \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5747
        GEN_EXCP_NO_AP(ctx);                                                  \
5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_ARITH1(name)                                                \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5760
        GEN_EXCP_NO_AP(ctx);                                                  \
5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_COMP(name)                                                  \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5772
        GEN_EXCP_NO_AP(ctx);                                                  \
5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
}

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

/* Arithmetic */
GEN_SPEOP_ARITH2(evaddw);
GEN_SPEOP_ARITH2(evsubfw);
GEN_SPEOP_ARITH1(evabs);
GEN_SPEOP_ARITH1(evneg);
GEN_SPEOP_ARITH1(evextsb);
GEN_SPEOP_ARITH1(evextsh);
GEN_SPEOP_ARITH1(evrndw);
GEN_SPEOP_ARITH1(evcntlzw);
GEN_SPEOP_ARITH1(evcntlsw);
static inline void gen_brinc (DisasContext *ctx)
{
    /* Note: brinc is usable even if SPE is disabled */
    gen_op_load_gpr64_T0(rA(ctx->opcode));
    gen_op_load_gpr64_T1(rB(ctx->opcode));
    gen_op_brinc();
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

#define GEN_SPEOP_ARITH_IMM2(name)                                            \
static inline void gen_##name##i (DisasContext *ctx)                          \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5822
        GEN_EXCP_NO_AP(ctx);                                                  \
5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

#define GEN_SPEOP_LOGIC_IMM2(name)                                            \
static inline void gen_##name##i (DisasContext *ctx)                          \
{                                                                             \
    if (unlikely(!ctx->spe_enabled)) {                                        \
5835
        GEN_EXCP_NO_AP(ctx);                                                  \
5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906
        return;                                                               \
    }                                                                         \
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
    gen_op_splatwi_T1_64(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

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

static inline void gen_evsplati (DisasContext *ctx)
{
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;

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

static inline void gen_evsplatfi (DisasContext *ctx)
{
    uint32_t imm = rA(ctx->opcode) << 27;

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

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

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

static inline void gen_evsel (DisasContext *ctx)
{
    if (unlikely(!ctx->spe_enabled)) {
5907
        GEN_EXCP_NO_AP(ctx);
5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371
        return;
    }
    gen_op_load_crf_T0(ctx->opcode & 0x7);
    gen_op_load_gpr64_T0(rA(ctx->opcode));
    gen_op_load_gpr64_T1(rB(ctx->opcode));
    gen_op_evsel();
    gen_op_store_T0_gpr64(rD(ctx->opcode));
}

GEN_HANDLER(evsel0, 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER(evsel1, 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER(evsel2, 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}
GEN_HANDLER(evsel3, 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
{
    gen_evsel(ctx);
}

/* Load and stores */
#if defined(TARGET_PPC64)
/* In that case, we already have 64 bits load & stores
 * so, spe_ldd is equivalent to ld and spe_std is equivalent to std
 */
#if defined(CONFIG_USER_ONLY)
#define gen_op_spe_ldd_raw gen_op_ld_raw
#define gen_op_spe_ldd_64_raw gen_op_ld_64_raw
#define gen_op_spe_ldd_le_raw gen_op_ld_le_raw
#define gen_op_spe_ldd_le_64_raw gen_op_ld_le_64_raw
#define gen_op_spe_stdd_raw gen_op_ld_raw
#define gen_op_spe_stdd_64_raw gen_op_std_64_raw
#define gen_op_spe_stdd_le_raw gen_op_std_le_raw
#define gen_op_spe_stdd_le_64_raw gen_op_std_le_64_raw
#else /* defined(CONFIG_USER_ONLY) */
#define gen_op_spe_ldd_kernel gen_op_ld_kernel
#define gen_op_spe_ldd_64_kernel gen_op_ld_64_kernel
#define gen_op_spe_ldd_le_kernel gen_op_ld_kernel
#define gen_op_spe_ldd_le_64_kernel gen_op_ld_64_kernel
#define gen_op_spe_ldd_user gen_op_ld_user
#define gen_op_spe_ldd_64_user gen_op_ld_64_user
#define gen_op_spe_ldd_le_user gen_op_ld_le_user
#define gen_op_spe_ldd_le_64_user gen_op_ld_le_64_user
#define gen_op_spe_stdd_kernel gen_op_std_kernel
#define gen_op_spe_stdd_64_kernel gen_op_std_64_kernel
#define gen_op_spe_stdd_le_kernel gen_op_std_kernel
#define gen_op_spe_stdd_le_64_kernel gen_op_std_64_kernel
#define gen_op_spe_stdd_user gen_op_std_user
#define gen_op_spe_stdd_64_user gen_op_std_64_user
#define gen_op_spe_stdd_le_user gen_op_std_le_user
#define gen_op_spe_stdd_le_64_user gen_op_std_le_64_user
#endif /* defined(CONFIG_USER_ONLY) */
#endif /* defined(TARGET_PPC64) */
GEN_SPEOP_LDST(dd, 3);
GEN_SPEOP_LDST(dw, 3);
GEN_SPEOP_LDST(dh, 3);
GEN_SPEOP_LDST(whe, 2);
GEN_SPEOP_LD(whou, 2);
GEN_SPEOP_LD(whos, 2);
GEN_SPEOP_ST(who, 2);

#if defined(TARGET_PPC64)
/* In that case, spe_stwwo is equivalent to stw */
#if defined(CONFIG_USER_ONLY)
#define gen_op_spe_stwwo_raw gen_op_stw_raw
#define gen_op_spe_stwwo_le_raw gen_op_stw_le_raw
#define gen_op_spe_stwwo_64_raw gen_op_stw_64_raw
#define gen_op_spe_stwwo_le_64_raw gen_op_stw_le_64_raw
#else
#define gen_op_spe_stwwo_user gen_op_stw_user
#define gen_op_spe_stwwo_le_user gen_op_stw_le_user
#define gen_op_spe_stwwo_64_user gen_op_stw_64_user
#define gen_op_spe_stwwo_le_64_user gen_op_stw_le_64_user
#define gen_op_spe_stwwo_kernel gen_op_stw_kernel
#define gen_op_spe_stwwo_le_kernel gen_op_stw_le_kernel
#define gen_op_spe_stwwo_64_kernel gen_op_stw_64_kernel
#define gen_op_spe_stwwo_le_64_kernel gen_op_stw_le_64_kernel
#endif
#endif
#define _GEN_OP_SPE_STWWE(suffix)                                             \
static inline void gen_op_spe_stwwe_##suffix (void)                           \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_##suffix();                                              \
}
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
static inline void gen_op_spe_stwwe_le_##suffix (void)                        \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_##suffix();                                           \
}
#if defined(TARGET_PPC64)
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
static inline void gen_op_spe_stwwe_64_##suffix (void)                        \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_64_##suffix();                                           \
}                                                                             \
static inline void gen_op_spe_stwwe_le_64_##suffix (void)                     \
{                                                                             \
    gen_op_srli32_T1_64();                                                    \
    gen_op_spe_stwwo_le_64_##suffix();                                        \
}
#else
#define GEN_OP_SPE_STWWE(suffix)                                              \
_GEN_OP_SPE_STWWE(suffix);                                                    \
_GEN_OP_SPE_STWWE_LE(suffix)
#endif
#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_STWWE(raw);
#else /* defined(CONFIG_USER_ONLY) */
GEN_OP_SPE_STWWE(kernel);
GEN_OP_SPE_STWWE(user);
#endif /* defined(CONFIG_USER_ONLY) */
GEN_SPEOP_ST(wwe, 2);
GEN_SPEOP_ST(wwo, 2);

#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
static inline void gen_op_spe_l##name##_##suffix (void)                       \
{                                                                             \
    gen_op_##op##_##suffix();                                                 \
    gen_op_splatw_T1_64();                                                    \
}

#define GEN_OP_SPE_LHE(suffix)                                                \
static inline void gen_op_spe_lhe_##suffix (void)                             \
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_sli16_T1_64();                                                     \
}

#define GEN_OP_SPE_LHX(suffix)                                                \
static inline void gen_op_spe_lhx_##suffix (void)                             \
{                                                                             \
    gen_op_spe_lh_##suffix();                                                 \
    gen_op_extsh_T1_64();                                                     \
}

#if defined(CONFIG_USER_ONLY)
GEN_OP_SPE_LHE(raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
GEN_OP_SPE_LHE(le_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
GEN_OP_SPE_LHX(raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
GEN_OP_SPE_LHX(le_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
GEN_OP_SPE_LHE(le_64_raw);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
GEN_OP_SPE_LHX(64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
GEN_OP_SPE_LHX(le_64_raw);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
#endif
#else
GEN_OP_SPE_LHE(kernel);
GEN_OP_SPE_LHE(user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
GEN_OP_SPE_LHE(le_kernel);
GEN_OP_SPE_LHE(le_user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
GEN_OP_SPE_LHX(kernel);
GEN_OP_SPE_LHX(user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
GEN_OP_SPE_LHX(le_kernel);
GEN_OP_SPE_LHX(le_user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
#if defined(TARGET_PPC64)
GEN_OP_SPE_LHE(64_kernel);
GEN_OP_SPE_LHE(64_user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
GEN_OP_SPE_LHE(le_64_kernel);
GEN_OP_SPE_LHE(le_64_user);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
GEN_OP_SPE_LHX(64_kernel);
GEN_OP_SPE_LHX(64_user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
GEN_OP_SPE_LHX(le_64_kernel);
GEN_OP_SPE_LHX(le_64_user);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
#endif
#endif
GEN_SPEOP_LD(hhesplat, 1);
GEN_SPEOP_LD(hhousplat, 1);
GEN_SPEOP_LD(hhossplat, 1);
GEN_SPEOP_LD(wwsplat, 2);
GEN_SPEOP_LD(whsplat, 2);

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

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

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

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

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

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

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

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

/***                      SPE floating-point extension                     ***/
#define GEN_SPEFPUOP_CONV(name)                                               \
static inline void gen_##name (DisasContext *ctx)                             \
{                                                                             \
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
    gen_op_##name();                                                          \
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
}

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

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

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

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

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

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

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

B
bellard 已提交
6372 6373 6374
/* End opcode list */
GEN_OPCODE_MARK(end);

6375
#include "translate_init.c"
B
bellard 已提交
6376

6377
/*****************************************************************************/
6378
/* Misc PowerPC helpers */
6379 6380 6381 6382 6383 6384 6385 6386 6387
static inline uint32_t load_xer (CPUState *env)
{
    return (xer_so << XER_SO) |
        (xer_ov << XER_OV) |
        (xer_ca << XER_CA) |
        (xer_bc << XER_BC) |
        (xer_cmp << XER_CMP);
}

6388 6389 6390
void cpu_dump_state (CPUState *env, FILE *f,
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                     int flags)
B
bellard 已提交
6391
{
6392 6393 6394 6395 6396 6397 6398 6399 6400 6401
#if defined(TARGET_PPC64) || 1
#define FILL ""
#define RGPL  4
#define RFPL  4
#else
#define FILL "        "
#define RGPL  8
#define RFPL  4
#endif

B
bellard 已提交
6402 6403
    int i;

6404
    cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX "\n",
6405
                env->nip, env->lr, env->ctr);
6406 6407 6408
    cpu_fprintf(f, "MSR " REGX FILL " XER %08x      "
#if !defined(NO_TIMER_DUMP)
                "TB %08x %08x "
6409 6410
#if !defined(CONFIG_USER_ONLY)
                "DECR %08x"
6411
#endif
6412 6413
#endif
                "\n",
6414 6415 6416
                do_load_msr(env), load_xer(env)
#if !defined(NO_TIMER_DUMP)
                , cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6417 6418
#if !defined(CONFIG_USER_ONLY)
                , cpu_ppc_load_decr(env)
6419
#endif
6420 6421 6422
#endif
                );
    for (i = 0; i < 32; i++) {
6423 6424
        if ((i & (RGPL - 1)) == 0)
            cpu_fprintf(f, "GPR%02d", i);
6425
        cpu_fprintf(f, " " REGX, (target_ulong)env->gpr[i]);
6426
        if ((i & (RGPL - 1)) == (RGPL - 1))
B
bellard 已提交
6427
            cpu_fprintf(f, "\n");
6428
    }
6429
    cpu_fprintf(f, "CR ");
6430
    for (i = 0; i < 8; i++)
B
bellard 已提交
6431 6432
        cpu_fprintf(f, "%01x", env->crf[i]);
    cpu_fprintf(f, "  [");
6433 6434 6435 6436 6437 6438 6439 6440
    for (i = 0; i < 8; i++) {
        char a = '-';
        if (env->crf[i] & 0x08)
            a = 'L';
        else if (env->crf[i] & 0x04)
            a = 'G';
        else if (env->crf[i] & 0x02)
            a = 'E';
B
bellard 已提交
6441
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6442
    }
6443 6444 6445 6446
    cpu_fprintf(f, " ]             " FILL "RES " REGX "\n", env->reserve);
    for (i = 0; i < 32; i++) {
        if ((i & (RFPL - 1)) == 0)
            cpu_fprintf(f, "FPR%02d", i);
B
bellard 已提交
6447
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6448
        if ((i & (RFPL - 1)) == (RFPL - 1))
B
bellard 已提交
6449
            cpu_fprintf(f, "\n");
B
bellard 已提交
6450
    }
6451 6452 6453
    cpu_fprintf(f, "SRR0 " REGX " SRR1 " REGX "         " FILL FILL FILL
                "SDR1 " REGX "\n",
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
B
bellard 已提交
6454

6455 6456 6457
#undef RGPL
#undef RFPL
#undef FILL
B
bellard 已提交
6458 6459
}

6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506
void cpu_dump_statistics (CPUState *env, FILE*f,
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
                          int flags)
{
#if defined(DO_PPC_STATISTICS)
    opc_handler_t **t1, **t2, **t3, *handler;
    int op1, op2, op3;

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

6507
/*****************************************************************************/
6508 6509 6510
static inline int gen_intermediate_code_internal (CPUState *env,
                                                  TranslationBlock *tb,
                                                  int search_pc)
B
bellard 已提交
6511
{
6512
    DisasContext ctx, *ctxp = &ctx;
B
bellard 已提交
6513
    opc_handler_t **table, *handler;
B
bellard 已提交
6514
    target_ulong pc_start;
B
bellard 已提交
6515
    uint16_t *gen_opc_end;
6516
    int supervisor;
6517
    int single_step, branch_step;
B
bellard 已提交
6518 6519 6520 6521 6522 6523
    int j, lj = -1;

    pc_start = tb->pc;
    gen_opc_ptr = gen_opc_buf;
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
    gen_opparam_ptr = gen_opparam_buf;
B
bellard 已提交
6524
    nb_gen_labels = 0;
B
bellard 已提交
6525
    ctx.nip = pc_start;
B
bellard 已提交
6526
    ctx.tb = tb;
6527
    ctx.exception = POWERPC_EXCP_NONE;
6528
    ctx.spr_cb = env->spr_cb;
6529
#if defined(CONFIG_USER_ONLY)
6530
    supervisor = 0;
6531
#else
6532 6533
#if defined(TARGET_PPC64H)
    if (msr_pr == 0 && msr_hv == 1)
6534
        supervisor = 2;
6535 6536
    else
#endif
6537 6538
        supervisor = 1 - msr_pr;
    ctx.supervisor = supervisor;
6539 6540 6541
#endif
#if defined(TARGET_PPC64)
    ctx.sf_mode = msr_sf;
6542 6543 6544
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | msr_le;
#else
    ctx.mem_idx = (supervisor << 1) | msr_le;
6545
#endif
6546
    ctx.dcache_line_size = env->dcache_line_size;
B
bellard 已提交
6547
    ctx.fpu_enabled = msr_fp;
6548
#if defined(TARGET_PPCEMB)
6549 6550 6551 6552
    if (env->flags & POWERPC_FLAG_SPE)
        ctx.spe_enabled = msr_spe;
    else
        ctx.spe_enabled = 0;
6553
#endif
6554 6555 6556 6557 6558 6559 6560 6561 6562
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
        single_step = 1;
    else
        single_step = 0;
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
        branch_step = 1;
    else
        branch_step = 0;
    ctx.singlestep_enabled = env->singlestep_enabled || single_step == 1;;
6563
#if defined (DO_SINGLE_STEP) && 0
6564 6565 6566 6567
    /* Single step trace mode */
    msr_se = 1;
#endif
    /* Set env in case of segfault during code fetch */
6568
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6569 6570
        if (unlikely(env->nb_breakpoints > 0)) {
            for (j = 0; j < env->nb_breakpoints; j++) {
6571
                if (env->breakpoints[j] == ctx.nip) {
6572
                    gen_update_nip(&ctx, ctx.nip);
6573 6574 6575 6576 6577
                    gen_op_debug();
                    break;
                }
            }
        }
6578
        if (unlikely(search_pc)) {
B
bellard 已提交
6579 6580 6581 6582 6583
            j = gen_opc_ptr - gen_opc_buf;
            if (lj < j) {
                lj++;
                while (lj < j)
                    gen_opc_instr_start[lj++] = 0;
B
bellard 已提交
6584
                gen_opc_pc[lj] = ctx.nip;
B
bellard 已提交
6585 6586 6587
                gen_opc_instr_start[lj] = 1;
            }
        }
6588 6589
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
B
bellard 已提交
6590
            fprintf(logfile, "----------------\n");
6591
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6592 6593 6594
                    ctx.nip, 1 - msr_pr, msr_ir);
        }
#endif
B
bellard 已提交
6595
        ctx.opcode = ldl_code(ctx.nip);
6596 6597 6598 6599 6600 6601
        if (msr_le) {
            ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
                ((ctx.opcode & 0x00FF0000) >> 8) |
                ((ctx.opcode & 0x0000FF00) << 8) |
                ((ctx.opcode & 0x000000FF) << 24);
        }
6602 6603
#if defined PPC_DEBUG_DISAS
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6604
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6605
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6606
                    opc3(ctx.opcode), msr_le ? "little" : "big");
B
bellard 已提交
6607 6608
        }
#endif
B
bellard 已提交
6609
        ctx.nip += 4;
6610
        table = env->opcodes;
B
bellard 已提交
6611 6612 6613 6614 6615 6616 6617 6618 6619 6620
        handler = table[opc1(ctx.opcode)];
        if (is_indirect_opcode(handler)) {
            table = ind_table(handler);
            handler = table[opc2(ctx.opcode)];
            if (is_indirect_opcode(handler)) {
                table = ind_table(handler);
                handler = table[opc3(ctx.opcode)];
            }
        }
        /* Is opcode *REALLY* valid ? */
6621
        if (unlikely(handler->handler == &gen_invalid)) {
J
j_mayer 已提交
6622
            if (loglevel != 0) {
6623
                fprintf(logfile, "invalid/unsupported opcode: "
6624
                        "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
6625
                        opc1(ctx.opcode), opc2(ctx.opcode),
B
bellard 已提交
6626 6627 6628
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
            } else {
                printf("invalid/unsupported opcode: "
6629
                       "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
B
bellard 已提交
6630 6631 6632
                       opc1(ctx.opcode), opc2(ctx.opcode),
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
            }
6633 6634
        } else {
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
J
j_mayer 已提交
6635
                if (loglevel != 0) {
B
bellard 已提交
6636
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6637
                            "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
B
bellard 已提交
6638 6639
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
                            opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6640
                            ctx.opcode, ctx.nip - 4);
6641 6642
                } else {
                    printf("invalid bits: %08x for opcode: "
6643
                           "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
6644 6645
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
                           opc2(ctx.opcode), opc3(ctx.opcode),
B
bellard 已提交
6646
                           ctx.opcode, ctx.nip - 4);
6647
                }
6648
                GEN_EXCP_INVAL(ctxp);
B
bellard 已提交
6649
                break;
B
bellard 已提交
6650 6651
            }
        }
B
bellard 已提交
6652
        (*(handler->handler))(&ctx);
6653 6654 6655
#if defined(DO_PPC_STATISTICS)
        handler->count++;
#endif
6656
        /* Check trace mode exceptions */
6657 6658 6659 6660 6661 6662
        if (unlikely(branch_step != 0 &&
                     ctx.exception == POWERPC_EXCP_BRANCH)) {
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
        } else if (unlikely(single_step != 0 &&
                            (ctx.nip <= 0x100 || ctx.nip > 0xF00 ||
                             (ctx.nip & 0xFC) != 0x04) &&
6663
#if defined(CONFIG_USER_ONLY)
6664
                            ctx.exception != POWERPC_EXCP_SYSCALL_USER &&
6665
#else
6666
                            ctx.exception != POWERPC_EXCP_SYSCALL &&
6667
#endif
6668
                            ctx.exception != POWERPC_EXCP_TRAP)) {
6669
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6670 6671 6672 6673 6674
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
                            (env->singlestep_enabled))) {
            /* if we reach a page boundary or are single stepping, stop
             * generation
             */
6675
            break;
6676
        }
6677 6678 6679 6680
#if defined (DO_SINGLE_STEP)
        break;
#endif
    }
6681
    if (ctx.exception == POWERPC_EXCP_NONE) {
6682
        gen_goto_tb(&ctx, 0, ctx.nip);
6683
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6684 6685 6686
        gen_op_reset_T0();
        /* Generate the return instruction */
        gen_op_exit_tb();
6687
    }
B
bellard 已提交
6688
    *gen_opc_ptr = INDEX_op_end;
6689
    if (unlikely(search_pc)) {
6690 6691 6692 6693 6694
        j = gen_opc_ptr - gen_opc_buf;
        lj++;
        while (lj <= j)
            gen_opc_instr_start[lj++] = 0;
    } else {
B
bellard 已提交
6695
        tb->size = ctx.nip - pc_start;
6696
    }
6697
#if defined(DEBUG_DISAS)
6698
    if (loglevel & CPU_LOG_TB_CPU) {
6699
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
B
bellard 已提交
6700
        cpu_dump_state(env, logfile, fprintf, 0);
6701 6702
    }
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6703
        int flags;
6704 6705
        flags = env->bfd_mach;
        flags |= msr_le << 16;
B
bellard 已提交
6706
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6707
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
B
bellard 已提交
6708
        fprintf(logfile, "\n");
6709 6710
    }
    if (loglevel & CPU_LOG_TB_OP) {
B
bellard 已提交
6711 6712 6713 6714 6715 6716 6717 6718
        fprintf(logfile, "OP:\n");
        dump_ops(gen_opc_buf, gen_opparam_buf);
        fprintf(logfile, "\n");
    }
#endif
    return 0;
}

6719
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6720 6721 6722 6723
{
    return gen_intermediate_code_internal(env, tb, 0);
}

6724
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
B
bellard 已提交
6725 6726 6727
{
    return gen_intermediate_code_internal(env, tb, 1);
}