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