Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ fe1e5c53

History | View | Annotate | Download (285.7 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 "tcg-op.h"
30
#include "qemu-common.h"
31

    
32
#include "helper.h"
33
#define GEN_HELPER 1
34
#include "helper.h"
35

    
36
#define CPU_SINGLE_STEP 0x1
37
#define CPU_BRANCH_STEP 0x2
38
#define GDBSTUB_SINGLE_STEP 0x4
39

    
40
/* Include definitions for instructions classes and implementations flags */
41
//#define DO_SINGLE_STEP
42
//#define PPC_DEBUG_DISAS
43
//#define DO_PPC_STATISTICS
44
//#define OPTIMIZE_FPRF_UPDATE
45

    
46
/*****************************************************************************/
47
/* Code translation helpers                                                  */
48

    
49
/* global register indexes */
50
static TCGv_ptr cpu_env;
51
static char cpu_reg_names[10*3 + 22*4 /* GPR */
52
#if !defined(TARGET_PPC64)
53
    + 10*4 + 22*5 /* SPE GPRh */
54
#endif
55
    + 10*4 + 22*5 /* FPR */
56
    + 2*(10*6 + 22*7) /* AVRh, AVRl */
57
    + 8*5 /* CRF */];
58
static TCGv cpu_gpr[32];
59
#if !defined(TARGET_PPC64)
60
static TCGv cpu_gprh[32];
61
#endif
62
static TCGv_i64 cpu_fpr[32];
63
static TCGv_i64 cpu_avrh[32], cpu_avrl[32];
64
static TCGv_i32 cpu_crf[8];
65
static TCGv cpu_nip;
66
static TCGv cpu_ctr;
67
static TCGv cpu_lr;
68
static TCGv cpu_xer;
69
static TCGv_i32 cpu_fpscr;
70
static TCGv_i32 cpu_access_type;
71

    
72
/* dyngen register indexes */
73
static TCGv cpu_T[3];
74
#if defined(TARGET_PPC64)
75
#define cpu_T64 cpu_T
76
#else
77
static TCGv_i64 cpu_T64[3];
78
#endif
79
static TCGv_i64 cpu_FT[2];
80

    
81
#include "gen-icount.h"
82

    
83
void ppc_translate_init(void)
84
{
85
    int i;
86
    char* p;
87
    static int done_init = 0;
88

    
89
    if (done_init)
90
        return;
91

    
92
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
93
#if TARGET_LONG_BITS > HOST_LONG_BITS
94
    cpu_T[0] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t0), "T0");
95
    cpu_T[1] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t1), "T1");
96
    cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
97
#else
98
    cpu_T[0] = tcg_global_reg_new(TCG_AREG1, "T0");
99
    cpu_T[1] = tcg_global_reg_new(TCG_AREG2, "T1");
100
#ifdef HOST_I386
101
    /* XXX: This is a temporary workaround for i386.
102
     *      On i386 qemu_st32 runs out of registers.
103
     *      The proper fix is to remove cpu_T.
104
     */
105
    cpu_T[2] = tcg_global_mem_new(TCG_AREG0, offsetof(CPUState, t2), "T2");
106
#else
107
    cpu_T[2] = tcg_global_reg_new(TCG_AREG3, "T2");
108
#endif
109
#endif
110
#if !defined(TARGET_PPC64)
111
    cpu_T64[0] = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUState, t0_64),
112
                                        "T0_64");
113
    cpu_T64[1] = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUState, t1_64),
114
                                        "T1_64");
115
    cpu_T64[2] = tcg_global_mem_new_i64(TCG_AREG0, offsetof(CPUState, t2_64),
116
                                        "T2_64");
117
#endif
118

    
119
    cpu_FT[0] = tcg_global_mem_new_i64(TCG_AREG0,
120
                                       offsetof(CPUState, ft0), "FT0");
121
    cpu_FT[1] = tcg_global_mem_new_i64(TCG_AREG0,
122
                                       offsetof(CPUState, ft1), "FT1");
123

    
124
    p = cpu_reg_names;
125

    
126
    for (i = 0; i < 8; i++) {
127
        sprintf(p, "crf%d", i);
128
        cpu_crf[i] = tcg_global_mem_new_i32(TCG_AREG0,
129
                                            offsetof(CPUState, crf[i]), p);
130
        p += 5;
131
    }
132

    
133
    for (i = 0; i < 32; i++) {
134
        sprintf(p, "r%d", i);
135
        cpu_gpr[i] = tcg_global_mem_new(TCG_AREG0,
136
                                        offsetof(CPUState, gpr[i]), p);
137
        p += (i < 10) ? 3 : 4;
138
#if !defined(TARGET_PPC64)
139
        sprintf(p, "r%dH", i);
140
        cpu_gprh[i] = tcg_global_mem_new_i32(TCG_AREG0,
141
                                             offsetof(CPUState, gprh[i]), p);
142
        p += (i < 10) ? 4 : 5;
143
#endif
144

    
145
        sprintf(p, "fp%d", i);
146
        cpu_fpr[i] = tcg_global_mem_new_i64(TCG_AREG0,
147
                                            offsetof(CPUState, fpr[i]), p);
148
        p += (i < 10) ? 4 : 5;
149

    
150
        sprintf(p, "avr%dH", i);
151
#ifdef WORDS_BIGENDIAN
152
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
153
                                             offsetof(CPUState, avr[i].u64[0]), p);
154
#else
155
        cpu_avrh[i] = tcg_global_mem_new_i64(TCG_AREG0,
156
                                             offsetof(CPUState, avr[i].u64[1]), p);
157
#endif
158
        p += (i < 10) ? 6 : 7;
159

    
160
        sprintf(p, "avr%dL", i);
161
#ifdef WORDS_BIGENDIAN
162
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
163
                                             offsetof(CPUState, avr[i].u64[1]), p);
164
#else
165
        cpu_avrl[i] = tcg_global_mem_new_i64(TCG_AREG0,
166
                                             offsetof(CPUState, avr[i].u64[0]), p);
167
#endif
168
        p += (i < 10) ? 6 : 7;
169
    }
170

    
171
    cpu_nip = tcg_global_mem_new(TCG_AREG0,
172
                                 offsetof(CPUState, nip), "nip");
173

    
174
    cpu_ctr = tcg_global_mem_new(TCG_AREG0,
175
                                 offsetof(CPUState, ctr), "ctr");
176

    
177
    cpu_lr = tcg_global_mem_new(TCG_AREG0,
178
                                offsetof(CPUState, lr), "lr");
179

    
180
    cpu_xer = tcg_global_mem_new(TCG_AREG0,
181
                                 offsetof(CPUState, xer), "xer");
182

    
183
    cpu_fpscr = tcg_global_mem_new_i32(TCG_AREG0,
184
                                       offsetof(CPUState, fpscr), "fpscr");
185

    
186
    cpu_access_type = tcg_global_mem_new_i32(TCG_AREG0,
187
                                             offsetof(CPUState, access_type), "access_type");
188

    
189
    /* register helpers */
190
#define GEN_HELPER 2
191
#include "helper.h"
192

    
193
    done_init = 1;
194
}
195

    
196
#if defined(OPTIMIZE_FPRF_UPDATE)
197
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
198
static uint16_t **gen_fprf_ptr;
199
#endif
200

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

    
224
struct opc_handler_t {
225
    /* invalid bits */
226
    uint32_t inval;
227
    /* instruction type */
228
    uint64_t type;
229
    /* handler */
230
    void (*handler)(DisasContext *ctx);
231
#if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
232
    const char *oname;
233
#endif
234
#if defined(DO_PPC_STATISTICS)
235
    uint64_t count;
236
#endif
237
};
238

    
239
static always_inline void gen_reset_fpstatus (void)
240
{
241
#ifdef CONFIG_SOFTFLOAT
242
    gen_op_reset_fpstatus();
243
#endif
244
}
245

    
246
static always_inline void gen_compute_fprf (TCGv_i64 arg, int set_fprf, int set_rc)
247
{
248
    TCGv_i32 t0 = tcg_temp_new_i32();
249

    
250
    if (set_fprf != 0) {
251
        /* This case might be optimized later */
252
#if defined(OPTIMIZE_FPRF_UPDATE)
253
        *gen_fprf_ptr++ = gen_opc_ptr;
254
#endif
255
        tcg_gen_movi_i32(t0, 1);
256
        gen_helper_compute_fprf(t0, arg, t0);
257
        if (unlikely(set_rc)) {
258
            tcg_gen_mov_i32(cpu_crf[1], t0);
259
        }
260
        gen_helper_float_check_status();
261
    } else if (unlikely(set_rc)) {
262
        /* We always need to compute fpcc */
263
        tcg_gen_movi_i32(t0, 0);
264
        gen_helper_compute_fprf(t0, arg, t0);
265
        tcg_gen_mov_i32(cpu_crf[1], t0);
266
        if (set_fprf)
267
            gen_helper_float_check_status();
268
    }
269

    
270
    tcg_temp_free_i32(t0);
271
}
272

    
273
static always_inline void gen_optimize_fprf (void)
274
{
275
#if defined(OPTIMIZE_FPRF_UPDATE)
276
    uint16_t **ptr;
277

    
278
    for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
279
        *ptr = INDEX_op_nop1;
280
    gen_fprf_ptr = gen_fprf_buf;
281
#endif
282
}
283

    
284
static always_inline void gen_set_access_type(int access_type)
285
{
286
    tcg_gen_movi_i32(cpu_access_type, access_type);
287
}
288

    
289
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
290
{
291
#if defined(TARGET_PPC64)
292
    if (ctx->sf_mode)
293
        tcg_gen_movi_tl(cpu_nip, nip);
294
    else
295
#endif
296
        tcg_gen_movi_tl(cpu_nip, (uint32_t)nip);
297
}
298

    
299
#define GEN_EXCP(ctx, excp, error)                                            \
300
do {                                                                          \
301
    TCGv_i32 t0 = tcg_const_i32(excp);                                        \
302
    TCGv_i32 t1 = tcg_const_i32(error);                                       \
303
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
304
        gen_update_nip(ctx, (ctx)->nip);                                      \
305
    }                                                                         \
306
    gen_helper_raise_exception_err(t0, t1);                                   \
307
    tcg_temp_free_i32(t0);                                                    \
308
    tcg_temp_free_i32(t1);                                                    \
309
    ctx->exception = (excp);                                                  \
310
} while (0)
311

    
312
#define GEN_EXCP_INVAL(ctx)                                                   \
313
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
314
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
315

    
316
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
317
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
318
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
319

    
320
#define GEN_EXCP_PRIVREG(ctx)                                                 \
321
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
322
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
323

    
324
#define GEN_EXCP_NO_FP(ctx)                                                   \
325
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
326

    
327
#define GEN_EXCP_NO_AP(ctx)                                                   \
328
GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
329

    
330
#define GEN_EXCP_NO_VR(ctx)                                                   \
331
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)
332

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
481
    return ret;
482
}
483

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

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

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

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

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

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

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

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

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

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

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

    
707
/* Start opcode list */
708
GEN_OPCODE_MARK(start);
709

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

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

    
722
/***                           Integer comparison                          ***/
723

    
724
static always_inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
725
{
726
    int l1, l2, l3;
727

    
728
    tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_xer);
729
    tcg_gen_shri_i32(cpu_crf[crf], cpu_crf[crf], XER_SO);
730
    tcg_gen_andi_i32(cpu_crf[crf], cpu_crf[crf], 1);
731

    
732
    l1 = gen_new_label();
733
    l2 = gen_new_label();
734
    l3 = gen_new_label();
735
    if (s) {
736
        tcg_gen_brcond_tl(TCG_COND_LT, arg0, arg1, l1);
737
        tcg_gen_brcond_tl(TCG_COND_GT, arg0, arg1, l2);
738
    } else {
739
        tcg_gen_brcond_tl(TCG_COND_LTU, arg0, arg1, l1);
740
        tcg_gen_brcond_tl(TCG_COND_GTU, arg0, arg1, l2);
741
    }
742
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_EQ);
743
    tcg_gen_br(l3);
744
    gen_set_label(l1);
745
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_LT);
746
    tcg_gen_br(l3);
747
    gen_set_label(l2);
748
    tcg_gen_ori_i32(cpu_crf[crf], cpu_crf[crf], 1 << CRF_GT);
749
    gen_set_label(l3);
750
}
751

    
752
static always_inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
753
{
754
    TCGv t0 = tcg_const_local_tl(arg1);
755
    gen_op_cmp(arg0, t0, s, crf);
756
    tcg_temp_free(t0);
757
}
758

    
759
#if defined(TARGET_PPC64)
760
static always_inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
761
{
762
    TCGv t0, t1;
763
    t0 = tcg_temp_local_new();
764
    t1 = tcg_temp_local_new();
765
    if (s) {
766
        tcg_gen_ext32s_tl(t0, arg0);
767
        tcg_gen_ext32s_tl(t1, arg1);
768
    } else {
769
        tcg_gen_ext32u_tl(t0, arg0);
770
        tcg_gen_ext32u_tl(t1, arg1);
771
    }
772
    gen_op_cmp(t0, t1, s, crf);
773
    tcg_temp_free(t1);
774
    tcg_temp_free(t0);
775
}
776

    
777
static always_inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
778
{
779
    TCGv t0 = tcg_const_local_tl(arg1);
780
    gen_op_cmp32(arg0, t0, s, crf);
781
    tcg_temp_free(t0);
782
}
783
#endif
784

    
785
static always_inline void gen_set_Rc0 (DisasContext *ctx, TCGv reg)
786
{
787
#if defined(TARGET_PPC64)
788
    if (!(ctx->sf_mode))
789
        gen_op_cmpi32(reg, 0, 1, 0);
790
    else
791
#endif
792
        gen_op_cmpi(reg, 0, 1, 0);
793
}
794

    
795
/* cmp */
796
GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER)
797
{
798
#if defined(TARGET_PPC64)
799
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
800
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
801
                     1, crfD(ctx->opcode));
802
    else
803
#endif
804
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
805
                   1, crfD(ctx->opcode));
806
}
807

    
808
/* cmpi */
809
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
810
{
811
#if defined(TARGET_PPC64)
812
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
813
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
814
                      1, crfD(ctx->opcode));
815
    else
816
#endif
817
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
818
                    1, crfD(ctx->opcode));
819
}
820

    
821
/* cmpl */
822
GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400000, PPC_INTEGER)
823
{
824
#if defined(TARGET_PPC64)
825
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
826
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
827
                     0, crfD(ctx->opcode));
828
    else
829
#endif
830
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
831
                   0, crfD(ctx->opcode));
832
}
833

    
834
/* cmpli */
835
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
836
{
837
#if defined(TARGET_PPC64)
838
    if (!(ctx->sf_mode && (ctx->opcode & 0x00200000)))
839
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
840
                      0, crfD(ctx->opcode));
841
    else
842
#endif
843
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
844
                    0, crfD(ctx->opcode));
845
}
846

    
847
/* isel (PowerPC 2.03 specification) */
848
GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL)
849
{
850
    int l1, l2;
851
    uint32_t bi = rC(ctx->opcode);
852
    uint32_t mask;
853
    TCGv_i32 t0;
854

    
855
    l1 = gen_new_label();
856
    l2 = gen_new_label();
857

    
858
    mask = 1 << (3 - (bi & 0x03));
859
    t0 = tcg_temp_new_i32();
860
    tcg_gen_andi_i32(t0, cpu_crf[bi >> 2], mask);
861
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
862
    if (rA(ctx->opcode) == 0)
863
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
864
    else
865
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
866
    tcg_gen_br(l2);
867
    gen_set_label(l1);
868
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
869
    gen_set_label(l2);
870
    tcg_temp_free_i32(t0);
871
}
872

    
873
/***                           Integer arithmetic                          ***/
874

    
875
static always_inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0, TCGv arg1, TCGv arg2, int sub)
876
{
877
    int l1;
878
    TCGv t0;
879

    
880
    l1 = gen_new_label();
881
    /* Start with XER OV disabled, the most likely case */
882
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
883
    t0 = tcg_temp_local_new();
884
    tcg_gen_xor_tl(t0, arg0, arg1);
885
#if defined(TARGET_PPC64)
886
    if (!ctx->sf_mode)
887
        tcg_gen_ext32s_tl(t0, t0);
888
#endif
889
    if (sub)
890
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
891
    else
892
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
893
    tcg_gen_xor_tl(t0, arg1, arg2);
894
#if defined(TARGET_PPC64)
895
    if (!ctx->sf_mode)
896
        tcg_gen_ext32s_tl(t0, t0);
897
#endif
898
    if (sub)
899
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
900
    else
901
        tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0, l1);
902
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
903
    gen_set_label(l1);
904
    tcg_temp_free(t0);
905
}
906

    
907
static always_inline void gen_op_arith_compute_ca(DisasContext *ctx, TCGv arg1, TCGv arg2, int sub)
908
{
909
    int l1 = gen_new_label();
910

    
911
#if defined(TARGET_PPC64)
912
    if (!(ctx->sf_mode)) {
913
        TCGv t0, t1;
914
        t0 = tcg_temp_new();
915
        t1 = tcg_temp_new();
916

    
917
        tcg_gen_ext32u_tl(t0, arg1);
918
        tcg_gen_ext32u_tl(t1, arg2);
919
        if (sub) {
920
            tcg_gen_brcond_tl(TCG_COND_GTU, t0, t1, l1);
921
        } else {
922
            tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
923
        }
924
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
925
        gen_set_label(l1);
926
        tcg_temp_free(t0);
927
        tcg_temp_free(t1);
928
    } else
929
#endif
930
    {
931
        if (sub) {
932
            tcg_gen_brcond_tl(TCG_COND_GTU, arg1, arg2, l1);
933
        } else {
934
            tcg_gen_brcond_tl(TCG_COND_GEU, arg1, arg2, l1);
935
        }
936
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
937
        gen_set_label(l1);
938
    }
939
}
940

    
941
/* Common add function */
942
static always_inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
943
                                           int add_ca, int compute_ca, int compute_ov)
944
{
945
    TCGv t0, t1;
946

    
947
    if ((!compute_ca && !compute_ov) ||
948
        (!TCGV_EQUAL(ret,arg1) && !TCGV_EQUAL(ret, arg2)))  {
949
        t0 = ret;
950
    } else {
951
        t0 = tcg_temp_local_new();
952
    }
953

    
954
    if (add_ca) {
955
        t1 = tcg_temp_local_new();
956
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
957
        tcg_gen_shri_tl(t1, t1, XER_CA);
958
    }
959

    
960
    if (compute_ca && compute_ov) {
961
        /* Start with XER CA and OV disabled, the most likely case */
962
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
963
    } else if (compute_ca) {
964
        /* Start with XER CA disabled, the most likely case */
965
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
966
    } else if (compute_ov) {
967
        /* Start with XER OV disabled, the most likely case */
968
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
969
    }
970

    
971
    tcg_gen_add_tl(t0, arg1, arg2);
972

    
973
    if (compute_ca) {
974
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
975
    }
976
    if (add_ca) {
977
        tcg_gen_add_tl(t0, t0, t1);
978
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
979
        tcg_temp_free(t1);
980
    }
981
    if (compute_ov) {
982
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
983
    }
984

    
985
    if (unlikely(Rc(ctx->opcode) != 0))
986
        gen_set_Rc0(ctx, t0);
987

    
988
    if (!TCGV_EQUAL(t0, ret)) {
989
        tcg_gen_mov_tl(ret, t0);
990
        tcg_temp_free(t0);
991
    }
992
}
993
/* Add functions with two operands */
994
#define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
995
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER)                  \
996
{                                                                             \
997
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
998
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
999
                     add_ca, compute_ca, compute_ov);                         \
1000
}
1001
/* Add functions with one operand and one immediate */
1002
#define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
1003
                                add_ca, compute_ca, compute_ov)               \
1004
GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER)                  \
1005
{                                                                             \
1006
    TCGv t0 = tcg_const_local_tl(const_val);                                  \
1007
    gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
1008
                     cpu_gpr[rA(ctx->opcode)], t0,                            \
1009
                     add_ca, compute_ca, compute_ov);                         \
1010
    tcg_temp_free(t0);                                                        \
1011
}
1012

    
1013
/* add  add.  addo  addo. */
1014
GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
1015
GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
1016
/* addc  addc.  addco  addco. */
1017
GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
1018
GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
1019
/* adde  adde.  addeo  addeo. */
1020
GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
1021
GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
1022
/* addme  addme.  addmeo  addmeo.  */
1023
GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
1024
GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
1025
/* addze  addze.  addzeo  addzeo.*/
1026
GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
1027
GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
1028
/* addi */
1029
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1030
{
1031
    target_long simm = SIMM(ctx->opcode);
1032

    
1033
    if (rA(ctx->opcode) == 0) {
1034
        /* li case */
1035
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
1036
    } else {
1037
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm);
1038
    }
1039
}
1040
/* addic  addic.*/
1041
static always_inline void gen_op_addic (DisasContext *ctx, TCGv ret, TCGv arg1,
1042
                                        int compute_Rc0)
1043
{
1044
    target_long simm = SIMM(ctx->opcode);
1045

    
1046
    /* Start with XER CA and OV disabled, the most likely case */
1047
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1048

    
1049
    if (likely(simm != 0)) {
1050
        TCGv t0 = tcg_temp_local_new();
1051
        tcg_gen_addi_tl(t0, arg1, simm);
1052
        gen_op_arith_compute_ca(ctx, t0, arg1, 0);
1053
        tcg_gen_mov_tl(ret, t0);
1054
        tcg_temp_free(t0);
1055
    } else {
1056
        tcg_gen_mov_tl(ret, arg1);
1057
    }
1058
    if (compute_Rc0) {
1059
        gen_set_Rc0(ctx, ret);
1060
    }
1061
}
1062
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1063
{
1064
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1065
}
1066
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1067
{
1068
    gen_op_addic(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1069
}
1070
/* addis */
1071
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1072
{
1073
    target_long simm = SIMM(ctx->opcode);
1074

    
1075
    if (rA(ctx->opcode) == 0) {
1076
        /* lis case */
1077
        tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
1078
    } else {
1079
        tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], simm << 16);
1080
    }
1081
}
1082

    
1083
static always_inline void gen_op_arith_divw (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1084
                                             int sign, int compute_ov)
1085
{
1086
    int l1 = gen_new_label();
1087
    int l2 = gen_new_label();
1088
    TCGv_i32 t0 = tcg_temp_local_new_i32();
1089
    TCGv_i32 t1 = tcg_temp_local_new_i32();
1090

    
1091
    tcg_gen_trunc_tl_i32(t0, arg1);
1092
    tcg_gen_trunc_tl_i32(t1, arg2);
1093
    tcg_gen_brcondi_i32(TCG_COND_EQ, t1, 0, l1);
1094
    if (sign) {
1095
        int l3 = gen_new_label();
1096
        tcg_gen_brcondi_i32(TCG_COND_NE, t1, -1, l3);
1097
        tcg_gen_brcondi_i32(TCG_COND_EQ, t0, INT32_MIN, l1);
1098
        gen_set_label(l3);
1099
        tcg_gen_div_i32(t0, t0, t1);
1100
    } else {
1101
        tcg_gen_divu_i32(t0, t0, t1);
1102
    }
1103
    if (compute_ov) {
1104
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1105
    }
1106
    tcg_gen_br(l2);
1107
    gen_set_label(l1);
1108
    if (sign) {
1109
        tcg_gen_sari_i32(t0, t0, 31);
1110
    } else {
1111
        tcg_gen_movi_i32(t0, 0);
1112
    }
1113
    if (compute_ov) {
1114
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1115
    }
1116
    gen_set_label(l2);
1117
    tcg_gen_extu_i32_tl(ret, t0);
1118
    tcg_temp_free_i32(t0);
1119
    tcg_temp_free_i32(t1);
1120
    if (unlikely(Rc(ctx->opcode) != 0))
1121
        gen_set_Rc0(ctx, ret);
1122
}
1123
/* Div functions */
1124
#define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
1125
GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)                  \
1126
{                                                                             \
1127
    gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1128
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
1129
                     sign, compute_ov);                                       \
1130
}
1131
/* divwu  divwu.  divwuo  divwuo.   */
1132
GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1133
GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1134
/* divw  divw.  divwo  divwo.   */
1135
GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1136
GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1137
#if defined(TARGET_PPC64)
1138
static always_inline void gen_op_arith_divd (DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1139
                                             int sign, int compute_ov)
1140
{
1141
    int l1 = gen_new_label();
1142
    int l2 = gen_new_label();
1143

    
1144
    tcg_gen_brcondi_i64(TCG_COND_EQ, arg2, 0, l1);
1145
    if (sign) {
1146
        int l3 = gen_new_label();
1147
        tcg_gen_brcondi_i64(TCG_COND_NE, arg2, -1, l3);
1148
        tcg_gen_brcondi_i64(TCG_COND_EQ, arg1, INT64_MIN, l1);
1149
        gen_set_label(l3);
1150
        tcg_gen_div_i64(ret, arg1, arg2);
1151
    } else {
1152
        tcg_gen_divu_i64(ret, arg1, arg2);
1153
    }
1154
    if (compute_ov) {
1155
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1156
    }
1157
    tcg_gen_br(l2);
1158
    gen_set_label(l1);
1159
    if (sign) {
1160
        tcg_gen_sari_i64(ret, arg1, 63);
1161
    } else {
1162
        tcg_gen_movi_i64(ret, 0);
1163
    }
1164
    if (compute_ov) {
1165
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1166
    }
1167
    gen_set_label(l2);
1168
    if (unlikely(Rc(ctx->opcode) != 0))
1169
        gen_set_Rc0(ctx, ret);
1170
}
1171
#define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1172
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
1173
{                                                                             \
1174
    gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1175
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1176
                      sign, compute_ov);                                      \
1177
}
1178
/* divwu  divwu.  divwuo  divwuo.   */
1179
GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1180
GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1181
/* divw  divw.  divwo  divwo.   */
1182
GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1183
GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1184
#endif
1185

    
1186
/* mulhw  mulhw. */
1187
GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER)
1188
{
1189
    TCGv_i64 t0, t1;
1190

    
1191
    t0 = tcg_temp_new_i64();
1192
    t1 = tcg_temp_new_i64();
1193
#if defined(TARGET_PPC64)
1194
    tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1195
    tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1196
    tcg_gen_mul_i64(t0, t0, t1);
1197
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1198
#else
1199
    tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1200
    tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1201
    tcg_gen_mul_i64(t0, t0, t1);
1202
    tcg_gen_shri_i64(t0, t0, 32);
1203
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1204
#endif
1205
    tcg_temp_free_i64(t0);
1206
    tcg_temp_free_i64(t1);
1207
    if (unlikely(Rc(ctx->opcode) != 0))
1208
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1209
}
1210
/* mulhwu  mulhwu.  */
1211
GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER)
1212
{
1213
    TCGv_i64 t0, t1;
1214

    
1215
    t0 = tcg_temp_new_i64();
1216
    t1 = tcg_temp_new_i64();
1217
#if defined(TARGET_PPC64)
1218
    tcg_gen_ext32u_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1219
    tcg_gen_ext32u_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1220
    tcg_gen_mul_i64(t0, t0, t1);
1221
    tcg_gen_shri_i64(cpu_gpr[rD(ctx->opcode)], t0, 32);
1222
#else
1223
    tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1224
    tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1225
    tcg_gen_mul_i64(t0, t0, t1);
1226
    tcg_gen_shri_i64(t0, t0, 32);
1227
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1228
#endif
1229
    tcg_temp_free_i64(t0);
1230
    tcg_temp_free_i64(t1);
1231
    if (unlikely(Rc(ctx->opcode) != 0))
1232
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1233
}
1234
/* mullw  mullw. */
1235
GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER)
1236
{
1237
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1238
                   cpu_gpr[rB(ctx->opcode)]);
1239
    tcg_gen_ext32s_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)]);
1240
    if (unlikely(Rc(ctx->opcode) != 0))
1241
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1242
}
1243
/* mullwo  mullwo. */
1244
GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER)
1245
{
1246
    int l1;
1247
    TCGv_i64 t0, t1;
1248

    
1249
    t0 = tcg_temp_new_i64();
1250
    t1 = tcg_temp_new_i64();
1251
    l1 = gen_new_label();
1252
    /* Start with XER OV disabled, the most likely case */
1253
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1254
#if defined(TARGET_PPC64)
1255
    tcg_gen_ext32s_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1256
    tcg_gen_ext32s_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1257
#else
1258
    tcg_gen_ext_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
1259
    tcg_gen_ext_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
1260
#endif
1261
    tcg_gen_mul_i64(t0, t0, t1);
1262
#if defined(TARGET_PPC64)
1263
    tcg_gen_ext32s_i64(cpu_gpr[rD(ctx->opcode)], t0);
1264
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, cpu_gpr[rD(ctx->opcode)], l1);
1265
#else
1266
    tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t0);
1267
    tcg_gen_ext32s_i64(t1, t0);
1268
    tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
1269
#endif
1270
    tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1271
    gen_set_label(l1);
1272
    tcg_temp_free_i64(t0);
1273
    tcg_temp_free_i64(t1);
1274
    if (unlikely(Rc(ctx->opcode) != 0))
1275
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1276
}
1277
/* mulli */
1278
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1279
{
1280
    tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1281
                    SIMM(ctx->opcode));
1282
}
1283
#if defined(TARGET_PPC64)
1284
#define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
1285
GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)                      \
1286
{                                                                             \
1287
    gen_helper_##name (cpu_gpr[rD(ctx->opcode)],                              \
1288
                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);   \
1289
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1290
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1291
}
1292
/* mulhd  mulhd. */
1293
GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00);
1294
/* mulhdu  mulhdu. */
1295
GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02);
1296
/* mulld  mulld. */
1297
GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B)
1298
{
1299
    tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1300
                   cpu_gpr[rB(ctx->opcode)]);
1301
    if (unlikely(Rc(ctx->opcode) != 0))
1302
        gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1303
}
1304
/* mulldo  mulldo. */
1305
GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17);
1306
#endif
1307

    
1308
/* neg neg. nego nego. */
1309
static always_inline void gen_op_arith_neg (DisasContext *ctx, TCGv ret, TCGv arg1, int ov_check)
1310
{
1311
    int l1 = gen_new_label();
1312
    int l2 = gen_new_label();
1313
    TCGv t0 = tcg_temp_local_new();
1314
#if defined(TARGET_PPC64)
1315
    if (ctx->sf_mode) {
1316
        tcg_gen_mov_tl(t0, arg1);
1317
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT64_MIN, l1);
1318
    } else
1319
#endif
1320
    {
1321
        tcg_gen_ext32s_tl(t0, arg1);
1322
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, INT32_MIN, l1);
1323
    }
1324
    tcg_gen_neg_tl(ret, arg1);
1325
    if (ov_check) {
1326
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1327
    }
1328
    tcg_gen_br(l2);
1329
    gen_set_label(l1);
1330
    tcg_gen_mov_tl(ret, t0);
1331
    if (ov_check) {
1332
        tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
1333
    }
1334
    gen_set_label(l2);
1335
    tcg_temp_free(t0);
1336
    if (unlikely(Rc(ctx->opcode) != 0))
1337
        gen_set_Rc0(ctx, ret);
1338
}
1339
GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER)
1340
{
1341
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0);
1342
}
1343
GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER)
1344
{
1345
    gen_op_arith_neg(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 1);
1346
}
1347

    
1348
/* Common subf function */
1349
static always_inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1, TCGv arg2,
1350
                                            int add_ca, int compute_ca, int compute_ov)
1351
{
1352
    TCGv t0, t1;
1353

    
1354
    if ((!compute_ca && !compute_ov) ||
1355
        (!TCGV_EQUAL(ret, arg1) && !TCGV_EQUAL(ret, arg2)))  {
1356
        t0 = ret;
1357
    } else {
1358
        t0 = tcg_temp_local_new();
1359
    }
1360

    
1361
    if (add_ca) {
1362
        t1 = tcg_temp_local_new();
1363
        tcg_gen_andi_tl(t1, cpu_xer, (1 << XER_CA));
1364
        tcg_gen_shri_tl(t1, t1, XER_CA);
1365
    }
1366

    
1367
    if (compute_ca && compute_ov) {
1368
        /* Start with XER CA and OV disabled, the most likely case */
1369
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~((1 << XER_CA) | (1 << XER_OV)));
1370
    } else if (compute_ca) {
1371
        /* Start with XER CA disabled, the most likely case */
1372
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1373
    } else if (compute_ov) {
1374
        /* Start with XER OV disabled, the most likely case */
1375
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
1376
    }
