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