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
{
67 target_ulong cache_pc
;
76 unsigned int cpustate_changed
;
77 unsigned int delayed_branch
;
78 unsigned int tb_flags
, synced_flags
; /* tb dependent flags. */
79 unsigned int clear_imm
;
84 #define JMP_INDIRECT 2
88 int abort_at_next_insn
;
90 struct TranslationBlock
*tb
;
91 int singlestep_enabled
;
94 const static 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 const static 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 inline TCGv
*dec_alu_op_b(DisasContext
*dc
)
159 if (dc
->tb_flags
& IMM_FLAG
)
160 tcg_gen_ori_tl(env_imm
, env_imm
, dc
->imm
);
162 tcg_gen_movi_tl(env_imm
, (int32_t)((int16_t)dc
->imm
));
165 return &cpu_R
[dc
->rb
];
168 static void dec_add(DisasContext
*dc
)
175 LOG_DIS("add%s%s%s r%d r%d r%d\n",
176 dc
->type_b
? "i" : "", k
? "k" : "", c
? "c" : "",
177 dc
->rd
, dc
->ra
, dc
->rb
);
179 if (k
&& !c
&& dc
->rd
)
180 tcg_gen_add_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
182 gen_helper_addkc(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
183 tcg_const_tl(k
), tcg_const_tl(c
));
185 TCGv d
= tcg_temp_new();
186 gen_helper_addkc(d
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
187 tcg_const_tl(k
), tcg_const_tl(c
));
192 static void dec_sub(DisasContext
*dc
)
194 unsigned int u
, cmp
, k
, c
;
199 cmp
= (dc
->imm
& 1) && (!dc
->type_b
) && k
;
202 LOG_DIS("cmp%s r%d, r%d ir=%x\n", u
? "u" : "", dc
->rd
, dc
->ra
, dc
->ir
);
205 gen_helper_cmpu(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
207 gen_helper_cmp(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
210 LOG_DIS("sub%s%s r%d, r%d r%d\n",
211 k
? "k" : "", c
? "c" : "", dc
->rd
, dc
->ra
, dc
->rb
);
217 gen_helper_subkc(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
218 tcg_const_tl(k
), tcg_const_tl(c
));
220 gen_helper_subkc(t
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)),
221 tcg_const_tl(k
), tcg_const_tl(c
));
225 tcg_gen_sub_tl(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
229 static void dec_pattern(DisasContext
*dc
)
234 mode
= dc
->opcode
& 3;
238 LOG_DIS("pcmpbf r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
240 gen_helper_pcmpbf(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
243 LOG_DIS("pcmpeq r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
245 TCGv t0
= tcg_temp_local_new();
246 l1
= gen_new_label();
247 tcg_gen_movi_tl(t0
, 1);
248 tcg_gen_brcond_tl(TCG_COND_EQ
,
249 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
250 tcg_gen_movi_tl(t0
, 0);
252 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
257 LOG_DIS("pcmpne r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
258 l1
= gen_new_label();
260 TCGv t0
= tcg_temp_local_new();
261 tcg_gen_movi_tl(t0
, 1);
262 tcg_gen_brcond_tl(TCG_COND_NE
,
263 cpu_R
[dc
->ra
], cpu_R
[dc
->rb
], l1
);
264 tcg_gen_movi_tl(t0
, 0);
266 tcg_gen_mov_tl(cpu_R
[dc
->rd
], t0
);
272 "unsupported pattern insn opcode=%x\n", dc
->opcode
);
277 static void dec_and(DisasContext
*dc
)
281 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
286 not = dc
->opcode
& (1 << 1);
287 LOG_DIS("and%s\n", not ? "n" : "");
293 TCGv t
= tcg_temp_new();
294 tcg_gen_not_tl(t
, *(dec_alu_op_b(dc
)));
295 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t
);
298 tcg_gen_and_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
301 static void dec_or(DisasContext
*dc
)
303 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
308 LOG_DIS("or r%d r%d r%d imm=%x\n", dc
->rd
, dc
->ra
, dc
->rb
, dc
->imm
);
310 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
313 static void dec_xor(DisasContext
*dc
)
315 if (!dc
->type_b
&& (dc
->imm
& (1 << 10))) {
320 LOG_DIS("xor r%d\n", dc
->rd
);
322 tcg_gen_xor_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
325 static void read_carry(DisasContext
*dc
, TCGv d
)
327 tcg_gen_shri_tl(d
, cpu_SR
[SR_MSR
], 31);
330 static void write_carry(DisasContext
*dc
, TCGv v
)
332 TCGv t0
= tcg_temp_new();
333 tcg_gen_shli_tl(t0
, v
, 31);
334 tcg_gen_sari_tl(t0
, t0
, 31);
335 tcg_gen_mov_tl(env_debug
, t0
);
336 tcg_gen_andi_tl(t0
, t0
, (MSR_C
| MSR_CC
));
337 tcg_gen_andi_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
],
339 tcg_gen_or_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], t0
);
344 static inline void msr_read(DisasContext
*dc
, TCGv d
)
346 tcg_gen_mov_tl(d
, cpu_SR
[SR_MSR
]);
349 static inline void msr_write(DisasContext
*dc
, TCGv v
)
351 dc
->cpustate_changed
= 1;
352 tcg_gen_mov_tl(cpu_SR
[SR_MSR
], v
);
353 /* PVR, we have a processor version register. */
354 tcg_gen_ori_tl(cpu_SR
[SR_MSR
], cpu_SR
[SR_MSR
], (1 << 10));
357 static void dec_msr(DisasContext
*dc
)
360 unsigned int sr
, to
, rn
;
362 sr
= dc
->imm
& ((1 << 14) - 1);
363 to
= dc
->imm
& (1 << 14);
366 dc
->cpustate_changed
= 1;
368 /* msrclr and msrset. */
369 if (!(dc
->imm
& (1 << 15))) {
370 unsigned int clr
= dc
->ir
& (1 << 16);
372 LOG_DIS("msr%s r%d imm=%x\n", clr
? "clr" : "set",
375 msr_read(dc
, cpu_R
[dc
->rd
]);
380 tcg_gen_mov_tl(t1
, *(dec_alu_op_b(dc
)));
383 tcg_gen_not_tl(t1
, t1
);
384 tcg_gen_and_tl(t0
, t0
, t1
);
386 tcg_gen_or_tl(t0
, t0
, t1
);
390 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
+ 4);
391 dc
->is_jmp
= DISAS_UPDATE
;
395 #if !defined(CONFIG_USER_ONLY)
396 /* Catch read/writes to the mmu block. */
397 if ((sr
& ~0xff) == 0x1000) {
399 LOG_DIS("m%ss sr%d r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
401 gen_helper_mmu_write(tcg_const_tl(sr
), cpu_R
[dc
->ra
]);
403 gen_helper_mmu_read(cpu_R
[dc
->rd
], tcg_const_tl(sr
));
409 LOG_DIS("m%ss sr%x r%d imm=%x\n", to
? "t" : "f", sr
, dc
->ra
, dc
->imm
);
414 msr_write(dc
, cpu_R
[dc
->ra
]);
417 tcg_gen_mov_tl(cpu_SR
[SR_EAR
], cpu_R
[dc
->ra
]);
420 tcg_gen_mov_tl(cpu_SR
[SR_ESR
], cpu_R
[dc
->ra
]);
423 /* Ignored at the moment. */
426 cpu_abort(dc
->env
, "unknown mts reg %x\n", sr
);
430 LOG_DIS("m%ss r%d sr%x imm=%x\n", to
? "t" : "f", dc
->rd
, sr
, dc
->imm
);
434 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
437 msr_read(dc
, cpu_R
[dc
->rd
]);
440 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_EAR
]);
443 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_ESR
]);
446 tcg_gen_movi_tl(cpu_R
[dc
->rd
], 0);
449 tcg_gen_mov_tl(cpu_R
[dc
->rd
], cpu_SR
[SR_BTR
]);
465 tcg_gen_ld_tl(cpu_R
[dc
->rd
],
466 cpu_env
, offsetof(CPUState
, pvr
.regs
[rn
]));
469 cpu_abort(dc
->env
, "unknown mfs reg %x\n", sr
);
475 /* 64-bit signed mul, lower result in d and upper in d2. */
476 static void t_gen_muls(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
480 t0
= tcg_temp_new_i64();
481 t1
= tcg_temp_new_i64();
483 tcg_gen_ext_i32_i64(t0
, a
);
484 tcg_gen_ext_i32_i64(t1
, b
);
485 tcg_gen_mul_i64(t0
, t0
, t1
);
487 tcg_gen_trunc_i64_i32(d
, t0
);
488 tcg_gen_shri_i64(t0
, t0
, 32);
489 tcg_gen_trunc_i64_i32(d2
, t0
);
491 tcg_temp_free_i64(t0
);
492 tcg_temp_free_i64(t1
);
495 /* 64-bit unsigned muls, lower result in d and upper in d2. */
496 static void t_gen_mulu(TCGv d
, TCGv d2
, TCGv a
, TCGv b
)
500 t0
= tcg_temp_new_i64();
501 t1
= tcg_temp_new_i64();
503 tcg_gen_extu_i32_i64(t0
, a
);
504 tcg_gen_extu_i32_i64(t1
, b
);
505 tcg_gen_mul_i64(t0
, t0
, t1
);
507 tcg_gen_trunc_i64_i32(d
, t0
);
508 tcg_gen_shri_i64(t0
, t0
, 32);
509 tcg_gen_trunc_i64_i32(d2
, t0
);
511 tcg_temp_free_i64(t0
);
512 tcg_temp_free_i64(t1
);
515 /* Multiplier unit. */
516 static void dec_mul(DisasContext
*dc
)
519 unsigned int subcode
;
521 subcode
= dc
->imm
& 3;
522 d
[0] = tcg_temp_new();
523 d
[1] = tcg_temp_new();
526 LOG_DIS("muli r%d r%d %x\n", dc
->rd
, dc
->ra
, dc
->imm
);
527 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
533 LOG_DIS("mul r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
534 t_gen_mulu(cpu_R
[dc
->rd
], d
[1], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
537 LOG_DIS("mulh r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
538 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
541 LOG_DIS("mulhsu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
542 t_gen_muls(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
545 LOG_DIS("mulhu r%d r%d r%d\n", dc
->rd
, dc
->ra
, dc
->rb
);
546 t_gen_mulu(d
[0], cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
549 cpu_abort(dc
->env
, "unknown MUL insn %x\n", subcode
);
558 static void dec_div(DisasContext
*dc
)
565 /* FIXME: support div by zero exceptions. */
567 gen_helper_divu(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
569 gen_helper_divs(cpu_R
[dc
->rd
], *(dec_alu_op_b(dc
)), cpu_R
[dc
->ra
]);
571 tcg_gen_movi_tl(cpu_R
[dc
->rd
], 0);
574 static void dec_barrel(DisasContext
*dc
)
579 s
= dc
->imm
& (1 << 10);
580 t
= dc
->imm
& (1 << 9);
582 LOG_DIS("bs%s%s r%d r%d r%d\n",
583 s
? "l" : "r", t
? "a" : "l", dc
->rd
, dc
->ra
, dc
->rb
);
587 tcg_gen_mov_tl(t0
, *(dec_alu_op_b(dc
)));
588 tcg_gen_andi_tl(t0
, t0
, 31);
591 tcg_gen_shl_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
594 tcg_gen_sar_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
596 tcg_gen_shr_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], t0
);
600 static void dec_bit(DisasContext
*dc
)
605 op
= dc
->ir
& ((1 << 8) - 1);
611 LOG_DIS("src r%d r%d\n", dc
->rd
, dc
->ra
);
612 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
616 tcg_gen_shli_tl(t1
, t1
, 31);
618 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
619 tcg_gen_or_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->rd
], t1
);
632 LOG_DIS("srl r%d r%d\n", dc
->rd
, dc
->ra
);
635 tcg_gen_andi_tl(t0
, cpu_R
[dc
->ra
], 1);
640 tcg_gen_shri_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
642 tcg_gen_sari_tl(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
], 1);
646 LOG_DIS("ext8s r%d r%d\n", dc
->rd
, dc
->ra
);
647 tcg_gen_ext8s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
650 LOG_DIS("ext16s r%d r%d\n", dc
->rd
, dc
->ra
);
651 tcg_gen_ext16s_i32(cpu_R
[dc
->rd
], cpu_R
[dc
->ra
]);
655 LOG_DIS("wdc r%d\n", dc
->ra
);
659 LOG_DIS("wic r%d\n", dc
->ra
);
662 cpu_abort(dc
->env
, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
663 dc
->pc
, op
, dc
->rd
, dc
->ra
, dc
->rb
);
668 static inline void sync_jmpstate(DisasContext
*dc
)
670 if (dc
->jmp
== JMP_DIRECT
) {
671 dc
->jmp
= JMP_INDIRECT
;
672 tcg_gen_movi_tl(env_btaken
, 1);
673 tcg_gen_movi_tl(env_btarget
, dc
->jmp_pc
);
677 static void dec_imm(DisasContext
*dc
)
679 LOG_DIS("imm %x\n", dc
->imm
<< 16);
680 tcg_gen_movi_tl(env_imm
, (dc
->imm
<< 16));
681 dc
->tb_flags
|= IMM_FLAG
;
685 static inline void gen_load(DisasContext
*dc
, TCGv dst
, TCGv addr
,
688 int mem_index
= cpu_mmu_index(dc
->env
);
691 tcg_gen_qemu_ld8u(dst
, addr
, mem_index
);
692 } else if (size
== 2) {
693 tcg_gen_qemu_ld16u(dst
, addr
, mem_index
);
694 } else if (size
== 4) {
695 tcg_gen_qemu_ld32u(dst
, addr
, mem_index
);
697 cpu_abort(dc
->env
, "Incorrect load size %d\n", size
);
700 static inline TCGv
*compute_ldst_addr(DisasContext
*dc
, TCGv
*t
)
702 unsigned int extimm
= dc
->tb_flags
& IMM_FLAG
;
704 /* Treat the fast cases first. */
707 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], cpu_R
[dc
->rb
]);
713 return &cpu_R
[dc
->ra
];
716 tcg_gen_movi_tl(*t
, (int32_t)((int16_t)dc
->imm
));
717 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *t
);
720 tcg_gen_add_tl(*t
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
726 static void dec_load(DisasContext
*dc
)
731 size
= 1 << (dc
->opcode
& 3);
733 LOG_DIS("l %x %d\n", dc
->opcode
, size
);
735 addr
= compute_ldst_addr(dc
, &t
);
737 /* If we get a fault on a dslot, the jmpstate better be in sync. */
740 gen_load(dc
, cpu_R
[dc
->rd
], *addr
, size
);
742 gen_load(dc
, env_imm
, *addr
, size
);
749 static void gen_store(DisasContext
*dc
, TCGv addr
, TCGv val
,
752 int mem_index
= cpu_mmu_index(dc
->env
);
755 tcg_gen_qemu_st8(val
, addr
, mem_index
);
756 else if (size
== 2) {
757 tcg_gen_qemu_st16(val
, addr
, mem_index
);
758 } else if (size
== 4) {
759 tcg_gen_qemu_st32(val
, addr
, mem_index
);
761 cpu_abort(dc
->env
, "Incorrect store size %d\n", size
);
764 static void dec_store(DisasContext
*dc
)
769 size
= 1 << (dc
->opcode
& 3);
771 LOG_DIS("s%d%s\n", size
, dc
->type_b
? "i" : "");
773 /* If we get a fault on a dslot, the jmpstate better be in sync. */
775 addr
= compute_ldst_addr(dc
, &t
);
776 gen_store(dc
, *addr
, cpu_R
[dc
->rd
], size
);
781 static inline void eval_cc(DisasContext
*dc
, unsigned int cc
,
782 TCGv d
, TCGv a
, TCGv b
)
788 l1
= gen_new_label();
789 tcg_gen_movi_tl(env_btaken
, 1);
790 tcg_gen_brcond_tl(TCG_COND_EQ
, a
, b
, l1
);
791 tcg_gen_movi_tl(env_btaken
, 0);
795 l1
= gen_new_label();
796 tcg_gen_movi_tl(env_btaken
, 1);
797 tcg_gen_brcond_tl(TCG_COND_NE
, a
, b
, l1
);
798 tcg_gen_movi_tl(env_btaken
, 0);
802 l1
= gen_new_label();
803 tcg_gen_movi_tl(env_btaken
, 1);
804 tcg_gen_brcond_tl(TCG_COND_LT
, a
, b
, l1
);
805 tcg_gen_movi_tl(env_btaken
, 0);
809 l1
= gen_new_label();
810 tcg_gen_movi_tl(env_btaken
, 1);
811 tcg_gen_brcond_tl(TCG_COND_LE
, a
, b
, l1
);
812 tcg_gen_movi_tl(env_btaken
, 0);
816 l1
= gen_new_label();
817 tcg_gen_movi_tl(env_btaken
, 1);
818 tcg_gen_brcond_tl(TCG_COND_GE
, a
, b
, l1
);
819 tcg_gen_movi_tl(env_btaken
, 0);
823 l1
= gen_new_label();
824 tcg_gen_movi_tl(env_btaken
, 1);
825 tcg_gen_brcond_tl(TCG_COND_GT
, a
, b
, l1
);
826 tcg_gen_movi_tl(env_btaken
, 0);
830 cpu_abort(dc
->env
, "Unknown condition code %x.\n", cc
);
835 static void eval_cond_jmp(DisasContext
*dc
, TCGv pc_true
, TCGv pc_false
)
839 l1
= gen_new_label();
840 /* Conditional jmp. */
841 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_false
);
842 tcg_gen_brcondi_tl(TCG_COND_EQ
, env_btaken
, 0, l1
);
843 tcg_gen_mov_tl(cpu_SR
[SR_PC
], pc_true
);
847 static void dec_bcc(DisasContext
*dc
)
852 cc
= EXTRACT_FIELD(dc
->ir
, 21, 23);
853 dslot
= dc
->ir
& (1 << 25);
854 LOG_DIS("bcc%s r%d %x\n", dslot
? "d" : "", dc
->ra
, dc
->imm
);
856 dc
->delayed_branch
= 1;
858 dc
->delayed_branch
= 2;
859 dc
->tb_flags
|= D_FLAG
;
860 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
861 cpu_env
, offsetof(CPUState
, bimm
));
864 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
865 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
866 eval_cc(dc
, cc
, env_btaken
, cpu_R
[dc
->ra
], tcg_const_tl(0));
867 dc
->jmp
= JMP_INDIRECT
;
870 static void dec_br(DisasContext
*dc
)
872 unsigned int dslot
, link
, abs
;
874 dslot
= dc
->ir
& (1 << 20);
875 abs
= dc
->ir
& (1 << 19);
876 link
= dc
->ir
& (1 << 18);
877 LOG_DIS("br%s%s%s%s imm=%x\n",
878 abs
? "a" : "", link
? "l" : "",
879 dc
->type_b
? "i" : "", dslot
? "d" : "",
882 dc
->delayed_branch
= 1;
884 dc
->delayed_branch
= 2;
885 dc
->tb_flags
|= D_FLAG
;
886 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
887 cpu_env
, offsetof(CPUState
, bimm
));
890 tcg_gen_movi_tl(cpu_R
[dc
->rd
], dc
->pc
);
892 dc
->jmp
= JMP_INDIRECT
;
894 tcg_gen_movi_tl(env_btaken
, 1);
895 tcg_gen_mov_tl(env_btarget
, *(dec_alu_op_b(dc
)));
896 if (link
&& !(dc
->tb_flags
& IMM_FLAG
)
897 && (dc
->imm
== 8 || dc
->imm
== 0x18))
898 t_gen_raise_exception(dc
, EXCP_BREAK
);
900 t_gen_raise_exception(dc
, EXCP_DEBUG
);
902 if (dc
->tb_flags
& IMM_FLAG
) {
903 tcg_gen_movi_tl(env_btaken
, 1);
904 tcg_gen_movi_tl(env_btarget
, dc
->pc
);
905 tcg_gen_add_tl(env_btarget
, env_btarget
, *(dec_alu_op_b(dc
)));
907 dc
->jmp
= JMP_DIRECT
;
908 dc
->jmp_pc
= dc
->pc
+ (int32_t)((int16_t)dc
->imm
);
913 static inline void do_rti(DisasContext
*dc
)
918 tcg_gen_shri_tl(t0
, cpu_SR
[SR_MSR
], 1);
919 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_IE
);
920 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
922 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
923 tcg_gen_or_tl(t1
, t1
, t0
);
927 dc
->tb_flags
&= ~DRTI_FLAG
;
930 static inline void do_rtb(DisasContext
*dc
)
935 tcg_gen_andi_tl(t1
, cpu_SR
[SR_MSR
], ~MSR_BIP
);
936 tcg_gen_shri_tl(t0
, t1
, 1);
937 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
939 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
940 tcg_gen_or_tl(t1
, t1
, t0
);
944 dc
->tb_flags
&= ~DRTB_FLAG
;
947 static inline void do_rte(DisasContext
*dc
)
953 tcg_gen_ori_tl(t1
, cpu_SR
[SR_MSR
], MSR_EE
);
954 tcg_gen_andi_tl(t1
, t1
, ~MSR_EIP
);
955 tcg_gen_shri_tl(t0
, t1
, 1);
956 tcg_gen_andi_tl(t0
, t0
, (MSR_VM
| MSR_UM
));
958 tcg_gen_andi_tl(t1
, t1
, ~(MSR_VM
| MSR_UM
));
959 tcg_gen_or_tl(t1
, t1
, t0
);
963 dc
->tb_flags
&= ~DRTE_FLAG
;
966 static void dec_rts(DisasContext
*dc
)
968 unsigned int b_bit
, i_bit
, e_bit
;
970 i_bit
= dc
->ir
& (1 << 21);
971 b_bit
= dc
->ir
& (1 << 22);
972 e_bit
= dc
->ir
& (1 << 23);
974 dc
->delayed_branch
= 2;
975 dc
->tb_flags
|= D_FLAG
;
976 tcg_gen_st_tl(tcg_const_tl(dc
->type_b
&& (dc
->tb_flags
& IMM_FLAG
)),
977 cpu_env
, offsetof(CPUState
, bimm
));
980 LOG_DIS("rtid ir=%x\n", dc
->ir
);
981 dc
->tb_flags
|= DRTI_FLAG
;
983 LOG_DIS("rtbd ir=%x\n", dc
->ir
);
984 dc
->tb_flags
|= DRTB_FLAG
;
986 LOG_DIS("rted ir=%x\n", dc
->ir
);
987 dc
->tb_flags
|= DRTE_FLAG
;
989 LOG_DIS("rts ir=%x\n", dc
->ir
);
991 tcg_gen_movi_tl(env_btaken
, 1);
992 tcg_gen_add_tl(env_btarget
, cpu_R
[dc
->ra
], *(dec_alu_op_b(dc
)));
995 static void dec_null(DisasContext
*dc
)
997 qemu_log ("unknown insn pc=%x opc=%x\n", dc
->pc
, dc
->opcode
);
998 dc
->abort_at_next_insn
= 1;
1001 static struct decoder_info
{
1006 void (*dec
)(DisasContext
*dc
);
1014 {DEC_BARREL
, dec_barrel
},
1016 {DEC_ST
, dec_store
},
1027 static inline void decode(DisasContext
*dc
)
1032 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP
)))
1033 tcg_gen_debug_insn_start(dc
->pc
);
1035 dc
->ir
= ir
= ldl_code(dc
->pc
);
1036 LOG_DIS("%8.8x\t", dc
->ir
);
1041 LOG_DIS("nr_nops=%d\t", dc
->nr_nops
);
1043 if (dc
->nr_nops
> 4)
1044 cpu_abort(dc
->env
, "fetching nop sequence\n");
1046 /* bit 2 seems to indicate insn type. */
1047 dc
->type_b
= ir
& (1 << 29);
1049 dc
->opcode
= EXTRACT_FIELD(ir
, 26, 31);
1050 dc
->rd
= EXTRACT_FIELD(ir
, 21, 25);
1051 dc
->ra
= EXTRACT_FIELD(ir
, 16, 20);
1052 dc
->rb
= EXTRACT_FIELD(ir
, 11, 15);
1053 dc
->imm
= EXTRACT_FIELD(ir
, 0, 15);
1055 /* Large switch for all insns. */
1056 for (i
= 0; i
< ARRAY_SIZE(decinfo
); i
++) {
1057 if ((dc
->opcode
& decinfo
[i
].mask
) == decinfo
[i
].bits
) {
1065 static void check_breakpoint(CPUState
*env
, DisasContext
*dc
)
1069 if (unlikely(!TAILQ_EMPTY(&env
->breakpoints
))) {
1070 TAILQ_FOREACH(bp
, &env
->breakpoints
, entry
) {
1071 if (bp
->pc
== dc
->pc
) {
1072 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1073 dc
->is_jmp
= DISAS_UPDATE
;
1079 /* generate intermediate code for basic block 'tb'. */
1081 gen_intermediate_code_internal(CPUState
*env
, TranslationBlock
*tb
,
1084 uint16_t *gen_opc_end
;
1087 struct DisasContext ctx
;
1088 struct DisasContext
*dc
= &ctx
;
1089 uint32_t next_page_start
, org_flags
;
1094 qemu_log_try_set_file(stderr
);
1099 org_flags
= dc
->synced_flags
= dc
->tb_flags
= tb
->flags
;
1101 gen_opc_end
= gen_opc_buf
+ OPC_MAX_SIZE
;
1103 dc
->is_jmp
= DISAS_NEXT
;
1105 dc
->delayed_branch
= !!(dc
->tb_flags
& D_FLAG
);
1109 dc
->singlestep_enabled
= env
->singlestep_enabled
;
1110 dc
->cpustate_changed
= 0;
1111 dc
->abort_at_next_insn
= 0;
1115 cpu_abort(env
, "Microblaze: unaligned PC=%x\n", pc_start
);
1117 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1119 qemu_log("--------------\n");
1120 log_cpu_state(env
, 0);
1124 next_page_start
= (pc_start
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
;
1127 max_insns
= tb
->cflags
& CF_COUNT_MASK
;
1129 max_insns
= CF_COUNT_MASK
;
1135 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1136 tcg_gen_movi_tl(cpu_SR
[SR_PC
], dc
->pc
);
1140 check_breakpoint(env
, dc
);
1143 j
= gen_opc_ptr
- gen_opc_buf
;
1147 gen_opc_instr_start
[lj
++] = 0;
1149 gen_opc_pc
[lj
] = dc
->pc
;
1150 gen_opc_instr_start
[lj
] = 1;
1151 gen_opc_icount
[lj
] = num_insns
;
1155 LOG_DIS("%8.8x:\t", dc
->pc
);
1157 if (num_insns
+ 1 == max_insns
&& (tb
->cflags
& CF_LAST_IO
))
1163 dc
->tb_flags
&= ~IMM_FLAG
;
1168 if (dc
->delayed_branch
) {
1169 dc
->delayed_branch
--;
1170 if (!dc
->delayed_branch
) {
1171 if (dc
->tb_flags
& DRTI_FLAG
)
1173 if (dc
->tb_flags
& DRTB_FLAG
)
1175 if (dc
->tb_flags
& DRTE_FLAG
)
1177 /* Clear the delay slot flag. */
1178 dc
->tb_flags
&= ~D_FLAG
;
1179 /* If it is a direct jump, try direct chaining. */
1180 if (dc
->jmp
!= JMP_DIRECT
) {
1181 eval_cond_jmp(dc
, env_btarget
, tcg_const_tl(dc
->pc
));
1182 dc
->is_jmp
= DISAS_JUMP
;
1187 if (env
->singlestep_enabled
)
1189 } while (!dc
->is_jmp
&& !dc
->cpustate_changed
1190 && gen_opc_ptr
< gen_opc_end
1192 && (dc
->pc
< next_page_start
)
1193 && num_insns
< max_insns
);
1196 if (dc
->jmp
== JMP_DIRECT
) {
1197 if (dc
->tb_flags
& D_FLAG
) {
1198 dc
->is_jmp
= DISAS_UPDATE
;
1199 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1205 if (tb
->cflags
& CF_LAST_IO
)
1207 /* Force an update if the per-tb cpu state has changed. */
1208 if (dc
->is_jmp
== DISAS_NEXT
1209 && (dc
->cpustate_changed
|| org_flags
!= dc
->tb_flags
)) {
1210 dc
->is_jmp
= DISAS_UPDATE
;
1211 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1215 if (unlikely(env
->singlestep_enabled
)) {
1216 t_gen_raise_exception(dc
, EXCP_DEBUG
);
1217 if (dc
->is_jmp
== DISAS_NEXT
)
1218 tcg_gen_movi_tl(cpu_SR
[SR_PC
], npc
);
1220 switch(dc
->is_jmp
) {
1222 gen_goto_tb(dc
, 1, npc
);
1227 /* indicate that the hash table must be used
1228 to find the next TB */
1232 /* nothing more to generate */
1236 gen_icount_end(tb
, num_insns
);
1237 *gen_opc_ptr
= INDEX_op_end
;
1239 j
= gen_opc_ptr
- gen_opc_buf
;
1242 gen_opc_instr_start
[lj
++] = 0;
1244 tb
->size
= dc
->pc
- pc_start
;
1245 tb
->icount
= num_insns
;
1250 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM
)) {
1253 log_target_disas(pc_start
, dc
->pc
- pc_start
, 0);
1255 qemu_log("\nisize=%d osize=%zd\n",
1256 dc
->pc
- pc_start
, gen_opc_ptr
- gen_opc_buf
);
1260 assert(!dc
->abort_at_next_insn
);
1263 void gen_intermediate_code (CPUState
*env
, struct TranslationBlock
*tb
)
1265 gen_intermediate_code_internal(env
, tb
, 0);
1268 void gen_intermediate_code_pc (CPUState
*env
, struct TranslationBlock
*tb
)
1270 gen_intermediate_code_internal(env
, tb
, 1);
1273 void cpu_dump_state (CPUState
*env
, FILE *f
,
1274 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...),
1282 cpu_fprintf(f
, "IN: PC=%x %s\n",
1283 env
->sregs
[SR_PC
], lookup_symbol(env
->sregs
[SR_PC
]));
1284 cpu_fprintf(f
, "rmsr=%x resr=%x debug[%x] imm=%x iflags=%x\n",
1285 env
->sregs
[SR_MSR
], env
->sregs
[SR_ESR
],
1286 env
->debug
, env
->imm
, env
->iflags
);
1287 cpu_fprintf(f
, "btaken=%d btarget=%x mode=%s(saved=%s)\n",
1288 env
->btaken
, env
->btarget
,
1289 (env
->sregs
[SR_MSR
] & MSR_UM
) ? "user" : "kernel",
1290 (env
->sregs
[SR_MSR
] & MSR_UMS
) ? "user" : "kernel");
1291 for (i
= 0; i
< 32; i
++) {
1292 cpu_fprintf(f
, "r%2.2d=%8.8x ", i
, env
->regs
[i
]);
1293 if ((i
+ 1) % 4 == 0)
1294 cpu_fprintf(f
, "\n");
1296 cpu_fprintf(f
, "\n\n");
1299 CPUState
*cpu_mb_init (const char *cpu_model
)
1302 static int tcg_initialized
= 0;
1305 env
= qemu_mallocz(sizeof(CPUState
));
1310 env
->pvr
.regs
[0] = PVR0_PVR_FULL_MASK \
1311 | PVR0_USE_BARREL_MASK \
1312 | PVR0_USE_DIV_MASK \
1313 | PVR0_USE_HW_MUL_MASK \
1314 | PVR0_USE_EXC_MASK \
1315 | PVR0_USE_ICACHE_MASK \
1316 | PVR0_USE_DCACHE_MASK \
1319 env
->pvr
.regs
[2] = PVR2_D_OPB_MASK \
1323 | PVR2_USE_MSR_INSTR \
1324 | PVR2_USE_PCMP_INSTR \
1325 | PVR2_USE_BARREL_MASK \
1326 | PVR2_USE_DIV_MASK \
1327 | PVR2_USE_HW_MUL_MASK \
1328 | PVR2_USE_MUL64_MASK \
1330 env
->pvr
.regs
[10] = 0x0c000000; /* Default to spartan 3a dsp family. */
1331 env
->pvr
.regs
[11] = PVR11_USE_MMU
;
1333 if (tcg_initialized
)
1336 tcg_initialized
= 1;
1338 cpu_env
= tcg_global_reg_new_ptr(TCG_AREG0
, "env");
1340 env_debug
= tcg_global_mem_new(TCG_AREG0
,
1341 offsetof(CPUState
, debug
),
1343 env_iflags
= tcg_global_mem_new(TCG_AREG0
,
1344 offsetof(CPUState
, iflags
),
1346 env_imm
= tcg_global_mem_new(TCG_AREG0
,
1347 offsetof(CPUState
, imm
),
1349 env_btarget
= tcg_global_mem_new(TCG_AREG0
,
1350 offsetof(CPUState
, btarget
),
1352 env_btaken
= tcg_global_mem_new(TCG_AREG0
,
1353 offsetof(CPUState
, btaken
),
1355 for (i
= 0; i
< ARRAY_SIZE(cpu_R
); i
++) {
1356 cpu_R
[i
] = tcg_global_mem_new(TCG_AREG0
,
1357 offsetof(CPUState
, regs
[i
]),
1360 for (i
= 0; i
< ARRAY_SIZE(cpu_SR
); i
++) {
1361 cpu_SR
[i
] = tcg_global_mem_new(TCG_AREG0
,
1362 offsetof(CPUState
, sregs
[i
]),
1363 special_regnames
[i
]);
1365 #define GEN_HELPER 2
1371 void cpu_reset (CPUState
*env
)
1373 if (qemu_loglevel_mask(CPU_LOG_RESET
)) {
1374 qemu_log("CPU Reset (CPU %d)\n", env
->cpu_index
);
1375 log_cpu_state(env
, 0);
1378 memset(env
, 0, offsetof(CPUMBState
, breakpoints
));
1381 env
->sregs
[SR_MSR
] = 0;
1382 #if defined(CONFIG_USER_ONLY)
1383 /* start in user mode with interrupts enabled. */
1384 env
->pvr
.regs
[10] = 0x0c000000; /* Spartan 3a dsp. */
1386 mmu_init(&env
->mmu
);
1390 void gen_pc_load(CPUState
*env
, struct TranslationBlock
*tb
,
1391 unsigned long searched_pc
, int pc_pos
, void *puc
)
1393 env
->sregs
[SR_PC
] = gen_opc_pc
[pc_pos
];