1377

    
1378
    if (add_ca) {
1379
        tcg_gen_not_tl(t0, arg1);
1380
        tcg_gen_add_tl(t0, t0, arg2);
1381
        gen_op_arith_compute_ca(ctx, t0, arg2, 0);
1382
        tcg_gen_add_tl(t0, t0, t1);
1383
        gen_op_arith_compute_ca(ctx, t0, t1, 0);
1384
        tcg_temp_free(t1);
1385
    } else {
1386
        tcg_gen_sub_tl(t0, arg2, arg1);
1387
        if (compute_ca) {
1388
            gen_op_arith_compute_ca(ctx, t0, arg2, 1);
1389
        }
1390
    }
1391
    if (compute_ov) {
1392
        gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1393
    }
1394

    
1395
    if (unlikely(Rc(ctx->opcode) != 0))
1396
        gen_set_Rc0(ctx, t0);
1397

    
1398
    if (!TCGV_EQUAL(t0, ret)) {
1399
        tcg_gen_mov_tl(ret, t0);
1400
        tcg_temp_free(t0);
1401
    }
1402
}
1403
/* Sub functions with Two operands functions */
1404
#define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1405
GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER)                  \
1406
{                                                                             \
1407
    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1408
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1409
                      add_ca, compute_ca, compute_ov);                        \
1410
}
1411
/* Sub functions with one operand and one immediate */
1412
#define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1413
                                add_ca, compute_ca, compute_ov)               \
1414
GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER)                  \
1415
{                                                                             \
1416
    TCGv t0 = tcg_const_local_tl(const_val);                                  \
1417
    gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1418
                      cpu_gpr[rA(ctx->opcode)], t0,                           \
1419
                      add_ca, compute_ca, compute_ov);                        \
1420
    tcg_temp_free(t0);                                                        \
1421
}
1422
/* subf  subf.  subfo  subfo. */
1423
GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1424
GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1425
/* subfc  subfc.  subfco  subfco. */
1426
GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1427
GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1428
/* subfe  subfe.  subfeo  subfo. */
1429
GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1430
GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1431
/* subfme  subfme.  subfmeo  subfmeo.  */
1432
GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1433
GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1434
/* subfze  subfze.  subfzeo  subfzeo.*/
1435
GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1436
GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1437
/* subfic */
1438
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1439
{
1440
    /* Start with XER CA and OV disabled, the most likely case */
1441
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1442
    TCGv t0 = tcg_temp_local_new();
1443
    TCGv t1 = tcg_const_local_tl(SIMM(ctx->opcode));
1444
    tcg_gen_sub_tl(t0, t1, cpu_gpr[rA(ctx->opcode)]);
1445
    gen_op_arith_compute_ca(ctx, t0, t1, 1);
1446
    tcg_temp_free(t1);
1447
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
1448
    tcg_temp_free(t0);
1449
}
1450

    
1451
/***                            Integer logical                            ***/
1452
#define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1453
GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)                          \
1454
{                                                                             \
1455
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1456
       cpu_gpr[rB(ctx->opcode)]);                                             \
1457
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1458
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1459
}
1460

    
1461
#define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1462
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
1463
{                                                                             \
1464
    tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1465
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1466
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1467
}
1468

    
1469
/* and & and. */
1470
GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1471
/* andc & andc. */
1472
GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1473
/* andi. */
1474
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1475
{
1476
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode));
1477
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1478
}
1479
/* andis. */
1480
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1481
{
1482
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], UIMM(ctx->opcode) << 16);
1483
    gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1484
}
1485
/* cntlzw */
1486
GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER)
1487
{
1488
    gen_helper_cntlzw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1489
    if (unlikely(Rc(ctx->opcode) != 0))
1490
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1491
}
1492
/* eqv & eqv. */
1493
GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1494
/* extsb & extsb. */
1495
GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1496
/* extsh & extsh. */
1497
GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1498
/* nand & nand. */
1499
GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1500
/* nor & nor. */
1501
GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1502
/* or & or. */
1503
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1504
{
1505
    int rs, ra, rb;
1506

    
1507
    rs = rS(ctx->opcode);
1508
    ra = rA(ctx->opcode);
1509
    rb = rB(ctx->opcode);
1510
    /* Optimisation for mr. ri case */
1511
    if (rs != ra || rs != rb) {
1512
        if (rs != rb)
1513
            tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1514
        else
1515
            tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1516
        if (unlikely(Rc(ctx->opcode) != 0))
1517
            gen_set_Rc0(ctx, cpu_gpr[ra]);
1518
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1519
        gen_set_Rc0(ctx, cpu_gpr[rs]);
1520
#if defined(TARGET_PPC64)
1521
    } else {
1522
        int prio = 0;
1523

    
1524
        switch (rs) {
1525
        case 1:
1526
            /* Set process priority to low */
1527
            prio = 2;
1528
            break;
1529
        case 6:
1530
            /* Set process priority to medium-low */
1531
            prio = 3;
1532
            break;
1533
        case 2:
1534
            /* Set process priority to normal */
1535
            prio = 4;
1536
            break;
1537
#if !defined(CONFIG_USER_ONLY)
1538
        case 31:
1539
            if (ctx->supervisor > 0) {
1540
                /* Set process priority to very low */
1541
                prio = 1;
1542
            }
1543
            break;
1544
        case 5:
1545
            if (ctx->supervisor > 0) {
1546
                /* Set process priority to medium-hight */
1547
                prio = 5;
1548
            }
1549
            break;
1550
        case 3:
1551
            if (ctx->supervisor > 0) {
1552
                /* Set process priority to high */
1553
                prio = 6;
1554
            }
1555
            break;
1556
        case 7:
1557
            if (ctx->supervisor > 1) {
1558
                /* Set process priority to very high */
1559
                prio = 7;
1560
            }
1561
            break;
1562
#endif
1563
        default:
1564
            /* nop */
1565
            break;
1566
        }
1567
        if (prio) {
1568
            TCGv t0 = tcg_temp_new();
1569
            tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
1570
            tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1571
            tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1572
            tcg_gen_st_tl(t0, cpu_env, offsetof(CPUState, spr[SPR_PPR]));
1573
            tcg_temp_free(t0);
1574
        }
1575
#endif
1576
    }
1577
}
1578
/* orc & orc. */
1579
GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1580
/* xor & xor. */
1581
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1582
{
1583
    /* Optimisation for "set to zero" case */
1584
    if (rS(ctx->opcode) != rB(ctx->opcode))
1585
        tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1586
    else
1587
        tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1588
    if (unlikely(Rc(ctx->opcode) != 0))
1589
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1590
}
1591
/* ori */
1592
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1593
{
1594
    target_ulong uimm = UIMM(ctx->opcode);
1595

    
1596
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1597
        /* NOP */
1598
        /* XXX: should handle special NOPs for POWER series */
1599
        return;
1600
    }
1601
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1602
}
1603
/* oris */
1604
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1605
{
1606
    target_ulong uimm = UIMM(ctx->opcode);
1607

    
1608
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1609
        /* NOP */
1610
        return;
1611
    }
1612
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1613
}
1614
/* xori */
1615
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1616
{
1617
    target_ulong uimm = UIMM(ctx->opcode);
1618

    
1619
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1620
        /* NOP */
1621
        return;
1622
    }
1623
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1624
}
1625
/* xoris */
1626
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1627
{
1628
    target_ulong uimm = UIMM(ctx->opcode);
1629

    
1630
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1631
        /* NOP */
1632
        return;
1633
    }
1634
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm << 16);
1635
}
1636
/* popcntb : PowerPC 2.03 specification */
1637
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_POPCNTB)
1638
{
1639
#if defined(TARGET_PPC64)
1640
    if (ctx->sf_mode)
1641
        gen_helper_popcntb_64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1642
    else
1643
#endif
1644
        gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1645
}
1646

    
1647
#if defined(TARGET_PPC64)
1648
/* extsw & extsw. */
1649
GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1650
/* cntlzd */
1651
GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B)
1652
{
1653
    gen_helper_cntlzd(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1654
    if (unlikely(Rc(ctx->opcode) != 0))
1655
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1656
}
1657
#endif
1658

    
1659
/***                             Integer rotate                            ***/
1660
/* rlwimi & rlwimi. */
1661
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1662
{
1663
    uint32_t mb, me, sh;
1664

    
1665
    mb = MB(ctx->opcode);
1666
    me = ME(ctx->opcode);
1667
    sh = SH(ctx->opcode);
1668
    if (likely(sh == 0 && mb == 0 && me == 31)) {
1669
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1670
    } else {
1671
        target_ulong mask;
1672
        TCGv t1;
1673
        TCGv t0 = tcg_temp_new();
1674
#if defined(TARGET_PPC64)
1675
        TCGv_i32 t2 = tcg_temp_new_i32();
1676
        tcg_gen_trunc_i64_i32(t2, cpu_gpr[rS(ctx->opcode)]);
1677
        tcg_gen_rotli_i32(t2, t2, sh);
1678
        tcg_gen_extu_i32_i64(t0, t2);
1679
        tcg_temp_free_i32(t2);
1680
#else
1681
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1682
#endif
1683
#if defined(TARGET_PPC64)
1684
        mb += 32;
1685
        me += 32;
1686
#endif
1687
        mask = MASK(mb, me);
1688
        t1 = tcg_temp_new();
1689
        tcg_gen_andi_tl(t0, t0, mask);
1690
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1691
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1692
        tcg_temp_free(t0);
1693
        tcg_temp_free(t1);
1694
    }
1695
    if (unlikely(Rc(ctx->opcode) != 0))
1696
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1697
}
1698
/* rlwinm & rlwinm. */
1699
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1700
{
1701
    uint32_t mb, me, sh;
1702

    
1703
    sh = SH(ctx->opcode);
1704
    mb = MB(ctx->opcode);
1705
    me = ME(ctx->opcode);
1706

    
1707
    if (likely(mb == 0 && me == (31 - sh))) {
1708
        if (likely(sh == 0)) {
1709
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1710
        } else {
1711
            TCGv t0 = tcg_temp_new();
1712
            tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1713
            tcg_gen_shli_tl(t0, t0, sh);
1714
            tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1715
            tcg_temp_free(t0);
1716
        }
1717
    } else if (likely(sh != 0 && me == 31 && sh == (32 - mb))) {
1718
        TCGv t0 = tcg_temp_new();
1719
        tcg_gen_ext32u_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1720
        tcg_gen_shri_tl(t0, t0, mb);
1721
        tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], t0);
1722
        tcg_temp_free(t0);
1723
    } else {
1724
        TCGv t0 = tcg_temp_new();
1725
#if defined(TARGET_PPC64)
1726
        TCGv_i32 t1 = tcg_temp_new_i32();
1727
        tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1728
        tcg_gen_rotli_i32(t1, t1, sh);
1729
        tcg_gen_extu_i32_i64(t0, t1);
1730
        tcg_temp_free_i32(t1);
1731
#else
1732
        tcg_gen_rotli_i32(t0, cpu_gpr[rS(ctx->opcode)], sh);
1733
#endif
1734
#if defined(TARGET_PPC64)
1735
        mb += 32;
1736
        me += 32;
1737
#endif
1738
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1739
        tcg_temp_free(t0);
1740
    }
1741
    if (unlikely(Rc(ctx->opcode) != 0))
1742
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1743
}
1744
/* rlwnm & rlwnm. */
1745
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1746
{
1747
    uint32_t mb, me;
1748
    TCGv t0;
1749
#if defined(TARGET_PPC64)
1750
    TCGv_i32 t1, t2;
1751
#endif
1752

    
1753
    mb = MB(ctx->opcode);
1754
    me = ME(ctx->opcode);
1755
    t0 = tcg_temp_new();
1756
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1f);
1757
#if defined(TARGET_PPC64)
1758
    t1 = tcg_temp_new_i32();
1759
    t2 = tcg_temp_new_i32();
1760
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rS(ctx->opcode)]);
1761
    tcg_gen_trunc_i64_i32(t2, t0);
1762
    tcg_gen_rotl_i32(t1, t1, t2);
1763
    tcg_gen_extu_i32_i64(t0, t1);
1764
    tcg_temp_free_i32(t1);
1765
    tcg_temp_free_i32(t2);
1766
#else
1767
    tcg_gen_rotl_i32(t0, cpu_gpr[rS(ctx->opcode)], t0);
1768
#endif
1769
    if (unlikely(mb != 0 || me != 31)) {
1770
#if defined(TARGET_PPC64)
1771
        mb += 32;
1772
        me += 32;
1773
#endif
1774
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1775
    } else {
1776
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1777
    }
1778
    tcg_temp_free(t0);
1779
    if (unlikely(Rc(ctx->opcode) != 0))
1780
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1781
}
1782

    
1783
#if defined(TARGET_PPC64)
1784
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1785
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1786
{                                                                             \
1787
    gen_##name(ctx, 0);                                                       \
1788
}                                                                             \
1789
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1790
             PPC_64B)                                                         \
1791
{                                                                             \
1792
    gen_##name(ctx, 1);                                                       \
1793
}
1794
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1795
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1796
{                                                                             \
1797
    gen_##name(ctx, 0, 0);                                                    \
1798
}                                                                             \
1799
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
1800
             PPC_64B)                                                         \
1801
{                                                                             \
1802
    gen_##name(ctx, 0, 1);                                                    \
1803
}                                                                             \
1804
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1805
             PPC_64B)                                                         \
1806
{                                                                             \
1807
    gen_##name(ctx, 1, 0);                                                    \
1808
}                                                                             \
1809
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
1810
             PPC_64B)                                                         \
1811
{                                                                             \
1812
    gen_##name(ctx, 1, 1);                                                    \
1813
}
1814

    
1815
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1816
                                      uint32_t me, uint32_t sh)
1817
{
1818
    if (likely(sh != 0 && mb == 0 && me == (63 - sh))) {
1819
        tcg_gen_shli_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
1820
    } else if (likely(sh != 0 && me == 63 && sh == (64 - mb))) {
1821
        tcg_gen_shri_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], mb);
1822
    } else {
1823
        TCGv t0 = tcg_temp_new();
1824
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1825
        if (likely(mb == 0 && me == 63)) {
1826
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1827
        } else {
1828
            tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1829
        }
1830
        tcg_temp_free(t0);
1831
    }
1832
    if (unlikely(Rc(ctx->opcode) != 0))
1833
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1834
}
1835
/* rldicl - rldicl. */
1836
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1837
{
1838
    uint32_t sh, mb;
1839

    
1840
    sh = SH(ctx->opcode) | (shn << 5);
1841
    mb = MB(ctx->opcode) | (mbn << 5);
1842
    gen_rldinm(ctx, mb, 63, sh);
1843
}
1844
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1845
/* rldicr - rldicr. */
1846
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1847
{
1848
    uint32_t sh, me;
1849

    
1850
    sh = SH(ctx->opcode) | (shn << 5);
1851
    me = MB(ctx->opcode) | (men << 5);
1852
    gen_rldinm(ctx, 0, me, sh);
1853
}
1854
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1855
/* rldic - rldic. */
1856
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1857
{
1858
    uint32_t sh, mb;
1859

    
1860
    sh = SH(ctx->opcode) | (shn << 5);
1861
    mb = MB(ctx->opcode) | (mbn << 5);
1862
    gen_rldinm(ctx, mb, 63 - sh, sh);
1863
}
1864
GEN_PPC64_R4(rldic, 0x1E, 0x04);
1865

    
1866
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1867
                                     uint32_t me)
1868
{
1869
    TCGv t0;
1870

    
1871
    mb = MB(ctx->opcode);
1872
    me = ME(ctx->opcode);
1873
    t0 = tcg_temp_new();
1874
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1875
    tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
1876
    if (unlikely(mb != 0 || me != 63)) {
1877
        tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], t0, MASK(mb, me));
1878
    } else {
1879
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
1880
    }
1881
    tcg_temp_free(t0);
1882
    if (unlikely(Rc(ctx->opcode) != 0))
1883
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1884
}
1885

    
1886
/* rldcl - rldcl. */
1887
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1888
{
1889
    uint32_t mb;
1890

    
1891
    mb = MB(ctx->opcode) | (mbn << 5);
1892
    gen_rldnm(ctx, mb, 63);
1893
}
1894
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1895
/* rldcr - rldcr. */
1896
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1897
{
1898
    uint32_t me;
1899

    
1900
    me = MB(ctx->opcode) | (men << 5);
1901
    gen_rldnm(ctx, 0, me);
1902
}
1903
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1904
/* rldimi - rldimi. */
1905
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1906
{
1907
    uint32_t sh, mb, me;
1908

    
1909
    sh = SH(ctx->opcode) | (shn << 5);
1910
    mb = MB(ctx->opcode) | (mbn << 5);
1911
    me = 63 - sh;
1912
    if (unlikely(sh == 0 && mb == 0)) {
1913
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1914
    } else {
1915
        TCGv t0, t1;
1916
        target_ulong mask;
1917

    
1918
        t0 = tcg_temp_new();
1919
        tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
1920
        t1 = tcg_temp_new();
1921
        mask = MASK(mb, me);
1922
        tcg_gen_andi_tl(t0, t0, mask);
1923
        tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], ~mask);
1924
        tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
1925
        tcg_temp_free(t0);
1926
        tcg_temp_free(t1);
1927
    }
1928
    if (unlikely(Rc(ctx->opcode) != 0))
1929
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1930
}
1931
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1932
#endif
1933

    
1934
/***                             Integer shift                             ***/
1935
/* slw & slw. */
1936
GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER)
1937
{
1938
    TCGv t0;
1939
    int l1, l2;
1940
    l1 = gen_new_label();
1941
    l2 = gen_new_label();
1942

    
1943
    t0 = tcg_temp_local_new();
1944
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
1945
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
1946
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1947
    tcg_gen_br(l2);
1948
    gen_set_label(l1);
1949
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
1950
    tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1951
    gen_set_label(l2);
1952
    tcg_temp_free(t0);
1953
    if (unlikely(Rc(ctx->opcode) != 0))
1954
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1955
}
1956
/* sraw & sraw. */
1957
GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER)
1958
{
1959
    gen_helper_sraw(cpu_gpr[rA(ctx->opcode)],
1960
                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1961
    if (unlikely(Rc(ctx->opcode) != 0))
1962
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1963
}
1964
/* srawi & srawi. */
1965
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1966
{
1967
    int sh = SH(ctx->opcode);
1968
    if (sh != 0) {
1969
        int l1, l2;
1970
        TCGv t0;
1971
        l1 = gen_new_label();
1972
        l2 = gen_new_label();
1973
        t0 = tcg_temp_local_new();
1974
        tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1975
        tcg_gen_brcondi_tl(TCG_COND_GE, t0, 0, l1);
1976
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
1977
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
1978
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
1979
        tcg_gen_br(l2);
1980
        gen_set_label(l1);
1981
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1982
        gen_set_label(l2);
1983
        tcg_gen_ext32s_tl(t0, cpu_gpr[rS(ctx->opcode)]);
1984
        tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], t0, sh);
1985
        tcg_temp_free(t0);
1986
    } else {
1987
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1988
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
1989
    }
1990
    if (unlikely(Rc(ctx->opcode) != 0))
1991
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1992
}
1993
/* srw & srw. */
1994
GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER)
1995
{
1996
    TCGv t0, t1;
1997
    int l1, l2;
1998
    l1 = gen_new_label();
1999
    l2 = gen_new_label();
2000

    
2001
    t0 = tcg_temp_local_new();
2002
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
2003
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x20, l1);
2004
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2005
    tcg_gen_br(l2);
2006
    gen_set_label(l1);
2007
    t1 = tcg_temp_new();
2008
    tcg_gen_ext32u_tl(t1, cpu_gpr[rS(ctx->opcode)]);
2009
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t1, t0);
2010
    tcg_temp_free(t1);
2011
    gen_set_label(l2);
2012
    tcg_temp_free(t0);
2013
    if (unlikely(Rc(ctx->opcode) != 0))
2014
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2015
}
2016
#if defined(TARGET_PPC64)
2017
/* sld & sld. */
2018
GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B)
2019
{
2020
    TCGv t0;
2021
    int l1, l2;
2022
    l1 = gen_new_label();
2023
    l2 = gen_new_label();
2024

    
2025
    t0 = tcg_temp_local_new();
2026
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2027
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2028
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2029
    tcg_gen_br(l2);
2030
    gen_set_label(l1);
2031
    tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2032
    gen_set_label(l2);
2033
    tcg_temp_free(t0);
2034
    if (unlikely(Rc(ctx->opcode) != 0))
2035
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2036
}
2037
/* srad & srad. */
2038
GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B)
2039
{
2040
    gen_helper_srad(cpu_gpr[rA(ctx->opcode)],
2041
                    cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2042
    if (unlikely(Rc(ctx->opcode) != 0))
2043
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2044
}
2045
/* sradi & sradi. */
2046
static always_inline void gen_sradi (DisasContext *ctx, int n)
2047
{
2048
    int sh = SH(ctx->opcode) + (n << 5);
2049
    if (sh != 0) {
2050
        int l1, l2;
2051
        TCGv t0;
2052
        l1 = gen_new_label();
2053
        l2 = gen_new_label();
2054
        t0 = tcg_temp_local_new();
2055
        tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
2056
        tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], (1ULL << sh) - 1);
2057
        tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
2058
        tcg_gen_ori_tl(cpu_xer, cpu_xer, 1 << XER_CA);
2059
        tcg_gen_br(l2);
2060
        gen_set_label(l1);
2061
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2062
        gen_set_label(l2);
2063
        tcg_temp_free(t0);
2064
        tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
2065
    } else {
2066
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2067
        tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_CA));
2068
    }
2069
    if (unlikely(Rc(ctx->opcode) != 0))
2070
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2071
}
2072
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
2073
{
2074
    gen_sradi(ctx, 0);
2075
}
2076
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
2077
{
2078
    gen_sradi(ctx, 1);
2079
}
2080
/* srd & srd. */
2081
GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B)
2082
{
2083
    TCGv t0;
2084
    int l1, l2;
2085
    l1 = gen_new_label();
2086
    l2 = gen_new_label();
2087

    
2088
    t0 = tcg_temp_local_new();
2089
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x7f);
2090
    tcg_gen_brcondi_tl(TCG_COND_LT, t0, 0x40, l1);
2091
    tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2092
    tcg_gen_br(l2);
2093
    gen_set_label(l1);
2094
    tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t0);
2095
    gen_set_label(l2);
2096
    tcg_temp_free(t0);
2097
    if (unlikely(Rc(ctx->opcode) != 0))
2098
        gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2099
}
2100
#endif
2101

    
2102
/***                       Floating-Point arithmetic                       ***/
2103
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
2104
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
2105
{                                                                             \
2106
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2107
        GEN_EXCP_NO_FP(ctx);                                                  \
2108
        return;                                                               \
2109
    }                                                                         \
2110
    gen_reset_fpstatus();                                                     \
2111
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2112
                     cpu_fpr[rC(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);     \
2113
    if (isfloat) {                                                            \
2114
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2115
    }                                                                         \
2116
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], set_fprf,                      \
2117
                     Rc(ctx->opcode) != 0);                                   \
2118
}
2119

    
2120
#define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
2121
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
2122
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
2123

    
2124
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2125
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2126
{                                                                             \
2127
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2128
        GEN_EXCP_NO_FP(ctx);                                                  \
2129
        return;                                                               \
2130
    }                                                                         \
2131
    gen_reset_fpstatus();                                                     \
2132
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2133
                     cpu_fpr[rB(ctx->opcode)]);                               \
2134
    if (isfloat) {                                                            \
2135
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2136
    }                                                                         \
2137
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2138
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2139
}
2140
#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
2141
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2142
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2143

    
2144
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
2145
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
2146
{                                                                             \
2147
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2148
        GEN_EXCP_NO_FP(ctx);                                                  \
2149
        return;                                                               \
2150
    }                                                                         \
2151
    gen_reset_fpstatus();                                                     \
2152
    gen_helper_f##op(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rA(ctx->opcode)],      \
2153
                       cpu_fpr[rC(ctx->opcode)]);                             \
2154
    if (isfloat) {                                                            \
2155
        gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);  \
2156
    }                                                                         \
2157
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2158
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2159
}
2160
#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
2161
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
2162
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
2163

    
2164
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
2165
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
2166
{                                                                             \
2167
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2168
        GEN_EXCP_NO_FP(ctx);                                                  \
2169
        return;                                                               \
2170
    }                                                                         \
2171
    gen_reset_fpstatus();                                                     \
2172
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2173
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2174
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2175
}
2176

    
2177
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
2178
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
2179
{                                                                             \
2180
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2181
        GEN_EXCP_NO_FP(ctx);                                                  \
2182
        return;                                                               \
2183
    }                                                                         \
2184
    gen_reset_fpstatus();                                                     \
2185
    gen_helper_f##name(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);   \
2186
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)],                                \
2187
                     set_fprf, Rc(ctx->opcode) != 0);                         \
2188
}
2189

    
2190
/* fadd - fadds */
2191
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
2192
/* fdiv - fdivs */
2193
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
2194
/* fmul - fmuls */
2195
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
2196

    
2197
/* fre */
2198
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
2199

    
2200
/* fres */
2201
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
2202

    
2203
/* frsqrte */
2204
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
2205

    
2206
/* frsqrtes */
2207
GEN_HANDLER(frsqrtes, 0x3B, 0x1A, 0xFF, 0x001F07C0, PPC_FLOAT_FRSQRTES)
2208
{
2209
    if (unlikely(!ctx->fpu_enabled)) {
2210
        GEN_EXCP_NO_FP(ctx);
2211
        return;
2212
    }
2213
    gen_reset_fpstatus();
2214
    gen_helper_frsqrte(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2215
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2216
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2217
}
2218

    
2219
/* fsel */
2220
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
2221
/* fsub - fsubs */
2222
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
2223
/* Optional: */
2224
/* fsqrt */
2225
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2226
{
2227
    if (unlikely(!ctx->fpu_enabled)) {
2228
        GEN_EXCP_NO_FP(ctx);
2229
        return;
2230
    }
2231
    gen_reset_fpstatus();
2232
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2233
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2234
}
2235

    
2236
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
2237
{
2238
    if (unlikely(!ctx->fpu_enabled)) {
2239
        GEN_EXCP_NO_FP(ctx);
2240
        return;
2241
    }
2242
    gen_reset_fpstatus();
2243
    gen_helper_fsqrt(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2244
    gen_helper_frsp(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rD(ctx->opcode)]);
2245
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 1, Rc(ctx->opcode) != 0);
2246
}
2247

    
2248
/***                     Floating-Point multiply-and-add                   ***/
2249
/* fmadd - fmadds */
2250
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
2251
/* fmsub - fmsubs */
2252
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
2253
/* fnmadd - fnmadds */
2254
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
2255
/* fnmsub - fnmsubs */
2256
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
2257

    
2258
/***                     Floating-Point round & convert                    ***/
2259
/* fctiw */
2260
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
2261
/* fctiwz */
2262
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
2263
/* frsp */
2264
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
2265
#if defined(TARGET_PPC64)
2266
/* fcfid */
2267
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
2268
/* fctid */
2269
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
2270
/* fctidz */
2271
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
2272
#endif
2273

    
2274
/* frin */
2275
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
2276
/* friz */
2277
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
2278
/* frip */
2279
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
2280
/* frim */
2281
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
2282

    
2283
/***                         Floating-Point compare                        ***/
2284
/* fcmpo */
2285
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
2286
{
2287
    if (unlikely(!ctx->fpu_enabled)) {
2288
        GEN_EXCP_NO_FP(ctx);
2289
        return;
2290
    }
2291
    gen_reset_fpstatus();
2292
    gen_helper_fcmpo(cpu_crf[crfD(ctx->opcode)],
2293
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2294
    gen_helper_float_check_status();
2295
}
2296

    
2297
/* fcmpu */
2298
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
2299
{
2300
    if (unlikely(!ctx->fpu_enabled)) {
2301
        GEN_EXCP_NO_FP(ctx);
2302
        return;
2303
    }
2304
    gen_reset_fpstatus();
2305
    gen_helper_fcmpu(cpu_crf[crfD(ctx->opcode)],
2306
                     cpu_fpr[rA(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2307
    gen_helper_float_check_status();
2308
}
2309

    
2310
/***                         Floating-point move                           ***/
2311
/* fabs */
2312
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
2313
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
2314

    
2315
/* fmr  - fmr. */
2316
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
2317
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
2318
{
2319
    if (unlikely(!ctx->fpu_enabled)) {
2320
        GEN_EXCP_NO_FP(ctx);
2321
        return;
2322
    }
2323
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpr[rB(ctx->opcode)]);
2324
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2325
}
2326

    
2327
/* fnabs */
2328
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
2329
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
2330
/* fneg */
2331
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
2332
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
2333

    
2334
/***                  Floating-Point status & ctrl register                ***/
2335
/* mcrfs */
2336
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
2337
{
2338
    int bfa;
2339

    
2340
    if (unlikely(!ctx->fpu_enabled)) {
2341
        GEN_EXCP_NO_FP(ctx);
2342
        return;
2343
    }
2344
    gen_optimize_fprf();
2345
    bfa = 4 * (7 - crfS(ctx->opcode));
2346
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_fpscr, bfa);
2347
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], 0xf);
2348
    tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(0xF << bfa));
2349
}
2350

    
2351
/* mffs */
2352
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
2353
{
2354
    if (unlikely(!ctx->fpu_enabled)) {
2355
        GEN_EXCP_NO_FP(ctx);
2356
        return;
2357
    }
2358
    gen_optimize_fprf();
2359
    gen_reset_fpstatus();
2360
    tcg_gen_extu_i32_i64(cpu_fpr[rD(ctx->opcode)], cpu_fpscr);
2361
    gen_compute_fprf(cpu_fpr[rD(ctx->opcode)], 0, Rc(ctx->opcode) != 0);
2362
}
2363

    
2364
/* mtfsb0 */
2365
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
2366
{
2367
    uint8_t crb;
2368

    
2369
    if (unlikely(!ctx->fpu_enabled)) {
2370
        GEN_EXCP_NO_FP(ctx);
2371
        return;
2372
    }
2373
    crb = 32 - (crbD(ctx->opcode) >> 2);
2374
    gen_optimize_fprf();
2375
    gen_reset_fpstatus();
2376
    if (likely(crb != 30 && crb != 29))
2377
        tcg_gen_andi_i32(cpu_fpscr, cpu_fpscr, ~(1 << crb));
2378
    if (unlikely(Rc(ctx->opcode) != 0)) {
2379
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2380
    }
2381
}
2382

    
2383
/* mtfsb1 */
2384
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
2385
{
2386
    uint8_t crb;
2387

    
2388
    if (unlikely(!ctx->fpu_enabled)) {
2389
        GEN_EXCP_NO_FP(ctx);
2390
        return;
2391
    }
2392
    crb = 32 - (crbD(ctx->opcode) >> 2);
2393
    gen_optimize_fprf();
2394
    gen_reset_fpstatus();
2395
    /* XXX: we pretend we can only do IEEE floating-point computations */
2396
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI)) {
2397
        TCGv_i32 t0 = tcg_const_i32(crb);
2398
        gen_helper_fpscr_setbit(t0);
2399
        tcg_temp_free_i32(t0);
2400
    }
2401
    if (unlikely(Rc(ctx->opcode) != 0)) {
2402
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2403
    }
2404
    /* We can raise a differed exception */
2405
    gen_helper_float_check_status();
2406
}
2407

    
2408
/* mtfsf */
2409
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2410
{
2411
    TCGv_i32 t0;
2412

    
2413
    if (unlikely(!ctx->fpu_enabled)) {
2414
        GEN_EXCP_NO_FP(ctx);
2415
        return;
2416
    }
2417
    gen_optimize_fprf();
2418
    gen_reset_fpstatus();
2419
    t0 = tcg_const_i32(FM(ctx->opcode));
2420
    gen_helper_store_fpscr(cpu_fpr[rB(ctx->opcode)], t0);
2421
    tcg_temp_free_i32(t0);
2422
    if (unlikely(Rc(ctx->opcode) != 0)) {
2423
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2424
    }
2425
    /* We can raise a differed exception */
2426
    gen_helper_float_check_status();
2427
}
2428

    
2429
/* mtfsfi */
2430
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2431
{
2432
    int bf, sh;
2433
    TCGv_i64 t0;
2434
    TCGv_i32 t1;
2435

    
2436
    if (unlikely(!ctx->fpu_enabled)) {
2437
        GEN_EXCP_NO_FP(ctx);
2438
        return;
2439
    }
2440
    bf = crbD(ctx->opcode) >> 2;
2441
    sh = 7 - bf;
2442
    gen_optimize_fprf();
2443
    gen_reset_fpstatus();
2444
    t0 = tcg_const_i64(FPIMM(ctx->opcode) << (4 * sh));
2445
    t1 = tcg_const_i32(1 << sh);
2446
    gen_helper_store_fpscr(t0, t1);
2447
    tcg_temp_free_i64(t0);
2448
    tcg_temp_free_i32(t1);
2449
    if (unlikely(Rc(ctx->opcode) != 0)) {
2450
        tcg_gen_shri_i32(cpu_crf[1], cpu_fpscr, FPSCR_OX);
2451
    }
2452
    /* We can raise a differed exception */
2453
    gen_helper_float_check_status();
2454
}
2455

    
2456
/***                           Addressing modes                            ***/
2457
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2458
static always_inline void gen_addr_imm_index (TCGv EA,
2459
                                              DisasContext *ctx,
2460
                                              target_long maskl)
