Statistics
| Branch: | Revision:

root / target-ppc / translate.c @ 18fba28c

History | View | Annotate | Download (106.4 kB)

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

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

    
30
//#define DO_SINGLE_STEP
31
//#define PPC_DEBUG_DISAS
32

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

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

    
43
#include "gen-op.h"
44

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

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

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

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

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

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

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

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

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

    
129
static uint8_t  spr_access[1024 / 2];
130

    
131
/* internal defines */
132
typedef struct DisasContext {
133
    struct TranslationBlock *tb;
134
    target_ulong nip;
135
    uint32_t opcode;
136
    uint32_t exception;
137
    /* 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(ctx, excp, error)                                            \
155
do {                                                                          \
156
    if ((ctx)->exception == EXCP_NONE) {                                      \
157
        gen_op_update_nip((ctx)->nip);                                        \
158
    }                                                                         \
159
    gen_op_raise_exception_err((excp), (error));                              \
160
    ctx->exception = (excp);                                                  \
161
} while (0)
162

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
281
    return ret;
282
}
283

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

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

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

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

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

    
327
/* Special opcode to stop emulation */
328
GEN_HANDLER(stop, 0x06, 0x00, 0xFF, 0x03FFFFC1, PPC_COMMON)
329
{
330
    RET_EXCP(ctx, EXCP_HLT, 0);
331
}
332

    
333
/* Special opcode to call open-firmware */
334
GEN_HANDLER(of_enter, 0x06, 0x01, 0xFF, 0x03FFFFC1, PPC_COMMON)
335
{
336
    RET_EXCP(ctx, EXCP_OFCALL, 0);
337
}
338

    
339
/* Special opcode to call RTAS */
340
GEN_HANDLER(rtas_enter, 0x06, 0x02, 0xFF, 0x03FFFFC1, PPC_COMMON)
341
{
342
    printf("RTAS entry point !\n");
343
    RET_EXCP(ctx, EXCP_RTASCALL, 0);
344
}
345

    
346
static opc_handler_t invalid_handler = {
347
    .inval   = 0xFFFFFFFF,
348
    .type    = PPC_NONE,
349
    .handler = gen_invalid,
350
};
351

    
352
/***                           Integer arithmetic                          ***/
353
#define __GEN_INT_ARITH2(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();                                                     \
361
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
362
}
363

    
364
#define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval)                     \
365
GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
366
{                                                                             \
367
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
368
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
369
    gen_op_##name();                                                          \
370
    if (Rc(ctx->opcode) != 0)                                                 \
371
        gen_op_set_Rc0();                                                     \
372
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
373
}
374

    
375
#define __GEN_INT_ARITH1(name, opc1, opc2, opc3)                              \
376
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
377
{                                                                             \
378
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
379
    gen_op_##name();                                                          \
380
    if (Rc(ctx->opcode) != 0)                                                 \
381
        gen_op_set_Rc0();                                                     \
382
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
383
}
384
#define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3)                            \
385
GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
386
{                                                                             \
387
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
388
    gen_op_##name();                                                          \
389
    if (Rc(ctx->opcode) != 0)                                                 \
390
        gen_op_set_Rc0();                                                     \
391
    gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
392
}
393

    
394
/* Two operands arithmetic functions */
395
#define GEN_INT_ARITH2(name, opc1, opc2, opc3)                                \
396
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000)                          \
397
__GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000)
398

    
399
/* Two operands arithmetic functions with no overflow allowed */
400
#define GEN_INT_ARITHN(name, opc1, opc2, opc3)                                \
401
__GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000400)
402

    
403
/* One operand arithmetic functions */
404
#define GEN_INT_ARITH1(name, opc1, opc2, opc3)                                \
405
__GEN_INT_ARITH1(name, opc1, opc2, opc3)                                      \
406
__GEN_INT_ARITH1_O(name##o, opc1, opc2, opc3 | 0x10)
407

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

    
445
    if (rA(ctx->opcode) == 0) {
446
        gen_op_set_T0(simm);
447
    } else {
448
        gen_op_load_gpr_T0(rA(ctx->opcode));
449
        gen_op_addi(simm);
450
    }
451
    gen_op_store_T0_gpr(rD(ctx->opcode));
452
}
453
/* addic */
454
GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
455
{
456
    gen_op_load_gpr_T0(rA(ctx->opcode));
457
    gen_op_addic(SIMM(ctx->opcode));
458
    gen_op_store_T0_gpr(rD(ctx->opcode));
459
}
460
/* addic. */
461
GEN_HANDLER(addic_, 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
462
{
463
    gen_op_load_gpr_T0(rA(ctx->opcode));
464
    gen_op_addic(SIMM(ctx->opcode));
465
    gen_op_set_Rc0();
466
    gen_op_store_T0_gpr(rD(ctx->opcode));
467
}
468
/* addis */
469
GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
470
{
471
    int32_t simm = SIMM(ctx->opcode);
472

    
473
    if (rA(ctx->opcode) == 0) {
474
        gen_op_set_T0(simm << 16);
475
    } else {
476
        gen_op_load_gpr_T0(rA(ctx->opcode));
477
        gen_op_addi(simm << 16);
478
    }
479
    gen_op_store_T0_gpr(rD(ctx->opcode));
480
}
481
/* mulli */
482
GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
483
{
484
    gen_op_load_gpr_T0(rA(ctx->opcode));
485
    gen_op_mulli(SIMM(ctx->opcode));
486
    gen_op_store_T0_gpr(rD(ctx->opcode));
487
}
488
/* subfic */
489
GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
490
{
491
    gen_op_load_gpr_T0(rA(ctx->opcode));
492
    gen_op_subfic(SIMM(ctx->opcode));
493
    gen_op_store_T0_gpr(rD(ctx->opcode));
494
}
495

    
496
/***                           Integer comparison                          ***/
497
#define GEN_CMP(name, opc)                                                    \
498
GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, PPC_INTEGER)                   \
499
{                                                                             \
500
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
501
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
502
    gen_op_##name();                                                          \
503
    gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
504
}
505

    
506
/* cmp */
507
GEN_CMP(cmp, 0x00);
508
/* cmpi */
509
GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
510
{
511
    gen_op_load_gpr_T0(rA(ctx->opcode));
512
    gen_op_cmpi(SIMM(ctx->opcode));
513
    gen_op_store_T0_crf(crfD(ctx->opcode));
514
}
515
/* cmpl */
516
GEN_CMP(cmpl, 0x01);
517
/* cmpli */
518
GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
519
{
520
    gen_op_load_gpr_T0(rA(ctx->opcode));
521
    gen_op_cmpli(UIMM(ctx->opcode));
522
    gen_op_store_T0_crf(crfD(ctx->opcode));
523
}
524

    
525
/***                            Integer logical                            ***/
526
#define __GEN_LOGICAL2(name, opc2, opc3)                                      \
527
GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, PPC_INTEGER)                  \
528
{                                                                             \
529
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
530
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
531
    gen_op_##name();                                                          \
532
    if (Rc(ctx->opcode) != 0)                                                 \
533
        gen_op_set_Rc0();                                                     \
534
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
535
}
536
#define GEN_LOGICAL2(name, opc)                                               \
537
__GEN_LOGICAL2(name, 0x1C, opc)
538

    
539
#define GEN_LOGICAL1(name, opc)                                               \
540
GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, PPC_INTEGER)                   \
541
{                                                                             \
542
    gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
543
    gen_op_##name();                                                          \
544
    if (Rc(ctx->opcode) != 0)                                                 \
545
        gen_op_set_Rc0();                                                     \
546
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
547
}
548

    
549
/* and & and. */
550
GEN_LOGICAL2(and, 0x00);
551
/* andc & andc. */
552
GEN_LOGICAL2(andc, 0x01);
553
/* andi. */
554
GEN_HANDLER(andi_, 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
555
{
556
    gen_op_load_gpr_T0(rS(ctx->opcode));
557
    gen_op_andi_(UIMM(ctx->opcode));
558
    gen_op_set_Rc0();
559
    gen_op_store_T0_gpr(rA(ctx->opcode));
560
}
561
/* andis. */
562
GEN_HANDLER(andis_, 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
563
{
564
    gen_op_load_gpr_T0(rS(ctx->opcode));
565
    gen_op_andi_(UIMM(ctx->opcode) << 16);
566
    gen_op_set_Rc0();
567
    gen_op_store_T0_gpr(rA(ctx->opcode));
568
}
569

    
570
/* cntlzw */
571
GEN_LOGICAL1(cntlzw, 0x00);
572
/* eqv & eqv. */
573
GEN_LOGICAL2(eqv, 0x08);
574
/* extsb & extsb. */
575
GEN_LOGICAL1(extsb, 0x1D);
576
/* extsh & extsh. */
577
GEN_LOGICAL1(extsh, 0x1C);
578
/* nand & nand. */
579
GEN_LOGICAL2(nand, 0x0E);
580
/* nor & nor. */
581
GEN_LOGICAL2(nor, 0x03);
582

    
583
/* or & or. */
584
GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
585
{
586
    gen_op_load_gpr_T0(rS(ctx->opcode));
587
    /* Optimisation for mr case */
588
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
589
        gen_op_load_gpr_T1(rB(ctx->opcode));
590
        gen_op_or();
591
    }
592
    if (Rc(ctx->opcode) != 0)
593
        gen_op_set_Rc0();
594
    gen_op_store_T0_gpr(rA(ctx->opcode));
595
}
596

    
597
/* orc & orc. */
598
GEN_LOGICAL2(orc, 0x0C);
599
/* xor & xor. */
600
GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
601
{
602
    gen_op_load_gpr_T0(rS(ctx->opcode));
603
    /* Optimisation for "set to zero" case */
604
    if (rS(ctx->opcode) != rB(ctx->opcode)) {
605
        gen_op_load_gpr_T1(rB(ctx->opcode));
606
        gen_op_xor();
607
    } else {
608
        gen_op_set_T0(0);
609
    }
610
    if (Rc(ctx->opcode) != 0)
611
        gen_op_set_Rc0();
612
    gen_op_store_T0_gpr(rA(ctx->opcode));
613
}
614
/* ori */
615
GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
616
{
617
    uint32_t uimm = UIMM(ctx->opcode);
618

    
619
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
620
        /* NOP */
621
        return;
622
        }
623
        gen_op_load_gpr_T0(rS(ctx->opcode));
624
    if (uimm != 0)
625
        gen_op_ori(uimm);
626
        gen_op_store_T0_gpr(rA(ctx->opcode));
627
}
628
/* oris */
629
GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
630
{
631
    uint32_t uimm = UIMM(ctx->opcode);
632

    
633
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
634
        /* NOP */
635
        return;
636
        }
637
        gen_op_load_gpr_T0(rS(ctx->opcode));
638
    if (uimm != 0)
639
        gen_op_ori(uimm << 16);
640
        gen_op_store_T0_gpr(rA(ctx->opcode));
641
}
642
/* xori */
643
GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
644
{
645
    uint32_t uimm = UIMM(ctx->opcode);
646

    
647
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
648
        /* NOP */
649
        return;
650
    }
651
    gen_op_load_gpr_T0(rS(ctx->opcode));
652
    if (uimm != 0)
653
    gen_op_xori(uimm);
654
    gen_op_store_T0_gpr(rA(ctx->opcode));
655
}
656

    
657
/* xoris */
658
GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
659
{
660
    uint32_t uimm = UIMM(ctx->opcode);
661

    
662
    if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
663
        /* NOP */
664
        return;
665
    }
666
    gen_op_load_gpr_T0(rS(ctx->opcode));
667
    if (uimm != 0)
668
    gen_op_xori(uimm << 16);
669
    gen_op_store_T0_gpr(rA(ctx->opcode));
