Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ 3cd7d1dd

History | View | Annotate | Download (243.9 kB)

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

    
26
#include "cpu.h"
27
#include "exec-all.h"
28
#include "disas.h"
29

    
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
//#define OPTIMIZE_FPRF_UPDATE
36

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

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

    
52
static uint16_t *gen_opc_ptr;
53
static uint32_t *gen_opparam_ptr;
54
#if defined(OPTIMIZE_FPRF_UPDATE)
55
static uint16_t *gen_fprf_buf[OPC_BUF_SIZE];
56
static uint16_t **gen_fprf_ptr;
57
#endif
58

    
59
#include "gen-op.h"
60

    
61
static always_inline void gen_set_T0 (target_ulong val)
62
{
63
#if defined(TARGET_PPC64)
64
    if (val >> 32)
65
        gen_op_set_T0_64(val >> 32, val);
66
    else
67
#endif
68
        gen_op_set_T0(val);
69
}
70

    
71
static always_inline void gen_set_T1 (target_ulong val)
72
{
73
#if defined(TARGET_PPC64)
74
    if (val >> 32)
75
        gen_op_set_T1_64(val >> 32, val);
76
    else
77
#endif
78
        gen_op_set_T1(val);
79
}
80

    
81
#define GEN8(func, NAME)                                                      \
82
static GenOpFunc *NAME ## _table [8] = {                                      \
83
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
84
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
85
};                                                                            \
86
static always_inline void func (int n)                                        \
87
{                                                                             \
88
    NAME ## _table[n]();                                                      \
89
}
90

    
91
#define GEN16(func, NAME)                                                     \
92
static GenOpFunc *NAME ## _table [16] = {                                     \
93
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
94
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
95
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
96
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
97
};                                                                            \
98
static always_inline void func (int n)                                        \
99
{                                                                             \
100
    NAME ## _table[n]();                                                      \
101
}
102

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

    
119
/* Condition register moves */
120
GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);
121
GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);
122
GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);
123
#if 0 // Unused
124
GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);
125
#endif
126

    
127
/* General purpose registers moves */
128
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
129
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
130
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);
131

    
132
GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
133
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
134
#if 0 // unused
135
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
136
#endif
137

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

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

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

    
186
static always_inline void gen_set_Rc0 (DisasContext *ctx)
187
{
188
#if defined(TARGET_PPC64)
189
    if (ctx->sf_mode)
190
        gen_op_cmpi_64(0);
191
    else
192
#endif
193
        gen_op_cmpi(0);
194
    gen_op_set_Rc0();
195
}
196

    
197
static always_inline void gen_reset_fpstatus (void)
198
{
199
#ifdef CONFIG_SOFTFLOAT
200
    gen_op_reset_fpstatus();
201
#endif
202
}
203

    
204
static always_inline void gen_compute_fprf (int set_fprf, int set_rc)
205
{
206
    if (set_fprf != 0) {
207
        /* This case might be optimized later */
208
#if defined(OPTIMIZE_FPRF_UPDATE)
209
        *gen_fprf_ptr++ = gen_opc_ptr;
210
#endif
211
        gen_op_compute_fprf(1);
212
        if (unlikely(set_rc))
213
            gen_op_store_T0_crf(1);
214
        gen_op_float_check_status();
215
    } else if (unlikely(set_rc)) {
216
        /* We always need to compute fpcc */
217
        gen_op_compute_fprf(0);
218
        gen_op_store_T0_crf(1);
219
        if (set_fprf)
220
            gen_op_float_check_status();
221
    }
222
}
223

    
224
static always_inline void gen_optimize_fprf (void)
225
{
226
#if defined(OPTIMIZE_FPRF_UPDATE)
227
    uint16_t **ptr;
228

    
229
    for (ptr = gen_fprf_buf; ptr != (gen_fprf_ptr - 1); ptr++)
230
        *ptr = INDEX_op_nop1;
231
    gen_fprf_ptr = gen_fprf_buf;
232
#endif
233
}
234

    
235
static always_inline void gen_update_nip (DisasContext *ctx, target_ulong nip)
236
{
237
#if defined(TARGET_PPC64)
238
    if (ctx->sf_mode)
239
        gen_op_update_nip_64(nip >> 32, nip);
240
    else
241
#endif
242
        gen_op_update_nip(nip);
243
}
244

    
245
#define GEN_EXCP(ctx, excp, error)                                            \
246
do {                                                                          \
247
    if ((ctx)->exception == POWERPC_EXCP_NONE) {                              \
248
        gen_update_nip(ctx, (ctx)->nip);                                      \
249
    }                                                                         \
250
    gen_op_raise_exception_err((excp), (error));                              \
251
    ctx->exception = (excp);                                                  \
252
} while (0)
253

    
254
#define GEN_EXCP_INVAL(ctx)                                                   \
255
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
256
         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL)
257

    
258
#define GEN_EXCP_PRIVOPC(ctx)                                                 \
259
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
260
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_OPC)
261

    
262
#define GEN_EXCP_PRIVREG(ctx)                                                 \
263
GEN_EXCP((ctx), POWERPC_EXCP_PROGRAM,                                         \
264
         POWERPC_EXCP_INVAL | POWERPC_EXCP_PRIV_REG)
265

    
266
#define GEN_EXCP_NO_FP(ctx)                                                   \
267
GEN_EXCP(ctx, POWERPC_EXCP_FPU, 0)
268

    
269
#define GEN_EXCP_NO_AP(ctx)                                                   \
270
GEN_EXCP(ctx, POWERPC_EXCP_APU, 0)
271

    
272
#define GEN_EXCP_NO_VR(ctx)                                                   \
273
GEN_EXCP(ctx, POWERPC_EXCP_VPU, 0)
274

    
275
/* Stop translation */
276
static always_inline void GEN_STOP (DisasContext *ctx)
277
{
278
    gen_update_nip(ctx, ctx->nip);
279
    ctx->exception = POWERPC_EXCP_STOP;
280
}
281

    
282
/* No need to update nip here, as execution flow will change */
283
static always_inline void GEN_SYNC (DisasContext *ctx)
284
{
285
    ctx->exception = POWERPC_EXCP_SYNC;
286
}
287

    
288
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
289
static void gen_##name (DisasContext *ctx);                                   \
290
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
291
static void gen_##name (DisasContext *ctx)
292

    
293
#define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
294
static void gen_##name (DisasContext *ctx);                                   \
295
GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type);                       \
296
static void gen_##name (DisasContext *ctx)
297

    
298
typedef struct opcode_t {
299
    unsigned char opc1, opc2, opc3;
300
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
301
    unsigned char pad[5];
302
#else
303
    unsigned char pad[1];
304
#endif
305
    opc_handler_t handler;
306
    const unsigned char *oname;
307
} opcode_t;
308

    
309
/*****************************************************************************/
310
/***                           Instruction decoding                        ***/
311
#define EXTRACT_HELPER(name, shift, nb)                                       \
312
static always_inline uint32_t name (uint32_t opcode)                          \
313
{                                                                             \
314
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
315
}
316

    
317
#define EXTRACT_SHELPER(name, shift, nb)                                      \
318
static always_inline int32_t name (uint32_t opcode)                           \
319
{                                                                             \
320
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
321
}
322

    
323
/* Opcode part 1 */
324
EXTRACT_HELPER(opc1, 26, 6);
325
/* Opcode part 2 */
326
EXTRACT_HELPER(opc2, 1, 5);
327
/* Opcode part 3 */
328
EXTRACT_HELPER(opc3, 6, 5);
329
/* Update Cr0 flags */
330
EXTRACT_HELPER(Rc, 0, 1);
331
/* Destination */
332
EXTRACT_HELPER(rD, 21, 5);
333
/* Source */
334
EXTRACT_HELPER(rS, 21, 5);
335
/* First operand */
336
EXTRACT_HELPER(rA, 16, 5);
337
/* Second operand */
338
EXTRACT_HELPER(rB, 11, 5);
339
/* Third operand */
340
EXTRACT_HELPER(rC, 6, 5);
341
/***                               Get CRn                                 ***/
342
EXTRACT_HELPER(crfD, 23, 3);
343
EXTRACT_HELPER(crfS, 18, 3);
344
EXTRACT_HELPER(crbD, 21, 5);
345
EXTRACT_HELPER(crbA, 16, 5);
346
EXTRACT_HELPER(crbB, 11, 5);
347
/* SPR / TBL */
348
EXTRACT_HELPER(_SPR, 11, 10);
349
static always_inline uint32_t SPR (uint32_t opcode)
350
{
351
    uint32_t sprn = _SPR(opcode);
352

    
353
    return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
354
}
355
/***                              Get constants                            ***/
356
EXTRACT_HELPER(IMM, 12, 8);
357
/* 16 bits signed immediate value */
358
EXTRACT_SHELPER(SIMM, 0, 16);
359
/* 16 bits unsigned immediate value */
360
EXTRACT_HELPER(UIMM, 0, 16);
361
/* Bit count */
362
EXTRACT_HELPER(NB, 11, 5);
363
/* Shift count */
364
EXTRACT_HELPER(SH, 11, 5);
365
/* Mask start */
366
EXTRACT_HELPER(MB, 6, 5);
367
/* Mask end */
368
EXTRACT_HELPER(ME, 1, 5);
369
/* Trap operand */
370
EXTRACT_HELPER(TO, 21, 5);
371

    
372
EXTRACT_HELPER(CRM, 12, 8);
373
EXTRACT_HELPER(FM, 17, 8);
374
EXTRACT_HELPER(SR, 16, 4);
375
EXTRACT_HELPER(FPIMM, 20, 4);
376

    
377
/***                            Jump target decoding                       ***/
378
/* Displacement */
379
EXTRACT_SHELPER(d, 0, 16);
380
/* Immediate address */
381
static always_inline target_ulong LI (uint32_t opcode)
382
{
383
    return (opcode >> 0) & 0x03FFFFFC;
384
}
385

    
386
static always_inline uint32_t BD (uint32_t opcode)
387
{
388
    return (opcode >> 0) & 0xFFFC;
389
}
390

    
391
EXTRACT_HELPER(BO, 21, 5);
392
EXTRACT_HELPER(BI, 16, 5);
393
/* Absolute/relative address */
394
EXTRACT_HELPER(AA, 1, 1);
395
/* Link */
396
EXTRACT_HELPER(LK, 0, 1);
397

    
398
/* Create a mask between <start> and <end> bits */
399
static always_inline target_ulong MASK (uint32_t start, uint32_t end)
400
{
401
    target_ulong ret;
402

    
403
#if defined(TARGET_PPC64)
404
    if (likely(start == 0)) {
405
        ret = UINT64_MAX << (63 - end);
406
    } else if (likely(end == 63)) {
407
        ret = UINT64_MAX >> start;
408
    }
409
#else
410
    if (likely(start == 0)) {
411
        ret = UINT32_MAX << (31  - end);
412
    } else if (likely(end == 31)) {
413
        ret = UINT32_MAX >> start;
414
    }
415
#endif
416
    else {
417
        ret = (((target_ulong)(-1ULL)) >> (start)) ^
418
            (((target_ulong)(-1ULL) >> (end)) >> 1);
419
        if (unlikely(start > end))
420
            return ~ret;
421
    }
422

    
423
    return ret;
424
}
425

    
426
/*****************************************************************************/
427
/* PowerPC Instructions types definitions                                    */
428
enum {
429
    PPC_NONE          = 0x0000000000000000ULL,
430
    /* PowerPC base instructions set                                         */
431
    PPC_INSNS_BASE    = 0x0000000000000001ULL,
432
    /* integer operations instructions                                       */
433
#define PPC_INTEGER PPC_INSNS_BASE
434
    /* flow control instructions                                             */
435
#define PPC_FLOW    PPC_INSNS_BASE
436
    /* virtual memory instructions                                           */
437
#define PPC_MEM     PPC_INSNS_BASE
438
    /* ld/st with reservation instructions                                   */
439
#define PPC_RES     PPC_INSNS_BASE
440
    /* cache control instructions                                            */
441
#define PPC_CACHE   PPC_INSNS_BASE
442
    /* spr/msr access instructions                                           */
443
#define PPC_MISC    PPC_INSNS_BASE
444
    /* Optional floating point instructions                                  */
445
    PPC_FLOAT         = 0x0000000000000002ULL,
446
    PPC_FLOAT_FSQRT   = 0x0000000000000004ULL,
447
    PPC_FLOAT_FRES    = 0x0000000000000008ULL,
448
    PPC_FLOAT_FRSQRTE = 0x0000000000000010ULL,
449
    PPC_FLOAT_FSEL    = 0x0000000000000020ULL,
450
    PPC_FLOAT_STFIWX  = 0x0000000000000040ULL,
451
    /* external control instructions                                         */
452
    PPC_EXTERN        = 0x0000000000000080ULL,
453
    /* segment register access instructions                                  */
454
    PPC_SEGMENT       = 0x0000000000000100ULL,
455
    /* Optional cache control instruction                                    */
456
    PPC_CACHE_DCBA    = 0x0000000000000200ULL,
457
    /* Optional memory control instructions                                  */
458
    PPC_MEM_TLBIA     = 0x0000000000000400ULL,
459
    PPC_MEM_TLBIE     = 0x0000000000000800ULL,
460
    PPC_MEM_TLBSYNC   = 0x0000000000001000ULL,
461
    /* eieio & sync                                                          */
462
    PPC_MEM_SYNC      = 0x0000000000002000ULL,
463
    /* PowerPC 6xx TLB management instructions                               */
464
    PPC_6xx_TLB       = 0x0000000000004000ULL,
465
    /* Altivec support                                                       */
466
    PPC_ALTIVEC       = 0x0000000000008000ULL,
467
    /* Time base mftb instruction                                            */
468
    PPC_MFTB          = 0x0000000000010000ULL,
469
    /* Embedded PowerPC dedicated instructions                               */
470
    PPC_EMB_COMMON    = 0x0000000000020000ULL,
471
    /* PowerPC 40x exception model                                           */
472
    PPC_40x_EXCP      = 0x0000000000040000ULL,
473
    /* PowerPC 40x TLB management instructions                               */
474
    PPC_40x_TLB       = 0x0000000000080000ULL,
475
    /* PowerPC 405 Mac instructions                                          */
476
    PPC_405_MAC       = 0x0000000000100000ULL,
477
    /* PowerPC 440 specific instructions                                     */
478
    PPC_440_SPEC      = 0x0000000000200000ULL,
479
    /* Power-to-PowerPC bridge (601)                                         */
480
    PPC_POWER_BR      = 0x0000000000400000ULL,
481
    /* PowerPC 602 specific                                                  */
482
    PPC_602_SPEC      = 0x0000000000800000ULL,
483
    /* Deprecated instructions                                               */
484
    /* Original POWER instruction set                                        */
485
    PPC_POWER         = 0x0000000001000000ULL,
486
    /* POWER2 instruction set extension                                      */
487
    PPC_POWER2        = 0x0000000002000000ULL,
488
    /* Power RTC support                                                     */
489
    PPC_POWER_RTC     = 0x0000000004000000ULL,
490
    /* 64 bits PowerPC instruction set                                       */
491
    PPC_64B           = 0x0000000008000000ULL,
492
    /* 64 bits hypervisor extensions                                         */
493
    PPC_64H           = 0x0000000010000000ULL,
494
    /* segment register access instructions for PowerPC 64 "bridge"          */
495
    PPC_SEGMENT_64B   = 0x0000000020000000ULL,
496
    /* BookE (embedded) PowerPC specification                                */
497
    PPC_BOOKE         = 0x0000000040000000ULL,
498
    /* eieio                                                                 */
499
    PPC_MEM_EIEIO     = 0x0000000080000000ULL,
500
    /* e500 vector instructions                                              */
501
    PPC_E500_VECTOR   = 0x0000000100000000ULL,
502
    /* PowerPC 4xx dedicated instructions                                    */
503
    PPC_4xx_COMMON    = 0x0000000200000000ULL,
504
    /* PowerPC 2.03 specification extensions                                 */
505
    PPC_203           = 0x0000000400000000ULL,
506
    /* PowerPC 2.03 SPE extension                                            */
507
    PPC_SPE           = 0x0000000800000000ULL,
508
    /* PowerPC 2.03 SPE floating-point extension                             */
509
    PPC_SPEFPU        = 0x0000001000000000ULL,
510
    /* SLB management                                                        */
511
    PPC_SLBI          = 0x0000002000000000ULL,
512
    /* PowerPC 40x ibct instructions                                         */
513
    PPC_40x_ICBT      = 0x0000004000000000ULL,
514
    /* PowerPC 74xx TLB management instructions                              */
515
    PPC_74xx_TLB      = 0x0000008000000000ULL,
516
    /* More BookE (embedded) instructions...                                 */
517
    PPC_BOOKE_EXT     = 0x0000010000000000ULL,
518
    /* rfmci is not implemented in all BookE PowerPC                         */
519
    PPC_RFMCI         = 0x0000020000000000ULL,
520
    /* user-mode DCR access, implemented in PowerPC 460                      */
521
    PPC_DCRUX         = 0x0000040000000000ULL,
522
    /* New floating-point extensions (PowerPC 2.0x)                          */
523
    PPC_FLOAT_EXT     = 0x0000080000000000ULL,
524
    /* New wait instruction (PowerPC 2.0x)                                   */
525
    PPC_WAIT          = 0x0000100000000000ULL,
526
    /* New 64 bits extensions (PowerPC 2.0x)                                 */
527
    PPC_64BX          = 0x0000200000000000ULL,
528
    /* dcbz instruction with fixed cache line size                           */
529
    PPC_CACHE_DCBZ    = 0x0000400000000000ULL,
530
    /* dcbz instruction with tunable cache line size                         */
531
    PPC_CACHE_DCBZT   = 0x0000800000000000ULL,
532
    /* frsqrtes extension                                                    */
533
    PPC_FLOAT_FRSQRTES = 0x0001000000000000ULL,
534
};
535

    
536
/*****************************************************************************/
537
/* PowerPC instructions table                                                */
538
#if HOST_LONG_BITS == 64
539
#define OPC_ALIGN 8
540
#else
541
#define OPC_ALIGN 4
542
#endif
543
#if defined(__APPLE__)
544
#define OPCODES_SECTION                                                       \
545
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))
546
#else
547
#define OPCODES_SECTION                                                       \
548
    __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))
549
#endif
550

    
551
#if defined(DO_PPC_STATISTICS)
552
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
553
OPCODES_SECTION opcode_t opc_##name = {                                       \
554
    .opc1 = op1,                                                              \
555
    .opc2 = op2,                                                              \
556
    .opc3 = op3,                                                              \
557
    .pad  = { 0, },                                                           \
558
    .handler = {                                                              \
559
        .inval   = invl,                                                      \
560
        .type = _typ,                                                         \
561
        .handler = &gen_##name,                                               \
562
        .oname = stringify(name),                                             \
563
    },                                                                        \
564
    .oname = stringify(name),                                                 \
565
}
566
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
567
OPCODES_SECTION opcode_t opc_##name = {                                       \
568
    .opc1 = op1,                                                              \
569
    .opc2 = op2,                                                              \
570
    .opc3 = op3,                                                              \
571
    .pad  = { 0, },                                                           \
572
    .handler = {                                                              \
573
        .inval   = invl,                                                      \
574
        .type = _typ,                                                         \
575
        .handler = &gen_##name,                                               \
576
        .oname = onam,                                                        \
577
    },                                                                        \
578
    .oname = onam,                                                            \
579
}
580
#else
581
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
582
OPCODES_SECTION opcode_t opc_##name = {                                       \
583
    .opc1 = op1,                                                              \
584
    .opc2 = op2,                                                              \
585
    .opc3 = op3,                                                              \
586
    .pad  = { 0, },                                                           \
587
    .handler = {                                                              \
588
        .inval   = invl,                                                      \
589
        .type = _typ,                                                         \
590
        .handler = &gen_##name,                                               \
591
    },                                                                        \
592
    .oname = stringify(name),                                                 \
593
}
594
#define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ)                    \
595
OPCODES_SECTION opcode_t opc_##name = {                                       \
596
    .opc1 = op1,                                                              \
597
    .opc2 = op2,                                                              \
598
    .opc3 = op3,                                                              \
599
    .pad  = { 0, },                                                           \
600
    .handler = {                                                              \
601
        .inval   = invl,                                                      \
602
        .type = _typ,                                                         \
603
        .handler = &gen_##name,                                               \
604
    },                                                                        \
605
    .oname = onam,                                                            \
606
}
607
#endif
608

    
609
#define GEN_OPCODE_MARK(name)                                                 \
610
OPCODES_SECTION opcode_t opc_##name = {                                       \
611
    .opc1 = 0xFF,                                                             \
612
    .opc2 = 0xFF,                                                             \
613
    .opc3 = 0xFF,                                                             \
614
    .pad  = { 0, },                                                           \
615
    .handler = {                                                              \
616
        .inval   = 0x00000000,                                                \
617
        .type = 0x00,                                                         \
618
        .handler = NULL,                                                      \
619
    },                                                                        \
620
    .oname = stringify(name),                                                 \
621
}
622

    
623
/* Start opcode list */
624
GEN_OPCODE_MARK(start);
625

    
626
/* Invalid instruction */
627
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
628
{
629
    GEN_EXCP_INVAL(ctx);
630
}
631

    
632
static opc_handler_t invalid_handler = {
633
    .inval   = 0xFFFFFFFF,
634
    .type    = PPC_NONE,
635
    .handler = gen_invalid,
636
};
637

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

    
650
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval, type)               \
651
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
652
{                                                                             \
653
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
654
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
655
    gen_op_##name();                                                          \
656
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
657
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
658
        gen_set_Rc0(ctx);                                                     \
659
}
660

    
661
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3, type)                        \
662
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
663
{                                                                             \
664
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
665
    gen_op_##name();                                                          \
666
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
667
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
668
        gen_set_Rc0(ctx);                                                     \
669
}
670
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3, type)                      \
671
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
672
{                                                                             \
673
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
674
    gen_op_##name();                                                          \
675
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
676
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
677
        gen_set_Rc0(ctx);                                                     \
678
}
679

    
680
/* Two operands arithmetic functions */
681
#define GEN_INT_ARITH2(name, opc1, opc2, opc3, type)                          \
682
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000, type)                    \
683
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
684

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

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

    
694
#if defined(TARGET_PPC64)
695
#define __GEN_INT_ARITH2_64(name, opc1, opc2, opc3, inval, type)              \
696
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
697
{                                                                             \
698
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
699
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
700
    if (ctx->sf_mode)                                                         \
701
        gen_op_##name##_64();                                                 \
702
    else                                                                      \
703
        gen_op_##name();                                                      \
704
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
705
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
706
        gen_set_Rc0(ctx);                                                     \
707
}
708

    
709
#define __GEN_INT_ARITH2_O_64(name, opc1, opc2, opc3, inval, type)            \
710
GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                              \
711
{                                                                             \
712
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
713
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
714
    if (ctx->sf_mode)                                                         \
715
        gen_op_##name##_64();                                                 \
716
    else                                                                      \
717
        gen_op_##name();                                                      \
718
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
719
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
720
        gen_set_Rc0(ctx);                                                     \
721
}
722

    
723
#define __GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                     \
724
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
725
{                                                                             \
726
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
727
    if (ctx->sf_mode)                                                         \
728
        gen_op_##name##_64();                                                 \
729
    else                                                                      \
730
        gen_op_##name();                                                      \
731
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
732
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
733
        gen_set_Rc0(ctx);                                                     \
734
}
735
#define __GEN_INT_ARITH1_O_64(name, opc1, opc2, opc3, type)                   \
736
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, type)                         \
737
{                                                                             \
738
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
739
    if (ctx->sf_mode)                                                         \
740
        gen_op_##name##_64();                                                 \
741
    else                                                                      \
742
        gen_op_##name();                                                      \
743
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
744
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
745
        gen_set_Rc0(ctx);                                                     \
746
}
747

    
748
/* Two operands arithmetic functions */
749
#define GEN_INT_ARITH2_64(name, opc1, opc2, opc3, type)                       \
750
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000000, type)                 \
751
__GEN_INT_ARITH2_O_64(name##o, opc1, opc2, opc3 | 0x10, 0x00000000, type)
752

    
753
/* Two operands arithmetic functions with no overflow allowed */
754
#define GEN_INT_ARITHN_64(name, opc1, opc2, opc3, type)                       \
755
__GEN_INT_ARITH2_64(name, opc1, opc2, opc3, 0x00000400, type)
756

    
757
/* One operand arithmetic functions */
758
#define GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                       \
759
__GEN_INT_ARITH1_64(name, opc1, opc2, opc3, type)                             \
760
__GEN_INT_ARITH1_O_64(name##o, opc1, opc2, opc3 | 0x10, type)
761
#else
762
#define GEN_INT_ARITH2_64 GEN_INT_ARITH2
763
#define GEN_INT_ARITHN_64 GEN_INT_ARITHN
764
#define GEN_INT_ARITH1_64 GEN_INT_ARITH1
765
#endif
766

    
767
/* add    add.    addo    addo.    */
768
static always_inline void gen_op_addo (void)
769
{
770
    gen_op_move_T2_T0();
771
    gen_op_add();
772
    gen_op_check_addo();
773
}
774
#if defined(TARGET_PPC64)
775
#define gen_op_add_64 gen_op_add
776
static always_inline void gen_op_addo_64 (void)
777
{
778
    gen_op_move_T2_T0();
779
    gen_op_add();
780
    gen_op_check_addo_64();
781
}
782
#endif
783
GEN_INT_ARITH2_64 (add,    0x1F, 0x0A, 0x08, PPC_INTEGER);
784
/* addc   addc.   addco   addco.   */
785
static always_inline void gen_op_addc (void)
786
{
787
    gen_op_move_T2_T0();
788
    gen_op_add();
789
    gen_op_check_addc();
790
}
791
static always_inline void gen_op_addco (void)
792
{
793
    gen_op_move_T2_T0();
794
    gen_op_add();
795
    gen_op_check_addc();
796
    gen_op_check_addo();
797
}
798
#if defined(TARGET_PPC64)
799
static always_inline void gen_op_addc_64 (void)
800
{
801
    gen_op_move_T2_T0();
802
    gen_op_add();
803
    gen_op_check_addc_64();
804
}
805
static always_inline void gen_op_addco_64 (void)
806
{
807
    gen_op_move_T2_T0();
808
    gen_op_add();
809
    gen_op_check_addc_64();
810
    gen_op_check_addo_64();
811
}
812
#endif
813
GEN_INT_ARITH2_64 (addc,   0x1F, 0x0A, 0x00, PPC_INTEGER);
814
/* adde   adde.   addeo   addeo.   */
815
static always_inline void gen_op_addeo (void)
816
{
817
    gen_op_move_T2_T0();
818
    gen_op_adde();
819
    gen_op_check_addo();
820
}
821
#if defined(TARGET_PPC64)
822
static always_inline void gen_op_addeo_64 (void)
823
{
824
    gen_op_move_T2_T0();
825
    gen_op_adde_64();
826
    gen_op_check_addo_64();
827
}
828
#endif
829
GEN_INT_ARITH2_64 (adde,   0x1F, 0x0A, 0x04, PPC_INTEGER);
830
/* addme  addme.  addmeo  addmeo.  */
831
static always_inline void gen_op_addme (void)
832
{
833
    gen_op_move_T1_T0();
834
    gen_op_add_me();
835
}
836
#if defined(TARGET_PPC64)
837
static always_inline void gen_op_addme_64 (void)
838
{
839
    gen_op_move_T1_T0();
840
    gen_op_add_me_64();
841
}
842
#endif
843
GEN_INT_ARITH1_64 (addme,  0x1F, 0x0A, 0x07, PPC_INTEGER);
844
/* addze  addze.  addzeo  addzeo.  */
845
static always_inline void gen_op_addze (void)
846
{
847
    gen_op_move_T2_T0();
848
    gen_op_add_ze();
849
    gen_op_check_addc();
850
}
851
static always_inline void gen_op_addzeo (void)
852
{
853
    gen_op_move_T2_T0();
854
    gen_op_add_ze();
855
    gen_op_check_addc();
856
    gen_op_check_addo();
857
}
858
#if defined(TARGET_PPC64)
859
static always_inline void gen_op_addze_64 (void)
860
{
861
    gen_op_move_T2_T0();
862
    gen_op_add_ze();
863
    gen_op_check_addc_64();
864
}
865
static always_inline void gen_op_addzeo_64 (void)
866
{
867
    gen_op_move_T2_T0();
868
    gen_op_add_ze();
869
    gen_op_check_addc_64();
870
    gen_op_check_addo_64();
871
}
872
#endif
873
GEN_INT_ARITH1_64 (addze,  0x1F, 0x0A, 0x06, PPC_INTEGER);
874
/* divw   divw.   divwo   divwo.   */
875
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F, PPC_INTEGER);
876
/* divwu  divwu.  divwuo  divwuo.  */
877
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E, PPC_INTEGER);
878
/* mulhw  mulhw.                   */
879
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02, PPC_INTEGER);
880
/* mulhwu mulhwu.                  */
881
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00, PPC_INTEGER);
882
/* mullw  mullw.  mullwo  mullwo.  */
883
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07, PPC_INTEGER);
884
/* neg    neg.    nego    nego.    */
885
GEN_INT_ARITH1_64 (neg,    0x1F, 0x08, 0x03, PPC_INTEGER);
886
/* subf   subf.   subfo   subfo.   */
887
static always_inline void gen_op_subfo (void)
888
{
889
    gen_op_moven_T2_T0();
890
    gen_op_subf();
891
    gen_op_check_addo();
892
}
893
#if defined(TARGET_PPC64)
894
#define gen_op_subf_64 gen_op_subf
895
static always_inline void gen_op_subfo_64 (void)
896
{
897
    gen_op_moven_T2_T0();
898
    gen_op_subf();
899
    gen_op_check_addo_64();
900
}
901
#endif
902
GEN_INT_ARITH2_64 (subf,   0x1F, 0x08, 0x01, PPC_INTEGER);
903
/* subfc  subfc.  subfco  subfco.  */
904
static always_inline void gen_op_subfc (void)
905
{
906
    gen_op_subf();
907
    gen_op_check_subfc();
908
}
909
static always_inline void gen_op_subfco (void)
910
{
911
    gen_op_moven_T2_T0();
912
    gen_op_subf();
913
    gen_op_check_subfc();
914
    gen_op_check_addo();
915
}
916
#if defined(TARGET_PPC64)
917
static always_inline void gen_op_subfc_64 (void)
918
{
919
    gen_op_subf();
920
    gen_op_check_subfc_64();
921
}
922
static always_inline void gen_op_subfco_64 (void)
923
{
924
    gen_op_moven_T2_T0();
925
    gen_op_subf();
926
    gen_op_check_subfc_64();
927
    gen_op_check_addo_64();
928
}
929
#endif
930
GEN_INT_ARITH2_64 (subfc,  0x1F, 0x08, 0x00, PPC_INTEGER);
931
/* subfe  subfe.  subfeo  subfeo.  */
932
static always_inline void gen_op_subfeo (void)
933
{
934
    gen_op_moven_T2_T0();
935
    gen_op_subfe();
936
    gen_op_check_addo();
937
}
938
#if defined(TARGET_PPC64)
939
#define gen_op_subfe_64 gen_op_subfe
940
static always_inline void gen_op_subfeo_64 (void)
941
{
942
    gen_op_moven_T2_T0();
943
    gen_op_subfe_64();
944
    gen_op_check_addo_64();
945
}
946
#endif
947
GEN_INT_ARITH2_64 (subfe,  0x1F, 0x08, 0x04, PPC_INTEGER);
948
/* subfme subfme. subfmeo subfmeo. */
949
GEN_INT_ARITH1_64 (subfme, 0x1F, 0x08, 0x07, PPC_INTEGER);
950
/* subfze subfze. subfzeo subfzeo. */
951
GEN_INT_ARITH1_64 (subfze, 0x1F, 0x08, 0x06, PPC_INTEGER);
952
/* addi */
953
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
954
{
955
    target_long simm = SIMM(ctx->opcode);
956

    
957
    if (rA(ctx->opcode) == 0) {
958
        /* li case */
959
        gen_set_T0(simm);
960
    } else {
961
        gen_op_load_gpr_T0(rA(ctx->opcode));
962
        if (likely(simm != 0))
963
            gen_op_addi(simm);
964
    }
965
    gen_op_store_T0_gpr(rD(ctx->opcode));
966
}
967
/* addic */
968
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
969
{
970
    target_long simm = SIMM(ctx->opcode);
971

    
972
    gen_op_load_gpr_T0(rA(ctx->opcode));
973
    if (likely(simm != 0)) {
974
        gen_op_move_T2_T0();
975
        gen_op_addi(simm);
976
#if defined(TARGET_PPC64)
977
        if (ctx->sf_mode)
978
            gen_op_check_addc_64();
979
        else
980
#endif
981
            gen_op_check_addc();
982
    } else {
983
        gen_op_clear_xer_ca();
984
    }
985
    gen_op_store_T0_gpr(rD(ctx->opcode));
986
}
987
/* addic. */
988
GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
989
{
990
    target_long simm = SIMM(ctx->opcode);
991

    
992
    gen_op_load_gpr_T0(rA(ctx->opcode));
993
    if (likely(simm != 0)) {
994
        gen_op_move_T2_T0();
995
        gen_op_addi(simm);
996
#if defined(TARGET_PPC64)
997
        if (ctx->sf_mode)
998
            gen_op_check_addc_64();
999
        else
1000
#endif
1001
            gen_op_check_addc();
1002
    } else {
1003
        gen_op_clear_xer_ca();
1004
    }
1005
    gen_op_store_T0_gpr(rD(ctx->opcode));
1006
    gen_set_Rc0(ctx);
1007
}
1008
/* addis */
1009
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1010
{
1011
    target_long simm = SIMM(ctx->opcode);
1012

    
1013
    if (rA(ctx->opcode) == 0) {
1014
        /* lis case */
1015
        gen_set_T0(simm << 16);
1016
    } else {
1017
        gen_op_load_gpr_T0(rA(ctx->opcode));
1018
        if (likely(simm != 0))
1019
            gen_op_addi(simm << 16);
1020
    }
1021
    gen_op_store_T0_gpr(rD(ctx->opcode));
1022
}
1023
/* mulli */
1024
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1025
{
1026
    gen_op_load_gpr_T0(rA(ctx->opcode));
1027
    gen_op_mulli(SIMM(ctx->opcode));
1028
    gen_op_store_T0_gpr(rD(ctx->opcode));
1029
}
1030
/* subfic */
1031
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1032
{
1033
    gen_op_load_gpr_T0(rA(ctx->opcode));
1034
#if defined(TARGET_PPC64)
1035
    if (ctx->sf_mode)
1036
        gen_op_subfic_64(SIMM(ctx->opcode));
1037
    else
1038
#endif
1039
        gen_op_subfic(SIMM(ctx->opcode));
1040
    gen_op_store_T0_gpr(rD(ctx->opcode));
1041
}
1042

    
1043
#if defined(TARGET_PPC64)
1044
/* mulhd  mulhd.                   */
1045
GEN_INT_ARITHN (mulhd,  0x1F, 0x09, 0x02, PPC_64B);
1046
/* mulhdu mulhdu.                  */
1047
GEN_INT_ARITHN (mulhdu, 0x1F, 0x09, 0x00, PPC_64B);
1048
/* mulld  mulld.  mulldo  mulldo.  */
1049
GEN_INT_ARITH2 (mulld,  0x1F, 0x09, 0x07, PPC_64B);
1050
/* divd   divd.   divdo   divdo.   */
1051
GEN_INT_ARITH2 (divd,   0x1F, 0x09, 0x0F, PPC_64B);
1052
/* divdu  divdu.  divduo  divduo.  */
1053
GEN_INT_ARITH2 (divdu,  0x1F, 0x09, 0x0E, PPC_64B);
1054
#endif
1055

    
1056
/***                           Integer comparison                          ***/
1057
#if defined(TARGET_PPC64)
1058
#define GEN_CMP(name, opc, type)                                              \
1059
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
1060
{                                                                             \
1061
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1062
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1063
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))                           \
1064
        gen_op_##name##_64();                                                 \