2461
{
2462
    target_long simm = SIMM(ctx->opcode);
2463

    
2464
    simm &= ~maskl;
2465
    if (rA(ctx->opcode) == 0)
2466
        tcg_gen_movi_tl(EA, simm);
2467
    else if (likely(simm != 0))
2468
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2469
    else
2470
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2471
}
2472

    
2473
static always_inline void gen_addr_reg_index (TCGv EA,
2474
                                              DisasContext *ctx)
2475
{
2476
    if (rA(ctx->opcode) == 0)
2477
        tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2478
    else
2479
        tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2480
}
2481

    
2482
static always_inline void gen_addr_register (TCGv EA,
2483
                                             DisasContext *ctx)
2484
{
2485
    if (rA(ctx->opcode) == 0)
2486
        tcg_gen_movi_tl(EA, 0);
2487
    else
2488
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2489
}
2490

    
2491
#if defined(TARGET_PPC64)
2492
#define _GEN_MEM_FUNCS(name, mode)                                            \
2493
    &gen_op_##name##_##mode,                                                  \
2494
    &gen_op_##name##_le_##mode,                                               \
2495
    &gen_op_##name##_64_##mode,                                               \
2496
    &gen_op_##name##_le_64_##mode
2497
#else
2498
#define _GEN_MEM_FUNCS(name, mode)                                            \
2499
    &gen_op_##name##_##mode,                                                  \
2500
    &gen_op_##name##_le_##mode
2501
#endif
2502
#if defined(CONFIG_USER_ONLY)
2503
#if defined(TARGET_PPC64)
2504
#define NB_MEM_FUNCS 4
2505
#else
2506
#define NB_MEM_FUNCS 2
2507
#endif
2508
#define GEN_MEM_FUNCS(name)                                                   \
2509
    _GEN_MEM_FUNCS(name, raw)
2510
#else
2511
#if defined(TARGET_PPC64)
2512
#define NB_MEM_FUNCS 12
2513
#else
2514
#define NB_MEM_FUNCS 6
2515
#endif
2516
#define GEN_MEM_FUNCS(name)                                                   \
2517
    _GEN_MEM_FUNCS(name, user),                                               \
2518
    _GEN_MEM_FUNCS(name, kernel),                                             \
2519
    _GEN_MEM_FUNCS(name, hypv)
2520
#endif
2521

    
2522
/***                             Integer load                              ***/
2523
#if defined(TARGET_PPC64)
2524
#define GEN_QEMU_LD_PPC64(width)                                                 \
2525
static always_inline void gen_qemu_ld##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2526
{                                                                                \
2527
    if (likely(flags & 2))                                                       \
2528
        tcg_gen_qemu_ld##width(t0, t1, flags >> 2);                              \
2529
    else {                                                                       \
2530
        TCGv addr = tcg_temp_new();                                   \
2531
        tcg_gen_ext32u_tl(addr, t1);                                             \
2532
        tcg_gen_qemu_ld##width(t0, addr, flags >> 2);                            \
2533
        tcg_temp_free(addr);                                                     \
2534
    }                                                                            \
2535
}
2536
GEN_QEMU_LD_PPC64(8u)
2537
GEN_QEMU_LD_PPC64(8s)
2538
GEN_QEMU_LD_PPC64(16u)
2539
GEN_QEMU_LD_PPC64(16s)
2540
GEN_QEMU_LD_PPC64(32u)
2541
GEN_QEMU_LD_PPC64(32s)
2542
GEN_QEMU_LD_PPC64(64)
2543

    
2544
#define GEN_QEMU_ST_PPC64(width)                                                 \
2545
static always_inline void gen_qemu_st##width##_ppc64(TCGv t0, TCGv t1, int flags)\
2546
{                                                                                \
2547
    if (likely(flags & 2))                                                       \
2548
        tcg_gen_qemu_st##width(t0, t1, flags >> 2);                              \
2549
    else {                                                                       \
2550
        TCGv addr = tcg_temp_new();                                   \
2551
        tcg_gen_ext32u_tl(addr, t1);                                             \
2552
        tcg_gen_qemu_st##width(t0, addr, flags >> 2);                            \
2553
        tcg_temp_free(addr);                                                     \
2554
    }                                                                            \
2555
}
2556
GEN_QEMU_ST_PPC64(8)
2557
GEN_QEMU_ST_PPC64(16)
2558
GEN_QEMU_ST_PPC64(32)
2559
GEN_QEMU_ST_PPC64(64)
2560

    
2561
static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
2562
{
2563
    gen_qemu_ld8u_ppc64(arg0, arg1, flags);
2564
}
2565

    
2566
static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
2567
{
2568
    gen_qemu_ld8s_ppc64(arg0, arg1, flags);
2569
}
2570

    
2571
static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
2572
{
2573
    if (unlikely(flags & 1)) {
2574
        TCGv_i32 t0;
2575
        gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2576
        t0 = tcg_temp_new_i32();
2577
        tcg_gen_trunc_tl_i32(t0, arg0);
2578
        tcg_gen_bswap16_i32(t0, t0);
2579
        tcg_gen_extu_i32_tl(arg0, t0);
2580
        tcg_temp_free_i32(t0);
2581
    } else
2582
        gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2583
}
2584

    
2585
static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
2586
{
2587
    if (unlikely(flags & 1)) {
2588
        TCGv_i32 t0;
2589
        gen_qemu_ld16u_ppc64(arg0, arg1, flags);
2590
        t0 = tcg_temp_new_i32();
2591
        tcg_gen_trunc_tl_i32(t0, arg0);
2592
        tcg_gen_bswap16_i32(t0, t0);
2593
        tcg_gen_extu_i32_tl(arg0, t0);
2594
        tcg_gen_ext16s_tl(arg0, arg0);
2595
        tcg_temp_free_i32(t0);
2596
    } else
2597
        gen_qemu_ld16s_ppc64(arg0, arg1, flags);
2598
}
2599

    
2600
static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
2601
{
2602
    if (unlikely(flags & 1)) {
2603
        TCGv_i32 t0;
2604
        gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2605
        t0 = tcg_temp_new_i32();
2606
        tcg_gen_trunc_tl_i32(t0, arg0);
2607
        tcg_gen_bswap_i32(t0, t0);
2608
        tcg_gen_extu_i32_tl(arg0, t0);
2609
        tcg_temp_free_i32(t0);
2610
    } else
2611
        gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2612
}
2613

    
2614
static always_inline void gen_qemu_ld32s(TCGv arg0, TCGv arg1, int flags)
2615
{
2616
    if (unlikely(flags & 1)) {
2617
        TCGv_i32 t0;
2618
        gen_qemu_ld32u_ppc64(arg0, arg1, flags);
2619
        t0 = tcg_temp_new_i32();
2620
        tcg_gen_trunc_tl_i32(t0, arg0);
2621
        tcg_gen_bswap_i32(t0, t0);
2622
        tcg_gen_ext_i32_tl(arg0, t0);
2623
        tcg_temp_free_i32(t0);
2624
    } else
2625
        gen_qemu_ld32s_ppc64(arg0, arg1, flags);
2626
}
2627

    
2628
static always_inline void gen_qemu_ld64(TCGv arg0, TCGv arg1, int flags)
2629
{
2630
    gen_qemu_ld64_ppc64(arg0, arg1, flags);
2631
    if (unlikely(flags & 1))
2632
        tcg_gen_bswap_i64(arg0, arg0);
2633
}
2634

    
2635
static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
2636
{
2637
    gen_qemu_st8_ppc64(arg0, arg1, flags);
2638
}
2639

    
2640
static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
2641
{
2642
    if (unlikely(flags & 1)) {
2643
        TCGv_i32 t0;
2644
        TCGv_i64 t1;
2645
        t0 = tcg_temp_new_i32();
2646
        tcg_gen_trunc_tl_i32(t0, arg0);
2647
        tcg_gen_ext16u_i32(t0, t0);
2648
        tcg_gen_bswap16_i32(t0, t0);
2649
        t1 = tcg_temp_new_i64();
2650
        tcg_gen_extu_i32_tl(t1, t0);
2651
        tcg_temp_free_i32(t0);
2652
        gen_qemu_st16_ppc64(t1, arg1, flags);
2653
        tcg_temp_free_i64(t1);
2654
    } else
2655
        gen_qemu_st16_ppc64(arg0, arg1, flags);
2656
}
2657

    
2658
static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
2659
{
2660
    if (unlikely(flags & 1)) {
2661
        TCGv_i32 t0;
2662
        TCGv_i64 t1;
2663
        t0 = tcg_temp_new_i32();
2664
        tcg_gen_trunc_tl_i32(t0, arg0);
2665
        tcg_gen_bswap_i32(t0, t0);
2666
        t1 = tcg_temp_new_i64();
2667
        tcg_gen_extu_i32_tl(t1, t0);
2668
        tcg_temp_free_i32(t0);
2669
        gen_qemu_st32_ppc64(t1, arg1, flags);
2670
        tcg_temp_free_i64(t1);
2671
    } else
2672
        gen_qemu_st32_ppc64(arg0, arg1, flags);
2673
}
2674

    
2675
static always_inline void gen_qemu_st64(TCGv arg0, TCGv arg1, int flags)
2676
{
2677
    if (unlikely(flags & 1)) {
2678
        TCGv_i64 t0 = tcg_temp_new_i64();
2679
        tcg_gen_bswap_i64(t0, arg0);
2680
        gen_qemu_st64_ppc64(t0, arg1, flags);
2681
        tcg_temp_free_i64(t0);
2682
    } else
2683
        gen_qemu_st64_ppc64(arg0, arg1, flags);
2684
}
2685

    
2686

    
2687
#else /* defined(TARGET_PPC64) */
2688
#define GEN_QEMU_LD_PPC32(width)                                                      \
2689
static always_inline void gen_qemu_ld##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2690
{                                                                                     \
2691
    tcg_gen_qemu_ld##width(arg0, arg1, flags >> 1);                                   \
2692
}
2693
GEN_QEMU_LD_PPC32(8u)
2694
GEN_QEMU_LD_PPC32(8s)
2695
GEN_QEMU_LD_PPC32(16u)
2696
GEN_QEMU_LD_PPC32(16s)
2697
GEN_QEMU_LD_PPC32(32u)
2698
GEN_QEMU_LD_PPC32(32s)
2699
static always_inline void gen_qemu_ld64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2700
{
2701
    tcg_gen_qemu_ld64(arg0, arg1, flags >> 1);
2702
}
2703

    
2704
#define GEN_QEMU_ST_PPC32(width)                                                      \
2705
static always_inline void gen_qemu_st##width##_ppc32(TCGv arg0, TCGv arg1, int flags) \
2706
{                                                                                     \
2707
    tcg_gen_qemu_st##width(arg0, arg1, flags >> 1);                                   \
2708
}
2709
GEN_QEMU_ST_PPC32(8)
2710
GEN_QEMU_ST_PPC32(16)
2711
GEN_QEMU_ST_PPC32(32)
2712
static always_inline void gen_qemu_st64_ppc32(TCGv_i64 arg0, TCGv arg1, int flags)
2713
{
2714
    tcg_gen_qemu_st64(arg0, arg1, flags >> 1);
2715
}
2716

    
2717
static always_inline void gen_qemu_ld8u(TCGv arg0, TCGv arg1, int flags)
2718
{
2719
    gen_qemu_ld8u_ppc32(arg0, arg1, flags >> 1);
2720
}
2721

    
2722
static always_inline void gen_qemu_ld8s(TCGv arg0, TCGv arg1, int flags)
2723
{
2724
    gen_qemu_ld8s_ppc32(arg0, arg1, flags >> 1);
2725
}
2726

    
2727
static always_inline void gen_qemu_ld16u(TCGv arg0, TCGv arg1, int flags)
2728
{
2729
    gen_qemu_ld16u_ppc32(arg0, arg1, flags >> 1);
2730
    if (unlikely(flags & 1))
2731
        tcg_gen_bswap16_i32(arg0, arg0);
2732
}
2733

    
2734
static always_inline void gen_qemu_ld16s(TCGv arg0, TCGv arg1, int flags)
2735
{
2736
    if (unlikely(flags & 1)) {
2737
        gen_qemu_ld16u_ppc32(arg0, arg1, flags);
2738
        tcg_gen_bswap16_i32(arg0, arg0);
2739
        tcg_gen_ext16s_i32(arg0, arg0);
2740
    } else
2741
        gen_qemu_ld16s_ppc32(arg0, arg1, flags);
2742
}
2743

    
2744
static always_inline void gen_qemu_ld32u(TCGv arg0, TCGv arg1, int flags)
2745
{
2746
    gen_qemu_ld32u_ppc32(arg0, arg1, flags);
2747
    if (unlikely(flags & 1))
2748
        tcg_gen_bswap_i32(arg0, arg0);
2749
}
2750

    
2751
static always_inline void gen_qemu_ld64(TCGv_i64 arg0, TCGv arg1, int flags)
2752
{
2753
    gen_qemu_ld64_ppc32(arg0, arg1, flags);
2754
    if (unlikely(flags & 1))
2755
        tcg_gen_bswap_i64(arg0, arg0);
2756
}
2757

    
2758
static always_inline void gen_qemu_st8(TCGv arg0, TCGv arg1, int flags)
2759
{
2760
    gen_qemu_st8_ppc32(arg0, arg1, flags);
2761
}
2762

    
2763
static always_inline void gen_qemu_st16(TCGv arg0, TCGv arg1, int flags)
2764
{
2765
    if (unlikely(flags & 1)) {
2766
        TCGv_i32 temp = tcg_temp_new_i32();
2767
        tcg_gen_ext16u_i32(temp, arg0);
2768
        tcg_gen_bswap16_i32(temp, temp);
2769
        gen_qemu_st16_ppc32(temp, arg1, flags);
2770
        tcg_temp_free_i32(temp);
2771
    } else
2772
        gen_qemu_st16_ppc32(arg0, arg1, flags);
2773
}
2774

    
2775
static always_inline void gen_qemu_st32(TCGv arg0, TCGv arg1, int flags)
2776
{
2777
    if (unlikely(flags & 1)) {
2778
        TCGv_i32 temp = tcg_temp_new_i32();
2779
        tcg_gen_bswap_i32(temp, arg0);
2780
        gen_qemu_st32_ppc32(temp, arg1, flags);
2781
        tcg_temp_free_i32(temp);
2782
    } else
2783
        gen_qemu_st32_ppc32(arg0, arg1, flags);
2784
}
2785

    
2786
static always_inline void gen_qemu_st64(TCGv_i64 arg0, TCGv arg1, int flags)
2787
{
2788
    if (unlikely(flags & 1)) {
2789
        TCGv_i64 temp = tcg_temp_new_i64();
2790
        tcg_gen_bswap_i64(temp, arg0);
2791
        gen_qemu_st64_ppc32(temp, arg1, flags);
2792
        tcg_temp_free_i64(temp);
2793
    } else
2794
        gen_qemu_st64_ppc32(arg0, arg1, flags);
2795
}
2796
#endif
2797

    
2798
#define GEN_LD(name, ldop, opc, type)                                         \
2799
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2800
{                                                                             \
2801
    TCGv EA = tcg_temp_new();                                                 \
2802
    gen_set_access_type(ACCESS_INT);                                          \
2803
    gen_addr_imm_index(EA, ctx, 0);                                           \
2804
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2805
    tcg_temp_free(EA);                                                        \
2806
}
2807

    
2808
#define GEN_LDU(name, ldop, opc, type)                                        \
2809
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2810
{                                                                             \
2811
    TCGv EA;                                                                  \
2812
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2813
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2814
        GEN_EXCP_INVAL(ctx);                                                  \
2815
        return;                                                               \
2816
    }                                                                         \
2817
    EA = tcg_temp_new();                                                      \
2818
    gen_set_access_type(ACCESS_INT);                                          \
2819
    if (type == PPC_64B)                                                      \
2820
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
2821
    else                                                                      \
2822
        gen_addr_imm_index(EA, ctx, 0);                                       \
2823
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2824
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2825
    tcg_temp_free(EA);                                                        \
2826
}
2827

    
2828
#define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2829
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2830
{                                                                             \
2831
    TCGv EA;                                                                  \
2832
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2833
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2834
        GEN_EXCP_INVAL(ctx);                                                  \
2835
        return;                                                               \
2836
    }                                                                         \
2837
    EA = tcg_temp_new();                                                      \
2838
    gen_set_access_type(ACCESS_INT);                                          \
2839
    gen_addr_reg_index(EA, ctx);                                              \
2840
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2841
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2842
    tcg_temp_free(EA);                                                        \
2843
}
2844

    
2845
#define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2846
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2847
{                                                                             \
2848
    TCGv EA = tcg_temp_new();                                                 \
2849
    gen_set_access_type(ACCESS_INT);                                          \
2850
    gen_addr_reg_index(EA, ctx);                                              \
2851
    gen_qemu_##ldop(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
2852
    tcg_temp_free(EA);                                                        \
2853
}
2854

    
2855
#define GEN_LDS(name, ldop, op, type)                                         \
2856
GEN_LD(name, ldop, op | 0x20, type);                                          \
2857
GEN_LDU(name, ldop, op | 0x21, type);                                         \
2858
GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2859
GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2860

    
2861
/* lbz lbzu lbzux lbzx */
2862
GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2863
/* lha lhau lhaux lhax */
2864
GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2865
/* lhz lhzu lhzux lhzx */
2866
GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2867
/* lwz lwzu lwzux lwzx */
2868
GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2869
#if defined(TARGET_PPC64)
2870
/* lwaux */
2871
GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2872
/* lwax */
2873
GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2874
/* ldux */
2875
GEN_LDUX(ld, ld64, 0x15, 0x01, PPC_64B);
2876
/* ldx */
2877
GEN_LDX(ld, ld64, 0x15, 0x00, PPC_64B);
2878
GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B)
2879
{
2880
    TCGv EA;
2881
    if (Rc(ctx->opcode)) {
2882
        if (unlikely(rA(ctx->opcode) == 0 ||
2883
                     rA(ctx->opcode) == rD(ctx->opcode))) {
2884
            GEN_EXCP_INVAL(ctx);
2885
            return;
2886
        }
2887
    }
2888
    EA = tcg_temp_new();
2889
    gen_set_access_type(ACCESS_INT);
2890
    gen_addr_imm_index(EA, ctx, 0x03);
2891
    if (ctx->opcode & 0x02) {
2892
        /* lwa (lwau is undefined) */
2893
        gen_qemu_ld32s(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2894
    } else {
2895
        /* ld - ldu */
2896
        gen_qemu_ld64(cpu_gpr[rD(ctx->opcode)], EA, ctx->mem_idx);
2897
    }
2898
    if (Rc(ctx->opcode))
2899
        tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2900
    tcg_temp_free(EA);
2901
}
2902
/* lq */
2903
GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX)
2904
{
2905
#if defined(CONFIG_USER_ONLY)
2906
    GEN_EXCP_PRIVOPC(ctx);
2907
#else
2908
    int ra, rd;
2909
    TCGv EA;
2910

    
2911
    /* Restore CPU state */
2912
    if (unlikely(ctx->supervisor == 0)) {
2913
        GEN_EXCP_PRIVOPC(ctx);
2914
        return;
2915
    }
2916
    ra = rA(ctx->opcode);
2917
    rd = rD(ctx->opcode);
2918
    if (unlikely((rd & 1) || rd == ra)) {
2919
        GEN_EXCP_INVAL(ctx);
2920
        return;
2921
    }
2922
    if (unlikely(ctx->mem_idx & 1)) {
2923
        /* Little-endian mode is not handled */
2924
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2925
        return;
2926
    }
2927
    EA = tcg_temp_new();
2928
    gen_set_access_type(ACCESS_INT);
2929
    gen_addr_imm_index(EA, ctx, 0x0F);
2930
    gen_qemu_ld64(cpu_gpr[rd], EA, ctx->mem_idx);
2931
    tcg_gen_addi_tl(EA, EA, 8);
2932
    gen_qemu_ld64(cpu_gpr[rd+1], EA, ctx->mem_idx);
2933
    tcg_temp_free(EA);
2934
#endif
2935
}
2936
#endif
2937

    
2938
/***                              Integer store                            ***/
2939
#define GEN_ST(name, stop, opc, type)                                         \
2940
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
2941
{                                                                             \
2942
    TCGv EA = tcg_temp_new();                                                 \
2943
    gen_set_access_type(ACCESS_INT);                                          \
2944
    gen_addr_imm_index(EA, ctx, 0);                                           \
2945
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2946
    tcg_temp_free(EA);                                                        \
2947
}
2948

    
2949
#define GEN_STU(name, stop, opc, type)                                        \
2950
GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
2951
{                                                                             \
2952
    TCGv EA;                                                                  \
2953
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2954
        GEN_EXCP_INVAL(ctx);                                                  \
2955
        return;                                                               \
2956
    }                                                                         \
2957
    EA = tcg_temp_new();                                                      \
2958
    gen_set_access_type(ACCESS_INT);                                          \
2959
    if (type == PPC_64B)                                                      \
2960
        gen_addr_imm_index(EA, ctx, 0x03);                                    \
2961
    else                                                                      \
2962
        gen_addr_imm_index(EA, ctx, 0);                                       \
2963
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2964
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2965
    tcg_temp_free(EA);                                                        \
2966
}
2967

    
2968
#define GEN_STUX(name, stop, opc2, opc3, type)                                \
2969
GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type)                     \
2970
{                                                                             \
2971
    TCGv EA;                                                                  \
2972
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2973
        GEN_EXCP_INVAL(ctx);                                                  \
2974
        return;                                                               \
2975
    }                                                                         \
2976
    EA = tcg_temp_new();                                                      \
2977
    gen_set_access_type(ACCESS_INT);                                          \
2978
    gen_addr_reg_index(EA, ctx);                                              \
2979
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2980
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2981
    tcg_temp_free(EA);                                                        \
2982
}
2983

    
2984
#define GEN_STX(name, stop, opc2, opc3, type)                                 \
2985
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
2986
{                                                                             \
2987
    TCGv EA = tcg_temp_new();                                                 \
2988
    gen_set_access_type(ACCESS_INT);                                          \
2989
    gen_addr_reg_index(EA, ctx);                                              \
2990
    gen_qemu_##stop(cpu_gpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
2991
    tcg_temp_free(EA);                                                        \
2992
}
2993

    
2994
#define GEN_STS(name, stop, op, type)                                         \
2995
GEN_ST(name, stop, op | 0x20, type);                                          \
2996
GEN_STU(name, stop, op | 0x21, type);                                         \
2997
GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2998
GEN_STX(name, stop, 0x17, op | 0x00, type)
2999

    
3000
/* stb stbu stbux stbx */
3001
GEN_STS(stb, st8, 0x06, PPC_INTEGER);
3002
/* sth sthu sthux sthx */
3003
GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
3004
/* stw stwu stwux stwx */
3005
GEN_STS(stw, st32, 0x04, PPC_INTEGER);
3006
#if defined(TARGET_PPC64)
3007
GEN_STUX(std, st64, 0x15, 0x05, PPC_64B);
3008
GEN_STX(std, st64, 0x15, 0x04, PPC_64B);
3009
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
3010
{
3011
    int rs;
3012
    TCGv EA;
3013

    
3014
    rs = rS(ctx->opcode);
3015
    if ((ctx->opcode & 0x3) == 0x2) {
3016
#if defined(CONFIG_USER_ONLY)
3017
        GEN_EXCP_PRIVOPC(ctx);
3018
#else
3019
        /* stq */
3020
        if (unlikely(ctx->supervisor == 0)) {
3021
            GEN_EXCP_PRIVOPC(ctx);
3022
            return;
3023
        }
3024
        if (unlikely(rs & 1)) {
3025
            GEN_EXCP_INVAL(ctx);
3026
            return;
3027
        }
3028
        if (unlikely(ctx->mem_idx & 1)) {
3029
            /* Little-endian mode is not handled */
3030
            GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
3031
            return;
3032
        }
3033
        EA = tcg_temp_new();
3034
        gen_set_access_type(ACCESS_INT);
3035
        gen_addr_imm_index(EA, ctx, 0x03);
3036
        gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
3037
        tcg_gen_addi_tl(EA, EA, 8);
3038
        gen_qemu_st64(cpu_gpr[rs+1], EA, ctx->mem_idx);
3039
        tcg_temp_free(EA);
3040
#endif
3041
    } else {
3042
        /* std / stdu */
3043
        if (Rc(ctx->opcode)) {
3044
            if (unlikely(rA(ctx->opcode) == 0)) {
3045
                GEN_EXCP_INVAL(ctx);
3046
                return;
3047
            }
3048
        }
3049
        EA = tcg_temp_new();
3050
        gen_set_access_type(ACCESS_INT);
3051
        gen_addr_imm_index(EA, ctx, 0x03);
3052
        gen_qemu_st64(cpu_gpr[rs], EA, ctx->mem_idx);
3053
        if (Rc(ctx->opcode))
3054
            tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
3055
        tcg_temp_free(EA);
3056
    }
3057
}
3058
#endif
3059
/***                Integer load and store with byte reverse               ***/
3060
/* lhbrx */
3061
void always_inline gen_qemu_ld16ur(TCGv t0, TCGv t1, int flags)
3062
{
3063
    TCGv_i32 temp = tcg_temp_new_i32();
3064
    gen_qemu_ld16u(t0, t1, flags);
3065
    tcg_gen_trunc_tl_i32(temp, t0);
3066
    tcg_gen_bswap16_i32(temp, temp);
3067
    tcg_gen_extu_i32_tl(t0, temp);
3068
    tcg_temp_free_i32(temp);
3069
}
3070
GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3071

    
3072
/* lwbrx */
3073
void always_inline gen_qemu_ld32ur(TCGv t0, TCGv t1, int flags)
3074
{
3075
    TCGv_i32 temp = tcg_temp_new_i32();
3076
    gen_qemu_ld32u(t0, t1, flags);
3077
    tcg_gen_trunc_tl_i32(temp, t0);
3078
    tcg_gen_bswap_i32(temp, temp);
3079
    tcg_gen_extu_i32_tl(t0, temp);
3080
    tcg_temp_free_i32(temp);
3081
}
3082
GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3083

    
3084
/* sthbrx */
3085
void always_inline gen_qemu_st16r(TCGv t0, TCGv t1, int flags)
3086
{
3087
    TCGv_i32 temp = tcg_temp_new_i32();
3088
    TCGv t2 = tcg_temp_new();
3089
    tcg_gen_trunc_tl_i32(temp, t0);
3090
    tcg_gen_ext16u_i32(temp, temp);
3091
    tcg_gen_bswap16_i32(temp, temp);
3092
    tcg_gen_extu_i32_tl(t2, temp);
3093
    tcg_temp_free_i32(temp);
3094
    gen_qemu_st16(t2, t1, flags);
3095
    tcg_temp_free(t2);
3096
}
3097
GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3098

    
3099
/* stwbrx */
3100
void always_inline gen_qemu_st32r(TCGv t0, TCGv t1, int flags)
3101
{
3102
    TCGv_i32 temp = tcg_temp_new_i32();
3103
    TCGv t2 = tcg_temp_new();
3104
    tcg_gen_trunc_tl_i32(temp, t0);
3105
    tcg_gen_bswap_i32(temp, temp);
3106
    tcg_gen_extu_i32_tl(t2, temp);
3107
    tcg_temp_free_i32(temp);
3108
    gen_qemu_st32(t2, t1, flags);
3109
    tcg_temp_free(t2);
3110
}
3111
GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3112

    
3113
/***                    Integer load and store multiple                    ***/
3114
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
3115
static GenOpFunc1 *gen_op_lmw[NB_MEM_FUNCS] = {
3116
    GEN_MEM_FUNCS(lmw),
3117
};
3118
static GenOpFunc1 *gen_op_stmw[NB_MEM_FUNCS] = {
3119
    GEN_MEM_FUNCS(stmw),
3120
};
3121

    
3122
/* lmw */
3123
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3124
{
3125
    /* NIP cannot be restored if the memory exception comes from an helper */
3126
    gen_update_nip(ctx, ctx->nip - 4);
3127
    gen_addr_imm_index(cpu_T[0], ctx, 0);
3128
    op_ldstm(lmw, rD(ctx->opcode));
3129
}
3130

    
3131
/* stmw */
3132
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
3133
{
3134
    /* NIP cannot be restored if the memory exception comes from an helper */
3135
    gen_update_nip(ctx, ctx->nip - 4);
3136
    gen_addr_imm_index(cpu_T[0], ctx, 0);
3137
    op_ldstm(stmw, rS(ctx->opcode));
3138
}
3139

    
3140
/***                    Integer load and store strings                     ***/
3141
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
3142
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
3143
/* string load & stores are by definition endian-safe */
3144
#define gen_op_lswi_le_raw       gen_op_lswi_raw
3145
#define gen_op_lswi_le_user      gen_op_lswi_user
3146
#define gen_op_lswi_le_kernel    gen_op_lswi_kernel
3147
#define gen_op_lswi_le_hypv      gen_op_lswi_hypv
3148
#define gen_op_lswi_le_64_raw    gen_op_lswi_raw
3149
#define gen_op_lswi_le_64_user   gen_op_lswi_user
3150
#define gen_op_lswi_le_64_kernel gen_op_lswi_kernel
3151
#define gen_op_lswi_le_64_hypv   gen_op_lswi_hypv
3152
static GenOpFunc1 *gen_op_lswi[NB_MEM_FUNCS] = {
3153
    GEN_MEM_FUNCS(lswi),
3154
};
3155
#define gen_op_lswx_le_raw       gen_op_lswx_raw
3156
#define gen_op_lswx_le_user      gen_op_lswx_user
3157
#define gen_op_lswx_le_kernel    gen_op_lswx_kernel
3158
#define gen_op_lswx_le_hypv      gen_op_lswx_hypv
3159
#define gen_op_lswx_le_64_raw    gen_op_lswx_raw
3160
#define gen_op_lswx_le_64_user   gen_op_lswx_user
3161
#define gen_op_lswx_le_64_kernel gen_op_lswx_kernel
3162
#define gen_op_lswx_le_64_hypv   gen_op_lswx_hypv
3163
static GenOpFunc3 *gen_op_lswx[NB_MEM_FUNCS] = {
3164
    GEN_MEM_FUNCS(lswx),
3165
};
3166
#define gen_op_stsw_le_raw       gen_op_stsw_raw
3167
#define gen_op_stsw_le_user      gen_op_stsw_user
3168
#define gen_op_stsw_le_kernel    gen_op_stsw_kernel
3169
#define gen_op_stsw_le_hypv      gen_op_stsw_hypv
3170
#define gen_op_stsw_le_64_raw    gen_op_stsw_raw
3171
#define gen_op_stsw_le_64_user   gen_op_stsw_user
3172
#define gen_op_stsw_le_64_kernel gen_op_stsw_kernel
3173
#define gen_op_stsw_le_64_hypv   gen_op_stsw_hypv
3174
static GenOpFunc1 *gen_op_stsw[NB_MEM_FUNCS] = {
3175
    GEN_MEM_FUNCS(stsw),
3176
};
3177

    
3178
/* lswi */
3179
/* PowerPC32 specification says we must generate an exception if
3180
 * rA is in the range of registers to be loaded.
3181
 * In an other hand, IBM says this is valid, but rA won't be loaded.
3182
 * For now, I'll follow the spec...
3183
 */
