xref: /openbmc/qemu/target/ppc/translate.c (revision 09a98dd9)
1 /*
2  *  PowerPC emulation for qemu: main translation routines.
3  *
4  *  Copyright (c) 2003-2007 Jocelyn Mayer
5  *  Copyright (C) 2011 Freescale Semiconductor, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "internal.h"
24 #include "disas/disas.h"
25 #include "exec/exec-all.h"
26 #include "tcg/tcg-op.h"
27 #include "tcg/tcg-op-gvec.h"
28 #include "qemu/host-utils.h"
29 #include "qemu/main-loop.h"
30 #include "exec/cpu_ldst.h"
31 
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
34 
35 #include "trace-tcg.h"
36 #include "exec/translator.h"
37 #include "exec/log.h"
38 #include "qemu/atomic128.h"
39 
40 
41 #define CPU_SINGLE_STEP 0x1
42 #define CPU_BRANCH_STEP 0x2
43 #define GDBSTUB_SINGLE_STEP 0x4
44 
45 /* Include definitions for instructions classes and implementations flags */
46 /* #define PPC_DEBUG_DISAS */
47 /* #define DO_PPC_STATISTICS */
48 
49 #ifdef PPC_DEBUG_DISAS
50 #  define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
51 #else
52 #  define LOG_DISAS(...) do { } while (0)
53 #endif
54 /*****************************************************************************/
55 /* Code translation helpers                                                  */
56 
57 /* global register indexes */
58 static char cpu_reg_names[10 * 3 + 22 * 4   /* GPR */
59                           + 10 * 4 + 22 * 5 /* SPE GPRh */
60                           + 8 * 5           /* CRF */];
61 static TCGv cpu_gpr[32];
62 static TCGv cpu_gprh[32];
63 static TCGv_i32 cpu_crf[8];
64 static TCGv cpu_nip;
65 static TCGv cpu_msr;
66 static TCGv cpu_ctr;
67 static TCGv cpu_lr;
68 #if defined(TARGET_PPC64)
69 static TCGv cpu_cfar;
70 #endif
71 static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca, cpu_ov32, cpu_ca32;
72 static TCGv cpu_reserve;
73 static TCGv cpu_reserve_val;
74 static TCGv cpu_fpscr;
75 static TCGv_i32 cpu_access_type;
76 
77 #include "exec/gen-icount.h"
78 
79 void ppc_translate_init(void)
80 {
81     int i;
82     char *p;
83     size_t cpu_reg_names_size;
84 
85     p = cpu_reg_names;
86     cpu_reg_names_size = sizeof(cpu_reg_names);
87 
88     for (i = 0; i < 8; i++) {
89         snprintf(p, cpu_reg_names_size, "crf%d", i);
90         cpu_crf[i] = tcg_global_mem_new_i32(cpu_env,
91                                             offsetof(CPUPPCState, crf[i]), p);
92         p += 5;
93         cpu_reg_names_size -= 5;
94     }
95 
96     for (i = 0; i < 32; i++) {
97         snprintf(p, cpu_reg_names_size, "r%d", i);
98         cpu_gpr[i] = tcg_global_mem_new(cpu_env,
99                                         offsetof(CPUPPCState, gpr[i]), p);
100         p += (i < 10) ? 3 : 4;
101         cpu_reg_names_size -= (i < 10) ? 3 : 4;
102         snprintf(p, cpu_reg_names_size, "r%dH", i);
103         cpu_gprh[i] = tcg_global_mem_new(cpu_env,
104                                          offsetof(CPUPPCState, gprh[i]), p);
105         p += (i < 10) ? 4 : 5;
106         cpu_reg_names_size -= (i < 10) ? 4 : 5;
107     }
108 
109     cpu_nip = tcg_global_mem_new(cpu_env,
110                                  offsetof(CPUPPCState, nip), "nip");
111 
112     cpu_msr = tcg_global_mem_new(cpu_env,
113                                  offsetof(CPUPPCState, msr), "msr");
114 
115     cpu_ctr = tcg_global_mem_new(cpu_env,
116                                  offsetof(CPUPPCState, ctr), "ctr");
117 
118     cpu_lr = tcg_global_mem_new(cpu_env,
119                                 offsetof(CPUPPCState, lr), "lr");
120 
121 #if defined(TARGET_PPC64)
122     cpu_cfar = tcg_global_mem_new(cpu_env,
123                                   offsetof(CPUPPCState, cfar), "cfar");
124 #endif
125 
126     cpu_xer = tcg_global_mem_new(cpu_env,
127                                  offsetof(CPUPPCState, xer), "xer");
128     cpu_so = tcg_global_mem_new(cpu_env,
129                                 offsetof(CPUPPCState, so), "SO");
130     cpu_ov = tcg_global_mem_new(cpu_env,
131                                 offsetof(CPUPPCState, ov), "OV");
132     cpu_ca = tcg_global_mem_new(cpu_env,
133                                 offsetof(CPUPPCState, ca), "CA");
134     cpu_ov32 = tcg_global_mem_new(cpu_env,
135                                   offsetof(CPUPPCState, ov32), "OV32");
136     cpu_ca32 = tcg_global_mem_new(cpu_env,
137                                   offsetof(CPUPPCState, ca32), "CA32");
138 
139     cpu_reserve = tcg_global_mem_new(cpu_env,
140                                      offsetof(CPUPPCState, reserve_addr),
141                                      "reserve_addr");
142     cpu_reserve_val = tcg_global_mem_new(cpu_env,
143                                      offsetof(CPUPPCState, reserve_val),
144                                      "reserve_val");
145 
146     cpu_fpscr = tcg_global_mem_new(cpu_env,
147                                    offsetof(CPUPPCState, fpscr), "fpscr");
148 
149     cpu_access_type = tcg_global_mem_new_i32(cpu_env,
150                                              offsetof(CPUPPCState, access_type),
151                                              "access_type");
152 }
153 
154 /* internal defines */
155 struct DisasContext {
156     DisasContextBase base;
157     uint32_t opcode;
158     uint32_t exception;
159     /* Routine used to access memory */
160     bool pr, hv, dr, le_mode;
161     bool lazy_tlb_flush;
162     bool need_access_type;
163     int mem_idx;
164     int access_type;
165     /* Translation flags */
166     MemOp default_tcg_memop_mask;
167 #if defined(TARGET_PPC64)
168     bool sf_mode;
169     bool has_cfar;
170 #endif
171     bool fpu_enabled;
172     bool altivec_enabled;
173     bool vsx_enabled;
174     bool spe_enabled;
175     bool tm_enabled;
176     bool gtse;
177     ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
178     int singlestep_enabled;
179     uint32_t flags;
180     uint64_t insns_flags;
181     uint64_t insns_flags2;
182 };
183 
184 /* Return true iff byteswap is needed in a scalar memop */
185 static inline bool need_byteswap(const DisasContext *ctx)
186 {
187 #if defined(TARGET_WORDS_BIGENDIAN)
188      return ctx->le_mode;
189 #else
190      return !ctx->le_mode;
191 #endif
192 }
193 
194 /* True when active word size < size of target_long.  */
195 #ifdef TARGET_PPC64
196 # define NARROW_MODE(C)  (!(C)->sf_mode)
197 #else
198 # define NARROW_MODE(C)  0
199 #endif
200 
201 struct opc_handler_t {
202     /* invalid bits for instruction 1 (Rc(opcode) == 0) */
203     uint32_t inval1;
204     /* invalid bits for instruction 2 (Rc(opcode) == 1) */
205     uint32_t inval2;
206     /* instruction type */
207     uint64_t type;
208     /* extended instruction type */
209     uint64_t type2;
210     /* handler */
211     void (*handler)(DisasContext *ctx);
212 #if defined(DO_PPC_STATISTICS) || defined(PPC_DUMP_CPU)
213     const char *oname;
214 #endif
215 #if defined(DO_PPC_STATISTICS)
216     uint64_t count;
217 #endif
218 };
219 
220 /* SPR load/store helpers */
221 static inline void gen_load_spr(TCGv t, int reg)
222 {
223     tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
224 }
225 
226 static inline void gen_store_spr(int reg, TCGv t)
227 {
228     tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
229 }
230 
231 static inline void gen_set_access_type(DisasContext *ctx, int access_type)
232 {
233     if (ctx->need_access_type && ctx->access_type != access_type) {
234         tcg_gen_movi_i32(cpu_access_type, access_type);
235         ctx->access_type = access_type;
236     }
237 }
238 
239 static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
240 {
241     if (NARROW_MODE(ctx)) {
242         nip = (uint32_t)nip;
243     }
244     tcg_gen_movi_tl(cpu_nip, nip);
245 }
246 
247 static void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
248 {
249     TCGv_i32 t0, t1;
250 
251     /*
252      * These are all synchronous exceptions, we set the PC back to the
253      * faulting instruction
254      */
255     if (ctx->exception == POWERPC_EXCP_NONE) {
256         gen_update_nip(ctx, ctx->base.pc_next - 4);
257     }
258     t0 = tcg_const_i32(excp);
259     t1 = tcg_const_i32(error);
260     gen_helper_raise_exception_err(cpu_env, t0, t1);
261     tcg_temp_free_i32(t0);
262     tcg_temp_free_i32(t1);
263     ctx->exception = (excp);
264 }
265 
266 static void gen_exception(DisasContext *ctx, uint32_t excp)
267 {
268     TCGv_i32 t0;
269 
270     /*
271      * These are all synchronous exceptions, we set the PC back to the
272      * faulting instruction
273      */
274     if (ctx->exception == POWERPC_EXCP_NONE) {
275         gen_update_nip(ctx, ctx->base.pc_next - 4);
276     }
277     t0 = tcg_const_i32(excp);
278     gen_helper_raise_exception(cpu_env, t0);
279     tcg_temp_free_i32(t0);
280     ctx->exception = (excp);
281 }
282 
283 static void gen_exception_nip(DisasContext *ctx, uint32_t excp,
284                               target_ulong nip)
285 {
286     TCGv_i32 t0;
287 
288     gen_update_nip(ctx, nip);
289     t0 = tcg_const_i32(excp);
290     gen_helper_raise_exception(cpu_env, t0);
291     tcg_temp_free_i32(t0);
292     ctx->exception = (excp);
293 }
294 
295 /*
296  * Tells the caller what is the appropriate exception to generate and prepares
297  * SPR registers for this exception.
298  *
299  * The exception can be either POWERPC_EXCP_TRACE (on most PowerPCs) or
300  * POWERPC_EXCP_DEBUG (on BookE).
301  */
302 static uint32_t gen_prep_dbgex(DisasContext *ctx)
303 {
304     if (ctx->flags & POWERPC_FLAG_DE) {
305         target_ulong dbsr = 0;
306         if (ctx->singlestep_enabled & CPU_SINGLE_STEP) {
307             dbsr = DBCR0_ICMP;
308         } else {
309             /* Must have been branch */
310             dbsr = DBCR0_BRT;
311         }
312         TCGv t0 = tcg_temp_new();
313         gen_load_spr(t0, SPR_BOOKE_DBSR);
314         tcg_gen_ori_tl(t0, t0, dbsr);
315         gen_store_spr(SPR_BOOKE_DBSR, t0);
316         tcg_temp_free(t0);
317         return POWERPC_EXCP_DEBUG;
318     } else {
319         return POWERPC_EXCP_TRACE;
320     }
321 }
322 
323 static void gen_debug_exception(DisasContext *ctx)
324 {
325     TCGv_i32 t0;
326 
327     /*
328      * These are all synchronous exceptions, we set the PC back to the
329      * faulting instruction
330      */
331     if ((ctx->exception != POWERPC_EXCP_BRANCH) &&
332         (ctx->exception != POWERPC_EXCP_SYNC)) {
333         gen_update_nip(ctx, ctx->base.pc_next);
334     }
335     t0 = tcg_const_i32(EXCP_DEBUG);
336     gen_helper_raise_exception(cpu_env, t0);
337     tcg_temp_free_i32(t0);
338 }
339 
340 static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
341 {
342     /* Will be converted to program check if needed */
343     gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_INVAL | error);
344 }
345 
346 static inline void gen_priv_exception(DisasContext *ctx, uint32_t error)
347 {
348     gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_PRIV | error);
349 }
350 
351 static inline void gen_hvpriv_exception(DisasContext *ctx, uint32_t error)
352 {
353     /* Will be converted to program check if needed */
354     gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_PRIV | error);
355 }
356 
357 /* Stop translation */
358 static inline void gen_stop_exception(DisasContext *ctx)
359 {
360     gen_update_nip(ctx, ctx->base.pc_next);
361     ctx->exception = POWERPC_EXCP_STOP;
362 }
363 
364 #ifndef CONFIG_USER_ONLY
365 /* No need to update nip here, as execution flow will change */
366 static inline void gen_sync_exception(DisasContext *ctx)
367 {
368     ctx->exception = POWERPC_EXCP_SYNC;
369 }
370 #endif
371 
372 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type)                      \
373 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
374 
375 #define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2)             \
376 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
377 
378 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type)               \
379 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
380 
381 #define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2)      \
382 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
383 
384 #define GEN_HANDLER_E_2(name, opc1, opc2, opc3, opc4, inval, type, type2)     \
385 GEN_OPCODE3(name, opc1, opc2, opc3, opc4, inval, type, type2)
386 
387 #define GEN_HANDLER2_E_2(name, onam, opc1, opc2, opc3, opc4, inval, typ, typ2) \
388 GEN_OPCODE4(name, onam, opc1, opc2, opc3, opc4, inval, typ, typ2)
389 
390 typedef struct opcode_t {
391     unsigned char opc1, opc2, opc3, opc4;
392 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
393     unsigned char pad[4];
394 #endif
395     opc_handler_t handler;
396     const char *oname;
397 } opcode_t;
398 
399 /* Helpers for priv. check */
400 #define GEN_PRIV                                                \
401     do {                                                        \
402         gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC); return; \
403     } while (0)
404 
405 #if defined(CONFIG_USER_ONLY)
406 #define CHK_HV GEN_PRIV
407 #define CHK_SV GEN_PRIV
408 #define CHK_HVRM GEN_PRIV
409 #else
410 #define CHK_HV                                                          \
411     do {                                                                \
412         if (unlikely(ctx->pr || !ctx->hv)) {                            \
413             GEN_PRIV;                                                   \
414         }                                                               \
415     } while (0)
416 #define CHK_SV                   \
417     do {                         \
418         if (unlikely(ctx->pr)) { \
419             GEN_PRIV;            \
420         }                        \
421     } while (0)
422 #define CHK_HVRM                                            \
423     do {                                                    \
424         if (unlikely(ctx->pr || !ctx->hv || ctx->dr)) {     \
425             GEN_PRIV;                                       \
426         }                                                   \
427     } while (0)
428 #endif
429 
430 #define CHK_NONE
431 
432 /*****************************************************************************/
433 /* PowerPC instructions table                                                */
434 
435 #if defined(DO_PPC_STATISTICS)
436 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
437 {                                                                             \
438     .opc1 = op1,                                                              \
439     .opc2 = op2,                                                              \
440     .opc3 = op3,                                                              \
441     .opc4 = 0xff,                                                             \
442     .handler = {                                                              \
443         .inval1  = invl,                                                      \
444         .type = _typ,                                                         \
445         .type2 = _typ2,                                                       \
446         .handler = &gen_##name,                                               \
447         .oname = stringify(name),                                             \
448     },                                                                        \
449     .oname = stringify(name),                                                 \
450 }
451 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
452 {                                                                             \
453     .opc1 = op1,                                                              \
454     .opc2 = op2,                                                              \
455     .opc3 = op3,                                                              \
456     .opc4 = 0xff,                                                             \
457     .handler = {                                                              \
458         .inval1  = invl1,                                                     \
459         .inval2  = invl2,                                                     \
460         .type = _typ,                                                         \
461         .type2 = _typ2,                                                       \
462         .handler = &gen_##name,                                               \
463         .oname = stringify(name),                                             \
464     },                                                                        \
465     .oname = stringify(name),                                                 \
466 }
467 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
468 {                                                                             \
469     .opc1 = op1,                                                              \
470     .opc2 = op2,                                                              \
471     .opc3 = op3,                                                              \
472     .opc4 = 0xff,                                                             \
473     .handler = {                                                              \
474         .inval1  = invl,                                                      \
475         .type = _typ,                                                         \
476         .type2 = _typ2,                                                       \
477         .handler = &gen_##name,                                               \
478         .oname = onam,                                                        \
479     },                                                                        \
480     .oname = onam,                                                            \
481 }
482 #define GEN_OPCODE3(name, op1, op2, op3, op4, invl, _typ, _typ2)              \
483 {                                                                             \
484     .opc1 = op1,                                                              \
485     .opc2 = op2,                                                              \
486     .opc3 = op3,                                                              \
487     .opc4 = op4,                                                              \
488     .handler = {                                                              \
489         .inval1  = invl,                                                      \
490         .type = _typ,                                                         \
491         .type2 = _typ2,                                                       \
492         .handler = &gen_##name,                                               \
493         .oname = stringify(name),                                             \
494     },                                                                        \
495     .oname = stringify(name),                                                 \
496 }
497 #define GEN_OPCODE4(name, onam, op1, op2, op3, op4, invl, _typ, _typ2)        \
498 {                                                                             \
499     .opc1 = op1,                                                              \
500     .opc2 = op2,                                                              \
501     .opc3 = op3,                                                              \
502     .opc4 = op4,                                                              \
503     .handler = {                                                              \
504         .inval1  = invl,                                                      \
505         .type = _typ,                                                         \
506         .type2 = _typ2,                                                       \
507         .handler = &gen_##name,                                               \
508         .oname = onam,                                                        \
509     },                                                                        \
510     .oname = onam,                                                            \
511 }
512 #else
513 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2)                    \
514 {                                                                             \
515     .opc1 = op1,                                                              \
516     .opc2 = op2,                                                              \
517     .opc3 = op3,                                                              \
518     .opc4 = 0xff,                                                             \
519     .handler = {                                                              \
520         .inval1  = invl,                                                      \
521         .type = _typ,                                                         \
522         .type2 = _typ2,                                                       \
523         .handler = &gen_##name,                                               \
524     },                                                                        \
525     .oname = stringify(name),                                                 \
526 }
527 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2)       \
528 {                                                                             \
529     .opc1 = op1,                                                              \
530     .opc2 = op2,                                                              \
531     .opc3 = op3,                                                              \
532     .opc4 = 0xff,                                                             \
533     .handler = {                                                              \
534         .inval1  = invl1,                                                     \
535         .inval2  = invl2,                                                     \
536         .type = _typ,                                                         \
537         .type2 = _typ2,                                                       \
538         .handler = &gen_##name,                                               \
539     },                                                                        \
540     .oname = stringify(name),                                                 \
541 }
542 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2)             \
543 {                                                                             \
544     .opc1 = op1,                                                              \
545     .opc2 = op2,                                                              \
546     .opc3 = op3,                                                              \
547     .opc4 = 0xff,                                                             \
548     .handler = {                                                              \
549         .inval1  = invl,                                                      \
550         .type = _typ,                                                         \
551         .type2 = _typ2,                                                       \
552         .handler = &gen_##name,                                               \
553     },                                                                        \
554     .oname = onam,                                                            \
555 }
556 #define GEN_OPCODE3(name, op1, op2, op3, op4, invl, _typ, _typ2)              \
557 {                                                                             \
558     .opc1 = op1,                                                              \
559     .opc2 = op2,                                                              \
560     .opc3 = op3,                                                              \
561     .opc4 = op4,                                                              \
562     .handler = {                                                              \
563         .inval1  = invl,                                                      \
564         .type = _typ,                                                         \
565         .type2 = _typ2,                                                       \
566         .handler = &gen_##name,                                               \
567     },                                                                        \
568     .oname = stringify(name),                                                 \
569 }
570 #define GEN_OPCODE4(name, onam, op1, op2, op3, op4, invl, _typ, _typ2)        \
571 {                                                                             \
572     .opc1 = op1,                                                              \
573     .opc2 = op2,                                                              \
574     .opc3 = op3,                                                              \
575     .opc4 = op4,                                                              \
576     .handler = {                                                              \
577         .inval1  = invl,                                                      \
578         .type = _typ,                                                         \
579         .type2 = _typ2,                                                       \
580         .handler = &gen_##name,                                               \
581     },                                                                        \
582     .oname = onam,                                                            \
583 }
584 #endif
585 
586 /* Invalid instruction */
587 static void gen_invalid(DisasContext *ctx)
588 {
589     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
590 }
591 
592 static opc_handler_t invalid_handler = {
593     .inval1  = 0xFFFFFFFF,
594     .inval2  = 0xFFFFFFFF,
595     .type    = PPC_NONE,
596     .type2   = PPC_NONE,
597     .handler = gen_invalid,
598 };
599 
600 /***                           Integer comparison                          ***/
601 
602 static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
603 {
604     TCGv t0 = tcg_temp_new();
605     TCGv t1 = tcg_temp_new();
606     TCGv_i32 t = tcg_temp_new_i32();
607 
608     tcg_gen_movi_tl(t0, CRF_EQ);
609     tcg_gen_movi_tl(t1, CRF_LT);
610     tcg_gen_movcond_tl((s ? TCG_COND_LT : TCG_COND_LTU),
611                        t0, arg0, arg1, t1, t0);
612     tcg_gen_movi_tl(t1, CRF_GT);
613     tcg_gen_movcond_tl((s ? TCG_COND_GT : TCG_COND_GTU),
614                        t0, arg0, arg1, t1, t0);
615 
616     tcg_gen_trunc_tl_i32(t, t0);
617     tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
618     tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t);
619 
620     tcg_temp_free(t0);
621     tcg_temp_free(t1);
622     tcg_temp_free_i32(t);
623 }
624 
625 static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
626 {
627     TCGv t0 = tcg_const_tl(arg1);
628     gen_op_cmp(arg0, t0, s, crf);
629     tcg_temp_free(t0);
630 }
631 
632 static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
633 {
634     TCGv t0, t1;
635     t0 = tcg_temp_new();
636     t1 = tcg_temp_new();
637     if (s) {
638         tcg_gen_ext32s_tl(t0, arg0);
639         tcg_gen_ext32s_tl(t1, arg1);
640     } else {
641         tcg_gen_ext32u_tl(t0, arg0);
642         tcg_gen_ext32u_tl(t1, arg1);
643     }
644     gen_op_cmp(t0, t1, s, crf);
645     tcg_temp_free(t1);
646     tcg_temp_free(t0);
647 }
648 
649 static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
650 {
651     TCGv t0 = tcg_const_tl(arg1);
652     gen_op_cmp32(arg0, t0, s, crf);
653     tcg_temp_free(t0);
654 }
655 
656 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
657 {
658     if (NARROW_MODE(ctx)) {
659         gen_op_cmpi32(reg, 0, 1, 0);
660     } else {
661         gen_op_cmpi(reg, 0, 1, 0);
662     }
663 }
664 
665 /* cmp */
666 static void gen_cmp(DisasContext *ctx)
667 {
668     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
669         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
670                    1, crfD(ctx->opcode));
671     } else {
672         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
673                      1, crfD(ctx->opcode));
674     }
675 }
676 
677 /* cmpi */
678 static void gen_cmpi(DisasContext *ctx)
679 {
680     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
681         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
682                     1, crfD(ctx->opcode));
683     } else {
684         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
685                       1, crfD(ctx->opcode));
686     }
687 }
688 
689 /* cmpl */
690 static void gen_cmpl(DisasContext *ctx)
691 {
692     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
693         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
694                    0, crfD(ctx->opcode));
695     } else {
696         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
697                      0, crfD(ctx->opcode));
698     }
699 }
700 
701 /* cmpli */
702 static void gen_cmpli(DisasContext *ctx)
703 {
704     if ((ctx->opcode & 0x00200000) && (ctx->insns_flags & PPC_64B)) {
705         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
706                     0, crfD(ctx->opcode));
707     } else {
708         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
709                       0, crfD(ctx->opcode));
710     }
711 }
712 
713 /* cmprb - range comparison: isupper, isaplha, islower*/
714 static void gen_cmprb(DisasContext *ctx)
715 {
716     TCGv_i32 src1 = tcg_temp_new_i32();
717     TCGv_i32 src2 = tcg_temp_new_i32();
718     TCGv_i32 src2lo = tcg_temp_new_i32();
719     TCGv_i32 src2hi = tcg_temp_new_i32();
720     TCGv_i32 crf = cpu_crf[crfD(ctx->opcode)];
721 
722     tcg_gen_trunc_tl_i32(src1, cpu_gpr[rA(ctx->opcode)]);
723     tcg_gen_trunc_tl_i32(src2, cpu_gpr[rB(ctx->opcode)]);
724 
725     tcg_gen_andi_i32(src1, src1, 0xFF);
726     tcg_gen_ext8u_i32(src2lo, src2);
727     tcg_gen_shri_i32(src2, src2, 8);
728     tcg_gen_ext8u_i32(src2hi, src2);
729 
730     tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
731     tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
732     tcg_gen_and_i32(crf, src2lo, src2hi);
733 
734     if (ctx->opcode & 0x00200000) {
735         tcg_gen_shri_i32(src2, src2, 8);
736         tcg_gen_ext8u_i32(src2lo, src2);
737         tcg_gen_shri_i32(src2, src2, 8);
738         tcg_gen_ext8u_i32(src2hi, src2);
739         tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
740         tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
741         tcg_gen_and_i32(src2lo, src2lo, src2hi);
742         tcg_gen_or_i32(crf, crf, src2lo);
743     }
744     tcg_gen_shli_i32(crf, crf, CRF_GT_BIT);
745     tcg_temp_free_i32(src1);
746     tcg_temp_free_i32(src2);
747     tcg_temp_free_i32(src2lo);
748     tcg_temp_free_i32(src2hi);
749 }
750 
751 #if defined(TARGET_PPC64)
752 /* cmpeqb */
753 static void gen_cmpeqb(DisasContext *ctx)
754 {
755     gen_helper_cmpeqb(cpu_crf[crfD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
756                       cpu_gpr[rB(ctx->opcode)]);
757 }
758 #endif
759 
760 /* isel (PowerPC 2.03 specification) */
761 static void gen_isel(DisasContext *ctx)
762 {
763     uint32_t bi = rC(ctx->opcode);
764     uint32_t mask = 0x08 >> (bi & 0x03);
765     TCGv t0 = tcg_temp_new();
766     TCGv zr;
767 
768     tcg_gen_extu_i32_tl(t0, cpu_crf[bi >> 2]);
769     tcg_gen_andi_tl(t0, t0, mask);
770 
771     zr = tcg_const_tl(0);
772     tcg_gen_movcond_tl(TCG_COND_NE, cpu_gpr[rD(ctx->opcode)], t0, zr,
773                        rA(ctx->opcode) ? cpu_gpr[rA(ctx->opcode)] : zr,
774                        cpu_gpr[rB(ctx->opcode)]);
775     tcg_temp_free(zr);
776     tcg_temp_free(t0);
777 }
778 
779 /* cmpb: PowerPC 2.05 specification */
780 static void gen_cmpb(DisasContext *ctx)
781 {
782     gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
783                     cpu_gpr[rB(ctx->opcode)]);
784 }
785 
786 /***                           Integer arithmetic                          ***/
787 
788 static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
789                                            TCGv arg1, TCGv arg2, int sub)
790 {
791     TCGv t0 = tcg_temp_new();
792 
793     tcg_gen_xor_tl(cpu_ov, arg0, arg2);
794     tcg_gen_xor_tl(t0, arg1, arg2);
795     if (sub) {
796         tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
797     } else {
798         tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
799     }
800     tcg_temp_free(t0);
801     if (NARROW_MODE(ctx)) {
802         tcg_gen_extract_tl(cpu_ov, cpu_ov, 31, 1);
803         if (is_isa300(ctx)) {
804             tcg_gen_mov_tl(cpu_ov32, cpu_ov);
805         }
806     } else {
807         if (is_isa300(ctx)) {
808             tcg_gen_extract_tl(cpu_ov32, cpu_ov, 31, 1);
809         }
810         tcg_gen_extract_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1, 1);
811     }
812     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
813 }
814 
815 static inline void gen_op_arith_compute_ca32(DisasContext *ctx,
816                                              TCGv res, TCGv arg0, TCGv arg1,
817                                              TCGv ca32, int sub)
818 {
819     TCGv t0;
820 
821     if (!is_isa300(ctx)) {
822         return;
823     }
824 
825     t0 = tcg_temp_new();
826     if (sub) {
827         tcg_gen_eqv_tl(t0, arg0, arg1);
828     } else {
829         tcg_gen_xor_tl(t0, arg0, arg1);
830     }
831     tcg_gen_xor_tl(t0, t0, res);
832     tcg_gen_extract_tl(ca32, t0, 32, 1);
833     tcg_temp_free(t0);
834 }
835 
836 /* Common add function */
837 static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
838                                     TCGv arg2, TCGv ca, TCGv ca32,
839                                     bool add_ca, bool compute_ca,
840                                     bool compute_ov, bool compute_rc0)
841 {
842     TCGv t0 = ret;
843 
844     if (compute_ca || compute_ov) {
845         t0 = tcg_temp_new();
846     }
847 
848     if (compute_ca) {
849         if (NARROW_MODE(ctx)) {
850             /*
851              * Caution: a non-obvious corner case of the spec is that
852              * we must produce the *entire* 64-bit addition, but
853              * produce the carry into bit 32.
854              */
855             TCGv t1 = tcg_temp_new();
856             tcg_gen_xor_tl(t1, arg1, arg2);        /* add without carry */
857             tcg_gen_add_tl(t0, arg1, arg2);
858             if (add_ca) {
859                 tcg_gen_add_tl(t0, t0, ca);
860             }
861             tcg_gen_xor_tl(ca, t0, t1);        /* bits changed w/ carry */
862             tcg_temp_free(t1);
863             tcg_gen_extract_tl(ca, ca, 32, 1);
864             if (is_isa300(ctx)) {
865                 tcg_gen_mov_tl(ca32, ca);
866             }
867         } else {
868             TCGv zero = tcg_const_tl(0);
869             if (add_ca) {
870                 tcg_gen_add2_tl(t0, ca, arg1, zero, ca, zero);
871                 tcg_gen_add2_tl(t0, ca, t0, ca, arg2, zero);
872             } else {
873                 tcg_gen_add2_tl(t0, ca, arg1, zero, arg2, zero);
874             }
875             gen_op_arith_compute_ca32(ctx, t0, arg1, arg2, ca32, 0);
876             tcg_temp_free(zero);
877         }
878     } else {
879         tcg_gen_add_tl(t0, arg1, arg2);
880         if (add_ca) {
881             tcg_gen_add_tl(t0, t0, ca);
882         }
883     }
884 
885     if (compute_ov) {
886         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
887     }
888     if (unlikely(compute_rc0)) {
889         gen_set_Rc0(ctx, t0);
890     }
891 
892     if (t0 != ret) {
893         tcg_gen_mov_tl(ret, t0);
894         tcg_temp_free(t0);
895     }
896 }
897 /* Add functions with two operands */
898 #define GEN_INT_ARITH_ADD(name, opc3, ca, add_ca, compute_ca, compute_ov)     \
899 static void glue(gen_, name)(DisasContext *ctx)                               \
900 {                                                                             \
901     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
902                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
903                      ca, glue(ca, 32),                                        \
904                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
905 }
906 /* Add functions with one operand and one immediate */
907 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, ca,                    \
908                                 add_ca, compute_ca, compute_ov)               \
909 static void glue(gen_, name)(DisasContext *ctx)                               \
910 {                                                                             \
911     TCGv t0 = tcg_const_tl(const_val);                                        \
912     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)],                           \
913                      cpu_gpr[rA(ctx->opcode)], t0,                            \
914                      ca, glue(ca, 32),                                        \
915                      add_ca, compute_ca, compute_ov, Rc(ctx->opcode));        \
916     tcg_temp_free(t0);                                                        \
917 }
918 
919 /* add  add.  addo  addo. */
920 GEN_INT_ARITH_ADD(add, 0x08, cpu_ca, 0, 0, 0)
921 GEN_INT_ARITH_ADD(addo, 0x18, cpu_ca, 0, 0, 1)
922 /* addc  addc.  addco  addco. */
923 GEN_INT_ARITH_ADD(addc, 0x00, cpu_ca, 0, 1, 0)
924 GEN_INT_ARITH_ADD(addco, 0x10, cpu_ca, 0, 1, 1)
925 /* adde  adde.  addeo  addeo. */
926 GEN_INT_ARITH_ADD(adde, 0x04, cpu_ca, 1, 1, 0)
927 GEN_INT_ARITH_ADD(addeo, 0x14, cpu_ca, 1, 1, 1)
928 /* addme  addme.  addmeo  addmeo.  */
929 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, cpu_ca, 1, 1, 0)
930 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, cpu_ca, 1, 1, 1)
931 /* addex */
932 GEN_INT_ARITH_ADD(addex, 0x05, cpu_ov, 1, 1, 0);
933 /* addze  addze.  addzeo  addzeo.*/
934 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, cpu_ca, 1, 1, 0)
935 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, cpu_ca, 1, 1, 1)
936 /* addi */
937 static void gen_addi(DisasContext *ctx)
938 {
939     target_long simm = SIMM(ctx->opcode);
940 
941     if (rA(ctx->opcode) == 0) {
942         /* li case */
943         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm);
944     } else {
945         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
946                         cpu_gpr[rA(ctx->opcode)], simm);
947     }
948 }
949 /* addic  addic.*/
950 static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
951 {
952     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
953     gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
954                      c, cpu_ca, cpu_ca32, 0, 1, 0, compute_rc0);
955     tcg_temp_free(c);
956 }
957 
958 static void gen_addic(DisasContext *ctx)
959 {
960     gen_op_addic(ctx, 0);
961 }
962 
963 static void gen_addic_(DisasContext *ctx)
964 {
965     gen_op_addic(ctx, 1);
966 }
967 
968 /* addis */
969 static void gen_addis(DisasContext *ctx)
970 {
971     target_long simm = SIMM(ctx->opcode);
972 
973     if (rA(ctx->opcode) == 0) {
974         /* lis case */
975         tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], simm << 16);
976     } else {
977         tcg_gen_addi_tl(cpu_gpr[rD(ctx->opcode)],
978                         cpu_gpr[rA(ctx->opcode)], simm << 16);
979     }
980 }
981 
982 /* addpcis */
983 static void gen_addpcis(DisasContext *ctx)
984 {
985     target_long d = DX(ctx->opcode);
986 
987     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], ctx->base.pc_next + (d << 16));
988 }
989 
990 static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
991                                      TCGv arg2, int sign, int compute_ov)
992 {
993     TCGv_i32 t0 = tcg_temp_new_i32();
994     TCGv_i32 t1 = tcg_temp_new_i32();
995     TCGv_i32 t2 = tcg_temp_new_i32();
996     TCGv_i32 t3 = tcg_temp_new_i32();
997 
998     tcg_gen_trunc_tl_i32(t0, arg1);
999     tcg_gen_trunc_tl_i32(t1, arg2);
1000     if (sign) {
1001         tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1002         tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1003         tcg_gen_and_i32(t2, t2, t3);
1004         tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1005         tcg_gen_or_i32(t2, t2, t3);
1006         tcg_gen_movi_i32(t3, 0);
1007         tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1008         tcg_gen_div_i32(t3, t0, t1);
1009         tcg_gen_extu_i32_tl(ret, t3);
1010     } else {
1011         tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t1, 0);
1012         tcg_gen_movi_i32(t3, 0);
1013         tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1014         tcg_gen_divu_i32(t3, t0, t1);
1015         tcg_gen_extu_i32_tl(ret, t3);
1016     }
1017     if (compute_ov) {
1018         tcg_gen_extu_i32_tl(cpu_ov, t2);
1019         if (is_isa300(ctx)) {
1020             tcg_gen_extu_i32_tl(cpu_ov32, t2);
1021         }
1022         tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1023     }
1024     tcg_temp_free_i32(t0);
1025     tcg_temp_free_i32(t1);
1026     tcg_temp_free_i32(t2);
1027     tcg_temp_free_i32(t3);
1028 
1029     if (unlikely(Rc(ctx->opcode) != 0)) {
1030         gen_set_Rc0(ctx, ret);
1031     }
1032 }
1033 /* Div functions */
1034 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
1035 static void glue(gen_, name)(DisasContext *ctx)                               \
1036 {                                                                             \
1037     gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1038                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],      \
1039                      sign, compute_ov);                                       \
1040 }
1041 /* divwu  divwu.  divwuo  divwuo.   */
1042 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1043 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1044 /* divw  divw.  divwo  divwo.   */
1045 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1046 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1047 
1048 /* div[wd]eu[o][.] */
1049 #define GEN_DIVE(name, hlpr, compute_ov)                                      \
1050 static void gen_##name(DisasContext *ctx)                                     \
1051 {                                                                             \
1052     TCGv_i32 t0 = tcg_const_i32(compute_ov);                                  \
1053     gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env,                      \
1054                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1055     tcg_temp_free_i32(t0);                                                    \
1056     if (unlikely(Rc(ctx->opcode) != 0)) {                                     \
1057         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);                           \
1058     }                                                                         \
1059 }
1060 
1061 GEN_DIVE(divweu, divweu, 0);
1062 GEN_DIVE(divweuo, divweu, 1);
1063 GEN_DIVE(divwe, divwe, 0);
1064 GEN_DIVE(divweo, divwe, 1);
1065 
1066 #if defined(TARGET_PPC64)
1067 static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1068                                      TCGv arg2, int sign, int compute_ov)
1069 {
1070     TCGv_i64 t0 = tcg_temp_new_i64();
1071     TCGv_i64 t1 = tcg_temp_new_i64();
1072     TCGv_i64 t2 = tcg_temp_new_i64();
1073     TCGv_i64 t3 = tcg_temp_new_i64();
1074 
1075     tcg_gen_mov_i64(t0, arg1);
1076     tcg_gen_mov_i64(t1, arg2);
1077     if (sign) {
1078         tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t0, INT64_MIN);
1079         tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, -1);
1080         tcg_gen_and_i64(t2, t2, t3);
1081         tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, 0);
1082         tcg_gen_or_i64(t2, t2, t3);
1083         tcg_gen_movi_i64(t3, 0);
1084         tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1085         tcg_gen_div_i64(ret, t0, t1);
1086     } else {
1087         tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t1, 0);
1088         tcg_gen_movi_i64(t3, 0);
1089         tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1090         tcg_gen_divu_i64(ret, t0, t1);
1091     }
1092     if (compute_ov) {
1093         tcg_gen_mov_tl(cpu_ov, t2);
1094         if (is_isa300(ctx)) {
1095             tcg_gen_mov_tl(cpu_ov32, t2);
1096         }
1097         tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1098     }
1099     tcg_temp_free_i64(t0);
1100     tcg_temp_free_i64(t1);
1101     tcg_temp_free_i64(t2);
1102     tcg_temp_free_i64(t3);
1103 
1104     if (unlikely(Rc(ctx->opcode) != 0)) {
1105         gen_set_Rc0(ctx, ret);
1106     }
1107 }
1108 
1109 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
1110 static void glue(gen_, name)(DisasContext *ctx)                               \
1111 {                                                                             \
1112     gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1113                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1114                       sign, compute_ov);                                      \
1115 }
1116 /* divdu  divdu.  divduo  divduo.   */
1117 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1118 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1119 /* divd  divd.  divdo  divdo.   */
1120 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1121 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1122 
1123 GEN_DIVE(divdeu, divdeu, 0);
1124 GEN_DIVE(divdeuo, divdeu, 1);
1125 GEN_DIVE(divde, divde, 0);
1126 GEN_DIVE(divdeo, divde, 1);
1127 #endif
1128 
1129 static inline void gen_op_arith_modw(DisasContext *ctx, TCGv ret, TCGv arg1,
1130                                      TCGv arg2, int sign)
1131 {
1132     TCGv_i32 t0 = tcg_temp_new_i32();
1133     TCGv_i32 t1 = tcg_temp_new_i32();
1134 
1135     tcg_gen_trunc_tl_i32(t0, arg1);
1136     tcg_gen_trunc_tl_i32(t1, arg2);
1137     if (sign) {
1138         TCGv_i32 t2 = tcg_temp_new_i32();
1139         TCGv_i32 t3 = tcg_temp_new_i32();
1140         tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1141         tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1142         tcg_gen_and_i32(t2, t2, t3);
1143         tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1144         tcg_gen_or_i32(t2, t2, t3);
1145         tcg_gen_movi_i32(t3, 0);
1146         tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1147         tcg_gen_rem_i32(t3, t0, t1);
1148         tcg_gen_ext_i32_tl(ret, t3);
1149         tcg_temp_free_i32(t2);
1150         tcg_temp_free_i32(t3);
1151     } else {
1152         TCGv_i32 t2 = tcg_const_i32(1);
1153         TCGv_i32 t3 = tcg_const_i32(0);
1154         tcg_gen_movcond_i32(TCG_COND_EQ, t1, t1, t3, t2, t1);
1155         tcg_gen_remu_i32(t3, t0, t1);
1156         tcg_gen_extu_i32_tl(ret, t3);
1157         tcg_temp_free_i32(t2);
1158         tcg_temp_free_i32(t3);
1159     }
1160     tcg_temp_free_i32(t0);
1161     tcg_temp_free_i32(t1);
1162 }
1163 
1164 #define GEN_INT_ARITH_MODW(name, opc3, sign)                                \
1165 static void glue(gen_, name)(DisasContext *ctx)                             \
1166 {                                                                           \
1167     gen_op_arith_modw(ctx, cpu_gpr[rD(ctx->opcode)],                        \
1168                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],   \
1169                       sign);                                                \
1170 }
1171 
1172 GEN_INT_ARITH_MODW(moduw, 0x08, 0);
1173 GEN_INT_ARITH_MODW(modsw, 0x18, 1);
1174 
1175 #if defined(TARGET_PPC64)
1176 static inline void gen_op_arith_modd(DisasContext *ctx, TCGv ret, TCGv arg1,
1177                                      TCGv arg2, int sign)
1178 {
1179     TCGv_i64 t0 = tcg_temp_new_i64();
1180     TCGv_i64 t1 = tcg_temp_new_i64();
1181 
1182     tcg_gen_mov_i64(t0, arg1);
1183     tcg_gen_mov_i64(t1, arg2);
1184     if (sign) {
1185         TCGv_i64 t2 = tcg_temp_new_i64();
1186         TCGv_i64 t3 = tcg_temp_new_i64();
1187         tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t0, INT64_MIN);
1188         tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, -1);
1189         tcg_gen_and_i64(t2, t2, t3);
1190         tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, 0);
1191         tcg_gen_or_i64(t2, t2, t3);
1192         tcg_gen_movi_i64(t3, 0);
1193         tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1194         tcg_gen_rem_i64(ret, t0, t1);
1195         tcg_temp_free_i64(t2);
1196         tcg_temp_free_i64(t3);
1197     } else {
1198         TCGv_i64 t2 = tcg_const_i64(1);
1199         TCGv_i64 t3 = tcg_const_i64(0);
1200         tcg_gen_movcond_i64(TCG_COND_EQ, t1, t1, t3, t2, t1);
1201         tcg_gen_remu_i64(ret, t0, t1);
1202         tcg_temp_free_i64(t2);
1203         tcg_temp_free_i64(t3);
1204     }
1205     tcg_temp_free_i64(t0);
1206     tcg_temp_free_i64(t1);
1207 }
1208 
1209 #define GEN_INT_ARITH_MODD(name, opc3, sign)                            \
1210 static void glue(gen_, name)(DisasContext *ctx)                           \
1211 {                                                                         \
1212   gen_op_arith_modd(ctx, cpu_gpr[rD(ctx->opcode)],                        \
1213                     cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],   \
1214                     sign);                                                \
1215 }
1216 
1217 GEN_INT_ARITH_MODD(modud, 0x08, 0);
1218 GEN_INT_ARITH_MODD(modsd, 0x18, 1);
1219 #endif
1220 
1221 /* mulhw  mulhw. */
1222 static void gen_mulhw(DisasContext *ctx)
1223 {
1224     TCGv_i32 t0 = tcg_temp_new_i32();
1225     TCGv_i32 t1 = tcg_temp_new_i32();
1226 
1227     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1228     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1229     tcg_gen_muls2_i32(t0, t1, t0, t1);
1230     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1231     tcg_temp_free_i32(t0);
1232     tcg_temp_free_i32(t1);
1233     if (unlikely(Rc(ctx->opcode) != 0)) {
1234         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1235     }
1236 }
1237 
1238 /* mulhwu  mulhwu.  */
1239 static void gen_mulhwu(DisasContext *ctx)
1240 {
1241     TCGv_i32 t0 = tcg_temp_new_i32();
1242     TCGv_i32 t1 = tcg_temp_new_i32();
1243 
1244     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1245     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1246     tcg_gen_mulu2_i32(t0, t1, t0, t1);
1247     tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1248     tcg_temp_free_i32(t0);
1249     tcg_temp_free_i32(t1);
1250     if (unlikely(Rc(ctx->opcode) != 0)) {
1251         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1252     }
1253 }
1254 
1255 /* mullw  mullw. */
1256 static void gen_mullw(DisasContext *ctx)
1257 {
1258 #if defined(TARGET_PPC64)
1259     TCGv_i64 t0, t1;
1260     t0 = tcg_temp_new_i64();
1261     t1 = tcg_temp_new_i64();
1262     tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1263     tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1264     tcg_gen_mul_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1265     tcg_temp_free(t0);
1266     tcg_temp_free(t1);
1267 #else
1268     tcg_gen_mul_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1269                     cpu_gpr[rB(ctx->opcode)]);
1270 #endif
1271     if (unlikely(Rc(ctx->opcode) != 0)) {
1272         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1273     }
1274 }
1275 
1276 /* mullwo  mullwo. */
1277 static void gen_mullwo(DisasContext *ctx)
1278 {
1279     TCGv_i32 t0 = tcg_temp_new_i32();
1280     TCGv_i32 t1 = tcg_temp_new_i32();
1281 
1282     tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1283     tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1284     tcg_gen_muls2_i32(t0, t1, t0, t1);
1285 #if defined(TARGET_PPC64)
1286     tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1287 #else
1288     tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], t0);
1289 #endif
1290 
1291     tcg_gen_sari_i32(t0, t0, 31);
1292     tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
1293     tcg_gen_extu_i32_tl(cpu_ov, t0);
1294     if (is_isa300(ctx)) {
1295         tcg_gen_mov_tl(cpu_ov32, cpu_ov);
1296     }
1297     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1298 
1299     tcg_temp_free_i32(t0);
1300     tcg_temp_free_i32(t1);
1301     if (unlikely(Rc(ctx->opcode) != 0)) {
1302         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1303     }
1304 }
1305 
1306 /* mulli */
1307 static void gen_mulli(DisasContext *ctx)
1308 {
1309     tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1310                     SIMM(ctx->opcode));
1311 }
1312 
1313 #if defined(TARGET_PPC64)
1314 /* mulhd  mulhd. */
1315 static void gen_mulhd(DisasContext *ctx)
1316 {
1317     TCGv lo = tcg_temp_new();
1318     tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1319                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1320     tcg_temp_free(lo);
1321     if (unlikely(Rc(ctx->opcode) != 0)) {
1322         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1323     }
1324 }
1325 
1326 /* mulhdu  mulhdu. */
1327 static void gen_mulhdu(DisasContext *ctx)
1328 {
1329     TCGv lo = tcg_temp_new();
1330     tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
1331                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1332     tcg_temp_free(lo);
1333     if (unlikely(Rc(ctx->opcode) != 0)) {
1334         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1335     }
1336 }
1337 
1338 /* mulld  mulld. */
1339 static void gen_mulld(DisasContext *ctx)
1340 {
1341     tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1342                    cpu_gpr[rB(ctx->opcode)]);
1343     if (unlikely(Rc(ctx->opcode) != 0)) {
1344         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1345     }
1346 }
1347 
1348 /* mulldo  mulldo. */
1349 static void gen_mulldo(DisasContext *ctx)
1350 {
1351     TCGv_i64 t0 = tcg_temp_new_i64();
1352     TCGv_i64 t1 = tcg_temp_new_i64();
1353 
1354     tcg_gen_muls2_i64(t0, t1, cpu_gpr[rA(ctx->opcode)],
1355                       cpu_gpr[rB(ctx->opcode)]);
1356     tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], t0);
1357 
1358     tcg_gen_sari_i64(t0, t0, 63);
1359     tcg_gen_setcond_i64(TCG_COND_NE, cpu_ov, t0, t1);
1360     if (is_isa300(ctx)) {
1361         tcg_gen_mov_tl(cpu_ov32, cpu_ov);
1362     }
1363     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1364 
1365     tcg_temp_free_i64(t0);
1366     tcg_temp_free_i64(t1);
1367 
1368     if (unlikely(Rc(ctx->opcode) != 0)) {
1369         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1370     }
1371 }
1372 #endif
1373 
1374 /* Common subf function */
1375 static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
1376                                      TCGv arg2, bool add_ca, bool compute_ca,
1377                                      bool compute_ov, bool compute_rc0)
1378 {
1379     TCGv t0 = ret;
1380 
1381     if (compute_ca || compute_ov) {
1382         t0 = tcg_temp_new();
1383     }
1384 
1385     if (compute_ca) {
1386         /* dest = ~arg1 + arg2 [+ ca].  */
1387         if (NARROW_MODE(ctx)) {
1388             /*
1389              * Caution: a non-obvious corner case of the spec is that
1390              * we must produce the *entire* 64-bit addition, but
1391              * produce the carry into bit 32.
1392              */
1393             TCGv inv1 = tcg_temp_new();
1394             TCGv t1 = tcg_temp_new();
1395             tcg_gen_not_tl(inv1, arg1);
1396             if (add_ca) {
1397                 tcg_gen_add_tl(t0, arg2, cpu_ca);
1398             } else {
1399                 tcg_gen_addi_tl(t0, arg2, 1);
1400             }
1401             tcg_gen_xor_tl(t1, arg2, inv1);         /* add without carry */
1402             tcg_gen_add_tl(t0, t0, inv1);
1403             tcg_temp_free(inv1);
1404             tcg_gen_xor_tl(cpu_ca, t0, t1);         /* bits changes w/ carry */
1405             tcg_temp_free(t1);
1406             tcg_gen_extract_tl(cpu_ca, cpu_ca, 32, 1);
1407             if (is_isa300(ctx)) {
1408                 tcg_gen_mov_tl(cpu_ca32, cpu_ca);
1409             }
1410         } else if (add_ca) {
1411             TCGv zero, inv1 = tcg_temp_new();
1412             tcg_gen_not_tl(inv1, arg1);
1413             zero = tcg_const_tl(0);
1414             tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
1415             tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
1416             gen_op_arith_compute_ca32(ctx, t0, inv1, arg2, cpu_ca32, 0);
1417             tcg_temp_free(zero);
1418             tcg_temp_free(inv1);
1419         } else {
1420             tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
1421             tcg_gen_sub_tl(t0, arg2, arg1);
1422             gen_op_arith_compute_ca32(ctx, t0, arg1, arg2, cpu_ca32, 1);
1423         }
1424     } else if (add_ca) {
1425         /*
1426          * Since we're ignoring carry-out, we can simplify the
1427          * standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.
1428          */
1429         tcg_gen_sub_tl(t0, arg2, arg1);
1430         tcg_gen_add_tl(t0, t0, cpu_ca);
1431         tcg_gen_subi_tl(t0, t0, 1);
1432     } else {
1433         tcg_gen_sub_tl(t0, arg2, arg1);
1434     }
1435 
1436     if (compute_ov) {
1437         gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
1438     }
1439     if (unlikely(compute_rc0)) {
1440         gen_set_Rc0(ctx, t0);
1441     }
1442 
1443     if (t0 != ret) {
1444         tcg_gen_mov_tl(ret, t0);
1445         tcg_temp_free(t0);
1446     }
1447 }
1448 /* Sub functions with Two operands functions */
1449 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
1450 static void glue(gen_, name)(DisasContext *ctx)                               \
1451 {                                                                             \
1452     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1453                       cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],     \
1454                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1455 }
1456 /* Sub functions with one operand and one immediate */
1457 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
1458                                 add_ca, compute_ca, compute_ov)               \
1459 static void glue(gen_, name)(DisasContext *ctx)                               \
1460 {                                                                             \
1461     TCGv t0 = tcg_const_tl(const_val);                                        \
1462     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)],                          \
1463                       cpu_gpr[rA(ctx->opcode)], t0,                           \
1464                       add_ca, compute_ca, compute_ov, Rc(ctx->opcode));       \
1465     tcg_temp_free(t0);                                                        \
1466 }
1467 /* subf  subf.  subfo  subfo. */
1468 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
1469 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
1470 /* subfc  subfc.  subfco  subfco. */
1471 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
1472 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
1473 /* subfe  subfe.  subfeo  subfo. */
1474 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
1475 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
1476 /* subfme  subfme.  subfmeo  subfmeo.  */
1477 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
1478 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
1479 /* subfze  subfze.  subfzeo  subfzeo.*/
1480 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
1481 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
1482 
1483 /* subfic */
1484 static void gen_subfic(DisasContext *ctx)
1485 {
1486     TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1487     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1488                       c, 0, 1, 0, 0);
1489     tcg_temp_free(c);
1490 }
1491 
1492 /* neg neg. nego nego. */
1493 static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
1494 {
1495     TCGv zero = tcg_const_tl(0);
1496     gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1497                       zero, 0, 0, compute_ov, Rc(ctx->opcode));
1498     tcg_temp_free(zero);
1499 }
1500 
1501 static void gen_neg(DisasContext *ctx)
1502 {
1503     tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
1504     if (unlikely(Rc(ctx->opcode))) {
1505         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1506     }
1507 }
1508 
1509 static void gen_nego(DisasContext *ctx)
1510 {
1511     gen_op_arith_neg(ctx, 1);
1512 }
1513 
1514 /***                            Integer logical                            ***/
1515 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
1516 static void glue(gen_, name)(DisasContext *ctx)                               \
1517 {                                                                             \
1518     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],                \
1519        cpu_gpr[rB(ctx->opcode)]);                                             \
1520     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1521         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1522 }
1523 
1524 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
1525 static void glue(gen_, name)(DisasContext *ctx)                               \
1526 {                                                                             \
1527     tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);               \
1528     if (unlikely(Rc(ctx->opcode) != 0))                                       \
1529         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);                           \
1530 }
1531 
1532 /* and & and. */
1533 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
1534 /* andc & andc. */
1535 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
1536 
1537 /* andi. */
1538 static void gen_andi_(DisasContext *ctx)
1539 {
1540     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1541                     UIMM(ctx->opcode));
1542     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1543 }
1544 
1545 /* andis. */
1546 static void gen_andis_(DisasContext *ctx)
1547 {
1548     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1549                     UIMM(ctx->opcode) << 16);
1550     gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1551 }
1552 
1553 /* cntlzw */
1554 static void gen_cntlzw(DisasContext *ctx)
1555 {
1556     TCGv_i32 t = tcg_temp_new_i32();
1557 
1558     tcg_gen_trunc_tl_i32(t, cpu_gpr[rS(ctx->opcode)]);
1559     tcg_gen_clzi_i32(t, t, 32);
1560     tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t);
1561     tcg_temp_free_i32(t);
1562 
1563     if (unlikely(Rc(ctx->opcode) != 0)) {
1564         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1565     }
1566 }
1567 
1568 /* cnttzw */
1569 static void gen_cnttzw(DisasContext *ctx)
1570 {
1571     TCGv_i32 t = tcg_temp_new_i32();
1572 
1573     tcg_gen_trunc_tl_i32(t, cpu_gpr[rS(ctx->opcode)]);
1574     tcg_gen_ctzi_i32(t, t, 32);
1575     tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t);
1576     tcg_temp_free_i32(t);
1577 
1578     if (unlikely(Rc(ctx->opcode) != 0)) {
1579         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1580     }
1581 }
1582 
1583 /* eqv & eqv. */
1584 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
1585 /* extsb & extsb. */
1586 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
1587 /* extsh & extsh. */
1588 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
1589 /* nand & nand. */
1590 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
1591 /* nor & nor. */
1592 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
1593 
1594 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
1595 static void gen_pause(DisasContext *ctx)
1596 {
1597     TCGv_i32 t0 = tcg_const_i32(0);
1598     tcg_gen_st_i32(t0, cpu_env,
1599                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
1600     tcg_temp_free_i32(t0);
1601 
1602     /* Stop translation, this gives other CPUs a chance to run */
1603     gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
1604 }
1605 #endif /* defined(TARGET_PPC64) */
1606 
1607 /* or & or. */
1608 static void gen_or(DisasContext *ctx)
1609 {
1610     int rs, ra, rb;
1611 
1612     rs = rS(ctx->opcode);
1613     ra = rA(ctx->opcode);
1614     rb = rB(ctx->opcode);
1615     /* Optimisation for mr. ri case */
1616     if (rs != ra || rs != rb) {
1617         if (rs != rb) {
1618             tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
1619         } else {
1620             tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
1621         }
1622         if (unlikely(Rc(ctx->opcode) != 0)) {
1623             gen_set_Rc0(ctx, cpu_gpr[ra]);
1624         }
1625     } else if (unlikely(Rc(ctx->opcode) != 0)) {
1626         gen_set_Rc0(ctx, cpu_gpr[rs]);
1627 #if defined(TARGET_PPC64)
1628     } else if (rs != 0) { /* 0 is nop */
1629         int prio = 0;
1630 
1631         switch (rs) {
1632         case 1:
1633             /* Set process priority to low */
1634             prio = 2;
1635             break;
1636         case 6:
1637             /* Set process priority to medium-low */
1638             prio = 3;
1639             break;
1640         case 2:
1641             /* Set process priority to normal */
1642             prio = 4;
1643             break;
1644 #if !defined(CONFIG_USER_ONLY)
1645         case 31:
1646             if (!ctx->pr) {
1647                 /* Set process priority to very low */
1648                 prio = 1;
1649             }
1650             break;
1651         case 5:
1652             if (!ctx->pr) {
1653                 /* Set process priority to medium-hight */
1654                 prio = 5;
1655             }
1656             break;
1657         case 3:
1658             if (!ctx->pr) {
1659                 /* Set process priority to high */
1660                 prio = 6;
1661             }
1662             break;
1663         case 7:
1664             if (ctx->hv && !ctx->pr) {
1665                 /* Set process priority to very high */
1666                 prio = 7;
1667             }
1668             break;
1669 #endif
1670         default:
1671             break;
1672         }
1673         if (prio) {
1674             TCGv t0 = tcg_temp_new();
1675             gen_load_spr(t0, SPR_PPR);
1676             tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
1677             tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
1678             gen_store_spr(SPR_PPR, t0);
1679             tcg_temp_free(t0);
1680         }
1681 #if !defined(CONFIG_USER_ONLY)
1682         /*
1683          * Pause out of TCG otherwise spin loops with smt_low eat too
1684          * much CPU and the kernel hangs.  This applies to all
1685          * encodings other than no-op, e.g., miso(rs=26), yield(27),
1686          * mdoio(29), mdoom(30), and all currently undefined.
1687          */
1688         gen_pause(ctx);
1689 #endif
1690 #endif
1691     }
1692 }
1693 /* orc & orc. */
1694 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
1695 
1696 /* xor & xor. */
1697 static void gen_xor(DisasContext *ctx)
1698 {
1699     /* Optimisation for "set to zero" case */
1700     if (rS(ctx->opcode) != rB(ctx->opcode)) {
1701         tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1702                        cpu_gpr[rB(ctx->opcode)]);
1703     } else {
1704         tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
1705     }
1706     if (unlikely(Rc(ctx->opcode) != 0)) {
1707         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1708     }
1709 }
1710 
1711 /* ori */
1712 static void gen_ori(DisasContext *ctx)
1713 {
1714     target_ulong uimm = UIMM(ctx->opcode);
1715 
1716     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1717         return;
1718     }
1719     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1720 }
1721 
1722 /* oris */
1723 static void gen_oris(DisasContext *ctx)
1724 {
1725     target_ulong uimm = UIMM(ctx->opcode);
1726 
1727     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1728         /* NOP */
1729         return;
1730     }
1731     tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1732                    uimm << 16);
1733 }
1734 
1735 /* xori */
1736 static void gen_xori(DisasContext *ctx)
1737 {
1738     target_ulong uimm = UIMM(ctx->opcode);
1739 
1740     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1741         /* NOP */
1742         return;
1743     }
1744     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
1745 }
1746 
1747 /* xoris */
1748 static void gen_xoris(DisasContext *ctx)
1749 {
1750     target_ulong uimm = UIMM(ctx->opcode);
1751 
1752     if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
1753         /* NOP */
1754         return;
1755     }
1756     tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1757                     uimm << 16);
1758 }
1759 
1760 /* popcntb : PowerPC 2.03 specification */
1761 static void gen_popcntb(DisasContext *ctx)
1762 {
1763     gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1764 }
1765 
1766 static void gen_popcntw(DisasContext *ctx)
1767 {
1768 #if defined(TARGET_PPC64)
1769     gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1770 #else
1771     tcg_gen_ctpop_i32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1772 #endif
1773 }
1774 
1775 #if defined(TARGET_PPC64)
1776 /* popcntd: PowerPC 2.06 specification */
1777 static void gen_popcntd(DisasContext *ctx)
1778 {
1779     tcg_gen_ctpop_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
1780 }
1781 #endif
1782 
1783 /* prtyw: PowerPC 2.05 specification */
1784 static void gen_prtyw(DisasContext *ctx)
1785 {
1786     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1787     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1788     TCGv t0 = tcg_temp_new();
1789     tcg_gen_shri_tl(t0, rs, 16);
1790     tcg_gen_xor_tl(ra, rs, t0);
1791     tcg_gen_shri_tl(t0, ra, 8);
1792     tcg_gen_xor_tl(ra, ra, t0);
1793     tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
1794     tcg_temp_free(t0);
1795 }
1796 
1797 #if defined(TARGET_PPC64)
1798 /* prtyd: PowerPC 2.05 specification */
1799 static void gen_prtyd(DisasContext *ctx)
1800 {
1801     TCGv ra = cpu_gpr[rA(ctx->opcode)];
1802     TCGv rs = cpu_gpr[rS(ctx->opcode)];
1803     TCGv t0 = tcg_temp_new();
1804     tcg_gen_shri_tl(t0, rs, 32);
1805     tcg_gen_xor_tl(ra, rs, t0);
1806     tcg_gen_shri_tl(t0, ra, 16);
1807     tcg_gen_xor_tl(ra, ra, t0);
1808     tcg_gen_shri_tl(t0, ra, 8);
1809     tcg_gen_xor_tl(ra, ra, t0);
1810     tcg_gen_andi_tl(ra, ra, 1);
1811     tcg_temp_free(t0);
1812 }
1813 #endif
1814 
1815 #if defined(TARGET_PPC64)
1816 /* bpermd */
1817 static void gen_bpermd(DisasContext *ctx)
1818 {
1819     gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
1820                       cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
1821 }
1822 #endif
1823 
1824 #if defined(TARGET_PPC64)
1825 /* extsw & extsw. */
1826 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
1827 
1828 /* cntlzd */
1829 static void gen_cntlzd(DisasContext *ctx)
1830 {
1831     tcg_gen_clzi_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], 64);
1832     if (unlikely(Rc(ctx->opcode) != 0)) {
1833         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1834     }
1835 }
1836 
1837 /* cnttzd */
1838 static void gen_cnttzd(DisasContext *ctx)
1839 {
1840     tcg_gen_ctzi_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], 64);
1841     if (unlikely(Rc(ctx->opcode) != 0)) {
1842         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
1843     }
1844 }
1845 
1846 /* darn */
1847 static void gen_darn(DisasContext *ctx)
1848 {
1849     int l = L(ctx->opcode);
1850 
1851     if (l > 2) {
1852         tcg_gen_movi_i64(cpu_gpr[rD(ctx->opcode)], -1);
1853     } else {
1854         if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
1855             gen_io_start();
1856         }
1857         if (l == 0) {
1858             gen_helper_darn32(cpu_gpr[rD(ctx->opcode)]);
1859         } else {
1860             /* Return 64-bit random for both CRN and RRN */
1861             gen_helper_darn64(cpu_gpr[rD(ctx->opcode)]);
1862         }
1863         if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
1864             gen_stop_exception(ctx);
1865         }
1866     }
1867 }
1868 #endif
1869 
1870 /***                             Integer rotate                            ***/
1871 
1872 /* rlwimi & rlwimi. */
1873 static void gen_rlwimi(DisasContext *ctx)
1874 {
1875     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1876     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1877     uint32_t sh = SH(ctx->opcode);
1878     uint32_t mb = MB(ctx->opcode);
1879     uint32_t me = ME(ctx->opcode);
1880 
1881     if (sh == (31 - me) && mb <= me) {
1882         tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
1883     } else {
1884         target_ulong mask;
1885         TCGv t1;
1886 
1887 #if defined(TARGET_PPC64)
1888         mb += 32;
1889         me += 32;
1890 #endif
1891         mask = MASK(mb, me);
1892 
1893         t1 = tcg_temp_new();
1894         if (mask <= 0xffffffffu) {
1895             TCGv_i32 t0 = tcg_temp_new_i32();
1896             tcg_gen_trunc_tl_i32(t0, t_rs);
1897             tcg_gen_rotli_i32(t0, t0, sh);
1898             tcg_gen_extu_i32_tl(t1, t0);
1899             tcg_temp_free_i32(t0);
1900         } else {
1901 #if defined(TARGET_PPC64)
1902             tcg_gen_deposit_i64(t1, t_rs, t_rs, 32, 32);
1903             tcg_gen_rotli_i64(t1, t1, sh);
1904 #else
1905             g_assert_not_reached();
1906 #endif
1907         }
1908 
1909         tcg_gen_andi_tl(t1, t1, mask);
1910         tcg_gen_andi_tl(t_ra, t_ra, ~mask);
1911         tcg_gen_or_tl(t_ra, t_ra, t1);
1912         tcg_temp_free(t1);
1913     }
1914     if (unlikely(Rc(ctx->opcode) != 0)) {
1915         gen_set_Rc0(ctx, t_ra);
1916     }
1917 }
1918 
1919 /* rlwinm & rlwinm. */
1920 static void gen_rlwinm(DisasContext *ctx)
1921 {
1922     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1923     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1924     int sh = SH(ctx->opcode);
1925     int mb = MB(ctx->opcode);
1926     int me = ME(ctx->opcode);
1927     int len = me - mb + 1;
1928     int rsh = (32 - sh) & 31;
1929 
1930     if (sh != 0 && len > 0 && me == (31 - sh)) {
1931         tcg_gen_deposit_z_tl(t_ra, t_rs, sh, len);
1932     } else if (me == 31 && rsh + len <= 32) {
1933         tcg_gen_extract_tl(t_ra, t_rs, rsh, len);
1934     } else {
1935         target_ulong mask;
1936 #if defined(TARGET_PPC64)
1937         mb += 32;
1938         me += 32;
1939 #endif
1940         mask = MASK(mb, me);
1941         if (mask <= 0xffffffffu) {
1942             if (sh == 0) {
1943                 tcg_gen_andi_tl(t_ra, t_rs, mask);
1944             } else {
1945                 TCGv_i32 t0 = tcg_temp_new_i32();
1946                 tcg_gen_trunc_tl_i32(t0, t_rs);
1947                 tcg_gen_rotli_i32(t0, t0, sh);
1948                 tcg_gen_andi_i32(t0, t0, mask);
1949                 tcg_gen_extu_i32_tl(t_ra, t0);
1950                 tcg_temp_free_i32(t0);
1951             }
1952         } else {
1953 #if defined(TARGET_PPC64)
1954             tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
1955             tcg_gen_rotli_i64(t_ra, t_ra, sh);
1956             tcg_gen_andi_i64(t_ra, t_ra, mask);
1957 #else
1958             g_assert_not_reached();
1959 #endif
1960         }
1961     }
1962     if (unlikely(Rc(ctx->opcode) != 0)) {
1963         gen_set_Rc0(ctx, t_ra);
1964     }
1965 }
1966 
1967 /* rlwnm & rlwnm. */
1968 static void gen_rlwnm(DisasContext *ctx)
1969 {
1970     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
1971     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
1972     TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
1973     uint32_t mb = MB(ctx->opcode);
1974     uint32_t me = ME(ctx->opcode);
1975     target_ulong mask;
1976 
1977 #if defined(TARGET_PPC64)
1978     mb += 32;
1979     me += 32;
1980 #endif
1981     mask = MASK(mb, me);
1982 
1983     if (mask <= 0xffffffffu) {
1984         TCGv_i32 t0 = tcg_temp_new_i32();
1985         TCGv_i32 t1 = tcg_temp_new_i32();
1986         tcg_gen_trunc_tl_i32(t0, t_rb);
1987         tcg_gen_trunc_tl_i32(t1, t_rs);
1988         tcg_gen_andi_i32(t0, t0, 0x1f);
1989         tcg_gen_rotl_i32(t1, t1, t0);
1990         tcg_gen_extu_i32_tl(t_ra, t1);
1991         tcg_temp_free_i32(t0);
1992         tcg_temp_free_i32(t1);
1993     } else {
1994 #if defined(TARGET_PPC64)
1995         TCGv_i64 t0 = tcg_temp_new_i64();
1996         tcg_gen_andi_i64(t0, t_rb, 0x1f);
1997         tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
1998         tcg_gen_rotl_i64(t_ra, t_ra, t0);
1999         tcg_temp_free_i64(t0);
2000 #else
2001         g_assert_not_reached();
2002 #endif
2003     }
2004 
2005     tcg_gen_andi_tl(t_ra, t_ra, mask);
2006 
2007     if (unlikely(Rc(ctx->opcode) != 0)) {
2008         gen_set_Rc0(ctx, t_ra);
2009     }
2010 }
2011 
2012 #if defined(TARGET_PPC64)
2013 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
2014 static void glue(gen_, name##0)(DisasContext *ctx)                            \
2015 {                                                                             \
2016     gen_##name(ctx, 0);                                                       \
2017 }                                                                             \
2018                                                                               \
2019 static void glue(gen_, name##1)(DisasContext *ctx)                            \
2020 {                                                                             \
2021     gen_##name(ctx, 1);                                                       \
2022 }
2023 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
2024 static void glue(gen_, name##0)(DisasContext *ctx)                            \
2025 {                                                                             \
2026     gen_##name(ctx, 0, 0);                                                    \
2027 }                                                                             \
2028                                                                               \
2029 static void glue(gen_, name##1)(DisasContext *ctx)                            \
2030 {                                                                             \
2031     gen_##name(ctx, 0, 1);                                                    \
2032 }                                                                             \
2033                                                                               \
2034 static void glue(gen_, name##2)(DisasContext *ctx)                            \
2035 {                                                                             \
2036     gen_##name(ctx, 1, 0);                                                    \
2037 }                                                                             \
2038                                                                               \
2039 static void glue(gen_, name##3)(DisasContext *ctx)                            \
2040 {                                                                             \
2041     gen_##name(ctx, 1, 1);                                                    \
2042 }
2043 
2044 static void gen_rldinm(DisasContext *ctx, int mb, int me, int sh)
2045 {
2046     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2047     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2048     int len = me - mb + 1;
2049     int rsh = (64 - sh) & 63;
2050 
2051     if (sh != 0 && len > 0 && me == (63 - sh)) {
2052         tcg_gen_deposit_z_tl(t_ra, t_rs, sh, len);
2053     } else if (me == 63 && rsh + len <= 64) {
2054         tcg_gen_extract_tl(t_ra, t_rs, rsh, len);
2055     } else {
2056         tcg_gen_rotli_tl(t_ra, t_rs, sh);
2057         tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2058     }
2059     if (unlikely(Rc(ctx->opcode) != 0)) {
2060         gen_set_Rc0(ctx, t_ra);
2061     }
2062 }
2063 
2064 /* rldicl - rldicl. */
2065 static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
2066 {
2067     uint32_t sh, mb;
2068 
2069     sh = SH(ctx->opcode) | (shn << 5);
2070     mb = MB(ctx->opcode) | (mbn << 5);
2071     gen_rldinm(ctx, mb, 63, sh);
2072 }
2073 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
2074 
2075 /* rldicr - rldicr. */
2076 static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
2077 {
2078     uint32_t sh, me;
2079 
2080     sh = SH(ctx->opcode) | (shn << 5);
2081     me = MB(ctx->opcode) | (men << 5);
2082     gen_rldinm(ctx, 0, me, sh);
2083 }
2084 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
2085 
2086 /* rldic - rldic. */
2087 static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
2088 {
2089     uint32_t sh, mb;
2090 
2091     sh = SH(ctx->opcode) | (shn << 5);
2092     mb = MB(ctx->opcode) | (mbn << 5);
2093     gen_rldinm(ctx, mb, 63 - sh, sh);
2094 }
2095 GEN_PPC64_R4(rldic, 0x1E, 0x04);
2096 
2097 static void gen_rldnm(DisasContext *ctx, int mb, int me)
2098 {
2099     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2100     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2101     TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
2102     TCGv t0;
2103 
2104     t0 = tcg_temp_new();
2105     tcg_gen_andi_tl(t0, t_rb, 0x3f);
2106     tcg_gen_rotl_tl(t_ra, t_rs, t0);
2107     tcg_temp_free(t0);
2108 
2109     tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2110     if (unlikely(Rc(ctx->opcode) != 0)) {
2111         gen_set_Rc0(ctx, t_ra);
2112     }
2113 }
2114 
2115 /* rldcl - rldcl. */
2116 static inline void gen_rldcl(DisasContext *ctx, int mbn)
2117 {
2118     uint32_t mb;
2119 
2120     mb = MB(ctx->opcode) | (mbn << 5);
2121     gen_rldnm(ctx, mb, 63);
2122 }
2123 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
2124 
2125 /* rldcr - rldcr. */
2126 static inline void gen_rldcr(DisasContext *ctx, int men)
2127 {
2128     uint32_t me;
2129 
2130     me = MB(ctx->opcode) | (men << 5);
2131     gen_rldnm(ctx, 0, me);
2132 }
2133 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
2134 
2135 /* rldimi - rldimi. */
2136 static void gen_rldimi(DisasContext *ctx, int mbn, int shn)
2137 {
2138     TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2139     TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2140     uint32_t sh = SH(ctx->opcode) | (shn << 5);
2141     uint32_t mb = MB(ctx->opcode) | (mbn << 5);
2142     uint32_t me = 63 - sh;
2143 
2144     if (mb <= me) {
2145         tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
2146     } else {
2147         target_ulong mask = MASK(mb, me);
2148         TCGv t1 = tcg_temp_new();
2149 
2150         tcg_gen_rotli_tl(t1, t_rs, sh);
2151         tcg_gen_andi_tl(t1, t1, mask);
2152         tcg_gen_andi_tl(t_ra, t_ra, ~mask);
2153         tcg_gen_or_tl(t_ra, t_ra, t1);
2154         tcg_temp_free(t1);
2155     }
2156     if (unlikely(Rc(ctx->opcode) != 0)) {
2157         gen_set_Rc0(ctx, t_ra);
2158     }
2159 }
2160 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
2161 #endif
2162 
2163 /***                             Integer shift                             ***/
2164 
2165 /* slw & slw. */
2166 static void gen_slw(DisasContext *ctx)
2167 {
2168     TCGv t0, t1;
2169 
2170     t0 = tcg_temp_new();
2171     /* AND rS with a mask that is 0 when rB >= 0x20 */
2172 #if defined(TARGET_PPC64)
2173     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2174     tcg_gen_sari_tl(t0, t0, 0x3f);
2175 #else
2176     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2177     tcg_gen_sari_tl(t0, t0, 0x1f);
2178 #endif
2179     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2180     t1 = tcg_temp_new();
2181     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2182     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2183     tcg_temp_free(t1);
2184     tcg_temp_free(t0);
2185     tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
2186     if (unlikely(Rc(ctx->opcode) != 0)) {
2187         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2188     }
2189 }
2190 
2191 /* sraw & sraw. */
2192 static void gen_sraw(DisasContext *ctx)
2193 {
2194     gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
2195                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2196     if (unlikely(Rc(ctx->opcode) != 0)) {
2197         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2198     }
2199 }
2200 
2201 /* srawi & srawi. */
2202 static void gen_srawi(DisasContext *ctx)
2203 {
2204     int sh = SH(ctx->opcode);
2205     TCGv dst = cpu_gpr[rA(ctx->opcode)];
2206     TCGv src = cpu_gpr[rS(ctx->opcode)];
2207     if (sh == 0) {
2208         tcg_gen_ext32s_tl(dst, src);
2209         tcg_gen_movi_tl(cpu_ca, 0);
2210         if (is_isa300(ctx)) {
2211             tcg_gen_movi_tl(cpu_ca32, 0);
2212         }
2213     } else {
2214         TCGv t0;
2215         tcg_gen_ext32s_tl(dst, src);
2216         tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
2217         t0 = tcg_temp_new();
2218         tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
2219         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2220         tcg_temp_free(t0);
2221         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2222         if (is_isa300(ctx)) {
2223             tcg_gen_mov_tl(cpu_ca32, cpu_ca);
2224         }
2225         tcg_gen_sari_tl(dst, dst, sh);
2226     }
2227     if (unlikely(Rc(ctx->opcode) != 0)) {
2228         gen_set_Rc0(ctx, dst);
2229     }
2230 }
2231 
2232 /* srw & srw. */
2233 static void gen_srw(DisasContext *ctx)
2234 {
2235     TCGv t0, t1;
2236 
2237     t0 = tcg_temp_new();
2238     /* AND rS with a mask that is 0 when rB >= 0x20 */
2239 #if defined(TARGET_PPC64)
2240     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2241     tcg_gen_sari_tl(t0, t0, 0x3f);
2242 #else
2243     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2244     tcg_gen_sari_tl(t0, t0, 0x1f);
2245 #endif
2246     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2247     tcg_gen_ext32u_tl(t0, t0);
2248     t1 = tcg_temp_new();
2249     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2250     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2251     tcg_temp_free(t1);
2252     tcg_temp_free(t0);
2253     if (unlikely(Rc(ctx->opcode) != 0)) {
2254         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2255     }
2256 }
2257 
2258 #if defined(TARGET_PPC64)
2259 /* sld & sld. */
2260 static void gen_sld(DisasContext *ctx)
2261 {
2262     TCGv t0, t1;
2263 
2264     t0 = tcg_temp_new();
2265     /* AND rS with a mask that is 0 when rB >= 0x40 */
2266     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2267     tcg_gen_sari_tl(t0, t0, 0x3f);
2268     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2269     t1 = tcg_temp_new();
2270     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2271     tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2272     tcg_temp_free(t1);
2273     tcg_temp_free(t0);
2274     if (unlikely(Rc(ctx->opcode) != 0)) {
2275         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2276     }
2277 }
2278 
2279 /* srad & srad. */
2280 static void gen_srad(DisasContext *ctx)
2281 {
2282     gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
2283                     cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2284     if (unlikely(Rc(ctx->opcode) != 0)) {
2285         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2286     }
2287 }
2288 /* sradi & sradi. */
2289 static inline void gen_sradi(DisasContext *ctx, int n)
2290 {
2291     int sh = SH(ctx->opcode) + (n << 5);
2292     TCGv dst = cpu_gpr[rA(ctx->opcode)];
2293     TCGv src = cpu_gpr[rS(ctx->opcode)];
2294     if (sh == 0) {
2295         tcg_gen_mov_tl(dst, src);
2296         tcg_gen_movi_tl(cpu_ca, 0);
2297         if (is_isa300(ctx)) {
2298             tcg_gen_movi_tl(cpu_ca32, 0);
2299         }
2300     } else {
2301         TCGv t0;
2302         tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
2303         t0 = tcg_temp_new();
2304         tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
2305         tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2306         tcg_temp_free(t0);
2307         tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2308         if (is_isa300(ctx)) {
2309             tcg_gen_mov_tl(cpu_ca32, cpu_ca);
2310         }
2311         tcg_gen_sari_tl(dst, src, sh);
2312     }
2313     if (unlikely(Rc(ctx->opcode) != 0)) {
2314         gen_set_Rc0(ctx, dst);
2315     }
2316 }
2317 
2318 static void gen_sradi0(DisasContext *ctx)
2319 {
2320     gen_sradi(ctx, 0);
2321 }
2322 
2323 static void gen_sradi1(DisasContext *ctx)
2324 {
2325     gen_sradi(ctx, 1);
2326 }
2327 
2328 /* extswsli & extswsli. */
2329 static inline void gen_extswsli(DisasContext *ctx, int n)
2330 {
2331     int sh = SH(ctx->opcode) + (n << 5);
2332     TCGv dst = cpu_gpr[rA(ctx->opcode)];
2333     TCGv src = cpu_gpr[rS(ctx->opcode)];
2334 
2335     tcg_gen_ext32s_tl(dst, src);
2336     tcg_gen_shli_tl(dst, dst, sh);
2337     if (unlikely(Rc(ctx->opcode) != 0)) {
2338         gen_set_Rc0(ctx, dst);
2339     }
2340 }
2341 
2342 static void gen_extswsli0(DisasContext *ctx)
2343 {
2344     gen_extswsli(ctx, 0);
2345 }
2346 
2347 static void gen_extswsli1(DisasContext *ctx)
2348 {
2349     gen_extswsli(ctx, 1);
2350 }
2351 
2352 /* srd & srd. */
2353 static void gen_srd(DisasContext *ctx)
2354 {
2355     TCGv t0, t1;
2356 
2357     t0 = tcg_temp_new();
2358     /* AND rS with a mask that is 0 when rB >= 0x40 */
2359     tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2360     tcg_gen_sari_tl(t0, t0, 0x3f);
2361     tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2362     t1 = tcg_temp_new();
2363     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2364     tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2365     tcg_temp_free(t1);
2366     tcg_temp_free(t0);
2367     if (unlikely(Rc(ctx->opcode) != 0)) {
2368         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2369     }
2370 }
2371 #endif
2372 
2373 /***                           Addressing modes                            ***/
2374 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
2375 static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
2376                                       target_long maskl)
2377 {
2378     target_long simm = SIMM(ctx->opcode);
2379 
2380     simm &= ~maskl;
2381     if (rA(ctx->opcode) == 0) {
2382         if (NARROW_MODE(ctx)) {
2383             simm = (uint32_t)simm;
2384         }
2385         tcg_gen_movi_tl(EA, simm);
2386     } else if (likely(simm != 0)) {
2387         tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
2388         if (NARROW_MODE(ctx)) {
2389             tcg_gen_ext32u_tl(EA, EA);
2390         }
2391     } else {
2392         if (NARROW_MODE(ctx)) {
2393             tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2394         } else {
2395             tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2396         }
2397     }
2398 }
2399 
2400 static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
2401 {
2402     if (rA(ctx->opcode) == 0) {
2403         if (NARROW_MODE(ctx)) {
2404             tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2405         } else {
2406             tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
2407         }
2408     } else {
2409         tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2410         if (NARROW_MODE(ctx)) {
2411             tcg_gen_ext32u_tl(EA, EA);
2412         }
2413     }
2414 }
2415 
2416 static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
2417 {
2418     if (rA(ctx->opcode) == 0) {
2419         tcg_gen_movi_tl(EA, 0);
2420     } else if (NARROW_MODE(ctx)) {
2421         tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2422     } else {
2423         tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
2424     }
2425 }
2426 
2427 static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
2428                                 target_long val)
2429 {
2430     tcg_gen_addi_tl(ret, arg1, val);
2431     if (NARROW_MODE(ctx)) {
2432         tcg_gen_ext32u_tl(ret, ret);
2433     }
2434 }
2435 
2436 static inline void gen_align_no_le(DisasContext *ctx)
2437 {
2438     gen_exception_err(ctx, POWERPC_EXCP_ALIGN,
2439                       (ctx->opcode & 0x03FF0000) | POWERPC_EXCP_ALIGN_LE);
2440 }
2441 
2442 /***                             Integer load                              ***/
2443 #define DEF_MEMOP(op) ((op) | ctx->default_tcg_memop_mask)
2444 #define BSWAP_MEMOP(op) ((op) | (ctx->default_tcg_memop_mask ^ MO_BSWAP))
2445 
2446 #define GEN_QEMU_LOAD_TL(ldop, op)                                      \
2447 static void glue(gen_qemu_, ldop)(DisasContext *ctx,                    \
2448                                   TCGv val,                             \
2449                                   TCGv addr)                            \
2450 {                                                                       \
2451     tcg_gen_qemu_ld_tl(val, addr, ctx->mem_idx, op);                    \
2452 }
2453 
2454 GEN_QEMU_LOAD_TL(ld8u,  DEF_MEMOP(MO_UB))
2455 GEN_QEMU_LOAD_TL(ld16u, DEF_MEMOP(MO_UW))
2456 GEN_QEMU_LOAD_TL(ld16s, DEF_MEMOP(MO_SW))
2457 GEN_QEMU_LOAD_TL(ld32u, DEF_MEMOP(MO_UL))
2458 GEN_QEMU_LOAD_TL(ld32s, DEF_MEMOP(MO_SL))
2459 
2460 GEN_QEMU_LOAD_TL(ld16ur, BSWAP_MEMOP(MO_UW))
2461 GEN_QEMU_LOAD_TL(ld32ur, BSWAP_MEMOP(MO_UL))
2462 
2463 #define GEN_QEMU_LOAD_64(ldop, op)                                  \
2464 static void glue(gen_qemu_, glue(ldop, _i64))(DisasContext *ctx,    \
2465                                              TCGv_i64 val,          \
2466                                              TCGv addr)             \
2467 {                                                                   \
2468     tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, op);               \
2469 }
2470 
2471 GEN_QEMU_LOAD_64(ld8u,  DEF_MEMOP(MO_UB))
2472 GEN_QEMU_LOAD_64(ld16u, DEF_MEMOP(MO_UW))
2473 GEN_QEMU_LOAD_64(ld32u, DEF_MEMOP(MO_UL))
2474 GEN_QEMU_LOAD_64(ld32s, DEF_MEMOP(MO_SL))
2475 GEN_QEMU_LOAD_64(ld64,  DEF_MEMOP(MO_Q))
2476 
2477 #if defined(TARGET_PPC64)
2478 GEN_QEMU_LOAD_64(ld64ur, BSWAP_MEMOP(MO_Q))
2479 #endif
2480 
2481 #define GEN_QEMU_STORE_TL(stop, op)                                     \
2482 static void glue(gen_qemu_, stop)(DisasContext *ctx,                    \
2483                                   TCGv val,                             \
2484                                   TCGv addr)                            \
2485 {                                                                       \
2486     tcg_gen_qemu_st_tl(val, addr, ctx->mem_idx, op);                    \
2487 }
2488 
2489 GEN_QEMU_STORE_TL(st8,  DEF_MEMOP(MO_UB))
2490 GEN_QEMU_STORE_TL(st16, DEF_MEMOP(MO_UW))
2491 GEN_QEMU_STORE_TL(st32, DEF_MEMOP(MO_UL))
2492 
2493 GEN_QEMU_STORE_TL(st16r, BSWAP_MEMOP(MO_UW))
2494 GEN_QEMU_STORE_TL(st32r, BSWAP_MEMOP(MO_UL))
2495 
2496 #define GEN_QEMU_STORE_64(stop, op)                               \
2497 static void glue(gen_qemu_, glue(stop, _i64))(DisasContext *ctx,  \
2498                                               TCGv_i64 val,       \
2499                                               TCGv addr)          \
2500 {                                                                 \
2501     tcg_gen_qemu_st_i64(val, addr, ctx->mem_idx, op);             \
2502 }
2503 
2504 GEN_QEMU_STORE_64(st8,  DEF_MEMOP(MO_UB))
2505 GEN_QEMU_STORE_64(st16, DEF_MEMOP(MO_UW))
2506 GEN_QEMU_STORE_64(st32, DEF_MEMOP(MO_UL))
2507 GEN_QEMU_STORE_64(st64, DEF_MEMOP(MO_Q))
2508 
2509 #if defined(TARGET_PPC64)
2510 GEN_QEMU_STORE_64(st64r, BSWAP_MEMOP(MO_Q))
2511 #endif
2512 
2513 #define GEN_LD(name, ldop, opc, type)                                         \
2514 static void glue(gen_, name)(DisasContext *ctx)                               \
2515 {                                                                             \
2516     TCGv EA;                                                                  \
2517     gen_set_access_type(ctx, ACCESS_INT);                                     \
2518     EA = tcg_temp_new();                                                      \
2519     gen_addr_imm_index(ctx, EA, 0);                                           \
2520     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2521     tcg_temp_free(EA);                                                        \
2522 }
2523 
2524 #define GEN_LDU(name, ldop, opc, type)                                        \
2525 static void glue(gen_, name##u)(DisasContext *ctx)                            \
2526 {                                                                             \
2527     TCGv EA;                                                                  \
2528     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2529                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2530         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2531         return;                                                               \
2532     }                                                                         \
2533     gen_set_access_type(ctx, ACCESS_INT);                                     \
2534     EA = tcg_temp_new();                                                      \
2535     if (type == PPC_64B)                                                      \
2536         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2537     else                                                                      \
2538         gen_addr_imm_index(ctx, EA, 0);                                       \
2539     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2540     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2541     tcg_temp_free(EA);                                                        \
2542 }
2543 
2544 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
2545 static void glue(gen_, name##ux)(DisasContext *ctx)                           \
2546 {                                                                             \
2547     TCGv EA;                                                                  \
2548     if (unlikely(rA(ctx->opcode) == 0 ||                                      \
2549                  rA(ctx->opcode) == rD(ctx->opcode))) {                       \
2550         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2551         return;                                                               \
2552     }                                                                         \
2553     gen_set_access_type(ctx, ACCESS_INT);                                     \
2554     EA = tcg_temp_new();                                                      \
2555     gen_addr_reg_index(ctx, EA);                                              \
2556     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2557     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2558     tcg_temp_free(EA);                                                        \
2559 }
2560 
2561 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
2562 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2563 {                                                                             \
2564     TCGv EA;                                                                  \
2565     chk;                                                                      \
2566     gen_set_access_type(ctx, ACCESS_INT);                                     \
2567     EA = tcg_temp_new();                                                      \
2568     gen_addr_reg_index(ctx, EA);                                              \
2569     gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA);                       \
2570     tcg_temp_free(EA);                                                        \
2571 }
2572 
2573 #define GEN_LDX(name, ldop, opc2, opc3, type)                                 \
2574     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_NONE)
2575 
2576 #define GEN_LDX_HVRM(name, ldop, opc2, opc3, type)                            \
2577     GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
2578 
2579 #define GEN_LDS(name, ldop, op, type)                                         \
2580 GEN_LD(name, ldop, op | 0x20, type);                                          \
2581 GEN_LDU(name, ldop, op | 0x21, type);                                         \
2582 GEN_LDUX(name, ldop, 0x17, op | 0x01, type);                                  \
2583 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
2584 
2585 /* lbz lbzu lbzux lbzx */
2586 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER);
2587 /* lha lhau lhaux lhax */
2588 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER);
2589 /* lhz lhzu lhzux lhzx */
2590 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER);
2591 /* lwz lwzu lwzux lwzx */
2592 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER);
2593 
2594 #define GEN_LDEPX(name, ldop, opc2, opc3)                                     \
2595 static void glue(gen_, name##epx)(DisasContext *ctx)                          \
2596 {                                                                             \
2597     TCGv EA;                                                                  \
2598     CHK_SV;                                                                   \
2599     gen_set_access_type(ctx, ACCESS_INT);                                     \
2600     EA = tcg_temp_new();                                                      \
2601     gen_addr_reg_index(ctx, EA);                                              \
2602     tcg_gen_qemu_ld_tl(cpu_gpr[rD(ctx->opcode)], EA, PPC_TLB_EPID_LOAD, ldop);\
2603     tcg_temp_free(EA);                                                        \
2604 }
2605 
2606 GEN_LDEPX(lb, DEF_MEMOP(MO_UB), 0x1F, 0x02)
2607 GEN_LDEPX(lh, DEF_MEMOP(MO_UW), 0x1F, 0x08)
2608 GEN_LDEPX(lw, DEF_MEMOP(MO_UL), 0x1F, 0x00)
2609 #if defined(TARGET_PPC64)
2610 GEN_LDEPX(ld, DEF_MEMOP(MO_Q), 0x1D, 0x00)
2611 #endif
2612 
2613 #if defined(TARGET_PPC64)
2614 /* lwaux */
2615 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B);
2616 /* lwax */
2617 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B);
2618 /* ldux */
2619 GEN_LDUX(ld, ld64_i64, 0x15, 0x01, PPC_64B);
2620 /* ldx */
2621 GEN_LDX(ld, ld64_i64, 0x15, 0x00, PPC_64B);
2622 
2623 /* CI load/store variants */
2624 GEN_LDX_HVRM(ldcix, ld64_i64, 0x15, 0x1b, PPC_CILDST)
2625 GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x15, PPC_CILDST)
2626 GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
2627 GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
2628 
2629 static void gen_ld(DisasContext *ctx)
2630 {
2631     TCGv EA;
2632     if (Rc(ctx->opcode)) {
2633         if (unlikely(rA(ctx->opcode) == 0 ||
2634                      rA(ctx->opcode) == rD(ctx->opcode))) {
2635             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2636             return;
2637         }
2638     }
2639     gen_set_access_type(ctx, ACCESS_INT);
2640     EA = tcg_temp_new();
2641     gen_addr_imm_index(ctx, EA, 0x03);
2642     if (ctx->opcode & 0x02) {
2643         /* lwa (lwau is undefined) */
2644         gen_qemu_ld32s(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2645     } else {
2646         /* ld - ldu */
2647         gen_qemu_ld64_i64(ctx, cpu_gpr[rD(ctx->opcode)], EA);
2648     }
2649     if (Rc(ctx->opcode)) {
2650         tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2651     }
2652     tcg_temp_free(EA);
2653 }
2654 
2655 /* lq */
2656 static void gen_lq(DisasContext *ctx)
2657 {
2658     int ra, rd;
2659     TCGv EA, hi, lo;
2660 
2661     /* lq is a legal user mode instruction starting in ISA 2.07 */
2662     bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2663     bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2664 
2665     if (!legal_in_user_mode && ctx->pr) {
2666         gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2667         return;
2668     }
2669 
2670     if (!le_is_supported && ctx->le_mode) {
2671         gen_align_no_le(ctx);
2672         return;
2673     }
2674     ra = rA(ctx->opcode);
2675     rd = rD(ctx->opcode);
2676     if (unlikely((rd & 1) || rd == ra)) {
2677         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2678         return;
2679     }
2680 
2681     gen_set_access_type(ctx, ACCESS_INT);
2682     EA = tcg_temp_new();
2683     gen_addr_imm_index(ctx, EA, 0x0F);
2684 
2685     /* Note that the low part is always in RD+1, even in LE mode.  */
2686     lo = cpu_gpr[rd + 1];
2687     hi = cpu_gpr[rd];
2688 
2689     if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2690         if (HAVE_ATOMIC128) {
2691             TCGv_i32 oi = tcg_temp_new_i32();
2692             if (ctx->le_mode) {
2693                 tcg_gen_movi_i32(oi, make_memop_idx(MO_LEQ, ctx->mem_idx));
2694                 gen_helper_lq_le_parallel(lo, cpu_env, EA, oi);
2695             } else {
2696                 tcg_gen_movi_i32(oi, make_memop_idx(MO_BEQ, ctx->mem_idx));
2697                 gen_helper_lq_be_parallel(lo, cpu_env, EA, oi);
2698             }
2699             tcg_temp_free_i32(oi);
2700             tcg_gen_ld_i64(hi, cpu_env, offsetof(CPUPPCState, retxh));
2701         } else {
2702             /* Restart with exclusive lock.  */
2703             gen_helper_exit_atomic(cpu_env);
2704             ctx->base.is_jmp = DISAS_NORETURN;
2705         }
2706     } else if (ctx->le_mode) {
2707         tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_LEQ);
2708         gen_addr_add(ctx, EA, EA, 8);
2709         tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_LEQ);
2710     } else {
2711         tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_BEQ);
2712         gen_addr_add(ctx, EA, EA, 8);
2713         tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_BEQ);
2714     }
2715     tcg_temp_free(EA);
2716 }
2717 #endif
2718 
2719 /***                              Integer store                            ***/
2720 #define GEN_ST(name, stop, opc, type)                                         \
2721 static void glue(gen_, name)(DisasContext *ctx)                               \
2722 {                                                                             \
2723     TCGv EA;                                                                  \
2724     gen_set_access_type(ctx, ACCESS_INT);                                     \
2725     EA = tcg_temp_new();                                                      \
2726     gen_addr_imm_index(ctx, EA, 0);                                           \
2727     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2728     tcg_temp_free(EA);                                                        \
2729 }
2730 
2731 #define GEN_STU(name, stop, opc, type)                                        \
2732 static void glue(gen_, stop##u)(DisasContext *ctx)                            \
2733 {                                                                             \
2734     TCGv EA;                                                                  \
2735     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2736         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2737         return;                                                               \
2738     }                                                                         \
2739     gen_set_access_type(ctx, ACCESS_INT);                                     \
2740     EA = tcg_temp_new();                                                      \
2741     if (type == PPC_64B)                                                      \
2742         gen_addr_imm_index(ctx, EA, 0x03);                                    \
2743     else                                                                      \
2744         gen_addr_imm_index(ctx, EA, 0);                                       \
2745     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2746     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2747     tcg_temp_free(EA);                                                        \
2748 }
2749 
2750 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
2751 static void glue(gen_, name##ux)(DisasContext *ctx)                           \
2752 {                                                                             \
2753     TCGv EA;                                                                  \
2754     if (unlikely(rA(ctx->opcode) == 0)) {                                     \
2755         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);                   \
2756         return;                                                               \
2757     }                                                                         \
2758     gen_set_access_type(ctx, ACCESS_INT);                                     \
2759     EA = tcg_temp_new();                                                      \
2760     gen_addr_reg_index(ctx, EA);                                              \
2761     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2762     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);                             \
2763     tcg_temp_free(EA);                                                        \
2764 }
2765 
2766 #define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
2767 static void glue(gen_, name##x)(DisasContext *ctx)                            \
2768 {                                                                             \
2769     TCGv EA;                                                                  \
2770     chk;                                                                      \
2771     gen_set_access_type(ctx, ACCESS_INT);                                     \
2772     EA = tcg_temp_new();                                                      \
2773     gen_addr_reg_index(ctx, EA);                                              \
2774     gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA);                       \
2775     tcg_temp_free(EA);                                                        \
2776 }
2777 #define GEN_STX(name, stop, opc2, opc3, type)                                 \
2778     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_NONE)
2779 
2780 #define GEN_STX_HVRM(name, stop, opc2, opc3, type)                            \
2781     GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
2782 
2783 #define GEN_STS(name, stop, op, type)                                         \
2784 GEN_ST(name, stop, op | 0x20, type);                                          \
2785 GEN_STU(name, stop, op | 0x21, type);                                         \
2786 GEN_STUX(name, stop, 0x17, op | 0x01, type);                                  \
2787 GEN_STX(name, stop, 0x17, op | 0x00, type)
2788 
2789 /* stb stbu stbux stbx */
2790 GEN_STS(stb, st8, 0x06, PPC_INTEGER);
2791 /* sth sthu sthux sthx */
2792 GEN_STS(sth, st16, 0x0C, PPC_INTEGER);
2793 /* stw stwu stwux stwx */
2794 GEN_STS(stw, st32, 0x04, PPC_INTEGER);
2795 
2796 #define GEN_STEPX(name, stop, opc2, opc3)                                     \
2797 static void glue(gen_, name##epx)(DisasContext *ctx)                          \
2798 {                                                                             \
2799     TCGv EA;                                                                  \
2800     CHK_SV;                                                                   \
2801     gen_set_access_type(ctx, ACCESS_INT);                                     \
2802     EA = tcg_temp_new();                                                      \
2803     gen_addr_reg_index(ctx, EA);                                              \
2804     tcg_gen_qemu_st_tl(                                                       \
2805         cpu_gpr[rD(ctx->opcode)], EA, PPC_TLB_EPID_STORE, stop);              \
2806     tcg_temp_free(EA);                                                        \
2807 }
2808 
2809 GEN_STEPX(stb, DEF_MEMOP(MO_UB), 0x1F, 0x06)
2810 GEN_STEPX(sth, DEF_MEMOP(MO_UW), 0x1F, 0x0C)
2811 GEN_STEPX(stw, DEF_MEMOP(MO_UL), 0x1F, 0x04)
2812 #if defined(TARGET_PPC64)
2813 GEN_STEPX(std, DEF_MEMOP(MO_Q), 0x1d, 0x04)
2814 #endif
2815 
2816 #if defined(TARGET_PPC64)
2817 GEN_STUX(std, st64_i64, 0x15, 0x05, PPC_64B);
2818 GEN_STX(std, st64_i64, 0x15, 0x04, PPC_64B);
2819 GEN_STX_HVRM(stdcix, st64_i64, 0x15, 0x1f, PPC_CILDST)
2820 GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
2821 GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
2822 GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
2823 
2824 static void gen_std(DisasContext *ctx)
2825 {
2826     int rs;
2827     TCGv EA;
2828 
2829     rs = rS(ctx->opcode);
2830     if ((ctx->opcode & 0x3) == 0x2) { /* stq */
2831         bool legal_in_user_mode = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2832         bool le_is_supported = (ctx->insns_flags2 & PPC2_LSQ_ISA207) != 0;
2833         TCGv hi, lo;
2834 
2835         if (!(ctx->insns_flags & PPC_64BX)) {
2836             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2837         }
2838 
2839         if (!legal_in_user_mode && ctx->pr) {
2840             gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
2841             return;
2842         }
2843 
2844         if (!le_is_supported && ctx->le_mode) {
2845             gen_align_no_le(ctx);
2846             return;
2847         }
2848 
2849         if (unlikely(rs & 1)) {
2850             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2851             return;
2852         }
2853         gen_set_access_type(ctx, ACCESS_INT);
2854         EA = tcg_temp_new();
2855         gen_addr_imm_index(ctx, EA, 0x03);
2856 
2857         /* Note that the low part is always in RS+1, even in LE mode.  */
2858         lo = cpu_gpr[rs + 1];
2859         hi = cpu_gpr[rs];
2860 
2861         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
2862             if (HAVE_ATOMIC128) {
2863                 TCGv_i32 oi = tcg_temp_new_i32();
2864                 if (ctx->le_mode) {
2865                     tcg_gen_movi_i32(oi, make_memop_idx(MO_LEQ, ctx->mem_idx));
2866                     gen_helper_stq_le_parallel(cpu_env, EA, lo, hi, oi);
2867                 } else {
2868                     tcg_gen_movi_i32(oi, make_memop_idx(MO_BEQ, ctx->mem_idx));
2869                     gen_helper_stq_be_parallel(cpu_env, EA, lo, hi, oi);
2870                 }
2871                 tcg_temp_free_i32(oi);
2872             } else {
2873                 /* Restart with exclusive lock.  */
2874                 gen_helper_exit_atomic(cpu_env);
2875                 ctx->base.is_jmp = DISAS_NORETURN;
2876             }
2877         } else if (ctx->le_mode) {
2878             tcg_gen_qemu_st_i64(lo, EA, ctx->mem_idx, MO_LEQ);
2879             gen_addr_add(ctx, EA, EA, 8);
2880             tcg_gen_qemu_st_i64(hi, EA, ctx->mem_idx, MO_LEQ);
2881         } else {
2882             tcg_gen_qemu_st_i64(hi, EA, ctx->mem_idx, MO_BEQ);
2883             gen_addr_add(ctx, EA, EA, 8);
2884             tcg_gen_qemu_st_i64(lo, EA, ctx->mem_idx, MO_BEQ);
2885         }
2886         tcg_temp_free(EA);
2887     } else {
2888         /* std / stdu */
2889         if (Rc(ctx->opcode)) {
2890             if (unlikely(rA(ctx->opcode) == 0)) {
2891                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
2892                 return;
2893             }
2894         }
2895         gen_set_access_type(ctx, ACCESS_INT);
2896         EA = tcg_temp_new();
2897         gen_addr_imm_index(ctx, EA, 0x03);
2898         gen_qemu_st64_i64(ctx, cpu_gpr[rs], EA);
2899         if (Rc(ctx->opcode)) {
2900             tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], EA);
2901         }
2902         tcg_temp_free(EA);
2903     }
2904 }
2905 #endif
2906 /***                Integer load and store with byte reverse               ***/
2907 
2908 /* lhbrx */
2909 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
2910 
2911 /* lwbrx */
2912 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
2913 
2914 #if defined(TARGET_PPC64)
2915 /* ldbrx */
2916 GEN_LDX_E(ldbr, ld64ur_i64, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE);
2917 /* stdbrx */
2918 GEN_STX_E(stdbr, st64r_i64, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE);
2919 #endif  /* TARGET_PPC64 */
2920 
2921 /* sthbrx */
2922 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
2923 /* stwbrx */
2924 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
2925 
2926 /***                    Integer load and store multiple                    ***/
2927 
2928 /* lmw */
2929 static void gen_lmw(DisasContext *ctx)
2930 {
2931     TCGv t0;
2932     TCGv_i32 t1;
2933 
2934     if (ctx->le_mode) {
2935         gen_align_no_le(ctx);
2936         return;
2937     }
2938     gen_set_access_type(ctx, ACCESS_INT);
2939     t0 = tcg_temp_new();
2940     t1 = tcg_const_i32(rD(ctx->opcode));
2941     gen_addr_imm_index(ctx, t0, 0);
2942     gen_helper_lmw(cpu_env, t0, t1);
2943     tcg_temp_free(t0);
2944     tcg_temp_free_i32(t1);
2945 }
2946 
2947 /* stmw */
2948 static void gen_stmw(DisasContext *ctx)
2949 {
2950     TCGv t0;
2951     TCGv_i32 t1;
2952 
2953     if (ctx->le_mode) {
2954         gen_align_no_le(ctx);
2955         return;
2956     }
2957     gen_set_access_type(ctx, ACCESS_INT);
2958     t0 = tcg_temp_new();
2959     t1 = tcg_const_i32(rS(ctx->opcode));
2960     gen_addr_imm_index(ctx, t0, 0);
2961     gen_helper_stmw(cpu_env, t0, t1);
2962     tcg_temp_free(t0);
2963     tcg_temp_free_i32(t1);
2964 }
2965 
2966 /***                    Integer load and store strings                     ***/
2967 
2968 /* lswi */
2969 /*
2970  * PowerPC32 specification says we must generate an exception if rA is
2971  * in the range of registers to be loaded.  In an other hand, IBM says
2972  * this is valid, but rA won't be loaded.  For now, I'll follow the
2973  * spec...
2974  */
2975 static void gen_lswi(DisasContext *ctx)
2976 {
2977     TCGv t0;
2978     TCGv_i32 t1, t2;
2979     int nb = NB(ctx->opcode);
2980     int start = rD(ctx->opcode);
2981     int ra = rA(ctx->opcode);
2982     int nr;
2983 
2984     if (ctx->le_mode) {
2985         gen_align_no_le(ctx);
2986         return;
2987     }
2988     if (nb == 0) {
2989         nb = 32;
2990     }
2991     nr = DIV_ROUND_UP(nb, 4);
2992     if (unlikely(lsw_reg_in_range(start, nr, ra))) {
2993         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
2994         return;
2995     }
2996     gen_set_access_type(ctx, ACCESS_INT);
2997     t0 = tcg_temp_new();
2998     gen_addr_register(ctx, t0);
2999     t1 = tcg_const_i32(nb);
3000     t2 = tcg_const_i32(start);
3001     gen_helper_lsw(cpu_env, t0, t1, t2);
3002     tcg_temp_free(t0);
3003     tcg_temp_free_i32(t1);
3004     tcg_temp_free_i32(t2);
3005 }
3006 
3007 /* lswx */
3008 static void gen_lswx(DisasContext *ctx)
3009 {
3010     TCGv t0;
3011     TCGv_i32 t1, t2, t3;
3012 
3013     if (ctx->le_mode) {
3014         gen_align_no_le(ctx);
3015         return;
3016     }
3017     gen_set_access_type(ctx, ACCESS_INT);
3018     t0 = tcg_temp_new();
3019     gen_addr_reg_index(ctx, t0);
3020     t1 = tcg_const_i32(rD(ctx->opcode));
3021     t2 = tcg_const_i32(rA(ctx->opcode));
3022     t3 = tcg_const_i32(rB(ctx->opcode));
3023     gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3024     tcg_temp_free(t0);
3025     tcg_temp_free_i32(t1);
3026     tcg_temp_free_i32(t2);
3027     tcg_temp_free_i32(t3);
3028 }
3029 
3030 /* stswi */
3031 static void gen_stswi(DisasContext *ctx)
3032 {
3033     TCGv t0;
3034     TCGv_i32 t1, t2;
3035     int nb = NB(ctx->opcode);
3036 
3037     if (ctx->le_mode) {
3038         gen_align_no_le(ctx);
3039         return;
3040     }
3041     gen_set_access_type(ctx, ACCESS_INT);
3042     t0 = tcg_temp_new();
3043     gen_addr_register(ctx, t0);
3044     if (nb == 0) {
3045         nb = 32;
3046     }
3047     t1 = tcg_const_i32(nb);
3048     t2 = tcg_const_i32(rS(ctx->opcode));
3049     gen_helper_stsw(cpu_env, t0, t1, t2);
3050     tcg_temp_free(t0);
3051     tcg_temp_free_i32(t1);
3052     tcg_temp_free_i32(t2);
3053 }
3054 
3055 /* stswx */
3056 static void gen_stswx(DisasContext *ctx)
3057 {
3058     TCGv t0;
3059     TCGv_i32 t1, t2;
3060 
3061     if (ctx->le_mode) {
3062         gen_align_no_le(ctx);
3063         return;
3064     }
3065     gen_set_access_type(ctx, ACCESS_INT);
3066     t0 = tcg_temp_new();
3067     gen_addr_reg_index(ctx, t0);
3068     t1 = tcg_temp_new_i32();
3069     tcg_gen_trunc_tl_i32(t1, cpu_xer);
3070     tcg_gen_andi_i32(t1, t1, 0x7F);
3071     t2 = tcg_const_i32(rS(ctx->opcode));
3072     gen_helper_stsw(cpu_env, t0, t1, t2);
3073     tcg_temp_free(t0);
3074     tcg_temp_free_i32(t1);
3075     tcg_temp_free_i32(t2);
3076 }
3077 
3078 /***                        Memory synchronisation                         ***/
3079 /* eieio */
3080 static void gen_eieio(DisasContext *ctx)
3081 {
3082     TCGBar bar = TCG_MO_LD_ST;
3083 
3084     /*
3085      * POWER9 has a eieio instruction variant using bit 6 as a hint to
3086      * tell the CPU it is a store-forwarding barrier.
3087      */
3088     if (ctx->opcode & 0x2000000) {
3089         /*
3090          * ISA says that "Reserved fields in instructions are ignored
3091          * by the processor". So ignore the bit 6 on non-POWER9 CPU but
3092          * as this is not an instruction software should be using,
3093          * complain to the user.
3094          */
3095         if (!(ctx->insns_flags2 & PPC2_ISA300)) {
3096             qemu_log_mask(LOG_GUEST_ERROR, "invalid eieio using bit 6 at @"
3097                           TARGET_FMT_lx "\n", ctx->base.pc_next - 4);
3098         } else {
3099             bar = TCG_MO_ST_LD;
3100         }
3101     }
3102 
3103     tcg_gen_mb(bar | TCG_BAR_SC);
3104 }
3105 
3106 #if !defined(CONFIG_USER_ONLY)
3107 static inline void gen_check_tlb_flush(DisasContext *ctx, bool global)
3108 {
3109     TCGv_i32 t;
3110     TCGLabel *l;
3111 
3112     if (!ctx->lazy_tlb_flush) {
3113         return;
3114     }
3115     l = gen_new_label();
3116     t = tcg_temp_new_i32();
3117     tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
3118     tcg_gen_brcondi_i32(TCG_COND_EQ, t, 0, l);
3119     if (global) {
3120         gen_helper_check_tlb_flush_global(cpu_env);
3121     } else {
3122         gen_helper_check_tlb_flush_local(cpu_env);
3123     }
3124     gen_set_label(l);
3125     tcg_temp_free_i32(t);
3126 }
3127 #else
3128 static inline void gen_check_tlb_flush(DisasContext *ctx, bool global) { }
3129 #endif
3130 
3131 /* isync */
3132 static void gen_isync(DisasContext *ctx)
3133 {
3134     /*
3135      * We need to check for a pending TLB flush. This can only happen in
3136      * kernel mode however so check MSR_PR
3137      */
3138     if (!ctx->pr) {
3139         gen_check_tlb_flush(ctx, false);
3140     }
3141     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
3142     gen_stop_exception(ctx);
3143 }
3144 
3145 #define MEMOP_GET_SIZE(x)  (1 << ((x) & MO_SIZE))
3146 
3147 static void gen_load_locked(DisasContext *ctx, MemOp memop)
3148 {
3149     TCGv gpr = cpu_gpr[rD(ctx->opcode)];
3150     TCGv t0 = tcg_temp_new();
3151 
3152     gen_set_access_type(ctx, ACCESS_RES);
3153     gen_addr_reg_index(ctx, t0);
3154     tcg_gen_qemu_ld_tl(gpr, t0, ctx->mem_idx, memop | MO_ALIGN);
3155     tcg_gen_mov_tl(cpu_reserve, t0);
3156     tcg_gen_mov_tl(cpu_reserve_val, gpr);
3157     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3158     tcg_temp_free(t0);
3159 }
3160 
3161 #define LARX(name, memop)                  \
3162 static void gen_##name(DisasContext *ctx)  \
3163 {                                          \
3164     gen_load_locked(ctx, memop);           \
3165 }
3166 
3167 /* lwarx */
3168 LARX(lbarx, DEF_MEMOP(MO_UB))
3169 LARX(lharx, DEF_MEMOP(MO_UW))
3170 LARX(lwarx, DEF_MEMOP(MO_UL))
3171 
3172 static void gen_fetch_inc_conditional(DisasContext *ctx, MemOp memop,
3173                                       TCGv EA, TCGCond cond, int addend)
3174 {
3175     TCGv t = tcg_temp_new();
3176     TCGv t2 = tcg_temp_new();
3177     TCGv u = tcg_temp_new();
3178 
3179     tcg_gen_qemu_ld_tl(t, EA, ctx->mem_idx, memop);
3180     tcg_gen_addi_tl(t2, EA, MEMOP_GET_SIZE(memop));
3181     tcg_gen_qemu_ld_tl(t2, t2, ctx->mem_idx, memop);
3182     tcg_gen_addi_tl(u, t, addend);
3183 
3184     /* E.g. for fetch and increment bounded... */
3185     /* mem(EA,s) = (t != t2 ? u = t + 1 : t) */
3186     tcg_gen_movcond_tl(cond, u, t, t2, u, t);
3187     tcg_gen_qemu_st_tl(u, EA, ctx->mem_idx, memop);
3188 
3189     /* RT = (t != t2 ? t : u = 1<<(s*8-1)) */
3190     tcg_gen_movi_tl(u, 1 << (MEMOP_GET_SIZE(memop) * 8 - 1));
3191     tcg_gen_movcond_tl(cond, cpu_gpr[rD(ctx->opcode)], t, t2, t, u);
3192 
3193     tcg_temp_free(t);
3194     tcg_temp_free(t2);
3195     tcg_temp_free(u);
3196 }
3197 
3198 static void gen_ld_atomic(DisasContext *ctx, MemOp memop)
3199 {
3200     uint32_t gpr_FC = FC(ctx->opcode);
3201     TCGv EA = tcg_temp_new();
3202     int rt = rD(ctx->opcode);
3203     bool need_serial;
3204     TCGv src, dst;
3205 
3206     gen_addr_register(ctx, EA);
3207     dst = cpu_gpr[rt];
3208     src = cpu_gpr[(rt + 1) & 31];
3209 
3210     need_serial = false;
3211     memop |= MO_ALIGN;
3212     switch (gpr_FC) {
3213     case 0: /* Fetch and add */
3214         tcg_gen_atomic_fetch_add_tl(dst, EA, src, ctx->mem_idx, memop);
3215         break;
3216     case 1: /* Fetch and xor */
3217         tcg_gen_atomic_fetch_xor_tl(dst, EA, src, ctx->mem_idx, memop);
3218         break;
3219     case 2: /* Fetch and or */
3220         tcg_gen_atomic_fetch_or_tl(dst, EA, src, ctx->mem_idx, memop);
3221         break;
3222     case 3: /* Fetch and 'and' */
3223         tcg_gen_atomic_fetch_and_tl(dst, EA, src, ctx->mem_idx, memop);
3224         break;
3225     case 4:  /* Fetch and max unsigned */
3226         tcg_gen_atomic_fetch_umax_tl(dst, EA, src, ctx->mem_idx, memop);
3227         break;
3228     case 5:  /* Fetch and max signed */
3229         tcg_gen_atomic_fetch_smax_tl(dst, EA, src, ctx->mem_idx, memop);
3230         break;
3231     case 6:  /* Fetch and min unsigned */
3232         tcg_gen_atomic_fetch_umin_tl(dst, EA, src, ctx->mem_idx, memop);
3233         break;
3234     case 7:  /* Fetch and min signed */
3235         tcg_gen_atomic_fetch_smin_tl(dst, EA, src, ctx->mem_idx, memop);
3236         break;
3237     case 8: /* Swap */
3238         tcg_gen_atomic_xchg_tl(dst, EA, src, ctx->mem_idx, memop);
3239         break;
3240 
3241     case 16: /* Compare and swap not equal */
3242         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3243             need_serial = true;
3244         } else {
3245             TCGv t0 = tcg_temp_new();
3246             TCGv t1 = tcg_temp_new();
3247 
3248             tcg_gen_qemu_ld_tl(t0, EA, ctx->mem_idx, memop);
3249             if ((memop & MO_SIZE) == MO_64 || TARGET_LONG_BITS == 32) {
3250                 tcg_gen_mov_tl(t1, src);
3251             } else {
3252                 tcg_gen_ext32u_tl(t1, src);
3253             }
3254             tcg_gen_movcond_tl(TCG_COND_NE, t1, t0, t1,
3255                                cpu_gpr[(rt + 2) & 31], t0);
3256             tcg_gen_qemu_st_tl(t1, EA, ctx->mem_idx, memop);
3257             tcg_gen_mov_tl(dst, t0);
3258 
3259             tcg_temp_free(t0);
3260             tcg_temp_free(t1);
3261         }
3262         break;
3263 
3264     case 24: /* Fetch and increment bounded */
3265         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3266             need_serial = true;
3267         } else {
3268             gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_NE, 1);
3269         }
3270         break;
3271     case 25: /* Fetch and increment equal */
3272         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3273             need_serial = true;
3274         } else {
3275             gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_EQ, 1);
3276         }
3277         break;
3278     case 28: /* Fetch and decrement bounded */
3279         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3280             need_serial = true;
3281         } else {
3282             gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_NE, -1);
3283         }
3284         break;
3285 
3286     default:
3287         /* invoke data storage error handler */
3288         gen_exception_err(ctx, POWERPC_EXCP_DSI, POWERPC_EXCP_INVAL);
3289     }
3290     tcg_temp_free(EA);
3291 
3292     if (need_serial) {
3293         /* Restart with exclusive lock.  */
3294         gen_helper_exit_atomic(cpu_env);
3295         ctx->base.is_jmp = DISAS_NORETURN;
3296     }
3297 }
3298 
3299 static void gen_lwat(DisasContext *ctx)
3300 {
3301     gen_ld_atomic(ctx, DEF_MEMOP(MO_UL));
3302 }
3303 
3304 #ifdef TARGET_PPC64
3305 static void gen_ldat(DisasContext *ctx)
3306 {
3307     gen_ld_atomic(ctx, DEF_MEMOP(MO_Q));
3308 }
3309 #endif
3310 
3311 static void gen_st_atomic(DisasContext *ctx, MemOp memop)
3312 {
3313     uint32_t gpr_FC = FC(ctx->opcode);
3314     TCGv EA = tcg_temp_new();
3315     TCGv src, discard;
3316 
3317     gen_addr_register(ctx, EA);
3318     src = cpu_gpr[rD(ctx->opcode)];
3319     discard = tcg_temp_new();
3320 
3321     memop |= MO_ALIGN;
3322     switch (gpr_FC) {
3323     case 0: /* add and Store */
3324         tcg_gen_atomic_add_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3325         break;
3326     case 1: /* xor and Store */
3327         tcg_gen_atomic_xor_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3328         break;
3329     case 2: /* Or and Store */
3330         tcg_gen_atomic_or_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3331         break;
3332     case 3: /* 'and' and Store */
3333         tcg_gen_atomic_and_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3334         break;
3335     case 4:  /* Store max unsigned */
3336         tcg_gen_atomic_umax_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3337         break;
3338     case 5:  /* Store max signed */
3339         tcg_gen_atomic_smax_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3340         break;
3341     case 6:  /* Store min unsigned */
3342         tcg_gen_atomic_umin_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3343         break;
3344     case 7:  /* Store min signed */
3345         tcg_gen_atomic_smin_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3346         break;
3347     case 24: /* Store twin  */
3348         if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3349             /* Restart with exclusive lock.  */
3350             gen_helper_exit_atomic(cpu_env);
3351             ctx->base.is_jmp = DISAS_NORETURN;
3352         } else {
3353             TCGv t = tcg_temp_new();
3354             TCGv t2 = tcg_temp_new();
3355             TCGv s = tcg_temp_new();
3356             TCGv s2 = tcg_temp_new();
3357             TCGv ea_plus_s = tcg_temp_new();
3358 
3359             tcg_gen_qemu_ld_tl(t, EA, ctx->mem_idx, memop);
3360             tcg_gen_addi_tl(ea_plus_s, EA, MEMOP_GET_SIZE(memop));
3361             tcg_gen_qemu_ld_tl(t2, ea_plus_s, ctx->mem_idx, memop);
3362             tcg_gen_movcond_tl(TCG_COND_EQ, s, t, t2, src, t);
3363             tcg_gen_movcond_tl(TCG_COND_EQ, s2, t, t2, src, t2);
3364             tcg_gen_qemu_st_tl(s, EA, ctx->mem_idx, memop);
3365             tcg_gen_qemu_st_tl(s2, ea_plus_s, ctx->mem_idx, memop);
3366 
3367             tcg_temp_free(ea_plus_s);
3368             tcg_temp_free(s2);
3369             tcg_temp_free(s);
3370             tcg_temp_free(t2);
3371             tcg_temp_free(t);
3372         }
3373         break;
3374     default:
3375         /* invoke data storage error handler */
3376         gen_exception_err(ctx, POWERPC_EXCP_DSI, POWERPC_EXCP_INVAL);
3377     }
3378     tcg_temp_free(discard);
3379     tcg_temp_free(EA);
3380 }
3381 
3382 static void gen_stwat(DisasContext *ctx)
3383 {
3384     gen_st_atomic(ctx, DEF_MEMOP(MO_UL));
3385 }
3386 
3387 #ifdef TARGET_PPC64
3388 static void gen_stdat(DisasContext *ctx)
3389 {
3390     gen_st_atomic(ctx, DEF_MEMOP(MO_Q));
3391 }
3392 #endif
3393 
3394 static void gen_conditional_store(DisasContext *ctx, MemOp memop)
3395 {
3396     TCGLabel *l1 = gen_new_label();
3397     TCGLabel *l2 = gen_new_label();
3398     TCGv t0 = tcg_temp_new();
3399     int reg = rS(ctx->opcode);
3400 
3401     gen_set_access_type(ctx, ACCESS_RES);
3402     gen_addr_reg_index(ctx, t0);
3403     tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3404     tcg_temp_free(t0);
3405 
3406     t0 = tcg_temp_new();
3407     tcg_gen_atomic_cmpxchg_tl(t0, cpu_reserve, cpu_reserve_val,
3408                               cpu_gpr[reg], ctx->mem_idx,
3409                               DEF_MEMOP(memop) | MO_ALIGN);
3410     tcg_gen_setcond_tl(TCG_COND_EQ, t0, t0, cpu_reserve_val);
3411     tcg_gen_shli_tl(t0, t0, CRF_EQ_BIT);
3412     tcg_gen_or_tl(t0, t0, cpu_so);
3413     tcg_gen_trunc_tl_i32(cpu_crf[0], t0);
3414     tcg_temp_free(t0);
3415     tcg_gen_br(l2);
3416 
3417     gen_set_label(l1);
3418 
3419     /*
3420      * Address mismatch implies failure.  But we still need to provide
3421      * the memory barrier semantics of the instruction.
3422      */
3423     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3424     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3425 
3426     gen_set_label(l2);
3427     tcg_gen_movi_tl(cpu_reserve, -1);
3428 }
3429 
3430 #define STCX(name, memop)                  \
3431 static void gen_##name(DisasContext *ctx)  \
3432 {                                          \
3433     gen_conditional_store(ctx, memop);     \
3434 }
3435 
3436 STCX(stbcx_, DEF_MEMOP(MO_UB))
3437 STCX(sthcx_, DEF_MEMOP(MO_UW))
3438 STCX(stwcx_, DEF_MEMOP(MO_UL))
3439 
3440 #if defined(TARGET_PPC64)
3441 /* ldarx */
3442 LARX(ldarx, DEF_MEMOP(MO_Q))
3443 /* stdcx. */
3444 STCX(stdcx_, DEF_MEMOP(MO_Q))
3445 
3446 /* lqarx */
3447 static void gen_lqarx(DisasContext *ctx)
3448 {
3449     int rd = rD(ctx->opcode);
3450     TCGv EA, hi, lo;
3451 
3452     if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3453                  (rd == rB(ctx->opcode)))) {
3454         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3455         return;
3456     }
3457 
3458     gen_set_access_type(ctx, ACCESS_RES);
3459     EA = tcg_temp_new();
3460     gen_addr_reg_index(ctx, EA);
3461 
3462     /* Note that the low part is always in RD+1, even in LE mode.  */
3463     lo = cpu_gpr[rd + 1];
3464     hi = cpu_gpr[rd];
3465 
3466     if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3467         if (HAVE_ATOMIC128) {
3468             TCGv_i32 oi = tcg_temp_new_i32();
3469             if (ctx->le_mode) {
3470                 tcg_gen_movi_i32(oi, make_memop_idx(MO_LEQ | MO_ALIGN_16,
3471                                                     ctx->mem_idx));
3472                 gen_helper_lq_le_parallel(lo, cpu_env, EA, oi);
3473             } else {
3474                 tcg_gen_movi_i32(oi, make_memop_idx(MO_BEQ | MO_ALIGN_16,
3475                                                     ctx->mem_idx));
3476                 gen_helper_lq_be_parallel(lo, cpu_env, EA, oi);
3477             }
3478             tcg_temp_free_i32(oi);
3479             tcg_gen_ld_i64(hi, cpu_env, offsetof(CPUPPCState, retxh));
3480         } else {
3481             /* Restart with exclusive lock.  */
3482             gen_helper_exit_atomic(cpu_env);
3483             ctx->base.is_jmp = DISAS_NORETURN;
3484             tcg_temp_free(EA);
3485             return;
3486         }
3487     } else if (ctx->le_mode) {
3488         tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_LEQ | MO_ALIGN_16);
3489         tcg_gen_mov_tl(cpu_reserve, EA);
3490         gen_addr_add(ctx, EA, EA, 8);
3491         tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_LEQ);
3492     } else {
3493         tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_BEQ | MO_ALIGN_16);
3494         tcg_gen_mov_tl(cpu_reserve, EA);
3495         gen_addr_add(ctx, EA, EA, 8);
3496         tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_BEQ);
3497     }
3498     tcg_temp_free(EA);
3499 
3500     tcg_gen_st_tl(hi, cpu_env, offsetof(CPUPPCState, reserve_val));
3501     tcg_gen_st_tl(lo, cpu_env, offsetof(CPUPPCState, reserve_val2));
3502 }
3503 
3504 /* stqcx. */
3505 static void gen_stqcx_(DisasContext *ctx)
3506 {
3507     int rs = rS(ctx->opcode);
3508     TCGv EA, hi, lo;
3509 
3510     if (unlikely(rs & 1)) {
3511         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3512         return;
3513     }
3514 
3515     gen_set_access_type(ctx, ACCESS_RES);
3516     EA = tcg_temp_new();
3517     gen_addr_reg_index(ctx, EA);
3518 
3519     /* Note that the low part is always in RS+1, even in LE mode.  */
3520     lo = cpu_gpr[rs + 1];
3521     hi = cpu_gpr[rs];
3522 
3523     if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3524         if (HAVE_CMPXCHG128) {
3525             TCGv_i32 oi = tcg_const_i32(DEF_MEMOP(MO_Q) | MO_ALIGN_16);
3526             if (ctx->le_mode) {
3527                 gen_helper_stqcx_le_parallel(cpu_crf[0], cpu_env,
3528                                              EA, lo, hi, oi);
3529             } else {
3530                 gen_helper_stqcx_be_parallel(cpu_crf[0], cpu_env,
3531                                              EA, lo, hi, oi);
3532             }
3533             tcg_temp_free_i32(oi);
3534         } else {
3535             /* Restart with exclusive lock.  */
3536             gen_helper_exit_atomic(cpu_env);
3537             ctx->base.is_jmp = DISAS_NORETURN;
3538         }
3539         tcg_temp_free(EA);
3540     } else {
3541         TCGLabel *lab_fail = gen_new_label();
3542         TCGLabel *lab_over = gen_new_label();
3543         TCGv_i64 t0 = tcg_temp_new_i64();
3544         TCGv_i64 t1 = tcg_temp_new_i64();
3545 
3546         tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, lab_fail);
3547         tcg_temp_free(EA);
3548 
3549         gen_qemu_ld64_i64(ctx, t0, cpu_reserve);
3550         tcg_gen_ld_i64(t1, cpu_env, (ctx->le_mode
3551                                      ? offsetof(CPUPPCState, reserve_val2)
3552                                      : offsetof(CPUPPCState, reserve_val)));
3553         tcg_gen_brcond_i64(TCG_COND_NE, t0, t1, lab_fail);
3554 
3555         tcg_gen_addi_i64(t0, cpu_reserve, 8);
3556         gen_qemu_ld64_i64(ctx, t0, t0);
3557         tcg_gen_ld_i64(t1, cpu_env, (ctx->le_mode
3558                                      ? offsetof(CPUPPCState, reserve_val)
3559                                      : offsetof(CPUPPCState, reserve_val2)));
3560         tcg_gen_brcond_i64(TCG_COND_NE, t0, t1, lab_fail);
3561 
3562         /* Success */
3563         gen_qemu_st64_i64(ctx, ctx->le_mode ? lo : hi, cpu_reserve);
3564         tcg_gen_addi_i64(t0, cpu_reserve, 8);
3565         gen_qemu_st64_i64(ctx, ctx->le_mode ? hi : lo, t0);
3566 
3567         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3568         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], CRF_EQ);
3569         tcg_gen_br(lab_over);
3570 
3571         gen_set_label(lab_fail);
3572         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3573 
3574         gen_set_label(lab_over);
3575         tcg_gen_movi_tl(cpu_reserve, -1);
3576         tcg_temp_free_i64(t0);
3577         tcg_temp_free_i64(t1);
3578     }
3579 }
3580 #endif /* defined(TARGET_PPC64) */
3581 
3582 /* sync */
3583 static void gen_sync(DisasContext *ctx)
3584 {
3585     uint32_t l = (ctx->opcode >> 21) & 3;
3586 
3587     /*
3588      * We may need to check for a pending TLB flush.
3589      *
3590      * We do this on ptesync (l == 2) on ppc64 and any sync pn ppc32.
3591      *
3592      * Additionally, this can only happen in kernel mode however so
3593      * check MSR_PR as well.
3594      */
3595     if (((l == 2) || !(ctx->insns_flags & PPC_64B)) && !ctx->pr) {
3596         gen_check_tlb_flush(ctx, true);
3597     }
3598     tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
3599 }
3600 
3601 /* wait */
3602 static void gen_wait(DisasContext *ctx)
3603 {
3604     TCGv_i32 t0 = tcg_const_i32(1);
3605     tcg_gen_st_i32(t0, cpu_env,
3606                    -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
3607     tcg_temp_free_i32(t0);
3608     /* Stop translation, as the CPU is supposed to sleep from now */
3609     gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3610 }
3611 
3612 #if defined(TARGET_PPC64)
3613 static void gen_doze(DisasContext *ctx)
3614 {
3615 #if defined(CONFIG_USER_ONLY)
3616     GEN_PRIV;
3617 #else
3618     TCGv_i32 t;
3619 
3620     CHK_HV;
3621     t = tcg_const_i32(PPC_PM_DOZE);
3622     gen_helper_pminsn(cpu_env, t);
3623     tcg_temp_free_i32(t);
3624     /* Stop translation, as the CPU is supposed to sleep from now */
3625     gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3626 #endif /* defined(CONFIG_USER_ONLY) */
3627 }
3628 
3629 static void gen_nap(DisasContext *ctx)
3630 {
3631 #if defined(CONFIG_USER_ONLY)
3632     GEN_PRIV;
3633 #else
3634     TCGv_i32 t;
3635 
3636     CHK_HV;
3637     t = tcg_const_i32(PPC_PM_NAP);
3638     gen_helper_pminsn(cpu_env, t);
3639     tcg_temp_free_i32(t);
3640     /* Stop translation, as the CPU is supposed to sleep from now */
3641     gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3642 #endif /* defined(CONFIG_USER_ONLY) */
3643 }
3644 
3645 static void gen_stop(DisasContext *ctx)
3646 {
3647 #if defined(CONFIG_USER_ONLY)
3648     GEN_PRIV;
3649 #else
3650     TCGv_i32 t;
3651 
3652     CHK_HV;
3653     t = tcg_const_i32(PPC_PM_STOP);
3654     gen_helper_pminsn(cpu_env, t);
3655     tcg_temp_free_i32(t);
3656     /* Stop translation, as the CPU is supposed to sleep from now */
3657     gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3658 #endif /* defined(CONFIG_USER_ONLY) */
3659 }
3660 
3661 static void gen_sleep(DisasContext *ctx)
3662 {
3663 #if defined(CONFIG_USER_ONLY)
3664     GEN_PRIV;
3665 #else
3666     TCGv_i32 t;
3667 
3668     CHK_HV;
3669     t = tcg_const_i32(PPC_PM_SLEEP);
3670     gen_helper_pminsn(cpu_env, t);
3671     tcg_temp_free_i32(t);
3672     /* Stop translation, as the CPU is supposed to sleep from now */
3673     gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3674 #endif /* defined(CONFIG_USER_ONLY) */
3675 }
3676 
3677 static void gen_rvwinkle(DisasContext *ctx)
3678 {
3679 #if defined(CONFIG_USER_ONLY)
3680     GEN_PRIV;
3681 #else
3682     TCGv_i32 t;
3683 
3684     CHK_HV;
3685     t = tcg_const_i32(PPC_PM_RVWINKLE);
3686     gen_helper_pminsn(cpu_env, t);
3687     tcg_temp_free_i32(t);
3688     /* Stop translation, as the CPU is supposed to sleep from now */
3689     gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
3690 #endif /* defined(CONFIG_USER_ONLY) */
3691 }
3692 #endif /* #if defined(TARGET_PPC64) */
3693 
3694 static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
3695 {
3696 #if defined(TARGET_PPC64)
3697     if (ctx->has_cfar) {
3698         tcg_gen_movi_tl(cpu_cfar, nip);
3699     }
3700 #endif
3701 }
3702 
3703 static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
3704 {
3705     if (unlikely(ctx->singlestep_enabled)) {
3706         return false;
3707     }
3708 
3709 #ifndef CONFIG_USER_ONLY
3710     return (ctx->base.tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
3711 #else
3712     return true;
3713 #endif
3714 }
3715 
3716 static void gen_lookup_and_goto_ptr(DisasContext *ctx)
3717 {
3718     int sse = ctx->singlestep_enabled;
3719     if (unlikely(sse)) {
3720         if (sse & GDBSTUB_SINGLE_STEP) {
3721             gen_debug_exception(ctx);
3722         } else if (sse & (CPU_SINGLE_STEP | CPU_BRANCH_STEP)) {
3723             uint32_t excp = gen_prep_dbgex(ctx);
3724             gen_exception(ctx, excp);
3725         }
3726         tcg_gen_exit_tb(NULL, 0);
3727     } else {
3728         tcg_gen_lookup_and_goto_ptr();
3729     }
3730 }
3731 
3732 /***                                Branch                                 ***/
3733 static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
3734 {
3735     if (NARROW_MODE(ctx)) {
3736         dest = (uint32_t) dest;
3737     }
3738     if (use_goto_tb(ctx, dest)) {
3739         tcg_gen_goto_tb(n);
3740         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3741         tcg_gen_exit_tb(ctx->base.tb, n);
3742     } else {
3743         tcg_gen_movi_tl(cpu_nip, dest & ~3);
3744         gen_lookup_and_goto_ptr(ctx);
3745     }
3746 }
3747 
3748 static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
3749 {
3750     if (NARROW_MODE(ctx)) {
3751         nip = (uint32_t)nip;
3752     }
3753     tcg_gen_movi_tl(cpu_lr, nip);
3754 }
3755 
3756 /* b ba bl bla */
3757 static void gen_b(DisasContext *ctx)
3758 {
3759     target_ulong li, target;
3760 
3761     ctx->exception = POWERPC_EXCP_BRANCH;
3762     /* sign extend LI */
3763     li = LI(ctx->opcode);
3764     li = (li ^ 0x02000000) - 0x02000000;
3765     if (likely(AA(ctx->opcode) == 0)) {
3766         target = ctx->base.pc_next + li - 4;
3767     } else {
3768         target = li;
3769     }
3770     if (LK(ctx->opcode)) {
3771         gen_setlr(ctx, ctx->base.pc_next);
3772     }
3773     gen_update_cfar(ctx, ctx->base.pc_next - 4);
3774     gen_goto_tb(ctx, 0, target);
3775 }
3776 
3777 #define BCOND_IM  0
3778 #define BCOND_LR  1
3779 #define BCOND_CTR 2
3780 #define BCOND_TAR 3
3781 
3782 static void gen_bcond(DisasContext *ctx, int type)
3783 {
3784     uint32_t bo = BO(ctx->opcode);
3785     TCGLabel *l1;
3786     TCGv target;
3787     ctx->exception = POWERPC_EXCP_BRANCH;
3788 
3789     if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
3790         target = tcg_temp_local_new();
3791         if (type == BCOND_CTR) {
3792             tcg_gen_mov_tl(target, cpu_ctr);
3793         } else if (type == BCOND_TAR) {
3794             gen_load_spr(target, SPR_TAR);
3795         } else {
3796             tcg_gen_mov_tl(target, cpu_lr);
3797         }
3798     } else {
3799         target = NULL;
3800     }
3801     if (LK(ctx->opcode)) {
3802         gen_setlr(ctx, ctx->base.pc_next);
3803     }
3804     l1 = gen_new_label();
3805     if ((bo & 0x4) == 0) {
3806         /* Decrement and test CTR */
3807         TCGv temp = tcg_temp_new();
3808 
3809         if (type == BCOND_CTR) {
3810             /*
3811              * All ISAs up to v3 describe this form of bcctr as invalid but
3812              * some processors, ie. 64-bit server processors compliant with
3813              * arch 2.x, do implement a "test and decrement" logic instead,
3814              * as described in their respective UMs. This logic involves CTR
3815              * to act as both the branch target and a counter, which makes
3816              * it basically useless and thus never used in real code.
3817              *
3818              * This form was hence chosen to trigger extra micro-architectural
3819              * side-effect on real HW needed for the Spectre v2 workaround.
3820              * It is up to guests that implement such workaround, ie. linux, to
3821              * use this form in a way it just triggers the side-effect without
3822              * doing anything else harmful.
3823              */
3824             if (unlikely(!is_book3s_arch2x(ctx))) {
3825                 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3826                 tcg_temp_free(temp);
3827                 tcg_temp_free(target);
3828                 return;
3829             }
3830 
3831             if (NARROW_MODE(ctx)) {
3832                 tcg_gen_ext32u_tl(temp, cpu_ctr);
3833             } else {
3834                 tcg_gen_mov_tl(temp, cpu_ctr);
3835             }
3836             if (bo & 0x2) {
3837                 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3838             } else {
3839                 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3840             }
3841             tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3842         } else {
3843             tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
3844             if (NARROW_MODE(ctx)) {
3845                 tcg_gen_ext32u_tl(temp, cpu_ctr);
3846             } else {
3847                 tcg_gen_mov_tl(temp, cpu_ctr);
3848             }
3849             if (bo & 0x2) {
3850                 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
3851             } else {
3852                 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
3853             }
3854         }
3855         tcg_temp_free(temp);
3856     }
3857     if ((bo & 0x10) == 0) {
3858         /* Test CR */
3859         uint32_t bi = BI(ctx->opcode);
3860         uint32_t mask = 0x08 >> (bi & 0x03);
3861         TCGv_i32 temp = tcg_temp_new_i32();
3862 
3863         if (bo & 0x8) {
3864             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3865             tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
3866         } else {
3867             tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
3868             tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
3869         }
3870         tcg_temp_free_i32(temp);
3871     }
3872     gen_update_cfar(ctx, ctx->base.pc_next - 4);
3873     if (type == BCOND_IM) {
3874         target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
3875         if (likely(AA(ctx->opcode) == 0)) {
3876             gen_goto_tb(ctx, 0, ctx->base.pc_next + li - 4);
3877         } else {
3878             gen_goto_tb(ctx, 0, li);
3879         }
3880     } else {
3881         if (NARROW_MODE(ctx)) {
3882             tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
3883         } else {
3884             tcg_gen_andi_tl(cpu_nip, target, ~3);
3885         }
3886         gen_lookup_and_goto_ptr(ctx);
3887         tcg_temp_free(target);
3888     }
3889     if ((bo & 0x14) != 0x14) {
3890         /* fallthrough case */
3891         gen_set_label(l1);
3892         gen_goto_tb(ctx, 1, ctx->base.pc_next);
3893     }
3894 }
3895 
3896 static void gen_bc(DisasContext *ctx)
3897 {
3898     gen_bcond(ctx, BCOND_IM);
3899 }
3900 
3901 static void gen_bcctr(DisasContext *ctx)
3902 {
3903     gen_bcond(ctx, BCOND_CTR);
3904 }
3905 
3906 static void gen_bclr(DisasContext *ctx)
3907 {
3908     gen_bcond(ctx, BCOND_LR);
3909 }
3910 
3911 static void gen_bctar(DisasContext *ctx)
3912 {
3913     gen_bcond(ctx, BCOND_TAR);
3914 }
3915 
3916 /***                      Condition register logical                       ***/
3917 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
3918 static void glue(gen_, name)(DisasContext *ctx)                               \
3919 {                                                                             \
3920     uint8_t bitmask;                                                          \
3921     int sh;                                                                   \
3922     TCGv_i32 t0, t1;                                                          \
3923     sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03);             \
3924     t0 = tcg_temp_new_i32();                                                  \
3925     if (sh > 0)                                                               \
3926         tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh);            \
3927     else if (sh < 0)                                                          \
3928         tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh);           \
3929     else                                                                      \
3930         tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]);                 \
3931     t1 = tcg_temp_new_i32();                                                  \
3932     sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03);             \
3933     if (sh > 0)                                                               \
3934         tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh);            \
3935     else if (sh < 0)                                                          \
3936         tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh);           \
3937     else                                                                      \
3938         tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]);                 \
3939     tcg_op(t0, t0, t1);                                                       \
3940     bitmask = 0x08 >> (crbD(ctx->opcode) & 0x03);                             \
3941     tcg_gen_andi_i32(t0, t0, bitmask);                                        \
3942     tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask);          \
3943     tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1);                  \
3944     tcg_temp_free_i32(t0);                                                    \
3945     tcg_temp_free_i32(t1);                                                    \
3946 }
3947 
3948 /* crand */
3949 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
3950 /* crandc */
3951 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
3952 /* creqv */
3953 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
3954 /* crnand */
3955 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
3956 /* crnor */
3957 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
3958 /* cror */
3959 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
3960 /* crorc */
3961 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
3962 /* crxor */
3963 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
3964 
3965 /* mcrf */
3966 static void gen_mcrf(DisasContext *ctx)
3967 {
3968     tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
3969 }
3970 
3971 /***                           System linkage                              ***/
3972 
3973 /* rfi (supervisor only) */
3974 static void gen_rfi(DisasContext *ctx)
3975 {
3976 #if defined(CONFIG_USER_ONLY)
3977     GEN_PRIV;
3978 #else
3979     /*
3980      * This instruction doesn't exist anymore on 64-bit server
3981      * processors compliant with arch 2.x
3982      */
3983     if (is_book3s_arch2x(ctx)) {
3984         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3985         return;
3986     }
3987     /* Restore CPU state */
3988     CHK_SV;
3989     if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
3990         gen_io_start();
3991     }
3992     gen_update_cfar(ctx, ctx->base.pc_next - 4);
3993     gen_helper_rfi(cpu_env);
3994     gen_sync_exception(ctx);
3995 #endif
3996 }
3997 
3998 #if defined(TARGET_PPC64)
3999 static void gen_rfid(DisasContext *ctx)
4000 {
4001 #if defined(CONFIG_USER_ONLY)
4002     GEN_PRIV;
4003 #else
4004     /* Restore CPU state */
4005     CHK_SV;
4006     if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
4007         gen_io_start();
4008     }
4009     gen_update_cfar(ctx, ctx->base.pc_next - 4);
4010     gen_helper_rfid(cpu_env);
4011     gen_sync_exception(ctx);
4012 #endif
4013 }
4014 
4015 static void gen_hrfid(DisasContext *ctx)
4016 {
4017 #if defined(CONFIG_USER_ONLY)
4018     GEN_PRIV;
4019 #else
4020     /* Restore CPU state */
4021     CHK_HV;
4022     gen_helper_hrfid(cpu_env);
4023     gen_sync_exception(ctx);
4024 #endif
4025 }
4026 #endif
4027 
4028 /* sc */
4029 #if defined(CONFIG_USER_ONLY)
4030 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4031 #else
4032 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4033 #endif
4034 static void gen_sc(DisasContext *ctx)
4035 {
4036     uint32_t lev;
4037 
4038     lev = (ctx->opcode >> 5) & 0x7F;
4039     gen_exception_err(ctx, POWERPC_SYSCALL, lev);
4040 }
4041 
4042 /***                                Trap                                   ***/
4043 
4044 /* Check for unconditional traps (always or never) */
4045 static bool check_unconditional_trap(DisasContext *ctx)
4046 {
4047     /* Trap never */
4048     if (TO(ctx->opcode) == 0) {
4049         return true;
4050     }
4051     /* Trap always */
4052     if (TO(ctx->opcode) == 31) {
4053         gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
4054         return true;
4055     }
4056     return false;
4057 }
4058 
4059 /* tw */
4060 static void gen_tw(DisasContext *ctx)
4061 {
4062     TCGv_i32 t0;
4063 
4064     if (check_unconditional_trap(ctx)) {
4065         return;
4066     }
4067     t0 = tcg_const_i32(TO(ctx->opcode));
4068     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4069                   t0);
4070     tcg_temp_free_i32(t0);
4071 }
4072 
4073 /* twi */
4074 static void gen_twi(DisasContext *ctx)
4075 {
4076     TCGv t0;
4077     TCGv_i32 t1;
4078 
4079     if (check_unconditional_trap(ctx)) {
4080         return;
4081     }
4082     t0 = tcg_const_tl(SIMM(ctx->opcode));
4083     t1 = tcg_const_i32(TO(ctx->opcode));
4084     gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4085     tcg_temp_free(t0);
4086     tcg_temp_free_i32(t1);
4087 }
4088 
4089 #if defined(TARGET_PPC64)
4090 /* td */
4091 static void gen_td(DisasContext *ctx)
4092 {
4093     TCGv_i32 t0;
4094 
4095     if (check_unconditional_trap(ctx)) {
4096         return;
4097     }
4098     t0 = tcg_const_i32(TO(ctx->opcode));
4099     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4100                   t0);
4101     tcg_temp_free_i32(t0);
4102 }
4103 
4104 /* tdi */
4105 static void gen_tdi(DisasContext *ctx)
4106 {
4107     TCGv t0;
4108     TCGv_i32 t1;
4109 
4110     if (check_unconditional_trap(ctx)) {
4111         return;
4112     }
4113     t0 = tcg_const_tl(SIMM(ctx->opcode));
4114     t1 = tcg_const_i32(TO(ctx->opcode));
4115     gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4116     tcg_temp_free(t0);
4117     tcg_temp_free_i32(t1);
4118 }
4119 #endif
4120 
4121 /***                          Processor control                            ***/
4122 
4123 static void gen_read_xer(DisasContext *ctx, TCGv dst)
4124 {
4125     TCGv t0 = tcg_temp_new();
4126     TCGv t1 = tcg_temp_new();
4127     TCGv t2 = tcg_temp_new();
4128     tcg_gen_mov_tl(dst, cpu_xer);
4129     tcg_gen_shli_tl(t0, cpu_so, XER_SO);
4130     tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
4131     tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
4132     tcg_gen_or_tl(t0, t0, t1);
4133     tcg_gen_or_tl(dst, dst, t2);
4134     tcg_gen_or_tl(dst, dst, t0);
4135     if (is_isa300(ctx)) {
4136         tcg_gen_shli_tl(t0, cpu_ov32, XER_OV32);
4137         tcg_gen_or_tl(dst, dst, t0);
4138         tcg_gen_shli_tl(t0, cpu_ca32, XER_CA32);
4139         tcg_gen_or_tl(dst, dst, t0);
4140     }
4141     tcg_temp_free(t0);
4142     tcg_temp_free(t1);
4143     tcg_temp_free(t2);
4144 }
4145 
4146 static void gen_write_xer(TCGv src)
4147 {
4148     /* Write all flags, while reading back check for isa300 */
4149     tcg_gen_andi_tl(cpu_xer, src,
4150                     ~((1u << XER_SO) |
4151                       (1u << XER_OV) | (1u << XER_OV32) |
4152                       (1u << XER_CA) | (1u << XER_CA32)));
4153     tcg_gen_extract_tl(cpu_ov32, src, XER_OV32, 1);
4154     tcg_gen_extract_tl(cpu_ca32, src, XER_CA32, 1);
4155     tcg_gen_extract_tl(cpu_so, src, XER_SO, 1);
4156     tcg_gen_extract_tl(cpu_ov, src, XER_OV, 1);
4157     tcg_gen_extract_tl(cpu_ca, src, XER_CA, 1);
4158 }
4159 
4160 /* mcrxr */
4161 static void gen_mcrxr(DisasContext *ctx)
4162 {
4163     TCGv_i32 t0 = tcg_temp_new_i32();
4164     TCGv_i32 t1 = tcg_temp_new_i32();
4165     TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4166 
4167     tcg_gen_trunc_tl_i32(t0, cpu_so);
4168     tcg_gen_trunc_tl_i32(t1, cpu_ov);
4169     tcg_gen_trunc_tl_i32(dst, cpu_ca);
4170     tcg_gen_shli_i32(t0, t0, 3);
4171     tcg_gen_shli_i32(t1, t1, 2);
4172     tcg_gen_shli_i32(dst, dst, 1);
4173     tcg_gen_or_i32(dst, dst, t0);
4174     tcg_gen_or_i32(dst, dst, t1);
4175     tcg_temp_free_i32(t0);
4176     tcg_temp_free_i32(t1);
4177 
4178     tcg_gen_movi_tl(cpu_so, 0);
4179     tcg_gen_movi_tl(cpu_ov, 0);
4180     tcg_gen_movi_tl(cpu_ca, 0);
4181 }
4182 
4183 #ifdef TARGET_PPC64
4184 /* mcrxrx */
4185 static void gen_mcrxrx(DisasContext *ctx)
4186 {
4187     TCGv t0 = tcg_temp_new();
4188     TCGv t1 = tcg_temp_new();
4189     TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4190 
4191     /* copy OV and OV32 */
4192     tcg_gen_shli_tl(t0, cpu_ov, 1);
4193     tcg_gen_or_tl(t0, t0, cpu_ov32);
4194     tcg_gen_shli_tl(t0, t0, 2);
4195     /* copy CA and CA32 */
4196     tcg_gen_shli_tl(t1, cpu_ca, 1);
4197     tcg_gen_or_tl(t1, t1, cpu_ca32);
4198     tcg_gen_or_tl(t0, t0, t1);
4199     tcg_gen_trunc_tl_i32(dst, t0);
4200     tcg_temp_free(t0);
4201     tcg_temp_free(t1);
4202 }
4203 #endif
4204 
4205 /* mfcr mfocrf */
4206 static void gen_mfcr(DisasContext *ctx)
4207 {
4208     uint32_t crm, crn;
4209 
4210     if (likely(ctx->opcode & 0x00100000)) {
4211         crm = CRM(ctx->opcode);
4212         if (likely(crm && ((crm & (crm - 1)) == 0))) {
4213             crn = ctz32(crm);
4214             tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
4215             tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4216                             cpu_gpr[rD(ctx->opcode)], crn * 4);
4217         }
4218     } else {
4219         TCGv_i32 t0 = tcg_temp_new_i32();
4220         tcg_gen_mov_i32(t0, cpu_crf[0]);
4221         tcg_gen_shli_i32(t0, t0, 4);
4222         tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4223         tcg_gen_shli_i32(t0, t0, 4);
4224         tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4225         tcg_gen_shli_i32(t0, t0, 4);
4226         tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4227         tcg_gen_shli_i32(t0, t0, 4);
4228         tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4229         tcg_gen_shli_i32(t0, t0, 4);
4230         tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4231         tcg_gen_shli_i32(t0, t0, 4);
4232         tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4233         tcg_gen_shli_i32(t0, t0, 4);
4234         tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4235         tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4236         tcg_temp_free_i32(t0);
4237     }
4238 }
4239 
4240 /* mfmsr */
4241 static void gen_mfmsr(DisasContext *ctx)
4242 {
4243     CHK_SV;
4244     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4245 }
4246 
4247 static void spr_noaccess(DisasContext *ctx, int gprn, int sprn)
4248 {
4249 #if 0
4250     sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
4251     printf("ERROR: try to access SPR %d !\n", sprn);
4252 #endif
4253 }
4254 #define SPR_NOACCESS (&spr_noaccess)
4255 
4256 /* mfspr */
4257 static inline void gen_op_mfspr(DisasContext *ctx)
4258 {
4259     void (*read_cb)(DisasContext *ctx, int gprn, int sprn);
4260     uint32_t sprn = SPR(ctx->opcode);
4261 
4262 #if defined(CONFIG_USER_ONLY)
4263     read_cb = ctx->spr_cb[sprn].uea_read;
4264 #else
4265     if (ctx->pr) {
4266         read_cb = ctx->spr_cb[sprn].uea_read;
4267     } else if (ctx->hv) {
4268         read_cb = ctx->spr_cb[sprn].hea_read;
4269     } else {
4270         read_cb = ctx->spr_cb[sprn].oea_read;
4271     }
4272 #endif
4273     if (likely(read_cb != NULL)) {
4274         if (likely(read_cb != SPR_NOACCESS)) {
4275             (*read_cb)(ctx, rD(ctx->opcode), sprn);
4276         } else {
4277             /* Privilege exception */
4278             /*
4279              * This is a hack to avoid warnings when running Linux:
4280              * this OS breaks the PowerPC virtualisation model,
4281              * allowing userland application to read the PVR
4282              */
4283             if (sprn != SPR_PVR) {
4284                 qemu_log_mask(LOG_GUEST_ERROR, "Trying to read privileged spr "
4285                               "%d (0x%03x) at " TARGET_FMT_lx "\n", sprn, sprn,
4286                               ctx->base.pc_next - 4);
4287             }
4288             gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4289         }
4290     } else {
4291         /* ISA 2.07 defines these as no-ops */
4292         if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4293             (sprn >= 808 && sprn <= 811)) {
4294             /* This is a nop */
4295             return;
4296         }
4297         /* Not defined */
4298         qemu_log_mask(LOG_GUEST_ERROR,
4299                       "Trying to read invalid spr %d (0x%03x) at "
4300                       TARGET_FMT_lx "\n", sprn, sprn, ctx->base.pc_next - 4);
4301 
4302         /*
4303          * The behaviour depends on MSR:PR and SPR# bit 0x10, it can
4304          * generate a priv, a hv emu or a no-op
4305          */
4306         if (sprn & 0x10) {
4307             if (ctx->pr) {
4308                 gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4309             }
4310         } else {
4311             if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {
4312                 gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4313             }
4314         }
4315     }
4316 }
4317 
4318 static void gen_mfspr(DisasContext *ctx)
4319 {
4320     gen_op_mfspr(ctx);
4321 }
4322 
4323 /* mftb */
4324 static void gen_mftb(DisasContext *ctx)
4325 {
4326     gen_op_mfspr(ctx);
4327 }
4328 
4329 /* mtcrf mtocrf*/
4330 static void gen_mtcrf(DisasContext *ctx)
4331 {
4332     uint32_t crm, crn;
4333 
4334     crm = CRM(ctx->opcode);
4335     if (likely((ctx->opcode & 0x00100000))) {
4336         if (crm && ((crm & (crm - 1)) == 0)) {
4337             TCGv_i32 temp = tcg_temp_new_i32();
4338             crn = ctz32(crm);
4339             tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4340             tcg_gen_shri_i32(temp, temp, crn * 4);
4341             tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4342             tcg_temp_free_i32(temp);
4343         }
4344     } else {
4345         TCGv_i32 temp = tcg_temp_new_i32();
4346         tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4347         for (crn = 0 ; crn < 8 ; crn++) {
4348             if (crm & (1 << crn)) {
4349                     tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4350                     tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4351             }
4352         }
4353         tcg_temp_free_i32(temp);
4354     }
4355 }
4356 
4357 /* mtmsr */
4358 #if defined(TARGET_PPC64)
4359 static void gen_mtmsrd(DisasContext *ctx)
4360 {
4361     CHK_SV;
4362 
4363 #if !defined(CONFIG_USER_ONLY)
4364     if (ctx->opcode & 0x00010000) {
4365         /* Special form that does not need any synchronisation */
4366         TCGv t0 = tcg_temp_new();
4367         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)],
4368                         (1 << MSR_RI) | (1 << MSR_EE));
4369         tcg_gen_andi_tl(cpu_msr, cpu_msr,
4370                         ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
4371         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4372         tcg_temp_free(t0);
4373     } else {
4374         /*
4375          * XXX: we need to update nip before the store if we enter
4376          *      power saving mode, we will exit the loop directly from
4377          *      ppc_store_msr
4378          */
4379         if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
4380             gen_io_start();
4381         }
4382         gen_update_nip(ctx, ctx->base.pc_next);
4383         gen_helper_store_msr(cpu_env, cpu_gpr[rS(ctx->opcode)]);
4384         /* Must stop the translation as machine state (may have) changed */
4385         /* Note that mtmsr is not always defined as context-synchronizing */
4386         gen_stop_exception(ctx);
4387     }
4388 #endif /* !defined(CONFIG_USER_ONLY) */
4389 }
4390 #endif /* defined(TARGET_PPC64) */
4391 
4392 static void gen_mtmsr(DisasContext *ctx)
4393 {
4394     CHK_SV;
4395 
4396 #if !defined(CONFIG_USER_ONLY)
4397    if (ctx->opcode & 0x00010000) {
4398         /* Special form that does not need any synchronisation */
4399         TCGv t0 = tcg_temp_new();
4400         tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)],
4401                         (1 << MSR_RI) | (1 << MSR_EE));
4402         tcg_gen_andi_tl(cpu_msr, cpu_msr,
4403                         ~(target_ulong)((1 << MSR_RI) | (1 << MSR_EE)));
4404         tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
4405         tcg_temp_free(t0);
4406     } else {
4407         TCGv msr = tcg_temp_new();
4408 
4409         /*
4410          * XXX: we need to update nip before the store if we enter
4411          *      power saving mode, we will exit the loop directly from
4412          *      ppc_store_msr
4413          */
4414         if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
4415             gen_io_start();
4416         }
4417         gen_update_nip(ctx, ctx->base.pc_next);
4418 #if defined(TARGET_PPC64)
4419         tcg_gen_deposit_tl(msr, cpu_msr, cpu_gpr[rS(ctx->opcode)], 0, 32);
4420 #else
4421         tcg_gen_mov_tl(msr, cpu_gpr[rS(ctx->opcode)]);
4422 #endif
4423         gen_helper_store_msr(cpu_env, msr);
4424         tcg_temp_free(msr);
4425         /* Must stop the translation as machine state (may have) changed */
4426         /* Note that mtmsr is not always defined as context-synchronizing */
4427         gen_stop_exception(ctx);
4428     }
4429 #endif
4430 }
4431 
4432 /* mtspr */
4433 static void gen_mtspr(DisasContext *ctx)
4434 {
4435     void (*write_cb)(DisasContext *ctx, int sprn, int gprn);
4436     uint32_t sprn = SPR(ctx->opcode);
4437 
4438 #if defined(CONFIG_USER_ONLY)
4439     write_cb = ctx->spr_cb[sprn].uea_write;
4440 #else
4441     if (ctx->pr) {
4442         write_cb = ctx->spr_cb[sprn].uea_write;
4443     } else if (ctx->hv) {
4444         write_cb = ctx->spr_cb[sprn].hea_write;
4445     } else {
4446         write_cb = ctx->spr_cb[sprn].oea_write;
4447     }
4448 #endif
4449     if (likely(write_cb != NULL)) {
4450         if (likely(write_cb != SPR_NOACCESS)) {
4451             (*write_cb)(ctx, sprn, rS(ctx->opcode));
4452         } else {
4453             /* Privilege exception */
4454             qemu_log_mask(LOG_GUEST_ERROR, "Trying to write privileged spr "
4455                           "%d (0x%03x) at " TARGET_FMT_lx "\n", sprn, sprn,
4456                           ctx->base.pc_next - 4);
4457             gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4458         }
4459     } else {
4460         /* ISA 2.07 defines these as no-ops */
4461         if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4462             (sprn >= 808 && sprn <= 811)) {
4463             /* This is a nop */
4464             return;
4465         }
4466 
4467         /* Not defined */
4468         qemu_log_mask(LOG_GUEST_ERROR,
4469                       "Trying to write invalid spr %d (0x%03x) at "
4470                       TARGET_FMT_lx "\n", sprn, sprn, ctx->base.pc_next - 4);
4471 
4472 
4473         /*
4474          * The behaviour depends on MSR:PR and SPR# bit 0x10, it can
4475          * generate a priv, a hv emu or a no-op
4476          */
4477         if (sprn & 0x10) {
4478             if (ctx->pr) {
4479                 gen_priv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4480             }
4481         } else {
4482             if (ctx->pr || sprn == 0) {
4483                 gen_hvpriv_exception(ctx, POWERPC_EXCP_INVAL_SPR);
4484             }
4485         }
4486     }
4487 }
4488 
4489 #if defined(TARGET_PPC64)
4490 /* setb */
4491 static void gen_setb(DisasContext *ctx)
4492 {
4493     TCGv_i32 t0 = tcg_temp_new_i32();
4494     TCGv_i32 t8 = tcg_temp_new_i32();
4495     TCGv_i32 tm1 = tcg_temp_new_i32();
4496     int crf = crfS(ctx->opcode);
4497 
4498     tcg_gen_setcondi_i32(TCG_COND_GEU, t0, cpu_crf[crf], 4);
4499     tcg_gen_movi_i32(t8, 8);
4500     tcg_gen_movi_i32(tm1, -1);
4501     tcg_gen_movcond_i32(TCG_COND_GEU, t0, cpu_crf[crf], t8, tm1, t0);
4502     tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4503 
4504     tcg_temp_free_i32(t0);
4505     tcg_temp_free_i32(t8);
4506     tcg_temp_free_i32(tm1);
4507 }
4508 #endif
4509 
4510 /***                         Cache management                              ***/
4511 
4512 /* dcbf */
4513 static void gen_dcbf(DisasContext *ctx)
4514 {
4515     /* XXX: specification says this is treated as a load by the MMU */
4516     TCGv t0;
4517     gen_set_access_type(ctx, ACCESS_CACHE);
4518     t0 = tcg_temp_new();
4519     gen_addr_reg_index(ctx, t0);
4520     gen_qemu_ld8u(ctx, t0, t0);
4521     tcg_temp_free(t0);
4522 }
4523 
4524 /* dcbfep (external PID dcbf) */
4525 static void gen_dcbfep(DisasContext *ctx)
4526 {
4527     /* XXX: specification says this is treated as a load by the MMU */
4528     TCGv t0;
4529     CHK_SV;
4530     gen_set_access_type(ctx, ACCESS_CACHE);
4531     t0 = tcg_temp_new();
4532     gen_addr_reg_index(ctx, t0);
4533     tcg_gen_qemu_ld_tl(t0, t0, PPC_TLB_EPID_LOAD, DEF_MEMOP(MO_UB));
4534     tcg_temp_free(t0);
4535 }
4536 
4537 /* dcbi (Supervisor only) */
4538 static void gen_dcbi(DisasContext *ctx)
4539 {
4540 #if defined(CONFIG_USER_ONLY)
4541     GEN_PRIV;
4542 #else
4543     TCGv EA, val;
4544 
4545     CHK_SV;
4546     EA = tcg_temp_new();
4547     gen_set_access_type(ctx, ACCESS_CACHE);
4548     gen_addr_reg_index(ctx, EA);
4549     val = tcg_temp_new();
4550     /* XXX: specification says this should be treated as a store by the MMU */
4551     gen_qemu_ld8u(ctx, val, EA);
4552     gen_qemu_st8(ctx, val, EA);
4553     tcg_temp_free(val);
4554     tcg_temp_free(EA);
4555 #endif /* defined(CONFIG_USER_ONLY) */
4556 }
4557 
4558 /* dcdst */
4559 static void gen_dcbst(DisasContext *ctx)
4560 {
4561     /* XXX: specification say this is treated as a load by the MMU */
4562     TCGv t0;
4563     gen_set_access_type(ctx, ACCESS_CACHE);
4564     t0 = tcg_temp_new();
4565     gen_addr_reg_index(ctx, t0);
4566     gen_qemu_ld8u(ctx, t0, t0);
4567     tcg_temp_free(t0);
4568 }
4569 
4570 /* dcbstep (dcbstep External PID version) */
4571 static void gen_dcbstep(DisasContext *ctx)
4572 {
4573     /* XXX: specification say this is treated as a load by the MMU */
4574     TCGv t0;
4575     gen_set_access_type(ctx, ACCESS_CACHE);
4576     t0 = tcg_temp_new();
4577     gen_addr_reg_index(ctx, t0);
4578     tcg_gen_qemu_ld_tl(t0, t0, PPC_TLB_EPID_LOAD, DEF_MEMOP(MO_UB));
4579     tcg_temp_free(t0);
4580 }
4581 
4582 /* dcbt */
4583 static void gen_dcbt(DisasContext *ctx)
4584 {
4585     /*
4586      * interpreted as no-op
4587      * XXX: specification say this is treated as a load by the MMU but
4588      *      does not generate any exception
4589      */
4590 }
4591 
4592 /* dcbtep */
4593 static void gen_dcbtep(DisasContext *ctx)
4594 {
4595     /*
4596      * interpreted as no-op
4597      * XXX: specification say this is treated as a load by the MMU but
4598      *      does not generate any exception
4599      */
4600 }
4601 
4602 /* dcbtst */
4603 static void gen_dcbtst(DisasContext *ctx)
4604 {
4605     /*
4606      * interpreted as no-op
4607      * XXX: specification say this is treated as a load by the MMU but
4608      *      does not generate any exception
4609      */
4610 }
4611 
4612 /* dcbtstep */
4613 static void gen_dcbtstep(DisasContext *ctx)
4614 {
4615     /*
4616      * interpreted as no-op
4617      * XXX: specification say this is treated as a load by the MMU but
4618      *      does not generate any exception
4619      */
4620 }
4621 
4622 /* dcbtls */
4623 static void gen_dcbtls(DisasContext *ctx)
4624 {
4625     /* Always fails locking the cache */
4626     TCGv t0 = tcg_temp_new();
4627     gen_load_spr(t0, SPR_Exxx_L1CSR0);
4628     tcg_gen_ori_tl(t0, t0, L1CSR0_CUL);
4629     gen_store_spr(SPR_Exxx_L1CSR0, t0);
4630     tcg_temp_free(t0);
4631 }
4632 
4633 /* dcbz */
4634 static void gen_dcbz(DisasContext *ctx)
4635 {
4636     TCGv tcgv_addr;
4637     TCGv_i32 tcgv_op;
4638 
4639     gen_set_access_type(ctx, ACCESS_CACHE);
4640     tcgv_addr = tcg_temp_new();
4641     tcgv_op = tcg_const_i32(ctx->opcode & 0x03FF000);
4642     gen_addr_reg_index(ctx, tcgv_addr);
4643     gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_op);
4644     tcg_temp_free(tcgv_addr);
4645     tcg_temp_free_i32(tcgv_op);
4646 }
4647 
4648 /* dcbzep */
4649 static void gen_dcbzep(DisasContext *ctx)
4650 {
4651     TCGv tcgv_addr;
4652     TCGv_i32 tcgv_op;
4653 
4654     gen_set_access_type(ctx, ACCESS_CACHE);
4655     tcgv_addr = tcg_temp_new();
4656     tcgv_op = tcg_const_i32(ctx->opcode & 0x03FF000);
4657     gen_addr_reg_index(ctx, tcgv_addr);
4658     gen_helper_dcbzep(cpu_env, tcgv_addr, tcgv_op);
4659     tcg_temp_free(tcgv_addr);
4660     tcg_temp_free_i32(tcgv_op);
4661 }
4662 
4663 /* dst / dstt */
4664 static void gen_dst(DisasContext *ctx)
4665 {
4666     if (rA(ctx->opcode) == 0) {
4667         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
4668     } else {
4669         /* interpreted as no-op */
4670     }
4671 }
4672 
4673 /* dstst /dststt */
4674 static void gen_dstst(DisasContext *ctx)
4675 {
4676     if (rA(ctx->opcode) == 0) {
4677         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
4678     } else {
4679         /* interpreted as no-op */
4680     }
4681 
4682 }
4683 
4684 /* dss / dssall */
4685 static void gen_dss(DisasContext *ctx)
4686 {
4687     /* interpreted as no-op */
4688 }
4689 
4690 /* icbi */
4691 static void gen_icbi(DisasContext *ctx)
4692 {
4693     TCGv t0;
4694     gen_set_access_type(ctx, ACCESS_CACHE);
4695     t0 = tcg_temp_new();
4696     gen_addr_reg_index(ctx, t0);
4697     gen_helper_icbi(cpu_env, t0);
4698     tcg_temp_free(t0);
4699 }
4700 
4701 /* icbiep */
4702 static void gen_icbiep(DisasContext *ctx)
4703 {
4704     TCGv t0;
4705     gen_set_access_type(ctx, ACCESS_CACHE);
4706     t0 = tcg_temp_new();
4707     gen_addr_reg_index(ctx, t0);
4708     gen_helper_icbiep(cpu_env, t0);
4709     tcg_temp_free(t0);
4710 }
4711 
4712 /* Optional: */
4713 /* dcba */
4714 static void gen_dcba(DisasContext *ctx)
4715 {
4716     /*
4717      * interpreted as no-op
4718      * XXX: specification say this is treated as a store by the MMU
4719      *      but does not generate any exception
4720      */
4721 }
4722 
4723 /***                    Segment register manipulation                      ***/
4724 /* Supervisor only: */
4725 
4726 /* mfsr */
4727 static void gen_mfsr(DisasContext *ctx)
4728 {
4729 #if defined(CONFIG_USER_ONLY)
4730     GEN_PRIV;
4731 #else
4732     TCGv t0;
4733 
4734     CHK_SV;
4735     t0 = tcg_const_tl(SR(ctx->opcode));
4736     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4737     tcg_temp_free(t0);
4738 #endif /* defined(CONFIG_USER_ONLY) */
4739 }
4740 
4741 /* mfsrin */
4742 static void gen_mfsrin(DisasContext *ctx)
4743 {
4744 #if defined(CONFIG_USER_ONLY)
4745     GEN_PRIV;
4746 #else
4747     TCGv t0;
4748 
4749     CHK_SV;
4750     t0 = tcg_temp_new();
4751     tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4752     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4753     tcg_temp_free(t0);
4754 #endif /* defined(CONFIG_USER_ONLY) */
4755 }
4756 
4757 /* mtsr */
4758 static void gen_mtsr(DisasContext *ctx)
4759 {
4760 #if defined(CONFIG_USER_ONLY)
4761     GEN_PRIV;
4762 #else
4763     TCGv t0;
4764 
4765     CHK_SV;
4766     t0 = tcg_const_tl(SR(ctx->opcode));
4767     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4768     tcg_temp_free(t0);
4769 #endif /* defined(CONFIG_USER_ONLY) */
4770 }
4771 
4772 /* mtsrin */
4773 static void gen_mtsrin(DisasContext *ctx)
4774 {
4775 #if defined(CONFIG_USER_ONLY)
4776     GEN_PRIV;
4777 #else
4778     TCGv t0;
4779     CHK_SV;
4780 
4781     t0 = tcg_temp_new();
4782     tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4783     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
4784     tcg_temp_free(t0);
4785 #endif /* defined(CONFIG_USER_ONLY) */
4786 }
4787 
4788 #if defined(TARGET_PPC64)
4789 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
4790 
4791 /* mfsr */
4792 static void gen_mfsr_64b(DisasContext *ctx)
4793 {
4794 #if defined(CONFIG_USER_ONLY)
4795     GEN_PRIV;
4796 #else
4797     TCGv t0;
4798 
4799     CHK_SV;
4800     t0 = tcg_const_tl(SR(ctx->opcode));
4801     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4802     tcg_temp_free(t0);
4803 #endif /* defined(CONFIG_USER_ONLY) */
4804 }
4805 
4806 /* mfsrin */
4807 static void gen_mfsrin_64b(DisasContext *ctx)
4808 {
4809 #if defined(CONFIG_USER_ONLY)
4810     GEN_PRIV;
4811 #else
4812     TCGv t0;
4813 
4814     CHK_SV;
4815     t0 = tcg_temp_new();
4816     tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4817     gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
4818     tcg_temp_free(t0);
4819 #endif /* defined(CONFIG_USER_ONLY) */
4820 }
4821 
4822 /* mtsr */
4823 static void gen_mtsr_64b(DisasContext *ctx)
4824 {
4825 #if defined(CONFIG_USER_ONLY)
4826     GEN_PRIV;
4827 #else
4828     TCGv t0;
4829 
4830     CHK_SV;
4831     t0 = tcg_const_tl(SR(ctx->opcode));
4832     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4833     tcg_temp_free(t0);
4834 #endif /* defined(CONFIG_USER_ONLY) */
4835 }
4836 
4837 /* mtsrin */
4838 static void gen_mtsrin_64b(DisasContext *ctx)
4839 {
4840 #if defined(CONFIG_USER_ONLY)
4841     GEN_PRIV;
4842 #else
4843     TCGv t0;
4844 
4845     CHK_SV;
4846     t0 = tcg_temp_new();
4847     tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
4848     gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
4849     tcg_temp_free(t0);
4850 #endif /* defined(CONFIG_USER_ONLY) */
4851 }
4852 
4853 /* slbmte */
4854 static void gen_slbmte(DisasContext *ctx)
4855 {
4856 #if defined(CONFIG_USER_ONLY)
4857     GEN_PRIV;
4858 #else
4859     CHK_SV;
4860 
4861     gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
4862                          cpu_gpr[rS(ctx->opcode)]);
4863 #endif /* defined(CONFIG_USER_ONLY) */
4864 }
4865 
4866 static void gen_slbmfee(DisasContext *ctx)
4867 {
4868 #if defined(CONFIG_USER_ONLY)
4869     GEN_PRIV;
4870 #else
4871     CHK_SV;
4872 
4873     gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4874                              cpu_gpr[rB(ctx->opcode)]);
4875 #endif /* defined(CONFIG_USER_ONLY) */
4876 }
4877 
4878 static void gen_slbmfev(DisasContext *ctx)
4879 {
4880 #if defined(CONFIG_USER_ONLY)
4881     GEN_PRIV;
4882 #else
4883     CHK_SV;
4884 
4885     gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4886                              cpu_gpr[rB(ctx->opcode)]);
4887 #endif /* defined(CONFIG_USER_ONLY) */
4888 }
4889 
4890 static void gen_slbfee_(DisasContext *ctx)
4891 {
4892 #if defined(CONFIG_USER_ONLY)
4893     gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4894 #else
4895     TCGLabel *l1, *l2;
4896 
4897     if (unlikely(ctx->pr)) {
4898         gen_inval_exception(ctx, POWERPC_EXCP_PRIV_REG);
4899         return;
4900     }
4901     gen_helper_find_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
4902                              cpu_gpr[rB(ctx->opcode)]);
4903     l1 = gen_new_label();
4904     l2 = gen_new_label();
4905     tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4906     tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rS(ctx->opcode)], -1, l1);
4907     tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], CRF_EQ);
4908     tcg_gen_br(l2);
4909     gen_set_label(l1);
4910     tcg_gen_movi_tl(cpu_gpr[rS(ctx->opcode)], 0);
4911     gen_set_label(l2);
4912 #endif
4913 }
4914 #endif /* defined(TARGET_PPC64) */
4915 
4916 /***                      Lookaside buffer management                      ***/
4917 /* Optional & supervisor only: */
4918 
4919 /* tlbia */
4920 static void gen_tlbia(DisasContext *ctx)
4921 {
4922 #if defined(CONFIG_USER_ONLY)
4923     GEN_PRIV;
4924 #else
4925     CHK_HV;
4926 
4927     gen_helper_tlbia(cpu_env);
4928 #endif  /* defined(CONFIG_USER_ONLY) */
4929 }
4930 
4931 /* tlbiel */
4932 static void gen_tlbiel(DisasContext *ctx)
4933 {
4934 #if defined(CONFIG_USER_ONLY)
4935     GEN_PRIV;
4936 #else
4937     CHK_SV;
4938 
4939     gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4940 #endif /* defined(CONFIG_USER_ONLY) */
4941 }
4942 
4943 /* tlbie */
4944 static void gen_tlbie(DisasContext *ctx)
4945 {
4946 #if defined(CONFIG_USER_ONLY)
4947     GEN_PRIV;
4948 #else
4949     TCGv_i32 t1;
4950 
4951     if (ctx->gtse) {
4952         CHK_SV; /* If gtse is set then tlbie is supervisor privileged */
4953     } else {
4954         CHK_HV; /* Else hypervisor privileged */
4955     }
4956 
4957     if (NARROW_MODE(ctx)) {
4958         TCGv t0 = tcg_temp_new();
4959         tcg_gen_ext32u_tl(t0, cpu_gpr[rB(ctx->opcode)]);
4960         gen_helper_tlbie(cpu_env, t0);
4961         tcg_temp_free(t0);
4962     } else {
4963         gen_helper_tlbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
4964     }
4965     t1 = tcg_temp_new_i32();
4966     tcg_gen_ld_i32(t1, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
4967     tcg_gen_ori_i32(t1, t1, TLB_NEED_GLOBAL_FLUSH);
4968     tcg_gen_st_i32(t1, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
4969     tcg_temp_free_i32(t1);
4970 #endif /* defined(CONFIG_USER_ONLY) */
4971 }
4972 
4973 /* tlbsync */
4974 static void gen_tlbsync(DisasContext *ctx)
4975 {
4976 #if defined(CONFIG_USER_ONLY)
4977     GEN_PRIV;
4978 #else
4979 
4980     if (ctx->gtse) {
4981         CHK_SV; /* If gtse is set then tlbsync is supervisor privileged */
4982     } else {
4983         CHK_HV; /* Else hypervisor privileged */
4984     }
4985 
4986     /* BookS does both ptesync and tlbsync make tlbsync a nop for server */
4987     if (ctx->insns_flags & PPC_BOOKE) {
4988         gen_check_tlb_flush(ctx, true);
4989     }
4990 #endif /* defined(CONFIG_USER_ONLY) */
4991 }
4992 
4993 #if defined(TARGET_PPC64)
4994 /* slbia */
4995 static void gen_slbia(DisasContext *ctx)
4996 {
4997 #if defined(CONFIG_USER_ONLY)
4998     GEN_PRIV;
4999 #else
5000     uint32_t ih = (ctx->opcode >> 21) & 0x7;
5001     TCGv_i32 t0 = tcg_const_i32(ih);
5002 
5003     CHK_SV;
5004 
5005     gen_helper_slbia(cpu_env, t0);
5006 #endif /* defined(CONFIG_USER_ONLY) */
5007 }
5008 
5009 /* slbie */
5010 static void gen_slbie(DisasContext *ctx)
5011 {
5012 #if defined(CONFIG_USER_ONLY)
5013     GEN_PRIV;
5014 #else
5015     CHK_SV;
5016 
5017     gen_helper_slbie(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5018 #endif /* defined(CONFIG_USER_ONLY) */
5019 }
5020 
5021 /* slbieg */
5022 static void gen_slbieg(DisasContext *ctx)
5023 {
5024 #if defined(CONFIG_USER_ONLY)
5025     GEN_PRIV;
5026 #else
5027     CHK_SV;
5028 
5029     gen_helper_slbieg(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5030 #endif /* defined(CONFIG_USER_ONLY) */
5031 }
5032 
5033 /* slbsync */
5034 static void gen_slbsync(DisasContext *ctx)
5035 {
5036 #if defined(CONFIG_USER_ONLY)
5037     GEN_PRIV;
5038 #else
5039     CHK_SV;
5040     gen_check_tlb_flush(ctx, true);
5041 #endif /* defined(CONFIG_USER_ONLY) */
5042 }
5043 
5044 #endif  /* defined(TARGET_PPC64) */
5045 
5046 /***                              External control                         ***/
5047 /* Optional: */
5048 
5049 /* eciwx */
5050 static void gen_eciwx(DisasContext *ctx)
5051 {
5052     TCGv t0;
5053     /* Should check EAR[E] ! */
5054     gen_set_access_type(ctx, ACCESS_EXT);
5055     t0 = tcg_temp_new();
5056     gen_addr_reg_index(ctx, t0);
5057     tcg_gen_qemu_ld_tl(cpu_gpr[rD(ctx->opcode)], t0, ctx->mem_idx,
5058                        DEF_MEMOP(MO_UL | MO_ALIGN));
5059     tcg_temp_free(t0);
5060 }
5061 
5062 /* ecowx */
5063 static void gen_ecowx(DisasContext *ctx)
5064 {
5065     TCGv t0;
5066     /* Should check EAR[E] ! */
5067     gen_set_access_type(ctx, ACCESS_EXT);
5068     t0 = tcg_temp_new();
5069     gen_addr_reg_index(ctx, t0);
5070     tcg_gen_qemu_st_tl(cpu_gpr[rD(ctx->opcode)], t0, ctx->mem_idx,
5071                        DEF_MEMOP(MO_UL | MO_ALIGN));
5072     tcg_temp_free(t0);
5073 }
5074 
5075 /* PowerPC 601 specific instructions */
5076 
5077 /* abs - abs. */
5078 static void gen_abs(DisasContext *ctx)
5079 {
5080     TCGv d = cpu_gpr[rD(ctx->opcode)];
5081     TCGv a = cpu_gpr[rA(ctx->opcode)];
5082 
5083     tcg_gen_abs_tl(d, a);
5084     if (unlikely(Rc(ctx->opcode) != 0)) {
5085         gen_set_Rc0(ctx, d);
5086     }
5087 }
5088 
5089 /* abso - abso. */
5090 static void gen_abso(DisasContext *ctx)
5091 {
5092     TCGv d = cpu_gpr[rD(ctx->opcode)];
5093     TCGv a = cpu_gpr[rA(ctx->opcode)];
5094 
5095     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_ov, a, 0x80000000);
5096     tcg_gen_abs_tl(d, a);
5097     tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
5098     if (unlikely(Rc(ctx->opcode) != 0)) {
5099         gen_set_Rc0(ctx, d);
5100     }
5101 }
5102 
5103 /* clcs */
5104 static void gen_clcs(DisasContext *ctx)
5105 {
5106     TCGv_i32 t0 = tcg_const_i32(rA(ctx->opcode));
5107     gen_helper_clcs(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5108     tcg_temp_free_i32(t0);
5109     /* Rc=1 sets CR0 to an undefined state */
5110 }
5111 
5112 /* div - div. */
5113 static void gen_div(DisasContext *ctx)
5114 {
5115     gen_helper_div(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5116                    cpu_gpr[rB(ctx->opcode)]);
5117     if (unlikely(Rc(ctx->opcode) != 0)) {
5118         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5119     }
5120 }
5121 
5122 /* divo - divo. */
5123 static void gen_divo(DisasContext *ctx)
5124 {
5125     gen_helper_divo(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5126                     cpu_gpr[rB(ctx->opcode)]);
5127     if (unlikely(Rc(ctx->opcode) != 0)) {
5128         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5129     }
5130 }
5131 
5132 /* divs - divs. */
5133 static void gen_divs(DisasContext *ctx)
5134 {
5135     gen_helper_divs(cpu_gpr[rD(ctx->opcode)], cpu_env, cpu_gpr[rA(ctx->opcode)],
5136                     cpu_gpr[rB(ctx->opcode)]);
5137     if (unlikely(Rc(ctx->opcode) != 0)) {
5138         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5139     }
5140 }
5141 
5142 /* divso - divso. */
5143 static void gen_divso(DisasContext *ctx)
5144 {
5145     gen_helper_divso(cpu_gpr[rD(ctx->opcode)], cpu_env,
5146                      cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5147     if (unlikely(Rc(ctx->opcode) != 0)) {
5148         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5149     }
5150 }
5151 
5152 /* doz - doz. */
5153 static void gen_doz(DisasContext *ctx)
5154 {
5155     TCGLabel *l1 = gen_new_label();
5156     TCGLabel *l2 = gen_new_label();
5157     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)],
5158                       cpu_gpr[rA(ctx->opcode)], l1);
5159     tcg_gen_sub_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
5160                    cpu_gpr[rA(ctx->opcode)]);
5161     tcg_gen_br(l2);
5162     gen_set_label(l1);
5163     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5164     gen_set_label(l2);
5165     if (unlikely(Rc(ctx->opcode) != 0)) {
5166         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5167     }
5168 }
5169 
5170 /* dozo - dozo. */
5171 static void gen_dozo(DisasContext *ctx)
5172 {
5173     TCGLabel *l1 = gen_new_label();
5174     TCGLabel *l2 = gen_new_label();
5175     TCGv t0 = tcg_temp_new();
5176     TCGv t1 = tcg_temp_new();
5177     TCGv t2 = tcg_temp_new();
5178     /* Start with XER OV disabled, the most likely case */
5179     tcg_gen_movi_tl(cpu_ov, 0);
5180     tcg_gen_brcond_tl(TCG_COND_GE, cpu_gpr[rB(ctx->opcode)],
5181                       cpu_gpr[rA(ctx->opcode)], l1);
5182     tcg_gen_sub_tl(t0, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5183     tcg_gen_xor_tl(t1, cpu_gpr[rB(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5184     tcg_gen_xor_tl(t2, cpu_gpr[rA(ctx->opcode)], t0);
5185     tcg_gen_andc_tl(t1, t1, t2);
5186     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], t0);
5187     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5188     tcg_gen_movi_tl(cpu_ov, 1);
5189     tcg_gen_movi_tl(cpu_so, 1);
5190     tcg_gen_br(l2);
5191     gen_set_label(l1);
5192     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5193     gen_set_label(l2);
5194     tcg_temp_free(t0);
5195     tcg_temp_free(t1);
5196     tcg_temp_free(t2);
5197     if (unlikely(Rc(ctx->opcode) != 0)) {
5198         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5199     }
5200 }
5201 
5202 /* dozi */
5203 static void gen_dozi(DisasContext *ctx)
5204 {
5205     target_long simm = SIMM(ctx->opcode);
5206     TCGLabel *l1 = gen_new_label();
5207     TCGLabel *l2 = gen_new_label();
5208     tcg_gen_brcondi_tl(TCG_COND_LT, cpu_gpr[rA(ctx->opcode)], simm, l1);
5209     tcg_gen_subfi_tl(cpu_gpr[rD(ctx->opcode)], simm, cpu_gpr[rA(ctx->opcode)]);
5210     tcg_gen_br(l2);
5211     gen_set_label(l1);
5212     tcg_gen_movi_tl(cpu_gpr[rD(ctx->opcode)], 0);
5213     gen_set_label(l2);
5214     if (unlikely(Rc(ctx->opcode) != 0)) {
5215         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5216     }
5217 }
5218 
5219 /* lscbx - lscbx. */
5220 static void gen_lscbx(DisasContext *ctx)
5221 {
5222     TCGv t0 = tcg_temp_new();
5223     TCGv_i32 t1 = tcg_const_i32(rD(ctx->opcode));
5224     TCGv_i32 t2 = tcg_const_i32(rA(ctx->opcode));
5225     TCGv_i32 t3 = tcg_const_i32(rB(ctx->opcode));
5226 
5227     gen_addr_reg_index(ctx, t0);
5228     gen_helper_lscbx(t0, cpu_env, t0, t1, t2, t3);
5229     tcg_temp_free_i32(t1);
5230     tcg_temp_free_i32(t2);
5231     tcg_temp_free_i32(t3);
5232     tcg_gen_andi_tl(cpu_xer, cpu_xer, ~0x7F);
5233     tcg_gen_or_tl(cpu_xer, cpu_xer, t0);
5234     if (unlikely(Rc(ctx->opcode) != 0)) {
5235         gen_set_Rc0(ctx, t0);
5236     }
5237     tcg_temp_free(t0);
5238 }
5239 
5240 /* maskg - maskg. */
5241 static void gen_maskg(DisasContext *ctx)
5242 {
5243     TCGLabel *l1 = gen_new_label();
5244     TCGv t0 = tcg_temp_new();
5245     TCGv t1 = tcg_temp_new();
5246     TCGv t2 = tcg_temp_new();
5247     TCGv t3 = tcg_temp_new();
5248     tcg_gen_movi_tl(t3, 0xFFFFFFFF);
5249     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5250     tcg_gen_andi_tl(t1, cpu_gpr[rS(ctx->opcode)], 0x1F);
5251     tcg_gen_addi_tl(t2, t0, 1);
5252     tcg_gen_shr_tl(t2, t3, t2);
5253     tcg_gen_shr_tl(t3, t3, t1);
5254     tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], t2, t3);
5255     tcg_gen_brcond_tl(TCG_COND_GE, t0, t1, l1);
5256     tcg_gen_neg_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5257     gen_set_label(l1);
5258     tcg_temp_free(t0);
5259     tcg_temp_free(t1);
5260     tcg_temp_free(t2);
5261     tcg_temp_free(t3);
5262     if (unlikely(Rc(ctx->opcode) != 0)) {
5263         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5264     }
5265 }
5266 
5267 /* maskir - maskir. */
5268 static void gen_maskir(DisasContext *ctx)
5269 {
5270     TCGv t0 = tcg_temp_new();
5271     TCGv t1 = tcg_temp_new();
5272     tcg_gen_and_tl(t0, cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5273     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
5274     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5275     tcg_temp_free(t0);
5276     tcg_temp_free(t1);
5277     if (unlikely(Rc(ctx->opcode) != 0)) {
5278         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5279     }
5280 }
5281 
5282 /* mul - mul. */
5283 static void gen_mul(DisasContext *ctx)
5284 {
5285     TCGv_i64 t0 = tcg_temp_new_i64();
5286     TCGv_i64 t1 = tcg_temp_new_i64();
5287     TCGv t2 = tcg_temp_new();
5288     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5289     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5290     tcg_gen_mul_i64(t0, t0, t1);
5291     tcg_gen_trunc_i64_tl(t2, t0);
5292     gen_store_spr(SPR_MQ, t2);
5293     tcg_gen_shri_i64(t1, t0, 32);
5294     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5295     tcg_temp_free_i64(t0);
5296     tcg_temp_free_i64(t1);
5297     tcg_temp_free(t2);
5298     if (unlikely(Rc(ctx->opcode) != 0)) {
5299         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5300     }
5301 }
5302 
5303 /* mulo - mulo. */
5304 static void gen_mulo(DisasContext *ctx)
5305 {
5306     TCGLabel *l1 = gen_new_label();
5307     TCGv_i64 t0 = tcg_temp_new_i64();
5308     TCGv_i64 t1 = tcg_temp_new_i64();
5309     TCGv t2 = tcg_temp_new();
5310     /* Start with XER OV disabled, the most likely case */
5311     tcg_gen_movi_tl(cpu_ov, 0);
5312     tcg_gen_extu_tl_i64(t0, cpu_gpr[rA(ctx->opcode)]);
5313     tcg_gen_extu_tl_i64(t1, cpu_gpr[rB(ctx->opcode)]);
5314     tcg_gen_mul_i64(t0, t0, t1);
5315     tcg_gen_trunc_i64_tl(t2, t0);
5316     gen_store_spr(SPR_MQ, t2);
5317     tcg_gen_shri_i64(t1, t0, 32);
5318     tcg_gen_trunc_i64_tl(cpu_gpr[rD(ctx->opcode)], t1);
5319     tcg_gen_ext32s_i64(t1, t0);
5320     tcg_gen_brcond_i64(TCG_COND_EQ, t0, t1, l1);
5321     tcg_gen_movi_tl(cpu_ov, 1);
5322     tcg_gen_movi_tl(cpu_so, 1);
5323     gen_set_label(l1);
5324     tcg_temp_free_i64(t0);
5325     tcg_temp_free_i64(t1);
5326     tcg_temp_free(t2);
5327     if (unlikely(Rc(ctx->opcode) != 0)) {
5328         gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
5329     }
5330 }
5331 
5332 /* nabs - nabs. */
5333 static void gen_nabs(DisasContext *ctx)
5334 {
5335     TCGv d = cpu_gpr[rD(ctx->opcode)];
5336     TCGv a = cpu_gpr[rA(ctx->opcode)];
5337 
5338     tcg_gen_abs_tl(d, a);
5339     tcg_gen_neg_tl(d, d);
5340     if (unlikely(Rc(ctx->opcode) != 0)) {
5341         gen_set_Rc0(ctx, d);
5342     }
5343 }
5344 
5345 /* nabso - nabso. */
5346 static void gen_nabso(DisasContext *ctx)
5347 {
5348     TCGv d = cpu_gpr[rD(ctx->opcode)];
5349     TCGv a = cpu_gpr[rA(ctx->opcode)];
5350 
5351     tcg_gen_abs_tl(d, a);
5352     tcg_gen_neg_tl(d, d);
5353     /* nabs never overflows */
5354     tcg_gen_movi_tl(cpu_ov, 0);
5355     if (unlikely(Rc(ctx->opcode) != 0)) {
5356         gen_set_Rc0(ctx, d);
5357     }
5358 }
5359 
5360 /* rlmi - rlmi. */
5361 static void gen_rlmi(DisasContext *ctx)
5362 {
5363     uint32_t mb = MB(ctx->opcode);
5364     uint32_t me = ME(ctx->opcode);
5365     TCGv t0 = tcg_temp_new();
5366     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5367     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5368     tcg_gen_andi_tl(t0, t0, MASK(mb, me));
5369     tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
5370                     ~MASK(mb, me));
5371     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], t0);
5372     tcg_temp_free(t0);
5373     if (unlikely(Rc(ctx->opcode) != 0)) {
5374         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5375     }
5376 }
5377 
5378 /* rrib - rrib. */
5379 static void gen_rrib(DisasContext *ctx)
5380 {
5381     TCGv t0 = tcg_temp_new();
5382     TCGv t1 = tcg_temp_new();
5383     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5384     tcg_gen_movi_tl(t1, 0x80000000);
5385     tcg_gen_shr_tl(t1, t1, t0);
5386     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5387     tcg_gen_and_tl(t0, t0, t1);
5388     tcg_gen_andc_tl(t1, cpu_gpr[rA(ctx->opcode)], t1);
5389     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5390     tcg_temp_free(t0);
5391     tcg_temp_free(t1);
5392     if (unlikely(Rc(ctx->opcode) != 0)) {
5393         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5394     }
5395 }
5396 
5397 /* sle - sle. */
5398 static void gen_sle(DisasContext *ctx)
5399 {
5400     TCGv t0 = tcg_temp_new();
5401     TCGv t1 = tcg_temp_new();
5402     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5403     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5404     tcg_gen_subfi_tl(t1, 32, t1);
5405     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5406     tcg_gen_or_tl(t1, t0, t1);
5407     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5408     gen_store_spr(SPR_MQ, t1);
5409     tcg_temp_free(t0);
5410     tcg_temp_free(t1);
5411     if (unlikely(Rc(ctx->opcode) != 0)) {
5412         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5413     }
5414 }
5415 
5416 /* sleq - sleq. */
5417 static void gen_sleq(DisasContext *ctx)
5418 {
5419     TCGv t0 = tcg_temp_new();
5420     TCGv t1 = tcg_temp_new();
5421     TCGv t2 = tcg_temp_new();
5422     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5423     tcg_gen_movi_tl(t2, 0xFFFFFFFF);
5424     tcg_gen_shl_tl(t2, t2, t0);
5425     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5426     gen_load_spr(t1, SPR_MQ);
5427     gen_store_spr(SPR_MQ, t0);
5428     tcg_gen_and_tl(t0, t0, t2);
5429     tcg_gen_andc_tl(t1, t1, t2);
5430     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5431     tcg_temp_free(t0);
5432     tcg_temp_free(t1);
5433     tcg_temp_free(t2);
5434     if (unlikely(Rc(ctx->opcode) != 0)) {
5435         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5436     }
5437 }
5438 
5439 /* sliq - sliq. */
5440 static void gen_sliq(DisasContext *ctx)
5441 {
5442     int sh = SH(ctx->opcode);
5443     TCGv t0 = tcg_temp_new();
5444     TCGv t1 = tcg_temp_new();
5445     tcg_gen_shli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5446     tcg_gen_shri_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5447     tcg_gen_or_tl(t1, t0, t1);
5448     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5449     gen_store_spr(SPR_MQ, t1);
5450     tcg_temp_free(t0);
5451     tcg_temp_free(t1);
5452     if (unlikely(Rc(ctx->opcode) != 0)) {
5453         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5454     }
5455 }
5456 
5457 /* slliq - slliq. */
5458 static void gen_slliq(DisasContext *ctx)
5459 {
5460     int sh = SH(ctx->opcode);
5461     TCGv t0 = tcg_temp_new();
5462     TCGv t1 = tcg_temp_new();
5463     tcg_gen_rotli_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5464     gen_load_spr(t1, SPR_MQ);
5465     gen_store_spr(SPR_MQ, t0);
5466     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU << sh));
5467     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU << sh));
5468     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5469     tcg_temp_free(t0);
5470     tcg_temp_free(t1);
5471     if (unlikely(Rc(ctx->opcode) != 0)) {
5472         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5473     }
5474 }
5475 
5476 /* sllq - sllq. */
5477 static void gen_sllq(DisasContext *ctx)
5478 {
5479     TCGLabel *l1 = gen_new_label();
5480     TCGLabel *l2 = gen_new_label();
5481     TCGv t0 = tcg_temp_local_new();
5482     TCGv t1 = tcg_temp_local_new();
5483     TCGv t2 = tcg_temp_local_new();
5484     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5485     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5486     tcg_gen_shl_tl(t1, t1, t2);
5487     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5488     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5489     gen_load_spr(t0, SPR_MQ);
5490     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5491     tcg_gen_br(l2);
5492     gen_set_label(l1);
5493     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5494     gen_load_spr(t2, SPR_MQ);
5495     tcg_gen_andc_tl(t1, t2, t1);
5496     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5497     gen_set_label(l2);
5498     tcg_temp_free(t0);
5499     tcg_temp_free(t1);
5500     tcg_temp_free(t2);
5501     if (unlikely(Rc(ctx->opcode) != 0)) {
5502         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5503     }
5504 }
5505 
5506 /* slq - slq. */
5507 static void gen_slq(DisasContext *ctx)
5508 {
5509     TCGLabel *l1 = gen_new_label();
5510     TCGv t0 = tcg_temp_new();
5511     TCGv t1 = tcg_temp_new();
5512     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5513     tcg_gen_shl_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5514     tcg_gen_subfi_tl(t1, 32, t1);
5515     tcg_gen_shr_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5516     tcg_gen_or_tl(t1, t0, t1);
5517     gen_store_spr(SPR_MQ, t1);
5518     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5519     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5520     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5521     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5522     gen_set_label(l1);
5523     tcg_temp_free(t0);
5524     tcg_temp_free(t1);
5525     if (unlikely(Rc(ctx->opcode) != 0)) {
5526         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5527     }
5528 }
5529 
5530 /* sraiq - sraiq. */
5531 static void gen_sraiq(DisasContext *ctx)
5532 {
5533     int sh = SH(ctx->opcode);
5534     TCGLabel *l1 = gen_new_label();
5535     TCGv t0 = tcg_temp_new();
5536     TCGv t1 = tcg_temp_new();
5537     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5538     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5539     tcg_gen_or_tl(t0, t0, t1);
5540     gen_store_spr(SPR_MQ, t0);
5541     tcg_gen_movi_tl(cpu_ca, 0);
5542     tcg_gen_brcondi_tl(TCG_COND_EQ, t1, 0, l1);
5543     tcg_gen_brcondi_tl(TCG_COND_GE, cpu_gpr[rS(ctx->opcode)], 0, l1);
5544     tcg_gen_movi_tl(cpu_ca, 1);
5545     gen_set_label(l1);
5546     tcg_gen_sari_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], sh);
5547     tcg_temp_free(t0);
5548     tcg_temp_free(t1);
5549     if (unlikely(Rc(ctx->opcode) != 0)) {
5550         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5551     }
5552 }
5553 
5554 /* sraq - sraq. */
5555 static void gen_sraq(DisasContext *ctx)
5556 {
5557     TCGLabel *l1 = gen_new_label();
5558     TCGLabel *l2 = gen_new_label();
5559     TCGv t0 = tcg_temp_new();
5560     TCGv t1 = tcg_temp_local_new();
5561     TCGv t2 = tcg_temp_local_new();
5562     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5563     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5564     tcg_gen_sar_tl(t1, cpu_gpr[rS(ctx->opcode)], t2);
5565     tcg_gen_subfi_tl(t2, 32, t2);
5566     tcg_gen_shl_tl(t2, cpu_gpr[rS(ctx->opcode)], t2);
5567     tcg_gen_or_tl(t0, t0, t2);
5568     gen_store_spr(SPR_MQ, t0);
5569     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5570     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l1);
5571     tcg_gen_mov_tl(t2, cpu_gpr[rS(ctx->opcode)]);
5572     tcg_gen_sari_tl(t1, cpu_gpr[rS(ctx->opcode)], 31);
5573     gen_set_label(l1);
5574     tcg_temp_free(t0);
5575     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t1);
5576     tcg_gen_movi_tl(cpu_ca, 0);
5577     tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l2);
5578     tcg_gen_brcondi_tl(TCG_COND_EQ, t2, 0, l2);
5579     tcg_gen_movi_tl(cpu_ca, 1);
5580     gen_set_label(l2);
5581     tcg_temp_free(t1);
5582     tcg_temp_free(t2);
5583     if (unlikely(Rc(ctx->opcode) != 0)) {
5584         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5585     }
5586 }
5587 
5588 /* sre - sre. */
5589 static void gen_sre(DisasContext *ctx)
5590 {
5591     TCGv t0 = tcg_temp_new();
5592     TCGv t1 = tcg_temp_new();
5593     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5594     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5595     tcg_gen_subfi_tl(t1, 32, t1);
5596     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5597     tcg_gen_or_tl(t1, t0, t1);
5598     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5599     gen_store_spr(SPR_MQ, t1);
5600     tcg_temp_free(t0);
5601     tcg_temp_free(t1);
5602     if (unlikely(Rc(ctx->opcode) != 0)) {
5603         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5604     }
5605 }
5606 
5607 /* srea - srea. */
5608 static void gen_srea(DisasContext *ctx)
5609 {
5610     TCGv t0 = tcg_temp_new();
5611     TCGv t1 = tcg_temp_new();
5612     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5613     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5614     gen_store_spr(SPR_MQ, t0);
5615     tcg_gen_sar_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], t1);
5616     tcg_temp_free(t0);
5617     tcg_temp_free(t1);
5618     if (unlikely(Rc(ctx->opcode) != 0)) {
5619         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5620     }
5621 }
5622 
5623 /* sreq */
5624 static void gen_sreq(DisasContext *ctx)
5625 {
5626     TCGv t0 = tcg_temp_new();
5627     TCGv t1 = tcg_temp_new();
5628     TCGv t2 = tcg_temp_new();
5629     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1F);
5630     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5631     tcg_gen_shr_tl(t1, t1, t0);
5632     tcg_gen_rotr_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
5633     gen_load_spr(t2, SPR_MQ);
5634     gen_store_spr(SPR_MQ, t0);
5635     tcg_gen_and_tl(t0, t0, t1);
5636     tcg_gen_andc_tl(t2, t2, t1);
5637     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5638     tcg_temp_free(t0);
5639     tcg_temp_free(t1);
5640     tcg_temp_free(t2);
5641     if (unlikely(Rc(ctx->opcode) != 0)) {
5642         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5643     }
5644 }
5645 
5646 /* sriq */
5647 static void gen_sriq(DisasContext *ctx)
5648 {
5649     int sh = SH(ctx->opcode);
5650     TCGv t0 = tcg_temp_new();
5651     TCGv t1 = tcg_temp_new();
5652     tcg_gen_shri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5653     tcg_gen_shli_tl(t1, cpu_gpr[rS(ctx->opcode)], 32 - sh);
5654     tcg_gen_or_tl(t1, t0, t1);
5655     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5656     gen_store_spr(SPR_MQ, t1);
5657     tcg_temp_free(t0);
5658     tcg_temp_free(t1);
5659     if (unlikely(Rc(ctx->opcode) != 0)) {
5660         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5661     }
5662 }
5663 
5664 /* srliq */
5665 static void gen_srliq(DisasContext *ctx)
5666 {
5667     int sh = SH(ctx->opcode);
5668     TCGv t0 = tcg_temp_new();
5669     TCGv t1 = tcg_temp_new();
5670     tcg_gen_rotri_tl(t0, cpu_gpr[rS(ctx->opcode)], sh);
5671     gen_load_spr(t1, SPR_MQ);
5672     gen_store_spr(SPR_MQ, t0);
5673     tcg_gen_andi_tl(t0, t0,  (0xFFFFFFFFU >> sh));
5674     tcg_gen_andi_tl(t1, t1, ~(0xFFFFFFFFU >> sh));
5675     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5676     tcg_temp_free(t0);
5677     tcg_temp_free(t1);
5678     if (unlikely(Rc(ctx->opcode) != 0)) {
5679         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5680     }
5681 }
5682 
5683 /* srlq */
5684 static void gen_srlq(DisasContext *ctx)
5685 {
5686     TCGLabel *l1 = gen_new_label();
5687     TCGLabel *l2 = gen_new_label();
5688     TCGv t0 = tcg_temp_local_new();
5689     TCGv t1 = tcg_temp_local_new();
5690     TCGv t2 = tcg_temp_local_new();
5691     tcg_gen_andi_tl(t2, cpu_gpr[rB(ctx->opcode)], 0x1F);
5692     tcg_gen_movi_tl(t1, 0xFFFFFFFF);
5693     tcg_gen_shr_tl(t2, t1, t2);
5694     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x20);
5695     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5696     gen_load_spr(t0, SPR_MQ);
5697     tcg_gen_and_tl(cpu_gpr[rA(ctx->opcode)], t0, t2);
5698     tcg_gen_br(l2);
5699     gen_set_label(l1);
5700     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t2);
5701     tcg_gen_and_tl(t0, t0, t2);
5702     gen_load_spr(t1, SPR_MQ);
5703     tcg_gen_andc_tl(t1, t1, t2);
5704     tcg_gen_or_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
5705     gen_set_label(l2);
5706     tcg_temp_free(t0);
5707     tcg_temp_free(t1);
5708     tcg_temp_free(t2);
5709     if (unlikely(Rc(ctx->opcode) != 0)) {
5710         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5711     }
5712 }
5713 
5714 /* srq */
5715 static void gen_srq(DisasContext *ctx)
5716 {
5717     TCGLabel *l1 = gen_new_label();
5718     TCGv t0 = tcg_temp_new();
5719     TCGv t1 = tcg_temp_new();
5720     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1F);
5721     tcg_gen_shr_tl(t0, cpu_gpr[rS(ctx->opcode)], t1);
5722     tcg_gen_subfi_tl(t1, 32, t1);
5723     tcg_gen_shl_tl(t1, cpu_gpr[rS(ctx->opcode)], t1);
5724     tcg_gen_or_tl(t1, t0, t1);
5725     gen_store_spr(SPR_MQ, t1);
5726     tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x20);
5727     tcg_gen_mov_tl(cpu_gpr[rA(ctx->opcode)], t0);
5728     tcg_gen_brcondi_tl(TCG_COND_EQ, t0, 0, l1);
5729     tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
5730     gen_set_label(l1);
5731     tcg_temp_free(t0);
5732     tcg_temp_free(t1);
5733     if (unlikely(Rc(ctx->opcode) != 0)) {
5734         gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
5735     }
5736 }
5737 
5738 /* PowerPC 602 specific instructions */
5739 
5740 /* dsa  */
5741 static void gen_dsa(DisasContext *ctx)
5742 {
5743     /* XXX: TODO */
5744     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5745 }
5746 
5747 /* esa */
5748 static void gen_esa(DisasContext *ctx)
5749 {
5750     /* XXX: TODO */
5751     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5752 }
5753 
5754 /* mfrom */
5755 static void gen_mfrom(DisasContext *ctx)
5756 {
5757 #if defined(CONFIG_USER_ONLY)
5758     GEN_PRIV;
5759 #else
5760     CHK_SV;
5761     gen_helper_602_mfrom(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
5762 #endif /* defined(CONFIG_USER_ONLY) */
5763 }
5764 
5765 /* 602 - 603 - G2 TLB management */
5766 
5767 /* tlbld */
5768 static void gen_tlbld_6xx(DisasContext *ctx)
5769 {
5770 #if defined(CONFIG_USER_ONLY)
5771     GEN_PRIV;
5772 #else
5773     CHK_SV;
5774     gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5775 #endif /* defined(CONFIG_USER_ONLY) */
5776 }
5777 
5778 /* tlbli */
5779 static void gen_tlbli_6xx(DisasContext *ctx)
5780 {
5781 #if defined(CONFIG_USER_ONLY)
5782     GEN_PRIV;
5783 #else
5784     CHK_SV;
5785     gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5786 #endif /* defined(CONFIG_USER_ONLY) */
5787 }
5788 
5789 /* 74xx TLB management */
5790 
5791 /* tlbld */
5792 static void gen_tlbld_74xx(DisasContext *ctx)
5793 {
5794 #if defined(CONFIG_USER_ONLY)
5795     GEN_PRIV;
5796 #else
5797     CHK_SV;
5798     gen_helper_74xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5799 #endif /* defined(CONFIG_USER_ONLY) */
5800 }
5801 
5802 /* tlbli */
5803 static void gen_tlbli_74xx(DisasContext *ctx)
5804 {
5805 #if defined(CONFIG_USER_ONLY)
5806     GEN_PRIV;
5807 #else
5808     CHK_SV;
5809     gen_helper_74xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5810 #endif /* defined(CONFIG_USER_ONLY) */
5811 }
5812 
5813 /* POWER instructions not in PowerPC 601 */
5814 
5815 /* clf */
5816 static void gen_clf(DisasContext *ctx)
5817 {
5818     /* Cache line flush: implemented as no-op */
5819 }
5820 
5821 /* cli */
5822 static void gen_cli(DisasContext *ctx)
5823 {
5824 #if defined(CONFIG_USER_ONLY)
5825     GEN_PRIV;
5826 #else
5827     /* Cache line invalidate: privileged and treated as no-op */
5828     CHK_SV;
5829 #endif /* defined(CONFIG_USER_ONLY) */
5830 }
5831 
5832 /* dclst */
5833 static void gen_dclst(DisasContext *ctx)
5834 {
5835     /* Data cache line store: treated as no-op */
5836 }
5837 
5838 static void gen_mfsri(DisasContext *ctx)
5839 {
5840 #if defined(CONFIG_USER_ONLY)
5841     GEN_PRIV;
5842 #else
5843     int ra = rA(ctx->opcode);
5844     int rd = rD(ctx->opcode);
5845     TCGv t0;
5846 
5847     CHK_SV;
5848     t0 = tcg_temp_new();
5849     gen_addr_reg_index(ctx, t0);
5850     tcg_gen_extract_tl(t0, t0, 28, 4);
5851     gen_helper_load_sr(cpu_gpr[rd], cpu_env, t0);
5852     tcg_temp_free(t0);
5853     if (ra != 0 && ra != rd) {
5854         tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rd]);
5855     }
5856 #endif /* defined(CONFIG_USER_ONLY) */
5857 }
5858 
5859 static void gen_rac(DisasContext *ctx)
5860 {
5861 #if defined(CONFIG_USER_ONLY)
5862     GEN_PRIV;
5863 #else
5864     TCGv t0;
5865 
5866     CHK_SV;
5867     t0 = tcg_temp_new();
5868     gen_addr_reg_index(ctx, t0);
5869     gen_helper_rac(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5870     tcg_temp_free(t0);
5871 #endif /* defined(CONFIG_USER_ONLY) */
5872 }
5873 
5874 static void gen_rfsvc(DisasContext *ctx)
5875 {
5876 #if defined(CONFIG_USER_ONLY)
5877     GEN_PRIV;
5878 #else
5879     CHK_SV;
5880 
5881     gen_helper_rfsvc(cpu_env);
5882     gen_sync_exception(ctx);
5883 #endif /* defined(CONFIG_USER_ONLY) */
5884 }
5885 
5886 /* svc is not implemented for now */
5887 
5888 /* BookE specific instructions */
5889 
5890 /* XXX: not implemented on 440 ? */
5891 static void gen_mfapidi(DisasContext *ctx)
5892 {
5893     /* XXX: TODO */
5894     gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5895 }
5896 
5897 /* XXX: not implemented on 440 ? */
5898 static void gen_tlbiva(DisasContext *ctx)
5899 {
5900 #if defined(CONFIG_USER_ONLY)
5901     GEN_PRIV;
5902 #else
5903     TCGv t0;
5904 
5905     CHK_SV;
5906     t0 = tcg_temp_new();
5907     gen_addr_reg_index(ctx, t0);
5908     gen_helper_tlbiva(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5909     tcg_temp_free(t0);
5910 #endif /* defined(CONFIG_USER_ONLY) */
5911 }
5912 
5913 /* All 405 MAC instructions are translated here */
5914 static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5915                                         int ra, int rb, int rt, int Rc)
5916 {
5917     TCGv t0, t1;
5918 
5919     t0 = tcg_temp_local_new();
5920     t1 = tcg_temp_local_new();
5921 
5922     switch (opc3 & 0x0D) {
5923     case 0x05:
5924         /* macchw    - macchw.    - macchwo   - macchwo.   */
5925         /* macchws   - macchws.   - macchwso  - macchwso.  */
5926         /* nmacchw   - nmacchw.   - nmacchwo  - nmacchwo.  */
5927         /* nmacchws  - nmacchws.  - nmacchwso - nmacchwso. */
5928         /* mulchw - mulchw. */
5929         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5930         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5931         tcg_gen_ext16s_tl(t1, t1);
5932         break;
5933     case 0x04:
5934         /* macchwu   - macchwu.   - macchwuo  - macchwuo.  */
5935         /* macchwsu  - macchwsu.  - macchwsuo - macchwsuo. */
5936         /* mulchwu - mulchwu. */
5937         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5938         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5939         tcg_gen_ext16u_tl(t1, t1);
5940         break;
5941     case 0x01:
5942         /* machhw    - machhw.    - machhwo   - machhwo.   */
5943         /* machhws   - machhws.   - machhwso  - machhwso.  */
5944         /* nmachhw   - nmachhw.   - nmachhwo  - nmachhwo.  */
5945         /* nmachhws  - nmachhws.  - nmachhwso - nmachhwso. */
5946         /* mulhhw - mulhhw. */
5947         tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5948         tcg_gen_ext16s_tl(t0, t0);
5949         tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5950         tcg_gen_ext16s_tl(t1, t1);
5951         break;
5952     case 0x00:
5953         /* machhwu   - machhwu.   - machhwuo  - machhwuo.  */
5954         /* machhwsu  - machhwsu.  - machhwsuo - machhwsuo. */
5955         /* mulhhwu - mulhhwu. */
5956         tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5957         tcg_gen_ext16u_tl(t0, t0);
5958         tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5959         tcg_gen_ext16u_tl(t1, t1);
5960         break;
5961     case 0x0D:
5962         /* maclhw    - maclhw.    - maclhwo   - maclhwo.   */
5963         /* maclhws   - maclhws.   - maclhwso  - maclhwso.  */
5964         /* nmaclhw   - nmaclhw.   - nmaclhwo  - nmaclhwo.  */
5965         /* nmaclhws  - nmaclhws.  - nmaclhwso - nmaclhwso. */
5966         /* mullhw - mullhw. */
5967         tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5968         tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5969         break;
5970     case 0x0C:
5971         /* maclhwu   - maclhwu.   - maclhwuo  - maclhwuo.  */
5972         /* maclhwsu  - maclhwsu.  - maclhwsuo - maclhwsuo. */
5973         /* mullhwu - mullhwu. */
5974         tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5975         tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5976         break;
5977     }
5978     if (opc2 & 0x04) {
5979         /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5980         tcg_gen_mul_tl(t1, t0, t1);
5981         if (opc2 & 0x02) {
5982             /* nmultiply-and-accumulate (0x0E) */
5983             tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5984         } else {
5985             /* multiply-and-accumulate (0x0C) */
5986             tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5987         }
5988 
5989         if (opc3 & 0x12) {
5990             /* Check overflow and/or saturate */
5991             TCGLabel *l1 = gen_new_label();
5992 
5993             if (opc3 & 0x10) {
5994                 /* Start with XER OV disabled, the most likely case */
5995                 tcg_gen_movi_tl(cpu_ov, 0);
5996             }
5997             if (opc3 & 0x01) {
5998                 /* Signed */
5999                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
6000                 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
6001                 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
6002                 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
6003                 if (opc3 & 0x02) {
6004                     /* Saturate */
6005                     tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
6006                     tcg_gen_xori_tl(t0, t0, 0x7fffffff);
6007                 }
6008             } else {
6009                 /* Unsigned */
6010                 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
6011                 if (opc3 & 0x02) {
6012                     /* Saturate */
6013                     tcg_gen_movi_tl(t0, UINT32_MAX);
6014                 }
6015             }
6016             if (opc3 & 0x10) {
6017                 /* Check overflow */
6018                 tcg_gen_movi_tl(cpu_ov, 1);
6019                 tcg_gen_movi_tl(cpu_so, 1);
6020             }
6021             gen_set_label(l1);
6022             tcg_gen_mov_tl(cpu_gpr[rt], t0);
6023         }
6024     } else {
6025         tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
6026     }
6027     tcg_temp_free(t0);
6028     tcg_temp_free(t1);
6029     if (unlikely(Rc) != 0) {
6030         /* Update Rc0 */
6031         gen_set_Rc0(ctx, cpu_gpr[rt]);
6032     }
6033 }
6034 
6035 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
6036 static void glue(gen_, name)(DisasContext *ctx)                               \
6037 {                                                                             \
6038     gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode),   \
6039                          rD(ctx->opcode), Rc(ctx->opcode));                   \
6040 }
6041 
6042 /* macchw    - macchw.    */
6043 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
6044 /* macchwo   - macchwo.   */
6045 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
6046 /* macchws   - macchws.   */
6047 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
6048 /* macchwso  - macchwso.  */
6049 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
6050 /* macchwsu  - macchwsu.  */
6051 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
6052 /* macchwsuo - macchwsuo. */
6053 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
6054 /* macchwu   - macchwu.   */
6055 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
6056 /* macchwuo  - macchwuo.  */
6057 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
6058 /* machhw    - machhw.    */
6059 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
6060 /* machhwo   - machhwo.   */
6061 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
6062 /* machhws   - machhws.   */
6063 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
6064 /* machhwso  - machhwso.  */
6065 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
6066 /* machhwsu  - machhwsu.  */
6067 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
6068 /* machhwsuo - machhwsuo. */
6069 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
6070 /* machhwu   - machhwu.   */
6071 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
6072 /* machhwuo  - machhwuo.  */
6073 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
6074 /* maclhw    - maclhw.    */
6075 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
6076 /* maclhwo   - maclhwo.   */
6077 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
6078 /* maclhws   - maclhws.   */
6079 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
6080 /* maclhwso  - maclhwso.  */
6081 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
6082 /* maclhwu   - maclhwu.   */
6083 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
6084 /* maclhwuo  - maclhwuo.  */
6085 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
6086 /* maclhwsu  - maclhwsu.  */
6087 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
6088 /* maclhwsuo - maclhwsuo. */
6089 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
6090 /* nmacchw   - nmacchw.   */
6091 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
6092 /* nmacchwo  - nmacchwo.  */
6093 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
6094 /* nmacchws  - nmacchws.  */
6095 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
6096 /* nmacchwso - nmacchwso. */
6097 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
6098 /* nmachhw   - nmachhw.   */
6099 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
6100 /* nmachhwo  - nmachhwo.  */
6101 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
6102 /* nmachhws  - nmachhws.  */
6103 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
6104 /* nmachhwso - nmachhwso. */
6105 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
6106 /* nmaclhw   - nmaclhw.   */
6107 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
6108 /* nmaclhwo  - nmaclhwo.  */
6109 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
6110 /* nmaclhws  - nmaclhws.  */
6111 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
6112 /* nmaclhwso - nmaclhwso. */
6113 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
6114 
6115 /* mulchw  - mulchw.  */
6116 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
6117 /* mulchwu - mulchwu. */
6118 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
6119 /* mulhhw  - mulhhw.  */
6120 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
6121 /* mulhhwu - mulhhwu. */
6122 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
6123 /* mullhw  - mullhw.  */
6124 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
6125 /* mullhwu - mullhwu. */
6126 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
6127 
6128 /* mfdcr */
6129 static void gen_mfdcr(DisasContext *ctx)
6130 {
6131 #if defined(CONFIG_USER_ONLY)
6132     GEN_PRIV;
6133 #else
6134     TCGv dcrn;
6135 
6136     CHK_SV;
6137     dcrn = tcg_const_tl(SPR(ctx->opcode));
6138     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
6139     tcg_temp_free(dcrn);
6140 #endif /* defined(CONFIG_USER_ONLY) */
6141 }
6142 
6143 /* mtdcr */
6144 static void gen_mtdcr(DisasContext *ctx)
6145 {
6146 #if defined(CONFIG_USER_ONLY)
6147     GEN_PRIV;
6148 #else
6149     TCGv dcrn;
6150 
6151     CHK_SV;
6152     dcrn = tcg_const_tl(SPR(ctx->opcode));
6153     gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
6154     tcg_temp_free(dcrn);
6155 #endif /* defined(CONFIG_USER_ONLY) */
6156 }
6157 
6158 /* mfdcrx */
6159 /* XXX: not implemented on 440 ? */
6160 static void gen_mfdcrx(DisasContext *ctx)
6161 {
6162 #if defined(CONFIG_USER_ONLY)
6163     GEN_PRIV;
6164 #else
6165     CHK_SV;
6166     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6167                         cpu_gpr[rA(ctx->opcode)]);
6168     /* Note: Rc update flag set leads to undefined state of Rc0 */
6169 #endif /* defined(CONFIG_USER_ONLY) */
6170 }
6171 
6172 /* mtdcrx */
6173 /* XXX: not implemented on 440 ? */
6174 static void gen_mtdcrx(DisasContext *ctx)
6175 {
6176 #if defined(CONFIG_USER_ONLY)
6177     GEN_PRIV;
6178 #else
6179     CHK_SV;
6180     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6181                          cpu_gpr[rS(ctx->opcode)]);
6182     /* Note: Rc update flag set leads to undefined state of Rc0 */
6183 #endif /* defined(CONFIG_USER_ONLY) */
6184 }
6185 
6186 /* mfdcrux (PPC 460) : user-mode access to DCR */
6187 static void gen_mfdcrux(DisasContext *ctx)
6188 {
6189     gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
6190                         cpu_gpr[rA(ctx->opcode)]);
6191     /* Note: Rc update flag set leads to undefined state of Rc0 */
6192 }
6193 
6194 /* mtdcrux (PPC 460) : user-mode access to DCR */
6195 static void gen_mtdcrux(DisasContext *ctx)
6196 {
6197     gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
6198                          cpu_gpr[rS(ctx->opcode)]);
6199     /* Note: Rc update flag set leads to undefined state of Rc0 */
6200 }
6201 
6202 /* dccci */
6203 static void gen_dccci(DisasContext *ctx)
6204 {
6205     CHK_SV;
6206     /* interpreted as no-op */
6207 }
6208 
6209 /* dcread */
6210 static void gen_dcread(DisasContext *ctx)
6211 {
6212 #if defined(CONFIG_USER_ONLY)
6213     GEN_PRIV;
6214 #else
6215     TCGv EA, val;
6216 
6217     CHK_SV;
6218     gen_set_access_type(ctx, ACCESS_CACHE);
6219     EA = tcg_temp_new();
6220     gen_addr_reg_index(ctx, EA);
6221     val = tcg_temp_new();
6222     gen_qemu_ld32u(ctx, val, EA);
6223     tcg_temp_free(val);
6224     tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
6225     tcg_temp_free(EA);
6226 #endif /* defined(CONFIG_USER_ONLY) */
6227 }
6228 
6229 /* icbt */
6230 static void gen_icbt_40x(DisasContext *ctx)
6231 {
6232     /*
6233      * interpreted as no-op
6234      * XXX: specification say this is treated as a load by the MMU but
6235      *      does not generate any exception
6236      */
6237 }
6238 
6239 /* iccci */
6240 static void gen_iccci(DisasContext *ctx)
6241 {
6242     CHK_SV;
6243     /* interpreted as no-op */
6244 }
6245 
6246 /* icread */
6247 static void gen_icread(DisasContext *ctx)
6248 {
6249     CHK_SV;
6250     /* interpreted as no-op */
6251 }
6252 
6253 /* rfci (supervisor only) */
6254 static void gen_rfci_40x(DisasContext *ctx)
6255 {
6256 #if defined(CONFIG_USER_ONLY)
6257     GEN_PRIV;
6258 #else
6259     CHK_SV;
6260     /* Restore CPU state */
6261     gen_helper_40x_rfci(cpu_env);
6262     gen_sync_exception(ctx);
6263 #endif /* defined(CONFIG_USER_ONLY) */
6264 }
6265 
6266 static void gen_rfci(DisasContext *ctx)
6267 {
6268 #if defined(CONFIG_USER_ONLY)
6269     GEN_PRIV;
6270 #else
6271     CHK_SV;
6272     /* Restore CPU state */
6273     gen_helper_rfci(cpu_env);
6274     gen_sync_exception(ctx);
6275 #endif /* defined(CONFIG_USER_ONLY) */
6276 }
6277 
6278 /* BookE specific */
6279 
6280 /* XXX: not implemented on 440 ? */
6281 static void gen_rfdi(DisasContext *ctx)
6282 {
6283 #if defined(CONFIG_USER_ONLY)
6284     GEN_PRIV;
6285 #else
6286     CHK_SV;
6287     /* Restore CPU state */
6288     gen_helper_rfdi(cpu_env);
6289     gen_sync_exception(ctx);
6290 #endif /* defined(CONFIG_USER_ONLY) */
6291 }
6292 
6293 /* XXX: not implemented on 440 ? */
6294 static void gen_rfmci(DisasContext *ctx)
6295 {
6296 #if defined(CONFIG_USER_ONLY)
6297     GEN_PRIV;
6298 #else
6299     CHK_SV;
6300     /* Restore CPU state */
6301     gen_helper_rfmci(cpu_env);
6302     gen_sync_exception(ctx);
6303 #endif /* defined(CONFIG_USER_ONLY) */
6304 }
6305 
6306 /* TLB management - PowerPC 405 implementation */
6307 
6308 /* tlbre */
6309 static void gen_tlbre_40x(DisasContext *ctx)
6310 {
6311 #if defined(CONFIG_USER_ONLY)
6312     GEN_PRIV;
6313 #else
6314     CHK_SV;
6315     switch (rB(ctx->opcode)) {
6316     case 0:
6317         gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
6318                                 cpu_gpr[rA(ctx->opcode)]);
6319         break;
6320     case 1:
6321         gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
6322                                 cpu_gpr[rA(ctx->opcode)]);
6323         break;
6324     default:
6325         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6326         break;
6327     }
6328 #endif /* defined(CONFIG_USER_ONLY) */
6329 }
6330 
6331 /* tlbsx - tlbsx. */
6332 static void gen_tlbsx_40x(DisasContext *ctx)
6333 {
6334 #if defined(CONFIG_USER_ONLY)
6335     GEN_PRIV;
6336 #else
6337     TCGv t0;
6338 
6339     CHK_SV;
6340     t0 = tcg_temp_new();
6341     gen_addr_reg_index(ctx, t0);
6342     gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6343     tcg_temp_free(t0);
6344     if (Rc(ctx->opcode)) {
6345         TCGLabel *l1 = gen_new_label();
6346         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6347         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6348         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6349         gen_set_label(l1);
6350     }
6351 #endif /* defined(CONFIG_USER_ONLY) */
6352 }
6353 
6354 /* tlbwe */
6355 static void gen_tlbwe_40x(DisasContext *ctx)
6356 {
6357 #if defined(CONFIG_USER_ONLY)
6358     GEN_PRIV;
6359 #else
6360     CHK_SV;
6361 
6362     switch (rB(ctx->opcode)) {
6363     case 0:
6364         gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6365                                 cpu_gpr[rS(ctx->opcode)]);
6366         break;
6367     case 1:
6368         gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6369                                 cpu_gpr[rS(ctx->opcode)]);
6370         break;
6371     default:
6372         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6373         break;
6374     }
6375 #endif /* defined(CONFIG_USER_ONLY) */
6376 }
6377 
6378 /* TLB management - PowerPC 440 implementation */
6379 
6380 /* tlbre */
6381 static void gen_tlbre_440(DisasContext *ctx)
6382 {
6383 #if defined(CONFIG_USER_ONLY)
6384     GEN_PRIV;
6385 #else
6386     CHK_SV;
6387 
6388     switch (rB(ctx->opcode)) {
6389     case 0:
6390     case 1:
6391     case 2:
6392         {
6393             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6394             gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6395                                  t0, cpu_gpr[rA(ctx->opcode)]);
6396             tcg_temp_free_i32(t0);
6397         }
6398         break;
6399     default:
6400         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6401         break;
6402     }
6403 #endif /* defined(CONFIG_USER_ONLY) */
6404 }
6405 
6406 /* tlbsx - tlbsx. */
6407 static void gen_tlbsx_440(DisasContext *ctx)
6408 {
6409 #if defined(CONFIG_USER_ONLY)
6410     GEN_PRIV;
6411 #else
6412     TCGv t0;
6413 
6414     CHK_SV;
6415     t0 = tcg_temp_new();
6416     gen_addr_reg_index(ctx, t0);
6417     gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6418     tcg_temp_free(t0);
6419     if (Rc(ctx->opcode)) {
6420         TCGLabel *l1 = gen_new_label();
6421         tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6422         tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6423         tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6424         gen_set_label(l1);
6425     }
6426 #endif /* defined(CONFIG_USER_ONLY) */
6427 }
6428 
6429 /* tlbwe */
6430 static void gen_tlbwe_440(DisasContext *ctx)
6431 {
6432 #if defined(CONFIG_USER_ONLY)
6433     GEN_PRIV;
6434 #else
6435     CHK_SV;
6436     switch (rB(ctx->opcode)) {
6437     case 0:
6438     case 1:
6439     case 2:
6440         {
6441             TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6442             gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6443                                  cpu_gpr[rS(ctx->opcode)]);
6444             tcg_temp_free_i32(t0);
6445         }
6446         break;
6447     default:
6448         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6449         break;
6450     }
6451 #endif /* defined(CONFIG_USER_ONLY) */
6452 }
6453 
6454 /* TLB management - PowerPC BookE 2.06 implementation */
6455 
6456 /* tlbre */
6457 static void gen_tlbre_booke206(DisasContext *ctx)
6458 {
6459  #if defined(CONFIG_USER_ONLY)
6460     GEN_PRIV;
6461 #else
6462    CHK_SV;
6463     gen_helper_booke206_tlbre(cpu_env);
6464 #endif /* defined(CONFIG_USER_ONLY) */
6465 }
6466 
6467 /* tlbsx - tlbsx. */
6468 static void gen_tlbsx_booke206(DisasContext *ctx)
6469 {
6470 #if defined(CONFIG_USER_ONLY)
6471     GEN_PRIV;
6472 #else
6473     TCGv t0;
6474 
6475     CHK_SV;
6476     if (rA(ctx->opcode)) {
6477         t0 = tcg_temp_new();
6478         tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6479     } else {
6480         t0 = tcg_const_tl(0);
6481     }
6482 
6483     tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6484     gen_helper_booke206_tlbsx(cpu_env, t0);
6485     tcg_temp_free(t0);
6486 #endif /* defined(CONFIG_USER_ONLY) */
6487 }
6488 
6489 /* tlbwe */
6490 static void gen_tlbwe_booke206(DisasContext *ctx)
6491 {
6492 #if defined(CONFIG_USER_ONLY)
6493     GEN_PRIV;
6494 #else
6495     CHK_SV;
6496     gen_helper_booke206_tlbwe(cpu_env);
6497 #endif /* defined(CONFIG_USER_ONLY) */
6498 }
6499 
6500 static void gen_tlbivax_booke206(DisasContext *ctx)
6501 {
6502 #if defined(CONFIG_USER_ONLY)
6503     GEN_PRIV;
6504 #else
6505     TCGv t0;
6506 
6507     CHK_SV;
6508     t0 = tcg_temp_new();
6509     gen_addr_reg_index(ctx, t0);
6510     gen_helper_booke206_tlbivax(cpu_env, t0);
6511     tcg_temp_free(t0);
6512 #endif /* defined(CONFIG_USER_ONLY) */
6513 }
6514 
6515 static void gen_tlbilx_booke206(DisasContext *ctx)
6516 {
6517 #if defined(CONFIG_USER_ONLY)
6518     GEN_PRIV;
6519 #else
6520     TCGv t0;
6521 
6522     CHK_SV;
6523     t0 = tcg_temp_new();
6524     gen_addr_reg_index(ctx, t0);
6525 
6526     switch ((ctx->opcode >> 21) & 0x3) {
6527     case 0:
6528         gen_helper_booke206_tlbilx0(cpu_env, t0);
6529         break;
6530     case 1:
6531         gen_helper_booke206_tlbilx1(cpu_env, t0);
6532         break;
6533     case 3:
6534         gen_helper_booke206_tlbilx3(cpu_env, t0);
6535         break;
6536     default:
6537         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6538         break;
6539     }
6540 
6541     tcg_temp_free(t0);
6542 #endif /* defined(CONFIG_USER_ONLY) */
6543 }
6544 
6545 
6546 /* wrtee */
6547 static void gen_wrtee(DisasContext *ctx)
6548 {
6549 #if defined(CONFIG_USER_ONLY)
6550     GEN_PRIV;
6551 #else
6552     TCGv t0;
6553 
6554     CHK_SV;
6555     t0 = tcg_temp_new();
6556     tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6557     tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6558     tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6559     tcg_temp_free(t0);
6560     /*
6561      * Stop translation to have a chance to raise an exception if we
6562      * just set msr_ee to 1
6563      */
6564     gen_stop_exception(ctx);
6565 #endif /* defined(CONFIG_USER_ONLY) */
6566 }
6567 
6568 /* wrteei */
6569 static void gen_wrteei(DisasContext *ctx)
6570 {
6571 #if defined(CONFIG_USER_ONLY)
6572     GEN_PRIV;
6573 #else
6574     CHK_SV;
6575     if (ctx->opcode & 0x00008000) {
6576         tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6577         /* Stop translation to have a chance to raise an exception */
6578         gen_stop_exception(ctx);
6579     } else {
6580         tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6581     }
6582 #endif /* defined(CONFIG_USER_ONLY) */
6583 }
6584 
6585 /* PowerPC 440 specific instructions */
6586 
6587 /* dlmzb */
6588 static void gen_dlmzb(DisasContext *ctx)
6589 {
6590     TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6591     gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6592                      cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6593     tcg_temp_free_i32(t0);
6594 }
6595 
6596 /* mbar replaces eieio on 440 */
6597 static void gen_mbar(DisasContext *ctx)
6598 {
6599     /* interpreted as no-op */
6600 }
6601 
6602 /* msync replaces sync on 440 */
6603 static void gen_msync_4xx(DisasContext *ctx)
6604 {
6605     /* Only e500 seems to treat reserved bits as invalid */
6606     if ((ctx->insns_flags2 & PPC2_BOOKE206) &&
6607         (ctx->opcode & 0x03FFF801)) {
6608         gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6609     }
6610     /* otherwise interpreted as no-op */
6611 }
6612 
6613 /* icbt */
6614 static void gen_icbt_440(DisasContext *ctx)
6615 {
6616     /*
6617      * interpreted as no-op
6618      * XXX: specification say this is treated as a load by the MMU but
6619      *      does not generate any exception
6620      */
6621 }
6622 
6623 /* Embedded.Processor Control */
6624 
6625 static void gen_msgclr(DisasContext *ctx)
6626 {
6627 #if defined(CONFIG_USER_ONLY)
6628     GEN_PRIV;
6629 #else
6630     CHK_HV;
6631     if (is_book3s_arch2x(ctx)) {
6632         gen_helper_book3s_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6633     } else {
6634         gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6635     }
6636 #endif /* defined(CONFIG_USER_ONLY) */
6637 }
6638 
6639 static void gen_msgsnd(DisasContext *ctx)
6640 {
6641 #if defined(CONFIG_USER_ONLY)
6642     GEN_PRIV;
6643 #else
6644     CHK_HV;
6645     if (is_book3s_arch2x(ctx)) {
6646         gen_helper_book3s_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6647     } else {
6648         gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6649     }
6650 #endif /* defined(CONFIG_USER_ONLY) */
6651 }
6652 
6653 #if defined(TARGET_PPC64)
6654 static void gen_msgclrp(DisasContext *ctx)
6655 {
6656 #if defined(CONFIG_USER_ONLY)
6657     GEN_PRIV;
6658 #else
6659     CHK_SV;
6660     gen_helper_book3s_msgclrp(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6661 #endif /* defined(CONFIG_USER_ONLY) */
6662 }
6663 
6664 static void gen_msgsndp(DisasContext *ctx)
6665 {
6666 #if defined(CONFIG_USER_ONLY)
6667     GEN_PRIV;
6668 #else
6669     CHK_SV;
6670     gen_helper_book3s_msgsndp(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6671 #endif /* defined(CONFIG_USER_ONLY) */
6672 }
6673 #endif
6674 
6675 static void gen_msgsync(DisasContext *ctx)
6676 {
6677 #if defined(CONFIG_USER_ONLY)
6678     GEN_PRIV;
6679 #else
6680     CHK_HV;
6681 #endif /* defined(CONFIG_USER_ONLY) */
6682     /* interpreted as no-op */
6683 }
6684 
6685 #if defined(TARGET_PPC64)
6686 static void gen_maddld(DisasContext *ctx)
6687 {
6688     TCGv_i64 t1 = tcg_temp_new_i64();
6689 
6690     tcg_gen_mul_i64(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6691     tcg_gen_add_i64(cpu_gpr[rD(ctx->opcode)], t1, cpu_gpr[rC(ctx->opcode)]);
6692     tcg_temp_free_i64(t1);
6693 }
6694 
6695 /* maddhd maddhdu */
6696 static void gen_maddhd_maddhdu(DisasContext *ctx)
6697 {
6698     TCGv_i64 lo = tcg_temp_new_i64();
6699     TCGv_i64 hi = tcg_temp_new_i64();
6700     TCGv_i64 t1 = tcg_temp_new_i64();
6701 
6702     if (Rc(ctx->opcode)) {
6703         tcg_gen_mulu2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6704                           cpu_gpr[rB(ctx->opcode)]);
6705         tcg_gen_movi_i64(t1, 0);
6706     } else {
6707         tcg_gen_muls2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6708                           cpu_gpr[rB(ctx->opcode)]);
6709         tcg_gen_sari_i64(t1, cpu_gpr[rC(ctx->opcode)], 63);
6710     }
6711     tcg_gen_add2_i64(t1, cpu_gpr[rD(ctx->opcode)], lo, hi,
6712                      cpu_gpr[rC(ctx->opcode)], t1);
6713     tcg_temp_free_i64(lo);
6714     tcg_temp_free_i64(hi);
6715     tcg_temp_free_i64(t1);
6716 }
6717 #endif /* defined(TARGET_PPC64) */
6718 
6719 static void gen_tbegin(DisasContext *ctx)
6720 {
6721     if (unlikely(!ctx->tm_enabled)) {
6722         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6723         return;
6724     }
6725     gen_helper_tbegin(cpu_env);
6726 }
6727 
6728 #define GEN_TM_NOOP(name)                                      \
6729 static inline void gen_##name(DisasContext *ctx)               \
6730 {                                                              \
6731     if (unlikely(!ctx->tm_enabled)) {                          \
6732         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
6733         return;                                                \
6734     }                                                          \
6735     /*                                                         \
6736      * Because tbegin always fails in QEMU, these user         \
6737      * space instructions all have a simple implementation:    \
6738      *                                                         \
6739      *     CR[0] = 0b0 || MSR[TS] || 0b0                       \
6740      *           = 0b0 || 0b00    || 0b0                       \
6741      */                                                        \
6742     tcg_gen_movi_i32(cpu_crf[0], 0);                           \
6743 }
6744 
6745 GEN_TM_NOOP(tend);
6746 GEN_TM_NOOP(tabort);
6747 GEN_TM_NOOP(tabortwc);
6748 GEN_TM_NOOP(tabortwci);
6749 GEN_TM_NOOP(tabortdc);
6750 GEN_TM_NOOP(tabortdci);
6751 GEN_TM_NOOP(tsr);
6752 
6753 static inline void gen_cp_abort(DisasContext *ctx)
6754 {
6755     /* Do Nothing */
6756 }
6757 
6758 #define GEN_CP_PASTE_NOOP(name)                           \
6759 static inline void gen_##name(DisasContext *ctx)          \
6760 {                                                         \
6761     /*                                                    \
6762      * Generate invalid exception until we have an        \
6763      * implementation of the copy paste facility          \
6764      */                                                   \
6765     gen_invalid(ctx);                                     \
6766 }
6767 
6768 GEN_CP_PASTE_NOOP(copy)
6769 GEN_CP_PASTE_NOOP(paste)
6770 
6771 static void gen_tcheck(DisasContext *ctx)
6772 {
6773     if (unlikely(!ctx->tm_enabled)) {
6774         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6775         return;
6776     }
6777     /*
6778      * Because tbegin always fails, the tcheck implementation is
6779      * simple:
6780      *
6781      * CR[CRF] = TDOOMED || MSR[TS] || 0b0
6782      *         = 0b1 || 0b00 || 0b0
6783      */
6784     tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0x8);
6785 }
6786 
6787 #if defined(CONFIG_USER_ONLY)
6788 #define GEN_TM_PRIV_NOOP(name)                                 \
6789 static inline void gen_##name(DisasContext *ctx)               \
6790 {                                                              \
6791     gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);            \
6792 }
6793 
6794 #else
6795 
6796 #define GEN_TM_PRIV_NOOP(name)                                 \
6797 static inline void gen_##name(DisasContext *ctx)               \
6798 {                                                              \
6799     CHK_SV;                                                    \
6800     if (unlikely(!ctx->tm_enabled)) {                          \
6801         gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);   \
6802         return;                                                \
6803     }                                                          \
6804     /*                                                         \
6805      * Because tbegin always fails, the implementation is      \
6806      * simple:                                                 \
6807      *                                                         \
6808      *   CR[0] = 0b0 || MSR[TS] || 0b0                         \
6809      *         = 0b0 || 0b00 | 0b0                             \
6810      */                                                        \
6811     tcg_gen_movi_i32(cpu_crf[0], 0);                           \
6812 }
6813 
6814 #endif
6815 
6816 GEN_TM_PRIV_NOOP(treclaim);
6817 GEN_TM_PRIV_NOOP(trechkpt);
6818 
6819 static inline void get_fpr(TCGv_i64 dst, int regno)
6820 {
6821     tcg_gen_ld_i64(dst, cpu_env, fpr_offset(regno));
6822 }
6823 
6824 static inline void set_fpr(int regno, TCGv_i64 src)
6825 {
6826     tcg_gen_st_i64(src, cpu_env, fpr_offset(regno));
6827 }
6828 
6829 static inline void get_avr64(TCGv_i64 dst, int regno, bool high)
6830 {
6831     tcg_gen_ld_i64(dst, cpu_env, avr64_offset(regno, high));
6832 }
6833 
6834 static inline void set_avr64(int regno, TCGv_i64 src, bool high)
6835 {
6836     tcg_gen_st_i64(src, cpu_env, avr64_offset(regno, high));
6837 }
6838 
6839 #include "translate/fp-impl.inc.c"
6840 
6841 #include "translate/vmx-impl.inc.c"
6842 
6843 #include "translate/vsx-impl.inc.c"
6844 
6845 #include "translate/dfp-impl.inc.c"
6846 
6847 #include "translate/spe-impl.inc.c"
6848 
6849 /* Handles lfdp, lxsd, lxssp */
6850 static void gen_dform39(DisasContext *ctx)
6851 {
6852     switch (ctx->opcode & 0x3) {
6853     case 0: /* lfdp */
6854         if (ctx->insns_flags2 & PPC2_ISA205) {
6855             return gen_lfdp(ctx);
6856         }
6857         break;
6858     case 2: /* lxsd */
6859         if (ctx->insns_flags2 & PPC2_ISA300) {
6860             return gen_lxsd(ctx);
6861         }
6862         break;
6863     case 3: /* lxssp */
6864         if (ctx->insns_flags2 & PPC2_ISA300) {
6865             return gen_lxssp(ctx);
6866         }
6867         break;
6868     }
6869     return gen_invalid(ctx);
6870 }
6871 
6872 /* handles stfdp, lxv, stxsd, stxssp lxvx */
6873 static void gen_dform3D(DisasContext *ctx)
6874 {
6875     if ((ctx->opcode & 3) == 1) { /* DQ-FORM */
6876         switch (ctx->opcode & 0x7) {
6877         case 1: /* lxv */
6878             if (ctx->insns_flags2 & PPC2_ISA300) {
6879                 return gen_lxv(ctx);
6880             }
6881             break;
6882         case 5: /* stxv */
6883             if (ctx->insns_flags2 & PPC2_ISA300) {
6884                 return gen_stxv(ctx);
6885             }
6886             break;
6887         }
6888     } else { /* DS-FORM */
6889         switch (ctx->opcode & 0x3) {
6890         case 0: /* stfdp */
6891             if (ctx->insns_flags2 & PPC2_ISA205) {
6892                 return gen_stfdp(ctx);
6893             }
6894             break;
6895         case 2: /* stxsd */
6896             if (ctx->insns_flags2 & PPC2_ISA300) {
6897                 return gen_stxsd(ctx);
6898             }
6899             break;
6900         case 3: /* stxssp */
6901             if (ctx->insns_flags2 & PPC2_ISA300) {
6902                 return gen_stxssp(ctx);
6903             }
6904             break;
6905         }
6906     }
6907     return gen_invalid(ctx);
6908 }
6909 
6910 static opcode_t opcodes[] = {
6911 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
6912 GEN_HANDLER(cmp, 0x1F, 0x00, 0x00, 0x00400000, PPC_INTEGER),
6913 GEN_HANDLER(cmpi, 0x0B, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
6914 GEN_HANDLER(cmpl, 0x1F, 0x00, 0x01, 0x00400001, PPC_INTEGER),
6915 GEN_HANDLER(cmpli, 0x0A, 0xFF, 0xFF, 0x00400000, PPC_INTEGER),
6916 #if defined(TARGET_PPC64)
6917 GEN_HANDLER_E(cmpeqb, 0x1F, 0x00, 0x07, 0x00600000, PPC_NONE, PPC2_ISA300),
6918 #endif
6919 GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
6920 GEN_HANDLER_E(cmprb, 0x1F, 0x00, 0x06, 0x00400001, PPC_NONE, PPC2_ISA300),
6921 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
6922 GEN_HANDLER(addi, 0x0E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6923 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6924 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6925 GEN_HANDLER(addis, 0x0F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6926 GEN_HANDLER_E(addpcis, 0x13, 0x2, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
6927 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
6928 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
6929 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
6930 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
6931 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6932 #if defined(TARGET_PPC64)
6933 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
6934 #endif
6935 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
6936 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
6937 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6938 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6939 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6940 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
6941 GEN_HANDLER_E(cnttzw, 0x1F, 0x1A, 0x10, 0x00000000, PPC_NONE, PPC2_ISA300),
6942 GEN_HANDLER_E(copy, 0x1F, 0x06, 0x18, 0x03C00001, PPC_NONE, PPC2_ISA300),
6943 GEN_HANDLER_E(cp_abort, 0x1F, 0x06, 0x1A, 0x03FFF801, PPC_NONE, PPC2_ISA300),
6944 GEN_HANDLER_E(paste, 0x1F, 0x06, 0x1C, 0x03C00000, PPC_NONE, PPC2_ISA300),
6945 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
6946 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
6947 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6948 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6949 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6950 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6951 GEN_HANDLER(popcntb, 0x1F, 0x1A, 0x03, 0x0000F801, PPC_POPCNTB),
6952 GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
6953 GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
6954 #if defined(TARGET_PPC64)
6955 GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
6956 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
6957 GEN_HANDLER_E(cnttzd, 0x1F, 0x1A, 0x11, 0x00000000, PPC_NONE, PPC2_ISA300),
6958 GEN_HANDLER_E(darn, 0x1F, 0x13, 0x17, 0x001CF801, PPC_NONE, PPC2_ISA300),
6959 GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
6960 GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
6961 #endif
6962 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6963 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6964 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6965 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
6966 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
6967 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
6968 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
6969 #if defined(TARGET_PPC64)
6970 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
6971 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
6972 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
6973 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
6974 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
6975 GEN_HANDLER2_E(extswsli0, "extswsli", 0x1F, 0x1A, 0x1B, 0x00000000,
6976                PPC_NONE, PPC2_ISA300),
6977 GEN_HANDLER2_E(extswsli1, "extswsli", 0x1F, 0x1B, 0x1B, 0x00000000,
6978                PPC_NONE, PPC2_ISA300),
6979 #endif
6980 #if defined(TARGET_PPC64)
6981 GEN_HANDLER(ld, 0x3A, 0xFF, 0xFF, 0x00000000, PPC_64B),
6982 GEN_HANDLER(lq, 0x38, 0xFF, 0xFF, 0x00000000, PPC_64BX),
6983 GEN_HANDLER(std, 0x3E, 0xFF, 0xFF, 0x00000000, PPC_64B),
6984 #endif
6985 /* handles lfdp, lxsd, lxssp */
6986 GEN_HANDLER_E(dform39, 0x39, 0xFF, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA205),
6987 /* handles stfdp, lxv, stxsd, stxssp, stxv */
6988 GEN_HANDLER_E(dform3D, 0x3D, 0xFF, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA205),
6989 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6990 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6991 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
6992 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
6993 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
6994 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
6995 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x01FFF801, PPC_MEM_EIEIO),
6996 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
6997 GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6998 GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6999 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
7000 GEN_HANDLER_E(lwat, 0x1F, 0x06, 0x12, 0x00000001, PPC_NONE, PPC2_ISA300),
7001 GEN_HANDLER_E(stwat, 0x1F, 0x06, 0x16, 0x00000001, PPC_NONE, PPC2_ISA300),
7002 GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
7003 GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
7004 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
7005 #if defined(TARGET_PPC64)
7006 GEN_HANDLER_E(ldat, 0x1F, 0x06, 0x13, 0x00000001, PPC_NONE, PPC2_ISA300),
7007 GEN_HANDLER_E(stdat, 0x1F, 0x06, 0x17, 0x00000001, PPC_NONE, PPC2_ISA300),
7008 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
7009 GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
7010 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
7011 GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
7012 #endif
7013 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
7014 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
7015 GEN_HANDLER_E(wait, 0x1F, 0x1E, 0x00, 0x039FF801, PPC_NONE, PPC2_ISA300),
7016 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
7017 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
7018 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
7019 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
7020 GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0x0000E000, PPC_NONE, PPC2_BCTAR_ISA207),
7021 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
7022 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
7023 #if defined(TARGET_PPC64)
7024 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
7025 GEN_HANDLER_E(stop, 0x13, 0x12, 0x0b, 0x03FFF801, PPC_NONE, PPC2_ISA300),
7026 GEN_HANDLER_E(doze, 0x13, 0x12, 0x0c, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7027 GEN_HANDLER_E(nap, 0x13, 0x12, 0x0d, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7028 GEN_HANDLER_E(sleep, 0x13, 0x12, 0x0e, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7029 GEN_HANDLER_E(rvwinkle, 0x13, 0x12, 0x0f, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
7030 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
7031 #endif
7032 GEN_HANDLER(sc, 0x11, 0xFF, 0xFF, 0x03FFF01D, PPC_FLOW),
7033 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
7034 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
7035 #if defined(TARGET_PPC64)
7036 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
7037 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
7038 #endif
7039 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
7040 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
7041 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
7042 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
7043 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
7044 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
7045 #if defined(TARGET_PPC64)
7046 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
7047 GEN_HANDLER_E(setb, 0x1F, 0x00, 0x04, 0x0003F801, PPC_NONE, PPC2_ISA300),
7048 GEN_HANDLER_E(mcrxrx, 0x1F, 0x00, 0x12, 0x007FF801, PPC_NONE, PPC2_ISA300),
7049 #endif
7050 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001EF801, PPC_MISC),
7051 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000000, PPC_MISC),
7052 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
7053 GEN_HANDLER_E(dcbfep, 0x1F, 0x1F, 0x03, 0x03C00001, PPC_NONE, PPC2_BOOKE206),
7054 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
7055 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
7056 GEN_HANDLER_E(dcbstep, 0x1F, 0x1F, 0x01, 0x03E00001, PPC_NONE, PPC2_BOOKE206),
7057 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
7058 GEN_HANDLER_E(dcbtep, 0x1F, 0x1F, 0x09, 0x00000001, PPC_NONE, PPC2_BOOKE206),
7059 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
7060 GEN_HANDLER_E(dcbtstep, 0x1F, 0x1F, 0x07, 0x00000001, PPC_NONE, PPC2_BOOKE206),
7061 GEN_HANDLER_E(dcbtls, 0x1F, 0x06, 0x05, 0x02000001, PPC_BOOKE, PPC2_BOOKE206),
7062 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
7063 GEN_HANDLER_E(dcbzep, 0x1F, 0x1F, 0x1F, 0x03C00001, PPC_NONE, PPC2_BOOKE206),
7064 GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
7065 GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x01800001, PPC_ALTIVEC),
7066 GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
7067 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
7068 GEN_HANDLER_E(icbiep, 0x1F, 0x1F, 0x1E, 0x03E00001, PPC_NONE, PPC2_BOOKE206),
7069 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
7070 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
7071 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
7072 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
7073 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
7074 #if defined(TARGET_PPC64)
7075 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
7076 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
7077              PPC_SEGMENT_64B),
7078 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
7079 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
7080              PPC_SEGMENT_64B),
7081 GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
7082 GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
7083 GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
7084 GEN_HANDLER2(slbfee_, "slbfee.", 0x1F, 0x13, 0x1E, 0x001F0000, PPC_SEGMENT_64B),
7085 #endif
7086 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
7087 /*
7088  * XXX Those instructions will need to be handled differently for
7089  * different ISA versions
7090  */
7091 GEN_HANDLER(tlbiel, 0x1F, 0x12, 0x08, 0x001F0001, PPC_MEM_TLBIE),
7092 GEN_HANDLER(tlbie, 0x1F, 0x12, 0x09, 0x001F0001, PPC_MEM_TLBIE),
7093 GEN_HANDLER_E(tlbiel, 0x1F, 0x12, 0x08, 0x00100001, PPC_NONE, PPC2_ISA300),
7094 GEN_HANDLER_E(tlbie, 0x1F, 0x12, 0x09, 0x00100001, PPC_NONE, PPC2_ISA300),
7095 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
7096 #if defined(TARGET_PPC64)
7097 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x031FFC01, PPC_SLBI),
7098 GEN_HANDLER(slbie, 0x1F, 0x12, 0x0D, 0x03FF0001, PPC_SLBI),
7099 GEN_HANDLER_E(slbieg, 0x1F, 0x12, 0x0E, 0x001F0001, PPC_NONE, PPC2_ISA300),
7100 GEN_HANDLER_E(slbsync, 0x1F, 0x12, 0x0A, 0x03FFF801, PPC_NONE, PPC2_ISA300),
7101 #endif
7102 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
7103 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
7104 GEN_HANDLER(abs, 0x1F, 0x08, 0x0B, 0x0000F800, PPC_POWER_BR),
7105 GEN_HANDLER(abso, 0x1F, 0x08, 0x1B, 0x0000F800, PPC_POWER_BR),
7106 GEN_HANDLER(clcs, 0x1F, 0x10, 0x13, 0x0000F800, PPC_POWER_BR),
7107 GEN_HANDLER(div, 0x1F, 0x0B, 0x0A, 0x00000000, PPC_POWER_BR),
7108 GEN_HANDLER(divo, 0x1F, 0x0B, 0x1A, 0x00000000, PPC_POWER_BR),
7109 GEN_HANDLER(divs, 0x1F, 0x0B, 0x0B, 0x00000000, PPC_POWER_BR),
7110 GEN_HANDLER(divso, 0x1F, 0x0B, 0x1B, 0x00000000, PPC_POWER_BR),
7111 GEN_HANDLER(doz, 0x1F, 0x08, 0x08, 0x00000000, PPC_POWER_BR),
7112 GEN_HANDLER(dozo, 0x1F, 0x08, 0x18, 0x00000000, PPC_POWER_BR),
7113 GEN_HANDLER(dozi, 0x09, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
7114 GEN_HANDLER(lscbx, 0x1F, 0x15, 0x08, 0x00000000, PPC_POWER_BR),
7115 GEN_HANDLER(maskg, 0x1F, 0x1D, 0x00, 0x00000000, PPC_POWER_BR),
7116 GEN_HANDLER(maskir, 0x1F, 0x1D, 0x10, 0x00000000, PPC_POWER_BR),
7117 GEN_HANDLER(mul, 0x1F, 0x0B, 0x03, 0x00000000, PPC_POWER_BR),
7118 GEN_HANDLER(mulo, 0x1F, 0x0B, 0x13, 0x00000000, PPC_POWER_BR),
7119 GEN_HANDLER(nabs, 0x1F, 0x08, 0x0F, 0x00000000, PPC_POWER_BR),
7120 GEN_HANDLER(nabso, 0x1F, 0x08, 0x1F, 0x00000000, PPC_POWER_BR),
7121 GEN_HANDLER(rlmi, 0x16, 0xFF, 0xFF, 0x00000000, PPC_POWER_BR),
7122 GEN_HANDLER(rrib, 0x1F, 0x19, 0x10, 0x00000000, PPC_POWER_BR),
7123 GEN_HANDLER(sle, 0x1F, 0x19, 0x04, 0x00000000, PPC_POWER_BR),
7124 GEN_HANDLER(sleq, 0x1F, 0x19, 0x06, 0x00000000, PPC_POWER_BR),
7125 GEN_HANDLER(sliq, 0x1F, 0x18, 0x05, 0x00000000, PPC_POWER_BR),
7126 GEN_HANDLER(slliq, 0x1F, 0x18, 0x07, 0x00000000, PPC_POWER_BR),
7127 GEN_HANDLER(sllq, 0x1F, 0x18, 0x06, 0x00000000, PPC_POWER_BR),
7128 GEN_HANDLER(slq, 0x1F, 0x18, 0x04, 0x00000000, PPC_POWER_BR),
7129 GEN_HANDLER(sraiq, 0x1F, 0x18, 0x1D, 0x00000000, PPC_POWER_BR),
7130 GEN_HANDLER(sraq, 0x1F, 0x18, 0x1C, 0x00000000, PPC_POWER_BR),
7131 GEN_HANDLER(sre, 0x1F, 0x19, 0x14, 0x00000000, PPC_POWER_BR),
7132 GEN_HANDLER(srea, 0x1F, 0x19, 0x1C, 0x00000000, PPC_POWER_BR),
7133 GEN_HANDLER(sreq, 0x1F, 0x19, 0x16, 0x00000000, PPC_POWER_BR),
7134 GEN_HANDLER(sriq, 0x1F, 0x18, 0x15, 0x00000000, PPC_POWER_BR),
7135 GEN_HANDLER(srliq, 0x1F, 0x18, 0x17, 0x00000000, PPC_POWER_BR),
7136 GEN_HANDLER(srlq, 0x1F, 0x18, 0x16, 0x00000000, PPC_POWER_BR),
7137 GEN_HANDLER(srq, 0x1F, 0x18, 0x14, 0x00000000, PPC_POWER_BR),
7138 GEN_HANDLER(dsa, 0x1F, 0x14, 0x13, 0x03FFF801, PPC_602_SPEC),
7139 GEN_HANDLER(esa, 0x1F, 0x14, 0x12, 0x03FFF801, PPC_602_SPEC),
7140 GEN_HANDLER(mfrom, 0x1F, 0x09, 0x08, 0x03E0F801, PPC_602_SPEC),
7141 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
7142 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
7143 GEN_HANDLER2(tlbld_74xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_74xx_TLB),
7144 GEN_HANDLER2(tlbli_74xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_74xx_TLB),
7145 GEN_HANDLER(clf, 0x1F, 0x16, 0x03, 0x03E00000, PPC_POWER),
7146 GEN_HANDLER(cli, 0x1F, 0x16, 0x0F, 0x03E00000, PPC_POWER),
7147 GEN_HANDLER(dclst, 0x1F, 0x16, 0x13, 0x03E00000, PPC_POWER),
7148 GEN_HANDLER(mfsri, 0x1F, 0x13, 0x13, 0x00000001, PPC_POWER),
7149 GEN_HANDLER(rac, 0x1F, 0x12, 0x19, 0x00000001, PPC_POWER),
7150 GEN_HANDLER(rfsvc, 0x13, 0x12, 0x02, 0x03FFF0001, PPC_POWER),
7151 GEN_HANDLER(lfq, 0x38, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7152 GEN_HANDLER(lfqu, 0x39, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7153 GEN_HANDLER(lfqux, 0x1F, 0x17, 0x19, 0x00000001, PPC_POWER2),
7154 GEN_HANDLER(lfqx, 0x1F, 0x17, 0x18, 0x00000001, PPC_POWER2),
7155 GEN_HANDLER(stfq, 0x3C, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7156 GEN_HANDLER(stfqu, 0x3D, 0xFF, 0xFF, 0x00000003, PPC_POWER2),
7157 GEN_HANDLER(stfqux, 0x1F, 0x17, 0x1D, 0x00000001, PPC_POWER2),
7158 GEN_HANDLER(stfqx, 0x1F, 0x17, 0x1C, 0x00000001, PPC_POWER2),
7159 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
7160 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
7161 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
7162 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
7163 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
7164 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
7165 GEN_HANDLER(mfdcrux, 0x1F, 0x03, 0x09, 0x00000000, PPC_DCRUX),
7166 GEN_HANDLER(mtdcrux, 0x1F, 0x03, 0x0D, 0x00000000, PPC_DCRUX),
7167 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
7168 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
7169 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
7170 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
7171 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
7172 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
7173 GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
7174 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
7175 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
7176 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
7177 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
7178 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
7179 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
7180 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
7181 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
7182 GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
7183                PPC_NONE, PPC2_BOOKE206),
7184 GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
7185                PPC_NONE, PPC2_BOOKE206),
7186 GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
7187                PPC_NONE, PPC2_BOOKE206),
7188 GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
7189                PPC_NONE, PPC2_BOOKE206),
7190 GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
7191                PPC_NONE, PPC2_BOOKE206),
7192 GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
7193                PPC_NONE, PPC2_PRCNTL),
7194 GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
7195                PPC_NONE, PPC2_PRCNTL),
7196 GEN_HANDLER2_E(msgsync, "msgsync", 0x1F, 0x16, 0x1B, 0x00000000,
7197                PPC_NONE, PPC2_PRCNTL),
7198 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
7199 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
7200 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
7201 GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
7202               PPC_BOOKE, PPC2_BOOKE206),
7203 GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x039FF801, PPC_BOOKE),
7204 GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
7205                PPC_BOOKE, PPC2_BOOKE206),
7206 GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x06, 0x08, 0x03E00001,
7207              PPC_440_SPEC),
7208 GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
7209 GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
7210 GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
7211 GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
7212 GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
7213 #if defined(TARGET_PPC64)
7214 GEN_HANDLER_E(maddhd_maddhdu, 0x04, 0x18, 0xFF, 0x00000000, PPC_NONE,
7215               PPC2_ISA300),
7216 GEN_HANDLER_E(maddld, 0x04, 0x19, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
7217 GEN_HANDLER2_E(msgsndp, "msgsndp", 0x1F, 0x0E, 0x04, 0x03ff0001,
7218                PPC_NONE, PPC2_ISA207S),
7219 GEN_HANDLER2_E(msgclrp, "msgclrp", 0x1F, 0x0E, 0x05, 0x03ff0001,
7220                PPC_NONE, PPC2_ISA207S),
7221 #endif
7222 
7223 #undef GEN_INT_ARITH_ADD
7224 #undef GEN_INT_ARITH_ADD_CONST
7225 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov)         \
7226 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
7227 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val,                        \
7228                                 add_ca, compute_ca, compute_ov)               \
7229 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
7230 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
7231 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
7232 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
7233 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
7234 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
7235 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
7236 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
7237 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
7238 GEN_HANDLER_E(addex, 0x1F, 0x0A, 0x05, 0x00000000, PPC_NONE, PPC2_ISA300),
7239 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
7240 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
7241 
7242 #undef GEN_INT_ARITH_DIVW
7243 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov)                      \
7244 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
7245 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
7246 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
7247 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
7248 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
7249 GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7250 GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7251 GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7252 GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7253 GEN_HANDLER_E(modsw, 0x1F, 0x0B, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
7254 GEN_HANDLER_E(moduw, 0x1F, 0x0B, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
7255 
7256 #if defined(TARGET_PPC64)
7257 #undef GEN_INT_ARITH_DIVD
7258 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov)                      \
7259 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
7260 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
7261 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
7262 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
7263 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
7264 
7265 GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7266 GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
7267 GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7268 GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7269 GEN_HANDLER_E(modsd, 0x1F, 0x09, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
7270 GEN_HANDLER_E(modud, 0x1F, 0x09, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
7271 
7272 #undef GEN_INT_ARITH_MUL_HELPER
7273 #define GEN_INT_ARITH_MUL_HELPER(name, opc3)                                  \
7274 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
7275 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
7276 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
7277 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
7278 #endif
7279 
7280 #undef GEN_INT_ARITH_SUBF
7281 #undef GEN_INT_ARITH_SUBF_CONST
7282 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov)        \
7283 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
7284 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val,                       \
7285                                 add_ca, compute_ca, compute_ov)               \
7286 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
7287 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
7288 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
7289 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
7290 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
7291 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
7292 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
7293 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
7294 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
7295 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
7296 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
7297 
7298 #undef GEN_LOGICAL1
7299 #undef GEN_LOGICAL2
7300 #define GEN_LOGICAL2(name, tcg_op, opc, type)                                 \
7301 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
7302 #define GEN_LOGICAL1(name, tcg_op, opc, type)                                 \
7303 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
7304 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
7305 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
7306 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
7307 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
7308 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
7309 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
7310 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
7311 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
7312 #if defined(TARGET_PPC64)
7313 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
7314 #endif
7315 
7316 #if defined(TARGET_PPC64)
7317 #undef GEN_PPC64_R2
7318 #undef GEN_PPC64_R4
7319 #define GEN_PPC64_R2(name, opc1, opc2)                                        \
7320 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
7321 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
7322              PPC_64B)
7323 #define GEN_PPC64_R4(name, opc1, opc2)                                        \
7324 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
7325 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000,   \
7326              PPC_64B),                                                        \
7327 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000,   \
7328              PPC_64B),                                                        \
7329 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000,   \
7330              PPC_64B)
7331 GEN_PPC64_R4(rldicl, 0x1E, 0x00),
7332 GEN_PPC64_R4(rldicr, 0x1E, 0x02),
7333 GEN_PPC64_R4(rldic, 0x1E, 0x04),
7334 GEN_PPC64_R2(rldcl, 0x1E, 0x08),
7335 GEN_PPC64_R2(rldcr, 0x1E, 0x09),
7336 GEN_PPC64_R4(rldimi, 0x1E, 0x06),
7337 #endif
7338 
7339 #undef GEN_LD
7340 #undef GEN_LDU
7341 #undef GEN_LDUX
7342 #undef GEN_LDX_E
7343 #undef GEN_LDS
7344 #define GEN_LD(name, ldop, opc, type)                                         \
7345 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
7346 #define GEN_LDU(name, ldop, opc, type)                                        \
7347 GEN_HANDLER(name##u, opc, 0xFF, 0xFF, 0x00000000, type),
7348 #define GEN_LDUX(name, ldop, opc2, opc3, type)                                \
7349 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
7350 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk)                   \
7351 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
7352 #define GEN_LDS(name, ldop, op, type)                                         \
7353 GEN_LD(name, ldop, op | 0x20, type)                                           \
7354 GEN_LDU(name, ldop, op | 0x21, type)                                          \
7355 GEN_LDUX(name, ldop, 0x17, op | 0x01, type)                                   \
7356 GEN_LDX(name, ldop, 0x17, op | 0x00, type)
7357 
7358 GEN_LDS(lbz, ld8u, 0x02, PPC_INTEGER)
7359 GEN_LDS(lha, ld16s, 0x0A, PPC_INTEGER)
7360 GEN_LDS(lhz, ld16u, 0x08, PPC_INTEGER)
7361 GEN_LDS(lwz, ld32u, 0x00, PPC_INTEGER)
7362 #if defined(TARGET_PPC64)
7363 GEN_LDUX(lwa, ld32s, 0x15, 0x0B, PPC_64B)
7364 GEN_LDX(lwa, ld32s, 0x15, 0x0A, PPC_64B)
7365 GEN_LDUX(ld, ld64_i64, 0x15, 0x01, PPC_64B)
7366 GEN_LDX(ld, ld64_i64, 0x15, 0x00, PPC_64B)
7367 GEN_LDX_E(ldbr, ld64ur_i64, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE)
7368 
7369 /* HV/P7 and later only */
7370 GEN_LDX_HVRM(ldcix, ld64_i64, 0x15, 0x1b, PPC_CILDST)
7371 GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x18, PPC_CILDST)
7372 GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
7373 GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
7374 #endif
7375 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
7376 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
7377 
7378 /* External PID based load */
7379 #undef GEN_LDEPX
7380 #define GEN_LDEPX(name, ldop, opc2, opc3)                                     \
7381 GEN_HANDLER_E(name##epx, 0x1F, opc2, opc3,                                    \
7382               0x00000001, PPC_NONE, PPC2_BOOKE206),
7383 
7384 GEN_LDEPX(lb, DEF_MEMOP(MO_UB), 0x1F, 0x02)
7385 GEN_LDEPX(lh, DEF_MEMOP(MO_UW), 0x1F, 0x08)
7386 GEN_LDEPX(lw, DEF_MEMOP(MO_UL), 0x1F, 0x00)
7387 #if defined(TARGET_PPC64)
7388 GEN_LDEPX(ld, DEF_MEMOP(MO_Q), 0x1D, 0x00)
7389 #endif
7390 
7391 #undef GEN_ST
7392 #undef GEN_STU
7393 #undef GEN_STUX
7394 #undef GEN_STX_E
7395 #undef GEN_STS
7396 #define GEN_ST(name, stop, opc, type)                                         \
7397 GEN_HANDLER(name, opc, 0xFF, 0xFF, 0x00000000, type),
7398 #define GEN_STU(name, stop, opc, type)                                        \
7399 GEN_HANDLER(stop##u, opc, 0xFF, 0xFF, 0x00000000, type),
7400 #define GEN_STUX(name, stop, opc2, opc3, type)                                \
7401 GEN_HANDLER(name##ux, 0x1F, opc2, opc3, 0x00000001, type),
7402 #define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk)                   \
7403 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000000, type, type2),
7404 #define GEN_STS(name, stop, op, type)                                         \
7405 GEN_ST(name, stop, op | 0x20, type)                                           \
7406 GEN_STU(name, stop, op | 0x21, type)                                          \
7407 GEN_STUX(name, stop, 0x17, op | 0x01, type)                                   \
7408 GEN_STX(name, stop, 0x17, op | 0x00, type)
7409 
7410 GEN_STS(stb, st8, 0x06, PPC_INTEGER)
7411 GEN_STS(sth, st16, 0x0C, PPC_INTEGER)
7412 GEN_STS(stw, st32, 0x04, PPC_INTEGER)
7413 #if defined(TARGET_PPC64)
7414 GEN_STUX(std, st64_i64, 0x15, 0x05, PPC_64B)
7415 GEN_STX(std, st64_i64, 0x15, 0x04, PPC_64B)
7416 GEN_STX_E(stdbr, st64r_i64, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE)
7417 GEN_STX_HVRM(stdcix, st64_i64, 0x15, 0x1f, PPC_CILDST)
7418 GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
7419 GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
7420 GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
7421 #endif
7422 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
7423 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
7424 
7425 #undef GEN_STEPX
7426 #define GEN_STEPX(name, ldop, opc2, opc3)                                     \
7427 GEN_HANDLER_E(name##epx, 0x1F, opc2, opc3,                                    \
7428               0x00000001, PPC_NONE, PPC2_BOOKE206),
7429 
7430 GEN_STEPX(stb, DEF_MEMOP(MO_UB), 0x1F, 0x06)
7431 GEN_STEPX(sth, DEF_MEMOP(MO_UW), 0x1F, 0x0C)
7432 GEN_STEPX(stw, DEF_MEMOP(MO_UL), 0x1F, 0x04)
7433 #if defined(TARGET_PPC64)
7434 GEN_STEPX(std, DEF_MEMOP(MO_Q), 0x1D, 0x04)
7435 #endif
7436 
7437 #undef GEN_CRLOGIC
7438 #define GEN_CRLOGIC(name, tcg_op, opc)                                        \
7439 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
7440 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
7441 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
7442 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
7443 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
7444 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
7445 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
7446 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
7447 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
7448 
7449 #undef GEN_MAC_HANDLER
7450 #define GEN_MAC_HANDLER(name, opc2, opc3)                                     \
7451 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
7452 GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
7453 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
7454 GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
7455 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
7456 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
7457 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
7458 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
7459 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
7460 GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
7461 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
7462 GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
7463 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
7464 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
7465 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
7466 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
7467 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
7468 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
7469 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
7470 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
7471 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
7472 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
7473 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
7474 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
7475 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
7476 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
7477 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
7478 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
7479 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
7480 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
7481 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
7482 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
7483 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
7484 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
7485 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
7486 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
7487 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
7488 GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
7489 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
7490 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
7491 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
7492 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
7493 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
7494 
7495 GEN_HANDLER2_E(tbegin, "tbegin", 0x1F, 0x0E, 0x14, 0x01DFF800, \
7496                PPC_NONE, PPC2_TM),
7497 GEN_HANDLER2_E(tend,   "tend",   0x1F, 0x0E, 0x15, 0x01FFF800, \
7498                PPC_NONE, PPC2_TM),
7499 GEN_HANDLER2_E(tabort, "tabort", 0x1F, 0x0E, 0x1C, 0x03E0F800, \
7500                PPC_NONE, PPC2_TM),
7501 GEN_HANDLER2_E(tabortwc, "tabortwc", 0x1F, 0x0E, 0x18, 0x00000000, \
7502                PPC_NONE, PPC2_TM),
7503 GEN_HANDLER2_E(tabortwci, "tabortwci", 0x1F, 0x0E, 0x1A, 0x00000000, \
7504                PPC_NONE, PPC2_TM),
7505 GEN_HANDLER2_E(tabortdc, "tabortdc", 0x1F, 0x0E, 0x19, 0x00000000, \
7506                PPC_NONE, PPC2_TM),
7507 GEN_HANDLER2_E(tabortdci, "tabortdci", 0x1F, 0x0E, 0x1B, 0x00000000, \
7508                PPC_NONE, PPC2_TM),
7509 GEN_HANDLER2_E(tsr, "tsr", 0x1F, 0x0E, 0x17, 0x03DFF800, \
7510                PPC_NONE, PPC2_TM),
7511 GEN_HANDLER2_E(tcheck, "tcheck", 0x1F, 0x0E, 0x16, 0x007FF800, \
7512                PPC_NONE, PPC2_TM),
7513 GEN_HANDLER2_E(treclaim, "treclaim", 0x1F, 0x0E, 0x1D, 0x03E0F800, \
7514                PPC_NONE, PPC2_TM),
7515 GEN_HANDLER2_E(trechkpt, "trechkpt", 0x1F, 0x0E, 0x1F, 0x03FFF800, \
7516                PPC_NONE, PPC2_TM),
7517 
7518 #include "translate/fp-ops.inc.c"
7519 
7520 #include "translate/vmx-ops.inc.c"
7521 
7522 #include "translate/vsx-ops.inc.c"
7523 
7524 #include "translate/dfp-ops.inc.c"
7525 
7526 #include "translate/spe-ops.inc.c"
7527 };
7528 
7529 #include "helper_regs.h"
7530 #include "translate_init.inc.c"
7531 
7532 /*****************************************************************************/
7533 /* Misc PowerPC helpers */
7534 void ppc_cpu_dump_state(CPUState *cs, FILE *f, int flags)
7535 {
7536 #define RGPL  4
7537 #define RFPL  4
7538 
7539     PowerPCCPU *cpu = POWERPC_CPU(cs);
7540     CPUPPCState *env = &cpu->env;
7541     int i;
7542 
7543     qemu_fprintf(f, "NIP " TARGET_FMT_lx "   LR " TARGET_FMT_lx " CTR "
7544                  TARGET_FMT_lx " XER " TARGET_FMT_lx " CPU#%d\n",
7545                  env->nip, env->lr, env->ctr, cpu_read_xer(env),
7546                  cs->cpu_index);
7547     qemu_fprintf(f, "MSR " TARGET_FMT_lx " HID0 " TARGET_FMT_lx "  HF "
7548                  TARGET_FMT_lx " iidx %d didx %d\n",
7549                  env->msr, env->spr[SPR_HID0],
7550                  env->hflags, env->immu_idx, env->dmmu_idx);
7551 #if !defined(NO_TIMER_DUMP)
7552     qemu_fprintf(f, "TB %08" PRIu32 " %08" PRIu64
7553 #if !defined(CONFIG_USER_ONLY)
7554                  " DECR " TARGET_FMT_lu
7555 #endif
7556                  "\n",
7557                  cpu_ppc_load_tbu(env), cpu_ppc_load_tbl(env)
7558 #if !defined(CONFIG_USER_ONLY)
7559                  , cpu_ppc_load_decr(env)
7560 #endif
7561         );
7562 #endif
7563     for (i = 0; i < 32; i++) {
7564         if ((i & (RGPL - 1)) == 0) {
7565             qemu_fprintf(f, "GPR%02d", i);
7566         }
7567         qemu_fprintf(f, " %016" PRIx64, ppc_dump_gpr(env, i));
7568         if ((i & (RGPL - 1)) == (RGPL - 1)) {
7569             qemu_fprintf(f, "\n");
7570         }
7571     }
7572     qemu_fprintf(f, "CR ");
7573     for (i = 0; i < 8; i++)
7574         qemu_fprintf(f, "%01x", env->crf[i]);
7575     qemu_fprintf(f, "  [");
7576     for (i = 0; i < 8; i++) {
7577         char a = '-';
7578         if (env->crf[i] & 0x08) {
7579             a = 'L';
7580         } else if (env->crf[i] & 0x04) {
7581             a = 'G';
7582         } else if (env->crf[i] & 0x02) {
7583             a = 'E';
7584         }
7585         qemu_fprintf(f, " %c%c", a, env->crf[i] & 0x01 ? 'O' : ' ');
7586     }
7587     qemu_fprintf(f, " ]             RES " TARGET_FMT_lx "\n",
7588                  env->reserve_addr);
7589 
7590     if (flags & CPU_DUMP_FPU) {
7591         for (i = 0; i < 32; i++) {
7592             if ((i & (RFPL - 1)) == 0) {
7593                 qemu_fprintf(f, "FPR%02d", i);
7594             }
7595             qemu_fprintf(f, " %016" PRIx64, *cpu_fpr_ptr(env, i));
7596             if ((i & (RFPL - 1)) == (RFPL - 1)) {
7597                 qemu_fprintf(f, "\n");
7598             }
7599         }
7600         qemu_fprintf(f, "FPSCR " TARGET_FMT_lx "\n", env->fpscr);
7601     }
7602 
7603 #if !defined(CONFIG_USER_ONLY)
7604     qemu_fprintf(f, " SRR0 " TARGET_FMT_lx "  SRR1 " TARGET_FMT_lx
7605                  "    PVR " TARGET_FMT_lx " VRSAVE " TARGET_FMT_lx "\n",
7606                  env->spr[SPR_SRR0], env->spr[SPR_SRR1],
7607                  env->spr[SPR_PVR], env->spr[SPR_VRSAVE]);
7608 
7609     qemu_fprintf(f, "SPRG0 " TARGET_FMT_lx " SPRG1 " TARGET_FMT_lx
7610                  "  SPRG2 " TARGET_FMT_lx "  SPRG3 " TARGET_FMT_lx "\n",
7611                  env->spr[SPR_SPRG0], env->spr[SPR_SPRG1],
7612                  env->spr[SPR_SPRG2], env->spr[SPR_SPRG3]);
7613 
7614     qemu_fprintf(f, "SPRG4 " TARGET_FMT_lx " SPRG5 " TARGET_FMT_lx
7615                  "  SPRG6 " TARGET_FMT_lx "  SPRG7 " TARGET_FMT_lx "\n",
7616                  env->spr[SPR_SPRG4], env->spr[SPR_SPRG5],
7617                  env->spr[SPR_SPRG6], env->spr[SPR_SPRG7]);
7618 
7619 #if defined(TARGET_PPC64)
7620     if (env->excp_model == POWERPC_EXCP_POWER7 ||
7621         env->excp_model == POWERPC_EXCP_POWER8 ||
7622         env->excp_model == POWERPC_EXCP_POWER9)  {
7623         qemu_fprintf(f, "HSRR0 " TARGET_FMT_lx " HSRR1 " TARGET_FMT_lx "\n",
7624                      env->spr[SPR_HSRR0], env->spr[SPR_HSRR1]);
7625     }
7626 #endif
7627     if (env->excp_model == POWERPC_EXCP_BOOKE) {
7628         qemu_fprintf(f, "CSRR0 " TARGET_FMT_lx " CSRR1 " TARGET_FMT_lx
7629                      " MCSRR0 " TARGET_FMT_lx " MCSRR1 " TARGET_FMT_lx "\n",
7630                      env->spr[SPR_BOOKE_CSRR0], env->spr[SPR_BOOKE_CSRR1],
7631                      env->spr[SPR_BOOKE_MCSRR0], env->spr[SPR_BOOKE_MCSRR1]);
7632 
7633         qemu_fprintf(f, "  TCR " TARGET_FMT_lx "   TSR " TARGET_FMT_lx
7634                      "    ESR " TARGET_FMT_lx "   DEAR " TARGET_FMT_lx "\n",
7635                      env->spr[SPR_BOOKE_TCR], env->spr[SPR_BOOKE_TSR],
7636                      env->spr[SPR_BOOKE_ESR], env->spr[SPR_BOOKE_DEAR]);
7637 
7638         qemu_fprintf(f, "  PIR " TARGET_FMT_lx " DECAR " TARGET_FMT_lx
7639                      "   IVPR " TARGET_FMT_lx "   EPCR " TARGET_FMT_lx "\n",
7640                      env->spr[SPR_BOOKE_PIR], env->spr[SPR_BOOKE_DECAR],
7641                      env->spr[SPR_BOOKE_IVPR], env->spr[SPR_BOOKE_EPCR]);
7642 
7643         qemu_fprintf(f, " MCSR " TARGET_FMT_lx " SPRG8 " TARGET_FMT_lx
7644                      "    EPR " TARGET_FMT_lx "\n",
7645                      env->spr[SPR_BOOKE_MCSR], env->spr[SPR_BOOKE_SPRG8],
7646                      env->spr[SPR_BOOKE_EPR]);
7647 
7648         /* FSL-specific */
7649         qemu_fprintf(f, " MCAR " TARGET_FMT_lx "  PID1 " TARGET_FMT_lx
7650                      "   PID2 " TARGET_FMT_lx "    SVR " TARGET_FMT_lx "\n",
7651                      env->spr[SPR_Exxx_MCAR], env->spr[SPR_BOOKE_PID1],
7652                      env->spr[SPR_BOOKE_PID2], env->spr[SPR_E500_SVR]);
7653 
7654         /*
7655          * IVORs are left out as they are large and do not change often --
7656          * they can be read with "p $ivor0", "p $ivor1", etc.
7657          */
7658     }
7659 
7660 #if defined(TARGET_PPC64)
7661     if (env->flags & POWERPC_FLAG_CFAR) {
7662         qemu_fprintf(f, " CFAR " TARGET_FMT_lx"\n", env->cfar);
7663     }
7664 #endif
7665 
7666     if (env->spr_cb[SPR_LPCR].name) {
7667         qemu_fprintf(f, " LPCR " TARGET_FMT_lx "\n", env->spr[SPR_LPCR]);
7668     }
7669 
7670     switch (env->mmu_model) {
7671     case POWERPC_MMU_32B:
7672     case POWERPC_MMU_601:
7673     case POWERPC_MMU_SOFT_6xx:
7674     case POWERPC_MMU_SOFT_74xx:
7675 #if defined(TARGET_PPC64)
7676     case POWERPC_MMU_64B:
7677     case POWERPC_MMU_2_03:
7678     case POWERPC_MMU_2_06:
7679     case POWERPC_MMU_2_07:
7680     case POWERPC_MMU_3_00:
7681 #endif
7682         if (env->spr_cb[SPR_SDR1].name) { /* SDR1 Exists */
7683             qemu_fprintf(f, " SDR1 " TARGET_FMT_lx " ", env->spr[SPR_SDR1]);
7684         }
7685         if (env->spr_cb[SPR_PTCR].name) { /* PTCR Exists */
7686             qemu_fprintf(f, " PTCR " TARGET_FMT_lx " ", env->spr[SPR_PTCR]);
7687         }
7688         qemu_fprintf(f, "  DAR " TARGET_FMT_lx "  DSISR " TARGET_FMT_lx "\n",
7689                      env->spr[SPR_DAR], env->spr[SPR_DSISR]);
7690         break;
7691     case POWERPC_MMU_BOOKE206:
7692         qemu_fprintf(f, " MAS0 " TARGET_FMT_lx "  MAS1 " TARGET_FMT_lx
7693                      "   MAS2 " TARGET_FMT_lx "   MAS3 " TARGET_FMT_lx "\n",
7694                      env->spr[SPR_BOOKE_MAS0], env->spr[SPR_BOOKE_MAS1],
7695                      env->spr[SPR_BOOKE_MAS2], env->spr[SPR_BOOKE_MAS3]);
7696 
7697         qemu_fprintf(f, " MAS4 " TARGET_FMT_lx "  MAS6 " TARGET_FMT_lx
7698                      "   MAS7 " TARGET_FMT_lx "    PID " TARGET_FMT_lx "\n",
7699                      env->spr[SPR_BOOKE_MAS4], env->spr[SPR_BOOKE_MAS6],
7700                      env->spr[SPR_BOOKE_MAS7], env->spr[SPR_BOOKE_PID]);
7701 
7702         qemu_fprintf(f, "MMUCFG " TARGET_FMT_lx " TLB0CFG " TARGET_FMT_lx
7703                      " TLB1CFG " TARGET_FMT_lx "\n",
7704                      env->spr[SPR_MMUCFG], env->spr[SPR_BOOKE_TLB0CFG],
7705                      env->spr[SPR_BOOKE_TLB1CFG]);
7706         break;
7707     default:
7708         break;
7709     }
7710 #endif
7711 
7712 #undef RGPL
7713 #undef RFPL
7714 }
7715 
7716 void ppc_cpu_dump_statistics(CPUState *cs, int flags)
7717 {
7718 #if defined(DO_PPC_STATISTICS)
7719     PowerPCCPU *cpu = POWERPC_CPU(cs);
7720     opc_handler_t **t1, **t2, **t3, *handler;
7721     int op1, op2, op3;
7722 
7723     t1 = cpu->env.opcodes;
7724     for (op1 = 0; op1 < 64; op1++) {
7725         handler = t1[op1];
7726         if (is_indirect_opcode(handler)) {
7727             t2 = ind_table(handler);
7728             for (op2 = 0; op2 < 32; op2++) {
7729                 handler = t2[op2];
7730                 if (is_indirect_opcode(handler)) {
7731                     t3 = ind_table(handler);
7732                     for (op3 = 0; op3 < 32; op3++) {
7733                         handler = t3[op3];
7734                         if (handler->count == 0) {
7735                             continue;
7736                         }
7737                         qemu_printf("%02x %02x %02x (%02x %04d) %16s: "
7738                                     "%016" PRIx64 " %" PRId64 "\n",
7739                                     op1, op2, op3, op1, (op3 << 5) | op2,
7740                                     handler->oname,
7741                                     handler->count, handler->count);
7742                     }
7743                 } else {
7744                     if (handler->count == 0) {
7745                         continue;
7746                     }
7747                     qemu_printf("%02x %02x    (%02x %04d) %16s: "
7748                                 "%016" PRIx64 " %" PRId64 "\n",
7749                                 op1, op2, op1, op2, handler->oname,
7750                                 handler->count, handler->count);
7751                 }
7752             }
7753         } else {
7754             if (handler->count == 0) {
7755                 continue;
7756             }
7757             qemu_printf("%02x       (%02x     ) %16s: %016" PRIx64
7758                         " %" PRId64 "\n",
7759                         op1, op1, handler->oname,
7760                         handler->count, handler->count);
7761         }
7762     }
7763 #endif
7764 }
7765 
7766 static void ppc_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
7767 {
7768     DisasContext *ctx = container_of(dcbase, DisasContext, base);
7769     CPUPPCState *env = cs->env_ptr;
7770     int bound;
7771 
7772     ctx->exception = POWERPC_EXCP_NONE;
7773     ctx->spr_cb = env->spr_cb;
7774     ctx->pr = msr_pr;
7775     ctx->mem_idx = env->dmmu_idx;
7776     ctx->dr = msr_dr;
7777 #if !defined(CONFIG_USER_ONLY)
7778     ctx->hv = msr_hv || !env->has_hv_mode;
7779 #endif
7780     ctx->insns_flags = env->insns_flags;
7781     ctx->insns_flags2 = env->insns_flags2;
7782     ctx->access_type = -1;
7783     ctx->need_access_type = !(env->mmu_model & POWERPC_MMU_64B);
7784     ctx->le_mode = !!(env->hflags & (1 << MSR_LE));
7785     ctx->default_tcg_memop_mask = ctx->le_mode ? MO_LE : MO_BE;
7786     ctx->flags = env->flags;
7787 #if defined(TARGET_PPC64)
7788     ctx->sf_mode = msr_is_64bit(env, env->msr);
7789     ctx->has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
7790 #endif
7791     ctx->lazy_tlb_flush = env->mmu_model == POWERPC_MMU_32B
7792         || env->mmu_model == POWERPC_MMU_601
7793         || (env->mmu_model & POWERPC_MMU_64B);
7794 
7795     ctx->fpu_enabled = !!msr_fp;
7796     if ((env->flags & POWERPC_FLAG_SPE) && msr_spe) {
7797         ctx->spe_enabled = !!msr_spe;
7798     } else {
7799         ctx->spe_enabled = false;
7800     }
7801     if ((env->flags & POWERPC_FLAG_VRE) && msr_vr) {
7802         ctx->altivec_enabled = !!msr_vr;
7803     } else {
7804         ctx->altivec_enabled = false;
7805     }
7806     if ((env->flags & POWERPC_FLAG_VSX) && msr_vsx) {
7807         ctx->vsx_enabled = !!msr_vsx;
7808     } else {
7809         ctx->vsx_enabled = false;
7810     }
7811 #if defined(TARGET_PPC64)
7812     if ((env->flags & POWERPC_FLAG_TM) && msr_tm) {
7813         ctx->tm_enabled = !!msr_tm;
7814     } else {
7815         ctx->tm_enabled = false;
7816     }
7817 #endif
7818     ctx->gtse = !!(env->spr[SPR_LPCR] & LPCR_GTSE);
7819     if ((env->flags & POWERPC_FLAG_SE) && msr_se) {
7820         ctx->singlestep_enabled = CPU_SINGLE_STEP;
7821     } else {
7822         ctx->singlestep_enabled = 0;
7823     }
7824     if ((env->flags & POWERPC_FLAG_BE) && msr_be) {
7825         ctx->singlestep_enabled |= CPU_BRANCH_STEP;
7826     }
7827     if ((env->flags & POWERPC_FLAG_DE) && msr_de) {
7828         ctx->singlestep_enabled = 0;
7829         target_ulong dbcr0 = env->spr[SPR_BOOKE_DBCR0];
7830         if (dbcr0 & DBCR0_ICMP) {
7831             ctx->singlestep_enabled |= CPU_SINGLE_STEP;
7832         }
7833         if (dbcr0 & DBCR0_BRT) {
7834             ctx->singlestep_enabled |= CPU_BRANCH_STEP;
7835         }
7836 
7837     }
7838     if (unlikely(ctx->base.singlestep_enabled)) {
7839         ctx->singlestep_enabled |= GDBSTUB_SINGLE_STEP;
7840     }
7841 #if defined(DO_SINGLE_STEP) && 0
7842     /* Single step trace mode */
7843     msr_se = 1;
7844 #endif
7845 
7846     bound = -(ctx->base.pc_first | TARGET_PAGE_MASK) / 4;
7847     ctx->base.max_insns = MIN(ctx->base.max_insns, bound);
7848 }
7849 
7850 static void ppc_tr_tb_start(DisasContextBase *db, CPUState *cs)
7851 {
7852 }
7853 
7854 static void ppc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
7855 {
7856     tcg_gen_insn_start(dcbase->pc_next);
7857 }
7858 
7859 static bool ppc_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,
7860                                     const CPUBreakpoint *bp)
7861 {
7862     DisasContext *ctx = container_of(dcbase, DisasContext, base);
7863 
7864     gen_debug_exception(ctx);
7865     dcbase->is_jmp = DISAS_NORETURN;
7866     /*
7867      * The address covered by the breakpoint must be included in
7868      * [tb->pc, tb->pc + tb->size) in order to for it to be properly
7869      * cleared -- thus we increment the PC here so that the logic
7870      * setting tb->size below does the right thing.
7871      */
7872     ctx->base.pc_next += 4;
7873     return true;
7874 }
7875 
7876 static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
7877 {
7878     DisasContext *ctx = container_of(dcbase, DisasContext, base);
7879     PowerPCCPU *cpu = POWERPC_CPU(cs);
7880     CPUPPCState *env = cs->env_ptr;
7881     opc_handler_t **table, *handler;
7882 
7883     LOG_DISAS("----------------\n");
7884     LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
7885               ctx->base.pc_next, ctx->mem_idx, (int)msr_ir);
7886 
7887     ctx->opcode = translator_ldl_swap(env, ctx->base.pc_next,
7888                                       need_byteswap(ctx));
7889 
7890     LOG_DISAS("translate opcode %08x (%02x %02x %02x %02x) (%s)\n",
7891               ctx->opcode, opc1(ctx->opcode), opc2(ctx->opcode),
7892               opc3(ctx->opcode), opc4(ctx->opcode),
7893               ctx->le_mode ? "little" : "big");
7894     ctx->base.pc_next += 4;
7895     table = cpu->opcodes;
7896     handler = table[opc1(ctx->opcode)];
7897     if (is_indirect_opcode(handler)) {
7898         table = ind_table(handler);
7899         handler = table[opc2(ctx->opcode)];
7900         if (is_indirect_opcode(handler)) {
7901             table = ind_table(handler);
7902             handler = table[opc3(ctx->opcode)];
7903             if (is_indirect_opcode(handler)) {
7904                 table = ind_table(handler);
7905                 handler = table[opc4(ctx->opcode)];
7906             }
7907         }
7908     }
7909     /* Is opcode *REALLY* valid ? */
7910     if (unlikely(handler->handler == &gen_invalid)) {
7911         qemu_log_mask(LOG_GUEST_ERROR, "invalid/unsupported opcode: "
7912                       "%02x - %02x - %02x - %02x (%08x) "
7913                       TARGET_FMT_lx " %d\n",
7914                       opc1(ctx->opcode), opc2(ctx->opcode),
7915                       opc3(ctx->opcode), opc4(ctx->opcode),
7916                       ctx->opcode, ctx->base.pc_next - 4, (int)msr_ir);
7917     } else {
7918         uint32_t inval;
7919 
7920         if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE)
7921                      && Rc(ctx->opcode))) {
7922             inval = handler->inval2;
7923         } else {
7924             inval = handler->inval1;
7925         }
7926 
7927         if (unlikely((ctx->opcode & inval) != 0)) {
7928             qemu_log_mask(LOG_GUEST_ERROR, "invalid bits: %08x for opcode: "
7929                           "%02x - %02x - %02x - %02x (%08x) "
7930                           TARGET_FMT_lx "\n", ctx->opcode & inval,
7931                           opc1(ctx->opcode), opc2(ctx->opcode),
7932                           opc3(ctx->opcode), opc4(ctx->opcode),
7933                           ctx->opcode, ctx->base.pc_next - 4);
7934             gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
7935             ctx->base.is_jmp = DISAS_NORETURN;
7936             return;
7937         }
7938     }
7939     (*(handler->handler))(ctx);
7940 #if defined(DO_PPC_STATISTICS)
7941     handler->count++;
7942 #endif
7943     /* Check trace mode exceptions */
7944     if (unlikely(ctx->singlestep_enabled & CPU_SINGLE_STEP &&
7945                  (ctx->base.pc_next <= 0x100 || ctx->base.pc_next > 0xF00) &&
7946                  ctx->exception != POWERPC_SYSCALL &&
7947                  ctx->exception != POWERPC_EXCP_TRAP &&
7948                  ctx->exception != POWERPC_EXCP_BRANCH)) {
7949         uint32_t excp = gen_prep_dbgex(ctx);
7950         gen_exception_nip(ctx, excp, ctx->base.pc_next);
7951     }
7952 
7953     if (tcg_check_temp_count()) {
7954         qemu_log("Opcode %02x %02x %02x %02x (%08x) leaked "
7955                  "temporaries\n", opc1(ctx->opcode), opc2(ctx->opcode),
7956                  opc3(ctx->opcode), opc4(ctx->opcode), ctx->opcode);
7957     }
7958 
7959     ctx->base.is_jmp = ctx->exception == POWERPC_EXCP_NONE ?
7960         DISAS_NEXT : DISAS_NORETURN;
7961 }
7962 
7963 static void ppc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
7964 {
7965     DisasContext *ctx = container_of(dcbase, DisasContext, base);
7966 
7967     if (ctx->exception == POWERPC_EXCP_NONE) {
7968         gen_goto_tb(ctx, 0, ctx->base.pc_next);
7969     } else if (ctx->exception != POWERPC_EXCP_BRANCH) {
7970         if (unlikely(ctx->base.singlestep_enabled)) {
7971             gen_debug_exception(ctx);
7972         }
7973         /* Generate the return instruction */
7974         tcg_gen_exit_tb(NULL, 0);
7975     }
7976 }
7977 
7978 static void ppc_tr_disas_log(const DisasContextBase *dcbase, CPUState *cs)
7979 {
7980     qemu_log("IN: %s\n", lookup_symbol(dcbase->pc_first));
7981     log_target_disas(cs, dcbase->pc_first, dcbase->tb->size);
7982 }
7983 
7984 static const TranslatorOps ppc_tr_ops = {
7985     .init_disas_context = ppc_tr_init_disas_context,
7986     .tb_start           = ppc_tr_tb_start,
7987     .insn_start         = ppc_tr_insn_start,
7988     .breakpoint_check   = ppc_tr_breakpoint_check,
7989     .translate_insn     = ppc_tr_translate_insn,
7990     .tb_stop            = ppc_tr_tb_stop,
7991     .disas_log          = ppc_tr_disas_log,
7992 };
7993 
7994 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
7995 {
7996     DisasContext ctx;
7997 
7998     translator_loop(&ppc_tr_ops, &ctx.base, cs, tb, max_insns);
7999 }
8000 
8001 void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb,
8002                           target_ulong *data)
8003 {
8004     env->nip = data[0];
8005 }
8006