xref: /openbmc/qemu/target/microblaze/translate.c (revision 4597463b3851d9f6ec22542b6645511d7f889f78)
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
10ee452036SChetan Pant  * version 2.1 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"
25*4597463bSPhilippe Mathieu-Daudé #include "exec/cpu_ldst.h"
26dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
27fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
28fcf5ef2aSThomas Huth #include "exec/helper-gen.h"
2977fc6f5eSLluís Vilanova #include "exec/translator.h"
3090c84c56SMarkus Armbruster #include "qemu/qemu-print.h"
31fcf5ef2aSThomas Huth 
32fcf5ef2aSThomas Huth #include "exec/log.h"
33fcf5ef2aSThomas Huth 
34d53106c9SRichard Henderson #define HELPER_H "helper.h"
35d53106c9SRichard Henderson #include "exec/helper-info.c.inc"
36d53106c9SRichard Henderson #undef  HELPER_H
37d53106c9SRichard Henderson 
38fcf5ef2aSThomas Huth #define EXTRACT_FIELD(src, start, end) \
39fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
40fcf5ef2aSThomas Huth 
4177fc6f5eSLluís Vilanova /* is_jmp field values */
4277fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
4317e77796SRichard Henderson #define DISAS_EXIT    DISAS_TARGET_1 /* all cpu state modified dynamically */
4477fc6f5eSLluís Vilanova 
45f6278ca9SRichard Henderson /* cpu state besides pc was modified dynamically; update pc to next */
46f6278ca9SRichard Henderson #define DISAS_EXIT_NEXT DISAS_TARGET_2
47f6278ca9SRichard Henderson /* cpu state besides pc was modified dynamically; update pc to btarget */
48f6278ca9SRichard Henderson #define DISAS_EXIT_JUMP DISAS_TARGET_3
49f6278ca9SRichard Henderson 
50cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
510f96e96bSRichard Henderson static TCGv_i32 cpu_pc;
523e0e16aeSRichard Henderson static TCGv_i32 cpu_msr;
531074c0fbSRichard Henderson static TCGv_i32 cpu_msr_c;
549b158558SRichard Henderson static TCGv_i32 cpu_imm;
55b9c58aabSRichard Henderson static TCGv_i32 cpu_bvalue;
560f96e96bSRichard Henderson static TCGv_i32 cpu_btarget;
579b158558SRichard Henderson static TCGv_i32 cpu_iflags;
589b158558SRichard Henderson static TCGv cpu_res_addr;
599b158558SRichard Henderson static TCGv_i32 cpu_res_val;
60fcf5ef2aSThomas Huth 
61fcf5ef2aSThomas Huth /* This is the state at translation time.  */
62fcf5ef2aSThomas Huth typedef struct DisasContext {
63d4705ae0SRichard Henderson     DisasContextBase base;
644b893631SRichard Henderson     const MicroBlazeCPUConfig *cfg;
65fcf5ef2aSThomas Huth 
6620800179SRichard Henderson     TCGv_i32 r0;
6720800179SRichard Henderson     bool r0_set;
6820800179SRichard Henderson 
69fcf5ef2aSThomas Huth     /* Decoder.  */
70d7ecb757SRichard Henderson     uint32_t ext_imm;
71683a247eSRichard Henderson     unsigned int tb_flags;
726f9642d7SRichard Henderson     unsigned int tb_flags_to_set;
73287b1defSRichard Henderson     int mem_index;
74fcf5ef2aSThomas Huth 
75b9c58aabSRichard Henderson     /* Condition under which to jump, including NEVER and ALWAYS. */
76b9c58aabSRichard Henderson     TCGCond jmp_cond;
77b9c58aabSRichard Henderson 
78b9c58aabSRichard Henderson     /* Immediate branch-taken destination, or -1 for indirect. */
79b9c58aabSRichard Henderson     uint32_t jmp_dest;
80fcf5ef2aSThomas Huth } DisasContext;
81fcf5ef2aSThomas Huth 
8220800179SRichard Henderson static int typeb_imm(DisasContext *dc, int x)
8320800179SRichard Henderson {
8420800179SRichard Henderson     if (dc->tb_flags & IMM_FLAG) {
8520800179SRichard Henderson         return deposit32(dc->ext_imm, 0, 16, x);
8620800179SRichard Henderson     }
8720800179SRichard Henderson     return x;
8820800179SRichard Henderson }
8920800179SRichard Henderson 
9044d1432bSRichard Henderson /* Include the auto-generated decoder.  */
9144d1432bSRichard Henderson #include "decode-insns.c.inc"
9244d1432bSRichard Henderson 
93683a247eSRichard Henderson static void t_sync_flags(DisasContext *dc)
94fcf5ef2aSThomas Huth {
95fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
9688e74b61SRichard Henderson     if ((dc->tb_flags ^ dc->base.tb->flags) & IFLAGS_TB_MASK) {
9788e74b61SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags & IFLAGS_TB_MASK);
98fcf5ef2aSThomas Huth     }
99fcf5ef2aSThomas Huth }
100fcf5ef2aSThomas Huth 
10141ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
102fcf5ef2aSThomas Huth {
103ad75a51eSRichard Henderson     gen_helper_raise_exception(tcg_env, tcg_constant_i32(index));
104d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
105fcf5ef2aSThomas Huth }
106fcf5ef2aSThomas Huth 
10741ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
10841ba37c4SRichard Henderson {
10941ba37c4SRichard Henderson     t_sync_flags(dc);
110d4705ae0SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
11141ba37c4SRichard Henderson     gen_raise_exception(dc, index);
11241ba37c4SRichard Henderson }
11341ba37c4SRichard Henderson 
11441ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
11541ba37c4SRichard Henderson {
116a5ea3dd7SRichard Henderson     TCGv_i32 tmp = tcg_constant_i32(esr_ec);
117ad75a51eSRichard Henderson     tcg_gen_st_i32(tmp, tcg_env, offsetof(CPUMBState, esr));
11841ba37c4SRichard Henderson 
11941ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
12041ba37c4SRichard Henderson }
12141ba37c4SRichard Henderson 
122fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
123fcf5ef2aSThomas Huth {
12466345580SRichard Henderson     if (translator_use_goto_tb(&dc->base, dest)) {
125fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1260f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
127d4705ae0SRichard Henderson         tcg_gen_exit_tb(dc->base.tb, n);
128fcf5ef2aSThomas Huth     } else {
1290f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
1304059bd90SRichard Henderson         tcg_gen_lookup_and_goto_ptr();
131fcf5ef2aSThomas Huth     }
132d4705ae0SRichard Henderson     dc->base.is_jmp = DISAS_NORETURN;
133fcf5ef2aSThomas Huth }
134fcf5ef2aSThomas Huth 
135bdfc1e88SEdgar E. Iglesias /*
1369ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1379ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1389ba8cd45SEdgar E. Iglesias  */
1399ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1409ba8cd45SEdgar E. Iglesias {
1412c32179fSRichard Henderson     if (cond && (dc->tb_flags & MSR_EE)
1424b893631SRichard Henderson         && dc->cfg->illegal_opcode_exception) {
14341ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1449ba8cd45SEdgar E. Iglesias     }
1459ba8cd45SEdgar E. Iglesias     return cond;
1469ba8cd45SEdgar E. Iglesias }
1479ba8cd45SEdgar E. Iglesias 
1489ba8cd45SEdgar E. Iglesias /*
149bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
150bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
151bdfc1e88SEdgar E. Iglesias  */
152bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
153bdfc1e88SEdgar E. Iglesias {
154287b1defSRichard Henderson     bool cond_user = cond && dc->mem_index == MMU_USER_IDX;
155bdfc1e88SEdgar E. Iglesias 
1562c32179fSRichard Henderson     if (cond_user && (dc->tb_flags & MSR_EE)) {
15741ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
158bdfc1e88SEdgar E. Iglesias     }
159bdfc1e88SEdgar E. Iglesias     return cond_user;
160bdfc1e88SEdgar E. Iglesias }
161bdfc1e88SEdgar E. Iglesias 
1622a7567a2SRichard Henderson /*
1632a7567a2SRichard Henderson  * Return true, and log an error, if the current insn is
1642a7567a2SRichard Henderson  * within a delay slot.
1652a7567a2SRichard Henderson  */
1662a7567a2SRichard Henderson static bool invalid_delay_slot(DisasContext *dc, const char *insn_type)
1672a7567a2SRichard Henderson {
1682a7567a2SRichard Henderson     if (dc->tb_flags & D_FLAG) {
1692a7567a2SRichard Henderson         qemu_log_mask(LOG_GUEST_ERROR,
1702a7567a2SRichard Henderson                       "Invalid insn in delay slot: %s at %08x\n",
1712a7567a2SRichard Henderson                       insn_type, (uint32_t)dc->base.pc_next);
1722a7567a2SRichard Henderson         return true;
1732a7567a2SRichard Henderson     }
1742a7567a2SRichard Henderson     return false;
1752a7567a2SRichard Henderson }
1762a7567a2SRichard Henderson 
17720800179SRichard Henderson static TCGv_i32 reg_for_read(DisasContext *dc, int reg)
178fcf5ef2aSThomas Huth {
17920800179SRichard Henderson     if (likely(reg != 0)) {
18020800179SRichard Henderson         return cpu_R[reg];
181fcf5ef2aSThomas Huth     }
18220800179SRichard Henderson     if (!dc->r0_set) {
18320800179SRichard Henderson         if (dc->r0 == NULL) {
18420800179SRichard Henderson             dc->r0 = tcg_temp_new_i32();
185fcf5ef2aSThomas Huth         }
18620800179SRichard Henderson         tcg_gen_movi_i32(dc->r0, 0);
18720800179SRichard Henderson         dc->r0_set = true;
18820800179SRichard Henderson     }
18920800179SRichard Henderson     return dc->r0;
190fcf5ef2aSThomas Huth }
191fcf5ef2aSThomas Huth 
19220800179SRichard Henderson static TCGv_i32 reg_for_write(DisasContext *dc, int reg)
19320800179SRichard Henderson {
19420800179SRichard Henderson     if (likely(reg != 0)) {
19520800179SRichard Henderson         return cpu_R[reg];
19620800179SRichard Henderson     }
19720800179SRichard Henderson     if (dc->r0 == NULL) {
19820800179SRichard Henderson         dc->r0 = tcg_temp_new_i32();
19920800179SRichard Henderson     }
20020800179SRichard Henderson     return dc->r0;
201fcf5ef2aSThomas Huth }
202fcf5ef2aSThomas Huth 
20320800179SRichard Henderson static bool do_typea(DisasContext *dc, arg_typea *arg, bool side_effects,
20420800179SRichard Henderson                      void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32))
20520800179SRichard Henderson {
20620800179SRichard Henderson     TCGv_i32 rd, ra, rb;
20720800179SRichard Henderson 
20820800179SRichard Henderson     if (arg->rd == 0 && !side_effects) {
20920800179SRichard Henderson         return true;
210fcf5ef2aSThomas Huth     }
21120800179SRichard Henderson 
21220800179SRichard Henderson     rd = reg_for_write(dc, arg->rd);
21320800179SRichard Henderson     ra = reg_for_read(dc, arg->ra);
21420800179SRichard Henderson     rb = reg_for_read(dc, arg->rb);
21520800179SRichard Henderson     fn(rd, ra, rb);
21620800179SRichard Henderson     return true;
21720800179SRichard Henderson }
21820800179SRichard Henderson 
21939cf3864SRichard Henderson static bool do_typea0(DisasContext *dc, arg_typea0 *arg, bool side_effects,
22039cf3864SRichard Henderson                       void (*fn)(TCGv_i32, TCGv_i32))
22139cf3864SRichard Henderson {
22239cf3864SRichard Henderson     TCGv_i32 rd, ra;
22339cf3864SRichard Henderson 
22439cf3864SRichard Henderson     if (arg->rd == 0 && !side_effects) {
22539cf3864SRichard Henderson         return true;
22639cf3864SRichard Henderson     }
22739cf3864SRichard Henderson 
22839cf3864SRichard Henderson     rd = reg_for_write(dc, arg->rd);
22939cf3864SRichard Henderson     ra = reg_for_read(dc, arg->ra);
23039cf3864SRichard Henderson     fn(rd, ra);
23139cf3864SRichard Henderson     return true;
23239cf3864SRichard Henderson }
23339cf3864SRichard Henderson 
23420800179SRichard Henderson static bool do_typeb_imm(DisasContext *dc, arg_typeb *arg, bool side_effects,
23520800179SRichard Henderson                          void (*fni)(TCGv_i32, TCGv_i32, int32_t))
23620800179SRichard Henderson {
23720800179SRichard Henderson     TCGv_i32 rd, ra;
23820800179SRichard Henderson 
23920800179SRichard Henderson     if (arg->rd == 0 && !side_effects) {
24020800179SRichard Henderson         return true;
24120800179SRichard Henderson     }
24220800179SRichard Henderson 
24320800179SRichard Henderson     rd = reg_for_write(dc, arg->rd);
24420800179SRichard Henderson     ra = reg_for_read(dc, arg->ra);
24520800179SRichard Henderson     fni(rd, ra, arg->imm);
24620800179SRichard Henderson     return true;
24720800179SRichard Henderson }
24820800179SRichard Henderson 
24920800179SRichard Henderson static bool do_typeb_val(DisasContext *dc, arg_typeb *arg, bool side_effects,
25020800179SRichard Henderson                          void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32))
25120800179SRichard Henderson {
25220800179SRichard Henderson     TCGv_i32 rd, ra, imm;
25320800179SRichard Henderson 
25420800179SRichard Henderson     if (arg->rd == 0 && !side_effects) {
25520800179SRichard Henderson         return true;
25620800179SRichard Henderson     }
25720800179SRichard Henderson 
25820800179SRichard Henderson     rd = reg_for_write(dc, arg->rd);
25920800179SRichard Henderson     ra = reg_for_read(dc, arg->ra);
260a5ea3dd7SRichard Henderson     imm = tcg_constant_i32(arg->imm);
26120800179SRichard Henderson 
26220800179SRichard Henderson     fn(rd, ra, imm);
26320800179SRichard Henderson     return true;
26420800179SRichard Henderson }
26520800179SRichard Henderson 
26620800179SRichard Henderson #define DO_TYPEA(NAME, SE, FN) \
26720800179SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea *a) \
26820800179SRichard Henderson     { return do_typea(dc, a, SE, FN); }
26920800179SRichard Henderson 
270607f5767SRichard Henderson #define DO_TYPEA_CFG(NAME, CFG, SE, FN) \
271607f5767SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea *a) \
2724b893631SRichard Henderson     { return dc->cfg->CFG && do_typea(dc, a, SE, FN); }
273607f5767SRichard Henderson 
27439cf3864SRichard Henderson #define DO_TYPEA0(NAME, SE, FN) \
27539cf3864SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea0 *a) \
27639cf3864SRichard Henderson     { return do_typea0(dc, a, SE, FN); }
27739cf3864SRichard Henderson 
27839cf3864SRichard Henderson #define DO_TYPEA0_CFG(NAME, CFG, SE, FN) \
27939cf3864SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea0 *a) \
2804b893631SRichard Henderson     { return dc->cfg->CFG && do_typea0(dc, a, SE, FN); }
28139cf3864SRichard Henderson 
28220800179SRichard Henderson #define DO_TYPEBI(NAME, SE, FNI) \
28320800179SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typeb *a) \
28420800179SRichard Henderson     { return do_typeb_imm(dc, a, SE, FNI); }
28520800179SRichard Henderson 
28697955cebSRichard Henderson #define DO_TYPEBI_CFG(NAME, CFG, SE, FNI) \
28797955cebSRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typeb *a) \
2884b893631SRichard Henderson     { return dc->cfg->CFG && do_typeb_imm(dc, a, SE, FNI); }
28997955cebSRichard 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 
294d5aead3dSRichard Henderson #define ENV_WRAPPER2(NAME, HELPER) \
295d5aead3dSRichard Henderson     static void NAME(TCGv_i32 out, TCGv_i32 ina) \
296ad75a51eSRichard Henderson     { HELPER(out, tcg_env, ina); }
297d5aead3dSRichard Henderson 
298d5aead3dSRichard Henderson #define ENV_WRAPPER3(NAME, HELPER) \
299d5aead3dSRichard Henderson     static void NAME(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb) \
300ad75a51eSRichard Henderson     { HELPER(out, tcg_env, ina, inb); }
301d5aead3dSRichard Henderson 
30220800179SRichard Henderson /* No input carry, but output carry. */
30320800179SRichard Henderson static void gen_add(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
30420800179SRichard Henderson {
305a5ea3dd7SRichard Henderson     TCGv_i32 zero = tcg_constant_i32(0);
30620800179SRichard Henderson 
30720800179SRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, ina, zero, inb, zero);
30820800179SRichard Henderson }
30920800179SRichard Henderson 
31020800179SRichard Henderson /* Input and output carry. */
31120800179SRichard Henderson static void gen_addc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
31220800179SRichard Henderson {
313a5ea3dd7SRichard Henderson     TCGv_i32 zero = tcg_constant_i32(0);
31420800179SRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
31520800179SRichard Henderson 
31620800179SRichard Henderson     tcg_gen_add2_i32(tmp, cpu_msr_c, ina, zero, cpu_msr_c, zero);
31720800179SRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, tmp, cpu_msr_c, inb, zero);
31820800179SRichard Henderson }
31920800179SRichard Henderson 
32020800179SRichard Henderson /* Input carry, but no output carry. */
32120800179SRichard Henderson static void gen_addkc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
32220800179SRichard Henderson {
32320800179SRichard Henderson     tcg_gen_add_i32(out, ina, inb);
32420800179SRichard Henderson     tcg_gen_add_i32(out, out, cpu_msr_c);
32520800179SRichard Henderson }
32620800179SRichard Henderson 
32720800179SRichard Henderson DO_TYPEA(add, true, gen_add)
32820800179SRichard Henderson DO_TYPEA(addc, true, gen_addc)
32920800179SRichard Henderson DO_TYPEA(addk, false, tcg_gen_add_i32)
33020800179SRichard Henderson DO_TYPEA(addkc, true, gen_addkc)
33120800179SRichard Henderson 
33220800179SRichard Henderson DO_TYPEBV(addi, true, gen_add)
33320800179SRichard Henderson DO_TYPEBV(addic, true, gen_addc)
33420800179SRichard Henderson DO_TYPEBI(addik, false, tcg_gen_addi_i32)
33520800179SRichard Henderson DO_TYPEBV(addikc, true, gen_addkc)
33620800179SRichard Henderson 
337cb0a0a4cSRichard Henderson static void gen_andni(TCGv_i32 out, TCGv_i32 ina, int32_t imm)
338cb0a0a4cSRichard Henderson {
339cb0a0a4cSRichard Henderson     tcg_gen_andi_i32(out, ina, ~imm);
340cb0a0a4cSRichard Henderson }
341cb0a0a4cSRichard Henderson 
342cb0a0a4cSRichard Henderson DO_TYPEA(and, false, tcg_gen_and_i32)
343cb0a0a4cSRichard Henderson DO_TYPEBI(andi, false, tcg_gen_andi_i32)
344cb0a0a4cSRichard Henderson DO_TYPEA(andn, false, tcg_gen_andc_i32)
345cb0a0a4cSRichard Henderson DO_TYPEBI(andni, false, gen_andni)
346cb0a0a4cSRichard Henderson 
347081d8e02SRichard Henderson static void gen_bsra(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
348081d8e02SRichard Henderson {
349081d8e02SRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
350081d8e02SRichard Henderson     tcg_gen_andi_i32(tmp, inb, 31);
351081d8e02SRichard Henderson     tcg_gen_sar_i32(out, ina, tmp);
352081d8e02SRichard Henderson }
353081d8e02SRichard Henderson 
354081d8e02SRichard Henderson static void gen_bsrl(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
355081d8e02SRichard Henderson {
356081d8e02SRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
357081d8e02SRichard Henderson     tcg_gen_andi_i32(tmp, inb, 31);
358081d8e02SRichard Henderson     tcg_gen_shr_i32(out, ina, tmp);
359081d8e02SRichard Henderson }
360081d8e02SRichard Henderson 
361081d8e02SRichard Henderson static void gen_bsll(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
362081d8e02SRichard Henderson {
363081d8e02SRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
364081d8e02SRichard Henderson     tcg_gen_andi_i32(tmp, inb, 31);
365081d8e02SRichard Henderson     tcg_gen_shl_i32(out, ina, tmp);
366081d8e02SRichard Henderson }
367081d8e02SRichard Henderson 
368081d8e02SRichard Henderson static void gen_bsefi(TCGv_i32 out, TCGv_i32 ina, int32_t imm)
369081d8e02SRichard Henderson {
370081d8e02SRichard Henderson     /* Note that decodetree has extracted and reassembled imm_w/imm_s. */
371081d8e02SRichard Henderson     int imm_w = extract32(imm, 5, 5);
372081d8e02SRichard Henderson     int imm_s = extract32(imm, 0, 5);
373081d8e02SRichard Henderson 
374081d8e02SRichard Henderson     if (imm_w + imm_s > 32 || imm_w == 0) {
375081d8e02SRichard Henderson         /* These inputs have an undefined behavior.  */
376081d8e02SRichard Henderson         qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
377081d8e02SRichard Henderson                       imm_w, imm_s);
378081d8e02SRichard Henderson     } else {
379081d8e02SRichard Henderson         tcg_gen_extract_i32(out, ina, imm_s, imm_w);
380081d8e02SRichard Henderson     }
381081d8e02SRichard Henderson }
382081d8e02SRichard Henderson 
383081d8e02SRichard Henderson static void gen_bsifi(TCGv_i32 out, TCGv_i32 ina, int32_t imm)
384081d8e02SRichard Henderson {
385081d8e02SRichard Henderson     /* Note that decodetree has extracted and reassembled imm_w/imm_s. */
386081d8e02SRichard Henderson     int imm_w = extract32(imm, 5, 5);
387081d8e02SRichard Henderson     int imm_s = extract32(imm, 0, 5);
388081d8e02SRichard Henderson     int width = imm_w - imm_s + 1;
389081d8e02SRichard Henderson 
390081d8e02SRichard Henderson     if (imm_w < imm_s) {
391081d8e02SRichard Henderson         /* These inputs have an undefined behavior.  */
392081d8e02SRichard Henderson         qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
393081d8e02SRichard Henderson                       imm_w, imm_s);
394081d8e02SRichard Henderson     } else {
395081d8e02SRichard Henderson         tcg_gen_deposit_i32(out, out, ina, imm_s, width);
396081d8e02SRichard Henderson     }
397081d8e02SRichard Henderson }
398081d8e02SRichard Henderson 
399081d8e02SRichard Henderson DO_TYPEA_CFG(bsra, use_barrel, false, gen_bsra)
400081d8e02SRichard Henderson DO_TYPEA_CFG(bsrl, use_barrel, false, gen_bsrl)
401081d8e02SRichard Henderson DO_TYPEA_CFG(bsll, use_barrel, false, gen_bsll)
402081d8e02SRichard Henderson 
403081d8e02SRichard Henderson DO_TYPEBI_CFG(bsrai, use_barrel, false, tcg_gen_sari_i32)
404081d8e02SRichard Henderson DO_TYPEBI_CFG(bsrli, use_barrel, false, tcg_gen_shri_i32)
405081d8e02SRichard Henderson DO_TYPEBI_CFG(bslli, use_barrel, false, tcg_gen_shli_i32)
406081d8e02SRichard Henderson 
407081d8e02SRichard Henderson DO_TYPEBI_CFG(bsefi, use_barrel, false, gen_bsefi)
408081d8e02SRichard Henderson DO_TYPEBI_CFG(bsifi, use_barrel, false, gen_bsifi)
409081d8e02SRichard Henderson 
41039cf3864SRichard Henderson static void gen_clz(TCGv_i32 out, TCGv_i32 ina)
41139cf3864SRichard Henderson {
41239cf3864SRichard Henderson     tcg_gen_clzi_i32(out, ina, 32);
41339cf3864SRichard Henderson }
41439cf3864SRichard Henderson 
41539cf3864SRichard Henderson DO_TYPEA0_CFG(clz, use_pcmp_instr, false, gen_clz)
41639cf3864SRichard Henderson 
41758b48b63SRichard Henderson static void gen_cmp(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
41858b48b63SRichard Henderson {
41958b48b63SRichard Henderson     TCGv_i32 lt = tcg_temp_new_i32();
42058b48b63SRichard Henderson 
42158b48b63SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_LT, lt, inb, ina);
42258b48b63SRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
42358b48b63SRichard Henderson     tcg_gen_deposit_i32(out, out, lt, 31, 1);
42458b48b63SRichard Henderson }
42558b48b63SRichard Henderson 
42658b48b63SRichard Henderson static void gen_cmpu(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
42758b48b63SRichard Henderson {
42858b48b63SRichard Henderson     TCGv_i32 lt = tcg_temp_new_i32();
42958b48b63SRichard Henderson 
43058b48b63SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_LTU, lt, inb, ina);
43158b48b63SRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
43258b48b63SRichard Henderson     tcg_gen_deposit_i32(out, out, lt, 31, 1);
43358b48b63SRichard Henderson }
43458b48b63SRichard Henderson 
43558b48b63SRichard Henderson DO_TYPEA(cmp, false, gen_cmp)
43658b48b63SRichard Henderson DO_TYPEA(cmpu, false, gen_cmpu)
437a2b0b90eSRichard Henderson 
438d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fadd, gen_helper_fadd)
439d5aead3dSRichard Henderson ENV_WRAPPER3(gen_frsub, gen_helper_frsub)
440d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fmul, gen_helper_fmul)
441d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fdiv, gen_helper_fdiv)
442d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fcmp_un, gen_helper_fcmp_un)
443d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fcmp_lt, gen_helper_fcmp_lt)
444d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fcmp_eq, gen_helper_fcmp_eq)
445d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fcmp_le, gen_helper_fcmp_le)
446d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fcmp_gt, gen_helper_fcmp_gt)
447d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fcmp_ne, gen_helper_fcmp_ne)
448d5aead3dSRichard Henderson ENV_WRAPPER3(gen_fcmp_ge, gen_helper_fcmp_ge)
449d5aead3dSRichard Henderson 
450d5aead3dSRichard Henderson DO_TYPEA_CFG(fadd, use_fpu, true, gen_fadd)
451d5aead3dSRichard Henderson DO_TYPEA_CFG(frsub, use_fpu, true, gen_frsub)
452d5aead3dSRichard Henderson DO_TYPEA_CFG(fmul, use_fpu, true, gen_fmul)
453d5aead3dSRichard Henderson DO_TYPEA_CFG(fdiv, use_fpu, true, gen_fdiv)
454d5aead3dSRichard Henderson DO_TYPEA_CFG(fcmp_un, use_fpu, true, gen_fcmp_un)
455d5aead3dSRichard Henderson DO_TYPEA_CFG(fcmp_lt, use_fpu, true, gen_fcmp_lt)
456d5aead3dSRichard Henderson DO_TYPEA_CFG(fcmp_eq, use_fpu, true, gen_fcmp_eq)
457d5aead3dSRichard Henderson DO_TYPEA_CFG(fcmp_le, use_fpu, true, gen_fcmp_le)
458d5aead3dSRichard Henderson DO_TYPEA_CFG(fcmp_gt, use_fpu, true, gen_fcmp_gt)
459d5aead3dSRichard Henderson DO_TYPEA_CFG(fcmp_ne, use_fpu, true, gen_fcmp_ne)
460d5aead3dSRichard Henderson DO_TYPEA_CFG(fcmp_ge, use_fpu, true, gen_fcmp_ge)
461d5aead3dSRichard Henderson 
462d5aead3dSRichard Henderson ENV_WRAPPER2(gen_flt, gen_helper_flt)
463d5aead3dSRichard Henderson ENV_WRAPPER2(gen_fint, gen_helper_fint)
464d5aead3dSRichard Henderson ENV_WRAPPER2(gen_fsqrt, gen_helper_fsqrt)
465d5aead3dSRichard Henderson 
466d5aead3dSRichard Henderson DO_TYPEA0_CFG(flt, use_fpu >= 2, true, gen_flt)
467d5aead3dSRichard Henderson DO_TYPEA0_CFG(fint, use_fpu >= 2, true, gen_fint)
468d5aead3dSRichard Henderson DO_TYPEA0_CFG(fsqrt, use_fpu >= 2, true, gen_fsqrt)
469d5aead3dSRichard Henderson 
470d5aead3dSRichard Henderson /* Does not use ENV_WRAPPER3, because arguments are swapped as well. */
471b1354342SRichard Henderson static void gen_idiv(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
472b1354342SRichard Henderson {
473ad75a51eSRichard Henderson     gen_helper_divs(out, tcg_env, inb, ina);
474b1354342SRichard Henderson }
475b1354342SRichard Henderson 
476b1354342SRichard Henderson static void gen_idivu(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
477b1354342SRichard Henderson {
478ad75a51eSRichard Henderson     gen_helper_divu(out, tcg_env, inb, ina);
479b1354342SRichard Henderson }
480b1354342SRichard Henderson 
481b1354342SRichard Henderson DO_TYPEA_CFG(idiv, use_div, true, gen_idiv)
482b1354342SRichard Henderson DO_TYPEA_CFG(idivu, use_div, true, gen_idivu)
483b1354342SRichard Henderson 
484e64b2e5cSRichard Henderson static bool trans_imm(DisasContext *dc, arg_imm *arg)
485e64b2e5cSRichard Henderson {
4862a7567a2SRichard Henderson     if (invalid_delay_slot(dc, "imm")) {
4872a7567a2SRichard Henderson         return true;
4882a7567a2SRichard Henderson     }
489e64b2e5cSRichard Henderson     dc->ext_imm = arg->imm << 16;
490e64b2e5cSRichard Henderson     tcg_gen_movi_i32(cpu_imm, dc->ext_imm);
4916f9642d7SRichard Henderson     dc->tb_flags_to_set = IMM_FLAG;
492e64b2e5cSRichard Henderson     return true;
493e64b2e5cSRichard Henderson }
494e64b2e5cSRichard Henderson 
49597955cebSRichard Henderson static void gen_mulh(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
49697955cebSRichard Henderson {
49797955cebSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
49897955cebSRichard Henderson     tcg_gen_muls2_i32(tmp, out, ina, inb);
49997955cebSRichard Henderson }
50097955cebSRichard Henderson 
50197955cebSRichard Henderson static void gen_mulhu(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
50297955cebSRichard Henderson {
50397955cebSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
50497955cebSRichard Henderson     tcg_gen_mulu2_i32(tmp, out, ina, inb);
50597955cebSRichard Henderson }
50697955cebSRichard Henderson 
50797955cebSRichard Henderson static void gen_mulhsu(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
50897955cebSRichard Henderson {
50997955cebSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
51097955cebSRichard Henderson     tcg_gen_mulsu2_i32(tmp, out, ina, inb);
51197955cebSRichard Henderson }
51297955cebSRichard Henderson 
51397955cebSRichard Henderson DO_TYPEA_CFG(mul, use_hw_mul, false, tcg_gen_mul_i32)
51497955cebSRichard Henderson DO_TYPEA_CFG(mulh, use_hw_mul >= 2, false, gen_mulh)
51597955cebSRichard Henderson DO_TYPEA_CFG(mulhu, use_hw_mul >= 2, false, gen_mulhu)
51697955cebSRichard Henderson DO_TYPEA_CFG(mulhsu, use_hw_mul >= 2, false, gen_mulhsu)
51797955cebSRichard Henderson DO_TYPEBI_CFG(muli, use_hw_mul, false, tcg_gen_muli_i32)
51897955cebSRichard Henderson 
519cb0a0a4cSRichard Henderson DO_TYPEA(or, false, tcg_gen_or_i32)
520cb0a0a4cSRichard Henderson DO_TYPEBI(ori, false, tcg_gen_ori_i32)
521cb0a0a4cSRichard Henderson 
522607f5767SRichard Henderson static void gen_pcmpeq(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
523607f5767SRichard Henderson {
524607f5767SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_EQ, out, ina, inb);
525607f5767SRichard Henderson }
526607f5767SRichard Henderson 
527607f5767SRichard Henderson static void gen_pcmpne(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
528607f5767SRichard Henderson {
529607f5767SRichard Henderson     tcg_gen_setcond_i32(TCG_COND_NE, out, ina, inb);
530607f5767SRichard Henderson }
531607f5767SRichard Henderson 
532607f5767SRichard Henderson DO_TYPEA_CFG(pcmpbf, use_pcmp_instr, false, gen_helper_pcmpbf)
533607f5767SRichard Henderson DO_TYPEA_CFG(pcmpeq, use_pcmp_instr, false, gen_pcmpeq)
534607f5767SRichard Henderson DO_TYPEA_CFG(pcmpne, use_pcmp_instr, false, gen_pcmpne)
535607f5767SRichard Henderson 
536a2b0b90eSRichard Henderson /* No input carry, but output carry. */
537a2b0b90eSRichard Henderson static void gen_rsub(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
538a2b0b90eSRichard Henderson {
539a2b0b90eSRichard Henderson     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_msr_c, inb, ina);
540a2b0b90eSRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
541a2b0b90eSRichard Henderson }
542a2b0b90eSRichard Henderson 
543a2b0b90eSRichard Henderson /* Input and output carry. */
544a2b0b90eSRichard Henderson static void gen_rsubc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
545a2b0b90eSRichard Henderson {
546a5ea3dd7SRichard Henderson     TCGv_i32 zero = tcg_constant_i32(0);
547a2b0b90eSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
548a2b0b90eSRichard Henderson 
549a2b0b90eSRichard Henderson     tcg_gen_not_i32(tmp, ina);
550a2b0b90eSRichard Henderson     tcg_gen_add2_i32(tmp, cpu_msr_c, tmp, zero, cpu_msr_c, zero);
551a2b0b90eSRichard Henderson     tcg_gen_add2_i32(out, cpu_msr_c, tmp, cpu_msr_c, inb, zero);
552a2b0b90eSRichard Henderson }
553a2b0b90eSRichard Henderson 
554a2b0b90eSRichard Henderson /* No input or output carry. */
555a2b0b90eSRichard Henderson static void gen_rsubk(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
556a2b0b90eSRichard Henderson {
557a2b0b90eSRichard Henderson     tcg_gen_sub_i32(out, inb, ina);
558a2b0b90eSRichard Henderson }
559a2b0b90eSRichard Henderson 
560a2b0b90eSRichard Henderson /* Input carry, no output carry. */
561a2b0b90eSRichard Henderson static void gen_rsubkc(TCGv_i32 out, TCGv_i32 ina, TCGv_i32 inb)
562a2b0b90eSRichard Henderson {
563a2b0b90eSRichard Henderson     TCGv_i32 nota = tcg_temp_new_i32();
564a2b0b90eSRichard Henderson 
565a2b0b90eSRichard Henderson     tcg_gen_not_i32(nota, ina);
566a2b0b90eSRichard Henderson     tcg_gen_add_i32(out, inb, nota);
567a2b0b90eSRichard Henderson     tcg_gen_add_i32(out, out, cpu_msr_c);
568a2b0b90eSRichard Henderson }
569a2b0b90eSRichard Henderson 
570a2b0b90eSRichard Henderson DO_TYPEA(rsub, true, gen_rsub)
571a2b0b90eSRichard Henderson DO_TYPEA(rsubc, true, gen_rsubc)
572a2b0b90eSRichard Henderson DO_TYPEA(rsubk, false, gen_rsubk)
573a2b0b90eSRichard Henderson DO_TYPEA(rsubkc, true, gen_rsubkc)
574a2b0b90eSRichard Henderson 
575a2b0b90eSRichard Henderson DO_TYPEBV(rsubi, true, gen_rsub)
576a2b0b90eSRichard Henderson DO_TYPEBV(rsubic, true, gen_rsubc)
577a2b0b90eSRichard Henderson DO_TYPEBV(rsubik, false, gen_rsubk)
578a2b0b90eSRichard Henderson DO_TYPEBV(rsubikc, true, gen_rsubkc)
579a2b0b90eSRichard Henderson 
58039cf3864SRichard Henderson DO_TYPEA0(sext8, false, tcg_gen_ext8s_i32)
58139cf3864SRichard Henderson DO_TYPEA0(sext16, false, tcg_gen_ext16s_i32)
58239cf3864SRichard Henderson 
58339cf3864SRichard Henderson static void gen_sra(TCGv_i32 out, TCGv_i32 ina)
58439cf3864SRichard Henderson {
58539cf3864SRichard Henderson     tcg_gen_andi_i32(cpu_msr_c, ina, 1);
58639cf3864SRichard Henderson     tcg_gen_sari_i32(out, ina, 1);
58739cf3864SRichard Henderson }
58839cf3864SRichard Henderson 
58939cf3864SRichard Henderson static void gen_src(TCGv_i32 out, TCGv_i32 ina)
59039cf3864SRichard Henderson {
59139cf3864SRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
59239cf3864SRichard Henderson 
59339cf3864SRichard Henderson     tcg_gen_mov_i32(tmp, cpu_msr_c);
59439cf3864SRichard Henderson     tcg_gen_andi_i32(cpu_msr_c, ina, 1);
59539cf3864SRichard Henderson     tcg_gen_extract2_i32(out, ina, tmp, 1);
59639cf3864SRichard Henderson }
59739cf3864SRichard Henderson 
59839cf3864SRichard Henderson static void gen_srl(TCGv_i32 out, TCGv_i32 ina)
59939cf3864SRichard Henderson {
60039cf3864SRichard Henderson     tcg_gen_andi_i32(cpu_msr_c, ina, 1);
60139cf3864SRichard Henderson     tcg_gen_shri_i32(out, ina, 1);
60239cf3864SRichard Henderson }
60339cf3864SRichard Henderson 
60439cf3864SRichard Henderson DO_TYPEA0(sra, false, gen_sra)
60539cf3864SRichard Henderson DO_TYPEA0(src, false, gen_src)
60639cf3864SRichard Henderson DO_TYPEA0(srl, false, gen_srl)
60739cf3864SRichard Henderson 
60839cf3864SRichard Henderson static void gen_swaph(TCGv_i32 out, TCGv_i32 ina)
60939cf3864SRichard Henderson {
61039cf3864SRichard Henderson     tcg_gen_rotri_i32(out, ina, 16);
61139cf3864SRichard Henderson }
61239cf3864SRichard Henderson 
61339cf3864SRichard Henderson DO_TYPEA0(swapb, false, tcg_gen_bswap32_i32)
61439cf3864SRichard Henderson DO_TYPEA0(swaph, false, gen_swaph)
61539cf3864SRichard Henderson 
61639cf3864SRichard Henderson static bool trans_wdic(DisasContext *dc, arg_wdic *a)
61739cf3864SRichard Henderson {
61839cf3864SRichard Henderson     /* Cache operations are nops: only check for supervisor mode.  */
61939cf3864SRichard Henderson     trap_userspace(dc, true);
62039cf3864SRichard Henderson     return true;
62139cf3864SRichard Henderson }
62239cf3864SRichard Henderson 
623cb0a0a4cSRichard Henderson DO_TYPEA(xor, false, tcg_gen_xor_i32)
624cb0a0a4cSRichard Henderson DO_TYPEBI(xori, false, tcg_gen_xori_i32)
625cb0a0a4cSRichard Henderson 
626d8e59c4aSRichard Henderson static TCGv compute_ldst_addr_typea(DisasContext *dc, int ra, int rb)
627d8e59c4aSRichard Henderson {
628d8e59c4aSRichard Henderson     TCGv ret = tcg_temp_new();
629d8e59c4aSRichard Henderson 
630d8e59c4aSRichard Henderson     /* If any of the regs is r0, set t to the value of the other reg.  */
631d8e59c4aSRichard Henderson     if (ra && rb) {
632d8e59c4aSRichard Henderson         TCGv_i32 tmp = tcg_temp_new_i32();
633d8e59c4aSRichard Henderson         tcg_gen_add_i32(tmp, cpu_R[ra], cpu_R[rb]);
634d8e59c4aSRichard Henderson         tcg_gen_extu_i32_tl(ret, tmp);
635d8e59c4aSRichard Henderson     } else if (ra) {
636d8e59c4aSRichard Henderson         tcg_gen_extu_i32_tl(ret, cpu_R[ra]);
637d8e59c4aSRichard Henderson     } else if (rb) {
638d8e59c4aSRichard Henderson         tcg_gen_extu_i32_tl(ret, cpu_R[rb]);
639d8e59c4aSRichard Henderson     } else {
640d8e59c4aSRichard Henderson         tcg_gen_movi_tl(ret, 0);
641d8e59c4aSRichard Henderson     }
642d8e59c4aSRichard Henderson 
6434b893631SRichard Henderson     if ((ra == 1 || rb == 1) && dc->cfg->stackprot) {
644ad75a51eSRichard Henderson         gen_helper_stackprot(tcg_env, ret);
645d8e59c4aSRichard Henderson     }
646d8e59c4aSRichard Henderson     return ret;
647d8e59c4aSRichard Henderson }
648d8e59c4aSRichard Henderson 
649d8e59c4aSRichard Henderson static TCGv compute_ldst_addr_typeb(DisasContext *dc, int ra, int imm)
650d8e59c4aSRichard Henderson {
651d8e59c4aSRichard Henderson     TCGv ret = tcg_temp_new();
652d8e59c4aSRichard Henderson 
653d8e59c4aSRichard Henderson     /* If any of the regs is r0, set t to the value of the other reg.  */
654d8e59c4aSRichard Henderson     if (ra) {
655d8e59c4aSRichard Henderson         TCGv_i32 tmp = tcg_temp_new_i32();
656d8e59c4aSRichard Henderson         tcg_gen_addi_i32(tmp, cpu_R[ra], imm);
657d8e59c4aSRichard Henderson         tcg_gen_extu_i32_tl(ret, tmp);
658d8e59c4aSRichard Henderson     } else {
659d8e59c4aSRichard Henderson         tcg_gen_movi_tl(ret, (uint32_t)imm);
660d8e59c4aSRichard Henderson     }
661d8e59c4aSRichard Henderson 
6624b893631SRichard Henderson     if (ra == 1 && dc->cfg->stackprot) {
663ad75a51eSRichard Henderson         gen_helper_stackprot(tcg_env, ret);
664d8e59c4aSRichard Henderson     }
665d8e59c4aSRichard Henderson     return ret;
666d8e59c4aSRichard Henderson }
667d8e59c4aSRichard Henderson 
66819f27b6cSRichard Henderson #ifndef CONFIG_USER_ONLY
669d8e59c4aSRichard Henderson static TCGv compute_ldst_addr_ea(DisasContext *dc, int ra, int rb)
670d8e59c4aSRichard Henderson {
6714b893631SRichard Henderson     int addr_size = dc->cfg->addr_size;
672d8e59c4aSRichard Henderson     TCGv ret = tcg_temp_new();
673d8e59c4aSRichard Henderson 
674d8e59c4aSRichard Henderson     if (addr_size == 32 || ra == 0) {
675d8e59c4aSRichard Henderson         if (rb) {
676d8e59c4aSRichard Henderson             tcg_gen_extu_i32_tl(ret, cpu_R[rb]);
677d8e59c4aSRichard Henderson         } else {
678d8e59c4aSRichard Henderson             tcg_gen_movi_tl(ret, 0);
679d8e59c4aSRichard Henderson         }
680d8e59c4aSRichard Henderson     } else {
681d8e59c4aSRichard Henderson         if (rb) {
682d8e59c4aSRichard Henderson             tcg_gen_concat_i32_i64(ret, cpu_R[rb], cpu_R[ra]);
683d8e59c4aSRichard Henderson         } else {
684d8e59c4aSRichard Henderson             tcg_gen_extu_i32_tl(ret, cpu_R[ra]);
685d8e59c4aSRichard Henderson             tcg_gen_shli_tl(ret, ret, 32);
686d8e59c4aSRichard Henderson         }
687d8e59c4aSRichard Henderson         if (addr_size < 64) {
688d8e59c4aSRichard Henderson             /* Mask off out of range bits.  */
689d8e59c4aSRichard Henderson             tcg_gen_andi_i64(ret, ret, MAKE_64BIT_MASK(0, addr_size));
690d8e59c4aSRichard Henderson         }
691d8e59c4aSRichard Henderson     }
692d8e59c4aSRichard Henderson     return ret;
693d8e59c4aSRichard Henderson }
69419f27b6cSRichard Henderson #endif
695d8e59c4aSRichard Henderson 
696b414df75SRichard Henderson #ifndef CONFIG_USER_ONLY
697ab0c8d0fSRichard Henderson static void record_unaligned_ess(DisasContext *dc, int rd,
698ab0c8d0fSRichard Henderson                                  MemOp size, bool store)
699ab0c8d0fSRichard Henderson {
700e2313450SRichard Henderson     uint32_t iflags = tcg_get_insn_start_param(dc->base.insn_start, 1);
701ab0c8d0fSRichard Henderson 
702ab0c8d0fSRichard Henderson     iflags |= ESR_ESS_FLAG;
703ab0c8d0fSRichard Henderson     iflags |= rd << 5;
704ab0c8d0fSRichard Henderson     iflags |= store * ESR_S;
705ab0c8d0fSRichard Henderson     iflags |= (size == MO_32) * ESR_W;
706ab0c8d0fSRichard Henderson 
707e2313450SRichard Henderson     tcg_set_insn_start_param(dc->base.insn_start, 1, iflags);
708ab0c8d0fSRichard Henderson }
709b414df75SRichard Henderson #endif
710ab0c8d0fSRichard Henderson 
711d8e59c4aSRichard Henderson static bool do_load(DisasContext *dc, int rd, TCGv addr, MemOp mop,
712d8e59c4aSRichard Henderson                     int mem_index, bool rev)
713d8e59c4aSRichard Henderson {
714d8e59c4aSRichard Henderson     MemOp size = mop & MO_SIZE;
715d8e59c4aSRichard Henderson 
716d8e59c4aSRichard Henderson     /*
717d8e59c4aSRichard Henderson      * When doing reverse accesses we need to do two things.
718d8e59c4aSRichard Henderson      *
719d8e59c4aSRichard Henderson      * 1. Reverse the address wrt endianness.
720d8e59c4aSRichard Henderson      * 2. Byteswap the data lanes on the way back into the CPU core.
721d8e59c4aSRichard Henderson      */
722d8e59c4aSRichard Henderson     if (rev) {
723d8e59c4aSRichard Henderson         if (size > MO_8) {
724d8e59c4aSRichard Henderson             mop ^= MO_BSWAP;
725d8e59c4aSRichard Henderson         }
726d8e59c4aSRichard Henderson         if (size < MO_32) {
727d8e59c4aSRichard Henderson             tcg_gen_xori_tl(addr, addr, 3 - size);
728d8e59c4aSRichard Henderson         }
729d8e59c4aSRichard Henderson     }
730d8e59c4aSRichard Henderson 
731b414df75SRichard Henderson     /*
732b414df75SRichard Henderson      * For system mode, enforce alignment if the cpu configuration
733b414df75SRichard Henderson      * requires it.  For user-mode, the Linux kernel will have fixed up
734b414df75SRichard Henderson      * any unaligned access, so emulate that by *not* setting MO_ALIGN.
735b414df75SRichard Henderson      */
736b414df75SRichard Henderson #ifndef CONFIG_USER_ONLY
737ab0c8d0fSRichard Henderson     if (size > MO_8 &&
738ab0c8d0fSRichard Henderson         (dc->tb_flags & MSR_EE) &&
7394b893631SRichard Henderson         dc->cfg->unaligned_exceptions) {
740ab0c8d0fSRichard Henderson         record_unaligned_ess(dc, rd, size, false);
741ab0c8d0fSRichard Henderson         mop |= MO_ALIGN;
742d8e59c4aSRichard Henderson     }
743b414df75SRichard Henderson #endif
744d8e59c4aSRichard Henderson 
745ab0c8d0fSRichard Henderson     tcg_gen_qemu_ld_i32(reg_for_write(dc, rd), addr, mem_index, mop);
746d8e59c4aSRichard Henderson     return true;
747d8e59c4aSRichard Henderson }
748d8e59c4aSRichard Henderson 
749d8e59c4aSRichard Henderson static bool trans_lbu(DisasContext *dc, arg_typea *arg)
750d8e59c4aSRichard Henderson {
751d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
752d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_UB, dc->mem_index, false);
753d8e59c4aSRichard Henderson }
754d8e59c4aSRichard Henderson 
755d8e59c4aSRichard Henderson static bool trans_lbur(DisasContext *dc, arg_typea *arg)
756d8e59c4aSRichard Henderson {
757d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
758d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_UB, dc->mem_index, true);
759d8e59c4aSRichard Henderson }
760d8e59c4aSRichard Henderson 
761d8e59c4aSRichard Henderson static bool trans_lbuea(DisasContext *dc, arg_typea *arg)
762d8e59c4aSRichard Henderson {
763d8e59c4aSRichard Henderson     if (trap_userspace(dc, true)) {
764d8e59c4aSRichard Henderson         return true;
765d8e59c4aSRichard Henderson     }
76619f27b6cSRichard Henderson #ifdef CONFIG_USER_ONLY
76719f27b6cSRichard Henderson     return true;
76819f27b6cSRichard Henderson #else
769d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_ea(dc, arg->ra, arg->rb);
770d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_UB, MMU_NOMMU_IDX, false);
77119f27b6cSRichard Henderson #endif
772d8e59c4aSRichard Henderson }
773d8e59c4aSRichard Henderson 
774d8e59c4aSRichard Henderson static bool trans_lbui(DisasContext *dc, arg_typeb *arg)
775d8e59c4aSRichard Henderson {
776d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typeb(dc, arg->ra, arg->imm);
777d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_UB, dc->mem_index, false);
778d8e59c4aSRichard Henderson }
779d8e59c4aSRichard Henderson 
780d8e59c4aSRichard Henderson static bool trans_lhu(DisasContext *dc, arg_typea *arg)
781d8e59c4aSRichard Henderson {
782d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
783d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUW, dc->mem_index, false);
784d8e59c4aSRichard Henderson }
785d8e59c4aSRichard Henderson 
786d8e59c4aSRichard Henderson static bool trans_lhur(DisasContext *dc, arg_typea *arg)
787d8e59c4aSRichard Henderson {
788d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
789d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUW, dc->mem_index, true);
790d8e59c4aSRichard Henderson }
791d8e59c4aSRichard Henderson 
792d8e59c4aSRichard Henderson static bool trans_lhuea(DisasContext *dc, arg_typea *arg)
793d8e59c4aSRichard Henderson {
794d8e59c4aSRichard Henderson     if (trap_userspace(dc, true)) {
795d8e59c4aSRichard Henderson         return true;
796d8e59c4aSRichard Henderson     }
79719f27b6cSRichard Henderson #ifdef CONFIG_USER_ONLY
79819f27b6cSRichard Henderson     return true;
79919f27b6cSRichard Henderson #else
800d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_ea(dc, arg->ra, arg->rb);
801d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUW, MMU_NOMMU_IDX, false);
80219f27b6cSRichard Henderson #endif
803d8e59c4aSRichard Henderson }
804d8e59c4aSRichard Henderson 
805d8e59c4aSRichard Henderson static bool trans_lhui(DisasContext *dc, arg_typeb *arg)
806d8e59c4aSRichard Henderson {
807d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typeb(dc, arg->ra, arg->imm);
808d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUW, dc->mem_index, false);
809d8e59c4aSRichard Henderson }
810d8e59c4aSRichard Henderson 
811d8e59c4aSRichard Henderson static bool trans_lw(DisasContext *dc, arg_typea *arg)
812d8e59c4aSRichard Henderson {
813d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
814d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUL, dc->mem_index, false);
815d8e59c4aSRichard Henderson }
816d8e59c4aSRichard Henderson 
817d8e59c4aSRichard Henderson static bool trans_lwr(DisasContext *dc, arg_typea *arg)
818d8e59c4aSRichard Henderson {
819d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
820d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUL, dc->mem_index, true);
821d8e59c4aSRichard Henderson }
822d8e59c4aSRichard Henderson 
823d8e59c4aSRichard Henderson static bool trans_lwea(DisasContext *dc, arg_typea *arg)
824d8e59c4aSRichard Henderson {
825d8e59c4aSRichard Henderson     if (trap_userspace(dc, true)) {
826d8e59c4aSRichard Henderson         return true;
827d8e59c4aSRichard Henderson     }
82819f27b6cSRichard Henderson #ifdef CONFIG_USER_ONLY
82919f27b6cSRichard Henderson     return true;
83019f27b6cSRichard Henderson #else
831d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_ea(dc, arg->ra, arg->rb);
832d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUL, MMU_NOMMU_IDX, false);
83319f27b6cSRichard Henderson #endif
834d8e59c4aSRichard Henderson }
835d8e59c4aSRichard Henderson 
836d8e59c4aSRichard Henderson static bool trans_lwi(DisasContext *dc, arg_typeb *arg)
837d8e59c4aSRichard Henderson {
838d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typeb(dc, arg->ra, arg->imm);
839d8e59c4aSRichard Henderson     return do_load(dc, arg->rd, addr, MO_TEUL, dc->mem_index, false);
840d8e59c4aSRichard Henderson }
841d8e59c4aSRichard Henderson 
842d8e59c4aSRichard Henderson static bool trans_lwx(DisasContext *dc, arg_typea *arg)
843d8e59c4aSRichard Henderson {
844d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
845d8e59c4aSRichard Henderson 
846d8e59c4aSRichard Henderson     /* lwx does not throw unaligned access errors, so force alignment */
847d8e59c4aSRichard Henderson     tcg_gen_andi_tl(addr, addr, ~3);
848d8e59c4aSRichard Henderson 
849d8e59c4aSRichard Henderson     tcg_gen_qemu_ld_i32(cpu_res_val, addr, dc->mem_index, MO_TEUL);
850d8e59c4aSRichard Henderson     tcg_gen_mov_tl(cpu_res_addr, addr);
851d8e59c4aSRichard Henderson 
852d8e59c4aSRichard Henderson     if (arg->rd) {
853d8e59c4aSRichard Henderson         tcg_gen_mov_i32(cpu_R[arg->rd], cpu_res_val);
854d8e59c4aSRichard Henderson     }
855d8e59c4aSRichard Henderson 
856d8e59c4aSRichard Henderson     /* No support for AXI exclusive so always clear C */
857d8e59c4aSRichard Henderson     tcg_gen_movi_i32(cpu_msr_c, 0);
858d8e59c4aSRichard Henderson     return true;
859d8e59c4aSRichard Henderson }
860d8e59c4aSRichard Henderson 
861d8e59c4aSRichard Henderson static bool do_store(DisasContext *dc, int rd, TCGv addr, MemOp mop,
862d8e59c4aSRichard Henderson                      int mem_index, bool rev)
863d8e59c4aSRichard Henderson {
864d8e59c4aSRichard Henderson     MemOp size = mop & MO_SIZE;
865d8e59c4aSRichard Henderson 
866d8e59c4aSRichard Henderson     /*
867d8e59c4aSRichard Henderson      * When doing reverse accesses we need to do two things.
868d8e59c4aSRichard Henderson      *
869d8e59c4aSRichard Henderson      * 1. Reverse the address wrt endianness.
870d8e59c4aSRichard Henderson      * 2. Byteswap the data lanes on the way back into the CPU core.
871d8e59c4aSRichard Henderson      */
872d8e59c4aSRichard Henderson     if (rev) {
873d8e59c4aSRichard Henderson         if (size > MO_8) {
874d8e59c4aSRichard Henderson             mop ^= MO_BSWAP;
875d8e59c4aSRichard Henderson         }
876d8e59c4aSRichard Henderson         if (size < MO_32) {
877d8e59c4aSRichard Henderson             tcg_gen_xori_tl(addr, addr, 3 - size);
878d8e59c4aSRichard Henderson         }
879d8e59c4aSRichard Henderson     }
880d8e59c4aSRichard Henderson 
881b414df75SRichard Henderson     /*
882b414df75SRichard Henderson      * For system mode, enforce alignment if the cpu configuration
883b414df75SRichard Henderson      * requires it.  For user-mode, the Linux kernel will have fixed up
884b414df75SRichard Henderson      * any unaligned access, so emulate that by *not* setting MO_ALIGN.
885b414df75SRichard Henderson      */
886b414df75SRichard Henderson #ifndef CONFIG_USER_ONLY
887ab0c8d0fSRichard Henderson     if (size > MO_8 &&
888ab0c8d0fSRichard Henderson         (dc->tb_flags & MSR_EE) &&
8894b893631SRichard Henderson         dc->cfg->unaligned_exceptions) {
890ab0c8d0fSRichard Henderson         record_unaligned_ess(dc, rd, size, true);
891ab0c8d0fSRichard Henderson         mop |= MO_ALIGN;
892d8e59c4aSRichard Henderson     }
893b414df75SRichard Henderson #endif
894d8e59c4aSRichard Henderson 
895ab0c8d0fSRichard Henderson     tcg_gen_qemu_st_i32(reg_for_read(dc, rd), addr, mem_index, mop);
896d8e59c4aSRichard Henderson     return true;
897d8e59c4aSRichard Henderson }
898d8e59c4aSRichard Henderson 
899d8e59c4aSRichard Henderson static bool trans_sb(DisasContext *dc, arg_typea *arg)
900d8e59c4aSRichard Henderson {
901d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
902d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_UB, dc->mem_index, false);
903d8e59c4aSRichard Henderson }
904d8e59c4aSRichard Henderson 
905d8e59c4aSRichard Henderson static bool trans_sbr(DisasContext *dc, arg_typea *arg)
906d8e59c4aSRichard Henderson {
907d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
908d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_UB, dc->mem_index, true);
909d8e59c4aSRichard Henderson }
910d8e59c4aSRichard Henderson 
911d8e59c4aSRichard Henderson static bool trans_sbea(DisasContext *dc, arg_typea *arg)
912d8e59c4aSRichard Henderson {
913d8e59c4aSRichard Henderson     if (trap_userspace(dc, true)) {
914d8e59c4aSRichard Henderson         return true;
915d8e59c4aSRichard Henderson     }
91619f27b6cSRichard Henderson #ifdef CONFIG_USER_ONLY
91719f27b6cSRichard Henderson     return true;
91819f27b6cSRichard Henderson #else
919d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_ea(dc, arg->ra, arg->rb);
920d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_UB, MMU_NOMMU_IDX, false);
92119f27b6cSRichard Henderson #endif
922d8e59c4aSRichard Henderson }
923d8e59c4aSRichard Henderson 
924d8e59c4aSRichard Henderson static bool trans_sbi(DisasContext *dc, arg_typeb *arg)
925d8e59c4aSRichard Henderson {
926d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typeb(dc, arg->ra, arg->imm);
927d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_UB, dc->mem_index, false);
928d8e59c4aSRichard Henderson }
929d8e59c4aSRichard Henderson 
930d8e59c4aSRichard Henderson static bool trans_sh(DisasContext *dc, arg_typea *arg)
931d8e59c4aSRichard Henderson {
932d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
933d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUW, dc->mem_index, false);
934d8e59c4aSRichard Henderson }
935d8e59c4aSRichard Henderson 
936d8e59c4aSRichard Henderson static bool trans_shr(DisasContext *dc, arg_typea *arg)
937d8e59c4aSRichard Henderson {
938d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
939d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUW, dc->mem_index, true);
940d8e59c4aSRichard Henderson }
941d8e59c4aSRichard Henderson 
942d8e59c4aSRichard Henderson static bool trans_shea(DisasContext *dc, arg_typea *arg)
943d8e59c4aSRichard Henderson {
944d8e59c4aSRichard Henderson     if (trap_userspace(dc, true)) {
945d8e59c4aSRichard Henderson         return true;
946d8e59c4aSRichard Henderson     }
94719f27b6cSRichard Henderson #ifdef CONFIG_USER_ONLY
94819f27b6cSRichard Henderson     return true;
94919f27b6cSRichard Henderson #else
950d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_ea(dc, arg->ra, arg->rb);
951d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUW, MMU_NOMMU_IDX, false);
95219f27b6cSRichard Henderson #endif
953d8e59c4aSRichard Henderson }
954d8e59c4aSRichard Henderson 
955d8e59c4aSRichard Henderson static bool trans_shi(DisasContext *dc, arg_typeb *arg)
956d8e59c4aSRichard Henderson {
957d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typeb(dc, arg->ra, arg->imm);
958d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUW, dc->mem_index, false);
959d8e59c4aSRichard Henderson }
960d8e59c4aSRichard Henderson 
961d8e59c4aSRichard Henderson static bool trans_sw(DisasContext *dc, arg_typea *arg)
962d8e59c4aSRichard Henderson {
963d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
964d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUL, dc->mem_index, false);
965d8e59c4aSRichard Henderson }
966d8e59c4aSRichard Henderson 
967d8e59c4aSRichard Henderson static bool trans_swr(DisasContext *dc, arg_typea *arg)
968d8e59c4aSRichard Henderson {
969d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
970d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUL, dc->mem_index, true);
971d8e59c4aSRichard Henderson }
972d8e59c4aSRichard Henderson 
973d8e59c4aSRichard Henderson static bool trans_swea(DisasContext *dc, arg_typea *arg)
974d8e59c4aSRichard Henderson {
975d8e59c4aSRichard Henderson     if (trap_userspace(dc, true)) {
976d8e59c4aSRichard Henderson         return true;
977d8e59c4aSRichard Henderson     }
97819f27b6cSRichard Henderson #ifdef CONFIG_USER_ONLY
97919f27b6cSRichard Henderson     return true;
98019f27b6cSRichard Henderson #else
981d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_ea(dc, arg->ra, arg->rb);
982d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUL, MMU_NOMMU_IDX, false);
98319f27b6cSRichard Henderson #endif
984d8e59c4aSRichard Henderson }
985d8e59c4aSRichard Henderson 
986d8e59c4aSRichard Henderson static bool trans_swi(DisasContext *dc, arg_typeb *arg)
987d8e59c4aSRichard Henderson {
988d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typeb(dc, arg->ra, arg->imm);
989d8e59c4aSRichard Henderson     return do_store(dc, arg->rd, addr, MO_TEUL, dc->mem_index, false);
990d8e59c4aSRichard Henderson }
991d8e59c4aSRichard Henderson 
992d8e59c4aSRichard Henderson static bool trans_swx(DisasContext *dc, arg_typea *arg)
993d8e59c4aSRichard Henderson {
994d8e59c4aSRichard Henderson     TCGv addr = compute_ldst_addr_typea(dc, arg->ra, arg->rb);
995d8e59c4aSRichard Henderson     TCGLabel *swx_done = gen_new_label();
996d8e59c4aSRichard Henderson     TCGLabel *swx_fail = gen_new_label();
997d8e59c4aSRichard Henderson     TCGv_i32 tval;
998d8e59c4aSRichard Henderson 
999d8e59c4aSRichard Henderson     /* swx does not throw unaligned access errors, so force alignment */
1000d8e59c4aSRichard Henderson     tcg_gen_andi_tl(addr, addr, ~3);
1001d8e59c4aSRichard Henderson 
1002d8e59c4aSRichard Henderson     /*
1003d8e59c4aSRichard Henderson      * Compare the address vs the one we used during lwx.
1004d8e59c4aSRichard Henderson      * On mismatch, the operation fails.  On match, addr dies at the
1005d8e59c4aSRichard Henderson      * branch, but we know we can use the equal version in the global.
1006d8e59c4aSRichard Henderson      * In either case, addr is no longer needed.
1007d8e59c4aSRichard Henderson      */
1008d8e59c4aSRichard Henderson     tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_fail);
1009d8e59c4aSRichard Henderson 
1010d8e59c4aSRichard Henderson     /*
1011d8e59c4aSRichard Henderson      * Compare the value loaded during lwx with current contents of
1012d8e59c4aSRichard Henderson      * the reserved location.
1013d8e59c4aSRichard Henderson      */
1014d8e59c4aSRichard Henderson     tval = tcg_temp_new_i32();
1015d8e59c4aSRichard Henderson 
1016d8e59c4aSRichard Henderson     tcg_gen_atomic_cmpxchg_i32(tval, cpu_res_addr, cpu_res_val,
1017d8e59c4aSRichard Henderson                                reg_for_write(dc, arg->rd),
1018d8e59c4aSRichard Henderson                                dc->mem_index, MO_TEUL);
1019d8e59c4aSRichard Henderson 
1020d8e59c4aSRichard Henderson     tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_fail);
1021d8e59c4aSRichard Henderson 
1022d8e59c4aSRichard Henderson     /* Success */
1023d8e59c4aSRichard Henderson     tcg_gen_movi_i32(cpu_msr_c, 0);
1024d8e59c4aSRichard Henderson     tcg_gen_br(swx_done);
1025d8e59c4aSRichard Henderson 
1026d8e59c4aSRichard Henderson     /* Failure */
1027d8e59c4aSRichard Henderson     gen_set_label(swx_fail);
1028d8e59c4aSRichard Henderson     tcg_gen_movi_i32(cpu_msr_c, 1);
1029d8e59c4aSRichard Henderson 
1030d8e59c4aSRichard Henderson     gen_set_label(swx_done);
1031d8e59c4aSRichard Henderson 
1032d8e59c4aSRichard Henderson     /*
1033d8e59c4aSRichard Henderson      * Prevent the saved address from working again without another ldx.
1034d8e59c4aSRichard Henderson      * Akin to the pseudocode setting reservation = 0.
1035d8e59c4aSRichard Henderson      */
1036d8e59c4aSRichard Henderson     tcg_gen_movi_tl(cpu_res_addr, -1);
1037d8e59c4aSRichard Henderson     return true;
1038d8e59c4aSRichard Henderson }
1039d8e59c4aSRichard Henderson 
104016bbbbc9SRichard Henderson static void setup_dslot(DisasContext *dc, bool type_b)
104116bbbbc9SRichard Henderson {
104216bbbbc9SRichard Henderson     dc->tb_flags_to_set |= D_FLAG;
104316bbbbc9SRichard Henderson     if (type_b && (dc->tb_flags & IMM_FLAG)) {
104416bbbbc9SRichard Henderson         dc->tb_flags_to_set |= BIMM_FLAG;
104516bbbbc9SRichard Henderson     }
104616bbbbc9SRichard Henderson }
104716bbbbc9SRichard Henderson 
104816bbbbc9SRichard Henderson static bool do_branch(DisasContext *dc, int dest_rb, int dest_imm,
104916bbbbc9SRichard Henderson                       bool delay, bool abs, int link)
105016bbbbc9SRichard Henderson {
105116bbbbc9SRichard Henderson     uint32_t add_pc;
105216bbbbc9SRichard Henderson 
10532a7567a2SRichard Henderson     if (invalid_delay_slot(dc, "branch")) {
10542a7567a2SRichard Henderson         return true;
10552a7567a2SRichard Henderson     }
105616bbbbc9SRichard Henderson     if (delay) {
105716bbbbc9SRichard Henderson         setup_dslot(dc, dest_rb < 0);
105816bbbbc9SRichard Henderson     }
105916bbbbc9SRichard Henderson 
106016bbbbc9SRichard Henderson     if (link) {
106116bbbbc9SRichard Henderson         tcg_gen_movi_i32(cpu_R[link], dc->base.pc_next);
106216bbbbc9SRichard Henderson     }
106316bbbbc9SRichard Henderson 
106416bbbbc9SRichard Henderson     /* Store the branch taken destination into btarget.  */
106516bbbbc9SRichard Henderson     add_pc = abs ? 0 : dc->base.pc_next;
106616bbbbc9SRichard Henderson     if (dest_rb > 0) {
106716bbbbc9SRichard Henderson         dc->jmp_dest = -1;
106816bbbbc9SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dest_rb], add_pc);
106916bbbbc9SRichard Henderson     } else {
107016bbbbc9SRichard Henderson         dc->jmp_dest = add_pc + dest_imm;
107116bbbbc9SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_dest);
107216bbbbc9SRichard Henderson     }
107316bbbbc9SRichard Henderson     dc->jmp_cond = TCG_COND_ALWAYS;
107416bbbbc9SRichard Henderson     return true;
107516bbbbc9SRichard Henderson }
107616bbbbc9SRichard Henderson 
107716bbbbc9SRichard Henderson #define DO_BR(NAME, NAMEI, DELAY, ABS, LINK)                               \
107816bbbbc9SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea_br *arg)          \
107916bbbbc9SRichard Henderson     { return do_branch(dc, arg->rb, 0, DELAY, ABS, LINK ? arg->rd : 0); }  \
108016bbbbc9SRichard Henderson     static bool trans_##NAMEI(DisasContext *dc, arg_typeb_br *arg)         \
108116bbbbc9SRichard Henderson     { return do_branch(dc, -1, arg->imm, DELAY, ABS, LINK ? arg->rd : 0); }
108216bbbbc9SRichard Henderson 
108316bbbbc9SRichard Henderson DO_BR(br, bri, false, false, false)
108416bbbbc9SRichard Henderson DO_BR(bra, brai, false, true, false)
108516bbbbc9SRichard Henderson DO_BR(brd, brid, true, false, false)
108616bbbbc9SRichard Henderson DO_BR(brad, braid, true, true, false)
108716bbbbc9SRichard Henderson DO_BR(brld, brlid, true, false, true)
108816bbbbc9SRichard Henderson DO_BR(brald, bralid, true, true, true)
108916bbbbc9SRichard Henderson 
1090fd779113SRichard Henderson static bool do_bcc(DisasContext *dc, int dest_rb, int dest_imm,
1091fd779113SRichard Henderson                    TCGCond cond, int ra, bool delay)
1092fd779113SRichard Henderson {
1093fd779113SRichard Henderson     TCGv_i32 zero, next;
1094fd779113SRichard Henderson 
10952a7567a2SRichard Henderson     if (invalid_delay_slot(dc, "bcc")) {
10962a7567a2SRichard Henderson         return true;
10972a7567a2SRichard Henderson     }
1098fd779113SRichard Henderson     if (delay) {
1099fd779113SRichard Henderson         setup_dslot(dc, dest_rb < 0);
1100fd779113SRichard Henderson     }
1101fd779113SRichard Henderson 
1102fd779113SRichard Henderson     dc->jmp_cond = cond;
1103fd779113SRichard Henderson 
1104fd779113SRichard Henderson     /* Cache the condition register in cpu_bvalue across any delay slot.  */
1105fd779113SRichard Henderson     tcg_gen_mov_i32(cpu_bvalue, reg_for_read(dc, ra));
1106fd779113SRichard Henderson 
1107fd779113SRichard Henderson     /* Store the branch taken destination into btarget.  */
1108fd779113SRichard Henderson     if (dest_rb > 0) {
1109fd779113SRichard Henderson         dc->jmp_dest = -1;
1110fd779113SRichard Henderson         tcg_gen_addi_i32(cpu_btarget, cpu_R[dest_rb], dc->base.pc_next);
1111fd779113SRichard Henderson     } else {
1112fd779113SRichard Henderson         dc->jmp_dest = dc->base.pc_next + dest_imm;
1113fd779113SRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_dest);
1114fd779113SRichard Henderson     }
1115fd779113SRichard Henderson 
1116fd779113SRichard Henderson     /* Compute the final destination into btarget.  */
1117a5ea3dd7SRichard Henderson     zero = tcg_constant_i32(0);
1118a5ea3dd7SRichard Henderson     next = tcg_constant_i32(dc->base.pc_next + (delay + 1) * 4);
1119fd779113SRichard Henderson     tcg_gen_movcond_i32(dc->jmp_cond, cpu_btarget,
1120fd779113SRichard Henderson                         reg_for_read(dc, ra), zero,
1121fd779113SRichard Henderson                         cpu_btarget, next);
1122fd779113SRichard Henderson 
1123fd779113SRichard Henderson     return true;
1124fd779113SRichard Henderson }
1125fd779113SRichard Henderson 
1126fd779113SRichard Henderson #define DO_BCC(NAME, COND)                                              \
1127fd779113SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typea_bc *arg)       \
1128fd779113SRichard Henderson     { return do_bcc(dc, arg->rb, 0, COND, arg->ra, false); }            \
1129fd779113SRichard Henderson     static bool trans_##NAME##d(DisasContext *dc, arg_typea_bc *arg)    \
1130fd779113SRichard Henderson     { return do_bcc(dc, arg->rb, 0, COND, arg->ra, true); }             \
1131fd779113SRichard Henderson     static bool trans_##NAME##i(DisasContext *dc, arg_typeb_bc *arg)    \
1132fd779113SRichard Henderson     { return do_bcc(dc, -1, arg->imm, COND, arg->ra, false); }          \
1133fd779113SRichard Henderson     static bool trans_##NAME##id(DisasContext *dc, arg_typeb_bc *arg)   \
1134fd779113SRichard Henderson     { return do_bcc(dc, -1, arg->imm, COND, arg->ra, true); }
1135fd779113SRichard Henderson 
1136fd779113SRichard Henderson DO_BCC(beq, TCG_COND_EQ)
1137fd779113SRichard Henderson DO_BCC(bge, TCG_COND_GE)
1138fd779113SRichard Henderson DO_BCC(bgt, TCG_COND_GT)
1139fd779113SRichard Henderson DO_BCC(ble, TCG_COND_LE)
1140fd779113SRichard Henderson DO_BCC(blt, TCG_COND_LT)
1141fd779113SRichard Henderson DO_BCC(bne, TCG_COND_NE)
1142fd779113SRichard Henderson 
1143f5235314SRichard Henderson static bool trans_brk(DisasContext *dc, arg_typea_br *arg)
1144f5235314SRichard Henderson {
1145f5235314SRichard Henderson     if (trap_userspace(dc, true)) {
1146f5235314SRichard Henderson         return true;
1147f5235314SRichard Henderson     }
11482a7567a2SRichard Henderson     if (invalid_delay_slot(dc, "brk")) {
11492a7567a2SRichard Henderson         return true;
11502a7567a2SRichard Henderson     }
11512a7567a2SRichard Henderson 
1152f5235314SRichard Henderson     tcg_gen_mov_i32(cpu_pc, reg_for_read(dc, arg->rb));
1153f5235314SRichard Henderson     if (arg->rd) {
1154f5235314SRichard Henderson         tcg_gen_movi_i32(cpu_R[arg->rd], dc->base.pc_next);
1155f5235314SRichard Henderson     }
1156f5235314SRichard Henderson     tcg_gen_ori_i32(cpu_msr, cpu_msr, MSR_BIP);
1157f5235314SRichard Henderson     tcg_gen_movi_tl(cpu_res_addr, -1);
1158f5235314SRichard Henderson 
115917e77796SRichard Henderson     dc->base.is_jmp = DISAS_EXIT;
1160f5235314SRichard Henderson     return true;
1161f5235314SRichard Henderson }
1162f5235314SRichard Henderson 
1163f5235314SRichard Henderson static bool trans_brki(DisasContext *dc, arg_typeb_br *arg)
1164f5235314SRichard Henderson {
1165f5235314SRichard Henderson     uint32_t imm = arg->imm;
1166f5235314SRichard Henderson 
1167f5235314SRichard Henderson     if (trap_userspace(dc, imm != 0x8 && imm != 0x18)) {
1168f5235314SRichard Henderson         return true;
1169f5235314SRichard Henderson     }
11702a7567a2SRichard Henderson     if (invalid_delay_slot(dc, "brki")) {
11712a7567a2SRichard Henderson         return true;
11722a7567a2SRichard Henderson     }
11732a7567a2SRichard Henderson 
1174f5235314SRichard Henderson     tcg_gen_movi_i32(cpu_pc, imm);
1175f5235314SRichard Henderson     if (arg->rd) {
1176f5235314SRichard Henderson         tcg_gen_movi_i32(cpu_R[arg->rd], dc->base.pc_next);
1177f5235314SRichard Henderson     }
1178f5235314SRichard Henderson     tcg_gen_movi_tl(cpu_res_addr, -1);
1179f5235314SRichard Henderson 
1180f5235314SRichard Henderson #ifdef CONFIG_USER_ONLY
1181f5235314SRichard Henderson     switch (imm) {
1182f5235314SRichard Henderson     case 0x8:  /* syscall trap */
1183f5235314SRichard Henderson         gen_raise_exception_sync(dc, EXCP_SYSCALL);
1184f5235314SRichard Henderson         break;
1185f5235314SRichard Henderson     case 0x18: /* debug trap */
1186f5235314SRichard Henderson         gen_raise_exception_sync(dc, EXCP_DEBUG);
1187f5235314SRichard Henderson         break;
1188f5235314SRichard Henderson     default:   /* eliminated with trap_userspace check */
1189f5235314SRichard Henderson         g_assert_not_reached();
1190f5235314SRichard Henderson     }
1191f5235314SRichard Henderson #else
1192f5235314SRichard Henderson     uint32_t msr_to_set = 0;
1193f5235314SRichard Henderson 
1194f5235314SRichard Henderson     if (imm != 0x18) {
1195f5235314SRichard Henderson         msr_to_set |= MSR_BIP;
1196f5235314SRichard Henderson     }
1197f5235314SRichard Henderson     if (imm == 0x8 || imm == 0x18) {
1198f5235314SRichard Henderson         /* MSR_UM and MSR_VM are in tb_flags, so we know their value. */
1199f5235314SRichard Henderson         msr_to_set |= (dc->tb_flags & (MSR_UM | MSR_VM)) << 1;
1200f5235314SRichard Henderson         tcg_gen_andi_i32(cpu_msr, cpu_msr,
1201f5235314SRichard Henderson                          ~(MSR_VMS | MSR_UMS | MSR_VM | MSR_UM));
1202f5235314SRichard Henderson     }
1203f5235314SRichard Henderson     tcg_gen_ori_i32(cpu_msr, cpu_msr, msr_to_set);
120417e77796SRichard Henderson     dc->base.is_jmp = DISAS_EXIT;
1205f5235314SRichard Henderson #endif
1206f5235314SRichard Henderson 
1207f5235314SRichard Henderson     return true;
1208f5235314SRichard Henderson }
1209f5235314SRichard Henderson 
1210ee8c7f9fSRichard Henderson static bool trans_mbar(DisasContext *dc, arg_mbar *arg)
1211ee8c7f9fSRichard Henderson {
1212ee8c7f9fSRichard Henderson     int mbar_imm = arg->imm;
1213ee8c7f9fSRichard Henderson 
12142a7567a2SRichard Henderson     /* Note that mbar is a specialized branch instruction. */
12152a7567a2SRichard Henderson     if (invalid_delay_slot(dc, "mbar")) {
12162a7567a2SRichard Henderson         return true;
12172a7567a2SRichard Henderson     }
12182a7567a2SRichard Henderson 
1219ee8c7f9fSRichard Henderson     /* Data access memory barrier.  */
1220ee8c7f9fSRichard Henderson     if ((mbar_imm & 2) == 0) {
1221ee8c7f9fSRichard Henderson         tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
1222ee8c7f9fSRichard Henderson     }
1223ee8c7f9fSRichard Henderson 
1224ee8c7f9fSRichard Henderson     /* Sleep. */
1225ee8c7f9fSRichard Henderson     if (mbar_imm & 16) {
1226ee8c7f9fSRichard Henderson         if (trap_userspace(dc, true)) {
1227ee8c7f9fSRichard Henderson             /* Sleep is a privileged instruction.  */
1228ee8c7f9fSRichard Henderson             return true;
1229ee8c7f9fSRichard Henderson         }
1230ee8c7f9fSRichard Henderson 
1231ee8c7f9fSRichard Henderson         t_sync_flags(dc);
1232ee8c7f9fSRichard Henderson 
1233ad75a51eSRichard Henderson         tcg_gen_st_i32(tcg_constant_i32(1), tcg_env,
1234ee8c7f9fSRichard Henderson                        -offsetof(MicroBlazeCPU, env)
1235ee8c7f9fSRichard Henderson                        +offsetof(CPUState, halted));
1236ee8c7f9fSRichard Henderson 
1237ee8c7f9fSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next + 4);
1238ee8c7f9fSRichard Henderson 
1239ee8c7f9fSRichard Henderson         gen_raise_exception(dc, EXCP_HLT);
1240ee8c7f9fSRichard Henderson     }
1241ee8c7f9fSRichard Henderson 
1242ee8c7f9fSRichard Henderson     /*
1243ee8c7f9fSRichard Henderson      * If !(mbar_imm & 1), this is an instruction access memory barrier
1244ee8c7f9fSRichard Henderson      * and we need to end the TB so that we recognize self-modified
1245ee8c7f9fSRichard Henderson      * code immediately.
1246ee8c7f9fSRichard Henderson      *
1247ee8c7f9fSRichard Henderson      * However, there are some data mbars that need the TB break
1248ee8c7f9fSRichard Henderson      * (and return to main loop) to recognize interrupts right away.
1249ee8c7f9fSRichard Henderson      * E.g. recognizing a change to an interrupt controller register.
1250ee8c7f9fSRichard Henderson      *
1251ee8c7f9fSRichard Henderson      * Therefore, choose to end the TB always.
1252ee8c7f9fSRichard Henderson      */
125343b34134SRichard Henderson     dc->base.is_jmp = DISAS_EXIT_NEXT;
1254ee8c7f9fSRichard Henderson     return true;
1255ee8c7f9fSRichard Henderson }
1256ee8c7f9fSRichard Henderson 
1257e6cb0354SRichard Henderson static bool do_rts(DisasContext *dc, arg_typeb_bc *arg, int to_set)
1258e6cb0354SRichard Henderson {
1259e6cb0354SRichard Henderson     if (trap_userspace(dc, to_set)) {
1260e6cb0354SRichard Henderson         return true;
1261e6cb0354SRichard Henderson     }
12622a7567a2SRichard Henderson     if (invalid_delay_slot(dc, "rts")) {
12632a7567a2SRichard Henderson         return true;
12642a7567a2SRichard Henderson     }
12652a7567a2SRichard Henderson 
1266e6cb0354SRichard Henderson     dc->tb_flags_to_set |= to_set;
1267e6cb0354SRichard Henderson     setup_dslot(dc, true);
1268e6cb0354SRichard Henderson 
1269e6cb0354SRichard Henderson     dc->jmp_cond = TCG_COND_ALWAYS;
1270e6cb0354SRichard Henderson     dc->jmp_dest = -1;
1271e6cb0354SRichard Henderson     tcg_gen_addi_i32(cpu_btarget, reg_for_read(dc, arg->ra), arg->imm);
1272e6cb0354SRichard Henderson     return true;
1273e6cb0354SRichard Henderson }
1274e6cb0354SRichard Henderson 
1275e6cb0354SRichard Henderson #define DO_RTS(NAME, IFLAG) \
1276e6cb0354SRichard Henderson     static bool trans_##NAME(DisasContext *dc, arg_typeb_bc *arg) \
1277e6cb0354SRichard Henderson     { return do_rts(dc, arg, IFLAG); }
1278e6cb0354SRichard Henderson 
1279e6cb0354SRichard Henderson DO_RTS(rtbd, DRTB_FLAG)
1280e6cb0354SRichard Henderson DO_RTS(rtid, DRTI_FLAG)
1281e6cb0354SRichard Henderson DO_RTS(rted, DRTE_FLAG)
1282e6cb0354SRichard Henderson DO_RTS(rtsd, 0)
1283e6cb0354SRichard Henderson 
128420800179SRichard Henderson static bool trans_zero(DisasContext *dc, arg_zero *arg)
128520800179SRichard Henderson {
128620800179SRichard Henderson     /* If opcode_0_illegal, trap.  */
12874b893631SRichard Henderson     if (dc->cfg->opcode_0_illegal) {
128820800179SRichard Henderson         trap_illegal(dc, true);
128920800179SRichard Henderson         return true;
129020800179SRichard Henderson     }
129120800179SRichard Henderson     /*
129220800179SRichard Henderson      * Otherwise, this is "add r0, r0, r0".
129320800179SRichard Henderson      * Continue to trans_add so that MSR[C] gets cleared.
129420800179SRichard Henderson      */
129520800179SRichard Henderson     return false;
1296fcf5ef2aSThomas Huth }
1297fcf5ef2aSThomas Huth 
12981074c0fbSRichard Henderson static void msr_read(DisasContext *dc, TCGv_i32 d)
1299fcf5ef2aSThomas Huth {
13001074c0fbSRichard Henderson     TCGv_i32 t;
13011074c0fbSRichard Henderson 
13021074c0fbSRichard Henderson     /* Replicate the cpu_msr_c boolean into the proper bit and the copy. */
13031074c0fbSRichard Henderson     t = tcg_temp_new_i32();
13041074c0fbSRichard Henderson     tcg_gen_muli_i32(t, cpu_msr_c, MSR_C | MSR_CC);
13051074c0fbSRichard Henderson     tcg_gen_or_i32(d, cpu_msr, t);
1306fcf5ef2aSThomas Huth }
1307fcf5ef2aSThomas Huth 
1308536e340fSRichard Henderson static bool do_msrclrset(DisasContext *dc, arg_type_msr *arg, bool set)
1309536e340fSRichard Henderson {
1310536e340fSRichard Henderson     uint32_t imm = arg->imm;
1311536e340fSRichard Henderson 
1312536e340fSRichard Henderson     if (trap_userspace(dc, imm != MSR_C)) {
1313536e340fSRichard Henderson         return true;
1314536e340fSRichard Henderson     }
1315536e340fSRichard Henderson 
1316536e340fSRichard Henderson     if (arg->rd) {
1317536e340fSRichard Henderson         msr_read(dc, cpu_R[arg->rd]);
1318536e340fSRichard Henderson     }
1319536e340fSRichard Henderson 
1320536e340fSRichard Henderson     /*
1321536e340fSRichard Henderson      * Handle the carry bit separately.
1322536e340fSRichard Henderson      * This is the only bit that userspace can modify.
1323536e340fSRichard Henderson      */
1324536e340fSRichard Henderson     if (imm & MSR_C) {
1325536e340fSRichard Henderson         tcg_gen_movi_i32(cpu_msr_c, set);
1326536e340fSRichard Henderson     }
1327536e340fSRichard Henderson 
1328536e340fSRichard Henderson     /*
1329536e340fSRichard Henderson      * MSR_C and MSR_CC set above.
1330536e340fSRichard Henderson      * MSR_PVR is not writable, and is always clear.
1331536e340fSRichard Henderson      */
1332536e340fSRichard Henderson     imm &= ~(MSR_C | MSR_CC | MSR_PVR);
1333536e340fSRichard Henderson 
1334536e340fSRichard Henderson     if (imm != 0) {
1335536e340fSRichard Henderson         if (set) {
1336536e340fSRichard Henderson             tcg_gen_ori_i32(cpu_msr, cpu_msr, imm);
1337536e340fSRichard Henderson         } else {
1338536e340fSRichard Henderson             tcg_gen_andi_i32(cpu_msr, cpu_msr, ~imm);
1339536e340fSRichard Henderson         }
134043b34134SRichard Henderson         dc->base.is_jmp = DISAS_EXIT_NEXT;
1341536e340fSRichard Henderson     }
1342536e340fSRichard Henderson     return true;
1343536e340fSRichard Henderson }
1344536e340fSRichard Henderson 
1345536e340fSRichard Henderson static bool trans_msrclr(DisasContext *dc, arg_type_msr *arg)
1346536e340fSRichard Henderson {
1347536e340fSRichard Henderson     return do_msrclrset(dc, arg, false);
1348536e340fSRichard Henderson }
1349536e340fSRichard Henderson 
1350536e340fSRichard Henderson static bool trans_msrset(DisasContext *dc, arg_type_msr *arg)
1351536e340fSRichard Henderson {
1352536e340fSRichard Henderson     return do_msrclrset(dc, arg, true);
1353536e340fSRichard Henderson }
1354536e340fSRichard Henderson 
13559df297a2SRichard Henderson static bool trans_mts(DisasContext *dc, arg_mts *arg)
1356fcf5ef2aSThomas Huth {
13579df297a2SRichard Henderson     if (trap_userspace(dc, true)) {
13589df297a2SRichard Henderson         return true;
1359f0f7e7f7SEdgar E. Iglesias     }
1360f0f7e7f7SEdgar E. Iglesias 
13619df297a2SRichard Henderson #ifdef CONFIG_USER_ONLY
13629df297a2SRichard Henderson     g_assert_not_reached();
13639df297a2SRichard Henderson #else
13649df297a2SRichard Henderson     if (arg->e && arg->rs != 0x1003) {
13659df297a2SRichard Henderson         qemu_log_mask(LOG_GUEST_ERROR,
13669df297a2SRichard Henderson                       "Invalid extended mts reg 0x%x\n", arg->rs);
13679df297a2SRichard Henderson         return true;
13682023e9a3SEdgar E. Iglesias     }
1369fcf5ef2aSThomas Huth 
13709df297a2SRichard Henderson     TCGv_i32 src = reg_for_read(dc, arg->ra);
13719df297a2SRichard Henderson     switch (arg->rs) {
1372aa28e6d4SRichard Henderson     case SR_MSR:
137343b34134SRichard Henderson         /* Install MSR_C.  */
137443b34134SRichard Henderson         tcg_gen_extract_i32(cpu_msr_c, src, 2, 1);
137543b34134SRichard Henderson         /*
137643b34134SRichard Henderson          * Clear MSR_C and MSR_CC;
137743b34134SRichard Henderson          * MSR_PVR is not writable, and is always clear.
137843b34134SRichard Henderson          */
137943b34134SRichard Henderson         tcg_gen_andi_i32(cpu_msr, src, ~(MSR_C | MSR_CC | MSR_PVR));
1380fcf5ef2aSThomas Huth         break;
13819df297a2SRichard Henderson     case SR_FSR:
1382ad75a51eSRichard Henderson         tcg_gen_st_i32(src, tcg_env, offsetof(CPUMBState, fsr));
13839df297a2SRichard Henderson         break;
13849df297a2SRichard Henderson     case 0x800:
1385ad75a51eSRichard Henderson         tcg_gen_st_i32(src, tcg_env, offsetof(CPUMBState, slr));
13869df297a2SRichard Henderson         break;
13879df297a2SRichard Henderson     case 0x802:
1388ad75a51eSRichard Henderson         tcg_gen_st_i32(src, tcg_env, offsetof(CPUMBState, shr));
13899df297a2SRichard Henderson         break;
13909df297a2SRichard Henderson 
13919df297a2SRichard Henderson     case 0x1000: /* PID */
13929df297a2SRichard Henderson     case 0x1001: /* ZPR */
13939df297a2SRichard Henderson     case 0x1002: /* TLBX */
13949df297a2SRichard Henderson     case 0x1003: /* TLBLO */
13959df297a2SRichard Henderson     case 0x1004: /* TLBHI */
13969df297a2SRichard Henderson     case 0x1005: /* TLBSX */
13979df297a2SRichard Henderson         {
1398a5ea3dd7SRichard Henderson             TCGv_i32 tmp_ext = tcg_constant_i32(arg->e);
1399a5ea3dd7SRichard Henderson             TCGv_i32 tmp_reg = tcg_constant_i32(arg->rs & 7);
14009df297a2SRichard Henderson 
1401ad75a51eSRichard Henderson             gen_helper_mmu_write(tcg_env, tmp_ext, tmp_reg, src);
14029df297a2SRichard Henderson         }
14039df297a2SRichard Henderson         break;
14049df297a2SRichard Henderson 
14059df297a2SRichard Henderson     default:
14069df297a2SRichard Henderson         qemu_log_mask(LOG_GUEST_ERROR, "Invalid mts reg 0x%x\n", arg->rs);
14079df297a2SRichard Henderson         return true;
14089df297a2SRichard Henderson     }
140943b34134SRichard Henderson     dc->base.is_jmp = DISAS_EXIT_NEXT;
14109df297a2SRichard Henderson     return true;
14119df297a2SRichard Henderson #endif
14129df297a2SRichard Henderson }
14139df297a2SRichard Henderson 
14149df297a2SRichard Henderson static bool trans_mfs(DisasContext *dc, arg_mfs *arg)
14159df297a2SRichard Henderson {
14169df297a2SRichard Henderson     TCGv_i32 dest = reg_for_write(dc, arg->rd);
14179df297a2SRichard Henderson 
14189df297a2SRichard Henderson     if (arg->e) {
14199df297a2SRichard Henderson         switch (arg->rs) {
1420351527b7SEdgar E. Iglesias         case SR_EAR:
1421dbdb77c4SRichard Henderson             {
1422dbdb77c4SRichard Henderson                 TCGv_i64 t64 = tcg_temp_new_i64();
1423ad75a51eSRichard Henderson                 tcg_gen_ld_i64(t64, tcg_env, offsetof(CPUMBState, ear));
14249df297a2SRichard Henderson                 tcg_gen_extrh_i64_i32(dest, t64);
1425dbdb77c4SRichard Henderson             }
14269df297a2SRichard Henderson             return true;
14279df297a2SRichard Henderson #ifndef CONFIG_USER_ONLY
14289df297a2SRichard Henderson         case 0x1003: /* TLBLO */
14299df297a2SRichard Henderson             /* Handled below. */
1430aa28e6d4SRichard Henderson             break;
14319df297a2SRichard Henderson #endif
14329df297a2SRichard Henderson         case 0x2006 ... 0x2009:
14339df297a2SRichard Henderson             /* High bits of PVR6-9 not implemented. */
14349df297a2SRichard Henderson             tcg_gen_movi_i32(dest, 0);
14359df297a2SRichard Henderson             return true;
1436fcf5ef2aSThomas Huth         default:
14379df297a2SRichard Henderson             qemu_log_mask(LOG_GUEST_ERROR,
14389df297a2SRichard Henderson                           "Invalid extended mfs reg 0x%x\n", arg->rs);
14399df297a2SRichard Henderson             return true;
1440fcf5ef2aSThomas Huth         }
14419df297a2SRichard Henderson     }
14429df297a2SRichard Henderson 
14439df297a2SRichard Henderson     switch (arg->rs) {
1444aa28e6d4SRichard Henderson     case SR_PC:
14459df297a2SRichard Henderson         tcg_gen_movi_i32(dest, dc->base.pc_next);
1446fcf5ef2aSThomas Huth         break;
1447aa28e6d4SRichard Henderson     case SR_MSR:
14489df297a2SRichard Henderson         msr_read(dc, dest);
1449fcf5ef2aSThomas Huth         break;
1450351527b7SEdgar E. Iglesias     case SR_EAR:
1451dbdb77c4SRichard Henderson         {
1452dbdb77c4SRichard Henderson             TCGv_i64 t64 = tcg_temp_new_i64();
1453ad75a51eSRichard Henderson             tcg_gen_ld_i64(t64, tcg_env, offsetof(CPUMBState, ear));
14549df297a2SRichard Henderson             tcg_gen_extrl_i64_i32(dest, t64);
1455a1b48e3aSEdgar E. Iglesias         }
1456aa28e6d4SRichard Henderson         break;
1457351527b7SEdgar E. Iglesias     case SR_ESR:
1458ad75a51eSRichard Henderson         tcg_gen_ld_i32(dest, tcg_env, offsetof(CPUMBState, esr));
1459aa28e6d4SRichard Henderson         break;
1460351527b7SEdgar E. Iglesias     case SR_FSR:
1461ad75a51eSRichard Henderson         tcg_gen_ld_i32(dest, tcg_env, offsetof(CPUMBState, fsr));
1462aa28e6d4SRichard Henderson         break;
1463351527b7SEdgar E. Iglesias     case SR_BTR:
1464ad75a51eSRichard Henderson         tcg_gen_ld_i32(dest, tcg_env, offsetof(CPUMBState, btr));
1465aa28e6d4SRichard Henderson         break;
14667cdae31dSTong Ho     case SR_EDR:
1467ad75a51eSRichard Henderson         tcg_gen_ld_i32(dest, tcg_env, offsetof(CPUMBState, edr));
1468fcf5ef2aSThomas Huth         break;
1469fcf5ef2aSThomas Huth     case 0x800:
1470ad75a51eSRichard Henderson         tcg_gen_ld_i32(dest, tcg_env, offsetof(CPUMBState, slr));
1471fcf5ef2aSThomas Huth         break;
1472fcf5ef2aSThomas Huth     case 0x802:
1473ad75a51eSRichard Henderson         tcg_gen_ld_i32(dest, tcg_env, offsetof(CPUMBState, shr));
1474fcf5ef2aSThomas Huth         break;
14759df297a2SRichard Henderson 
14769df297a2SRichard Henderson #ifndef CONFIG_USER_ONLY
14779df297a2SRichard Henderson     case 0x1000: /* PID */
14789df297a2SRichard Henderson     case 0x1001: /* ZPR */
14799df297a2SRichard Henderson     case 0x1002: /* TLBX */
14809df297a2SRichard Henderson     case 0x1003: /* TLBLO */
14819df297a2SRichard Henderson     case 0x1004: /* TLBHI */
14829df297a2SRichard Henderson     case 0x1005: /* TLBSX */
14839df297a2SRichard Henderson         {
1484a5ea3dd7SRichard Henderson             TCGv_i32 tmp_ext = tcg_constant_i32(arg->e);
1485a5ea3dd7SRichard Henderson             TCGv_i32 tmp_reg = tcg_constant_i32(arg->rs & 7);
14869df297a2SRichard Henderson 
1487ad75a51eSRichard Henderson             gen_helper_mmu_read(dest, tcg_env, tmp_ext, tmp_reg);
14889df297a2SRichard Henderson         }
14899df297a2SRichard Henderson         break;
14909df297a2SRichard Henderson #endif
14919df297a2SRichard Henderson 
1492351527b7SEdgar E. Iglesias     case 0x2000 ... 0x200c:
1493ad75a51eSRichard Henderson         tcg_gen_ld_i32(dest, tcg_env,
1494a4bcfc33SRichard Henderson                        offsetof(MicroBlazeCPU, cfg.pvr_regs[arg->rs - 0x2000])
1495a4bcfc33SRichard Henderson                        - offsetof(MicroBlazeCPU, env));
1496fcf5ef2aSThomas Huth         break;
1497fcf5ef2aSThomas Huth     default:
14989df297a2SRichard Henderson         qemu_log_mask(LOG_GUEST_ERROR, "Invalid mfs reg 0x%x\n", arg->rs);
1499fcf5ef2aSThomas Huth         break;
1500fcf5ef2aSThomas Huth     }
15019df297a2SRichard Henderson     return true;
1502fcf5ef2aSThomas Huth }
1503fcf5ef2aSThomas Huth 
15043fb394fdSRichard Henderson static void do_rti(DisasContext *dc)
1505fcf5ef2aSThomas Huth {
15063fb394fdSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
1507fcf5ef2aSThomas Huth 
15083fb394fdSRichard Henderson     tcg_gen_shri_i32(tmp, cpu_msr, 1);
15093fb394fdSRichard Henderson     tcg_gen_ori_i32(cpu_msr, cpu_msr, MSR_IE);
15103fb394fdSRichard Henderson     tcg_gen_andi_i32(tmp, tmp, MSR_VM | MSR_UM);
15113fb394fdSRichard Henderson     tcg_gen_andi_i32(cpu_msr, cpu_msr, ~(MSR_VM | MSR_UM));
15123fb394fdSRichard Henderson     tcg_gen_or_i32(cpu_msr, cpu_msr, tmp);
1513fcf5ef2aSThomas Huth }
1514fcf5ef2aSThomas Huth 
15153fb394fdSRichard Henderson static void do_rtb(DisasContext *dc)
1516fcf5ef2aSThomas Huth {
15173fb394fdSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
1518fcf5ef2aSThomas Huth 
15193fb394fdSRichard Henderson     tcg_gen_shri_i32(tmp, cpu_msr, 1);
15203fb394fdSRichard Henderson     tcg_gen_andi_i32(cpu_msr, cpu_msr, ~(MSR_VM | MSR_UM | MSR_BIP));
15213fb394fdSRichard Henderson     tcg_gen_andi_i32(tmp, tmp, (MSR_VM | MSR_UM));
15223fb394fdSRichard Henderson     tcg_gen_or_i32(cpu_msr, cpu_msr, tmp);
1523fcf5ef2aSThomas Huth }
1524fcf5ef2aSThomas Huth 
15253fb394fdSRichard Henderson static void do_rte(DisasContext *dc)
1526fcf5ef2aSThomas Huth {
15273fb394fdSRichard Henderson     TCGv_i32 tmp = tcg_temp_new_i32();
1528fcf5ef2aSThomas Huth 
15293fb394fdSRichard Henderson     tcg_gen_shri_i32(tmp, cpu_msr, 1);
15303fb394fdSRichard Henderson     tcg_gen_ori_i32(cpu_msr, cpu_msr, MSR_EE);
15313fb394fdSRichard Henderson     tcg_gen_andi_i32(tmp, tmp, (MSR_VM | MSR_UM));
15323fb394fdSRichard Henderson     tcg_gen_andi_i32(cpu_msr, cpu_msr, ~(MSR_VM | MSR_UM | MSR_EIP));
15333fb394fdSRichard Henderson     tcg_gen_or_i32(cpu_msr, cpu_msr, tmp);
1534fcf5ef2aSThomas Huth }
1535fcf5ef2aSThomas Huth 
1536fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
153752065d8fSRichard Henderson static bool do_get(DisasContext *dc, int rd, int rb, int imm, int ctrl)
1538fcf5ef2aSThomas Huth {
1539fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1540fcf5ef2aSThomas Huth 
1541bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
154252065d8fSRichard Henderson         return true;
1543fcf5ef2aSThomas Huth     }
1544fcf5ef2aSThomas Huth 
1545cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
154652065d8fSRichard Henderson     if (rb) {
154752065d8fSRichard Henderson         tcg_gen_andi_i32(t_id, cpu_R[rb], 0xf);
1548fcf5ef2aSThomas Huth     } else {
154952065d8fSRichard Henderson         tcg_gen_movi_i32(t_id, imm);
1550fcf5ef2aSThomas Huth     }
1551fcf5ef2aSThomas Huth 
1552a5ea3dd7SRichard Henderson     t_ctrl = tcg_constant_i32(ctrl);
155352065d8fSRichard Henderson     gen_helper_get(reg_for_write(dc, rd), t_id, t_ctrl);
155452065d8fSRichard Henderson     return true;
155552065d8fSRichard Henderson }
155652065d8fSRichard Henderson 
155752065d8fSRichard Henderson static bool trans_get(DisasContext *dc, arg_get *arg)
155852065d8fSRichard Henderson {
155952065d8fSRichard Henderson     return do_get(dc, arg->rd, 0, arg->imm, arg->ctrl);
156052065d8fSRichard Henderson }
156152065d8fSRichard Henderson 
156252065d8fSRichard Henderson static bool trans_getd(DisasContext *dc, arg_getd *arg)
156352065d8fSRichard Henderson {
156452065d8fSRichard Henderson     return do_get(dc, arg->rd, arg->rb, 0, arg->ctrl);
156552065d8fSRichard Henderson }
156652065d8fSRichard Henderson 
156752065d8fSRichard Henderson static bool do_put(DisasContext *dc, int ra, int rb, int imm, int ctrl)
156852065d8fSRichard Henderson {
156952065d8fSRichard Henderson     TCGv_i32 t_id, t_ctrl;
157052065d8fSRichard Henderson 
157152065d8fSRichard Henderson     if (trap_userspace(dc, true)) {
157252065d8fSRichard Henderson         return true;
157352065d8fSRichard Henderson     }
157452065d8fSRichard Henderson 
157552065d8fSRichard Henderson     t_id = tcg_temp_new_i32();
157652065d8fSRichard Henderson     if (rb) {
157752065d8fSRichard Henderson         tcg_gen_andi_i32(t_id, cpu_R[rb], 0xf);
157852065d8fSRichard Henderson     } else {
157952065d8fSRichard Henderson         tcg_gen_movi_i32(t_id, imm);
158052065d8fSRichard Henderson     }
158152065d8fSRichard Henderson 
1582a5ea3dd7SRichard Henderson     t_ctrl = tcg_constant_i32(ctrl);
158352065d8fSRichard Henderson     gen_helper_put(t_id, t_ctrl, reg_for_read(dc, ra));
158452065d8fSRichard Henderson     return true;
158552065d8fSRichard Henderson }
158652065d8fSRichard Henderson 
158752065d8fSRichard Henderson static bool trans_put(DisasContext *dc, arg_put *arg)
158852065d8fSRichard Henderson {
158952065d8fSRichard Henderson     return do_put(dc, arg->ra, 0, arg->imm, arg->ctrl);
159052065d8fSRichard Henderson }
159152065d8fSRichard Henderson 
159252065d8fSRichard Henderson static bool trans_putd(DisasContext *dc, arg_putd *arg)
159352065d8fSRichard Henderson {
159452065d8fSRichard Henderson     return do_put(dc, arg->ra, arg->rb, 0, arg->ctrl);
1595fcf5ef2aSThomas Huth }
1596fcf5ef2aSThomas Huth 
1597372122e3SRichard Henderson static void mb_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1598fcf5ef2aSThomas Huth {
1599372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1600372122e3SRichard Henderson     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1601372122e3SRichard Henderson     int bound;
1602fcf5ef2aSThomas Huth 
16034b893631SRichard Henderson     dc->cfg = &cpu->cfg;
1604683a247eSRichard Henderson     dc->tb_flags = dc->base.tb->flags;
1605d7ecb757SRichard Henderson     dc->ext_imm = dc->base.tb->cs_base;
160620800179SRichard Henderson     dc->r0 = NULL;
160720800179SRichard Henderson     dc->r0_set = false;
16083b916140SRichard Henderson     dc->mem_index = cpu_mmu_index(cs, false);
1609b9c58aabSRichard Henderson     dc->jmp_cond = dc->tb_flags & D_FLAG ? TCG_COND_ALWAYS : TCG_COND_NEVER;
1610b9c58aabSRichard Henderson     dc->jmp_dest = -1;
1611fcf5ef2aSThomas Huth 
1612372122e3SRichard Henderson     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1613372122e3SRichard Henderson     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1614fcf5ef2aSThomas Huth }
1615fcf5ef2aSThomas Huth 
1616372122e3SRichard Henderson static void mb_tr_tb_start(DisasContextBase *dcb, CPUState *cs)
1617fcf5ef2aSThomas Huth {
1618fcf5ef2aSThomas Huth }
1619fcf5ef2aSThomas Huth 
1620372122e3SRichard Henderson static void mb_tr_insn_start(DisasContextBase *dcb, CPUState *cs)
1621372122e3SRichard Henderson {
1622683a247eSRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1623683a247eSRichard Henderson 
1624683a247eSRichard Henderson     tcg_gen_insn_start(dc->base.pc_next, dc->tb_flags & ~MSR_TB_MASK);
1625372122e3SRichard Henderson }
1626fcf5ef2aSThomas Huth 
1627372122e3SRichard Henderson static void mb_tr_translate_insn(DisasContextBase *dcb, CPUState *cs)
1628372122e3SRichard Henderson {
1629372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
163044d1432bSRichard Henderson     uint32_t ir;
1631372122e3SRichard Henderson 
1632372122e3SRichard Henderson     /* TODO: This should raise an exception, not terminate qemu. */
1633372122e3SRichard Henderson     if (dc->base.pc_next & 3) {
1634372122e3SRichard Henderson         cpu_abort(cs, "Microblaze: unaligned PC=%x\n",
1635372122e3SRichard Henderson                   (uint32_t)dc->base.pc_next);
1636fcf5ef2aSThomas Huth     }
1637fcf5ef2aSThomas Huth 
16386f9642d7SRichard Henderson     dc->tb_flags_to_set = 0;
16396f9642d7SRichard Henderson 
1640da953643SPhilippe Mathieu-Daudé     ir = cpu_ldl_code(cpu_env(cs), dc->base.pc_next);
164144d1432bSRichard Henderson     if (!decode(dc, ir)) {
1642921afa9dSRichard Henderson         trap_illegal(dc, true);
164344d1432bSRichard Henderson     }
164420800179SRichard Henderson 
164520800179SRichard Henderson     if (dc->r0) {
164620800179SRichard Henderson         dc->r0 = NULL;
164720800179SRichard Henderson         dc->r0_set = false;
164820800179SRichard Henderson     }
164920800179SRichard Henderson 
16506f9642d7SRichard Henderson     /* Discard the imm global when its contents cannot be used. */
16516f9642d7SRichard Henderson     if ((dc->tb_flags & ~dc->tb_flags_to_set) & IMM_FLAG) {
1652d7ecb757SRichard Henderson         tcg_gen_discard_i32(cpu_imm);
1653372122e3SRichard Henderson     }
16546f9642d7SRichard Henderson 
16551e521ce3SRichard Henderson     dc->tb_flags &= ~(IMM_FLAG | BIMM_FLAG | D_FLAG);
16566f9642d7SRichard Henderson     dc->tb_flags |= dc->tb_flags_to_set;
1657d4705ae0SRichard Henderson     dc->base.pc_next += 4;
1658fcf5ef2aSThomas Huth 
1659b9c58aabSRichard Henderson     if (dc->jmp_cond != TCG_COND_NEVER && !(dc->tb_flags & D_FLAG)) {
16603d35bcc2SRichard Henderson         /*
16613d35bcc2SRichard Henderson          * Finish any return-from branch.
16623d35bcc2SRichard Henderson          */
16633c745866SRichard Henderson         uint32_t rt_ibe = dc->tb_flags & (DRTI_FLAG | DRTB_FLAG | DRTE_FLAG);
16643c745866SRichard Henderson         if (unlikely(rt_ibe != 0)) {
16653c745866SRichard Henderson             dc->tb_flags &= ~(DRTI_FLAG | DRTB_FLAG | DRTE_FLAG);
16663c745866SRichard Henderson             if (rt_ibe & DRTI_FLAG) {
1667fcf5ef2aSThomas Huth                 do_rti(dc);
16683c745866SRichard Henderson             } else if (rt_ibe & DRTB_FLAG) {
1669fcf5ef2aSThomas Huth                 do_rtb(dc);
16703c745866SRichard Henderson             } else {
1671fcf5ef2aSThomas Huth                 do_rte(dc);
1672372122e3SRichard Henderson             }
16733c745866SRichard Henderson         }
16743d35bcc2SRichard Henderson 
16753d35bcc2SRichard Henderson         /* Complete the branch, ending the TB. */
16763d35bcc2SRichard Henderson         switch (dc->base.is_jmp) {
16773d35bcc2SRichard Henderson         case DISAS_NORETURN:
16783d35bcc2SRichard Henderson             /*
16793d35bcc2SRichard Henderson              * E.g. illegal insn in a delay slot.  We've already exited
16803d35bcc2SRichard Henderson              * and will handle D_FLAG in mb_cpu_do_interrupt.
16813d35bcc2SRichard Henderson              */
16823d35bcc2SRichard Henderson             break;
16833d35bcc2SRichard Henderson         case DISAS_NEXT:
16843c745866SRichard Henderson             /*
16853c745866SRichard Henderson              * Normal insn a delay slot.
16863c745866SRichard Henderson              * However, the return-from-exception type insns should
16873c745866SRichard Henderson              * return to the main loop, as they have adjusted MSR.
16883c745866SRichard Henderson              */
16893c745866SRichard Henderson             dc->base.is_jmp = (rt_ibe ? DISAS_EXIT_JUMP : DISAS_JUMP);
16903d35bcc2SRichard Henderson             break;
16913d35bcc2SRichard Henderson         case DISAS_EXIT_NEXT:
16923d35bcc2SRichard Henderson             /*
16933d35bcc2SRichard Henderson              * E.g. mts insn in a delay slot.  Continue with btarget,
16943d35bcc2SRichard Henderson              * but still return to the main loop.
16953d35bcc2SRichard Henderson              */
16963d35bcc2SRichard Henderson             dc->base.is_jmp = DISAS_EXIT_JUMP;
16973d35bcc2SRichard Henderson             break;
16983d35bcc2SRichard Henderson         default:
16993d35bcc2SRichard Henderson             g_assert_not_reached();
17003d35bcc2SRichard Henderson         }
1701372122e3SRichard Henderson     }
1702372122e3SRichard Henderson }
1703372122e3SRichard Henderson 
1704372122e3SRichard Henderson static void mb_tr_tb_stop(DisasContextBase *dcb, CPUState *cs)
1705372122e3SRichard Henderson {
1706372122e3SRichard Henderson     DisasContext *dc = container_of(dcb, DisasContext, base);
1707372122e3SRichard Henderson 
1708372122e3SRichard Henderson     if (dc->base.is_jmp == DISAS_NORETURN) {
1709372122e3SRichard Henderson         /* We have already exited the TB. */
1710372122e3SRichard Henderson         return;
1711372122e3SRichard Henderson     }
1712372122e3SRichard Henderson 
1713372122e3SRichard Henderson     t_sync_flags(dc);
1714372122e3SRichard Henderson 
1715372122e3SRichard Henderson     switch (dc->base.is_jmp) {
1716372122e3SRichard Henderson     case DISAS_TOO_MANY:
1717372122e3SRichard Henderson         gen_goto_tb(dc, 0, dc->base.pc_next);
1718372122e3SRichard Henderson         return;
1719372122e3SRichard Henderson 
172017e77796SRichard Henderson     case DISAS_EXIT:
1721f6278ca9SRichard Henderson         break;
1722f6278ca9SRichard Henderson     case DISAS_EXIT_NEXT:
1723f6278ca9SRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->base.pc_next);
1724f6278ca9SRichard Henderson         break;
1725f6278ca9SRichard Henderson     case DISAS_EXIT_JUMP:
1726f6278ca9SRichard Henderson         tcg_gen_mov_i32(cpu_pc, cpu_btarget);
1727f6278ca9SRichard Henderson         tcg_gen_discard_i32(cpu_btarget);
1728f6278ca9SRichard Henderson         break;
1729372122e3SRichard Henderson 
1730372122e3SRichard Henderson     case DISAS_JUMP:
1731fbafb3a4SRichard Henderson         if (dc->jmp_dest != -1 && !(tb_cflags(dc->base.tb) & CF_NO_GOTO_TB)) {
1732b9c58aabSRichard Henderson             /* Direct jump. */
1733b9c58aabSRichard Henderson             tcg_gen_discard_i32(cpu_btarget);
1734b9c58aabSRichard Henderson 
1735b9c58aabSRichard Henderson             if (dc->jmp_cond != TCG_COND_ALWAYS) {
1736b9c58aabSRichard Henderson                 /* Conditional direct jump. */
1737b9c58aabSRichard Henderson                 TCGLabel *taken = gen_new_label();
1738b9c58aabSRichard Henderson                 TCGv_i32 tmp = tcg_temp_new_i32();
1739b9c58aabSRichard Henderson 
1740b9c58aabSRichard Henderson                 /*
1741b9c58aabSRichard Henderson                  * Copy bvalue to a temp now, so we can discard bvalue.
1742b9c58aabSRichard Henderson                  * This can avoid writing bvalue to memory when the
1743b9c58aabSRichard Henderson                  * delay slot cannot raise an exception.
1744b9c58aabSRichard Henderson                  */
1745b9c58aabSRichard Henderson                 tcg_gen_mov_i32(tmp, cpu_bvalue);
1746b9c58aabSRichard Henderson                 tcg_gen_discard_i32(cpu_bvalue);
1747b9c58aabSRichard Henderson 
1748b9c58aabSRichard Henderson                 tcg_gen_brcondi_i32(dc->jmp_cond, tmp, 0, taken);
1749b9c58aabSRichard Henderson                 gen_goto_tb(dc, 1, dc->base.pc_next);
1750b9c58aabSRichard Henderson                 gen_set_label(taken);
1751b9c58aabSRichard Henderson             }
1752b9c58aabSRichard Henderson             gen_goto_tb(dc, 0, dc->jmp_dest);
1753b9c58aabSRichard Henderson             return;
1754b9c58aabSRichard Henderson         }
1755b9c58aabSRichard Henderson 
1756fbafb3a4SRichard Henderson         /* Indirect jump (or direct jump w/ goto_tb disabled) */
1757b9c58aabSRichard Henderson         tcg_gen_mov_i32(cpu_pc, cpu_btarget);
1758b9c58aabSRichard Henderson         tcg_gen_discard_i32(cpu_btarget);
17594059bd90SRichard Henderson         tcg_gen_lookup_and_goto_ptr();
1760372122e3SRichard Henderson         return;
1761372122e3SRichard Henderson 
1762a2b80dbdSRichard Henderson     default:
1763a2b80dbdSRichard Henderson         g_assert_not_reached();
1764fcf5ef2aSThomas Huth     }
1765f6278ca9SRichard Henderson 
1766f6278ca9SRichard Henderson     /* Finish DISAS_EXIT_* */
1767f6278ca9SRichard Henderson     if (unlikely(cs->singlestep_enabled)) {
1768f6278ca9SRichard Henderson         gen_raise_exception(dc, EXCP_DEBUG);
1769f6278ca9SRichard Henderson     } else {
1770f6278ca9SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
1771f6278ca9SRichard Henderson     }
1772fcf5ef2aSThomas Huth }
1773fcf5ef2aSThomas Huth 
17748eb806a7SRichard Henderson static void mb_tr_disas_log(const DisasContextBase *dcb,
17758eb806a7SRichard Henderson                             CPUState *cs, FILE *logfile)
1776372122e3SRichard Henderson {
17778eb806a7SRichard Henderson     fprintf(logfile, "IN: %s\n", lookup_symbol(dcb->pc_first));
17788eb806a7SRichard Henderson     target_disas(logfile, cs, dcb->pc_first, dcb->tb->size);
1779fcf5ef2aSThomas Huth }
1780372122e3SRichard Henderson 
1781372122e3SRichard Henderson static const TranslatorOps mb_tr_ops = {
1782372122e3SRichard Henderson     .init_disas_context = mb_tr_init_disas_context,
1783372122e3SRichard Henderson     .tb_start           = mb_tr_tb_start,
1784372122e3SRichard Henderson     .insn_start         = mb_tr_insn_start,
1785372122e3SRichard Henderson     .translate_insn     = mb_tr_translate_insn,
1786372122e3SRichard Henderson     .tb_stop            = mb_tr_tb_stop,
1787372122e3SRichard Henderson     .disas_log          = mb_tr_disas_log,
1788372122e3SRichard Henderson };
1789372122e3SRichard Henderson 
1790597f9b2dSRichard Henderson void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb, int *max_insns,
179132f0c394SAnton Johansson                            vaddr pc, void *host_pc)
1792372122e3SRichard Henderson {
1793372122e3SRichard Henderson     DisasContext dc;
1794306c8721SRichard Henderson     translator_loop(cpu, tb, max_insns, pc, host_pc, &mb_tr_ops, &dc.base);
1795fcf5ef2aSThomas Huth }
1796fcf5ef2aSThomas Huth 
179790c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1798fcf5ef2aSThomas Huth {
1799da953643SPhilippe Mathieu-Daudé     CPUMBState *env = cpu_env(cs);
18000c3da918SRichard Henderson     uint32_t iflags;
1801fcf5ef2aSThomas Huth     int i;
1802fcf5ef2aSThomas Huth 
18030c3da918SRichard Henderson     qemu_fprintf(f, "pc=0x%08x msr=0x%05x mode=%s(saved=%s) eip=%d ie=%d\n",
18040c3da918SRichard Henderson                  env->pc, env->msr,
18052e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
18062e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
18072e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
18082e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
18090c3da918SRichard Henderson 
18100c3da918SRichard Henderson     iflags = env->iflags;
18110c3da918SRichard Henderson     qemu_fprintf(f, "iflags: 0x%08x", iflags);
18120c3da918SRichard Henderson     if (iflags & IMM_FLAG) {
18130c3da918SRichard Henderson         qemu_fprintf(f, " IMM(0x%08x)", env->imm);
18142ead1b18SJoe Komlodi     }
18150c3da918SRichard Henderson     if (iflags & BIMM_FLAG) {
18160c3da918SRichard Henderson         qemu_fprintf(f, " BIMM");
18170c3da918SRichard Henderson     }
18180c3da918SRichard Henderson     if (iflags & D_FLAG) {
1819b9c58aabSRichard Henderson         qemu_fprintf(f, " D(btarget=0x%08x)", env->btarget);
18200c3da918SRichard Henderson     }
18210c3da918SRichard Henderson     if (iflags & DRTI_FLAG) {
18220c3da918SRichard Henderson         qemu_fprintf(f, " DRTI");
18230c3da918SRichard Henderson     }
18240c3da918SRichard Henderson     if (iflags & DRTE_FLAG) {
18250c3da918SRichard Henderson         qemu_fprintf(f, " DRTE");
18260c3da918SRichard Henderson     }
18270c3da918SRichard Henderson     if (iflags & DRTB_FLAG) {
18280c3da918SRichard Henderson         qemu_fprintf(f, " DRTB");
18290c3da918SRichard Henderson     }
18300c3da918SRichard Henderson     if (iflags & ESR_ESS_FLAG) {
18310c3da918SRichard Henderson         qemu_fprintf(f, " ESR_ESS(0x%04x)", iflags & ESR_ESS_MASK);
18322ead1b18SJoe Komlodi     }
1833fcf5ef2aSThomas Huth 
18340c3da918SRichard Henderson     qemu_fprintf(f, "\nesr=0x%04x fsr=0x%02x btr=0x%08x edr=0x%x\n"
183519f27b6cSRichard Henderson                  "ear=0x" TARGET_FMT_lx " slr=0x%x shr=0x%x\n",
18360c3da918SRichard Henderson                  env->esr, env->fsr, env->btr, env->edr,
18370c3da918SRichard Henderson                  env->ear, env->slr, env->shr);
18380c3da918SRichard Henderson 
18390c3da918SRichard Henderson     for (i = 0; i < 32; i++) {
18400c3da918SRichard Henderson         qemu_fprintf(f, "r%2.2d=%08x%c",
18410c3da918SRichard Henderson                      i, env->regs[i], i % 4 == 3 ? '\n' : ' ');
18420c3da918SRichard Henderson     }
18430c3da918SRichard Henderson     qemu_fprintf(f, "\n");
1844fcf5ef2aSThomas Huth }
1845fcf5ef2aSThomas Huth 
1846fcf5ef2aSThomas Huth void mb_tcg_init(void)
1847fcf5ef2aSThomas Huth {
1848480d29a8SRichard Henderson #define R(X)  { &cpu_R[X], offsetof(CPUMBState, regs[X]), "r" #X }
1849480d29a8SRichard Henderson #define SP(X) { &cpu_##X, offsetof(CPUMBState, X), #X }
1850fcf5ef2aSThomas Huth 
1851480d29a8SRichard Henderson     static const struct {
1852480d29a8SRichard Henderson         TCGv_i32 *var; int ofs; char name[8];
1853480d29a8SRichard Henderson     } i32s[] = {
1854e47c2231SRichard Henderson         /*
1855e47c2231SRichard Henderson          * Note that r0 is handled specially in reg_for_read
1856e47c2231SRichard Henderson          * and reg_for_write.  Nothing should touch cpu_R[0].
1857e47c2231SRichard Henderson          * Leave that element NULL, which will assert quickly
1858e47c2231SRichard Henderson          * inside the tcg generator functions.
1859e47c2231SRichard Henderson          */
1860e47c2231SRichard Henderson                R(1),  R(2),  R(3),  R(4),  R(5),  R(6),  R(7),
1861480d29a8SRichard Henderson         R(8),  R(9),  R(10), R(11), R(12), R(13), R(14), R(15),
1862480d29a8SRichard Henderson         R(16), R(17), R(18), R(19), R(20), R(21), R(22), R(23),
1863480d29a8SRichard Henderson         R(24), R(25), R(26), R(27), R(28), R(29), R(30), R(31),
1864480d29a8SRichard Henderson 
1865480d29a8SRichard Henderson         SP(pc),
1866480d29a8SRichard Henderson         SP(msr),
18671074c0fbSRichard Henderson         SP(msr_c),
1868480d29a8SRichard Henderson         SP(imm),
1869480d29a8SRichard Henderson         SP(iflags),
1870b9c58aabSRichard Henderson         SP(bvalue),
1871480d29a8SRichard Henderson         SP(btarget),
1872480d29a8SRichard Henderson         SP(res_val),
1873480d29a8SRichard Henderson     };
1874480d29a8SRichard Henderson 
1875480d29a8SRichard Henderson #undef R
1876480d29a8SRichard Henderson #undef SP
1877480d29a8SRichard Henderson 
1878480d29a8SRichard Henderson     for (int i = 0; i < ARRAY_SIZE(i32s); ++i) {
1879480d29a8SRichard Henderson         *i32s[i].var =
1880ad75a51eSRichard Henderson           tcg_global_mem_new_i32(tcg_env, i32s[i].ofs, i32s[i].name);
1881fcf5ef2aSThomas Huth     }
188276e8187dSRichard Henderson 
1883480d29a8SRichard Henderson     cpu_res_addr =
1884ad75a51eSRichard Henderson         tcg_global_mem_new(tcg_env, offsetof(CPUMBState, res_addr), "res_addr");
1885fcf5ef2aSThomas Huth }
1886