Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ d63001d1

History | View | Annotate | Download (222.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

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

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

    
44
enum {
45
#define DEF(s, n, copy_size) INDEX_op_ ## s,
46
#include "opc.h"
47
#undef DEF
48
    NB_OPS,
49
};
50

    
51
static uint16_t *gen_opc_ptr;
52
static uint32_t *gen_opparam_ptr;
53

    
54
#include "gen-op.h"
55

    
56
static inline void gen_set_T0 (target_ulong val)
57
{
58
#if defined(TARGET_PPC64)
59
    if (val >> 32)
60
        gen_op_set_T0_64(val >> 32, val);
61
    else
62
#endif
63
        gen_op_set_T0(val);
64
}
65

    
66
static inline void gen_set_T1 (target_ulong val)
67
{
68
#if defined(TARGET_PPC64)
69
    if (val >> 32)
70
        gen_op_set_T1_64(val >> 32, val);
71
    else
72
#endif
73
        gen_op_set_T1(val);
74
}
75

    
76
#define GEN8(func, NAME)                                                      \
77
static GenOpFunc *NAME ## _table [8] = {                                      \
78
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
79
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
80
};                                                                            \
81
static inline void func(int n)                                                \
82
{                                                                             \
83
    NAME ## _table[n]();                                                      \
84
}
85

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

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

    
114
/* Condition register moves */
115
GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);
116
GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);
117
GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);
118
GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);
119

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

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

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

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

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

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

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

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

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

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

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

    
228
#define GEN_EXCP_PRIVREG(ctx)                                                 \
229
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
230
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
231

    
232
#define GEN_EXCP_NO_FP(ctx)                                                   \
233
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
234

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

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

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

    
251
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
252
static void gen_##name (DisasContext *ctx);                                   \
253
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
254
static void gen_##name (DisasContext *ctx)
255

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

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

    
275
#define EXTRACT_SHELPER(name, shift, nb)                                      \
276
static inline int32_t name (uint32_t opcode)                                  \
277
{                                                                             \
278
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
279
}
280

    
281
/* Opcode part 1 */
282
EXTRACT_HELPER(opc1, 26, 6);
283
/* Opcode part 2 */
284
EXTRACT_HELPER(opc2, 1, 5);
285
/* Opcode part 3 */
286
EXTRACT_HELPER(opc3, 6, 5);
287
/* Update Cr0 flags */
288
EXTRACT_HELPER(Rc, 0, 1);
289
/* Destination */
290
EXTRACT_HELPER(rD, 21, 5);
291
/* Source */
292
EXTRACT_HELPER(rS, 21, 5);
293
/* First operand */
294
EXTRACT_HELPER(rA, 16, 5);
295
/* Second operand */
296
EXTRACT_HELPER(rB, 11, 5);
297
/* Third operand */
298
EXTRACT_HELPER(rC, 6, 5);
299
/***                               Get CRn                                 ***/
300
EXTRACT_HELPER(crfD, 23, 3);
301
EXTRACT_HELPER(crfS, 18, 3);
302
EXTRACT_HELPER(crbD, 21, 5);
303
EXTRACT_HELPER(crbA, 16, 5);
304
EXTRACT_HELPER(crbB, 11, 5);
305
/* SPR / TBL */
306
EXTRACT_HELPER(_SPR, 11, 10);
307
static inline uint32_t SPR (uint32_t opcode)
308
{
309
    uint32_t sprn = _SPR(opcode);
310

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

    
330
EXTRACT_HELPER(CRM, 12, 8);
331
EXTRACT_HELPER(FM, 17, 8);
332
EXTRACT_HELPER(SR, 16, 4);
333
EXTRACT_HELPER(FPIMM, 20, 4);
334

    
335
/***                            Jump target decoding                       ***/
336
/* Displacement */
337
EXTRACT_SHELPER(d, 0, 16);
338
/* Immediate address */
339
static inline target_ulong LI (uint32_t opcode)
340
{
341
    return (opcode >> 0) & 0x03FFFFFC;
342
}
343

    
344
static inline uint32_t BD (uint32_t opcode)
345
{
346
    return (opcode >> 0) & 0xFFFC;
347
}
348

    
349
EXTRACT_HELPER(BO, 21, 5);
350
EXTRACT_HELPER(BI, 16, 5);
351
/* Absolute/relative address */
352
EXTRACT_HELPER(AA, 1, 1);
353
/* Link */
354
EXTRACT_HELPER(LK, 0, 1);
355

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

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

    
381
    return ret;
382
}
383

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

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

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

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

    
552
/* Start opcode list */
553
GEN_OPCODE_MARK(start);
554

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

    
561
static opc_handler_t invalid_handler = {
562
    .inval   = 0xFFFFFFFF,
563
    .type    = PPC_NONE,
564
    .handler = gen_invalid,
565
};
566

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

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

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

    
609
/* Two operands arithmetic functions */
610
#define GEN_INT_ARITH2(name, opc1, opc2, opc3, type)                          \
611
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000, type)                    \
612
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
613

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

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

    
623
#if defined(TARGET_PPC64)
624
#define __GEN_INT_ARITH2_64(name, opc1, opc2, opc3, inval, type)              \
625
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
626
{                                                                             \
627
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
628
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
629
    if (ctx->sf_mode)                                                         \
630
        gen_op_##name##_64();                                                 \
631
    else                                                                      \
632
        gen_op_##name();                                                      \
633
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
634
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
635
        gen_set_Rc0(ctx);                                                     \
636
}
637

    
638
#define __GEN_INT_ARITH2_O_64(name, opc1, opc2, opc3, inval, type)            \
639
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
640
{                                                                             \
641
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
642
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
643
    if (ctx->sf_mode)                                                         \
644
        gen_op_##name##_64();                                                 \
645
    else                                                                      \
646
        gen_op_##name();                                                      \
647
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
648
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
649
        gen_set_Rc0(ctx);                                                     \
650
}
651

    
652
#define __GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                     \
653
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
654
{                                                                             \
655
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
656
    if (ctx->sf_mode)                                                         \
657
        gen_op_##name##_64();                                                 \
658
    else                                                                      \
659
        gen_op_##name();                                                      \
660
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
661
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
662
        gen_set_Rc0(ctx);                                                     \
663
}
664
#define __GEN_INT_ARITH1_O_64(name, opc1, opc2, opc3, type)                   \
665
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
666
{                                                                             \
667
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
668
    if (ctx->sf_mode)                                                         \
669
        gen_op_##name##_64();                                                 \
670
    else                                                                      \
671
        gen_op_##name();                                                      \
672
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
673
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
674
        gen_set_Rc0(ctx);                                                     \
675
}
676

    
677
/* Two operands arithmetic functions */
678
#define GEN_INT_ARITH2_64(name, opc1, opc2, opc3, type)                       \
679
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000000, type)                 \
680
__GEN_INT_ARITH2_O_64(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
681

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

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

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

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

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

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

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

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

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

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

    
1038
/* isel (PowerPC 2.03 specification) */
1039
GEN_HANDLER(isel, 0x1F, 0x0F, 0x00, 0x00000001, PPC_203)
1040
{
1041
    uint32_t bi = rC(ctx->opcode);
1042
    uint32_t mask;
1043

    
1044
    if (rA(ctx->opcode) == 0) {
1045
        gen_set_T0(0);
1046
    } else {
1047
        gen_op_load_gpr_T1(rA(ctx->opcode));
1048
    }
1049
    gen_op_load_gpr_T2(rB(ctx->opcode));
1050
    mask = 1 << (3 - (bi & 0x03));
1051
    gen_op_load_crf_T0(bi >> 2);
1052
    gen_op_test_true(mask);
1053
    gen_op_isel();
1054
    gen_op_store_T0_gpr(rD(ctx->opcode));
1055
}
1056

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

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

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

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

    
1115
/* or & or. */
1116
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1117
{
1118
    int rs, ra, rb;
1119

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

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

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

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

    
1238
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1239
        /* NOP */
1240
        return;
1241
    }
1242
    gen_op_load_gpr_T0(rS(ctx->opcode));
1243
    if (likely(uimm != 0))
1244
        gen_op_xori(uimm);
1245
    gen_op_store_T0_gpr(rA(ctx->opcode));
1246
}
1247

    
1248
/* xoris */
1249
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1250
{
1251
    target_ulong uimm = UIMM(ctx->opcode);
1252

    
1253
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1254
        /* NOP */
1255
        return;
1256
    }
1257
    gen_op_load_gpr_T0(rS(ctx->opcode));
1258
    if (likely(uimm != 0))
1259
        gen_op_xori(uimm << 16);
1260
    gen_op_store_T0_gpr(rA(ctx->opcode));
1261
}
1262

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

    
1276
#if defined(TARGET_PPC64)
1277
/* extsw & extsw. */
1278
GEN_LOGICAL1(extsw, 0x1E, PPC_64B);
1279
/* cntlzd */
1280
GEN_LOGICAL1(cntlzd, 0x01, PPC_64B);
1281
#endif
1282

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

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

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

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

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

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

    
1418
static inline void gen_andi_T1_64 (DisasContext *ctx, uint64_t mask)
1419
{
1420
    if (mask >> 32)
1421
        gen_op_andi_T1_64(mask >> 32, mask & 0xFFFFFFFF);
1422
    else
1423
        gen_op_andi_T1(mask);
1424
}
1425

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

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

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

    
1480
    sh = SH(ctx->opcode) | (shn << 5);
1481
    mb = MB(ctx->opcode) | (mbn << 5);
1482
    gen_rldinm(ctx, mb, 63 - sh, sh);
