Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ 47e4661c

History | View | Annotate | Download (228.9 kB)

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

    
26
#include "cpu.h"
27
#include "exec-all.h"
28
#include "disas.h"
29
#include "helper.h"
30
#include "tcg-op.h"
31
#include "qemu-common.h"
32

    
33
#define CPU_SINGLE_STEP 0x1
34
#define CPU_BRANCH_STEP 0x2
35
#define GDBSTUB_SINGLE_STEP 0x4
36

    
37
/* Include definitions for instructions classes and implementations flags */
38
//#define DO_SINGLE_STEP
39
//#define PPC_DEBUG_DISAS
40
//#define DEBUG_MEMORY_ACCESSES
41
//#define DO_PPC_STATISTICS
42
//#define OPTIMIZE_FPRF_UPDATE
43

    
44
/*****************************************************************************/
45
/* Code translation helpers                                                  */
46

    
47
/* global register indexes */
48
static TCGv cpu_env;
49
static char cpu_reg_names[10*3 + 22*4 /* GPR */
50
#if !defined(TARGET_PPC64)
51
    + 10*4 + 22*5 /* SPE GPRh */
52
#endif
53
    + 10*4 + 22*5 /* FPR */
54
    + 2*(10*6 + 22*7) /* AVRh, AVRl */
55
    + 8*5 /* CRF */];
56
static TCGv cpu_gpr[32];
57
#if !defined(TARGET_PPC64)
58
static TCGv cpu_gprh[32];
59
#endif
60
static TCGv cpu_fpr[32];
61
static TCGv cpu_avrh[32], cpu_avrl[32];
62
static TCGv cpu_crf[8];
63

    
64
/* dyngen register indexes */
65
static TCGv cpu_T[3];
66
#if defined(TARGET_PPC64)
67
#define cpu_T64 cpu_T
68
#else
69
static TCGv cpu_T64[3];
70
#endif
71
static TCGv cpu_FT[3];
72
static TCGv cpu_AVRh[3], cpu_AVRl[3];
73

    
74
#include "gen-icount.h"
75

    
76
void ppc_translate_init(void)
77
{
78
    int i;
79
    char* p;
80
    static int done_init = 0;
81

    
82
    if (done_init)
83
        return;
84

    
85
    cpu_env = tcg_global_reg_new(TCG_TYPE_PTR, TCG_AREG0, "env");
86
#if TARGET_LONG_BITS > HOST_LONG_BITS
87
    cpu_T[0] = tcg_global_mem_new(TCG_TYPE_TL,
88
                                  TCG_AREG0, offsetof(CPUState, t0), "T0");
89
    cpu_T[1] = tcg_global_mem_new(TCG_TYPE_TL,
90
                                  TCG_AREG0, offsetof(CPUState, t1), "T1");
91
    cpu_T[2] = tcg_global_mem_new(TCG_TYPE_TL,
92
                                  TCG_AREG0, offsetof(CPUState, t2), "T2");
93
#else
94
    cpu_T[0] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG1, "T0");
95
    cpu_T[1] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG2, "T1");
96
    cpu_T[2] = tcg_global_reg_new(TCG_TYPE_TL, TCG_AREG3, "T2");
97
#endif
98
#if !defined(TARGET_PPC64)
99
    cpu_T64[0] = tcg_global_mem_new(TCG_TYPE_I64,
100
                                    TCG_AREG0, offsetof(CPUState, t0_64),
101
                                    "T0_64");
102
    cpu_T64[1] = tcg_global_mem_new(TCG_TYPE_I64,
103
                                    TCG_AREG0, offsetof(CPUState, t1_64),
104
                                    "T1_64");
105
    cpu_T64[2] = tcg_global_mem_new(TCG_TYPE_I64,
106
                                    TCG_AREG0, offsetof(CPUState, t2_64),
107
                                    "T2_64");
108
#endif
109

    
110
    cpu_FT[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
111
                                   offsetof(CPUState, ft0), "FT0");
112
    cpu_FT[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
113
                                   offsetof(CPUState, ft1), "FT1");
114
    cpu_FT[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
115
                                   offsetof(CPUState, ft2), "FT2");
116

    
117
    cpu_AVRh[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
118
                                     offsetof(CPUState, avr0.u64[0]), "AVR0H");
119
    cpu_AVRl[0] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
120
                                     offsetof(CPUState, avr0.u64[1]), "AVR0L");
121
    cpu_AVRh[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
122
                                     offsetof(CPUState, avr1.u64[0]), "AVR1H");
123
    cpu_AVRl[1] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
124
                                     offsetof(CPUState, avr1.u64[1]), "AVR1L");
125
    cpu_AVRh[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
126
                                     offsetof(CPUState, avr2.u64[0]), "AVR2H");
127
    cpu_AVRl[2] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
128
                                     offsetof(CPUState, avr2.u64[1]), "AVR2L");
129

    
130
    p = cpu_reg_names;
131

    
132
    for (i = 0; i < 8; i++) {
133
        sprintf(p, "crf%d", i);
134
        cpu_crf[i] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
135
                                        offsetof(CPUState, crf[i]), p);
136
        p += 5;
137
    }
138

    
139
    for (i = 0; i < 32; i++) {
140
        sprintf(p, "r%d", i);
141
        cpu_gpr[i] = tcg_global_mem_new(TCG_TYPE_TL, TCG_AREG0,
142
                                        offsetof(CPUState, gpr[i]), p);
143
        p += (i < 10) ? 3 : 4;
144
#if !defined(TARGET_PPC64)
145
        sprintf(p, "r%dH", i);
146
        cpu_gprh[i] = tcg_global_mem_new(TCG_TYPE_I32, TCG_AREG0,
147
                                         offsetof(CPUState, gprh[i]), p);
148
        p += (i < 10) ? 4 : 5;
149
#endif
150

    
151
        sprintf(p, "fp%d", i);
152
        cpu_fpr[i] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
153
                                        offsetof(CPUState, fpr[i]), p);
154
        p += (i < 10) ? 4 : 5;
155

    
156
        sprintf(p, "avr%dH", i);
157
        cpu_avrh[i] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
158
                                         offsetof(CPUState, avr[i].u64[0]), p);
159
        p += (i < 10) ? 6 : 7;
160

    
161
        sprintf(p, "avr%dL", i);
162
        cpu_avrl[i] = tcg_global_mem_new(TCG_TYPE_I64, TCG_AREG0,
163
                                         offsetof(CPUState, avr[i].u64[1]), p);
164
        p += (i < 10) ? 6 : 7;
165
    }
166

    
167
    /* register helpers */
168
#undef DEF_HELPER
169
#define DEF_HELPER(ret, name, params) tcg_register_helper(name, #name);
170
#include "helper.h"
171

    
172
    done_init = 1;
173
}
174

    
175
#if defined(OPTIMIZE_FPRF_UPDATE)
176
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
177
static uint16_t **gen_fprf_ptr;
178
#endif
179

    
180
#define GEN16(func, NAME)                                                     \
181
static GenOpFunc *NAME ## _table [16] = {                                     \
182
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
183
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
184
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
185
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
186
};                                                                            \
187
static always_inline void func (int n)                                        \
188
{                                                                             \
189
    NAME ## _table[n]();                                                      \
190
}
191

    
192
#define GEN32(func, NAME)                                                     \
193
static GenOpFunc *NAME ## _table [32] = {                                     \
194
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
195
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
196
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
197
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
198
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,                               \
199
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,                               \
200
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,                               \
201
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,                               \
202
};                                                                            \
203
static always_inline void func (int n)                                        \
204
{                                                                             \
205
    NAME ## _table[n]();                                                      \
206
}
207

    
208
/* internal defines */
209
typedef struct DisasContext {
210
    struct TranslationBlock *tb;
211
    target_ulong nip;
212
    uint32_t opcode;
213
    uint32_t exception;
214
    /* Routine used to access memory */
215
    int mem_idx;
216
    /* Translation flags */
217
#if !defined(CONFIG_USER_ONLY)
218
    int supervisor;
219
#endif
220
#if defined(TARGET_PPC64)
221
    int sf_mode;
222
#endif
223
    int fpu_enabled;
224
    int altivec_enabled;
225
    int spe_enabled;
226
    ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
227
    int singlestep_enabled;
228
    int dcache_line_size;
229
} DisasContext;
230

    
231
struct opc_handler_t {
232
    /* invalid bits */
233
    uint32_t inval;
234
    /* instruction type */
235
    uint64_t type;
236
    /* handler */
237
    void (*handler)(DisasContext *ctx);
238
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
239
    const unsigned char *oname;
240
#endif
241
#if defined(DO_PPC_STATISTICS)
242
    uint64_t count;
243
#endif
244
};
245

    
246
static always_inline void gen_set_Rc0 (DisasContext *ctx)
247
{
248
#if defined(TARGET_PPC64)
249
    if (ctx->sf_mode)
250
        gen_op_cmpi_64(0);
251
    else
252
#endif
253
        gen_op_cmpi(0);
254
    gen_op_set_Rc0();
255
}
256

    
257
static always_inline void gen_reset_fpstatus (void)
258
{
259
#ifdef CONFIG_SOFTFLOAT
260
    gen_op_reset_fpstatus();
261
#endif
262
}
263

    
264
static always_inline void gen_compute_fprf (int set_fprf, int set_rc)
265
{
266
    if (set_fprf != 0) {
267
        /* This case might be optimized later */
268
#if defined(OPTIMIZE_FPRF_UPDATE)
269
        *gen_fprf_ptr++ = gen_opc_ptr;
270
#endif
271
        gen_op_compute_fprf(1);
272
        if (unlikely(set_rc))
273
            tcg_gen_andi_i32(cpu_crf[1], cpu_T[0], 0xf);
274
        gen_op_float_check_status();
275
    } else if (unlikely(set_rc)) {
276
        /* We always need to compute fpcc */
277
        gen_op_compute_fprf(0);
278
        tcg_gen_andi_i32(cpu_crf[1], cpu_T[0], 0xf);
279
        if (set_fprf)
280
            gen_op_float_check_status();
281
    }
282
}
283

    
284
static always_inline void gen_optimize_fprf (void)
285
{
286
#if defined(OPTIMIZE_FPRF_UPDATE)
287
    uint16_t **ptr;
288

    
289
    for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
290
        *ptr = INDEX_op_nop1;
291
    gen_fprf_ptr = gen_fprf_buf;
292
#endif
293
}
294

    
295
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
296
{
297
#if defined(TARGET_PPC64)
298
    if (ctx->sf_mode)
299
        gen_op_update_nip_64(nip >> 32, nip);
300
    else
301
#endif
302
        gen_op_update_nip(nip);
303
}
304

    
305
#define GEN_EXCP(ctx, excp, error)                                            \
306
do {                                                                          \
307
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
308
        gen_update_nip(ctx, (ctx)->nip);                                      \
309
    }                                                                         \
310
    gen_op_raise_exception_err((excp), (error));                              \
311
    ctx->exception = (excp);                                                  \
312
} while (0)
313

    
314
#define GEN_EXCP_INVAL(ctx)                                                   \
315
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
316
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
317

    
318
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
319
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
320
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
321

    
322
#define GEN_EXCP_PRIVREG(ctx)                                                 \
323
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
324
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
325

    
326
#define GEN_EXCP_NO_FP(ctx)                                                   \
327
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
328

    
329
#define GEN_EXCP_NO_AP(ctx)                                                   \
330
GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
331

    
332
#define GEN_EXCP_NO_VR(ctx)                                                   \
333
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)
334

    
335
/* Stop translation */
336
static always_inline void GEN_STOP (DisasContext *ctx)
337
{
338
    gen_update_nip(ctx, ctx->nip);
339
    ctx->exception = POWERPC_EXCP_STOP;
340
}
341

    
342
/* No need to update nip here, as execution flow will change */
343
static always_inline void GEN_SYNC (DisasContext *ctx)
344
{
345
    ctx->exception = POWERPC_EXCP_SYNC;
346
}
347

    
348
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
349
static void gen_##name (DisasContext *ctx);                                   \
350
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
351
static void gen_##name (DisasContext *ctx)
352

    
353
#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
354
static void gen_##name (DisasContext *ctx);                                   \
355
GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
356
static void gen_##name (DisasContext *ctx)
357

    
358
typedef struct opcode_t {
359
    unsigned char opc1, opc2, opc3;
360
#if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
361
    unsigned char pad[5];
362
#else
363
    unsigned char pad[1];
364
#endif
365
    opc_handler_t handler;
366
    const unsigned char *oname;
367
} opcode_t;
368

    
369
/*****************************************************************************/
370
/***                           Instruction decoding                        ***/
371
#define EXTRACT_HELPER(name, shift, nb)                                       \
372
static always_inline uint32_t name (uint32_t opcode)                          \
373
{                                                                             \
374
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
375
}
376

    
377
#define EXTRACT_SHELPER(name, shift, nb)                                      \
378
static always_inline int32_t name (uint32_t opcode)                           \
379
{                                                                             \
380
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
381
}
382

    
383
/* Opcode part 1 */
384
EXTRACT_HELPER(opc1, 26, 6);
385
/* Opcode part 2 */
386
EXTRACT_HELPER(opc2, 1, 5);
387
/* Opcode part 3 */
388
EXTRACT_HELPER(opc3, 6, 5);
389
/* Update Cr0 flags */
390
EXTRACT_HELPER(Rc, 0, 1);
391
/* Destination */
392
EXTRACT_HELPER(rD, 21, 5);
393
/* Source */
394
EXTRACT_HELPER(rS, 21, 5);
395
/* First operand */
396
EXTRACT_HELPER(rA, 16, 5);
397
/* Second operand */
398
EXTRACT_HELPER(rB, 11, 5);
399
/* Third operand */
400
EXTRACT_HELPER(rC, 6, 5);
401
/***                               Get CRn                                 ***/
402
EXTRACT_HELPER(crfD, 23, 3);
403
EXTRACT_HELPER(crfS, 18, 3);
404
EXTRACT_HELPER(crbD, 21, 5);
405
EXTRACT_HELPER(crbA, 16, 5);
406
EXTRACT_HELPER(crbB, 11, 5);
407
/* SPR / TBL */
408
EXTRACT_HELPER(_SPR, 11, 10);
409
static always_inline uint32_t SPR (uint32_t opcode)
410
{
411
    uint32_t sprn = _SPR(opcode);
412

    
413
    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
414
}
415
/***                              Get constants                            ***/
416
EXTRACT_HELPER(IMM, 12, 8);
417
/* 16 bits signed immediate value */
418
EXTRACT_SHELPER(SIMM, 0, 16);
419
/* 16 bits unsigned immediate value */
420
EXTRACT_HELPER(UIMM, 0, 16);
421
/* Bit count */
422
EXTRACT_HELPER(NB, 11, 5);
423
/* Shift count */
424
EXTRACT_HELPER(SH, 11, 5);
425
/* Mask start */
426
EXTRACT_HELPER(MB, 6, 5);
427
/* Mask end */
428
EXTRACT_HELPER(ME, 1, 5);
429
/* Trap operand */
430
EXTRACT_HELPER(TO, 21, 5);
431

    
432
EXTRACT_HELPER(CRM, 12, 8);
433
EXTRACT_HELPER(FM, 17, 8);
434
EXTRACT_HELPER(SR, 16, 4);
435
EXTRACT_HELPER(FPIMM, 12, 4);
436

    
437
/***                            Jump target decoding                       ***/
438
/* Displacement */
439
EXTRACT_SHELPER(d, 0, 16);
440
/* Immediate address */
441
static always_inline target_ulong LI (uint32_t opcode)
442
{
443
    return (opcode >> 0) & 0x03FFFFFC;
444
}
445

    
446
static always_inline uint32_t BD (uint32_t opcode)
447
{
448
    return (opcode >> 0) & 0xFFFC;
449
}
450

    
451
EXTRACT_HELPER(BO, 21, 5);
452
EXTRACT_HELPER(BI, 16, 5);
453
/* Absolute/relative address */
454
EXTRACT_HELPER(AA, 1, 1);
455
/* Link */
456
EXTRACT_HELPER(LK, 0, 1);
457

    
458
/* Create a mask between <start> and <end> bits */
459
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
460
{
461
    target_ulong ret;
462

    
463
#if defined(TARGET_PPC64)
464
    if (likely(start == 0)) {
465
        ret = UINT64_MAX << (63 - end);
466
    } else if (likely(end == 63)) {
467
        ret = UINT64_MAX >> start;
468
    }
469
#else
470
    if (likely(start == 0)) {
471
        ret = UINT32_MAX << (31  - end);
472
    } else if (likely(end == 31)) {
473
        ret = UINT32_MAX >> start;
474
    }
475
#endif
476
    else {
477
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
478
            (((target_ulong)(-1ULL) >> (end)) >> 1);
479
        if (unlikely(start > end))
480
            return ~ret;
481
    }
482

    
483
    return ret;
484
}
485

    
486
/*****************************************************************************/
487
/* PowerPC Instructions types definitions                                    */
488
enum {
489
    PPC_NONE           = 0x0000000000000000ULL,
490
    /* PowerPC base instructions set                                         */
491
    PPC_INSNS_BASE     = 0x0000000000000001ULL,
492
    /*   integer operations instructions                                     */
493
#define PPC_INTEGER PPC_INSNS_BASE
494
    /*   flow control instructions                                           */
495
#define PPC_FLOW    PPC_INSNS_BASE
496
    /*   virtual memory instructions                                         */
497
#define PPC_MEM     PPC_INSNS_BASE
498
    /*   ld/st with reservation instructions                                 */
499
#define PPC_RES     PPC_INSNS_BASE
500
    /*   spr/msr access instructions                                         */
501
#define PPC_MISC    PPC_INSNS_BASE
502
    /* Deprecated instruction sets                                           */
503
    /*   Original POWER instruction set                                      */
504
    PPC_POWER          = 0x0000000000000002ULL,
505
    /*   POWER2 instruction set extension                                    */
506
    PPC_POWER2         = 0x0000000000000004ULL,
507
    /*   Power RTC support                                                   */
508
    PPC_POWER_RTC      = 0x0000000000000008ULL,
509
    /*   Power-to-PowerPC bridge (601)                                       */
510
    PPC_POWER_BR       = 0x0000000000000010ULL,
511
    /* 64 bits PowerPC instruction set                                       */
512
    PPC_64B            = 0x0000000000000020ULL,
513
    /*   New 64 bits extensions (PowerPC 2.0x)                               */
514
    PPC_64BX           = 0x0000000000000040ULL,
515
    /*   64 bits hypervisor extensions                                       */
516
    PPC_64H            = 0x0000000000000080ULL,
517
    /*   New wait instruction (PowerPC 2.0x)                                 */
518
    PPC_WAIT           = 0x0000000000000100ULL,
519
    /*   Time base mftb instruction                                          */
520
    PPC_MFTB           = 0x0000000000000200ULL,
521

    
522
    /* Fixed-point unit extensions                                           */
523
    /*   PowerPC 602 specific                                                */
524
    PPC_602_SPEC       = 0x0000000000000400ULL,
525
    /*   isel instruction                                                    */
526
    PPC_ISEL           = 0x0000000000000800ULL,
527
    /*   popcntb instruction                                                 */
528
    PPC_POPCNTB        = 0x0000000000001000ULL,
529
    /*   string load / store                                                 */
530
    PPC_STRING         = 0x0000000000002000ULL,
531

    
532
    /* Floating-point unit extensions                                        */
533
    /*   Optional floating point instructions                                */
534
    PPC_FLOAT          = 0x0000000000010000ULL,
535
    /* New floating-point extensions (PowerPC 2.0x)                          */
536
    PPC_FLOAT_EXT      = 0x0000000000020000ULL,
537
    PPC_FLOAT_FSQRT    = 0x0000000000040000ULL,
538
    PPC_FLOAT_FRES     = 0x0000000000080000ULL,
539
    PPC_FLOAT_FRSQRTE  = 0x0000000000100000ULL,
540
    PPC_FLOAT_FRSQRTES = 0x0000000000200000ULL,
541
    PPC_FLOAT_FSEL     = 0x0000000000400000ULL,
542
    PPC_FLOAT_STFIWX   = 0x0000000000800000ULL,
543

    
544
    /* Vector/SIMD extensions                                                */
545
    /*   Altivec support                                                     */
546
    PPC_ALTIVEC        = 0x0000000001000000ULL,
547
    /*   PowerPC 2.03 SPE extension                                          */
548
    PPC_SPE            = 0x0000000002000000ULL,
549
    /*   PowerPC 2.03 SPE floating-point extension                           */
550
    PPC_SPEFPU         = 0x0000000004000000ULL,
551

    
552
    /* Optional memory control instructions                                  */
553
    PPC_MEM_TLBIA      = 0x0000000010000000ULL,
554
    PPC_MEM_TLBIE      = 0x0000000020000000ULL,
555
    PPC_MEM_TLBSYNC    = 0x0000000040000000ULL,
556
    /*   sync instruction                                                    */
557
    PPC_MEM_SYNC       = 0x0000000080000000ULL,
558
    /*   eieio instruction                                                   */
559
    PPC_MEM_EIEIO      = 0x0000000100000000ULL,
560

    
561
    /* Cache control instructions                                            */
562
    PPC_CACHE          = 0x0000000200000000ULL,
563
    /*   icbi instruction                                                    */
564
    PPC_CACHE_ICBI     = 0x0000000400000000ULL,
565
    /*   dcbz instruction with fixed cache line size                         */
566
    PPC_CACHE_DCBZ     = 0x0000000800000000ULL,
567
    /*   dcbz instruction with tunable cache line size                       */
568
    PPC_CACHE_DCBZT    = 0x0000001000000000ULL,
569
    /*   dcba instruction                                                    */
570
    PPC_CACHE_DCBA     = 0x0000002000000000ULL,
571
    /*   Freescale cache locking instructions                                */
572
    PPC_CACHE_LOCK     = 0x0000004000000000ULL,
573

    
574
    /* MMU related extensions                                                */
575
    /*   external control instructions                                       */
576
    PPC_EXTERN         = 0x0000010000000000ULL,
577
    /*   segment register access instructions                                */
578
    PPC_SEGMENT        = 0x0000020000000000ULL,
579
    /*   PowerPC 6xx TLB management instructions                             */
580
    PPC_6xx_TLB        = 0x0000040000000000ULL,
581
    /* PowerPC 74xx TLB management instructions                              */
582
    PPC_74xx_TLB       = 0x0000080000000000ULL,
583
    /*   PowerPC 40x TLB management instructions                             */
584
    PPC_40x_TLB        = 0x0000100000000000ULL,
585
    /*   segment register access instructions for PowerPC 64 "bridge"        */
586
    PPC_SEGMENT_64B    = 0x0000200000000000ULL,
587
    /*   SLB management                                                      */
588
    PPC_SLBI           = 0x0000400000000000ULL,
589

    
590
    /* Embedded PowerPC dedicated instructions                               */
591
    PPC_WRTEE          = 0x0001000000000000ULL,
592
    /* PowerPC 40x exception model                                           */
593
    PPC_40x_EXCP       = 0x0002000000000000ULL,
594
    /* PowerPC 405 Mac instructions                                          */
595
    PPC_405_MAC        = 0x0004000000000000ULL,
596
    /* PowerPC 440 specific instructions                                     */
597
    PPC_440_SPEC       = 0x0008000000000000ULL,
598
    /* BookE (embedded) PowerPC specification                                */
599
    PPC_BOOKE          = 0x0010000000000000ULL,
600
    /* mfapidi instruction                                                   */
601
    PPC_MFAPIDI        = 0x0020000000000000ULL,
602
    /* tlbiva instruction                                                    */
603
    PPC_TLBIVA         = 0x0040000000000000ULL,
604
    /* tlbivax instruction                                                   */
605
    PPC_TLBIVAX        = 0x0080000000000000ULL,
606
    /* PowerPC 4xx dedicated instructions                                    */
607
    PPC_4xx_COMMON     = 0x0100000000000000ULL,
608
    /* PowerPC 40x ibct instructions                                         */
609
    PPC_40x_ICBT       = 0x0200000000000000ULL,
610
    /* rfmci is not implemented in all BookE PowerPC                         */
611
    PPC_RFMCI          = 0x0400000000000000ULL,
612
    /* rfdi instruction                                                      */
613
    PPC_RFDI           = 0x0800000000000000ULL,
614
    /* DCR accesses                                                          */
615
    PPC_DCR            = 0x1000000000000000ULL,
616
    /* DCR extended accesse                                                  */
617
    PPC_DCRX           = 0x2000000000000000ULL,
618
    /* user-mode DCR access, implemented in PowerPC 460                      */
619
    PPC_DCRUX          = 0x4000000000000000ULL,
620
};
621

    
622
/*****************************************************************************/
623
/* PowerPC instructions table                                                */
624
#if HOST_LONG_BITS == 64
625
#define OPC_ALIGN 8
626
#else
627
#define OPC_ALIGN 4
628
#endif
629
#if defined(__APPLE__)
630
#define OPCODES_SECTION                                                       \
631
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
632
#else
633
#define OPCODES_SECTION                                                       \
634
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
635
#endif
636

    
637
#if defined(DO_PPC_STATISTICS)
638
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
639
OPCODES_SECTION opcode_t opc_##name = {                                       \
640
    .opc1 = op1,                                                              \
641
    .opc2 = op2,                                                              \
642
    .opc3 = op3,                                                              \
643
    .pad  = { 0, },                                                           \
644
    .handler = {                                                              \
645
        .inval   = invl,                                                      \
646
        .type = _typ,                                                         \
647
        .handler = &gen_##name,                                               \
648
        .oname = stringify(name),                                             \
649
    },                                                                        \
650
    .oname = stringify(name),                                                 \
651
}
652
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
653
OPCODES_SECTION opcode_t opc_##name = {                                       \
654
    .opc1 = op1,                                                              \
655
    .opc2 = op2,                                                              \
656
    .opc3 = op3,                                                              \
657
    .pad  = { 0, },                                                           \
658
    .handler = {                                                              \
659
        .inval   = invl,                                                      \
660
        .type = _typ,                                                         \
661
        .handler = &gen_##name,                                               \
662
        .oname = onam,                                                        \
663
    },                                                                        \
664
    .oname = onam,                                                            \
665
}
666
#else
667
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
668
OPCODES_SECTION opcode_t opc_##name = {                                       \
669
    .opc1 = op1,                                                              \
670
    .opc2 = op2,                                                              \
671
    .opc3 = op3,                                                              \
672
    .pad  = { 0, },                                                           \
673
    .handler = {                                                              \
674
        .inval   = invl,                                                      \
675
        .type = _typ,                                                         \
676
        .handler = &gen_##name,                                               \
677
    },                                                                        \
678
    .oname = stringify(name),                                                 \
679
}
680
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
681
OPCODES_SECTION opcode_t opc_##name = {                                       \
682
    .opc1 = op1,                                                              \
683
    .opc2 = op2,                                                              \
684
    .opc3 = op3,                                                              \
685
    .pad  = { 0, },                                                           \
686
    .handler = {                                                              \
687
        .inval   = invl,                                                      \
688
        .type = _typ,                                                         \
689
        .handler = &gen_##name,                                               \
690
    },                                                                        \
691
    .oname = onam,                                                            \
692
}
693
#endif
694

    
695
#define GEN_OPCODE_MARK(name)                                                 \
696
OPCODES_SECTION opcode_t opc_##name = {                                       \
697
    .opc1 = 0xFF,                                                             \
698
    .opc2 = 0xFF,                                                             \
699
    .opc3 = 0xFF,                                                             \
700
    .pad  = { 0, },                                                           \
701
    .handler = {                                                              \
702
        .inval   = 0x00000000,                                                \
703
        .type = 0x00,                                                         \
704
        .handler = NULL,                                                      \
705
    },                                                                        \
706
    .oname = stringify(name),                                                 \