3184
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING)
3185
{
3186
    int nb = NB(ctx->opcode);
3187
    int start = rD(ctx->opcode);
3188
    int ra = rA(ctx->opcode);
3189
    int nr;
3190

    
3191
    if (nb == 0)
3192
        nb = 32;
3193
    nr = nb / 4;
3194
    if (unlikely(((start + nr) > 32  &&
3195
                  start <= ra && (start + nr - 32) > ra) ||
3196
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
3197
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3198
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
3199
        return;
3200
    }
3201
    /* NIP cannot be restored if the memory exception comes from an helper */
3202
    gen_update_nip(ctx, ctx->nip - 4);
3203
    gen_addr_register(cpu_T[0], ctx);
3204
    tcg_gen_movi_tl(cpu_T[1], nb);
3205
    op_ldsts(lswi, start);
3206
}
3207

    
3208
/* lswx */
3209
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING)
3210
{
3211
    int ra = rA(ctx->opcode);
3212
    int rb = rB(ctx->opcode);
3213

    
3214
    /* NIP cannot be restored if the memory exception comes from an helper */
3215
    gen_update_nip(ctx, ctx->nip - 4);
3216
    gen_addr_reg_index(cpu_T[0], ctx);
3217
    if (ra == 0) {
3218
        ra = rb;
3219
    }
3220
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3221
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
3222
}
3223

    
3224
/* stswi */
3225
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING)
3226
{
3227
    int nb = NB(ctx->opcode);
3228

    
3229
    /* NIP cannot be restored if the memory exception comes from an helper */
3230
    gen_update_nip(ctx, ctx->nip - 4);
3231
    gen_addr_register(cpu_T[0], ctx);
3232
    if (nb == 0)
3233
        nb = 32;
3234
    tcg_gen_movi_tl(cpu_T[1], nb);
3235
    op_ldsts(stsw, rS(ctx->opcode));
3236
}
3237

    
3238
/* stswx */
3239
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING)
3240
{
3241
    /* NIP cannot be restored if the memory exception comes from an helper */
3242
    gen_update_nip(ctx, ctx->nip - 4);
3243
    gen_addr_reg_index(cpu_T[0], ctx);
3244
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
3245
    op_ldsts(stsw, rS(ctx->opcode));
3246
}
3247

    
3248
/***                        Memory synchronisation                         ***/
3249
/* eieio */
3250
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
3251
{
3252
}
3253

    
3254
/* isync */
3255
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
3256
{
3257
    GEN_STOP(ctx);
3258
}
3259

    
3260
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
3261
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
3262
static GenOpFunc *gen_op_lwarx[NB_MEM_FUNCS] = {
3263
    GEN_MEM_FUNCS(lwarx),
3264
};
3265
static GenOpFunc *gen_op_stwcx[NB_MEM_FUNCS] = {
3266
    GEN_MEM_FUNCS(stwcx),
3267
};
3268

    
3269
/* lwarx */
3270
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
3271
{
3272
    /* NIP cannot be restored if the memory exception comes from an helper */
3273
    gen_update_nip(ctx, ctx->nip - 4);
3274
    gen_set_access_type(ACCESS_RES);
3275
    gen_addr_reg_index(cpu_T[0], ctx);
3276
    op_lwarx();
3277
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
3278
}
3279

    
3280
/* stwcx. */
3281
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
3282
{
3283
    /* NIP cannot be restored if the memory exception comes from an helper */
3284
    gen_update_nip(ctx, ctx->nip - 4);
3285
    gen_set_access_type(ACCESS_RES);
3286
    gen_addr_reg_index(cpu_T[0], ctx);
3287
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3288
    op_stwcx();
3289
}
3290

    
3291
#if defined(TARGET_PPC64)
3292
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
3293
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
3294
static GenOpFunc *gen_op_ldarx[NB_MEM_FUNCS] = {
3295
    GEN_MEM_FUNCS(ldarx),
3296
};
3297
static GenOpFunc *gen_op_stdcx[NB_MEM_FUNCS] = {
3298
    GEN_MEM_FUNCS(stdcx),
3299
};
3300

    
3301
/* ldarx */
3302
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
3303
{
3304
    /* NIP cannot be restored if the memory exception comes from an helper */
3305
    gen_update_nip(ctx, ctx->nip - 4);
3306
    gen_set_access_type(ACCESS_RES);
3307
    gen_addr_reg_index(cpu_T[0], ctx);
3308
    op_ldarx();
3309
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[1]);
3310
}
3311

    
3312
/* stdcx. */
3313
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
3314
{
3315
    /* NIP cannot be restored if the memory exception comes from an helper */
3316
    gen_update_nip(ctx, ctx->nip - 4);
3317
    gen_set_access_type(ACCESS_RES);
3318
    gen_addr_reg_index(cpu_T[0], ctx);
3319
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
3320
    op_stdcx();
3321
}
3322
#endif /* defined(TARGET_PPC64) */
3323

    
3324
/* sync */
3325
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
3326
{
3327
}
3328

    
3329
/* wait */
3330
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
3331
{
3332
    /* Stop translation, as the CPU is supposed to sleep from now */
3333
    gen_op_wait();
3334
    GEN_EXCP(ctx, EXCP_HLT, 1);
3335
}
3336

    
3337
/***                         Floating-point load                           ***/
3338
#define GEN_LDF(name, ldop, opc, type)                                        \
3339
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3340
{                                                                             \
3341
    TCGv EA;                                                                  \
3342
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3343
        GEN_EXCP_NO_FP(ctx);                                                  \
3344
        return;                                                               \
3345
    }                                                                         \
3346
    gen_set_access_type(ACCESS_FLOAT);                                        \
3347
    EA = tcg_temp_new();                                                      \
3348
    gen_addr_imm_index(EA, ctx, 0);                                           \
3349
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3350
    tcg_temp_free(EA);                                                        \
3351
}
3352

    
3353
#define GEN_LDUF(name, ldop, opc, type)                                       \
3354
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3355
{                                                                             \
3356
    TCGv EA;                                                                  \
3357
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3358
        GEN_EXCP_NO_FP(ctx);                                                  \
3359
        return;                                                               \
3360
    }                                                                         \
3361
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3362
        GEN_EXCP_INVAL(ctx);                                                  \
3363
        return;                                                               \
3364
    }                                                                         \
3365
    gen_set_access_type(ACCESS_FLOAT);                                        \
3366
    EA = tcg_temp_new();                                                      \
3367
    gen_addr_imm_index(EA, ctx, 0);                                           \
3368
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3369
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3370
    tcg_temp_free(EA);                                                        \
3371
}
3372

    
3373
#define GEN_LDUXF(name, ldop, opc, type)                                      \
3374
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3375
{                                                                             \
3376
    TCGv EA;                                                                  \
3377
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3378
        GEN_EXCP_NO_FP(ctx);                                                  \
3379
        return;                                                               \
3380
    }                                                                         \
3381
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3382
        GEN_EXCP_INVAL(ctx);                                                  \
3383
        return;                                                               \
3384
    }                                                                         \
3385
    gen_set_access_type(ACCESS_FLOAT);                                        \
3386
    EA = tcg_temp_new();                                                      \
3387
    gen_addr_reg_index(EA, ctx);                                              \
3388
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3389
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3390
    tcg_temp_free(EA);                                                        \
3391
}
3392

    
3393
#define GEN_LDXF(name, ldop, opc2, opc3, type)                                \
3394
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3395
{                                                                             \
3396
    TCGv EA;                                                                  \
3397
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3398
        GEN_EXCP_NO_FP(ctx);                                                  \
3399
        return;                                                               \
3400
    }                                                                         \
3401
    gen_set_access_type(ACCESS_FLOAT);                                        \
3402
    EA = tcg_temp_new();                                                      \
3403
    gen_addr_reg_index(EA, ctx);                                              \
3404
    gen_qemu_##ldop(cpu_fpr[rD(ctx->opcode)], EA, ctx->mem_idx);              \
3405
    tcg_temp_free(EA);                                                        \
3406
}
3407

    
3408
#define GEN_LDFS(name, ldop, op, type)                                        \
3409
GEN_LDF(name, ldop, op | 0x20, type);                                         \
3410
GEN_LDUF(name, ldop, op | 0x21, type);                                        \
3411
GEN_LDUXF(name, ldop, op | 0x01, type);                                       \
3412
GEN_LDXF(name, ldop, 0x17, op | 0x00, type)
3413

    
3414
static always_inline void gen_qemu_ld32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3415
{
3416
    TCGv t0 = tcg_temp_new();
3417
    TCGv_i32 t1 = tcg_temp_new_i32();
3418
    gen_qemu_ld32u(t0, arg2, flags);
3419
    tcg_gen_trunc_tl_i32(t1, t0);
3420
    tcg_temp_free(t0);
3421
    gen_helper_float32_to_float64(arg1, t1);
3422
    tcg_temp_free_i32(t1);
3423
}
3424

    
3425
 /* lfd lfdu lfdux lfdx */
3426
GEN_LDFS(lfd, ld64, 0x12, PPC_FLOAT);
3427
 /* lfs lfsu lfsux lfsx */
3428
GEN_LDFS(lfs, ld32fs, 0x10, PPC_FLOAT);
3429

    
3430
/***                         Floating-point store                          ***/
3431
#define GEN_STF(name, stop, opc, type)                                        \
3432
GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type)                          \
3433
{                                                                             \
3434
    TCGv EA;                                                                  \
3435
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3436
        GEN_EXCP_NO_FP(ctx);                                                  \
3437
        return;                                                               \
3438
    }                                                                         \
3439
    gen_set_access_type(ACCESS_FLOAT);                                        \
3440
    EA = tcg_temp_new();                                                      \
3441
    gen_addr_imm_index(EA, ctx, 0);                                           \
3442
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3443
    tcg_temp_free(EA);                                                        \
3444
}
3445

    
3446
#define GEN_STUF(name, stop, opc, type)                                       \
3447
GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type)                       \
3448
{                                                                             \
3449
    TCGv EA;                                                                  \
3450
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3451
        GEN_EXCP_NO_FP(ctx);                                                  \
3452
        return;                                                               \
3453
    }                                                                         \
3454
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3455
        GEN_EXCP_INVAL(ctx);                                                  \
3456
        return;                                                               \
3457
    }                                                                         \
3458
    gen_set_access_type(ACCESS_FLOAT);                                        \
3459
    EA = tcg_temp_new();                                                      \
3460
    gen_addr_imm_index(EA, ctx, 0);                                           \
3461
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3462
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3463
    tcg_temp_free(EA);                                                        \
3464
}
3465

    
3466
#define GEN_STUXF(name, stop, opc, type)                                      \
3467
GEN_HANDLER(name##ux, 0x1F, 0x17, opc, 0x00000001, type)                      \
3468
{                                                                             \
3469
    TCGv EA;                                                                  \
3470
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3471
        GEN_EXCP_NO_FP(ctx);                                                  \
3472
        return;                                                               \
3473
    }                                                                         \
3474
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3475
        GEN_EXCP_INVAL(ctx);                                                  \
3476
        return;                                                               \
3477
    }                                                                         \
3478
    gen_set_access_type(ACCESS_FLOAT);                                        \
3479
    EA = tcg_temp_new();                                                      \
3480
    gen_addr_reg_index(EA, ctx);                                              \
3481
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3482
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
3483
    tcg_temp_free(EA);                                                        \
3484
}
3485

    
3486
#define GEN_STXF(name, stop, opc2, opc3, type)                                \
3487
GEN_HANDLER(name##x, 0x1F, opc2, opc3, 0x00000001, type)                      \
3488
{                                                                             \
3489
    TCGv EA;                                                                  \
3490
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3491
        GEN_EXCP_NO_FP(ctx);                                                  \
3492
        return;                                                               \
3493
    }                                                                         \
3494
    gen_set_access_type(ACCESS_FLOAT);                                        \
3495
    EA = tcg_temp_new();                                                      \
3496
    gen_addr_reg_index(EA, ctx);                                              \
3497
    gen_qemu_##stop(cpu_fpr[rS(ctx->opcode)], EA, ctx->mem_idx);              \
3498
    tcg_temp_free(EA);                                                        \
3499
}
3500

    
3501
#define GEN_STFS(name, stop, op, type)                                        \
3502
GEN_STF(name, stop, op | 0x20, type);                                         \
3503
GEN_STUF(name, stop, op | 0x21, type);                                        \
3504
GEN_STUXF(name, stop, op | 0x01, type);                                       \
3505
GEN_STXF(name, stop, 0x17, op | 0x00, type)
3506

    
3507
static always_inline void gen_qemu_st32fs(TCGv_i64 arg1, TCGv arg2, int flags)
3508
{
3509
    TCGv_i32 t0 = tcg_temp_new_i32();
3510
    TCGv t1 = tcg_temp_new();
3511
    gen_helper_float64_to_float32(t0, arg1);
3512
    tcg_gen_extu_i32_tl(t1, t0);
3513
    tcg_temp_free_i32(t0);
3514
    gen_qemu_st32(t1, arg2, flags);
3515
    tcg_temp_free(t1);
3516
}
3517

    
3518
/* stfd stfdu stfdux stfdx */
3519
GEN_STFS(stfd, st64, 0x16, PPC_FLOAT);
3520
/* stfs stfsu stfsux stfsx */
3521
GEN_STFS(stfs, st32fs, 0x14, PPC_FLOAT);
3522

    
3523
/* Optional: */
3524
static always_inline void gen_qemu_st32fiw(TCGv_i64 arg1, TCGv arg2, int flags)
3525
{
3526
    TCGv t0 = tcg_temp_new();
3527
    tcg_gen_trunc_i64_tl(t0, arg1),
3528
    gen_qemu_st32(t0, arg2, flags);
3529
    tcg_temp_free(t0);
3530
}
3531
/* stfiwx */
3532
GEN_STXF(stfiw, st32fiw, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3533

    
3534
/***                                Branch                                 ***/
3535
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3536
                                       target_ulong dest)
3537
{
3538
    TranslationBlock *tb;
3539
    tb = ctx->tb;
3540
#if defined(TARGET_PPC64)
3541
    if (!ctx->sf_mode)
3542
        dest = (uint32_t) dest;
3543
#endif
3544
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
3545
        likely(!ctx->singlestep_enabled)) {
3546
        tcg_gen_goto_tb(n);
3547
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3548
        tcg_gen_exit_tb((long)tb + n);
3549
    } else {
3550
        tcg_gen_movi_tl(cpu_nip, dest & ~3);
3551
        if (unlikely(ctx->singlestep_enabled)) {
3552
            if ((ctx->singlestep_enabled &
3553
                (CPU_BRANCH_STEP | CPU_SINGLE_STEP)) &&
3554
                ctx->exception == POWERPC_EXCP_BRANCH) {
3555
                target_ulong tmp = ctx->nip;
3556
                ctx->nip = dest;
3557
                GEN_EXCP(ctx, POWERPC_EXCP_TRACE, 0);
3558
                ctx->nip = tmp;
3559
            }
3560
            if (ctx->singlestep_enabled & GDBSTUB_SINGLE_STEP) {
3561
                gen_update_nip(ctx, dest);
3562
                gen_helper_raise_debug();
3563
            }
3564
        }
3565
        tcg_gen_exit_tb(0);
3566
    }
3567
}
3568

    
3569
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3570
{
3571
#if defined(TARGET_PPC64)
3572
    if (ctx->sf_mode == 0)
3573
        tcg_gen_movi_tl(cpu_lr, (uint32_t)nip);
3574
    else
3575
#endif
3576
        tcg_gen_movi_tl(cpu_lr, nip);
3577
}
3578

    
3579
/* b ba bl bla */
3580
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3581
{
3582
    target_ulong li, target;
3583

    
3584
    ctx->exception = POWERPC_EXCP_BRANCH;
3585
    /* sign extend LI */
3586
#if defined(TARGET_PPC64)
3587
    if (ctx->sf_mode)
3588
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3589
    else
3590
#endif
3591
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3592
    if (likely(AA(ctx->opcode) == 0))
3593
        target = ctx->nip + li - 4;
3594
    else
3595
        target = li;
3596
    if (LK(ctx->opcode))
3597
        gen_setlr(ctx, ctx->nip);
3598
    gen_goto_tb(ctx, 0, target);
3599
}
3600

    
3601
#define BCOND_IM  0
3602
#define BCOND_LR  1
3603
#define BCOND_CTR 2
3604

    
3605
static always_inline void gen_bcond (DisasContext *ctx, int type)
3606
{
3607
    uint32_t bo = BO(ctx->opcode);
3608
    int l1 = gen_new_label();
3609
    TCGv target;
3610

    
3611
    ctx->exception = POWERPC_EXCP_BRANCH;
3612
    if (type == BCOND_LR || type == BCOND_CTR) {
3613
        target = tcg_temp_local_new();
3614
        if (type == BCOND_CTR)
3615
            tcg_gen_mov_tl(target, cpu_ctr);
3616
        else
3617
            tcg_gen_mov_tl(target, cpu_lr);
3618
    }
3619
    if (LK(ctx->opcode))
3620
        gen_setlr(ctx, ctx->nip);
3621
    l1 = gen_new_label();
3622
    if ((bo & 0x4) == 0) {
3623
        /* Decrement and test CTR */
3624
        TCGv temp = tcg_temp_new();
3625
        if (unlikely(type == BCOND_CTR)) {
3626
            GEN_EXCP_INVAL(ctx);
3627
            return;
3628
        }
3629
        tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3630
#if defined(TARGET_PPC64)
3631
        if (!ctx->sf_mode)
3632
            tcg_gen_ext32u_tl(temp, cpu_ctr);
3633
        else
3634
#endif
3635
            tcg_gen_mov_tl(temp, cpu_ctr);
3636
        if (bo & 0x2) {
3637
            tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3638
        } else {
3639
            tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3640
        }
3641
        tcg_temp_free(temp);
3642
    }
3643
    if ((bo & 0x10) == 0) {
3644
        /* Test CR */
3645
        uint32_t bi = BI(ctx->opcode);
3646
        uint32_t mask = 1 << (3 - (bi & 0x03));
3647
        TCGv_i32 temp = tcg_temp_new_i32();
3648

    
3649
        if (bo & 0x8) {
3650
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3651
            tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3652
        } else {
3653
            tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3654
            tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3655
        }
3656
        tcg_temp_free_i32(temp);
3657
    }
3658
    if (type == BCOND_IM) {
3659
        target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3660
        if (likely(AA(ctx->opcode) == 0)) {
3661
            gen_goto_tb(ctx, 0, ctx->nip + li - 4);
3662
        } else {
3663
            gen_goto_tb(ctx, 0, li);
3664
        }
3665
        gen_set_label(l1);
3666
        gen_goto_tb(ctx, 1, ctx->nip);
3667
    } else {
3668
#if defined(TARGET_PPC64)
3669
        if (!(ctx->sf_mode))
3670
            tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3671
        else
3672
#endif
3673
            tcg_gen_andi_tl(cpu_nip, target, ~3);
3674
        tcg_gen_exit_tb(0);
3675
        gen_set_label(l1);
3676
#if defined(TARGET_PPC64)
3677
        if (!(ctx->sf_mode))
3678
            tcg_gen_movi_tl(cpu_nip, (uint32_t)ctx->nip);
3679
        else
3680
#endif
3681
            tcg_gen_movi_tl(cpu_nip, ctx->nip);
3682
        tcg_gen_exit_tb(0);
3683
    }
3684
}
3685

    
3686
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3687
{
3688
    gen_bcond(ctx, BCOND_IM);
3689
}
3690

    
3691
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3692
{
3693
    gen_bcond(ctx, BCOND_CTR);
3694
}
3695

    
3696
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3697
{
3698
    gen_bcond(ctx, BCOND_LR);
3699
}
3700

    
3701
/***                      Condition register logical                       ***/
3702
#define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3703
GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                   \
3704
{                                                                             \
3705
    uint8_t bitmask;                                                          \
3706
    int sh;                                                                   \
3707
    TCGv_i32 t0, t1;                                                          \
3708
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3709
    t0 = tcg_temp_new_i32();                                                  \
3710
    if (sh > 0)                                                               \
3711
        tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3712
    else if (sh < 0)                                                          \
3713
        tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3714
    else                                                                      \
3715
        tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3716
    t1 = tcg_temp_new_i32();                                                  \
3717
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3718
    if (sh > 0)                                                               \
3719
        tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3720
    else if (sh < 0)                                                          \
3721
        tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3722
    else                                                                      \
3723
        tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3724
    tcg_op(t0, t0, t1);                                                       \
3725
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3726
    tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3727
    tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3728
    tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3729
    tcg_temp_free_i32(t0);                                                    \
3730
    tcg_temp_free_i32(t1);                                                    \
3731
}
3732

    
3733
/* crand */
3734
GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3735
/* crandc */
3736
GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3737
/* creqv */
3738
GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3739
/* crnand */
3740
GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3741
/* crnor */
3742
GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3743
/* cror */
3744
GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3745
/* crorc */
3746
GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3747
/* crxor */
3748
GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3749
/* mcrf */
3750
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3751
{
3752
    tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3753
}
3754

    
3755
/***                           System linkage                              ***/
3756
/* rfi (supervisor only) */
3757
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3758
{
3759
#if defined(CONFIG_USER_ONLY)
3760
    GEN_EXCP_PRIVOPC(ctx);
3761
#else
3762
    /* Restore CPU state */
3763
    if (unlikely(!ctx->supervisor)) {
3764
        GEN_EXCP_PRIVOPC(ctx);
3765
        return;
3766
    }
3767
    gen_op_rfi();
3768
    GEN_SYNC(ctx);
3769
#endif
3770
}
3771

    
3772
#if defined(TARGET_PPC64)
3773
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3774
{
3775
#if defined(CONFIG_USER_ONLY)
3776
    GEN_EXCP_PRIVOPC(ctx);
3777
#else
3778
    /* Restore CPU state */
3779
    if (unlikely(!ctx->supervisor)) {
3780
        GEN_EXCP_PRIVOPC(ctx);
3781
        return;
3782
    }
3783
    gen_op_rfid();
3784
    GEN_SYNC(ctx);
3785
#endif
3786
}
3787

    
3788
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H)
3789
{
3790
#if defined(CONFIG_USER_ONLY)
3791
    GEN_EXCP_PRIVOPC(ctx);
3792
#else
3793
    /* Restore CPU state */
3794
    if (unlikely(ctx->supervisor <= 1)) {
3795
        GEN_EXCP_PRIVOPC(ctx);
3796
        return;
3797
    }
3798
    gen_op_hrfid();
3799
    GEN_SYNC(ctx);
3800
#endif
3801
}
3802
#endif
3803

    
3804
/* sc */
3805
#if defined(CONFIG_USER_ONLY)
3806
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3807
#else
3808
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3809
#endif
3810
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3811
{
3812
    uint32_t lev;
3813

    
3814
    lev = (ctx->opcode >> 5) & 0x7F;
3815
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
3816
}
3817

    
3818
/***                                Trap                                   ***/
3819
/* tw */
3820
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3821
{
3822
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3823
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3824
    /* Update the nip since this might generate a trap exception */
3825
    gen_update_nip(ctx, ctx->nip);
3826
    gen_op_tw(TO(ctx->opcode));
3827
}
3828

    
3829
/* twi */
3830
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3831
{
3832
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3833
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3834
    /* Update the nip since this might generate a trap exception */
3835
    gen_update_nip(ctx, ctx->nip);
3836
    gen_op_tw(TO(ctx->opcode));
3837
}
3838

    
3839
#if defined(TARGET_PPC64)
3840
/* td */
3841
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3842
{
3843
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3844
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
3845
    /* Update the nip since this might generate a trap exception */
3846
    gen_update_nip(ctx, ctx->nip);
3847
    gen_op_td(TO(ctx->opcode));
3848
}
3849

    
3850
/* tdi */
3851
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3852
{
3853
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
3854
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
3855
    /* Update the nip since this might generate a trap exception */
3856
    gen_update_nip(ctx, ctx->nip);
3857
    gen_op_td(TO(ctx->opcode));
3858
}
3859
#endif
3860

    
3861
/***                          Processor control                            ***/
3862
/* mcrxr */
3863
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3864
{
3865
    tcg_gen_trunc_tl_i32(cpu_crf[crfD(ctx->opcode)], cpu_xer);
3866
    tcg_gen_shri_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)], XER_CA);
3867
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_SO | 1 << XER_OV | 1 << XER_CA));
3868
}
3869

    
3870
/* mfcr */
3871
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3872
{
3873
    uint32_t crm, crn;
3874

    
3875
    if (likely(ctx->opcode & 0x00100000)) {
3876
        crm = CRM(ctx->opcode);
3877
        if (likely((crm ^ (crm - 1)) == 0)) {
3878
            crn = ffs(crm);
3879
            tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
3880
        }
3881
    } else {
3882
        gen_helper_load_cr(cpu_gpr[rD(ctx->opcode)]);
3883
    }
3884
}
3885

    
3886
/* mfmsr */
3887
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3888
{
3889
#if defined(CONFIG_USER_ONLY)
3890
    GEN_EXCP_PRIVREG(ctx);
3891
#else
3892
    if (unlikely(!ctx->supervisor)) {
3893
        GEN_EXCP_PRIVREG(ctx);
3894
        return;
3895
    }
3896
    gen_op_load_msr();
3897
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3898
#endif
3899
}
3900

    
3901
#if 1
3902
#define SPR_NOACCESS ((void *)(-1UL))
3903
#else
3904
static void spr_noaccess (void *opaque, int sprn)
3905
{
3906
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3907
    printf("ERROR: try to access SPR %d !\n", sprn);
3908
}
3909
#define SPR_NOACCESS (&spr_noaccess)
3910
#endif
3911

    
3912
/* mfspr */
3913
static always_inline void gen_op_mfspr (DisasContext *ctx)
3914
{
3915
    void (*read_cb)(void *opaque, int sprn);
3916
    uint32_t sprn = SPR(ctx->opcode);
3917

    
3918
#if !defined(CONFIG_USER_ONLY)
3919
    if (ctx->supervisor == 2)
3920
        read_cb = ctx->spr_cb[sprn].hea_read;
3921
    else if (ctx->supervisor)
3922
        read_cb = ctx->spr_cb[sprn].oea_read;
3923
    else
3924
#endif
3925
        read_cb = ctx->spr_cb[sprn].uea_read;
3926
    if (likely(read_cb != NULL)) {
3927
        if (likely(read_cb != SPR_NOACCESS)) {
3928
            (*read_cb)(ctx, sprn);
3929
            tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
3930
        } else {
3931
            /* Privilege exception */
3932
            /* This is a hack to avoid warnings when running Linux:
3933
             * this OS breaks the PowerPC virtualisation model,
3934
             * allowing userland application to read the PVR
3935
             */
3936
            if (sprn != SPR_PVR) {
3937
                if (loglevel != 0) {
3938
                    fprintf(logfile, "Trying to read privileged spr %d %03x at "
3939
                            ADDRX "\n", sprn, sprn, ctx->nip);
3940
                }
3941
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3942
                       sprn, sprn, ctx->nip);
3943
            }
3944
            GEN_EXCP_PRIVREG(ctx);
3945
        }
3946
    } else {
3947
        /* Not defined */
3948
        if (loglevel != 0) {
3949
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
3950
                    ADDRX "\n", sprn, sprn, ctx->nip);
3951
        }
3952
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3953
               sprn, sprn, ctx->nip);
3954
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3955
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3956
    }
3957
}
3958

    
3959
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3960
{
3961
    gen_op_mfspr(ctx);
3962
}
3963

    
3964
/* mftb */
3965
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3966
{
3967
    gen_op_mfspr(ctx);
3968
}
3969

    
3970
/* mtcrf */
3971
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3972
{
3973
    uint32_t crm, crn;
3974

    
3975
    crm = CRM(ctx->opcode);
3976
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3977
        TCGv_i32 temp = tcg_temp_new_i32();
3978
        crn = ffs(crm);
3979
        tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
3980
        tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
3981
        tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
3982
        tcg_temp_free_i32(temp);
3983
    } else {
3984
        TCGv_i32 temp = tcg_const_i32(crm);
3985
        gen_helper_store_cr(cpu_gpr[rS(ctx->opcode)], temp);
3986
        tcg_temp_free_i32(temp);
3987
    }
3988
}
3989

    
3990
/* mtmsr */
3991
#if defined(TARGET_PPC64)
3992
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3993
{
3994
#if defined(CONFIG_USER_ONLY)
3995
    GEN_EXCP_PRIVREG(ctx);
3996
#else
3997
    if (unlikely(!ctx->supervisor)) {
3998
        GEN_EXCP_PRIVREG(ctx);
3999
        return;
4000
    }
4001
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4002
    if (ctx->opcode & 0x00010000) {
4003
        /* Special form that does not need any synchronisation */
4004
        gen_op_update_riee();
4005
    } else {
4006
        /* XXX: we need to update nip before the store
4007
         *      if we enter power saving mode, we will exit the loop
4008
         *      directly from ppc_store_msr
4009
         */
4010
        gen_update_nip(ctx, ctx->nip);
4011
        gen_op_store_msr();
4012
        /* Must stop the translation as machine state (may have) changed */
4013
        /* Note that mtmsr is not always defined as context-synchronizing */
4014
        ctx->exception = POWERPC_EXCP_STOP;
4015
    }
4016
#endif
4017
}
4018
#endif
4019

    
4020
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
4021
{
4022
#if defined(CONFIG_USER_ONLY)
4023
    GEN_EXCP_PRIVREG(ctx);
4024
#else
4025
    if (unlikely(!ctx->supervisor)) {
4026
        GEN_EXCP_PRIVREG(ctx);
4027
        return;
4028
    }
4029
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4030
    if (ctx->opcode & 0x00010000) {
4031
        /* Special form that does not need any synchronisation */
4032
        gen_op_update_riee();
4033
    } else {
4034
        /* XXX: we need to update nip before the store
4035
         *      if we enter power saving mode, we will exit the loop
4036
         *      directly from ppc_store_msr
4037
         */
4038
        gen_update_nip(ctx, ctx->nip);
4039
#if defined(TARGET_PPC64)
4040
        if (!ctx->sf_mode)
4041
            gen_op_store_msr_32();
4042
        else
4043
#endif
4044
            gen_op_store_msr();
4045
        /* Must stop the translation as machine state (may have) changed */
4046
        /* Note that mtmsrd is not always defined as context-synchronizing */
4047
        ctx->exception = POWERPC_EXCP_STOP;
4048
    }
4049
#endif
4050
}
4051

    
4052
/* mtspr */
4053
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
4054
{
4055
    void (*write_cb)(void *opaque, int sprn);
4056
    uint32_t sprn = SPR(ctx->opcode);
4057

    
4058
#if !defined(CONFIG_USER_ONLY)
4059
    if (ctx->supervisor == 2)
4060
        write_cb = ctx->spr_cb[sprn].hea_write;
4061
    else if (ctx->supervisor)
4062
        write_cb = ctx->spr_cb[sprn].oea_write;
4063
    else
4064
#endif
4065
        write_cb = ctx->spr_cb[sprn].uea_write;
4066
    if (likely(write_cb != NULL)) {
4067
        if (likely(write_cb != SPR_NOACCESS)) {
4068
            tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4069
            (*write_cb)(ctx, sprn);
4070
        } else {
4071
            /* Privilege exception */
4072
            if (loglevel != 0) {
4073
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
4074
                        ADDRX "\n", sprn, sprn, ctx->nip);
4075
            }
4076
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
4077
                   sprn, sprn, ctx->nip);
4078
            GEN_EXCP_PRIVREG(ctx);
4079
        }
4080
    } else {
4081
        /* Not defined */
4082
        if (loglevel != 0) {
4083
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
4084
                    ADDRX "\n", sprn, sprn, ctx->nip);
4085
        }
4086
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
4087
               sprn, sprn, ctx->nip);
4088
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
4089
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
4090
    }