1483
}
1484
GEN_PPC64_R4(rldic, 0x1E, 0x04);
1485

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

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

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

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

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

    
1553
/***                             Integer shift                             ***/
1554
/* slw & slw. */
1555
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
1556
/* sraw & sraw. */
1557
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
1558
/* srawi & srawi. */
1559
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1560
{
1561
    int mb, me;
1562
    gen_op_load_gpr_T0(rS(ctx->opcode));
1563
    if (SH(ctx->opcode) != 0) {
1564
        gen_op_move_T1_T0();
1565
        mb = 32 - SH(ctx->opcode);
1566
        me = 31;
1567
#if defined(TARGET_PPC64)
1568
        mb += 32;
1569
        me += 32;
1570
#endif
1571
        gen_op_srawi(SH(ctx->opcode), MASK(mb, me));
1572
    }
1573
    gen_op_store_T0_gpr(rA(ctx->opcode));
1574
    if (unlikely(Rc(ctx->opcode) != 0))
1575
        gen_set_Rc0(ctx);
1576
}
1577
/* srw & srw. */
1578
__GEN_LOGICAL2(srw, 0x18, 0x10, PPC_INTEGER);
1579

    
1580
#if defined(TARGET_PPC64)
1581
/* sld & sld. */
1582
__GEN_LOGICAL2(sld, 0x1B, 0x00, PPC_64B);
1583
/* srad & srad. */
1584
__GEN_LOGICAL2(srad, 0x1A, 0x18, PPC_64B);
1585
/* sradi & sradi. */
1586
static inline void gen_sradi (DisasContext *ctx, int n)
1587
{
1588
    uint64_t mask;
1589
    int sh, mb, me;
1590

    
1591
    gen_op_load_gpr_T0(rS(ctx->opcode));
1592
    sh = SH(ctx->opcode) + (n << 5);
1593
    if (sh != 0) {
1594
        gen_op_move_T1_T0();
1595
        mb = 64 - SH(ctx->opcode);
1596
        me = 63;
1597
        mask = MASK(mb, me);
1598
        gen_op_sradi(sh, mask >> 32, mask);
1599
    }
1600
    gen_op_store_T0_gpr(rA(ctx->opcode));
1601
    if (unlikely(Rc(ctx->opcode) != 0))
1602
        gen_set_Rc0(ctx);
1603
}
1604
GEN_HANDLER(sradi0, 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
1605
{
1606
    gen_sradi(ctx, 0);
1607
}
1608
GEN_HANDLER(sradi1, 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
1609
{
1610
    gen_sradi(ctx, 1);
1611
}
1612
/* srd & srd. */
1613
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
1614
#endif
1615

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

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

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

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

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

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

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

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

    
1725
/* fres */
1726
GEN_FLOAT_BS(res, 0x3B, 0x18, PPC_FLOAT_FRES);
1727

    
1728
/* frsqrte */
1729
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, PPC_FLOAT_FRSQRTE);
1730

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

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

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

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

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

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

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

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

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

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

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

    
1866
/* mffs */
1867
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
1868
{
1869
    if (unlikely(!ctx->fpu_enabled)) {
1870
        GEN_EXCP_NO_FP(ctx);
1871
        return;
1872
    }
1873
    gen_op_load_fpscr();
1874
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1875
    if (unlikely(Rc(ctx->opcode) != 0))
1876
        gen_op_set_Rc1();
1877
}
1878

    
1879
/* mtfsb0 */
1880
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
1881
{
1882
    uint8_t crb;
1883

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

    
1896
/* mtfsb1 */
1897
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
1898
{
1899
    uint8_t crb;
1900

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

    
1913
/* mtfsf */
1914
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
1915
{
1916
    if (unlikely(!ctx->fpu_enabled)) {
1917
        GEN_EXCP_NO_FP(ctx);
1918
        return;
1919
    }
1920
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1921
    gen_op_store_fpscr(FM(ctx->opcode));
1922
    if (unlikely(Rc(ctx->opcode) != 0))
1923
        gen_op_set_Rc1();
1924
}
1925

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

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

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

    
1957
static inline void gen_addr_reg_index (DisasContext *ctx)
1958
{
1959
    if (rA(ctx->opcode) == 0) {
1960
        gen_op_load_gpr_T0(rB(ctx->opcode));
1961
    } else {
1962
        gen_op_load_gpr_T0(rA(ctx->opcode));
1963
        gen_op_load_gpr_T1(rB(ctx->opcode));
1964
        gen_op_add();
1965
    }
1966
#ifdef DEBUG_MEMORY_ACCESSES
1967
    gen_op_print_mem_EA();
1968
#endif
1969
}
1970

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

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

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

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

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

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

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

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

    
2216
    /* Restore CPU state */
2217
    if (unlikely(ctx->supervisor == 0)) {
2218
        GEN_EXCP_PRIVOPC(ctx);
2219
        return;
2220
    }
2221
    ra = rA(ctx->opcode);
2222
    rd = rD(ctx->opcode);
2223
    if (unlikely((rd & 1) || rd == ra)) {
2224
        GEN_EXCP_INVAL(ctx);
2225
        return;
2226
    }
2227
    if (unlikely(ctx->mem_idx & 1)) {
2228
        /* Little-endian mode is not handled */
2229
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2230
        return;
2231
    }
2232
    gen_addr_imm_index(ctx, 0x0F);
2233
    op_ldst(ld);
2234
    gen_op_store_T1_gpr(rd);
2235
    gen_op_addi(8);
2236
    op_ldst(ld);
2237
    gen_op_store_T1_gpr(rd + 1);
2238
#endif
2239
}
2240
#endif
2241

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

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

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

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

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

    
2295
/* stb stbu stbux stbx */
2296
GEN_STS(b, 0x06, PPC_INTEGER);
2297
/* sth sthu sthux sthx */
2298
GEN_STS(h, 0x0C, PPC_INTEGER);
2299
/* stw stwu stwux stwx */
2300
GEN_STS(w, 0x04, PPC_INTEGER);
2301
#if defined(TARGET_PPC64)
2302
OP_ST_TABLE(d);
2303
GEN_STUX(d, 0x15, 0x05, PPC_64B);
2304
GEN_STX(d, 0x15, 0x04, PPC_64B);
2305
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2306
{
2307
    int rs;
2308

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

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

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

    
2446
/* stmw */
2447
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
2448
{
2449
    /* NIP cannot be restored if the memory exception comes from an helper */
2450
    gen_update_nip(ctx, ctx->nip - 4);
2451
    gen_addr_imm_index(ctx, 0);
2452
    op_ldstm(stmw, rS(ctx->opcode));
2453
}
2454

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

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

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

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

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

    
2604
/* stswi */
2605
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
2606
{
2607
    int nb = NB(ctx->opcode);
2608

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

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

    
2628
/***                        Memory synchronisation                         ***/
2629
/* eieio */
2630
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
2631
{
2632
}
2633

    
2634
/* isync */
2635
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
2636
{
2637
    GEN_STOP(ctx);
2638
}
2639

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2885
/* lfd lfdu lfdux lfdx */
2886
GEN_LDFS(fd, 0x12, PPC_FLOAT);
2887
/* lfs lfsu lfsux lfsx */
2888
GEN_LDFS(fs, 0x10, PPC_FLOAT);
2889

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

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

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

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

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

    
2956
/* stfd stfdu stfdux stfdx */
2957
GEN_STFS(fd, 0x16, PPC_FLOAT);
2958
/* stfs stfsu stfsux stfsx */
2959
GEN_STFS(fs, 0x14, PPC_FLOAT);
2960

    
2961
/* Optional: */
2962
/* stfiwx */
2963
OP_ST_TABLE(fiwx);
2964
GEN_STXF(fiwx, 0x17, 0x1E, PPC_FLOAT_STFIWX);
2965

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

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

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

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

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

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

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

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

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

    
3198
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3199
{
3200
    gen_bcond(ctx, BCOND_LR);
3201
}
3202

    
3203
/***                      Condition register logical                       ***/
3204
#define GEN_CRLOGIC(op, opc)                                                  \
3205
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
3206
{                                                                             \
3207
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
3208
    gen_op_getbit_T0(3 - (crbA(ctx->opcode) & 0x03));                         \
3209
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
3210
    gen_op_getbit_T1(3 - (crbB(ctx->opcode) & 0x03));                         \
3211
    gen_op_##op();                                                            \
3212
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
3213
    gen_op_setcrfbit(~(1 << (3 - (crbD(ctx->opcode) & 0x03))),                \
3214
                     3 - (crbD(ctx->opcode) & 0x03));                         \
3215
    gen_op_store_T1_crf(crbD(ctx->opcode) >> 2);                              \
3216
}
3217

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

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

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

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

    
3292
/* sc */
3293
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3294
{
3295
    uint32_t lev;
3296

    
3297
    lev = (ctx->opcode >> 5) & 0x7F;
3298
#if defined(CONFIG_USER_ONLY)
3299
    GEN_EXCP(ctx, POWERPC_EXCP_SYSCALL_USER, lev);
3300
#else
3301
    GEN_EXCP(ctx, POWERPC_EXCP_SYSCALL, lev);
3302
#endif
3303
}
3304

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

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

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

    
3337
/* tdi */
3338
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3339
{
3340
    gen_op_load_gpr_T0(rA(ctx->opcode));
3341
    gen_set_T1(SIMM(ctx->opcode));
3342
    /* Update the nip since this might generate a trap exception */
3343
    gen_update_nip(ctx, ctx->nip);
3344
    gen_op_td(TO(ctx->opcode));
3345
}
3346
#endif
3347

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

    
3358
/* mfcr */
3359
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3360
{
3361
    uint32_t crm, crn;
3362

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

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

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

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

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

    
3443
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3444
{
3445
    gen_op_mfspr(ctx);
3446
}
3447

    
3448
/* mftb */
3449
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3450
{
3451
    gen_op_mfspr(ctx);
3452
}
3453

    
3454
/* mtcrf */
3455
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3456
{
3457
    uint32_t crm, crn;
3458

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

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

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

    
3533
/* mtspr */
3534
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
3535
{
3536
    void (*write_cb)(void *opaque, int sprn);
3537
    uint32_t sprn = SPR(ctx->opcode);
3538

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

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

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

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

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

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

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

    
3768
static inline void handler_dcbz (DisasContext *ctx, int dcache_line_size)
3769
{
3770
    int n;
3771

    
3772
    switch (dcache_line_size) {
3773
    case 32:
3774
        n = 0;
3775
        break;
3776
    case 64:
3777
        n = 1;
3778
        break;
3779
    case 128:
3780
        n = 2;
3781
        break;
3782
    default:
3783
        n = 3;
3784
        break;
3785
    }
3786
    op_dcbz(n);
3787
}
3788

    
3789
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
3790
{
3791
    gen_addr_reg_index(ctx);
3792
    handler_dcbz(ctx, ctx->dcache_line_size);
3793
    gen_op_check_reservation();
3794
}
3795

    
3796
GEN_HANDLER(dcbz_970, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
3797
{
3798
    gen_addr_reg_index(ctx);
3799
    if (ctx->opcode & 0x00200000)
3800
        handler_dcbz(ctx, ctx->dcache_line_size);
3801
    else
3802
        handler_dcbz(ctx, -1);
3803
    gen_op_check_reservation();
3804
}
3805

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

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

    
3856
/* Optional: */
3857
/* dcba */
3858
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
3859
{
3860
    /* interpreted as no-op */
3861
    /* XXX: specification say this is treated as a store by the MMU
3862
     *      but does not generate any exception
3863
     */
3864
}
3865

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

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

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

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

    
3934
/***                      Lookaside buffer management                      ***/
3935
/* Optional & supervisor only: */
3936
/* tlbia */
3937
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
3938
{
3939
#if defined(CONFIG_USER_ONLY)
3940
    GEN_EXCP_PRIVOPC(ctx);
3941
#else
3942
    if (unlikely(!ctx->supervisor)) {
3943
        if (loglevel != 0)
3944
            fprintf(logfile, "%s: ! supervisor\n", __func__);
3945
        GEN_EXCP_PRIVOPC(ctx);
3946
        return;
3947
    }
3948
    gen_op_tlbia();
3949
#endif
3950
}
3951

    
3952
/* tlbie */
3953
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
3954
{
3955
#if defined(CONFIG_USER_ONLY)
3956
    GEN_EXCP_PRIVOPC(ctx);
3957
#else
3958
    if (unlikely(!ctx->supervisor)) {
3959
        GEN_EXCP_PRIVOPC(ctx);
3960
        return;
3961
    }
3962
    gen_op_load_gpr_T0(rB(ctx->opcode));
3963
#if defined(TARGET_PPC64)
3964
    if (ctx->sf_mode)
3965
        gen_op_tlbie_64();
3966
    else
3967
#endif
3968
        gen_op_tlbie();
3969
#endif
3970
}
3971

    
3972
/* tlbsync */
3973
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
3974
{
3975
#if defined(CONFIG_USER_ONLY)
3976
    GEN_EXCP_PRIVOPC(ctx);
3977
#else
3978
    if (unlikely(!ctx->supervisor)) {
3979
        GEN_EXCP_PRIVOPC(ctx);
3980
        return;
3981
    }
3982
    /* This has no effect: it should ensure that all previous
3983
     * tlbie have completed
3984
     */
3985
    GEN_STOP(ctx);
3986
#endif
3987
}
3988

    
3989
#if defined(TARGET_PPC64)
3990
/* slbia */
3991
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
3992
{
3993
#if defined(CONFIG_USER_ONLY)
3994
    GEN_EXCP_PRIVOPC(ctx);
3995
#else
3996
    if (unlikely(!ctx->supervisor)) {
3997
        if (loglevel != 0)
3998
            fprintf(logfile, "%s: ! supervisor\n", __func__);
3999
        GEN_EXCP_PRIVOPC(ctx);
4000
        return;
4001
    }
4002
    gen_op_slbia();
4003
#endif
4004
}
4005

    
4006
/* slbie */
4007
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4008
{
4009
#if defined(CONFIG_USER_ONLY)
4010
    GEN_EXCP_PRIVOPC(ctx);
4011
#else
4012
    if (unlikely(!ctx->supervisor)) {
4013
        GEN_EXCP_PRIVOPC(ctx);
4014
        return;
4015
    }
4016
    gen_op_load_gpr_T0(rB(ctx->opcode));
4017
    gen_op_slbie();
4018
#endif
4019
}
4020
#endif
4021

    
4022
/***                              External control                         ***/
4023
/* Optional: */
4024
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
4025
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4026
#if defined(CONFIG_USER_ONLY)
4027
/* User-mode only */
4028
static GenOpFunc *gen_op_eciwx[] = {
4029
    &gen_op_eciwx_raw,
4030
    &gen_op_eciwx_le_raw,
4031
#if defined(TARGET_PPC64)
4032
    &gen_op_eciwx_64_raw,
4033
    &gen_op_eciwx_le_64_raw,
4034
#endif
4035
};
4036
static GenOpFunc *gen_op_ecowx[] = {
4037
    &gen_op_ecowx_raw,
4038
    &gen_op_ecowx_le_raw,
4039
#if defined(TARGET_PPC64)
4040
    &gen_op_ecowx_64_raw,
4041
    &gen_op_ecowx_le_64_raw,
4042
#endif
4043
};
4044
#else
4045
#if defined(TARGET_PPC64)
4046
/* Full system - 64 bits mode */
4047
static GenOpFunc *gen_op_eciwx[] = {
4048
    &gen_op_eciwx_user,
4049
    &gen_op_eciwx_le_user,
4050
    &gen_op_eciwx_64_user,
4051
    &gen_op_eciwx_le_64_user,
4052
    &gen_op_eciwx_kernel,
4053
    &gen_op_eciwx_le_kernel,
4054
    &gen_op_eciwx_64_kernel,
4055
    &gen_op_eciwx_le_64_kernel,
4056
#if defined(TARGET_PPC64H)
4057
    &gen_op_eciwx_hypv,
4058
    &gen_op_eciwx_le_hypv,
4059
    &gen_op_eciwx_64_hypv,
4060
    &gen_op_eciwx_le_64_hypv,
4061
#endif
4062
};
4063
static GenOpFunc *gen_op_ecowx[] = {
4064
    &gen_op_ecowx_user,
4065
    &gen_op_ecowx_le_user,
4066
    &gen_op_ecowx_64_user,
4067
    &gen_op_ecowx_le_64_user,
4068
    &gen_op_ecowx_kernel,
4069
    &gen_op_ecowx_le_kernel,
4070
    &gen_op_ecowx_64_kernel,
4071
    &gen_op_ecowx_le_64_kernel,
4072
#if defined(TARGET_PPC64H)
4073
    &gen_op_ecowx_hypv,
4074
    &gen_op_ecowx_le_hypv,
4075
    &gen_op_ecowx_64_hypv,
4076
    &gen_op_ecowx_le_64_hypv,
4077
#endif
4078
};
4079
#else
4080
/* Full system - 32 bits mode */
4081
static GenOpFunc *gen_op_eciwx[] = {
4082
    &gen_op_eciwx_user,
4083
    &gen_op_eciwx_le_user,
4084
    &gen_op_eciwx_kernel,
4085
    &gen_op_eciwx_le_kernel,
4086
};
4087
static GenOpFunc *gen_op_ecowx[] = {
4088
    &gen_op_ecowx_user,
4089
    &gen_op_ecowx_le_user,
4090
    &gen_op_ecowx_kernel,
4091
    &gen_op_ecowx_le_kernel,
4092
};
4093
#endif
4094
#endif
4095

    
4096
/* eciwx */
4097
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4098
{
4099
    /* Should check EAR[E] & alignment ! */
4100
    gen_addr_reg_index(ctx);
4101
    op_eciwx();
4102
    gen_op_store_T0_gpr(rD(ctx->opcode));
4103
}
4104

    
4105
/* ecowx */
4106
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4107
{
4108
    /* Should check EAR[E] & alignment ! */
4109
    gen_addr_reg_index(ctx);
4110
    gen_op_load_gpr_T1(rS(ctx->opcode));
4111
    op_ecowx();
4112
}
4113

    
4114
/* PowerPC 601 specific instructions */
4115
/* abs - abs. */
4116
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4117
{
4118
    gen_op_load_gpr_T0(rA(ctx->opcode));
4119
    gen_op_POWER_abs();
4120
    gen_op_store_T0_gpr(rD(ctx->opcode));
4121
    if (unlikely(Rc(ctx->opcode) != 0))
4122
        gen_set_Rc0(ctx);
4123
}
4124

    
4125
/* abso - abso. */
4126
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4127
{
4128
    gen_op_load_gpr_T0(rA(ctx->opcode));
4129
    gen_op_POWER_abso();
4130
    gen_op_store_T0_gpr(rD(ctx->opcode));
4131
    if (unlikely(Rc(ctx->opcode) != 0))
4132
        gen_set_Rc0(ctx);
4133
}
4134

    
4135
/* clcs */
4136
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4137
{
4138
    gen_op_load_gpr_T0(rA(ctx->opcode));
4139
    gen_op_POWER_clcs();
4140
    gen_op_store_T0_gpr(rD(ctx->opcode));
4141
}
4142

    
4143
/* div - div. */
4144
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4145
{
4146
    gen_op_load_gpr_T0(rA(ctx->opcode));
4147
    gen_op_load_gpr_T1(rB(ctx->opcode));
4148
    gen_op_POWER_div();
4149
    gen_op_store_T0_gpr(rD(ctx->opcode));
4150
    if (unlikely(Rc(ctx->opcode) != 0))
4151
        gen_set_Rc0(ctx);
4152
}
4153

    
4154
/* divo - divo. */
4155
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4156
{
4157
    gen_op_load_gpr_T0(rA(ctx->opcode));
4158
    gen_op_load_gpr_T1(rB(ctx->opcode));
4159
    gen_op_POWER_divo();
4160
    gen_op_store_T0_gpr(rD(ctx->opcode));
4161
    if (unlikely(Rc(ctx->opcode) != 0))
4162
        gen_set_Rc0(ctx);
4163
}
4164

    
4165
/* divs - divs. */
4166
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4167
{
4168
    gen_op_load_gpr_T0(rA(ctx->opcode));
4169
    gen_op_load_gpr_T1(rB(ctx->opcode));
4170
    gen_op_POWER_divs();
4171
    gen_op_store_T0_gpr(rD(ctx->opcode));
4172
    if (unlikely(Rc(ctx->opcode) != 0))
4173
        gen_set_Rc0(ctx);
4174
}
4175

    
4176
/* divso - divso. */
4177
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4178
{
4179
    gen_op_load_gpr_T0(rA(ctx->opcode));
4180
    gen_op_load_gpr_T1(rB(ctx->opcode));
4181
    gen_op_POWER_divso();
4182
    gen_op_store_T0_gpr(rD(ctx->opcode));
4183
    if (unlikely(Rc(ctx->opcode) != 0))
4184
        gen_set_Rc0(ctx);
4185
}
4186

    
4187
/* doz - doz. */
4188
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4189
{
4190
    gen_op_load_gpr_T0(rA(ctx->opcode));
4191
    gen_op_load_gpr_T1(rB(ctx->opcode));
4192
    gen_op_POWER_doz();
4193
    gen_op_store_T0_gpr(rD(ctx->opcode));
4194
    if (unlikely(Rc(ctx->opcode) != 0))
4195
        gen_set_Rc0(ctx);
4196
}
4197

    
4198
/* dozo - dozo. */
4199
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4200
{
4201
    gen_op_load_gpr_T0(rA(ctx->opcode));
4202
    gen_op_load_gpr_T1(rB(ctx->opcode));
4203
    gen_op_POWER_dozo();
4204
    gen_op_store_T0_gpr(rD(ctx->opcode));
4205
    if (unlikely(Rc(ctx->opcode) != 0))
4206
        gen_set_Rc0(ctx);
4207
}
4208

    
4209
/* dozi */
4210
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4211
{
4212
    gen_op_load_gpr_T0(rA(ctx->opcode));
4213
    gen_op_set_T1(SIMM(ctx->opcode));
4214
    gen_op_POWER_doz();
4215
    gen_op_store_T0_gpr(rD(ctx->opcode));
4216
}
4217

    
4218
/* As lscbx load from memory byte after byte, it's always endian safe */
4219
#define op_POWER_lscbx(start, ra, rb)                                         \
4220
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4221
#if defined(CONFIG_USER_ONLY)
4222
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
4223
    &gen_op_POWER_lscbx_raw,
4224
    &gen_op_POWER_lscbx_raw,
4225
};
4226
#else
4227
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
4228
    &gen_op_POWER_lscbx_user,
4229
    &gen_op_POWER_lscbx_user,
4230
    &gen_op_POWER_lscbx_kernel,
4231
    &gen_op_POWER_lscbx_kernel,
4232
};
4233
#endif
4234

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

    
4241
    gen_addr_reg_index(ctx);
4242
    if (ra == 0) {
4243
        ra = rb;
4244
    }
4245
    /* NIP cannot be restored if the memory exception comes from an helper */
4246
    gen_update_nip(ctx, ctx->nip - 4);
4247
    gen_op_load_xer_bc();
4248
    gen_op_load_xer_cmp();
4249
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
4250
    gen_op_store_xer_bc();
4251
    if (unlikely(Rc(ctx->opcode) != 0))
4252
        gen_set_Rc0(ctx);
4253
}
4254

    
4255
/* maskg - maskg. */
4256
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4257
{
4258
    gen_op_load_gpr_T0(rS(ctx->opcode));
4259
    gen_op_load_gpr_T1(rB(ctx->opcode));
4260
    gen_op_POWER_maskg();
4261
    gen_op_store_T0_gpr(rA(ctx->opcode));
4262
    if (unlikely(Rc(ctx->opcode) != 0))
4263
        gen_set_Rc0(ctx);
4264
}
4265

    
4266
/* maskir - maskir. */
4267
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4268
{
4269
    gen_op_load_gpr_T0(rA(ctx->opcode));
4270
    gen_op_load_gpr_T1(rS(ctx->opcode));
4271
    gen_op_load_gpr_T2(rB(ctx->opcode));
4272
    gen_op_POWER_maskir();
4273
    gen_op_store_T0_gpr(rA(ctx->opcode));
4274
    if (unlikely(Rc(ctx->opcode) != 0))
4275
        gen_set_Rc0(ctx);
4276
}
4277

    
4278
/* mul - mul. */
4279
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4280
{
4281
    gen_op_load_gpr_T0(rA(ctx->opcode));
4282
    gen_op_load_gpr_T1(rB(ctx->opcode));
4283
    gen_op_POWER_mul();
4284
    gen_op_store_T0_gpr(rD(ctx->opcode));
4285
    if (unlikely(Rc(ctx->opcode) != 0))
4286
        gen_set_Rc0(ctx);
4287
}
4288

    
4289
/* mulo - mulo. */
4290
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4291
{
4292
    gen_op_load_gpr_T0(rA(ctx->opcode));
4293
    gen_op_load_gpr_T1(rB(ctx->opcode));
4294
    gen_op_POWER_mulo();
4295
    gen_op_store_T0_gpr(rD(ctx->opcode));
4296
    if (unlikely(Rc(ctx->opcode) != 0))
4297
        gen_set_Rc0(ctx);
4298
}
4299

    
4300
/* nabs - nabs. */
4301
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4302
{
4303
    gen_op_load_gpr_T0(rA(ctx->opcode));
4304
    gen_op_POWER_nabs();
4305
    gen_op_store_T0_gpr(rD(ctx->opcode));
4306
    if (unlikely(Rc(ctx->opcode) != 0))
4307
        gen_set_Rc0(ctx);
4308
}
4309

    
4310
/* nabso - nabso. */
4311
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4312
{
4313
    gen_op_load_gpr_T0(rA(ctx->opcode));
4314
    gen_op_POWER_nabso();
4315
    gen_op_store_T0_gpr(rD(ctx->opcode));
4316
    if (unlikely(Rc(ctx->opcode) != 0))
4317
        gen_set_Rc0(ctx);
4318
}
4319

    
4320
/* rlmi - rlmi. */
4321
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4322
{
4323
    uint32_t mb, me;
4324

    
4325
    mb = MB(ctx->opcode);
4326
    me = ME(ctx->opcode);
4327
    gen_op_load_gpr_T0(rS(ctx->opcode));
4328
    gen_op_load_gpr_T1(rA(ctx->opcode));
4329
    gen_op_load_gpr_T2(rB(ctx->opcode));
4330
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
4331
    gen_op_store_T0_gpr(rA(ctx->opcode));
4332
    if (unlikely(Rc(ctx->opcode) != 0))
4333
        gen_set_Rc0(ctx);
4334
}
4335

    
4336
/* rrib - rrib. */
4337
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4338
{
4339
    gen_op_load_gpr_T0(rS(ctx->opcode));
4340
    gen_op_load_gpr_T1(rA(ctx->opcode));
4341
    gen_op_load_gpr_T2(rB(ctx->opcode));
4342
    gen_op_POWER_rrib();
4343
    gen_op_store_T0_gpr(rA(ctx->opcode));
4344
    if (unlikely(Rc(ctx->opcode) != 0))
4345
        gen_set_Rc0(ctx);
4346
}
4347

    
4348
/* sle - sle. */
4349
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4350
{
4351
    gen_op_load_gpr_T0(rS(ctx->opcode));
4352
    gen_op_load_gpr_T1(rB(ctx->opcode));
4353
    gen_op_POWER_sle();
4354
    gen_op_store_T0_gpr(rA(ctx->opcode));
4355
    if (unlikely(Rc(ctx->opcode) != 0))
4356
        gen_set_Rc0(ctx);
4357
}
4358

    
4359
/* sleq - sleq. */
4360
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4361
{
4362
    gen_op_load_gpr_T0(rS(ctx->opcode));
4363
    gen_op_load_gpr_T1(rB(ctx->opcode));
4364
    gen_op_POWER_sleq();
4365
    gen_op_store_T0_gpr(rA(ctx->opcode));
4366
    if (unlikely(Rc(ctx->opcode) != 0))
4367
        gen_set_Rc0(ctx);
4368
}
4369

    
4370
/* sliq - sliq. */
4371
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4372
{
4373
    gen_op_load_gpr_T0(rS(ctx->opcode));
4374
    gen_op_set_T1(SH(ctx->opcode));
4375
    gen_op_POWER_sle();
4376
    gen_op_store_T0_gpr(rA(ctx->opcode));
4377
    if (unlikely(Rc(ctx->opcode) != 0))
4378
        gen_set_Rc0(ctx);
4379
}
4380

    
4381
/* slliq - slliq. */
4382
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4383
{
4384
    gen_op_load_gpr_T0(rS(ctx->opcode));
4385
    gen_op_set_T1(SH(ctx->opcode));
4386
    gen_op_POWER_sleq();
4387
    gen_op_store_T0_gpr(rA(ctx->opcode));
4388
    if (unlikely(Rc(ctx->opcode) != 0))
4389
        gen_set_Rc0(ctx);
4390
}
4391

    
4392
/* sllq - sllq. */
4393
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4394
{
4395
    gen_op_load_gpr_T0(rS(ctx->opcode));
4396
    gen_op_load_gpr_T1(rB(ctx->opcode));
4397
    gen_op_POWER_sllq();
4398
    gen_op_store_T0_gpr(rA(ctx->opcode));
4399
    if (unlikely(Rc(ctx->opcode) != 0))
4400
        gen_set_Rc0(ctx);
4401
}
4402

    
4403
/* slq - slq. */
4404
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4405
{
4406
    gen_op_load_gpr_T0(rS(ctx->opcode));
4407
    gen_op_load_gpr_T1(rB(ctx->opcode));
4408
    gen_op_POWER_slq();
4409
    gen_op_store_T0_gpr(rA(ctx->opcode));
4410
    if (unlikely(Rc(ctx->opcode) != 0))
4411
        gen_set_Rc0(ctx);
4412
}
4413

    
4414
/* sraiq - sraiq. */
4415
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4416
{
4417
    gen_op_load_gpr_T0(rS(ctx->opcode));
4418
    gen_op_set_T1(SH(ctx->opcode));
4419
    gen_op_POWER_sraq();
4420
    gen_op_store_T0_gpr(rA(ctx->opcode));
4421
    if (unlikely(Rc(ctx->opcode) != 0))
4422
        gen_set_Rc0(ctx);
4423
}
4424

    
4425
/* sraq - sraq. */
4426
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4427
{
4428
    gen_op_load_gpr_T0(rS(ctx->opcode));
4429
    gen_op_load_gpr_T1(rB(ctx->opcode));
4430
    gen_op_POWER_sraq();
4431
    gen_op_store_T0_gpr(rA(ctx->opcode));
4432
    if (unlikely(Rc(ctx->opcode) != 0))
4433
        gen_set_Rc0(ctx);
4434
}
4435

    
4436
/* sre - sre. */
4437
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4438
{
4439
    gen_op_load_gpr_T0(rS(ctx->opcode));
4440
    gen_op_load_gpr_T1(rB(ctx->opcode));
4441
    gen_op_POWER_sre();
4442
    gen_op_store_T0_gpr(rA(ctx->opcode));
4443
    if (unlikely(Rc(ctx->opcode) != 0))
4444
        gen_set_Rc0(ctx);
4445
}
4446

    
4447
/* srea - srea. */
4448
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4449
{
4450
    gen_op_load_gpr_T0(rS(ctx->opcode));
4451
    gen_op_load_gpr_T1(rB(ctx->opcode));
4452
    gen_op_POWER_srea();
4453
    gen_op_store_T0_gpr(rA(ctx->opcode));
4454
    if (unlikely(Rc(ctx->opcode) != 0))
4455
        gen_set_Rc0(ctx);
4456
}
4457

    
4458
/* sreq */
4459
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4460
{
4461
    gen_op_load_gpr_T0(rS(ctx->opcode));
4462
    gen_op_load_gpr_T1(rB(ctx->opcode));
4463
    gen_op_POWER_sreq();
4464
    gen_op_store_T0_gpr(rA(ctx->opcode));
4465
    if (unlikely(Rc(ctx->opcode) != 0))
4466
        gen_set_Rc0(ctx);
4467
}
4468

    
4469
/* sriq */
4470
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4471
{
4472
    gen_op_load_gpr_T0(rS(ctx->opcode));
4473
    gen_op_set_T1(SH(ctx->opcode));
4474
    gen_op_POWER_srq();
4475
    gen_op_store_T0_gpr(rA(ctx->opcode));
4476
    if (unlikely(Rc(ctx->opcode) != 0))
4477
        gen_set_Rc0(ctx);
4478
}
4479

    
4480
/* srliq */
4481
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4482
{
4483
    gen_op_load_gpr_T0(rS(ctx->opcode));
4484
    gen_op_load_gpr_T1(rB(ctx->opcode));
4485
    gen_op_set_T1(SH(ctx->opcode));
4486
    gen_op_POWER_srlq();
4487
    gen_op_store_T0_gpr(rA(ctx->opcode));
4488
    if (unlikely(Rc(ctx->opcode) != 0))
4489
        gen_set_Rc0(ctx);
4490
}
4491

    
4492
/* srlq */
4493
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
4494
{
4495
    gen_op_load_gpr_T0(rS(ctx->opcode));
4496
    gen_op_load_gpr_T1(rB(ctx->opcode));
4497
    gen_op_POWER_srlq();
4498
    gen_op_store_T0_gpr(rA(ctx->opcode));
4499
    if (unlikely(Rc(ctx->opcode) != 0))
4500
        gen_set_Rc0(ctx);
4501
}
4502

    
4503
/* srq */
4504
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
4505
{
4506
    gen_op_load_gpr_T0(rS(ctx->opcode));
4507
    gen_op_load_gpr_T1(rB(ctx->opcode));
4508
    gen_op_POWER_srq();
4509
    gen_op_store_T0_gpr(rA(ctx->opcode));
4510
    if (unlikely(Rc(ctx->opcode) != 0))
4511
        gen_set_Rc0(ctx);
4512
}
4513

    
4514
/* PowerPC 602 specific instructions */
4515
/* dsa  */
4516
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
4517
{
4518
    /* XXX: TODO */
4519
    GEN_EXCP_INVAL(ctx);
4520
}
4521

    
4522
/* esa */
4523
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
4524
{
4525
    /* XXX: TODO */
4526
    GEN_EXCP_INVAL(ctx);
4527
}
4528

    
4529
/* mfrom */
4530
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
4531
{
4532
#if defined(CONFIG_USER_ONLY)
4533
    GEN_EXCP_PRIVOPC(ctx);
4534
#else
4535
    if (unlikely(!ctx->supervisor)) {
4536
        GEN_EXCP_PRIVOPC(ctx);
4537
        return;
4538
    }
4539
    gen_op_load_gpr_T0(rA(ctx->opcode));
4540
    gen_op_602_mfrom();
4541
    gen_op_store_T0_gpr(rD(ctx->opcode));
4542
#endif
4543
}
4544

    
4545
/* 602 - 603 - G2 TLB management */
4546
/* tlbld */
4547
GEN_HANDLER(tlbld_6xx, 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4548
{
4549
#if defined(CONFIG_USER_ONLY)
4550
    GEN_EXCP_PRIVOPC(ctx);
4551
#else
4552
    if (unlikely(!ctx->supervisor)) {
4553
        GEN_EXCP_PRIVOPC(ctx);
4554
        return;
4555
    }
4556
    gen_op_load_gpr_T0(rB(ctx->opcode));
4557
    gen_op_6xx_tlbld();
4558
#endif
4559
}
4560

    
4561
/* tlbli */
4562
GEN_HANDLER(tlbli_6xx, 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4563
{
4564
#if defined(CONFIG_USER_ONLY)
4565
    GEN_EXCP_PRIVOPC(ctx);
4566
#else
4567
    if (unlikely(!ctx->supervisor)) {
4568
        GEN_EXCP_PRIVOPC(ctx);
4569
        return;
4570
    }
4571
    gen_op_load_gpr_T0(rB(ctx->opcode));
4572
    gen_op_6xx_tlbli();
4573
#endif
4574
}
4575

    
4576
/* 74xx TLB management */
4577
/* tlbld */
4578
GEN_HANDLER(tlbld_74xx, 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4579
{
4580
#if defined(CONFIG_USER_ONLY)
4581
    GEN_EXCP_PRIVOPC(ctx);
4582
#else
4583
    if (unlikely(!ctx->supervisor)) {
4584
        GEN_EXCP_PRIVOPC(ctx);
4585
        return;
4586
    }
4587
    gen_op_load_gpr_T0(rB(ctx->opcode));
4588
    gen_op_74xx_tlbld();
4589
#endif
4590
}
4591

    
4592
/* tlbli */
4593
GEN_HANDLER(tlbli_74xx, 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
4594
{
4595
#if defined(CONFIG_USER_ONLY)
4596
    GEN_EXCP_PRIVOPC(ctx);
4597
#else
4598
    if (unlikely(!ctx->supervisor)) {
4599
        GEN_EXCP_PRIVOPC(ctx);
4600
        return;
4601
    }
4602
    gen_op_load_gpr_T0(rB(ctx->opcode));
4603
    gen_op_74xx_tlbli();
4604
#endif
4605
}
4606

    
4607
/* POWER instructions not in PowerPC 601 */
4608
/* clf */
4609
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
4610
{
4611
    /* Cache line flush: implemented as no-op */
4612
}
4613

    
4614
/* cli */
4615
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
4616
{
4617
    /* Cache line invalidate: privileged and treated as no-op */
4618
#if defined(CONFIG_USER_ONLY)
4619
    GEN_EXCP_PRIVOPC(ctx);
4620
#else
4621
    if (unlikely(!ctx->supervisor)) {
4622
        GEN_EXCP_PRIVOPC(ctx);
4623
        return;
4624
    }
4625
#endif
4626
}
4627

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

    
4634
GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
4635
{
4636
#if defined(CONFIG_USER_ONLY)
4637
    GEN_EXCP_PRIVOPC(ctx);
4638
#else
4639
    if (unlikely(!ctx->supervisor)) {
4640
        GEN_EXCP_PRIVOPC(ctx);
4641
        return;
4642
    }
4643
    int ra = rA(ctx->opcode);
4644
    int rd = rD(ctx->opcode);
4645

    
4646
    gen_addr_reg_index(ctx);
4647
    gen_op_POWER_mfsri();
4648
    gen_op_store_T0_gpr(rd);
4649
    if (ra != 0 && ra != rd)
4650
        gen_op_store_T1_gpr(ra);
4651
#endif
4652
}
4653

    
4654
GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
4655
{
4656
#if defined(CONFIG_USER_ONLY)
4657
    GEN_EXCP_PRIVOPC(ctx);
4658
#else
4659
    if (unlikely(!ctx->supervisor)) {
4660
        GEN_EXCP_PRIVOPC(ctx);
4661
        return;
4662
    }
4663
    gen_addr_reg_index(ctx);
4664
    gen_op_POWER_rac();
4665
    gen_op_store_T0_gpr(rD(ctx->opcode));
4666
#endif
4667
}
4668

    
4669
GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
4670
{
4671
#if defined(CONFIG_USER_ONLY)
4672
    GEN_EXCP_PRIVOPC(ctx);
4673
#else
4674
    if (unlikely(!ctx->supervisor)) {
4675
        GEN_EXCP_PRIVOPC(ctx);
4676
        return;
4677
    }
4678
    gen_op_POWER_rfsvc();
4679
    GEN_SYNC(ctx);
4680
#endif
4681
}
4682

    
4683
/* svc is not implemented for now */
4684

    
4685
/* POWER2 specific instructions */
4686
/* Quad manipulation (load/store two floats at a time) */
4687
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
4688
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
4689
#if defined(CONFIG_USER_ONLY)
4690
static GenOpFunc *gen_op_POWER2_lfq[] = {
4691
    &gen_op_POWER2_lfq_le_raw,
4692
    &gen_op_POWER2_lfq_raw,
4693
};
4694
static GenOpFunc *gen_op_POWER2_stfq[] = {
4695
    &gen_op_POWER2_stfq_le_raw,
4696
    &gen_op_POWER2_stfq_raw,
4697
};
4698
#else
4699
static GenOpFunc *gen_op_POWER2_lfq[] = {
4700
    &gen_op_POWER2_lfq_le_user,
4701
    &gen_op_POWER2_lfq_user,
4702
    &gen_op_POWER2_lfq_le_kernel,
4703
    &gen_op_POWER2_lfq_kernel,
4704
};
4705
static GenOpFunc *gen_op_POWER2_stfq[] = {
4706
    &gen_op_POWER2_stfq_le_user,
4707
    &gen_op_POWER2_stfq_user,
4708
    &gen_op_POWER2_stfq_le_kernel,
4709
    &gen_op_POWER2_stfq_kernel,
4710
};
4711
#endif
4712

    
4713
/* lfq */
4714
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4715
{
4716
    /* NIP cannot be restored if the memory exception comes from an helper */
4717
    gen_update_nip(ctx, ctx->nip - 4);
4718
    gen_addr_imm_index(ctx, 0);
4719
    op_POWER2_lfq();
4720
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4721
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4722
}
4723

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

    
4729
    /* NIP cannot be restored if the memory exception comes from an helper */
4730
    gen_update_nip(ctx, ctx->nip - 4);
4731
    gen_addr_imm_index(ctx, 0);
4732
    op_POWER2_lfq();
4733
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4734
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4735
    if (ra != 0)
4736
        gen_op_store_T0_gpr(ra);
4737
}
4738

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

    
4744
    /* NIP cannot be restored if the memory exception comes from an helper */
4745
    gen_update_nip(ctx, ctx->nip - 4);
4746
    gen_addr_reg_index(ctx);
4747
    op_POWER2_lfq();
4748
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4749
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4750
    if (ra != 0)
4751
        gen_op_store_T0_gpr(ra);
4752
}
4753

    
4754
/* lfqx */
4755
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
4756
{
4757
    /* NIP cannot be restored if the memory exception comes from an helper */
4758
    gen_update_nip(ctx, ctx->nip - 4);
4759
    gen_addr_reg_index(ctx);
4760
    op_POWER2_lfq();
4761
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4762
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4763
}
4764

    
4765
/* stfq */
4766
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4767
{
4768
    /* NIP cannot be restored if the memory exception comes from an helper */
4769
    gen_update_nip(ctx, ctx->nip - 4);
4770
    gen_addr_imm_index(ctx, 0);
4771
    gen_op_load_fpr_FT0(rS(ctx->opcode));
4772
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
4773
    op_POWER2_stfq();
4774
}
4775

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

    
4781
    /* NIP cannot be restored if the memory exception comes from an helper */
4782
    gen_update_nip(ctx, ctx->nip - 4);
4783
    gen_addr_imm_index(ctx, 0);
4784
    gen_op_load_fpr_FT0(rS(ctx->opcode));
4785
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
4786
    op_POWER2_stfq();
4787
    if (ra != 0)
4788
        gen_op_store_T0_gpr(ra);
4789
}
4790

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

    
4796
    /* NIP cannot be restored if the memory exception comes from an helper */
4797
    gen_update_nip(ctx, ctx->nip - 4);
4798
    gen_addr_reg_index(ctx);
4799
    gen_op_load_fpr_FT0(rS(ctx->opcode));
4800
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
4801
    op_POWER2_stfq();
4802
    if (ra != 0)
4803
        gen_op_store_T0_gpr(ra);
4804
}
4805

    
4806
/* stfqx */
4807
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
4808
{
4809
    /* NIP cannot be restored if the memory exception comes from an helper */
4810
    gen_update_nip(ctx, ctx->nip - 4);
4811
    gen_addr_reg_index(ctx);
4812
    gen_op_load_fpr_FT0(rS(ctx->opcode));
4813
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
4814
    op_POWER2_stfq();
4815
}
4816

    
4817
/* BookE specific instructions */
4818
/* XXX: not implemented on 440 ? */
4819
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_BOOKE_EXT)
4820
{
4821
    /* XXX: TODO */
4822
    GEN_EXCP_INVAL(ctx);
4823
}
4824

    
4825
/* XXX: not implemented on 440 ? */
4826
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_BOOKE_EXT)
4827
{
4828
#if defined(CONFIG_USER_ONLY)
4829
    GEN_EXCP_PRIVOPC(ctx);
4830
#else
4831
    if (unlikely(!ctx->supervisor)) {
4832
        GEN_EXCP_PRIVOPC(ctx);
4833
        return;
4834
    }
4835
    gen_addr_reg_index(ctx);
4836
    /* Use the same micro-ops as for tlbie */
4837
#if defined(TARGET_PPC64)
4838
    if (ctx->sf_mode)
4839
        gen_op_tlbie_64();
4840
    else
4841
#endif
4842
        gen_op_tlbie();
4843
#endif
4844
}
4845

    
4846
/* All 405 MAC instructions are translated here */
4847
static inline void gen_405_mulladd_insn (DisasContext *ctx, int opc2, int opc3,
4848
                                         int ra, int rb, int rt, int Rc)
4849
{
4850
    gen_op_load_gpr_T0(ra);
4851
    gen_op_load_gpr_T1(rb);
4852
    switch (opc3 & 0x0D) {
4853
    case 0x05:
4854
        /* macchw    - macchw.    - macchwo   - macchwo.   */
4855
        /* macchws   - macchws.   - macchwso  - macchwso.  */
4856
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
4857
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
4858
        /* mulchw - mulchw. */
4859
        gen_op_405_mulchw();
4860
        break;
4861
    case 0x04:
4862
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
4863
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
4864
        /* mulchwu - mulchwu. */
4865
        gen_op_405_mulchwu();
4866
        break;
4867
    case 0x01:
4868
        /* machhw    - machhw.    - machhwo   - machhwo.   */
4869
        /* machhws   - machhws.   - machhwso  - machhwso.  */
4870
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
4871
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
4872
        /* mulhhw - mulhhw. */
4873
        gen_op_405_mulhhw();
4874
        break;
4875
    case 0x00:
4876
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
4877
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
4878
        /* mulhhwu - mulhhwu. */
4879
        gen_op_405_mulhhwu();
4880
        break;
4881
    case 0x0D:
4882
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
4883
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
4884
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
4885
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
4886
        /* mullhw - mullhw. */
4887
        gen_op_405_mullhw();
4888
        break;
4889
    case 0x0C:
4890
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
4891
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
4892
        /* mullhwu - mullhwu. */
4893
        gen_op_405_mullhwu();
4894
        break;
4895
    }
4896
    if (opc2 & 0x02) {
4897
        /* nmultiply-and-accumulate (0x0E) */
4898
        gen_op_neg();
4899
    }
4900
    if (opc2 & 0x04) {
4901
        /* (n)multiply-and-accumulate (0x0C - 0x0E) */
4902
        gen_op_load_gpr_T2(rt);
4903
        gen_op_move_T1_T0();
4904
        gen_op_405_add_T0_T2();
4905
    }
4906
    if (opc3 & 0x10) {
4907
        /* Check overflow */
4908
        if (opc3 & 0x01)
4909
            gen_op_405_check_ov();
4910
        else
4911
            gen_op_405_check_ovu();
4912
    }
4913
    if (opc3 & 0x02) {
4914
        /* Saturate */
4915
        if (opc3 & 0x01)
4916
            gen_op_405_check_sat();
4917
        else
4918
            gen_op_405_check_satu();
4919
    }
4920
    gen_op_store_T0_gpr(rt);
4921
    if (unlikely(Rc) != 0) {
4922
        /* Update Rc0 */
4923
        gen_set_Rc0(ctx);
4924
    }
4925
}
4926

    
4927
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
4928
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
4929
{                                                                             \
4930
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
4931
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
4932
}
4933

    
4934
/* macchw    - macchw.    */
4935
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
4936
/* macchwo   - macchwo.   */
4937
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
4938
/* macchws   - macchws.   */
4939
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
4940
/* macchwso  - macchwso.  */
4941
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
4942
/* macchwsu  - macchwsu.  */
4943
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
4944
/* macchwsuo - macchwsuo. */
4945
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
4946
/* macchwu   - macchwu.   */
4947
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
4948
/* macchwuo  - macchwuo.  */
4949
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
4950
/* machhw    - machhw.    */
4951
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
4952
/* machhwo   - machhwo.   */
4953
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
4954
/* machhws   - machhws.   */
4955
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
4956
/* machhwso  - machhwso.  */
4957
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
4958
/* machhwsu  - machhwsu.  */
4959
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
4960
/* machhwsuo - machhwsuo. */
4961
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
4962
/* machhwu   - machhwu.   */
4963
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
4964
/* machhwuo  - machhwuo.  */
4965
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
4966
/* maclhw    - maclhw.    */
4967
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
4968
/* maclhwo   - maclhwo.   */
4969
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
4970
/* maclhws   - maclhws.   */
4971
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
4972
/* maclhwso  - maclhwso.  */
4973
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
4974
/* maclhwu   - maclhwu.   */
4975
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
4976
/* maclhwuo  - maclhwuo.  */
4977
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
4978
/* maclhwsu  - maclhwsu.  */
4979
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
4980
/* maclhwsuo - maclhwsuo. */
4981
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
4982
/* nmacchw   - nmacchw.   */
4983
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
4984
/* nmacchwo  - nmacchwo.  */
4985
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
4986
/* nmacchws  - nmacchws.  */
4987
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
4988
/* nmacchwso - nmacchwso. */
4989
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
4990
/* nmachhw   - nmachhw.   */
4991
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
4992
/* nmachhwo  - nmachhwo.  */
4993
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
4994
/* nmachhws  - nmachhws.  */
4995
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
4996
/* nmachhwso - nmachhwso. */
4997
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
4998
/* nmaclhw   - nmaclhw.   */
4999
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5000
/* nmaclhwo  - nmaclhwo.  */
5001
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5002
/* nmaclhws  - nmaclhws.  */
5003
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5004
/* nmaclhwso - nmaclhwso. */
5005
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5006

    
5007
/* mulchw  - mulchw.  */
5008
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5009
/* mulchwu - mulchwu. */
5010
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5011
/* mulhhw  - mulhhw.  */
5012
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5013
/* mulhhwu - mulhhwu. */
5014
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5015
/* mullhw  - mullhw.  */
5016
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5017
/* mullhwu - mullhwu. */
5018
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5019

    
5020
/* mfdcr */
5021
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_EMB_COMMON)
5022
{
5023
#if defined(CONFIG_USER_ONLY)
5024
    GEN_EXCP_PRIVREG(ctx);
5025
#else
5026
    uint32_t dcrn = SPR(ctx->opcode);
5027

    
5028
    if (unlikely(!ctx->supervisor)) {
5029
        GEN_EXCP_PRIVREG(ctx);
5030
        return;
5031
    }
5032
    gen_op_set_T0(dcrn);
5033
    gen_op_load_dcr();
5034
    gen_op_store_T0_gpr(rD(ctx->opcode));
5035
#endif
5036
}
5037

    
5038
/* mtdcr */
5039
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_EMB_COMMON)
5040
{
5041
#if defined(CONFIG_USER_ONLY)
5042
    GEN_EXCP_PRIVREG(ctx);
5043
#else
5044
    uint32_t dcrn = SPR(ctx->opcode);
5045

    
5046
    if (unlikely(!ctx->supervisor)) {
5047
        GEN_EXCP_PRIVREG(ctx);
5048
        return;
5049
    }
5050
    gen_op_set_T0(dcrn);
5051
    gen_op_load_gpr_T1(rS(ctx->opcode));
5052
    gen_op_store_dcr();
5053
#endif
5054
}
5055

    
5056
/* mfdcrx */
5057
/* XXX: not implemented on 440 ? */
5058
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_BOOKE_EXT)
5059
{
5060
#if defined(CONFIG_USER_ONLY)
5061
    GEN_EXCP_PRIVREG(ctx);
5062
#else
5063
    if (unlikely(!ctx->supervisor)) {
5064
        GEN_EXCP_PRIVREG(ctx);
5065
        return;
5066
    }
5067
    gen_op_load_gpr_T0(rA(ctx->opcode));
5068
    gen_op_load_dcr();
5069
    gen_op_store_T0_gpr(rD(ctx->opcode));
5070
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5071
#endif
5072
}
5073

    
5074
/* mtdcrx */
5075
/* XXX: not implemented on 440 ? */
5076
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_BOOKE_EXT)
5077
{
5078
#if defined(CONFIG_USER_ONLY)
5079
    GEN_EXCP_PRIVREG(ctx);
5080
#else
5081
    if (unlikely(!ctx->supervisor)) {
5082
        GEN_EXCP_PRIVREG(ctx);
5083
        return;
5084
    }
5085
    gen_op_load_gpr_T0(rA(ctx->opcode));
5086
    gen_op_load_gpr_T1(rS(ctx->opcode));
5087
    gen_op_store_dcr();
5088
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5089
#endif
5090
}
5091

    
5092
/* mfdcrux (PPC 460) : user-mode access to DCR */
5093
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5094
{
5095
    gen_op_load_gpr_T0(rA(ctx->opcode));
5096
    gen_op_load_dcr();
5097
    gen_op_store_T0_gpr(rD(ctx->opcode));
5098
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5099
}
5100

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

    
5110
/* dccci */
5111
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5112
{
5113
#if defined(CONFIG_USER_ONLY)
5114
    GEN_EXCP_PRIVOPC(ctx);
5115
#else
5116
    if (unlikely(!ctx->supervisor)) {
5117
        GEN_EXCP_PRIVOPC(ctx);
5118
        return;
5119
    }
5120
    /* interpreted as no-op */
5121
#endif
5122
}
5123

    
5124
/* dcread */
5125
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5126
{
5127
#if defined(CONFIG_USER_ONLY)
5128
    GEN_EXCP_PRIVOPC(ctx);
5129
#else
5130
    if (unlikely(!ctx->supervisor)) {
5131
        GEN_EXCP_PRIVOPC(ctx);
5132
        return;
5133
    }
5134
    gen_addr_reg_index(ctx);
5135
    op_ldst(lwz);
5136
    gen_op_store_T0_gpr(rD(ctx->opcode));
5137
#endif
5138
}
5139

    
5140
/* icbt */
5141
GEN_HANDLER(icbt_40x, 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5142
{
5143
    /* interpreted as no-op */
5144
    /* XXX: specification say this is treated as a load by the MMU
5145
     *      but does not generate any exception
5146
     */
5147
}
5148

    
5149
/* iccci */
5150
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5151
{
5152
#if defined(CONFIG_USER_ONLY)
5153
    GEN_EXCP_PRIVOPC(ctx);
5154
#else
5155
    if (unlikely(!ctx->supervisor)) {
5156
        GEN_EXCP_PRIVOPC(ctx);
5157
        return;
5158
    }
5159
    /* interpreted as no-op */
5160
#endif
5161
}
5162

    
5163
/* icread */
5164
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5165
{
5166
#if defined(CONFIG_USER_ONLY)
5167
    GEN_EXCP_PRIVOPC(ctx);
5168
#else
5169
    if (unlikely(!ctx->supervisor)) {
5170
        GEN_EXCP_PRIVOPC(ctx);
5171
        return;
5172
    }
5173
    /* interpreted as no-op */
5174
#endif
5175
}
5176

    
5177
/* rfci (supervisor only) */
5178
GEN_HANDLER(rfci_40x, 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
5179
{
5180
#if defined(CONFIG_USER_ONLY)
5181
    GEN_EXCP_PRIVOPC(ctx);
5182
#else
5183
    if (unlikely(!ctx->supervisor)) {
5184
        GEN_EXCP_PRIVOPC(ctx);
5185
        return;
5186
    }
5187
    /* Restore CPU state */
5188
    gen_op_40x_rfci();
5189
    GEN_SYNC(ctx);
5190
#endif
5191
}
5192

    
5193
GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5194
{
5195
#if defined(CONFIG_USER_ONLY)
5196
    GEN_EXCP_PRIVOPC(ctx);
5197
#else
5198
    if (unlikely(!ctx->supervisor)) {
5199
        GEN_EXCP_PRIVOPC(ctx);
5200
        return;
5201
    }
5202
    /* Restore CPU state */
5203
    gen_op_rfci();
5204
    GEN_SYNC(ctx);
5205
#endif
5206
}
5207

    
5208
/* BookE specific */
5209
/* XXX: not implemented on 440 ? */
5210
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_BOOKE_EXT)
5211
{
5212
#if defined(CONFIG_USER_ONLY)
5213
    GEN_EXCP_PRIVOPC(ctx);
5214
#else
5215
    if (unlikely(!ctx->supervisor)) {
5216
        GEN_EXCP_PRIVOPC(ctx);
5217
        return;
5218
    }
5219
    /* Restore CPU state */
5220
    gen_op_rfdi();
5221
    GEN_SYNC(ctx);
5222
#endif
5223
}
5224

    
5225
/* XXX: not implemented on 440 ? */
5226
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5227
{
5228
#if defined(CONFIG_USER_ONLY)
5229
    GEN_EXCP_PRIVOPC(ctx);
5230
#else
5231
    if (unlikely(!ctx->supervisor)) {
5232
        GEN_EXCP_PRIVOPC(ctx);
5233
        return;
5234
    }
5235
    /* Restore CPU state */
5236
    gen_op_rfmci();
5237
    GEN_SYNC(ctx);
5238
#endif
5239
}
5240

    
5241
/* TLB management - PowerPC 405 implementation */
5242
/* tlbre */
5243
GEN_HANDLER(tlbre_40x, 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5244
{
5245
#if defined(CONFIG_USER_ONLY)
5246
    GEN_EXCP_PRIVOPC(ctx);
5247
#else
5248
    if (unlikely(!ctx->supervisor)) {
5249
        GEN_EXCP_PRIVOPC(ctx);
5250
        return;
5251
    }
5252
    switch (rB(ctx->opcode)) {
5253
    case 0:
5254
        gen_op_load_gpr_T0(rA(ctx->opcode));
5255
        gen_op_4xx_tlbre_hi();
5256
        gen_op_store_T0_gpr(rD(ctx->opcode));
5257
        break;
5258
    case 1:
5259
        gen_op_load_gpr_T0(rA(ctx->opcode));
5260
        gen_op_4xx_tlbre_lo();
5261
        gen_op_store_T0_gpr(rD(ctx->opcode));
5262
        break;
5263
    default:
5264
        GEN_EXCP_INVAL(ctx);
5265
        break;
5266
    }
5267
#endif
5268
}
5269

    
5270
/* tlbsx - tlbsx. */
5271
GEN_HANDLER(tlbsx_40x, 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5272
{
5273
#if defined(CONFIG_USER_ONLY)
5274
    GEN_EXCP_PRIVOPC(ctx);
5275
#else
5276
    if (unlikely(!ctx->supervisor)) {
5277
        GEN_EXCP_PRIVOPC(ctx);
5278
        return;
5279
    }
5280
    gen_addr_reg_index(ctx);
5281
    gen_op_4xx_tlbsx();
5282
    if (Rc(ctx->opcode))
5283
        gen_op_4xx_tlbsx_check();
5284
    gen_op_store_T0_gpr(rD(ctx->opcode));
5285
#endif
5286
}
5287

    
5288
/* tlbwe */
5289
GEN_HANDLER(tlbwe_40x, 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5290
{
5291
#if defined(CONFIG_USER_ONLY)
5292
    GEN_EXCP_PRIVOPC(ctx);
5293
#else
5294
    if (unlikely(!ctx->supervisor)) {
5295
        GEN_EXCP_PRIVOPC(ctx);
5296
        return;
5297
    }
5298
    switch (rB(ctx->opcode)) {
5299
    case 0:
5300
        gen_op_load_gpr_T0(rA(ctx->opcode));
5301
        gen_op_load_gpr_T1(rS(ctx->opcode));
5302
        gen_op_4xx_tlbwe_hi();
5303
        break;
5304
    case 1:
5305
        gen_op_load_gpr_T0(rA(ctx->opcode));
5306
        gen_op_load_gpr_T1(rS(ctx->opcode));
5307
        gen_op_4xx_tlbwe_lo();
5308
        break;
5309
    default:
5310
        GEN_EXCP_INVAL(ctx);
5311
        break;
5312
    }
5313
#endif
5314
}
5315

    
5316
/* TLB management - PowerPC 440 implementation */
5317
/* tlbre */
5318
GEN_HANDLER(tlbre_440, 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5319
{
5320
#if defined(CONFIG_USER_ONLY)
5321
    GEN_EXCP_PRIVOPC(ctx);
5322
#else
5323
    if (unlikely(!ctx->supervisor)) {
5324
        GEN_EXCP_PRIVOPC(ctx);
5325
        return;
5326
    }
5327
    switch (rB(ctx->opcode)) {
5328
    case 0:
5329
    case 1:
5330
    case 2:
5331
        gen_op_load_gpr_T0(rA(ctx->opcode));
5332
        gen_op_440_tlbre(rB(ctx->opcode));
5333
        gen_op_store_T0_gpr(rD(ctx->opcode));
5334
        break;
5335
    default:
5336
        GEN_EXCP_INVAL(ctx);
5337
        break;
5338
    }
5339
#endif
5340
}
5341

    
5342
/* tlbsx - tlbsx. */
5343
GEN_HANDLER(tlbsx_440, 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5344
{
5345
#if defined(CONFIG_USER_ONLY)
5346
    GEN_EXCP_PRIVOPC(ctx);
5347
#else
5348
    if (unlikely(!ctx->supervisor)) {
5349
        GEN_EXCP_PRIVOPC(ctx);
5350
        return;
5351
    }
5352
    gen_addr_reg_index(ctx);
5353
    gen_op_440_tlbsx();
5354
    if (Rc(ctx->opcode))
5355
        gen_op_4xx_tlbsx_check();
5356
    gen_op_store_T0_gpr(rD(ctx->opcode));
5357
#endif
5358
}
5359

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

    
5385
/* wrtee */
5386
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_EMB_COMMON)
5387
{
5388
#if defined(CONFIG_USER_ONLY)
5389
    GEN_EXCP_PRIVOPC(ctx);
5390
#else
5391
    if (unlikely(!ctx->supervisor)) {
5392
        GEN_EXCP_PRIVOPC(ctx);
5393
        return;
5394
    }
5395
    gen_op_load_gpr_T0(rD(ctx->opcode));
5396
    gen_op_wrte();
5397
    /* Stop translation to have a chance to raise an exception
5398
     * if we just set msr_ee to 1
5399
     */
5400
    GEN_STOP(ctx);
5401
#endif
5402
}
5403

    
5404
/* wrteei */
5405
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_EMB_COMMON)
5406
{
5407
#if defined(CONFIG_USER_ONLY)
5408
    GEN_EXCP_PRIVOPC(ctx);
5409
#else
5410
    if (unlikely(!ctx->supervisor)) {
5411
        GEN_EXCP_PRIVOPC(ctx);
5412
        return;
5413
    }
5414
    gen_op_set_T0(ctx->opcode & 0x00010000);
5415
    gen_op_wrte();
5416
    /* Stop translation to have a chance to raise an exception
5417
     * if we just set msr_ee to 1
5418
     */
5419
    GEN_STOP(ctx);
5420
#endif
5421
}
5422

    
5423
/* PowerPC 440 specific instructions */
5424
/* dlmzb */
5425
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
5426
{
5427
    gen_op_load_gpr_T0(rS(ctx->opcode));
5428
    gen_op_load_gpr_T1(rB(ctx->opcode));
5429
    gen_op_440_dlmzb();
5430
    gen_op_store_T0_gpr(rA(ctx->opcode));
5431
    gen_op_store_xer_bc();
5432
    if (Rc(ctx->opcode)) {
5433
        gen_op_440_dlmzb_update_Rc();
5434
        gen_op_store_T0_crf(0);
5435
    }
5436
}
5437

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

    
5444
/* msync replaces sync on 440 */
5445
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5446
{
5447
    /* interpreted as no-op */
5448
}
5449

    
5450
/* icbt */
5451
GEN_HANDLER(icbt_440, 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5452
{
5453
    /* interpreted as no-op */
5454
    /* XXX: specification say this is treated as a load by the MMU
5455
     *      but does not generate any exception
5456
     */
5457
}
5458

    
5459
#if defined(TARGET_PPCEMB)
5460
/***                           SPE extension                               ***/
5461

    
5462
/* Register moves */
5463
GEN32(gen_op_load_gpr64_T0, gen_op_load_gpr64_T0_gpr);
5464
GEN32(gen_op_load_gpr64_T1, gen_op_load_gpr64_T1_gpr);
5465
#if 0 // unused
5466
GEN32(gen_op_load_gpr64_T2, gen_op_load_gpr64_T2_gpr);
5467
#endif
5468

    
5469
GEN32(gen_op_store_T0_gpr64, gen_op_store_T0_gpr64_gpr);
5470
GEN32(gen_op_store_T1_gpr64, gen_op_store_T1_gpr64_gpr);
5471
#if 0 // unused
5472
GEN32(gen_op_store_T2_gpr64, gen_op_store_T2_gpr64_gpr);
5473
#endif
5474

    
5475
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
5476
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
5477
{                                                                             \
5478
    if (Rc(ctx->opcode))                                                      \
5479
        gen_##name1(ctx);                                                     \
5480
    else                                                                      \
5481
        gen_##name0(ctx);                                                     \
5482
}
5483

    
5484
/* Handler for undefined SPE opcodes */
5485
static inline void gen_speundef (DisasContext *ctx)
5486
{
5487
    GEN_EXCP_INVAL(ctx);
5488
}
5489

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

    
5495
    if (rA(ctx->opcode) == 0) {
5496
        gen_set_T0(simm << sh);
5497
    } else {
5498
        gen_op_load_gpr_T0(rA(ctx->opcode));
5499
        if (likely(simm != 0))
5500
            gen_op_addi(simm << sh);
5501
    }
5502
}
5503

    
5504
#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
5505
#if defined(CONFIG_USER_ONLY)
5506
#if defined(TARGET_PPC64)
5507
/* User-mode only - 64 bits mode */
5508
#define OP_SPE_LD_TABLE(name)                                                 \
5509
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5510
    &gen_op_spe_l##name##_raw,                                                \
5511
    &gen_op_spe_l##name##_le_raw,                                             \
5512
    &gen_op_spe_l##name##_64_raw,                                             \
5513
    &gen_op_spe_l##name##_le_64_raw,                                          \
5514
};
5515
#define OP_SPE_ST_TABLE(name)                                                 \
5516
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5517
    &gen_op_spe_st##name##_raw,                                               \
5518
    &gen_op_spe_st##name##_le_raw,                                            \
5519
    &gen_op_spe_st##name##_64_raw,                                            \
5520
    &gen_op_spe_st##name##_le_64_raw,                                         \
5521
};
5522
#else /* defined(TARGET_PPC64) */
5523
/* User-mode only - 32 bits mode */
5524
#define OP_SPE_LD_TABLE(name)                                                 \
5525
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5526
    &gen_op_spe_l##name##_raw,                                                \
5527
    &gen_op_spe_l##name##_le_raw,                                             \
5528
};
5529
#define OP_SPE_ST_TABLE(name)                                                 \
5530
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5531
    &gen_op_spe_st##name##_raw,                                               \
5532
    &gen_op_spe_st##name##_le_raw,                                            \
5533
};
5534
#endif /* defined(TARGET_PPC64) */
5535
#else /* defined(CONFIG_USER_ONLY) */
5536
#if defined(TARGET_PPC64H)
5537
/* Full system with hypervisor mode */
5538
#define OP_SPE_LD_TABLE(name)                                                 \
5539
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5540
    &gen_op_spe_l##name##_user,                                               \