707
}
708

    
709
/* Start opcode list */
710
GEN_OPCODE_MARK(start);
711

    
712
/* Invalid instruction */
713
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
714
{
715
    GEN_EXCP_INVAL(ctx);
716
}
717

    
718
static opc_handler_t invalid_handler = {
719
    .inval   = 0xFFFFFFFF,
720
    .type    = PPC_NONE,
721
    .handler = gen_invalid,
722
};
723

    
724
/***                           Integer arithmetic                          ***/
725
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval, type)                 \
726
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
727
{                                                                             \
728
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
729
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
730
    gen_op_##name();                                                          \
731
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
732
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
733
        gen_set_Rc0(ctx);                                                     \
734
}
735

    
736
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
737
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
738
{                                                                             \
739
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
740
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
741
    gen_op_##name();                                                          \
742
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
743
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
744
        gen_set_Rc0(ctx);                                                     \
745
}
746

    
747
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
748
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
749
{                                                                             \
750
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
751
    gen_op_##name();                                                          \
752
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
753
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
754
        gen_set_Rc0(ctx);                                                     \
755
}
756
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
757
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
758
{                                                                             \
759
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
760
    gen_op_##name();                                                          \
761
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
762
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
763
        gen_set_Rc0(ctx);                                                     \
764
}
765

    
766
/* Two operands arithmetic functions */
767
#define GEN_INT_ARITH2(name, opc1, opc2, opc3, type)                          \
768
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000, type)                    \
769
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
770

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

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

    
780
#if defined(TARGET_PPC64)
781
#define __GEN_INT_ARITH2_64(name, opc1, opc2, opc3, inval, type)              \
782
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
783
{                                                                             \
784
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
785
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
786
    if (ctx->sf_mode)                                                         \
787
        gen_op_##name##_64();                                                 \
788
    else                                                                      \
789
        gen_op_##name();                                                      \
790
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
791
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
792
        gen_set_Rc0(ctx);                                                     \
793
}
794

    
795
#define __GEN_INT_ARITH2_O_64(name, opc1, opc2, opc3, inval, type)            \
796
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
797
{                                                                             \
798
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
799
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
800
    if (ctx->sf_mode)                                                         \
801
        gen_op_##name##_64();                                                 \
802
    else                                                                      \
803
        gen_op_##name();                                                      \
804
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
805
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
806
        gen_set_Rc0(ctx);                                                     \
807
}
808

    
809
#define __GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                     \
810
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
811
{                                                                             \
812
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
813
    if (ctx->sf_mode)                                                         \
814
        gen_op_##name##_64();                                                 \
815
    else                                                                      \
816
        gen_op_##name();                                                      \
817
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
818
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
819
        gen_set_Rc0(ctx);                                                     \
820
}
821
#define __GEN_INT_ARITH1_O_64(name, opc1, opc2, opc3, type)                   \
822
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
823
{                                                                             \
824
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
825
    if (ctx->sf_mode)                                                         \
826
        gen_op_##name##_64();                                                 \
827
    else                                                                      \
828
        gen_op_##name();                                                      \
829
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);                       \
830
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
831
        gen_set_Rc0(ctx);                                                     \
832
}
833

    
834
/* Two operands arithmetic functions */
835
#define GEN_INT_ARITH2_64(name, opc1, opc2, opc3, type)                       \
836
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000000, type)                 \
837
__GEN_INT_ARITH2_O_64(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
838

    
839
/* Two operands arithmetic functions with no overflow allowed */
840
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
841
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
842

    
843
/* One operand arithmetic functions */
844
#define GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                       \
845
__GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                             \
846
__GEN_INT_ARITH1_O_64(name##o, opc1, opc2, opc3 | 0x10, type)
847
#else
848
#define GEN_INT_ARITH2_64 GEN_INT_ARITH2
849
#define GEN_INT_ARITHN_64 GEN_INT_ARITHN
850
#define GEN_INT_ARITH1_64 GEN_INT_ARITH1
851
#endif
852

    
853
/* add    add.    addo    addo.    */
854
static always_inline void gen_op_addo (void)
855
{
856
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
857
    gen_op_add();
858
    gen_op_check_addo();
859
}
860
#if defined(TARGET_PPC64)
861
#define gen_op_add_64 gen_op_add
862
static always_inline void gen_op_addo_64 (void)
863
{
864
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
865
    gen_op_add();
866
    gen_op_check_addo_64();
867
}
868
#endif
869
GEN_INT_ARITH2_64 (add,    0x1F, 0x0A, 0x08, PPC_INTEGER);
870
/* addc   addc.   addco   addco.   */
871
static always_inline void gen_op_addc (void)
872
{
873
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
874
    gen_op_add();
875
    gen_op_check_addc();
876
}
877
static always_inline void gen_op_addco (void)
878
{
879
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
880
    gen_op_add();
881
    gen_op_check_addc();
882
    gen_op_check_addo();
883
}
884
#if defined(TARGET_PPC64)
885
static always_inline void gen_op_addc_64 (void)
886
{
887
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
888
    gen_op_add();
889
    gen_op_check_addc_64();
890
}
891
static always_inline void gen_op_addco_64 (void)
892
{
893
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
894
    gen_op_add();
895
    gen_op_check_addc_64();
896
    gen_op_check_addo_64();
897
}
898
#endif
899
GEN_INT_ARITH2_64 (addc,   0x1F, 0x0A, 0x00, PPC_INTEGER);
900
/* adde   adde.   addeo   addeo.   */
901
static always_inline void gen_op_addeo (void)
902
{
903
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
904
    gen_op_adde();
905
    gen_op_check_addo();
906
}
907
#if defined(TARGET_PPC64)
908
static always_inline void gen_op_addeo_64 (void)
909
{
910
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
911
    gen_op_adde_64();
912
    gen_op_check_addo_64();
913
}
914
#endif
915
GEN_INT_ARITH2_64 (adde,   0x1F, 0x0A, 0x04, PPC_INTEGER);
916
/* addme  addme.  addmeo  addmeo.  */
917
static always_inline void gen_op_addme (void)
918
{
919
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
920
    gen_op_add_me();
921
}
922
#if defined(TARGET_PPC64)
923
static always_inline void gen_op_addme_64 (void)
924
{
925
    tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
926
    gen_op_add_me_64();
927
}
928
#endif
929
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
930
/* addze  addze.  addzeo  addzeo.  */
931
static always_inline void gen_op_addze (void)
932
{
933
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
934
    gen_op_add_ze();
935
    gen_op_check_addc();
936
}
937
static always_inline void gen_op_addzeo (void)
938
{
939
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
940
    gen_op_add_ze();
941
    gen_op_check_addc();
942
    gen_op_check_addo();
943
}
944
#if defined(TARGET_PPC64)
945
static always_inline void gen_op_addze_64 (void)
946
{
947
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
948
    gen_op_add_ze();
949
    gen_op_check_addc_64();
950
}
951
static always_inline void gen_op_addzeo_64 (void)
952
{
953
    tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
954
    gen_op_add_ze();
955
    gen_op_check_addc_64();
956
    gen_op_check_addo_64();
957
}
958
#endif
959
GEN_INT_ARITH1_64 (addze,  0x1F, 0x0A, 0x06, PPC_INTEGER);
960
/* divw   divw.   divwo   divwo.   */
961
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
962
/* divwu  divwu.  divwuo  divwuo.  */
963
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
964
/* mulhw  mulhw.                   */
965
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
966
/* mulhwu mulhwu.                  */
967
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
968
/* mullw  mullw.  mullwo  mullwo.  */
969
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
970
/* neg    neg.    nego    nego.    */
971
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
972
/* subf   subf.   subfo   subfo.   */
973
static always_inline void gen_op_subfo (void)
974
{
975
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
976
    gen_op_subf();
977
    gen_op_check_addo();
978
}
979
#if defined(TARGET_PPC64)
980
#define gen_op_subf_64 gen_op_subf
981
static always_inline void gen_op_subfo_64 (void)
982
{
983
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
984
    gen_op_subf();
985
    gen_op_check_addo_64();
986
}
987
#endif
988
GEN_INT_ARITH2_64 (subf,   0x1F, 0x08, 0x01, PPC_INTEGER);
989
/* subfc  subfc.  subfco  subfco.  */
990
static always_inline void gen_op_subfc (void)
991
{
992
    gen_op_subf();
993
    gen_op_check_subfc();
994
}
995
static always_inline void gen_op_subfco (void)
996
{
997
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
998
    gen_op_subf();
999
    gen_op_check_subfc();
1000
    gen_op_check_addo();
1001
}
1002
#if defined(TARGET_PPC64)
1003
static always_inline void gen_op_subfc_64 (void)
1004
{
1005
    gen_op_subf();
1006
    gen_op_check_subfc_64();
1007
}
1008
static always_inline void gen_op_subfco_64 (void)
1009
{
1010
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
1011
    gen_op_subf();
1012
    gen_op_check_subfc_64();
1013
    gen_op_check_addo_64();
1014
}
1015
#endif
1016
GEN_INT_ARITH2_64 (subfc,  0x1F, 0x08, 0x00, PPC_INTEGER);
1017
/* subfe  subfe.  subfeo  subfeo.  */
1018
static always_inline void gen_op_subfeo (void)
1019
{
1020
    tcg_gen_not_tl(cpu_T[2], cpu_T[0]);
1021
    gen_op_subfe();
1022
    gen_op_check_addo();
1023
}
1024
#if defined(TARGET_PPC64)
1025
#define gen_op_subfe_64 gen_op_subfe
1026
static always_inline void gen_op_subfeo_64 (void)
1027
{
1028
    tcg_gen_not_i64(cpu_T[2], cpu_T[0]);
1029
    gen_op_subfe_64();
1030
    gen_op_check_addo_64();
1031
}
1032
#endif
1033
GEN_INT_ARITH2_64 (subfe,  0x1F, 0x08, 0x04, PPC_INTEGER);
1034
/* subfme subfme. subfmeo subfmeo. */
1035
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
1036
/* subfze subfze. subfzeo subfzeo. */
1037
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
1038
/* addi */
1039
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1040
{
1041
    target_long simm = SIMM(ctx->opcode);
1042

    
1043
    if (rA(ctx->opcode) == 0) {
1044
        /* li case */
1045
        tcg_gen_movi_tl(cpu_T[0], simm);
1046
    } else {
1047
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1048
        if (likely(simm != 0))
1049
            gen_op_addi(simm);
1050
    }
1051
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1052
}
1053
/* addic */
1054
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1055
{
1056
    target_long simm = SIMM(ctx->opcode);
1057

    
1058
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1059
    if (likely(simm != 0)) {
1060
        tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1061
        gen_op_addi(simm);
1062
#if defined(TARGET_PPC64)
1063
        if (ctx->sf_mode)
1064
            gen_op_check_addc_64();
1065
        else
1066
#endif
1067
            gen_op_check_addc();
1068
    } else {
1069
        gen_op_clear_xer_ca();
1070
    }
1071
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1072
}
1073
/* addic. */
1074
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1075
{
1076
    target_long simm = SIMM(ctx->opcode);
1077

    
1078
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1079
    if (likely(simm != 0)) {
1080
        tcg_gen_mov_tl(cpu_T[2], cpu_T[0]);
1081
        gen_op_addi(simm);
1082
#if defined(TARGET_PPC64)
1083
        if (ctx->sf_mode)
1084
            gen_op_check_addc_64();
1085
        else
1086
#endif
1087
            gen_op_check_addc();
1088
    } else {
1089
        gen_op_clear_xer_ca();
1090
    }
1091
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1092
    gen_set_Rc0(ctx);
1093
}
1094
/* addis */
1095
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1096
{
1097
    target_long simm = SIMM(ctx->opcode);
1098

    
1099
    if (rA(ctx->opcode) == 0) {
1100
        /* lis case */
1101
        tcg_gen_movi_tl(cpu_T[0], simm << 16);
1102
    } else {
1103
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1104
        if (likely(simm != 0))
1105
            gen_op_addi(simm << 16);
1106
    }
1107
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1108
}
1109
/* mulli */
1110
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1111
{
1112
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1113
    gen_op_mulli(SIMM(ctx->opcode));
1114
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1115
}
1116
/* subfic */
1117
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1118
{
1119
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1120
#if defined(TARGET_PPC64)
1121
    if (ctx->sf_mode)
1122
        gen_op_subfic_64(SIMM(ctx->opcode));
1123
    else
1124
#endif
1125
        gen_op_subfic(SIMM(ctx->opcode));
1126
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1127
}
1128

    
1129
#if defined(TARGET_PPC64)
1130
/* mulhd  mulhd.                   */
1131
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
1132
/* mulhdu mulhdu.                  */
1133
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
1134
/* mulld  mulld.  mulldo  mulldo.  */
1135
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
1136
/* divd   divd.   divdo   divdo.   */
1137
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
1138
/* divdu  divdu.  divduo  divduo.  */
1139
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
1140
#endif
1141

    
1142
/***                           Integer comparison                          ***/
1143
#if defined(TARGET_PPC64)
1144
#define GEN_CMP(name, opc, type)                                              \
1145
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
1146
{                                                                             \
1147
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
1148
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
1149
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))                           \
1150
        gen_op_##name##_64();                                                 \
1151
    else                                                                      \
1152
        gen_op_##name();                                                      \
1153
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);              \
1154
}
1155
#else
1156
#define GEN_CMP(name, opc, type)                                              \
1157
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
1158
{                                                                             \
1159
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);                       \
1160
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
1161
    gen_op_##name();                                                          \
1162
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);              \
1163
}
1164
#endif
1165

    
1166
/* cmp */
1167
GEN_CMP(cmp, 0x00, PPC_INTEGER);
1168
/* cmpi */
1169
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
1170
{
1171
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1172
#if defined(TARGET_PPC64)
1173
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1174
        gen_op_cmpi_64(SIMM(ctx->opcode));
1175
    else
1176
#endif
1177
        gen_op_cmpi(SIMM(ctx->opcode));
1178
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
1179
}
1180
/* cmpl */
1181
GEN_CMP(cmpl, 0x01, PPC_INTEGER);
1182
/* cmpli */
1183
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
1184
{
1185
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1186
#if defined(TARGET_PPC64)
1187
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1188
        gen_op_cmpli_64(UIMM(ctx->opcode));
1189
    else
1190
#endif
1191
        gen_op_cmpli(UIMM(ctx->opcode));
1192
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
1193
}
1194

    
1195
/* isel (PowerPC 2.03 specification) */
1196
GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
1197
{
1198
    uint32_t bi = rC(ctx->opcode);
1199
    uint32_t mask;
1200

    
1201
    if (rA(ctx->opcode) == 0) {
1202
        tcg_gen_movi_tl(cpu_T[0], 0);
1203
    } else {
1204
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1205
    }
1206
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
1207
    mask = 1 << (3 - (bi & 0x03));
1208
    tcg_gen_mov_i32(cpu_T[0], cpu_crf[bi >> 2]);
1209
    gen_op_test_true(mask);
1210
    gen_op_isel();
1211
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
1212
}
1213

    
1214
/***                            Integer logical                            ***/
1215
#define __GEN_LOGICAL2(name, opc2, opc3, type)                                \
1216
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, type)                         \
1217
{                                                                             \
1218
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);                       \
1219
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);                       \
1220
    gen_op_##name();                                                          \
1221
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
1222
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1223
        gen_set_Rc0(ctx);                                                     \
1224
}
1225
#define GEN_LOGICAL2(name, opc, type)                                         \
1226
__GEN_LOGICAL2(name, 0x1C, opc, type)
1227

    
1228
#define GEN_LOGICAL1(name, opc, type)                                         \
1229
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
1230
{                                                                             \
1231
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);                       \
1232
    gen_op_##name();                                                          \
1233
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
1234
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1235
        gen_set_Rc0(ctx);                                                     \
1236
}
1237

    
1238
/* and & and. */
1239
GEN_LOGICAL2(and, 0x00, PPC_INTEGER);
1240
/* andc & andc. */
1241
GEN_LOGICAL2(andc, 0x01, PPC_INTEGER);
1242
/* andi. */
1243
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1244
{
1245
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1246
    gen_op_andi_T0(UIMM(ctx->opcode));
1247
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1248
    gen_set_Rc0(ctx);
1249
}
1250
/* andis. */
1251
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1252
{
1253
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1254
    gen_op_andi_T0(UIMM(ctx->opcode) << 16);
1255
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1256
    gen_set_Rc0(ctx);
1257
}
1258

    
1259
/* cntlzw */
1260
GEN_LOGICAL1(cntlzw, 0x00, PPC_INTEGER);
1261
/* eqv & eqv. */
1262
GEN_LOGICAL2(eqv, 0x08, PPC_INTEGER);
1263
/* extsb & extsb. */
1264
GEN_LOGICAL1(extsb, 0x1D, PPC_INTEGER);
1265
/* extsh & extsh. */
1266
GEN_LOGICAL1(extsh, 0x1C, PPC_INTEGER);
1267
/* nand & nand. */
1268
GEN_LOGICAL2(nand, 0x0E, PPC_INTEGER);
1269
/* nor & nor. */
1270
GEN_LOGICAL2(nor, 0x03, PPC_INTEGER);
1271

    
1272
/* or & or. */
1273
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1274
{
1275
    int rs, ra, rb;
1276

    
1277
    rs = rS(ctx->opcode);
1278
    ra = rA(ctx->opcode);
1279
    rb = rB(ctx->opcode);
1280
    /* Optimisation for mr. ri case */
1281
    if (rs != ra || rs != rb) {
1282
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rs]);
1283
        if (rs != rb) {
1284
            tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rb]);
1285
            gen_op_or();
1286
        }
1287
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
1288
        if (unlikely(Rc(ctx->opcode) != 0))
1289
            gen_set_Rc0(ctx);
1290
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1291
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rs]);
1292
        gen_set_Rc0(ctx);
1293
#if defined(TARGET_PPC64)
1294
    } else {
1295
        switch (rs) {
1296
        case 1:
1297
            /* Set process priority to low */
1298
            gen_op_store_pri(2);
1299
            break;
1300
        case 6:
1301
            /* Set process priority to medium-low */
1302
            gen_op_store_pri(3);
1303
            break;
1304
        case 2:
1305
            /* Set process priority to normal */
1306
            gen_op_store_pri(4);
1307
            break;
1308
#if !defined(CONFIG_USER_ONLY)
1309
        case 31:
1310
            if (ctx->supervisor > 0) {
1311
                /* Set process priority to very low */
1312
                gen_op_store_pri(1);
1313
            }
1314
            break;
1315
        case 5:
1316
            if (ctx->supervisor > 0) {
1317
                /* Set process priority to medium-hight */
1318
                gen_op_store_pri(5);
1319
            }
1320
            break;
1321
        case 3:
1322
            if (ctx->supervisor > 0) {
1323
                /* Set process priority to high */
1324
                gen_op_store_pri(6);
1325
            }
1326
            break;
1327
        case 7:
1328
            if (ctx->supervisor > 1) {
1329
                /* Set process priority to very high */
1330
                gen_op_store_pri(7);
1331
            }
1332
            break;
1333
#endif
1334
        default:
1335
            /* nop */
1336
            break;
1337
        }
1338
#endif
1339
    }
1340
}
1341

    
1342
/* orc & orc. */
1343
GEN_LOGICAL2(orc, 0x0C, PPC_INTEGER);
1344
/* xor & xor. */
1345
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1346
{
1347
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1348
    /* Optimisation for "set to zero" case */
1349
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
1350
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
1351
        gen_op_xor();
1352
    } else {
1353
        tcg_gen_movi_tl(cpu_T[0], 0);
1354
    }
1355
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1356
    if (unlikely(Rc(ctx->opcode) != 0))
1357
        gen_set_Rc0(ctx);
1358
}
1359
/* ori */
1360
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1361
{
1362
    target_ulong uimm = UIMM(ctx->opcode);
1363

    
1364
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1365
        /* NOP */
1366
        /* XXX: should handle special NOPs for POWER series */
1367
        return;
1368
    }
1369
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1370
    if (likely(uimm != 0))
1371
        gen_op_ori(uimm);
1372
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1373
}
1374
/* oris */
1375
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1376
{
1377
    target_ulong uimm = UIMM(ctx->opcode);
1378

    
1379
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1380
        /* NOP */
1381
        return;
1382
    }
1383
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1384
    if (likely(uimm != 0))
1385
        gen_op_ori(uimm << 16);
1386
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1387
}
1388
/* xori */
1389
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1390
{
1391
    target_ulong uimm = UIMM(ctx->opcode);
1392

    
1393
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1394
        /* NOP */
1395
        return;
1396
    }
1397
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1398
    if (likely(uimm != 0))
1399
        gen_op_xori(uimm);
1400
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1401
}
1402

    
1403
/* xoris */
1404
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1405
{
1406
    target_ulong uimm = UIMM(ctx->opcode);
1407

    
1408
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1409
        /* NOP */
1410
        return;
1411
    }
1412
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1413
    if (likely(uimm != 0))
1414
        gen_op_xori(uimm << 16);
1415
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1416
}
1417

    
1418
/* popcntb : PowerPC 2.03 specification */
1419
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1420
{
1421
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1422
#if defined(TARGET_PPC64)
1423
    if (ctx->sf_mode)
1424
        gen_op_popcntb_64();
1425
    else
1426
#endif
1427
        gen_op_popcntb();
1428
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1429
}
1430

    
1431
#if defined(TARGET_PPC64)
1432
/* extsw & extsw. */
1433
GEN_LOGICAL1(extsw, 0x1E, PPC_64B);
1434
/* cntlzd */
1435
GEN_LOGICAL1(cntlzd, 0x01, PPC_64B);
1436
#endif
1437

    
1438
/***                             Integer rotate                            ***/
1439
/* rlwimi & rlwimi. */
1440
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1441
{
1442
    target_ulong mask;
1443
    uint32_t mb, me, sh;
1444

    
1445
    mb = MB(ctx->opcode);
1446
    me = ME(ctx->opcode);
1447
    sh = SH(ctx->opcode);
1448
    if (likely(sh == 0)) {
1449
        if (likely(mb == 0 && me == 31)) {
1450
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1451
            goto do_store;
1452
        } else if (likely(mb == 31 && me == 0)) {
1453
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
1454
            goto do_store;
1455
        }
1456
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1457
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1458
        goto do_mask;
1459
    }
1460
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1461
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1462
    gen_op_rotli32_T0(SH(ctx->opcode));
1463
 do_mask:
1464
#if defined(TARGET_PPC64)
1465
    mb += 32;
1466
    me += 32;
1467
#endif
1468
    mask = MASK(mb, me);
1469
    gen_op_andi_T0(mask);
1470
    gen_op_andi_T1(~mask);
1471
    gen_op_or();
1472
 do_store:
1473
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1474
    if (unlikely(Rc(ctx->opcode) != 0))
1475
        gen_set_Rc0(ctx);
1476
}
1477
/* rlwinm & rlwinm. */
1478
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1479
{
1480
    uint32_t mb, me, sh;
1481

    
1482
    sh = SH(ctx->opcode);
1483
    mb = MB(ctx->opcode);
1484
    me = ME(ctx->opcode);
1485
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1486
    if (likely(sh == 0)) {
1487
        goto do_mask;
1488
    }
1489
    if (likely(mb == 0)) {
1490
        if (likely(me == 31)) {
1491
            gen_op_rotli32_T0(sh);
1492
            goto do_store;
1493
        } else if (likely(me == (31 - sh))) {
1494
            gen_op_sli_T0(sh);
1495
            goto do_store;
1496
        }
1497
    } else if (likely(me == 31)) {
1498
        if (likely(sh == (32 - mb))) {
1499
            gen_op_srli_T0(mb);
1500
            goto do_store;
1501
        }
1502
    }
1503
    gen_op_rotli32_T0(sh);
1504
 do_mask:
1505
#if defined(TARGET_PPC64)
1506
    mb += 32;
1507
    me += 32;
1508
#endif
1509
    gen_op_andi_T0(MASK(mb, me));
1510
 do_store:
1511
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1512
    if (unlikely(Rc(ctx->opcode) != 0))
1513
        gen_set_Rc0(ctx);
1514
}
1515
/* rlwnm & rlwnm. */
1516
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1517
{
1518
    uint32_t mb, me;
1519

    
1520
    mb = MB(ctx->opcode);
1521
    me = ME(ctx->opcode);
1522
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1523
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
1524
    gen_op_rotl32_T0_T1();
1525
    if (unlikely(mb != 0 || me != 31)) {
1526
#if defined(TARGET_PPC64)
1527
        mb += 32;
1528
        me += 32;
1529
#endif
1530
        gen_op_andi_T0(MASK(mb, me));
1531
    }
1532
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1533
    if (unlikely(Rc(ctx->opcode) != 0))
1534
        gen_set_Rc0(ctx);
1535
}
1536

    
1537
#if defined(TARGET_PPC64)
1538
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1539
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1540
{                                                                             \
1541
    gen_##name(ctx, 0);                                                       \
1542
}                                                                             \
1543
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1544
             PPC_64B)                                                         \
1545
{                                                                             \
1546
    gen_##name(ctx, 1);                                                       \
1547
}
1548
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1549
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1550
{                                                                             \
1551
    gen_##name(ctx, 0, 0);                                                    \
1552
}                                                                             \
1553
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
1554
             PPC_64B)                                                         \
1555
{                                                                             \
1556
    gen_##name(ctx, 0, 1);                                                    \
1557
}                                                                             \
1558
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1559
             PPC_64B)                                                         \
1560
{                                                                             \
1561
    gen_##name(ctx, 1, 0);                                                    \
1562
}                                                                             \
1563
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
1564
             PPC_64B)                                                         \
1565
{                                                                             \
1566
    gen_##name(ctx, 1, 1);                                                    \
1567
}
1568

    
1569
static always_inline void gen_andi_T0_64 (DisasContext *ctx, uint64_t mask)
1570
{
1571
    if (mask >> 32)
1572
        gen_op_andi_T0_64(mask >> 32, mask & 0xFFFFFFFF);
1573
    else
1574
        gen_op_andi_T0(mask);
1575
}
1576

    
1577
static always_inline void gen_andi_T1_64 (DisasContext *ctx, uint64_t mask)
1578
{
1579
    if (mask >> 32)
1580
        gen_op_andi_T1_64(mask >> 32, mask & 0xFFFFFFFF);
1581
    else
1582
        gen_op_andi_T1(mask);
1583
}
1584

    
1585
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1586
                                      uint32_t me, uint32_t sh)
1587
{
1588
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1589
    if (likely(sh == 0)) {
1590
        goto do_mask;
1591
    }
1592
    if (likely(mb == 0)) {
1593
        if (likely(me == 63)) {
1594
            gen_op_rotli64_T0(sh);
1595
            goto do_store;
1596
        } else if (likely(me == (63 - sh))) {
1597
            gen_op_sli_T0(sh);
1598
            goto do_store;
1599
        }
1600
    } else if (likely(me == 63)) {
1601
        if (likely(sh == (64 - mb))) {
1602
            gen_op_srli_T0_64(mb);
1603
            goto do_store;
1604
        }
1605
    }
1606
    gen_op_rotli64_T0(sh);
1607
 do_mask:
1608
    gen_andi_T0_64(ctx, MASK(mb, me));
1609
 do_store:
1610
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1611
    if (unlikely(Rc(ctx->opcode) != 0))
1612
        gen_set_Rc0(ctx);
1613
}
1614
/* rldicl - rldicl. */
1615
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1616
{
1617
    uint32_t sh, mb;
1618

    
1619
    sh = SH(ctx->opcode) | (shn << 5);
1620
    mb = MB(ctx->opcode) | (mbn << 5);
1621
    gen_rldinm(ctx, mb, 63, sh);
1622
}
1623
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1624
/* rldicr - rldicr. */
1625
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1626
{
1627
    uint32_t sh, me;
1628

    
1629
    sh = SH(ctx->opcode) | (shn << 5);
1630
    me = MB(ctx->opcode) | (men << 5);
1631
    gen_rldinm(ctx, 0, me, sh);
1632
}
1633
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1634
/* rldic - rldic. */
1635
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1636
{
1637
    uint32_t sh, mb;
1638

    
1639
    sh = SH(ctx->opcode) | (shn << 5);
1640
    mb = MB(ctx->opcode) | (mbn << 5);
1641
    gen_rldinm(ctx, mb, 63 - sh, sh);
1642
}
1643
GEN_PPC64_R4(rldic, 0x1E, 0x04);
1644

    
1645
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1646
                                     uint32_t me)