1065
    else                                                                      \
1066
        gen_op_##name();                                                      \
1067
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
1068
}
1069
#else
1070
#define GEN_CMP(name, opc, type)                                              \
1071
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, type)                          \
1072
{                                                                             \
1073
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1074
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1075
    gen_op_##name();                                                          \
1076
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
1077
}
1078
#endif
1079

    
1080
/* cmp */
1081
GEN_CMP(cmp, 0x00, PPC_INTEGER);
1082
/* cmpi */
1083
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
1084
{
1085
    gen_op_load_gpr_T0(rA(ctx->opcode));
1086
#if defined(TARGET_PPC64)
1087
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1088
        gen_op_cmpi_64(SIMM(ctx->opcode));
1089
    else
1090
#endif
1091
        gen_op_cmpi(SIMM(ctx->opcode));
1092
    gen_op_store_T0_crf(crfD(ctx->opcode));
1093
}
1094
/* cmpl */
1095
GEN_CMP(cmpl, 0x01, PPC_INTEGER);
1096
/* cmpli */
1097
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
1098
{
1099
    gen_op_load_gpr_T0(rA(ctx->opcode));
1100
#if defined(TARGET_PPC64)
1101
    if (ctx->sf_mode && (ctx->opcode & 0x00200000))
1102
        gen_op_cmpli_64(UIMM(ctx->opcode));
1103
    else
1104
#endif
1105
        gen_op_cmpli(UIMM(ctx->opcode));
1106
    gen_op_store_T0_crf(crfD(ctx->opcode));
1107
}
1108

    
1109
/* isel (PowerPC 2.03 specification) */
1110
GEN_HANDLER(isel, 0x1F, 0x0F, 0x00, 0x00000001, PPC_203)
1111
{
1112
    uint32_t bi = rC(ctx->opcode);
1113
    uint32_t mask;
1114

    
1115
    if (rA(ctx->opcode) == 0) {
1116
        gen_set_T0(0);
1117
    } else {
1118
        gen_op_load_gpr_T1(rA(ctx->opcode));
1119
    }
1120
    gen_op_load_gpr_T2(rB(ctx->opcode));
1121
    mask = 1 << (3 - (bi & 0x03));
1122
    gen_op_load_crf_T0(bi >> 2);
1123
    gen_op_test_true(mask);
1124
    gen_op_isel();
1125
    gen_op_store_T0_gpr(rD(ctx->opcode));
1126
}
1127

    
1128
/***                            Integer logical                            ***/
1129
#define __GEN_LOGICAL2(name, opc2, opc3, type)                                \
1130
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, type)                         \
1131
{                                                                             \
1132
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
1133
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1134
    gen_op_##name();                                                          \
1135
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1136
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1137
        gen_set_Rc0(ctx);                                                     \
1138
}
1139
#define GEN_LOGICAL2(name, opc, type)                                         \
1140
__GEN_LOGICAL2(name, 0x1C, opc, type)
1141

    
1142
#define GEN_LOGICAL1(name, opc, type)                                         \
1143
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)                          \
1144
{                                                                             \
1145
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
1146
    gen_op_##name();                                                          \
1147
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1148
    if (unlikely(Rc(ctx->opcode) != 0))                                       \
1149
        gen_set_Rc0(ctx);                                                     \
1150
}
1151

    
1152
/* and & and. */
1153
GEN_LOGICAL2(and, 0x00, PPC_INTEGER);
1154
/* andc & andc. */
1155
GEN_LOGICAL2(andc, 0x01, PPC_INTEGER);
1156
/* andi. */
1157
GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1158
{
1159
    gen_op_load_gpr_T0(rS(ctx->opcode));
1160
    gen_op_andi_T0(UIMM(ctx->opcode));
1161
    gen_op_store_T0_gpr(rA(ctx->opcode));
1162
    gen_set_Rc0(ctx);
1163
}
1164
/* andis. */
1165
GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1166
{
1167
    gen_op_load_gpr_T0(rS(ctx->opcode));
1168
    gen_op_andi_T0(UIMM(ctx->opcode) << 16);
1169
    gen_op_store_T0_gpr(rA(ctx->opcode));
1170
    gen_set_Rc0(ctx);
1171
}
1172

    
1173
/* cntlzw */
1174
GEN_LOGICAL1(cntlzw, 0x00, PPC_INTEGER);
1175
/* eqv & eqv. */
1176
GEN_LOGICAL2(eqv, 0x08, PPC_INTEGER);
1177
/* extsb & extsb. */
1178
GEN_LOGICAL1(extsb, 0x1D, PPC_INTEGER);
1179
/* extsh & extsh. */
1180
GEN_LOGICAL1(extsh, 0x1C, PPC_INTEGER);
1181
/* nand & nand. */
1182
GEN_LOGICAL2(nand, 0x0E, PPC_INTEGER);
1183
/* nor & nor. */
1184
GEN_LOGICAL2(nor, 0x03, PPC_INTEGER);
1185

    
1186
/* or & or. */
1187
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
1188
{
1189
    int rs, ra, rb;
1190

    
1191
    rs = rS(ctx->opcode);
1192
    ra = rA(ctx->opcode);
1193
    rb = rB(ctx->opcode);
1194
    /* Optimisation for mr. ri case */
1195
    if (rs != ra || rs != rb) {
1196
        gen_op_load_gpr_T0(rs);
1197
        if (rs != rb) {
1198
            gen_op_load_gpr_T1(rb);
1199
            gen_op_or();
1200
        }
1201
        gen_op_store_T0_gpr(ra);
1202
        if (unlikely(Rc(ctx->opcode) != 0))
1203
            gen_set_Rc0(ctx);
1204
    } else if (unlikely(Rc(ctx->opcode) != 0)) {
1205
        gen_op_load_gpr_T0(rs);
1206
        gen_set_Rc0(ctx);
1207
#if defined(TARGET_PPC64)
1208
    } else {
1209
        switch (rs) {
1210
        case 1:
1211
            /* Set process priority to low */
1212
            gen_op_store_pri(2);
1213
            break;
1214
        case 6:
1215
            /* Set process priority to medium-low */
1216
            gen_op_store_pri(3);
1217
            break;
1218
        case 2:
1219
            /* Set process priority to normal */
1220
            gen_op_store_pri(4);
1221
            break;
1222
#if !defined(CONFIG_USER_ONLY)
1223
        case 31:
1224
            if (ctx->supervisor > 0) {
1225
                /* Set process priority to very low */
1226
                gen_op_store_pri(1);
1227
            }
1228
            break;
1229
        case 5:
1230
            if (ctx->supervisor > 0) {
1231
                /* Set process priority to medium-hight */
1232
                gen_op_store_pri(5);
1233
            }
1234
            break;
1235
        case 3:
1236
            if (ctx->supervisor > 0) {
1237
                /* Set process priority to high */
1238
                gen_op_store_pri(6);
1239
            }
1240
            break;
1241
#if defined(TARGET_PPC64H)
1242
        case 7:
1243
            if (ctx->supervisor > 1) {
1244
                /* Set process priority to very high */
1245
                gen_op_store_pri(7);
1246
            }
1247
            break;
1248
#endif
1249
#endif
1250
        default:
1251
            /* nop */
1252
            break;
1253
        }
1254
#endif
1255
    }
1256
}
1257

    
1258
/* orc & orc. */
1259
GEN_LOGICAL2(orc, 0x0C, PPC_INTEGER);
1260
/* xor & xor. */
1261
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
1262
{
1263
    gen_op_load_gpr_T0(rS(ctx->opcode));
1264
    /* Optimisation for "set to zero" case */
1265
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
1266
        gen_op_load_gpr_T1(rB(ctx->opcode));
1267
        gen_op_xor();
1268
    } else {
1269
        gen_op_reset_T0();
1270
    }
1271
    gen_op_store_T0_gpr(rA(ctx->opcode));
1272
    if (unlikely(Rc(ctx->opcode) != 0))
1273
        gen_set_Rc0(ctx);
1274
}
1275
/* ori */
1276
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1277
{
1278
    target_ulong uimm = UIMM(ctx->opcode);
1279

    
1280
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1281
        /* NOP */
1282
        /* XXX: should handle special NOPs for POWER series */
1283
        return;
1284
    }
1285
    gen_op_load_gpr_T0(rS(ctx->opcode));
1286
    if (likely(uimm != 0))
1287
        gen_op_ori(uimm);
1288
    gen_op_store_T0_gpr(rA(ctx->opcode));
1289
}
1290
/* oris */
1291
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1292
{
1293
    target_ulong uimm = UIMM(ctx->opcode);
1294

    
1295
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1296
        /* NOP */
1297
        return;
1298
    }
1299
    gen_op_load_gpr_T0(rS(ctx->opcode));
1300
    if (likely(uimm != 0))
1301
        gen_op_ori(uimm << 16);
1302
    gen_op_store_T0_gpr(rA(ctx->opcode));
1303
}
1304
/* xori */
1305
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1306
{
1307
    target_ulong uimm = UIMM(ctx->opcode);
1308

    
1309
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1310
        /* NOP */
1311
        return;
1312
    }
1313
    gen_op_load_gpr_T0(rS(ctx->opcode));
1314
    if (likely(uimm != 0))
1315
        gen_op_xori(uimm);
1316
    gen_op_store_T0_gpr(rA(ctx->opcode));
1317
}
1318

    
1319
/* xoris */
1320
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1321
{
1322
    target_ulong uimm = UIMM(ctx->opcode);
1323

    
1324
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1325
        /* NOP */
1326
        return;
1327
    }
1328
    gen_op_load_gpr_T0(rS(ctx->opcode));
1329
    if (likely(uimm != 0))
1330
        gen_op_xori(uimm << 16);
1331
    gen_op_store_T0_gpr(rA(ctx->opcode));
1332
}
1333

    
1334
/* popcntb : PowerPC 2.03 specification */
1335
GEN_HANDLER(popcntb, 0x1F, 0x03, 0x03, 0x0000F801, PPC_203)
1336
{
1337
    gen_op_load_gpr_T0(rS(ctx->opcode));
1338
#if defined(TARGET_PPC64)
1339
    if (ctx->sf_mode)
1340
        gen_op_popcntb_64();
1341
    else
1342
#endif
1343
        gen_op_popcntb();
1344
    gen_op_store_T0_gpr(rA(ctx->opcode));
1345
}
1346

    
1347
#if defined(TARGET_PPC64)
1348
/* extsw & extsw. */
1349
GEN_LOGICAL1(extsw, 0x1E, PPC_64B);
1350
/* cntlzd */
1351
GEN_LOGICAL1(cntlzd, 0x01, PPC_64B);
1352
#endif
1353

    
1354
/***                             Integer rotate                            ***/
1355
/* rlwimi & rlwimi. */
1356
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1357
{
1358
    target_ulong mask;
1359
    uint32_t mb, me, sh;
1360

    
1361
    mb = MB(ctx->opcode);
1362
    me = ME(ctx->opcode);
1363
    sh = SH(ctx->opcode);
1364
    if (likely(sh == 0)) {
1365
        if (likely(mb == 0 && me == 31)) {
1366
            gen_op_load_gpr_T0(rS(ctx->opcode));
1367
            goto do_store;
1368
        } else if (likely(mb == 31 && me == 0)) {
1369
            gen_op_load_gpr_T0(rA(ctx->opcode));
1370
            goto do_store;
1371
        }
1372
        gen_op_load_gpr_T0(rS(ctx->opcode));
1373
        gen_op_load_gpr_T1(rA(ctx->opcode));
1374
        goto do_mask;
1375
    }
1376
    gen_op_load_gpr_T0(rS(ctx->opcode));
1377
    gen_op_load_gpr_T1(rA(ctx->opcode));
1378
    gen_op_rotli32_T0(SH(ctx->opcode));
1379
 do_mask:
1380
#if defined(TARGET_PPC64)
1381
    mb += 32;
1382
    me += 32;
1383
#endif
1384
    mask = MASK(mb, me);
1385
    gen_op_andi_T0(mask);
1386
    gen_op_andi_T1(~mask);
1387
    gen_op_or();
1388
 do_store:
1389
    gen_op_store_T0_gpr(rA(ctx->opcode));
1390
    if (unlikely(Rc(ctx->opcode) != 0))
1391
        gen_set_Rc0(ctx);
1392
}
1393
/* rlwinm & rlwinm. */
1394
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1395
{
1396
    uint32_t mb, me, sh;
1397

    
1398
    sh = SH(ctx->opcode);
1399
    mb = MB(ctx->opcode);
1400
    me = ME(ctx->opcode);
1401
    gen_op_load_gpr_T0(rS(ctx->opcode));
1402
    if (likely(sh == 0)) {
1403
        goto do_mask;
1404
    }
1405
    if (likely(mb == 0)) {
1406
        if (likely(me == 31)) {
1407
            gen_op_rotli32_T0(sh);
1408
            goto do_store;
1409
        } else if (likely(me == (31 - sh))) {
1410
            gen_op_sli_T0(sh);
1411
            goto do_store;
1412
        }
1413
    } else if (likely(me == 31)) {
1414
        if (likely(sh == (32 - mb))) {
1415
            gen_op_srli_T0(mb);
1416
            goto do_store;
1417
        }
1418
    }
1419
    gen_op_rotli32_T0(sh);
1420
 do_mask:
1421
#if defined(TARGET_PPC64)
1422
    mb += 32;
1423
    me += 32;
1424
#endif
1425
    gen_op_andi_T0(MASK(mb, me));
1426
 do_store:
1427
    gen_op_store_T0_gpr(rA(ctx->opcode));
1428
    if (unlikely(Rc(ctx->opcode) != 0))
1429
        gen_set_Rc0(ctx);
1430
}
1431
/* rlwnm & rlwnm. */
1432
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1433
{
1434
    uint32_t mb, me;
1435

    
1436
    mb = MB(ctx->opcode);
1437
    me = ME(ctx->opcode);
1438
    gen_op_load_gpr_T0(rS(ctx->opcode));
1439
    gen_op_load_gpr_T1(rB(ctx->opcode));
1440
    gen_op_rotl32_T0_T1();
1441
    if (unlikely(mb != 0 || me != 31)) {
1442
#if defined(TARGET_PPC64)
1443
        mb += 32;
1444
        me += 32;
1445
#endif
1446
        gen_op_andi_T0(MASK(mb, me));
1447
    }
1448
    gen_op_store_T0_gpr(rA(ctx->opcode));
1449
    if (unlikely(Rc(ctx->opcode) != 0))
1450
        gen_set_Rc0(ctx);
1451
}
1452

    
1453
#if defined(TARGET_PPC64)
1454
#define GEN_PPC64_R2(name, opc1, opc2)                                        \
1455
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1456
{                                                                             \
1457
    gen_##name(ctx, 0);                                                       \
1458
}                                                                             \
1459
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1460
             PPC_64B)                                                         \
1461
{                                                                             \
1462
    gen_##name(ctx, 1);                                                       \
1463
}
1464
#define GEN_PPC64_R4(name, opc1, opc2)                                        \
1465
GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B) \
1466
{                                                                             \
1467
    gen_##name(ctx, 0, 0);                                                    \
1468
}                                                                             \
1469
GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
1470
             PPC_64B)                                                         \
1471
{                                                                             \
1472
    gen_##name(ctx, 0, 1);                                                    \
1473
}                                                                             \
1474
GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
1475
             PPC_64B)                                                         \
1476
{                                                                             \
1477
    gen_##name(ctx, 1, 0);                                                    \
1478
}                                                                             \
1479
GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
1480
             PPC_64B)                                                         \
1481
{                                                                             \
1482
    gen_##name(ctx, 1, 1);                                                    \
1483
}
1484

    
1485
static always_inline void gen_andi_T0_64 (DisasContext *ctx, uint64_t mask)
1486
{
1487
    if (mask >> 32)
1488
        gen_op_andi_T0_64(mask >> 32, mask & 0xFFFFFFFF);
1489
    else
1490
        gen_op_andi_T0(mask);
1491
}
1492

    
1493
static always_inline void gen_andi_T1_64 (DisasContext *ctx, uint64_t mask)
1494
{
1495
    if (mask >> 32)
1496
        gen_op_andi_T1_64(mask >> 32, mask & 0xFFFFFFFF);
1497
    else
1498
        gen_op_andi_T1(mask);
1499
}
1500

    
1501
static always_inline void gen_rldinm (DisasContext *ctx, uint32_t mb,
1502
                                      uint32_t me, uint32_t sh)
1503
{
1504
    gen_op_load_gpr_T0(rS(ctx->opcode));
1505
    if (likely(sh == 0)) {
1506
        goto do_mask;
1507
    }
1508
    if (likely(mb == 0)) {
1509
        if (likely(me == 63)) {
1510
            gen_op_rotli64_T0(sh);
1511
            goto do_store;
1512
        } else if (likely(me == (63 - sh))) {
1513
            gen_op_sli_T0(sh);
1514
            goto do_store;
1515
        }
1516
    } else if (likely(me == 63)) {
1517
        if (likely(sh == (64 - mb))) {
1518
            gen_op_srli_T0_64(mb);
1519
            goto do_store;
1520
        }
1521
    }
1522
    gen_op_rotli64_T0(sh);
1523
 do_mask:
1524
    gen_andi_T0_64(ctx, MASK(mb, me));
1525
 do_store:
1526
    gen_op_store_T0_gpr(rA(ctx->opcode));
1527
    if (unlikely(Rc(ctx->opcode) != 0))
1528
        gen_set_Rc0(ctx);
1529
}
1530
/* rldicl - rldicl. */
1531
static always_inline void gen_rldicl (DisasContext *ctx, int mbn, int shn)
1532
{
1533
    uint32_t sh, mb;
1534

    
1535
    sh = SH(ctx->opcode) | (shn << 5);
1536
    mb = MB(ctx->opcode) | (mbn << 5);
1537
    gen_rldinm(ctx, mb, 63, sh);
1538
}
1539
GEN_PPC64_R4(rldicl, 0x1E, 0x00);
1540
/* rldicr - rldicr. */
1541
static always_inline void gen_rldicr (DisasContext *ctx, int men, int shn)
1542
{
1543
    uint32_t sh, me;
1544

    
1545
    sh = SH(ctx->opcode) | (shn << 5);
1546
    me = MB(ctx->opcode) | (men << 5);
1547
    gen_rldinm(ctx, 0, me, sh);
1548
}
1549
GEN_PPC64_R4(rldicr, 0x1E, 0x02);
1550
/* rldic - rldic. */
1551
static always_inline void gen_rldic (DisasContext *ctx, int mbn, int shn)
1552
{
1553
    uint32_t sh, mb;
1554

    
1555
    sh = SH(ctx->opcode) | (shn << 5);
1556
    mb = MB(ctx->opcode) | (mbn << 5);
1557
    gen_rldinm(ctx, mb, 63 - sh, sh);
1558
}
1559
GEN_PPC64_R4(rldic, 0x1E, 0x04);
1560

    
1561
static always_inline void gen_rldnm (DisasContext *ctx, uint32_t mb,
1562
                                     uint32_t me)
1563
{
1564
    gen_op_load_gpr_T0(rS(ctx->opcode));
1565
    gen_op_load_gpr_T1(rB(ctx->opcode));
1566
    gen_op_rotl64_T0_T1();
1567
    if (unlikely(mb != 0 || me != 63)) {
1568
        gen_andi_T0_64(ctx, MASK(mb, me));
1569
    }
1570
    gen_op_store_T0_gpr(rA(ctx->opcode));
1571
    if (unlikely(Rc(ctx->opcode) != 0))
1572
        gen_set_Rc0(ctx);
1573
}
1574

    
1575
/* rldcl - rldcl. */
1576
static always_inline void gen_rldcl (DisasContext *ctx, int mbn)
1577
{
1578
    uint32_t mb;
1579

    
1580
    mb = MB(ctx->opcode) | (mbn << 5);
1581
    gen_rldnm(ctx, mb, 63);
1582
}
1583
GEN_PPC64_R2(rldcl, 0x1E, 0x08);
1584
/* rldcr - rldcr. */
1585
static always_inline void gen_rldcr (DisasContext *ctx, int men)
1586
{
1587
    uint32_t me;
1588

    
1589
    me = MB(ctx->opcode) | (men << 5);
1590
    gen_rldnm(ctx, 0, me);
1591
}
1592
GEN_PPC64_R2(rldcr, 0x1E, 0x09);
1593
/* rldimi - rldimi. */
1594
static always_inline void gen_rldimi (DisasContext *ctx, int mbn, int shn)
1595
{
1596
    uint64_t mask;
1597
    uint32_t sh, mb;
1598

    
1599
    sh = SH(ctx->opcode) | (shn << 5);
1600
    mb = MB(ctx->opcode) | (mbn << 5);
1601
    if (likely(sh == 0)) {
1602
        if (likely(mb == 0)) {
1603
            gen_op_load_gpr_T0(rS(ctx->opcode));
1604
            goto do_store;
1605
        } else if (likely(mb == 63)) {
1606
            gen_op_load_gpr_T0(rA(ctx->opcode));
1607
            goto do_store;
1608
        }
1609
        gen_op_load_gpr_T0(rS(ctx->opcode));
1610
        gen_op_load_gpr_T1(rA(ctx->opcode));
1611
        goto do_mask;
1612
    }
1613
    gen_op_load_gpr_T0(rS(ctx->opcode));
1614
    gen_op_load_gpr_T1(rA(ctx->opcode));
1615
    gen_op_rotli64_T0(sh);
1616
 do_mask:
1617
    mask = MASK(mb, 63 - sh);
1618
    gen_andi_T0_64(ctx, mask);
1619
    gen_andi_T1_64(ctx, ~mask);
1620
    gen_op_or();
1621
 do_store:
1622
    gen_op_store_T0_gpr(rA(ctx->opcode));
1623
    if (unlikely(Rc(ctx->opcode) != 0))
1624
        gen_set_Rc0(ctx);
1625
}
1626
GEN_PPC64_R4(rldimi, 0x1E, 0x06);
1627
#endif
1628

    
1629
/***                             Integer shift                             ***/
1630
/* slw & slw. */
1631
__GEN_LOGICAL2(slw, 0x18, 0x00, PPC_INTEGER);
1632
/* sraw & sraw. */
1633
__GEN_LOGICAL2(sraw, 0x18, 0x18, PPC_INTEGER);
1634
/* srawi & srawi. */
1635
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
1636
{
1637
    int mb, me;
1638
    gen_op_load_gpr_T0(rS(ctx->opcode));
1639
    if (SH(ctx->opcode) != 0) {
1640
        gen_op_move_T1_T0();
1641
        mb = 32 - SH(ctx->opcode);
1642
        me = 31;
1643
#if defined(TARGET_PPC64)
1644
        mb += 32;
1645
        me += 32;
1646
#endif
1647
        gen_op_srawi(SH(ctx->opcode), MASK(mb, me));
1648
    }
1649
    gen_op_store_T0_gpr(rA(ctx->opcode));
1650
    if (unlikely(Rc(ctx->opcode) != 0))
1651
        gen_set_Rc0(ctx);
1652
}
1653
/* srw & srw. */
1654
__GEN_LOGICAL2(srw, 0x18, 0x10, PPC_INTEGER);
1655

    
1656
#if defined(TARGET_PPC64)
1657
/* sld & sld. */
1658
__GEN_LOGICAL2(sld, 0x1B, 0x00, PPC_64B);
1659
/* srad & srad. */
1660
__GEN_LOGICAL2(srad, 0x1A, 0x18, PPC_64B);
1661
/* sradi & sradi. */
1662
static always_inline void gen_sradi (DisasContext *ctx, int n)
1663
{
1664
    uint64_t mask;
1665
    int sh, mb, me;
1666

    
1667
    gen_op_load_gpr_T0(rS(ctx->opcode));
1668
    sh = SH(ctx->opcode) + (n << 5);
1669
    if (sh != 0) {
1670
        gen_op_move_T1_T0();
1671
        mb = 64 - SH(ctx->opcode);
1672
        me = 63;
1673
        mask = MASK(mb, me);
1674
        gen_op_sradi(sh, mask >> 32, mask);
1675
    }
1676
    gen_op_store_T0_gpr(rA(ctx->opcode));
1677
    if (unlikely(Rc(ctx->opcode) != 0))
1678
        gen_set_Rc0(ctx);
1679
}
1680
GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B)
1681
{
1682
    gen_sradi(ctx, 0);
1683
}
1684
GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B)
1685
{
1686
    gen_sradi(ctx, 1);
1687
}
1688
/* srd & srd. */
1689
__GEN_LOGICAL2(srd, 0x1B, 0x10, PPC_64B);
1690
#endif
1691

    
1692
/***                       Floating-Point arithmetic                       ***/
1693
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat, set_fprf, type)           \
1694
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, type)                        \
1695
{                                                                             \
1696
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1697
        GEN_EXCP_NO_FP(ctx);                                                  \
1698
        return;                                                               \
1699
    }                                                                         \
1700
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
1701
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
1702
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
1703
    gen_reset_fpstatus();                                                     \
1704
    gen_op_f##op();                                                           \
1705
    if (isfloat) {                                                            \
1706
        gen_op_frsp();                                                        \
1707
    }                                                                         \
1708
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1709
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1710
}
1711

    
1712
#define GEN_FLOAT_ACB(name, op2, set_fprf, type)                              \
1713
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0, set_fprf, type);                     \
1714
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1, set_fprf, type);
1715

    
1716
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
1717
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1718
{                                                                             \
1719
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1720
        GEN_EXCP_NO_FP(ctx);                                                  \
1721
        return;                                                               \
1722
    }                                                                         \
1723
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
1724
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
1725
    gen_reset_fpstatus();                                                     \
1726
    gen_op_f##op();                                                           \
1727
    if (isfloat) {                                                            \
1728
        gen_op_frsp();                                                        \
1729
    }                                                                         \
1730
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1731
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1732
}
1733
#define GEN_FLOAT_AB(name, op2, inval, set_fprf, type)                        \
1734
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
1735
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
1736

    
1737
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat, set_fprf, type)     \
1738
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, type)                             \
1739
{                                                                             \
1740
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1741
        GEN_EXCP_NO_FP(ctx);                                                  \
1742
        return;                                                               \
1743
    }                                                                         \
1744
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
1745
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
1746
    gen_reset_fpstatus();                                                     \
1747
    gen_op_f##op();                                                           \
1748
    if (isfloat) {                                                            \
1749
        gen_op_frsp();                                                        \
1750
    }                                                                         \
1751
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1752
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1753
}
1754
#define GEN_FLOAT_AC(name, op2, inval, set_fprf, type)                        \
1755
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0, set_fprf, type);               \
1756
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1, set_fprf, type);
1757

    
1758
#define GEN_FLOAT_B(name, op2, op3, set_fprf, type)                           \
1759
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, type)                        \
1760
{                                                                             \
1761
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1762
        GEN_EXCP_NO_FP(ctx);                                                  \
1763
        return;                                                               \
1764
    }                                                                         \
1765
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
1766
    gen_reset_fpstatus();                                                     \
1767
    gen_op_f##name();                                                         \
1768
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1769
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1770
}
1771

    
1772
#define GEN_FLOAT_BS(name, op1, op2, set_fprf, type)                          \
1773
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, type)                        \
1774
{                                                                             \
1775
    if (unlikely(!ctx->fpu_enabled)) {                                        \
1776
        GEN_EXCP_NO_FP(ctx);                                                  \
1777
        return;                                                               \
1778
    }                                                                         \
1779
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
1780
    gen_reset_fpstatus();                                                     \
1781
    gen_op_f##name();                                                         \
1782
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
1783
    gen_compute_fprf(set_fprf, Rc(ctx->opcode) != 0);                         \
1784
}
1785

    
1786
/* fadd - fadds */
1787
GEN_FLOAT_AB(add, 0x15, 0x000007C0, 1, PPC_FLOAT);
1788
/* fdiv - fdivs */
1789
GEN_FLOAT_AB(div, 0x12, 0x000007C0, 1, PPC_FLOAT);
1790
/* fmul - fmuls */
1791
GEN_FLOAT_AC(mul, 0x19, 0x0000F800, 1, PPC_FLOAT);
1792

    
1793
/* fre */
1794
GEN_FLOAT_BS(re, 0x3F, 0x18, 1, PPC_FLOAT_EXT);
1795

    
1796
/* fres */
1797
GEN_FLOAT_BS(res, 0x3B, 0x18, 1, PPC_FLOAT_FRES);
1798

    
1799
/* frsqrte */
1800
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTE);
1801

    
1802
/* frsqrtes */
1803
static always_inline void gen_op_frsqrtes (void)
1804
{
1805
    gen_op_frsqrte();
1806
    gen_op_frsp();
1807
}
1808
GEN_FLOAT_BS(rsqrtes, 0x3F, 0x1A, 1, PPC_FLOAT_FRSQRTES);
1809

    
1810
/* fsel */
1811
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0, 0, PPC_FLOAT_FSEL);
1812
/* fsub - fsubs */
1813
GEN_FLOAT_AB(sub, 0x14, 0x000007C0, 1, PPC_FLOAT);
1814
/* Optional: */
1815
/* fsqrt */
1816
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1817
{
1818
    if (unlikely(!ctx->fpu_enabled)) {
1819
        GEN_EXCP_NO_FP(ctx);
1820
        return;
1821
    }
1822
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1823
    gen_reset_fpstatus();
1824
    gen_op_fsqrt();
1825
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1826
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
1827
}
1828

    
1829
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_FSQRT)
1830
{
1831
    if (unlikely(!ctx->fpu_enabled)) {
1832
        GEN_EXCP_NO_FP(ctx);
1833
        return;
1834
    }
1835
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1836
    gen_reset_fpstatus();
1837
    gen_op_fsqrt();
1838
    gen_op_frsp();
1839
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1840
    gen_compute_fprf(1, Rc(ctx->opcode) != 0);
1841
}
1842

    
1843
/***                     Floating-Point multiply-and-add                   ***/
1844
/* fmadd - fmadds */
1845
GEN_FLOAT_ACB(madd, 0x1D, 1, PPC_FLOAT);
1846
/* fmsub - fmsubs */
1847
GEN_FLOAT_ACB(msub, 0x1C, 1, PPC_FLOAT);
1848
/* fnmadd - fnmadds */
1849
GEN_FLOAT_ACB(nmadd, 0x1F, 1, PPC_FLOAT);
1850
/* fnmsub - fnmsubs */
1851
GEN_FLOAT_ACB(nmsub, 0x1E, 1, PPC_FLOAT);
1852

    
1853
/***                     Floating-Point round & convert                    ***/
1854
/* fctiw */
1855
GEN_FLOAT_B(ctiw, 0x0E, 0x00, 0, PPC_FLOAT);
1856
/* fctiwz */
1857
GEN_FLOAT_B(ctiwz, 0x0F, 0x00, 0, PPC_FLOAT);
1858
/* frsp */
1859
GEN_FLOAT_B(rsp, 0x0C, 0x00, 1, PPC_FLOAT);
1860
#if defined(TARGET_PPC64)
1861
/* fcfid */
1862
GEN_FLOAT_B(cfid, 0x0E, 0x1A, 1, PPC_64B);
1863
/* fctid */
1864
GEN_FLOAT_B(ctid, 0x0E, 0x19, 0, PPC_64B);
1865
/* fctidz */
1866
GEN_FLOAT_B(ctidz, 0x0F, 0x19, 0, PPC_64B);
1867
#endif
1868

    
1869
/* frin */
1870
GEN_FLOAT_B(rin, 0x08, 0x0C, 1, PPC_FLOAT_EXT);
1871
/* friz */
1872
GEN_FLOAT_B(riz, 0x08, 0x0D, 1, PPC_FLOAT_EXT);
1873
/* frip */
1874
GEN_FLOAT_B(rip, 0x08, 0x0E, 1, PPC_FLOAT_EXT);
1875
/* frim */
1876
GEN_FLOAT_B(rim, 0x08, 0x0F, 1, PPC_FLOAT_EXT);
1877

    
1878
/***                         Floating-Point compare                        ***/
1879
/* fcmpo */
1880
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
1881
{
1882
    if (unlikely(!ctx->fpu_enabled)) {
1883
        GEN_EXCP_NO_FP(ctx);
1884
        return;
1885
    }
1886
    gen_op_load_fpr_FT0(rA(ctx->opcode));
1887
    gen_op_load_fpr_FT1(rB(ctx->opcode));
1888
    gen_reset_fpstatus();
1889
    gen_op_fcmpo();
1890
    gen_op_store_T0_crf(crfD(ctx->opcode));
1891
    gen_op_float_check_status();
1892
}
1893

    
1894
/* fcmpu */
1895
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
1896
{
1897
    if (unlikely(!ctx->fpu_enabled)) {
1898
        GEN_EXCP_NO_FP(ctx);
1899
        return;
1900
    }
1901
    gen_op_load_fpr_FT0(rA(ctx->opcode));
1902
    gen_op_load_fpr_FT1(rB(ctx->opcode));
1903
    gen_reset_fpstatus();
1904
    gen_op_fcmpu();
1905
    gen_op_store_T0_crf(crfD(ctx->opcode));
1906
    gen_op_float_check_status();
1907
}
1908

    
1909
/***                         Floating-point move                           ***/
1910
/* fabs */
1911
/* XXX: beware that fabs never checks for NaNs nor update FPSCR */
1912
GEN_FLOAT_B(abs, 0x08, 0x08, 0, PPC_FLOAT);
1913

    
1914
/* fmr  - fmr. */
1915
/* XXX: beware that fmr never checks for NaNs nor update FPSCR */
1916
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
1917
{
1918
    if (unlikely(!ctx->fpu_enabled)) {
1919
        GEN_EXCP_NO_FP(ctx);
1920
        return;
1921
    }
1922
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1923
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1924
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
1925
}
1926

    
1927
/* fnabs */
1928
/* XXX: beware that fnabs never checks for NaNs nor update FPSCR */
1929
GEN_FLOAT_B(nabs, 0x08, 0x04, 0, PPC_FLOAT);
1930
/* fneg */
1931
/* XXX: beware that fneg never checks for NaNs nor update FPSCR */
1932
GEN_FLOAT_B(neg, 0x08, 0x01, 0, PPC_FLOAT);
1933

    
1934
/***                  Floating-Point status & ctrl register                ***/
1935
/* mcrfs */
1936
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
1937
{
1938
    int bfa;
1939

    
1940
    if (unlikely(!ctx->fpu_enabled)) {
1941
        GEN_EXCP_NO_FP(ctx);
1942
        return;
1943
    }
1944
    gen_optimize_fprf();
1945
    bfa = 4 * (7 - crfS(ctx->opcode));
1946
    gen_op_load_fpscr_T0(bfa);
1947
    gen_op_store_T0_crf(crfD(ctx->opcode));
1948
    gen_op_fpscr_resetbit(~(0xF << bfa));
1949
}
1950

    
1951
/* mffs */
1952
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
1953
{
1954
    if (unlikely(!ctx->fpu_enabled)) {
1955
        GEN_EXCP_NO_FP(ctx);
1956
        return;
1957
    }
1958
    gen_optimize_fprf();
1959
    gen_reset_fpstatus();
1960
    gen_op_load_fpscr_FT0();
1961
    gen_op_store_FT0_fpr(rD(ctx->opcode));
1962
    gen_compute_fprf(0, Rc(ctx->opcode) != 0);
1963
}
1964

    
1965
/* mtfsb0 */
1966
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
1967
{
1968
    uint8_t crb;
1969

    
1970
    if (unlikely(!ctx->fpu_enabled)) {
1971
        GEN_EXCP_NO_FP(ctx);
1972
        return;
1973
    }
1974
    crb = 32 - (crbD(ctx->opcode) >> 2);
1975
    gen_optimize_fprf();
1976
    gen_reset_fpstatus();
1977
    if (likely(crb != 30 && crb != 29))
1978
        gen_op_fpscr_resetbit(~(1 << crb));
1979
    if (unlikely(Rc(ctx->opcode) != 0)) {
1980
        gen_op_load_fpcc();
1981
        gen_op_set_Rc0();
1982
    }
1983
}
1984

    
1985
/* mtfsb1 */
1986
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
1987
{
1988
    uint8_t crb;
1989

    
1990
    if (unlikely(!ctx->fpu_enabled)) {
1991
        GEN_EXCP_NO_FP(ctx);
1992
        return;
1993
    }
1994
    crb = 32 - (crbD(ctx->opcode) >> 2);
1995
    gen_optimize_fprf();
1996
    gen_reset_fpstatus();
1997
    /* XXX: we pretend we can only do IEEE floating-point computations */
1998
    if (likely(crb != FPSCR_FEX && crb != FPSCR_VX && crb != FPSCR_NI))
1999
        gen_op_fpscr_setbit(crb);
2000
    if (unlikely(Rc(ctx->opcode) != 0)) {
2001
        gen_op_load_fpcc();
2002
        gen_op_set_Rc0();
2003
    }
2004
    /* We can raise a differed exception */
2005
    gen_op_float_check_status();
2006
}
2007

    
2008
/* mtfsf */
2009
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
2010
{
2011
    if (unlikely(!ctx->fpu_enabled)) {
2012
        GEN_EXCP_NO_FP(ctx);
2013
        return;
2014
    }
2015
    gen_optimize_fprf();
2016
    gen_op_load_fpr_FT0(rB(ctx->opcode));
2017
    gen_reset_fpstatus();
2018
    gen_op_store_fpscr(FM(ctx->opcode));
2019
    if (unlikely(Rc(ctx->opcode) != 0)) {
2020
        gen_op_load_fpcc();
2021
        gen_op_set_Rc0();
2022
    }
2023
    /* We can raise a differed exception */
2024
    gen_op_float_check_status();
2025
}
2026

    
2027
/* mtfsfi */
2028
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
2029
{
2030
    int bf, sh;
2031

    
2032
    if (unlikely(!ctx->fpu_enabled)) {
2033
        GEN_EXCP_NO_FP(ctx);
2034
        return;
2035
    }
2036
    bf = crbD(ctx->opcode) >> 2;
2037
    sh = 7 - bf;
2038
    gen_optimize_fprf();
2039
    gen_op_set_FT0(FPIMM(ctx->opcode) << (4 * sh));
2040
    gen_reset_fpstatus();
2041
    gen_op_store_fpscr(1 << sh);
2042
    if (unlikely(Rc(ctx->opcode) != 0)) {
2043
        gen_op_load_fpcc();
2044
        gen_op_set_Rc0();
2045
    }
2046
    /* We can raise a differed exception */
2047
    gen_op_float_check_status();
2048
}
2049

    
2050
/***                           Addressing modes                            ***/
2051
/* Register indirect with immediate index : EA = (rA|0) + SIMM */
2052
static always_inline void gen_addr_imm_index (DisasContext *ctx,
2053
                                              target_long maskl)
