PowerPC merge
[qemu] / target-ppc / translate.c
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     uint32_t 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     opc_handler_t handler;
183 } opcode_t;
184
185 /***                           Instruction decoding                        ***/
186 #define EXTRACT_HELPER(name, shift, nb)                                       \
187 static inline uint32_t name (uint32_t opcode)                                 \
188 {                                                                             \
189     return (opcode >> (shift)) & ((1 << (nb)) - 1);                           \
190 }
191
192 #define EXTRACT_SHELPER(name, shift, nb)                                      \
193 static inline int32_t name (uint32_t opcode)                                  \
194 {                                                                             \
195     return s_ext16((opcode >> (shift)) & ((1 << (nb)) - 1));                  \
196 }
197
198 /* Opcode part 1 */
199 EXTRACT_HELPER(opc1, 26, 6);
200 /* Opcode part 2 */
201 EXTRACT_HELPER(opc2, 1, 5);
202 /* Opcode part 3 */
203 EXTRACT_HELPER(opc3, 6, 5);
204 /* Update Cr0 flags */
205 EXTRACT_HELPER(Rc, 0, 1);
206 /* Destination */
207 EXTRACT_HELPER(rD, 21, 5);
208 /* Source */
209 EXTRACT_HELPER(rS, 21, 5);
210 /* First operand */
211 EXTRACT_HELPER(rA, 16, 5);
212 /* Second operand */
213 EXTRACT_HELPER(rB, 11, 5);
214 /* Third operand */
215 EXTRACT_HELPER(rC, 6, 5);
216 /***                               Get CRn                                 ***/
217 EXTRACT_HELPER(crfD, 23, 3);
218 EXTRACT_HELPER(crfS, 18, 3);
219 EXTRACT_HELPER(crbD, 21, 5);
220 EXTRACT_HELPER(crbA, 16, 5);
221 EXTRACT_HELPER(crbB, 11, 5);
222 /* SPR / TBL */
223 EXTRACT_HELPER(SPR, 11, 10);
224 /***                              Get constants                            ***/
225 EXTRACT_HELPER(IMM, 12, 8);
226 /* 16 bits signed immediate value */
227 EXTRACT_SHELPER(SIMM, 0, 16);
228 /* 16 bits unsigned immediate value */
229 EXTRACT_HELPER(UIMM, 0, 16);
230 /* Bit count */
231 EXTRACT_HELPER(NB, 11, 5);
232 /* Shift count */
233 EXTRACT_HELPER(SH, 11, 5);
234 /* Mask start */
235 EXTRACT_HELPER(MB, 6, 5);
236 /* Mask end */
237 EXTRACT_HELPER(ME, 1, 5);
238 /* Trap operand */
239 EXTRACT_HELPER(TO, 21, 5);
240
241 EXTRACT_HELPER(CRM, 12, 8);
242 EXTRACT_HELPER(FM, 17, 8);
243 EXTRACT_HELPER(SR, 16, 4);
244 EXTRACT_HELPER(FPIMM, 20, 4);
245
246 /***                            Jump target decoding                       ***/
247 /* Displacement */
248 EXTRACT_SHELPER(d, 0, 16);
249 /* Immediate address */
250 static inline uint32_t LI (uint32_t opcode)
251 {
252     return (opcode >> 0) & 0x03FFFFFC;
253 }
254
255 static inline uint32_t BD (uint32_t opcode)
256 {
257     return (opcode >> 0) & 0xFFFC;
258 }
259
260 EXTRACT_HELPER(BO, 21, 5);
261 EXTRACT_HELPER(BI, 16, 5);
262 /* Absolute/relative address */
263 EXTRACT_HELPER(AA, 1, 1);
264 /* Link */
265 EXTRACT_HELPER(LK, 0, 1);
266
267 /* Create a mask between <start> and <end> bits */
268 static inline uint32_t MASK (uint32_t start, uint32_t end)
269 {
270     uint32_t ret;
271
272     ret = (((uint32_t)(-1)) >> (start)) ^ (((uint32_t)(-1) >> (end)) >> 1);
273     if (start > end)
274         return ~ret;
275
276     return ret;
277 }
278
279 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ)                           \
280 __attribute__ ((section(".opcodes"), unused, aligned (8) ))                   \
281 static opcode_t opc_##name = {                                                \
282     .opc1 = op1,                                                              \
283     .opc2 = op2,                                                              \
284     .opc3 = op3,                                                              \
285     .handler = {                                                              \
286         .inval   = invl,                                                      \
287         .type = _typ,                                                         \
288         .handler = &gen_##name,                                               \
289     },                                                                        \
290 }
291
292 #define GEN_OPCODE_MARK(name)                                                 \
293 __attribute__ ((section(".opcodes"), unused, aligned (8) ))                   \
294 static opcode_t opc_##name = {                                                \
295     .opc1 = 0xFF,                                                             \
296     .opc2 = 0xFF,                                                             \
297     .opc3 = 0xFF,                                                             \
298     .handler = {                                                              \
299         .inval   = 0x00000000,                                                \
300         .type = 0x00,                                                         \
301         .handler = NULL,                                                      \
302     },                                                                        \
303 }
304
305 /* Start opcode list */
306 GEN_OPCODE_MARK(start);
307
308 /* Invalid instruction */
309 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE)
310 {
311     RET_INVAL(ctx);
312 }
313
314 /* Special opcode to stop emulation */
315 GEN_HANDLER(stop, 0x06, 0x00, 0xFF, 0x03FFFFC1, PPC_COMMON)
316 {
317     RET_EXCP(ctx, EXCP_HLT, 0);
318 }
319
320 /* Special opcode to call open-firmware */
321 GEN_HANDLER(of_enter, 0x06, 0x01, 0xFF, 0x03FFFFC1, PPC_COMMON)
322 {
323     RET_EXCP(ctx, EXCP_OFCALL, 0);
324 }
325
326 /* Special opcode to call RTAS */
327 GEN_HANDLER(rtas_enter, 0x06, 0x02, 0xFF, 0x03FFFFC1, PPC_COMMON)
328 {
329     printf("RTAS entry point !\n");
330     RET_EXCP(ctx, EXCP_RTASCALL, 0);
331 }
332
333 static opc_handler_t invalid_handler = {
334     .inval   = 0xFFFFFFFF,
335     .type    = PPC_NONE,
336     .handler = gen_invalid,
337 };
338
339 /***                           Integer arithmetic                          ***/
340 #define __GEN_INT_ARITH2(name, opc1, opc2, opc3, inval)                       \
341 GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
342 {                                                                             \
343     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
344     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
345     gen_op_##name();                                                          \
346     if (Rc(ctx->opcode) != 0)                                                 \
347         gen_op_set_Rc0();                                                     \
348     gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
349 }
350
351 #define __GEN_INT_ARITH2_O(name, opc1, opc2, opc3, inval)                     \
352 GEN_HANDLER(name, opc1, opc2, opc3, inval, PPC_INTEGER)                       \
353 {                                                                             \
354     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
355     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
356     gen_op_##name();                                                          \
357     if (Rc(ctx->opcode) != 0)                                                 \
358         gen_op_set_Rc0_ov();                                                  \
359     gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
360 }
361
362 #define __GEN_INT_ARITH1(name, opc1, opc2, opc3)                              \
363 GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
364 {                                                                             \
365     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
366     gen_op_##name();                                                          \
367     if (Rc(ctx->opcode) != 0)                                                 \
368         gen_op_set_Rc0();                                                     \
369     gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
370 }
371 #define __GEN_INT_ARITH1_O(name, opc1, opc2, opc3)                            \
372 GEN_HANDLER(name, opc1, opc2, opc3, 0x0000F800, PPC_INTEGER)                  \
373 {                                                                             \
374     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
375     gen_op_##name();                                                          \
376     if (Rc(ctx->opcode) != 0)                                                 \
377         gen_op_set_Rc0_ov();                                                  \
378     gen_op_store_T0_gpr(rD(ctx->opcode));                                     \
379 }
380
381 /* Two operands arithmetic functions */
382 #define GEN_INT_ARITH2(name, opc1, opc2, opc3)                                \
383 __GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000000)                          \
384 __GEN_INT_ARITH2_O(name##o, opc1, opc2, opc3 | 0x10, 0x00000000)
385
386 /* Two operands arithmetic functions with no overflow allowed */
387 #define GEN_INT_ARITHN(name, opc1, opc2, opc3)                                \
388 __GEN_INT_ARITH2(name, opc1, opc2, opc3, 0x00000400)
389
390 /* One operand arithmetic functions */
391 #define GEN_INT_ARITH1(name, opc1, opc2, opc3)                                \
392 __GEN_INT_ARITH1(name, opc1, opc2, opc3)                                      \
393 __GEN_INT_ARITH1_O(name##o, opc1, opc2, opc3 | 0x10)
394
395 /* add    add.    addo    addo.    */
396 GEN_INT_ARITH2 (add,    0x1F, 0x0A, 0x08);
397 /* addc   addc.   addco   addco.   */
398 GEN_INT_ARITH2 (addc,   0x1F, 0x0A, 0x00);
399 /* adde   adde.   addeo   addeo.   */
400 GEN_INT_ARITH2 (adde,   0x1F, 0x0A, 0x04);
401 /* addme  addme.  addmeo  addmeo.  */
402 GEN_INT_ARITH1 (addme,  0x1F, 0x0A, 0x07);
403 /* addze  addze.  addzeo  addzeo.  */
404 GEN_INT_ARITH1 (addze,  0x1F, 0x0A, 0x06);
405 /* divw   divw.   divwo   divwo.   */
406 GEN_INT_ARITH2 (divw,   0x1F, 0x0B, 0x0F);
407 /* divwu  divwu.  divwuo  divwuo.  */
408 GEN_INT_ARITH2 (divwu,  0x1F, 0x0B, 0x0E);
409 /* mulhw  mulhw.                   */
410 GEN_INT_ARITHN (mulhw,  0x1F, 0x0B, 0x02);
411 /* mulhwu mulhwu.                  */
412 GEN_INT_ARITHN (mulhwu, 0x1F, 0x0B, 0x00);
413 /* mullw  mullw.  mullwo  mullwo.  */
414 GEN_INT_ARITH2 (mullw,  0x1F, 0x0B, 0x07);
415 /* neg    neg.    nego    nego.    */
416 GEN_INT_ARITH1 (neg,    0x1F, 0x08, 0x03);
417 /* subf   subf.   subfo   subfo.   */
418 GEN_INT_ARITH2 (subf,   0x1F, 0x08, 0x01);
419 /* subfc  subfc.  subfco  subfco.  */
420 GEN_INT_ARITH2 (subfc,  0x1F, 0x08, 0x00);
421 /* subfe  subfe.  subfeo  subfeo.  */
422 GEN_INT_ARITH2 (subfe,  0x1F, 0x08, 0x04);
423 /* subfme subfme. subfmeo subfmeo. */
424 GEN_INT_ARITH1 (subfme, 0x1F, 0x08, 0x07);
425 /* subfze subfze. subfzeo subfzeo. */
426 GEN_INT_ARITH1 (subfze, 0x1F, 0x08, 0x06);
427 /* addi */
428 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
429 {
430     int32_t simm = SIMM(ctx->opcode);
431
432     if (rA(ctx->opcode) == 0) {
433         gen_op_set_T0(simm);
434     } else {
435         gen_op_load_gpr_T0(rA(ctx->opcode));
436         gen_op_addi(simm);
437     }
438     gen_op_store_T0_gpr(rD(ctx->opcode));
439 }
440 /* addic */
441 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
442 {
443     gen_op_load_gpr_T0(rA(ctx->opcode));
444     gen_op_addic(SIMM(ctx->opcode));
445     gen_op_store_T0_gpr(rD(ctx->opcode));
446 }
447 /* addic. */
448 GEN_HANDLER(addic_, 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
449 {
450     gen_op_load_gpr_T0(rA(ctx->opcode));
451     gen_op_addic(SIMM(ctx->opcode));
452     gen_op_set_Rc0();
453     gen_op_store_T0_gpr(rD(ctx->opcode));
454 }
455 /* addis */
456 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
457 {
458     int32_t simm = SIMM(ctx->opcode);
459
460     if (rA(ctx->opcode) == 0) {
461         gen_op_set_T0(simm << 16);
462     } else {
463         gen_op_load_gpr_T0(rA(ctx->opcode));
464         gen_op_addi(simm << 16);
465     }
466     gen_op_store_T0_gpr(rD(ctx->opcode));
467 }
468 /* mulli */
469 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
470 {
471     gen_op_load_gpr_T0(rA(ctx->opcode));
472     gen_op_mulli(SIMM(ctx->opcode));
473     gen_op_store_T0_gpr(rD(ctx->opcode));
474 }
475 /* subfic */
476 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
477 {
478     gen_op_load_gpr_T0(rA(ctx->opcode));
479     gen_op_subfic(SIMM(ctx->opcode));
480     gen_op_store_T0_gpr(rD(ctx->opcode));
481 }
482
483 /***                           Integer comparison                          ***/
484 #define GEN_CMP(name, opc)                                                    \
485 GEN_HANDLER(name, 0x1F, 0x00, opc, 0x00400000, PPC_INTEGER)                   \
486 {                                                                             \
487     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
488     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
489     gen_op_##name();                                                          \
490     gen_op_store_T0_crf(crfD(ctx->opcode));                                   \
491 }
492
493 /* cmp */
494 GEN_CMP(cmp, 0x00);
495 /* cmpi */
496 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
497 {
498     gen_op_load_gpr_T0(rA(ctx->opcode));
499     gen_op_cmpi(SIMM(ctx->opcode));
500     gen_op_store_T0_crf(crfD(ctx->opcode));
501 }
502 /* cmpl */
503 GEN_CMP(cmpl, 0x01);
504 /* cmpli */
505 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER)
506 {
507     gen_op_load_gpr_T0(rA(ctx->opcode));
508     gen_op_cmpli(UIMM(ctx->opcode));
509     gen_op_store_T0_crf(crfD(ctx->opcode));
510 }
511
512 /***                            Integer logical                            ***/
513 #define __GEN_LOGICAL2(name, opc2, opc3)                                      \
514 GEN_HANDLER(name, 0x1F, opc2, opc3, 0x00000000, PPC_INTEGER)                  \
515 {                                                                             \
516     gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
517     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
518     gen_op_##name();                                                          \
519     if (Rc(ctx->opcode) != 0)                                                 \
520         gen_op_set_Rc0();                                                     \
521     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
522 }
523 #define GEN_LOGICAL2(name, opc)                                               \
524 __GEN_LOGICAL2(name, 0x1C, opc)
525
526 #define GEN_LOGICAL1(name, opc)                                               \
527 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, PPC_INTEGER)                   \
528 {                                                                             \
529     gen_op_load_gpr_T0(rS(ctx->opcode));                                      \
530     gen_op_##name();                                                          \
531     if (Rc(ctx->opcode) != 0)                                                 \
532         gen_op_set_Rc0();                                                     \
533     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
534 }
535
536 /* and & and. */
537 GEN_LOGICAL2(and, 0x00);
538 /* andc & andc. */
539 GEN_LOGICAL2(andc, 0x01);
540 /* andi. */
541 GEN_HANDLER(andi_, 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
542 {
543     gen_op_load_gpr_T0(rS(ctx->opcode));
544     gen_op_andi_(UIMM(ctx->opcode));
545     gen_op_set_Rc0();
546     gen_op_store_T0_gpr(rA(ctx->opcode));
547 }
548 /* andis. */
549 GEN_HANDLER(andis_, 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
550 {
551     gen_op_load_gpr_T0(rS(ctx->opcode));
552     gen_op_andi_(UIMM(ctx->opcode) << 16);
553     gen_op_set_Rc0();
554     gen_op_store_T0_gpr(rA(ctx->opcode));
555 }
556
557 /* cntlzw */
558 GEN_LOGICAL1(cntlzw, 0x00);
559 /* eqv & eqv. */
560 GEN_LOGICAL2(eqv, 0x08);
561 /* extsb & extsb. */
562 GEN_LOGICAL1(extsb, 0x1D);
563 /* extsh & extsh. */
564 GEN_LOGICAL1(extsh, 0x1C);
565 /* nand & nand. */
566 GEN_LOGICAL2(nand, 0x0E);
567 /* nor & nor. */
568 GEN_LOGICAL2(nor, 0x03);
569
570 /* or & or. */
571 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER)
572 {
573     gen_op_load_gpr_T0(rS(ctx->opcode));
574     /* Optimisation for mr case */
575     if (rS(ctx->opcode) != rB(ctx->opcode)) {
576         gen_op_load_gpr_T1(rB(ctx->opcode));
577         gen_op_or();
578     }
579     if (Rc(ctx->opcode) != 0)
580         gen_op_set_Rc0();
581     gen_op_store_T0_gpr(rA(ctx->opcode));
582 }
583
584 /* orc & orc. */
585 GEN_LOGICAL2(orc, 0x0C);
586 /* xor & xor. */
587 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER)
588 {
589     gen_op_load_gpr_T0(rS(ctx->opcode));
590     /* Optimisation for "set to zero" case */
591     if (rS(ctx->opcode) != rB(ctx->opcode)) {
592         gen_op_load_gpr_T1(rB(ctx->opcode));
593         gen_op_xor();
594     } else {
595         gen_op_set_T0(0);
596     }
597     if (Rc(ctx->opcode) != 0)
598         gen_op_set_Rc0();
599     gen_op_store_T0_gpr(rA(ctx->opcode));
600 }
601 /* ori */
602 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
603 {
604     uint32_t uimm = UIMM(ctx->opcode);
605
606     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
607         /* NOP */
608         return;
609         }
610         gen_op_load_gpr_T0(rS(ctx->opcode));
611     if (uimm != 0)
612         gen_op_ori(uimm);
613         gen_op_store_T0_gpr(rA(ctx->opcode));
614 }
615 /* oris */
616 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
617 {
618     uint32_t uimm = UIMM(ctx->opcode);
619
620     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
621         /* NOP */
622         return;
623         }
624         gen_op_load_gpr_T0(rS(ctx->opcode));
625     if (uimm != 0)
626         gen_op_ori(uimm << 16);
627         gen_op_store_T0_gpr(rA(ctx->opcode));
628 }
629 /* xori */
630 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
631 {
632     uint32_t uimm = UIMM(ctx->opcode);
633
634     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
635         /* NOP */
636         return;
637     }
638     gen_op_load_gpr_T0(rS(ctx->opcode));
639     if (uimm != 0)
640     gen_op_xori(uimm);
641     gen_op_store_T0_gpr(rA(ctx->opcode));
642 }
643
644 /* xoris */
645 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
646 {
647     uint32_t uimm = UIMM(ctx->opcode);
648
649     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
650         /* NOP */
651         return;
652     }
653     gen_op_load_gpr_T0(rS(ctx->opcode));
654     if (uimm != 0)
655     gen_op_xori(uimm << 16);
656     gen_op_store_T0_gpr(rA(ctx->opcode));
657 }
658
659 /***                             Integer rotate                            ***/
660 /* rlwimi & rlwimi. */
661 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
662 {
663     uint32_t mb, me;
664
665     mb = MB(ctx->opcode);
666     me = ME(ctx->opcode);
667     gen_op_load_gpr_T0(rS(ctx->opcode));
668     gen_op_load_gpr_T1(rA(ctx->opcode));
669     gen_op_rlwimi(SH(ctx->opcode), MASK(mb, me), ~MASK(mb, me));
670     if (Rc(ctx->opcode) != 0)
671         gen_op_set_Rc0();
672     gen_op_store_T0_gpr(rA(ctx->opcode));
673 }
674 /* rlwinm & rlwinm. */
675 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
676 {
677     uint32_t mb, me, sh;
678     
679     sh = SH(ctx->opcode);
680     mb = MB(ctx->opcode);
681     me = ME(ctx->opcode);
682     gen_op_load_gpr_T0(rS(ctx->opcode));
683 #if 1 // TRY
684     if (sh == 0) {
685         gen_op_andi_(MASK(mb, me));
686         goto store;
687     }
688 #endif
689     if (mb == 0) {
690         if (me == 31) {
691             gen_op_rotlwi(sh);
692             goto store;
693 #if 0
694         } else if (me == (31 - sh)) {
695             gen_op_slwi(sh);
696             goto store;
697 #endif
698         }
699     } else if (me == 31) {
700 #if 0
701         if (sh == (32 - mb)) {
702             gen_op_srwi(mb);
703             goto store;
704         }
705 #endif
706     }
707     gen_op_rlwinm(sh, MASK(mb, me));
708 store:
709     if (Rc(ctx->opcode) != 0)
710         gen_op_set_Rc0();
711     gen_op_store_T0_gpr(rA(ctx->opcode));
712 }
713 /* rlwnm & rlwnm. */
714 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
715 {
716     uint32_t mb, me;
717
718     mb = MB(ctx->opcode);
719     me = ME(ctx->opcode);
720     gen_op_load_gpr_T0(rS(ctx->opcode));
721     gen_op_load_gpr_T1(rB(ctx->opcode));
722     if (mb == 0 && me == 31) {
723         gen_op_rotl();
724     } else
725     {
726         gen_op_rlwnm(MASK(mb, me));
727     }
728     if (Rc(ctx->opcode) != 0)
729         gen_op_set_Rc0();
730     gen_op_store_T0_gpr(rA(ctx->opcode));
731 }
732
733 /***                             Integer shift                             ***/
734 /* slw & slw. */
735 __GEN_LOGICAL2(slw, 0x18, 0x00);
736 /* sraw & sraw. */
737 __GEN_LOGICAL2(sraw, 0x18, 0x18);
738 /* srawi & srawi. */
739 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER)
740 {
741     gen_op_load_gpr_T0(rS(ctx->opcode));
742     gen_op_srawi(SH(ctx->opcode), MASK(32 - SH(ctx->opcode), 31));
743     if (Rc(ctx->opcode) != 0)
744         gen_op_set_Rc0();
745     gen_op_store_T0_gpr(rA(ctx->opcode));
746 }
747 /* srw & srw. */
748 __GEN_LOGICAL2(srw, 0x18, 0x10);
749
750 /***                       Floating-Point arithmetic                       ***/
751 #define _GEN_FLOAT_ACB(name, op1, op2)                                        \
752 GEN_HANDLER(f##name, op1, op2, 0xFF, 0x00000000, PPC_FLOAT)                   \
753 {                                                                             \
754     gen_op_reset_scrfx();                                                     \
755     gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
756     gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
757     gen_op_load_fpr_FT2(rB(ctx->opcode));                                     \
758     gen_op_f##name();                                                         \
759     gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
760     if (Rc(ctx->opcode))                                                      \
761         gen_op_set_Rc1();                                                     \
762 }
763
764 #define GEN_FLOAT_ACB(name, op2)                                              \
765 _GEN_FLOAT_ACB(name, 0x3F, op2);                                              \
766 _GEN_FLOAT_ACB(name##s, 0x3B, op2);
767
768 #define _GEN_FLOAT_AB(name, op1, op2, inval)                                  \
769 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
770 {                                                                             \
771     gen_op_reset_scrfx();                                                     \
772     gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
773     gen_op_load_fpr_FT1(rB(ctx->opcode));                                     \
774     gen_op_f##name();                                                         \
775     gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
776     if (Rc(ctx->opcode))                                                      \
777         gen_op_set_Rc1();                                                     \
778 }
779 #define GEN_FLOAT_AB(name, op2, inval)                                        \
780 _GEN_FLOAT_AB(name, 0x3F, op2, inval);                                        \
781 _GEN_FLOAT_AB(name##s, 0x3B, op2, inval);
782
783 #define _GEN_FLOAT_AC(name, op1, op2, inval)                                  \
784 GEN_HANDLER(f##name, op1, op2, 0xFF, inval, PPC_FLOAT)                        \
785 {                                                                             \
786     gen_op_reset_scrfx();                                                     \
787     gen_op_load_fpr_FT0(rA(ctx->opcode));                                     \
788     gen_op_load_fpr_FT1(rC(ctx->opcode));                                     \
789     gen_op_f##name();                                                         \
790     gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
791     if (Rc(ctx->opcode))                                                      \
792         gen_op_set_Rc1();                                                     \
793 }
794 #define GEN_FLOAT_AC(name, op2, inval)                                        \
795 _GEN_FLOAT_AC(name, 0x3F, op2, inval);                                        \
796 _GEN_FLOAT_AC(name##s, 0x3B, op2, inval);
797
798 #define GEN_FLOAT_B(name, op2, op3)                                           \
799 GEN_HANDLER(f##name, 0x3F, op2, op3, 0x001F0000, PPC_FLOAT)                   \
800 {                                                                             \
801     gen_op_reset_scrfx();                                                     \
802     gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
803     gen_op_f##name();                                                         \
804     gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
805     if (Rc(ctx->opcode))                                                      \
806         gen_op_set_Rc1();                                                     \
807 }
808
809 #define GEN_FLOAT_BS(name, op2)                                               \
810 GEN_HANDLER(f##name, 0x3F, op2, 0xFF, 0x001F07C0, PPC_FLOAT)                  \
811 {                                                                             \
812     gen_op_reset_scrfx();                                                     \
813     gen_op_load_fpr_FT0(rB(ctx->opcode));                                     \
814     gen_op_f##name();                                                         \
815     gen_op_store_FT0_fpr(rD(ctx->opcode));                                    \
816     if (Rc(ctx->opcode))                                                      \
817         gen_op_set_Rc1();                                                     \
818 }
819
820 /* fadd - fadds */
821 GEN_FLOAT_AB(add, 0x15, 0x000007C0);
822 /* fdiv */
823 GEN_FLOAT_AB(div, 0x12, 0x000007C0);
824 /* fmul */
825 GEN_FLOAT_AC(mul, 0x19, 0x0000F800);
826
827 /* fres */
828 GEN_FLOAT_BS(res, 0x18);
829
830 /* frsqrte */
831 GEN_FLOAT_BS(rsqrte, 0x1A);
832
833 /* fsel */
834 _GEN_FLOAT_ACB(sel, 0x3F, 0x17);
835 /* fsub */
836 GEN_FLOAT_AB(sub, 0x14, 0x000007C0);
837 /* Optional: */
838 /* fsqrt */
839 GEN_FLOAT_BS(sqrt, 0x16);
840
841 GEN_HANDLER(fsqrts, 0x3B, 0x16, 0xFF, 0x001F07C0, PPC_FLOAT_OPT)
842 {
843     gen_op_reset_scrfx();
844     gen_op_load_fpr_FT0(rB(ctx->opcode));
845     gen_op_fsqrts();
846     gen_op_store_FT0_fpr(rD(ctx->opcode));
847     if (Rc(ctx->opcode))
848         gen_op_set_Rc1();
849 }
850
851 /***                     Floating-Point multiply-and-add                   ***/
852 /* fmadd */
853 GEN_FLOAT_ACB(madd, 0x1D);
854 /* fmsub */
855 GEN_FLOAT_ACB(msub, 0x1C);
856 /* fnmadd */
857 GEN_FLOAT_ACB(nmadd, 0x1F);
858 /* fnmsub */
859 GEN_FLOAT_ACB(nmsub, 0x1E);
860
861 /***                     Floating-Point round & convert                    ***/
862 /* fctiw */
863 GEN_FLOAT_B(ctiw, 0x0E, 0x00);
864 /* fctiwz */
865 GEN_FLOAT_B(ctiwz, 0x0F, 0x00);
866 /* frsp */
867 GEN_FLOAT_B(rsp, 0x0C, 0x00);
868
869 /***                         Floating-Point compare                        ***/
870 /* fcmpo */
871 GEN_HANDLER(fcmpo, 0x3F, 0x00, 0x00, 0x00600001, PPC_FLOAT)
872 {
873     gen_op_reset_scrfx();
874     gen_op_load_fpr_FT0(rA(ctx->opcode));
875     gen_op_load_fpr_FT1(rB(ctx->opcode));
876     gen_op_fcmpo();
877     gen_op_store_T0_crf(crfD(ctx->opcode));
878 }
879
880 /* fcmpu */
881 GEN_HANDLER(fcmpu, 0x3F, 0x00, 0x01, 0x00600001, PPC_FLOAT)
882 {
883     gen_op_reset_scrfx();
884     gen_op_load_fpr_FT0(rA(ctx->opcode));
885     gen_op_load_fpr_FT1(rB(ctx->opcode));
886     gen_op_fcmpu();
887     gen_op_store_T0_crf(crfD(ctx->opcode));
888 }
889
890 /***                         Floating-point move                           ***/
891 /* fabs */
892 GEN_FLOAT_B(abs, 0x08, 0x08);
893
894 /* fmr  - fmr. */
895 GEN_HANDLER(fmr, 0x3F, 0x08, 0x02, 0x001F0000, PPC_FLOAT)
896 {
897     gen_op_reset_scrfx();
898     gen_op_load_fpr_FT0(rB(ctx->opcode));
899     gen_op_store_FT0_fpr(rD(ctx->opcode));
900     if (Rc(ctx->opcode))
901         gen_op_set_Rc1();
902 }
903
904 /* fnabs */
905 GEN_FLOAT_B(nabs, 0x08, 0x04);
906 /* fneg */
907 GEN_FLOAT_B(neg, 0x08, 0x01);
908
909 /***                  Floating-Point status & ctrl register                ***/
910 /* mcrfs */
911 GEN_HANDLER(mcrfs, 0x3F, 0x00, 0x02, 0x0063F801, PPC_FLOAT)
912 {
913     gen_op_load_fpscr_T0(crfS(ctx->opcode));
914     gen_op_store_T0_crf(crfD(ctx->opcode));
915     gen_op_clear_fpscr(crfS(ctx->opcode));
916 }
917
918 /* mffs */
919 GEN_HANDLER(mffs, 0x3F, 0x07, 0x12, 0x001FF800, PPC_FLOAT)
920 {
921     gen_op_load_fpscr();
922     gen_op_store_FT0_fpr(rD(ctx->opcode));
923     if (Rc(ctx->opcode))
924         gen_op_set_Rc1();
925 }
926
927 /* mtfsb0 */
928 GEN_HANDLER(mtfsb0, 0x3F, 0x06, 0x02, 0x001FF800, PPC_FLOAT)
929 {
930     uint8_t crb;
931     
932     crb = crbD(ctx->opcode) >> 2;
933     gen_op_load_fpscr_T0(crb);
934     gen_op_andi_(~(1 << (crbD(ctx->opcode) & 0x03)));
935     gen_op_store_T0_fpscr(crb);
936     if (Rc(ctx->opcode))
937         gen_op_set_Rc1();
938 }
939
940 /* mtfsb1 */
941 GEN_HANDLER(mtfsb1, 0x3F, 0x06, 0x01, 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_ori(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 /* mtfsf */
954 GEN_HANDLER(mtfsf, 0x3F, 0x07, 0x16, 0x02010000, PPC_FLOAT)
955 {
956     gen_op_load_fpr_FT0(rB(ctx->opcode));
957     gen_op_store_fpscr(FM(ctx->opcode));
958     if (Rc(ctx->opcode))
959         gen_op_set_Rc1();
960 }
961
962 /* mtfsfi */
963 GEN_HANDLER(mtfsfi, 0x3F, 0x06, 0x04, 0x006f0800, PPC_FLOAT)
964 {
965     gen_op_store_T0_fpscri(crbD(ctx->opcode) >> 2, FPIMM(ctx->opcode));
966     if (Rc(ctx->opcode))
967         gen_op_set_Rc1();
968 }
969
970 /***                             Integer load                              ***/
971 #if defined(CONFIG_USER_ONLY)
972 #define op_ldst(name)        gen_op_##name##_raw()
973 #define OP_LD_TABLE(width)
974 #define OP_ST_TABLE(width)
975 #else
976 #define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
977 #define OP_LD_TABLE(width)                                                    \
978 static GenOpFunc *gen_op_l##width[] = {                                       \
979     &gen_op_l##width##_user,                                                  \
980     &gen_op_l##width##_kernel,                                                \
981 }
982 #define OP_ST_TABLE(width)                                                    \
983 static GenOpFunc *gen_op_st##width[] = {                                      \
984     &gen_op_st##width##_user,                                                 \
985     &gen_op_st##width##_kernel,                                               \
986 }
987 #endif
988
989 #define GEN_LD(width, opc)                                                    \
990 GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)               \
991 {                                                                             \
992     uint32_t simm = SIMM(ctx->opcode);                                        \
993     if (rA(ctx->opcode) == 0) {                                               \
994         gen_op_set_T0(simm);                                                  \
995     } else {                                                                  \
996         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
997         if (simm != 0)                                                        \
998             gen_op_addi(simm);                                                \
999     }                                                                         \
1000     op_ldst(l##width);                                                        \
1001     gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1002 }
1003
1004 #define GEN_LDU(width, opc)                                                   \
1005 GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
1006 {                                                                             \
1007     uint32_t simm = SIMM(ctx->opcode);                                        \
1008     if (rA(ctx->opcode) == 0 ||                                               \
1009         rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1010         RET_INVAL(ctx);                                                       \
1011         return;                                                               \
1012     }                                                                         \
1013     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1014     if (simm != 0)                                                            \
1015         gen_op_addi(simm);                                                    \
1016     op_ldst(l##width);                                                        \
1017     gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1018     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1019 }
1020
1021 #define GEN_LDUX(width, opc)                                                  \
1022 GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
1023 {                                                                             \
1024     if (rA(ctx->opcode) == 0 ||                                               \
1025         rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1026         RET_INVAL(ctx);                                                       \
1027         return;                                                               \
1028     }                                                                         \
1029     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1030     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1031     gen_op_add();                                                             \
1032     op_ldst(l##width);                                                        \
1033     gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1034     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1035 }
1036
1037 #define GEN_LDX(width, opc2, opc3)                                            \
1038 GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
1039 {                                                                             \
1040     if (rA(ctx->opcode) == 0) {                                               \
1041         gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1042     } else {                                                                  \
1043         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1044         gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1045         gen_op_add();                                                         \
1046     }                                                                         \
1047     op_ldst(l##width);                                                        \
1048     gen_op_store_T1_gpr(rD(ctx->opcode));                                     \
1049 }
1050
1051 #define GEN_LDS(width, op)                                                    \
1052 OP_LD_TABLE(width);                                                           \
1053 GEN_LD(width, op | 0x20);                                                     \
1054 GEN_LDU(width, op | 0x21);                                                    \
1055 GEN_LDUX(width, op | 0x01);                                                   \
1056 GEN_LDX(width, 0x17, op | 0x00)
1057
1058 /* lbz lbzu lbzux lbzx */
1059 GEN_LDS(bz, 0x02);
1060 /* lha lhau lhaux lhax */
1061 GEN_LDS(ha, 0x0A);
1062 /* lhz lhzu lhzux lhzx */
1063 GEN_LDS(hz, 0x08);
1064 /* lwz lwzu lwzux lwzx */
1065 GEN_LDS(wz, 0x00);
1066
1067 /***                              Integer store                            ***/
1068 #define GEN_ST(width, opc)                                                    \
1069 GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
1070 {                                                                             \
1071     uint32_t simm = SIMM(ctx->opcode);                                        \
1072     if (rA(ctx->opcode) == 0) {                                               \
1073         gen_op_set_T0(simm);                                                  \
1074     } else {                                                                  \
1075         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1076         if (simm != 0)                                                        \
1077             gen_op_addi(simm);                                                \
1078     }                                                                         \
1079     gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1080     op_ldst(st##width);                                                       \
1081 }
1082
1083 #define GEN_STU(width, opc)                                                   \
1084 GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
1085 {                                                                             \
1086     uint32_t simm = SIMM(ctx->opcode);                                        \
1087     if (rA(ctx->opcode) == 0) {                                               \
1088         RET_INVAL(ctx);                                                       \
1089         return;                                                               \
1090     }                                                                         \
1091     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1092     if (simm != 0)                                                            \
1093         gen_op_addi(simm);                                                    \
1094     gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1095     op_ldst(st##width);                                                       \
1096     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1097 }
1098
1099 #define GEN_STUX(width, opc)                                                  \
1100 GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
1101 {                                                                             \
1102     if (rA(ctx->opcode) == 0) {                                               \
1103         RET_INVAL(ctx);                                                       \
1104         return;                                                               \
1105     }                                                                         \
1106     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1107     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1108     gen_op_add();                                                             \
1109     gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1110     op_ldst(st##width);                                                       \
1111     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1112 }
1113
1114 #define GEN_STX(width, opc2, opc3)                                            \
1115 GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
1116 {                                                                             \
1117     if (rA(ctx->opcode) == 0) {                                               \
1118         gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1119     } else {                                                                  \
1120         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1121         gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1122         gen_op_add();                                                         \
1123     }                                                                         \
1124     gen_op_load_gpr_T1(rS(ctx->opcode));                                      \
1125     op_ldst(st##width);                                                       \
1126 }
1127
1128 #define GEN_STS(width, op)                                                    \
1129 OP_ST_TABLE(width);                                                           \
1130 GEN_ST(width, op | 0x20);                                                     \
1131 GEN_STU(width, op | 0x21);                                                    \
1132 GEN_STUX(width, op | 0x01);                                                   \
1133 GEN_STX(width, 0x17, op | 0x00)
1134
1135 /* stb stbu stbux stbx */
1136 GEN_STS(b, 0x06);
1137 /* sth sthu sthux sthx */
1138 GEN_STS(h, 0x0C);
1139 /* stw stwu stwux stwx */
1140 GEN_STS(w, 0x04);
1141
1142 /***                Integer load and store with byte reverse               ***/
1143 /* lhbrx */
1144 OP_LD_TABLE(hbr);
1145 GEN_LDX(hbr, 0x16, 0x18);
1146 /* lwbrx */
1147 OP_LD_TABLE(wbr);
1148 GEN_LDX(wbr, 0x16, 0x10);
1149 /* sthbrx */
1150 OP_ST_TABLE(hbr);
1151 GEN_STX(hbr, 0x16, 0x1C);
1152 /* stwbrx */
1153 OP_ST_TABLE(wbr);
1154 GEN_STX(wbr, 0x16, 0x14);
1155
1156 /***                    Integer load and store multiple                    ***/
1157 #if defined(CONFIG_USER_ONLY)
1158 #define op_ldstm(name, reg) gen_op_##name##_raw(reg)
1159 #else
1160 #define op_ldstm(name, reg) (*gen_op_##name[ctx->mem_idx])(reg)
1161 static GenOpFunc1 *gen_op_lmw[] = {
1162     &gen_op_lmw_user,
1163     &gen_op_lmw_kernel,
1164 };
1165 static GenOpFunc1 *gen_op_stmw[] = {
1166     &gen_op_stmw_user,
1167     &gen_op_stmw_kernel,
1168 };
1169 #endif
1170
1171 /* lmw */
1172 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1173 {
1174     int simm = SIMM(ctx->opcode);
1175
1176     if (rA(ctx->opcode) == 0) {
1177         gen_op_set_T0(simm);
1178     } else {
1179         gen_op_load_gpr_T0(rA(ctx->opcode));
1180         if (simm != 0)
1181             gen_op_addi(simm);
1182     }
1183     op_ldstm(lmw, rD(ctx->opcode));
1184 }
1185
1186 /* stmw */
1187 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)
1188 {
1189     int simm = SIMM(ctx->opcode);
1190
1191     if (rA(ctx->opcode) == 0) {
1192         gen_op_set_T0(simm);
1193     } else {
1194         gen_op_load_gpr_T0(rA(ctx->opcode));
1195         if (simm != 0)
1196             gen_op_addi(simm);
1197     }
1198     op_ldstm(stmw, rS(ctx->opcode));
1199 }
1200
1201 /***                    Integer load and store strings                     ***/
1202 #if defined(CONFIG_USER_ONLY)
1203 #define op_ldsts(name, start) gen_op_##name##_raw(start)
1204 #define op_ldstsx(name, rd, ra, rb) gen_op_##name##_raw(rd, ra, rb)
1205 #else
1206 #define op_ldsts(name, start) (*gen_op_##name[ctx->mem_idx])(start)
1207 #define op_ldstsx(name, rd, ra, rb) (*gen_op_##name[ctx->mem_idx])(rd, ra, rb)
1208 static GenOpFunc1 *gen_op_lswi[] = {
1209     &gen_op_lswi_user,
1210     &gen_op_lswi_kernel,
1211 };
1212 static GenOpFunc3 *gen_op_lswx[] = {
1213     &gen_op_lswx_user,
1214     &gen_op_lswx_kernel,
1215 };
1216 static GenOpFunc1 *gen_op_stsw[] = {
1217     &gen_op_stsw_user,
1218     &gen_op_stsw_kernel,
1219 };
1220 #endif
1221
1222 /* lswi */
1223 /* PPC32 specification says we must generate an exception if
1224  * rA is in the range of registers to be loaded.
1225  * In an other hand, IBM says this is valid, but rA won't be loaded.
1226  * For now, I'll follow the spec...
1227  */
1228 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_INTEGER)
1229 {
1230     int nb = NB(ctx->opcode);
1231     int start = rD(ctx->opcode);
1232     int ra = rA(ctx->opcode);
1233     int nr;
1234
1235     if (nb == 0)
1236         nb = 32;
1237     nr = nb / 4;
1238     if (((start + nr) > 32  && start <= ra && (start + nr - 32) > ra) ||
1239         ((start + nr) <= 32 && start <= ra && (start + nr) > ra)) {
1240         RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_LSWX);
1241         return;
1242     }
1243     if (ra == 0) {
1244         gen_op_set_T0(0);
1245     } else {
1246         gen_op_load_gpr_T0(ra);
1247     }
1248     gen_op_set_T1(nb);
1249     op_ldsts(lswi, start);
1250 }
1251
1252 /* lswx */
1253 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_INTEGER)
1254 {
1255     int ra = rA(ctx->opcode);
1256     int rb = rB(ctx->opcode);
1257
1258     if (ra == 0) {
1259         gen_op_load_gpr_T0(rb);
1260         ra = rb;
1261     } else {
1262         gen_op_load_gpr_T0(ra);
1263         gen_op_load_gpr_T1(rb);
1264         gen_op_add();
1265     }
1266     gen_op_load_xer_bc();
1267     op_ldstsx(lswx, rD(ctx->opcode), ra, rb);
1268 }
1269
1270 /* stswi */
1271 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_INTEGER)
1272 {
1273     int nb = NB(ctx->opcode);
1274
1275     if (rA(ctx->opcode) == 0) {
1276         gen_op_set_T0(0);
1277     } else {
1278         gen_op_load_gpr_T0(rA(ctx->opcode));
1279     }
1280     if (nb == 0)
1281         nb = 32;
1282     gen_op_set_T1(nb);
1283     op_ldsts(stsw, rS(ctx->opcode));
1284 }
1285
1286 /* stswx */
1287 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_INTEGER)
1288 {
1289     int ra = rA(ctx->opcode);
1290
1291     if (ra == 0) {
1292         gen_op_load_gpr_T0(rB(ctx->opcode));
1293         ra = rB(ctx->opcode);
1294     } else {
1295         gen_op_load_gpr_T0(ra);
1296         gen_op_load_gpr_T1(rB(ctx->opcode));
1297         gen_op_add();
1298     }
1299     gen_op_load_xer_bc();
1300     op_ldsts(stsw, rS(ctx->opcode));
1301 }
1302
1303 /***                        Memory synchronisation                         ***/
1304 /* eieio */
1305 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x03FF0801, PPC_MEM)
1306 {
1307 }
1308
1309 /* isync */
1310 GEN_HANDLER(isync, 0x13, 0x16, 0xFF, 0x03FF0801, PPC_MEM)
1311 {
1312 }
1313
1314 /* lwarx */
1315 #if defined(CONFIG_USER_ONLY)
1316 #define op_lwarx() gen_op_lwarx_raw()
1317 #define op_stwcx() gen_op_stwcx_raw()
1318 #else
1319 #define op_lwarx() (*gen_op_lwarx[ctx->mem_idx])()
1320 static GenOpFunc *gen_op_lwarx[] = {
1321     &gen_op_lwarx_user,
1322     &gen_op_lwarx_kernel,
1323 };
1324 #define op_stwcx() (*gen_op_stwcx[ctx->mem_idx])()
1325 static GenOpFunc *gen_op_stwcx[] = {
1326     &gen_op_stwcx_user,
1327     &gen_op_stwcx_kernel,
1328 };
1329 #endif
1330
1331 GEN_HANDLER(lwarx, 0x1F, 0x14, 0xFF, 0x00000001, PPC_RES)
1332 {
1333     if (rA(ctx->opcode) == 0) {
1334         gen_op_load_gpr_T0(rB(ctx->opcode));
1335     } else {
1336         gen_op_load_gpr_T0(rA(ctx->opcode));
1337         gen_op_load_gpr_T1(rB(ctx->opcode));
1338         gen_op_add();
1339     }
1340     op_lwarx();
1341     gen_op_store_T1_gpr(rD(ctx->opcode));
1342 }
1343
1344 /* stwcx. */
1345 GEN_HANDLER(stwcx_, 0x1F, 0x16, 0x04, 0x00000000, PPC_RES)
1346 {
1347         if (rA(ctx->opcode) == 0) {
1348             gen_op_load_gpr_T0(rB(ctx->opcode));
1349         } else {
1350             gen_op_load_gpr_T0(rA(ctx->opcode));
1351             gen_op_load_gpr_T1(rB(ctx->opcode));
1352         gen_op_add();
1353         }
1354     gen_op_load_gpr_T1(rS(ctx->opcode));
1355     op_stwcx();
1356 }
1357
1358 /* sync */
1359 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x03FF0801, PPC_MEM)
1360 {
1361 }
1362
1363 /***                         Floating-point load                           ***/
1364 #define GEN_LDF(width, opc)                                                   \
1365 GEN_HANDLER(l##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)               \
1366 {                                                                             \
1367     uint32_t simm = SIMM(ctx->opcode);                                        \
1368     if (rA(ctx->opcode) == 0) {                                               \
1369         gen_op_set_T0(simm);                                                  \
1370     } else {                                                                  \
1371         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1372         if (simm != 0)                                                        \
1373             gen_op_addi(simm);                                                \
1374     }                                                                         \
1375     op_ldst(l##width);                                                        \
1376     gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1377 }
1378
1379 #define GEN_LDUF(width, opc)                                                  \
1380 GEN_HANDLER(l##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)            \
1381 {                                                                             \
1382     uint32_t simm = SIMM(ctx->opcode);                                        \
1383     if (rA(ctx->opcode) == 0 ||                                               \
1384         rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1385         RET_INVAL(ctx);                                                       \
1386         return;                                                               \
1387     }                                                                         \
1388     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1389     if (simm != 0)                                                            \
1390         gen_op_addi(simm);                                                    \
1391     op_ldst(l##width);                                                        \
1392     gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1393     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1394 }
1395
1396 #define GEN_LDUXF(width, opc)                                                 \
1397 GEN_HANDLER(l##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)           \
1398 {                                                                             \
1399     if (rA(ctx->opcode) == 0 ||                                               \
1400         rA(ctx->opcode) == rD(ctx->opcode)) {                                 \
1401         RET_INVAL(ctx);                                                       \
1402         return;                                                               \
1403     }                                                                         \
1404     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1405     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1406     gen_op_add();                                                             \
1407     op_ldst(l##width);                                                        \
1408     gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1409     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1410 }
1411
1412 #define GEN_LDXF(width, opc2, opc3)                                           \
1413 GEN_HANDLER(l##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)           \
1414 {                                                                             \
1415     if (rA(ctx->opcode) == 0) {                                               \
1416         gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1417     } else {                                                                  \
1418         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1419         gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1420         gen_op_add();                                                         \
1421     }                                                                         \
1422     op_ldst(l##width);                                                        \
1423     gen_op_store_FT1_fpr(rD(ctx->opcode));                                    \
1424 }
1425
1426 #define GEN_LDFS(width, op)                                                   \
1427 OP_LD_TABLE(width);                                                           \
1428 GEN_LDF(width, op | 0x20);                                                    \
1429 GEN_LDUF(width, op | 0x21);                                                   \
1430 GEN_LDUXF(width, op | 0x01);                                                  \
1431 GEN_LDXF(width, 0x17, op | 0x00)
1432
1433 /* lfd lfdu lfdux lfdx */
1434 GEN_LDFS(fd, 0x12);
1435 /* lfs lfsu lfsux lfsx */
1436 GEN_LDFS(fs, 0x10);
1437
1438 /***                         Floating-point store                          ***/
1439 #define GEN_STF(width, opc)                                                   \
1440 GEN_HANDLER(st##width, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)              \
1441 {                                                                             \
1442     uint32_t simm = SIMM(ctx->opcode);                                        \
1443     if (rA(ctx->opcode) == 0) {                                               \
1444         gen_op_set_T0(simm);                                                  \
1445     } else {                                                                  \
1446         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1447         if (simm != 0)                                                        \
1448             gen_op_addi(simm);                                                \
1449     }                                                                         \
1450     gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1451     op_ldst(st##width);                                                       \
1452 }
1453
1454 #define GEN_STUF(width, opc)                                                  \
1455 GEN_HANDLER(st##width##u, opc, 0xFF, 0xFF, 0x00000000, PPC_INTEGER)           \
1456 {                                                                             \
1457     uint32_t simm = SIMM(ctx->opcode);                                        \
1458     if (rA(ctx->opcode) == 0) {                                               \
1459         RET_INVAL(ctx);                                                       \
1460         return;                                                               \
1461     }                                                                         \
1462     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1463     if (simm != 0)                                                            \
1464         gen_op_addi(simm);                                                    \
1465     gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1466     op_ldst(st##width);                                                       \
1467     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1468 }
1469
1470 #define GEN_STUXF(width, opc)                                                 \
1471 GEN_HANDLER(st##width##ux, 0x1F, 0x17, opc, 0x00000001, PPC_INTEGER)          \
1472 {                                                                             \
1473     if (rA(ctx->opcode) == 0) {                                               \
1474         RET_INVAL(ctx);                                                       \
1475         return;                                                               \
1476     }                                                                         \
1477     gen_op_load_gpr_T0(rA(ctx->opcode));                                      \
1478     gen_op_load_gpr_T1(rB(ctx->opcode));                                      \
1479     gen_op_add();                                                             \
1480     gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1481     op_ldst(st##width);                                                       \
1482     gen_op_store_T0_gpr(rA(ctx->opcode));                                     \
1483 }
1484
1485 #define GEN_STXF(width, opc2, opc3)                                           \
1486 GEN_HANDLER(st##width##x, 0x1F, opc2, opc3, 0x00000001, PPC_INTEGER)          \
1487 {                                                                             \
1488     if (rA(ctx->opcode) == 0) {                                               \
1489         gen_op_load_gpr_T0(rB(ctx->opcode));                                  \
1490     } else {                                                                  \
1491         gen_op_load_gpr_T0(rA(ctx->opcode));                                  \
1492         gen_op_load_gpr_T1(rB(ctx->opcode));                                  \
1493         gen_op_add();                                                         \
1494     }                                                                         \
1495     gen_op_load_fpr_FT1(rS(ctx->opcode));                                     \
1496     op_ldst(st##width);                                                       \
1497 }
1498
1499 #define GEN_STFS(width, op)                                                   \
1500 OP_ST_TABLE(width);                                                           \
1501 GEN_STF(width, op | 0x20);                                                    \
1502 GEN_STUF(width, op | 0x21);                                                   \
1503 GEN_STUXF(width, op | 0x01);                                                  \
1504 GEN_STXF(width, 0x17, op | 0x00)
1505
1506 /* stfd stfdu stfdux stfdx */
1507 GEN_STFS(fd, 0x16);
1508 /* stfs stfsu stfsux stfsx */
1509 GEN_STFS(fs, 0x14);
1510
1511 /* Optional: */
1512 /* stfiwx */
1513 GEN_HANDLER(stfiwx, 0x1F, 0x17, 0x1E, 0x00000001, PPC_FLOAT)
1514 {
1515     RET_INVAL(ctx);
1516 }
1517
1518 /***                                Branch                                 ***/
1519
1520 /* b ba bl bla */
1521 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1522 {
1523     uint32_t li = s_ext24(LI(ctx->opcode)), target;
1524
1525     if (AA(ctx->opcode) == 0)
1526         target = ctx->nip + li - 4;
1527     else
1528         target = li;
1529     if (LK(ctx->opcode)) {
1530         gen_op_setlr(ctx->nip);
1531     }
1532     gen_op_b((long)ctx->tb, target);
1533     ctx->exception = EXCP_BRANCH;
1534 }
1535
1536 #define BCOND_IM  0
1537 #define BCOND_LR  1
1538 #define BCOND_CTR 2
1539
1540 static inline void gen_bcond(DisasContext *ctx, int type) 
1541 {                                                                             
1542     uint32_t target = 0;
1543     uint32_t bo = BO(ctx->opcode);                                            
1544     uint32_t bi = BI(ctx->opcode);                                            
1545     uint32_t mask;                                                            
1546     uint32_t li;
1547
1548     if ((bo & 0x4) == 0)
1549         gen_op_dec_ctr();                                                     
1550     switch(type) {
1551     case BCOND_IM:
1552         li = s_ext16(BD(ctx->opcode));
1553         if (AA(ctx->opcode) == 0) {
1554             target = ctx->nip + li - 4;
1555         } else {
1556             target = li;
1557         }
1558         break;
1559     case BCOND_CTR:
1560         gen_op_movl_T1_ctr();
1561         break;
1562     default:
1563     case BCOND_LR:
1564         gen_op_movl_T1_lr();
1565         break;
1566     }
1567     if (LK(ctx->opcode)) {                                        
1568         gen_op_setlr(ctx->nip);
1569     }
1570     if (bo & 0x10) {
1571         /* No CR condition */                                                 
1572         switch (bo & 0x6) {                                                   
1573         case 0:                                                               
1574             gen_op_test_ctr();
1575             break;
1576         case 2:                                                               
1577             gen_op_test_ctrz();
1578             break;                                                            
1579         default:
1580         case 4:                                                               
1581         case 6:                                                               
1582             if (type == BCOND_IM) {
1583                 gen_op_b((long)ctx->tb, target);
1584             } else {
1585                 gen_op_b_T1();
1586             }
1587             goto no_test;
1588         }
1589     } else {                                                                  
1590         mask = 1 << (3 - (bi & 0x03));                                        
1591         gen_op_load_crf_T0(bi >> 2);                                          
1592         if (bo & 0x8) {                                                       
1593             switch (bo & 0x6) {                                               
1594             case 0:                                                           
1595                 gen_op_test_ctr_true(mask);
1596                 break;                                                        
1597             case 2:                                                           
1598                 gen_op_test_ctrz_true(mask);
1599                 break;                                                        
1600             default:                                                          
1601             case 4:                                                           
1602             case 6:                                                           
1603                 gen_op_test_true(mask);
1604                 break;                                                        
1605             }                                                                 
1606         } else {                                                              
1607             switch (bo & 0x6) {                                               
1608             case 0:                                                           
1609                 gen_op_test_ctr_false(mask);
1610                 break;                                                        
1611             case 2:                                                           
1612                 gen_op_test_ctrz_false(mask);
1613                 break;                                                        
1614             default:
1615             case 4:                                                           
1616             case 6:                                                           
1617                 gen_op_test_false(mask);
1618                 break;                                                        
1619             }                                                                 
1620         }                                                                     
1621     }                                                                         
1622     if (type == BCOND_IM) {
1623         gen_op_btest((long)ctx->tb, target, ctx->nip);
1624     } else {
1625         gen_op_btest_T1(ctx->nip);
1626     }
1627  no_test:
1628     ctx->exception = EXCP_BRANCH;                                             
1629 }
1630
1631 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1632 {                                                                             
1633     gen_bcond(ctx, BCOND_IM);
1634 }
1635
1636 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW)
1637 {                                                                             
1638     gen_bcond(ctx, BCOND_CTR);
1639 }
1640
1641 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW)
1642 {                                                                             
1643     gen_bcond(ctx, BCOND_LR);
1644 }
1645
1646 /***                      Condition register logical                       ***/
1647 #define GEN_CRLOGIC(op, opc)                                                  \
1648 GEN_HANDLER(cr##op, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)                 \
1649 {                                                                             \
1650     gen_op_load_crf_T0(crbA(ctx->opcode) >> 2);                               \
1651     gen_op_getbit_T0(3 - (crbA(ctx->opcode) & 0x03));                         \
1652     gen_op_load_crf_T1(crbB(ctx->opcode) >> 2);                               \
1653     gen_op_getbit_T1(3 - (crbB(ctx->opcode) & 0x03));                         \
1654     gen_op_##op();                                                            \
1655     gen_op_load_crf_T1(crbD(ctx->opcode) >> 2);                               \
1656     gen_op_setcrfbit(~(1 << (3 - (crbD(ctx->opcode) & 0x03))),                \
1657                      3 - (crbD(ctx->opcode) & 0x03));                         \
1658     gen_op_store_T1_crf(crbD(ctx->opcode) >> 2);                              \
1659 }
1660
1661 /* crand */
1662 GEN_CRLOGIC(and, 0x08)
1663 /* crandc */
1664 GEN_CRLOGIC(andc, 0x04)
1665 /* creqv */
1666 GEN_CRLOGIC(eqv, 0x09)
1667 /* crnand */
1668 GEN_CRLOGIC(nand, 0x07)
1669 /* crnor */
1670 GEN_CRLOGIC(nor, 0x01)
1671 /* cror */
1672 GEN_CRLOGIC(or, 0x0E)
1673 /* crorc */
1674 GEN_CRLOGIC(orc, 0x0D)
1675 /* crxor */
1676 GEN_CRLOGIC(xor, 0x06)
1677 /* mcrf */
1678 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER)
1679 {
1680     gen_op_load_crf_T0(crfS(ctx->opcode));
1681     gen_op_store_T0_crf(crfD(ctx->opcode));
1682 }
1683
1684 /***                           System linkage                              ***/
1685 /* rfi (supervisor only) */
1686 GEN_HANDLER(rfi, 0x13, 0x12, 0xFF, 0x03FF8001, PPC_FLOW)
1687 {
1688 #if defined(CONFIG_USER_ONLY)
1689     RET_PRIVOPC(ctx);
1690 #else
1691     /* Restore CPU state */
1692     if (!ctx->supervisor) {
1693         RET_PRIVOPC(ctx);
1694         return;
1695     }
1696     gen_op_rfi();
1697     RET_EXCP(ctx, EXCP_RFI, 0);
1698 #endif
1699 }
1700
1701 /* sc */
1702 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFFFFD, PPC_FLOW)
1703 {
1704 #if defined(CONFIG_USER_ONLY)
1705     RET_EXCP(ctx, EXCP_SYSCALL_USER, 0);
1706 #else
1707     RET_EXCP(ctx, EXCP_SYSCALL, 0);
1708 #endif
1709 }
1710
1711 /***                                Trap                                   ***/
1712 /* tw */
1713 GEN_HANDLER(tw, 0x1F, 0x04, 0xFF, 0x00000001, PPC_FLOW)
1714 {
1715     gen_op_load_gpr_T0(rA(ctx->opcode));
1716     gen_op_load_gpr_T1(rB(ctx->opcode));
1717     gen_op_tw(TO(ctx->opcode));
1718 }
1719
1720 /* twi */
1721 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW)
1722 {
1723     gen_op_load_gpr_T0(rA(ctx->opcode));
1724 #if 0
1725     printf("%s: param=0x%04x T0=0x%04x\n", __func__,
1726            SIMM(ctx->opcode), TO(ctx->opcode));
1727 #endif
1728     gen_op_twi(SIMM(ctx->opcode), TO(ctx->opcode));
1729 }
1730
1731 /***                          Processor control                            ***/
1732 static inline int check_spr_access (int spr, int rw, int supervisor)
1733 {
1734     uint32_t rights = spr_access[spr >> 1] >> (4 * (spr & 1));
1735
1736 #if 0
1737     if (spr != LR && spr != CTR) {
1738     if (loglevel > 0) {
1739         fprintf(logfile, "%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1740                 SPR_ENCODE(spr), supervisor, rw, rights,
1741                 (rights >> ((2 * supervisor) + rw)) & 1);
1742     } else {
1743         printf("%s reg=%d s=%d rw=%d r=0x%02x 0x%02x\n", __func__,
1744                SPR_ENCODE(spr), supervisor, rw, rights,
1745                (rights >> ((2 * supervisor) + rw)) & 1);
1746     }
1747     }
1748 #endif
1749     if (rights == 0)
1750         return -1;
1751     rights = rights >> (2 * supervisor);
1752     rights = rights >> rw;
1753
1754     return rights & 1;
1755 }
1756
1757 /* mcrxr */
1758 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC)
1759 {
1760     gen_op_load_xer_cr();
1761     gen_op_store_T0_crf(crfD(ctx->opcode));
1762     gen_op_clear_xer_cr();
1763 }
1764
1765 /* mfcr */
1766 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x001FF801, PPC_MISC)
1767 {
1768     gen_op_load_cr();
1769     gen_op_store_T0_gpr(rD(ctx->opcode));
1770 }
1771
1772 /* mfmsr */
1773 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC)
1774 {
1775 #if defined(CONFIG_USER_ONLY)
1776     RET_PRIVREG(ctx);
1777 #else
1778     if (!ctx->supervisor) {
1779         RET_PRIVREG(ctx);
1780         return;
1781     }
1782     gen_op_load_msr();
1783     gen_op_store_T0_gpr(rD(ctx->opcode));
1784 #endif
1785 }
1786
1787 /* mfspr */
1788 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC)
1789 {
1790     uint32_t sprn = SPR(ctx->opcode);
1791
1792 #if defined(CONFIG_USER_ONLY)
1793     switch (check_spr_access(sprn, 0, 0))
1794 #else
1795     switch (check_spr_access(sprn, 0, ctx->supervisor))
1796 #endif
1797     {
1798     case -1:
1799         RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
1800         return;
1801     case 0:
1802         RET_PRIVREG(ctx);
1803         return;
1804     default:
1805         break;
1806         }
1807     switch (sprn) {
1808     case XER:
1809         gen_op_load_xer();
1810         break;
1811     case LR:
1812         gen_op_load_lr();
1813         break;
1814     case CTR:
1815         gen_op_load_ctr();
1816         break;
1817     case IBAT0U:
1818         gen_op_load_ibat(0, 0);
1819         break;
1820     case IBAT1U:
1821         gen_op_load_ibat(0, 1);
1822         break;
1823     case IBAT2U:
1824         gen_op_load_ibat(0, 2);
1825         break;
1826     case IBAT3U:
1827         gen_op_load_ibat(0, 3);
1828         break;
1829     case IBAT4U:
1830         gen_op_load_ibat(0, 4);
1831         break;
1832     case IBAT5U:
1833         gen_op_load_ibat(0, 5);
1834         break;
1835     case IBAT6U:
1836         gen_op_load_ibat(0, 6);
1837         break;
1838     case IBAT7U:
1839         gen_op_load_ibat(0, 7);
1840         break;
1841     case IBAT0L:
1842         gen_op_load_ibat(1, 0);
1843         break;
1844     case IBAT1L:
1845         gen_op_load_ibat(1, 1);
1846         break;
1847     case IBAT2L:
1848         gen_op_load_ibat(1, 2);
1849         break;
1850     case IBAT3L:
1851         gen_op_load_ibat(1, 3);
1852         break;
1853     case IBAT4L:
1854         gen_op_load_ibat(1, 4);
1855         break;
1856     case IBAT5L:
1857         gen_op_load_ibat(1, 5);
1858         break;
1859     case IBAT6L:
1860         gen_op_load_ibat(1, 6);
1861         break;
1862     case IBAT7L:
1863         gen_op_load_ibat(1, 7);
1864         break;
1865     case DBAT0U:
1866         gen_op_load_dbat(0, 0);
1867         break;
1868     case DBAT1U:
1869         gen_op_load_dbat(0, 1);
1870         break;
1871     case DBAT2U:
1872         gen_op_load_dbat(0, 2);
1873         break;
1874     case DBAT3U:
1875         gen_op_load_dbat(0, 3);
1876         break;
1877     case DBAT4U:
1878         gen_op_load_dbat(0, 4);
1879         break;
1880     case DBAT5U:
1881         gen_op_load_dbat(0, 5);
1882         break;
1883     case DBAT6U:
1884         gen_op_load_dbat(0, 6);
1885         break;
1886     case DBAT7U:
1887         gen_op_load_dbat(0, 7);
1888         break;
1889     case DBAT0L:
1890         gen_op_load_dbat(1, 0);
1891         break;
1892     case DBAT1L:
1893         gen_op_load_dbat(1, 1);
1894         break;
1895     case DBAT2L:
1896         gen_op_load_dbat(1, 2);
1897         break;
1898     case DBAT3L:
1899         gen_op_load_dbat(1, 3);
1900         break;
1901     case DBAT4L:
1902         gen_op_load_dbat(1, 4);
1903         break;
1904     case DBAT5L:
1905         gen_op_load_dbat(1, 5);
1906         break;
1907     case DBAT6L:
1908         gen_op_load_dbat(1, 6);
1909         break;
1910     case DBAT7L:
1911         gen_op_load_dbat(1, 7);
1912         break;
1913     case SDR1:
1914         gen_op_load_sdr1();
1915         break;
1916     case V_TBL:
1917         gen_op_load_tbl();
1918         break;
1919     case V_TBU:
1920         gen_op_load_tbu();
1921         break;
1922     case DECR:
1923         gen_op_load_decr();
1924         break;
1925     default:
1926         gen_op_load_spr(sprn);
1927         break;
1928     }
1929     gen_op_store_T0_gpr(rD(ctx->opcode));
1930 }
1931
1932 /* mftb */
1933 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MISC)
1934 {
1935     uint32_t sprn = SPR(ctx->opcode);
1936
1937         /* We need to update the time base before reading it */
1938     switch (sprn) {
1939     case V_TBL:
1940         gen_op_load_tbl();
1941         break;
1942     case V_TBU:
1943         gen_op_load_tbu();
1944         break;
1945     default:
1946         RET_INVAL(ctx);
1947         return;
1948     }
1949     gen_op_store_T0_gpr(rD(ctx->opcode));
1950 }
1951
1952 /* mtcrf */
1953 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00100801, PPC_MISC)
1954 {
1955     gen_op_load_gpr_T0(rS(ctx->opcode));
1956     gen_op_store_cr(CRM(ctx->opcode));
1957 }
1958
1959 /* mtmsr */
1960 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001FF801, PPC_MISC)
1961 {
1962 #if defined(CONFIG_USER_ONLY)
1963     RET_PRIVREG(ctx);
1964 #else
1965     if (!ctx->supervisor) {
1966         RET_PRIVREG(ctx);
1967         return;
1968     }
1969     gen_op_load_gpr_T0(rS(ctx->opcode));
1970     gen_op_store_msr();
1971     /* Must stop the translation as machine state (may have) changed */
1972     RET_MTMSR(ctx);
1973 #endif
1974 }
1975
1976 /* mtspr */
1977 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000001, PPC_MISC)
1978 {
1979     uint32_t sprn = SPR(ctx->opcode);
1980
1981 #if 0
1982     if (loglevel > 0) {
1983         fprintf(logfile, "MTSPR %d src=%d (%d)\n", SPR_ENCODE(sprn),
1984                 rS(ctx->opcode), sprn);
1985     }
1986 #endif
1987 #if defined(CONFIG_USER_ONLY)
1988     switch (check_spr_access(sprn, 1, 0))
1989 #else
1990     switch (check_spr_access(sprn, 1, ctx->supervisor))
1991 #endif
1992     {
1993     case -1:
1994         RET_EXCP(ctx, EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_SPR);
1995         break;
1996     case 0:
1997         RET_PRIVREG(ctx);
1998         break;
1999     default:
2000         break;
2001     }
2002     gen_op_load_gpr_T0(rS(ctx->opcode));
2003     switch (sprn) {
2004     case XER:
2005         gen_op_store_xer();
2006         break;
2007     case LR:
2008         gen_op_store_lr();
2009         break;
2010     case CTR:
2011         gen_op_store_ctr();
2012         break;
2013     case IBAT0U:
2014         gen_op_store_ibat(0, 0);
2015         RET_MTMSR(ctx);
2016         break;
2017     case IBAT1U:
2018         gen_op_store_ibat(0, 1);
2019         RET_MTMSR(ctx);
2020         break;
2021     case IBAT2U:
2022         gen_op_store_ibat(0, 2);
2023         RET_MTMSR(ctx);
2024         break;
2025     case IBAT3U:
2026         gen_op_store_ibat(0, 3);
2027         RET_MTMSR(ctx);
2028         break;
2029     case IBAT4U:
2030         gen_op_store_ibat(0, 4);
2031         RET_MTMSR(ctx);
2032         break;
2033     case IBAT5U:
2034         gen_op_store_ibat(0, 5);
2035         RET_MTMSR(ctx);
2036         break;
2037     case IBAT6U:
2038         gen_op_store_ibat(0, 6);
2039         RET_MTMSR(ctx);
2040         break;
2041     case IBAT7U:
2042         gen_op_store_ibat(0, 7);
2043         RET_MTMSR(ctx);
2044         break;
2045     case IBAT0L:
2046         gen_op_store_ibat(1, 0);
2047         RET_MTMSR(ctx);
2048         break;
2049     case IBAT1L:
2050         gen_op_store_ibat(1, 1);
2051         RET_MTMSR(ctx);
2052         break;
2053     case IBAT2L:
2054         gen_op_store_ibat(1, 2);
2055         RET_MTMSR(ctx);
2056         break;
2057     case IBAT3L:
2058         gen_op_store_ibat(1, 3);
2059         RET_MTMSR(ctx);
2060         break;
2061     case IBAT4L:
2062         gen_op_store_ibat(1, 4);
2063         RET_MTMSR(ctx);
2064         break;
2065     case IBAT5L:
2066         gen_op_store_ibat(1, 5);
2067         RET_MTMSR(ctx);
2068         break;
2069     case IBAT6L:
2070         gen_op_store_ibat(1, 6);
2071         RET_MTMSR(ctx);
2072         break;
2073     case IBAT7L:
2074         gen_op_store_ibat(1, 7);
2075         RET_MTMSR(ctx);
2076         break;
2077     case DBAT0U:
2078         gen_op_store_dbat(0, 0);
2079         RET_MTMSR(ctx);
2080         break;
2081     case DBAT1U:
2082         gen_op_store_dbat(0, 1);
2083         RET_MTMSR(ctx);
2084         break;
2085     case DBAT2U:
2086         gen_op_store_dbat(0, 2);
2087         RET_MTMSR(ctx);
2088         break;
2089     case DBAT3U:
2090         gen_op_store_dbat(0, 3);
2091         RET_MTMSR(ctx);
2092         break;
2093     case DBAT4U:
2094         gen_op_store_dbat(0, 4);
2095         RET_MTMSR(ctx);
2096         break;
2097     case DBAT5U:
2098         gen_op_store_dbat(0, 5);
2099         RET_MTMSR(ctx);
2100         break;
2101     case DBAT6U:
2102         gen_op_store_dbat(0, 6);
2103         RET_MTMSR(ctx);
2104         break;
2105     case DBAT7U:
2106         gen_op_store_dbat(0, 7);
2107         RET_MTMSR(ctx);
2108         break;
2109     case DBAT0L:
2110         gen_op_store_dbat(1, 0);
2111         RET_MTMSR(ctx);
2112         break;
2113     case DBAT1L:
2114         gen_op_store_dbat(1, 1);
2115         RET_MTMSR(ctx);
2116         break;
2117     case DBAT2L:
2118         gen_op_store_dbat(1, 2);
2119         RET_MTMSR(ctx);
2120         break;
2121     case DBAT3L:
2122         gen_op_store_dbat(1, 3);
2123         RET_MTMSR(ctx);
2124         break;
2125     case DBAT4L:
2126         gen_op_store_dbat(1, 4);
2127         RET_MTMSR(ctx);
2128         break;
2129     case DBAT5L:
2130         gen_op_store_dbat(1, 5);
2131         RET_MTMSR(ctx);
2132         break;
2133     case DBAT6L:
2134         gen_op_store_dbat(1, 6);
2135         RET_MTMSR(ctx);
2136         break;
2137     case DBAT7L:
2138         gen_op_store_dbat(1, 7);
2139         RET_MTMSR(ctx);
2140         break;
2141     case SDR1:
2142         gen_op_store_sdr1();
2143         RET_MTMSR(ctx);
2144         break;
2145     case O_TBL:
2146         gen_op_store_tbl();
2147         break;
2148     case O_TBU:
2149         gen_op_store_tbu();
2150         break;
2151     case DECR:
2152         gen_op_store_decr();
2153         break;
2154 #if 0
2155     case HID0:
2156         gen_op_store_hid0();
2157         break;
2158 #endif
2159     default:
2160         gen_op_store_spr(sprn);
2161         break;
2162     }
2163 }
2164
2165 /***                         Cache management                              ***/
2166 /* For now, all those will be implemented as nop:
2167  * this is valid, regarding the PowerPC specs...
2168  * We just have to flush tb while invalidating instruction cache lines...
2169  */
2170 /* dcbf */
2171 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03E00001, PPC_CACHE)
2172 {
2173     if (rA(ctx->opcode) == 0) {
2174         gen_op_load_gpr_T0(rB(ctx->opcode));
2175     } else {
2176         gen_op_load_gpr_T0(rA(ctx->opcode));
2177         gen_op_load_gpr_T1(rB(ctx->opcode));
2178         gen_op_add();
2179     }
2180     op_ldst(lbz);
2181 }
2182
2183 /* dcbi (Supervisor only) */
2184 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE)
2185 {
2186 #if defined(CONFIG_USER_ONLY)
2187     RET_PRIVOPC(ctx);
2188 #else
2189     if (!ctx->supervisor) {
2190         RET_PRIVOPC(ctx);
2191         return;
2192     }
2193     if (rA(ctx->opcode) == 0) {
2194         gen_op_load_gpr_T0(rB(ctx->opcode));
2195     } else {
2196         gen_op_load_gpr_T0(rA(ctx->opcode));
2197         gen_op_load_gpr_T1(rB(ctx->opcode));
2198         gen_op_add();
2199     }
2200     op_ldst(lbz);
2201     op_ldst(stb);
2202 #endif
2203 }
2204
2205 /* dcdst */
2206 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE)
2207 {
2208     if (rA(ctx->opcode) == 0) {
2209         gen_op_load_gpr_T0(rB(ctx->opcode));
2210     } else {
2211         gen_op_load_gpr_T0(rA(ctx->opcode));
2212         gen_op_load_gpr_T1(rB(ctx->opcode));
2213         gen_op_add();
2214     }
2215     op_ldst(lbz);
2216 }
2217
2218 /* dcbt */
2219 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x03E00001, PPC_CACHE)
2220 {
2221 }
2222
2223 /* dcbtst */
2224 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE)
2225 {
2226 }
2227
2228 /* dcbz */
2229 #if defined(CONFIG_USER_ONLY)
2230 #define op_dcbz() gen_op_dcbz_raw()
2231 #else
2232 #define op_dcbz() (*gen_op_dcbz[ctx->mem_idx])()
2233 static GenOpFunc *gen_op_dcbz[] = {
2234     &gen_op_dcbz_user,
2235     &gen_op_dcbz_kernel,
2236 };
2237 #endif
2238
2239 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03E00001, PPC_CACHE)
2240 {
2241     if (rA(ctx->opcode) == 0) {
2242         gen_op_load_gpr_T0(rB(ctx->opcode));
2243     } else {
2244         gen_op_load_gpr_T0(rA(ctx->opcode));
2245         gen_op_load_gpr_T1(rB(ctx->opcode));
2246         gen_op_add();
2247     }
2248     op_dcbz();
2249     gen_op_check_reservation();
2250 }
2251
2252 /* icbi */
2253 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE)
2254 {
2255     if (rA(ctx->opcode) == 0) {
2256         gen_op_load_gpr_T0(rB(ctx->opcode));
2257     } else {
2258         gen_op_load_gpr_T0(rA(ctx->opcode));
2259         gen_op_load_gpr_T1(rB(ctx->opcode));
2260         gen_op_add();
2261     }
2262     gen_op_icbi();
2263 }
2264
2265 /* Optional: */
2266 /* dcba */
2267 GEN_HANDLER(dcba, 0x1F, 0x16, 0x07, 0x03E00001, PPC_CACHE_OPT)
2268 {
2269 }
2270
2271 /***                    Segment register manipulation                      ***/
2272 /* Supervisor only: */
2273 /* mfsr */
2274 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT)
2275 {
2276 #if defined(CONFIG_USER_ONLY)
2277     RET_PRIVREG(ctx);
2278 #else
2279     if (!ctx->supervisor) {
2280         RET_PRIVREG(ctx);
2281         return;
2282     }
2283     gen_op_load_sr(SR(ctx->opcode));
2284     gen_op_store_T0_gpr(rD(ctx->opcode));
2285 #endif
2286 }
2287
2288 /* mfsrin */
2289 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT)
2290 {
2291 #if defined(CONFIG_USER_ONLY)
2292     RET_PRIVREG(ctx);
2293 #else
2294     if (!ctx->supervisor) {
2295         RET_PRIVREG(ctx);
2296         return;
2297     }
2298     gen_op_load_gpr_T1(rB(ctx->opcode));
2299     gen_op_load_srin();
2300     gen_op_store_T0_gpr(rD(ctx->opcode));
2301 #endif
2302 }
2303
2304 /* mtsr */
2305 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT)
2306 {
2307 #if defined(CONFIG_USER_ONLY)
2308     RET_PRIVREG(ctx);
2309 #else
2310     if (!ctx->supervisor) {
2311         RET_PRIVREG(ctx);
2312         return;
2313     }
2314     gen_op_load_gpr_T0(rS(ctx->opcode));
2315     gen_op_store_sr(SR(ctx->opcode));
2316 #endif
2317 }
2318
2319 /* mtsrin */
2320 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT)
2321 {
2322 #if defined(CONFIG_USER_ONLY)
2323     RET_PRIVREG(ctx);
2324 #else
2325     if (!ctx->supervisor) {
2326         RET_PRIVREG(ctx);
2327         return;
2328     }
2329     gen_op_load_gpr_T0(rS(ctx->opcode));
2330     gen_op_load_gpr_T1(rB(ctx->opcode));
2331     gen_op_store_srin();
2332 #endif
2333 }
2334
2335 /***                      Lookaside buffer management                      ***/
2336 /* Optional & supervisor only: */
2337 /* tlbia */
2338 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_OPT)
2339 {
2340 #if defined(CONFIG_USER_ONLY)
2341     RET_PRIVOPC(ctx);
2342 #else
2343     if (!ctx->supervisor) {
2344         if (loglevel)
2345             fprintf(logfile, "%s: ! supervisor\n", __func__);
2346         RET_PRIVOPC(ctx);
2347         return;
2348     }
2349     gen_op_tlbia();
2350     RET_MTMSR(ctx);
2351 #endif
2352 }
2353
2354 /* tlbie */
2355 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x03FF0001, PPC_MEM)
2356 {
2357 #if defined(CONFIG_USER_ONLY)
2358     RET_PRIVOPC(ctx);
2359 #else
2360     if (!ctx->supervisor) {
2361         RET_PRIVOPC(ctx);
2362         return;
2363     }
2364     gen_op_load_gpr_T0(rB(ctx->opcode));
2365     gen_op_tlbie();
2366     RET_MTMSR(ctx);
2367 #endif
2368 }
2369
2370 /* tlbsync */
2371 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM)
2372 {
2373 #if defined(CONFIG_USER_ONLY)
2374     RET_PRIVOPC(ctx);
2375 #else
2376     if (!ctx->supervisor) {
2377         RET_PRIVOPC(ctx);
2378         return;
2379     }
2380     /* This has no effect: it should ensure that all previous
2381      * tlbie have completed
2382      */
2383     RET_MTMSR(ctx);
2384 #endif
2385 }
2386
2387 /***                              External control                         ***/
2388 /* Optional: */
2389 /* eciwx */
2390 #if defined(CONFIG_USER_ONLY)
2391 #define op_eciwx() gen_op_eciwx_raw()
2392 #define op_ecowx() gen_op_ecowx_raw()
2393 #else
2394 #define op_eciwx() (*gen_op_eciwx[ctx->mem_idx])()
2395 #define op_ecowx() (*gen_op_ecowx[ctx->mem_idx])()
2396 static GenOpFunc *gen_op_eciwx[] = {
2397     &gen_op_eciwx_user,
2398     &gen_op_eciwx_kernel,
2399 };
2400 static GenOpFunc *gen_op_ecowx[] = {
2401     &gen_op_ecowx_user,
2402     &gen_op_ecowx_kernel,
2403 };
2404 #endif
2405
2406 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN)
2407 {
2408     /* Should check EAR[E] & alignment ! */
2409     if (rA(ctx->opcode) == 0) {
2410         gen_op_load_gpr_T0(rB(ctx->opcode));
2411     } else {
2412         gen_op_load_gpr_T0(rA(ctx->opcode));
2413         gen_op_load_gpr_T1(rB(ctx->opcode));
2414         gen_op_add();
2415     }
2416     op_eciwx();
2417     gen_op_store_T0_gpr(rD(ctx->opcode));
2418 }
2419
2420 /* ecowx */
2421 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN)
2422 {
2423     /* Should check EAR[E] & alignment ! */
2424     if (rA(ctx->opcode) == 0) {
2425         gen_op_load_gpr_T0(rB(ctx->opcode));
2426     } else {
2427         gen_op_load_gpr_T0(rA(ctx->opcode));
2428         gen_op_load_gpr_T1(rB(ctx->opcode));
2429         gen_op_add();
2430     }
2431     gen_op_load_gpr_T2(rS(ctx->opcode));
2432     op_ecowx();
2433 }
2434
2435 /* End opcode list */
2436 GEN_OPCODE_MARK(end);
2437
2438 /*****************************************************************************/
2439 #include <stdlib.h>
2440 #include <string.h>
2441
2442 int fflush (FILE *stream);
2443
2444 /* Main ppc opcodes table:
2445  * at init, all opcodes are invalids
2446  */
2447 static opc_handler_t *ppc_opcodes[0x40];
2448
2449 /* Opcode types */
2450 enum {
2451     PPC_DIRECT   = 0, /* Opcode routine        */
2452     PPC_INDIRECT = 1, /* Indirect opcode table */
2453 };
2454
2455 static inline int is_indirect_opcode (void *handler)
2456 {
2457     return ((unsigned long)handler & 0x03) == PPC_INDIRECT;
2458 }
2459
2460 static inline opc_handler_t **ind_table(void *handler)
2461 {
2462     return (opc_handler_t **)((unsigned long)handler & ~3);
2463 }
2464
2465 /* Instruction table creation */
2466 /* Opcodes tables creation */
2467 static void fill_new_table (opc_handler_t **table, int len)
2468 {
2469     int i;
2470
2471     for (i = 0; i < len; i++)
2472         table[i] = &invalid_handler;
2473 }
2474
2475 static int create_new_table (opc_handler_t **table, unsigned char idx)
2476 {
2477     opc_handler_t **tmp;
2478
2479     tmp = malloc(0x20 * sizeof(opc_handler_t));
2480     if (tmp == NULL)
2481         return -1;
2482     fill_new_table(tmp, 0x20);
2483     table[idx] = (opc_handler_t *)((unsigned long)tmp | PPC_INDIRECT);
2484
2485     return 0;
2486 }
2487
2488 static int insert_in_table (opc_handler_t **table, unsigned char idx,
2489                             opc_handler_t *handler)
2490 {
2491     if (table[idx] != &invalid_handler)
2492         return -1;
2493     table[idx] = handler;
2494
2495     return 0;
2496 }
2497
2498 static int register_direct_insn (opc_handler_t **ppc_opcodes,
2499                                  unsigned char idx, opc_handler_t *handler)
2500 {
2501     if (insert_in_table(ppc_opcodes, idx, handler) < 0) {
2502         printf("*** ERROR: opcode %02x already assigned in main "
2503                 "opcode table\n", idx);
2504         return -1;
2505     }
2506
2507     return 0;
2508 }
2509
2510 static int register_ind_in_table (opc_handler_t **table,
2511                                   unsigned char idx1, unsigned char idx2,
2512                                   opc_handler_t *handler)
2513 {
2514     if (table[idx1] == &invalid_handler) {
2515         if (create_new_table(table, idx1) < 0) {
2516             printf("*** ERROR: unable to create indirect table "
2517                     "idx=%02x\n", idx1);
2518             return -1;
2519         }
2520     } else {
2521         if (!is_indirect_opcode(table[idx1])) {
2522             printf("*** ERROR: idx %02x already assigned to a direct "
2523                     "opcode\n", idx1);
2524             return -1;
2525         }
2526     }
2527     if (handler != NULL &&
2528         insert_in_table(ind_table(table[idx1]), idx2, handler) < 0) {
2529         printf("*** ERROR: opcode %02x already assigned in "
2530                 "opcode table %02x\n", idx2, idx1);
2531         return -1;
2532     }
2533
2534     return 0;
2535 }
2536
2537 static int register_ind_insn (opc_handler_t **ppc_opcodes,
2538                               unsigned char idx1, unsigned char idx2,
2539                                opc_handler_t *handler)
2540 {
2541     int ret;
2542
2543     ret = register_ind_in_table(ppc_opcodes, idx1, idx2, handler);
2544
2545     return ret;
2546 }
2547
2548 static int register_dblind_insn (opc_handler_t **ppc_opcodes, 
2549                                  unsigned char idx1, unsigned char idx2,
2550                                   unsigned char idx3, opc_handler_t *handler)
2551 {
2552     if (register_ind_in_table(ppc_opcodes, idx1, idx2, NULL) < 0) {
2553         printf("*** ERROR: unable to join indirect table idx "
2554                 "[%02x-%02x]\n", idx1, idx2);
2555         return -1;
2556     }
2557     if (register_ind_in_table(ind_table(ppc_opcodes[idx1]), idx2, idx3,
2558                               handler) < 0) {
2559         printf("*** ERROR: unable to insert opcode "
2560                 "[%02x-%02x-%02x]\n", idx1, idx2, idx3);
2561         return -1;
2562     }
2563
2564     return 0;
2565 }
2566
2567 static int register_insn (opc_handler_t **ppc_opcodes, opcode_t *insn)
2568 {
2569     if (insn->opc2 != 0xFF) {
2570         if (insn->opc3 != 0xFF) {
2571             if (register_dblind_insn(ppc_opcodes, insn->opc1, insn->opc2,
2572                                      insn->opc3, &insn->handler) < 0)
2573                 return -1;
2574         } else {
2575             if (register_ind_insn(ppc_opcodes, insn->opc1,
2576                                   insn->opc2, &insn->handler) < 0)
2577                 return -1;
2578         }
2579     } else {
2580         if (register_direct_insn(ppc_opcodes, insn->opc1, &insn->handler) < 0)
2581             return -1;
2582     }
2583
2584     return 0;
2585 }
2586
2587 static int test_opcode_table (opc_handler_t **table, int len)
2588 {
2589     int i, count, tmp;
2590
2591     for (i = 0, count = 0; i < len; i++) {
2592         /* Consistency fixup */
2593         if (table[i] == NULL)
2594             table[i] = &invalid_handler;
2595         if (table[i] != &invalid_handler) {
2596             if (is_indirect_opcode(table[i])) {
2597                 tmp = test_opcode_table(ind_table(table[i]), 0x20);
2598                 if (tmp == 0) {
2599                     free(table[i]);
2600                     table[i] = &invalid_handler;
2601                 } else {
2602                     count++;
2603                 }
2604             } else {
2605                 count++;
2606             }
2607         }
2608     }
2609
2610     return count;
2611 }
2612
2613 static void fix_opcode_tables (opc_handler_t **ppc_opcodes)
2614 {
2615     if (test_opcode_table(ppc_opcodes, 0x40) == 0)
2616         printf("*** WARNING: no opcode defined !\n");
2617 }
2618
2619 #define SPR_RIGHTS(rw, priv) (1 << ((2 * (priv)) + (rw)))
2620 #define SPR_UR SPR_RIGHTS(0, 0)
2621 #define SPR_UW SPR_RIGHTS(1, 0)
2622 #define SPR_SR SPR_RIGHTS(0, 1)
2623 #define SPR_SW SPR_RIGHTS(1, 1)
2624
2625 #define spr_set_rights(spr, rights)                            \
2626 do {                                                           \
2627     spr_access[(spr) >> 1] |= ((rights) << (4 * ((spr) & 1))); \
2628 } while (0)
2629
2630 static void init_spr_rights (uint32_t pvr)
2631 {
2632     /* XER    (SPR 1) */
2633     spr_set_rights(XER,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2634     /* LR     (SPR 8) */
2635     spr_set_rights(LR,     SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2636     /* CTR    (SPR 9) */
2637     spr_set_rights(CTR,    SPR_UR | SPR_UW | SPR_SR | SPR_SW);
2638     /* TBL    (SPR 268) */
2639     spr_set_rights(V_TBL,  SPR_UR | SPR_SR);
2640     /* TBU    (SPR 269) */
2641     spr_set_rights(V_TBU,  SPR_UR | SPR_SR);
2642     /* DSISR  (SPR 18) */
2643     spr_set_rights(DSISR,  SPR_SR | SPR_SW);
2644     /* DAR    (SPR 19) */
2645     spr_set_rights(DAR,    SPR_SR | SPR_SW);
2646     /* DEC    (SPR 22) */
2647     spr_set_rights(DECR,   SPR_SR | SPR_SW);
2648     /* SDR1   (SPR 25) */
2649     spr_set_rights(SDR1,   SPR_SR | SPR_SW);
2650     /* SRR0   (SPR 26) */
2651     spr_set_rights(SRR0,   SPR_SR | SPR_SW);
2652     /* SRR1   (SPR 27) */
2653     spr_set_rights(SRR1,   SPR_SR | SPR_SW);
2654     /* SPRG0  (SPR 272) */
2655     spr_set_rights(SPRG0,  SPR_SR | SPR_SW);
2656     /* SPRG1  (SPR 273) */
2657     spr_set_rights(SPRG1,  SPR_SR | SPR_SW);
2658     /* SPRG2  (SPR 274) */
2659     spr_set_rights(SPRG2,  SPR_SR | SPR_SW);
2660     /* SPRG3  (SPR 275) */
2661     spr_set_rights(SPRG3,  SPR_SR | SPR_SW);
2662     /* ASR    (SPR 280) */
2663     spr_set_rights(ASR,    SPR_SR | SPR_SW);
2664     /* EAR    (SPR 282) */
2665     spr_set_rights(EAR,    SPR_SR | SPR_SW);
2666     /* TBL    (SPR 284) */
2667     spr_set_rights(O_TBL,  SPR_SW);
2668     /* TBU    (SPR 285) */
2669     spr_set_rights(O_TBU,  SPR_SW);
2670     /* PVR    (SPR 287) */
2671     spr_set_rights(PVR,    SPR_SR);
2672     /* IBAT0U (SPR 528) */
2673     spr_set_rights(IBAT0U, SPR_SR | SPR_SW);
2674     /* IBAT0L (SPR 529) */
2675     spr_set_rights(IBAT0L, SPR_SR | SPR_SW);
2676     /* IBAT1U (SPR 530) */
2677     spr_set_rights(IBAT1U, SPR_SR | SPR_SW);
2678     /* IBAT1L (SPR 531) */
2679     spr_set_rights(IBAT1L, SPR_SR | SPR_SW);
2680     /* IBAT2U (SPR 532) */
2681     spr_set_rights(IBAT2U, SPR_SR | SPR_SW);
2682     /* IBAT2L (SPR 533) */
2683     spr_set_rights(IBAT2L, SPR_SR | SPR_SW);
2684     /* IBAT3U (SPR 534) */
2685     spr_set_rights(IBAT3U, SPR_SR | SPR_SW);
2686     /* IBAT3L (SPR 535) */
2687     spr_set_rights(IBAT3L, SPR_SR | SPR_SW);
2688     /* DBAT0U (SPR 536) */
2689     spr_set_rights(DBAT0U, SPR_SR | SPR_SW);
2690     /* DBAT0L (SPR 537) */
2691     spr_set_rights(DBAT0L, SPR_SR | SPR_SW);
2692     /* DBAT1U (SPR 538) */
2693     spr_set_rights(DBAT1U, SPR_SR | SPR_SW);
2694     /* DBAT1L (SPR 539) */
2695     spr_set_rights(DBAT1L, SPR_SR | SPR_SW);
2696     /* DBAT2U (SPR 540) */
2697     spr_set_rights(DBAT2U, SPR_SR | SPR_SW);
2698     /* DBAT2L (SPR 541) */
2699     spr_set_rights(DBAT2L, SPR_SR | SPR_SW);
2700     /* DBAT3U (SPR 542) */
2701     spr_set_rights(DBAT3U, SPR_SR | SPR_SW);
2702     /* DBAT3L (SPR 543) */
2703     spr_set_rights(DBAT3L, SPR_SR | SPR_SW);
2704     /* FPECR  (SPR 1022) */
2705     spr_set_rights(FPECR,  SPR_SR | SPR_SW);
2706     /* Special registers for PPC 604 */
2707     if ((pvr & 0xFFFF0000) == 0x00040000) {
2708         /* IABR */
2709         spr_set_rights(IABR ,  SPR_SR | SPR_SW);
2710         /* DABR   (SPR 1013) */
2711         spr_set_rights(DABR,   SPR_SR | SPR_SW);
2712         /* HID0 */
2713         spr_set_rights(HID0,   SPR_SR | SPR_SW);
2714         /* PIR */
2715     spr_set_rights(PIR,    SPR_SR | SPR_SW);
2716         /* PMC1 */
2717         spr_set_rights(PMC1,   SPR_SR | SPR_SW);
2718         /* PMC2 */
2719         spr_set_rights(PMC2,   SPR_SR | SPR_SW);
2720         /* MMCR0 */
2721         spr_set_rights(MMCR0,  SPR_SR | SPR_SW);
2722         /* SIA */
2723         spr_set_rights(SIA,    SPR_SR | SPR_SW);
2724         /* SDA */
2725         spr_set_rights(SDA,    SPR_SR | SPR_SW);
2726     }
2727     /* Special registers for MPC740/745/750/755 (aka G3) & IBM 750 */
2728     if ((pvr & 0xFFFF0000) == 0x00080000 ||
2729         (pvr & 0xFFFF0000) == 0x70000000) {
2730         /* HID0 */
2731         spr_set_rights(HID0,   SPR_SR | SPR_SW);
2732         /* HID1 */
2733         spr_set_rights(HID1,   SPR_SR | SPR_SW);
2734         /* IABR */
2735         spr_set_rights(IABR,   SPR_SR | SPR_SW);
2736         /* ICTC */
2737         spr_set_rights(ICTC,   SPR_SR | SPR_SW);
2738         /* L2CR */
2739         spr_set_rights(L2CR,   SPR_SR | SPR_SW);
2740         /* MMCR0 */
2741         spr_set_rights(MMCR0,  SPR_SR | SPR_SW);
2742         /* MMCR1 */
2743         spr_set_rights(MMCR1,  SPR_SR | SPR_SW);
2744         /* PMC1 */
2745         spr_set_rights(PMC1,   SPR_SR | SPR_SW);
2746         /* PMC2 */
2747         spr_set_rights(PMC2,   SPR_SR | SPR_SW);
2748         /* PMC3 */
2749         spr_set_rights(PMC3,   SPR_SR | SPR_SW);
2750         /* PMC4 */
2751         spr_set_rights(PMC4,   SPR_SR | SPR_SW);
2752         /* SIA */
2753         spr_set_rights(SIA,    SPR_SR | SPR_SW);
2754         /* SDA */
2755         spr_set_rights(SDA,    SPR_SR | SPR_SW);
2756         /* THRM1 */
2757         spr_set_rights(THRM1,  SPR_SR | SPR_SW);
2758         /* THRM2 */
2759         spr_set_rights(THRM2,  SPR_SR | SPR_SW);
2760         /* THRM3 */
2761         spr_set_rights(THRM3,  SPR_SR | SPR_SW);
2762         /* UMMCR0 */
2763         spr_set_rights(UMMCR0, SPR_UR | SPR_UW);
2764         /* UMMCR1 */
2765         spr_set_rights(UMMCR1, SPR_UR | SPR_UW);
2766         /* UPMC1 */
2767         spr_set_rights(UPMC1,  SPR_UR | SPR_UW);
2768         /* UPMC2 */
2769         spr_set_rights(UPMC2,  SPR_UR | SPR_UW);
2770         /* UPMC3 */
2771         spr_set_rights(UPMC3,  SPR_UR | SPR_UW);
2772         /* UPMC4 */
2773         spr_set_rights(UPMC4,  SPR_UR | SPR_UW);
2774         /* USIA */
2775         spr_set_rights(USIA,   SPR_UR | SPR_UW);
2776     }
2777     /* MPC755 has special registers */
2778     if (pvr == 0x00083100) {
2779         /* SPRG4 */
2780         spr_set_rights(SPRG4, SPR_SR | SPR_SW);
2781         /* SPRG5 */
2782         spr_set_rights(SPRG5, SPR_SR | SPR_SW);
2783         /* SPRG6 */
2784         spr_set_rights(SPRG6, SPR_SR | SPR_SW);
2785         /* SPRG7 */
2786         spr_set_rights(SPRG7, SPR_SR | SPR_SW);
2787         /* IBAT4U */
2788         spr_set_rights(IBAT4U, SPR_SR | SPR_SW);
2789         /* IBAT4L */
2790         spr_set_rights(IBAT4L, SPR_SR | SPR_SW);
2791         /* IBAT5U */
2792         spr_set_rights(IBAT5U, SPR_SR | SPR_SW);
2793         /* IBAT5L */
2794         spr_set_rights(IBAT5L, SPR_SR | SPR_SW);
2795         /* IBAT6U */
2796         spr_set_rights(IBAT6U, SPR_SR | SPR_SW);
2797         /* IBAT6L */
2798         spr_set_rights(IBAT6L, SPR_SR | SPR_SW);
2799         /* IBAT7U */
2800         spr_set_rights(IBAT7U, SPR_SR | SPR_SW);
2801         /* IBAT7L */
2802         spr_set_rights(IBAT7L, SPR_SR | SPR_SW);
2803         /* DBAT4U */
2804         spr_set_rights(DBAT4U, SPR_SR | SPR_SW);
2805         /* DBAT4L */
2806         spr_set_rights(DBAT4L, SPR_SR | SPR_SW);
2807         /* DBAT5U */
2808         spr_set_rights(DBAT5U, SPR_SR | SPR_SW);
2809         /* DBAT5L */
2810         spr_set_rights(DBAT5L, SPR_SR | SPR_SW);
2811         /* DBAT6U */
2812         spr_set_rights(DBAT6U, SPR_SR | SPR_SW);
2813         /* DBAT6L */
2814         spr_set_rights(DBAT6L, SPR_SR | SPR_SW);
2815         /* DBAT7U */
2816         spr_set_rights(DBAT7U, SPR_SR | SPR_SW);
2817         /* DBAT7L */
2818         spr_set_rights(DBAT7L, SPR_SR | SPR_SW);
2819         /* DMISS */
2820         spr_set_rights(DMISS,  SPR_SR | SPR_SW);
2821         /* DCMP */
2822         spr_set_rights(DCMP,   SPR_SR | SPR_SW);
2823         /* DHASH1 */
2824         spr_set_rights(DHASH1, SPR_SR | SPR_SW);
2825         /* DHASH2 */
2826         spr_set_rights(DHASH2, SPR_SR | SPR_SW);
2827         /* IMISS */
2828         spr_set_rights(IMISS,  SPR_SR | SPR_SW);
2829         /* ICMP */
2830         spr_set_rights(ICMP,   SPR_SR | SPR_SW);
2831         /* RPA */
2832         spr_set_rights(RPA,    SPR_SR | SPR_SW);
2833         /* HID2 */
2834         spr_set_rights(HID2,   SPR_SR | SPR_SW);
2835         /* L2PM */
2836         spr_set_rights(L2PM,   SPR_SR | SPR_SW);
2837     }
2838 }
2839
2840 /*****************************************************************************/
2841 /* PPC "main stream" common instructions (no optional ones) */
2842
2843 typedef struct ppc_proc_t {
2844     int flags;
2845     void *specific;
2846 } ppc_proc_t;
2847
2848 typedef struct ppc_def_t {
2849     unsigned long pvr;
2850     unsigned long pvr_mask;
2851     ppc_proc_t *proc;
2852 } ppc_def_t;
2853
2854 static ppc_proc_t ppc_proc_common = {
2855     .flags    = PPC_COMMON,
2856     .specific = NULL,
2857 };
2858
2859 static ppc_proc_t ppc_proc_G3 = {
2860     .flags    = PPC_750,
2861     .specific = NULL,
2862 };
2863
2864 static ppc_def_t ppc_defs[] =
2865 {
2866     /* MPC740/745/750/755 (G3) */
2867     {
2868         .pvr      = 0x00080000,
2869         .pvr_mask = 0xFFFF0000,
2870         .proc     = &ppc_proc_G3,
2871     },
2872     /* IBM 750FX (G3 embedded) */
2873     {
2874         .pvr      = 0x70000000,
2875         .pvr_mask = 0xFFFF0000,
2876         .proc     = &ppc_proc_G3,
2877     },
2878     /* Fallback (generic PPC) */
2879     {
2880         .pvr      = 0x00000000,
2881         .pvr_mask = 0x00000000,
2882         .proc     = &ppc_proc_common,
2883     },
2884 };
2885
2886 static int create_ppc_proc (opc_handler_t **ppc_opcodes, unsigned long pvr)
2887 {
2888     opcode_t *opc;
2889     int i, flags;
2890
2891     fill_new_table(ppc_opcodes, 0x40);
2892     for (i = 0; ; i++) {
2893         if ((ppc_defs[i].pvr & ppc_defs[i].pvr_mask) ==
2894             (pvr & ppc_defs[i].pvr_mask)) {
2895             flags = ppc_defs[i].proc->flags;
2896             break;
2897         }
2898     }
2899     
2900     for (opc = &opc_start + 1; opc != &opc_end; opc++) {
2901         if ((opc->handler.type & flags) != 0)
2902             if (register_insn(ppc_opcodes, opc) < 0) {
2903                 printf("*** ERROR initializing PPC instruction "
2904                         "0x%02x 0x%02x 0x%02x\n", opc->opc1, opc->opc2,
2905                         opc->opc3);
2906                 return -1;
2907             }
2908     }
2909     fix_opcode_tables(ppc_opcodes);
2910
2911     return 0;
2912 }
2913
2914
2915 /*****************************************************************************/
2916 /* Misc PPC helpers */
2917
2918 void cpu_ppc_dump_state(CPUPPCState *env, FILE *f, int flags)
2919 {
2920     int i;
2921
2922     fprintf(f, "nip=0x%08x LR=0x%08x CTR=0x%08x XER=0x%08x "
2923             "MSR=0x%08x\n", env->nip, env->lr, env->ctr,
2924             _load_xer(env), _load_msr(env));
2925         for (i = 0; i < 32; i++) {
2926             if ((i & 7) == 0)
2927             fprintf(f, "GPR%02d:", i);
2928         fprintf(f, " %08x", env->gpr[i]);
2929             if ((i & 7) == 7)
2930             fprintf(f, "\n");
2931         }
2932     fprintf(f, "CR: 0x");
2933         for (i = 0; i < 8; i++)
2934         fprintf(f, "%01x", env->crf[i]);
2935     fprintf(f, "  [");
2936         for (i = 0; i < 8; i++) {
2937             char a = '-';
2938             if (env->crf[i] & 0x08)
2939                 a = 'L';
2940             else if (env->crf[i] & 0x04)
2941                 a = 'G';
2942             else if (env->crf[i] & 0x02)
2943                 a = 'E';
2944         fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
2945         }
2946     fprintf(f, " ] ");
2947     fprintf(f, "TB: 0x%08x %08x\n", cpu_ppc_load_tbu(env),
2948             cpu_ppc_load_tbl(env));
2949         for (i = 0; i < 16; i++) {
2950             if ((i & 3) == 0)
2951             fprintf(f, "FPR%02d:", i);
2952         fprintf(f, " %016llx", *((uint64_t *)&env->fpr[i]));
2953             if ((i & 3) == 3)
2954             fprintf(f, "\n");
2955     }
2956     fprintf(f, "SRR0 0x%08x SRR1 0x%08x DECR=0x%08x\n",
2957             env->spr[SRR0], env->spr[SRR1], cpu_ppc_load_decr(env));
2958     fprintf(f, "reservation 0x%08x\n", env->reserve);
2959     fflush(f);
2960 }
2961
2962 #if !defined(CONFIG_USER_ONLY) && defined (USE_OPENFIRMWARE)
2963 int setup_machine (CPUPPCState *env, uint32_t mid);
2964 #endif
2965
2966 CPUPPCState *cpu_ppc_init(void)
2967 {
2968     CPUPPCState *env;
2969
2970     cpu_exec_init();
2971
2972     env = qemu_mallocz(sizeof(CPUPPCState));
2973     if (!env)
2974         return NULL;
2975 #if !defined(CONFIG_USER_ONLY) && defined (USE_OPEN_FIRMWARE)
2976     setup_machine(env, 0);
2977 #else
2978 //    env->spr[PVR] = 0; /* Basic PPC */
2979     env->spr[PVR] = 0x00080100; /* G3 CPU */
2980 //    env->spr[PVR] = 0x00083100; /* MPC755 (G3 embedded) */
2981 //    env->spr[PVR] = 0x00070100; /* IBM 750FX */
2982 #endif
2983     tlb_flush(env, 1);
2984 #if defined (DO_SINGLE_STEP)
2985     /* Single step trace mode */
2986     msr_se = 1;
2987 #endif
2988     msr_fp = 1; /* Allow floating point exceptions */
2989     msr_me = 1; /* Allow machine check exceptions  */
2990 #if defined(CONFIG_USER_ONLY)
2991     msr_pr = 1;
2992     cpu_ppc_register(env, 0x00080000);
2993 #else
2994     env->nip = 0xFFFFFFFC;
2995 #endif
2996     env->access_type = ACCESS_INT;
2997
2998     return env;
2999 }
3000
3001 int cpu_ppc_register (CPUPPCState *env, uint32_t pvr)
3002 {
3003     env->spr[PVR] = pvr;
3004     if (create_ppc_proc(ppc_opcodes, env->spr[PVR]) < 0)
3005         return -1;
3006     init_spr_rights(env->spr[PVR]);
3007
3008     return 0;
3009 }
3010
3011 void cpu_ppc_close(CPUPPCState *env)
3012 {
3013     /* Should also remove all opcode tables... */
3014     free(env);
3015 }
3016
3017 /*****************************************************************************/
3018 int print_insn_powerpc (FILE *out, unsigned long insn, unsigned memaddr,
3019                         int dialect);
3020
3021 int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
3022                                     int search_pc)
3023 {
3024     DisasContext ctx, *ctxp = &ctx;
3025     opc_handler_t **table, *handler;
3026     uint32_t pc_start;
3027     uint16_t *gen_opc_end;
3028     int j, lj = -1;
3029
3030     pc_start = tb->pc;
3031     gen_opc_ptr = gen_opc_buf;
3032     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
3033     gen_opparam_ptr = gen_opparam_buf;
3034     ctx.nip = pc_start;
3035     ctx.tb = tb;
3036     ctx.exception = EXCP_NONE;
3037 #if defined(CONFIG_USER_ONLY)
3038     ctx.mem_idx = 0;
3039 #else
3040     ctx.supervisor = 1 - msr_pr;
3041     ctx.mem_idx = (1 - msr_pr);
3042 #endif
3043 #if defined (DO_SINGLE_STEP)
3044     /* Single step trace mode */
3045     msr_se = 1;
3046 #endif
3047     env->access_type = ACCESS_CODE;
3048     /* Set env in case of segfault during code fetch */
3049     while (ctx.exception == EXCP_NONE && gen_opc_ptr < gen_opc_end) {
3050         if (search_pc) {
3051             if (loglevel > 0)
3052                 fprintf(logfile, "Search PC...\n");
3053             j = gen_opc_ptr - gen_opc_buf;
3054             if (lj < j) {
3055                 lj++;
3056                 while (lj < j)
3057                     gen_opc_instr_start[lj++] = 0;
3058                 gen_opc_pc[lj] = ctx.nip;
3059                 gen_opc_instr_start[lj] = 1;
3060             }
3061         }
3062 #if defined PPC_DEBUG_DISAS
3063         if (loglevel & CPU_LOG_TB_IN_ASM) {
3064             fprintf(logfile, "----------------\n");
3065             fprintf(logfile, "nip=%08x super=%d ir=%d\n",
3066                     ctx.nip, 1 - msr_pr, msr_ir);
3067         }
3068 #endif
3069         ctx.opcode = ldl_code((void *)ctx.nip);
3070 #if defined PPC_DEBUG_DISAS
3071         if (loglevel & CPU_LOG_TB_IN_ASM) {
3072             fprintf(logfile, "translate opcode %08x (%02x %02x %02x)\n",
3073                     ctx.opcode, opc1(ctx.opcode), opc2(ctx.opcode),
3074                     opc3(ctx.opcode));
3075         }
3076 #endif
3077         ctx.nip += 4;
3078         table = ppc_opcodes;
3079         handler = table[opc1(ctx.opcode)];
3080         if (is_indirect_opcode(handler)) {
3081             table = ind_table(handler);
3082             handler = table[opc2(ctx.opcode)];
3083             if (is_indirect_opcode(handler)) {
3084                 table = ind_table(handler);
3085                 handler = table[opc3(ctx.opcode)];
3086             }
3087         }
3088         /* Is opcode *REALLY* valid ? */
3089                 if (handler->handler == &gen_invalid) {
3090             if (loglevel > 0) {
3091                     fprintf(logfile, "invalid/unsupported opcode: "
3092                         "%02x - %02x - %02x (%08x) 0x%08x %d\n",
3093                             opc1(ctx.opcode), opc2(ctx.opcode),
3094                         opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
3095             } else {
3096                 printf("invalid/unsupported opcode: "
3097                        "%02x - %02x - %02x (%08x) 0x%08x %d\n",
3098                        opc1(ctx.opcode), opc2(ctx.opcode),
3099                        opc3(ctx.opcode), ctx.opcode, ctx.nip - 4, msr_ir);
3100             }
3101                 } else {
3102             if ((ctx.opcode & handler->inval) != 0) {
3103                 if (loglevel > 0) {
3104                     fprintf(logfile, "invalid bits: %08x for opcode: "
3105                             "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
3106                             ctx.opcode & handler->inval, opc1(ctx.opcode),
3107                             opc2(ctx.opcode), opc3(ctx.opcode),
3108                             ctx.opcode, ctx.nip - 4);
3109                 } else {
3110                     printf("invalid bits: %08x for opcode: "
3111                            "%02x -%02x - %02x (0x%08x) (0x%08x)\n",
3112                             ctx.opcode & handler->inval, opc1(ctx.opcode),
3113                             opc2(ctx.opcode), opc3(ctx.opcode),
3114                            ctx.opcode, ctx.nip - 4);
3115             }
3116                 RET_INVAL(ctxp);
3117                 break;
3118             }
3119         }
3120         (*(handler->handler))(&ctx);
3121         /* Check trace mode exceptions */
3122         if ((msr_be && ctx.exception == EXCP_BRANCH) ||
3123             /* Check in single step trace mode
3124              * we need to stop except if:
3125              * - rfi, trap or syscall
3126              * - first instruction of an exception handler
3127              */
3128             (msr_se && (ctx.nip < 0x100 ||
3129                         ctx.nip > 0xF00 ||
3130                         (ctx.nip & 0xFC) != 0x04) &&
3131              ctx.exception != EXCP_SYSCALL && ctx.exception != EXCP_RFI &&
3132              ctx.exception != EXCP_TRAP)) {
3133             RET_EXCP(ctxp, EXCP_TRACE, 0);
3134         }
3135         /* if we reach a page boundary, stop generation */
3136         if ((ctx.nip & (TARGET_PAGE_SIZE - 1)) == 0) {
3137             RET_EXCP(ctxp, EXCP_BRANCH, 0);
3138     }
3139     }
3140     if (ctx.exception == EXCP_NONE) {
3141         gen_op_b((unsigned long)ctx.tb, ctx.nip);
3142     } else if (ctx.exception != EXCP_BRANCH) {
3143         gen_op_set_T0(0);
3144     }
3145 #if 1
3146     /* TO BE FIXED: T0 hasn't got a proper value, which makes tb_add_jump
3147      *              do bad business and then qemu crashes !
3148      */
3149     gen_op_set_T0(0);
3150 #endif
3151     /* Generate the return instruction */
3152     gen_op_exit_tb();
3153     *gen_opc_ptr = INDEX_op_end;
3154     if (search_pc) {
3155         j = gen_opc_ptr - gen_opc_buf;
3156         lj++;
3157         while (lj <= j)
3158             gen_opc_instr_start[lj++] = 0;
3159         tb->size = 0;
3160 #if 0
3161         if (loglevel > 0) {
3162             page_dump(logfile);
3163         }
3164 #endif
3165     } else {
3166         tb->size = ctx.nip - pc_start;
3167     }
3168 #ifdef DEBUG_DISAS
3169     if (loglevel & CPU_LOG_TB_CPU) {
3170         fprintf(logfile, "---------------- excp: %04x\n", ctx.exception);
3171         cpu_ppc_dump_state(env, logfile, 0);
3172     }
3173     if (loglevel & CPU_LOG_TB_IN_ASM) {
3174         fprintf(logfile, "IN: %s\n", lookup_symbol((void *)pc_start));
3175         disas(logfile, (void *)pc_start, ctx.nip - pc_start, 0, 0);
3176         fprintf(logfile, "\n");
3177     }
3178     if (loglevel & CPU_LOG_TB_OP) {
3179         fprintf(logfile, "OP:\n");
3180         dump_ops(gen_opc_buf, gen_opparam_buf);
3181         fprintf(logfile, "\n");
3182     }
3183 #endif
3184     env->access_type = ACCESS_INT;
3185
3186     return 0;
3187 }
3188
3189 int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
3190 {
3191     return gen_intermediate_code_internal(env, tb, 0);
3192 }
3193
3194 int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
3195 {
3196     return gen_intermediate_code_internal(env, tb, 1);
3197 }