1647
{
1648
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1649
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
1650
    gen_op_rotl64_T0_T1();
1651
    if (unlikely(mb != 0 || me != 63)) {
1652
        gen_andi_T0_64(ctx, MASK(mb, me));
1653
    }
1654
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1655
    if (unlikely(Rc(ctx->opcode) != 0))
1656
        gen_set_Rc0(ctx);
1657
}
1658

    
1659
/* rldcl - rldcl. */
1660
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1661
{
1662
    uint32_t mb;
1663

    
1664
    mb = MB(ctx->opcode) | (mbn << 5);
1665
    gen_rldnm(ctx, mb, 63);
1666
}
1667
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1668
/* rldcr - rldcr. */
1669
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1670
{
1671
    uint32_t me;
1672

    
1673
    me = MB(ctx->opcode) | (men << 5);
1674
    gen_rldnm(ctx, 0, me);
1675
}
1676
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1677
/* rldimi - rldimi. */
1678
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1679
{
1680
    uint64_t mask;
1681
    uint32_t sh, mb, me;
1682

    
1683
    sh = SH(ctx->opcode) | (shn << 5);
1684
    mb = MB(ctx->opcode) | (mbn << 5);
1685
    me = 63 - sh;
1686
    if (likely(sh == 0)) {
1687
        if (likely(mb == 0)) {
1688
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1689
            goto do_store;
1690
        }
1691
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1692
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1693
        goto do_mask;
1694
    }
1695
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1696
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
1697
    gen_op_rotli64_T0(sh);
1698
 do_mask:
1699
    mask = MASK(mb, me);
1700
    gen_andi_T0_64(ctx, mask);
1701
    gen_andi_T1_64(ctx, ~mask);
1702
    gen_op_or();
1703
 do_store:
1704
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1705
    if (unlikely(Rc(ctx->opcode) != 0))
1706
        gen_set_Rc0(ctx);
1707
}
1708
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1709
#endif
1710

    
1711
/***                             Integer shift                             ***/
1712
/* slw & slw. */
1713
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
1714
/* sraw & sraw. */
1715
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
1716
/* srawi & srawi. */
1717
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1718
{
1719
    int mb, me;
1720
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1721
    if (SH(ctx->opcode) != 0) {
1722
        tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
1723
        mb = 32 - SH(ctx->opcode);
1724
        me = 31;
1725
#if defined(TARGET_PPC64)
1726
        mb += 32;
1727
        me += 32;
1728
#endif
1729
        gen_op_srawi(SH(ctx->opcode), MASK(mb, me));
1730
    }
1731
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1732
    if (unlikely(Rc(ctx->opcode) != 0))
1733
        gen_set_Rc0(ctx);
1734
}
1735
/* srw & srw. */
1736
__GEN_LOGICAL2(srw, 0x18, 0x10, PPC_INTEGER);
1737

    
1738
#if defined(TARGET_PPC64)
1739
/* sld & sld. */
1740
__GEN_LOGICAL2(sld, 0x1B, 0x00, PPC_64B);
1741
/* srad & srad. */
1742
__GEN_LOGICAL2(srad, 0x1A, 0x18, PPC_64B);
1743
/* sradi & sradi. */
1744
static always_inline void gen_sradi (DisasContext *ctx, int n)
1745
{
1746
    uint64_t mask;
1747
    int sh, mb, me;
1748

    
1749
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
1750
    sh = SH(ctx->opcode) + (n << 5);
1751
    if (sh != 0) {
1752
        tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
1753
        mb = 64 - SH(ctx->opcode);
1754
        me = 63;
1755
        mask = MASK(mb, me);
1756
        gen_op_sradi(sh, mask >> 32, mask);
1757
    }
1758
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
1759
    if (unlikely(Rc(ctx->opcode) != 0))
1760
        gen_set_Rc0(ctx);
1761
}
1762
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
1763
{
1764
    gen_sradi(ctx, 0);
1765
}
1766
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
1767
{
1768
    gen_sradi(ctx, 1);
1769
}
1770
/* srd & srd. */
1771
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
1772
#endif
1773

    
1774
/***                       Floating-Point arithmetic                       ***/
1775
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
1776
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1777
{                                                                             \
1778
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1779
        GEN_EXCP_NO_FP(ctx);                                                  \
1780
        return;                                                               \
1781
    }                                                                         \
1782
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
1783
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rC(ctx->opcode)]);                     \
1784
    tcg_gen_mov_i64(cpu_FT[2], cpu_fpr[rB(ctx->opcode)]);                     \
1785
    gen_reset_fpstatus();                                                     \
1786
    gen_op_f##op();                                                           \
1787
    if (isfloat) {                                                            \
1788
        gen_op_frsp();                                                        \
1789
    }                                                                         \
1790
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1791
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1792
}
1793

    
1794
#define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
1795
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
1796
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
1797

    
1798
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
1799
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1800
{                                                                             \
1801
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1802
        GEN_EXCP_NO_FP(ctx);                                                  \
1803
        return;                                                               \
1804
    }                                                                         \
1805
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
1806
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);                     \
1807
    gen_reset_fpstatus();                                                     \
1808
    gen_op_f##op();                                                           \
1809
    if (isfloat) {                                                            \
1810
        gen_op_frsp();                                                        \
1811
    }                                                                         \
1812
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1813
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1814
}
1815
#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
1816
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
1817
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
1818

    
1819
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
1820
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1821
{                                                                             \
1822
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1823
        GEN_EXCP_NO_FP(ctx);                                                  \
1824
        return;                                                               \
1825
    }                                                                         \
1826
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);                     \
1827
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rC(ctx->opcode)]);                     \
1828
    gen_reset_fpstatus();                                                     \
1829
    gen_op_f##op();                                                           \
1830
    if (isfloat) {                                                            \
1831
        gen_op_frsp();                                                        \
1832
    }                                                                         \
1833
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1834
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1835
}
1836
#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
1837
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
1838
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
1839

    
1840
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
1841
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
1842
{                                                                             \
1843
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1844
        GEN_EXCP_NO_FP(ctx);                                                  \
1845
        return;                                                               \
1846
    }                                                                         \
1847
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);                     \
1848
    gen_reset_fpstatus();                                                     \
1849
    gen_op_f##name();                                                         \
1850
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1851
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1852
}
1853

    
1854
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
1855
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
1856
{                                                                             \
1857
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1858
        GEN_EXCP_NO_FP(ctx);                                                  \
1859
        return;                                                               \
1860
    }                                                                         \
1861
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);                     \
1862
    gen_reset_fpstatus();                                                     \
1863
    gen_op_f##name();                                                         \
1864
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
1865
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1866
}
1867

    
1868
/* fadd - fadds */
1869
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
1870
/* fdiv - fdivs */
1871
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
1872
/* fmul - fmuls */
1873
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
1874

    
1875
/* fre */
1876
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
1877

    
1878
/* fres */
1879
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
1880

    
1881
/* frsqrte */
1882
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
1883

    
1884
/* frsqrtes */
1885
static always_inline void gen_op_frsqrtes (void)
1886
{
1887
    gen_op_frsqrte();
1888
    gen_op_frsp();
1889
}
1890
GEN_FLOAT_BS(rsqrtes, 0x3B, 0x1A, 1, PPC_FLOAT_FRSQRTES);
1891

    
1892
/* fsel */
1893
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
1894
/* fsub - fsubs */
1895
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
1896
/* Optional: */
1897
/* fsqrt */
1898
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1899
{
1900
    if (unlikely(!ctx->fpu_enabled)) {
1901
        GEN_EXCP_NO_FP(ctx);
1902
        return;
1903
    }
1904
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
1905
    gen_reset_fpstatus();
1906
    gen_op_fsqrt();
1907
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
1908
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
1909
}
1910

    
1911
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1912
{
1913
    if (unlikely(!ctx->fpu_enabled)) {
1914
        GEN_EXCP_NO_FP(ctx);
1915
        return;
1916
    }
1917
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
1918
    gen_reset_fpstatus();
1919
    gen_op_fsqrt();
1920
    gen_op_frsp();
1921
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
1922
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
1923
}
1924

    
1925
/***                     Floating-Point multiply-and-add                   ***/
1926
/* fmadd - fmadds */
1927
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
1928
/* fmsub - fmsubs */
1929
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
1930
/* fnmadd - fnmadds */
1931
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
1932
/* fnmsub - fnmsubs */
1933
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
1934

    
1935
/***                     Floating-Point round & convert                    ***/
1936
/* fctiw */
1937
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
1938
/* fctiwz */
1939
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
1940
/* frsp */
1941
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
1942
#if defined(TARGET_PPC64)
1943
/* fcfid */
1944
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
1945
/* fctid */
1946
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
1947
/* fctidz */
1948
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
1949
#endif
1950

    
1951
/* frin */
1952
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
1953
/* friz */
1954
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
1955
/* frip */
1956
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
1957
/* frim */
1958
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
1959

    
1960
/***                         Floating-Point compare                        ***/
1961
/* fcmpo */
1962
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
1963
{
1964
    if (unlikely(!ctx->fpu_enabled)) {
1965
        GEN_EXCP_NO_FP(ctx);
1966
        return;
1967
    }
1968
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);
1969
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);
1970
    gen_reset_fpstatus();
1971
    gen_op_fcmpo();
1972
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
1973
    gen_op_float_check_status();
1974
}
1975

    
1976
/* fcmpu */
1977
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
1978
{
1979
    if (unlikely(!ctx->fpu_enabled)) {
1980
        GEN_EXCP_NO_FP(ctx);
1981
        return;
1982
    }
1983
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rA(ctx->opcode)]);
1984
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rB(ctx->opcode)]);
1985
    gen_reset_fpstatus();
1986
    gen_op_fcmpu();
1987
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
1988
    gen_op_float_check_status();
1989
}
1990

    
1991
/***                         Floating-point move                           ***/
1992
/* fabs */
1993
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
1994
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
1995

    
1996
/* fmr  - fmr. */
1997
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
1998
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
1999
{
2000
    if (unlikely(!ctx->fpu_enabled)) {
2001
        GEN_EXCP_NO_FP(ctx);
2002
        return;
2003
    }
2004
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
2005
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
2006
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
2007
}
2008

    
2009
/* fnabs */
2010
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2011
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2012
/* fneg */
2013
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2014
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2015

    
2016
/***                  Floating-Point status & ctrl register                ***/
2017
/* mcrfs */
2018
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2019
{
2020
    int bfa;
2021

    
2022
    if (unlikely(!ctx->fpu_enabled)) {
2023
        GEN_EXCP_NO_FP(ctx);
2024
        return;
2025
    }
2026
    gen_optimize_fprf();
2027
    bfa = 4 * (7 - crfS(ctx->opcode));
2028
    gen_op_load_fpscr_T0(bfa);
2029
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
2030
    gen_op_fpscr_resetbit(~(0xF << bfa));
2031
}
2032

    
2033
/* mffs */
2034
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2035
{
2036
    if (unlikely(!ctx->fpu_enabled)) {
2037
        GEN_EXCP_NO_FP(ctx);
2038
        return;
2039
    }
2040
    gen_optimize_fprf();
2041
    gen_reset_fpstatus();
2042
    gen_op_load_fpscr_FT0();
2043
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
2044
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
2045
}
2046

    
2047
/* mtfsb0 */
2048
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2049
{
2050
    uint8_t crb;
2051

    
2052
    if (unlikely(!ctx->fpu_enabled)) {
2053
        GEN_EXCP_NO_FP(ctx);
2054
        return;
2055
    }
2056
    crb = 32 - (crbD(ctx->opcode) >> 2);
2057
    gen_optimize_fprf();
2058
    gen_reset_fpstatus();
2059
    if (likely(crb != 30 && crb != 29))
2060
        gen_op_fpscr_resetbit(~(1 << crb));
2061
    if (unlikely(Rc(ctx->opcode) != 0)) {
2062
        gen_op_load_fpcc();
2063
        gen_op_set_Rc0();
2064
    }
2065
}
2066

    
2067
/* mtfsb1 */
2068
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2069
{
2070
    uint8_t crb;
2071

    
2072
    if (unlikely(!ctx->fpu_enabled)) {
2073
        GEN_EXCP_NO_FP(ctx);
2074
        return;
2075
    }
2076
    crb = 32 - (crbD(ctx->opcode) >> 2);
2077
    gen_optimize_fprf();
2078
    gen_reset_fpstatus();
2079
    /* XXX: we pretend we can only do IEEE floating-point computations */
2080
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI))
2081
        gen_op_fpscr_setbit(crb);
2082
    if (unlikely(Rc(ctx->opcode) != 0)) {
2083
        gen_op_load_fpcc();
2084
        gen_op_set_Rc0();
2085
    }
2086
    /* We can raise a differed exception */
2087
    gen_op_float_check_status();
2088
}
2089

    
2090
/* mtfsf */
2091
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2092
{
2093
    if (unlikely(!ctx->fpu_enabled)) {
2094
        GEN_EXCP_NO_FP(ctx);
2095
        return;
2096
    }
2097
    gen_optimize_fprf();
2098
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rB(ctx->opcode)]);
2099
    gen_reset_fpstatus();
2100
    gen_op_store_fpscr(FM(ctx->opcode));
2101
    if (unlikely(Rc(ctx->opcode) != 0)) {
2102
        gen_op_load_fpcc();
2103
        gen_op_set_Rc0();
2104
    }
2105
    /* We can raise a differed exception */
2106
    gen_op_float_check_status();
2107
}
2108

    
2109
/* mtfsfi */
2110
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2111
{
2112
    int bf, sh;
2113

    
2114
    if (unlikely(!ctx->fpu_enabled)) {
2115
        GEN_EXCP_NO_FP(ctx);
2116
        return;
2117
    }
2118
    bf = crbD(ctx->opcode) >> 2;
2119
    sh = 7 - bf;
2120
    gen_optimize_fprf();
2121
    gen_op_set_FT0(FPIMM(ctx->opcode) << (4 * sh));
2122
    gen_reset_fpstatus();
2123
    gen_op_store_fpscr(1 << sh);
2124
    if (unlikely(Rc(ctx->opcode) != 0)) {
2125
        gen_op_load_fpcc();
2126
        gen_op_set_Rc0();
2127
    }
2128
    /* We can raise a differed exception */
2129
    gen_op_float_check_status();
2130
}
2131

    
2132
/***                           Addressing modes                            ***/
2133
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2134
static always_inline void gen_addr_imm_index (DisasContext *ctx,
2135
                                              target_long maskl)
2136
{
2137
    target_long simm = SIMM(ctx->opcode);
2138

    
2139
    simm &= ~maskl;
2140
    if (rA(ctx->opcode) == 0) {
2141
        tcg_gen_movi_tl(cpu_T[0], simm);
2142
    } else {
2143
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
2144
        if (likely(simm != 0))
2145
            gen_op_addi(simm);
2146
    }
2147
#ifdef DEBUG_MEMORY_ACCESSES
2148
    gen_op_print_mem_EA();
2149
#endif
2150
}
2151

    
2152
static always_inline void gen_addr_reg_index (DisasContext *ctx)
2153
{
2154
    if (rA(ctx->opcode) == 0) {
2155
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
2156
    } else {
2157
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
2158
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
2159
        gen_op_add();
2160
    }
2161
#ifdef DEBUG_MEMORY_ACCESSES
2162
    gen_op_print_mem_EA();
2163
#endif
2164
}
2165

    
2166
static always_inline void gen_addr_register (DisasContext *ctx)
2167
{
2168
    if (rA(ctx->opcode) == 0) {
2169
        tcg_gen_movi_tl(cpu_T[0], 0);
2170
    } else {
2171
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
2172
    }
2173
#ifdef DEBUG_MEMORY_ACCESSES
2174
    gen_op_print_mem_EA();
2175
#endif
2176
}
2177

    
2178
#if defined(TARGET_PPC64)
2179
#define _GEN_MEM_FUNCS(name, mode)                                            \
2180
    &gen_op_##name##_##mode,                                                  \
2181
    &gen_op_##name##_le_##mode,                                               \
2182
    &gen_op_##name##_64_##mode,                                               \
2183
    &gen_op_##name##_le_64_##mode
2184
#else
2185
#define _GEN_MEM_FUNCS(name, mode)                                            \
2186
    &gen_op_##name##_##mode,                                                  \
2187
    &gen_op_##name##_le_##mode
2188
#endif
2189
#if defined(CONFIG_USER_ONLY)
2190
#if defined(TARGET_PPC64)
2191
#define NB_MEM_FUNCS 4
2192
#else
2193
#define NB_MEM_FUNCS 2
2194
#endif
2195
#define GEN_MEM_FUNCS(name)                                                   \
2196
    _GEN_MEM_FUNCS(name, raw)
2197
#else
2198
#if defined(TARGET_PPC64)
2199
#define NB_MEM_FUNCS 12
2200
#else
2201
#define NB_MEM_FUNCS 6
2202
#endif
2203
#define GEN_MEM_FUNCS(name)                                                   \
2204
    _GEN_MEM_FUNCS(name, user),                                               \
2205
    _GEN_MEM_FUNCS(name, kernel),                                             \
2206
    _GEN_MEM_FUNCS(name, hypv)
2207
#endif
2208

    
2209
/***                             Integer load                              ***/
2210
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
2211
/* Byte access routine are endian safe */
2212
#define gen_op_lbz_le_raw       gen_op_lbz_raw
2213
#define gen_op_lbz_le_user      gen_op_lbz_user
2214
#define gen_op_lbz_le_kernel    gen_op_lbz_kernel
2215
#define gen_op_lbz_le_hypv      gen_op_lbz_hypv
2216
#define gen_op_lbz_le_64_raw    gen_op_lbz_64_raw
2217
#define gen_op_lbz_le_64_user   gen_op_lbz_64_user
2218
#define gen_op_lbz_le_64_kernel gen_op_lbz_64_kernel
2219
#define gen_op_lbz_le_64_hypv   gen_op_lbz_64_hypv
2220
#define gen_op_stb_le_raw       gen_op_stb_raw
2221
#define gen_op_stb_le_user      gen_op_stb_user
2222
#define gen_op_stb_le_kernel    gen_op_stb_kernel
2223
#define gen_op_stb_le_hypv      gen_op_stb_hypv
2224
#define gen_op_stb_le_64_raw    gen_op_stb_64_raw
2225
#define gen_op_stb_le_64_user   gen_op_stb_64_user
2226
#define gen_op_stb_le_64_kernel gen_op_stb_64_kernel
2227
#define gen_op_stb_le_64_hypv   gen_op_stb_64_hypv
2228
#define OP_LD_TABLE(width)                                                    \
2229
static GenOpFunc *gen_op_l##width[NB_MEM_FUNCS] = {                           \
2230
    GEN_MEM_FUNCS(l##width),                                                  \
2231
};
2232
#define OP_ST_TABLE(width)                                                    \
2233
static GenOpFunc *gen_op_st##width[NB_MEM_FUNCS] = {                          \
2234
    GEN_MEM_FUNCS(st##width),                                                 \
2235
};
2236

    
2237
#define GEN_LD(width, opc, type)                                              \
2238
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
2239
{                                                                             \
2240
    gen_addr_imm_index(ctx, 0);                                               \
2241
    op_ldst(l##width);                                                        \
2242
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);                       \
2243
}
2244

    
2245
#define GEN_LDU(width, opc, type)                                             \
2246
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
2247
{                                                                             \
2248
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2249
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2250
        GEN_EXCP_INVAL(ctx);                                                  \
2251
        return;                                                               \
2252
    }                                                                         \
2253
    if (type == PPC_64B)                                                      \
2254
        gen_addr_imm_index(ctx, 0x03);                                        \
2255
    else                                                                      \
2256
        gen_addr_imm_index(ctx, 0);                                           \
2257
    op_ldst(l##width);                                                        \
2258
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);                       \
2259
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2260
}
2261

    
2262
#define GEN_LDUX(width, opc2, opc3, type)                                     \
2263
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
2264
{                                                                             \
2265
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2266
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2267
        GEN_EXCP_INVAL(ctx);                                                  \
2268
        return;                                                               \
2269
    }                                                                         \
2270
    gen_addr_reg_index(ctx);                                                  \
2271
    op_ldst(l##width);                                                        \
2272
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);                       \
2273
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2274
}
2275

    
2276
#define GEN_LDX(width, opc2, opc3, type)                                      \
2277
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
2278
{                                                                             \
2279
    gen_addr_reg_index(ctx);                                                  \
2280
    op_ldst(l##width);                                                        \
2281
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);                       \
2282
}
2283

    
2284
#define GEN_LDS(width, op, type)                                              \
2285
OP_LD_TABLE(width);                                                           \
2286
GEN_LD(width, op | 0x20, type);                                               \
2287
GEN_LDU(width, op | 0x21, type);                                              \
2288
GEN_LDUX(width, 0x17, op | 0x01, type);                                       \
2289
GEN_LDX(width, 0x17, op | 0x00, type)
2290

    
2291
/* lbz lbzu lbzux lbzx */
2292
GEN_LDS(bz, 0x02, PPC_INTEGER);
2293
/* lha lhau lhaux lhax */
2294
GEN_LDS(ha, 0x0A, PPC_INTEGER);
2295
/* lhz lhzu lhzux lhzx */
2296
GEN_LDS(hz, 0x08, PPC_INTEGER);
2297
/* lwz lwzu lwzux lwzx */
2298
GEN_LDS(wz, 0x00, PPC_INTEGER);
2299
#if defined(TARGET_PPC64)
2300
OP_LD_TABLE(wa);
2301
OP_LD_TABLE(d);
2302
/* lwaux */
2303
GEN_LDUX(wa, 0x15, 0x0B, PPC_64B);
2304
/* lwax */
2305
GEN_LDX(wa, 0x15, 0x0A, PPC_64B);
2306
/* ldux */
2307
GEN_LDUX(d, 0x15, 0x01, PPC_64B);
2308
/* ldx */
2309
GEN_LDX(d, 0x15, 0x00, PPC_64B);
2310
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2311
{
2312
    if (Rc(ctx->opcode)) {
2313
        if (unlikely(rA(ctx->opcode) == 0 ||
2314
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2315
            GEN_EXCP_INVAL(ctx);
2316
            return;
2317
        }
2318
    }
2319
    gen_addr_imm_index(ctx, 0x03);
2320
    if (ctx->opcode & 0x02) {
2321
        /* lwa (lwau is undefined) */
2322
        op_ldst(lwa);
2323
    } else {
2324
        /* ld - ldu */
2325
        op_ldst(ld);
2326
    }
2327
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
2328
    if (Rc(ctx->opcode))
2329
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
2330
}
2331
/* lq */
2332
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2333
{
2334
#if defined(CONFIG_USER_ONLY)
2335
    GEN_EXCP_PRIVOPC(ctx);
2336
#else
2337
    int ra, rd;
2338

    
2339
    /* Restore CPU state */
2340
    if (unlikely(ctx->supervisor == 0)) {
2341
        GEN_EXCP_PRIVOPC(ctx);
2342
        return;
2343
    }
2344
    ra = rA(ctx->opcode);
2345
    rd = rD(ctx->opcode);
2346
    if (unlikely((rd & 1) || rd == ra)) {
2347
        GEN_EXCP_INVAL(ctx);
2348
        return;
2349
    }
2350
    if (unlikely(ctx->mem_idx & 1)) {
2351
        /* Little-endian mode is not handled */
2352
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2353
        return;
2354
    }
2355
    gen_addr_imm_index(ctx, 0x0F);
2356
    op_ldst(ld);
2357
    tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[1]);
2358
    gen_op_addi(8);
2359
    op_ldst(ld);
2360
    tcg_gen_mov_tl(cpu_gpr[rd + 1], cpu_T[1]);
2361
#endif
2362
}
2363
#endif
2364

    
2365
/***                              Integer store                            ***/
2366
#define GEN_ST(width, opc, type)                                              \
2367
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
2368
{                                                                             \
2369
    gen_addr_imm_index(ctx, 0);                                               \
2370
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);                       \
2371
    op_ldst(st##width);                                                       \
2372
}
2373

    
2374
#define GEN_STU(width, opc, type)                                             \
2375
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
2376
{                                                                             \
2377
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2378
        GEN_EXCP_INVAL(ctx);                                                  \
2379
        return;                                                               \
2380
    }                                                                         \
2381
    if (type == PPC_64B)                                                      \
2382
        gen_addr_imm_index(ctx, 0x03);                                        \
2383
    else                                                                      \
2384
        gen_addr_imm_index(ctx, 0);                                           \
2385
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);                       \
2386
    op_ldst(st##width);                                                       \
2387
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2388
}
2389

    
2390
#define GEN_STUX(width, opc2, opc3, type)                                     \
2391
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
2392
{                                                                             \
2393
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2394
        GEN_EXCP_INVAL(ctx);                                                  \
2395
        return;                                                               \
2396
    }                                                                         \
2397
    gen_addr_reg_index(ctx);                                                  \
2398
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);                       \
2399
    op_ldst(st##width);                                                       \
2400
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2401
}
2402

    
2403
#define GEN_STX(width, opc2, opc3, type)                                      \
2404
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
2405
{                                                                             \
2406
    gen_addr_reg_index(ctx);                                                  \
2407
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);                       \
2408
    op_ldst(st##width);                                                       \
2409
}
2410

    
2411
#define GEN_STS(width, op, type)                                              \
2412
OP_ST_TABLE(width);                                                           \
2413
GEN_ST(width, op | 0x20, type);                                               \
2414
GEN_STU(width, op | 0x21, type);                                              \
2415
GEN_STUX(width, 0x17, op | 0x01, type);                                       \
2416
GEN_STX(width, 0x17, op | 0x00, type)
2417

    
2418
/* stb stbu stbux stbx */
2419
GEN_STS(b, 0x06, PPC_INTEGER);
2420
/* sth sthu sthux sthx */
2421
GEN_STS(h, 0x0C, PPC_INTEGER);
2422
/* stw stwu stwux stwx */
2423
GEN_STS(w, 0x04, PPC_INTEGER);
2424
#if defined(TARGET_PPC64)
2425
OP_ST_TABLE(d);
2426
GEN_STUX(d, 0x15, 0x05, PPC_64B);
2427
GEN_STX(d, 0x15, 0x04, PPC_64B);
2428
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2429
{
2430
    int rs;
2431

    
2432
    rs = rS(ctx->opcode);
2433
    if ((ctx->opcode & 0x3) == 0x2) {
2434
#if defined(CONFIG_USER_ONLY)
2435
        GEN_EXCP_PRIVOPC(ctx);
2436
#else
2437
        /* stq */
2438
        if (unlikely(ctx->supervisor == 0)) {
2439
            GEN_EXCP_PRIVOPC(ctx);
2440
            return;
2441
        }
2442
        if (unlikely(rs & 1)) {
2443
            GEN_EXCP_INVAL(ctx);
2444
            return;
2445
        }
2446
        if (unlikely(ctx->mem_idx & 1)) {
2447
            /* Little-endian mode is not handled */
2448
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2449
            return;
2450
        }
2451
        gen_addr_imm_index(ctx, 0x03);
2452
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rs]);
2453
        op_ldst(std);
2454
        gen_op_addi(8);
2455
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rs + 1]);
2456
        op_ldst(std);