2054
{
2055
    target_long simm = SIMM(ctx->opcode);
2056

    
2057
    simm &= ~maskl;
2058
    if (rA(ctx->opcode) == 0) {
2059
        gen_set_T0(simm);
2060
    } else {
2061
        gen_op_load_gpr_T0(rA(ctx->opcode));
2062
        if (likely(simm != 0))
2063
            gen_op_addi(simm);
2064
    }
2065
#ifdef DEBUG_MEMORY_ACCESSES
2066
    gen_op_print_mem_EA();
2067
#endif
2068
}
2069

    
2070
static always_inline void gen_addr_reg_index (DisasContext *ctx)
2071
{
2072
    if (rA(ctx->opcode) == 0) {
2073
        gen_op_load_gpr_T0(rB(ctx->opcode));
2074
    } else {
2075
        gen_op_load_gpr_T0(rA(ctx->opcode));
2076
        gen_op_load_gpr_T1(rB(ctx->opcode));
2077
        gen_op_add();
2078
    }
2079
#ifdef DEBUG_MEMORY_ACCESSES
2080
    gen_op_print_mem_EA();
2081
#endif
2082
}
2083

    
2084
static always_inline void gen_addr_register (DisasContext *ctx)
2085
{
2086
    if (rA(ctx->opcode) == 0) {
2087
        gen_op_reset_T0();
2088
    } else {
2089
        gen_op_load_gpr_T0(rA(ctx->opcode));
2090
    }
2091
#ifdef DEBUG_MEMORY_ACCESSES
2092
    gen_op_print_mem_EA();
2093
#endif
2094
}
2095

    
2096
/***                             Integer load                              ***/
2097
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
2098
#if defined(CONFIG_USER_ONLY)
2099
#if defined(TARGET_PPC64)
2100
/* User mode only - 64 bits */
2101
#define OP_LD_TABLE(width)                                                    \
2102
static GenOpFunc *gen_op_l##width[] = {                                       \
2103
    &gen_op_l##width##_raw,                                                   \
2104
    &gen_op_l##width##_le_raw,                                                \
2105
    &gen_op_l##width##_64_raw,                                                \
2106
    &gen_op_l##width##_le_64_raw,                                             \
2107
};
2108
#define OP_ST_TABLE(width)                                                    \
2109
static GenOpFunc *gen_op_st##width[] = {                                      \
2110
    &gen_op_st##width##_raw,                                                  \
2111
    &gen_op_st##width##_le_raw,                                               \
2112
    &gen_op_st##width##_64_raw,                                               \
2113
    &gen_op_st##width##_le_64_raw,                                            \
2114
};
2115
/* Byte access routine are endian safe */
2116
#define gen_op_stb_le_64_raw gen_op_stb_64_raw
2117
#define gen_op_lbz_le_64_raw gen_op_lbz_64_raw
2118
#else
2119
/* User mode only - 32 bits */
2120
#define OP_LD_TABLE(width)                                                    \
2121
static GenOpFunc *gen_op_l##width[] = {                                       \
2122
    &gen_op_l##width##_raw,                                                   \
2123
    &gen_op_l##width##_le_raw,                                                \
2124
};
2125
#define OP_ST_TABLE(width)                                                    \
2126
static GenOpFunc *gen_op_st##width[] = {                                      \
2127
    &gen_op_st##width##_raw,                                                  \
2128
    &gen_op_st##width##_le_raw,                                               \
2129
};
2130
#endif
2131
/* Byte access routine are endian safe */
2132
#define gen_op_stb_le_raw gen_op_stb_raw
2133
#define gen_op_lbz_le_raw gen_op_lbz_raw
2134
#else
2135
#if defined(TARGET_PPC64)
2136
#if defined(TARGET_PPC64H)
2137
/* Full system - 64 bits with hypervisor mode */
2138
#define OP_LD_TABLE(width)                                                    \
2139
static GenOpFunc *gen_op_l##width[] = {                                       \
2140
    &gen_op_l##width##_user,                                                  \
2141
    &gen_op_l##width##_le_user,                                               \
2142
    &gen_op_l##width##_64_user,                                               \
2143
    &gen_op_l##width##_le_64_user,                                            \
2144
    &gen_op_l##width##_kernel,                                                \
2145
    &gen_op_l##width##_le_kernel,                                             \
2146
    &gen_op_l##width##_64_kernel,                                             \
2147
    &gen_op_l##width##_le_64_kernel,                                          \
2148
    &gen_op_l##width##_hypv,                                                  \
2149
    &gen_op_l##width##_le_hypv,                                               \
2150
    &gen_op_l##width##_64_hypv,                                               \
2151
    &gen_op_l##width##_le_64_hypv,                                            \
2152
};
2153
#define OP_ST_TABLE(width)                                                    \
2154
static GenOpFunc *gen_op_st##width[] = {                                      \
2155
    &gen_op_st##width##_user,                                                 \
2156
    &gen_op_st##width##_le_user,                                              \
2157
    &gen_op_st##width##_64_user,                                              \
2158
    &gen_op_st##width##_le_64_user,                                           \
2159
    &gen_op_st##width##_kernel,                                               \
2160
    &gen_op_st##width##_le_kernel,                                            \
2161
    &gen_op_st##width##_64_kernel,                                            \
2162
    &gen_op_st##width##_le_64_kernel,                                         \
2163
    &gen_op_st##width##_hypv,                                                 \
2164
    &gen_op_st##width##_le_hypv,                                              \
2165
    &gen_op_st##width##_64_hypv,                                              \
2166
    &gen_op_st##width##_le_64_hypv,                                           \
2167
};
2168
/* Byte access routine are endian safe */
2169
#define gen_op_stb_le_hypv      gen_op_stb_64_hypv
2170
#define gen_op_lbz_le_hypv      gen_op_lbz_64_hypv
2171
#define gen_op_stb_le_64_hypv   gen_op_stb_64_hypv
2172
#define gen_op_lbz_le_64_hypv   gen_op_lbz_64_hypv
2173
#else
2174
/* Full system - 64 bits */
2175
#define OP_LD_TABLE(width)                                                    \
2176
static GenOpFunc *gen_op_l##width[] = {                                       \
2177
    &gen_op_l##width##_user,                                                  \
2178
    &gen_op_l##width##_le_user,                                               \
2179
    &gen_op_l##width##_64_user,                                               \
2180
    &gen_op_l##width##_le_64_user,                                            \
2181
    &gen_op_l##width##_kernel,                                                \
2182
    &gen_op_l##width##_le_kernel,                                             \
2183
    &gen_op_l##width##_64_kernel,                                             \
2184
    &gen_op_l##width##_le_64_kernel,                                          \
2185
};
2186
#define OP_ST_TABLE(width)                                                    \
2187
static GenOpFunc *gen_op_st##width[] = {                                      \
2188
    &gen_op_st##width##_user,                                                 \
2189
    &gen_op_st##width##_le_user,                                              \
2190
    &gen_op_st##width##_64_user,                                              \
2191
    &gen_op_st##width##_le_64_user,                                           \
2192
    &gen_op_st##width##_kernel,                                               \
2193
    &gen_op_st##width##_le_kernel,                                            \
2194
    &gen_op_st##width##_64_kernel,                                            \
2195
    &gen_op_st##width##_le_64_kernel,                                         \
2196
};
2197
#endif
2198
/* Byte access routine are endian safe */
2199
#define gen_op_stb_le_64_user   gen_op_stb_64_user
2200
#define gen_op_lbz_le_64_user   gen_op_lbz_64_user
2201
#define gen_op_stb_le_64_kernel gen_op_stb_64_kernel
2202
#define gen_op_lbz_le_64_kernel gen_op_lbz_64_kernel
2203
#else
2204
/* Full system - 32 bits */
2205
#define OP_LD_TABLE(width)                                                    \
2206
static GenOpFunc *gen_op_l##width[] = {                                       \
2207
    &gen_op_l##width##_user,                                                  \
2208
    &gen_op_l##width##_le_user,                                               \
2209
    &gen_op_l##width##_kernel,                                                \
2210
    &gen_op_l##width##_le_kernel,                                             \
2211
};
2212
#define OP_ST_TABLE(width)                                                    \
2213
static GenOpFunc *gen_op_st##width[] = {                                      \
2214
    &gen_op_st##width##_user,                                                 \
2215
    &gen_op_st##width##_le_user,                                              \
2216
    &gen_op_st##width##_kernel,                                               \
2217
    &gen_op_st##width##_le_kernel,                                            \
2218
};
2219
#endif
2220
/* Byte access routine are endian safe */
2221
#define gen_op_stb_le_user   gen_op_stb_user
2222
#define gen_op_lbz_le_user   gen_op_lbz_user
2223
#define gen_op_stb_le_kernel gen_op_stb_kernel
2224
#define gen_op_lbz_le_kernel gen_op_lbz_kernel
2225
#endif
2226

    
2227
#define GEN_LD(width, opc, type)                                              \
2228
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
2229
{                                                                             \
2230
    gen_addr_imm_index(ctx, 0);                                               \
2231
    op_ldst(l##width);                                                        \
2232
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
2233
}
2234

    
2235
#define GEN_LDU(width, opc, type)                                             \
2236
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
2237
{                                                                             \
2238
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2239
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2240
        GEN_EXCP_INVAL(ctx);                                                  \
2241
        return;                                                               \
2242
    }                                                                         \
2243
    if (type == PPC_64B)                                                      \
2244
        gen_addr_imm_index(ctx, 0x03);                                        \
2245
    else                                                                      \
2246
        gen_addr_imm_index(ctx, 0);                                           \
2247
    op_ldst(l##width);                                                        \
2248
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
2249
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
2250
}
2251

    
2252
#define GEN_LDUX(width, opc2, opc3, type)                                     \
2253
GEN_HANDLER(l##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                 \
2254
{                                                                             \
2255
    if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2256
                 rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2257
        GEN_EXCP_INVAL(ctx);                                                  \
2258
        return;                                                               \
2259
    }                                                                         \
2260
    gen_addr_reg_index(ctx);                                                  \
2261
    op_ldst(l##width);                                                        \
2262
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
2263
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
2264
}
2265

    
2266
#define GEN_LDX(width, opc2, opc3, type)                                      \
2267
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
2268
{                                                                             \
2269
    gen_addr_reg_index(ctx);                                                  \
2270
    op_ldst(l##width);                                                        \
2271
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
2272
}
2273

    
2274
#define GEN_LDS(width, op, type)                                              \
2275
OP_LD_TABLE(width);                                                           \
2276
GEN_LD(width, op | 0x20, type);                                               \
2277
GEN_LDU(width, op | 0x21, type);                                              \
2278
GEN_LDUX(width, 0x17, op | 0x01, type);                                       \
2279
GEN_LDX(width, 0x17, op | 0x00, type)
2280

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

    
2329
    /* Restore CPU state */
2330
    if (unlikely(ctx->supervisor == 0)) {
2331
        GEN_EXCP_PRIVOPC(ctx);
2332
        return;
2333
    }
2334
    ra = rA(ctx->opcode);
2335
    rd = rD(ctx->opcode);
2336
    if (unlikely((rd & 1) || rd == ra)) {
2337
        GEN_EXCP_INVAL(ctx);
2338
        return;
2339
    }
2340
    if (unlikely(ctx->mem_idx & 1)) {
2341
        /* Little-endian mode is not handled */
2342
        GEN_EXCP(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_LE);
2343
        return;
2344
    }
2345
    gen_addr_imm_index(ctx, 0x0F);
2346
    op_ldst(ld);
2347
    gen_op_store_T1_gpr(rd);
2348
    gen_op_addi(8);
2349
    op_ldst(ld);
2350
    gen_op_store_T1_gpr(rd + 1);
2351
#endif
2352
}
2353
#endif
2354

    
2355
/***                              Integer store                            ***/
2356
#define GEN_ST(width, opc, type)                                              \
2357
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
2358
{                                                                             \
2359
    gen_addr_imm_index(ctx, 0);                                               \
2360
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2361
    op_ldst(st##width);                                                       \
2362
}
2363

    
2364
#define GEN_STU(width, opc, type)                                             \
2365
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
2366
{                                                                             \
2367
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2368
        GEN_EXCP_INVAL(ctx);                                                  \
2369
        return;                                                               \
2370
    }                                                                         \
2371
    if (type == PPC_64B)                                                      \
2372
        gen_addr_imm_index(ctx, 0x03);                                        \
2373
    else                                                                      \
2374
        gen_addr_imm_index(ctx, 0);                                           \
2375
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2376
    op_ldst(st##width);                                                       \
2377
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
2378
}
2379

    
2380
#define GEN_STUX(width, opc2, opc3, type)                                     \
2381
GEN_HANDLER(st##width##ux, 0x1F, opc2, opc3, 0x00000001, type)                \
2382
{                                                                             \
2383
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2384
        GEN_EXCP_INVAL(ctx);                                                  \
2385
        return;                                                               \
2386
    }                                                                         \
2387
    gen_addr_reg_index(ctx);                                                  \
2388
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2389
    op_ldst(st##width);                                                       \
2390
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
2391
}
2392

    
2393
#define GEN_STX(width, opc2, opc3, type)                                      \
2394
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
2395
{                                                                             \
2396
    gen_addr_reg_index(ctx);                                                  \
2397
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
2398
    op_ldst(st##width);                                                       \
2399
}
2400

    
2401
#define GEN_STS(width, op, type)                                              \
2402
OP_ST_TABLE(width);                                                           \
2403
GEN_ST(width, op | 0x20, type);                                               \
2404
GEN_STU(width, op | 0x21, type);                                              \
2405
GEN_STUX(width, 0x17, op | 0x01, type);                                       \
2406
GEN_STX(width, 0x17, op | 0x00, type)
2407

    
2408
/* stb stbu stbux stbx */
2409
GEN_STS(b, 0x06, PPC_INTEGER);
2410
/* sth sthu sthux sthx */
2411
GEN_STS(h, 0x0C, PPC_INTEGER);
2412
/* stw stwu stwux stwx */
2413
GEN_STS(w, 0x04, PPC_INTEGER);
2414
#if defined(TARGET_PPC64)
2415
OP_ST_TABLE(d);
2416
GEN_STUX(d, 0x15, 0x05, PPC_64B);
2417
GEN_STX(d, 0x15, 0x04, PPC_64B);
2418
GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B)
2419
{
2420
    int rs;
2421

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

    
2478
/***                    Integer load and store multiple                    ***/
2479
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
2480
#if defined(CONFIG_USER_ONLY)
2481
/* User-mode only */
2482
static GenOpFunc1 *gen_op_lmw[] = {
2483
    &gen_op_lmw_raw,
2484
    &gen_op_lmw_le_raw,
2485
#if defined(TARGET_PPC64)
2486
    &gen_op_lmw_64_raw,
2487
    &gen_op_lmw_le_64_raw,
2488
#endif
2489
};
2490
static GenOpFunc1 *gen_op_stmw[] = {
2491
    &gen_op_stmw_raw,
2492
    &gen_op_stmw_le_raw,
2493
#if defined(TARGET_PPC64)
2494
    &gen_op_stmw_64_raw,
2495
    &gen_op_stmw_le_64_raw,
2496
#endif
2497
};
2498
#else
2499
#if defined(TARGET_PPC64)
2500
/* Full system - 64 bits mode */
2501
static GenOpFunc1 *gen_op_lmw[] = {
2502
    &gen_op_lmw_user,
2503
    &gen_op_lmw_le_user,
2504
    &gen_op_lmw_64_user,
2505
    &gen_op_lmw_le_64_user,
2506
    &gen_op_lmw_kernel,
2507
    &gen_op_lmw_le_kernel,
2508
    &gen_op_lmw_64_kernel,
2509
    &gen_op_lmw_le_64_kernel,
2510
#if defined(TARGET_PPC64H)
2511
    &gen_op_lmw_hypv,
2512
    &gen_op_lmw_le_hypv,
2513
    &gen_op_lmw_64_hypv,
2514
    &gen_op_lmw_le_64_hypv,
2515
#endif
2516
};
2517
static GenOpFunc1 *gen_op_stmw[] = {
2518
    &gen_op_stmw_user,
2519
    &gen_op_stmw_le_user,
2520
    &gen_op_stmw_64_user,
2521
    &gen_op_stmw_le_64_user,
2522
    &gen_op_stmw_kernel,
2523
    &gen_op_stmw_le_kernel,
2524
    &gen_op_stmw_64_kernel,
2525
    &gen_op_stmw_le_64_kernel,
2526
#if defined(TARGET_PPC64H)
2527
    &gen_op_stmw_hypv,
2528
    &gen_op_stmw_le_hypv,
2529
    &gen_op_stmw_64_hypv,
2530
    &gen_op_stmw_le_64_hypv,
2531
#endif
2532
};
2533
#else
2534
/* Full system - 32 bits mode */
2535
static GenOpFunc1 *gen_op_lmw[] = {
2536
    &gen_op_lmw_user,
2537
    &gen_op_lmw_le_user,
2538
    &gen_op_lmw_kernel,
2539
    &gen_op_lmw_le_kernel,
2540
};
2541
static GenOpFunc1 *gen_op_stmw[] = {
2542
    &gen_op_stmw_user,
2543
    &gen_op_stmw_le_user,
2544
    &gen_op_stmw_kernel,
2545
    &gen_op_stmw_le_kernel,
2546
};
2547
#endif
2548
#endif
2549

    
2550
/* lmw */
2551
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
2552
{
2553
    /* NIP cannot be restored if the memory exception comes from an helper */
2554
    gen_update_nip(ctx, ctx->nip - 4);
2555
    gen_addr_imm_index(ctx, 0);
2556
    op_ldstm(lmw, rD(ctx->opcode));
2557
}
2558

    
2559
/* stmw */
2560
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
2561
{
2562
    /* NIP cannot be restored if the memory exception comes from an helper */
2563
    gen_update_nip(ctx, ctx->nip - 4);
2564
    gen_addr_imm_index(ctx, 0);
2565
    op_ldstm(stmw, rS(ctx->opcode));
2566
}
2567

    
2568
/***                    Integer load and store strings                     ***/
2569
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
2570
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
2571
#if defined(CONFIG_USER_ONLY)
2572
/* User-mode only */
2573
static GenOpFunc1 *gen_op_lswi[] = {
2574
    &gen_op_lswi_raw,
2575
    &gen_op_lswi_le_raw,
2576
#if defined(TARGET_PPC64)
2577
    &gen_op_lswi_64_raw,
2578
    &gen_op_lswi_le_64_raw,
2579
#endif
2580
};
2581
static GenOpFunc3 *gen_op_lswx[] = {
2582
    &gen_op_lswx_raw,
2583
    &gen_op_lswx_le_raw,
2584
#if defined(TARGET_PPC64)
2585
    &gen_op_lswx_64_raw,
2586
    &gen_op_lswx_le_64_raw,
2587
#endif
2588
};
2589
static GenOpFunc1 *gen_op_stsw[] = {
2590
    &gen_op_stsw_raw,
2591
    &gen_op_stsw_le_raw,
2592
#if defined(TARGET_PPC64)
2593
    &gen_op_stsw_64_raw,
2594
    &gen_op_stsw_le_64_raw,
2595
#endif
2596
};
2597
#else
2598
#if defined(TARGET_PPC64)
2599
/* Full system - 64 bits mode */
2600
static GenOpFunc1 *gen_op_lswi[] = {
2601
    &gen_op_lswi_user,
2602
    &gen_op_lswi_le_user,
2603
    &gen_op_lswi_64_user,
2604
    &gen_op_lswi_le_64_user,
2605
    &gen_op_lswi_kernel,
2606
    &gen_op_lswi_le_kernel,
2607
    &gen_op_lswi_64_kernel,
2608
    &gen_op_lswi_le_64_kernel,
2609
#if defined(TARGET_PPC64H)
2610
    &gen_op_lswi_hypv,
2611
    &gen_op_lswi_le_hypv,
2612
    &gen_op_lswi_64_hypv,
2613
    &gen_op_lswi_le_64_hypv,
2614
#endif
2615
};
2616
static GenOpFunc3 *gen_op_lswx[] = {
2617
    &gen_op_lswx_user,
2618
    &gen_op_lswx_le_user,
2619
    &gen_op_lswx_64_user,
2620
    &gen_op_lswx_le_64_user,
2621
    &gen_op_lswx_kernel,
2622
    &gen_op_lswx_le_kernel,
2623
    &gen_op_lswx_64_kernel,
2624
    &gen_op_lswx_le_64_kernel,
2625
#if defined(TARGET_PPC64H)
2626
    &gen_op_lswx_hypv,
2627
    &gen_op_lswx_le_hypv,
2628
    &gen_op_lswx_64_hypv,
2629
    &gen_op_lswx_le_64_hypv,
2630
#endif
2631
};
2632
static GenOpFunc1 *gen_op_stsw[] = {
2633
    &gen_op_stsw_user,
2634
    &gen_op_stsw_le_user,
2635
    &gen_op_stsw_64_user,
2636
    &gen_op_stsw_le_64_user,
2637
    &gen_op_stsw_kernel,
2638
    &gen_op_stsw_le_kernel,
2639
    &gen_op_stsw_64_kernel,
2640
    &gen_op_stsw_le_64_kernel,
2641
#if defined(TARGET_PPC64H)
2642
    &gen_op_stsw_hypv,
2643
    &gen_op_stsw_le_hypv,
2644
    &gen_op_stsw_64_hypv,
2645
    &gen_op_stsw_le_64_hypv,
2646
#endif
2647
};
2648
#else
2649
/* Full system - 32 bits mode */
2650
static GenOpFunc1 *gen_op_lswi[] = {
2651
    &gen_op_lswi_user,
2652
    &gen_op_lswi_le_user,
2653
    &gen_op_lswi_kernel,
2654
    &gen_op_lswi_le_kernel,
2655
};
2656
static GenOpFunc3 *gen_op_lswx[] = {
2657
    &gen_op_lswx_user,
2658
    &gen_op_lswx_le_user,
2659
    &gen_op_lswx_kernel,
2660
    &gen_op_lswx_le_kernel,
2661
};
2662
static GenOpFunc1 *gen_op_stsw[] = {
2663
    &gen_op_stsw_user,
2664
    &gen_op_stsw_le_user,
2665
    &gen_op_stsw_kernel,
2666
    &gen_op_stsw_le_kernel,
2667
};
2668
#endif
2669
#endif
2670

    
2671
/* lswi */
2672
/* PowerPC32 specification says we must generate an exception if
2673
 * rA is in the range of registers to be loaded.
2674
 * In an other hand, IBM says this is valid, but rA won't be loaded.
2675
 * For now, I'll follow the spec...
2676
 */
2677
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
2678
{
2679
    int nb = NB(ctx->opcode);
2680
    int start = rD(ctx->opcode);
2681
    int ra = rA(ctx->opcode);
2682
    int nr;
2683

    
2684
    if (nb == 0)
2685
        nb = 32;
2686
    nr = nb / 4;
2687
    if (unlikely(((start + nr) > 32  &&
2688
                  start <= ra && (start + nr - 32) > ra) ||
2689
                 ((start + nr) <= 32 && start <= ra && (start + nr) > ra))) {
2690
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
2691
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_LSWX);
2692
        return;
2693
    }
2694
    /* NIP cannot be restored if the memory exception comes from an helper */
2695
    gen_update_nip(ctx, ctx->nip - 4);
2696
    gen_addr_register(ctx);
2697
    gen_op_set_T1(nb);
2698
    op_ldsts(lswi, start);
2699
}
2700

    
2701
/* lswx */
2702
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
2703
{
2704
    int ra = rA(ctx->opcode);
2705
    int rb = rB(ctx->opcode);
2706

    
2707
    /* NIP cannot be restored if the memory exception comes from an helper */
2708
    gen_update_nip(ctx, ctx->nip - 4);
2709
    gen_addr_reg_index(ctx);
2710
    if (ra == 0) {
2711
        ra = rb;
2712
    }
2713
    gen_op_load_xer_bc();
2714
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
2715
}
2716

    
2717
/* stswi */
2718
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
2719
{
2720
    int nb = NB(ctx->opcode);
2721

    
2722
    /* NIP cannot be restored if the memory exception comes from an helper */
2723
    gen_update_nip(ctx, ctx->nip - 4);
2724
    gen_addr_register(ctx);
2725
    if (nb == 0)
2726
        nb = 32;
2727
    gen_op_set_T1(nb);
2728
    op_ldsts(stsw, rS(ctx->opcode));
2729
}
2730

    
2731
/* stswx */
2732
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
2733
{
2734
    /* NIP cannot be restored if the memory exception comes from an helper */
2735
    gen_update_nip(ctx, ctx->nip - 4);
2736
    gen_addr_reg_index(ctx);
2737
    gen_op_load_xer_bc();
2738
    op_ldsts(stsw, rS(ctx->opcode));
2739
}
2740

    
2741
/***                        Memory synchronisation                         ***/
2742
/* eieio */
2743
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FFF801, PPC_MEM_EIEIO)
2744
{
2745
}
2746

    
2747
/* isync */
2748
GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM)
2749
{
2750
    GEN_STOP(ctx);
2751
}
2752

    
2753
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
2754
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
2755
#if defined(CONFIG_USER_ONLY)
2756
/* User-mode only */
2757
static GenOpFunc *gen_op_lwarx[] = {
2758
    &gen_op_lwarx_raw,
2759
    &gen_op_lwarx_le_raw,
2760
#if defined(TARGET_PPC64)
2761
    &gen_op_lwarx_64_raw,
2762
    &gen_op_lwarx_le_64_raw,
2763
#endif
2764
};
2765
static GenOpFunc *gen_op_stwcx[] = {
2766
    &gen_op_stwcx_raw,
2767
    &gen_op_stwcx_le_raw,
2768
#if defined(TARGET_PPC64)
2769
    &gen_op_stwcx_64_raw,
2770
    &gen_op_stwcx_le_64_raw,
2771
#endif
2772
};
2773
#else
2774
#if defined(TARGET_PPC64)
2775
/* Full system - 64 bits mode */
2776
static GenOpFunc *gen_op_lwarx[] = {
2777
    &gen_op_lwarx_user,
2778
    &gen_op_lwarx_le_user,
2779
    &gen_op_lwarx_64_user,
2780
    &gen_op_lwarx_le_64_user,
2781
    &gen_op_lwarx_kernel,
2782
    &gen_op_lwarx_le_kernel,
2783
    &gen_op_lwarx_64_kernel,
2784
    &gen_op_lwarx_le_64_kernel,
2785
#if defined(TARGET_PPC64H)
2786
    &gen_op_lwarx_hypv,
2787
    &gen_op_lwarx_le_hypv,
2788
    &gen_op_lwarx_64_hypv,
2789
    &gen_op_lwarx_le_64_hypv,
2790
#endif
2791
};
2792
static GenOpFunc *gen_op_stwcx[] = {
2793
    &gen_op_stwcx_user,
2794
    &gen_op_stwcx_le_user,
2795
    &gen_op_stwcx_64_user,
2796
    &gen_op_stwcx_le_64_user,
2797
    &gen_op_stwcx_kernel,
2798
    &gen_op_stwcx_le_kernel,
2799
    &gen_op_stwcx_64_kernel,
2800
    &gen_op_stwcx_le_64_kernel,
2801
#if defined(TARGET_PPC64H)
2802
    &gen_op_stwcx_hypv,
2803
    &gen_op_stwcx_le_hypv,
2804
    &gen_op_stwcx_64_hypv,
2805
    &gen_op_stwcx_le_64_hypv,
2806
#endif
2807
};
2808
#else
2809
/* Full system - 32 bits mode */
2810
static GenOpFunc *gen_op_lwarx[] = {
2811
    &gen_op_lwarx_user,
2812
    &gen_op_lwarx_le_user,
2813
    &gen_op_lwarx_kernel,
2814
    &gen_op_lwarx_le_kernel,
2815
};
2816
static GenOpFunc *gen_op_stwcx[] = {
2817
    &gen_op_stwcx_user,
2818
    &gen_op_stwcx_le_user,
2819
    &gen_op_stwcx_kernel,
2820
    &gen_op_stwcx_le_kernel,
2821
};
2822
#endif
2823
#endif
2824

    
2825
/* lwarx */
2826
GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000001, PPC_RES)
2827
{
2828
    /* NIP cannot be restored if the memory exception comes from an helper */
2829
    gen_update_nip(ctx, ctx->nip - 4);
2830
    gen_addr_reg_index(ctx);
2831
    op_lwarx();
2832
    gen_op_store_T1_gpr(rD(ctx->opcode));
2833
}
2834

    
2835
/* stwcx. */
2836
GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
2837
{
2838
    /* NIP cannot be restored if the memory exception comes from an helper */
2839
    gen_update_nip(ctx, ctx->nip - 4);
2840
    gen_addr_reg_index(ctx);
2841
    gen_op_load_gpr_T1(rS(ctx->opcode));
2842
    op_stwcx();
2843
}
2844

    
2845
#if defined(TARGET_PPC64)
2846
#define op_ldarx() (*gen_op_ldarx[ctx->mem_idx])()
2847
#define op_stdcx() (*gen_op_stdcx[ctx->mem_idx])()
2848
#if defined(CONFIG_USER_ONLY)
2849
/* User-mode only */
2850
static GenOpFunc *gen_op_ldarx[] = {
2851
    &gen_op_ldarx_raw,
2852
    &gen_op_ldarx_le_raw,
2853
    &gen_op_ldarx_64_raw,
2854
    &gen_op_ldarx_le_64_raw,
2855
};
2856
static GenOpFunc *gen_op_stdcx[] = {
2857
    &gen_op_stdcx_raw,
2858
    &gen_op_stdcx_le_raw,
2859
    &gen_op_stdcx_64_raw,
2860
    &gen_op_stdcx_le_64_raw,
2861
};
2862
#else
2863
/* Full system */
2864
static GenOpFunc *gen_op_ldarx[] = {
2865
    &gen_op_ldarx_user,
2866
    &gen_op_ldarx_le_user,
2867
    &gen_op_ldarx_64_user,
2868
    &gen_op_ldarx_le_64_user,
2869
    &gen_op_ldarx_kernel,
2870
    &gen_op_ldarx_le_kernel,
2871
    &gen_op_ldarx_64_kernel,
2872
    &gen_op_ldarx_le_64_kernel,
2873
#if defined(TARGET_PPC64H)
2874
    &gen_op_ldarx_hypv,
2875
    &gen_op_ldarx_le_hypv,
2876
    &gen_op_ldarx_64_hypv,
2877
    &gen_op_ldarx_le_64_hypv,
2878
#endif
2879
};
2880
static GenOpFunc *gen_op_stdcx[] = {
2881
    &gen_op_stdcx_user,
2882
    &gen_op_stdcx_le_user,
2883
    &gen_op_stdcx_64_user,
2884
    &gen_op_stdcx_le_64_user,
2885
    &gen_op_stdcx_kernel,
2886
    &gen_op_stdcx_le_kernel,
2887
    &gen_op_stdcx_64_kernel,
2888
    &gen_op_stdcx_le_64_kernel,
2889
#if defined(TARGET_PPC64H)
2890
    &gen_op_stdcx_hypv,
2891
    &gen_op_stdcx_le_hypv,
2892
    &gen_op_stdcx_64_hypv,
2893
    &gen_op_stdcx_le_64_hypv,
2894
#endif
2895
};
2896
#endif
2897

    
2898
/* ldarx */
2899
GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000001, PPC_64B)
2900
{
2901
    /* NIP cannot be restored if the memory exception comes from an helper */
2902
    gen_update_nip(ctx, ctx->nip - 4);
2903
    gen_addr_reg_index(ctx);
2904
    op_ldarx();
2905
    gen_op_store_T1_gpr(rD(ctx->opcode));
2906
}
2907

    
2908
/* stdcx. */
2909
GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B)
2910
{
2911
    /* NIP cannot be restored if the memory exception comes from an helper */
2912
    gen_update_nip(ctx, ctx->nip - 4);
2913
    gen_addr_reg_index(ctx);
2914
    gen_op_load_gpr_T1(rS(ctx->opcode));
2915
    op_stdcx();
2916
}
2917
#endif /* defined(TARGET_PPC64) */
2918

    
2919
/* sync */
2920
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC)
2921
{
2922
}
2923

    
2924
/* wait */
2925
GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT)
2926
{
2927
    /* Stop translation, as the CPU is supposed to sleep from now */
2928
    gen_op_wait();
2929
    GEN_EXCP(ctx, EXCP_HLT, 1);
2930
}
2931

    
2932
/***                         Floating-point load                           ***/
2933
#define GEN_LDF(width, opc, type)                                             \
2934
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, type)                      \
2935
{                                                                             \
2936
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2937
        GEN_EXCP_NO_FP(ctx);                                                  \
2938
        return;                                                               \
2939
    }                                                                         \