4091
}
4092

    
4093
/***                         Cache management                              ***/
4094
/* dcbf */
4095
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
4096
{
4097
    /* XXX: specification says this is treated as a load by the MMU */
4098
    TCGv t0 = tcg_temp_new();
4099
    gen_set_access_type(ACCESS_CACHE);
4100
    gen_addr_reg_index(t0, ctx);
4101
    gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4102
    tcg_temp_free(t0);
4103
}
4104

    
4105
/* dcbi (Supervisor only) */
4106
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
4107
{
4108
#if defined(CONFIG_USER_ONLY)
4109
    GEN_EXCP_PRIVOPC(ctx);
4110
#else
4111
    TCGv EA, val;
4112
    if (unlikely(!ctx->supervisor)) {
4113
        GEN_EXCP_PRIVOPC(ctx);
4114
        return;
4115
    }
4116
    EA = tcg_temp_new();
4117
    gen_set_access_type(ACCESS_CACHE);
4118
    gen_addr_reg_index(EA, ctx);
4119
    val = tcg_temp_new();
4120
    /* XXX: specification says this should be treated as a store by the MMU */
4121
    gen_qemu_ld8u(val, EA, ctx->mem_idx);
4122
    gen_qemu_st8(val, EA, ctx->mem_idx);
4123
    tcg_temp_free(val);
4124
    tcg_temp_free(EA);
4125
#endif
4126
}
4127

    
4128
/* dcdst */
4129
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
4130
{
4131
    /* XXX: specification say this is treated as a load by the MMU */
4132
    TCGv t0 = tcg_temp_new();
4133
    gen_set_access_type(ACCESS_CACHE);
4134
    gen_addr_reg_index(t0, ctx);
4135
    gen_qemu_ld8u(t0, t0, ctx->mem_idx);
4136
    tcg_temp_free(t0);
4137
}
4138

    
4139
/* dcbt */
4140
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
4141
{
4142
    /* interpreted as no-op */
4143
    /* XXX: specification say this is treated as a load by the MMU
4144
     *      but does not generate any exception
4145
     */
4146
}
4147

    
4148
/* dcbtst */
4149
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
4150
{
4151
    /* interpreted as no-op */
4152
    /* XXX: specification say this is treated as a load by the MMU
4153
     *      but does not generate any exception
4154
     */
4155
}
4156

    
4157
/* dcbz */
4158
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
4159
static GenOpFunc *gen_op_dcbz[4][NB_MEM_FUNCS] = {
4160
    /* 32 bytes cache line size */
4161
    {
4162
#define gen_op_dcbz_l32_le_raw        gen_op_dcbz_l32_raw
4163
#define gen_op_dcbz_l32_le_user       gen_op_dcbz_l32_user
4164
#define gen_op_dcbz_l32_le_kernel     gen_op_dcbz_l32_kernel
4165
#define gen_op_dcbz_l32_le_hypv       gen_op_dcbz_l32_hypv
4166
#define gen_op_dcbz_l32_le_64_raw     gen_op_dcbz_l32_64_raw
4167
#define gen_op_dcbz_l32_le_64_user    gen_op_dcbz_l32_64_user
4168
#define gen_op_dcbz_l32_le_64_kernel  gen_op_dcbz_l32_64_kernel
4169
#define gen_op_dcbz_l32_le_64_hypv    gen_op_dcbz_l32_64_hypv
4170
        GEN_MEM_FUNCS(dcbz_l32),
4171
    },
4172
    /* 64 bytes cache line size */
4173
    {
4174
#define gen_op_dcbz_l64_le_raw        gen_op_dcbz_l64_raw
4175
#define gen_op_dcbz_l64_le_user       gen_op_dcbz_l64_user
4176
#define gen_op_dcbz_l64_le_kernel     gen_op_dcbz_l64_kernel
4177
#define gen_op_dcbz_l64_le_hypv       gen_op_dcbz_l64_hypv
4178
#define gen_op_dcbz_l64_le_64_raw     gen_op_dcbz_l64_64_raw
4179
#define gen_op_dcbz_l64_le_64_user    gen_op_dcbz_l64_64_user
4180
#define gen_op_dcbz_l64_le_64_kernel  gen_op_dcbz_l64_64_kernel
4181
#define gen_op_dcbz_l64_le_64_hypv    gen_op_dcbz_l64_64_hypv
4182
        GEN_MEM_FUNCS(dcbz_l64),
4183
    },
4184
    /* 128 bytes cache line size */
4185
    {
4186
#define gen_op_dcbz_l128_le_raw       gen_op_dcbz_l128_raw
4187
#define gen_op_dcbz_l128_le_user      gen_op_dcbz_l128_user
4188
#define gen_op_dcbz_l128_le_kernel    gen_op_dcbz_l128_kernel
4189
#define gen_op_dcbz_l128_le_hypv      gen_op_dcbz_l128_hypv
4190
#define gen_op_dcbz_l128_le_64_raw    gen_op_dcbz_l128_64_raw
4191
#define gen_op_dcbz_l128_le_64_user   gen_op_dcbz_l128_64_user
4192
#define gen_op_dcbz_l128_le_64_kernel gen_op_dcbz_l128_64_kernel
4193
#define gen_op_dcbz_l128_le_64_hypv   gen_op_dcbz_l128_64_hypv
4194
        GEN_MEM_FUNCS(dcbz_l128),
4195
    },
4196
    /* tunable cache line size */
4197
    {
4198
#define gen_op_dcbz_le_raw            gen_op_dcbz_raw
4199
#define gen_op_dcbz_le_user           gen_op_dcbz_user
4200
#define gen_op_dcbz_le_kernel         gen_op_dcbz_kernel
4201
#define gen_op_dcbz_le_hypv           gen_op_dcbz_hypv
4202
#define gen_op_dcbz_le_64_raw         gen_op_dcbz_64_raw
4203
#define gen_op_dcbz_le_64_user        gen_op_dcbz_64_user
4204
#define gen_op_dcbz_le_64_kernel      gen_op_dcbz_64_kernel
4205
#define gen_op_dcbz_le_64_hypv        gen_op_dcbz_64_hypv
4206
        GEN_MEM_FUNCS(dcbz),
4207
    },
4208
};
4209

    
4210
static always_inline void handler_dcbz (DisasContext *ctx,
4211
                                        int dcache_line_size)
4212
{
4213
    int n;
4214

    
4215
    switch (dcache_line_size) {
4216
    case 32:
4217
        n = 0;
4218
        break;
4219
    case 64:
4220
        n = 1;
4221
        break;
4222
    case 128:
4223
        n = 2;
4224
        break;
4225
    default:
4226
        n = 3;
4227
        break;
4228
    }
4229
    op_dcbz(n);
4230
}
4231

    
4232
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
4233
{
4234
    gen_addr_reg_index(cpu_T[0], ctx);
4235
    handler_dcbz(ctx, ctx->dcache_line_size);
4236
    gen_op_check_reservation();
4237
}
4238

    
4239
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
4240
{
4241
    gen_addr_reg_index(cpu_T[0], ctx);
4242
    if (ctx->opcode & 0x00200000)
4243
        handler_dcbz(ctx, ctx->dcache_line_size);
4244
    else
4245
        handler_dcbz(ctx, -1);
4246
    gen_op_check_reservation();
4247
}
4248

    
4249
/* icbi */
4250
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
4251
#define gen_op_icbi_le_raw       gen_op_icbi_raw
4252
#define gen_op_icbi_le_user      gen_op_icbi_user
4253
#define gen_op_icbi_le_kernel    gen_op_icbi_kernel
4254
#define gen_op_icbi_le_hypv      gen_op_icbi_hypv
4255
#define gen_op_icbi_le_64_raw    gen_op_icbi_64_raw
4256
#define gen_op_icbi_le_64_user   gen_op_icbi_64_user
4257
#define gen_op_icbi_le_64_kernel gen_op_icbi_64_kernel
4258
#define gen_op_icbi_le_64_hypv   gen_op_icbi_64_hypv
4259
static GenOpFunc *gen_op_icbi[NB_MEM_FUNCS] = {
4260
    GEN_MEM_FUNCS(icbi),
4261
};
4262

    
4263
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI)
4264
{
4265
    /* NIP cannot be restored if the memory exception comes from an helper */
4266
    gen_update_nip(ctx, ctx->nip - 4);
4267
    gen_addr_reg_index(cpu_T[0], ctx);
4268
    op_icbi();
4269
}
4270

    
4271
/* Optional: */
4272
/* dcba */
4273
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
4274
{
4275
    /* interpreted as no-op */
4276
    /* XXX: specification say this is treated as a store by the MMU
4277
     *      but does not generate any exception
4278
     */
4279
}
4280

    
4281
/***                    Segment register manipulation                      ***/
4282
/* Supervisor only: */
4283
/* mfsr */
4284
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4285
{
4286
#if defined(CONFIG_USER_ONLY)
4287
    GEN_EXCP_PRIVREG(ctx);
4288
#else
4289
    if (unlikely(!ctx->supervisor)) {
4290
        GEN_EXCP_PRIVREG(ctx);
4291
        return;
4292
    }
4293
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4294
    gen_op_load_sr();
4295
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4296
#endif
4297
}
4298

    
4299
/* mfsrin */
4300
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
4301
{
4302
#if defined(CONFIG_USER_ONLY)
4303
    GEN_EXCP_PRIVREG(ctx);
4304
#else
4305
    if (unlikely(!ctx->supervisor)) {
4306
        GEN_EXCP_PRIVREG(ctx);
4307
        return;
4308
    }
4309
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4310
    gen_op_srli_T1(28);
4311
    gen_op_load_sr();
4312
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4313
#endif
4314
}
4315

    
4316
/* mtsr */
4317
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
4318
{
4319
#if defined(CONFIG_USER_ONLY)
4320
    GEN_EXCP_PRIVREG(ctx);
4321
#else
4322
    if (unlikely(!ctx->supervisor)) {
4323
        GEN_EXCP_PRIVREG(ctx);
4324
        return;
4325
    }
4326
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4327
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4328
    gen_op_store_sr();
4329
#endif
4330
}
4331

    
4332
/* mtsrin */
4333
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
4334
{
4335
#if defined(CONFIG_USER_ONLY)
4336
    GEN_EXCP_PRIVREG(ctx);
4337
#else
4338
    if (unlikely(!ctx->supervisor)) {
4339
        GEN_EXCP_PRIVREG(ctx);
4340
        return;
4341
    }
4342
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4343
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4344
    gen_op_srli_T1(28);
4345
    gen_op_store_sr();
4346
#endif
4347
}
4348

    
4349
#if defined(TARGET_PPC64)
4350
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4351
/* mfsr */
4352
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4353
{
4354
#if defined(CONFIG_USER_ONLY)
4355
    GEN_EXCP_PRIVREG(ctx);
4356
#else
4357
    if (unlikely(!ctx->supervisor)) {
4358
        GEN_EXCP_PRIVREG(ctx);
4359
        return;
4360
    }
4361
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4362
    gen_op_load_slb();
4363
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4364
#endif
4365
}
4366

    
4367
/* mfsrin */
4368
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4369
             PPC_SEGMENT_64B)
4370
{
4371
#if defined(CONFIG_USER_ONLY)
4372
    GEN_EXCP_PRIVREG(ctx);
4373
#else
4374
    if (unlikely(!ctx->supervisor)) {
4375
        GEN_EXCP_PRIVREG(ctx);
4376
        return;
4377
    }
4378
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4379
    gen_op_srli_T1(28);
4380
    gen_op_load_slb();
4381
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4382
#endif
4383
}
4384

    
4385
/* mtsr */
4386
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4387
{
4388
#if defined(CONFIG_USER_ONLY)
4389
    GEN_EXCP_PRIVREG(ctx);
4390
#else
4391
    if (unlikely(!ctx->supervisor)) {
4392
        GEN_EXCP_PRIVREG(ctx);
4393
        return;
4394
    }
4395
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4396
    tcg_gen_movi_tl(cpu_T[1], SR(ctx->opcode));
4397
    gen_op_store_slb();
4398
#endif
4399
}
4400

    
4401
/* mtsrin */
4402
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4403
             PPC_SEGMENT_64B)
4404
{
4405
#if defined(CONFIG_USER_ONLY)
4406
    GEN_EXCP_PRIVREG(ctx);
4407
#else
4408
    if (unlikely(!ctx->supervisor)) {
4409
        GEN_EXCP_PRIVREG(ctx);
4410
        return;
4411
    }
4412
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4413
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4414
    gen_op_srli_T1(28);
4415
    gen_op_store_slb();
4416
#endif
4417
}
4418
#endif /* defined(TARGET_PPC64) */
4419

    
4420
/***                      Lookaside buffer management                      ***/
4421
/* Optional & supervisor only: */
4422
/* tlbia */
4423
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
4424
{
4425
#if defined(CONFIG_USER_ONLY)
4426
    GEN_EXCP_PRIVOPC(ctx);
4427
#else
4428
    if (unlikely(!ctx->supervisor)) {
4429
        GEN_EXCP_PRIVOPC(ctx);
4430
        return;
4431
    }
4432
    gen_op_tlbia();
4433
#endif
4434
}
4435

    
4436
/* tlbie */
4437
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
4438
{
4439
#if defined(CONFIG_USER_ONLY)
4440
    GEN_EXCP_PRIVOPC(ctx);
4441
#else
4442
    if (unlikely(!ctx->supervisor)) {
4443
        GEN_EXCP_PRIVOPC(ctx);
4444
        return;
4445
    }
4446
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4447
#if defined(TARGET_PPC64)
4448
    if (ctx->sf_mode)
4449
        gen_op_tlbie_64();
4450
    else
4451
#endif
4452
        gen_op_tlbie();
4453
#endif
4454
}
4455

    
4456
/* tlbsync */
4457
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
4458
{
4459
#if defined(CONFIG_USER_ONLY)
4460
    GEN_EXCP_PRIVOPC(ctx);
4461
#else
4462
    if (unlikely(!ctx->supervisor)) {
4463
        GEN_EXCP_PRIVOPC(ctx);
4464
        return;
4465
    }
4466
    /* This has no effect: it should ensure that all previous
4467
     * tlbie have completed
4468
     */
4469
    GEN_STOP(ctx);
4470
#endif
4471
}
4472

    
4473
#if defined(TARGET_PPC64)
4474
/* slbia */
4475
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4476
{
4477
#if defined(CONFIG_USER_ONLY)
4478
    GEN_EXCP_PRIVOPC(ctx);
4479
#else
4480
    if (unlikely(!ctx->supervisor)) {
4481
        GEN_EXCP_PRIVOPC(ctx);
4482
        return;
4483
    }
4484
    gen_op_slbia();
4485
#endif
4486
}
4487

    
4488
/* slbie */
4489
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4490
{
4491
#if defined(CONFIG_USER_ONLY)
4492
    GEN_EXCP_PRIVOPC(ctx);
4493
#else
4494
    if (unlikely(!ctx->supervisor)) {
4495
        GEN_EXCP_PRIVOPC(ctx);
4496
        return;
4497
    }
4498
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4499
    gen_op_slbie();
4500
#endif
4501
}
4502
#endif
4503

    
4504
/***                              External control                         ***/
4505
/* Optional: */
4506
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
4507
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4508
static GenOpFunc *gen_op_eciwx[NB_MEM_FUNCS] = {
4509
    GEN_MEM_FUNCS(eciwx),
4510
};
4511
static GenOpFunc *gen_op_ecowx[NB_MEM_FUNCS] = {
4512
    GEN_MEM_FUNCS(ecowx),
4513
};
4514

    
4515
/* eciwx */
4516
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4517
{
4518
    /* Should check EAR[E] & alignment ! */
4519
    gen_set_access_type(ACCESS_RES);
4520
    gen_addr_reg_index(cpu_T[0], ctx);
4521
    op_eciwx();
4522
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4523
}
4524

    
4525
/* ecowx */
4526
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4527
{
4528
    /* Should check EAR[E] & alignment ! */
4529
    gen_addr_reg_index(cpu_T[0], ctx);
4530
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4531
    op_ecowx();
4532
}
4533

    
4534
/* PowerPC 601 specific instructions */
4535
/* abs - abs. */
4536
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4537
{
4538
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4539
    gen_op_POWER_abs();
4540
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4541
    if (unlikely(Rc(ctx->opcode) != 0))
4542
        gen_set_Rc0(ctx, cpu_T[0]);
4543
}
4544

    
4545
/* abso - abso. */
4546
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4547
{
4548
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4549
    gen_op_POWER_abso();
4550
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4551
    if (unlikely(Rc(ctx->opcode) != 0))
4552
        gen_set_Rc0(ctx, cpu_T[0]);
4553
}
4554

    
4555
/* clcs */
4556
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4557
{
4558
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4559
    gen_op_POWER_clcs();
4560
    /* Rc=1 sets CR0 to an undefined state */
4561
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4562
}
4563

    
4564
/* div - div. */
4565
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4566
{
4567
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4568
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4569
    gen_op_POWER_div();
4570
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4571
    if (unlikely(Rc(ctx->opcode) != 0))
4572
        gen_set_Rc0(ctx, cpu_T[0]);
4573
}
4574

    
4575
/* divo - divo. */
4576
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4577
{
4578
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4579
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4580
    gen_op_POWER_divo();
4581
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4582
    if (unlikely(Rc(ctx->opcode) != 0))
4583
        gen_set_Rc0(ctx, cpu_T[0]);
4584
}
4585

    
4586
/* divs - divs. */
4587
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4588
{
4589
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4590
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4591
    gen_op_POWER_divs();
4592
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4593
    if (unlikely(Rc(ctx->opcode) != 0))
4594
        gen_set_Rc0(ctx, cpu_T[0]);
4595
}
4596

    
4597
/* divso - divso. */
4598
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4599
{
4600
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4601
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4602
    gen_op_POWER_divso();
4603
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4604
    if (unlikely(Rc(ctx->opcode) != 0))
4605
        gen_set_Rc0(ctx, cpu_T[0]);
4606
}
4607

    
4608
/* doz - doz. */
4609
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4610
{
4611
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4612
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4613
    gen_op_POWER_doz();
4614
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4615
    if (unlikely(Rc(ctx->opcode) != 0))
4616
        gen_set_Rc0(ctx, cpu_T[0]);
4617
}
4618

    
4619
/* dozo - dozo. */
4620
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4621
{
4622
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4623
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4624
    gen_op_POWER_dozo();
4625
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4626
    if (unlikely(Rc(ctx->opcode) != 0))
4627
        gen_set_Rc0(ctx, cpu_T[0]);
4628
}
4629

    
4630
/* dozi */
4631
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4632
{
4633
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4634
    tcg_gen_movi_tl(cpu_T[1], SIMM(ctx->opcode));
4635
    gen_op_POWER_doz();
4636
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4637
}
4638

    
4639
/* As lscbx load from memory byte after byte, it's always endian safe.
4640
 * Original POWER is 32 bits only, define 64 bits ops as 32 bits ones
4641
 */
4642
#define op_POWER_lscbx(start, ra, rb)                                         \
4643
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4644
#define gen_op_POWER_lscbx_64_raw       gen_op_POWER_lscbx_raw
4645
#define gen_op_POWER_lscbx_64_user      gen_op_POWER_lscbx_user
4646
#define gen_op_POWER_lscbx_64_kernel    gen_op_POWER_lscbx_kernel
4647
#define gen_op_POWER_lscbx_64_hypv      gen_op_POWER_lscbx_hypv
4648
#define gen_op_POWER_lscbx_le_raw       gen_op_POWER_lscbx_raw
4649
#define gen_op_POWER_lscbx_le_user      gen_op_POWER_lscbx_user
4650
#define gen_op_POWER_lscbx_le_kernel    gen_op_POWER_lscbx_kernel
4651
#define gen_op_POWER_lscbx_le_hypv      gen_op_POWER_lscbx_hypv
4652
#define gen_op_POWER_lscbx_le_64_raw    gen_op_POWER_lscbx_raw
4653
#define gen_op_POWER_lscbx_le_64_user   gen_op_POWER_lscbx_user
4654
#define gen_op_POWER_lscbx_le_64_kernel gen_op_POWER_lscbx_kernel
4655
#define gen_op_POWER_lscbx_le_64_hypv   gen_op_POWER_lscbx_hypv
4656
static GenOpFunc3 *gen_op_POWER_lscbx[NB_MEM_FUNCS] = {
4657
    GEN_MEM_FUNCS(POWER_lscbx),
4658
};
4659

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

    
4666
    gen_addr_reg_index(cpu_T[0], ctx);
4667
    if (ra == 0) {
4668
        ra = rb;
4669
    }
4670
    /* NIP cannot be restored if the memory exception comes from an helper */
4671
    gen_update_nip(ctx, ctx->nip - 4);
4672
    tcg_gen_andi_tl(cpu_T[1], cpu_xer, 0x7F);
4673
    tcg_gen_shri_tl(cpu_T[2], cpu_xer, XER_CMP);
4674
    tcg_gen_andi_tl(cpu_T[2], cpu_T[2], 0xFF);
4675
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
4676
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
4677
    tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
4678
    if (unlikely(Rc(ctx->opcode) != 0))
4679
        gen_set_Rc0(ctx, cpu_T[0]);
4680
}
4681

    
4682
/* maskg - maskg. */
4683
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4684
{
4685
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4686
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4687
    gen_op_POWER_maskg();
4688
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4689
    if (unlikely(Rc(ctx->opcode) != 0))
4690
        gen_set_Rc0(ctx, cpu_T[0]);
4691
}
4692

    
4693
/* maskir - maskir. */
4694
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4695
{
4696
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4697
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
4698
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4699
    gen_op_POWER_maskir();
4700
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4701
    if (unlikely(Rc(ctx->opcode) != 0))
4702
        gen_set_Rc0(ctx, cpu_T[0]);
4703
}
4704

    
4705
/* mul - mul. */
4706
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4707
{
4708
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4709
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4710
    gen_op_POWER_mul();
4711
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4712
    if (unlikely(Rc(ctx->opcode) != 0))
4713
        gen_set_Rc0(ctx, cpu_T[0]);
4714
}
4715

    
4716
/* mulo - mulo. */
4717
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4718
{
4719
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4720
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4721
    gen_op_POWER_mulo();
4722
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4723
    if (unlikely(Rc(ctx->opcode) != 0))
4724
        gen_set_Rc0(ctx, cpu_T[0]);
4725
}
4726

    
4727
/* nabs - nabs. */
4728
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4729
{
4730
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4731
    gen_op_POWER_nabs();
4732
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4733
    if (unlikely(Rc(ctx->opcode) != 0))
4734
        gen_set_Rc0(ctx, cpu_T[0]);
4735
}
4736

    
4737
/* nabso - nabso. */
4738
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4739
{
4740
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4741
    gen_op_POWER_nabso();
4742
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4743
    if (unlikely(Rc(ctx->opcode) != 0))
4744
        gen_set_Rc0(ctx, cpu_T[0]);
4745
}
4746

    
4747
/* rlmi - rlmi. */
4748
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4749
{
4750
    uint32_t mb, me;
4751

    
4752
    mb = MB(ctx->opcode);
4753
    me = ME(ctx->opcode);
4754
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4755
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4756
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4757
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
4758
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4759
    if (unlikely(Rc(ctx->opcode) != 0))
4760
        gen_set_Rc0(ctx, cpu_T[0]);
4761
}
4762

    
4763
/* rrib - rrib. */
4764
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4765
{
4766
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4767
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rA(ctx->opcode)]);
4768
    tcg_gen_mov_tl(cpu_T[2], cpu_gpr[rB(ctx->opcode)]);
4769
    gen_op_POWER_rrib();
4770
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4771
    if (unlikely(Rc(ctx->opcode) != 0))
4772
        gen_set_Rc0(ctx, cpu_T[0]);
4773
}
4774

    
4775
/* sle - sle. */
4776
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4777
{
4778
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4779
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4780
    gen_op_POWER_sle();
4781
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4782
    if (unlikely(Rc(ctx->opcode) != 0))
4783
        gen_set_Rc0(ctx, cpu_T[0]);
4784
}
4785

    
4786
/* sleq - sleq. */
4787
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4788
{
4789
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4790
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4791
    gen_op_POWER_sleq();
4792
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4793
    if (unlikely(Rc(ctx->opcode) != 0))
4794
        gen_set_Rc0(ctx, cpu_T[0]);
4795
}
4796

    
4797
/* sliq - sliq. */
4798
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4799
{
4800
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4801
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4802
    gen_op_POWER_sle();
4803
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4804
    if (unlikely(Rc(ctx->opcode) != 0))
4805
        gen_set_Rc0(ctx, cpu_T[0]);
4806
}
4807

    
4808
/* slliq - slliq. */
4809
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4810
{
4811
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4812
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4813
    gen_op_POWER_sleq();
4814
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4815
    if (unlikely(Rc(ctx->opcode) != 0))
4816
        gen_set_Rc0(ctx, cpu_T[0]);
4817
}
4818

    
4819
/* sllq - sllq. */
4820
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4821
{
4822
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4823
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4824
    gen_op_POWER_sllq();
4825
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4826
    if (unlikely(Rc(ctx->opcode) != 0))
4827
        gen_set_Rc0(ctx, cpu_T[0]);
4828
}
4829

    
4830
/* slq - slq. */
4831
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4832
{
4833
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4834
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4835
    gen_op_POWER_slq();
4836
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4837
    if (unlikely(Rc(ctx->opcode) != 0))
4838
        gen_set_Rc0(ctx, cpu_T[0]);
4839
}
4840

    
4841
/* sraiq - sraiq. */
4842
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4843
{
4844
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4845
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4846
    gen_op_POWER_sraq();
4847
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4848
    if (unlikely(Rc(ctx->opcode) != 0))
4849
        gen_set_Rc0(ctx, cpu_T[0]);
4850
}
4851

    
4852
/* sraq - sraq. */
4853
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4854
{
4855
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4856
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4857
    gen_op_POWER_sraq();
4858
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4859
    if (unlikely(Rc(ctx->opcode) != 0))
4860
        gen_set_Rc0(ctx, cpu_T[0]);
4861
}
4862

    
4863
/* sre - sre. */
4864
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4865
{
4866
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4867
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4868
    gen_op_POWER_sre();
4869
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4870
    if (unlikely(Rc(ctx->opcode) != 0))
4871
        gen_set_Rc0(ctx, cpu_T[0]);
4872
}
4873

    
4874
/* srea - srea. */
4875
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4876
{
4877
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4878
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4879
    gen_op_POWER_srea();
4880
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4881
    if (unlikely(Rc(ctx->opcode) != 0))
4882
        gen_set_Rc0(ctx, cpu_T[0]);
4883
}
4884

    
4885
/* sreq */
4886
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4887
{
4888
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4889
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4890
    gen_op_POWER_sreq();
4891
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4892
    if (unlikely(Rc(ctx->opcode) != 0))
4893
        gen_set_Rc0(ctx, cpu_T[0]);
4894
}
4895

    
4896
/* sriq */
4897
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4898
{
4899
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4900
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4901
    gen_op_POWER_srq();
4902
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4903
    if (unlikely(Rc(ctx->opcode) != 0))
4904
        gen_set_Rc0(ctx, cpu_T[0]);
4905
}
4906

    
4907
/* srliq */
4908
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4909
{
4910
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4911
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4912
    tcg_gen_movi_tl(cpu_T[1], SH(ctx->opcode));
4913
    gen_op_POWER_srlq();
4914
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4915
    if (unlikely(Rc(ctx->opcode) != 0))
4916
        gen_set_Rc0(ctx, cpu_T[0]);
4917
}
4918

    
4919
/* srlq */
4920
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
4921
{
4922
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4923
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4924
    gen_op_POWER_srlq();
4925
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4926
    if (unlikely(Rc(ctx->opcode) != 0))
4927
        gen_set_Rc0(ctx, cpu_T[0]);
4928
}
4929

    
4930
/* srq */
4931
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
4932
{
4933
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
4934
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
4935
    gen_op_POWER_srq();
4936
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
4937
    if (unlikely(Rc(ctx->opcode) != 0))
4938
        gen_set_Rc0(ctx, cpu_T[0]);
4939
}
4940

    
4941
/* PowerPC 602 specific instructions */
4942
/* dsa  */
4943
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
4944
{
4945
    /* XXX: TODO */
4946
    GEN_EXCP_INVAL(ctx);
4947
}
4948

    
4949
/* esa */
4950
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
4951
{
4952
    /* XXX: TODO */
4953
    GEN_EXCP_INVAL(ctx);
4954
}
4955

    
4956
/* mfrom */
4957
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
4958
{
4959
#if defined(CONFIG_USER_ONLY)
4960
    GEN_EXCP_PRIVOPC(ctx);
4961
#else
4962
    if (unlikely(!ctx->supervisor)) {
4963
        GEN_EXCP_PRIVOPC(ctx);
4964
        return;
4965
    }
4966
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
4967
    gen_op_602_mfrom();
4968
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
4969
#endif
4970
}
4971

    
4972
/* 602 - 603 - G2 TLB management */
4973
/* tlbld */
4974
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4975
{
4976
#if defined(CONFIG_USER_ONLY)
4977
    GEN_EXCP_PRIVOPC(ctx);
4978
#else
4979
    if (unlikely(!ctx->supervisor)) {
4980
        GEN_EXCP_PRIVOPC(ctx);
4981
        return;
4982
    }
4983
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4984
    gen_op_6xx_tlbld();
4985
#endif
4986
}
4987

    
4988
/* tlbli */
4989
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4990
{
4991
#if defined(CONFIG_USER_ONLY)
4992
    GEN_EXCP_PRIVOPC(ctx);
4993
#else
4994
    if (unlikely(!ctx->supervisor)) {
4995
        GEN_EXCP_PRIVOPC(ctx);
4996
        return;
4997
    }
4998
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
4999
    gen_op_6xx_tlbli();
5000
#endif
5001
}
5002

    
5003
/* 74xx TLB management */
5004
/* tlbld */
5005
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
5006
{
5007
#if defined(CONFIG_USER_ONLY)
5008
    GEN_EXCP_PRIVOPC(ctx);
5009
#else
5010
    if (unlikely(!ctx->supervisor)) {
5011
        GEN_EXCP_PRIVOPC(ctx);
5012
        return;
5013
    }
5014
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
5015
    gen_op_74xx_tlbld();
5016
#endif
5017
}
5018

    
5019
/* tlbli */
5020
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
5021
{
5022
#if defined(CONFIG_USER_ONLY)
5023
    GEN_EXCP_PRIVOPC(ctx);
5024
#else
5025
    if (unlikely(!ctx->supervisor)) {
5026
        GEN_EXCP_PRIVOPC(ctx);
5027
        return;
5028
    }
5029
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rB(ctx->opcode)]);
5030
    gen_op_74xx_tlbli();
5031
#endif
5032
}
5033

    
5034
/* POWER instructions not in PowerPC 601 */
5035
/* clf */
5036
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
5037
{
5038
    /* Cache line flush: implemented as no-op */
5039
}
5040

    
5041
/* cli */
5042
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
5043
{
5044
    /* Cache line invalidate: privileged and treated as no-op */
5045
#if defined(CONFIG_USER_ONLY)
5046
    GEN_EXCP_PRIVOPC(ctx);
5047
#else
5048
    if (unlikely(!ctx->supervisor)) {
5049
        GEN_EXCP_PRIVOPC(ctx);
5050
        return;
5051
    }
5052
#endif
5053
}
5054

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

    
5061
GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
5062
{
5063
#if defined(CONFIG_USER_ONLY)
5064
    GEN_EXCP_PRIVOPC(ctx);
5065
#else
5066
    if (unlikely(!ctx->supervisor)) {
5067
        GEN_EXCP_PRIVOPC(ctx);
5068
        return;
5069
    }
5070
    int ra = rA(ctx->opcode);
5071
    int rd = rD(ctx->opcode);
5072

    
5073
    gen_addr_reg_index(cpu_T[0], ctx);
5074
    gen_op_POWER_mfsri();
5075
    tcg_gen_mov_tl(cpu_gpr[rd], cpu_T[0]);
5076
    if (ra != 0 && ra != rd)
5077
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[1]);
5078
#endif
5079
}
5080

    
5081
GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
5082
{
5083
#if defined(CONFIG_USER_ONLY)
5084
    GEN_EXCP_PRIVOPC(ctx);
5085
#else
5086
    if (unlikely(!ctx->supervisor)) {
5087
        GEN_EXCP_PRIVOPC(ctx);
5088
        return;
5089
    }
5090
    gen_addr_reg_index(cpu_T[0], ctx);
5091
    gen_op_POWER_rac();
5092
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5093
#endif
5094
}
5095

    
5096
GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
5097
{
5098
#if defined(CONFIG_USER_ONLY)
5099
    GEN_EXCP_PRIVOPC(ctx);
5100
#else
5101
    if (unlikely(!ctx->supervisor)) {
5102
        GEN_EXCP_PRIVOPC(ctx);
5103
        return;
5104
    }
5105
    gen_op_POWER_rfsvc();
5106
    GEN_SYNC(ctx);
5107
#endif
5108
}
5109

    
5110
/* svc is not implemented for now */
5111

    
5112
/* POWER2 specific instructions */
5113
/* Quad manipulation (load/store two floats at a time) */
5114
/* Original POWER2 is 32 bits only, define 64 bits ops as 32 bits ones */
5115
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
5116
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
5117
#define gen_op_POWER2_lfq_64_raw        gen_op_POWER2_lfq_raw
5118
#define gen_op_POWER2_lfq_64_user       gen_op_POWER2_lfq_user
5119
#define gen_op_POWER2_lfq_64_kernel     gen_op_POWER2_lfq_kernel
5120
#define gen_op_POWER2_lfq_64_hypv       gen_op_POWER2_lfq_hypv
5121
#define gen_op_POWER2_lfq_le_64_raw     gen_op_POWER2_lfq_le_raw
5122
#define gen_op_POWER2_lfq_le_64_user    gen_op_POWER2_lfq_le_user
5123
#define gen_op_POWER2_lfq_le_64_kernel  gen_op_POWER2_lfq_le_kernel
5124
#define gen_op_POWER2_lfq_le_64_hypv    gen_op_POWER2_lfq_le_hypv
5125
#define gen_op_POWER2_stfq_64_raw       gen_op_POWER2_stfq_raw
5126
#define gen_op_POWER2_stfq_64_user      gen_op_POWER2_stfq_user
5127
#define gen_op_POWER2_stfq_64_kernel    gen_op_POWER2_stfq_kernel
5128
#define gen_op_POWER2_stfq_64_hypv      gen_op_POWER2_stfq_hypv
5129
#define gen_op_POWER2_stfq_le_64_raw    gen_op_POWER2_stfq_le_raw
5130
#define gen_op_POWER2_stfq_le_64_user   gen_op_POWER2_stfq_le_user
5131
#define gen_op_POWER2_stfq_le_64_kernel gen_op_POWER2_stfq_le_kernel
5132
#define gen_op_POWER2_stfq_le_64_hypv   gen_op_POWER2_stfq_le_hypv
5133
static GenOpFunc *gen_op_POWER2_lfq[NB_MEM_FUNCS] = {
5134
    GEN_MEM_FUNCS(POWER2_lfq),
5135
};
5136
static GenOpFunc *gen_op_POWER2_stfq[NB_MEM_FUNCS] = {
5137
    GEN_MEM_FUNCS(POWER2_stfq),
5138
};
5139

    
5140
/* lfq */
5141
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5142
{
5143
    /* NIP cannot be restored if the memory exception comes from an helper */
5144
    gen_update_nip(ctx, ctx->nip - 4);
5145
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5146
    op_POWER2_lfq();
5147
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5148
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5149
}
5150

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

    
5156
    /* NIP cannot be restored if the memory exception comes from an helper */