2457
#endif
2458
    } else {
2459
        /* std / stdu */
2460
        if (Rc(ctx->opcode)) {
2461
            if (unlikely(rA(ctx->opcode) == 0)) {
2462
                GEN_EXCP_INVAL(ctx);
2463
                return;
2464
            }
2465
        }
2466
        gen_addr_imm_index(ctx, 0x03);
2467
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rs]);
2468
        op_ldst(std);
2469
        if (Rc(ctx->opcode))
2470
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
2471
    }
2472
}
2473
#endif
2474
/***                Integer load and store with byte reverse               ***/
2475
/* lhbrx */
2476
OP_LD_TABLE(hbr);
2477
GEN_LDX(hbr, 0x16, 0x18, PPC_INTEGER);
2478
/* lwbrx */
2479
OP_LD_TABLE(wbr);
2480
GEN_LDX(wbr, 0x16, 0x10, PPC_INTEGER);
2481
/* sthbrx */
2482
OP_ST_TABLE(hbr);
2483
GEN_STX(hbr, 0x16, 0x1C, PPC_INTEGER);
2484
/* stwbrx */
2485
OP_ST_TABLE(wbr);
2486
GEN_STX(wbr, 0x16, 0x14, PPC_INTEGER);
2487

    
2488
/***                    Integer load and store multiple                    ***/
2489
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2490
static GenOpFunc1 *gen_op_lmw[NB_MEM_FUNCS] = {
2491
    GEN_MEM_FUNCS(lmw),
2492
};
2493
static GenOpFunc1 *gen_op_stmw[NB_MEM_FUNCS] = {
2494
    GEN_MEM_FUNCS(stmw),
2495
};
2496

    
2497
/* lmw */
2498
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
2499
{
2500
    /* NIP cannot be restored if the memory exception comes from an helper */
2501
    gen_update_nip(ctx, ctx->nip - 4);
2502
    gen_addr_imm_index(ctx, 0);
2503
    op_ldstm(lmw, rD(ctx->opcode));
2504
}
2505

    
2506
/* stmw */
2507
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
2508
{
2509
    /* NIP cannot be restored if the memory exception comes from an helper */
2510
    gen_update_nip(ctx, ctx->nip - 4);
2511
    gen_addr_imm_index(ctx, 0);
2512
    op_ldstm(stmw, rS(ctx->opcode));
2513
}
2514

    
2515
/***                    Integer load and store strings                     ***/
2516
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
2517
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
2518
/* string load & stores are by definition endian-safe */
2519
#define gen_op_lswi_le_raw       gen_op_lswi_raw
2520
#define gen_op_lswi_le_user      gen_op_lswi_user
2521
#define gen_op_lswi_le_kernel    gen_op_lswi_kernel
2522
#define gen_op_lswi_le_hypv      gen_op_lswi_hypv
2523
#define gen_op_lswi_le_64_raw    gen_op_lswi_raw
2524
#define gen_op_lswi_le_64_user   gen_op_lswi_user
2525
#define gen_op_lswi_le_64_kernel gen_op_lswi_kernel
2526
#define gen_op_lswi_le_64_hypv   gen_op_lswi_hypv
2527
static GenOpFunc1 *gen_op_lswi[NB_MEM_FUNCS] = {
2528
    GEN_MEM_FUNCS(lswi),
2529
};
2530
#define gen_op_lswx_le_raw       gen_op_lswx_raw
2531
#define gen_op_lswx_le_user      gen_op_lswx_user
2532
#define gen_op_lswx_le_kernel    gen_op_lswx_kernel
2533
#define gen_op_lswx_le_hypv      gen_op_lswx_hypv
2534
#define gen_op_lswx_le_64_raw    gen_op_lswx_raw
2535
#define gen_op_lswx_le_64_user   gen_op_lswx_user
2536
#define gen_op_lswx_le_64_kernel gen_op_lswx_kernel
2537
#define gen_op_lswx_le_64_hypv   gen_op_lswx_hypv
2538
static GenOpFunc3 *gen_op_lswx[NB_MEM_FUNCS] = {
2539
    GEN_MEM_FUNCS(lswx),
2540
};
2541
#define gen_op_stsw_le_raw       gen_op_stsw_raw
2542
#define gen_op_stsw_le_user      gen_op_stsw_user
2543
#define gen_op_stsw_le_kernel    gen_op_stsw_kernel
2544
#define gen_op_stsw_le_hypv      gen_op_stsw_hypv
2545
#define gen_op_stsw_le_64_raw    gen_op_stsw_raw
2546
#define gen_op_stsw_le_64_user   gen_op_stsw_user
2547
#define gen_op_stsw_le_64_kernel gen_op_stsw_kernel
2548
#define gen_op_stsw_le_64_hypv   gen_op_stsw_hypv
2549
static GenOpFunc1 *gen_op_stsw[NB_MEM_FUNCS] = {
2550
    GEN_MEM_FUNCS(stsw),
2551
};
2552

    
2553
/* lswi */
2554
/* PowerPC32 specification says we must generate an exception if
2555
 * rA is in the range of registers to be loaded.
2556
 * In an other hand, IBM says this is valid, but rA won't be loaded.
2557
 * For now, I'll follow the spec...
2558
 */
2559
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
2560
{
2561
    int nb = NB(ctx->opcode);
2562
    int start = rD(ctx->opcode);
2563
    int ra = rA(ctx->opcode);
2564
    int nr;
2565

    
2566
    if (nb == 0)
2567
        nb = 32;
2568
    nr = nb / 4;
2569
    if (unlikely(((start + nr) > 32  &&
2570
                  start <= ra && (start + nr - 32) > ra) ||
2571
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
2572
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
2573
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
2574
        return;
2575
    }
2576
    /* NIP cannot be restored if the memory exception comes from an helper */
2577
    gen_update_nip(ctx, ctx->nip - 4);
2578
    gen_addr_register(ctx);
2579
    tcg_gen_movi_tl(cpu_T[1], nb);
2580
    op_ldsts(lswi, start);
2581
}
2582

    
2583
/* lswx */
2584
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
2585
{
2586
    int ra = rA(ctx->opcode);
2587
    int rb = rB(ctx->opcode);
2588

    
2589
    /* NIP cannot be restored if the memory exception comes from an helper */
2590
    gen_update_nip(ctx, ctx->nip - 4);
2591
    gen_addr_reg_index(ctx);
2592
    if (ra == 0) {
2593
        ra = rb;
2594
    }
2595
    gen_op_load_xer_bc();
2596
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
2597
}
2598

    
2599
/* stswi */
2600
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
2601
{
2602
    int nb = NB(ctx->opcode);
2603

    
2604
    /* NIP cannot be restored if the memory exception comes from an helper */
2605
    gen_update_nip(ctx, ctx->nip - 4);
2606
    gen_addr_register(ctx);
2607
    if (nb == 0)
2608
        nb = 32;
2609
    tcg_gen_movi_tl(cpu_T[1], nb);
2610
    op_ldsts(stsw, rS(ctx->opcode));
2611
}
2612

    
2613
/* stswx */
2614
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
2615
{
2616
    /* NIP cannot be restored if the memory exception comes from an helper */
2617
    gen_update_nip(ctx, ctx->nip - 4);
2618
    gen_addr_reg_index(ctx);
2619
    gen_op_load_xer_bc();
2620
    op_ldsts(stsw, rS(ctx->opcode));
2621
}
2622

    
2623
/***                        Memory synchronisation                         ***/
2624
/* eieio */
2625
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
2626
{
2627
}
2628

    
2629
/* isync */
2630
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
2631
{
2632
    GEN_STOP(ctx);
2633
}
2634

    
2635
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
2636
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
2637
static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
2638
    GEN_MEM_FUNCS(lwarx),
2639
};
2640
static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
2641
    GEN_MEM_FUNCS(stwcx),
2642
};
2643

    
2644
/* lwarx */
2645
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
2646
{
2647
    /* NIP cannot be restored if the memory exception comes from an helper */
2648
    gen_update_nip(ctx, ctx->nip - 4);
2649
    gen_addr_reg_index(ctx);
2650
    op_lwarx();
2651
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
2652
}
2653

    
2654
/* stwcx. */
2655
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
2656
{
2657
    /* NIP cannot be restored if the memory exception comes from an helper */
2658
    gen_update_nip(ctx, ctx->nip - 4);
2659
    gen_addr_reg_index(ctx);
2660
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
2661
    op_stwcx();
2662
}
2663

    
2664
#if defined(TARGET_PPC64)
2665
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
2666
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
2667
static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
2668
    GEN_MEM_FUNCS(ldarx),
2669
};
2670
static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
2671
    GEN_MEM_FUNCS(stdcx),
2672
};
2673

    
2674
/* ldarx */
2675
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
2676
{
2677
    /* NIP cannot be restored if the memory exception comes from an helper */
2678
    gen_update_nip(ctx, ctx->nip - 4);
2679
    gen_addr_reg_index(ctx);
2680
    op_ldarx();
2681
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
2682
}
2683

    
2684
/* stdcx. */
2685
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
2686
{
2687
    /* NIP cannot be restored if the memory exception comes from an helper */
2688
    gen_update_nip(ctx, ctx->nip - 4);
2689
    gen_addr_reg_index(ctx);
2690
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
2691
    op_stdcx();
2692
}
2693
#endif /* defined(TARGET_PPC64) */
2694

    
2695
/* sync */
2696
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
2697
{
2698
}
2699

    
2700
/* wait */
2701
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
2702
{
2703
    /* Stop translation, as the CPU is supposed to sleep from now */
2704
    gen_op_wait();
2705
    GEN_EXCP(ctx, EXCP_HLT, 1);
2706
}
2707

    
2708
/***                         Floating-point load                           ***/
2709
#define GEN_LDF(width, opc, type)                                             \
2710
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
2711
{                                                                             \
2712
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2713
        GEN_EXCP_NO_FP(ctx);                                                  \
2714
        return;                                                               \
2715
    }                                                                         \
2716
    gen_addr_imm_index(ctx, 0);                                               \
2717
    op_ldst(l##width);                                                        \
2718
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
2719
}
2720

    
2721
#define GEN_LDUF(width, opc, type)                                            \
2722
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
2723
{                                                                             \
2724
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2725
        GEN_EXCP_NO_FP(ctx);                                                  \
2726
        return;                                                               \
2727
    }                                                                         \
2728
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2729
        GEN_EXCP_INVAL(ctx);                                                  \
2730
        return;                                                               \
2731
    }                                                                         \
2732
    gen_addr_imm_index(ctx, 0);                                               \
2733
    op_ldst(l##width);                                                        \
2734
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
2735
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2736
}
2737

    
2738
#define GEN_LDUXF(width, opc, type)                                           \
2739
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
2740
{                                                                             \
2741
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2742
        GEN_EXCP_NO_FP(ctx);                                                  \
2743
        return;                                                               \
2744
    }                                                                         \
2745
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2746
        GEN_EXCP_INVAL(ctx);                                                  \
2747
        return;                                                               \
2748
    }                                                                         \
2749
    gen_addr_reg_index(ctx);                                                  \
2750
    op_ldst(l##width);                                                        \
2751
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
2752
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2753
}
2754

    
2755
#define GEN_LDXF(width, opc2, opc3, type)                                     \
2756
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
2757
{                                                                             \
2758
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2759
        GEN_EXCP_NO_FP(ctx);                                                  \
2760
        return;                                                               \
2761
    }                                                                         \
2762
    gen_addr_reg_index(ctx);                                                  \
2763
    op_ldst(l##width);                                                        \
2764
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);                     \
2765
}
2766

    
2767
#define GEN_LDFS(width, op, type)                                             \
2768
OP_LD_TABLE(width);                                                           \
2769
GEN_LDF(width, op | 0x20, type);                                              \
2770
GEN_LDUF(width, op | 0x21, type);                                             \
2771
GEN_LDUXF(width, op | 0x01, type);                                            \
2772
GEN_LDXF(width, 0x17, op | 0x00, type)
2773

    
2774
/* lfd lfdu lfdux lfdx */
2775
GEN_LDFS(fd, 0x12, PPC_FLOAT);
2776
/* lfs lfsu lfsux lfsx */
2777
GEN_LDFS(fs, 0x10, PPC_FLOAT);
2778

    
2779
/***                         Floating-point store                          ***/
2780
#define GEN_STF(width, opc, type)                                             \
2781
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
2782
{                                                                             \
2783
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2784
        GEN_EXCP_NO_FP(ctx);                                                  \
2785
        return;                                                               \
2786
    }                                                                         \
2787
    gen_addr_imm_index(ctx, 0);                                               \
2788
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
2789
    op_ldst(st##width);                                                       \
2790
}
2791

    
2792
#define GEN_STUF(width, opc, type)                                            \
2793
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
2794
{                                                                             \
2795
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2796
        GEN_EXCP_NO_FP(ctx);                                                  \
2797
        return;                                                               \
2798
    }                                                                         \
2799
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2800
        GEN_EXCP_INVAL(ctx);                                                  \
2801
        return;                                                               \
2802
    }                                                                         \
2803
    gen_addr_imm_index(ctx, 0);                                               \
2804
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
2805
    op_ldst(st##width);                                                       \
2806
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2807
}
2808

    
2809
#define GEN_STUXF(width, opc, type)                                           \
2810
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
2811
{                                                                             \
2812
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2813
        GEN_EXCP_NO_FP(ctx);                                                  \
2814
        return;                                                               \
2815
    }                                                                         \
2816
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2817
        GEN_EXCP_INVAL(ctx);                                                  \
2818
        return;                                                               \
2819
    }                                                                         \
2820
    gen_addr_reg_index(ctx);                                                  \
2821
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
2822
    op_ldst(st##width);                                                       \
2823
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);                       \
2824
}
2825

    
2826
#define GEN_STXF(width, opc2, opc3, type)                                     \
2827
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
2828
{                                                                             \
2829
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2830
        GEN_EXCP_NO_FP(ctx);                                                  \
2831
        return;                                                               \
2832
    }                                                                         \
2833
    gen_addr_reg_index(ctx);                                                  \
2834
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);                     \
2835
    op_ldst(st##width);                                                       \
2836
}
2837

    
2838
#define GEN_STFS(width, op, type)                                             \
2839
OP_ST_TABLE(width);                                                           \
2840
GEN_STF(width, op | 0x20, type);                                              \
2841
GEN_STUF(width, op | 0x21, type);                                             \
2842
GEN_STUXF(width, op | 0x01, type);                                            \
2843
GEN_STXF(width, 0x17, op | 0x00, type)
2844

    
2845
/* stfd stfdu stfdux stfdx */
2846
GEN_STFS(fd, 0x16, PPC_FLOAT);
2847
/* stfs stfsu stfsux stfsx */
2848
GEN_STFS(fs, 0x14, PPC_FLOAT);
2849

    
2850
/* Optional: */
2851
/* stfiwx */
2852
OP_ST_TABLE(fiw);
2853
GEN_STXF(fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
2854

    
2855
/***                                Branch                                 ***/
2856
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
2857
                                       target_ulong dest)
2858
{
2859
    TranslationBlock *tb;
2860
    tb = ctx->tb;
2861
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
2862
        likely(!ctx->singlestep_enabled)) {
2863
        tcg_gen_goto_tb(n);
2864
        tcg_gen_movi_tl(cpu_T[1], dest);
2865
#if defined(TARGET_PPC64)
2866
        if (ctx->sf_mode)
2867
            gen_op_b_T1_64();
2868
        else
2869
#endif
2870
            gen_op_b_T1();
2871
        tcg_gen_exit_tb((long)tb + n);
2872
    } else {
2873
        tcg_gen_movi_tl(cpu_T[1], dest);
2874
#if defined(TARGET_PPC64)
2875
        if (ctx->sf_mode)
2876
            gen_op_b_T1_64();
2877
        else
2878
#endif
2879
            gen_op_b_T1();
2880
        if (unlikely(ctx->singlestep_enabled)) {
2881
            if ((ctx->singlestep_enabled &
2882
                 (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
2883
                ctx->exception == POWERPC_EXCP_BRANCH) {
2884
                target_ulong tmp = ctx->nip;
2885
                ctx->nip = dest;
2886
                GEN_EXCP(ctx, POWERPC_EXCP_TRACE, 0);
2887
                ctx->nip = tmp;
2888
            }
2889
            if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
2890
                gen_update_nip(ctx, dest);
2891
                gen_op_debug();
2892
            }
2893
        }
2894
        tcg_gen_exit_tb(0);
2895
    }
2896
}
2897

    
2898
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
2899
{
2900
#if defined(TARGET_PPC64)
2901
    if (ctx->sf_mode != 0 && (nip >> 32))
2902
        gen_op_setlr_64(ctx->nip >> 32, ctx->nip);
2903
    else
2904
#endif
2905
        gen_op_setlr(ctx->nip);
2906
}
2907

    
2908
/* b ba bl bla */
2909
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
2910
{
2911
    target_ulong li, target;
2912

    
2913
    ctx->exception = POWERPC_EXCP_BRANCH;
2914
    /* sign extend LI */
2915
#if defined(TARGET_PPC64)
2916
    if (ctx->sf_mode)
2917
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
2918
    else
2919
#endif
2920
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
2921
    if (likely(AA(ctx->opcode) == 0))
2922
        target = ctx->nip + li - 4;
2923
    else
2924
        target = li;
2925
#if defined(TARGET_PPC64)
2926
    if (!ctx->sf_mode)
2927
        target = (uint32_t)target;
2928
#endif
2929
    if (LK(ctx->opcode))
2930
        gen_setlr(ctx, ctx->nip);
2931
    gen_goto_tb(ctx, 0, target);
2932
}
2933

    
2934
#define BCOND_IM  0
2935
#define BCOND_LR  1
2936
#define BCOND_CTR 2
2937

    
2938
static always_inline void gen_bcond (DisasContext *ctx, int type)
2939
{
2940
    target_ulong target = 0;
2941
    target_ulong li;
2942
    uint32_t bo = BO(ctx->opcode);
2943
    uint32_t bi = BI(ctx->opcode);
2944
    uint32_t mask;
2945

    
2946
    ctx->exception = POWERPC_EXCP_BRANCH;
2947
    if ((bo & 0x4) == 0)
2948
        gen_op_dec_ctr();
2949
    switch(type) {
2950
    case BCOND_IM:
2951
        li = (target_long)((int16_t)(BD(ctx->opcode)));
2952
        if (likely(AA(ctx->opcode) == 0)) {
2953
            target = ctx->nip + li - 4;
2954
        } else {
2955
            target = li;
2956
        }
2957
#if defined(TARGET_PPC64)
2958
        if (!ctx->sf_mode)
2959
            target = (uint32_t)target;
2960
#endif
2961
        break;
2962
    case BCOND_CTR:
2963
        gen_op_movl_T1_ctr();
2964
        break;
2965
    default:
2966
    case BCOND_LR:
2967
        gen_op_movl_T1_lr();
2968
        break;
2969
    }
2970
    if (LK(ctx->opcode))
2971
        gen_setlr(ctx, ctx->nip);
2972
    if (bo & 0x10) {
2973
        /* No CR condition */
2974
        switch (bo & 0x6) {
2975
        case 0:
2976
#if defined(TARGET_PPC64)
2977
            if (ctx->sf_mode)
2978
                gen_op_test_ctr_64();
2979
            else
2980
#endif
2981
                gen_op_test_ctr();
2982
            break;
2983
        case 2:
2984
#if defined(TARGET_PPC64)
2985
            if (ctx->sf_mode)
2986
                gen_op_test_ctrz_64();
2987
            else
2988
#endif
2989
                gen_op_test_ctrz();
2990
            break;
2991
        default:
2992
        case 4:
2993
        case 6:
2994
            if (type == BCOND_IM) {
2995
                gen_goto_tb(ctx, 0, target);
2996
                return;
2997
            } else {
2998
#if defined(TARGET_PPC64)
2999
                if (ctx->sf_mode)
3000
                    gen_op_b_T1_64();
3001
                else
3002
#endif
3003
                    gen_op_b_T1();
3004
                goto no_test;
3005
            }
3006
            break;
3007
        }
3008
    } else {
3009
        mask = 1 << (3 - (bi & 0x03));
3010
        tcg_gen_mov_i32(cpu_T[0], cpu_crf[bi >> 2]);
3011
        if (bo & 0x8) {
3012
            switch (bo & 0x6) {
3013
            case 0:
3014
#if defined(TARGET_PPC64)
3015
                if (ctx->sf_mode)
3016
                    gen_op_test_ctr_true_64(mask);
3017
                else
3018
#endif
3019
                    gen_op_test_ctr_true(mask);
3020
                break;
3021
            case 2:
3022
#if defined(TARGET_PPC64)
3023
                if (ctx->sf_mode)
3024
                    gen_op_test_ctrz_true_64(mask);
3025
                else
3026
#endif
3027
                    gen_op_test_ctrz_true(mask);
3028
                break;
3029
            default:
3030
            case 4:
3031
            case 6:
3032
                gen_op_test_true(mask);
3033
                break;
3034
            }
3035
        } else {
3036
            switch (bo & 0x6) {
3037
            case 0:
3038
#if defined(TARGET_PPC64)
3039
                if (ctx->sf_mode)
3040
                    gen_op_test_ctr_false_64(mask);
3041
                else
3042
#endif
3043
                    gen_op_test_ctr_false(mask);
3044
                break;
3045
            case 2:
3046
#if defined(TARGET_PPC64)
3047
                if (ctx->sf_mode)
3048
                    gen_op_test_ctrz_false_64(mask);
3049
                else
3050
#endif
3051
                    gen_op_test_ctrz_false(mask);
3052
                break;
3053
            default:
3054
            case 4:
3055
            case 6:
3056
                gen_op_test_false(mask);
3057
                break;
3058
            }
3059
        }
3060
    }
3061
    if (type == BCOND_IM) {
3062
        int l1 = gen_new_label();
3063
        gen_op_jz_T0(l1);
3064
        gen_goto_tb(ctx, 0, target);
3065
        gen_set_label(l1);
3066
        gen_goto_tb(ctx, 1, ctx->nip);
3067
    } else {
3068
#if defined(TARGET_PPC64)
3069
        if (ctx->sf_mode)
3070
            gen_op_btest_T1_64(ctx->nip >> 32, ctx->nip);
3071
        else
3072
#endif
3073
            gen_op_btest_T1(ctx->nip);
3074
    no_test:
3075
        if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3076
            gen_update_nip(ctx, ctx->nip);
3077
            gen_op_debug();
3078
        }
3079
        tcg_gen_exit_tb(0);
3080
    }
3081
}
3082

    
3083
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3084
{
3085
    gen_bcond(ctx, BCOND_IM);
3086
}
3087

    
3088
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3089
{
3090
    gen_bcond(ctx, BCOND_CTR);
3091
}
3092

    
3093
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3094
{
3095
    gen_bcond(ctx, BCOND_LR);
3096
}
3097

    
3098
/***                      Condition register logical                       ***/
3099
#define GEN_CRLOGIC(op, opc)                                                  \
3100
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
3101
{                                                                             \
3102
    uint8_t bitmask;                                                          \
3103
    int sh;                                                                   \
3104
    tcg_gen_mov_i32(cpu_T[0], cpu_crf[crbA(ctx->opcode) >> 2]);               \
3105
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3106
    if (sh > 0)                                                               \
3107
        gen_op_srli_T0(sh);                                                   \
3108
    else if (sh < 0)                                                          \
3109
        gen_op_sli_T0(-sh);                                                   \
3110
    tcg_gen_mov_i32(cpu_T[1], cpu_crf[crbB(ctx->opcode) >> 2]);               \
3111
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3112
    if (sh > 0)                                                               \
3113
        gen_op_srli_T1(sh);                                                   \
3114
    else if (sh < 0)                                                          \
3115
        gen_op_sli_T1(-sh);                                                   \
3116
    gen_op_##op();                                                            \
3117
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3118
    gen_op_andi_T0(bitmask);                                                  \
3119
    tcg_gen_andi_i32(cpu_T[1], cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);    \
3120
    gen_op_or();                                                              \
3121
    tcg_gen_andi_i32(cpu_crf[crbD(ctx->opcode) >> 2], cpu_T[0], 0xf);         \
3122
}
3123

    
3124
/* crand */
3125
GEN_CRLOGIC(and, 0x08);
3126
/* crandc */
3127
GEN_CRLOGIC(andc, 0x04);
3128
/* creqv */
3129
GEN_CRLOGIC(eqv, 0x09);
3130
/* crnand */
3131
GEN_CRLOGIC(nand, 0x07);
3132
/* crnor */
3133
GEN_CRLOGIC(nor, 0x01);
3134
/* cror */
3135
GEN_CRLOGIC(or, 0x0E);
3136
/* crorc */
3137
GEN_CRLOGIC(orc, 0x0D);
3138
/* crxor */
3139
GEN_CRLOGIC(xor, 0x06);
3140
/* mcrf */
3141
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3142
{
3143
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3144
}
3145

    
3146
/***                           System linkage                              ***/
3147
/* rfi (supervisor only) */
3148
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3149
{
3150
#if defined(CONFIG_USER_ONLY)
3151
    GEN_EXCP_PRIVOPC(ctx);
3152
#else
3153
    /* Restore CPU state */
3154
    if (unlikely(!ctx->supervisor)) {
3155
        GEN_EXCP_PRIVOPC(ctx);
3156
        return;
3157
    }
3158
    gen_op_rfi();
3159
    GEN_SYNC(ctx);
3160
#endif
3161
}
3162

    
3163
#if defined(TARGET_PPC64)
3164
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3165
{
3166
#if defined(CONFIG_USER_ONLY)
3167
    GEN_EXCP_PRIVOPC(ctx);
3168
#else
3169
    /* Restore CPU state */
3170
    if (unlikely(!ctx->supervisor)) {
3171
        GEN_EXCP_PRIVOPC(ctx);
3172
        return;
3173
    }
3174
    gen_op_rfid();
3175
    GEN_SYNC(ctx);
3176
#endif
3177
}
3178

    
3179
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3180
{
3181
#if defined(CONFIG_USER_ONLY)
3182
    GEN_EXCP_PRIVOPC(ctx);
3183
#else
3184
    /* Restore CPU state */
3185
    if (unlikely(ctx->supervisor <= 1)) {
3186
        GEN_EXCP_PRIVOPC(ctx);
3187
        return;
3188
    }
3189
    gen_op_hrfid();
3190
    GEN_SYNC(ctx);
3191
#endif
3192
}
3193
#endif
3194

    
3195
/* sc */
3196
#if defined(CONFIG_USER_ONLY)
3197
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3198
#else
3199
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3200
#endif
3201
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3202
{
3203
    uint32_t lev;
3204

    
3205
    lev = (ctx->opcode >> 5) & 0x7F;
3206
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
3207
}
3208

    
3209
/***                                Trap                                   ***/
3210
/* tw */
3211
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3212
{
3213
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3214
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3215
    /* Update the nip since this might generate a trap exception */
3216
    gen_update_nip(ctx, ctx->nip);
3217
    gen_op_tw(TO(ctx->opcode));
3218
}
3219

    
3220
/* twi */
3221
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3222
{
3223
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3224
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3225
    /* Update the nip since this might generate a trap exception */
3226
    gen_update_nip(ctx, ctx->nip);
3227
    gen_op_tw(TO(ctx->opcode));
3228
}
3229

    
3230
#if defined(TARGET_PPC64)
3231
/* td */
3232
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3233
{
3234
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3235
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3236
    /* Update the nip since this might generate a trap exception */
3237
    gen_update_nip(ctx, ctx->nip);
3238
    gen_op_td(TO(ctx->opcode));
3239
}
3240

    
3241
/* tdi */
3242
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3243
{
3244
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3245
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3246
    /* Update the nip since this might generate a trap exception */
3247
    gen_update_nip(ctx, ctx->nip);
3248
    gen_op_td(TO(ctx->opcode));
3249
}
3250
#endif
3251

    
3252
/***                          Processor control                            ***/
3253
/* mcrxr */
3254
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3255
{
3256
    gen_op_load_xer_cr();
3257
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);
3258
    gen_op_clear_xer_ov();
