Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ 2d618793

History | View | Annotate | Download (114.9 kB)

1
/*
2
 *  PPC emulation for qemu: main translation routines.
3
 * 
4
 *  Copyright (c) 2003 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
//#define DO_SINGLE_STEP
31
//#define PPC_DEBUG_DISAS
32

    
33
enum {
34
#define DEF(s, n, copy_size) INDEX_op_ ## s,
35
#include "opc.h"
36
#undef DEF
37
    NB_OPS,
38
};
39

    
40
static uint16_t *gen_opc_ptr;
41
static uint32_t *gen_opparam_ptr;
42

    
43
#include "gen-op.h"
44

    
45
#define GEN8(func, NAME) \
46
static GenOpFunc *NAME ## _table [8] = {                                      \
47
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
48
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
49
};                                                                            \
50
static inline void func(int n)                                                \
51
{                                                                             \
52
    NAME ## _table[n]();                                                      \
53
}
54

    
55
#define GEN16(func, NAME)                                                     \
56
static GenOpFunc *NAME ## _table [16] = {                                     \
57
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
58
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
59
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
60
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
61
};                                                                            \
62
static inline void func(int n)                                                \
63
{                                                                             \
64
    NAME ## _table[n]();                                                      \
65
}
66

    
67
#define GEN32(func, NAME) \
68
static GenOpFunc *NAME ## _table [32] = {                                     \
69
NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3,                                   \
70
NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7,                                   \
71
NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11,                                 \
72
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
73
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,                               \
74
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,                               \
75
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,                               \
76
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,                               \
77
};                                                                            \
78
static inline void func(int n)                                                \
79
{                                                                             \
80
    NAME ## _table[n]();                                                      \
81
}
82

    
83
/* Condition register moves */
84
GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);
85
GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);
86
GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);
87
GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);
88

    
89
/* Floating point condition and status register moves */
90
GEN8(gen_op_load_fpscr_T0, gen_op_load_fpscr_T0_fpscr);
91
GEN8(gen_op_store_T0_fpscr, gen_op_store_T0_fpscr_fpscr);
92
GEN8(gen_op_clear_fpscr, gen_op_clear_fpscr_fpscr);
93
static GenOpFunc1 *gen_op_store_T0_fpscri_fpscr_table[8] = {
94
    &gen_op_store_T0_fpscri_fpscr0,
95
    &gen_op_store_T0_fpscri_fpscr1,
96
    &gen_op_store_T0_fpscri_fpscr2,
97
    &gen_op_store_T0_fpscri_fpscr3,
98
    &gen_op_store_T0_fpscri_fpscr4,
99
    &gen_op_store_T0_fpscri_fpscr5,
100
    &gen_op_store_T0_fpscri_fpscr6,
101
    &gen_op_store_T0_fpscri_fpscr7,
102
};
103
static inline void gen_op_store_T0_fpscri(int n, uint8_t param)
104
{
105
    (*gen_op_store_T0_fpscri_fpscr_table[n])(param);
106
}
107

    
108
/* Segment register moves */
109
GEN16(gen_op_load_sr, gen_op_load_sr);
110
GEN16(gen_op_store_sr, gen_op_store_sr);
111

    
112
/* General purpose registers moves */
113
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
114
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
115
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);
116

    
117
GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
118
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
119
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
120

    
121
/* floating point registers moves */
122
GEN32(gen_op_load_fpr_FT0, gen_op_load_fpr_FT0_fpr);
123
GEN32(gen_op_load_fpr_FT1, gen_op_load_fpr_FT1_fpr);
124
GEN32(gen_op_load_fpr_FT2, gen_op_load_fpr_FT2_fpr);
125
GEN32(gen_op_store_FT0_fpr, gen_op_store_FT0_fpr_fpr);
126
GEN32(gen_op_store_FT1_fpr, gen_op_store_FT1_fpr_fpr);
127
GEN32(gen_op_store_FT2_fpr, gen_op_store_FT2_fpr_fpr);
128

    
129
static uint8_t  spr_access[1024 / 2];
130

    
131
/* internal defines */
132
typedef struct DisasContext {
133
    struct TranslationBlock *tb;
134
    target_ulong nip;
135
    uint32_t opcode;
136
    uint32_t exception;
137
    /* Routine used to access memory */
138
    int mem_idx;
139
    /* Translation flags */
140
#if !defined(CONFIG_USER_ONLY)
141
    int supervisor;
142
#endif
143
    int fpu_enabled;
144
} DisasContext;
145

    
146
typedef struct opc_handler_t {
147
    /* invalid bits */
148
    uint32_t inval;
149
    /* instruction type */
150
    uint32_t type;
151
    /* handler */
152
    void (*handler)(DisasContext *ctx);
153
} opc_handler_t;
154

    
155
#define RET_EXCP(ctx, excp, error)                                            \
156
do {                                                                          \
157
    if ((ctx)->exception == EXCP_NONE) {                                      \
158
        gen_op_update_nip((ctx)->nip);                                        \
159
    }                                                                         \
160
    gen_op_raise_exception_err((excp), (error));                              \
161
    ctx->exception = (excp);                                                  \
162
} while (0)
163

    
164
#define RET_INVAL(ctx)                                                        \
165
RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_INVAL)
166

    
167
#define RET_PRIVOPC(ctx)                                                      \
168
RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_OPC)
169

    
170
#define RET_PRIVREG(ctx)                                                      \
171
RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_REG)
172

    
173
#define RET_MTMSR(ctx)                                                        \
174
RET_EXCP((ctx), EXCP_MTMSR, 0)
175

    
176
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
177
static void gen_##name (DisasContext *ctx);                                   \
178
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
179
static void gen_##name (DisasContext *ctx)
180

    
181
typedef struct opcode_t {
182
    unsigned char opc1, opc2, opc3;
183
#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */
184
    unsigned char pad[5];
185
#else
186
    unsigned char pad[1];
187
#endif
188
    opc_handler_t handler;
189
} opcode_t;
190

    
191
/***                           Instruction decoding                        ***/
192
#define EXTRACT_HELPER(name, shift, nb)                                       \
193
static inline uint32_t name (uint32_t opcode)                                 \
194
{                                                                             \
195
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
196
}
197

    
198
#define EXTRACT_SHELPER(name, shift, nb)                                      \
199
static inline int32_t name (uint32_t opcode)                                  \
200
{                                                                             \
201
    return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1));                \
202
}
203

    
204
/* Opcode part 1 */
205
EXTRACT_HELPER(opc1, 26, 6);
206
/* Opcode part 2 */
207
EXTRACT_HELPER(opc2, 1, 5);
208
/* Opcode part 3 */
209
EXTRACT_HELPER(opc3, 6, 5);
210
/* Update Cr0 flags */
211
EXTRACT_HELPER(Rc, 0, 1);
212
/* Destination */
213
EXTRACT_HELPER(rD, 21, 5);
214
/* Source */
215
EXTRACT_HELPER(rS, 21, 5);
216
/* First operand */
217
EXTRACT_HELPER(rA, 16, 5);
218
/* Second operand */
219
EXTRACT_HELPER(rB, 11, 5);
220
/* Third operand */
221
EXTRACT_HELPER(rC, 6, 5);
222
/***                               Get CRn                                 ***/
223
EXTRACT_HELPER(crfD, 23, 3);
224
EXTRACT_HELPER(crfS, 18, 3);
225
EXTRACT_HELPER(crbD, 21, 5);
226
EXTRACT_HELPER(crbA, 16, 5);
227
EXTRACT_HELPER(crbB, 11, 5);
228
/* SPR / TBL */
229
EXTRACT_HELPER(SPR, 11, 10);
230
/***                              Get constants                            ***/
231
EXTRACT_HELPER(IMM, 12, 8);
232
/* 16 bits signed immediate value */
233
EXTRACT_SHELPER(SIMM, 0, 16);
234
/* 16 bits unsigned immediate value */
235
EXTRACT_HELPER(UIMM, 0, 16);
236
/* Bit count */
237
EXTRACT_HELPER(NB, 11, 5);
238
/* Shift count */
239
EXTRACT_HELPER(SH, 11, 5);
240
/* Mask start */
241
EXTRACT_HELPER(MB, 6, 5);
242
/* Mask end */
243
EXTRACT_HELPER(ME, 1, 5);
244
/* Trap operand */
245
EXTRACT_HELPER(TO, 21, 5);
246

    
247
EXTRACT_HELPER(CRM, 12, 8);
248
EXTRACT_HELPER(FM, 17, 8);
249
EXTRACT_HELPER(SR, 16, 4);
250
EXTRACT_HELPER(FPIMM, 20, 4);
251

    
252
/***                            Jump target decoding                       ***/
253
/* Displacement */
254
EXTRACT_SHELPER(d, 0, 16);
255
/* Immediate address */
256
static inline uint32_t LI (uint32_t opcode)
257
{
258
    return (opcode >> 0) & 0x03FFFFFC;
259
}
260

    
261
static inline uint32_t BD (uint32_t opcode)
262
{
263
    return (opcode >> 0) & 0xFFFC;
264
}
265

    
266
EXTRACT_HELPER(BO, 21, 5);
267
EXTRACT_HELPER(BI, 16, 5);
268
/* Absolute/relative address */
269
EXTRACT_HELPER(AA, 1, 1);
270
/* Link */
271
EXTRACT_HELPER(LK, 0, 1);
272

    
273
/* Create a mask between <start> and <end> bits */
274
static inline uint32_t MASK (uint32_t start, uint32_t end)
275
{
276
    uint32_t ret;
277

    
278
    ret = (((uint32_t)(-1)) >> (start)) ^ (((uint32_t)(-1) >> (end)) >> 1);
279
    if (start > end)
280
        return ~ret;
281

    
282
    return ret;
283
}
284

    
285
#if defined(__APPLE__)
286
#define OPCODES_SECTION \
287
    __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (8) ))
288
#else
289
#define OPCODES_SECTION \
290
    __attribute__ ((section(".opcodes"), unused, aligned (8) ))
291
#endif
292

    
293
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
294
OPCODES_SECTION opcode_t opc_##name = {                                       \
295
    .opc1 = op1,                                                              \
296
    .opc2 = op2,                                                              \
297
    .opc3 = op3,                                                              \
298
    .pad  = { 0, },                                                           \
299
    .handler = {                                                              \
300
        .inval   = invl,                                                      \
301
        .type = _typ,                                                         \
302
        .handler = &gen_##name,                                               \
303
    },                                                                        \
304
}
305

    
306
#define GEN_OPCODE_MARK(name)                                                 \
307
OPCODES_SECTION opcode_t opc_##name = {                                       \
308
    .opc1 = 0xFF,                                                             \
309
    .opc2 = 0xFF,                                                             \
310
    .opc3 = 0xFF,                                                             \
311
    .pad  = { 0, },                                                           \
312
    .handler = {                                                              \
313
        .inval   = 0x00000000,                                                \
314
        .type = 0x00,                                                         \
315
        .handler = NULL,                                                      \
316
    },                                                                        \