670
}
671

    
672
/***                             Integer rotate                            ***/
673
/* rlwimi & rlwimi. */
674
GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
675
{
676
    uint32_t mb, me;
677

    
678
    mb = MB(ctx->opcode);
679
    me = ME(ctx->opcode);
680
    gen_op_load_gpr_T0(rS(ctx->opcode));
681
    gen_op_load_gpr_T1(rA(ctx->opcode));
682
    gen_op_rlwimi(SH(ctx->opcode), MASK(mb, me), ~MASK(mb, me));
683
    if (Rc(ctx->opcode) != 0)
684
        gen_op_set_Rc0();
685
    gen_op_store_T0_gpr(rA(ctx->opcode));
686
}
687
/* rlwinm & rlwinm. */
688
GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
689
{
690
    uint32_t mb, me, sh;
691
    
692
    sh = SH(ctx->opcode);
693
    mb = MB(ctx->opcode);
694
    me = ME(ctx->opcode);
695
    gen_op_load_gpr_T0(rS(ctx->opcode));
696
#if 1 // TRY
697
    if (sh == 0) {
698
        gen_op_andi_(MASK(mb, me));
699
        goto store;
700
    }
701
#endif
702
    if (mb == 0) {
703
        if (me == 31) {
704
            gen_op_rotlwi(sh);
705
            goto store;
706
#if 0
707
        } else if (me == (31 - sh)) {
708
            gen_op_slwi(sh);
709
            goto store;
710
#endif
711
        }
712
    } else if (me == 31) {
713
#if 0
714
        if (sh == (32 - mb)) {
715
            gen_op_srwi(mb);
716
            goto store;
717
        }
718
#endif
719
    }
720
    gen_op_rlwinm(sh, MASK(mb, me));
721
store:
722
    if (Rc(ctx->opcode) != 0)
723
        gen_op_set_Rc0();
724
    gen_op_store_T0_gpr(rA(ctx->opcode));
725
}
726
/* rlwnm & rlwnm. */
727
GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
728
{
729
    uint32_t mb, me;
730

    
731
    mb = MB(ctx->opcode);
732
    me = ME(ctx->opcode);
733
    gen_op_load_gpr_T0(rS(ctx->opcode));
734
    gen_op_load_gpr_T1(rB(ctx->opcode));
735
    if (mb == 0 && me == 31) {
736
        gen_op_rotl();
737
    } else
738
    {
739
        gen_op_rlwnm(MASK(mb, me));
740
    }
741
    if (Rc(ctx->opcode) != 0)
742
        gen_op_set_Rc0();
743
    gen_op_store_T0_gpr(rA(ctx->opcode));
744
}
745

    
746
/***                             Integer shift                             ***/
747
/* slw & slw. */
748
__GEN_LOGICAL2(slw, 0x18, 0x00);
749
/* sraw & sraw. */
750
__GEN_LOGICAL2(sraw, 0x18, 0x18);
751
/* srawi & srawi. */
752
GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
753
{
754
    gen_op_load_gpr_T0(rS(ctx->opcode));
755
    gen_op_srawi(SH(ctx->opcode), MASK(32 - SH(ctx->opcode), 31));
756
    if (Rc(ctx->opcode) != 0)
757
        gen_op_set_Rc0();
758
    gen_op_store_T0_gpr(rA(ctx->opcode));
759
}
760
/* srw & srw. */
761
__GEN_LOGICAL2(srw, 0x18, 0x10);
762

    
763
/***                       Floating-Point arithmetic                       ***/
764
#define _GEN_FLOAT_ACB(name, op1, op2)                                        \
765
GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, PPC_FLOAT)                   \
766
{                                                                             \
767
    gen_op_reset_scrfx();                                                     \
768
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
769
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
770
    gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
771
    gen_op_f##name();                                                         \
772
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
773
    if (Rc(ctx->opcode))                                                      \
774
        gen_op_set_Rc1();                                                     \
775
}
776

    
777
#define GEN_FLOAT_ACB(name, op2)                                              \
778
_GEN_FLOAT_ACB(name, 0x3F, op2);                                              \
779
_GEN_FLOAT_ACB(name##s, 0x3B, op2);
780

    
781
#define _GEN_FLOAT_AB(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(rB(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_AB(name, op2, inval)                                        \
793
_GEN_FLOAT_AB(name, 0x3F, op2, inval);                                        \
794
_GEN_FLOAT_AB(name##s, 0x3B, op2, inval);
795

    
796
#define _GEN_FLOAT_AC(name, op1, op2, inval)                                  \
797
GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
798
{                                                                             \
799
    gen_op_reset_scrfx();                                                     \
800
    gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
801
    gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
802
    gen_op_f##name();                                                         \
803
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
804
    if (Rc(ctx->opcode))                                                      \
805
        gen_op_set_Rc1();                                                     \
806
}
807
#define GEN_FLOAT_AC(name, op2, inval)                                        \
808
_GEN_FLOAT_AC(name, 0x3F, op2, inval);                                        \
809
_GEN_FLOAT_AC(name##s, 0x3B, op2, inval);
810

    
811
#define GEN_FLOAT_B(name, op2, op3)                                           \
812
GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, PPC_FLOAT)                   \
813
{                                                                             \
814
    gen_op_reset_scrfx();                                                     \
815
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
816
    gen_op_f##name();                                                         \
817
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
818
    if (Rc(ctx->opcode))                                                      \
819
        gen_op_set_Rc1();                                                     \
820
}
821

    
822
#define GEN_FLOAT_BS(name, op2)                                               \
823
GEN_HANDLER(f##name, 0x3F, op2, 0xFF, 0x001F07C0, PPC_FLOAT)                  \
824
{                                                                             \
825
    gen_op_reset_scrfx();                                                     \
826
    gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
827
    gen_op_f##name();                                                         \
828
    gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
829
    if (Rc(ctx->opcode))                                                      \
830
        gen_op_set_Rc1();                                                     \
831
}
832

    
833
/* fadd - fadds */
834
GEN_FLOAT_AB(add, 0x15, 0x000007C0);
835
/* fdiv */
836
GEN_FLOAT_AB(div, 0x12, 0x000007C0);
837
/* fmul */
838
GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
839

    
840
/* fres */
841
GEN_FLOAT_BS(res, 0x18);
842

    
843
/* frsqrte */
844
GEN_FLOAT_BS(rsqrte, 0x1A);
845

    
846
/* fsel */
847
_GEN_FLOAT_ACB(sel, 0x3F, 0x17);
848
/* fsub */
849
GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
850
/* Optional: */
851
/* fsqrt */
852
GEN_FLOAT_BS(sqrt, 0x16);
853

    
854
GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_OPT)
855
{
856
    gen_op_reset_scrfx();
857
    gen_op_load_fpr_FT0(rB(ctx->opcode));
858
    gen_op_fsqrts();
859
    gen_op_store_FT0_fpr(rD(ctx->opcode));
860
    if (Rc(ctx->opcode))
861
        gen_op_set_Rc1();
862
}
863

    
864
/***                     Floating-Point multiply-and-add                   ***/
865
/* fmadd */
866
GEN_FLOAT_ACB(madd, 0x1D);
867
/* fmsub */
868
GEN_FLOAT_ACB(msub, 0x1C);
869
/* fnmadd */
870
GEN_FLOAT_ACB(nmadd, 0x1F);
871
/* fnmsub */
872
GEN_FLOAT_ACB(nmsub, 0x1E);
873

    
874
/***                     Floating-Point round & convert                    ***/
875
/* fctiw */
876
GEN_FLOAT_B(ctiw, 0x0E, 0x00);
877
/* fctiwz */
878
GEN_FLOAT_B(ctiwz, 0x0F, 0x00);
879
/* frsp */
880
GEN_FLOAT_B(rsp, 0x0C, 0x00);
881

    
882
/***                         Floating-Point compare                        ***/
883
/* fcmpo */
884
GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
885
{
886
    gen_op_reset_scrfx();
887
    gen_op_load_fpr_FT0(rA(ctx->opcode));
888
    gen_op_load_fpr_FT1(rB(ctx->opcode));
889
    gen_op_fcmpo();
890
    gen_op_store_T0_crf(crfD(ctx->opcode));
891
}
892

    
893
/* fcmpu */
894
GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
895
{
896
    gen_op_reset_scrfx();
897
    gen_op_load_fpr_FT0(rA(ctx->opcode));
898
    gen_op_load_fpr_FT1(rB(ctx->opcode));
899
    gen_op_fcmpu();
900
    gen_op_store_T0_crf(crfD(ctx->opcode));
901
}
902

    
903
/***                         Floating-point move                           ***/
904
/* fabs */
905
GEN_FLOAT_B(abs, 0x08, 0x08);
906

    
907
/* fmr  - fmr. */
908
GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
909
{
910
    gen_op_reset_scrfx();
911
    gen_op_load_fpr_FT0(rB(ctx->opcode));
912
    gen_op_store_FT0_fpr(rD(ctx->opcode));
913
    if (Rc(ctx->opcode))
914
        gen_op_set_Rc1();
915
}
916

    
917
/* fnabs */
918
GEN_FLOAT_B(nabs, 0x08, 0x04);
919
/* fneg */
920
GEN_FLOAT_B(neg, 0x08, 0x01);
921

    
922
/***                  Floating-Point status & ctrl register                ***/
923
/* mcrfs */
924
GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
925
{
926
    gen_op_load_fpscr_T0(crfS(ctx->opcode));
927
    gen_op_store_T0_crf(crfD(ctx->opcode));
928
    gen_op_clear_fpscr(crfS(ctx->opcode));
929
}
930

    
931
/* mffs */
932
GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
933
{
934
    gen_op_load_fpscr();
935
    gen_op_store_FT0_fpr(rD(ctx->opcode));
936
    if (Rc(ctx->opcode))
937
        gen_op_set_Rc1();
938
}
939

    
940
/* mtfsb0 */
941
GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
942
{
943
    uint8_t crb;
944
    
945
    crb = crbD(ctx->opcode) >> 2;
946
    gen_op_load_fpscr_T0(crb);
947
    gen_op_andi_(~(1 << (crbD(ctx->opcode) & 0x03)));
948
    gen_op_store_T0_fpscr(crb);
949
    if (Rc(ctx->opcode))
950
        gen_op_set_Rc1();
951
}
952

    
953
/* mtfsb1 */
954
GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 0x001FF800, PPC_FLOAT)
955
{
956
    uint8_t crb;
957
    
958
    crb = crbD(ctx->opcode) >> 2;
959
    gen_op_load_fpscr_T0(crb);
960
    gen_op_ori(1 << (crbD(ctx->opcode) & 0x03));
961
    gen_op_store_T0_fpscr(crb);
962
    if (Rc(ctx->opcode))
963
        gen_op_set_Rc1();
964
}
965

    
966
/* mtfsf */
967
GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
968
{
969
    gen_op_load_fpr_FT0(rB(ctx->opcode));
970
    gen_op_store_fpscr(FM(ctx->opcode));
971
    if (Rc(ctx->opcode))
972
        gen_op_set_Rc1();
973
}
974

    
975
/* mtfsfi */
976
GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
977
{
978
    gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
979
    if (Rc(ctx->opcode))
980
        gen_op_set_Rc1();
981
}
982

    
983
/***                             Integer load                              ***/
984
#if defined(CONFIG_USER_ONLY)
985
#define op_ldst(name)        gen_op_##name##_raw()
986
#define OP_LD_TABLE(width)
987
#define OP_ST_TABLE(width)
988
#else
989
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
990
#define OP_LD_TABLE(width)                                                    \
991
static GenOpFunc *gen_op_l##width[] = {                                       \
992
    &gen_op_l##width##_user,                                                  \
993
    &gen_op_l##width##_kernel,                                                \
994
}
995
#define OP_ST_TABLE(width)                                                    \
996
static GenOpFunc *gen_op_st##width[] = {                                      \
997
    &gen_op_st##width##_user,                                                 \
998
    &gen_op_st##width##_kernel,                                               \