3259
    gen_op_clear_xer_ca();
3260
}
3261

    
3262
/* mfcr */
3263
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3264
{
3265
    uint32_t crm, crn;
3266

    
3267
    if (likely(ctx->opcode & 0x00100000)) {
3268
        crm = CRM(ctx->opcode);
3269
        if (likely((crm ^ (crm - 1)) == 0)) {
3270
            crn = ffs(crm);
3271
            tcg_gen_mov_i32(cpu_T[0], cpu_crf[7 - crn]);
3272
        }
3273
    } else {
3274
        gen_op_load_cr();
3275
    }
3276
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3277
}
3278

    
3279
/* mfmsr */
3280
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3281
{
3282
#if defined(CONFIG_USER_ONLY)
3283
    GEN_EXCP_PRIVREG(ctx);
3284
#else
3285
    if (unlikely(!ctx->supervisor)) {
3286
        GEN_EXCP_PRIVREG(ctx);
3287
        return;
3288
    }
3289
    gen_op_load_msr();
3290
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3291
#endif
3292
}
3293

    
3294
#if 1
3295
#define SPR_NOACCESS ((void *)(-1UL))
3296
#else
3297
static void spr_noaccess (void *opaque, int sprn)
3298
{
3299
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3300
    printf("ERROR: try to access SPR %d !\n", sprn);
3301
}
3302
#define SPR_NOACCESS (&spr_noaccess)
3303
#endif
3304

    
3305
/* mfspr */
3306
static always_inline void gen_op_mfspr (DisasContext *ctx)
3307
{
3308
    void (*read_cb)(void *opaque, int sprn);
3309
    uint32_t sprn = SPR(ctx->opcode);
3310

    
3311
#if !defined(CONFIG_USER_ONLY)
3312
    if (ctx->supervisor == 2)
3313
        read_cb = ctx->spr_cb[sprn].hea_read;
3314
    else if (ctx->supervisor)
3315
        read_cb = ctx->spr_cb[sprn].oea_read;
3316
    else
3317
#endif
3318
        read_cb = ctx->spr_cb[sprn].uea_read;
3319
    if (likely(read_cb != NULL)) {
3320
        if (likely(read_cb != SPR_NOACCESS)) {
3321
            (*read_cb)(ctx, sprn);
3322
            tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3323
        } else {
3324
            /* Privilege exception */
3325
            /* This is a hack to avoid warnings when running Linux:
3326
             * this OS breaks the PowerPC virtualisation model,
3327
             * allowing userland application to read the PVR
3328
             */
3329
            if (sprn != SPR_PVR) {
3330
                if (loglevel != 0) {
3331
                    fprintf(logfile, "Trying to read privileged spr %d %03x at "
3332
                            ADDRX "\n", sprn, sprn, ctx->nip);
3333
                }
3334
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3335
                       sprn, sprn, ctx->nip);
3336
            }
3337
            GEN_EXCP_PRIVREG(ctx);
3338
        }
3339
    } else {
3340
        /* Not defined */
3341
        if (loglevel != 0) {
3342
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
3343
                    ADDRX "\n", sprn, sprn, ctx->nip);
3344
        }
3345
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3346
               sprn, sprn, ctx->nip);
3347
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3348
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3349
    }
3350
}
3351

    
3352
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3353
{
3354
    gen_op_mfspr(ctx);
3355
}
3356

    
3357
/* mftb */
3358
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3359
{
3360
    gen_op_mfspr(ctx);
3361
}
3362

    
3363
/* mtcrf */
3364
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3365
{
3366
    uint32_t crm, crn;
3367

    
3368
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3369
    crm = CRM(ctx->opcode);
3370
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3371
        crn = ffs(crm);
3372
        gen_op_srli_T0(crn * 4);
3373
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_T[0], 0xf);
3374
    } else {
3375
        gen_op_store_cr(crm);
3376
    }
3377
}
3378

    
3379
/* mtmsr */
3380
#if defined(TARGET_PPC64)
3381
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3382
{
3383
#if defined(CONFIG_USER_ONLY)
3384
    GEN_EXCP_PRIVREG(ctx);
3385
#else
3386
    if (unlikely(!ctx->supervisor)) {
3387
        GEN_EXCP_PRIVREG(ctx);
3388
        return;
3389
    }
3390
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3391
    if (ctx->opcode & 0x00010000) {
3392
        /* Special form that does not need any synchronisation */
3393
        gen_op_update_riee();
3394
    } else {
3395
        /* XXX: we need to update nip before the store
3396
         *      if we enter power saving mode, we will exit the loop
3397
         *      directly from ppc_store_msr
3398
         */
3399
        gen_update_nip(ctx, ctx->nip);
3400
        gen_op_store_msr();
3401
        /* Must stop the translation as machine state (may have) changed */
3402
        /* Note that mtmsr is not always defined as context-synchronizing */
3403
        ctx->exception = POWERPC_EXCP_STOP;
3404
    }
3405
#endif
3406
}
3407
#endif
3408

    
3409
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
3410
{
3411
#if defined(CONFIG_USER_ONLY)
3412
    GEN_EXCP_PRIVREG(ctx);
3413
#else
3414
    if (unlikely(!ctx->supervisor)) {
3415
        GEN_EXCP_PRIVREG(ctx);
3416
        return;
3417
    }
3418
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3419
    if (ctx->opcode & 0x00010000) {
3420
        /* Special form that does not need any synchronisation */
3421
        gen_op_update_riee();
3422
    } else {
3423
        /* XXX: we need to update nip before the store
3424
         *      if we enter power saving mode, we will exit the loop
3425
         *      directly from ppc_store_msr
3426
         */
3427
        gen_update_nip(ctx, ctx->nip);
3428
#if defined(TARGET_PPC64)
3429
        if (!ctx->sf_mode)
3430
            gen_op_store_msr_32();
3431
        else
3432
#endif
3433
            gen_op_store_msr();
3434
        /* Must stop the translation as machine state (may have) changed */
3435
        /* Note that mtmsrd is not always defined as context-synchronizing */
3436
        ctx->exception = POWERPC_EXCP_STOP;
3437
    }
3438
#endif
3439
}
3440

    
3441
/* mtspr */
3442
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
3443
{
3444
    void (*write_cb)(void *opaque, int sprn);
3445
    uint32_t sprn = SPR(ctx->opcode);
3446

    
3447
#if !defined(CONFIG_USER_ONLY)
3448
    if (ctx->supervisor == 2)
3449
        write_cb = ctx->spr_cb[sprn].hea_write;
3450
    else if (ctx->supervisor)
3451
        write_cb = ctx->spr_cb[sprn].oea_write;
3452
    else
3453
#endif
3454
        write_cb = ctx->spr_cb[sprn].uea_write;
3455
    if (likely(write_cb != NULL)) {
3456
        if (likely(write_cb != SPR_NOACCESS)) {
3457
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3458
            (*write_cb)(ctx, sprn);
3459
        } else {
3460
            /* Privilege exception */
3461
            if (loglevel != 0) {
3462
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
3463
                        ADDRX "\n", sprn, sprn, ctx->nip);
3464
            }
3465
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
3466
                   sprn, sprn, ctx->nip);
3467
            GEN_EXCP_PRIVREG(ctx);
3468
        }
3469
    } else {
3470
        /* Not defined */
3471
        if (loglevel != 0) {
3472
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
3473
                    ADDRX "\n", sprn, sprn, ctx->nip);
3474
        }
3475
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
3476
               sprn, sprn, ctx->nip);
3477
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3478
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3479
    }
3480
}
3481

    
3482
/***                         Cache management                              ***/
3483
/* dcbf */
3484
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
3485
{
3486
    /* XXX: specification says this is treated as a load by the MMU */
3487
    gen_addr_reg_index(ctx);
3488
    op_ldst(lbz);
3489
}
3490

    
3491
/* dcbi (Supervisor only) */
3492
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
3493
{
3494
#if defined(CONFIG_USER_ONLY)
3495
    GEN_EXCP_PRIVOPC(ctx);
3496
#else
3497
    if (unlikely(!ctx->supervisor)) {
3498
        GEN_EXCP_PRIVOPC(ctx);
3499
        return;
3500
    }
3501
    gen_addr_reg_index(ctx);
3502
    /* XXX: specification says this should be treated as a store by the MMU */
3503
    op_ldst(lbz);
3504
    op_ldst(stb);
3505
#endif
3506
}
3507

    
3508
/* dcdst */
3509
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
3510
{
3511
    /* XXX: specification say this is treated as a load by the MMU */
3512
    gen_addr_reg_index(ctx);
3513
    op_ldst(lbz);
3514
}
3515

    
3516
/* dcbt */
3517
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
3518
{
3519
    /* interpreted as no-op */
3520
    /* XXX: specification say this is treated as a load by the MMU
3521
     *      but does not generate any exception
3522
     */
3523
}
3524

    
3525
/* dcbtst */
3526
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
3527
{
3528
    /* interpreted as no-op */
3529
    /* XXX: specification say this is treated as a load by the MMU
3530
     *      but does not generate any exception
3531
     */
3532
}
3533

    
3534
/* dcbz */
3535
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
3536
static GenOpFunc *gen_op_dcbz[4][NB_MEM_FUNCS] = {
3537
    /* 32 bytes cache line size */
3538
    {
3539
#define gen_op_dcbz_l32_le_raw        gen_op_dcbz_l32_raw
3540
#define gen_op_dcbz_l32_le_user       gen_op_dcbz_l32_user
3541
#define gen_op_dcbz_l32_le_kernel     gen_op_dcbz_l32_kernel
3542
#define gen_op_dcbz_l32_le_hypv       gen_op_dcbz_l32_hypv
3543
#define gen_op_dcbz_l32_le_64_raw     gen_op_dcbz_l32_64_raw
3544
#define gen_op_dcbz_l32_le_64_user    gen_op_dcbz_l32_64_user
3545
#define gen_op_dcbz_l32_le_64_kernel  gen_op_dcbz_l32_64_kernel
3546
#define gen_op_dcbz_l32_le_64_hypv    gen_op_dcbz_l32_64_hypv
3547
        GEN_MEM_FUNCS(dcbz_l32),
3548
    },
3549
    /* 64 bytes cache line size */
3550
    {
3551
#define gen_op_dcbz_l64_le_raw        gen_op_dcbz_l64_raw
3552
#define gen_op_dcbz_l64_le_user       gen_op_dcbz_l64_user
3553
#define gen_op_dcbz_l64_le_kernel     gen_op_dcbz_l64_kernel
3554
#define gen_op_dcbz_l64_le_hypv       gen_op_dcbz_l64_hypv
3555
#define gen_op_dcbz_l64_le_64_raw     gen_op_dcbz_l64_64_raw
3556
#define gen_op_dcbz_l64_le_64_user    gen_op_dcbz_l64_64_user
3557
#define gen_op_dcbz_l64_le_64_kernel  gen_op_dcbz_l64_64_kernel
3558
#define gen_op_dcbz_l64_le_64_hypv    gen_op_dcbz_l64_64_hypv
3559
        GEN_MEM_FUNCS(dcbz_l64),
3560
    },
3561
    /* 128 bytes cache line size */
3562
    {
3563
#define gen_op_dcbz_l128_le_raw       gen_op_dcbz_l128_raw
3564
#define gen_op_dcbz_l128_le_user      gen_op_dcbz_l128_user
3565
#define gen_op_dcbz_l128_le_kernel    gen_op_dcbz_l128_kernel
3566
#define gen_op_dcbz_l128_le_hypv      gen_op_dcbz_l128_hypv
3567
#define gen_op_dcbz_l128_le_64_raw    gen_op_dcbz_l128_64_raw
3568
#define gen_op_dcbz_l128_le_64_user   gen_op_dcbz_l128_64_user
3569
#define gen_op_dcbz_l128_le_64_kernel gen_op_dcbz_l128_64_kernel
3570
#define gen_op_dcbz_l128_le_64_hypv   gen_op_dcbz_l128_64_hypv
3571
        GEN_MEM_FUNCS(dcbz_l128),
3572
    },
3573
    /* tunable cache line size */
3574
    {
3575
#define gen_op_dcbz_le_raw            gen_op_dcbz_raw
3576
#define gen_op_dcbz_le_user           gen_op_dcbz_user
3577
#define gen_op_dcbz_le_kernel         gen_op_dcbz_kernel
3578
#define gen_op_dcbz_le_hypv           gen_op_dcbz_hypv
3579
#define gen_op_dcbz_le_64_raw         gen_op_dcbz_64_raw
3580
#define gen_op_dcbz_le_64_user        gen_op_dcbz_64_user
3581
#define gen_op_dcbz_le_64_kernel      gen_op_dcbz_64_kernel
3582
#define gen_op_dcbz_le_64_hypv        gen_op_dcbz_64_hypv
3583
        GEN_MEM_FUNCS(dcbz),
3584
    },
3585
};
3586

    
3587
static always_inline void handler_dcbz (DisasContext *ctx,
3588
                                        int dcache_line_size)
3589
{
3590
    int n;
3591

    
3592
    switch (dcache_line_size) {
3593
    case 32:
3594
        n = 0;
3595
        break;
3596
    case 64:
3597
        n = 1;
3598
        break;
3599
    case 128:
3600
        n = 2;
3601
        break;
3602
    default:
3603
        n = 3;
3604
        break;
3605
    }
3606
    op_dcbz(n);
3607
}
3608

    
3609
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
3610
{
3611
    gen_addr_reg_index(ctx);
3612
    handler_dcbz(ctx, ctx->dcache_line_size);
3613
    gen_op_check_reservation();
3614
}
3615

    
3616
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
3617
{
3618
    gen_addr_reg_index(ctx);
3619
    if (ctx->opcode & 0x00200000)
3620
        handler_dcbz(ctx, ctx->dcache_line_size);
3621
    else
3622
        handler_dcbz(ctx, -1);
3623
    gen_op_check_reservation();
3624
}
3625

    
3626
/* icbi */
3627
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
3628
#define gen_op_icbi_le_raw       gen_op_icbi_raw
3629
#define gen_op_icbi_le_user      gen_op_icbi_user
3630
#define gen_op_icbi_le_kernel    gen_op_icbi_kernel
3631
#define gen_op_icbi_le_hypv      gen_op_icbi_hypv
3632
#define gen_op_icbi_le_64_raw    gen_op_icbi_64_raw
3633
#define gen_op_icbi_le_64_user   gen_op_icbi_64_user
3634
#define gen_op_icbi_le_64_kernel gen_op_icbi_64_kernel
3635
#define gen_op_icbi_le_64_hypv   gen_op_icbi_64_hypv
3636
static GenOpFunc *gen_op_icbi[NB_MEM_FUNCS] = {
3637
    GEN_MEM_FUNCS(icbi),
3638
};
3639

    
3640
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
3641
{
3642
    /* NIP cannot be restored if the memory exception comes from an helper */
3643
    gen_update_nip(ctx, ctx->nip - 4);
3644
    gen_addr_reg_index(ctx);
3645
    op_icbi();
3646
}
3647

    
3648
/* Optional: */
3649
/* dcba */
3650
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
3651
{
3652
    /* interpreted as no-op */
3653
    /* XXX: specification say this is treated as a store by the MMU
3654
     *      but does not generate any exception
3655
     */
3656
}
3657

    
3658
/***                    Segment register manipulation                      ***/
3659
/* Supervisor only: */
3660
/* mfsr */
3661
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
3662
{
3663
#if defined(CONFIG_USER_ONLY)
3664
    GEN_EXCP_PRIVREG(ctx);
3665
#else
3666
    if (unlikely(!ctx->supervisor)) {
3667
        GEN_EXCP_PRIVREG(ctx);
3668
        return;
3669
    }
3670
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
3671
    gen_op_load_sr();
3672
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3673
#endif
3674
}
3675

    
3676
/* mfsrin */
3677
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
3678
{
3679
#if defined(CONFIG_USER_ONLY)
3680
    GEN_EXCP_PRIVREG(ctx);
3681
#else
3682
    if (unlikely(!ctx->supervisor)) {
3683
        GEN_EXCP_PRIVREG(ctx);
3684
        return;
3685
    }
3686
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3687
    gen_op_srli_T1(28);
3688
    gen_op_load_sr();
3689
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3690
#endif
3691
}
3692

    
3693
/* mtsr */
3694
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
3695
{
3696
#if defined(CONFIG_USER_ONLY)
3697
    GEN_EXCP_PRIVREG(ctx);
3698
#else
3699
    if (unlikely(!ctx->supervisor)) {
3700
        GEN_EXCP_PRIVREG(ctx);
3701
        return;
3702
    }
3703
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3704
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
3705
    gen_op_store_sr();
3706
#endif
3707
}
3708

    
3709
/* mtsrin */
3710
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
3711
{
3712
#if defined(CONFIG_USER_ONLY)
3713
    GEN_EXCP_PRIVREG(ctx);
3714
#else
3715
    if (unlikely(!ctx->supervisor)) {
3716
        GEN_EXCP_PRIVREG(ctx);
3717
        return;
3718
    }
3719
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3720
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3721
    gen_op_srli_T1(28);
3722
    gen_op_store_sr();
3723
#endif
3724
}
3725

    
3726
#if defined(TARGET_PPC64)
3727
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
3728
/* mfsr */
3729
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
3730
{
3731
#if defined(CONFIG_USER_ONLY)
3732
    GEN_EXCP_PRIVREG(ctx);
3733
#else
3734
    if (unlikely(!ctx->supervisor)) {
3735
        GEN_EXCP_PRIVREG(ctx);
3736
        return;
3737
    }
3738
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
3739
    gen_op_load_slb();
3740
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3741
#endif
3742
}
3743

    
3744
/* mfsrin */
3745
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
3746
             PPC_SEGMENT_64B)
3747
{
3748
#if defined(CONFIG_USER_ONLY)
3749
    GEN_EXCP_PRIVREG(ctx);
3750
#else
3751
    if (unlikely(!ctx->supervisor)) {
3752
        GEN_EXCP_PRIVREG(ctx);
3753
        return;
3754
    }
3755
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3756
    gen_op_srli_T1(28);
3757
    gen_op_load_slb();
3758
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3759
#endif
3760
}
3761

    
3762
/* mtsr */
3763
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
3764
{
3765
#if defined(CONFIG_USER_ONLY)
3766
    GEN_EXCP_PRIVREG(ctx);
3767
#else
3768
    if (unlikely(!ctx->supervisor)) {
3769
        GEN_EXCP_PRIVREG(ctx);
3770
        return;
3771
    }
3772
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3773
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
3774
    gen_op_store_slb();
3775
#endif
3776
}
3777

    
3778
/* mtsrin */
3779
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
3780
             PPC_SEGMENT_64B)
3781
{
3782
#if defined(CONFIG_USER_ONLY)
3783
    GEN_EXCP_PRIVREG(ctx);
3784
#else
3785
    if (unlikely(!ctx->supervisor)) {
3786
        GEN_EXCP_PRIVREG(ctx);
3787
        return;
3788
    }
3789
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
3790
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3791
    gen_op_srli_T1(28);
3792
    gen_op_store_slb();
3793
#endif
3794
}
3795
#endif /* defined(TARGET_PPC64) */
3796

    
3797
/***                      Lookaside buffer management                      ***/
3798
/* Optional & supervisor only: */
3799
/* tlbia */
3800
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
3801
{
3802
#if defined(CONFIG_USER_ONLY)
3803
    GEN_EXCP_PRIVOPC(ctx);
3804
#else
3805
    if (unlikely(!ctx->supervisor)) {
3806
        GEN_EXCP_PRIVOPC(ctx);
3807
        return;
3808
    }
3809
    gen_op_tlbia();
3810
#endif
3811
}
3812

    
3813
/* tlbie */
3814
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
3815
{
3816
#if defined(CONFIG_USER_ONLY)
3817
    GEN_EXCP_PRIVOPC(ctx);
3818
#else
3819
    if (unlikely(!ctx->supervisor)) {
3820
        GEN_EXCP_PRIVOPC(ctx);
3821
        return;
3822
    }
3823
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
3824
#if defined(TARGET_PPC64)
3825
    if (ctx->sf_mode)
3826
        gen_op_tlbie_64();
3827
    else
3828
#endif
3829
        gen_op_tlbie();
3830
#endif
3831
}
3832

    
3833
/* tlbsync */
3834
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
3835
{
3836
#if defined(CONFIG_USER_ONLY)
3837
    GEN_EXCP_PRIVOPC(ctx);
3838
#else
3839
    if (unlikely(!ctx->supervisor)) {
3840
        GEN_EXCP_PRIVOPC(ctx);
3841
        return;
3842
    }
3843
    /* This has no effect: it should ensure that all previous
3844
     * tlbie have completed
3845
     */
3846
    GEN_STOP(ctx);
3847
#endif
3848
}
3849

    
3850
#if defined(TARGET_PPC64)
3851
/* slbia */
3852
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
3853
{
3854
#if defined(CONFIG_USER_ONLY)
3855
    GEN_EXCP_PRIVOPC(ctx);
3856
#else
3857
    if (unlikely(!ctx->supervisor)) {
3858
        GEN_EXCP_PRIVOPC(ctx);
3859
        return;
3860
    }
3861
    gen_op_slbia();
3862
#endif
3863
}
3864

    
3865
/* slbie */
3866
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
3867
{
3868
#if defined(CONFIG_USER_ONLY)
3869
    GEN_EXCP_PRIVOPC(ctx);
3870
#else
3871
    if (unlikely(!ctx->supervisor)) {
3872
        GEN_EXCP_PRIVOPC(ctx);
3873
        return;
3874
    }
3875
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
3876
    gen_op_slbie();
3877
#endif
3878
}
3879
#endif
3880

    
3881
/***                              External control                         ***/
3882
/* Optional: */
3883
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
3884
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
3885
static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
3886
    GEN_MEM_FUNCS(eciwx),
3887
};
3888
static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
3889
    GEN_MEM_FUNCS(ecowx),
3890
};
3891

    
3892
/* eciwx */
3893
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
3894
{
3895
    /* Should check EAR[E] & alignment ! */
3896
    gen_addr_reg_index(ctx);
3897
    op_eciwx();
3898
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3899
}
3900

    
3901
/* ecowx */
3902
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
3903
{
3904
    /* Should check EAR[E] & alignment ! */
3905
    gen_addr_reg_index(ctx);
3906
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3907
    op_ecowx();
3908
}
3909

    
3910
/* PowerPC 601 specific instructions */
3911
/* abs - abs. */
3912
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
3913
{
3914
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3915
    gen_op_POWER_abs();
3916
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3917
    if (unlikely(Rc(ctx->opcode) != 0))
3918
        gen_set_Rc0(ctx);
3919
}
3920

    
3921
/* abso - abso. */
3922
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
3923
{
3924
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3925
    gen_op_POWER_abso();
3926
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3927
    if (unlikely(Rc(ctx->opcode) != 0))
3928
        gen_set_Rc0(ctx);
3929
}
3930

    
3931
/* clcs */
3932
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
3933
{
3934
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3935
    gen_op_POWER_clcs();
3936
    /* Rc=1 sets CR0 to an undefined state */
3937
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3938
}
3939

    
3940
/* div - div. */
3941
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
3942
{
3943
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3944
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3945
    gen_op_POWER_div();
3946
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3947
    if (unlikely(Rc(ctx->opcode) != 0))
3948
        gen_set_Rc0(ctx);
3949
}
3950

    
3951
/* divo - divo. */
3952
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
3953
{
3954
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3955
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3956
    gen_op_POWER_divo();
3957
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3958
    if (unlikely(Rc(ctx->opcode) != 0))
3959
        gen_set_Rc0(ctx);
3960
}
3961

    
3962
/* divs - divs. */
3963
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
3964
{
3965
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3966
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3967
    gen_op_POWER_divs();
3968
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3969
    if (unlikely(Rc(ctx->opcode) != 0))
3970
        gen_set_Rc0(ctx);
3971
}
3972

    
3973
/* divso - divso. */
3974
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
3975
{
3976
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3977
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3978
    gen_op_POWER_divso();
3979
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3980
    if (unlikely(Rc(ctx->opcode) != 0))
3981
        gen_set_Rc0(ctx);
3982
}
3983

    
3984
/* doz - doz. */
3985
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
3986
{
3987
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3988
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3989
    gen_op_POWER_doz();
3990
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3991
    if (unlikely(Rc(ctx->opcode) != 0))
3992
        gen_set_Rc0(ctx);
3993
}
3994

    
3995
/* dozo - dozo. */
3996
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
3997
{
3998
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3999
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4000
    gen_op_POWER_dozo();
4001
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4002
    if (unlikely(Rc(ctx->opcode) != 0))
4003
        gen_set_Rc0(ctx);
4004
}
4005

    
4006
/* dozi */
4007
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4008
{
4009
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4010
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
4011
    gen_op_POWER_doz();
4012
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4013
}
4014

    
4015
/* As lscbx load from memory byte after byte, it's always endian safe.
4016
 * Original POWER is 32 bits only, define 64 bits ops as 32 bits ones
4017
 */
4018
#define op_POWER_lscbx(start, ra, rb)                                         \
4019
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4020
#define gen_op_POWER_lscbx_64_raw       gen_op_POWER_lscbx_raw
4021
#define gen_op_POWER_lscbx_64_user      gen_op_POWER_lscbx_user
4022
#define gen_op_POWER_lscbx_64_kernel    gen_op_POWER_lscbx_kernel
4023
#define gen_op_POWER_lscbx_64_hypv      gen_op_POWER_lscbx_hypv
4024
#define gen_op_POWER_lscbx_le_raw       gen_op_POWER_lscbx_raw
4025
#define gen_op_POWER_lscbx_le_user      gen_op_POWER_lscbx_user
4026
#define gen_op_POWER_lscbx_le_kernel    gen_op_POWER_lscbx_kernel
4027
#define gen_op_POWER_lscbx_le_hypv      gen_op_POWER_lscbx_hypv
4028
#define gen_op_POWER_lscbx_le_64_raw    gen_op_POWER_lscbx_raw
4029
#define gen_op_POWER_lscbx_le_64_user   gen_op_POWER_lscbx_user
4030
#define gen_op_POWER_lscbx_le_64_kernel gen_op_POWER_lscbx_kernel
4031
#define gen_op_POWER_lscbx_le_64_hypv   gen_op_POWER_lscbx_hypv
4032
static GenOpFunc3 *gen_op_POWER_lscbx[NB_MEM_FUNCS] = {
4033
    GEN_MEM_FUNCS(POWER_lscbx),
4034
};
4035

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

    
4042
    gen_addr_reg_index(ctx);
4043
    if (ra == 0) {
4044
        ra = rb;
4045
    }
4046
    /* NIP cannot be restored if the memory exception comes from an helper */
4047
    gen_update_nip(ctx, ctx->nip - 4);
4048
    gen_op_load_xer_bc();
4049
    gen_op_load_xer_cmp();
4050
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
4051
    gen_op_store_xer_bc();
4052
    if (unlikely(Rc(ctx->opcode) != 0))
4053
        gen_set_Rc0(ctx);
