Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ e98a6e40

History | View | Annotate | Download (105.1 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 "dyngen-exec.h"
21
#include "cpu.h"
22
#include "exec.h"
23
#include "disas.h"
24

    
25
//#define DO_SINGLE_STEP
26
//#define DO_STEP_FLUSH
27
//#define DEBUG_DISAS
28

    
29
enum {
30
#define DEF(s, n, copy_size) INDEX_op_ ## s,
31
#include "opc.h"
32
#undef DEF
33
    NB_OPS,
34
};
35

    
36
static uint16_t *gen_opc_ptr;
37
static uint32_t *gen_opparam_ptr;
38

    
39
#include "gen-op.h"
40

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

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

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

    
79
/* Condition register moves */
80
GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);
81
GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);
82
GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);
83
GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);
84

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

    
104
/* Segment register moves */
105
GEN16(gen_op_load_sr, gen_op_load_sr);
106
GEN16(gen_op_store_sr, gen_op_store_sr);
107

    
108
/* General purpose registers moves */
109
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
110
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
111
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);
112

    
113
GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
114
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
115
GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);
116

    
117
/* floating point registers moves */
118
GEN32(gen_op_load_fpr_FT0, gen_op_load_fpr_FT0_fpr);
119
GEN32(gen_op_load_fpr_FT1, gen_op_load_fpr_FT1_fpr);
120
GEN32(gen_op_load_fpr_FT2, gen_op_load_fpr_FT2_fpr);
121
GEN32(gen_op_store_FT0_fpr, gen_op_store_FT0_fpr_fpr);
122
GEN32(gen_op_store_FT1_fpr, gen_op_store_FT1_fpr_fpr);
123
GEN32(gen_op_store_FT2_fpr, gen_op_store_FT2_fpr_fpr);
124

    
125
static uint8_t  spr_access[1024 / 2];
126

    
127
/* internal defines */
128
typedef struct DisasContext {
129
    struct TranslationBlock *tb;
130
    uint32_t *nip;
131
    uint32_t opcode;
132
    uint32_t exception;
133
    /* Time base offset */
134
    uint32_t tb_offset;
135
    /* Decrementer offset */
136
    uint32_t decr_offset;
137
    /* Execution mode */
138
#if !defined(CONFIG_USER_ONLY)
139
    int supervisor;
140
#endif
141
    /* Routine used to access memory */
142
    int mem_idx;
143
} DisasContext;
144

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

    
154
#define RET_EXCP(excp, error)                                                 \
155
do {                                                                          \
156
    gen_op_queue_exception_err(excp, error);                                  \
157
    ctx->exception = excp;                                                    \
158
    return;                                                                   \
159
} while (0)
160

    
161
#define RET_INVAL()                                                           \
162
RET_EXCP(EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_INVAL)
163

    
164
#define RET_PRIVOPC()                                                         \
165
RET_EXCP(EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_OPC)
166

    
167
#define RET_PRIVREG()                                                         \
168
RET_EXCP(EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_REG)
169

    
170
#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
171
static void gen_##name (DisasContext *ctx);                                   \
172
GEN_OPCODE(name, opc1, opc2, opc3, inval, type);                              \
173
static void gen_##name (DisasContext *ctx)
174

    
175
typedef struct opcode_t {
176
    unsigned char opc1, opc2, opc3;
177
    opc_handler_t handler;
178
} opcode_t;
179

    
180
/* XXX: move that elsewhere */
181
extern FILE *logfile;
182
extern int loglevel;
183

    
184
/***                           Instruction decoding                        ***/
185
#define EXTRACT_HELPER(name, shift, nb)                                       \
186
static inline uint32_t name (uint32_t opcode)                                 \
187
{                                                                             \
188
    return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
189
}
190

    
191
#define EXTRACT_SHELPER(name, shift, nb)                                      \
192
static inline int32_t name (uint32_t opcode)                                  \
193
{                                                                             \
194
    return s_ext16((opcode >> (shift)) & ((1 << (nb)) - 1));                  \
195
}
196

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

    
240
EXTRACT_HELPER(CRM, 12, 8);
241
EXTRACT_HELPER(FM, 17, 8);
242
EXTRACT_HELPER(SR, 16, 4);
243
EXTRACT_HELPER(FPIMM, 20, 4);
244

    
245
/***                            Jump target decoding                       ***/
246
/* Displacement */
247
EXTRACT_SHELPER(d, 0, 16);
248
/* Immediate address */
249
static inline uint32_t LI (uint32_t opcode)
250
{
251
    return (opcode >> 0) & 0x03FFFFFC;
252
}
253

    
254
static inline uint32_t BD (uint32_t opcode)
255
{
256
    return (opcode >> 0) & 0xFFFC;
257
}
258

    
259
EXTRACT_HELPER(BO, 21, 5);
260
EXTRACT_HELPER(BI, 16, 5);
261
/* Absolute/relative address */
262
EXTRACT_HELPER(AA, 1, 1);
263
/* Link */
264
EXTRACT_HELPER(LK, 0, 1);
265

    
266
/* Create a mask between <start> and <end> bits */
267
static inline uint32_t MASK (uint32_t start, uint32_t end)
268
{
269
    uint32_t ret;
270

    
271
    ret = (((uint32_t)(-1)) >> (start)) ^ (((uint32_t)(-1) >> (end)) >> 1);
272
    if (start > end)
273
        return ~ret;
274

    
275
    return ret;
276
}
277

    
278
#define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
279
__attribute__ ((section(".opcodes"), unused))                                 \
280
static opcode_t opc_##name = {                                                \
281
    .opc1 = op1,                                                              \
282
    .opc2 = op2,                                                              \
283
    .opc3 = op3,                                                              \
284
    .handler = {                                                              \
285
        .inval   = invl,                                                      \
286
        .type = _typ,                                                         \
287
        .handler = &gen_##name,                                               \
288
    },                                                                        \
289
}
290

    
291
#define GEN_OPCODE_MARK(name)                                                 \
292
__attribute__ ((section(".opcodes"), unused))                                 \
293
static opcode_t opc_##name = {                                                \
294
    .opc1 = 0xFF,                                                             \
295
    .opc2 = 0xFF,                                                             \
296
    .opc3 = 0xFF,                                                             \
297
    .handler = {                                                              \
298
        .inval   = 0x00000000,                                                \
299
        .type = 0x00,                                                         \
300
        .handler = NULL,                                                      \
301
    },                                                                        \
302
}
303

    
304
/* Start opcode list */
305
GEN_OPCODE_MARK(start);
306

    
307
/* Invalid instruction */
308
GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
309
{
310
    RET_INVAL();
311
}
312

    
313
/* Special opcode to stop emulation */
314
GEN_HANDLER(stop, 0x06, 0x00, 0xFF, 0x03FFFFC1, PPC_COMMON)
315
{
316
    gen_op_queue_exception(EXCP_HLT);
317
    ctx->exception = EXCP_HLT;
318
}
319

    
320
/* Special opcode to call open-firmware */
321
GEN_HANDLER(of_enter, 0x06, 0x01, 0xFF, 0x03FFFFC1, PPC_COMMON)
322
{
323
    gen_op_queue_exception(EXCP_OFCALL);
324
    ctx->exception = EXCP_OFCALL;
325
}
326

    
327
/* Special opcode to call RTAS */
328
GEN_HANDLER(rtas_enter, 0x06, 0x02, 0xFF, 0x03FFFFC1, PPC_COMMON)
329
{
330
    printf("RTAS entry point !\n");
331
    gen_op_queue_exception(EXCP_RTASCALL);
332
    ctx->exception = EXCP_RTASCALL;
333
}
334

    
335
static opc_handler_t invalid_handler = {
336
    .inval   = 0xFFFFFFFF,
337
    .type    = PPC_NONE,
338
    .handler = gen_invalid,
339
};
340

    
341
/***                           Integer arithmetic                          ***/
342
#define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval)                       \
343
GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
344
{                                                                             \
345
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
346
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
347
    gen_op_##name();                                                          \
348
    if (Rc(ctx->opcode) != 0)                                                 \
349
        gen_op_set_Rc0();                                                     \
350
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
351
}
352

    
353
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval)                     \
354
GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
355
{                                                                             \
356
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
357
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
358
    gen_op_##name();                                                          \
359
    if (Rc(ctx->opcode) != 0)                                                 \
360
        gen_op_set_Rc0_ov();                                                  \
361
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
362
}
363

    
364
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3)                              \
365
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
366
{                                                                             \
367
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
368
    gen_op_##name();                                                          \
369
    if (Rc(ctx->opcode) != 0)                                                 \
370
        gen_op_set_Rc0();                                                     \
371
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
372
}
373
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3)                            \
374
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
375
{                                                                             \
376
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
377
    gen_op_##name();                                                          \
378
    if (Rc(ctx->opcode) != 0)                                                 \
379
        gen_op_set_Rc0_ov();                                                  \
380
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
381
}
382

    
383
/* Two operands arithmetic functions */
384
#define GEN_INT_ARITH2(name, opc1, opc2, opc3)                                \
385
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000)                          \
386
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000)
387

    
388
/* Two operands arithmetic functions with no overflow allowed */
389
#define GEN_INT_ARITHN(name, opc1, opc2, opc3)                                \
390
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000400)
391

    
392
/* One operand arithmetic functions */
393
#define GEN_INT_ARITH1(name, opc1, opc2, opc3)                                \
394
__GEN_INT_ARITH1(name, opc1, opc2, opc3)                                      \
395
__GEN_INT_ARITH1_O(name##o, opc1, opc2, opc3 | 0x10)
396

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

    
434
    if (rA(ctx->opcode) == 0) {
435
        gen_op_set_T0(simm);
436
    } else {
437
        gen_op_load_gpr_T0(rA(ctx->opcode));
438
        gen_op_addi(simm);
439
    }
440
    gen_op_store_T0_gpr(rD(ctx->opcode));
441
}
442
/* addic */
443
GEN_HANDLER(addic, 0x0C, 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_store_T0_gpr(rD(ctx->opcode));
448
}
449
/* addic. */
450
GEN_HANDLER(addic_, 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
451
{
452
    gen_op_load_gpr_T0(rA(ctx->opcode));
453
    gen_op_addic(SIMM(ctx->opcode));
454
    gen_op_set_Rc0();
455
    gen_op_store_T0_gpr(rD(ctx->opcode));
456
}
457
/* addis */
458
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
459
{
460
    int32_t simm = SIMM(ctx->opcode);
461

    
462
    if (rA(ctx->opcode) == 0) {
463
        gen_op_set_T0(simm << 16);
464
    } else {
465
        gen_op_load_gpr_T0(rA(ctx->opcode));
466
        gen_op_addi(simm << 16);
467
    }
468
    gen_op_store_T0_gpr(rD(ctx->opcode));
469
}
470
/* mulli */
471
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
472
{
473
    gen_op_load_gpr_T0(rA(ctx->opcode));
474
    gen_op_mulli(SIMM(ctx->opcode));
475
    gen_op_store_T0_gpr(rD(ctx->opcode));
476
}
477
/* subfic */
478
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
479
{
480
    gen_op_load_gpr_T0(rA(ctx->opcode));
481
    gen_op_subfic(SIMM(ctx->opcode));
482
    gen_op_store_T0_gpr(rD(ctx->opcode));
483
}
484

    
485
/***                           Integer comparison                          ***/
486
#define GEN_CMP(name, opc)                                                    \
487
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, PPC_INTEGER)                   \
488
{                                                                             \
489
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
490
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
491
    gen_op_##name();                                                          \
492
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
493
}
494

    
495
/* cmp */
496
GEN_CMP(cmp, 0x00);
497
/* cmpi */
498
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
499
{
500
    gen_op_load_gpr_T0(rA(ctx->opcode));
501
    gen_op_cmpi(SIMM(ctx->opcode));
502
    gen_op_store_T0_crf(crfD(ctx->opcode));
503
}
504
/* cmpl */
505
GEN_CMP(cmpl, 0x01);
506
/* cmpli */
507
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
508
{
509
    gen_op_load_gpr_T0(rA(ctx->opcode));
510
    gen_op_cmpli(UIMM(ctx->opcode));
511
    gen_op_store_T0_crf(crfD(ctx->opcode));
512
}
513

    
514
/***                            Integer logical                            ***/
515
#define __GEN_LOGICAL2(name, opc2, opc3)                                      \
516
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, PPC_INTEGER)                  \
517
{                                                                             \
518
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
519
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
520
    gen_op_##name();                                                          \
521
    if (Rc(ctx->opcode) != 0)                                                 \
522
        gen_op_set_Rc0();                                                     \
523
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
524
}
525
#define GEN_LOGICAL2(name, opc)                                               \
526
__GEN_LOGICAL2(name, 0x1C, opc)
527

    
528
#define GEN_LOGICAL1(name, opc)                                               \
529
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, PPC_INTEGER)                   \
530
{                                                                             \
531
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
532
    gen_op_##name();                                                          \
533
    if (Rc(ctx->opcode) != 0)                                                 \
534
        gen_op_set_Rc0();                                                     \
535
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
536
}
537

    
538
/* and & and. */
539
GEN_LOGICAL2(and, 0x00);
540
/* andc & andc. */
541
GEN_LOGICAL2(andc, 0x01);
542
/* andi. */
543
GEN_HANDLER(andi_, 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
544
{
545
    gen_op_load_gpr_T0(rS(ctx->opcode));
546
    gen_op_andi_(UIMM(ctx->opcode));
547
    gen_op_set_Rc0();
548
    gen_op_store_T0_gpr(rA(ctx->opcode));
549
}
550
/* andis. */
551
GEN_HANDLER(andis_, 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
552
{
553
    gen_op_load_gpr_T0(rS(ctx->opcode));
554
    gen_op_andi_(UIMM(ctx->opcode) << 16);
555
    gen_op_set_Rc0();
556
    gen_op_store_T0_gpr(rA(ctx->opcode));
557
}
558

    
559
/* cntlzw */
560
GEN_LOGICAL1(cntlzw, 0x00);
561
/* eqv & eqv. */
562
GEN_LOGICAL2(eqv, 0x08);
563
/* extsb & extsb. */
564
GEN_LOGICAL1(extsb, 0x1D);
565
/* extsh & extsh. */
566
GEN_LOGICAL1(extsh, 0x1C);
567
/* nand & nand. */
568
GEN_LOGICAL2(nand, 0x0E);
569
/* nor & nor. */
570
GEN_LOGICAL2(nor, 0x03);
571

    
572
/* or & or. */
573
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
574
{
575
    gen_op_load_gpr_T0(rS(ctx->opcode));
576
    /* Optimisation for mr case */
577
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
578
        gen_op_load_gpr_T1(rB(ctx->opcode));
579
        gen_op_or();
580
    }
581
    if (Rc(ctx->opcode) != 0)
582
        gen_op_set_Rc0();
583
    gen_op_store_T0_gpr(rA(ctx->opcode));
584
}
585

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

    
608
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
609
        /* NOP */
610
        return;
611
        }