999
}
1000
#endif
1001

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

    
1017
#define GEN_LDU(width, opc)                                                   \
1018
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
1019
{                                                                             \
1020
    uint32_t simm = SIMM(ctx->opcode);                                        \
1021
    if (rA(ctx->opcode) == 0 ||                                               \
1022
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1023
        RET_INVAL(ctx);                                                       \
1024
        return;                                                               \
1025
    }                                                                         \
1026
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1027
    if (simm != 0)                                                            \
1028
        gen_op_addi(simm);                                                    \
1029
    op_ldst(l##width);                                                        \
1030
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1031
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1032
}
1033

    
1034
#define GEN_LDUX(width, opc)                                                  \
1035
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
1036
{                                                                             \
1037
    if (rA(ctx->opcode) == 0 ||                                               \
1038
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1039
        RET_INVAL(ctx);                                                       \
1040
        return;                                                               \
1041
    }                                                                         \
1042
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1043
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1044
    gen_op_add();                                                             \
1045
    op_ldst(l##width);                                                        \
1046
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1047
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1048
}
1049

    
1050
#define GEN_LDX(width, opc2, opc3)                                            \
1051
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
1052
{                                                                             \
1053
    if (rA(ctx->opcode) == 0) {                                               \
1054
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1055
    } else {                                                                  \
1056
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1057
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1058
        gen_op_add();                                                         \
1059
    }                                                                         \
1060
    op_ldst(l##width);                                                        \
1061
    gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1062
}
1063

    
1064
#define GEN_LDS(width, op)                                                    \
1065
OP_LD_TABLE(width);                                                           \
1066
GEN_LD(width, op | 0x20);                                                     \
1067
GEN_LDU(width, op | 0x21);                                                    \
1068
GEN_LDUX(width, op | 0x01);                                                   \
1069
GEN_LDX(width, 0x17, op | 0x00)
1070

    
1071
/* lbz lbzu lbzux lbzx */
1072
GEN_LDS(bz, 0x02);
1073
/* lha lhau lhaux lhax */
1074
GEN_LDS(ha, 0x0A);
1075
/* lhz lhzu lhzux lhzx */
1076
GEN_LDS(hz, 0x08);
1077
/* lwz lwzu lwzux lwzx */
1078
GEN_LDS(wz, 0x00);
1079

    
1080
/***                              Integer store                            ***/
1081
#define GEN_ST(width, opc)                                                    \
1082
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
1083
{                                                                             \
1084
    uint32_t simm = SIMM(ctx->opcode);                                        \
1085
    if (rA(ctx->opcode) == 0) {                                               \
1086
        gen_op_set_T0(simm);                                                  \
1087
    } else {                                                                  \
1088
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1089
        if (simm != 0)                                                        \
1090
            gen_op_addi(simm);                                                \
1091
    }                                                                         \
1092
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1093
    op_ldst(st##width);                                                       \
1094
}
1095

    
1096
#define GEN_STU(width, opc)                                                   \
1097
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
1098
{                                                                             \
1099
    uint32_t simm = SIMM(ctx->opcode);                                        \
1100
    if (rA(ctx->opcode) == 0) {                                               \
1101
        RET_INVAL(ctx);                                                       \
1102
        return;                                                               \
1103
    }                                                                         \
1104
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1105
    if (simm != 0)                                                            \
1106
        gen_op_addi(simm);                                                    \
1107
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1108
    op_ldst(st##width);                                                       \
1109
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1110
}
1111

    
1112
#define GEN_STUX(width, opc)                                                  \
1113
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
1114
{                                                                             \
1115
    if (rA(ctx->opcode) == 0) {                                               \
1116
        RET_INVAL(ctx);                                                       \
1117
        return;                                                               \
1118
    }                                                                         \
1119
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1120
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1121
    gen_op_add();                                                             \
1122
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1123
    op_ldst(st##width);                                                       \
1124
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1125
}
1126

    
1127
#define GEN_STX(width, opc2, opc3)                                            \
1128
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
1129
{                                                                             \
1130
    if (rA(ctx->opcode) == 0) {                                               \
1131
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1132
    } else {                                                                  \
1133
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1134
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1135
        gen_op_add();                                                         \
1136
    }                                                                         \
1137
    gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1138
    op_ldst(st##width);                                                       \
1139
}
1140

    
1141
#define GEN_STS(width, op)                                                    \
1142
OP_ST_TABLE(width);                                                           \
1143
GEN_ST(width, op | 0x20);                                                     \
1144
GEN_STU(width, op | 0x21);                                                    \
1145
GEN_STUX(width, op | 0x01);                                                   \
1146
GEN_STX(width, 0x17, op | 0x00)
1147

    
1148
/* stb stbu stbux stbx */
1149
GEN_STS(b, 0x06);
1150
/* sth sthu sthux sthx */
1151
GEN_STS(h, 0x0C);
1152
/* stw stwu stwux stwx */
1153
GEN_STS(w, 0x04);
1154

    
1155
/***                Integer load and store with byte reverse               ***/
1156
/* lhbrx */
1157
OP_LD_TABLE(hbr);
1158
GEN_LDX(hbr, 0x16, 0x18);
1159
/* lwbrx */
1160
OP_LD_TABLE(wbr);
1161
GEN_LDX(wbr, 0x16, 0x10);
1162
/* sthbrx */
1163
OP_ST_TABLE(hbr);
1164
GEN_STX(hbr, 0x16, 0x1C);
1165
/* stwbrx */
1166
OP_ST_TABLE(wbr);
1167
GEN_STX(wbr, 0x16, 0x14);
1168

    
1169
/***                    Integer load and store multiple                    ***/
1170
#if defined(CONFIG_USER_ONLY)
1171
#define op_ldstm(name, reg) gen_op_##name##_raw(reg)
1172
#else
1173
#define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
1174
static GenOpFunc1 *gen_op_lmw[] = {
1175
    &gen_op_lmw_user,
1176
    &gen_op_lmw_kernel,
1177
};
1178
static GenOpFunc1 *gen_op_stmw[] = {
1179
    &gen_op_stmw_user,
1180
    &gen_op_stmw_kernel,
1181
};
1182
#endif
1183

    
1184
/* lmw */
1185
GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1186
{
1187
    int simm = SIMM(ctx->opcode);
1188

    
1189
    if (rA(ctx->opcode) == 0) {
1190
        gen_op_set_T0(simm);
1191
    } else {
1192
        gen_op_load_gpr_T0(rA(ctx->opcode));
1193
        if (simm != 0)
1194
            gen_op_addi(simm);
1195
    }
1196
    op_ldstm(lmw, rD(ctx->opcode));
1197
}
1198

    
1199
/* stmw */
1200
GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1201
{
1202
    int simm = SIMM(ctx->opcode);
1203

    
1204
    if (rA(ctx->opcode) == 0) {
1205
        gen_op_set_T0(simm);
1206
    } else {
1207
        gen_op_load_gpr_T0(rA(ctx->opcode));
1208
        if (simm != 0)
1209
            gen_op_addi(simm);
1210
    }
1211
    op_ldstm(stmw, rS(ctx->opcode));
1212
}
1213

    
1214
/***                    Integer load and store strings                     ***/
1215
#if defined(CONFIG_USER_ONLY)
1216
#define op_ldsts(name, start) gen_op_##name##_raw(start)
1217
#define op_ldstsx(name, rd, ra, rb) gen_op_##name##_raw(rd, ra, rb)
1218
#else
1219
#define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
1220
#define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
1221
static GenOpFunc1 *gen_op_lswi[] = {
1222
    &gen_op_lswi_user,
1223
    &gen_op_lswi_kernel,
1224
};
1225
static GenOpFunc3 *gen_op_lswx[] = {
1226
    &gen_op_lswx_user,
1227
    &gen_op_lswx_kernel,
1228
};
1229
static GenOpFunc1 *gen_op_stsw[] = {
1230
    &gen_op_stsw_user,
1231
    &gen_op_stsw_kernel,
1232
};
1233
#endif
1234

    
1235
/* lswi */
1236
/* PPC32 specification says we must generate an exception if
1237
 * rA is in the range of registers to be loaded.
1238
 * In an other hand, IBM says this is valid, but rA won't be loaded.
1239
 * For now, I'll follow the spec...
1240
 */
1241
GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
1242
{
1243
    int nb = NB(ctx->opcode);
1244
    int start = rD(ctx->opcode);
1245
    int ra = rA(ctx->opcode);
1246
    int nr;
1247

    
1248
    if (nb == 0)
1249
        nb = 32;
1250
    nr = nb / 4;
1251
    if (((start + nr) > 32  && start <= ra && (start + nr - 32) > ra) ||
1252
        ((start + nr) <= 32 && start <= ra && (start + nr) > ra)) {
1253
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_LSWX);
1254
        return;
1255
    }
1256
    if (ra == 0) {
1257
        gen_op_set_T0(0);
1258
    } else {
1259
        gen_op_load_gpr_T0(ra);
1260
    }
1261
    gen_op_set_T1(nb);
1262
    op_ldsts(lswi, start);
1263
}
1264

    
1265
/* lswx */
1266
GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
1267
{
1268
    int ra = rA(ctx->opcode);
1269
    int rb = rB(ctx->opcode);
1270

    
1271
    if (ra == 0) {
1272
        gen_op_load_gpr_T0(rb);
1273
        ra = rb;
1274
    } else {
1275
        gen_op_load_gpr_T0(ra);
1276
        gen_op_load_gpr_T1(rb);
1277
        gen_op_add();
1278
    }
1279
    gen_op_load_xer_bc();
1280
    op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
1281
}
1282

    
1283
/* stswi */
1284
GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
1285
{
1286
    int nb = NB(ctx->opcode);
1287

    
1288
    if (rA(ctx->opcode) == 0) {
1289
        gen_op_set_T0(0);
1290
    } else {
1291
        gen_op_load_gpr_T0(rA(ctx->opcode));
1292
    }
1293
    if (nb == 0)
1294
        nb = 32;
1295
    gen_op_set_T1(nb);
1296
    op_ldsts(stsw, rS(ctx->opcode));
1297
}
1298

    
1299
/* stswx */
1300
GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
1301
{
1302
    int ra = rA(ctx->opcode);
1303

    
1304
    if (ra == 0) {
1305
        gen_op_load_gpr_T0(rB(ctx->opcode));
1306
        ra = rB(ctx->opcode);
1307
    } else {
1308
        gen_op_load_gpr_T0(ra);
1309
        gen_op_load_gpr_T1(rB(ctx->opcode));
1310
        gen_op_add();
1311
    }
1312
    gen_op_load_xer_bc();
1313
    op_ldsts(stsw, rS(ctx->opcode));
1314
}
1315

    
1316
/***                        Memory synchronisation                         ***/
1317
/* eieio */
1318
GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FF0801, PPC_MEM)
1319
{
1320
}
1321

    
1322
/* isync */
1323
GEN_HANDLER(isync, 0x13, 0x16, 0xFF, 0x03FF0801, PPC_MEM)
1324
{
1325
}
1326

    
1327
/* lwarx */
1328
#if defined(CONFIG_USER_ONLY)
1329
#define op_lwarx() gen_op_lwarx_raw()
1330
#define op_stwcx() gen_op_stwcx_raw()
1331
#else
1332
#define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
1333
static GenOpFunc *gen_op_lwarx[] = {
1334
    &gen_op_lwarx_user,
1335
    &gen_op_lwarx_kernel,
1336
};
1337
#define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
1338
static GenOpFunc *gen_op_stwcx[] = {
1339
    &gen_op_stwcx_user,
1340
    &gen_op_stwcx_kernel,
1341
};
1342
#endif
1343

    
1344
GEN_HANDLER(lwarx, 0x1F, 0x14, 0xFF, 0x00000001, PPC_RES)
1345
{
1346
    if (rA(ctx->opcode) == 0) {
1347
        gen_op_load_gpr_T0(rB(ctx->opcode));
1348
    } else {
1349
        gen_op_load_gpr_T0(rA(ctx->opcode));
1350
        gen_op_load_gpr_T1(rB(ctx->opcode));
1351
        gen_op_add();
1352
    }
1353
    op_lwarx();
1354
    gen_op_store_T1_gpr(rD(ctx->opcode));
1355
}
1356

    
1357
/* stwcx. */
1358
GEN_HANDLER(stwcx_, 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
1359
{
1360
        if (rA(ctx->opcode) == 0) {
1361
            gen_op_load_gpr_T0(rB(ctx->opcode));
1362
        } else {
1363
            gen_op_load_gpr_T0(rA(ctx->opcode));
1364
            gen_op_load_gpr_T1(rB(ctx->opcode));
1365
        gen_op_add();
1366
        }
1367
    gen_op_load_gpr_T1(rS(ctx->opcode));
1368
    op_stwcx();
1369
}
1370

    
1371
/* sync */
1372
GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x03FF0801, PPC_MEM)
1373
{
1374
}
1375

    
1376
/***                         Floating-point load                           ***/
1377
#define GEN_LDF(width, opc)                                                   \
1378
GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)               \
1379
{                                                                             \
1380
    uint32_t simm = SIMM(ctx->opcode);                                        \
1381
    if (rA(ctx->opcode) == 0) {                                               \
1382
        gen_op_set_T0(simm);                                                  \
1383
    } else {                                                                  \
1384
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1385
        if (simm != 0)                                                        \
1386
            gen_op_addi(simm);                                                \
1387
    }                                                                         \
1388
    op_ldst(l##width);                                                        \
1389
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1390
}
1391

    
1392
#define GEN_LDUF(width, opc)                                                  \
1393
GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
1394
{                                                                             \
1395
    uint32_t simm = SIMM(ctx->opcode);                                        \
1396
    if (rA(ctx->opcode) == 0 ||                                               \
1397
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1398
        RET_INVAL(ctx);                                                       \
1399
        return;                                                               \
1400
    }                                                                         \
1401
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1402
    if (simm != 0)                                                            \
1403
        gen_op_addi(simm);                                                    \
1404
    op_ldst(l##width);                                                        \
1405
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1406
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1407
}
1408

    
1409
#define GEN_LDUXF(width, opc)                                                 \
1410
GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
1411
{                                                                             \
1412
    if (rA(ctx->opcode) == 0 ||                                               \
1413
        rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1414
        RET_INVAL(ctx);                                                       \
1415
        return;                                                               \
1416
    }                                                                         \
1417
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1418
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1419
    gen_op_add();                                                             \
1420
    op_ldst(l##width);                                                        \
1421
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1422
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1423
}
1424

    
1425
#define GEN_LDXF(width, opc2, opc3)                                           \
1426
GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
1427
{                                                                             \
1428
    if (rA(ctx->opcode) == 0) {                                               \
1429
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1430
    } else {                                                                  \
1431
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1432
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1433
        gen_op_add();                                                         \
1434
    }                                                                         \
1435
    op_ldst(l##width);                                                        \
1436
    gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1437
}
1438

    
1439
#define GEN_LDFS(width, op)                                                   \
1440
OP_LD_TABLE(width);                                                           \
1441
GEN_LDF(width, op | 0x20);                                                    \
1442
GEN_LDUF(width, op | 0x21);                                                   \
1443
GEN_LDUXF(width, op | 0x01);                                                  \
1444
GEN_LDXF(width, 0x17, op | 0x00)
1445

    
1446
/* lfd lfdu lfdux lfdx */
1447
GEN_LDFS(fd, 0x12);
1448
/* lfs lfsu lfsux lfsx */
1449
GEN_LDFS(fs, 0x10);
1450

    
1451
/***                         Floating-point store                          ***/
1452
#define GEN_STF(width, opc)                                                   \
1453
GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
1454
{                                                                             \
1455
    uint32_t simm = SIMM(ctx->opcode);                                        \
1456
    if (rA(ctx->opcode) == 0) {                                               \
1457
        gen_op_set_T0(simm);                                                  \
1458
    } else {                                                                  \
1459
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1460
        if (simm != 0)                                                        \
1461
            gen_op_addi(simm);                                                \
1462
    }                                                                         \
1463
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1464
    op_ldst(st##width);                                                       \
1465
}
1466

    
1467
#define GEN_STUF(width, opc)                                                  \
1468
GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
1469
{                                                                             \
1470
    uint32_t simm = SIMM(ctx->opcode);                                        \
1471
    if (rA(ctx->opcode) == 0) {                                               \
1472
        RET_INVAL(ctx);                                                       \
1473
        return;                                                               \
1474
    }                                                                         \
1475
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1476
    if (simm != 0)                                                            \
1477
        gen_op_addi(simm);                                                    \
1478
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1479
    op_ldst(st##width);                                                       \
1480
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1481
}
1482

    
1483
#define GEN_STUXF(width, opc)                                                 \
1484
GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
1485
{                                                                             \
1486
    if (rA(ctx->opcode) == 0) {                                               \
1487
        RET_INVAL(ctx);                                                       \
1488
        return;                                                               \
1489
    }                                                                         \
1490
    gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1491
    gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1492
    gen_op_add();                                                             \
1493
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1494
    op_ldst(st##width);                                                       \
1495
    gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1496
}
1497

    
1498
#define GEN_STXF(width, opc2, opc3)                                           \
1499
GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
1500
{                                                                             \
1501
    if (rA(ctx->opcode) == 0) {                                               \
1502
        gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1503
    } else {                                                                  \
1504
        gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1505
        gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1506
        gen_op_add();                                                         \
1507
    }                                                                         \
1508
    gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1509
    op_ldst(st##width);                                                       \
1510
}
1511

    
1512
#define GEN_STFS(width, op)                                                   \
1513
OP_ST_TABLE(width);                                                           \
1514
GEN_STF(width, op | 0x20);                                                    \
1515
GEN_STUF(width, op | 0x21);                                                   \
1516
GEN_STUXF(width, op | 0x01);                                                  \
1517
GEN_STXF(width, 0x17, op | 0x00)
1518

    
1519
/* stfd stfdu stfdux stfdx */
1520
GEN_STFS(fd, 0x16);
1521
/* stfs stfsu stfsux stfsx */
1522
GEN_STFS(fs, 0x14);
1523

    
1524
/* Optional: */
1525
/* stfiwx */
1526
GEN_HANDLER(stfiwx, 0x1F, 0x17, 0x1E, 0x00000001, PPC_FLOAT)
1527
{
1528
    RET_INVAL(ctx);
1529
}
1530

    
1531
/***                                Branch                                 ***/
1532

    
1533
/* b ba bl bla */
1534
GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1535
{
1536
    uint32_t li, target;
1537

    
1538
    /* sign extend LI */
1539
    li = ((int32_t)LI(ctx->opcode) << 6) >> 6;
1540

    
1541
    if (AA(ctx->opcode) == 0)
1542
        target = ctx->nip + li - 4;
1543
    else
1544
        target = li;
1545
    if (LK(ctx->opcode)) {
1546
        gen_op_setlr(ctx->nip);
1547
    }
1548
    gen_op_b((long)ctx->tb, target);
1549
    ctx->exception = EXCP_BRANCH;
1550
}
1551

    
1552
#define BCOND_IM  0
1553
#define BCOND_LR  1
1554
#define BCOND_CTR 2
1555

    
1556
static inline void gen_bcond(DisasContext *ctx, int type) 
1557
{                                                                             
1558
    uint32_t target = 0;
1559
    uint32_t bo = BO(ctx->opcode);                                            
1560
    uint32_t bi = BI(ctx->opcode);                                            
1561
    uint32_t mask;                                                            
1562
    uint32_t li;
1563

    
1564
    if ((bo & 0x4) == 0)
1565
        gen_op_dec_ctr();                                                     
1566
    switch(type) {
1567
    case BCOND_IM:
1568
        li = (int32_t)((int16_t)(BD(ctx->opcode)));
1569
        if (AA(ctx->opcode) == 0) {
1570
            target = ctx->nip + li - 4;
1571
        } else {
1572
            target = li;
1573
        }
1574
        break;
1575
    case BCOND_CTR:
1576
        gen_op_movl_T1_ctr();
1577
        break;
1578
    default:
1579
    case BCOND_LR:
1580
        gen_op_movl_T1_lr();
1581
        break;
1582
    }
1583
    if (LK(ctx->opcode)) {                                        
1584
        gen_op_setlr(ctx->nip);
1585
    }
1586
    if (bo & 0x10) {
1587
        /* No CR condition */                                                 
1588
        switch (bo & 0x6) {                                                   
1589
        case 0:                                                               
1590
            gen_op_test_ctr();
1591
            break;
1592
        case 2:                                                               
1593
            gen_op_test_ctrz();
1594
            break;                                                            
1595
        default:
1596
        case 4:                                                               
1597
        case 6:                                                               
1598
            if (type == BCOND_IM) {
1599
                gen_op_b((long)ctx->tb, target);
1600
            } else {
1601
                gen_op_b_T1();
1602
            }
1603
            goto no_test;
1604
        }
1605
    } else {                                                                  
1606
        mask = 1 << (3 - (bi & 0x03));                                        
1607
        gen_op_load_crf_T0(bi >> 2);                                          
1608
        if (bo & 0x8) {                                                       
1609
            switch (bo & 0x6) {                                               
1610
            case 0:                                                           
1611
                gen_op_test_ctr_true(mask);
1612
                break;                                                        
1613
            case 2:                                                           
1614
                gen_op_test_ctrz_true(mask);
1615
                break;                                                        
1616
            default:                                                          
1617
            case 4:                                                           
1618
            case 6:                                                           
1619
                gen_op_test_true(mask);
1620
                break;                                                        
1621
            }                                                                 
1622
        } else {                                                              
1623
            switch (bo & 0x6) {                                               
1624
            case 0:                                                           
1625
                gen_op_test_ctr_false(mask);
1626
                break;                                                        
1627
            case 2:                                                           
1628
                gen_op_test_ctrz_false(mask);
1629
                break;                                                        
1630
            default:
1631
            case 4:                                                           
1632
            case 6:                                                           
1633
                gen_op_test_false(mask);
1634
                break;                                                        
1635
            }                                                                 
1636
        }                                                                     
1637
    }                                                                         
1638
    if (type == BCOND_IM) {
1639
        gen_op_btest((long)ctx->tb, target, ctx->nip);
1640
    } else {
1641
        gen_op_btest_T1(ctx->nip);
1642
    }
1643
 no_test:
1644
    ctx->exception = EXCP_BRANCH;                                             
1645
}
1646

    
1647
GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1648
{                                                                             
1649
    gen_bcond(ctx, BCOND_IM);
1650
}
1651

    
1652
GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
1653
{                                                                             
1654
    gen_bcond(ctx, BCOND_CTR);
1655
}
1656

    
1657
GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
1658
{                                                                             
1659
    gen_bcond(ctx, BCOND_LR);
1660
}
1661

    
1662
/***                      Condition register logical                       ***/
1663
#define GEN_CRLOGIC(op, opc)                                                  \
1664
GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
1665
{                                                                             \
1666
    gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
1667
    gen_op_getbit_T0(3 - (crbA(ctx->opcode) & 0x03));                         \
1668
    gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
1669
    gen_op_getbit_T1(3 - (crbB(ctx->opcode) & 0x03));                         \
1670
    gen_op_##op();                                                            \
1671
    gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
1672
    gen_op_setcrfbit(~(1 << (3 - (crbD(ctx->opcode) & 0x03))),                \
1673
                     3 - (crbD(ctx->opcode) & 0x03));                         \
1674
    gen_op_store_T1_crf(crbD(ctx->opcode) >> 2);                              \
1675
}
1676

    
1677
/* crand */
1678
GEN_CRLOGIC(and, 0x08)
1679
/* crandc */
1680
GEN_CRLOGIC(andc, 0x04)
1681
/* creqv */
1682
GEN_CRLOGIC(eqv, 0x09)
1683
/* crnand */
1684
GEN_CRLOGIC(nand, 0x07)
1685
/* crnor */
1686
GEN_CRLOGIC(nor, 0x01)
1687
/* cror */
1688
GEN_CRLOGIC(or, 0x0E)
1689
/* crorc */
1690
GEN_CRLOGIC(orc, 0x0D)
1691
/* crxor */
1692
GEN_CRLOGIC(xor, 0x06)
1693
/* mcrf */
1694
GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
1695
{
1696
    gen_op_load_crf_T0(crfS(ctx->opcode));
1697
    gen_op_store_T0_crf(crfD(ctx->opcode));
1698
}
1699

    
1700
/***                           System linkage                              ***/
1701
/* rfi (supervisor only) */
1702
GEN_HANDLER(rfi, 0x13, 0x12, 0xFF, 0x03FF8001, PPC_FLOW)
1703
{
1704
#if defined(CONFIG_USER_ONLY)
1705
    RET_PRIVOPC(ctx);
1706
#else
1707
    /* Restore CPU state */
1708
    if (!ctx->supervisor) {
1709
        RET_PRIVOPC(ctx);
1710
        return;
1711
    }
1712
    gen_op_rfi();
1713
    RET_EXCP(ctx, EXCP_RFI, 0);
1714
#endif
1715
}
1716

    
1717
/* sc */
1718
GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFFFFD, PPC_FLOW)
1719
{
1720
#if defined(CONFIG_USER_ONLY)
1721
    RET_EXCP(ctx, EXCP_SYSCALL_USER, 0);
1722
#else
1723
    RET_EXCP(ctx, EXCP_SYSCALL, 0);
1724
#endif
1725
}
1726

    
1727
/***                                Trap                                   ***/
1728
/* tw */
1729
GEN_HANDLER(tw, 0x1F, 0x04, 0xFF, 0x00000001, PPC_FLOW)
1730
{
1731
    gen_op_load_gpr_T0(rA(ctx->opcode));
1732
    gen_op_load_gpr_T1(rB(ctx->opcode));
1733
    gen_op_tw(TO(ctx->opcode));
1734
}
1735

    
1736
/* twi */
1737
GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1738
{
1739
    gen_op_load_gpr_T0(rA(ctx->opcode));
1740
#if 0
1741
    printf("%s: param=0x%04x T0=0x%04x\n", __func__,
1742
           SIMM(ctx->opcode), TO(ctx->opcode));
1743
#endif
1744
    gen_op_twi(SIMM(ctx->opcode), TO(ctx->opcode));
1745
}
1746

    
1747
/***                          Processor control                            ***/
1748
static inline int check_spr_access (int spr, int rw, int supervisor)
1749
{
1750
    uint32_t rights = spr_access[spr >> 1] >> (4 * (spr & 1));
1751

    
1752
#if 0
1753
    if (spr != LR && spr != CTR) {
1754
    if (loglevel > 0) {
1755
        fprintf(logfile, "%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1756
                SPR_ENCODE(spr), supervisor, rw, rights,
1757
                (rights >> ((2 * supervisor) + rw)) & 1);
1758
    } else {
1759
        printf("%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1760
               SPR_ENCODE(spr), supervisor, rw, rights,
1761
               (rights >> ((2 * supervisor) + rw)) & 1);
1762
    }
1763
    }
1764
#endif
1765
    if (rights == 0)
1766
        return -1;
1767
    rights = rights >> (2 * supervisor);
1768
    rights = rights >> rw;
1769

    
1770
    return rights & 1;
1771
}
1772

    
1773
/* mcrxr */
1774
GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
1775
{
1776
    gen_op_load_xer_cr();
1777
    gen_op_store_T0_crf(crfD(ctx->opcode));
1778
    gen_op_clear_xer_cr();
1779
}
1780

    
1781
/* mfcr */
1782
GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x001FF801, PPC_MISC)
1783
{
1784
    gen_op_load_cr();
1785
    gen_op_store_T0_gpr(rD(ctx->opcode));
1786
}
1787

    
1788
/* mfmsr */
1789
GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
1790
{
1791
#if defined(CONFIG_USER_ONLY)
1792
    RET_PRIVREG(ctx);
1793
#else
1794
    if (!ctx->supervisor) {
1795
        RET_PRIVREG(ctx);
1796
        return;
1797
    }
1798
    gen_op_load_msr();
1799
    gen_op_store_T0_gpr(rD(ctx->opcode));
1800
#endif
1801
}
1802

    
1803
/* mfspr */
1804
GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
1805
{
1806
    uint32_t sprn = SPR(ctx->opcode);
1807

    
1808
#if defined(CONFIG_USER_ONLY)
1809
    switch (check_spr_access(sprn, 0, 0))
1810
#else
1811
    switch (check_spr_access(sprn, 0, ctx->supervisor))
1812
#endif
1813
    {
1814
    case -1:
1815
        RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
1816
        return;
1817
    case 0:
1818
        RET_PRIVREG(ctx);
1819
        return;
1820
    default:
1821
        break;
1822
        }
1823
    switch (sprn) {
1824
    case XER:
1825
        gen_op_load_xer();
1826
        break;
1827
    case LR:
1828
        gen_op_load_lr();
1829
        break;
1830
    case CTR:
1831
        gen_op_load_ctr();
1832
        break;
1833
    case IBAT0U:
1834
        gen_op_load_ibat(0, 0);
1835
        break;
1836
    case IBAT1U:
1837
        gen_op_load_ibat(0, 1);
1838
        break;
1839
    case IBAT2U:
1840
        gen_op_load_ibat(0, 2);
1841
        break;
1842
    case IBAT3U:
1843
        gen_op_load_ibat(0, 3);
1844
        break;
1845
    case IBAT4U:
1846
        gen_op_load_ibat(0, 4);
1847
        break;
1848
    case IBAT5U:
1849
        gen_op_load_ibat(0, 5);
1850
        break;
1851
    case IBAT6U:
1852
        gen_op_load_ibat(0, 6);
1853
        break;
1854
    case IBAT7U:
1855
        gen_op_load_ibat(0, 7);
1856
        break;
1857
    case IBAT0L:
1858
        gen_op_load_ibat(1, 0);
1859
        break;
1860
    case IBAT1L:
1861
        gen_op_load_ibat(1, 1);
1862
        break;
1863
    case IBAT2L:
1864
        gen_op_load_ibat(1, 2);
1865
        break;
1866
    case IBAT3L:
1867
        gen_op_load_ibat(1, 3);
1868
        break;
1869
    case IBAT4L:
1870
        gen_op_load_ibat(1, 4);
1871
        break;
1872
    case IBAT5L:
1873
        gen_op_load_ibat(1, 5);
1874
        break;
1875
    case IBAT6L:
1876
        gen_op_load_ibat(1, 6);
1877
        break;
1878
    case IBAT7L:
1879
        gen_op_load_ibat(1, 7);
1880
        break;
1881
    case DBAT0U:
1882
        gen_op_load_dbat(0, 0);
1883
        break;
1884
    case DBAT1U:
1885
        gen_op_load_dbat(0, 1);
1886
        break;
1887
    case DBAT2U:
1888
        gen_op_load_dbat(0, 2);
1889
        break;
1890
    case DBAT3U:
1891
        gen_op_load_dbat(0, 3);
1892
        break;
1893
    case DBAT4U:
1894
        gen_op_load_dbat(0, 4);
1895
        break;
1896
    case DBAT5U:
1897
        gen_op_load_dbat(0, 5);
1898
        break;
1899
    case DBAT6U:
1900
        gen_op_load_dbat(0, 6);
1901
        break;
1902
    case DBAT7U:
1903
        gen_op_load_dbat(0, 7);
1904
        break;
1905
    case DBAT0L:
1906
        gen_op_load_dbat(1, 0);
1907
        break;
1908
    case DBAT1L:
1909
        gen_op_load_dbat(1, 1);
1910
        break;
1911
    case DBAT2L:
1912
        gen_op_load_dbat(1, 2);
1913
        break;
1914
    case DBAT3L:
1915
        gen_op_load_dbat(1, 3);
1916
        break;
1917
    case DBAT4L:
1918
        gen_op_load_dbat(1, 4);
1919
        break;
1920
    case DBAT5L:
1921
        gen_op_load_dbat(1, 5);
1922
        break;
1923
    case DBAT6L:
1924
        gen_op_load_dbat(1, 6);
1925
        break;
1926
    case DBAT7L:
1927
        gen_op_load_dbat(1, 7);
1928
        break;
1929
    case SDR1:
1930
        gen_op_load_sdr1();
1931
        break;
1932
    case V_TBL:
1933
        gen_op_load_tbl();
1934
        break;
1935
    case V_TBU:
1936
        gen_op_load_tbu();
1937
        break;
1938
    case DECR:
1939
        gen_op_load_decr();
1940
        break;
1941
    default:
1942
        gen_op_load_spr(sprn);
1943
        break;
1944
    }
1945
    gen_op_store_T0_gpr(rD(ctx->opcode));
1946
}
1947

    
1948
/* mftb */
1949
GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MISC)
1950
{
1951
    uint32_t sprn = SPR(ctx->opcode);
1952

    
1953
        /* We need to update the time base before reading it */
1954
    switch (sprn) {
1955
    case V_TBL:
1956
        gen_op_load_tbl();
1957
        break;
1958
    case V_TBU:
1959
        gen_op_load_tbu();
1960
        break;
1961
    default:
1962
        RET_INVAL(ctx);
1963
        return;
1964
    }
1965
    gen_op_store_T0_gpr(rD(ctx->opcode));
1966
}
1967

    
1968
/* mtcrf */
1969
GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00100801, PPC_MISC)
1970
{
1971
    gen_op_load_gpr_T0(rS(ctx->opcode));
1972
    gen_op_store_cr(CRM(ctx->opcode));
1973
}
1974

    
1975
/* mtmsr */
1976
GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
1977
{
1978
#if defined(CONFIG_USER_ONLY)
1979
    RET_PRIVREG(ctx);
1980
#else
1981
    if (!ctx->supervisor) {
1982
        RET_PRIVREG(ctx);
1983
        return;
1984
    }
1985
    gen_op_load_gpr_T0(rS(ctx->opcode));
1986
    gen_op_store_msr();
1987
    /* Must stop the translation as machine state (may have) changed */
1988
    RET_MTMSR(ctx);
1989
#endif
1990
}
1991

    
1992
/* mtspr */
1993
GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
1994
{
1995
    uint32_t sprn = SPR(ctx->opcode);
1996

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

    
2176
/***                         Cache management                              ***/
2177
/* For now, all those will be implemented as nop:
2178
 * this is valid, regarding the PowerPC specs...
2179
 * We just have to flush tb while invalidating instruction cache lines...
2180
 */
2181
/* dcbf */
2182
GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03E00001, PPC_CACHE)
2183
{
2184
    if (rA(ctx->opcode) == 0) {
2185
        gen_op_load_gpr_T0(rB(ctx->opcode));
2186
    } else {
2187
        gen_op_load_gpr_T0(rA(ctx->opcode));
2188
        gen_op_load_gpr_T1(rB(ctx->opcode));
2189
        gen_op_add();
2190
    }
2191
    op_ldst(lbz);
2192
}
2193

    
2194
/* dcbi (Supervisor only) */
2195
GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
2196
{
2197
#if defined(CONFIG_USER_ONLY)
2198
    RET_PRIVOPC(ctx);
2199
#else
2200
    if (!ctx->supervisor) {
2201
        RET_PRIVOPC(ctx);
2202
        return;
2203
    }
2204
    if (rA(ctx->opcode) == 0) {
2205
        gen_op_load_gpr_T0(rB(ctx->opcode));
2206
    } else {
2207
        gen_op_load_gpr_T0(rA(ctx->opcode));
2208
        gen_op_load_gpr_T1(rB(ctx->opcode));
2209
        gen_op_add();
2210
    }
2211
    op_ldst(lbz);
2212
    op_ldst(stb);
2213
#endif
2214
}
2215

    
2216
/* dcdst */
2217
GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
2218
{
2219
    if (rA(ctx->opcode) == 0) {
2220
        gen_op_load_gpr_T0(rB(ctx->opcode));
2221
    } else {
2222
        gen_op_load_gpr_T0(rA(ctx->opcode));
2223
        gen_op_load_gpr_T1(rB(ctx->opcode));
2224
        gen_op_add();
2225
    }
2226
    op_ldst(lbz);
2227
}
2228

    
2229
/* dcbt */
2230
GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x03E00001, PPC_CACHE)
2231
{
2232
}
2233

    
2234
/* dcbtst */
2235
GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE)
2236
{
2237
}
2238

    
2239
/* dcbz */
2240
#if defined(CONFIG_USER_ONLY)
2241
#define op_dcbz() gen_op_dcbz_raw()
2242
#else
2243
#define op_dcbz() (*gen_op_dcbz[ctx->mem_idx])()
2244
static GenOpFunc *gen_op_dcbz[] = {
2245
    &gen_op_dcbz_user,
2246
    &gen_op_dcbz_kernel,
2247
};
2248
#endif
2249

    
2250
GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE)
2251
{
2252
    if (rA(ctx->opcode) == 0) {
2253
        gen_op_load_gpr_T0(rB(ctx->opcode));
2254
    } else {
2255
        gen_op_load_gpr_T0(rA(ctx->opcode));
2256
        gen_op_load_gpr_T1(rB(ctx->opcode));
2257
        gen_op_add();
2258
    }
2259
    op_dcbz();
2260
    gen_op_check_reservation();
2261
}
2262

    
2263
/* icbi */
2264
GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
2265
{
2266
    if (rA(ctx->opcode) == 0) {
2267
        gen_op_load_gpr_T0(rB(ctx->opcode));
2268
    } else {
2269
        gen_op_load_gpr_T0(rA(ctx->opcode));
2270
        gen_op_load_gpr_T1(rB(ctx->opcode));
2271
        gen_op_add();
2272
    }
2273
    gen_op_icbi();
2274
}
2275

    
2276
/* Optional: */
2277
/* dcba */
2278
GEN_HANDLER(dcba, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE_OPT)
2279
{
2280
}
2281

    
2282
/***                    Segment register manipulation                      ***/
2283
/* Supervisor only: */
2284
/* mfsr */
2285
GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
2286
{
2287
#if defined(CONFIG_USER_ONLY)
2288
    RET_PRIVREG(ctx);
2289
#else
2290
    if (!ctx->supervisor) {
2291
        RET_PRIVREG(ctx);
2292
        return;
2293
    }
2294
    gen_op_load_sr(SR(ctx->opcode));
2295
    gen_op_store_T0_gpr(rD(ctx->opcode));
2296
#endif
2297
}
2298

    
2299
/* mfsrin */
2300
GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
2301
{
2302
#if defined(CONFIG_USER_ONLY)
2303
    RET_PRIVREG(ctx);
2304
#else
2305
    if (!ctx->supervisor) {
2306
        RET_PRIVREG(ctx);
2307
        return;
2308
    }
2309
    gen_op_load_gpr_T1(rB(ctx->opcode));
2310
    gen_op_load_srin();
2311
    gen_op_store_T0_gpr(rD(ctx->opcode));
2312
#endif
2313
}
2314

    
2315
/* mtsr */
2316
GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
2317
{
2318
#if defined(CONFIG_USER_ONLY)
2319
    RET_PRIVREG(ctx);
2320
#else
2321
    if (!ctx->supervisor) {
2322
        RET_PRIVREG(ctx);
2323
        return;
2324
    }
2325
    gen_op_load_gpr_T0(rS(ctx->opcode));
2326
    gen_op_store_sr(SR(ctx->opcode));
2327
#endif
2328
}
2329

    
2330
/* mtsrin */
2331
GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
2332
{
2333
#if defined(CONFIG_USER_ONLY)
2334
    RET_PRIVREG(ctx);
2335
#else
2336
    if (!ctx->supervisor) {
2337
        RET_PRIVREG(ctx);
2338
        return;
2339
    }
2340
    gen_op_load_gpr_T0(rS(ctx->opcode));
2341
    gen_op_load_gpr_T1(rB(ctx->opcode));
2342
    gen_op_store_srin();
2343
#endif
2344
}
2345

    
2346
/***                      Lookaside buffer management                      ***/
2347
/* Optional & supervisor only: */
2348
/* tlbia */
2349
GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_OPT)
2350
{
2351
#if defined(CONFIG_USER_ONLY)
2352
    RET_PRIVOPC(ctx);
2353
#else
2354
    if (!ctx->supervisor) {
2355
        if (loglevel)
2356
            fprintf(logfile, "%s: ! supervisor\n", __func__);
2357
        RET_PRIVOPC(ctx);
2358
        return;
2359
    }
2360
    gen_op_tlbia();
2361
    RET_MTMSR(ctx);
2362
#endif
2363
}
2364

    
2365
/* tlbie */
2366
GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM)
2367
{
2368
#if defined(CONFIG_USER_ONLY)
2369
    RET_PRIVOPC(ctx);
2370
#else
2371
    if (!ctx->supervisor) {
2372
        RET_PRIVOPC(ctx);
2373
        return;
2374
    }
2375
    gen_op_load_gpr_T0(rB(ctx->opcode));
2376
    gen_op_tlbie();
2377
    RET_MTMSR(ctx);
2378
#endif
2379
}
2380

    
2381
/* tlbsync */
2382
GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM)
2383
{
2384
#if defined(CONFIG_USER_ONLY)
2385
    RET_PRIVOPC(ctx);
2386
#else
2387
    if (!ctx->supervisor) {
2388
        RET_PRIVOPC(ctx);
2389
        return;
2390
    }
2391
    /* This has no effect: it should ensure that all previous
2392
     * tlbie have completed
2393
     */
2394
    RET_MTMSR(ctx);
2395
#endif
2396
}
2397

    
2398
/***                              External control                         ***/
2399
/* Optional: */
2400
/* eciwx */
2401
#if defined(CONFIG_USER_ONLY)
2402
#define op_eciwx() gen_op_eciwx_raw()
2403
#define op_ecowx() gen_op_ecowx_raw()
2404
#else
2405
#define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
2406
#define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
2407
static GenOpFunc *gen_op_eciwx[] = {
2408
    &gen_op_eciwx_user,
2409
    &gen_op_eciwx_kernel,
2410
};
2411
static GenOpFunc *gen_op_ecowx[] = {
2412
    &gen_op_ecowx_user,
2413
    &gen_op_ecowx_kernel,
2414
};
2415
#endif
2416

    
2417
GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
2418
{
2419
    /* Should check EAR[E] & alignment ! */
2420
    if (rA(ctx->opcode) == 0) {
2421
        gen_op_load_gpr_T0(rB(ctx->opcode));
2422
    } else {
2423
        gen_op_load_gpr_T0(rA(ctx->opcode));
2424
        gen_op_load_gpr_T1(rB(ctx->opcode));
2425
        gen_op_add();
2426
    }
2427
    op_eciwx();
2428
    gen_op_store_T0_gpr(rD(ctx->opcode));
2429
}
2430

    
2431
/* ecowx */
2432
GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
2433
{
2434
    /* Should check EAR[E] & alignment ! */
2435
    if (rA(ctx->opcode) == 0) {
2436
        gen_op_load_gpr_T0(rB(ctx->opcode));
2437
    } else {
2438
        gen_op_load_gpr_T0(rA(ctx->opcode));
2439
        gen_op_load_gpr_T1(rB(ctx->opcode));
2440
        gen_op_add();
2441
    }
2442
    gen_op_load_gpr_T2(rS(ctx->opcode));
2443
    op_ecowx();
2444
}
2445

    
2446
/* End opcode list */
2447
GEN_OPCODE_MARK(end);
2448

    
2449
/*****************************************************************************/
2450
#include <stdlib.h>
2451
#include <string.h>
2452

    
2453
int fflush (FILE *stream);
2454

    
2455
/* Main ppc opcodes table:
2456
 * at init, all opcodes are invalids
2457
 */
2458
static opc_handler_t *ppc_opcodes[0x40];
2459

    
2460
/* Opcode types */
2461
enum {
2462
    PPC_DIRECT   = 0, /* Opcode routine        */
2463
    PPC_INDIRECT = 1, /* Indirect opcode table */
2464
};
2465

    
2466
static inline int is_indirect_opcode (void *handler)
2467
{
2468
    return ((unsigned long)handler & 0x03) == PPC_INDIRECT;
2469
}
2470

    
2471
static inline opc_handler_t **ind_table(void *handler)
2472
{
2473
    return (opc_handler_t **)((unsigned long)handler & ~3);
2474
}
2475

    
2476
/* Instruction table creation */
2477
/* Opcodes tables creation */
2478
static void fill_new_table (opc_handler_t **table, int len)
2479
{
2480
    int i;
2481

    
2482
    for (i = 0; i < len; i++)
2483
        table[i] = &invalid_handler;
2484
}
2485

    
2486
static int create_new_table (opc_handler_t **table, unsigned char idx)
2487
{
2488
    opc_handler_t **tmp;
2489

    
2490
    tmp = malloc(0x20 * sizeof(opc_handler_t));
2491
    if (tmp == NULL)
2492
        return -1;
2493
    fill_new_table(tmp, 0x20);
2494
    table[idx] = (opc_handler_t *)((unsigned long)tmp | PPC_INDIRECT);
2495

    
2496
    return 0;
2497
}
2498

    
2499
static int insert_in_table (opc_handler_t **table, unsigned char idx,
2500
                            opc_handler_t *handler)
2501
{
2502
    if (table[idx] != &invalid_handler)
2503
        return -1;
2504
    table[idx] = handler;
2505

    
2506
    return 0;
2507
}
2508

    
2509
static int register_direct_insn (opc_handler_t **ppc_opcodes,
2510
                                 unsigned char idx, opc_handler_t *handler)
2511
{
2512
    if (insert_in_table(ppc_opcodes, idx, handler) < 0) {
2513
        printf("*** ERROR: opcode %02x already assigned in main "
2514
                "opcode table\n", idx);
2515
        return -1;
2516
    }
2517

    
2518
    return 0;
2519
}
2520

    
2521
static int register_ind_in_table (opc_handler_t **table,
2522
                                  unsigned char idx1, unsigned char idx2,
2523
                                  opc_handler_t *handler)
2524
{
2525
    if (table[idx1] == &invalid_handler) {
2526
        if (create_new_table(table, idx1) < 0) {
2527
            printf("*** ERROR: unable to create indirect table "
2528
                    "idx=%02x\n", idx1);
2529
            return -1;
2530
        }
2531
    } else {
2532
        if (!is_indirect_opcode(table[idx1])) {
2533
            printf("*** ERROR: idx %02x already assigned to a direct "
2534
                    "opcode\n", idx1);
2535
            return -1;
2536
        }
2537
    }
2538
    if (handler != NULL &&
2539
        insert_in_table(ind_table(table[idx1]), idx2, handler) < 0) {
2540
        printf("*** ERROR: opcode %02x already assigned in "
2541
                "opcode table %02x\n", idx2, idx1);
2542
        return -1;
2543
    }
2544

    
2545
    return 0;
2546
}
2547

    
2548
static int register_ind_insn (opc_handler_t **ppc_opcodes,
2549
                              unsigned char idx1, unsigned char idx2,
2550
                               opc_handler_t *handler)
2551
{
2552
    int ret;
2553

    
2554
    ret = register_ind_in_table(ppc_opcodes, idx1, idx2, handler);
2555

    
2556
    return ret;
2557
}
2558

    
2559
static int register_dblind_insn (opc_handler_t **ppc_opcodes, 
2560
                                 unsigned char idx1, unsigned char idx2,
2561
                                  unsigned char idx3, opc_handler_t *handler)
2562
{
2563
    if (register_ind_in_table(ppc_opcodes, idx1, idx2, NULL) < 0) {
2564
        printf("*** ERROR: unable to join indirect table idx "
2565
                "[%02x-%02x]\n", idx1, idx2);
2566
        return -1;
2567
    }
2568
    if (register_ind_in_table(ind_table(ppc_opcodes[idx1]), idx2, idx3,
2569
                              handler) < 0) {
2570
        printf("*** ERROR: unable to insert opcode "
2571
                "[%02x-%02x-%02x]\n", idx1, idx2, idx3);
2572
        return -1;
2573
    }
2574

    
2575
    return 0;
2576
}
2577

    
2578
static int register_insn (opc_handler_t **ppc_opcodes, opcode_t *insn)
2579
{
2580
    if (insn->opc2 != 0xFF) {
2581
        if (insn->opc3 != 0xFF) {
2582
            if (register_dblind_insn(ppc_opcodes, insn->opc1, insn->opc2,
2583
                                     insn->opc3, &insn->handler) < 0)
2584
                return -1;
2585
        } else {
2586
            if (register_ind_insn(ppc_opcodes, insn->opc1,
2587
                                  insn->opc2, &insn->handler) < 0)
2588
                return -1;
2589
        }
2590
    } else {
2591
        if (register_direct_insn(ppc_opcodes, insn->opc1, &insn->handler) < 0)
2592
            return -1;
2593
    }
2594

    
2595
    return 0;
2596
}
2597

    
2598
static int test_opcode_table (opc_handler_t **table, int len)
2599
{
2600
    int i, count, tmp;
2601

    
2602
    for (i = 0, count = 0; i < len; i++) {
2603
        /* Consistency fixup */
2604
        if (table[i] == NULL)
2605
            table[i] = &invalid_handler;
2606
        if (table[i] != &invalid_handler) {
2607
            if (is_indirect_opcode(table[i])) {
2608
                tmp = test_opcode_table(ind_table(table[i]), 0x20);
2609
                if (tmp == 0) {
2610
                    free(table[i]);
2611
                    table[i] = &invalid_handler;
2612
                } else {
2613
                    count++;
2614
                }
2615
            } else {
2616
                count++;
2617
            }
2618
        }
2619
    }
2620

    
2621
    return count;
2622
}
2623

    
2624
static void fix_opcode_tables (opc_handler_t **ppc_opcodes)
2625
{
2626
    if (test_opcode_table(ppc_opcodes, 0x40) == 0)
2627
        printf("*** WARNING: no opcode defined !\n");
2628
}
2629

    
2630
#define SPR_RIGHTS(rw, priv) (1 << ((2 * (priv)) + (rw)))
2631
#define SPR_UR SPR_RIGHTS(0, 0)
2632
#define SPR_UW SPR_RIGHTS(1, 0)
2633
#define SPR_SR SPR_RIGHTS(0, 1)
2634
#define SPR_SW SPR_RIGHTS(1, 1)
2635

    
2636
#define spr_set_rights(spr, rights)                            \
2637
do {                                                           \
2638
    spr_access[(spr) >> 1] |= ((rights) << (4 * ((spr) & 1))); \
2639
} while (0)
2640

    
2641
static void init_spr_rights (uint32_t pvr)
2642
{
2643
    /* XER    (SPR 1) */
2644
    spr_set_rights(XER,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2645
    /* LR     (SPR 8) */
2646
    spr_set_rights(LR,     SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2647
    /* CTR    (SPR 9) */
2648
    spr_set_rights(CTR,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2649
    /* TBL    (SPR 268) */
2650
    spr_set_rights(V_TBL,  SPR_UR | SPR_SR);
2651
    /* TBU    (SPR 269) */
2652
    spr_set_rights(V_TBU,  SPR_UR | SPR_SR);
2653
    /* DSISR  (SPR 18) */
2654
    spr_set_rights(DSISR,  SPR_SR | SPR_SW);
2655
    /* DAR    (SPR 19) */
2656
    spr_set_rights(DAR,    SPR_SR | SPR_SW);
2657
    /* DEC    (SPR 22) */
2658
    spr_set_rights(DECR,   SPR_SR | SPR_SW);
2659
    /* SDR1   (SPR 25) */
2660
    spr_set_rights(SDR1,   SPR_SR | SPR_SW);
2661
    /* SRR0   (SPR 26) */
2662
    spr_set_rights(SRR0,   SPR_SR | SPR_SW);
2663
    /* SRR1   (SPR 27) */
2664
    spr_set_rights(SRR1,   SPR_SR | SPR_SW);
2665
    /* SPRG0  (SPR 272) */
2666
    spr_set_rights(SPRG0,  SPR_SR | SPR_SW);
2667
    /* SPRG1  (SPR 273) */
2668
    spr_set_rights(SPRG1,  SPR_SR | SPR_SW);
2669
    /* SPRG2  (SPR 274) */
2670
    spr_set_rights(SPRG2,  SPR_SR | SPR_SW);
2671
    /* SPRG3  (SPR 275) */
2672
    spr_set_rights(SPRG3,  SPR_SR | SPR_SW);
2673
    /* ASR    (SPR 280) */
2674
    spr_set_rights(ASR,    SPR_SR | SPR_SW);
2675
    /* EAR    (SPR 282) */
2676
    spr_set_rights(EAR,    SPR_SR | SPR_SW);
2677
    /* TBL    (SPR 284) */
2678
    spr_set_rights(O_TBL,  SPR_SW);
2679
    /* TBU    (SPR 285) */
2680
    spr_set_rights(O_TBU,  SPR_SW);
2681
    /* PVR    (SPR 287) */
2682
    spr_set_rights(PVR,    SPR_SR);
2683
    /* IBAT0U (SPR 528) */
2684
    spr_set_rights(IBAT0U, SPR_SR | SPR_SW);
2685
    /* IBAT0L (SPR 529) */
2686
    spr_set_rights(IBAT0L, SPR_SR | SPR_SW);
2687
    /* IBAT1U (SPR 530) */
2688
    spr_set_rights(IBAT1U, SPR_SR | SPR_SW);
2689
    /* IBAT1L (SPR 531) */
2690
    spr_set_rights(IBAT1L, SPR_SR | SPR_SW);
2691
    /* IBAT2U (SPR 532) */
2692
    spr_set_rights(IBAT2U, SPR_SR | SPR_SW);
2693
    /* IBAT2L (SPR 533) */
2694
    spr_set_rights(IBAT2L, SPR_SR | SPR_SW);
2695
    /* IBAT3U (SPR 534) */
2696
    spr_set_rights(IBAT3U, SPR_SR | SPR_SW);
2697
    /* IBAT3L (SPR 535) */
2698
    spr_set_rights(IBAT3L, SPR_SR | SPR_SW);
2699
    /* DBAT0U (SPR 536) */
2700
    spr_set_rights(DBAT0U, SPR_SR | SPR_SW);
2701
    /* DBAT0L (SPR 537) */
2702
    spr_set_rights(DBAT0L, SPR_SR | SPR_SW);
2703
    /* DBAT1U (SPR 538) */
2704
    spr_set_rights(DBAT1U, SPR_SR | SPR_SW);
2705
    /* DBAT1L (SPR 539) */
2706
    spr_set_rights(DBAT1L, SPR_SR | SPR_SW);
2707
    /* DBAT2U (SPR 540) */
2708
    spr_set_rights(DBAT2U, SPR_SR | SPR_SW);
2709
    /* DBAT2L (SPR 541) */
2710
    spr_set_rights(DBAT2L, SPR_SR | SPR_SW);
2711
    /* DBAT3U (SPR 542) */
2712
    spr_set_rights(DBAT3U, SPR_SR | SPR_SW);
2713
    /* DBAT3L (SPR 543) */
2714
    spr_set_rights(DBAT3L, SPR_SR | SPR_SW);
2715
    /* FPECR  (SPR 1022) */
2716
    spr_set_rights(FPECR,  SPR_SR | SPR_SW);
2717
    /* Special registers for PPC 604 */
2718
    if ((pvr & 0xFFFF0000) == 0x00040000) {
2719
        /* IABR */
2720
        spr_set_rights(IABR ,  SPR_SR | SPR_SW);
2721
        /* DABR   (SPR 1013) */
2722
        spr_set_rights(DABR,   SPR_SR | SPR_SW);
2723
        /* HID0 */
2724
        spr_set_rights(HID0,   SPR_SR | SPR_SW);
2725
        /* PIR */
2726
    spr_set_rights(PIR,    SPR_SR | SPR_SW);
2727
        /* PMC1 */
2728
        spr_set_rights(PMC1,   SPR_SR | SPR_SW);
2729
        /* PMC2 */
2730
        spr_set_rights(PMC2,   SPR_SR | SPR_SW);
2731
        /* MMCR0 */
2732
        spr_set_rights(MMCR0,  SPR_SR | SPR_SW);
2733
        /* SIA */
2734
        spr_set_rights(SIA,    SPR_SR | SPR_SW);
2735
        /* SDA */
2736
        spr_set_rights(SDA,    SPR_SR | SPR_SW);
2737
    }
2738
    /* Special registers for MPC740/745/750/755 (aka G3) & IBM 750 */
2739
    if ((pvr & 0xFFFF0000) == 0x00080000 ||
2740
        (pvr & 0xFFFF0000) == 0x70000000) {
2741
        /* HID0 */
2742
        spr_set_rights(HID0,   SPR_SR | SPR_SW);
2743
        /* HID1 */
2744
        spr_set_rights(HID1,   SPR_SR | SPR_SW);
2745
        /* IABR */
2746
        spr_set_rights(IABR,   SPR_SR | SPR_SW);
2747
        /* ICTC */
2748
        spr_set_rights(ICTC,   SPR_SR | SPR_SW);
2749
        /* L2CR */
2750
        spr_set_rights(L2CR,   SPR_SR | SPR_SW);
2751
        /* MMCR0 */
2752
        spr_set_rights(MMCR0,  SPR_SR | SPR_SW);
2753
        /* MMCR1 */
2754
        spr_set_rights(MMCR1,  SPR_SR | SPR_SW);
2755
        /* PMC1 */
2756
        spr_set_rights(PMC1,   SPR_SR | SPR_SW);
2757
        /* PMC2 */
2758
        spr_set_rights(PMC2,   SPR_SR | SPR_SW);
2759
        /* PMC3 */
2760
        spr_set_rights(PMC3,   SPR_SR | SPR_SW);
2761
        /* PMC4 */
2762
        spr_set_rights(PMC4,   SPR_SR | SPR_SW);
2763
        /* SIA */
2764
        spr_set_rights(SIA,    SPR_SR | SPR_SW);
2765
        /* SDA */
2766
        spr_set_rights(SDA,    SPR_SR | SPR_SW);
2767
        /* THRM1 */
2768
        spr_set_rights(THRM1,  SPR_SR | SPR_SW);
2769
        /* THRM2 */
2770
        spr_set_rights(THRM2,  SPR_SR | SPR_SW);
2771
        /* THRM3 */
2772
        spr_set_rights(THRM3,  SPR_SR | SPR_SW);
2773
        /* UMMCR0 */
2774
        spr_set_rights(UMMCR0, SPR_UR | SPR_UW);
2775
        /* UMMCR1 */
2776
        spr_set_rights(UMMCR1, SPR_UR | SPR_UW);
2777
        /* UPMC1 */
2778
        spr_set_rights(UPMC1,  SPR_UR | SPR_UW);
2779
        /* UPMC2 */
2780
        spr_set_rights(UPMC2,  SPR_UR | SPR_UW);
2781
        /* UPMC3 */
2782
        spr_set_rights(UPMC3,  SPR_UR | SPR_UW);
2783
        /* UPMC4 */
2784
        spr_set_rights(UPMC4,  SPR_UR | SPR_UW);
2785
        /* USIA */
2786
        spr_set_rights(USIA,   SPR_UR | SPR_UW);
2787
    }
2788
    /* MPC755 has special registers */
2789
    if (pvr == 0x00083100) {
2790
        /* SPRG4 */
2791
        spr_set_rights(SPRG4, SPR_SR | SPR_SW);
2792
        /* SPRG5 */
2793
        spr_set_rights(SPRG5, SPR_SR | SPR_SW);
2794
        /* SPRG6 */
2795
        spr_set_rights(SPRG6, SPR_SR | SPR_SW);
2796
        /* SPRG7 */
2797
        spr_set_rights(SPRG7, SPR_SR | SPR_SW);
2798
        /* IBAT4U */
2799
        spr_set_rights(IBAT4U, SPR_SR | SPR_SW);
2800
        /* IBAT4L */
2801
        spr_set_rights(IBAT4L, SPR_SR | SPR_SW);
2802
        /* IBAT5U */
2803
        spr_set_rights(IBAT5U, SPR_SR | SPR_SW);
2804
        /* IBAT5L */
2805
        spr_set_rights(IBAT5L, SPR_SR | SPR_SW);
2806
        /* IBAT6U */
2807
        spr_set_rights(IBAT6U, SPR_SR | SPR_SW);
2808
        /* IBAT6L */
2809
        spr_set_rights(IBAT6L, SPR_SR | SPR_SW);
2810
        /* IBAT7U */
2811
        spr_set_rights(IBAT7U, SPR_SR | SPR_SW);
2812
        /* IBAT7L */
2813
        spr_set_rights(IBAT7L, SPR_SR | SPR_SW);
2814
        /* DBAT4U */
2815
        spr_set_rights(DBAT4U, SPR_SR | SPR_SW);
2816
        /* DBAT4L */
2817
        spr_set_rights(DBAT4L, SPR_SR | SPR_SW);
2818
        /* DBAT5U */
2819
        spr_set_rights(DBAT5U, SPR_SR | SPR_SW);
2820
        /* DBAT5L */
2821
        spr_set_rights(DBAT5L, SPR_SR | SPR_SW);
2822
        /* DBAT6U */
2823
        spr_set_rights(DBAT6U, SPR_SR | SPR_SW);
2824
        /* DBAT6L */
2825
        spr_set_rights(DBAT6L, SPR_SR | SPR_SW);
2826
        /* DBAT7U */
2827
        spr_set_rights(DBAT7U, SPR_SR | SPR_SW);
2828
        /* DBAT7L */
2829
        spr_set_rights(DBAT7L, SPR_SR | SPR_SW);
2830
        /* DMISS */
2831
        spr_set_rights(DMISS,  SPR_SR | SPR_SW);
2832
        /* DCMP */
2833
        spr_set_rights(DCMP,   SPR_SR | SPR_SW);
2834
        /* DHASH1 */
2835
        spr_set_rights(DHASH1, SPR_SR | SPR_SW);
2836
        /* DHASH2 */
2837
        spr_set_rights(DHASH2, SPR_SR | SPR_SW);
2838
        /* IMISS */
2839
        spr_set_rights(IMISS,  SPR_SR | SPR_SW);
2840
        /* ICMP */
2841
        spr_set_rights(ICMP,   SPR_SR | SPR_SW);
2842
        /* RPA */
2843
        spr_set_rights(RPA,    SPR_SR | SPR_SW);
2844
        /* HID2 */
2845
        spr_set_rights(HID2,   SPR_SR | SPR_SW);
2846
        /* L2PM */
2847
        spr_set_rights(L2PM,   SPR_SR | SPR_SW);
2848
    }
2849
}
2850

    
2851
/*****************************************************************************/
2852
/* PPC "main stream" common instructions (no optional ones) */
2853

    
2854
typedef struct ppc_proc_t {
2855
    int flags;
2856
    void *specific;
2857
} ppc_proc_t;
2858

    
2859
typedef struct ppc_def_t {
2860
    unsigned long pvr;
2861
    unsigned long pvr_mask;
2862
    ppc_proc_t *proc;
2863
} ppc_def_t;
2864

    
2865
static ppc_proc_t ppc_proc_common = {
2866
    .flags    = PPC_COMMON,
2867
    .specific = NULL,
2868
};
2869

    
2870
static ppc_proc_t ppc_proc_G3 = {
2871
    .flags    = PPC_750,
2872
    .specific = NULL,
2873
};
2874

    
2875
static ppc_def_t ppc_defs[] =
2876
{
2877
    /* MPC740/745/750/755 (G3) */
2878
    {
2879
        .pvr      = 0x00080000,
2880
        .pvr_mask = 0xFFFF0000,
2881
        .proc     = &ppc_proc_G3,
2882
    },
2883
    /* IBM 750FX (G3 embedded) */
2884
    {
2885
        .pvr      = 0x70000000,
2886
        .pvr_mask = 0xFFFF0000,
2887
        .proc     = &ppc_proc_G3,
2888
    },
2889
    /* Fallback (generic PPC) */
2890
    {
2891
        .pvr      = 0x00000000,
2892
        .pvr_mask = 0x00000000,
2893
        .proc     = &ppc_proc_common,
2894
    },
2895
};
2896

    
2897
static int create_ppc_proc (opc_handler_t **ppc_opcodes, unsigned long pvr)
2898
{
2899
    opcode_t *opc, *start, *end;
2900
    int i, flags;
2901

    
2902
    fill_new_table(ppc_opcodes, 0x40);
2903
    for (i = 0; ; i++) {
2904
        if ((ppc_defs[i].pvr & ppc_defs[i].pvr_mask) ==
2905
            (pvr & ppc_defs[i].pvr_mask)) {
2906
            flags = ppc_defs[i].proc->flags;
2907
            break;
2908
        }
2909
    }
2910
    
2911
    if (&opc_start < &opc_end) {
2912
        start = &opc_start;
2913
        end = &opc_end;
2914
    } else {
2915
        start = &opc_end;
2916
        end = &opc_start;
2917
    }
2918
    for (opc = start + 1; opc != end; opc++) {
2919
        if ((opc->handler.type & flags) != 0)
2920
            if (register_insn(ppc_opcodes, opc) < 0) {
2921
                printf("*** ERROR initializing PPC instruction "
2922
                        "0x%02x 0x%02x 0x%02x\n", opc->opc1, opc->opc2,
2923
                        opc->opc3);
2924
                return -1;
2925
            }
2926
    }
2927
    fix_opcode_tables(ppc_opcodes);
2928

    
2929
    return 0;
2930
}
2931

    
2932

    
2933
/*****************************************************************************/
2934
/* Misc PPC helpers */
2935

    
2936
void cpu_dump_state(CPUState *env, FILE *f, 
2937
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
2938
                    int flags)
2939
{
2940
    int i;
2941

    
2942
    cpu_fprintf(f, "nip=0x%08x LR=0x%08x CTR=0x%08x XER=0x%08x "
2943
            "MSR=0x%08x\n", env->nip, env->lr, env->ctr,
2944
            _load_xer(env), _load_msr(env));
2945
        for (i = 0; i < 32; i++) {
2946
            if ((i & 7) == 0)
2947
            cpu_fprintf(f, "GPR%02d:", i);
2948
        cpu_fprintf(f, " %08x", env->gpr[i]);
2949
            if ((i & 7) == 7)
2950
            cpu_fprintf(f, "\n");
2951
        }
2952
    cpu_fprintf(f, "CR: 0x");
2953
        for (i = 0; i < 8; i++)
2954
        cpu_fprintf(f, "%01x", env->crf[i]);
2955
    cpu_fprintf(f, "  [");
2956
        for (i = 0; i < 8; i++) {
2957
            char a = '-';
2958
            if (env->crf[i] & 0x08)
2959
                a = 'L';
2960
            else if (env->crf[i] & 0x04)
2961
                a = 'G';
2962
            else if (env->crf[i] & 0x02)
2963
                a = 'E';
2964
        cpu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
2965
        }
2966
    cpu_fprintf(f, " ] ");
2967
    cpu_fprintf(f, "TB: 0x%08x %08x\n", cpu_ppc_load_tbu(env),
2968
            cpu_ppc_load_tbl(env));
2969
        for (i = 0; i < 16; i++) {
2970
            if ((i & 3) == 0)
2971
            cpu_fprintf(f, "FPR%02d:", i);
2972
        cpu_fprintf(f, " %016llx", *((uint64_t *)&env->fpr[i]));
2973
            if ((i & 3) == 3)
2974
            cpu_fprintf(f, "\n");
2975
    }
2976
    cpu_fprintf(f, "SRR0 0x%08x SRR1 0x%08x DECR=0x%08x\n",
2977
            env->spr[SRR0], env->spr[SRR1], cpu_ppc_load_decr(env));
2978
    cpu_fprintf(f, "reservation 0x%08x\n", env->reserve);
2979
}
2980

    
2981
#if !defined(CONFIG_USER_ONLY) && defined (USE_OPENFIRMWARE)
2982
int setup_machine (CPUPPCState *env, uint32_t mid);
2983
#endif
2984

    
2985
CPUPPCState *cpu_ppc_init(void)
2986
{
2987
    CPUPPCState *env;
2988

    
2989
    cpu_exec_init();
2990

    
2991
    env = qemu_mallocz(sizeof(CPUPPCState));
2992
    if (!env)
2993
        return NULL;
2994
#if !defined(CONFIG_USER_ONLY) && defined (USE_OPEN_FIRMWARE)
2995
    setup_machine(env, 0);
2996
#else
2997
//    env->spr[PVR] = 0; /* Basic PPC */
2998
    env->spr[PVR] = 0x00080100; /* G3 CPU */
2999
//    env->spr[PVR] = 0x00083100; /* MPC755 (G3 embedded) */
3000
//    env->spr[PVR] = 0x00070100; /* IBM 750FX */
3001
#endif
3002
    tlb_flush(env, 1);
3003
#if defined (DO_SINGLE_STEP)
3004
    /* Single step trace mode */
3005
    msr_se = 1;
3006
#endif
3007
    msr_fp = 1; /* Allow floating point exceptions */
3008
    msr_me = 1; /* Allow machine check exceptions  */
3009
#if defined(CONFIG_USER_ONLY)
3010
    msr_pr = 1;
3011
    cpu_ppc_register(env, 0x00080000);
3012
#else
3013
    env->nip = 0xFFFFFFFC;
3014
#endif
3015
    cpu_single_env = env;
3016
    return env;
3017
}
3018

    
3019
int cpu_ppc_register (CPUPPCState *env, uint32_t pvr)
3020
{
3021
    env->spr[PVR] = pvr;
3022
    if (create_ppc_proc(ppc_opcodes, env->spr[PVR]) < 0)
3023
        return -1;
3024
    init_spr_rights(env->spr[PVR]);
3025

    
3026
    return 0;
3027
}
3028

    
3029
void cpu_ppc_close(CPUPPCState *env)
3030
{
3031
    /* Should also remove all opcode tables... */
3032
    free(env);
3033
}
3034

    
3035
/*****************************************************************************/
3036
int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
3037
                                    int search_pc)
3038
{
3039
    DisasContext ctx, *ctxp = &ctx;
3040
    opc_handler_t **table, *handler;
3041
    target_ulong pc_start;
3042
    uint16_t *gen_opc_end;
3043
    int j, lj = -1;
3044

    
3045
    pc_start = tb->pc;
3046
    gen_opc_ptr = gen_opc_buf;
3047
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
3048
    gen_opparam_ptr = gen_opparam_buf;
3049
    ctx.nip = pc_start;
3050
    ctx.tb = tb;
3051
    ctx.exception = EXCP_NONE;
3052
#if defined(CONFIG_USER_ONLY)
3053
    ctx.mem_idx = 0;
3054
#else
3055
    ctx.supervisor = 1 - msr_pr;
3056
    ctx.mem_idx = (1 - msr_pr);
3057
#endif
3058
#if defined (DO_SINGLE_STEP)
3059
    /* Single step trace mode */
3060
    msr_se = 1;
3061
#endif
3062
    /* Set env in case of segfault during code fetch */
3063
    while (ctx.exception == EXCP_NONE && gen_opc_ptr < gen_opc_end) {
3064
        if (search_pc) {
3065
            j = gen_opc_ptr - gen_opc_buf;
3066
            if (lj < j) {
3067
                lj++;
3068
                while (lj < j)
3069
                    gen_opc_instr_start[lj++] = 0;
3070
                gen_opc_pc[lj] = ctx.nip;
3071
                gen_opc_instr_start[lj] = 1;
3072
            }
3073
        }
3074
#if defined PPC_DEBUG_DISAS
3075
        if (loglevel & CPU_LOG_TB_IN_ASM) {
3076
            fprintf(logfile, "----------------\n");
3077
            fprintf(logfile, "nip=%08x super=%d ir=%d\n",
3078
                    ctx.nip, 1 - msr_pr, msr_ir);
3079
        }
3080
#endif
3081
        ctx.opcode = ldl_code(ctx.nip);
3082
#if defined PPC_DEBUG_DISAS
3083
        if (loglevel & CPU_LOG_TB_IN_ASM) {
3084
            fprintf(logfile, "translate opcode %08x (%02x %02x %02x)\n",
3085
                    ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
3086
                    opc3(ctx.opcode));
3087
        }
3088
#endif
3089
        ctx.nip += 4;
3090
        table = ppc_opcodes;
3091
        handler = table[opc1(ctx.opcode)];
3092
        if (is_indirect_opcode(handler)) {
3093
            table = ind_table(handler);
3094
            handler = table[opc2(ctx.opcode)];
3095
            if (is_indirect_opcode(handler)) {
3096
                table = ind_table(handler);
3097
                handler = table[opc3(ctx.opcode)];
3098
            }
3099
        }
3100
        /* Is opcode *REALLY* valid ? */
3101
                if (handler->handler == &gen_invalid) {
3102
            if (loglevel > 0) {
3103
                    fprintf(logfile, "invalid/unsupported opcode: "
3104
                        "%02x - %02x - %02x (%08x) 0x%08x %d\n",
3105
                            opc1(ctx.opcode), opc2(ctx.opcode),
3106
                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
3107
            } else {
3108
                printf("invalid/unsupported opcode: "
3109
                       "%02x - %02x - %02x (%08x) 0x%08x %d\n",
3110
                       opc1(ctx.opcode), opc2(ctx.opcode),
3111
                       opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
3112
            }
3113
                } else {
3114
            if ((ctx.opcode & handler->inval) != 0) {
3115
                if (loglevel > 0) {
3116
                    fprintf(logfile, "invalid bits: %08x for opcode: "
3117
                            "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
3118
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
3119
                            opc2(ctx.opcode), opc3(ctx.opcode),
3120
                            ctx.opcode, ctx.nip - 4);
3121
                } else {
3122
                    printf("invalid bits: %08x for opcode: "
3123
                           "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
3124
                            ctx.opcode & handler->inval, opc1(ctx.opcode),
3125
                            opc2(ctx.opcode), opc3(ctx.opcode),
3126
                           ctx.opcode, ctx.nip - 4);
3127
            }
3128
                RET_INVAL(ctxp);
3129
                break;
3130
            }
3131
        }
3132
        (*(handler->handler))(&ctx);
3133
        /* Check trace mode exceptions */
3134
        if ((msr_be && ctx.exception == EXCP_BRANCH) ||
3135
            /* Check in single step trace mode
3136
             * we need to stop except if:
3137
             * - rfi, trap or syscall
3138
             * - first instruction of an exception handler
3139
             */
3140
            (msr_se && (ctx.nip < 0x100 ||
3141
                        ctx.nip > 0xF00 ||
3142
                        (ctx.nip & 0xFC) != 0x04) &&
3143
             ctx.exception != EXCP_SYSCALL && ctx.exception != EXCP_RFI &&
3144
             ctx.exception != EXCP_TRAP)) {
3145
            RET_EXCP(ctxp, EXCP_TRACE, 0);
3146
        }
3147
        /* if we reach a page boundary, stop generation */
3148
        if ((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) {
3149
            RET_EXCP(ctxp, EXCP_BRANCH, 0);
3150
    }
3151
    }
3152
    if (ctx.exception == EXCP_NONE) {
3153
        gen_op_b((unsigned long)ctx.tb, ctx.nip);
3154
    } else if (ctx.exception != EXCP_BRANCH) {
3155
        gen_op_set_T0(0);
3156
    }
3157
#if 1
3158
    /* TO BE FIXED: T0 hasn't got a proper value, which makes tb_add_jump
3159
     *              do bad business and then qemu crashes !
3160
     */
3161
    gen_op_set_T0(0);
3162
#endif
3163
    /* Generate the return instruction */
3164
    gen_op_exit_tb();
3165
    *gen_opc_ptr = INDEX_op_end;
3166
    if (search_pc) {
3167
        j = gen_opc_ptr - gen_opc_buf;
3168
        lj++;
3169
        while (lj <= j)
3170
            gen_opc_instr_start[lj++] = 0;
3171
        tb->size = 0;
3172
#if 0
3173
        if (loglevel > 0) {
3174
            page_dump(logfile);
3175
        }
3176
#endif
3177
    } else {
3178
        tb->size = ctx.nip - pc_start;
3179
    }
3180
#ifdef DEBUG_DISAS
3181
    if (loglevel & CPU_LOG_TB_CPU) {
3182
        fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
3183
        cpu_dump_state(env, logfile, fprintf, 0);
3184
    }
3185
    if (loglevel & CPU_LOG_TB_IN_ASM) {
3186
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
3187
        target_disas(logfile, pc_start, ctx.nip - pc_start, 0);
3188
        fprintf(logfile, "\n");
3189
    }
3190
    if (loglevel & CPU_LOG_TB_OP) {
3191
        fprintf(logfile, "OP:\n");
3192
        dump_ops(gen_opc_buf, gen_opparam_buf);
3193
        fprintf(logfile, "\n");
3194
    }
3195
#endif
3196
    return 0;
3197
}
3198

    
3199
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
3200
{
3201
    return gen_intermediate_code_internal(env, tb, 0);
3202
}
3203

    
3204
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
3205
{
3206
    return gen_intermediate_code_internal(env, tb, 1);
3207
}