317
}
318

    
319
/* Start opcode list */
320
GEN_OPCODE_MARK(start);
321

    
322
/* Invalid instruction */
323
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
324
{
325
    RET_INVAL(ctx);
326
}
327

    
328
static opc_handler_t invalid_handler = {
329
    .inval   = 0xFFFFFFFF,
330
    .type    = PPC_NONE,
331
    .handler = gen_invalid,
332
};
333

    
334
/***                           Integer arithmetic                          ***/
335
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval)                       \
336
GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
337
{                                                                             \
338
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
339
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
340
    gen_op_##name();                                                          \
341
    if (Rc(ctx->opcode) != 0)                                                 \
342
        gen_op_set_Rc0();                                                     \
343
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
344
}
345

    
346
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval)                     \
347
GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
348
{                                                                             \
349
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
350
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
351
    gen_op_##name();                                                          \
352
    if (Rc(ctx->opcode) != 0)                                                 \
353
        gen_op_set_Rc0();                                                     \
354
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
355
}
356

    
357
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3)                              \
358
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
359
{                                                                             \
360
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
361
    gen_op_##name();                                                          \
362
    if (Rc(ctx->opcode) != 0)                                                 \
363
        gen_op_set_Rc0();                                                     \
364
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
365
}
366
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3)                            \
367
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
368
{                                                                             \
369
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
370
    gen_op_##name();                                                          \
371
    if (Rc(ctx->opcode) != 0)                                                 \
372
        gen_op_set_Rc0();                                                     \
373
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
374
}
375

    
376
/* Two operands arithmetic functions */
377
#define GEN_INT_ARITH2(name, opc1, opc2, opc3)                                \
378
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000)                          \
379
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000)
380

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

    
385
/* One operand arithmetic functions */
386
#define GEN_INT_ARITH1(name, opc1, opc2, opc3)                                \
387
__GEN_INT_ARITH1(name, opc1, opc2, opc3)                                      \
388
__GEN_INT_ARITH1_O(name##o, opc1, opc2, opc3 | 0x10)
389

    
390
/* add    add.    addo    addo.    */
391
GEN_INT_ARITH2 (add,    0x1F, 0x0A, 0x08);
392
/* addc   addc.   addco   addco.   */
393
GEN_INT_ARITH2 (addc,   0x1F, 0x0A, 0x00);
394
/* adde   adde.   addeo   addeo.   */
395
GEN_INT_ARITH2 (adde,   0x1F, 0x0A, 0x04);
396
/* addme  addme.  addmeo  addmeo.  */
397
GEN_INT_ARITH1 (addme,  0x1F, 0x0A, 0x07);
398
/* addze  addze.  addzeo  addzeo.  */
399
GEN_INT_ARITH1 (addze,  0x1F, 0x0A, 0x06);
400
/* divw   divw.   divwo   divwo.   */
401
GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F);
402
/* divwu  divwu.  divwuo  divwuo.  */
403
GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E);
404
/* mulhw  mulhw.                   */
405
GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02);
406
/* mulhwu mulhwu.                  */
407
GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00);
408
/* mullw  mullw.  mullwo  mullwo.  */
409
GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07);
410
/* neg    neg.    nego    nego.    */
411
GEN_INT_ARITH1 (neg,    0x1F, 0x08, 0x03);
412
/* subf   subf.   subfo   subfo.   */
413
GEN_INT_ARITH2 (subf,   0x1F, 0x08, 0x01);
414
/* subfc  subfc.  subfco  subfco.  */
415
GEN_INT_ARITH2 (subfc,  0x1F, 0x08, 0x00);
416
/* subfe  subfe.  subfeo  subfeo.  */
417
GEN_INT_ARITH2 (subfe,  0x1F, 0x08, 0x04);
418
/* subfme subfme. subfmeo subfmeo. */
419
GEN_INT_ARITH1 (subfme, 0x1F, 0x08, 0x07);
420
/* subfze subfze. subfzeo subfzeo. */
421
GEN_INT_ARITH1 (subfze, 0x1F, 0x08, 0x06);
422
/* addi */
423
GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
424
{
425
    int32_t simm = SIMM(ctx->opcode);
426

    
427
    if (rA(ctx->opcode) == 0) {
428
        gen_op_set_T0(simm);
429
    } else {
430
        gen_op_load_gpr_T0(rA(ctx->opcode));
431
        gen_op_addi(simm);
432
    }
433
    gen_op_store_T0_gpr(rD(ctx->opcode));
434
}
435
/* addic */
436
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
437
{
438
    gen_op_load_gpr_T0(rA(ctx->opcode));
439
    gen_op_addic(SIMM(ctx->opcode));
440
    gen_op_store_T0_gpr(rD(ctx->opcode));
441
}
442
/* addic. */
443
GEN_HANDLER(addic_, 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
444
{
445
    gen_op_load_gpr_T0(rA(ctx->opcode));
446
    gen_op_addic(SIMM(ctx->opcode));
447
    gen_op_set_Rc0();
448
    gen_op_store_T0_gpr(rD(ctx->opcode));
449
}
450
/* addis */
451
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
452
{
453
    int32_t simm = SIMM(ctx->opcode);
454

    
455
    if (rA(ctx->opcode) == 0) {
456
        gen_op_set_T0(simm << 16);
457
    } else {
458
        gen_op_load_gpr_T0(rA(ctx->opcode));
459
        gen_op_addi(simm << 16);
460
    }
461
    gen_op_store_T0_gpr(rD(ctx->opcode));
462
}
463
/* mulli */
464
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
465
{
466
    gen_op_load_gpr_T0(rA(ctx->opcode));
467
    gen_op_mulli(SIMM(ctx->opcode));
468
    gen_op_store_T0_gpr(rD(ctx->opcode));
469
}
470
/* subfic */
471
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
472
{
473
    gen_op_load_gpr_T0(rA(ctx->opcode));
474
    gen_op_subfic(SIMM(ctx->opcode));
475
    gen_op_store_T0_gpr(rD(ctx->opcode));
476
}
477

    
478
/***                           Integer comparison                          ***/
479
#define GEN_CMP(name, opc)                                                    \
480
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, PPC_INTEGER)                   \
481
{                                                                             \
482
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
483
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
484
    gen_op_##name();                                                          \
485
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
486
}
487

    
488
/* cmp */
489
GEN_CMP(cmp, 0x00);
490
/* cmpi */
491
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
492
{
493
    gen_op_load_gpr_T0(rA(ctx->opcode));
494
    gen_op_cmpi(SIMM(ctx->opcode));
495
    gen_op_store_T0_crf(crfD(ctx->opcode));
496
}
497
/* cmpl */
498
GEN_CMP(cmpl, 0x01);
499
/* cmpli */
500
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
501
{
502
    gen_op_load_gpr_T0(rA(ctx->opcode));
503
    gen_op_cmpli(UIMM(ctx->opcode));
504
    gen_op_store_T0_crf(crfD(ctx->opcode));
505
}
506

    
507
/***                            Integer logical                            ***/
508
#define __GEN_LOGICAL2(name, opc2, opc3)                                      \
509
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, PPC_INTEGER)                  \
510
{                                                                             \
511
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
512
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
513
    gen_op_##name();                                                          \
514
    if (Rc(ctx->opcode) != 0)                                                 \
515
        gen_op_set_Rc0();                                                     \
516
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
517
}
518
#define GEN_LOGICAL2(name, opc)                                               \
519
__GEN_LOGICAL2(name, 0x1C, opc)
520

    
521
#define GEN_LOGICAL1(name, opc)                                               \
522
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, PPC_INTEGER)                   \
523
{                                                                             \
524
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
525
    gen_op_##name();                                                          \
526
    if (Rc(ctx->opcode) != 0)                                                 \
527
        gen_op_set_Rc0();                                                     \
528
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
529
}
530

    
531
/* and & and. */
532
GEN_LOGICAL2(and, 0x00);
533
/* andc & andc. */
534
GEN_LOGICAL2(andc, 0x01);
535
/* andi. */
536
GEN_HANDLER(andi_, 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
537
{
538
    gen_op_load_gpr_T0(rS(ctx->opcode));
539
    gen_op_andi_(UIMM(ctx->opcode));
540
    gen_op_set_Rc0();
541
    gen_op_store_T0_gpr(rA(ctx->opcode));
542
}
543
/* andis. */
544
GEN_HANDLER(andis_, 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
545
{
546
    gen_op_load_gpr_T0(rS(ctx->opcode));
547
    gen_op_andi_(UIMM(ctx->opcode) << 16);
548
    gen_op_set_Rc0();
549
    gen_op_store_T0_gpr(rA(ctx->opcode));
550
}
551

    
552
/* cntlzw */
553
GEN_LOGICAL1(cntlzw, 0x00);
554
/* eqv & eqv. */
555
GEN_LOGICAL2(eqv, 0x08);
556
/* extsb & extsb. */
557
GEN_LOGICAL1(extsb, 0x1D);
558
/* extsh & extsh. */
559
GEN_LOGICAL1(extsh, 0x1C);
560
/* nand & nand. */
561
GEN_LOGICAL2(nand, 0x0E);
562
/* nor & nor. */
563
GEN_LOGICAL2(nor, 0x03);
564

    
565
/* or & or. */
566
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
567
{
568
    gen_op_load_gpr_T0(rS(ctx->opcode));
569
    /* Optimisation for mr case */
570
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
571
        gen_op_load_gpr_T1(rB(ctx->opcode));
572
        gen_op_or();
573
    }
574
    if (Rc(ctx->opcode) != 0)
575
        gen_op_set_Rc0();
576
    gen_op_store_T0_gpr(rA(ctx->opcode));
577
}
578

    
579
/* orc & orc. */
580
GEN_LOGICAL2(orc, 0x0C);
581
/* xor & xor. */
582
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
583
{
584
    gen_op_load_gpr_T0(rS(ctx->opcode));
585
    /* Optimisation for "set to zero" case */
586
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
587
        gen_op_load_gpr_T1(rB(ctx->opcode));
588
        gen_op_xor();
589
    } else {
590
        gen_op_set_T0(0);
591
    }
592
    if (Rc(ctx->opcode) != 0)
593
        gen_op_set_Rc0();
594
    gen_op_store_T0_gpr(rA(ctx->opcode));
595
}
596
/* ori */
597
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
598
{
599
    uint32_t uimm = UIMM(ctx->opcode);
600

    
601
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
602
        /* NOP */
603
        return;
604
        }
605
        gen_op_load_gpr_T0(rS(ctx->opcode));
606
    if (uimm != 0)
607
        gen_op_ori(uimm);
608
        gen_op_store_T0_gpr(rA(ctx->opcode));
609
}
610
/* oris */
611
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
612
{
613
    uint32_t uimm = UIMM(ctx->opcode);
614

    
615
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
616
        /* NOP */
617
        return;
618
        }
619
        gen_op_load_gpr_T0(rS(ctx->opcode));
620
    if (uimm != 0)
621
        gen_op_ori(uimm << 16);
622
        gen_op_store_T0_gpr(rA(ctx->opcode));
623
}
624
/* xori */
625
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
626
{
627
    uint32_t uimm = UIMM(ctx->opcode);
628

    
629
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
630
        /* NOP */
631
        return;
632
    }
633
    gen_op_load_gpr_T0(rS(ctx->opcode));
634
    if (uimm != 0)
635
    gen_op_xori(uimm);
636
    gen_op_store_T0_gpr(rA(ctx->opcode));
637
}
638

    
639
/* xoris */
640
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
641
{
642
    uint32_t uimm = UIMM(ctx->opcode);
643

    
644
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
645
        /* NOP */
646
        return;
647
    }
648
    gen_op_load_gpr_T0(rS(ctx->opcode));
649
    if (uimm != 0)
650
    gen_op_xori(uimm << 16);
651
    gen_op_store_T0_gpr(rA(ctx->opcode));
652
}
653

    
654
/***                             Integer rotate                            ***/
655
/* rlwimi & rlwimi. */
656
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
657
{
658
    uint32_t mb, me;
659

    
660
    mb = MB(ctx->opcode);
661
    me = ME(ctx->opcode);
662
    gen_op_load_gpr_T0(rS(ctx->opcode));
663
    gen_op_load_gpr_T1(rA(ctx->opcode));
664
    gen_op_rlwimi(SH(ctx->opcode), MASK(mb, me), ~MASK(mb, me));
665
    if (Rc(ctx->opcode) != 0)
666
        gen_op_set_Rc0();
667
    gen_op_store_T0_gpr(rA(ctx->opcode));
668
}
669
/* rlwinm & rlwinm. */
670
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
671
{
672
    uint32_t mb, me, sh;
673
    
674
    sh = SH(ctx->opcode);
675
    mb = MB(ctx->opcode);
676
    me = ME(ctx->opcode);
677
    gen_op_load_gpr_T0(rS(ctx->opcode));
678
#if 1 // TRY
679
    if (sh == 0) {
680
        gen_op_andi_(MASK(mb, me));
681
        goto store;
682
    }
683
#endif
684
    if (mb == 0) {
685
        if (me == 31) {
686
            gen_op_rotlwi(sh);
687
            goto store;
688
#if 0
689
        } else if (me == (31 - sh)) {
690
            gen_op_slwi(sh);
691
            goto store;
692
#endif
693
        }
694
    } else if (me == 31) {
695
#if 0
696
        if (sh == (32 - mb)) {
697
            gen_op_srwi(mb);
698
            goto store;
699
        }
700
#endif
701
    }
702
    gen_op_rlwinm(sh, MASK(mb, me));
703
store:
704
    if (Rc(ctx->opcode) != 0)
705
        gen_op_set_Rc0();
706
    gen_op_store_T0_gpr(rA(ctx->opcode));
707
}
708
/* rlwnm & rlwnm. */
709
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
710
{
711
    uint32_t mb, me;
712

    
713
    mb = MB(ctx->opcode);
714
    me = ME(ctx->opcode);
715
    gen_op_load_gpr_T0(rS(ctx->opcode));
716
    gen_op_load_gpr_T1(rB(ctx->opcode));
717
    if (mb == 0 && me == 31) {
718
        gen_op_rotl();
719
    } else
720
    {
721
        gen_op_rlwnm(MASK(mb, me));
722
    }
723
    if (Rc(ctx->opcode) != 0)
724
        gen_op_set_Rc0();
725
    gen_op_store_T0_gpr(rA(ctx->opcode));
726
}
727

    
728
/***                             Integer shift                             ***/
729
/* slw & slw. */
730
__GEN_LOGICAL2(slw, 0x18, 0x00);
731
/* sraw & sraw. */
732
__GEN_LOGICAL2(sraw, 0x18, 0x18);
733
/* srawi & srawi. */
734
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
735
{
736
    gen_op_load_gpr_T0(rS(ctx->opcode));
737
    if (SH(ctx->opcode) != 0)
738
    gen_op_srawi(SH(ctx->opcode), MASK(32 - SH(ctx->opcode), 31));
739
    if (Rc(ctx->opcode) != 0)
740
        gen_op_set_Rc0();
741
    gen_op_store_T0_gpr(rA(ctx->opcode));
742
}
743
/* srw & srw. */
744
__GEN_LOGICAL2(srw, 0x18, 0x10);
745

    
746
/***                       Floating-Point arithmetic                       ***/
747
#define _GEN_FLOAT_ACB(name, op, op1, op2, isfloat)                           \
748
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, PPC_FLOAT)                   \
749
{                                                                             \
750
    if (!ctx->fpu_enabled) {                                                  \
751
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
752
        return;                                                               \
753
    }                                                                         \
754
    gen_op_reset_scrfx();                                                     \
755
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
756
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
757
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
758
    gen_op_f##op();                                                           \
759
    if (isfloat) {                                                            \
760
        gen_op_frsp();                                                        \
761
    }                                                                         \
762
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
763
    if (Rc(ctx->opcode))                                                      \
764
        gen_op_set_Rc1();                                                     \
765
}
766

    
767
#define GEN_FLOAT_ACB(name, op2)                                              \
768
_GEN_FLOAT_ACB(name, name, 0x3F, op2, 0);                                     \
769
_GEN_FLOAT_ACB(name##s, name, 0x3B, op2, 1);
770

    
771
#define _GEN_FLOAT_AB(name, op, op1, op2, inval, isfloat)                     \
772
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
773
{                                                                             \
774
    if (!ctx->fpu_enabled) {                                                  \
775
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
776
        return;                                                               \
777
    }                                                                         \
778
    gen_op_reset_scrfx();                                                     \
779
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
780
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
781
    gen_op_f##op();                                                           \
782
    if (isfloat) {                                                            \
783
        gen_op_frsp();                                                        \
784
    }                                                                         \
785
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
786
    if (Rc(ctx->opcode))                                                      \
787
        gen_op_set_Rc1();                                                     \
788
}
789
#define GEN_FLOAT_AB(name, op2, inval)                                        \
790
_GEN_FLOAT_AB(name, name, 0x3F, op2, inval, 0);                               \
791
_GEN_FLOAT_AB(name##s, name, 0x3B, op2, inval, 1);
792

    
793
#define _GEN_FLOAT_AC(name, op, op1, op2, inval, isfloat)                     \
794
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
795
{                                                                             \
796
    if (!ctx->fpu_enabled) {                                                  \
797
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
798
        return;                                                               \
799
    }                                                                         \
800
    gen_op_reset_scrfx();                                                     \
801
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
802
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
803
    gen_op_f##op();                                                           \
804
    if (isfloat) {                                                            \
805
        gen_op_frsp();                                                        \
806
    }                                                                         \
807
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
808
    if (Rc(ctx->opcode))                                                      \
809
        gen_op_set_Rc1();                                                     \
810
}
811
#define GEN_FLOAT_AC(name, op2, inval)                                        \
812
_GEN_FLOAT_AC(name, name, 0x3F, op2, inval, 0);                               \
813
_GEN_FLOAT_AC(name##s, name, 0x3B, op2, inval, 1);
814

    
815
#define GEN_FLOAT_B(name, op2, op3)                                           \
816
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, PPC_FLOAT)                   \
817
{                                                                             \
818
    if (!ctx->fpu_enabled) {                                                  \
819
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
820
        return;                                                               \
821
    }                                                                         \
822
    gen_op_reset_scrfx();                                                     \
823
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
824
    gen_op_f##name();                                                         \
825
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
826
    if (Rc(ctx->opcode))                                                      \
827
        gen_op_set_Rc1();                                                     \
828
}
829

    
830
#define GEN_FLOAT_BS(name, op1, op2)                                          \
831
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x001F07C0, PPC_FLOAT)                   \
832
{                                                                             \
833
    if (!ctx->fpu_enabled) {                                                  \
834
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
835
        return;                                                               \
836
    }                                                                         \
837
    gen_op_reset_scrfx();                                                     \
838
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
839
    gen_op_f##name();                                                         \
840
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
841
    if (Rc(ctx->opcode))                                                      \
842
        gen_op_set_Rc1();                                                     \
843
}
844

    
845
/* fadd - fadds */
846
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
847
/* fdiv - fdivs */
848
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
849
/* fmul - fmuls */
850
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
851

    
852
/* fres */
853
GEN_FLOAT_BS(res, 0x3B, 0x18);
854

    
855
/* frsqrte */
856
GEN_FLOAT_BS(rsqrte, 0x3F, 0x1A);
857

    
858
/* fsel */
859
_GEN_FLOAT_ACB(sel, sel, 0x3F, 0x17, 0);
860
/* fsub - fsubs */
861
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
862
/* Optional: */
863
/* fsqrt */
864
GEN_HANDLER(fsqrt, 0x3F, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_OPT)
865
{
866
    if (!ctx->fpu_enabled) {
867
        RET_EXCP(ctx, EXCP_NO_FP, 0);
868
        return;
869
    }
870
    gen_op_reset_scrfx();
871
    gen_op_load_fpr_FT0(rB(ctx->opcode));
872
    gen_op_fsqrt();
873
    gen_op_store_FT0_fpr(rD(ctx->opcode));
874
    if (Rc(ctx->opcode))
875
        gen_op_set_Rc1();
876
}
877

    
878
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_OPT)
879
{
880
    if (!ctx->fpu_enabled) {
881
        RET_EXCP(ctx, EXCP_NO_FP, 0);
882
        return;
883
    }
884
    gen_op_reset_scrfx();
885
    gen_op_load_fpr_FT0(rB(ctx->opcode));
886
    gen_op_fsqrt();
887
    gen_op_frsp();
888
    gen_op_store_FT0_fpr(rD(ctx->opcode));
889
    if (Rc(ctx->opcode))
890
        gen_op_set_Rc1();
891
}
892

    
893
/***                     Floating-Point multiply-and-add                   ***/
894
/* fmadd - fmadds */
895
GEN_FLOAT_ACB(madd, 0x1D);
896
/* fmsub - fmsubs */
897
GEN_FLOAT_ACB(msub, 0x1C);
898
/* fnmadd - fnmadds */
899
GEN_FLOAT_ACB(nmadd, 0x1F);
900
/* fnmsub - fnmsubs */
901
GEN_FLOAT_ACB(nmsub, 0x1E);
902

    
903
/***                     Floating-Point round & convert                    ***/
904
/* fctiw */
905
GEN_FLOAT_B(ctiw, 0x0E, 0x00);
906
/* fctiwz */
907
GEN_FLOAT_B(ctiwz, 0x0F, 0x00);
908
/* frsp */
909
GEN_FLOAT_B(rsp, 0x0C, 0x00);
910

    
911
/***                         Floating-Point compare                        ***/
912
/* fcmpo */
913
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
914
{
915
    if (!ctx->fpu_enabled) {
916
        RET_EXCP(ctx, EXCP_NO_FP, 0);
917
        return;
918
    }
919
    gen_op_reset_scrfx();
920
    gen_op_load_fpr_FT0(rA(ctx->opcode));
921
    gen_op_load_fpr_FT1(rB(ctx->opcode));
922
    gen_op_fcmpo();
923
    gen_op_store_T0_crf(crfD(ctx->opcode));
924
}
925

    
926
/* fcmpu */
927
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
928
{
929
    if (!ctx->fpu_enabled) {
930
        RET_EXCP(ctx, EXCP_NO_FP, 0);
931
        return;
932
    }
933
    gen_op_reset_scrfx();
934
    gen_op_load_fpr_FT0(rA(ctx->opcode));
935
    gen_op_load_fpr_FT1(rB(ctx->opcode));
936
    gen_op_fcmpu();
937
    gen_op_store_T0_crf(crfD(ctx->opcode));
938
}
939

    
940
/***                         Floating-point move                           ***/
941
/* fabs */
942
GEN_FLOAT_B(abs, 0x08, 0x08);
943

    
944
/* fmr  - fmr. */
945
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
946
{
947
    if (!ctx->fpu_enabled) {
948
        RET_EXCP(ctx, EXCP_NO_FP, 0);
949
        return;
950
    }
951
    gen_op_reset_scrfx();
952
    gen_op_load_fpr_FT0(rB(ctx->opcode));
953
    gen_op_store_FT0_fpr(rD(ctx->opcode));
954
    if (Rc(ctx->opcode))
955
        gen_op_set_Rc1();
956
}
957

    
958
/* fnabs */
959
GEN_FLOAT_B(nabs, 0x08, 0x04);
960
/* fneg */
961
GEN_FLOAT_B(neg, 0x08, 0x01);
962

    
963
/***                  Floating-Point status & ctrl register                ***/
964
/* mcrfs */
965
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
966
{
967
    if (!ctx->fpu_enabled) {
968
        RET_EXCP(ctx, EXCP_NO_FP, 0);
969
        return;
970
    }
971
    gen_op_load_fpscr_T0(crfS(ctx->opcode));
972
    gen_op_store_T0_crf(crfD(ctx->opcode));
973
    gen_op_clear_fpscr(crfS(ctx->opcode));
974
}
975

    
976
/* mffs */
977
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
978
{
979
    if (!ctx->fpu_enabled) {
980
        RET_EXCP(ctx, EXCP_NO_FP, 0);
981
        return;
982
    }
983
    gen_op_load_fpscr();
984
    gen_op_store_FT0_fpr(rD(ctx->opcode));
985
    if (Rc(ctx->opcode))
986
        gen_op_set_Rc1();
987
}
988

    
989
/* mtfsb0 */
990
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
991
{
992
    uint8_t crb;
993
    
994
    if (!ctx->fpu_enabled) {
995
        RET_EXCP(ctx, EXCP_NO_FP, 0);
996
        return;
997
    }
998
    crb = crbD(ctx->opcode) >> 2;
999
    gen_op_load_fpscr_T0(crb);
1000
    gen_op_andi_(~(1 << (crbD(ctx->opcode) & 0x03)));
1001
    gen_op_store_T0_fpscr(crb);
1002
    if (Rc(ctx->opcode))
1003
        gen_op_set_Rc1();
1004
}
1005

    
1006
/* mtfsb1 */
1007
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
1008
{
1009
    uint8_t crb;
1010
    
1011
    if (!ctx->fpu_enabled) {
1012
        RET_EXCP(ctx, EXCP_NO_FP, 0);
1013
        return;
1014
    }
1015
    crb = crbD(ctx->opcode) >> 2;
1016
    gen_op_load_fpscr_T0(crb);
1017
    gen_op_ori(1 << (crbD(ctx->opcode) & 0x03));
1018
    gen_op_store_T0_fpscr(crb);
1019
    if (Rc(ctx->opcode))
1020
        gen_op_set_Rc1();
1021
}
1022

    
1023
/* mtfsf */
1024
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
1025
{
1026
    if (!ctx->fpu_enabled) {
1027
        RET_EXCP(ctx, EXCP_NO_FP, 0);
1028
        return;
1029
    }
1030
    gen_op_load_fpr_FT0(rB(ctx->opcode));
1031
    gen_op_store_fpscr(FM(ctx->opcode));
1032
    if (Rc(ctx->opcode))
1033
        gen_op_set_Rc1();
1034
}
1035

    
1036
/* mtfsfi */
1037
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
1038
{
1039
    if (!ctx->fpu_enabled) {
1040
        RET_EXCP(ctx, EXCP_NO_FP, 0);
1041
        return;
1042
    }
1043
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
1044
    if (Rc(ctx->opcode))
1045
        gen_op_set_Rc1();
1046
}
1047

    
1048
/***                             Integer load                              ***/
1049
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
1050
#if defined(CONFIG_USER_ONLY)
1051
#define OP_LD_TABLE(width)                                                    \
1052
static GenOpFunc *gen_op_l##width[] = {                                       \
1053
    &gen_op_l##width##_raw,                                                   \