2940
    gen_addr_imm_index(ctx, 0);                                               \
2941
    op_ldst(l##width);                                                        \
2942
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
2943
}
2944

    
2945
#define GEN_LDUF(width, opc, type)                                            \
2946
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                   \
2947
{                                                                             \
2948
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2949
        GEN_EXCP_NO_FP(ctx);                                                  \
2950
        return;                                                               \
2951
    }                                                                         \
2952
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2953
        GEN_EXCP_INVAL(ctx);                                                  \
2954
        return;                                                               \
2955
    }                                                                         \
2956
    gen_addr_imm_index(ctx, 0);                                               \
2957
    op_ldst(l##width);                                                        \
2958
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
2959
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
2960
}
2961

    
2962
#define GEN_LDUXF(width, opc, type)                                           \
2963
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                  \
2964
{                                                                             \
2965
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2966
        GEN_EXCP_NO_FP(ctx);                                                  \
2967
        return;                                                               \
2968
    }                                                                         \
2969
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2970
        GEN_EXCP_INVAL(ctx);                                                  \
2971
        return;                                                               \
2972
    }                                                                         \
2973
    gen_addr_reg_index(ctx);                                                  \
2974
    op_ldst(l##width);                                                        \
2975
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
2976
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
2977
}
2978

    
2979
#define GEN_LDXF(width, opc2, opc3, type)                                     \
2980
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, type)                  \
2981
{                                                                             \
2982
    if (unlikely(!ctx->fpu_enabled)) {                                        \
2983
        GEN_EXCP_NO_FP(ctx);                                                  \
2984
        return;                                                               \
2985
    }                                                                         \
2986
    gen_addr_reg_index(ctx);                                                  \
2987
    op_ldst(l##width);                                                        \
2988
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
2989
}
2990

    
2991
#define GEN_LDFS(width, op, type)                                             \
2992
OP_LD_TABLE(width);                                                           \
2993
GEN_LDF(width, op | 0x20, type);                                              \
2994
GEN_LDUF(width, op | 0x21, type);                                             \
2995
GEN_LDUXF(width, op | 0x01, type);                                            \
2996
GEN_LDXF(width, 0x17, op | 0x00, type)
2997

    
2998
/* lfd lfdu lfdux lfdx */
2999
GEN_LDFS(fd, 0x12, PPC_FLOAT);
3000
/* lfs lfsu lfsux lfsx */
3001
GEN_LDFS(fs, 0x10, PPC_FLOAT);
3002

    
3003
/***                         Floating-point store                          ***/
3004
#define GEN_STF(width, opc, type)                                             \
3005
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, type)                     \
3006
{                                                                             \
3007
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3008
        GEN_EXCP_NO_FP(ctx);                                                  \
3009
        return;                                                               \
3010
    }                                                                         \
3011
    gen_addr_imm_index(ctx, 0);                                               \
3012
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
3013
    op_ldst(st##width);                                                       \
3014
}
3015

    
3016
#define GEN_STUF(width, opc, type)                                            \
3017
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, type)                  \
3018
{                                                                             \
3019
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3020
        GEN_EXCP_NO_FP(ctx);                                                  \
3021
        return;                                                               \
3022
    }                                                                         \
3023
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3024
        GEN_EXCP_INVAL(ctx);                                                  \
3025
        return;                                                               \
3026
    }                                                                         \
3027
    gen_addr_imm_index(ctx, 0);                                               \
3028
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
3029
    op_ldst(st##width);                                                       \
3030
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
3031
}
3032

    
3033
#define GEN_STUXF(width, opc, type)                                           \
3034
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, type)                 \
3035
{                                                                             \
3036
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3037
        GEN_EXCP_NO_FP(ctx);                                                  \
3038
        return;                                                               \
3039
    }                                                                         \
3040
    if (unlikely(rA(ctx->opcode) == 0)) {                                     \
3041
        GEN_EXCP_INVAL(ctx);                                                  \
3042
        return;                                                               \
3043
    }                                                                         \
3044
    gen_addr_reg_index(ctx);                                                  \
3045
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
3046
    op_ldst(st##width);                                                       \
3047
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
3048
}
3049

    
3050
#define GEN_STXF(width, opc2, opc3, type)                                     \
3051
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, type)                 \
3052
{                                                                             \
3053
    if (unlikely(!ctx->fpu_enabled)) {                                        \
3054
        GEN_EXCP_NO_FP(ctx);                                                  \
3055
        return;                                                               \
3056
    }                                                                         \
3057
    gen_addr_reg_index(ctx);                                                  \
3058
    gen_op_load_fpr_FT0(rS(ctx->opcode));                                     \
3059
    op_ldst(st##width);                                                       \
3060
}
3061

    
3062
#define GEN_STFS(width, op, type)                                             \
3063
OP_ST_TABLE(width);                                                           \
3064
GEN_STF(width, op | 0x20, type);                                              \
3065
GEN_STUF(width, op | 0x21, type);                                             \
3066
GEN_STUXF(width, op | 0x01, type);                                            \
3067
GEN_STXF(width, 0x17, op | 0x00, type)
3068

    
3069
/* stfd stfdu stfdux stfdx */
3070
GEN_STFS(fd, 0x16, PPC_FLOAT);
3071
/* stfs stfsu stfsux stfsx */
3072
GEN_STFS(fs, 0x14, PPC_FLOAT);
3073

    
3074
/* Optional: */
3075
/* stfiwx */
3076
OP_ST_TABLE(fiwx);
3077
GEN_STXF(fiwx, 0x17, 0x1E, PPC_FLOAT_STFIWX);
3078

    
3079
/***                                Branch                                 ***/
3080
static always_inline void gen_goto_tb (DisasContext *ctx, int n,
3081
                                       target_ulong dest)
3082
{
3083
    TranslationBlock *tb;
3084
    tb = ctx->tb;
3085
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
3086
        if (n == 0)
3087
            gen_op_goto_tb0(TBPARAM(tb));
3088
        else
3089
            gen_op_goto_tb1(TBPARAM(tb));
3090
        gen_set_T1(dest);
3091
#if defined(TARGET_PPC64)
3092
        if (ctx->sf_mode)
3093
            gen_op_b_T1_64();
3094
        else
3095
#endif
3096
            gen_op_b_T1();
3097
        gen_op_set_T0((long)tb + n);
3098
        if (ctx->singlestep_enabled)
3099
            gen_op_debug();
3100
        gen_op_exit_tb();
3101
    } else {
3102
        gen_set_T1(dest);
3103
#if defined(TARGET_PPC64)
3104
        if (ctx->sf_mode)
3105
            gen_op_b_T1_64();
3106
        else
3107
#endif
3108
            gen_op_b_T1();
3109
        gen_op_reset_T0();
3110
        if (ctx->singlestep_enabled)
3111
            gen_op_debug();
3112
        gen_op_exit_tb();
3113
    }
3114
}
3115

    
3116
static always_inline void gen_setlr (DisasContext *ctx, target_ulong nip)
3117
{
3118
#if defined(TARGET_PPC64)
3119
    if (ctx->sf_mode != 0 && (nip >> 32))
3120
        gen_op_setlr_64(ctx->nip >> 32, ctx->nip);
3121
    else
3122
#endif
3123
        gen_op_setlr(ctx->nip);
3124
}
3125

    
3126
/* b ba bl bla */
3127
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3128
{
3129
    target_ulong li, target;
3130

    
3131
    /* sign extend LI */
3132
#if defined(TARGET_PPC64)
3133
    if (ctx->sf_mode)
3134
        li = ((int64_t)LI(ctx->opcode) << 38) >> 38;
3135
    else
3136
#endif
3137
        li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
3138
    if (likely(AA(ctx->opcode) == 0))
3139
        target = ctx->nip + li - 4;
3140
    else
3141
        target = li;
3142
#if defined(TARGET_PPC64)
3143
    if (!ctx->sf_mode)
3144
        target = (uint32_t)target;
3145
#endif
3146
    if (LK(ctx->opcode))
3147
        gen_setlr(ctx, ctx->nip);
3148
    gen_goto_tb(ctx, 0, target);
3149
    ctx->exception = POWERPC_EXCP_BRANCH;
3150
}
3151

    
3152
#define BCOND_IM  0
3153
#define BCOND_LR  1
3154
#define BCOND_CTR 2
3155

    
3156
static always_inline void gen_bcond (DisasContext *ctx, int type)
3157
{
3158
    target_ulong target = 0;
3159
    target_ulong li;
3160
    uint32_t bo = BO(ctx->opcode);
3161
    uint32_t bi = BI(ctx->opcode);
3162
    uint32_t mask;
3163

    
3164
    if ((bo & 0x4) == 0)
3165
        gen_op_dec_ctr();
3166
    switch(type) {
3167
    case BCOND_IM:
3168
        li = (target_long)((int16_t)(BD(ctx->opcode)));
3169
        if (likely(AA(ctx->opcode) == 0)) {
3170
            target = ctx->nip + li - 4;
3171
        } else {
3172
            target = li;
3173
        }
3174
#if defined(TARGET_PPC64)
3175
        if (!ctx->sf_mode)
3176
            target = (uint32_t)target;
3177
#endif
3178
        break;
3179
    case BCOND_CTR:
3180
        gen_op_movl_T1_ctr();
3181
        break;
3182
    default:
3183
    case BCOND_LR:
3184
        gen_op_movl_T1_lr();
3185
        break;
3186
    }
3187
    if (LK(ctx->opcode))
3188
        gen_setlr(ctx, ctx->nip);
3189
    if (bo & 0x10) {
3190
        /* No CR condition */
3191
        switch (bo & 0x6) {
3192
        case 0:
3193
#if defined(TARGET_PPC64)
3194
            if (ctx->sf_mode)
3195
                gen_op_test_ctr_64();
3196
            else
3197
#endif
3198
                gen_op_test_ctr();
3199
            break;
3200
        case 2:
3201
#if defined(TARGET_PPC64)
3202
            if (ctx->sf_mode)
3203
                gen_op_test_ctrz_64();
3204
            else
3205
#endif
3206
                gen_op_test_ctrz();
3207
            break;
3208
        default:
3209
        case 4:
3210
        case 6:
3211
            if (type == BCOND_IM) {
3212
                gen_goto_tb(ctx, 0, target);
3213
                goto out;
3214
            } else {
3215
#if defined(TARGET_PPC64)
3216
                if (ctx->sf_mode)
3217
                    gen_op_b_T1_64();
3218
                else
3219
#endif
3220
                    gen_op_b_T1();
3221
                gen_op_reset_T0();
3222
                goto no_test;
3223
            }
3224
            break;
3225
        }
3226
    } else {
3227
        mask = 1 << (3 - (bi & 0x03));
3228
        gen_op_load_crf_T0(bi >> 2);
3229
        if (bo & 0x8) {
3230
            switch (bo & 0x6) {
3231
            case 0:
3232
#if defined(TARGET_PPC64)
3233
                if (ctx->sf_mode)
3234
                    gen_op_test_ctr_true_64(mask);
3235
                else
3236
#endif
3237
                    gen_op_test_ctr_true(mask);
3238
                break;
3239
            case 2:
3240
#if defined(TARGET_PPC64)
3241
                if (ctx->sf_mode)
3242
                    gen_op_test_ctrz_true_64(mask);
3243
                else
3244
#endif
3245
                    gen_op_test_ctrz_true(mask);
3246
                break;
3247
            default:
3248
            case 4:
3249
            case 6:
3250
                gen_op_test_true(mask);
3251
                break;
3252
            }
3253
        } else {
3254
            switch (bo & 0x6) {
3255
            case 0:
3256
#if defined(TARGET_PPC64)
3257
                if (ctx->sf_mode)
3258
                    gen_op_test_ctr_false_64(mask);
3259
                else
3260
#endif
3261
                    gen_op_test_ctr_false(mask);
3262
                break;
3263
            case 2:
3264
#if defined(TARGET_PPC64)
3265
                if (ctx->sf_mode)
3266
                    gen_op_test_ctrz_false_64(mask);
3267
                else
3268
#endif
3269
                    gen_op_test_ctrz_false(mask);
3270
                break;
3271
            default:
3272
            case 4:
3273
            case 6:
3274
                gen_op_test_false(mask);
3275
                break;
3276
            }
3277
        }
3278
    }
3279
    if (type == BCOND_IM) {
3280
        int l1 = gen_new_label();
3281
        gen_op_jz_T0(l1);
3282
        gen_goto_tb(ctx, 0, target);
3283
        gen_set_label(l1);
3284
        gen_goto_tb(ctx, 1, ctx->nip);
3285
    } else {
3286
#if defined(TARGET_PPC64)
3287
        if (ctx->sf_mode)
3288
            gen_op_btest_T1_64(ctx->nip >> 32, ctx->nip);
3289
        else
3290
#endif
3291
            gen_op_btest_T1(ctx->nip);
3292
        gen_op_reset_T0();
3293
    no_test:
3294
        if (ctx->singlestep_enabled)
3295
            gen_op_debug();
3296
        gen_op_exit_tb();
3297
    }
3298
 out:
3299
    ctx->exception = POWERPC_EXCP_BRANCH;
3300
}
3301

    
3302
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3303
{
3304
    gen_bcond(ctx, BCOND_IM);
3305
}
3306

    
3307
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
3308
{
3309
    gen_bcond(ctx, BCOND_CTR);
3310
}
3311

    
3312
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
3313
{
3314
    gen_bcond(ctx, BCOND_LR);
3315
}
3316

    
3317
/***                      Condition register logical                       ***/
3318
#define GEN_CRLOGIC(op, opc)                                                  \
3319
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
3320
{                                                                             \
3321
    uint8_t bitmask;                                                          \
3322
    int sh;                                                                   \
3323
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
3324
    sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3325
    if (sh > 0)                                                               \
3326
        gen_op_srli_T0(sh);                                                   \
3327
    else if (sh < 0)                                                          \
3328
        gen_op_sli_T0(-sh);                                                   \
3329
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
3330
    sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3331
    if (sh > 0)                                                               \
3332
        gen_op_srli_T1(sh);                                                   \
3333
    else if (sh < 0)                                                          \
3334
        gen_op_sli_T1(-sh);                                                   \
3335
    gen_op_##op();                                                            \
3336
    bitmask = 1 << (3 - (crbD(ctx->opcode) & 0x03));                          \
3337
    gen_op_andi_T0(bitmask);                                                  \
3338
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
3339
    gen_op_andi_T1(~bitmask);                                                 \
3340
    gen_op_or();                                                              \
3341
    gen_op_store_T0_crf(crbD(ctx->opcode) >> 2);                              \
3342
}
3343

    
3344
/* crand */
3345
GEN_CRLOGIC(and, 0x08);
3346
/* crandc */
3347
GEN_CRLOGIC(andc, 0x04);
3348
/* creqv */
3349
GEN_CRLOGIC(eqv, 0x09);
3350
/* crnand */
3351
GEN_CRLOGIC(nand, 0x07);
3352
/* crnor */
3353
GEN_CRLOGIC(nor, 0x01);
3354
/* cror */
3355
GEN_CRLOGIC(or, 0x0E);
3356
/* crorc */
3357
GEN_CRLOGIC(orc, 0x0D);
3358
/* crxor */
3359
GEN_CRLOGIC(xor, 0x06);
3360
/* mcrf */
3361
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
3362
{
3363
    gen_op_load_crf_T0(crfS(ctx->opcode));
3364
    gen_op_store_T0_crf(crfD(ctx->opcode));
3365
}
3366

    
3367
/***                           System linkage                              ***/
3368
/* rfi (supervisor only) */
3369
GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW)
3370
{
3371
#if defined(CONFIG_USER_ONLY)
3372
    GEN_EXCP_PRIVOPC(ctx);
3373
#else
3374
    /* Restore CPU state */
3375
    if (unlikely(!ctx->supervisor)) {
3376
        GEN_EXCP_PRIVOPC(ctx);
3377
        return;
3378
    }
3379
    gen_op_rfi();
3380
    GEN_SYNC(ctx);
3381
#endif
3382
}
3383

    
3384
#if defined(TARGET_PPC64)
3385
GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B)
3386
{
3387
#if defined(CONFIG_USER_ONLY)
3388
    GEN_EXCP_PRIVOPC(ctx);
3389
#else
3390
    /* Restore CPU state */
3391
    if (unlikely(!ctx->supervisor)) {
3392
        GEN_EXCP_PRIVOPC(ctx);
3393
        return;
3394
    }
3395
    gen_op_rfid();
3396
    GEN_SYNC(ctx);
3397
#endif
3398
}
3399
#endif
3400

    
3401
#if defined(TARGET_PPC64H)
3402
GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64B)
3403
{
3404
#if defined(CONFIG_USER_ONLY)
3405
    GEN_EXCP_PRIVOPC(ctx);
3406
#else
3407
    /* Restore CPU state */
3408
    if (unlikely(ctx->supervisor <= 1)) {
3409
        GEN_EXCP_PRIVOPC(ctx);
3410
        return;
3411
    }
3412
    gen_op_hrfid();
3413
    GEN_SYNC(ctx);
3414
#endif
3415
}
3416
#endif
3417

    
3418
/* sc */
3419
#if defined(CONFIG_USER_ONLY)
3420
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
3421
#else
3422
#define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
3423
#endif
3424
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW)
3425
{
3426
    uint32_t lev;
3427

    
3428
    lev = (ctx->opcode >> 5) & 0x7F;
3429
    GEN_EXCP(ctx, POWERPC_SYSCALL, lev);
3430
}
3431

    
3432
/***                                Trap                                   ***/
3433
/* tw */
3434
GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW)
3435
{
3436
    gen_op_load_gpr_T0(rA(ctx->opcode));
3437
    gen_op_load_gpr_T1(rB(ctx->opcode));
3438
    /* Update the nip since this might generate a trap exception */
3439
    gen_update_nip(ctx, ctx->nip);
3440
    gen_op_tw(TO(ctx->opcode));
3441
}
3442

    
3443
/* twi */
3444
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
3445
{
3446
    gen_op_load_gpr_T0(rA(ctx->opcode));
3447
    gen_set_T1(SIMM(ctx->opcode));
3448
    /* Update the nip since this might generate a trap exception */
3449
    gen_update_nip(ctx, ctx->nip);
3450
    gen_op_tw(TO(ctx->opcode));
3451
}
3452

    
3453
#if defined(TARGET_PPC64)
3454
/* td */
3455
GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B)
3456
{
3457
    gen_op_load_gpr_T0(rA(ctx->opcode));
3458
    gen_op_load_gpr_T1(rB(ctx->opcode));
3459
    /* Update the nip since this might generate a trap exception */
3460
    gen_update_nip(ctx, ctx->nip);
3461
    gen_op_td(TO(ctx->opcode));
3462
}
3463

    
3464
/* tdi */
3465
GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B)
3466
{
3467
    gen_op_load_gpr_T0(rA(ctx->opcode));
3468
    gen_set_T1(SIMM(ctx->opcode));
3469
    /* Update the nip since this might generate a trap exception */
3470
    gen_update_nip(ctx, ctx->nip);
3471
    gen_op_td(TO(ctx->opcode));
3472
}
3473
#endif
3474

    
3475
/***                          Processor control                            ***/
3476
/* mcrxr */
3477
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
3478
{
3479
    gen_op_load_xer_cr();
3480
    gen_op_store_T0_crf(crfD(ctx->opcode));
3481
    gen_op_clear_xer_ov();
3482
    gen_op_clear_xer_ca();
3483
}
3484

    
3485
/* mfcr */
3486
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC)
3487
{
3488
    uint32_t crm, crn;
3489

    
3490
    if (likely(ctx->opcode & 0x00100000)) {
3491
        crm = CRM(ctx->opcode);
3492
        if (likely((crm ^ (crm - 1)) == 0)) {
3493
            crn = ffs(crm);
3494
            gen_op_load_cro(7 - crn);
3495
        }
3496
    } else {
3497
        gen_op_load_cr();
3498
    }
3499
    gen_op_store_T0_gpr(rD(ctx->opcode));
3500
}
3501

    
3502
/* mfmsr */
3503
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
3504
{
3505
#if defined(CONFIG_USER_ONLY)
3506
    GEN_EXCP_PRIVREG(ctx);
3507
#else
3508
    if (unlikely(!ctx->supervisor)) {
3509
        GEN_EXCP_PRIVREG(ctx);
3510
        return;
3511
    }
3512
    gen_op_load_msr();
3513
    gen_op_store_T0_gpr(rD(ctx->opcode));
3514
#endif
3515
}
3516

    
3517
#if 1
3518
#define SPR_NOACCESS ((void *)(-1UL))
3519
#else
3520
static void spr_noaccess (void *opaque, int sprn)
3521
{
3522
    sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
3523
    printf("ERROR: try to access SPR %d !\n", sprn);
3524
}
3525
#define SPR_NOACCESS (&spr_noaccess)
3526
#endif
3527

    
3528
/* mfspr */
3529
static always_inline void gen_op_mfspr (DisasContext *ctx)
3530
{
3531
    void (*read_cb)(void *opaque, int sprn);
3532
    uint32_t sprn = SPR(ctx->opcode);
3533

    
3534
#if !defined(CONFIG_USER_ONLY)
3535
#if defined(TARGET_PPC64H)
3536
    if (ctx->supervisor == 2)
3537
        read_cb = ctx->spr_cb[sprn].hea_read;
3538
    else
3539
#endif
3540
    if (ctx->supervisor)
3541
        read_cb = ctx->spr_cb[sprn].oea_read;
3542
    else
3543
#endif
3544
        read_cb = ctx->spr_cb[sprn].uea_read;
3545
    if (likely(read_cb != NULL)) {
3546
        if (likely(read_cb != SPR_NOACCESS)) {
3547
            (*read_cb)(ctx, sprn);
3548
            gen_op_store_T0_gpr(rD(ctx->opcode));
3549
        } else {
3550
            /* Privilege exception */
3551
            /* This is a hack to avoid warnings when running Linux:
3552
             * this OS breaks the PowerPC virtualisation model,
3553
             * allowing userland application to read the PVR
3554
             */
3555
            if (sprn != SPR_PVR) {
3556
                if (loglevel != 0) {
3557
                    fprintf(logfile, "Trying to read privileged spr %d %03x at"
3558
                            ADDRX "\n", sprn, sprn, ctx->nip);
3559
                }
3560
                printf("Trying to read privileged spr %d %03x at " ADDRX "\n",
3561
                       sprn, sprn, ctx->nip);
3562
            }
3563
            GEN_EXCP_PRIVREG(ctx);
3564
        }
3565
    } else {
3566
        /* Not defined */
3567
        if (loglevel != 0) {
3568
            fprintf(logfile, "Trying to read invalid spr %d %03x at "
3569
                    ADDRX "\n", sprn, sprn, ctx->nip);
3570
        }
3571
        printf("Trying to read invalid spr %d %03x at " ADDRX "\n",
3572
               sprn, sprn, ctx->nip);
3573
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3574
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3575
    }
3576
}
3577

    
3578
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
3579
{
3580
    gen_op_mfspr(ctx);
3581
}
3582

    
3583
/* mftb */
3584
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB)
3585
{
3586
    gen_op_mfspr(ctx);
3587
}
3588

    
3589
/* mtcrf */
3590
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
3591
{
3592
    uint32_t crm, crn;
3593

    
3594
    gen_op_load_gpr_T0(rS(ctx->opcode));
3595
    crm = CRM(ctx->opcode);
3596
    if (likely((ctx->opcode & 0x00100000) || (crm ^ (crm - 1)) == 0)) {
3597
        crn = ffs(crm);
3598
        gen_op_srli_T0(crn * 4);
3599
        gen_op_andi_T0(0xF);
3600
        gen_op_store_cro(7 - crn);
3601
    } else {
3602
        gen_op_store_cr(crm);
3603
    }
3604
}
3605

    
3606
/* mtmsr */
3607
#if defined(TARGET_PPC64)
3608
GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B)
3609
{
3610
#if defined(CONFIG_USER_ONLY)
3611
    GEN_EXCP_PRIVREG(ctx);
3612
#else
3613
    if (unlikely(!ctx->supervisor)) {
3614
        GEN_EXCP_PRIVREG(ctx);
3615
        return;
3616
    }
3617
    gen_op_load_gpr_T0(rS(ctx->opcode));
3618
    if (ctx->opcode & 0x00010000) {
3619
        /* Special form that does not need any synchronisation */
3620
        gen_op_update_riee();
3621
    } else {
3622
        /* XXX: we need to update nip before the store
3623
         *      if we enter power saving mode, we will exit the loop
3624
         *      directly from ppc_store_msr
3625
         */
3626
        gen_update_nip(ctx, ctx->nip);
3627
        gen_op_store_msr();
3628
        /* Must stop the translation as machine state (may have) changed */
3629
        /* Note that mtmsr is not always defined as context-synchronizing */
3630
        ctx->exception = POWERPC_EXCP_STOP;
3631
    }
3632
#endif
3633
}
3634
#endif
3635

    
3636
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
3637
{
3638
#if defined(CONFIG_USER_ONLY)
3639
    GEN_EXCP_PRIVREG(ctx);
3640
#else
3641
    if (unlikely(!ctx->supervisor)) {
3642
        GEN_EXCP_PRIVREG(ctx);
3643
        return;
3644
    }
3645
    gen_op_load_gpr_T0(rS(ctx->opcode));
3646
    if (ctx->opcode & 0x00010000) {
3647
        /* Special form that does not need any synchronisation */
3648
        gen_op_update_riee();
3649
    } else {
3650
        /* XXX: we need to update nip before the store
3651
         *      if we enter power saving mode, we will exit the loop
3652
         *      directly from ppc_store_msr
3653
         */
3654
        gen_update_nip(ctx, ctx->nip);
3655
#if defined(TARGET_PPC64)
3656
        if (!ctx->sf_mode)
3657
            gen_op_store_msr_32();
3658
        else
3659
#endif
3660
            gen_op_store_msr();
3661
        /* Must stop the translation as machine state (may have) changed */
3662
        /* Note that mtmsrd is not always defined as context-synchronizing */
3663
        ctx->exception = POWERPC_EXCP_STOP;
3664
    }
3665
#endif
3666
}
3667

    
3668
/* mtspr */
3669
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
3670
{
3671
    void (*write_cb)(void *opaque, int sprn);
3672
    uint32_t sprn = SPR(ctx->opcode);
3673

    
3674
#if !defined(CONFIG_USER_ONLY)
3675
#if defined(TARGET_PPC64H)
3676
    if (ctx->supervisor == 2)
3677
        write_cb = ctx->spr_cb[sprn].hea_write;
3678
    else
3679
#endif
3680
    if (ctx->supervisor)
3681
        write_cb = ctx->spr_cb[sprn].oea_write;
3682
    else
3683
#endif
3684
        write_cb = ctx->spr_cb[sprn].uea_write;
3685
    if (likely(write_cb != NULL)) {
3686
        if (likely(write_cb != SPR_NOACCESS)) {
3687
            gen_op_load_gpr_T0(rS(ctx->opcode));
3688
            (*write_cb)(ctx, sprn);
3689
        } else {
3690
            /* Privilege exception */
3691
            if (loglevel != 0) {
3692
                fprintf(logfile, "Trying to write privileged spr %d %03x at "
3693
                        ADDRX "\n", sprn, sprn, ctx->nip);
3694
            }
3695
            printf("Trying to write privileged spr %d %03x at " ADDRX "\n",
3696
                   sprn, sprn, ctx->nip);
3697
            GEN_EXCP_PRIVREG(ctx);
3698
        }
3699
    } else {
3700
        /* Not defined */
3701
        if (loglevel != 0) {
3702
            fprintf(logfile, "Trying to write invalid spr %d %03x at "
3703
                    ADDRX "\n", sprn, sprn, ctx->nip);
3704
        }
3705
        printf("Trying to write invalid spr %d %03x at " ADDRX "\n",
3706
               sprn, sprn, ctx->nip);
3707
        GEN_EXCP(ctx, POWERPC_EXCP_PROGRAM,
3708
                 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_SPR);
3709
    }
3710
}
3711

    
3712
/***                         Cache management                              ***/
3713
/* dcbf */
3714
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE)
3715
{
3716
    /* XXX: specification says this is treated as a load by the MMU */
3717
    gen_addr_reg_index(ctx);
3718
    op_ldst(lbz);
3719
}
3720

    
3721
/* dcbi (Supervisor only) */
3722
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
3723
{
3724
#if defined(CONFIG_USER_ONLY)
3725
    GEN_EXCP_PRIVOPC(ctx);
3726
#else
3727
    if (unlikely(!ctx->supervisor)) {
3728
        GEN_EXCP_PRIVOPC(ctx);
3729
        return;
3730
    }
3731
    gen_addr_reg_index(ctx);
3732
    /* XXX: specification says this should be treated as a store by the MMU */
3733
    op_ldst(lbz);
3734
    op_ldst(stb);
3735
#endif
3736
}
3737

    
3738
/* dcdst */
3739
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
3740
{
3741
    /* XXX: specification say this is treated as a load by the MMU */
3742
    gen_addr_reg_index(ctx);
3743
    op_ldst(lbz);
3744
}
3745

    
3746
/* dcbt */
3747
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x02000001, PPC_CACHE)
3748
{
3749
    /* interpreted as no-op */
3750
    /* XXX: specification say this is treated as a load by the MMU
3751
     *      but does not generate any exception
3752
     */
3753
}
3754

    
3755
/* dcbtst */
3756
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x02000001, PPC_CACHE)
3757
{
3758
    /* interpreted as no-op */
3759
    /* XXX: specification say this is treated as a load by the MMU
3760
     *      but does not generate any exception
3761
     */
3762
}
3763

    
3764
/* dcbz */
3765
#define op_dcbz(n) (*gen_op_dcbz[n][ctx->mem_idx])()
3766
#if defined(CONFIG_USER_ONLY)
3767
/* User-mode only */
3768
static GenOpFunc *gen_op_dcbz[4][4] = {
3769
    {
3770
        &gen_op_dcbz_l32_raw,
3771
        &gen_op_dcbz_l32_raw,
3772
#if defined(TARGET_PPC64)
3773
        &gen_op_dcbz_l32_64_raw,
3774
        &gen_op_dcbz_l32_64_raw,
3775
#endif
3776
    },
3777
    {
3778
        &gen_op_dcbz_l64_raw,
3779
        &gen_op_dcbz_l64_raw,
3780
#if defined(TARGET_PPC64)
3781
        &gen_op_dcbz_l64_64_raw,
3782
        &gen_op_dcbz_l64_64_raw,
3783
#endif
3784
    },
3785
    {
3786
        &gen_op_dcbz_l128_raw,
3787
        &gen_op_dcbz_l128_raw,
3788
#if defined(TARGET_PPC64)
3789
        &gen_op_dcbz_l128_64_raw,
3790
        &gen_op_dcbz_l128_64_raw,
3791
#endif
3792
    },
3793
    {
3794
        &gen_op_dcbz_raw,
3795
        &gen_op_dcbz_raw,
3796
#if defined(TARGET_PPC64)
3797
        &gen_op_dcbz_64_raw,
3798
        &gen_op_dcbz_64_raw,
3799
#endif
3800
    },
3801
};
3802
#else
3803
#if defined(TARGET_PPC64)
3804
/* Full system - 64 bits mode */
3805
static GenOpFunc *gen_op_dcbz[4][12] = {
3806
    {
3807
        &gen_op_dcbz_l32_user,
3808
        &gen_op_dcbz_l32_user,
3809
        &gen_op_dcbz_l32_64_user,
3810
        &gen_op_dcbz_l32_64_user,
3811
        &gen_op_dcbz_l32_kernel,
3812
        &gen_op_dcbz_l32_kernel,
3813
        &gen_op_dcbz_l32_64_kernel,
3814
        &gen_op_dcbz_l32_64_kernel,
3815
#if defined(TARGET_PPC64H)
3816
        &gen_op_dcbz_l32_hypv,
3817
        &gen_op_dcbz_l32_hypv,
3818
        &gen_op_dcbz_l32_64_hypv,
3819
        &gen_op_dcbz_l32_64_hypv,
3820
#endif
3821
    },
3822
    {
3823
        &gen_op_dcbz_l64_user,
3824
        &gen_op_dcbz_l64_user,
3825
        &gen_op_dcbz_l64_64_user,
3826
        &gen_op_dcbz_l64_64_user,
3827
        &gen_op_dcbz_l64_kernel,
3828
        &gen_op_dcbz_l64_kernel,
3829
        &gen_op_dcbz_l64_64_kernel,
3830
        &gen_op_dcbz_l64_64_kernel,
3831
#if defined(TARGET_PPC64H)
3832
        &gen_op_dcbz_l64_hypv,
3833
        &gen_op_dcbz_l64_hypv,
3834
        &gen_op_dcbz_l64_64_hypv,
3835
        &gen_op_dcbz_l64_64_hypv,
3836
#endif
3837
    },
3838
    {
3839
        &gen_op_dcbz_l128_user,
3840
        &gen_op_dcbz_l128_user,
3841
        &gen_op_dcbz_l128_64_user,
3842
        &gen_op_dcbz_l128_64_user,
3843
        &gen_op_dcbz_l128_kernel,
3844
        &gen_op_dcbz_l128_kernel,
3845
        &gen_op_dcbz_l128_64_kernel,
3846
        &gen_op_dcbz_l128_64_kernel,
3847
#if defined(TARGET_PPC64H)
3848
        &gen_op_dcbz_l128_hypv,
3849
        &gen_op_dcbz_l128_hypv,
3850
        &gen_op_dcbz_l128_64_hypv,
3851
        &gen_op_dcbz_l128_64_hypv,
3852
#endif
3853
    },
3854
    {
3855
        &gen_op_dcbz_user,
3856
        &gen_op_dcbz_user,
3857
        &gen_op_dcbz_64_user,
3858
        &gen_op_dcbz_64_user,
3859
        &gen_op_dcbz_kernel,
3860
        &gen_op_dcbz_kernel,
3861
        &gen_op_dcbz_64_kernel,
3862
        &gen_op_dcbz_64_kernel,
3863
#if defined(TARGET_PPC64H)
3864
        &gen_op_dcbz_hypv,
3865
        &gen_op_dcbz_hypv,
3866
        &gen_op_dcbz_64_hypv,
3867
        &gen_op_dcbz_64_hypv,
3868
#endif
3869
    },
3870
};
3871
#else
3872
/* Full system - 32 bits mode */
3873
static GenOpFunc *gen_op_dcbz[4][4] = {
3874
    {
3875
        &gen_op_dcbz_l32_user,
3876
        &gen_op_dcbz_l32_user,
3877
        &gen_op_dcbz_l32_kernel,
3878
        &gen_op_dcbz_l32_kernel,
3879
    },
3880
    {
3881
        &gen_op_dcbz_l64_user,
3882
        &gen_op_dcbz_l64_user,
3883
        &gen_op_dcbz_l64_kernel,
3884
        &gen_op_dcbz_l64_kernel,
3885
    },
3886
    {
3887
        &gen_op_dcbz_l128_user,
3888
        &gen_op_dcbz_l128_user,
3889
        &gen_op_dcbz_l128_kernel,
3890
        &gen_op_dcbz_l128_kernel,
3891
    },
3892
    {
3893
        &gen_op_dcbz_user,
3894
        &gen_op_dcbz_user,
3895
        &gen_op_dcbz_kernel,
3896
        &gen_op_dcbz_kernel,
3897
    },
3898
};
3899
#endif
3900
#endif
3901

    
3902
static always_inline void handler_dcbz (DisasContext *ctx,
3903
                                        int dcache_line_size)
