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