612
        gen_op_load_gpr_T0(rS(ctx->opcode));
613
    if (uimm != 0)
614
        gen_op_ori(uimm);
615
        gen_op_store_T0_gpr(rA(ctx->opcode));
616
}
617
/* oris */
618
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
619
{
620
    uint32_t uimm = UIMM(ctx->opcode);
621

    
622
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
623
        /* NOP */
624
        return;
625
        }
626
        gen_op_load_gpr_T0(rS(ctx->opcode));
627
    if (uimm != 0)
628
        gen_op_ori(uimm << 16);
629
        gen_op_store_T0_gpr(rA(ctx->opcode));
630
}
631
/* xori */
632
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
633
{
634
    uint32_t uimm = UIMM(ctx->opcode);
635

    
636
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
637
        /* NOP */
638
        return;
639
    }
640
    gen_op_load_gpr_T0(rS(ctx->opcode));
641
    if (uimm != 0)
642
    gen_op_xori(UIMM(ctx->opcode));
643
    gen_op_store_T0_gpr(rA(ctx->opcode));
644
}
645

    
646
/* xoris */
647
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
648
{
649
    uint32_t uimm = UIMM(ctx->opcode);
650

    
651
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
652
        /* NOP */
653
        return;
654
    }
655
    gen_op_load_gpr_T0(rS(ctx->opcode));
656
    if (uimm != 0)
657
    gen_op_xori(UIMM(ctx->opcode) << 16);
658
    gen_op_store_T0_gpr(rA(ctx->opcode));
