xref: /openbmc/qemu/target/microblaze/translate.c (revision 372122e3e7fe1134dbd232aa4f9f56ffef84143a)
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 {
70d4705ae0SRichard Henderson     DisasContextBase base;
71fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
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 
85fcf5ef2aSThomas Huth #define JMP_NOJMP     0
86fcf5ef2aSThomas Huth #define JMP_DIRECT    1
87fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
88fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
89fcf5ef2aSThomas Huth     unsigned int jmp;
90fcf5ef2aSThomas Huth     uint32_t jmp_pc;
91fcf5ef2aSThomas Huth 
92fcf5ef2aSThomas Huth     int abort_at_next_insn;
93fcf5ef2aSThomas Huth } DisasContext;
94fcf5ef2aSThomas Huth 
95fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
96fcf5ef2aSThomas Huth {
97fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
98fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
999b158558SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags);
100fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
101fcf5ef2aSThomas Huth     }
102fcf5ef2aSThomas Huth }
103fcf5ef2aSThomas Huth 
10441ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
105fcf5ef2aSThomas Huth {
106fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
107fcf5ef2aSThomas Huth 
108fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
109fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
110d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
111fcf5ef2aSThomas Huth }
112fcf5ef2aSThomas Huth 
11341ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
11441ba37c4SRichard Henderson {
11541ba37c4SRichard Henderson     t_sync_flags(dc);
116d4705ae0SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
11741ba37c4SRichard Henderson     gen_raise_exception(dc, index);
11841ba37c4SRichard Henderson }
11941ba37c4SRichard Henderson 
12041ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
12141ba37c4SRichard Henderson {
12241ba37c4SRichard Henderson     TCGv_i32 tmp = tcg_const_i32(esr_ec);
12341ba37c4SRichard Henderson     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, esr));
12441ba37c4SRichard Henderson     tcg_temp_free_i32(tmp);
12541ba37c4SRichard Henderson 
12641ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
12741ba37c4SRichard Henderson }
12841ba37c4SRichard Henderson 
129fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
130fcf5ef2aSThomas Huth {
131fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
132d4705ae0SRichard Henderson     return (dc->base.pc_first & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
133fcf5ef2aSThomas Huth #else
134fcf5ef2aSThomas Huth     return true;
135fcf5ef2aSThomas Huth #endif
136fcf5ef2aSThomas Huth }
137fcf5ef2aSThomas Huth 
138fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
139fcf5ef2aSThomas Huth {
140d4705ae0SRichard Henderson     if (dc->base.singlestep_enabled) {
1410b46fa08SRichard Henderson         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1420b46fa08SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
1430b46fa08SRichard Henderson         gen_helper_raise_exception(cpu_env, tmp);
1440b46fa08SRichard Henderson         tcg_temp_free_i32(tmp);
1450b46fa08SRichard Henderson     } else if (use_goto_tb(dc, dest)) {
146fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1470f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
148d4705ae0SRichard Henderson         tcg_gen_exit_tb(dc->base.tb, n);
149fcf5ef2aSThomas Huth     } else {
1500f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
15107ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
152fcf5ef2aSThomas Huth     }
153d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
154fcf5ef2aSThomas Huth }
155fcf5ef2aSThomas Huth 
156bdfc1e88SEdgar E. Iglesias /*
1579ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1589ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1599ba8cd45SEdgar E. Iglesias  */
1609ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1619ba8cd45SEdgar E. Iglesias {
1629ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1635143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
16441ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1659ba8cd45SEdgar E. Iglesias     }
1669ba8cd45SEdgar E. Iglesias     return cond;
1679ba8cd45SEdgar E. Iglesias }
1689ba8cd45SEdgar E. Iglesias 
1699ba8cd45SEdgar E. Iglesias /*
170bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
171bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
172bdfc1e88SEdgar E. Iglesias  */
173bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
174bdfc1e88SEdgar E. Iglesias {
175bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
176bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
177bdfc1e88SEdgar E. Iglesias 
178bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
17941ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
180bdfc1e88SEdgar E. Iglesias     }
181bdfc1e88SEdgar E. Iglesias     return cond_user;
182bdfc1e88SEdgar E. Iglesias }
183bdfc1e88SEdgar E. Iglesias 
184fcf5ef2aSThomas Huth /* True if ALU operand b is a small immediate that may deserve
185fcf5ef2aSThomas Huth    faster treatment.  */
186fcf5ef2aSThomas Huth static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
187fcf5ef2aSThomas Huth {
188fcf5ef2aSThomas Huth     /* Immediate insn without the imm prefix ?  */
189fcf5ef2aSThomas Huth     return dc->type_b && !(dc->tb_flags & IMM_FLAG);
190fcf5ef2aSThomas Huth }
191fcf5ef2aSThomas Huth 
192cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
193fcf5ef2aSThomas Huth {
194fcf5ef2aSThomas Huth     if (dc->type_b) {
195fcf5ef2aSThomas Huth         if (dc->tb_flags & IMM_FLAG)
1969b158558SRichard Henderson             tcg_gen_ori_i32(cpu_imm, cpu_imm, dc->imm);
197fcf5ef2aSThomas Huth         else
1989b158558SRichard Henderson             tcg_gen_movi_i32(cpu_imm, (int32_t)((int16_t)dc->imm));
1999b158558SRichard Henderson         return &cpu_imm;
200fcf5ef2aSThomas Huth     } else
201fcf5ef2aSThomas Huth         return &cpu_R[dc->rb];
202fcf5ef2aSThomas Huth }
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
205fcf5ef2aSThomas Huth {
206fcf5ef2aSThomas Huth     unsigned int k, c;
207cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
208fcf5ef2aSThomas Huth 
209fcf5ef2aSThomas Huth     k = dc->opcode & 4;
210fcf5ef2aSThomas Huth     c = dc->opcode & 2;
211fcf5ef2aSThomas Huth 
212fcf5ef2aSThomas Huth     LOG_DIS("add%s%s%s r%d r%d r%d\n",
213fcf5ef2aSThomas Huth             dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
214fcf5ef2aSThomas Huth             dc->rd, dc->ra, dc->rb);
215fcf5ef2aSThomas Huth 
216fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
217fcf5ef2aSThomas Huth     if (k) {
218fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
219fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
220fcf5ef2aSThomas Huth         if (dc->rd) {
221cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
222fcf5ef2aSThomas Huth 
223fcf5ef2aSThomas Huth             if (c) {
224fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2251074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
226fcf5ef2aSThomas Huth             }
227fcf5ef2aSThomas Huth         }
228fcf5ef2aSThomas Huth         return;
229fcf5ef2aSThomas Huth     }
230fcf5ef2aSThomas Huth 
231fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
232fcf5ef2aSThomas Huth     /* Extract carry.  */
233cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
234fcf5ef2aSThomas Huth     if (c) {
2351074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
236fcf5ef2aSThomas Huth     } else {
237cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
238fcf5ef2aSThomas Huth     }
239fcf5ef2aSThomas Huth 
2401074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
241fcf5ef2aSThomas Huth     if (dc->rd) {
242cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
243cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
244fcf5ef2aSThomas Huth     }
245cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
246fcf5ef2aSThomas Huth }
247fcf5ef2aSThomas Huth 
248fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
249fcf5ef2aSThomas Huth {
250fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
251cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
252fcf5ef2aSThomas Huth 
253fcf5ef2aSThomas Huth     u = dc->imm & 2;
254fcf5ef2aSThomas Huth     k = dc->opcode & 4;
255fcf5ef2aSThomas Huth     c = dc->opcode & 2;
256fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
257fcf5ef2aSThomas Huth 
258fcf5ef2aSThomas Huth     if (cmp) {
259fcf5ef2aSThomas Huth         LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
260fcf5ef2aSThomas Huth         if (dc->rd) {
261fcf5ef2aSThomas Huth             if (u)
262fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
263fcf5ef2aSThomas Huth             else
264fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
265fcf5ef2aSThomas Huth         }
266fcf5ef2aSThomas Huth         return;
267fcf5ef2aSThomas Huth     }
268fcf5ef2aSThomas Huth 
269fcf5ef2aSThomas Huth     LOG_DIS("sub%s%s r%d, r%d r%d\n",
270fcf5ef2aSThomas Huth              k ? "k" : "",  c ? "c" : "", dc->rd, dc->ra, dc->rb);
271fcf5ef2aSThomas Huth 
272fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
273fcf5ef2aSThomas Huth     if (k) {
274fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
275fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
276fcf5ef2aSThomas Huth         if (dc->rd) {
277cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
278fcf5ef2aSThomas Huth 
279fcf5ef2aSThomas Huth             if (c) {
280fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2811074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
282fcf5ef2aSThomas Huth             }
283fcf5ef2aSThomas Huth         }
284fcf5ef2aSThomas Huth         return;
285fcf5ef2aSThomas Huth     }
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
288fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
289cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
290cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
291fcf5ef2aSThomas Huth     if (c) {
2921074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
293fcf5ef2aSThomas Huth     } else {
294cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
295fcf5ef2aSThomas Huth     }
296fcf5ef2aSThomas Huth 
297fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
298cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
299fcf5ef2aSThomas Huth 
3001074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, na, *(dec_alu_op_b(dc)), cf);
301fcf5ef2aSThomas Huth     if (dc->rd) {
302cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
303cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
304fcf5ef2aSThomas Huth     }
305cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
306cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
307fcf5ef2aSThomas Huth }
308fcf5ef2aSThomas Huth 
309fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
310fcf5ef2aSThomas Huth {
311fcf5ef2aSThomas Huth     unsigned int mode;
312fcf5ef2aSThomas Huth 
3139ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
3149ba8cd45SEdgar E. Iglesias         return;
315fcf5ef2aSThomas Huth     }
316fcf5ef2aSThomas Huth 
317fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
318fcf5ef2aSThomas Huth     switch (mode) {
319fcf5ef2aSThomas Huth         case 0:
320fcf5ef2aSThomas Huth             /* pcmpbf.  */
321fcf5ef2aSThomas Huth             LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
322fcf5ef2aSThomas Huth             if (dc->rd)
323fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
324fcf5ef2aSThomas Huth             break;
325fcf5ef2aSThomas Huth         case 2:
326fcf5ef2aSThomas Huth             LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
327fcf5ef2aSThomas Huth             if (dc->rd) {
328cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
329fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
330fcf5ef2aSThomas Huth             }
331fcf5ef2aSThomas Huth             break;
332fcf5ef2aSThomas Huth         case 3:
333fcf5ef2aSThomas Huth             LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
334fcf5ef2aSThomas Huth             if (dc->rd) {
335cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
336fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
337fcf5ef2aSThomas Huth             }
338fcf5ef2aSThomas Huth             break;
339fcf5ef2aSThomas Huth         default:
340fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
341fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
342fcf5ef2aSThomas Huth             break;
343fcf5ef2aSThomas Huth     }
344fcf5ef2aSThomas Huth }
345fcf5ef2aSThomas Huth 
346fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
347fcf5ef2aSThomas Huth {
348fcf5ef2aSThomas Huth     unsigned int not;
349fcf5ef2aSThomas Huth 
350fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
351fcf5ef2aSThomas Huth         dec_pattern(dc);
352fcf5ef2aSThomas Huth         return;
353fcf5ef2aSThomas Huth     }
354fcf5ef2aSThomas Huth 
355fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
356fcf5ef2aSThomas Huth     LOG_DIS("and%s\n", not ? "n" : "");
357fcf5ef2aSThomas Huth 
358fcf5ef2aSThomas Huth     if (!dc->rd)
359fcf5ef2aSThomas Huth         return;
360fcf5ef2aSThomas Huth 
361fcf5ef2aSThomas Huth     if (not) {
362cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
363fcf5ef2aSThomas Huth     } else
364cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
365fcf5ef2aSThomas Huth }
366fcf5ef2aSThomas Huth 
367fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
368fcf5ef2aSThomas Huth {
369fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
370fcf5ef2aSThomas Huth         dec_pattern(dc);
371fcf5ef2aSThomas Huth         return;
372fcf5ef2aSThomas Huth     }
373fcf5ef2aSThomas Huth 
374fcf5ef2aSThomas Huth     LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
375fcf5ef2aSThomas Huth     if (dc->rd)
376cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
377fcf5ef2aSThomas Huth }
378fcf5ef2aSThomas Huth 
379fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
380fcf5ef2aSThomas Huth {
381fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
382fcf5ef2aSThomas Huth         dec_pattern(dc);
383fcf5ef2aSThomas Huth         return;
384fcf5ef2aSThomas Huth     }
385fcf5ef2aSThomas Huth 
386fcf5ef2aSThomas Huth     LOG_DIS("xor r%d\n", dc->rd);
387fcf5ef2aSThomas Huth     if (dc->rd)
388cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
389fcf5ef2aSThomas Huth }
390fcf5ef2aSThomas Huth 
3911074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
392fcf5ef2aSThomas Huth {
3931074c0fbSRichard Henderson     TCGv_i32 t;
3941074c0fbSRichard Henderson 
3951074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
3961074c0fbSRichard Henderson     t = tcg_temp_new_i32();
3971074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
3981074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
3991074c0fbSRichard Henderson     tcg_temp_free_i32(t);
400fcf5ef2aSThomas Huth }
401fcf5ef2aSThomas Huth 
4021074c0fbSRichard Henderson static void msr_write(DisasContext *dc, TCGv_i32 v)
403fcf5ef2aSThomas Huth {
404fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
4051074c0fbSRichard Henderson 
4061074c0fbSRichard Henderson     /* Install MSR_C.  */
4071074c0fbSRichard Henderson     tcg_gen_extract_i32(cpu_msr_c, v, 2, 1);
4081074c0fbSRichard Henderson 
4091074c0fbSRichard Henderson     /* Clear MSR_C and MSR_CC; MSR_PVR is not writable, and is always clear. */
4101074c0fbSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~(MSR_C | MSR_CC | MSR_PVR));
411fcf5ef2aSThomas Huth }
412fcf5ef2aSThomas Huth 
413fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
414fcf5ef2aSThomas Huth {
415fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
416cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4172023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
418f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
419fcf5ef2aSThomas Huth 
4202023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
4212023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
4222023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
423fcf5ef2aSThomas Huth     dc->type_b = 1;
4242023e9a3SEdgar E. Iglesias     if (to) {
425fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
426f0f7e7f7SEdgar E. Iglesias     }
427f0f7e7f7SEdgar E. Iglesias 
428f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
429f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
430f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
431f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
432f0f7e7f7SEdgar E. Iglesias 
433f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4342023e9a3SEdgar E. Iglesias     }
435fcf5ef2aSThomas Huth 
436fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4372023e9a3SEdgar E. Iglesias     if (clrset) {
4382023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
439fcf5ef2aSThomas Huth 
440fcf5ef2aSThomas Huth         LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
441fcf5ef2aSThomas Huth                 dc->rd, dc->imm);
442fcf5ef2aSThomas Huth 
44356837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
444fcf5ef2aSThomas Huth             /* nop??? */
445fcf5ef2aSThomas Huth             return;
446fcf5ef2aSThomas Huth         }
447fcf5ef2aSThomas Huth 
448bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
449fcf5ef2aSThomas Huth             return;
450fcf5ef2aSThomas Huth         }
451fcf5ef2aSThomas Huth 
452fcf5ef2aSThomas Huth         if (dc->rd)
453fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
454fcf5ef2aSThomas Huth 
455cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
456cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
457fcf5ef2aSThomas Huth         msr_read(dc, t0);
458cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
459fcf5ef2aSThomas Huth 
460fcf5ef2aSThomas Huth         if (clr) {
461cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
462cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
463fcf5ef2aSThomas Huth         } else
464cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
465fcf5ef2aSThomas Huth         msr_write(dc, t0);
466cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
467cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
468d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
469d4705ae0SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
470fcf5ef2aSThomas Huth         return;
471fcf5ef2aSThomas Huth     }
472fcf5ef2aSThomas Huth 
473bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
474fcf5ef2aSThomas Huth         return;
475fcf5ef2aSThomas Huth     }
476fcf5ef2aSThomas Huth 
477fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
478fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
479fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
480f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
48105a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
48205a9a651SEdgar E. Iglesias 
483fcf5ef2aSThomas Huth         sr &= 7;
48405a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
485fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
48605a9a651SEdgar E. Iglesias         if (to) {
487f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
48805a9a651SEdgar E. Iglesias         } else {
489f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
49005a9a651SEdgar E. Iglesias         }
49105a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
492f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
493fcf5ef2aSThomas Huth         return;
494fcf5ef2aSThomas Huth     }
495fcf5ef2aSThomas Huth #endif
496fcf5ef2aSThomas Huth 
497fcf5ef2aSThomas Huth     if (to) {
498fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
499fcf5ef2aSThomas Huth         switch (sr) {
500aa28e6d4SRichard Henderson             case SR_PC:
501fcf5ef2aSThomas Huth                 break;
502aa28e6d4SRichard Henderson             case SR_MSR:
503fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
504fcf5ef2aSThomas Huth                 break;
505351527b7SEdgar E. Iglesias             case SR_EAR:
506dbdb77c4SRichard Henderson                 {
507dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
508dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
509dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
510dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
511dbdb77c4SRichard Henderson                 }
512aa28e6d4SRichard Henderson                 break;
513351527b7SEdgar E. Iglesias             case SR_ESR:
51441ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
51541ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
516aa28e6d4SRichard Henderson                 break;
517ab6dd380SEdgar E. Iglesias             case SR_FSR:
51886017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
51986017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
520aa28e6d4SRichard Henderson                 break;
521aa28e6d4SRichard Henderson             case SR_BTR:
522ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
523ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
524aa28e6d4SRichard Henderson                 break;
525aa28e6d4SRichard Henderson             case SR_EDR:
52639db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
52739db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
528fcf5ef2aSThomas Huth                 break;
529fcf5ef2aSThomas Huth             case 0x800:
530cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
531cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
532fcf5ef2aSThomas Huth                 break;
533fcf5ef2aSThomas Huth             case 0x802:
534cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
535cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
536fcf5ef2aSThomas Huth                 break;
537fcf5ef2aSThomas Huth             default:
538fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
539fcf5ef2aSThomas Huth                 break;
540fcf5ef2aSThomas Huth         }
541fcf5ef2aSThomas Huth     } else {
542fcf5ef2aSThomas Huth         LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
543fcf5ef2aSThomas Huth 
544fcf5ef2aSThomas Huth         switch (sr) {
545aa28e6d4SRichard Henderson             case SR_PC:
546d4705ae0SRichard Henderson                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
547fcf5ef2aSThomas Huth                 break;
548aa28e6d4SRichard Henderson             case SR_MSR:
549fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
550fcf5ef2aSThomas Huth                 break;
551351527b7SEdgar E. Iglesias             case SR_EAR:
552dbdb77c4SRichard Henderson                 {
553dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
554dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
555a1b48e3aSEdgar E. Iglesias                     if (extended) {
556dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
557aa28e6d4SRichard Henderson                     } else {
558dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
559dbdb77c4SRichard Henderson                     }
560dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
561a1b48e3aSEdgar E. Iglesias                 }
562aa28e6d4SRichard Henderson                 break;
563351527b7SEdgar E. Iglesias             case SR_ESR:
56441ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
56541ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
566aa28e6d4SRichard Henderson                 break;
567351527b7SEdgar E. Iglesias             case SR_FSR:
56886017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
56986017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
570aa28e6d4SRichard Henderson                 break;
571351527b7SEdgar E. Iglesias             case SR_BTR:
572ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
573ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
574aa28e6d4SRichard Henderson                 break;
5757cdae31dSTong Ho             case SR_EDR:
57639db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
57739db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
578fcf5ef2aSThomas Huth                 break;
579fcf5ef2aSThomas Huth             case 0x800:
580cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
581cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
582fcf5ef2aSThomas Huth                 break;
583fcf5ef2aSThomas Huth             case 0x802:
584cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
585cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
586fcf5ef2aSThomas Huth                 break;
587351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
588fcf5ef2aSThomas Huth                 rn = sr & 0xf;
589cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
590fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
591fcf5ef2aSThomas Huth                 break;
592fcf5ef2aSThomas Huth             default:
593fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
594fcf5ef2aSThomas Huth                 break;
595fcf5ef2aSThomas Huth         }
596fcf5ef2aSThomas Huth     }
597fcf5ef2aSThomas Huth 
598fcf5ef2aSThomas Huth     if (dc->rd == 0) {
599cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
600fcf5ef2aSThomas Huth     }
601fcf5ef2aSThomas Huth }
602fcf5ef2aSThomas Huth 
603fcf5ef2aSThomas Huth /* Multiplier unit.  */
604fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
605fcf5ef2aSThomas Huth {
606cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
607fcf5ef2aSThomas Huth     unsigned int subcode;
608fcf5ef2aSThomas Huth 
6099ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
610fcf5ef2aSThomas Huth         return;
611fcf5ef2aSThomas Huth     }
612fcf5ef2aSThomas Huth 
613fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
614fcf5ef2aSThomas Huth 
615fcf5ef2aSThomas Huth     if (dc->type_b) {
616fcf5ef2aSThomas Huth         LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
617cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
618fcf5ef2aSThomas Huth         return;
619fcf5ef2aSThomas Huth     }
620fcf5ef2aSThomas Huth 
621fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6229b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
623fcf5ef2aSThomas Huth         /* nop??? */
624fcf5ef2aSThomas Huth     }
625fcf5ef2aSThomas Huth 
626cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
627fcf5ef2aSThomas Huth     switch (subcode) {
628fcf5ef2aSThomas Huth         case 0:
629fcf5ef2aSThomas Huth             LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
630cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
631fcf5ef2aSThomas Huth             break;
632fcf5ef2aSThomas Huth         case 1:
633fcf5ef2aSThomas Huth             LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
634cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
635cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
636fcf5ef2aSThomas Huth             break;
637fcf5ef2aSThomas Huth         case 2:
638fcf5ef2aSThomas Huth             LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
639cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
640cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
641fcf5ef2aSThomas Huth             break;
642fcf5ef2aSThomas Huth         case 3:
643fcf5ef2aSThomas Huth             LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
644cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
645fcf5ef2aSThomas Huth             break;
646fcf5ef2aSThomas Huth         default:
647fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
648fcf5ef2aSThomas Huth             break;
649fcf5ef2aSThomas Huth     }
650cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
651fcf5ef2aSThomas Huth }
652fcf5ef2aSThomas Huth 
653fcf5ef2aSThomas Huth /* Div unit.  */
654fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
655fcf5ef2aSThomas Huth {
656fcf5ef2aSThomas Huth     unsigned int u;
657fcf5ef2aSThomas Huth 
658fcf5ef2aSThomas Huth     u = dc->imm & 2;
659fcf5ef2aSThomas Huth     LOG_DIS("div\n");
660fcf5ef2aSThomas Huth 
6619ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
6629ba8cd45SEdgar E. Iglesias         return;
663fcf5ef2aSThomas Huth     }
664fcf5ef2aSThomas Huth 
665fcf5ef2aSThomas Huth     if (u)
666fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
667fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
668fcf5ef2aSThomas Huth     else
669fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
670fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
671fcf5ef2aSThomas Huth     if (!dc->rd)
672cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
673fcf5ef2aSThomas Huth }
674fcf5ef2aSThomas Huth 
675fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
676fcf5ef2aSThomas Huth {
677cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
678faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
679d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
680fcf5ef2aSThomas Huth 
6819ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
682fcf5ef2aSThomas Huth         return;
683fcf5ef2aSThomas Huth     }
684fcf5ef2aSThomas Huth 
685faa48d74SEdgar E. Iglesias     if (dc->type_b) {
686faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
687d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
688faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
689faa48d74SEdgar E. Iglesias     }
690e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
691e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
692faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
693faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
694fcf5ef2aSThomas Huth 
695faa48d74SEdgar E. Iglesias     LOG_DIS("bs%s%s%s r%d r%d r%d\n",
696faa48d74SEdgar E. Iglesias             e ? "e" : "",
697fcf5ef2aSThomas Huth             s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
698fcf5ef2aSThomas Huth 
699faa48d74SEdgar E. Iglesias     if (e) {
700faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
701faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
702faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
703faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
704faa48d74SEdgar E. Iglesias         } else {
705faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
706faa48d74SEdgar E. Iglesias         }
707d09b2585SEdgar E. Iglesias     } else if (i) {
708d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
709d09b2585SEdgar E. Iglesias 
710d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
711d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
712d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
713d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
714d09b2585SEdgar E. Iglesias         } else {
715d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
716d09b2585SEdgar E. Iglesias                                 imm_s, width);
717d09b2585SEdgar E. Iglesias         }
718faa48d74SEdgar E. Iglesias     } else {
719cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
720fcf5ef2aSThomas Huth 
721cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
722cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
723fcf5ef2aSThomas Huth 
7242acf6d53SEdgar E. Iglesias         if (s) {
725cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7262acf6d53SEdgar E. Iglesias         } else {
7272acf6d53SEdgar E. Iglesias             if (t) {
728cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7292acf6d53SEdgar E. Iglesias             } else {
730cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
731fcf5ef2aSThomas Huth             }
732fcf5ef2aSThomas Huth         }
733cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7342acf6d53SEdgar E. Iglesias     }
735faa48d74SEdgar E. Iglesias }
736fcf5ef2aSThomas Huth 
737fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
738fcf5ef2aSThomas Huth {
739fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
740cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
741fcf5ef2aSThomas Huth     unsigned int op;
742fcf5ef2aSThomas Huth 
743fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
744fcf5ef2aSThomas Huth     switch (op) {
745fcf5ef2aSThomas Huth         case 0x21:
746fcf5ef2aSThomas Huth             /* src.  */
747cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
748fcf5ef2aSThomas Huth 
749fcf5ef2aSThomas Huth             LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
7501074c0fbSRichard Henderson             tcg_gen_shli_i32(t0, cpu_msr_c, 31);
7511074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
752fcf5ef2aSThomas Huth             if (dc->rd) {
753cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
754cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
755fcf5ef2aSThomas Huth             }
756cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
757fcf5ef2aSThomas Huth             break;
758fcf5ef2aSThomas Huth 
759fcf5ef2aSThomas Huth         case 0x1:
760fcf5ef2aSThomas Huth         case 0x41:
761fcf5ef2aSThomas Huth             /* srl.  */
762fcf5ef2aSThomas Huth             LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
763fcf5ef2aSThomas Huth 
7641074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
765fcf5ef2aSThomas Huth             if (dc->rd) {
766fcf5ef2aSThomas Huth                 if (op == 0x41)
767cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
768fcf5ef2aSThomas Huth                 else
769cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
770fcf5ef2aSThomas Huth             }
771fcf5ef2aSThomas Huth             break;
772fcf5ef2aSThomas Huth         case 0x60:
773fcf5ef2aSThomas Huth             LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
774fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
775fcf5ef2aSThomas Huth             break;
776fcf5ef2aSThomas Huth         case 0x61:
777fcf5ef2aSThomas Huth             LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
778fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
779fcf5ef2aSThomas Huth             break;
780fcf5ef2aSThomas Huth         case 0x64:
781fcf5ef2aSThomas Huth         case 0x66:
782fcf5ef2aSThomas Huth         case 0x74:
783fcf5ef2aSThomas Huth         case 0x76:
784fcf5ef2aSThomas Huth             /* wdc.  */
785fcf5ef2aSThomas Huth             LOG_DIS("wdc r%d\n", dc->ra);
786bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
787fcf5ef2aSThomas Huth             break;
788fcf5ef2aSThomas Huth         case 0x68:
789fcf5ef2aSThomas Huth             /* wic.  */
790fcf5ef2aSThomas Huth             LOG_DIS("wic r%d\n", dc->ra);
791bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
792fcf5ef2aSThomas Huth             break;
793fcf5ef2aSThomas Huth         case 0xe0:
7949ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
7959ba8cd45SEdgar E. Iglesias                 return;
796fcf5ef2aSThomas Huth             }
7978fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
7985318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
799fcf5ef2aSThomas Huth             }
800fcf5ef2aSThomas Huth             break;
801fcf5ef2aSThomas Huth         case 0x1e0:
802fcf5ef2aSThomas Huth             /* swapb */
803fcf5ef2aSThomas Huth             LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
804fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
805fcf5ef2aSThomas Huth             break;
806fcf5ef2aSThomas Huth         case 0x1e2:
807fcf5ef2aSThomas Huth             /*swaph */
808fcf5ef2aSThomas Huth             LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
809fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
810fcf5ef2aSThomas Huth             break;
811fcf5ef2aSThomas Huth         default:
812fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
813d4705ae0SRichard Henderson                       (uint32_t)dc->base.pc_next, op, dc->rd, dc->ra, dc->rb);
814fcf5ef2aSThomas Huth             break;
815fcf5ef2aSThomas Huth     }
816fcf5ef2aSThomas Huth }
817fcf5ef2aSThomas Huth 
818fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
819fcf5ef2aSThomas Huth {
820fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
821fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
8229b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
823fcf5ef2aSThomas Huth         }
824fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
8250f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
826fcf5ef2aSThomas Huth     }
827fcf5ef2aSThomas Huth }
828fcf5ef2aSThomas Huth 
829fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
830fcf5ef2aSThomas Huth {
831fcf5ef2aSThomas Huth     LOG_DIS("imm %x\n", dc->imm << 16);
8329b158558SRichard Henderson     tcg_gen_movi_i32(cpu_imm, (dc->imm << 16));
833fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
834fcf5ef2aSThomas Huth     dc->clear_imm = 0;
835fcf5ef2aSThomas Huth }
836fcf5ef2aSThomas Huth 
837d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
838fcf5ef2aSThomas Huth {
8390e9033c8SEdgar E. Iglesias     bool extimm = dc->tb_flags & IMM_FLAG;
8400e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8410e9033c8SEdgar E. Iglesias     bool stackprot = false;
842403322eaSEdgar E. Iglesias     TCGv_i32 t32;
843fcf5ef2aSThomas Huth 
844fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
845fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8460e9033c8SEdgar E. Iglesias         stackprot = true;
847fcf5ef2aSThomas Huth     }
848fcf5ef2aSThomas Huth 
849fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
850fcf5ef2aSThomas Huth     if (!dc->type_b) {
851d248e1beSEdgar E. Iglesias         if (ea) {
852d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
853d248e1beSEdgar E. Iglesias 
854d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
855d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
856d248e1beSEdgar E. Iglesias                 return;
857d248e1beSEdgar E. Iglesias             }
858d248e1beSEdgar E. Iglesias 
859d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
860d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
861d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
862d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
863d248e1beSEdgar E. Iglesias             }
864d248e1beSEdgar E. Iglesias             return;
865d248e1beSEdgar E. Iglesias         }
866d248e1beSEdgar E. Iglesias 
8670dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
868fcf5ef2aSThomas Huth         if (dc->ra == 0) {
869403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8700dc4af5cSEdgar E. Iglesias             return;
871fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
872403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
8730dc4af5cSEdgar E. Iglesias             return;
874fcf5ef2aSThomas Huth         }
875fcf5ef2aSThomas Huth 
876fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
8770e9033c8SEdgar E. Iglesias             stackprot = true;
878fcf5ef2aSThomas Huth         }
879fcf5ef2aSThomas Huth 
880403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
881403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
882403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
883403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
884fcf5ef2aSThomas Huth 
885fcf5ef2aSThomas Huth         if (stackprot) {
8860a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
887fcf5ef2aSThomas Huth         }
8880dc4af5cSEdgar E. Iglesias         return;
889fcf5ef2aSThomas Huth     }
890fcf5ef2aSThomas Huth     /* Immediate.  */
891403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
892fcf5ef2aSThomas Huth     if (!extimm) {
893f7a66e3aSEdgar E. Iglesias         tcg_gen_addi_i32(t32, cpu_R[dc->ra], (int16_t)dc->imm);
894403322eaSEdgar E. Iglesias     } else {
895403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
896403322eaSEdgar E. Iglesias     }
897403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
898403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
899fcf5ef2aSThomas Huth 
900fcf5ef2aSThomas Huth     if (stackprot) {
9010a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
902fcf5ef2aSThomas Huth     }
9030dc4af5cSEdgar E. Iglesias     return;
904fcf5ef2aSThomas Huth }
905fcf5ef2aSThomas Huth 
906fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
907fcf5ef2aSThomas Huth {
908403322eaSEdgar E. Iglesias     TCGv_i32 v;
909403322eaSEdgar E. Iglesias     TCGv addr;
9108534063aSEdgar E. Iglesias     unsigned int size;
911d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
912d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
91314776ab5STony Nguyen     MemOp mop;
914fcf5ef2aSThomas Huth 
915fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
916fcf5ef2aSThomas Huth     size = 1 << mop;
917fcf5ef2aSThomas Huth     if (!dc->type_b) {
918d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9198534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9208534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
921fcf5ef2aSThomas Huth     }
922fcf5ef2aSThomas Huth     mop |= MO_TE;
923fcf5ef2aSThomas Huth     if (rev) {
924fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
925fcf5ef2aSThomas Huth     }
926fcf5ef2aSThomas Huth 
9279ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
928fcf5ef2aSThomas Huth         return;
929fcf5ef2aSThomas Huth     }
930fcf5ef2aSThomas Huth 
931d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
932d248e1beSEdgar E. Iglesias         return;
933d248e1beSEdgar E. Iglesias     }
934d248e1beSEdgar E. Iglesias 
935d248e1beSEdgar E. Iglesias     LOG_DIS("l%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
936d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
937d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
938fcf5ef2aSThomas Huth 
939fcf5ef2aSThomas Huth     t_sync_flags(dc);
940403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
941d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
942d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
943d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
944fcf5ef2aSThomas Huth 
945fcf5ef2aSThomas Huth     /*
946fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
947fcf5ef2aSThomas Huth      *
948fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
949fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
950fcf5ef2aSThomas Huth      */
951fcf5ef2aSThomas Huth     if (rev && size != 4) {
952fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
953fcf5ef2aSThomas Huth         switch (size) {
954fcf5ef2aSThomas Huth             case 1:
955fcf5ef2aSThomas Huth             {
956a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
957fcf5ef2aSThomas Huth                 break;
958fcf5ef2aSThomas Huth             }
959fcf5ef2aSThomas Huth 
960fcf5ef2aSThomas Huth             case 2:
961fcf5ef2aSThomas Huth                 /* 00 -> 10
962fcf5ef2aSThomas Huth                    10 -> 00.  */
963403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
964fcf5ef2aSThomas Huth                 break;
965fcf5ef2aSThomas Huth             default:
966fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
967fcf5ef2aSThomas Huth                 break;
968fcf5ef2aSThomas Huth         }
969fcf5ef2aSThomas Huth     }
970fcf5ef2aSThomas Huth 
971fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
972fcf5ef2aSThomas Huth     if (ex) {
973403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
974fcf5ef2aSThomas Huth     }
975fcf5ef2aSThomas Huth 
976fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
977fcf5ef2aSThomas Huth     sync_jmpstate(dc);
978fcf5ef2aSThomas Huth 
979fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
980fcf5ef2aSThomas Huth     /*
981fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
982fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
983fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
984fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
985fcf5ef2aSThomas Huth      */
986cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
987d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
988fcf5ef2aSThomas Huth 
9891507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
990a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
991a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
992a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
993a6338015SEdgar E. Iglesias 
994d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
995a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
996a6338015SEdgar E. Iglesias 
997a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
998a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
999a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1000fcf5ef2aSThomas Huth     }
1001fcf5ef2aSThomas Huth 
1002fcf5ef2aSThomas Huth     if (ex) {
10039b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
10049b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
1005fcf5ef2aSThomas Huth     }
1006fcf5ef2aSThomas Huth     if (dc->rd) {
1007cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1008fcf5ef2aSThomas Huth     }
1009cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1010fcf5ef2aSThomas Huth 
1011fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1012fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
10131074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1014fcf5ef2aSThomas Huth     }
1015fcf5ef2aSThomas Huth 
1016403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1017fcf5ef2aSThomas Huth }
1018fcf5ef2aSThomas Huth 
1019fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1020fcf5ef2aSThomas Huth {
1021403322eaSEdgar E. Iglesias     TCGv addr;
1022fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1023b51b3d43SEdgar E. Iglesias     unsigned int size;
1024d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1025d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
102614776ab5STony Nguyen     MemOp mop;
1027fcf5ef2aSThomas Huth 
1028fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1029fcf5ef2aSThomas Huth     size = 1 << mop;
1030fcf5ef2aSThomas Huth     if (!dc->type_b) {
1031d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1032b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1033b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1034fcf5ef2aSThomas Huth     }
1035fcf5ef2aSThomas Huth     mop |= MO_TE;
1036fcf5ef2aSThomas Huth     if (rev) {
1037fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1038fcf5ef2aSThomas Huth     }
1039fcf5ef2aSThomas Huth 
10409ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1041fcf5ef2aSThomas Huth         return;
1042fcf5ef2aSThomas Huth     }
1043fcf5ef2aSThomas Huth 
1044d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1045d248e1beSEdgar E. Iglesias 
1046d248e1beSEdgar E. Iglesias     LOG_DIS("s%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1047d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
1048d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
1049fcf5ef2aSThomas Huth     t_sync_flags(dc);
1050fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1051fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10520dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1053403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1054d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1055d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1056d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1057fcf5ef2aSThomas Huth 
1058fcf5ef2aSThomas Huth     if (ex) { /* swx */
1059cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1060fcf5ef2aSThomas Huth 
1061fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1062403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1063fcf5ef2aSThomas Huth 
10641074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 1);
1065fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
10669b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1067fcf5ef2aSThomas Huth 
1068071cdc67SEdgar E. Iglesias         /*
1069071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1070071cdc67SEdgar E. Iglesias          * the reserved location.
1071071cdc67SEdgar E. Iglesias          */
1072cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1073071cdc67SEdgar E. Iglesias 
10749b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1075071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1076071cdc67SEdgar E. Iglesias                                    mop);
1077071cdc67SEdgar E. Iglesias 
10789b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
10791074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1080cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1081fcf5ef2aSThomas Huth     }
1082fcf5ef2aSThomas Huth 
1083fcf5ef2aSThomas Huth     if (rev && size != 4) {
1084fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1085fcf5ef2aSThomas Huth         switch (size) {
1086fcf5ef2aSThomas Huth             case 1:
1087fcf5ef2aSThomas Huth             {
1088a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1089fcf5ef2aSThomas Huth                 break;
1090fcf5ef2aSThomas Huth             }
1091fcf5ef2aSThomas Huth 
1092fcf5ef2aSThomas Huth             case 2:
1093fcf5ef2aSThomas Huth                 /* 00 -> 10
1094fcf5ef2aSThomas Huth                    10 -> 00.  */
1095fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1096403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1097fcf5ef2aSThomas Huth                 break;
1098fcf5ef2aSThomas Huth             default:
1099fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1100fcf5ef2aSThomas Huth                 break;
1101fcf5ef2aSThomas Huth         }
1102fcf5ef2aSThomas Huth     }
1103071cdc67SEdgar E. Iglesias 
1104071cdc67SEdgar E. Iglesias     if (!ex) {
1105d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1106071cdc67SEdgar E. Iglesias     }
1107fcf5ef2aSThomas Huth 
1108fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11091507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1110a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1111a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1112a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1113a6338015SEdgar E. Iglesias 
1114d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1115fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1116fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1117fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1118fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1119fcf5ef2aSThomas Huth          *        access.
1120fcf5ef2aSThomas Huth          */
1121a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1122a6338015SEdgar E. Iglesias 
1123a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1124a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1125a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1126fcf5ef2aSThomas Huth     }
1127fcf5ef2aSThomas Huth 
1128fcf5ef2aSThomas Huth     if (ex) {
1129fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1130fcf5ef2aSThomas Huth     }
1131fcf5ef2aSThomas Huth 
1132403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1133fcf5ef2aSThomas Huth }
1134fcf5ef2aSThomas Huth 
1135fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11369e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1137fcf5ef2aSThomas Huth {
1138d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1139d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1140d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1141d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1142d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1143d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1144d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1145d89b86e9SEdgar E. Iglesias     };
1146d89b86e9SEdgar E. Iglesias 
1147fcf5ef2aSThomas Huth     switch (cc) {
1148fcf5ef2aSThomas Huth     case CC_EQ:
1149fcf5ef2aSThomas Huth     case CC_NE:
1150fcf5ef2aSThomas Huth     case CC_LT:
1151fcf5ef2aSThomas Huth     case CC_LE:
1152fcf5ef2aSThomas Huth     case CC_GE:
1153fcf5ef2aSThomas Huth     case CC_GT:
11549e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1155fcf5ef2aSThomas Huth         break;
1156fcf5ef2aSThomas Huth     default:
1157fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1158fcf5ef2aSThomas Huth         break;
1159fcf5ef2aSThomas Huth     }
1160fcf5ef2aSThomas Huth }
1161fcf5ef2aSThomas Huth 
11620f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1163fcf5ef2aSThomas Huth {
11640f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1165e956caf2SEdgar E. Iglesias 
11660f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
11679b158558SRichard Henderson                         cpu_btaken, zero,
1168e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1169e956caf2SEdgar E. Iglesias 
11700f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1171fcf5ef2aSThomas Huth }
1172fcf5ef2aSThomas Huth 
1173f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1174f91c60f0SEdgar E. Iglesias {
1175f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1176f91c60f0SEdgar E. Iglesias 
1177f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1178f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1179f91c60f0SEdgar E. Iglesias 
1180f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1181f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1182f91c60f0SEdgar E. Iglesias }
1183f91c60f0SEdgar E. Iglesias 
1184fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1185fcf5ef2aSThomas Huth {
1186fcf5ef2aSThomas Huth     unsigned int cc;
1187fcf5ef2aSThomas Huth     unsigned int dslot;
1188fcf5ef2aSThomas Huth 
1189fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1190fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1191fcf5ef2aSThomas Huth     LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1192fcf5ef2aSThomas Huth 
1193fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1194fcf5ef2aSThomas Huth     if (dslot) {
1195f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1196fcf5ef2aSThomas Huth     }
1197fcf5ef2aSThomas Huth 
1198fcf5ef2aSThomas Huth     if (dec_alu_op_b_is_small_imm(dc)) {
1199fcf5ef2aSThomas Huth         int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend.  */
1200fcf5ef2aSThomas Huth 
1201d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->base.pc_next + offset);
1202fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1203d4705ae0SRichard Henderson         dc->jmp_pc = dc->base.pc_next + offset;
1204fcf5ef2aSThomas Huth     } else {
1205fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1206d4705ae0SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->base.pc_next);
1207fcf5ef2aSThomas Huth     }
12089b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1209fcf5ef2aSThomas Huth }
1210fcf5ef2aSThomas Huth 
1211fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1212fcf5ef2aSThomas Huth {
1213fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1214fcf5ef2aSThomas Huth 
1215fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1216fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1217fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1218fcf5ef2aSThomas Huth 
1219fcf5ef2aSThomas Huth     /* Memory barrier.  */
1220fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1221fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1222badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1223badcbf9dSEdgar E. Iglesias 
12246f3c458bSEdgar E. Iglesias         LOG_DIS("mbar %d\n", mbar_imm);
12256f3c458bSEdgar E. Iglesias 
12263f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12273f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12283f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12293f172744SEdgar E. Iglesias         }
12303f172744SEdgar E. Iglesias 
1231fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1232badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
123341ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1234fcf5ef2aSThomas Huth 
1235fcf5ef2aSThomas Huth             LOG_DIS("sleep\n");
1236fcf5ef2aSThomas Huth 
1237b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1238b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1239b4919e7dSEdgar E. Iglesias                 return;
1240b4919e7dSEdgar E. Iglesias             }
1241b4919e7dSEdgar E. Iglesias 
1242fcf5ef2aSThomas Huth             t_sync_flags(dc);
124341ba37c4SRichard Henderson 
124441ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1245fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1246fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1247fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1248fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
124941ba37c4SRichard Henderson 
1250d4705ae0SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
125141ba37c4SRichard Henderson 
125241ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1253fcf5ef2aSThomas Huth             return;
1254fcf5ef2aSThomas Huth         }
1255fcf5ef2aSThomas Huth         /* Break the TB.  */
1256fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1257fcf5ef2aSThomas Huth         return;
1258fcf5ef2aSThomas Huth     }
1259fcf5ef2aSThomas Huth 
1260fcf5ef2aSThomas Huth     LOG_DIS("br%s%s%s%s imm=%x\n",
1261fcf5ef2aSThomas Huth              abs ? "a" : "", link ? "l" : "",
1262fcf5ef2aSThomas Huth              dc->type_b ? "i" : "", dslot ? "d" : "",
1263fcf5ef2aSThomas Huth              dc->imm);
1264fcf5ef2aSThomas Huth 
1265fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1266fcf5ef2aSThomas Huth     if (dslot) {
1267f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1268fcf5ef2aSThomas Huth     }
1269fcf5ef2aSThomas Huth     if (link && dc->rd)
1270d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
1271fcf5ef2aSThomas Huth 
1272fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1273fcf5ef2aSThomas Huth     if (abs) {
12749b158558SRichard Henderson         tcg_gen_movi_i32(cpu_btaken, 1);
12750f96e96bSRichard Henderson         tcg_gen_mov_i32(cpu_btarget, *(dec_alu_op_b(dc)));
1276fcf5ef2aSThomas Huth         if (link && !dslot) {
127741ba37c4SRichard Henderson             if (!(dc->tb_flags & IMM_FLAG) &&
127841ba37c4SRichard Henderson                 (dc->imm == 8 || dc->imm == 0x18)) {
127941ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
128041ba37c4SRichard Henderson             }
1281fcf5ef2aSThomas Huth             if (dc->imm == 0) {
1282bdfc1e88SEdgar E. Iglesias                 if (trap_userspace(dc, true)) {
1283fcf5ef2aSThomas Huth                     return;
1284fcf5ef2aSThomas Huth                 }
128541ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_DEBUG);
1286fcf5ef2aSThomas Huth             }
1287fcf5ef2aSThomas Huth         }
1288fcf5ef2aSThomas Huth     } else {
1289fcf5ef2aSThomas Huth         if (dec_alu_op_b_is_small_imm(dc)) {
1290fcf5ef2aSThomas Huth             dc->jmp = JMP_DIRECT;
1291d4705ae0SRichard Henderson             dc->jmp_pc = dc->base.pc_next + (int32_t)((int16_t)dc->imm);
1292fcf5ef2aSThomas Huth         } else {
12939b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
1294d4705ae0SRichard Henderson             tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->base.pc_next);
1295fcf5ef2aSThomas Huth         }
1296fcf5ef2aSThomas Huth     }
1297fcf5ef2aSThomas Huth }
1298fcf5ef2aSThomas Huth 
1299fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1300fcf5ef2aSThomas Huth {
1301cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1302cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1303cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13043e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13050a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13060a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1307cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1308fcf5ef2aSThomas Huth 
1309cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1310cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1311fcf5ef2aSThomas Huth     msr_write(dc, t1);
1312cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1313cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1314fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1315fcf5ef2aSThomas Huth }
1316fcf5ef2aSThomas Huth 
1317fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1318fcf5ef2aSThomas Huth {
1319cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1320cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1321cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13223e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13230a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1324cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1325cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1326fcf5ef2aSThomas Huth 
1327cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1328cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1329fcf5ef2aSThomas Huth     msr_write(dc, t1);
1330cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1331cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1332fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1333fcf5ef2aSThomas Huth }
1334fcf5ef2aSThomas Huth 
1335fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1336fcf5ef2aSThomas Huth {
1337cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1338cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1339cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1340fcf5ef2aSThomas Huth 
13413e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13420a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1343cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1344cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1345cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1346fcf5ef2aSThomas Huth 
1347cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1348cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1349fcf5ef2aSThomas Huth     msr_write(dc, t1);
1350cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1351cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1352fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1353fcf5ef2aSThomas Huth }
1354fcf5ef2aSThomas Huth 
1355fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1356fcf5ef2aSThomas Huth {
1357fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1358fcf5ef2aSThomas Huth 
1359fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1360fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1361fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1362fcf5ef2aSThomas Huth 
1363bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1364bdfc1e88SEdgar E. Iglesias         return;
1365bdfc1e88SEdgar E. Iglesias     }
1366bdfc1e88SEdgar E. Iglesias 
1367f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1368fcf5ef2aSThomas Huth 
1369fcf5ef2aSThomas Huth     if (i_bit) {
1370fcf5ef2aSThomas Huth         LOG_DIS("rtid ir=%x\n", dc->ir);
1371fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1372fcf5ef2aSThomas Huth     } else if (b_bit) {
1373fcf5ef2aSThomas Huth         LOG_DIS("rtbd ir=%x\n", dc->ir);
1374fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1375fcf5ef2aSThomas Huth     } else if (e_bit) {
1376fcf5ef2aSThomas Huth         LOG_DIS("rted ir=%x\n", dc->ir);
1377fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
1378fcf5ef2aSThomas Huth     } else
1379fcf5ef2aSThomas Huth         LOG_DIS("rts ir=%x\n", dc->ir);
1380fcf5ef2aSThomas Huth 
1381fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
13829b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
13830f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1384fcf5ef2aSThomas Huth }
1385fcf5ef2aSThomas Huth 
1386fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1387fcf5ef2aSThomas Huth {
1388fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
138941ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1390fcf5ef2aSThomas Huth     }
13912016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1392fcf5ef2aSThomas Huth }
1393fcf5ef2aSThomas Huth 
1394fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1395fcf5ef2aSThomas Huth {
1396fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1397fcf5ef2aSThomas Huth 
13989ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1399fcf5ef2aSThomas Huth         return;
1400fcf5ef2aSThomas Huth     }
1401fcf5ef2aSThomas Huth 
1402fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1403fcf5ef2aSThomas Huth 
1404fcf5ef2aSThomas Huth     switch (fpu_insn) {
1405fcf5ef2aSThomas Huth         case 0:
1406fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1407fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1408fcf5ef2aSThomas Huth             break;
1409fcf5ef2aSThomas Huth 
1410fcf5ef2aSThomas Huth         case 1:
1411fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1412fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1413fcf5ef2aSThomas Huth             break;
1414fcf5ef2aSThomas Huth 
1415fcf5ef2aSThomas Huth         case 2:
1416fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1417fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1418fcf5ef2aSThomas Huth             break;
1419fcf5ef2aSThomas Huth 
1420fcf5ef2aSThomas Huth         case 3:
1421fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1422fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1423fcf5ef2aSThomas Huth             break;
1424fcf5ef2aSThomas Huth 
1425fcf5ef2aSThomas Huth         case 4:
1426fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1427fcf5ef2aSThomas Huth                 case 0:
1428fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1429fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1430fcf5ef2aSThomas Huth                     break;
1431fcf5ef2aSThomas Huth                 case 1:
1432fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1433fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1434fcf5ef2aSThomas Huth                     break;
1435fcf5ef2aSThomas Huth                 case 2:
1436fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1437fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1438fcf5ef2aSThomas Huth                     break;
1439fcf5ef2aSThomas Huth                 case 3:
1440fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1441fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1442fcf5ef2aSThomas Huth                     break;
1443fcf5ef2aSThomas Huth                 case 4:
1444fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1445fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1446fcf5ef2aSThomas Huth                     break;
1447fcf5ef2aSThomas Huth                 case 5:
1448fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1449fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1450fcf5ef2aSThomas Huth                     break;
1451fcf5ef2aSThomas Huth                 case 6:
1452fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1453fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1454fcf5ef2aSThomas Huth                     break;
1455fcf5ef2aSThomas Huth                 default:
1456fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1457fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1458fcf5ef2aSThomas Huth                                   " opc=%x\n",
1459d4705ae0SRichard Henderson                                   fpu_insn, (uint32_t)dc->base.pc_next,
1460d4705ae0SRichard Henderson                                   dc->opcode);
1461fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1462fcf5ef2aSThomas Huth                     break;
1463fcf5ef2aSThomas Huth             }
1464fcf5ef2aSThomas Huth             break;
1465fcf5ef2aSThomas Huth 
1466fcf5ef2aSThomas Huth         case 5:
1467fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1468fcf5ef2aSThomas Huth                 return;
1469fcf5ef2aSThomas Huth             }
1470fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1471fcf5ef2aSThomas Huth             break;
1472fcf5ef2aSThomas Huth 
1473fcf5ef2aSThomas Huth         case 6:
1474fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1475fcf5ef2aSThomas Huth                 return;
1476fcf5ef2aSThomas Huth             }
1477fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1478fcf5ef2aSThomas Huth             break;
1479fcf5ef2aSThomas Huth 
1480fcf5ef2aSThomas Huth         case 7:
1481fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1482fcf5ef2aSThomas Huth                 return;
1483fcf5ef2aSThomas Huth             }
1484fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1485fcf5ef2aSThomas Huth             break;
1486fcf5ef2aSThomas Huth 
1487fcf5ef2aSThomas Huth         default:
1488fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1489fcf5ef2aSThomas Huth                           " opc=%x\n",
1490d4705ae0SRichard Henderson                           fpu_insn, (uint32_t)dc->base.pc_next, dc->opcode);
1491fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1492fcf5ef2aSThomas Huth             break;
1493fcf5ef2aSThomas Huth     }
1494fcf5ef2aSThomas Huth }
1495fcf5ef2aSThomas Huth 
1496fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1497fcf5ef2aSThomas Huth {
14989ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1499fcf5ef2aSThomas Huth         return;
1500fcf5ef2aSThomas Huth     }
1501d4705ae0SRichard Henderson     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n",
1502d4705ae0SRichard Henderson                   (uint32_t)dc->base.pc_next, dc->opcode);
1503fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1504fcf5ef2aSThomas Huth }
1505fcf5ef2aSThomas Huth 
1506fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1507fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1508fcf5ef2aSThomas Huth {
1509fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1510fcf5ef2aSThomas Huth     int ctrl;
1511fcf5ef2aSThomas Huth 
1512fcf5ef2aSThomas Huth     LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1513fcf5ef2aSThomas Huth             dc->type_b ? "" : "d", dc->imm);
1514fcf5ef2aSThomas Huth 
1515bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1516fcf5ef2aSThomas Huth         return;
1517fcf5ef2aSThomas Huth     }
1518fcf5ef2aSThomas Huth 
1519cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1520fcf5ef2aSThomas Huth     if (dc->type_b) {
1521cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1522fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1523fcf5ef2aSThomas Huth     } else {
1524cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1525fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1526fcf5ef2aSThomas Huth     }
1527fcf5ef2aSThomas Huth 
1528cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1529fcf5ef2aSThomas Huth 
1530fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1531fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1532fcf5ef2aSThomas Huth     } else {
1533fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1534fcf5ef2aSThomas Huth     }
1535cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1536cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1537fcf5ef2aSThomas Huth }
1538fcf5ef2aSThomas Huth 
1539fcf5ef2aSThomas Huth static struct decoder_info {
1540fcf5ef2aSThomas Huth     struct {
1541fcf5ef2aSThomas Huth         uint32_t bits;
1542fcf5ef2aSThomas Huth         uint32_t mask;
1543fcf5ef2aSThomas Huth     };
1544fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1545fcf5ef2aSThomas Huth } decinfo[] = {
1546fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1547fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1548fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1549fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1550fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1551fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1552fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1553fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1554fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1555fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1556fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1557fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1558fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1559fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1560fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1561fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1562fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1563fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1564fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1565fcf5ef2aSThomas Huth };
1566fcf5ef2aSThomas Huth 
1567fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1568fcf5ef2aSThomas Huth {
1569fcf5ef2aSThomas Huth     int i;
1570fcf5ef2aSThomas Huth 
1571fcf5ef2aSThomas Huth     dc->ir = ir;
1572fcf5ef2aSThomas Huth     LOG_DIS("%8.8x\t", dc->ir);
1573fcf5ef2aSThomas Huth 
1574462c2544SEdgar E. Iglesias     if (ir == 0) {
15751ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1576462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1577462c2544SEdgar E. Iglesias         return;
1578462c2544SEdgar E. Iglesias     }
1579fcf5ef2aSThomas Huth 
1580fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1581fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1582fcf5ef2aSThomas Huth 
1583fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1584fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1585fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1586fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1587fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1588fcf5ef2aSThomas Huth 
1589fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1590fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1591fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1592fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1593fcf5ef2aSThomas Huth             break;
1594fcf5ef2aSThomas Huth         }
1595fcf5ef2aSThomas Huth     }
1596fcf5ef2aSThomas Huth }
1597fcf5ef2aSThomas Huth 
1598*372122e3SRichard Henderson static void mb_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1599fcf5ef2aSThomas Huth {
1600*372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1601*372122e3SRichard Henderson     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1602*372122e3SRichard Henderson     int bound;
1603fcf5ef2aSThomas Huth 
1604fcf5ef2aSThomas Huth     dc->cpu = cpu;
1605*372122e3SRichard Henderson     dc->synced_flags = dc->tb_flags = dc->base.tb->flags;
1606fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1607*372122e3SRichard Henderson     dc->jmp = dc->delayed_branch ? JMP_INDIRECT : JMP_NOJMP;
1608fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1609fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1610fcf5ef2aSThomas Huth 
1611*372122e3SRichard Henderson     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1612*372122e3SRichard Henderson     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1613fcf5ef2aSThomas Huth }
1614fcf5ef2aSThomas Huth 
1615*372122e3SRichard Henderson static void mb_tr_tb_start(DisasContextBase *dcb, CPUState *cs)
1616fcf5ef2aSThomas Huth {
1617fcf5ef2aSThomas Huth }
1618fcf5ef2aSThomas Huth 
1619*372122e3SRichard Henderson static void mb_tr_insn_start(DisasContextBase *dcb, CPUState *cs)
1620*372122e3SRichard Henderson {
1621*372122e3SRichard Henderson     tcg_gen_insn_start(dcb->pc_next);
1622*372122e3SRichard Henderson }
1623fcf5ef2aSThomas Huth 
1624*372122e3SRichard Henderson static bool mb_tr_breakpoint_check(DisasContextBase *dcb, CPUState *cs,
1625*372122e3SRichard Henderson                                    const CPUBreakpoint *bp)
1626*372122e3SRichard Henderson {
1627*372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1628*372122e3SRichard Henderson 
1629*372122e3SRichard Henderson     gen_raise_exception_sync(dc, EXCP_DEBUG);
1630*372122e3SRichard Henderson 
1631*372122e3SRichard Henderson     /*
1632*372122e3SRichard Henderson      * The address covered by the breakpoint must be included in
1633*372122e3SRichard Henderson      * [tb->pc, tb->pc + tb->size) in order to for it to be
1634*372122e3SRichard Henderson      * properly cleared -- thus we increment the PC here so that
1635*372122e3SRichard Henderson      * the logic setting tb->size below does the right thing.
1636*372122e3SRichard Henderson      */
1637*372122e3SRichard Henderson     dc->base.pc_next += 4;
1638*372122e3SRichard Henderson     return true;
1639*372122e3SRichard Henderson }
1640*372122e3SRichard Henderson 
1641*372122e3SRichard Henderson static void mb_tr_translate_insn(DisasContextBase *dcb, CPUState *cs)
1642*372122e3SRichard Henderson {
1643*372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1644*372122e3SRichard Henderson     CPUMBState *env = cs->env_ptr;
1645*372122e3SRichard Henderson 
1646*372122e3SRichard Henderson     /* TODO: This should raise an exception, not terminate qemu. */
1647*372122e3SRichard Henderson     if (dc->base.pc_next & 3) {
1648*372122e3SRichard Henderson         cpu_abort(cs, "Microblaze: unaligned PC=%x\n",
1649*372122e3SRichard Henderson                   (uint32_t)dc->base.pc_next);
1650fcf5ef2aSThomas Huth     }
1651fcf5ef2aSThomas Huth 
1652fcf5ef2aSThomas Huth     dc->clear_imm = 1;
1653d4705ae0SRichard Henderson     decode(dc, cpu_ldl_code(env, dc->base.pc_next));
1654*372122e3SRichard Henderson     if (dc->clear_imm) {
1655fcf5ef2aSThomas Huth         dc->tb_flags &= ~IMM_FLAG;
1656*372122e3SRichard Henderson     }
1657d4705ae0SRichard Henderson     dc->base.pc_next += 4;
1658fcf5ef2aSThomas Huth 
1659*372122e3SRichard Henderson     if (dc->delayed_branch && --dc->delayed_branch == 0) {
1660*372122e3SRichard Henderson         if (dc->tb_flags & DRTI_FLAG) {
1661fcf5ef2aSThomas Huth             do_rti(dc);
1662*372122e3SRichard Henderson         }
1663*372122e3SRichard Henderson         if (dc->tb_flags & DRTB_FLAG) {
1664fcf5ef2aSThomas Huth             do_rtb(dc);
1665*372122e3SRichard Henderson         }
1666*372122e3SRichard Henderson         if (dc->tb_flags & DRTE_FLAG) {
1667fcf5ef2aSThomas Huth             do_rte(dc);
1668*372122e3SRichard Henderson         }
1669fcf5ef2aSThomas Huth         /* Clear the delay slot flag.  */
1670fcf5ef2aSThomas Huth         dc->tb_flags &= ~D_FLAG;
1671*372122e3SRichard Henderson         dc->base.is_jmp = DISAS_JUMP;
1672*372122e3SRichard Henderson     }
1673*372122e3SRichard Henderson 
1674*372122e3SRichard Henderson     /* Force an exit if the per-tb cpu state has changed.  */
1675*372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NEXT && dc->cpustate_changed) {
1676*372122e3SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
1677*372122e3SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1678*372122e3SRichard Henderson     }
1679*372122e3SRichard Henderson }
1680*372122e3SRichard Henderson 
1681*372122e3SRichard Henderson static void mb_tr_tb_stop(DisasContextBase *dcb, CPUState *cs)
1682*372122e3SRichard Henderson {
1683*372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1684*372122e3SRichard Henderson 
1685*372122e3SRichard Henderson     assert(!dc->abort_at_next_insn);
1686*372122e3SRichard Henderson 
1687*372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NORETURN) {
1688*372122e3SRichard Henderson         /* We have already exited the TB. */
1689*372122e3SRichard Henderson         return;
1690*372122e3SRichard Henderson     }
1691*372122e3SRichard Henderson 
1692*372122e3SRichard Henderson     t_sync_flags(dc);
1693*372122e3SRichard Henderson     if (dc->tb_flags & D_FLAG) {
1694*372122e3SRichard Henderson         sync_jmpstate(dc);
1695*372122e3SRichard Henderson         dc->jmp = JMP_NOJMP;
1696*372122e3SRichard Henderson     }
1697*372122e3SRichard Henderson 
1698*372122e3SRichard Henderson     switch (dc->base.is_jmp) {
1699*372122e3SRichard Henderson     case DISAS_TOO_MANY:
1700*372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1701*372122e3SRichard Henderson         gen_goto_tb(dc, 0, dc->base.pc_next);
1702*372122e3SRichard Henderson         return;
1703*372122e3SRichard Henderson 
1704*372122e3SRichard Henderson     case DISAS_UPDATE:
1705*372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1706*372122e3SRichard Henderson         if (unlikely(cs->singlestep_enabled)) {
1707*372122e3SRichard Henderson             gen_raise_exception(dc, EXCP_DEBUG);
1708*372122e3SRichard Henderson         } else {
1709*372122e3SRichard Henderson             tcg_gen_exit_tb(NULL, 0);
1710*372122e3SRichard Henderson         }
1711*372122e3SRichard Henderson         return;
1712*372122e3SRichard Henderson 
1713*372122e3SRichard Henderson     case DISAS_JUMP:
1714*372122e3SRichard Henderson         switch (dc->jmp) {
1715*372122e3SRichard Henderson         case JMP_INDIRECT:
1716*372122e3SRichard Henderson             {
1717d4705ae0SRichard Henderson                 TCGv_i32 tmp_pc = tcg_const_i32(dc->base.pc_next);
17180f96e96bSRichard Henderson                 eval_cond_jmp(dc, cpu_btarget, tmp_pc);
17190f96e96bSRichard Henderson                 tcg_temp_free_i32(tmp_pc);
1720*372122e3SRichard Henderson 
1721*372122e3SRichard Henderson                 if (unlikely(cs->singlestep_enabled)) {
1722*372122e3SRichard Henderson                     gen_raise_exception(dc, EXCP_DEBUG);
1723*372122e3SRichard Henderson                 } else {
1724*372122e3SRichard Henderson                     tcg_gen_exit_tb(NULL, 0);
1725*372122e3SRichard Henderson                 }
1726*372122e3SRichard Henderson             }
1727*372122e3SRichard Henderson             return;
1728*372122e3SRichard Henderson 
1729*372122e3SRichard Henderson         case JMP_DIRECT_CC:
1730*372122e3SRichard Henderson             {
1731fcf5ef2aSThomas Huth                 TCGLabel *l1 = gen_new_label();
17329b158558SRichard Henderson                 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1733d4705ae0SRichard Henderson                 gen_goto_tb(dc, 1, dc->base.pc_next);
1734fcf5ef2aSThomas Huth                 gen_set_label(l1);
1735*372122e3SRichard Henderson             }
1736*372122e3SRichard Henderson             /* fall through */
1737*372122e3SRichard Henderson 
1738*372122e3SRichard Henderson         case JMP_DIRECT:
1739fcf5ef2aSThomas Huth             gen_goto_tb(dc, 0, dc->jmp_pc);
1740*372122e3SRichard Henderson             return;
1741fcf5ef2aSThomas Huth         }
1742*372122e3SRichard Henderson         /* fall through */
1743fcf5ef2aSThomas Huth 
1744a2b80dbdSRichard Henderson     default:
1745a2b80dbdSRichard Henderson         g_assert_not_reached();
1746fcf5ef2aSThomas Huth     }
1747fcf5ef2aSThomas Huth }
1748fcf5ef2aSThomas Huth 
1749*372122e3SRichard Henderson static void mb_tr_disas_log(const DisasContextBase *dcb, CPUState *cs)
1750*372122e3SRichard Henderson {
1751fcf5ef2aSThomas Huth #ifdef DEBUG_DISAS
1752fcf5ef2aSThomas Huth #if !SIM_COMPAT
1753*372122e3SRichard Henderson     qemu_log("IN: %s\n", lookup_symbol(dcb->pc_first));
1754*372122e3SRichard Henderson     log_target_disas(cs, dcb->pc_first, dcb->tb->size);
1755*372122e3SRichard Henderson #endif
1756*372122e3SRichard Henderson #endif
1757fcf5ef2aSThomas Huth }
1758*372122e3SRichard Henderson 
1759*372122e3SRichard Henderson static const TranslatorOps mb_tr_ops = {
1760*372122e3SRichard Henderson     .init_disas_context = mb_tr_init_disas_context,
1761*372122e3SRichard Henderson     .tb_start           = mb_tr_tb_start,
1762*372122e3SRichard Henderson     .insn_start         = mb_tr_insn_start,
1763*372122e3SRichard Henderson     .breakpoint_check   = mb_tr_breakpoint_check,
1764*372122e3SRichard Henderson     .translate_insn     = mb_tr_translate_insn,
1765*372122e3SRichard Henderson     .tb_stop            = mb_tr_tb_stop,
1766*372122e3SRichard Henderson     .disas_log          = mb_tr_disas_log,
1767*372122e3SRichard Henderson };
1768*372122e3SRichard Henderson 
1769*372122e3SRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
1770*372122e3SRichard Henderson {
1771*372122e3SRichard Henderson     DisasContext dc;
1772*372122e3SRichard Henderson     translator_loop(&mb_tr_ops, &dc.base, cpu, tb, max_insns);
1773fcf5ef2aSThomas Huth }
1774fcf5ef2aSThomas Huth 
177590c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1776fcf5ef2aSThomas Huth {
1777fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1778fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1779fcf5ef2aSThomas Huth     int i;
1780fcf5ef2aSThomas Huth 
178190c84c56SMarkus Armbruster     if (!env) {
1782fcf5ef2aSThomas Huth         return;
178390c84c56SMarkus Armbruster     }
1784fcf5ef2aSThomas Huth 
17850f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
178676e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
17876efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1788eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
178978e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1790eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
17910f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1792fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
17932e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
17942e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
17952e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
17962e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
17972ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
17982ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
17992ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
18002ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
18012ead1b18SJoe Komlodi         }
18022ead1b18SJoe Komlodi     }
1803fcf5ef2aSThomas Huth 
18042ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
180539db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1806af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
18072ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1808fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
180990c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1810fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
181190c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1812fcf5ef2aSThomas Huth         }
181390c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1814fcf5ef2aSThomas Huth }
1815fcf5ef2aSThomas Huth 
1816fcf5ef2aSThomas Huth void mb_tcg_init(void)
1817fcf5ef2aSThomas Huth {
1818480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1819480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1820fcf5ef2aSThomas Huth 
1821480d29a8SRichard Henderson     static const struct {
1822480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1823480d29a8SRichard Henderson     } i32s[] = {
1824480d29a8SRichard Henderson         R(0),  R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1825480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1826480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1827480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1828480d29a8SRichard Henderson 
1829480d29a8SRichard Henderson         SP(pc),
1830480d29a8SRichard Henderson         SP(msr),
18311074c0fbSRichard Henderson         SP(msr_c),
1832480d29a8SRichard Henderson         SP(imm),
1833480d29a8SRichard Henderson         SP(iflags),
1834480d29a8SRichard Henderson         SP(btaken),
1835480d29a8SRichard Henderson         SP(btarget),
1836480d29a8SRichard Henderson         SP(res_val),
1837480d29a8SRichard Henderson     };
1838480d29a8SRichard Henderson 
1839480d29a8SRichard Henderson #undef R
1840480d29a8SRichard Henderson #undef SP
1841480d29a8SRichard Henderson 
1842480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1843480d29a8SRichard Henderson         *i32s[i].var =
1844480d29a8SRichard Henderson           tcg_global_mem_new_i32(cpu_env, i32s[i].ofs, i32s[i].name);
1845fcf5ef2aSThomas Huth     }
184676e8187dSRichard Henderson 
1847480d29a8SRichard Henderson     cpu_res_addr =
1848480d29a8SRichard Henderson         tcg_global_mem_new(cpu_env, offsetof(CPUMBState, res_addr), "res_addr");
1849fcf5ef2aSThomas Huth }
1850fcf5ef2aSThomas Huth 
1851fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1852fcf5ef2aSThomas Huth                           target_ulong *data)
1853fcf5ef2aSThomas Huth {
185476e8187dSRichard Henderson     env->pc = data[0];
1855fcf5ef2aSThomas Huth }
1856