5541
    &gen_op_spe_l##name##_le_user,                                            \
5542
    &gen_op_spe_l##name##_64_user,                                            \
5543
    &gen_op_spe_l##name##_le_64_user,                                         \
5544
    &gen_op_spe_l##name##_kernel,                                             \
5545
    &gen_op_spe_l##name##_le_kernel,                                          \
5546
    &gen_op_spe_l##name##_64_kernel,                                          \
5547
    &gen_op_spe_l##name##_le_64_kernel,                                       \
5548
    &gen_op_spe_l##name##_hypv,                                               \
5549
    &gen_op_spe_l##name##_le_hypv,                                            \
5550
    &gen_op_spe_l##name##_64_hypv,                                            \
5551
    &gen_op_spe_l##name##_le_64_hypv,                                         \
5552
};
5553
#define OP_SPE_ST_TABLE(name)                                                 \
5554
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5555
    &gen_op_spe_st##name##_user,                                              \
5556
    &gen_op_spe_st##name##_le_user,                                           \
5557
    &gen_op_spe_st##name##_64_user,                                           \
5558
    &gen_op_spe_st##name##_le_64_user,                                        \
5559
    &gen_op_spe_st##name##_kernel,                                            \
5560
    &gen_op_spe_st##name##_le_kernel,                                         \