4054
}
4055

    
4056
/* maskg - maskg. */
4057
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4058
{
4059
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4060
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4061
    gen_op_POWER_maskg();
4062
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4063
    if (unlikely(Rc(ctx->opcode) != 0))
4064
        gen_set_Rc0(ctx);
4065
}
4066

    
4067
/* maskir - maskir. */
4068
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4069
{
4070
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4071
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4072
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4073
    gen_op_POWER_maskir();
4074
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4075
    if (unlikely(Rc(ctx->opcode) != 0))
4076
        gen_set_Rc0(ctx);
4077
}
4078

    
4079
/* mul - mul. */
4080
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4081
{
4082
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4083
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4084
    gen_op_POWER_mul();
4085
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4086
    if (unlikely(Rc(ctx->opcode) != 0))
4087
        gen_set_Rc0(ctx);
4088
}
4089

    
4090
/* mulo - mulo. */
4091
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4092
{
4093
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4094
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4095
    gen_op_POWER_mulo();
4096
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4097
    if (unlikely(Rc(ctx->opcode) != 0))
4098
        gen_set_Rc0(ctx);
4099
}
4100

    
4101
/* nabs - nabs. */
4102
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4103
{
4104
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4105
    gen_op_POWER_nabs();
4106
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4107
    if (unlikely(Rc(ctx->opcode) != 0))
4108
        gen_set_Rc0(ctx);
4109
}
4110

    
4111
/* nabso - nabso. */
4112
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4113
{
4114
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4115
    gen_op_POWER_nabso();
4116
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4117
    if (unlikely(Rc(ctx->opcode) != 0))
4118
        gen_set_Rc0(ctx);
4119
}
4120

    
4121
/* rlmi - rlmi. */
4122
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4123
{
4124
    uint32_t mb, me;
4125

    
4126
    mb = MB(ctx->opcode);
4127
    me = ME(ctx->opcode);
4128
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4129
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4130
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4131
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
4132
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4133
    if (unlikely(Rc(ctx->opcode) != 0))
4134
        gen_set_Rc0(ctx);
4135
}
4136

    
4137
/* rrib - rrib. */
4138
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4139
{
4140
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4141
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4142
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4143
    gen_op_POWER_rrib();
4144
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4145
    if (unlikely(Rc(ctx->opcode) != 0))
4146
        gen_set_Rc0(ctx);
4147
}
4148

    
4149
/* sle - sle. */
4150
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4151
{
4152
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4153
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4154
    gen_op_POWER_sle();
4155
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4156
    if (unlikely(Rc(ctx->opcode) != 0))
4157
        gen_set_Rc0(ctx);
4158
}
4159

    
4160
/* sleq - sleq. */
4161
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4162
{
4163
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4164
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4165
    gen_op_POWER_sleq();
4166
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4167
    if (unlikely(Rc(ctx->opcode) != 0))
4168
        gen_set_Rc0(ctx);
4169
}
4170

    
4171
/* sliq - sliq. */
4172
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4173
{
4174
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4175
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4176
    gen_op_POWER_sle();
4177
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4178
    if (unlikely(Rc(ctx->opcode) != 0))
4179
        gen_set_Rc0(ctx);
4180
}
4181

    
4182
/* slliq - slliq. */
4183
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4184
{
4185
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4186
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4187
    gen_op_POWER_sleq();
4188
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4189
    if (unlikely(Rc(ctx->opcode) != 0))
4190
        gen_set_Rc0(ctx);
4191
}
4192

    
4193
/* sllq - sllq. */
4194
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4195
{
4196
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4197
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4198
    gen_op_POWER_sllq();
4199
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4200
    if (unlikely(Rc(ctx->opcode) != 0))
4201
        gen_set_Rc0(ctx);
4202
}
4203

    
4204
/* slq - slq. */
4205
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4206
{
4207
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4208
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4209
    gen_op_POWER_slq();
4210
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4211
    if (unlikely(Rc(ctx->opcode) != 0))
4212
        gen_set_Rc0(ctx);
4213
}
4214

    
4215
/* sraiq - sraiq. */
4216
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4217
{
4218
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4219
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4220
    gen_op_POWER_sraq();
4221
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4222
    if (unlikely(Rc(ctx->opcode) != 0))
4223
        gen_set_Rc0(ctx);
4224
}
4225

    
4226
/* sraq - sraq. */
4227
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4228
{
4229
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4230
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4231
    gen_op_POWER_sraq();
4232
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4233
    if (unlikely(Rc(ctx->opcode) != 0))
4234
        gen_set_Rc0(ctx);
4235
}
4236

    
4237
/* sre - sre. */
4238
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4239
{
4240
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4241
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4242
    gen_op_POWER_sre();
4243
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4244
    if (unlikely(Rc(ctx->opcode) != 0))
4245
        gen_set_Rc0(ctx);
4246
}
4247

    
4248
/* srea - srea. */
4249
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4250
{
4251
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4252
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4253
    gen_op_POWER_srea();
4254
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4255
    if (unlikely(Rc(ctx->opcode) != 0))
4256
        gen_set_Rc0(ctx);
4257
}
4258

    
4259
/* sreq */
4260
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4261
{
4262
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4263
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4264
    gen_op_POWER_sreq();
4265
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4266
    if (unlikely(Rc(ctx->opcode) != 0))
4267
        gen_set_Rc0(ctx);
4268
}
4269

    
4270
/* sriq */
4271
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4272
{
4273
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4274
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4275
    gen_op_POWER_srq();
4276
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4277
    if (unlikely(Rc(ctx->opcode) != 0))
4278
        gen_set_Rc0(ctx);
4279
}
4280

    
4281
/* srliq */
4282
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4283
{
4284
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4285
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4286
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4287
    gen_op_POWER_srlq();
4288
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4289
    if (unlikely(Rc(ctx->opcode) != 0))
4290
        gen_set_Rc0(ctx);
4291
}
4292

    
4293
/* srlq */
4294
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
4295
{
4296
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4297
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4298
    gen_op_POWER_srlq();
4299
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4300
    if (unlikely(Rc(ctx->opcode) != 0))
4301
        gen_set_Rc0(ctx);
4302
}
4303

    
4304
/* srq */
4305
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
4306
{
4307
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4308
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4309
    gen_op_POWER_srq();
4310
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4311
    if (unlikely(Rc(ctx->opcode) != 0))
4312
        gen_set_Rc0(ctx);
4313
}
4314

    
4315
/* PowerPC 602 specific instructions */
4316
/* dsa  */
4317
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
4318
{
4319
    /* XXX: TODO */
4320
    GEN_EXCP_INVAL(ctx);
4321
}
4322

    
4323
/* esa */
4324
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
4325
{
4326
    /* XXX: TODO */
4327
    GEN_EXCP_INVAL(ctx);
4328
}
4329

    
4330
/* mfrom */
4331
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
4332
{
4333
#if defined(CONFIG_USER_ONLY)
4334
    GEN_EXCP_PRIVOPC(ctx);
4335
#else
4336
    if (unlikely(!ctx->supervisor)) {
4337
        GEN_EXCP_PRIVOPC(ctx);
4338
        return;
4339
    }
4340
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4341
    gen_op_602_mfrom();
4342
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4343
#endif
4344
}
4345

    
4346
/* 602 - 603 - G2 TLB management */
4347
/* tlbld */
4348
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4349
{
4350
#if defined(CONFIG_USER_ONLY)
4351
    GEN_EXCP_PRIVOPC(ctx);
4352
#else
4353
    if (unlikely(!ctx->supervisor)) {
4354
        GEN_EXCP_PRIVOPC(ctx);
4355
        return;
4356
    }
4357
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4358
    gen_op_6xx_tlbld();
4359
#endif
4360
}
4361

    
4362
/* tlbli */
4363
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4364
{
4365
#if defined(CONFIG_USER_ONLY)
4366
    GEN_EXCP_PRIVOPC(ctx);
4367
#else
4368
    if (unlikely(!ctx->supervisor)) {
4369
        GEN_EXCP_PRIVOPC(ctx);
4370
        return;
4371
    }
4372
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4373
    gen_op_6xx_tlbli();
4374
#endif
4375
}
4376

    
4377
/* 74xx TLB management */
4378
/* tlbld */
4379
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4380
{
4381
#if defined(CONFIG_USER_ONLY)
4382
    GEN_EXCP_PRIVOPC(ctx);
4383
#else
4384
    if (unlikely(!ctx->supervisor)) {
4385
        GEN_EXCP_PRIVOPC(ctx);
4386
        return;
4387
    }
4388
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4389
    gen_op_74xx_tlbld();
4390
#endif
4391
}
4392

    
4393
/* tlbli */
4394
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
4395
{
4396
#if defined(CONFIG_USER_ONLY)
4397
    GEN_EXCP_PRIVOPC(ctx);
4398
#else
4399
    if (unlikely(!ctx->supervisor)) {
4400
        GEN_EXCP_PRIVOPC(ctx);
4401
        return;
4402
    }
4403
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4404
    gen_op_74xx_tlbli();
4405
#endif
4406
}
4407

    
4408
/* POWER instructions not in PowerPC 601 */
4409
/* clf */
4410
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
4411
{
4412
    /* Cache line flush: implemented as no-op */
4413
}
4414

    
4415
/* cli */
4416
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
4417
{
4418
    /* Cache line invalidate: privileged and treated as no-op */
4419
#if defined(CONFIG_USER_ONLY)
4420
    GEN_EXCP_PRIVOPC(ctx);
4421
#else
4422
    if (unlikely(!ctx->supervisor)) {
4423
        GEN_EXCP_PRIVOPC(ctx);
4424
        return;
4425
    }
4426
#endif
4427
}
4428

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

    
4435
GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
4436
{
4437
#if defined(CONFIG_USER_ONLY)
4438
    GEN_EXCP_PRIVOPC(ctx);
4439
#else
4440
    if (unlikely(!ctx->supervisor)) {
4441
        GEN_EXCP_PRIVOPC(ctx);
4442
        return;
4443
    }
4444
    int ra = rA(ctx->opcode);
4445
    int rd = rD(ctx->opcode);
4446

    
4447
    gen_addr_reg_index(ctx);
4448
    gen_op_POWER_mfsri();
4449
    tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[0]);
4450
    if (ra != 0 && ra != rd)
4451
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[1]);
4452
#endif
4453
}
4454

    
4455
GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
4456
{
4457
#if defined(CONFIG_USER_ONLY)
4458
    GEN_EXCP_PRIVOPC(ctx);
4459
#else
4460
    if (unlikely(!ctx->supervisor)) {
4461
        GEN_EXCP_PRIVOPC(ctx);
4462
        return;
4463
    }
4464
    gen_addr_reg_index(ctx);
4465
    gen_op_POWER_rac();
4466
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4467
#endif
4468
}
4469

    
4470
GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
4471
{
4472
#if defined(CONFIG_USER_ONLY)
4473
    GEN_EXCP_PRIVOPC(ctx);
4474
#else
4475
    if (unlikely(!ctx->supervisor)) {
4476
        GEN_EXCP_PRIVOPC(ctx);
4477
        return;
4478
    }
4479
    gen_op_POWER_rfsvc();
4480
    GEN_SYNC(ctx);
4481
#endif
4482
}
4483

    
4484
/* svc is not implemented for now */
4485

    
4486
/* POWER2 specific instructions */
4487
/* Quad manipulation (load/store two floats at a time) */
4488
/* Original POWER2 is 32 bits only, define 64 bits ops as 32 bits ones */
4489
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
4490
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
4491
#define gen_op_POWER2_lfq_64_raw        gen_op_POWER2_lfq_raw
4492
#define gen_op_POWER2_lfq_64_user       gen_op_POWER2_lfq_user
4493
#define gen_op_POWER2_lfq_64_kernel     gen_op_POWER2_lfq_kernel
4494
#define gen_op_POWER2_lfq_64_hypv       gen_op_POWER2_lfq_hypv
4495
#define gen_op_POWER2_lfq_le_64_raw     gen_op_POWER2_lfq_le_raw
4496
#define gen_op_POWER2_lfq_le_64_user    gen_op_POWER2_lfq_le_user
4497
#define gen_op_POWER2_lfq_le_64_kernel  gen_op_POWER2_lfq_le_kernel
4498
#define gen_op_POWER2_lfq_le_64_hypv    gen_op_POWER2_lfq_le_hypv
4499
#define gen_op_POWER2_stfq_64_raw       gen_op_POWER2_stfq_raw
4500
#define gen_op_POWER2_stfq_64_user      gen_op_POWER2_stfq_user
4501
#define gen_op_POWER2_stfq_64_kernel    gen_op_POWER2_stfq_kernel
4502
#define gen_op_POWER2_stfq_64_hypv      gen_op_POWER2_stfq_hypv
4503
#define gen_op_POWER2_stfq_le_64_raw    gen_op_POWER2_stfq_le_raw
4504
#define gen_op_POWER2_stfq_le_64_user   gen_op_POWER2_stfq_le_user
4505
#define gen_op_POWER2_stfq_le_64_kernel gen_op_POWER2_stfq_le_kernel
4506
#define gen_op_POWER2_stfq_le_64_hypv   gen_op_POWER2_stfq_le_hypv
4507
static GenOpFunc *gen_op_POWER2_lfq[NB_MEM_FUNCS] = {
4508
    GEN_MEM_FUNCS(POWER2_lfq),
4509
};
4510
static GenOpFunc *gen_op_POWER2_stfq[NB_MEM_FUNCS] = {
4511
    GEN_MEM_FUNCS(POWER2_stfq),
4512
};
4513

    
4514
/* lfq */
4515
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4516
{
4517
    /* NIP cannot be restored if the memory exception comes from an helper */
4518
    gen_update_nip(ctx, ctx->nip - 4);
4519
    gen_addr_imm_index(ctx, 0);
4520
    op_POWER2_lfq();
4521
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
4522
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4523
}
4524

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

    
4530
    /* NIP cannot be restored if the memory exception comes from an helper */
4531
    gen_update_nip(ctx, ctx->nip - 4);
4532
    gen_addr_imm_index(ctx, 0);
4533
    op_POWER2_lfq();
4534
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
4535
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4536
    if (ra != 0)
4537
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4538
}
4539

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

    
4545
    /* NIP cannot be restored if the memory exception comes from an helper */
4546
    gen_update_nip(ctx, ctx->nip - 4);
4547
    gen_addr_reg_index(ctx);
4548
    op_POWER2_lfq();
4549
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
4550
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4551
    if (ra != 0)
4552
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4553
}
4554

    
4555
/* lfqx */
4556
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
4557
{
4558
    /* NIP cannot be restored if the memory exception comes from an helper */
4559
    gen_update_nip(ctx, ctx->nip - 4);
4560
    gen_addr_reg_index(ctx);
4561
    op_POWER2_lfq();
4562
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
4563
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
4564
}
4565

    
4566
/* stfq */
4567
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4568
{
4569
    /* NIP cannot be restored if the memory exception comes from an helper */
4570
    gen_update_nip(ctx, ctx->nip - 4);
4571
    gen_addr_imm_index(ctx, 0);
4572
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
4573
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
4574
    op_POWER2_stfq();
4575
}
4576

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

    
4582
    /* NIP cannot be restored if the memory exception comes from an helper */
4583
    gen_update_nip(ctx, ctx->nip - 4);
4584
    gen_addr_imm_index(ctx, 0);
4585
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
4586
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
4587
    op_POWER2_stfq();
4588
    if (ra != 0)
4589
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4590
}
4591

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

    
4597
    /* NIP cannot be restored if the memory exception comes from an helper */
4598
    gen_update_nip(ctx, ctx->nip - 4);
4599
    gen_addr_reg_index(ctx);
4600
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
4601
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
4602
    op_POWER2_stfq();
4603
    if (ra != 0)
4604
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
4605
}
4606

    
4607
/* stfqx */
4608
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
4609
{
4610
    /* NIP cannot be restored if the memory exception comes from an helper */
4611
    gen_update_nip(ctx, ctx->nip - 4);
4612
    gen_addr_reg_index(ctx);
4613
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
4614
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
4615
    op_POWER2_stfq();
4616
}
4617

    
4618
/* BookE specific instructions */
4619
/* XXX: not implemented on 440 ? */
4620
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
4621
{
4622
    /* XXX: TODO */
4623
    GEN_EXCP_INVAL(ctx);
4624
}
4625

    
4626
/* XXX: not implemented on 440 ? */
4627
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
4628
{
4629
#if defined(CONFIG_USER_ONLY)
4630
    GEN_EXCP_PRIVOPC(ctx);
4631
#else
4632
    if (unlikely(!ctx->supervisor)) {
4633
        GEN_EXCP_PRIVOPC(ctx);
4634
        return;
4635
    }
4636
    gen_addr_reg_index(ctx);
4637
    /* Use the same micro-ops as for tlbie */
4638
#if defined(TARGET_PPC64)
4639
    if (ctx->sf_mode)
4640
        gen_op_tlbie_64();
4641
    else
4642
#endif
4643
        gen_op_tlbie();
4644
#endif
4645
}
4646

    
4647
/* All 405 MAC instructions are translated here */
4648
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
4649
                                                int opc2, int opc3,
4650
                                                int ra, int rb, int rt, int Rc)
4651
{
4652
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[ra]);
4653
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rb]);
4654
    switch (opc3 & 0x0D) {
4655
    case 0x05:
4656
        /* macchw    - macchw.    - macchwo   - macchwo.   */
4657
        /* macchws   - macchws.   - macchwso  - macchwso.  */
4658
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
4659
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
4660
        /* mulchw - mulchw. */
4661
        gen_op_405_mulchw();
4662
        break;
4663
    case 0x04:
4664
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
4665
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
4666
        /* mulchwu - mulchwu. */
4667
        gen_op_405_mulchwu();
4668
        break;
4669
    case 0x01:
4670
        /* machhw    - machhw.    - machhwo   - machhwo.   */
4671
        /* machhws   - machhws.   - machhwso  - machhwso.  */
4672
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
4673
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
4674
        /* mulhhw - mulhhw. */
4675
        gen_op_405_mulhhw();
4676
        break;
4677
    case 0x00:
4678
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
4679
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
4680
        /* mulhhwu - mulhhwu. */
4681
        gen_op_405_mulhhwu();
4682
        break;
4683
    case 0x0D:
4684
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
4685
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
4686
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
4687
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
4688
        /* mullhw - mullhw. */
4689
        gen_op_405_mullhw();
4690
        break;
4691
    case 0x0C:
4692
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
4693
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
4694
        /* mullhwu - mullhwu. */
4695
        gen_op_405_mullhwu();
4696
        break;
4697
    }
4698
    if (opc2 & 0x02) {
4699
        /* nmultiply-and-accumulate (0x0E) */
4700
        gen_op_neg();
4701
    }
4702
    if (opc2 & 0x04) {
4703
        /* (n)multiply-and-accumulate (0x0C - 0x0E) */
4704
        tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rt]);
4705
        tcg_gen_mov_tl(cpu_T[1], cpu_T[0]);
4706
        gen_op_405_add_T0_T2();
4707
    }
4708
    if (opc3 & 0x10) {
4709
        /* Check overflow */
4710
        if (opc3 & 0x01)
4711
            gen_op_check_addo();
4712
        else
4713
            gen_op_405_check_ovu();
4714
    }
4715
    if (opc3 & 0x02) {
4716
        /* Saturate */
4717
        if (opc3 & 0x01)
4718
            gen_op_405_check_sat();
4719
        else
4720
            gen_op_405_check_satu();
4721
    }
4722
    tcg_gen_mov_tl(cpu_gpr[rt], cpu_T[0]);
4723
    if (unlikely(Rc) != 0) {
4724
        /* Update Rc0 */
4725
        gen_set_Rc0(ctx);
4726
    }
4727
}
4728

    
4729
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
4730
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
4731
{                                                                             \
4732
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
4733
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
4734
}
4735

    
4736
/* macchw    - macchw.    */
4737
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
4738
/* macchwo   - macchwo.   */
4739
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
4740
/* macchws   - macchws.   */
4741
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
4742
/* macchwso  - macchwso.  */
4743
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
4744
/* macchwsu  - macchwsu.  */
4745
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
4746
/* macchwsuo - macchwsuo. */
4747
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
4748
/* macchwu   - macchwu.   */
4749
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
4750
/* macchwuo  - macchwuo.  */
4751
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
4752
/* machhw    - machhw.    */
4753
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
4754
/* machhwo   - machhwo.   */
4755
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
4756
/* machhws   - machhws.   */
4757
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
4758
/* machhwso  - machhwso.  */
4759
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
4760
/* machhwsu  - machhwsu.  */
4761
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
4762
/* machhwsuo - machhwsuo. */
4763
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
4764
/* machhwu   - machhwu.   */
4765
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
4766
/* machhwuo  - machhwuo.  */
4767
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
4768
/* maclhw    - maclhw.    */
4769
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
4770
/* maclhwo   - maclhwo.   */
4771
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
4772
/* maclhws   - maclhws.   */
4773
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
4774
/* maclhwso  - maclhwso.  */
4775
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
4776
/* maclhwu   - maclhwu.   */
4777
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
4778
/* maclhwuo  - maclhwuo.  */
4779
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
4780
/* maclhwsu  - maclhwsu.  */
4781
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
4782
/* maclhwsuo - maclhwsuo. */
4783
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
4784
/* nmacchw   - nmacchw.   */
4785
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
4786
/* nmacchwo  - nmacchwo.  */
4787
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
4788
/* nmacchws  - nmacchws.  */
4789
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
4790
/* nmacchwso - nmacchwso. */
4791
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
4792
/* nmachhw   - nmachhw.   */
4793
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
4794
/* nmachhwo  - nmachhwo.  */
4795
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
4796
/* nmachhws  - nmachhws.  */
4797
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
4798
/* nmachhwso - nmachhwso. */
4799
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
4800
/* nmaclhw   - nmaclhw.   */
4801
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
4802
/* nmaclhwo  - nmaclhwo.  */
4803
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
4804
/* nmaclhws  - nmaclhws.  */
4805
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
4806
/* nmaclhwso - nmaclhwso. */
4807
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
4808

    
4809
/* mulchw  - mulchw.  */
4810
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
4811
/* mulchwu - mulchwu. */
4812
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
4813
/* mulhhw  - mulhhw.  */
4814
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
4815
/* mulhhwu - mulhhwu. */
4816
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
4817
/* mullhw  - mullhw.  */
4818
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
4819
/* mullhwu - mullhwu. */
4820
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
4821

    
4822
/* mfdcr */
4823
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
4824
{
4825
#if defined(CONFIG_USER_ONLY)
4826
    GEN_EXCP_PRIVREG(ctx);
4827
#else
4828
    uint32_t dcrn = SPR(ctx->opcode);
4829

    
4830
    if (unlikely(!ctx->supervisor)) {
4831
        GEN_EXCP_PRIVREG(ctx);
4832
        return;
4833
    }
4834
    tcg_gen_movi_tl(cpu_T[0], dcrn);
4835
    gen_op_load_dcr();
4836
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4837
#endif
4838
}
4839

    
4840
/* mtdcr */
4841
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
4842
{
4843
#if defined(CONFIG_USER_ONLY)
4844
    GEN_EXCP_PRIVREG(ctx);
4845
#else
4846
    uint32_t dcrn = SPR(ctx->opcode);
4847

    
4848
    if (unlikely(!ctx->supervisor)) {
4849
        GEN_EXCP_PRIVREG(ctx);
4850
        return;
4851
    }
4852
    tcg_gen_movi_tl(cpu_T[0], dcrn);
4853
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4854
    gen_op_store_dcr();
4855
#endif
4856
}
4857

    
4858
/* mfdcrx */
4859
/* XXX: not implemented on 440 ? */
4860
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
4861
{
4862
#if defined(CONFIG_USER_ONLY)
4863
    GEN_EXCP_PRIVREG(ctx);
4864
#else
4865
    if (unlikely(!ctx->supervisor)) {
4866
        GEN_EXCP_PRIVREG(ctx);
4867
        return;
4868
    }
4869
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4870
    gen_op_load_dcr();
4871
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4872
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4873
#endif
4874
}
4875

    
4876
/* mtdcrx */
4877
/* XXX: not implemented on 440 ? */
4878
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
4879
{
4880
#if defined(CONFIG_USER_ONLY)
4881
    GEN_EXCP_PRIVREG(ctx);
4882
#else
4883
    if (unlikely(!ctx->supervisor)) {
4884
        GEN_EXCP_PRIVREG(ctx);
4885
        return;
4886
    }
4887
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4888
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4889
    gen_op_store_dcr();
4890
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4891
#endif
4892
}
4893

    
4894
/* mfdcrux (PPC 460) : user-mode access to DCR */
4895
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
4896
{
4897
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4898
    gen_op_load_dcr();
4899
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4900
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4901
}
4902

    
4903
/* mtdcrux (PPC 460) : user-mode access to DCR */
4904
GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
4905
{
4906
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4907
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4908
    gen_op_store_dcr();
4909
    /* Note: Rc update flag set leads to undefined state of Rc0 */
4910
}
4911

    
4912
/* dccci */
4913
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
4914
{
4915
#if defined(CONFIG_USER_ONLY)
4916
    GEN_EXCP_PRIVOPC(ctx);
4917
#else
4918
    if (unlikely(!ctx->supervisor)) {
4919
        GEN_EXCP_PRIVOPC(ctx);
4920
        return;
4921
    }
4922
    /* interpreted as no-op */
4923
#endif
4924
}
4925

    
4926
/* dcread */
4927
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
4928
{
4929
#if defined(CONFIG_USER_ONLY)
4930
    GEN_EXCP_PRIVOPC(ctx);
4931
#else
4932
    if (unlikely(!ctx->supervisor)) {
4933
        GEN_EXCP_PRIVOPC(ctx);
4934
        return;
4935
    }
4936
    gen_addr_reg_index(ctx);
4937
    op_ldst(lwz);
4938
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4939
#endif
4940
}
4941

    
4942
/* icbt */
4943
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
4944
{
4945
    /* interpreted as no-op */
4946
    /* XXX: specification say this is treated as a load by the MMU
4947
     *      but does not generate any exception
4948
     */
4949
}
4950

    
4951
/* iccci */
4952
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
4953
{
4954
#if defined(CONFIG_USER_ONLY)
4955
    GEN_EXCP_PRIVOPC(ctx);
4956
#else
4957
    if (unlikely(!ctx->supervisor)) {
4958
        GEN_EXCP_PRIVOPC(ctx);
4959
        return;
4960
    }
4961
    /* interpreted as no-op */
4962
#endif
4963
}
4964

    
4965
/* icread */
4966
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
4967
{
4968
#if defined(CONFIG_USER_ONLY)
4969
    GEN_EXCP_PRIVOPC(ctx);
4970
#else
4971
    if (unlikely(!ctx->supervisor)) {
4972
        GEN_EXCP_PRIVOPC(ctx);
4973
        return;
4974
    }
4975
    /* interpreted as no-op */
4976
#endif
4977
}
4978

    
4979
/* rfci (supervisor only) */
4980
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
4981
{
4982
#if defined(CONFIG_USER_ONLY)
4983
    GEN_EXCP_PRIVOPC(ctx);
4984
#else
4985
    if (unlikely(!ctx->supervisor)) {
4986
        GEN_EXCP_PRIVOPC(ctx);
4987
        return;
4988
    }
4989
    /* Restore CPU state */
4990
    gen_op_40x_rfci();
4991
    GEN_SYNC(ctx);
4992
#endif
4993
}
4994

    
4995
GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
4996
{
4997
#if defined(CONFIG_USER_ONLY)
4998
    GEN_EXCP_PRIVOPC(ctx);
4999
#else
5000
    if (unlikely(!ctx->supervisor)) {
5001
        GEN_EXCP_PRIVOPC(ctx);
5002
        return;
5003
    }
5004
    /* Restore CPU state */
5005
    gen_op_rfci();
5006
    GEN_SYNC(ctx);
5007
#endif
5008
}
5009

    
5010
/* BookE specific */
5011
/* XXX: not implemented on 440 ? */
5012
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5013
{
5014
#if defined(CONFIG_USER_ONLY)
5015
    GEN_EXCP_PRIVOPC(ctx);
5016
#else
5017
    if (unlikely(!ctx->supervisor)) {
5018
        GEN_EXCP_PRIVOPC(ctx);
5019
        return;
5020
    }
5021
    /* Restore CPU state */
5022
    gen_op_rfdi();
5023
    GEN_SYNC(ctx);
5024
#endif
5025
}
5026

    
5027
/* XXX: not implemented on 440 ? */
5028
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5029
{
5030
#if defined(CONFIG_USER_ONLY)
5031
    GEN_EXCP_PRIVOPC(ctx);
5032
#else
5033
    if (unlikely(!ctx->supervisor)) {
5034
        GEN_EXCP_PRIVOPC(ctx);
5035
        return;
5036
    }
5037
    /* Restore CPU state */
5038
    gen_op_rfmci();
5039
    GEN_SYNC(ctx);
5040
#endif
5041
}
5042

    
5043
/* TLB management - PowerPC 405 implementation */
5044
/* tlbre */
5045
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5046
{
5047
#if defined(CONFIG_USER_ONLY)
5048
    GEN_EXCP_PRIVOPC(ctx);
5049
#else
5050
    if (unlikely(!ctx->supervisor)) {
5051
        GEN_EXCP_PRIVOPC(ctx);
5052
        return;
5053
    }
5054
    switch (rB(ctx->opcode)) {
5055
    case 0:
5056
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5057
        gen_op_4xx_tlbre_hi();
5058
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5059
        break;
5060
    case 1:
5061
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5062
        gen_op_4xx_tlbre_lo();
5063
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5064
        break;
5065
    default:
5066
        GEN_EXCP_INVAL(ctx);
5067
        break;
5068
    }
5069
#endif
5070
}
5071

    
5072
/* tlbsx - tlbsx. */
5073
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5074
{
5075
#if defined(CONFIG_USER_ONLY)
5076
    GEN_EXCP_PRIVOPC(ctx);
5077
#else
5078
    if (unlikely(!ctx->supervisor)) {
5079
        GEN_EXCP_PRIVOPC(ctx);
5080
        return;
5081
    }
5082
    gen_addr_reg_index(ctx);
5083
    gen_op_4xx_tlbsx();
5084
    if (Rc(ctx->opcode))
5085
        gen_op_4xx_tlbsx_check();
5086
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5087
#endif
5088
}
5089

    
5090
/* tlbwe */
5091
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5092
{
5093
#if defined(CONFIG_USER_ONLY)
5094
    GEN_EXCP_PRIVOPC(ctx);
5095
#else
5096
    if (unlikely(!ctx->supervisor)) {
5097
        GEN_EXCP_PRIVOPC(ctx);
5098
        return;
5099
    }
5100
    switch (rB(ctx->opcode)) {
5101
    case 0:
5102
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5103
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5104
        gen_op_4xx_tlbwe_hi();
5105
        break;
5106
    case 1:
5107
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5108
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5109
        gen_op_4xx_tlbwe_lo();
5110
        break;
5111
    default:
5112
        GEN_EXCP_INVAL(ctx);
5113
        break;
5114
    }
5115
#endif
5116
}
5117

    
5118
/* TLB management - PowerPC 440 implementation */
5119
/* tlbre */
5120
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5121
{
5122
#if defined(CONFIG_USER_ONLY)
5123
    GEN_EXCP_PRIVOPC(ctx);
5124
#else
5125
    if (unlikely(!ctx->supervisor)) {
5126
        GEN_EXCP_PRIVOPC(ctx);
5127
        return;
5128
    }
5129
    switch (rB(ctx->opcode)) {
5130
    case 0:
5131
    case 1:
5132
    case 2:
5133
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5134
        gen_op_440_tlbre(rB(ctx->opcode));
5135
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5136
        break;
5137
    default:
5138
        GEN_EXCP_INVAL(ctx);
5139
        break;
5140
    }
5141
#endif
5142
}
5143

    
5144
/* tlbsx - tlbsx. */
5145
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5146
{
5147
#if defined(CONFIG_USER_ONLY)
5148
    GEN_EXCP_PRIVOPC(ctx);
5149
#else
5150
    if (unlikely(!ctx->supervisor)) {
5151
        GEN_EXCP_PRIVOPC(ctx);
5152
        return;
5153
    }
5154
    gen_addr_reg_index(ctx);
5155
    gen_op_440_tlbsx();
5156
    if (Rc(ctx->opcode))
5157
        gen_op_4xx_tlbsx_check();
5158
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5159
#endif
5160
}
5161

    
5162
/* tlbwe */
5163
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5164
{
5165
#if defined(CONFIG_USER_ONLY)
5166
    GEN_EXCP_PRIVOPC(ctx);
5167
#else
5168
    if (unlikely(!ctx->supervisor)) {
5169
        GEN_EXCP_PRIVOPC(ctx);
5170
        return;
5171
    }
5172
    switch (rB(ctx->opcode)) {
5173
    case 0:
5174
    case 1:
5175
    case 2:
5176
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5177
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5178
        gen_op_440_tlbwe(rB(ctx->opcode));
5179
        break;
5180
    default:
5181
        GEN_EXCP_INVAL(ctx);
5182
        break;
5183
    }
5184
#endif
5185
}
5186

    
5187
/* wrtee */
5188
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
5189
{
5190
#if defined(CONFIG_USER_ONLY)
5191
    GEN_EXCP_PRIVOPC(ctx);
5192
#else
5193
    if (unlikely(!ctx->supervisor)) {
5194
        GEN_EXCP_PRIVOPC(ctx);
5195
        return;
5196
    }
5197
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rD(ctx->opcode)]);
5198
    gen_op_wrte();
