xref: /openbmc/qemu/target/microblaze/translate.c (revision a2b80dbd96569338a4ac3f32aebbbd2d9a7f9718)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  Xilinx MicroBlaze emulation for qemu: main translation routines.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2009 Edgar E. Iglesias.
5fcf5ef2aSThomas Huth  *  Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "disas/disas.h"
24fcf5ef2aSThomas Huth #include "exec/exec-all.h"
25dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
26fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
27fcf5ef2aSThomas Huth #include "microblaze-decode.h"
28fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h"
29fcf5ef2aSThomas Huth #include "exec/helper-gen.h"
3077fc6f5eSLluís Vilanova #include "exec/translator.h"
3190c84c56SMarkus Armbruster #include "qemu/qemu-print.h"
32fcf5ef2aSThomas Huth 
33fcf5ef2aSThomas Huth #include "trace-tcg.h"
34fcf5ef2aSThomas Huth #include "exec/log.h"
35fcf5ef2aSThomas Huth 
36fcf5ef2aSThomas Huth 
37fcf5ef2aSThomas Huth #define SIM_COMPAT 0
38fcf5ef2aSThomas Huth #define DISAS_GNU 1
39fcf5ef2aSThomas Huth #define DISAS_MB 1
40fcf5ef2aSThomas Huth #if DISAS_MB && !SIM_COMPAT
41fcf5ef2aSThomas Huth #  define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
42fcf5ef2aSThomas Huth #else
43fcf5ef2aSThomas Huth #  define LOG_DIS(...) do { } while (0)
44fcf5ef2aSThomas Huth #endif
45fcf5ef2aSThomas Huth 
46fcf5ef2aSThomas Huth #define D(x)
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth #define EXTRACT_FIELD(src, start, end) \
49fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
50fcf5ef2aSThomas Huth 
5177fc6f5eSLluís Vilanova /* is_jmp field values */
5277fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
5377fc6f5eSLluís Vilanova #define DISAS_UPDATE  DISAS_TARGET_1 /* cpu state was modified dynamically */
5477fc6f5eSLluís Vilanova 
55cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
560f96e96bSRichard Henderson static TCGv_i32 cpu_pc;
573e0e16aeSRichard Henderson static TCGv_i32 cpu_msr;
581074c0fbSRichard Henderson static TCGv_i32 cpu_msr_c;
599b158558SRichard Henderson static TCGv_i32 cpu_imm;
609b158558SRichard Henderson static TCGv_i32 cpu_btaken;
610f96e96bSRichard Henderson static TCGv_i32 cpu_btarget;
629b158558SRichard Henderson static TCGv_i32 cpu_iflags;
639b158558SRichard Henderson static TCGv cpu_res_addr;
649b158558SRichard Henderson static TCGv_i32 cpu_res_val;
65fcf5ef2aSThomas Huth 
66fcf5ef2aSThomas Huth #include "exec/gen-icount.h"
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth /* This is the state at translation time.  */
69fcf5ef2aSThomas Huth typedef struct DisasContext {
70fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
71cfeea807SEdgar E. Iglesias     uint32_t pc;
72fcf5ef2aSThomas Huth 
73fcf5ef2aSThomas Huth     /* Decoder.  */
74fcf5ef2aSThomas Huth     int type_b;
75fcf5ef2aSThomas Huth     uint32_t ir;
76fcf5ef2aSThomas Huth     uint8_t opcode;
77fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
78fcf5ef2aSThomas Huth     uint16_t imm;
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
81fcf5ef2aSThomas Huth     unsigned int delayed_branch;
82fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
83fcf5ef2aSThomas Huth     unsigned int clear_imm;
84fcf5ef2aSThomas Huth     int is_jmp;
85fcf5ef2aSThomas Huth 
86fcf5ef2aSThomas Huth #define JMP_NOJMP     0
87fcf5ef2aSThomas Huth #define JMP_DIRECT    1
88fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
89fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
90fcf5ef2aSThomas Huth     unsigned int jmp;
91fcf5ef2aSThomas Huth     uint32_t jmp_pc;
92fcf5ef2aSThomas Huth 
93fcf5ef2aSThomas Huth     int abort_at_next_insn;
94fcf5ef2aSThomas Huth     struct TranslationBlock *tb;
95fcf5ef2aSThomas Huth     int singlestep_enabled;
96fcf5ef2aSThomas Huth } DisasContext;
97fcf5ef2aSThomas Huth 
98fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
99fcf5ef2aSThomas Huth {
100fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
101fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
1029b158558SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags);
103fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
104fcf5ef2aSThomas Huth     }
105fcf5ef2aSThomas Huth }
106fcf5ef2aSThomas Huth 
10741ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
108fcf5ef2aSThomas Huth {
109fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
110fcf5ef2aSThomas Huth 
111fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
112fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
113*a2b80dbdSRichard Henderson     dc->is_jmp = DISAS_NORETURN;
114fcf5ef2aSThomas Huth }
115fcf5ef2aSThomas Huth 
11641ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
11741ba37c4SRichard Henderson {
11841ba37c4SRichard Henderson     t_sync_flags(dc);
11941ba37c4SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->pc);
12041ba37c4SRichard Henderson     gen_raise_exception(dc, index);
12141ba37c4SRichard Henderson }
12241ba37c4SRichard Henderson 
12341ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
12441ba37c4SRichard Henderson {
12541ba37c4SRichard Henderson     TCGv_i32 tmp = tcg_const_i32(esr_ec);
12641ba37c4SRichard Henderson     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, esr));
12741ba37c4SRichard Henderson     tcg_temp_free_i32(tmp);
12841ba37c4SRichard Henderson 
12941ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
13041ba37c4SRichard Henderson }
13141ba37c4SRichard Henderson 
132fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
133fcf5ef2aSThomas Huth {
134fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
135fcf5ef2aSThomas Huth     return (dc->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
136fcf5ef2aSThomas Huth #else
137fcf5ef2aSThomas Huth     return true;
138fcf5ef2aSThomas Huth #endif
139fcf5ef2aSThomas Huth }
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
142fcf5ef2aSThomas Huth {
143fcf5ef2aSThomas Huth     if (use_goto_tb(dc, dest)) {
144fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1450f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
14607ea28b4SRichard Henderson         tcg_gen_exit_tb(dc->tb, n);
147fcf5ef2aSThomas Huth     } else {
1480f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
14907ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
150fcf5ef2aSThomas Huth     }
151*a2b80dbdSRichard Henderson     dc->is_jmp = DISAS_NORETURN;
152fcf5ef2aSThomas Huth }
153fcf5ef2aSThomas Huth 
154bdfc1e88SEdgar E. Iglesias /*
1559ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1569ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1579ba8cd45SEdgar E. Iglesias  */
1589ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1599ba8cd45SEdgar E. Iglesias {
1609ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1615143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
16241ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1639ba8cd45SEdgar E. Iglesias     }
1649ba8cd45SEdgar E. Iglesias     return cond;
1659ba8cd45SEdgar E. Iglesias }
1669ba8cd45SEdgar E. Iglesias 
1679ba8cd45SEdgar E. Iglesias /*
168bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
169bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
170bdfc1e88SEdgar E. Iglesias  */
171bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
172bdfc1e88SEdgar E. Iglesias {
173bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
174bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
175bdfc1e88SEdgar E. Iglesias 
176bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
17741ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
178bdfc1e88SEdgar E. Iglesias     }
179bdfc1e88SEdgar E. Iglesias     return cond_user;
180bdfc1e88SEdgar E. Iglesias }
181bdfc1e88SEdgar E. Iglesias 
182fcf5ef2aSThomas Huth /* True if ALU operand b is a small immediate that may deserve
183fcf5ef2aSThomas Huth    faster treatment.  */
184fcf5ef2aSThomas Huth static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
185fcf5ef2aSThomas Huth {
186fcf5ef2aSThomas Huth     /* Immediate insn without the imm prefix ?  */
187fcf5ef2aSThomas Huth     return dc->type_b && !(dc->tb_flags & IMM_FLAG);
188fcf5ef2aSThomas Huth }
189fcf5ef2aSThomas Huth 
190cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
191fcf5ef2aSThomas Huth {
192fcf5ef2aSThomas Huth     if (dc->type_b) {
193fcf5ef2aSThomas Huth         if (dc->tb_flags & IMM_FLAG)
1949b158558SRichard Henderson             tcg_gen_ori_i32(cpu_imm, cpu_imm, dc->imm);
195fcf5ef2aSThomas Huth         else
1969b158558SRichard Henderson             tcg_gen_movi_i32(cpu_imm, (int32_t)((int16_t)dc->imm));
1979b158558SRichard Henderson         return &cpu_imm;
198fcf5ef2aSThomas Huth     } else
199fcf5ef2aSThomas Huth         return &cpu_R[dc->rb];
200fcf5ef2aSThomas Huth }
201fcf5ef2aSThomas Huth 
202fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
203fcf5ef2aSThomas Huth {
204fcf5ef2aSThomas Huth     unsigned int k, c;
205cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
206fcf5ef2aSThomas Huth 
207fcf5ef2aSThomas Huth     k = dc->opcode & 4;
208fcf5ef2aSThomas Huth     c = dc->opcode & 2;
209fcf5ef2aSThomas Huth 
210fcf5ef2aSThomas Huth     LOG_DIS("add%s%s%s r%d r%d r%d\n",
211fcf5ef2aSThomas Huth             dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
212fcf5ef2aSThomas Huth             dc->rd, dc->ra, dc->rb);
213fcf5ef2aSThomas Huth 
214fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
215fcf5ef2aSThomas Huth     if (k) {
216fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
217fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
218fcf5ef2aSThomas Huth         if (dc->rd) {
219cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
220fcf5ef2aSThomas Huth 
221fcf5ef2aSThomas Huth             if (c) {
222fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2231074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
224fcf5ef2aSThomas Huth             }
225fcf5ef2aSThomas Huth         }
226fcf5ef2aSThomas Huth         return;
227fcf5ef2aSThomas Huth     }
228fcf5ef2aSThomas Huth 
229fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
230fcf5ef2aSThomas Huth     /* Extract carry.  */
231cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
232fcf5ef2aSThomas Huth     if (c) {
2331074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
234fcf5ef2aSThomas Huth     } else {
235cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
236fcf5ef2aSThomas Huth     }
237fcf5ef2aSThomas Huth 
2381074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
239fcf5ef2aSThomas Huth     if (dc->rd) {
240cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
241cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
242fcf5ef2aSThomas Huth     }
243cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
244fcf5ef2aSThomas Huth }
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
247fcf5ef2aSThomas Huth {
248fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
249cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
250fcf5ef2aSThomas Huth 
251fcf5ef2aSThomas Huth     u = dc->imm & 2;
252fcf5ef2aSThomas Huth     k = dc->opcode & 4;
253fcf5ef2aSThomas Huth     c = dc->opcode & 2;
254fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
255fcf5ef2aSThomas Huth 
256fcf5ef2aSThomas Huth     if (cmp) {
257fcf5ef2aSThomas Huth         LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
258fcf5ef2aSThomas Huth         if (dc->rd) {
259fcf5ef2aSThomas Huth             if (u)
260fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
261fcf5ef2aSThomas Huth             else
262fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
263fcf5ef2aSThomas Huth         }
264fcf5ef2aSThomas Huth         return;
265fcf5ef2aSThomas Huth     }
266fcf5ef2aSThomas Huth 
267fcf5ef2aSThomas Huth     LOG_DIS("sub%s%s r%d, r%d r%d\n",
268fcf5ef2aSThomas Huth              k ? "k" : "",  c ? "c" : "", dc->rd, dc->ra, dc->rb);
269fcf5ef2aSThomas Huth 
270fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
271fcf5ef2aSThomas Huth     if (k) {
272fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
273fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
274fcf5ef2aSThomas Huth         if (dc->rd) {
275cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
276fcf5ef2aSThomas Huth 
277fcf5ef2aSThomas Huth             if (c) {
278fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2791074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
280fcf5ef2aSThomas Huth             }
281fcf5ef2aSThomas Huth         }
282fcf5ef2aSThomas Huth         return;
283fcf5ef2aSThomas Huth     }
284fcf5ef2aSThomas Huth 
285fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
286fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
287cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
288cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
289fcf5ef2aSThomas Huth     if (c) {
2901074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
291fcf5ef2aSThomas Huth     } else {
292cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
293fcf5ef2aSThomas Huth     }
294fcf5ef2aSThomas Huth 
295fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
296cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
297fcf5ef2aSThomas Huth 
2981074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, na, *(dec_alu_op_b(dc)), cf);
299fcf5ef2aSThomas Huth     if (dc->rd) {
300cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
301cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
302fcf5ef2aSThomas Huth     }
303cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
304cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
305fcf5ef2aSThomas Huth }
306fcf5ef2aSThomas Huth 
307fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
308fcf5ef2aSThomas Huth {
309fcf5ef2aSThomas Huth     unsigned int mode;
310fcf5ef2aSThomas Huth 
3119ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
3129ba8cd45SEdgar E. Iglesias         return;
313fcf5ef2aSThomas Huth     }
314fcf5ef2aSThomas Huth 
315fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
316fcf5ef2aSThomas Huth     switch (mode) {
317fcf5ef2aSThomas Huth         case 0:
318fcf5ef2aSThomas Huth             /* pcmpbf.  */
319fcf5ef2aSThomas Huth             LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
320fcf5ef2aSThomas Huth             if (dc->rd)
321fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
322fcf5ef2aSThomas Huth             break;
323fcf5ef2aSThomas Huth         case 2:
324fcf5ef2aSThomas Huth             LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
325fcf5ef2aSThomas Huth             if (dc->rd) {
326cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
327fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
328fcf5ef2aSThomas Huth             }
329fcf5ef2aSThomas Huth             break;
330fcf5ef2aSThomas Huth         case 3:
331fcf5ef2aSThomas Huth             LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
332fcf5ef2aSThomas Huth             if (dc->rd) {
333cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
334fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
335fcf5ef2aSThomas Huth             }
336fcf5ef2aSThomas Huth             break;
337fcf5ef2aSThomas Huth         default:
338fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
339fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
340fcf5ef2aSThomas Huth             break;
341fcf5ef2aSThomas Huth     }
342fcf5ef2aSThomas Huth }
343fcf5ef2aSThomas Huth 
344fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
345fcf5ef2aSThomas Huth {
346fcf5ef2aSThomas Huth     unsigned int not;
347fcf5ef2aSThomas Huth 
348fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
349fcf5ef2aSThomas Huth         dec_pattern(dc);
350fcf5ef2aSThomas Huth         return;
351fcf5ef2aSThomas Huth     }
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
354fcf5ef2aSThomas Huth     LOG_DIS("and%s\n", not ? "n" : "");
355fcf5ef2aSThomas Huth 
356fcf5ef2aSThomas Huth     if (!dc->rd)
357fcf5ef2aSThomas Huth         return;
358fcf5ef2aSThomas Huth 
359fcf5ef2aSThomas Huth     if (not) {
360cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
361fcf5ef2aSThomas Huth     } else
362cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
363fcf5ef2aSThomas Huth }
364fcf5ef2aSThomas Huth 
365fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
366fcf5ef2aSThomas Huth {
367fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
368fcf5ef2aSThomas Huth         dec_pattern(dc);
369fcf5ef2aSThomas Huth         return;
370fcf5ef2aSThomas Huth     }
371fcf5ef2aSThomas Huth 
372fcf5ef2aSThomas Huth     LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
373fcf5ef2aSThomas Huth     if (dc->rd)
374cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
375fcf5ef2aSThomas Huth }
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
378fcf5ef2aSThomas Huth {
379fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
380fcf5ef2aSThomas Huth         dec_pattern(dc);
381fcf5ef2aSThomas Huth         return;
382fcf5ef2aSThomas Huth     }
383fcf5ef2aSThomas Huth 
384fcf5ef2aSThomas Huth     LOG_DIS("xor r%d\n", dc->rd);
385fcf5ef2aSThomas Huth     if (dc->rd)
386cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
387fcf5ef2aSThomas Huth }
388fcf5ef2aSThomas Huth 
3891074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
390fcf5ef2aSThomas Huth {
3911074c0fbSRichard Henderson     TCGv_i32 t;
3921074c0fbSRichard Henderson 
3931074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
3941074c0fbSRichard Henderson     t = tcg_temp_new_i32();
3951074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
3961074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
3971074c0fbSRichard Henderson     tcg_temp_free_i32(t);
398fcf5ef2aSThomas Huth }
399fcf5ef2aSThomas Huth 
4001074c0fbSRichard Henderson static void msr_write(DisasContext *dc, TCGv_i32 v)
401fcf5ef2aSThomas Huth {
402fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
4031074c0fbSRichard Henderson 
4041074c0fbSRichard Henderson     /* Install MSR_C.  */
4051074c0fbSRichard Henderson     tcg_gen_extract_i32(cpu_msr_c, v, 2, 1);
4061074c0fbSRichard Henderson 
4071074c0fbSRichard Henderson     /* Clear MSR_C and MSR_CC; MSR_PVR is not writable, and is always clear. */
4081074c0fbSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~(MSR_C | MSR_CC | MSR_PVR));
409fcf5ef2aSThomas Huth }
410fcf5ef2aSThomas Huth 
411fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
412fcf5ef2aSThomas Huth {
413fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
414cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4152023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
416f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
417fcf5ef2aSThomas Huth 
4182023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
4192023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
4202023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
421fcf5ef2aSThomas Huth     dc->type_b = 1;
4222023e9a3SEdgar E. Iglesias     if (to) {
423fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
424f0f7e7f7SEdgar E. Iglesias     }
425f0f7e7f7SEdgar E. Iglesias 
426f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
427f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
428f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
429f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
430f0f7e7f7SEdgar E. Iglesias 
431f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4322023e9a3SEdgar E. Iglesias     }
433fcf5ef2aSThomas Huth 
434fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4352023e9a3SEdgar E. Iglesias     if (clrset) {
4362023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
437fcf5ef2aSThomas Huth 
438fcf5ef2aSThomas Huth         LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
439fcf5ef2aSThomas Huth                 dc->rd, dc->imm);
440fcf5ef2aSThomas Huth 
44156837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
442fcf5ef2aSThomas Huth             /* nop??? */
443fcf5ef2aSThomas Huth             return;
444fcf5ef2aSThomas Huth         }
445fcf5ef2aSThomas Huth 
446bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
447fcf5ef2aSThomas Huth             return;
448fcf5ef2aSThomas Huth         }
449fcf5ef2aSThomas Huth 
450fcf5ef2aSThomas Huth         if (dc->rd)
451fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
452fcf5ef2aSThomas Huth 
453cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
454cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
455fcf5ef2aSThomas Huth         msr_read(dc, t0);
456cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
457fcf5ef2aSThomas Huth 
458fcf5ef2aSThomas Huth         if (clr) {
459cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
460cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
461fcf5ef2aSThomas Huth         } else
462cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
463fcf5ef2aSThomas Huth         msr_write(dc, t0);
464cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
465cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
4660f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
467fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
468fcf5ef2aSThomas Huth         return;
469fcf5ef2aSThomas Huth     }
470fcf5ef2aSThomas Huth 
471bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
472fcf5ef2aSThomas Huth         return;
473fcf5ef2aSThomas Huth     }
474fcf5ef2aSThomas Huth 
475fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
476fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
477fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
478f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
47905a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
48005a9a651SEdgar E. Iglesias 
481fcf5ef2aSThomas Huth         sr &= 7;
48205a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
483fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
48405a9a651SEdgar E. Iglesias         if (to) {
485f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
48605a9a651SEdgar E. Iglesias         } else {
487f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
48805a9a651SEdgar E. Iglesias         }
48905a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
490f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
491fcf5ef2aSThomas Huth         return;
492fcf5ef2aSThomas Huth     }
493fcf5ef2aSThomas Huth #endif
494fcf5ef2aSThomas Huth 
495fcf5ef2aSThomas Huth     if (to) {
496fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
497fcf5ef2aSThomas Huth         switch (sr) {
498aa28e6d4SRichard Henderson             case SR_PC:
499fcf5ef2aSThomas Huth                 break;
500aa28e6d4SRichard Henderson             case SR_MSR:
501fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
502fcf5ef2aSThomas Huth                 break;
503351527b7SEdgar E. Iglesias             case SR_EAR:
504dbdb77c4SRichard Henderson                 {
505dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
506dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
507dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
508dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
509dbdb77c4SRichard Henderson                 }
510aa28e6d4SRichard Henderson                 break;
511351527b7SEdgar E. Iglesias             case SR_ESR:
51241ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
51341ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
514aa28e6d4SRichard Henderson                 break;
515ab6dd380SEdgar E. Iglesias             case SR_FSR:
51686017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
51786017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
518aa28e6d4SRichard Henderson                 break;
519aa28e6d4SRichard Henderson             case SR_BTR:
520ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
521ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
522aa28e6d4SRichard Henderson                 break;
523aa28e6d4SRichard Henderson             case SR_EDR:
52439db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
52539db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
526fcf5ef2aSThomas Huth                 break;
527fcf5ef2aSThomas Huth             case 0x800:
528cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
529cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
530fcf5ef2aSThomas Huth                 break;
531fcf5ef2aSThomas Huth             case 0x802:
532cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
533cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
534fcf5ef2aSThomas Huth                 break;
535fcf5ef2aSThomas Huth             default:
536fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
537fcf5ef2aSThomas Huth                 break;
538fcf5ef2aSThomas Huth         }
539fcf5ef2aSThomas Huth     } else {
540fcf5ef2aSThomas Huth         LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
541fcf5ef2aSThomas Huth 
542fcf5ef2aSThomas Huth         switch (sr) {
543aa28e6d4SRichard Henderson             case SR_PC:
544cfeea807SEdgar E. Iglesias                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
545fcf5ef2aSThomas Huth                 break;
546aa28e6d4SRichard Henderson             case SR_MSR:
547fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
548fcf5ef2aSThomas Huth                 break;
549351527b7SEdgar E. Iglesias             case SR_EAR:
550dbdb77c4SRichard Henderson                 {
551dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
552dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
553a1b48e3aSEdgar E. Iglesias                     if (extended) {
554dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
555aa28e6d4SRichard Henderson                     } else {
556dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
557dbdb77c4SRichard Henderson                     }
558dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
559a1b48e3aSEdgar E. Iglesias                 }
560aa28e6d4SRichard Henderson                 break;
561351527b7SEdgar E. Iglesias             case SR_ESR:
56241ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
56341ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
564aa28e6d4SRichard Henderson                 break;
565351527b7SEdgar E. Iglesias             case SR_FSR:
56686017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
56786017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
568aa28e6d4SRichard Henderson                 break;
569351527b7SEdgar E. Iglesias             case SR_BTR:
570ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
571ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
572aa28e6d4SRichard Henderson                 break;
5737cdae31dSTong Ho             case SR_EDR:
57439db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
57539db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
576fcf5ef2aSThomas Huth                 break;
577fcf5ef2aSThomas Huth             case 0x800:
578cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
579cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
580fcf5ef2aSThomas Huth                 break;
581fcf5ef2aSThomas Huth             case 0x802:
582cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
583cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
584fcf5ef2aSThomas Huth                 break;
585351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
586fcf5ef2aSThomas Huth                 rn = sr & 0xf;
587cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
588fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
589fcf5ef2aSThomas Huth                 break;
590fcf5ef2aSThomas Huth             default:
591fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
592fcf5ef2aSThomas Huth                 break;
593fcf5ef2aSThomas Huth         }
594fcf5ef2aSThomas Huth     }
595fcf5ef2aSThomas Huth 
596fcf5ef2aSThomas Huth     if (dc->rd == 0) {
597cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
598fcf5ef2aSThomas Huth     }
599fcf5ef2aSThomas Huth }
600fcf5ef2aSThomas Huth 
601fcf5ef2aSThomas Huth /* Multiplier unit.  */
602fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
603fcf5ef2aSThomas Huth {
604cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
605fcf5ef2aSThomas Huth     unsigned int subcode;
606fcf5ef2aSThomas Huth 
6079ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
608fcf5ef2aSThomas Huth         return;
609fcf5ef2aSThomas Huth     }
610fcf5ef2aSThomas Huth 
611fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
612fcf5ef2aSThomas Huth 
613fcf5ef2aSThomas Huth     if (dc->type_b) {
614fcf5ef2aSThomas Huth         LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
615cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
616fcf5ef2aSThomas Huth         return;
617fcf5ef2aSThomas Huth     }
618fcf5ef2aSThomas Huth 
619fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6209b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
621fcf5ef2aSThomas Huth         /* nop??? */
622fcf5ef2aSThomas Huth     }
623fcf5ef2aSThomas Huth 
624cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
625fcf5ef2aSThomas Huth     switch (subcode) {
626fcf5ef2aSThomas Huth         case 0:
627fcf5ef2aSThomas Huth             LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
628cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
629fcf5ef2aSThomas Huth             break;
630fcf5ef2aSThomas Huth         case 1:
631fcf5ef2aSThomas Huth             LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
632cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
633cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
634fcf5ef2aSThomas Huth             break;
635fcf5ef2aSThomas Huth         case 2:
636fcf5ef2aSThomas Huth             LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
637cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
638cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
639fcf5ef2aSThomas Huth             break;
640fcf5ef2aSThomas Huth         case 3:
641fcf5ef2aSThomas Huth             LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
642cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
643fcf5ef2aSThomas Huth             break;
644fcf5ef2aSThomas Huth         default:
645fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
646fcf5ef2aSThomas Huth             break;
647fcf5ef2aSThomas Huth     }
648cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
649fcf5ef2aSThomas Huth }
650fcf5ef2aSThomas Huth 
651fcf5ef2aSThomas Huth /* Div unit.  */
652fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
653fcf5ef2aSThomas Huth {
654fcf5ef2aSThomas Huth     unsigned int u;
655fcf5ef2aSThomas Huth 
656fcf5ef2aSThomas Huth     u = dc->imm & 2;
657fcf5ef2aSThomas Huth     LOG_DIS("div\n");
658fcf5ef2aSThomas Huth 
6599ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
6609ba8cd45SEdgar E. Iglesias         return;
661fcf5ef2aSThomas Huth     }
662fcf5ef2aSThomas Huth 
663fcf5ef2aSThomas Huth     if (u)
664fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
665fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
666fcf5ef2aSThomas Huth     else
667fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
668fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
669fcf5ef2aSThomas Huth     if (!dc->rd)
670cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
671fcf5ef2aSThomas Huth }
672fcf5ef2aSThomas Huth 
673fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
674fcf5ef2aSThomas Huth {
675cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
676faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
677d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
678fcf5ef2aSThomas Huth 
6799ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
680fcf5ef2aSThomas Huth         return;
681fcf5ef2aSThomas Huth     }
682fcf5ef2aSThomas Huth 
683faa48d74SEdgar E. Iglesias     if (dc->type_b) {
684faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
685d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
686faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
687faa48d74SEdgar E. Iglesias     }
688e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
689e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
690faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
691faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
692fcf5ef2aSThomas Huth 
693faa48d74SEdgar E. Iglesias     LOG_DIS("bs%s%s%s r%d r%d r%d\n",
694faa48d74SEdgar E. Iglesias             e ? "e" : "",
695fcf5ef2aSThomas Huth             s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
696fcf5ef2aSThomas Huth 
697faa48d74SEdgar E. Iglesias     if (e) {
698faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
699faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
700faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
701faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
702faa48d74SEdgar E. Iglesias         } else {
703faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
704faa48d74SEdgar E. Iglesias         }
705d09b2585SEdgar E. Iglesias     } else if (i) {
706d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
707d09b2585SEdgar E. Iglesias 
708d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
709d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
710d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
711d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
712d09b2585SEdgar E. Iglesias         } else {
713d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
714d09b2585SEdgar E. Iglesias                                 imm_s, width);
715d09b2585SEdgar E. Iglesias         }
716faa48d74SEdgar E. Iglesias     } else {
717cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
718fcf5ef2aSThomas Huth 
719cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
720cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
721fcf5ef2aSThomas Huth 
7222acf6d53SEdgar E. Iglesias         if (s) {
723cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7242acf6d53SEdgar E. Iglesias         } else {
7252acf6d53SEdgar E. Iglesias             if (t) {
726cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7272acf6d53SEdgar E. Iglesias             } else {
728cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
729fcf5ef2aSThomas Huth             }
730fcf5ef2aSThomas Huth         }
731cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7322acf6d53SEdgar E. Iglesias     }
733faa48d74SEdgar E. Iglesias }
734fcf5ef2aSThomas Huth 
735fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
736fcf5ef2aSThomas Huth {
737fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
738cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
739fcf5ef2aSThomas Huth     unsigned int op;
740fcf5ef2aSThomas Huth 
741fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
742fcf5ef2aSThomas Huth     switch (op) {
743fcf5ef2aSThomas Huth         case 0x21:
744fcf5ef2aSThomas Huth             /* src.  */
745cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
746fcf5ef2aSThomas Huth 
747fcf5ef2aSThomas Huth             LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
7481074c0fbSRichard Henderson             tcg_gen_shli_i32(t0, cpu_msr_c, 31);
7491074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
750fcf5ef2aSThomas Huth             if (dc->rd) {
751cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
752cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
753fcf5ef2aSThomas Huth             }
754cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
755fcf5ef2aSThomas Huth             break;
756fcf5ef2aSThomas Huth 
757fcf5ef2aSThomas Huth         case 0x1:
758fcf5ef2aSThomas Huth         case 0x41:
759fcf5ef2aSThomas Huth             /* srl.  */
760fcf5ef2aSThomas Huth             LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
761fcf5ef2aSThomas Huth 
7621074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
763fcf5ef2aSThomas Huth             if (dc->rd) {
764fcf5ef2aSThomas Huth                 if (op == 0x41)
765cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
766fcf5ef2aSThomas Huth                 else
767cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
768fcf5ef2aSThomas Huth             }
769fcf5ef2aSThomas Huth             break;
770fcf5ef2aSThomas Huth         case 0x60:
771fcf5ef2aSThomas Huth             LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
772fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
773fcf5ef2aSThomas Huth             break;
774fcf5ef2aSThomas Huth         case 0x61:
775fcf5ef2aSThomas Huth             LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
776fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
777fcf5ef2aSThomas Huth             break;
778fcf5ef2aSThomas Huth         case 0x64:
779fcf5ef2aSThomas Huth         case 0x66:
780fcf5ef2aSThomas Huth         case 0x74:
781fcf5ef2aSThomas Huth         case 0x76:
782fcf5ef2aSThomas Huth             /* wdc.  */
783fcf5ef2aSThomas Huth             LOG_DIS("wdc r%d\n", dc->ra);
784bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
785fcf5ef2aSThomas Huth             break;
786fcf5ef2aSThomas Huth         case 0x68:
787fcf5ef2aSThomas Huth             /* wic.  */
788fcf5ef2aSThomas Huth             LOG_DIS("wic r%d\n", dc->ra);
789bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
790fcf5ef2aSThomas Huth             break;
791fcf5ef2aSThomas Huth         case 0xe0:
7929ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
7939ba8cd45SEdgar E. Iglesias                 return;
794fcf5ef2aSThomas Huth             }
7958fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
7965318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
797fcf5ef2aSThomas Huth             }
798fcf5ef2aSThomas Huth             break;
799fcf5ef2aSThomas Huth         case 0x1e0:
800fcf5ef2aSThomas Huth             /* swapb */
801fcf5ef2aSThomas Huth             LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
802fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
803fcf5ef2aSThomas Huth             break;
804fcf5ef2aSThomas Huth         case 0x1e2:
805fcf5ef2aSThomas Huth             /*swaph */
806fcf5ef2aSThomas Huth             LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
807fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
808fcf5ef2aSThomas Huth             break;
809fcf5ef2aSThomas Huth         default:
810fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
811fcf5ef2aSThomas Huth                       dc->pc, op, dc->rd, dc->ra, dc->rb);
812fcf5ef2aSThomas Huth             break;
813fcf5ef2aSThomas Huth     }
814fcf5ef2aSThomas Huth }
815fcf5ef2aSThomas Huth 
816fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
817fcf5ef2aSThomas Huth {
818fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
819fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
8209b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
821fcf5ef2aSThomas Huth         }
822fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
8230f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
824fcf5ef2aSThomas Huth     }
825fcf5ef2aSThomas Huth }
826fcf5ef2aSThomas Huth 
827fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
828fcf5ef2aSThomas Huth {
829fcf5ef2aSThomas Huth     LOG_DIS("imm %x\n", dc->imm << 16);
8309b158558SRichard Henderson     tcg_gen_movi_i32(cpu_imm, (dc->imm << 16));
831fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
832fcf5ef2aSThomas Huth     dc->clear_imm = 0;
833fcf5ef2aSThomas Huth }
834fcf5ef2aSThomas Huth 
835d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
836fcf5ef2aSThomas Huth {
8370e9033c8SEdgar E. Iglesias     bool extimm = dc->tb_flags & IMM_FLAG;
8380e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8390e9033c8SEdgar E. Iglesias     bool stackprot = false;
840403322eaSEdgar E. Iglesias     TCGv_i32 t32;
841fcf5ef2aSThomas Huth 
842fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
843fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8440e9033c8SEdgar E. Iglesias         stackprot = true;
845fcf5ef2aSThomas Huth     }
846fcf5ef2aSThomas Huth 
847fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
848fcf5ef2aSThomas Huth     if (!dc->type_b) {
849d248e1beSEdgar E. Iglesias         if (ea) {
850d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
851d248e1beSEdgar E. Iglesias 
852d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
853d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
854d248e1beSEdgar E. Iglesias                 return;
855d248e1beSEdgar E. Iglesias             }
856d248e1beSEdgar E. Iglesias 
857d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
858d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
859d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
860d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
861d248e1beSEdgar E. Iglesias             }
862d248e1beSEdgar E. Iglesias             return;
863d248e1beSEdgar E. Iglesias         }
864d248e1beSEdgar E. Iglesias 
8650dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
866fcf5ef2aSThomas Huth         if (dc->ra == 0) {
867403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8680dc4af5cSEdgar E. Iglesias             return;
869fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
870403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
8710dc4af5cSEdgar E. Iglesias             return;
872fcf5ef2aSThomas Huth         }
873fcf5ef2aSThomas Huth 
874fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
8750e9033c8SEdgar E. Iglesias             stackprot = true;
876fcf5ef2aSThomas Huth         }
877fcf5ef2aSThomas Huth 
878403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
879403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
880403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
881403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
882fcf5ef2aSThomas Huth 
883fcf5ef2aSThomas Huth         if (stackprot) {
8840a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
885fcf5ef2aSThomas Huth         }
8860dc4af5cSEdgar E. Iglesias         return;
887fcf5ef2aSThomas Huth     }
888fcf5ef2aSThomas Huth     /* Immediate.  */
889403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
890fcf5ef2aSThomas Huth     if (!extimm) {
891f7a66e3aSEdgar E. Iglesias         tcg_gen_addi_i32(t32, cpu_R[dc->ra], (int16_t)dc->imm);
892403322eaSEdgar E. Iglesias     } else {
893403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
894403322eaSEdgar E. Iglesias     }
895403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
896403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
897fcf5ef2aSThomas Huth 
898fcf5ef2aSThomas Huth     if (stackprot) {
8990a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
900fcf5ef2aSThomas Huth     }
9010dc4af5cSEdgar E. Iglesias     return;
902fcf5ef2aSThomas Huth }
903fcf5ef2aSThomas Huth 
904fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
905fcf5ef2aSThomas Huth {
906403322eaSEdgar E. Iglesias     TCGv_i32 v;
907403322eaSEdgar E. Iglesias     TCGv addr;
9088534063aSEdgar E. Iglesias     unsigned int size;
909d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
910d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
91114776ab5STony Nguyen     MemOp mop;
912fcf5ef2aSThomas Huth 
913fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
914fcf5ef2aSThomas Huth     size = 1 << mop;
915fcf5ef2aSThomas Huth     if (!dc->type_b) {
916d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9178534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9188534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
919fcf5ef2aSThomas Huth     }
920fcf5ef2aSThomas Huth     mop |= MO_TE;
921fcf5ef2aSThomas Huth     if (rev) {
922fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
923fcf5ef2aSThomas Huth     }
924fcf5ef2aSThomas Huth 
9259ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
926fcf5ef2aSThomas Huth         return;
927fcf5ef2aSThomas Huth     }
928fcf5ef2aSThomas Huth 
929d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
930d248e1beSEdgar E. Iglesias         return;
931d248e1beSEdgar E. Iglesias     }
932d248e1beSEdgar E. Iglesias 
933d248e1beSEdgar E. Iglesias     LOG_DIS("l%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
934d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
935d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
936fcf5ef2aSThomas Huth 
937fcf5ef2aSThomas Huth     t_sync_flags(dc);
938403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
939d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
940d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
941d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
942fcf5ef2aSThomas Huth 
943fcf5ef2aSThomas Huth     /*
944fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
945fcf5ef2aSThomas Huth      *
946fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
947fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
948fcf5ef2aSThomas Huth      */
949fcf5ef2aSThomas Huth     if (rev && size != 4) {
950fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
951fcf5ef2aSThomas Huth         switch (size) {
952fcf5ef2aSThomas Huth             case 1:
953fcf5ef2aSThomas Huth             {
954a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
955fcf5ef2aSThomas Huth                 break;
956fcf5ef2aSThomas Huth             }
957fcf5ef2aSThomas Huth 
958fcf5ef2aSThomas Huth             case 2:
959fcf5ef2aSThomas Huth                 /* 00 -> 10
960fcf5ef2aSThomas Huth                    10 -> 00.  */
961403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
962fcf5ef2aSThomas Huth                 break;
963fcf5ef2aSThomas Huth             default:
964fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
965fcf5ef2aSThomas Huth                 break;
966fcf5ef2aSThomas Huth         }
967fcf5ef2aSThomas Huth     }
968fcf5ef2aSThomas Huth 
969fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
970fcf5ef2aSThomas Huth     if (ex) {
971403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
972fcf5ef2aSThomas Huth     }
973fcf5ef2aSThomas Huth 
974fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
975fcf5ef2aSThomas Huth     sync_jmpstate(dc);
976fcf5ef2aSThomas Huth 
977fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
978fcf5ef2aSThomas Huth     /*
979fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
980fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
981fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
982fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
983fcf5ef2aSThomas Huth      */
984cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
985d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
986fcf5ef2aSThomas Huth 
9871507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
988a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
989a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
990a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
991a6338015SEdgar E. Iglesias 
9920f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
993a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
994a6338015SEdgar E. Iglesias 
995a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
996a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
997a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
998fcf5ef2aSThomas Huth     }
999fcf5ef2aSThomas Huth 
1000fcf5ef2aSThomas Huth     if (ex) {
10019b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
10029b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
1003fcf5ef2aSThomas Huth     }
1004fcf5ef2aSThomas Huth     if (dc->rd) {
1005cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1006fcf5ef2aSThomas Huth     }
1007cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1008fcf5ef2aSThomas Huth 
1009fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1010fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
10111074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1012fcf5ef2aSThomas Huth     }
1013fcf5ef2aSThomas Huth 
1014403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1015fcf5ef2aSThomas Huth }
1016fcf5ef2aSThomas Huth 
1017fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1018fcf5ef2aSThomas Huth {
1019403322eaSEdgar E. Iglesias     TCGv addr;
1020fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1021b51b3d43SEdgar E. Iglesias     unsigned int size;
1022d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1023d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
102414776ab5STony Nguyen     MemOp mop;
1025fcf5ef2aSThomas Huth 
1026fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1027fcf5ef2aSThomas Huth     size = 1 << mop;
1028fcf5ef2aSThomas Huth     if (!dc->type_b) {
1029d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1030b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1031b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1032fcf5ef2aSThomas Huth     }
1033fcf5ef2aSThomas Huth     mop |= MO_TE;
1034fcf5ef2aSThomas Huth     if (rev) {
1035fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1036fcf5ef2aSThomas Huth     }
1037fcf5ef2aSThomas Huth 
10389ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1039fcf5ef2aSThomas Huth         return;
1040fcf5ef2aSThomas Huth     }
1041fcf5ef2aSThomas Huth 
1042d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1043d248e1beSEdgar E. Iglesias 
1044d248e1beSEdgar E. Iglesias     LOG_DIS("s%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1045d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
1046d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
1047fcf5ef2aSThomas Huth     t_sync_flags(dc);
1048fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1049fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10500dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1051403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1052d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1053d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1054d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1055fcf5ef2aSThomas Huth 
1056fcf5ef2aSThomas Huth     if (ex) { /* swx */
1057cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1058fcf5ef2aSThomas Huth 
1059fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1060403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1061fcf5ef2aSThomas Huth 
10621074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 1);
1063fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
10649b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1065fcf5ef2aSThomas Huth 
1066071cdc67SEdgar E. Iglesias         /*
1067071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1068071cdc67SEdgar E. Iglesias          * the reserved location.
1069071cdc67SEdgar E. Iglesias          */
1070cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1071071cdc67SEdgar E. Iglesias 
10729b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1073071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1074071cdc67SEdgar E. Iglesias                                    mop);
1075071cdc67SEdgar E. Iglesias 
10769b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
10771074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1078cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1079fcf5ef2aSThomas Huth     }
1080fcf5ef2aSThomas Huth 
1081fcf5ef2aSThomas Huth     if (rev && size != 4) {
1082fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1083fcf5ef2aSThomas Huth         switch (size) {
1084fcf5ef2aSThomas Huth             case 1:
1085fcf5ef2aSThomas Huth             {
1086a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1087fcf5ef2aSThomas Huth                 break;
1088fcf5ef2aSThomas Huth             }
1089fcf5ef2aSThomas Huth 
1090fcf5ef2aSThomas Huth             case 2:
1091fcf5ef2aSThomas Huth                 /* 00 -> 10
1092fcf5ef2aSThomas Huth                    10 -> 00.  */
1093fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1094403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1095fcf5ef2aSThomas Huth                 break;
1096fcf5ef2aSThomas Huth             default:
1097fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1098fcf5ef2aSThomas Huth                 break;
1099fcf5ef2aSThomas Huth         }
1100fcf5ef2aSThomas Huth     }
1101071cdc67SEdgar E. Iglesias 
1102071cdc67SEdgar E. Iglesias     if (!ex) {
1103d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1104071cdc67SEdgar E. Iglesias     }
1105fcf5ef2aSThomas Huth 
1106fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11071507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1108a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1109a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1110a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1111a6338015SEdgar E. Iglesias 
11120f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
1113fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1114fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1115fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1116fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1117fcf5ef2aSThomas Huth          *        access.
1118fcf5ef2aSThomas Huth          */
1119a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1120a6338015SEdgar E. Iglesias 
1121a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1122a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1123a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1124fcf5ef2aSThomas Huth     }
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth     if (ex) {
1127fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1128fcf5ef2aSThomas Huth     }
1129fcf5ef2aSThomas Huth 
1130403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1131fcf5ef2aSThomas Huth }
1132fcf5ef2aSThomas Huth 
1133fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11349e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1135fcf5ef2aSThomas Huth {
1136d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1137d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1138d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1139d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1140d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1141d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1142d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1143d89b86e9SEdgar E. Iglesias     };
1144d89b86e9SEdgar E. Iglesias 
1145fcf5ef2aSThomas Huth     switch (cc) {
1146fcf5ef2aSThomas Huth     case CC_EQ:
1147fcf5ef2aSThomas Huth     case CC_NE:
1148fcf5ef2aSThomas Huth     case CC_LT:
1149fcf5ef2aSThomas Huth     case CC_LE:
1150fcf5ef2aSThomas Huth     case CC_GE:
1151fcf5ef2aSThomas Huth     case CC_GT:
11529e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1153fcf5ef2aSThomas Huth         break;
1154fcf5ef2aSThomas Huth     default:
1155fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1156fcf5ef2aSThomas Huth         break;
1157fcf5ef2aSThomas Huth     }
1158fcf5ef2aSThomas Huth }
1159fcf5ef2aSThomas Huth 
11600f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1161fcf5ef2aSThomas Huth {
11620f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1163e956caf2SEdgar E. Iglesias 
11640f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
11659b158558SRichard Henderson                         cpu_btaken, zero,
1166e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1167e956caf2SEdgar E. Iglesias 
11680f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1169fcf5ef2aSThomas Huth }
1170fcf5ef2aSThomas Huth 
1171f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1172f91c60f0SEdgar E. Iglesias {
1173f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1174f91c60f0SEdgar E. Iglesias 
1175f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1176f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1177f91c60f0SEdgar E. Iglesias 
1178f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1179f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1180f91c60f0SEdgar E. Iglesias }
1181f91c60f0SEdgar E. Iglesias 
1182fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1183fcf5ef2aSThomas Huth {
1184fcf5ef2aSThomas Huth     unsigned int cc;
1185fcf5ef2aSThomas Huth     unsigned int dslot;
1186fcf5ef2aSThomas Huth 
1187fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1188fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1189fcf5ef2aSThomas Huth     LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1190fcf5ef2aSThomas Huth 
1191fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1192fcf5ef2aSThomas Huth     if (dslot) {
1193f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1194fcf5ef2aSThomas Huth     }
1195fcf5ef2aSThomas Huth 
1196fcf5ef2aSThomas Huth     if (dec_alu_op_b_is_small_imm(dc)) {
1197fcf5ef2aSThomas Huth         int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend.  */
1198fcf5ef2aSThomas Huth 
11990f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->pc + offset);
1200fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1201fcf5ef2aSThomas Huth         dc->jmp_pc = dc->pc + offset;
1202fcf5ef2aSThomas Huth     } else {
1203fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
12040f96e96bSRichard Henderson         tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1205fcf5ef2aSThomas Huth     }
12069b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1207fcf5ef2aSThomas Huth }
1208fcf5ef2aSThomas Huth 
1209fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1210fcf5ef2aSThomas Huth {
1211fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1212fcf5ef2aSThomas Huth 
1213fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1214fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1215fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1216fcf5ef2aSThomas Huth 
1217fcf5ef2aSThomas Huth     /* Memory barrier.  */
1218fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1219fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1220badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1221badcbf9dSEdgar E. Iglesias 
12226f3c458bSEdgar E. Iglesias         LOG_DIS("mbar %d\n", mbar_imm);
12236f3c458bSEdgar E. Iglesias 
12243f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12253f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12263f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12273f172744SEdgar E. Iglesias         }
12283f172744SEdgar E. Iglesias 
1229fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1230badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
123141ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1232fcf5ef2aSThomas Huth 
1233fcf5ef2aSThomas Huth             LOG_DIS("sleep\n");
1234fcf5ef2aSThomas Huth 
1235b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1236b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1237b4919e7dSEdgar E. Iglesias                 return;
1238b4919e7dSEdgar E. Iglesias             }
1239b4919e7dSEdgar E. Iglesias 
1240fcf5ef2aSThomas Huth             t_sync_flags(dc);
124141ba37c4SRichard Henderson 
124241ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1243fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1244fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1245fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1246fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
124741ba37c4SRichard Henderson 
124841ba37c4SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
124941ba37c4SRichard Henderson 
125041ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1251fcf5ef2aSThomas Huth             return;
1252fcf5ef2aSThomas Huth         }
1253fcf5ef2aSThomas Huth         /* Break the TB.  */
1254fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1255fcf5ef2aSThomas Huth         return;
1256fcf5ef2aSThomas Huth     }
1257fcf5ef2aSThomas Huth 
1258fcf5ef2aSThomas Huth     LOG_DIS("br%s%s%s%s imm=%x\n",
1259fcf5ef2aSThomas Huth              abs ? "a" : "", link ? "l" : "",
1260fcf5ef2aSThomas Huth              dc->type_b ? "i" : "", dslot ? "d" : "",
1261fcf5ef2aSThomas Huth              dc->imm);
1262fcf5ef2aSThomas Huth 
1263fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1264fcf5ef2aSThomas Huth     if (dslot) {
1265f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1266fcf5ef2aSThomas Huth     }
1267fcf5ef2aSThomas Huth     if (link && dc->rd)
1268cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
1269fcf5ef2aSThomas Huth 
1270fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1271fcf5ef2aSThomas Huth     if (abs) {
12729b158558SRichard Henderson         tcg_gen_movi_i32(cpu_btaken, 1);
12730f96e96bSRichard Henderson         tcg_gen_mov_i32(cpu_btarget, *(dec_alu_op_b(dc)));
1274fcf5ef2aSThomas Huth         if (link && !dslot) {
127541ba37c4SRichard Henderson             if (!(dc->tb_flags & IMM_FLAG) &&
127641ba37c4SRichard Henderson                 (dc->imm == 8 || dc->imm == 0x18)) {
127741ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
127841ba37c4SRichard Henderson             }
1279fcf5ef2aSThomas Huth             if (dc->imm == 0) {
1280bdfc1e88SEdgar E. Iglesias                 if (trap_userspace(dc, true)) {
1281fcf5ef2aSThomas Huth                     return;
1282fcf5ef2aSThomas Huth                 }
128341ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_DEBUG);
1284fcf5ef2aSThomas Huth             }
1285fcf5ef2aSThomas Huth         }
1286fcf5ef2aSThomas Huth     } else {
1287fcf5ef2aSThomas Huth         if (dec_alu_op_b_is_small_imm(dc)) {
1288fcf5ef2aSThomas Huth             dc->jmp = JMP_DIRECT;
1289fcf5ef2aSThomas Huth             dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1290fcf5ef2aSThomas Huth         } else {
12919b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
12920f96e96bSRichard Henderson             tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1293fcf5ef2aSThomas Huth         }
1294fcf5ef2aSThomas Huth     }
1295fcf5ef2aSThomas Huth }
1296fcf5ef2aSThomas Huth 
1297fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1298fcf5ef2aSThomas Huth {
1299cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1300cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1301cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13023e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13030a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13040a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1305cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1306fcf5ef2aSThomas Huth 
1307cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1308cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1309fcf5ef2aSThomas Huth     msr_write(dc, t1);
1310cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1311cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1312fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1313fcf5ef2aSThomas Huth }
1314fcf5ef2aSThomas Huth 
1315fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1316fcf5ef2aSThomas Huth {
1317cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1318cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1319cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13203e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13210a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1322cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1323cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1324fcf5ef2aSThomas Huth 
1325cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1326cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1327fcf5ef2aSThomas Huth     msr_write(dc, t1);
1328cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1329cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1330fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1331fcf5ef2aSThomas Huth }
1332fcf5ef2aSThomas Huth 
1333fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1334fcf5ef2aSThomas Huth {
1335cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1336cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1337cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1338fcf5ef2aSThomas Huth 
13393e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13400a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1341cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1342cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1343cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1344fcf5ef2aSThomas Huth 
1345cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1346cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1347fcf5ef2aSThomas Huth     msr_write(dc, t1);
1348cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1349cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1350fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1351fcf5ef2aSThomas Huth }
1352fcf5ef2aSThomas Huth 
1353fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1354fcf5ef2aSThomas Huth {
1355fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1356fcf5ef2aSThomas Huth 
1357fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1358fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1359fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1360fcf5ef2aSThomas Huth 
1361bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1362bdfc1e88SEdgar E. Iglesias         return;
1363bdfc1e88SEdgar E. Iglesias     }
1364bdfc1e88SEdgar E. Iglesias 
1365f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1366fcf5ef2aSThomas Huth 
1367fcf5ef2aSThomas Huth     if (i_bit) {
1368fcf5ef2aSThomas Huth         LOG_DIS("rtid ir=%x\n", dc->ir);
1369fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1370fcf5ef2aSThomas Huth     } else if (b_bit) {
1371fcf5ef2aSThomas Huth         LOG_DIS("rtbd ir=%x\n", dc->ir);
1372fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1373fcf5ef2aSThomas Huth     } else if (e_bit) {
1374fcf5ef2aSThomas Huth         LOG_DIS("rted ir=%x\n", dc->ir);
1375fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
1376fcf5ef2aSThomas Huth     } else
1377fcf5ef2aSThomas Huth         LOG_DIS("rts ir=%x\n", dc->ir);
1378fcf5ef2aSThomas Huth 
1379fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
13809b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
13810f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1382fcf5ef2aSThomas Huth }
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1385fcf5ef2aSThomas Huth {
1386fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
138741ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1388fcf5ef2aSThomas Huth     }
13892016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1390fcf5ef2aSThomas Huth }
1391fcf5ef2aSThomas Huth 
1392fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1393fcf5ef2aSThomas Huth {
1394fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1395fcf5ef2aSThomas Huth 
13969ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1397fcf5ef2aSThomas Huth         return;
1398fcf5ef2aSThomas Huth     }
1399fcf5ef2aSThomas Huth 
1400fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1401fcf5ef2aSThomas Huth 
1402fcf5ef2aSThomas Huth     switch (fpu_insn) {
1403fcf5ef2aSThomas Huth         case 0:
1404fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1405fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1406fcf5ef2aSThomas Huth             break;
1407fcf5ef2aSThomas Huth 
1408fcf5ef2aSThomas Huth         case 1:
1409fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1410fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1411fcf5ef2aSThomas Huth             break;
1412fcf5ef2aSThomas Huth 
1413fcf5ef2aSThomas Huth         case 2:
1414fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1415fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1416fcf5ef2aSThomas Huth             break;
1417fcf5ef2aSThomas Huth 
1418fcf5ef2aSThomas Huth         case 3:
1419fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1420fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1421fcf5ef2aSThomas Huth             break;
1422fcf5ef2aSThomas Huth 
1423fcf5ef2aSThomas Huth         case 4:
1424fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1425fcf5ef2aSThomas Huth                 case 0:
1426fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1427fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1428fcf5ef2aSThomas Huth                     break;
1429fcf5ef2aSThomas Huth                 case 1:
1430fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1431fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1432fcf5ef2aSThomas Huth                     break;
1433fcf5ef2aSThomas Huth                 case 2:
1434fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1435fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1436fcf5ef2aSThomas Huth                     break;
1437fcf5ef2aSThomas Huth                 case 3:
1438fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1439fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1440fcf5ef2aSThomas Huth                     break;
1441fcf5ef2aSThomas Huth                 case 4:
1442fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1443fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1444fcf5ef2aSThomas Huth                     break;
1445fcf5ef2aSThomas Huth                 case 5:
1446fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1447fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1448fcf5ef2aSThomas Huth                     break;
1449fcf5ef2aSThomas Huth                 case 6:
1450fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1451fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1452fcf5ef2aSThomas Huth                     break;
1453fcf5ef2aSThomas Huth                 default:
1454fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1455fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1456fcf5ef2aSThomas Huth                                   " opc=%x\n",
1457fcf5ef2aSThomas Huth                                   fpu_insn, dc->pc, dc->opcode);
1458fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1459fcf5ef2aSThomas Huth                     break;
1460fcf5ef2aSThomas Huth             }
1461fcf5ef2aSThomas Huth             break;
1462fcf5ef2aSThomas Huth 
1463fcf5ef2aSThomas Huth         case 5:
1464fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1465fcf5ef2aSThomas Huth                 return;
1466fcf5ef2aSThomas Huth             }
1467fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1468fcf5ef2aSThomas Huth             break;
1469fcf5ef2aSThomas Huth 
1470fcf5ef2aSThomas Huth         case 6:
1471fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1472fcf5ef2aSThomas Huth                 return;
1473fcf5ef2aSThomas Huth             }
1474fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1475fcf5ef2aSThomas Huth             break;
1476fcf5ef2aSThomas Huth 
1477fcf5ef2aSThomas Huth         case 7:
1478fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1479fcf5ef2aSThomas Huth                 return;
1480fcf5ef2aSThomas Huth             }
1481fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1482fcf5ef2aSThomas Huth             break;
1483fcf5ef2aSThomas Huth 
1484fcf5ef2aSThomas Huth         default:
1485fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1486fcf5ef2aSThomas Huth                           " opc=%x\n",
1487fcf5ef2aSThomas Huth                           fpu_insn, dc->pc, dc->opcode);
1488fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1489fcf5ef2aSThomas Huth             break;
1490fcf5ef2aSThomas Huth     }
1491fcf5ef2aSThomas Huth }
1492fcf5ef2aSThomas Huth 
1493fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1494fcf5ef2aSThomas Huth {
14959ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1496fcf5ef2aSThomas Huth         return;
1497fcf5ef2aSThomas Huth     }
1498fcf5ef2aSThomas Huth     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1499fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1500fcf5ef2aSThomas Huth }
1501fcf5ef2aSThomas Huth 
1502fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1503fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1504fcf5ef2aSThomas Huth {
1505fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1506fcf5ef2aSThomas Huth     int ctrl;
1507fcf5ef2aSThomas Huth 
1508fcf5ef2aSThomas Huth     LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1509fcf5ef2aSThomas Huth             dc->type_b ? "" : "d", dc->imm);
1510fcf5ef2aSThomas Huth 
1511bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1512fcf5ef2aSThomas Huth         return;
1513fcf5ef2aSThomas Huth     }
1514fcf5ef2aSThomas Huth 
1515cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1516fcf5ef2aSThomas Huth     if (dc->type_b) {
1517cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1518fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1519fcf5ef2aSThomas Huth     } else {
1520cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1521fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1522fcf5ef2aSThomas Huth     }
1523fcf5ef2aSThomas Huth 
1524cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1525fcf5ef2aSThomas Huth 
1526fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1527fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1528fcf5ef2aSThomas Huth     } else {
1529fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1530fcf5ef2aSThomas Huth     }
1531cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1532cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1533fcf5ef2aSThomas Huth }
1534fcf5ef2aSThomas Huth 
1535fcf5ef2aSThomas Huth static struct decoder_info {
1536fcf5ef2aSThomas Huth     struct {
1537fcf5ef2aSThomas Huth         uint32_t bits;
1538fcf5ef2aSThomas Huth         uint32_t mask;
1539fcf5ef2aSThomas Huth     };
1540fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1541fcf5ef2aSThomas Huth } decinfo[] = {
1542fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1543fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1544fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1545fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1546fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1547fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1548fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1549fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1550fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1551fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1552fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1553fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1554fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1555fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1556fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1557fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1558fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1559fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1560fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1561fcf5ef2aSThomas Huth };
1562fcf5ef2aSThomas Huth 
1563fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1564fcf5ef2aSThomas Huth {
1565fcf5ef2aSThomas Huth     int i;
1566fcf5ef2aSThomas Huth 
1567fcf5ef2aSThomas Huth     dc->ir = ir;
1568fcf5ef2aSThomas Huth     LOG_DIS("%8.8x\t", dc->ir);
1569fcf5ef2aSThomas Huth 
1570462c2544SEdgar E. Iglesias     if (ir == 0) {
15711ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1572462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1573462c2544SEdgar E. Iglesias         return;
1574462c2544SEdgar E. Iglesias     }
1575fcf5ef2aSThomas Huth 
1576fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1577fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1578fcf5ef2aSThomas Huth 
1579fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1580fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1581fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1582fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1583fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1584fcf5ef2aSThomas Huth 
1585fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1586fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1587fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1588fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1589fcf5ef2aSThomas Huth             break;
1590fcf5ef2aSThomas Huth         }
1591fcf5ef2aSThomas Huth     }
1592fcf5ef2aSThomas Huth }
1593fcf5ef2aSThomas Huth 
1594fcf5ef2aSThomas Huth /* generate intermediate code for basic block 'tb'.  */
15958b86d6d2SRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1596fcf5ef2aSThomas Huth {
15979c489ea6SLluís Vilanova     CPUMBState *env = cs->env_ptr;
1598f5c7e93aSRichard Henderson     MicroBlazeCPU *cpu = env_archcpu(env);
1599fcf5ef2aSThomas Huth     uint32_t pc_start;
1600fcf5ef2aSThomas Huth     struct DisasContext ctx;
1601fcf5ef2aSThomas Huth     struct DisasContext *dc = &ctx;
160256371527SEmilio G. Cota     uint32_t page_start, org_flags;
1603cfeea807SEdgar E. Iglesias     uint32_t npc;
1604fcf5ef2aSThomas Huth     int num_insns;
1605fcf5ef2aSThomas Huth 
1606fcf5ef2aSThomas Huth     pc_start = tb->pc;
1607fcf5ef2aSThomas Huth     dc->cpu = cpu;
1608fcf5ef2aSThomas Huth     dc->tb = tb;
1609fcf5ef2aSThomas Huth     org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1610fcf5ef2aSThomas Huth 
1611fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_NEXT;
1612fcf5ef2aSThomas Huth     dc->jmp = 0;
1613fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1614fcf5ef2aSThomas Huth     if (dc->delayed_branch) {
1615fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1616fcf5ef2aSThomas Huth     }
1617fcf5ef2aSThomas Huth     dc->pc = pc_start;
1618fcf5ef2aSThomas Huth     dc->singlestep_enabled = cs->singlestep_enabled;
1619fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1620fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1621fcf5ef2aSThomas Huth 
1622fcf5ef2aSThomas Huth     if (pc_start & 3) {
1623fcf5ef2aSThomas Huth         cpu_abort(cs, "Microblaze: unaligned PC=%x\n", pc_start);
1624fcf5ef2aSThomas Huth     }
1625fcf5ef2aSThomas Huth 
162656371527SEmilio G. Cota     page_start = pc_start & TARGET_PAGE_MASK;
1627fcf5ef2aSThomas Huth     num_insns = 0;
1628fcf5ef2aSThomas Huth 
1629fcf5ef2aSThomas Huth     gen_tb_start(tb);
1630fcf5ef2aSThomas Huth     do
1631fcf5ef2aSThomas Huth     {
1632fcf5ef2aSThomas Huth         tcg_gen_insn_start(dc->pc);
1633fcf5ef2aSThomas Huth         num_insns++;
1634fcf5ef2aSThomas Huth 
1635fcf5ef2aSThomas Huth         if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
163641ba37c4SRichard Henderson             gen_raise_exception_sync(dc, EXCP_DEBUG);
1637fcf5ef2aSThomas Huth             /* The address covered by the breakpoint must be included in
1638fcf5ef2aSThomas Huth                [tb->pc, tb->pc + tb->size) in order to for it to be
1639fcf5ef2aSThomas Huth                properly cleared -- thus we increment the PC here so that
1640fcf5ef2aSThomas Huth                the logic setting tb->size below does the right thing.  */
1641fcf5ef2aSThomas Huth             dc->pc += 4;
1642fcf5ef2aSThomas Huth             break;
1643fcf5ef2aSThomas Huth         }
1644fcf5ef2aSThomas Huth 
1645fcf5ef2aSThomas Huth         /* Pretty disas.  */
1646fcf5ef2aSThomas Huth         LOG_DIS("%8.8x:\t", dc->pc);
1647fcf5ef2aSThomas Huth 
1648c5a49c63SEmilio G. Cota         if (num_insns == max_insns && (tb_cflags(tb) & CF_LAST_IO)) {
1649fcf5ef2aSThomas Huth             gen_io_start();
1650fcf5ef2aSThomas Huth         }
1651fcf5ef2aSThomas Huth 
1652fcf5ef2aSThomas Huth         dc->clear_imm = 1;
1653fcf5ef2aSThomas Huth         decode(dc, cpu_ldl_code(env, dc->pc));
1654fcf5ef2aSThomas Huth         if (dc->clear_imm)
1655fcf5ef2aSThomas Huth             dc->tb_flags &= ~IMM_FLAG;
1656fcf5ef2aSThomas Huth         dc->pc += 4;
1657fcf5ef2aSThomas Huth 
1658fcf5ef2aSThomas Huth         if (dc->delayed_branch) {
1659fcf5ef2aSThomas Huth             dc->delayed_branch--;
1660fcf5ef2aSThomas Huth             if (!dc->delayed_branch) {
1661fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTI_FLAG)
1662fcf5ef2aSThomas Huth                     do_rti(dc);
1663fcf5ef2aSThomas Huth                  if (dc->tb_flags & DRTB_FLAG)
1664fcf5ef2aSThomas Huth                     do_rtb(dc);
1665fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTE_FLAG)
1666fcf5ef2aSThomas Huth                     do_rte(dc);
1667fcf5ef2aSThomas Huth                 /* Clear the delay slot flag.  */
1668fcf5ef2aSThomas Huth                 dc->tb_flags &= ~D_FLAG;
1669fcf5ef2aSThomas Huth                 /* If it is a direct jump, try direct chaining.  */
1670fcf5ef2aSThomas Huth                 if (dc->jmp == JMP_INDIRECT) {
16710f96e96bSRichard Henderson                     TCGv_i32 tmp_pc = tcg_const_i32(dc->pc);
16720f96e96bSRichard Henderson                     eval_cond_jmp(dc, cpu_btarget, tmp_pc);
16730f96e96bSRichard Henderson                     tcg_temp_free_i32(tmp_pc);
1674fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_JUMP;
1675fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT) {
1676fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1677fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1678fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT_CC) {
1679fcf5ef2aSThomas Huth                     TCGLabel *l1 = gen_new_label();
1680fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1681fcf5ef2aSThomas Huth                     /* Conditional jmp.  */
16829b158558SRichard Henderson                     tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1683fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 1, dc->pc);
1684fcf5ef2aSThomas Huth                     gen_set_label(l1);
1685fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1686fcf5ef2aSThomas Huth                 }
1687fcf5ef2aSThomas Huth                 break;
1688fcf5ef2aSThomas Huth             }
1689fcf5ef2aSThomas Huth         }
1690fcf5ef2aSThomas Huth         if (cs->singlestep_enabled) {
1691fcf5ef2aSThomas Huth             break;
1692fcf5ef2aSThomas Huth         }
1693fcf5ef2aSThomas Huth     } while (!dc->is_jmp && !dc->cpustate_changed
1694fcf5ef2aSThomas Huth              && !tcg_op_buf_full()
1695fcf5ef2aSThomas Huth              && !singlestep
169656371527SEmilio G. Cota              && (dc->pc - page_start < TARGET_PAGE_SIZE)
1697fcf5ef2aSThomas Huth              && num_insns < max_insns);
1698fcf5ef2aSThomas Huth 
1699fcf5ef2aSThomas Huth     npc = dc->pc;
1700fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
1701fcf5ef2aSThomas Huth         if (dc->tb_flags & D_FLAG) {
1702fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
17030f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1704fcf5ef2aSThomas Huth             sync_jmpstate(dc);
1705fcf5ef2aSThomas Huth         } else
1706fcf5ef2aSThomas Huth             npc = dc->jmp_pc;
1707fcf5ef2aSThomas Huth     }
1708fcf5ef2aSThomas Huth 
1709fcf5ef2aSThomas Huth     /* Force an update if the per-tb cpu state has changed.  */
1710fcf5ef2aSThomas Huth     if (dc->is_jmp == DISAS_NEXT
1711fcf5ef2aSThomas Huth         && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1712fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
17130f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, npc);
1714fcf5ef2aSThomas Huth     }
1715fcf5ef2aSThomas Huth     t_sync_flags(dc);
1716fcf5ef2aSThomas Huth 
1717*a2b80dbdSRichard Henderson     if (dc->is_jmp == DISAS_NORETURN) {
1718*a2b80dbdSRichard Henderson         /* nothing more to generate */
1719*a2b80dbdSRichard Henderson     } else if (unlikely(cs->singlestep_enabled)) {
1720fcf5ef2aSThomas Huth         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1721fcf5ef2aSThomas Huth 
1722fcf5ef2aSThomas Huth         if (dc->is_jmp != DISAS_JUMP) {
17230f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1724fcf5ef2aSThomas Huth         }
1725fcf5ef2aSThomas Huth         gen_helper_raise_exception(cpu_env, tmp);
1726fcf5ef2aSThomas Huth         tcg_temp_free_i32(tmp);
1727fcf5ef2aSThomas Huth     } else {
1728fcf5ef2aSThomas Huth         switch(dc->is_jmp) {
1729fcf5ef2aSThomas Huth             case DISAS_NEXT:
1730fcf5ef2aSThomas Huth                 gen_goto_tb(dc, 1, npc);
1731fcf5ef2aSThomas Huth                 break;
1732fcf5ef2aSThomas Huth             case DISAS_JUMP:
1733fcf5ef2aSThomas Huth             case DISAS_UPDATE:
1734fcf5ef2aSThomas Huth                 /* indicate that the hash table must be used
1735fcf5ef2aSThomas Huth                    to find the next TB */
173607ea28b4SRichard Henderson                 tcg_gen_exit_tb(NULL, 0);
1737fcf5ef2aSThomas Huth                 break;
1738*a2b80dbdSRichard Henderson             default:
1739*a2b80dbdSRichard Henderson                 g_assert_not_reached();
1740fcf5ef2aSThomas Huth         }
1741fcf5ef2aSThomas Huth     }
1742fcf5ef2aSThomas Huth     gen_tb_end(tb, num_insns);
1743fcf5ef2aSThomas Huth 
1744fcf5ef2aSThomas Huth     tb->size = dc->pc - pc_start;
1745fcf5ef2aSThomas Huth     tb->icount = num_insns;
1746fcf5ef2aSThomas Huth 
1747fcf5ef2aSThomas Huth #ifdef DEBUG_DISAS
1748fcf5ef2aSThomas Huth #if !SIM_COMPAT
1749fcf5ef2aSThomas Huth     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
1750fcf5ef2aSThomas Huth         && qemu_log_in_addr_range(pc_start)) {
1751fc59d2d8SRobert Foley         FILE *logfile = qemu_log_lock();
1752fcf5ef2aSThomas Huth         qemu_log("--------------\n");
17531d48474dSRichard Henderson         log_target_disas(cs, pc_start, dc->pc - pc_start);
1754fc59d2d8SRobert Foley         qemu_log_unlock(logfile);
1755fcf5ef2aSThomas Huth     }
1756fcf5ef2aSThomas Huth #endif
1757fcf5ef2aSThomas Huth #endif
1758fcf5ef2aSThomas Huth     assert(!dc->abort_at_next_insn);
1759fcf5ef2aSThomas Huth }
1760fcf5ef2aSThomas Huth 
176190c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1762fcf5ef2aSThomas Huth {
1763fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1764fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1765fcf5ef2aSThomas Huth     int i;
1766fcf5ef2aSThomas Huth 
176790c84c56SMarkus Armbruster     if (!env) {
1768fcf5ef2aSThomas Huth         return;
176990c84c56SMarkus Armbruster     }
1770fcf5ef2aSThomas Huth 
17710f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
177276e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
17736efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1774eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
177578e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1776eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
17770f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1778fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
17792e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
17802e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
17812e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
17822e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
17832ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
17842ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
17852ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
17862ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
17872ead1b18SJoe Komlodi         }
17882ead1b18SJoe Komlodi     }
1789fcf5ef2aSThomas Huth 
17902ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
179139db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1792af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
17932ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1794fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
179590c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1796fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
179790c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1798fcf5ef2aSThomas Huth         }
179990c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1800fcf5ef2aSThomas Huth }
1801fcf5ef2aSThomas Huth 
1802fcf5ef2aSThomas Huth void mb_tcg_init(void)
1803fcf5ef2aSThomas Huth {
1804480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1805480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1806fcf5ef2aSThomas Huth 
1807480d29a8SRichard Henderson     static const struct {
1808480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1809480d29a8SRichard Henderson     } i32s[] = {
1810480d29a8SRichard Henderson         R(0),  R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1811480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1812480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1813480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1814480d29a8SRichard Henderson 
1815480d29a8SRichard Henderson         SP(pc),
1816480d29a8SRichard Henderson         SP(msr),
18171074c0fbSRichard Henderson         SP(msr_c),
1818480d29a8SRichard Henderson         SP(imm),
1819480d29a8SRichard Henderson         SP(iflags),
1820480d29a8SRichard Henderson         SP(btaken),
1821480d29a8SRichard Henderson         SP(btarget),
1822480d29a8SRichard Henderson         SP(res_val),
1823480d29a8SRichard Henderson     };
1824480d29a8SRichard Henderson 
1825480d29a8SRichard Henderson #undef R
1826480d29a8SRichard Henderson #undef SP
1827480d29a8SRichard Henderson 
1828480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1829480d29a8SRichard Henderson         *i32s[i].var =
1830480d29a8SRichard Henderson           tcg_global_mem_new_i32(cpu_env, i32s[i].ofs, i32s[i].name);
1831fcf5ef2aSThomas Huth     }
183276e8187dSRichard Henderson 
1833480d29a8SRichard Henderson     cpu_res_addr =
1834480d29a8SRichard Henderson         tcg_global_mem_new(cpu_env, offsetof(CPUMBState, res_addr), "res_addr");
1835fcf5ef2aSThomas Huth }
1836fcf5ef2aSThomas Huth 
1837fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1838fcf5ef2aSThomas Huth                           target_ulong *data)
1839fcf5ef2aSThomas Huth {
184076e8187dSRichard Henderson     env->pc = data[0];
1841fcf5ef2aSThomas Huth }
1842