5157
    gen_update_nip(ctx, ctx->nip - 4);
5158
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5159
    op_POWER2_lfq();
5160
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5161
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5162
    if (ra != 0)
5163
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5164
}
5165

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

    
5171
    /* NIP cannot be restored if the memory exception comes from an helper */
5172
    gen_update_nip(ctx, ctx->nip - 4);
5173
    gen_addr_reg_index(cpu_T[0], ctx);
5174
    op_POWER2_lfq();
5175
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5176
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5177
    if (ra != 0)
5178
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5179
}
5180

    
5181
/* lfqx */
5182
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
5183
{
5184
    /* NIP cannot be restored if the memory exception comes from an helper */
5185
    gen_update_nip(ctx, ctx->nip - 4);
5186
    gen_addr_reg_index(cpu_T[0], ctx);
5187
    op_POWER2_lfq();
5188
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode)], cpu_FT[0]);
5189
    tcg_gen_mov_i64(cpu_fpr[rD(ctx->opcode) + 1], cpu_FT[1]);
5190
}
5191

    
5192
/* stfq */
5193
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
5194
{
5195
    /* NIP cannot be restored if the memory exception comes from an helper */
5196
    gen_update_nip(ctx, ctx->nip - 4);
5197
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5198
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5199
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5200
    op_POWER2_stfq();
5201
}
5202

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

    
5208
    /* NIP cannot be restored if the memory exception comes from an helper */
5209
    gen_update_nip(ctx, ctx->nip - 4);
5210
    gen_addr_imm_index(cpu_T[0], ctx, 0);
5211
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5212
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5213
    op_POWER2_stfq();
5214
    if (ra != 0)
5215
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5216
}
5217

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

    
5223
    /* NIP cannot be restored if the memory exception comes from an helper */
5224
    gen_update_nip(ctx, ctx->nip - 4);
5225
    gen_addr_reg_index(cpu_T[0], ctx);
5226
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5227
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5228
    op_POWER2_stfq();
5229
    if (ra != 0)
5230
        tcg_gen_mov_tl(cpu_gpr[ra], cpu_T[0]);
5231
}
5232

    
5233
/* stfqx */
5234
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5235
{
5236
    /* NIP cannot be restored if the memory exception comes from an helper */
5237
    gen_update_nip(ctx, ctx->nip - 4);
5238
    gen_addr_reg_index(cpu_T[0], ctx);
5239
    tcg_gen_mov_i64(cpu_FT[0], cpu_fpr[rS(ctx->opcode)]);
5240
    tcg_gen_mov_i64(cpu_FT[1], cpu_fpr[rS(ctx->opcode) + 1]);
5241
    op_POWER2_stfq();
5242
}
5243

    
5244
/* BookE specific instructions */
5245
/* XXX: not implemented on 440 ? */
5246
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI)
5247
{
5248
    /* XXX: TODO */
5249
    GEN_EXCP_INVAL(ctx);
5250
}
5251

    
5252
/* XXX: not implemented on 440 ? */
5253
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA)
5254
{
5255
#if defined(CONFIG_USER_ONLY)
5256
    GEN_EXCP_PRIVOPC(ctx);
5257
#else
5258
    if (unlikely(!ctx->supervisor)) {
5259
        GEN_EXCP_PRIVOPC(ctx);
5260
        return;
5261
    }
5262
    gen_addr_reg_index(cpu_T[0], ctx);
5263
    /* Use the same micro-ops as for tlbie */
5264
#if defined(TARGET_PPC64)
5265
    if (ctx->sf_mode)
5266
        gen_op_tlbie_64();
5267
    else
5268
#endif
5269
        gen_op_tlbie();
5270
#endif
5271
}
5272

    
5273
/* All 405 MAC instructions are translated here */
5274
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5275
                                                int opc2, int opc3,
5276
                                                int ra, int rb, int rt, int Rc)
5277
{
5278
    TCGv t0, t1;
5279

    
5280
    t0 = tcg_temp_local_new();
5281
    t1 = tcg_temp_local_new();
5282

    
5283
    switch (opc3 & 0x0D) {
5284
    case 0x05:
5285
        /* macchw    - macchw.    - macchwo   - macchwo.   */
5286
        /* macchws   - macchws.   - macchwso  - macchwso.  */
5287
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5288
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5289
        /* mulchw - mulchw. */
5290
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5291
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5292
        tcg_gen_ext16s_tl(t1, t1);
5293
        break;
5294
    case 0x04:
5295
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5296
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5297
        /* mulchwu - mulchwu. */
5298
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5299
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5300
        tcg_gen_ext16u_tl(t1, t1);
5301
        break;
5302
    case 0x01:
5303
        /* machhw    - machhw.    - machhwo   - machhwo.   */
5304
        /* machhws   - machhws.   - machhwso  - machhwso.  */
5305
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5306
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5307
        /* mulhhw - mulhhw. */
5308
        tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5309
        tcg_gen_ext16s_tl(t0, t0);
5310
        tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5311
        tcg_gen_ext16s_tl(t1, t1);
5312
        break;
5313
    case 0x00:
5314
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5315
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5316
        /* mulhhwu - mulhhwu. */
5317
        tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5318
        tcg_gen_ext16u_tl(t0, t0);
5319
        tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5320
        tcg_gen_ext16u_tl(t1, t1);
5321
        break;
5322
    case 0x0D:
5323
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5324
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5325
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5326
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5327
        /* mullhw - mullhw. */
5328
        tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5329
        tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5330
        break;
5331
    case 0x0C:
5332
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5333
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5334
        /* mullhwu - mullhwu. */
5335
        tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5336
        tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5337
        break;
5338
    }
5339
    if (opc2 & 0x04) {
5340
        /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5341
        tcg_gen_mul_tl(t1, t0, t1);
5342
        if (opc2 & 0x02) {
5343
            /* nmultiply-and-accumulate (0x0E) */
5344
            tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5345
        } else {
5346
            /* multiply-and-accumulate (0x0C) */
5347
            tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5348
        }
5349

    
5350
        if (opc3 & 0x12) {
5351
            /* Check overflow and/or saturate */
5352
            int l1 = gen_new_label();
5353

    
5354
            if (opc3 & 0x10) {
5355
                /* Start with XER OV disabled, the most likely case */
5356
                tcg_gen_andi_tl(cpu_xer, cpu_xer, ~(1 << XER_OV));
5357
            }
5358
            if (opc3 & 0x01) {
5359
                /* Signed */
5360
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5361
                tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5362
                tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5363
                tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5364
                if (opc3 & 0x02) {
5365
                    /* Saturate */
5366
                    tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5367
                    tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5368
                }
5369
            } else {
5370
                /* Unsigned */
5371
                tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5372
                if (opc3 & 0x02) {
5373
                    /* Saturate */
5374
                    tcg_gen_movi_tl(t0, UINT32_MAX);
5375
                }
5376
            }
5377
            if (opc3 & 0x10) {
5378
                /* Check overflow */
5379
                tcg_gen_ori_tl(cpu_xer, cpu_xer, (1 << XER_OV) | (1 << XER_SO));
5380
            }
5381
            gen_set_label(l1);
5382
            tcg_gen_mov_tl(cpu_gpr[rt], t0);
5383
        }
5384
    } else {
5385
        tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5386
    }
5387
    tcg_temp_free(t0);
5388
    tcg_temp_free(t1);
5389
    if (unlikely(Rc) != 0) {
5390
        /* Update Rc0 */
5391
        gen_set_Rc0(ctx, cpu_gpr[rt]);
5392
    }
5393
}
5394

    
5395
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
5396
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5397
{                                                                             \
5398
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
5399
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
5400
}
5401

    
5402
/* macchw    - macchw.    */
5403
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5404
/* macchwo   - macchwo.   */
5405
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5406
/* macchws   - macchws.   */
5407
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5408
/* macchwso  - macchwso.  */
5409
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5410
/* macchwsu  - macchwsu.  */
5411
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5412
/* macchwsuo - macchwsuo. */
5413
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5414
/* macchwu   - macchwu.   */
5415
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5416
/* macchwuo  - macchwuo.  */
5417
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5418
/* machhw    - machhw.    */
5419
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5420
/* machhwo   - machhwo.   */
5421
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5422
/* machhws   - machhws.   */
5423
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5424
/* machhwso  - machhwso.  */
5425
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5426
/* machhwsu  - machhwsu.  */
5427
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5428
/* machhwsuo - machhwsuo. */
5429
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5430
/* machhwu   - machhwu.   */
5431
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5432
/* machhwuo  - machhwuo.  */
5433
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5434
/* maclhw    - maclhw.    */
5435
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5436
/* maclhwo   - maclhwo.   */
5437
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5438
/* maclhws   - maclhws.   */
5439
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5440
/* maclhwso  - maclhwso.  */
5441
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5442
/* maclhwu   - maclhwu.   */
5443
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5444
/* maclhwuo  - maclhwuo.  */
5445
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5446
/* maclhwsu  - maclhwsu.  */
5447
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5448
/* maclhwsuo - maclhwsuo. */
5449
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5450
/* nmacchw   - nmacchw.   */
5451
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5452
/* nmacchwo  - nmacchwo.  */
5453
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5454
/* nmacchws  - nmacchws.  */
5455
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5456
/* nmacchwso - nmacchwso. */
5457
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5458
/* nmachhw   - nmachhw.   */
5459
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5460
/* nmachhwo  - nmachhwo.  */
5461
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5462
/* nmachhws  - nmachhws.  */
5463
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5464
/* nmachhwso - nmachhwso. */
5465
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5466
/* nmaclhw   - nmaclhw.   */
5467
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5468
/* nmaclhwo  - nmaclhwo.  */
5469
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5470
/* nmaclhws  - nmaclhws.  */
5471
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5472
/* nmaclhwso - nmaclhwso. */
5473
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5474

    
5475
/* mulchw  - mulchw.  */
5476
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5477
/* mulchwu - mulchwu. */
5478
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5479
/* mulhhw  - mulhhw.  */
5480
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5481
/* mulhhwu - mulhhwu. */
5482
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5483
/* mullhw  - mullhw.  */
5484
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5485
/* mullhwu - mullhwu. */
5486
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5487

    
5488
/* mfdcr */
5489
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR)
5490
{
5491
#if defined(CONFIG_USER_ONLY)
5492
    GEN_EXCP_PRIVREG(ctx);
5493
#else
5494
    uint32_t dcrn = SPR(ctx->opcode);
5495

    
5496
    if (unlikely(!ctx->supervisor)) {
5497
        GEN_EXCP_PRIVREG(ctx);
5498
        return;
5499
    }
5500
    tcg_gen_movi_tl(cpu_T[0], dcrn);
5501
    gen_op_load_dcr();
5502
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5503
#endif
5504
}
5505

    
5506
/* mtdcr */
5507
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR)
5508
{
5509
#if defined(CONFIG_USER_ONLY)
5510
    GEN_EXCP_PRIVREG(ctx);
5511
#else
5512
    uint32_t dcrn = SPR(ctx->opcode);
5513

    
5514
    if (unlikely(!ctx->supervisor)) {
5515
        GEN_EXCP_PRIVREG(ctx);
5516
        return;
5517
    }
5518
    tcg_gen_movi_tl(cpu_T[0], dcrn);
5519
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5520
    gen_op_store_dcr();
5521
#endif
5522
}
5523

    
5524
/* mfdcrx */
5525
/* XXX: not implemented on 440 ? */
5526
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX)
5527
{
5528
#if defined(CONFIG_USER_ONLY)
5529
    GEN_EXCP_PRIVREG(ctx);
5530
#else
5531
    if (unlikely(!ctx->supervisor)) {
5532
        GEN_EXCP_PRIVREG(ctx);
5533
        return;
5534
    }
5535
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5536
    gen_op_load_dcr();
5537
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5538
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5539
#endif
5540
}
5541

    
5542
/* mtdcrx */
5543
/* XXX: not implemented on 440 ? */
5544
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX)
5545
{
5546
#if defined(CONFIG_USER_ONLY)
5547
    GEN_EXCP_PRIVREG(ctx);
5548
#else
5549
    if (unlikely(!ctx->supervisor)) {
5550
        GEN_EXCP_PRIVREG(ctx);
5551
        return;
5552
    }
5553
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5554
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5555
    gen_op_store_dcr();
5556
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5557
#endif
5558
}
5559

    
5560
/* mfdcrux (PPC 460) : user-mode access to DCR */
5561
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5562
{
5563
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5564
    gen_op_load_dcr();
5565
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5566
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5567
}
5568

    
5569
/* mtdcrux (PPC 460) : user-mode access to DCR */
5570
GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX)
5571
{
5572
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5573
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5574
    gen_op_store_dcr();
5575
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5576
}
5577

    
5578
/* dccci */
5579
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5580
{
5581
#if defined(CONFIG_USER_ONLY)
5582
    GEN_EXCP_PRIVOPC(ctx);
5583
#else
5584
    if (unlikely(!ctx->supervisor)) {
5585
        GEN_EXCP_PRIVOPC(ctx);
5586
        return;
5587
    }
5588
    /* interpreted as no-op */
5589
#endif
5590
}
5591

    
5592
/* dcread */
5593
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5594
{
5595
#if defined(CONFIG_USER_ONLY)
5596
    GEN_EXCP_PRIVOPC(ctx);
5597
#else
5598
    TCGv EA, val;
5599
    if (unlikely(!ctx->supervisor)) {
5600
        GEN_EXCP_PRIVOPC(ctx);
5601
        return;
5602
    }
5603
    EA = tcg_temp_new();
5604
    gen_set_access_type(ACCESS_CACHE);
5605
    gen_addr_reg_index(EA, ctx);
5606
    val = tcg_temp_new();
5607
    gen_qemu_ld32u(val, EA, ctx->mem_idx);
5608
    tcg_temp_free(val);
5609
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5610
    tcg_temp_free(EA);
5611
#endif
5612
}
5613

    
5614
/* icbt */
5615
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5616
{
5617
    /* interpreted as no-op */
5618
    /* XXX: specification say this is treated as a load by the MMU
5619
     *      but does not generate any exception
5620
     */
5621
}
5622

    
5623
/* iccci */
5624
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5625
{
5626
#if defined(CONFIG_USER_ONLY)
5627
    GEN_EXCP_PRIVOPC(ctx);
5628
#else
5629
    if (unlikely(!ctx->supervisor)) {
5630
        GEN_EXCP_PRIVOPC(ctx);
5631
        return;
5632
    }
5633
    /* interpreted as no-op */
5634
#endif
5635
}
5636

    
5637
/* icread */
5638
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5639
{
5640
#if defined(CONFIG_USER_ONLY)
5641
    GEN_EXCP_PRIVOPC(ctx);
5642
#else
5643
    if (unlikely(!ctx->supervisor)) {
5644
        GEN_EXCP_PRIVOPC(ctx);
5645
        return;
5646
    }
5647
    /* interpreted as no-op */
5648
#endif
5649
}
5650

    
5651
/* rfci (supervisor only) */
5652
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5653
{
5654
#if defined(CONFIG_USER_ONLY)
5655
    GEN_EXCP_PRIVOPC(ctx);
5656
#else
5657
    if (unlikely(!ctx->supervisor)) {
5658
        GEN_EXCP_PRIVOPC(ctx);
5659
        return;
5660
    }
5661
    /* Restore CPU state */
5662
    gen_op_40x_rfci();
5663
    GEN_SYNC(ctx);
5664
#endif
5665
}
5666

    
5667
GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5668
{
5669
#if defined(CONFIG_USER_ONLY)
5670
    GEN_EXCP_PRIVOPC(ctx);
5671
#else
5672
    if (unlikely(!ctx->supervisor)) {
5673
        GEN_EXCP_PRIVOPC(ctx);
5674
        return;
5675
    }
5676
    /* Restore CPU state */
5677
    gen_op_rfci();
5678
    GEN_SYNC(ctx);
5679
#endif
5680
}
5681

    
5682
/* BookE specific */
5683
/* XXX: not implemented on 440 ? */
5684
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI)
5685
{
5686
#if defined(CONFIG_USER_ONLY)
5687
    GEN_EXCP_PRIVOPC(ctx);
5688
#else
5689
    if (unlikely(!ctx->supervisor)) {
5690
        GEN_EXCP_PRIVOPC(ctx);
5691
        return;
5692
    }
5693
    /* Restore CPU state */
5694
    gen_op_rfdi();
5695
    GEN_SYNC(ctx);
5696
#endif
5697
}
5698

    
5699
/* XXX: not implemented on 440 ? */
5700
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5701
{
5702
#if defined(CONFIG_USER_ONLY)
5703
    GEN_EXCP_PRIVOPC(ctx);
5704
#else
5705
    if (unlikely(!ctx->supervisor)) {
5706
        GEN_EXCP_PRIVOPC(ctx);
5707
        return;
5708
    }
5709
    /* Restore CPU state */
5710
    gen_op_rfmci();
5711
    GEN_SYNC(ctx);
5712
#endif
5713
}
5714

    
5715
/* TLB management - PowerPC 405 implementation */
5716
/* tlbre */
5717
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5718
{
5719
#if defined(CONFIG_USER_ONLY)
5720
    GEN_EXCP_PRIVOPC(ctx);
5721
#else
5722
    if (unlikely(!ctx->supervisor)) {
5723
        GEN_EXCP_PRIVOPC(ctx);
5724
        return;
5725
    }
5726
    switch (rB(ctx->opcode)) {
5727
    case 0:
5728
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5729
        gen_op_4xx_tlbre_hi();
5730
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5731
        break;
5732
    case 1:
5733
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5734
        gen_op_4xx_tlbre_lo();
5735
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5736
        break;
5737
    default:
5738
        GEN_EXCP_INVAL(ctx);
5739
        break;
5740
    }
5741
#endif
5742
}
5743

    
5744
/* tlbsx - tlbsx. */
5745
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5746
{
5747
#if defined(CONFIG_USER_ONLY)
5748
    GEN_EXCP_PRIVOPC(ctx);
5749
#else
5750
    if (unlikely(!ctx->supervisor)) {
5751
        GEN_EXCP_PRIVOPC(ctx);
5752
        return;
5753
    }
5754
    gen_addr_reg_index(cpu_T[0], ctx);
5755
    gen_op_4xx_tlbsx();
5756
    if (Rc(ctx->opcode))
5757
        gen_op_4xx_tlbsx_check();
5758
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5759
#endif
5760
}
5761

    
5762
/* tlbwe */
5763
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5764
{
5765
#if defined(CONFIG_USER_ONLY)
5766
    GEN_EXCP_PRIVOPC(ctx);
5767
#else
5768
    if (unlikely(!ctx->supervisor)) {
5769
        GEN_EXCP_PRIVOPC(ctx);
5770
        return;
5771
    }
5772
    switch (rB(ctx->opcode)) {
5773
    case 0:
5774
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5775
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5776
        gen_op_4xx_tlbwe_hi();
5777
        break;
5778
    case 1:
5779
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5780
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5781
        gen_op_4xx_tlbwe_lo();
5782
        break;
5783
    default:
5784
        GEN_EXCP_INVAL(ctx);
5785
        break;
5786
    }
5787
#endif
5788
}
5789

    
5790
/* TLB management - PowerPC 440 implementation */
5791
/* tlbre */
5792
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5793
{
5794
#if defined(CONFIG_USER_ONLY)
5795
    GEN_EXCP_PRIVOPC(ctx);
5796
#else
5797
    if (unlikely(!ctx->supervisor)) {
5798
        GEN_EXCP_PRIVOPC(ctx);
5799
        return;
5800
    }
5801
    switch (rB(ctx->opcode)) {
5802
    case 0:
5803
    case 1:
5804
    case 2:
5805
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5806
        gen_op_440_tlbre(rB(ctx->opcode));
5807
        tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5808
        break;
5809
    default:
5810
        GEN_EXCP_INVAL(ctx);
5811
        break;
5812
    }
5813
#endif
5814
}
5815

    
5816
/* tlbsx - tlbsx. */
5817
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5818
{
5819
#if defined(CONFIG_USER_ONLY)
5820
    GEN_EXCP_PRIVOPC(ctx);
5821
#else
5822
    if (unlikely(!ctx->supervisor)) {
5823
        GEN_EXCP_PRIVOPC(ctx);
5824
        return;
5825
    }
5826
    gen_addr_reg_index(cpu_T[0], ctx);
5827
    gen_op_440_tlbsx();
5828
    if (Rc(ctx->opcode))
5829
        gen_op_4xx_tlbsx_check();
5830
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_T[0]);
5831
#endif
5832
}
5833

    
5834
/* tlbwe */
5835
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5836
{
5837
#if defined(CONFIG_USER_ONLY)
5838
    GEN_EXCP_PRIVOPC(ctx);
5839
#else
5840
    if (unlikely(!ctx->supervisor)) {
5841
        GEN_EXCP_PRIVOPC(ctx);
5842
        return;
5843
    }
5844
    switch (rB(ctx->opcode)) {
5845
    case 0:
5846
    case 1:
5847
    case 2:
5848
        tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rA(ctx->opcode)]);
5849
        tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rS(ctx->opcode)]);
5850
        gen_op_440_tlbwe(rB(ctx->opcode));
5851
        break;
5852
    default:
5853
        GEN_EXCP_INVAL(ctx);
5854
        break;
5855
    }
5856
#endif
5857
}
5858

    
5859
/* wrtee */
5860
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE)
5861
{
5862
#if defined(CONFIG_USER_ONLY)
5863
    GEN_EXCP_PRIVOPC(ctx);
5864
#else
5865
    if (unlikely(!ctx->supervisor)) {
5866
        GEN_EXCP_PRIVOPC(ctx);
5867
        return;
5868
    }
5869
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rD(ctx->opcode)]);
5870
    gen_op_wrte();
5871
    /* Stop translation to have a chance to raise an exception
5872
     * if we just set msr_ee to 1
5873
     */
5874
    GEN_STOP(ctx);
5875
#endif
5876
}
5877

    
5878
/* wrteei */
5879
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_WRTEE)
5880
{
5881
#if defined(CONFIG_USER_ONLY)
5882
    GEN_EXCP_PRIVOPC(ctx);
5883
#else
5884
    if (unlikely(!ctx->supervisor)) {
5885
        GEN_EXCP_PRIVOPC(ctx);
5886
        return;
5887
    }
5888
    tcg_gen_movi_tl(cpu_T[0], ctx->opcode & 0x00010000);
5889
    gen_op_wrte();
5890
    /* Stop translation to have a chance to raise an exception
5891
     * if we just set msr_ee to 1
5892
     */
5893
    GEN_STOP(ctx);
5894
#endif
5895
}
5896

    
5897
/* PowerPC 440 specific instructions */
5898
/* dlmzb */
5899
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
5900
{
5901
    tcg_gen_mov_tl(cpu_T[0], cpu_gpr[rS(ctx->opcode)]);
5902
    tcg_gen_mov_tl(cpu_T[1], cpu_gpr[rB(ctx->opcode)]);
5903
    gen_op_440_dlmzb();
5904
    tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], cpu_T[0]);
5905
    tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5906
    tcg_gen_or_tl(cpu_xer, cpu_xer, cpu_T[0]);
5907
    if (Rc(ctx->opcode)) {
5908
        gen_op_440_dlmzb_update_Rc();
5909
        tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_T[0]);
5910
        tcg_gen_andi_i32(cpu_crf[0], cpu_crf[0], 0xf);
5911
    }
5912
}
5913

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

    
5920
/* msync replaces sync on 440 */
5921
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5922
{
5923
    /* interpreted as no-op */
5924
}
5925

    
5926
/* icbt */
5927
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5928
{
5929
    /* interpreted as no-op */
5930
    /* XXX: specification say this is treated as a load by the MMU
5931
     *      but does not generate any exception
5932
     */
5933
}
5934

    
5935
/***                      Altivec vector extension                         ***/
5936
/* Altivec registers moves */
5937

    
5938
#define GEN_VR_LDX(name, opc2, opc3)                                          \
5939
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)                  \
5940
{                                                                             \
5941
    TCGv EA;                                                                  \
5942
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5943
        GEN_EXCP_NO_VR(ctx);                                                  \
5944
        return;                                                               \
5945
    }                                                                         \
5946
    EA = tcg_temp_new();                                                      \
5947
    gen_addr_reg_index(EA, ctx);                                              \
5948
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
5949
    if (ctx->mem_idx & 1) {                                                   \
5950
        gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5951
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5952
        gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5953
    } else {                                                                  \
5954
        gen_qemu_ld64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5955
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5956
        gen_qemu_ld64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5957
    }                                                                         \
5958
    tcg_temp_free(EA);                                                        \
5959
}
5960

    
5961
#define GEN_VR_STX(name, opc2, opc3)                                          \
5962
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
5963
{                                                                             \
5964
    TCGv EA;                                                                  \
5965
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5966
        GEN_EXCP_NO_VR(ctx);                                                  \
5967
        return;                                                               \
5968
    }                                                                         \
5969
    EA = tcg_temp_new();                                                      \
5970
    gen_addr_reg_index(EA, ctx);                                              \
5971
    tcg_gen_andi_tl(EA, EA, ~0xf);                                            \
5972
    if (ctx->mem_idx & 1) {                                                   \
5973
        gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5974
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5975
        gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5976
    } else {                                                                  \
5977
        gen_qemu_st64(cpu_avrh[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5978
        tcg_gen_addi_tl(EA, EA, 8);                                           \
5979
        gen_qemu_st64(cpu_avrl[rD(ctx->opcode)], EA, ctx->mem_idx);           \
5980
    }                                                                         \
5981
    tcg_temp_free(EA);                                                        \
5982
}
5983

    
5984
GEN_VR_LDX(lvx, 0x07, 0x03);
5985
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
5986
GEN_VR_LDX(lvxl, 0x07, 0x0B);
5987

    
5988
GEN_VR_STX(svx, 0x07, 0x07);
5989
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
5990
GEN_VR_STX(svxl, 0x07, 0x0F);
5991

    
5992
/***                           SPE extension                               ***/
5993
/* Register moves */
5994

    
5995
static always_inline void gen_load_gpr64(TCGv_i64 t, int reg) {
5996
#if defined(TARGET_PPC64)
5997
    tcg_gen_mov_i64(t, cpu_gpr[reg]);
5998
#else
5999
    tcg_gen_concat_i32_i64(t, cpu_gpr[reg], cpu_gprh[reg]);
6000
#endif
6001
}
6002

    
6003
static always_inline void gen_store_gpr64(int reg, TCGv_i64 t) {
6004
#if defined(TARGET_PPC64)
6005
    tcg_gen_mov_i64(cpu_gpr[reg], t);
6006
#else
6007
    TCGv_i64 tmp = tcg_temp_new_i64();
6008
    tcg_gen_trunc_i64_i32(cpu_gpr[reg], t);
6009
    tcg_gen_shri_i64(tmp, t, 32);
6010
    tcg_gen_trunc_i64_i32(cpu_gprh[reg], tmp);
6011
    tcg_temp_free_i64(tmp);
6012
#endif
6013
}
6014

    
6015
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
6016
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
6017
{                                                                             \
6018
    if (Rc(ctx->opcode))                                                      \
6019
        gen_##name1(ctx);                                                     \
6020
    else                                                                      \
6021
        gen_##name0(ctx);                                                     \
6022
}
6023

    
6024
/* Handler for undefined SPE opcodes */
6025
static always_inline void gen_speundef (DisasContext *ctx)
6026
{
6027
    GEN_EXCP_INVAL(ctx);
6028
}
6029

    
6030
/* SPE load and stores */
6031
static always_inline void gen_addr_spe_imm_index (TCGv EA, DisasContext *ctx, int sh)
6032
{
6033
    target_long simm = rB(ctx->opcode);
6034

    
6035
    if (rA(ctx->opcode) == 0)
6036
        tcg_gen_movi_tl(EA, simm << sh);
6037
    else if (likely(simm != 0))
6038
        tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm << sh);
6039
    else
6040
        tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
6041
}
6042

    
6043
#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
6044
#define OP_SPE_LD_TABLE(name)                                                 \
6045
static GenOpFunc *gen_op_spe_l##name[NB_MEM_FUNCS] = {                        \
6046
    GEN_MEM_FUNCS(spe_l##name),                                               \
6047
};
6048
#define OP_SPE_ST_TABLE(name)                                                 \
6049
static GenOpFunc *gen_op_spe_st##name[NB_MEM_FUNCS] = {                       \
6050
    GEN_MEM_FUNCS(spe_st##name),                                              \
6051
};
6052

    
6053
#define GEN_SPE_LD(name, sh)                                                  \
6054
static always_inline void gen_evl##name (DisasContext *ctx)                   \
6055
{                                                                             \
6056
    if (unlikely(!ctx->spe_enabled)) {                                        \
6057
        GEN_EXCP_NO_AP(ctx);                                                  \
6058
        return;                                                               \
6059
    }                                                                         \
6060
    gen_addr_spe_imm_index(cpu_T[0], ctx, sh);                                \
6061
    op_spe_ldst(spe_l##name);                                                 \
6062
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
6063
}
6064

    
6065
#define GEN_SPE_LDX(name)                                                     \
6066
static always_inline void gen_evl##name##x (DisasContext *ctx)                \
6067
{                                                                             \
6068
    if (unlikely(!ctx->spe_enabled)) {                                        \
6069
        GEN_EXCP_NO_AP(ctx);                                                  \
6070
        return;                                                               \
6071
    }                                                                         \
6072
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
6073
    op_spe_ldst(spe_l##name);                                                 \
6074
    gen_store_gpr64(rD(ctx->opcode), cpu_T64[1]);                             \
6075
}
6076

    
6077
#define GEN_SPEOP_LD(name, sh)                                                \
6078
OP_SPE_LD_TABLE(name);                                                        \
6079
GEN_SPE_LD(name, sh);                                                         \
6080
GEN_SPE_LDX(name)
6081

    
6082
#define GEN_SPE_ST(name, sh)                                                  \
6083
static always_inline void gen_evst##name (DisasContext *ctx)                  \
6084
{                                                                             \
6085
    if (unlikely(!ctx->spe_enabled)) {                                        \
6086
        GEN_EXCP_NO_AP(ctx);                                                  \
6087
        return;                                                               \
6088
    }                                                                         \
6089
    gen_addr_spe_imm_index(cpu_T[0], ctx, sh);                                \
6090
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
6091
    op_spe_ldst(spe_st##name);                                                \
6092
}
6093

    
6094
#define GEN_SPE_STX(name)                                                     \
6095
static always_inline void gen_evst##name##x (DisasContext *ctx)               \
6096
{                                                                             \
6097
    if (unlikely(!ctx->spe_enabled)) {                                        \
6098
        GEN_EXCP_NO_AP(ctx);                                                  \
6099
        return;                                                               \
6100
    }                                                                         \
6101
    gen_addr_reg_index(cpu_T[0], ctx);                                        \
6102
    gen_load_gpr64(cpu_T64[1], rS(ctx->opcode));                              \
6103
    op_spe_ldst(spe_st##name);                                                \
6104
}
6105

    
6106
#define GEN_SPEOP_ST(name, sh)                                                \
6107
OP_SPE_ST_TABLE(name);                                                        \
6108
GEN_SPE_ST(name, sh);                                                         \
6109
GEN_SPE_STX(name)
6110

    
6111
#define GEN_SPEOP_LDST(name, sh)                                              \
6112
GEN_SPEOP_LD(name, sh);                                                       \
6113
GEN_SPEOP_ST(name, sh)
6114

    
6115
/* SPE logic */
6116
#if defined(TARGET_PPC64)
6117
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6118
static always_inline void gen_##name (DisasContext *ctx)                      \
6119
{                                                                             \
6120
    if (unlikely(!ctx->spe_enabled)) {                                        \
6121
        GEN_EXCP_NO_AP(ctx);                                                  \
6122
        return;                                                               \
6123
    }                                                                         \
6124
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6125
           cpu_gpr[rB(ctx->opcode)]);                                         \
6126
}
6127
#else
6128
#define GEN_SPEOP_LOGIC2(name, tcg_op)                                        \
6129
static always_inline void gen_##name (DisasContext *ctx)                      \
6130
{                                                                             \
6131
    if (unlikely(!ctx->spe_enabled)) {                                        \
6132
        GEN_EXCP_NO_AP(ctx);                                                  \
6133
        return;                                                               \
6134
    }                                                                         \
6135
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6136
           cpu_gpr[rB(ctx->opcode)]);                                         \
6137
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6138
           cpu_gprh[rB(ctx->opcode)]);                                        \
6139
}
6140
#endif
6141

    
6142
GEN_SPEOP_LOGIC2(evand, tcg_gen_and_tl);
6143
GEN_SPEOP_LOGIC2(evandc, tcg_gen_andc_tl);
6144
GEN_SPEOP_LOGIC2(evxor, tcg_gen_xor_tl);
6145
GEN_SPEOP_LOGIC2(evor, tcg_gen_or_tl);
6146
GEN_SPEOP_LOGIC2(evnor, tcg_gen_nor_tl);
6147
GEN_SPEOP_LOGIC2(eveqv, tcg_gen_eqv_tl);
6148
GEN_SPEOP_LOGIC2(evorc, tcg_gen_orc_tl);
6149
GEN_SPEOP_LOGIC2(evnand, tcg_gen_nand_tl);
6150

    
6151
/* SPE logic immediate */
6152
#if defined(TARGET_PPC64)
6153
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6154
static always_inline void gen_##name (DisasContext *ctx)                      \
6155
{                                                                             \
6156
    if (unlikely(!ctx->spe_enabled)) {                                        \
6157
        GEN_EXCP_NO_AP(ctx);                                                  \
6158
        return;                                                               \
6159
    }                                                                         \
6160
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6161
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6162
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6163
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6164
    tcg_opi(t0, t0, rB(ctx->opcode));                                         \
6165
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6166
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6167
    tcg_temp_free_i64(t2);                                                    \
6168
    tcg_opi(t1, t1, rB(ctx->opcode));                                         \
6169
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6170
    tcg_temp_free_i32(t0);                                                    \
6171
    tcg_temp_free_i32(t1);                                                    \
6172
}
6173
#else
6174
#define GEN_SPEOP_TCG_LOGIC_IMM2(name, tcg_opi)                               \
6175
static always_inline void gen_##name (DisasContext *ctx)                      \
6176
{                                                                             \
6177
    if (unlikely(!ctx->spe_enabled)) {                                        \
6178
        GEN_EXCP_NO_AP(ctx);                                                  \
6179
        return;                                                               \
6180
    }                                                                         \
6181
    tcg_opi(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],               \
6182
            rB(ctx->opcode));                                                 \
6183
    tcg_opi(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],             \
6184
            rB(ctx->opcode));                                                 \
