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