xref: /openbmc/qemu/target/microblaze/translate.c (revision a2b0b90e7960c6dcf52be237149c1b9ff289d9a5)
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 
6120800179SRichard Henderson     TCGv_i32 r0;
6220800179SRichard Henderson     bool r0_set;
6320800179SRichard Henderson 
64fcf5ef2aSThomas Huth     /* Decoder.  */
65fcf5ef2aSThomas Huth     int type_b;
66fcf5ef2aSThomas Huth     uint32_t ir;
67d7ecb757SRichard Henderson     uint32_t ext_imm;
68fcf5ef2aSThomas Huth     uint8_t opcode;
69fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
70fcf5ef2aSThomas Huth     uint16_t imm;
71fcf5ef2aSThomas Huth 
72fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
73fcf5ef2aSThomas Huth     unsigned int delayed_branch;
74fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
75fcf5ef2aSThomas Huth     unsigned int clear_imm;
76fcf5ef2aSThomas Huth 
77fcf5ef2aSThomas Huth #define JMP_NOJMP     0
78fcf5ef2aSThomas Huth #define JMP_DIRECT    1
79fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
80fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
81fcf5ef2aSThomas Huth     unsigned int jmp;
82fcf5ef2aSThomas Huth     uint32_t jmp_pc;
83fcf5ef2aSThomas Huth 
84fcf5ef2aSThomas Huth     int abort_at_next_insn;
85fcf5ef2aSThomas Huth } DisasContext;
86fcf5ef2aSThomas Huth 
8720800179SRichard Henderson static int typeb_imm(DisasContext *dc, int x)
8820800179SRichard Henderson {
8920800179SRichard Henderson     if (dc->tb_flags & IMM_FLAG) {
9020800179SRichard Henderson         return deposit32(dc->ext_imm, 0, 16, x);
9120800179SRichard Henderson     }
9220800179SRichard Henderson     return x;
9320800179SRichard Henderson }
9420800179SRichard Henderson 
9544d1432bSRichard Henderson /* Include the auto-generated decoder.  */
9644d1432bSRichard Henderson #include "decode-insns.c.inc"
9744d1432bSRichard Henderson 
98fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
99fcf5ef2aSThomas Huth {
100fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
101fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
1029b158558SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags);
103fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
104fcf5ef2aSThomas Huth     }
105fcf5ef2aSThomas Huth }
106fcf5ef2aSThomas Huth 
10741ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
108fcf5ef2aSThomas Huth {
109fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
110fcf5ef2aSThomas Huth 
111fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
112fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
113d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
114fcf5ef2aSThomas Huth }
115fcf5ef2aSThomas Huth 
11641ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
11741ba37c4SRichard Henderson {
11841ba37c4SRichard Henderson     t_sync_flags(dc);
119d4705ae0SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
12041ba37c4SRichard Henderson     gen_raise_exception(dc, index);
12141ba37c4SRichard Henderson }
12241ba37c4SRichard Henderson 
12341ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
12441ba37c4SRichard Henderson {
12541ba37c4SRichard Henderson     TCGv_i32 tmp = tcg_const_i32(esr_ec);
12641ba37c4SRichard Henderson     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, esr));
12741ba37c4SRichard Henderson     tcg_temp_free_i32(tmp);
12841ba37c4SRichard Henderson 
12941ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
13041ba37c4SRichard Henderson }
13141ba37c4SRichard Henderson 
132fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
133fcf5ef2aSThomas Huth {
134fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
135d4705ae0SRichard Henderson     return (dc->base.pc_first & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
136fcf5ef2aSThomas Huth #else
137fcf5ef2aSThomas Huth     return true;
138fcf5ef2aSThomas Huth #endif
139fcf5ef2aSThomas Huth }
140fcf5ef2aSThomas Huth 
141fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
142fcf5ef2aSThomas Huth {
143d4705ae0SRichard Henderson     if (dc->base.singlestep_enabled) {
1440b46fa08SRichard Henderson         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1450b46fa08SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
1460b46fa08SRichard Henderson         gen_helper_raise_exception(cpu_env, tmp);
1470b46fa08SRichard Henderson         tcg_temp_free_i32(tmp);
1480b46fa08SRichard Henderson     } else if (use_goto_tb(dc, dest)) {
149fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1500f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
151d4705ae0SRichard Henderson         tcg_gen_exit_tb(dc->base.tb, n);
152fcf5ef2aSThomas Huth     } else {
1530f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
15407ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
155fcf5ef2aSThomas Huth     }
156d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
157fcf5ef2aSThomas Huth }
158fcf5ef2aSThomas Huth 
159bdfc1e88SEdgar E. Iglesias /*
1609ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1619ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1629ba8cd45SEdgar E. Iglesias  */
1639ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1649ba8cd45SEdgar E. Iglesias {
1659ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1665143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
16741ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1689ba8cd45SEdgar E. Iglesias     }
1699ba8cd45SEdgar E. Iglesias     return cond;
1709ba8cd45SEdgar E. Iglesias }
1719ba8cd45SEdgar E. Iglesias 
1729ba8cd45SEdgar E. Iglesias /*
173bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
174bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
175bdfc1e88SEdgar E. Iglesias  */
176bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
177bdfc1e88SEdgar E. Iglesias {
178bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
179bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
180bdfc1e88SEdgar E. Iglesias 
181bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
18241ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
183bdfc1e88SEdgar E. Iglesias     }
184bdfc1e88SEdgar E. Iglesias     return cond_user;
185bdfc1e88SEdgar E. Iglesias }
186bdfc1e88SEdgar E. Iglesias 
187d7ecb757SRichard Henderson static int32_t dec_alu_typeb_imm(DisasContext *dc)
188fcf5ef2aSThomas Huth {
189d7ecb757SRichard Henderson     tcg_debug_assert(dc->type_b);
19020800179SRichard Henderson     return typeb_imm(dc, (int16_t)dc->imm);
191fcf5ef2aSThomas Huth }
192fcf5ef2aSThomas Huth 
193cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
194fcf5ef2aSThomas Huth {
195fcf5ef2aSThomas Huth     if (dc->type_b) {
196d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_imm, dec_alu_typeb_imm(dc));
1979b158558SRichard Henderson         return &cpu_imm;
198d7ecb757SRichard Henderson     }
199fcf5ef2aSThomas Huth     return &cpu_R[dc->rb];
200fcf5ef2aSThomas Huth }
201fcf5ef2aSThomas Huth 
20220800179SRichard Henderson static TCGv_i32 reg_for_read(DisasContext *dc, int reg)
203fcf5ef2aSThomas Huth {
20420800179SRichard Henderson     if (likely(reg != 0)) {
20520800179SRichard Henderson         return cpu_R[reg];
206fcf5ef2aSThomas Huth     }
20720800179SRichard Henderson     if (!dc->r0_set) {
20820800179SRichard Henderson         if (dc->r0 == NULL) {
20920800179SRichard Henderson             dc->r0 = tcg_temp_new_i32();
210fcf5ef2aSThomas Huth         }
21120800179SRichard Henderson         tcg_gen_movi_i32(dc->r0, 0);
21220800179SRichard Henderson         dc->r0_set = true;
21320800179SRichard Henderson     }
21420800179SRichard Henderson     return dc->r0;
215fcf5ef2aSThomas Huth }
216fcf5ef2aSThomas Huth 
21720800179SRichard Henderson static TCGv_i32 reg_for_write(DisasContext *dc, int reg)
21820800179SRichard Henderson {
21920800179SRichard Henderson     if (likely(reg != 0)) {
22020800179SRichard Henderson         return cpu_R[reg];
22120800179SRichard Henderson     }
22220800179SRichard Henderson     if (dc->r0 == NULL) {
22320800179SRichard Henderson         dc->r0 = tcg_temp_new_i32();
22420800179SRichard Henderson     }
22520800179SRichard Henderson     return dc->r0;
226fcf5ef2aSThomas Huth }
227fcf5ef2aSThomas Huth 
22820800179SRichard Henderson static bool do_typea(DisasContext *dc, arg_typea *arg, bool side_effects,
22920800179SRichard Henderson                      void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32))
23020800179SRichard Henderson {
23120800179SRichard Henderson     TCGv_i32 rd, ra, rb;
23220800179SRichard Henderson 
23320800179SRichard Henderson     if (arg->rd == 0 && !side_effects) {
23420800179SRichard Henderson         return true;
235fcf5ef2aSThomas Huth     }
23620800179SRichard Henderson 
23720800179SRichard Henderson     rd = reg_for_write(dc, arg->rd);
23820800179SRichard Henderson     ra = reg_for_read(dc, arg->ra);
23920800179SRichard Henderson     rb = reg_for_read(dc, arg->rb);
24020800179SRichard Henderson     fn(rd, ra, rb);
24120800179SRichard Henderson     return true;
24220800179SRichard Henderson }
24320800179SRichard Henderson 
24420800179SRichard Henderson static bool do_typeb_imm(DisasContext *dc, arg_typeb *arg, bool side_effects,
24520800179SRichard Henderson                          void (*fni)(TCGv_i32, TCGv_i32, int32_t))
24620800179SRichard Henderson {
24720800179SRichard Henderson     TCGv_i32 rd, ra;
24820800179SRichard Henderson 
24920800179SRichard Henderson     if (arg->rd == 0 && !side_effects) {
25020800179SRichard Henderson         return true;
25120800179SRichard Henderson     }
25220800179SRichard Henderson 
25320800179SRichard Henderson     rd = reg_for_write(dc, arg->rd);
25420800179SRichard Henderson     ra = reg_for_read(dc, arg->ra);
25520800179SRichard Henderson     fni(rd, ra, arg->imm);
25620800179SRichard Henderson     return true;
25720800179SRichard Henderson }
25820800179SRichard Henderson 
25920800179SRichard Henderson static bool do_typeb_val(DisasContext *dc, arg_typeb *arg, bool side_effects,
26020800179SRichard Henderson                          void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32))
26120800179SRichard Henderson {
26220800179SRichard Henderson     TCGv_i32 rd, ra, imm;
26320800179SRichard Henderson 
26420800179SRichard Henderson     if (arg->rd == 0 && !side_effects) {
26520800179SRichard Henderson         return true;
26620800179SRichard Henderson     }
26720800179SRichard Henderson 
26820800179SRichard Henderson     rd = reg_for_write(dc, arg->rd);
26920800179SRichard Henderson     ra = reg_for_read(dc, arg->ra);
27020800179SRichard Henderson     imm = tcg_const_i32(arg->imm);
27120800179SRichard Henderson 
27220800179SRichard Henderson     fn(rd, ra, imm);
27320800179SRichard Henderson 
27420800179SRichard Henderson     tcg_temp_free_i32(imm);
27520800179SRichard Henderson     return true;
27620800179SRichard Henderson }
27720800179SRichard Henderson 
27820800179SRichard Henderson #define DO_TYPEA(NAME, SE, FN) \
27920800179SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea *a) \
28020800179SRichard Henderson     { return do_typea(dc, a, SE, FN); }
28120800179SRichard Henderson 
28220800179SRichard Henderson #define DO_TYPEBI(NAME, SE, FNI) \
28320800179SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typeb *a) \
28420800179SRichard Henderson     { return do_typeb_imm(dc, a, SE, FNI); }
28520800179SRichard Henderson 
28620800179SRichard Henderson #define DO_TYPEBV(NAME, SE, FN) \
28720800179SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typeb *a) \
28820800179SRichard Henderson     { return do_typeb_val(dc, a, SE, FN); }
28920800179SRichard Henderson 
29020800179SRichard Henderson /* No input carry, but output carry. */
29120800179SRichard Henderson static void gen_add(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
29220800179SRichard Henderson {
29320800179SRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
29420800179SRichard Henderson 
29520800179SRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, ina, zero, inb, zero);
29620800179SRichard Henderson 
29720800179SRichard Henderson     tcg_temp_free_i32(zero);
29820800179SRichard Henderson }
29920800179SRichard Henderson 
30020800179SRichard Henderson /* Input and output carry. */
30120800179SRichard Henderson static void gen_addc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
30220800179SRichard Henderson {
30320800179SRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
30420800179SRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
30520800179SRichard Henderson 
30620800179SRichard Henderson     tcg_gen_add2_i32(tmp, cpu_msr_c, ina, zero, cpu_msr_c, zero);
30720800179SRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, tmp, cpu_msr_c, inb, zero);
30820800179SRichard Henderson 
30920800179SRichard Henderson     tcg_temp_free_i32(tmp);
31020800179SRichard Henderson     tcg_temp_free_i32(zero);
31120800179SRichard Henderson }
31220800179SRichard Henderson 
31320800179SRichard Henderson /* Input carry, but no output carry. */
31420800179SRichard Henderson static void gen_addkc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
31520800179SRichard Henderson {
31620800179SRichard Henderson     tcg_gen_add_i32(out, ina, inb);
31720800179SRichard Henderson     tcg_gen_add_i32(out, out, cpu_msr_c);
31820800179SRichard Henderson }
31920800179SRichard Henderson 
32020800179SRichard Henderson DO_TYPEA(add, true, gen_add)
32120800179SRichard Henderson DO_TYPEA(addc, true, gen_addc)
32220800179SRichard Henderson DO_TYPEA(addk, false, tcg_gen_add_i32)
32320800179SRichard Henderson DO_TYPEA(addkc, true, gen_addkc)
32420800179SRichard Henderson 
32520800179SRichard Henderson DO_TYPEBV(addi, true, gen_add)
32620800179SRichard Henderson DO_TYPEBV(addic, true, gen_addc)
32720800179SRichard Henderson DO_TYPEBI(addik, false, tcg_gen_addi_i32)
32820800179SRichard Henderson DO_TYPEBV(addikc, true, gen_addkc)
32920800179SRichard Henderson 
330*a2b0b90eSRichard Henderson DO_TYPEA(cmp, false, gen_helper_cmp)
331*a2b0b90eSRichard Henderson DO_TYPEA(cmpu, false, gen_helper_cmpu)
332*a2b0b90eSRichard Henderson 
333*a2b0b90eSRichard Henderson /* No input carry, but output carry. */
334*a2b0b90eSRichard Henderson static void gen_rsub(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
335*a2b0b90eSRichard Henderson {
336*a2b0b90eSRichard Henderson     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_msr_c, inb, ina);
337*a2b0b90eSRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
338*a2b0b90eSRichard Henderson }
339*a2b0b90eSRichard Henderson 
340*a2b0b90eSRichard Henderson /* Input and output carry. */
341*a2b0b90eSRichard Henderson static void gen_rsubc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
342*a2b0b90eSRichard Henderson {
343*a2b0b90eSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
344*a2b0b90eSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
345*a2b0b90eSRichard Henderson 
346*a2b0b90eSRichard Henderson     tcg_gen_not_i32(tmp, ina);
347*a2b0b90eSRichard Henderson     tcg_gen_add2_i32(tmp, cpu_msr_c, tmp, zero, cpu_msr_c, zero);
348*a2b0b90eSRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, tmp, cpu_msr_c, inb, zero);
349*a2b0b90eSRichard Henderson 
350*a2b0b90eSRichard Henderson     tcg_temp_free_i32(zero);
351*a2b0b90eSRichard Henderson     tcg_temp_free_i32(tmp);
352*a2b0b90eSRichard Henderson }
353*a2b0b90eSRichard Henderson 
354*a2b0b90eSRichard Henderson /* No input or output carry. */
355*a2b0b90eSRichard Henderson static void gen_rsubk(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
356*a2b0b90eSRichard Henderson {
357*a2b0b90eSRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
358*a2b0b90eSRichard Henderson }
359*a2b0b90eSRichard Henderson 
360*a2b0b90eSRichard Henderson /* Input carry, no output carry. */
361*a2b0b90eSRichard Henderson static void gen_rsubkc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
362*a2b0b90eSRichard Henderson {
363*a2b0b90eSRichard Henderson     TCGv_i32 nota = tcg_temp_new_i32();
364*a2b0b90eSRichard Henderson 
365*a2b0b90eSRichard Henderson     tcg_gen_not_i32(nota, ina);
366*a2b0b90eSRichard Henderson     tcg_gen_add_i32(out, inb, nota);
367*a2b0b90eSRichard Henderson     tcg_gen_add_i32(out, out, cpu_msr_c);
368*a2b0b90eSRichard Henderson 
369*a2b0b90eSRichard Henderson     tcg_temp_free_i32(nota);
370*a2b0b90eSRichard Henderson }
371*a2b0b90eSRichard Henderson 
372*a2b0b90eSRichard Henderson DO_TYPEA(rsub, true, gen_rsub)
373*a2b0b90eSRichard Henderson DO_TYPEA(rsubc, true, gen_rsubc)
374*a2b0b90eSRichard Henderson DO_TYPEA(rsubk, false, gen_rsubk)
375*a2b0b90eSRichard Henderson DO_TYPEA(rsubkc, true, gen_rsubkc)
376*a2b0b90eSRichard Henderson 
377*a2b0b90eSRichard Henderson DO_TYPEBV(rsubi, true, gen_rsub)
378*a2b0b90eSRichard Henderson DO_TYPEBV(rsubic, true, gen_rsubc)
379*a2b0b90eSRichard Henderson DO_TYPEBV(rsubik, false, gen_rsubk)
380*a2b0b90eSRichard Henderson DO_TYPEBV(rsubikc, true, gen_rsubkc)
381*a2b0b90eSRichard Henderson 
38220800179SRichard Henderson static bool trans_zero(DisasContext *dc, arg_zero *arg)
38320800179SRichard Henderson {
38420800179SRichard Henderson     /* If opcode_0_illegal, trap.  */
38520800179SRichard Henderson     if (dc->cpu->cfg.opcode_0_illegal) {
38620800179SRichard Henderson         trap_illegal(dc, true);
38720800179SRichard Henderson         return true;
38820800179SRichard Henderson     }
38920800179SRichard Henderson     /*
39020800179SRichard Henderson      * Otherwise, this is "add r0, r0, r0".
39120800179SRichard Henderson      * Continue to trans_add so that MSR[C] gets cleared.
39220800179SRichard Henderson      */
39320800179SRichard Henderson     return false;
394fcf5ef2aSThomas Huth }
395fcf5ef2aSThomas Huth 
396fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
397fcf5ef2aSThomas Huth {
398fcf5ef2aSThomas Huth     unsigned int mode;
399fcf5ef2aSThomas Huth 
4009ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
4019ba8cd45SEdgar E. Iglesias         return;
402fcf5ef2aSThomas Huth     }
403fcf5ef2aSThomas Huth 
404fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
405fcf5ef2aSThomas Huth     switch (mode) {
406fcf5ef2aSThomas Huth         case 0:
407fcf5ef2aSThomas Huth             /* pcmpbf.  */
408fcf5ef2aSThomas Huth             if (dc->rd)
409fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
410fcf5ef2aSThomas Huth             break;
411fcf5ef2aSThomas Huth         case 2:
412fcf5ef2aSThomas Huth             if (dc->rd) {
413cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
414fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
415fcf5ef2aSThomas Huth             }
416fcf5ef2aSThomas Huth             break;
417fcf5ef2aSThomas Huth         case 3:
418fcf5ef2aSThomas Huth             if (dc->rd) {
419cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
420fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
421fcf5ef2aSThomas Huth             }
422fcf5ef2aSThomas Huth             break;
423fcf5ef2aSThomas Huth         default:
424fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
425fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
426fcf5ef2aSThomas Huth             break;
427fcf5ef2aSThomas Huth     }
428fcf5ef2aSThomas Huth }
429fcf5ef2aSThomas Huth 
430fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
431fcf5ef2aSThomas Huth {
432fcf5ef2aSThomas Huth     unsigned int not;
433fcf5ef2aSThomas Huth 
434fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
435fcf5ef2aSThomas Huth         dec_pattern(dc);
436fcf5ef2aSThomas Huth         return;
437fcf5ef2aSThomas Huth     }
438fcf5ef2aSThomas Huth 
439fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
440fcf5ef2aSThomas Huth 
441fcf5ef2aSThomas Huth     if (!dc->rd)
442fcf5ef2aSThomas Huth         return;
443fcf5ef2aSThomas Huth 
444fcf5ef2aSThomas Huth     if (not) {
445cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
446fcf5ef2aSThomas Huth     } else
447cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
448fcf5ef2aSThomas Huth }
449fcf5ef2aSThomas Huth 
450fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
451fcf5ef2aSThomas Huth {
452fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
453fcf5ef2aSThomas Huth         dec_pattern(dc);
454fcf5ef2aSThomas Huth         return;
455fcf5ef2aSThomas Huth     }
456fcf5ef2aSThomas Huth 
457fcf5ef2aSThomas Huth     if (dc->rd)
458cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
459fcf5ef2aSThomas Huth }
460fcf5ef2aSThomas Huth 
461fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
462fcf5ef2aSThomas Huth {
463fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
464fcf5ef2aSThomas Huth         dec_pattern(dc);
465fcf5ef2aSThomas Huth         return;
466fcf5ef2aSThomas Huth     }
467fcf5ef2aSThomas Huth 
468fcf5ef2aSThomas Huth     if (dc->rd)
469cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
470fcf5ef2aSThomas Huth }
471fcf5ef2aSThomas Huth 
4721074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
473fcf5ef2aSThomas Huth {
4741074c0fbSRichard Henderson     TCGv_i32 t;
4751074c0fbSRichard Henderson 
4761074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
4771074c0fbSRichard Henderson     t = tcg_temp_new_i32();
4781074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
4791074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
4801074c0fbSRichard Henderson     tcg_temp_free_i32(t);
481fcf5ef2aSThomas Huth }
482fcf5ef2aSThomas Huth 
4831074c0fbSRichard Henderson static void msr_write(DisasContext *dc, TCGv_i32 v)
484fcf5ef2aSThomas Huth {
485fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
4861074c0fbSRichard Henderson 
4871074c0fbSRichard Henderson     /* Install MSR_C.  */
4881074c0fbSRichard Henderson     tcg_gen_extract_i32(cpu_msr_c, v, 2, 1);
4891074c0fbSRichard Henderson 
4901074c0fbSRichard Henderson     /* Clear MSR_C and MSR_CC; MSR_PVR is not writable, and is always clear. */
4911074c0fbSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~(MSR_C | MSR_CC | MSR_PVR));
492fcf5ef2aSThomas Huth }
493fcf5ef2aSThomas Huth 
494fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
495fcf5ef2aSThomas Huth {
496fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
497cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4982023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
499f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
500fcf5ef2aSThomas Huth 
5012023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
5022023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
5032023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
504fcf5ef2aSThomas Huth     dc->type_b = 1;
5052023e9a3SEdgar E. Iglesias     if (to) {
506fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
507f0f7e7f7SEdgar E. Iglesias     }
508f0f7e7f7SEdgar E. Iglesias 
509f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
510f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
511f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
512f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
513f0f7e7f7SEdgar E. Iglesias 
514f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
5152023e9a3SEdgar E. Iglesias     }
516fcf5ef2aSThomas Huth 
517fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
5182023e9a3SEdgar E. Iglesias     if (clrset) {
5192023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
520fcf5ef2aSThomas Huth 
52156837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
522fcf5ef2aSThomas Huth             /* nop??? */
523fcf5ef2aSThomas Huth             return;
524fcf5ef2aSThomas Huth         }
525fcf5ef2aSThomas Huth 
526bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
527fcf5ef2aSThomas Huth             return;
528fcf5ef2aSThomas Huth         }
529fcf5ef2aSThomas Huth 
530fcf5ef2aSThomas Huth         if (dc->rd)
531fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
532fcf5ef2aSThomas Huth 
533cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
534cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
535fcf5ef2aSThomas Huth         msr_read(dc, t0);
536cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
537fcf5ef2aSThomas Huth 
538fcf5ef2aSThomas Huth         if (clr) {
539cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
540cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
541fcf5ef2aSThomas Huth         } else
542cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
543fcf5ef2aSThomas Huth         msr_write(dc, t0);
544cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
545cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
546d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
547d4705ae0SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
548fcf5ef2aSThomas Huth         return;
549fcf5ef2aSThomas Huth     }
550fcf5ef2aSThomas Huth 
551bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
552fcf5ef2aSThomas Huth         return;
553fcf5ef2aSThomas Huth     }
554fcf5ef2aSThomas Huth 
555fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
556fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
557fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
558f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
55905a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
56005a9a651SEdgar E. Iglesias 
561fcf5ef2aSThomas Huth         sr &= 7;
56205a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
56305a9a651SEdgar E. Iglesias         if (to) {
564f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
56505a9a651SEdgar E. Iglesias         } else {
566f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
56705a9a651SEdgar E. Iglesias         }
56805a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
569f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
570fcf5ef2aSThomas Huth         return;
571fcf5ef2aSThomas Huth     }
572fcf5ef2aSThomas Huth #endif
573fcf5ef2aSThomas Huth 
574fcf5ef2aSThomas Huth     if (to) {
575fcf5ef2aSThomas Huth         switch (sr) {
576aa28e6d4SRichard Henderson             case SR_PC:
577fcf5ef2aSThomas Huth                 break;
578aa28e6d4SRichard Henderson             case SR_MSR:
579fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
580fcf5ef2aSThomas Huth                 break;
581351527b7SEdgar E. Iglesias             case SR_EAR:
582dbdb77c4SRichard Henderson                 {
583dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
584dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
585dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
586dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
587dbdb77c4SRichard Henderson                 }
588aa28e6d4SRichard Henderson                 break;
589351527b7SEdgar E. Iglesias             case SR_ESR:
59041ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
59141ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
592aa28e6d4SRichard Henderson                 break;
593ab6dd380SEdgar E. Iglesias             case SR_FSR:
59486017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
59586017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
596aa28e6d4SRichard Henderson                 break;
597aa28e6d4SRichard Henderson             case SR_BTR:
598ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
599ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
600aa28e6d4SRichard Henderson                 break;
601aa28e6d4SRichard Henderson             case SR_EDR:
60239db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
60339db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
604fcf5ef2aSThomas Huth                 break;
605fcf5ef2aSThomas Huth             case 0x800:
606cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
607cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
608fcf5ef2aSThomas Huth                 break;
609fcf5ef2aSThomas Huth             case 0x802:
610cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
611cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
612fcf5ef2aSThomas Huth                 break;
613fcf5ef2aSThomas Huth             default:
614fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
615fcf5ef2aSThomas Huth                 break;
616fcf5ef2aSThomas Huth         }
617fcf5ef2aSThomas Huth     } else {
618fcf5ef2aSThomas Huth         switch (sr) {
619aa28e6d4SRichard Henderson             case SR_PC:
620d4705ae0SRichard Henderson                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
621fcf5ef2aSThomas Huth                 break;
622aa28e6d4SRichard Henderson             case SR_MSR:
623fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
624fcf5ef2aSThomas Huth                 break;
625351527b7SEdgar E. Iglesias             case SR_EAR:
626dbdb77c4SRichard Henderson                 {
627dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
628dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
629a1b48e3aSEdgar E. Iglesias                     if (extended) {
630dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
631aa28e6d4SRichard Henderson                     } else {
632dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
633dbdb77c4SRichard Henderson                     }
634dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
635a1b48e3aSEdgar E. Iglesias                 }
636aa28e6d4SRichard Henderson                 break;
637351527b7SEdgar E. Iglesias             case SR_ESR:
63841ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
63941ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
640aa28e6d4SRichard Henderson                 break;
641351527b7SEdgar E. Iglesias             case SR_FSR:
64286017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
64386017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
644aa28e6d4SRichard Henderson                 break;
645351527b7SEdgar E. Iglesias             case SR_BTR:
646ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
647ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
648aa28e6d4SRichard Henderson                 break;
6497cdae31dSTong Ho             case SR_EDR:
65039db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
65139db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
652fcf5ef2aSThomas Huth                 break;
653fcf5ef2aSThomas Huth             case 0x800:
654cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
655cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
656fcf5ef2aSThomas Huth                 break;
657fcf5ef2aSThomas Huth             case 0x802:
658cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
659cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
660fcf5ef2aSThomas Huth                 break;
661351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
662fcf5ef2aSThomas Huth                 rn = sr & 0xf;
663cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
664fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
665fcf5ef2aSThomas Huth                 break;
666fcf5ef2aSThomas Huth             default:
667fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
668fcf5ef2aSThomas Huth                 break;
669fcf5ef2aSThomas Huth         }
670fcf5ef2aSThomas Huth     }
671fcf5ef2aSThomas Huth 
672fcf5ef2aSThomas Huth     if (dc->rd == 0) {
673cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
674fcf5ef2aSThomas Huth     }
675fcf5ef2aSThomas Huth }
676fcf5ef2aSThomas Huth 
677fcf5ef2aSThomas Huth /* Multiplier unit.  */
678fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
679fcf5ef2aSThomas Huth {
680cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
681fcf5ef2aSThomas Huth     unsigned int subcode;
682fcf5ef2aSThomas Huth 
6839ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
684fcf5ef2aSThomas Huth         return;
685fcf5ef2aSThomas Huth     }
686fcf5ef2aSThomas Huth 
687fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
688fcf5ef2aSThomas Huth 
689fcf5ef2aSThomas Huth     if (dc->type_b) {
690cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
691fcf5ef2aSThomas Huth         return;
692fcf5ef2aSThomas Huth     }
693fcf5ef2aSThomas Huth 
694fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6959b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
696fcf5ef2aSThomas Huth         /* nop??? */
697fcf5ef2aSThomas Huth     }
698fcf5ef2aSThomas Huth 
699cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
700fcf5ef2aSThomas Huth     switch (subcode) {
701fcf5ef2aSThomas Huth         case 0:
702cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
703fcf5ef2aSThomas Huth             break;
704fcf5ef2aSThomas Huth         case 1:
705cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
706cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
707fcf5ef2aSThomas Huth             break;
708fcf5ef2aSThomas Huth         case 2:
709cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
710cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
711fcf5ef2aSThomas Huth             break;
712fcf5ef2aSThomas Huth         case 3:
713cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
714fcf5ef2aSThomas Huth             break;
715fcf5ef2aSThomas Huth         default:
716fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
717fcf5ef2aSThomas Huth             break;
718fcf5ef2aSThomas Huth     }
719cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
720fcf5ef2aSThomas Huth }
721fcf5ef2aSThomas Huth 
722fcf5ef2aSThomas Huth /* Div unit.  */
723fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
724fcf5ef2aSThomas Huth {
725fcf5ef2aSThomas Huth     unsigned int u;
726fcf5ef2aSThomas Huth 
727fcf5ef2aSThomas Huth     u = dc->imm & 2;
728fcf5ef2aSThomas Huth 
7299ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
7309ba8cd45SEdgar E. Iglesias         return;
731fcf5ef2aSThomas Huth     }
732fcf5ef2aSThomas Huth 
733fcf5ef2aSThomas Huth     if (u)
734fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
735fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
736fcf5ef2aSThomas Huth     else
737fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
738fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
739fcf5ef2aSThomas Huth     if (!dc->rd)
740cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
741fcf5ef2aSThomas Huth }
742fcf5ef2aSThomas Huth 
743fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
744fcf5ef2aSThomas Huth {
745cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
746faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
747d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
748fcf5ef2aSThomas Huth 
7499ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
750fcf5ef2aSThomas Huth         return;
751fcf5ef2aSThomas Huth     }
752fcf5ef2aSThomas Huth 
753faa48d74SEdgar E. Iglesias     if (dc->type_b) {
754faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
755d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
756faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
757faa48d74SEdgar E. Iglesias     }
758e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
759e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
760faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
761faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
762fcf5ef2aSThomas Huth 
763faa48d74SEdgar E. Iglesias     if (e) {
764faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
765faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
766faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
767faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
768faa48d74SEdgar E. Iglesias         } else {
769faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
770faa48d74SEdgar E. Iglesias         }
771d09b2585SEdgar E. Iglesias     } else if (i) {
772d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
773d09b2585SEdgar E. Iglesias 
774d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
775d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
776d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
777d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
778d09b2585SEdgar E. Iglesias         } else {
779d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
780d09b2585SEdgar E. Iglesias                                 imm_s, width);
781d09b2585SEdgar E. Iglesias         }
782faa48d74SEdgar E. Iglesias     } else {
783cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
784fcf5ef2aSThomas Huth 
785cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
786cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
787fcf5ef2aSThomas Huth 
7882acf6d53SEdgar E. Iglesias         if (s) {
789cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7902acf6d53SEdgar E. Iglesias         } else {
7912acf6d53SEdgar E. Iglesias             if (t) {
792cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7932acf6d53SEdgar E. Iglesias             } else {
794cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
795fcf5ef2aSThomas Huth             }
796fcf5ef2aSThomas Huth         }
797cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7982acf6d53SEdgar E. Iglesias     }
799faa48d74SEdgar E. Iglesias }
800fcf5ef2aSThomas Huth 
801fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
802fcf5ef2aSThomas Huth {
803fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
804cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
805fcf5ef2aSThomas Huth     unsigned int op;
806fcf5ef2aSThomas Huth 
807fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
808fcf5ef2aSThomas Huth     switch (op) {
809fcf5ef2aSThomas Huth         case 0x21:
810fcf5ef2aSThomas Huth             /* src.  */
811cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
812fcf5ef2aSThomas Huth 
8131074c0fbSRichard Henderson             tcg_gen_shli_i32(t0, cpu_msr_c, 31);
8141074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
815fcf5ef2aSThomas Huth             if (dc->rd) {
816cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
817cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
818fcf5ef2aSThomas Huth             }
819cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
820fcf5ef2aSThomas Huth             break;
821fcf5ef2aSThomas Huth 
822fcf5ef2aSThomas Huth         case 0x1:
823fcf5ef2aSThomas Huth         case 0x41:
824fcf5ef2aSThomas Huth             /* srl.  */
8251074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
826fcf5ef2aSThomas Huth             if (dc->rd) {
827fcf5ef2aSThomas Huth                 if (op == 0x41)
828cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
829fcf5ef2aSThomas Huth                 else
830cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
831fcf5ef2aSThomas Huth             }
832fcf5ef2aSThomas Huth             break;
833fcf5ef2aSThomas Huth         case 0x60:
834fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
835fcf5ef2aSThomas Huth             break;
836fcf5ef2aSThomas Huth         case 0x61:
837fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
838fcf5ef2aSThomas Huth             break;
839fcf5ef2aSThomas Huth         case 0x64:
840fcf5ef2aSThomas Huth         case 0x66:
841fcf5ef2aSThomas Huth         case 0x74:
842fcf5ef2aSThomas Huth         case 0x76:
843fcf5ef2aSThomas Huth             /* wdc.  */
844bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
845fcf5ef2aSThomas Huth             break;
846fcf5ef2aSThomas Huth         case 0x68:
847fcf5ef2aSThomas Huth             /* wic.  */
848bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
849fcf5ef2aSThomas Huth             break;
850fcf5ef2aSThomas Huth         case 0xe0:
8519ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
8529ba8cd45SEdgar E. Iglesias                 return;
853fcf5ef2aSThomas Huth             }
8548fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
8555318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
856fcf5ef2aSThomas Huth             }
857fcf5ef2aSThomas Huth             break;
858fcf5ef2aSThomas Huth         case 0x1e0:
859fcf5ef2aSThomas Huth             /* swapb */
860fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
861fcf5ef2aSThomas Huth             break;
862fcf5ef2aSThomas Huth         case 0x1e2:
863fcf5ef2aSThomas Huth             /*swaph */
864fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
865fcf5ef2aSThomas Huth             break;
866fcf5ef2aSThomas Huth         default:
867fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
868d4705ae0SRichard Henderson                       (uint32_t)dc->base.pc_next, op, dc->rd, dc->ra, dc->rb);
869fcf5ef2aSThomas Huth             break;
870fcf5ef2aSThomas Huth     }
871fcf5ef2aSThomas Huth }
872fcf5ef2aSThomas Huth 
873fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
874fcf5ef2aSThomas Huth {
875fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
876fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
8779b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
878fcf5ef2aSThomas Huth         }
879fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
8800f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
881fcf5ef2aSThomas Huth     }
882fcf5ef2aSThomas Huth }
883fcf5ef2aSThomas Huth 
884fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
885fcf5ef2aSThomas Huth {
886d7ecb757SRichard Henderson     dc->ext_imm = dc->imm << 16;
887d7ecb757SRichard Henderson     tcg_gen_movi_i32(cpu_imm, dc->ext_imm);
888fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
889fcf5ef2aSThomas Huth     dc->clear_imm = 0;
890fcf5ef2aSThomas Huth }
891fcf5ef2aSThomas Huth 
892d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
893fcf5ef2aSThomas Huth {
8940e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8950e9033c8SEdgar E. Iglesias     bool stackprot = false;
896403322eaSEdgar E. Iglesias     TCGv_i32 t32;
897fcf5ef2aSThomas Huth 
898fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
899fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
9000e9033c8SEdgar E. Iglesias         stackprot = true;
901fcf5ef2aSThomas Huth     }
902fcf5ef2aSThomas Huth 
903fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
904fcf5ef2aSThomas Huth     if (!dc->type_b) {
905d248e1beSEdgar E. Iglesias         if (ea) {
906d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
907d248e1beSEdgar E. Iglesias 
908d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
909d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
910d248e1beSEdgar E. Iglesias                 return;
911d248e1beSEdgar E. Iglesias             }
912d248e1beSEdgar E. Iglesias 
913d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
914d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
915d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
916d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
917d248e1beSEdgar E. Iglesias             }
918d248e1beSEdgar E. Iglesias             return;
919d248e1beSEdgar E. Iglesias         }
920d248e1beSEdgar E. Iglesias 
9210dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
922fcf5ef2aSThomas Huth         if (dc->ra == 0) {
923403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
9240dc4af5cSEdgar E. Iglesias             return;
925fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
926403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
9270dc4af5cSEdgar E. Iglesias             return;
928fcf5ef2aSThomas Huth         }
929fcf5ef2aSThomas Huth 
930fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
9310e9033c8SEdgar E. Iglesias             stackprot = true;
932fcf5ef2aSThomas Huth         }
933fcf5ef2aSThomas Huth 
934403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
935403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
936403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
937403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
938fcf5ef2aSThomas Huth 
939fcf5ef2aSThomas Huth         if (stackprot) {
9400a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
941fcf5ef2aSThomas Huth         }
9420dc4af5cSEdgar E. Iglesias         return;
943fcf5ef2aSThomas Huth     }
944fcf5ef2aSThomas Huth     /* Immediate.  */
945403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
946d7ecb757SRichard Henderson     tcg_gen_addi_i32(t32, cpu_R[dc->ra], dec_alu_typeb_imm(dc));
947403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
948403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
949fcf5ef2aSThomas Huth 
950fcf5ef2aSThomas Huth     if (stackprot) {
9510a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
952fcf5ef2aSThomas Huth     }
9530dc4af5cSEdgar E. Iglesias     return;
954fcf5ef2aSThomas Huth }
955fcf5ef2aSThomas Huth 
956fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
957fcf5ef2aSThomas Huth {
958403322eaSEdgar E. Iglesias     TCGv_i32 v;
959403322eaSEdgar E. Iglesias     TCGv addr;
9608534063aSEdgar E. Iglesias     unsigned int size;
961d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
962d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
96314776ab5STony Nguyen     MemOp mop;
964fcf5ef2aSThomas Huth 
965fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
966fcf5ef2aSThomas Huth     size = 1 << mop;
967fcf5ef2aSThomas Huth     if (!dc->type_b) {
968d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9698534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9708534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
971fcf5ef2aSThomas Huth     }
972fcf5ef2aSThomas Huth     mop |= MO_TE;
973fcf5ef2aSThomas Huth     if (rev) {
974fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
975fcf5ef2aSThomas Huth     }
976fcf5ef2aSThomas Huth 
9779ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
978fcf5ef2aSThomas Huth         return;
979fcf5ef2aSThomas Huth     }
980fcf5ef2aSThomas Huth 
981d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
982d248e1beSEdgar E. Iglesias         return;
983d248e1beSEdgar E. Iglesias     }
984d248e1beSEdgar E. Iglesias 
985fcf5ef2aSThomas Huth     t_sync_flags(dc);
986403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
987d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
988d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
989d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
990fcf5ef2aSThomas Huth 
991fcf5ef2aSThomas Huth     /*
992fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
993fcf5ef2aSThomas Huth      *
994fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
995fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
996fcf5ef2aSThomas Huth      */
997fcf5ef2aSThomas Huth     if (rev && size != 4) {
998fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
999fcf5ef2aSThomas Huth         switch (size) {
1000fcf5ef2aSThomas Huth             case 1:
1001fcf5ef2aSThomas Huth             {
1002a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1003fcf5ef2aSThomas Huth                 break;
1004fcf5ef2aSThomas Huth             }
1005fcf5ef2aSThomas Huth 
1006fcf5ef2aSThomas Huth             case 2:
1007fcf5ef2aSThomas Huth                 /* 00 -> 10
1008fcf5ef2aSThomas Huth                    10 -> 00.  */
1009403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1010fcf5ef2aSThomas Huth                 break;
1011fcf5ef2aSThomas Huth             default:
1012fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1013fcf5ef2aSThomas Huth                 break;
1014fcf5ef2aSThomas Huth         }
1015fcf5ef2aSThomas Huth     }
1016fcf5ef2aSThomas Huth 
1017fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
1018fcf5ef2aSThomas Huth     if (ex) {
1019403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1020fcf5ef2aSThomas Huth     }
1021fcf5ef2aSThomas Huth 
1022fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1023fcf5ef2aSThomas Huth     sync_jmpstate(dc);
1024fcf5ef2aSThomas Huth 
1025fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
1026fcf5ef2aSThomas Huth     /*
1027fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
1028fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
1029fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
1030fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
1031fcf5ef2aSThomas Huth      */
1032cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
1033d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
1034fcf5ef2aSThomas Huth 
10351507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1036a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
1037a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1038a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1039a6338015SEdgar E. Iglesias 
1040d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1041a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
1042a6338015SEdgar E. Iglesias 
1043a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
1044a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1045a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1046fcf5ef2aSThomas Huth     }
1047fcf5ef2aSThomas Huth 
1048fcf5ef2aSThomas Huth     if (ex) {
10499b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
10509b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
1051fcf5ef2aSThomas Huth     }
1052fcf5ef2aSThomas Huth     if (dc->rd) {
1053cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1054fcf5ef2aSThomas Huth     }
1055cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1056fcf5ef2aSThomas Huth 
1057fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1058fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
10591074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1060fcf5ef2aSThomas Huth     }
1061fcf5ef2aSThomas Huth 
1062403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1063fcf5ef2aSThomas Huth }
1064fcf5ef2aSThomas Huth 
1065fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1066fcf5ef2aSThomas Huth {
1067403322eaSEdgar E. Iglesias     TCGv addr;
1068fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1069b51b3d43SEdgar E. Iglesias     unsigned int size;
1070d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1071d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
107214776ab5STony Nguyen     MemOp mop;
1073fcf5ef2aSThomas Huth 
1074fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1075fcf5ef2aSThomas Huth     size = 1 << mop;
1076fcf5ef2aSThomas Huth     if (!dc->type_b) {
1077d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1078b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1079b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1080fcf5ef2aSThomas Huth     }
1081fcf5ef2aSThomas Huth     mop |= MO_TE;
1082fcf5ef2aSThomas Huth     if (rev) {
1083fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1084fcf5ef2aSThomas Huth     }
1085fcf5ef2aSThomas Huth 
10869ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1087fcf5ef2aSThomas Huth         return;
1088fcf5ef2aSThomas Huth     }
1089fcf5ef2aSThomas Huth 
1090d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1091d248e1beSEdgar E. Iglesias 
1092fcf5ef2aSThomas Huth     t_sync_flags(dc);
1093fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1094fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10950dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1096403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1097d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1098d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1099d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1100fcf5ef2aSThomas Huth 
1101fcf5ef2aSThomas Huth     if (ex) { /* swx */
1102cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1103fcf5ef2aSThomas Huth 
1104fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1105403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1106fcf5ef2aSThomas Huth 
11071074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 1);
1108fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
11099b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1110fcf5ef2aSThomas Huth 
1111071cdc67SEdgar E. Iglesias         /*
1112071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1113071cdc67SEdgar E. Iglesias          * the reserved location.
1114071cdc67SEdgar E. Iglesias          */
1115cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1116071cdc67SEdgar E. Iglesias 
11179b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1118071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1119071cdc67SEdgar E. Iglesias                                    mop);
1120071cdc67SEdgar E. Iglesias 
11219b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
11221074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1123cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1124fcf5ef2aSThomas Huth     }
1125fcf5ef2aSThomas Huth 
1126fcf5ef2aSThomas Huth     if (rev && size != 4) {
1127fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1128fcf5ef2aSThomas Huth         switch (size) {
1129fcf5ef2aSThomas Huth             case 1:
1130fcf5ef2aSThomas Huth             {
1131a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1132fcf5ef2aSThomas Huth                 break;
1133fcf5ef2aSThomas Huth             }
1134fcf5ef2aSThomas Huth 
1135fcf5ef2aSThomas Huth             case 2:
1136fcf5ef2aSThomas Huth                 /* 00 -> 10
1137fcf5ef2aSThomas Huth                    10 -> 00.  */
1138fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1139403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1140fcf5ef2aSThomas Huth                 break;
1141fcf5ef2aSThomas Huth             default:
1142fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1143fcf5ef2aSThomas Huth                 break;
1144fcf5ef2aSThomas Huth         }
1145fcf5ef2aSThomas Huth     }
1146071cdc67SEdgar E. Iglesias 
1147071cdc67SEdgar E. Iglesias     if (!ex) {
1148d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1149071cdc67SEdgar E. Iglesias     }
1150fcf5ef2aSThomas Huth 
1151fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11521507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1153a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1154a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1155a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1156a6338015SEdgar E. Iglesias 
1157d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1158fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1159fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1160fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1161fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1162fcf5ef2aSThomas Huth          *        access.
1163fcf5ef2aSThomas Huth          */
1164a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1165a6338015SEdgar E. Iglesias 
1166a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1167a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1168a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1169fcf5ef2aSThomas Huth     }
1170fcf5ef2aSThomas Huth 
1171fcf5ef2aSThomas Huth     if (ex) {
1172fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1173fcf5ef2aSThomas Huth     }
1174fcf5ef2aSThomas Huth 
1175403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1176fcf5ef2aSThomas Huth }
1177fcf5ef2aSThomas Huth 
1178fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11799e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1180fcf5ef2aSThomas Huth {
1181d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1182d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1183d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1184d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1185d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1186d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1187d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1188d89b86e9SEdgar E. Iglesias     };
1189d89b86e9SEdgar E. Iglesias 
1190fcf5ef2aSThomas Huth     switch (cc) {
1191fcf5ef2aSThomas Huth     case CC_EQ:
1192fcf5ef2aSThomas Huth     case CC_NE:
1193fcf5ef2aSThomas Huth     case CC_LT:
1194fcf5ef2aSThomas Huth     case CC_LE:
1195fcf5ef2aSThomas Huth     case CC_GE:
1196fcf5ef2aSThomas Huth     case CC_GT:
11979e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1198fcf5ef2aSThomas Huth         break;
1199fcf5ef2aSThomas Huth     default:
1200fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1201fcf5ef2aSThomas Huth         break;
1202fcf5ef2aSThomas Huth     }
1203fcf5ef2aSThomas Huth }
1204fcf5ef2aSThomas Huth 
12050f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1206fcf5ef2aSThomas Huth {
12070f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1208e956caf2SEdgar E. Iglesias 
12090f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
12109b158558SRichard Henderson                         cpu_btaken, zero,
1211e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1212e956caf2SEdgar E. Iglesias 
12130f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1214fcf5ef2aSThomas Huth }
1215fcf5ef2aSThomas Huth 
1216f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1217f91c60f0SEdgar E. Iglesias {
1218f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1219f91c60f0SEdgar E. Iglesias 
1220f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1221f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1222f91c60f0SEdgar E. Iglesias 
1223f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1224f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1225f91c60f0SEdgar E. Iglesias }
1226f91c60f0SEdgar E. Iglesias 
1227fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1228fcf5ef2aSThomas Huth {
1229fcf5ef2aSThomas Huth     unsigned int cc;
1230fcf5ef2aSThomas Huth     unsigned int dslot;
1231fcf5ef2aSThomas Huth 
1232fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1233fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1234fcf5ef2aSThomas Huth 
1235fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1236fcf5ef2aSThomas Huth     if (dslot) {
1237f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1238fcf5ef2aSThomas Huth     }
1239fcf5ef2aSThomas Huth 
1240d7ecb757SRichard Henderson     if (dc->type_b) {
1241fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
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 {
1245fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1246d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1247fcf5ef2aSThomas Huth     }
12489b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1249fcf5ef2aSThomas Huth }
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1252fcf5ef2aSThomas Huth {
1253fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1254fcf5ef2aSThomas Huth 
1255fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1256fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1257fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1258fcf5ef2aSThomas Huth 
1259fcf5ef2aSThomas Huth     /* Memory barrier.  */
1260fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1261fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1262badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1263badcbf9dSEdgar E. Iglesias 
12643f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12653f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12663f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12673f172744SEdgar E. Iglesias         }
12683f172744SEdgar E. Iglesias 
1269fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1270badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
127141ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1272fcf5ef2aSThomas Huth 
1273b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1274b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1275b4919e7dSEdgar E. Iglesias                 return;
1276b4919e7dSEdgar E. Iglesias             }
1277b4919e7dSEdgar E. Iglesias 
1278fcf5ef2aSThomas Huth             t_sync_flags(dc);
127941ba37c4SRichard Henderson 
128041ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1281fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1282fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1283fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1284fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
128541ba37c4SRichard Henderson 
1286d4705ae0SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
128741ba37c4SRichard Henderson 
128841ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1289fcf5ef2aSThomas Huth             return;
1290fcf5ef2aSThomas Huth         }
1291fcf5ef2aSThomas Huth         /* Break the TB.  */
1292fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1293fcf5ef2aSThomas Huth         return;
1294fcf5ef2aSThomas Huth     }
1295fcf5ef2aSThomas Huth 
1296d7ecb757SRichard Henderson     if (abs && link && !dslot) {
1297d7ecb757SRichard Henderson         if (dc->type_b) {
1298d7ecb757SRichard Henderson             /* BRKI */
1299d7ecb757SRichard Henderson             uint32_t imm = dec_alu_typeb_imm(dc);
1300d7ecb757SRichard Henderson             if (trap_userspace(dc, imm != 8 && imm != 0x18)) {
1301d7ecb757SRichard Henderson                 return;
1302d7ecb757SRichard Henderson             }
1303d7ecb757SRichard Henderson         } else {
1304d7ecb757SRichard Henderson             /* BRK */
1305d7ecb757SRichard Henderson             if (trap_userspace(dc, true)) {
1306d7ecb757SRichard Henderson                 return;
1307d7ecb757SRichard Henderson             }
1308d7ecb757SRichard Henderson         }
1309d7ecb757SRichard Henderson     }
1310d7ecb757SRichard Henderson 
1311fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1312fcf5ef2aSThomas Huth     if (dslot) {
1313f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1314fcf5ef2aSThomas Huth     }
1315d7ecb757SRichard Henderson     if (link && dc->rd) {
1316d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
1317d7ecb757SRichard Henderson     }
1318fcf5ef2aSThomas Huth 
1319fcf5ef2aSThomas Huth     if (abs) {
1320d7ecb757SRichard Henderson         if (dc->type_b) {
1321d7ecb757SRichard Henderson             uint32_t dest = dec_alu_typeb_imm(dc);
1322d7ecb757SRichard Henderson 
1323d7ecb757SRichard Henderson             dc->jmp = JMP_DIRECT;
1324d7ecb757SRichard Henderson             dc->jmp_pc = dest;
1325d7ecb757SRichard Henderson             tcg_gen_movi_i32(cpu_btarget, dest);
1326fcf5ef2aSThomas Huth             if (link && !dslot) {
1327d7ecb757SRichard Henderson                 switch (dest) {
1328d7ecb757SRichard Henderson                 case 8:
1329d7ecb757SRichard Henderson                 case 0x18:
1330d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_BREAK);
1331d7ecb757SRichard Henderson                     break;
1332d7ecb757SRichard Henderson                 case 0:
1333d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_DEBUG);
1334d7ecb757SRichard Henderson                     break;
1335d7ecb757SRichard Henderson                 }
1336d7ecb757SRichard Henderson             }
1337d7ecb757SRichard Henderson         } else {
1338d7ecb757SRichard Henderson             dc->jmp = JMP_INDIRECT;
1339d7ecb757SRichard Henderson             tcg_gen_mov_i32(cpu_btarget, cpu_R[dc->rb]);
1340d7ecb757SRichard Henderson             if (link && !dslot) {
134141ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
134241ba37c4SRichard Henderson             }
1343fcf5ef2aSThomas Huth         }
1344d7ecb757SRichard Henderson     } else if (dc->type_b) {
1345fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT;
1346d7ecb757SRichard Henderson         dc->jmp_pc = dc->base.pc_next + dec_alu_typeb_imm(dc);
1347d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
1348fcf5ef2aSThomas Huth     } else {
1349d7ecb757SRichard Henderson         dc->jmp = JMP_INDIRECT;
1350d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1351d7ecb757SRichard Henderson     }
13529b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
1353fcf5ef2aSThomas Huth }
1354fcf5ef2aSThomas Huth 
1355fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1356fcf5ef2aSThomas Huth {
1357cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1358cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1359cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13603e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13610a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13620a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1363cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1364fcf5ef2aSThomas Huth 
1365cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1366cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1367fcf5ef2aSThomas Huth     msr_write(dc, t1);
1368cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1369cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1370fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1371fcf5ef2aSThomas Huth }
1372fcf5ef2aSThomas Huth 
1373fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1374fcf5ef2aSThomas Huth {
1375cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1376cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1377cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13783e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13790a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1380cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1381cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1382fcf5ef2aSThomas Huth 
1383cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1384cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1385fcf5ef2aSThomas Huth     msr_write(dc, t1);
1386cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1387cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1388fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1389fcf5ef2aSThomas Huth }
1390fcf5ef2aSThomas Huth 
1391fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1392fcf5ef2aSThomas Huth {
1393cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1394cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1395cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1396fcf5ef2aSThomas Huth 
13973e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13980a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1399cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1400cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1401cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1402fcf5ef2aSThomas Huth 
1403cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1404cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1405fcf5ef2aSThomas Huth     msr_write(dc, t1);
1406cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1407cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1408fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1409fcf5ef2aSThomas Huth }
1410fcf5ef2aSThomas Huth 
1411fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1412fcf5ef2aSThomas Huth {
1413fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1414fcf5ef2aSThomas Huth 
1415fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1416fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1417fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1418fcf5ef2aSThomas Huth 
1419bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1420bdfc1e88SEdgar E. Iglesias         return;
1421bdfc1e88SEdgar E. Iglesias     }
1422bdfc1e88SEdgar E. Iglesias 
1423f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1424fcf5ef2aSThomas Huth 
1425fcf5ef2aSThomas Huth     if (i_bit) {
1426fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1427fcf5ef2aSThomas Huth     } else if (b_bit) {
1428fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1429fcf5ef2aSThomas Huth     } else if (e_bit) {
1430fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
143111105d67SRichard Henderson     }
1432fcf5ef2aSThomas Huth 
1433fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
14349b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
14350f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1436fcf5ef2aSThomas Huth }
1437fcf5ef2aSThomas Huth 
1438fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1439fcf5ef2aSThomas Huth {
1440fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
144141ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1442fcf5ef2aSThomas Huth     }
14432016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1444fcf5ef2aSThomas Huth }
1445fcf5ef2aSThomas Huth 
1446fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1447fcf5ef2aSThomas Huth {
1448fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1449fcf5ef2aSThomas Huth 
14509ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1451fcf5ef2aSThomas Huth         return;
1452fcf5ef2aSThomas Huth     }
1453fcf5ef2aSThomas Huth 
1454fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1455fcf5ef2aSThomas Huth 
1456fcf5ef2aSThomas Huth     switch (fpu_insn) {
1457fcf5ef2aSThomas Huth         case 0:
1458fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1459fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1460fcf5ef2aSThomas Huth             break;
1461fcf5ef2aSThomas Huth 
1462fcf5ef2aSThomas Huth         case 1:
1463fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1464fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1465fcf5ef2aSThomas Huth             break;
1466fcf5ef2aSThomas Huth 
1467fcf5ef2aSThomas Huth         case 2:
1468fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1469fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1470fcf5ef2aSThomas Huth             break;
1471fcf5ef2aSThomas Huth 
1472fcf5ef2aSThomas Huth         case 3:
1473fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1474fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1475fcf5ef2aSThomas Huth             break;
1476fcf5ef2aSThomas Huth 
1477fcf5ef2aSThomas Huth         case 4:
1478fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1479fcf5ef2aSThomas Huth                 case 0:
1480fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1481fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1482fcf5ef2aSThomas Huth                     break;
1483fcf5ef2aSThomas Huth                 case 1:
1484fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1485fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1486fcf5ef2aSThomas Huth                     break;
1487fcf5ef2aSThomas Huth                 case 2:
1488fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1489fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1490fcf5ef2aSThomas Huth                     break;
1491fcf5ef2aSThomas Huth                 case 3:
1492fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1493fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1494fcf5ef2aSThomas Huth                     break;
1495fcf5ef2aSThomas Huth                 case 4:
1496fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1497fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1498fcf5ef2aSThomas Huth                     break;
1499fcf5ef2aSThomas Huth                 case 5:
1500fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1501fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1502fcf5ef2aSThomas Huth                     break;
1503fcf5ef2aSThomas Huth                 case 6:
1504fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1505fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1506fcf5ef2aSThomas Huth                     break;
1507fcf5ef2aSThomas Huth                 default:
1508fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1509fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1510fcf5ef2aSThomas Huth                                   " opc=%x\n",
1511d4705ae0SRichard Henderson                                   fpu_insn, (uint32_t)dc->base.pc_next,
1512d4705ae0SRichard Henderson                                   dc->opcode);
1513fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1514fcf5ef2aSThomas Huth                     break;
1515fcf5ef2aSThomas Huth             }
1516fcf5ef2aSThomas Huth             break;
1517fcf5ef2aSThomas Huth 
1518fcf5ef2aSThomas Huth         case 5:
1519fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1520fcf5ef2aSThomas Huth                 return;
1521fcf5ef2aSThomas Huth             }
1522fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1523fcf5ef2aSThomas Huth             break;
1524fcf5ef2aSThomas Huth 
1525fcf5ef2aSThomas Huth         case 6:
1526fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1527fcf5ef2aSThomas Huth                 return;
1528fcf5ef2aSThomas Huth             }
1529fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1530fcf5ef2aSThomas Huth             break;
1531fcf5ef2aSThomas Huth 
1532fcf5ef2aSThomas Huth         case 7:
1533fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1534fcf5ef2aSThomas Huth                 return;
1535fcf5ef2aSThomas Huth             }
1536fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1537fcf5ef2aSThomas Huth             break;
1538fcf5ef2aSThomas Huth 
1539fcf5ef2aSThomas Huth         default:
1540fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1541fcf5ef2aSThomas Huth                           " opc=%x\n",
1542d4705ae0SRichard Henderson                           fpu_insn, (uint32_t)dc->base.pc_next, dc->opcode);
1543fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1544fcf5ef2aSThomas Huth             break;
1545fcf5ef2aSThomas Huth     }
1546fcf5ef2aSThomas Huth }
1547fcf5ef2aSThomas Huth 
1548fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1549fcf5ef2aSThomas Huth {
15509ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1551fcf5ef2aSThomas Huth         return;
1552fcf5ef2aSThomas Huth     }
1553d4705ae0SRichard Henderson     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n",
1554d4705ae0SRichard Henderson                   (uint32_t)dc->base.pc_next, dc->opcode);
1555fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1556fcf5ef2aSThomas Huth }
1557fcf5ef2aSThomas Huth 
1558fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1559fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1560fcf5ef2aSThomas Huth {
1561fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1562fcf5ef2aSThomas Huth     int ctrl;
1563fcf5ef2aSThomas Huth 
1564bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1565fcf5ef2aSThomas Huth         return;
1566fcf5ef2aSThomas Huth     }
1567fcf5ef2aSThomas Huth 
1568cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1569fcf5ef2aSThomas Huth     if (dc->type_b) {
1570cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1571fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1572fcf5ef2aSThomas Huth     } else {
1573cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1574fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1575fcf5ef2aSThomas Huth     }
1576fcf5ef2aSThomas Huth 
1577cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1578fcf5ef2aSThomas Huth 
1579fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1580fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1581fcf5ef2aSThomas Huth     } else {
1582fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1583fcf5ef2aSThomas Huth     }
1584cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1585cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1586fcf5ef2aSThomas Huth }
1587fcf5ef2aSThomas Huth 
1588fcf5ef2aSThomas Huth static struct decoder_info {
1589fcf5ef2aSThomas Huth     struct {
1590fcf5ef2aSThomas Huth         uint32_t bits;
1591fcf5ef2aSThomas Huth         uint32_t mask;
1592fcf5ef2aSThomas Huth     };
1593fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1594fcf5ef2aSThomas Huth } decinfo[] = {
1595fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1596fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1597fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1598fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1599fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1600fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1601fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1602fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1603fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1604fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1605fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1606fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1607fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1608fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1609fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1610fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1611fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1612fcf5ef2aSThomas Huth };
1613fcf5ef2aSThomas Huth 
161444d1432bSRichard Henderson static void old_decode(DisasContext *dc, uint32_t ir)
1615fcf5ef2aSThomas Huth {
1616fcf5ef2aSThomas Huth     int i;
1617fcf5ef2aSThomas Huth 
1618fcf5ef2aSThomas Huth     dc->ir = ir;
1619fcf5ef2aSThomas Huth 
1620fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1621fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1622fcf5ef2aSThomas Huth 
1623fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1624fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1625fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1626fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1627fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1628fcf5ef2aSThomas Huth 
1629fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1630fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1631fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1632fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1633fcf5ef2aSThomas Huth             break;
1634fcf5ef2aSThomas Huth         }
1635fcf5ef2aSThomas Huth     }
1636fcf5ef2aSThomas Huth }
1637fcf5ef2aSThomas Huth 
1638372122e3SRichard Henderson static void mb_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1639fcf5ef2aSThomas Huth {
1640372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1641372122e3SRichard Henderson     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1642372122e3SRichard Henderson     int bound;
1643fcf5ef2aSThomas Huth 
1644fcf5ef2aSThomas Huth     dc->cpu = cpu;
1645372122e3SRichard Henderson     dc->synced_flags = dc->tb_flags = dc->base.tb->flags;
1646fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1647372122e3SRichard Henderson     dc->jmp = dc->delayed_branch ? JMP_INDIRECT : JMP_NOJMP;
1648fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1649fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1650d7ecb757SRichard Henderson     dc->ext_imm = dc->base.tb->cs_base;
165120800179SRichard Henderson     dc->r0 = NULL;
165220800179SRichard Henderson     dc->r0_set = false;
1653fcf5ef2aSThomas Huth 
1654372122e3SRichard Henderson     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1655372122e3SRichard Henderson     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1656fcf5ef2aSThomas Huth }
1657fcf5ef2aSThomas Huth 
1658372122e3SRichard Henderson static void mb_tr_tb_start(DisasContextBase *dcb, CPUState *cs)
1659fcf5ef2aSThomas Huth {
1660fcf5ef2aSThomas Huth }
1661fcf5ef2aSThomas Huth 
1662372122e3SRichard Henderson static void mb_tr_insn_start(DisasContextBase *dcb, CPUState *cs)
1663372122e3SRichard Henderson {
1664372122e3SRichard Henderson     tcg_gen_insn_start(dcb->pc_next);
1665372122e3SRichard Henderson }
1666fcf5ef2aSThomas Huth 
1667372122e3SRichard Henderson static bool mb_tr_breakpoint_check(DisasContextBase *dcb, CPUState *cs,
1668372122e3SRichard Henderson                                    const CPUBreakpoint *bp)
1669372122e3SRichard Henderson {
1670372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1671372122e3SRichard Henderson 
1672372122e3SRichard Henderson     gen_raise_exception_sync(dc, EXCP_DEBUG);
1673372122e3SRichard Henderson 
1674372122e3SRichard Henderson     /*
1675372122e3SRichard Henderson      * The address covered by the breakpoint must be included in
1676372122e3SRichard Henderson      * [tb->pc, tb->pc + tb->size) in order to for it to be
1677372122e3SRichard Henderson      * properly cleared -- thus we increment the PC here so that
1678372122e3SRichard Henderson      * the logic setting tb->size below does the right thing.
1679372122e3SRichard Henderson      */
1680372122e3SRichard Henderson     dc->base.pc_next += 4;
1681372122e3SRichard Henderson     return true;
1682372122e3SRichard Henderson }
1683372122e3SRichard Henderson 
1684372122e3SRichard Henderson static void mb_tr_translate_insn(DisasContextBase *dcb, CPUState *cs)
1685372122e3SRichard Henderson {
1686372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1687372122e3SRichard Henderson     CPUMBState *env = cs->env_ptr;
168844d1432bSRichard Henderson     uint32_t ir;
1689372122e3SRichard Henderson 
1690372122e3SRichard Henderson     /* TODO: This should raise an exception, not terminate qemu. */
1691372122e3SRichard Henderson     if (dc->base.pc_next & 3) {
1692372122e3SRichard Henderson         cpu_abort(cs, "Microblaze: unaligned PC=%x\n",
1693372122e3SRichard Henderson                   (uint32_t)dc->base.pc_next);
1694fcf5ef2aSThomas Huth     }
1695fcf5ef2aSThomas Huth 
1696fcf5ef2aSThomas Huth     dc->clear_imm = 1;
169744d1432bSRichard Henderson     ir = cpu_ldl_code(env, dc->base.pc_next);
169844d1432bSRichard Henderson     if (!decode(dc, ir)) {
169944d1432bSRichard Henderson         old_decode(dc, ir);
170044d1432bSRichard Henderson     }
170120800179SRichard Henderson 
170220800179SRichard Henderson     if (dc->r0) {
170320800179SRichard Henderson         tcg_temp_free_i32(dc->r0);
170420800179SRichard Henderson         dc->r0 = NULL;
170520800179SRichard Henderson         dc->r0_set = false;
170620800179SRichard Henderson     }
170720800179SRichard Henderson 
1708d7ecb757SRichard Henderson     if (dc->clear_imm && (dc->tb_flags & IMM_FLAG)) {
1709fcf5ef2aSThomas Huth         dc->tb_flags &= ~IMM_FLAG;
1710d7ecb757SRichard Henderson         tcg_gen_discard_i32(cpu_imm);
1711372122e3SRichard Henderson     }
1712d4705ae0SRichard Henderson     dc->base.pc_next += 4;
1713fcf5ef2aSThomas Huth 
1714372122e3SRichard Henderson     if (dc->delayed_branch && --dc->delayed_branch == 0) {
1715372122e3SRichard Henderson         if (dc->tb_flags & DRTI_FLAG) {
1716fcf5ef2aSThomas Huth             do_rti(dc);
1717372122e3SRichard Henderson         }
1718372122e3SRichard Henderson         if (dc->tb_flags & DRTB_FLAG) {
1719fcf5ef2aSThomas Huth             do_rtb(dc);
1720372122e3SRichard Henderson         }
1721372122e3SRichard Henderson         if (dc->tb_flags & DRTE_FLAG) {
1722fcf5ef2aSThomas Huth             do_rte(dc);
1723372122e3SRichard Henderson         }
1724fcf5ef2aSThomas Huth         /* Clear the delay slot flag.  */
1725fcf5ef2aSThomas Huth         dc->tb_flags &= ~D_FLAG;
1726372122e3SRichard Henderson         dc->base.is_jmp = DISAS_JUMP;
1727372122e3SRichard Henderson     }
1728372122e3SRichard Henderson 
1729372122e3SRichard Henderson     /* Force an exit if the per-tb cpu state has changed.  */
1730372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NEXT && dc->cpustate_changed) {
1731372122e3SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
1732372122e3SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1733372122e3SRichard Henderson     }
1734372122e3SRichard Henderson }
1735372122e3SRichard Henderson 
1736372122e3SRichard Henderson static void mb_tr_tb_stop(DisasContextBase *dcb, CPUState *cs)
1737372122e3SRichard Henderson {
1738372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1739372122e3SRichard Henderson 
1740372122e3SRichard Henderson     assert(!dc->abort_at_next_insn);
1741372122e3SRichard Henderson 
1742372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NORETURN) {
1743372122e3SRichard Henderson         /* We have already exited the TB. */
1744372122e3SRichard Henderson         return;
1745372122e3SRichard Henderson     }
1746372122e3SRichard Henderson 
1747372122e3SRichard Henderson     t_sync_flags(dc);
1748372122e3SRichard Henderson     if (dc->tb_flags & D_FLAG) {
1749372122e3SRichard Henderson         sync_jmpstate(dc);
1750372122e3SRichard Henderson         dc->jmp = JMP_NOJMP;
1751372122e3SRichard Henderson     }
1752372122e3SRichard Henderson 
1753372122e3SRichard Henderson     switch (dc->base.is_jmp) {
1754372122e3SRichard Henderson     case DISAS_TOO_MANY:
1755372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1756372122e3SRichard Henderson         gen_goto_tb(dc, 0, dc->base.pc_next);
1757372122e3SRichard Henderson         return;
1758372122e3SRichard Henderson 
1759372122e3SRichard Henderson     case DISAS_UPDATE:
1760372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1761372122e3SRichard Henderson         if (unlikely(cs->singlestep_enabled)) {
1762372122e3SRichard Henderson             gen_raise_exception(dc, EXCP_DEBUG);
1763372122e3SRichard Henderson         } else {
1764372122e3SRichard Henderson             tcg_gen_exit_tb(NULL, 0);
1765372122e3SRichard Henderson         }
1766372122e3SRichard Henderson         return;
1767372122e3SRichard Henderson 
1768372122e3SRichard Henderson     case DISAS_JUMP:
1769372122e3SRichard Henderson         switch (dc->jmp) {
1770372122e3SRichard Henderson         case JMP_INDIRECT:
1771372122e3SRichard Henderson             {
1772d4705ae0SRichard Henderson                 TCGv_i32 tmp_pc = tcg_const_i32(dc->base.pc_next);
17730f96e96bSRichard Henderson                 eval_cond_jmp(dc, cpu_btarget, tmp_pc);
17740f96e96bSRichard Henderson                 tcg_temp_free_i32(tmp_pc);
1775372122e3SRichard Henderson 
1776372122e3SRichard Henderson                 if (unlikely(cs->singlestep_enabled)) {
1777372122e3SRichard Henderson                     gen_raise_exception(dc, EXCP_DEBUG);
1778372122e3SRichard Henderson                 } else {
1779372122e3SRichard Henderson                     tcg_gen_exit_tb(NULL, 0);
1780372122e3SRichard Henderson                 }
1781372122e3SRichard Henderson             }
1782372122e3SRichard Henderson             return;
1783372122e3SRichard Henderson 
1784372122e3SRichard Henderson         case JMP_DIRECT_CC:
1785372122e3SRichard Henderson             {
1786fcf5ef2aSThomas Huth                 TCGLabel *l1 = gen_new_label();
17879b158558SRichard Henderson                 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1788d4705ae0SRichard Henderson                 gen_goto_tb(dc, 1, dc->base.pc_next);
1789fcf5ef2aSThomas Huth                 gen_set_label(l1);
1790372122e3SRichard Henderson             }
1791372122e3SRichard Henderson             /* fall through */
1792372122e3SRichard Henderson 
1793372122e3SRichard Henderson         case JMP_DIRECT:
1794fcf5ef2aSThomas Huth             gen_goto_tb(dc, 0, dc->jmp_pc);
1795372122e3SRichard Henderson             return;
1796fcf5ef2aSThomas Huth         }
1797372122e3SRichard Henderson         /* fall through */
1798fcf5ef2aSThomas Huth 
1799a2b80dbdSRichard Henderson     default:
1800a2b80dbdSRichard Henderson         g_assert_not_reached();
1801fcf5ef2aSThomas Huth     }
1802fcf5ef2aSThomas Huth }
1803fcf5ef2aSThomas Huth 
1804372122e3SRichard Henderson static void mb_tr_disas_log(const DisasContextBase *dcb, CPUState *cs)
1805372122e3SRichard Henderson {
1806372122e3SRichard Henderson     qemu_log("IN: %s\n", lookup_symbol(dcb->pc_first));
1807372122e3SRichard Henderson     log_target_disas(cs, dcb->pc_first, dcb->tb->size);
1808fcf5ef2aSThomas Huth }
1809372122e3SRichard Henderson 
1810372122e3SRichard Henderson static const TranslatorOps mb_tr_ops = {
1811372122e3SRichard Henderson     .init_disas_context = mb_tr_init_disas_context,
1812372122e3SRichard Henderson     .tb_start           = mb_tr_tb_start,
1813372122e3SRichard Henderson     .insn_start         = mb_tr_insn_start,
1814372122e3SRichard Henderson     .breakpoint_check   = mb_tr_breakpoint_check,
1815372122e3SRichard Henderson     .translate_insn     = mb_tr_translate_insn,
1816372122e3SRichard Henderson     .tb_stop            = mb_tr_tb_stop,
1817372122e3SRichard Henderson     .disas_log          = mb_tr_disas_log,
1818372122e3SRichard Henderson };
1819372122e3SRichard Henderson 
1820372122e3SRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
1821372122e3SRichard Henderson {
1822372122e3SRichard Henderson     DisasContext dc;
1823372122e3SRichard Henderson     translator_loop(&mb_tr_ops, &dc.base, cpu, tb, max_insns);
1824fcf5ef2aSThomas Huth }
1825fcf5ef2aSThomas Huth 
182690c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1827fcf5ef2aSThomas Huth {
1828fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1829fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1830fcf5ef2aSThomas Huth     int i;
1831fcf5ef2aSThomas Huth 
183290c84c56SMarkus Armbruster     if (!env) {
1833fcf5ef2aSThomas Huth         return;
183490c84c56SMarkus Armbruster     }
1835fcf5ef2aSThomas Huth 
18360f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
183776e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
18386efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1839eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
184078e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1841eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
18420f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1843fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
18442e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
18452e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
18462e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
18472e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
18482ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
18492ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
18502ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
18512ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
18522ead1b18SJoe Komlodi         }
18532ead1b18SJoe Komlodi     }
1854fcf5ef2aSThomas Huth 
18552ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
185639db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1857af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
18582ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1859fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
186090c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1861fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
186290c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1863fcf5ef2aSThomas Huth         }
186490c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1865fcf5ef2aSThomas Huth }
1866fcf5ef2aSThomas Huth 
1867fcf5ef2aSThomas Huth void mb_tcg_init(void)
1868fcf5ef2aSThomas Huth {
1869480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1870480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1871fcf5ef2aSThomas Huth 
1872480d29a8SRichard Henderson     static const struct {
1873480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1874480d29a8SRichard Henderson     } i32s[] = {
1875480d29a8SRichard Henderson         R(0),  R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1876480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1877480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1878480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1879480d29a8SRichard Henderson 
1880480d29a8SRichard Henderson         SP(pc),
1881480d29a8SRichard Henderson         SP(msr),
18821074c0fbSRichard Henderson         SP(msr_c),
1883480d29a8SRichard Henderson         SP(imm),
1884480d29a8SRichard Henderson         SP(iflags),
1885480d29a8SRichard Henderson         SP(btaken),
1886480d29a8SRichard Henderson         SP(btarget),
1887480d29a8SRichard Henderson         SP(res_val),
1888480d29a8SRichard Henderson     };
1889480d29a8SRichard Henderson 
1890480d29a8SRichard Henderson #undef R
1891480d29a8SRichard Henderson #undef SP
1892480d29a8SRichard Henderson 
1893480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1894480d29a8SRichard Henderson         *i32s[i].var =
1895480d29a8SRichard Henderson           tcg_global_mem_new_i32(cpu_env, i32s[i].ofs, i32s[i].name);
1896fcf5ef2aSThomas Huth     }
189776e8187dSRichard Henderson 
1898480d29a8SRichard Henderson     cpu_res_addr =
1899480d29a8SRichard Henderson         tcg_global_mem_new(cpu_env, offsetof(CPUMBState, res_addr), "res_addr");
1900fcf5ef2aSThomas Huth }
1901fcf5ef2aSThomas Huth 
1902fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1903fcf5ef2aSThomas Huth                           target_ulong *data)
1904fcf5ef2aSThomas Huth {
190576e8187dSRichard Henderson     env->pc = data[0];
1906fcf5ef2aSThomas Huth }
1907