1054
    &gen_op_l##width##_le_raw,                                                \
1055
};
1056
#define OP_ST_TABLE(width)                                                    \
1057
static GenOpFunc *gen_op_st##width[] = {                                      \
1058
    &gen_op_st##width##_raw,                                                  \
1059
    &gen_op_st##width##_le_raw,                                               \
1060
};
1061
/* Byte access routine are endian safe */
1062
#define gen_op_stb_le_raw gen_op_stb_raw
1063
#define gen_op_lbz_le_raw gen_op_lbz_raw
1064
#else
1065
#define OP_LD_TABLE(width)                                                    \
1066
static GenOpFunc *gen_op_l##width[] = {                                       \
1067
    &gen_op_l##width##_user,                                                  \
1068
    &gen_op_l##width##_le_user,                                               \
1069
    &gen_op_l##width##_kernel,                                                \
1070
    &gen_op_l##width##_le_kernel,                                             \
1071
};
1072
#define OP_ST_TABLE(width)                                                    \
1073
static GenOpFunc *gen_op_st##width[] = {                                      \
1074
    &gen_op_st##width##_user,                                                 \
1075
    &gen_op_st##width##_le_user,                                              \
1076
    &gen_op_st##width##_kernel,                                               \
1077
    &gen_op_st##width##_le_kernel,                                            \
