2 * Xilinx MicroBlaze emulation for qemu: main translation routines.
4 * Copyright (c) 2009 Edgar E. Iglesias.
5 * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "disas/disas.h"
25 #include "microblaze-decode.h"
33 #if DISAS_MB && !SIM_COMPAT
34 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
36 # define LOG_DIS(...) do { } while (0)
41 #define EXTRACT_FIELD(src, start, end) \
42 (((src) >> start) & ((1 << (end - start + 1)) - 1))
44 static TCGv env_debug;
45 static TCGv_ptr cpu_env;
46 static TCGv cpu_R[32];
47 static TCGv cpu_SR[18];
49 static TCGv env_btaken;
50 static TCGv env_btarget;
51 static TCGv env_iflags;
53 #include "exec/gen-icount.h"
55 /* This is the state at translation time. */
56 typedef struct DisasContext {
67 unsigned int cpustate_changed;
68 unsigned int delayed_branch;
69 unsigned int tb_flags, synced_flags; /* tb dependent flags. */
70 unsigned int clear_imm;
75 #define JMP_DIRECT_CC 2
76 #define JMP_INDIRECT 3
80 int abort_at_next_insn;
82 struct TranslationBlock *tb;
83 int singlestep_enabled;
86 static const char *regnames[] =
88 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
89 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
90 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
91 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
94 static const char *special_regnames[] =
96 "rpc", "rmsr", "sr2", "sr3", "sr4", "sr5", "sr6", "sr7",
97 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
98 "sr16", "sr17", "sr18"
101 /* Sign extend at translation time. */
102 static inline int sign_extend(unsigned int val, unsigned int width)
114 static inline void t_sync_flags(DisasContext *dc)
116 /* Synch the tb dependent flags between translator and runtime. */
117 if (dc->tb_flags != dc->synced_flags) {
118 tcg_gen_movi_tl(env_iflags, dc->tb_flags);
119 dc->synced_flags = dc->tb_flags;
123 static inline void t_gen_raise_exception(DisasContext *dc, uint32_t index)
125 TCGv_i32 tmp = tcg_const_i32(index);
128 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
129 gen_helper_raise_exception(cpu_env, tmp);
130 tcg_temp_free_i32(tmp);
131 dc->is_jmp = DISAS_UPDATE;
134 static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
136 TranslationBlock *tb;
138 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
140 tcg_gen_movi_tl(cpu_SR[SR_PC], dest);
141 tcg_gen_exit_tb((uintptr_t)tb + n);
143 tcg_gen_movi_tl(cpu_SR[SR_PC], dest);
148 static void read_carry(DisasContext *dc, TCGv d)
150 tcg_gen_shri_tl(d, cpu_SR[SR_MSR], 31);
154 * write_carry sets the carry bits in MSR based on bit 0 of v.
155 * v[31:1] are ignored.
157 static void write_carry(DisasContext *dc, TCGv v)
159 TCGv t0 = tcg_temp_new();
160 tcg_gen_shli_tl(t0, v, 31);
161 tcg_gen_sari_tl(t0, t0, 31);
162 tcg_gen_andi_tl(t0, t0, (MSR_C | MSR_CC));
163 tcg_gen_andi_tl(cpu_SR[SR_MSR], cpu_SR[SR_MSR],
165 tcg_gen_or_tl(cpu_SR[SR_MSR], cpu_SR[SR_MSR], t0);
169 static void write_carryi(DisasContext *dc, bool carry)
171 TCGv t0 = tcg_temp_new();
172 tcg_gen_movi_tl(t0, carry);
177 /* True if ALU operand b is a small immediate that may deserve
179 static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
181 /* Immediate insn without the imm prefix ? */
182 return dc->type_b && !(dc->tb_flags & IMM_FLAG);
185 static inline TCGv *dec_alu_op_b(DisasContext *dc)
188 if (dc->tb_flags & IMM_FLAG)
189 tcg_gen_ori_tl(env_imm, env_imm, dc->imm);
191 tcg_gen_movi_tl(env_imm, (int32_t)((int16_t)dc->imm));
194 return &cpu_R[dc->rb];
197 static void dec_add(DisasContext *dc)
205 LOG_DIS("add%s%s%s r%d r%d r%d\n",
206 dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
207 dc->rd, dc->ra, dc->rb);
209 /* Take care of the easy cases first. */
211 /* k - keep carry, no need to update MSR. */
212 /* If rd == r0, it's a nop. */
214 tcg_gen_add_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
217 /* c - Add carry into the result. */
221 tcg_gen_add_tl(cpu_R[dc->rd], cpu_R[dc->rd], cf);
228 /* From now on, we can assume k is zero. So we need to update MSR. */
234 tcg_gen_movi_tl(cf, 0);
238 TCGv ncf = tcg_temp_new();
239 gen_helper_carry(ncf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
240 tcg_gen_add_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
241 tcg_gen_add_tl(cpu_R[dc->rd], cpu_R[dc->rd], cf);
242 write_carry(dc, ncf);
245 gen_helper_carry(cf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
251 static void dec_sub(DisasContext *dc)
253 unsigned int u, cmp, k, c;
259 cmp = (dc->imm & 1) && (!dc->type_b) && k;
262 LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
265 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
267 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
272 LOG_DIS("sub%s%s r%d, r%d r%d\n",
273 k ? "k" : "", c ? "c" : "", dc->rd, dc->ra, dc->rb);
275 /* Take care of the easy cases first. */
277 /* k - keep carry, no need to update MSR. */
278 /* If rd == r0, it's a nop. */
280 tcg_gen_sub_tl(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
283 /* c - Add carry into the result. */
287 tcg_gen_add_tl(cpu_R[dc->rd], cpu_R[dc->rd], cf);
294 /* From now on, we can assume k is zero. So we need to update MSR. */
295 /* Extract carry. And complement a into na. */
301 tcg_gen_movi_tl(cf, 1);
304 /* d = b + ~a + c. carry defaults to 1. */
305 tcg_gen_not_tl(na, cpu_R[dc->ra]);
308 TCGv ncf = tcg_temp_new();
309 gen_helper_carry(ncf, na, *(dec_alu_op_b(dc)), cf);
310 tcg_gen_add_tl(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
311 tcg_gen_add_tl(cpu_R[dc->rd], cpu_R[dc->rd], cf);
312 write_carry(dc, ncf);
315 gen_helper_carry(cf, na, *(dec_alu_op_b(dc)), cf);
322 static void dec_pattern(DisasContext *dc)
327 if ((dc->tb_flags & MSR_EE_FLAG)
328 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
329 && !((dc->env->pvr.regs[2] & PVR2_USE_PCMP_INSTR))) {
330 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
331 t_gen_raise_exception(dc, EXCP_HW_EXCP);
334 mode = dc->opcode & 3;
338 LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
340 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
343 LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
345 TCGv t0 = tcg_temp_local_new();
346 l1 = gen_new_label();
347 tcg_gen_movi_tl(t0, 1);
348 tcg_gen_brcond_tl(TCG_COND_EQ,
349 cpu_R[dc->ra], cpu_R[dc->rb], l1);
350 tcg_gen_movi_tl(t0, 0);
352 tcg_gen_mov_tl(cpu_R[dc->rd], t0);
357 LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
358 l1 = gen_new_label();
360 TCGv t0 = tcg_temp_local_new();
361 tcg_gen_movi_tl(t0, 1);
362 tcg_gen_brcond_tl(TCG_COND_NE,
363 cpu_R[dc->ra], cpu_R[dc->rb], l1);
364 tcg_gen_movi_tl(t0, 0);
366 tcg_gen_mov_tl(cpu_R[dc->rd], t0);
372 "unsupported pattern insn opcode=%x\n", dc->opcode);
377 static void dec_and(DisasContext *dc)
381 if (!dc->type_b && (dc->imm & (1 << 10))) {
386 not = dc->opcode & (1 << 1);
387 LOG_DIS("and%s\n", not ? "n" : "");
393 TCGv t = tcg_temp_new();
394 tcg_gen_not_tl(t, *(dec_alu_op_b(dc)));
395 tcg_gen_and_tl(cpu_R[dc->rd], cpu_R[dc->ra], t);
398 tcg_gen_and_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
401 static void dec_or(DisasContext *dc)
403 if (!dc->type_b && (dc->imm & (1 << 10))) {
408 LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
410 tcg_gen_or_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
413 static void dec_xor(DisasContext *dc)
415 if (!dc->type_b && (dc->imm & (1 << 10))) {
420 LOG_DIS("xor r%d\n", dc->rd);
422 tcg_gen_xor_tl(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
425 static inline void msr_read(DisasContext *dc, TCGv d)
427 tcg_gen_mov_tl(d, cpu_SR[SR_MSR]);
430 static inline void msr_write(DisasContext *dc, TCGv v)
435 dc->cpustate_changed = 1;
436 /* PVR bit is not writable. */
437 tcg_gen_andi_tl(t, v, ~MSR_PVR);
438 tcg_gen_andi_tl(cpu_SR[SR_MSR], cpu_SR[SR_MSR], MSR_PVR);
439 tcg_gen_or_tl(cpu_SR[SR_MSR], cpu_SR[SR_MSR], v);
443 static void dec_msr(DisasContext *dc)
446 unsigned int sr, to, rn;
447 int mem_index = cpu_mmu_index(dc->env);
449 sr = dc->imm & ((1 << 14) - 1);
450 to = dc->imm & (1 << 14);
453 dc->cpustate_changed = 1;
455 /* msrclr and msrset. */
456 if (!(dc->imm & (1 << 15))) {
457 unsigned int clr = dc->ir & (1 << 16);
459 LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
462 if (!(dc->env->pvr.regs[2] & PVR2_USE_MSR_INSTR)) {
467 if ((dc->tb_flags & MSR_EE_FLAG)
468 && mem_index == MMU_USER_IDX && (dc->imm != 4 && dc->imm != 0)) {
469 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
470 t_gen_raise_exception(dc, EXCP_HW_EXCP);
475 msr_read(dc, cpu_R[dc->rd]);
480 tcg_gen_mov_tl(t1, *(dec_alu_op_b(dc)));
483 tcg_gen_not_tl(t1, t1);
484 tcg_gen_and_tl(t0, t0, t1);
486 tcg_gen_or_tl(t0, t0, t1);
490 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc + 4);
491 dc->is_jmp = DISAS_UPDATE;
496 if ((dc->tb_flags & MSR_EE_FLAG)
497 && mem_index == MMU_USER_IDX) {
498 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
499 t_gen_raise_exception(dc, EXCP_HW_EXCP);
504 #if !defined(CONFIG_USER_ONLY)
505 /* Catch read/writes to the mmu block. */
506 if ((sr & ~0xff) == 0x1000) {
508 LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
510 gen_helper_mmu_write(cpu_env, tcg_const_tl(sr), cpu_R[dc->ra]);
512 gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tcg_const_tl(sr));
518 LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
523 msr_write(dc, cpu_R[dc->ra]);
526 tcg_gen_mov_tl(cpu_SR[SR_EAR], cpu_R[dc->ra]);
529 tcg_gen_mov_tl(cpu_SR[SR_ESR], cpu_R[dc->ra]);
532 tcg_gen_andi_tl(cpu_SR[SR_FSR], cpu_R[dc->ra], 31);
535 tcg_gen_st_tl(cpu_R[dc->ra], cpu_env, offsetof(CPUMBState, slr));
538 tcg_gen_st_tl(cpu_R[dc->ra], cpu_env, offsetof(CPUMBState, shr));
541 cpu_abort(dc->env, "unknown mts reg %x\n", sr);
545 LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
549 tcg_gen_movi_tl(cpu_R[dc->rd], dc->pc);
552 msr_read(dc, cpu_R[dc->rd]);
555 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_EAR]);
558 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_ESR]);
561 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_FSR]);
564 tcg_gen_mov_tl(cpu_R[dc->rd], cpu_SR[SR_BTR]);
567 tcg_gen_ld_tl(cpu_R[dc->rd], cpu_env, offsetof(CPUMBState, slr));
570 tcg_gen_ld_tl(cpu_R[dc->rd], cpu_env, offsetof(CPUMBState, shr));
586 tcg_gen_ld_tl(cpu_R[dc->rd],
587 cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
590 cpu_abort(dc->env, "unknown mfs reg %x\n", sr);
596 tcg_gen_movi_tl(cpu_R[0], 0);
600 /* 64-bit signed mul, lower result in d and upper in d2. */
601 static void t_gen_muls(TCGv d, TCGv d2, TCGv a, TCGv b)
605 t0 = tcg_temp_new_i64();
606 t1 = tcg_temp_new_i64();
608 tcg_gen_ext_i32_i64(t0, a);
609 tcg_gen_ext_i32_i64(t1, b);
610 tcg_gen_mul_i64(t0, t0, t1);
612 tcg_gen_trunc_i64_i32(d, t0);
613 tcg_gen_shri_i64(t0, t0, 32);
614 tcg_gen_trunc_i64_i32(d2, t0);
616 tcg_temp_free_i64(t0);
617 tcg_temp_free_i64(t1);
620 /* 64-bit unsigned muls, lower result in d and upper in d2. */
621 static void t_gen_mulu(TCGv d, TCGv d2, TCGv a, TCGv b)
625 t0 = tcg_temp_new_i64();
626 t1 = tcg_temp_new_i64();
628 tcg_gen_extu_i32_i64(t0, a);
629 tcg_gen_extu_i32_i64(t1, b);
630 tcg_gen_mul_i64(t0, t0, t1);
632 tcg_gen_trunc_i64_i32(d, t0);
633 tcg_gen_shri_i64(t0, t0, 32);
634 tcg_gen_trunc_i64_i32(d2, t0);
636 tcg_temp_free_i64(t0);
637 tcg_temp_free_i64(t1);
640 /* Multiplier unit. */
641 static void dec_mul(DisasContext *dc)
644 unsigned int subcode;
646 if ((dc->tb_flags & MSR_EE_FLAG)
647 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
648 && !(dc->env->pvr.regs[0] & PVR0_USE_HW_MUL_MASK)) {
649 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
650 t_gen_raise_exception(dc, EXCP_HW_EXCP);
654 subcode = dc->imm & 3;
655 d[0] = tcg_temp_new();
656 d[1] = tcg_temp_new();
659 LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
660 t_gen_mulu(cpu_R[dc->rd], d[1], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
664 /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2. */
665 if (subcode >= 1 && subcode <= 3
666 && !((dc->env->pvr.regs[2] & PVR2_USE_MUL64_MASK))) {
672 LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
673 t_gen_mulu(cpu_R[dc->rd], d[1], cpu_R[dc->ra], cpu_R[dc->rb]);
676 LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
677 t_gen_muls(d[0], cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
680 LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
681 t_gen_muls(d[0], cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
684 LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
685 t_gen_mulu(d[0], cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
688 cpu_abort(dc->env, "unknown MUL insn %x\n", subcode);
697 static void dec_div(DisasContext *dc)
704 if ((dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
705 && !((dc->env->pvr.regs[0] & PVR0_USE_DIV_MASK))) {
706 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
707 t_gen_raise_exception(dc, EXCP_HW_EXCP);
711 gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
714 gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
717 tcg_gen_movi_tl(cpu_R[dc->rd], 0);
720 static void dec_barrel(DisasContext *dc)
725 if ((dc->tb_flags & MSR_EE_FLAG)
726 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
727 && !(dc->env->pvr.regs[0] & PVR0_USE_BARREL_MASK)) {
728 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
729 t_gen_raise_exception(dc, EXCP_HW_EXCP);
733 s = dc->imm & (1 << 10);
734 t = dc->imm & (1 << 9);
736 LOG_DIS("bs%s%s r%d r%d r%d\n",
737 s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
741 tcg_gen_mov_tl(t0, *(dec_alu_op_b(dc)));
742 tcg_gen_andi_tl(t0, t0, 31);
745 tcg_gen_shl_tl(cpu_R[dc->rd], cpu_R[dc->ra], t0);
748 tcg_gen_sar_tl(cpu_R[dc->rd], cpu_R[dc->ra], t0);
750 tcg_gen_shr_tl(cpu_R[dc->rd], cpu_R[dc->ra], t0);
754 static void dec_bit(DisasContext *dc)
758 int mem_index = cpu_mmu_index(dc->env);
760 op = dc->ir & ((1 << 9) - 1);
766 LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
767 tcg_gen_andi_tl(t0, cpu_R[dc->ra], 1);
771 tcg_gen_shli_tl(t1, t1, 31);
773 tcg_gen_shri_tl(cpu_R[dc->rd], cpu_R[dc->ra], 1);
774 tcg_gen_or_tl(cpu_R[dc->rd], cpu_R[dc->rd], t1);
787 LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
790 tcg_gen_andi_tl(t0, cpu_R[dc->ra], 1);
795 tcg_gen_shri_tl(cpu_R[dc->rd], cpu_R[dc->ra], 1);
797 tcg_gen_sari_tl(cpu_R[dc->rd], cpu_R[dc->ra], 1);
801 LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
802 tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
805 LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
806 tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
813 LOG_DIS("wdc r%d\n", dc->ra);
814 if ((dc->tb_flags & MSR_EE_FLAG)
815 && mem_index == MMU_USER_IDX) {
816 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
817 t_gen_raise_exception(dc, EXCP_HW_EXCP);
823 LOG_DIS("wic r%d\n", dc->ra);
824 if ((dc->tb_flags & MSR_EE_FLAG)
825 && mem_index == MMU_USER_IDX) {
826 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
827 t_gen_raise_exception(dc, EXCP_HW_EXCP);
832 if ((dc->tb_flags & MSR_EE_FLAG)
833 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
834 && !((dc->env->pvr.regs[2] & PVR2_USE_PCMP_INSTR))) {
835 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
836 t_gen_raise_exception(dc, EXCP_HW_EXCP);
838 if (dc->env->pvr.regs[2] & PVR2_USE_PCMP_INSTR) {
839 gen_helper_clz(cpu_R[dc->rd], cpu_R[dc->ra]);
844 LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
845 tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
849 LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
850 tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
853 cpu_abort(dc->env, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
854 dc->pc, op, dc->rd, dc->ra, dc->rb);
859 static inline void sync_jmpstate(DisasContext *dc)
861 if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
862 if (dc->jmp == JMP_DIRECT) {
863 tcg_gen_movi_tl(env_btaken, 1);
865 dc->jmp = JMP_INDIRECT;
866 tcg_gen_movi_tl(env_btarget, dc->jmp_pc);
870 static void dec_imm(DisasContext *dc)
872 LOG_DIS("imm %x\n", dc->imm << 16);
873 tcg_gen_movi_tl(env_imm, (dc->imm << 16));
874 dc->tb_flags |= IMM_FLAG;
878 static inline void gen_load(DisasContext *dc, TCGv dst, TCGv addr,
881 int mem_index = cpu_mmu_index(dc->env);
884 tcg_gen_qemu_ld8u(dst, addr, mem_index);
885 } else if (size == 2) {
886 tcg_gen_qemu_ld16u(dst, addr, mem_index);
887 } else if (size == 4) {
888 tcg_gen_qemu_ld32u(dst, addr, mem_index);
890 cpu_abort(dc->env, "Incorrect load size %d\n", size);
893 static inline TCGv *compute_ldst_addr(DisasContext *dc, TCGv *t)
895 unsigned int extimm = dc->tb_flags & IMM_FLAG;
896 /* Should be set to one if r1 is used by loadstores. */
899 /* All load/stores use ra. */
904 /* Treat the common cases first. */
906 /* If any of the regs is r0, return a ptr to the other. */
908 return &cpu_R[dc->rb];
909 } else if (dc->rb == 0) {
910 return &cpu_R[dc->ra];
918 tcg_gen_add_tl(*t, cpu_R[dc->ra], cpu_R[dc->rb]);
921 gen_helper_stackprot(cpu_env, *t);
928 return &cpu_R[dc->ra];
931 tcg_gen_movi_tl(*t, (int32_t)((int16_t)dc->imm));
932 tcg_gen_add_tl(*t, cpu_R[dc->ra], *t);
935 tcg_gen_add_tl(*t, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
939 gen_helper_stackprot(cpu_env, *t);
944 static inline void dec_byteswap(DisasContext *dc, TCGv dst, TCGv src, int size)
947 tcg_gen_bswap32_tl(dst, src);
948 } else if (size == 2) {
949 TCGv t = tcg_temp_new();
951 /* bswap16 assumes the high bits are zero. */
952 tcg_gen_andi_tl(t, src, 0xffff);
953 tcg_gen_bswap16_tl(dst, t);
957 cpu_abort(dc->env, "Invalid ldst byteswap size %d\n", size);
962 static void dec_load(DisasContext *dc)
965 unsigned int size, rev = 0, ex = 0;
967 size = 1 << (dc->opcode & 3);
970 rev = (dc->ir >> 9) & 1;
971 ex = (dc->ir >> 10) & 1;
974 if (size > 4 && (dc->tb_flags & MSR_EE_FLAG)
975 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
976 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
977 t_gen_raise_exception(dc, EXCP_HW_EXCP);
981 LOG_DIS("l%d%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
985 addr = compute_ldst_addr(dc, &t);
988 * When doing reverse accesses we need to do two things.
990 * 1. Reverse the address wrt endianness.
991 * 2. Byteswap the data lanes on the way back into the CPU core.
993 if (rev && size != 4) {
994 /* Endian reverse the address. t is addr. */
1002 TCGv low = tcg_temp_new();
1004 /* Force addr into the temp. */
1007 tcg_gen_mov_tl(t, *addr);
1011 tcg_gen_andi_tl(low, t, 3);
1012 tcg_gen_sub_tl(low, tcg_const_tl(3), low);
1013 tcg_gen_andi_tl(t, t, ~3);
1014 tcg_gen_or_tl(t, t, low);
1015 tcg_gen_mov_tl(env_imm, t);
1023 /* Force addr into the temp. */
1026 tcg_gen_xori_tl(t, *addr, 2);
1029 tcg_gen_xori_tl(t, t, 2);
1033 cpu_abort(dc->env, "Invalid reverse size\n");
1038 /* lwx does not throw unaligned access errors, so force alignment */
1040 /* Force addr into the temp. */
1043 tcg_gen_mov_tl(t, *addr);
1046 tcg_gen_andi_tl(t, t, ~3);
1049 /* If we get a fault on a dslot, the jmpstate better be in sync. */
1052 /* Verify alignment if needed. */
1053 if ((dc->env->pvr.regs[2] & PVR2_UNALIGNED_EXC_MASK) && size > 1) {
1054 TCGv v = tcg_temp_new();
1057 * Microblaze gives MMU faults priority over faults due to
1058 * unaligned addresses. That's why we speculatively do the load
1059 * into v. If the load succeeds, we verify alignment of the
1060 * address and if that succeeds we write into the destination reg.
1062 gen_load(dc, v, *addr, size);
1064 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
1065 gen_helper_memalign(cpu_env, *addr, tcg_const_tl(dc->rd),
1066 tcg_const_tl(0), tcg_const_tl(size - 1));
1069 dec_byteswap(dc, cpu_R[dc->rd], v, size);
1071 tcg_gen_mov_tl(cpu_R[dc->rd], v);
1077 gen_load(dc, cpu_R[dc->rd], *addr, size);
1079 dec_byteswap(dc, cpu_R[dc->rd], cpu_R[dc->rd], size);
1082 /* We are loading into r0, no need to reverse. */
1083 gen_load(dc, env_imm, *addr, size);
1088 /* no support for for AXI exclusive so always clear C */
1089 write_carryi(dc, 0);
1090 tcg_gen_st_tl(*addr, cpu_env, offsetof(CPUMBState, res_addr));
1097 static void gen_store(DisasContext *dc, TCGv addr, TCGv val,
1100 int mem_index = cpu_mmu_index(dc->env);
1103 tcg_gen_qemu_st8(val, addr, mem_index);
1104 else if (size == 2) {
1105 tcg_gen_qemu_st16(val, addr, mem_index);
1106 } else if (size == 4) {
1107 tcg_gen_qemu_st32(val, addr, mem_index);
1109 cpu_abort(dc->env, "Incorrect store size %d\n", size);
1112 static void dec_store(DisasContext *dc)
1114 TCGv t, *addr, swx_addr, r_check;
1116 unsigned int size, rev = 0, ex = 0;
1118 size = 1 << (dc->opcode & 3);
1120 rev = (dc->ir >> 9) & 1;
1121 ex = (dc->ir >> 10) & 1;
1124 if (size > 4 && (dc->tb_flags & MSR_EE_FLAG)
1125 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
1126 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1127 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1131 LOG_DIS("s%d%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1134 /* If we get a fault on a dslot, the jmpstate better be in sync. */
1136 addr = compute_ldst_addr(dc, &t);
1138 r_check = tcg_temp_new();
1139 swx_addr = tcg_temp_local_new();
1142 /* Force addr into the swx_addr. */
1143 tcg_gen_mov_tl(swx_addr, *addr);
1145 /* swx does not throw unaligned access errors, so force alignment */
1146 tcg_gen_andi_tl(swx_addr, swx_addr, ~3);
1148 tcg_gen_ld_tl(r_check, cpu_env, offsetof(CPUMBState, res_addr));
1149 write_carryi(dc, 1);
1150 swx_skip = gen_new_label();
1151 tcg_gen_brcond_tl(TCG_COND_NE, r_check, swx_addr, swx_skip);
1152 write_carryi(dc, 0);
1155 if (rev && size != 4) {
1156 /* Endian reverse the address. t is addr. */
1164 TCGv low = tcg_temp_new();
1166 /* Force addr into the temp. */
1169 tcg_gen_mov_tl(t, *addr);
1173 tcg_gen_andi_tl(low, t, 3);
1174 tcg_gen_sub_tl(low, tcg_const_tl(3), low);
1175 tcg_gen_andi_tl(t, t, ~3);
1176 tcg_gen_or_tl(t, t, low);
1177 tcg_gen_mov_tl(env_imm, t);
1185 /* Force addr into the temp. */
1188 tcg_gen_xori_tl(t, *addr, 2);
1191 tcg_gen_xori_tl(t, t, 2);
1195 cpu_abort(dc->env, "Invalid reverse size\n");
1200 TCGv bs_data = tcg_temp_new();
1201 dec_byteswap(dc, bs_data, cpu_R[dc->rd], size);
1202 gen_store(dc, *addr, bs_data, size);
1203 tcg_temp_free(bs_data);
1205 gen_store(dc, *addr, cpu_R[dc->rd], size);
1209 TCGv bs_data = tcg_temp_new();
1210 dec_byteswap(dc, bs_data, cpu_R[dc->rd], size);
1211 gen_store(dc, *addr, bs_data, size);
1212 tcg_temp_free(bs_data);
1214 gen_store(dc, *addr, cpu_R[dc->rd], size);
1218 /* Verify alignment if needed. */
1219 if ((dc->env->pvr.regs[2] & PVR2_UNALIGNED_EXC_MASK) && size > 1) {
1220 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
1221 /* FIXME: if the alignment is wrong, we should restore the value
1222 * in memory. One possible way to achieve this is to probe
1223 * the MMU prior to the memaccess, thay way we could put
1224 * the alignment checks in between the probe and the mem
1227 gen_helper_memalign(cpu_env, *addr, tcg_const_tl(dc->rd),
1228 tcg_const_tl(1), tcg_const_tl(size - 1));
1232 gen_set_label(swx_skip);
1234 tcg_temp_free(r_check);
1235 tcg_temp_free(swx_addr);
1241 static inline void eval_cc(DisasContext *dc, unsigned int cc,
1242 TCGv d, TCGv a, TCGv b)
1246 tcg_gen_setcond_tl(TCG_COND_EQ, d, a, b);
1249 tcg_gen_setcond_tl(TCG_COND_NE, d, a, b);
1252 tcg_gen_setcond_tl(TCG_COND_LT, d, a, b);
1255 tcg_gen_setcond_tl(TCG_COND_LE, d, a, b);
1258 tcg_gen_setcond_tl(TCG_COND_GE, d, a, b);
1261 tcg_gen_setcond_tl(TCG_COND_GT, d, a, b);
1264 cpu_abort(dc->env, "Unknown condition code %x.\n", cc);
1269 static void eval_cond_jmp(DisasContext *dc, TCGv pc_true, TCGv pc_false)
1273 l1 = gen_new_label();
1274 /* Conditional jmp. */
1275 tcg_gen_mov_tl(cpu_SR[SR_PC], pc_false);
1276 tcg_gen_brcondi_tl(TCG_COND_EQ, env_btaken, 0, l1);
1277 tcg_gen_mov_tl(cpu_SR[SR_PC], pc_true);
1281 static void dec_bcc(DisasContext *dc)
1286 cc = EXTRACT_FIELD(dc->ir, 21, 23);
1287 dslot = dc->ir & (1 << 25);
1288 LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1290 dc->delayed_branch = 1;
1292 dc->delayed_branch = 2;
1293 dc->tb_flags |= D_FLAG;
1294 tcg_gen_st_tl(tcg_const_tl(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1295 cpu_env, offsetof(CPUMBState, bimm));
1298 if (dec_alu_op_b_is_small_imm(dc)) {
1299 int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend. */
1301 tcg_gen_movi_tl(env_btarget, dc->pc + offset);
1302 dc->jmp = JMP_DIRECT_CC;
1303 dc->jmp_pc = dc->pc + offset;
1305 dc->jmp = JMP_INDIRECT;
1306 tcg_gen_movi_tl(env_btarget, dc->pc);
1307 tcg_gen_add_tl(env_btarget, env_btarget, *(dec_alu_op_b(dc)));
1309 eval_cc(dc, cc, env_btaken, cpu_R[dc->ra], tcg_const_tl(0));
1312 static void dec_br(DisasContext *dc)
1314 unsigned int dslot, link, abs, mbar;
1315 int mem_index = cpu_mmu_index(dc->env);
1317 dslot = dc->ir & (1 << 20);
1318 abs = dc->ir & (1 << 19);
1319 link = dc->ir & (1 << 18);
1321 /* Memory barrier. */
1322 mbar = (dc->ir >> 16) & 31;
1323 if (mbar == 2 && dc->imm == 4) {
1324 /* mbar IMM & 16 decodes to sleep. */
1326 TCGv_i32 tmp_hlt = tcg_const_i32(EXCP_HLT);
1327 TCGv_i32 tmp_1 = tcg_const_i32(1);
1332 tcg_gen_st_i32(tmp_1, cpu_env,
1333 -offsetof(MicroBlazeCPU, env)
1334 +offsetof(CPUState, halted));
1335 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc + 4);
1336 gen_helper_raise_exception(cpu_env, tmp_hlt);
1337 tcg_temp_free_i32(tmp_hlt);
1338 tcg_temp_free_i32(tmp_1);
1341 LOG_DIS("mbar %d\n", dc->rd);
1343 dc->cpustate_changed = 1;
1347 LOG_DIS("br%s%s%s%s imm=%x\n",
1348 abs ? "a" : "", link ? "l" : "",
1349 dc->type_b ? "i" : "", dslot ? "d" : "",
1352 dc->delayed_branch = 1;
1354 dc->delayed_branch = 2;
1355 dc->tb_flags |= D_FLAG;
1356 tcg_gen_st_tl(tcg_const_tl(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1357 cpu_env, offsetof(CPUMBState, bimm));
1360 tcg_gen_movi_tl(cpu_R[dc->rd], dc->pc);
1362 dc->jmp = JMP_INDIRECT;
1364 tcg_gen_movi_tl(env_btaken, 1);
1365 tcg_gen_mov_tl(env_btarget, *(dec_alu_op_b(dc)));
1366 if (link && !dslot) {
1367 if (!(dc->tb_flags & IMM_FLAG) && (dc->imm == 8 || dc->imm == 0x18))
1368 t_gen_raise_exception(dc, EXCP_BREAK);
1370 if ((dc->tb_flags & MSR_EE_FLAG) && mem_index == MMU_USER_IDX) {
1371 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1372 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1376 t_gen_raise_exception(dc, EXCP_DEBUG);
1380 if (dec_alu_op_b_is_small_imm(dc)) {
1381 dc->jmp = JMP_DIRECT;
1382 dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1384 tcg_gen_movi_tl(env_btaken, 1);
1385 tcg_gen_movi_tl(env_btarget, dc->pc);
1386 tcg_gen_add_tl(env_btarget, env_btarget, *(dec_alu_op_b(dc)));
1391 static inline void do_rti(DisasContext *dc)
1394 t0 = tcg_temp_new();
1395 t1 = tcg_temp_new();
1396 tcg_gen_shri_tl(t0, cpu_SR[SR_MSR], 1);
1397 tcg_gen_ori_tl(t1, cpu_SR[SR_MSR], MSR_IE);
1398 tcg_gen_andi_tl(t0, t0, (MSR_VM | MSR_UM));
1400 tcg_gen_andi_tl(t1, t1, ~(MSR_VM | MSR_UM));
1401 tcg_gen_or_tl(t1, t1, t0);
1405 dc->tb_flags &= ~DRTI_FLAG;
1408 static inline void do_rtb(DisasContext *dc)
1411 t0 = tcg_temp_new();
1412 t1 = tcg_temp_new();
1413 tcg_gen_andi_tl(t1, cpu_SR[SR_MSR], ~MSR_BIP);
1414 tcg_gen_shri_tl(t0, t1, 1);
1415 tcg_gen_andi_tl(t0, t0, (MSR_VM | MSR_UM));
1417 tcg_gen_andi_tl(t1, t1, ~(MSR_VM | MSR_UM));
1418 tcg_gen_or_tl(t1, t1, t0);
1422 dc->tb_flags &= ~DRTB_FLAG;
1425 static inline void do_rte(DisasContext *dc)
1428 t0 = tcg_temp_new();
1429 t1 = tcg_temp_new();
1431 tcg_gen_ori_tl(t1, cpu_SR[SR_MSR], MSR_EE);
1432 tcg_gen_andi_tl(t1, t1, ~MSR_EIP);
1433 tcg_gen_shri_tl(t0, t1, 1);
1434 tcg_gen_andi_tl(t0, t0, (MSR_VM | MSR_UM));
1436 tcg_gen_andi_tl(t1, t1, ~(MSR_VM | MSR_UM));
1437 tcg_gen_or_tl(t1, t1, t0);
1441 dc->tb_flags &= ~DRTE_FLAG;
1444 static void dec_rts(DisasContext *dc)
1446 unsigned int b_bit, i_bit, e_bit;
1447 int mem_index = cpu_mmu_index(dc->env);
1449 i_bit = dc->ir & (1 << 21);
1450 b_bit = dc->ir & (1 << 22);
1451 e_bit = dc->ir & (1 << 23);
1453 dc->delayed_branch = 2;
1454 dc->tb_flags |= D_FLAG;
1455 tcg_gen_st_tl(tcg_const_tl(dc->type_b && (dc->tb_flags & IMM_FLAG)),
1456 cpu_env, offsetof(CPUMBState, bimm));
1459 LOG_DIS("rtid ir=%x\n", dc->ir);
1460 if ((dc->tb_flags & MSR_EE_FLAG)
1461 && mem_index == MMU_USER_IDX) {
1462 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1463 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1465 dc->tb_flags |= DRTI_FLAG;
1467 LOG_DIS("rtbd ir=%x\n", dc->ir);
1468 if ((dc->tb_flags & MSR_EE_FLAG)
1469 && mem_index == MMU_USER_IDX) {
1470 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1471 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1473 dc->tb_flags |= DRTB_FLAG;
1475 LOG_DIS("rted ir=%x\n", dc->ir);
1476 if ((dc->tb_flags & MSR_EE_FLAG)
1477 && mem_index == MMU_USER_IDX) {
1478 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1479 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1481 dc->tb_flags |= DRTE_FLAG;
1483 LOG_DIS("rts ir=%x\n", dc->ir);
1485 dc->jmp = JMP_INDIRECT;
1486 tcg_gen_movi_tl(env_btaken, 1);
1487 tcg_gen_add_tl(env_btarget, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
1490 static int dec_check_fpuv2(DisasContext *dc)
1494 r = dc->env->pvr.regs[2] & PVR2_USE_FPU2_MASK;
1496 if (!r && (dc->tb_flags & MSR_EE_FLAG)) {
1497 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_FPU);
1498 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1503 static void dec_fpu(DisasContext *dc)
1505 unsigned int fpu_insn;
1507 if ((dc->tb_flags & MSR_EE_FLAG)
1508 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
1509 && !((dc->env->pvr.regs[2] & PVR2_USE_FPU_MASK))) {
1510 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1511 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1515 fpu_insn = (dc->ir >> 7) & 7;
1519 gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1524 gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1529 gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1534 gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1539 switch ((dc->ir >> 4) & 7) {
1541 gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1542 cpu_R[dc->ra], cpu_R[dc->rb]);
1545 gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1546 cpu_R[dc->ra], cpu_R[dc->rb]);
1549 gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1550 cpu_R[dc->ra], cpu_R[dc->rb]);
1553 gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1554 cpu_R[dc->ra], cpu_R[dc->rb]);
1557 gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1558 cpu_R[dc->ra], cpu_R[dc->rb]);
1561 gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1562 cpu_R[dc->ra], cpu_R[dc->rb]);
1565 gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1566 cpu_R[dc->ra], cpu_R[dc->rb]);
1569 qemu_log_mask(LOG_UNIMP,
1570 "unimplemented fcmp fpu_insn=%x pc=%x"
1572 fpu_insn, dc->pc, dc->opcode);
1573 dc->abort_at_next_insn = 1;
1579 if (!dec_check_fpuv2(dc)) {
1582 gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1586 if (!dec_check_fpuv2(dc)) {
1589 gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1593 if (!dec_check_fpuv2(dc)) {
1596 gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1600 qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1602 fpu_insn, dc->pc, dc->opcode);
1603 dc->abort_at_next_insn = 1;
1608 static void dec_null(DisasContext *dc)
1610 if ((dc->tb_flags & MSR_EE_FLAG)
1611 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)) {
1612 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1613 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1616 qemu_log ("unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1617 dc->abort_at_next_insn = 1;
1620 /* Insns connected to FSL or AXI stream attached devices. */
1621 static void dec_stream(DisasContext *dc)
1623 int mem_index = cpu_mmu_index(dc->env);
1624 TCGv_i32 t_id, t_ctrl;
1627 LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1628 dc->type_b ? "" : "d", dc->imm);
1630 if ((dc->tb_flags & MSR_EE_FLAG) && (mem_index == MMU_USER_IDX)) {
1631 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_PRIVINSN);
1632 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1636 t_id = tcg_temp_new();
1638 tcg_gen_movi_tl(t_id, dc->imm & 0xf);
1639 ctrl = dc->imm >> 10;
1641 tcg_gen_andi_tl(t_id, cpu_R[dc->rb], 0xf);
1642 ctrl = dc->imm >> 5;
1645 t_ctrl = tcg_const_tl(ctrl);
1648 gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1650 gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1652 tcg_temp_free(t_id);
1653 tcg_temp_free(t_ctrl);
1656 static struct decoder_info {
1661 void (*dec)(DisasContext *dc);
1669 {DEC_BARREL, dec_barrel},
1671 {DEC_ST, dec_store},
1680 {DEC_STREAM, dec_stream},
1684 static inline void decode(DisasContext *dc, uint32_t ir)
1688 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
1689 tcg_gen_debug_insn_start(dc->pc);
1693 LOG_DIS("%8.8x\t", dc->ir);
1698 if ((dc->tb_flags & MSR_EE_FLAG)
1699 && (dc->env->pvr.regs[2] & PVR2_ILL_OPCODE_EXC_MASK)
1700 && (dc->env->pvr.regs[2] & PVR2_OPCODE_0x0_ILL_MASK)) {
1701 tcg_gen_movi_tl(cpu_SR[SR_ESR], ESR_EC_ILLEGAL_OP);
1702 t_gen_raise_exception(dc, EXCP_HW_EXCP);
1706 LOG_DIS("nr_nops=%d\t", dc->nr_nops);
1708 if (dc->nr_nops > 4)
1709 cpu_abort(dc->env, "fetching nop sequence\n");
1711 /* bit 2 seems to indicate insn type. */
1712 dc->type_b = ir & (1 << 29);
1714 dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1715 dc->rd = EXTRACT_FIELD(ir, 21, 25);
1716 dc->ra = EXTRACT_FIELD(ir, 16, 20);
1717 dc->rb = EXTRACT_FIELD(ir, 11, 15);
1718 dc->imm = EXTRACT_FIELD(ir, 0, 15);
1720 /* Large switch for all insns. */
1721 for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1722 if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1729 static void check_breakpoint(CPUMBState *env, DisasContext *dc)
1733 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
1734 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
1735 if (bp->pc == dc->pc) {
1736 t_gen_raise_exception(dc, EXCP_DEBUG);
1737 dc->is_jmp = DISAS_UPDATE;
1743 /* generate intermediate code for basic block 'tb'. */
1745 gen_intermediate_code_internal(MicroBlazeCPU *cpu, TranslationBlock *tb,
1748 CPUState *cs = CPU(cpu);
1749 CPUMBState *env = &cpu->env;
1750 uint16_t *gen_opc_end;
1753 struct DisasContext ctx;
1754 struct DisasContext *dc = &ctx;
1755 uint32_t next_page_start, org_flags;
1763 org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1765 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
1767 dc->is_jmp = DISAS_NEXT;
1769 dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1770 if (dc->delayed_branch) {
1771 dc->jmp = JMP_INDIRECT;
1774 dc->singlestep_enabled = cs->singlestep_enabled;
1775 dc->cpustate_changed = 0;
1776 dc->abort_at_next_insn = 0;
1780 cpu_abort(env, "Microblaze: unaligned PC=%x\n", pc_start);
1782 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1784 qemu_log("--------------\n");
1785 log_cpu_state(CPU(cpu), 0);
1789 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
1792 max_insns = tb->cflags & CF_COUNT_MASK;
1794 max_insns = CF_COUNT_MASK;
1800 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1801 tcg_gen_movi_tl(cpu_SR[SR_PC], dc->pc);
1805 check_breakpoint(env, dc);
1808 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
1812 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1814 tcg_ctx.gen_opc_pc[lj] = dc->pc;
1815 tcg_ctx.gen_opc_instr_start[lj] = 1;
1816 tcg_ctx.gen_opc_icount[lj] = num_insns;
1820 LOG_DIS("%8.8x:\t", dc->pc);
1822 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
1826 decode(dc, cpu_ldl_code(env, dc->pc));
1828 dc->tb_flags &= ~IMM_FLAG;
1832 if (dc->delayed_branch) {
1833 dc->delayed_branch--;
1834 if (!dc->delayed_branch) {
1835 if (dc->tb_flags & DRTI_FLAG)
1837 if (dc->tb_flags & DRTB_FLAG)
1839 if (dc->tb_flags & DRTE_FLAG)
1841 /* Clear the delay slot flag. */
1842 dc->tb_flags &= ~D_FLAG;
1843 /* If it is a direct jump, try direct chaining. */
1844 if (dc->jmp == JMP_INDIRECT) {
1845 eval_cond_jmp(dc, env_btarget, tcg_const_tl(dc->pc));
1846 dc->is_jmp = DISAS_JUMP;
1847 } else if (dc->jmp == JMP_DIRECT) {
1849 gen_goto_tb(dc, 0, dc->jmp_pc);
1850 dc->is_jmp = DISAS_TB_JUMP;
1851 } else if (dc->jmp == JMP_DIRECT_CC) {
1855 l1 = gen_new_label();
1856 /* Conditional jmp. */
1857 tcg_gen_brcondi_tl(TCG_COND_NE, env_btaken, 0, l1);
1858 gen_goto_tb(dc, 1, dc->pc);
1860 gen_goto_tb(dc, 0, dc->jmp_pc);
1862 dc->is_jmp = DISAS_TB_JUMP;
1867 if (cs->singlestep_enabled) {
1870 } while (!dc->is_jmp && !dc->cpustate_changed
1871 && tcg_ctx.gen_opc_ptr < gen_opc_end
1873 && (dc->pc < next_page_start)
1874 && num_insns < max_insns);
1877 if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
1878 if (dc->tb_flags & D_FLAG) {
1879 dc->is_jmp = DISAS_UPDATE;
1880 tcg_gen_movi_tl(cpu_SR[SR_PC], npc);
1886 if (tb->cflags & CF_LAST_IO)
1888 /* Force an update if the per-tb cpu state has changed. */
1889 if (dc->is_jmp == DISAS_NEXT
1890 && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1891 dc->is_jmp = DISAS_UPDATE;
1892 tcg_gen_movi_tl(cpu_SR[SR_PC], npc);
1896 if (unlikely(cs->singlestep_enabled)) {
1897 TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1899 if (dc->is_jmp != DISAS_JUMP) {
1900 tcg_gen_movi_tl(cpu_SR[SR_PC], npc);
1902 gen_helper_raise_exception(cpu_env, tmp);
1903 tcg_temp_free_i32(tmp);
1905 switch(dc->is_jmp) {
1907 gen_goto_tb(dc, 1, npc);
1912 /* indicate that the hash table must be used
1913 to find the next TB */
1917 /* nothing more to generate */
1921 gen_tb_end(tb, num_insns);
1922 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
1924 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
1927 tcg_ctx.gen_opc_instr_start[lj++] = 0;
1929 tb->size = dc->pc - pc_start;
1930 tb->icount = num_insns;
1935 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
1938 log_target_disas(env, pc_start, dc->pc - pc_start, 0);
1940 qemu_log("\nisize=%d osize=%td\n",
1941 dc->pc - pc_start, tcg_ctx.gen_opc_ptr -
1942 tcg_ctx.gen_opc_buf);
1946 assert(!dc->abort_at_next_insn);
1949 void gen_intermediate_code (CPUMBState *env, struct TranslationBlock *tb)
1951 gen_intermediate_code_internal(mb_env_get_cpu(env), tb, false);
1954 void gen_intermediate_code_pc (CPUMBState *env, struct TranslationBlock *tb)
1956 gen_intermediate_code_internal(mb_env_get_cpu(env), tb, true);
1959 void mb_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
1962 MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1963 CPUMBState *env = &cpu->env;
1969 cpu_fprintf(f, "IN: PC=%x %s\n",
1970 env->sregs[SR_PC], lookup_symbol(env->sregs[SR_PC]));
1971 cpu_fprintf(f, "rmsr=%x resr=%x rear=%x debug=%x imm=%x iflags=%x fsr=%x\n",
1972 env->sregs[SR_MSR], env->sregs[SR_ESR], env->sregs[SR_EAR],
1973 env->debug, env->imm, env->iflags, env->sregs[SR_FSR]);
1974 cpu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1975 env->btaken, env->btarget,
1976 (env->sregs[SR_MSR] & MSR_UM) ? "user" : "kernel",
1977 (env->sregs[SR_MSR] & MSR_UMS) ? "user" : "kernel",
1978 (env->sregs[SR_MSR] & MSR_EIP),
1979 (env->sregs[SR_MSR] & MSR_IE));
1981 for (i = 0; i < 32; i++) {
1982 cpu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1983 if ((i + 1) % 4 == 0)
1984 cpu_fprintf(f, "\n");
1986 cpu_fprintf(f, "\n\n");
1989 MicroBlazeCPU *cpu_mb_init(const char *cpu_model)
1993 cpu = MICROBLAZE_CPU(object_new(TYPE_MICROBLAZE_CPU));
1995 object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
2000 void mb_tcg_init(void)
2004 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
2006 env_debug = tcg_global_mem_new(TCG_AREG0,
2007 offsetof(CPUMBState, debug),
2009 env_iflags = tcg_global_mem_new(TCG_AREG0,
2010 offsetof(CPUMBState, iflags),
2012 env_imm = tcg_global_mem_new(TCG_AREG0,
2013 offsetof(CPUMBState, imm),
2015 env_btarget = tcg_global_mem_new(TCG_AREG0,
2016 offsetof(CPUMBState, btarget),
2018 env_btaken = tcg_global_mem_new(TCG_AREG0,
2019 offsetof(CPUMBState, btaken),
2021 for (i = 0; i < ARRAY_SIZE(cpu_R); i++) {
2022 cpu_R[i] = tcg_global_mem_new(TCG_AREG0,
2023 offsetof(CPUMBState, regs[i]),
2026 for (i = 0; i < ARRAY_SIZE(cpu_SR); i++) {
2027 cpu_SR[i] = tcg_global_mem_new(TCG_AREG0,
2028 offsetof(CPUMBState, sregs[i]),
2029 special_regnames[i]);
2033 void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb, int pc_pos)
2035 env->sregs[SR_PC] = tcg_ctx.gen_opc_pc[pc_pos];