5561
    &gen_op_spe_st##name##_64_kernel,                                         \
5562
    &gen_op_spe_st##name##_le_64_kernel,                                      \
5563
    &gen_op_spe_st##name##_hypv,                                              \
5564
    &gen_op_spe_st##name##_le_hypv,                                           \
5565
    &gen_op_spe_st##name##_64_hypv,                                           \
5566
    &gen_op_spe_st##name##_le_64_hypv,                                        \
5567
};
5568
#elif defined(TARGET_PPC64)
5569
/* Full system - 64 bits mode */
5570
#define OP_SPE_LD_TABLE(name)                                                 \
5571
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5572
    &gen_op_spe_l##name##_user,                                               \
5573
    &gen_op_spe_l##name##_le_user,                                            \
5574
    &gen_op_spe_l##name##_64_user,                                            \
5575
    &gen_op_spe_l##name##_le_64_user,                                         \
5576
    &gen_op_spe_l##name##_kernel,                                             \
5577
    &gen_op_spe_l##name##_le_kernel,                                          \
5578
    &gen_op_spe_l##name##_64_kernel,                                          \
5579
    &gen_op_spe_l##name##_le_64_kernel,                                       \
5580
};
5581
#define OP_SPE_ST_TABLE(name)                                                 \
5582
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5583
    &gen_op_spe_st##name##_user,                                              \