1078
};
1079
/* Byte access routine are endian safe */
1080
#define gen_op_stb_le_user gen_op_stb_user
1081
#define gen_op_lbz_le_user gen_op_lbz_user
1082
#define gen_op_stb_le_kernel gen_op_stb_kernel
1083
#define gen_op_lbz_le_kernel gen_op_lbz_kernel
1084
#endif
1085

    
1086
#define GEN_LD(width, opc)                                                    \
1087
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)               \
1088
{                                                                             \
1089
    uint32_t simm = SIMM(ctx->opcode);                                        \
1090
    if (rA(ctx->opcode) == 0) {                                               \
1091
        gen_op_set_T0(simm);                                                  \
1092
    } else {                                                                  \
1093
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1094
        if (simm != 0)                                                        \
1095
            gen_op_addi(simm);                                                \
1096
    }                                                                         \
1097
    op_ldst(l##width);                                                        \
1098
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1099
}
1100

    
1101
#define GEN_LDU(width, opc)                                                   \
1102
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
1103
{                                                                             \
1104
    uint32_t simm = SIMM(ctx->opcode);                                        \
1105
    if (rA(ctx->opcode) == 0 ||                                               \
1106
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1107
        RET_INVAL(ctx);                                                       \
1108
        return;                                                               \
1109
    }                                                                         \
1110
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1111
    if (simm != 0)                                                            \
1112
        gen_op_addi(simm);                                                    \
1113
    op_ldst(l##width);                                                        \
1114
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1115
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1116
}
1117

    
1118
#define GEN_LDUX(width, opc)                                                  \
1119
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
1120
{                                                                             \
1121
    if (rA(ctx->opcode) == 0 ||                                               \
1122
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1123
        RET_INVAL(ctx);                                                       \
1124
        return;                                                               \
1125
    }                                                                         \
1126
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1127
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1128
    gen_op_add();                                                             \
1129
    op_ldst(l##width);                                                        \
1130
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1131
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1132
}
1133

    
1134
#define GEN_LDX(width, opc2, opc3)                                            \
1135
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
1136
{                                                                             \
1137
    if (rA(ctx->opcode) == 0) {                                               \
1138
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1139
    } else {                                                                  \
1140
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1141
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1142
        gen_op_add();                                                         \
1143
    }                                                                         \
1144
    op_ldst(l##width);                                                        \
1145
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1146
}
1147

    
1148
#define GEN_LDS(width, op)                                                    \
1149
OP_LD_TABLE(width);                                                           \
1150
GEN_LD(width, op | 0x20);                                                     \
1151
GEN_LDU(width, op | 0x21);                                                    \
1152
GEN_LDUX(width, op | 0x01);                                                   \
1153
GEN_LDX(width, 0x17, op | 0x00)
1154

    
1155
/* lbz lbzu lbzux lbzx */
1156
GEN_LDS(bz, 0x02);
1157
/* lha lhau lhaux lhax */
1158
GEN_LDS(ha, 0x0A);
1159
/* lhz lhzu lhzux lhzx */
1160
GEN_LDS(hz, 0x08);
1161
/* lwz lwzu lwzux lwzx */
1162
GEN_LDS(wz, 0x00);
1163

    
1164
/***                              Integer store                            ***/
1165
#define GEN_ST(width, opc)                                                    \
1166
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
1167
{                                                                             \
1168
    uint32_t simm = SIMM(ctx->opcode);                                        \
1169
    if (rA(ctx->opcode) == 0) {                                               \
1170
        gen_op_set_T0(simm);                                                  \
1171
    } else {                                                                  \
1172
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1173
        if (simm != 0)                                                        \
1174
            gen_op_addi(simm);                                                \
1175
    }                                                                         \
1176
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1177
    op_ldst(st##width);                                                       \
1178
}
1179

    
1180
#define GEN_STU(width, opc)                                                   \
1181
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
1182
{                                                                             \
1183
    uint32_t simm = SIMM(ctx->opcode);                                        \
1184
    if (rA(ctx->opcode) == 0) {                                               \
1185
        RET_INVAL(ctx);                                                       \
1186
        return;                                                               \
1187
    }                                                                         \
1188
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1189
    if (simm != 0)                                                            \
1190
        gen_op_addi(simm);                                                    \
1191
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1192
    op_ldst(st##width);                                                       \
1193
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1194
}
1195

    
1196
#define GEN_STUX(width, opc)                                                  \
1197
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
1198
{                                                                             \
1199
    if (rA(ctx->opcode) == 0) {                                               \
1200
        RET_INVAL(ctx);                                                       \
1201
        return;                                                               \
1202
    }                                                                         \
1203
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1204
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1205
    gen_op_add();                                                             \
1206
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1207
    op_ldst(st##width);                                                       \
1208
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1209
}
1210

    
1211
#define GEN_STX(width, opc2, opc3)                                            \
1212
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
1213
{                                                                             \
1214
    if (rA(ctx->opcode) == 0) {                                               \
1215
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1216
    } else {                                                                  \
1217
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1218
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1219
        gen_op_add();                                                         \
1220
    }                                                                         \
1221
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1222
    op_ldst(st##width);                                                       \
1223
}
1224

    
1225
#define GEN_STS(width, op)                                                    \
1226
OP_ST_TABLE(width);                                                           \
1227
GEN_ST(width, op | 0x20);                                                     \
1228
GEN_STU(width, op | 0x21);                                                    \
1229
GEN_STUX(width, op | 0x01);                                                   \
1230
GEN_STX(width, 0x17, op | 0x00)
1231

    
1232
/* stb stbu stbux stbx */
1233
GEN_STS(b, 0x06);
1234
/* sth sthu sthux sthx */
1235
GEN_STS(h, 0x0C);
1236
/* stw stwu stwux stwx */
1237
GEN_STS(w, 0x04);
1238

    
1239
/***                Integer load and store with byte reverse               ***/
1240
/* lhbrx */
1241
OP_LD_TABLE(hbr);
1242
GEN_LDX(hbr, 0x16, 0x18);
1243
/* lwbrx */
1244
OP_LD_TABLE(wbr);
1245
GEN_LDX(wbr, 0x16, 0x10);
1246
/* sthbrx */
1247
OP_ST_TABLE(hbr);
1248
GEN_STX(hbr, 0x16, 0x1C);
1249
/* stwbrx */
1250
OP_ST_TABLE(wbr);
1251
GEN_STX(wbr, 0x16, 0x14);
1252

    
1253
/***                    Integer load and store multiple                    ***/
1254
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
1255
#if defined(CONFIG_USER_ONLY)
1256
static GenOpFunc1 *gen_op_lmw[] = {
1257
    &gen_op_lmw_raw,
1258
    &gen_op_lmw_le_raw,
1259
};
1260
static GenOpFunc1 *gen_op_stmw[] = {
1261
    &gen_op_stmw_raw,
1262
    &gen_op_stmw_le_raw,
1263
};
1264
#else
1265
static GenOpFunc1 *gen_op_lmw[] = {
1266
    &gen_op_lmw_user,
1267
    &gen_op_lmw_le_user,
1268
    &gen_op_lmw_kernel,
1269
    &gen_op_lmw_le_kernel,
1270
};
1271
static GenOpFunc1 *gen_op_stmw[] = {
1272
    &gen_op_stmw_user,
1273
    &gen_op_stmw_le_user,
1274
    &gen_op_stmw_kernel,
1275
    &gen_op_stmw_le_kernel,
1276
};
1277
#endif
1278

    
1279
/* lmw */
1280
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1281
{
1282
    int simm = SIMM(ctx->opcode);
1283

    
1284
    if (rA(ctx->opcode) == 0) {
1285
        gen_op_set_T0(simm);
1286
    } else {
1287
        gen_op_load_gpr_T0(rA(ctx->opcode));
1288
        if (simm != 0)
1289
            gen_op_addi(simm);
1290
    }
1291
    op_ldstm(lmw, rD(ctx->opcode));
1292
}
1293

    
1294
/* stmw */
1295
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1296
{
1297
    int simm = SIMM(ctx->opcode);
1298

    
1299
    if (rA(ctx->opcode) == 0) {
1300
        gen_op_set_T0(simm);
1301
    } else {
1302
        gen_op_load_gpr_T0(rA(ctx->opcode));
1303
        if (simm != 0)
1304
            gen_op_addi(simm);
1305
    }
1306
    op_ldstm(stmw, rS(ctx->opcode));
1307
}
1308

    
1309
/***                    Integer load and store strings                     ***/
1310
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
1311
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
1312
#if defined(CONFIG_USER_ONLY)
1313
static GenOpFunc1 *gen_op_lswi[] = {
1314
    &gen_op_lswi_raw,
1315
    &gen_op_lswi_le_raw,
1316
};
1317
static GenOpFunc3 *gen_op_lswx[] = {
1318
    &gen_op_lswx_raw,
1319
    &gen_op_lswx_le_raw,
1320
};
1321
static GenOpFunc1 *gen_op_stsw[] = {
1322
    &gen_op_stsw_raw,
1323
    &gen_op_stsw_le_raw,
1324
};
1325
#else
1326
static GenOpFunc1 *gen_op_lswi[] = {
1327
    &gen_op_lswi_user,
1328
    &gen_op_lswi_le_user,
1329
    &gen_op_lswi_kernel,
1330
    &gen_op_lswi_le_kernel,
1331
};
1332
static GenOpFunc3 *gen_op_lswx[] = {
1333
    &gen_op_lswx_user,
1334
    &gen_op_lswx_le_user,
1335
    &gen_op_lswx_kernel,
1336
    &gen_op_lswx_le_kernel,
1337
};
1338
static GenOpFunc1 *gen_op_stsw[] = {
1339
    &gen_op_stsw_user,
1340
    &gen_op_stsw_le_user,
1341
    &gen_op_stsw_kernel,
1342
    &gen_op_stsw_le_kernel,
1343
};
1344
#endif
1345

    
1346
/* lswi */
1347
/* PPC32 specification says we must generate an exception if
1348
 * rA is in the range of registers to be loaded.
1349
 * In an other hand, IBM says this is valid, but rA won't be loaded.
1350
 * For now, I'll follow the spec...
1351
 */
1352
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
1353
{
1354
    int nb = NB(ctx->opcode);
1355
    int start = rD(ctx->opcode);
1356
    int ra = rA(ctx->opcode);
1357
    int nr;
1358

    
1359
    if (nb == 0)
1360
        nb = 32;
1361
    nr = nb / 4;
1362
    if (((start + nr) > 32  && start <= ra && (start + nr - 32) > ra) ||
1363
        ((start + nr) <= 32 && start <= ra && (start + nr) > ra)) {
1364
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_LSWX);
1365
        return;
1366
    }
1367
    if (ra == 0) {
1368
        gen_op_set_T0(0);
1369
    } else {
1370
        gen_op_load_gpr_T0(ra);
1371
    }
1372
    gen_op_set_T1(nb);
1373
    /* NIP cannot be restored if the memory exception comes from an helper */
1374
    gen_op_update_nip((ctx)->nip - 4); 
1375
    op_ldsts(lswi, start);
1376
}
1377

    
1378
/* lswx */
1379
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
1380
{
1381
    int ra = rA(ctx->opcode);
1382
    int rb = rB(ctx->opcode);
1383

    
1384
    if (ra == 0) {
1385
        gen_op_load_gpr_T0(rb);
1386
        ra = rb;
1387
    } else {
1388
        gen_op_load_gpr_T0(ra);
1389
        gen_op_load_gpr_T1(rb);
1390
        gen_op_add();
1391
    }
1392
    gen_op_load_xer_bc();
1393
    /* NIP cannot be restored if the memory exception comes from an helper */
1394
    gen_op_update_nip((ctx)->nip - 4); 
1395
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
1396
}
1397

    
1398
/* stswi */
1399
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
1400
{
1401
    int nb = NB(ctx->opcode);
1402

    
1403
    if (rA(ctx->opcode) == 0) {
1404
        gen_op_set_T0(0);
1405
    } else {
1406
        gen_op_load_gpr_T0(rA(ctx->opcode));
1407
    }
1408
    if (nb == 0)
1409
        nb = 32;
1410
    gen_op_set_T1(nb);
1411
    /* NIP cannot be restored if the memory exception comes from an helper */
1412
    gen_op_update_nip((ctx)->nip - 4); 
1413
    op_ldsts(stsw, rS(ctx->opcode));
1414
}
1415

    
1416
/* stswx */
1417
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
1418
{
1419
    int ra = rA(ctx->opcode);
1420

    
1421
    if (ra == 0) {
1422
        gen_op_load_gpr_T0(rB(ctx->opcode));
1423
        ra = rB(ctx->opcode);
1424
    } else {
1425
        gen_op_load_gpr_T0(ra);
1426
        gen_op_load_gpr_T1(rB(ctx->opcode));
1427
        gen_op_add();
1428
    }
1429
    gen_op_load_xer_bc();
1430
    /* NIP cannot be restored if the memory exception comes from an helper */
1431
    gen_op_update_nip((ctx)->nip - 4); 
1432
    op_ldsts(stsw, rS(ctx->opcode));
1433
}
1434

    
1435
/***                        Memory synchronisation                         ***/
1436
/* eieio */
1437
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FF0801, PPC_MEM)
1438
{
1439
}
1440

    
1441
/* isync */
1442
GEN_HANDLER(isync, 0x13, 0x16, 0xFF, 0x03FF0801, PPC_MEM)
1443
{
1444
}
1445

    
1446
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
1447
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
1448
#if defined(CONFIG_USER_ONLY)
1449
static GenOpFunc *gen_op_lwarx[] = {
1450
    &gen_op_lwarx_raw,
1451
    &gen_op_lwarx_le_raw,
1452
};
1453
static GenOpFunc *gen_op_stwcx[] = {
1454
    &gen_op_stwcx_raw,
1455
    &gen_op_stwcx_le_raw,
1456
};
1457
#else
1458
static GenOpFunc *gen_op_lwarx[] = {
1459
    &gen_op_lwarx_user,
1460
    &gen_op_lwarx_le_user,
1461
    &gen_op_lwarx_kernel,
1462
    &gen_op_lwarx_le_kernel,
1463
};
1464
static GenOpFunc *gen_op_stwcx[] = {
1465
    &gen_op_stwcx_user,
1466
    &gen_op_stwcx_le_user,
1467
    &gen_op_stwcx_kernel,
1468
    &gen_op_stwcx_le_kernel,
1469
};
1470
#endif
1471

    
1472
/* lwarx */
1473
GEN_HANDLER(lwarx, 0x1F, 0x14, 0xFF, 0x00000001, PPC_RES)
1474
{
1475
    if (rA(ctx->opcode) == 0) {
1476
        gen_op_load_gpr_T0(rB(ctx->opcode));
1477
    } else {
1478
        gen_op_load_gpr_T0(rA(ctx->opcode));
1479
        gen_op_load_gpr_T1(rB(ctx->opcode));
1480
        gen_op_add();
1481
    }
1482
    op_lwarx();
1483
    gen_op_store_T1_gpr(rD(ctx->opcode));
1484
}
1485

    
1486
/* stwcx. */
1487
GEN_HANDLER(stwcx_, 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
1488
{
1489
        if (rA(ctx->opcode) == 0) {
1490
            gen_op_load_gpr_T0(rB(ctx->opcode));
1491
        } else {
1492
            gen_op_load_gpr_T0(rA(ctx->opcode));
1493
            gen_op_load_gpr_T1(rB(ctx->opcode));
1494
        gen_op_add();
1495
        }
1496
    gen_op_load_gpr_T1(rS(ctx->opcode));
1497
    op_stwcx();
1498
}
1499

    
1500
/* sync */
1501
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x03FF0801, PPC_MEM)
1502
{
1503
}
1504

    
1505
/***                         Floating-point load                           ***/
1506
#define GEN_LDF(width, opc)                                                   \
1507
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)                 \
1508
{                                                                             \
1509
    uint32_t simm = SIMM(ctx->opcode);                                        \
1510
    if (!ctx->fpu_enabled) {                                                  \
1511
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1512
        return;                                                               \
1513
    }                                                                         \
1514
    if (rA(ctx->opcode) == 0) {                                               \
1515
        gen_op_set_T0(simm);                                                  \
1516
    } else {                                                                  \
1517
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1518
        if (simm != 0)                                                        \
1519
            gen_op_addi(simm);                                                \
1520
    }                                                                         \
1521
    op_ldst(l##width);                                                        \
1522
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1523
}
1524

    
1525
#define GEN_LDUF(width, opc)                                                  \
1526
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)              \
1527
{                                                                             \
1528
    uint32_t simm = SIMM(ctx->opcode);                                        \
1529
    if (!ctx->fpu_enabled) {                                                  \
1530
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1531
        return;                                                               \
1532
    }                                                                         \
1533
    if (rA(ctx->opcode) == 0 ||                                               \
1534
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1535
        RET_INVAL(ctx);                                                       \
1536
        return;                                                               \
1537
    }                                                                         \
1538
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1539
    if (simm != 0)                                                            \
1540
        gen_op_addi(simm);                                                    \
1541
    op_ldst(l##width);                                                        \
1542
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1543
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1544
}
1545

    
1546
#define GEN_LDUXF(width, opc)                                                 \
1547
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_FLOAT)             \
1548
{                                                                             \
1549
    if (!ctx->fpu_enabled) {                                                  \
1550
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1551
        return;                                                               \
1552
    }                                                                         \
1553
    if (rA(ctx->opcode) == 0 ||                                               \
1554
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1555
        RET_INVAL(ctx);                                                       \
1556
        return;                                                               \
1557
    }                                                                         \
1558
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1559
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1560
    gen_op_add();                                                             \
1561
    op_ldst(l##width);                                                        \
1562
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1563
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1564
}
1565

    
1566
#define GEN_LDXF(width, opc2, opc3)                                           \
1567
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_FLOAT)             \
1568
{                                                                             \
1569
    if (!ctx->fpu_enabled) {                                                  \
1570
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1571
        return;                                                               \
1572
    }                                                                         \
1573
    if (rA(ctx->opcode) == 0) {                                               \
1574
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1575
    } else {                                                                  \
1576
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1577
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1578
        gen_op_add();                                                         \
1579
    }                                                                         \
1580
    op_ldst(l##width);                                                        \
1581
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1582
}
1583

    
1584
#define GEN_LDFS(width, op)                                                   \
1585
OP_LD_TABLE(width);                                                           \
1586
GEN_LDF(width, op | 0x20);                                                    \
1587
GEN_LDUF(width, op | 0x21);                                                   \
1588
GEN_LDUXF(width, op | 0x01);                                                  \
1589
GEN_LDXF(width, 0x17, op | 0x00)
1590

    
1591
/* lfd lfdu lfdux lfdx */
1592
GEN_LDFS(fd, 0x12);
1593
/* lfs lfsu lfsux lfsx */
1594
GEN_LDFS(fs, 0x10);
1595

    
1596
/***                         Floating-point store                          ***/
1597
#define GEN_STF(width, opc)                                                   \
1598
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)                \
1599
{                                                                             \
1600
    uint32_t simm = SIMM(ctx->opcode);                                        \
1601
    if (!ctx->fpu_enabled) {                                                  \
1602
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1603
        return;                                                               \
1604
    }                                                                         \
1605
    if (rA(ctx->opcode) == 0) {                                               \
1606
        gen_op_set_T0(simm);                                                  \
1607
    } else {                                                                  \
1608
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1609
        if (simm != 0)                                                        \
1610
            gen_op_addi(simm);                                                \
1611
    }                                                                         \
1612
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1613
    op_ldst(st##width);                                                       \
1614
}
1615

    
1616
#define GEN_STUF(width, opc)                                                  \
1617
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_FLOAT)             \
1618
{                                                                             \
1619
    uint32_t simm = SIMM(ctx->opcode);                                        \
1620
    if (!ctx->fpu_enabled) {                                                  \
1621
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1622
        return;                                                               \
1623
    }                                                                         \
1624
    if (rA(ctx->opcode) == 0) {                                               \
1625
        RET_INVAL(ctx);                                                       \
1626
        return;                                                               \
1627
    }                                                                         \
1628
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1629
    if (simm != 0)                                                            \
1630
        gen_op_addi(simm);                                                    \
1631
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1632
    op_ldst(st##width);                                                       \
1633
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1634
}
1635

    
1636
#define GEN_STUXF(width, opc)                                                 \
1637
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_FLOAT)            \
1638
{                                                                             \
1639
    if (!ctx->fpu_enabled) {                                                  \
1640
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1641
        return;                                                               \
1642
    }                                                                         \
1643
    if (rA(ctx->opcode) == 0) {                                               \
1644
        RET_INVAL(ctx);                                                       \
1645
        return;                                                               \
1646
    }                                                                         \
1647
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1648
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1649
    gen_op_add();                                                             \
1650
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1651
    op_ldst(st##width);                                                       \
1652
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1653
}
1654

    
1655
#define GEN_STXF(width, opc2, opc3)                                           \
1656
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_FLOAT)            \
1657
{                                                                             \
1658
    if (!ctx->fpu_enabled) {                                                  \
1659
        RET_EXCP(ctx, EXCP_NO_FP, 0);                                         \
1660
        return;                                                               \
1661
    }                                                                         \
1662
    if (rA(ctx->opcode) == 0) {                                               \
1663
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1664
    } else {                                                                  \
1665
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1666
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1667
        gen_op_add();                                                         \
1668
    }                                                                         \
1669
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1670
    op_ldst(st##width);                                                       \
1671
}
1672

    
1673
#define GEN_STFS(width, op)                                                   \
1674
OP_ST_TABLE(width);                                                           \
1675
GEN_STF(width, op | 0x20);                                                    \
1676
GEN_STUF(width, op | 0x21);                                                   \
1677
GEN_STUXF(width, op | 0x01);                                                  \
1678
GEN_STXF(width, 0x17, op | 0x00)
1679

    
1680
/* stfd stfdu stfdux stfdx */
1681
GEN_STFS(fd, 0x16);
1682
/* stfs stfsu stfsux stfsx */
1683
GEN_STFS(fs, 0x14);
1684

    
1685
/* Optional: */
1686
/* stfiwx */
1687
GEN_HANDLER(stfiwx, 0x1F, 0x17, 0x1E, 0x00000001, PPC_FLOAT)
1688
{
1689
    if (!ctx->fpu_enabled) {
1690
        RET_EXCP(ctx, EXCP_NO_FP, 0);
1691
        return;
1692
    }
1693
    RET_INVAL(ctx);
1694
}
1695

    
1696
/***                                Branch                                 ***/
1697

    
1698
/* b ba bl bla */
1699
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1700
{
1701
    uint32_t li, target;
1702

    
1703
    /* sign extend LI */
1704
    li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
1705

    
1706
    if (AA(ctx->opcode) == 0)
1707
        target = ctx->nip + li - 4;
1708
    else
1709
        target = li;
1710
    if (LK(ctx->opcode)) {
1711
        gen_op_setlr(ctx->nip);
1712
    }
1713
    gen_op_b((long)ctx->tb, target);
1714
    ctx->exception = EXCP_BRANCH;
1715
}
1716

    
1717
#define BCOND_IM  0
1718
#define BCOND_LR  1
1719
#define BCOND_CTR 2
1720

    
1721
static inline void gen_bcond(DisasContext *ctx, int type) 
1722
{                                                                             
1723
    uint32_t target = 0;
1724
    uint32_t bo = BO(ctx->opcode);                                            
1725
    uint32_t bi = BI(ctx->opcode);                                            
1726
    uint32_t mask;                                                            
1727
    uint32_t li;
1728

    
1729
    if ((bo & 0x4) == 0)
1730
        gen_op_dec_ctr();                                                     
1731
    switch(type) {
1732
    case BCOND_IM:
1733
        li = (int32_t)((int16_t)(BD(ctx->opcode)));
1734
        if (AA(ctx->opcode) == 0) {
1735
            target = ctx->nip + li - 4;
1736
        } else {
1737
            target = li;
1738
        }
1739
        break;
1740
    case BCOND_CTR:
1741
        gen_op_movl_T1_ctr();
1742
        break;
1743
    default:
1744
    case BCOND_LR:
1745
        gen_op_movl_T1_lr();
1746
        break;
1747
    }
1748
    if (LK(ctx->opcode)) {                                        
1749
        gen_op_setlr(ctx->nip);
1750
    }
1751
    if (bo & 0x10) {
1752
        /* No CR condition */                                                 
1753
        switch (bo & 0x6) {                                                   
1754
        case 0:                                                               
1755
            gen_op_test_ctr();
1756
            break;
1757
        case 2:                                                               
1758
            gen_op_test_ctrz();
1759
            break;                                                            
1760
        default:
1761
        case 4:                                                               
1762
        case 6:                                                               
1763
            if (type == BCOND_IM) {
1764
                gen_op_b((long)ctx->tb, target);
1765
            } else {
1766
                gen_op_b_T1();
1767
            }
1768
            goto no_test;
1769
        }
1770
    } else {                                                                  
1771
        mask = 1 << (3 - (bi & 0x03));                                        
1772
        gen_op_load_crf_T0(bi >> 2);                                          
1773
        if (bo & 0x8) {                                                       
1774
            switch (bo & 0x6) {                                               
1775
            case 0:                                                           
1776
                gen_op_test_ctr_true(mask);
1777
                break;                                                        
1778
            case 2:                                                           
1779
                gen_op_test_ctrz_true(mask);
1780
                break;                                                        
1781
            default:                                                          
1782
            case 4:                                                           
1783
            case 6:                                                           
1784
                gen_op_test_true(mask);
1785
                break;                                                        
1786
            }                                                                 
1787
        } else {                                                              
1788
            switch (bo & 0x6) {                                               
1789
            case 0:                                                           
1790
                gen_op_test_ctr_false(mask);
1791
                break;                                                        
1792
            case 2:                                                           
1793
                gen_op_test_ctrz_false(mask);
1794
                break;                                                        
1795
            default:
1796
            case 4:                                                           
1797
            case 6:                                                           
1798
                gen_op_test_false(mask);
1799
                break;                                                        
1800
            }                                                                 
1801
        }                                                                     
1802
    }                                                                         
1803
    if (type == BCOND_IM) {
1804
        gen_op_btest((long)ctx->tb, target, ctx->nip);
1805
    } else {
1806
        gen_op_btest_T1(ctx->nip);
1807
    }
1808
 no_test:
1809
    ctx->exception = EXCP_BRANCH;                                             
1810
}
1811

    
1812
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1813
{                                                                             
1814
    gen_bcond(ctx, BCOND_IM);
1815
}
1816

    
1817
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
1818
{                                                                             
1819
    gen_bcond(ctx, BCOND_CTR);
1820
}
1821

    
1822
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
1823
{                                                                             
1824
    gen_bcond(ctx, BCOND_LR);
1825
}
1826

    
1827
/***                      Condition register logical                       ***/
1828
#define GEN_CRLOGIC(op, opc)                                                  \
1829
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
1830
{                                                                             \
1831
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
1832
    gen_op_getbit_T0(3 - (crbA(ctx->opcode) & 0x03));                         \
1833
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
1834
    gen_op_getbit_T1(3 - (crbB(ctx->opcode) & 0x03));                         \
1835
    gen_op_##op();                                                            \
1836
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
1837
    gen_op_setcrfbit(~(1 << (3 - (crbD(ctx->opcode) & 0x03))),                \
1838
                     3 - (crbD(ctx->opcode) & 0x03));                         \
1839
    gen_op_store_T1_crf(crbD(ctx->opcode) >> 2);                              \
1840
}
1841

    
1842
/* crand */
1843
GEN_CRLOGIC(and, 0x08)
1844
/* crandc */
1845
GEN_CRLOGIC(andc, 0x04)
1846
/* creqv */
1847
GEN_CRLOGIC(eqv, 0x09)
1848
/* crnand */
1849
GEN_CRLOGIC(nand, 0x07)
1850
/* crnor */
1851
GEN_CRLOGIC(nor, 0x01)
1852
/* cror */
1853
GEN_CRLOGIC(or, 0x0E)
1854
/* crorc */
1855
GEN_CRLOGIC(orc, 0x0D)
1856
/* crxor */
1857
GEN_CRLOGIC(xor, 0x06)
1858
/* mcrf */
1859
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
1860
{
1861
    gen_op_load_crf_T0(crfS(ctx->opcode));
1862
    gen_op_store_T0_crf(crfD(ctx->opcode));
1863
}
1864

    
1865
/***                           System linkage                              ***/
1866
/* rfi (supervisor only) */
1867
GEN_HANDLER(rfi, 0x13, 0x12, 0xFF, 0x03FF8001, PPC_FLOW)
1868
{
1869
#if defined(CONFIG_USER_ONLY)
1870
    RET_PRIVOPC(ctx);
1871
#else
1872
    /* Restore CPU state */
1873
    if (!ctx->supervisor) {
1874
        RET_PRIVOPC(ctx);
1875
        return;
1876
    }
1877
    gen_op_rfi();
1878
    RET_EXCP(ctx, EXCP_RFI, 0);
1879
#endif
1880
}
1881

    
1882
/* sc */
1883
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFFFFD, PPC_FLOW)
1884
{
1885
#if defined(CONFIG_USER_ONLY)
1886
    RET_EXCP(ctx, EXCP_SYSCALL_USER, 0);
1887
#else
1888
    RET_EXCP(ctx, EXCP_SYSCALL, 0);
1889
#endif
1890
}
1891

    
1892
/***                                Trap                                   ***/
1893
/* tw */
1894
GEN_HANDLER(tw, 0x1F, 0x04, 0xFF, 0x00000001, PPC_FLOW)
1895
{
1896
    gen_op_load_gpr_T0(rA(ctx->opcode));
1897
    gen_op_load_gpr_T1(rB(ctx->opcode));
1898
    gen_op_tw(TO(ctx->opcode));
1899
}
1900

    
1901
/* twi */
1902
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1903
{
1904
    gen_op_load_gpr_T0(rA(ctx->opcode));
1905
#if 0
1906
    printf("%s: param=0x%04x T0=0x%04x\n", __func__,
1907
           SIMM(ctx->opcode), TO(ctx->opcode));
1908
#endif
1909
    gen_op_twi(SIMM(ctx->opcode), TO(ctx->opcode));
1910
}
1911

    
1912
/***                          Processor control                            ***/
1913
static inline int check_spr_access (int spr, int rw, int supervisor)
1914
{
1915
    uint32_t rights = spr_access[spr >> 1] >> (4 * (spr & 1));
1916

    
1917
#if 0
1918
    if (spr != LR && spr != CTR) {
1919
    if (loglevel > 0) {
1920
        fprintf(logfile, "%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1921
                SPR_ENCODE(spr), supervisor, rw, rights,
1922
                (rights >> ((2 * supervisor) + rw)) & 1);
1923
    } else {
1924
        printf("%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1925
               SPR_ENCODE(spr), supervisor, rw, rights,
1926
               (rights >> ((2 * supervisor) + rw)) & 1);
1927
    }
1928
    }
1929
#endif
1930
    if (rights == 0)
1931
        return -1;
1932
    rights = rights >> (2 * supervisor);
1933
    rights = rights >> rw;
1934

    
1935
    return rights & 1;
1936
}
1937

    
1938
/* mcrxr */
1939
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
1940
{
1941
    gen_op_load_xer_cr();
1942
    gen_op_store_T0_crf(crfD(ctx->opcode));
1943
    gen_op_clear_xer_cr();
1944
}
1945

    
1946
/* mfcr */
1947
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x001FF801, PPC_MISC)
1948
{
1949
    gen_op_load_cr();
1950
    gen_op_store_T0_gpr(rD(ctx->opcode));
1951
}
1952

    
1953
/* mfmsr */
1954
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
1955
{
1956
#if defined(CONFIG_USER_ONLY)
1957
    RET_PRIVREG(ctx);
1958
#else
1959
    if (!ctx->supervisor) {
1960
        RET_PRIVREG(ctx);
1961
        return;
1962
    }
1963
    gen_op_load_msr();
1964
    gen_op_store_T0_gpr(rD(ctx->opcode));
1965
#endif
1966
}
1967

    
1968
/* mfspr */
1969
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
1970
{
1971
    uint32_t sprn = SPR(ctx->opcode);
1972

    
1973
#if defined(CONFIG_USER_ONLY)
1974
    switch (check_spr_access(sprn, 0, 0))
1975
#else
1976
    switch (check_spr_access(sprn, 0, ctx->supervisor))
1977
#endif
1978
    {
1979
    case -1:
1980
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
1981
        return;
1982
    case 0:
1983
        RET_PRIVREG(ctx);
1984
        return;
1985
    default:
1986
        break;
1987
        }
1988
    switch (sprn) {
1989
    case XER:
1990
        gen_op_load_xer();
1991
        break;
1992
    case LR:
1993
        gen_op_load_lr();
1994
        break;
1995
    case CTR:
1996
        gen_op_load_ctr();
1997
        break;
1998
    case IBAT0U:
1999
        gen_op_load_ibat(0, 0);
2000
        break;
2001
    case IBAT1U:
2002
        gen_op_load_ibat(0, 1);
2003
        break;
2004
    case IBAT2U:
2005
        gen_op_load_ibat(0, 2);
2006
        break;
2007
    case IBAT3U:
2008
        gen_op_load_ibat(0, 3);
2009
        break;
2010
    case IBAT4U:
2011
        gen_op_load_ibat(0, 4);
2012
        break;
2013
    case IBAT5U:
2014
        gen_op_load_ibat(0, 5);
2015
        break;
2016
    case IBAT6U:
2017
        gen_op_load_ibat(0, 6);
2018
        break;
2019
    case IBAT7U:
2020
        gen_op_load_ibat(0, 7);
2021
        break;
2022
    case IBAT0L:
2023
        gen_op_load_ibat(1, 0);
2024
        break;
2025
    case IBAT1L:
2026
        gen_op_load_ibat(1, 1);
2027
        break;
2028
    case IBAT2L:
2029
        gen_op_load_ibat(1, 2);
2030
        break;
2031
    case IBAT3L:
2032
        gen_op_load_ibat(1, 3);
2033
        break;
2034
    case IBAT4L:
2035
        gen_op_load_ibat(1, 4);
2036
        break;
2037
    case IBAT5L:
2038
        gen_op_load_ibat(1, 5);
2039
        break;
2040
    case IBAT6L:
2041
        gen_op_load_ibat(1, 6);
2042
        break;
2043
    case IBAT7L:
2044
        gen_op_load_ibat(1, 7);
2045
        break;
2046
    case DBAT0U:
2047
        gen_op_load_dbat(0, 0);
2048
        break;
2049
    case DBAT1U:
2050
        gen_op_load_dbat(0, 1);
2051
        break;
2052
    case DBAT2U:
2053
        gen_op_load_dbat(0, 2);
2054
        break;
2055
    case DBAT3U:
2056
        gen_op_load_dbat(0, 3);
2057
        break;
2058
    case DBAT4U:
2059
        gen_op_load_dbat(0, 4);
2060
        break;
2061
    case DBAT5U:
2062
        gen_op_load_dbat(0, 5);
2063
        break;
2064
    case DBAT6U:
2065
        gen_op_load_dbat(0, 6);
2066
        break;
2067
    case DBAT7U:
2068
        gen_op_load_dbat(0, 7);
2069
        break;
2070
    case DBAT0L:
2071
        gen_op_load_dbat(1, 0);
2072
        break;
2073
    case DBAT1L:
2074
        gen_op_load_dbat(1, 1);
2075
        break;
2076
    case DBAT2L:
2077
        gen_op_load_dbat(1, 2);
2078
        break;
2079
    case DBAT3L:
2080
        gen_op_load_dbat(1, 3);
2081
        break;
2082
    case DBAT4L:
2083
        gen_op_load_dbat(1, 4);
2084
        break;
2085
    case DBAT5L:
2086
        gen_op_load_dbat(1, 5);
2087
        break;
2088
    case DBAT6L:
2089
        gen_op_load_dbat(1, 6);
2090
        break;
2091
    case DBAT7L:
2092
        gen_op_load_dbat(1, 7);
2093
        break;
2094
    case SDR1:
2095
        gen_op_load_sdr1();
2096
        break;
2097
    case V_TBL:
2098
        gen_op_load_tbl();
2099
        break;
2100
    case V_TBU:
2101
        gen_op_load_tbu();
2102
        break;
2103
    case DECR:
2104
        gen_op_load_decr();
2105
        break;
2106
    default:
2107
        gen_op_load_spr(sprn);
2108
        break;
2109
    }
2110
    gen_op_store_T0_gpr(rD(ctx->opcode));
2111
}
2112

    
2113
/* mftb */
2114
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MISC)
2115
{
2116
    uint32_t sprn = SPR(ctx->opcode);
2117

    
2118
        /* We need to update the time base before reading it */
2119
    switch (sprn) {
2120
    case V_TBL:
2121
        gen_op_load_tbl();
2122
        break;
2123
    case V_TBU:
2124
        gen_op_load_tbu();
2125
        break;
2126
    default:
2127
        RET_INVAL(ctx);
2128
        return;
2129
    }
2130
    gen_op_store_T0_gpr(rD(ctx->opcode));
2131
}
2132

    
2133
/* mtcrf */
2134
/* The mask should be 0x00100801, but Mac OS X 10.4 use an alternate form */
2135
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC)
2136
{
2137
    gen_op_load_gpr_T0(rS(ctx->opcode));
2138
    gen_op_store_cr(CRM(ctx->opcode));
2139
}
2140

    
2141
/* mtmsr */
2142
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
2143
{
2144
#if defined(CONFIG_USER_ONLY)
2145
    RET_PRIVREG(ctx);
2146
#else
2147
    if (!ctx->supervisor) {
2148
        RET_PRIVREG(ctx);
2149
        return;
2150
    }
2151
    gen_op_load_gpr_T0(rS(ctx->opcode));
2152
    gen_op_store_msr();
2153
    /* Must stop the translation as machine state (may have) changed */
2154
    RET_MTMSR(ctx);
2155
#endif
2156
}
2157

    
2158
/* mtspr */
2159
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
2160
{
2161
    uint32_t sprn = SPR(ctx->opcode);
2162

    
2163
#if 0
2164
    if (loglevel > 0) {
2165
        fprintf(logfile, "MTSPR %d src=%d (%d)\n", SPR_ENCODE(sprn),
2166
                rS(ctx->opcode), sprn);
2167
    }
2168
#endif
2169
#if defined(CONFIG_USER_ONLY)
2170
    switch (check_spr_access(sprn, 1, 0))
2171
#else
2172
    switch (check_spr_access(sprn, 1, ctx->supervisor))
2173
#endif
2174
    {
2175
    case -1:
2176
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
2177
        break;
2178
    case 0:
2179
        RET_PRIVREG(ctx);
2180
        break;
2181
    default:
2182
        break;
2183
    }
2184
    gen_op_load_gpr_T0(rS(ctx->opcode));
2185
    switch (sprn) {
2186
    case XER:
2187
        gen_op_store_xer();
2188
        break;
2189
    case LR:
2190
        gen_op_store_lr();
2191
        break;
2192
    case CTR:
2193
        gen_op_store_ctr();
2194
        break;
2195
    case IBAT0U:
2196
        gen_op_store_ibat(0, 0);
2197
        RET_MTMSR(ctx);
2198
        break;
2199
    case IBAT1U:
2200
        gen_op_store_ibat(0, 1);
2201
        RET_MTMSR(ctx);
2202
        break;
2203
    case IBAT2U:
2204
        gen_op_store_ibat(0, 2);
2205
        RET_MTMSR(ctx);
2206
        break;
2207
    case IBAT3U:
2208
        gen_op_store_ibat(0, 3);
2209
        RET_MTMSR(ctx);
2210
        break;
2211
    case IBAT4U:
2212
        gen_op_store_ibat(0, 4);
2213
        RET_MTMSR(ctx);
2214
        break;
2215
    case IBAT5U:
2216
        gen_op_store_ibat(0, 5);
2217
        RET_MTMSR(ctx);
2218
        break;
2219
    case IBAT6U:
2220
        gen_op_store_ibat(0, 6);
2221
        RET_MTMSR(ctx);
2222
        break;
2223
    case IBAT7U:
2224
        gen_op_store_ibat(0, 7);
2225
        RET_MTMSR(ctx);
2226
        break;
2227
    case IBAT0L:
2228
        gen_op_store_ibat(1, 0);
2229
        RET_MTMSR(ctx);
2230
        break;
2231
    case IBAT1L:
2232
        gen_op_store_ibat(1, 1);
2233
        RET_MTMSR(ctx);
2234
        break;
2235
    case IBAT2L:
2236
        gen_op_store_ibat(1, 2);
2237
        RET_MTMSR(ctx);
2238
        break;
2239
    case IBAT3L:
2240
        gen_op_store_ibat(1, 3);
2241
        RET_MTMSR(ctx);
2242
        break;
2243
    case IBAT4L:
2244
        gen_op_store_ibat(1, 4);
2245
        RET_MTMSR(ctx);
2246
        break;
2247
    case IBAT5L:
2248
        gen_op_store_ibat(1, 5);
2249
        RET_MTMSR(ctx);
2250
        break;
2251
    case IBAT6L:
2252
        gen_op_store_ibat(1, 6);
2253
        RET_MTMSR(ctx);
2254
        break;
2255
    case IBAT7L:
2256
        gen_op_store_ibat(1, 7);
2257
        RET_MTMSR(ctx);
2258
        break;
2259
    case DBAT0U:
2260
        gen_op_store_dbat(0, 0);
2261
        RET_MTMSR(ctx);
2262
        break;
2263
    case DBAT1U:
2264
        gen_op_store_dbat(0, 1);
2265
        RET_MTMSR(ctx);
2266
        break;
2267
    case DBAT2U:
2268
        gen_op_store_dbat(0, 2);
2269
        RET_MTMSR(ctx);
2270
        break;
2271
    case DBAT3U:
2272
        gen_op_store_dbat(0, 3);
2273
        RET_MTMSR(ctx);
2274
        break;
2275
    case DBAT4U:
2276
        gen_op_store_dbat(0, 4);
2277
        RET_MTMSR(ctx);
2278
        break;
2279
    case DBAT5U:
2280
        gen_op_store_dbat(0, 5);
2281
        RET_MTMSR(ctx);
2282
        break;
2283
    case DBAT6U:
2284
        gen_op_store_dbat(0, 6);
2285
        RET_MTMSR(ctx);
2286
        break;
2287
    case DBAT7U:
2288
        gen_op_store_dbat(0, 7);
2289
        RET_MTMSR(ctx);
2290
        break;
2291
    case DBAT0L:
2292
        gen_op_store_dbat(1, 0);
2293
        RET_MTMSR(ctx);
2294
        break;
2295
    case DBAT1L:
2296
        gen_op_store_dbat(1, 1);
2297
        RET_MTMSR(ctx);
2298
        break;
2299
    case DBAT2L:
2300
        gen_op_store_dbat(1, 2);
2301
        RET_MTMSR(ctx);
2302
        break;
2303
    case DBAT3L:
2304
        gen_op_store_dbat(1, 3);
2305
        RET_MTMSR(ctx);
2306
        break;
2307
    case DBAT4L:
2308
        gen_op_store_dbat(1, 4);
2309
        RET_MTMSR(ctx);
2310
        break;
2311
    case DBAT5L:
2312
        gen_op_store_dbat(1, 5);
2313
        RET_MTMSR(ctx);
2314
        break;
2315
    case DBAT6L:
2316
        gen_op_store_dbat(1, 6);
2317
        RET_MTMSR(ctx);
2318
        break;
2319
    case DBAT7L:
2320
        gen_op_store_dbat(1, 7);
2321
        RET_MTMSR(ctx);
2322
        break;
2323
    case SDR1:
2324
        gen_op_store_sdr1();
2325
        RET_MTMSR(ctx);
2326
        break;
2327
    case O_TBL:
2328
        gen_op_store_tbl();
2329
        break;
2330
    case O_TBU:
2331
        gen_op_store_tbu();
2332
        break;
2333
    case DECR:
2334
        gen_op_store_decr();
2335
        break;
2336
    default:
2337
        gen_op_store_spr(sprn);
2338
        break;
2339
    }
2340
}
2341

    
2342
/***                         Cache management                              ***/
2343
/* For now, all those will be implemented as nop:
2344
 * this is valid, regarding the PowerPC specs...
2345
 * We just have to flush tb while invalidating instruction cache lines...
2346
 */
2347
/* dcbf */
2348
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03E00001, PPC_CACHE)
2349
{
2350
    if (rA(ctx->opcode) == 0) {
2351
        gen_op_load_gpr_T0(rB(ctx->opcode));
2352
    } else {
2353
        gen_op_load_gpr_T0(rA(ctx->opcode));
2354
        gen_op_load_gpr_T1(rB(ctx->opcode));
2355
        gen_op_add();
2356
    }
2357
    op_ldst(lbz);
2358
}
2359

    
2360
/* dcbi (Supervisor only) */
2361
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
2362
{
2363
#if defined(CONFIG_USER_ONLY)
2364
    RET_PRIVOPC(ctx);
2365
#else
2366
    if (!ctx->supervisor) {
2367
        RET_PRIVOPC(ctx);
2368
        return;
2369
    }
2370
    if (rA(ctx->opcode) == 0) {
2371
        gen_op_load_gpr_T0(rB(ctx->opcode));
2372
    } else {
2373
        gen_op_load_gpr_T0(rA(ctx->opcode));
2374
        gen_op_load_gpr_T1(rB(ctx->opcode));
2375
        gen_op_add();
2376
    }
2377
    op_ldst(lbz);
2378
    op_ldst(stb);
2379
#endif
2380
}
2381

    
2382
/* dcdst */
2383
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
2384
{
2385
    if (rA(ctx->opcode) == 0) {
2386
        gen_op_load_gpr_T0(rB(ctx->opcode));
2387
    } else {
2388
        gen_op_load_gpr_T0(rA(ctx->opcode));
2389
        gen_op_load_gpr_T1(rB(ctx->opcode));
2390
        gen_op_add();
2391
    }
2392
    op_ldst(lbz);
2393
}
2394

    
2395
/* dcbt */
2396
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x03E00001, PPC_CACHE)
2397
{
2398
}
2399

    
2400
/* dcbtst */
2401
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE)
2402
{
2403
}
2404

    
2405
/* dcbz */
2406
#if defined(CONFIG_USER_ONLY)
2407
#define op_dcbz() gen_op_dcbz_raw()
2408
#else
2409
#define op_dcbz() (*gen_op_dcbz[ctx->mem_idx])()
2410
static GenOpFunc *gen_op_dcbz[] = {
2411
    &gen_op_dcbz_user,
2412
    &gen_op_dcbz_user,
2413
    &gen_op_dcbz_kernel,
2414
    &gen_op_dcbz_kernel,
2415
};
2416
#endif
2417

    
2418
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE)
2419
{
2420
    if (rA(ctx->opcode) == 0) {
2421
        gen_op_load_gpr_T0(rB(ctx->opcode));
2422
    } else {
2423
        gen_op_load_gpr_T0(rA(ctx->opcode));
2424
        gen_op_load_gpr_T1(rB(ctx->opcode));
2425
        gen_op_add();
2426
    }
2427
    op_dcbz();
2428
    gen_op_check_reservation();
2429
}
2430

    
2431
/* icbi */
2432
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
2433
{
2434
    if (rA(ctx->opcode) == 0) {
2435
        gen_op_load_gpr_T0(rB(ctx->opcode));
2436
    } else {
2437
        gen_op_load_gpr_T0(rA(ctx->opcode));
2438
        gen_op_load_gpr_T1(rB(ctx->opcode));
2439
        gen_op_add();
2440
    }
2441
    gen_op_icbi();
2442
}
2443

    
2444
/* Optional: */
2445
/* dcba */
2446
GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_OPT)
2447
{
2448
}
2449

    
2450
/***                    Segment register manipulation                      ***/
2451
/* Supervisor only: */
2452
/* mfsr */
2453
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
2454
{
2455
#if defined(CONFIG_USER_ONLY)
2456
    RET_PRIVREG(ctx);
2457
#else
2458
    if (!ctx->supervisor) {
2459
        RET_PRIVREG(ctx);
2460
        return;
2461
    }
2462
    gen_op_load_sr(SR(ctx->opcode));
2463
    gen_op_store_T0_gpr(rD(ctx->opcode));
2464
#endif
2465
}
2466

    
2467
/* mfsrin */
2468
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
2469
{
2470
#if defined(CONFIG_USER_ONLY)
2471
    RET_PRIVREG(ctx);
2472
#else
2473
    if (!ctx->supervisor) {
2474
        RET_PRIVREG(ctx);
2475
        return;
2476
    }
2477
    gen_op_load_gpr_T1(rB(ctx->opcode));
2478
    gen_op_load_srin();
2479
    gen_op_store_T0_gpr(rD(ctx->opcode));
2480
#endif
2481
}
2482

    
2483
/* mtsr */
2484
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
2485
{
2486
#if defined(CONFIG_USER_ONLY)
2487
    RET_PRIVREG(ctx);
2488
#else
2489
    if (!ctx->supervisor) {
2490
        RET_PRIVREG(ctx);
2491
        return;
2492
    }
2493
    gen_op_load_gpr_T0(rS(ctx->opcode));
2494
    gen_op_store_sr(SR(ctx->opcode));
2495
#endif
2496
}
2497

    
2498
/* mtsrin */
2499
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
2500
{
2501
#if defined(CONFIG_USER_ONLY)
2502
    RET_PRIVREG(ctx);
2503
#else
2504
    if (!ctx->supervisor) {
2505
        RET_PRIVREG(ctx);
2506
        return;
2507
    }
2508
    gen_op_load_gpr_T0(rS(ctx->opcode));
2509
    gen_op_load_gpr_T1(rB(ctx->opcode));
2510
    gen_op_store_srin();
2511
#endif
2512
}
2513

    
2514
/***                      Lookaside buffer management                      ***/
2515
/* Optional & supervisor only: */
2516
/* tlbia */
2517
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_OPT)
2518
{
2519
#if defined(CONFIG_USER_ONLY)
2520
    RET_PRIVOPC(ctx);
2521
#else
2522
    if (!ctx->supervisor) {
2523
        if (loglevel)
2524
            fprintf(logfile, "%s: ! supervisor\n", __func__);
2525
        RET_PRIVOPC(ctx);
2526
        return;
2527
    }
2528
    gen_op_tlbia();
2529
    RET_MTMSR(ctx);
2530
#endif
2531
}
2532

    
2533
/* tlbie */
2534
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM)
2535
{
2536
#if defined(CONFIG_USER_ONLY)
2537
    RET_PRIVOPC(ctx);
2538
#else
2539
    if (!ctx->supervisor) {
2540
        RET_PRIVOPC(ctx);
2541
        return;
2542
    }
2543
    gen_op_load_gpr_T0(rB(ctx->opcode));
2544
    gen_op_tlbie();
2545
    RET_MTMSR(ctx);
2546
#endif
2547
}
2548

    
2549
/* tlbsync */
2550
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM)
2551
{
2552
#if defined(CONFIG_USER_ONLY)
2553
    RET_PRIVOPC(ctx);
2554
#else
2555
    if (!ctx->supervisor) {
2556
        RET_PRIVOPC(ctx);
2557
        return;
2558
    }
2559
    /* This has no effect: it should ensure that all previous
2560
     * tlbie have completed
2561
     */
2562
    RET_MTMSR(ctx);
2563
#endif
2564
}
2565

    
2566
/***                              External control                         ***/
2567
/* Optional: */
2568
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
2569
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
2570
#if defined(CONFIG_USER_ONLY)
2571
static GenOpFunc *gen_op_eciwx[] = {
2572
    &gen_op_eciwx_raw,
2573
    &gen_op_eciwx_le_raw,
2574
};
2575
static GenOpFunc *gen_op_ecowx[] = {
2576
    &gen_op_ecowx_raw,
2577
    &gen_op_ecowx_le_raw,
2578
};
2579
#else
2580
static GenOpFunc *gen_op_eciwx[] = {
2581
    &gen_op_eciwx_user,
2582
    &gen_op_eciwx_le_user,
2583
    &gen_op_eciwx_kernel,
2584
    &gen_op_eciwx_le_kernel,
2585
};
2586
static GenOpFunc *gen_op_ecowx[] = {
2587
    &gen_op_ecowx_user,
2588
    &gen_op_ecowx_le_user,
2589
    &gen_op_ecowx_kernel,
2590
    &gen_op_ecowx_le_kernel,
2591
};
2592
#endif
2593

    
2594
/* eciwx */
2595
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
2596
{
2597
    /* Should check EAR[E] & alignment ! */
2598
    if (rA(ctx->opcode) == 0) {
2599
        gen_op_load_gpr_T0(rB(ctx->opcode));
2600
    } else {
2601
        gen_op_load_gpr_T0(rA(ctx->opcode));
2602
        gen_op_load_gpr_T1(rB(ctx->opcode));
2603
        gen_op_add();
2604
    }
2605
    op_eciwx();
2606
    gen_op_store_T0_gpr(rD(ctx->opcode));
2607
}
2608

    
2609
/* ecowx */
2610
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
2611
{
2612
    /* Should check EAR[E] & alignment ! */
2613
    if (rA(ctx->opcode) == 0) {
2614
        gen_op_load_gpr_T0(rB(ctx->opcode));
2615
    } else {
2616
        gen_op_load_gpr_T0(rA(ctx->opcode));
2617
        gen_op_load_gpr_T1(rB(ctx->opcode));
2618
        gen_op_add();
2619
    }
2620
    gen_op_load_gpr_T2(rS(ctx->opcode));
2621
    op_ecowx();
2622
}
2623

    
2624
/* End opcode list */
2625
GEN_OPCODE_MARK(end);
2626

    
2627
/*****************************************************************************/
2628
#include <stdlib.h>
2629
#include <string.h>
2630

    
2631
int fflush (FILE *stream);
2632

    
2633
/* Main ppc opcodes table:
2634
 * at init, all opcodes are invalids
2635
 */
