xref: /openbmc/qemu/target/microblaze/translate.c (revision 607f576762948d1b0d47d74a42e3269beb4adf23)
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 
282*607f5767SRichard Henderson #define DO_TYPEA_CFG(NAME, CFG, SE, FN) \
283*607f5767SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea *a) \
284*607f5767SRichard Henderson     { return dc->cpu->cfg.CFG && do_typea(dc, a, SE, FN); }
285*607f5767SRichard Henderson 
28620800179SRichard Henderson #define DO_TYPEBI(NAME, SE, FNI) \
28720800179SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typeb *a) \
28820800179SRichard Henderson     { return do_typeb_imm(dc, a, SE, FNI); }
28920800179SRichard Henderson 
29020800179SRichard Henderson #define DO_TYPEBV(NAME, SE, FN) \
29120800179SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typeb *a) \
29220800179SRichard Henderson     { return do_typeb_val(dc, a, SE, FN); }
29320800179SRichard Henderson 
29420800179SRichard Henderson /* No input carry, but output carry. */
29520800179SRichard Henderson static void gen_add(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
29620800179SRichard Henderson {
29720800179SRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
29820800179SRichard Henderson 
29920800179SRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, ina, zero, inb, zero);
30020800179SRichard Henderson 
30120800179SRichard Henderson     tcg_temp_free_i32(zero);
30220800179SRichard Henderson }
30320800179SRichard Henderson 
30420800179SRichard Henderson /* Input and output carry. */
30520800179SRichard Henderson static void gen_addc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
30620800179SRichard Henderson {
30720800179SRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
30820800179SRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
30920800179SRichard Henderson 
31020800179SRichard Henderson     tcg_gen_add2_i32(tmp, cpu_msr_c, ina, zero, cpu_msr_c, zero);
31120800179SRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, tmp, cpu_msr_c, inb, zero);
31220800179SRichard Henderson 
31320800179SRichard Henderson     tcg_temp_free_i32(tmp);
31420800179SRichard Henderson     tcg_temp_free_i32(zero);
31520800179SRichard Henderson }
31620800179SRichard Henderson 
31720800179SRichard Henderson /* Input carry, but no output carry. */
31820800179SRichard Henderson static void gen_addkc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
31920800179SRichard Henderson {
32020800179SRichard Henderson     tcg_gen_add_i32(out, ina, inb);
32120800179SRichard Henderson     tcg_gen_add_i32(out, out, cpu_msr_c);
32220800179SRichard Henderson }
32320800179SRichard Henderson 
32420800179SRichard Henderson DO_TYPEA(add, true, gen_add)
32520800179SRichard Henderson DO_TYPEA(addc, true, gen_addc)
32620800179SRichard Henderson DO_TYPEA(addk, false, tcg_gen_add_i32)
32720800179SRichard Henderson DO_TYPEA(addkc, true, gen_addkc)
32820800179SRichard Henderson 
32920800179SRichard Henderson DO_TYPEBV(addi, true, gen_add)
33020800179SRichard Henderson DO_TYPEBV(addic, true, gen_addc)
33120800179SRichard Henderson DO_TYPEBI(addik, false, tcg_gen_addi_i32)
33220800179SRichard Henderson DO_TYPEBV(addikc, true, gen_addkc)
33320800179SRichard Henderson 
33458b48b63SRichard Henderson static void gen_cmp(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
33558b48b63SRichard Henderson {
33658b48b63SRichard Henderson     TCGv_i32 lt = tcg_temp_new_i32();
33758b48b63SRichard Henderson 
33858b48b63SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_LT, lt, inb, ina);
33958b48b63SRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
34058b48b63SRichard Henderson     tcg_gen_deposit_i32(out, out, lt, 31, 1);
34158b48b63SRichard Henderson     tcg_temp_free_i32(lt);
34258b48b63SRichard Henderson }
34358b48b63SRichard Henderson 
34458b48b63SRichard Henderson static void gen_cmpu(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
34558b48b63SRichard Henderson {
34658b48b63SRichard Henderson     TCGv_i32 lt = tcg_temp_new_i32();
34758b48b63SRichard Henderson 
34858b48b63SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_LTU, lt, inb, ina);
34958b48b63SRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
35058b48b63SRichard Henderson     tcg_gen_deposit_i32(out, out, lt, 31, 1);
35158b48b63SRichard Henderson     tcg_temp_free_i32(lt);
35258b48b63SRichard Henderson }
35358b48b63SRichard Henderson 
35458b48b63SRichard Henderson DO_TYPEA(cmp, false, gen_cmp)
35558b48b63SRichard Henderson DO_TYPEA(cmpu, false, gen_cmpu)
356a2b0b90eSRichard Henderson 
357*607f5767SRichard Henderson static void gen_pcmpeq(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
358*607f5767SRichard Henderson {
359*607f5767SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_EQ, out, ina, inb);
360*607f5767SRichard Henderson }
361*607f5767SRichard Henderson 
362*607f5767SRichard Henderson static void gen_pcmpne(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
363*607f5767SRichard Henderson {
364*607f5767SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_NE, out, ina, inb);
365*607f5767SRichard Henderson }
366*607f5767SRichard Henderson 
367*607f5767SRichard Henderson DO_TYPEA_CFG(pcmpbf, use_pcmp_instr, false, gen_helper_pcmpbf)
368*607f5767SRichard Henderson DO_TYPEA_CFG(pcmpeq, use_pcmp_instr, false, gen_pcmpeq)
369*607f5767SRichard Henderson DO_TYPEA_CFG(pcmpne, use_pcmp_instr, false, gen_pcmpne)
370*607f5767SRichard Henderson 
371a2b0b90eSRichard Henderson /* No input carry, but output carry. */
372a2b0b90eSRichard Henderson static void gen_rsub(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
373a2b0b90eSRichard Henderson {
374a2b0b90eSRichard Henderson     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_msr_c, inb, ina);
375a2b0b90eSRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
376a2b0b90eSRichard Henderson }
377a2b0b90eSRichard Henderson 
378a2b0b90eSRichard Henderson /* Input and output carry. */
379a2b0b90eSRichard Henderson static void gen_rsubc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
380a2b0b90eSRichard Henderson {
381a2b0b90eSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
382a2b0b90eSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
383a2b0b90eSRichard Henderson 
384a2b0b90eSRichard Henderson     tcg_gen_not_i32(tmp, ina);
385a2b0b90eSRichard Henderson     tcg_gen_add2_i32(tmp, cpu_msr_c, tmp, zero, cpu_msr_c, zero);
386a2b0b90eSRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, tmp, cpu_msr_c, inb, zero);
387a2b0b90eSRichard Henderson 
388a2b0b90eSRichard Henderson     tcg_temp_free_i32(zero);
389a2b0b90eSRichard Henderson     tcg_temp_free_i32(tmp);
390a2b0b90eSRichard Henderson }
391a2b0b90eSRichard Henderson 
392a2b0b90eSRichard Henderson /* No input or output carry. */
393a2b0b90eSRichard Henderson static void gen_rsubk(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
394a2b0b90eSRichard Henderson {
395a2b0b90eSRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
396a2b0b90eSRichard Henderson }
397a2b0b90eSRichard Henderson 
398a2b0b90eSRichard Henderson /* Input carry, no output carry. */
399a2b0b90eSRichard Henderson static void gen_rsubkc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
400a2b0b90eSRichard Henderson {
401a2b0b90eSRichard Henderson     TCGv_i32 nota = tcg_temp_new_i32();
402a2b0b90eSRichard Henderson 
403a2b0b90eSRichard Henderson     tcg_gen_not_i32(nota, ina);
404a2b0b90eSRichard Henderson     tcg_gen_add_i32(out, inb, nota);
405a2b0b90eSRichard Henderson     tcg_gen_add_i32(out, out, cpu_msr_c);
406a2b0b90eSRichard Henderson 
407a2b0b90eSRichard Henderson     tcg_temp_free_i32(nota);
408a2b0b90eSRichard Henderson }
409a2b0b90eSRichard Henderson 
410a2b0b90eSRichard Henderson DO_TYPEA(rsub, true, gen_rsub)
411a2b0b90eSRichard Henderson DO_TYPEA(rsubc, true, gen_rsubc)
412a2b0b90eSRichard Henderson DO_TYPEA(rsubk, false, gen_rsubk)
413a2b0b90eSRichard Henderson DO_TYPEA(rsubkc, true, gen_rsubkc)
414a2b0b90eSRichard Henderson 
415a2b0b90eSRichard Henderson DO_TYPEBV(rsubi, true, gen_rsub)
416a2b0b90eSRichard Henderson DO_TYPEBV(rsubic, true, gen_rsubc)
417a2b0b90eSRichard Henderson DO_TYPEBV(rsubik, false, gen_rsubk)
418a2b0b90eSRichard Henderson DO_TYPEBV(rsubikc, true, gen_rsubkc)
419a2b0b90eSRichard Henderson 
42020800179SRichard Henderson static bool trans_zero(DisasContext *dc, arg_zero *arg)
42120800179SRichard Henderson {
42220800179SRichard Henderson     /* If opcode_0_illegal, trap.  */
42320800179SRichard Henderson     if (dc->cpu->cfg.opcode_0_illegal) {
42420800179SRichard Henderson         trap_illegal(dc, true);
42520800179SRichard Henderson         return true;
42620800179SRichard Henderson     }
42720800179SRichard Henderson     /*
42820800179SRichard Henderson      * Otherwise, this is "add r0, r0, r0".
42920800179SRichard Henderson      * Continue to trans_add so that MSR[C] gets cleared.
43020800179SRichard Henderson      */
43120800179SRichard Henderson     return false;
432fcf5ef2aSThomas Huth }
433fcf5ef2aSThomas Huth 
434fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
435fcf5ef2aSThomas Huth {
436fcf5ef2aSThomas Huth     unsigned int not;
437fcf5ef2aSThomas Huth 
438fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
439fcf5ef2aSThomas Huth 
440fcf5ef2aSThomas Huth     if (!dc->rd)
441fcf5ef2aSThomas Huth         return;
442fcf5ef2aSThomas Huth 
443fcf5ef2aSThomas Huth     if (not) {
444cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
445fcf5ef2aSThomas Huth     } else
446cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
447fcf5ef2aSThomas Huth }
448fcf5ef2aSThomas Huth 
449fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
450fcf5ef2aSThomas Huth {
451fcf5ef2aSThomas Huth     if (dc->rd)
452cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
453fcf5ef2aSThomas Huth }
454fcf5ef2aSThomas Huth 
455fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
456fcf5ef2aSThomas Huth {
457fcf5ef2aSThomas Huth     if (dc->rd)
458cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
459fcf5ef2aSThomas Huth }
460fcf5ef2aSThomas Huth 
4611074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
462fcf5ef2aSThomas Huth {
4631074c0fbSRichard Henderson     TCGv_i32 t;
4641074c0fbSRichard Henderson 
4651074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
4661074c0fbSRichard Henderson     t = tcg_temp_new_i32();
4671074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
4681074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
4691074c0fbSRichard Henderson     tcg_temp_free_i32(t);
470fcf5ef2aSThomas Huth }
471fcf5ef2aSThomas Huth 
4721074c0fbSRichard Henderson static void msr_write(DisasContext *dc, TCGv_i32 v)
473fcf5ef2aSThomas Huth {
474fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
4751074c0fbSRichard Henderson 
4761074c0fbSRichard Henderson     /* Install MSR_C.  */
4771074c0fbSRichard Henderson     tcg_gen_extract_i32(cpu_msr_c, v, 2, 1);
4781074c0fbSRichard Henderson 
4791074c0fbSRichard Henderson     /* Clear MSR_C and MSR_CC; MSR_PVR is not writable, and is always clear. */
4801074c0fbSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~(MSR_C | MSR_CC | MSR_PVR));
481fcf5ef2aSThomas Huth }
482fcf5ef2aSThomas Huth 
483fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
484fcf5ef2aSThomas Huth {
485fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
486cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4872023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
488f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
489fcf5ef2aSThomas Huth 
4902023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
4912023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
4922023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
493fcf5ef2aSThomas Huth     dc->type_b = 1;
4942023e9a3SEdgar E. Iglesias     if (to) {
495fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
496f0f7e7f7SEdgar E. Iglesias     }
497f0f7e7f7SEdgar E. Iglesias 
498f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
499f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
500f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
501f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
502f0f7e7f7SEdgar E. Iglesias 
503f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
5042023e9a3SEdgar E. Iglesias     }
505fcf5ef2aSThomas Huth 
506fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
5072023e9a3SEdgar E. Iglesias     if (clrset) {
5082023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
509fcf5ef2aSThomas Huth 
51056837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
511fcf5ef2aSThomas Huth             /* nop??? */
512fcf5ef2aSThomas Huth             return;
513fcf5ef2aSThomas Huth         }
514fcf5ef2aSThomas Huth 
515bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
516fcf5ef2aSThomas Huth             return;
517fcf5ef2aSThomas Huth         }
518fcf5ef2aSThomas Huth 
519fcf5ef2aSThomas Huth         if (dc->rd)
520fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
521fcf5ef2aSThomas Huth 
522cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
523cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
524fcf5ef2aSThomas Huth         msr_read(dc, t0);
525cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
526fcf5ef2aSThomas Huth 
527fcf5ef2aSThomas Huth         if (clr) {
528cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
529cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
530fcf5ef2aSThomas Huth         } else
531cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
532fcf5ef2aSThomas Huth         msr_write(dc, t0);
533cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
534cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
535d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
536d4705ae0SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
537fcf5ef2aSThomas Huth         return;
538fcf5ef2aSThomas Huth     }
539fcf5ef2aSThomas Huth 
540bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
541fcf5ef2aSThomas Huth         return;
542fcf5ef2aSThomas Huth     }
543fcf5ef2aSThomas Huth 
544fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
545fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
546fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
547f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
54805a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
54905a9a651SEdgar E. Iglesias 
550fcf5ef2aSThomas Huth         sr &= 7;
55105a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
55205a9a651SEdgar E. Iglesias         if (to) {
553f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
55405a9a651SEdgar E. Iglesias         } else {
555f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
55605a9a651SEdgar E. Iglesias         }
55705a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
558f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
559fcf5ef2aSThomas Huth         return;
560fcf5ef2aSThomas Huth     }
561fcf5ef2aSThomas Huth #endif
562fcf5ef2aSThomas Huth 
563fcf5ef2aSThomas Huth     if (to) {
564fcf5ef2aSThomas Huth         switch (sr) {
565aa28e6d4SRichard Henderson             case SR_PC:
566fcf5ef2aSThomas Huth                 break;
567aa28e6d4SRichard Henderson             case SR_MSR:
568fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
569fcf5ef2aSThomas Huth                 break;
570351527b7SEdgar E. Iglesias             case SR_EAR:
571dbdb77c4SRichard Henderson                 {
572dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
573dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
574dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
575dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
576dbdb77c4SRichard Henderson                 }
577aa28e6d4SRichard Henderson                 break;
578351527b7SEdgar E. Iglesias             case SR_ESR:
57941ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
58041ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
581aa28e6d4SRichard Henderson                 break;
582ab6dd380SEdgar E. Iglesias             case SR_FSR:
58386017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
58486017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
585aa28e6d4SRichard Henderson                 break;
586aa28e6d4SRichard Henderson             case SR_BTR:
587ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
588ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
589aa28e6d4SRichard Henderson                 break;
590aa28e6d4SRichard Henderson             case SR_EDR:
59139db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
59239db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
593fcf5ef2aSThomas Huth                 break;
594fcf5ef2aSThomas Huth             case 0x800:
595cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
596cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
597fcf5ef2aSThomas Huth                 break;
598fcf5ef2aSThomas Huth             case 0x802:
599cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
600cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
601fcf5ef2aSThomas Huth                 break;
602fcf5ef2aSThomas Huth             default:
603fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
604fcf5ef2aSThomas Huth                 break;
605fcf5ef2aSThomas Huth         }
606fcf5ef2aSThomas Huth     } else {
607fcf5ef2aSThomas Huth         switch (sr) {
608aa28e6d4SRichard Henderson             case SR_PC:
609d4705ae0SRichard Henderson                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
610fcf5ef2aSThomas Huth                 break;
611aa28e6d4SRichard Henderson             case SR_MSR:
612fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
613fcf5ef2aSThomas Huth                 break;
614351527b7SEdgar E. Iglesias             case SR_EAR:
615dbdb77c4SRichard Henderson                 {
616dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
617dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
618a1b48e3aSEdgar E. Iglesias                     if (extended) {
619dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
620aa28e6d4SRichard Henderson                     } else {
621dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
622dbdb77c4SRichard Henderson                     }
623dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
624a1b48e3aSEdgar E. Iglesias                 }
625aa28e6d4SRichard Henderson                 break;
626351527b7SEdgar E. Iglesias             case SR_ESR:
62741ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
62841ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
629aa28e6d4SRichard Henderson                 break;
630351527b7SEdgar E. Iglesias             case SR_FSR:
63186017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
63286017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
633aa28e6d4SRichard Henderson                 break;
634351527b7SEdgar E. Iglesias             case SR_BTR:
635ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
636ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
637aa28e6d4SRichard Henderson                 break;
6387cdae31dSTong Ho             case SR_EDR:
63939db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
64039db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
641fcf5ef2aSThomas Huth                 break;
642fcf5ef2aSThomas Huth             case 0x800:
643cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
644cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
645fcf5ef2aSThomas Huth                 break;
646fcf5ef2aSThomas Huth             case 0x802:
647cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
648cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
649fcf5ef2aSThomas Huth                 break;
650351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
651fcf5ef2aSThomas Huth                 rn = sr & 0xf;
652cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
653fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
654fcf5ef2aSThomas Huth                 break;
655fcf5ef2aSThomas Huth             default:
656fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
657fcf5ef2aSThomas Huth                 break;
658fcf5ef2aSThomas Huth         }
659fcf5ef2aSThomas Huth     }
660fcf5ef2aSThomas Huth 
661fcf5ef2aSThomas Huth     if (dc->rd == 0) {
662cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
663fcf5ef2aSThomas Huth     }
664fcf5ef2aSThomas Huth }
665fcf5ef2aSThomas Huth 
666fcf5ef2aSThomas Huth /* Multiplier unit.  */
667fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
668fcf5ef2aSThomas Huth {
669cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
670fcf5ef2aSThomas Huth     unsigned int subcode;
671fcf5ef2aSThomas Huth 
6729ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
673fcf5ef2aSThomas Huth         return;
674fcf5ef2aSThomas Huth     }
675fcf5ef2aSThomas Huth 
676fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
677fcf5ef2aSThomas Huth 
678fcf5ef2aSThomas Huth     if (dc->type_b) {
679cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
680fcf5ef2aSThomas Huth         return;
681fcf5ef2aSThomas Huth     }
682fcf5ef2aSThomas Huth 
683fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6849b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
685fcf5ef2aSThomas Huth         /* nop??? */
686fcf5ef2aSThomas Huth     }
687fcf5ef2aSThomas Huth 
688cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
689fcf5ef2aSThomas Huth     switch (subcode) {
690fcf5ef2aSThomas Huth         case 0:
691cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
692fcf5ef2aSThomas Huth             break;
693fcf5ef2aSThomas Huth         case 1:
694cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
695cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
696fcf5ef2aSThomas Huth             break;
697fcf5ef2aSThomas Huth         case 2:
698cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
699cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
700fcf5ef2aSThomas Huth             break;
701fcf5ef2aSThomas Huth         case 3:
702cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
703fcf5ef2aSThomas Huth             break;
704fcf5ef2aSThomas Huth         default:
705fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
706fcf5ef2aSThomas Huth             break;
707fcf5ef2aSThomas Huth     }
708cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
709fcf5ef2aSThomas Huth }
710fcf5ef2aSThomas Huth 
711fcf5ef2aSThomas Huth /* Div unit.  */
712fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
713fcf5ef2aSThomas Huth {
714fcf5ef2aSThomas Huth     unsigned int u;
715fcf5ef2aSThomas Huth 
716fcf5ef2aSThomas Huth     u = dc->imm & 2;
717fcf5ef2aSThomas Huth 
7189ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
7199ba8cd45SEdgar E. Iglesias         return;
720fcf5ef2aSThomas Huth     }
721fcf5ef2aSThomas Huth 
722fcf5ef2aSThomas Huth     if (u)
723fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
724fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
725fcf5ef2aSThomas Huth     else
726fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
727fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
728fcf5ef2aSThomas Huth     if (!dc->rd)
729cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
730fcf5ef2aSThomas Huth }
731fcf5ef2aSThomas Huth 
732fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
733fcf5ef2aSThomas Huth {
734cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
735faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
736d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
737fcf5ef2aSThomas Huth 
7389ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
739fcf5ef2aSThomas Huth         return;
740fcf5ef2aSThomas Huth     }
741fcf5ef2aSThomas Huth 
742faa48d74SEdgar E. Iglesias     if (dc->type_b) {
743faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
744d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
745faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
746faa48d74SEdgar E. Iglesias     }
747e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
748e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
749faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
750faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
751fcf5ef2aSThomas Huth 
752faa48d74SEdgar E. Iglesias     if (e) {
753faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
754faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
755faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
756faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
757faa48d74SEdgar E. Iglesias         } else {
758faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
759faa48d74SEdgar E. Iglesias         }
760d09b2585SEdgar E. Iglesias     } else if (i) {
761d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
762d09b2585SEdgar E. Iglesias 
763d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
764d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
765d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
766d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
767d09b2585SEdgar E. Iglesias         } else {
768d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
769d09b2585SEdgar E. Iglesias                                 imm_s, width);
770d09b2585SEdgar E. Iglesias         }
771faa48d74SEdgar E. Iglesias     } else {
772cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
773fcf5ef2aSThomas Huth 
774cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
775cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
776fcf5ef2aSThomas Huth 
7772acf6d53SEdgar E. Iglesias         if (s) {
778cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7792acf6d53SEdgar E. Iglesias         } else {
7802acf6d53SEdgar E. Iglesias             if (t) {
781cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7822acf6d53SEdgar E. Iglesias             } else {
783cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
784fcf5ef2aSThomas Huth             }
785fcf5ef2aSThomas Huth         }
786cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7872acf6d53SEdgar E. Iglesias     }
788faa48d74SEdgar E. Iglesias }
789fcf5ef2aSThomas Huth 
790fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
791fcf5ef2aSThomas Huth {
792fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
793cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
794fcf5ef2aSThomas Huth     unsigned int op;
795fcf5ef2aSThomas Huth 
796fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
797fcf5ef2aSThomas Huth     switch (op) {
798fcf5ef2aSThomas Huth         case 0x21:
799fcf5ef2aSThomas Huth             /* src.  */
800cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
801fcf5ef2aSThomas Huth 
8021074c0fbSRichard Henderson             tcg_gen_shli_i32(t0, cpu_msr_c, 31);
8031074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
804fcf5ef2aSThomas Huth             if (dc->rd) {
805cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
806cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
807fcf5ef2aSThomas Huth             }
808cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
809fcf5ef2aSThomas Huth             break;
810fcf5ef2aSThomas Huth 
811fcf5ef2aSThomas Huth         case 0x1:
812fcf5ef2aSThomas Huth         case 0x41:
813fcf5ef2aSThomas Huth             /* srl.  */
8141074c0fbSRichard Henderson             tcg_gen_andi_i32(cpu_msr_c, cpu_R[dc->ra], 1);
815fcf5ef2aSThomas Huth             if (dc->rd) {
816fcf5ef2aSThomas Huth                 if (op == 0x41)
817cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
818fcf5ef2aSThomas Huth                 else
819cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
820fcf5ef2aSThomas Huth             }
821fcf5ef2aSThomas Huth             break;
822fcf5ef2aSThomas Huth         case 0x60:
823fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
824fcf5ef2aSThomas Huth             break;
825fcf5ef2aSThomas Huth         case 0x61:
826fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
827fcf5ef2aSThomas Huth             break;
828fcf5ef2aSThomas Huth         case 0x64:
829fcf5ef2aSThomas Huth         case 0x66:
830fcf5ef2aSThomas Huth         case 0x74:
831fcf5ef2aSThomas Huth         case 0x76:
832fcf5ef2aSThomas Huth             /* wdc.  */
833bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
834fcf5ef2aSThomas Huth             break;
835fcf5ef2aSThomas Huth         case 0x68:
836fcf5ef2aSThomas Huth             /* wic.  */
837bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
838fcf5ef2aSThomas Huth             break;
839fcf5ef2aSThomas Huth         case 0xe0:
8409ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
8419ba8cd45SEdgar E. Iglesias                 return;
842fcf5ef2aSThomas Huth             }
8438fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
8445318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
845fcf5ef2aSThomas Huth             }
846fcf5ef2aSThomas Huth             break;
847fcf5ef2aSThomas Huth         case 0x1e0:
848fcf5ef2aSThomas Huth             /* swapb */
849fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
850fcf5ef2aSThomas Huth             break;
851fcf5ef2aSThomas Huth         case 0x1e2:
852fcf5ef2aSThomas Huth             /*swaph */
853fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
854fcf5ef2aSThomas Huth             break;
855fcf5ef2aSThomas Huth         default:
856fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
857d4705ae0SRichard Henderson                       (uint32_t)dc->base.pc_next, op, dc->rd, dc->ra, dc->rb);
858fcf5ef2aSThomas Huth             break;
859fcf5ef2aSThomas Huth     }
860fcf5ef2aSThomas Huth }
861fcf5ef2aSThomas Huth 
862fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
863fcf5ef2aSThomas Huth {
864fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
865fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
8669b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
867fcf5ef2aSThomas Huth         }
868fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
8690f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
870fcf5ef2aSThomas Huth     }
871fcf5ef2aSThomas Huth }
872fcf5ef2aSThomas Huth 
873fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
874fcf5ef2aSThomas Huth {
875d7ecb757SRichard Henderson     dc->ext_imm = dc->imm << 16;
876d7ecb757SRichard Henderson     tcg_gen_movi_i32(cpu_imm, dc->ext_imm);
877fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
878fcf5ef2aSThomas Huth     dc->clear_imm = 0;
879fcf5ef2aSThomas Huth }
880fcf5ef2aSThomas Huth 
881d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
882fcf5ef2aSThomas Huth {
8830e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8840e9033c8SEdgar E. Iglesias     bool stackprot = false;
885403322eaSEdgar E. Iglesias     TCGv_i32 t32;
886fcf5ef2aSThomas Huth 
887fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
888fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8890e9033c8SEdgar E. Iglesias         stackprot = true;
890fcf5ef2aSThomas Huth     }
891fcf5ef2aSThomas Huth 
892fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
893fcf5ef2aSThomas Huth     if (!dc->type_b) {
894d248e1beSEdgar E. Iglesias         if (ea) {
895d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
896d248e1beSEdgar E. Iglesias 
897d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
898d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
899d248e1beSEdgar E. Iglesias                 return;
900d248e1beSEdgar E. Iglesias             }
901d248e1beSEdgar E. Iglesias 
902d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
903d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
904d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
905d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
906d248e1beSEdgar E. Iglesias             }
907d248e1beSEdgar E. Iglesias             return;
908d248e1beSEdgar E. Iglesias         }
909d248e1beSEdgar E. Iglesias 
9100dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
911fcf5ef2aSThomas Huth         if (dc->ra == 0) {
912403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
9130dc4af5cSEdgar E. Iglesias             return;
914fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
915403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
9160dc4af5cSEdgar E. Iglesias             return;
917fcf5ef2aSThomas Huth         }
918fcf5ef2aSThomas Huth 
919fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
9200e9033c8SEdgar E. Iglesias             stackprot = true;
921fcf5ef2aSThomas Huth         }
922fcf5ef2aSThomas Huth 
923403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
924403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
925403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
926403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
927fcf5ef2aSThomas Huth 
928fcf5ef2aSThomas Huth         if (stackprot) {
9290a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
930fcf5ef2aSThomas Huth         }
9310dc4af5cSEdgar E. Iglesias         return;
932fcf5ef2aSThomas Huth     }
933fcf5ef2aSThomas Huth     /* Immediate.  */
934403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
935d7ecb757SRichard Henderson     tcg_gen_addi_i32(t32, cpu_R[dc->ra], dec_alu_typeb_imm(dc));
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 
945fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
946fcf5ef2aSThomas Huth {
947403322eaSEdgar E. Iglesias     TCGv_i32 v;
948403322eaSEdgar E. Iglesias     TCGv addr;
9498534063aSEdgar E. Iglesias     unsigned int size;
950d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
951d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
95214776ab5STony Nguyen     MemOp mop;
953fcf5ef2aSThomas Huth 
954fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
955fcf5ef2aSThomas Huth     size = 1 << mop;
956fcf5ef2aSThomas Huth     if (!dc->type_b) {
957d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9588534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9598534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
960fcf5ef2aSThomas Huth     }
961fcf5ef2aSThomas Huth     mop |= MO_TE;
962fcf5ef2aSThomas Huth     if (rev) {
963fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
964fcf5ef2aSThomas Huth     }
965fcf5ef2aSThomas Huth 
9669ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
967fcf5ef2aSThomas Huth         return;
968fcf5ef2aSThomas Huth     }
969fcf5ef2aSThomas Huth 
970d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
971d248e1beSEdgar E. Iglesias         return;
972d248e1beSEdgar E. Iglesias     }
973d248e1beSEdgar E. Iglesias 
974fcf5ef2aSThomas Huth     t_sync_flags(dc);
975403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
976d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
977d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
978d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
979fcf5ef2aSThomas Huth 
980fcf5ef2aSThomas Huth     /*
981fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
982fcf5ef2aSThomas Huth      *
983fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
984fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
985fcf5ef2aSThomas Huth      */
986fcf5ef2aSThomas Huth     if (rev && size != 4) {
987fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
988fcf5ef2aSThomas Huth         switch (size) {
989fcf5ef2aSThomas Huth             case 1:
990fcf5ef2aSThomas Huth             {
991a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
992fcf5ef2aSThomas Huth                 break;
993fcf5ef2aSThomas Huth             }
994fcf5ef2aSThomas Huth 
995fcf5ef2aSThomas Huth             case 2:
996fcf5ef2aSThomas Huth                 /* 00 -> 10
997fcf5ef2aSThomas Huth                    10 -> 00.  */
998403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
999fcf5ef2aSThomas Huth                 break;
1000fcf5ef2aSThomas Huth             default:
1001fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1002fcf5ef2aSThomas Huth                 break;
1003fcf5ef2aSThomas Huth         }
1004fcf5ef2aSThomas Huth     }
1005fcf5ef2aSThomas Huth 
1006fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
1007fcf5ef2aSThomas Huth     if (ex) {
1008403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1009fcf5ef2aSThomas Huth     }
1010fcf5ef2aSThomas Huth 
1011fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1012fcf5ef2aSThomas Huth     sync_jmpstate(dc);
1013fcf5ef2aSThomas Huth 
1014fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
1015fcf5ef2aSThomas Huth     /*
1016fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
1017fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
1018fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
1019fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
1020fcf5ef2aSThomas Huth      */
1021cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
1022d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
1023fcf5ef2aSThomas Huth 
10241507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1025a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
1026a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1027a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1028a6338015SEdgar E. Iglesias 
1029d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1030a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
1031a6338015SEdgar E. Iglesias 
1032a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
1033a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1034a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1035fcf5ef2aSThomas Huth     }
1036fcf5ef2aSThomas Huth 
1037fcf5ef2aSThomas Huth     if (ex) {
10389b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
10399b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
1040fcf5ef2aSThomas Huth     }
1041fcf5ef2aSThomas Huth     if (dc->rd) {
1042cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1043fcf5ef2aSThomas Huth     }
1044cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1045fcf5ef2aSThomas Huth 
1046fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1047fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
10481074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1049fcf5ef2aSThomas Huth     }
1050fcf5ef2aSThomas Huth 
1051403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1052fcf5ef2aSThomas Huth }
1053fcf5ef2aSThomas Huth 
1054fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1055fcf5ef2aSThomas Huth {
1056403322eaSEdgar E. Iglesias     TCGv addr;
1057fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1058b51b3d43SEdgar E. Iglesias     unsigned int size;
1059d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1060d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
106114776ab5STony Nguyen     MemOp mop;
1062fcf5ef2aSThomas Huth 
1063fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1064fcf5ef2aSThomas Huth     size = 1 << mop;
1065fcf5ef2aSThomas Huth     if (!dc->type_b) {
1066d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1067b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1068b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1069fcf5ef2aSThomas Huth     }
1070fcf5ef2aSThomas Huth     mop |= MO_TE;
1071fcf5ef2aSThomas Huth     if (rev) {
1072fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1073fcf5ef2aSThomas Huth     }
1074fcf5ef2aSThomas Huth 
10759ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1076fcf5ef2aSThomas Huth         return;
1077fcf5ef2aSThomas Huth     }
1078fcf5ef2aSThomas Huth 
1079d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1080d248e1beSEdgar E. Iglesias 
1081fcf5ef2aSThomas Huth     t_sync_flags(dc);
1082fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1083fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10840dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1085403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1086d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1087d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1088d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1089fcf5ef2aSThomas Huth 
1090fcf5ef2aSThomas Huth     if (ex) { /* swx */
1091cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1092fcf5ef2aSThomas Huth 
1093fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1094403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1095fcf5ef2aSThomas Huth 
10961074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 1);
1097fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
10989b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1099fcf5ef2aSThomas Huth 
1100071cdc67SEdgar E. Iglesias         /*
1101071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1102071cdc67SEdgar E. Iglesias          * the reserved location.
1103071cdc67SEdgar E. Iglesias          */
1104cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1105071cdc67SEdgar E. Iglesias 
11069b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1107071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1108071cdc67SEdgar E. Iglesias                                    mop);
1109071cdc67SEdgar E. Iglesias 
11109b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
11111074c0fbSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, 0);
1112cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1113fcf5ef2aSThomas Huth     }
1114fcf5ef2aSThomas Huth 
1115fcf5ef2aSThomas Huth     if (rev && size != 4) {
1116fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1117fcf5ef2aSThomas Huth         switch (size) {
1118fcf5ef2aSThomas Huth             case 1:
1119fcf5ef2aSThomas Huth             {
1120a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1121fcf5ef2aSThomas Huth                 break;
1122fcf5ef2aSThomas Huth             }
1123fcf5ef2aSThomas Huth 
1124fcf5ef2aSThomas Huth             case 2:
1125fcf5ef2aSThomas Huth                 /* 00 -> 10
1126fcf5ef2aSThomas Huth                    10 -> 00.  */
1127fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1128403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1129fcf5ef2aSThomas Huth                 break;
1130fcf5ef2aSThomas Huth             default:
1131fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1132fcf5ef2aSThomas Huth                 break;
1133fcf5ef2aSThomas Huth         }
1134fcf5ef2aSThomas Huth     }
1135071cdc67SEdgar E. Iglesias 
1136071cdc67SEdgar E. Iglesias     if (!ex) {
1137d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1138071cdc67SEdgar E. Iglesias     }
1139fcf5ef2aSThomas Huth 
1140fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11411507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1142a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1143a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1144a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1145a6338015SEdgar E. Iglesias 
1146d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1147fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1148fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1149fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1150fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1151fcf5ef2aSThomas Huth          *        access.
1152fcf5ef2aSThomas Huth          */
1153a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1154a6338015SEdgar E. Iglesias 
1155a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1156a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1157a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1158fcf5ef2aSThomas Huth     }
1159fcf5ef2aSThomas Huth 
1160fcf5ef2aSThomas Huth     if (ex) {
1161fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1162fcf5ef2aSThomas Huth     }
1163fcf5ef2aSThomas Huth 
1164403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1165fcf5ef2aSThomas Huth }
1166fcf5ef2aSThomas Huth 
1167fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11689e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1169fcf5ef2aSThomas Huth {
1170d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1171d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1172d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1173d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1174d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1175d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1176d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1177d89b86e9SEdgar E. Iglesias     };
1178d89b86e9SEdgar E. Iglesias 
1179fcf5ef2aSThomas Huth     switch (cc) {
1180fcf5ef2aSThomas Huth     case CC_EQ:
1181fcf5ef2aSThomas Huth     case CC_NE:
1182fcf5ef2aSThomas Huth     case CC_LT:
1183fcf5ef2aSThomas Huth     case CC_LE:
1184fcf5ef2aSThomas Huth     case CC_GE:
1185fcf5ef2aSThomas Huth     case CC_GT:
11869e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1187fcf5ef2aSThomas Huth         break;
1188fcf5ef2aSThomas Huth     default:
1189fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1190fcf5ef2aSThomas Huth         break;
1191fcf5ef2aSThomas Huth     }
1192fcf5ef2aSThomas Huth }
1193fcf5ef2aSThomas Huth 
11940f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1195fcf5ef2aSThomas Huth {
11960f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1197e956caf2SEdgar E. Iglesias 
11980f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
11999b158558SRichard Henderson                         cpu_btaken, zero,
1200e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1201e956caf2SEdgar E. Iglesias 
12020f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1203fcf5ef2aSThomas Huth }
1204fcf5ef2aSThomas Huth 
1205f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1206f91c60f0SEdgar E. Iglesias {
1207f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1208f91c60f0SEdgar E. Iglesias 
1209f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1210f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1211f91c60f0SEdgar E. Iglesias 
1212f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1213f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1214f91c60f0SEdgar E. Iglesias }
1215f91c60f0SEdgar E. Iglesias 
1216fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1217fcf5ef2aSThomas Huth {
1218fcf5ef2aSThomas Huth     unsigned int cc;
1219fcf5ef2aSThomas Huth     unsigned int dslot;
1220fcf5ef2aSThomas Huth 
1221fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1222fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1223fcf5ef2aSThomas Huth 
1224fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1225fcf5ef2aSThomas Huth     if (dslot) {
1226f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1227fcf5ef2aSThomas Huth     }
1228fcf5ef2aSThomas Huth 
1229d7ecb757SRichard Henderson     if (dc->type_b) {
1230fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1231d7ecb757SRichard Henderson         dc->jmp_pc = dc->base.pc_next + dec_alu_typeb_imm(dc);
1232d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
1233fcf5ef2aSThomas Huth     } else {
1234fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1235d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1236fcf5ef2aSThomas Huth     }
12379b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1238fcf5ef2aSThomas Huth }
1239fcf5ef2aSThomas Huth 
1240fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1241fcf5ef2aSThomas Huth {
1242fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1243fcf5ef2aSThomas Huth 
1244fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1245fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1246fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1247fcf5ef2aSThomas Huth 
1248fcf5ef2aSThomas Huth     /* Memory barrier.  */
1249fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1250fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1251badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1252badcbf9dSEdgar E. Iglesias 
12533f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12543f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12553f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12563f172744SEdgar E. Iglesias         }
12573f172744SEdgar E. Iglesias 
1258fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1259badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
126041ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1261fcf5ef2aSThomas Huth 
1262b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1263b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1264b4919e7dSEdgar E. Iglesias                 return;
1265b4919e7dSEdgar E. Iglesias             }
1266b4919e7dSEdgar E. Iglesias 
1267fcf5ef2aSThomas Huth             t_sync_flags(dc);
126841ba37c4SRichard Henderson 
126941ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1270fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1271fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1272fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1273fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
127441ba37c4SRichard Henderson 
1275d4705ae0SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
127641ba37c4SRichard Henderson 
127741ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1278fcf5ef2aSThomas Huth             return;
1279fcf5ef2aSThomas Huth         }
1280fcf5ef2aSThomas Huth         /* Break the TB.  */
1281fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1282fcf5ef2aSThomas Huth         return;
1283fcf5ef2aSThomas Huth     }
1284fcf5ef2aSThomas Huth 
1285d7ecb757SRichard Henderson     if (abs && link && !dslot) {
1286d7ecb757SRichard Henderson         if (dc->type_b) {
1287d7ecb757SRichard Henderson             /* BRKI */
1288d7ecb757SRichard Henderson             uint32_t imm = dec_alu_typeb_imm(dc);
1289d7ecb757SRichard Henderson             if (trap_userspace(dc, imm != 8 && imm != 0x18)) {
1290d7ecb757SRichard Henderson                 return;
1291d7ecb757SRichard Henderson             }
1292d7ecb757SRichard Henderson         } else {
1293d7ecb757SRichard Henderson             /* BRK */
1294d7ecb757SRichard Henderson             if (trap_userspace(dc, true)) {
1295d7ecb757SRichard Henderson                 return;
1296d7ecb757SRichard Henderson             }
1297d7ecb757SRichard Henderson         }
1298d7ecb757SRichard Henderson     }
1299d7ecb757SRichard Henderson 
1300fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1301fcf5ef2aSThomas Huth     if (dslot) {
1302f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1303fcf5ef2aSThomas Huth     }
1304d7ecb757SRichard Henderson     if (link && dc->rd) {
1305d4705ae0SRichard Henderson         tcg_gen_movi_i32(cpu_R[dc->rd], dc->base.pc_next);
1306d7ecb757SRichard Henderson     }
1307fcf5ef2aSThomas Huth 
1308fcf5ef2aSThomas Huth     if (abs) {
1309d7ecb757SRichard Henderson         if (dc->type_b) {
1310d7ecb757SRichard Henderson             uint32_t dest = dec_alu_typeb_imm(dc);
1311d7ecb757SRichard Henderson 
1312d7ecb757SRichard Henderson             dc->jmp = JMP_DIRECT;
1313d7ecb757SRichard Henderson             dc->jmp_pc = dest;
1314d7ecb757SRichard Henderson             tcg_gen_movi_i32(cpu_btarget, dest);
1315fcf5ef2aSThomas Huth             if (link && !dslot) {
1316d7ecb757SRichard Henderson                 switch (dest) {
1317d7ecb757SRichard Henderson                 case 8:
1318d7ecb757SRichard Henderson                 case 0x18:
1319d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_BREAK);
1320d7ecb757SRichard Henderson                     break;
1321d7ecb757SRichard Henderson                 case 0:
1322d7ecb757SRichard Henderson                     gen_raise_exception_sync(dc, EXCP_DEBUG);
1323d7ecb757SRichard Henderson                     break;
1324d7ecb757SRichard Henderson                 }
1325d7ecb757SRichard Henderson             }
1326d7ecb757SRichard Henderson         } else {
1327d7ecb757SRichard Henderson             dc->jmp = JMP_INDIRECT;
1328d7ecb757SRichard Henderson             tcg_gen_mov_i32(cpu_btarget, cpu_R[dc->rb]);
1329d7ecb757SRichard Henderson             if (link && !dslot) {
133041ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
133141ba37c4SRichard Henderson             }
1332fcf5ef2aSThomas Huth         }
1333d7ecb757SRichard Henderson     } else if (dc->type_b) {
1334fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT;
1335d7ecb757SRichard Henderson         dc->jmp_pc = dc->base.pc_next + dec_alu_typeb_imm(dc);
1336d7ecb757SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
1337fcf5ef2aSThomas Huth     } else {
1338d7ecb757SRichard Henderson         dc->jmp = JMP_INDIRECT;
1339d7ecb757SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dc->rb], dc->base.pc_next);
1340d7ecb757SRichard Henderson     }
13419b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
1342fcf5ef2aSThomas Huth }
1343fcf5ef2aSThomas Huth 
1344fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1345fcf5ef2aSThomas Huth {
1346cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1347cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1348cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13493e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13500a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13510a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1352cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1353fcf5ef2aSThomas Huth 
1354cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1355cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1356fcf5ef2aSThomas Huth     msr_write(dc, t1);
1357cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1358cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1359fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1360fcf5ef2aSThomas Huth }
1361fcf5ef2aSThomas Huth 
1362fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1363fcf5ef2aSThomas Huth {
1364cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1365cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1366cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13673e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13680a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1369cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1370cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1371fcf5ef2aSThomas Huth 
1372cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1373cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1374fcf5ef2aSThomas Huth     msr_write(dc, t1);
1375cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1376cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1377fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1378fcf5ef2aSThomas Huth }
1379fcf5ef2aSThomas Huth 
1380fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1381fcf5ef2aSThomas Huth {
1382cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1383cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1384cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1385fcf5ef2aSThomas Huth 
13863e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13870a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1388cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1389cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1390cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1391fcf5ef2aSThomas Huth 
1392cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1393cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1394fcf5ef2aSThomas Huth     msr_write(dc, t1);
1395cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1396cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1397fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1398fcf5ef2aSThomas Huth }
1399fcf5ef2aSThomas Huth 
1400fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1401fcf5ef2aSThomas Huth {
1402fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1403fcf5ef2aSThomas Huth 
1404fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1405fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1406fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1407fcf5ef2aSThomas Huth 
1408bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1409bdfc1e88SEdgar E. Iglesias         return;
1410bdfc1e88SEdgar E. Iglesias     }
1411bdfc1e88SEdgar E. Iglesias 
1412f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1413fcf5ef2aSThomas Huth 
1414fcf5ef2aSThomas Huth     if (i_bit) {
1415fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1416fcf5ef2aSThomas Huth     } else if (b_bit) {
1417fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1418fcf5ef2aSThomas Huth     } else if (e_bit) {
1419fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
142011105d67SRichard Henderson     }
1421fcf5ef2aSThomas Huth 
1422fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
14239b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
14240f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1425fcf5ef2aSThomas Huth }
1426fcf5ef2aSThomas Huth 
1427fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1428fcf5ef2aSThomas Huth {
1429fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
143041ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1431fcf5ef2aSThomas Huth     }
14322016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1433fcf5ef2aSThomas Huth }
1434fcf5ef2aSThomas Huth 
1435fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1436fcf5ef2aSThomas Huth {
1437fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1438fcf5ef2aSThomas Huth 
14399ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1440fcf5ef2aSThomas Huth         return;
1441fcf5ef2aSThomas Huth     }
1442fcf5ef2aSThomas Huth 
1443fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1444fcf5ef2aSThomas Huth 
1445fcf5ef2aSThomas Huth     switch (fpu_insn) {
1446fcf5ef2aSThomas Huth         case 0:
1447fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1448fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1449fcf5ef2aSThomas Huth             break;
1450fcf5ef2aSThomas Huth 
1451fcf5ef2aSThomas Huth         case 1:
1452fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1453fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1454fcf5ef2aSThomas Huth             break;
1455fcf5ef2aSThomas Huth 
1456fcf5ef2aSThomas Huth         case 2:
1457fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1458fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1459fcf5ef2aSThomas Huth             break;
1460fcf5ef2aSThomas Huth 
1461fcf5ef2aSThomas Huth         case 3:
1462fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1463fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1464fcf5ef2aSThomas Huth             break;
1465fcf5ef2aSThomas Huth 
1466fcf5ef2aSThomas Huth         case 4:
1467fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1468fcf5ef2aSThomas Huth                 case 0:
1469fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1470fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1471fcf5ef2aSThomas Huth                     break;
1472fcf5ef2aSThomas Huth                 case 1:
1473fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1474fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1475fcf5ef2aSThomas Huth                     break;
1476fcf5ef2aSThomas Huth                 case 2:
1477fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1478fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1479fcf5ef2aSThomas Huth                     break;
1480fcf5ef2aSThomas Huth                 case 3:
1481fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1482fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1483fcf5ef2aSThomas Huth                     break;
1484fcf5ef2aSThomas Huth                 case 4:
1485fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1486fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1487fcf5ef2aSThomas Huth                     break;
1488fcf5ef2aSThomas Huth                 case 5:
1489fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1490fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1491fcf5ef2aSThomas Huth                     break;
1492fcf5ef2aSThomas Huth                 case 6:
1493fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1494fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1495fcf5ef2aSThomas Huth                     break;
1496fcf5ef2aSThomas Huth                 default:
1497fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1498fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1499fcf5ef2aSThomas Huth                                   " opc=%x\n",
1500d4705ae0SRichard Henderson                                   fpu_insn, (uint32_t)dc->base.pc_next,
1501d4705ae0SRichard Henderson                                   dc->opcode);
1502fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1503fcf5ef2aSThomas Huth                     break;
1504fcf5ef2aSThomas Huth             }
1505fcf5ef2aSThomas Huth             break;
1506fcf5ef2aSThomas Huth 
1507fcf5ef2aSThomas Huth         case 5:
1508fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1509fcf5ef2aSThomas Huth                 return;
1510fcf5ef2aSThomas Huth             }
1511fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1512fcf5ef2aSThomas Huth             break;
1513fcf5ef2aSThomas Huth 
1514fcf5ef2aSThomas Huth         case 6:
1515fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1516fcf5ef2aSThomas Huth                 return;
1517fcf5ef2aSThomas Huth             }
1518fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1519fcf5ef2aSThomas Huth             break;
1520fcf5ef2aSThomas Huth 
1521fcf5ef2aSThomas Huth         case 7:
1522fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1523fcf5ef2aSThomas Huth                 return;
1524fcf5ef2aSThomas Huth             }
1525fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1526fcf5ef2aSThomas Huth             break;
1527fcf5ef2aSThomas Huth 
1528fcf5ef2aSThomas Huth         default:
1529fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1530fcf5ef2aSThomas Huth                           " opc=%x\n",
1531d4705ae0SRichard Henderson                           fpu_insn, (uint32_t)dc->base.pc_next, dc->opcode);
1532fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1533fcf5ef2aSThomas Huth             break;
1534fcf5ef2aSThomas Huth     }
1535fcf5ef2aSThomas Huth }
1536fcf5ef2aSThomas Huth 
1537fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1538fcf5ef2aSThomas Huth {
15399ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1540fcf5ef2aSThomas Huth         return;
1541fcf5ef2aSThomas Huth     }
1542d4705ae0SRichard Henderson     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n",
1543d4705ae0SRichard Henderson                   (uint32_t)dc->base.pc_next, dc->opcode);
1544fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1545fcf5ef2aSThomas Huth }
1546fcf5ef2aSThomas Huth 
1547fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1548fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1549fcf5ef2aSThomas Huth {
1550fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1551fcf5ef2aSThomas Huth     int ctrl;
1552fcf5ef2aSThomas Huth 
1553bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1554fcf5ef2aSThomas Huth         return;
1555fcf5ef2aSThomas Huth     }
1556fcf5ef2aSThomas Huth 
1557cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1558fcf5ef2aSThomas Huth     if (dc->type_b) {
1559cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1560fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1561fcf5ef2aSThomas Huth     } else {
1562cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1563fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1564fcf5ef2aSThomas Huth     }
1565fcf5ef2aSThomas Huth 
1566cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1567fcf5ef2aSThomas Huth 
1568fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1569fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1570fcf5ef2aSThomas Huth     } else {
1571fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1572fcf5ef2aSThomas Huth     }
1573cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1574cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1575fcf5ef2aSThomas Huth }
1576fcf5ef2aSThomas Huth 
1577fcf5ef2aSThomas Huth static struct decoder_info {
1578fcf5ef2aSThomas Huth     struct {
1579fcf5ef2aSThomas Huth         uint32_t bits;
1580fcf5ef2aSThomas Huth         uint32_t mask;
1581fcf5ef2aSThomas Huth     };
1582fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1583fcf5ef2aSThomas Huth } decinfo[] = {
1584fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1585fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1586fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1587fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1588fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1589fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1590fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1591fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1592fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1593fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1594fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1595fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1596fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1597fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1598fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1599fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1600fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1601fcf5ef2aSThomas Huth };
1602fcf5ef2aSThomas Huth 
160344d1432bSRichard Henderson static void old_decode(DisasContext *dc, uint32_t ir)
1604fcf5ef2aSThomas Huth {
1605fcf5ef2aSThomas Huth     int i;
1606fcf5ef2aSThomas Huth 
1607fcf5ef2aSThomas Huth     dc->ir = ir;
1608fcf5ef2aSThomas Huth 
1609fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1610fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1611fcf5ef2aSThomas Huth 
1612fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1613fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1614fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1615fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1616fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1617fcf5ef2aSThomas Huth 
1618fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1619fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1620fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1621fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1622fcf5ef2aSThomas Huth             break;
1623fcf5ef2aSThomas Huth         }
1624fcf5ef2aSThomas Huth     }
1625fcf5ef2aSThomas Huth }
1626fcf5ef2aSThomas Huth 
1627372122e3SRichard Henderson static void mb_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1628fcf5ef2aSThomas Huth {
1629372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1630372122e3SRichard Henderson     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1631372122e3SRichard Henderson     int bound;
1632fcf5ef2aSThomas Huth 
1633fcf5ef2aSThomas Huth     dc->cpu = cpu;
1634372122e3SRichard Henderson     dc->synced_flags = dc->tb_flags = dc->base.tb->flags;
1635fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1636372122e3SRichard Henderson     dc->jmp = dc->delayed_branch ? JMP_INDIRECT : JMP_NOJMP;
1637fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1638fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1639d7ecb757SRichard Henderson     dc->ext_imm = dc->base.tb->cs_base;
164020800179SRichard Henderson     dc->r0 = NULL;
164120800179SRichard Henderson     dc->r0_set = false;
1642fcf5ef2aSThomas Huth 
1643372122e3SRichard Henderson     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1644372122e3SRichard Henderson     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1645fcf5ef2aSThomas Huth }
1646fcf5ef2aSThomas Huth 
1647372122e3SRichard Henderson static void mb_tr_tb_start(DisasContextBase *dcb, CPUState *cs)
1648fcf5ef2aSThomas Huth {
1649fcf5ef2aSThomas Huth }
1650fcf5ef2aSThomas Huth 
1651372122e3SRichard Henderson static void mb_tr_insn_start(DisasContextBase *dcb, CPUState *cs)
1652372122e3SRichard Henderson {
1653372122e3SRichard Henderson     tcg_gen_insn_start(dcb->pc_next);
1654372122e3SRichard Henderson }
1655fcf5ef2aSThomas Huth 
1656372122e3SRichard Henderson static bool mb_tr_breakpoint_check(DisasContextBase *dcb, CPUState *cs,
1657372122e3SRichard Henderson                                    const CPUBreakpoint *bp)
1658372122e3SRichard Henderson {
1659372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1660372122e3SRichard Henderson 
1661372122e3SRichard Henderson     gen_raise_exception_sync(dc, EXCP_DEBUG);
1662372122e3SRichard Henderson 
1663372122e3SRichard Henderson     /*
1664372122e3SRichard Henderson      * The address covered by the breakpoint must be included in
1665372122e3SRichard Henderson      * [tb->pc, tb->pc + tb->size) in order to for it to be
1666372122e3SRichard Henderson      * properly cleared -- thus we increment the PC here so that
1667372122e3SRichard Henderson      * the logic setting tb->size below does the right thing.
1668372122e3SRichard Henderson      */
1669372122e3SRichard Henderson     dc->base.pc_next += 4;
1670372122e3SRichard Henderson     return true;
1671372122e3SRichard Henderson }
1672372122e3SRichard Henderson 
1673372122e3SRichard Henderson static void mb_tr_translate_insn(DisasContextBase *dcb, CPUState *cs)
1674372122e3SRichard Henderson {
1675372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1676372122e3SRichard Henderson     CPUMBState *env = cs->env_ptr;
167744d1432bSRichard Henderson     uint32_t ir;
1678372122e3SRichard Henderson 
1679372122e3SRichard Henderson     /* TODO: This should raise an exception, not terminate qemu. */
1680372122e3SRichard Henderson     if (dc->base.pc_next & 3) {
1681372122e3SRichard Henderson         cpu_abort(cs, "Microblaze: unaligned PC=%x\n",
1682372122e3SRichard Henderson                   (uint32_t)dc->base.pc_next);
1683fcf5ef2aSThomas Huth     }
1684fcf5ef2aSThomas Huth 
1685fcf5ef2aSThomas Huth     dc->clear_imm = 1;
168644d1432bSRichard Henderson     ir = cpu_ldl_code(env, dc->base.pc_next);
168744d1432bSRichard Henderson     if (!decode(dc, ir)) {
168844d1432bSRichard Henderson         old_decode(dc, ir);
168944d1432bSRichard Henderson     }
169020800179SRichard Henderson 
169120800179SRichard Henderson     if (dc->r0) {
169220800179SRichard Henderson         tcg_temp_free_i32(dc->r0);
169320800179SRichard Henderson         dc->r0 = NULL;
169420800179SRichard Henderson         dc->r0_set = false;
169520800179SRichard Henderson     }
169620800179SRichard Henderson 
1697d7ecb757SRichard Henderson     if (dc->clear_imm && (dc->tb_flags & IMM_FLAG)) {
1698fcf5ef2aSThomas Huth         dc->tb_flags &= ~IMM_FLAG;
1699d7ecb757SRichard Henderson         tcg_gen_discard_i32(cpu_imm);
1700372122e3SRichard Henderson     }
1701d4705ae0SRichard Henderson     dc->base.pc_next += 4;
1702fcf5ef2aSThomas Huth 
1703372122e3SRichard Henderson     if (dc->delayed_branch && --dc->delayed_branch == 0) {
1704372122e3SRichard Henderson         if (dc->tb_flags & DRTI_FLAG) {
1705fcf5ef2aSThomas Huth             do_rti(dc);
1706372122e3SRichard Henderson         }
1707372122e3SRichard Henderson         if (dc->tb_flags & DRTB_FLAG) {
1708fcf5ef2aSThomas Huth             do_rtb(dc);
1709372122e3SRichard Henderson         }
1710372122e3SRichard Henderson         if (dc->tb_flags & DRTE_FLAG) {
1711fcf5ef2aSThomas Huth             do_rte(dc);
1712372122e3SRichard Henderson         }
1713fcf5ef2aSThomas Huth         /* Clear the delay slot flag.  */
1714fcf5ef2aSThomas Huth         dc->tb_flags &= ~D_FLAG;
1715372122e3SRichard Henderson         dc->base.is_jmp = DISAS_JUMP;
1716372122e3SRichard Henderson     }
1717372122e3SRichard Henderson 
1718372122e3SRichard Henderson     /* Force an exit if the per-tb cpu state has changed.  */
1719372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NEXT && dc->cpustate_changed) {
1720372122e3SRichard Henderson         dc->base.is_jmp = DISAS_UPDATE;
1721372122e3SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1722372122e3SRichard Henderson     }
1723372122e3SRichard Henderson }
1724372122e3SRichard Henderson 
1725372122e3SRichard Henderson static void mb_tr_tb_stop(DisasContextBase *dcb, CPUState *cs)
1726372122e3SRichard Henderson {
1727372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1728372122e3SRichard Henderson 
1729372122e3SRichard Henderson     assert(!dc->abort_at_next_insn);
1730372122e3SRichard Henderson 
1731372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NORETURN) {
1732372122e3SRichard Henderson         /* We have already exited the TB. */
1733372122e3SRichard Henderson         return;
1734372122e3SRichard Henderson     }
1735372122e3SRichard Henderson 
1736372122e3SRichard Henderson     t_sync_flags(dc);
1737372122e3SRichard Henderson     if (dc->tb_flags & D_FLAG) {
1738372122e3SRichard Henderson         sync_jmpstate(dc);
1739372122e3SRichard Henderson         dc->jmp = JMP_NOJMP;
1740372122e3SRichard Henderson     }
1741372122e3SRichard Henderson 
1742372122e3SRichard Henderson     switch (dc->base.is_jmp) {
1743372122e3SRichard Henderson     case DISAS_TOO_MANY:
1744372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1745372122e3SRichard Henderson         gen_goto_tb(dc, 0, dc->base.pc_next);
1746372122e3SRichard Henderson         return;
1747372122e3SRichard Henderson 
1748372122e3SRichard Henderson     case DISAS_UPDATE:
1749372122e3SRichard Henderson         assert(dc->jmp == JMP_NOJMP);
1750372122e3SRichard Henderson         if (unlikely(cs->singlestep_enabled)) {
1751372122e3SRichard Henderson             gen_raise_exception(dc, EXCP_DEBUG);
1752372122e3SRichard Henderson         } else {
1753372122e3SRichard Henderson             tcg_gen_exit_tb(NULL, 0);
1754372122e3SRichard Henderson         }
1755372122e3SRichard Henderson         return;
1756372122e3SRichard Henderson 
1757372122e3SRichard Henderson     case DISAS_JUMP:
1758372122e3SRichard Henderson         switch (dc->jmp) {
1759372122e3SRichard Henderson         case JMP_INDIRECT:
1760372122e3SRichard Henderson             {
1761d4705ae0SRichard Henderson                 TCGv_i32 tmp_pc = tcg_const_i32(dc->base.pc_next);
17620f96e96bSRichard Henderson                 eval_cond_jmp(dc, cpu_btarget, tmp_pc);
17630f96e96bSRichard Henderson                 tcg_temp_free_i32(tmp_pc);
1764372122e3SRichard Henderson 
1765372122e3SRichard Henderson                 if (unlikely(cs->singlestep_enabled)) {
1766372122e3SRichard Henderson                     gen_raise_exception(dc, EXCP_DEBUG);
1767372122e3SRichard Henderson                 } else {
1768372122e3SRichard Henderson                     tcg_gen_exit_tb(NULL, 0);
1769372122e3SRichard Henderson                 }
1770372122e3SRichard Henderson             }
1771372122e3SRichard Henderson             return;
1772372122e3SRichard Henderson 
1773372122e3SRichard Henderson         case JMP_DIRECT_CC:
1774372122e3SRichard Henderson             {
1775fcf5ef2aSThomas Huth                 TCGLabel *l1 = gen_new_label();
17769b158558SRichard Henderson                 tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1777d4705ae0SRichard Henderson                 gen_goto_tb(dc, 1, dc->base.pc_next);
1778fcf5ef2aSThomas Huth                 gen_set_label(l1);
1779372122e3SRichard Henderson             }
1780372122e3SRichard Henderson             /* fall through */
1781372122e3SRichard Henderson 
1782372122e3SRichard Henderson         case JMP_DIRECT:
1783fcf5ef2aSThomas Huth             gen_goto_tb(dc, 0, dc->jmp_pc);
1784372122e3SRichard Henderson             return;
1785fcf5ef2aSThomas Huth         }
1786372122e3SRichard Henderson         /* fall through */
1787fcf5ef2aSThomas Huth 
1788a2b80dbdSRichard Henderson     default:
1789a2b80dbdSRichard Henderson         g_assert_not_reached();
1790fcf5ef2aSThomas Huth     }
1791fcf5ef2aSThomas Huth }
1792fcf5ef2aSThomas Huth 
1793372122e3SRichard Henderson static void mb_tr_disas_log(const DisasContextBase *dcb, CPUState *cs)
1794372122e3SRichard Henderson {
1795372122e3SRichard Henderson     qemu_log("IN: %s\n", lookup_symbol(dcb->pc_first));
1796372122e3SRichard Henderson     log_target_disas(cs, dcb->pc_first, dcb->tb->size);
1797fcf5ef2aSThomas Huth }
1798372122e3SRichard Henderson 
1799372122e3SRichard Henderson static const TranslatorOps mb_tr_ops = {
1800372122e3SRichard Henderson     .init_disas_context = mb_tr_init_disas_context,
1801372122e3SRichard Henderson     .tb_start           = mb_tr_tb_start,
1802372122e3SRichard Henderson     .insn_start         = mb_tr_insn_start,
1803372122e3SRichard Henderson     .breakpoint_check   = mb_tr_breakpoint_check,
1804372122e3SRichard Henderson     .translate_insn     = mb_tr_translate_insn,
1805372122e3SRichard Henderson     .tb_stop            = mb_tr_tb_stop,
1806372122e3SRichard Henderson     .disas_log          = mb_tr_disas_log,
1807372122e3SRichard Henderson };
1808372122e3SRichard Henderson 
1809372122e3SRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int max_insns)
1810372122e3SRichard Henderson {
1811372122e3SRichard Henderson     DisasContext dc;
1812372122e3SRichard Henderson     translator_loop(&mb_tr_ops, &dc.base, cpu, tb, max_insns);
1813fcf5ef2aSThomas Huth }
1814fcf5ef2aSThomas Huth 
181590c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1816fcf5ef2aSThomas Huth {
1817fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1818fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1819fcf5ef2aSThomas Huth     int i;
1820fcf5ef2aSThomas Huth 
182190c84c56SMarkus Armbruster     if (!env) {
1822fcf5ef2aSThomas Huth         return;
182390c84c56SMarkus Armbruster     }
1824fcf5ef2aSThomas Huth 
18250f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
182676e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
18276efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1828eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
182978e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1830eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
18310f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1832fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
18332e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
18342e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
18352e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
18362e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
18372ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
18382ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
18392ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
18402ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
18412ead1b18SJoe Komlodi         }
18422ead1b18SJoe Komlodi     }
1843fcf5ef2aSThomas Huth 
18442ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
184539db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1846af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
18472ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1848fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
184990c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1850fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
185190c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1852fcf5ef2aSThomas Huth         }
185390c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1854fcf5ef2aSThomas Huth }
1855fcf5ef2aSThomas Huth 
1856fcf5ef2aSThomas Huth void mb_tcg_init(void)
1857fcf5ef2aSThomas Huth {
1858480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1859480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1860fcf5ef2aSThomas Huth 
1861480d29a8SRichard Henderson     static const struct {
1862480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1863480d29a8SRichard Henderson     } i32s[] = {
1864480d29a8SRichard Henderson         R(0),  R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1865480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1866480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1867480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1868480d29a8SRichard Henderson 
1869480d29a8SRichard Henderson         SP(pc),
1870480d29a8SRichard Henderson         SP(msr),
18711074c0fbSRichard Henderson         SP(msr_c),
1872480d29a8SRichard Henderson         SP(imm),
1873480d29a8SRichard Henderson         SP(iflags),
1874480d29a8SRichard Henderson         SP(btaken),
1875480d29a8SRichard Henderson         SP(btarget),
1876480d29a8SRichard Henderson         SP(res_val),
1877480d29a8SRichard Henderson     };
1878480d29a8SRichard Henderson 
1879480d29a8SRichard Henderson #undef R
1880480d29a8SRichard Henderson #undef SP
1881480d29a8SRichard Henderson 
1882480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1883480d29a8SRichard Henderson         *i32s[i].var =
1884480d29a8SRichard Henderson           tcg_global_mem_new_i32(cpu_env, i32s[i].ofs, i32s[i].name);
1885fcf5ef2aSThomas Huth     }
188676e8187dSRichard Henderson 
1887480d29a8SRichard Henderson     cpu_res_addr =
1888480d29a8SRichard Henderson         tcg_global_mem_new(cpu_env, offsetof(CPUMBState, res_addr), "res_addr");
1889fcf5ef2aSThomas Huth }
1890fcf5ef2aSThomas Huth 
1891fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1892fcf5ef2aSThomas Huth                           target_ulong *data)
1893fcf5ef2aSThomas Huth {
189476e8187dSRichard Henderson     env->pc = data[0];
1895fcf5ef2aSThomas Huth }
1896