xref: /openbmc/qemu/target/microblaze/translate.c (revision d7ecb757d1991ff0a58dcecf6c8990dbc19636bd)
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 #define EXTRACT_FIELD(src, start, end) \
37fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
38fcf5ef2aSThomas Huth 
3977fc6f5eSLluís Vilanova /* is_jmp field values */
4077fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
4177fc6f5eSLluís Vilanova #define DISAS_UPDATE  DISAS_TARGET_1 /* cpu state was modified dynamically */
4277fc6f5eSLluís Vilanova 
43cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
440f96e96bSRichard Henderson static TCGv_i32 cpu_pc;
453e0e16aeSRichard Henderson static TCGv_i32 cpu_msr;
461074c0fbSRichard Henderson static TCGv_i32 cpu_msr_c;
479b158558SRichard Henderson static TCGv_i32 cpu_imm;
489b158558SRichard Henderson static TCGv_i32 cpu_btaken;
490f96e96bSRichard Henderson static TCGv_i32 cpu_btarget;
509b158558SRichard Henderson static TCGv_i32 cpu_iflags;
519b158558SRichard Henderson static TCGv cpu_res_addr;
529b158558SRichard Henderson static TCGv_i32 cpu_res_val;
53fcf5ef2aSThomas Huth 
54fcf5ef2aSThomas Huth #include "exec/gen-icount.h"
55fcf5ef2aSThomas Huth 
56fcf5ef2aSThomas Huth /* This is the state at translation time.  */
57fcf5ef2aSThomas Huth typedef struct DisasContext {
58d4705ae0SRichard Henderson     DisasContextBase base;
59fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
60fcf5ef2aSThomas Huth 
61fcf5ef2aSThomas Huth     /* Decoder.  */
62fcf5ef2aSThomas Huth     int type_b;
63fcf5ef2aSThomas Huth     uint32_t ir;
64*d7ecb757SRichard Henderson     uint32_t ext_imm;
65fcf5ef2aSThomas Huth     uint8_t opcode;
66fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
67fcf5ef2aSThomas Huth     uint16_t imm;
68fcf5ef2aSThomas Huth 
69fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
70fcf5ef2aSThomas Huth     unsigned int delayed_branch;
71fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
72fcf5ef2aSThomas Huth     unsigned int clear_imm;
73fcf5ef2aSThomas Huth 
74fcf5ef2aSThomas Huth #define JMP_NOJMP     0
75fcf5ef2aSThomas Huth #define JMP_DIRECT    1
76fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
77fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
78fcf5ef2aSThomas Huth     unsigned int jmp;
79fcf5ef2aSThomas Huth     uint32_t jmp_pc;
80fcf5ef2aSThomas Huth 
81fcf5ef2aSThomas Huth     int abort_at_next_insn;
82fcf5ef2aSThomas Huth } DisasContext;
83fcf5ef2aSThomas Huth 
84fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
85fcf5ef2aSThomas Huth {
86fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
87fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
889b158558SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags);
89fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
90fcf5ef2aSThomas Huth     }
91fcf5ef2aSThomas Huth }
92fcf5ef2aSThomas Huth 
9341ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
94fcf5ef2aSThomas Huth {
95fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
96fcf5ef2aSThomas Huth 
97fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
98fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
99d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
100fcf5ef2aSThomas Huth }
101fcf5ef2aSThomas Huth 
10241ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
10341ba37c4SRichard Henderson {
10441ba37c4SRichard Henderson     t_sync_flags(dc);
105d4705ae0SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
10641ba37c4SRichard Henderson     gen_raise_exception(dc, index);
10741ba37c4SRichard Henderson }
10841ba37c4SRichard Henderson 
10941ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
11041ba37c4SRichard Henderson {
11141ba37c4SRichard Henderson     TCGv_i32 tmp = tcg_const_i32(esr_ec);
11241ba37c4SRichard Henderson     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, esr));
11341ba37c4SRichard Henderson     tcg_temp_free_i32(tmp);
11441ba37c4SRichard Henderson 
11541ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
11641ba37c4SRichard Henderson }
11741ba37c4SRichard Henderson 
118fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
119fcf5ef2aSThomas Huth {
120fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
121d4705ae0SRichard Henderson     return (dc->base.pc_first & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
122fcf5ef2aSThomas Huth #else
123fcf5ef2aSThomas Huth     return true;
124fcf5ef2aSThomas Huth #endif
125fcf5ef2aSThomas Huth }
126fcf5ef2aSThomas Huth 
127fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
128fcf5ef2aSThomas Huth {
129d4705ae0SRichard Henderson     if (dc->base.singlestep_enabled) {
1300b46fa08SRichard Henderson         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1310b46fa08SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
1320b46fa08SRichard Henderson         gen_helper_raise_exception(cpu_env, tmp);
1330b46fa08SRichard Henderson         tcg_temp_free_i32(tmp);
1340b46fa08SRichard Henderson     } else if (use_goto_tb(dc, dest)) {
135fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1360f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
137d4705ae0SRichard Henderson         tcg_gen_exit_tb(dc->base.tb, n);
138fcf5ef2aSThomas Huth     } else {
1390f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
14007ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
141fcf5ef2aSThomas Huth     }
142d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
143fcf5ef2aSThomas Huth }
144fcf5ef2aSThomas Huth 
145bdfc1e88SEdgar E. Iglesias /*
1469ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1479ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1489ba8cd45SEdgar E. Iglesias  */
1499ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1509ba8cd45SEdgar E. Iglesias {
1519ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1525143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
15341ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1549ba8cd45SEdgar E. Iglesias     }
1559ba8cd45SEdgar E. Iglesias     return cond;
1569ba8cd45SEdgar E. Iglesias }
1579ba8cd45SEdgar E. Iglesias 
1589ba8cd45SEdgar E. Iglesias /*
159bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
160bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
161bdfc1e88SEdgar E. Iglesias  */
162bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
163bdfc1e88SEdgar E. Iglesias {
164bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
165bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
166bdfc1e88SEdgar E. Iglesias 
167bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
16841ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
169bdfc1e88SEdgar E. Iglesias     }
170bdfc1e88SEdgar E. Iglesias     return cond_user;
171bdfc1e88SEdgar E. Iglesias }
172bdfc1e88SEdgar E. Iglesias 
173*d7ecb757SRichard Henderson static int32_t dec_alu_typeb_imm(DisasContext *dc)
174fcf5ef2aSThomas Huth {
175*d7ecb757SRichard Henderson     tcg_debug_assert(dc->type_b);
176*d7ecb757SRichard Henderson     if (dc->tb_flags & IMM_FLAG) {
177*d7ecb757SRichard Henderson         return dc->ext_imm | dc->imm;
178*d7ecb757SRichard Henderson     } else {
179*d7ecb757SRichard Henderson         return (int16_t)dc->imm;
180*d7ecb757SRichard Henderson     }
181fcf5ef2aSThomas Huth }
182fcf5ef2aSThomas Huth 
183cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
184fcf5ef2aSThomas Huth {
185fcf5ef2aSThomas Huth     if (dc->type_b) {
186*d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_imm, dec_alu_typeb_imm(dc));
1879b158558SRichard Henderson         return &cpu_imm;
188*d7ecb757SRichard Henderson     }
189fcf5ef2aSThomas Huth     return &cpu_R[dc->rb];
190fcf5ef2aSThomas Huth }
191fcf5ef2aSThomas Huth 
192fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
193fcf5ef2aSThomas Huth {
194fcf5ef2aSThomas Huth     unsigned int k, c;
195cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
196fcf5ef2aSThomas Huth 
197fcf5ef2aSThomas Huth     k = dc->opcode & 4;
198fcf5ef2aSThomas Huth     c = dc->opcode & 2;
199fcf5ef2aSThomas Huth 
200fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
201fcf5ef2aSThomas Huth     if (k) {
202fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
203fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
204fcf5ef2aSThomas Huth         if (dc->rd) {
205cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
206fcf5ef2aSThomas Huth 
207fcf5ef2aSThomas Huth             if (c) {
208fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2091074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
210fcf5ef2aSThomas Huth             }
211fcf5ef2aSThomas Huth         }
212fcf5ef2aSThomas Huth         return;
213fcf5ef2aSThomas Huth     }
214fcf5ef2aSThomas Huth 
215fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
216fcf5ef2aSThomas Huth     /* Extract carry.  */
217cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
218fcf5ef2aSThomas Huth     if (c) {
2191074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
220fcf5ef2aSThomas Huth     } else {
221cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
222fcf5ef2aSThomas Huth     }
223fcf5ef2aSThomas Huth 
2241074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
225fcf5ef2aSThomas Huth     if (dc->rd) {
226cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
227cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
228fcf5ef2aSThomas Huth     }
229cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
230fcf5ef2aSThomas Huth }
231fcf5ef2aSThomas Huth 
232fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
233fcf5ef2aSThomas Huth {
234fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
235cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
236fcf5ef2aSThomas Huth 
237fcf5ef2aSThomas Huth     u = dc->imm & 2;
238fcf5ef2aSThomas Huth     k = dc->opcode & 4;
239fcf5ef2aSThomas Huth     c = dc->opcode & 2;
240fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
241fcf5ef2aSThomas Huth 
242fcf5ef2aSThomas Huth     if (cmp) {
243fcf5ef2aSThomas Huth         if (dc->rd) {
244fcf5ef2aSThomas Huth             if (u)
245fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
246fcf5ef2aSThomas Huth             else
247fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
248fcf5ef2aSThomas Huth         }
249fcf5ef2aSThomas Huth         return;
250fcf5ef2aSThomas Huth     }
251fcf5ef2aSThomas Huth 
252fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
253fcf5ef2aSThomas Huth     if (k) {
254fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
255fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
256fcf5ef2aSThomas Huth         if (dc->rd) {
257cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
258fcf5ef2aSThomas Huth 
259fcf5ef2aSThomas Huth             if (c) {
260fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2611074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
262fcf5ef2aSThomas Huth             }
263fcf5ef2aSThomas Huth         }
264fcf5ef2aSThomas Huth         return;
265fcf5ef2aSThomas Huth     }
266fcf5ef2aSThomas Huth 
267fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
268fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
269cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
270cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
271fcf5ef2aSThomas Huth     if (c) {
2721074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
273fcf5ef2aSThomas Huth     } else {
274cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
275fcf5ef2aSThomas Huth     }
276fcf5ef2aSThomas Huth 
277fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
278cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
279fcf5ef2aSThomas Huth 
2801074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, na, *(dec_alu_op_b(dc)), cf);
281fcf5ef2aSThomas Huth     if (dc->rd) {
282cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
283cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
284fcf5ef2aSThomas Huth     }
285cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
286cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
287fcf5ef2aSThomas Huth }
288fcf5ef2aSThomas Huth 
289fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
290fcf5ef2aSThomas Huth {
291fcf5ef2aSThomas Huth     unsigned int mode;
292fcf5ef2aSThomas Huth 
2939ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
2949ba8cd45SEdgar E. Iglesias         return;
295fcf5ef2aSThomas Huth     }
296fcf5ef2aSThomas Huth 
297fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
298fcf5ef2aSThomas Huth     switch (mode) {
299fcf5ef2aSThomas Huth         case 0:
300fcf5ef2aSThomas Huth             /* pcmpbf.  */
301fcf5ef2aSThomas Huth             if (dc->rd)
302fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
303fcf5ef2aSThomas Huth             break;
304fcf5ef2aSThomas Huth         case 2:
305fcf5ef2aSThomas Huth             if (dc->rd) {
306cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
307fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
308fcf5ef2aSThomas Huth             }
309fcf5ef2aSThomas Huth             break;
310fcf5ef2aSThomas Huth         case 3:
311fcf5ef2aSThomas Huth             if (dc->rd) {
312cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
313fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
314fcf5ef2aSThomas Huth             }
315fcf5ef2aSThomas Huth             break;
316fcf5ef2aSThomas Huth         default:
317fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
318fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
319fcf5ef2aSThomas Huth             break;
320fcf5ef2aSThomas Huth     }
321fcf5ef2aSThomas Huth }
322fcf5ef2aSThomas Huth 
323fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
324fcf5ef2aSThomas Huth {
325fcf5ef2aSThomas Huth     unsigned int not;
326fcf5ef2aSThomas Huth 
327fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
328fcf5ef2aSThomas Huth         dec_pattern(dc);
329fcf5ef2aSThomas Huth         return;
330fcf5ef2aSThomas Huth     }
331fcf5ef2aSThomas Huth 
332fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
333fcf5ef2aSThomas Huth 
334fcf5ef2aSThomas Huth     if (!dc->rd)
335fcf5ef2aSThomas Huth         return;
336fcf5ef2aSThomas Huth 
337fcf5ef2aSThomas Huth     if (not) {
338cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
339fcf5ef2aSThomas Huth     } else
340cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
341fcf5ef2aSThomas Huth }
342fcf5ef2aSThomas Huth 
343fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
344fcf5ef2aSThomas Huth {
345fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
346fcf5ef2aSThomas Huth         dec_pattern(dc);
347fcf5ef2aSThomas Huth         return;
348fcf5ef2aSThomas Huth     }
349fcf5ef2aSThomas Huth 
350fcf5ef2aSThomas Huth     if (dc->rd)
351cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
352fcf5ef2aSThomas Huth }
353fcf5ef2aSThomas Huth 
354fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
355fcf5ef2aSThomas Huth {
356fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
357fcf5ef2aSThomas Huth         dec_pattern(dc);
358fcf5ef2aSThomas Huth         return;
359fcf5ef2aSThomas Huth     }
360fcf5ef2aSThomas Huth 
361fcf5ef2aSThomas Huth     if (dc->rd)
362cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
363fcf5ef2aSThomas Huth }
364fcf5ef2aSThomas Huth 
3651074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
366fcf5ef2aSThomas Huth {
3671074c0fbSRichard Henderson     TCGv_i32 t;
3681074c0fbSRichard Henderson 
3691074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
3701074c0fbSRichard Henderson     t = tcg_temp_new_i32();
3711074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
3721074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
3731074c0fbSRichard Henderson     tcg_temp_free_i32(t);
374fcf5ef2aSThomas Huth }
375fcf5ef2aSThomas Huth 
3761074c0fbSRichard Henderson static void msr_write(DisasContext *dc, TCGv_i32 v)
377fcf5ef2aSThomas Huth {
378fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
3791074c0fbSRichard Henderson 
3801074c0fbSRichard Henderson     /* Install MSR_C.  */
3811074c0fbSRichard Henderson     tcg_gen_extract_i32(cpu_msr_c, v, 2, 1);
3821074c0fbSRichard Henderson 
3831074c0fbSRichard Henderson     /* Clear MSR_C and MSR_CC; MSR_PVR is not writable, and is always clear. */
3841074c0fbSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~(MSR_C | MSR_CC | MSR_PVR));
385fcf5ef2aSThomas Huth }
386fcf5ef2aSThomas Huth 
387fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
388fcf5ef2aSThomas Huth {
389fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
390cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
3912023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
392f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
393fcf5ef2aSThomas Huth 
3942023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
3952023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
3962023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
397fcf5ef2aSThomas Huth     dc->type_b = 1;
3982023e9a3SEdgar E. Iglesias     if (to) {
399fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
400f0f7e7f7SEdgar E. Iglesias     }
401f0f7e7f7SEdgar E. Iglesias 
402f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
403f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
404f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
405f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
406f0f7e7f7SEdgar E. Iglesias 
407f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4082023e9a3SEdgar E. Iglesias     }
409fcf5ef2aSThomas Huth 
410fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4112023e9a3SEdgar E. Iglesias     if (clrset) {
4122023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
413fcf5ef2aSThomas Huth 
41456837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
415fcf5ef2aSThomas Huth             /* nop??? */
416fcf5ef2aSThomas Huth             return;
417fcf5ef2aSThomas Huth         }
418fcf5ef2aSThomas Huth 
419bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
420fcf5ef2aSThomas Huth             return;
421fcf5ef2aSThomas Huth         }
422fcf5ef2aSThomas Huth 
423fcf5ef2aSThomas Huth         if (dc->rd)
424fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
425fcf5ef2aSThomas Huth 
426cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
427cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
428fcf5ef2aSThomas Huth         msr_read(dc, t0);
429cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
430fcf5ef2aSThomas Huth 
431fcf5ef2aSThomas Huth         if (clr) {
432cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
433cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
434fcf5ef2aSThomas Huth         } else
435cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
436fcf5ef2aSThomas Huth         msr_write(dc, t0);
437cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
438cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
439d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
440d4705ae0SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
441fcf5ef2aSThomas Huth         return;
442fcf5ef2aSThomas Huth     }
443fcf5ef2aSThomas Huth 
444bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
445fcf5ef2aSThomas Huth         return;
446fcf5ef2aSThomas Huth     }
447fcf5ef2aSThomas Huth 
448fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
449fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
450fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
451f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
45205a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
45305a9a651SEdgar E. Iglesias 
454fcf5ef2aSThomas Huth         sr &= 7;
45505a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
45605a9a651SEdgar E. Iglesias         if (to) {
457f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
45805a9a651SEdgar E. Iglesias         } else {
459f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
46005a9a651SEdgar E. Iglesias         }
46105a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
462f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
463fcf5ef2aSThomas Huth         return;
464fcf5ef2aSThomas Huth     }
465fcf5ef2aSThomas Huth #endif
466fcf5ef2aSThomas Huth 
467fcf5ef2aSThomas Huth     if (to) {
468fcf5ef2aSThomas Huth         switch (sr) {
469aa28e6d4SRichard Henderson             case SR_PC:
470fcf5ef2aSThomas Huth                 break;
471aa28e6d4SRichard Henderson             case SR_MSR:
472fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
473fcf5ef2aSThomas Huth                 break;
474351527b7SEdgar E. Iglesias             case SR_EAR:
475dbdb77c4SRichard Henderson                 {
476dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
477dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
478dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
479dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
480dbdb77c4SRichard Henderson                 }
481aa28e6d4SRichard Henderson                 break;
482351527b7SEdgar E. Iglesias             case SR_ESR:
48341ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
48441ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
485aa28e6d4SRichard Henderson                 break;
486ab6dd380SEdgar E. Iglesias             case SR_FSR:
48786017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
48886017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
489aa28e6d4SRichard Henderson                 break;
490aa28e6d4SRichard Henderson             case SR_BTR:
491ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
492ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
493aa28e6d4SRichard Henderson                 break;
494aa28e6d4SRichard Henderson             case SR_EDR:
49539db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
49639db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
497fcf5ef2aSThomas Huth                 break;
498fcf5ef2aSThomas Huth             case 0x800:
499cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
500cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
501fcf5ef2aSThomas Huth                 break;
502fcf5ef2aSThomas Huth             case 0x802:
503cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
504cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
505fcf5ef2aSThomas Huth                 break;
506fcf5ef2aSThomas Huth             default:
507fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
508fcf5ef2aSThomas Huth                 break;
509fcf5ef2aSThomas Huth         }
510fcf5ef2aSThomas Huth     } else {
511fcf5ef2aSThomas Huth         switch (sr) {
512aa28e6d4SRichard Henderson             case SR_PC:
513d4705ae0SRichard Henderson                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
514fcf5ef2aSThomas Huth                 break;
515aa28e6d4SRichard Henderson             case SR_MSR:
516fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
517fcf5ef2aSThomas Huth                 break;
518351527b7SEdgar E. Iglesias             case SR_EAR:
519dbdb77c4SRichard Henderson                 {
520dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
521dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
522a1b48e3aSEdgar E. Iglesias                     if (extended) {
523dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
524aa28e6d4SRichard Henderson                     } else {
525dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
526dbdb77c4SRichard Henderson                     }
527dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
528a1b48e3aSEdgar E. Iglesias                 }
529aa28e6d4SRichard Henderson                 break;
530351527b7SEdgar E. Iglesias             case SR_ESR:
53141ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
53241ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
533aa28e6d4SRichard Henderson                 break;
534351527b7SEdgar E. Iglesias             case SR_FSR:
53586017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
53686017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
537aa28e6d4SRichard Henderson                 break;
538351527b7SEdgar E. Iglesias             case SR_BTR:
539ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
540ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
541aa28e6d4SRichard Henderson                 break;
5427cdae31dSTong Ho             case SR_EDR:
54339db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
54439db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
545fcf5ef2aSThomas Huth                 break;
546fcf5ef2aSThomas Huth             case 0x800:
547cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
548cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
549fcf5ef2aSThomas Huth                 break;
550fcf5ef2aSThomas Huth             case 0x802:
551cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
552cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
553fcf5ef2aSThomas Huth                 break;
554351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
555fcf5ef2aSThomas Huth                 rn = sr & 0xf;
556cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
557fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
558fcf5ef2aSThomas Huth                 break;
559fcf5ef2aSThomas Huth             default:
560fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
561fcf5ef2aSThomas Huth                 break;
562fcf5ef2aSThomas Huth         }
563fcf5ef2aSThomas Huth     }
564fcf5ef2aSThomas Huth 
565fcf5ef2aSThomas Huth     if (dc->rd == 0) {
566cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
567fcf5ef2aSThomas Huth     }
568fcf5ef2aSThomas Huth }
569fcf5ef2aSThomas Huth 
570fcf5ef2aSThomas Huth /* Multiplier unit.  */
571fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
572fcf5ef2aSThomas Huth {
573cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
574fcf5ef2aSThomas Huth     unsigned int subcode;
575fcf5ef2aSThomas Huth 
5769ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
577fcf5ef2aSThomas Huth         return;
578fcf5ef2aSThomas Huth     }
579fcf5ef2aSThomas Huth 
580fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
581fcf5ef2aSThomas Huth 
582fcf5ef2aSThomas Huth     if (dc->type_b) {
583cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
584fcf5ef2aSThomas Huth         return;
585fcf5ef2aSThomas Huth     }
586fcf5ef2aSThomas Huth 
587fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
5889b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
589fcf5ef2aSThomas Huth         /* nop??? */
590fcf5ef2aSThomas Huth     }
591fcf5ef2aSThomas Huth 
592cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
593fcf5ef2aSThomas Huth     switch (subcode) {
594fcf5ef2aSThomas Huth         case 0:
595cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
596fcf5ef2aSThomas Huth             break;
597fcf5ef2aSThomas Huth         case 1:
598cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
599cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
600fcf5ef2aSThomas Huth             break;
601fcf5ef2aSThomas Huth         case 2:
602cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
603cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
604fcf5ef2aSThomas Huth             break;
605fcf5ef2aSThomas Huth         case 3:
606cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
607fcf5ef2aSThomas Huth             break;
608fcf5ef2aSThomas Huth         default:
609fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
610fcf5ef2aSThomas Huth             break;
611fcf5ef2aSThomas Huth     }
612cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
613fcf5ef2aSThomas Huth }
614fcf5ef2aSThomas Huth 
615fcf5ef2aSThomas Huth /* Div unit.  */
616fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
617fcf5ef2aSThomas Huth {
618fcf5ef2aSThomas Huth     unsigned int u;
619fcf5ef2aSThomas Huth 
620fcf5ef2aSThomas Huth     u = dc->imm & 2;
621fcf5ef2aSThomas Huth 
6229ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
6239ba8cd45SEdgar E. Iglesias         return;
624fcf5ef2aSThomas Huth     }
625fcf5ef2aSThomas Huth 
626fcf5ef2aSThomas Huth     if (u)
627fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
628fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
629fcf5ef2aSThomas Huth     else
630fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
631fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
632fcf5ef2aSThomas Huth     if (!dc->rd)
633cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
634fcf5ef2aSThomas Huth }
635fcf5ef2aSThomas Huth 
636fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
637fcf5ef2aSThomas Huth {
638cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
639faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
640d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
641fcf5ef2aSThomas Huth 
6429ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
643fcf5ef2aSThomas Huth         return;
644fcf5ef2aSThomas Huth     }
645fcf5ef2aSThomas Huth 
646faa48d74SEdgar E. Iglesias     if (dc->type_b) {
647faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
648d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
649faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
650faa48d74SEdgar E. Iglesias     }
651e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
652e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
653faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
654faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
655fcf5ef2aSThomas Huth 
656faa48d74SEdgar E. Iglesias     if (e) {
657faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
658faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
659faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
660faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
661faa48d74SEdgar E. Iglesias         } else {
662faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
663faa48d74SEdgar E. Iglesias         }
664d09b2585SEdgar E. Iglesias     } else if (i) {
665d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
666d09b2585SEdgar E. Iglesias 
667d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
668d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
669d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
670d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
671d09b2585SEdgar E. Iglesias         } else {
672d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
673d09b2585SEdgar E. Iglesias                                 imm_s, width);
674d09b2585SEdgar E. Iglesias         }
675faa48d74SEdgar E. Iglesias     } else {
676cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
677fcf5ef2aSThomas Huth 
678cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
679cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
680fcf5ef2aSThomas Huth 
6812acf6d53SEdgar E. Iglesias         if (s) {
682cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
6832acf6d53SEdgar E. Iglesias         } else {
6842acf6d53SEdgar E. Iglesias             if (t) {
685cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
6862acf6d53SEdgar E. Iglesias             } else {
687cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
688fcf5ef2aSThomas Huth             }
689fcf5ef2aSThomas Huth         }
690cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
6912acf6d53SEdgar E. Iglesias     }
692faa48d74SEdgar E. Iglesias }
693fcf5ef2aSThomas Huth 
694fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
695fcf5ef2aSThomas Huth {
696fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
697cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
698fcf5ef2aSThomas Huth     unsigned int op;
699fcf5ef2aSThomas Huth 
700fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
701fcf5ef2aSThomas Huth     switch (op) {
702fcf5ef2aSThomas Huth         case 0x21:
703fcf5ef2aSThomas Huth             /* src.  */
704cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
705fcf5ef2aSThomas Huth 
7061074c0fbSRichard Henderson             tcg_gen_shli_i32(t0, cpu_msr_c, 31);
7071074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
708fcf5ef2aSThomas Huth             if (dc->rd) {
709cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
710cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
711fcf5ef2aSThomas Huth             }
712cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
713fcf5ef2aSThomas Huth             break;
714fcf5ef2aSThomas Huth 
715fcf5ef2aSThomas Huth         case 0x1:
716fcf5ef2aSThomas Huth         case 0x41:
717fcf5ef2aSThomas Huth             /* srl.  */
7181074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
719fcf5ef2aSThomas Huth             if (dc->rd) {
720fcf5ef2aSThomas Huth                 if (op == 0x41)
721cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
722fcf5ef2aSThomas Huth                 else
723cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
724fcf5ef2aSThomas Huth             }
725fcf5ef2aSThomas Huth             break;
726fcf5ef2aSThomas Huth         case 0x60:
727fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
728fcf5ef2aSThomas Huth             break;
729fcf5ef2aSThomas Huth         case 0x61:
730fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
731fcf5ef2aSThomas Huth             break;
732fcf5ef2aSThomas Huth         case 0x64:
733fcf5ef2aSThomas Huth         case 0x66:
734fcf5ef2aSThomas Huth         case 0x74:
735fcf5ef2aSThomas Huth         case 0x76:
736fcf5ef2aSThomas Huth             /* wdc.  */
737bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
738fcf5ef2aSThomas Huth             break;
739fcf5ef2aSThomas Huth         case 0x68:
740fcf5ef2aSThomas Huth             /* wic.  */
741bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
742fcf5ef2aSThomas Huth             break;
743fcf5ef2aSThomas Huth         case 0xe0:
7449ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
7459ba8cd45SEdgar E. Iglesias                 return;
746fcf5ef2aSThomas Huth             }
7478fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
7485318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
749fcf5ef2aSThomas Huth             }
750fcf5ef2aSThomas Huth             break;
751fcf5ef2aSThomas Huth         case 0x1e0:
752fcf5ef2aSThomas Huth             /* swapb */
753fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
754fcf5ef2aSThomas Huth             break;
755fcf5ef2aSThomas Huth         case 0x1e2:
756fcf5ef2aSThomas Huth             /*swaph */
757fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
758fcf5ef2aSThomas Huth             break;
759fcf5ef2aSThomas Huth         default:
760fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
761d4705ae0SRichard Henderson                       (uint32_t)dc->base.pc_next, op, dc->rd, dc->ra, dc->rb);
762fcf5ef2aSThomas Huth             break;
763fcf5ef2aSThomas Huth     }
764fcf5ef2aSThomas Huth }
765fcf5ef2aSThomas Huth 
766fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
767fcf5ef2aSThomas Huth {
768fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
769fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
7709b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
771fcf5ef2aSThomas Huth         }
772fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
7730f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
774fcf5ef2aSThomas Huth     }
775fcf5ef2aSThomas Huth }
776fcf5ef2aSThomas Huth 
777fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
778fcf5ef2aSThomas Huth {
779*d7ecb757SRichard Henderson     dc->ext_imm = dc->imm << 16;
780*d7ecb757SRichard Henderson     tcg_gen_movi_i32(cpu_imm, dc->ext_imm);
781fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
782fcf5ef2aSThomas Huth     dc->clear_imm = 0;
783fcf5ef2aSThomas Huth }
784fcf5ef2aSThomas Huth 
785d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
786fcf5ef2aSThomas Huth {
7870e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
7880e9033c8SEdgar E. Iglesias     bool stackprot = false;
789403322eaSEdgar E. Iglesias     TCGv_i32 t32;
790fcf5ef2aSThomas Huth 
791fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
792fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
7930e9033c8SEdgar E. Iglesias         stackprot = true;
794fcf5ef2aSThomas Huth     }
795fcf5ef2aSThomas Huth 
796fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
797fcf5ef2aSThomas Huth     if (!dc->type_b) {
798d248e1beSEdgar E. Iglesias         if (ea) {
799d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
800d248e1beSEdgar E. Iglesias 
801d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
802d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
803d248e1beSEdgar E. Iglesias                 return;
804d248e1beSEdgar E. Iglesias             }
805d248e1beSEdgar E. Iglesias 
806d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
807d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
808d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
809d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
810d248e1beSEdgar E. Iglesias             }
811d248e1beSEdgar E. Iglesias             return;
812d248e1beSEdgar E. Iglesias         }
813d248e1beSEdgar E. Iglesias 
8140dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
815fcf5ef2aSThomas Huth         if (dc->ra == 0) {
816403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8170dc4af5cSEdgar E. Iglesias             return;
818fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
819403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
8200dc4af5cSEdgar E. Iglesias             return;
821fcf5ef2aSThomas Huth         }
822fcf5ef2aSThomas Huth 
823fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
8240e9033c8SEdgar E. Iglesias             stackprot = true;
825fcf5ef2aSThomas Huth         }
826fcf5ef2aSThomas Huth 
827403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
828403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
829403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
830403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
831fcf5ef2aSThomas Huth 
832fcf5ef2aSThomas Huth         if (stackprot) {
8330a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
834fcf5ef2aSThomas Huth         }
8350dc4af5cSEdgar E. Iglesias         return;
836fcf5ef2aSThomas Huth     }
837fcf5ef2aSThomas Huth     /* Immediate.  */
838403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
839*d7ecb757SRichard Henderson     tcg_gen_addi_i32(t32, cpu_R[dc->ra], dec_alu_typeb_imm(dc));
840403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
841403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
842fcf5ef2aSThomas Huth 
843fcf5ef2aSThomas Huth     if (stackprot) {
8440a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
845fcf5ef2aSThomas Huth     }
8460dc4af5cSEdgar E. Iglesias     return;
847fcf5ef2aSThomas Huth }
848fcf5ef2aSThomas Huth 
849fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
850fcf5ef2aSThomas Huth {
851403322eaSEdgar E. Iglesias     TCGv_i32 v;
852403322eaSEdgar E. Iglesias     TCGv addr;
8538534063aSEdgar E. Iglesias     unsigned int size;
854d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
855d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
85614776ab5STony Nguyen     MemOp mop;
857fcf5ef2aSThomas Huth 
858fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
859fcf5ef2aSThomas Huth     size = 1 << mop;
860fcf5ef2aSThomas Huth     if (!dc->type_b) {
861d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
8628534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
8638534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
864fcf5ef2aSThomas Huth     }
865fcf5ef2aSThomas Huth     mop |= MO_TE;
866fcf5ef2aSThomas Huth     if (rev) {
867fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
868fcf5ef2aSThomas Huth     }
869fcf5ef2aSThomas Huth 
8709ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
871fcf5ef2aSThomas Huth         return;
872fcf5ef2aSThomas Huth     }
873fcf5ef2aSThomas Huth 
874d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
875d248e1beSEdgar E. Iglesias         return;
876d248e1beSEdgar E. Iglesias     }
877d248e1beSEdgar E. Iglesias 
878fcf5ef2aSThomas Huth     t_sync_flags(dc);
879403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
880d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
881d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
882d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
883fcf5ef2aSThomas Huth 
884fcf5ef2aSThomas Huth     /*
885fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
886fcf5ef2aSThomas Huth      *
887fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
888fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
889fcf5ef2aSThomas Huth      */
890fcf5ef2aSThomas Huth     if (rev && size != 4) {
891fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
892fcf5ef2aSThomas Huth         switch (size) {
893fcf5ef2aSThomas Huth             case 1:
894fcf5ef2aSThomas Huth             {
895a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
896fcf5ef2aSThomas Huth                 break;
897fcf5ef2aSThomas Huth             }
898fcf5ef2aSThomas Huth 
899fcf5ef2aSThomas Huth             case 2:
900fcf5ef2aSThomas Huth                 /* 00 -> 10
901fcf5ef2aSThomas Huth                    10 -> 00.  */
902403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
903fcf5ef2aSThomas Huth                 break;
904fcf5ef2aSThomas Huth             default:
905fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
906fcf5ef2aSThomas Huth                 break;
907fcf5ef2aSThomas Huth         }
908fcf5ef2aSThomas Huth     }
909fcf5ef2aSThomas Huth 
910fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
911fcf5ef2aSThomas Huth     if (ex) {
912403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
913fcf5ef2aSThomas Huth     }
914fcf5ef2aSThomas Huth 
915fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
916fcf5ef2aSThomas Huth     sync_jmpstate(dc);
917fcf5ef2aSThomas Huth 
918fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
919fcf5ef2aSThomas Huth     /*
920fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
921fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
922fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
923fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
924fcf5ef2aSThomas Huth      */
925cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
926d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
927fcf5ef2aSThomas Huth 
9281507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
929a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
930a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
931a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
932a6338015SEdgar E. Iglesias 
933d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
934a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
935a6338015SEdgar E. Iglesias 
936a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
937a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
938a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
939fcf5ef2aSThomas Huth     }
940fcf5ef2aSThomas Huth 
941fcf5ef2aSThomas Huth     if (ex) {
9429b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
9439b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
944fcf5ef2aSThomas Huth     }
945fcf5ef2aSThomas Huth     if (dc->rd) {
946cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
947fcf5ef2aSThomas Huth     }
948cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
949fcf5ef2aSThomas Huth 
950fcf5ef2aSThomas Huth     if (ex) { /* lwx */
951fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
9521074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
953fcf5ef2aSThomas Huth     }
954fcf5ef2aSThomas Huth 
955403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
956fcf5ef2aSThomas Huth }
957fcf5ef2aSThomas Huth 
958fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
959fcf5ef2aSThomas Huth {
960403322eaSEdgar E. Iglesias     TCGv addr;
961fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
962b51b3d43SEdgar E. Iglesias     unsigned int size;
963d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
964d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
96514776ab5STony Nguyen     MemOp mop;
966fcf5ef2aSThomas Huth 
967fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
968fcf5ef2aSThomas Huth     size = 1 << mop;
969fcf5ef2aSThomas Huth     if (!dc->type_b) {
970d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
971b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
972b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
973fcf5ef2aSThomas Huth     }
974fcf5ef2aSThomas Huth     mop |= MO_TE;
975fcf5ef2aSThomas Huth     if (rev) {
976fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
977fcf5ef2aSThomas Huth     }
978fcf5ef2aSThomas Huth 
9799ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
980fcf5ef2aSThomas Huth         return;
981fcf5ef2aSThomas Huth     }
982fcf5ef2aSThomas Huth 
983d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
984d248e1beSEdgar E. Iglesias 
985fcf5ef2aSThomas Huth     t_sync_flags(dc);
986fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
987fcf5ef2aSThomas Huth     sync_jmpstate(dc);
9880dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
989403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
990d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
991d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
992d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
993fcf5ef2aSThomas Huth 
994fcf5ef2aSThomas Huth     if (ex) { /* swx */
995cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
996fcf5ef2aSThomas Huth 
997fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
998403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
999fcf5ef2aSThomas Huth 
10001074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 1);
1001fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
10029b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1003fcf5ef2aSThomas Huth 
1004071cdc67SEdgar E. Iglesias         /*
1005071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1006071cdc67SEdgar E. Iglesias          * the reserved location.
1007071cdc67SEdgar E. Iglesias          */
1008cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1009071cdc67SEdgar E. Iglesias 
10109b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1011071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1012071cdc67SEdgar E. Iglesias                                    mop);
1013071cdc67SEdgar E. Iglesias 
10149b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
10151074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1016cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1017fcf5ef2aSThomas Huth     }
1018fcf5ef2aSThomas Huth 
1019fcf5ef2aSThomas Huth     if (rev && size != 4) {
1020fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1021fcf5ef2aSThomas Huth         switch (size) {
1022fcf5ef2aSThomas Huth             case 1:
1023fcf5ef2aSThomas Huth             {
1024a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1025fcf5ef2aSThomas Huth                 break;
1026fcf5ef2aSThomas Huth             }
1027fcf5ef2aSThomas Huth 
1028fcf5ef2aSThomas Huth             case 2:
1029fcf5ef2aSThomas Huth                 /* 00 -> 10
1030fcf5ef2aSThomas Huth                    10 -> 00.  */
1031fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1032403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1033fcf5ef2aSThomas Huth                 break;
1034fcf5ef2aSThomas Huth             default:
1035fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1036fcf5ef2aSThomas Huth                 break;
1037fcf5ef2aSThomas Huth         }
1038fcf5ef2aSThomas Huth     }
1039071cdc67SEdgar E. Iglesias 
1040071cdc67SEdgar E. Iglesias     if (!ex) {
1041d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1042071cdc67SEdgar E. Iglesias     }
1043fcf5ef2aSThomas Huth 
1044fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
10451507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1046a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1047a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1048a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1049a6338015SEdgar E. Iglesias 
1050d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1051fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1052fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1053fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1054fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1055fcf5ef2aSThomas Huth          *        access.
1056fcf5ef2aSThomas Huth          */
1057a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1058a6338015SEdgar E. Iglesias 
1059a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1060a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1061a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1062fcf5ef2aSThomas Huth     }
1063fcf5ef2aSThomas Huth 
1064fcf5ef2aSThomas Huth     if (ex) {
1065fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1066fcf5ef2aSThomas Huth     }
1067fcf5ef2aSThomas Huth 
1068403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1069fcf5ef2aSThomas Huth }
1070fcf5ef2aSThomas Huth 
1071fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
10729e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1073fcf5ef2aSThomas Huth {
1074d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1075d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1076d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1077d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1078d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1079d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1080d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1081d89b86e9SEdgar E. Iglesias     };
1082d89b86e9SEdgar E. Iglesias 
1083fcf5ef2aSThomas Huth     switch (cc) {
1084fcf5ef2aSThomas Huth     case CC_EQ:
1085fcf5ef2aSThomas Huth     case CC_NE:
1086fcf5ef2aSThomas Huth     case CC_LT:
1087fcf5ef2aSThomas Huth     case CC_LE:
1088fcf5ef2aSThomas Huth     case CC_GE:
1089fcf5ef2aSThomas Huth     case CC_GT:
10909e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1091fcf5ef2aSThomas Huth         break;
1092fcf5ef2aSThomas Huth     default:
1093fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1094fcf5ef2aSThomas Huth         break;
1095fcf5ef2aSThomas Huth     }
1096fcf5ef2aSThomas Huth }
1097fcf5ef2aSThomas Huth 
10980f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1099fcf5ef2aSThomas Huth {
11000f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1101e956caf2SEdgar E. Iglesias 
11020f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
11039b158558SRichard Henderson                         cpu_btaken, zero,
1104e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1105e956caf2SEdgar E. Iglesias 
11060f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1107fcf5ef2aSThomas Huth }
1108fcf5ef2aSThomas Huth 
1109f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1110f91c60f0SEdgar E. Iglesias {
1111f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1112f91c60f0SEdgar E. Iglesias 
1113f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1114f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1115f91c60f0SEdgar E. Iglesias 
1116f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1117f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1118f91c60f0SEdgar E. Iglesias }
1119f91c60f0SEdgar E. Iglesias 
1120fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1121fcf5ef2aSThomas Huth {
1122fcf5ef2aSThomas Huth     unsigned int cc;
1123fcf5ef2aSThomas Huth     unsigned int dslot;
1124fcf5ef2aSThomas Huth 
1125fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1126fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1127fcf5ef2aSThomas Huth 
1128fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1129fcf5ef2aSThomas Huth     if (dslot) {
1130f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1131fcf5ef2aSThomas Huth     }
1132fcf5ef2aSThomas Huth 
1133*d7ecb757SRichard Henderson     if (dc->type_b) {
1134fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1135*d7ecb757SRichard Henderson         dc->jmp_pc = dc->base.pc_next + dec_alu_typeb_imm(dc);
1136*d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
1137fcf5ef2aSThomas Huth     } else {
1138fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1139*d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1140fcf5ef2aSThomas Huth     }
11419b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1142fcf5ef2aSThomas Huth }
1143fcf5ef2aSThomas Huth 
1144fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1145fcf5ef2aSThomas Huth {
1146fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1147fcf5ef2aSThomas Huth 
1148fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1149fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1150fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1151fcf5ef2aSThomas Huth 
1152fcf5ef2aSThomas Huth     /* Memory barrier.  */
1153fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1154fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1155badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1156badcbf9dSEdgar E. Iglesias 
11573f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
11583f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
11593f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
11603f172744SEdgar E. Iglesias         }
11613f172744SEdgar E. Iglesias 
1162fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1163badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
116441ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1165fcf5ef2aSThomas Huth 
1166b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1167b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1168b4919e7dSEdgar E. Iglesias                 return;
1169b4919e7dSEdgar E. Iglesias             }
1170b4919e7dSEdgar E. Iglesias 
1171fcf5ef2aSThomas Huth             t_sync_flags(dc);
117241ba37c4SRichard Henderson 
117341ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1174fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1175fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1176fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1177fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
117841ba37c4SRichard Henderson 
1179d4705ae0SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
118041ba37c4SRichard Henderson 
118141ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1182fcf5ef2aSThomas Huth             return;
1183fcf5ef2aSThomas Huth         }
1184fcf5ef2aSThomas Huth         /* Break the TB.  */
1185fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1186fcf5ef2aSThomas Huth         return;
1187fcf5ef2aSThomas Huth     }
1188fcf5ef2aSThomas Huth 
1189*d7ecb757SRichard Henderson     if (abs && link && !dslot) {
1190*d7ecb757SRichard Henderson         if (dc->type_b) {
1191*d7ecb757SRichard Henderson             /* BRKI */
1192*d7ecb757SRichard Henderson             uint32_t imm = dec_alu_typeb_imm(dc);
1193*d7ecb757SRichard Henderson             if (trap_userspace(dc, imm != 8 && imm != 0x18)) {
1194*d7ecb757SRichard Henderson                 return;
1195*d7ecb757SRichard Henderson             }
1196*d7ecb757SRichard Henderson         } else {
1197*d7ecb757SRichard Henderson             /* BRK */
1198*d7ecb757SRichard Henderson             if (trap_userspace(dc, true)) {
1199*d7ecb757SRichard Henderson                 return;
1200*d7ecb757SRichard Henderson             }
1201*d7ecb757SRichard Henderson         }
1202*d7ecb757SRichard Henderson     }
1203*d7ecb757SRichard Henderson 
1204fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1205fcf5ef2aSThomas Huth     if (dslot) {
1206f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1207fcf5ef2aSThomas Huth     }
1208*d7ecb757SRichard Henderson     if (link && dc->rd) {
1209d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
1210*d7ecb757SRichard Henderson     }
1211fcf5ef2aSThomas Huth 
1212fcf5ef2aSThomas Huth     if (abs) {
1213*d7ecb757SRichard Henderson         if (dc->type_b) {
1214*d7ecb757SRichard Henderson             uint32_t dest = dec_alu_typeb_imm(dc);
1215*d7ecb757SRichard Henderson 
1216*d7ecb757SRichard Henderson             dc->jmp = JMP_DIRECT;
1217*d7ecb757SRichard Henderson             dc->jmp_pc = dest;
1218*d7ecb757SRichard Henderson             tcg_gen_movi_i32(cpu_btarget, dest);
1219fcf5ef2aSThomas Huth             if (link && !dslot) {
1220*d7ecb757SRichard Henderson                 switch (dest) {
1221*d7ecb757SRichard Henderson                 case 8:
1222*d7ecb757SRichard Henderson                 case 0x18:
1223*d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_BREAK);
1224*d7ecb757SRichard Henderson                     break;
1225*d7ecb757SRichard Henderson                 case 0:
1226*d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_DEBUG);
1227*d7ecb757SRichard Henderson                     break;
1228*d7ecb757SRichard Henderson                 }
1229*d7ecb757SRichard Henderson             }
1230*d7ecb757SRichard Henderson         } else {
1231*d7ecb757SRichard Henderson             dc->jmp = JMP_INDIRECT;
1232*d7ecb757SRichard Henderson             tcg_gen_mov_i32(cpu_btarget, cpu_R[dc->rb]);
1233*d7ecb757SRichard Henderson             if (link && !dslot) {
123441ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
123541ba37c4SRichard Henderson             }
1236fcf5ef2aSThomas Huth         }
1237*d7ecb757SRichard Henderson     } else if (dc->type_b) {
1238fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT;
1239*d7ecb757SRichard Henderson         dc->jmp_pc = dc->base.pc_next + dec_alu_typeb_imm(dc);
1240*d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
1241fcf5ef2aSThomas Huth     } else {
1242*d7ecb757SRichard Henderson         dc->jmp = JMP_INDIRECT;
1243*d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1244*d7ecb757SRichard Henderson     }
12459b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
1246fcf5ef2aSThomas Huth }
1247fcf5ef2aSThomas Huth 
1248fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1249fcf5ef2aSThomas Huth {
1250cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1251cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1252cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
12533e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
12540a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
12550a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1256cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1257fcf5ef2aSThomas Huth 
1258cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1259cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1260fcf5ef2aSThomas Huth     msr_write(dc, t1);
1261cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1262cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1263fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1264fcf5ef2aSThomas Huth }
1265fcf5ef2aSThomas Huth 
1266fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1267fcf5ef2aSThomas Huth {
1268cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1269cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1270cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
12713e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
12720a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1273cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1274cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1275fcf5ef2aSThomas Huth 
1276cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1277cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1278fcf5ef2aSThomas Huth     msr_write(dc, t1);
1279cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1280cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1281fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1282fcf5ef2aSThomas Huth }
1283fcf5ef2aSThomas Huth 
1284fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1285fcf5ef2aSThomas Huth {
1286cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1287cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1288cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1289fcf5ef2aSThomas Huth 
12903e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
12910a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1292cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1293cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1294cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1295fcf5ef2aSThomas Huth 
1296cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1297cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1298fcf5ef2aSThomas Huth     msr_write(dc, t1);
1299cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1300cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1301fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1302fcf5ef2aSThomas Huth }
1303fcf5ef2aSThomas Huth 
1304fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1305fcf5ef2aSThomas Huth {
1306fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1307fcf5ef2aSThomas Huth 
1308fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1309fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1310fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1311fcf5ef2aSThomas Huth 
1312bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1313bdfc1e88SEdgar E. Iglesias         return;
1314bdfc1e88SEdgar E. Iglesias     }
1315bdfc1e88SEdgar E. Iglesias 
1316f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1317fcf5ef2aSThomas Huth 
1318fcf5ef2aSThomas Huth     if (i_bit) {
1319fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1320fcf5ef2aSThomas Huth     } else if (b_bit) {
1321fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1322fcf5ef2aSThomas Huth     } else if (e_bit) {
1323fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
132411105d67SRichard Henderson     }
1325fcf5ef2aSThomas Huth 
1326fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
13279b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
13280f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1329fcf5ef2aSThomas Huth }
1330fcf5ef2aSThomas Huth 
1331fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1332fcf5ef2aSThomas Huth {
1333fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
133441ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1335fcf5ef2aSThomas Huth     }
13362016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1337fcf5ef2aSThomas Huth }
1338fcf5ef2aSThomas Huth 
1339fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1340fcf5ef2aSThomas Huth {
1341fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1342fcf5ef2aSThomas Huth 
13439ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1344fcf5ef2aSThomas Huth         return;
1345fcf5ef2aSThomas Huth     }
1346fcf5ef2aSThomas Huth 
1347fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1348fcf5ef2aSThomas Huth 
1349fcf5ef2aSThomas Huth     switch (fpu_insn) {
1350fcf5ef2aSThomas Huth         case 0:
1351fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1352fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1353fcf5ef2aSThomas Huth             break;
1354fcf5ef2aSThomas Huth 
1355fcf5ef2aSThomas Huth         case 1:
1356fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1357fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1358fcf5ef2aSThomas Huth             break;
1359fcf5ef2aSThomas Huth 
1360fcf5ef2aSThomas Huth         case 2:
1361fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1362fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1363fcf5ef2aSThomas Huth             break;
1364fcf5ef2aSThomas Huth 
1365fcf5ef2aSThomas Huth         case 3:
1366fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1367fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1368fcf5ef2aSThomas Huth             break;
1369fcf5ef2aSThomas Huth 
1370fcf5ef2aSThomas Huth         case 4:
1371fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1372fcf5ef2aSThomas Huth                 case 0:
1373fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1374fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1375fcf5ef2aSThomas Huth                     break;
1376fcf5ef2aSThomas Huth                 case 1:
1377fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1378fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1379fcf5ef2aSThomas Huth                     break;
1380fcf5ef2aSThomas Huth                 case 2:
1381fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1382fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1383fcf5ef2aSThomas Huth                     break;
1384fcf5ef2aSThomas Huth                 case 3:
1385fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1386fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1387fcf5ef2aSThomas Huth                     break;
1388fcf5ef2aSThomas Huth                 case 4:
1389fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1390fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1391fcf5ef2aSThomas Huth                     break;
1392fcf5ef2aSThomas Huth                 case 5:
1393fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1394fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1395fcf5ef2aSThomas Huth                     break;
1396fcf5ef2aSThomas Huth                 case 6:
1397fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1398fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1399fcf5ef2aSThomas Huth                     break;
1400fcf5ef2aSThomas Huth                 default:
1401fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1402fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1403fcf5ef2aSThomas Huth                                   " opc=%x\n",
1404d4705ae0SRichard Henderson                                   fpu_insn, (uint32_t)dc->base.pc_next,
1405d4705ae0SRichard Henderson                                   dc->opcode);
1406fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1407fcf5ef2aSThomas Huth                     break;
1408fcf5ef2aSThomas Huth             }
1409fcf5ef2aSThomas Huth             break;
1410fcf5ef2aSThomas Huth 
1411fcf5ef2aSThomas Huth         case 5:
1412fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1413fcf5ef2aSThomas Huth                 return;
1414fcf5ef2aSThomas Huth             }
1415fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1416fcf5ef2aSThomas Huth             break;
1417fcf5ef2aSThomas Huth 
1418fcf5ef2aSThomas Huth         case 6:
1419fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1420fcf5ef2aSThomas Huth                 return;
1421fcf5ef2aSThomas Huth             }
1422fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1423fcf5ef2aSThomas Huth             break;
1424fcf5ef2aSThomas Huth 
1425fcf5ef2aSThomas Huth         case 7:
1426fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1427fcf5ef2aSThomas Huth                 return;
1428fcf5ef2aSThomas Huth             }
1429fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1430fcf5ef2aSThomas Huth             break;
1431fcf5ef2aSThomas Huth 
1432fcf5ef2aSThomas Huth         default:
1433fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1434fcf5ef2aSThomas Huth                           " opc=%x\n",
1435d4705ae0SRichard Henderson                           fpu_insn, (uint32_t)dc->base.pc_next, dc->opcode);
1436fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1437fcf5ef2aSThomas Huth             break;
1438fcf5ef2aSThomas Huth     }
1439fcf5ef2aSThomas Huth }
1440fcf5ef2aSThomas Huth 
1441fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1442fcf5ef2aSThomas Huth {
14439ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1444fcf5ef2aSThomas Huth         return;
1445fcf5ef2aSThomas Huth     }
1446d4705ae0SRichard Henderson     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n",
1447d4705ae0SRichard Henderson                   (uint32_t)dc->base.pc_next, dc->opcode);
1448fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1449fcf5ef2aSThomas Huth }
1450fcf5ef2aSThomas Huth 
1451fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1452fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1453fcf5ef2aSThomas Huth {
1454fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1455fcf5ef2aSThomas Huth     int ctrl;
1456fcf5ef2aSThomas Huth 
1457bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1458fcf5ef2aSThomas Huth         return;
1459fcf5ef2aSThomas Huth     }
1460fcf5ef2aSThomas Huth 
1461cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1462fcf5ef2aSThomas Huth     if (dc->type_b) {
1463cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1464fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1465fcf5ef2aSThomas Huth     } else {
1466cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1467fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1468fcf5ef2aSThomas Huth     }
1469fcf5ef2aSThomas Huth 
1470cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1471fcf5ef2aSThomas Huth 
1472fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1473fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1474fcf5ef2aSThomas Huth     } else {
1475fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1476fcf5ef2aSThomas Huth     }
1477cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1478cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1479fcf5ef2aSThomas Huth }
1480fcf5ef2aSThomas Huth 
1481fcf5ef2aSThomas Huth static struct decoder_info {
1482fcf5ef2aSThomas Huth     struct {
1483fcf5ef2aSThomas Huth         uint32_t bits;
1484fcf5ef2aSThomas Huth         uint32_t mask;
1485fcf5ef2aSThomas Huth     };
1486fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1487fcf5ef2aSThomas Huth } decinfo[] = {
1488fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1489fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1490fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1491fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1492fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1493fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1494fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1495fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1496fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1497fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1498fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1499fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1500fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1501fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1502fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1503fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1504fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1505fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1506fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1507fcf5ef2aSThomas Huth };
1508fcf5ef2aSThomas Huth 
1509fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1510fcf5ef2aSThomas Huth {
1511fcf5ef2aSThomas Huth     int i;
1512fcf5ef2aSThomas Huth 
1513fcf5ef2aSThomas Huth     dc->ir = ir;
1514fcf5ef2aSThomas Huth 
1515462c2544SEdgar E. Iglesias     if (ir == 0) {
15161ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1517462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1518462c2544SEdgar E. Iglesias         return;
1519462c2544SEdgar E. Iglesias     }
1520fcf5ef2aSThomas Huth 
1521fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1522fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1523fcf5ef2aSThomas Huth 
1524fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1525fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1526fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1527fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1528fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1529fcf5ef2aSThomas Huth 
1530fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1531fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1532fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1533fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1534fcf5ef2aSThomas Huth             break;
1535fcf5ef2aSThomas Huth         }
1536fcf5ef2aSThomas Huth     }
1537fcf5ef2aSThomas Huth }
1538fcf5ef2aSThomas Huth 
1539372122e3SRichard Henderson static void mb_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1540fcf5ef2aSThomas Huth {
1541372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1542372122e3SRichard Henderson     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1543372122e3SRichard Henderson     int bound;
1544fcf5ef2aSThomas Huth 
1545fcf5ef2aSThomas Huth     dc->cpu = cpu;
1546372122e3SRichard Henderson     dc->synced_flags = dc->tb_flags = dc->base.tb->flags;
1547fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1548372122e3SRichard Henderson     dc->jmp = dc->delayed_branch ? JMP_INDIRECT : JMP_NOJMP;
1549fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1550fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1551*d7ecb757SRichard Henderson     dc->ext_imm = dc->base.tb->cs_base;
1552fcf5ef2aSThomas Huth 
1553372122e3SRichard Henderson     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1554372122e3SRichard Henderson     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1555fcf5ef2aSThomas Huth }
1556fcf5ef2aSThomas Huth 
1557372122e3SRichard Henderson static void mb_tr_tb_start(DisasContextBase *dcb, CPUState *cs)
1558fcf5ef2aSThomas Huth {
1559fcf5ef2aSThomas Huth }
1560fcf5ef2aSThomas Huth 
1561372122e3SRichard Henderson static void mb_tr_insn_start(DisasContextBase *dcb, CPUState *cs)
1562372122e3SRichard Henderson {
1563372122e3SRichard Henderson     tcg_gen_insn_start(dcb->pc_next);
1564372122e3SRichard Henderson }
1565fcf5ef2aSThomas Huth 
1566372122e3SRichard Henderson static bool mb_tr_breakpoint_check(DisasContextBase *dcb, CPUState *cs,
1567372122e3SRichard Henderson                                    const CPUBreakpoint *bp)
1568372122e3SRichard Henderson {
1569372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1570372122e3SRichard Henderson 
1571372122e3SRichard Henderson     gen_raise_exception_sync(dc, EXCP_DEBUG);
1572372122e3SRichard Henderson 
1573372122e3SRichard Henderson     /*
1574372122e3SRichard Henderson      * The address covered by the breakpoint must be included in
1575372122e3SRichard Henderson      * [tb->pc, tb->pc + tb->size) in order to for it to be
1576372122e3SRichard Henderson      * properly cleared -- thus we increment the PC here so that
1577372122e3SRichard Henderson      * the logic setting tb->size below does the right thing.
1578372122e3SRichard Henderson      */
1579372122e3SRichard Henderson     dc->base.pc_next += 4;
1580372122e3SRichard Henderson     return true;
1581372122e3SRichard Henderson }
1582372122e3SRichard Henderson 
1583372122e3SRichard Henderson static void mb_tr_translate_insn(DisasContextBase *dcb, CPUState *cs)
1584372122e3SRichard Henderson {
1585372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1586372122e3SRichard Henderson     CPUMBState *env = cs->env_ptr;
1587372122e3SRichard Henderson 
1588372122e3SRichard Henderson     /* TODO: This should raise an exception, not terminate qemu. */
1589372122e3SRichard Henderson     if (dc->base.pc_next & 3) {
1590372122e3SRichard Henderson         cpu_abort(cs, "Microblaze: unaligned PC=%x\n",
1591372122e3SRichard Henderson                   (uint32_t)dc->base.pc_next);
1592fcf5ef2aSThomas Huth     }
1593fcf5ef2aSThomas Huth 
1594fcf5ef2aSThomas Huth     dc->clear_imm = 1;
1595d4705ae0SRichard Henderson     decode(dc, cpu_ldl_code(env, dc->base.pc_next));
1596*d7ecb757SRichard Henderson     if (dc->clear_imm && (dc->tb_flags & IMM_FLAG)) {
1597fcf5ef2aSThomas Huth         dc->tb_flags &= ~IMM_FLAG;
1598*d7ecb757SRichard Henderson         tcg_gen_discard_i32(cpu_imm);
1599372122e3SRichard Henderson     }
1600d4705ae0SRichard Henderson     dc->base.pc_next += 4;
1601fcf5ef2aSThomas Huth 
1602372122e3SRichard Henderson     if (dc->delayed_branch && --dc->delayed_branch == 0) {
1603372122e3SRichard Henderson         if (dc->tb_flags & DRTI_FLAG) {
1604fcf5ef2aSThomas Huth             do_rti(dc);
1605372122e3SRichard Henderson         }
1606372122e3SRichard Henderson         if (dc->tb_flags & DRTB_FLAG) {
1607fcf5ef2aSThomas Huth             do_rtb(dc);
1608372122e3SRichard Henderson         }
1609372122e3SRichard Henderson         if (dc->tb_flags & DRTE_FLAG) {
1610fcf5ef2aSThomas Huth             do_rte(dc);
1611372122e3SRichard Henderson         }
1612fcf5ef2aSThomas Huth         /* Clear the delay slot flag.  */
1613fcf5ef2aSThomas Huth         dc->tb_flags &= ~D_FLAG;
1614372122e3SRichard Henderson         dc->base.is_jmp = DISAS_JUMP;
1615372122e3SRichard Henderson     }
1616372122e3SRichard Henderson 
1617372122e3SRichard Henderson     /* Force an exit if the per-tb cpu state has changed.  */
1618372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NEXT && dc->cpustate_changed) {
1619372122e3SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
1620372122e3SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1621372122e3SRichard Henderson     }
1622372122e3SRichard Henderson }
1623372122e3SRichard Henderson 
1624372122e3SRichard Henderson static void mb_tr_tb_stop(DisasContextBase *dcb, CPUState *cs)
1625372122e3SRichard Henderson {
1626372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1627372122e3SRichard Henderson 
1628372122e3SRichard Henderson     assert(!dc->abort_at_next_insn);
1629372122e3SRichard Henderson 
1630372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NORETURN) {
1631372122e3SRichard Henderson         /* We have already exited the TB. */
1632372122e3SRichard Henderson         return;
1633372122e3SRichard Henderson     }
1634372122e3SRichard Henderson 
1635372122e3SRichard Henderson     t_sync_flags(dc);
1636372122e3SRichard Henderson     if (dc->tb_flags & D_FLAG) {
1637372122e3SRichard Henderson         sync_jmpstate(dc);
1638372122e3SRichard Henderson         dc->jmp = JMP_NOJMP;
1639372122e3SRichard Henderson     }
1640372122e3SRichard Henderson 
1641372122e3SRichard Henderson     switch (dc->base.is_jmp) {
1642372122e3SRichard Henderson     case DISAS_TOO_MANY:
1643372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1644372122e3SRichard Henderson         gen_goto_tb(dc, 0, dc->base.pc_next);
1645372122e3SRichard Henderson         return;
1646372122e3SRichard Henderson 
1647372122e3SRichard Henderson     case DISAS_UPDATE:
1648372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1649372122e3SRichard Henderson         if (unlikely(cs->singlestep_enabled)) {
1650372122e3SRichard Henderson             gen_raise_exception(dc, EXCP_DEBUG);
1651372122e3SRichard Henderson         } else {
1652372122e3SRichard Henderson             tcg_gen_exit_tb(NULL, 0);
1653372122e3SRichard Henderson         }
1654372122e3SRichard Henderson         return;
1655372122e3SRichard Henderson 
1656372122e3SRichard Henderson     case DISAS_JUMP:
1657372122e3SRichard Henderson         switch (dc->jmp) {
1658372122e3SRichard Henderson         case JMP_INDIRECT:
1659372122e3SRichard Henderson             {
1660d4705ae0SRichard Henderson                 TCGv_i32 tmp_pc = tcg_const_i32(dc->base.pc_next);
16610f96e96bSRichard Henderson                 eval_cond_jmp(dc, cpu_btarget, tmp_pc);
16620f96e96bSRichard Henderson                 tcg_temp_free_i32(tmp_pc);
1663372122e3SRichard Henderson 
1664372122e3SRichard Henderson                 if (unlikely(cs->singlestep_enabled)) {
1665372122e3SRichard Henderson                     gen_raise_exception(dc, EXCP_DEBUG);
1666372122e3SRichard Henderson                 } else {
1667372122e3SRichard Henderson                     tcg_gen_exit_tb(NULL, 0);
1668372122e3SRichard Henderson                 }
1669372122e3SRichard Henderson             }
1670372122e3SRichard Henderson             return;
1671372122e3SRichard Henderson 
1672372122e3SRichard Henderson         case JMP_DIRECT_CC:
1673372122e3SRichard Henderson             {
1674fcf5ef2aSThomas Huth                 TCGLabel *l1 = gen_new_label();
16759b158558SRichard Henderson                 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1676d4705ae0SRichard Henderson                 gen_goto_tb(dc, 1, dc->base.pc_next);
1677fcf5ef2aSThomas Huth                 gen_set_label(l1);
1678372122e3SRichard Henderson             }
1679372122e3SRichard Henderson             /* fall through */
1680372122e3SRichard Henderson 
1681372122e3SRichard Henderson         case JMP_DIRECT:
1682fcf5ef2aSThomas Huth             gen_goto_tb(dc, 0, dc->jmp_pc);
1683372122e3SRichard Henderson             return;
1684fcf5ef2aSThomas Huth         }
1685372122e3SRichard Henderson         /* fall through */
1686fcf5ef2aSThomas Huth 
1687a2b80dbdSRichard Henderson     default:
1688a2b80dbdSRichard Henderson         g_assert_not_reached();
1689fcf5ef2aSThomas Huth     }
1690fcf5ef2aSThomas Huth }
1691fcf5ef2aSThomas Huth 
1692372122e3SRichard Henderson static void mb_tr_disas_log(const DisasContextBase *dcb, CPUState *cs)
1693372122e3SRichard Henderson {
1694372122e3SRichard Henderson     qemu_log("IN: %s\n", lookup_symbol(dcb->pc_first));
1695372122e3SRichard Henderson     log_target_disas(cs, dcb->pc_first, dcb->tb->size);
1696fcf5ef2aSThomas Huth }
1697372122e3SRichard Henderson 
1698372122e3SRichard Henderson static const TranslatorOps mb_tr_ops = {
1699372122e3SRichard Henderson     .init_disas_context = mb_tr_init_disas_context,
1700372122e3SRichard Henderson     .tb_start           = mb_tr_tb_start,
1701372122e3SRichard Henderson     .insn_start         = mb_tr_insn_start,
1702372122e3SRichard Henderson     .breakpoint_check   = mb_tr_breakpoint_check,
1703372122e3SRichard Henderson     .translate_insn     = mb_tr_translate_insn,
1704372122e3SRichard Henderson     .tb_stop            = mb_tr_tb_stop,
1705372122e3SRichard Henderson     .disas_log          = mb_tr_disas_log,
1706372122e3SRichard Henderson };
1707372122e3SRichard Henderson 
1708372122e3SRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
1709372122e3SRichard Henderson {
1710372122e3SRichard Henderson     DisasContext dc;
1711372122e3SRichard Henderson     translator_loop(&mb_tr_ops, &dc.base, cpu, tb, max_insns);
1712fcf5ef2aSThomas Huth }
1713fcf5ef2aSThomas Huth 
171490c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1715fcf5ef2aSThomas Huth {
1716fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1717fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1718fcf5ef2aSThomas Huth     int i;
1719fcf5ef2aSThomas Huth 
172090c84c56SMarkus Armbruster     if (!env) {
1721fcf5ef2aSThomas Huth         return;
172290c84c56SMarkus Armbruster     }
1723fcf5ef2aSThomas Huth 
17240f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
172576e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
17266efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1727eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
172878e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1729eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
17300f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1731fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
17322e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
17332e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
17342e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
17352e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
17362ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
17372ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
17382ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
17392ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
17402ead1b18SJoe Komlodi         }
17412ead1b18SJoe Komlodi     }
1742fcf5ef2aSThomas Huth 
17432ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
174439db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1745af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
17462ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1747fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
174890c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1749fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
175090c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1751fcf5ef2aSThomas Huth         }
175290c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1753fcf5ef2aSThomas Huth }
1754fcf5ef2aSThomas Huth 
1755fcf5ef2aSThomas Huth void mb_tcg_init(void)
1756fcf5ef2aSThomas Huth {
1757480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1758480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1759fcf5ef2aSThomas Huth 
1760480d29a8SRichard Henderson     static const struct {
1761480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1762480d29a8SRichard Henderson     } i32s[] = {
1763480d29a8SRichard Henderson         R(0),  R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1764480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1765480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1766480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1767480d29a8SRichard Henderson 
1768480d29a8SRichard Henderson         SP(pc),
1769480d29a8SRichard Henderson         SP(msr),
17701074c0fbSRichard Henderson         SP(msr_c),
1771480d29a8SRichard Henderson         SP(imm),
1772480d29a8SRichard Henderson         SP(iflags),
1773480d29a8SRichard Henderson         SP(btaken),
1774480d29a8SRichard Henderson         SP(btarget),
1775480d29a8SRichard Henderson         SP(res_val),
1776480d29a8SRichard Henderson     };
1777480d29a8SRichard Henderson 
1778480d29a8SRichard Henderson #undef R
1779480d29a8SRichard Henderson #undef SP
1780480d29a8SRichard Henderson 
1781480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1782480d29a8SRichard Henderson         *i32s[i].var =
1783480d29a8SRichard Henderson           tcg_global_mem_new_i32(cpu_env, i32s[i].ofs, i32s[i].name);
1784fcf5ef2aSThomas Huth     }
178576e8187dSRichard Henderson 
1786480d29a8SRichard Henderson     cpu_res_addr =
1787480d29a8SRichard Henderson         tcg_global_mem_new(cpu_env, offsetof(CPUMBState, res_addr), "res_addr");
1788fcf5ef2aSThomas Huth }
1789fcf5ef2aSThomas Huth 
1790fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1791fcf5ef2aSThomas Huth                           target_ulong *data)
1792fcf5ef2aSThomas Huth {
179376e8187dSRichard Henderson     env->pc = data[0];
1794fcf5ef2aSThomas Huth }
1795