2636
static opc_handler_t *ppc_opcodes[0x40];
2637

    
2638
/* Opcode types */
2639
enum {
2640
    PPC_DIRECT   = 0, /* Opcode routine        */
2641
    PPC_INDIRECT = 1, /* Indirect opcode table */
2642
};
2643

    
2644
static inline int is_indirect_opcode (void *handler)
2645
{
2646
    return ((unsigned long)handler & 0x03) == PPC_INDIRECT;
2647
}
2648

    
2649
static inline opc_handler_t **ind_table(void *handler)
2650
{
2651
    return (opc_handler_t **)((unsigned long)handler & ~3);
2652
}
2653

    
2654
/* Instruction table creation */
2655
/* Opcodes tables creation */
2656
static void fill_new_table (opc_handler_t **table, int len)
2657
{
2658
    int i;
2659

    
2660
    for (i = 0; i < len; i++)
2661
        table[i] = &invalid_handler;
2662
}
2663

    
2664
static int create_new_table (opc_handler_t **table, unsigned char idx)
2665
{
2666
    opc_handler_t **tmp;
2667

    
2668
    tmp = malloc(0x20 * sizeof(opc_handler_t));
2669
    if (tmp == NULL)
2670
        return -1;
2671
    fill_new_table(tmp, 0x20);
2672
    table[idx] = (opc_handler_t *)((unsigned long)tmp | PPC_INDIRECT);
2673

    
2674
    return 0;
2675
}
2676

    
2677
static int insert_in_table (opc_handler_t **table, unsigned char idx,
2678
                            opc_handler_t *handler)