3904
{
3905
    int n;
3906

    
3907
    switch (dcache_line_size) {
3908
    case 32:
3909
        n = 0;
3910
        break;
3911
    case 64:
3912
        n = 1;
3913
        break;
3914
    case 128:
3915
        n = 2;
3916
        break;
3917
    default:
3918
        n = 3;
3919
        break;
3920
    }
3921
    op_dcbz(n);
3922
}
3923

    
3924
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE_DCBZ)
3925
{
3926
    gen_addr_reg_index(ctx);
3927
    handler_dcbz(ctx, ctx->dcache_line_size);
3928
    gen_op_check_reservation();
3929
}
3930

    
3931
GEN_HANDLER2(dcbz_970, "dcbz", 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZT)
3932
{
3933
    gen_addr_reg_index(ctx);
3934
    if (ctx->opcode & 0x00200000)
3935
        handler_dcbz(ctx, ctx->dcache_line_size);
3936
    else
3937
        handler_dcbz(ctx, -1);
3938
    gen_op_check_reservation();
3939
}
3940

    
3941
/* icbi */
3942
#define op_icbi() (*gen_op_icbi[ctx->mem_idx])()
3943
#if defined(CONFIG_USER_ONLY)
3944
/* User-mode only */
3945
static GenOpFunc *gen_op_icbi[] = {
3946
    &gen_op_icbi_raw,
3947
    &gen_op_icbi_raw,
3948
#if defined(TARGET_PPC64)
3949
    &gen_op_icbi_64_raw,
3950
    &gen_op_icbi_64_raw,
3951
#endif
3952
};
3953
#else
3954
/* Full system - 64 bits mode */
3955
#if defined(TARGET_PPC64)
3956
static GenOpFunc *gen_op_icbi[] = {
3957
    &gen_op_icbi_user,
3958
    &gen_op_icbi_user,
3959
    &gen_op_icbi_64_user,
3960
    &gen_op_icbi_64_user,
3961
    &gen_op_icbi_kernel,
3962
    &gen_op_icbi_kernel,
3963
    &gen_op_icbi_64_kernel,
3964
    &gen_op_icbi_64_kernel,
3965
#if defined(TARGET_PPC64H)
3966
    &gen_op_icbi_hypv,
3967
    &gen_op_icbi_hypv,
3968
    &gen_op_icbi_64_hypv,
3969
    &gen_op_icbi_64_hypv,
3970
#endif
3971
};
3972
#else
3973
/* Full system - 32 bits mode */
3974
static GenOpFunc *gen_op_icbi[] = {
3975
    &gen_op_icbi_user,
3976
    &gen_op_icbi_user,
3977
    &gen_op_icbi_kernel,
3978
    &gen_op_icbi_kernel,
3979
};
3980
#endif
3981
#endif
3982

    
3983
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
3984
{
3985
    /* NIP cannot be restored if the memory exception comes from an helper */
3986
    gen_update_nip(ctx, ctx->nip - 4);
3987
    gen_addr_reg_index(ctx);
3988
    op_icbi();
3989
}
3990

    
3991
/* Optional: */
3992
/* dcba */
3993
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA)
3994
{
3995
    /* interpreted as no-op */
3996
    /* XXX: specification say this is treated as a store by the MMU
3997
     *      but does not generate any exception
3998
     */
3999
}
4000

    
4001
/***                    Segment register manipulation                      ***/
4002
/* Supervisor only: */
4003
/* mfsr */
4004
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
4005
{
4006
#if defined(CONFIG_USER_ONLY)
4007
    GEN_EXCP_PRIVREG(ctx);
4008
#else
4009
    if (unlikely(!ctx->supervisor)) {
4010
        GEN_EXCP_PRIVREG(ctx);
4011
        return;
4012
    }
4013
    gen_op_set_T1(SR(ctx->opcode));
4014
    gen_op_load_sr();
4015
    gen_op_store_T0_gpr(rD(ctx->opcode));
4016
#endif
4017
}
4018

    
4019
/* mfsrin */
4020
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
4021
{
4022
#if defined(CONFIG_USER_ONLY)
4023
    GEN_EXCP_PRIVREG(ctx);
4024
#else
4025
    if (unlikely(!ctx->supervisor)) {
4026
        GEN_EXCP_PRIVREG(ctx);
4027
        return;
4028
    }
4029
    gen_op_load_gpr_T1(rB(ctx->opcode));
4030
    gen_op_srli_T1(28);
4031
    gen_op_load_sr();
4032
    gen_op_store_T0_gpr(rD(ctx->opcode));
4033
#endif
4034
}
4035

    
4036
/* mtsr */
4037
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
4038
{
4039
#if defined(CONFIG_USER_ONLY)
4040
    GEN_EXCP_PRIVREG(ctx);
4041
#else
4042
    if (unlikely(!ctx->supervisor)) {
4043
        GEN_EXCP_PRIVREG(ctx);
4044
        return;
4045
    }
4046
    gen_op_load_gpr_T0(rS(ctx->opcode));
4047
    gen_op_set_T1(SR(ctx->opcode));
4048
    gen_op_store_sr();
4049
#endif
4050
}
4051

    
4052
/* mtsrin */
4053
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
4054
{
4055
#if defined(CONFIG_USER_ONLY)
4056
    GEN_EXCP_PRIVREG(ctx);
4057
#else
4058
    if (unlikely(!ctx->supervisor)) {
4059
        GEN_EXCP_PRIVREG(ctx);
4060
        return;
4061
    }
4062
    gen_op_load_gpr_T0(rS(ctx->opcode));
4063
    gen_op_load_gpr_T1(rB(ctx->opcode));
4064
    gen_op_srli_T1(28);
4065
    gen_op_store_sr();
4066
#endif
4067
}
4068

    
4069
#if defined(TARGET_PPC64)
4070
/* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4071
/* mfsr */
4072
GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B)
4073
{
4074
#if defined(CONFIG_USER_ONLY)
4075
    GEN_EXCP_PRIVREG(ctx);
4076
#else
4077
    if (unlikely(!ctx->supervisor)) {
4078
        GEN_EXCP_PRIVREG(ctx);
4079
        return;
4080
    }
4081
    gen_op_set_T1(SR(ctx->opcode));
4082
    gen_op_load_slb();
4083
    gen_op_store_T0_gpr(rD(ctx->opcode));
4084
#endif
4085
}
4086

    
4087
/* mfsrin */
4088
GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
4089
             PPC_SEGMENT_64B)
4090
{
4091
#if defined(CONFIG_USER_ONLY)
4092
    GEN_EXCP_PRIVREG(ctx);
4093
#else
4094
    if (unlikely(!ctx->supervisor)) {
4095
        GEN_EXCP_PRIVREG(ctx);
4096
        return;
4097
    }
4098
    gen_op_load_gpr_T1(rB(ctx->opcode));
4099
    gen_op_srli_T1(28);
4100
    gen_op_load_slb();
4101
    gen_op_store_T0_gpr(rD(ctx->opcode));
4102
#endif
4103
}
4104

    
4105
/* mtsr */
4106
GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B)
4107
{
4108
#if defined(CONFIG_USER_ONLY)
4109
    GEN_EXCP_PRIVREG(ctx);
4110
#else
4111
    if (unlikely(!ctx->supervisor)) {
4112
        GEN_EXCP_PRIVREG(ctx);
4113
        return;
4114
    }
4115
    gen_op_load_gpr_T0(rS(ctx->opcode));
4116
    gen_op_set_T1(SR(ctx->opcode));
4117
    gen_op_store_slb();
4118
#endif
4119
}
4120

    
4121
/* mtsrin */
4122
GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
4123
             PPC_SEGMENT_64B)
4124
{
4125
#if defined(CONFIG_USER_ONLY)
4126
    GEN_EXCP_PRIVREG(ctx);
4127
#else
4128
    if (unlikely(!ctx->supervisor)) {
4129
        GEN_EXCP_PRIVREG(ctx);
4130
        return;
4131
    }
4132
    gen_op_load_gpr_T0(rS(ctx->opcode));
4133
    gen_op_load_gpr_T1(rB(ctx->opcode));
4134
    gen_op_srli_T1(28);
4135
    gen_op_store_slb();
4136
#endif
4137
}
4138
#endif /* defined(TARGET_PPC64) */
4139

    
4140
/***                      Lookaside buffer management                      ***/
4141
/* Optional & supervisor only: */
4142
/* tlbia */
4143
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA)
4144
{
4145
#if defined(CONFIG_USER_ONLY)
4146
    GEN_EXCP_PRIVOPC(ctx);
4147
#else
4148
    if (unlikely(!ctx->supervisor)) {
4149
        if (loglevel != 0)
4150
            fprintf(logfile, "%s: ! supervisor\n", __func__);
4151
        GEN_EXCP_PRIVOPC(ctx);
4152
        return;
4153
    }
4154
    gen_op_tlbia();
4155
#endif
4156
}
4157

    
4158
/* tlbie */
4159
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM_TLBIE)
4160
{
4161
#if defined(CONFIG_USER_ONLY)
4162
    GEN_EXCP_PRIVOPC(ctx);
4163
#else
4164
    if (unlikely(!ctx->supervisor)) {
4165
        GEN_EXCP_PRIVOPC(ctx);
4166
        return;
4167
    }
4168
    gen_op_load_gpr_T0(rB(ctx->opcode));
4169
#if defined(TARGET_PPC64)
4170
    if (ctx->sf_mode)
4171
        gen_op_tlbie_64();
4172
    else
4173
#endif
4174
        gen_op_tlbie();
4175
#endif
4176
}
4177

    
4178
/* tlbsync */
4179
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC)
4180
{
4181
#if defined(CONFIG_USER_ONLY)
4182
    GEN_EXCP_PRIVOPC(ctx);
4183
#else
4184
    if (unlikely(!ctx->supervisor)) {
4185
        GEN_EXCP_PRIVOPC(ctx);
4186
        return;
4187
    }
4188
    /* This has no effect: it should ensure that all previous
4189
     * tlbie have completed
4190
     */
4191
    GEN_STOP(ctx);
4192
#endif
4193
}
4194

    
4195
#if defined(TARGET_PPC64)
4196
/* slbia */
4197
GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x03FFFC01, PPC_SLBI)
4198
{
4199
#if defined(CONFIG_USER_ONLY)
4200
    GEN_EXCP_PRIVOPC(ctx);
4201
#else
4202
    if (unlikely(!ctx->supervisor)) {
4203
        if (loglevel != 0)
4204
            fprintf(logfile, "%s: ! supervisor\n", __func__);
4205
        GEN_EXCP_PRIVOPC(ctx);
4206
        return;
4207
    }
4208
    gen_op_slbia();
4209
#endif
4210
}
4211

    
4212
/* slbie */
4213
GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI)
4214
{
4215
#if defined(CONFIG_USER_ONLY)
4216
    GEN_EXCP_PRIVOPC(ctx);
4217
#else
4218
    if (unlikely(!ctx->supervisor)) {
4219
        GEN_EXCP_PRIVOPC(ctx);
4220
        return;
4221
    }
4222
    gen_op_load_gpr_T0(rB(ctx->opcode));
4223
    gen_op_slbie();
4224
#endif
4225
}
4226
#endif
4227

    
4228
/***                              External control                         ***/
4229
/* Optional: */
4230
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
4231
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
4232
#if defined(CONFIG_USER_ONLY)
4233
/* User-mode only */
4234
static GenOpFunc *gen_op_eciwx[] = {
4235
    &gen_op_eciwx_raw,
4236
    &gen_op_eciwx_le_raw,
4237
#if defined(TARGET_PPC64)
4238
    &gen_op_eciwx_64_raw,
4239
    &gen_op_eciwx_le_64_raw,
4240
#endif
4241
};
4242
static GenOpFunc *gen_op_ecowx[] = {
4243
    &gen_op_ecowx_raw,
4244
    &gen_op_ecowx_le_raw,
4245
#if defined(TARGET_PPC64)
4246
    &gen_op_ecowx_64_raw,
4247
    &gen_op_ecowx_le_64_raw,
4248
#endif
4249
};
4250
#else
4251
#if defined(TARGET_PPC64)
4252
/* Full system - 64 bits mode */
4253
static GenOpFunc *gen_op_eciwx[] = {
4254
    &gen_op_eciwx_user,
4255
    &gen_op_eciwx_le_user,
4256
    &gen_op_eciwx_64_user,
4257
    &gen_op_eciwx_le_64_user,
4258
    &gen_op_eciwx_kernel,
4259
    &gen_op_eciwx_le_kernel,
4260
    &gen_op_eciwx_64_kernel,
4261
    &gen_op_eciwx_le_64_kernel,
4262
#if defined(TARGET_PPC64H)
4263
    &gen_op_eciwx_hypv,
4264
    &gen_op_eciwx_le_hypv,
4265
    &gen_op_eciwx_64_hypv,
4266
    &gen_op_eciwx_le_64_hypv,
4267
#endif
4268
};
4269
static GenOpFunc *gen_op_ecowx[] = {
4270
    &gen_op_ecowx_user,
4271
    &gen_op_ecowx_le_user,
4272
    &gen_op_ecowx_64_user,
4273
    &gen_op_ecowx_le_64_user,
4274
    &gen_op_ecowx_kernel,
4275
    &gen_op_ecowx_le_kernel,
4276
    &gen_op_ecowx_64_kernel,
4277
    &gen_op_ecowx_le_64_kernel,
4278
#if defined(TARGET_PPC64H)
4279
    &gen_op_ecowx_hypv,
4280
    &gen_op_ecowx_le_hypv,
4281
    &gen_op_ecowx_64_hypv,
4282
    &gen_op_ecowx_le_64_hypv,
4283
#endif
4284
};
4285
#else
4286
/* Full system - 32 bits mode */
4287
static GenOpFunc *gen_op_eciwx[] = {
4288
    &gen_op_eciwx_user,
4289
    &gen_op_eciwx_le_user,
4290
    &gen_op_eciwx_kernel,
4291
    &gen_op_eciwx_le_kernel,
4292
};
4293
static GenOpFunc *gen_op_ecowx[] = {
4294
    &gen_op_ecowx_user,
4295
    &gen_op_ecowx_le_user,
4296
    &gen_op_ecowx_kernel,
4297
    &gen_op_ecowx_le_kernel,
4298
};
4299
#endif
4300
#endif
4301

    
4302
/* eciwx */
4303
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
4304
{
4305
    /* Should check EAR[E] & alignment ! */
4306
    gen_addr_reg_index(ctx);
4307
    op_eciwx();
4308
    gen_op_store_T0_gpr(rD(ctx->opcode));
4309
}
4310

    
4311
/* ecowx */
4312
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
4313
{
4314
    /* Should check EAR[E] & alignment ! */
4315
    gen_addr_reg_index(ctx);
4316
    gen_op_load_gpr_T1(rS(ctx->opcode));
4317
    op_ecowx();
4318
}
4319

    
4320
/* PowerPC 601 specific instructions */
4321
/* abs - abs. */
4322
GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR)
4323
{
4324
    gen_op_load_gpr_T0(rA(ctx->opcode));
4325
    gen_op_POWER_abs();
4326
    gen_op_store_T0_gpr(rD(ctx->opcode));
4327
    if (unlikely(Rc(ctx->opcode) != 0))
4328
        gen_set_Rc0(ctx);
4329
}
4330

    
4331
/* abso - abso. */
4332
GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR)
4333
{
4334
    gen_op_load_gpr_T0(rA(ctx->opcode));
4335
    gen_op_POWER_abso();
4336
    gen_op_store_T0_gpr(rD(ctx->opcode));
4337
    if (unlikely(Rc(ctx->opcode) != 0))
4338
        gen_set_Rc0(ctx);
4339
}
4340

    
4341
/* clcs */
4342
GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR)
4343
{
4344
    gen_op_load_gpr_T0(rA(ctx->opcode));
4345
    gen_op_POWER_clcs();
4346
    /* Rc=1 sets CR0 to an undefined state */
4347
    gen_op_store_T0_gpr(rD(ctx->opcode));
4348
}
4349

    
4350
/* div - div. */
4351
GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR)
4352
{
4353
    gen_op_load_gpr_T0(rA(ctx->opcode));
4354
    gen_op_load_gpr_T1(rB(ctx->opcode));
4355
    gen_op_POWER_div();
4356
    gen_op_store_T0_gpr(rD(ctx->opcode));
4357
    if (unlikely(Rc(ctx->opcode) != 0))
4358
        gen_set_Rc0(ctx);
4359
}
4360

    
4361
/* divo - divo. */
4362
GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR)
4363
{
4364
    gen_op_load_gpr_T0(rA(ctx->opcode));
4365
    gen_op_load_gpr_T1(rB(ctx->opcode));
4366
    gen_op_POWER_divo();
4367
    gen_op_store_T0_gpr(rD(ctx->opcode));
4368
    if (unlikely(Rc(ctx->opcode) != 0))
4369
        gen_set_Rc0(ctx);
4370
}
4371

    
4372
/* divs - divs. */
4373
GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR)
4374
{
4375
    gen_op_load_gpr_T0(rA(ctx->opcode));
4376
    gen_op_load_gpr_T1(rB(ctx->opcode));
4377
    gen_op_POWER_divs();
4378
    gen_op_store_T0_gpr(rD(ctx->opcode));
4379
    if (unlikely(Rc(ctx->opcode) != 0))
4380
        gen_set_Rc0(ctx);
4381
}
4382

    
4383
/* divso - divso. */
4384
GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR)
4385
{
4386
    gen_op_load_gpr_T0(rA(ctx->opcode));
4387
    gen_op_load_gpr_T1(rB(ctx->opcode));
4388
    gen_op_POWER_divso();
4389
    gen_op_store_T0_gpr(rD(ctx->opcode));
4390
    if (unlikely(Rc(ctx->opcode) != 0))
4391
        gen_set_Rc0(ctx);
4392
}
4393

    
4394
/* doz - doz. */
4395
GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR)
4396
{
4397
    gen_op_load_gpr_T0(rA(ctx->opcode));
4398
    gen_op_load_gpr_T1(rB(ctx->opcode));
4399
    gen_op_POWER_doz();
4400
    gen_op_store_T0_gpr(rD(ctx->opcode));
4401
    if (unlikely(Rc(ctx->opcode) != 0))
4402
        gen_set_Rc0(ctx);
4403
}
4404

    
4405
/* dozo - dozo. */
4406
GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR)
4407
{
4408
    gen_op_load_gpr_T0(rA(ctx->opcode));
4409
    gen_op_load_gpr_T1(rB(ctx->opcode));
4410
    gen_op_POWER_dozo();
4411
    gen_op_store_T0_gpr(rD(ctx->opcode));
4412
    if (unlikely(Rc(ctx->opcode) != 0))
4413
        gen_set_Rc0(ctx);
4414
}
4415

    
4416
/* dozi */
4417
GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4418
{
4419
    gen_op_load_gpr_T0(rA(ctx->opcode));
4420
    gen_op_set_T1(SIMM(ctx->opcode));
4421
    gen_op_POWER_doz();
4422
    gen_op_store_T0_gpr(rD(ctx->opcode));
4423
}
4424

    
4425
/* As lscbx load from memory byte after byte, it's always endian safe */
4426
#define op_POWER_lscbx(start, ra, rb)                                         \
4427
(*gen_op_POWER_lscbx[ctx->mem_idx])(start, ra, rb)
4428
#if defined(CONFIG_USER_ONLY)
4429
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
4430
    &gen_op_POWER_lscbx_raw,
4431
    &gen_op_POWER_lscbx_raw,
4432
};
4433
#else
4434
static GenOpFunc3 *gen_op_POWER_lscbx[] = {
4435
    &gen_op_POWER_lscbx_user,
4436
    &gen_op_POWER_lscbx_user,
4437
    &gen_op_POWER_lscbx_kernel,
4438
    &gen_op_POWER_lscbx_kernel,
4439
};
4440
#endif
4441

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

    
4448
    gen_addr_reg_index(ctx);
4449
    if (ra == 0) {
4450
        ra = rb;
4451
    }
4452
    /* NIP cannot be restored if the memory exception comes from an helper */
4453
    gen_update_nip(ctx, ctx->nip - 4);
4454
    gen_op_load_xer_bc();
4455
    gen_op_load_xer_cmp();
4456
    op_POWER_lscbx(rD(ctx->opcode), ra, rb);
4457
    gen_op_store_xer_bc();
4458
    if (unlikely(Rc(ctx->opcode) != 0))
4459
        gen_set_Rc0(ctx);
4460
}
4461

    
4462
/* maskg - maskg. */
4463
GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR)
4464
{
4465
    gen_op_load_gpr_T0(rS(ctx->opcode));
4466
    gen_op_load_gpr_T1(rB(ctx->opcode));
4467
    gen_op_POWER_maskg();
4468
    gen_op_store_T0_gpr(rA(ctx->opcode));
4469
    if (unlikely(Rc(ctx->opcode) != 0))
4470
        gen_set_Rc0(ctx);
4471
}
4472

    
4473
/* maskir - maskir. */
4474
GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR)
4475
{
4476
    gen_op_load_gpr_T0(rA(ctx->opcode));
4477
    gen_op_load_gpr_T1(rS(ctx->opcode));
4478
    gen_op_load_gpr_T2(rB(ctx->opcode));
4479
    gen_op_POWER_maskir();
4480
    gen_op_store_T0_gpr(rA(ctx->opcode));
4481
    if (unlikely(Rc(ctx->opcode) != 0))
4482
        gen_set_Rc0(ctx);
4483
}
4484

    
4485
/* mul - mul. */
4486
GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR)
4487
{
4488
    gen_op_load_gpr_T0(rA(ctx->opcode));
4489
    gen_op_load_gpr_T1(rB(ctx->opcode));
4490
    gen_op_POWER_mul();
4491
    gen_op_store_T0_gpr(rD(ctx->opcode));
4492
    if (unlikely(Rc(ctx->opcode) != 0))
4493
        gen_set_Rc0(ctx);
4494
}
4495

    
4496
/* mulo - mulo. */
4497
GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR)
4498
{
4499
    gen_op_load_gpr_T0(rA(ctx->opcode));
4500
    gen_op_load_gpr_T1(rB(ctx->opcode));
4501
    gen_op_POWER_mulo();
4502
    gen_op_store_T0_gpr(rD(ctx->opcode));
4503
    if (unlikely(Rc(ctx->opcode) != 0))
4504
        gen_set_Rc0(ctx);
4505
}
4506

    
4507
/* nabs - nabs. */
4508
GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR)
4509
{
4510
    gen_op_load_gpr_T0(rA(ctx->opcode));
4511
    gen_op_POWER_nabs();
4512
    gen_op_store_T0_gpr(rD(ctx->opcode));
4513
    if (unlikely(Rc(ctx->opcode) != 0))
4514
        gen_set_Rc0(ctx);
4515
}
4516

    
4517
/* nabso - nabso. */
4518
GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR)
4519
{
4520
    gen_op_load_gpr_T0(rA(ctx->opcode));
4521
    gen_op_POWER_nabso();
4522
    gen_op_store_T0_gpr(rD(ctx->opcode));
4523
    if (unlikely(Rc(ctx->opcode) != 0))
4524
        gen_set_Rc0(ctx);
4525
}
4526

    
4527
/* rlmi - rlmi. */
4528
GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR)
4529
{
4530
    uint32_t mb, me;
4531

    
4532
    mb = MB(ctx->opcode);
4533
    me = ME(ctx->opcode);
4534
    gen_op_load_gpr_T0(rS(ctx->opcode));
4535
    gen_op_load_gpr_T1(rA(ctx->opcode));
4536
    gen_op_load_gpr_T2(rB(ctx->opcode));
4537
    gen_op_POWER_rlmi(MASK(mb, me), ~MASK(mb, me));
4538
    gen_op_store_T0_gpr(rA(ctx->opcode));
4539
    if (unlikely(Rc(ctx->opcode) != 0))
4540
        gen_set_Rc0(ctx);
4541
}
4542

    
4543
/* rrib - rrib. */
4544
GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR)
4545
{
4546
    gen_op_load_gpr_T0(rS(ctx->opcode));
4547
    gen_op_load_gpr_T1(rA(ctx->opcode));
4548
    gen_op_load_gpr_T2(rB(ctx->opcode));
4549
    gen_op_POWER_rrib();
4550
    gen_op_store_T0_gpr(rA(ctx->opcode));
4551
    if (unlikely(Rc(ctx->opcode) != 0))
4552
        gen_set_Rc0(ctx);
4553
}
4554

    
4555
/* sle - sle. */
4556
GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR)
4557
{
4558
    gen_op_load_gpr_T0(rS(ctx->opcode));
4559
    gen_op_load_gpr_T1(rB(ctx->opcode));
4560
    gen_op_POWER_sle();
4561
    gen_op_store_T0_gpr(rA(ctx->opcode));
4562
    if (unlikely(Rc(ctx->opcode) != 0))
4563
        gen_set_Rc0(ctx);
4564
}
4565

    
4566
/* sleq - sleq. */
4567
GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR)
4568
{
4569
    gen_op_load_gpr_T0(rS(ctx->opcode));
4570
    gen_op_load_gpr_T1(rB(ctx->opcode));
4571
    gen_op_POWER_sleq();
4572
    gen_op_store_T0_gpr(rA(ctx->opcode));
4573
    if (unlikely(Rc(ctx->opcode) != 0))
4574
        gen_set_Rc0(ctx);
4575
}
4576

    
4577
/* sliq - sliq. */
4578
GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR)
4579
{
4580
    gen_op_load_gpr_T0(rS(ctx->opcode));
4581
    gen_op_set_T1(SH(ctx->opcode));
4582
    gen_op_POWER_sle();
4583
    gen_op_store_T0_gpr(rA(ctx->opcode));
4584
    if (unlikely(Rc(ctx->opcode) != 0))
4585
        gen_set_Rc0(ctx);
4586
}
4587

    
4588
/* slliq - slliq. */
4589
GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR)
4590
{
4591
    gen_op_load_gpr_T0(rS(ctx->opcode));
4592
    gen_op_set_T1(SH(ctx->opcode));
4593
    gen_op_POWER_sleq();
4594
    gen_op_store_T0_gpr(rA(ctx->opcode));
4595
    if (unlikely(Rc(ctx->opcode) != 0))
4596
        gen_set_Rc0(ctx);
4597
}
4598

    
4599
/* sllq - sllq. */
4600
GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR)
4601
{
4602
    gen_op_load_gpr_T0(rS(ctx->opcode));
4603
    gen_op_load_gpr_T1(rB(ctx->opcode));
4604
    gen_op_POWER_sllq();
4605
    gen_op_store_T0_gpr(rA(ctx->opcode));
4606
    if (unlikely(Rc(ctx->opcode) != 0))
4607
        gen_set_Rc0(ctx);
4608
}
4609

    
4610
/* slq - slq. */
4611
GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR)
4612
{
4613
    gen_op_load_gpr_T0(rS(ctx->opcode));
4614
    gen_op_load_gpr_T1(rB(ctx->opcode));
4615
    gen_op_POWER_slq();
4616
    gen_op_store_T0_gpr(rA(ctx->opcode));
4617
    if (unlikely(Rc(ctx->opcode) != 0))
4618
        gen_set_Rc0(ctx);
4619
}
4620

    
4621
/* sraiq - sraiq. */
4622
GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR)
4623
{
4624
    gen_op_load_gpr_T0(rS(ctx->opcode));
4625
    gen_op_set_T1(SH(ctx->opcode));
4626
    gen_op_POWER_sraq();
4627
    gen_op_store_T0_gpr(rA(ctx->opcode));
4628
    if (unlikely(Rc(ctx->opcode) != 0))
4629
        gen_set_Rc0(ctx);
4630
}
4631

    
4632
/* sraq - sraq. */
4633
GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR)
4634
{
4635
    gen_op_load_gpr_T0(rS(ctx->opcode));
4636
    gen_op_load_gpr_T1(rB(ctx->opcode));
4637
    gen_op_POWER_sraq();
4638
    gen_op_store_T0_gpr(rA(ctx->opcode));
4639
    if (unlikely(Rc(ctx->opcode) != 0))
4640
        gen_set_Rc0(ctx);
4641
}
4642

    
4643
/* sre - sre. */
4644
GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR)
4645
{
4646
    gen_op_load_gpr_T0(rS(ctx->opcode));
4647
    gen_op_load_gpr_T1(rB(ctx->opcode));
4648
    gen_op_POWER_sre();
4649
    gen_op_store_T0_gpr(rA(ctx->opcode));
4650
    if (unlikely(Rc(ctx->opcode) != 0))
4651
        gen_set_Rc0(ctx);
4652
}
4653

    
4654
/* srea - srea. */
4655
GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR)
4656
{
4657
    gen_op_load_gpr_T0(rS(ctx->opcode));
4658
    gen_op_load_gpr_T1(rB(ctx->opcode));
4659
    gen_op_POWER_srea();
4660
    gen_op_store_T0_gpr(rA(ctx->opcode));
4661
    if (unlikely(Rc(ctx->opcode) != 0))
4662
        gen_set_Rc0(ctx);
4663
}
4664

    
4665
/* sreq */
4666
GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR)
4667
{
4668
    gen_op_load_gpr_T0(rS(ctx->opcode));
4669
    gen_op_load_gpr_T1(rB(ctx->opcode));
4670
    gen_op_POWER_sreq();
4671
    gen_op_store_T0_gpr(rA(ctx->opcode));
4672
    if (unlikely(Rc(ctx->opcode) != 0))
4673
        gen_set_Rc0(ctx);
4674
}
4675

    
4676
/* sriq */
4677
GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR)
4678
{
4679
    gen_op_load_gpr_T0(rS(ctx->opcode));
4680
    gen_op_set_T1(SH(ctx->opcode));
4681
    gen_op_POWER_srq();
4682
    gen_op_store_T0_gpr(rA(ctx->opcode));
4683
    if (unlikely(Rc(ctx->opcode) != 0))
4684
        gen_set_Rc0(ctx);
4685
}
4686

    
4687
/* srliq */
4688
GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR)
4689
{
4690
    gen_op_load_gpr_T0(rS(ctx->opcode));
4691
    gen_op_load_gpr_T1(rB(ctx->opcode));
4692
    gen_op_set_T1(SH(ctx->opcode));
4693
    gen_op_POWER_srlq();
4694
    gen_op_store_T0_gpr(rA(ctx->opcode));
4695
    if (unlikely(Rc(ctx->opcode) != 0))
4696
        gen_set_Rc0(ctx);
4697
}
4698

    
4699
/* srlq */
4700
GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR)
4701
{
4702
    gen_op_load_gpr_T0(rS(ctx->opcode));
4703
    gen_op_load_gpr_T1(rB(ctx->opcode));
4704
    gen_op_POWER_srlq();
4705
    gen_op_store_T0_gpr(rA(ctx->opcode));
4706
    if (unlikely(Rc(ctx->opcode) != 0))
4707
        gen_set_Rc0(ctx);
4708
}
4709

    
4710
/* srq */
4711
GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR)
4712
{
4713
    gen_op_load_gpr_T0(rS(ctx->opcode));
4714
    gen_op_load_gpr_T1(rB(ctx->opcode));
4715
    gen_op_POWER_srq();
4716
    gen_op_store_T0_gpr(rA(ctx->opcode));
4717
    if (unlikely(Rc(ctx->opcode) != 0))
4718
        gen_set_Rc0(ctx);
4719
}
4720

    
4721
/* PowerPC 602 specific instructions */
4722
/* dsa  */
4723
GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC)
4724
{
4725
    /* XXX: TODO */
4726
    GEN_EXCP_INVAL(ctx);
4727
}
4728

    
4729
/* esa */
4730
GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC)
4731
{
4732
    /* XXX: TODO */
4733
    GEN_EXCP_INVAL(ctx);
4734
}
4735

    
4736
/* mfrom */
4737
GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC)
4738
{
4739
#if defined(CONFIG_USER_ONLY)
4740
    GEN_EXCP_PRIVOPC(ctx);
4741
#else
4742
    if (unlikely(!ctx->supervisor)) {
4743
        GEN_EXCP_PRIVOPC(ctx);
4744
        return;
4745
    }
4746
    gen_op_load_gpr_T0(rA(ctx->opcode));
4747
    gen_op_602_mfrom();
4748
    gen_op_store_T0_gpr(rD(ctx->opcode));
4749
#endif
4750
}
4751

    
4752
/* 602 - 603 - G2 TLB management */
4753
/* tlbld */
4754
GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB)
4755
{
4756
#if defined(CONFIG_USER_ONLY)
4757
    GEN_EXCP_PRIVOPC(ctx);
4758
#else
4759
    if (unlikely(!ctx->supervisor)) {
4760
        GEN_EXCP_PRIVOPC(ctx);
4761
        return;
4762
    }
4763
    gen_op_load_gpr_T0(rB(ctx->opcode));
4764
    gen_op_6xx_tlbld();
4765
#endif
4766
}
4767

    
4768
/* tlbli */
4769
GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB)
4770
{
4771
#if defined(CONFIG_USER_ONLY)
4772
    GEN_EXCP_PRIVOPC(ctx);
4773
#else
4774
    if (unlikely(!ctx->supervisor)) {
4775
        GEN_EXCP_PRIVOPC(ctx);
4776
        return;
4777
    }
4778
    gen_op_load_gpr_T0(rB(ctx->opcode));
4779
    gen_op_6xx_tlbli();
4780
#endif
4781
}
4782

    
4783
/* 74xx TLB management */
4784
/* tlbld */
4785
GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB)
4786
{
4787
#if defined(CONFIG_USER_ONLY)
4788
    GEN_EXCP_PRIVOPC(ctx);
4789
#else
4790
    if (unlikely(!ctx->supervisor)) {
4791
        GEN_EXCP_PRIVOPC(ctx);
4792
        return;
4793
    }
4794
    gen_op_load_gpr_T0(rB(ctx->opcode));
4795
    gen_op_74xx_tlbld();
4796
#endif
4797
}
4798

    
4799
/* tlbli */
4800
GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB)
4801
{
4802
#if defined(CONFIG_USER_ONLY)
4803
    GEN_EXCP_PRIVOPC(ctx);
4804
#else
4805
    if (unlikely(!ctx->supervisor)) {
4806
        GEN_EXCP_PRIVOPC(ctx);
4807
        return;
4808
    }
4809
    gen_op_load_gpr_T0(rB(ctx->opcode));
4810
    gen_op_74xx_tlbli();
4811
#endif
4812
}
4813

    
4814
/* POWER instructions not in PowerPC 601 */
4815
/* clf */
4816
GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER)
4817
{
4818
    /* Cache line flush: implemented as no-op */
4819
}
4820

    
4821
/* cli */
4822
GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER)
4823
{
4824
    /* Cache line invalidate: privileged and treated as no-op */
4825
#if defined(CONFIG_USER_ONLY)
4826
    GEN_EXCP_PRIVOPC(ctx);
4827
#else
4828
    if (unlikely(!ctx->supervisor)) {
4829
        GEN_EXCP_PRIVOPC(ctx);
4830
        return;
4831
    }
4832
#endif
4833
}
4834

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

    
4841
GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER)
4842
{
4843
#if defined(CONFIG_USER_ONLY)
4844
    GEN_EXCP_PRIVOPC(ctx);
4845
#else
4846
    if (unlikely(!ctx->supervisor)) {
4847
        GEN_EXCP_PRIVOPC(ctx);
4848
        return;
4849
    }
4850
    int ra = rA(ctx->opcode);
4851
    int rd = rD(ctx->opcode);
4852

    
4853
    gen_addr_reg_index(ctx);
4854
    gen_op_POWER_mfsri();
4855
    gen_op_store_T0_gpr(rd);
4856
    if (ra != 0 && ra != rd)
4857
        gen_op_store_T1_gpr(ra);
4858
#endif
4859
}
4860

    
4861
GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER)
4862
{
4863
#if defined(CONFIG_USER_ONLY)
4864
    GEN_EXCP_PRIVOPC(ctx);
4865
#else
4866
    if (unlikely(!ctx->supervisor)) {
4867
        GEN_EXCP_PRIVOPC(ctx);
4868
        return;
4869
    }
4870
    gen_addr_reg_index(ctx);
4871
    gen_op_POWER_rac();
4872
    gen_op_store_T0_gpr(rD(ctx->opcode));
4873
#endif
4874
}
4875

    
4876
GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER)
4877
{
4878
#if defined(CONFIG_USER_ONLY)
4879
    GEN_EXCP_PRIVOPC(ctx);
4880
#else
4881
    if (unlikely(!ctx->supervisor)) {
4882
        GEN_EXCP_PRIVOPC(ctx);
4883
        return;
4884
    }
4885
    gen_op_POWER_rfsvc();
4886
    GEN_SYNC(ctx);
4887
#endif
4888
}
4889

    
4890
/* svc is not implemented for now */
4891

    
4892
/* POWER2 specific instructions */
4893
/* Quad manipulation (load/store two floats at a time) */
4894
#define op_POWER2_lfq() (*gen_op_POWER2_lfq[ctx->mem_idx])()
4895
#define op_POWER2_stfq() (*gen_op_POWER2_stfq[ctx->mem_idx])()
4896
#if defined(CONFIG_USER_ONLY)
4897
static GenOpFunc *gen_op_POWER2_lfq[] = {
4898
    &gen_op_POWER2_lfq_le_raw,
4899
    &gen_op_POWER2_lfq_raw,
4900
};
4901
static GenOpFunc *gen_op_POWER2_stfq[] = {
4902
    &gen_op_POWER2_stfq_le_raw,
4903
    &gen_op_POWER2_stfq_raw,
4904
};
4905
#else
4906
static GenOpFunc *gen_op_POWER2_lfq[] = {
4907
    &gen_op_POWER2_lfq_le_user,
4908
    &gen_op_POWER2_lfq_user,
4909
    &gen_op_POWER2_lfq_le_kernel,
4910
    &gen_op_POWER2_lfq_kernel,
4911
};
4912
static GenOpFunc *gen_op_POWER2_stfq[] = {
4913
    &gen_op_POWER2_stfq_le_user,
4914
    &gen_op_POWER2_stfq_user,
4915
    &gen_op_POWER2_stfq_le_kernel,
4916
    &gen_op_POWER2_stfq_kernel,
4917
};
4918
#endif
4919

    
4920
/* lfq */
4921
GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4922
{
4923
    /* NIP cannot be restored if the memory exception comes from an helper */
4924
    gen_update_nip(ctx, ctx->nip - 4);
4925
    gen_addr_imm_index(ctx, 0);
4926
    op_POWER2_lfq();
4927
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4928
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4929
}
4930

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

    
4936
    /* NIP cannot be restored if the memory exception comes from an helper */