5584
    &gen_op_spe_st##name##_le_user,                                           \
5585
    &gen_op_spe_st##name##_64_user,                                           \
5586
    &gen_op_spe_st##name##_le_64_user,                                        \
5587
    &gen_op_spe_st##name##_kernel,                                            \
5588
    &gen_op_spe_st##name##_le_kernel,                                         \
5589
    &gen_op_spe_st##name##_64_kernel,                                         \
5590
    &gen_op_spe_st##name##_le_64_kernel,                                      \
5591
};
5592
#else /* defined(TARGET_PPC64) */
5593
/* Full system - 32 bits mode */
5594
#define OP_SPE_LD_TABLE(name)                                                 \
5595
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5596
    &gen_op_spe_l##name##_user,                                               \
5597
    &gen_op_spe_l##name##_le_user,                                            \
5598
    &gen_op_spe_l##name##_kernel,                                             \
5599
    &gen_op_spe_l##name##_le_kernel,                                          \
5600
};
5601
#define OP_SPE_ST_TABLE(name)                                                 \
5602
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5603
    &gen_op_spe_st##name##_user,                                              \
5604
    &gen_op_spe_st##name##_le_user,                                           \
5605
    &gen_op_spe_st##name##_kernel,                                            \
5606
    &gen_op_spe_st##name##_le_kernel,                                         \