2679
{
2680
    if (table[idx] != &invalid_handler)
2681
        return -1;
2682
    table[idx] = handler;
2683

    
2684
    return 0;
2685
}
2686

    
2687
static int register_direct_insn (opc_handler_t **ppc_opcodes,
2688
                                 unsigned char idx, opc_handler_t *handler)
2689
{
2690
    if (insert_in_table(ppc_opcodes, idx, handler) < 0) {
2691
        printf("*** ERROR: opcode %02x already assigned in main "
2692
                "opcode table\n", idx);
2693
        return -1;
2694
    }
2695

    
2696
    return 0;
2697
}
2698

    
2699
static int register_ind_in_table (opc_handler_t **table,
2700
                                  unsigned char idx1, unsigned char idx2,
2701
                                  opc_handler_t *handler)
2702
{
2703
    if (table[idx1] == &invalid_handler) {
2704
        if (create_new_table(table, idx1) < 0) {
2705
            printf("*** ERROR: unable to create indirect table "
2706
                    "idx=%02x\n", idx1);
2707
            return -1;
2708
        }
2709
    } else {
2710
        if (!is_indirect_opcode(table[idx1])) {
2711
            printf("*** ERROR: idx %02x already assigned to a direct "
2712
                    "opcode\n", idx1);
2713
            return -1;
2714
        }
2715
    }
2716
    if (handler != NULL &&
2717
        insert_in_table(ind_table(table[idx1]), idx2, handler) < 0) {
2718
        printf("*** ERROR: opcode %02x already assigned in "
2719
                "opcode table %02x\n", idx2, idx1);
2720
        return -1;
2721
    }
2722

    
2723
    return 0;
2724
}
2725

    
2726
static int register_ind_insn (opc_handler_t **ppc_opcodes,
2727
                              unsigned char idx1, unsigned char idx2,
2728
                               opc_handler_t *handler)