6185
}
6186
#endif
6187
GEN_SPEOP_TCG_LOGIC_IMM2(evslwi, tcg_gen_shli_i32);
6188
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwiu, tcg_gen_shri_i32);
6189
GEN_SPEOP_TCG_LOGIC_IMM2(evsrwis, tcg_gen_sari_i32);
6190
GEN_SPEOP_TCG_LOGIC_IMM2(evrlwi, tcg_gen_rotli_i32);
6191

    
6192
/* SPE arithmetic */
6193
#if defined(TARGET_PPC64)
6194
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6195
static always_inline void gen_##name (DisasContext *ctx)                      \
6196
{                                                                             \
6197
    if (unlikely(!ctx->spe_enabled)) {                                        \
6198
        GEN_EXCP_NO_AP(ctx);                                                  \
6199
        return;                                                               \
6200
    }                                                                         \
6201
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6202
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6203
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6204
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6205
    tcg_op(t0, t0);                                                           \
6206
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6207
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6208
    tcg_temp_free_i64(t2);                                                    \
6209
    tcg_op(t1, t1);                                                           \
6210
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6211
    tcg_temp_free_i32(t0);                                                    \
6212
    tcg_temp_free_i32(t1);                                                    \
6213
}
6214
#else
6215
#define GEN_SPEOP_ARITH1(name, tcg_op)                                        \
6216
static always_inline void gen_##name (DisasContext *ctx)                      \
6217
{                                                                             \
6218
    if (unlikely(!ctx->spe_enabled)) {                                        \
6219
        GEN_EXCP_NO_AP(ctx);                                                  \
6220
        return;                                                               \
6221
    }                                                                         \
6222
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);               \
6223
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);             \
6224
}
6225
#endif
6226

    
6227
static always_inline void gen_op_evabs (TCGv_i32 ret, TCGv_i32 arg1)
6228
{
6229
    int l1 = gen_new_label();
6230
    int l2 = gen_new_label();
6231

    
6232
    tcg_gen_brcondi_i32(TCG_COND_GE, arg1, 0, l1);
6233
    tcg_gen_neg_i32(ret, arg1);
6234
    tcg_gen_br(l2);
6235
    gen_set_label(l1);
6236
    tcg_gen_mov_i32(ret, arg1);
6237
    gen_set_label(l2);
6238
}
6239
GEN_SPEOP_ARITH1(evabs, gen_op_evabs);
6240
GEN_SPEOP_ARITH1(evneg, tcg_gen_neg_i32);
6241
GEN_SPEOP_ARITH1(evextsb, tcg_gen_ext8s_i32);
6242
GEN_SPEOP_ARITH1(evextsh, tcg_gen_ext16s_i32);
6243
static always_inline void gen_op_evrndw (TCGv_i32 ret, TCGv_i32 arg1)
6244
{
6245
    tcg_gen_addi_i32(ret, arg1, 0x8000);
6246
    tcg_gen_ext16u_i32(ret, ret);
6247
}
6248
GEN_SPEOP_ARITH1(evrndw, gen_op_evrndw);
6249
GEN_SPEOP_ARITH1(evcntlsw, gen_helper_cntlsw32);
6250
GEN_SPEOP_ARITH1(evcntlzw, gen_helper_cntlzw32);
6251

    
6252
#if defined(TARGET_PPC64)
6253
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6254
static always_inline void gen_##name (DisasContext *ctx)                      \
6255
{                                                                             \
6256
    if (unlikely(!ctx->spe_enabled)) {                                        \
6257
        GEN_EXCP_NO_AP(ctx);                                                  \
6258
        return;                                                               \
6259
    }                                                                         \
6260
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6261
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6262
    TCGv_i32 t2 = tcg_temp_local_new_i32();                                   \
6263
    TCGv_i64 t3 = tcg_temp_local_new(TCG_TYPE_I64);                           \
6264
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6265
    tcg_gen_trunc_i64_i32(t2, cpu_gpr[rB(ctx->opcode)]);                      \
6266
    tcg_op(t0, t0, t2);                                                       \
6267
    tcg_gen_shri_i64(t3, cpu_gpr[rA(ctx->opcode)], 32);                       \
6268
    tcg_gen_trunc_i64_i32(t1, t3);                                            \
6269
    tcg_gen_shri_i64(t3, cpu_gpr[rB(ctx->opcode)], 32);                       \
6270
    tcg_gen_trunc_i64_i32(t2, t3);                                            \
6271
    tcg_temp_free_i64(t3);                                                    \
6272
    tcg_op(t1, t1, t2);                                                       \
6273
    tcg_temp_free_i32(t2);                                                    \
6274
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6275
    tcg_temp_free_i32(t0);                                                    \
6276
    tcg_temp_free_i32(t1);                                                    \
6277
}
6278
#else
6279
#define GEN_SPEOP_ARITH2(name, tcg_op)                                        \
6280
static always_inline void gen_##name (DisasContext *ctx)                      \
6281
{                                                                             \
6282
    if (unlikely(!ctx->spe_enabled)) {                                        \
6283
        GEN_EXCP_NO_AP(ctx);                                                  \
6284
        return;                                                               \
6285
    }                                                                         \
6286
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],                \
6287
           cpu_gpr[rB(ctx->opcode)]);                                         \
6288
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)],              \
6289
           cpu_gprh[rB(ctx->opcode)]);                                        \
6290
}
6291
#endif
6292

    
6293
static always_inline void gen_op_evsrwu (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6294
{
6295
    TCGv_i32 t0;
6296
    int l1, l2;
6297

    
6298
    l1 = gen_new_label();
6299
    l2 = gen_new_label();
6300
    t0 = tcg_temp_local_new_i32();
6301
    /* No error here: 6 bits are used */
6302
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6303
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6304
    tcg_gen_shr_i32(ret, arg1, t0);
6305
    tcg_gen_br(l2);
6306
    gen_set_label(l1);
6307
    tcg_gen_movi_i32(ret, 0);
6308
    tcg_gen_br(l2);
6309
    tcg_temp_free_i32(t0);
6310
}
6311
GEN_SPEOP_ARITH2(evsrwu, gen_op_evsrwu);
6312
static always_inline void gen_op_evsrws (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6313
{
6314
    TCGv_i32 t0;
6315
    int l1, l2;
6316

    
6317
    l1 = gen_new_label();
6318
    l2 = gen_new_label();
6319
    t0 = tcg_temp_local_new_i32();
6320
    /* No error here: 6 bits are used */
6321
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6322
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6323
    tcg_gen_sar_i32(ret, arg1, t0);
6324
    tcg_gen_br(l2);
6325
    gen_set_label(l1);
6326
    tcg_gen_movi_i32(ret, 0);
6327
    tcg_gen_br(l2);
6328
    tcg_temp_free_i32(t0);
6329
}
6330
GEN_SPEOP_ARITH2(evsrws, gen_op_evsrws);
6331
static always_inline void gen_op_evslw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6332
{
6333
    TCGv_i32 t0;
6334
    int l1, l2;
6335

    
6336
    l1 = gen_new_label();
6337
    l2 = gen_new_label();
6338
    t0 = tcg_temp_local_new_i32();
6339
    /* No error here: 6 bits are used */
6340
    tcg_gen_andi_i32(t0, arg2, 0x3F);
6341
    tcg_gen_brcondi_i32(TCG_COND_GE, t0, 32, l1);
6342
    tcg_gen_shl_i32(ret, arg1, t0);
6343
    tcg_gen_br(l2);
6344
    gen_set_label(l1);
6345
    tcg_gen_movi_i32(ret, 0);
6346
    tcg_gen_br(l2);
6347
    tcg_temp_free_i32(t0);
6348
}
6349
GEN_SPEOP_ARITH2(evslw, gen_op_evslw);
6350
static always_inline void gen_op_evrlw (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6351
{
6352
    TCGv_i32 t0 = tcg_temp_new_i32();
6353
    tcg_gen_andi_i32(t0, arg2, 0x1F);
6354
    tcg_gen_rotl_i32(ret, arg1, t0);
6355
    tcg_temp_free_i32(t0);
6356
}
6357
GEN_SPEOP_ARITH2(evrlw, gen_op_evrlw);
6358
static always_inline void gen_evmergehi (DisasContext *ctx)
6359
{
6360
    if (unlikely(!ctx->spe_enabled)) {
6361
        GEN_EXCP_NO_AP(ctx);
6362
        return;
6363
    }
6364
#if defined(TARGET_PPC64)
6365
    TCGv t0 = tcg_temp_new();
6366
    TCGv t1 = tcg_temp_new();
6367
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6368
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6369
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6370
    tcg_temp_free(t0);
6371
    tcg_temp_free(t1);
6372
#else
6373
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6374
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6375
#endif
6376
}
6377
GEN_SPEOP_ARITH2(evaddw, tcg_gen_add_i32);
6378
static always_inline void gen_op_evsubf (TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
6379
{
6380
    tcg_gen_sub_i32(ret, arg2, arg1);
6381
}
6382
GEN_SPEOP_ARITH2(evsubfw, gen_op_evsubf);
6383

    
6384
/* SPE arithmetic immediate */
6385
#if defined(TARGET_PPC64)
6386
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6387
static always_inline void gen_##name (DisasContext *ctx)                      \
6388
{                                                                             \
6389
    if (unlikely(!ctx->spe_enabled)) {                                        \
6390
        GEN_EXCP_NO_AP(ctx);                                                  \
6391
        return;                                                               \
6392
    }                                                                         \
6393
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6394
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6395
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6396
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rB(ctx->opcode)]);                      \
6397
    tcg_op(t0, t0, rA(ctx->opcode));                                          \
6398
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6399
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6400
    tcg_temp_free_i64(t2);                                                        \
6401
    tcg_op(t1, t1, rA(ctx->opcode));                                          \
6402
    tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);                 \
6403
    tcg_temp_free_i32(t0);                                                    \
6404
    tcg_temp_free_i32(t1);                                                    \
6405
}
6406
#else
6407
#define GEN_SPEOP_ARITH_IMM2(name, tcg_op)                                    \
6408
static always_inline void gen_##name (DisasContext *ctx)                      \
6409
{                                                                             \
6410
    if (unlikely(!ctx->spe_enabled)) {                                        \
6411
        GEN_EXCP_NO_AP(ctx);                                                  \
6412
        return;                                                               \
6413
    }                                                                         \
6414
    tcg_op(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],                \
6415
           rA(ctx->opcode));                                                  \
6416
    tcg_op(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)],              \
6417
           rA(ctx->opcode));                                                  \
6418
}
6419
#endif
6420
GEN_SPEOP_ARITH_IMM2(evaddiw, tcg_gen_addi_i32);
6421
GEN_SPEOP_ARITH_IMM2(evsubifw, tcg_gen_subi_i32);
6422

    
6423
/* SPE comparison */
6424
#if defined(TARGET_PPC64)
6425
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
6426
static always_inline void gen_##name (DisasContext *ctx)                      \
6427
{                                                                             \
6428
    if (unlikely(!ctx->spe_enabled)) {                                        \
6429
        GEN_EXCP_NO_AP(ctx);                                                  \
6430
        return;                                                               \
6431
    }                                                                         \
6432
    int l1 = gen_new_label();                                                 \
6433
    int l2 = gen_new_label();                                                 \
6434
    int l3 = gen_new_label();                                                 \
6435
    int l4 = gen_new_label();                                                 \
6436
    TCGv_i32 t0 = tcg_temp_local_new_i32();                                   \
6437
    TCGv_i32 t1 = tcg_temp_local_new_i32();                                   \
6438
    TCGv_i64 t2 = tcg_temp_local_new_i64();                                   \
6439
    tcg_gen_trunc_i64_i32(t0, cpu_gpr[rA(ctx->opcode)]);                      \
6440
    tcg_gen_trunc_i64_i32(t1, cpu_gpr[rB(ctx->opcode)]);                      \
6441
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l1);                                 \
6442
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0);                          \
6443
    tcg_gen_br(l2);                                                           \
6444
    gen_set_label(l1);                                                        \
6445
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6446
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6447
    gen_set_label(l2);                                                        \
6448
    tcg_gen_shri_i64(t2, cpu_gpr[rA(ctx->opcode)], 32);                       \
6449
    tcg_gen_trunc_i64_i32(t0, t2);                                            \
6450
    tcg_gen_shri_i64(t2, cpu_gpr[rB(ctx->opcode)], 32);                       \
6451
    tcg_gen_trunc_i64_i32(t1, t2);                                            \
6452
    tcg_temp_free_i64(t2);                                                    \
6453
    tcg_gen_brcond_i32(tcg_cond, t0, t1, l3);                                 \
6454
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6455
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
6456
    tcg_gen_br(l4);                                                           \
6457
    gen_set_label(l3);                                                        \
6458
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6459
                    CRF_CH | CRF_CH_OR_CL);                                   \
6460
    gen_set_label(l4);                                                        \
6461
    tcg_temp_free_i32(t0);                                                    \
6462
    tcg_temp_free_i32(t1);                                                    \
6463
}
6464
#else
6465
#define GEN_SPEOP_COMP(name, tcg_cond)                                        \
6466
static always_inline void gen_##name (DisasContext *ctx)                      \
6467
{                                                                             \
6468
    if (unlikely(!ctx->spe_enabled)) {                                        \
6469
        GEN_EXCP_NO_AP(ctx);                                                  \
6470
        return;                                                               \
6471
    }                                                                         \
6472
    int l1 = gen_new_label();                                                 \
6473
    int l2 = gen_new_label();                                                 \
6474
    int l3 = gen_new_label();                                                 \
6475
    int l4 = gen_new_label();                                                 \
6476
                                                                              \
6477
    tcg_gen_brcond_i32(tcg_cond, cpu_gpr[rA(ctx->opcode)],                    \
6478
                       cpu_gpr[rB(ctx->opcode)], l1);                         \
6479
    tcg_gen_movi_tl(cpu_crf[crfD(ctx->opcode)], 0);                           \
6480
    tcg_gen_br(l2);                                                           \
6481
    gen_set_label(l1);                                                        \
6482
    tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)],                              \
6483
                     CRF_CL | CRF_CH_OR_CL | CRF_CH_AND_CL);                  \
6484
    gen_set_label(l2);                                                        \
6485
    tcg_gen_brcond_i32(tcg_cond, cpu_gprh[rA(ctx->opcode)],                   \
6486
                       cpu_gprh[rB(ctx->opcode)], l3);                        \
6487
    tcg_gen_andi_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],  \
6488
                     ~(CRF_CH | CRF_CH_AND_CL));                              \
6489
    tcg_gen_br(l4);                                                           \
6490
    gen_set_label(l3);                                                        \
6491
    tcg_gen_ori_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfD(ctx->opcode)],   \
6492
                    CRF_CH | CRF_CH_OR_CL);                                   \
6493
    gen_set_label(l4);                                                        \
6494
}
6495
#endif
6496
GEN_SPEOP_COMP(evcmpgtu, TCG_COND_GTU);
6497
GEN_SPEOP_COMP(evcmpgts, TCG_COND_GT);
6498
GEN_SPEOP_COMP(evcmpltu, TCG_COND_LTU);
6499
GEN_SPEOP_COMP(evcmplts, TCG_COND_LT);
6500
GEN_SPEOP_COMP(evcmpeq, TCG_COND_EQ);
6501

    
6502
/* SPE misc */
6503
static always_inline void gen_brinc (DisasContext *ctx)
6504
{
6505
    /* Note: brinc is usable even if SPE is disabled */
6506
    gen_helper_brinc(cpu_gpr[rD(ctx->opcode)],
6507
                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6508
}
6509
static always_inline void gen_evmergelo (DisasContext *ctx)
6510
{
6511
    if (unlikely(!ctx->spe_enabled)) {
6512
        GEN_EXCP_NO_AP(ctx);
6513
        return;
6514
    }
6515
#if defined(TARGET_PPC64)
6516
    TCGv t0 = tcg_temp_new();
6517
    TCGv t1 = tcg_temp_new();
6518
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6519
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6520
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6521
    tcg_temp_free(t0);
6522
    tcg_temp_free(t1);
6523
#else
6524
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6525
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6526
#endif
6527
}
6528
static always_inline void gen_evmergehilo (DisasContext *ctx)
6529
{
6530
    if (unlikely(!ctx->spe_enabled)) {
6531
        GEN_EXCP_NO_AP(ctx);
6532
        return;
6533
    }
6534
#if defined(TARGET_PPC64)
6535
    TCGv t0 = tcg_temp_new();
6536
    TCGv t1 = tcg_temp_new();
6537
    tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFLL);
6538
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF0000000ULL);
6539
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6540
    tcg_temp_free(t0);
6541
    tcg_temp_free(t1);
6542
#else
6543
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6544
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6545
#endif
6546
}
6547
static always_inline void gen_evmergelohi (DisasContext *ctx)
6548
{
6549
    if (unlikely(!ctx->spe_enabled)) {
6550
        GEN_EXCP_NO_AP(ctx);
6551
        return;
6552
    }
6553
#if defined(TARGET_PPC64)
6554
    TCGv t0 = tcg_temp_new();
6555
    TCGv t1 = tcg_temp_new();
6556
    tcg_gen_shri_tl(t0, cpu_gpr[rB(ctx->opcode)], 32);
6557
    tcg_gen_shli_tl(t1, cpu_gpr[rA(ctx->opcode)], 32);
6558
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6559
    tcg_temp_free(t0);
6560
    tcg_temp_free(t1);
6561
#else
6562
    tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6563
    tcg_gen_mov_i32(cpu_gprh[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6564
#endif
6565
}
6566
static always_inline void gen_evsplati (DisasContext *ctx)
6567
{
6568
    int32_t imm = (int32_t)(rA(ctx->opcode) << 11) >> 27;
6569

    
6570
#if defined(TARGET_PPC64)
6571
    TCGv t0 = tcg_temp_new();
6572
    TCGv t1 = tcg_temp_new();
6573
    tcg_gen_movi_tl(t0, imm);
6574
    tcg_gen_shri_tl(t1, t0, 32);
6575
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6576
    tcg_temp_free(t0);
6577
    tcg_temp_free(t1);
6578
#else
6579
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6580
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6581
#endif
6582
}
6583
static always_inline void gen_evsplatfi (DisasContext *ctx)
6584
{
6585
    uint32_t imm = rA(ctx->opcode) << 11;
6586

    
6587
#if defined(TARGET_PPC64)
6588
    TCGv t0 = tcg_temp_new();
6589
    TCGv t1 = tcg_temp_new();
6590
    tcg_gen_movi_tl(t0, imm);
6591
    tcg_gen_shri_tl(t1, t0, 32);
6592
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t0, t1);
6593
    tcg_temp_free(t0);
6594
    tcg_temp_free(t1);
6595
#else
6596
    tcg_gen_movi_i32(cpu_gpr[rD(ctx->opcode)], imm);
6597
    tcg_gen_movi_i32(cpu_gprh[rD(ctx->opcode)], imm);
6598
#endif
6599
}
6600

    
6601
static always_inline void gen_evsel (DisasContext *ctx)
6602
{
6603
    int l1 = gen_new_label();
6604
    int l2 = gen_new_label();
6605
    int l3 = gen_new_label();
6606
    int l4 = gen_new_label();
6607
    TCGv_i32 t0 = tcg_temp_local_new_i32();
6608
#if defined(TARGET_PPC64)
6609
    TCGv t1 = tcg_temp_local_new();
6610
    TCGv t2 = tcg_temp_local_new();
6611
#endif
6612
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 3);
6613
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l1);
6614
#if defined(TARGET_PPC64)
6615
    tcg_gen_andi_tl(t1, cpu_gpr[rA(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6616
#else
6617
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rA(ctx->opcode)]);
6618
#endif
6619
    tcg_gen_br(l2);
6620
    gen_set_label(l1);
6621
#if defined(TARGET_PPC64)
6622
    tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0xFFFFFFFF00000000ULL);
6623
#else
6624
    tcg_gen_mov_tl(cpu_gprh[rD(ctx->opcode)], cpu_gprh[rB(ctx->opcode)]);
6625
#endif
6626
    gen_set_label(l2);
6627
    tcg_gen_andi_i32(t0, cpu_crf[ctx->opcode & 0x07], 1 << 2);
6628
    tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, l3);
6629
#if defined(TARGET_PPC64)
6630
    tcg_gen_andi_tl(t2, cpu_gpr[rA(ctx->opcode)], 0x00000000FFFFFFFFULL);
6631
#else
6632
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
6633
#endif
6634
    tcg_gen_br(l4);
6635
    gen_set_label(l3);
6636
#if defined(TARGET_PPC64)
6637
    tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x00000000FFFFFFFFULL);
6638
#else
6639
    tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6640
#endif
6641
    gen_set_label(l4);
6642
    tcg_temp_free_i32(t0);
6643
#if defined(TARGET_PPC64)
6644
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], t1, t2);
6645
    tcg_temp_free(t1);
6646
    tcg_temp_free(t2);
6647
#endif
6648
}
6649
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6650
{
6651
    gen_evsel(ctx);
6652
}
6653
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6654
{
6655
    gen_evsel(ctx);
6656
}
6657
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6658
{
6659
    gen_evsel(ctx);
6660
}
6661
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6662
{
6663
    gen_evsel(ctx);
6664
}
6665

    
6666
GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
6667
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
6668
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
6669
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
6670
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
6671
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
6672
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
6673
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
6674
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
6675
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
6676
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
6677
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
6678
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
6679
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
6680
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
6681
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
6682
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
6683
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
6684
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
6685
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
6686
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
6687
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
6688
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
6689
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
6690
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////
6691

    
6692
/* Load and stores */
6693
GEN_SPEOP_LDST(dd, 3);
6694
GEN_SPEOP_LDST(dw, 3);
6695
GEN_SPEOP_LDST(dh, 3);
6696
GEN_SPEOP_LDST(whe, 2);
6697
GEN_SPEOP_LD(whou, 2);
6698
GEN_SPEOP_LD(whos, 2);
6699
GEN_SPEOP_ST(who, 2);
6700

    
6701
#define _GEN_OP_SPE_STWWE(suffix)                                             \
6702
static always_inline void gen_op_spe_stwwe_##suffix (void)                    \
6703
{                                                                             \
6704
    gen_op_srli32_T1_64();                                                    \
6705
    gen_op_spe_stwwo_##suffix();                                              \
6706
}
6707
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
6708
static always_inline void gen_op_spe_stwwe_le_##suffix (void)                 \
6709
{                                                                             \
6710
    gen_op_srli32_T1_64();                                                    \
6711
    gen_op_spe_stwwo_le_##suffix();                                           \
6712
}
6713
#if defined(TARGET_PPC64)
6714
#define GEN_OP_SPE_STWWE(suffix)                                              \
6715
_GEN_OP_SPE_STWWE(suffix);                                                    \
6716
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
6717
static always_inline void gen_op_spe_stwwe_64_##suffix (void)                 \
6718
{                                                                             \
6719
    gen_op_srli32_T1_64();                                                    \
6720
    gen_op_spe_stwwo_64_##suffix();                                           \
6721
}                                                                             \
6722
static always_inline void gen_op_spe_stwwe_le_64_##suffix (void)              \
6723
{                                                                             \
6724
    gen_op_srli32_T1_64();                                                    \
6725
    gen_op_spe_stwwo_le_64_##suffix();                                        \
6726
}
6727
#else
6728
#define GEN_OP_SPE_STWWE(suffix)                                              \
6729
_GEN_OP_SPE_STWWE(suffix);                                                    \
6730
_GEN_OP_SPE_STWWE_LE(suffix)
6731
#endif
6732
#if defined(CONFIG_USER_ONLY)
6733
GEN_OP_SPE_STWWE(raw);
6734
#else /* defined(CONFIG_USER_ONLY) */
6735
GEN_OP_SPE_STWWE(user);
6736
GEN_OP_SPE_STWWE(kernel);
6737
GEN_OP_SPE_STWWE(hypv);
6738
#endif /* defined(CONFIG_USER_ONLY) */
6739
GEN_SPEOP_ST(wwe, 2);
6740
GEN_SPEOP_ST(wwo, 2);
6741

    
6742
#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
6743
static always_inline void gen_op_spe_l##name##_##suffix (void)                \
6744
{                                                                             \
6745
    gen_op_##op##_##suffix();                                                 \
6746
    gen_op_splatw_T1_64();                                                    \
6747
}
6748

    
6749
#define GEN_OP_SPE_LHE(suffix)                                                \
6750
static always_inline void gen_op_spe_lhe_##suffix (void)                      \
6751
{                                                                             \
6752
    gen_op_spe_lh_##suffix();                                                 \
6753
    gen_op_sli16_T1_64();                                                     \
6754
}
6755

    
6756
#define GEN_OP_SPE_LHX(suffix)                                                \
6757
static always_inline void gen_op_spe_lhx_##suffix (void)                      \
6758
{                                                                             \
6759
    gen_op_spe_lh_##suffix();                                                 \
6760
    gen_op_extsh_T1_64();                                                     \
6761
}
6762

    
6763
#if defined(CONFIG_USER_ONLY)
6764
GEN_OP_SPE_LHE(raw);
6765
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
6766
GEN_OP_SPE_LHE(le_raw);
6767
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
6768
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
6769
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
6770
GEN_OP_SPE_LHX(raw);
6771
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
6772
GEN_OP_SPE_LHX(le_raw);
6773
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
6774
#if defined(TARGET_PPC64)
6775
GEN_OP_SPE_LHE(64_raw);
6776
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
6777
GEN_OP_SPE_LHE(le_64_raw);
6778
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
6779
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
6780
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
6781
GEN_OP_SPE_LHX(64_raw);
6782
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
6783
GEN_OP_SPE_LHX(le_64_raw);
6784
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
6785
#endif
6786
#else
6787
GEN_OP_SPE_LHE(user);
6788
GEN_OP_SPE_LHE(kernel);
6789
GEN_OP_SPE_LHE(hypv);
6790
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
6791
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
6792
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, hypv);
6793
GEN_OP_SPE_LHE(le_user);
6794
GEN_OP_SPE_LHE(le_kernel);
6795
GEN_OP_SPE_LHE(le_hypv);
6796
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
6797
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
6798
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_hypv);
6799
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
6800
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
6801
GEN_SPE_LDSPLAT(hhousplat, spe_lh, hypv);
6802
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
6803
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
6804
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_hypv);
6805
GEN_OP_SPE_LHX(user);
6806
GEN_OP_SPE_LHX(kernel);
6807
GEN_OP_SPE_LHX(hypv);
6808
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
6809
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
6810
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, hypv);
6811
GEN_OP_SPE_LHX(le_user);
6812
GEN_OP_SPE_LHX(le_kernel);
6813
GEN_OP_SPE_LHX(le_hypv);
6814
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
6815
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
6816
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_hypv);
6817
#if defined(TARGET_PPC64)
6818
GEN_OP_SPE_LHE(64_user);
6819
GEN_OP_SPE_LHE(64_kernel);
6820
GEN_OP_SPE_LHE(64_hypv);
6821
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
6822
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
6823
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_hypv);
6824
GEN_OP_SPE_LHE(le_64_user);
6825
GEN_OP_SPE_LHE(le_64_kernel);
6826
GEN_OP_SPE_LHE(le_64_hypv);
6827
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
6828
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
6829
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_hypv);
6830
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
6831
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
6832
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_hypv);
6833
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
6834
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
6835
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_hypv);
6836
GEN_OP_SPE_LHX(64_user);
6837
GEN_OP_SPE_LHX(64_kernel);
6838
GEN_OP_SPE_LHX(64_hypv);
6839
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
6840
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
6841
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_hypv);
6842
GEN_OP_SPE_LHX(le_64_user);
6843
GEN_OP_SPE_LHX(le_64_kernel);
6844
GEN_OP_SPE_LHX(le_64_hypv);
6845
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
6846
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
6847
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_hypv);
6848
#endif
6849
#endif
6850
GEN_SPEOP_LD(hhesplat, 1);
6851
GEN_SPEOP_LD(hhousplat, 1);
6852
GEN_SPEOP_LD(hhossplat, 1);
6853
GEN_SPEOP_LD(wwsplat, 2);
6854
GEN_SPEOP_LD(whsplat, 2);
6855

    
6856
GEN_SPE(evlddx,         evldd,         0x00, 0x0C, 0x00000000, PPC_SPE); //
6857
GEN_SPE(evldwx,         evldw,         0x01, 0x0C, 0x00000000, PPC_SPE); //
6858
GEN_SPE(evldhx,         evldh,         0x02, 0x0C, 0x00000000, PPC_SPE); //
6859
GEN_SPE(evlhhesplatx,   evlhhesplat,   0x04, 0x0C, 0x00000000, PPC_SPE); //
6860
GEN_SPE(evlhhousplatx,  evlhhousplat,  0x06, 0x0C, 0x00000000, PPC_SPE); //
6861
GEN_SPE(evlhhossplatx,  evlhhossplat,  0x07, 0x0C, 0x00000000, PPC_SPE); //
6862
GEN_SPE(evlwhex,        evlwhe,        0x08, 0x0C, 0x00000000, PPC_SPE); //
6863
GEN_SPE(evlwhoux,       evlwhou,       0x0A, 0x0C, 0x00000000, PPC_SPE); //
6864
GEN_SPE(evlwhosx,       evlwhos,       0x0B, 0x0C, 0x00000000, PPC_SPE); //
6865
GEN_SPE(evlwwsplatx,    evlwwsplat,    0x0C, 0x0C, 0x00000000, PPC_SPE); //
6866
GEN_SPE(evlwhsplatx,    evlwhsplat,    0x0E, 0x0C, 0x00000000, PPC_SPE); //
6867
GEN_SPE(evstddx,        evstdd,        0x10, 0x0C, 0x00000000, PPC_SPE); //
6868
GEN_SPE(evstdwx,        evstdw,        0x11, 0x0C, 0x00000000, PPC_SPE); //
6869
GEN_SPE(evstdhx,        evstdh,        0x12, 0x0C, 0x00000000, PPC_SPE); //
6870
GEN_SPE(evstwhex,       evstwhe,       0x18, 0x0C, 0x00000000, PPC_SPE); //
6871
GEN_SPE(evstwhox,       evstwho,       0x1A, 0x0C, 0x00000000, PPC_SPE); //
6872
GEN_SPE(evstwwex,       evstwwe,       0x1C, 0x0C, 0x00000000, PPC_SPE); //
6873
GEN_SPE(evstwwox,       evstwwo,       0x1E, 0x0C, 0x00000000, PPC_SPE); //
6874

    
6875
/* Multiply and add - TODO */
6876
#if 0
6877
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
6878
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
6879
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
6880
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
6881
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
6882
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
6883
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
6884
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
6885
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
6886
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
6887
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
6888
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);
6889