4937
    gen_update_nip(ctx, ctx->nip - 4);
4938
    gen_addr_imm_index(ctx, 0);
4939
    op_POWER2_lfq();
4940
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4941
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4942
    if (ra != 0)
4943
        gen_op_store_T0_gpr(ra);
4944
}
4945

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

    
4951
    /* NIP cannot be restored if the memory exception comes from an helper */
4952
    gen_update_nip(ctx, ctx->nip - 4);
4953
    gen_addr_reg_index(ctx);
4954
    op_POWER2_lfq();
4955
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4956
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4957
    if (ra != 0)
4958
        gen_op_store_T0_gpr(ra);
4959
}
4960

    
4961
/* lfqx */
4962
GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2)
4963
{
4964
    /* NIP cannot be restored if the memory exception comes from an helper */
4965
    gen_update_nip(ctx, ctx->nip - 4);
4966
    gen_addr_reg_index(ctx);
4967
    op_POWER2_lfq();
4968
    gen_op_store_FT0_fpr(rD(ctx->opcode));
4969
    gen_op_store_FT1_fpr(rD(ctx->opcode) + 1);
4970
}
4971

    
4972
/* stfq */
4973
GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2)
4974
{
4975
    /* NIP cannot be restored if the memory exception comes from an helper */
4976
    gen_update_nip(ctx, ctx->nip - 4);
4977
    gen_addr_imm_index(ctx, 0);
4978
    gen_op_load_fpr_FT0(rS(ctx->opcode));
4979
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
4980
    op_POWER2_stfq();
4981
}
4982

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

    
4988
    /* NIP cannot be restored if the memory exception comes from an helper */
4989
    gen_update_nip(ctx, ctx->nip - 4);
4990
    gen_addr_imm_index(ctx, 0);
4991
    gen_op_load_fpr_FT0(rS(ctx->opcode));
4992
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
4993
    op_POWER2_stfq();
4994
    if (ra != 0)
4995
        gen_op_store_T0_gpr(ra);
4996
}
4997

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

    
5003
    /* NIP cannot be restored if the memory exception comes from an helper */
5004
    gen_update_nip(ctx, ctx->nip - 4);
5005
    gen_addr_reg_index(ctx);
5006
    gen_op_load_fpr_FT0(rS(ctx->opcode));
5007
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
5008
    op_POWER2_stfq();
5009
    if (ra != 0)
5010
        gen_op_store_T0_gpr(ra);
5011
}
5012

    
5013
/* stfqx */
5014
GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2)
5015
{
5016
    /* NIP cannot be restored if the memory exception comes from an helper */
5017
    gen_update_nip(ctx, ctx->nip - 4);
5018
    gen_addr_reg_index(ctx);
5019
    gen_op_load_fpr_FT0(rS(ctx->opcode));
5020
    gen_op_load_fpr_FT1(rS(ctx->opcode) + 1);
5021
    op_POWER2_stfq();
5022
}
5023

    
5024
/* BookE specific instructions */
5025
/* XXX: not implemented on 440 ? */
5026
GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_BOOKE_EXT)
5027
{
5028
    /* XXX: TODO */
5029
    GEN_EXCP_INVAL(ctx);
5030
}
5031

    
5032
/* XXX: not implemented on 440 ? */
5033
GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_BOOKE_EXT)
5034
{
5035
#if defined(CONFIG_USER_ONLY)
5036
    GEN_EXCP_PRIVOPC(ctx);
5037
#else
5038
    if (unlikely(!ctx->supervisor)) {
5039
        GEN_EXCP_PRIVOPC(ctx);
5040
        return;
5041
    }
5042
    gen_addr_reg_index(ctx);
5043
    /* Use the same micro-ops as for tlbie */
5044
#if defined(TARGET_PPC64)
5045
    if (ctx->sf_mode)
5046
        gen_op_tlbie_64();
5047
    else
5048
#endif
5049
        gen_op_tlbie();
5050
#endif
5051
}
5052

    
5053
/* All 405 MAC instructions are translated here */
5054
static always_inline void gen_405_mulladd_insn (DisasContext *ctx,
5055
                                                int opc2, int opc3,
5056
                                                int ra, int rb, int rt, int Rc)
5057
{
5058
    gen_op_load_gpr_T0(ra);
5059
    gen_op_load_gpr_T1(rb);
5060
    switch (opc3 & 0x0D) {
5061
    case 0x05:
5062
        /* macchw    - macchw.    - macchwo   - macchwo.   */
5063
        /* macchws   - macchws.   - macchwso  - macchwso.  */
5064
        /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5065
        /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5066
        /* mulchw - mulchw. */
5067
        gen_op_405_mulchw();
5068
        break;
5069
    case 0x04:
5070
        /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5071
        /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5072
        /* mulchwu - mulchwu. */
5073
        gen_op_405_mulchwu();
5074
        break;
5075
    case 0x01:
5076
        /* machhw    - machhw.    - machhwo   - machhwo.   */
5077
        /* machhws   - machhws.   - machhwso  - machhwso.  */
5078
        /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5079
        /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5080
        /* mulhhw - mulhhw. */
5081
        gen_op_405_mulhhw();
5082
        break;
5083
    case 0x00:
5084
        /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5085
        /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5086
        /* mulhhwu - mulhhwu. */
5087
        gen_op_405_mulhhwu();
5088
        break;
5089
    case 0x0D:
5090
        /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5091
        /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5092
        /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5093
        /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5094
        /* mullhw - mullhw. */
5095
        gen_op_405_mullhw();
5096
        break;
5097
    case 0x0C:
5098
        /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5099
        /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5100
        /* mullhwu - mullhwu. */
5101
        gen_op_405_mullhwu();
5102
        break;
5103
    }
5104
    if (opc2 & 0x02) {
5105
        /* nmultiply-and-accumulate (0x0E) */
5106
        gen_op_neg();
5107
    }
5108
    if (opc2 & 0x04) {
5109
        /* (n)multiply-and-accumulate (0x0C - 0x0E) */
5110
        gen_op_load_gpr_T2(rt);
5111
        gen_op_move_T1_T0();
5112
        gen_op_405_add_T0_T2();
5113
    }
5114
    if (opc3 & 0x10) {
5115
        /* Check overflow */
5116
        if (opc3 & 0x01)
5117
            gen_op_check_addo();
5118
        else
5119
            gen_op_405_check_ovu();
5120
    }
5121
    if (opc3 & 0x02) {
5122
        /* Saturate */
5123
        if (opc3 & 0x01)
5124
            gen_op_405_check_sat();
5125
        else
5126
            gen_op_405_check_satu();
5127
    }
5128
    gen_op_store_T0_gpr(rt);
5129
    if (unlikely(Rc) != 0) {
5130
        /* Update Rc0 */
5131
        gen_set_Rc0(ctx);
5132
    }
5133
}
5134

    
5135
#define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
5136
GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)                  \
5137
{                                                                             \
5138
    gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
5139
                         rD(ctx->opcode), Rc(ctx->opcode));                   \
5140
}
5141

    
5142
/* macchw    - macchw.    */
5143
GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5144
/* macchwo   - macchwo.   */
5145
GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5146
/* macchws   - macchws.   */
5147
GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5148
/* macchwso  - macchwso.  */
5149
GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5150
/* macchwsu  - macchwsu.  */
5151
GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5152
/* macchwsuo - macchwsuo. */
5153
GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5154
/* macchwu   - macchwu.   */
5155
GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5156
/* macchwuo  - macchwuo.  */
5157
GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5158
/* machhw    - machhw.    */
5159
GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5160
/* machhwo   - machhwo.   */
5161
GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5162
/* machhws   - machhws.   */
5163
GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5164
/* machhwso  - machhwso.  */
5165
GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5166
/* machhwsu  - machhwsu.  */
5167
GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5168
/* machhwsuo - machhwsuo. */
5169
GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5170
/* machhwu   - machhwu.   */
5171
GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5172
/* machhwuo  - machhwuo.  */
5173
GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5174
/* maclhw    - maclhw.    */
5175
GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5176
/* maclhwo   - maclhwo.   */
5177
GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5178
/* maclhws   - maclhws.   */
5179
GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5180
/* maclhwso  - maclhwso.  */
5181
GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5182
/* maclhwu   - maclhwu.   */
5183
GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5184
/* maclhwuo  - maclhwuo.  */
5185
GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5186
/* maclhwsu  - maclhwsu.  */
5187
GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5188
/* maclhwsuo - maclhwsuo. */
5189
GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5190
/* nmacchw   - nmacchw.   */
5191
GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5192
/* nmacchwo  - nmacchwo.  */
5193
GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5194
/* nmacchws  - nmacchws.  */
5195
GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5196
/* nmacchwso - nmacchwso. */
5197
GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5198
/* nmachhw   - nmachhw.   */
5199
GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5200
/* nmachhwo  - nmachhwo.  */
5201
GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5202
/* nmachhws  - nmachhws.  */
5203
GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5204
/* nmachhwso - nmachhwso. */
5205
GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5206
/* nmaclhw   - nmaclhw.   */
5207
GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5208
/* nmaclhwo  - nmaclhwo.  */
5209
GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5210
/* nmaclhws  - nmaclhws.  */
5211
GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5212
/* nmaclhwso - nmaclhwso. */
5213
GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5214

    
5215
/* mulchw  - mulchw.  */
5216
GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5217
/* mulchwu - mulchwu. */
5218
GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5219
/* mulhhw  - mulhhw.  */
5220
GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5221
/* mulhhwu - mulhhwu. */
5222
GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5223
/* mullhw  - mullhw.  */
5224
GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5225
/* mullhwu - mullhwu. */
5226
GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5227

    
5228
/* mfdcr */
5229
GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_EMB_COMMON)
5230
{
5231
#if defined(CONFIG_USER_ONLY)
5232
    GEN_EXCP_PRIVREG(ctx);
5233
#else
5234
    uint32_t dcrn = SPR(ctx->opcode);
5235

    
5236
    if (unlikely(!ctx->supervisor)) {
5237
        GEN_EXCP_PRIVREG(ctx);
5238
        return;
5239
    }
5240
    gen_op_set_T0(dcrn);
5241
    gen_op_load_dcr();
5242
    gen_op_store_T0_gpr(rD(ctx->opcode));
5243
#endif
5244
}
5245

    
5246
/* mtdcr */
5247
GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_EMB_COMMON)
5248
{
5249
#if defined(CONFIG_USER_ONLY)
5250
    GEN_EXCP_PRIVREG(ctx);
5251
#else
5252
    uint32_t dcrn = SPR(ctx->opcode);
5253

    
5254
    if (unlikely(!ctx->supervisor)) {
5255
        GEN_EXCP_PRIVREG(ctx);
5256
        return;
5257
    }
5258
    gen_op_set_T0(dcrn);
5259
    gen_op_load_gpr_T1(rS(ctx->opcode));
5260
    gen_op_store_dcr();
5261
#endif
5262
}
5263

    
5264
/* mfdcrx */
5265
/* XXX: not implemented on 440 ? */
5266
GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_BOOKE_EXT)
5267
{
5268
#if defined(CONFIG_USER_ONLY)
5269
    GEN_EXCP_PRIVREG(ctx);
5270
#else
5271
    if (unlikely(!ctx->supervisor)) {
5272
        GEN_EXCP_PRIVREG(ctx);
5273
        return;
5274
    }
5275
    gen_op_load_gpr_T0(rA(ctx->opcode));
5276
    gen_op_load_dcr();
5277
    gen_op_store_T0_gpr(rD(ctx->opcode));
5278
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5279
#endif
5280
}
5281

    
5282
/* mtdcrx */
5283
/* XXX: not implemented on 440 ? */
5284
GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_BOOKE_EXT)
5285
{
5286
#if defined(CONFIG_USER_ONLY)
5287
    GEN_EXCP_PRIVREG(ctx);
5288
#else
5289
    if (unlikely(!ctx->supervisor)) {
5290
        GEN_EXCP_PRIVREG(ctx);
5291
        return;
5292
    }
5293
    gen_op_load_gpr_T0(rA(ctx->opcode));
5294
    gen_op_load_gpr_T1(rS(ctx->opcode));
5295
    gen_op_store_dcr();
5296
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5297
#endif
5298
}
5299

    
5300
/* mfdcrux (PPC 460) : user-mode access to DCR */
5301
GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX)
5302
{
5303
    gen_op_load_gpr_T0(rA(ctx->opcode));
5304
    gen_op_load_dcr();
5305
    gen_op_store_T0_gpr(rD(ctx->opcode));
5306
    /* Note: Rc update flag set leads to undefined state of Rc0 */
5307
}
5308

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

    
5318
/* dccci */
5319
GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON)
5320
{
5321
#if defined(CONFIG_USER_ONLY)
5322
    GEN_EXCP_PRIVOPC(ctx);
5323
#else
5324
    if (unlikely(!ctx->supervisor)) {
5325
        GEN_EXCP_PRIVOPC(ctx);
5326
        return;
5327
    }
5328
    /* interpreted as no-op */
5329
#endif
5330
}
5331

    
5332
/* dcread */
5333
GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON)
5334
{
5335
#if defined(CONFIG_USER_ONLY)
5336
    GEN_EXCP_PRIVOPC(ctx);
5337
#else
5338
    if (unlikely(!ctx->supervisor)) {
5339
        GEN_EXCP_PRIVOPC(ctx);
5340
        return;
5341
    }
5342
    gen_addr_reg_index(ctx);
5343
    op_ldst(lwz);
5344
    gen_op_store_T0_gpr(rD(ctx->opcode));
5345
#endif
5346
}
5347

    
5348
/* icbt */
5349
GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT)
5350
{
5351
    /* interpreted as no-op */
5352
    /* XXX: specification say this is treated as a load by the MMU
5353
     *      but does not generate any exception
5354
     */
5355
}
5356

    
5357
/* iccci */
5358
GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON)
5359
{
5360
#if defined(CONFIG_USER_ONLY)
5361
    GEN_EXCP_PRIVOPC(ctx);
5362
#else
5363
    if (unlikely(!ctx->supervisor)) {
5364
        GEN_EXCP_PRIVOPC(ctx);
5365
        return;
5366
    }
5367
    /* interpreted as no-op */
5368
#endif
5369
}
5370

    
5371
/* icread */
5372
GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON)
5373
{
5374
#if defined(CONFIG_USER_ONLY)
5375
    GEN_EXCP_PRIVOPC(ctx);
5376
#else
5377
    if (unlikely(!ctx->supervisor)) {
5378
        GEN_EXCP_PRIVOPC(ctx);
5379
        return;
5380
    }
5381
    /* interpreted as no-op */
5382
#endif
5383
}
5384

    
5385
/* rfci (supervisor only) */
5386
GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP)
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
    /* Restore CPU state */
5396
    gen_op_40x_rfci();
5397
    GEN_SYNC(ctx);
5398
#endif
5399
}
5400

    
5401
GEN_HANDLER(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE)
5402
{
5403
#if defined(CONFIG_USER_ONLY)
5404
    GEN_EXCP_PRIVOPC(ctx);
5405
#else
5406
    if (unlikely(!ctx->supervisor)) {
5407
        GEN_EXCP_PRIVOPC(ctx);
5408
        return;
5409
    }
5410
    /* Restore CPU state */
5411
    gen_op_rfci();
5412
    GEN_SYNC(ctx);
5413
#endif
5414
}
5415

    
5416
/* BookE specific */
5417
/* XXX: not implemented on 440 ? */
5418
GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_BOOKE_EXT)
5419
{
5420
#if defined(CONFIG_USER_ONLY)
5421
    GEN_EXCP_PRIVOPC(ctx);
5422
#else
5423
    if (unlikely(!ctx->supervisor)) {
5424
        GEN_EXCP_PRIVOPC(ctx);
5425
        return;
5426
    }
5427
    /* Restore CPU state */
5428
    gen_op_rfdi();
5429
    GEN_SYNC(ctx);
5430
#endif
5431
}
5432

    
5433
/* XXX: not implemented on 440 ? */
5434
GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI)
5435
{
5436
#if defined(CONFIG_USER_ONLY)
5437
    GEN_EXCP_PRIVOPC(ctx);
5438
#else
5439
    if (unlikely(!ctx->supervisor)) {
5440
        GEN_EXCP_PRIVOPC(ctx);
5441
        return;
5442
    }
5443
    /* Restore CPU state */
5444
    gen_op_rfmci();
5445
    GEN_SYNC(ctx);
5446
#endif
5447
}
5448

    
5449
/* TLB management - PowerPC 405 implementation */
5450
/* tlbre */
5451
GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB)
5452
{
5453
#if defined(CONFIG_USER_ONLY)
5454
    GEN_EXCP_PRIVOPC(ctx);
5455
#else
5456
    if (unlikely(!ctx->supervisor)) {
5457
        GEN_EXCP_PRIVOPC(ctx);
5458
        return;
5459
    }
5460
    switch (rB(ctx->opcode)) {
5461
    case 0:
5462
        gen_op_load_gpr_T0(rA(ctx->opcode));
5463
        gen_op_4xx_tlbre_hi();
5464
        gen_op_store_T0_gpr(rD(ctx->opcode));
5465
        break;
5466
    case 1:
5467
        gen_op_load_gpr_T0(rA(ctx->opcode));
5468
        gen_op_4xx_tlbre_lo();
5469
        gen_op_store_T0_gpr(rD(ctx->opcode));
5470
        break;
5471
    default:
5472
        GEN_EXCP_INVAL(ctx);
5473
        break;
5474
    }
5475
#endif
5476
}
5477

    
5478
/* tlbsx - tlbsx. */
5479
GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB)
5480
{
5481
#if defined(CONFIG_USER_ONLY)
5482
    GEN_EXCP_PRIVOPC(ctx);
5483
#else
5484
    if (unlikely(!ctx->supervisor)) {
5485
        GEN_EXCP_PRIVOPC(ctx);
5486
        return;
5487
    }
5488
    gen_addr_reg_index(ctx);
5489
    gen_op_4xx_tlbsx();
5490
    if (Rc(ctx->opcode))
5491
        gen_op_4xx_tlbsx_check();
5492
    gen_op_store_T0_gpr(rD(ctx->opcode));
5493
#endif
5494
}
5495

    
5496
/* tlbwe */
5497
GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB)
5498
{
5499
#if defined(CONFIG_USER_ONLY)
5500
    GEN_EXCP_PRIVOPC(ctx);
5501
#else
5502
    if (unlikely(!ctx->supervisor)) {
5503
        GEN_EXCP_PRIVOPC(ctx);
5504
        return;
5505
    }
5506
    switch (rB(ctx->opcode)) {
5507
    case 0:
5508
        gen_op_load_gpr_T0(rA(ctx->opcode));
5509
        gen_op_load_gpr_T1(rS(ctx->opcode));
5510
        gen_op_4xx_tlbwe_hi();
5511
        break;
5512
    case 1:
5513
        gen_op_load_gpr_T0(rA(ctx->opcode));
5514
        gen_op_load_gpr_T1(rS(ctx->opcode));
5515
        gen_op_4xx_tlbwe_lo();
5516
        break;
5517
    default:
5518
        GEN_EXCP_INVAL(ctx);
5519
        break;
5520
    }
5521
#endif
5522
}
5523

    
5524
/* TLB management - PowerPC 440 implementation */
5525
/* tlbre */
5526
GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE)
5527
{
5528
#if defined(CONFIG_USER_ONLY)
5529
    GEN_EXCP_PRIVOPC(ctx);
5530
#else
5531
    if (unlikely(!ctx->supervisor)) {
5532
        GEN_EXCP_PRIVOPC(ctx);
5533
        return;
5534
    }
5535
    switch (rB(ctx->opcode)) {
5536
    case 0:
5537
    case 1:
5538
    case 2:
5539
        gen_op_load_gpr_T0(rA(ctx->opcode));
5540
        gen_op_440_tlbre(rB(ctx->opcode));
5541
        gen_op_store_T0_gpr(rD(ctx->opcode));
5542
        break;
5543
    default:
5544
        GEN_EXCP_INVAL(ctx);
5545
        break;
5546
    }
5547
#endif
5548
}
5549

    
5550
/* tlbsx - tlbsx. */
5551
GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE)
5552
{
5553
#if defined(CONFIG_USER_ONLY)
5554
    GEN_EXCP_PRIVOPC(ctx);
5555
#else
5556
    if (unlikely(!ctx->supervisor)) {
5557
        GEN_EXCP_PRIVOPC(ctx);
5558
        return;
5559
    }
5560
    gen_addr_reg_index(ctx);
5561
    gen_op_440_tlbsx();
5562
    if (Rc(ctx->opcode))
5563
        gen_op_4xx_tlbsx_check();
5564
    gen_op_store_T0_gpr(rD(ctx->opcode));
5565
#endif
5566
}
5567

    
5568
/* tlbwe */
5569
GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE)
5570
{
5571
#if defined(CONFIG_USER_ONLY)
5572
    GEN_EXCP_PRIVOPC(ctx);
5573
#else
5574
    if (unlikely(!ctx->supervisor)) {
5575
        GEN_EXCP_PRIVOPC(ctx);
5576
        return;
5577
    }
5578
    switch (rB(ctx->opcode)) {
5579
    case 0:
5580
    case 1:
5581
    case 2:
5582
        gen_op_load_gpr_T0(rA(ctx->opcode));
5583
        gen_op_load_gpr_T1(rS(ctx->opcode));
5584
        gen_op_440_tlbwe(rB(ctx->opcode));
5585
        break;
5586
    default:
5587
        GEN_EXCP_INVAL(ctx);
5588
        break;
5589
    }
5590
#endif
5591
}
5592

    
5593
/* wrtee */
5594
GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_EMB_COMMON)
5595
{
5596
#if defined(CONFIG_USER_ONLY)
5597
    GEN_EXCP_PRIVOPC(ctx);
5598
#else
5599
    if (unlikely(!ctx->supervisor)) {
5600
        GEN_EXCP_PRIVOPC(ctx);
5601
        return;
5602
    }
5603
    gen_op_load_gpr_T0(rD(ctx->opcode));
5604
    gen_op_wrte();
5605
    /* Stop translation to have a chance to raise an exception
5606
     * if we just set msr_ee to 1
5607
     */
5608
    GEN_STOP(ctx);
5609
#endif
5610
}
5611

    
5612
/* wrteei */
5613
GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000EFC01, PPC_EMB_COMMON)
5614
{
5615
#if defined(CONFIG_USER_ONLY)
5616
    GEN_EXCP_PRIVOPC(ctx);
5617
#else
5618
    if (unlikely(!ctx->supervisor)) {
5619
        GEN_EXCP_PRIVOPC(ctx);
5620
        return;
5621
    }
5622
    gen_op_set_T0(ctx->opcode & 0x00010000);
5623
    gen_op_wrte();
5624
    /* Stop translation to have a chance to raise an exception
5625
     * if we just set msr_ee to 1
5626
     */
5627
    GEN_STOP(ctx);
5628
#endif
5629
}
5630

    
5631
/* PowerPC 440 specific instructions */
5632
/* dlmzb */
5633
GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC)
5634
{
5635
    gen_op_load_gpr_T0(rS(ctx->opcode));
5636
    gen_op_load_gpr_T1(rB(ctx->opcode));
5637
    gen_op_440_dlmzb();
5638
    gen_op_store_T0_gpr(rA(ctx->opcode));
5639
    gen_op_store_xer_bc();
5640
    if (Rc(ctx->opcode)) {
5641
        gen_op_440_dlmzb_update_Rc();
5642
        gen_op_store_T0_crf(0);
5643
    }
5644
}
5645

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

    
5652
/* msync replaces sync on 440 */
5653
GEN_HANDLER(msync, 0x1F, 0x16, 0x12, 0x03FFF801, PPC_BOOKE)
5654
{
5655
    /* interpreted as no-op */
5656
}
5657

    
5658
/* icbt */
5659
GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001, PPC_BOOKE)
5660
{
5661
    /* interpreted as no-op */
5662
    /* XXX: specification say this is treated as a load by the MMU
5663
     *      but does not generate any exception
5664
     */
5665
}
5666

    
5667
/***                      Altivec vector extension                         ***/
5668
/* Altivec registers moves */
5669
GEN32(gen_op_load_avr_A0, gen_op_load_avr_A0_avr);
5670
GEN32(gen_op_load_avr_A1, gen_op_load_avr_A1_avr);
5671
GEN32(gen_op_load_avr_A2, gen_op_load_avr_A2_avr);
5672

    
5673
GEN32(gen_op_store_A0_avr, gen_op_store_A0_avr_avr);
5674
GEN32(gen_op_store_A1_avr, gen_op_store_A1_avr_avr);
5675
#if 0 // unused
5676
GEN32(gen_op_store_A2_avr, gen_op_store_A2_avr_avr);
5677
#endif
5678

    
5679
#define op_vr_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
5680
#if defined(CONFIG_USER_ONLY)
5681
#if defined(TARGET_PPC64)
5682
/* User-mode only - 64 bits mode */
5683
#define OP_VR_LD_TABLE(name)                                                  \
5684
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
5685
    &gen_op_vr_l##name##_raw,                                                 \
5686
    &gen_op_vr_l##name##_le_raw,                                              \
5687
    &gen_op_vr_l##name##_64_raw,                                              \
5688
    &gen_op_vr_l##name##_le_64_raw,                                           \
5689
};
5690
#define OP_VR_ST_TABLE(name)                                                  \
5691
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
5692
    &gen_op_vr_st##name##_raw,                                                \
5693
    &gen_op_vr_st##name##_le_raw,                                             \
5694
    &gen_op_vr_st##name##_64_raw,                                             \
5695
    &gen_op_vr_st##name##_le_64_raw,                                          \
5696
};
5697
#else /* defined(TARGET_PPC64) */
5698
/* User-mode only - 32 bits mode */
5699
#define OP_VR_LD_TABLE(name)                                                  \
5700
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
5701
    &gen_op_vr_l##name##_raw,                                                 \
5702
    &gen_op_vr_l##name##_le_raw,                                              \
5703
};
5704
#define OP_VR_ST_TABLE(name)                                                  \
5705
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
5706
    &gen_op_vr_st##name##_raw,                                                \
5707
    &gen_op_vr_st##name##_le_raw,                                             \
5708
};
5709
#endif /* defined(TARGET_PPC64) */
5710
#else /* defined(CONFIG_USER_ONLY) */
5711
#if defined(TARGET_PPC64H)
5712
/* Full system with hypervisor mode */
5713
#define OP_VR_LD_TABLE(name)                                                  \
5714
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
5715
    &gen_op_vr_l##name##_user,                                                \
5716
    &gen_op_vr_l##name##_le_user,                                             \
5717
    &gen_op_vr_l##name##_64_user,                                             \
5718
    &gen_op_vr_l##name##_le_64_user,                                          \
5719
    &gen_op_vr_l##name##_kernel,                                              \
5720
    &gen_op_vr_l##name##_le_kernel,                                           \
5721
    &gen_op_vr_l##name##_64_kernel,                                           \
5722
    &gen_op_vr_l##name##_le_64_kernel,                                        \
5723
    &gen_op_vr_l##name##_hypv,                                                \
5724
    &gen_op_vr_l##name##_le_hypv,                                             \
5725
    &gen_op_vr_l##name##_64_hypv,                                             \
5726
    &gen_op_vr_l##name##_le_64_hypv,                                          \
5727
};
5728
#define OP_VR_ST_TABLE(name)                                                  \
5729
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
5730
    &gen_op_vr_st##name##_user,                                               \
5731
    &gen_op_vr_st##name##_le_user,                                            \
5732
    &gen_op_vr_st##name##_64_user,                                            \
5733
    &gen_op_vr_st##name##_le_64_user,                                         \
5734
    &gen_op_vr_st##name##_kernel,                                             \
5735
    &gen_op_vr_st##name##_le_kernel,                                          \
5736
    &gen_op_vr_st##name##_64_kernel,                                          \
5737
    &gen_op_vr_st##name##_le_64_kernel,                                       \
5738
    &gen_op_vr_st##name##_hypv,                                               \
5739
    &gen_op_vr_st##name##_le_hypv,                                            \
5740
    &gen_op_vr_st##name##_64_hypv,                                            \
5741
    &gen_op_vr_st##name##_le_64_hypv,                                         \
5742
};
5743
#elif defined(TARGET_PPC64)
5744
/* Full system - 64 bits mode */
5745
#define OP_VR_LD_TABLE(name)                                                  \
5746
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
5747
    &gen_op_vr_l##name##_user,                                                \
5748
    &gen_op_vr_l##name##_le_user,                                             \
5749
    &gen_op_vr_l##name##_64_user,                                             \
5750
    &gen_op_vr_l##name##_le_64_user,                                          \
5751
    &gen_op_vr_l##name##_kernel,                                              \
5752
    &gen_op_vr_l##name##_le_kernel,                                           \
5753
    &gen_op_vr_l##name##_64_kernel,                                           \
5754
    &gen_op_vr_l##name##_le_64_kernel,                                        \
5755
};
5756
#define OP_VR_ST_TABLE(name)                                                  \
5757
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
5758
    &gen_op_vr_st##name##_user,                                               \
5759
    &gen_op_vr_st##name##_le_user,                                            \
5760
    &gen_op_vr_st##name##_64_user,                                            \
5761
    &gen_op_vr_st##name##_le_64_user,                                         \
5762
    &gen_op_vr_st##name##_kernel,                                             \
5763
    &gen_op_vr_st##name##_le_kernel,                                          \
5764
    &gen_op_vr_st##name##_64_kernel,                                          \
5765
    &gen_op_vr_st##name##_le_64_kernel,                                       \
5766
};
5767
#else /* defined(TARGET_PPC64) */
5768
/* Full system - 32 bits mode */
5769
#define OP_VR_LD_TABLE(name)                                                  \
5770
static GenOpFunc *gen_op_vr_l##name[] = {                                     \
5771
    &gen_op_vr_l##name##_user,                                                \
5772
    &gen_op_vr_l##name##_le_user,                                             \
5773
    &gen_op_vr_l##name##_kernel,                                              \
5774
    &gen_op_vr_l##name##_le_kernel,                                           \
5775
};
5776
#define OP_VR_ST_TABLE(name)                                                  \
5777
static GenOpFunc *gen_op_vr_st##name[] = {                                    \
5778
    &gen_op_vr_st##name##_user,                                               \
5779
    &gen_op_vr_st##name##_le_user,                                            \
5780
    &gen_op_vr_st##name##_kernel,                                             \
5781
    &gen_op_vr_st##name##_le_kernel,                                          \
