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