6890
GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
6891
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
6892
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
6893
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
6894
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
6895
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
6896
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
6897
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
6898
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
6899
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
6900
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
6901
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
6902
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
6903
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);
6904

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

6912
GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
6913
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
6914
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
6915
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
6916
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
6917
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
6918
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
6919
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
6920
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
6921
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
6922
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
6923
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);
6924

6925
GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
6926
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
6927
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
6928
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
6929
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);
6930

6931
GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
6932
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
6933
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
6934
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
6935
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
6936
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
6937
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
6938
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
6939
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
6940
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
6941
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
6942
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);
6943

6944
GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
6945
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
6946
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
6947
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
6948
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
6949
#endif
6950

    
6951
/***                      SPE floating-point extension                     ***/
6952
#if defined(TARGET_PPC64)
6953
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
6954
static always_inline void gen_##name (DisasContext *ctx)                      \
6955
{                                                                             \
6956
    TCGv_i32 t0;                                                              \
6957
    TCGv t1;                                                                  \
6958
    t0 = tcg_temp_new_i32();                                                  \
6959
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
6960
    gen_helper_##name(t0, t0);                                                \
6961
    t1 = tcg_temp_new();                                                      \
6962
    tcg_gen_extu_i32_tl(t1, t0);                                              \
6963
    tcg_temp_free_i32(t0);                                                    \
6964
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
6965
                    0xFFFFFFFF00000000ULL);                                   \
6966
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
6967
    tcg_temp_free(t1);                                                        \
6968
}
6969
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
6970
static always_inline void gen_##name (DisasContext *ctx)                      \
6971
{                                                                             \
6972
    TCGv_i32 t0;                                                              \
6973
    TCGv t1;                                                                  \
6974
    t0 = tcg_temp_new_i32();                                                  \
6975
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
6976
    t1 = tcg_temp_new();                                                      \
6977
    tcg_gen_extu_i32_tl(t1, t0);                                              \
6978
    tcg_temp_free_i32(t0);                                                    \
6979
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
6980
                    0xFFFFFFFF00000000ULL);                                   \
6981
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t1);    \
6982
    tcg_temp_free(t1);                                                        \
6983
}
6984
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
6985
static always_inline void gen_##name (DisasContext *ctx)                      \
6986
{                                                                             \
6987
    TCGv_i32 t0 = tcg_temp_new_i32();                                         \
6988
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rB(ctx->opcode)]);                       \
6989
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
6990
    tcg_temp_free_i32(t0);                                                    \
6991
}
6992
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
6993
static always_inline void gen_##name (DisasContext *ctx)                      \
6994
{                                                                             \
6995
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
6996
}
6997
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
6998
static always_inline void gen_##name (DisasContext *ctx)                      \
6999
{                                                                             \
7000
    TCGv_i32 t0, t1;                                                          \
7001
    TCGv_i64 t2;                                                              \
7002
    if (unlikely(!ctx->spe_enabled)) {                                        \
7003
        GEN_EXCP_NO_AP(ctx);                                                  \
7004
        return;                                                               \
7005
    }                                                                         \
7006
    t0 = tcg_temp_new_i32();                                                  \
7007
    t1 = tcg_temp_new_i32();                                                  \
7008
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7009
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7010
    gen_helper_##name(t0, t0, t1);                                            \
7011
    tcg_temp_free_i32(t1);                                                    \
7012
    t2 = tcg_temp_new();                                                      \
7013
    tcg_gen_extu_i32_tl(t2, t0);                                              \
7014
    tcg_temp_free_i32(t0);                                                    \
7015
    tcg_gen_andi_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)],       \
7016
                    0xFFFFFFFF00000000ULL);                                   \
7017
    tcg_gen_or_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rD(ctx->opcode)], t2);    \
7018
    tcg_temp_free(t2);                                                        \
7019
}
7020
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7021
static always_inline void gen_##name (DisasContext *ctx)                      \
7022
{                                                                             \
7023
    if (unlikely(!ctx->spe_enabled)) {                                        \
7024
        GEN_EXCP_NO_AP(ctx);                                                  \
7025
        return;                                                               \
7026
    }                                                                         \
7027
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],     \
7028
                      cpu_gpr[rB(ctx->opcode)]);                              \
7029
}
7030
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7031
static always_inline void gen_##name (DisasContext *ctx)                      \
7032
{                                                                             \
7033
    TCGv_i32 t0, t1;                                                          \
7034
    if (unlikely(!ctx->spe_enabled)) {                                        \
7035
        GEN_EXCP_NO_AP(ctx);                                                  \
7036
        return;                                                               \
7037
    }                                                                         \
7038
    t0 = tcg_temp_new_i32();                                                  \
7039
    t1 = tcg_temp_new_i32();                                                  \
7040
    tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);                       \
7041
    tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);                       \
7042
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7043
    tcg_temp_free_i32(t0);                                                    \
7044
    tcg_temp_free_i32(t1);                                                    \
7045
}
7046
#define GEN_SPEFPUOP_COMP_64(name)                                            \
7047
static always_inline void gen_##name (DisasContext *ctx)                      \
7048
{                                                                             \
7049
    if (unlikely(!ctx->spe_enabled)) {                                        \
7050
        GEN_EXCP_NO_AP(ctx);                                                  \
7051
        return;                                                               \
7052
    }                                                                         \
7053
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7054
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7055
}
7056
#else
7057
#define GEN_SPEFPUOP_CONV_32_32(name)                                         \
7058
static always_inline void gen_##name (DisasContext *ctx)                      \
7059
{                                                                             \
7060
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7061
}
7062
#define GEN_SPEFPUOP_CONV_32_64(name)                                         \
7063
static always_inline void gen_##name (DisasContext *ctx)                      \
7064
{                                                                             \
7065
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7066
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7067
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)], t0);                          \
7068
    tcg_temp_free_i64(t0);                                                    \
7069
}
7070
#define GEN_SPEFPUOP_CONV_64_32(name)                                         \
7071
static always_inline void gen_##name (DisasContext *ctx)                      \
7072
{                                                                             \
7073
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7074
    gen_helper_##name(t0, cpu_gpr[rB(ctx->opcode)]);                          \
7075
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7076
    tcg_temp_free_i64(t0);                                                    \
7077
}
7078
#define GEN_SPEFPUOP_CONV_64_64(name)                                         \
7079
static always_inline void gen_##name (DisasContext *ctx)                      \
7080
{                                                                             \
7081
    TCGv_i64 t0 = tcg_temp_new_i64();                                         \
7082
    gen_load_gpr64(t0, rB(ctx->opcode));                                      \
7083
    gen_helper_##name(t0, t0);                                                \
7084
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7085
    tcg_temp_free_i64(t0);                                                    \
7086
}
7087
#define GEN_SPEFPUOP_ARITH2_32_32(name)                                       \
7088
static always_inline void gen_##name (DisasContext *ctx)                      \
7089
{                                                                             \
7090
    if (unlikely(!ctx->spe_enabled)) {                                        \
7091
        GEN_EXCP_NO_AP(ctx);                                                  \
7092
        return;                                                               \
7093
    }                                                                         \
7094
    gen_helper_##name(cpu_gpr[rD(ctx->opcode)],                               \
7095
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7096
}
7097
#define GEN_SPEFPUOP_ARITH2_64_64(name)                                       \
7098
static always_inline void gen_##name (DisasContext *ctx)                      \
7099
{                                                                             \
7100
    TCGv_i64 t0, t1;                                                          \
7101
    if (unlikely(!ctx->spe_enabled)) {                                        \
7102
        GEN_EXCP_NO_AP(ctx);                                                  \
7103
        return;                                                               \
7104
    }                                                                         \
7105
    t0 = tcg_temp_new_i64();                                                  \
7106
    t1 = tcg_temp_new_i64();                                                  \
7107
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7108
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7109
    gen_helper_##name(t0, t0, t1);                                            \
7110
    gen_store_gpr64(rD(ctx->opcode), t0);                                     \
7111
    tcg_temp_free_i64(t0);                                                    \
7112
    tcg_temp_free_i64(t1);                                                    \
7113
}
7114
#define GEN_SPEFPUOP_COMP_32(name)                                            \
7115
static always_inline void gen_##name (DisasContext *ctx)                      \
7116
{                                                                             \
7117
    if (unlikely(!ctx->spe_enabled)) {                                        \
7118
        GEN_EXCP_NO_AP(ctx);                                                  \
7119
        return;                                                               \
7120
    }                                                                         \
7121
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)],                             \
7122
                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);    \
7123
}
7124
#define GEN_SPEFPUOP_COMP_64(name)                                            \
7125
static always_inline void gen_##name (DisasContext *ctx)                      \
7126
{                                                                             \
7127
    TCGv_i64 t0, t1;                                                          \
7128
    if (unlikely(!ctx->spe_enabled)) {                                        \
7129
        GEN_EXCP_NO_AP(ctx);                                                  \
7130
        return;                                                               \
7131
    }                                                                         \
7132
    t0 = tcg_temp_new_i64();                                                  \
7133
    t1 = tcg_temp_new_i64();                                                  \
7134
    gen_load_gpr64(t0, rA(ctx->opcode));                                      \
7135
    gen_load_gpr64(t1, rB(ctx->opcode));                                      \
7136
    gen_helper_##name(cpu_crf[crfD(ctx->opcode)], t0, t1);                    \
7137
    tcg_temp_free_i64(t0);                                                    \
7138
    tcg_temp_free_i64(t1);                                                    \
7139
}
7140
#endif
7141

    
7142
/* Single precision floating-point vectors operations */
7143
/* Arithmetic */
7144
GEN_SPEFPUOP_ARITH2_64_64(evfsadd);
7145
GEN_SPEFPUOP_ARITH2_64_64(evfssub);
7146
GEN_SPEFPUOP_ARITH2_64_64(evfsmul);
7147
GEN_SPEFPUOP_ARITH2_64_64(evfsdiv);
7148
static always_inline void gen_evfsabs (DisasContext *ctx)
7149
{
7150
    if (unlikely(!ctx->spe_enabled)) {
7151
        GEN_EXCP_NO_AP(ctx);
7152
        return;
7153
    }
7154
#if defined(TARGET_PPC64)
7155
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000080000000LL);
7156
#else
7157
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x80000000);
7158
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7159
#endif
7160
}
7161
static always_inline void gen_evfsnabs (DisasContext *ctx)
7162
{
7163
    if (unlikely(!ctx->spe_enabled)) {
7164
        GEN_EXCP_NO_AP(ctx);
7165
        return;
7166
    }
7167
#if defined(TARGET_PPC64)
7168
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7169
#else
7170
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7171
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7172
#endif
7173
}
7174
static always_inline void gen_evfsneg (DisasContext *ctx)
7175
{
7176
    if (unlikely(!ctx->spe_enabled)) {
7177
        GEN_EXCP_NO_AP(ctx);
7178
        return;
7179
    }
7180
#if defined(TARGET_PPC64)
7181
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000080000000LL);
7182
#else
7183
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7184
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7185
#endif
7186
}
7187

    
7188
/* Conversion */
7189
GEN_SPEFPUOP_CONV_64_64(evfscfui);
7190
GEN_SPEFPUOP_CONV_64_64(evfscfsi);
7191
GEN_SPEFPUOP_CONV_64_64(evfscfuf);
7192
GEN_SPEFPUOP_CONV_64_64(evfscfsf);
7193
GEN_SPEFPUOP_CONV_64_64(evfsctui);
7194
GEN_SPEFPUOP_CONV_64_64(evfsctsi);
7195
GEN_SPEFPUOP_CONV_64_64(evfsctuf);
7196
GEN_SPEFPUOP_CONV_64_64(evfsctsf);
7197
GEN_SPEFPUOP_CONV_64_64(evfsctuiz);
7198
GEN_SPEFPUOP_CONV_64_64(evfsctsiz);
7199

    
7200
/* Comparison */
7201
GEN_SPEFPUOP_COMP_64(evfscmpgt);
7202
GEN_SPEFPUOP_COMP_64(evfscmplt);
7203
GEN_SPEFPUOP_COMP_64(evfscmpeq);
7204
GEN_SPEFPUOP_COMP_64(evfststgt);
7205
GEN_SPEFPUOP_COMP_64(evfststlt);
7206
GEN_SPEFPUOP_COMP_64(evfststeq);
7207

    
7208
/* Opcodes definitions */
7209
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
7210
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
7211
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
7212
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
7213
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
7214
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
7215
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
7216
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
7217
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
7218
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
7219
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
7220
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
7221
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
7222
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
7223

    
7224
/* Single precision floating-point operations */
7225
/* Arithmetic */
7226
GEN_SPEFPUOP_ARITH2_32_32(efsadd);
7227
GEN_SPEFPUOP_ARITH2_32_32(efssub);
7228
GEN_SPEFPUOP_ARITH2_32_32(efsmul);
7229
GEN_SPEFPUOP_ARITH2_32_32(efsdiv);
7230
static always_inline void gen_efsabs (DisasContext *ctx)
7231
{
7232
    if (unlikely(!ctx->spe_enabled)) {
7233
        GEN_EXCP_NO_AP(ctx);
7234
        return;
7235
    }
7236
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], (target_long)~0x80000000LL);
7237
}
7238
static always_inline void gen_efsnabs (DisasContext *ctx)
7239
{
7240
    if (unlikely(!ctx->spe_enabled)) {
7241
        GEN_EXCP_NO_AP(ctx);
7242
        return;
7243
    }
7244
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7245
}
7246
static always_inline void gen_efsneg (DisasContext *ctx)
7247
{
7248
    if (unlikely(!ctx->spe_enabled)) {
7249
        GEN_EXCP_NO_AP(ctx);
7250
        return;
7251
    }
7252
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x80000000);
7253
}
7254

    
7255
/* Conversion */
7256
GEN_SPEFPUOP_CONV_32_32(efscfui);
7257
GEN_SPEFPUOP_CONV_32_32(efscfsi);
7258
GEN_SPEFPUOP_CONV_32_32(efscfuf);
7259
GEN_SPEFPUOP_CONV_32_32(efscfsf);
7260
GEN_SPEFPUOP_CONV_32_32(efsctui);
7261
GEN_SPEFPUOP_CONV_32_32(efsctsi);
7262
GEN_SPEFPUOP_CONV_32_32(efsctuf);
7263
GEN_SPEFPUOP_CONV_32_32(efsctsf);
7264
GEN_SPEFPUOP_CONV_32_32(efsctuiz);
7265
GEN_SPEFPUOP_CONV_32_32(efsctsiz);
7266
GEN_SPEFPUOP_CONV_32_64(efscfd);
7267

    
7268
/* Comparison */
7269
GEN_SPEFPUOP_COMP_32(efscmpgt);
7270
GEN_SPEFPUOP_COMP_32(efscmplt);
7271
GEN_SPEFPUOP_COMP_32(efscmpeq);
7272
GEN_SPEFPUOP_COMP_32(efststgt);
7273
GEN_SPEFPUOP_COMP_32(efststlt);
7274
GEN_SPEFPUOP_COMP_32(efststeq);
7275

    
7276
/* Opcodes definitions */
7277
GEN_SPE(efsadd,         efssub,        0x00, 0x0B, 0x00000000, PPC_SPEFPU); //
7278
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
7279
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
7280
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
7281
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
7282
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
7283
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
7284
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
7285
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
7286
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
7287
GEN_SPE(efsctuiz,       speundef,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
7288
GEN_SPE(efsctsiz,       speundef,      0x0D, 0x0B, 0x00180000, PPC_SPEFPU); //
7289
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
7290
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
7291

    
7292
/* Double precision floating-point operations */
7293
/* Arithmetic */
7294
GEN_SPEFPUOP_ARITH2_64_64(efdadd);
7295
GEN_SPEFPUOP_ARITH2_64_64(efdsub);
7296
GEN_SPEFPUOP_ARITH2_64_64(efdmul);
7297
GEN_SPEFPUOP_ARITH2_64_64(efddiv);
7298
static always_inline void gen_efdabs (DisasContext *ctx)
7299
{
7300
    if (unlikely(!ctx->spe_enabled)) {
7301
        GEN_EXCP_NO_AP(ctx);
7302
        return;
7303
    }
7304
#if defined(TARGET_PPC64)
7305
    tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], ~0x8000000000000000LL);
7306
#else
7307
    tcg_gen_andi_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], ~0x80000000);
7308
#endif
7309
}
7310
static always_inline void gen_efdnabs (DisasContext *ctx)
7311
{
7312
    if (unlikely(!ctx->spe_enabled)) {
7313
        GEN_EXCP_NO_AP(ctx);
7314
        return;
7315
    }
7316
#if defined(TARGET_PPC64)
7317
    tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7318
#else
7319
    tcg_gen_ori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7320
#endif
7321
}
7322
static always_inline void gen_efdneg (DisasContext *ctx)
7323
{
7324
    if (unlikely(!ctx->spe_enabled)) {
7325
        GEN_EXCP_NO_AP(ctx);
7326
        return;
7327
    }
7328
#if defined(TARGET_PPC64)
7329
    tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 0x8000000000000000LL);
7330
#else
7331
    tcg_gen_xori_tl(cpu_gprh[rA(ctx->opcode)], cpu_gprh[rA(ctx->opcode)], 0x80000000);
7332
#endif
7333
}
7334

    
7335
/* Conversion */
7336
GEN_SPEFPUOP_CONV_64_32(efdcfui);
7337
GEN_SPEFPUOP_CONV_64_32(efdcfsi);
7338
GEN_SPEFPUOP_CONV_64_32(efdcfuf);
7339
GEN_SPEFPUOP_CONV_64_32(efdcfsf);
7340
GEN_SPEFPUOP_CONV_32_64(efdctui);
7341
GEN_SPEFPUOP_CONV_32_64(efdctsi);
7342
GEN_SPEFPUOP_CONV_32_64(efdctuf);
7343
GEN_SPEFPUOP_CONV_32_64(efdctsf);
7344
GEN_SPEFPUOP_CONV_32_64(efdctuiz);
7345
GEN_SPEFPUOP_CONV_32_64(efdctsiz);
7346
GEN_SPEFPUOP_CONV_64_32(efdcfs);
7347
GEN_SPEFPUOP_CONV_64_64(efdcfuid);
7348
GEN_SPEFPUOP_CONV_64_64(efdcfsid);
7349
GEN_SPEFPUOP_CONV_64_64(efdctuidz);
7350
GEN_SPEFPUOP_CONV_64_64(efdctsidz);
7351

    
7352
/* Comparison */
7353
GEN_SPEFPUOP_COMP_64(efdcmpgt);
7354
GEN_SPEFPUOP_COMP_64(efdcmplt);
7355
GEN_SPEFPUOP_COMP_64(efdcmpeq);
7356
GEN_SPEFPUOP_COMP_64(efdtstgt);
7357
GEN_SPEFPUOP_COMP_64(efdtstlt);
7358
GEN_SPEFPUOP_COMP_64(efdtsteq);
7359

    
7360
/* Opcodes definitions */
7361
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
7362
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
7363
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
7364
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
7365
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
7366
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
7367
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
7368
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
7369
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
7370
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
7371
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
7372
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
7373
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
7374
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
7375
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
7376
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
7377

    
7378
/* End opcode list */
7379
GEN_OPCODE_MARK(end);
7380

    
7381
#include "translate_init.c"
7382
#include "helper_regs.h"
7383

    
7384
/*****************************************************************************/
7385
/* Misc PowerPC helpers */
7386
void cpu_dump_state (CPUState *env, FILE *f,
7387
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7388
                     int flags)
7389
{
7390
#define RGPL  4
7391
#define RFPL  4
7392

    
7393
    int i;
7394

    
7395
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
7396
                env->nip, env->lr, env->ctr, env->xer);
7397
    cpu_fprintf(f, "MSR " ADDRX " HID0 " ADDRX "  HF " ADDRX " idx %d\n",
7398
                env->msr, env->spr[SPR_HID0], env->hflags, env->mmu_idx);
7399
#if !defined(NO_TIMER_DUMP)
7400
    cpu_fprintf(f, "TB %08x %08x "
7401
#if !defined(CONFIG_USER_ONLY)
7402
                "DECR %08x"
7403
#endif
7404
                "\n",
7405
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7406
#if !defined(CONFIG_USER_ONLY)
7407
                , cpu_ppc_load_decr(env)
7408
#endif
7409
                );
7410
#endif
7411
    for (i = 0; i < 32; i++) {
7412
        if ((i & (RGPL - 1)) == 0)
7413
            cpu_fprintf(f, "GPR%02d", i);
7414
        cpu_fprintf(f, " " REGX, ppc_dump_gpr(env, i));
7415
        if ((i & (RGPL - 1)) == (RGPL - 1))
7416
            cpu_fprintf(f, "\n");
7417
    }
7418
    cpu_fprintf(f, "CR ");
7419
    for (i = 0; i < 8; i++)
7420
        cpu_fprintf(f, "%01x", env->crf[i]);
7421
    cpu_fprintf(f, "  [");
7422
    for (i = 0; i < 8; i++) {
7423
        char a = '-';
7424
        if (env->crf[i] & 0x08)
7425
            a = 'L';
7426
        else if (env->crf[i] & 0x04)
7427
            a = 'G';
7428
        else if (env->crf[i] & 0x02)
7429
            a = 'E';
7430
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7431
    }
7432
    cpu_fprintf(f, " ]             RES " ADDRX "\n", env->reserve);
7433
    for (i = 0; i < 32; i++) {
7434
        if ((i & (RFPL - 1)) == 0)
7435
            cpu_fprintf(f, "FPR%02d", i);
7436
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
7437
        if ((i & (RFPL - 1)) == (RFPL - 1))
7438
            cpu_fprintf(f, "\n");
7439
    }
7440
#if !defined(CONFIG_USER_ONLY)
7441
    cpu_fprintf(f, "SRR0 " ADDRX " SRR1 " ADDRX " SDR1 " ADDRX "\n",
7442
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
7443
#endif
7444

    
7445
#undef RGPL
7446
#undef RFPL
7447
}
7448

    
7449
void cpu_dump_statistics (CPUState *env, FILE*f,
7450
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
7451
                          int flags)
7452
{
7453
#if defined(DO_PPC_STATISTICS)
7454
    opc_handler_t **t1, **t2, **t3, *handler;
7455
    int op1, op2, op3;
7456

    
7457
    t1 = env->opcodes;
7458
    for (op1 = 0; op1 < 64; op1++) {
7459
        handler = t1[op1];
7460
        if (is_indirect_opcode(handler)) {
7461
            t2 = ind_table(handler);
7462
            for (op2 = 0; op2 < 32; op2++) {
7463
                handler = t2[op2];
7464
                if (is_indirect_opcode(handler)) {
7465
                    t3 = ind_table(handler);
7466
                    for (op3 = 0; op3 < 32; op3++) {
7467
                        handler = t3[op3];
7468
                        if (handler->count == 0)
7469
                            continue;
7470
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
7471
                                    "%016llx %lld\n",
7472
                                    op1, op2, op3, op1, (op3 << 5) | op2,
7473
                                    handler->oname,
7474
                                    handler->count, handler->count);
7475
                    }
7476
                } else {
7477
                    if (handler->count == 0)
7478
                        continue;
7479
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
7480
                                "%016llx %lld\n",
7481
                                op1, op2, op1, op2, handler->oname,
7482
                                handler->count, handler->count);
7483
                }
7484
            }
7485
        } else {
7486
            if (handler->count == 0)
7487
                continue;
7488
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
7489
                        op1, op1, handler->oname,
7490
                        handler->count, handler->count);
7491
        }
7492
    }
7493
#endif
7494
}
7495

    
7496
/*****************************************************************************/
7497
static always_inline void gen_intermediate_code_internal (CPUState *env,
7498
                                                          TranslationBlock *tb,
7499
                                                          int search_pc)
7500
{
7501
    DisasContext ctx, *ctxp = &ctx;
7502
    opc_handler_t **table, *handler;
7503
    target_ulong pc_start;
7504
    uint16_t *gen_opc_end;
7505
    int supervisor, little_endian;
7506
    CPUBreakpoint *bp;
7507
    int j, lj = -1;
7508
    int num_insns;
7509
    int max_insns;
7510

    
7511
    pc_start = tb->pc;
7512
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
7513
#if defined(OPTIMIZE_FPRF_UPDATE)
7514
    gen_fprf_ptr = gen_fprf_buf;
7515
#endif
7516
    ctx.nip = pc_start;
7517
    ctx.tb = tb;
7518
    ctx.exception = POWERPC_EXCP_NONE;
7519
    ctx.spr_cb = env->spr_cb;
7520
    supervisor = env->mmu_idx;
7521
#if !defined(CONFIG_USER_ONLY)
7522
    ctx.supervisor = supervisor;
7523
#endif
7524
    little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
7525
#if defined(TARGET_PPC64)
7526
    ctx.sf_mode = msr_sf;
7527
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
7528
#else
7529
    ctx.mem_idx = (supervisor << 1) | little_endian;
7530
#endif
7531
    ctx.dcache_line_size = env->dcache_line_size;
7532
    ctx.fpu_enabled = msr_fp;
7533
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
7534
        ctx.spe_enabled = msr_spe;
7535
    else
7536
        ctx.spe_enabled = 0;
7537
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
7538
        ctx.altivec_enabled = msr_vr;
7539
    else
7540
        ctx.altivec_enabled = 0;
7541
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
7542
        ctx.singlestep_enabled = CPU_SINGLE_STEP;
7543
    else
7544
        ctx.singlestep_enabled = 0;
7545
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
7546
        ctx.singlestep_enabled |= CPU_BRANCH_STEP;
7547
    if (unlikely(env->singlestep_enabled))
7548
        ctx.singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7549
#if defined (DO_SINGLE_STEP) && 0
7550
    /* Single step trace mode */
7551
    msr_se = 1;
7552
#endif
7553
    num_insns = 0;
7554
    max_insns = tb->cflags & CF_COUNT_MASK;
7555
    if (max_insns == 0)
7556
        max_insns = CF_COUNT_MASK;
7557

    
7558
    gen_icount_start();
7559
    /* Set env in case of segfault during code fetch */
7560
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
7561
        if (unlikely(env->breakpoints)) {
7562
            for (bp = env->breakpoints; bp != NULL; bp = bp->next) {
7563
                if (bp->pc == ctx.nip) {
7564
                    gen_update_nip(&ctx, ctx.nip);
7565
                    gen_helper_raise_debug();
7566
                    break;
7567
                }
7568
            }
7569
        }
7570
        if (unlikely(search_pc)) {
7571
            j = gen_opc_ptr - gen_opc_buf;
7572
            if (lj < j) {
7573
                lj++;
7574
                while (lj < j)
7575
                    gen_opc_instr_start[lj++] = 0;
7576
                gen_opc_pc[lj] = ctx.nip;
7577
                gen_opc_instr_start[lj] = 1;
7578
                gen_opc_icount[lj] = num_insns;
7579
            }
7580
        }
7581
#if defined PPC_DEBUG_DISAS
7582
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7583
            fprintf(logfile, "----------------\n");
7584
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
7585
                    ctx.nip, supervisor, (int)msr_ir);
7586
        }
7587
#endif
7588
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
7589
            gen_io_start();
7590
        if (unlikely(little_endian)) {
7591
            ctx.opcode = bswap32(ldl_code(ctx.nip));
7592
        } else {
7593
            ctx.opcode = ldl_code(ctx.nip);
7594
        }
7595
#if defined PPC_DEBUG_DISAS
7596
        if (loglevel & CPU_LOG_TB_IN_ASM) {
7597
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
7598
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
7599
                    opc3(ctx.opcode), little_endian ? "little" : "big");
7600
        }
7601
#endif
7602
        ctx.nip += 4;
7603
        table = env->opcodes;
7604
        num_insns++;
7605
        handler = table[opc1(ctx.opcode)];
7606
        if (is_indirect_opcode(handler)) {
7607
            table = ind_table(handler);
7608
            handler = table[opc2(ctx.opcode)];
7609
            if (is_indirect_opcode(handler)) {
7610
                table = ind_table(handler);
7611
                handler = table[opc3(ctx.opcode)];
7612
            }
7613
        }
7614
        /* Is opcode *REALLY* valid ? */
7615
        if (unlikely(handler->handler == &gen_invalid)) {
7616
            if (loglevel != 0) {
7617
                fprintf(logfile, "invalid/unsupported opcode: "
7618
                        "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7619
                        opc1(ctx.opcode), opc2(ctx.opcode),
7620
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7621
            } else {
7622
                printf("invalid/unsupported opcode: "
7623
                       "%02x - %02x - %02x (%08x) " ADDRX " %d\n",
7624
                       opc1(ctx.opcode), opc2(ctx.opcode),
7625
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7626
            }
7627
        } else {
7628
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
7629
                if (loglevel != 0) {
7630
                    fprintf(logfile, "invalid bits: %08x for opcode: "
7631
                            "%02x - %02x - %02x (%08x) " ADDRX "\n",
7632
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
7633
                            opc2(ctx.opcode), opc3(ctx.opcode),
7634
                            ctx.opcode, ctx.nip - 4);
7635
                } else {
7636
                    printf("invalid bits: %08x for opcode: "
7637
                           "%02x - %02x - %02x (%08x) " ADDRX "\n",
7638
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
7639
                           opc2(ctx.opcode), opc3(ctx.opcode),
7640
                           ctx.opcode, ctx.nip - 4);
7641
                }
7642
                GEN_EXCP_INVAL(ctxp);
7643
                break;
7644
            }
7645
        }
7646
        (*(handler->handler))(&ctx);
7647
#if defined(DO_PPC_STATISTICS)
7648
        handler->count++;
7649
#endif
7650
        /* Check trace mode exceptions */
7651
        if (unlikely(ctx.singlestep_enabled & CPU_SINGLE_STEP &&
7652
                     (ctx.nip <= 0x100 || ctx.nip > 0xF00) &&
7653
                     ctx.exception != POWERPC_SYSCALL &&
7654
                     ctx.exception != POWERPC_EXCP_TRAP &&
7655
                     ctx.exception != POWERPC_EXCP_BRANCH)) {
7656
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
7657
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
7658
                            (env->singlestep_enabled) ||
7659
                            num_insns >= max_insns)) {
7660
            /* if we reach a page boundary or are single stepping, stop
7661
             * generation
7662
             */
7663
            break;
7664
        }
7665
#if defined (DO_SINGLE_STEP)
7666
        break;
7667
#endif
7668
    }
7669
    if (tb->cflags & CF_LAST_IO)
7670
        gen_io_end();
7671
    if (ctx.exception == POWERPC_EXCP_NONE) {
7672
        gen_goto_tb(&ctx, 0, ctx.nip);
7673
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7674
        if (unlikely(env->singlestep_enabled)) {
7675
            gen_update_nip(&ctx, ctx.nip);
7676
            gen_helper_raise_debug();
7677
        }
7678
        /* Generate the return instruction */
7679
        tcg_gen_exit_tb(0);
7680
    }
7681
    gen_icount_end(tb, num_insns);
7682
    *gen_opc_ptr = INDEX_op_end;
7683
    if (unlikely(search_pc)) {
7684
        j = gen_opc_ptr - gen_opc_buf;
7685
        lj++;
7686
        while (lj <= j)
7687
            gen_opc_instr_start[lj++] = 0;
7688
    } else {
7689
        tb->size = ctx.nip - pc_start;
7690
        tb->icount = num_insns;
7691
    }
7692
#if defined(DEBUG_DISAS)
7693
    if (loglevel & CPU_LOG_TB_CPU) {
7694
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7695
        cpu_dump_state(env, logfile, fprintf, 0);
7696
    }
7697
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7698
        int flags;
7699
        flags = env->bfd_mach;
7700
        flags |= little_endian << 16;
7701
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7702
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
7703
        fprintf(logfile, "\n");
7704
    }
7705
#endif
7706
}
7707

    
7708
void gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7709
{
7710
    gen_intermediate_code_internal(env, tb, 0);
7711
}
7712

    
7713
void gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7714
{
7715
    gen_intermediate_code_internal(env, tb, 1);
7716
}
7717

    
7718
void gen_pc_load(CPUState *env, TranslationBlock *tb,
7719
                unsigned long searched_pc, int pc_pos, void *puc)
7720
{
7721
    env->nip = gen_opc_pc[pc_pos];
7722
}