5782
};
5783
#endif /* defined(TARGET_PPC64) */
5784
#endif /* defined(CONFIG_USER_ONLY) */
5785

    
5786
#define GEN_VR_LDX(name, opc2, opc3)                                          \
5787
GEN_HANDLER(l##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)               \
5788
{                                                                             \
5789
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5790
        GEN_EXCP_NO_VR(ctx);                                                  \
5791
        return;                                                               \
5792
    }                                                                         \
5793
    gen_addr_reg_index(ctx);                                                  \
5794
    op_vr_ldst(vr_l##name);                                                   \
5795
    gen_op_store_A0_avr(rD(ctx->opcode));                                     \
5796
}
5797

    
5798
#define GEN_VR_STX(name, opc2, opc3)                                          \
5799
GEN_HANDLER(st##name, 0x1F, opc2, opc3, 0x00000001, PPC_ALTIVEC)              \
5800
{                                                                             \
5801
    if (unlikely(!ctx->altivec_enabled)) {                                    \
5802
        GEN_EXCP_NO_VR(ctx);                                                  \
5803
        return;                                                               \
5804
    }                                                                         \
5805
    gen_addr_reg_index(ctx);                                                  \
5806
    gen_op_load_avr_A0(rS(ctx->opcode));                                      \
5807
    op_vr_ldst(vr_st##name);                                                  \
5808
}
5809

    
5810
OP_VR_LD_TABLE(vx);
5811
GEN_VR_LDX(vx, 0x07, 0x03);
5812
/* As we don't emulate the cache, lvxl is stricly equivalent to lvx */
5813
#define gen_op_vr_lvxl gen_op_vr_lvx
5814
GEN_VR_LDX(vxl, 0x07, 0x0B);
5815

    
5816
OP_VR_ST_TABLE(vx);
5817
GEN_VR_STX(vx, 0x07, 0x07);
5818
/* As we don't emulate the cache, stvxl is stricly equivalent to stvx */
5819
#define gen_op_vr_stvxl gen_op_vr_stvx
5820
GEN_VR_STX(vxl, 0x07, 0x0F);
5821

    
5822
/***                           SPE extension                               ***/
5823

    
5824
/* Register moves */
5825
#if TARGET_GPR_BITS < 64
5826

    
5827
GEN32(gen_op_load_gpr64_T0, gen_op_load_gpr64_T0_gpr);
5828
GEN32(gen_op_load_gpr64_T1, gen_op_load_gpr64_T1_gpr);
5829
#if 0 // unused
5830
GEN32(gen_op_load_gpr64_T2, gen_op_load_gpr64_T2_gpr);
5831
#endif
5832

    
5833
GEN32(gen_op_store_T0_gpr64, gen_op_store_T0_gpr64_gpr);
5834
GEN32(gen_op_store_T1_gpr64, gen_op_store_T1_gpr64_gpr);
5835
#if 0 // unused
5836
GEN32(gen_op_store_T2_gpr64, gen_op_store_T2_gpr64_gpr);
5837
#endif
5838

    
5839
#else /* TARGET_GPR_BITS < 64 */
5840

    
5841
/* No specific load/store functions: GPRs are already 64 bits */
5842
#define gen_op_load_gpr64_T0 gen_op_load_gpr_T0
5843
#define gen_op_load_gpr64_T1 gen_op_load_gpr_T1
5844
#if 0 // unused
5845
#define gen_op_load_gpr64_T2 gen_op_load_gpr_T2
5846
#endif
5847

    
5848
#define gen_op_store_T0_gpr64 gen_op_store_T0_gpr
5849
#define gen_op_store_T1_gpr64 gen_op_store_T1_gpr
5850
#if 0 // unused
5851
#define gen_op_store_T2_gpr64 gen_op_store_T2_gpr
5852
#endif
5853

    
5854
#endif /* TARGET_GPR_BITS < 64 */
5855

    
5856
#define GEN_SPE(name0, name1, opc2, opc3, inval, type)                        \
5857
GEN_HANDLER(name0##_##name1, 0x04, opc2, opc3, inval, type)                   \
5858
{                                                                             \
5859
    if (Rc(ctx->opcode))                                                      \
5860
        gen_##name1(ctx);                                                     \
5861
    else                                                                      \
5862
        gen_##name0(ctx);                                                     \
5863
}
5864

    
5865
/* Handler for undefined SPE opcodes */
5866
static always_inline void gen_speundef (DisasContext *ctx)
5867
{
5868
    GEN_EXCP_INVAL(ctx);
5869
}
5870

    
5871
/* SPE load and stores */
5872
static always_inline void gen_addr_spe_imm_index (DisasContext *ctx, int sh)
5873
{
5874
    target_long simm = rB(ctx->opcode);
5875

    
5876
    if (rA(ctx->opcode) == 0) {
5877
        gen_set_T0(simm << sh);
5878
    } else {
5879
        gen_op_load_gpr_T0(rA(ctx->opcode));
5880
        if (likely(simm != 0))
5881
            gen_op_addi(simm << sh);
5882
    }
5883
}
5884

    
5885
#define op_spe_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
5886
#if defined(CONFIG_USER_ONLY)
5887
#if defined(TARGET_PPC64)
5888
/* User-mode only - 64 bits mode */
5889
#define OP_SPE_LD_TABLE(name)                                                 \
5890
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5891
    &gen_op_spe_l##name##_raw,                                                \
5892
    &gen_op_spe_l##name##_le_raw,                                             \
5893
    &gen_op_spe_l##name##_64_raw,                                             \
5894
    &gen_op_spe_l##name##_le_64_raw,                                          \
5895
};
5896
#define OP_SPE_ST_TABLE(name)                                                 \
5897
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5898
    &gen_op_spe_st##name##_raw,                                               \
5899
    &gen_op_spe_st##name##_le_raw,                                            \
5900
    &gen_op_spe_st##name##_64_raw,                                            \
5901
    &gen_op_spe_st##name##_le_64_raw,                                         \
5902
};
5903
#else /* defined(TARGET_PPC64) */
5904
/* User-mode only - 32 bits mode */
5905
#define OP_SPE_LD_TABLE(name)                                                 \
5906
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5907
    &gen_op_spe_l##name##_raw,                                                \
5908
    &gen_op_spe_l##name##_le_raw,                                             \
5909
};
5910
#define OP_SPE_ST_TABLE(name)                                                 \
5911
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5912
    &gen_op_spe_st##name##_raw,                                               \
5913
    &gen_op_spe_st##name##_le_raw,                                            \
5914
};
5915
#endif /* defined(TARGET_PPC64) */
5916
#else /* defined(CONFIG_USER_ONLY) */
5917
#if defined(TARGET_PPC64H)
5918
/* Full system with hypervisor mode */
5919
#define OP_SPE_LD_TABLE(name)                                                 \
5920
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5921
    &gen_op_spe_l##name##_user,                                               \
5922
    &gen_op_spe_l##name##_le_user,                                            \
5923
    &gen_op_spe_l##name##_64_user,                                            \
5924
    &gen_op_spe_l##name##_le_64_user,                                         \
5925
    &gen_op_spe_l##name##_kernel,                                             \
5926
    &gen_op_spe_l##name##_le_kernel,                                          \
5927
    &gen_op_spe_l##name##_64_kernel,                                          \
5928
    &gen_op_spe_l##name##_le_64_kernel,                                       \
5929
    &gen_op_spe_l##name##_hypv,                                               \
5930
    &gen_op_spe_l##name##_le_hypv,                                            \
5931
    &gen_op_spe_l##name##_64_hypv,                                            \
5932
    &gen_op_spe_l##name##_le_64_hypv,                                         \
5933
};
5934
#define OP_SPE_ST_TABLE(name)                                                 \
5935
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5936
    &gen_op_spe_st##name##_user,                                              \
5937
    &gen_op_spe_st##name##_le_user,                                           \
5938
    &gen_op_spe_st##name##_64_user,                                           \
5939
    &gen_op_spe_st##name##_le_64_user,                                        \
5940
    &gen_op_spe_st##name##_kernel,                                            \
5941
    &gen_op_spe_st##name##_le_kernel,                                         \
5942
    &gen_op_spe_st##name##_64_kernel,                                         \
5943
    &gen_op_spe_st##name##_le_64_kernel,                                      \
5944
    &gen_op_spe_st##name##_hypv,                                              \
5945
    &gen_op_spe_st##name##_le_hypv,                                           \
5946
    &gen_op_spe_st##name##_64_hypv,                                           \
5947
    &gen_op_spe_st##name##_le_64_hypv,                                        \
5948
};
5949
#elif defined(TARGET_PPC64)
5950
/* Full system - 64 bits mode */
5951
#define OP_SPE_LD_TABLE(name)                                                 \
5952
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5953
    &gen_op_spe_l##name##_user,                                               \
5954
    &gen_op_spe_l##name##_le_user,                                            \
5955
    &gen_op_spe_l##name##_64_user,                                            \
5956
    &gen_op_spe_l##name##_le_64_user,                                         \
5957
    &gen_op_spe_l##name##_kernel,                                             \
5958
    &gen_op_spe_l##name##_le_kernel,                                          \
5959
    &gen_op_spe_l##name##_64_kernel,                                          \
5960
    &gen_op_spe_l##name##_le_64_kernel,                                       \
5961
};
5962
#define OP_SPE_ST_TABLE(name)                                                 \
5963
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5964
    &gen_op_spe_st##name##_user,                                              \
5965
    &gen_op_spe_st##name##_le_user,                                           \
5966
    &gen_op_spe_st##name##_64_user,                                           \
5967
    &gen_op_spe_st##name##_le_64_user,                                        \
5968
    &gen_op_spe_st##name##_kernel,                                            \
5969
    &gen_op_spe_st##name##_le_kernel,                                         \
5970
    &gen_op_spe_st##name##_64_kernel,                                         \
5971
    &gen_op_spe_st##name##_le_64_kernel,                                      \
5972
};
5973
#else /* defined(TARGET_PPC64) */
5974
/* Full system - 32 bits mode */
5975
#define OP_SPE_LD_TABLE(name)                                                 \
5976
static GenOpFunc *gen_op_spe_l##name[] = {                                    \
5977
    &gen_op_spe_l##name##_user,                                               \
5978
    &gen_op_spe_l##name##_le_user,                                            \
5979
    &gen_op_spe_l##name##_kernel,                                             \
5980
    &gen_op_spe_l##name##_le_kernel,                                          \
5981
};
5982
#define OP_SPE_ST_TABLE(name)                                                 \
5983
static GenOpFunc *gen_op_spe_st##name[] = {                                   \
5984
    &gen_op_spe_st##name##_user,                                              \
5985
    &gen_op_spe_st##name##_le_user,                                           \
5986
    &gen_op_spe_st##name##_kernel,                                            \
5987
    &gen_op_spe_st##name##_le_kernel,                                         \
5988
};
5989
#endif /* defined(TARGET_PPC64) */
5990
#endif /* defined(CONFIG_USER_ONLY) */
5991

    
5992
#define GEN_SPE_LD(name, sh)                                                  \
5993
static always_inline void gen_evl##name (DisasContext *ctx)                   \
5994
{                                                                             \
5995
    if (unlikely(!ctx->spe_enabled)) {                                        \
5996
        GEN_EXCP_NO_AP(ctx);                                                  \
5997
        return;                                                               \
5998
    }                                                                         \
5999
    gen_addr_spe_imm_index(ctx, sh);                                          \
6000
    op_spe_ldst(spe_l##name);                                                 \
6001
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
6002
}
6003

    
6004
#define GEN_SPE_LDX(name)                                                     \
6005
static always_inline void gen_evl##name##x (DisasContext *ctx)                \
6006
{                                                                             \
6007
    if (unlikely(!ctx->spe_enabled)) {                                        \
6008
        GEN_EXCP_NO_AP(ctx);                                                  \
6009
        return;                                                               \
6010
    }                                                                         \
6011
    gen_addr_reg_index(ctx);                                                  \
6012
    op_spe_ldst(spe_l##name);                                                 \
6013
    gen_op_store_T1_gpr64(rD(ctx->opcode));                                   \
6014
}
6015

    
6016
#define GEN_SPEOP_LD(name, sh)                                                \
6017
OP_SPE_LD_TABLE(name);                                                        \
6018
GEN_SPE_LD(name, sh);                                                         \
6019
GEN_SPE_LDX(name)
6020

    
6021
#define GEN_SPE_ST(name, sh)                                                  \
6022
static always_inline void gen_evst##name (DisasContext *ctx)                  \
6023
{                                                                             \
6024
    if (unlikely(!ctx->spe_enabled)) {                                        \
6025
        GEN_EXCP_NO_AP(ctx);                                                  \
6026
        return;                                                               \
6027
    }                                                                         \
6028
    gen_addr_spe_imm_index(ctx, sh);                                          \
6029
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
6030
    op_spe_ldst(spe_st##name);                                                \
6031
}
6032

    
6033
#define GEN_SPE_STX(name)                                                     \
6034
static always_inline void gen_evst##name##x (DisasContext *ctx)               \
6035
{                                                                             \
6036
    if (unlikely(!ctx->spe_enabled)) {                                        \
6037
        GEN_EXCP_NO_AP(ctx);                                                  \
6038
        return;                                                               \
6039
    }                                                                         \
6040
    gen_addr_reg_index(ctx);                                                  \
6041
    gen_op_load_gpr64_T1(rS(ctx->opcode));                                    \
6042
    op_spe_ldst(spe_st##name);                                                \
6043
}
6044

    
6045
#define GEN_SPEOP_ST(name, sh)                                                \
6046
OP_SPE_ST_TABLE(name);                                                        \
6047
GEN_SPE_ST(name, sh);                                                         \
6048
GEN_SPE_STX(name)
6049

    
6050
#define GEN_SPEOP_LDST(name, sh)                                              \
6051
GEN_SPEOP_LD(name, sh);                                                       \
6052
GEN_SPEOP_ST(name, sh)
6053

    
6054
/* SPE arithmetic and logic */
6055
#define GEN_SPEOP_ARITH2(name)                                                \
6056
static always_inline void gen_##name (DisasContext *ctx)                      \
6057
{                                                                             \
6058
    if (unlikely(!ctx->spe_enabled)) {                                        \
6059
        GEN_EXCP_NO_AP(ctx);                                                  \
6060
        return;                                                               \
6061
    }                                                                         \
6062
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
6063
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
6064
    gen_op_##name();                                                          \
6065
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
6066
}
6067

    
6068
#define GEN_SPEOP_ARITH1(name)                                                \
6069
static always_inline void gen_##name (DisasContext *ctx)                      \
6070
{                                                                             \
6071
    if (unlikely(!ctx->spe_enabled)) {                                        \
6072
        GEN_EXCP_NO_AP(ctx);                                                  \
6073
        return;                                                               \
6074
    }                                                                         \
6075
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
6076
    gen_op_##name();                                                          \
6077
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
6078
}
6079

    
6080
#define GEN_SPEOP_COMP(name)                                                  \
6081
static always_inline void gen_##name (DisasContext *ctx)                      \
6082
{                                                                             \
6083
    if (unlikely(!ctx->spe_enabled)) {                                        \
6084
        GEN_EXCP_NO_AP(ctx);                                                  \
6085
        return;                                                               \
6086
    }                                                                         \
6087
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
6088
    gen_op_load_gpr64_T1(rB(ctx->opcode));                                    \
6089
    gen_op_##name();                                                          \
6090
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
6091
}
6092

    
6093
/* Logical */
6094
GEN_SPEOP_ARITH2(evand);
6095
GEN_SPEOP_ARITH2(evandc);
6096
GEN_SPEOP_ARITH2(evxor);
6097
GEN_SPEOP_ARITH2(evor);
6098
GEN_SPEOP_ARITH2(evnor);
6099
GEN_SPEOP_ARITH2(eveqv);
6100
GEN_SPEOP_ARITH2(evorc);
6101
GEN_SPEOP_ARITH2(evnand);
6102
GEN_SPEOP_ARITH2(evsrwu);
6103
GEN_SPEOP_ARITH2(evsrws);
6104
GEN_SPEOP_ARITH2(evslw);
6105
GEN_SPEOP_ARITH2(evrlw);
6106
GEN_SPEOP_ARITH2(evmergehi);
6107
GEN_SPEOP_ARITH2(evmergelo);
6108
GEN_SPEOP_ARITH2(evmergehilo);
6109
GEN_SPEOP_ARITH2(evmergelohi);
6110

    
6111
/* Arithmetic */
6112
GEN_SPEOP_ARITH2(evaddw);
6113
GEN_SPEOP_ARITH2(evsubfw);
6114
GEN_SPEOP_ARITH1(evabs);
6115
GEN_SPEOP_ARITH1(evneg);
6116
GEN_SPEOP_ARITH1(evextsb);
6117
GEN_SPEOP_ARITH1(evextsh);
6118
GEN_SPEOP_ARITH1(evrndw);
6119
GEN_SPEOP_ARITH1(evcntlzw);
6120
GEN_SPEOP_ARITH1(evcntlsw);
6121
static always_inline void gen_brinc (DisasContext *ctx)
6122
{
6123
    /* Note: brinc is usable even if SPE is disabled */
6124
    gen_op_load_gpr_T0(rA(ctx->opcode));
6125
    gen_op_load_gpr_T1(rB(ctx->opcode));
6126
    gen_op_brinc();
6127
    gen_op_store_T0_gpr(rD(ctx->opcode));
6128
}
6129

    
6130
#define GEN_SPEOP_ARITH_IMM2(name)                                            \
6131
static always_inline void gen_##name##i (DisasContext *ctx)                   \
6132
{                                                                             \
6133
    if (unlikely(!ctx->spe_enabled)) {                                        \
6134
        GEN_EXCP_NO_AP(ctx);                                                  \
6135
        return;                                                               \
6136
    }                                                                         \
6137
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
6138
    gen_op_splatwi_T1_64(rA(ctx->opcode));                                    \
6139
    gen_op_##name();                                                          \
6140
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
6141
}
6142

    
6143
#define GEN_SPEOP_LOGIC_IMM2(name)                                            \
6144
static always_inline void gen_##name##i (DisasContext *ctx)                   \
6145
{                                                                             \
6146
    if (unlikely(!ctx->spe_enabled)) {                                        \
6147
        GEN_EXCP_NO_AP(ctx);                                                  \
6148
        return;                                                               \
6149
    }                                                                         \
6150
    gen_op_load_gpr64_T0(rA(ctx->opcode));                                    \
6151
    gen_op_splatwi_T1_64(rB(ctx->opcode));                                    \
6152
    gen_op_##name();                                                          \
6153
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
6154
}
6155

    
6156
GEN_SPEOP_ARITH_IMM2(evaddw);
6157
#define gen_evaddiw gen_evaddwi
6158
GEN_SPEOP_ARITH_IMM2(evsubfw);
6159
#define gen_evsubifw gen_evsubfwi
6160
GEN_SPEOP_LOGIC_IMM2(evslw);
6161
GEN_SPEOP_LOGIC_IMM2(evsrwu);
6162
#define gen_evsrwis gen_evsrwsi
6163
GEN_SPEOP_LOGIC_IMM2(evsrws);
6164
#define gen_evsrwiu gen_evsrwui
6165
GEN_SPEOP_LOGIC_IMM2(evrlw);
6166

    
6167
static always_inline void gen_evsplati (DisasContext *ctx)
6168
{
6169
    int32_t imm = (int32_t)(rA(ctx->opcode) << 27) >> 27;
6170

    
6171
    gen_op_splatwi_T0_64(imm);
6172
    gen_op_store_T0_gpr64(rD(ctx->opcode));
6173
}
6174

    
6175
static always_inline void gen_evsplatfi (DisasContext *ctx)
6176
{
6177
    uint32_t imm = rA(ctx->opcode) << 27;
6178

    
6179
    gen_op_splatwi_T0_64(imm);
6180
    gen_op_store_T0_gpr64(rD(ctx->opcode));
6181
}
6182

    
6183
/* Comparison */
6184
GEN_SPEOP_COMP(evcmpgtu);
6185
GEN_SPEOP_COMP(evcmpgts);
6186
GEN_SPEOP_COMP(evcmpltu);
6187
GEN_SPEOP_COMP(evcmplts);
6188
GEN_SPEOP_COMP(evcmpeq);
6189

    
6190
GEN_SPE(evaddw,         speundef,      0x00, 0x08, 0x00000000, PPC_SPE); ////
6191
GEN_SPE(evaddiw,        speundef,      0x01, 0x08, 0x00000000, PPC_SPE);
6192
GEN_SPE(evsubfw,        speundef,      0x02, 0x08, 0x00000000, PPC_SPE); ////
6193
GEN_SPE(evsubifw,       speundef,      0x03, 0x08, 0x00000000, PPC_SPE);
6194
GEN_SPE(evabs,          evneg,         0x04, 0x08, 0x0000F800, PPC_SPE); ////
6195
GEN_SPE(evextsb,        evextsh,       0x05, 0x08, 0x0000F800, PPC_SPE); ////
6196
GEN_SPE(evrndw,         evcntlzw,      0x06, 0x08, 0x0000F800, PPC_SPE); ////
6197
GEN_SPE(evcntlsw,       brinc,         0x07, 0x08, 0x00000000, PPC_SPE); //
6198
GEN_SPE(speundef,       evand,         0x08, 0x08, 0x00000000, PPC_SPE); ////
6199
GEN_SPE(evandc,         speundef,      0x09, 0x08, 0x00000000, PPC_SPE); ////
6200
GEN_SPE(evxor,          evor,          0x0B, 0x08, 0x00000000, PPC_SPE); ////
6201
GEN_SPE(evnor,          eveqv,         0x0C, 0x08, 0x00000000, PPC_SPE); ////
6202
GEN_SPE(speundef,       evorc,         0x0D, 0x08, 0x00000000, PPC_SPE); ////
6203
GEN_SPE(evnand,         speundef,      0x0F, 0x08, 0x00000000, PPC_SPE); ////
6204
GEN_SPE(evsrwu,         evsrws,        0x10, 0x08, 0x00000000, PPC_SPE); ////
6205
GEN_SPE(evsrwiu,        evsrwis,       0x11, 0x08, 0x00000000, PPC_SPE);
6206
GEN_SPE(evslw,          speundef,      0x12, 0x08, 0x00000000, PPC_SPE); ////
6207
GEN_SPE(evslwi,         speundef,      0x13, 0x08, 0x00000000, PPC_SPE);
6208
GEN_SPE(evrlw,          evsplati,      0x14, 0x08, 0x00000000, PPC_SPE); //
6209
GEN_SPE(evrlwi,         evsplatfi,     0x15, 0x08, 0x00000000, PPC_SPE);
6210
GEN_SPE(evmergehi,      evmergelo,     0x16, 0x08, 0x00000000, PPC_SPE); ////
6211
GEN_SPE(evmergehilo,    evmergelohi,   0x17, 0x08, 0x00000000, PPC_SPE); ////
6212
GEN_SPE(evcmpgtu,       evcmpgts,      0x18, 0x08, 0x00600000, PPC_SPE); ////
6213
GEN_SPE(evcmpltu,       evcmplts,      0x19, 0x08, 0x00600000, PPC_SPE); ////
6214
GEN_SPE(evcmpeq,        speundef,      0x1A, 0x08, 0x00600000, PPC_SPE); ////
6215

    
6216
static always_inline void gen_evsel (DisasContext *ctx)
6217
{
6218
    if (unlikely(!ctx->spe_enabled)) {
6219
        GEN_EXCP_NO_AP(ctx);
6220
        return;
6221
    }
6222
    gen_op_load_crf_T0(ctx->opcode & 0x7);
6223
    gen_op_load_gpr64_T0(rA(ctx->opcode));
6224
    gen_op_load_gpr64_T1(rB(ctx->opcode));
6225
    gen_op_evsel();
6226
    gen_op_store_T0_gpr64(rD(ctx->opcode));
6227
}
6228

    
6229
GEN_HANDLER2(evsel0, "evsel", 0x04, 0x1c, 0x09, 0x00000000, PPC_SPE)
6230
{
6231
    gen_evsel(ctx);
6232
}
6233
GEN_HANDLER2(evsel1, "evsel", 0x04, 0x1d, 0x09, 0x00000000, PPC_SPE)
6234
{
6235
    gen_evsel(ctx);
6236
}
6237
GEN_HANDLER2(evsel2, "evsel", 0x04, 0x1e, 0x09, 0x00000000, PPC_SPE)
6238
{
6239
    gen_evsel(ctx);
6240
}
6241
GEN_HANDLER2(evsel3, "evsel", 0x04, 0x1f, 0x09, 0x00000000, PPC_SPE)
6242
{
6243
    gen_evsel(ctx);
6244
}
6245

    
6246
/* Load and stores */
6247
#if defined(TARGET_PPC64)
6248
/* In that case, we already have 64 bits load & stores
6249
 * so, spe_ldd is equivalent to ld and spe_std is equivalent to std
6250
 */
6251
#if defined(CONFIG_USER_ONLY)
6252
#define gen_op_spe_ldd_raw gen_op_ld_raw
6253
#define gen_op_spe_ldd_64_raw gen_op_ld_64_raw
6254
#define gen_op_spe_ldd_le_raw gen_op_ld_le_raw
6255
#define gen_op_spe_ldd_le_64_raw gen_op_ld_le_64_raw
6256
#define gen_op_spe_stdd_raw gen_op_ld_raw
6257
#define gen_op_spe_stdd_64_raw gen_op_std_64_raw
6258
#define gen_op_spe_stdd_le_raw gen_op_std_le_raw
6259
#define gen_op_spe_stdd_le_64_raw gen_op_std_le_64_raw
6260
#else /* defined(CONFIG_USER_ONLY) */
6261
#if defined(TARGET_PPC64H)
6262
#define gen_op_spe_ldd_hypv gen_op_ld_hypv
6263
#define gen_op_spe_ldd_64_hypv gen_op_ld_64_hypv
6264
#define gen_op_spe_ldd_le_hypv gen_op_ld_hypv
6265
#define gen_op_spe_ldd_le_64_hypv gen_op_ld_64_hypv
6266
#endif
6267
#define gen_op_spe_ldd_kernel gen_op_ld_kernel
6268
#define gen_op_spe_ldd_64_kernel gen_op_ld_64_kernel
6269
#define gen_op_spe_ldd_le_kernel gen_op_ld_kernel
6270
#define gen_op_spe_ldd_le_64_kernel gen_op_ld_64_kernel
6271
#define gen_op_spe_ldd_user gen_op_ld_user
6272
#define gen_op_spe_ldd_64_user gen_op_ld_64_user
6273
#define gen_op_spe_ldd_le_user gen_op_ld_le_user
6274
#define gen_op_spe_ldd_le_64_user gen_op_ld_le_64_user
6275
#if defined(TARGET_PPC64H)
6276
#define gen_op_spe_stdd_hypv gen_op_std_hypv
6277
#define gen_op_spe_stdd_64_hypv gen_op_std_64_hypv
6278
#define gen_op_spe_stdd_le_hypv gen_op_std_hypv
6279
#define gen_op_spe_stdd_le_64_hypv gen_op_std_64_hypv
6280
#endif
6281
#define gen_op_spe_stdd_kernel gen_op_std_kernel
6282
#define gen_op_spe_stdd_64_kernel gen_op_std_64_kernel
6283
#define gen_op_spe_stdd_le_kernel gen_op_std_kernel
6284
#define gen_op_spe_stdd_le_64_kernel gen_op_std_64_kernel
6285
#define gen_op_spe_stdd_user gen_op_std_user
6286
#define gen_op_spe_stdd_64_user gen_op_std_64_user
6287
#define gen_op_spe_stdd_le_user gen_op_std_le_user
6288
#define gen_op_spe_stdd_le_64_user gen_op_std_le_64_user
6289
#endif /* defined(CONFIG_USER_ONLY) */
6290
#endif /* defined(TARGET_PPC64) */
6291
GEN_SPEOP_LDST(dd, 3);
6292
GEN_SPEOP_LDST(dw, 3);
6293
GEN_SPEOP_LDST(dh, 3);
6294
GEN_SPEOP_LDST(whe, 2);
6295
GEN_SPEOP_LD(whou, 2);
6296
GEN_SPEOP_LD(whos, 2);
6297
GEN_SPEOP_ST(who, 2);
6298

    
6299
#if defined(TARGET_PPC64)
6300
/* In that case, spe_stwwo is equivalent to stw */
6301
#if defined(CONFIG_USER_ONLY)
6302
#define gen_op_spe_stwwo_raw gen_op_stw_raw
6303
#define gen_op_spe_stwwo_le_raw gen_op_stw_le_raw
6304
#define gen_op_spe_stwwo_64_raw gen_op_stw_64_raw
6305
#define gen_op_spe_stwwo_le_64_raw gen_op_stw_le_64_raw
6306
#else
6307
#define gen_op_spe_stwwo_user gen_op_stw_user
6308
#define gen_op_spe_stwwo_le_user gen_op_stw_le_user
6309
#define gen_op_spe_stwwo_64_user gen_op_stw_64_user
6310
#define gen_op_spe_stwwo_le_64_user gen_op_stw_le_64_user
6311
#define gen_op_spe_stwwo_kernel gen_op_stw_kernel
6312
#define gen_op_spe_stwwo_le_kernel gen_op_stw_le_kernel
6313
#define gen_op_spe_stwwo_64_kernel gen_op_stw_64_kernel
6314
#define gen_op_spe_stwwo_le_64_kernel gen_op_stw_le_64_kernel
6315
#if defined(TARGET_PPC64H)
6316
#define gen_op_spe_stwwo_hypv gen_op_stw_hypv
6317
#define gen_op_spe_stwwo_le_hypv gen_op_stw_le_hypv
6318
#define gen_op_spe_stwwo_64_hypv gen_op_stw_64_hypv
6319
#define gen_op_spe_stwwo_le_64_hypv gen_op_stw_le_64_hypv
6320
#endif
6321
#endif
6322
#endif
6323
#define _GEN_OP_SPE_STWWE(suffix)                                             \
6324
static always_inline void gen_op_spe_stwwe_##suffix (void)                    \
6325
{                                                                             \
6326
    gen_op_srli32_T1_64();                                                    \
6327
    gen_op_spe_stwwo_##suffix();                                              \
6328
}
6329
#define _GEN_OP_SPE_STWWE_LE(suffix)                                          \
6330
static always_inline void gen_op_spe_stwwe_le_##suffix (void)                 \
6331
{                                                                             \
6332
    gen_op_srli32_T1_64();                                                    \
6333
    gen_op_spe_stwwo_le_##suffix();                                           \
6334
}
6335
#if defined(TARGET_PPC64)
6336
#define GEN_OP_SPE_STWWE(suffix)                                              \
6337
_GEN_OP_SPE_STWWE(suffix);                                                    \
6338
_GEN_OP_SPE_STWWE_LE(suffix);                                                 \
6339
static always_inline void gen_op_spe_stwwe_64_##suffix (void)                 \
6340
{                                                                             \
6341
    gen_op_srli32_T1_64();                                                    \
6342
    gen_op_spe_stwwo_64_##suffix();                                           \
6343
}                                                                             \
6344
static always_inline void gen_op_spe_stwwe_le_64_##suffix (void)              \
6345
{                                                                             \
6346
    gen_op_srli32_T1_64();                                                    \
6347
    gen_op_spe_stwwo_le_64_##suffix();                                        \
6348
}
6349
#else
6350
#define GEN_OP_SPE_STWWE(suffix)                                              \
6351
_GEN_OP_SPE_STWWE(suffix);                                                    \
6352
_GEN_OP_SPE_STWWE_LE(suffix)
6353
#endif
6354
#if defined(CONFIG_USER_ONLY)
6355
GEN_OP_SPE_STWWE(raw);
6356
#else /* defined(CONFIG_USER_ONLY) */
6357
#if defined(TARGET_PPC64H)
6358
GEN_OP_SPE_STWWE(hypv);
6359
#endif
6360
GEN_OP_SPE_STWWE(kernel);
6361
GEN_OP_SPE_STWWE(user);
6362
#endif /* defined(CONFIG_USER_ONLY) */
6363
GEN_SPEOP_ST(wwe, 2);
6364
GEN_SPEOP_ST(wwo, 2);
6365

    
6366
#define GEN_SPE_LDSPLAT(name, op, suffix)                                     \
6367
static always_inline void gen_op_spe_l##name##_##suffix (void)                \
6368
{                                                                             \
6369
    gen_op_##op##_##suffix();                                                 \
6370
    gen_op_splatw_T1_64();                                                    \
6371
}
6372

    
6373
#define GEN_OP_SPE_LHE(suffix)                                                \
6374
static always_inline void gen_op_spe_lhe_##suffix (void)                      \
6375
{                                                                             \
6376
    gen_op_spe_lh_##suffix();                                                 \
6377
    gen_op_sli16_T1_64();                                                     \
6378
}
6379

    
6380
#define GEN_OP_SPE_LHX(suffix)                                                \
6381
static always_inline void gen_op_spe_lhx_##suffix (void)                      \
6382
{                                                                             \
6383
    gen_op_spe_lh_##suffix();                                                 \
6384
    gen_op_extsh_T1_64();                                                     \
6385
}
6386

    
6387
#if defined(CONFIG_USER_ONLY)
6388
GEN_OP_SPE_LHE(raw);
6389
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, raw);
6390
GEN_OP_SPE_LHE(le_raw);
6391
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_raw);
6392
GEN_SPE_LDSPLAT(hhousplat, spe_lh, raw);
6393
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_raw);
6394
GEN_OP_SPE_LHX(raw);
6395
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, raw);
6396
GEN_OP_SPE_LHX(le_raw);
6397
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_raw);
6398
#if defined(TARGET_PPC64)
6399
GEN_OP_SPE_LHE(64_raw);
6400
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_raw);
6401
GEN_OP_SPE_LHE(le_64_raw);
6402
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_raw);
6403
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_raw);
6404
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_raw);
6405
GEN_OP_SPE_LHX(64_raw);
6406
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_raw);
6407
GEN_OP_SPE_LHX(le_64_raw);
6408
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_raw);
6409
#endif
6410
#else
6411
#if defined(TARGET_PPC64H)
6412
GEN_OP_SPE_LHE(hypv);
6413
#endif
6414
GEN_OP_SPE_LHE(kernel);
6415
GEN_OP_SPE_LHE(user);
6416
#if defined(TARGET_PPC64H)
6417
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, hypv);
6418
#endif
6419
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, kernel);
6420
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, user);
6421
#if defined(TARGET_PPC64H)
6422
GEN_OP_SPE_LHE(le_hypv);
6423
#endif
6424
GEN_OP_SPE_LHE(le_kernel);
6425
GEN_OP_SPE_LHE(le_user);
6426
#if defined(TARGET_PPC64H)
6427
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_hypv);
6428
#endif
6429
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_kernel);
6430
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_user);
6431
#if defined(TARGET_PPC64H)
6432
GEN_SPE_LDSPLAT(hhousplat, spe_lh, hypv);
6433
#endif
6434
GEN_SPE_LDSPLAT(hhousplat, spe_lh, kernel);
6435
GEN_SPE_LDSPLAT(hhousplat, spe_lh, user);
6436
#if defined(TARGET_PPC64H)
6437
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_hypv);
6438
#endif
6439
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_kernel);
6440
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_user);
6441
#if defined(TARGET_PPC64H)
6442
GEN_OP_SPE_LHX(hypv);
6443
#endif
6444
GEN_OP_SPE_LHX(kernel);
6445
GEN_OP_SPE_LHX(user);
6446
#if defined(TARGET_PPC64H)
6447
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, hypv);
6448
#endif
6449
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, kernel);
6450
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, user);
6451
#if defined(TARGET_PPC64H)
6452
GEN_OP_SPE_LHX(le_hypv);
6453
#endif
6454
GEN_OP_SPE_LHX(le_kernel);
6455
GEN_OP_SPE_LHX(le_user);
6456
#if defined(TARGET_PPC64H)
6457
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_hypv);
6458
#endif
6459
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_kernel);
6460
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_user);
6461
#if defined(TARGET_PPC64)
6462
#if defined(TARGET_PPC64H)
6463
GEN_OP_SPE_LHE(64_hypv);
6464
#endif
6465
GEN_OP_SPE_LHE(64_kernel);
6466
GEN_OP_SPE_LHE(64_user);
6467
#if defined(TARGET_PPC64H)
6468
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_hypv);
6469
#endif
6470
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_kernel);
6471
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, 64_user);
6472
#if defined(TARGET_PPC64H)
6473
GEN_OP_SPE_LHE(le_64_hypv);
6474
#endif
6475
GEN_OP_SPE_LHE(le_64_kernel);
6476
GEN_OP_SPE_LHE(le_64_user);
6477
#if defined(TARGET_PPC64H)
6478
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_hypv);
6479
#endif
6480
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_kernel);
6481
GEN_SPE_LDSPLAT(hhesplat, spe_lhe, le_64_user);
6482
#if defined(TARGET_PPC64H)
6483
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_hypv);
6484
#endif
6485
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_kernel);
6486
GEN_SPE_LDSPLAT(hhousplat, spe_lh, 64_user);
6487
#if defined(TARGET_PPC64H)
6488
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_hypv);
6489
#endif
6490
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_kernel);
6491
GEN_SPE_LDSPLAT(hhousplat, spe_lh, le_64_user);
6492
#if defined(TARGET_PPC64H)
6493
GEN_OP_SPE_LHX(64_hypv);
6494
#endif
6495
GEN_OP_SPE_LHX(64_kernel);
6496
GEN_OP_SPE_LHX(64_user);
6497
#if defined(TARGET_PPC64H)
6498
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_hypv);
6499
#endif
6500
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_kernel);
6501
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, 64_user);
6502
#if defined(TARGET_PPC64H)
6503
GEN_OP_SPE_LHX(le_64_hypv);
6504
#endif
6505
GEN_OP_SPE_LHX(le_64_kernel);
6506
GEN_OP_SPE_LHX(le_64_user);
6507
#if defined(TARGET_PPC64H)
6508
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_hypv);
6509
#endif
6510
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_kernel);
6511
GEN_SPE_LDSPLAT(hhossplat, spe_lhx, le_64_user);
6512
#endif
6513
#endif
6514
GEN_SPEOP_LD(hhesplat, 1);
6515
GEN_SPEOP_LD(hhousplat, 1);
6516
GEN_SPEOP_LD(hhossplat, 1);
6517
GEN_SPEOP_LD(wwsplat, 2);
6518
GEN_SPEOP_LD(whsplat, 2);
6519

    
6520
GEN_SPE(evlddx,         evldd,         0x00, 0x0C, 0x00000000, PPC_SPE); //
6521
GEN_SPE(evldwx,         evldw,         0x01, 0x0C, 0x00000000, PPC_SPE); //
6522
GEN_SPE(evldhx,         evldh,         0x02, 0x0C, 0x00000000, PPC_SPE); //
6523
GEN_SPE(evlhhesplatx,   evlhhesplat,   0x04, 0x0C, 0x00000000, PPC_SPE); //
6524
GEN_SPE(evlhhousplatx,  evlhhousplat,  0x06, 0x0C, 0x00000000, PPC_SPE); //
6525
GEN_SPE(evlhhossplatx,  evlhhossplat,  0x07, 0x0C, 0x00000000, PPC_SPE); //
6526
GEN_SPE(evlwhex,        evlwhe,        0x08, 0x0C, 0x00000000, PPC_SPE); //
6527
GEN_SPE(evlwhoux,       evlwhou,       0x0A, 0x0C, 0x00000000, PPC_SPE); //
6528
GEN_SPE(evlwhosx,       evlwhos,       0x0B, 0x0C, 0x00000000, PPC_SPE); //
6529
GEN_SPE(evlwwsplatx,    evlwwsplat,    0x0C, 0x0C, 0x00000000, PPC_SPE); //
6530
GEN_SPE(evlwhsplatx,    evlwhsplat,    0x0E, 0x0C, 0x00000000, PPC_SPE); //
6531
GEN_SPE(evstddx,        evstdd,        0x10, 0x0C, 0x00000000, PPC_SPE); //
6532
GEN_SPE(evstdwx,        evstdw,        0x11, 0x0C, 0x00000000, PPC_SPE); //
6533
GEN_SPE(evstdhx,        evstdh,        0x12, 0x0C, 0x00000000, PPC_SPE); //
6534
GEN_SPE(evstwhex,       evstwhe,       0x18, 0x0C, 0x00000000, PPC_SPE); //
6535
GEN_SPE(evstwhox,       evstwho,       0x1A, 0x0C, 0x00000000, PPC_SPE); //
6536
GEN_SPE(evstwwex,       evstwwe,       0x1C, 0x0C, 0x00000000, PPC_SPE); //
6537
GEN_SPE(evstwwox,       evstwwo,       0x1E, 0x0C, 0x00000000, PPC_SPE); //
6538

    
6539
/* Multiply and add - TODO */
6540
#if 0
6541
GEN_SPE(speundef,       evmhessf,      0x01, 0x10, 0x00000000, PPC_SPE);
6542
GEN_SPE(speundef,       evmhossf,      0x03, 0x10, 0x00000000, PPC_SPE);
6543
GEN_SPE(evmheumi,       evmhesmi,      0x04, 0x10, 0x00000000, PPC_SPE);
6544
GEN_SPE(speundef,       evmhesmf,      0x05, 0x10, 0x00000000, PPC_SPE);
6545
GEN_SPE(evmhoumi,       evmhosmi,      0x06, 0x10, 0x00000000, PPC_SPE);
6546
GEN_SPE(speundef,       evmhosmf,      0x07, 0x10, 0x00000000, PPC_SPE);
6547
GEN_SPE(speundef,       evmhessfa,     0x11, 0x10, 0x00000000, PPC_SPE);
6548
GEN_SPE(speundef,       evmhossfa,     0x13, 0x10, 0x00000000, PPC_SPE);
6549
GEN_SPE(evmheumia,      evmhesmia,     0x14, 0x10, 0x00000000, PPC_SPE);
6550
GEN_SPE(speundef,       evmhesmfa,     0x15, 0x10, 0x00000000, PPC_SPE);
6551
GEN_SPE(evmhoumia,      evmhosmia,     0x16, 0x10, 0x00000000, PPC_SPE);
6552
GEN_SPE(speundef,       evmhosmfa,     0x17, 0x10, 0x00000000, PPC_SPE);
6553

