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