target/ppc: Move slbie to decodetree
[qemu.git] / target / ppc / translate.c
blob244eefd9652d02e494dc0cf8f7dabbe813b1e662
1 /*
2 * PowerPC emulation for qemu: main translation routines.
4 * Copyright (c) 2003-2007 Jocelyn Mayer
5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
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.
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.
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/>.
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"
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
35 #include "exec/translator.h"
36 #include "exec/log.h"
37 #include "qemu/atomic128.h"
38 #include "spr_common.h"
40 #include "qemu/qemu-print.h"
41 #include "qapi/error.h"
43 #define CPU_SINGLE_STEP 0x1
44 #define CPU_BRANCH_STEP 0x2
46 /* Include definitions for instructions classes and implementations flags */
47 /* #define PPC_DEBUG_DISAS */
49 #ifdef PPC_DEBUG_DISAS
50 # define LOG_DISAS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
51 #else
52 # define LOG_DISAS(...) do { } while (0)
53 #endif
54 /*****************************************************************************/
55 /* Code translation helpers */
57 /* global register indexes */
58 static char cpu_reg_names[10 * 3 + 22 * 4 /* GPR */
59 + 10 * 4 + 22 * 5 /* SPE GPRh */
60 + 8 * 5 /* CRF */];
61 static TCGv cpu_gpr[32];
62 static TCGv cpu_gprh[32];
63 static TCGv_i32 cpu_crf[8];
64 static TCGv cpu_nip;
65 static TCGv cpu_msr;
66 static TCGv cpu_ctr;
67 static TCGv cpu_lr;
68 #if defined(TARGET_PPC64)
69 static TCGv cpu_cfar;
70 #endif
71 static TCGv cpu_xer, cpu_so, cpu_ov, cpu_ca, cpu_ov32, cpu_ca32;
72 static TCGv cpu_reserve;
73 static TCGv cpu_reserve_val;
74 static TCGv cpu_fpscr;
75 static TCGv_i32 cpu_access_type;
77 #include "exec/gen-icount.h"
79 void ppc_translate_init(void)
81 int i;
82 char *p;
83 size_t cpu_reg_names_size;
85 p = cpu_reg_names;
86 cpu_reg_names_size = sizeof(cpu_reg_names);
88 for (i = 0; i < 8; i++) {
89 snprintf(p, cpu_reg_names_size, "crf%d", i);
90 cpu_crf[i] = tcg_global_mem_new_i32(cpu_env,
91 offsetof(CPUPPCState, crf[i]), p);
92 p += 5;
93 cpu_reg_names_size -= 5;
96 for (i = 0; i < 32; i++) {
97 snprintf(p, cpu_reg_names_size, "r%d", i);
98 cpu_gpr[i] = tcg_global_mem_new(cpu_env,
99 offsetof(CPUPPCState, gpr[i]), p);
100 p += (i < 10) ? 3 : 4;
101 cpu_reg_names_size -= (i < 10) ? 3 : 4;
102 snprintf(p, cpu_reg_names_size, "r%dH", i);
103 cpu_gprh[i] = tcg_global_mem_new(cpu_env,
104 offsetof(CPUPPCState, gprh[i]), p);
105 p += (i < 10) ? 4 : 5;
106 cpu_reg_names_size -= (i < 10) ? 4 : 5;
109 cpu_nip = tcg_global_mem_new(cpu_env,
110 offsetof(CPUPPCState, nip), "nip");
112 cpu_msr = tcg_global_mem_new(cpu_env,
113 offsetof(CPUPPCState, msr), "msr");
115 cpu_ctr = tcg_global_mem_new(cpu_env,
116 offsetof(CPUPPCState, ctr), "ctr");
118 cpu_lr = tcg_global_mem_new(cpu_env,
119 offsetof(CPUPPCState, lr), "lr");
121 #if defined(TARGET_PPC64)
122 cpu_cfar = tcg_global_mem_new(cpu_env,
123 offsetof(CPUPPCState, cfar), "cfar");
124 #endif
126 cpu_xer = tcg_global_mem_new(cpu_env,
127 offsetof(CPUPPCState, xer), "xer");
128 cpu_so = tcg_global_mem_new(cpu_env,
129 offsetof(CPUPPCState, so), "SO");
130 cpu_ov = tcg_global_mem_new(cpu_env,
131 offsetof(CPUPPCState, ov), "OV");
132 cpu_ca = tcg_global_mem_new(cpu_env,
133 offsetof(CPUPPCState, ca), "CA");
134 cpu_ov32 = tcg_global_mem_new(cpu_env,
135 offsetof(CPUPPCState, ov32), "OV32");
136 cpu_ca32 = tcg_global_mem_new(cpu_env,
137 offsetof(CPUPPCState, ca32), "CA32");
139 cpu_reserve = tcg_global_mem_new(cpu_env,
140 offsetof(CPUPPCState, reserve_addr),
141 "reserve_addr");
142 cpu_reserve_val = tcg_global_mem_new(cpu_env,
143 offsetof(CPUPPCState, reserve_val),
144 "reserve_val");
146 cpu_fpscr = tcg_global_mem_new(cpu_env,
147 offsetof(CPUPPCState, fpscr), "fpscr");
149 cpu_access_type = tcg_global_mem_new_i32(cpu_env,
150 offsetof(CPUPPCState, access_type),
151 "access_type");
154 /* internal defines */
155 struct DisasContext {
156 DisasContextBase base;
157 target_ulong cia; /* current instruction address */
158 uint32_t opcode;
159 /* Routine used to access memory */
160 bool pr, hv, dr, le_mode;
161 bool lazy_tlb_flush;
162 bool need_access_type;
163 int mem_idx;
164 int access_type;
165 /* Translation flags */
166 MemOp default_tcg_memop_mask;
167 #if defined(TARGET_PPC64)
168 bool sf_mode;
169 bool has_cfar;
170 #endif
171 bool fpu_enabled;
172 bool altivec_enabled;
173 bool vsx_enabled;
174 bool spe_enabled;
175 bool tm_enabled;
176 bool gtse;
177 bool hr;
178 bool mmcr0_pmcc0;
179 bool mmcr0_pmcc1;
180 bool pmu_insn_cnt;
181 ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */
182 int singlestep_enabled;
183 uint32_t flags;
184 uint64_t insns_flags;
185 uint64_t insns_flags2;
188 #define DISAS_EXIT DISAS_TARGET_0 /* exit to main loop, pc updated */
189 #define DISAS_EXIT_UPDATE DISAS_TARGET_1 /* exit to main loop, pc stale */
190 #define DISAS_CHAIN DISAS_TARGET_2 /* lookup next tb, pc updated */
191 #define DISAS_CHAIN_UPDATE DISAS_TARGET_3 /* lookup next tb, pc stale */
193 /* Return true iff byteswap is needed in a scalar memop */
194 static inline bool need_byteswap(const DisasContext *ctx)
196 #if TARGET_BIG_ENDIAN
197 return ctx->le_mode;
198 #else
199 return !ctx->le_mode;
200 #endif
203 /* True when active word size < size of target_long. */
204 #ifdef TARGET_PPC64
205 # define NARROW_MODE(C) (!(C)->sf_mode)
206 #else
207 # define NARROW_MODE(C) 0
208 #endif
210 struct opc_handler_t {
211 /* invalid bits for instruction 1 (Rc(opcode) == 0) */
212 uint32_t inval1;
213 /* invalid bits for instruction 2 (Rc(opcode) == 1) */
214 uint32_t inval2;
215 /* instruction type */
216 uint64_t type;
217 /* extended instruction type */
218 uint64_t type2;
219 /* handler */
220 void (*handler)(DisasContext *ctx);
223 /* SPR load/store helpers */
224 static inline void gen_load_spr(TCGv t, int reg)
226 tcg_gen_ld_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
229 static inline void gen_store_spr(int reg, TCGv t)
231 tcg_gen_st_tl(t, cpu_env, offsetof(CPUPPCState, spr[reg]));
234 static inline void gen_set_access_type(DisasContext *ctx, int access_type)
236 if (ctx->need_access_type && ctx->access_type != access_type) {
237 tcg_gen_movi_i32(cpu_access_type, access_type);
238 ctx->access_type = access_type;
242 static inline void gen_update_nip(DisasContext *ctx, target_ulong nip)
244 if (NARROW_MODE(ctx)) {
245 nip = (uint32_t)nip;
247 tcg_gen_movi_tl(cpu_nip, nip);
250 static void gen_exception_err(DisasContext *ctx, uint32_t excp, uint32_t error)
252 TCGv_i32 t0, t1;
255 * These are all synchronous exceptions, we set the PC back to the
256 * faulting instruction
258 gen_update_nip(ctx, ctx->cia);
259 t0 = tcg_const_i32(excp);
260 t1 = tcg_const_i32(error);
261 gen_helper_raise_exception_err(cpu_env, t0, t1);
262 tcg_temp_free_i32(t0);
263 tcg_temp_free_i32(t1);
264 ctx->base.is_jmp = DISAS_NORETURN;
267 static void gen_exception(DisasContext *ctx, uint32_t excp)
269 TCGv_i32 t0;
272 * These are all synchronous exceptions, we set the PC back to the
273 * faulting instruction
275 gen_update_nip(ctx, ctx->cia);
276 t0 = tcg_const_i32(excp);
277 gen_helper_raise_exception(cpu_env, t0);
278 tcg_temp_free_i32(t0);
279 ctx->base.is_jmp = DISAS_NORETURN;
282 static void gen_exception_nip(DisasContext *ctx, uint32_t excp,
283 target_ulong nip)
285 TCGv_i32 t0;
287 gen_update_nip(ctx, nip);
288 t0 = tcg_const_i32(excp);
289 gen_helper_raise_exception(cpu_env, t0);
290 tcg_temp_free_i32(t0);
291 ctx->base.is_jmp = DISAS_NORETURN;
294 static void gen_icount_io_start(DisasContext *ctx)
296 if (tb_cflags(ctx->base.tb) & CF_USE_ICOUNT) {
297 gen_io_start();
299 * An I/O instruction must be last in the TB.
300 * Chain to the next TB, and let the code from gen_tb_start
301 * decide if we need to return to the main loop.
302 * Doing this first also allows this value to be overridden.
304 ctx->base.is_jmp = DISAS_TOO_MANY;
309 * Tells the caller what is the appropriate exception to generate and prepares
310 * SPR registers for this exception.
312 * The exception can be either POWERPC_EXCP_TRACE (on most PowerPCs) or
313 * POWERPC_EXCP_DEBUG (on BookE).
315 static uint32_t gen_prep_dbgex(DisasContext *ctx)
317 if (ctx->flags & POWERPC_FLAG_DE) {
318 target_ulong dbsr = 0;
319 if (ctx->singlestep_enabled & CPU_SINGLE_STEP) {
320 dbsr = DBCR0_ICMP;
321 } else {
322 /* Must have been branch */
323 dbsr = DBCR0_BRT;
325 TCGv t0 = tcg_temp_new();
326 gen_load_spr(t0, SPR_BOOKE_DBSR);
327 tcg_gen_ori_tl(t0, t0, dbsr);
328 gen_store_spr(SPR_BOOKE_DBSR, t0);
329 tcg_temp_free(t0);
330 return POWERPC_EXCP_DEBUG;
331 } else {
332 return POWERPC_EXCP_TRACE;
336 static void gen_debug_exception(DisasContext *ctx)
338 gen_helper_raise_exception(cpu_env, tcg_constant_i32(gen_prep_dbgex(ctx)));
339 ctx->base.is_jmp = DISAS_NORETURN;
342 static inline void gen_inval_exception(DisasContext *ctx, uint32_t error)
344 /* Will be converted to program check if needed */
345 gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_INVAL | error);
348 static inline void gen_priv_exception(DisasContext *ctx, uint32_t error)
350 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_PRIV | error);
353 static inline void gen_hvpriv_exception(DisasContext *ctx, uint32_t error)
355 /* Will be converted to program check if needed */
356 gen_exception_err(ctx, POWERPC_EXCP_HV_EMU, POWERPC_EXCP_PRIV | error);
359 /*****************************************************************************/
360 /* SPR READ/WRITE CALLBACKS */
362 void spr_noaccess(DisasContext *ctx, int gprn, int sprn)
364 #if 0
365 sprn = ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);
366 printf("ERROR: try to access SPR %d !\n", sprn);
367 #endif
370 /* #define PPC_DUMP_SPR_ACCESSES */
373 * Generic callbacks:
374 * do nothing but store/retrieve spr value
376 static void spr_load_dump_spr(int sprn)
378 #ifdef PPC_DUMP_SPR_ACCESSES
379 TCGv_i32 t0 = tcg_const_i32(sprn);
380 gen_helper_load_dump_spr(cpu_env, t0);
381 tcg_temp_free_i32(t0);
382 #endif
385 void spr_read_generic(DisasContext *ctx, int gprn, int sprn)
387 gen_load_spr(cpu_gpr[gprn], sprn);
388 spr_load_dump_spr(sprn);
391 static void spr_store_dump_spr(int sprn)
393 #ifdef PPC_DUMP_SPR_ACCESSES
394 TCGv_i32 t0 = tcg_const_i32(sprn);
395 gen_helper_store_dump_spr(cpu_env, t0);
396 tcg_temp_free_i32(t0);
397 #endif
400 void spr_write_generic(DisasContext *ctx, int sprn, int gprn)
402 gen_store_spr(sprn, cpu_gpr[gprn]);
403 spr_store_dump_spr(sprn);
406 void spr_write_CTRL(DisasContext *ctx, int sprn, int gprn)
408 spr_write_generic(ctx, sprn, gprn);
411 * SPR_CTRL writes must force a new translation block,
412 * allowing the PMU to calculate the run latch events with
413 * more accuracy.
415 ctx->base.is_jmp = DISAS_EXIT_UPDATE;
418 #if !defined(CONFIG_USER_ONLY)
419 void spr_write_generic32(DisasContext *ctx, int sprn, int gprn)
421 #ifdef TARGET_PPC64
422 TCGv t0 = tcg_temp_new();
423 tcg_gen_ext32u_tl(t0, cpu_gpr[gprn]);
424 gen_store_spr(sprn, t0);
425 tcg_temp_free(t0);
426 spr_store_dump_spr(sprn);
427 #else
428 spr_write_generic(ctx, sprn, gprn);
429 #endif
432 void spr_write_clear(DisasContext *ctx, int sprn, int gprn)
434 TCGv t0 = tcg_temp_new();
435 TCGv t1 = tcg_temp_new();
436 gen_load_spr(t0, sprn);
437 tcg_gen_neg_tl(t1, cpu_gpr[gprn]);
438 tcg_gen_and_tl(t0, t0, t1);
439 gen_store_spr(sprn, t0);
440 tcg_temp_free(t0);
441 tcg_temp_free(t1);
444 void spr_access_nop(DisasContext *ctx, int sprn, int gprn)
448 #endif
450 /* SPR common to all PowerPC */
451 /* XER */
452 void spr_read_xer(DisasContext *ctx, int gprn, int sprn)
454 TCGv dst = cpu_gpr[gprn];
455 TCGv t0 = tcg_temp_new();
456 TCGv t1 = tcg_temp_new();
457 TCGv t2 = tcg_temp_new();
458 tcg_gen_mov_tl(dst, cpu_xer);
459 tcg_gen_shli_tl(t0, cpu_so, XER_SO);
460 tcg_gen_shli_tl(t1, cpu_ov, XER_OV);
461 tcg_gen_shli_tl(t2, cpu_ca, XER_CA);
462 tcg_gen_or_tl(t0, t0, t1);
463 tcg_gen_or_tl(dst, dst, t2);
464 tcg_gen_or_tl(dst, dst, t0);
465 if (is_isa300(ctx)) {
466 tcg_gen_shli_tl(t0, cpu_ov32, XER_OV32);
467 tcg_gen_or_tl(dst, dst, t0);
468 tcg_gen_shli_tl(t0, cpu_ca32, XER_CA32);
469 tcg_gen_or_tl(dst, dst, t0);
471 tcg_temp_free(t0);
472 tcg_temp_free(t1);
473 tcg_temp_free(t2);
476 void spr_write_xer(DisasContext *ctx, int sprn, int gprn)
478 TCGv src = cpu_gpr[gprn];
479 /* Write all flags, while reading back check for isa300 */
480 tcg_gen_andi_tl(cpu_xer, src,
481 ~((1u << XER_SO) |
482 (1u << XER_OV) | (1u << XER_OV32) |
483 (1u << XER_CA) | (1u << XER_CA32)));
484 tcg_gen_extract_tl(cpu_ov32, src, XER_OV32, 1);
485 tcg_gen_extract_tl(cpu_ca32, src, XER_CA32, 1);
486 tcg_gen_extract_tl(cpu_so, src, XER_SO, 1);
487 tcg_gen_extract_tl(cpu_ov, src, XER_OV, 1);
488 tcg_gen_extract_tl(cpu_ca, src, XER_CA, 1);
491 /* LR */
492 void spr_read_lr(DisasContext *ctx, int gprn, int sprn)
494 tcg_gen_mov_tl(cpu_gpr[gprn], cpu_lr);
497 void spr_write_lr(DisasContext *ctx, int sprn, int gprn)
499 tcg_gen_mov_tl(cpu_lr, cpu_gpr[gprn]);
502 /* CFAR */
503 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
504 void spr_read_cfar(DisasContext *ctx, int gprn, int sprn)
506 tcg_gen_mov_tl(cpu_gpr[gprn], cpu_cfar);
509 void spr_write_cfar(DisasContext *ctx, int sprn, int gprn)
511 tcg_gen_mov_tl(cpu_cfar, cpu_gpr[gprn]);
513 #endif /* defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY) */
515 /* CTR */
516 void spr_read_ctr(DisasContext *ctx, int gprn, int sprn)
518 tcg_gen_mov_tl(cpu_gpr[gprn], cpu_ctr);
521 void spr_write_ctr(DisasContext *ctx, int sprn, int gprn)
523 tcg_gen_mov_tl(cpu_ctr, cpu_gpr[gprn]);
526 /* User read access to SPR */
527 /* USPRx */
528 /* UMMCRx */
529 /* UPMCx */
530 /* USIA */
531 /* UDECR */
532 void spr_read_ureg(DisasContext *ctx, int gprn, int sprn)
534 gen_load_spr(cpu_gpr[gprn], sprn + 0x10);
537 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
538 void spr_write_ureg(DisasContext *ctx, int sprn, int gprn)
540 gen_store_spr(sprn + 0x10, cpu_gpr[gprn]);
542 #endif
544 /* SPR common to all non-embedded PowerPC */
545 /* DECR */
546 #if !defined(CONFIG_USER_ONLY)
547 void spr_read_decr(DisasContext *ctx, int gprn, int sprn)
549 gen_icount_io_start(ctx);
550 gen_helper_load_decr(cpu_gpr[gprn], cpu_env);
553 void spr_write_decr(DisasContext *ctx, int sprn, int gprn)
555 gen_icount_io_start(ctx);
556 gen_helper_store_decr(cpu_env, cpu_gpr[gprn]);
558 #endif
560 /* SPR common to all non-embedded PowerPC, except 601 */
561 /* Time base */
562 void spr_read_tbl(DisasContext *ctx, int gprn, int sprn)
564 gen_icount_io_start(ctx);
565 gen_helper_load_tbl(cpu_gpr[gprn], cpu_env);
568 void spr_read_tbu(DisasContext *ctx, int gprn, int sprn)
570 gen_icount_io_start(ctx);
571 gen_helper_load_tbu(cpu_gpr[gprn], cpu_env);
574 void spr_read_atbl(DisasContext *ctx, int gprn, int sprn)
576 gen_helper_load_atbl(cpu_gpr[gprn], cpu_env);
579 void spr_read_atbu(DisasContext *ctx, int gprn, int sprn)
581 gen_helper_load_atbu(cpu_gpr[gprn], cpu_env);
584 #if !defined(CONFIG_USER_ONLY)
585 void spr_write_tbl(DisasContext *ctx, int sprn, int gprn)
587 gen_icount_io_start(ctx);
588 gen_helper_store_tbl(cpu_env, cpu_gpr[gprn]);
591 void spr_write_tbu(DisasContext *ctx, int sprn, int gprn)
593 gen_icount_io_start(ctx);
594 gen_helper_store_tbu(cpu_env, cpu_gpr[gprn]);
597 void spr_write_atbl(DisasContext *ctx, int sprn, int gprn)
599 gen_helper_store_atbl(cpu_env, cpu_gpr[gprn]);
602 void spr_write_atbu(DisasContext *ctx, int sprn, int gprn)
604 gen_helper_store_atbu(cpu_env, cpu_gpr[gprn]);
607 #if defined(TARGET_PPC64)
608 void spr_read_purr(DisasContext *ctx, int gprn, int sprn)
610 gen_icount_io_start(ctx);
611 gen_helper_load_purr(cpu_gpr[gprn], cpu_env);
614 void spr_write_purr(DisasContext *ctx, int sprn, int gprn)
616 gen_icount_io_start(ctx);
617 gen_helper_store_purr(cpu_env, cpu_gpr[gprn]);
620 /* HDECR */
621 void spr_read_hdecr(DisasContext *ctx, int gprn, int sprn)
623 gen_icount_io_start(ctx);
624 gen_helper_load_hdecr(cpu_gpr[gprn], cpu_env);
627 void spr_write_hdecr(DisasContext *ctx, int sprn, int gprn)
629 gen_icount_io_start(ctx);
630 gen_helper_store_hdecr(cpu_env, cpu_gpr[gprn]);
633 void spr_read_vtb(DisasContext *ctx, int gprn, int sprn)
635 gen_icount_io_start(ctx);
636 gen_helper_load_vtb(cpu_gpr[gprn], cpu_env);
639 void spr_write_vtb(DisasContext *ctx, int sprn, int gprn)
641 gen_icount_io_start(ctx);
642 gen_helper_store_vtb(cpu_env, cpu_gpr[gprn]);
645 void spr_write_tbu40(DisasContext *ctx, int sprn, int gprn)
647 gen_icount_io_start(ctx);
648 gen_helper_store_tbu40(cpu_env, cpu_gpr[gprn]);
651 #endif
652 #endif
654 #if !defined(CONFIG_USER_ONLY)
655 /* IBAT0U...IBAT0U */
656 /* IBAT0L...IBAT7L */
657 void spr_read_ibat(DisasContext *ctx, int gprn, int sprn)
659 tcg_gen_ld_tl(cpu_gpr[gprn], cpu_env,
660 offsetof(CPUPPCState,
661 IBAT[sprn & 1][(sprn - SPR_IBAT0U) / 2]));
664 void spr_read_ibat_h(DisasContext *ctx, int gprn, int sprn)
666 tcg_gen_ld_tl(cpu_gpr[gprn], cpu_env,
667 offsetof(CPUPPCState,
668 IBAT[sprn & 1][((sprn - SPR_IBAT4U) / 2) + 4]));
671 void spr_write_ibatu(DisasContext *ctx, int sprn, int gprn)
673 TCGv_i32 t0 = tcg_const_i32((sprn - SPR_IBAT0U) / 2);
674 gen_helper_store_ibatu(cpu_env, t0, cpu_gpr[gprn]);
675 tcg_temp_free_i32(t0);
678 void spr_write_ibatu_h(DisasContext *ctx, int sprn, int gprn)
680 TCGv_i32 t0 = tcg_const_i32(((sprn - SPR_IBAT4U) / 2) + 4);
681 gen_helper_store_ibatu(cpu_env, t0, cpu_gpr[gprn]);
682 tcg_temp_free_i32(t0);
685 void spr_write_ibatl(DisasContext *ctx, int sprn, int gprn)
687 TCGv_i32 t0 = tcg_const_i32((sprn - SPR_IBAT0L) / 2);
688 gen_helper_store_ibatl(cpu_env, t0, cpu_gpr[gprn]);
689 tcg_temp_free_i32(t0);
692 void spr_write_ibatl_h(DisasContext *ctx, int sprn, int gprn)
694 TCGv_i32 t0 = tcg_const_i32(((sprn - SPR_IBAT4L) / 2) + 4);
695 gen_helper_store_ibatl(cpu_env, t0, cpu_gpr[gprn]);
696 tcg_temp_free_i32(t0);
699 /* DBAT0U...DBAT7U */
700 /* DBAT0L...DBAT7L */
701 void spr_read_dbat(DisasContext *ctx, int gprn, int sprn)
703 tcg_gen_ld_tl(cpu_gpr[gprn], cpu_env,
704 offsetof(CPUPPCState,
705 DBAT[sprn & 1][(sprn - SPR_DBAT0U) / 2]));
708 void spr_read_dbat_h(DisasContext *ctx, int gprn, int sprn)
710 tcg_gen_ld_tl(cpu_gpr[gprn], cpu_env,
711 offsetof(CPUPPCState,
712 DBAT[sprn & 1][((sprn - SPR_DBAT4U) / 2) + 4]));
715 void spr_write_dbatu(DisasContext *ctx, int sprn, int gprn)
717 TCGv_i32 t0 = tcg_const_i32((sprn - SPR_DBAT0U) / 2);
718 gen_helper_store_dbatu(cpu_env, t0, cpu_gpr[gprn]);
719 tcg_temp_free_i32(t0);
722 void spr_write_dbatu_h(DisasContext *ctx, int sprn, int gprn)
724 TCGv_i32 t0 = tcg_const_i32(((sprn - SPR_DBAT4U) / 2) + 4);
725 gen_helper_store_dbatu(cpu_env, t0, cpu_gpr[gprn]);
726 tcg_temp_free_i32(t0);
729 void spr_write_dbatl(DisasContext *ctx, int sprn, int gprn)
731 TCGv_i32 t0 = tcg_const_i32((sprn - SPR_DBAT0L) / 2);
732 gen_helper_store_dbatl(cpu_env, t0, cpu_gpr[gprn]);
733 tcg_temp_free_i32(t0);
736 void spr_write_dbatl_h(DisasContext *ctx, int sprn, int gprn)
738 TCGv_i32 t0 = tcg_const_i32(((sprn - SPR_DBAT4L) / 2) + 4);
739 gen_helper_store_dbatl(cpu_env, t0, cpu_gpr[gprn]);
740 tcg_temp_free_i32(t0);
743 /* SDR1 */
744 void spr_write_sdr1(DisasContext *ctx, int sprn, int gprn)
746 gen_helper_store_sdr1(cpu_env, cpu_gpr[gprn]);
749 #if defined(TARGET_PPC64)
750 /* 64 bits PowerPC specific SPRs */
751 /* PIDR */
752 void spr_write_pidr(DisasContext *ctx, int sprn, int gprn)
754 gen_helper_store_pidr(cpu_env, cpu_gpr[gprn]);
757 void spr_write_lpidr(DisasContext *ctx, int sprn, int gprn)
759 gen_helper_store_lpidr(cpu_env, cpu_gpr[gprn]);
762 void spr_read_hior(DisasContext *ctx, int gprn, int sprn)
764 tcg_gen_ld_tl(cpu_gpr[gprn], cpu_env, offsetof(CPUPPCState, excp_prefix));
767 void spr_write_hior(DisasContext *ctx, int sprn, int gprn)
769 TCGv t0 = tcg_temp_new();
770 tcg_gen_andi_tl(t0, cpu_gpr[gprn], 0x3FFFFF00000ULL);
771 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, excp_prefix));
772 tcg_temp_free(t0);
774 void spr_write_ptcr(DisasContext *ctx, int sprn, int gprn)
776 gen_helper_store_ptcr(cpu_env, cpu_gpr[gprn]);
779 void spr_write_pcr(DisasContext *ctx, int sprn, int gprn)
781 gen_helper_store_pcr(cpu_env, cpu_gpr[gprn]);
784 /* DPDES */
785 void spr_read_dpdes(DisasContext *ctx, int gprn, int sprn)
787 gen_helper_load_dpdes(cpu_gpr[gprn], cpu_env);
790 void spr_write_dpdes(DisasContext *ctx, int sprn, int gprn)
792 gen_helper_store_dpdes(cpu_env, cpu_gpr[gprn]);
794 #endif
795 #endif
797 /* PowerPC 40x specific registers */
798 #if !defined(CONFIG_USER_ONLY)
799 void spr_read_40x_pit(DisasContext *ctx, int gprn, int sprn)
801 gen_icount_io_start(ctx);
802 gen_helper_load_40x_pit(cpu_gpr[gprn], cpu_env);
805 void spr_write_40x_pit(DisasContext *ctx, int sprn, int gprn)
807 gen_icount_io_start(ctx);
808 gen_helper_store_40x_pit(cpu_env, cpu_gpr[gprn]);
811 void spr_write_40x_dbcr0(DisasContext *ctx, int sprn, int gprn)
813 gen_icount_io_start(ctx);
814 gen_store_spr(sprn, cpu_gpr[gprn]);
815 gen_helper_store_40x_dbcr0(cpu_env, cpu_gpr[gprn]);
816 /* We must stop translation as we may have rebooted */
817 ctx->base.is_jmp = DISAS_EXIT_UPDATE;
820 void spr_write_40x_sler(DisasContext *ctx, int sprn, int gprn)
822 gen_icount_io_start(ctx);
823 gen_helper_store_40x_sler(cpu_env, cpu_gpr[gprn]);
826 void spr_write_40x_tcr(DisasContext *ctx, int sprn, int gprn)
828 gen_icount_io_start(ctx);
829 gen_helper_store_40x_tcr(cpu_env, cpu_gpr[gprn]);
832 void spr_write_40x_tsr(DisasContext *ctx, int sprn, int gprn)
834 gen_icount_io_start(ctx);
835 gen_helper_store_40x_tsr(cpu_env, cpu_gpr[gprn]);
838 void spr_write_40x_pid(DisasContext *ctx, int sprn, int gprn)
840 TCGv t0 = tcg_temp_new();
841 tcg_gen_andi_tl(t0, cpu_gpr[gprn], 0xFF);
842 gen_helper_store_40x_pid(cpu_env, t0);
843 tcg_temp_free(t0);
846 void spr_write_booke_tcr(DisasContext *ctx, int sprn, int gprn)
848 gen_icount_io_start(ctx);
849 gen_helper_store_booke_tcr(cpu_env, cpu_gpr[gprn]);
852 void spr_write_booke_tsr(DisasContext *ctx, int sprn, int gprn)
854 gen_icount_io_start(ctx);
855 gen_helper_store_booke_tsr(cpu_env, cpu_gpr[gprn]);
857 #endif
859 /* PIR */
860 #if !defined(CONFIG_USER_ONLY)
861 void spr_write_pir(DisasContext *ctx, int sprn, int gprn)
863 TCGv t0 = tcg_temp_new();
864 tcg_gen_andi_tl(t0, cpu_gpr[gprn], 0xF);
865 gen_store_spr(SPR_PIR, t0);
866 tcg_temp_free(t0);
868 #endif
870 /* SPE specific registers */
871 void spr_read_spefscr(DisasContext *ctx, int gprn, int sprn)
873 TCGv_i32 t0 = tcg_temp_new_i32();
874 tcg_gen_ld_i32(t0, cpu_env, offsetof(CPUPPCState, spe_fscr));
875 tcg_gen_extu_i32_tl(cpu_gpr[gprn], t0);
876 tcg_temp_free_i32(t0);
879 void spr_write_spefscr(DisasContext *ctx, int sprn, int gprn)
881 TCGv_i32 t0 = tcg_temp_new_i32();
882 tcg_gen_trunc_tl_i32(t0, cpu_gpr[gprn]);
883 tcg_gen_st_i32(t0, cpu_env, offsetof(CPUPPCState, spe_fscr));
884 tcg_temp_free_i32(t0);
887 #if !defined(CONFIG_USER_ONLY)
888 /* Callback used to write the exception vector base */
889 void spr_write_excp_prefix(DisasContext *ctx, int sprn, int gprn)
891 TCGv t0 = tcg_temp_new();
892 tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUPPCState, ivpr_mask));
893 tcg_gen_and_tl(t0, t0, cpu_gpr[gprn]);
894 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, excp_prefix));
895 gen_store_spr(sprn, t0);
896 tcg_temp_free(t0);
899 void spr_write_excp_vector(DisasContext *ctx, int sprn, int gprn)
901 int sprn_offs;
903 if (sprn >= SPR_BOOKE_IVOR0 && sprn <= SPR_BOOKE_IVOR15) {
904 sprn_offs = sprn - SPR_BOOKE_IVOR0;
905 } else if (sprn >= SPR_BOOKE_IVOR32 && sprn <= SPR_BOOKE_IVOR37) {
906 sprn_offs = sprn - SPR_BOOKE_IVOR32 + 32;
907 } else if (sprn >= SPR_BOOKE_IVOR38 && sprn <= SPR_BOOKE_IVOR42) {
908 sprn_offs = sprn - SPR_BOOKE_IVOR38 + 38;
909 } else {
910 qemu_log_mask(LOG_GUEST_ERROR, "Trying to write an unknown exception"
911 " vector 0x%03x\n", sprn);
912 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
913 return;
916 TCGv t0 = tcg_temp_new();
917 tcg_gen_ld_tl(t0, cpu_env, offsetof(CPUPPCState, ivor_mask));
918 tcg_gen_and_tl(t0, t0, cpu_gpr[gprn]);
919 tcg_gen_st_tl(t0, cpu_env, offsetof(CPUPPCState, excp_vectors[sprn_offs]));
920 gen_store_spr(sprn, t0);
921 tcg_temp_free(t0);
923 #endif
925 #ifdef TARGET_PPC64
926 #ifndef CONFIG_USER_ONLY
927 void spr_write_amr(DisasContext *ctx, int sprn, int gprn)
929 TCGv t0 = tcg_temp_new();
930 TCGv t1 = tcg_temp_new();
931 TCGv t2 = tcg_temp_new();
934 * Note, the HV=1 PR=0 case is handled earlier by simply using
935 * spr_write_generic for HV mode in the SPR table
938 /* Build insertion mask into t1 based on context */
939 if (ctx->pr) {
940 gen_load_spr(t1, SPR_UAMOR);
941 } else {
942 gen_load_spr(t1, SPR_AMOR);
945 /* Mask new bits into t2 */
946 tcg_gen_and_tl(t2, t1, cpu_gpr[gprn]);
948 /* Load AMR and clear new bits in t0 */
949 gen_load_spr(t0, SPR_AMR);
950 tcg_gen_andc_tl(t0, t0, t1);
952 /* Or'in new bits and write it out */
953 tcg_gen_or_tl(t0, t0, t2);
954 gen_store_spr(SPR_AMR, t0);
955 spr_store_dump_spr(SPR_AMR);
957 tcg_temp_free(t0);
958 tcg_temp_free(t1);
959 tcg_temp_free(t2);
962 void spr_write_uamor(DisasContext *ctx, int sprn, int gprn)
964 TCGv t0 = tcg_temp_new();
965 TCGv t1 = tcg_temp_new();
966 TCGv t2 = tcg_temp_new();
969 * Note, the HV=1 case is handled earlier by simply using
970 * spr_write_generic for HV mode in the SPR table
973 /* Build insertion mask into t1 based on context */
974 gen_load_spr(t1, SPR_AMOR);
976 /* Mask new bits into t2 */
977 tcg_gen_and_tl(t2, t1, cpu_gpr[gprn]);
979 /* Load AMR and clear new bits in t0 */
980 gen_load_spr(t0, SPR_UAMOR);
981 tcg_gen_andc_tl(t0, t0, t1);
983 /* Or'in new bits and write it out */
984 tcg_gen_or_tl(t0, t0, t2);
985 gen_store_spr(SPR_UAMOR, t0);
986 spr_store_dump_spr(SPR_UAMOR);
988 tcg_temp_free(t0);
989 tcg_temp_free(t1);
990 tcg_temp_free(t2);
993 void spr_write_iamr(DisasContext *ctx, int sprn, int gprn)
995 TCGv t0 = tcg_temp_new();
996 TCGv t1 = tcg_temp_new();
997 TCGv t2 = tcg_temp_new();
1000 * Note, the HV=1 case is handled earlier by simply using
1001 * spr_write_generic for HV mode in the SPR table
1004 /* Build insertion mask into t1 based on context */
1005 gen_load_spr(t1, SPR_AMOR);
1007 /* Mask new bits into t2 */
1008 tcg_gen_and_tl(t2, t1, cpu_gpr[gprn]);
1010 /* Load AMR and clear new bits in t0 */
1011 gen_load_spr(t0, SPR_IAMR);
1012 tcg_gen_andc_tl(t0, t0, t1);
1014 /* Or'in new bits and write it out */
1015 tcg_gen_or_tl(t0, t0, t2);
1016 gen_store_spr(SPR_IAMR, t0);
1017 spr_store_dump_spr(SPR_IAMR);
1019 tcg_temp_free(t0);
1020 tcg_temp_free(t1);
1021 tcg_temp_free(t2);
1023 #endif
1024 #endif
1026 #ifndef CONFIG_USER_ONLY
1027 void spr_read_thrm(DisasContext *ctx, int gprn, int sprn)
1029 gen_helper_fixup_thrm(cpu_env);
1030 gen_load_spr(cpu_gpr[gprn], sprn);
1031 spr_load_dump_spr(sprn);
1033 #endif /* !CONFIG_USER_ONLY */
1035 #if !defined(CONFIG_USER_ONLY)
1036 void spr_write_e500_l1csr0(DisasContext *ctx, int sprn, int gprn)
1038 TCGv t0 = tcg_temp_new();
1040 tcg_gen_andi_tl(t0, cpu_gpr[gprn], L1CSR0_DCE | L1CSR0_CPE);
1041 gen_store_spr(sprn, t0);
1042 tcg_temp_free(t0);
1045 void spr_write_e500_l1csr1(DisasContext *ctx, int sprn, int gprn)
1047 TCGv t0 = tcg_temp_new();
1049 tcg_gen_andi_tl(t0, cpu_gpr[gprn], L1CSR1_ICE | L1CSR1_CPE);
1050 gen_store_spr(sprn, t0);
1051 tcg_temp_free(t0);
1054 void spr_write_e500_l2csr0(DisasContext *ctx, int sprn, int gprn)
1056 TCGv t0 = tcg_temp_new();
1058 tcg_gen_andi_tl(t0, cpu_gpr[gprn],
1059 ~(E500_L2CSR0_L2FI | E500_L2CSR0_L2FL | E500_L2CSR0_L2LFC));
1060 gen_store_spr(sprn, t0);
1061 tcg_temp_free(t0);
1064 void spr_write_booke206_mmucsr0(DisasContext *ctx, int sprn, int gprn)
1066 gen_helper_booke206_tlbflush(cpu_env, cpu_gpr[gprn]);
1069 void spr_write_booke_pid(DisasContext *ctx, int sprn, int gprn)
1071 TCGv_i32 t0 = tcg_const_i32(sprn);
1072 gen_helper_booke_setpid(cpu_env, t0, cpu_gpr[gprn]);
1073 tcg_temp_free_i32(t0);
1075 void spr_write_eplc(DisasContext *ctx, int sprn, int gprn)
1077 gen_helper_booke_set_eplc(cpu_env, cpu_gpr[gprn]);
1079 void spr_write_epsc(DisasContext *ctx, int sprn, int gprn)
1081 gen_helper_booke_set_epsc(cpu_env, cpu_gpr[gprn]);
1084 #endif
1086 #if !defined(CONFIG_USER_ONLY)
1087 void spr_write_mas73(DisasContext *ctx, int sprn, int gprn)
1089 TCGv val = tcg_temp_new();
1090 tcg_gen_ext32u_tl(val, cpu_gpr[gprn]);
1091 gen_store_spr(SPR_BOOKE_MAS3, val);
1092 tcg_gen_shri_tl(val, cpu_gpr[gprn], 32);
1093 gen_store_spr(SPR_BOOKE_MAS7, val);
1094 tcg_temp_free(val);
1097 void spr_read_mas73(DisasContext *ctx, int gprn, int sprn)
1099 TCGv mas7 = tcg_temp_new();
1100 TCGv mas3 = tcg_temp_new();
1101 gen_load_spr(mas7, SPR_BOOKE_MAS7);
1102 tcg_gen_shli_tl(mas7, mas7, 32);
1103 gen_load_spr(mas3, SPR_BOOKE_MAS3);
1104 tcg_gen_or_tl(cpu_gpr[gprn], mas3, mas7);
1105 tcg_temp_free(mas3);
1106 tcg_temp_free(mas7);
1109 #endif
1111 #ifdef TARGET_PPC64
1112 static void gen_fscr_facility_check(DisasContext *ctx, int facility_sprn,
1113 int bit, int sprn, int cause)
1115 TCGv_i32 t1 = tcg_const_i32(bit);
1116 TCGv_i32 t2 = tcg_const_i32(sprn);
1117 TCGv_i32 t3 = tcg_const_i32(cause);
1119 gen_helper_fscr_facility_check(cpu_env, t1, t2, t3);
1121 tcg_temp_free_i32(t3);
1122 tcg_temp_free_i32(t2);
1123 tcg_temp_free_i32(t1);
1126 static void gen_msr_facility_check(DisasContext *ctx, int facility_sprn,
1127 int bit, int sprn, int cause)
1129 TCGv_i32 t1 = tcg_const_i32(bit);
1130 TCGv_i32 t2 = tcg_const_i32(sprn);
1131 TCGv_i32 t3 = tcg_const_i32(cause);
1133 gen_helper_msr_facility_check(cpu_env, t1, t2, t3);
1135 tcg_temp_free_i32(t3);
1136 tcg_temp_free_i32(t2);
1137 tcg_temp_free_i32(t1);
1140 void spr_read_prev_upper32(DisasContext *ctx, int gprn, int sprn)
1142 TCGv spr_up = tcg_temp_new();
1143 TCGv spr = tcg_temp_new();
1145 gen_load_spr(spr, sprn - 1);
1146 tcg_gen_shri_tl(spr_up, spr, 32);
1147 tcg_gen_ext32u_tl(cpu_gpr[gprn], spr_up);
1149 tcg_temp_free(spr);
1150 tcg_temp_free(spr_up);
1153 void spr_write_prev_upper32(DisasContext *ctx, int sprn, int gprn)
1155 TCGv spr = tcg_temp_new();
1157 gen_load_spr(spr, sprn - 1);
1158 tcg_gen_deposit_tl(spr, spr, cpu_gpr[gprn], 32, 32);
1159 gen_store_spr(sprn - 1, spr);
1161 tcg_temp_free(spr);
1164 #if !defined(CONFIG_USER_ONLY)
1165 void spr_write_hmer(DisasContext *ctx, int sprn, int gprn)
1167 TCGv hmer = tcg_temp_new();
1169 gen_load_spr(hmer, sprn);
1170 tcg_gen_and_tl(hmer, cpu_gpr[gprn], hmer);
1171 gen_store_spr(sprn, hmer);
1172 spr_store_dump_spr(sprn);
1173 tcg_temp_free(hmer);
1176 void spr_write_lpcr(DisasContext *ctx, int sprn, int gprn)
1178 gen_helper_store_lpcr(cpu_env, cpu_gpr[gprn]);
1180 #endif /* !defined(CONFIG_USER_ONLY) */
1182 void spr_read_tar(DisasContext *ctx, int gprn, int sprn)
1184 gen_fscr_facility_check(ctx, SPR_FSCR, FSCR_TAR, sprn, FSCR_IC_TAR);
1185 spr_read_generic(ctx, gprn, sprn);
1188 void spr_write_tar(DisasContext *ctx, int sprn, int gprn)
1190 gen_fscr_facility_check(ctx, SPR_FSCR, FSCR_TAR, sprn, FSCR_IC_TAR);
1191 spr_write_generic(ctx, sprn, gprn);
1194 void spr_read_tm(DisasContext *ctx, int gprn, int sprn)
1196 gen_msr_facility_check(ctx, SPR_FSCR, MSR_TM, sprn, FSCR_IC_TM);
1197 spr_read_generic(ctx, gprn, sprn);
1200 void spr_write_tm(DisasContext *ctx, int sprn, int gprn)
1202 gen_msr_facility_check(ctx, SPR_FSCR, MSR_TM, sprn, FSCR_IC_TM);
1203 spr_write_generic(ctx, sprn, gprn);
1206 void spr_read_tm_upper32(DisasContext *ctx, int gprn, int sprn)
1208 gen_msr_facility_check(ctx, SPR_FSCR, MSR_TM, sprn, FSCR_IC_TM);
1209 spr_read_prev_upper32(ctx, gprn, sprn);
1212 void spr_write_tm_upper32(DisasContext *ctx, int sprn, int gprn)
1214 gen_msr_facility_check(ctx, SPR_FSCR, MSR_TM, sprn, FSCR_IC_TM);
1215 spr_write_prev_upper32(ctx, sprn, gprn);
1218 void spr_read_ebb(DisasContext *ctx, int gprn, int sprn)
1220 gen_fscr_facility_check(ctx, SPR_FSCR, FSCR_EBB, sprn, FSCR_IC_EBB);
1221 spr_read_generic(ctx, gprn, sprn);
1224 void spr_write_ebb(DisasContext *ctx, int sprn, int gprn)
1226 gen_fscr_facility_check(ctx, SPR_FSCR, FSCR_EBB, sprn, FSCR_IC_EBB);
1227 spr_write_generic(ctx, sprn, gprn);
1230 void spr_read_ebb_upper32(DisasContext *ctx, int gprn, int sprn)
1232 gen_fscr_facility_check(ctx, SPR_FSCR, FSCR_EBB, sprn, FSCR_IC_EBB);
1233 spr_read_prev_upper32(ctx, gprn, sprn);
1236 void spr_write_ebb_upper32(DisasContext *ctx, int sprn, int gprn)
1238 gen_fscr_facility_check(ctx, SPR_FSCR, FSCR_EBB, sprn, FSCR_IC_EBB);
1239 spr_write_prev_upper32(ctx, sprn, gprn);
1241 #endif
1243 #define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \
1244 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, PPC_NONE)
1246 #define GEN_HANDLER_E(name, opc1, opc2, opc3, inval, type, type2) \
1247 GEN_OPCODE(name, opc1, opc2, opc3, inval, type, type2)
1249 #define GEN_HANDLER2(name, onam, opc1, opc2, opc3, inval, type) \
1250 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, PPC_NONE)
1252 #define GEN_HANDLER2_E(name, onam, opc1, opc2, opc3, inval, type, type2) \
1253 GEN_OPCODE2(name, onam, opc1, opc2, opc3, inval, type, type2)
1255 #define GEN_HANDLER_E_2(name, opc1, opc2, opc3, opc4, inval, type, type2) \
1256 GEN_OPCODE3(name, opc1, opc2, opc3, opc4, inval, type, type2)
1258 #define GEN_HANDLER2_E_2(name, onam, opc1, opc2, opc3, opc4, inval, typ, typ2) \
1259 GEN_OPCODE4(name, onam, opc1, opc2, opc3, opc4, inval, typ, typ2)
1261 typedef struct opcode_t {
1262 unsigned char opc1, opc2, opc3, opc4;
1263 #if HOST_LONG_BITS == 64 /* Explicitly align to 64 bits */
1264 unsigned char pad[4];
1265 #endif
1266 opc_handler_t handler;
1267 const char *oname;
1268 } opcode_t;
1270 static void gen_priv_opc(DisasContext *ctx)
1272 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
1275 /* Helpers for priv. check */
1276 #define GEN_PRIV(CTX) \
1277 do { \
1278 gen_priv_opc(CTX); return; \
1279 } while (0)
1281 #if defined(CONFIG_USER_ONLY)
1282 #define CHK_HV(CTX) GEN_PRIV(CTX)
1283 #define CHK_SV(CTX) GEN_PRIV(CTX)
1284 #define CHK_HVRM(CTX) GEN_PRIV(CTX)
1285 #else
1286 #define CHK_HV(CTX) \
1287 do { \
1288 if (unlikely(ctx->pr || !ctx->hv)) {\
1289 GEN_PRIV(CTX); \
1291 } while (0)
1292 #define CHK_SV(CTX) \
1293 do { \
1294 if (unlikely(ctx->pr)) { \
1295 GEN_PRIV(CTX); \
1297 } while (0)
1298 #define CHK_HVRM(CTX) \
1299 do { \
1300 if (unlikely(ctx->pr || !ctx->hv || ctx->dr)) { \
1301 GEN_PRIV(CTX); \
1303 } while (0)
1304 #endif
1306 #define CHK_NONE(CTX)
1308 /*****************************************************************************/
1309 /* PowerPC instructions table */
1311 #define GEN_OPCODE(name, op1, op2, op3, invl, _typ, _typ2) \
1313 .opc1 = op1, \
1314 .opc2 = op2, \
1315 .opc3 = op3, \
1316 .opc4 = 0xff, \
1317 .handler = { \
1318 .inval1 = invl, \
1319 .type = _typ, \
1320 .type2 = _typ2, \
1321 .handler = &gen_##name, \
1322 }, \
1323 .oname = stringify(name), \
1325 #define GEN_OPCODE_DUAL(name, op1, op2, op3, invl1, invl2, _typ, _typ2) \
1327 .opc1 = op1, \
1328 .opc2 = op2, \
1329 .opc3 = op3, \
1330 .opc4 = 0xff, \
1331 .handler = { \
1332 .inval1 = invl1, \
1333 .inval2 = invl2, \
1334 .type = _typ, \
1335 .type2 = _typ2, \
1336 .handler = &gen_##name, \
1337 }, \
1338 .oname = stringify(name), \
1340 #define GEN_OPCODE2(name, onam, op1, op2, op3, invl, _typ, _typ2) \
1342 .opc1 = op1, \
1343 .opc2 = op2, \
1344 .opc3 = op3, \
1345 .opc4 = 0xff, \
1346 .handler = { \
1347 .inval1 = invl, \
1348 .type = _typ, \
1349 .type2 = _typ2, \
1350 .handler = &gen_##name, \
1351 }, \
1352 .oname = onam, \
1354 #define GEN_OPCODE3(name, op1, op2, op3, op4, invl, _typ, _typ2) \
1356 .opc1 = op1, \
1357 .opc2 = op2, \
1358 .opc3 = op3, \
1359 .opc4 = op4, \
1360 .handler = { \
1361 .inval1 = invl, \
1362 .type = _typ, \
1363 .type2 = _typ2, \
1364 .handler = &gen_##name, \
1365 }, \
1366 .oname = stringify(name), \
1368 #define GEN_OPCODE4(name, onam, op1, op2, op3, op4, invl, _typ, _typ2) \
1370 .opc1 = op1, \
1371 .opc2 = op2, \
1372 .opc3 = op3, \
1373 .opc4 = op4, \
1374 .handler = { \
1375 .inval1 = invl, \
1376 .type = _typ, \
1377 .type2 = _typ2, \
1378 .handler = &gen_##name, \
1379 }, \
1380 .oname = onam, \
1383 /* Invalid instruction */
1384 static void gen_invalid(DisasContext *ctx)
1386 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
1389 static opc_handler_t invalid_handler = {
1390 .inval1 = 0xFFFFFFFF,
1391 .inval2 = 0xFFFFFFFF,
1392 .type = PPC_NONE,
1393 .type2 = PPC_NONE,
1394 .handler = gen_invalid,
1397 /*** Integer comparison ***/
1399 static inline void gen_op_cmp(TCGv arg0, TCGv arg1, int s, int crf)
1401 TCGv t0 = tcg_temp_new();
1402 TCGv t1 = tcg_temp_new();
1403 TCGv_i32 t = tcg_temp_new_i32();
1405 tcg_gen_movi_tl(t0, CRF_EQ);
1406 tcg_gen_movi_tl(t1, CRF_LT);
1407 tcg_gen_movcond_tl((s ? TCG_COND_LT : TCG_COND_LTU),
1408 t0, arg0, arg1, t1, t0);
1409 tcg_gen_movi_tl(t1, CRF_GT);
1410 tcg_gen_movcond_tl((s ? TCG_COND_GT : TCG_COND_GTU),
1411 t0, arg0, arg1, t1, t0);
1413 tcg_gen_trunc_tl_i32(t, t0);
1414 tcg_gen_trunc_tl_i32(cpu_crf[crf], cpu_so);
1415 tcg_gen_or_i32(cpu_crf[crf], cpu_crf[crf], t);
1417 tcg_temp_free(t0);
1418 tcg_temp_free(t1);
1419 tcg_temp_free_i32(t);
1422 static inline void gen_op_cmpi(TCGv arg0, target_ulong arg1, int s, int crf)
1424 TCGv t0 = tcg_const_tl(arg1);
1425 gen_op_cmp(arg0, t0, s, crf);
1426 tcg_temp_free(t0);
1429 static inline void gen_op_cmp32(TCGv arg0, TCGv arg1, int s, int crf)
1431 TCGv t0, t1;
1432 t0 = tcg_temp_new();
1433 t1 = tcg_temp_new();
1434 if (s) {
1435 tcg_gen_ext32s_tl(t0, arg0);
1436 tcg_gen_ext32s_tl(t1, arg1);
1437 } else {
1438 tcg_gen_ext32u_tl(t0, arg0);
1439 tcg_gen_ext32u_tl(t1, arg1);
1441 gen_op_cmp(t0, t1, s, crf);
1442 tcg_temp_free(t1);
1443 tcg_temp_free(t0);
1446 static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
1448 TCGv t0 = tcg_const_tl(arg1);
1449 gen_op_cmp32(arg0, t0, s, crf);
1450 tcg_temp_free(t0);
1453 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
1455 if (NARROW_MODE(ctx)) {
1456 gen_op_cmpi32(reg, 0, 1, 0);
1457 } else {
1458 gen_op_cmpi(reg, 0, 1, 0);
1462 /* cmprb - range comparison: isupper, isaplha, islower*/
1463 static void gen_cmprb(DisasContext *ctx)
1465 TCGv_i32 src1 = tcg_temp_new_i32();
1466 TCGv_i32 src2 = tcg_temp_new_i32();
1467 TCGv_i32 src2lo = tcg_temp_new_i32();
1468 TCGv_i32 src2hi = tcg_temp_new_i32();
1469 TCGv_i32 crf = cpu_crf[crfD(ctx->opcode)];
1471 tcg_gen_trunc_tl_i32(src1, cpu_gpr[rA(ctx->opcode)]);
1472 tcg_gen_trunc_tl_i32(src2, cpu_gpr[rB(ctx->opcode)]);
1474 tcg_gen_andi_i32(src1, src1, 0xFF);
1475 tcg_gen_ext8u_i32(src2lo, src2);
1476 tcg_gen_shri_i32(src2, src2, 8);
1477 tcg_gen_ext8u_i32(src2hi, src2);
1479 tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
1480 tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
1481 tcg_gen_and_i32(crf, src2lo, src2hi);
1483 if (ctx->opcode & 0x00200000) {
1484 tcg_gen_shri_i32(src2, src2, 8);
1485 tcg_gen_ext8u_i32(src2lo, src2);
1486 tcg_gen_shri_i32(src2, src2, 8);
1487 tcg_gen_ext8u_i32(src2hi, src2);
1488 tcg_gen_setcond_i32(TCG_COND_LEU, src2lo, src2lo, src1);
1489 tcg_gen_setcond_i32(TCG_COND_LEU, src2hi, src1, src2hi);
1490 tcg_gen_and_i32(src2lo, src2lo, src2hi);
1491 tcg_gen_or_i32(crf, crf, src2lo);
1493 tcg_gen_shli_i32(crf, crf, CRF_GT_BIT);
1494 tcg_temp_free_i32(src1);
1495 tcg_temp_free_i32(src2);
1496 tcg_temp_free_i32(src2lo);
1497 tcg_temp_free_i32(src2hi);
1500 #if defined(TARGET_PPC64)
1501 /* cmpeqb */
1502 static void gen_cmpeqb(DisasContext *ctx)
1504 gen_helper_cmpeqb(cpu_crf[crfD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1505 cpu_gpr[rB(ctx->opcode)]);
1507 #endif
1509 /* isel (PowerPC 2.03 specification) */
1510 static void gen_isel(DisasContext *ctx)
1512 uint32_t bi = rC(ctx->opcode);
1513 uint32_t mask = 0x08 >> (bi & 0x03);
1514 TCGv t0 = tcg_temp_new();
1515 TCGv zr;
1517 tcg_gen_extu_i32_tl(t0, cpu_crf[bi >> 2]);
1518 tcg_gen_andi_tl(t0, t0, mask);
1520 zr = tcg_const_tl(0);
1521 tcg_gen_movcond_tl(TCG_COND_NE, cpu_gpr[rD(ctx->opcode)], t0, zr,
1522 rA(ctx->opcode) ? cpu_gpr[rA(ctx->opcode)] : zr,
1523 cpu_gpr[rB(ctx->opcode)]);
1524 tcg_temp_free(zr);
1525 tcg_temp_free(t0);
1528 /* cmpb: PowerPC 2.05 specification */
1529 static void gen_cmpb(DisasContext *ctx)
1531 gen_helper_cmpb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
1532 cpu_gpr[rB(ctx->opcode)]);
1535 /*** Integer arithmetic ***/
1537 static inline void gen_op_arith_compute_ov(DisasContext *ctx, TCGv arg0,
1538 TCGv arg1, TCGv arg2, int sub)
1540 TCGv t0 = tcg_temp_new();
1542 tcg_gen_xor_tl(cpu_ov, arg0, arg2);
1543 tcg_gen_xor_tl(t0, arg1, arg2);
1544 if (sub) {
1545 tcg_gen_and_tl(cpu_ov, cpu_ov, t0);
1546 } else {
1547 tcg_gen_andc_tl(cpu_ov, cpu_ov, t0);
1549 tcg_temp_free(t0);
1550 if (NARROW_MODE(ctx)) {
1551 tcg_gen_extract_tl(cpu_ov, cpu_ov, 31, 1);
1552 if (is_isa300(ctx)) {
1553 tcg_gen_mov_tl(cpu_ov32, cpu_ov);
1555 } else {
1556 if (is_isa300(ctx)) {
1557 tcg_gen_extract_tl(cpu_ov32, cpu_ov, 31, 1);
1559 tcg_gen_extract_tl(cpu_ov, cpu_ov, TARGET_LONG_BITS - 1, 1);
1561 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1564 static inline void gen_op_arith_compute_ca32(DisasContext *ctx,
1565 TCGv res, TCGv arg0, TCGv arg1,
1566 TCGv ca32, int sub)
1568 TCGv t0;
1570 if (!is_isa300(ctx)) {
1571 return;
1574 t0 = tcg_temp_new();
1575 if (sub) {
1576 tcg_gen_eqv_tl(t0, arg0, arg1);
1577 } else {
1578 tcg_gen_xor_tl(t0, arg0, arg1);
1580 tcg_gen_xor_tl(t0, t0, res);
1581 tcg_gen_extract_tl(ca32, t0, 32, 1);
1582 tcg_temp_free(t0);
1585 /* Common add function */
1586 static inline void gen_op_arith_add(DisasContext *ctx, TCGv ret, TCGv arg1,
1587 TCGv arg2, TCGv ca, TCGv ca32,
1588 bool add_ca, bool compute_ca,
1589 bool compute_ov, bool compute_rc0)
1591 TCGv t0 = ret;
1593 if (compute_ca || compute_ov) {
1594 t0 = tcg_temp_new();
1597 if (compute_ca) {
1598 if (NARROW_MODE(ctx)) {
1600 * Caution: a non-obvious corner case of the spec is that
1601 * we must produce the *entire* 64-bit addition, but
1602 * produce the carry into bit 32.
1604 TCGv t1 = tcg_temp_new();
1605 tcg_gen_xor_tl(t1, arg1, arg2); /* add without carry */
1606 tcg_gen_add_tl(t0, arg1, arg2);
1607 if (add_ca) {
1608 tcg_gen_add_tl(t0, t0, ca);
1610 tcg_gen_xor_tl(ca, t0, t1); /* bits changed w/ carry */
1611 tcg_temp_free(t1);
1612 tcg_gen_extract_tl(ca, ca, 32, 1);
1613 if (is_isa300(ctx)) {
1614 tcg_gen_mov_tl(ca32, ca);
1616 } else {
1617 TCGv zero = tcg_const_tl(0);
1618 if (add_ca) {
1619 tcg_gen_add2_tl(t0, ca, arg1, zero, ca, zero);
1620 tcg_gen_add2_tl(t0, ca, t0, ca, arg2, zero);
1621 } else {
1622 tcg_gen_add2_tl(t0, ca, arg1, zero, arg2, zero);
1624 gen_op_arith_compute_ca32(ctx, t0, arg1, arg2, ca32, 0);
1625 tcg_temp_free(zero);
1627 } else {
1628 tcg_gen_add_tl(t0, arg1, arg2);
1629 if (add_ca) {
1630 tcg_gen_add_tl(t0, t0, ca);
1634 if (compute_ov) {
1635 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 0);
1637 if (unlikely(compute_rc0)) {
1638 gen_set_Rc0(ctx, t0);
1641 if (t0 != ret) {
1642 tcg_gen_mov_tl(ret, t0);
1643 tcg_temp_free(t0);
1646 /* Add functions with two operands */
1647 #define GEN_INT_ARITH_ADD(name, opc3, ca, add_ca, compute_ca, compute_ov) \
1648 static void glue(gen_, name)(DisasContext *ctx) \
1650 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
1651 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1652 ca, glue(ca, 32), \
1653 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
1655 /* Add functions with one operand and one immediate */
1656 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, ca, \
1657 add_ca, compute_ca, compute_ov) \
1658 static void glue(gen_, name)(DisasContext *ctx) \
1660 TCGv t0 = tcg_const_tl(const_val); \
1661 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], \
1662 cpu_gpr[rA(ctx->opcode)], t0, \
1663 ca, glue(ca, 32), \
1664 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
1665 tcg_temp_free(t0); \
1668 /* add add. addo addo. */
1669 GEN_INT_ARITH_ADD(add, 0x08, cpu_ca, 0, 0, 0)
1670 GEN_INT_ARITH_ADD(addo, 0x18, cpu_ca, 0, 0, 1)
1671 /* addc addc. addco addco. */
1672 GEN_INT_ARITH_ADD(addc, 0x00, cpu_ca, 0, 1, 0)
1673 GEN_INT_ARITH_ADD(addco, 0x10, cpu_ca, 0, 1, 1)
1674 /* adde adde. addeo addeo. */
1675 GEN_INT_ARITH_ADD(adde, 0x04, cpu_ca, 1, 1, 0)
1676 GEN_INT_ARITH_ADD(addeo, 0x14, cpu_ca, 1, 1, 1)
1677 /* addme addme. addmeo addmeo. */
1678 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, cpu_ca, 1, 1, 0)
1679 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, cpu_ca, 1, 1, 1)
1680 /* addex */
1681 GEN_INT_ARITH_ADD(addex, 0x05, cpu_ov, 1, 1, 0);
1682 /* addze addze. addzeo addzeo.*/
1683 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, cpu_ca, 1, 1, 0)
1684 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, cpu_ca, 1, 1, 1)
1685 /* addic addic.*/
1686 static inline void gen_op_addic(DisasContext *ctx, bool compute_rc0)
1688 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
1689 gen_op_arith_add(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1690 c, cpu_ca, cpu_ca32, 0, 1, 0, compute_rc0);
1691 tcg_temp_free(c);
1694 static void gen_addic(DisasContext *ctx)
1696 gen_op_addic(ctx, 0);
1699 static void gen_addic_(DisasContext *ctx)
1701 gen_op_addic(ctx, 1);
1704 static inline void gen_op_arith_divw(DisasContext *ctx, TCGv ret, TCGv arg1,
1705 TCGv arg2, int sign, int compute_ov)
1707 TCGv_i32 t0 = tcg_temp_new_i32();
1708 TCGv_i32 t1 = tcg_temp_new_i32();
1709 TCGv_i32 t2 = tcg_temp_new_i32();
1710 TCGv_i32 t3 = tcg_temp_new_i32();
1712 tcg_gen_trunc_tl_i32(t0, arg1);
1713 tcg_gen_trunc_tl_i32(t1, arg2);
1714 if (sign) {
1715 tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1716 tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1717 tcg_gen_and_i32(t2, t2, t3);
1718 tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1719 tcg_gen_or_i32(t2, t2, t3);
1720 tcg_gen_movi_i32(t3, 0);
1721 tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1722 tcg_gen_div_i32(t3, t0, t1);
1723 tcg_gen_extu_i32_tl(ret, t3);
1724 } else {
1725 tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t1, 0);
1726 tcg_gen_movi_i32(t3, 0);
1727 tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1728 tcg_gen_divu_i32(t3, t0, t1);
1729 tcg_gen_extu_i32_tl(ret, t3);
1731 if (compute_ov) {
1732 tcg_gen_extu_i32_tl(cpu_ov, t2);
1733 if (is_isa300(ctx)) {
1734 tcg_gen_extu_i32_tl(cpu_ov32, t2);
1736 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1738 tcg_temp_free_i32(t0);
1739 tcg_temp_free_i32(t1);
1740 tcg_temp_free_i32(t2);
1741 tcg_temp_free_i32(t3);
1743 if (unlikely(Rc(ctx->opcode) != 0)) {
1744 gen_set_Rc0(ctx, ret);
1747 /* Div functions */
1748 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
1749 static void glue(gen_, name)(DisasContext *ctx) \
1751 gen_op_arith_divw(ctx, cpu_gpr[rD(ctx->opcode)], \
1752 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1753 sign, compute_ov); \
1755 /* divwu divwu. divwuo divwuo. */
1756 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0);
1757 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1);
1758 /* divw divw. divwo divwo. */
1759 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0);
1760 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1);
1762 /* div[wd]eu[o][.] */
1763 #define GEN_DIVE(name, hlpr, compute_ov) \
1764 static void gen_##name(DisasContext *ctx) \
1766 TCGv_i32 t0 = tcg_const_i32(compute_ov); \
1767 gen_helper_##hlpr(cpu_gpr[rD(ctx->opcode)], cpu_env, \
1768 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0); \
1769 tcg_temp_free_i32(t0); \
1770 if (unlikely(Rc(ctx->opcode) != 0)) { \
1771 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]); \
1775 GEN_DIVE(divweu, divweu, 0);
1776 GEN_DIVE(divweuo, divweu, 1);
1777 GEN_DIVE(divwe, divwe, 0);
1778 GEN_DIVE(divweo, divwe, 1);
1780 #if defined(TARGET_PPC64)
1781 static inline void gen_op_arith_divd(DisasContext *ctx, TCGv ret, TCGv arg1,
1782 TCGv arg2, int sign, int compute_ov)
1784 TCGv_i64 t0 = tcg_temp_new_i64();
1785 TCGv_i64 t1 = tcg_temp_new_i64();
1786 TCGv_i64 t2 = tcg_temp_new_i64();
1787 TCGv_i64 t3 = tcg_temp_new_i64();
1789 tcg_gen_mov_i64(t0, arg1);
1790 tcg_gen_mov_i64(t1, arg2);
1791 if (sign) {
1792 tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t0, INT64_MIN);
1793 tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, -1);
1794 tcg_gen_and_i64(t2, t2, t3);
1795 tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, 0);
1796 tcg_gen_or_i64(t2, t2, t3);
1797 tcg_gen_movi_i64(t3, 0);
1798 tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1799 tcg_gen_div_i64(ret, t0, t1);
1800 } else {
1801 tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t1, 0);
1802 tcg_gen_movi_i64(t3, 0);
1803 tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1804 tcg_gen_divu_i64(ret, t0, t1);
1806 if (compute_ov) {
1807 tcg_gen_mov_tl(cpu_ov, t2);
1808 if (is_isa300(ctx)) {
1809 tcg_gen_mov_tl(cpu_ov32, t2);
1811 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
1813 tcg_temp_free_i64(t0);
1814 tcg_temp_free_i64(t1);
1815 tcg_temp_free_i64(t2);
1816 tcg_temp_free_i64(t3);
1818 if (unlikely(Rc(ctx->opcode) != 0)) {
1819 gen_set_Rc0(ctx, ret);
1823 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
1824 static void glue(gen_, name)(DisasContext *ctx) \
1826 gen_op_arith_divd(ctx, cpu_gpr[rD(ctx->opcode)], \
1827 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1828 sign, compute_ov); \
1830 /* divdu divdu. divduo divduo. */
1831 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0);
1832 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1);
1833 /* divd divd. divdo divdo. */
1834 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0);
1835 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1);
1837 GEN_DIVE(divdeu, divdeu, 0);
1838 GEN_DIVE(divdeuo, divdeu, 1);
1839 GEN_DIVE(divde, divde, 0);
1840 GEN_DIVE(divdeo, divde, 1);
1841 #endif
1843 static inline void gen_op_arith_modw(DisasContext *ctx, TCGv ret, TCGv arg1,
1844 TCGv arg2, int sign)
1846 TCGv_i32 t0 = tcg_temp_new_i32();
1847 TCGv_i32 t1 = tcg_temp_new_i32();
1849 tcg_gen_trunc_tl_i32(t0, arg1);
1850 tcg_gen_trunc_tl_i32(t1, arg2);
1851 if (sign) {
1852 TCGv_i32 t2 = tcg_temp_new_i32();
1853 TCGv_i32 t3 = tcg_temp_new_i32();
1854 tcg_gen_setcondi_i32(TCG_COND_EQ, t2, t0, INT_MIN);
1855 tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, -1);
1856 tcg_gen_and_i32(t2, t2, t3);
1857 tcg_gen_setcondi_i32(TCG_COND_EQ, t3, t1, 0);
1858 tcg_gen_or_i32(t2, t2, t3);
1859 tcg_gen_movi_i32(t3, 0);
1860 tcg_gen_movcond_i32(TCG_COND_NE, t1, t2, t3, t2, t1);
1861 tcg_gen_rem_i32(t3, t0, t1);
1862 tcg_gen_ext_i32_tl(ret, t3);
1863 tcg_temp_free_i32(t2);
1864 tcg_temp_free_i32(t3);
1865 } else {
1866 TCGv_i32 t2 = tcg_const_i32(1);
1867 TCGv_i32 t3 = tcg_const_i32(0);
1868 tcg_gen_movcond_i32(TCG_COND_EQ, t1, t1, t3, t2, t1);
1869 tcg_gen_remu_i32(t3, t0, t1);
1870 tcg_gen_extu_i32_tl(ret, t3);
1871 tcg_temp_free_i32(t2);
1872 tcg_temp_free_i32(t3);
1874 tcg_temp_free_i32(t0);
1875 tcg_temp_free_i32(t1);
1878 #define GEN_INT_ARITH_MODW(name, opc3, sign) \
1879 static void glue(gen_, name)(DisasContext *ctx) \
1881 gen_op_arith_modw(ctx, cpu_gpr[rD(ctx->opcode)], \
1882 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1883 sign); \
1886 GEN_INT_ARITH_MODW(moduw, 0x08, 0);
1887 GEN_INT_ARITH_MODW(modsw, 0x18, 1);
1889 #if defined(TARGET_PPC64)
1890 static inline void gen_op_arith_modd(DisasContext *ctx, TCGv ret, TCGv arg1,
1891 TCGv arg2, int sign)
1893 TCGv_i64 t0 = tcg_temp_new_i64();
1894 TCGv_i64 t1 = tcg_temp_new_i64();
1896 tcg_gen_mov_i64(t0, arg1);
1897 tcg_gen_mov_i64(t1, arg2);
1898 if (sign) {
1899 TCGv_i64 t2 = tcg_temp_new_i64();
1900 TCGv_i64 t3 = tcg_temp_new_i64();
1901 tcg_gen_setcondi_i64(TCG_COND_EQ, t2, t0, INT64_MIN);
1902 tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, -1);
1903 tcg_gen_and_i64(t2, t2, t3);
1904 tcg_gen_setcondi_i64(TCG_COND_EQ, t3, t1, 0);
1905 tcg_gen_or_i64(t2, t2, t3);
1906 tcg_gen_movi_i64(t3, 0);
1907 tcg_gen_movcond_i64(TCG_COND_NE, t1, t2, t3, t2, t1);
1908 tcg_gen_rem_i64(ret, t0, t1);
1909 tcg_temp_free_i64(t2);
1910 tcg_temp_free_i64(t3);
1911 } else {
1912 TCGv_i64 t2 = tcg_const_i64(1);
1913 TCGv_i64 t3 = tcg_const_i64(0);
1914 tcg_gen_movcond_i64(TCG_COND_EQ, t1, t1, t3, t2, t1);
1915 tcg_gen_remu_i64(ret, t0, t1);
1916 tcg_temp_free_i64(t2);
1917 tcg_temp_free_i64(t3);
1919 tcg_temp_free_i64(t0);
1920 tcg_temp_free_i64(t1);
1923 #define GEN_INT_ARITH_MODD(name, opc3, sign) \
1924 static void glue(gen_, name)(DisasContext *ctx) \
1926 gen_op_arith_modd(ctx, cpu_gpr[rD(ctx->opcode)], \
1927 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
1928 sign); \
1931 GEN_INT_ARITH_MODD(modud, 0x08, 0);
1932 GEN_INT_ARITH_MODD(modsd, 0x18, 1);
1933 #endif
1935 /* mulhw mulhw. */
1936 static void gen_mulhw(DisasContext *ctx)
1938 TCGv_i32 t0 = tcg_temp_new_i32();
1939 TCGv_i32 t1 = tcg_temp_new_i32();
1941 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1942 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1943 tcg_gen_muls2_i32(t0, t1, t0, t1);
1944 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1945 tcg_temp_free_i32(t0);
1946 tcg_temp_free_i32(t1);
1947 if (unlikely(Rc(ctx->opcode) != 0)) {
1948 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1952 /* mulhwu mulhwu. */
1953 static void gen_mulhwu(DisasContext *ctx)
1955 TCGv_i32 t0 = tcg_temp_new_i32();
1956 TCGv_i32 t1 = tcg_temp_new_i32();
1958 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1959 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1960 tcg_gen_mulu2_i32(t0, t1, t0, t1);
1961 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t1);
1962 tcg_temp_free_i32(t0);
1963 tcg_temp_free_i32(t1);
1964 if (unlikely(Rc(ctx->opcode) != 0)) {
1965 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1969 /* mullw mullw. */
1970 static void gen_mullw(DisasContext *ctx)
1972 #if defined(TARGET_PPC64)
1973 TCGv_i64 t0, t1;
1974 t0 = tcg_temp_new_i64();
1975 t1 = tcg_temp_new_i64();
1976 tcg_gen_ext32s_tl(t0, cpu_gpr[rA(ctx->opcode)]);
1977 tcg_gen_ext32s_tl(t1, cpu_gpr[rB(ctx->opcode)]);
1978 tcg_gen_mul_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
1979 tcg_temp_free(t0);
1980 tcg_temp_free(t1);
1981 #else
1982 tcg_gen_mul_i32(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
1983 cpu_gpr[rB(ctx->opcode)]);
1984 #endif
1985 if (unlikely(Rc(ctx->opcode) != 0)) {
1986 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
1990 /* mullwo mullwo. */
1991 static void gen_mullwo(DisasContext *ctx)
1993 TCGv_i32 t0 = tcg_temp_new_i32();
1994 TCGv_i32 t1 = tcg_temp_new_i32();
1996 tcg_gen_trunc_tl_i32(t0, cpu_gpr[rA(ctx->opcode)]);
1997 tcg_gen_trunc_tl_i32(t1, cpu_gpr[rB(ctx->opcode)]);
1998 tcg_gen_muls2_i32(t0, t1, t0, t1);
1999 #if defined(TARGET_PPC64)
2000 tcg_gen_concat_i32_i64(cpu_gpr[rD(ctx->opcode)], t0, t1);
2001 #else
2002 tcg_gen_mov_i32(cpu_gpr[rD(ctx->opcode)], t0);
2003 #endif
2005 tcg_gen_sari_i32(t0, t0, 31);
2006 tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t1);
2007 tcg_gen_extu_i32_tl(cpu_ov, t0);
2008 if (is_isa300(ctx)) {
2009 tcg_gen_mov_tl(cpu_ov32, cpu_ov);
2011 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
2013 tcg_temp_free_i32(t0);
2014 tcg_temp_free_i32(t1);
2015 if (unlikely(Rc(ctx->opcode) != 0)) {
2016 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
2020 /* mulli */
2021 static void gen_mulli(DisasContext *ctx)
2023 tcg_gen_muli_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
2024 SIMM(ctx->opcode));
2027 #if defined(TARGET_PPC64)
2028 /* mulhd mulhd. */
2029 static void gen_mulhd(DisasContext *ctx)
2031 TCGv lo = tcg_temp_new();
2032 tcg_gen_muls2_tl(lo, cpu_gpr[rD(ctx->opcode)],
2033 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2034 tcg_temp_free(lo);
2035 if (unlikely(Rc(ctx->opcode) != 0)) {
2036 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
2040 /* mulhdu mulhdu. */
2041 static void gen_mulhdu(DisasContext *ctx)
2043 TCGv lo = tcg_temp_new();
2044 tcg_gen_mulu2_tl(lo, cpu_gpr[rD(ctx->opcode)],
2045 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2046 tcg_temp_free(lo);
2047 if (unlikely(Rc(ctx->opcode) != 0)) {
2048 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
2052 /* mulld mulld. */
2053 static void gen_mulld(DisasContext *ctx)
2055 tcg_gen_mul_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
2056 cpu_gpr[rB(ctx->opcode)]);
2057 if (unlikely(Rc(ctx->opcode) != 0)) {
2058 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
2062 /* mulldo mulldo. */
2063 static void gen_mulldo(DisasContext *ctx)
2065 TCGv_i64 t0 = tcg_temp_new_i64();
2066 TCGv_i64 t1 = tcg_temp_new_i64();
2068 tcg_gen_muls2_i64(t0, t1, cpu_gpr[rA(ctx->opcode)],
2069 cpu_gpr[rB(ctx->opcode)]);
2070 tcg_gen_mov_i64(cpu_gpr[rD(ctx->opcode)], t0);
2072 tcg_gen_sari_i64(t0, t0, 63);
2073 tcg_gen_setcond_i64(TCG_COND_NE, cpu_ov, t0, t1);
2074 if (is_isa300(ctx)) {
2075 tcg_gen_mov_tl(cpu_ov32, cpu_ov);
2077 tcg_gen_or_tl(cpu_so, cpu_so, cpu_ov);
2079 tcg_temp_free_i64(t0);
2080 tcg_temp_free_i64(t1);
2082 if (unlikely(Rc(ctx->opcode) != 0)) {
2083 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
2086 #endif
2088 /* Common subf function */
2089 static inline void gen_op_arith_subf(DisasContext *ctx, TCGv ret, TCGv arg1,
2090 TCGv arg2, bool add_ca, bool compute_ca,
2091 bool compute_ov, bool compute_rc0)
2093 TCGv t0 = ret;
2095 if (compute_ca || compute_ov) {
2096 t0 = tcg_temp_new();
2099 if (compute_ca) {
2100 /* dest = ~arg1 + arg2 [+ ca]. */
2101 if (NARROW_MODE(ctx)) {
2103 * Caution: a non-obvious corner case of the spec is that
2104 * we must produce the *entire* 64-bit addition, but
2105 * produce the carry into bit 32.
2107 TCGv inv1 = tcg_temp_new();
2108 TCGv t1 = tcg_temp_new();
2109 tcg_gen_not_tl(inv1, arg1);
2110 if (add_ca) {
2111 tcg_gen_add_tl(t0, arg2, cpu_ca);
2112 } else {
2113 tcg_gen_addi_tl(t0, arg2, 1);
2115 tcg_gen_xor_tl(t1, arg2, inv1); /* add without carry */
2116 tcg_gen_add_tl(t0, t0, inv1);
2117 tcg_temp_free(inv1);
2118 tcg_gen_xor_tl(cpu_ca, t0, t1); /* bits changes w/ carry */
2119 tcg_temp_free(t1);
2120 tcg_gen_extract_tl(cpu_ca, cpu_ca, 32, 1);
2121 if (is_isa300(ctx)) {
2122 tcg_gen_mov_tl(cpu_ca32, cpu_ca);
2124 } else if (add_ca) {
2125 TCGv zero, inv1 = tcg_temp_new();
2126 tcg_gen_not_tl(inv1, arg1);
2127 zero = tcg_const_tl(0);
2128 tcg_gen_add2_tl(t0, cpu_ca, arg2, zero, cpu_ca, zero);
2129 tcg_gen_add2_tl(t0, cpu_ca, t0, cpu_ca, inv1, zero);
2130 gen_op_arith_compute_ca32(ctx, t0, inv1, arg2, cpu_ca32, 0);
2131 tcg_temp_free(zero);
2132 tcg_temp_free(inv1);
2133 } else {
2134 tcg_gen_setcond_tl(TCG_COND_GEU, cpu_ca, arg2, arg1);
2135 tcg_gen_sub_tl(t0, arg2, arg1);
2136 gen_op_arith_compute_ca32(ctx, t0, arg1, arg2, cpu_ca32, 1);
2138 } else if (add_ca) {
2140 * Since we're ignoring carry-out, we can simplify the
2141 * standard ~arg1 + arg2 + ca to arg2 - arg1 + ca - 1.
2143 tcg_gen_sub_tl(t0, arg2, arg1);
2144 tcg_gen_add_tl(t0, t0, cpu_ca);
2145 tcg_gen_subi_tl(t0, t0, 1);
2146 } else {
2147 tcg_gen_sub_tl(t0, arg2, arg1);
2150 if (compute_ov) {
2151 gen_op_arith_compute_ov(ctx, t0, arg1, arg2, 1);
2153 if (unlikely(compute_rc0)) {
2154 gen_set_Rc0(ctx, t0);
2157 if (t0 != ret) {
2158 tcg_gen_mov_tl(ret, t0);
2159 tcg_temp_free(t0);
2162 /* Sub functions with Two operands functions */
2163 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
2164 static void glue(gen_, name)(DisasContext *ctx) \
2166 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
2167 cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], \
2168 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
2170 /* Sub functions with one operand and one immediate */
2171 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
2172 add_ca, compute_ca, compute_ov) \
2173 static void glue(gen_, name)(DisasContext *ctx) \
2175 TCGv t0 = tcg_const_tl(const_val); \
2176 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], \
2177 cpu_gpr[rA(ctx->opcode)], t0, \
2178 add_ca, compute_ca, compute_ov, Rc(ctx->opcode)); \
2179 tcg_temp_free(t0); \
2181 /* subf subf. subfo subfo. */
2182 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
2183 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
2184 /* subfc subfc. subfco subfco. */
2185 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
2186 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
2187 /* subfe subfe. subfeo subfo. */
2188 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
2189 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
2190 /* subfme subfme. subfmeo subfmeo. */
2191 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
2192 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
2193 /* subfze subfze. subfzeo subfzeo.*/
2194 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
2195 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
2197 /* subfic */
2198 static void gen_subfic(DisasContext *ctx)
2200 TCGv c = tcg_const_tl(SIMM(ctx->opcode));
2201 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
2202 c, 0, 1, 0, 0);
2203 tcg_temp_free(c);
2206 /* neg neg. nego nego. */
2207 static inline void gen_op_arith_neg(DisasContext *ctx, bool compute_ov)
2209 TCGv zero = tcg_const_tl(0);
2210 gen_op_arith_subf(ctx, cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)],
2211 zero, 0, 0, compute_ov, Rc(ctx->opcode));
2212 tcg_temp_free(zero);
2215 static void gen_neg(DisasContext *ctx)
2217 tcg_gen_neg_tl(cpu_gpr[rD(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
2218 if (unlikely(Rc(ctx->opcode))) {
2219 gen_set_Rc0(ctx, cpu_gpr[rD(ctx->opcode)]);
2223 static void gen_nego(DisasContext *ctx)
2225 gen_op_arith_neg(ctx, 1);
2228 /*** Integer logical ***/
2229 #define GEN_LOGICAL2(name, tcg_op, opc, type) \
2230 static void glue(gen_, name)(DisasContext *ctx) \
2232 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], \
2233 cpu_gpr[rB(ctx->opcode)]); \
2234 if (unlikely(Rc(ctx->opcode) != 0)) \
2235 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
2238 #define GEN_LOGICAL1(name, tcg_op, opc, type) \
2239 static void glue(gen_, name)(DisasContext *ctx) \
2241 tcg_op(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]); \
2242 if (unlikely(Rc(ctx->opcode) != 0)) \
2243 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]); \
2246 /* and & and. */
2247 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER);
2248 /* andc & andc. */
2249 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER);
2251 /* andi. */
2252 static void gen_andi_(DisasContext *ctx)
2254 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
2255 UIMM(ctx->opcode));
2256 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2259 /* andis. */
2260 static void gen_andis_(DisasContext *ctx)
2262 tcg_gen_andi_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
2263 UIMM(ctx->opcode) << 16);
2264 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2267 /* cntlzw */
2268 static void gen_cntlzw(DisasContext *ctx)
2270 TCGv_i32 t = tcg_temp_new_i32();
2272 tcg_gen_trunc_tl_i32(t, cpu_gpr[rS(ctx->opcode)]);
2273 tcg_gen_clzi_i32(t, t, 32);
2274 tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t);
2275 tcg_temp_free_i32(t);
2277 if (unlikely(Rc(ctx->opcode) != 0)) {
2278 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2282 /* cnttzw */
2283 static void gen_cnttzw(DisasContext *ctx)
2285 TCGv_i32 t = tcg_temp_new_i32();
2287 tcg_gen_trunc_tl_i32(t, cpu_gpr[rS(ctx->opcode)]);
2288 tcg_gen_ctzi_i32(t, t, 32);
2289 tcg_gen_extu_i32_tl(cpu_gpr[rA(ctx->opcode)], t);
2290 tcg_temp_free_i32(t);
2292 if (unlikely(Rc(ctx->opcode) != 0)) {
2293 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2297 /* eqv & eqv. */
2298 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER);
2299 /* extsb & extsb. */
2300 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER);
2301 /* extsh & extsh. */
2302 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER);
2303 /* nand & nand. */
2304 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER);
2305 /* nor & nor. */
2306 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER);
2308 #if defined(TARGET_PPC64) && !defined(CONFIG_USER_ONLY)
2309 static void gen_pause(DisasContext *ctx)
2311 TCGv_i32 t0 = tcg_const_i32(0);
2312 tcg_gen_st_i32(t0, cpu_env,
2313 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
2314 tcg_temp_free_i32(t0);
2316 /* Stop translation, this gives other CPUs a chance to run */
2317 gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
2319 #endif /* defined(TARGET_PPC64) */
2321 /* or & or. */
2322 static void gen_or(DisasContext *ctx)
2324 int rs, ra, rb;
2326 rs = rS(ctx->opcode);
2327 ra = rA(ctx->opcode);
2328 rb = rB(ctx->opcode);
2329 /* Optimisation for mr. ri case */
2330 if (rs != ra || rs != rb) {
2331 if (rs != rb) {
2332 tcg_gen_or_tl(cpu_gpr[ra], cpu_gpr[rs], cpu_gpr[rb]);
2333 } else {
2334 tcg_gen_mov_tl(cpu_gpr[ra], cpu_gpr[rs]);
2336 if (unlikely(Rc(ctx->opcode) != 0)) {
2337 gen_set_Rc0(ctx, cpu_gpr[ra]);
2339 } else if (unlikely(Rc(ctx->opcode) != 0)) {
2340 gen_set_Rc0(ctx, cpu_gpr[rs]);
2341 #if defined(TARGET_PPC64)
2342 } else if (rs != 0) { /* 0 is nop */
2343 int prio = 0;
2345 switch (rs) {
2346 case 1:
2347 /* Set process priority to low */
2348 prio = 2;
2349 break;
2350 case 6:
2351 /* Set process priority to medium-low */
2352 prio = 3;
2353 break;
2354 case 2:
2355 /* Set process priority to normal */
2356 prio = 4;
2357 break;
2358 #if !defined(CONFIG_USER_ONLY)
2359 case 31:
2360 if (!ctx->pr) {
2361 /* Set process priority to very low */
2362 prio = 1;
2364 break;
2365 case 5:
2366 if (!ctx->pr) {
2367 /* Set process priority to medium-hight */
2368 prio = 5;
2370 break;
2371 case 3:
2372 if (!ctx->pr) {
2373 /* Set process priority to high */
2374 prio = 6;
2376 break;
2377 case 7:
2378 if (ctx->hv && !ctx->pr) {
2379 /* Set process priority to very high */
2380 prio = 7;
2382 break;
2383 #endif
2384 default:
2385 break;
2387 if (prio) {
2388 TCGv t0 = tcg_temp_new();
2389 gen_load_spr(t0, SPR_PPR);
2390 tcg_gen_andi_tl(t0, t0, ~0x001C000000000000ULL);
2391 tcg_gen_ori_tl(t0, t0, ((uint64_t)prio) << 50);
2392 gen_store_spr(SPR_PPR, t0);
2393 tcg_temp_free(t0);
2395 #if !defined(CONFIG_USER_ONLY)
2397 * Pause out of TCG otherwise spin loops with smt_low eat too
2398 * much CPU and the kernel hangs. This applies to all
2399 * encodings other than no-op, e.g., miso(rs=26), yield(27),
2400 * mdoio(29), mdoom(30), and all currently undefined.
2402 gen_pause(ctx);
2403 #endif
2404 #endif
2407 /* orc & orc. */
2408 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER);
2410 /* xor & xor. */
2411 static void gen_xor(DisasContext *ctx)
2413 /* Optimisation for "set to zero" case */
2414 if (rS(ctx->opcode) != rB(ctx->opcode)) {
2415 tcg_gen_xor_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
2416 cpu_gpr[rB(ctx->opcode)]);
2417 } else {
2418 tcg_gen_movi_tl(cpu_gpr[rA(ctx->opcode)], 0);
2420 if (unlikely(Rc(ctx->opcode) != 0)) {
2421 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2425 /* ori */
2426 static void gen_ori(DisasContext *ctx)
2428 target_ulong uimm = UIMM(ctx->opcode);
2430 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
2431 return;
2433 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
2436 /* oris */
2437 static void gen_oris(DisasContext *ctx)
2439 target_ulong uimm = UIMM(ctx->opcode);
2441 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
2442 /* NOP */
2443 return;
2445 tcg_gen_ori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
2446 uimm << 16);
2449 /* xori */
2450 static void gen_xori(DisasContext *ctx)
2452 target_ulong uimm = UIMM(ctx->opcode);
2454 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
2455 /* NOP */
2456 return;
2458 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], uimm);
2461 /* xoris */
2462 static void gen_xoris(DisasContext *ctx)
2464 target_ulong uimm = UIMM(ctx->opcode);
2466 if (rS(ctx->opcode) == rA(ctx->opcode) && uimm == 0) {
2467 /* NOP */
2468 return;
2470 tcg_gen_xori_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)],
2471 uimm << 16);
2474 /* popcntb : PowerPC 2.03 specification */
2475 static void gen_popcntb(DisasContext *ctx)
2477 gen_helper_popcntb(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2480 static void gen_popcntw(DisasContext *ctx)
2482 #if defined(TARGET_PPC64)
2483 gen_helper_popcntw(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2484 #else
2485 tcg_gen_ctpop_i32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2486 #endif
2489 #if defined(TARGET_PPC64)
2490 /* popcntd: PowerPC 2.06 specification */
2491 static void gen_popcntd(DisasContext *ctx)
2493 tcg_gen_ctpop_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
2495 #endif
2497 /* prtyw: PowerPC 2.05 specification */
2498 static void gen_prtyw(DisasContext *ctx)
2500 TCGv ra = cpu_gpr[rA(ctx->opcode)];
2501 TCGv rs = cpu_gpr[rS(ctx->opcode)];
2502 TCGv t0 = tcg_temp_new();
2503 tcg_gen_shri_tl(t0, rs, 16);
2504 tcg_gen_xor_tl(ra, rs, t0);
2505 tcg_gen_shri_tl(t0, ra, 8);
2506 tcg_gen_xor_tl(ra, ra, t0);
2507 tcg_gen_andi_tl(ra, ra, (target_ulong)0x100000001ULL);
2508 tcg_temp_free(t0);
2511 #if defined(TARGET_PPC64)
2512 /* prtyd: PowerPC 2.05 specification */
2513 static void gen_prtyd(DisasContext *ctx)
2515 TCGv ra = cpu_gpr[rA(ctx->opcode)];
2516 TCGv rs = cpu_gpr[rS(ctx->opcode)];
2517 TCGv t0 = tcg_temp_new();
2518 tcg_gen_shri_tl(t0, rs, 32);
2519 tcg_gen_xor_tl(ra, rs, t0);
2520 tcg_gen_shri_tl(t0, ra, 16);
2521 tcg_gen_xor_tl(ra, ra, t0);
2522 tcg_gen_shri_tl(t0, ra, 8);
2523 tcg_gen_xor_tl(ra, ra, t0);
2524 tcg_gen_andi_tl(ra, ra, 1);
2525 tcg_temp_free(t0);
2527 #endif
2529 #if defined(TARGET_PPC64)
2530 /* bpermd */
2531 static void gen_bpermd(DisasContext *ctx)
2533 gen_helper_bpermd(cpu_gpr[rA(ctx->opcode)],
2534 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2536 #endif
2538 #if defined(TARGET_PPC64)
2539 /* extsw & extsw. */
2540 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B);
2542 /* cntlzd */
2543 static void gen_cntlzd(DisasContext *ctx)
2545 tcg_gen_clzi_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], 64);
2546 if (unlikely(Rc(ctx->opcode) != 0)) {
2547 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2551 /* cnttzd */
2552 static void gen_cnttzd(DisasContext *ctx)
2554 tcg_gen_ctzi_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)], 64);
2555 if (unlikely(Rc(ctx->opcode) != 0)) {
2556 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2560 /* darn */
2561 static void gen_darn(DisasContext *ctx)
2563 int l = L(ctx->opcode);
2565 if (l > 2) {
2566 tcg_gen_movi_i64(cpu_gpr[rD(ctx->opcode)], -1);
2567 } else {
2568 gen_icount_io_start(ctx);
2569 if (l == 0) {
2570 gen_helper_darn32(cpu_gpr[rD(ctx->opcode)]);
2571 } else {
2572 /* Return 64-bit random for both CRN and RRN */
2573 gen_helper_darn64(cpu_gpr[rD(ctx->opcode)]);
2577 #endif
2579 /*** Integer rotate ***/
2581 /* rlwimi & rlwimi. */
2582 static void gen_rlwimi(DisasContext *ctx)
2584 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2585 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2586 uint32_t sh = SH(ctx->opcode);
2587 uint32_t mb = MB(ctx->opcode);
2588 uint32_t me = ME(ctx->opcode);
2590 if (sh == (31 - me) && mb <= me) {
2591 tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
2592 } else {
2593 target_ulong mask;
2594 bool mask_in_32b = true;
2595 TCGv t1;
2597 #if defined(TARGET_PPC64)
2598 mb += 32;
2599 me += 32;
2600 #endif
2601 mask = MASK(mb, me);
2603 #if defined(TARGET_PPC64)
2604 if (mask > 0xffffffffu) {
2605 mask_in_32b = false;
2607 #endif
2608 t1 = tcg_temp_new();
2609 if (mask_in_32b) {
2610 TCGv_i32 t0 = tcg_temp_new_i32();
2611 tcg_gen_trunc_tl_i32(t0, t_rs);
2612 tcg_gen_rotli_i32(t0, t0, sh);
2613 tcg_gen_extu_i32_tl(t1, t0);
2614 tcg_temp_free_i32(t0);
2615 } else {
2616 #if defined(TARGET_PPC64)
2617 tcg_gen_deposit_i64(t1, t_rs, t_rs, 32, 32);
2618 tcg_gen_rotli_i64(t1, t1, sh);
2619 #else
2620 g_assert_not_reached();
2621 #endif
2624 tcg_gen_andi_tl(t1, t1, mask);
2625 tcg_gen_andi_tl(t_ra, t_ra, ~mask);
2626 tcg_gen_or_tl(t_ra, t_ra, t1);
2627 tcg_temp_free(t1);
2629 if (unlikely(Rc(ctx->opcode) != 0)) {
2630 gen_set_Rc0(ctx, t_ra);
2634 /* rlwinm & rlwinm. */
2635 static void gen_rlwinm(DisasContext *ctx)
2637 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2638 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2639 int sh = SH(ctx->opcode);
2640 int mb = MB(ctx->opcode);
2641 int me = ME(ctx->opcode);
2642 int len = me - mb + 1;
2643 int rsh = (32 - sh) & 31;
2645 if (sh != 0 && len > 0 && me == (31 - sh)) {
2646 tcg_gen_deposit_z_tl(t_ra, t_rs, sh, len);
2647 } else if (me == 31 && rsh + len <= 32) {
2648 tcg_gen_extract_tl(t_ra, t_rs, rsh, len);
2649 } else {
2650 target_ulong mask;
2651 bool mask_in_32b = true;
2652 #if defined(TARGET_PPC64)
2653 mb += 32;
2654 me += 32;
2655 #endif
2656 mask = MASK(mb, me);
2657 #if defined(TARGET_PPC64)
2658 if (mask > 0xffffffffu) {
2659 mask_in_32b = false;
2661 #endif
2662 if (mask_in_32b) {
2663 if (sh == 0) {
2664 tcg_gen_andi_tl(t_ra, t_rs, mask);
2665 } else {
2666 TCGv_i32 t0 = tcg_temp_new_i32();
2667 tcg_gen_trunc_tl_i32(t0, t_rs);
2668 tcg_gen_rotli_i32(t0, t0, sh);
2669 tcg_gen_andi_i32(t0, t0, mask);
2670 tcg_gen_extu_i32_tl(t_ra, t0);
2671 tcg_temp_free_i32(t0);
2673 } else {
2674 #if defined(TARGET_PPC64)
2675 tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
2676 tcg_gen_rotli_i64(t_ra, t_ra, sh);
2677 tcg_gen_andi_i64(t_ra, t_ra, mask);
2678 #else
2679 g_assert_not_reached();
2680 #endif
2683 if (unlikely(Rc(ctx->opcode) != 0)) {
2684 gen_set_Rc0(ctx, t_ra);
2688 /* rlwnm & rlwnm. */
2689 static void gen_rlwnm(DisasContext *ctx)
2691 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2692 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2693 TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
2694 uint32_t mb = MB(ctx->opcode);
2695 uint32_t me = ME(ctx->opcode);
2696 target_ulong mask;
2697 bool mask_in_32b = true;
2699 #if defined(TARGET_PPC64)
2700 mb += 32;
2701 me += 32;
2702 #endif
2703 mask = MASK(mb, me);
2705 #if defined(TARGET_PPC64)
2706 if (mask > 0xffffffffu) {
2707 mask_in_32b = false;
2709 #endif
2710 if (mask_in_32b) {
2711 TCGv_i32 t0 = tcg_temp_new_i32();
2712 TCGv_i32 t1 = tcg_temp_new_i32();
2713 tcg_gen_trunc_tl_i32(t0, t_rb);
2714 tcg_gen_trunc_tl_i32(t1, t_rs);
2715 tcg_gen_andi_i32(t0, t0, 0x1f);
2716 tcg_gen_rotl_i32(t1, t1, t0);
2717 tcg_gen_extu_i32_tl(t_ra, t1);
2718 tcg_temp_free_i32(t0);
2719 tcg_temp_free_i32(t1);
2720 } else {
2721 #if defined(TARGET_PPC64)
2722 TCGv_i64 t0 = tcg_temp_new_i64();
2723 tcg_gen_andi_i64(t0, t_rb, 0x1f);
2724 tcg_gen_deposit_i64(t_ra, t_rs, t_rs, 32, 32);
2725 tcg_gen_rotl_i64(t_ra, t_ra, t0);
2726 tcg_temp_free_i64(t0);
2727 #else
2728 g_assert_not_reached();
2729 #endif
2732 tcg_gen_andi_tl(t_ra, t_ra, mask);
2734 if (unlikely(Rc(ctx->opcode) != 0)) {
2735 gen_set_Rc0(ctx, t_ra);
2739 #if defined(TARGET_PPC64)
2740 #define GEN_PPC64_R2(name, opc1, opc2) \
2741 static void glue(gen_, name##0)(DisasContext *ctx) \
2743 gen_##name(ctx, 0); \
2746 static void glue(gen_, name##1)(DisasContext *ctx) \
2748 gen_##name(ctx, 1); \
2750 #define GEN_PPC64_R4(name, opc1, opc2) \
2751 static void glue(gen_, name##0)(DisasContext *ctx) \
2753 gen_##name(ctx, 0, 0); \
2756 static void glue(gen_, name##1)(DisasContext *ctx) \
2758 gen_##name(ctx, 0, 1); \
2761 static void glue(gen_, name##2)(DisasContext *ctx) \
2763 gen_##name(ctx, 1, 0); \
2766 static void glue(gen_, name##3)(DisasContext *ctx) \
2768 gen_##name(ctx, 1, 1); \
2771 static void gen_rldinm(DisasContext *ctx, int mb, int me, int sh)
2773 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2774 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2775 int len = me - mb + 1;
2776 int rsh = (64 - sh) & 63;
2778 if (sh != 0 && len > 0 && me == (63 - sh)) {
2779 tcg_gen_deposit_z_tl(t_ra, t_rs, sh, len);
2780 } else if (me == 63 && rsh + len <= 64) {
2781 tcg_gen_extract_tl(t_ra, t_rs, rsh, len);
2782 } else {
2783 tcg_gen_rotli_tl(t_ra, t_rs, sh);
2784 tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2786 if (unlikely(Rc(ctx->opcode) != 0)) {
2787 gen_set_Rc0(ctx, t_ra);
2791 /* rldicl - rldicl. */
2792 static inline void gen_rldicl(DisasContext *ctx, int mbn, int shn)
2794 uint32_t sh, mb;
2796 sh = SH(ctx->opcode) | (shn << 5);
2797 mb = MB(ctx->opcode) | (mbn << 5);
2798 gen_rldinm(ctx, mb, 63, sh);
2800 GEN_PPC64_R4(rldicl, 0x1E, 0x00);
2802 /* rldicr - rldicr. */
2803 static inline void gen_rldicr(DisasContext *ctx, int men, int shn)
2805 uint32_t sh, me;
2807 sh = SH(ctx->opcode) | (shn << 5);
2808 me = MB(ctx->opcode) | (men << 5);
2809 gen_rldinm(ctx, 0, me, sh);
2811 GEN_PPC64_R4(rldicr, 0x1E, 0x02);
2813 /* rldic - rldic. */
2814 static inline void gen_rldic(DisasContext *ctx, int mbn, int shn)
2816 uint32_t sh, mb;
2818 sh = SH(ctx->opcode) | (shn << 5);
2819 mb = MB(ctx->opcode) | (mbn << 5);
2820 gen_rldinm(ctx, mb, 63 - sh, sh);
2822 GEN_PPC64_R4(rldic, 0x1E, 0x04);
2824 static void gen_rldnm(DisasContext *ctx, int mb, int me)
2826 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2827 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2828 TCGv t_rb = cpu_gpr[rB(ctx->opcode)];
2829 TCGv t0;
2831 t0 = tcg_temp_new();
2832 tcg_gen_andi_tl(t0, t_rb, 0x3f);
2833 tcg_gen_rotl_tl(t_ra, t_rs, t0);
2834 tcg_temp_free(t0);
2836 tcg_gen_andi_tl(t_ra, t_ra, MASK(mb, me));
2837 if (unlikely(Rc(ctx->opcode) != 0)) {
2838 gen_set_Rc0(ctx, t_ra);
2842 /* rldcl - rldcl. */
2843 static inline void gen_rldcl(DisasContext *ctx, int mbn)
2845 uint32_t mb;
2847 mb = MB(ctx->opcode) | (mbn << 5);
2848 gen_rldnm(ctx, mb, 63);
2850 GEN_PPC64_R2(rldcl, 0x1E, 0x08);
2852 /* rldcr - rldcr. */
2853 static inline void gen_rldcr(DisasContext *ctx, int men)
2855 uint32_t me;
2857 me = MB(ctx->opcode) | (men << 5);
2858 gen_rldnm(ctx, 0, me);
2860 GEN_PPC64_R2(rldcr, 0x1E, 0x09);
2862 /* rldimi - rldimi. */
2863 static void gen_rldimi(DisasContext *ctx, int mbn, int shn)
2865 TCGv t_ra = cpu_gpr[rA(ctx->opcode)];
2866 TCGv t_rs = cpu_gpr[rS(ctx->opcode)];
2867 uint32_t sh = SH(ctx->opcode) | (shn << 5);
2868 uint32_t mb = MB(ctx->opcode) | (mbn << 5);
2869 uint32_t me = 63 - sh;
2871 if (mb <= me) {
2872 tcg_gen_deposit_tl(t_ra, t_ra, t_rs, sh, me - mb + 1);
2873 } else {
2874 target_ulong mask = MASK(mb, me);
2875 TCGv t1 = tcg_temp_new();
2877 tcg_gen_rotli_tl(t1, t_rs, sh);
2878 tcg_gen_andi_tl(t1, t1, mask);
2879 tcg_gen_andi_tl(t_ra, t_ra, ~mask);
2880 tcg_gen_or_tl(t_ra, t_ra, t1);
2881 tcg_temp_free(t1);
2883 if (unlikely(Rc(ctx->opcode) != 0)) {
2884 gen_set_Rc0(ctx, t_ra);
2887 GEN_PPC64_R4(rldimi, 0x1E, 0x06);
2888 #endif
2890 /*** Integer shift ***/
2892 /* slw & slw. */
2893 static void gen_slw(DisasContext *ctx)
2895 TCGv t0, t1;
2897 t0 = tcg_temp_new();
2898 /* AND rS with a mask that is 0 when rB >= 0x20 */
2899 #if defined(TARGET_PPC64)
2900 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2901 tcg_gen_sari_tl(t0, t0, 0x3f);
2902 #else
2903 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2904 tcg_gen_sari_tl(t0, t0, 0x1f);
2905 #endif
2906 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2907 t1 = tcg_temp_new();
2908 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2909 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2910 tcg_temp_free(t1);
2911 tcg_temp_free(t0);
2912 tcg_gen_ext32u_tl(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)]);
2913 if (unlikely(Rc(ctx->opcode) != 0)) {
2914 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2918 /* sraw & sraw. */
2919 static void gen_sraw(DisasContext *ctx)
2921 gen_helper_sraw(cpu_gpr[rA(ctx->opcode)], cpu_env,
2922 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
2923 if (unlikely(Rc(ctx->opcode) != 0)) {
2924 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2928 /* srawi & srawi. */
2929 static void gen_srawi(DisasContext *ctx)
2931 int sh = SH(ctx->opcode);
2932 TCGv dst = cpu_gpr[rA(ctx->opcode)];
2933 TCGv src = cpu_gpr[rS(ctx->opcode)];
2934 if (sh == 0) {
2935 tcg_gen_ext32s_tl(dst, src);
2936 tcg_gen_movi_tl(cpu_ca, 0);
2937 if (is_isa300(ctx)) {
2938 tcg_gen_movi_tl(cpu_ca32, 0);
2940 } else {
2941 TCGv t0;
2942 tcg_gen_ext32s_tl(dst, src);
2943 tcg_gen_andi_tl(cpu_ca, dst, (1ULL << sh) - 1);
2944 t0 = tcg_temp_new();
2945 tcg_gen_sari_tl(t0, dst, TARGET_LONG_BITS - 1);
2946 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
2947 tcg_temp_free(t0);
2948 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
2949 if (is_isa300(ctx)) {
2950 tcg_gen_mov_tl(cpu_ca32, cpu_ca);
2952 tcg_gen_sari_tl(dst, dst, sh);
2954 if (unlikely(Rc(ctx->opcode) != 0)) {
2955 gen_set_Rc0(ctx, dst);
2959 /* srw & srw. */
2960 static void gen_srw(DisasContext *ctx)
2962 TCGv t0, t1;
2964 t0 = tcg_temp_new();
2965 /* AND rS with a mask that is 0 when rB >= 0x20 */
2966 #if defined(TARGET_PPC64)
2967 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3a);
2968 tcg_gen_sari_tl(t0, t0, 0x3f);
2969 #else
2970 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x1a);
2971 tcg_gen_sari_tl(t0, t0, 0x1f);
2972 #endif
2973 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2974 tcg_gen_ext32u_tl(t0, t0);
2975 t1 = tcg_temp_new();
2976 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x1f);
2977 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2978 tcg_temp_free(t1);
2979 tcg_temp_free(t0);
2980 if (unlikely(Rc(ctx->opcode) != 0)) {
2981 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
2985 #if defined(TARGET_PPC64)
2986 /* sld & sld. */
2987 static void gen_sld(DisasContext *ctx)
2989 TCGv t0, t1;
2991 t0 = tcg_temp_new();
2992 /* AND rS with a mask that is 0 when rB >= 0x40 */
2993 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
2994 tcg_gen_sari_tl(t0, t0, 0x3f);
2995 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
2996 t1 = tcg_temp_new();
2997 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
2998 tcg_gen_shl_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
2999 tcg_temp_free(t1);
3000 tcg_temp_free(t0);
3001 if (unlikely(Rc(ctx->opcode) != 0)) {
3002 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
3006 /* srad & srad. */
3007 static void gen_srad(DisasContext *ctx)
3009 gen_helper_srad(cpu_gpr[rA(ctx->opcode)], cpu_env,
3010 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
3011 if (unlikely(Rc(ctx->opcode) != 0)) {
3012 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
3015 /* sradi & sradi. */
3016 static inline void gen_sradi(DisasContext *ctx, int n)
3018 int sh = SH(ctx->opcode) + (n << 5);
3019 TCGv dst = cpu_gpr[rA(ctx->opcode)];
3020 TCGv src = cpu_gpr[rS(ctx->opcode)];
3021 if (sh == 0) {
3022 tcg_gen_mov_tl(dst, src);
3023 tcg_gen_movi_tl(cpu_ca, 0);
3024 if (is_isa300(ctx)) {
3025 tcg_gen_movi_tl(cpu_ca32, 0);
3027 } else {
3028 TCGv t0;
3029 tcg_gen_andi_tl(cpu_ca, src, (1ULL << sh) - 1);
3030 t0 = tcg_temp_new();
3031 tcg_gen_sari_tl(t0, src, TARGET_LONG_BITS - 1);
3032 tcg_gen_and_tl(cpu_ca, cpu_ca, t0);
3033 tcg_temp_free(t0);
3034 tcg_gen_setcondi_tl(TCG_COND_NE, cpu_ca, cpu_ca, 0);
3035 if (is_isa300(ctx)) {
3036 tcg_gen_mov_tl(cpu_ca32, cpu_ca);
3038 tcg_gen_sari_tl(dst, src, sh);
3040 if (unlikely(Rc(ctx->opcode) != 0)) {
3041 gen_set_Rc0(ctx, dst);
3045 static void gen_sradi0(DisasContext *ctx)
3047 gen_sradi(ctx, 0);
3050 static void gen_sradi1(DisasContext *ctx)
3052 gen_sradi(ctx, 1);
3055 /* extswsli & extswsli. */
3056 static inline void gen_extswsli(DisasContext *ctx, int n)
3058 int sh = SH(ctx->opcode) + (n << 5);
3059 TCGv dst = cpu_gpr[rA(ctx->opcode)];
3060 TCGv src = cpu_gpr[rS(ctx->opcode)];
3062 tcg_gen_ext32s_tl(dst, src);
3063 tcg_gen_shli_tl(dst, dst, sh);
3064 if (unlikely(Rc(ctx->opcode) != 0)) {
3065 gen_set_Rc0(ctx, dst);
3069 static void gen_extswsli0(DisasContext *ctx)
3071 gen_extswsli(ctx, 0);
3074 static void gen_extswsli1(DisasContext *ctx)
3076 gen_extswsli(ctx, 1);
3079 /* srd & srd. */
3080 static void gen_srd(DisasContext *ctx)
3082 TCGv t0, t1;
3084 t0 = tcg_temp_new();
3085 /* AND rS with a mask that is 0 when rB >= 0x40 */
3086 tcg_gen_shli_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x39);
3087 tcg_gen_sari_tl(t0, t0, 0x3f);
3088 tcg_gen_andc_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
3089 t1 = tcg_temp_new();
3090 tcg_gen_andi_tl(t1, cpu_gpr[rB(ctx->opcode)], 0x3f);
3091 tcg_gen_shr_tl(cpu_gpr[rA(ctx->opcode)], t0, t1);
3092 tcg_temp_free(t1);
3093 tcg_temp_free(t0);
3094 if (unlikely(Rc(ctx->opcode) != 0)) {
3095 gen_set_Rc0(ctx, cpu_gpr[rA(ctx->opcode)]);
3098 #endif
3100 /*** Addressing modes ***/
3101 /* Register indirect with immediate index : EA = (rA|0) + SIMM */
3102 static inline void gen_addr_imm_index(DisasContext *ctx, TCGv EA,
3103 target_long maskl)
3105 target_long simm = SIMM(ctx->opcode);
3107 simm &= ~maskl;
3108 if (rA(ctx->opcode) == 0) {
3109 if (NARROW_MODE(ctx)) {
3110 simm = (uint32_t)simm;
3112 tcg_gen_movi_tl(EA, simm);
3113 } else if (likely(simm != 0)) {
3114 tcg_gen_addi_tl(EA, cpu_gpr[rA(ctx->opcode)], simm);
3115 if (NARROW_MODE(ctx)) {
3116 tcg_gen_ext32u_tl(EA, EA);
3118 } else {
3119 if (NARROW_MODE(ctx)) {
3120 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
3121 } else {
3122 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
3127 static inline void gen_addr_reg_index(DisasContext *ctx, TCGv EA)
3129 if (rA(ctx->opcode) == 0) {
3130 if (NARROW_MODE(ctx)) {
3131 tcg_gen_ext32u_tl(EA, cpu_gpr[rB(ctx->opcode)]);
3132 } else {
3133 tcg_gen_mov_tl(EA, cpu_gpr[rB(ctx->opcode)]);
3135 } else {
3136 tcg_gen_add_tl(EA, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
3137 if (NARROW_MODE(ctx)) {
3138 tcg_gen_ext32u_tl(EA, EA);
3143 static inline void gen_addr_register(DisasContext *ctx, TCGv EA)
3145 if (rA(ctx->opcode) == 0) {
3146 tcg_gen_movi_tl(EA, 0);
3147 } else if (NARROW_MODE(ctx)) {
3148 tcg_gen_ext32u_tl(EA, cpu_gpr[rA(ctx->opcode)]);
3149 } else {
3150 tcg_gen_mov_tl(EA, cpu_gpr[rA(ctx->opcode)]);
3154 static inline void gen_addr_add(DisasContext *ctx, TCGv ret, TCGv arg1,
3155 target_long val)
3157 tcg_gen_addi_tl(ret, arg1, val);
3158 if (NARROW_MODE(ctx)) {
3159 tcg_gen_ext32u_tl(ret, ret);
3163 static inline void gen_align_no_le(DisasContext *ctx)
3165 gen_exception_err(ctx, POWERPC_EXCP_ALIGN,
3166 (ctx->opcode & 0x03FF0000) | POWERPC_EXCP_ALIGN_LE);
3169 static TCGv do_ea_calc(DisasContext *ctx, int ra, TCGv displ)
3171 TCGv ea = tcg_temp_new();
3172 if (ra) {
3173 tcg_gen_add_tl(ea, cpu_gpr[ra], displ);
3174 } else {
3175 tcg_gen_mov_tl(ea, displ);
3177 if (NARROW_MODE(ctx)) {
3178 tcg_gen_ext32u_tl(ea, ea);
3180 return ea;
3183 /*** Integer load ***/
3184 #define DEF_MEMOP(op) ((op) | ctx->default_tcg_memop_mask)
3185 #define BSWAP_MEMOP(op) ((op) | (ctx->default_tcg_memop_mask ^ MO_BSWAP))
3187 #define GEN_QEMU_LOAD_TL(ldop, op) \
3188 static void glue(gen_qemu_, ldop)(DisasContext *ctx, \
3189 TCGv val, \
3190 TCGv addr) \
3192 tcg_gen_qemu_ld_tl(val, addr, ctx->mem_idx, op); \
3195 GEN_QEMU_LOAD_TL(ld8u, DEF_MEMOP(MO_UB))
3196 GEN_QEMU_LOAD_TL(ld16u, DEF_MEMOP(MO_UW))
3197 GEN_QEMU_LOAD_TL(ld16s, DEF_MEMOP(MO_SW))
3198 GEN_QEMU_LOAD_TL(ld32u, DEF_MEMOP(MO_UL))
3199 GEN_QEMU_LOAD_TL(ld32s, DEF_MEMOP(MO_SL))
3201 GEN_QEMU_LOAD_TL(ld16ur, BSWAP_MEMOP(MO_UW))
3202 GEN_QEMU_LOAD_TL(ld32ur, BSWAP_MEMOP(MO_UL))
3204 #define GEN_QEMU_LOAD_64(ldop, op) \
3205 static void glue(gen_qemu_, glue(ldop, _i64))(DisasContext *ctx, \
3206 TCGv_i64 val, \
3207 TCGv addr) \
3209 tcg_gen_qemu_ld_i64(val, addr, ctx->mem_idx, op); \
3212 GEN_QEMU_LOAD_64(ld8u, DEF_MEMOP(MO_UB))
3213 GEN_QEMU_LOAD_64(ld16u, DEF_MEMOP(MO_UW))
3214 GEN_QEMU_LOAD_64(ld32u, DEF_MEMOP(MO_UL))
3215 GEN_QEMU_LOAD_64(ld32s, DEF_MEMOP(MO_SL))
3216 GEN_QEMU_LOAD_64(ld64, DEF_MEMOP(MO_UQ))
3218 #if defined(TARGET_PPC64)
3219 GEN_QEMU_LOAD_64(ld64ur, BSWAP_MEMOP(MO_UQ))
3220 #endif
3222 #define GEN_QEMU_STORE_TL(stop, op) \
3223 static void glue(gen_qemu_, stop)(DisasContext *ctx, \
3224 TCGv val, \
3225 TCGv addr) \
3227 tcg_gen_qemu_st_tl(val, addr, ctx->mem_idx, op); \
3230 #if defined(TARGET_PPC64) || !defined(CONFIG_USER_ONLY)
3231 GEN_QEMU_STORE_TL(st8, DEF_MEMOP(MO_UB))
3232 #endif
3233 GEN_QEMU_STORE_TL(st16, DEF_MEMOP(MO_UW))
3234 GEN_QEMU_STORE_TL(st32, DEF_MEMOP(MO_UL))
3236 GEN_QEMU_STORE_TL(st16r, BSWAP_MEMOP(MO_UW))
3237 GEN_QEMU_STORE_TL(st32r, BSWAP_MEMOP(MO_UL))
3239 #define GEN_QEMU_STORE_64(stop, op) \
3240 static void glue(gen_qemu_, glue(stop, _i64))(DisasContext *ctx, \
3241 TCGv_i64 val, \
3242 TCGv addr) \
3244 tcg_gen_qemu_st_i64(val, addr, ctx->mem_idx, op); \
3247 GEN_QEMU_STORE_64(st8, DEF_MEMOP(MO_UB))
3248 GEN_QEMU_STORE_64(st16, DEF_MEMOP(MO_UW))
3249 GEN_QEMU_STORE_64(st32, DEF_MEMOP(MO_UL))
3250 GEN_QEMU_STORE_64(st64, DEF_MEMOP(MO_UQ))
3252 #if defined(TARGET_PPC64)
3253 GEN_QEMU_STORE_64(st64r, BSWAP_MEMOP(MO_UQ))
3254 #endif
3256 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk) \
3257 static void glue(gen_, name##x)(DisasContext *ctx) \
3259 TCGv EA; \
3260 chk(ctx); \
3261 gen_set_access_type(ctx, ACCESS_INT); \
3262 EA = tcg_temp_new(); \
3263 gen_addr_reg_index(ctx, EA); \
3264 gen_qemu_##ldop(ctx, cpu_gpr[rD(ctx->opcode)], EA); \
3265 tcg_temp_free(EA); \
3268 #define GEN_LDX(name, ldop, opc2, opc3, type) \
3269 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_NONE)
3271 #define GEN_LDX_HVRM(name, ldop, opc2, opc3, type) \
3272 GEN_LDX_E(name, ldop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
3274 #define GEN_LDEPX(name, ldop, opc2, opc3) \
3275 static void glue(gen_, name##epx)(DisasContext *ctx) \
3277 TCGv EA; \
3278 CHK_SV(ctx); \
3279 gen_set_access_type(ctx, ACCESS_INT); \
3280 EA = tcg_temp_new(); \
3281 gen_addr_reg_index(ctx, EA); \
3282 tcg_gen_qemu_ld_tl(cpu_gpr[rD(ctx->opcode)], EA, PPC_TLB_EPID_LOAD, ldop);\
3283 tcg_temp_free(EA); \
3286 GEN_LDEPX(lb, DEF_MEMOP(MO_UB), 0x1F, 0x02)
3287 GEN_LDEPX(lh, DEF_MEMOP(MO_UW), 0x1F, 0x08)
3288 GEN_LDEPX(lw, DEF_MEMOP(MO_UL), 0x1F, 0x00)
3289 #if defined(TARGET_PPC64)
3290 GEN_LDEPX(ld, DEF_MEMOP(MO_UQ), 0x1D, 0x00)
3291 #endif
3293 #if defined(TARGET_PPC64)
3294 /* CI load/store variants */
3295 GEN_LDX_HVRM(ldcix, ld64_i64, 0x15, 0x1b, PPC_CILDST)
3296 GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x15, PPC_CILDST)
3297 GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
3298 GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
3299 #endif
3301 /*** Integer store ***/
3302 #define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk) \
3303 static void glue(gen_, name##x)(DisasContext *ctx) \
3305 TCGv EA; \
3306 chk(ctx); \
3307 gen_set_access_type(ctx, ACCESS_INT); \
3308 EA = tcg_temp_new(); \
3309 gen_addr_reg_index(ctx, EA); \
3310 gen_qemu_##stop(ctx, cpu_gpr[rS(ctx->opcode)], EA); \
3311 tcg_temp_free(EA); \
3313 #define GEN_STX(name, stop, opc2, opc3, type) \
3314 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_NONE)
3316 #define GEN_STX_HVRM(name, stop, opc2, opc3, type) \
3317 GEN_STX_E(name, stop, opc2, opc3, type, PPC_NONE, CHK_HVRM)
3319 #define GEN_STEPX(name, stop, opc2, opc3) \
3320 static void glue(gen_, name##epx)(DisasContext *ctx) \
3322 TCGv EA; \
3323 CHK_SV(ctx); \
3324 gen_set_access_type(ctx, ACCESS_INT); \
3325 EA = tcg_temp_new(); \
3326 gen_addr_reg_index(ctx, EA); \
3327 tcg_gen_qemu_st_tl( \
3328 cpu_gpr[rD(ctx->opcode)], EA, PPC_TLB_EPID_STORE, stop); \
3329 tcg_temp_free(EA); \
3332 GEN_STEPX(stb, DEF_MEMOP(MO_UB), 0x1F, 0x06)
3333 GEN_STEPX(sth, DEF_MEMOP(MO_UW), 0x1F, 0x0C)
3334 GEN_STEPX(stw, DEF_MEMOP(MO_UL), 0x1F, 0x04)
3335 #if defined(TARGET_PPC64)
3336 GEN_STEPX(std, DEF_MEMOP(MO_UQ), 0x1d, 0x04)
3337 #endif
3339 #if defined(TARGET_PPC64)
3340 GEN_STX_HVRM(stdcix, st64_i64, 0x15, 0x1f, PPC_CILDST)
3341 GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
3342 GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
3343 GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
3344 #endif
3345 /*** Integer load and store with byte reverse ***/
3347 /* lhbrx */
3348 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER);
3350 /* lwbrx */
3351 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER);
3353 #if defined(TARGET_PPC64)
3354 /* ldbrx */
3355 GEN_LDX_E(ldbr, ld64ur_i64, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE);
3356 /* stdbrx */
3357 GEN_STX_E(stdbr, st64r_i64, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE);
3358 #endif /* TARGET_PPC64 */
3360 /* sthbrx */
3361 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER);
3362 /* stwbrx */
3363 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER);
3365 /*** Integer load and store multiple ***/
3367 /* lmw */
3368 static void gen_lmw(DisasContext *ctx)
3370 TCGv t0;
3371 TCGv_i32 t1;
3373 if (ctx->le_mode) {
3374 gen_align_no_le(ctx);
3375 return;
3377 gen_set_access_type(ctx, ACCESS_INT);
3378 t0 = tcg_temp_new();
3379 t1 = tcg_const_i32(rD(ctx->opcode));
3380 gen_addr_imm_index(ctx, t0, 0);
3381 gen_helper_lmw(cpu_env, t0, t1);
3382 tcg_temp_free(t0);
3383 tcg_temp_free_i32(t1);
3386 /* stmw */
3387 static void gen_stmw(DisasContext *ctx)
3389 TCGv t0;
3390 TCGv_i32 t1;
3392 if (ctx->le_mode) {
3393 gen_align_no_le(ctx);
3394 return;
3396 gen_set_access_type(ctx, ACCESS_INT);
3397 t0 = tcg_temp_new();
3398 t1 = tcg_const_i32(rS(ctx->opcode));
3399 gen_addr_imm_index(ctx, t0, 0);
3400 gen_helper_stmw(cpu_env, t0, t1);
3401 tcg_temp_free(t0);
3402 tcg_temp_free_i32(t1);
3405 /*** Integer load and store strings ***/
3407 /* lswi */
3409 * PowerPC32 specification says we must generate an exception if rA is
3410 * in the range of registers to be loaded. In an other hand, IBM says
3411 * this is valid, but rA won't be loaded. For now, I'll follow the
3412 * spec...
3414 static void gen_lswi(DisasContext *ctx)
3416 TCGv t0;
3417 TCGv_i32 t1, t2;
3418 int nb = NB(ctx->opcode);
3419 int start = rD(ctx->opcode);
3420 int ra = rA(ctx->opcode);
3421 int nr;
3423 if (ctx->le_mode) {
3424 gen_align_no_le(ctx);
3425 return;
3427 if (nb == 0) {
3428 nb = 32;
3430 nr = DIV_ROUND_UP(nb, 4);
3431 if (unlikely(lsw_reg_in_range(start, nr, ra))) {
3432 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_LSWX);
3433 return;
3435 gen_set_access_type(ctx, ACCESS_INT);
3436 t0 = tcg_temp_new();
3437 gen_addr_register(ctx, t0);
3438 t1 = tcg_const_i32(nb);
3439 t2 = tcg_const_i32(start);
3440 gen_helper_lsw(cpu_env, t0, t1, t2);
3441 tcg_temp_free(t0);
3442 tcg_temp_free_i32(t1);
3443 tcg_temp_free_i32(t2);
3446 /* lswx */
3447 static void gen_lswx(DisasContext *ctx)
3449 TCGv t0;
3450 TCGv_i32 t1, t2, t3;
3452 if (ctx->le_mode) {
3453 gen_align_no_le(ctx);
3454 return;
3456 gen_set_access_type(ctx, ACCESS_INT);
3457 t0 = tcg_temp_new();
3458 gen_addr_reg_index(ctx, t0);
3459 t1 = tcg_const_i32(rD(ctx->opcode));
3460 t2 = tcg_const_i32(rA(ctx->opcode));
3461 t3 = tcg_const_i32(rB(ctx->opcode));
3462 gen_helper_lswx(cpu_env, t0, t1, t2, t3);
3463 tcg_temp_free(t0);
3464 tcg_temp_free_i32(t1);
3465 tcg_temp_free_i32(t2);
3466 tcg_temp_free_i32(t3);
3469 /* stswi */
3470 static void gen_stswi(DisasContext *ctx)
3472 TCGv t0;
3473 TCGv_i32 t1, t2;
3474 int nb = NB(ctx->opcode);
3476 if (ctx->le_mode) {
3477 gen_align_no_le(ctx);
3478 return;
3480 gen_set_access_type(ctx, ACCESS_INT);
3481 t0 = tcg_temp_new();
3482 gen_addr_register(ctx, t0);
3483 if (nb == 0) {
3484 nb = 32;
3486 t1 = tcg_const_i32(nb);
3487 t2 = tcg_const_i32(rS(ctx->opcode));
3488 gen_helper_stsw(cpu_env, t0, t1, t2);
3489 tcg_temp_free(t0);
3490 tcg_temp_free_i32(t1);
3491 tcg_temp_free_i32(t2);
3494 /* stswx */
3495 static void gen_stswx(DisasContext *ctx)
3497 TCGv t0;
3498 TCGv_i32 t1, t2;
3500 if (ctx->le_mode) {
3501 gen_align_no_le(ctx);
3502 return;
3504 gen_set_access_type(ctx, ACCESS_INT);
3505 t0 = tcg_temp_new();
3506 gen_addr_reg_index(ctx, t0);
3507 t1 = tcg_temp_new_i32();
3508 tcg_gen_trunc_tl_i32(t1, cpu_xer);
3509 tcg_gen_andi_i32(t1, t1, 0x7F);
3510 t2 = tcg_const_i32(rS(ctx->opcode));
3511 gen_helper_stsw(cpu_env, t0, t1, t2);
3512 tcg_temp_free(t0);
3513 tcg_temp_free_i32(t1);
3514 tcg_temp_free_i32(t2);
3517 /*** Memory synchronisation ***/
3518 /* eieio */
3519 static void gen_eieio(DisasContext *ctx)
3521 TCGBar bar = TCG_MO_ALL;
3524 * eieio has complex semanitcs. It provides memory ordering between
3525 * operations in the set:
3526 * - loads from CI memory.
3527 * - stores to CI memory.
3528 * - stores to WT memory.
3530 * It separately also orders memory for operations in the set:
3531 * - stores to cacheble memory.
3533 * It also serializes instructions:
3534 * - dcbt and dcbst.
3536 * It separately serializes:
3537 * - tlbie and tlbsync.
3539 * And separately serializes:
3540 * - slbieg, slbiag, and slbsync.
3542 * The end result is that CI memory ordering requires TCG_MO_ALL
3543 * and it is not possible to special-case more relaxed ordering for
3544 * cacheable accesses. TCG_BAR_SC is required to provide this
3545 * serialization.
3549 * POWER9 has a eieio instruction variant using bit 6 as a hint to
3550 * tell the CPU it is a store-forwarding barrier.
3552 if (ctx->opcode & 0x2000000) {
3554 * ISA says that "Reserved fields in instructions are ignored
3555 * by the processor". So ignore the bit 6 on non-POWER9 CPU but
3556 * as this is not an instruction software should be using,
3557 * complain to the user.
3559 if (!(ctx->insns_flags2 & PPC2_ISA300)) {
3560 qemu_log_mask(LOG_GUEST_ERROR, "invalid eieio using bit 6 at @"
3561 TARGET_FMT_lx "\n", ctx->cia);
3562 } else {
3563 bar = TCG_MO_ST_LD;
3567 tcg_gen_mb(bar | TCG_BAR_SC);
3570 #if !defined(CONFIG_USER_ONLY)
3571 static inline void gen_check_tlb_flush(DisasContext *ctx, bool global)
3573 TCGv_i32 t;
3574 TCGLabel *l;
3576 if (!ctx->lazy_tlb_flush) {
3577 return;
3579 l = gen_new_label();
3580 t = tcg_temp_new_i32();
3581 tcg_gen_ld_i32(t, cpu_env, offsetof(CPUPPCState, tlb_need_flush));
3582 tcg_gen_brcondi_i32(TCG_COND_EQ, t, 0, l);
3583 if (global) {
3584 gen_helper_check_tlb_flush_global(cpu_env);
3585 } else {
3586 gen_helper_check_tlb_flush_local(cpu_env);
3588 gen_set_label(l);
3589 tcg_temp_free_i32(t);
3591 #else
3592 static inline void gen_check_tlb_flush(DisasContext *ctx, bool global) { }
3593 #endif
3595 /* isync */
3596 static void gen_isync(DisasContext *ctx)
3599 * We need to check for a pending TLB flush. This can only happen in
3600 * kernel mode however so check MSR_PR
3602 if (!ctx->pr) {
3603 gen_check_tlb_flush(ctx, false);
3605 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
3606 ctx->base.is_jmp = DISAS_EXIT_UPDATE;
3609 #define MEMOP_GET_SIZE(x) (1 << ((x) & MO_SIZE))
3611 static void gen_load_locked(DisasContext *ctx, MemOp memop)
3613 TCGv gpr = cpu_gpr[rD(ctx->opcode)];
3614 TCGv t0 = tcg_temp_new();
3616 gen_set_access_type(ctx, ACCESS_RES);
3617 gen_addr_reg_index(ctx, t0);
3618 tcg_gen_qemu_ld_tl(gpr, t0, ctx->mem_idx, memop | MO_ALIGN);
3619 tcg_gen_mov_tl(cpu_reserve, t0);
3620 tcg_gen_mov_tl(cpu_reserve_val, gpr);
3621 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3622 tcg_temp_free(t0);
3625 #define LARX(name, memop) \
3626 static void gen_##name(DisasContext *ctx) \
3628 gen_load_locked(ctx, memop); \
3631 /* lwarx */
3632 LARX(lbarx, DEF_MEMOP(MO_UB))
3633 LARX(lharx, DEF_MEMOP(MO_UW))
3634 LARX(lwarx, DEF_MEMOP(MO_UL))
3636 static void gen_fetch_inc_conditional(DisasContext *ctx, MemOp memop,
3637 TCGv EA, TCGCond cond, int addend)
3639 TCGv t = tcg_temp_new();
3640 TCGv t2 = tcg_temp_new();
3641 TCGv u = tcg_temp_new();
3643 tcg_gen_qemu_ld_tl(t, EA, ctx->mem_idx, memop);
3644 tcg_gen_addi_tl(t2, EA, MEMOP_GET_SIZE(memop));
3645 tcg_gen_qemu_ld_tl(t2, t2, ctx->mem_idx, memop);
3646 tcg_gen_addi_tl(u, t, addend);
3648 /* E.g. for fetch and increment bounded... */
3649 /* mem(EA,s) = (t != t2 ? u = t + 1 : t) */
3650 tcg_gen_movcond_tl(cond, u, t, t2, u, t);
3651 tcg_gen_qemu_st_tl(u, EA, ctx->mem_idx, memop);
3653 /* RT = (t != t2 ? t : u = 1<<(s*8-1)) */
3654 tcg_gen_movi_tl(u, 1 << (MEMOP_GET_SIZE(memop) * 8 - 1));
3655 tcg_gen_movcond_tl(cond, cpu_gpr[rD(ctx->opcode)], t, t2, t, u);
3657 tcg_temp_free(t);
3658 tcg_temp_free(t2);
3659 tcg_temp_free(u);
3662 static void gen_ld_atomic(DisasContext *ctx, MemOp memop)
3664 uint32_t gpr_FC = FC(ctx->opcode);
3665 TCGv EA = tcg_temp_new();
3666 int rt = rD(ctx->opcode);
3667 bool need_serial;
3668 TCGv src, dst;
3670 gen_addr_register(ctx, EA);
3671 dst = cpu_gpr[rt];
3672 src = cpu_gpr[(rt + 1) & 31];
3674 need_serial = false;
3675 memop |= MO_ALIGN;
3676 switch (gpr_FC) {
3677 case 0: /* Fetch and add */
3678 tcg_gen_atomic_fetch_add_tl(dst, EA, src, ctx->mem_idx, memop);
3679 break;
3680 case 1: /* Fetch and xor */
3681 tcg_gen_atomic_fetch_xor_tl(dst, EA, src, ctx->mem_idx, memop);
3682 break;
3683 case 2: /* Fetch and or */
3684 tcg_gen_atomic_fetch_or_tl(dst, EA, src, ctx->mem_idx, memop);
3685 break;
3686 case 3: /* Fetch and 'and' */
3687 tcg_gen_atomic_fetch_and_tl(dst, EA, src, ctx->mem_idx, memop);
3688 break;
3689 case 4: /* Fetch and max unsigned */
3690 tcg_gen_atomic_fetch_umax_tl(dst, EA, src, ctx->mem_idx, memop);
3691 break;
3692 case 5: /* Fetch and max signed */
3693 tcg_gen_atomic_fetch_smax_tl(dst, EA, src, ctx->mem_idx, memop);
3694 break;
3695 case 6: /* Fetch and min unsigned */
3696 tcg_gen_atomic_fetch_umin_tl(dst, EA, src, ctx->mem_idx, memop);
3697 break;
3698 case 7: /* Fetch and min signed */
3699 tcg_gen_atomic_fetch_smin_tl(dst, EA, src, ctx->mem_idx, memop);
3700 break;
3701 case 8: /* Swap */
3702 tcg_gen_atomic_xchg_tl(dst, EA, src, ctx->mem_idx, memop);
3703 break;
3705 case 16: /* Compare and swap not equal */
3706 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3707 need_serial = true;
3708 } else {
3709 TCGv t0 = tcg_temp_new();
3710 TCGv t1 = tcg_temp_new();
3712 tcg_gen_qemu_ld_tl(t0, EA, ctx->mem_idx, memop);
3713 if ((memop & MO_SIZE) == MO_64 || TARGET_LONG_BITS == 32) {
3714 tcg_gen_mov_tl(t1, src);
3715 } else {
3716 tcg_gen_ext32u_tl(t1, src);
3718 tcg_gen_movcond_tl(TCG_COND_NE, t1, t0, t1,
3719 cpu_gpr[(rt + 2) & 31], t0);
3720 tcg_gen_qemu_st_tl(t1, EA, ctx->mem_idx, memop);
3721 tcg_gen_mov_tl(dst, t0);
3723 tcg_temp_free(t0);
3724 tcg_temp_free(t1);
3726 break;
3728 case 24: /* Fetch and increment bounded */
3729 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3730 need_serial = true;
3731 } else {
3732 gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_NE, 1);
3734 break;
3735 case 25: /* Fetch and increment equal */
3736 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3737 need_serial = true;
3738 } else {
3739 gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_EQ, 1);
3741 break;
3742 case 28: /* Fetch and decrement bounded */
3743 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3744 need_serial = true;
3745 } else {
3746 gen_fetch_inc_conditional(ctx, memop, EA, TCG_COND_NE, -1);
3748 break;
3750 default:
3751 /* invoke data storage error handler */
3752 gen_exception_err(ctx, POWERPC_EXCP_DSI, POWERPC_EXCP_INVAL);
3754 tcg_temp_free(EA);
3756 if (need_serial) {
3757 /* Restart with exclusive lock. */
3758 gen_helper_exit_atomic(cpu_env);
3759 ctx->base.is_jmp = DISAS_NORETURN;
3763 static void gen_lwat(DisasContext *ctx)
3765 gen_ld_atomic(ctx, DEF_MEMOP(MO_UL));
3768 #ifdef TARGET_PPC64
3769 static void gen_ldat(DisasContext *ctx)
3771 gen_ld_atomic(ctx, DEF_MEMOP(MO_UQ));
3773 #endif
3775 static void gen_st_atomic(DisasContext *ctx, MemOp memop)
3777 uint32_t gpr_FC = FC(ctx->opcode);
3778 TCGv EA = tcg_temp_new();
3779 TCGv src, discard;
3781 gen_addr_register(ctx, EA);
3782 src = cpu_gpr[rD(ctx->opcode)];
3783 discard = tcg_temp_new();
3785 memop |= MO_ALIGN;
3786 switch (gpr_FC) {
3787 case 0: /* add and Store */
3788 tcg_gen_atomic_add_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3789 break;
3790 case 1: /* xor and Store */
3791 tcg_gen_atomic_xor_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3792 break;
3793 case 2: /* Or and Store */
3794 tcg_gen_atomic_or_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3795 break;
3796 case 3: /* 'and' and Store */
3797 tcg_gen_atomic_and_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3798 break;
3799 case 4: /* Store max unsigned */
3800 tcg_gen_atomic_umax_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3801 break;
3802 case 5: /* Store max signed */
3803 tcg_gen_atomic_smax_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3804 break;
3805 case 6: /* Store min unsigned */
3806 tcg_gen_atomic_umin_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3807 break;
3808 case 7: /* Store min signed */
3809 tcg_gen_atomic_smin_fetch_tl(discard, EA, src, ctx->mem_idx, memop);
3810 break;
3811 case 24: /* Store twin */
3812 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3813 /* Restart with exclusive lock. */
3814 gen_helper_exit_atomic(cpu_env);
3815 ctx->base.is_jmp = DISAS_NORETURN;
3816 } else {
3817 TCGv t = tcg_temp_new();
3818 TCGv t2 = tcg_temp_new();
3819 TCGv s = tcg_temp_new();
3820 TCGv s2 = tcg_temp_new();
3821 TCGv ea_plus_s = tcg_temp_new();
3823 tcg_gen_qemu_ld_tl(t, EA, ctx->mem_idx, memop);
3824 tcg_gen_addi_tl(ea_plus_s, EA, MEMOP_GET_SIZE(memop));
3825 tcg_gen_qemu_ld_tl(t2, ea_plus_s, ctx->mem_idx, memop);
3826 tcg_gen_movcond_tl(TCG_COND_EQ, s, t, t2, src, t);
3827 tcg_gen_movcond_tl(TCG_COND_EQ, s2, t, t2, src, t2);
3828 tcg_gen_qemu_st_tl(s, EA, ctx->mem_idx, memop);
3829 tcg_gen_qemu_st_tl(s2, ea_plus_s, ctx->mem_idx, memop);
3831 tcg_temp_free(ea_plus_s);
3832 tcg_temp_free(s2);
3833 tcg_temp_free(s);
3834 tcg_temp_free(t2);
3835 tcg_temp_free(t);
3837 break;
3838 default:
3839 /* invoke data storage error handler */
3840 gen_exception_err(ctx, POWERPC_EXCP_DSI, POWERPC_EXCP_INVAL);
3842 tcg_temp_free(discard);
3843 tcg_temp_free(EA);
3846 static void gen_stwat(DisasContext *ctx)
3848 gen_st_atomic(ctx, DEF_MEMOP(MO_UL));
3851 #ifdef TARGET_PPC64
3852 static void gen_stdat(DisasContext *ctx)
3854 gen_st_atomic(ctx, DEF_MEMOP(MO_UQ));
3856 #endif
3858 static void gen_conditional_store(DisasContext *ctx, MemOp memop)
3860 TCGLabel *l1 = gen_new_label();
3861 TCGLabel *l2 = gen_new_label();
3862 TCGv t0 = tcg_temp_new();
3863 int reg = rS(ctx->opcode);
3865 gen_set_access_type(ctx, ACCESS_RES);
3866 gen_addr_reg_index(ctx, t0);
3867 tcg_gen_brcond_tl(TCG_COND_NE, t0, cpu_reserve, l1);
3868 tcg_temp_free(t0);
3870 t0 = tcg_temp_new();
3871 tcg_gen_atomic_cmpxchg_tl(t0, cpu_reserve, cpu_reserve_val,
3872 cpu_gpr[reg], ctx->mem_idx,
3873 DEF_MEMOP(memop) | MO_ALIGN);
3874 tcg_gen_setcond_tl(TCG_COND_EQ, t0, t0, cpu_reserve_val);
3875 tcg_gen_shli_tl(t0, t0, CRF_EQ_BIT);
3876 tcg_gen_or_tl(t0, t0, cpu_so);
3877 tcg_gen_trunc_tl_i32(cpu_crf[0], t0);
3878 tcg_temp_free(t0);
3879 tcg_gen_br(l2);
3881 gen_set_label(l1);
3884 * Address mismatch implies failure. But we still need to provide
3885 * the memory barrier semantics of the instruction.
3887 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3888 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
3890 gen_set_label(l2);
3891 tcg_gen_movi_tl(cpu_reserve, -1);
3894 #define STCX(name, memop) \
3895 static void gen_##name(DisasContext *ctx) \
3897 gen_conditional_store(ctx, memop); \
3900 STCX(stbcx_, DEF_MEMOP(MO_UB))
3901 STCX(sthcx_, DEF_MEMOP(MO_UW))
3902 STCX(stwcx_, DEF_MEMOP(MO_UL))
3904 #if defined(TARGET_PPC64)
3905 /* ldarx */
3906 LARX(ldarx, DEF_MEMOP(MO_UQ))
3907 /* stdcx. */
3908 STCX(stdcx_, DEF_MEMOP(MO_UQ))
3910 /* lqarx */
3911 static void gen_lqarx(DisasContext *ctx)
3913 int rd = rD(ctx->opcode);
3914 TCGv EA, hi, lo;
3916 if (unlikely((rd & 1) || (rd == rA(ctx->opcode)) ||
3917 (rd == rB(ctx->opcode)))) {
3918 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3919 return;
3922 gen_set_access_type(ctx, ACCESS_RES);
3923 EA = tcg_temp_new();
3924 gen_addr_reg_index(ctx, EA);
3926 /* Note that the low part is always in RD+1, even in LE mode. */
3927 lo = cpu_gpr[rd + 1];
3928 hi = cpu_gpr[rd];
3930 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3931 if (HAVE_ATOMIC128) {
3932 TCGv_i32 oi = tcg_temp_new_i32();
3933 if (ctx->le_mode) {
3934 tcg_gen_movi_i32(oi, make_memop_idx(MO_LE | MO_128 | MO_ALIGN,
3935 ctx->mem_idx));
3936 gen_helper_lq_le_parallel(lo, cpu_env, EA, oi);
3937 } else {
3938 tcg_gen_movi_i32(oi, make_memop_idx(MO_BE | MO_128 | MO_ALIGN,
3939 ctx->mem_idx));
3940 gen_helper_lq_be_parallel(lo, cpu_env, EA, oi);
3942 tcg_temp_free_i32(oi);
3943 tcg_gen_ld_i64(hi, cpu_env, offsetof(CPUPPCState, retxh));
3944 } else {
3945 /* Restart with exclusive lock. */
3946 gen_helper_exit_atomic(cpu_env);
3947 ctx->base.is_jmp = DISAS_NORETURN;
3948 tcg_temp_free(EA);
3949 return;
3951 } else if (ctx->le_mode) {
3952 tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_LEUQ | MO_ALIGN_16);
3953 tcg_gen_mov_tl(cpu_reserve, EA);
3954 gen_addr_add(ctx, EA, EA, 8);
3955 tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_LEUQ);
3956 } else {
3957 tcg_gen_qemu_ld_i64(hi, EA, ctx->mem_idx, MO_BEUQ | MO_ALIGN_16);
3958 tcg_gen_mov_tl(cpu_reserve, EA);
3959 gen_addr_add(ctx, EA, EA, 8);
3960 tcg_gen_qemu_ld_i64(lo, EA, ctx->mem_idx, MO_BEUQ);
3962 tcg_temp_free(EA);
3964 tcg_gen_st_tl(hi, cpu_env, offsetof(CPUPPCState, reserve_val));
3965 tcg_gen_st_tl(lo, cpu_env, offsetof(CPUPPCState, reserve_val2));
3968 /* stqcx. */
3969 static void gen_stqcx_(DisasContext *ctx)
3971 int rs = rS(ctx->opcode);
3972 TCGv EA, hi, lo;
3974 if (unlikely(rs & 1)) {
3975 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
3976 return;
3979 gen_set_access_type(ctx, ACCESS_RES);
3980 EA = tcg_temp_new();
3981 gen_addr_reg_index(ctx, EA);
3983 /* Note that the low part is always in RS+1, even in LE mode. */
3984 lo = cpu_gpr[rs + 1];
3985 hi = cpu_gpr[rs];
3987 if (tb_cflags(ctx->base.tb) & CF_PARALLEL) {
3988 if (HAVE_CMPXCHG128) {
3989 TCGv_i32 oi = tcg_const_i32(DEF_MEMOP(MO_128) | MO_ALIGN);
3990 if (ctx->le_mode) {
3991 gen_helper_stqcx_le_parallel(cpu_crf[0], cpu_env,
3992 EA, lo, hi, oi);
3993 } else {
3994 gen_helper_stqcx_be_parallel(cpu_crf[0], cpu_env,
3995 EA, lo, hi, oi);
3997 tcg_temp_free_i32(oi);
3998 } else {
3999 /* Restart with exclusive lock. */
4000 gen_helper_exit_atomic(cpu_env);
4001 ctx->base.is_jmp = DISAS_NORETURN;
4003 tcg_temp_free(EA);
4004 } else {
4005 TCGLabel *lab_fail = gen_new_label();
4006 TCGLabel *lab_over = gen_new_label();
4007 TCGv_i64 t0 = tcg_temp_new_i64();
4008 TCGv_i64 t1 = tcg_temp_new_i64();
4010 tcg_gen_brcond_tl(TCG_COND_NE, EA, cpu_reserve, lab_fail);
4011 tcg_temp_free(EA);
4013 gen_qemu_ld64_i64(ctx, t0, cpu_reserve);
4014 tcg_gen_ld_i64(t1, cpu_env, (ctx->le_mode
4015 ? offsetof(CPUPPCState, reserve_val2)
4016 : offsetof(CPUPPCState, reserve_val)));
4017 tcg_gen_brcond_i64(TCG_COND_NE, t0, t1, lab_fail);
4019 tcg_gen_addi_i64(t0, cpu_reserve, 8);
4020 gen_qemu_ld64_i64(ctx, t0, t0);
4021 tcg_gen_ld_i64(t1, cpu_env, (ctx->le_mode
4022 ? offsetof(CPUPPCState, reserve_val)
4023 : offsetof(CPUPPCState, reserve_val2)));
4024 tcg_gen_brcond_i64(TCG_COND_NE, t0, t1, lab_fail);
4026 /* Success */
4027 gen_qemu_st64_i64(ctx, ctx->le_mode ? lo : hi, cpu_reserve);
4028 tcg_gen_addi_i64(t0, cpu_reserve, 8);
4029 gen_qemu_st64_i64(ctx, ctx->le_mode ? hi : lo, t0);
4031 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4032 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], CRF_EQ);
4033 tcg_gen_br(lab_over);
4035 gen_set_label(lab_fail);
4036 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
4038 gen_set_label(lab_over);
4039 tcg_gen_movi_tl(cpu_reserve, -1);
4040 tcg_temp_free_i64(t0);
4041 tcg_temp_free_i64(t1);
4044 #endif /* defined(TARGET_PPC64) */
4046 /* sync */
4047 static void gen_sync(DisasContext *ctx)
4049 TCGBar bar = TCG_MO_ALL;
4050 uint32_t l = (ctx->opcode >> 21) & 3;
4052 if ((l == 1) && (ctx->insns_flags2 & PPC2_MEM_LWSYNC)) {
4053 bar = TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST;
4057 * We may need to check for a pending TLB flush.
4059 * We do this on ptesync (l == 2) on ppc64 and any sync pn ppc32.
4061 * Additionally, this can only happen in kernel mode however so
4062 * check MSR_PR as well.
4064 if (((l == 2) || !(ctx->insns_flags & PPC_64B)) && !ctx->pr) {
4065 gen_check_tlb_flush(ctx, true);
4068 tcg_gen_mb(bar | TCG_BAR_SC);
4071 /* wait */
4072 static void gen_wait(DisasContext *ctx)
4074 TCGv_i32 t0 = tcg_const_i32(1);
4075 tcg_gen_st_i32(t0, cpu_env,
4076 -offsetof(PowerPCCPU, env) + offsetof(CPUState, halted));
4077 tcg_temp_free_i32(t0);
4078 /* Stop translation, as the CPU is supposed to sleep from now */
4079 gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
4082 #if defined(TARGET_PPC64)
4083 static void gen_doze(DisasContext *ctx)
4085 #if defined(CONFIG_USER_ONLY)
4086 GEN_PRIV(ctx);
4087 #else
4088 TCGv_i32 t;
4090 CHK_HV(ctx);
4091 t = tcg_const_i32(PPC_PM_DOZE);
4092 gen_helper_pminsn(cpu_env, t);
4093 tcg_temp_free_i32(t);
4094 /* Stop translation, as the CPU is supposed to sleep from now */
4095 gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
4096 #endif /* defined(CONFIG_USER_ONLY) */
4099 static void gen_nap(DisasContext *ctx)
4101 #if defined(CONFIG_USER_ONLY)
4102 GEN_PRIV(ctx);
4103 #else
4104 TCGv_i32 t;
4106 CHK_HV(ctx);
4107 t = tcg_const_i32(PPC_PM_NAP);
4108 gen_helper_pminsn(cpu_env, t);
4109 tcg_temp_free_i32(t);
4110 /* Stop translation, as the CPU is supposed to sleep from now */
4111 gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
4112 #endif /* defined(CONFIG_USER_ONLY) */
4115 static void gen_stop(DisasContext *ctx)
4117 #if defined(CONFIG_USER_ONLY)
4118 GEN_PRIV(ctx);
4119 #else
4120 TCGv_i32 t;
4122 CHK_HV(ctx);
4123 t = tcg_const_i32(PPC_PM_STOP);
4124 gen_helper_pminsn(cpu_env, t);
4125 tcg_temp_free_i32(t);
4126 /* Stop translation, as the CPU is supposed to sleep from now */
4127 gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
4128 #endif /* defined(CONFIG_USER_ONLY) */
4131 static void gen_sleep(DisasContext *ctx)
4133 #if defined(CONFIG_USER_ONLY)
4134 GEN_PRIV(ctx);
4135 #else
4136 TCGv_i32 t;
4138 CHK_HV(ctx);
4139 t = tcg_const_i32(PPC_PM_SLEEP);
4140 gen_helper_pminsn(cpu_env, t);
4141 tcg_temp_free_i32(t);
4142 /* Stop translation, as the CPU is supposed to sleep from now */
4143 gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
4144 #endif /* defined(CONFIG_USER_ONLY) */
4147 static void gen_rvwinkle(DisasContext *ctx)
4149 #if defined(CONFIG_USER_ONLY)
4150 GEN_PRIV(ctx);
4151 #else
4152 TCGv_i32 t;
4154 CHK_HV(ctx);
4155 t = tcg_const_i32(PPC_PM_RVWINKLE);
4156 gen_helper_pminsn(cpu_env, t);
4157 tcg_temp_free_i32(t);
4158 /* Stop translation, as the CPU is supposed to sleep from now */
4159 gen_exception_nip(ctx, EXCP_HLT, ctx->base.pc_next);
4160 #endif /* defined(CONFIG_USER_ONLY) */
4162 #endif /* #if defined(TARGET_PPC64) */
4164 static inline void gen_update_cfar(DisasContext *ctx, target_ulong nip)
4166 #if defined(TARGET_PPC64)
4167 if (ctx->has_cfar) {
4168 tcg_gen_movi_tl(cpu_cfar, nip);
4170 #endif
4173 #if defined(TARGET_PPC64)
4174 static void pmu_count_insns(DisasContext *ctx)
4177 * Do not bother calling the helper if the PMU isn't counting
4178 * instructions.
4180 if (!ctx->pmu_insn_cnt) {
4181 return;
4184 #if !defined(CONFIG_USER_ONLY)
4186 * The PMU insns_inc() helper stops the internal PMU timer if a
4187 * counter overflows happens. In that case, if the guest is
4188 * running with icount and we do not handle it beforehand,
4189 * the helper can trigger a 'bad icount read'.
4191 gen_icount_io_start(ctx);
4193 gen_helper_insns_inc(cpu_env, tcg_constant_i32(ctx->base.num_insns));
4194 #else
4196 * User mode can read (but not write) PMC5 and start/stop
4197 * the PMU via MMCR0_FC. In this case just increment
4198 * PMC5 with base.num_insns.
4200 TCGv t0 = tcg_temp_new();
4202 gen_load_spr(t0, SPR_POWER_PMC5);
4203 tcg_gen_addi_tl(t0, t0, ctx->base.num_insns);
4204 gen_store_spr(SPR_POWER_PMC5, t0);
4206 tcg_temp_free(t0);
4207 #endif /* #if !defined(CONFIG_USER_ONLY) */
4209 #else
4210 static void pmu_count_insns(DisasContext *ctx)
4212 return;
4214 #endif /* #if defined(TARGET_PPC64) */
4216 static inline bool use_goto_tb(DisasContext *ctx, target_ulong dest)
4218 return translator_use_goto_tb(&ctx->base, dest);
4221 static void gen_lookup_and_goto_ptr(DisasContext *ctx)
4223 if (unlikely(ctx->singlestep_enabled)) {
4224 gen_debug_exception(ctx);
4225 } else {
4227 * tcg_gen_lookup_and_goto_ptr will exit the TB if
4228 * CF_NO_GOTO_PTR is set. Count insns now.
4230 if (ctx->base.tb->flags & CF_NO_GOTO_PTR) {
4231 pmu_count_insns(ctx);
4234 tcg_gen_lookup_and_goto_ptr();
4238 /*** Branch ***/
4239 static void gen_goto_tb(DisasContext *ctx, int n, target_ulong dest)
4241 if (NARROW_MODE(ctx)) {
4242 dest = (uint32_t) dest;
4244 if (use_goto_tb(ctx, dest)) {
4245 pmu_count_insns(ctx);
4246 tcg_gen_goto_tb(n);
4247 tcg_gen_movi_tl(cpu_nip, dest & ~3);
4248 tcg_gen_exit_tb(ctx->base.tb, n);
4249 } else {
4250 tcg_gen_movi_tl(cpu_nip, dest & ~3);
4251 gen_lookup_and_goto_ptr(ctx);
4255 static inline void gen_setlr(DisasContext *ctx, target_ulong nip)
4257 if (NARROW_MODE(ctx)) {
4258 nip = (uint32_t)nip;
4260 tcg_gen_movi_tl(cpu_lr, nip);
4263 /* b ba bl bla */
4264 static void gen_b(DisasContext *ctx)
4266 target_ulong li, target;
4268 /* sign extend LI */
4269 li = LI(ctx->opcode);
4270 li = (li ^ 0x02000000) - 0x02000000;
4271 if (likely(AA(ctx->opcode) == 0)) {
4272 target = ctx->cia + li;
4273 } else {
4274 target = li;
4276 if (LK(ctx->opcode)) {
4277 gen_setlr(ctx, ctx->base.pc_next);
4279 gen_update_cfar(ctx, ctx->cia);
4280 gen_goto_tb(ctx, 0, target);
4281 ctx->base.is_jmp = DISAS_NORETURN;
4284 #define BCOND_IM 0
4285 #define BCOND_LR 1
4286 #define BCOND_CTR 2
4287 #define BCOND_TAR 3
4289 static void gen_bcond(DisasContext *ctx, int type)
4291 uint32_t bo = BO(ctx->opcode);
4292 TCGLabel *l1;
4293 TCGv target;
4295 if (type == BCOND_LR || type == BCOND_CTR || type == BCOND_TAR) {
4296 target = tcg_temp_local_new();
4297 if (type == BCOND_CTR) {
4298 tcg_gen_mov_tl(target, cpu_ctr);
4299 } else if (type == BCOND_TAR) {
4300 gen_load_spr(target, SPR_TAR);
4301 } else {
4302 tcg_gen_mov_tl(target, cpu_lr);
4304 } else {
4305 target = NULL;
4307 if (LK(ctx->opcode)) {
4308 gen_setlr(ctx, ctx->base.pc_next);
4310 l1 = gen_new_label();
4311 if ((bo & 0x4) == 0) {
4312 /* Decrement and test CTR */
4313 TCGv temp = tcg_temp_new();
4315 if (type == BCOND_CTR) {
4317 * All ISAs up to v3 describe this form of bcctr as invalid but
4318 * some processors, ie. 64-bit server processors compliant with
4319 * arch 2.x, do implement a "test and decrement" logic instead,
4320 * as described in their respective UMs. This logic involves CTR
4321 * to act as both the branch target and a counter, which makes
4322 * it basically useless and thus never used in real code.
4324 * This form was hence chosen to trigger extra micro-architectural
4325 * side-effect on real HW needed for the Spectre v2 workaround.
4326 * It is up to guests that implement such workaround, ie. linux, to
4327 * use this form in a way it just triggers the side-effect without
4328 * doing anything else harmful.
4330 if (unlikely(!is_book3s_arch2x(ctx))) {
4331 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
4332 tcg_temp_free(temp);
4333 tcg_temp_free(target);
4334 return;
4337 if (NARROW_MODE(ctx)) {
4338 tcg_gen_ext32u_tl(temp, cpu_ctr);
4339 } else {
4340 tcg_gen_mov_tl(temp, cpu_ctr);
4342 if (bo & 0x2) {
4343 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
4344 } else {
4345 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
4347 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
4348 } else {
4349 tcg_gen_subi_tl(cpu_ctr, cpu_ctr, 1);
4350 if (NARROW_MODE(ctx)) {
4351 tcg_gen_ext32u_tl(temp, cpu_ctr);
4352 } else {
4353 tcg_gen_mov_tl(temp, cpu_ctr);
4355 if (bo & 0x2) {
4356 tcg_gen_brcondi_tl(TCG_COND_NE, temp, 0, l1);
4357 } else {
4358 tcg_gen_brcondi_tl(TCG_COND_EQ, temp, 0, l1);
4361 tcg_temp_free(temp);
4363 if ((bo & 0x10) == 0) {
4364 /* Test CR */
4365 uint32_t bi = BI(ctx->opcode);
4366 uint32_t mask = 0x08 >> (bi & 0x03);
4367 TCGv_i32 temp = tcg_temp_new_i32();
4369 if (bo & 0x8) {
4370 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
4371 tcg_gen_brcondi_i32(TCG_COND_EQ, temp, 0, l1);
4372 } else {
4373 tcg_gen_andi_i32(temp, cpu_crf[bi >> 2], mask);
4374 tcg_gen_brcondi_i32(TCG_COND_NE, temp, 0, l1);
4376 tcg_temp_free_i32(temp);
4378 gen_update_cfar(ctx, ctx->cia);
4379 if (type == BCOND_IM) {
4380 target_ulong li = (target_long)((int16_t)(BD(ctx->opcode)));
4381 if (likely(AA(ctx->opcode) == 0)) {
4382 gen_goto_tb(ctx, 0, ctx->cia + li);
4383 } else {
4384 gen_goto_tb(ctx, 0, li);
4386 } else {
4387 if (NARROW_MODE(ctx)) {
4388 tcg_gen_andi_tl(cpu_nip, target, (uint32_t)~3);
4389 } else {
4390 tcg_gen_andi_tl(cpu_nip, target, ~3);
4392 gen_lookup_and_goto_ptr(ctx);
4393 tcg_temp_free(target);
4395 if ((bo & 0x14) != 0x14) {
4396 /* fallthrough case */
4397 gen_set_label(l1);
4398 gen_goto_tb(ctx, 1, ctx->base.pc_next);
4400 ctx->base.is_jmp = DISAS_NORETURN;
4403 static void gen_bc(DisasContext *ctx)
4405 gen_bcond(ctx, BCOND_IM);
4408 static void gen_bcctr(DisasContext *ctx)
4410 gen_bcond(ctx, BCOND_CTR);
4413 static void gen_bclr(DisasContext *ctx)
4415 gen_bcond(ctx, BCOND_LR);
4418 static void gen_bctar(DisasContext *ctx)
4420 gen_bcond(ctx, BCOND_TAR);
4423 /*** Condition register logical ***/
4424 #define GEN_CRLOGIC(name, tcg_op, opc) \
4425 static void glue(gen_, name)(DisasContext *ctx) \
4427 uint8_t bitmask; \
4428 int sh; \
4429 TCGv_i32 t0, t1; \
4430 sh = (crbD(ctx->opcode) & 0x03) - (crbA(ctx->opcode) & 0x03); \
4431 t0 = tcg_temp_new_i32(); \
4432 if (sh > 0) \
4433 tcg_gen_shri_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], sh); \
4434 else if (sh < 0) \
4435 tcg_gen_shli_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2], -sh); \
4436 else \
4437 tcg_gen_mov_i32(t0, cpu_crf[crbA(ctx->opcode) >> 2]); \
4438 t1 = tcg_temp_new_i32(); \
4439 sh = (crbD(ctx->opcode) & 0x03) - (crbB(ctx->opcode) & 0x03); \
4440 if (sh > 0) \
4441 tcg_gen_shri_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], sh); \
4442 else if (sh < 0) \
4443 tcg_gen_shli_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2], -sh); \
4444 else \
4445 tcg_gen_mov_i32(t1, cpu_crf[crbB(ctx->opcode) >> 2]); \
4446 tcg_op(t0, t0, t1); \
4447 bitmask = 0x08 >> (crbD(ctx->opcode) & 0x03); \
4448 tcg_gen_andi_i32(t0, t0, bitmask); \
4449 tcg_gen_andi_i32(t1, cpu_crf[crbD(ctx->opcode) >> 2], ~bitmask); \
4450 tcg_gen_or_i32(cpu_crf[crbD(ctx->opcode) >> 2], t0, t1); \
4451 tcg_temp_free_i32(t0); \
4452 tcg_temp_free_i32(t1); \
4455 /* crand */
4456 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08);
4457 /* crandc */
4458 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04);
4459 /* creqv */
4460 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09);
4461 /* crnand */
4462 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07);
4463 /* crnor */
4464 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01);
4465 /* cror */
4466 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E);
4467 /* crorc */
4468 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D);
4469 /* crxor */
4470 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06);
4472 /* mcrf */
4473 static void gen_mcrf(DisasContext *ctx)
4475 tcg_gen_mov_i32(cpu_crf[crfD(ctx->opcode)], cpu_crf[crfS(ctx->opcode)]);
4478 /*** System linkage ***/
4480 /* rfi (supervisor only) */
4481 static void gen_rfi(DisasContext *ctx)
4483 #if defined(CONFIG_USER_ONLY)
4484 GEN_PRIV(ctx);
4485 #else
4487 * This instruction doesn't exist anymore on 64-bit server
4488 * processors compliant with arch 2.x
4490 if (is_book3s_arch2x(ctx)) {
4491 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
4492 return;
4494 /* Restore CPU state */
4495 CHK_SV(ctx);
4496 gen_icount_io_start(ctx);
4497 gen_update_cfar(ctx, ctx->cia);
4498 gen_helper_rfi(cpu_env);
4499 ctx->base.is_jmp = DISAS_EXIT;
4500 #endif
4503 #if defined(TARGET_PPC64)
4504 static void gen_rfid(DisasContext *ctx)
4506 #if defined(CONFIG_USER_ONLY)
4507 GEN_PRIV(ctx);
4508 #else
4509 /* Restore CPU state */
4510 CHK_SV(ctx);
4511 gen_icount_io_start(ctx);
4512 gen_update_cfar(ctx, ctx->cia);
4513 gen_helper_rfid(cpu_env);
4514 ctx->base.is_jmp = DISAS_EXIT;
4515 #endif
4518 #if !defined(CONFIG_USER_ONLY)
4519 static void gen_rfscv(DisasContext *ctx)
4521 #if defined(CONFIG_USER_ONLY)
4522 GEN_PRIV(ctx);
4523 #else
4524 /* Restore CPU state */
4525 CHK_SV(ctx);
4526 gen_icount_io_start(ctx);
4527 gen_update_cfar(ctx, ctx->cia);
4528 gen_helper_rfscv(cpu_env);
4529 ctx->base.is_jmp = DISAS_EXIT;
4530 #endif
4532 #endif
4534 static void gen_hrfid(DisasContext *ctx)
4536 #if defined(CONFIG_USER_ONLY)
4537 GEN_PRIV(ctx);
4538 #else
4539 /* Restore CPU state */
4540 CHK_HV(ctx);
4541 gen_helper_hrfid(cpu_env);
4542 ctx->base.is_jmp = DISAS_EXIT;
4543 #endif
4545 #endif
4547 /* sc */
4548 #if defined(CONFIG_USER_ONLY)
4549 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL_USER
4550 #else
4551 #define POWERPC_SYSCALL POWERPC_EXCP_SYSCALL
4552 #define POWERPC_SYSCALL_VECTORED POWERPC_EXCP_SYSCALL_VECTORED
4553 #endif
4554 static void gen_sc(DisasContext *ctx)
4556 uint32_t lev;
4558 lev = (ctx->opcode >> 5) & 0x7F;
4559 gen_exception_err(ctx, POWERPC_SYSCALL, lev);
4562 #if defined(TARGET_PPC64)
4563 #if !defined(CONFIG_USER_ONLY)
4564 static void gen_scv(DisasContext *ctx)
4566 uint32_t lev = (ctx->opcode >> 5) & 0x7F;
4568 /* Set the PC back to the faulting instruction. */
4569 gen_update_nip(ctx, ctx->cia);
4570 gen_helper_scv(cpu_env, tcg_constant_i32(lev));
4572 ctx->base.is_jmp = DISAS_NORETURN;
4574 #endif
4575 #endif
4577 /*** Trap ***/
4579 /* Check for unconditional traps (always or never) */
4580 static bool check_unconditional_trap(DisasContext *ctx)
4582 /* Trap never */
4583 if (TO(ctx->opcode) == 0) {
4584 return true;
4586 /* Trap always */
4587 if (TO(ctx->opcode) == 31) {
4588 gen_exception_err(ctx, POWERPC_EXCP_PROGRAM, POWERPC_EXCP_TRAP);
4589 return true;
4591 return false;
4594 /* tw */
4595 static void gen_tw(DisasContext *ctx)
4597 TCGv_i32 t0;
4599 if (check_unconditional_trap(ctx)) {
4600 return;
4602 t0 = tcg_const_i32(TO(ctx->opcode));
4603 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4604 t0);
4605 tcg_temp_free_i32(t0);
4608 /* twi */
4609 static void gen_twi(DisasContext *ctx)
4611 TCGv t0;
4612 TCGv_i32 t1;
4614 if (check_unconditional_trap(ctx)) {
4615 return;
4617 t0 = tcg_const_tl(SIMM(ctx->opcode));
4618 t1 = tcg_const_i32(TO(ctx->opcode));
4619 gen_helper_tw(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4620 tcg_temp_free(t0);
4621 tcg_temp_free_i32(t1);
4624 #if defined(TARGET_PPC64)
4625 /* td */
4626 static void gen_td(DisasContext *ctx)
4628 TCGv_i32 t0;
4630 if (check_unconditional_trap(ctx)) {
4631 return;
4633 t0 = tcg_const_i32(TO(ctx->opcode));
4634 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
4635 t0);
4636 tcg_temp_free_i32(t0);
4639 /* tdi */
4640 static void gen_tdi(DisasContext *ctx)
4642 TCGv t0;
4643 TCGv_i32 t1;
4645 if (check_unconditional_trap(ctx)) {
4646 return;
4648 t0 = tcg_const_tl(SIMM(ctx->opcode));
4649 t1 = tcg_const_i32(TO(ctx->opcode));
4650 gen_helper_td(cpu_env, cpu_gpr[rA(ctx->opcode)], t0, t1);
4651 tcg_temp_free(t0);
4652 tcg_temp_free_i32(t1);
4654 #endif
4656 /*** Processor control ***/
4658 /* mcrxr */
4659 static void gen_mcrxr(DisasContext *ctx)
4661 TCGv_i32 t0 = tcg_temp_new_i32();
4662 TCGv_i32 t1 = tcg_temp_new_i32();
4663 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4665 tcg_gen_trunc_tl_i32(t0, cpu_so);
4666 tcg_gen_trunc_tl_i32(t1, cpu_ov);
4667 tcg_gen_trunc_tl_i32(dst, cpu_ca);
4668 tcg_gen_shli_i32(t0, t0, 3);
4669 tcg_gen_shli_i32(t1, t1, 2);
4670 tcg_gen_shli_i32(dst, dst, 1);
4671 tcg_gen_or_i32(dst, dst, t0);
4672 tcg_gen_or_i32(dst, dst, t1);
4673 tcg_temp_free_i32(t0);
4674 tcg_temp_free_i32(t1);
4676 tcg_gen_movi_tl(cpu_so, 0);
4677 tcg_gen_movi_tl(cpu_ov, 0);
4678 tcg_gen_movi_tl(cpu_ca, 0);
4681 #ifdef TARGET_PPC64
4682 /* mcrxrx */
4683 static void gen_mcrxrx(DisasContext *ctx)
4685 TCGv t0 = tcg_temp_new();
4686 TCGv t1 = tcg_temp_new();
4687 TCGv_i32 dst = cpu_crf[crfD(ctx->opcode)];
4689 /* copy OV and OV32 */
4690 tcg_gen_shli_tl(t0, cpu_ov, 1);
4691 tcg_gen_or_tl(t0, t0, cpu_ov32);
4692 tcg_gen_shli_tl(t0, t0, 2);
4693 /* copy CA and CA32 */
4694 tcg_gen_shli_tl(t1, cpu_ca, 1);
4695 tcg_gen_or_tl(t1, t1, cpu_ca32);
4696 tcg_gen_or_tl(t0, t0, t1);
4697 tcg_gen_trunc_tl_i32(dst, t0);
4698 tcg_temp_free(t0);
4699 tcg_temp_free(t1);
4701 #endif
4703 /* mfcr mfocrf */
4704 static void gen_mfcr(DisasContext *ctx)
4706 uint32_t crm, crn;
4708 if (likely(ctx->opcode & 0x00100000)) {
4709 crm = CRM(ctx->opcode);
4710 if (likely(crm && ((crm & (crm - 1)) == 0))) {
4711 crn = ctz32(crm);
4712 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], cpu_crf[7 - crn]);
4713 tcg_gen_shli_tl(cpu_gpr[rD(ctx->opcode)],
4714 cpu_gpr[rD(ctx->opcode)], crn * 4);
4716 } else {
4717 TCGv_i32 t0 = tcg_temp_new_i32();
4718 tcg_gen_mov_i32(t0, cpu_crf[0]);
4719 tcg_gen_shli_i32(t0, t0, 4);
4720 tcg_gen_or_i32(t0, t0, cpu_crf[1]);
4721 tcg_gen_shli_i32(t0, t0, 4);
4722 tcg_gen_or_i32(t0, t0, cpu_crf[2]);
4723 tcg_gen_shli_i32(t0, t0, 4);
4724 tcg_gen_or_i32(t0, t0, cpu_crf[3]);
4725 tcg_gen_shli_i32(t0, t0, 4);
4726 tcg_gen_or_i32(t0, t0, cpu_crf[4]);
4727 tcg_gen_shli_i32(t0, t0, 4);
4728 tcg_gen_or_i32(t0, t0, cpu_crf[5]);
4729 tcg_gen_shli_i32(t0, t0, 4);
4730 tcg_gen_or_i32(t0, t0, cpu_crf[6]);
4731 tcg_gen_shli_i32(t0, t0, 4);
4732 tcg_gen_or_i32(t0, t0, cpu_crf[7]);
4733 tcg_gen_extu_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
4734 tcg_temp_free_i32(t0);
4738 /* mfmsr */
4739 static void gen_mfmsr(DisasContext *ctx)
4741 CHK_SV(ctx);
4742 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], cpu_msr);
4745 /* mfspr */
4746 static inline void gen_op_mfspr(DisasContext *ctx)
4748 void (*read_cb)(DisasContext *ctx, int gprn, int sprn);
4749 uint32_t sprn = SPR(ctx->opcode);
4751 #if defined(CONFIG_USER_ONLY)
4752 read_cb = ctx->spr_cb[sprn].uea_read;
4753 #else
4754 if (ctx->pr) {
4755 read_cb = ctx->spr_cb[sprn].uea_read;
4756 } else if (ctx->hv) {
4757 read_cb = ctx->spr_cb[sprn].hea_read;
4758 } else {
4759 read_cb = ctx->spr_cb[sprn].oea_read;
4761 #endif
4762 if (likely(read_cb != NULL)) {
4763 if (likely(read_cb != SPR_NOACCESS)) {
4764 (*read_cb)(ctx, rD(ctx->opcode), sprn);
4765 } else {
4766 /* Privilege exception */
4768 * This is a hack to avoid warnings when running Linux:
4769 * this OS breaks the PowerPC virtualisation model,
4770 * allowing userland application to read the PVR
4772 if (sprn != SPR_PVR) {
4773 qemu_log_mask(LOG_GUEST_ERROR, "Trying to read privileged spr "
4774 "%d (0x%03x) at " TARGET_FMT_lx "\n", sprn, sprn,
4775 ctx->cia);
4777 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4779 } else {
4780 /* ISA 2.07 defines these as no-ops */
4781 if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4782 (sprn >= 808 && sprn <= 811)) {
4783 /* This is a nop */
4784 return;
4786 /* Not defined */
4787 qemu_log_mask(LOG_GUEST_ERROR,
4788 "Trying to read invalid spr %d (0x%03x) at "
4789 TARGET_FMT_lx "\n", sprn, sprn, ctx->cia);
4792 * The behaviour depends on MSR:PR and SPR# bit 0x10, it can
4793 * generate a priv, a hv emu or a no-op
4795 if (sprn & 0x10) {
4796 if (ctx->pr) {
4797 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4799 } else {
4800 if (ctx->pr || sprn == 0 || sprn == 4 || sprn == 5 || sprn == 6) {
4801 gen_hvpriv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4807 static void gen_mfspr(DisasContext *ctx)
4809 gen_op_mfspr(ctx);
4812 /* mftb */
4813 static void gen_mftb(DisasContext *ctx)
4815 gen_op_mfspr(ctx);
4818 /* mtcrf mtocrf*/
4819 static void gen_mtcrf(DisasContext *ctx)
4821 uint32_t crm, crn;
4823 crm = CRM(ctx->opcode);
4824 if (likely((ctx->opcode & 0x00100000))) {
4825 if (crm && ((crm & (crm - 1)) == 0)) {
4826 TCGv_i32 temp = tcg_temp_new_i32();
4827 crn = ctz32(crm);
4828 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4829 tcg_gen_shri_i32(temp, temp, crn * 4);
4830 tcg_gen_andi_i32(cpu_crf[7 - crn], temp, 0xf);
4831 tcg_temp_free_i32(temp);
4833 } else {
4834 TCGv_i32 temp = tcg_temp_new_i32();
4835 tcg_gen_trunc_tl_i32(temp, cpu_gpr[rS(ctx->opcode)]);
4836 for (crn = 0 ; crn < 8 ; crn++) {
4837 if (crm & (1 << crn)) {
4838 tcg_gen_shri_i32(cpu_crf[7 - crn], temp, crn * 4);
4839 tcg_gen_andi_i32(cpu_crf[7 - crn], cpu_crf[7 - crn], 0xf);
4842 tcg_temp_free_i32(temp);
4846 /* mtmsr */
4847 #if defined(TARGET_PPC64)
4848 static void gen_mtmsrd(DisasContext *ctx)
4850 if (unlikely(!is_book3s_arch2x(ctx))) {
4851 gen_invalid(ctx);
4852 return;
4855 CHK_SV(ctx);
4857 #if !defined(CONFIG_USER_ONLY)
4858 TCGv t0, t1;
4859 target_ulong mask;
4861 t0 = tcg_temp_new();
4862 t1 = tcg_temp_new();
4864 gen_icount_io_start(ctx);
4866 if (ctx->opcode & 0x00010000) {
4867 /* L=1 form only updates EE and RI */
4868 mask = (1ULL << MSR_RI) | (1ULL << MSR_EE);
4869 } else {
4870 /* mtmsrd does not alter HV, S, ME, or LE */
4871 mask = ~((1ULL << MSR_LE) | (1ULL << MSR_ME) | (1ULL << MSR_S) |
4872 (1ULL << MSR_HV));
4874 * XXX: we need to update nip before the store if we enter
4875 * power saving mode, we will exit the loop directly from
4876 * ppc_store_msr
4878 gen_update_nip(ctx, ctx->base.pc_next);
4881 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], mask);
4882 tcg_gen_andi_tl(t1, cpu_msr, ~mask);
4883 tcg_gen_or_tl(t0, t0, t1);
4885 gen_helper_store_msr(cpu_env, t0);
4887 /* Must stop the translation as machine state (may have) changed */
4888 ctx->base.is_jmp = DISAS_EXIT_UPDATE;
4890 tcg_temp_free(t0);
4891 tcg_temp_free(t1);
4892 #endif /* !defined(CONFIG_USER_ONLY) */
4894 #endif /* defined(TARGET_PPC64) */
4896 static void gen_mtmsr(DisasContext *ctx)
4898 CHK_SV(ctx);
4900 #if !defined(CONFIG_USER_ONLY)
4901 TCGv t0, t1;
4902 target_ulong mask = 0xFFFFFFFF;
4904 t0 = tcg_temp_new();
4905 t1 = tcg_temp_new();
4907 gen_icount_io_start(ctx);
4908 if (ctx->opcode & 0x00010000) {
4909 /* L=1 form only updates EE and RI */
4910 mask &= (1ULL << MSR_RI) | (1ULL << MSR_EE);
4911 } else {
4912 /* mtmsr does not alter S, ME, or LE */
4913 mask &= ~((1ULL << MSR_LE) | (1ULL << MSR_ME) | (1ULL << MSR_S));
4916 * XXX: we need to update nip before the store if we enter
4917 * power saving mode, we will exit the loop directly from
4918 * ppc_store_msr
4920 gen_update_nip(ctx, ctx->base.pc_next);
4923 tcg_gen_andi_tl(t0, cpu_gpr[rS(ctx->opcode)], mask);
4924 tcg_gen_andi_tl(t1, cpu_msr, ~mask);
4925 tcg_gen_or_tl(t0, t0, t1);
4927 gen_helper_store_msr(cpu_env, t0);
4929 /* Must stop the translation as machine state (may have) changed */
4930 ctx->base.is_jmp = DISAS_EXIT_UPDATE;
4932 tcg_temp_free(t0);
4933 tcg_temp_free(t1);
4934 #endif
4937 /* mtspr */
4938 static void gen_mtspr(DisasContext *ctx)
4940 void (*write_cb)(DisasContext *ctx, int sprn, int gprn);
4941 uint32_t sprn = SPR(ctx->opcode);
4943 #if defined(CONFIG_USER_ONLY)
4944 write_cb = ctx->spr_cb[sprn].uea_write;
4945 #else
4946 if (ctx->pr) {
4947 write_cb = ctx->spr_cb[sprn].uea_write;
4948 } else if (ctx->hv) {
4949 write_cb = ctx->spr_cb[sprn].hea_write;
4950 } else {
4951 write_cb = ctx->spr_cb[sprn].oea_write;
4953 #endif
4954 if (likely(write_cb != NULL)) {
4955 if (likely(write_cb != SPR_NOACCESS)) {
4956 (*write_cb)(ctx, sprn, rS(ctx->opcode));
4957 } else {
4958 /* Privilege exception */
4959 qemu_log_mask(LOG_GUEST_ERROR, "Trying to write privileged spr "
4960 "%d (0x%03x) at " TARGET_FMT_lx "\n", sprn, sprn,
4961 ctx->cia);
4962 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4964 } else {
4965 /* ISA 2.07 defines these as no-ops */
4966 if ((ctx->insns_flags2 & PPC2_ISA207S) &&
4967 (sprn >= 808 && sprn <= 811)) {
4968 /* This is a nop */
4969 return;
4972 /* Not defined */
4973 qemu_log_mask(LOG_GUEST_ERROR,
4974 "Trying to write invalid spr %d (0x%03x) at "
4975 TARGET_FMT_lx "\n", sprn, sprn, ctx->cia);
4979 * The behaviour depends on MSR:PR and SPR# bit 0x10, it can
4980 * generate a priv, a hv emu or a no-op
4982 if (sprn & 0x10) {
4983 if (ctx->pr) {
4984 gen_priv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4986 } else {
4987 if (ctx->pr || sprn == 0) {
4988 gen_hvpriv_exception(ctx, POWERPC_EXCP_PRIV_REG);
4994 #if defined(TARGET_PPC64)
4995 /* setb */
4996 static void gen_setb(DisasContext *ctx)
4998 TCGv_i32 t0 = tcg_temp_new_i32();
4999 TCGv_i32 t8 = tcg_constant_i32(8);
5000 TCGv_i32 tm1 = tcg_constant_i32(-1);
5001 int crf = crfS(ctx->opcode);
5003 tcg_gen_setcondi_i32(TCG_COND_GEU, t0, cpu_crf[crf], 4);
5004 tcg_gen_movcond_i32(TCG_COND_GEU, t0, cpu_crf[crf], t8, tm1, t0);
5005 tcg_gen_ext_i32_tl(cpu_gpr[rD(ctx->opcode)], t0);
5007 tcg_temp_free_i32(t0);
5009 #endif
5011 /*** Cache management ***/
5013 /* dcbf */
5014 static void gen_dcbf(DisasContext *ctx)
5016 /* XXX: specification says this is treated as a load by the MMU */
5017 TCGv t0;
5018 gen_set_access_type(ctx, ACCESS_CACHE);
5019 t0 = tcg_temp_new();
5020 gen_addr_reg_index(ctx, t0);
5021 gen_qemu_ld8u(ctx, t0, t0);
5022 tcg_temp_free(t0);
5025 /* dcbfep (external PID dcbf) */
5026 static void gen_dcbfep(DisasContext *ctx)
5028 /* XXX: specification says this is treated as a load by the MMU */
5029 TCGv t0;
5030 CHK_SV(ctx);
5031 gen_set_access_type(ctx, ACCESS_CACHE);
5032 t0 = tcg_temp_new();
5033 gen_addr_reg_index(ctx, t0);
5034 tcg_gen_qemu_ld_tl(t0, t0, PPC_TLB_EPID_LOAD, DEF_MEMOP(MO_UB));
5035 tcg_temp_free(t0);
5038 /* dcbi (Supervisor only) */
5039 static void gen_dcbi(DisasContext *ctx)
5041 #if defined(CONFIG_USER_ONLY)
5042 GEN_PRIV(ctx);
5043 #else
5044 TCGv EA, val;
5046 CHK_SV(ctx);
5047 EA = tcg_temp_new();
5048 gen_set_access_type(ctx, ACCESS_CACHE);
5049 gen_addr_reg_index(ctx, EA);
5050 val = tcg_temp_new();
5051 /* XXX: specification says this should be treated as a store by the MMU */
5052 gen_qemu_ld8u(ctx, val, EA);
5053 gen_qemu_st8(ctx, val, EA);
5054 tcg_temp_free(val);
5055 tcg_temp_free(EA);
5056 #endif /* defined(CONFIG_USER_ONLY) */
5059 /* dcdst */
5060 static void gen_dcbst(DisasContext *ctx)
5062 /* XXX: specification say this is treated as a load by the MMU */
5063 TCGv t0;
5064 gen_set_access_type(ctx, ACCESS_CACHE);
5065 t0 = tcg_temp_new();
5066 gen_addr_reg_index(ctx, t0);
5067 gen_qemu_ld8u(ctx, t0, t0);
5068 tcg_temp_free(t0);
5071 /* dcbstep (dcbstep External PID version) */
5072 static void gen_dcbstep(DisasContext *ctx)
5074 /* XXX: specification say this is treated as a load by the MMU */
5075 TCGv t0;
5076 gen_set_access_type(ctx, ACCESS_CACHE);
5077 t0 = tcg_temp_new();
5078 gen_addr_reg_index(ctx, t0);
5079 tcg_gen_qemu_ld_tl(t0, t0, PPC_TLB_EPID_LOAD, DEF_MEMOP(MO_UB));
5080 tcg_temp_free(t0);
5083 /* dcbt */
5084 static void gen_dcbt(DisasContext *ctx)
5087 * interpreted as no-op
5088 * XXX: specification say this is treated as a load by the MMU but
5089 * does not generate any exception
5093 /* dcbtep */
5094 static void gen_dcbtep(DisasContext *ctx)
5097 * interpreted as no-op
5098 * XXX: specification say this is treated as a load by the MMU but
5099 * does not generate any exception
5103 /* dcbtst */
5104 static void gen_dcbtst(DisasContext *ctx)
5107 * interpreted as no-op
5108 * XXX: specification say this is treated as a load by the MMU but
5109 * does not generate any exception
5113 /* dcbtstep */
5114 static void gen_dcbtstep(DisasContext *ctx)
5117 * interpreted as no-op
5118 * XXX: specification say this is treated as a load by the MMU but
5119 * does not generate any exception
5123 /* dcbtls */
5124 static void gen_dcbtls(DisasContext *ctx)
5126 /* Always fails locking the cache */
5127 TCGv t0 = tcg_temp_new();
5128 gen_load_spr(t0, SPR_Exxx_L1CSR0);
5129 tcg_gen_ori_tl(t0, t0, L1CSR0_CUL);
5130 gen_store_spr(SPR_Exxx_L1CSR0, t0);
5131 tcg_temp_free(t0);
5134 /* dcbz */
5135 static void gen_dcbz(DisasContext *ctx)
5137 TCGv tcgv_addr;
5138 TCGv_i32 tcgv_op;
5140 gen_set_access_type(ctx, ACCESS_CACHE);
5141 tcgv_addr = tcg_temp_new();
5142 tcgv_op = tcg_const_i32(ctx->opcode & 0x03FF000);
5143 gen_addr_reg_index(ctx, tcgv_addr);
5144 gen_helper_dcbz(cpu_env, tcgv_addr, tcgv_op);
5145 tcg_temp_free(tcgv_addr);
5146 tcg_temp_free_i32(tcgv_op);
5149 /* dcbzep */
5150 static void gen_dcbzep(DisasContext *ctx)
5152 TCGv tcgv_addr;
5153 TCGv_i32 tcgv_op;
5155 gen_set_access_type(ctx, ACCESS_CACHE);
5156 tcgv_addr = tcg_temp_new();
5157 tcgv_op = tcg_const_i32(ctx->opcode & 0x03FF000);
5158 gen_addr_reg_index(ctx, tcgv_addr);
5159 gen_helper_dcbzep(cpu_env, tcgv_addr, tcgv_op);
5160 tcg_temp_free(tcgv_addr);
5161 tcg_temp_free_i32(tcgv_op);
5164 /* dst / dstt */
5165 static void gen_dst(DisasContext *ctx)
5167 if (rA(ctx->opcode) == 0) {
5168 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5169 } else {
5170 /* interpreted as no-op */
5174 /* dstst /dststt */
5175 static void gen_dstst(DisasContext *ctx)
5177 if (rA(ctx->opcode) == 0) {
5178 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5179 } else {
5180 /* interpreted as no-op */
5185 /* dss / dssall */
5186 static void gen_dss(DisasContext *ctx)
5188 /* interpreted as no-op */
5191 /* icbi */
5192 static void gen_icbi(DisasContext *ctx)
5194 TCGv t0;
5195 gen_set_access_type(ctx, ACCESS_CACHE);
5196 t0 = tcg_temp_new();
5197 gen_addr_reg_index(ctx, t0);
5198 gen_helper_icbi(cpu_env, t0);
5199 tcg_temp_free(t0);
5202 /* icbiep */
5203 static void gen_icbiep(DisasContext *ctx)
5205 TCGv t0;
5206 gen_set_access_type(ctx, ACCESS_CACHE);
5207 t0 = tcg_temp_new();
5208 gen_addr_reg_index(ctx, t0);
5209 gen_helper_icbiep(cpu_env, t0);
5210 tcg_temp_free(t0);
5213 /* Optional: */
5214 /* dcba */
5215 static void gen_dcba(DisasContext *ctx)
5218 * interpreted as no-op
5219 * XXX: specification say this is treated as a store by the MMU
5220 * but does not generate any exception
5224 /*** Segment register manipulation ***/
5225 /* Supervisor only: */
5227 /* mfsr */
5228 static void gen_mfsr(DisasContext *ctx)
5230 #if defined(CONFIG_USER_ONLY)
5231 GEN_PRIV(ctx);
5232 #else
5233 TCGv t0;
5235 CHK_SV(ctx);
5236 t0 = tcg_const_tl(SR(ctx->opcode));
5237 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5238 tcg_temp_free(t0);
5239 #endif /* defined(CONFIG_USER_ONLY) */
5242 /* mfsrin */
5243 static void gen_mfsrin(DisasContext *ctx)
5245 #if defined(CONFIG_USER_ONLY)
5246 GEN_PRIV(ctx);
5247 #else
5248 TCGv t0;
5250 CHK_SV(ctx);
5251 t0 = tcg_temp_new();
5252 tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
5253 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5254 tcg_temp_free(t0);
5255 #endif /* defined(CONFIG_USER_ONLY) */
5258 /* mtsr */
5259 static void gen_mtsr(DisasContext *ctx)
5261 #if defined(CONFIG_USER_ONLY)
5262 GEN_PRIV(ctx);
5263 #else
5264 TCGv t0;
5266 CHK_SV(ctx);
5267 t0 = tcg_const_tl(SR(ctx->opcode));
5268 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
5269 tcg_temp_free(t0);
5270 #endif /* defined(CONFIG_USER_ONLY) */
5273 /* mtsrin */
5274 static void gen_mtsrin(DisasContext *ctx)
5276 #if defined(CONFIG_USER_ONLY)
5277 GEN_PRIV(ctx);
5278 #else
5279 TCGv t0;
5280 CHK_SV(ctx);
5282 t0 = tcg_temp_new();
5283 tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
5284 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rD(ctx->opcode)]);
5285 tcg_temp_free(t0);
5286 #endif /* defined(CONFIG_USER_ONLY) */
5289 #if defined(TARGET_PPC64)
5290 /* Specific implementation for PowerPC 64 "bridge" emulation using SLB */
5292 /* mfsr */
5293 static void gen_mfsr_64b(DisasContext *ctx)
5295 #if defined(CONFIG_USER_ONLY)
5296 GEN_PRIV(ctx);
5297 #else
5298 TCGv t0;
5300 CHK_SV(ctx);
5301 t0 = tcg_const_tl(SR(ctx->opcode));
5302 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5303 tcg_temp_free(t0);
5304 #endif /* defined(CONFIG_USER_ONLY) */
5307 /* mfsrin */
5308 static void gen_mfsrin_64b(DisasContext *ctx)
5310 #if defined(CONFIG_USER_ONLY)
5311 GEN_PRIV(ctx);
5312 #else
5313 TCGv t0;
5315 CHK_SV(ctx);
5316 t0 = tcg_temp_new();
5317 tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
5318 gen_helper_load_sr(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5319 tcg_temp_free(t0);
5320 #endif /* defined(CONFIG_USER_ONLY) */
5323 /* mtsr */
5324 static void gen_mtsr_64b(DisasContext *ctx)
5326 #if defined(CONFIG_USER_ONLY)
5327 GEN_PRIV(ctx);
5328 #else
5329 TCGv t0;
5331 CHK_SV(ctx);
5332 t0 = tcg_const_tl(SR(ctx->opcode));
5333 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
5334 tcg_temp_free(t0);
5335 #endif /* defined(CONFIG_USER_ONLY) */
5338 /* mtsrin */
5339 static void gen_mtsrin_64b(DisasContext *ctx)
5341 #if defined(CONFIG_USER_ONLY)
5342 GEN_PRIV(ctx);
5343 #else
5344 TCGv t0;
5346 CHK_SV(ctx);
5347 t0 = tcg_temp_new();
5348 tcg_gen_extract_tl(t0, cpu_gpr[rB(ctx->opcode)], 28, 4);
5349 gen_helper_store_sr(cpu_env, t0, cpu_gpr[rS(ctx->opcode)]);
5350 tcg_temp_free(t0);
5351 #endif /* defined(CONFIG_USER_ONLY) */
5354 /* slbmte */
5355 static void gen_slbmte(DisasContext *ctx)
5357 #if defined(CONFIG_USER_ONLY)
5358 GEN_PRIV(ctx);
5359 #else
5360 CHK_SV(ctx);
5362 gen_helper_store_slb(cpu_env, cpu_gpr[rB(ctx->opcode)],
5363 cpu_gpr[rS(ctx->opcode)]);
5364 #endif /* defined(CONFIG_USER_ONLY) */
5367 static void gen_slbmfee(DisasContext *ctx)
5369 #if defined(CONFIG_USER_ONLY)
5370 GEN_PRIV(ctx);
5371 #else
5372 CHK_SV(ctx);
5374 gen_helper_load_slb_esid(cpu_gpr[rS(ctx->opcode)], cpu_env,
5375 cpu_gpr[rB(ctx->opcode)]);
5376 #endif /* defined(CONFIG_USER_ONLY) */
5379 static void gen_slbmfev(DisasContext *ctx)
5381 #if defined(CONFIG_USER_ONLY)
5382 GEN_PRIV(ctx);
5383 #else
5384 CHK_SV(ctx);
5386 gen_helper_load_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
5387 cpu_gpr[rB(ctx->opcode)]);
5388 #endif /* defined(CONFIG_USER_ONLY) */
5391 static void gen_slbfee_(DisasContext *ctx)
5393 #if defined(CONFIG_USER_ONLY)
5394 gen_hvpriv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5395 #else
5396 TCGLabel *l1, *l2;
5398 if (unlikely(ctx->pr)) {
5399 gen_hvpriv_exception(ctx, POWERPC_EXCP_PRIV_OPC);
5400 return;
5402 gen_helper_find_slb_vsid(cpu_gpr[rS(ctx->opcode)], cpu_env,
5403 cpu_gpr[rB(ctx->opcode)]);
5404 l1 = gen_new_label();
5405 l2 = gen_new_label();
5406 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
5407 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rS(ctx->opcode)], -1, l1);
5408 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], CRF_EQ);
5409 tcg_gen_br(l2);
5410 gen_set_label(l1);
5411 tcg_gen_movi_tl(cpu_gpr[rS(ctx->opcode)], 0);
5412 gen_set_label(l2);
5413 #endif
5415 #endif /* defined(TARGET_PPC64) */
5417 /*** Lookaside buffer management ***/
5418 /* Optional & supervisor only: */
5420 /* tlbia */
5421 static void gen_tlbia(DisasContext *ctx)
5423 #if defined(CONFIG_USER_ONLY)
5424 GEN_PRIV(ctx);
5425 #else
5426 CHK_HV(ctx);
5428 gen_helper_tlbia(cpu_env);
5429 #endif /* defined(CONFIG_USER_ONLY) */
5432 /* tlbsync */
5433 static void gen_tlbsync(DisasContext *ctx)
5435 #if defined(CONFIG_USER_ONLY)
5436 GEN_PRIV(ctx);
5437 #else
5439 if (ctx->gtse) {
5440 CHK_SV(ctx); /* If gtse is set then tlbsync is supervisor privileged */
5441 } else {
5442 CHK_HV(ctx); /* Else hypervisor privileged */
5445 /* BookS does both ptesync and tlbsync make tlbsync a nop for server */
5446 if (ctx->insns_flags & PPC_BOOKE) {
5447 gen_check_tlb_flush(ctx, true);
5449 #endif /* defined(CONFIG_USER_ONLY) */
5452 #if defined(TARGET_PPC64)
5453 /* slbia */
5454 static void gen_slbia(DisasContext *ctx)
5456 #if defined(CONFIG_USER_ONLY)
5457 GEN_PRIV(ctx);
5458 #else
5459 uint32_t ih = (ctx->opcode >> 21) & 0x7;
5460 TCGv_i32 t0 = tcg_const_i32(ih);
5462 CHK_SV(ctx);
5464 gen_helper_slbia(cpu_env, t0);
5465 tcg_temp_free_i32(t0);
5466 #endif /* defined(CONFIG_USER_ONLY) */
5469 /* slbieg */
5470 static void gen_slbieg(DisasContext *ctx)
5472 #if defined(CONFIG_USER_ONLY)
5473 GEN_PRIV(ctx);
5474 #else
5475 CHK_SV(ctx);
5477 gen_helper_slbieg(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5478 #endif /* defined(CONFIG_USER_ONLY) */
5481 /* slbsync */
5482 static void gen_slbsync(DisasContext *ctx)
5484 #if defined(CONFIG_USER_ONLY)
5485 GEN_PRIV(ctx);
5486 #else
5487 CHK_SV(ctx);
5488 gen_check_tlb_flush(ctx, true);
5489 #endif /* defined(CONFIG_USER_ONLY) */
5492 #endif /* defined(TARGET_PPC64) */
5494 /*** External control ***/
5495 /* Optional: */
5497 /* eciwx */
5498 static void gen_eciwx(DisasContext *ctx)
5500 TCGv t0;
5501 /* Should check EAR[E] ! */
5502 gen_set_access_type(ctx, ACCESS_EXT);
5503 t0 = tcg_temp_new();
5504 gen_addr_reg_index(ctx, t0);
5505 tcg_gen_qemu_ld_tl(cpu_gpr[rD(ctx->opcode)], t0, ctx->mem_idx,
5506 DEF_MEMOP(MO_UL | MO_ALIGN));
5507 tcg_temp_free(t0);
5510 /* ecowx */
5511 static void gen_ecowx(DisasContext *ctx)
5513 TCGv t0;
5514 /* Should check EAR[E] ! */
5515 gen_set_access_type(ctx, ACCESS_EXT);
5516 t0 = tcg_temp_new();
5517 gen_addr_reg_index(ctx, t0);
5518 tcg_gen_qemu_st_tl(cpu_gpr[rD(ctx->opcode)], t0, ctx->mem_idx,
5519 DEF_MEMOP(MO_UL | MO_ALIGN));
5520 tcg_temp_free(t0);
5523 /* 602 - 603 - G2 TLB management */
5525 /* tlbld */
5526 static void gen_tlbld_6xx(DisasContext *ctx)
5528 #if defined(CONFIG_USER_ONLY)
5529 GEN_PRIV(ctx);
5530 #else
5531 CHK_SV(ctx);
5532 gen_helper_6xx_tlbd(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5533 #endif /* defined(CONFIG_USER_ONLY) */
5536 /* tlbli */
5537 static void gen_tlbli_6xx(DisasContext *ctx)
5539 #if defined(CONFIG_USER_ONLY)
5540 GEN_PRIV(ctx);
5541 #else
5542 CHK_SV(ctx);
5543 gen_helper_6xx_tlbi(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5544 #endif /* defined(CONFIG_USER_ONLY) */
5547 /* BookE specific instructions */
5549 /* XXX: not implemented on 440 ? */
5550 static void gen_mfapidi(DisasContext *ctx)
5552 /* XXX: TODO */
5553 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5556 /* XXX: not implemented on 440 ? */
5557 static void gen_tlbiva(DisasContext *ctx)
5559 #if defined(CONFIG_USER_ONLY)
5560 GEN_PRIV(ctx);
5561 #else
5562 TCGv t0;
5564 CHK_SV(ctx);
5565 t0 = tcg_temp_new();
5566 gen_addr_reg_index(ctx, t0);
5567 gen_helper_tlbiva(cpu_env, cpu_gpr[rB(ctx->opcode)]);
5568 tcg_temp_free(t0);
5569 #endif /* defined(CONFIG_USER_ONLY) */
5572 /* All 405 MAC instructions are translated here */
5573 static inline void gen_405_mulladd_insn(DisasContext *ctx, int opc2, int opc3,
5574 int ra, int rb, int rt, int Rc)
5576 TCGv t0, t1;
5578 t0 = tcg_temp_local_new();
5579 t1 = tcg_temp_local_new();
5581 switch (opc3 & 0x0D) {
5582 case 0x05:
5583 /* macchw - macchw. - macchwo - macchwo. */
5584 /* macchws - macchws. - macchwso - macchwso. */
5585 /* nmacchw - nmacchw. - nmacchwo - nmacchwo. */
5586 /* nmacchws - nmacchws. - nmacchwso - nmacchwso. */
5587 /* mulchw - mulchw. */
5588 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5589 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5590 tcg_gen_ext16s_tl(t1, t1);
5591 break;
5592 case 0x04:
5593 /* macchwu - macchwu. - macchwuo - macchwuo. */
5594 /* macchwsu - macchwsu. - macchwsuo - macchwsuo. */
5595 /* mulchwu - mulchwu. */
5596 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5597 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5598 tcg_gen_ext16u_tl(t1, t1);
5599 break;
5600 case 0x01:
5601 /* machhw - machhw. - machhwo - machhwo. */
5602 /* machhws - machhws. - machhwso - machhwso. */
5603 /* nmachhw - nmachhw. - nmachhwo - nmachhwo. */
5604 /* nmachhws - nmachhws. - nmachhwso - nmachhwso. */
5605 /* mulhhw - mulhhw. */
5606 tcg_gen_sari_tl(t0, cpu_gpr[ra], 16);
5607 tcg_gen_ext16s_tl(t0, t0);
5608 tcg_gen_sari_tl(t1, cpu_gpr[rb], 16);
5609 tcg_gen_ext16s_tl(t1, t1);
5610 break;
5611 case 0x00:
5612 /* machhwu - machhwu. - machhwuo - machhwuo. */
5613 /* machhwsu - machhwsu. - machhwsuo - machhwsuo. */
5614 /* mulhhwu - mulhhwu. */
5615 tcg_gen_shri_tl(t0, cpu_gpr[ra], 16);
5616 tcg_gen_ext16u_tl(t0, t0);
5617 tcg_gen_shri_tl(t1, cpu_gpr[rb], 16);
5618 tcg_gen_ext16u_tl(t1, t1);
5619 break;
5620 case 0x0D:
5621 /* maclhw - maclhw. - maclhwo - maclhwo. */
5622 /* maclhws - maclhws. - maclhwso - maclhwso. */
5623 /* nmaclhw - nmaclhw. - nmaclhwo - nmaclhwo. */
5624 /* nmaclhws - nmaclhws. - nmaclhwso - nmaclhwso. */
5625 /* mullhw - mullhw. */
5626 tcg_gen_ext16s_tl(t0, cpu_gpr[ra]);
5627 tcg_gen_ext16s_tl(t1, cpu_gpr[rb]);
5628 break;
5629 case 0x0C:
5630 /* maclhwu - maclhwu. - maclhwuo - maclhwuo. */
5631 /* maclhwsu - maclhwsu. - maclhwsuo - maclhwsuo. */
5632 /* mullhwu - mullhwu. */
5633 tcg_gen_ext16u_tl(t0, cpu_gpr[ra]);
5634 tcg_gen_ext16u_tl(t1, cpu_gpr[rb]);
5635 break;
5637 if (opc2 & 0x04) {
5638 /* (n)multiply-and-accumulate (0x0C / 0x0E) */
5639 tcg_gen_mul_tl(t1, t0, t1);
5640 if (opc2 & 0x02) {
5641 /* nmultiply-and-accumulate (0x0E) */
5642 tcg_gen_sub_tl(t0, cpu_gpr[rt], t1);
5643 } else {
5644 /* multiply-and-accumulate (0x0C) */
5645 tcg_gen_add_tl(t0, cpu_gpr[rt], t1);
5648 if (opc3 & 0x12) {
5649 /* Check overflow and/or saturate */
5650 TCGLabel *l1 = gen_new_label();
5652 if (opc3 & 0x10) {
5653 /* Start with XER OV disabled, the most likely case */
5654 tcg_gen_movi_tl(cpu_ov, 0);
5656 if (opc3 & 0x01) {
5657 /* Signed */
5658 tcg_gen_xor_tl(t1, cpu_gpr[rt], t1);
5659 tcg_gen_brcondi_tl(TCG_COND_GE, t1, 0, l1);
5660 tcg_gen_xor_tl(t1, cpu_gpr[rt], t0);
5661 tcg_gen_brcondi_tl(TCG_COND_LT, t1, 0, l1);
5662 if (opc3 & 0x02) {
5663 /* Saturate */
5664 tcg_gen_sari_tl(t0, cpu_gpr[rt], 31);
5665 tcg_gen_xori_tl(t0, t0, 0x7fffffff);
5667 } else {
5668 /* Unsigned */
5669 tcg_gen_brcond_tl(TCG_COND_GEU, t0, t1, l1);
5670 if (opc3 & 0x02) {
5671 /* Saturate */
5672 tcg_gen_movi_tl(t0, UINT32_MAX);
5675 if (opc3 & 0x10) {
5676 /* Check overflow */
5677 tcg_gen_movi_tl(cpu_ov, 1);
5678 tcg_gen_movi_tl(cpu_so, 1);
5680 gen_set_label(l1);
5681 tcg_gen_mov_tl(cpu_gpr[rt], t0);
5683 } else {
5684 tcg_gen_mul_tl(cpu_gpr[rt], t0, t1);
5686 tcg_temp_free(t0);
5687 tcg_temp_free(t1);
5688 if (unlikely(Rc) != 0) {
5689 /* Update Rc0 */
5690 gen_set_Rc0(ctx, cpu_gpr[rt]);
5694 #define GEN_MAC_HANDLER(name, opc2, opc3) \
5695 static void glue(gen_, name)(DisasContext *ctx) \
5697 gen_405_mulladd_insn(ctx, opc2, opc3, rA(ctx->opcode), rB(ctx->opcode), \
5698 rD(ctx->opcode), Rc(ctx->opcode)); \
5701 /* macchw - macchw. */
5702 GEN_MAC_HANDLER(macchw, 0x0C, 0x05);
5703 /* macchwo - macchwo. */
5704 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15);
5705 /* macchws - macchws. */
5706 GEN_MAC_HANDLER(macchws, 0x0C, 0x07);
5707 /* macchwso - macchwso. */
5708 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17);
5709 /* macchwsu - macchwsu. */
5710 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06);
5711 /* macchwsuo - macchwsuo. */
5712 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16);
5713 /* macchwu - macchwu. */
5714 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04);
5715 /* macchwuo - macchwuo. */
5716 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14);
5717 /* machhw - machhw. */
5718 GEN_MAC_HANDLER(machhw, 0x0C, 0x01);
5719 /* machhwo - machhwo. */
5720 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11);
5721 /* machhws - machhws. */
5722 GEN_MAC_HANDLER(machhws, 0x0C, 0x03);
5723 /* machhwso - machhwso. */
5724 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13);
5725 /* machhwsu - machhwsu. */
5726 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02);
5727 /* machhwsuo - machhwsuo. */
5728 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12);
5729 /* machhwu - machhwu. */
5730 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00);
5731 /* machhwuo - machhwuo. */
5732 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10);
5733 /* maclhw - maclhw. */
5734 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D);
5735 /* maclhwo - maclhwo. */
5736 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D);
5737 /* maclhws - maclhws. */
5738 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F);
5739 /* maclhwso - maclhwso. */
5740 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F);
5741 /* maclhwu - maclhwu. */
5742 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C);
5743 /* maclhwuo - maclhwuo. */
5744 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C);
5745 /* maclhwsu - maclhwsu. */
5746 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E);
5747 /* maclhwsuo - maclhwsuo. */
5748 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E);
5749 /* nmacchw - nmacchw. */
5750 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05);
5751 /* nmacchwo - nmacchwo. */
5752 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15);
5753 /* nmacchws - nmacchws. */
5754 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07);
5755 /* nmacchwso - nmacchwso. */
5756 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17);
5757 /* nmachhw - nmachhw. */
5758 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01);
5759 /* nmachhwo - nmachhwo. */
5760 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11);
5761 /* nmachhws - nmachhws. */
5762 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03);
5763 /* nmachhwso - nmachhwso. */
5764 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13);
5765 /* nmaclhw - nmaclhw. */
5766 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D);
5767 /* nmaclhwo - nmaclhwo. */
5768 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D);
5769 /* nmaclhws - nmaclhws. */
5770 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F);
5771 /* nmaclhwso - nmaclhwso. */
5772 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F);
5774 /* mulchw - mulchw. */
5775 GEN_MAC_HANDLER(mulchw, 0x08, 0x05);
5776 /* mulchwu - mulchwu. */
5777 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04);
5778 /* mulhhw - mulhhw. */
5779 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01);
5780 /* mulhhwu - mulhhwu. */
5781 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00);
5782 /* mullhw - mullhw. */
5783 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D);
5784 /* mullhwu - mullhwu. */
5785 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C);
5787 /* mfdcr */
5788 static void gen_mfdcr(DisasContext *ctx)
5790 #if defined(CONFIG_USER_ONLY)
5791 GEN_PRIV(ctx);
5792 #else
5793 TCGv dcrn;
5795 CHK_SV(ctx);
5796 dcrn = tcg_const_tl(SPR(ctx->opcode));
5797 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env, dcrn);
5798 tcg_temp_free(dcrn);
5799 #endif /* defined(CONFIG_USER_ONLY) */
5802 /* mtdcr */
5803 static void gen_mtdcr(DisasContext *ctx)
5805 #if defined(CONFIG_USER_ONLY)
5806 GEN_PRIV(ctx);
5807 #else
5808 TCGv dcrn;
5810 CHK_SV(ctx);
5811 dcrn = tcg_const_tl(SPR(ctx->opcode));
5812 gen_helper_store_dcr(cpu_env, dcrn, cpu_gpr[rS(ctx->opcode)]);
5813 tcg_temp_free(dcrn);
5814 #endif /* defined(CONFIG_USER_ONLY) */
5817 /* mfdcrx */
5818 /* XXX: not implemented on 440 ? */
5819 static void gen_mfdcrx(DisasContext *ctx)
5821 #if defined(CONFIG_USER_ONLY)
5822 GEN_PRIV(ctx);
5823 #else
5824 CHK_SV(ctx);
5825 gen_helper_load_dcr(cpu_gpr[rD(ctx->opcode)], cpu_env,
5826 cpu_gpr[rA(ctx->opcode)]);
5827 /* Note: Rc update flag set leads to undefined state of Rc0 */
5828 #endif /* defined(CONFIG_USER_ONLY) */
5831 /* mtdcrx */
5832 /* XXX: not implemented on 440 ? */
5833 static void gen_mtdcrx(DisasContext *ctx)
5835 #if defined(CONFIG_USER_ONLY)
5836 GEN_PRIV(ctx);
5837 #else
5838 CHK_SV(ctx);
5839 gen_helper_store_dcr(cpu_env, cpu_gpr[rA(ctx->opcode)],
5840 cpu_gpr[rS(ctx->opcode)]);
5841 /* Note: Rc update flag set leads to undefined state of Rc0 */
5842 #endif /* defined(CONFIG_USER_ONLY) */
5845 /* dccci */
5846 static void gen_dccci(DisasContext *ctx)
5848 CHK_SV(ctx);
5849 /* interpreted as no-op */
5852 /* dcread */
5853 static void gen_dcread(DisasContext *ctx)
5855 #if defined(CONFIG_USER_ONLY)
5856 GEN_PRIV(ctx);
5857 #else
5858 TCGv EA, val;
5860 CHK_SV(ctx);
5861 gen_set_access_type(ctx, ACCESS_CACHE);
5862 EA = tcg_temp_new();
5863 gen_addr_reg_index(ctx, EA);
5864 val = tcg_temp_new();
5865 gen_qemu_ld32u(ctx, val, EA);
5866 tcg_temp_free(val);
5867 tcg_gen_mov_tl(cpu_gpr[rD(ctx->opcode)], EA);
5868 tcg_temp_free(EA);
5869 #endif /* defined(CONFIG_USER_ONLY) */
5872 /* icbt */
5873 static void gen_icbt_40x(DisasContext *ctx)
5876 * interpreted as no-op
5877 * XXX: specification say this is treated as a load by the MMU but
5878 * does not generate any exception
5882 /* iccci */
5883 static void gen_iccci(DisasContext *ctx)
5885 CHK_SV(ctx);
5886 /* interpreted as no-op */
5889 /* icread */
5890 static void gen_icread(DisasContext *ctx)
5892 CHK_SV(ctx);
5893 /* interpreted as no-op */
5896 /* rfci (supervisor only) */
5897 static void gen_rfci_40x(DisasContext *ctx)
5899 #if defined(CONFIG_USER_ONLY)
5900 GEN_PRIV(ctx);
5901 #else
5902 CHK_SV(ctx);
5903 /* Restore CPU state */
5904 gen_helper_40x_rfci(cpu_env);
5905 ctx->base.is_jmp = DISAS_EXIT;
5906 #endif /* defined(CONFIG_USER_ONLY) */
5909 static void gen_rfci(DisasContext *ctx)
5911 #if defined(CONFIG_USER_ONLY)
5912 GEN_PRIV(ctx);
5913 #else
5914 CHK_SV(ctx);
5915 /* Restore CPU state */
5916 gen_helper_rfci(cpu_env);
5917 ctx->base.is_jmp = DISAS_EXIT;
5918 #endif /* defined(CONFIG_USER_ONLY) */
5921 /* BookE specific */
5923 /* XXX: not implemented on 440 ? */
5924 static void gen_rfdi(DisasContext *ctx)
5926 #if defined(CONFIG_USER_ONLY)
5927 GEN_PRIV(ctx);
5928 #else
5929 CHK_SV(ctx);
5930 /* Restore CPU state */
5931 gen_helper_rfdi(cpu_env);
5932 ctx->base.is_jmp = DISAS_EXIT;
5933 #endif /* defined(CONFIG_USER_ONLY) */
5936 /* XXX: not implemented on 440 ? */
5937 static void gen_rfmci(DisasContext *ctx)
5939 #if defined(CONFIG_USER_ONLY)
5940 GEN_PRIV(ctx);
5941 #else
5942 CHK_SV(ctx);
5943 /* Restore CPU state */
5944 gen_helper_rfmci(cpu_env);
5945 ctx->base.is_jmp = DISAS_EXIT;
5946 #endif /* defined(CONFIG_USER_ONLY) */
5949 /* TLB management - PowerPC 405 implementation */
5951 /* tlbre */
5952 static void gen_tlbre_40x(DisasContext *ctx)
5954 #if defined(CONFIG_USER_ONLY)
5955 GEN_PRIV(ctx);
5956 #else
5957 CHK_SV(ctx);
5958 switch (rB(ctx->opcode)) {
5959 case 0:
5960 gen_helper_4xx_tlbre_hi(cpu_gpr[rD(ctx->opcode)], cpu_env,
5961 cpu_gpr[rA(ctx->opcode)]);
5962 break;
5963 case 1:
5964 gen_helper_4xx_tlbre_lo(cpu_gpr[rD(ctx->opcode)], cpu_env,
5965 cpu_gpr[rA(ctx->opcode)]);
5966 break;
5967 default:
5968 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
5969 break;
5971 #endif /* defined(CONFIG_USER_ONLY) */
5974 /* tlbsx - tlbsx. */
5975 static void gen_tlbsx_40x(DisasContext *ctx)
5977 #if defined(CONFIG_USER_ONLY)
5978 GEN_PRIV(ctx);
5979 #else
5980 TCGv t0;
5982 CHK_SV(ctx);
5983 t0 = tcg_temp_new();
5984 gen_addr_reg_index(ctx, t0);
5985 gen_helper_4xx_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
5986 tcg_temp_free(t0);
5987 if (Rc(ctx->opcode)) {
5988 TCGLabel *l1 = gen_new_label();
5989 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
5990 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
5991 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
5992 gen_set_label(l1);
5994 #endif /* defined(CONFIG_USER_ONLY) */
5997 /* tlbwe */
5998 static void gen_tlbwe_40x(DisasContext *ctx)
6000 #if defined(CONFIG_USER_ONLY)
6001 GEN_PRIV(ctx);
6002 #else
6003 CHK_SV(ctx);
6005 switch (rB(ctx->opcode)) {
6006 case 0:
6007 gen_helper_4xx_tlbwe_hi(cpu_env, cpu_gpr[rA(ctx->opcode)],
6008 cpu_gpr[rS(ctx->opcode)]);
6009 break;
6010 case 1:
6011 gen_helper_4xx_tlbwe_lo(cpu_env, cpu_gpr[rA(ctx->opcode)],
6012 cpu_gpr[rS(ctx->opcode)]);
6013 break;
6014 default:
6015 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6016 break;
6018 #endif /* defined(CONFIG_USER_ONLY) */
6021 /* TLB management - PowerPC 440 implementation */
6023 /* tlbre */
6024 static void gen_tlbre_440(DisasContext *ctx)
6026 #if defined(CONFIG_USER_ONLY)
6027 GEN_PRIV(ctx);
6028 #else
6029 CHK_SV(ctx);
6031 switch (rB(ctx->opcode)) {
6032 case 0:
6033 case 1:
6034 case 2:
6036 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6037 gen_helper_440_tlbre(cpu_gpr[rD(ctx->opcode)], cpu_env,
6038 t0, cpu_gpr[rA(ctx->opcode)]);
6039 tcg_temp_free_i32(t0);
6041 break;
6042 default:
6043 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6044 break;
6046 #endif /* defined(CONFIG_USER_ONLY) */
6049 /* tlbsx - tlbsx. */
6050 static void gen_tlbsx_440(DisasContext *ctx)
6052 #if defined(CONFIG_USER_ONLY)
6053 GEN_PRIV(ctx);
6054 #else
6055 TCGv t0;
6057 CHK_SV(ctx);
6058 t0 = tcg_temp_new();
6059 gen_addr_reg_index(ctx, t0);
6060 gen_helper_440_tlbsx(cpu_gpr[rD(ctx->opcode)], cpu_env, t0);
6061 tcg_temp_free(t0);
6062 if (Rc(ctx->opcode)) {
6063 TCGLabel *l1 = gen_new_label();
6064 tcg_gen_trunc_tl_i32(cpu_crf[0], cpu_so);
6065 tcg_gen_brcondi_tl(TCG_COND_EQ, cpu_gpr[rD(ctx->opcode)], -1, l1);
6066 tcg_gen_ori_i32(cpu_crf[0], cpu_crf[0], 0x02);
6067 gen_set_label(l1);
6069 #endif /* defined(CONFIG_USER_ONLY) */
6072 /* tlbwe */
6073 static void gen_tlbwe_440(DisasContext *ctx)
6075 #if defined(CONFIG_USER_ONLY)
6076 GEN_PRIV(ctx);
6077 #else
6078 CHK_SV(ctx);
6079 switch (rB(ctx->opcode)) {
6080 case 0:
6081 case 1:
6082 case 2:
6084 TCGv_i32 t0 = tcg_const_i32(rB(ctx->opcode));
6085 gen_helper_440_tlbwe(cpu_env, t0, cpu_gpr[rA(ctx->opcode)],
6086 cpu_gpr[rS(ctx->opcode)]);
6087 tcg_temp_free_i32(t0);
6089 break;
6090 default:
6091 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6092 break;
6094 #endif /* defined(CONFIG_USER_ONLY) */
6097 /* TLB management - PowerPC BookE 2.06 implementation */
6099 /* tlbre */
6100 static void gen_tlbre_booke206(DisasContext *ctx)
6102 #if defined(CONFIG_USER_ONLY)
6103 GEN_PRIV(ctx);
6104 #else
6105 CHK_SV(ctx);
6106 gen_helper_booke206_tlbre(cpu_env);
6107 #endif /* defined(CONFIG_USER_ONLY) */
6110 /* tlbsx - tlbsx. */
6111 static void gen_tlbsx_booke206(DisasContext *ctx)
6113 #if defined(CONFIG_USER_ONLY)
6114 GEN_PRIV(ctx);
6115 #else
6116 TCGv t0;
6118 CHK_SV(ctx);
6119 if (rA(ctx->opcode)) {
6120 t0 = tcg_temp_new();
6121 tcg_gen_mov_tl(t0, cpu_gpr[rD(ctx->opcode)]);
6122 } else {
6123 t0 = tcg_const_tl(0);
6126 tcg_gen_add_tl(t0, t0, cpu_gpr[rB(ctx->opcode)]);
6127 gen_helper_booke206_tlbsx(cpu_env, t0);
6128 tcg_temp_free(t0);
6129 #endif /* defined(CONFIG_USER_ONLY) */
6132 /* tlbwe */
6133 static void gen_tlbwe_booke206(DisasContext *ctx)
6135 #if defined(CONFIG_USER_ONLY)
6136 GEN_PRIV(ctx);
6137 #else
6138 CHK_SV(ctx);
6139 gen_helper_booke206_tlbwe(cpu_env);
6140 #endif /* defined(CONFIG_USER_ONLY) */
6143 static void gen_tlbivax_booke206(DisasContext *ctx)
6145 #if defined(CONFIG_USER_ONLY)
6146 GEN_PRIV(ctx);
6147 #else
6148 TCGv t0;
6150 CHK_SV(ctx);
6151 t0 = tcg_temp_new();
6152 gen_addr_reg_index(ctx, t0);
6153 gen_helper_booke206_tlbivax(cpu_env, t0);
6154 tcg_temp_free(t0);
6155 #endif /* defined(CONFIG_USER_ONLY) */
6158 static void gen_tlbilx_booke206(DisasContext *ctx)
6160 #if defined(CONFIG_USER_ONLY)
6161 GEN_PRIV(ctx);
6162 #else
6163 TCGv t0;
6165 CHK_SV(ctx);
6166 t0 = tcg_temp_new();
6167 gen_addr_reg_index(ctx, t0);
6169 switch ((ctx->opcode >> 21) & 0x3) {
6170 case 0:
6171 gen_helper_booke206_tlbilx0(cpu_env, t0);
6172 break;
6173 case 1:
6174 gen_helper_booke206_tlbilx1(cpu_env, t0);
6175 break;
6176 case 3:
6177 gen_helper_booke206_tlbilx3(cpu_env, t0);
6178 break;
6179 default:
6180 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6181 break;
6184 tcg_temp_free(t0);
6185 #endif /* defined(CONFIG_USER_ONLY) */
6189 /* wrtee */
6190 static void gen_wrtee(DisasContext *ctx)
6192 #if defined(CONFIG_USER_ONLY)
6193 GEN_PRIV(ctx);
6194 #else
6195 TCGv t0;
6197 CHK_SV(ctx);
6198 t0 = tcg_temp_new();
6199 tcg_gen_andi_tl(t0, cpu_gpr[rD(ctx->opcode)], (1 << MSR_EE));
6200 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6201 tcg_gen_or_tl(cpu_msr, cpu_msr, t0);
6202 tcg_temp_free(t0);
6204 * Stop translation to have a chance to raise an exception if we
6205 * just set msr_ee to 1
6207 ctx->base.is_jmp = DISAS_EXIT_UPDATE;
6208 #endif /* defined(CONFIG_USER_ONLY) */
6211 /* wrteei */
6212 static void gen_wrteei(DisasContext *ctx)
6214 #if defined(CONFIG_USER_ONLY)
6215 GEN_PRIV(ctx);
6216 #else
6217 CHK_SV(ctx);
6218 if (ctx->opcode & 0x00008000) {
6219 tcg_gen_ori_tl(cpu_msr, cpu_msr, (1 << MSR_EE));
6220 /* Stop translation to have a chance to raise an exception */
6221 ctx->base.is_jmp = DISAS_EXIT_UPDATE;
6222 } else {
6223 tcg_gen_andi_tl(cpu_msr, cpu_msr, ~(1 << MSR_EE));
6225 #endif /* defined(CONFIG_USER_ONLY) */
6228 /* PowerPC 440 specific instructions */
6230 /* dlmzb */
6231 static void gen_dlmzb(DisasContext *ctx)
6233 TCGv_i32 t0 = tcg_const_i32(Rc(ctx->opcode));
6234 gen_helper_dlmzb(cpu_gpr[rA(ctx->opcode)], cpu_env,
6235 cpu_gpr[rS(ctx->opcode)], cpu_gpr[rB(ctx->opcode)], t0);
6236 tcg_temp_free_i32(t0);
6239 /* mbar replaces eieio on 440 */
6240 static void gen_mbar(DisasContext *ctx)
6242 /* interpreted as no-op */
6245 /* msync replaces sync on 440 */
6246 static void gen_msync_4xx(DisasContext *ctx)
6248 /* Only e500 seems to treat reserved bits as invalid */
6249 if ((ctx->insns_flags2 & PPC2_BOOKE206) &&
6250 (ctx->opcode & 0x03FFF801)) {
6251 gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
6253 /* otherwise interpreted as no-op */
6256 /* icbt */
6257 static void gen_icbt_440(DisasContext *ctx)
6260 * interpreted as no-op
6261 * XXX: specification say this is treated as a load by the MMU but
6262 * does not generate any exception
6266 /* Embedded.Processor Control */
6268 static void gen_msgclr(DisasContext *ctx)
6270 #if defined(CONFIG_USER_ONLY)
6271 GEN_PRIV(ctx);
6272 #else
6273 CHK_HV(ctx);
6274 if (is_book3s_arch2x(ctx)) {
6275 gen_helper_book3s_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6276 } else {
6277 gen_helper_msgclr(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6279 #endif /* defined(CONFIG_USER_ONLY) */
6282 static void gen_msgsnd(DisasContext *ctx)
6284 #if defined(CONFIG_USER_ONLY)
6285 GEN_PRIV(ctx);
6286 #else
6287 CHK_HV(ctx);
6288 if (is_book3s_arch2x(ctx)) {
6289 gen_helper_book3s_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6290 } else {
6291 gen_helper_msgsnd(cpu_gpr[rB(ctx->opcode)]);
6293 #endif /* defined(CONFIG_USER_ONLY) */
6296 #if defined(TARGET_PPC64)
6297 static void gen_msgclrp(DisasContext *ctx)
6299 #if defined(CONFIG_USER_ONLY)
6300 GEN_PRIV(ctx);
6301 #else
6302 CHK_SV(ctx);
6303 gen_helper_book3s_msgclrp(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6304 #endif /* defined(CONFIG_USER_ONLY) */
6307 static void gen_msgsndp(DisasContext *ctx)
6309 #if defined(CONFIG_USER_ONLY)
6310 GEN_PRIV(ctx);
6311 #else
6312 CHK_SV(ctx);
6313 gen_helper_book3s_msgsndp(cpu_env, cpu_gpr[rB(ctx->opcode)]);
6314 #endif /* defined(CONFIG_USER_ONLY) */
6316 #endif
6318 static void gen_msgsync(DisasContext *ctx)
6320 #if defined(CONFIG_USER_ONLY)
6321 GEN_PRIV(ctx);
6322 #else
6323 CHK_HV(ctx);
6324 #endif /* defined(CONFIG_USER_ONLY) */
6325 /* interpreted as no-op */
6328 #if defined(TARGET_PPC64)
6329 static void gen_maddld(DisasContext *ctx)
6331 TCGv_i64 t1 = tcg_temp_new_i64();
6333 tcg_gen_mul_i64(t1, cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)]);
6334 tcg_gen_add_i64(cpu_gpr[rD(ctx->opcode)], t1, cpu_gpr[rC(ctx->opcode)]);
6335 tcg_temp_free_i64(t1);
6338 /* maddhd maddhdu */
6339 static void gen_maddhd_maddhdu(DisasContext *ctx)
6341 TCGv_i64 lo = tcg_temp_new_i64();
6342 TCGv_i64 hi = tcg_temp_new_i64();
6343 TCGv_i64 t1 = tcg_temp_new_i64();
6345 if (Rc(ctx->opcode)) {
6346 tcg_gen_mulu2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6347 cpu_gpr[rB(ctx->opcode)]);
6348 tcg_gen_movi_i64(t1, 0);
6349 } else {
6350 tcg_gen_muls2_i64(lo, hi, cpu_gpr[rA(ctx->opcode)],
6351 cpu_gpr[rB(ctx->opcode)]);
6352 tcg_gen_sari_i64(t1, cpu_gpr[rC(ctx->opcode)], 63);
6354 tcg_gen_add2_i64(t1, cpu_gpr[rD(ctx->opcode)], lo, hi,
6355 cpu_gpr[rC(ctx->opcode)], t1);
6356 tcg_temp_free_i64(lo);
6357 tcg_temp_free_i64(hi);
6358 tcg_temp_free_i64(t1);
6360 #endif /* defined(TARGET_PPC64) */
6362 static void gen_tbegin(DisasContext *ctx)
6364 if (unlikely(!ctx->tm_enabled)) {
6365 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6366 return;
6368 gen_helper_tbegin(cpu_env);
6371 #define GEN_TM_NOOP(name) \
6372 static inline void gen_##name(DisasContext *ctx) \
6374 if (unlikely(!ctx->tm_enabled)) { \
6375 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM); \
6376 return; \
6378 /* \
6379 * Because tbegin always fails in QEMU, these user \
6380 * space instructions all have a simple implementation: \
6382 * CR[0] = 0b0 || MSR[TS] || 0b0 \
6383 * = 0b0 || 0b00 || 0b0 \
6384 */ \
6385 tcg_gen_movi_i32(cpu_crf[0], 0); \
6388 GEN_TM_NOOP(tend);
6389 GEN_TM_NOOP(tabort);
6390 GEN_TM_NOOP(tabortwc);
6391 GEN_TM_NOOP(tabortwci);
6392 GEN_TM_NOOP(tabortdc);
6393 GEN_TM_NOOP(tabortdci);
6394 GEN_TM_NOOP(tsr);
6396 static inline void gen_cp_abort(DisasContext *ctx)
6398 /* Do Nothing */
6401 #define GEN_CP_PASTE_NOOP(name) \
6402 static inline void gen_##name(DisasContext *ctx) \
6404 /* \
6405 * Generate invalid exception until we have an \
6406 * implementation of the copy paste facility \
6407 */ \
6408 gen_invalid(ctx); \
6411 GEN_CP_PASTE_NOOP(copy)
6412 GEN_CP_PASTE_NOOP(paste)
6414 static void gen_tcheck(DisasContext *ctx)
6416 if (unlikely(!ctx->tm_enabled)) {
6417 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM);
6418 return;
6421 * Because tbegin always fails, the tcheck implementation is
6422 * simple:
6424 * CR[CRF] = TDOOMED || MSR[TS] || 0b0
6425 * = 0b1 || 0b00 || 0b0
6427 tcg_gen_movi_i32(cpu_crf[crfD(ctx->opcode)], 0x8);
6430 #if defined(CONFIG_USER_ONLY)
6431 #define GEN_TM_PRIV_NOOP(name) \
6432 static inline void gen_##name(DisasContext *ctx) \
6434 gen_priv_opc(ctx); \
6437 #else
6439 #define GEN_TM_PRIV_NOOP(name) \
6440 static inline void gen_##name(DisasContext *ctx) \
6442 CHK_SV(ctx); \
6443 if (unlikely(!ctx->tm_enabled)) { \
6444 gen_exception_err(ctx, POWERPC_EXCP_FU, FSCR_IC_TM); \
6445 return; \
6447 /* \
6448 * Because tbegin always fails, the implementation is \
6449 * simple: \
6451 * CR[0] = 0b0 || MSR[TS] || 0b0 \
6452 * = 0b0 || 0b00 | 0b0 \
6453 */ \
6454 tcg_gen_movi_i32(cpu_crf[0], 0); \
6457 #endif
6459 GEN_TM_PRIV_NOOP(treclaim);
6460 GEN_TM_PRIV_NOOP(trechkpt);
6462 static inline void get_fpr(TCGv_i64 dst, int regno)
6464 tcg_gen_ld_i64(dst, cpu_env, fpr_offset(regno));
6467 static inline void set_fpr(int regno, TCGv_i64 src)
6469 tcg_gen_st_i64(src, cpu_env, fpr_offset(regno));
6472 static inline void get_avr64(TCGv_i64 dst, int regno, bool high)
6474 tcg_gen_ld_i64(dst, cpu_env, avr64_offset(regno, high));
6477 static inline void set_avr64(int regno, TCGv_i64 src, bool high)
6479 tcg_gen_st_i64(src, cpu_env, avr64_offset(regno, high));
6483 * Helpers for decodetree used by !function for decoding arguments.
6485 static int times_2(DisasContext *ctx, int x)
6487 return x * 2;
6490 static int times_4(DisasContext *ctx, int x)
6492 return x * 4;
6495 static int times_16(DisasContext *ctx, int x)
6497 return x * 16;
6501 * Helpers for trans_* functions to check for specific insns flags.
6502 * Use token pasting to ensure that we use the proper flag with the
6503 * proper variable.
6505 #define REQUIRE_INSNS_FLAGS(CTX, NAME) \
6506 do { \
6507 if (((CTX)->insns_flags & PPC_##NAME) == 0) { \
6508 return false; \
6510 } while (0)
6512 #define REQUIRE_INSNS_FLAGS2(CTX, NAME) \
6513 do { \
6514 if (((CTX)->insns_flags2 & PPC2_##NAME) == 0) { \
6515 return false; \
6517 } while (0)
6519 /* Then special-case the check for 64-bit so that we elide code for ppc32. */
6520 #if TARGET_LONG_BITS == 32
6521 # define REQUIRE_64BIT(CTX) return false
6522 #else
6523 # define REQUIRE_64BIT(CTX) REQUIRE_INSNS_FLAGS(CTX, 64B)
6524 #endif
6526 #define REQUIRE_VECTOR(CTX) \
6527 do { \
6528 if (unlikely(!(CTX)->altivec_enabled)) { \
6529 gen_exception((CTX), POWERPC_EXCP_VPU); \
6530 return true; \
6532 } while (0)
6534 #define REQUIRE_VSX(CTX) \
6535 do { \
6536 if (unlikely(!(CTX)->vsx_enabled)) { \
6537 gen_exception((CTX), POWERPC_EXCP_VSXU); \
6538 return true; \
6540 } while (0)
6542 #define REQUIRE_FPU(ctx) \
6543 do { \
6544 if (unlikely(!(ctx)->fpu_enabled)) { \
6545 gen_exception((ctx), POWERPC_EXCP_FPU); \
6546 return true; \
6548 } while (0)
6550 #if !defined(CONFIG_USER_ONLY)
6551 #define REQUIRE_SV(CTX) \
6552 do { \
6553 if (unlikely((CTX)->pr)) { \
6554 gen_priv_opc(CTX); \
6555 return true; \
6557 } while (0)
6559 #define REQUIRE_HV(CTX) \
6560 do { \
6561 if (unlikely((CTX)->pr || !(CTX)->hv)) \
6562 gen_priv_opc(CTX); \
6563 return true; \
6565 } while (0)
6566 #else
6567 #define REQUIRE_SV(CTX) do { gen_priv_opc(CTX); return true; } while (0)
6568 #define REQUIRE_HV(CTX) do { gen_priv_opc(CTX); return true; } while (0)
6569 #endif
6572 * Helpers for implementing sets of trans_* functions.
6573 * Defer the implementation of NAME to FUNC, with optional extra arguments.
6575 #define TRANS(NAME, FUNC, ...) \
6576 static bool trans_##NAME(DisasContext *ctx, arg_##NAME *a) \
6577 { return FUNC(ctx, a, __VA_ARGS__); }
6578 #define TRANS_FLAGS(FLAGS, NAME, FUNC, ...) \
6579 static bool trans_##NAME(DisasContext *ctx, arg_##NAME *a) \
6581 REQUIRE_INSNS_FLAGS(ctx, FLAGS); \
6582 return FUNC(ctx, a, __VA_ARGS__); \
6584 #define TRANS_FLAGS2(FLAGS2, NAME, FUNC, ...) \
6585 static bool trans_##NAME(DisasContext *ctx, arg_##NAME *a) \
6587 REQUIRE_INSNS_FLAGS2(ctx, FLAGS2); \
6588 return FUNC(ctx, a, __VA_ARGS__); \
6591 #define TRANS64(NAME, FUNC, ...) \
6592 static bool trans_##NAME(DisasContext *ctx, arg_##NAME *a) \
6593 { REQUIRE_64BIT(ctx); return FUNC(ctx, a, __VA_ARGS__); }
6594 #define TRANS64_FLAGS2(FLAGS2, NAME, FUNC, ...) \
6595 static bool trans_##NAME(DisasContext *ctx, arg_##NAME *a) \
6597 REQUIRE_64BIT(ctx); \
6598 REQUIRE_INSNS_FLAGS2(ctx, FLAGS2); \
6599 return FUNC(ctx, a, __VA_ARGS__); \
6602 /* TODO: More TRANS* helpers for extra insn_flags checks. */
6605 #include "decode-insn32.c.inc"
6606 #include "decode-insn64.c.inc"
6607 #include "power8-pmu-regs.c.inc"
6610 * Incorporate CIA into the constant when R=1.
6611 * Validate that when R=1, RA=0.
6613 static bool resolve_PLS_D(DisasContext *ctx, arg_D *d, arg_PLS_D *a)
6615 d->rt = a->rt;
6616 d->ra = a->ra;
6617 d->si = a->si;
6618 if (a->r) {
6619 if (unlikely(a->ra != 0)) {
6620 gen_invalid(ctx);
6621 return false;
6623 d->si += ctx->cia;
6625 return true;
6628 #include "translate/fixedpoint-impl.c.inc"
6630 #include "translate/fp-impl.c.inc"
6632 #include "translate/vmx-impl.c.inc"
6634 #include "translate/vsx-impl.c.inc"
6636 #include "translate/dfp-impl.c.inc"
6638 #include "translate/spe-impl.c.inc"
6640 #include "translate/branch-impl.c.inc"
6642 #include "translate/storage-ctrl-impl.c.inc"
6644 /* Handles lfdp */
6645 static void gen_dform39(DisasContext *ctx)
6647 if ((ctx->opcode & 0x3) == 0) {
6648 if (ctx->insns_flags2 & PPC2_ISA205) {
6649 return gen_lfdp(ctx);
6652 return gen_invalid(ctx);
6655 /* Handles stfdp */
6656 static void gen_dform3D(DisasContext *ctx)
6658 if ((ctx->opcode & 3) == 0) { /* DS-FORM */
6659 /* stfdp */
6660 if (ctx->insns_flags2 & PPC2_ISA205) {
6661 return gen_stfdp(ctx);
6664 return gen_invalid(ctx);
6667 #if defined(TARGET_PPC64)
6668 /* brd */
6669 static void gen_brd(DisasContext *ctx)
6671 tcg_gen_bswap64_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
6674 /* brw */
6675 static void gen_brw(DisasContext *ctx)
6677 tcg_gen_bswap64_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rS(ctx->opcode)]);
6678 tcg_gen_rotli_i64(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rA(ctx->opcode)], 32);
6682 /* brh */
6683 static void gen_brh(DisasContext *ctx)
6685 TCGv_i64 mask = tcg_constant_i64(0x00ff00ff00ff00ffull);
6686 TCGv_i64 t1 = tcg_temp_new_i64();
6687 TCGv_i64 t2 = tcg_temp_new_i64();
6689 tcg_gen_shri_i64(t1, cpu_gpr[rS(ctx->opcode)], 8);
6690 tcg_gen_and_i64(t2, t1, mask);
6691 tcg_gen_and_i64(t1, cpu_gpr[rS(ctx->opcode)], mask);
6692 tcg_gen_shli_i64(t1, t1, 8);
6693 tcg_gen_or_i64(cpu_gpr[rA(ctx->opcode)], t1, t2);
6695 tcg_temp_free_i64(t1);
6696 tcg_temp_free_i64(t2);
6698 #endif
6700 static opcode_t opcodes[] = {
6701 #if defined(TARGET_PPC64)
6702 GEN_HANDLER_E(brd, 0x1F, 0x1B, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA310),
6703 GEN_HANDLER_E(brw, 0x1F, 0x1B, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA310),
6704 GEN_HANDLER_E(brh, 0x1F, 0x1B, 0x06, 0x0000F801, PPC_NONE, PPC2_ISA310),
6705 #endif
6706 GEN_HANDLER(invalid, 0x00, 0x00, 0x00, 0xFFFFFFFF, PPC_NONE),
6707 #if defined(TARGET_PPC64)
6708 GEN_HANDLER_E(cmpeqb, 0x1F, 0x00, 0x07, 0x00600000, PPC_NONE, PPC2_ISA300),
6709 #endif
6710 GEN_HANDLER_E(cmpb, 0x1F, 0x1C, 0x0F, 0x00000001, PPC_NONE, PPC2_ISA205),
6711 GEN_HANDLER_E(cmprb, 0x1F, 0x00, 0x06, 0x00400001, PPC_NONE, PPC2_ISA300),
6712 GEN_HANDLER(isel, 0x1F, 0x0F, 0xFF, 0x00000001, PPC_ISEL),
6713 GEN_HANDLER(addic, 0x0C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6714 GEN_HANDLER2(addic_, "addic.", 0x0D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6715 GEN_HANDLER(mulhw, 0x1F, 0x0B, 0x02, 0x00000400, PPC_INTEGER),
6716 GEN_HANDLER(mulhwu, 0x1F, 0x0B, 0x00, 0x00000400, PPC_INTEGER),
6717 GEN_HANDLER(mullw, 0x1F, 0x0B, 0x07, 0x00000000, PPC_INTEGER),
6718 GEN_HANDLER(mullwo, 0x1F, 0x0B, 0x17, 0x00000000, PPC_INTEGER),
6719 GEN_HANDLER(mulli, 0x07, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6720 #if defined(TARGET_PPC64)
6721 GEN_HANDLER(mulld, 0x1F, 0x09, 0x07, 0x00000000, PPC_64B),
6722 #endif
6723 GEN_HANDLER(neg, 0x1F, 0x08, 0x03, 0x0000F800, PPC_INTEGER),
6724 GEN_HANDLER(nego, 0x1F, 0x08, 0x13, 0x0000F800, PPC_INTEGER),
6725 GEN_HANDLER(subfic, 0x08, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6726 GEN_HANDLER2(andi_, "andi.", 0x1C, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6727 GEN_HANDLER2(andis_, "andis.", 0x1D, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6728 GEN_HANDLER(cntlzw, 0x1F, 0x1A, 0x00, 0x00000000, PPC_INTEGER),
6729 GEN_HANDLER_E(cnttzw, 0x1F, 0x1A, 0x10, 0x00000000, PPC_NONE, PPC2_ISA300),
6730 GEN_HANDLER_E(copy, 0x1F, 0x06, 0x18, 0x03C00001, PPC_NONE, PPC2_ISA300),
6731 GEN_HANDLER_E(cp_abort, 0x1F, 0x06, 0x1A, 0x03FFF801, PPC_NONE, PPC2_ISA300),
6732 GEN_HANDLER_E(paste, 0x1F, 0x06, 0x1C, 0x03C00000, PPC_NONE, PPC2_ISA300),
6733 GEN_HANDLER(or, 0x1F, 0x1C, 0x0D, 0x00000000, PPC_INTEGER),
6734 GEN_HANDLER(xor, 0x1F, 0x1C, 0x09, 0x00000000, PPC_INTEGER),
6735 GEN_HANDLER(ori, 0x18, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6736 GEN_HANDLER(oris, 0x19, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6737 GEN_HANDLER(xori, 0x1A, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6738 GEN_HANDLER(xoris, 0x1B, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6739 GEN_HANDLER(popcntb, 0x1F, 0x1A, 0x03, 0x0000F801, PPC_POPCNTB),
6740 GEN_HANDLER(popcntw, 0x1F, 0x1A, 0x0b, 0x0000F801, PPC_POPCNTWD),
6741 GEN_HANDLER_E(prtyw, 0x1F, 0x1A, 0x04, 0x0000F801, PPC_NONE, PPC2_ISA205),
6742 #if defined(TARGET_PPC64)
6743 GEN_HANDLER(popcntd, 0x1F, 0x1A, 0x0F, 0x0000F801, PPC_POPCNTWD),
6744 GEN_HANDLER(cntlzd, 0x1F, 0x1A, 0x01, 0x00000000, PPC_64B),
6745 GEN_HANDLER_E(cnttzd, 0x1F, 0x1A, 0x11, 0x00000000, PPC_NONE, PPC2_ISA300),
6746 GEN_HANDLER_E(darn, 0x1F, 0x13, 0x17, 0x001CF801, PPC_NONE, PPC2_ISA300),
6747 GEN_HANDLER_E(prtyd, 0x1F, 0x1A, 0x05, 0x0000F801, PPC_NONE, PPC2_ISA205),
6748 GEN_HANDLER_E(bpermd, 0x1F, 0x1C, 0x07, 0x00000001, PPC_NONE, PPC2_PERM_ISA206),
6749 #endif
6750 GEN_HANDLER(rlwimi, 0x14, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6751 GEN_HANDLER(rlwinm, 0x15, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6752 GEN_HANDLER(rlwnm, 0x17, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6753 GEN_HANDLER(slw, 0x1F, 0x18, 0x00, 0x00000000, PPC_INTEGER),
6754 GEN_HANDLER(sraw, 0x1F, 0x18, 0x18, 0x00000000, PPC_INTEGER),
6755 GEN_HANDLER(srawi, 0x1F, 0x18, 0x19, 0x00000000, PPC_INTEGER),
6756 GEN_HANDLER(srw, 0x1F, 0x18, 0x10, 0x00000000, PPC_INTEGER),
6757 #if defined(TARGET_PPC64)
6758 GEN_HANDLER(sld, 0x1F, 0x1B, 0x00, 0x00000000, PPC_64B),
6759 GEN_HANDLER(srad, 0x1F, 0x1A, 0x18, 0x00000000, PPC_64B),
6760 GEN_HANDLER2(sradi0, "sradi", 0x1F, 0x1A, 0x19, 0x00000000, PPC_64B),
6761 GEN_HANDLER2(sradi1, "sradi", 0x1F, 0x1B, 0x19, 0x00000000, PPC_64B),
6762 GEN_HANDLER(srd, 0x1F, 0x1B, 0x10, 0x00000000, PPC_64B),
6763 GEN_HANDLER2_E(extswsli0, "extswsli", 0x1F, 0x1A, 0x1B, 0x00000000,
6764 PPC_NONE, PPC2_ISA300),
6765 GEN_HANDLER2_E(extswsli1, "extswsli", 0x1F, 0x1B, 0x1B, 0x00000000,
6766 PPC_NONE, PPC2_ISA300),
6767 #endif
6768 /* handles lfdp, lxsd, lxssp */
6769 GEN_HANDLER_E(dform39, 0x39, 0xFF, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA205),
6770 /* handles stfdp, stxsd, stxssp */
6771 GEN_HANDLER_E(dform3D, 0x3D, 0xFF, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA205),
6772 GEN_HANDLER(lmw, 0x2E, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6773 GEN_HANDLER(stmw, 0x2F, 0xFF, 0xFF, 0x00000000, PPC_INTEGER),
6774 GEN_HANDLER(lswi, 0x1F, 0x15, 0x12, 0x00000001, PPC_STRING),
6775 GEN_HANDLER(lswx, 0x1F, 0x15, 0x10, 0x00000001, PPC_STRING),
6776 GEN_HANDLER(stswi, 0x1F, 0x15, 0x16, 0x00000001, PPC_STRING),
6777 GEN_HANDLER(stswx, 0x1F, 0x15, 0x14, 0x00000001, PPC_STRING),
6778 GEN_HANDLER(eieio, 0x1F, 0x16, 0x1A, 0x01FFF801, PPC_MEM_EIEIO),
6779 GEN_HANDLER(isync, 0x13, 0x16, 0x04, 0x03FFF801, PPC_MEM),
6780 GEN_HANDLER_E(lbarx, 0x1F, 0x14, 0x01, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6781 GEN_HANDLER_E(lharx, 0x1F, 0x14, 0x03, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6782 GEN_HANDLER(lwarx, 0x1F, 0x14, 0x00, 0x00000000, PPC_RES),
6783 GEN_HANDLER_E(lwat, 0x1F, 0x06, 0x12, 0x00000001, PPC_NONE, PPC2_ISA300),
6784 GEN_HANDLER_E(stwat, 0x1F, 0x06, 0x16, 0x00000001, PPC_NONE, PPC2_ISA300),
6785 GEN_HANDLER_E(stbcx_, 0x1F, 0x16, 0x15, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6786 GEN_HANDLER_E(sthcx_, 0x1F, 0x16, 0x16, 0, PPC_NONE, PPC2_ATOMIC_ISA206),
6787 GEN_HANDLER2(stwcx_, "stwcx.", 0x1F, 0x16, 0x04, 0x00000000, PPC_RES),
6788 #if defined(TARGET_PPC64)
6789 GEN_HANDLER_E(ldat, 0x1F, 0x06, 0x13, 0x00000001, PPC_NONE, PPC2_ISA300),
6790 GEN_HANDLER_E(stdat, 0x1F, 0x06, 0x17, 0x00000001, PPC_NONE, PPC2_ISA300),
6791 GEN_HANDLER(ldarx, 0x1F, 0x14, 0x02, 0x00000000, PPC_64B),
6792 GEN_HANDLER_E(lqarx, 0x1F, 0x14, 0x08, 0, PPC_NONE, PPC2_LSQ_ISA207),
6793 GEN_HANDLER2(stdcx_, "stdcx.", 0x1F, 0x16, 0x06, 0x00000000, PPC_64B),
6794 GEN_HANDLER_E(stqcx_, 0x1F, 0x16, 0x05, 0, PPC_NONE, PPC2_LSQ_ISA207),
6795 #endif
6796 GEN_HANDLER(sync, 0x1F, 0x16, 0x12, 0x039FF801, PPC_MEM_SYNC),
6797 GEN_HANDLER(wait, 0x1F, 0x1E, 0x01, 0x03FFF801, PPC_WAIT),
6798 GEN_HANDLER_E(wait, 0x1F, 0x1E, 0x00, 0x039FF801, PPC_NONE, PPC2_ISA300),
6799 GEN_HANDLER(b, 0x12, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
6800 GEN_HANDLER(bc, 0x10, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
6801 GEN_HANDLER(bcctr, 0x13, 0x10, 0x10, 0x00000000, PPC_FLOW),
6802 GEN_HANDLER(bclr, 0x13, 0x10, 0x00, 0x00000000, PPC_FLOW),
6803 GEN_HANDLER_E(bctar, 0x13, 0x10, 0x11, 0x0000E000, PPC_NONE, PPC2_BCTAR_ISA207),
6804 GEN_HANDLER(mcrf, 0x13, 0x00, 0xFF, 0x00000001, PPC_INTEGER),
6805 GEN_HANDLER(rfi, 0x13, 0x12, 0x01, 0x03FF8001, PPC_FLOW),
6806 #if defined(TARGET_PPC64)
6807 GEN_HANDLER(rfid, 0x13, 0x12, 0x00, 0x03FF8001, PPC_64B),
6808 #if !defined(CONFIG_USER_ONLY)
6809 /* Top bit of opc2 corresponds with low bit of LEV, so use two handlers */
6810 GEN_HANDLER_E(scv, 0x11, 0x10, 0xFF, 0x03FFF01E, PPC_NONE, PPC2_ISA300),
6811 GEN_HANDLER_E(scv, 0x11, 0x00, 0xFF, 0x03FFF01E, PPC_NONE, PPC2_ISA300),
6812 GEN_HANDLER_E(rfscv, 0x13, 0x12, 0x02, 0x03FF8001, PPC_NONE, PPC2_ISA300),
6813 #endif
6814 GEN_HANDLER_E(stop, 0x13, 0x12, 0x0b, 0x03FFF801, PPC_NONE, PPC2_ISA300),
6815 GEN_HANDLER_E(doze, 0x13, 0x12, 0x0c, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
6816 GEN_HANDLER_E(nap, 0x13, 0x12, 0x0d, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
6817 GEN_HANDLER_E(sleep, 0x13, 0x12, 0x0e, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
6818 GEN_HANDLER_E(rvwinkle, 0x13, 0x12, 0x0f, 0x03FFF801, PPC_NONE, PPC2_PM_ISA206),
6819 GEN_HANDLER(hrfid, 0x13, 0x12, 0x08, 0x03FF8001, PPC_64H),
6820 #endif
6821 /* Top bit of opc2 corresponds with low bit of LEV, so use two handlers */
6822 GEN_HANDLER(sc, 0x11, 0x11, 0xFF, 0x03FFF01D, PPC_FLOW),
6823 GEN_HANDLER(sc, 0x11, 0x01, 0xFF, 0x03FFF01D, PPC_FLOW),
6824 GEN_HANDLER(tw, 0x1F, 0x04, 0x00, 0x00000001, PPC_FLOW),
6825 GEN_HANDLER(twi, 0x03, 0xFF, 0xFF, 0x00000000, PPC_FLOW),
6826 #if defined(TARGET_PPC64)
6827 GEN_HANDLER(td, 0x1F, 0x04, 0x02, 0x00000001, PPC_64B),
6828 GEN_HANDLER(tdi, 0x02, 0xFF, 0xFF, 0x00000000, PPC_64B),
6829 #endif
6830 GEN_HANDLER(mcrxr, 0x1F, 0x00, 0x10, 0x007FF801, PPC_MISC),
6831 GEN_HANDLER(mfcr, 0x1F, 0x13, 0x00, 0x00000801, PPC_MISC),
6832 GEN_HANDLER(mfmsr, 0x1F, 0x13, 0x02, 0x001FF801, PPC_MISC),
6833 GEN_HANDLER(mfspr, 0x1F, 0x13, 0x0A, 0x00000001, PPC_MISC),
6834 GEN_HANDLER(mftb, 0x1F, 0x13, 0x0B, 0x00000001, PPC_MFTB),
6835 GEN_HANDLER(mtcrf, 0x1F, 0x10, 0x04, 0x00000801, PPC_MISC),
6836 #if defined(TARGET_PPC64)
6837 GEN_HANDLER(mtmsrd, 0x1F, 0x12, 0x05, 0x001EF801, PPC_64B),
6838 GEN_HANDLER_E(setb, 0x1F, 0x00, 0x04, 0x0003F801, PPC_NONE, PPC2_ISA300),
6839 GEN_HANDLER_E(mcrxrx, 0x1F, 0x00, 0x12, 0x007FF801, PPC_NONE, PPC2_ISA300),
6840 #endif
6841 GEN_HANDLER(mtmsr, 0x1F, 0x12, 0x04, 0x001EF801, PPC_MISC),
6842 GEN_HANDLER(mtspr, 0x1F, 0x13, 0x0E, 0x00000000, PPC_MISC),
6843 GEN_HANDLER(dcbf, 0x1F, 0x16, 0x02, 0x03C00001, PPC_CACHE),
6844 GEN_HANDLER_E(dcbfep, 0x1F, 0x1F, 0x03, 0x03C00001, PPC_NONE, PPC2_BOOKE206),
6845 GEN_HANDLER(dcbi, 0x1F, 0x16, 0x0E, 0x03E00001, PPC_CACHE),
6846 GEN_HANDLER(dcbst, 0x1F, 0x16, 0x01, 0x03E00001, PPC_CACHE),
6847 GEN_HANDLER_E(dcbstep, 0x1F, 0x1F, 0x01, 0x03E00001, PPC_NONE, PPC2_BOOKE206),
6848 GEN_HANDLER(dcbt, 0x1F, 0x16, 0x08, 0x00000001, PPC_CACHE),
6849 GEN_HANDLER_E(dcbtep, 0x1F, 0x1F, 0x09, 0x00000001, PPC_NONE, PPC2_BOOKE206),
6850 GEN_HANDLER(dcbtst, 0x1F, 0x16, 0x07, 0x00000001, PPC_CACHE),
6851 GEN_HANDLER_E(dcbtstep, 0x1F, 0x1F, 0x07, 0x00000001, PPC_NONE, PPC2_BOOKE206),
6852 GEN_HANDLER_E(dcbtls, 0x1F, 0x06, 0x05, 0x02000001, PPC_BOOKE, PPC2_BOOKE206),
6853 GEN_HANDLER(dcbz, 0x1F, 0x16, 0x1F, 0x03C00001, PPC_CACHE_DCBZ),
6854 GEN_HANDLER_E(dcbzep, 0x1F, 0x1F, 0x1F, 0x03C00001, PPC_NONE, PPC2_BOOKE206),
6855 GEN_HANDLER(dst, 0x1F, 0x16, 0x0A, 0x01800001, PPC_ALTIVEC),
6856 GEN_HANDLER(dstst, 0x1F, 0x16, 0x0B, 0x01800001, PPC_ALTIVEC),
6857 GEN_HANDLER(dss, 0x1F, 0x16, 0x19, 0x019FF801, PPC_ALTIVEC),
6858 GEN_HANDLER(icbi, 0x1F, 0x16, 0x1E, 0x03E00001, PPC_CACHE_ICBI),
6859 GEN_HANDLER_E(icbiep, 0x1F, 0x1F, 0x1E, 0x03E00001, PPC_NONE, PPC2_BOOKE206),
6860 GEN_HANDLER(dcba, 0x1F, 0x16, 0x17, 0x03E00001, PPC_CACHE_DCBA),
6861 GEN_HANDLER(mfsr, 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT),
6862 GEN_HANDLER(mfsrin, 0x1F, 0x13, 0x14, 0x001F0001, PPC_SEGMENT),
6863 GEN_HANDLER(mtsr, 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT),
6864 GEN_HANDLER(mtsrin, 0x1F, 0x12, 0x07, 0x001F0001, PPC_SEGMENT),
6865 #if defined(TARGET_PPC64)
6866 GEN_HANDLER2(mfsr_64b, "mfsr", 0x1F, 0x13, 0x12, 0x0010F801, PPC_SEGMENT_64B),
6867 GEN_HANDLER2(mfsrin_64b, "mfsrin", 0x1F, 0x13, 0x14, 0x001F0001,
6868 PPC_SEGMENT_64B),
6869 GEN_HANDLER2(mtsr_64b, "mtsr", 0x1F, 0x12, 0x06, 0x0010F801, PPC_SEGMENT_64B),
6870 GEN_HANDLER2(mtsrin_64b, "mtsrin", 0x1F, 0x12, 0x07, 0x001F0001,
6871 PPC_SEGMENT_64B),
6872 GEN_HANDLER2(slbmte, "slbmte", 0x1F, 0x12, 0x0C, 0x001F0001, PPC_SEGMENT_64B),
6873 GEN_HANDLER2(slbmfee, "slbmfee", 0x1F, 0x13, 0x1C, 0x001F0001, PPC_SEGMENT_64B),
6874 GEN_HANDLER2(slbmfev, "slbmfev", 0x1F, 0x13, 0x1A, 0x001F0001, PPC_SEGMENT_64B),
6875 GEN_HANDLER2(slbfee_, "slbfee.", 0x1F, 0x13, 0x1E, 0x001F0000, PPC_SEGMENT_64B),
6876 #endif
6877 GEN_HANDLER(tlbia, 0x1F, 0x12, 0x0B, 0x03FFFC01, PPC_MEM_TLBIA),
6879 * XXX Those instructions will need to be handled differently for
6880 * different ISA versions
6882 GEN_HANDLER(tlbsync, 0x1F, 0x16, 0x11, 0x03FFF801, PPC_MEM_TLBSYNC),
6883 #if defined(TARGET_PPC64)
6884 GEN_HANDLER(slbia, 0x1F, 0x12, 0x0F, 0x031FFC01, PPC_SLBI),
6885 GEN_HANDLER_E(slbieg, 0x1F, 0x12, 0x0E, 0x001F0001, PPC_NONE, PPC2_ISA300),
6886 GEN_HANDLER_E(slbsync, 0x1F, 0x12, 0x0A, 0x03FFF801, PPC_NONE, PPC2_ISA300),
6887 #endif
6888 GEN_HANDLER(eciwx, 0x1F, 0x16, 0x0D, 0x00000001, PPC_EXTERN),
6889 GEN_HANDLER(ecowx, 0x1F, 0x16, 0x09, 0x00000001, PPC_EXTERN),
6890 GEN_HANDLER2(tlbld_6xx, "tlbld", 0x1F, 0x12, 0x1E, 0x03FF0001, PPC_6xx_TLB),
6891 GEN_HANDLER2(tlbli_6xx, "tlbli", 0x1F, 0x12, 0x1F, 0x03FF0001, PPC_6xx_TLB),
6892 GEN_HANDLER(mfapidi, 0x1F, 0x13, 0x08, 0x0000F801, PPC_MFAPIDI),
6893 GEN_HANDLER(tlbiva, 0x1F, 0x12, 0x18, 0x03FFF801, PPC_TLBIVA),
6894 GEN_HANDLER(mfdcr, 0x1F, 0x03, 0x0A, 0x00000001, PPC_DCR),
6895 GEN_HANDLER(mtdcr, 0x1F, 0x03, 0x0E, 0x00000001, PPC_DCR),
6896 GEN_HANDLER(mfdcrx, 0x1F, 0x03, 0x08, 0x00000000, PPC_DCRX),
6897 GEN_HANDLER(mtdcrx, 0x1F, 0x03, 0x0C, 0x00000000, PPC_DCRX),
6898 GEN_HANDLER(dccci, 0x1F, 0x06, 0x0E, 0x03E00001, PPC_4xx_COMMON),
6899 GEN_HANDLER(dcread, 0x1F, 0x06, 0x0F, 0x00000001, PPC_4xx_COMMON),
6900 GEN_HANDLER2(icbt_40x, "icbt", 0x1F, 0x06, 0x08, 0x03E00001, PPC_40x_ICBT),
6901 GEN_HANDLER(iccci, 0x1F, 0x06, 0x1E, 0x00000001, PPC_4xx_COMMON),
6902 GEN_HANDLER(icread, 0x1F, 0x06, 0x1F, 0x03E00001, PPC_4xx_COMMON),
6903 GEN_HANDLER2(rfci_40x, "rfci", 0x13, 0x13, 0x01, 0x03FF8001, PPC_40x_EXCP),
6904 GEN_HANDLER_E(rfci, 0x13, 0x13, 0x01, 0x03FF8001, PPC_BOOKE, PPC2_BOOKE206),
6905 GEN_HANDLER(rfdi, 0x13, 0x07, 0x01, 0x03FF8001, PPC_RFDI),
6906 GEN_HANDLER(rfmci, 0x13, 0x06, 0x01, 0x03FF8001, PPC_RFMCI),
6907 GEN_HANDLER2(tlbre_40x, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_40x_TLB),
6908 GEN_HANDLER2(tlbsx_40x, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_40x_TLB),
6909 GEN_HANDLER2(tlbwe_40x, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_40x_TLB),
6910 GEN_HANDLER2(tlbre_440, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001, PPC_BOOKE),
6911 GEN_HANDLER2(tlbsx_440, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000, PPC_BOOKE),
6912 GEN_HANDLER2(tlbwe_440, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001, PPC_BOOKE),
6913 GEN_HANDLER2_E(tlbre_booke206, "tlbre", 0x1F, 0x12, 0x1D, 0x00000001,
6914 PPC_NONE, PPC2_BOOKE206),
6915 GEN_HANDLER2_E(tlbsx_booke206, "tlbsx", 0x1F, 0x12, 0x1C, 0x00000000,
6916 PPC_NONE, PPC2_BOOKE206),
6917 GEN_HANDLER2_E(tlbwe_booke206, "tlbwe", 0x1F, 0x12, 0x1E, 0x00000001,
6918 PPC_NONE, PPC2_BOOKE206),
6919 GEN_HANDLER2_E(tlbivax_booke206, "tlbivax", 0x1F, 0x12, 0x18, 0x00000001,
6920 PPC_NONE, PPC2_BOOKE206),
6921 GEN_HANDLER2_E(tlbilx_booke206, "tlbilx", 0x1F, 0x12, 0x00, 0x03800001,
6922 PPC_NONE, PPC2_BOOKE206),
6923 GEN_HANDLER2_E(msgsnd, "msgsnd", 0x1F, 0x0E, 0x06, 0x03ff0001,
6924 PPC_NONE, PPC2_PRCNTL),
6925 GEN_HANDLER2_E(msgclr, "msgclr", 0x1F, 0x0E, 0x07, 0x03ff0001,
6926 PPC_NONE, PPC2_PRCNTL),
6927 GEN_HANDLER2_E(msgsync, "msgsync", 0x1F, 0x16, 0x1B, 0x00000000,
6928 PPC_NONE, PPC2_PRCNTL),
6929 GEN_HANDLER(wrtee, 0x1F, 0x03, 0x04, 0x000FFC01, PPC_WRTEE),
6930 GEN_HANDLER(wrteei, 0x1F, 0x03, 0x05, 0x000E7C01, PPC_WRTEE),
6931 GEN_HANDLER(dlmzb, 0x1F, 0x0E, 0x02, 0x00000000, PPC_440_SPEC),
6932 GEN_HANDLER_E(mbar, 0x1F, 0x16, 0x1a, 0x001FF801,
6933 PPC_BOOKE, PPC2_BOOKE206),
6934 GEN_HANDLER(msync_4xx, 0x1F, 0x16, 0x12, 0x039FF801, PPC_BOOKE),
6935 GEN_HANDLER2_E(icbt_440, "icbt", 0x1F, 0x16, 0x00, 0x03E00001,
6936 PPC_BOOKE, PPC2_BOOKE206),
6937 GEN_HANDLER2(icbt_440, "icbt", 0x1F, 0x06, 0x08, 0x03E00001,
6938 PPC_440_SPEC),
6939 GEN_HANDLER(lvsl, 0x1f, 0x06, 0x00, 0x00000001, PPC_ALTIVEC),
6940 GEN_HANDLER(lvsr, 0x1f, 0x06, 0x01, 0x00000001, PPC_ALTIVEC),
6941 GEN_HANDLER(mfvscr, 0x04, 0x2, 0x18, 0x001ff800, PPC_ALTIVEC),
6942 GEN_HANDLER(mtvscr, 0x04, 0x2, 0x19, 0x03ff0000, PPC_ALTIVEC),
6943 GEN_HANDLER(vmladduhm, 0x04, 0x11, 0xFF, 0x00000000, PPC_ALTIVEC),
6944 #if defined(TARGET_PPC64)
6945 GEN_HANDLER_E(maddhd_maddhdu, 0x04, 0x18, 0xFF, 0x00000000, PPC_NONE,
6946 PPC2_ISA300),
6947 GEN_HANDLER_E(maddld, 0x04, 0x19, 0xFF, 0x00000000, PPC_NONE, PPC2_ISA300),
6948 GEN_HANDLER2_E(msgsndp, "msgsndp", 0x1F, 0x0E, 0x04, 0x03ff0001,
6949 PPC_NONE, PPC2_ISA207S),
6950 GEN_HANDLER2_E(msgclrp, "msgclrp", 0x1F, 0x0E, 0x05, 0x03ff0001,
6951 PPC_NONE, PPC2_ISA207S),
6952 #endif
6954 #undef GEN_INT_ARITH_ADD
6955 #undef GEN_INT_ARITH_ADD_CONST
6956 #define GEN_INT_ARITH_ADD(name, opc3, add_ca, compute_ca, compute_ov) \
6957 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x00000000, PPC_INTEGER),
6958 #define GEN_INT_ARITH_ADD_CONST(name, opc3, const_val, \
6959 add_ca, compute_ca, compute_ov) \
6960 GEN_HANDLER(name, 0x1F, 0x0A, opc3, 0x0000F800, PPC_INTEGER),
6961 GEN_INT_ARITH_ADD(add, 0x08, 0, 0, 0)
6962 GEN_INT_ARITH_ADD(addo, 0x18, 0, 0, 1)
6963 GEN_INT_ARITH_ADD(addc, 0x00, 0, 1, 0)
6964 GEN_INT_ARITH_ADD(addco, 0x10, 0, 1, 1)
6965 GEN_INT_ARITH_ADD(adde, 0x04, 1, 1, 0)
6966 GEN_INT_ARITH_ADD(addeo, 0x14, 1, 1, 1)
6967 GEN_INT_ARITH_ADD_CONST(addme, 0x07, -1LL, 1, 1, 0)
6968 GEN_INT_ARITH_ADD_CONST(addmeo, 0x17, -1LL, 1, 1, 1)
6969 GEN_HANDLER_E(addex, 0x1F, 0x0A, 0x05, 0x00000000, PPC_NONE, PPC2_ISA300),
6970 GEN_INT_ARITH_ADD_CONST(addze, 0x06, 0, 1, 1, 0)
6971 GEN_INT_ARITH_ADD_CONST(addzeo, 0x16, 0, 1, 1, 1)
6973 #undef GEN_INT_ARITH_DIVW
6974 #define GEN_INT_ARITH_DIVW(name, opc3, sign, compute_ov) \
6975 GEN_HANDLER(name, 0x1F, 0x0B, opc3, 0x00000000, PPC_INTEGER)
6976 GEN_INT_ARITH_DIVW(divwu, 0x0E, 0, 0),
6977 GEN_INT_ARITH_DIVW(divwuo, 0x1E, 0, 1),
6978 GEN_INT_ARITH_DIVW(divw, 0x0F, 1, 0),
6979 GEN_INT_ARITH_DIVW(divwo, 0x1F, 1, 1),
6980 GEN_HANDLER_E(divwe, 0x1F, 0x0B, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
6981 GEN_HANDLER_E(divweo, 0x1F, 0x0B, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
6982 GEN_HANDLER_E(divweu, 0x1F, 0x0B, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
6983 GEN_HANDLER_E(divweuo, 0x1F, 0x0B, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
6984 GEN_HANDLER_E(modsw, 0x1F, 0x0B, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
6985 GEN_HANDLER_E(moduw, 0x1F, 0x0B, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
6987 #if defined(TARGET_PPC64)
6988 #undef GEN_INT_ARITH_DIVD
6989 #define GEN_INT_ARITH_DIVD(name, opc3, sign, compute_ov) \
6990 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
6991 GEN_INT_ARITH_DIVD(divdu, 0x0E, 0, 0),
6992 GEN_INT_ARITH_DIVD(divduo, 0x1E, 0, 1),
6993 GEN_INT_ARITH_DIVD(divd, 0x0F, 1, 0),
6994 GEN_INT_ARITH_DIVD(divdo, 0x1F, 1, 1),
6996 GEN_HANDLER_E(divdeu, 0x1F, 0x09, 0x0C, 0, PPC_NONE, PPC2_DIVE_ISA206),
6997 GEN_HANDLER_E(divdeuo, 0x1F, 0x09, 0x1C, 0, PPC_NONE, PPC2_DIVE_ISA206),
6998 GEN_HANDLER_E(divde, 0x1F, 0x09, 0x0D, 0, PPC_NONE, PPC2_DIVE_ISA206),
6999 GEN_HANDLER_E(divdeo, 0x1F, 0x09, 0x1D, 0, PPC_NONE, PPC2_DIVE_ISA206),
7000 GEN_HANDLER_E(modsd, 0x1F, 0x09, 0x18, 0x00000001, PPC_NONE, PPC2_ISA300),
7001 GEN_HANDLER_E(modud, 0x1F, 0x09, 0x08, 0x00000001, PPC_NONE, PPC2_ISA300),
7003 #undef GEN_INT_ARITH_MUL_HELPER
7004 #define GEN_INT_ARITH_MUL_HELPER(name, opc3) \
7005 GEN_HANDLER(name, 0x1F, 0x09, opc3, 0x00000000, PPC_64B)
7006 GEN_INT_ARITH_MUL_HELPER(mulhdu, 0x00),
7007 GEN_INT_ARITH_MUL_HELPER(mulhd, 0x02),
7008 GEN_INT_ARITH_MUL_HELPER(mulldo, 0x17),
7009 #endif
7011 #undef GEN_INT_ARITH_SUBF
7012 #undef GEN_INT_ARITH_SUBF_CONST
7013 #define GEN_INT_ARITH_SUBF(name, opc3, add_ca, compute_ca, compute_ov) \
7014 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x00000000, PPC_INTEGER),
7015 #define GEN_INT_ARITH_SUBF_CONST(name, opc3, const_val, \
7016 add_ca, compute_ca, compute_ov) \
7017 GEN_HANDLER(name, 0x1F, 0x08, opc3, 0x0000F800, PPC_INTEGER),
7018 GEN_INT_ARITH_SUBF(subf, 0x01, 0, 0, 0)
7019 GEN_INT_ARITH_SUBF(subfo, 0x11, 0, 0, 1)
7020 GEN_INT_ARITH_SUBF(subfc, 0x00, 0, 1, 0)
7021 GEN_INT_ARITH_SUBF(subfco, 0x10, 0, 1, 1)
7022 GEN_INT_ARITH_SUBF(subfe, 0x04, 1, 1, 0)
7023 GEN_INT_ARITH_SUBF(subfeo, 0x14, 1, 1, 1)
7024 GEN_INT_ARITH_SUBF_CONST(subfme, 0x07, -1LL, 1, 1, 0)
7025 GEN_INT_ARITH_SUBF_CONST(subfmeo, 0x17, -1LL, 1, 1, 1)
7026 GEN_INT_ARITH_SUBF_CONST(subfze, 0x06, 0, 1, 1, 0)
7027 GEN_INT_ARITH_SUBF_CONST(subfzeo, 0x16, 0, 1, 1, 1)
7029 #undef GEN_LOGICAL1
7030 #undef GEN_LOGICAL2
7031 #define GEN_LOGICAL2(name, tcg_op, opc, type) \
7032 GEN_HANDLER(name, 0x1F, 0x1C, opc, 0x00000000, type)
7033 #define GEN_LOGICAL1(name, tcg_op, opc, type) \
7034 GEN_HANDLER(name, 0x1F, 0x1A, opc, 0x00000000, type)
7035 GEN_LOGICAL2(and, tcg_gen_and_tl, 0x00, PPC_INTEGER),
7036 GEN_LOGICAL2(andc, tcg_gen_andc_tl, 0x01, PPC_INTEGER),
7037 GEN_LOGICAL2(eqv, tcg_gen_eqv_tl, 0x08, PPC_INTEGER),
7038 GEN_LOGICAL1(extsb, tcg_gen_ext8s_tl, 0x1D, PPC_INTEGER),
7039 GEN_LOGICAL1(extsh, tcg_gen_ext16s_tl, 0x1C, PPC_INTEGER),
7040 GEN_LOGICAL2(nand, tcg_gen_nand_tl, 0x0E, PPC_INTEGER),
7041 GEN_LOGICAL2(nor, tcg_gen_nor_tl, 0x03, PPC_INTEGER),
7042 GEN_LOGICAL2(orc, tcg_gen_orc_tl, 0x0C, PPC_INTEGER),
7043 #if defined(TARGET_PPC64)
7044 GEN_LOGICAL1(extsw, tcg_gen_ext32s_tl, 0x1E, PPC_64B),
7045 #endif
7047 #if defined(TARGET_PPC64)
7048 #undef GEN_PPC64_R2
7049 #undef GEN_PPC64_R4
7050 #define GEN_PPC64_R2(name, opc1, opc2) \
7051 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
7052 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
7053 PPC_64B)
7054 #define GEN_PPC64_R4(name, opc1, opc2) \
7055 GEN_HANDLER2(name##0, stringify(name), opc1, opc2, 0xFF, 0x00000000, PPC_64B),\
7056 GEN_HANDLER2(name##1, stringify(name), opc1, opc2 | 0x01, 0xFF, 0x00000000, \
7057 PPC_64B), \
7058 GEN_HANDLER2(name##2, stringify(name), opc1, opc2 | 0x10, 0xFF, 0x00000000, \
7059 PPC_64B), \
7060 GEN_HANDLER2(name##3, stringify(name), opc1, opc2 | 0x11, 0xFF, 0x00000000, \
7061 PPC_64B)
7062 GEN_PPC64_R4(rldicl, 0x1E, 0x00),
7063 GEN_PPC64_R4(rldicr, 0x1E, 0x02),
7064 GEN_PPC64_R4(rldic, 0x1E, 0x04),
7065 GEN_PPC64_R2(rldcl, 0x1E, 0x08),
7066 GEN_PPC64_R2(rldcr, 0x1E, 0x09),
7067 GEN_PPC64_R4(rldimi, 0x1E, 0x06),
7068 #endif
7070 #undef GEN_LDX_E
7071 #define GEN_LDX_E(name, ldop, opc2, opc3, type, type2, chk) \
7072 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000001, type, type2),
7074 #if defined(TARGET_PPC64)
7075 GEN_LDX_E(ldbr, ld64ur_i64, 0x14, 0x10, PPC_NONE, PPC2_DBRX, CHK_NONE)
7077 /* HV/P7 and later only */
7078 GEN_LDX_HVRM(ldcix, ld64_i64, 0x15, 0x1b, PPC_CILDST)
7079 GEN_LDX_HVRM(lwzcix, ld32u, 0x15, 0x18, PPC_CILDST)
7080 GEN_LDX_HVRM(lhzcix, ld16u, 0x15, 0x19, PPC_CILDST)
7081 GEN_LDX_HVRM(lbzcix, ld8u, 0x15, 0x1a, PPC_CILDST)
7082 #endif
7083 GEN_LDX(lhbr, ld16ur, 0x16, 0x18, PPC_INTEGER)
7084 GEN_LDX(lwbr, ld32ur, 0x16, 0x10, PPC_INTEGER)
7086 /* External PID based load */
7087 #undef GEN_LDEPX
7088 #define GEN_LDEPX(name, ldop, opc2, opc3) \
7089 GEN_HANDLER_E(name##epx, 0x1F, opc2, opc3, \
7090 0x00000001, PPC_NONE, PPC2_BOOKE206),
7092 GEN_LDEPX(lb, DEF_MEMOP(MO_UB), 0x1F, 0x02)
7093 GEN_LDEPX(lh, DEF_MEMOP(MO_UW), 0x1F, 0x08)
7094 GEN_LDEPX(lw, DEF_MEMOP(MO_UL), 0x1F, 0x00)
7095 #if defined(TARGET_PPC64)
7096 GEN_LDEPX(ld, DEF_MEMOP(MO_UQ), 0x1D, 0x00)
7097 #endif
7099 #undef GEN_STX_E
7100 #define GEN_STX_E(name, stop, opc2, opc3, type, type2, chk) \
7101 GEN_HANDLER_E(name##x, 0x1F, opc2, opc3, 0x00000000, type, type2),
7103 #if defined(TARGET_PPC64)
7104 GEN_STX_E(stdbr, st64r_i64, 0x14, 0x14, PPC_NONE, PPC2_DBRX, CHK_NONE)
7105 GEN_STX_HVRM(stdcix, st64_i64, 0x15, 0x1f, PPC_CILDST)
7106 GEN_STX_HVRM(stwcix, st32, 0x15, 0x1c, PPC_CILDST)
7107 GEN_STX_HVRM(sthcix, st16, 0x15, 0x1d, PPC_CILDST)
7108 GEN_STX_HVRM(stbcix, st8, 0x15, 0x1e, PPC_CILDST)
7109 #endif
7110 GEN_STX(sthbr, st16r, 0x16, 0x1C, PPC_INTEGER)
7111 GEN_STX(stwbr, st32r, 0x16, 0x14, PPC_INTEGER)
7113 #undef GEN_STEPX
7114 #define GEN_STEPX(name, ldop, opc2, opc3) \
7115 GEN_HANDLER_E(name##epx, 0x1F, opc2, opc3, \
7116 0x00000001, PPC_NONE, PPC2_BOOKE206),
7118 GEN_STEPX(stb, DEF_MEMOP(MO_UB), 0x1F, 0x06)
7119 GEN_STEPX(sth, DEF_MEMOP(MO_UW), 0x1F, 0x0C)
7120 GEN_STEPX(stw, DEF_MEMOP(MO_UL), 0x1F, 0x04)
7121 #if defined(TARGET_PPC64)
7122 GEN_STEPX(std, DEF_MEMOP(MO_UQ), 0x1D, 0x04)
7123 #endif
7125 #undef GEN_CRLOGIC
7126 #define GEN_CRLOGIC(name, tcg_op, opc) \
7127 GEN_HANDLER(name, 0x13, 0x01, opc, 0x00000001, PPC_INTEGER)
7128 GEN_CRLOGIC(crand, tcg_gen_and_i32, 0x08),
7129 GEN_CRLOGIC(crandc, tcg_gen_andc_i32, 0x04),
7130 GEN_CRLOGIC(creqv, tcg_gen_eqv_i32, 0x09),
7131 GEN_CRLOGIC(crnand, tcg_gen_nand_i32, 0x07),
7132 GEN_CRLOGIC(crnor, tcg_gen_nor_i32, 0x01),
7133 GEN_CRLOGIC(cror, tcg_gen_or_i32, 0x0E),
7134 GEN_CRLOGIC(crorc, tcg_gen_orc_i32, 0x0D),
7135 GEN_CRLOGIC(crxor, tcg_gen_xor_i32, 0x06),
7137 #undef GEN_MAC_HANDLER
7138 #define GEN_MAC_HANDLER(name, opc2, opc3) \
7139 GEN_HANDLER(name, 0x04, opc2, opc3, 0x00000000, PPC_405_MAC)
7140 GEN_MAC_HANDLER(macchw, 0x0C, 0x05),
7141 GEN_MAC_HANDLER(macchwo, 0x0C, 0x15),
7142 GEN_MAC_HANDLER(macchws, 0x0C, 0x07),
7143 GEN_MAC_HANDLER(macchwso, 0x0C, 0x17),
7144 GEN_MAC_HANDLER(macchwsu, 0x0C, 0x06),
7145 GEN_MAC_HANDLER(macchwsuo, 0x0C, 0x16),
7146 GEN_MAC_HANDLER(macchwu, 0x0C, 0x04),
7147 GEN_MAC_HANDLER(macchwuo, 0x0C, 0x14),
7148 GEN_MAC_HANDLER(machhw, 0x0C, 0x01),
7149 GEN_MAC_HANDLER(machhwo, 0x0C, 0x11),
7150 GEN_MAC_HANDLER(machhws, 0x0C, 0x03),
7151 GEN_MAC_HANDLER(machhwso, 0x0C, 0x13),
7152 GEN_MAC_HANDLER(machhwsu, 0x0C, 0x02),
7153 GEN_MAC_HANDLER(machhwsuo, 0x0C, 0x12),
7154 GEN_MAC_HANDLER(machhwu, 0x0C, 0x00),
7155 GEN_MAC_HANDLER(machhwuo, 0x0C, 0x10),
7156 GEN_MAC_HANDLER(maclhw, 0x0C, 0x0D),
7157 GEN_MAC_HANDLER(maclhwo, 0x0C, 0x1D),
7158 GEN_MAC_HANDLER(maclhws, 0x0C, 0x0F),
7159 GEN_MAC_HANDLER(maclhwso, 0x0C, 0x1F),
7160 GEN_MAC_HANDLER(maclhwu, 0x0C, 0x0C),
7161 GEN_MAC_HANDLER(maclhwuo, 0x0C, 0x1C),
7162 GEN_MAC_HANDLER(maclhwsu, 0x0C, 0x0E),
7163 GEN_MAC_HANDLER(maclhwsuo, 0x0C, 0x1E),
7164 GEN_MAC_HANDLER(nmacchw, 0x0E, 0x05),
7165 GEN_MAC_HANDLER(nmacchwo, 0x0E, 0x15),
7166 GEN_MAC_HANDLER(nmacchws, 0x0E, 0x07),
7167 GEN_MAC_HANDLER(nmacchwso, 0x0E, 0x17),
7168 GEN_MAC_HANDLER(nmachhw, 0x0E, 0x01),
7169 GEN_MAC_HANDLER(nmachhwo, 0x0E, 0x11),
7170 GEN_MAC_HANDLER(nmachhws, 0x0E, 0x03),
7171 GEN_MAC_HANDLER(nmachhwso, 0x0E, 0x13),
7172 GEN_MAC_HANDLER(nmaclhw, 0x0E, 0x0D),
7173 GEN_MAC_HANDLER(nmaclhwo, 0x0E, 0x1D),
7174 GEN_MAC_HANDLER(nmaclhws, 0x0E, 0x0F),
7175 GEN_MAC_HANDLER(nmaclhwso, 0x0E, 0x1F),
7176 GEN_MAC_HANDLER(mulchw, 0x08, 0x05),
7177 GEN_MAC_HANDLER(mulchwu, 0x08, 0x04),
7178 GEN_MAC_HANDLER(mulhhw, 0x08, 0x01),
7179 GEN_MAC_HANDLER(mulhhwu, 0x08, 0x00),
7180 GEN_MAC_HANDLER(mullhw, 0x08, 0x0D),
7181 GEN_MAC_HANDLER(mullhwu, 0x08, 0x0C),
7183 GEN_HANDLER2_E(tbegin, "tbegin", 0x1F, 0x0E, 0x14, 0x01DFF800, \
7184 PPC_NONE, PPC2_TM),
7185 GEN_HANDLER2_E(tend, "tend", 0x1F, 0x0E, 0x15, 0x01FFF800, \
7186 PPC_NONE, PPC2_TM),
7187 GEN_HANDLER2_E(tabort, "tabort", 0x1F, 0x0E, 0x1C, 0x03E0F800, \
7188 PPC_NONE, PPC2_TM),
7189 GEN_HANDLER2_E(tabortwc, "tabortwc", 0x1F, 0x0E, 0x18, 0x00000000, \
7190 PPC_NONE, PPC2_TM),
7191 GEN_HANDLER2_E(tabortwci, "tabortwci", 0x1F, 0x0E, 0x1A, 0x00000000, \
7192 PPC_NONE, PPC2_TM),
7193 GEN_HANDLER2_E(tabortdc, "tabortdc", 0x1F, 0x0E, 0x19, 0x00000000, \
7194 PPC_NONE, PPC2_TM),
7195 GEN_HANDLER2_E(tabortdci, "tabortdci", 0x1F, 0x0E, 0x1B, 0x00000000, \
7196 PPC_NONE, PPC2_TM),
7197 GEN_HANDLER2_E(tsr, "tsr", 0x1F, 0x0E, 0x17, 0x03DFF800, \
7198 PPC_NONE, PPC2_TM),
7199 GEN_HANDLER2_E(tcheck, "tcheck", 0x1F, 0x0E, 0x16, 0x007FF800, \
7200 PPC_NONE, PPC2_TM),
7201 GEN_HANDLER2_E(treclaim, "treclaim", 0x1F, 0x0E, 0x1D, 0x03E0F800, \
7202 PPC_NONE, PPC2_TM),
7203 GEN_HANDLER2_E(trechkpt, "trechkpt", 0x1F, 0x0E, 0x1F, 0x03FFF800, \
7204 PPC_NONE, PPC2_TM),
7206 #include "translate/fp-ops.c.inc"
7208 #include "translate/vmx-ops.c.inc"
7210 #include "translate/vsx-ops.c.inc"
7212 #include "translate/spe-ops.c.inc"
7215 /*****************************************************************************/
7216 /* Opcode types */
7217 enum {
7218 PPC_DIRECT = 0, /* Opcode routine */
7219 PPC_INDIRECT = 1, /* Indirect opcode table */
7222 #define PPC_OPCODE_MASK 0x3
7224 static inline int is_indirect_opcode(void *handler)
7226 return ((uintptr_t)handler & PPC_OPCODE_MASK) == PPC_INDIRECT;
7229 static inline opc_handler_t **ind_table(void *handler)
7231 return (opc_handler_t **)((uintptr_t)handler & ~PPC_OPCODE_MASK);
7234 /* Instruction table creation */
7235 /* Opcodes tables creation */
7236 static void fill_new_table(opc_handler_t **table, int len)
7238 int i;
7240 for (i = 0; i < len; i++) {
7241 table[i] = &invalid_handler;
7245 static int create_new_table(opc_handler_t **table, unsigned char idx)
7247 opc_handler_t **tmp;
7249 tmp = g_new(opc_handler_t *, PPC_CPU_INDIRECT_OPCODES_LEN);
7250 fill_new_table(tmp, PPC_CPU_INDIRECT_OPCODES_LEN);
7251 table[idx] = (opc_handler_t *)((uintptr_t)tmp | PPC_INDIRECT);
7253 return 0;
7256 static int insert_in_table(opc_handler_t **table, unsigned char idx,
7257 opc_handler_t *handler)
7259 if (table[idx] != &invalid_handler) {
7260 return -1;
7262 table[idx] = handler;
7264 return 0;
7267 static int register_direct_insn(opc_handler_t **ppc_opcodes,
7268 unsigned char idx, opc_handler_t *handler)
7270 if (insert_in_table(ppc_opcodes, idx, handler) < 0) {
7271 printf("*** ERROR: opcode %02x already assigned in main "
7272 "opcode table\n", idx);
7273 return -1;
7276 return 0;
7279 static int register_ind_in_table(opc_handler_t **table,
7280 unsigned char idx1, unsigned char idx2,
7281 opc_handler_t *handler)
7283 if (table[idx1] == &invalid_handler) {
7284 if (create_new_table(table, idx1) < 0) {
7285 printf("*** ERROR: unable to create indirect table "
7286 "idx=%02x\n", idx1);
7287 return -1;
7289 } else {
7290 if (!is_indirect_opcode(table[idx1])) {
7291 printf("*** ERROR: idx %02x already assigned to a direct "
7292 "opcode\n", idx1);
7293 return -1;
7296 if (handler != NULL &&
7297 insert_in_table(ind_table(table[idx1]), idx2, handler) < 0) {
7298 printf("*** ERROR: opcode %02x already assigned in "
7299 "opcode table %02x\n", idx2, idx1);
7300 return -1;
7303 return 0;
7306 static int register_ind_insn(opc_handler_t **ppc_opcodes,
7307 unsigned char idx1, unsigned char idx2,
7308 opc_handler_t *handler)
7310 return register_ind_in_table(ppc_opcodes, idx1, idx2, handler);
7313 static int register_dblind_insn(opc_handler_t **ppc_opcodes,
7314 unsigned char idx1, unsigned char idx2,
7315 unsigned char idx3, opc_handler_t *handler)
7317 if (register_ind_in_table(ppc_opcodes, idx1, idx2, NULL) < 0) {
7318 printf("*** ERROR: unable to join indirect table idx "
7319 "[%02x-%02x]\n", idx1, idx2);
7320 return -1;
7322 if (register_ind_in_table(ind_table(ppc_opcodes[idx1]), idx2, idx3,
7323 handler) < 0) {
7324 printf("*** ERROR: unable to insert opcode "
7325 "[%02x-%02x-%02x]\n", idx1, idx2, idx3);
7326 return -1;
7329 return 0;
7332 static int register_trplind_insn(opc_handler_t **ppc_opcodes,
7333 unsigned char idx1, unsigned char idx2,
7334 unsigned char idx3, unsigned char idx4,
7335 opc_handler_t *handler)
7337 opc_handler_t **table;
7339 if (register_ind_in_table(ppc_opcodes, idx1, idx2, NULL) < 0) {
7340 printf("*** ERROR: unable to join indirect table idx "
7341 "[%02x-%02x]\n", idx1, idx2);
7342 return -1;
7344 table = ind_table(ppc_opcodes[idx1]);
7345 if (register_ind_in_table(table, idx2, idx3, NULL) < 0) {
7346 printf("*** ERROR: unable to join 2nd-level indirect table idx "
7347 "[%02x-%02x-%02x]\n", idx1, idx2, idx3);
7348 return -1;
7350 table = ind_table(table[idx2]);
7351 if (register_ind_in_table(table, idx3, idx4, handler) < 0) {
7352 printf("*** ERROR: unable to insert opcode "
7353 "[%02x-%02x-%02x-%02x]\n", idx1, idx2, idx3, idx4);
7354 return -1;
7356 return 0;
7358 static int register_insn(opc_handler_t **ppc_opcodes, opcode_t *insn)
7360 if (insn->opc2 != 0xFF) {
7361 if (insn->opc3 != 0xFF) {
7362 if (insn->opc4 != 0xFF) {
7363 if (register_trplind_insn(ppc_opcodes, insn->opc1, insn->opc2,
7364 insn->opc3, insn->opc4,
7365 &insn->handler) < 0) {
7366 return -1;
7368 } else {
7369 if (register_dblind_insn(ppc_opcodes, insn->opc1, insn->opc2,
7370 insn->opc3, &insn->handler) < 0) {
7371 return -1;
7374 } else {
7375 if (register_ind_insn(ppc_opcodes, insn->opc1,
7376 insn->opc2, &insn->handler) < 0) {
7377 return -1;
7380 } else {
7381 if (register_direct_insn(ppc_opcodes, insn->opc1, &insn->handler) < 0) {
7382 return -1;
7386 return 0;
7389 static int test_opcode_table(opc_handler_t **table, int len)
7391 int i, count, tmp;
7393 for (i = 0, count = 0; i < len; i++) {
7394 /* Consistency fixup */
7395 if (table[i] == NULL) {
7396 table[i] = &invalid_handler;
7398 if (table[i] != &invalid_handler) {
7399 if (is_indirect_opcode(table[i])) {
7400 tmp = test_opcode_table(ind_table(table[i]),
7401 PPC_CPU_INDIRECT_OPCODES_LEN);
7402 if (tmp == 0) {
7403 free(table[i]);
7404 table[i] = &invalid_handler;
7405 } else {
7406 count++;
7408 } else {
7409 count++;
7414 return count;
7417 static void fix_opcode_tables(opc_handler_t **ppc_opcodes)
7419 if (test_opcode_table(ppc_opcodes, PPC_CPU_OPCODES_LEN) == 0) {
7420 printf("*** WARNING: no opcode defined !\n");
7424 /*****************************************************************************/
7425 void create_ppc_opcodes(PowerPCCPU *cpu, Error **errp)
7427 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
7428 opcode_t *opc;
7430 fill_new_table(cpu->opcodes, PPC_CPU_OPCODES_LEN);
7431 for (opc = opcodes; opc < &opcodes[ARRAY_SIZE(opcodes)]; opc++) {
7432 if (((opc->handler.type & pcc->insns_flags) != 0) ||
7433 ((opc->handler.type2 & pcc->insns_flags2) != 0)) {
7434 if (register_insn(cpu->opcodes, opc) < 0) {
7435 error_setg(errp, "ERROR initializing PowerPC instruction "
7436 "0x%02x 0x%02x 0x%02x", opc->opc1, opc->opc2,
7437 opc->opc3);
7438 return;
7442 fix_opcode_tables(cpu->opcodes);
7443 fflush(stdout);
7444 fflush(stderr);
7447 void destroy_ppc_opcodes(PowerPCCPU *cpu)
7449 opc_handler_t **table, **table_2;
7450 int i, j, k;
7452 for (i = 0; i < PPC_CPU_OPCODES_LEN; i++) {
7453 if (cpu->opcodes[i] == &invalid_handler) {
7454 continue;
7456 if (is_indirect_opcode(cpu->opcodes[i])) {
7457 table = ind_table(cpu->opcodes[i]);
7458 for (j = 0; j < PPC_CPU_INDIRECT_OPCODES_LEN; j++) {
7459 if (table[j] == &invalid_handler) {
7460 continue;
7462 if (is_indirect_opcode(table[j])) {
7463 table_2 = ind_table(table[j]);
7464 for (k = 0; k < PPC_CPU_INDIRECT_OPCODES_LEN; k++) {
7465 if (table_2[k] != &invalid_handler &&
7466 is_indirect_opcode(table_2[k])) {
7467 g_free((opc_handler_t *)((uintptr_t)table_2[k] &
7468 ~PPC_INDIRECT));
7471 g_free((opc_handler_t *)((uintptr_t)table[j] &
7472 ~PPC_INDIRECT));
7475 g_free((opc_handler_t *)((uintptr_t)cpu->opcodes[i] &
7476 ~PPC_INDIRECT));
7481 int ppc_fixup_cpu(PowerPCCPU *cpu)
7483 CPUPPCState *env = &cpu->env;
7486 * TCG doesn't (yet) emulate some groups of instructions that are
7487 * implemented on some otherwise supported CPUs (e.g. VSX and
7488 * decimal floating point instructions on POWER7). We remove
7489 * unsupported instruction groups from the cpu state's instruction
7490 * masks and hope the guest can cope. For at least the pseries
7491 * machine, the unavailability of these instructions can be
7492 * advertised to the guest via the device tree.
7494 if ((env->insns_flags & ~PPC_TCG_INSNS)
7495 || (env->insns_flags2 & ~PPC_TCG_INSNS2)) {
7496 warn_report("Disabling some instructions which are not "
7497 "emulated by TCG (0x%" PRIx64 ", 0x%" PRIx64 ")",
7498 env->insns_flags & ~PPC_TCG_INSNS,
7499 env->insns_flags2 & ~PPC_TCG_INSNS2);
7501 env->insns_flags &= PPC_TCG_INSNS;
7502 env->insns_flags2 &= PPC_TCG_INSNS2;
7503 return 0;
7506 static bool decode_legacy(PowerPCCPU *cpu, DisasContext *ctx, uint32_t insn)
7508 opc_handler_t **table, *handler;
7509 uint32_t inval;
7511 ctx->opcode = insn;
7513 LOG_DISAS("translate opcode %08x (%02x %02x %02x %02x) (%s)\n",
7514 insn, opc1(insn), opc2(insn), opc3(insn), opc4(insn),
7515 ctx->le_mode ? "little" : "big");
7517 table = cpu->opcodes;
7518 handler = table[opc1(insn)];
7519 if (is_indirect_opcode(handler)) {
7520 table = ind_table(handler);
7521 handler = table[opc2(insn)];
7522 if (is_indirect_opcode(handler)) {
7523 table = ind_table(handler);
7524 handler = table[opc3(insn)];
7525 if (is_indirect_opcode(handler)) {
7526 table = ind_table(handler);
7527 handler = table[opc4(insn)];
7532 /* Is opcode *REALLY* valid ? */
7533 if (unlikely(handler->handler == &gen_invalid)) {
7534 qemu_log_mask(LOG_GUEST_ERROR, "invalid/unsupported opcode: "
7535 "%02x - %02x - %02x - %02x (%08x) "
7536 TARGET_FMT_lx "\n",
7537 opc1(insn), opc2(insn), opc3(insn), opc4(insn),
7538 insn, ctx->cia);
7539 return false;
7542 if (unlikely(handler->type & (PPC_SPE | PPC_SPE_SINGLE | PPC_SPE_DOUBLE)
7543 && Rc(insn))) {
7544 inval = handler->inval2;
7545 } else {
7546 inval = handler->inval1;
7549 if (unlikely((insn & inval) != 0)) {
7550 qemu_log_mask(LOG_GUEST_ERROR, "invalid bits: %08x for opcode: "
7551 "%02x - %02x - %02x - %02x (%08x) "
7552 TARGET_FMT_lx "\n", insn & inval,
7553 opc1(insn), opc2(insn), opc3(insn), opc4(insn),
7554 insn, ctx->cia);
7555 return false;
7558 handler->handler(ctx);
7559 return true;
7562 static void ppc_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)
7564 DisasContext *ctx = container_of(dcbase, DisasContext, base);
7565 CPUPPCState *env = cs->env_ptr;
7566 uint32_t hflags = ctx->base.tb->flags;
7568 ctx->spr_cb = env->spr_cb;
7569 ctx->pr = (hflags >> HFLAGS_PR) & 1;
7570 ctx->mem_idx = (hflags >> HFLAGS_DMMU_IDX) & 7;
7571 ctx->dr = (hflags >> HFLAGS_DR) & 1;
7572 ctx->hv = (hflags >> HFLAGS_HV) & 1;
7573 ctx->insns_flags = env->insns_flags;
7574 ctx->insns_flags2 = env->insns_flags2;
7575 ctx->access_type = -1;
7576 ctx->need_access_type = !mmu_is_64bit(env->mmu_model);
7577 ctx->le_mode = (hflags >> HFLAGS_LE) & 1;
7578 ctx->default_tcg_memop_mask = ctx->le_mode ? MO_LE : MO_BE;
7579 ctx->flags = env->flags;
7580 #if defined(TARGET_PPC64)
7581 ctx->sf_mode = (hflags >> HFLAGS_64) & 1;
7582 ctx->has_cfar = !!(env->flags & POWERPC_FLAG_CFAR);
7583 #endif
7584 ctx->lazy_tlb_flush = env->mmu_model == POWERPC_MMU_32B
7585 || env->mmu_model & POWERPC_MMU_64;
7587 ctx->fpu_enabled = (hflags >> HFLAGS_FP) & 1;
7588 ctx->spe_enabled = (hflags >> HFLAGS_SPE) & 1;
7589 ctx->altivec_enabled = (hflags >> HFLAGS_VR) & 1;
7590 ctx->vsx_enabled = (hflags >> HFLAGS_VSX) & 1;
7591 ctx->tm_enabled = (hflags >> HFLAGS_TM) & 1;
7592 ctx->gtse = (hflags >> HFLAGS_GTSE) & 1;
7593 ctx->hr = (hflags >> HFLAGS_HR) & 1;
7594 ctx->mmcr0_pmcc0 = (hflags >> HFLAGS_PMCC0) & 1;
7595 ctx->mmcr0_pmcc1 = (hflags >> HFLAGS_PMCC1) & 1;
7596 ctx->pmu_insn_cnt = (hflags >> HFLAGS_INSN_CNT) & 1;
7598 ctx->singlestep_enabled = 0;
7599 if ((hflags >> HFLAGS_SE) & 1) {
7600 ctx->singlestep_enabled |= CPU_SINGLE_STEP;
7601 ctx->base.max_insns = 1;
7603 if ((hflags >> HFLAGS_BE) & 1) {
7604 ctx->singlestep_enabled |= CPU_BRANCH_STEP;
7608 static void ppc_tr_tb_start(DisasContextBase *db, CPUState *cs)
7612 static void ppc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
7614 tcg_gen_insn_start(dcbase->pc_next);
7617 static bool is_prefix_insn(DisasContext *ctx, uint32_t insn)
7619 REQUIRE_INSNS_FLAGS2(ctx, ISA310);
7620 return opc1(insn) == 1;
7623 static void ppc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
7625 DisasContext *ctx = container_of(dcbase, DisasContext, base);
7626 PowerPCCPU *cpu = POWERPC_CPU(cs);
7627 CPUPPCState *env = cs->env_ptr;
7628 target_ulong pc;
7629 uint32_t insn;
7630 bool ok;
7632 LOG_DISAS("----------------\n");
7633 LOG_DISAS("nip=" TARGET_FMT_lx " super=%d ir=%d\n",
7634 ctx->base.pc_next, ctx->mem_idx, (int)msr_ir);
7636 ctx->cia = pc = ctx->base.pc_next;
7637 insn = translator_ldl_swap(env, dcbase, pc, need_byteswap(ctx));
7638 ctx->base.pc_next = pc += 4;
7640 if (!is_prefix_insn(ctx, insn)) {
7641 ok = (decode_insn32(ctx, insn) ||
7642 decode_legacy(cpu, ctx, insn));
7643 } else if ((pc & 63) == 0) {
7645 * Power v3.1, section 1.9 Exceptions:
7646 * attempt to execute a prefixed instruction that crosses a
7647 * 64-byte address boundary (system alignment error).
7649 gen_exception_err(ctx, POWERPC_EXCP_ALIGN, POWERPC_EXCP_ALIGN_INSN);
7650 ok = true;
7651 } else {
7652 uint32_t insn2 = translator_ldl_swap(env, dcbase, pc,
7653 need_byteswap(ctx));
7654 ctx->base.pc_next = pc += 4;
7655 ok = decode_insn64(ctx, deposit64(insn2, 32, 32, insn));
7657 if (!ok) {
7658 gen_invalid(ctx);
7661 /* End the TB when crossing a page boundary. */
7662 if (ctx->base.is_jmp == DISAS_NEXT && !(pc & ~TARGET_PAGE_MASK)) {
7663 ctx->base.is_jmp = DISAS_TOO_MANY;
7666 translator_loop_temp_check(&ctx->base);
7669 static void ppc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
7671 DisasContext *ctx = container_of(dcbase, DisasContext, base);
7672 DisasJumpType is_jmp = ctx->base.is_jmp;
7673 target_ulong nip = ctx->base.pc_next;
7675 if (is_jmp == DISAS_NORETURN) {
7676 /* We have already exited the TB. */
7677 return;
7680 /* Honor single stepping. */
7681 if (unlikely(ctx->singlestep_enabled & CPU_SINGLE_STEP)
7682 && (nip <= 0x100 || nip > 0xf00)) {
7683 switch (is_jmp) {
7684 case DISAS_TOO_MANY:
7685 case DISAS_EXIT_UPDATE:
7686 case DISAS_CHAIN_UPDATE:
7687 gen_update_nip(ctx, nip);
7688 break;
7689 case DISAS_EXIT:
7690 case DISAS_CHAIN:
7691 break;
7692 default:
7693 g_assert_not_reached();
7696 gen_debug_exception(ctx);
7697 return;
7700 switch (is_jmp) {
7701 case DISAS_TOO_MANY:
7702 if (use_goto_tb(ctx, nip)) {
7703 pmu_count_insns(ctx);
7704 tcg_gen_goto_tb(0);
7705 gen_update_nip(ctx, nip);
7706 tcg_gen_exit_tb(ctx->base.tb, 0);
7707 break;
7709 /* fall through */
7710 case DISAS_CHAIN_UPDATE:
7711 gen_update_nip(ctx, nip);
7712 /* fall through */
7713 case DISAS_CHAIN:
7715 * tcg_gen_lookup_and_goto_ptr will exit the TB if
7716 * CF_NO_GOTO_PTR is set. Count insns now.
7718 if (ctx->base.tb->flags & CF_NO_GOTO_PTR) {
7719 pmu_count_insns(ctx);
7722 tcg_gen_lookup_and_goto_ptr();
7723 break;
7725 case DISAS_EXIT_UPDATE:
7726 gen_update_nip(ctx, nip);
7727 /* fall through */
7728 case DISAS_EXIT:
7729 pmu_count_insns(ctx);
7730 tcg_gen_exit_tb(NULL, 0);
7731 break;
7733 default:
7734 g_assert_not_reached();
7738 static void ppc_tr_disas_log(const DisasContextBase *dcbase,
7739 CPUState *cs, FILE *logfile)
7741 fprintf(logfile, "IN: %s\n", lookup_symbol(dcbase->pc_first));
7742 target_disas(logfile, cs, dcbase->pc_first, dcbase->tb->size);
7745 static const TranslatorOps ppc_tr_ops = {
7746 .init_disas_context = ppc_tr_init_disas_context,
7747 .tb_start = ppc_tr_tb_start,
7748 .insn_start = ppc_tr_insn_start,
7749 .translate_insn = ppc_tr_translate_insn,
7750 .tb_stop = ppc_tr_tb_stop,
7751 .disas_log = ppc_tr_disas_log,
7754 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
7756 DisasContext ctx;
7758 translator_loop(&ppc_tr_ops, &ctx.base, cs, tb, max_insns);
7761 void restore_state_to_opc(CPUPPCState *env, TranslationBlock *tb,
7762 target_ulong *data)
7764 env->nip = data[0];