5199
    /* Stop translation to have a chance to raise an exception
5200
     * if we just set msr_ee to 1
5201
     */
5202
    GEN_STOP(ctx);
5203
#endif
5204
}
5205

    
5206
/* wrteei */
5207
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
5208
{
5209
#if defined(CONFIG_USER_ONLY)
5210
    GEN_EXCP_PRIVOPC(ctx);
5211
#else
5212
    if (unlikely(!ctx->supervisor)) {
5213
        GEN_EXCP_PRIVOPC(ctx);
5214
        return;
5215
    }
5216
    tcg_gen_movi_tl(cpu_T[0], ctx->opcode & 0x00010000);
5217
    gen_op_wrte();
5218
    /* Stop translation to have a chance to raise an exception
5219
     * if we just set msr_ee to 1
5220
     */
5221
    GEN_STOP(ctx);
5222
#endif
5223
}
5224

    
5225
/* PowerPC 440 specific instructions */
5226
/* dlmzb */
5227
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
5228
{
5229
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
5230
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5231
    gen_op_440_dlmzb();
5232
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
5233
    gen_op_store_xer_bc();
5234
    if (Rc(ctx->opcode)) {
5235
        gen_op_440_dlmzb_update_Rc();
5236
        tcg_gen_andi_i32(cpu_crf[0], cpu_T[0], 0xf);
5237
    }
5238
}
5239

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

    
5246
/* msync replaces sync on 440 */
5247
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5248
{
5249
    /* interpreted as no-op */
5250
}
5251

    
5252
/* icbt */
5253
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5254
{
5255
    /* interpreted as no-op */
5256
    /* XXX: specification say this is treated as a load by the MMU
5257
     *      but does not generate any exception
5258
     */
5259
}
5260

    
5261
/***                      Altivec vector extension                         ***/
5262
/* Altivec registers moves */
5263

    
5264
static always_inline void gen_load_avr(int t, int reg) {
5265
    tcg_gen_mov_i64(cpu_AVRh[t], cpu_avrh[reg]);
5266
    tcg_gen_mov_i64(cpu_AVRl[t], cpu_avrl[reg]);
5267
}
5268

    
5269
static always_inline void gen_store_avr(int reg, int t) {
5270
    tcg_gen_mov_i64(cpu_avrh[reg], cpu_AVRh[t]);
5271
    tcg_gen_mov_i64(cpu_avrl[reg], cpu_AVRl[t]);
5272
}
5273

    
5274
#define op_vr_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
5275
#define OP_VR_LD_TABLE(name)                                                  \
5276
static GenOpFunc *gen_op_vr_l##name[NB_MEM_FUNCS] = {                         \
5277
    GEN_MEM_FUNCS(vr_l##name),                                                \
5278
};
5279
#define OP_VR_ST_TABLE(name)                                                  \
5280
static GenOpFunc *gen_op_vr_st##name[NB_MEM_FUNCS] = {                        \
5281
    GEN_MEM_FUNCS(vr_st##name),                                               \
5282
};
5283

    
5284
#define GEN_VR_LDX(name, opc2, opc3)                                          \
5285
GEN_HANDLER(l##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)               \
5286
{                                                                             \
5287
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5288
        GEN_EXCP_NO_VR(ctx);                                                  \
5289
        return;                                                               \
5290
    }                                                                         \
5291
    gen_addr_reg_index(ctx);                                                  \
5292
    op_vr_ldst(vr_l##name);                                                   \
5293
    gen_store_avr(rD(ctx->opcode), 0);                                        \
5294
}
5295

    
5296
#define GEN_VR_STX(name, opc2, opc3)                                          \
5297
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
5298
{                                                                             \
5299
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5300
        GEN_EXCP_NO_VR(ctx);                                                  \
5301
        return;                                                               \
5302
    }                                                                         \
5303
    gen_addr_reg_index(ctx);                                                  \
5304
    gen_load_avr(0, rS(ctx->opcode));                                         \
5305
    op_vr_ldst(vr_st##name);                                                  \
5306
}
5307

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

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

    
5320
/***                           SPE extension                               ***/
5321
/* Register moves */
5322

    
5323
static always_inline void gen_load_gpr64(TCGv t, int reg) {
5324
#if defined(TARGET_PPC64)
5325
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
5326
#else
5327
    tcg_gen_extu_i32_i64(t, cpu_gprh[reg]);
5328
    tcg_gen_shli_i64(t, t, 32);
5329
    TCGv tmp = tcg_temp_local_new(TCG_TYPE_I64);
5330
    tcg_gen_extu_i32_i64(tmp, cpu_gpr[reg]);
5331
    tcg_gen_or_i64(t, t, tmp);
5332
    tcg_temp_free(tmp);
5333
#endif
5334
}
5335

    
5336
static always_inline void gen_store_gpr64(int reg, TCGv t) {
5337
#if defined(TARGET_PPC64)
5338
    tcg_gen_mov_i64(cpu_gpr[reg], t);
5339
#else
5340
    tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
5341
    TCGv tmp = tcg_temp_local_new(TCG_TYPE_I64);
5342
    tcg_gen_shri_i64(tmp, t, 32);
5343
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
5344
    tcg_temp_free(tmp);
5345
#endif
5346
}
5347

    
5348
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
5349
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
5350
{                                                                             \
5351
    if (Rc(ctx->opcode))                                                      \
5352
        gen_##name1(ctx);                                                     \
5353
    else                                                                      \
5354
        gen_##name0(ctx);                                                     \
5355
}
5356

    
5357
/* Handler for undefined SPE opcodes */
5358
static always_inline void gen_speundef (DisasContext *ctx)
5359
{
5360
    GEN_EXCP_INVAL(ctx);
5361
}
5362

    
5363
/* SPE load and stores */
5364
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, int sh)
5365
{
5366
    target_long simm = rB(ctx->opcode);
5367

    
5368
    if (rA(ctx->opcode) == 0) {
5369
        tcg_gen_movi_tl(cpu_T[0], simm << sh);
5370
    } else {
5371
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5372
        if (likely(simm != 0))
5373
            gen_op_addi(simm << sh);
5374
    }
5375
}
5376

    
5377
#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
5378
#define OP_SPE_LD_TABLE(name)                                                 \
5379
static GenOpFunc *gen_op_spe_l##name[NB_MEM_FUNCS] = {                        \
5380
    GEN_MEM_FUNCS(spe_l##name),                                               \
5381
};
5382
#define OP_SPE_ST_TABLE(name)                                                 \
5383
static GenOpFunc *gen_op_spe_st##name[NB_MEM_FUNCS] = {                       \
5384
    GEN_MEM_FUNCS(spe_st##name),                                              \
5385
};
5386

    
5387
#define GEN_SPE_LD(name, sh)                                                  \
5388
static always_inline void gen_evl##name (DisasContext *ctx)                   \
5389
{                                                                             \
5390
    if (unlikely(!ctx->spe_enabled)) {                                        \
5391
        GEN_EXCP_NO_AP(ctx);                                                  \
5392
        return;                                                               \
5393
    }                                                                         \
5394
    gen_addr_spe_imm_index(ctx, sh);                                          \
5395
    op_spe_ldst(spe_l##name);                                                 \
5396
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
5397
}
5398

    
5399
#define GEN_SPE_LDX(name)                                                     \
5400
static always_inline void gen_evl##name##x (DisasContext *ctx)                \
5401
{                                                                             \
5402
    if (unlikely(!ctx->spe_enabled)) {                                        \
5403
        GEN_EXCP_NO_AP(ctx);                                                  \
5404
        return;                                                               \
5405
    }                                                                         \
5406
    gen_addr_reg_index(ctx);                                                  \
5407
    op_spe_ldst(spe_l##name);                                                 \
5408
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
5409
}
5410

    
5411
#define GEN_SPEOP_LD(name, sh)                                                \
5412
OP_SPE_LD_TABLE(name);                                                        \
5413
GEN_SPE_LD(name, sh);                                                         \
5414
GEN_SPE_LDX(name)
5415

    
5416
#define GEN_SPE_ST(name, sh)                                                  \
5417
static always_inline void gen_evst##name (DisasContext *ctx)                  \
5418
{                                                                             \
5419
    if (unlikely(!ctx->spe_enabled)) {                                        \
5420
        GEN_EXCP_NO_AP(ctx);                                                  \
5421
        return;                                                               \
5422
    }                                                                         \
5423
    gen_addr_spe_imm_index(ctx, sh);                                          \
5424
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
5425
    op_spe_ldst(spe_st##name);                                                \
5426
}
5427

    
5428
#define GEN_SPE_STX(name)                                                     \
5429
static always_inline void gen_evst##name##x (DisasContext *ctx)               \
5430
{                                                                             \
5431
    if (unlikely(!ctx->spe_enabled)) {                                        \
5432
        GEN_EXCP_NO_AP(ctx);                                                  \
5433
        return;                                                               \
5434
    }                                                                         \
5435
    gen_addr_reg_index(ctx);                                                  \
5436
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
5437
    op_spe_ldst(spe_st##name);                                                \
5438
}
5439

    
5440
#define GEN_SPEOP_ST(name, sh)                                                \
5441
OP_SPE_ST_TABLE(name);                                                        \
5442
GEN_SPE_ST(name, sh);                                                         \
5443
GEN_SPE_STX(name)
5444

    
5445
#define GEN_SPEOP_LDST(name, sh)                                              \
5446
GEN_SPEOP_LD(name, sh);                                                       \
5447
GEN_SPEOP_ST(name, sh)
5448

    
5449
/* SPE arithmetic and logic */
5450
#define GEN_SPEOP_ARITH2(name)                                                \
5451
static always_inline void gen_##name (DisasContext *ctx)                      \
5452
{                                                                             \
5453
    if (unlikely(!ctx->spe_enabled)) {                                        \
5454
        GEN_EXCP_NO_AP(ctx);                                                  \
5455
        return;                                                               \
5456
    }                                                                         \
5457
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5458
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));                              \
5459
    gen_op_##name();                                                          \
5460
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5461
}
5462

    
5463
#define GEN_SPEOP_ARITH1(name)                                                \
5464
static always_inline void gen_##name (DisasContext *ctx)                      \
5465
{                                                                             \
5466
    if (unlikely(!ctx->spe_enabled)) {                                        \
5467
        GEN_EXCP_NO_AP(ctx);                                                  \
5468
        return;                                                               \
5469
    }                                                                         \
5470
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5471
    gen_op_##name();                                                          \
5472
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5473
}
5474

    
5475
#define GEN_SPEOP_COMP(name)                                                  \
5476
static always_inline void gen_##name (DisasContext *ctx)                      \
5477
{                                                                             \
5478
    if (unlikely(!ctx->spe_enabled)) {                                        \
5479
        GEN_EXCP_NO_AP(ctx);                                                  \
5480
        return;                                                               \
5481
    }                                                                         \
5482
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5483
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));                              \
5484
    gen_op_##name();                                                          \
5485
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_T[0], 0xf);              \
5486
}
5487

    
5488
/* Logical */
5489
GEN_SPEOP_ARITH2(evand);
5490
GEN_SPEOP_ARITH2(evandc);
5491
GEN_SPEOP_ARITH2(evxor);
5492
GEN_SPEOP_ARITH2(evor);
5493
GEN_SPEOP_ARITH2(evnor);
5494
GEN_SPEOP_ARITH2(eveqv);
5495
GEN_SPEOP_ARITH2(evorc);
5496
GEN_SPEOP_ARITH2(evnand);
5497
GEN_SPEOP_ARITH2(evsrwu);
5498
GEN_SPEOP_ARITH2(evsrws);
5499
GEN_SPEOP_ARITH2(evslw);
5500
GEN_SPEOP_ARITH2(evrlw);
5501
GEN_SPEOP_ARITH2(evmergehi);
5502
GEN_SPEOP_ARITH2(evmergelo);
5503
GEN_SPEOP_ARITH2(evmergehilo);
5504
GEN_SPEOP_ARITH2(evmergelohi);
5505

    
5506
/* Arithmetic */
5507
GEN_SPEOP_ARITH2(evaddw);
5508
GEN_SPEOP_ARITH2(evsubfw);
5509
GEN_SPEOP_ARITH1(evabs);
5510
GEN_SPEOP_ARITH1(evneg);
5511
GEN_SPEOP_ARITH1(evextsb);
5512
GEN_SPEOP_ARITH1(evextsh);
5513
GEN_SPEOP_ARITH1(evrndw);
5514
GEN_SPEOP_ARITH1(evcntlzw);
5515
GEN_SPEOP_ARITH1(evcntlsw);
5516
static always_inline void gen_brinc (DisasContext *ctx)
5517
{
5518
    /* Note: brinc is usable even if SPE is disabled */
5519
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5520
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5521
    gen_op_brinc();
5522
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5523
}
5524

    
5525
#define GEN_SPEOP_ARITH_IMM2(name)                                            \
5526
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5527
{                                                                             \
5528
    if (unlikely(!ctx->spe_enabled)) {                                        \
5529
        GEN_EXCP_NO_AP(ctx);                                                  \
5530
        return;                                                               \
5531
    }                                                                         \
5532
    gen_load_gpr64(cpu_T64[0], rB(ctx->opcode));                              \
5533
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
5534
    gen_op_##name();                                                          \
5535
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5536
}
5537

    
5538
#define GEN_SPEOP_LOGIC_IMM2(name)                                            \
5539
static always_inline void gen_##name##i (DisasContext *ctx)                   \
5540
{                                                                             \
5541
    if (unlikely(!ctx->spe_enabled)) {                                        \
5542
        GEN_EXCP_NO_AP(ctx);                                                  \
5543
        return;                                                               \
5544
    }                                                                         \
5545
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));                              \
5546
    gen_op_splatwi_T1_64(rB(ctx->opcode));                                    \
5547
    gen_op_##name();                                                          \
5548
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5549
}
5550

    
5551
GEN_SPEOP_ARITH_IMM2(evaddw);
5552
#define gen_evaddiw gen_evaddwi
5553
GEN_SPEOP_ARITH_IMM2(evsubfw);
5554
#define gen_evsubifw gen_evsubfwi
5555
GEN_SPEOP_LOGIC_IMM2(evslw);
5556
GEN_SPEOP_LOGIC_IMM2(evsrwu);
5557
#define gen_evsrwis gen_evsrwsi
5558
GEN_SPEOP_LOGIC_IMM2(evsrws);
5559
#define gen_evsrwiu gen_evsrwui
5560
GEN_SPEOP_LOGIC_IMM2(evrlw);
5561

    
5562
static always_inline void gen_evsplati (DisasContext *ctx)
5563
{
5564
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;
5565

    
5566
    gen_op_splatwi_T0_64(imm);
5567
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5568
}
5569

    
5570
static always_inline void gen_evsplatfi (DisasContext *ctx)
5571
{
5572
    uint32_t imm = rA(ctx->opcode) << 27;
5573

    
5574
    gen_op_splatwi_T0_64(imm);
5575
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5576
}
5577

    
5578
/* Comparison */
5579
GEN_SPEOP_COMP(evcmpgtu);
5580
GEN_SPEOP_COMP(evcmpgts);
5581
GEN_SPEOP_COMP(evcmpltu);
5582
GEN_SPEOP_COMP(evcmplts);
5583
GEN_SPEOP_COMP(evcmpeq);
5584

    
5585
GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
5586
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
5587
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
5588
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
5589
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
5590
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
5591
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
5592
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
5593
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
5594
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
5595
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
5596
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
5597
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
5598
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
5599
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
5600
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
5601
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
5602
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
5603
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
5604
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
5605
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
5606
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
5607
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
5608
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
5609
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////
5610

    
5611
static always_inline void gen_evsel (DisasContext *ctx)
5612
{
5613
    if (unlikely(!ctx->spe_enabled)) {
5614
        GEN_EXCP_NO_AP(ctx);
5615
        return;
5616
    }
5617
    tcg_gen_mov_i32(cpu_T[0], cpu_crf[ctx->opcode & 0x7]);
5618
    gen_load_gpr64(cpu_T64[0], rA(ctx->opcode));
5619
    gen_load_gpr64(cpu_T64[1], rB(ctx->opcode));
5620
    gen_op_evsel();
5621
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);
5622
}
5623

    
5624
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
5625
{
5626
    gen_evsel(ctx);
5627
}
5628
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
5629
{
5630
    gen_evsel(ctx);
5631
}
5632
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
5633
{
5634
    gen_evsel(ctx);
5635
}
5636
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
5637
{
5638
    gen_evsel(ctx);
5639
}
5640

    
5641
/* Load and stores */
5642
#if defined(TARGET_PPC64)
5643
/* In that case, we already have 64 bits load & stores
5644
 * so, spe_ldd is equivalent to ld and spe_std is equivalent to std
5645
 */
