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/>.
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 "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(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((tcg_target_long
)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);
153 static void write_carry(DisasContext
*dc
, TCGv v
)
155 TCGv t0
= tcg_temp_new();
156 tcg_gen_shli_tl(t0
, v
, 31);
157 tcg_gen_sari_tl(t0
, t0
, 31);
158 tcg_gen_andi_tl(t0
, t0
, (MSR_C
| MSR_CC
));
159 tcg_gen_andi_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
],
161 tcg_gen_or_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], t0
);
165 /* True if ALU operand b is a small immediate that may deserve
167 static inline int dec_alu_op_b_is_small_imm(DisasContext
*dc
)
169 /* Immediate insn without the imm prefix ? */
170 return dc
->type_b
&& !(dc
->tb_flags
& IMM_FLAG
);
173 static inline TCGv
*dec_alu_op_b(DisasContext
*dc
)
176 if (dc
->tb_flags
& IMM_FLAG
)
177 tcg_gen_ori_tl(env_imm
, env_imm
, dc
->imm
);
179 tcg_gen_movi_tl(env_imm
, (int32_t)((int16_t)dc
->imm
));
182 return &cpu_R
[dc
->rb
];
185 static void dec_add(DisasContext
*dc
)
193 LOG_DIS("add%s%s%s r%d r%d r%d\n",
194 dc
->type_b
? "i" : "", k
? "k" : "", c
? "c" : "",
195 dc
->rd
, dc
->ra
, dc
->rb
);
197 /* Take care of the easy cases first. */
199 /* k - keep carry, no need to update MSR. */
200 /* If rd == r0, it's a nop. */
202 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
205 /* c - Add carry into the result. */
209 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
216 /* From now on, we can assume k is zero. So we need to update MSR. */
222 tcg_gen_movi_tl(cf
, 0);
226 TCGv ncf
= tcg_temp_new();
227 gen_helper_carry(ncf
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)), cf
);
228 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
229 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
230 write_carry(dc
, ncf
);
233 gen_helper_carry(cf
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)), cf
);
239 static void dec_sub(DisasContext
*dc
)
241 unsigned int u
, cmp
, k
, c
;
247 cmp
= (dc
->imm
& 1) && (!dc
->type_b
) && k
;
250 LOG_DIS("cmp%s r%d, r%d ir=%x\n", u
? "u" : "", dc
->rd
, dc
->ra
, dc
->ir
);
253 gen_helper_cmpu(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
255 gen_helper_cmp(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
260 LOG_DIS("sub%s%s r%d, r%d r%d\n",
261 k
? "k" : "", c
? "c" : "", dc
->rd
, dc
->ra
, dc
->rb
);
263 /* Take care of the easy cases first. */
265 /* k - keep carry, no need to update MSR. */
266 /* If rd == r0, it's a nop. */
268 tcg_gen_sub_tl(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
271 /* c - Add carry into the result. */
275 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
282 /* From now on, we can assume k is zero. So we need to update MSR. */
283 /* Extract carry. And complement a into na. */
289 tcg_gen_movi_tl(cf
, 1);
292 /* d = b + ~a + c. carry defaults to 1. */
293 tcg_gen_not_tl(na
, cpu_R
[dc
->ra
]);
296 TCGv ncf
= tcg_temp_new();
297 gen_helper_carry(ncf
, na
, *(dec_alu_op_b(dc
)), cf
);
298 tcg_gen_add_tl(cpu_R
[dc
->rd
], na
, *(dec_alu_op_b(dc
)));
299 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
300 write_carry(dc
, ncf
);
303 gen_helper_carry(cf
, na
, *(dec_alu_op_b(dc
)), cf
);
310 static void dec_pattern(DisasContext
*dc
)
315 if ((dc
->tb_flags
& MSR_EE_FLAG
)
316 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
317 && !((dc
->env
->pvr
.regs
[2] & PVR2_USE_PCMP_INSTR
))) {
318 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
319 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
322 mode
= dc
->opcode
& 3;
326 LOG_DIS("pcmpbf r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
328 gen_helper_pcmpbf(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
331 LOG_DIS("pcmpeq r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
333 TCGv t0
= tcg_temp_local_new();
334 l1
= gen_new_label();
335 tcg_gen_movi_tl(t0
, 1);
336 tcg_gen_brcond_tl(TCG_COND_EQ
,
337 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
338 tcg_gen_movi_tl(t0
, 0);
340 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
345 LOG_DIS("pcmpne r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
346 l1
= gen_new_label();
348 TCGv t0
= tcg_temp_local_new();
349 tcg_gen_movi_tl(t0
, 1);
350 tcg_gen_brcond_tl(TCG_COND_NE
,
351 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
352 tcg_gen_movi_tl(t0
, 0);
354 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
360 "unsupported pattern insn opcode=%x\n", dc
->opcode
);
365 static void dec_and(DisasContext
*dc
)
369 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
374 not = dc
->opcode
& (1 << 1);
375 LOG_DIS("and%s\n", not ? "n" : "");
381 TCGv t
= tcg_temp_new();
382 tcg_gen_not_tl(t
, *(dec_alu_op_b(dc
)));
383 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t
);
386 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
389 static void dec_or(DisasContext
*dc
)
391 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
396 LOG_DIS("or r%d r%d r%d imm=%x\n", dc
->rd
, dc
->ra
, dc
->rb
, dc
->imm
);
398 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
401 static void dec_xor(DisasContext
*dc
)
403 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
408 LOG_DIS("xor r%d\n", dc
->rd
);
410 tcg_gen_xor_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
413 static inline void msr_read(DisasContext
*dc
, TCGv d
)
415 tcg_gen_mov_tl(d
, cpu_SR
[SR_MSR
]);
418 static inline void msr_write(DisasContext
*dc
, TCGv v
)
423 dc
->cpustate_changed
= 1;
424 /* PVR bit is not writable. */
425 tcg_gen_andi_tl(t
, v
, ~MSR_PVR
);
426 tcg_gen_andi_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], MSR_PVR
);
427 tcg_gen_or_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], v
);
431 static void dec_msr(DisasContext
*dc
)
434 unsigned int sr
, to
, rn
;
435 int mem_index
= cpu_mmu_index(dc
->env
);
437 sr
= dc
->imm
& ((1 << 14) - 1);
438 to
= dc
->imm
& (1 << 14);
441 dc
->cpustate_changed
= 1;
443 /* msrclr and msrset. */
444 if (!(dc
->imm
& (1 << 15))) {
445 unsigned int clr
= dc
->ir
& (1 << 16);
447 LOG_DIS("msr%s r%d imm=%x\n", clr
? "clr" : "set",
450 if (!(dc
->env
->pvr
.regs
[2] & PVR2_USE_MSR_INSTR
)) {
455 if ((dc
->tb_flags
& MSR_EE_FLAG
)
456 && mem_index
== MMU_USER_IDX
&& (dc
->imm
!= 4 && dc
->imm
!= 0)) {
457 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
458 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
463 msr_read(dc
, cpu_R
[dc
->rd
]);
468 tcg_gen_mov_tl(t1
, *(dec_alu_op_b(dc
)));
471 tcg_gen_not_tl(t1
, t1
);
472 tcg_gen_and_tl(t0
, t0
, t1
);
474 tcg_gen_or_tl(t0
, t0
, t1
);
478 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
+ 4);
479 dc
->is_jmp
= DISAS_UPDATE
;
484 if ((dc
->tb_flags
& MSR_EE_FLAG
)
485 && mem_index
== MMU_USER_IDX
) {
486 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
487 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
492 #if !defined(CONFIG_USER_ONLY)
493 /* Catch read/writes to the mmu block. */
494 if ((sr
& ~0xff) == 0x1000) {
496 LOG_DIS("m%ss sr%d r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
498 gen_helper_mmu_write(tcg_const_tl(sr
), cpu_R
[dc
->ra
]);
500 gen_helper_mmu_read(cpu_R
[dc
->rd
], tcg_const_tl(sr
));
506 LOG_DIS("m%ss sr%x r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
511 msr_write(dc
, cpu_R
[dc
->ra
]);
514 tcg_gen_mov_tl(cpu_SR
[SR_EAR
], cpu_R
[dc
->ra
]);
517 tcg_gen_mov_tl(cpu_SR
[SR_ESR
], cpu_R
[dc
->ra
]);
520 tcg_gen_andi_tl(cpu_SR
[SR_FSR
], cpu_R
[dc
->ra
], 31);
523 tcg_gen_st_tl(cpu_R
[dc
->ra
], cpu_env
, offsetof(CPUMBState
, slr
));
526 tcg_gen_st_tl(cpu_R
[dc
->ra
], cpu_env
, offsetof(CPUMBState
, shr
));
529 cpu_abort(dc
->env
, "unknown mts reg %x\n", sr
);
533 LOG_DIS("m%ss r%d sr%x imm=%x\n", to
? "t" : "f", dc
->rd
, sr
, dc
->imm
);
537 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
540 msr_read(dc
, cpu_R
[dc
->rd
]);
543 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_EAR
]);
546 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_ESR
]);
549 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_FSR
]);
552 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_BTR
]);
555 tcg_gen_ld_tl(cpu_R
[dc
->rd
], cpu_env
, offsetof(CPUMBState
, slr
));
558 tcg_gen_ld_tl(cpu_R
[dc
->rd
], cpu_env
, offsetof(CPUMBState
, shr
));
574 tcg_gen_ld_tl(cpu_R
[dc
->rd
],
575 cpu_env
, offsetof(CPUMBState
, pvr
.regs
[rn
]));
578 cpu_abort(dc
->env
, "unknown mfs reg %x\n", sr
);
584 tcg_gen_movi_tl(cpu_R
[0], 0);
588 /* 64-bit signed mul, lower result in d and upper in d2. */
589 static void t_gen_muls(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
593 t0
= tcg_temp_new_i64();
594 t1
= tcg_temp_new_i64();
596 tcg_gen_ext_i32_i64(t0
, a
);
597 tcg_gen_ext_i32_i64(t1
, b
);
598 tcg_gen_mul_i64(t0
, t0
, t1
);
600 tcg_gen_trunc_i64_i32(d
, t0
);
601 tcg_gen_shri_i64(t0
, t0
, 32);
602 tcg_gen_trunc_i64_i32(d2
, t0
);
604 tcg_temp_free_i64(t0
);
605 tcg_temp_free_i64(t1
);
608 /* 64-bit unsigned muls, lower result in d and upper in d2. */
609 static void t_gen_mulu(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
613 t0
= tcg_temp_new_i64();
614 t1
= tcg_temp_new_i64();
616 tcg_gen_extu_i32_i64(t0
, a
);
617 tcg_gen_extu_i32_i64(t1
, b
);
618 tcg_gen_mul_i64(t0
, t0
, t1
);
620 tcg_gen_trunc_i64_i32(d
, t0
);
621 tcg_gen_shri_i64(t0
, t0
, 32);
622 tcg_gen_trunc_i64_i32(d2
, t0
);
624 tcg_temp_free_i64(t0
);
625 tcg_temp_free_i64(t1
);
628 /* Multiplier unit. */
629 static void dec_mul(DisasContext
*dc
)
632 unsigned int subcode
;
634 if ((dc
->tb_flags
& MSR_EE_FLAG
)
635 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
636 && !(dc
->env
->pvr
.regs
[0] & PVR0_USE_HW_MUL_MASK
)) {
637 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
638 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
642 subcode
= dc
->imm
& 3;
643 d
[0] = tcg_temp_new();
644 d
[1] = tcg_temp_new();
647 LOG_DIS("muli r%d r%d %x\n", dc
->rd
, dc
->ra
, dc
->imm
);
648 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
652 /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2. */
653 if (subcode
>= 1 && subcode
<= 3
654 && !((dc
->env
->pvr
.regs
[2] & PVR2_USE_MUL64_MASK
))) {
660 LOG_DIS("mul r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
661 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
664 LOG_DIS("mulh r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
665 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
668 LOG_DIS("mulhsu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
669 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
672 LOG_DIS("mulhu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
673 t_gen_mulu(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
676 cpu_abort(dc
->env
, "unknown MUL insn %x\n", subcode
);
685 static void dec_div(DisasContext
*dc
)
692 if ((dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
693 && !((dc
->env
->pvr
.regs
[0] & PVR0_USE_DIV_MASK
))) {
694 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
695 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
699 gen_helper_divu(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
701 gen_helper_divs(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
703 tcg_gen_movi_tl(cpu_R
[dc
->rd
], 0);
706 static void dec_barrel(DisasContext
*dc
)
711 if ((dc
->tb_flags
& MSR_EE_FLAG
)
712 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
713 && !(dc
->env
->pvr
.regs
[0] & PVR0_USE_BARREL_MASK
)) {
714 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
715 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
719 s
= dc
->imm
& (1 << 10);
720 t
= dc
->imm
& (1 << 9);
722 LOG_DIS("bs%s%s r%d r%d r%d\n",
723 s
? "l" : "r", t
? "a" : "l", dc
->rd
, dc
->ra
, dc
->rb
);
727 tcg_gen_mov_tl(t0
, *(dec_alu_op_b(dc
)));
728 tcg_gen_andi_tl(t0
, t0
, 31);
731 tcg_gen_shl_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
734 tcg_gen_sar_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
736 tcg_gen_shr_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
740 static void dec_bit(DisasContext
*dc
)
744 int mem_index
= cpu_mmu_index(dc
->env
);
746 op
= dc
->ir
& ((1 << 9) - 1);
752 LOG_DIS("src r%d r%d\n", dc
->rd
, dc
->ra
);
753 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
757 tcg_gen_shli_tl(t1
, t1
, 31);
759 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
760 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], t1
);
773 LOG_DIS("srl r%d r%d\n", dc
->rd
, dc
->ra
);
776 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
781 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
783 tcg_gen_sari_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
787 LOG_DIS("ext8s r%d r%d\n", dc
->rd
, dc
->ra
);
788 tcg_gen_ext8s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
791 LOG_DIS("ext16s r%d r%d\n", dc
->rd
, dc
->ra
);
792 tcg_gen_ext16s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
799 LOG_DIS("wdc r%d\n", dc
->ra
);
800 if ((dc
->tb_flags
& MSR_EE_FLAG
)
801 && mem_index
== MMU_USER_IDX
) {
802 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
803 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
809 LOG_DIS("wic r%d\n", dc
->ra
);
810 if ((dc
->tb_flags
& MSR_EE_FLAG
)
811 && mem_index
== MMU_USER_IDX
) {
812 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
813 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
818 if ((dc
->tb_flags
& MSR_EE_FLAG
)
819 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
820 && !((dc
->env
->pvr
.regs
[2] & PVR2_USE_PCMP_INSTR
))) {
821 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
822 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
824 if (dc
->env
->pvr
.regs
[2] & PVR2_USE_PCMP_INSTR
) {
825 gen_helper_clz(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
830 LOG_DIS("swapb r%d r%d\n", dc
->rd
, dc
->ra
);
831 tcg_gen_bswap32_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
835 LOG_DIS("swaph r%d r%d\n", dc
->rd
, dc
->ra
);
836 tcg_gen_rotri_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 16);
839 cpu_abort(dc
->env
, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
840 dc
->pc
, op
, dc
->rd
, dc
->ra
, dc
->rb
);
845 static inline void sync_jmpstate(DisasContext
*dc
)
847 if (dc
->jmp
== JMP_DIRECT
|| dc
->jmp
== JMP_DIRECT_CC
) {
848 if (dc
->jmp
== JMP_DIRECT
) {
849 tcg_gen_movi_tl(env_btaken
, 1);
851 dc
->jmp
= JMP_INDIRECT
;
852 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
856 static void dec_imm(DisasContext
*dc
)
858 LOG_DIS("imm %x\n", dc
->imm
<< 16);
859 tcg_gen_movi_tl(env_imm
, (dc
->imm
<< 16));
860 dc
->tb_flags
|= IMM_FLAG
;
864 static inline void gen_load(DisasContext
*dc
, TCGv dst
, TCGv addr
,
867 int mem_index
= cpu_mmu_index(dc
->env
);
870 tcg_gen_qemu_ld8u(dst
, addr
, mem_index
);
871 } else if (size
== 2) {
872 tcg_gen_qemu_ld16u(dst
, addr
, mem_index
);
873 } else if (size
== 4) {
874 tcg_gen_qemu_ld32u(dst
, addr
, mem_index
);
876 cpu_abort(dc
->env
, "Incorrect load size %d\n", size
);
879 static inline TCGv
*compute_ldst_addr(DisasContext
*dc
, TCGv
*t
)
881 unsigned int extimm
= dc
->tb_flags
& IMM_FLAG
;
882 /* Should be set to one if r1 is used by loadstores. */
885 /* All load/stores use ra. */
890 /* Treat the common cases first. */
892 /* If any of the regs is r0, return a ptr to the other. */
894 return &cpu_R
[dc
->rb
];
895 } else if (dc
->rb
== 0) {
896 return &cpu_R
[dc
->ra
];
904 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
907 gen_helper_stackprot(*t
);
914 return &cpu_R
[dc
->ra
];
917 tcg_gen_movi_tl(*t
, (int32_t)((int16_t)dc
->imm
));
918 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *t
);
921 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
925 gen_helper_stackprot(*t
);
930 static inline void dec_byteswap(DisasContext
*dc
, TCGv dst
, TCGv src
, int size
)
933 tcg_gen_bswap32_tl(dst
, src
);
934 } else if (size
== 2) {
935 TCGv t
= tcg_temp_new();
937 /* bswap16 assumes the high bits are zero. */
938 tcg_gen_andi_tl(t
, src
, 0xffff);
939 tcg_gen_bswap16_tl(dst
, t
);
943 cpu_abort(dc->env, "Invalid ldst byteswap size %d\n", size);
948 static void dec_load(DisasContext
*dc
)
951 unsigned int size
, rev
= 0;
953 size
= 1 << (dc
->opcode
& 3);
956 rev
= (dc
->ir
>> 9) & 1;
959 if (size
> 4 && (dc
->tb_flags
& MSR_EE_FLAG
)
960 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)) {
961 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
962 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
966 LOG_DIS("l%d%s%s\n", size
, dc
->type_b
? "i" : "", rev
? "r" : "");
969 addr
= compute_ldst_addr(dc
, &t
);
972 * When doing reverse accesses we need to do two things.
974 * 1. Reverse the address wrt endianness.
975 * 2. Byteswap the data lanes on the way back into the CPU core.
977 if (rev
&& size
!= 4) {
978 /* Endian reverse the address. t is addr. */
986 TCGv low
= tcg_temp_new();
988 /* Force addr into the temp. */
991 tcg_gen_mov_tl(t
, *addr
);
995 tcg_gen_andi_tl(low
, t
, 3);
996 tcg_gen_sub_tl(low
, tcg_const_tl(3), low
);
997 tcg_gen_andi_tl(t
, t
, ~3);
998 tcg_gen_or_tl(t
, t
, low
);
999 tcg_gen_mov_tl(env_imm
, t
);
1007 /* Force addr into the temp. */
1010 tcg_gen_xori_tl(t
, *addr
, 2);
1013 tcg_gen_xori_tl(t
, t
, 2);
1017 cpu_abort(dc
->env
, "Invalid reverse size\n");
1022 /* If we get a fault on a dslot, the jmpstate better be in sync. */
1025 /* Verify alignment if needed. */
1026 if ((dc
->env
->pvr
.regs
[2] & PVR2_UNALIGNED_EXC_MASK
) && size
> 1) {
1027 TCGv v
= tcg_temp_new();
1030 * Microblaze gives MMU faults priority over faults due to
1031 * unaligned addresses. That's why we speculatively do the load
1032 * into v. If the load succeeds, we verify alignment of the
1033 * address and if that succeeds we write into the destination reg.
1035 gen_load(dc
, v
, *addr
, size
);
1037 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
1038 gen_helper_memalign(*addr
, tcg_const_tl(dc
->rd
),
1039 tcg_const_tl(0), tcg_const_tl(size
- 1));
1042 dec_byteswap(dc
, cpu_R
[dc
->rd
], v
, size
);
1044 tcg_gen_mov_tl(cpu_R
[dc
->rd
], v
);
1050 gen_load(dc
, cpu_R
[dc
->rd
], *addr
, size
);
1052 dec_byteswap(dc
, cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], size
);
1055 /* We are loading into r0, no need to reverse. */
1056 gen_load(dc
, env_imm
, *addr
, size
);
1064 static void gen_store(DisasContext
*dc
, TCGv addr
, TCGv val
,
1067 int mem_index
= cpu_mmu_index(dc
->env
);
1070 tcg_gen_qemu_st8(val
, addr
, mem_index
);
1071 else if (size
== 2) {
1072 tcg_gen_qemu_st16(val
, addr
, mem_index
);
1073 } else if (size
== 4) {
1074 tcg_gen_qemu_st32(val
, addr
, mem_index
);
1076 cpu_abort(dc
->env
, "Incorrect store size %d\n", size
);
1079 static void dec_store(DisasContext
*dc
)
1082 unsigned int size
, rev
= 0;
1084 size
= 1 << (dc
->opcode
& 3);
1086 rev
= (dc
->ir
>> 9) & 1;
1089 if (size
> 4 && (dc
->tb_flags
& MSR_EE_FLAG
)
1090 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)) {
1091 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1092 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1096 LOG_DIS("s%d%s%s\n", size
, dc
->type_b
? "i" : "", rev
? "r" : "");
1098 /* If we get a fault on a dslot, the jmpstate better be in sync. */
1100 addr
= compute_ldst_addr(dc
, &t
);
1102 if (rev
&& size
!= 4) {
1103 /* Endian reverse the address. t is addr. */
1111 TCGv low
= tcg_temp_new();
1113 /* Force addr into the temp. */
1116 tcg_gen_mov_tl(t
, *addr
);
1120 tcg_gen_andi_tl(low
, t
, 3);
1121 tcg_gen_sub_tl(low
, tcg_const_tl(3), low
);
1122 tcg_gen_andi_tl(t
, t
, ~3);
1123 tcg_gen_or_tl(t
, t
, low
);
1124 tcg_gen_mov_tl(env_imm
, t
);
1132 /* Force addr into the temp. */
1135 tcg_gen_xori_tl(t
, *addr
, 2);
1138 tcg_gen_xori_tl(t
, t
, 2);
1142 cpu_abort(dc
->env
, "Invalid reverse size\n");
1147 TCGv bs_data
= tcg_temp_new();
1148 dec_byteswap(dc
, bs_data
, cpu_R
[dc
->rd
], size
);
1149 gen_store(dc
, *addr
, bs_data
, size
);
1150 tcg_temp_free(bs_data
);
1152 gen_store(dc
, *addr
, cpu_R
[dc
->rd
], size
);
1156 TCGv bs_data
= tcg_temp_new();
1157 dec_byteswap(dc
, bs_data
, cpu_R
[dc
->rd
], size
);
1158 gen_store(dc
, *addr
, bs_data
, size
);
1159 tcg_temp_free(bs_data
);
1161 gen_store(dc
, *addr
, cpu_R
[dc
->rd
], size
);
1165 /* Verify alignment if needed. */
1166 if ((dc
->env
->pvr
.regs
[2] & PVR2_UNALIGNED_EXC_MASK
) && size
> 1) {
1167 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
1168 /* FIXME: if the alignment is wrong, we should restore the value
1169 * in memory. One possible way to achieve this is to probe
1170 * the MMU prior to the memaccess, thay way we could put
1171 * the alignment checks in between the probe and the mem
1174 gen_helper_memalign(*addr
, tcg_const_tl(dc
->rd
),
1175 tcg_const_tl(1), tcg_const_tl(size
- 1));
1182 static inline void eval_cc(DisasContext
*dc
, unsigned int cc
,
1183 TCGv d
, TCGv a
, TCGv b
)
1187 tcg_gen_setcond_tl(TCG_COND_EQ
, d
, a
, b
);
1190 tcg_gen_setcond_tl(TCG_COND_NE
, d
, a
, b
);
1193 tcg_gen_setcond_tl(TCG_COND_LT
, d
, a
, b
);
1196 tcg_gen_setcond_tl(TCG_COND_LE
, d
, a
, b
);
1199 tcg_gen_setcond_tl(TCG_COND_GE
, d
, a
, b
);
1202 tcg_gen_setcond_tl(TCG_COND_GT
, d
, a
, b
);
1205 cpu_abort(dc
->env
, "Unknown condition code %x.\n", cc
);
1210 static void eval_cond_jmp(DisasContext
*dc
, TCGv pc_true
, TCGv pc_false
)
1214 l1
= gen_new_label();
1215 /* Conditional jmp. */
1216 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_false
);
1217 tcg_gen_brcondi_tl(TCG_COND_EQ
, env_btaken
, 0, l1
);
1218 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_true
);
1222 static void dec_bcc(DisasContext
*dc
)
1227 cc
= EXTRACT_FIELD(dc
->ir
, 21, 23);
1228 dslot
= dc
->ir
& (1 << 25);
1229 LOG_DIS("bcc%s r%d %x\n", dslot
? "d" : "", dc
->ra
, dc
->imm
);
1231 dc
->delayed_branch
= 1;
1233 dc
->delayed_branch
= 2;
1234 dc
->tb_flags
|= D_FLAG
;
1235 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
1236 cpu_env
, offsetof(CPUMBState
, bimm
));
1239 if (dec_alu_op_b_is_small_imm(dc
)) {
1240 int32_t offset
= (int32_t)((int16_t)dc
->imm
); /* sign-extend. */
1242 tcg_gen_movi_tl(env_btarget
, dc
->pc
+ offset
);
1243 dc
->jmp
= JMP_DIRECT_CC
;
1244 dc
->jmp_pc
= dc
->pc
+ offset
;
1246 dc
->jmp
= JMP_INDIRECT
;
1247 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
1248 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
1250 eval_cc(dc
, cc
, env_btaken
, cpu_R
[dc
->ra
], tcg_const_tl(0));
1253 static void dec_br(DisasContext
*dc
)
1255 unsigned int dslot
, link
, abs
, mbar
;
1256 int mem_index
= cpu_mmu_index(dc
->env
);
1258 dslot
= dc
->ir
& (1 << 20);
1259 abs
= dc
->ir
& (1 << 19);
1260 link
= dc
->ir
& (1 << 18);
1262 /* Memory barrier. */
1263 mbar
= (dc
->ir
>> 16) & 31;
1264 if (mbar
== 2 && dc
->imm
== 4) {
1265 LOG_DIS("mbar %d\n", dc
->rd
);
1267 dc
->cpustate_changed
= 1;
1271 LOG_DIS("br%s%s%s%s imm=%x\n",
1272 abs
? "a" : "", link
? "l" : "",
1273 dc
->type_b
? "i" : "", dslot
? "d" : "",
1276 dc
->delayed_branch
= 1;
1278 dc
->delayed_branch
= 2;
1279 dc
->tb_flags
|= D_FLAG
;
1280 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
1281 cpu_env
, offsetof(CPUMBState
, bimm
));
1284 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
1286 dc
->jmp
= JMP_INDIRECT
;
1288 tcg_gen_movi_tl(env_btaken
, 1);
1289 tcg_gen_mov_tl(env_btarget
, *(dec_alu_op_b(dc
)));
1290 if (link
&& !dslot
) {
1291 if (!(dc
->tb_flags
& IMM_FLAG
) && (dc
->imm
== 8 || dc
->imm
== 0x18))
1292 t_gen_raise_exception(dc
, EXCP_BREAK
);
1294 if ((dc
->tb_flags
& MSR_EE_FLAG
) && mem_index
== MMU_USER_IDX
) {
1295 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1296 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1300 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1304 if (dec_alu_op_b_is_small_imm(dc
)) {
1305 dc
->jmp
= JMP_DIRECT
;
1306 dc
->jmp_pc
= dc
->pc
+ (int32_t)((int16_t)dc
->imm
);
1308 tcg_gen_movi_tl(env_btaken
, 1);
1309 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
1310 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
1315 static inline void do_rti(DisasContext
*dc
)
1318 t0
= tcg_temp_new();
1319 t1
= tcg_temp_new();
1320 tcg_gen_shri_tl(t0
, cpu_SR
[SR_MSR
], 1);
1321 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_IE
);
1322 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
1324 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
1325 tcg_gen_or_tl(t1
, t1
, t0
);
1329 dc
->tb_flags
&= ~DRTI_FLAG
;
1332 static inline void do_rtb(DisasContext
*dc
)
1335 t0
= tcg_temp_new();
1336 t1
= tcg_temp_new();
1337 tcg_gen_andi_tl(t1
, cpu_SR
[SR_MSR
], ~MSR_BIP
);
1338 tcg_gen_shri_tl(t0
, t1
, 1);
1339 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
1341 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
1342 tcg_gen_or_tl(t1
, t1
, t0
);
1346 dc
->tb_flags
&= ~DRTB_FLAG
;
1349 static inline void do_rte(DisasContext
*dc
)
1352 t0
= tcg_temp_new();
1353 t1
= tcg_temp_new();
1355 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_EE
);
1356 tcg_gen_andi_tl(t1
, t1
, ~MSR_EIP
);
1357 tcg_gen_shri_tl(t0
, t1
, 1);
1358 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
1360 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
1361 tcg_gen_or_tl(t1
, t1
, t0
);
1365 dc
->tb_flags
&= ~DRTE_FLAG
;
1368 static void dec_rts(DisasContext
*dc
)
1370 unsigned int b_bit
, i_bit
, e_bit
;
1371 int mem_index
= cpu_mmu_index(dc
->env
);
1373 i_bit
= dc
->ir
& (1 << 21);
1374 b_bit
= dc
->ir
& (1 << 22);
1375 e_bit
= dc
->ir
& (1 << 23);
1377 dc
->delayed_branch
= 2;
1378 dc
->tb_flags
|= D_FLAG
;
1379 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
1380 cpu_env
, offsetof(CPUMBState
, bimm
));
1383 LOG_DIS("rtid ir=%x\n", dc
->ir
);
1384 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1385 && mem_index
== MMU_USER_IDX
) {
1386 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1387 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1389 dc
->tb_flags
|= DRTI_FLAG
;
1391 LOG_DIS("rtbd ir=%x\n", dc
->ir
);
1392 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1393 && mem_index
== MMU_USER_IDX
) {
1394 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1395 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1397 dc
->tb_flags
|= DRTB_FLAG
;
1399 LOG_DIS("rted ir=%x\n", dc
->ir
);
1400 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1401 && mem_index
== MMU_USER_IDX
) {
1402 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1403 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1405 dc
->tb_flags
|= DRTE_FLAG
;
1407 LOG_DIS("rts ir=%x\n", dc
->ir
);
1409 dc
->jmp
= JMP_INDIRECT
;
1410 tcg_gen_movi_tl(env_btaken
, 1);
1411 tcg_gen_add_tl(env_btarget
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
1414 static int dec_check_fpuv2(DisasContext
*dc
)
1418 r
= dc
->env
->pvr
.regs
[2] & PVR2_USE_FPU2_MASK
;
1420 if (!r
&& (dc
->tb_flags
& MSR_EE_FLAG
)) {
1421 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_FPU
);
1422 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1427 static void dec_fpu(DisasContext
*dc
)
1429 unsigned int fpu_insn
;
1431 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1432 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
1433 && !((dc
->env
->pvr
.regs
[2] & PVR2_USE_FPU_MASK
))) {
1434 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1435 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1439 fpu_insn
= (dc
->ir
>> 7) & 7;
1443 gen_helper_fadd(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1447 gen_helper_frsub(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1451 gen_helper_fmul(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1455 gen_helper_fdiv(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1459 switch ((dc
->ir
>> 4) & 7) {
1461 gen_helper_fcmp_un(cpu_R
[dc
->rd
],
1462 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1465 gen_helper_fcmp_lt(cpu_R
[dc
->rd
],
1466 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1469 gen_helper_fcmp_eq(cpu_R
[dc
->rd
],
1470 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1473 gen_helper_fcmp_le(cpu_R
[dc
->rd
],
1474 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1477 gen_helper_fcmp_gt(cpu_R
[dc
->rd
],
1478 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1481 gen_helper_fcmp_ne(cpu_R
[dc
->rd
],
1482 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1485 gen_helper_fcmp_ge(cpu_R
[dc
->rd
],
1486 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1489 qemu_log ("unimplemented fcmp fpu_insn=%x pc=%x opc=%x\n",
1490 fpu_insn
, dc
->pc
, dc
->opcode
);
1491 dc
->abort_at_next_insn
= 1;
1497 if (!dec_check_fpuv2(dc
)) {
1500 gen_helper_flt(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
1504 if (!dec_check_fpuv2(dc
)) {
1507 gen_helper_fint(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
1511 if (!dec_check_fpuv2(dc
)) {
1514 gen_helper_fsqrt(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
1518 qemu_log ("unimplemented FPU insn fpu_insn=%x pc=%x opc=%x\n",
1519 fpu_insn
, dc
->pc
, dc
->opcode
);
1520 dc
->abort_at_next_insn
= 1;
1525 static void dec_null(DisasContext
*dc
)
1527 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1528 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)) {
1529 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1530 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1533 qemu_log ("unknown insn pc=%x opc=%x\n", dc
->pc
, dc
->opcode
);
1534 dc
->abort_at_next_insn
= 1;
1537 /* Insns connected to FSL or AXI stream attached devices. */
1538 static void dec_stream(DisasContext
*dc
)
1540 int mem_index
= cpu_mmu_index(dc
->env
);
1541 TCGv_i32 t_id
, t_ctrl
;
1544 LOG_DIS("%s%s imm=%x\n", dc
->rd
? "get" : "put",
1545 dc
->type_b
? "" : "d", dc
->imm
);
1547 if ((dc
->tb_flags
& MSR_EE_FLAG
) && (mem_index
== MMU_USER_IDX
)) {
1548 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1549 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1553 t_id
= tcg_temp_new();
1555 tcg_gen_movi_tl(t_id
, dc
->imm
& 0xf);
1556 ctrl
= dc
->imm
>> 10;
1558 tcg_gen_andi_tl(t_id
, cpu_R
[dc
->rb
], 0xf);
1559 ctrl
= dc
->imm
>> 5;
1562 t_ctrl
= tcg_const_tl(ctrl
);
1565 gen_helper_put(t_id
, t_ctrl
, cpu_R
[dc
->ra
]);
1567 gen_helper_get(cpu_R
[dc
->rd
], t_id
, t_ctrl
);
1569 tcg_temp_free(t_id
);
1570 tcg_temp_free(t_ctrl
);
1573 static struct decoder_info
{
1578 void (*dec
)(DisasContext
*dc
);
1586 {DEC_BARREL
, dec_barrel
},
1588 {DEC_ST
, dec_store
},
1597 {DEC_STREAM
, dec_stream
},
1601 static inline void decode(DisasContext
*dc
)
1606 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
)))
1607 tcg_gen_debug_insn_start(dc
->pc
);
1609 dc
->ir
= ir
= ldl_code(dc
->pc
);
1610 LOG_DIS("%8.8x\t", dc
->ir
);
1615 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1616 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
1617 && (dc
->env
->pvr
.regs
[2] & PVR2_OPCODE_0x0_ILL_MASK
)) {
1618 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1619 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1623 LOG_DIS("nr_nops=%d\t", dc
->nr_nops
);
1625 if (dc
->nr_nops
> 4)
1626 cpu_abort(dc
->env
, "fetching nop sequence\n");
1628 /* bit 2 seems to indicate insn type. */
1629 dc
->type_b
= ir
& (1 << 29);
1631 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
1632 dc
->rd
= EXTRACT_FIELD(ir
, 21, 25);
1633 dc
->ra
= EXTRACT_FIELD(ir
, 16, 20);
1634 dc
->rb
= EXTRACT_FIELD(ir
, 11, 15);
1635 dc
->imm
= EXTRACT_FIELD(ir
, 0, 15);
1637 /* Large switch for all insns. */
1638 for (i
= 0; i
< ARRAY_SIZE(decinfo
); i
++) {
1639 if ((dc
->opcode
& decinfo
[i
].mask
) == decinfo
[i
].bits
) {
1646 static void check_breakpoint(CPUMBState
*env
, DisasContext
*dc
)
1650 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
1651 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1652 if (bp
->pc
== dc
->pc
) {
1653 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1654 dc
->is_jmp
= DISAS_UPDATE
;
1660 /* generate intermediate code for basic block 'tb'. */
1662 gen_intermediate_code_internal(CPUMBState
*env
, TranslationBlock
*tb
,
1665 uint16_t *gen_opc_end
;
1668 struct DisasContext ctx
;
1669 struct DisasContext
*dc
= &ctx
;
1670 uint32_t next_page_start
, org_flags
;
1675 qemu_log_try_set_file(stderr
);
1680 org_flags
= dc
->synced_flags
= dc
->tb_flags
= tb
->flags
;
1682 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
1684 dc
->is_jmp
= DISAS_NEXT
;
1686 dc
->delayed_branch
= !!(dc
->tb_flags
& D_FLAG
);
1687 if (dc
->delayed_branch
) {
1688 dc
->jmp
= JMP_INDIRECT
;
1691 dc
->singlestep_enabled
= env
->singlestep_enabled
;
1692 dc
->cpustate_changed
= 0;
1693 dc
->abort_at_next_insn
= 0;
1697 cpu_abort(env
, "Microblaze: unaligned PC=%x\n", pc_start
);
1699 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1701 qemu_log("--------------\n");
1702 log_cpu_state(env
, 0);
1706 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1709 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1711 max_insns
= CF_COUNT_MASK
;
1717 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1718 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
1722 check_breakpoint(env
, dc
);
1725 j
= gen_opc_ptr
- gen_opc_buf
;
1729 gen_opc_instr_start
[lj
++] = 0;
1731 gen_opc_pc
[lj
] = dc
->pc
;
1732 gen_opc_instr_start
[lj
] = 1;
1733 gen_opc_icount
[lj
] = num_insns
;
1737 LOG_DIS("%8.8x:\t", dc
->pc
);
1739 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
1745 dc
->tb_flags
&= ~IMM_FLAG
;
1749 if (dc
->delayed_branch
) {
1750 dc
->delayed_branch
--;
1751 if (!dc
->delayed_branch
) {
1752 if (dc
->tb_flags
& DRTI_FLAG
)
1754 if (dc
->tb_flags
& DRTB_FLAG
)
1756 if (dc
->tb_flags
& DRTE_FLAG
)
1758 /* Clear the delay slot flag. */
1759 dc
->tb_flags
&= ~D_FLAG
;
1760 /* If it is a direct jump, try direct chaining. */
1761 if (dc
->jmp
== JMP_INDIRECT
) {
1762 eval_cond_jmp(dc
, env_btarget
, tcg_const_tl(dc
->pc
));
1763 dc
->is_jmp
= DISAS_JUMP
;
1764 } else if (dc
->jmp
== JMP_DIRECT
) {
1766 gen_goto_tb(dc
, 0, dc
->jmp_pc
);
1767 dc
->is_jmp
= DISAS_TB_JUMP
;
1768 } else if (dc
->jmp
== JMP_DIRECT_CC
) {
1772 l1
= gen_new_label();
1773 /* Conditional jmp. */
1774 tcg_gen_brcondi_tl(TCG_COND_NE
, env_btaken
, 0, l1
);
1775 gen_goto_tb(dc
, 1, dc
->pc
);
1777 gen_goto_tb(dc
, 0, dc
->jmp_pc
);
1779 dc
->is_jmp
= DISAS_TB_JUMP
;
1784 if (env
->singlestep_enabled
)
1786 } while (!dc
->is_jmp
&& !dc
->cpustate_changed
1787 && gen_opc_ptr
< gen_opc_end
1789 && (dc
->pc
< next_page_start
)
1790 && num_insns
< max_insns
);
1793 if (dc
->jmp
== JMP_DIRECT
|| dc
->jmp
== JMP_DIRECT_CC
) {
1794 if (dc
->tb_flags
& D_FLAG
) {
1795 dc
->is_jmp
= DISAS_UPDATE
;
1796 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1802 if (tb
->cflags
& CF_LAST_IO
)
1804 /* Force an update if the per-tb cpu state has changed. */
1805 if (dc
->is_jmp
== DISAS_NEXT
1806 && (dc
->cpustate_changed
|| org_flags
!= dc
->tb_flags
)) {
1807 dc
->is_jmp
= DISAS_UPDATE
;
1808 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1812 if (unlikely(env
->singlestep_enabled
)) {
1813 TCGv_i32 tmp
= tcg_const_i32(EXCP_DEBUG
);
1815 if (dc
->is_jmp
!= DISAS_JUMP
) {
1816 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1818 gen_helper_raise_exception(tmp
);
1819 tcg_temp_free_i32(tmp
);
1821 switch(dc
->is_jmp
) {
1823 gen_goto_tb(dc
, 1, npc
);
1828 /* indicate that the hash table must be used
1829 to find the next TB */
1833 /* nothing more to generate */
1837 gen_icount_end(tb
, num_insns
);
1838 *gen_opc_ptr
= INDEX_op_end
;
1840 j
= gen_opc_ptr
- gen_opc_buf
;
1843 gen_opc_instr_start
[lj
++] = 0;
1845 tb
->size
= dc
->pc
- pc_start
;
1846 tb
->icount
= num_insns
;
1851 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1854 log_target_disas(pc_start
, dc
->pc
- pc_start
, 0);
1856 qemu_log("\nisize=%d osize=%td\n",
1857 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
1861 assert(!dc
->abort_at_next_insn
);
1864 void gen_intermediate_code (CPUMBState
*env
, struct TranslationBlock
*tb
)
1866 gen_intermediate_code_internal(env
, tb
, 0);
1869 void gen_intermediate_code_pc (CPUMBState
*env
, struct TranslationBlock
*tb
)
1871 gen_intermediate_code_internal(env
, tb
, 1);
1874 void cpu_dump_state (CPUMBState
*env
, FILE *f
, fprintf_function cpu_fprintf
,
1882 cpu_fprintf(f
, "IN: PC=%x %s\n",
1883 env
->sregs
[SR_PC
], lookup_symbol(env
->sregs
[SR_PC
]));
1884 cpu_fprintf(f
, "rmsr=%x resr=%x rear=%x debug=%x imm=%x iflags=%x fsr=%x\n",
1885 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
], env
->sregs
[SR_EAR
],
1886 env
->debug
, env
->imm
, env
->iflags
, env
->sregs
[SR_FSR
]);
1887 cpu_fprintf(f
, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1888 env
->btaken
, env
->btarget
,
1889 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
1890 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel",
1891 (env
->sregs
[SR_MSR
] & MSR_EIP
),
1892 (env
->sregs
[SR_MSR
] & MSR_IE
));
1894 for (i
= 0; i
< 32; i
++) {
1895 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1896 if ((i
+ 1) % 4 == 0)
1897 cpu_fprintf(f
, "\n");
1899 cpu_fprintf(f
, "\n\n");
1902 CPUMBState
*cpu_mb_init (const char *cpu_model
)
1906 static int tcg_initialized
= 0;
1909 cpu
= MICROBLAZE_CPU(object_new(TYPE_MICROBLAZE_CPU
));
1912 cpu_reset(CPU(cpu
));
1913 qemu_init_vcpu(env
);
1915 if (tcg_initialized
)
1918 tcg_initialized
= 1;
1920 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1922 env_debug
= tcg_global_mem_new(TCG_AREG0
,
1923 offsetof(CPUMBState
, debug
),
1925 env_iflags
= tcg_global_mem_new(TCG_AREG0
,
1926 offsetof(CPUMBState
, iflags
),
1928 env_imm
= tcg_global_mem_new(TCG_AREG0
,
1929 offsetof(CPUMBState
, imm
),
1931 env_btarget
= tcg_global_mem_new(TCG_AREG0
,
1932 offsetof(CPUMBState
, btarget
),
1934 env_btaken
= tcg_global_mem_new(TCG_AREG0
,
1935 offsetof(CPUMBState
, btaken
),
1937 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1938 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
1939 offsetof(CPUMBState
, regs
[i
]),
1942 for (i
= 0; i
< ARRAY_SIZE(cpu_SR
); i
++) {
1943 cpu_SR
[i
] = tcg_global_mem_new(TCG_AREG0
,
1944 offsetof(CPUMBState
, sregs
[i
]),
1945 special_regnames
[i
]);
1947 #define GEN_HELPER 2
1953 void cpu_state_reset(CPUMBState
*env
)
1955 cpu_reset(ENV_GET_CPU(env
));
1958 void restore_state_to_opc(CPUMBState
*env
, TranslationBlock
*tb
, int pc_pos
)
1960 env
->sregs
[SR_PC
] = gen_opc_pc
[pc_pos
];