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