xref: /openbmc/qemu/target/microblaze/translate.c (revision 0b46fa08213b5e575b14a6dc883b45af8c37f6f6)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  Xilinx MicroBlaze emulation for qemu: main translation routines.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2009 Edgar E. Iglesias.
5fcf5ef2aSThomas Huth  *  Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "disas/disas.h"
24fcf5ef2aSThomas Huth #include "exec/exec-all.h"
25dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
26fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
27fcf5ef2aSThomas Huth #include "microblaze-decode.h"
28fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h"
29fcf5ef2aSThomas Huth #include "exec/helper-gen.h"
3077fc6f5eSLluís Vilanova #include "exec/translator.h"
3190c84c56SMarkus Armbruster #include "qemu/qemu-print.h"
32fcf5ef2aSThomas Huth 
33fcf5ef2aSThomas Huth #include "trace-tcg.h"
34fcf5ef2aSThomas Huth #include "exec/log.h"
35fcf5ef2aSThomas Huth 
36fcf5ef2aSThomas Huth 
37fcf5ef2aSThomas Huth #define SIM_COMPAT 0
38fcf5ef2aSThomas Huth #define DISAS_GNU 1
39fcf5ef2aSThomas Huth #define DISAS_MB 1
40fcf5ef2aSThomas Huth #if DISAS_MB && !SIM_COMPAT
41fcf5ef2aSThomas Huth #  define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
42fcf5ef2aSThomas Huth #else
43fcf5ef2aSThomas Huth #  define LOG_DIS(...) do { } while (0)
44fcf5ef2aSThomas Huth #endif
45fcf5ef2aSThomas Huth 
46fcf5ef2aSThomas Huth #define D(x)
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth #define EXTRACT_FIELD(src, start, end) \
49fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
50fcf5ef2aSThomas Huth 
5177fc6f5eSLluís Vilanova /* is_jmp field values */
5277fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
5377fc6f5eSLluís Vilanova #define DISAS_UPDATE  DISAS_TARGET_1 /* cpu state was modified dynamically */
5477fc6f5eSLluís Vilanova 
55cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
560f96e96bSRichard Henderson static TCGv_i32 cpu_pc;
573e0e16aeSRichard Henderson static TCGv_i32 cpu_msr;
581074c0fbSRichard Henderson static TCGv_i32 cpu_msr_c;
599b158558SRichard Henderson static TCGv_i32 cpu_imm;
609b158558SRichard Henderson static TCGv_i32 cpu_btaken;
610f96e96bSRichard Henderson static TCGv_i32 cpu_btarget;
629b158558SRichard Henderson static TCGv_i32 cpu_iflags;
639b158558SRichard Henderson static TCGv cpu_res_addr;
649b158558SRichard Henderson static TCGv_i32 cpu_res_val;
65fcf5ef2aSThomas Huth 
66fcf5ef2aSThomas Huth #include "exec/gen-icount.h"
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth /* This is the state at translation time.  */
69fcf5ef2aSThomas Huth typedef struct DisasContext {
70fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
71cfeea807SEdgar E. Iglesias     uint32_t pc;
72fcf5ef2aSThomas Huth 
73fcf5ef2aSThomas Huth     /* Decoder.  */
74fcf5ef2aSThomas Huth     int type_b;
75fcf5ef2aSThomas Huth     uint32_t ir;
76fcf5ef2aSThomas Huth     uint8_t opcode;
77fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
78fcf5ef2aSThomas Huth     uint16_t imm;
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
81fcf5ef2aSThomas Huth     unsigned int delayed_branch;
82fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
83fcf5ef2aSThomas Huth     unsigned int clear_imm;
84fcf5ef2aSThomas Huth     int is_jmp;
85fcf5ef2aSThomas Huth 
86fcf5ef2aSThomas Huth #define JMP_NOJMP     0
87fcf5ef2aSThomas Huth #define JMP_DIRECT    1
88fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
89fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
90fcf5ef2aSThomas Huth     unsigned int jmp;
91fcf5ef2aSThomas Huth     uint32_t jmp_pc;
92fcf5ef2aSThomas Huth 
93fcf5ef2aSThomas Huth     int abort_at_next_insn;
94fcf5ef2aSThomas Huth     struct TranslationBlock *tb;
95fcf5ef2aSThomas Huth     int singlestep_enabled;
96fcf5ef2aSThomas Huth } DisasContext;
97fcf5ef2aSThomas Huth 
98fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
99fcf5ef2aSThomas Huth {
100fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
101fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
1029b158558SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags);
103fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
104fcf5ef2aSThomas Huth     }
105fcf5ef2aSThomas Huth }
106fcf5ef2aSThomas Huth 
10741ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
108fcf5ef2aSThomas Huth {
109fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
110fcf5ef2aSThomas Huth 
111fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
112fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
113a2b80dbdSRichard Henderson     dc->is_jmp = DISAS_NORETURN;
114fcf5ef2aSThomas Huth }
115fcf5ef2aSThomas Huth 
11641ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
11741ba37c4SRichard Henderson {
11841ba37c4SRichard Henderson     t_sync_flags(dc);
11941ba37c4SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->pc);
12041ba37c4SRichard Henderson     gen_raise_exception(dc, index);
12141ba37c4SRichard Henderson }
12241ba37c4SRichard Henderson 
12341ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
12441ba37c4SRichard Henderson {
12541ba37c4SRichard Henderson     TCGv_i32 tmp = tcg_const_i32(esr_ec);
12641ba37c4SRichard Henderson     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, esr));
12741ba37c4SRichard Henderson     tcg_temp_free_i32(tmp);
12841ba37c4SRichard Henderson 
12941ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
13041ba37c4SRichard Henderson }
13141ba37c4SRichard Henderson 
132fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
133fcf5ef2aSThomas Huth {
134fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
135fcf5ef2aSThomas Huth     return (dc->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
136fcf5ef2aSThomas Huth #else
137fcf5ef2aSThomas Huth     return true;
138fcf5ef2aSThomas Huth #endif
139fcf5ef2aSThomas Huth }
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
142fcf5ef2aSThomas Huth {
143*0b46fa08SRichard Henderson     if (dc->singlestep_enabled) {
144*0b46fa08SRichard Henderson         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
145*0b46fa08SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
146*0b46fa08SRichard Henderson         gen_helper_raise_exception(cpu_env, tmp);
147*0b46fa08SRichard Henderson         tcg_temp_free_i32(tmp);
148*0b46fa08SRichard Henderson     } else if (use_goto_tb(dc, dest)) {
149fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1500f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
15107ea28b4SRichard Henderson         tcg_gen_exit_tb(dc->tb, n);
152fcf5ef2aSThomas Huth     } else {
1530f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
15407ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
155fcf5ef2aSThomas Huth     }
156a2b80dbdSRichard Henderson     dc->is_jmp = DISAS_NORETURN;
157fcf5ef2aSThomas Huth }
158fcf5ef2aSThomas Huth 
159bdfc1e88SEdgar E. Iglesias /*
1609ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1619ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1629ba8cd45SEdgar E. Iglesias  */
1639ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1649ba8cd45SEdgar E. Iglesias {
1659ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1665143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
16741ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1689ba8cd45SEdgar E. Iglesias     }
1699ba8cd45SEdgar E. Iglesias     return cond;
1709ba8cd45SEdgar E. Iglesias }
1719ba8cd45SEdgar E. Iglesias 
1729ba8cd45SEdgar E. Iglesias /*
173bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
174bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
175bdfc1e88SEdgar E. Iglesias  */
176bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
177bdfc1e88SEdgar E. Iglesias {
178bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
179bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
180bdfc1e88SEdgar E. Iglesias 
181bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
18241ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
183bdfc1e88SEdgar E. Iglesias     }
184bdfc1e88SEdgar E. Iglesias     return cond_user;
185bdfc1e88SEdgar E. Iglesias }
186bdfc1e88SEdgar E. Iglesias 
187fcf5ef2aSThomas Huth /* True if ALU operand b is a small immediate that may deserve
188fcf5ef2aSThomas Huth    faster treatment.  */
189fcf5ef2aSThomas Huth static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
190fcf5ef2aSThomas Huth {
191fcf5ef2aSThomas Huth     /* Immediate insn without the imm prefix ?  */
192fcf5ef2aSThomas Huth     return dc->type_b && !(dc->tb_flags & IMM_FLAG);
193fcf5ef2aSThomas Huth }
194fcf5ef2aSThomas Huth 
195cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
196fcf5ef2aSThomas Huth {
197fcf5ef2aSThomas Huth     if (dc->type_b) {
198fcf5ef2aSThomas Huth         if (dc->tb_flags & IMM_FLAG)
1999b158558SRichard Henderson             tcg_gen_ori_i32(cpu_imm, cpu_imm, dc->imm);
200fcf5ef2aSThomas Huth         else
2019b158558SRichard Henderson             tcg_gen_movi_i32(cpu_imm, (int32_t)((int16_t)dc->imm));
2029b158558SRichard Henderson         return &cpu_imm;
203fcf5ef2aSThomas Huth     } else
204fcf5ef2aSThomas Huth         return &cpu_R[dc->rb];
205fcf5ef2aSThomas Huth }
206fcf5ef2aSThomas Huth 
207fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
208fcf5ef2aSThomas Huth {
209fcf5ef2aSThomas Huth     unsigned int k, c;
210cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
211fcf5ef2aSThomas Huth 
212fcf5ef2aSThomas Huth     k = dc->opcode & 4;
213fcf5ef2aSThomas Huth     c = dc->opcode & 2;
214fcf5ef2aSThomas Huth 
215fcf5ef2aSThomas Huth     LOG_DIS("add%s%s%s r%d r%d r%d\n",
216fcf5ef2aSThomas Huth             dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
217fcf5ef2aSThomas Huth             dc->rd, dc->ra, dc->rb);
218fcf5ef2aSThomas Huth 
219fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
220fcf5ef2aSThomas Huth     if (k) {
221fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
222fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
223fcf5ef2aSThomas Huth         if (dc->rd) {
224cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
225fcf5ef2aSThomas Huth 
226fcf5ef2aSThomas Huth             if (c) {
227fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2281074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
229fcf5ef2aSThomas Huth             }
230fcf5ef2aSThomas Huth         }
231fcf5ef2aSThomas Huth         return;
232fcf5ef2aSThomas Huth     }
233fcf5ef2aSThomas Huth 
234fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
235fcf5ef2aSThomas Huth     /* Extract carry.  */
236cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
237fcf5ef2aSThomas Huth     if (c) {
2381074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
239fcf5ef2aSThomas Huth     } else {
240cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
241fcf5ef2aSThomas Huth     }
242fcf5ef2aSThomas Huth 
2431074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
244fcf5ef2aSThomas Huth     if (dc->rd) {
245cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
246cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
247fcf5ef2aSThomas Huth     }
248cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
249fcf5ef2aSThomas Huth }
250fcf5ef2aSThomas Huth 
251fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
252fcf5ef2aSThomas Huth {
253fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
254cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
255fcf5ef2aSThomas Huth 
256fcf5ef2aSThomas Huth     u = dc->imm & 2;
257fcf5ef2aSThomas Huth     k = dc->opcode & 4;
258fcf5ef2aSThomas Huth     c = dc->opcode & 2;
259fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
260fcf5ef2aSThomas Huth 
261fcf5ef2aSThomas Huth     if (cmp) {
262fcf5ef2aSThomas Huth         LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
263fcf5ef2aSThomas Huth         if (dc->rd) {
264fcf5ef2aSThomas Huth             if (u)
265fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
266fcf5ef2aSThomas Huth             else
267fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
268fcf5ef2aSThomas Huth         }
269fcf5ef2aSThomas Huth         return;
270fcf5ef2aSThomas Huth     }
271fcf5ef2aSThomas Huth 
272fcf5ef2aSThomas Huth     LOG_DIS("sub%s%s r%d, r%d r%d\n",
273fcf5ef2aSThomas Huth              k ? "k" : "",  c ? "c" : "", dc->rd, dc->ra, dc->rb);
274fcf5ef2aSThomas Huth 
275fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
276fcf5ef2aSThomas Huth     if (k) {
277fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
278fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
279fcf5ef2aSThomas Huth         if (dc->rd) {
280cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
281fcf5ef2aSThomas Huth 
282fcf5ef2aSThomas Huth             if (c) {
283fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2841074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
285fcf5ef2aSThomas Huth             }
286fcf5ef2aSThomas Huth         }
287fcf5ef2aSThomas Huth         return;
288fcf5ef2aSThomas Huth     }
289fcf5ef2aSThomas Huth 
290fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
291fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
292cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
293cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
294fcf5ef2aSThomas Huth     if (c) {
2951074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
296fcf5ef2aSThomas Huth     } else {
297cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
298fcf5ef2aSThomas Huth     }
299fcf5ef2aSThomas Huth 
300fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
301cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
302fcf5ef2aSThomas Huth 
3031074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, na, *(dec_alu_op_b(dc)), cf);
304fcf5ef2aSThomas Huth     if (dc->rd) {
305cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
306cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
307fcf5ef2aSThomas Huth     }
308cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
309cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
310fcf5ef2aSThomas Huth }
311fcf5ef2aSThomas Huth 
312fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
313fcf5ef2aSThomas Huth {
314fcf5ef2aSThomas Huth     unsigned int mode;
315fcf5ef2aSThomas Huth 
3169ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
3179ba8cd45SEdgar E. Iglesias         return;
318fcf5ef2aSThomas Huth     }
319fcf5ef2aSThomas Huth 
320fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
321fcf5ef2aSThomas Huth     switch (mode) {
322fcf5ef2aSThomas Huth         case 0:
323fcf5ef2aSThomas Huth             /* pcmpbf.  */
324fcf5ef2aSThomas Huth             LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
325fcf5ef2aSThomas Huth             if (dc->rd)
326fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
327fcf5ef2aSThomas Huth             break;
328fcf5ef2aSThomas Huth         case 2:
329fcf5ef2aSThomas Huth             LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
330fcf5ef2aSThomas Huth             if (dc->rd) {
331cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
332fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
333fcf5ef2aSThomas Huth             }
334fcf5ef2aSThomas Huth             break;
335fcf5ef2aSThomas Huth         case 3:
336fcf5ef2aSThomas Huth             LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
337fcf5ef2aSThomas Huth             if (dc->rd) {
338cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
339fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
340fcf5ef2aSThomas Huth             }
341fcf5ef2aSThomas Huth             break;
342fcf5ef2aSThomas Huth         default:
343fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
344fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
345fcf5ef2aSThomas Huth             break;
346fcf5ef2aSThomas Huth     }
347fcf5ef2aSThomas Huth }
348fcf5ef2aSThomas Huth 
349fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
350fcf5ef2aSThomas Huth {
351fcf5ef2aSThomas Huth     unsigned int not;
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
354fcf5ef2aSThomas Huth         dec_pattern(dc);
355fcf5ef2aSThomas Huth         return;
356fcf5ef2aSThomas Huth     }
357fcf5ef2aSThomas Huth 
358fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
359fcf5ef2aSThomas Huth     LOG_DIS("and%s\n", not ? "n" : "");
360fcf5ef2aSThomas Huth 
361fcf5ef2aSThomas Huth     if (!dc->rd)
362fcf5ef2aSThomas Huth         return;
363fcf5ef2aSThomas Huth 
364fcf5ef2aSThomas Huth     if (not) {
365cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
366fcf5ef2aSThomas Huth     } else
367cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
368fcf5ef2aSThomas Huth }
369fcf5ef2aSThomas Huth 
370fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
371fcf5ef2aSThomas Huth {
372fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
373fcf5ef2aSThomas Huth         dec_pattern(dc);
374fcf5ef2aSThomas Huth         return;
375fcf5ef2aSThomas Huth     }
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth     LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
378fcf5ef2aSThomas Huth     if (dc->rd)
379cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
380fcf5ef2aSThomas Huth }
381fcf5ef2aSThomas Huth 
382fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
383fcf5ef2aSThomas Huth {
384fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
385fcf5ef2aSThomas Huth         dec_pattern(dc);
386fcf5ef2aSThomas Huth         return;
387fcf5ef2aSThomas Huth     }
388fcf5ef2aSThomas Huth 
389fcf5ef2aSThomas Huth     LOG_DIS("xor r%d\n", dc->rd);
390fcf5ef2aSThomas Huth     if (dc->rd)
391cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
392fcf5ef2aSThomas Huth }
393fcf5ef2aSThomas Huth 
3941074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
395fcf5ef2aSThomas Huth {
3961074c0fbSRichard Henderson     TCGv_i32 t;
3971074c0fbSRichard Henderson 
3981074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
3991074c0fbSRichard Henderson     t = tcg_temp_new_i32();
4001074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
4011074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
4021074c0fbSRichard Henderson     tcg_temp_free_i32(t);
403fcf5ef2aSThomas Huth }
404fcf5ef2aSThomas Huth 
4051074c0fbSRichard Henderson static void msr_write(DisasContext *dc, TCGv_i32 v)
406fcf5ef2aSThomas Huth {
407fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
4081074c0fbSRichard Henderson 
4091074c0fbSRichard Henderson     /* Install MSR_C.  */
4101074c0fbSRichard Henderson     tcg_gen_extract_i32(cpu_msr_c, v, 2, 1);
4111074c0fbSRichard Henderson 
4121074c0fbSRichard Henderson     /* Clear MSR_C and MSR_CC; MSR_PVR is not writable, and is always clear. */
4131074c0fbSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~(MSR_C | MSR_CC | MSR_PVR));
414fcf5ef2aSThomas Huth }
415fcf5ef2aSThomas Huth 
416fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
417fcf5ef2aSThomas Huth {
418fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
419cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4202023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
421f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
422fcf5ef2aSThomas Huth 
4232023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
4242023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
4252023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
426fcf5ef2aSThomas Huth     dc->type_b = 1;
4272023e9a3SEdgar E. Iglesias     if (to) {
428fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
429f0f7e7f7SEdgar E. Iglesias     }
430f0f7e7f7SEdgar E. Iglesias 
431f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
432f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
433f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
434f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
435f0f7e7f7SEdgar E. Iglesias 
436f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4372023e9a3SEdgar E. Iglesias     }
438fcf5ef2aSThomas Huth 
439fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4402023e9a3SEdgar E. Iglesias     if (clrset) {
4412023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
442fcf5ef2aSThomas Huth 
443fcf5ef2aSThomas Huth         LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
444fcf5ef2aSThomas Huth                 dc->rd, dc->imm);
445fcf5ef2aSThomas Huth 
44656837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
447fcf5ef2aSThomas Huth             /* nop??? */
448fcf5ef2aSThomas Huth             return;
449fcf5ef2aSThomas Huth         }
450fcf5ef2aSThomas Huth 
451bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
452fcf5ef2aSThomas Huth             return;
453fcf5ef2aSThomas Huth         }
454fcf5ef2aSThomas Huth 
455fcf5ef2aSThomas Huth         if (dc->rd)
456fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
457fcf5ef2aSThomas Huth 
458cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
459cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
460fcf5ef2aSThomas Huth         msr_read(dc, t0);
461cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
462fcf5ef2aSThomas Huth 
463fcf5ef2aSThomas Huth         if (clr) {
464cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
465cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
466fcf5ef2aSThomas Huth         } else
467cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
468fcf5ef2aSThomas Huth         msr_write(dc, t0);
469cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
470cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
4710f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
472fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
473fcf5ef2aSThomas Huth         return;
474fcf5ef2aSThomas Huth     }
475fcf5ef2aSThomas Huth 
476bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
477fcf5ef2aSThomas Huth         return;
478fcf5ef2aSThomas Huth     }
479fcf5ef2aSThomas Huth 
480fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
481fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
482fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
483f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
48405a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
48505a9a651SEdgar E. Iglesias 
486fcf5ef2aSThomas Huth         sr &= 7;
48705a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
488fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
48905a9a651SEdgar E. Iglesias         if (to) {
490f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
49105a9a651SEdgar E. Iglesias         } else {
492f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
49305a9a651SEdgar E. Iglesias         }
49405a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
495f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
496fcf5ef2aSThomas Huth         return;
497fcf5ef2aSThomas Huth     }
498fcf5ef2aSThomas Huth #endif
499fcf5ef2aSThomas Huth 
500fcf5ef2aSThomas Huth     if (to) {
501fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
502fcf5ef2aSThomas Huth         switch (sr) {
503aa28e6d4SRichard Henderson             case SR_PC:
504fcf5ef2aSThomas Huth                 break;
505aa28e6d4SRichard Henderson             case SR_MSR:
506fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
507fcf5ef2aSThomas Huth                 break;
508351527b7SEdgar E. Iglesias             case SR_EAR:
509dbdb77c4SRichard Henderson                 {
510dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
511dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
512dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
513dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
514dbdb77c4SRichard Henderson                 }
515aa28e6d4SRichard Henderson                 break;
516351527b7SEdgar E. Iglesias             case SR_ESR:
51741ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
51841ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
519aa28e6d4SRichard Henderson                 break;
520ab6dd380SEdgar E. Iglesias             case SR_FSR:
52186017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
52286017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
523aa28e6d4SRichard Henderson                 break;
524aa28e6d4SRichard Henderson             case SR_BTR:
525ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
526ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
527aa28e6d4SRichard Henderson                 break;
528aa28e6d4SRichard Henderson             case SR_EDR:
52939db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
53039db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
531fcf5ef2aSThomas Huth                 break;
532fcf5ef2aSThomas Huth             case 0x800:
533cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
534cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
535fcf5ef2aSThomas Huth                 break;
536fcf5ef2aSThomas Huth             case 0x802:
537cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
538cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
539fcf5ef2aSThomas Huth                 break;
540fcf5ef2aSThomas Huth             default:
541fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
542fcf5ef2aSThomas Huth                 break;
543fcf5ef2aSThomas Huth         }
544fcf5ef2aSThomas Huth     } else {
545fcf5ef2aSThomas Huth         LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
546fcf5ef2aSThomas Huth 
547fcf5ef2aSThomas Huth         switch (sr) {
548aa28e6d4SRichard Henderson             case SR_PC:
549cfeea807SEdgar E. Iglesias                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
550fcf5ef2aSThomas Huth                 break;
551aa28e6d4SRichard Henderson             case SR_MSR:
552fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
553fcf5ef2aSThomas Huth                 break;
554351527b7SEdgar E. Iglesias             case SR_EAR:
555dbdb77c4SRichard Henderson                 {
556dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
557dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
558a1b48e3aSEdgar E. Iglesias                     if (extended) {
559dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
560aa28e6d4SRichard Henderson                     } else {
561dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
562dbdb77c4SRichard Henderson                     }
563dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
564a1b48e3aSEdgar E. Iglesias                 }
565aa28e6d4SRichard Henderson                 break;
566351527b7SEdgar E. Iglesias             case SR_ESR:
56741ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
56841ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
569aa28e6d4SRichard Henderson                 break;
570351527b7SEdgar E. Iglesias             case SR_FSR:
57186017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
57286017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
573aa28e6d4SRichard Henderson                 break;
574351527b7SEdgar E. Iglesias             case SR_BTR:
575ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
576ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
577aa28e6d4SRichard Henderson                 break;
5787cdae31dSTong Ho             case SR_EDR:
57939db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
58039db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
581fcf5ef2aSThomas Huth                 break;
582fcf5ef2aSThomas Huth             case 0x800:
583cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
584cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
585fcf5ef2aSThomas Huth                 break;
586fcf5ef2aSThomas Huth             case 0x802:
587cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
588cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
589fcf5ef2aSThomas Huth                 break;
590351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
591fcf5ef2aSThomas Huth                 rn = sr & 0xf;
592cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
593fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
594fcf5ef2aSThomas Huth                 break;
595fcf5ef2aSThomas Huth             default:
596fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
597fcf5ef2aSThomas Huth                 break;
598fcf5ef2aSThomas Huth         }
599fcf5ef2aSThomas Huth     }
600fcf5ef2aSThomas Huth 
601fcf5ef2aSThomas Huth     if (dc->rd == 0) {
602cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
603fcf5ef2aSThomas Huth     }
604fcf5ef2aSThomas Huth }
605fcf5ef2aSThomas Huth 
606fcf5ef2aSThomas Huth /* Multiplier unit.  */
607fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
608fcf5ef2aSThomas Huth {
609cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
610fcf5ef2aSThomas Huth     unsigned int subcode;
611fcf5ef2aSThomas Huth 
6129ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
613fcf5ef2aSThomas Huth         return;
614fcf5ef2aSThomas Huth     }
615fcf5ef2aSThomas Huth 
616fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
617fcf5ef2aSThomas Huth 
618fcf5ef2aSThomas Huth     if (dc->type_b) {
619fcf5ef2aSThomas Huth         LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
620cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
621fcf5ef2aSThomas Huth         return;
622fcf5ef2aSThomas Huth     }
623fcf5ef2aSThomas Huth 
624fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6259b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
626fcf5ef2aSThomas Huth         /* nop??? */
627fcf5ef2aSThomas Huth     }
628fcf5ef2aSThomas Huth 
629cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
630fcf5ef2aSThomas Huth     switch (subcode) {
631fcf5ef2aSThomas Huth         case 0:
632fcf5ef2aSThomas Huth             LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
633cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
634fcf5ef2aSThomas Huth             break;
635fcf5ef2aSThomas Huth         case 1:
636fcf5ef2aSThomas Huth             LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
637cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
638cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
639fcf5ef2aSThomas Huth             break;
640fcf5ef2aSThomas Huth         case 2:
641fcf5ef2aSThomas Huth             LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
642cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
643cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
644fcf5ef2aSThomas Huth             break;
645fcf5ef2aSThomas Huth         case 3:
646fcf5ef2aSThomas Huth             LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
647cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
648fcf5ef2aSThomas Huth             break;
649fcf5ef2aSThomas Huth         default:
650fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
651fcf5ef2aSThomas Huth             break;
652fcf5ef2aSThomas Huth     }
653cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
654fcf5ef2aSThomas Huth }
655fcf5ef2aSThomas Huth 
656fcf5ef2aSThomas Huth /* Div unit.  */
657fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
658fcf5ef2aSThomas Huth {
659fcf5ef2aSThomas Huth     unsigned int u;
660fcf5ef2aSThomas Huth 
661fcf5ef2aSThomas Huth     u = dc->imm & 2;
662fcf5ef2aSThomas Huth     LOG_DIS("div\n");
663fcf5ef2aSThomas Huth 
6649ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
6659ba8cd45SEdgar E. Iglesias         return;
666fcf5ef2aSThomas Huth     }
667fcf5ef2aSThomas Huth 
668fcf5ef2aSThomas Huth     if (u)
669fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
670fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
671fcf5ef2aSThomas Huth     else
672fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
673fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
674fcf5ef2aSThomas Huth     if (!dc->rd)
675cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
676fcf5ef2aSThomas Huth }
677fcf5ef2aSThomas Huth 
678fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
679fcf5ef2aSThomas Huth {
680cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
681faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
682d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
683fcf5ef2aSThomas Huth 
6849ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
685fcf5ef2aSThomas Huth         return;
686fcf5ef2aSThomas Huth     }
687fcf5ef2aSThomas Huth 
688faa48d74SEdgar E. Iglesias     if (dc->type_b) {
689faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
690d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
691faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
692faa48d74SEdgar E. Iglesias     }
693e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
694e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
695faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
696faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
697fcf5ef2aSThomas Huth 
698faa48d74SEdgar E. Iglesias     LOG_DIS("bs%s%s%s r%d r%d r%d\n",
699faa48d74SEdgar E. Iglesias             e ? "e" : "",
700fcf5ef2aSThomas Huth             s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
701fcf5ef2aSThomas Huth 
702faa48d74SEdgar E. Iglesias     if (e) {
703faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
704faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
705faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
706faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
707faa48d74SEdgar E. Iglesias         } else {
708faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
709faa48d74SEdgar E. Iglesias         }
710d09b2585SEdgar E. Iglesias     } else if (i) {
711d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
712d09b2585SEdgar E. Iglesias 
713d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
714d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
715d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
716d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
717d09b2585SEdgar E. Iglesias         } else {
718d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
719d09b2585SEdgar E. Iglesias                                 imm_s, width);
720d09b2585SEdgar E. Iglesias         }
721faa48d74SEdgar E. Iglesias     } else {
722cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
723fcf5ef2aSThomas Huth 
724cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
725cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
726fcf5ef2aSThomas Huth 
7272acf6d53SEdgar E. Iglesias         if (s) {
728cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7292acf6d53SEdgar E. Iglesias         } else {
7302acf6d53SEdgar E. Iglesias             if (t) {
731cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7322acf6d53SEdgar E. Iglesias             } else {
733cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
734fcf5ef2aSThomas Huth             }
735fcf5ef2aSThomas Huth         }
736cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7372acf6d53SEdgar E. Iglesias     }
738faa48d74SEdgar E. Iglesias }
739fcf5ef2aSThomas Huth 
740fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
741fcf5ef2aSThomas Huth {
742fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
743cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
744fcf5ef2aSThomas Huth     unsigned int op;
745fcf5ef2aSThomas Huth 
746fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
747fcf5ef2aSThomas Huth     switch (op) {
748fcf5ef2aSThomas Huth         case 0x21:
749fcf5ef2aSThomas Huth             /* src.  */
750cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
751fcf5ef2aSThomas Huth 
752fcf5ef2aSThomas Huth             LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
7531074c0fbSRichard Henderson             tcg_gen_shli_i32(t0, cpu_msr_c, 31);
7541074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
755fcf5ef2aSThomas Huth             if (dc->rd) {
756cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
757cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
758fcf5ef2aSThomas Huth             }
759cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
760fcf5ef2aSThomas Huth             break;
761fcf5ef2aSThomas Huth 
762fcf5ef2aSThomas Huth         case 0x1:
763fcf5ef2aSThomas Huth         case 0x41:
764fcf5ef2aSThomas Huth             /* srl.  */
765fcf5ef2aSThomas Huth             LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
766fcf5ef2aSThomas Huth 
7671074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
768fcf5ef2aSThomas Huth             if (dc->rd) {
769fcf5ef2aSThomas Huth                 if (op == 0x41)
770cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
771fcf5ef2aSThomas Huth                 else
772cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
773fcf5ef2aSThomas Huth             }
774fcf5ef2aSThomas Huth             break;
775fcf5ef2aSThomas Huth         case 0x60:
776fcf5ef2aSThomas Huth             LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
777fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
778fcf5ef2aSThomas Huth             break;
779fcf5ef2aSThomas Huth         case 0x61:
780fcf5ef2aSThomas Huth             LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
781fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
782fcf5ef2aSThomas Huth             break;
783fcf5ef2aSThomas Huth         case 0x64:
784fcf5ef2aSThomas Huth         case 0x66:
785fcf5ef2aSThomas Huth         case 0x74:
786fcf5ef2aSThomas Huth         case 0x76:
787fcf5ef2aSThomas Huth             /* wdc.  */
788fcf5ef2aSThomas Huth             LOG_DIS("wdc r%d\n", dc->ra);
789bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
790fcf5ef2aSThomas Huth             break;
791fcf5ef2aSThomas Huth         case 0x68:
792fcf5ef2aSThomas Huth             /* wic.  */
793fcf5ef2aSThomas Huth             LOG_DIS("wic r%d\n", dc->ra);
794bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
795fcf5ef2aSThomas Huth             break;
796fcf5ef2aSThomas Huth         case 0xe0:
7979ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
7989ba8cd45SEdgar E. Iglesias                 return;
799fcf5ef2aSThomas Huth             }
8008fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
8015318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
802fcf5ef2aSThomas Huth             }
803fcf5ef2aSThomas Huth             break;
804fcf5ef2aSThomas Huth         case 0x1e0:
805fcf5ef2aSThomas Huth             /* swapb */
806fcf5ef2aSThomas Huth             LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
807fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
808fcf5ef2aSThomas Huth             break;
809fcf5ef2aSThomas Huth         case 0x1e2:
810fcf5ef2aSThomas Huth             /*swaph */
811fcf5ef2aSThomas Huth             LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
812fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
813fcf5ef2aSThomas Huth             break;
814fcf5ef2aSThomas Huth         default:
815fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
816fcf5ef2aSThomas Huth                       dc->pc, op, dc->rd, dc->ra, dc->rb);
817fcf5ef2aSThomas Huth             break;
818fcf5ef2aSThomas Huth     }
819fcf5ef2aSThomas Huth }
820fcf5ef2aSThomas Huth 
821fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
822fcf5ef2aSThomas Huth {
823fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
824fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
8259b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
826fcf5ef2aSThomas Huth         }
827fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
8280f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
829fcf5ef2aSThomas Huth     }
830fcf5ef2aSThomas Huth }
831fcf5ef2aSThomas Huth 
832fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
833fcf5ef2aSThomas Huth {
834fcf5ef2aSThomas Huth     LOG_DIS("imm %x\n", dc->imm << 16);
8359b158558SRichard Henderson     tcg_gen_movi_i32(cpu_imm, (dc->imm << 16));
836fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
837fcf5ef2aSThomas Huth     dc->clear_imm = 0;
838fcf5ef2aSThomas Huth }
839fcf5ef2aSThomas Huth 
840d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
841fcf5ef2aSThomas Huth {
8420e9033c8SEdgar E. Iglesias     bool extimm = dc->tb_flags & IMM_FLAG;
8430e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8440e9033c8SEdgar E. Iglesias     bool stackprot = false;
845403322eaSEdgar E. Iglesias     TCGv_i32 t32;
846fcf5ef2aSThomas Huth 
847fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
848fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8490e9033c8SEdgar E. Iglesias         stackprot = true;
850fcf5ef2aSThomas Huth     }
851fcf5ef2aSThomas Huth 
852fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
853fcf5ef2aSThomas Huth     if (!dc->type_b) {
854d248e1beSEdgar E. Iglesias         if (ea) {
855d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
856d248e1beSEdgar E. Iglesias 
857d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
858d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
859d248e1beSEdgar E. Iglesias                 return;
860d248e1beSEdgar E. Iglesias             }
861d248e1beSEdgar E. Iglesias 
862d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
863d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
864d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
865d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
866d248e1beSEdgar E. Iglesias             }
867d248e1beSEdgar E. Iglesias             return;
868d248e1beSEdgar E. Iglesias         }
869d248e1beSEdgar E. Iglesias 
8700dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
871fcf5ef2aSThomas Huth         if (dc->ra == 0) {
872403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8730dc4af5cSEdgar E. Iglesias             return;
874fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
875403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
8760dc4af5cSEdgar E. Iglesias             return;
877fcf5ef2aSThomas Huth         }
878fcf5ef2aSThomas Huth 
879fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
8800e9033c8SEdgar E. Iglesias             stackprot = true;
881fcf5ef2aSThomas Huth         }
882fcf5ef2aSThomas Huth 
883403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
884403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
885403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
886403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
887fcf5ef2aSThomas Huth 
888fcf5ef2aSThomas Huth         if (stackprot) {
8890a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
890fcf5ef2aSThomas Huth         }
8910dc4af5cSEdgar E. Iglesias         return;
892fcf5ef2aSThomas Huth     }
893fcf5ef2aSThomas Huth     /* Immediate.  */
894403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
895fcf5ef2aSThomas Huth     if (!extimm) {
896f7a66e3aSEdgar E. Iglesias         tcg_gen_addi_i32(t32, cpu_R[dc->ra], (int16_t)dc->imm);
897403322eaSEdgar E. Iglesias     } else {
898403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
899403322eaSEdgar E. Iglesias     }
900403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
901403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
902fcf5ef2aSThomas Huth 
903fcf5ef2aSThomas Huth     if (stackprot) {
9040a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
905fcf5ef2aSThomas Huth     }
9060dc4af5cSEdgar E. Iglesias     return;
907fcf5ef2aSThomas Huth }
908fcf5ef2aSThomas Huth 
909fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
910fcf5ef2aSThomas Huth {
911403322eaSEdgar E. Iglesias     TCGv_i32 v;
912403322eaSEdgar E. Iglesias     TCGv addr;
9138534063aSEdgar E. Iglesias     unsigned int size;
914d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
915d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
91614776ab5STony Nguyen     MemOp mop;
917fcf5ef2aSThomas Huth 
918fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
919fcf5ef2aSThomas Huth     size = 1 << mop;
920fcf5ef2aSThomas Huth     if (!dc->type_b) {
921d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9228534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9238534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
924fcf5ef2aSThomas Huth     }
925fcf5ef2aSThomas Huth     mop |= MO_TE;
926fcf5ef2aSThomas Huth     if (rev) {
927fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
928fcf5ef2aSThomas Huth     }
929fcf5ef2aSThomas Huth 
9309ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
931fcf5ef2aSThomas Huth         return;
932fcf5ef2aSThomas Huth     }
933fcf5ef2aSThomas Huth 
934d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
935d248e1beSEdgar E. Iglesias         return;
936d248e1beSEdgar E. Iglesias     }
937d248e1beSEdgar E. Iglesias 
938d248e1beSEdgar E. Iglesias     LOG_DIS("l%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
939d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
940d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
941fcf5ef2aSThomas Huth 
942fcf5ef2aSThomas Huth     t_sync_flags(dc);
943403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
944d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
945d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
946d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
947fcf5ef2aSThomas Huth 
948fcf5ef2aSThomas Huth     /*
949fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
950fcf5ef2aSThomas Huth      *
951fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
952fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
953fcf5ef2aSThomas Huth      */
954fcf5ef2aSThomas Huth     if (rev && size != 4) {
955fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
956fcf5ef2aSThomas Huth         switch (size) {
957fcf5ef2aSThomas Huth             case 1:
958fcf5ef2aSThomas Huth             {
959a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
960fcf5ef2aSThomas Huth                 break;
961fcf5ef2aSThomas Huth             }
962fcf5ef2aSThomas Huth 
963fcf5ef2aSThomas Huth             case 2:
964fcf5ef2aSThomas Huth                 /* 00 -> 10
965fcf5ef2aSThomas Huth                    10 -> 00.  */
966403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
967fcf5ef2aSThomas Huth                 break;
968fcf5ef2aSThomas Huth             default:
969fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
970fcf5ef2aSThomas Huth                 break;
971fcf5ef2aSThomas Huth         }
972fcf5ef2aSThomas Huth     }
973fcf5ef2aSThomas Huth 
974fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
975fcf5ef2aSThomas Huth     if (ex) {
976403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
977fcf5ef2aSThomas Huth     }
978fcf5ef2aSThomas Huth 
979fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
980fcf5ef2aSThomas Huth     sync_jmpstate(dc);
981fcf5ef2aSThomas Huth 
982fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
983fcf5ef2aSThomas Huth     /*
984fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
985fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
986fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
987fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
988fcf5ef2aSThomas Huth      */
989cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
990d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
991fcf5ef2aSThomas Huth 
9921507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
993a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
994a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
995a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
996a6338015SEdgar E. Iglesias 
9970f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
998a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
999a6338015SEdgar E. Iglesias 
1000a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
1001a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1002a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1003fcf5ef2aSThomas Huth     }
1004fcf5ef2aSThomas Huth 
1005fcf5ef2aSThomas Huth     if (ex) {
10069b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
10079b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
1008fcf5ef2aSThomas Huth     }
1009fcf5ef2aSThomas Huth     if (dc->rd) {
1010cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1011fcf5ef2aSThomas Huth     }
1012cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1013fcf5ef2aSThomas Huth 
1014fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1015fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
10161074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1017fcf5ef2aSThomas Huth     }
1018fcf5ef2aSThomas Huth 
1019403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1020fcf5ef2aSThomas Huth }
1021fcf5ef2aSThomas Huth 
1022fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1023fcf5ef2aSThomas Huth {
1024403322eaSEdgar E. Iglesias     TCGv addr;
1025fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1026b51b3d43SEdgar E. Iglesias     unsigned int size;
1027d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1028d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
102914776ab5STony Nguyen     MemOp mop;
1030fcf5ef2aSThomas Huth 
1031fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1032fcf5ef2aSThomas Huth     size = 1 << mop;
1033fcf5ef2aSThomas Huth     if (!dc->type_b) {
1034d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1035b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1036b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1037fcf5ef2aSThomas Huth     }
1038fcf5ef2aSThomas Huth     mop |= MO_TE;
1039fcf5ef2aSThomas Huth     if (rev) {
1040fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1041fcf5ef2aSThomas Huth     }
1042fcf5ef2aSThomas Huth 
10439ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1044fcf5ef2aSThomas Huth         return;
1045fcf5ef2aSThomas Huth     }
1046fcf5ef2aSThomas Huth 
1047d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1048d248e1beSEdgar E. Iglesias 
1049d248e1beSEdgar E. Iglesias     LOG_DIS("s%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1050d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
1051d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
1052fcf5ef2aSThomas Huth     t_sync_flags(dc);
1053fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1054fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10550dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1056403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1057d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1058d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1059d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1060fcf5ef2aSThomas Huth 
1061fcf5ef2aSThomas Huth     if (ex) { /* swx */
1062cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1063fcf5ef2aSThomas Huth 
1064fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1065403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1066fcf5ef2aSThomas Huth 
10671074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 1);
1068fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
10699b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1070fcf5ef2aSThomas Huth 
1071071cdc67SEdgar E. Iglesias         /*
1072071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1073071cdc67SEdgar E. Iglesias          * the reserved location.
1074071cdc67SEdgar E. Iglesias          */
1075cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1076071cdc67SEdgar E. Iglesias 
10779b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1078071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1079071cdc67SEdgar E. Iglesias                                    mop);
1080071cdc67SEdgar E. Iglesias 
10819b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
10821074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1083cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1084fcf5ef2aSThomas Huth     }
1085fcf5ef2aSThomas Huth 
1086fcf5ef2aSThomas Huth     if (rev && size != 4) {
1087fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1088fcf5ef2aSThomas Huth         switch (size) {
1089fcf5ef2aSThomas Huth             case 1:
1090fcf5ef2aSThomas Huth             {
1091a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1092fcf5ef2aSThomas Huth                 break;
1093fcf5ef2aSThomas Huth             }
1094fcf5ef2aSThomas Huth 
1095fcf5ef2aSThomas Huth             case 2:
1096fcf5ef2aSThomas Huth                 /* 00 -> 10
1097fcf5ef2aSThomas Huth                    10 -> 00.  */
1098fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1099403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1100fcf5ef2aSThomas Huth                 break;
1101fcf5ef2aSThomas Huth             default:
1102fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1103fcf5ef2aSThomas Huth                 break;
1104fcf5ef2aSThomas Huth         }
1105fcf5ef2aSThomas Huth     }
1106071cdc67SEdgar E. Iglesias 
1107071cdc67SEdgar E. Iglesias     if (!ex) {
1108d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1109071cdc67SEdgar E. Iglesias     }
1110fcf5ef2aSThomas Huth 
1111fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11121507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1113a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1114a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1115a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1116a6338015SEdgar E. Iglesias 
11170f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
1118fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1119fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1120fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1121fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1122fcf5ef2aSThomas Huth          *        access.
1123fcf5ef2aSThomas Huth          */
1124a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1125a6338015SEdgar E. Iglesias 
1126a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1127a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1128a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1129fcf5ef2aSThomas Huth     }
1130fcf5ef2aSThomas Huth 
1131fcf5ef2aSThomas Huth     if (ex) {
1132fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1133fcf5ef2aSThomas Huth     }
1134fcf5ef2aSThomas Huth 
1135403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1136fcf5ef2aSThomas Huth }
1137fcf5ef2aSThomas Huth 
1138fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11399e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1140fcf5ef2aSThomas Huth {
1141d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1142d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1143d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1144d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1145d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1146d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1147d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1148d89b86e9SEdgar E. Iglesias     };
1149d89b86e9SEdgar E. Iglesias 
1150fcf5ef2aSThomas Huth     switch (cc) {
1151fcf5ef2aSThomas Huth     case CC_EQ:
1152fcf5ef2aSThomas Huth     case CC_NE:
1153fcf5ef2aSThomas Huth     case CC_LT:
1154fcf5ef2aSThomas Huth     case CC_LE:
1155fcf5ef2aSThomas Huth     case CC_GE:
1156fcf5ef2aSThomas Huth     case CC_GT:
11579e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1158fcf5ef2aSThomas Huth         break;
1159fcf5ef2aSThomas Huth     default:
1160fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1161fcf5ef2aSThomas Huth         break;
1162fcf5ef2aSThomas Huth     }
1163fcf5ef2aSThomas Huth }
1164fcf5ef2aSThomas Huth 
11650f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1166fcf5ef2aSThomas Huth {
11670f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1168e956caf2SEdgar E. Iglesias 
11690f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
11709b158558SRichard Henderson                         cpu_btaken, zero,
1171e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1172e956caf2SEdgar E. Iglesias 
11730f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1174fcf5ef2aSThomas Huth }
1175fcf5ef2aSThomas Huth 
1176f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1177f91c60f0SEdgar E. Iglesias {
1178f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1179f91c60f0SEdgar E. Iglesias 
1180f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1181f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1182f91c60f0SEdgar E. Iglesias 
1183f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1184f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1185f91c60f0SEdgar E. Iglesias }
1186f91c60f0SEdgar E. Iglesias 
1187fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1188fcf5ef2aSThomas Huth {
1189fcf5ef2aSThomas Huth     unsigned int cc;
1190fcf5ef2aSThomas Huth     unsigned int dslot;
1191fcf5ef2aSThomas Huth 
1192fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1193fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1194fcf5ef2aSThomas Huth     LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1195fcf5ef2aSThomas Huth 
1196fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1197fcf5ef2aSThomas Huth     if (dslot) {
1198f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1199fcf5ef2aSThomas Huth     }
1200fcf5ef2aSThomas Huth 
1201fcf5ef2aSThomas Huth     if (dec_alu_op_b_is_small_imm(dc)) {
1202fcf5ef2aSThomas Huth         int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend.  */
1203fcf5ef2aSThomas Huth 
12040f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->pc + offset);
1205fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1206fcf5ef2aSThomas Huth         dc->jmp_pc = dc->pc + offset;
1207fcf5ef2aSThomas Huth     } else {
1208fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
12090f96e96bSRichard Henderson         tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1210fcf5ef2aSThomas Huth     }
12119b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1212fcf5ef2aSThomas Huth }
1213fcf5ef2aSThomas Huth 
1214fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1215fcf5ef2aSThomas Huth {
1216fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1217fcf5ef2aSThomas Huth 
1218fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1219fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1220fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1221fcf5ef2aSThomas Huth 
1222fcf5ef2aSThomas Huth     /* Memory barrier.  */
1223fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1224fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1225badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1226badcbf9dSEdgar E. Iglesias 
12276f3c458bSEdgar E. Iglesias         LOG_DIS("mbar %d\n", mbar_imm);
12286f3c458bSEdgar E. Iglesias 
12293f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12303f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12313f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12323f172744SEdgar E. Iglesias         }
12333f172744SEdgar E. Iglesias 
1234fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1235badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
123641ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1237fcf5ef2aSThomas Huth 
1238fcf5ef2aSThomas Huth             LOG_DIS("sleep\n");
1239fcf5ef2aSThomas Huth 
1240b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1241b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1242b4919e7dSEdgar E. Iglesias                 return;
1243b4919e7dSEdgar E. Iglesias             }
1244b4919e7dSEdgar E. Iglesias 
1245fcf5ef2aSThomas Huth             t_sync_flags(dc);
124641ba37c4SRichard Henderson 
124741ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1248fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1249fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1250fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1251fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
125241ba37c4SRichard Henderson 
125341ba37c4SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
125441ba37c4SRichard Henderson 
125541ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1256fcf5ef2aSThomas Huth             return;
1257fcf5ef2aSThomas Huth         }
1258fcf5ef2aSThomas Huth         /* Break the TB.  */
1259fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1260fcf5ef2aSThomas Huth         return;
1261fcf5ef2aSThomas Huth     }
1262fcf5ef2aSThomas Huth 
1263fcf5ef2aSThomas Huth     LOG_DIS("br%s%s%s%s imm=%x\n",
1264fcf5ef2aSThomas Huth              abs ? "a" : "", link ? "l" : "",
1265fcf5ef2aSThomas Huth              dc->type_b ? "i" : "", dslot ? "d" : "",
1266fcf5ef2aSThomas Huth              dc->imm);
1267fcf5ef2aSThomas Huth 
1268fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1269fcf5ef2aSThomas Huth     if (dslot) {
1270f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1271fcf5ef2aSThomas Huth     }
1272fcf5ef2aSThomas Huth     if (link && dc->rd)
1273cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
1274fcf5ef2aSThomas Huth 
1275fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1276fcf5ef2aSThomas Huth     if (abs) {
12779b158558SRichard Henderson         tcg_gen_movi_i32(cpu_btaken, 1);
12780f96e96bSRichard Henderson         tcg_gen_mov_i32(cpu_btarget, *(dec_alu_op_b(dc)));
1279fcf5ef2aSThomas Huth         if (link && !dslot) {
128041ba37c4SRichard Henderson             if (!(dc->tb_flags & IMM_FLAG) &&
128141ba37c4SRichard Henderson                 (dc->imm == 8 || dc->imm == 0x18)) {
128241ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
128341ba37c4SRichard Henderson             }
1284fcf5ef2aSThomas Huth             if (dc->imm == 0) {
1285bdfc1e88SEdgar E. Iglesias                 if (trap_userspace(dc, true)) {
1286fcf5ef2aSThomas Huth                     return;
1287fcf5ef2aSThomas Huth                 }
128841ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_DEBUG);
1289fcf5ef2aSThomas Huth             }
1290fcf5ef2aSThomas Huth         }
1291fcf5ef2aSThomas Huth     } else {
1292fcf5ef2aSThomas Huth         if (dec_alu_op_b_is_small_imm(dc)) {
1293fcf5ef2aSThomas Huth             dc->jmp = JMP_DIRECT;
1294fcf5ef2aSThomas Huth             dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1295fcf5ef2aSThomas Huth         } else {
12969b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
12970f96e96bSRichard Henderson             tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1298fcf5ef2aSThomas Huth         }
1299fcf5ef2aSThomas Huth     }
1300fcf5ef2aSThomas Huth }
1301fcf5ef2aSThomas Huth 
1302fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1303fcf5ef2aSThomas Huth {
1304cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1305cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1306cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13073e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13080a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13090a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1310cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1311fcf5ef2aSThomas Huth 
1312cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1313cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1314fcf5ef2aSThomas Huth     msr_write(dc, t1);
1315cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1316cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1317fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1318fcf5ef2aSThomas Huth }
1319fcf5ef2aSThomas Huth 
1320fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1321fcf5ef2aSThomas Huth {
1322cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1323cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1324cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13253e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13260a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1327cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1328cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1329fcf5ef2aSThomas Huth 
1330cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1331cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1332fcf5ef2aSThomas Huth     msr_write(dc, t1);
1333cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1334cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1335fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1336fcf5ef2aSThomas Huth }
1337fcf5ef2aSThomas Huth 
1338fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1339fcf5ef2aSThomas Huth {
1340cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1341cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1342cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1343fcf5ef2aSThomas Huth 
13443e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13450a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1346cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1347cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1348cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1349fcf5ef2aSThomas Huth 
1350cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1351cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1352fcf5ef2aSThomas Huth     msr_write(dc, t1);
1353cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1354cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1355fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1356fcf5ef2aSThomas Huth }
1357fcf5ef2aSThomas Huth 
1358fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1359fcf5ef2aSThomas Huth {
1360fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1361fcf5ef2aSThomas Huth 
1362fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1363fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1364fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1365fcf5ef2aSThomas Huth 
1366bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1367bdfc1e88SEdgar E. Iglesias         return;
1368bdfc1e88SEdgar E. Iglesias     }
1369bdfc1e88SEdgar E. Iglesias 
1370f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1371fcf5ef2aSThomas Huth 
1372fcf5ef2aSThomas Huth     if (i_bit) {
1373fcf5ef2aSThomas Huth         LOG_DIS("rtid ir=%x\n", dc->ir);
1374fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1375fcf5ef2aSThomas Huth     } else if (b_bit) {
1376fcf5ef2aSThomas Huth         LOG_DIS("rtbd ir=%x\n", dc->ir);
1377fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1378fcf5ef2aSThomas Huth     } else if (e_bit) {
1379fcf5ef2aSThomas Huth         LOG_DIS("rted ir=%x\n", dc->ir);
1380fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
1381fcf5ef2aSThomas Huth     } else
1382fcf5ef2aSThomas Huth         LOG_DIS("rts ir=%x\n", dc->ir);
1383fcf5ef2aSThomas Huth 
1384fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
13859b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
13860f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1387fcf5ef2aSThomas Huth }
1388fcf5ef2aSThomas Huth 
1389fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1390fcf5ef2aSThomas Huth {
1391fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
139241ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1393fcf5ef2aSThomas Huth     }
13942016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1395fcf5ef2aSThomas Huth }
1396fcf5ef2aSThomas Huth 
1397fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1398fcf5ef2aSThomas Huth {
1399fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1400fcf5ef2aSThomas Huth 
14019ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1402fcf5ef2aSThomas Huth         return;
1403fcf5ef2aSThomas Huth     }
1404fcf5ef2aSThomas Huth 
1405fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1406fcf5ef2aSThomas Huth 
1407fcf5ef2aSThomas Huth     switch (fpu_insn) {
1408fcf5ef2aSThomas Huth         case 0:
1409fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1410fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1411fcf5ef2aSThomas Huth             break;
1412fcf5ef2aSThomas Huth 
1413fcf5ef2aSThomas Huth         case 1:
1414fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1415fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1416fcf5ef2aSThomas Huth             break;
1417fcf5ef2aSThomas Huth 
1418fcf5ef2aSThomas Huth         case 2:
1419fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1420fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1421fcf5ef2aSThomas Huth             break;
1422fcf5ef2aSThomas Huth 
1423fcf5ef2aSThomas Huth         case 3:
1424fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1425fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1426fcf5ef2aSThomas Huth             break;
1427fcf5ef2aSThomas Huth 
1428fcf5ef2aSThomas Huth         case 4:
1429fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1430fcf5ef2aSThomas Huth                 case 0:
1431fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1432fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1433fcf5ef2aSThomas Huth                     break;
1434fcf5ef2aSThomas Huth                 case 1:
1435fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1436fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1437fcf5ef2aSThomas Huth                     break;
1438fcf5ef2aSThomas Huth                 case 2:
1439fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1440fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1441fcf5ef2aSThomas Huth                     break;
1442fcf5ef2aSThomas Huth                 case 3:
1443fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1444fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1445fcf5ef2aSThomas Huth                     break;
1446fcf5ef2aSThomas Huth                 case 4:
1447fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1448fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1449fcf5ef2aSThomas Huth                     break;
1450fcf5ef2aSThomas Huth                 case 5:
1451fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1452fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1453fcf5ef2aSThomas Huth                     break;
1454fcf5ef2aSThomas Huth                 case 6:
1455fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1456fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1457fcf5ef2aSThomas Huth                     break;
1458fcf5ef2aSThomas Huth                 default:
1459fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1460fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1461fcf5ef2aSThomas Huth                                   " opc=%x\n",
1462fcf5ef2aSThomas Huth                                   fpu_insn, dc->pc, dc->opcode);
1463fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1464fcf5ef2aSThomas Huth                     break;
1465fcf5ef2aSThomas Huth             }
1466fcf5ef2aSThomas Huth             break;
1467fcf5ef2aSThomas Huth 
1468fcf5ef2aSThomas Huth         case 5:
1469fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1470fcf5ef2aSThomas Huth                 return;
1471fcf5ef2aSThomas Huth             }
1472fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1473fcf5ef2aSThomas Huth             break;
1474fcf5ef2aSThomas Huth 
1475fcf5ef2aSThomas Huth         case 6:
1476fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1477fcf5ef2aSThomas Huth                 return;
1478fcf5ef2aSThomas Huth             }
1479fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1480fcf5ef2aSThomas Huth             break;
1481fcf5ef2aSThomas Huth 
1482fcf5ef2aSThomas Huth         case 7:
1483fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1484fcf5ef2aSThomas Huth                 return;
1485fcf5ef2aSThomas Huth             }
1486fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1487fcf5ef2aSThomas Huth             break;
1488fcf5ef2aSThomas Huth 
1489fcf5ef2aSThomas Huth         default:
1490fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1491fcf5ef2aSThomas Huth                           " opc=%x\n",
1492fcf5ef2aSThomas Huth                           fpu_insn, dc->pc, dc->opcode);
1493fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1494fcf5ef2aSThomas Huth             break;
1495fcf5ef2aSThomas Huth     }
1496fcf5ef2aSThomas Huth }
1497fcf5ef2aSThomas Huth 
1498fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1499fcf5ef2aSThomas Huth {
15009ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1501fcf5ef2aSThomas Huth         return;
1502fcf5ef2aSThomas Huth     }
1503fcf5ef2aSThomas Huth     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1504fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1505fcf5ef2aSThomas Huth }
1506fcf5ef2aSThomas Huth 
1507fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1508fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1509fcf5ef2aSThomas Huth {
1510fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1511fcf5ef2aSThomas Huth     int ctrl;
1512fcf5ef2aSThomas Huth 
1513fcf5ef2aSThomas Huth     LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1514fcf5ef2aSThomas Huth             dc->type_b ? "" : "d", dc->imm);
1515fcf5ef2aSThomas Huth 
1516bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1517fcf5ef2aSThomas Huth         return;
1518fcf5ef2aSThomas Huth     }
1519fcf5ef2aSThomas Huth 
1520cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1521fcf5ef2aSThomas Huth     if (dc->type_b) {
1522cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1523fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1524fcf5ef2aSThomas Huth     } else {
1525cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1526fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1527fcf5ef2aSThomas Huth     }
1528fcf5ef2aSThomas Huth 
1529cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1530fcf5ef2aSThomas Huth 
1531fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1532fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1533fcf5ef2aSThomas Huth     } else {
1534fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1535fcf5ef2aSThomas Huth     }
1536cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1537cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1538fcf5ef2aSThomas Huth }
1539fcf5ef2aSThomas Huth 
1540fcf5ef2aSThomas Huth static struct decoder_info {
1541fcf5ef2aSThomas Huth     struct {
1542fcf5ef2aSThomas Huth         uint32_t bits;
1543fcf5ef2aSThomas Huth         uint32_t mask;
1544fcf5ef2aSThomas Huth     };
1545fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1546fcf5ef2aSThomas Huth } decinfo[] = {
1547fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1548fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1549fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1550fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1551fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1552fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1553fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1554fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1555fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1556fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1557fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1558fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1559fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1560fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1561fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1562fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1563fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1564fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1565fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1566fcf5ef2aSThomas Huth };
1567fcf5ef2aSThomas Huth 
1568fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1569fcf5ef2aSThomas Huth {
1570fcf5ef2aSThomas Huth     int i;
1571fcf5ef2aSThomas Huth 
1572fcf5ef2aSThomas Huth     dc->ir = ir;
1573fcf5ef2aSThomas Huth     LOG_DIS("%8.8x\t", dc->ir);
1574fcf5ef2aSThomas Huth 
1575462c2544SEdgar E. Iglesias     if (ir == 0) {
15761ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1577462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1578462c2544SEdgar E. Iglesias         return;
1579462c2544SEdgar E. Iglesias     }
1580fcf5ef2aSThomas Huth 
1581fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1582fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1583fcf5ef2aSThomas Huth 
1584fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1585fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1586fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1587fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1588fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1589fcf5ef2aSThomas Huth 
1590fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1591fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1592fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1593fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1594fcf5ef2aSThomas Huth             break;
1595fcf5ef2aSThomas Huth         }
1596fcf5ef2aSThomas Huth     }
1597fcf5ef2aSThomas Huth }
1598fcf5ef2aSThomas Huth 
1599fcf5ef2aSThomas Huth /* generate intermediate code for basic block 'tb'.  */
16008b86d6d2SRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1601fcf5ef2aSThomas Huth {
16029c489ea6SLluís Vilanova     CPUMBState *env = cs->env_ptr;
1603f5c7e93aSRichard Henderson     MicroBlazeCPU *cpu = env_archcpu(env);
1604fcf5ef2aSThomas Huth     uint32_t pc_start;
1605fcf5ef2aSThomas Huth     struct DisasContext ctx;
1606fcf5ef2aSThomas Huth     struct DisasContext *dc = &ctx;
160756371527SEmilio G. Cota     uint32_t page_start, org_flags;
1608cfeea807SEdgar E. Iglesias     uint32_t npc;
1609fcf5ef2aSThomas Huth     int num_insns;
1610fcf5ef2aSThomas Huth 
1611fcf5ef2aSThomas Huth     pc_start = tb->pc;
1612fcf5ef2aSThomas Huth     dc->cpu = cpu;
1613fcf5ef2aSThomas Huth     dc->tb = tb;
1614fcf5ef2aSThomas Huth     org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1615fcf5ef2aSThomas Huth 
1616fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_NEXT;
1617fcf5ef2aSThomas Huth     dc->jmp = 0;
1618fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1619fcf5ef2aSThomas Huth     if (dc->delayed_branch) {
1620fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1621fcf5ef2aSThomas Huth     }
1622fcf5ef2aSThomas Huth     dc->pc = pc_start;
1623fcf5ef2aSThomas Huth     dc->singlestep_enabled = cs->singlestep_enabled;
1624fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1625fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1626fcf5ef2aSThomas Huth 
1627fcf5ef2aSThomas Huth     if (pc_start & 3) {
1628fcf5ef2aSThomas Huth         cpu_abort(cs, "Microblaze: unaligned PC=%x\n", pc_start);
1629fcf5ef2aSThomas Huth     }
1630fcf5ef2aSThomas Huth 
163156371527SEmilio G. Cota     page_start = pc_start & TARGET_PAGE_MASK;
1632fcf5ef2aSThomas Huth     num_insns = 0;
1633fcf5ef2aSThomas Huth 
1634fcf5ef2aSThomas Huth     gen_tb_start(tb);
1635fcf5ef2aSThomas Huth     do
1636fcf5ef2aSThomas Huth     {
1637fcf5ef2aSThomas Huth         tcg_gen_insn_start(dc->pc);
1638fcf5ef2aSThomas Huth         num_insns++;
1639fcf5ef2aSThomas Huth 
1640fcf5ef2aSThomas Huth         if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
164141ba37c4SRichard Henderson             gen_raise_exception_sync(dc, EXCP_DEBUG);
1642fcf5ef2aSThomas Huth             /* The address covered by the breakpoint must be included in
1643fcf5ef2aSThomas Huth                [tb->pc, tb->pc + tb->size) in order to for it to be
1644fcf5ef2aSThomas Huth                properly cleared -- thus we increment the PC here so that
1645fcf5ef2aSThomas Huth                the logic setting tb->size below does the right thing.  */
1646fcf5ef2aSThomas Huth             dc->pc += 4;
1647fcf5ef2aSThomas Huth             break;
1648fcf5ef2aSThomas Huth         }
1649fcf5ef2aSThomas Huth 
1650fcf5ef2aSThomas Huth         /* Pretty disas.  */
1651fcf5ef2aSThomas Huth         LOG_DIS("%8.8x:\t", dc->pc);
1652fcf5ef2aSThomas Huth 
1653c5a49c63SEmilio G. Cota         if (num_insns == max_insns && (tb_cflags(tb) & CF_LAST_IO)) {
1654fcf5ef2aSThomas Huth             gen_io_start();
1655fcf5ef2aSThomas Huth         }
1656fcf5ef2aSThomas Huth 
1657fcf5ef2aSThomas Huth         dc->clear_imm = 1;
1658fcf5ef2aSThomas Huth         decode(dc, cpu_ldl_code(env, dc->pc));
1659fcf5ef2aSThomas Huth         if (dc->clear_imm)
1660fcf5ef2aSThomas Huth             dc->tb_flags &= ~IMM_FLAG;
1661fcf5ef2aSThomas Huth         dc->pc += 4;
1662fcf5ef2aSThomas Huth 
1663fcf5ef2aSThomas Huth         if (dc->delayed_branch) {
1664fcf5ef2aSThomas Huth             dc->delayed_branch--;
1665fcf5ef2aSThomas Huth             if (!dc->delayed_branch) {
1666fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTI_FLAG)
1667fcf5ef2aSThomas Huth                     do_rti(dc);
1668fcf5ef2aSThomas Huth                  if (dc->tb_flags & DRTB_FLAG)
1669fcf5ef2aSThomas Huth                     do_rtb(dc);
1670fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTE_FLAG)
1671fcf5ef2aSThomas Huth                     do_rte(dc);
1672fcf5ef2aSThomas Huth                 /* Clear the delay slot flag.  */
1673fcf5ef2aSThomas Huth                 dc->tb_flags &= ~D_FLAG;
1674fcf5ef2aSThomas Huth                 /* If it is a direct jump, try direct chaining.  */
1675fcf5ef2aSThomas Huth                 if (dc->jmp == JMP_INDIRECT) {
16760f96e96bSRichard Henderson                     TCGv_i32 tmp_pc = tcg_const_i32(dc->pc);
16770f96e96bSRichard Henderson                     eval_cond_jmp(dc, cpu_btarget, tmp_pc);
16780f96e96bSRichard Henderson                     tcg_temp_free_i32(tmp_pc);
1679fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_JUMP;
1680fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT) {
1681fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1682fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1683fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT_CC) {
1684fcf5ef2aSThomas Huth                     TCGLabel *l1 = gen_new_label();
1685fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1686fcf5ef2aSThomas Huth                     /* Conditional jmp.  */
16879b158558SRichard Henderson                     tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1688fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 1, dc->pc);
1689fcf5ef2aSThomas Huth                     gen_set_label(l1);
1690fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1691fcf5ef2aSThomas Huth                 }
1692fcf5ef2aSThomas Huth                 break;
1693fcf5ef2aSThomas Huth             }
1694fcf5ef2aSThomas Huth         }
1695fcf5ef2aSThomas Huth         if (cs->singlestep_enabled) {
1696fcf5ef2aSThomas Huth             break;
1697fcf5ef2aSThomas Huth         }
1698fcf5ef2aSThomas Huth     } while (!dc->is_jmp && !dc->cpustate_changed
1699fcf5ef2aSThomas Huth              && !tcg_op_buf_full()
1700fcf5ef2aSThomas Huth              && !singlestep
170156371527SEmilio G. Cota              && (dc->pc - page_start < TARGET_PAGE_SIZE)
1702fcf5ef2aSThomas Huth              && num_insns < max_insns);
1703fcf5ef2aSThomas Huth 
1704fcf5ef2aSThomas Huth     npc = dc->pc;
1705fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
1706fcf5ef2aSThomas Huth         if (dc->tb_flags & D_FLAG) {
1707fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
17080f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1709fcf5ef2aSThomas Huth             sync_jmpstate(dc);
1710fcf5ef2aSThomas Huth         } else
1711fcf5ef2aSThomas Huth             npc = dc->jmp_pc;
1712fcf5ef2aSThomas Huth     }
1713fcf5ef2aSThomas Huth 
1714fcf5ef2aSThomas Huth     /* Force an update if the per-tb cpu state has changed.  */
1715fcf5ef2aSThomas Huth     if (dc->is_jmp == DISAS_NEXT
1716fcf5ef2aSThomas Huth         && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1717fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
17180f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, npc);
1719fcf5ef2aSThomas Huth     }
1720fcf5ef2aSThomas Huth     t_sync_flags(dc);
1721fcf5ef2aSThomas Huth 
1722a2b80dbdSRichard Henderson     if (dc->is_jmp == DISAS_NORETURN) {
1723a2b80dbdSRichard Henderson         /* nothing more to generate */
1724a2b80dbdSRichard Henderson     } else if (unlikely(cs->singlestep_enabled)) {
1725fcf5ef2aSThomas Huth         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1726fcf5ef2aSThomas Huth 
1727fcf5ef2aSThomas Huth         if (dc->is_jmp != DISAS_JUMP) {
17280f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1729fcf5ef2aSThomas Huth         }
1730fcf5ef2aSThomas Huth         gen_helper_raise_exception(cpu_env, tmp);
1731fcf5ef2aSThomas Huth         tcg_temp_free_i32(tmp);
1732fcf5ef2aSThomas Huth     } else {
1733fcf5ef2aSThomas Huth         switch(dc->is_jmp) {
1734fcf5ef2aSThomas Huth             case DISAS_NEXT:
1735fcf5ef2aSThomas Huth                 gen_goto_tb(dc, 1, npc);
1736fcf5ef2aSThomas Huth                 break;
1737fcf5ef2aSThomas Huth             case DISAS_JUMP:
1738fcf5ef2aSThomas Huth             case DISAS_UPDATE:
1739fcf5ef2aSThomas Huth                 /* indicate that the hash table must be used
1740fcf5ef2aSThomas Huth                    to find the next TB */
174107ea28b4SRichard Henderson                 tcg_gen_exit_tb(NULL, 0);
1742fcf5ef2aSThomas Huth                 break;
1743a2b80dbdSRichard Henderson             default:
1744a2b80dbdSRichard Henderson                 g_assert_not_reached();
1745fcf5ef2aSThomas Huth         }
1746fcf5ef2aSThomas Huth     }
1747fcf5ef2aSThomas Huth     gen_tb_end(tb, num_insns);
1748fcf5ef2aSThomas Huth 
1749fcf5ef2aSThomas Huth     tb->size = dc->pc - pc_start;
1750fcf5ef2aSThomas Huth     tb->icount = num_insns;
1751fcf5ef2aSThomas Huth 
1752fcf5ef2aSThomas Huth #ifdef DEBUG_DISAS
1753fcf5ef2aSThomas Huth #if !SIM_COMPAT
1754fcf5ef2aSThomas Huth     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
1755fcf5ef2aSThomas Huth         && qemu_log_in_addr_range(pc_start)) {
1756fc59d2d8SRobert Foley         FILE *logfile = qemu_log_lock();
1757fcf5ef2aSThomas Huth         qemu_log("--------------\n");
17581d48474dSRichard Henderson         log_target_disas(cs, pc_start, dc->pc - pc_start);
1759fc59d2d8SRobert Foley         qemu_log_unlock(logfile);
1760fcf5ef2aSThomas Huth     }
1761fcf5ef2aSThomas Huth #endif
1762fcf5ef2aSThomas Huth #endif
1763fcf5ef2aSThomas Huth     assert(!dc->abort_at_next_insn);
1764fcf5ef2aSThomas Huth }
1765fcf5ef2aSThomas Huth 
176690c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1767fcf5ef2aSThomas Huth {
1768fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1769fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1770fcf5ef2aSThomas Huth     int i;
1771fcf5ef2aSThomas Huth 
177290c84c56SMarkus Armbruster     if (!env) {
1773fcf5ef2aSThomas Huth         return;
177490c84c56SMarkus Armbruster     }
1775fcf5ef2aSThomas Huth 
17760f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
177776e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
17786efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1779eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
178078e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1781eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
17820f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1783fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
17842e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
17852e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
17862e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
17872e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
17882ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
17892ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
17902ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
17912ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
17922ead1b18SJoe Komlodi         }
17932ead1b18SJoe Komlodi     }
1794fcf5ef2aSThomas Huth 
17952ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
179639db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1797af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
17982ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1799fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
180090c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1801fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
180290c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1803fcf5ef2aSThomas Huth         }
180490c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1805fcf5ef2aSThomas Huth }
1806fcf5ef2aSThomas Huth 
1807fcf5ef2aSThomas Huth void mb_tcg_init(void)
1808fcf5ef2aSThomas Huth {
1809480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1810480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1811fcf5ef2aSThomas Huth 
1812480d29a8SRichard Henderson     static const struct {
1813480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1814480d29a8SRichard Henderson     } i32s[] = {
1815480d29a8SRichard Henderson         R(0),  R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1816480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1817480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1818480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1819480d29a8SRichard Henderson 
1820480d29a8SRichard Henderson         SP(pc),
1821480d29a8SRichard Henderson         SP(msr),
18221074c0fbSRichard Henderson         SP(msr_c),
1823480d29a8SRichard Henderson         SP(imm),
1824480d29a8SRichard Henderson         SP(iflags),
1825480d29a8SRichard Henderson         SP(btaken),
1826480d29a8SRichard Henderson         SP(btarget),
1827480d29a8SRichard Henderson         SP(res_val),
1828480d29a8SRichard Henderson     };
1829480d29a8SRichard Henderson 
1830480d29a8SRichard Henderson #undef R
1831480d29a8SRichard Henderson #undef SP
1832480d29a8SRichard Henderson 
1833480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1834480d29a8SRichard Henderson         *i32s[i].var =
1835480d29a8SRichard Henderson           tcg_global_mem_new_i32(cpu_env, i32s[i].ofs, i32s[i].name);
1836fcf5ef2aSThomas Huth     }
183776e8187dSRichard Henderson 
1838480d29a8SRichard Henderson     cpu_res_addr =
1839480d29a8SRichard Henderson         tcg_global_mem_new(cpu_env, offsetof(CPUMBState, res_addr), "res_addr");
1840fcf5ef2aSThomas Huth }
1841fcf5ef2aSThomas Huth 
1842fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1843fcf5ef2aSThomas Huth                           target_ulong *data)
1844fcf5ef2aSThomas Huth {
184576e8187dSRichard Henderson     env->pc = data[0];
1846fcf5ef2aSThomas Huth }
1847