2 * Xilinx MicroBlaze emulation for qemu: main translation routines.
4 * Copyright (c) 2009 Edgar E. Iglesias.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
32 #include "microblaze-decode.h"
33 #include "qemu-common.h"
41 #if DISAS_MB && !SIM_COMPAT
42 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
44 # define LOG_DIS(...) do { } while (0)
49 #define EXTRACT_FIELD(src, start, end) \
50 (((src) >> start) & ((1 << (end - start + 1)) - 1))
52 static TCGv env_debug
;
53 static TCGv_ptr cpu_env
;
54 static TCGv cpu_R
[32];
55 static TCGv cpu_SR
[18];
57 static TCGv env_btaken
;
58 static TCGv env_btarget
;
59 static TCGv env_iflags
;
61 #include "gen-icount.h"
63 /* This is the state at translation time. */
64 typedef struct DisasContext
{
75 unsigned int cpustate_changed
;
76 unsigned int delayed_branch
;
77 unsigned int tb_flags
, synced_flags
; /* tb dependent flags. */
78 unsigned int clear_imm
;
83 #define JMP_DIRECT_CC 2
84 #define JMP_INDIRECT 3
88 int abort_at_next_insn
;
90 struct TranslationBlock
*tb
;
91 int singlestep_enabled
;
94 static const char *regnames
[] =
96 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
97 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
98 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
99 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
102 static const char *special_regnames
[] =
104 "rpc", "rmsr", "sr2", "sr3", "sr4", "sr5", "sr6", "sr7",
105 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
106 "sr16", "sr17", "sr18"
109 /* Sign extend at translation time. */
110 static inline int sign_extend(unsigned int val
, unsigned int width
)
122 static inline void t_sync_flags(DisasContext
*dc
)
124 /* Synch the tb dependant flags between translator and runtime. */
125 if (dc
->tb_flags
!= dc
->synced_flags
) {
126 tcg_gen_movi_tl(env_iflags
, dc
->tb_flags
);
127 dc
->synced_flags
= dc
->tb_flags
;
131 static inline void t_gen_raise_exception(DisasContext
*dc
, uint32_t index
)
133 TCGv_i32 tmp
= tcg_const_i32(index
);
136 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
137 gen_helper_raise_exception(tmp
);
138 tcg_temp_free_i32(tmp
);
139 dc
->is_jmp
= DISAS_UPDATE
;
142 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
144 TranslationBlock
*tb
;
146 if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
)) {
148 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dest
);
149 tcg_gen_exit_tb((long)tb
+ n
);
151 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dest
);
156 static void read_carry(DisasContext
*dc
, TCGv d
)
158 tcg_gen_shri_tl(d
, cpu_SR
[SR_MSR
], 31);
161 static void write_carry(DisasContext
*dc
, TCGv v
)
163 TCGv t0
= tcg_temp_new();
164 tcg_gen_shli_tl(t0
, v
, 31);
165 tcg_gen_sari_tl(t0
, t0
, 31);
166 tcg_gen_andi_tl(t0
, t0
, (MSR_C
| MSR_CC
));
167 tcg_gen_andi_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
],
169 tcg_gen_or_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], t0
);
173 /* True if ALU operand b is a small immediate that may deserve
175 static inline int dec_alu_op_b_is_small_imm(DisasContext
*dc
)
177 /* Immediate insn without the imm prefix ? */
178 return dc
->type_b
&& !(dc
->tb_flags
& IMM_FLAG
);
181 static inline TCGv
*dec_alu_op_b(DisasContext
*dc
)
184 if (dc
->tb_flags
& IMM_FLAG
)
185 tcg_gen_ori_tl(env_imm
, env_imm
, dc
->imm
);
187 tcg_gen_movi_tl(env_imm
, (int32_t)((int16_t)dc
->imm
));
190 return &cpu_R
[dc
->rb
];
193 static void dec_add(DisasContext
*dc
)
201 LOG_DIS("add%s%s%s r%d r%d r%d\n",
202 dc
->type_b
? "i" : "", k
? "k" : "", c
? "c" : "",
203 dc
->rd
, dc
->ra
, dc
->rb
);
205 /* Take care of the easy cases first. */
207 /* k - keep carry, no need to update MSR. */
208 /* If rd == r0, it's a nop. */
210 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
213 /* c - Add carry into the result. */
217 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
224 /* From now on, we can assume k is zero. So we need to update MSR. */
230 tcg_gen_movi_tl(cf
, 0);
234 TCGv ncf
= tcg_temp_new();
235 gen_helper_carry(ncf
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)), cf
);
236 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
237 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
238 write_carry(dc
, ncf
);
241 gen_helper_carry(cf
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)), cf
);
247 static void dec_sub(DisasContext
*dc
)
249 unsigned int u
, cmp
, k
, c
;
255 cmp
= (dc
->imm
& 1) && (!dc
->type_b
) && k
;
258 LOG_DIS("cmp%s r%d, r%d ir=%x\n", u
? "u" : "", dc
->rd
, dc
->ra
, dc
->ir
);
261 gen_helper_cmpu(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
263 gen_helper_cmp(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
268 LOG_DIS("sub%s%s r%d, r%d r%d\n",
269 k
? "k" : "", c
? "c" : "", dc
->rd
, dc
->ra
, dc
->rb
);
271 /* Take care of the easy cases first. */
273 /* k - keep carry, no need to update MSR. */
274 /* If rd == r0, it's a nop. */
276 tcg_gen_sub_tl(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
279 /* c - Add carry into the result. */
283 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
290 /* From now on, we can assume k is zero. So we need to update MSR. */
291 /* Extract carry. And complement a into na. */
297 tcg_gen_movi_tl(cf
, 1);
300 /* d = b + ~a + c. carry defaults to 1. */
301 tcg_gen_not_tl(na
, cpu_R
[dc
->ra
]);
304 TCGv ncf
= tcg_temp_new();
305 gen_helper_carry(ncf
, na
, *(dec_alu_op_b(dc
)), cf
);
306 tcg_gen_add_tl(cpu_R
[dc
->rd
], na
, *(dec_alu_op_b(dc
)));
307 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], cf
);
308 write_carry(dc
, ncf
);
311 gen_helper_carry(cf
, na
, *(dec_alu_op_b(dc
)), cf
);
318 static void dec_pattern(DisasContext
*dc
)
323 if ((dc
->tb_flags
& MSR_EE_FLAG
)
324 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
325 && !((dc
->env
->pvr
.regs
[2] & PVR2_USE_PCMP_INSTR
))) {
326 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
327 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
330 mode
= dc
->opcode
& 3;
334 LOG_DIS("pcmpbf r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
336 gen_helper_pcmpbf(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
339 LOG_DIS("pcmpeq r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
341 TCGv t0
= tcg_temp_local_new();
342 l1
= gen_new_label();
343 tcg_gen_movi_tl(t0
, 1);
344 tcg_gen_brcond_tl(TCG_COND_EQ
,
345 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
346 tcg_gen_movi_tl(t0
, 0);
348 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
353 LOG_DIS("pcmpne r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
354 l1
= gen_new_label();
356 TCGv t0
= tcg_temp_local_new();
357 tcg_gen_movi_tl(t0
, 1);
358 tcg_gen_brcond_tl(TCG_COND_NE
,
359 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
360 tcg_gen_movi_tl(t0
, 0);
362 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
368 "unsupported pattern insn opcode=%x\n", dc
->opcode
);
373 static void dec_and(DisasContext
*dc
)
377 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
382 not = dc
->opcode
& (1 << 1);
383 LOG_DIS("and%s\n", not ? "n" : "");
389 TCGv t
= tcg_temp_new();
390 tcg_gen_not_tl(t
, *(dec_alu_op_b(dc
)));
391 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t
);
394 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
397 static void dec_or(DisasContext
*dc
)
399 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
404 LOG_DIS("or r%d r%d r%d imm=%x\n", dc
->rd
, dc
->ra
, dc
->rb
, dc
->imm
);
406 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
409 static void dec_xor(DisasContext
*dc
)
411 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
416 LOG_DIS("xor r%d\n", dc
->rd
);
418 tcg_gen_xor_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
421 static inline void msr_read(DisasContext
*dc
, TCGv d
)
423 tcg_gen_mov_tl(d
, cpu_SR
[SR_MSR
]);
426 static inline void msr_write(DisasContext
*dc
, TCGv v
)
428 dc
->cpustate_changed
= 1;
429 tcg_gen_mov_tl(cpu_SR
[SR_MSR
], v
);
430 /* PVR, we have a processor version register. */
431 tcg_gen_ori_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], (1 << 10));
434 static void dec_msr(DisasContext
*dc
)
437 unsigned int sr
, to
, rn
;
438 int mem_index
= cpu_mmu_index(dc
->env
);
440 sr
= dc
->imm
& ((1 << 14) - 1);
441 to
= dc
->imm
& (1 << 14);
444 dc
->cpustate_changed
= 1;
446 /* msrclr and msrset. */
447 if (!(dc
->imm
& (1 << 15))) {
448 unsigned int clr
= dc
->ir
& (1 << 16);
450 LOG_DIS("msr%s r%d imm=%x\n", clr
? "clr" : "set",
453 if (!(dc
->env
->pvr
.regs
[2] & PVR2_USE_MSR_INSTR
)) {
458 if ((dc
->tb_flags
& MSR_EE_FLAG
)
459 && mem_index
== MMU_USER_IDX
&& (dc
->imm
!= 4 && dc
->imm
!= 0)) {
460 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
461 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
466 msr_read(dc
, cpu_R
[dc
->rd
]);
471 tcg_gen_mov_tl(t1
, *(dec_alu_op_b(dc
)));
474 tcg_gen_not_tl(t1
, t1
);
475 tcg_gen_and_tl(t0
, t0
, t1
);
477 tcg_gen_or_tl(t0
, t0
, t1
);
481 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
+ 4);
482 dc
->is_jmp
= DISAS_UPDATE
;
487 if ((dc
->tb_flags
& MSR_EE_FLAG
)
488 && mem_index
== MMU_USER_IDX
) {
489 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
490 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
495 #if !defined(CONFIG_USER_ONLY)
496 /* Catch read/writes to the mmu block. */
497 if ((sr
& ~0xff) == 0x1000) {
499 LOG_DIS("m%ss sr%d r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
501 gen_helper_mmu_write(tcg_const_tl(sr
), cpu_R
[dc
->ra
]);
503 gen_helper_mmu_read(cpu_R
[dc
->rd
], tcg_const_tl(sr
));
509 LOG_DIS("m%ss sr%x r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
514 msr_write(dc
, cpu_R
[dc
->ra
]);
517 tcg_gen_mov_tl(cpu_SR
[SR_EAR
], cpu_R
[dc
->ra
]);
520 tcg_gen_mov_tl(cpu_SR
[SR_ESR
], cpu_R
[dc
->ra
]);
523 tcg_gen_andi_tl(cpu_SR
[SR_FSR
], cpu_R
[dc
->ra
], 31);
526 cpu_abort(dc
->env
, "unknown mts reg %x\n", sr
);
530 LOG_DIS("m%ss r%d sr%x imm=%x\n", to
? "t" : "f", dc
->rd
, sr
, dc
->imm
);
534 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
537 msr_read(dc
, cpu_R
[dc
->rd
]);
540 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_EAR
]);
543 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_ESR
]);
546 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_FSR
]);
549 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_BTR
]);
565 tcg_gen_ld_tl(cpu_R
[dc
->rd
],
566 cpu_env
, offsetof(CPUState
, pvr
.regs
[rn
]));
569 cpu_abort(dc
->env
, "unknown mfs reg %x\n", sr
);
575 tcg_gen_movi_tl(cpu_R
[0], 0);
579 /* 64-bit signed mul, lower result in d and upper in d2. */
580 static void t_gen_muls(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
584 t0
= tcg_temp_new_i64();
585 t1
= tcg_temp_new_i64();
587 tcg_gen_ext_i32_i64(t0
, a
);
588 tcg_gen_ext_i32_i64(t1
, b
);
589 tcg_gen_mul_i64(t0
, t0
, t1
);
591 tcg_gen_trunc_i64_i32(d
, t0
);
592 tcg_gen_shri_i64(t0
, t0
, 32);
593 tcg_gen_trunc_i64_i32(d2
, t0
);
595 tcg_temp_free_i64(t0
);
596 tcg_temp_free_i64(t1
);
599 /* 64-bit unsigned muls, lower result in d and upper in d2. */
600 static void t_gen_mulu(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
604 t0
= tcg_temp_new_i64();
605 t1
= tcg_temp_new_i64();
607 tcg_gen_extu_i32_i64(t0
, a
);
608 tcg_gen_extu_i32_i64(t1
, b
);
609 tcg_gen_mul_i64(t0
, t0
, t1
);
611 tcg_gen_trunc_i64_i32(d
, t0
);
612 tcg_gen_shri_i64(t0
, t0
, 32);
613 tcg_gen_trunc_i64_i32(d2
, t0
);
615 tcg_temp_free_i64(t0
);
616 tcg_temp_free_i64(t1
);
619 /* Multiplier unit. */
620 static void dec_mul(DisasContext
*dc
)
623 unsigned int subcode
;
625 if ((dc
->tb_flags
& MSR_EE_FLAG
)
626 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
627 && !(dc
->env
->pvr
.regs
[0] & PVR0_USE_HW_MUL_MASK
)) {
628 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
629 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
633 subcode
= dc
->imm
& 3;
634 d
[0] = tcg_temp_new();
635 d
[1] = tcg_temp_new();
638 LOG_DIS("muli r%d r%d %x\n", dc
->rd
, dc
->ra
, dc
->imm
);
639 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
643 /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2. */
644 if (subcode
>= 1 && subcode
<= 3
645 && !((dc
->env
->pvr
.regs
[2] & PVR2_USE_MUL64_MASK
))) {
651 LOG_DIS("mul r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
652 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
655 LOG_DIS("mulh r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
656 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
659 LOG_DIS("mulhsu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
660 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
663 LOG_DIS("mulhu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
664 t_gen_mulu(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
667 cpu_abort(dc
->env
, "unknown MUL insn %x\n", subcode
);
676 static void dec_div(DisasContext
*dc
)
683 if ((dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
684 && !((dc
->env
->pvr
.regs
[0] & PVR0_USE_DIV_MASK
))) {
685 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
686 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
690 gen_helper_divu(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
692 gen_helper_divs(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
694 tcg_gen_movi_tl(cpu_R
[dc
->rd
], 0);
697 static void dec_barrel(DisasContext
*dc
)
702 if ((dc
->tb_flags
& MSR_EE_FLAG
)
703 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
704 && !(dc
->env
->pvr
.regs
[0] & PVR0_USE_BARREL_MASK
)) {
705 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
706 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
710 s
= dc
->imm
& (1 << 10);
711 t
= dc
->imm
& (1 << 9);
713 LOG_DIS("bs%s%s r%d r%d r%d\n",
714 s
? "l" : "r", t
? "a" : "l", dc
->rd
, dc
->ra
, dc
->rb
);
718 tcg_gen_mov_tl(t0
, *(dec_alu_op_b(dc
)));
719 tcg_gen_andi_tl(t0
, t0
, 31);
722 tcg_gen_shl_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
725 tcg_gen_sar_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
727 tcg_gen_shr_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
731 static void dec_bit(DisasContext
*dc
)
735 int mem_index
= cpu_mmu_index(dc
->env
);
737 op
= dc
->ir
& ((1 << 8) - 1);
743 LOG_DIS("src r%d r%d\n", dc
->rd
, dc
->ra
);
744 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
748 tcg_gen_shli_tl(t1
, t1
, 31);
750 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
751 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], t1
);
764 LOG_DIS("srl r%d r%d\n", dc
->rd
, dc
->ra
);
767 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
772 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
774 tcg_gen_sari_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
778 LOG_DIS("ext8s r%d r%d\n", dc
->rd
, dc
->ra
);
779 tcg_gen_ext8s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
782 LOG_DIS("ext16s r%d r%d\n", dc
->rd
, dc
->ra
);
783 tcg_gen_ext16s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
790 LOG_DIS("wdc r%d\n", dc
->ra
);
791 if ((dc
->tb_flags
& MSR_EE_FLAG
)
792 && mem_index
== MMU_USER_IDX
) {
793 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
794 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
800 LOG_DIS("wic r%d\n", dc
->ra
);
801 if ((dc
->tb_flags
& MSR_EE_FLAG
)
802 && mem_index
== MMU_USER_IDX
) {
803 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
804 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
809 cpu_abort(dc
->env
, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
810 dc
->pc
, op
, dc
->rd
, dc
->ra
, dc
->rb
);
815 static inline void sync_jmpstate(DisasContext
*dc
)
817 if (dc
->jmp
== JMP_DIRECT
|| dc
->jmp
== JMP_DIRECT_CC
) {
818 if (dc
->jmp
== JMP_DIRECT
) {
819 tcg_gen_movi_tl(env_btaken
, 1);
821 dc
->jmp
= JMP_INDIRECT
;
822 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
826 static void dec_imm(DisasContext
*dc
)
828 LOG_DIS("imm %x\n", dc
->imm
<< 16);
829 tcg_gen_movi_tl(env_imm
, (dc
->imm
<< 16));
830 dc
->tb_flags
|= IMM_FLAG
;
834 static inline void gen_load(DisasContext
*dc
, TCGv dst
, TCGv addr
,
837 int mem_index
= cpu_mmu_index(dc
->env
);
840 tcg_gen_qemu_ld8u(dst
, addr
, mem_index
);
841 } else if (size
== 2) {
842 tcg_gen_qemu_ld16u(dst
, addr
, mem_index
);
843 } else if (size
== 4) {
844 tcg_gen_qemu_ld32u(dst
, addr
, mem_index
);
846 cpu_abort(dc
->env
, "Incorrect load size %d\n", size
);
849 static inline TCGv
*compute_ldst_addr(DisasContext
*dc
, TCGv
*t
)
851 unsigned int extimm
= dc
->tb_flags
& IMM_FLAG
;
853 /* Treat the common cases first. */
855 /* If any of the regs is r0, return a ptr to the other. */
857 return &cpu_R
[dc
->rb
];
858 } else if (dc
->rb
== 0) {
859 return &cpu_R
[dc
->ra
];
863 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
869 return &cpu_R
[dc
->ra
];
872 tcg_gen_movi_tl(*t
, (int32_t)((int16_t)dc
->imm
));
873 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *t
);
876 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
882 static inline void dec_byteswap(DisasContext
*dc
, TCGv dst
, TCGv src
, int size
)
885 tcg_gen_bswap32_tl(dst
, src
);
886 } else if (size
== 2) {
887 TCGv t
= tcg_temp_new();
889 /* bswap16 assumes the high bits are zero. */
890 tcg_gen_andi_tl(t
, src
, 0xffff);
891 tcg_gen_bswap16_tl(dst
, t
);
895 cpu_abort(dc->env, "Invalid ldst byteswap size %d\n", size);
900 static void dec_load(DisasContext
*dc
)
903 unsigned int size
, rev
= 0;
905 size
= 1 << (dc
->opcode
& 3);
908 rev
= (dc
->ir
>> 9) & 1;
911 if (size
> 4 && (dc
->tb_flags
& MSR_EE_FLAG
)
912 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)) {
913 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
914 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
918 LOG_DIS("l%d%s%s\n", size
, dc
->type_b
? "i" : "", rev
? "r" : "");
921 addr
= compute_ldst_addr(dc
, &t
);
924 * When doing reverse accesses we need to do two things.
926 * 1. Reverse the address wrt endianess.
927 * 2. Byteswap the data lanes on the way back into the CPU core.
929 if (rev
&& size
!= 4) {
930 /* Endian reverse the address. t is addr. */
938 TCGv low
= tcg_temp_new();
940 /* Force addr into the temp. */
943 tcg_gen_mov_tl(t
, *addr
);
947 tcg_gen_andi_tl(low
, t
, 3);
948 tcg_gen_sub_tl(low
, tcg_const_tl(3), low
);
949 tcg_gen_andi_tl(t
, t
, ~3);
950 tcg_gen_or_tl(t
, t
, low
);
951 tcg_gen_mov_tl(env_imm
, t
);
959 /* Force addr into the temp. */
962 tcg_gen_xori_tl(t
, *addr
, 2);
965 tcg_gen_xori_tl(t
, t
, 2);
969 cpu_abort(dc
->env
, "Invalid reverse size\n");
974 /* If we get a fault on a dslot, the jmpstate better be in sync. */
977 /* Verify alignment if needed. */
978 if ((dc
->env
->pvr
.regs
[2] & PVR2_UNALIGNED_EXC_MASK
) && size
> 1) {
979 TCGv v
= tcg_temp_new();
982 * Microblaze gives MMU faults priority over faults due to
983 * unaligned addresses. That's why we speculatively do the load
984 * into v. If the load succeeds, we verify alignment of the
985 * address and if that succeeds we write into the destination reg.
987 gen_load(dc
, v
, *addr
, size
);
989 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
990 gen_helper_memalign(*addr
, tcg_const_tl(dc
->rd
),
991 tcg_const_tl(0), tcg_const_tl(size
- 1));
994 dec_byteswap(dc
, cpu_R
[dc
->rd
], v
, size
);
996 tcg_gen_mov_tl(cpu_R
[dc
->rd
], v
);
1002 gen_load(dc
, cpu_R
[dc
->rd
], *addr
, size
);
1004 dec_byteswap(dc
, cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], size
);
1007 /* We are loading into r0, no need to reverse. */
1008 gen_load(dc
, env_imm
, *addr
, size
);
1016 static void gen_store(DisasContext
*dc
, TCGv addr
, TCGv val
,
1019 int mem_index
= cpu_mmu_index(dc
->env
);
1022 tcg_gen_qemu_st8(val
, addr
, mem_index
);
1023 else if (size
== 2) {
1024 tcg_gen_qemu_st16(val
, addr
, mem_index
);
1025 } else if (size
== 4) {
1026 tcg_gen_qemu_st32(val
, addr
, mem_index
);
1028 cpu_abort(dc
->env
, "Incorrect store size %d\n", size
);
1031 static void dec_store(DisasContext
*dc
)
1034 unsigned int size
, rev
= 0;
1036 size
= 1 << (dc
->opcode
& 3);
1038 rev
= (dc
->ir
>> 9) & 1;
1041 if (size
> 4 && (dc
->tb_flags
& MSR_EE_FLAG
)
1042 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)) {
1043 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1044 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1048 LOG_DIS("s%d%s%s\n", size
, dc
->type_b
? "i" : "", rev
? "r" : "");
1050 /* If we get a fault on a dslot, the jmpstate better be in sync. */
1052 addr
= compute_ldst_addr(dc
, &t
);
1054 if (rev
&& size
!= 4) {
1055 /* Endian reverse the address. t is addr. */
1063 TCGv low
= tcg_temp_new();
1065 /* Force addr into the temp. */
1068 tcg_gen_mov_tl(t
, *addr
);
1072 tcg_gen_andi_tl(low
, t
, 3);
1073 tcg_gen_sub_tl(low
, tcg_const_tl(3), low
);
1074 tcg_gen_andi_tl(t
, t
, ~3);
1075 tcg_gen_or_tl(t
, t
, low
);
1076 tcg_gen_mov_tl(env_imm
, t
);
1084 /* Force addr into the temp. */
1087 tcg_gen_xori_tl(t
, *addr
, 2);
1090 tcg_gen_xori_tl(t
, t
, 2);
1094 cpu_abort(dc
->env
, "Invalid reverse size\n");
1099 TCGv bs_data
= tcg_temp_new();
1100 dec_byteswap(dc
, bs_data
, cpu_R
[dc
->rd
], size
);
1101 gen_store(dc
, *addr
, bs_data
, size
);
1102 tcg_temp_free(bs_data
);
1104 gen_store(dc
, *addr
, cpu_R
[dc
->rd
], size
);
1108 TCGv bs_data
= tcg_temp_new();
1109 dec_byteswap(dc
, bs_data
, cpu_R
[dc
->rd
], size
);
1110 gen_store(dc
, *addr
, bs_data
, size
);
1111 tcg_temp_free(bs_data
);
1113 gen_store(dc
, *addr
, cpu_R
[dc
->rd
], size
);
1117 /* Verify alignment if needed. */
1118 if ((dc
->env
->pvr
.regs
[2] & PVR2_UNALIGNED_EXC_MASK
) && size
> 1) {
1119 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
1120 /* FIXME: if the alignment is wrong, we should restore the value
1121 * in memory. One possible way to acheive this is to probe
1122 * the MMU prior to the memaccess, thay way we could put
1123 * the alignment checks in between the probe and the mem
1126 gen_helper_memalign(*addr
, tcg_const_tl(dc
->rd
),
1127 tcg_const_tl(1), tcg_const_tl(size
- 1));
1134 static inline void eval_cc(DisasContext
*dc
, unsigned int cc
,
1135 TCGv d
, TCGv a
, TCGv b
)
1139 tcg_gen_setcond_tl(TCG_COND_EQ
, d
, a
, b
);
1142 tcg_gen_setcond_tl(TCG_COND_NE
, d
, a
, b
);
1145 tcg_gen_setcond_tl(TCG_COND_LT
, d
, a
, b
);
1148 tcg_gen_setcond_tl(TCG_COND_LE
, d
, a
, b
);
1151 tcg_gen_setcond_tl(TCG_COND_GE
, d
, a
, b
);
1154 tcg_gen_setcond_tl(TCG_COND_GT
, d
, a
, b
);
1157 cpu_abort(dc
->env
, "Unknown condition code %x.\n", cc
);
1162 static void eval_cond_jmp(DisasContext
*dc
, TCGv pc_true
, TCGv pc_false
)
1166 l1
= gen_new_label();
1167 /* Conditional jmp. */
1168 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_false
);
1169 tcg_gen_brcondi_tl(TCG_COND_EQ
, env_btaken
, 0, l1
);
1170 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_true
);
1174 static void dec_bcc(DisasContext
*dc
)
1179 cc
= EXTRACT_FIELD(dc
->ir
, 21, 23);
1180 dslot
= dc
->ir
& (1 << 25);
1181 LOG_DIS("bcc%s r%d %x\n", dslot
? "d" : "", dc
->ra
, dc
->imm
);
1183 dc
->delayed_branch
= 1;
1185 dc
->delayed_branch
= 2;
1186 dc
->tb_flags
|= D_FLAG
;
1187 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
1188 cpu_env
, offsetof(CPUState
, bimm
));
1191 if (dec_alu_op_b_is_small_imm(dc
)) {
1192 int32_t offset
= (int32_t)((int16_t)dc
->imm
); /* sign-extend. */
1194 tcg_gen_movi_tl(env_btarget
, dc
->pc
+ offset
);
1195 dc
->jmp
= JMP_DIRECT_CC
;
1196 dc
->jmp_pc
= dc
->pc
+ offset
;
1198 dc
->jmp
= JMP_INDIRECT
;
1199 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
1200 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
1202 eval_cc(dc
, cc
, env_btaken
, cpu_R
[dc
->ra
], tcg_const_tl(0));
1205 static void dec_br(DisasContext
*dc
)
1207 unsigned int dslot
, link
, abs
;
1208 int mem_index
= cpu_mmu_index(dc
->env
);
1210 dslot
= dc
->ir
& (1 << 20);
1211 abs
= dc
->ir
& (1 << 19);
1212 link
= dc
->ir
& (1 << 18);
1213 LOG_DIS("br%s%s%s%s imm=%x\n",
1214 abs
? "a" : "", link
? "l" : "",
1215 dc
->type_b
? "i" : "", dslot
? "d" : "",
1218 dc
->delayed_branch
= 1;
1220 dc
->delayed_branch
= 2;
1221 dc
->tb_flags
|= D_FLAG
;
1222 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
1223 cpu_env
, offsetof(CPUState
, bimm
));
1226 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
1228 dc
->jmp
= JMP_INDIRECT
;
1230 tcg_gen_movi_tl(env_btaken
, 1);
1231 tcg_gen_mov_tl(env_btarget
, *(dec_alu_op_b(dc
)));
1232 if (link
&& !dslot
) {
1233 if (!(dc
->tb_flags
& IMM_FLAG
) && (dc
->imm
== 8 || dc
->imm
== 0x18))
1234 t_gen_raise_exception(dc
, EXCP_BREAK
);
1236 if ((dc
->tb_flags
& MSR_EE_FLAG
) && mem_index
== MMU_USER_IDX
) {
1237 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1238 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1242 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1246 if (dec_alu_op_b_is_small_imm(dc
)) {
1247 dc
->jmp
= JMP_DIRECT
;
1248 dc
->jmp_pc
= dc
->pc
+ (int32_t)((int16_t)dc
->imm
);
1250 tcg_gen_movi_tl(env_btaken
, 1);
1251 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
1252 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
1257 static inline void do_rti(DisasContext
*dc
)
1260 t0
= tcg_temp_new();
1261 t1
= tcg_temp_new();
1262 tcg_gen_shri_tl(t0
, cpu_SR
[SR_MSR
], 1);
1263 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_IE
);
1264 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
1266 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
1267 tcg_gen_or_tl(t1
, t1
, t0
);
1271 dc
->tb_flags
&= ~DRTI_FLAG
;
1274 static inline void do_rtb(DisasContext
*dc
)
1277 t0
= tcg_temp_new();
1278 t1
= tcg_temp_new();
1279 tcg_gen_andi_tl(t1
, cpu_SR
[SR_MSR
], ~MSR_BIP
);
1280 tcg_gen_shri_tl(t0
, t1
, 1);
1281 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
1283 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
1284 tcg_gen_or_tl(t1
, t1
, t0
);
1288 dc
->tb_flags
&= ~DRTB_FLAG
;
1291 static inline void do_rte(DisasContext
*dc
)
1294 t0
= tcg_temp_new();
1295 t1
= tcg_temp_new();
1297 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_EE
);
1298 tcg_gen_andi_tl(t1
, t1
, ~MSR_EIP
);
1299 tcg_gen_shri_tl(t0
, t1
, 1);
1300 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
1302 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
1303 tcg_gen_or_tl(t1
, t1
, t0
);
1307 dc
->tb_flags
&= ~DRTE_FLAG
;
1310 static void dec_rts(DisasContext
*dc
)
1312 unsigned int b_bit
, i_bit
, e_bit
;
1313 int mem_index
= cpu_mmu_index(dc
->env
);
1315 i_bit
= dc
->ir
& (1 << 21);
1316 b_bit
= dc
->ir
& (1 << 22);
1317 e_bit
= dc
->ir
& (1 << 23);
1319 dc
->delayed_branch
= 2;
1320 dc
->tb_flags
|= D_FLAG
;
1321 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
1322 cpu_env
, offsetof(CPUState
, bimm
));
1325 LOG_DIS("rtid ir=%x\n", dc
->ir
);
1326 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1327 && mem_index
== MMU_USER_IDX
) {
1328 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1329 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1331 dc
->tb_flags
|= DRTI_FLAG
;
1333 LOG_DIS("rtbd ir=%x\n", dc
->ir
);
1334 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1335 && mem_index
== MMU_USER_IDX
) {
1336 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1337 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1339 dc
->tb_flags
|= DRTB_FLAG
;
1341 LOG_DIS("rted ir=%x\n", dc
->ir
);
1342 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1343 && mem_index
== MMU_USER_IDX
) {
1344 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_PRIVINSN
);
1345 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1347 dc
->tb_flags
|= DRTE_FLAG
;
1349 LOG_DIS("rts ir=%x\n", dc
->ir
);
1351 dc
->jmp
= JMP_INDIRECT
;
1352 tcg_gen_movi_tl(env_btaken
, 1);
1353 tcg_gen_add_tl(env_btarget
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
1356 static int dec_check_fpuv2(DisasContext
*dc
)
1360 r
= dc
->env
->pvr
.regs
[2] & PVR2_USE_FPU2_MASK
;
1362 if (!r
&& (dc
->tb_flags
& MSR_EE_FLAG
)) {
1363 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_FPU
);
1364 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1369 static void dec_fpu(DisasContext
*dc
)
1371 unsigned int fpu_insn
;
1373 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1374 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
1375 && !((dc
->env
->pvr
.regs
[2] & PVR2_USE_FPU_MASK
))) {
1376 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1377 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1381 fpu_insn
= (dc
->ir
>> 7) & 7;
1385 gen_helper_fadd(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1389 gen_helper_frsub(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1393 gen_helper_fmul(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1397 gen_helper_fdiv(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1401 switch ((dc
->ir
>> 4) & 7) {
1403 gen_helper_fcmp_un(cpu_R
[dc
->rd
],
1404 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1407 gen_helper_fcmp_lt(cpu_R
[dc
->rd
],
1408 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1411 gen_helper_fcmp_eq(cpu_R
[dc
->rd
],
1412 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1415 gen_helper_fcmp_le(cpu_R
[dc
->rd
],
1416 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1419 gen_helper_fcmp_gt(cpu_R
[dc
->rd
],
1420 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1423 gen_helper_fcmp_ne(cpu_R
[dc
->rd
],
1424 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1427 gen_helper_fcmp_ge(cpu_R
[dc
->rd
],
1428 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
1431 qemu_log ("unimplemented fcmp fpu_insn=%x pc=%x opc=%x\n",
1432 fpu_insn
, dc
->pc
, dc
->opcode
);
1433 dc
->abort_at_next_insn
= 1;
1439 if (!dec_check_fpuv2(dc
)) {
1442 gen_helper_flt(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
1446 if (!dec_check_fpuv2(dc
)) {
1449 gen_helper_fint(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
1453 if (!dec_check_fpuv2(dc
)) {
1456 gen_helper_fsqrt(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
1460 qemu_log ("unimplemented FPU insn fpu_insn=%x pc=%x opc=%x\n",
1461 fpu_insn
, dc
->pc
, dc
->opcode
);
1462 dc
->abort_at_next_insn
= 1;
1467 static void dec_null(DisasContext
*dc
)
1469 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1470 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)) {
1471 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1472 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1475 qemu_log ("unknown insn pc=%x opc=%x\n", dc
->pc
, dc
->opcode
);
1476 dc
->abort_at_next_insn
= 1;
1479 static struct decoder_info
{
1484 void (*dec
)(DisasContext
*dc
);
1492 {DEC_BARREL
, dec_barrel
},
1494 {DEC_ST
, dec_store
},
1506 static inline void decode(DisasContext
*dc
)
1511 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
)))
1512 tcg_gen_debug_insn_start(dc
->pc
);
1514 dc
->ir
= ir
= ldl_code(dc
->pc
);
1515 LOG_DIS("%8.8x\t", dc
->ir
);
1520 if ((dc
->tb_flags
& MSR_EE_FLAG
)
1521 && (dc
->env
->pvr
.regs
[2] & PVR2_ILL_OPCODE_EXC_MASK
)
1522 && (dc
->env
->pvr
.regs
[2] & PVR2_OPCODE_0x0_ILL_MASK
)) {
1523 tcg_gen_movi_tl(cpu_SR
[SR_ESR
], ESR_EC_ILLEGAL_OP
);
1524 t_gen_raise_exception(dc
, EXCP_HW_EXCP
);
1528 LOG_DIS("nr_nops=%d\t", dc
->nr_nops
);
1530 if (dc
->nr_nops
> 4)
1531 cpu_abort(dc
->env
, "fetching nop sequence\n");
1533 /* bit 2 seems to indicate insn type. */
1534 dc
->type_b
= ir
& (1 << 29);
1536 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
1537 dc
->rd
= EXTRACT_FIELD(ir
, 21, 25);
1538 dc
->ra
= EXTRACT_FIELD(ir
, 16, 20);
1539 dc
->rb
= EXTRACT_FIELD(ir
, 11, 15);
1540 dc
->imm
= EXTRACT_FIELD(ir
, 0, 15);
1542 /* Large switch for all insns. */
1543 for (i
= 0; i
< ARRAY_SIZE(decinfo
); i
++) {
1544 if ((dc
->opcode
& decinfo
[i
].mask
) == decinfo
[i
].bits
) {
1551 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
1555 if (unlikely(!QTAILQ_EMPTY(&env
->breakpoints
))) {
1556 QTAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1557 if (bp
->pc
== dc
->pc
) {
1558 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1559 dc
->is_jmp
= DISAS_UPDATE
;
1565 /* generate intermediate code for basic block 'tb'. */
1567 gen_intermediate_code_internal(CPUState
*env
, TranslationBlock
*tb
,
1570 uint16_t *gen_opc_end
;
1573 struct DisasContext ctx
;
1574 struct DisasContext
*dc
= &ctx
;
1575 uint32_t next_page_start
, org_flags
;
1580 qemu_log_try_set_file(stderr
);
1585 org_flags
= dc
->synced_flags
= dc
->tb_flags
= tb
->flags
;
1587 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
1589 dc
->is_jmp
= DISAS_NEXT
;
1591 dc
->delayed_branch
= !!(dc
->tb_flags
& D_FLAG
);
1592 if (dc
->delayed_branch
) {
1593 dc
->jmp
= JMP_INDIRECT
;
1596 dc
->singlestep_enabled
= env
->singlestep_enabled
;
1597 dc
->cpustate_changed
= 0;
1598 dc
->abort_at_next_insn
= 0;
1602 cpu_abort(env
, "Microblaze: unaligned PC=%x\n", pc_start
);
1604 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1606 qemu_log("--------------\n");
1607 log_cpu_state(env
, 0);
1611 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1614 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1616 max_insns
= CF_COUNT_MASK
;
1622 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1623 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
1627 check_breakpoint(env
, dc
);
1630 j
= gen_opc_ptr
- gen_opc_buf
;
1634 gen_opc_instr_start
[lj
++] = 0;
1636 gen_opc_pc
[lj
] = dc
->pc
;
1637 gen_opc_instr_start
[lj
] = 1;
1638 gen_opc_icount
[lj
] = num_insns
;
1642 LOG_DIS("%8.8x:\t", dc
->pc
);
1644 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
1650 dc
->tb_flags
&= ~IMM_FLAG
;
1654 if (dc
->delayed_branch
) {
1655 dc
->delayed_branch
--;
1656 if (!dc
->delayed_branch
) {
1657 if (dc
->tb_flags
& DRTI_FLAG
)
1659 if (dc
->tb_flags
& DRTB_FLAG
)
1661 if (dc
->tb_flags
& DRTE_FLAG
)
1663 /* Clear the delay slot flag. */
1664 dc
->tb_flags
&= ~D_FLAG
;
1665 /* If it is a direct jump, try direct chaining. */
1666 if (dc
->jmp
== JMP_INDIRECT
) {
1667 eval_cond_jmp(dc
, env_btarget
, tcg_const_tl(dc
->pc
));
1668 dc
->is_jmp
= DISAS_JUMP
;
1669 } else if (dc
->jmp
== JMP_DIRECT
) {
1671 gen_goto_tb(dc
, 0, dc
->jmp_pc
);
1672 dc
->is_jmp
= DISAS_TB_JUMP
;
1673 } else if (dc
->jmp
== JMP_DIRECT_CC
) {
1677 l1
= gen_new_label();
1678 /* Conditional jmp. */
1679 tcg_gen_brcondi_tl(TCG_COND_NE
, env_btaken
, 0, l1
);
1680 gen_goto_tb(dc
, 1, dc
->pc
);
1682 gen_goto_tb(dc
, 0, dc
->jmp_pc
);
1684 dc
->is_jmp
= DISAS_TB_JUMP
;
1689 if (env
->singlestep_enabled
)
1691 } while (!dc
->is_jmp
&& !dc
->cpustate_changed
1692 && gen_opc_ptr
< gen_opc_end
1694 && (dc
->pc
< next_page_start
)
1695 && num_insns
< max_insns
);
1698 if (dc
->jmp
== JMP_DIRECT
|| dc
->jmp
== JMP_DIRECT_CC
) {
1699 if (dc
->tb_flags
& D_FLAG
) {
1700 dc
->is_jmp
= DISAS_UPDATE
;
1701 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1707 if (tb
->cflags
& CF_LAST_IO
)
1709 /* Force an update if the per-tb cpu state has changed. */
1710 if (dc
->is_jmp
== DISAS_NEXT
1711 && (dc
->cpustate_changed
|| org_flags
!= dc
->tb_flags
)) {
1712 dc
->is_jmp
= DISAS_UPDATE
;
1713 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1717 if (unlikely(env
->singlestep_enabled
)) {
1718 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1719 if (dc
->is_jmp
== DISAS_NEXT
)
1720 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1722 switch(dc
->is_jmp
) {
1724 gen_goto_tb(dc
, 1, npc
);
1729 /* indicate that the hash table must be used
1730 to find the next TB */
1734 /* nothing more to generate */
1738 gen_icount_end(tb
, num_insns
);
1739 *gen_opc_ptr
= INDEX_op_end
;
1741 j
= gen_opc_ptr
- gen_opc_buf
;
1744 gen_opc_instr_start
[lj
++] = 0;
1746 tb
->size
= dc
->pc
- pc_start
;
1747 tb
->icount
= num_insns
;
1752 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1755 log_target_disas(pc_start
, dc
->pc
- pc_start
, 0);
1757 qemu_log("\nisize=%d osize=%td\n",
1758 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
1762 assert(!dc
->abort_at_next_insn
);
1765 void gen_intermediate_code (CPUState
*env
, struct TranslationBlock
*tb
)
1767 gen_intermediate_code_internal(env
, tb
, 0);
1770 void gen_intermediate_code_pc (CPUState
*env
, struct TranslationBlock
*tb
)
1772 gen_intermediate_code_internal(env
, tb
, 1);
1775 void cpu_dump_state (CPUState
*env
, FILE *f
, fprintf_function cpu_fprintf
,
1783 cpu_fprintf(f
, "IN: PC=%x %s\n",
1784 env
->sregs
[SR_PC
], lookup_symbol(env
->sregs
[SR_PC
]));
1785 cpu_fprintf(f
, "rmsr=%x resr=%x rear=%x debug=%x imm=%x iflags=%x fsr=%x\n",
1786 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
], env
->sregs
[SR_EAR
],
1787 env
->debug
, env
->imm
, env
->iflags
, env
->sregs
[SR_FSR
]);
1788 cpu_fprintf(f
, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1789 env
->btaken
, env
->btarget
,
1790 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
1791 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel",
1792 (env
->sregs
[SR_MSR
] & MSR_EIP
),
1793 (env
->sregs
[SR_MSR
] & MSR_IE
));
1795 for (i
= 0; i
< 32; i
++) {
1796 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1797 if ((i
+ 1) % 4 == 0)
1798 cpu_fprintf(f
, "\n");
1800 cpu_fprintf(f
, "\n\n");
1803 CPUState
*cpu_mb_init (const char *cpu_model
)
1806 static int tcg_initialized
= 0;
1809 env
= qemu_mallocz(sizeof(CPUState
));
1813 set_float_rounding_mode(float_round_nearest_even
, &env
->fp_status
);
1815 if (tcg_initialized
)
1818 tcg_initialized
= 1;
1820 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1822 env_debug
= tcg_global_mem_new(TCG_AREG0
,
1823 offsetof(CPUState
, debug
),
1825 env_iflags
= tcg_global_mem_new(TCG_AREG0
,
1826 offsetof(CPUState
, iflags
),
1828 env_imm
= tcg_global_mem_new(TCG_AREG0
,
1829 offsetof(CPUState
, imm
),
1831 env_btarget
= tcg_global_mem_new(TCG_AREG0
,
1832 offsetof(CPUState
, btarget
),
1834 env_btaken
= tcg_global_mem_new(TCG_AREG0
,
1835 offsetof(CPUState
, btaken
),
1837 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1838 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
1839 offsetof(CPUState
, regs
[i
]),
1842 for (i
= 0; i
< ARRAY_SIZE(cpu_SR
); i
++) {
1843 cpu_SR
[i
] = tcg_global_mem_new(TCG_AREG0
,
1844 offsetof(CPUState
, sregs
[i
]),
1845 special_regnames
[i
]);
1847 #define GEN_HELPER 2
1853 void cpu_reset (CPUState
*env
)
1855 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
1856 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
1857 log_cpu_state(env
, 0);
1860 memset(env
, 0, offsetof(CPUMBState
, breakpoints
));
1863 env
->pvr
.regs
[0] = PVR0_PVR_FULL_MASK \
1864 | PVR0_USE_BARREL_MASK \
1865 | PVR0_USE_DIV_MASK \
1866 | PVR0_USE_HW_MUL_MASK \
1867 | PVR0_USE_EXC_MASK \
1868 | PVR0_USE_ICACHE_MASK \
1869 | PVR0_USE_DCACHE_MASK \
1872 env
->pvr
.regs
[2] = PVR2_D_OPB_MASK \
1876 | PVR2_USE_MSR_INSTR \
1877 | PVR2_USE_PCMP_INSTR \
1878 | PVR2_USE_BARREL_MASK \
1879 | PVR2_USE_DIV_MASK \
1880 | PVR2_USE_HW_MUL_MASK \
1881 | PVR2_USE_MUL64_MASK \
1882 | PVR2_USE_FPU_MASK \
1883 | PVR2_USE_FPU2_MASK \
1884 | PVR2_FPU_EXC_MASK \
1886 env
->pvr
.regs
[10] = 0x0c000000; /* Default to spartan 3a dsp family. */
1887 env
->pvr
.regs
[11] = PVR11_USE_MMU
| (16 << 17);
1889 #if defined(CONFIG_USER_ONLY)
1890 /* start in user mode with interrupts enabled. */
1891 env
->sregs
[SR_MSR
] = MSR_EE
| MSR_IE
| MSR_VM
| MSR_UM
;
1892 env
->pvr
.regs
[10] = 0x0c000000; /* Spartan 3a dsp. */
1894 env
->sregs
[SR_MSR
] = 0;
1895 mmu_init(&env
->mmu
);
1897 env
->mmu
.c_mmu_tlb_access
= 3;
1898 env
->mmu
.c_mmu_zones
= 16;
1902 void gen_pc_load(CPUState
*env
, struct TranslationBlock
*tb
,
1903 unsigned long searched_pc
, int pc_pos
, void *puc
)
1905 env
->sregs
[SR_PC
] = gen_opc_pc
[pc_pos
];