5607
};
5608
#endif /* defined(TARGET_PPC64) */
5609
#endif /* defined(CONFIG_USER_ONLY) */
5610

    
5611
#define GEN_SPE_LD(name, sh)                                                  \
5612
static inline void gen_evl##name (DisasContext *ctx)                          \
5613
{                                                                             \
5614
    if (unlikely(!ctx->spe_enabled)) {                                        \
5615
        GEN_EXCP_NO_AP(ctx);                                                  \
5616
        return;                                                               \
5617
    }                                                                         \
5618
    gen_addr_spe_imm_index(ctx, sh);                                          \
5619
    op_spe_ldst(spe_l##name);                                                 \
5620
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
5621
}
5622

    
5623
#define GEN_SPE_LDX(name)                                                     \
5624
static inline void gen_evl##name##x (DisasContext *ctx)                       \
5625
{                                                                             \
5626
    if (unlikely(!ctx->spe_enabled)) {                                        \
5627
        GEN_EXCP_NO_AP(ctx);                                                  \
5628
        return;                                                               \
5629
    }                                                                         \
5630
    gen_addr_reg_index(ctx);                                                  \
5631
    op_spe_ldst(spe_l##name);                                                 \
5632
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
5633
}
5634

    
5635
#define GEN_SPEOP_LD(name, sh)                                                \
5636
OP_SPE_LD_TABLE(name);                                                        \
5637
GEN_SPE_LD(name, sh);                                                         \
5638
GEN_SPE_LDX(name)
5639

    
5640
#define GEN_SPE_ST(name, sh)                                                  \
5641
static inline void gen_evst##name (DisasContext *ctx)                         \
5642
{                                                                             \
5643
    if (unlikely(!ctx->spe_enabled)) {                                        \
5644
        GEN_EXCP_NO_AP(ctx);                                                  \
5645
        return;                                                               \
5646
    }                                                                         \
5647
    gen_addr_spe_imm_index(ctx, sh);                                          \
5648
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
5649
    op_spe_ldst(spe_st##name);                                                \
5650
}
5651

    
5652
#define GEN_SPE_STX(name)                                                     \
5653
static inline void gen_evst##name##x (DisasContext *ctx)                      \
5654
{                                                                             \
5655
    if (unlikely(!ctx->spe_enabled)) {                                        \
5656
        GEN_EXCP_NO_AP(ctx);                                                  \
5657
        return;                                                               \
5658
    }                                                                         \
5659
    gen_addr_reg_index(ctx);                                                  \
5660
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
5661
    op_spe_ldst(spe_st##name);                                                \
5662
}
5663

    
5664
#define GEN_SPEOP_ST(name, sh)                                                \
5665
OP_SPE_ST_TABLE(name);                                                        \
5666
GEN_SPE_ST(name, sh);                                                         \
5667
GEN_SPE_STX(name)
5668

    
5669
#define GEN_SPEOP_LDST(name, sh)                                              \
5670
GEN_SPEOP_LD(name, sh);                                                       \
5671
GEN_SPEOP_ST(name, sh)
5672

    
5673
/* SPE arithmetic and logic */
5674
#define GEN_SPEOP_ARITH2(name)                                                \
5675
static inline void gen_##name (DisasContext *ctx)                             \
5676
{                                                                             \
5677
    if (unlikely(!ctx->spe_enabled)) {                                        \
5678
        GEN_EXCP_NO_AP(ctx);                                                  \
5679
        return;                                                               \
5680
    }                                                                         \
5681
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
5682
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
5683
    gen_op_##name();                                                          \
5684
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
5685
}
5686

    
5687
#define GEN_SPEOP_ARITH1(name)                                                \
5688
static inline void gen_##name (DisasContext *ctx)                             \
5689
{                                                                             \
5690
    if (unlikely(!ctx->spe_enabled)) {                                        \
5691
        GEN_EXCP_NO_AP(ctx);                                                  \
5692
        return;                                                               \
5693
    }                                                                         \
5694
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
5695
    gen_op_##name();                                                          \
5696
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
5697
}
5698

    
5699
#define GEN_SPEOP_COMP(name)                                                  \
5700
static inline void gen_##name (DisasContext *ctx)                             \
5701
{                                                                             \
5702
    if (unlikely(!ctx->spe_enabled)) {                                        \
5703
        GEN_EXCP_NO_AP(ctx);                                                  \
5704
        return;                                                               \
5705
    }                                                                         \
5706
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
5707
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
5708
    gen_op_##name();                                                          \
5709
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
5710
}
5711

    
5712
/* Logical */
5713
GEN_SPEOP_ARITH2(evand);
5714
GEN_SPEOP_ARITH2(evandc);
5715
GEN_SPEOP_ARITH2(evxor);
5716
GEN_SPEOP_ARITH2(evor);
5717
GEN_SPEOP_ARITH2(evnor);
5718
GEN_SPEOP_ARITH2(eveqv);
5719
GEN_SPEOP_ARITH2(evorc);
5720
GEN_SPEOP_ARITH2(evnand);
5721
GEN_SPEOP_ARITH2(evsrwu);
5722
GEN_SPEOP_ARITH2(evsrws);
5723
GEN_SPEOP_ARITH2(evslw);
5724
GEN_SPEOP_ARITH2(evrlw);
5725
GEN_SPEOP_ARITH2(evmergehi);
5726
GEN_SPEOP_ARITH2(evmergelo);
5727
GEN_SPEOP_ARITH2(evmergehilo);
5728
GEN_SPEOP_ARITH2(evmergelohi);
5729

    
5730
/* Arithmetic */
5731
GEN_SPEOP_ARITH2(evaddw);
5732
GEN_SPEOP_ARITH2(evsubfw);
5733
GEN_SPEOP_ARITH1(evabs);
5734
GEN_SPEOP_ARITH1(evneg);
5735
GEN_SPEOP_ARITH1(evextsb);
5736
GEN_SPEOP_ARITH1(evextsh);
5737
GEN_SPEOP_ARITH1(evrndw);
5738
GEN_SPEOP_ARITH1(evcntlzw);
5739
GEN_SPEOP_ARITH1(evcntlsw);
5740
static inline void gen_brinc (DisasContext *ctx)
5741
{
5742
    /* Note: brinc is usable even if SPE is disabled */
5743
    gen_op_load_gpr64_T0(rA(ctx->opcode));
5744
    gen_op_load_gpr64_T1(rB(ctx->opcode));
5745
    gen_op_brinc();
5746
    gen_op_store_T0_gpr64(rD(ctx->opcode));
5747
}
5748

    
5749
#define GEN_SPEOP_ARITH_IMM2(name)                                            \
5750
static inline void gen_##name##i (DisasContext *ctx)                          \
5751
{                                                                             \
5752
    if (unlikely(!ctx->spe_enabled)) {                                        \
5753
        GEN_EXCP_NO_AP(ctx);                                                  \
5754
        return;                                                               \
5755
    }                                                                         \
5756
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
5757
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
5758
    gen_op_##name();                                                          \
5759
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
5760
}
5761

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

    
5775
GEN_SPEOP_ARITH_IMM2(evaddw);
5776
#define gen_evaddiw gen_evaddwi
5777
GEN_SPEOP_ARITH_IMM2(evsubfw);
5778
#define gen_evsubifw gen_evsubfwi
5779
GEN_SPEOP_LOGIC_IMM2(evslw);
5780
GEN_SPEOP_LOGIC_IMM2(evsrwu);
5781
#define gen_evsrwis gen_evsrwsi
5782
GEN_SPEOP_LOGIC_IMM2(evsrws);
5783
#define gen_evsrwiu gen_evsrwui
5784
GEN_SPEOP_LOGIC_IMM2(evrlw);
5785

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

    
5790
    gen_op_splatwi_T0_64(imm);
5791
    gen_op_store_T0_gpr64(rD(ctx->opcode));
5792
}
5793

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

    
5798
    gen_op_splatwi_T0_64(imm);
5799
    gen_op_store_T0_gpr64(rD(ctx->opcode));
5800
}
5801

    
5802
/* Comparison */
5803
GEN_SPEOP_COMP(evcmpgtu);
5804
GEN_SPEOP_COMP(evcmpgts);
5805
GEN_SPEOP_COMP(evcmpltu);
5806
GEN_SPEOP_COMP(evcmplts);
5807
GEN_SPEOP_COMP(evcmpeq);
5808

    
5809
GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
5810
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
5811
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
5812
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
5813
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
5814
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
5815
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
5816
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
5817
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
5818
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
5819
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
5820
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
5821
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
5822
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
5823
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
5824
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
5825
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
5826
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
5827
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
5828
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
5829
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
5830
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
5831
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
5832
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
5833
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////
5834

    
5835
static inline void gen_evsel (DisasContext *ctx)
5836
{
5837
    if (unlikely(!ctx->spe_enabled)) {
5838
        GEN_EXCP_NO_AP(ctx);
5839
        return;
5840
    }
5841
    gen_op_load_crf_T0(ctx->opcode & 0x7);
5842
    gen_op_load_gpr64_T0(rA(ctx->opcode));
5843
    gen_op_load_gpr64_T1(rB(ctx->opcode));
5844
    gen_op_evsel();
5845
    gen_op_store_T0_gpr64(rD(ctx->opcode));
5846
}
5847

    
5848
GEN_HANDLER(evsel0, 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
5849
{
5850
    gen_evsel(ctx);
5851
}
5852
GEN_HANDLER(evsel1, 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
5853
{
5854
    gen_evsel(ctx);
5855
}
5856
GEN_HANDLER(evsel2, 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
5857
{
5858
    gen_evsel(ctx);
5859
}
5860
GEN_HANDLER(evsel3, 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
5861
{
5862
    gen_evsel(ctx);
5863
}
5864

    
5865
/* Load and stores */
5866
#if defined(TARGET_PPC64)
5867
/* In that case, we already have 64 bits load & stores
5868
 * so, spe_ldd is equivalent to ld and spe_std is equivalent to std
5869
 */
5870
#if defined(CONFIG_USER_ONLY)
5871
#define gen_op_spe_ldd_raw gen_op_ld_raw
5872
#define gen_op_spe_ldd_64_raw gen_op_ld_64_raw
5873
#define gen_op_spe_ldd_le_raw gen_op_ld_le_raw
5874
#define gen_op_spe_ldd_le_64_raw gen_op_ld_le_64_raw
5875
#define gen_op_spe_stdd_raw gen_op_ld_raw
5876
#define gen_op_spe_stdd_64_raw gen_op_std_64_raw
5877
#define gen_op_spe_stdd_le_raw gen_op_std_le_raw
5878
#define gen_op_spe_stdd_le_64_raw gen_op_std_le_64_raw
5879
#else /* defined(CONFIG_USER_ONLY) */
5880
#define gen_op_spe_ldd_kernel gen_op_ld_kernel
5881
#define gen_op_spe_ldd_64_kernel gen_op_ld_64_kernel
5882
#define gen_op_spe_ldd_le_kernel gen_op_ld_kernel
5883
#define gen_op_spe_ldd_le_64_kernel gen_op_ld_64_kernel
5884
#define gen_op_spe_ldd_user gen_op_ld_user
5885
#define gen_op_spe_ldd_64_user gen_op_ld_64_user
5886
#define gen_op_spe_ldd_le_user gen_op_ld_le_user
5887
#define gen_op_spe_ldd_le_64_user gen_op_ld_le_64_user
5888
#define gen_op_spe_stdd_kernel gen_op_std_kernel
5889
#define gen_op_spe_stdd_64_kernel gen_op_std_64_kernel
5890
#define gen_op_spe_stdd_le_kernel gen_op_std_kernel
5891
#define gen_op_spe_stdd_le_64_kernel gen_op_std_64_kernel
5892
#define gen_op_spe_stdd_user gen_op_std_user
5893
#define gen_op_spe_stdd_64_user gen_op_std_64_user
5894
#define gen_op_spe_stdd_le_user gen_op_std_le_user
5895
#define gen_op_spe_stdd_le_64_user gen_op_std_le_64_user
5896
#endif /* defined(CONFIG_USER_ONLY) */
5897
#endif /* defined(TARGET_PPC64) */
5898
GEN_SPEOP_LDST(dd, 3);
5899
GEN_SPEOP_LDST(dw, 3);
5900
GEN_SPEOP_LDST(dh, 3);
5901
GEN_SPEOP_LDST(whe, 2);
5902
GEN_SPEOP_LD(whou, 2);
5903
GEN_SPEOP_LD(whos, 2);
5904
GEN_SPEOP_ST(who, 2);
5905

    
5906
#if defined(TARGET_PPC64)
5907
/* In that case, spe_stwwo is equivalent to stw */
5908
#if defined(CONFIG_USER_ONLY)
5909
#define gen_op_spe_stwwo_raw gen_op_stw_raw
5910
#define gen_op_spe_stwwo_le_raw gen_op_stw_le_raw
5911
#define gen_op_spe_stwwo_64_raw gen_op_stw_64_raw
5912
#define gen_op_spe_stwwo_le_64_raw gen_op_stw_le_64_raw
5913
#else
5914
#define gen_op_spe_stwwo_user gen_op_stw_user
5915
#define gen_op_spe_stwwo_le_user gen_op_stw_le_user
5916
#define gen_op_spe_stwwo_64_user gen_op_stw_64_user
5917
#define gen_op_spe_stwwo_le_64_user gen_op_stw_le_64_user
5918
#define gen_op_spe_stwwo_kernel gen_op_stw_kernel
5919
#define gen_op_spe_stwwo_le_kernel gen_op_stw_le_kernel
5920
#define gen_op_spe_stwwo_64_kernel gen_op_stw_64_kernel
5921
#define gen_op_spe_stwwo_le_64_kernel gen_op_stw_le_64_kernel
5922
#endif
5923
#endif
5924
#define _GEN_OP_SPE_STWWE(suffix)                                             \
5925
static inline void gen_op_spe_stwwe_##suffix (void)                           \
5926
{                                                                             \
5927
    gen_op_srli32_T1_64();                                                    \
5928
    gen_op_spe_stwwo_##suffix();                                              \
5929
}
5930
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
5931
static inline void gen_op_spe_stwwe_le_##suffix (void)                        \
5932
{                                                                             \
5933
    gen_op_srli32_T1_64();                                                    \
5934
    gen_op_spe_stwwo_le_##suffix();                                           \
5935
}
5936
#if defined(TARGET_PPC64)
5937
#define GEN_OP_SPE_STWWE(suffix)                                              \
5938
_GEN_OP_SPE_STWWE(suffix);                                                    \
5939
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
5940
static inline void gen_op_spe_stwwe_64_##suffix (void)                        \
5941
{                                                                             \
5942
    gen_op_srli32_T1_64();                                                    \
5943
    gen_op_spe_stwwo_64_##suffix();                                           \
5944
}                                                                             \
5945
static inline void gen_op_spe_stwwe_le_64_##suffix (void)                     \
5946
{                                                                             \
5947
    gen_op_srli32_T1_64();                                                    \
5948
    gen_op_spe_stwwo_le_64_##suffix();                                        \
5949
}
5950
#else
5951
#define GEN_OP_SPE_STWWE(suffix)                                              \
5952
_GEN_OP_SPE_STWWE(suffix);                                                    \
5953
_GEN_OP_SPE_STWWE_LE(suffix)
5954
#endif
5955
#if defined(CONFIG_USER_ONLY)
5956
GEN_OP_SPE_STWWE(raw);
5957
#else /* defined(CONFIG_USER_ONLY) */
5958
GEN_OP_SPE_STWWE(kernel);
5959
GEN_OP_SPE_STWWE(user);
5960
#endif /* defined(CONFIG_USER_ONLY) */
5961
GEN_SPEOP_ST(wwe, 2);
5962
GEN_SPEOP_ST(wwo, 2);
5963

    
5964
#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
5965
static inline void gen_op_spe_l##name##_##suffix (void)                       \
5966
{                                                                             \
5967
    gen_op_##op##_##suffix();                                                 \
5968
    gen_op_splatw_T1_64();                                                    \
5969
}
5970

    
5971
#define GEN_OP_SPE_LHE(suffix)                                                \
5972
static inline void gen_op_spe_lhe_##suffix (void)                             \
5973
{                                                                             \
5974
    gen_op_spe_lh_##suffix();                                                 \
5975
    gen_op_sli16_T1_64();                                                     \
5976
}
5977

    
5978
#define GEN_OP_SPE_LHX(suffix)                                                \
5979
static inline void gen_op_spe_lhx_##suffix (void)                             \
5980
{                                                                             \
5981
    gen_op_spe_lh_##suffix();                                                 \
5982
    gen_op_extsh_T1_64();                                                     \
5983
}
5984

    
5985
#if defined(CONFIG_USER_ONLY)
5986
GEN_OP_SPE_LHE(raw);
5987
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
5988
GEN_OP_SPE_LHE(le_raw);
5989
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
5990
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
5991
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
5992
GEN_OP_SPE_LHX(raw);
5993
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
5994
GEN_OP_SPE_LHX(le_raw);
5995
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
5996
#if defined(TARGET_PPC64)
5997
GEN_OP_SPE_LHE(64_raw);
5998
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
5999
GEN_OP_SPE_LHE(le_64_raw);
6000
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
6001
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
6002
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
6003
GEN_OP_SPE_LHX(64_raw);
6004
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
6005
GEN_OP_SPE_LHX(le_64_raw);
6006
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
6007
#endif
6008
#else
6009
GEN_OP_SPE_LHE(kernel);
6010
GEN_OP_SPE_LHE(user);
6011
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
6012
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
6013
GEN_OP_SPE_LHE(le_kernel);
6014
GEN_OP_SPE_LHE(le_user);
6015
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
6016
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
6017
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
6018
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
6019
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
6020
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
6021
GEN_OP_SPE_LHX(kernel);
6022
GEN_OP_SPE_LHX(user);
6023
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
6024
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
6025
GEN_OP_SPE_LHX(le_kernel);
6026
GEN_OP_SPE_LHX(le_user);
6027
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
6028
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
6029
#if defined(TARGET_PPC64)
6030
GEN_OP_SPE_LHE(64_kernel);
6031
GEN_OP_SPE_LHE(64_user);
6032
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
6033
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
6034
GEN_OP_SPE_LHE(le_64_kernel);
6035
GEN_OP_SPE_LHE(le_64_user);
6036
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
6037
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
6038
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
6039
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
6040
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
6041
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
6042
GEN_OP_SPE_LHX(64_kernel);
6043
GEN_OP_SPE_LHX(64_user);
6044
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
6045
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
6046
GEN_OP_SPE_LHX(le_64_kernel);
6047
GEN_OP_SPE_LHX(le_64_user);
6048
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
6049
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
6050
#endif
6051
#endif
6052
GEN_SPEOP_LD(hhesplat, 1);
6053
GEN_SPEOP_LD(hhousplat, 1);
6054
GEN_SPEOP_LD(hhossplat, 1);
6055
GEN_SPEOP_LD(wwsplat, 2);
6056
GEN_SPEOP_LD(whsplat, 2);
6057

    
6058
GEN_SPE(evlddx,         evldd,         0x00, 0x0C, 0x00000000, PPC_SPE); //
6059
GEN_SPE(evldwx,         evldw,         0x01, 0x0C, 0x00000000, PPC_SPE); //
6060
GEN_SPE(evldhx,         evldh,         0x02, 0x0C, 0x00000000, PPC_SPE); //
6061
GEN_SPE(evlhhesplatx,   evlhhesplat,   0x04, 0x0C, 0x00000000, PPC_SPE); //
6062
GEN_SPE(evlhhousplatx,  evlhhousplat,  0x06, 0x0C, 0x00000000, PPC_SPE); //
6063
GEN_SPE(evlhhossplatx,  evlhhossplat,  0x07, 0x0C, 0x00000000, PPC_SPE); //
6064
GEN_SPE(evlwhex,        evlwhe,        0x08, 0x0C, 0x00000000, PPC_SPE); //
6065
GEN_SPE(evlwhoux,       evlwhou,       0x0A, 0x0C, 0x00000000, PPC_SPE); //
6066
GEN_SPE(evlwhosx,       evlwhos,       0x0B, 0x0C, 0x00000000, PPC_SPE); //
6067
GEN_SPE(evlwwsplatx,    evlwwsplat,    0x0C, 0x0C, 0x00000000, PPC_SPE); //
6068
GEN_SPE(evlwhsplatx,    evlwhsplat,    0x0E, 0x0C, 0x00000000, PPC_SPE); //
6069
GEN_SPE(evstddx,        evstdd,        0x10, 0x0C, 0x00000000, PPC_SPE); //
6070
GEN_SPE(evstdwx,        evstdw,        0x11, 0x0C, 0x00000000, PPC_SPE); //
6071
GEN_SPE(evstdhx,        evstdh,        0x12, 0x0C, 0x00000000, PPC_SPE); //
6072
GEN_SPE(evstwhex,       evstwhe,       0x18, 0x0C, 0x00000000, PPC_SPE); //
6073
GEN_SPE(evstwhox,       evstwho,       0x1A, 0x0C, 0x00000000, PPC_SPE); //
6074
GEN_SPE(evstwwex,       evstwwe,       0x1C, 0x0C, 0x00000000, PPC_SPE); //
6075
GEN_SPE(evstwwox,       evstwwo,       0x1E, 0x0C, 0x00000000, PPC_SPE); //
6076

    
6077
/* Multiply and add - TODO */
6078
#if 0
6079
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
6080
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
6081
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
6082
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
6083
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
6084
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
6085
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
6086
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
6087
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
6088
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
6089
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
6090
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);
6091

6092
GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
6093
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
6094
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
6095
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
6096
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
6097
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
6098
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
6099
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
6100
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
6101
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
6102
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
6103
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
6104
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
6105
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);
6106

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

6114
GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
6115
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
6116
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
6117
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
6118
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
6119
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
6120
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
6121
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
6122
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
6123
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
6124
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
6125
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);
6126

6127
GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
6128
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
6129
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
6130
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
6131
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);
6132

6133
GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
6134
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
6135
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
6136
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
6137
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
6138
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
6139
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
6140
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
6141
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
6142
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
6143
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
6144
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);
6145

6146
GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
6147
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
6148
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
6149
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
6150
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
6151
#endif
6152

    
6153
/***                      SPE floating-point extension                     ***/
6154
#define GEN_SPEFPUOP_CONV(name)                                               \
6155
static inline void gen_##name (DisasContext *ctx)                             \
6156
{                                                                             \
6157
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
6158
    gen_op_##name();                                                          \
6159
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
6160
}
6161

    
6162
/* Single precision floating-point vectors operations */
6163
/* Arithmetic */
6164
GEN_SPEOP_ARITH2(evfsadd);
6165
GEN_SPEOP_ARITH2(evfssub);
6166
GEN_SPEOP_ARITH2(evfsmul);
6167
GEN_SPEOP_ARITH2(evfsdiv);
6168
GEN_SPEOP_ARITH1(evfsabs);
6169
GEN_SPEOP_ARITH1(evfsnabs);
6170
GEN_SPEOP_ARITH1(evfsneg);
6171
/* Conversion */
6172
GEN_SPEFPUOP_CONV(evfscfui);
6173
GEN_SPEFPUOP_CONV(evfscfsi);
6174
GEN_SPEFPUOP_CONV(evfscfuf);
6175
GEN_SPEFPUOP_CONV(evfscfsf);
6176
GEN_SPEFPUOP_CONV(evfsctui);
6177
GEN_SPEFPUOP_CONV(evfsctsi);
6178
GEN_SPEFPUOP_CONV(evfsctuf);
6179
GEN_SPEFPUOP_CONV(evfsctsf);
6180
GEN_SPEFPUOP_CONV(evfsctuiz);
6181
GEN_SPEFPUOP_CONV(evfsctsiz);
6182
/* Comparison */
6183
GEN_SPEOP_COMP(evfscmpgt);
6184
GEN_SPEOP_COMP(evfscmplt);
6185
GEN_SPEOP_COMP(evfscmpeq);
6186
GEN_SPEOP_COMP(evfststgt);
6187
GEN_SPEOP_COMP(evfststlt);
6188
GEN_SPEOP_COMP(evfststeq);
6189

    
6190
/* Opcodes definitions */
6191
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
6192
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
6193
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
6194
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
6195
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
6196
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
6197
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
6198
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
6199
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
6200
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
6201
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
6202
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
6203
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
6204
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
6205

    
6206
/* Single precision floating-point operations */
6207
/* Arithmetic */
6208
GEN_SPEOP_ARITH2(efsadd);
6209
GEN_SPEOP_ARITH2(efssub);
6210
GEN_SPEOP_ARITH2(efsmul);
6211
GEN_SPEOP_ARITH2(efsdiv);
6212
GEN_SPEOP_ARITH1(efsabs);
6213
GEN_SPEOP_ARITH1(efsnabs);
6214
GEN_SPEOP_ARITH1(efsneg);
6215
/* Conversion */
6216
GEN_SPEFPUOP_CONV(efscfui);
6217
GEN_SPEFPUOP_CONV(efscfsi);
6218
GEN_SPEFPUOP_CONV(efscfuf);
6219
GEN_SPEFPUOP_CONV(efscfsf);
6220
GEN_SPEFPUOP_CONV(efsctui);
6221
GEN_SPEFPUOP_CONV(efsctsi);
6222
GEN_SPEFPUOP_CONV(efsctuf);
6223
GEN_SPEFPUOP_CONV(efsctsf);
6224
GEN_SPEFPUOP_CONV(efsctuiz);
6225
GEN_SPEFPUOP_CONV(efsctsiz);
6226
GEN_SPEFPUOP_CONV(efscfd);
6227
/* Comparison */
6228
GEN_SPEOP_COMP(efscmpgt);
6229
GEN_SPEOP_COMP(efscmplt);
6230
GEN_SPEOP_COMP(efscmpeq);
6231
GEN_SPEOP_COMP(efststgt);
6232
GEN_SPEOP_COMP(efststlt);
6233
GEN_SPEOP_COMP(efststeq);
6234

    
6235
/* Opcodes definitions */
6236
GEN_SPE(efsadd,         efssub,        0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
6237
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
6238
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
6239
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
6240
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
6241
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
6242
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
6243
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
6244
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
6245
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
6246
GEN_SPE(efsctuiz,       efsctsiz,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
6247
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
6248
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
6249

    
6250
/* Double precision floating-point operations */
6251
/* Arithmetic */
6252
GEN_SPEOP_ARITH2(efdadd);
6253
GEN_SPEOP_ARITH2(efdsub);
6254
GEN_SPEOP_ARITH2(efdmul);
6255
GEN_SPEOP_ARITH2(efddiv);
6256
GEN_SPEOP_ARITH1(efdabs);
6257
GEN_SPEOP_ARITH1(efdnabs);
6258
GEN_SPEOP_ARITH1(efdneg);
6259
/* Conversion */
6260

    
6261
GEN_SPEFPUOP_CONV(efdcfui);
6262
GEN_SPEFPUOP_CONV(efdcfsi);
6263
GEN_SPEFPUOP_CONV(efdcfuf);
6264
GEN_SPEFPUOP_CONV(efdcfsf);
6265
GEN_SPEFPUOP_CONV(efdctui);
6266
GEN_SPEFPUOP_CONV(efdctsi);
6267
GEN_SPEFPUOP_CONV(efdctuf);
6268
GEN_SPEFPUOP_CONV(efdctsf);
6269
GEN_SPEFPUOP_CONV(efdctuiz);
6270
GEN_SPEFPUOP_CONV(efdctsiz);
6271
GEN_SPEFPUOP_CONV(efdcfs);
6272
GEN_SPEFPUOP_CONV(efdcfuid);
6273
GEN_SPEFPUOP_CONV(efdcfsid);
6274
GEN_SPEFPUOP_CONV(efdctuidz);
6275
GEN_SPEFPUOP_CONV(efdctsidz);
6276
/* Comparison */
6277
GEN_SPEOP_COMP(efdcmpgt);
6278
GEN_SPEOP_COMP(efdcmplt);
6279
GEN_SPEOP_COMP(efdcmpeq);
6280
GEN_SPEOP_COMP(efdtstgt);
6281
GEN_SPEOP_COMP(efdtstlt);
6282
GEN_SPEOP_COMP(efdtsteq);
6283

    
6284
/* Opcodes definitions */
6285
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
6286
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
6287
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
6288
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
6289
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
6290
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
6291
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
6292
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
6293
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
6294
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
6295
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
6296
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
6297
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
6298
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
6299
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
6300
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
6301
#endif
6302

    
6303
/* End opcode list */
6304
GEN_OPCODE_MARK(end);
6305

    
6306
#include "translate_init.c"
6307

    
6308
/*****************************************************************************/
6309
/* Misc PowerPC helpers */
6310
static inline uint32_t load_xer (CPUState *env)
6311
{
6312
    return (xer_so << XER_SO) |
6313
        (xer_ov << XER_OV) |
6314
        (xer_ca << XER_CA) |
6315
        (xer_bc << XER_BC) |
6316
        (xer_cmp << XER_CMP);
6317
}
6318

    
6319
void cpu_dump_state (CPUState *env, FILE *f,
6320
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
6321
                     int flags)
6322
{
6323
#if defined(TARGET_PPC64) || 1
6324
#define FILL ""
6325
#define RGPL  4
6326
#define RFPL  4
6327
#else
6328
#define FILL "        "
6329
#define RGPL  8
6330
#define RFPL  4
6331
#endif
6332

    
6333
    int i;
6334

    
6335
    cpu_fprintf(f, "NIP " ADDRX " LR " ADDRX " CTR " ADDRX "\n",
6336
                env->nip, env->lr, env->ctr);
6337
    cpu_fprintf(f, "MSR " REGX FILL " XER %08x      "
6338
#if !defined(NO_TIMER_DUMP)
6339
                "TB %08x %08x "
6340
#if !defined(CONFIG_USER_ONLY)
6341
                "DECR %08x"
6342
#endif
6343
#endif
6344
                "\n",
6345
                do_load_msr(env), load_xer(env)
6346
#if !defined(NO_TIMER_DUMP)
6347
                , cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6348
#if !defined(CONFIG_USER_ONLY)
6349
                , cpu_ppc_load_decr(env)
6350
#endif
6351
#endif
6352
                );
6353
    for (i = 0; i < 32; i++) {
6354
        if ((i & (RGPL - 1)) == 0)
6355
            cpu_fprintf(f, "GPR%02d", i);
6356
        cpu_fprintf(f, " " REGX, (target_ulong)env->gpr[i]);
6357
        if ((i & (RGPL - 1)) == (RGPL - 1))
6358
            cpu_fprintf(f, "\n");
6359
    }
6360
    cpu_fprintf(f, "CR ");
6361
    for (i = 0; i < 8; i++)
6362
        cpu_fprintf(f, "%01x", env->crf[i]);
6363
    cpu_fprintf(f, "  [");
6364
    for (i = 0; i < 8; i++) {
6365
        char a = '-';
6366
        if (env->crf[i] & 0x08)
6367
            a = 'L';
6368
        else if (env->crf[i] & 0x04)
6369
            a = 'G';
6370
        else if (env->crf[i] & 0x02)
6371
            a = 'E';
6372
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6373
    }
6374
    cpu_fprintf(f, " ]             " FILL "RES " REGX "\n", env->reserve);
6375
    for (i = 0; i < 32; i++) {
6376
        if ((i & (RFPL - 1)) == 0)
6377
            cpu_fprintf(f, "FPR%02d", i);
6378
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6379
        if ((i & (RFPL - 1)) == (RFPL - 1))
6380
            cpu_fprintf(f, "\n");
6381
    }
6382
    cpu_fprintf(f, "SRR0 " REGX " SRR1 " REGX "         " FILL FILL FILL
6383
                "SDR1 " REGX "\n",
6384
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
6385

    
6386
#undef RGPL
6387
#undef RFPL
6388
#undef FILL
6389
}
6390

    
6391
void cpu_dump_statistics (CPUState *env, FILE*f,
6392
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
6393
                          int flags)
6394
{
6395
#if defined(DO_PPC_STATISTICS)
6396
    opc_handler_t **t1, **t2, **t3, *handler;
6397
    int op1, op2, op3;
6398

    
6399
    t1 = env->opcodes;
6400
    for (op1 = 0; op1 < 64; op1++) {
6401
        handler = t1[op1];
6402
        if (is_indirect_opcode(handler)) {
6403
            t2 = ind_table(handler);
6404
            for (op2 = 0; op2 < 32; op2++) {
6405
                handler = t2[op2];
6406
                if (is_indirect_opcode(handler)) {
6407
                    t3 = ind_table(handler);
6408
                    for (op3 = 0; op3 < 32; op3++) {
6409
                        handler = t3[op3];
6410
                        if (handler->count == 0)
6411
                            continue;
6412
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
6413
                                    "%016llx %lld\n",
6414
                                    op1, op2, op3, op1, (op3 << 5) | op2,
6415
                                    handler->oname,
6416
                                    handler->count, handler->count);
6417
                    }
6418
                } else {
6419
                    if (handler->count == 0)
6420
                        continue;
6421
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
6422
                                "%016llx %lld\n",
6423
                                op1, op2, op1, op2, handler->oname,
6424
                                handler->count, handler->count);
6425
                }
6426
            }
6427
        } else {
6428
            if (handler->count == 0)
6429
                continue;
6430
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
6431
                        op1, op1, handler->oname,
6432
                        handler->count, handler->count);
6433
        }
6434
    }
6435
#endif
6436
}
6437

    
6438
/*****************************************************************************/
6439
static inline int gen_intermediate_code_internal (CPUState *env,
6440
                                                  TranslationBlock *tb,
6441
                                                  int search_pc)
6442
{
6443
    DisasContext ctx, *ctxp = &ctx;
6444
    opc_handler_t **table, *handler;
6445
    target_ulong pc_start;
6446
    uint16_t *gen_opc_end;
6447
    int supervisor;
6448
    int j, lj = -1;
6449

    
6450
    pc_start = tb->pc;
6451
    gen_opc_ptr = gen_opc_buf;
6452
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
6453
    gen_opparam_ptr = gen_opparam_buf;
6454
    nb_gen_labels = 0;
6455
    ctx.nip = pc_start;
6456
    ctx.tb = tb;
6457
    ctx.exception = POWERPC_EXCP_NONE;
6458
    ctx.spr_cb = env->spr_cb;
6459
#if defined(CONFIG_USER_ONLY)
6460
    supervisor = 0;
6461
#else
6462
#if defined(TARGET_PPC64H)
6463
    if (msr_pr == 0 && msr_hv == 1)
6464
        supervisor = 2;
6465
    else
6466
#endif
6467
        supervisor = 1 - msr_pr;
6468
    ctx.supervisor = supervisor;
6469
#endif
6470
#if defined(TARGET_PPC64)
6471
    ctx.sf_mode = msr_sf;
6472
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | msr_le;
6473
#else
6474
    ctx.mem_idx = (supervisor << 1) | msr_le;
6475
#endif
6476
    ctx.dcache_line_size = env->dcache_line_size;
6477
    ctx.fpu_enabled = msr_fp;
6478
#if defined(TARGET_PPCEMB)
6479
    ctx.spe_enabled = msr_spe;
6480
#endif
6481
    ctx.singlestep_enabled = env->singlestep_enabled;
6482
#if defined (DO_SINGLE_STEP) && 0
6483
    /* Single step trace mode */
6484
    msr_se = 1;
6485
#endif
6486
    /* Set env in case of segfault during code fetch */
6487
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6488
        if (unlikely(env->nb_breakpoints > 0)) {
6489
            for (j = 0; j < env->nb_breakpoints; j++) {
6490
                if (env->breakpoints[j] == ctx.nip) {
6491
                    gen_update_nip(&ctx, ctx.nip);
6492
                    gen_op_debug();
6493
                    break;
6494
                }
6495
            }
6496
        }
6497
        if (unlikely(search_pc)) {
6498
            j = gen_opc_ptr - gen_opc_buf;
6499
            if (lj < j) {
6500
                lj++;
6501
                while (lj < j)
6502
                    gen_opc_instr_start[lj++] = 0;
6503
                gen_opc_pc[lj] = ctx.nip;
6504
                gen_opc_instr_start[lj] = 1;
6505
            }
6506
        }
6507
#if defined PPC_DEBUG_DISAS
6508
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6509
            fprintf(logfile, "----------------\n");
6510
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6511
                    ctx.nip, 1 - msr_pr, msr_ir);
6512
        }