5646
#define gen_op_spe_ldd_raw           gen_op_ld_raw
5647
#define gen_op_spe_ldd_user          gen_op_ld_user
5648
#define gen_op_spe_ldd_kernel        gen_op_ld_kernel
5649
#define gen_op_spe_ldd_hypv          gen_op_ld_hypv
5650
#define gen_op_spe_ldd_64_raw        gen_op_ld_64_raw
5651
#define gen_op_spe_ldd_64_user       gen_op_ld_64_user
5652
#define gen_op_spe_ldd_64_kernel     gen_op_ld_64_kernel
5653
#define gen_op_spe_ldd_64_hypv       gen_op_ld_64_hypv
5654
#define gen_op_spe_ldd_le_raw        gen_op_ld_le_raw
5655
#define gen_op_spe_ldd_le_user       gen_op_ld_le_user
5656
#define gen_op_spe_ldd_le_kernel     gen_op_ld_le_kernel
5657
#define gen_op_spe_ldd_le_hypv       gen_op_ld_le_hypv
5658
#define gen_op_spe_ldd_le_64_raw     gen_op_ld_le_64_raw
5659
#define gen_op_spe_ldd_le_64_user    gen_op_ld_le_64_user
5660
#define gen_op_spe_ldd_le_64_kernel  gen_op_ld_le_64_kernel
5661
#define gen_op_spe_ldd_le_64_hypv    gen_op_ld_le_64_hypv
5662
#define gen_op_spe_stdd_raw          gen_op_std_raw
5663
#define gen_op_spe_stdd_user         gen_op_std_user
5664
#define gen_op_spe_stdd_kernel       gen_op_std_kernel
5665
#define gen_op_spe_stdd_hypv         gen_op_std_hypv
5666
#define gen_op_spe_stdd_64_raw       gen_op_std_64_raw
5667
#define gen_op_spe_stdd_64_user      gen_op_std_64_user
5668
#define gen_op_spe_stdd_64_kernel    gen_op_std_64_kernel
5669
#define gen_op_spe_stdd_64_hypv      gen_op_std_64_hypv
5670
#define gen_op_spe_stdd_le_raw       gen_op_std_le_raw
5671
#define gen_op_spe_stdd_le_user      gen_op_std_le_user
5672
#define gen_op_spe_stdd_le_kernel    gen_op_std_le_kernel
5673
#define gen_op_spe_stdd_le_hypv      gen_op_std_le_hypv
5674
#define gen_op_spe_stdd_le_64_raw    gen_op_std_le_64_raw
5675
#define gen_op_spe_stdd_le_64_user   gen_op_std_le_64_user
5676
#define gen_op_spe_stdd_le_64_kernel gen_op_std_le_64_kernel
5677
#define gen_op_spe_stdd_le_64_hypv   gen_op_std_le_64_hypv
5678
#endif /* defined(TARGET_PPC64) */
5679
GEN_SPEOP_LDST(dd, 3);
5680
GEN_SPEOP_LDST(dw, 3);
5681
GEN_SPEOP_LDST(dh, 3);
5682
GEN_SPEOP_LDST(whe, 2);
5683
GEN_SPEOP_LD(whou, 2);
5684
GEN_SPEOP_LD(whos, 2);
5685
GEN_SPEOP_ST(who, 2);
5686

    
5687
#if defined(TARGET_PPC64)
5688
/* In that case, spe_stwwo is equivalent to stw */
5689
#define gen_op_spe_stwwo_raw          gen_op_stw_raw
5690
#define gen_op_spe_stwwo_user         gen_op_stw_user
5691
#define gen_op_spe_stwwo_kernel       gen_op_stw_kernel
5692
#define gen_op_spe_stwwo_hypv         gen_op_stw_hypv
5693
#define gen_op_spe_stwwo_le_raw       gen_op_stw_le_raw
5694
#define gen_op_spe_stwwo_le_user      gen_op_stw_le_user
5695
#define gen_op_spe_stwwo_le_kernel    gen_op_stw_le_kernel
5696
#define gen_op_spe_stwwo_le_hypv      gen_op_stw_le_hypv
5697
#define gen_op_spe_stwwo_64_raw       gen_op_stw_64_raw
5698
#define gen_op_spe_stwwo_64_user      gen_op_stw_64_user
5699
#define gen_op_spe_stwwo_64_kernel    gen_op_stw_64_kernel
5700
#define gen_op_spe_stwwo_64_hypv      gen_op_stw_64_hypv
5701
#define gen_op_spe_stwwo_le_64_raw    gen_op_stw_le_64_raw
5702
#define gen_op_spe_stwwo_le_64_user   gen_op_stw_le_64_user
5703
#define gen_op_spe_stwwo_le_64_kernel gen_op_stw_le_64_kernel
5704
#define gen_op_spe_stwwo_le_64_hypv   gen_op_stw_le_64_hypv
5705
#endif
5706
#define _GEN_OP_SPE_STWWE(suffix)                                             \
5707
static always_inline void gen_op_spe_stwwe_##suffix (void)                    \
5708
{                                                                             \
5709
    gen_op_srli32_T1_64();                                                    \
5710
    gen_op_spe_stwwo_##suffix();                                              \
5711
}
5712
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
5713
static always_inline void gen_op_spe_stwwe_le_##suffix (void)                 \
5714
{                                                                             \
5715
    gen_op_srli32_T1_64();                                                    \
5716
    gen_op_spe_stwwo_le_##suffix();                                           \
5717
}
5718
#if defined(TARGET_PPC64)
5719
#define GEN_OP_SPE_STWWE(suffix)                                              \
5720
_GEN_OP_SPE_STWWE(suffix);                                                    \
5721
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
5722
static always_inline void gen_op_spe_stwwe_64_##suffix (void)                 \
5723
{                                                                             \
5724
    gen_op_srli32_T1_64();                                                    \
5725
    gen_op_spe_stwwo_64_##suffix();                                           \
5726
}                                                                             \
5727
static always_inline void gen_op_spe_stwwe_le_64_##suffix (void)              \
5728
{                                                                             \
5729
    gen_op_srli32_T1_64();                                                    \
5730
    gen_op_spe_stwwo_le_64_##suffix();                                        \
5731
}
5732
#else
5733
#define GEN_OP_SPE_STWWE(suffix)                                              \
5734
_GEN_OP_SPE_STWWE(suffix);                                                    \
5735
_GEN_OP_SPE_STWWE_LE(suffix)
5736
#endif
5737
#if defined(CONFIG_USER_ONLY)
5738
GEN_OP_SPE_STWWE(raw);
5739
#else /* defined(CONFIG_USER_ONLY) */
5740
GEN_OP_SPE_STWWE(user);
5741
GEN_OP_SPE_STWWE(kernel);
5742
GEN_OP_SPE_STWWE(hypv);
5743
#endif /* defined(CONFIG_USER_ONLY) */
5744
GEN_SPEOP_ST(wwe, 2);
5745
GEN_SPEOP_ST(wwo, 2);
5746

    
5747
#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
5748
static always_inline void gen_op_spe_l##name##_##suffix (void)                \
5749
{                                                                             \
5750
    gen_op_##op##_##suffix();                                                 \
5751
    gen_op_splatw_T1_64();                                                    \
5752
}
5753

    
5754
#define GEN_OP_SPE_LHE(suffix)                                                \
5755
static always_inline void gen_op_spe_lhe_##suffix (void)                      \
5756
{                                                                             \
5757
    gen_op_spe_lh_##suffix();                                                 \
5758
    gen_op_sli16_T1_64();                                                     \
5759
}
5760

    
5761
#define GEN_OP_SPE_LHX(suffix)                                                \
5762
static always_inline void gen_op_spe_lhx_##suffix (void)                      \
5763
{                                                                             \
5764
    gen_op_spe_lh_##suffix();                                                 \
5765
    gen_op_extsh_T1_64();                                                     \
5766
}
5767

    
5768
#if defined(CONFIG_USER_ONLY)
5769
GEN_OP_SPE_LHE(raw);
5770
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
5771
GEN_OP_SPE_LHE(le_raw);
5772
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
5773
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
5774
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
5775
GEN_OP_SPE_LHX(raw);
5776
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
5777
GEN_OP_SPE_LHX(le_raw);
5778
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
5779
#if defined(TARGET_PPC64)
5780
GEN_OP_SPE_LHE(64_raw);
5781
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
5782
GEN_OP_SPE_LHE(le_64_raw);
5783
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
5784
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
5785
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
5786
GEN_OP_SPE_LHX(64_raw);
5787
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
5788
GEN_OP_SPE_LHX(le_64_raw);
5789
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
5790
#endif
5791
#else
5792
GEN_OP_SPE_LHE(user);
5793
GEN_OP_SPE_LHE(kernel);
5794
GEN_OP_SPE_LHE(hypv);
5795
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
5796
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
5797
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, hypv);
5798
GEN_OP_SPE_LHE(le_user);
5799
GEN_OP_SPE_LHE(le_kernel);
5800
GEN_OP_SPE_LHE(le_hypv);
5801
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
5802
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
5803
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_hypv);
5804
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
5805
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
5806
GEN_SPE_LDSPLAT(hhousplat, spe_lh, hypv);
5807
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
5808
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
5809
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_hypv);
5810
GEN_OP_SPE_LHX(user);
5811
GEN_OP_SPE_LHX(kernel);
5812
GEN_OP_SPE_LHX(hypv);
5813
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
5814
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
5815
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, hypv);
5816
GEN_OP_SPE_LHX(le_user);
5817
GEN_OP_SPE_LHX(le_kernel);
5818
GEN_OP_SPE_LHX(le_hypv);
5819
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
5820
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
5821
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_hypv);
5822
#if defined(TARGET_PPC64)
5823
GEN_OP_SPE_LHE(64_user);
5824
GEN_OP_SPE_LHE(64_kernel);
5825
GEN_OP_SPE_LHE(64_hypv);
5826
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
5827
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
5828
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_hypv);
5829
GEN_OP_SPE_LHE(le_64_user);
5830
GEN_OP_SPE_LHE(le_64_kernel);
5831
GEN_OP_SPE_LHE(le_64_hypv);
5832
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
5833
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
5834
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_hypv);
5835
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
5836
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
5837
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_hypv);
5838
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
5839
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
5840
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_hypv);
5841
GEN_OP_SPE_LHX(64_user);
5842
GEN_OP_SPE_LHX(64_kernel);
5843
GEN_OP_SPE_LHX(64_hypv);
5844
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
5845
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
5846
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_hypv);
5847
GEN_OP_SPE_LHX(le_64_user);
5848
GEN_OP_SPE_LHX(le_64_kernel);
5849
GEN_OP_SPE_LHX(le_64_hypv);
5850
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
5851
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
5852
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_hypv);
5853
#endif
5854
#endif
5855
GEN_SPEOP_LD(hhesplat, 1);
5856
GEN_SPEOP_LD(hhousplat, 1);
5857
GEN_SPEOP_LD(hhossplat, 1);
5858
GEN_SPEOP_LD(wwsplat, 2);
5859
GEN_SPEOP_LD(whsplat, 2);
5860

    
5861
GEN_SPE(evlddx,         evldd,         0x00, 0x0C, 0x00000000, PPC_SPE); //
5862
GEN_SPE(evldwx,         evldw,         0x01, 0x0C, 0x00000000, PPC_SPE); //
5863
GEN_SPE(evldhx,         evldh,         0x02, 0x0C, 0x00000000, PPC_SPE); //
5864
GEN_SPE(evlhhesplatx,   evlhhesplat,   0x04, 0x0C, 0x00000000, PPC_SPE); //
5865
GEN_SPE(evlhhousplatx,  evlhhousplat,  0x06, 0x0C, 0x00000000, PPC_SPE); //
5866
GEN_SPE(evlhhossplatx,  evlhhossplat,  0x07, 0x0C, 0x00000000, PPC_SPE); //
5867
GEN_SPE(evlwhex,        evlwhe,        0x08, 0x0C, 0x00000000, PPC_SPE); //
5868
GEN_SPE(evlwhoux,       evlwhou,       0x0A, 0x0C, 0x00000000, PPC_SPE); //
5869
GEN_SPE(evlwhosx,       evlwhos,       0x0B, 0x0C, 0x00000000, PPC_SPE); //
5870
GEN_SPE(evlwwsplatx,    evlwwsplat,    0x0C, 0x0C, 0x00000000, PPC_SPE); //
5871
GEN_SPE(evlwhsplatx,    evlwhsplat,    0x0E, 0x0C, 0x00000000, PPC_SPE); //
5872
GEN_SPE(evstddx,        evstdd,        0x10, 0x0C, 0x00000000, PPC_SPE); //
5873
GEN_SPE(evstdwx,        evstdw,        0x11, 0x0C, 0x00000000, PPC_SPE); //
5874
GEN_SPE(evstdhx,        evstdh,        0x12, 0x0C, 0x00000000, PPC_SPE); //
5875
GEN_SPE(evstwhex,       evstwhe,       0x18, 0x0C, 0x00000000, PPC_SPE); //
5876
GEN_SPE(evstwhox,       evstwho,       0x1A, 0x0C, 0x00000000, PPC_SPE); //
5877
GEN_SPE(evstwwex,       evstwwe,       0x1C, 0x0C, 0x00000000, PPC_SPE); //
5878
GEN_SPE(evstwwox,       evstwwo,       0x1E, 0x0C, 0x00000000, PPC_SPE); //
5879

    
5880
/* Multiply and add - TODO */
5881
#if 0
5882
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
5883
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
5884
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
5885
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
5886
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
5887
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
5888
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
5889
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
5890
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
5891
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
5892
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
5893
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);
5894

5895
GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
5896
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
5897
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
5898
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
5899
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
5900
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
5901
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
5902
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
5903
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
5904
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
5905
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
5906
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
5907
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
5908
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);
5909

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

5917
GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
5918
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
5919
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
5920
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
5921
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
5922
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
5923
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
5924
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
5925
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
5926
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
5927
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
5928
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);
5929

5930
GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
5931
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
5932
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
5933
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
5934
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);
5935

5936
GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
5937
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
5938
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
5939
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
5940
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
5941
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
5942
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
5943
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
5944
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
5945
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
5946
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
5947
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);
5948

5949
GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
5950
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
5951
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
5952
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
5953
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
5954
#endif
5955

    
5956
/***                      SPE floating-point extension                     ***/
5957
#define GEN_SPEFPUOP_CONV(name)                                               \
5958
static always_inline void gen_##name (DisasContext *ctx)                      \
5959
{                                                                             \
5960
    gen_load_gpr64(cpu_T64[0], rB(ctx->opcode));                              \
5961
    gen_op_##name();                                                          \
5962
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[0]);                             \
5963
}
5964

    
5965
/* Single precision floating-point vectors operations */
5966
/* Arithmetic */
5967
GEN_SPEOP_ARITH2(evfsadd);
5968
GEN_SPEOP_ARITH2(evfssub);
5969
GEN_SPEOP_ARITH2(evfsmul);
5970
GEN_SPEOP_ARITH2(evfsdiv);
5971
GEN_SPEOP_ARITH1(evfsabs);
5972
GEN_SPEOP_ARITH1(evfsnabs);
5973
GEN_SPEOP_ARITH1(evfsneg);
5974
/* Conversion */
5975
GEN_SPEFPUOP_CONV(evfscfui);
5976
GEN_SPEFPUOP_CONV(evfscfsi);
5977
GEN_SPEFPUOP_CONV(evfscfuf);
5978
GEN_SPEFPUOP_CONV(evfscfsf);
5979
GEN_SPEFPUOP_CONV(evfsctui);
5980
GEN_SPEFPUOP_CONV(evfsctsi);
5981
GEN_SPEFPUOP_CONV(evfsctuf);
5982
GEN_SPEFPUOP_CONV(evfsctsf);
5983
GEN_SPEFPUOP_CONV(evfsctuiz);
5984
GEN_SPEFPUOP_CONV(evfsctsiz);
5985
/* Comparison */
5986
GEN_SPEOP_COMP(evfscmpgt);
5987
GEN_SPEOP_COMP(evfscmplt);
5988
GEN_SPEOP_COMP(evfscmpeq);
5989
GEN_SPEOP_COMP(evfststgt);
5990
GEN_SPEOP_COMP(evfststlt);
5991
GEN_SPEOP_COMP(evfststeq);
5992

    
5993
/* Opcodes definitions */
5994
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
5995
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
5996
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
5997
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
5998
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
5999
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
6000
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
6001
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
6002
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
6003
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
6004
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
6005
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
6006
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
6007
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
6008

    
6009
/* Single precision floating-point operations */
6010
/* Arithmetic */
6011
GEN_SPEOP_ARITH2(efsadd);
6012
GEN_SPEOP_ARITH2(efssub);
6013
GEN_SPEOP_ARITH2(efsmul);
6014
GEN_SPEOP_ARITH2(efsdiv);
6015
GEN_SPEOP_ARITH1(efsabs);
6016
GEN_SPEOP_ARITH1(efsnabs);
6017
GEN_SPEOP_ARITH1(efsneg);
6018
/* Conversion */
6019
GEN_SPEFPUOP_CONV(efscfui);
6020
GEN_SPEFPUOP_CONV(efscfsi);
6021
GEN_SPEFPUOP_CONV(efscfuf);
6022
GEN_SPEFPUOP_CONV(efscfsf);
6023
GEN_SPEFPUOP_CONV(efsctui);
6024
GEN_SPEFPUOP_CONV(efsctsi);
6025
GEN_SPEFPUOP_CONV(efsctuf);
6026
GEN_SPEFPUOP_CONV(efsctsf);
6027
GEN_SPEFPUOP_CONV(efsctuiz);
6028
GEN_SPEFPUOP_CONV(efsctsiz);
6029
GEN_SPEFPUOP_CONV(efscfd);
6030
/* Comparison */
6031
GEN_SPEOP_COMP(efscmpgt);
6032
GEN_SPEOP_COMP(efscmplt);
6033
GEN_SPEOP_COMP(efscmpeq);
6034
GEN_SPEOP_COMP(efststgt);
6035
GEN_SPEOP_COMP(efststlt);
6036
GEN_SPEOP_COMP(efststeq);
6037

    
6038
/* Opcodes definitions */
6039
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
6040
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
6041
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
6042
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
6043
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
6044
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
6045
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
6046
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
6047
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
6048
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
6049
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
6050
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
6051
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
6052
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
6053

    
6054
/* Double precision floating-point operations */
6055
/* Arithmetic */
6056
GEN_SPEOP_ARITH2(efdadd);
6057
GEN_SPEOP_ARITH2(efdsub);
6058
GEN_SPEOP_ARITH2(efdmul);
6059
GEN_SPEOP_ARITH2(efddiv);
6060
GEN_SPEOP_ARITH1(efdabs);
6061
GEN_SPEOP_ARITH1(efdnabs);
6062
GEN_SPEOP_ARITH1(efdneg);
6063
/* Conversion */
6064

    
6065
GEN_SPEFPUOP_CONV(efdcfui);
6066
GEN_SPEFPUOP_CONV(efdcfsi);
6067
GEN_SPEFPUOP_CONV(efdcfuf);
6068
GEN_SPEFPUOP_CONV(efdcfsf);
6069
GEN_SPEFPUOP_CONV(efdctui);
6070
GEN_SPEFPUOP_CONV(efdctsi);
6071
GEN_SPEFPUOP_CONV(efdctuf);
6072
GEN_SPEFPUOP_CONV(efdctsf);
6073
GEN_SPEFPUOP_CONV(efdctuiz);
6074
GEN_SPEFPUOP_CONV(efdctsiz);
6075
GEN_SPEFPUOP_CONV(efdcfs);
6076
GEN_SPEFPUOP_CONV(efdcfuid);
6077
GEN_SPEFPUOP_CONV(efdcfsid);
6078
GEN_SPEFPUOP_CONV(efdctuidz);
6079
GEN_SPEFPUOP_CONV(efdctsidz);
6080
/* Comparison */
6081
GEN_SPEOP_COMP(efdcmpgt);
6082
GEN_SPEOP_COMP(efdcmplt);
6083
GEN_SPEOP_COMP(efdcmpeq);
6084
GEN_SPEOP_COMP(efdtstgt);
6085
GEN_SPEOP_COMP(efdtstlt);
6086
GEN_SPEOP_COMP(efdtsteq);
6087

    
6088
/* Opcodes definitions */
6089
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
6090
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
6091
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
6092
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
6093
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
6094
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
6095
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
6096
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
6097
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
6098
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
6099
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
6100
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
6101
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
6102
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
6103
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
6104
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
6105

    
6106
/* End opcode list */
6107
GEN_OPCODE_MARK(end);
6108

    
6109
#include "translate_init.c"
6110
#include "helper_regs.h"
6111

    
6112
/*****************************************************************************/
6113
/* Misc PowerPC helpers */
6114
void cpu_dump_state (CPUState *env, FILE *f,
6115
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
6116
                     int flags)
6117
{
6118
#define RGPL  4
6119
#define RFPL  4
6120

    
6121
    int i;
6122

    
6123
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
6124
                env->nip, env->lr, env->ctr, hreg_load_xer(env));
6125
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
6126
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
6127
#if !defined(NO_TIMER_DUMP)
6128
    cpu_fprintf(f, "TB %08x %08x "
6129
#if !defined(CONFIG_USER_ONLY)
6130
                "DECR %08x"
6131
#endif
6132
                "\n",
6133
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6134
#if !defined(CONFIG_USER_ONLY)
6135
                , cpu_ppc_load_decr(env)
6136
#endif
6137
                );
6138
#endif
6139
    for (i = 0; i < 32; i++) {
6140
        if ((i & (RGPL - 1)) == 0)
6141
            cpu_fprintf(f, "GPR%02d", i);
6142
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
6143
        if ((i & (RGPL - 1)) == (RGPL - 1))
6144
            cpu_fprintf(f, "\n");
6145
    }
6146
    cpu_fprintf(f, "CR ");
6147
    for (i = 0; i < 8; i++)
6148
        cpu_fprintf(f, "%01x", env->crf[i]);
6149
    cpu_fprintf(f, "  [");
6150
    for (i = 0; i < 8; i++) {
6151
        char a = '-';
6152
        if (env->crf[i] & 0x08)
6153
            a = 'L';
6154
        else if (env->crf[i] & 0x04)
6155
            a = 'G';
6156
        else if (env->crf[i] & 0x02)
6157
            a = 'E';
6158
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6159
    }
6160
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
6161
    for (i = 0; i < 32; i++) {
6162
        if ((i & (RFPL - 1)) == 0)
6163
            cpu_fprintf(f, "FPR%02d", i);
6164
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6165
        if ((i & (RFPL - 1)) == (RFPL - 1))
6166
            cpu_fprintf(f, "\n");
6167
    }
6168
#if !defined(CONFIG_USER_ONLY)
6169
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
6170
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
6171
#endif
6172

    
6173
#undef RGPL
6174
#undef RFPL
6175
}
6176

    
6177
void cpu_dump_statistics (CPUState *env, FILE*f,
6178
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
6179
                          int flags)
6180
{
6181
#if defined(DO_PPC_STATISTICS)
6182
    opc_handler_t **t1, **t2, **t3, *handler;
6183
    int op1, op2, op3;
6184

    
6185
    t1 = env->opcodes;
6186
    for (op1 = 0; op1 < 64; op1++) {
6187
        handler = t1[op1];
6188
        if (is_indirect_opcode(handler)) {
6189
            t2 = ind_table(handler);
6190
            for (op2 = 0; op2 < 32; op2++) {
6191
                handler = t2[op2];
6192
                if (is_indirect_opcode(handler)) {
6193
                    t3 = ind_table(handler);
6194
                    for (op3 = 0; op3 < 32; op3++) {
6195
                        handler = t3[op3];
6196
                        if (handler->count == 0)
6197
                            continue;
6198
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
6199
                                    "%016llx %lld\n",
6200
                                    op1, op2, op3, op1, (op3 << 5) | op2,
6201
                                    handler->oname,
6202
                                    handler->count, handler->count);
6203
                    }
6204
                } else {
6205
                    if (handler->count == 0)
6206
                        continue;
6207
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
6208
                                "%016llx %lld\n",
6209
                                op1, op2, op1, op2, handler->oname,
6210
                                handler->count, handler->count);
6211
                }
6212
            }
6213
        } else {
6214
            if (handler->count == 0)
6215
                continue;
6216
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
6217
                        op1, op1, handler->oname,
6218
                        handler->count, handler->count);
6219
        }
6220
    }
6221
#endif
6222
}
6223

    
6224
/*****************************************************************************/
6225
static always_inline void gen_intermediate_code_internal (CPUState *env,
6226
                                                          TranslationBlock *tb,
6227
                                                          int search_pc)
6228
{
6229
    DisasContext ctx, *ctxp = &ctx;
6230
    opc_handler_t **table, *handler;
6231
    target_ulong pc_start;
6232
    uint16_t *gen_opc_end;
6233
    int supervisor, little_endian;
6234
    int j, lj = -1;
6235
    int num_insns;
6236
    int max_insns;
6237

    
6238
    pc_start = tb->pc;
6239
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
6240
#if defined(OPTIMIZE_FPRF_UPDATE)
6241
    gen_fprf_ptr = gen_fprf_buf;
6242
#endif
6243
    ctx.nip = pc_start;
6244
    ctx.tb = tb;
6245
    ctx.exception = POWERPC_EXCP_NONE;
6246
    ctx.spr_cb = env->spr_cb;
6247
    supervisor = env->mmu_idx;
6248
#if !defined(CONFIG_USER_ONLY)
6249
    ctx.supervisor = supervisor;
6250
#endif
6251
    little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
6252
#if defined(TARGET_PPC64)
6253
    ctx.sf_mode = msr_sf;
6254
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
6255
#else
6256
    ctx.mem_idx = (supervisor << 1) | little_endian;
6257
#endif
6258
    ctx.dcache_line_size = env->dcache_line_size;
6259
    ctx.fpu_enabled = msr_fp;
6260
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
6261
        ctx.spe_enabled = msr_spe;
6262
    else
6263
        ctx.spe_enabled = 0;
6264
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
6265
        ctx.altivec_enabled = msr_vr;
6266
    else
6267
        ctx.altivec_enabled = 0;
6268
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
6269
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
6270
    else
6271
        ctx.singlestep_enabled = 0;
6272
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
6273
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
6274
    if (unlikely(env->singlestep_enabled))
6275
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
6276
#if defined (DO_SINGLE_STEP) && 0
6277
    /* Single step trace mode */
6278
    msr_se = 1;
6279
#endif
6280
    num_insns = 0;
6281
    max_insns = tb->cflags & CF_COUNT_MASK;
6282
    if (max_insns == 0)
6283
        max_insns = CF_COUNT_MASK;
6284

    
6285
    gen_icount_start();
6286
    /* Set env in case of segfault during code fetch */
6287
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6288
        if (unlikely(env->nb_breakpoints > 0)) {
6289
            for (j = 0; j < env->nb_breakpoints; j++) {
6290
                if (env->breakpoints[j] == ctx.nip) {
6291
                    gen_update_nip(&ctx, ctx.nip);
6292
                    gen_op_debug();
6293
                    break;
6294
                }
6295
            }
6296
        }
6297
        if (unlikely(search_pc)) {
6298
            j = gen_opc_ptr - gen_opc_buf;
6299
            if (lj < j) {
6300
                lj++;
6301
                while (lj < j)
6302
                    gen_opc_instr_start[lj++] = 0;
6303
                gen_opc_pc[lj] = ctx.nip;
6304
                gen_opc_instr_start[lj] = 1;
6305
                gen_opc_icount[lj] = num_insns;
6306
            }
6307
        }
6308
#if defined PPC_DEBUG_DISAS
6309
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6310
            fprintf(logfile, "----------------\n");
6311
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6312
                    ctx.nip, supervisor, (int)msr_ir);
6313
        }
6314
#endif
6315
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
6316
            gen_io_start();
6317
        if (unlikely(little_endian)) {
6318
            ctx.opcode = bswap32(ldl_code(ctx.nip));
6319
        } else {
6320
            ctx.opcode = ldl_code(ctx.nip);
6321
        }
6322
#if defined PPC_DEBUG_DISAS
6323
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6324
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6325
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6326
                    opc3(ctx.opcode), little_endian ? "little" : "big");
6327
        }
6328
#endif
6329
        ctx.nip += 4;
6330
        table = env->opcodes;
6331
        num_insns++;
6332
        handler = table[opc1(ctx.opcode)];
6333
        if (is_indirect_opcode(handler)) {
6334
            table = ind_table(handler);
6335
            handler = table[opc2(ctx.opcode)];
6336
            if (is_indirect_opcode(handler)) {
6337
                table = ind_table(handler);
6338
                handler = table[opc3(ctx.opcode)];
6339
            }
6340
        }
6341
        /* Is opcode *REALLY* valid ? */
6342
        if (unlikely(handler->handler == &gen_invalid)) {
6343
            if (loglevel != 0) {
6344
                fprintf(logfile, "invalid/unsupported opcode: "
6345
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
6346
                        opc1(ctx.opcode), opc2(ctx.opcode),
6347
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
6348
            } else {
6349
                printf("invalid/unsupported opcode: "
6350
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
6351
                       opc1(ctx.opcode), opc2(ctx.opcode),
6352
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
6353
            }
6354
        } else {
6355
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
6356
                if (loglevel != 0) {
6357
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6358
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
6359
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
6360
                            opc2(ctx.opcode), opc3(ctx.opcode),
6361
                            ctx.opcode, ctx.nip - 4);
6362
                } else {
6363
                    printf("invalid bits: %08x for opcode: "
6364
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
6365
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
6366
                           opc2(ctx.opcode), opc3(ctx.opcode),
6367
                           ctx.opcode, ctx.nip - 4);
6368
                }
6369
                GEN_EXCP_INVAL(ctxp);
6370
                break;
6371
            }
6372
        }
6373
        (*(handler->handler))(&ctx);
6374
#if defined(DO_PPC_STATISTICS)
6375
        handler->count++;
6376
#endif
6377
        /* Check trace mode exceptions */
6378
        if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
6379
                     (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
6380
                     ctx.exception != POWERPC_SYSCALL &&
6381
                     ctx.exception != POWERPC_EXCP_TRAP &&
6382
                     ctx.exception != POWERPC_EXCP_BRANCH)) {
6383
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6384
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
6385
                            (env->singlestep_enabled) ||
6386
                            num_insns >= max_insns)) {
6387
            /* if we reach a page boundary or are single stepping, stop
6388
             * generation
6389
             */
6390
            break;
6391
        }
6392
#if defined (DO_SINGLE_STEP)
6393
        break;
6394
#endif
6395
    }
6396
    if (tb->cflags & CF_LAST_IO)
6397
        gen_io_end();
6398
    if (ctx.exception == POWERPC_EXCP_NONE) {
6399
        gen_goto_tb(&ctx, 0, ctx.nip);
6400
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6401
        if (unlikely(env->singlestep_enabled)) {
6402
            gen_update_nip(&ctx, ctx.nip);
6403
            gen_op_debug();
6404
        }
6405
        /* Generate the return instruction */
6406
        tcg_gen_exit_tb(0);
6407
    }
6408
    gen_icount_end(tb, num_insns);
6409
    *gen_opc_ptr = INDEX_op_end;
6410
    if (unlikely(search_pc)) {
6411
        j = gen_opc_ptr - gen_opc_buf;
6412
        lj++;
6413
        while (lj <= j)
6414
            gen_opc_instr_start[lj++] = 0;
6415
    } else {
6416
        tb->size = ctx.nip - pc_start;
6417
        tb->icount = num_insns;
6418
    }
6419
#if defined(DEBUG_DISAS)
6420
    if (loglevel & CPU_LOG_TB_CPU) {
6421
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
6422
        cpu_dump_state(env, logfile, fprintf, 0);
6423
    }
6424
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6425
        int flags;
6426
        flags = env->bfd_mach;
6427
        flags |= little_endian << 16;
6428
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6429
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
6430
        fprintf(logfile, "\n");
6431
    }
6432
#endif
6433
}
6434

    
6435
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
6436
{
6437
    gen_intermediate_code_internal(env, tb, 0);
6438
}
6439

    
6440
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
6441
{
6442
    gen_intermediate_code_internal(env, tb, 1);
6443
}
6444

    
6445
void gen_pc_load(CPUState *env, TranslationBlock *tb,
6446
                unsigned long searched_pc, int pc_pos, void *puc)
6447
{
6448
    int type, c;
6449
    /* for PPC, we need to look at the micro operation to get the
6450
     * access type */
6451
    env->nip = gen_opc_pc[pc_pos];
6452
    c = gen_opc_buf[pc_pos];
6453
    switch(c) {
6454
#if defined(CONFIG_USER_ONLY)
6455
#define CASE3(op)\
6456
    case INDEX_op_ ## op ## _raw
6457
#else
6458
#define CASE3(op)\
6459
    case INDEX_op_ ## op ## _user:\
6460
    case INDEX_op_ ## op ## _kernel:\
6461
    case INDEX_op_ ## op ## _hypv
6462
#endif
6463

    
6464
    CASE3(stfd):
6465
    CASE3(stfs):
6466
    CASE3(lfd):
6467
    CASE3(lfs):
6468
        type = ACCESS_FLOAT;
6469
        break;
6470
    CASE3(lwarx):
6471
        type = ACCESS_RES;
6472
        break;
6473
    CASE3(stwcx):
6474
        type = ACCESS_RES;
6475
        break;
6476
    CASE3(eciwx):
6477
    CASE3(ecowx):
6478
        type = ACCESS_EXT;
6479
        break;
6480
    default:
6481
        type = ACCESS_INT;
6482
        break;
6483
    }
6484
    env->access_type = type;
6485
}