659
}
660

    
661
/***                             Integer rotate                            ***/
662
/* rlwimi & rlwimi. */
663
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
664
{
665
    uint32_t mb, me;
666

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

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

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

    
748
/***                       Floating-Point arithmetic                       ***/
749
#define _GEN_FLOAT_ACB(name, op1, op2)                                        \
750
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, PPC_FLOAT)                   \
751
{                                                                             \
752
    gen_op_reset_scrfx();                                                     \
753
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
754
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
755
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
756
    gen_op_f##name();                                                         \
757
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
758
    if (Rc(ctx->opcode))                                                      \
759
        gen_op_set_Rc1();                                                     \
760
}
761

    
762
#define GEN_FLOAT_ACB(name, op2)                                              \
763
_GEN_FLOAT_ACB(name, 0x3F, op2);                                              \
764
_GEN_FLOAT_ACB(name##s, 0x3B, op2);
765

    
766
#define _GEN_FLOAT_AB(name, op1, op2, inval)                                  \
767
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
768
{                                                                             \
769
    gen_op_reset_scrfx();                                                     \
770
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
771
    gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
772
    gen_op_f##name();                                                         \
773
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
774
    if (Rc(ctx->opcode))                                                      \
775
        gen_op_set_Rc1();                                                     \
776
}
777
#define GEN_FLOAT_AB(name, op2, inval)                                        \
778
_GEN_FLOAT_AB(name, 0x3F, op2, inval);                                        \
779
_GEN_FLOAT_AB(name##s, 0x3B, op2, inval);
780

    
781
#define _GEN_FLOAT_AC(name, op1, op2, inval)                                  \
782
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
783
{                                                                             \
784
    gen_op_reset_scrfx();                                                     \
785
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
786
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
787
    gen_op_f##name();                                                         \
788
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
789
    if (Rc(ctx->opcode))                                                      \
790
        gen_op_set_Rc1();                                                     \
791
}
792
#define GEN_FLOAT_AC(name, op2, inval)                                        \
793
_GEN_FLOAT_AC(name, 0x3F, op2, inval);                                        \
794
_GEN_FLOAT_AC(name##s, 0x3B, op2, inval);
795

    
796
#define GEN_FLOAT_B(name, op2, op3)                                           \
797
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, PPC_FLOAT)                   \
798
{                                                                             \
799
    gen_op_reset_scrfx();                                                     \
800
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
801
    gen_op_f##name();                                                         \
802
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
803
    if (Rc(ctx->opcode))                                                      \
804
        gen_op_set_Rc1();                                                     \
805
}
806

    
807
#define GEN_FLOAT_BS(name, op2)                                               \
808
GEN_HANDLER(f##name, 0x3F, op2, 0xFF, 0x001F07C0, PPC_FLOAT)                  \
809
{                                                                             \
810
    gen_op_reset_scrfx();                                                     \
811
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
812
    gen_op_f##name();                                                         \
813
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
814
    if (Rc(ctx->opcode))                                                      \
815
        gen_op_set_Rc1();                                                     \
816
}
817

    
818
/* fadd - fadds */
819
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
820
/* fdiv */
821
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
822
/* fmul */
823
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
824

    
825
/* fres */
826
GEN_FLOAT_BS(res, 0x18);
827

    
828
/* frsqrte */
829
GEN_FLOAT_BS(rsqrte, 0x1A);
830

    
831
/* fsel */
832
_GEN_FLOAT_ACB(sel, 0x3F, 0x17);
833
/* fsub */
834
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
835
/* Optional: */
836
/* fsqrt */
837
GEN_FLOAT_BS(sqrt, 0x16);
838

    
839
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_OPT)
840
{
841
    gen_op_reset_scrfx();
842
    gen_op_load_fpr_FT0(rB(ctx->opcode));
843
    gen_op_fsqrts();
844
    gen_op_store_FT0_fpr(rD(ctx->opcode));
845
    if (Rc(ctx->opcode))
846
        gen_op_set_Rc1();
847
}
848

    
849
/***                     Floating-Point multiply-and-add                   ***/
850
/* fmadd */
851
GEN_FLOAT_ACB(madd, 0x1D);
852
/* fmsub */
853
GEN_FLOAT_ACB(msub, 0x1C);
854
/* fnmadd */
855
GEN_FLOAT_ACB(nmadd, 0x1F);
856
/* fnmsub */
857
GEN_FLOAT_ACB(nmsub, 0x1E);
858

    
859
/***                     Floating-Point round & convert                    ***/
860
/* fctiw */
861
GEN_FLOAT_B(ctiw, 0x0E, 0x00);
862
/* fctiwz */
863
GEN_FLOAT_B(ctiwz, 0x0F, 0x00);
864
/* frsp */
865
GEN_FLOAT_B(rsp, 0x0C, 0x00);
866

    
867
/***                         Floating-Point compare                        ***/
868
/* fcmpo */
869
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
870
{
871
    gen_op_reset_scrfx();
872
    gen_op_load_fpr_FT0(rA(ctx->opcode));
873
    gen_op_load_fpr_FT1(rB(ctx->opcode));
874
    gen_op_fcmpo();
875
    gen_op_store_T0_crf(crfD(ctx->opcode));
876
}
877

    
878
/* fcmpu */
879
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
880
{
881
    gen_op_reset_scrfx();
882
    gen_op_load_fpr_FT0(rA(ctx->opcode));
883
    gen_op_load_fpr_FT1(rB(ctx->opcode));
884
    gen_op_fcmpu();
885
    gen_op_store_T0_crf(crfD(ctx->opcode));
886
}
887

    
888
/***                         Floating-point move                           ***/
889
/* fabs */
890
GEN_FLOAT_B(abs, 0x08, 0x08);
891

    
892
/* fmr  - fmr. */
893
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
894
{
895
    gen_op_reset_scrfx();
896
    gen_op_load_fpr_FT0(rB(ctx->opcode));
897
    gen_op_store_FT0_fpr(rD(ctx->opcode));
898
    if (Rc(ctx->opcode))
899
        gen_op_set_Rc1();
900
}
901

    
902
/* fnabs */
903
GEN_FLOAT_B(nabs, 0x08, 0x04);
904
/* fneg */
905
GEN_FLOAT_B(neg, 0x08, 0x01);
906

    
907
/***                  Floating-Point status & ctrl register                ***/
908
/* mcrfs */
909
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
910
{
911
    gen_op_load_fpscr_T0(crfS(ctx->opcode));
912
    gen_op_store_T0_crf(crfD(ctx->opcode));
913
    gen_op_clear_fpscr(crfS(ctx->opcode));
914
}
915

    
916
/* mffs */
917
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
918
{
919
    gen_op_load_fpscr();
920
    gen_op_store_FT0_fpr(rD(ctx->opcode));
921
    if (Rc(ctx->opcode))
922
        gen_op_set_Rc1();
923
}
924

    
925
/* mtfsb0 */
926
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
927
{
928
    uint8_t crb;
929
    
930
    crb = crbD(ctx->opcode) >> 2;
931
    gen_op_load_fpscr_T0(crb);
932
    gen_op_andi_(~(1 << (crbD(ctx->opcode) & 0x03)));
933
    gen_op_store_T0_fpscr(crb);
934
    if (Rc(ctx->opcode))
935
        gen_op_set_Rc1();
936
}
937

    
938
/* mtfsb1 */
939
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
940
{
941
    uint8_t crb;
942
    
943
    crb = crbD(ctx->opcode) >> 2;
944
    gen_op_load_fpscr_T0(crb);
945
    gen_op_ori(1 << (crbD(ctx->opcode) & 0x03));
946
    gen_op_store_T0_fpscr(crb);
947
    if (Rc(ctx->opcode))
948
        gen_op_set_Rc1();
949
}
950

    
951
/* mtfsf */
952
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
953
{
954
    gen_op_load_fpr_FT0(rB(ctx->opcode));
955
    gen_op_store_fpscr(FM(ctx->opcode));
956
    if (Rc(ctx->opcode))
957
        gen_op_set_Rc1();
958
}
959

    
960
/* mtfsfi */
961
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
962
{
963
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
964
    if (Rc(ctx->opcode))
965
        gen_op_set_Rc1();
966
}
967

    
968
/***                             Integer load                              ***/
969
#if defined(CONFIG_USER_ONLY)
970
#define op_ldst(name)        gen_op_##name##_raw()
971
#define OP_LD_TABLE(width)
972
#define OP_ST_TABLE(width)
973
#else
974
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
975
#define OP_LD_TABLE(width)                                                    \
976
static GenOpFunc *gen_op_l##width[] = {                                       \
977
    &gen_op_l##width##_user,                                                  \
978
    &gen_op_l##width##_kernel,                                                \
979
}
980
#define OP_ST_TABLE(width)                                                    \
981
static GenOpFunc *gen_op_st##width[] = {                                      \
982
    &gen_op_st##width##_user,                                                 \
983
    &gen_op_st##width##_kernel,                                               \
984
}
985
#endif
986

    
987
#define GEN_LD(width, opc)                                                    \
988
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)               \
989
{                                                                             \
990
    uint32_t simm = SIMM(ctx->opcode);                                        \
991
    if (rA(ctx->opcode) == 0) {                                               \
992
        gen_op_set_T0(simm);                                                  \
993
    } else {                                                                  \
994
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
995
        if (simm != 0)                                                        \
996
            gen_op_addi(simm);                                                \
997
    }                                                                         \
998
    op_ldst(l##width);                                                        \
999
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1000
}
1001

    
1002
#define GEN_LDU(width, opc)                                                   \
1003
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
1004
{                                                                             \
1005
    uint32_t simm = SIMM(ctx->opcode);                                        \
1006
    if (rA(ctx->opcode) == 0 ||                                               \
1007
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1008
        RET_INVAL();                                                          \
1009
    }                                                                         \
1010
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1011
    if (simm != 0)                                                            \
1012
        gen_op_addi(simm);                                                    \
1013
    op_ldst(l##width);                                                        \
1014
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1015
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1016
}
1017

    
1018
#define GEN_LDUX(width, opc)                                                  \
1019
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
1020
{                                                                             \
1021
    if (rA(ctx->opcode) == 0 ||                                               \
1022
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1023
        RET_INVAL();                                                          \
1024
    }                                                                         \
1025
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1026
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1027
    gen_op_add();                                                             \
1028
    op_ldst(l##width);                                                        \
1029
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1030
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1031
}
1032

    
1033
#define GEN_LDX(width, opc2, opc3)                                            \
1034
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
1035
{                                                                             \
1036
    if (rA(ctx->opcode) == 0) {                                               \
1037
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1038
    } else {                                                                  \
1039
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1040
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1041
        gen_op_add();                                                         \
1042
    }                                                                         \
1043
    op_ldst(l##width);                                                        \
1044
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1045
}
1046

    
1047
#define GEN_LDS(width, op)                                                    \
1048
OP_LD_TABLE(width);                                                           \
1049
GEN_LD(width, op | 0x20);                                                     \
1050
GEN_LDU(width, op | 0x21);                                                    \
1051
GEN_LDUX(width, op | 0x01);                                                   \
1052
GEN_LDX(width, 0x17, op | 0x00)
1053

    
1054
/* lbz lbzu lbzux lbzx */
1055
GEN_LDS(bz, 0x02);
1056
/* lha lhau lhaux lhax */
1057
GEN_LDS(ha, 0x0A);
1058
/* lhz lhzu lhzux lhzx */
1059
GEN_LDS(hz, 0x08);
1060
/* lwz lwzu lwzux lwzx */
1061
GEN_LDS(wz, 0x00);
1062

    
1063
/***                              Integer store                            ***/
1064
#define GEN_ST(width, opc)                                                    \
1065
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
1066
{                                                                             \
1067
    uint32_t simm = SIMM(ctx->opcode);                                        \
1068
    if (rA(ctx->opcode) == 0) {                                               \
1069
        gen_op_set_T0(simm);                                                  \
1070
    } else {                                                                  \
1071
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1072
        if (simm != 0)                                                        \
1073
            gen_op_addi(simm);                                                \
1074
    }                                                                         \
1075
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1076
    op_ldst(st##width);                                                       \
1077
}
1078

    
1079
#define GEN_STU(width, opc)                                                   \
1080
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
1081
{                                                                             \
1082
    uint32_t simm = SIMM(ctx->opcode);                                        \
1083
    if (rA(ctx->opcode) == 0) {                                               \
1084
        RET_INVAL();                                                          \
1085
    }                                                                         \
1086
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1087
    if (simm != 0)                                                            \
1088
        gen_op_addi(simm);                                                    \
1089
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1090
    op_ldst(st##width);                                                       \
1091
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1092
}
1093

    
1094
#define GEN_STUX(width, opc)                                                  \
1095
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
1096
{                                                                             \
1097
    if (rA(ctx->opcode) == 0) {                                               \
1098
        RET_INVAL();                                                          \
1099
    }                                                                         \
1100
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1101
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1102
    gen_op_add();                                                             \
1103
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1104
    op_ldst(st##width);                                                       \
1105
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1106
}
1107

    
1108
#define GEN_STX(width, opc2, opc3)                                            \
1109
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
1110
{                                                                             \
1111
    if (rA(ctx->opcode) == 0) {                                               \
1112
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1113
    } else {                                                                  \
1114
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1115
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1116
        gen_op_add();                                                         \
1117
    }                                                                         \
1118
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1119
    op_ldst(st##width);                                                       \
1120
}
1121

    
1122
#define GEN_STS(width, op)                                                    \
1123
OP_ST_TABLE(width);                                                           \
1124
GEN_ST(width, op | 0x20);                                                     \
1125
GEN_STU(width, op | 0x21);                                                    \
1126
GEN_STUX(width, op | 0x01);                                                   \
1127
GEN_STX(width, 0x17, op | 0x00)
1128

    
1129
/* stb stbu stbux stbx */
1130
GEN_STS(b, 0x06);
1131
/* sth sthu sthux sthx */
1132
GEN_STS(h, 0x0C);
1133
/* stw stwu stwux stwx */
1134
GEN_STS(w, 0x04);
1135

    
1136
/***                Integer load and store with byte reverse               ***/
1137
/* lhbrx */
1138
OP_LD_TABLE(hbr);
1139
GEN_LDX(hbr, 0x16, 0x18);
1140
/* lwbrx */
1141
OP_LD_TABLE(wbr);
1142
GEN_LDX(wbr, 0x16, 0x10);
1143
/* sthbrx */
1144
OP_ST_TABLE(hbr);
1145
GEN_STX(hbr, 0x16, 0x1C);
1146
/* stwbrx */
1147
OP_ST_TABLE(wbr);
1148
GEN_STX(wbr, 0x16, 0x14);
1149

    
1150
/***                    Integer load and store multiple                    ***/
1151
#if defined(CONFIG_USER_ONLY)
1152
#define op_ldstm(name, reg) gen_op_##name##_raw(reg)
1153
#else
1154
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
1155
static GenOpFunc1 *gen_op_lmw[] = {
1156
    &gen_op_lmw_user,
1157
    &gen_op_lmw_kernel,
1158
};
1159
static GenOpFunc1 *gen_op_stmw[] = {
1160
    &gen_op_stmw_user,
1161
    &gen_op_stmw_kernel,
1162
};
1163
#endif
1164

    
1165
/* lmw */
1166
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1167
{
1168
    int simm = SIMM(ctx->opcode);
1169

    
1170
    if (rA(ctx->opcode) == 0) {
1171
        gen_op_set_T0(simm);
1172
    } else {
1173
        gen_op_load_gpr_T0(rA(ctx->opcode));
1174
        if (simm != 0)
1175
            gen_op_addi(simm);
1176
    }
1177
    op_ldstm(lmw, rD(ctx->opcode));
1178
}
1179

    
1180
/* stmw */
1181
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1182
{
1183
    int simm = SIMM(ctx->opcode);
1184

    
1185
    if (rA(ctx->opcode) == 0) {
1186
        gen_op_set_T0(simm);
1187
    } else {
1188
        gen_op_load_gpr_T0(rA(ctx->opcode));
1189
        if (simm != 0)
1190
            gen_op_addi(simm);
1191
    }
1192
    op_ldstm(stmw, rS(ctx->opcode));
1193
}
1194

    
1195
/***                    Integer load and store strings                     ***/
1196
#if defined(CONFIG_USER_ONLY)
1197
#define op_ldsts(name, start) gen_op_##name##_raw(start)
1198
#define op_ldstsx(name, rd, ra, rb) gen_op_##name##_raw(rd, ra, rb)
1199
#else
1200
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
1201
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
1202
static GenOpFunc1 *gen_op_lswi[] = {
1203
    &gen_op_lswi_user,
1204
    &gen_op_lswi_kernel,
1205
};
1206
static GenOpFunc3 *gen_op_lswx[] = {
1207
    &gen_op_lswx_user,
1208
    &gen_op_lswx_kernel,
1209
};
1210
static GenOpFunc1 *gen_op_stsw[] = {
1211
    &gen_op_stsw_user,
1212
    &gen_op_stsw_kernel,
1213
};
1214
#endif
1215

    
1216
/* lswi */
1217
/* PPC32 specification says we must generate an exception if
1218
 * rA is in the range of registers to be loaded.
1219
 * In an other hand, IBM says this is valid, but rA won't be loaded.
1220
 * For now, I'll follow the spec...
1221
 */
1222
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
1223
{
1224
    int nb = NB(ctx->opcode);
1225
    int start = rD(ctx->opcode);
1226
    int ra = rA(ctx->opcode);
1227
    int nr;
1228

    
1229
    if (nb == 0)
1230
        nb = 32;
1231
    nr = nb / 4;
1232
    if (((start + nr) > 32  && start <= ra && (start + nr - 32) > ra) ||
1233
        ((start + nr) <= 32 && start <= ra && (start + nr) > ra)) {
1234
        RET_EXCP(EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_LSWX);
1235
    }
1236
    if (ra == 0) {
1237
        gen_op_set_T0(0);
1238
    } else {
1239
        gen_op_load_gpr_T0(ra);
1240
    }
1241
    gen_op_set_T1(nb);
1242
    op_ldsts(lswi, start);
1243
}
1244

    
1245
/* lswx */
1246
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
1247
{
1248
    int ra = rA(ctx->opcode);
1249
    int rb = rB(ctx->opcode);
1250

    
1251
    if (ra == 0) {
1252
        gen_op_load_gpr_T0(rb);
1253
        ra = rb;
1254
    } else {
1255
        gen_op_load_gpr_T0(ra);
1256
        gen_op_load_gpr_T1(rb);
1257
        gen_op_add();
1258
    }
1259
    gen_op_load_xer_bc();
1260
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
1261
}
1262

    
1263
/* stswi */
1264
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
1265
{
1266
    if (rA(ctx->opcode) == 0) {
1267
        gen_op_set_T0(0);
1268
    } else {
1269
        gen_op_load_gpr_T0(rA(ctx->opcode));
1270
    }
1271
    gen_op_set_T1(NB(ctx->opcode));
1272
    op_ldsts(stsw, rS(ctx->opcode));
1273
}
1274

    
1275
/* stswx */
1276
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
1277
{
1278
    int ra = rA(ctx->opcode);
1279

    
1280
    if (ra == 0) {
1281
        gen_op_load_gpr_T0(rB(ctx->opcode));
1282
        ra = rB(ctx->opcode);
1283
    } else {
1284
        gen_op_load_gpr_T0(ra);
1285
        gen_op_load_gpr_T1(rB(ctx->opcode));
1286
        gen_op_add();
1287
    }
1288
    gen_op_load_xer_bc();
1289
    op_ldsts(stsw, rS(ctx->opcode));
1290
}
1291

    
1292
/***                        Memory synchronisation                         ***/
1293
/* eieio */
1294
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FF0801, PPC_MEM)
1295
{
1296
}
1297

    
1298
/* isync */
1299
GEN_HANDLER(isync, 0x13, 0x16, 0xFF, 0x03FF0801, PPC_MEM)
1300
{
1301
}
1302

    
1303
/* lwarx */
1304
#if defined(CONFIG_USER_ONLY)
1305
#define op_lwarx() gen_op_lwarx_raw()
1306
#define op_stwcx() gen_op_stwcx_raw()
1307
#else
1308
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
1309
static GenOpFunc *gen_op_lwarx[] = {
1310
    &gen_op_lwarx_user,
1311
    &gen_op_lwarx_kernel,
1312
};
1313
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
1314
static GenOpFunc *gen_op_stwcx[] = {
1315
    &gen_op_stwcx_user,
1316
    &gen_op_stwcx_kernel,
1317
};
1318
#endif
1319

    
1320
GEN_HANDLER(lwarx, 0x1F, 0x14, 0xFF, 0x00000001, PPC_RES)
1321
{
1322
    if (rA(ctx->opcode) == 0) {
1323
        gen_op_load_gpr_T0(rB(ctx->opcode));
1324
    } else {
1325
        gen_op_load_gpr_T0(rA(ctx->opcode));
1326
        gen_op_load_gpr_T1(rB(ctx->opcode));
1327
        gen_op_add();
1328
    }
1329
    op_lwarx();
1330
    gen_op_store_T1_gpr(rD(ctx->opcode));
1331
}
1332

    
1333
/* stwcx. */
1334
GEN_HANDLER(stwcx_, 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
1335
{
1336
        if (rA(ctx->opcode) == 0) {
1337
            gen_op_load_gpr_T0(rB(ctx->opcode));
1338
        } else {
1339
            gen_op_load_gpr_T0(rA(ctx->opcode));
1340
            gen_op_load_gpr_T1(rB(ctx->opcode));
1341
        gen_op_add();
1342
        }
1343
    gen_op_load_gpr_T1(rS(ctx->opcode));
1344
    op_stwcx();
1345
}
1346

    
1347
/* sync */
1348
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x03FF0801, PPC_MEM)
1349
{
1350
}
1351

    
1352
/***                         Floating-point load                           ***/
1353
#define GEN_LDF(width, opc)                                                   \
1354
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)               \
1355
{                                                                             \
1356
    uint32_t simm = SIMM(ctx->opcode);                                        \
1357
    if (rA(ctx->opcode) == 0) {                                               \
1358
        gen_op_set_T0(simm);                                                  \
1359
    } else {                                                                  \
1360
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1361
        if (simm != 0)                                                        \
1362
            gen_op_addi(simm);                                                \
1363
    }                                                                         \
1364
    op_ldst(l##width);                                                        \
1365
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1366
}
1367

    
1368
#define GEN_LDUF(width, opc)                                                  \
1369
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
1370
{                                                                             \
1371
    uint32_t simm = SIMM(ctx->opcode);                                        \
1372
    if (rA(ctx->opcode) == 0 ||                                               \
1373
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1374
        RET_INVAL();                                                          \
1375
    }                                                                         \
1376
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1377
    if (simm != 0)                                                            \
1378
        gen_op_addi(simm);                                                    \
1379
    op_ldst(l##width);                                                        \
1380
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1381
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1382
}
1383

    
1384
#define GEN_LDUXF(width, opc)                                                 \
1385
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
1386
{                                                                             \
1387
    if (rA(ctx->opcode) == 0 ||                                               \
1388
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1389
        RET_INVAL();                                                          \
1390
    }                                                                         \
1391
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1392
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1393
    gen_op_add();                                                             \
1394
    op_ldst(l##width);                                                        \
1395
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1396
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1397
}
1398

    
1399
#define GEN_LDXF(width, opc2, opc3)                                           \
1400
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
1401
{                                                                             \
1402
    if (rA(ctx->opcode) == 0) {                                               \
1403
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1404
    } else {                                                                  \
1405
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1406
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1407
        gen_op_add();                                                         \
1408
    }                                                                         \
1409
    op_ldst(l##width);                                                        \
1410
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1411
}
1412

    
1413
#define GEN_LDFS(width, op)                                                   \
1414
OP_LD_TABLE(width);                                                           \
1415
GEN_LDF(width, op | 0x20);                                                    \
1416
GEN_LDUF(width, op | 0x21);                                                   \
1417
GEN_LDUXF(width, op | 0x01);                                                  \
1418
GEN_LDXF(width, 0x17, op | 0x00)
1419

    
1420
/* lfd lfdu lfdux lfdx */
1421
GEN_LDFS(fd, 0x12);
1422
/* lfs lfsu lfsux lfsx */
1423
GEN_LDFS(fs, 0x10);
1424

    
1425
/***                         Floating-point store                          ***/
1426
#define GEN_STF(width, opc)                                                   \
1427
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
1428
{                                                                             \
1429
    uint32_t simm = SIMM(ctx->opcode);                                        \
1430
    if (rA(ctx->opcode) == 0) {                                               \
1431
        gen_op_set_T0(simm);                                                  \
1432
    } else {                                                                  \
1433
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1434
        if (simm != 0)                                                        \
1435
            gen_op_addi(simm);                                                \
1436
    }                                                                         \
1437
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1438
    op_ldst(st##width);                                                       \
1439
}
1440

    
1441
#define GEN_STUF(width, opc)                                                  \
1442
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
1443
{                                                                             \
1444
    uint32_t simm = SIMM(ctx->opcode);                                        \
1445
    if (rA(ctx->opcode) == 0) {                                               \
1446
        RET_INVAL();                                                          \
1447
    }                                                                         \
1448
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1449
    if (simm != 0)                                                            \
1450
        gen_op_addi(simm);                                                    \
1451
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1452
    op_ldst(st##width);                                                       \
1453
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1454
}
1455

    
1456
#define GEN_STUXF(width, opc)                                                 \
1457
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
1458
{                                                                             \
1459
    if (rA(ctx->opcode) == 0) {                                               \
1460
        RET_INVAL();                                                          \
1461
    }                                                                         \
1462
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1463
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1464
    gen_op_add();                                                             \
1465
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1466
    op_ldst(st##width);                                                       \
1467
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1468
}
1469

    
1470
#define GEN_STXF(width, opc2, opc3)                                           \
1471
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
1472
{                                                                             \
1473
    if (rA(ctx->opcode) == 0) {                                               \
1474
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1475
    } else {                                                                  \
1476
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1477
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1478
        gen_op_add();                                                         \
1479
    }                                                                         \
1480
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1481
    op_ldst(st##width);                                                       \
1482
}
1483

    
1484
#define GEN_STFS(width, op)                                                   \
1485
OP_ST_TABLE(width);                                                           \
1486
GEN_STF(width, op | 0x20);                                                    \
1487
GEN_STUF(width, op | 0x21);                                                   \
1488
GEN_STUXF(width, op | 0x01);                                                  \
1489
GEN_STXF(width, 0x17, op | 0x00)
1490

    
1491
/* stfd stfdu stfdux stfdx */
1492
GEN_STFS(fd, 0x16);
1493
/* stfs stfsu stfsux stfsx */
1494
GEN_STFS(fs, 0x14);
1495

    
1496
/* Optional: */
1497
/* stfiwx */
1498
GEN_HANDLER(stfiwx, 0x1F, 0x17, 0x1E, 0x00000001, PPC_FLOAT)
1499
{
1500
    RET_INVAL();
1501
}
1502

    
1503
/***                                Branch                                 ***/
1504

    
1505
/* b ba bl bla */
1506
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1507
{
1508
    uint32_t li = s_ext24(LI(ctx->opcode)), target;
1509

    
1510
    gen_op_update_tb(ctx->tb_offset);
1511
    gen_op_update_decr(ctx->decr_offset);
1512
    gen_op_process_exceptions((uint32_t)ctx->nip - 4);
1513
    if (AA(ctx->opcode) == 0)
1514
        target = (uint32_t)ctx->nip + li - 4;
1515
    else
1516
        target = li;
1517
    if (LK(ctx->opcode)) {
1518
        gen_op_setlr((uint32_t)ctx->nip);
1519
    }
1520
    gen_op_b((long)ctx->tb, target);
1521
    ctx->exception = EXCP_BRANCH;
1522
}
1523

    
1524
#define BCOND_IM  0
1525
#define BCOND_LR  1
1526
#define BCOND_CTR 2
1527

    
1528
static inline void gen_bcond(DisasContext *ctx, int type) 
1529
{                                                                             
1530
    uint32_t target = 0;
1531
    uint32_t bo = BO(ctx->opcode);                                            
1532
    uint32_t bi = BI(ctx->opcode);                                            
1533
    uint32_t mask;                                                            
1534
    uint32_t li;
1535

    
1536
    gen_op_update_tb(ctx->tb_offset);                                         
1537
    gen_op_update_decr(ctx->decr_offset);                                     
1538
    gen_op_process_exceptions((uint32_t)ctx->nip - 4);                        
1539

    
1540
    if ((bo & 0x4) == 0)
1541
        gen_op_dec_ctr();                                                     
1542
    switch(type) {
1543
    case BCOND_IM:
1544
        li = s_ext16(BD(ctx->opcode));
1545
        if (AA(ctx->opcode) == 0) {
1546
            target = (uint32_t)ctx->nip + li - 4;
1547
        } else {
1548
            target = li;
1549
        }
1550
        break;
1551
    case BCOND_CTR:
1552
        gen_op_movl_T1_ctr();
1553
        break;
1554
    default:
1555
    case BCOND_LR:
1556
        gen_op_movl_T1_lr();
1557
        break;
1558
    }
1559
    if (LK(ctx->opcode)) {                                        
1560
        gen_op_setlr((uint32_t)ctx->nip);
1561
    }
1562
    if (bo & 0x10) {
1563
        /* No CR condition */                                                 
1564
        switch (bo & 0x6) {                                                   
1565
        case 0:                                                               
1566
            gen_op_test_ctr();
1567
            break;
1568
        case 2:                                                               
1569
            gen_op_test_ctrz();
1570
            break;                                                            
1571
        default:
1572
        case 4:                                                               
1573
        case 6:                                                               
1574
            if (type == BCOND_IM) {
1575
                gen_op_b((long)ctx->tb, target);
1576
            } else {
1577
                gen_op_b_T1();
1578
                break;
1579
            }
1580
            goto no_test;
1581
        }
1582
    } else {                                                                  
1583
        mask = 1 << (3 - (bi & 0x03));                                        
1584
        gen_op_load_crf_T0(bi >> 2);                                          
1585
        if (bo & 0x8) {                                                       
1586
            switch (bo & 0x6) {                                               
1587
            case 0:                                                           
1588
                gen_op_test_ctr_true(mask);
1589
                break;                                                        
1590
            case 2:                                                           
1591
                gen_op_test_ctrz_true(mask);
1592
                break;                                                        
1593
            default:                                                          
1594
            case 4:                                                           
1595
            case 6:                                                           
1596
                gen_op_test_true(mask);
1597
                break;                                                        
1598
            }                                                                 
1599
        } else {                                                              
1600
            switch (bo & 0x6) {                                               
1601
            case 0:                                                           
1602
                gen_op_test_ctr_false(mask);
1603
                break;                                                        
1604
            case 2:                                                           
1605
                gen_op_test_ctrz_false(mask);
1606
                break;                                                        
1607
            default:
1608
            case 4:                                                           
1609
            case 6:                                                           
1610
                gen_op_test_false(mask);
1611
                break;                                                        
1612
            }                                                                 
1613
        }                                                                     
1614
    }                                                                         
1615
    if (type == BCOND_IM) {
1616
        gen_op_btest((long)ctx->tb, target, (uint32_t)ctx->nip);
1617
    } else {
1618
        gen_op_btest_T1((uint32_t)ctx->nip);
1619
    }
1620
 no_test:
1621
    ctx->exception = EXCP_BRANCH;                                             
1622
}
1623

    
1624
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1625
{                                                                             
1626
    gen_bcond(ctx, BCOND_IM);
1627
}
1628

    
1629
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
1630
{                                                                             
1631
    gen_bcond(ctx, BCOND_CTR);
1632
}
1633

    
1634
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
1635
{                                                                             
1636
    gen_bcond(ctx, BCOND_LR);
1637
}
1638

    
1639
/***                      Condition register logical                       ***/
1640
#define GEN_CRLOGIC(op, opc)                                                  \
1641
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
1642
{                                                                             \
1643
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
1644
    gen_op_getbit_T0(3 - (crbA(ctx->opcode) & 0x03));                         \
1645
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
1646
    gen_op_getbit_T1(3 - (crbB(ctx->opcode) & 0x03));                         \
1647
    gen_op_##op();                                                            \
1648
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
1649
    gen_op_setcrfbit(~(1 << (3 - (crbD(ctx->opcode) & 0x03))),                \
1650
                     3 - (crbD(ctx->opcode) & 0x03));                         \
1651
    gen_op_store_T1_crf(crbD(ctx->opcode) >> 2);                              \
1652
}
1653

    
1654
/* crand */
1655
GEN_CRLOGIC(and, 0x08)
1656
/* crandc */
1657
GEN_CRLOGIC(andc, 0x04)
1658
/* creqv */
1659
GEN_CRLOGIC(eqv, 0x09)
1660
/* crnand */
1661
GEN_CRLOGIC(nand, 0x07)
1662
/* crnor */
1663
GEN_CRLOGIC(nor, 0x01)
1664
/* cror */
1665
GEN_CRLOGIC(or, 0x0E)
1666
/* crorc */
1667
GEN_CRLOGIC(orc, 0x0D)
1668
/* crxor */
1669
GEN_CRLOGIC(xor, 0x06)
1670
/* mcrf */
1671
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
1672
{
1673
    gen_op_load_crf_T0(crfS(ctx->opcode));
1674
    gen_op_store_T0_crf(crfD(ctx->opcode));
1675
}
1676

    
1677
/***                           System linkage                              ***/
1678
/* rfi (supervisor only) */
1679
GEN_HANDLER(rfi, 0x13, 0x12, 0xFF, 0x03FF8001, PPC_FLOW)
1680
{
1681
#if defined(CONFIG_USER_ONLY)
1682
    RET_PRIVOPC();
1683
#else
1684
    /* Restore CPU state */
1685
    if (!ctx->supervisor) {
1686
        RET_PRIVOPC();
1687
    }
1688
    gen_op_rfi();
1689
    ctx->exception = EXCP_RFI;
1690
#endif
1691
}
1692

    
1693
/* sc */
1694
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFFFFD, PPC_FLOW)
1695
{
1696
#if defined(CONFIG_USER_ONLY)
1697
    gen_op_queue_exception(EXCP_SYSCALL_USER);
1698
#else
1699
    gen_op_queue_exception(EXCP_SYSCALL);
1700
#endif
1701
    ctx->exception = EXCP_SYSCALL;
1702
}
1703

    
1704
/***                                Trap                                   ***/
1705
/* tw */
1706
GEN_HANDLER(tw, 0x1F, 0x04, 0xFF, 0x00000001, PPC_FLOW)
1707
{
1708
    gen_op_load_gpr_T0(rA(ctx->opcode));
1709
    gen_op_load_gpr_T1(rB(ctx->opcode));
1710
    gen_op_tw(TO(ctx->opcode));
1711
}
1712

    
1713
/* twi */
1714
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1715
{
1716
    gen_op_load_gpr_T0(rA(ctx->opcode));
1717
#if 0
1718
    printf("%s: param=0x%04x T0=0x%04x\n", __func__,
1719
           SIMM(ctx->opcode), TO(ctx->opcode));
1720
#endif
1721
    gen_op_twi(SIMM(ctx->opcode), TO(ctx->opcode));
1722
}
1723

    
1724
/***                          Processor control                            ***/
1725
static inline int check_spr_access (int spr, int rw, int supervisor)
1726
{
1727
    uint32_t rights = spr_access[spr >> 1] >> (4 * (spr & 1));
1728

    
1729
#if 0
1730
    if (spr != LR && spr != CTR) {
1731
    if (loglevel > 0) {
1732
        fprintf(logfile, "%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1733
                SPR_ENCODE(spr), supervisor, rw, rights,
1734
                (rights >> ((2 * supervisor) + rw)) & 1);
1735
    } else {
1736
        printf("%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1737
               SPR_ENCODE(spr), supervisor, rw, rights,
1738
               (rights >> ((2 * supervisor) + rw)) & 1);
1739
    }
1740
    }
1741
#endif
1742
    if (rights == 0)
1743
        return -1;
1744
    rights = rights >> (2 * supervisor);
1745
    rights = rights >> rw;
1746

    
1747
    return rights & 1;
1748
}
1749

    
1750
/* mcrxr */
1751
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
1752
{
1753
    gen_op_load_xer_cr();
1754
    gen_op_store_T0_crf(crfD(ctx->opcode));
1755
    gen_op_clear_xer_cr();
1756
}
1757

    
1758
/* mfcr */
1759
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x001FF801, PPC_MISC)
1760
{
1761
    gen_op_load_cr();
1762
    gen_op_store_T0_gpr(rD(ctx->opcode));
1763
}
1764

    
1765
/* mfmsr */
1766
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
1767
{
1768
#if defined(CONFIG_USER_ONLY)
1769
    RET_PRIVREG();
1770
#else
1771
    if (!ctx->supervisor) {
1772
        RET_PRIVREG();
1773
    }
1774
    gen_op_load_msr();
1775
    gen_op_store_T0_gpr(rD(ctx->opcode));
1776
#endif
1777
}
1778

    
1779
/* mfspr */
1780
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
1781
{
1782
    uint32_t sprn = SPR(ctx->opcode);
1783

    
1784
#if defined(CONFIG_USER_ONLY)
1785
    switch (check_spr_access(sprn, 0, 0))
1786
#else
1787
    switch (check_spr_access(sprn, 0, ctx->supervisor))
1788
#endif
1789
    {
1790
    case -1:
1791
        RET_EXCP(EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
1792
        break;
1793
    case 0:
1794
        RET_PRIVREG();
1795
        break;
1796
    default:
1797
        break;
1798
        }
1799
    switch (sprn) {
1800
    case XER:
1801
        gen_op_load_xer();
1802
        break;
1803
    case LR:
1804
        gen_op_load_lr();
1805
        break;
1806
    case CTR:
1807
        gen_op_load_ctr();
1808
        break;
1809
    case IBAT0U:
1810
        gen_op_load_ibat(0, 0);
1811
        break;
1812
    case IBAT1U:
1813
        gen_op_load_ibat(0, 1);
1814
        break;
1815
    case IBAT2U:
1816
        gen_op_load_ibat(0, 2);
1817
        break;
1818
    case IBAT3U:
1819
        gen_op_load_ibat(0, 3);
1820
        break;
1821
    case IBAT4U:
1822
        gen_op_load_ibat(0, 4);
1823
        break;
1824
    case IBAT5U:
1825
        gen_op_load_ibat(0, 5);
1826
        break;
1827
    case IBAT6U:
1828
        gen_op_load_ibat(0, 6);
1829
        break;
1830
    case IBAT7U:
1831
        gen_op_load_ibat(0, 7);
1832
        break;
1833
    case IBAT0L:
1834
        gen_op_load_ibat(1, 0);
1835
        break;
1836
    case IBAT1L:
1837
        gen_op_load_ibat(1, 1);
1838
        break;
1839
    case IBAT2L:
1840
        gen_op_load_ibat(1, 2);
1841
        break;
1842
    case IBAT3L:
1843
        gen_op_load_ibat(1, 3);
1844
        break;
1845
    case IBAT4L:
1846
        gen_op_load_ibat(1, 4);
1847
        break;
1848
    case IBAT5L:
1849
        gen_op_load_ibat(1, 5);
1850
        break;
1851
    case IBAT6L:
1852
        gen_op_load_ibat(1, 6);
1853
        break;
1854
    case IBAT7L:
1855
        gen_op_load_ibat(1, 7);
1856
        break;
1857
    case DBAT0U:
1858
        gen_op_load_dbat(0, 0);
1859
        break;
1860
    case DBAT1U:
1861
        gen_op_load_dbat(0, 1);
1862
        break;
1863
    case DBAT2U:
1864
        gen_op_load_dbat(0, 2);
1865
        break;
1866
    case DBAT3U:
1867
        gen_op_load_dbat(0, 3);
1868
        break;
1869
    case DBAT4U:
1870
        gen_op_load_dbat(0, 4);
1871
        break;
1872
    case DBAT5U:
1873
        gen_op_load_dbat(0, 5);
1874
        break;
1875
    case DBAT6U:
1876
        gen_op_load_dbat(0, 6);
1877
        break;
1878
    case DBAT7U:
1879
        gen_op_load_dbat(0, 7);
1880
        break;
1881
    case DBAT0L:
1882
        gen_op_load_dbat(1, 0);
1883
        break;
1884
    case DBAT1L:
1885
        gen_op_load_dbat(1, 1);
1886
        break;
1887
    case DBAT2L:
1888
        gen_op_load_dbat(1, 2);
1889
        break;
1890
    case DBAT3L:
1891
        gen_op_load_dbat(1, 3);
1892
        break;
1893
    case DBAT4L:
1894
        gen_op_load_dbat(1, 4);
1895
        break;
1896
    case DBAT5L:
1897
        gen_op_load_dbat(1, 5);
1898
        break;
1899
    case DBAT6L:
1900
        gen_op_load_dbat(1, 6);
1901
        break;
1902
    case DBAT7L:
1903
        gen_op_load_dbat(1, 7);
1904
        break;
1905
    case SDR1:
1906
        gen_op_load_sdr1();
1907
        break;
1908
    case V_TBL:
1909
        gen_op_update_tb(ctx->tb_offset);
1910
        ctx->tb_offset = 0;
1911
        /* TBL is still in T0 */
1912
        break;
1913
    case V_TBU:
1914
        gen_op_update_tb(ctx->tb_offset);
1915
        ctx->tb_offset = 0;
1916
        gen_op_load_tb(1);
1917
        break;
1918
    case DECR:
1919
        gen_op_update_decr(ctx->decr_offset);
1920
        ctx->decr_offset = 0;
1921
        /* decr is still in T0 */
1922
        break;
1923
    default:
1924
        gen_op_load_spr(sprn);
1925
        break;
1926
    }
1927
    gen_op_store_T0_gpr(rD(ctx->opcode));
1928
}
1929

    
1930
/* mftb */
1931
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MISC)
1932
{
1933
    uint32_t sprn = SPR(ctx->opcode);
1934

    
1935
        /* We need to update the time base before reading it */
1936
    switch (sprn) {
1937
    case V_TBL:
1938
        gen_op_update_tb(ctx->tb_offset);
1939
        /* TBL is still in T0 */
1940
        break;
1941
    case V_TBU:
1942
        gen_op_update_tb(ctx->tb_offset);
1943
        gen_op_load_tb(1);
1944
        break;
1945
    default:
1946
        RET_INVAL();
1947
        break;
1948
    }
1949
    ctx->tb_offset = 0;
1950
    gen_op_store_T0_gpr(rD(ctx->opcode));
1951
}
1952

    
1953
/* mtcrf */
1954
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00100801, PPC_MISC)
1955
{
1956
    gen_op_load_gpr_T0(rS(ctx->opcode));
1957
    gen_op_store_cr(CRM(ctx->opcode));
1958
}
1959

    
1960
/* mtmsr */
1961
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
1962
{
1963
#if defined(CONFIG_USER_ONLY)
1964
    RET_PRIVREG();
1965
#else
1966
    if (!ctx->supervisor) {
1967
        RET_PRIVREG();
1968
    }
1969
    gen_op_load_gpr_T0(rS(ctx->opcode));
1970
    gen_op_store_msr();
1971
    /* Must stop the translation as machine state (may have) changed */
1972
    ctx->exception = EXCP_MTMSR;
1973
#endif
1974
}
1975

    
1976
/* mtspr */
1977
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
1978
{
1979
    uint32_t sprn = SPR(ctx->opcode);
1980

    
1981
#if 0
1982
    if (loglevel > 0) {
1983
        fprintf(logfile, "MTSPR %d src=%d (%d)\n", SPR_ENCODE(sprn),
1984
                rS(ctx->opcode), sprn);
1985
    }
1986
#endif
1987
#if defined(CONFIG_USER_ONLY)
1988
    switch (check_spr_access(sprn, 1, 0))
1989
#else
1990
    switch (check_spr_access(sprn, 1, ctx->supervisor))
1991
#endif
1992
    {
1993
    case -1:
1994
        RET_EXCP(EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
1995
        break;
1996
    case 0:
1997
        RET_PRIVREG();
1998
        break;
1999
    default:
2000
        break;
2001
    }
2002
    gen_op_load_gpr_T0(rS(ctx->opcode));
2003
    switch (sprn) {
2004
    case XER:
2005
        gen_op_store_xer();
2006
        break;
2007
    case LR:
2008
        gen_op_andi_(~0x03);
2009
        gen_op_store_lr();
2010
        break;
2011
    case CTR:
2012
        gen_op_store_ctr();
2013
        break;
2014
    case IBAT0U:
2015
        gen_op_store_ibat(0, 0);
2016
        gen_op_tlbia();
2017
        break;
2018
    case IBAT1U:
2019
        gen_op_store_ibat(0, 1);
2020
        gen_op_tlbia();
2021
        break;
2022
    case IBAT2U:
2023
        gen_op_store_ibat(0, 2);
2024
        gen_op_tlbia();
2025
        break;
2026
    case IBAT3U:
2027
        gen_op_store_ibat(0, 3);
2028
        gen_op_tlbia();
2029
        break;
2030
    case IBAT4U:
2031
        gen_op_store_ibat(0, 4);
2032
        gen_op_tlbia();
2033
        break;
2034
    case IBAT5U:
2035
        gen_op_store_ibat(0, 5);
2036
        gen_op_tlbia();
2037
        break;
2038
    case IBAT6U:
2039
        gen_op_store_ibat(0, 6);
2040
        gen_op_tlbia();
2041
        break;
2042
    case IBAT7U:
2043
        gen_op_store_ibat(0, 7);
2044
        gen_op_tlbia();
2045
        break;
2046
    case IBAT0L:
2047
        gen_op_store_ibat(1, 0);
2048
        gen_op_tlbia();
2049
        break;
2050
    case IBAT1L:
2051
        gen_op_store_ibat(1, 1);
2052
        gen_op_tlbia();
2053
        break;
2054
    case IBAT2L:
2055
        gen_op_store_ibat(1, 2);
2056
        gen_op_tlbia();
2057
        break;
2058
    case IBAT3L:
2059
        gen_op_store_ibat(1, 3);
2060
        gen_op_tlbia();
2061
        break;
2062
    case IBAT4L:
2063
        gen_op_store_ibat(1, 4);
2064
        gen_op_tlbia();
2065
        break;
2066
    case IBAT5L:
2067
        gen_op_store_ibat(1, 5);
2068
        gen_op_tlbia();
2069
        break;
2070
    case IBAT6L:
2071
        gen_op_store_ibat(1, 6);
2072
        gen_op_tlbia();
2073
        break;
2074
    case IBAT7L:
2075
        gen_op_store_ibat(1, 7);
2076
        gen_op_tlbia();
2077
        break;
2078
    case DBAT0U:
2079
        gen_op_store_dbat(0, 0);
2080
        gen_op_tlbia();
2081
        break;
2082
    case DBAT1U:
2083
        gen_op_store_dbat(0, 1);
2084
        gen_op_tlbia();
2085
        break;
2086
    case DBAT2U:
2087
        gen_op_store_dbat(0, 2);
2088
        gen_op_tlbia();
2089
        break;
2090
    case DBAT3U:
2091
        gen_op_store_dbat(0, 3);
2092
        gen_op_tlbia();
2093
        break;
2094
    case DBAT4U:
2095
        gen_op_store_dbat(0, 4);
2096
        gen_op_tlbia();
2097
        break;
2098
    case DBAT5U:
2099
        gen_op_store_dbat(0, 5);
2100
        gen_op_tlbia();
2101
        break;
2102
    case DBAT6U:
2103
        gen_op_store_dbat(0, 6);
2104
        gen_op_tlbia();
2105
        break;
2106
    case DBAT7U:
2107
        gen_op_store_dbat(0, 7);
2108
        gen_op_tlbia();
2109
        break;
2110
    case DBAT0L:
2111
        gen_op_store_dbat(1, 0);
2112
        gen_op_tlbia();
2113
        break;
2114
    case DBAT1L:
2115
        gen_op_store_dbat(1, 1);
2116
        gen_op_tlbia();
2117
        break;
2118
    case DBAT2L:
2119
        gen_op_store_dbat(1, 2);
2120
        gen_op_tlbia();
2121
        break;
2122
    case DBAT3L:
2123
        gen_op_store_dbat(1, 3);
2124
        gen_op_tlbia();
2125
        break;
2126
    case DBAT4L:
2127
        gen_op_store_dbat(1, 4);
2128
        gen_op_tlbia();
2129
        break;
2130
    case DBAT5L:
2131
        gen_op_store_dbat(1, 5);
2132
        gen_op_tlbia();
2133
        break;
2134
    case DBAT6L:
2135
        gen_op_store_dbat(1, 6);
2136
        gen_op_tlbia();
2137
        break;
2138
    case DBAT7L:
2139
        gen_op_store_dbat(1, 7);
2140
        gen_op_tlbia();
2141
        break;
2142
    case SDR1:
2143
        gen_op_store_sdr1();
2144
        gen_op_tlbia();
2145
        break;
2146
    case O_TBL:
2147
        gen_op_store_tb(0);
2148
        ctx->tb_offset = 0;
2149
        break;
2150
    case O_TBU:
2151
        gen_op_store_tb(1);
2152
        ctx->tb_offset = 0;
2153
        break;
2154
    case DECR:
2155
        gen_op_store_decr();
2156
        ctx->decr_offset = 0;
2157
        break;
2158
    default:
2159
        gen_op_store_spr(sprn);
2160
        break;
2161
    }
2162
}
2163

    
2164
/***                         Cache management                              ***/
2165
/* For now, all those will be implemented as nop:
2166
 * this is valid, regarding the PowerPC specs...
2167
 * We just have to flush tb while invalidating instruction cache lines...
2168
 */
2169
/* dcbf */
2170
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03E00001, PPC_CACHE)
2171
{
2172
}
2173

    
2174
/* dcbi (Supervisor only) */
2175
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
2176
{
2177
#if !defined(CONFIG_USER_ONLY)
2178
    if (!ctx->supervisor)
2179
#endif
2180
    {
2181
        RET_PRIVOPC();
2182
    }
2183
}
2184

    
2185
/* dcdst */
2186
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
2187
{
2188
}
2189

    
2190
/* dcbt */
2191
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x03E00001, PPC_CACHE)
2192
{
2193
}
2194

    
2195
/* dcbtst */
2196
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE)
2197
{
2198
}
2199

    
2200
/* dcbz */
2201
#if defined(CONFIG_USER_ONLY)
2202
#define op_dcbz() gen_op_dcbz_raw()
2203
#else
2204
#define op_dcbz() (*gen_op_dcbz[ctx->mem_idx])()
2205
static GenOpFunc *gen_op_dcbz[] = {
2206
    &gen_op_dcbz_user,
2207
    &gen_op_dcbz_kernel,
2208
};
2209
#endif
2210

    
2211
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE)
2212
{
2213
    if (rA(ctx->opcode) == 0) {
2214
        gen_op_load_gpr_T0(rB(ctx->opcode));
2215
    } else {
2216
        gen_op_load_gpr_T0(rA(ctx->opcode));
2217
        gen_op_load_gpr_T1(rB(ctx->opcode));
2218
        gen_op_add();
2219
    }
2220
    op_dcbz();
2221
}
2222

    
2223
/* icbi */
2224
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
2225
{
2226
    if (rA(ctx->opcode) == 0) {
2227
        gen_op_load_gpr_T0(rB(ctx->opcode));
2228
    } else {
2229
        gen_op_load_gpr_T0(rA(ctx->opcode));
2230
        gen_op_load_gpr_T1(rB(ctx->opcode));
2231
        gen_op_add();
2232
    }
2233
    gen_op_icbi();
2234
}
2235

    
2236
/* Optional: */
2237
/* dcba */
2238
GEN_HANDLER(dcba, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE_OPT)
2239
{
2240
}
2241

    
2242
/***                    Segment register manipulation                      ***/
2243
/* Supervisor only: */
2244
/* mfsr */
2245
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
2246
{
2247
#if defined(CONFIG_USER_ONLY)
2248
    RET_PRIVREG();
2249
#else
2250
    if (!ctx->supervisor) {
2251
        RET_PRIVREG();
2252
    }
2253
    gen_op_load_sr(SR(ctx->opcode));
2254
    gen_op_store_T0_gpr(rD(ctx->opcode));
2255
#endif
2256
}
2257

    
2258
/* mfsrin */
2259
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
2260
{
2261
#if defined(CONFIG_USER_ONLY)
2262
    RET_PRIVREG();
2263
#else
2264
    if (!ctx->supervisor) {
2265
        RET_PRIVREG();
2266
    }
2267
    gen_op_load_gpr_T1(rB(ctx->opcode));
2268
    gen_op_load_srin();
2269
    gen_op_store_T0_gpr(rD(ctx->opcode));
2270
#endif
2271
}
2272

    
2273
/* mtsr */
2274
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x02, 0x0010F801, PPC_SEGMENT)
2275
{
2276
#if defined(CONFIG_USER_ONLY)
2277
    RET_PRIVREG();
2278
#else
2279
    if (!ctx->supervisor) {
2280
        RET_PRIVREG();
2281
    }
2282
    gen_op_load_gpr_T0(rS(ctx->opcode));
2283
    gen_op_store_sr(SR(ctx->opcode));
2284
    gen_op_tlbia();
2285
#endif
2286
}
2287

    
2288
/* mtsrin */
2289
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
2290
{
2291
#if defined(CONFIG_USER_ONLY)
2292
    RET_PRIVREG();
2293
#else
2294
    if (!ctx->supervisor) {
2295
        RET_PRIVREG();
2296
    }
2297
    gen_op_load_gpr_T0(rS(ctx->opcode));
2298
    gen_op_load_gpr_T1(rB(ctx->opcode));
2299
    gen_op_store_srin();
2300
    gen_op_tlbia();
2301
#endif
2302
}
2303

    
2304
/***                      Lookaside buffer management                      ***/
2305
/* Optional & supervisor only: */
2306
/* tlbia */
2307
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_OPT)
2308
{
2309
#if defined(CONFIG_USER_ONLY)
2310
    RET_PRIVOPC();
2311
#else
2312
    if (!ctx->supervisor) {
2313
        RET_PRIVOPC();
2314
    }
2315
    gen_op_tlbia();
2316
#endif
2317
}
2318

    
2319
/* tlbie */
2320
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM)
2321
{
2322
#if defined(CONFIG_USER_ONLY)
2323
    RET_PRIVOPC();
2324
#else
2325
    if (!ctx->supervisor) {
2326
        RET_PRIVOPC();
2327
    }
2328
    gen_op_load_gpr_T0(rB(ctx->opcode));
2329
    gen_op_tlbie();
2330
#endif
2331
}
2332

    
2333
/* tlbsync */
2334
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFFC01, PPC_MEM)
2335
{
2336
#if defined(CONFIG_USER_ONLY)
2337
    RET_PRIVOPC();
2338
#else
2339
    if (!ctx->supervisor) {
2340
        RET_PRIVOPC();
2341
    }
2342
    /* This has no effect: it should ensure that all previous
2343
     * tlbie have completed
2344
     */
2345
#endif
2346
}
2347

    
2348
/***                              External control                         ***/
2349
/* Optional: */
2350
/* eciwx */
2351
#if defined(CONFIG_USER_ONLY)
2352
#define op_eciwx() gen_op_eciwx_raw()
2353
#define op_ecowx() gen_op_ecowx_raw()
2354
#else
2355
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
2356
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
2357
static GenOpFunc *gen_op_eciwx[] = {
2358
    &gen_op_eciwx_user,
2359
    &gen_op_eciwx_kernel,
2360
};
2361
static GenOpFunc *gen_op_ecowx[] = {
2362
    &gen_op_ecowx_user,
2363
    &gen_op_ecowx_kernel,
2364
};
2365
#endif
2366

    
2367
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
2368
{
2369
    /* Should check EAR[E] & alignment ! */
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_eciwx();
2378
    gen_op_store_T0_gpr(rD(ctx->opcode));
2379
}
2380

    
2381
/* ecowx */
2382
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
2383
{
2384
    /* Should check EAR[E] & alignment ! */
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
    gen_op_load_gpr_T2(rS(ctx->opcode));
2393
    op_ecowx();
2394
}
2395

    
2396
/* End opcode list */
2397
GEN_OPCODE_MARK(end);
2398

    
2399
/*****************************************************************************/
2400
#include <stdlib.h>
2401
#include <string.h>
2402

    
2403
int fflush (FILE *stream);
2404

    
2405
/* Main ppc opcodes table:
2406
 * at init, all opcodes are invalids
2407
 */
2408
static opc_handler_t *ppc_opcodes[0x40];
2409

    
2410
/* Opcode types */
2411
enum {
2412
    PPC_DIRECT   = 0, /* Opcode routine        */
2413
    PPC_INDIRECT = 1, /* Indirect opcode table */
2414
};
2415

    
2416
static inline int is_indirect_opcode (void *handler)
2417
{
2418
    return ((unsigned long)handler & 0x03) == PPC_INDIRECT;
2419
}
2420

    
2421
static inline opc_handler_t **ind_table(void *handler)
2422
{
2423
    return (opc_handler_t **)((unsigned long)handler & ~3);
2424
}
2425

    
2426
/* Instruction table creation */
2427
/* Opcodes tables creation */
2428
static void fill_new_table (opc_handler_t **table, int len)
2429
{
2430
    int i;
2431

    
2432
    for (i = 0; i < len; i++)
2433
        table[i] = &invalid_handler;
2434
}
2435

    
2436
static int create_new_table (opc_handler_t **table, unsigned char idx)
2437
{
2438
    opc_handler_t **tmp;
2439

    
2440
    tmp = malloc(0x20 * sizeof(opc_handler_t));
2441
    if (tmp == NULL)
2442
        return -1;
2443
    fill_new_table(tmp, 0x20);
2444
    table[idx] = (opc_handler_t *)((unsigned long)tmp | PPC_INDIRECT);
2445

    
2446
    return 0;
2447
}
2448

    
2449
static int insert_in_table (opc_handler_t **table, unsigned char idx,
2450
                            opc_handler_t *handler)
2451
{
2452
    if (table[idx] != &invalid_handler)
2453
        return -1;
2454
    table[idx] = handler;
2455

    
2456
    return 0;
2457
}
2458

    
2459
static int register_direct_insn (opc_handler_t **ppc_opcodes,
2460
                                 unsigned char idx, opc_handler_t *handler)
2461
{
2462
    if (insert_in_table(ppc_opcodes, idx, handler) < 0) {
2463
        printf("*** ERROR: opcode %02x already assigned in main "
2464
                "opcode table\n", idx);
2465
        return -1;
2466
    }
2467

    
2468
    return 0;
2469
}
2470

    
2471
static int register_ind_in_table (opc_handler_t **table,
2472
                                  unsigned char idx1, unsigned char idx2,
2473
                                  opc_handler_t *handler)
2474
{
2475
    if (table[idx1] == &invalid_handler) {
2476
        if (create_new_table(table, idx1) < 0) {
2477
            printf("*** ERROR: unable to create indirect table "
2478
                    "idx=%02x\n", idx1);
2479
            return -1;
2480
        }
2481
    } else {
2482
        if (!is_indirect_opcode(table[idx1])) {
2483
            printf("*** ERROR: idx %02x already assigned to a direct "
2484
                    "opcode\n", idx1);
2485
            return -1;
2486
        }
2487
    }
2488
    if (handler != NULL &&
2489
        insert_in_table(ind_table(table[idx1]), idx2, handler) < 0) {
2490
        printf("*** ERROR: opcode %02x already assigned in "
2491
                "opcode table %02x\n", idx2, idx1);
2492
        return -1;
2493
    }
2494

    
2495
    return 0;
2496
}
2497

    
2498
static int register_ind_insn (opc_handler_t **ppc_opcodes,
2499
                              unsigned char idx1, unsigned char idx2,
2500
                               opc_handler_t *handler)
2501
{
2502
    int ret;
2503

    
2504
    ret = register_ind_in_table(ppc_opcodes, idx1, idx2, handler);
2505

    
2506
    return ret;
2507
}
2508

    
2509
static int register_dblind_insn (opc_handler_t **ppc_opcodes, 
2510
                                 unsigned char idx1, unsigned char idx2,
2511
                                  unsigned char idx3, opc_handler_t *handler)
2512
{
2513
    if (register_ind_in_table(ppc_opcodes, idx1, idx2, NULL) < 0) {
2514
        printf("*** ERROR: unable to join indirect table idx "
2515
                "[%02x-%02x]\n", idx1, idx2);
2516
        return -1;
2517
    }
2518
    if (register_ind_in_table(ind_table(ppc_opcodes[idx1]), idx2, idx3,
2519
                              handler) < 0) {
2520
        printf("*** ERROR: unable to insert opcode "
2521
                "[%02x-%02x-%02x]\n", idx1, idx2, idx3);
2522
        return -1;
2523
    }
2524

    
2525
    return 0;
2526
}
2527

    
2528
static int register_insn (opc_handler_t **ppc_opcodes, opcode_t *insn)
2529
{
2530
    if (insn->opc2 != 0xFF) {
2531
        if (insn->opc3 != 0xFF) {
2532
            if (register_dblind_insn(ppc_opcodes, insn->opc1, insn->opc2,
2533
                                     insn->opc3, &insn->handler) < 0)
2534
                return -1;
2535
        } else {
2536
            if (register_ind_insn(ppc_opcodes, insn->opc1,
2537
                                  insn->opc2, &insn->handler) < 0)
2538
                return -1;
2539
        }
2540
    } else {
2541
        if (register_direct_insn(ppc_opcodes, insn->opc1, &insn->handler) < 0)
2542
            return -1;
2543
    }
2544

    
2545
    return 0;
2546
}
2547

    
2548
static int test_opcode_table (opc_handler_t **table, int len)
2549
{
2550
    int i, count, tmp;
2551

    
2552
    for (i = 0, count = 0; i < len; i++) {
2553
        /* Consistency fixup */
2554
        if (table[i] == NULL)
2555
            table[i] = &invalid_handler;
2556
        if (table[i] != &invalid_handler) {
2557
            if (is_indirect_opcode(table[i])) {
2558
                tmp = test_opcode_table(ind_table(table[i]), 0x20);
2559
                if (tmp == 0) {
2560
                    free(table[i]);
2561
                    table[i] = &invalid_handler;
2562
                } else {
2563
                    count++;
2564
                }
2565
            } else {
2566
                count++;
2567
            }
2568
        }
2569
    }
2570

    
2571
    return count;
2572
}
2573

    
2574
static void fix_opcode_tables (opc_handler_t **ppc_opcodes)
2575
{
2576
    if (test_opcode_table(ppc_opcodes, 0x40) == 0)
2577
        printf("*** WARNING: no opcode defined !\n");
2578
}
2579

    
2580
#define SPR_RIGHTS(rw, priv) (1 << ((2 * (priv)) + (rw)))
2581
#define SPR_UR SPR_RIGHTS(0, 0)
2582
#define SPR_UW SPR_RIGHTS(1, 0)
2583
#define SPR_SR SPR_RIGHTS(0, 1)
2584
#define SPR_SW SPR_RIGHTS(1, 1)
2585

    
2586
#define spr_set_rights(spr, rights)                            \
2587
do {                                                           \
2588
    spr_access[(spr) >> 1] |= ((rights) << (4 * ((spr) & 1))); \
2589
} while (0)
2590

    
2591
static void init_spr_rights (uint32_t pvr)
2592
{
2593
    /* XER    (SPR 1) */
2594
    spr_set_rights(XER,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2595
    /* LR     (SPR 8) */
2596
    spr_set_rights(LR,     SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2597
    /* CTR    (SPR 9) */
2598
    spr_set_rights(CTR,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2599
    /* TBL    (SPR 268) */
2600
    spr_set_rights(V_TBL,  SPR_UR | SPR_SR);
2601
    /* TBU    (SPR 269) */
2602
    spr_set_rights(V_TBU,  SPR_UR | SPR_SR);
2603
    /* DSISR  (SPR 18) */
2604
    spr_set_rights(DSISR,  SPR_SR | SPR_SW);
2605
    /* DAR    (SPR 19) */
2606
    spr_set_rights(DAR,    SPR_SR | SPR_SW);
2607
    /* DEC    (SPR 22) */
2608
    spr_set_rights(DECR,   SPR_SR | SPR_SW);
2609
    /* SDR1   (SPR 25) */
2610
    spr_set_rights(SDR1,   SPR_SR | SPR_SW);
2611
    /* SRR0   (SPR 26) */
2612
    spr_set_rights(SRR0,   SPR_SR | SPR_SW);
2613
    /* SRR1   (SPR 27) */
2614
    spr_set_rights(SRR1,   SPR_SR | SPR_SW);
2615
    /* SPRG0  (SPR 272) */
2616
    spr_set_rights(SPRG0,  SPR_SR | SPR_SW);
2617
    /* SPRG1  (SPR 273) */
2618
    spr_set_rights(SPRG1,  SPR_SR | SPR_SW);
2619
    /* SPRG2  (SPR 274) */
2620
    spr_set_rights(SPRG2,  SPR_SR | SPR_SW);
2621
    /* SPRG3  (SPR 275) */
2622
    spr_set_rights(SPRG3,  SPR_SR | SPR_SW);
2623
    /* ASR    (SPR 280) */
2624
    spr_set_rights(ASR,    SPR_SR | SPR_SW);
2625
    /* EAR    (SPR 282) */
2626
    spr_set_rights(EAR,    SPR_SR | SPR_SW);
2627
    /* TBL    (SPR 284) */
2628
    spr_set_rights(O_TBL,  SPR_SW);
2629
    /* TBU    (SPR 285) */
2630
    spr_set_rights(O_TBU,  SPR_SW);
2631
    /* PVR    (SPR 287) */
2632
    spr_set_rights(PVR,    SPR_SR);
2633
    /* IBAT0U (SPR 528) */
2634
    spr_set_rights(IBAT0U, SPR_SR | SPR_SW);
2635
    /* IBAT0L (SPR 529) */
2636
    spr_set_rights(IBAT0L, SPR_SR | SPR_SW);
2637
    /* IBAT1U (SPR 530) */
2638
    spr_set_rights(IBAT1U, SPR_SR | SPR_SW);
2639
    /* IBAT1L (SPR 531) */
2640
    spr_set_rights(IBAT1L, SPR_SR | SPR_SW);
2641
    /* IBAT2U (SPR 532) */
2642
    spr_set_rights(IBAT2U, SPR_SR | SPR_SW);
2643
    /* IBAT2L (SPR 533) */
2644
    spr_set_rights(IBAT2L, SPR_SR | SPR_SW);
2645
    /* IBAT3U (SPR 534) */
2646
    spr_set_rights(IBAT3U, SPR_SR | SPR_SW);
2647
    /* IBAT3L (SPR 535) */
2648
    spr_set_rights(IBAT3L, SPR_SR | SPR_SW);
2649
    /* DBAT0U (SPR 536) */
2650
    spr_set_rights(DBAT0U, SPR_SR | SPR_SW);
2651
    /* DBAT0L (SPR 537) */
2652
    spr_set_rights(DBAT0L, SPR_SR | SPR_SW);
2653
    /* DBAT1U (SPR 538) */
2654
    spr_set_rights(DBAT1U, SPR_SR | SPR_SW);
2655
    /* DBAT1L (SPR 539) */
2656
    spr_set_rights(DBAT1L, SPR_SR | SPR_SW);
2657
    /* DBAT2U (SPR 540) */
2658
    spr_set_rights(DBAT2U, SPR_SR | SPR_SW);
2659
    /* DBAT2L (SPR 541) */
2660
    spr_set_rights(DBAT2L, SPR_SR | SPR_SW);
2661
    /* DBAT3U (SPR 542) */
2662
    spr_set_rights(DBAT3U, SPR_SR | SPR_SW);
2663
    /* DBAT3L (SPR 543) */
2664
    spr_set_rights(DBAT3L, SPR_SR | SPR_SW);
2665
    /* DABR   (SPR 1013) */
2666
    spr_set_rights(DABR,   SPR_SR | SPR_SW);
2667
    /* FPECR  (SPR 1022) */
2668
    spr_set_rights(FPECR,  SPR_SR | SPR_SW);
2669
    /* PIR    (SPR 1023) */
2670
    spr_set_rights(PIR,    SPR_SR | SPR_SW);
2671
    /* Special registers for MPC740/745/750/755 (aka G3) & IBM 750 */
2672
    if ((pvr & 0xFFFF0000) == 0x00080000 ||
2673
        (pvr & 0xFFFF0000) == 0x70000000) {
2674
        /* HID0 */
2675
        spr_set_rights(SPR_ENCODE(1008), SPR_SR | SPR_SW);
2676
        /* HID1 */
2677
        spr_set_rights(SPR_ENCODE(1009), SPR_SR | SPR_SW);
2678
        /* IABR */
2679
        spr_set_rights(SPR_ENCODE(1010), SPR_SR | SPR_SW);
2680
        /* ICTC */
2681
        spr_set_rights(SPR_ENCODE(1019), SPR_SR | SPR_SW);
2682
        /* L2CR */
2683
        spr_set_rights(SPR_ENCODE(1017), SPR_SR | SPR_SW);
2684
        /* MMCR0 */
2685
        spr_set_rights(SPR_ENCODE(952), SPR_SR | SPR_SW);
2686
        /* MMCR1 */
2687
        spr_set_rights(SPR_ENCODE(956), SPR_SR | SPR_SW);
2688
        /* PMC1 */
2689
        spr_set_rights(SPR_ENCODE(953), SPR_SR | SPR_SW);
2690
        /* PMC2 */
2691
        spr_set_rights(SPR_ENCODE(954), SPR_SR | SPR_SW);
2692
        /* PMC3 */
2693
        spr_set_rights(SPR_ENCODE(957), SPR_SR | SPR_SW);
2694
        /* PMC4 */
2695
        spr_set_rights(SPR_ENCODE(958), SPR_SR | SPR_SW);
2696
        /* SIA */
2697
        spr_set_rights(SPR_ENCODE(955), SPR_SR | SPR_SW);
2698
        /* THRM1 */
2699
        spr_set_rights(SPR_ENCODE(1020), SPR_SR | SPR_SW);
2700
        /* THRM2 */
2701
        spr_set_rights(SPR_ENCODE(1021), SPR_SR | SPR_SW);
2702
        /* THRM3 */
2703
        spr_set_rights(SPR_ENCODE(1022), SPR_SR | SPR_SW);
2704
        /* UMMCR0 */
2705
        spr_set_rights(SPR_ENCODE(936), SPR_UR | SPR_UW);
2706
        /* UMMCR1 */
2707
        spr_set_rights(SPR_ENCODE(940), SPR_UR | SPR_UW);
2708
        /* UPMC1 */
2709
        spr_set_rights(SPR_ENCODE(937), SPR_UR | SPR_UW);
2710
        /* UPMC2 */
2711
        spr_set_rights(SPR_ENCODE(938), SPR_UR | SPR_UW);
2712
        /* UPMC3 */
2713
        spr_set_rights(SPR_ENCODE(941), SPR_UR | SPR_UW);
2714
        /* UPMC4 */
2715
        spr_set_rights(SPR_ENCODE(942), SPR_UR | SPR_UW);
2716
        /* USIA */
2717
        spr_set_rights(SPR_ENCODE(939), SPR_UR | SPR_UW);
2718
    }
2719
    /* MPC755 has special registers */
2720
    if (pvr == 0x00083100) {
2721
        /* SPRG4 */
2722
        spr_set_rights(SPRG4, SPR_SR | SPR_SW);
2723
        /* SPRG5 */
2724
        spr_set_rights(SPRG5, SPR_SR | SPR_SW);
2725
        /* SPRG6 */
2726
        spr_set_rights(SPRG6, SPR_SR | SPR_SW);
2727
        /* SPRG7 */
2728
        spr_set_rights(SPRG7, SPR_SR | SPR_SW);
2729
        /* IBAT4U */
2730
        spr_set_rights(IBAT4U, SPR_SR | SPR_SW);
2731
        /* IBAT4L */
2732
        spr_set_rights(IBAT4L, SPR_SR | SPR_SW);
2733
        /* IBAT5U */
2734
        spr_set_rights(IBAT5U, SPR_SR | SPR_SW);
2735
        /* IBAT5L */
2736
        spr_set_rights(IBAT5L, SPR_SR | SPR_SW);
2737
        /* IBAT6U */
2738
        spr_set_rights(IBAT6U, SPR_SR | SPR_SW);
2739
        /* IBAT6L */
2740
        spr_set_rights(IBAT6L, SPR_SR | SPR_SW);
2741
        /* IBAT7U */
2742
        spr_set_rights(IBAT7U, SPR_SR | SPR_SW);
2743
        /* IBAT7L */
2744
        spr_set_rights(IBAT7L, SPR_SR | SPR_SW);
2745
        /* DBAT4U */
2746
        spr_set_rights(DBAT4U, SPR_SR | SPR_SW);
2747
        /* DBAT4L */
2748
        spr_set_rights(DBAT4L, SPR_SR | SPR_SW);
2749
        /* DBAT5U */
2750
        spr_set_rights(DBAT5U, SPR_SR | SPR_SW);
2751
        /* DBAT5L */
2752
        spr_set_rights(DBAT5L, SPR_SR | SPR_SW);
2753
        /* DBAT6U */
2754
        spr_set_rights(DBAT6U, SPR_SR | SPR_SW);
2755
        /* DBAT6L */
2756
        spr_set_rights(DBAT6L, SPR_SR | SPR_SW);
2757
        /* DBAT7U */
2758
        spr_set_rights(DBAT7U, SPR_SR | SPR_SW);
2759
        /* DBAT7L */
2760
        spr_set_rights(DBAT7L, SPR_SR | SPR_SW);
2761
        /* DMISS */
2762
        spr_set_rights(SPR_ENCODE(976), SPR_SR | SPR_SW);
2763
        /* DCMP */
2764
        spr_set_rights(SPR_ENCODE(977), SPR_SR | SPR_SW);
2765
        /* DHASH1 */
2766
        spr_set_rights(SPR_ENCODE(978), SPR_SR | SPR_SW);
2767
        /* DHASH2 */
2768
        spr_set_rights(SPR_ENCODE(979), SPR_SR | SPR_SW);
2769
        /* IMISS */
2770
        spr_set_rights(SPR_ENCODE(980), SPR_SR | SPR_SW);
2771
        /* ICMP */
2772
        spr_set_rights(SPR_ENCODE(981), SPR_SR | SPR_SW);
2773
        /* RPA */
2774
        spr_set_rights(SPR_ENCODE(982), SPR_SR | SPR_SW);
2775
        /* HID2 */
2776
        spr_set_rights(SPR_ENCODE(1011), SPR_SR | SPR_SW);
2777
        /* L2PM */
2778
        spr_set_rights(SPR_ENCODE(1016), SPR_SR | SPR_SW);
2779
    }
2780
}
2781

    
2782
/*****************************************************************************/
2783
/* PPC "main stream" common instructions (no optional ones) */
2784

    
2785
typedef struct ppc_proc_t {
2786
    int flags;
2787
    void *specific;
2788
} ppc_proc_t;
2789

    
2790
typedef struct ppc_def_t {
2791
    unsigned long pvr;
2792
    unsigned long pvr_mask;
2793
    ppc_proc_t *proc;
2794
} ppc_def_t;
2795

    
2796
static ppc_proc_t ppc_proc_common = {
2797
    .flags    = PPC_COMMON,
2798
    .specific = NULL,
2799
};
2800

    
2801
static ppc_proc_t ppc_proc_G3 = {
2802
    .flags    = PPC_750,
2803
    .specific = NULL,
2804
};
2805

    
2806
static ppc_def_t ppc_defs[] =
2807
{
2808
    /* MPC740/745/750/755 (G3) */
2809
    {
2810
        .pvr      = 0x00080000,
2811
        .pvr_mask = 0xFFFF0000,
2812
        .proc     = &ppc_proc_G3,
2813
    },
2814
    /* IBM 750FX (G3 embedded) */
2815
    {
2816
        .pvr      = 0x70000000,
2817
        .pvr_mask = 0xFFFF0000,
2818
        .proc     = &ppc_proc_G3,
2819
    },
2820
    /* Fallback (generic PPC) */
2821
    {
2822
        .pvr      = 0x00000000,
2823
        .pvr_mask = 0x00000000,
2824
        .proc     = &ppc_proc_common,
2825
    },
2826
};
2827

    
2828
static int create_ppc_proc (opc_handler_t **ppc_opcodes, unsigned long pvr)
2829
{
2830
    opcode_t *opc;
2831
    int i, flags;
2832

    
2833
    fill_new_table(ppc_opcodes, 0x40);
2834
    for (i = 0; ; i++) {
2835
        if ((ppc_defs[i].pvr & ppc_defs[i].pvr_mask) ==
2836
            (pvr & ppc_defs[i].pvr_mask)) {
2837
            flags = ppc_defs[i].proc->flags;
2838
            break;
2839
        }
2840
    }
2841
    
2842
    for (opc = &opc_start + 1; opc != &opc_end; opc++) {
2843
        if ((opc->handler.type & flags) != 0)
2844
            if (register_insn(ppc_opcodes, opc) < 0) {
2845
                printf("*** ERROR initializing PPC instruction "
2846
                        "0x%02x 0x%02x 0x%02x\n", opc->opc1, opc->opc2,
2847
                        opc->opc3);
2848
                return -1;
2849
            }
2850
    }
2851
    fix_opcode_tables(ppc_opcodes);
2852

    
2853
    return 0;
2854
}
2855

    
2856

    
2857
/*****************************************************************************/
2858
/* Misc PPC helpers */
2859
FILE *stdout;
2860

    
2861
void cpu_ppc_dump_state(CPUPPCState *env, FILE *f, int flags)
2862
{
2863
    int i;
2864

    
2865
    fprintf(f, "nip=0x%08x LR=0x%08x CTR=0x%08x XER=0x%08x "
2866
            "MSR=0x%08x\n", env->nip, env->lr, env->ctr,
2867
            _load_xer(), _load_msr());
2868
        for (i = 0; i < 32; i++) {
2869
            if ((i & 7) == 0)
2870
            fprintf(f, "GPR%02d:", i);
2871
        fprintf(f, " %08x", env->gpr[i]);
2872
            if ((i & 7) == 7)
2873
            fprintf(f, "\n");
2874
        }
2875
    fprintf(f, "CR: 0x");
2876
        for (i = 0; i < 8; i++)
2877
        fprintf(f, "%01x", env->crf[i]);
2878
    fprintf(f, "  [");
2879
        for (i = 0; i < 8; i++) {
2880
            char a = '-';
2881
            if (env->crf[i] & 0x08)
2882
                a = 'L';
2883
            else if (env->crf[i] & 0x04)
2884
                a = 'G';
2885
            else if (env->crf[i] & 0x02)
2886
                a = 'E';
2887
        fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
2888
        }
2889
    fprintf(f, " ] ");
2890
    fprintf(f, "TB: 0x%08x %08x\n", env->tb[1], env->tb[0]);
2891
        for (i = 0; i < 16; i++) {
2892
            if ((i & 3) == 0)
2893
            fprintf(f, "FPR%02d:", i);
2894
        fprintf(f, " %016llx", *((uint64_t *)&env->fpr[i]));
2895
            if ((i & 3) == 3)
2896
            fprintf(f, "\n");
2897
    }
2898
    fprintf(f, "SRR0 0x%08x SRR1 0x%08x\n",
2899
            env->spr[SRR0], env->spr[SRR1]);
2900
    fprintf(f, "reservation 0x%08x\n", env->reserve);
2901
    fflush(f);
2902
}
2903

    
2904
#if !defined(CONFIG_USER_ONLY) && defined (USE_OPENFIRMWARE)
2905
int setup_machine (CPUPPCState *env, uint32_t mid);
2906
#endif
2907

    
2908
CPUPPCState *cpu_ppc_init(void)
2909
{
2910
    CPUPPCState *env;
2911

    
2912
    cpu_exec_init();
2913

    
2914
    env = malloc(sizeof(CPUPPCState));
2915
    if (!env)
2916
        return NULL;
2917
    memset(env, 0, sizeof(CPUPPCState));
2918
#if !defined(CONFIG_USER_ONLY) && defined (USE_OPEN_FIRMWARE)
2919
    setup_machine(env, 0);
2920
#else
2921
//    env->spr[PVR] = 0; /* Basic PPC */
2922
    env->spr[PVR] = 0x00080100; /* G3 CPU */
2923
//    env->spr[PVR] = 0x00083100; /* MPC755 (G3 embedded) */
2924
//    env->spr[PVR] = 0x00070100; /* IBM 750FX */
2925
#endif
2926
    env->decr = 0xFFFFFFFF;
2927
    if (create_ppc_proc(ppc_opcodes, env->spr[PVR]) < 0)
2928
        return NULL;
2929
    init_spr_rights(env->spr[PVR]);
2930
    tlb_flush(env, 1);
2931
#if defined (DO_SINGLE_STEP)
2932
    /* Single step trace mode */
2933
    msr_se = 1;
2934
#endif
2935
#if defined(CONFIG_USER_ONLY)
2936
    msr_pr = 1;
2937
#endif
2938

    
2939
    return env;
2940
}
2941

    
2942
void cpu_ppc_close(CPUPPCState *env)
2943
{
2944
    /* Should also remove all opcode tables... */
2945
    free(env);
2946
}
2947

    
2948
/*****************************************************************************/
2949
void raise_exception_err (int exception_index, int error_code);
2950
int print_insn_powerpc (FILE *out, unsigned long insn, unsigned memaddr,
2951
                        int dialect);
2952

    
2953
int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
2954
                                    int search_pc)
2955
{
2956
    DisasContext ctx;
2957
    opc_handler_t **table, *handler;
2958
    uint32_t pc_start;
2959
    uint16_t *gen_opc_end;
2960
    int j, lj = -1;
2961

    
2962
    pc_start = tb->pc;
2963
    gen_opc_ptr = gen_opc_buf;
2964
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
2965
    gen_opparam_ptr = gen_opparam_buf;
2966
    ctx.nip = (uint32_t *)pc_start;
2967
    ctx.tb_offset = 0;
2968
    ctx.decr_offset = 0;
2969
    ctx.tb = tb;
2970
    ctx.exception = EXCP_NONE;
2971
#if defined(CONFIG_USER_ONLY)
2972
    ctx.mem_idx = 0;
2973
#else
2974
    ctx.supervisor = 1 - msr_pr;
2975
    ctx.mem_idx = (1 - msr_pr);
2976
#endif
2977
#if defined (DO_SINGLE_STEP)
2978
    /* Single step trace mode */
2979
    msr_se = 1;
2980
#endif
2981
    /* Set env in case of segfault during code fetch */
2982
    while (ctx.exception == EXCP_NONE && gen_opc_ptr < gen_opc_end) {
2983
        if (search_pc) {
2984
            if (loglevel > 0)
2985
                fprintf(logfile, "Search PC...\n");
2986
            j = gen_opc_ptr - gen_opc_buf;
2987
            if (lj < j) {
2988
                lj++;
2989
                while (lj < j)
2990
                    gen_opc_instr_start[lj++] = 0;
2991
                gen_opc_pc[lj] = (uint32_t)ctx.nip;
2992
                gen_opc_instr_start[lj] = 1;
2993
            }
2994
        }
2995
#if defined DEBUG_DISAS
2996
        if (loglevel > 0) {
2997
            fprintf(logfile, "----------------\n");
2998
            fprintf(logfile, "nip=%p super=%d ir=%d\n",
2999
                    ctx.nip, 1 - msr_pr, msr_ir);
3000
        }
3001
#endif
3002
        ctx.opcode = ldl_code(ctx.nip);
3003
#if defined DEBUG_DISAS
3004
        if (loglevel > 0) {
3005
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x)\n",
3006
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
3007
                    opc3(ctx.opcode));
3008
        }
3009
#endif
3010
        ctx.nip++;
3011
        ctx.tb_offset++;
3012
        /* Check decrementer exception */
3013
        if (++ctx.decr_offset == env->decr + 1)
3014
            ctx.exception = EXCP_DECR;
3015
        table = ppc_opcodes;
3016
        handler = table[opc1(ctx.opcode)];
3017
        if (is_indirect_opcode(handler)) {
3018
            table = ind_table(handler);
3019
            handler = table[opc2(ctx.opcode)];
3020
            if (is_indirect_opcode(handler)) {
3021
                table = ind_table(handler);
3022
                handler = table[opc3(ctx.opcode)];
3023
            }
3024
        }
3025
        /* Is opcode *REALLY* valid ? */
3026
        if ((ctx.opcode & handler->inval) != 0) {
3027
            if (loglevel > 0) {
3028
                if (handler->handler == &gen_invalid) {
3029
                    fprintf(logfile, "invalid/unsupported opcode: "
3030
                            "%02x -%02x - %02x (%08x) %p\n",
3031
                            opc1(ctx.opcode), opc2(ctx.opcode),
3032
                            opc3(ctx.opcode), ctx.opcode, ctx.nip - 1);
3033
                } else {
3034
                    fprintf(logfile, "invalid bits: %08x for opcode: "
3035
                            "%02x -%02x - %02x (0x%08x) (%p)\n",
3036
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
3037
                            opc2(ctx.opcode), opc3(ctx.opcode),
3038
                            ctx.opcode, ctx.nip - 1);
3039
                }
3040
            } else {
3041
                if (handler->handler == &gen_invalid) {
3042
                    printf("invalid/unsupported opcode: "
3043
                           "%02x -%02x - %02x (%08x) %p\n",
3044
                           opc1(ctx.opcode), opc2(ctx.opcode),
3045
                           opc3(ctx.opcode), ctx.opcode, ctx.nip - 1);
3046
                } else {
3047
                    printf("invalid bits: %08x for opcode: "
3048
                           "%02x -%02x - %02x (0x%08x) (%p)\n",
3049
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
3050
                            opc2(ctx.opcode), opc3(ctx.opcode),
3051
                           ctx.opcode, ctx.nip - 1);
3052
            }
3053
            }
3054
            (*gen_invalid)(&ctx);
3055
        } else {
3056
            (*(handler->handler))(&ctx);
3057
        }
3058
        /* Check trace mode exceptions */
3059
        if ((msr_be && ctx.exception == EXCP_BRANCH) ||
3060
            /* Check in single step trace mode
3061
             * we need to stop except if:
3062
             * - rfi, trap or syscall
3063
             * - first instruction of an exception handler
3064
             */
3065
            (msr_se && ((uint32_t)ctx.nip < 0x100 ||
3066
                        (uint32_t)ctx.nip > 0xF00 ||
3067
                        ((uint32_t)ctx.nip & 0xFC) != 0x04) &&
3068
             ctx.exception != EXCP_SYSCALL && ctx.exception != EXCP_RFI &&
3069
             ctx.exception != EXCP_TRAP)) {
3070
#if !defined(CONFIG_USER_ONLY)
3071
            gen_op_queue_exception(EXCP_TRACE);
3072
#endif
3073
            if (ctx.exception == EXCP_NONE) {
3074
                ctx.exception = EXCP_TRACE;
3075
    }
3076
        }
3077
        /* if too long translation, stop generation too */
3078
        if (gen_opc_ptr >= gen_opc_end ||
3079
            ((uint32_t)ctx.nip - pc_start) >= (TARGET_PAGE_SIZE - 32)) {
3080
            if (ctx.exception == EXCP_NONE) {
3081
        gen_op_b((long)ctx.tb, (uint32_t)ctx.nip);
3082
                ctx.exception = EXCP_BRANCH;
3083
    }
3084
    }
3085
    }
3086
    /* In case of branch, this has already been done *BEFORE* the branch */
3087
    if (ctx.exception != EXCP_BRANCH && ctx.exception != EXCP_RFI) {
3088
        gen_op_update_tb(ctx.tb_offset);
3089
        gen_op_update_decr(ctx.decr_offset);
3090
        gen_op_process_exceptions((uint32_t)ctx.nip);
3091
    }
3092
#if 1
3093
    /* TO BE FIXED: T0 hasn't got a proper value, which makes tb_add_jump
3094
     *              do bad business and then qemu crashes !
3095
     */
3096
    gen_op_set_T0(0);
3097
#endif
3098
    /* Generate the return instruction */
3099
    gen_op_exit_tb();
3100
    *gen_opc_ptr = INDEX_op_end;
3101
    if (search_pc) {
3102
        j = gen_opc_ptr - gen_opc_buf;
3103
        lj++;
3104
        while (lj <= j)
3105
            gen_opc_instr_start[lj++] = 0;
3106
        tb->size = 0;
3107
#if 0
3108
        if (loglevel > 0) {
3109
            page_dump(logfile);
3110
        }
3111
#endif
3112
    } else {
3113
        tb->size = (uint32_t)ctx.nip - pc_start;
3114
    }
3115
#ifdef DEBUG_DISAS
3116
    if (loglevel > 0) {
3117
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
3118
        cpu_ppc_dump_state(env, logfile, 0);
3119
        fprintf(logfile, "IN: %s\n", lookup_symbol((void *)pc_start));
3120
        disas(logfile, (void *)pc_start, (uint32_t)ctx.nip - pc_start, 0, 0);
3121
        fprintf(logfile, "\n");
3122

    
3123
        fprintf(logfile, "OP:\n");
3124
        dump_ops(gen_opc_buf, gen_opparam_buf);
3125
        fprintf(logfile, "\n");
3126
    }
3127
#endif
3128

    
3129
    return 0;
3130
}
3131

    
3132
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
3133
{
3134
    return gen_intermediate_code_internal(env, tb, 0);
3135
}
3136

    
3137
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
3138
{
3139
    return gen_intermediate_code_internal(env, tb, 1);
3140
}