6554
GEN_SPE(speundef,       evmwhssf,      0x03, 0x11, 0x00000000, PPC_SPE);
6555
GEN_SPE(evmwlumi,       speundef,      0x04, 0x11, 0x00000000, PPC_SPE);
6556
GEN_SPE(evmwhumi,       evmwhsmi,      0x06, 0x11, 0x00000000, PPC_SPE);
6557
GEN_SPE(speundef,       evmwhsmf,      0x07, 0x11, 0x00000000, PPC_SPE);
6558
GEN_SPE(speundef,       evmwssf,       0x09, 0x11, 0x00000000, PPC_SPE);
6559
GEN_SPE(evmwumi,        evmwsmi,       0x0C, 0x11, 0x00000000, PPC_SPE);
6560
GEN_SPE(speundef,       evmwsmf,       0x0D, 0x11, 0x00000000, PPC_SPE);
6561
GEN_SPE(speundef,       evmwhssfa,     0x13, 0x11, 0x00000000, PPC_SPE);
6562
GEN_SPE(evmwlumia,      speundef,      0x14, 0x11, 0x00000000, PPC_SPE);
6563
GEN_SPE(evmwhumia,      evmwhsmia,     0x16, 0x11, 0x00000000, PPC_SPE);
6564
GEN_SPE(speundef,       evmwhsmfa,     0x17, 0x11, 0x00000000, PPC_SPE);
6565
GEN_SPE(speundef,       evmwssfa,      0x19, 0x11, 0x00000000, PPC_SPE);
6566
GEN_SPE(evmwumia,       evmwsmia,      0x1C, 0x11, 0x00000000, PPC_SPE);
6567
GEN_SPE(speundef,       evmwsmfa,      0x1D, 0x11, 0x00000000, PPC_SPE);
6568

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

6576
GEN_SPE(evmheusiaaw,    evmhessiaaw,   0x00, 0x14, 0x00000000, PPC_SPE);
6577
GEN_SPE(speundef,       evmhessfaaw,   0x01, 0x14, 0x00000000, PPC_SPE);
6578
GEN_SPE(evmhousiaaw,    evmhossiaaw,   0x02, 0x14, 0x00000000, PPC_SPE);
6579
GEN_SPE(speundef,       evmhossfaaw,   0x03, 0x14, 0x00000000, PPC_SPE);
6580
GEN_SPE(evmheumiaaw,    evmhesmiaaw,   0x04, 0x14, 0x00000000, PPC_SPE);
6581
GEN_SPE(speundef,       evmhesmfaaw,   0x05, 0x14, 0x00000000, PPC_SPE);
6582
GEN_SPE(evmhoumiaaw,    evmhosmiaaw,   0x06, 0x14, 0x00000000, PPC_SPE);
6583
GEN_SPE(speundef,       evmhosmfaaw,   0x07, 0x14, 0x00000000, PPC_SPE);
6584
GEN_SPE(evmhegumiaa,    evmhegsmiaa,   0x14, 0x14, 0x00000000, PPC_SPE);
6585
GEN_SPE(speundef,       evmhegsmfaa,   0x15, 0x14, 0x00000000, PPC_SPE);
6586
GEN_SPE(evmhogumiaa,    evmhogsmiaa,   0x16, 0x14, 0x00000000, PPC_SPE);
6587
GEN_SPE(speundef,       evmhogsmfaa,   0x17, 0x14, 0x00000000, PPC_SPE);
6588

6589
GEN_SPE(evmwlusiaaw,    evmwlssiaaw,   0x00, 0x15, 0x00000000, PPC_SPE);
6590
GEN_SPE(evmwlumiaaw,    evmwlsmiaaw,   0x04, 0x15, 0x00000000, PPC_SPE);
6591
GEN_SPE(speundef,       evmwssfaa,     0x09, 0x15, 0x00000000, PPC_SPE);
6592
GEN_SPE(evmwumiaa,      evmwsmiaa,     0x0C, 0x15, 0x00000000, PPC_SPE);
6593
GEN_SPE(speundef,       evmwsmfaa,     0x0D, 0x15, 0x00000000, PPC_SPE);
6594

6595
GEN_SPE(evmheusianw,    evmhessianw,   0x00, 0x16, 0x00000000, PPC_SPE);
6596
GEN_SPE(speundef,       evmhessfanw,   0x01, 0x16, 0x00000000, PPC_SPE);
6597
GEN_SPE(evmhousianw,    evmhossianw,   0x02, 0x16, 0x00000000, PPC_SPE);
6598
GEN_SPE(speundef,       evmhossfanw,   0x03, 0x16, 0x00000000, PPC_SPE);
6599
GEN_SPE(evmheumianw,    evmhesmianw,   0x04, 0x16, 0x00000000, PPC_SPE);
6600
GEN_SPE(speundef,       evmhesmfanw,   0x05, 0x16, 0x00000000, PPC_SPE);
6601
GEN_SPE(evmhoumianw,    evmhosmianw,   0x06, 0x16, 0x00000000, PPC_SPE);
6602
GEN_SPE(speundef,       evmhosmfanw,   0x07, 0x16, 0x00000000, PPC_SPE);
6603
GEN_SPE(evmhegumian,    evmhegsmian,   0x14, 0x16, 0x00000000, PPC_SPE);
6604
GEN_SPE(speundef,       evmhegsmfan,   0x15, 0x16, 0x00000000, PPC_SPE);
6605
GEN_SPE(evmhigumian,    evmhigsmian,   0x16, 0x16, 0x00000000, PPC_SPE);
6606
GEN_SPE(speundef,       evmhogsmfan,   0x17, 0x16, 0x00000000, PPC_SPE);
6607

6608
GEN_SPE(evmwlusianw,    evmwlssianw,   0x00, 0x17, 0x00000000, PPC_SPE);
6609
GEN_SPE(evmwlumianw,    evmwlsmianw,   0x04, 0x17, 0x00000000, PPC_SPE);
6610
GEN_SPE(speundef,       evmwssfan,     0x09, 0x17, 0x00000000, PPC_SPE);
6611
GEN_SPE(evmwumian,      evmwsmian,     0x0C, 0x17, 0x00000000, PPC_SPE);
6612
GEN_SPE(speundef,       evmwsmfan,     0x0D, 0x17, 0x00000000, PPC_SPE);
6613
#endif
6614

    
6615
/***                      SPE floating-point extension                     ***/
6616
#define GEN_SPEFPUOP_CONV(name)                                               \
6617
static always_inline void gen_##name (DisasContext *ctx)                      \
6618
{                                                                             \
6619
    gen_op_load_gpr64_T0(rB(ctx->opcode));                                    \
6620
    gen_op_##name();                                                          \
6621
    gen_op_store_T0_gpr64(rD(ctx->opcode));                                   \
6622
}
6623

    
6624
/* Single precision floating-point vectors operations */
6625
/* Arithmetic */
6626
GEN_SPEOP_ARITH2(evfsadd);
6627
GEN_SPEOP_ARITH2(evfssub);
6628
GEN_SPEOP_ARITH2(evfsmul);
6629
GEN_SPEOP_ARITH2(evfsdiv);
6630
GEN_SPEOP_ARITH1(evfsabs);
6631
GEN_SPEOP_ARITH1(evfsnabs);
6632
GEN_SPEOP_ARITH1(evfsneg);
6633
/* Conversion */
6634
GEN_SPEFPUOP_CONV(evfscfui);
6635
GEN_SPEFPUOP_CONV(evfscfsi);
6636
GEN_SPEFPUOP_CONV(evfscfuf);
6637
GEN_SPEFPUOP_CONV(evfscfsf);
6638
GEN_SPEFPUOP_CONV(evfsctui);
6639
GEN_SPEFPUOP_CONV(evfsctsi);
6640
GEN_SPEFPUOP_CONV(evfsctuf);
6641
GEN_SPEFPUOP_CONV(evfsctsf);
6642
GEN_SPEFPUOP_CONV(evfsctuiz);
6643
GEN_SPEFPUOP_CONV(evfsctsiz);
6644
/* Comparison */
6645
GEN_SPEOP_COMP(evfscmpgt);
6646
GEN_SPEOP_COMP(evfscmplt);
6647
GEN_SPEOP_COMP(evfscmpeq);
6648
GEN_SPEOP_COMP(evfststgt);
6649
GEN_SPEOP_COMP(evfststlt);
6650
GEN_SPEOP_COMP(evfststeq);
6651

    
6652
/* Opcodes definitions */
6653
GEN_SPE(evfsadd,        evfssub,       0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
6654
GEN_SPE(evfsabs,        evfsnabs,      0x02, 0x0A, 0x0000F800, PPC_SPEFPU); //
6655
GEN_SPE(evfsneg,        speundef,      0x03, 0x0A, 0x0000F800, PPC_SPEFPU); //
6656
GEN_SPE(evfsmul,        evfsdiv,       0x04, 0x0A, 0x00000000, PPC_SPEFPU); //
6657
GEN_SPE(evfscmpgt,      evfscmplt,     0x06, 0x0A, 0x00600000, PPC_SPEFPU); //
6658
GEN_SPE(evfscmpeq,      speundef,      0x07, 0x0A, 0x00600000, PPC_SPEFPU); //
6659
GEN_SPE(evfscfui,       evfscfsi,      0x08, 0x0A, 0x00180000, PPC_SPEFPU); //
6660
GEN_SPE(evfscfuf,       evfscfsf,      0x09, 0x0A, 0x00180000, PPC_SPEFPU); //
6661
GEN_SPE(evfsctui,       evfsctsi,      0x0A, 0x0A, 0x00180000, PPC_SPEFPU); //
6662
GEN_SPE(evfsctuf,       evfsctsf,      0x0B, 0x0A, 0x00180000, PPC_SPEFPU); //
6663
GEN_SPE(evfsctuiz,      speundef,      0x0C, 0x0A, 0x00180000, PPC_SPEFPU); //
6664
GEN_SPE(evfsctsiz,      speundef,      0x0D, 0x0A, 0x00180000, PPC_SPEFPU); //
6665
GEN_SPE(evfststgt,      evfststlt,     0x0E, 0x0A, 0x00600000, PPC_SPEFPU); //
6666
GEN_SPE(evfststeq,      speundef,      0x0F, 0x0A, 0x00600000, PPC_SPEFPU); //
6667

    
6668
/* Single precision floating-point operations */
6669
/* Arithmetic */
6670
GEN_SPEOP_ARITH2(efsadd);
6671
GEN_SPEOP_ARITH2(efssub);
6672
GEN_SPEOP_ARITH2(efsmul);
6673
GEN_SPEOP_ARITH2(efsdiv);
6674
GEN_SPEOP_ARITH1(efsabs);
6675
GEN_SPEOP_ARITH1(efsnabs);
6676
GEN_SPEOP_ARITH1(efsneg);
6677
/* Conversion */
6678
GEN_SPEFPUOP_CONV(efscfui);
6679
GEN_SPEFPUOP_CONV(efscfsi);
6680
GEN_SPEFPUOP_CONV(efscfuf);
6681
GEN_SPEFPUOP_CONV(efscfsf);
6682
GEN_SPEFPUOP_CONV(efsctui);
6683
GEN_SPEFPUOP_CONV(efsctsi);
6684
GEN_SPEFPUOP_CONV(efsctuf);
6685
GEN_SPEFPUOP_CONV(efsctsf);
6686
GEN_SPEFPUOP_CONV(efsctuiz);
6687
GEN_SPEFPUOP_CONV(efsctsiz);
6688
GEN_SPEFPUOP_CONV(efscfd);
6689
/* Comparison */
6690
GEN_SPEOP_COMP(efscmpgt);
6691
GEN_SPEOP_COMP(efscmplt);
6692
GEN_SPEOP_COMP(efscmpeq);
6693
GEN_SPEOP_COMP(efststgt);
6694
GEN_SPEOP_COMP(efststlt);
6695
GEN_SPEOP_COMP(efststeq);
6696

    
6697
/* Opcodes definitions */
6698
GEN_SPE(efsadd,         efssub,        0x00, 0x0A, 0x00000000, PPC_SPEFPU); //
6699
GEN_SPE(efsabs,         efsnabs,       0x02, 0x0B, 0x0000F800, PPC_SPEFPU); //
6700
GEN_SPE(efsneg,         speundef,      0x03, 0x0B, 0x0000F800, PPC_SPEFPU); //
6701
GEN_SPE(efsmul,         efsdiv,        0x04, 0x0B, 0x00000000, PPC_SPEFPU); //
6702
GEN_SPE(efscmpgt,       efscmplt,      0x06, 0x0B, 0x00600000, PPC_SPEFPU); //
6703
GEN_SPE(efscmpeq,       efscfd,        0x07, 0x0B, 0x00600000, PPC_SPEFPU); //
6704
GEN_SPE(efscfui,        efscfsi,       0x08, 0x0B, 0x00180000, PPC_SPEFPU); //
6705
GEN_SPE(efscfuf,        efscfsf,       0x09, 0x0B, 0x00180000, PPC_SPEFPU); //
6706
GEN_SPE(efsctui,        efsctsi,       0x0A, 0x0B, 0x00180000, PPC_SPEFPU); //
6707
GEN_SPE(efsctuf,        efsctsf,       0x0B, 0x0B, 0x00180000, PPC_SPEFPU); //
6708
GEN_SPE(efsctuiz,       efsctsiz,      0x0C, 0x0B, 0x00180000, PPC_SPEFPU); //
6709
GEN_SPE(efststgt,       efststlt,      0x0E, 0x0B, 0x00600000, PPC_SPEFPU); //
6710
GEN_SPE(efststeq,       speundef,      0x0F, 0x0B, 0x00600000, PPC_SPEFPU); //
6711

    
6712
/* Double precision floating-point operations */
6713
/* Arithmetic */
6714
GEN_SPEOP_ARITH2(efdadd);
6715
GEN_SPEOP_ARITH2(efdsub);
6716
GEN_SPEOP_ARITH2(efdmul);
6717
GEN_SPEOP_ARITH2(efddiv);
6718
GEN_SPEOP_ARITH1(efdabs);
6719
GEN_SPEOP_ARITH1(efdnabs);
6720
GEN_SPEOP_ARITH1(efdneg);
6721
/* Conversion */
6722

    
6723
GEN_SPEFPUOP_CONV(efdcfui);
6724
GEN_SPEFPUOP_CONV(efdcfsi);
6725
GEN_SPEFPUOP_CONV(efdcfuf);
6726
GEN_SPEFPUOP_CONV(efdcfsf);
6727
GEN_SPEFPUOP_CONV(efdctui);
6728
GEN_SPEFPUOP_CONV(efdctsi);
6729
GEN_SPEFPUOP_CONV(efdctuf);
6730
GEN_SPEFPUOP_CONV(efdctsf);
6731
GEN_SPEFPUOP_CONV(efdctuiz);
6732
GEN_SPEFPUOP_CONV(efdctsiz);
6733
GEN_SPEFPUOP_CONV(efdcfs);
6734
GEN_SPEFPUOP_CONV(efdcfuid);
6735
GEN_SPEFPUOP_CONV(efdcfsid);
6736
GEN_SPEFPUOP_CONV(efdctuidz);
6737
GEN_SPEFPUOP_CONV(efdctsidz);
6738
/* Comparison */
6739
GEN_SPEOP_COMP(efdcmpgt);
6740
GEN_SPEOP_COMP(efdcmplt);
6741
GEN_SPEOP_COMP(efdcmpeq);
6742
GEN_SPEOP_COMP(efdtstgt);
6743
GEN_SPEOP_COMP(efdtstlt);
6744
GEN_SPEOP_COMP(efdtsteq);
6745

    
6746
/* Opcodes definitions */
6747
GEN_SPE(efdadd,         efdsub,        0x10, 0x0B, 0x00000000, PPC_SPEFPU); //
6748
GEN_SPE(efdcfuid,       efdcfsid,      0x11, 0x0B, 0x00180000, PPC_SPEFPU); //
6749
GEN_SPE(efdabs,         efdnabs,       0x12, 0x0B, 0x0000F800, PPC_SPEFPU); //
6750
GEN_SPE(efdneg,         speundef,      0x13, 0x0B, 0x0000F800, PPC_SPEFPU); //
6751
GEN_SPE(efdmul,         efddiv,        0x14, 0x0B, 0x00000000, PPC_SPEFPU); //
6752
GEN_SPE(efdctuidz,      efdctsidz,     0x15, 0x0B, 0x00180000, PPC_SPEFPU); //
6753
GEN_SPE(efdcmpgt,       efdcmplt,      0x16, 0x0B, 0x00600000, PPC_SPEFPU); //
6754
GEN_SPE(efdcmpeq,       efdcfs,        0x17, 0x0B, 0x00600000, PPC_SPEFPU); //
6755
GEN_SPE(efdcfui,        efdcfsi,       0x18, 0x0B, 0x00180000, PPC_SPEFPU); //
6756
GEN_SPE(efdcfuf,        efdcfsf,       0x19, 0x0B, 0x00180000, PPC_SPEFPU); //
6757
GEN_SPE(efdctui,        efdctsi,       0x1A, 0x0B, 0x00180000, PPC_SPEFPU); //
6758
GEN_SPE(efdctuf,        efdctsf,       0x1B, 0x0B, 0x00180000, PPC_SPEFPU); //
6759
GEN_SPE(efdctuiz,       speundef,      0x1C, 0x0B, 0x00180000, PPC_SPEFPU); //
6760
GEN_SPE(efdctsiz,       speundef,      0x1D, 0x0B, 0x00180000, PPC_SPEFPU); //
6761
GEN_SPE(efdtstgt,       efdtstlt,      0x1E, 0x0B, 0x00600000, PPC_SPEFPU); //
6762
GEN_SPE(efdtsteq,       speundef,      0x1F, 0x0B, 0x00600000, PPC_SPEFPU); //
6763

    
6764
/* End opcode list */
6765
GEN_OPCODE_MARK(end);
6766

    
6767
#include "translate_init.c"
6768
#include "helper_regs.h"
6769

    
6770
/*****************************************************************************/
6771
/* Misc PowerPC helpers */
6772
void cpu_dump_state (CPUState *env, FILE *f,
6773
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
6774
                     int flags)
6775
{
6776
#if defined(TARGET_PPC64) || 1
6777
#define FILL ""
6778
#define RGPL  4
6779
#define RFPL  4
6780
#else
6781
#define FILL "        "
6782
#define RGPL  8
6783
#define RFPL  4
6784
#endif
6785

    
6786
    int i;
6787

    
6788
    cpu_fprintf(f, "NIP " ADDRX "   LR " ADDRX " CTR " ADDRX " XER %08x\n",
6789
                env->nip, env->lr, env->ctr, hreg_load_xer(env));
6790
    cpu_fprintf(f, "MSR " REGX FILL " HID0 " REGX FILL "  HF " REGX FILL
6791
                " idx %d\n",
6792
                env->msr, env->hflags, env->spr[SPR_HID0], env->mmu_idx);
6793
#if !defined(NO_TIMER_DUMP)
6794
    cpu_fprintf(f, "TB %08x %08x "
6795
#if !defined(CONFIG_USER_ONLY)
6796
                "DECR %08x"
6797
#endif
6798
                "\n",
6799
                cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
6800
#if !defined(CONFIG_USER_ONLY)
6801
                , cpu_ppc_load_decr(env)
6802
#endif
6803
                );
6804
#endif
6805
    for (i = 0; i < 32; i++) {
6806
        if ((i & (RGPL - 1)) == 0)
6807
            cpu_fprintf(f, "GPR%02d", i);
6808
        cpu_fprintf(f, " " REGX, (target_ulong)env->gpr[i]);
6809
        if ((i & (RGPL - 1)) == (RGPL - 1))
6810
            cpu_fprintf(f, "\n");
6811
    }
6812
    cpu_fprintf(f, "CR ");
6813
    for (i = 0; i < 8; i++)
6814
        cpu_fprintf(f, "%01x", env->crf[i]);
6815
    cpu_fprintf(f, "  [");
6816
    for (i = 0; i < 8; i++) {
6817
        char a = '-';
6818
        if (env->crf[i] & 0x08)
6819
            a = 'L';
6820
        else if (env->crf[i] & 0x04)
6821
            a = 'G';
6822
        else if (env->crf[i] & 0x02)
6823
            a = 'E';
6824
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
6825
    }
6826
    cpu_fprintf(f, " ]             " FILL "RES " REGX "\n", env->reserve);
6827
    for (i = 0; i < 32; i++) {
6828
        if ((i & (RFPL - 1)) == 0)
6829
            cpu_fprintf(f, "FPR%02d", i);
6830
        cpu_fprintf(f, " %016" PRIx64, *((uint64_t *)&env->fpr[i]));
6831
        if ((i & (RFPL - 1)) == (RFPL - 1))
6832
            cpu_fprintf(f, "\n");
6833
    }
6834
#if !defined(CONFIG_USER_ONLY)
6835
    cpu_fprintf(f, "SRR0 " REGX " SRR1 " REGX " SDR1 " REGX "\n",
6836
                env->spr[SPR_SRR0], env->spr[SPR_SRR1], env->sdr1);
6837
#endif
6838

    
6839
#undef RGPL
6840
#undef RFPL
6841
#undef FILL
6842
}
6843

    
6844
void cpu_dump_statistics (CPUState *env, FILE*f,
6845
                          int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
6846
                          int flags)
6847
{
6848
#if defined(DO_PPC_STATISTICS)
6849
    opc_handler_t **t1, **t2, **t3, *handler;
6850
    int op1, op2, op3;
6851

    
6852
    t1 = env->opcodes;
6853
    for (op1 = 0; op1 < 64; op1++) {
6854
        handler = t1[op1];
6855
        if (is_indirect_opcode(handler)) {
6856
            t2 = ind_table(handler);
6857
            for (op2 = 0; op2 < 32; op2++) {
6858
                handler = t2[op2];
6859
                if (is_indirect_opcode(handler)) {
6860
                    t3 = ind_table(handler);
6861
                    for (op3 = 0; op3 < 32; op3++) {
6862
                        handler = t3[op3];
6863
                        if (handler->count == 0)
6864
                            continue;
6865
                        cpu_fprintf(f, "%02x %02x %02x (%02x %04d) %16s: "
6866
                                    "%016llx %lld\n",
6867
                                    op1, op2, op3, op1, (op3 << 5) | op2,
6868
                                    handler->oname,
6869
                                    handler->count, handler->count);
6870
                    }
6871
                } else {
6872
                    if (handler->count == 0)
6873
                        continue;
6874
                    cpu_fprintf(f, "%02x %02x    (%02x %04d) %16s: "
6875
                                "%016llx %lld\n",
6876
                                op1, op2, op1, op2, handler->oname,
6877
                                handler->count, handler->count);
6878
                }
6879
            }
6880
        } else {
6881
            if (handler->count == 0)
6882
                continue;
6883
            cpu_fprintf(f, "%02x       (%02x     ) %16s: %016llx %lld\n",
6884
                        op1, op1, handler->oname,
6885
                        handler->count, handler->count);
6886
        }
6887
    }
6888
#endif
6889
}
6890

    
6891
/*****************************************************************************/
6892
static always_inline int gen_intermediate_code_internal (CPUState *env,
6893
                                                         TranslationBlock *tb,
6894
                                                         int search_pc)
6895
{
6896
    DisasContext ctx, *ctxp = &ctx;
6897
    opc_handler_t **table, *handler;
6898
    target_ulong pc_start;
6899
    uint16_t *gen_opc_end;
6900
    int supervisor, little_endian;
6901
    int single_step, branch_step;
6902
    int j, lj = -1;
6903

    
6904
    pc_start = tb->pc;
6905
    gen_opc_ptr = gen_opc_buf;
6906
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
6907
    gen_opparam_ptr = gen_opparam_buf;
6908
#if defined(OPTIMIZE_FPRF_UPDATE)
6909
    gen_fprf_ptr = gen_fprf_buf;
6910
#endif
6911
    nb_gen_labels = 0;
6912
    ctx.nip = pc_start;
6913
    ctx.tb = tb;
6914
    ctx.exception = POWERPC_EXCP_NONE;
6915
    ctx.spr_cb = env->spr_cb;
6916
    supervisor = env->mmu_idx;
6917
#if !defined(CONFIG_USER_ONLY)
6918
    ctx.supervisor = supervisor;
6919
#endif
6920
    little_endian = env->hflags & (1 << MSR_LE) ? 1 : 0;
6921
#if defined(TARGET_PPC64)
6922
    ctx.sf_mode = msr_sf;
6923
    ctx.mem_idx = (supervisor << 2) | (msr_sf << 1) | little_endian;
6924
#else
6925
    ctx.mem_idx = (supervisor << 1) | little_endian;
6926
#endif
6927
    ctx.dcache_line_size = env->dcache_line_size;
6928
    ctx.fpu_enabled = msr_fp;
6929
    if ((env->flags & POWERPC_FLAG_SPE) && msr_spe)
6930
        ctx.spe_enabled = msr_spe;
6931
    else
6932
        ctx.spe_enabled = 0;
6933
    if ((env->flags & POWERPC_FLAG_VRE) && msr_vr)
6934
        ctx.altivec_enabled = msr_vr;
6935
    else
6936
        ctx.altivec_enabled = 0;
6937
    if ((env->flags & POWERPC_FLAG_SE) && msr_se)
6938
        single_step = 1;
6939
    else
6940
        single_step = 0;
6941
    if ((env->flags & POWERPC_FLAG_BE) && msr_be)
6942
        branch_step = 1;
6943
    else
6944
        branch_step = 0;
6945
    ctx.singlestep_enabled = env->singlestep_enabled || single_step == 1;
6946
#if defined (DO_SINGLE_STEP) && 0
6947
    /* Single step trace mode */
6948
    msr_se = 1;
6949
#endif
6950
    /* Set env in case of segfault during code fetch */
6951
    while (ctx.exception == POWERPC_EXCP_NONE && gen_opc_ptr < gen_opc_end) {
6952
        if (unlikely(env->nb_breakpoints > 0)) {
6953
            for (j = 0; j < env->nb_breakpoints; j++) {
6954
                if (env->breakpoints[j] == ctx.nip) {
6955
                    gen_update_nip(&ctx, ctx.nip);
6956
                    gen_op_debug();
6957
                    break;
6958
                }
6959
            }
6960
        }
6961
        if (unlikely(search_pc)) {
6962
            j = gen_opc_ptr - gen_opc_buf;
6963
            if (lj < j) {
6964
                lj++;
6965
                while (lj < j)
6966
                    gen_opc_instr_start[lj++] = 0;
6967
                gen_opc_pc[lj] = ctx.nip;
6968
                gen_opc_instr_start[lj] = 1;
6969
            }
6970
        }
6971
#if defined PPC_DEBUG_DISAS
6972
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6973
            fprintf(logfile, "----------------\n");
6974
            fprintf(logfile, "nip=" ADDRX " super=%d ir=%d\n",
6975
                    ctx.nip, supervisor, (int)msr_ir);
6976
        }
6977
#endif
6978
        if (unlikely(little_endian)) {
6979
            ctx.opcode = bswap32(ldl_code(ctx.nip));
6980
        } else {
6981
            ctx.opcode = ldl_code(ctx.nip);
6982
        }
6983
#if defined PPC_DEBUG_DISAS
6984
        if (loglevel & CPU_LOG_TB_IN_ASM) {
6985
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
6986
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
6987
                    opc3(ctx.opcode), little_endian ? "little" : "big");
6988
        }
6989
#endif
6990
        ctx.nip += 4;
6991
        table = env->opcodes;
6992
        handler = table[opc1(ctx.opcode)];
6993
        if (is_indirect_opcode(handler)) {
6994
            table = ind_table(handler);
6995
            handler = table[opc2(ctx.opcode)];
6996
            if (is_indirect_opcode(handler)) {
6997
                table = ind_table(handler);
6998
                handler = table[opc3(ctx.opcode)];
6999
            }
7000
        }
7001
        /* Is opcode *REALLY* valid ? */
7002
        if (unlikely(handler->handler == &gen_invalid)) {
7003
            if (loglevel != 0) {
7004
                fprintf(logfile, "invalid/unsupported opcode: "
7005
                        "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
7006
                        opc1(ctx.opcode), opc2(ctx.opcode),
7007
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7008
            } else {
7009
                printf("invalid/unsupported opcode: "
7010
                       "%02x - %02x - %02x (%08x) 0x" ADDRX " %d\n",
7011
                       opc1(ctx.opcode), opc2(ctx.opcode),
7012
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, (int)msr_ir);
7013
            }
7014
        } else {
7015
            if (unlikely((ctx.opcode & handler->inval) != 0)) {
7016
                if (loglevel != 0) {
7017
                    fprintf(logfile, "invalid bits: %08x for opcode: "
7018
                            "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
7019
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
7020
                            opc2(ctx.opcode), opc3(ctx.opcode),
7021
                            ctx.opcode, ctx.nip - 4);
7022
                } else {
7023
                    printf("invalid bits: %08x for opcode: "
7024
                           "%02x - %02x - %02x (%08x) 0x" ADDRX "\n",
7025
                           ctx.opcode & handler->inval, opc1(ctx.opcode),
7026
                           opc2(ctx.opcode), opc3(ctx.opcode),
7027
                           ctx.opcode, ctx.nip - 4);
7028
                }
7029
                GEN_EXCP_INVAL(ctxp);
7030
                break;
7031
            }
7032
        }
7033
        (*(handler->handler))(&ctx);
7034
#if defined(DO_PPC_STATISTICS)
7035
        handler->count++;
7036
#endif
7037
        /* Check trace mode exceptions */
7038
        if (unlikely(branch_step != 0 &&
7039
                     ctx.exception == POWERPC_EXCP_BRANCH)) {
7040
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
7041
        } else if (unlikely(single_step != 0 &&
7042
                            (ctx.nip <= 0x100 || ctx.nip > 0xF00 ||
7043
                             (ctx.nip & 0xFC) != 0x04) &&
7044
                            ctx.exception != POWERPC_SYSCALL &&
7045
                            ctx.exception != POWERPC_EXCP_TRAP)) {
7046
            GEN_EXCP(ctxp, POWERPC_EXCP_TRACE, 0);
7047
        } else if (unlikely(((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) ||
7048
                            (env->singlestep_enabled))) {
7049
            /* if we reach a page boundary or are single stepping, stop
7050
             * generation
7051
             */
7052
            break;
7053
        }
7054
#if defined (DO_SINGLE_STEP)
7055
        break;
7056
#endif
7057
    }
7058
    if (ctx.exception == POWERPC_EXCP_NONE) {
7059
        gen_goto_tb(&ctx, 0, ctx.nip);
7060
    } else if (ctx.exception != POWERPC_EXCP_BRANCH) {
7061
        gen_op_reset_T0();
7062
        /* Generate the return instruction */
7063
        gen_op_exit_tb();
7064
    }
7065
    *gen_opc_ptr = INDEX_op_end;
7066
    if (unlikely(search_pc)) {
7067
        j = gen_opc_ptr - gen_opc_buf;
7068
        lj++;
7069
        while (lj <= j)
7070
            gen_opc_instr_start[lj++] = 0;
7071
    } else {
7072
        tb->size = ctx.nip - pc_start;
7073
    }
7074
#if defined(DEBUG_DISAS)
7075
    if (loglevel & CPU_LOG_TB_CPU) {
7076
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
7077
        cpu_dump_state(env, logfile, fprintf, 0);
7078
    }
7079
    if (loglevel & CPU_LOG_TB_IN_ASM) {
7080
        int flags;
7081
        flags = env->bfd_mach;
7082
        flags |= little_endian << 16;
7083
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
7084
        target_disas(logfile, pc_start, ctx.nip - pc_start, flags);
7085
        fprintf(logfile, "\n");
7086
    }
7087
    if (loglevel & CPU_LOG_TB_OP) {
7088
        fprintf(logfile, "OP:\n");
7089
        dump_ops(gen_opc_buf, gen_opparam_buf);
7090
        fprintf(logfile, "\n");
7091
    }
7092
#endif
7093
    return 0;
7094
}
7095

    
7096
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
7097
{
7098
    return gen_intermediate_code_internal(env, tb, 0);
7099
}
7100

    
7101
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
7102
{
7103
    return gen_intermediate_code_internal(env, tb, 1);
7104
}