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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
33 #include "microblaze-decode.h"
34 #include "qemu-common.h"
42 #if DISAS_MB && !SIM_COMPAT
43 # define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
45 # define LOG_DIS(...) do { } while (0)
50 #define EXTRACT_FIELD(src, start, end) \
51 (((src) >> start) & ((1 << (end - start + 1)) - 1))
53 static TCGv env_debug
;
54 static TCGv_ptr cpu_env
;
55 static TCGv cpu_R
[32];
56 static TCGv cpu_SR
[18];
58 static TCGv env_btaken
;
59 static TCGv env_btarget
;
60 static TCGv env_iflags
;
62 #include "gen-icount.h"
64 /* This is the state at translation time. */
65 typedef struct DisasContext
{
68 target_ulong cache_pc
;
77 unsigned int cpustate_changed
;
78 unsigned int delayed_branch
;
79 unsigned int tb_flags
, synced_flags
; /* tb dependent flags. */
80 unsigned int clear_imm
;
85 #define JMP_INDIRECT 2
89 int abort_at_next_insn
;
91 struct TranslationBlock
*tb
;
92 int singlestep_enabled
;
95 const static char *regnames
[] =
97 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
98 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
99 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
100 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
103 const static char *special_regnames
[] =
105 "rpc", "rmsr", "sr2", "sr3", "sr4", "sr5", "sr6", "sr7",
106 "sr8", "sr9", "sr10", "sr11", "sr12", "sr13", "sr14", "sr15",
107 "sr16", "sr17", "sr18"
110 /* Sign extend at translation time. */
111 static inline int sign_extend(unsigned int val
, unsigned int width
)
123 static inline void t_sync_flags(DisasContext
*dc
)
125 /* Synch the tb dependant flags between translator and runtime. */
126 if (dc
->tb_flags
!= dc
->synced_flags
) {
127 tcg_gen_movi_tl(env_iflags
, dc
->tb_flags
);
128 dc
->synced_flags
= dc
->tb_flags
;
132 static inline void t_gen_raise_exception(DisasContext
*dc
, uint32_t index
)
134 TCGv_i32 tmp
= tcg_const_i32(index
);
137 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
138 gen_helper_raise_exception(tmp
);
139 tcg_temp_free_i32(tmp
);
140 dc
->is_jmp
= DISAS_UPDATE
;
143 static void gen_goto_tb(DisasContext
*dc
, int n
, target_ulong dest
)
145 TranslationBlock
*tb
;
147 if ((tb
->pc
& TARGET_PAGE_MASK
) == (dest
& TARGET_PAGE_MASK
)) {
149 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dest
);
150 tcg_gen_exit_tb((long)tb
+ n
);
152 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dest
);
157 static inline TCGv
*dec_alu_op_b(DisasContext
*dc
)
160 if (dc
->tb_flags
& IMM_FLAG
)
161 tcg_gen_ori_tl(env_imm
, env_imm
, dc
->imm
);
163 tcg_gen_movi_tl(env_imm
, (int32_t)((int16_t)dc
->imm
));
166 return &cpu_R
[dc
->rb
];
169 static void dec_add(DisasContext
*dc
)
176 LOG_DIS("add%s%s%s r%d r%d r%d\n",
177 dc
->type_b
? "i" : "", k
? "k" : "", c
? "c" : "",
178 dc
->rd
, dc
->ra
, dc
->rb
);
180 if (k
&& !c
&& dc
->rd
)
181 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
183 gen_helper_addkc(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
184 tcg_const_tl(k
), tcg_const_tl(c
));
186 TCGv d
= tcg_temp_new();
187 gen_helper_addkc(d
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
188 tcg_const_tl(k
), tcg_const_tl(c
));
193 static void dec_sub(DisasContext
*dc
)
195 unsigned int u
, cmp
, k
, c
;
200 cmp
= (dc
->imm
& 1) && (!dc
->type_b
) && k
;
203 LOG_DIS("cmp%s r%d, r%d ir=%x\n", u
? "u" : "", dc
->rd
, dc
->ra
, dc
->ir
);
206 gen_helper_cmpu(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
208 gen_helper_cmp(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
211 LOG_DIS("sub%s%s r%d, r%d r%d\n",
212 k
? "k" : "", c
? "c" : "", dc
->rd
, dc
->ra
, dc
->rb
);
218 gen_helper_subkc(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
219 tcg_const_tl(k
), tcg_const_tl(c
));
221 gen_helper_subkc(t
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
222 tcg_const_tl(k
), tcg_const_tl(c
));
226 tcg_gen_sub_tl(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
230 static void dec_pattern(DisasContext
*dc
)
235 mode
= dc
->opcode
& 3;
239 LOG_DIS("pcmpbf r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
241 gen_helper_pcmpbf(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
244 LOG_DIS("pcmpeq r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
246 TCGv t0
= tcg_temp_local_new();
247 l1
= gen_new_label();
248 tcg_gen_movi_tl(t0
, 1);
249 tcg_gen_brcond_tl(TCG_COND_EQ
,
250 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
251 tcg_gen_movi_tl(t0
, 0);
253 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
258 LOG_DIS("pcmpne r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
259 l1
= gen_new_label();
261 TCGv t0
= tcg_temp_local_new();
262 tcg_gen_movi_tl(t0
, 1);
263 tcg_gen_brcond_tl(TCG_COND_NE
,
264 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
265 tcg_gen_movi_tl(t0
, 0);
267 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
273 "unsupported pattern insn opcode=%x\n", dc
->opcode
);
278 static void dec_and(DisasContext
*dc
)
282 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
287 not = dc
->opcode
& (1 << 1);
288 LOG_DIS("and%s\n", not ? "n" : "");
294 TCGv t
= tcg_temp_new();
295 tcg_gen_not_tl(t
, *(dec_alu_op_b(dc
)));
296 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t
);
299 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
302 static void dec_or(DisasContext
*dc
)
304 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
309 LOG_DIS("or r%d r%d r%d imm=%x\n", dc
->rd
, dc
->ra
, dc
->rb
, dc
->imm
);
311 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
314 static void dec_xor(DisasContext
*dc
)
316 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
321 LOG_DIS("xor r%d\n", dc
->rd
);
323 tcg_gen_xor_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
326 static void read_carry(DisasContext
*dc
, TCGv d
)
328 tcg_gen_shri_tl(d
, cpu_SR
[SR_MSR
], 31);
331 static void write_carry(DisasContext
*dc
, TCGv v
)
333 TCGv t0
= tcg_temp_new();
334 tcg_gen_shli_tl(t0
, v
, 31);
335 tcg_gen_sari_tl(t0
, t0
, 31);
336 tcg_gen_mov_tl(env_debug
, t0
);
337 tcg_gen_andi_tl(t0
, t0
, (MSR_C
| MSR_CC
));
338 tcg_gen_andi_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
],
340 tcg_gen_or_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], t0
);
345 static inline void msr_read(DisasContext
*dc
, TCGv d
)
347 tcg_gen_mov_tl(d
, cpu_SR
[SR_MSR
]);
350 static inline void msr_write(DisasContext
*dc
, TCGv v
)
352 dc
->cpustate_changed
= 1;
353 tcg_gen_mov_tl(cpu_SR
[SR_MSR
], v
);
354 /* PVR, we have a processor version register. */
355 tcg_gen_ori_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], (1 << 10));
358 static void dec_msr(DisasContext
*dc
)
361 unsigned int sr
, to
, rn
;
363 sr
= dc
->imm
& ((1 << 14) - 1);
364 to
= dc
->imm
& (1 << 14);
367 dc
->cpustate_changed
= 1;
369 /* msrclr and msrset. */
370 if (!(dc
->imm
& (1 << 15))) {
371 unsigned int clr
= dc
->ir
& (1 << 16);
373 LOG_DIS("msr%s r%d imm=%x\n", clr
? "clr" : "set",
376 msr_read(dc
, cpu_R
[dc
->rd
]);
381 tcg_gen_mov_tl(t1
, *(dec_alu_op_b(dc
)));
384 tcg_gen_not_tl(t1
, t1
);
385 tcg_gen_and_tl(t0
, t0
, t1
);
387 tcg_gen_or_tl(t0
, t0
, t1
);
391 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
+ 4);
392 dc
->is_jmp
= DISAS_UPDATE
;
396 #if !defined(CONFIG_USER_ONLY)
397 /* Catch read/writes to the mmu block. */
398 if ((sr
& ~0xff) == 0x1000) {
400 LOG_DIS("m%ss sr%d r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
402 gen_helper_mmu_write(tcg_const_tl(sr
), cpu_R
[dc
->ra
]);
404 gen_helper_mmu_read(cpu_R
[dc
->rd
], tcg_const_tl(sr
));
410 LOG_DIS("m%ss sr%x r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
415 msr_write(dc
, cpu_R
[dc
->ra
]);
418 tcg_gen_mov_tl(cpu_SR
[SR_EAR
], cpu_R
[dc
->ra
]);
421 tcg_gen_mov_tl(cpu_SR
[SR_ESR
], cpu_R
[dc
->ra
]);
424 /* Ignored at the moment. */
427 cpu_abort(dc
->env
, "unknown mts reg %x\n", sr
);
431 LOG_DIS("m%ss r%d sr%x imm=%x\n", to
? "t" : "f", dc
->rd
, sr
, dc
->imm
);
435 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
438 msr_read(dc
, cpu_R
[dc
->rd
]);
441 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_EAR
]);
444 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_ESR
]);
447 tcg_gen_movi_tl(cpu_R
[dc
->rd
], 0);
450 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_BTR
]);
466 tcg_gen_ld_tl(cpu_R
[dc
->rd
],
467 cpu_env
, offsetof(CPUState
, pvr
.regs
[rn
]));
470 cpu_abort(dc
->env
, "unknown mfs reg %x\n", sr
);
476 /* 64-bit signed mul, lower result in d and upper in d2. */
477 static void t_gen_muls(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
481 t0
= tcg_temp_new_i64();
482 t1
= tcg_temp_new_i64();
484 tcg_gen_ext_i32_i64(t0
, a
);
485 tcg_gen_ext_i32_i64(t1
, b
);
486 tcg_gen_mul_i64(t0
, t0
, t1
);
488 tcg_gen_trunc_i64_i32(d
, t0
);
489 tcg_gen_shri_i64(t0
, t0
, 32);
490 tcg_gen_trunc_i64_i32(d2
, t0
);
492 tcg_temp_free_i64(t0
);
493 tcg_temp_free_i64(t1
);
496 /* 64-bit unsigned muls, lower result in d and upper in d2. */
497 static void t_gen_mulu(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
501 t0
= tcg_temp_new_i64();
502 t1
= tcg_temp_new_i64();
504 tcg_gen_extu_i32_i64(t0
, a
);
505 tcg_gen_extu_i32_i64(t1
, b
);
506 tcg_gen_mul_i64(t0
, t0
, t1
);
508 tcg_gen_trunc_i64_i32(d
, t0
);
509 tcg_gen_shri_i64(t0
, t0
, 32);
510 tcg_gen_trunc_i64_i32(d2
, t0
);
512 tcg_temp_free_i64(t0
);
513 tcg_temp_free_i64(t1
);
516 /* Multiplier unit. */
517 static void dec_mul(DisasContext
*dc
)
520 unsigned int subcode
;
522 subcode
= dc
->imm
& 3;
523 d
[0] = tcg_temp_new();
524 d
[1] = tcg_temp_new();
527 LOG_DIS("muli r%d r%d %x\n", dc
->rd
, dc
->ra
, dc
->imm
);
528 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
534 LOG_DIS("mul r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
535 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
538 LOG_DIS("mulh r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
539 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
542 LOG_DIS("mulhsu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
543 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
546 LOG_DIS("mulhu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
547 t_gen_mulu(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
550 cpu_abort(dc
->env
, "unknown MUL insn %x\n", subcode
);
559 static void dec_div(DisasContext
*dc
)
566 /* FIXME: support div by zero exceptions. */
568 gen_helper_divu(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
570 gen_helper_divs(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
572 tcg_gen_movi_tl(cpu_R
[dc
->rd
], 0);
575 static void dec_barrel(DisasContext
*dc
)
580 s
= dc
->imm
& (1 << 10);
581 t
= dc
->imm
& (1 << 9);
583 LOG_DIS("bs%s%s r%d r%d r%d\n",
584 s
? "l" : "r", t
? "a" : "l", dc
->rd
, dc
->ra
, dc
->rb
);
588 tcg_gen_mov_tl(t0
, *(dec_alu_op_b(dc
)));
589 tcg_gen_andi_tl(t0
, t0
, 31);
592 tcg_gen_shl_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
595 tcg_gen_sar_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
597 tcg_gen_shr_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
601 static void dec_bit(DisasContext
*dc
)
606 op
= dc
->ir
& ((1 << 8) - 1);
612 LOG_DIS("src r%d r%d\n", dc
->rd
, dc
->ra
);
613 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
617 tcg_gen_shli_tl(t1
, t1
, 31);
619 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
620 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], t1
);
633 LOG_DIS("srl r%d r%d\n", dc
->rd
, dc
->ra
);
636 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
641 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
643 tcg_gen_sari_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
647 LOG_DIS("ext8s r%d r%d\n", dc
->rd
, dc
->ra
);
648 tcg_gen_ext8s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
651 LOG_DIS("ext16s r%d r%d\n", dc
->rd
, dc
->ra
);
652 tcg_gen_ext16s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
656 LOG_DIS("wdc r%d\n", dc
->ra
);
660 LOG_DIS("wic r%d\n", dc
->ra
);
663 cpu_abort(dc
->env
, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
664 dc
->pc
, op
, dc
->rd
, dc
->ra
, dc
->rb
);
669 static inline void sync_jmpstate(DisasContext
*dc
)
671 if (dc
->jmp
== JMP_DIRECT
) {
672 dc
->jmp
= JMP_INDIRECT
;
673 tcg_gen_movi_tl(env_btaken
, 1);
674 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
678 static void dec_imm(DisasContext
*dc
)
680 LOG_DIS("imm %x\n", dc
->imm
<< 16);
681 tcg_gen_movi_tl(env_imm
, (dc
->imm
<< 16));
682 dc
->tb_flags
|= IMM_FLAG
;
686 static inline void gen_load(DisasContext
*dc
, TCGv dst
, TCGv addr
,
689 int mem_index
= cpu_mmu_index(dc
->env
);
692 tcg_gen_qemu_ld8u(dst
, addr
, mem_index
);
693 } else if (size
== 2) {
694 tcg_gen_qemu_ld16u(dst
, addr
, mem_index
);
695 } else if (size
== 4) {
696 tcg_gen_qemu_ld32u(dst
, addr
, mem_index
);
698 cpu_abort(dc
->env
, "Incorrect load size %d\n", size
);
701 static inline TCGv
*compute_ldst_addr(DisasContext
*dc
, TCGv
*t
)
703 unsigned int extimm
= dc
->tb_flags
& IMM_FLAG
;
705 /* Treat the fast cases first. */
708 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
714 return &cpu_R
[dc
->ra
];
717 tcg_gen_movi_tl(*t
, (int32_t)((int16_t)dc
->imm
));
718 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *t
);
721 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
727 static void dec_load(DisasContext
*dc
)
732 size
= 1 << (dc
->opcode
& 3);
734 LOG_DIS("l %x %d\n", dc
->opcode
, size
);
736 addr
= compute_ldst_addr(dc
, &t
);
738 /* If we get a fault on a dslot, the jmpstate better be in sync. */
741 gen_load(dc
, cpu_R
[dc
->rd
], *addr
, size
);
743 gen_load(dc
, env_imm
, *addr
, size
);
750 static void gen_store(DisasContext
*dc
, TCGv addr
, TCGv val
,
753 int mem_index
= cpu_mmu_index(dc
->env
);
756 tcg_gen_qemu_st8(val
, addr
, mem_index
);
757 else if (size
== 2) {
758 tcg_gen_qemu_st16(val
, addr
, mem_index
);
759 } else if (size
== 4) {
760 tcg_gen_qemu_st32(val
, addr
, mem_index
);
762 cpu_abort(dc
->env
, "Incorrect store size %d\n", size
);
765 static void dec_store(DisasContext
*dc
)
770 size
= 1 << (dc
->opcode
& 3);
772 LOG_DIS("s%d%s\n", size
, dc
->type_b
? "i" : "");
774 /* If we get a fault on a dslot, the jmpstate better be in sync. */
776 addr
= compute_ldst_addr(dc
, &t
);
777 gen_store(dc
, *addr
, cpu_R
[dc
->rd
], size
);
782 static inline void eval_cc(DisasContext
*dc
, unsigned int cc
,
783 TCGv d
, TCGv a
, TCGv b
)
789 l1
= gen_new_label();
790 tcg_gen_movi_tl(env_btaken
, 1);
791 tcg_gen_brcond_tl(TCG_COND_EQ
, a
, b
, l1
);
792 tcg_gen_movi_tl(env_btaken
, 0);
796 l1
= gen_new_label();
797 tcg_gen_movi_tl(env_btaken
, 1);
798 tcg_gen_brcond_tl(TCG_COND_NE
, a
, b
, l1
);
799 tcg_gen_movi_tl(env_btaken
, 0);
803 l1
= gen_new_label();
804 tcg_gen_movi_tl(env_btaken
, 1);
805 tcg_gen_brcond_tl(TCG_COND_LT
, a
, b
, l1
);
806 tcg_gen_movi_tl(env_btaken
, 0);
810 l1
= gen_new_label();
811 tcg_gen_movi_tl(env_btaken
, 1);
812 tcg_gen_brcond_tl(TCG_COND_LE
, a
, b
, l1
);
813 tcg_gen_movi_tl(env_btaken
, 0);
817 l1
= gen_new_label();
818 tcg_gen_movi_tl(env_btaken
, 1);
819 tcg_gen_brcond_tl(TCG_COND_GE
, a
, b
, l1
);
820 tcg_gen_movi_tl(env_btaken
, 0);
824 l1
= gen_new_label();
825 tcg_gen_movi_tl(env_btaken
, 1);
826 tcg_gen_brcond_tl(TCG_COND_GT
, a
, b
, l1
);
827 tcg_gen_movi_tl(env_btaken
, 0);
831 cpu_abort(dc
->env
, "Unknown condition code %x.\n", cc
);
836 static void eval_cond_jmp(DisasContext
*dc
, TCGv pc_true
, TCGv pc_false
)
840 l1
= gen_new_label();
841 /* Conditional jmp. */
842 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_false
);
843 tcg_gen_brcondi_tl(TCG_COND_EQ
, env_btaken
, 0, l1
);
844 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_true
);
848 static void dec_bcc(DisasContext
*dc
)
853 cc
= EXTRACT_FIELD(dc
->ir
, 21, 23);
854 dslot
= dc
->ir
& (1 << 25);
855 LOG_DIS("bcc%s r%d %x\n", dslot
? "d" : "", dc
->ra
, dc
->imm
);
857 dc
->delayed_branch
= 1;
859 dc
->delayed_branch
= 2;
860 dc
->tb_flags
|= D_FLAG
;
861 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
862 cpu_env
, offsetof(CPUState
, bimm
));
865 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
866 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
867 eval_cc(dc
, cc
, env_btaken
, cpu_R
[dc
->ra
], tcg_const_tl(0));
868 dc
->jmp
= JMP_INDIRECT
;
871 static void dec_br(DisasContext
*dc
)
873 unsigned int dslot
, link
, abs
;
875 dslot
= dc
->ir
& (1 << 20);
876 abs
= dc
->ir
& (1 << 19);
877 link
= dc
->ir
& (1 << 18);
878 LOG_DIS("br%s%s%s%s imm=%x\n",
879 abs
? "a" : "", link
? "l" : "",
880 dc
->type_b
? "i" : "", dslot
? "d" : "",
883 dc
->delayed_branch
= 1;
885 dc
->delayed_branch
= 2;
886 dc
->tb_flags
|= D_FLAG
;
887 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
888 cpu_env
, offsetof(CPUState
, bimm
));
891 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
893 dc
->jmp
= JMP_INDIRECT
;
895 tcg_gen_movi_tl(env_btaken
, 1);
896 tcg_gen_mov_tl(env_btarget
, *(dec_alu_op_b(dc
)));
897 if (link
&& !(dc
->tb_flags
& IMM_FLAG
)
898 && (dc
->imm
== 8 || dc
->imm
== 0x18))
899 t_gen_raise_exception(dc
, EXCP_BREAK
);
901 t_gen_raise_exception(dc
, EXCP_DEBUG
);
903 if (dc
->tb_flags
& IMM_FLAG
) {
904 tcg_gen_movi_tl(env_btaken
, 1);
905 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
906 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
908 dc
->jmp
= JMP_DIRECT
;
909 dc
->jmp_pc
= dc
->pc
+ (int32_t)((int16_t)dc
->imm
);
914 static inline void do_rti(DisasContext
*dc
)
919 tcg_gen_shri_tl(t0
, cpu_SR
[SR_MSR
], 1);
920 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_IE
);
921 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
923 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
924 tcg_gen_or_tl(t1
, t1
, t0
);
928 dc
->tb_flags
&= ~DRTI_FLAG
;
931 static inline void do_rtb(DisasContext
*dc
)
936 tcg_gen_andi_tl(t1
, cpu_SR
[SR_MSR
], ~MSR_BIP
);
937 tcg_gen_shri_tl(t0
, t1
, 1);
938 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
940 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
941 tcg_gen_or_tl(t1
, t1
, t0
);
945 dc
->tb_flags
&= ~DRTB_FLAG
;
948 static inline void do_rte(DisasContext
*dc
)
954 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_EE
);
955 tcg_gen_andi_tl(t1
, t1
, ~MSR_EIP
);
956 tcg_gen_shri_tl(t0
, t1
, 1);
957 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
959 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
960 tcg_gen_or_tl(t1
, t1
, t0
);
964 dc
->tb_flags
&= ~DRTE_FLAG
;
967 static void dec_rts(DisasContext
*dc
)
969 unsigned int b_bit
, i_bit
, e_bit
;
971 i_bit
= dc
->ir
& (1 << 21);
972 b_bit
= dc
->ir
& (1 << 22);
973 e_bit
= dc
->ir
& (1 << 23);
975 dc
->delayed_branch
= 2;
976 dc
->tb_flags
|= D_FLAG
;
977 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
978 cpu_env
, offsetof(CPUState
, bimm
));
981 LOG_DIS("rtid ir=%x\n", dc
->ir
);
982 dc
->tb_flags
|= DRTI_FLAG
;
984 LOG_DIS("rtbd ir=%x\n", dc
->ir
);
985 dc
->tb_flags
|= DRTB_FLAG
;
987 LOG_DIS("rted ir=%x\n", dc
->ir
);
988 dc
->tb_flags
|= DRTE_FLAG
;
990 LOG_DIS("rts ir=%x\n", dc
->ir
);
992 tcg_gen_movi_tl(env_btaken
, 1);
993 tcg_gen_add_tl(env_btarget
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
996 static void dec_null(DisasContext
*dc
)
998 qemu_log ("unknown insn pc=%x opc=%x\n", dc
->pc
, dc
->opcode
);
999 dc
->abort_at_next_insn
= 1;
1002 static struct decoder_info
{
1007 void (*dec
)(DisasContext
*dc
);
1015 {DEC_BARREL
, dec_barrel
},
1017 {DEC_ST
, dec_store
},
1028 static inline void decode(DisasContext
*dc
)
1033 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
)))
1034 tcg_gen_debug_insn_start(dc
->pc
);
1036 dc
->ir
= ir
= ldl_code(dc
->pc
);
1037 LOG_DIS("%8.8x\t", dc
->ir
);
1042 LOG_DIS("nr_nops=%d\t", dc
->nr_nops
);
1044 if (dc
->nr_nops
> 4)
1045 cpu_abort(dc
->env
, "fetching nop sequence\n");
1047 /* bit 2 seems to indicate insn type. */
1048 dc
->type_b
= ir
& (1 << 29);
1050 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
1051 dc
->rd
= EXTRACT_FIELD(ir
, 21, 25);
1052 dc
->ra
= EXTRACT_FIELD(ir
, 16, 20);
1053 dc
->rb
= EXTRACT_FIELD(ir
, 11, 15);
1054 dc
->imm
= EXTRACT_FIELD(ir
, 0, 15);
1056 /* Large switch for all insns. */
1057 for (i
= 0; i
< ARRAY_SIZE(decinfo
); i
++) {
1058 if ((dc
->opcode
& decinfo
[i
].mask
) == decinfo
[i
].bits
) {
1066 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
1070 if (unlikely(!TAILQ_EMPTY(&env
->breakpoints
))) {
1071 TAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1072 if (bp
->pc
== dc
->pc
) {
1073 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1074 dc
->is_jmp
= DISAS_UPDATE
;
1080 /* generate intermediate code for basic block 'tb'. */
1082 gen_intermediate_code_internal(CPUState
*env
, TranslationBlock
*tb
,
1085 uint16_t *gen_opc_end
;
1088 struct DisasContext ctx
;
1089 struct DisasContext
*dc
= &ctx
;
1090 uint32_t next_page_start
, org_flags
;
1095 qemu_log_try_set_file(stderr
);
1100 org_flags
= dc
->synced_flags
= dc
->tb_flags
= tb
->flags
;
1102 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
1104 dc
->is_jmp
= DISAS_NEXT
;
1106 dc
->delayed_branch
= !!(dc
->tb_flags
& D_FLAG
);
1110 dc
->singlestep_enabled
= env
->singlestep_enabled
;
1111 dc
->cpustate_changed
= 0;
1112 dc
->abort_at_next_insn
= 0;
1116 cpu_abort(env
, "Microblaze: unaligned PC=%x\n", pc_start
);
1118 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1120 qemu_log("--------------\n");
1121 log_cpu_state(env
, 0);
1125 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1128 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1130 max_insns
= CF_COUNT_MASK
;
1136 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1137 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
1141 check_breakpoint(env
, dc
);
1144 j
= gen_opc_ptr
- gen_opc_buf
;
1148 gen_opc_instr_start
[lj
++] = 0;
1150 gen_opc_pc
[lj
] = dc
->pc
;
1151 gen_opc_instr_start
[lj
] = 1;
1152 gen_opc_icount
[lj
] = num_insns
;
1156 LOG_DIS("%8.8x:\t", dc
->pc
);
1158 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
1164 dc
->tb_flags
&= ~IMM_FLAG
;
1169 if (dc
->delayed_branch
) {
1170 dc
->delayed_branch
--;
1171 if (!dc
->delayed_branch
) {
1172 if (dc
->tb_flags
& DRTI_FLAG
)
1174 if (dc
->tb_flags
& DRTB_FLAG
)
1176 if (dc
->tb_flags
& DRTE_FLAG
)
1178 /* Clear the delay slot flag. */
1179 dc
->tb_flags
&= ~D_FLAG
;
1180 /* If it is a direct jump, try direct chaining. */
1181 if (dc
->jmp
!= JMP_DIRECT
) {
1182 eval_cond_jmp(dc
, env_btarget
, tcg_const_tl(dc
->pc
));
1183 dc
->is_jmp
= DISAS_JUMP
;
1188 if (env
->singlestep_enabled
)
1190 } while (!dc
->is_jmp
&& !dc
->cpustate_changed
1191 && gen_opc_ptr
< gen_opc_end
1193 && (dc
->pc
< next_page_start
)
1194 && num_insns
< max_insns
);
1197 if (dc
->jmp
== JMP_DIRECT
) {
1198 if (dc
->tb_flags
& D_FLAG
) {
1199 dc
->is_jmp
= DISAS_UPDATE
;
1200 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1206 if (tb
->cflags
& CF_LAST_IO
)
1208 /* Force an update if the per-tb cpu state has changed. */
1209 if (dc
->is_jmp
== DISAS_NEXT
1210 && (dc
->cpustate_changed
|| org_flags
!= dc
->tb_flags
)) {
1211 dc
->is_jmp
= DISAS_UPDATE
;
1212 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1216 if (unlikely(env
->singlestep_enabled
)) {
1217 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1218 if (dc
->is_jmp
== DISAS_NEXT
)
1219 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1221 switch(dc
->is_jmp
) {
1223 gen_goto_tb(dc
, 1, npc
);
1228 /* indicate that the hash table must be used
1229 to find the next TB */
1233 /* nothing more to generate */
1237 gen_icount_end(tb
, num_insns
);
1238 *gen_opc_ptr
= INDEX_op_end
;
1240 j
= gen_opc_ptr
- gen_opc_buf
;
1243 gen_opc_instr_start
[lj
++] = 0;
1245 tb
->size
= dc
->pc
- pc_start
;
1246 tb
->icount
= num_insns
;
1251 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1254 log_target_disas(pc_start
, dc
->pc
- pc_start
, 0);
1256 qemu_log("\nisize=%d osize=%zd\n",
1257 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
1261 assert(!dc
->abort_at_next_insn
);
1264 void gen_intermediate_code (CPUState
*env
, struct TranslationBlock
*tb
)
1266 gen_intermediate_code_internal(env
, tb
, 0);
1269 void gen_intermediate_code_pc (CPUState
*env
, struct TranslationBlock
*tb
)
1271 gen_intermediate_code_internal(env
, tb
, 1);
1274 void cpu_dump_state (CPUState
*env
, FILE *f
,
1275 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
1283 cpu_fprintf(f
, "IN: PC=%x %s\n",
1284 env
->sregs
[SR_PC
], lookup_symbol(env
->sregs
[SR_PC
]));
1285 cpu_fprintf(f
, "rmsr=%x resr=%x debug[%x] imm=%x iflags=%x\n",
1286 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
],
1287 env
->debug
, env
->imm
, env
->iflags
);
1288 cpu_fprintf(f
, "btaken=%d btarget=%x mode=%s(saved=%s)\n",
1289 env
->btaken
, env
->btarget
,
1290 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
1291 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel");
1292 for (i
= 0; i
< 32; i
++) {
1293 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1294 if ((i
+ 1) % 4 == 0)
1295 cpu_fprintf(f
, "\n");
1297 cpu_fprintf(f
, "\n\n");
1300 CPUState
*cpu_mb_init (const char *cpu_model
)
1303 static int tcg_initialized
= 0;
1306 env
= qemu_mallocz(sizeof(CPUState
));
1311 env
->pvr
.regs
[0] = PVR0_PVR_FULL_MASK \
1312 | PVR0_USE_BARREL_MASK \
1313 | PVR0_USE_DIV_MASK \
1314 | PVR0_USE_HW_MUL_MASK \
1315 | PVR0_USE_EXC_MASK \
1316 | PVR0_USE_ICACHE_MASK \
1317 | PVR0_USE_DCACHE_MASK \
1320 env
->pvr
.regs
[2] = PVR2_D_OPB_MASK \
1324 | PVR2_USE_MSR_INSTR \
1325 | PVR2_USE_PCMP_INSTR \
1326 | PVR2_USE_BARREL_MASK \
1327 | PVR2_USE_DIV_MASK \
1328 | PVR2_USE_HW_MUL_MASK \
1329 | PVR2_USE_MUL64_MASK \
1331 env
->pvr
.regs
[10] = 0x0c000000; /* Default to spartan 3a dsp family. */
1332 env
->pvr
.regs
[11] = PVR11_USE_MMU
;
1334 if (tcg_initialized
)
1337 tcg_initialized
= 1;
1339 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1341 env_debug
= tcg_global_mem_new(TCG_AREG0
,
1342 offsetof(CPUState
, debug
),
1344 env_iflags
= tcg_global_mem_new(TCG_AREG0
,
1345 offsetof(CPUState
, iflags
),
1347 env_imm
= tcg_global_mem_new(TCG_AREG0
,
1348 offsetof(CPUState
, imm
),
1350 env_btarget
= tcg_global_mem_new(TCG_AREG0
,
1351 offsetof(CPUState
, btarget
),
1353 env_btaken
= tcg_global_mem_new(TCG_AREG0
,
1354 offsetof(CPUState
, btaken
),
1356 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1357 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
1358 offsetof(CPUState
, regs
[i
]),
1361 for (i
= 0; i
< ARRAY_SIZE(cpu_SR
); i
++) {
1362 cpu_SR
[i
] = tcg_global_mem_new(TCG_AREG0
,
1363 offsetof(CPUState
, sregs
[i
]),
1364 special_regnames
[i
]);
1366 #define GEN_HELPER 2
1372 void cpu_reset (CPUState
*env
)
1374 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
1375 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
1376 log_cpu_state(env
, 0);
1379 memset(env
, 0, offsetof(CPUMBState
, breakpoints
));
1382 env
->sregs
[SR_MSR
] = 0;
1383 #if defined(CONFIG_USER_ONLY)
1384 /* start in user mode with interrupts enabled. */
1385 env
->pvr
.regs
[10] = 0x0c000000; /* Spartan 3a dsp. */
1387 mmu_init(&env
->mmu
);
1391 void gen_pc_load(CPUState
*env
, struct TranslationBlock
*tb
,
1392 unsigned long searched_pc
, int pc_pos
, void *puc
)
1394 env
->sregs
[SR_PC
] = gen_opc_pc
[pc_pos
];