xref: /openbmc/qemu/target/microblaze/translate.c (revision 44d1432ba25742624bdb9841b634d45b6711dcf8)
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;
64d7ecb757SRichard 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 
84*44d1432bSRichard Henderson /* Include the auto-generated decoder.  */
85*44d1432bSRichard Henderson #include "decode-insns.c.inc"
86*44d1432bSRichard Henderson 
87fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
88fcf5ef2aSThomas Huth {
89fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
90fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
919b158558SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags);
92fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
93fcf5ef2aSThomas Huth     }
94fcf5ef2aSThomas Huth }
95fcf5ef2aSThomas Huth 
9641ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
97fcf5ef2aSThomas Huth {
98fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
99fcf5ef2aSThomas Huth 
100fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
101fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
102d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
103fcf5ef2aSThomas Huth }
104fcf5ef2aSThomas Huth 
10541ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
10641ba37c4SRichard Henderson {
10741ba37c4SRichard Henderson     t_sync_flags(dc);
108d4705ae0SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
10941ba37c4SRichard Henderson     gen_raise_exception(dc, index);
11041ba37c4SRichard Henderson }
11141ba37c4SRichard Henderson 
11241ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
11341ba37c4SRichard Henderson {
11441ba37c4SRichard Henderson     TCGv_i32 tmp = tcg_const_i32(esr_ec);
11541ba37c4SRichard Henderson     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, esr));
11641ba37c4SRichard Henderson     tcg_temp_free_i32(tmp);
11741ba37c4SRichard Henderson 
11841ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
11941ba37c4SRichard Henderson }
12041ba37c4SRichard Henderson 
121fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
122fcf5ef2aSThomas Huth {
123fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
124d4705ae0SRichard Henderson     return (dc->base.pc_first & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
125fcf5ef2aSThomas Huth #else
126fcf5ef2aSThomas Huth     return true;
127fcf5ef2aSThomas Huth #endif
128fcf5ef2aSThomas Huth }
129fcf5ef2aSThomas Huth 
130fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
131fcf5ef2aSThomas Huth {
132d4705ae0SRichard Henderson     if (dc->base.singlestep_enabled) {
1330b46fa08SRichard Henderson         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1340b46fa08SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
1350b46fa08SRichard Henderson         gen_helper_raise_exception(cpu_env, tmp);
1360b46fa08SRichard Henderson         tcg_temp_free_i32(tmp);
1370b46fa08SRichard Henderson     } else if (use_goto_tb(dc, dest)) {
138fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1390f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
140d4705ae0SRichard Henderson         tcg_gen_exit_tb(dc->base.tb, n);
141fcf5ef2aSThomas Huth     } else {
1420f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
14307ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
144fcf5ef2aSThomas Huth     }
145d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
146fcf5ef2aSThomas Huth }
147fcf5ef2aSThomas Huth 
148bdfc1e88SEdgar E. Iglesias /*
1499ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1509ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1519ba8cd45SEdgar E. Iglesias  */
1529ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1539ba8cd45SEdgar E. Iglesias {
1549ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1555143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
15641ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1579ba8cd45SEdgar E. Iglesias     }
1589ba8cd45SEdgar E. Iglesias     return cond;
1599ba8cd45SEdgar E. Iglesias }
1609ba8cd45SEdgar E. Iglesias 
1619ba8cd45SEdgar E. Iglesias /*
162bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
163bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
164bdfc1e88SEdgar E. Iglesias  */
165bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
166bdfc1e88SEdgar E. Iglesias {
167bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
168bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
169bdfc1e88SEdgar E. Iglesias 
170bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
17141ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
172bdfc1e88SEdgar E. Iglesias     }
173bdfc1e88SEdgar E. Iglesias     return cond_user;
174bdfc1e88SEdgar E. Iglesias }
175bdfc1e88SEdgar E. Iglesias 
176d7ecb757SRichard Henderson static int32_t dec_alu_typeb_imm(DisasContext *dc)
177fcf5ef2aSThomas Huth {
178d7ecb757SRichard Henderson     tcg_debug_assert(dc->type_b);
179d7ecb757SRichard Henderson     if (dc->tb_flags & IMM_FLAG) {
180d7ecb757SRichard Henderson         return dc->ext_imm | dc->imm;
181d7ecb757SRichard Henderson     } else {
182d7ecb757SRichard Henderson         return (int16_t)dc->imm;
183d7ecb757SRichard Henderson     }
184fcf5ef2aSThomas Huth }
185fcf5ef2aSThomas Huth 
186cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
187fcf5ef2aSThomas Huth {
188fcf5ef2aSThomas Huth     if (dc->type_b) {
189d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_imm, dec_alu_typeb_imm(dc));
1909b158558SRichard Henderson         return &cpu_imm;
191d7ecb757SRichard Henderson     }
192fcf5ef2aSThomas Huth     return &cpu_R[dc->rb];
193fcf5ef2aSThomas Huth }
194fcf5ef2aSThomas Huth 
195fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
196fcf5ef2aSThomas Huth {
197fcf5ef2aSThomas Huth     unsigned int k, c;
198cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
199fcf5ef2aSThomas Huth 
200fcf5ef2aSThomas Huth     k = dc->opcode & 4;
201fcf5ef2aSThomas Huth     c = dc->opcode & 2;
202fcf5ef2aSThomas Huth 
203fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
204fcf5ef2aSThomas Huth     if (k) {
205fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
206fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
207fcf5ef2aSThomas Huth         if (dc->rd) {
208cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
209fcf5ef2aSThomas Huth 
210fcf5ef2aSThomas Huth             if (c) {
211fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2121074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
213fcf5ef2aSThomas Huth             }
214fcf5ef2aSThomas Huth         }
215fcf5ef2aSThomas Huth         return;
216fcf5ef2aSThomas Huth     }
217fcf5ef2aSThomas Huth 
218fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
219fcf5ef2aSThomas Huth     /* Extract carry.  */
220cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
221fcf5ef2aSThomas Huth     if (c) {
2221074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
223fcf5ef2aSThomas Huth     } else {
224cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
225fcf5ef2aSThomas Huth     }
226fcf5ef2aSThomas Huth 
2271074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
228fcf5ef2aSThomas Huth     if (dc->rd) {
229cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
230cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
231fcf5ef2aSThomas Huth     }
232cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
233fcf5ef2aSThomas Huth }
234fcf5ef2aSThomas Huth 
235fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
236fcf5ef2aSThomas Huth {
237fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
238cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
239fcf5ef2aSThomas Huth 
240fcf5ef2aSThomas Huth     u = dc->imm & 2;
241fcf5ef2aSThomas Huth     k = dc->opcode & 4;
242fcf5ef2aSThomas Huth     c = dc->opcode & 2;
243fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
244fcf5ef2aSThomas Huth 
245fcf5ef2aSThomas Huth     if (cmp) {
246fcf5ef2aSThomas Huth         if (dc->rd) {
247fcf5ef2aSThomas Huth             if (u)
248fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
249fcf5ef2aSThomas Huth             else
250fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
251fcf5ef2aSThomas Huth         }
252fcf5ef2aSThomas Huth         return;
253fcf5ef2aSThomas Huth     }
254fcf5ef2aSThomas Huth 
255fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
256fcf5ef2aSThomas Huth     if (k) {
257fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
258fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
259fcf5ef2aSThomas Huth         if (dc->rd) {
260cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
261fcf5ef2aSThomas Huth 
262fcf5ef2aSThomas Huth             if (c) {
263fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
2641074c0fbSRichard Henderson                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_msr_c);
265fcf5ef2aSThomas Huth             }
266fcf5ef2aSThomas Huth         }
267fcf5ef2aSThomas Huth         return;
268fcf5ef2aSThomas Huth     }
269fcf5ef2aSThomas Huth 
270fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
271fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
272cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
273cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
274fcf5ef2aSThomas Huth     if (c) {
2751074c0fbSRichard Henderson         tcg_gen_mov_i32(cf, cpu_msr_c);
276fcf5ef2aSThomas Huth     } else {
277cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
278fcf5ef2aSThomas Huth     }
279fcf5ef2aSThomas Huth 
280fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
281cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
282fcf5ef2aSThomas Huth 
2831074c0fbSRichard Henderson     gen_helper_carry(cpu_msr_c, na, *(dec_alu_op_b(dc)), cf);
284fcf5ef2aSThomas Huth     if (dc->rd) {
285cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
286cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
287fcf5ef2aSThomas Huth     }
288cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
289cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
290fcf5ef2aSThomas Huth }
291fcf5ef2aSThomas Huth 
292fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
293fcf5ef2aSThomas Huth {
294fcf5ef2aSThomas Huth     unsigned int mode;
295fcf5ef2aSThomas Huth 
2969ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
2979ba8cd45SEdgar E. Iglesias         return;
298fcf5ef2aSThomas Huth     }
299fcf5ef2aSThomas Huth 
300fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
301fcf5ef2aSThomas Huth     switch (mode) {
302fcf5ef2aSThomas Huth         case 0:
303fcf5ef2aSThomas Huth             /* pcmpbf.  */
304fcf5ef2aSThomas Huth             if (dc->rd)
305fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
306fcf5ef2aSThomas Huth             break;
307fcf5ef2aSThomas Huth         case 2:
308fcf5ef2aSThomas Huth             if (dc->rd) {
309cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
310fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
311fcf5ef2aSThomas Huth             }
312fcf5ef2aSThomas Huth             break;
313fcf5ef2aSThomas Huth         case 3:
314fcf5ef2aSThomas Huth             if (dc->rd) {
315cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
316fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
317fcf5ef2aSThomas Huth             }
318fcf5ef2aSThomas Huth             break;
319fcf5ef2aSThomas Huth         default:
320fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
321fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
322fcf5ef2aSThomas Huth             break;
323fcf5ef2aSThomas Huth     }
324fcf5ef2aSThomas Huth }
325fcf5ef2aSThomas Huth 
326fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
327fcf5ef2aSThomas Huth {
328fcf5ef2aSThomas Huth     unsigned int not;
329fcf5ef2aSThomas Huth 
330fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
331fcf5ef2aSThomas Huth         dec_pattern(dc);
332fcf5ef2aSThomas Huth         return;
333fcf5ef2aSThomas Huth     }
334fcf5ef2aSThomas Huth 
335fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
336fcf5ef2aSThomas Huth 
337fcf5ef2aSThomas Huth     if (!dc->rd)
338fcf5ef2aSThomas Huth         return;
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth     if (not) {
341cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
342fcf5ef2aSThomas Huth     } else
343cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
344fcf5ef2aSThomas Huth }
345fcf5ef2aSThomas Huth 
346fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
347fcf5ef2aSThomas Huth {
348fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
349fcf5ef2aSThomas Huth         dec_pattern(dc);
350fcf5ef2aSThomas Huth         return;
351fcf5ef2aSThomas Huth     }
352fcf5ef2aSThomas Huth 
353fcf5ef2aSThomas Huth     if (dc->rd)
354cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
355fcf5ef2aSThomas Huth }
356fcf5ef2aSThomas Huth 
357fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
358fcf5ef2aSThomas Huth {
359fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
360fcf5ef2aSThomas Huth         dec_pattern(dc);
361fcf5ef2aSThomas Huth         return;
362fcf5ef2aSThomas Huth     }
363fcf5ef2aSThomas Huth 
364fcf5ef2aSThomas Huth     if (dc->rd)
365cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
366fcf5ef2aSThomas Huth }
367fcf5ef2aSThomas Huth 
3681074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
369fcf5ef2aSThomas Huth {
3701074c0fbSRichard Henderson     TCGv_i32 t;
3711074c0fbSRichard Henderson 
3721074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
3731074c0fbSRichard Henderson     t = tcg_temp_new_i32();
3741074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
3751074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
3761074c0fbSRichard Henderson     tcg_temp_free_i32(t);
377fcf5ef2aSThomas Huth }
378fcf5ef2aSThomas Huth 
3791074c0fbSRichard Henderson static void msr_write(DisasContext *dc, TCGv_i32 v)
380fcf5ef2aSThomas Huth {
381fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
3821074c0fbSRichard Henderson 
3831074c0fbSRichard Henderson     /* Install MSR_C.  */
3841074c0fbSRichard Henderson     tcg_gen_extract_i32(cpu_msr_c, v, 2, 1);
3851074c0fbSRichard Henderson 
3861074c0fbSRichard Henderson     /* Clear MSR_C and MSR_CC; MSR_PVR is not writable, and is always clear. */
3871074c0fbSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~(MSR_C | MSR_CC | MSR_PVR));
388fcf5ef2aSThomas Huth }
389fcf5ef2aSThomas Huth 
390fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
391fcf5ef2aSThomas Huth {
392fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
393cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
3942023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
395f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
396fcf5ef2aSThomas Huth 
3972023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
3982023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
3992023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
400fcf5ef2aSThomas Huth     dc->type_b = 1;
4012023e9a3SEdgar E. Iglesias     if (to) {
402fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
403f0f7e7f7SEdgar E. Iglesias     }
404f0f7e7f7SEdgar E. Iglesias 
405f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
406f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
407f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
408f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
409f0f7e7f7SEdgar E. Iglesias 
410f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4112023e9a3SEdgar E. Iglesias     }
412fcf5ef2aSThomas Huth 
413fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4142023e9a3SEdgar E. Iglesias     if (clrset) {
4152023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
416fcf5ef2aSThomas Huth 
41756837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
418fcf5ef2aSThomas Huth             /* nop??? */
419fcf5ef2aSThomas Huth             return;
420fcf5ef2aSThomas Huth         }
421fcf5ef2aSThomas Huth 
422bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
423fcf5ef2aSThomas Huth             return;
424fcf5ef2aSThomas Huth         }
425fcf5ef2aSThomas Huth 
426fcf5ef2aSThomas Huth         if (dc->rd)
427fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
428fcf5ef2aSThomas Huth 
429cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
430cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
431fcf5ef2aSThomas Huth         msr_read(dc, t0);
432cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
433fcf5ef2aSThomas Huth 
434fcf5ef2aSThomas Huth         if (clr) {
435cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
436cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
437fcf5ef2aSThomas Huth         } else
438cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
439fcf5ef2aSThomas Huth         msr_write(dc, t0);
440cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
441cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
442d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
443d4705ae0SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
444fcf5ef2aSThomas Huth         return;
445fcf5ef2aSThomas Huth     }
446fcf5ef2aSThomas Huth 
447bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
448fcf5ef2aSThomas Huth         return;
449fcf5ef2aSThomas Huth     }
450fcf5ef2aSThomas Huth 
451fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
452fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
453fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
454f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
45505a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
45605a9a651SEdgar E. Iglesias 
457fcf5ef2aSThomas Huth         sr &= 7;
45805a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
45905a9a651SEdgar E. Iglesias         if (to) {
460f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
46105a9a651SEdgar E. Iglesias         } else {
462f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
46305a9a651SEdgar E. Iglesias         }
46405a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
465f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
466fcf5ef2aSThomas Huth         return;
467fcf5ef2aSThomas Huth     }
468fcf5ef2aSThomas Huth #endif
469fcf5ef2aSThomas Huth 
470fcf5ef2aSThomas Huth     if (to) {
471fcf5ef2aSThomas Huth         switch (sr) {
472aa28e6d4SRichard Henderson             case SR_PC:
473fcf5ef2aSThomas Huth                 break;
474aa28e6d4SRichard Henderson             case SR_MSR:
475fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
476fcf5ef2aSThomas Huth                 break;
477351527b7SEdgar E. Iglesias             case SR_EAR:
478dbdb77c4SRichard Henderson                 {
479dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
480dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
481dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
482dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
483dbdb77c4SRichard Henderson                 }
484aa28e6d4SRichard Henderson                 break;
485351527b7SEdgar E. Iglesias             case SR_ESR:
48641ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
48741ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
488aa28e6d4SRichard Henderson                 break;
489ab6dd380SEdgar E. Iglesias             case SR_FSR:
49086017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
49186017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
492aa28e6d4SRichard Henderson                 break;
493aa28e6d4SRichard Henderson             case SR_BTR:
494ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
495ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
496aa28e6d4SRichard Henderson                 break;
497aa28e6d4SRichard Henderson             case SR_EDR:
49839db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
49939db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
500fcf5ef2aSThomas Huth                 break;
501fcf5ef2aSThomas Huth             case 0x800:
502cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
503cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
504fcf5ef2aSThomas Huth                 break;
505fcf5ef2aSThomas Huth             case 0x802:
506cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
507cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
508fcf5ef2aSThomas Huth                 break;
509fcf5ef2aSThomas Huth             default:
510fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
511fcf5ef2aSThomas Huth                 break;
512fcf5ef2aSThomas Huth         }
513fcf5ef2aSThomas Huth     } else {
514fcf5ef2aSThomas Huth         switch (sr) {
515aa28e6d4SRichard Henderson             case SR_PC:
516d4705ae0SRichard Henderson                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
517fcf5ef2aSThomas Huth                 break;
518aa28e6d4SRichard Henderson             case SR_MSR:
519fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
520fcf5ef2aSThomas Huth                 break;
521351527b7SEdgar E. Iglesias             case SR_EAR:
522dbdb77c4SRichard Henderson                 {
523dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
524dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
525a1b48e3aSEdgar E. Iglesias                     if (extended) {
526dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
527aa28e6d4SRichard Henderson                     } else {
528dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
529dbdb77c4SRichard Henderson                     }
530dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
531a1b48e3aSEdgar E. Iglesias                 }
532aa28e6d4SRichard Henderson                 break;
533351527b7SEdgar E. Iglesias             case SR_ESR:
53441ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
53541ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
536aa28e6d4SRichard Henderson                 break;
537351527b7SEdgar E. Iglesias             case SR_FSR:
53886017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
53986017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
540aa28e6d4SRichard Henderson                 break;
541351527b7SEdgar E. Iglesias             case SR_BTR:
542ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
543ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
544aa28e6d4SRichard Henderson                 break;
5457cdae31dSTong Ho             case SR_EDR:
54639db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
54739db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
548fcf5ef2aSThomas Huth                 break;
549fcf5ef2aSThomas Huth             case 0x800:
550cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
551cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
552fcf5ef2aSThomas Huth                 break;
553fcf5ef2aSThomas Huth             case 0x802:
554cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
555cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
556fcf5ef2aSThomas Huth                 break;
557351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
558fcf5ef2aSThomas Huth                 rn = sr & 0xf;
559cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
560fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
561fcf5ef2aSThomas Huth                 break;
562fcf5ef2aSThomas Huth             default:
563fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
564fcf5ef2aSThomas Huth                 break;
565fcf5ef2aSThomas Huth         }
566fcf5ef2aSThomas Huth     }
567fcf5ef2aSThomas Huth 
568fcf5ef2aSThomas Huth     if (dc->rd == 0) {
569cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
570fcf5ef2aSThomas Huth     }
571fcf5ef2aSThomas Huth }
572fcf5ef2aSThomas Huth 
573fcf5ef2aSThomas Huth /* Multiplier unit.  */
574fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
575fcf5ef2aSThomas Huth {
576cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
577fcf5ef2aSThomas Huth     unsigned int subcode;
578fcf5ef2aSThomas Huth 
5799ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
580fcf5ef2aSThomas Huth         return;
581fcf5ef2aSThomas Huth     }
582fcf5ef2aSThomas Huth 
583fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
584fcf5ef2aSThomas Huth 
585fcf5ef2aSThomas Huth     if (dc->type_b) {
586cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
587fcf5ef2aSThomas Huth         return;
588fcf5ef2aSThomas Huth     }
589fcf5ef2aSThomas Huth 
590fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
5919b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
592fcf5ef2aSThomas Huth         /* nop??? */
593fcf5ef2aSThomas Huth     }
594fcf5ef2aSThomas Huth 
595cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
596fcf5ef2aSThomas Huth     switch (subcode) {
597fcf5ef2aSThomas Huth         case 0:
598cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
599fcf5ef2aSThomas Huth             break;
600fcf5ef2aSThomas Huth         case 1:
601cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
602cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
603fcf5ef2aSThomas Huth             break;
604fcf5ef2aSThomas Huth         case 2:
605cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
606cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
607fcf5ef2aSThomas Huth             break;
608fcf5ef2aSThomas Huth         case 3:
609cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
610fcf5ef2aSThomas Huth             break;
611fcf5ef2aSThomas Huth         default:
612fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
613fcf5ef2aSThomas Huth             break;
614fcf5ef2aSThomas Huth     }
615cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
616fcf5ef2aSThomas Huth }
617fcf5ef2aSThomas Huth 
618fcf5ef2aSThomas Huth /* Div unit.  */
619fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
620fcf5ef2aSThomas Huth {
621fcf5ef2aSThomas Huth     unsigned int u;
622fcf5ef2aSThomas Huth 
623fcf5ef2aSThomas Huth     u = dc->imm & 2;
624fcf5ef2aSThomas Huth 
6259ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
6269ba8cd45SEdgar E. Iglesias         return;
627fcf5ef2aSThomas Huth     }
628fcf5ef2aSThomas Huth 
629fcf5ef2aSThomas Huth     if (u)
630fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
631fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
632fcf5ef2aSThomas Huth     else
633fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
634fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
635fcf5ef2aSThomas Huth     if (!dc->rd)
636cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
637fcf5ef2aSThomas Huth }
638fcf5ef2aSThomas Huth 
639fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
640fcf5ef2aSThomas Huth {
641cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
642faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
643d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
644fcf5ef2aSThomas Huth 
6459ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
646fcf5ef2aSThomas Huth         return;
647fcf5ef2aSThomas Huth     }
648fcf5ef2aSThomas Huth 
649faa48d74SEdgar E. Iglesias     if (dc->type_b) {
650faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
651d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
652faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
653faa48d74SEdgar E. Iglesias     }
654e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
655e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
656faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
657faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
658fcf5ef2aSThomas Huth 
659faa48d74SEdgar E. Iglesias     if (e) {
660faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
661faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
662faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
663faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
664faa48d74SEdgar E. Iglesias         } else {
665faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
666faa48d74SEdgar E. Iglesias         }
667d09b2585SEdgar E. Iglesias     } else if (i) {
668d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
669d09b2585SEdgar E. Iglesias 
670d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
671d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
672d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
673d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
674d09b2585SEdgar E. Iglesias         } else {
675d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
676d09b2585SEdgar E. Iglesias                                 imm_s, width);
677d09b2585SEdgar E. Iglesias         }
678faa48d74SEdgar E. Iglesias     } else {
679cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
680fcf5ef2aSThomas Huth 
681cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
682cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
683fcf5ef2aSThomas Huth 
6842acf6d53SEdgar E. Iglesias         if (s) {
685cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
6862acf6d53SEdgar E. Iglesias         } else {
6872acf6d53SEdgar E. Iglesias             if (t) {
688cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
6892acf6d53SEdgar E. Iglesias             } else {
690cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
691fcf5ef2aSThomas Huth             }
692fcf5ef2aSThomas Huth         }
693cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
6942acf6d53SEdgar E. Iglesias     }
695faa48d74SEdgar E. Iglesias }
696fcf5ef2aSThomas Huth 
697fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
698fcf5ef2aSThomas Huth {
699fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
700cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
701fcf5ef2aSThomas Huth     unsigned int op;
702fcf5ef2aSThomas Huth 
703fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
704fcf5ef2aSThomas Huth     switch (op) {
705fcf5ef2aSThomas Huth         case 0x21:
706fcf5ef2aSThomas Huth             /* src.  */
707cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
708fcf5ef2aSThomas Huth 
7091074c0fbSRichard Henderson             tcg_gen_shli_i32(t0, cpu_msr_c, 31);
7101074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
711fcf5ef2aSThomas Huth             if (dc->rd) {
712cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
713cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
714fcf5ef2aSThomas Huth             }
715cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
716fcf5ef2aSThomas Huth             break;
717fcf5ef2aSThomas Huth 
718fcf5ef2aSThomas Huth         case 0x1:
719fcf5ef2aSThomas Huth         case 0x41:
720fcf5ef2aSThomas Huth             /* srl.  */
7211074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
722fcf5ef2aSThomas Huth             if (dc->rd) {
723fcf5ef2aSThomas Huth                 if (op == 0x41)
724cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
725fcf5ef2aSThomas Huth                 else
726cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
727fcf5ef2aSThomas Huth             }
728fcf5ef2aSThomas Huth             break;
729fcf5ef2aSThomas Huth         case 0x60:
730fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
731fcf5ef2aSThomas Huth             break;
732fcf5ef2aSThomas Huth         case 0x61:
733fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
734fcf5ef2aSThomas Huth             break;
735fcf5ef2aSThomas Huth         case 0x64:
736fcf5ef2aSThomas Huth         case 0x66:
737fcf5ef2aSThomas Huth         case 0x74:
738fcf5ef2aSThomas Huth         case 0x76:
739fcf5ef2aSThomas Huth             /* wdc.  */
740bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
741fcf5ef2aSThomas Huth             break;
742fcf5ef2aSThomas Huth         case 0x68:
743fcf5ef2aSThomas Huth             /* wic.  */
744bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
745fcf5ef2aSThomas Huth             break;
746fcf5ef2aSThomas Huth         case 0xe0:
7479ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
7489ba8cd45SEdgar E. Iglesias                 return;
749fcf5ef2aSThomas Huth             }
7508fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
7515318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
752fcf5ef2aSThomas Huth             }
753fcf5ef2aSThomas Huth             break;
754fcf5ef2aSThomas Huth         case 0x1e0:
755fcf5ef2aSThomas Huth             /* swapb */
756fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
757fcf5ef2aSThomas Huth             break;
758fcf5ef2aSThomas Huth         case 0x1e2:
759fcf5ef2aSThomas Huth             /*swaph */
760fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
761fcf5ef2aSThomas Huth             break;
762fcf5ef2aSThomas Huth         default:
763fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
764d4705ae0SRichard Henderson                       (uint32_t)dc->base.pc_next, op, dc->rd, dc->ra, dc->rb);
765fcf5ef2aSThomas Huth             break;
766fcf5ef2aSThomas Huth     }
767fcf5ef2aSThomas Huth }
768fcf5ef2aSThomas Huth 
769fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
770fcf5ef2aSThomas Huth {
771fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
772fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
7739b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
774fcf5ef2aSThomas Huth         }
775fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
7760f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
777fcf5ef2aSThomas Huth     }
778fcf5ef2aSThomas Huth }
779fcf5ef2aSThomas Huth 
780fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
781fcf5ef2aSThomas Huth {
782d7ecb757SRichard Henderson     dc->ext_imm = dc->imm << 16;
783d7ecb757SRichard Henderson     tcg_gen_movi_i32(cpu_imm, dc->ext_imm);
784fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
785fcf5ef2aSThomas Huth     dc->clear_imm = 0;
786fcf5ef2aSThomas Huth }
787fcf5ef2aSThomas Huth 
788d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
789fcf5ef2aSThomas Huth {
7900e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
7910e9033c8SEdgar E. Iglesias     bool stackprot = false;
792403322eaSEdgar E. Iglesias     TCGv_i32 t32;
793fcf5ef2aSThomas Huth 
794fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
795fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
7960e9033c8SEdgar E. Iglesias         stackprot = true;
797fcf5ef2aSThomas Huth     }
798fcf5ef2aSThomas Huth 
799fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
800fcf5ef2aSThomas Huth     if (!dc->type_b) {
801d248e1beSEdgar E. Iglesias         if (ea) {
802d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
803d248e1beSEdgar E. Iglesias 
804d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
805d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
806d248e1beSEdgar E. Iglesias                 return;
807d248e1beSEdgar E. Iglesias             }
808d248e1beSEdgar E. Iglesias 
809d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
810d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
811d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
812d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
813d248e1beSEdgar E. Iglesias             }
814d248e1beSEdgar E. Iglesias             return;
815d248e1beSEdgar E. Iglesias         }
816d248e1beSEdgar E. Iglesias 
8170dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
818fcf5ef2aSThomas Huth         if (dc->ra == 0) {
819403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
8200dc4af5cSEdgar E. Iglesias             return;
821fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
822403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
8230dc4af5cSEdgar E. Iglesias             return;
824fcf5ef2aSThomas Huth         }
825fcf5ef2aSThomas Huth 
826fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
8270e9033c8SEdgar E. Iglesias             stackprot = true;
828fcf5ef2aSThomas Huth         }
829fcf5ef2aSThomas Huth 
830403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
831403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
832403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
833403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
834fcf5ef2aSThomas Huth 
835fcf5ef2aSThomas Huth         if (stackprot) {
8360a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
837fcf5ef2aSThomas Huth         }
8380dc4af5cSEdgar E. Iglesias         return;
839fcf5ef2aSThomas Huth     }
840fcf5ef2aSThomas Huth     /* Immediate.  */
841403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
842d7ecb757SRichard Henderson     tcg_gen_addi_i32(t32, cpu_R[dc->ra], dec_alu_typeb_imm(dc));
843403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
844403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
845fcf5ef2aSThomas Huth 
846fcf5ef2aSThomas Huth     if (stackprot) {
8470a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
848fcf5ef2aSThomas Huth     }
8490dc4af5cSEdgar E. Iglesias     return;
850fcf5ef2aSThomas Huth }
851fcf5ef2aSThomas Huth 
852fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
853fcf5ef2aSThomas Huth {
854403322eaSEdgar E. Iglesias     TCGv_i32 v;
855403322eaSEdgar E. Iglesias     TCGv addr;
8568534063aSEdgar E. Iglesias     unsigned int size;
857d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
858d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
85914776ab5STony Nguyen     MemOp mop;
860fcf5ef2aSThomas Huth 
861fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
862fcf5ef2aSThomas Huth     size = 1 << mop;
863fcf5ef2aSThomas Huth     if (!dc->type_b) {
864d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
8658534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
8668534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
867fcf5ef2aSThomas Huth     }
868fcf5ef2aSThomas Huth     mop |= MO_TE;
869fcf5ef2aSThomas Huth     if (rev) {
870fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
871fcf5ef2aSThomas Huth     }
872fcf5ef2aSThomas Huth 
8739ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
874fcf5ef2aSThomas Huth         return;
875fcf5ef2aSThomas Huth     }
876fcf5ef2aSThomas Huth 
877d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
878d248e1beSEdgar E. Iglesias         return;
879d248e1beSEdgar E. Iglesias     }
880d248e1beSEdgar E. Iglesias 
881fcf5ef2aSThomas Huth     t_sync_flags(dc);
882403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
883d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
884d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
885d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
886fcf5ef2aSThomas Huth 
887fcf5ef2aSThomas Huth     /*
888fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
889fcf5ef2aSThomas Huth      *
890fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
891fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
892fcf5ef2aSThomas Huth      */
893fcf5ef2aSThomas Huth     if (rev && size != 4) {
894fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
895fcf5ef2aSThomas Huth         switch (size) {
896fcf5ef2aSThomas Huth             case 1:
897fcf5ef2aSThomas Huth             {
898a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
899fcf5ef2aSThomas Huth                 break;
900fcf5ef2aSThomas Huth             }
901fcf5ef2aSThomas Huth 
902fcf5ef2aSThomas Huth             case 2:
903fcf5ef2aSThomas Huth                 /* 00 -> 10
904fcf5ef2aSThomas Huth                    10 -> 00.  */
905403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
906fcf5ef2aSThomas Huth                 break;
907fcf5ef2aSThomas Huth             default:
908fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
909fcf5ef2aSThomas Huth                 break;
910fcf5ef2aSThomas Huth         }
911fcf5ef2aSThomas Huth     }
912fcf5ef2aSThomas Huth 
913fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
914fcf5ef2aSThomas Huth     if (ex) {
915403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
916fcf5ef2aSThomas Huth     }
917fcf5ef2aSThomas Huth 
918fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
919fcf5ef2aSThomas Huth     sync_jmpstate(dc);
920fcf5ef2aSThomas Huth 
921fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
922fcf5ef2aSThomas Huth     /*
923fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
924fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
925fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
926fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
927fcf5ef2aSThomas Huth      */
928cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
929d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
930fcf5ef2aSThomas Huth 
9311507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
932a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
933a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
934a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
935a6338015SEdgar E. Iglesias 
936d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
937a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
938a6338015SEdgar E. Iglesias 
939a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
940a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
941a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
942fcf5ef2aSThomas Huth     }
943fcf5ef2aSThomas Huth 
944fcf5ef2aSThomas Huth     if (ex) {
9459b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
9469b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
947fcf5ef2aSThomas Huth     }
948fcf5ef2aSThomas Huth     if (dc->rd) {
949cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
950fcf5ef2aSThomas Huth     }
951cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
952fcf5ef2aSThomas Huth 
953fcf5ef2aSThomas Huth     if (ex) { /* lwx */
954fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
9551074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
956fcf5ef2aSThomas Huth     }
957fcf5ef2aSThomas Huth 
958403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
959fcf5ef2aSThomas Huth }
960fcf5ef2aSThomas Huth 
961fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
962fcf5ef2aSThomas Huth {
963403322eaSEdgar E. Iglesias     TCGv addr;
964fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
965b51b3d43SEdgar E. Iglesias     unsigned int size;
966d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
967d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
96814776ab5STony Nguyen     MemOp mop;
969fcf5ef2aSThomas Huth 
970fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
971fcf5ef2aSThomas Huth     size = 1 << mop;
972fcf5ef2aSThomas Huth     if (!dc->type_b) {
973d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
974b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
975b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
976fcf5ef2aSThomas Huth     }
977fcf5ef2aSThomas Huth     mop |= MO_TE;
978fcf5ef2aSThomas Huth     if (rev) {
979fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
980fcf5ef2aSThomas Huth     }
981fcf5ef2aSThomas Huth 
9829ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
983fcf5ef2aSThomas Huth         return;
984fcf5ef2aSThomas Huth     }
985fcf5ef2aSThomas Huth 
986d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
987d248e1beSEdgar E. Iglesias 
988fcf5ef2aSThomas Huth     t_sync_flags(dc);
989fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
990fcf5ef2aSThomas Huth     sync_jmpstate(dc);
9910dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
992403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
993d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
994d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
995d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
996fcf5ef2aSThomas Huth 
997fcf5ef2aSThomas Huth     if (ex) { /* swx */
998cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
999fcf5ef2aSThomas Huth 
1000fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1001403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1002fcf5ef2aSThomas Huth 
10031074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 1);
1004fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
10059b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1006fcf5ef2aSThomas Huth 
1007071cdc67SEdgar E. Iglesias         /*
1008071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1009071cdc67SEdgar E. Iglesias          * the reserved location.
1010071cdc67SEdgar E. Iglesias          */
1011cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1012071cdc67SEdgar E. Iglesias 
10139b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1014071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1015071cdc67SEdgar E. Iglesias                                    mop);
1016071cdc67SEdgar E. Iglesias 
10179b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
10181074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1019cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1020fcf5ef2aSThomas Huth     }
1021fcf5ef2aSThomas Huth 
1022fcf5ef2aSThomas Huth     if (rev && size != 4) {
1023fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1024fcf5ef2aSThomas Huth         switch (size) {
1025fcf5ef2aSThomas Huth             case 1:
1026fcf5ef2aSThomas Huth             {
1027a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1028fcf5ef2aSThomas Huth                 break;
1029fcf5ef2aSThomas Huth             }
1030fcf5ef2aSThomas Huth 
1031fcf5ef2aSThomas Huth             case 2:
1032fcf5ef2aSThomas Huth                 /* 00 -> 10
1033fcf5ef2aSThomas Huth                    10 -> 00.  */
1034fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1035403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1036fcf5ef2aSThomas Huth                 break;
1037fcf5ef2aSThomas Huth             default:
1038fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1039fcf5ef2aSThomas Huth                 break;
1040fcf5ef2aSThomas Huth         }
1041fcf5ef2aSThomas Huth     }
1042071cdc67SEdgar E. Iglesias 
1043071cdc67SEdgar E. Iglesias     if (!ex) {
1044d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1045071cdc67SEdgar E. Iglesias     }
1046fcf5ef2aSThomas Huth 
1047fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
10481507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1049a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1050a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1051a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1052a6338015SEdgar E. Iglesias 
1053d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1054fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1055fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1056fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1057fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1058fcf5ef2aSThomas Huth          *        access.
1059fcf5ef2aSThomas Huth          */
1060a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1061a6338015SEdgar E. Iglesias 
1062a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1063a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1064a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1065fcf5ef2aSThomas Huth     }
1066fcf5ef2aSThomas Huth 
1067fcf5ef2aSThomas Huth     if (ex) {
1068fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1069fcf5ef2aSThomas Huth     }
1070fcf5ef2aSThomas Huth 
1071403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1072fcf5ef2aSThomas Huth }
1073fcf5ef2aSThomas Huth 
1074fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
10759e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1076fcf5ef2aSThomas Huth {
1077d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1078d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1079d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1080d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1081d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1082d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1083d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1084d89b86e9SEdgar E. Iglesias     };
1085d89b86e9SEdgar E. Iglesias 
1086fcf5ef2aSThomas Huth     switch (cc) {
1087fcf5ef2aSThomas Huth     case CC_EQ:
1088fcf5ef2aSThomas Huth     case CC_NE:
1089fcf5ef2aSThomas Huth     case CC_LT:
1090fcf5ef2aSThomas Huth     case CC_LE:
1091fcf5ef2aSThomas Huth     case CC_GE:
1092fcf5ef2aSThomas Huth     case CC_GT:
10939e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1094fcf5ef2aSThomas Huth         break;
1095fcf5ef2aSThomas Huth     default:
1096fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1097fcf5ef2aSThomas Huth         break;
1098fcf5ef2aSThomas Huth     }
1099fcf5ef2aSThomas Huth }
1100fcf5ef2aSThomas Huth 
11010f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1102fcf5ef2aSThomas Huth {
11030f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1104e956caf2SEdgar E. Iglesias 
11050f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
11069b158558SRichard Henderson                         cpu_btaken, zero,
1107e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1108e956caf2SEdgar E. Iglesias 
11090f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1110fcf5ef2aSThomas Huth }
1111fcf5ef2aSThomas Huth 
1112f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1113f91c60f0SEdgar E. Iglesias {
1114f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1115f91c60f0SEdgar E. Iglesias 
1116f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1117f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1118f91c60f0SEdgar E. Iglesias 
1119f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1120f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1121f91c60f0SEdgar E. Iglesias }
1122f91c60f0SEdgar E. Iglesias 
1123fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1124fcf5ef2aSThomas Huth {
1125fcf5ef2aSThomas Huth     unsigned int cc;
1126fcf5ef2aSThomas Huth     unsigned int dslot;
1127fcf5ef2aSThomas Huth 
1128fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1129fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1130fcf5ef2aSThomas Huth 
1131fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1132fcf5ef2aSThomas Huth     if (dslot) {
1133f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1134fcf5ef2aSThomas Huth     }
1135fcf5ef2aSThomas Huth 
1136d7ecb757SRichard Henderson     if (dc->type_b) {
1137fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1138d7ecb757SRichard Henderson         dc->jmp_pc = dc->base.pc_next + dec_alu_typeb_imm(dc);
1139d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
1140fcf5ef2aSThomas Huth     } else {
1141fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1142d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1143fcf5ef2aSThomas Huth     }
11449b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1145fcf5ef2aSThomas Huth }
1146fcf5ef2aSThomas Huth 
1147fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1148fcf5ef2aSThomas Huth {
1149fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1150fcf5ef2aSThomas Huth 
1151fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1152fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1153fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1154fcf5ef2aSThomas Huth 
1155fcf5ef2aSThomas Huth     /* Memory barrier.  */
1156fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1157fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1158badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1159badcbf9dSEdgar E. Iglesias 
11603f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
11613f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
11623f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
11633f172744SEdgar E. Iglesias         }
11643f172744SEdgar E. Iglesias 
1165fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1166badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
116741ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1168fcf5ef2aSThomas Huth 
1169b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1170b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1171b4919e7dSEdgar E. Iglesias                 return;
1172b4919e7dSEdgar E. Iglesias             }
1173b4919e7dSEdgar E. Iglesias 
1174fcf5ef2aSThomas Huth             t_sync_flags(dc);
117541ba37c4SRichard Henderson 
117641ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1177fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1178fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1179fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1180fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
118141ba37c4SRichard Henderson 
1182d4705ae0SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
118341ba37c4SRichard Henderson 
118441ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1185fcf5ef2aSThomas Huth             return;
1186fcf5ef2aSThomas Huth         }
1187fcf5ef2aSThomas Huth         /* Break the TB.  */
1188fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1189fcf5ef2aSThomas Huth         return;
1190fcf5ef2aSThomas Huth     }
1191fcf5ef2aSThomas Huth 
1192d7ecb757SRichard Henderson     if (abs && link && !dslot) {
1193d7ecb757SRichard Henderson         if (dc->type_b) {
1194d7ecb757SRichard Henderson             /* BRKI */
1195d7ecb757SRichard Henderson             uint32_t imm = dec_alu_typeb_imm(dc);
1196d7ecb757SRichard Henderson             if (trap_userspace(dc, imm != 8 && imm != 0x18)) {
1197d7ecb757SRichard Henderson                 return;
1198d7ecb757SRichard Henderson             }
1199d7ecb757SRichard Henderson         } else {
1200d7ecb757SRichard Henderson             /* BRK */
1201d7ecb757SRichard Henderson             if (trap_userspace(dc, true)) {
1202d7ecb757SRichard Henderson                 return;
1203d7ecb757SRichard Henderson             }
1204d7ecb757SRichard Henderson         }
1205d7ecb757SRichard Henderson     }
1206d7ecb757SRichard Henderson 
1207fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1208fcf5ef2aSThomas Huth     if (dslot) {
1209f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1210fcf5ef2aSThomas Huth     }
1211d7ecb757SRichard Henderson     if (link && dc->rd) {
1212d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
1213d7ecb757SRichard Henderson     }
1214fcf5ef2aSThomas Huth 
1215fcf5ef2aSThomas Huth     if (abs) {
1216d7ecb757SRichard Henderson         if (dc->type_b) {
1217d7ecb757SRichard Henderson             uint32_t dest = dec_alu_typeb_imm(dc);
1218d7ecb757SRichard Henderson 
1219d7ecb757SRichard Henderson             dc->jmp = JMP_DIRECT;
1220d7ecb757SRichard Henderson             dc->jmp_pc = dest;
1221d7ecb757SRichard Henderson             tcg_gen_movi_i32(cpu_btarget, dest);
1222fcf5ef2aSThomas Huth             if (link && !dslot) {
1223d7ecb757SRichard Henderson                 switch (dest) {
1224d7ecb757SRichard Henderson                 case 8:
1225d7ecb757SRichard Henderson                 case 0x18:
1226d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_BREAK);
1227d7ecb757SRichard Henderson                     break;
1228d7ecb757SRichard Henderson                 case 0:
1229d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_DEBUG);
1230d7ecb757SRichard Henderson                     break;
1231d7ecb757SRichard Henderson                 }
1232d7ecb757SRichard Henderson             }
1233d7ecb757SRichard Henderson         } else {
1234d7ecb757SRichard Henderson             dc->jmp = JMP_INDIRECT;
1235d7ecb757SRichard Henderson             tcg_gen_mov_i32(cpu_btarget, cpu_R[dc->rb]);
1236d7ecb757SRichard Henderson             if (link && !dslot) {
123741ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
123841ba37c4SRichard Henderson             }
1239fcf5ef2aSThomas Huth         }
1240d7ecb757SRichard Henderson     } else if (dc->type_b) {
1241fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT;
1242d7ecb757SRichard Henderson         dc->jmp_pc = dc->base.pc_next + dec_alu_typeb_imm(dc);
1243d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
1244fcf5ef2aSThomas Huth     } else {
1245d7ecb757SRichard Henderson         dc->jmp = JMP_INDIRECT;
1246d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1247d7ecb757SRichard Henderson     }
12489b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
1249fcf5ef2aSThomas Huth }
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1252fcf5ef2aSThomas Huth {
1253cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1254cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1255cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
12563e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
12570a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
12580a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1259cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1260fcf5ef2aSThomas Huth 
1261cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1262cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1263fcf5ef2aSThomas Huth     msr_write(dc, t1);
1264cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1265cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1266fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1267fcf5ef2aSThomas Huth }
1268fcf5ef2aSThomas Huth 
1269fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1270fcf5ef2aSThomas Huth {
1271cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1272cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1273cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
12743e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
12750a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1276cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1277cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1278fcf5ef2aSThomas Huth 
1279cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1280cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1281fcf5ef2aSThomas Huth     msr_write(dc, t1);
1282cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1283cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1284fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1285fcf5ef2aSThomas Huth }
1286fcf5ef2aSThomas Huth 
1287fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1288fcf5ef2aSThomas Huth {
1289cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1290cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1291cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1292fcf5ef2aSThomas Huth 
12933e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
12940a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1295cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1296cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1297cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1298fcf5ef2aSThomas Huth 
1299cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1300cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1301fcf5ef2aSThomas Huth     msr_write(dc, t1);
1302cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1303cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1304fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1305fcf5ef2aSThomas Huth }
1306fcf5ef2aSThomas Huth 
1307fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1308fcf5ef2aSThomas Huth {
1309fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1310fcf5ef2aSThomas Huth 
1311fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1312fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1313fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1314fcf5ef2aSThomas Huth 
1315bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1316bdfc1e88SEdgar E. Iglesias         return;
1317bdfc1e88SEdgar E. Iglesias     }
1318bdfc1e88SEdgar E. Iglesias 
1319f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1320fcf5ef2aSThomas Huth 
1321fcf5ef2aSThomas Huth     if (i_bit) {
1322fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1323fcf5ef2aSThomas Huth     } else if (b_bit) {
1324fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1325fcf5ef2aSThomas Huth     } else if (e_bit) {
1326fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
132711105d67SRichard Henderson     }
1328fcf5ef2aSThomas Huth 
1329fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
13309b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
13310f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1332fcf5ef2aSThomas Huth }
1333fcf5ef2aSThomas Huth 
1334fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1335fcf5ef2aSThomas Huth {
1336fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
133741ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1338fcf5ef2aSThomas Huth     }
13392016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1340fcf5ef2aSThomas Huth }
1341fcf5ef2aSThomas Huth 
1342fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1343fcf5ef2aSThomas Huth {
1344fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1345fcf5ef2aSThomas Huth 
13469ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1347fcf5ef2aSThomas Huth         return;
1348fcf5ef2aSThomas Huth     }
1349fcf5ef2aSThomas Huth 
1350fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1351fcf5ef2aSThomas Huth 
1352fcf5ef2aSThomas Huth     switch (fpu_insn) {
1353fcf5ef2aSThomas Huth         case 0:
1354fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1355fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1356fcf5ef2aSThomas Huth             break;
1357fcf5ef2aSThomas Huth 
1358fcf5ef2aSThomas Huth         case 1:
1359fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1360fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1361fcf5ef2aSThomas Huth             break;
1362fcf5ef2aSThomas Huth 
1363fcf5ef2aSThomas Huth         case 2:
1364fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1365fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1366fcf5ef2aSThomas Huth             break;
1367fcf5ef2aSThomas Huth 
1368fcf5ef2aSThomas Huth         case 3:
1369fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1370fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1371fcf5ef2aSThomas Huth             break;
1372fcf5ef2aSThomas Huth 
1373fcf5ef2aSThomas Huth         case 4:
1374fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1375fcf5ef2aSThomas Huth                 case 0:
1376fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1377fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1378fcf5ef2aSThomas Huth                     break;
1379fcf5ef2aSThomas Huth                 case 1:
1380fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1381fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1382fcf5ef2aSThomas Huth                     break;
1383fcf5ef2aSThomas Huth                 case 2:
1384fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1385fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1386fcf5ef2aSThomas Huth                     break;
1387fcf5ef2aSThomas Huth                 case 3:
1388fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1389fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1390fcf5ef2aSThomas Huth                     break;
1391fcf5ef2aSThomas Huth                 case 4:
1392fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1393fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1394fcf5ef2aSThomas Huth                     break;
1395fcf5ef2aSThomas Huth                 case 5:
1396fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1397fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1398fcf5ef2aSThomas Huth                     break;
1399fcf5ef2aSThomas Huth                 case 6:
1400fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1401fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1402fcf5ef2aSThomas Huth                     break;
1403fcf5ef2aSThomas Huth                 default:
1404fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1405fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1406fcf5ef2aSThomas Huth                                   " opc=%x\n",
1407d4705ae0SRichard Henderson                                   fpu_insn, (uint32_t)dc->base.pc_next,
1408d4705ae0SRichard Henderson                                   dc->opcode);
1409fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1410fcf5ef2aSThomas Huth                     break;
1411fcf5ef2aSThomas Huth             }
1412fcf5ef2aSThomas Huth             break;
1413fcf5ef2aSThomas Huth 
1414fcf5ef2aSThomas Huth         case 5:
1415fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1416fcf5ef2aSThomas Huth                 return;
1417fcf5ef2aSThomas Huth             }
1418fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1419fcf5ef2aSThomas Huth             break;
1420fcf5ef2aSThomas Huth 
1421fcf5ef2aSThomas Huth         case 6:
1422fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1423fcf5ef2aSThomas Huth                 return;
1424fcf5ef2aSThomas Huth             }
1425fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1426fcf5ef2aSThomas Huth             break;
1427fcf5ef2aSThomas Huth 
1428fcf5ef2aSThomas Huth         case 7:
1429fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1430fcf5ef2aSThomas Huth                 return;
1431fcf5ef2aSThomas Huth             }
1432fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1433fcf5ef2aSThomas Huth             break;
1434fcf5ef2aSThomas Huth 
1435fcf5ef2aSThomas Huth         default:
1436fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1437fcf5ef2aSThomas Huth                           " opc=%x\n",
1438d4705ae0SRichard Henderson                           fpu_insn, (uint32_t)dc->base.pc_next, dc->opcode);
1439fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1440fcf5ef2aSThomas Huth             break;
1441fcf5ef2aSThomas Huth     }
1442fcf5ef2aSThomas Huth }
1443fcf5ef2aSThomas Huth 
1444fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1445fcf5ef2aSThomas Huth {
14469ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1447fcf5ef2aSThomas Huth         return;
1448fcf5ef2aSThomas Huth     }
1449d4705ae0SRichard Henderson     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n",
1450d4705ae0SRichard Henderson                   (uint32_t)dc->base.pc_next, dc->opcode);
1451fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1452fcf5ef2aSThomas Huth }
1453fcf5ef2aSThomas Huth 
1454fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1455fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1456fcf5ef2aSThomas Huth {
1457fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1458fcf5ef2aSThomas Huth     int ctrl;
1459fcf5ef2aSThomas Huth 
1460bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1461fcf5ef2aSThomas Huth         return;
1462fcf5ef2aSThomas Huth     }
1463fcf5ef2aSThomas Huth 
1464cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1465fcf5ef2aSThomas Huth     if (dc->type_b) {
1466cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1467fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1468fcf5ef2aSThomas Huth     } else {
1469cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1470fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1471fcf5ef2aSThomas Huth     }
1472fcf5ef2aSThomas Huth 
1473cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1474fcf5ef2aSThomas Huth 
1475fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1476fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1477fcf5ef2aSThomas Huth     } else {
1478fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1479fcf5ef2aSThomas Huth     }
1480cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1481cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1482fcf5ef2aSThomas Huth }
1483fcf5ef2aSThomas Huth 
1484fcf5ef2aSThomas Huth static struct decoder_info {
1485fcf5ef2aSThomas Huth     struct {
1486fcf5ef2aSThomas Huth         uint32_t bits;
1487fcf5ef2aSThomas Huth         uint32_t mask;
1488fcf5ef2aSThomas Huth     };
1489fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1490fcf5ef2aSThomas Huth } decinfo[] = {
1491fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1492fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1493fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1494fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1495fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1496fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1497fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1498fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1499fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1500fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1501fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1502fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1503fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1504fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1505fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1506fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1507fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1508fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1509fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1510fcf5ef2aSThomas Huth };
1511fcf5ef2aSThomas Huth 
1512*44d1432bSRichard Henderson static void old_decode(DisasContext *dc, uint32_t ir)
1513fcf5ef2aSThomas Huth {
1514fcf5ef2aSThomas Huth     int i;
1515fcf5ef2aSThomas Huth 
1516fcf5ef2aSThomas Huth     dc->ir = ir;
1517fcf5ef2aSThomas Huth 
1518462c2544SEdgar E. Iglesias     if (ir == 0) {
15191ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1520462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1521462c2544SEdgar E. Iglesias         return;
1522462c2544SEdgar E. Iglesias     }
1523fcf5ef2aSThomas Huth 
1524fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1525fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1526fcf5ef2aSThomas Huth 
1527fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1528fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1529fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1530fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1531fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1532fcf5ef2aSThomas Huth 
1533fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1534fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1535fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1536fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1537fcf5ef2aSThomas Huth             break;
1538fcf5ef2aSThomas Huth         }
1539fcf5ef2aSThomas Huth     }
1540fcf5ef2aSThomas Huth }
1541fcf5ef2aSThomas Huth 
1542372122e3SRichard Henderson static void mb_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1543fcf5ef2aSThomas Huth {
1544372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1545372122e3SRichard Henderson     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1546372122e3SRichard Henderson     int bound;
1547fcf5ef2aSThomas Huth 
1548fcf5ef2aSThomas Huth     dc->cpu = cpu;
1549372122e3SRichard Henderson     dc->synced_flags = dc->tb_flags = dc->base.tb->flags;
1550fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1551372122e3SRichard Henderson     dc->jmp = dc->delayed_branch ? JMP_INDIRECT : JMP_NOJMP;
1552fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1553fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1554d7ecb757SRichard Henderson     dc->ext_imm = dc->base.tb->cs_base;
1555fcf5ef2aSThomas Huth 
1556372122e3SRichard Henderson     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1557372122e3SRichard Henderson     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1558fcf5ef2aSThomas Huth }
1559fcf5ef2aSThomas Huth 
1560372122e3SRichard Henderson static void mb_tr_tb_start(DisasContextBase *dcb, CPUState *cs)
1561fcf5ef2aSThomas Huth {
1562fcf5ef2aSThomas Huth }
1563fcf5ef2aSThomas Huth 
1564372122e3SRichard Henderson static void mb_tr_insn_start(DisasContextBase *dcb, CPUState *cs)
1565372122e3SRichard Henderson {
1566372122e3SRichard Henderson     tcg_gen_insn_start(dcb->pc_next);
1567372122e3SRichard Henderson }
1568fcf5ef2aSThomas Huth 
1569372122e3SRichard Henderson static bool mb_tr_breakpoint_check(DisasContextBase *dcb, CPUState *cs,
1570372122e3SRichard Henderson                                    const CPUBreakpoint *bp)
1571372122e3SRichard Henderson {
1572372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1573372122e3SRichard Henderson 
1574372122e3SRichard Henderson     gen_raise_exception_sync(dc, EXCP_DEBUG);
1575372122e3SRichard Henderson 
1576372122e3SRichard Henderson     /*
1577372122e3SRichard Henderson      * The address covered by the breakpoint must be included in
1578372122e3SRichard Henderson      * [tb->pc, tb->pc + tb->size) in order to for it to be
1579372122e3SRichard Henderson      * properly cleared -- thus we increment the PC here so that
1580372122e3SRichard Henderson      * the logic setting tb->size below does the right thing.
1581372122e3SRichard Henderson      */
1582372122e3SRichard Henderson     dc->base.pc_next += 4;
1583372122e3SRichard Henderson     return true;
1584372122e3SRichard Henderson }
1585372122e3SRichard Henderson 
1586372122e3SRichard Henderson static void mb_tr_translate_insn(DisasContextBase *dcb, CPUState *cs)
1587372122e3SRichard Henderson {
1588372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1589372122e3SRichard Henderson     CPUMBState *env = cs->env_ptr;
1590*44d1432bSRichard Henderson     uint32_t ir;
1591372122e3SRichard Henderson 
1592372122e3SRichard Henderson     /* TODO: This should raise an exception, not terminate qemu. */
1593372122e3SRichard Henderson     if (dc->base.pc_next & 3) {
1594372122e3SRichard Henderson         cpu_abort(cs, "Microblaze: unaligned PC=%x\n",
1595372122e3SRichard Henderson                   (uint32_t)dc->base.pc_next);
1596fcf5ef2aSThomas Huth     }
1597fcf5ef2aSThomas Huth 
1598fcf5ef2aSThomas Huth     dc->clear_imm = 1;
1599*44d1432bSRichard Henderson     ir = cpu_ldl_code(env, dc->base.pc_next);
1600*44d1432bSRichard Henderson     if (!decode(dc, ir)) {
1601*44d1432bSRichard Henderson         old_decode(dc, ir);
1602*44d1432bSRichard Henderson     }
1603d7ecb757SRichard Henderson     if (dc->clear_imm && (dc->tb_flags & IMM_FLAG)) {
1604fcf5ef2aSThomas Huth         dc->tb_flags &= ~IMM_FLAG;
1605d7ecb757SRichard Henderson         tcg_gen_discard_i32(cpu_imm);
1606372122e3SRichard Henderson     }
1607d4705ae0SRichard Henderson     dc->base.pc_next += 4;
1608fcf5ef2aSThomas Huth 
1609372122e3SRichard Henderson     if (dc->delayed_branch && --dc->delayed_branch == 0) {
1610372122e3SRichard Henderson         if (dc->tb_flags & DRTI_FLAG) {
1611fcf5ef2aSThomas Huth             do_rti(dc);
1612372122e3SRichard Henderson         }
1613372122e3SRichard Henderson         if (dc->tb_flags & DRTB_FLAG) {
1614fcf5ef2aSThomas Huth             do_rtb(dc);
1615372122e3SRichard Henderson         }
1616372122e3SRichard Henderson         if (dc->tb_flags & DRTE_FLAG) {
1617fcf5ef2aSThomas Huth             do_rte(dc);
1618372122e3SRichard Henderson         }
1619fcf5ef2aSThomas Huth         /* Clear the delay slot flag.  */
1620fcf5ef2aSThomas Huth         dc->tb_flags &= ~D_FLAG;
1621372122e3SRichard Henderson         dc->base.is_jmp = DISAS_JUMP;
1622372122e3SRichard Henderson     }
1623372122e3SRichard Henderson 
1624372122e3SRichard Henderson     /* Force an exit if the per-tb cpu state has changed.  */
1625372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NEXT && dc->cpustate_changed) {
1626372122e3SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
1627372122e3SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1628372122e3SRichard Henderson     }
1629372122e3SRichard Henderson }
1630372122e3SRichard Henderson 
1631372122e3SRichard Henderson static void mb_tr_tb_stop(DisasContextBase *dcb, CPUState *cs)
1632372122e3SRichard Henderson {
1633372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1634372122e3SRichard Henderson 
1635372122e3SRichard Henderson     assert(!dc->abort_at_next_insn);
1636372122e3SRichard Henderson 
1637372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NORETURN) {
1638372122e3SRichard Henderson         /* We have already exited the TB. */
1639372122e3SRichard Henderson         return;
1640372122e3SRichard Henderson     }
1641372122e3SRichard Henderson 
1642372122e3SRichard Henderson     t_sync_flags(dc);
1643372122e3SRichard Henderson     if (dc->tb_flags & D_FLAG) {
1644372122e3SRichard Henderson         sync_jmpstate(dc);
1645372122e3SRichard Henderson         dc->jmp = JMP_NOJMP;
1646372122e3SRichard Henderson     }
1647372122e3SRichard Henderson 
1648372122e3SRichard Henderson     switch (dc->base.is_jmp) {
1649372122e3SRichard Henderson     case DISAS_TOO_MANY:
1650372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1651372122e3SRichard Henderson         gen_goto_tb(dc, 0, dc->base.pc_next);
1652372122e3SRichard Henderson         return;
1653372122e3SRichard Henderson 
1654372122e3SRichard Henderson     case DISAS_UPDATE:
1655372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1656372122e3SRichard Henderson         if (unlikely(cs->singlestep_enabled)) {
1657372122e3SRichard Henderson             gen_raise_exception(dc, EXCP_DEBUG);
1658372122e3SRichard Henderson         } else {
1659372122e3SRichard Henderson             tcg_gen_exit_tb(NULL, 0);
1660372122e3SRichard Henderson         }
1661372122e3SRichard Henderson         return;
1662372122e3SRichard Henderson 
1663372122e3SRichard Henderson     case DISAS_JUMP:
1664372122e3SRichard Henderson         switch (dc->jmp) {
1665372122e3SRichard Henderson         case JMP_INDIRECT:
1666372122e3SRichard Henderson             {
1667d4705ae0SRichard Henderson                 TCGv_i32 tmp_pc = tcg_const_i32(dc->base.pc_next);
16680f96e96bSRichard Henderson                 eval_cond_jmp(dc, cpu_btarget, tmp_pc);
16690f96e96bSRichard Henderson                 tcg_temp_free_i32(tmp_pc);
1670372122e3SRichard Henderson 
1671372122e3SRichard Henderson                 if (unlikely(cs->singlestep_enabled)) {
1672372122e3SRichard Henderson                     gen_raise_exception(dc, EXCP_DEBUG);
1673372122e3SRichard Henderson                 } else {
1674372122e3SRichard Henderson                     tcg_gen_exit_tb(NULL, 0);
1675372122e3SRichard Henderson                 }
1676372122e3SRichard Henderson             }
1677372122e3SRichard Henderson             return;
1678372122e3SRichard Henderson 
1679372122e3SRichard Henderson         case JMP_DIRECT_CC:
1680372122e3SRichard Henderson             {
1681fcf5ef2aSThomas Huth                 TCGLabel *l1 = gen_new_label();
16829b158558SRichard Henderson                 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1683d4705ae0SRichard Henderson                 gen_goto_tb(dc, 1, dc->base.pc_next);
1684fcf5ef2aSThomas Huth                 gen_set_label(l1);
1685372122e3SRichard Henderson             }
1686372122e3SRichard Henderson             /* fall through */
1687372122e3SRichard Henderson 
1688372122e3SRichard Henderson         case JMP_DIRECT:
1689fcf5ef2aSThomas Huth             gen_goto_tb(dc, 0, dc->jmp_pc);
1690372122e3SRichard Henderson             return;
1691fcf5ef2aSThomas Huth         }
1692372122e3SRichard Henderson         /* fall through */
1693fcf5ef2aSThomas Huth 
1694a2b80dbdSRichard Henderson     default:
1695a2b80dbdSRichard Henderson         g_assert_not_reached();
1696fcf5ef2aSThomas Huth     }
1697fcf5ef2aSThomas Huth }
1698fcf5ef2aSThomas Huth 
1699372122e3SRichard Henderson static void mb_tr_disas_log(const DisasContextBase *dcb, CPUState *cs)
1700372122e3SRichard Henderson {
1701372122e3SRichard Henderson     qemu_log("IN: %s\n", lookup_symbol(dcb->pc_first));
1702372122e3SRichard Henderson     log_target_disas(cs, dcb->pc_first, dcb->tb->size);
1703fcf5ef2aSThomas Huth }
1704372122e3SRichard Henderson 
1705372122e3SRichard Henderson static const TranslatorOps mb_tr_ops = {
1706372122e3SRichard Henderson     .init_disas_context = mb_tr_init_disas_context,
1707372122e3SRichard Henderson     .tb_start           = mb_tr_tb_start,
1708372122e3SRichard Henderson     .insn_start         = mb_tr_insn_start,
1709372122e3SRichard Henderson     .breakpoint_check   = mb_tr_breakpoint_check,
1710372122e3SRichard Henderson     .translate_insn     = mb_tr_translate_insn,
1711372122e3SRichard Henderson     .tb_stop            = mb_tr_tb_stop,
1712372122e3SRichard Henderson     .disas_log          = mb_tr_disas_log,
1713372122e3SRichard Henderson };
1714372122e3SRichard Henderson 
1715372122e3SRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
1716372122e3SRichard Henderson {
1717372122e3SRichard Henderson     DisasContext dc;
1718372122e3SRichard Henderson     translator_loop(&mb_tr_ops, &dc.base, cpu, tb, max_insns);
1719fcf5ef2aSThomas Huth }
1720fcf5ef2aSThomas Huth 
172190c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1722fcf5ef2aSThomas Huth {
1723fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1724fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1725fcf5ef2aSThomas Huth     int i;
1726fcf5ef2aSThomas Huth 
172790c84c56SMarkus Armbruster     if (!env) {
1728fcf5ef2aSThomas Huth         return;
172990c84c56SMarkus Armbruster     }
1730fcf5ef2aSThomas Huth 
17310f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
173276e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
17336efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1734eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
173578e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1736eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
17370f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1738fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
17392e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
17402e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
17412e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
17422e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
17432ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
17442ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
17452ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
17462ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
17472ead1b18SJoe Komlodi         }
17482ead1b18SJoe Komlodi     }
1749fcf5ef2aSThomas Huth 
17502ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
175139db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1752af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
17532ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1754fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
175590c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1756fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
175790c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1758fcf5ef2aSThomas Huth         }
175990c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1760fcf5ef2aSThomas Huth }
1761fcf5ef2aSThomas Huth 
1762fcf5ef2aSThomas Huth void mb_tcg_init(void)
1763fcf5ef2aSThomas Huth {
1764480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1765480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1766fcf5ef2aSThomas Huth 
1767480d29a8SRichard Henderson     static const struct {
1768480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1769480d29a8SRichard Henderson     } i32s[] = {
1770480d29a8SRichard Henderson         R(0),  R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1771480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1772480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1773480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1774480d29a8SRichard Henderson 
1775480d29a8SRichard Henderson         SP(pc),
1776480d29a8SRichard Henderson         SP(msr),
17771074c0fbSRichard Henderson         SP(msr_c),
1778480d29a8SRichard Henderson         SP(imm),
1779480d29a8SRichard Henderson         SP(iflags),
1780480d29a8SRichard Henderson         SP(btaken),
1781480d29a8SRichard Henderson         SP(btarget),
1782480d29a8SRichard Henderson         SP(res_val),
1783480d29a8SRichard Henderson     };
1784480d29a8SRichard Henderson 
1785480d29a8SRichard Henderson #undef R
1786480d29a8SRichard Henderson #undef SP
1787480d29a8SRichard Henderson 
1788480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1789480d29a8SRichard Henderson         *i32s[i].var =
1790480d29a8SRichard Henderson           tcg_global_mem_new_i32(cpu_env, i32s[i].ofs, i32s[i].name);
1791fcf5ef2aSThomas Huth     }
179276e8187dSRichard Henderson 
1793480d29a8SRichard Henderson     cpu_res_addr =
1794480d29a8SRichard Henderson         tcg_global_mem_new(cpu_env, offsetof(CPUMBState, res_addr), "res_addr");
1795fcf5ef2aSThomas Huth }
1796fcf5ef2aSThomas Huth 
1797fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1798fcf5ef2aSThomas Huth                           target_ulong *data)
1799fcf5ef2aSThomas Huth {
180076e8187dSRichard Henderson     env->pc = data[0];
1801fcf5ef2aSThomas Huth }
1802