6513
#endif
6514
        ctx.opcode = ldl_code(ctx.nip);
6515
        if (msr_le) {
6516
            ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
6517
                ((ctx.opcode & 0x00FF0000) >> 8) |
6518
                ((ctx.opcode & 0x0000FF00) << 8) |
6519
                ((ctx.opcode & 0x000000FF) << 24);
6520
        }
6521
#if defined PPC_DEBUG_DISAS
6522
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6523
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6524
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6525
                    opc3(ctx.opcode), msr_le ? "little" : "big");
6526
        }
6527
#endif
6528
        ctx.nip += 4;
6529
        table = env->opcodes;
6530
        handler = table[opc1(ctx.opcode)];
6531
        if (is_indirect_opcode(handler)) {
6532
            table = ind_table(handler);
6533
            handler = table[opc2(ctx.opcode)];
6534
            if (is_indirect_opcode(handler)) {
6535
                table = ind_table(handler);
6536
                handler = table[opc3(ctx.opcode)];
6537
            }
6538
        }
6539
        /* Is opcode *REALLY* valid ? */
6540
        if (unlikely(handler->handler == &gen_invalid)) {
6541
            if (loglevel != 0) {
6542
                fprintf(logfile, "invalid/unsupported opcode: "
6543
                        "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
6544
                        opc1(ctx.opcode), opc2(ctx.opcode),
6545
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
6546
            } else {
6547
                printf("invalid/unsupported opcode: "
6548
                       "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
6549
                       opc1(ctx.opcode), opc2(ctx.opcode),
6550
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
6551
            }
6552
        } else {
6553
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
6554
                if (loglevel != 0) {
6555
                    fprintf(logfile, "invalid bits: %08x for opcode: "
6556
                            "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
6557
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
6558
                            opc2(ctx.opcode), opc3(ctx.opcode),
6559
                            ctx.opcode, ctx.nip - 4);
6560
                } else {
6561
                    printf("invalid bits: %08x for opcode: "
6562
                           "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
6563
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
6564
                           opc2(ctx.opcode), opc3(ctx.opcode),
6565
                           ctx.opcode, ctx.nip - 4);
6566
                }
6567
                GEN_EXCP_INVAL(ctxp);
6568
                break;
6569
            }
6570
        }
6571
        (*(handler->handler))(&ctx);
6572
#if defined(DO_PPC_STATISTICS)
6573
        handler->count++;
6574
#endif
6575
        /* Check trace mode exceptions */
6576
#if 0 // XXX: buggy on embedded PowerPC
6577
        if (unlikely((msr_be && ctx.exception == POWERPC_EXCP_BRANCH) ||
6578
                     /* Check in single step trace mode
6579
                      * we need to stop except if:
6580
                      * - rfi, trap or syscall
6581
                      * - first instruction of an exception handler
6582
                      */
6583
                     (msr_se && (ctx.nip < 0x100 ||
6584
                                 ctx.nip > 0xF00 ||
6585
                                 (ctx.nip & 0xFC) != 0x04) &&
6586
#if defined(CONFIG_USER_ONLY)
6587
                      ctx.exception != POWERPC_EXCP_SYSCALL_USER &&
6588
#else
6589
                      ctx.exception != POWERPC_EXCP_SYSCALL &&
6590
#endif
6591
                      ctx.exception != POWERPC_EXCP_TRAP))) {
6592
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
6593
        }
6594
#endif
6595
        /* if we reach a page boundary or are single stepping, stop
6596
         * generation
6597
         */
6598
        if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
6599
                     (env->singlestep_enabled))) {
6600
            break;
6601
        }