2729
{
2730
    int ret;
2731

    
2732
    ret = register_ind_in_table(ppc_opcodes, idx1, idx2, handler);
2733

    
2734
    return ret;
2735
}
2736

    
2737
static int register_dblind_insn (opc_handler_t **ppc_opcodes, 
2738
                                 unsigned char idx1, unsigned char idx2,
2739
                                  unsigned char idx3, opc_handler_t *handler)
2740
{
2741
    if (register_ind_in_table(ppc_opcodes, idx1, idx2, NULL) < 0) {
2742
        printf("*** ERROR: unable to join indirect table idx "
2743
                "[%02x-%02x]\n", idx1, idx2);
2744
        return -1;
2745
    }
2746
    if (register_ind_in_table(ind_table(ppc_opcodes[idx1]), idx2, idx3,
2747
                              handler) < 0) {
2748
        printf("*** ERROR: unable to insert opcode "
2749
                "[%02x-%02x-%02x]\n", idx1, idx2, idx3);
2750
        return -1;
2751
    }
2752

    
2753
    return 0;
2754
}
2755

    
2756
static int register_insn (opc_handler_t **ppc_opcodes, opcode_t *insn)
2757
{
2758
    if (insn->opc2 != 0xFF) {
2759
        if (insn->opc3 != 0xFF) {
2760
            if (register_dblind_insn(ppc_opcodes, insn->opc1, insn->opc2,
2761
                                     insn->opc3, &insn->handler) < 0)
2762
                return -1;
2763
        } else {
2764
            if (register_ind_insn(ppc_opcodes, insn->opc1,
2765
                                  insn->opc2, &insn->handler) < 0)
2766
                return -1;
2767
        }
2768
    } else {
2769
        if (register_direct_insn(ppc_opcodes, insn->opc1, &insn->handler) < 0)
2770
            return -1;
2771
    }
2772

    
2773
    return 0;
2774
}
2775

    
2776
static int test_opcode_table (opc_handler_t **table, int len)
2777
{
2778
    int i, count, tmp;
2779

    
2780
    for (i = 0, count = 0; i < len; i++) {
2781
        /* Consistency fixup */
2782
        if (table[i] == NULL)
2783
            table[i] = &invalid_handler;
2784
        if (table[i] != &invalid_handler) {
2785
            if (is_indirect_opcode(table[i])) {
2786
                tmp = test_opcode_table(ind_table(table[i]), 0x20);
2787
                if (tmp == 0) {
2788
                    free(table[i]);
2789
                    table[i] = &invalid_handler;
2790
                } else {
2791
                    count++;
2792
                }
2793
            } else {
2794
                count++;
2795
            }
2796
        }
2797
    }
2798

    
2799
    return count;
2800
}
2801

    
2802
static void fix_opcode_tables (opc_handler_t **ppc_opcodes)
2803
{
2804
    if (test_opcode_table(ppc_opcodes, 0x40) == 0)
2805
        printf("*** WARNING: no opcode defined !\n");
2806
}
2807

    
2808
#define SPR_RIGHTS(rw, priv) (1 << ((2 * (priv)) + (rw)))
2809
#define SPR_UR SPR_RIGHTS(0, 0)
2810
#define SPR_UW SPR_RIGHTS(1, 0)
2811
#define SPR_SR SPR_RIGHTS(0, 1)
2812
#define SPR_SW SPR_RIGHTS(1, 1)
2813

    
2814
#define spr_set_rights(spr, rights)                            \
2815
do {                                                           \
2816
    spr_access[(spr) >> 1] |= ((rights) << (4 * ((spr) & 1))); \
2817
} while (0)
2818

    
2819
static void init_spr_rights (uint32_t pvr)
2820
{
2821
    /* XER    (SPR 1) */
2822
    spr_set_rights(XER,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2823
    /* LR     (SPR 8) */
2824
    spr_set_rights(LR,     SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2825
    /* CTR    (SPR 9) */
2826
    spr_set_rights(CTR,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2827
    /* TBL    (SPR 268) */
2828
    spr_set_rights(V_TBL,  SPR_UR | SPR_SR);
2829
    /* TBU    (SPR 269) */
2830
    spr_set_rights(V_TBU,  SPR_UR | SPR_SR);
2831
    /* DSISR  (SPR 18) */
2832
    spr_set_rights(DSISR,  SPR_SR | SPR_SW);
2833
    /* DAR    (SPR 19) */
2834
    spr_set_rights(DAR,    SPR_SR | SPR_SW);
2835
    /* DEC    (SPR 22) */
2836
    spr_set_rights(DECR,   SPR_SR | SPR_SW);
2837
    /* SDR1   (SPR 25) */
2838
    spr_set_rights(SDR1,   SPR_SR | SPR_SW);
2839
    /* SRR0   (SPR 26) */
2840
    spr_set_rights(SRR0,   SPR_SR | SPR_SW);
2841
    /* SRR1   (SPR 27) */
2842
    spr_set_rights(SRR1,   SPR_SR | SPR_SW);
2843
    /* SPRG0  (SPR 272) */
2844
    spr_set_rights(SPRG0,  SPR_SR | SPR_SW);
2845
    /* SPRG1  (SPR 273) */
2846
    spr_set_rights(SPRG1,  SPR_SR | SPR_SW);
2847
    /* SPRG2  (SPR 274) */
2848
    spr_set_rights(SPRG2,  SPR_SR | SPR_SW);
2849
    /* SPRG3  (SPR 275) */
2850
    spr_set_rights(SPRG3,  SPR_SR | SPR_SW);
2851
    /* ASR    (SPR 280) */
2852
    spr_set_rights(ASR,    SPR_SR | SPR_SW);
2853
    /* EAR    (SPR 282) */
2854
    spr_set_rights(EAR,    SPR_SR | SPR_SW);
2855
    /* TBL    (SPR 284) */
2856
    spr_set_rights(O_TBL,  SPR_SW);
2857
    /* TBU    (SPR 285) */
2858
    spr_set_rights(O_TBU,  SPR_SW);
2859
    /* PVR    (SPR 287) */
2860
    spr_set_rights(PVR,    SPR_SR);
2861
    /* IBAT0U (SPR 528) */
2862
    spr_set_rights(IBAT0U, SPR_SR | SPR_SW);
2863
    /* IBAT0L (SPR 529) */
2864
    spr_set_rights(IBAT0L, SPR_SR | SPR_SW);
2865
    /* IBAT1U (SPR 530) */
2866
    spr_set_rights(IBAT1U, SPR_SR | SPR_SW);
2867
    /* IBAT1L (SPR 531) */
2868
    spr_set_rights(IBAT1L, SPR_SR | SPR_SW);
2869
    /* IBAT2U (SPR 532) */
2870
    spr_set_rights(IBAT2U, SPR_SR | SPR_SW);
2871
    /* IBAT2L (SPR 533) */
2872
    spr_set_rights(IBAT2L, SPR_SR | SPR_SW);
2873
    /* IBAT3U (SPR 534) */
2874
    spr_set_rights(IBAT3U, SPR_SR | SPR_SW);
2875
    /* IBAT3L (SPR 535) */
2876
    spr_set_rights(IBAT3L, SPR_SR | SPR_SW);
2877
    /* DBAT0U (SPR 536) */
2878
    spr_set_rights(DBAT0U, SPR_SR | SPR_SW);
2879
    /* DBAT0L (SPR 537) */
2880
    spr_set_rights(DBAT0L, SPR_SR | SPR_SW);
2881
    /* DBAT1U (SPR 538) */
2882
    spr_set_rights(DBAT1U, SPR_SR | SPR_SW);
2883
    /* DBAT1L (SPR 539) */
2884
    spr_set_rights(DBAT1L, SPR_SR | SPR_SW);
2885
    /* DBAT2U (SPR 540) */
2886
    spr_set_rights(DBAT2U, SPR_SR | SPR_SW);
2887
    /* DBAT2L (SPR 541) */
2888
    spr_set_rights(DBAT2L, SPR_SR | SPR_SW);
2889
    /* DBAT3U (SPR 542) */
2890
    spr_set_rights(DBAT3U, SPR_SR | SPR_SW);
2891
    /* DBAT3L (SPR 543) */
2892
    spr_set_rights(DBAT3L, SPR_SR | SPR_SW);
2893
    /* FPECR  (SPR 1022) */
2894
    spr_set_rights(FPECR,  SPR_SR | SPR_SW);
2895
    /* Special registers for PPC 604 */
2896
    if ((pvr & 0xFFFF0000) == 0x00040000) {
2897
        /* IABR */
2898
        spr_set_rights(IABR ,  SPR_SR | SPR_SW);
2899
        /* DABR   (SPR 1013) */
2900
        spr_set_rights(DABR,   SPR_SR | SPR_SW);
2901
        /* HID0 */
2902
        spr_set_rights(HID0,   SPR_SR | SPR_SW);
2903
        /* PIR */
2904
    spr_set_rights(PIR,    SPR_SR | SPR_SW);
2905
        /* PMC1 */
2906
        spr_set_rights(PMC1,   SPR_SR | SPR_SW);
2907
        /* PMC2 */
2908
        spr_set_rights(PMC2,   SPR_SR | SPR_SW);
2909
        /* MMCR0 */
2910
        spr_set_rights(MMCR0,  SPR_SR | SPR_SW);
2911
        /* SIA */
2912
        spr_set_rights(SIA,    SPR_SR | SPR_SW);
2913
        /* SDA */
2914
        spr_set_rights(SDA,    SPR_SR | SPR_SW);
2915
    }
2916
    /* Special registers for MPC740/745/750/755 (aka G3) & IBM 750 */
2917
    if ((pvr & 0xFFFF0000) == 0x00080000 ||
2918
        (pvr & 0xFFFF0000) == 0x70000000) {
2919
        /* HID0 */
2920
        spr_set_rights(HID0,   SPR_SR | SPR_SW);
2921
        /* HID1 */
2922
        spr_set_rights(HID1,   SPR_SR | SPR_SW);
2923
        /* IABR */
2924
        spr_set_rights(IABR,   SPR_SR | SPR_SW);
2925
        /* ICTC */
2926
        spr_set_rights(ICTC,   SPR_SR | SPR_SW);
2927
        /* L2CR */
2928
        spr_set_rights(L2CR,   SPR_SR | SPR_SW);
2929
        /* MMCR0 */
2930
        spr_set_rights(MMCR0,  SPR_SR | SPR_SW);
2931
        /* MMCR1 */
2932
        spr_set_rights(MMCR1,  SPR_SR | SPR_SW);
2933
        /* PMC1 */
2934
        spr_set_rights(PMC1,   SPR_SR | SPR_SW);
2935
        /* PMC2 */
2936
        spr_set_rights(PMC2,   SPR_SR | SPR_SW);
2937
        /* PMC3 */
2938
        spr_set_rights(PMC3,   SPR_SR | SPR_SW);
2939
        /* PMC4 */
2940
        spr_set_rights(PMC4,   SPR_SR | SPR_SW);
2941
        /* SIA */
2942
        spr_set_rights(SIA,    SPR_SR | SPR_SW);
2943
        /* SDA */
2944
        spr_set_rights(SDA,    SPR_SR | SPR_SW);
2945
        /* THRM1 */
2946
        spr_set_rights(THRM1,  SPR_SR | SPR_SW);
2947
        /* THRM2 */
2948
        spr_set_rights(THRM2,  SPR_SR | SPR_SW);
2949
        /* THRM3 */
2950
        spr_set_rights(THRM3,  SPR_SR | SPR_SW);
2951
        /* UMMCR0 */
2952
        spr_set_rights(UMMCR0, SPR_UR | SPR_UW);
2953
        /* UMMCR1 */
2954
        spr_set_rights(UMMCR1, SPR_UR | SPR_UW);
2955
        /* UPMC1 */
2956
        spr_set_rights(UPMC1,  SPR_UR | SPR_UW);
2957
        /* UPMC2 */
2958
        spr_set_rights(UPMC2,  SPR_UR | SPR_UW);
2959
        /* UPMC3 */
2960
        spr_set_rights(UPMC3,  SPR_UR | SPR_UW);
2961
        /* UPMC4 */
2962
        spr_set_rights(UPMC4,  SPR_UR | SPR_UW);
2963
        /* USIA */
2964
        spr_set_rights(USIA,   SPR_UR | SPR_UW);
2965
    }
2966
    /* MPC755 has special registers */
2967
    if (pvr == 0x00083100) {
2968
        /* SPRG4 */
2969
        spr_set_rights(SPRG4, SPR_SR | SPR_SW);
2970
        /* SPRG5 */
2971
        spr_set_rights(SPRG5, SPR_SR | SPR_SW);
2972
        /* SPRG6 */
2973
        spr_set_rights(SPRG6, SPR_SR | SPR_SW);
2974
        /* SPRG7 */
2975
        spr_set_rights(SPRG7, SPR_SR | SPR_SW);
2976
        /* IBAT4U */
2977
        spr_set_rights(IBAT4U, SPR_SR | SPR_SW);
2978
        /* IBAT4L */
2979
        spr_set_rights(IBAT4L, SPR_SR | SPR_SW);
2980
        /* IBAT5U */
2981
        spr_set_rights(IBAT5U, SPR_SR | SPR_SW);
2982
        /* IBAT5L */
2983
        spr_set_rights(IBAT5L, SPR_SR | SPR_SW);
2984
        /* IBAT6U */
2985
        spr_set_rights(IBAT6U, SPR_SR | SPR_SW);
2986
        /* IBAT6L */
2987
        spr_set_rights(IBAT6L, SPR_SR | SPR_SW);
2988
        /* IBAT7U */
2989
        spr_set_rights(IBAT7U, SPR_SR | SPR_SW);
2990
        /* IBAT7L */
2991
        spr_set_rights(IBAT7L, SPR_SR | SPR_SW);
2992
        /* DBAT4U */
2993
        spr_set_rights(DBAT4U, SPR_SR | SPR_SW);
2994
        /* DBAT4L */
2995
        spr_set_rights(DBAT4L, SPR_SR | SPR_SW);
2996
        /* DBAT5U */
2997
        spr_set_rights(DBAT5U, SPR_SR | SPR_SW);
2998
        /* DBAT5L */
2999
        spr_set_rights(DBAT5L, SPR_SR | SPR_SW);
3000
        /* DBAT6U */
3001
        spr_set_rights(DBAT6U, SPR_SR | SPR_SW);
3002
        /* DBAT6L */
3003
        spr_set_rights(DBAT6L, SPR_SR | SPR_SW);
3004
        /* DBAT7U */
3005
        spr_set_rights(DBAT7U, SPR_SR | SPR_SW);
3006
        /* DBAT7L */
3007
        spr_set_rights(DBAT7L, SPR_SR | SPR_SW);
3008
        /* DMISS */
3009
        spr_set_rights(DMISS,  SPR_SR | SPR_SW);
3010
        /* DCMP */
3011
        spr_set_rights(DCMP,   SPR_SR | SPR_SW);
3012
        /* DHASH1 */
3013
        spr_set_rights(DHASH1, SPR_SR | SPR_SW);
3014
        /* DHASH2 */
3015
        spr_set_rights(DHASH2, SPR_SR | SPR_SW);
3016
        /* IMISS */
3017
        spr_set_rights(IMISS,  SPR_SR | SPR_SW);
3018
        /* ICMP */
3019
        spr_set_rights(ICMP,   SPR_SR | SPR_SW);
3020
        /* RPA */
3021
        spr_set_rights(RPA,    SPR_SR | SPR_SW);
3022
        /* HID2 */
3023
        spr_set_rights(HID2,   SPR_SR | SPR_SW);
3024
        /* L2PM */
3025
        spr_set_rights(L2PM,   SPR_SR | SPR_SW);
3026
    }
3027
}
3028

    
3029
/*****************************************************************************/
3030
/* PPC "main stream" common instructions (no optional ones) */
3031

    
3032
typedef struct ppc_proc_t {
3033
    int flags;
3034
    void *specific;
3035
} ppc_proc_t;
3036

    
3037
typedef struct ppc_def_t {
3038
    unsigned long pvr;
3039
    unsigned long pvr_mask;
3040
    ppc_proc_t *proc;
3041
} ppc_def_t;
3042

    
3043
static ppc_proc_t ppc_proc_common = {
3044
    .flags    = PPC_COMMON,
3045
    .specific = NULL,
3046
};
3047

    
3048
static ppc_proc_t ppc_proc_G3 = {
3049
    .flags    = PPC_750,
3050
    .specific = NULL,
3051
};
3052

    
3053
static ppc_def_t ppc_defs[] =
3054
{
3055
    /* MPC740/745/750/755 (G3) */
3056
    {
3057
        .pvr      = 0x00080000,
3058
        .pvr_mask = 0xFFFF0000,
3059
        .proc     = &ppc_proc_G3,
3060
    },
3061
    /* IBM 750FX (G3 embedded) */
3062
    {
3063
        .pvr      = 0x70000000,
3064
        .pvr_mask = 0xFFFF0000,
3065
        .proc     = &ppc_proc_G3,
3066
    },
3067
    /* Fallback (generic PPC) */
3068
    {
3069
        .pvr      = 0x00000000,
3070
        .pvr_mask = 0x00000000,
3071
        .proc     = &ppc_proc_common,
3072
    },
3073
};
3074

    
3075
static int create_ppc_proc (opc_handler_t **ppc_opcodes, unsigned long pvr)
3076
{
3077
    opcode_t *opc, *start, *end;
3078
    int i, flags;
3079

    
3080
    fill_new_table(ppc_opcodes, 0x40);
3081
    for (i = 0; ; i++) {
3082
        if ((ppc_defs[i].pvr & ppc_defs[i].pvr_mask) ==
3083
            (pvr & ppc_defs[i].pvr_mask)) {
3084
            flags = ppc_defs[i].proc->flags;
3085
            break;
3086
        }
3087
    }
3088
    
3089
    if (&opc_start < &opc_end) {
3090
        start = &opc_start;
3091
        end = &opc_end;
3092
    } else {
3093
        start = &opc_end;
3094
        end = &opc_start;
3095
    }
3096
    for (opc = start + 1; opc != end; opc++) {
3097
        if ((opc->handler.type & flags) != 0)
3098
            if (register_insn(ppc_opcodes, opc) < 0) {
3099
                printf("*** ERROR initializing PPC instruction "
3100
                        "0x%02x 0x%02x 0x%02x\n", opc->opc1, opc->opc2,
3101
                        opc->opc3);
3102
                return -1;
3103
            }
3104
    }
3105
    fix_opcode_tables(ppc_opcodes);
3106

    
3107
    return 0;
3108
}
3109

    
3110

    
3111
/*****************************************************************************/
3112
/* Misc PPC helpers */
3113

    
3114
void cpu_dump_state(CPUState *env, FILE *f, 
3115
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
3116
                    int flags)
3117
{
3118
    int i;
3119

    
3120
    cpu_fprintf(f, "nip=0x%08x LR=0x%08x CTR=0x%08x XER=0x%08x "
3121
            "MSR=0x%08x\n", env->nip, env->lr, env->ctr,
3122
            _load_xer(env), _load_msr(env));
3123
        for (i = 0; i < 32; i++) {
3124
            if ((i & 7) == 0)
3125
            cpu_fprintf(f, "GPR%02d:", i);
3126
        cpu_fprintf(f, " %08x", env->gpr[i]);
3127
            if ((i & 7) == 7)
3128
            cpu_fprintf(f, "\n");
3129
        }
3130
    cpu_fprintf(f, "CR: 0x");
3131
        for (i = 0; i < 8; i++)
3132
        cpu_fprintf(f, "%01x", env->crf[i]);
3133
    cpu_fprintf(f, "  [");
3134
        for (i = 0; i < 8; i++) {
3135
            char a = '-';
3136
            if (env->crf[i] & 0x08)
3137
                a = 'L';
3138
            else if (env->crf[i] & 0x04)
3139
                a = 'G';
3140
            else if (env->crf[i] & 0x02)
3141
                a = 'E';
3142
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
3143
        }
3144
    cpu_fprintf(f, " ] ");
3145
    cpu_fprintf(f, "TB: 0x%08x %08x\n", cpu_ppc_load_tbu(env),
3146
            cpu_ppc_load_tbl(env));
3147
        for (i = 0; i < 16; i++) {
3148
            if ((i & 3) == 0)
3149
            cpu_fprintf(f, "FPR%02d:", i);
3150
        cpu_fprintf(f, " %016llx", *((uint64_t *)&env->fpr[i]));
3151
            if ((i & 3) == 3)
3152
            cpu_fprintf(f, "\n");
3153
    }
3154
    cpu_fprintf(f, "SRR0 0x%08x SRR1 0x%08x DECR=0x%08x\n",
3155
            env->spr[SRR0], env->spr[SRR1], cpu_ppc_load_decr(env));
3156
    cpu_fprintf(f, "reservation 0x%08x\n", env->reserve);
3157
}
3158

    
3159
CPUPPCState *cpu_ppc_init(void)
3160
{
3161
    CPUPPCState *env;
3162

    
3163
    cpu_exec_init();
3164

    
3165
    env = qemu_mallocz(sizeof(CPUPPCState));
3166
    if (!env)
3167
        return NULL;
3168
//    env->spr[PVR] = 0; /* Basic PPC */
3169
    env->spr[PVR] = 0x00080100; /* G3 CPU */
3170
//    env->spr[PVR] = 0x00083100; /* MPC755 (G3 embedded) */
3171
//    env->spr[PVR] = 0x00070100; /* IBM 750FX */
3172
    tlb_flush(env, 1);
3173
#if defined (DO_SINGLE_STEP)
3174
    /* Single step trace mode */
3175
    msr_se = 1;
3176
#endif
3177
    msr_fp = 1; /* Allow floating point exceptions */
3178
    msr_me = 1; /* Allow machine check exceptions  */
3179
#if defined(CONFIG_USER_ONLY)
3180
    msr_pr = 1;
3181
    cpu_ppc_register(env, 0x00080000);
3182
#else
3183
    env->nip = 0xFFFFFFFC;
3184
#endif
3185
    cpu_single_env = env;
3186
    return env;
3187
}
3188

    
3189
int cpu_ppc_register (CPUPPCState *env, uint32_t pvr)
3190
{
3191
    env->spr[PVR] = pvr;
3192
    if (create_ppc_proc(ppc_opcodes, env->spr[PVR]) < 0)
3193
        return -1;
3194
    init_spr_rights(env->spr[PVR]);
3195

    
3196
    return 0;
3197
}
3198

    
3199
void cpu_ppc_close(CPUPPCState *env)
3200
{
3201
    /* Should also remove all opcode tables... */
3202
    free(env);
3203
}
3204

    
3205
/*****************************************************************************/
3206
int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
3207
                                    int search_pc)
3208
{
3209
    DisasContext ctx, *ctxp = &ctx;
3210
    opc_handler_t **table, *handler;
3211
    target_ulong pc_start;
3212
    uint16_t *gen_opc_end;
3213
    int j, lj = -1;
3214

    
3215
    pc_start = tb->pc;
3216
    gen_opc_ptr = gen_opc_buf;
3217
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
3218
    gen_opparam_ptr = gen_opparam_buf;
3219
    ctx.nip = pc_start;
3220
    ctx.tb = tb;
3221
    ctx.exception = EXCP_NONE;
3222
#if defined(CONFIG_USER_ONLY)
3223
    ctx.mem_idx = msr_le;
3224
#else
3225
    ctx.supervisor = 1 - msr_pr;
3226
    ctx.mem_idx = ((1 - msr_pr) << 1) | msr_le;
3227
#endif
3228
    ctx.fpu_enabled = msr_fp;
3229
#if defined (DO_SINGLE_STEP)
3230
    /* Single step trace mode */
3231
    msr_se = 1;
3232
#endif
3233
    /* Set env in case of segfault during code fetch */
3234
    while (ctx.exception == EXCP_NONE && gen_opc_ptr < gen_opc_end) {
3235
        if (search_pc) {
3236
            j = gen_opc_ptr - gen_opc_buf;
3237
            if (lj < j) {
3238
                lj++;
3239
                while (lj < j)
3240
                    gen_opc_instr_start[lj++] = 0;
3241
                gen_opc_pc[lj] = ctx.nip;
3242
                gen_opc_instr_start[lj] = 1;
3243
            }
3244
        }
3245
#if defined PPC_DEBUG_DISAS
3246
        if (loglevel & CPU_LOG_TB_IN_ASM) {
3247
            fprintf(logfile, "----------------\n");
3248
            fprintf(logfile, "nip=%08x super=%d ir=%d\n",
3249
                    ctx.nip, 1 - msr_pr, msr_ir);
3250
        }
3251
#endif
3252
        ctx.opcode = ldl_code(ctx.nip);
3253
        if (msr_le) {
3254
            ctx.opcode = ((ctx.opcode & 0xFF000000) >> 24) |
3255
                ((ctx.opcode & 0x00FF0000) >> 8) |
3256
                ((ctx.opcode & 0x0000FF00) << 8) |
3257
                ((ctx.opcode & 0x000000FF) << 24);
3258
        }
3259
#if defined PPC_DEBUG_DISAS
3260
        if (loglevel & CPU_LOG_TB_IN_ASM) {
3261
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x) (%s)\n",
3262
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
3263
                    opc3(ctx.opcode), msr_le ? "little" : "big");
3264
        }
3265
#endif
3266
        ctx.nip += 4;
3267
        table = ppc_opcodes;
3268
        handler = table[opc1(ctx.opcode)];
3269
        if (is_indirect_opcode(handler)) {
3270
            table = ind_table(handler);
3271
            handler = table[opc2(ctx.opcode)];
3272
            if (is_indirect_opcode(handler)) {
3273
                table = ind_table(handler);
3274
                handler = table[opc3(ctx.opcode)];
3275
            }
3276
        }
3277
        /* Is opcode *REALLY* valid ? */
3278
                if (handler->handler == &gen_invalid) {
3279
            if (loglevel > 0) {
3280
                    fprintf(logfile, "invalid/unsupported opcode: "
3281
                        "%02x - %02x - %02x (%08x) 0x%08x %d\n",
3282
                            opc1(ctx.opcode), opc2(ctx.opcode),
3283
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
3284
            } else {
3285
                printf("invalid/unsupported opcode: "
3286
                       "%02x - %02x - %02x (%08x) 0x%08x %d\n",
3287
                       opc1(ctx.opcode), opc2(ctx.opcode),
3288
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
3289
            }
3290
                } else {
3291
            if ((ctx.opcode & handler->inval) != 0) {
3292
                if (loglevel > 0) {
3293
                    fprintf(logfile, "invalid bits: %08x for opcode: "
3294
                            "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
3295
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
3296
                            opc2(ctx.opcode), opc3(ctx.opcode),
3297
                            ctx.opcode, ctx.nip - 4);
3298
                } else {
3299
                    printf("invalid bits: %08x for opcode: "
3300
                           "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
3301
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
3302
                            opc2(ctx.opcode), opc3(ctx.opcode),
3303
                           ctx.opcode, ctx.nip - 4);
3304
            }
3305
                RET_INVAL(ctxp);
3306
                break;
3307
            }
3308
        }
3309
        (*(handler->handler))(&ctx);
3310
        /* Check trace mode exceptions */
3311
        if ((msr_be && ctx.exception == EXCP_BRANCH) ||
3312
            /* Check in single step trace mode
3313
             * we need to stop except if:
3314
             * - rfi, trap or syscall
3315
             * - first instruction of an exception handler
3316
             */
3317
            (msr_se && (ctx.nip < 0x100 ||
3318
                        ctx.nip > 0xF00 ||
3319
                        (ctx.nip & 0xFC) != 0x04) &&
3320
             ctx.exception != EXCP_SYSCALL && ctx.exception != EXCP_RFI &&
3321
             ctx.exception != EXCP_TRAP)) {
3322
            RET_EXCP(ctxp, EXCP_TRACE, 0);
3323
        }
3324
        /* if we reach a page boundary, stop generation */
3325
        if ((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) 
3326
            break;
3327
    }
3328
    if (ctx.exception == EXCP_NONE) {
3329
        gen_op_b((unsigned long)ctx.tb, ctx.nip);
3330
    } else if (ctx.exception != EXCP_BRANCH) {
3331
        gen_op_set_T0(0);
3332
    }
3333
#if 1
3334
    /* TO BE FIXED: T0 hasn't got a proper value, which makes tb_add_jump
3335
     *              do bad business and then qemu crashes !
3336
     */
3337
    gen_op_set_T0(0);
3338
#endif
3339
    /* Generate the return instruction */
3340
    gen_op_exit_tb();
3341
    *gen_opc_ptr = INDEX_op_end;
3342
    if (search_pc) {
3343
        j = gen_opc_ptr - gen_opc_buf;
3344
        lj++;
3345
        while (lj <= j)
3346
            gen_opc_instr_start[lj++] = 0;
3347
        tb->size = 0;
3348
#if 0
3349
        if (loglevel > 0) {
3350
            page_dump(logfile);
3351
        }
3352
#endif
3353
    } else {
3354
        tb->size = ctx.nip - pc_start;
3355
    }
3356
#ifdef DEBUG_DISAS
3357
    if (loglevel & CPU_LOG_TB_CPU) {
3358
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
3359
        cpu_dump_state(env, logfile, fprintf, 0);
3360
    }
3361
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3362
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
3363
        target_disas(logfile, pc_start, ctx.nip - pc_start, 0);
3364
        fprintf(logfile, "\n");
3365
    }
3366
    if (loglevel & CPU_LOG_TB_OP) {
3367
        fprintf(logfile, "OP:\n");
3368
        dump_ops(gen_opc_buf, gen_opparam_buf);
3369
        fprintf(logfile, "\n");
3370
    }
3371
#endif
3372
    return 0;
3373
}
3374

    
3375
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
3376
{
3377
    return gen_intermediate_code_internal(env, tb, 0);
3378
}
3379

    
3380
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
3381
{
3382
    return gen_intermediate_code_internal(env, tb, 1);
3383
}