6602
#if defined (DO_SINGLE_STEP)
6603
        break;
6604
#endif
6605
    }
6606
    if (ctx.exception == POWERPC_EXCP_NONE) {
6607
        gen_goto_tb(&ctx, 0, ctx.nip);
6608
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
6609
        gen_op_reset_T0();
6610
        /* Generate the return instruction */
6611
        gen_op_exit_tb();
6612
    }
6613
    *gen_opc_ptr = INDEX_op_end;
6614
    if (unlikely(search_pc)) {
6615
        j = gen_opc_ptr - gen_opc_buf;
6616
        lj++;
6617
        while (lj <= j)
6618
            gen_opc_instr_start[lj++] = 0;
6619
    } else {
6620
        tb->size = ctx.nip - pc_start;
6621
    }
6622
#if defined(DEBUG_DISAS)
6623
    if (loglevel & CPU_LOG_TB_CPU) {
6624
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
6625
        cpu_dump_state(env, logfile, fprintf, 0);
6626
    }
6627
    if (loglevel & CPU_LOG_TB_IN_ASM) {
6628
        int flags;
6629
        flags = env->bfd_mach;
6630
        flags |= msr_le << 16;
6631
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
6632
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
6633
        fprintf(logfile, "\n");
6634
    }
6635
    if (loglevel & CPU_LOG_TB_OP) {
6636
        fprintf(logfile, "OP:\n");
6637
        dump_ops(gen_opc_buf, gen_opparam_buf);
6638
        fprintf(logfile, "\n");
6639
    }
6640
#endif
6641
    return 0;
6642
}
6643

    
6644
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
6645
{
6646
    return gen_intermediate_code_internal(env, tb, 0);
6647
}
6648

    
6649
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
6650
{
6651
    return gen_intermediate_code_internal(env, tb, 1);
6652
}