xref: /openbmc/qemu/target/microblaze/translate.c (revision 9b1585589dd4f9a1357f49d233039db6c4788bf3)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  *  Xilinx MicroBlaze emulation for qemu: main translation routines.
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2009 Edgar E. Iglesias.
5fcf5ef2aSThomas Huth  *  Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
10fcf5ef2aSThomas Huth  * version 2 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "disas/disas.h"
24fcf5ef2aSThomas Huth #include "exec/exec-all.h"
25dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg-op.h"
26fcf5ef2aSThomas Huth #include "exec/helper-proto.h"
27fcf5ef2aSThomas Huth #include "microblaze-decode.h"
28fcf5ef2aSThomas Huth #include "exec/cpu_ldst.h"
29fcf5ef2aSThomas Huth #include "exec/helper-gen.h"
3077fc6f5eSLluís Vilanova #include "exec/translator.h"
3190c84c56SMarkus Armbruster #include "qemu/qemu-print.h"
32fcf5ef2aSThomas Huth 
33fcf5ef2aSThomas Huth #include "trace-tcg.h"
34fcf5ef2aSThomas Huth #include "exec/log.h"
35fcf5ef2aSThomas Huth 
36fcf5ef2aSThomas Huth 
37fcf5ef2aSThomas Huth #define SIM_COMPAT 0
38fcf5ef2aSThomas Huth #define DISAS_GNU 1
39fcf5ef2aSThomas Huth #define DISAS_MB 1
40fcf5ef2aSThomas Huth #if DISAS_MB && !SIM_COMPAT
41fcf5ef2aSThomas Huth #  define LOG_DIS(...) qemu_log_mask(CPU_LOG_TB_IN_ASM, ## __VA_ARGS__)
42fcf5ef2aSThomas Huth #else
43fcf5ef2aSThomas Huth #  define LOG_DIS(...) do { } while (0)
44fcf5ef2aSThomas Huth #endif
45fcf5ef2aSThomas Huth 
46fcf5ef2aSThomas Huth #define D(x)
47fcf5ef2aSThomas Huth 
48fcf5ef2aSThomas Huth #define EXTRACT_FIELD(src, start, end) \
49fcf5ef2aSThomas Huth             (((src) >> start) & ((1 << (end - start + 1)) - 1))
50fcf5ef2aSThomas Huth 
5177fc6f5eSLluís Vilanova /* is_jmp field values */
5277fc6f5eSLluís Vilanova #define DISAS_JUMP    DISAS_TARGET_0 /* only pc was modified dynamically */
5377fc6f5eSLluís Vilanova #define DISAS_UPDATE  DISAS_TARGET_1 /* cpu state was modified dynamically */
5477fc6f5eSLluís Vilanova #define DISAS_TB_JUMP DISAS_TARGET_2 /* only pc was modified statically */
5577fc6f5eSLluís Vilanova 
56cfeea807SEdgar E. Iglesias static TCGv_i32 cpu_R[32];
570f96e96bSRichard Henderson static TCGv_i32 cpu_pc;
583e0e16aeSRichard Henderson static TCGv_i32 cpu_msr;
59*9b158558SRichard Henderson static TCGv_i32 cpu_imm;
60*9b158558SRichard Henderson static TCGv_i32 cpu_btaken;
610f96e96bSRichard Henderson static TCGv_i32 cpu_btarget;
62*9b158558SRichard Henderson static TCGv_i32 cpu_iflags;
63*9b158558SRichard Henderson static TCGv cpu_res_addr;
64*9b158558SRichard Henderson static TCGv_i32 cpu_res_val;
65fcf5ef2aSThomas Huth 
66fcf5ef2aSThomas Huth #include "exec/gen-icount.h"
67fcf5ef2aSThomas Huth 
68fcf5ef2aSThomas Huth /* This is the state at translation time.  */
69fcf5ef2aSThomas Huth typedef struct DisasContext {
70fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu;
71cfeea807SEdgar E. Iglesias     uint32_t pc;
72fcf5ef2aSThomas Huth 
73fcf5ef2aSThomas Huth     /* Decoder.  */
74fcf5ef2aSThomas Huth     int type_b;
75fcf5ef2aSThomas Huth     uint32_t ir;
76fcf5ef2aSThomas Huth     uint8_t opcode;
77fcf5ef2aSThomas Huth     uint8_t rd, ra, rb;
78fcf5ef2aSThomas Huth     uint16_t imm;
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     unsigned int cpustate_changed;
81fcf5ef2aSThomas Huth     unsigned int delayed_branch;
82fcf5ef2aSThomas Huth     unsigned int tb_flags, synced_flags; /* tb dependent flags.  */
83fcf5ef2aSThomas Huth     unsigned int clear_imm;
84fcf5ef2aSThomas Huth     int is_jmp;
85fcf5ef2aSThomas Huth 
86fcf5ef2aSThomas Huth #define JMP_NOJMP     0
87fcf5ef2aSThomas Huth #define JMP_DIRECT    1
88fcf5ef2aSThomas Huth #define JMP_DIRECT_CC 2
89fcf5ef2aSThomas Huth #define JMP_INDIRECT  3
90fcf5ef2aSThomas Huth     unsigned int jmp;
91fcf5ef2aSThomas Huth     uint32_t jmp_pc;
92fcf5ef2aSThomas Huth 
93fcf5ef2aSThomas Huth     int abort_at_next_insn;
94fcf5ef2aSThomas Huth     struct TranslationBlock *tb;
95fcf5ef2aSThomas Huth     int singlestep_enabled;
96fcf5ef2aSThomas Huth } DisasContext;
97fcf5ef2aSThomas Huth 
98fcf5ef2aSThomas Huth static const char *regnames[] =
99fcf5ef2aSThomas Huth {
100fcf5ef2aSThomas Huth     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
101fcf5ef2aSThomas Huth     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
102fcf5ef2aSThomas Huth     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
103fcf5ef2aSThomas Huth     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
104fcf5ef2aSThomas Huth };
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth static inline void t_sync_flags(DisasContext *dc)
107fcf5ef2aSThomas Huth {
108fcf5ef2aSThomas Huth     /* Synch the tb dependent flags between translator and runtime.  */
109fcf5ef2aSThomas Huth     if (dc->tb_flags != dc->synced_flags) {
110*9b158558SRichard Henderson         tcg_gen_movi_i32(cpu_iflags, dc->tb_flags);
111fcf5ef2aSThomas Huth         dc->synced_flags = dc->tb_flags;
112fcf5ef2aSThomas Huth     }
113fcf5ef2aSThomas Huth }
114fcf5ef2aSThomas Huth 
11541ba37c4SRichard Henderson static void gen_raise_exception(DisasContext *dc, uint32_t index)
116fcf5ef2aSThomas Huth {
117fcf5ef2aSThomas Huth     TCGv_i32 tmp = tcg_const_i32(index);
118fcf5ef2aSThomas Huth 
119fcf5ef2aSThomas Huth     gen_helper_raise_exception(cpu_env, tmp);
120fcf5ef2aSThomas Huth     tcg_temp_free_i32(tmp);
121fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_UPDATE;
122fcf5ef2aSThomas Huth }
123fcf5ef2aSThomas Huth 
12441ba37c4SRichard Henderson static void gen_raise_exception_sync(DisasContext *dc, uint32_t index)
12541ba37c4SRichard Henderson {
12641ba37c4SRichard Henderson     t_sync_flags(dc);
12741ba37c4SRichard Henderson     tcg_gen_movi_i32(cpu_pc, dc->pc);
12841ba37c4SRichard Henderson     gen_raise_exception(dc, index);
12941ba37c4SRichard Henderson }
13041ba37c4SRichard Henderson 
13141ba37c4SRichard Henderson static void gen_raise_hw_excp(DisasContext *dc, uint32_t esr_ec)
13241ba37c4SRichard Henderson {
13341ba37c4SRichard Henderson     TCGv_i32 tmp = tcg_const_i32(esr_ec);
13441ba37c4SRichard Henderson     tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, esr));
13541ba37c4SRichard Henderson     tcg_temp_free_i32(tmp);
13641ba37c4SRichard Henderson 
13741ba37c4SRichard Henderson     gen_raise_exception_sync(dc, EXCP_HW_EXCP);
13841ba37c4SRichard Henderson }
13941ba37c4SRichard Henderson 
140fcf5ef2aSThomas Huth static inline bool use_goto_tb(DisasContext *dc, target_ulong dest)
141fcf5ef2aSThomas Huth {
142fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
143fcf5ef2aSThomas Huth     return (dc->tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
144fcf5ef2aSThomas Huth #else
145fcf5ef2aSThomas Huth     return true;
146fcf5ef2aSThomas Huth #endif
147fcf5ef2aSThomas Huth }
148fcf5ef2aSThomas Huth 
149fcf5ef2aSThomas Huth static void gen_goto_tb(DisasContext *dc, int n, target_ulong dest)
150fcf5ef2aSThomas Huth {
151fcf5ef2aSThomas Huth     if (use_goto_tb(dc, dest)) {
152fcf5ef2aSThomas Huth         tcg_gen_goto_tb(n);
1530f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
15407ea28b4SRichard Henderson         tcg_gen_exit_tb(dc->tb, n);
155fcf5ef2aSThomas Huth     } else {
1560f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dest);
15707ea28b4SRichard Henderson         tcg_gen_exit_tb(NULL, 0);
158fcf5ef2aSThomas Huth     }
159fcf5ef2aSThomas Huth }
160fcf5ef2aSThomas Huth 
161cfeea807SEdgar E. Iglesias static void read_carry(DisasContext *dc, TCGv_i32 d)
162fcf5ef2aSThomas Huth {
1633e0e16aeSRichard Henderson     tcg_gen_shri_i32(d, cpu_msr, 31);
164fcf5ef2aSThomas Huth }
165fcf5ef2aSThomas Huth 
166fcf5ef2aSThomas Huth /*
167fcf5ef2aSThomas Huth  * write_carry sets the carry bits in MSR based on bit 0 of v.
168fcf5ef2aSThomas Huth  * v[31:1] are ignored.
169fcf5ef2aSThomas Huth  */
170cfeea807SEdgar E. Iglesias static void write_carry(DisasContext *dc, TCGv_i32 v)
171fcf5ef2aSThomas Huth {
1720a22f8cfSEdgar E. Iglesias     /* Deposit bit 0 into MSR_C and the alias MSR_CC.  */
1733e0e16aeSRichard Henderson     tcg_gen_deposit_i32(cpu_msr, cpu_msr, v, 2, 1);
1743e0e16aeSRichard Henderson     tcg_gen_deposit_i32(cpu_msr, cpu_msr, v, 31, 1);
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth 
177fcf5ef2aSThomas Huth static void write_carryi(DisasContext *dc, bool carry)
178fcf5ef2aSThomas Huth {
179cfeea807SEdgar E. Iglesias     TCGv_i32 t0 = tcg_temp_new_i32();
180cfeea807SEdgar E. Iglesias     tcg_gen_movi_i32(t0, carry);
181fcf5ef2aSThomas Huth     write_carry(dc, t0);
182cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
183fcf5ef2aSThomas Huth }
184fcf5ef2aSThomas Huth 
185bdfc1e88SEdgar E. Iglesias /*
1869ba8cd45SEdgar E. Iglesias  * Returns true if the insn an illegal operation.
1879ba8cd45SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
1889ba8cd45SEdgar E. Iglesias  */
1899ba8cd45SEdgar E. Iglesias static bool trap_illegal(DisasContext *dc, bool cond)
1909ba8cd45SEdgar E. Iglesias {
1919ba8cd45SEdgar E. Iglesias     if (cond && (dc->tb_flags & MSR_EE_FLAG)
1925143fdf3SEdgar E. Iglesias         && dc->cpu->cfg.illegal_opcode_exception) {
19341ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_ILLEGAL_OP);
1949ba8cd45SEdgar E. Iglesias     }
1959ba8cd45SEdgar E. Iglesias     return cond;
1969ba8cd45SEdgar E. Iglesias }
1979ba8cd45SEdgar E. Iglesias 
1989ba8cd45SEdgar E. Iglesias /*
199bdfc1e88SEdgar E. Iglesias  * Returns true if the insn is illegal in userspace.
200bdfc1e88SEdgar E. Iglesias  * If exceptions are enabled, an exception is raised.
201bdfc1e88SEdgar E. Iglesias  */
202bdfc1e88SEdgar E. Iglesias static bool trap_userspace(DisasContext *dc, bool cond)
203bdfc1e88SEdgar E. Iglesias {
204bdfc1e88SEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
205bdfc1e88SEdgar E. Iglesias     bool cond_user = cond && mem_index == MMU_USER_IDX;
206bdfc1e88SEdgar E. Iglesias 
207bdfc1e88SEdgar E. Iglesias     if (cond_user && (dc->tb_flags & MSR_EE_FLAG)) {
20841ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_PRIVINSN);
209bdfc1e88SEdgar E. Iglesias     }
210bdfc1e88SEdgar E. Iglesias     return cond_user;
211bdfc1e88SEdgar E. Iglesias }
212bdfc1e88SEdgar E. Iglesias 
213fcf5ef2aSThomas Huth /* True if ALU operand b is a small immediate that may deserve
214fcf5ef2aSThomas Huth    faster treatment.  */
215fcf5ef2aSThomas Huth static inline int dec_alu_op_b_is_small_imm(DisasContext *dc)
216fcf5ef2aSThomas Huth {
217fcf5ef2aSThomas Huth     /* Immediate insn without the imm prefix ?  */
218fcf5ef2aSThomas Huth     return dc->type_b && !(dc->tb_flags & IMM_FLAG);
219fcf5ef2aSThomas Huth }
220fcf5ef2aSThomas Huth 
221cfeea807SEdgar E. Iglesias static inline TCGv_i32 *dec_alu_op_b(DisasContext *dc)
222fcf5ef2aSThomas Huth {
223fcf5ef2aSThomas Huth     if (dc->type_b) {
224fcf5ef2aSThomas Huth         if (dc->tb_flags & IMM_FLAG)
225*9b158558SRichard Henderson             tcg_gen_ori_i32(cpu_imm, cpu_imm, dc->imm);
226fcf5ef2aSThomas Huth         else
227*9b158558SRichard Henderson             tcg_gen_movi_i32(cpu_imm, (int32_t)((int16_t)dc->imm));
228*9b158558SRichard Henderson         return &cpu_imm;
229fcf5ef2aSThomas Huth     } else
230fcf5ef2aSThomas Huth         return &cpu_R[dc->rb];
231fcf5ef2aSThomas Huth }
232fcf5ef2aSThomas Huth 
233fcf5ef2aSThomas Huth static void dec_add(DisasContext *dc)
234fcf5ef2aSThomas Huth {
235fcf5ef2aSThomas Huth     unsigned int k, c;
236cfeea807SEdgar E. Iglesias     TCGv_i32 cf;
237fcf5ef2aSThomas Huth 
238fcf5ef2aSThomas Huth     k = dc->opcode & 4;
239fcf5ef2aSThomas Huth     c = dc->opcode & 2;
240fcf5ef2aSThomas Huth 
241fcf5ef2aSThomas Huth     LOG_DIS("add%s%s%s r%d r%d r%d\n",
242fcf5ef2aSThomas Huth             dc->type_b ? "i" : "", k ? "k" : "", c ? "c" : "",
243fcf5ef2aSThomas Huth             dc->rd, dc->ra, dc->rb);
244fcf5ef2aSThomas Huth 
245fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
246fcf5ef2aSThomas Huth     if (k) {
247fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
248fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
249fcf5ef2aSThomas Huth         if (dc->rd) {
250cfeea807SEdgar E. Iglesias             tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
251fcf5ef2aSThomas Huth 
252fcf5ef2aSThomas Huth             if (c) {
253fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
254cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
255fcf5ef2aSThomas Huth 
256fcf5ef2aSThomas Huth                 read_carry(dc, cf);
257cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
258cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
259fcf5ef2aSThomas Huth             }
260fcf5ef2aSThomas Huth         }
261fcf5ef2aSThomas Huth         return;
262fcf5ef2aSThomas Huth     }
263fcf5ef2aSThomas Huth 
264fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
265fcf5ef2aSThomas Huth     /* Extract carry.  */
266cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
267fcf5ef2aSThomas Huth     if (c) {
268fcf5ef2aSThomas Huth         read_carry(dc, cf);
269fcf5ef2aSThomas Huth     } else {
270cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 0);
271fcf5ef2aSThomas Huth     }
272fcf5ef2aSThomas Huth 
273fcf5ef2aSThomas Huth     if (dc->rd) {
274cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
275fcf5ef2aSThomas Huth         gen_helper_carry(ncf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
276cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
277cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
278fcf5ef2aSThomas Huth         write_carry(dc, ncf);
279cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
280fcf5ef2aSThomas Huth     } else {
281fcf5ef2aSThomas Huth         gen_helper_carry(cf, cpu_R[dc->ra], *(dec_alu_op_b(dc)), cf);
282fcf5ef2aSThomas Huth         write_carry(dc, cf);
283fcf5ef2aSThomas Huth     }
284cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
285fcf5ef2aSThomas Huth }
286fcf5ef2aSThomas Huth 
287fcf5ef2aSThomas Huth static void dec_sub(DisasContext *dc)
288fcf5ef2aSThomas Huth {
289fcf5ef2aSThomas Huth     unsigned int u, cmp, k, c;
290cfeea807SEdgar E. Iglesias     TCGv_i32 cf, na;
291fcf5ef2aSThomas Huth 
292fcf5ef2aSThomas Huth     u = dc->imm & 2;
293fcf5ef2aSThomas Huth     k = dc->opcode & 4;
294fcf5ef2aSThomas Huth     c = dc->opcode & 2;
295fcf5ef2aSThomas Huth     cmp = (dc->imm & 1) && (!dc->type_b) && k;
296fcf5ef2aSThomas Huth 
297fcf5ef2aSThomas Huth     if (cmp) {
298fcf5ef2aSThomas Huth         LOG_DIS("cmp%s r%d, r%d ir=%x\n", u ? "u" : "", dc->rd, dc->ra, dc->ir);
299fcf5ef2aSThomas Huth         if (dc->rd) {
300fcf5ef2aSThomas Huth             if (u)
301fcf5ef2aSThomas Huth                 gen_helper_cmpu(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
302fcf5ef2aSThomas Huth             else
303fcf5ef2aSThomas Huth                 gen_helper_cmp(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
304fcf5ef2aSThomas Huth         }
305fcf5ef2aSThomas Huth         return;
306fcf5ef2aSThomas Huth     }
307fcf5ef2aSThomas Huth 
308fcf5ef2aSThomas Huth     LOG_DIS("sub%s%s r%d, r%d r%d\n",
309fcf5ef2aSThomas Huth              k ? "k" : "",  c ? "c" : "", dc->rd, dc->ra, dc->rb);
310fcf5ef2aSThomas Huth 
311fcf5ef2aSThomas Huth     /* Take care of the easy cases first.  */
312fcf5ef2aSThomas Huth     if (k) {
313fcf5ef2aSThomas Huth         /* k - keep carry, no need to update MSR.  */
314fcf5ef2aSThomas Huth         /* If rd == r0, it's a nop.  */
315fcf5ef2aSThomas Huth         if (dc->rd) {
316cfeea807SEdgar E. Iglesias             tcg_gen_sub_i32(cpu_R[dc->rd], *(dec_alu_op_b(dc)), cpu_R[dc->ra]);
317fcf5ef2aSThomas Huth 
318fcf5ef2aSThomas Huth             if (c) {
319fcf5ef2aSThomas Huth                 /* c - Add carry into the result.  */
320cfeea807SEdgar E. Iglesias                 cf = tcg_temp_new_i32();
321fcf5ef2aSThomas Huth 
322fcf5ef2aSThomas Huth                 read_carry(dc, cf);
323cfeea807SEdgar E. Iglesias                 tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
324cfeea807SEdgar E. Iglesias                 tcg_temp_free_i32(cf);
325fcf5ef2aSThomas Huth             }
326fcf5ef2aSThomas Huth         }
327fcf5ef2aSThomas Huth         return;
328fcf5ef2aSThomas Huth     }
329fcf5ef2aSThomas Huth 
330fcf5ef2aSThomas Huth     /* From now on, we can assume k is zero.  So we need to update MSR.  */
331fcf5ef2aSThomas Huth     /* Extract carry. And complement a into na.  */
332cfeea807SEdgar E. Iglesias     cf = tcg_temp_new_i32();
333cfeea807SEdgar E. Iglesias     na = tcg_temp_new_i32();
334fcf5ef2aSThomas Huth     if (c) {
335fcf5ef2aSThomas Huth         read_carry(dc, cf);
336fcf5ef2aSThomas Huth     } else {
337cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cf, 1);
338fcf5ef2aSThomas Huth     }
339fcf5ef2aSThomas Huth 
340fcf5ef2aSThomas Huth     /* d = b + ~a + c. carry defaults to 1.  */
341cfeea807SEdgar E. Iglesias     tcg_gen_not_i32(na, cpu_R[dc->ra]);
342fcf5ef2aSThomas Huth 
343fcf5ef2aSThomas Huth     if (dc->rd) {
344cfeea807SEdgar E. Iglesias         TCGv_i32 ncf = tcg_temp_new_i32();
345fcf5ef2aSThomas Huth         gen_helper_carry(ncf, na, *(dec_alu_op_b(dc)), cf);
346cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], na, *(dec_alu_op_b(dc)));
347cfeea807SEdgar E. Iglesias         tcg_gen_add_i32(cpu_R[dc->rd], cpu_R[dc->rd], cf);
348fcf5ef2aSThomas Huth         write_carry(dc, ncf);
349cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(ncf);
350fcf5ef2aSThomas Huth     } else {
351fcf5ef2aSThomas Huth         gen_helper_carry(cf, na, *(dec_alu_op_b(dc)), cf);
352fcf5ef2aSThomas Huth         write_carry(dc, cf);
353fcf5ef2aSThomas Huth     }
354cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(cf);
355cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(na);
356fcf5ef2aSThomas Huth }
357fcf5ef2aSThomas Huth 
358fcf5ef2aSThomas Huth static void dec_pattern(DisasContext *dc)
359fcf5ef2aSThomas Huth {
360fcf5ef2aSThomas Huth     unsigned int mode;
361fcf5ef2aSThomas Huth 
3629ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
3639ba8cd45SEdgar E. Iglesias         return;
364fcf5ef2aSThomas Huth     }
365fcf5ef2aSThomas Huth 
366fcf5ef2aSThomas Huth     mode = dc->opcode & 3;
367fcf5ef2aSThomas Huth     switch (mode) {
368fcf5ef2aSThomas Huth         case 0:
369fcf5ef2aSThomas Huth             /* pcmpbf.  */
370fcf5ef2aSThomas Huth             LOG_DIS("pcmpbf r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
371fcf5ef2aSThomas Huth             if (dc->rd)
372fcf5ef2aSThomas Huth                 gen_helper_pcmpbf(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
373fcf5ef2aSThomas Huth             break;
374fcf5ef2aSThomas Huth         case 2:
375fcf5ef2aSThomas Huth             LOG_DIS("pcmpeq r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
376fcf5ef2aSThomas Huth             if (dc->rd) {
377cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_EQ, cpu_R[dc->rd],
378fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
379fcf5ef2aSThomas Huth             }
380fcf5ef2aSThomas Huth             break;
381fcf5ef2aSThomas Huth         case 3:
382fcf5ef2aSThomas Huth             LOG_DIS("pcmpne r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
383fcf5ef2aSThomas Huth             if (dc->rd) {
384cfeea807SEdgar E. Iglesias                 tcg_gen_setcond_i32(TCG_COND_NE, cpu_R[dc->rd],
385fcf5ef2aSThomas Huth                                    cpu_R[dc->ra], cpu_R[dc->rb]);
386fcf5ef2aSThomas Huth             }
387fcf5ef2aSThomas Huth             break;
388fcf5ef2aSThomas Huth         default:
389fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu),
390fcf5ef2aSThomas Huth                       "unsupported pattern insn opcode=%x\n", dc->opcode);
391fcf5ef2aSThomas Huth             break;
392fcf5ef2aSThomas Huth     }
393fcf5ef2aSThomas Huth }
394fcf5ef2aSThomas Huth 
395fcf5ef2aSThomas Huth static void dec_and(DisasContext *dc)
396fcf5ef2aSThomas Huth {
397fcf5ef2aSThomas Huth     unsigned int not;
398fcf5ef2aSThomas Huth 
399fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
400fcf5ef2aSThomas Huth         dec_pattern(dc);
401fcf5ef2aSThomas Huth         return;
402fcf5ef2aSThomas Huth     }
403fcf5ef2aSThomas Huth 
404fcf5ef2aSThomas Huth     not = dc->opcode & (1 << 1);
405fcf5ef2aSThomas Huth     LOG_DIS("and%s\n", not ? "n" : "");
406fcf5ef2aSThomas Huth 
407fcf5ef2aSThomas Huth     if (!dc->rd)
408fcf5ef2aSThomas Huth         return;
409fcf5ef2aSThomas Huth 
410fcf5ef2aSThomas Huth     if (not) {
411cfeea807SEdgar E. Iglesias         tcg_gen_andc_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
412fcf5ef2aSThomas Huth     } else
413cfeea807SEdgar E. Iglesias         tcg_gen_and_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
414fcf5ef2aSThomas Huth }
415fcf5ef2aSThomas Huth 
416fcf5ef2aSThomas Huth static void dec_or(DisasContext *dc)
417fcf5ef2aSThomas Huth {
418fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
419fcf5ef2aSThomas Huth         dec_pattern(dc);
420fcf5ef2aSThomas Huth         return;
421fcf5ef2aSThomas Huth     }
422fcf5ef2aSThomas Huth 
423fcf5ef2aSThomas Huth     LOG_DIS("or r%d r%d r%d imm=%x\n", dc->rd, dc->ra, dc->rb, dc->imm);
424fcf5ef2aSThomas Huth     if (dc->rd)
425cfeea807SEdgar E. Iglesias         tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
426fcf5ef2aSThomas Huth }
427fcf5ef2aSThomas Huth 
428fcf5ef2aSThomas Huth static void dec_xor(DisasContext *dc)
429fcf5ef2aSThomas Huth {
430fcf5ef2aSThomas Huth     if (!dc->type_b && (dc->imm & (1 << 10))) {
431fcf5ef2aSThomas Huth         dec_pattern(dc);
432fcf5ef2aSThomas Huth         return;
433fcf5ef2aSThomas Huth     }
434fcf5ef2aSThomas Huth 
435fcf5ef2aSThomas Huth     LOG_DIS("xor r%d\n", dc->rd);
436fcf5ef2aSThomas Huth     if (dc->rd)
437cfeea807SEdgar E. Iglesias         tcg_gen_xor_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
438fcf5ef2aSThomas Huth }
439fcf5ef2aSThomas Huth 
440cfeea807SEdgar E. Iglesias static inline void msr_read(DisasContext *dc, TCGv_i32 d)
441fcf5ef2aSThomas Huth {
4423e0e16aeSRichard Henderson     tcg_gen_mov_i32(d, cpu_msr);
443fcf5ef2aSThomas Huth }
444fcf5ef2aSThomas Huth 
445cfeea807SEdgar E. Iglesias static inline void msr_write(DisasContext *dc, TCGv_i32 v)
446fcf5ef2aSThomas Huth {
447fcf5ef2aSThomas Huth     dc->cpustate_changed = 1;
4483e0e16aeSRichard Henderson     /* PVR bit is not writable, and is never set. */
4493e0e16aeSRichard Henderson     tcg_gen_andi_i32(cpu_msr, v, ~MSR_PVR);
450fcf5ef2aSThomas Huth }
451fcf5ef2aSThomas Huth 
452fcf5ef2aSThomas Huth static void dec_msr(DisasContext *dc)
453fcf5ef2aSThomas Huth {
454fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
455cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
4562023e9a3SEdgar E. Iglesias     unsigned int sr, rn;
457f0f7e7f7SEdgar E. Iglesias     bool to, clrset, extended = false;
458fcf5ef2aSThomas Huth 
4592023e9a3SEdgar E. Iglesias     sr = extract32(dc->imm, 0, 14);
4602023e9a3SEdgar E. Iglesias     to = extract32(dc->imm, 14, 1);
4612023e9a3SEdgar E. Iglesias     clrset = extract32(dc->imm, 15, 1) == 0;
462fcf5ef2aSThomas Huth     dc->type_b = 1;
4632023e9a3SEdgar E. Iglesias     if (to) {
464fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
465f0f7e7f7SEdgar E. Iglesias     }
466f0f7e7f7SEdgar E. Iglesias 
467f0f7e7f7SEdgar E. Iglesias     /* Extended MSRs are only available if addr_size > 32.  */
468f0f7e7f7SEdgar E. Iglesias     if (dc->cpu->cfg.addr_size > 32) {
469f0f7e7f7SEdgar E. Iglesias         /* The E-bit is encoded differently for To/From MSR.  */
470f0f7e7f7SEdgar E. Iglesias         static const unsigned int e_bit[] = { 19, 24 };
471f0f7e7f7SEdgar E. Iglesias 
472f0f7e7f7SEdgar E. Iglesias         extended = extract32(dc->imm, e_bit[to], 1);
4732023e9a3SEdgar E. Iglesias     }
474fcf5ef2aSThomas Huth 
475fcf5ef2aSThomas Huth     /* msrclr and msrset.  */
4762023e9a3SEdgar E. Iglesias     if (clrset) {
4772023e9a3SEdgar E. Iglesias         bool clr = extract32(dc->ir, 16, 1);
478fcf5ef2aSThomas Huth 
479fcf5ef2aSThomas Huth         LOG_DIS("msr%s r%d imm=%x\n", clr ? "clr" : "set",
480fcf5ef2aSThomas Huth                 dc->rd, dc->imm);
481fcf5ef2aSThomas Huth 
48256837509SEdgar E. Iglesias         if (!dc->cpu->cfg.use_msr_instr) {
483fcf5ef2aSThomas Huth             /* nop??? */
484fcf5ef2aSThomas Huth             return;
485fcf5ef2aSThomas Huth         }
486fcf5ef2aSThomas Huth 
487bdfc1e88SEdgar E. Iglesias         if (trap_userspace(dc, dc->imm != 4 && dc->imm != 0)) {
488fcf5ef2aSThomas Huth             return;
489fcf5ef2aSThomas Huth         }
490fcf5ef2aSThomas Huth 
491fcf5ef2aSThomas Huth         if (dc->rd)
492fcf5ef2aSThomas Huth             msr_read(dc, cpu_R[dc->rd]);
493fcf5ef2aSThomas Huth 
494cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
495cfeea807SEdgar E. Iglesias         t1 = tcg_temp_new_i32();
496fcf5ef2aSThomas Huth         msr_read(dc, t0);
497cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t1, *(dec_alu_op_b(dc)));
498fcf5ef2aSThomas Huth 
499fcf5ef2aSThomas Huth         if (clr) {
500cfeea807SEdgar E. Iglesias             tcg_gen_not_i32(t1, t1);
501cfeea807SEdgar E. Iglesias             tcg_gen_and_i32(t0, t0, t1);
502fcf5ef2aSThomas Huth         } else
503cfeea807SEdgar E. Iglesias             tcg_gen_or_i32(t0, t0, t1);
504fcf5ef2aSThomas Huth         msr_write(dc, t0);
505cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
506cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t1);
5070f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
508fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
509fcf5ef2aSThomas Huth         return;
510fcf5ef2aSThomas Huth     }
511fcf5ef2aSThomas Huth 
512bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, to)) {
513fcf5ef2aSThomas Huth         return;
514fcf5ef2aSThomas Huth     }
515fcf5ef2aSThomas Huth 
516fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
517fcf5ef2aSThomas Huth     /* Catch read/writes to the mmu block.  */
518fcf5ef2aSThomas Huth     if ((sr & ~0xff) == 0x1000) {
519f0f7e7f7SEdgar E. Iglesias         TCGv_i32 tmp_ext = tcg_const_i32(extended);
52005a9a651SEdgar E. Iglesias         TCGv_i32 tmp_sr;
52105a9a651SEdgar E. Iglesias 
522fcf5ef2aSThomas Huth         sr &= 7;
52305a9a651SEdgar E. Iglesias         tmp_sr = tcg_const_i32(sr);
524fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%d r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
52505a9a651SEdgar E. Iglesias         if (to) {
526f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_write(cpu_env, tmp_ext, tmp_sr, cpu_R[dc->ra]);
52705a9a651SEdgar E. Iglesias         } else {
528f0f7e7f7SEdgar E. Iglesias             gen_helper_mmu_read(cpu_R[dc->rd], cpu_env, tmp_ext, tmp_sr);
52905a9a651SEdgar E. Iglesias         }
53005a9a651SEdgar E. Iglesias         tcg_temp_free_i32(tmp_sr);
531f0f7e7f7SEdgar E. Iglesias         tcg_temp_free_i32(tmp_ext);
532fcf5ef2aSThomas Huth         return;
533fcf5ef2aSThomas Huth     }
534fcf5ef2aSThomas Huth #endif
535fcf5ef2aSThomas Huth 
536fcf5ef2aSThomas Huth     if (to) {
537fcf5ef2aSThomas Huth         LOG_DIS("m%ss sr%x r%d imm=%x\n", to ? "t" : "f", sr, dc->ra, dc->imm);
538fcf5ef2aSThomas Huth         switch (sr) {
539aa28e6d4SRichard Henderson             case SR_PC:
540fcf5ef2aSThomas Huth                 break;
541aa28e6d4SRichard Henderson             case SR_MSR:
542fcf5ef2aSThomas Huth                 msr_write(dc, cpu_R[dc->ra]);
543fcf5ef2aSThomas Huth                 break;
544351527b7SEdgar E. Iglesias             case SR_EAR:
545dbdb77c4SRichard Henderson                 {
546dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
547dbdb77c4SRichard Henderson                     tcg_gen_extu_i32_i64(t64, cpu_R[dc->ra]);
548dbdb77c4SRichard Henderson                     tcg_gen_st_i64(t64, cpu_env, offsetof(CPUMBState, ear));
549dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
550dbdb77c4SRichard Henderson                 }
551aa28e6d4SRichard Henderson                 break;
552351527b7SEdgar E. Iglesias             case SR_ESR:
55341ba37c4SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
55441ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
555aa28e6d4SRichard Henderson                 break;
556ab6dd380SEdgar E. Iglesias             case SR_FSR:
55786017ccfSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
55886017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
559aa28e6d4SRichard Henderson                 break;
560aa28e6d4SRichard Henderson             case SR_BTR:
561ccf628b7SRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
562ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
563aa28e6d4SRichard Henderson                 break;
564aa28e6d4SRichard Henderson             case SR_EDR:
56539db007eSRichard Henderson                 tcg_gen_st_i32(cpu_R[dc->ra],
56639db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
567fcf5ef2aSThomas Huth                 break;
568fcf5ef2aSThomas Huth             case 0x800:
569cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
570cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
571fcf5ef2aSThomas Huth                 break;
572fcf5ef2aSThomas Huth             case 0x802:
573cfeea807SEdgar E. Iglesias                 tcg_gen_st_i32(cpu_R[dc->ra],
574cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
575fcf5ef2aSThomas Huth                 break;
576fcf5ef2aSThomas Huth             default:
577fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "unknown mts reg %x\n", sr);
578fcf5ef2aSThomas Huth                 break;
579fcf5ef2aSThomas Huth         }
580fcf5ef2aSThomas Huth     } else {
581fcf5ef2aSThomas Huth         LOG_DIS("m%ss r%d sr%x imm=%x\n", to ? "t" : "f", dc->rd, sr, dc->imm);
582fcf5ef2aSThomas Huth 
583fcf5ef2aSThomas Huth         switch (sr) {
584aa28e6d4SRichard Henderson             case SR_PC:
585cfeea807SEdgar E. Iglesias                 tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
586fcf5ef2aSThomas Huth                 break;
587aa28e6d4SRichard Henderson             case SR_MSR:
588fcf5ef2aSThomas Huth                 msr_read(dc, cpu_R[dc->rd]);
589fcf5ef2aSThomas Huth                 break;
590351527b7SEdgar E. Iglesias             case SR_EAR:
591dbdb77c4SRichard Henderson                 {
592dbdb77c4SRichard Henderson                     TCGv_i64 t64 = tcg_temp_new_i64();
593dbdb77c4SRichard Henderson                     tcg_gen_ld_i64(t64, cpu_env, offsetof(CPUMBState, ear));
594a1b48e3aSEdgar E. Iglesias                     if (extended) {
595dbdb77c4SRichard Henderson                         tcg_gen_extrh_i64_i32(cpu_R[dc->rd], t64);
596aa28e6d4SRichard Henderson                     } else {
597dbdb77c4SRichard Henderson                         tcg_gen_extrl_i64_i32(cpu_R[dc->rd], t64);
598dbdb77c4SRichard Henderson                     }
599dbdb77c4SRichard Henderson                     tcg_temp_free_i64(t64);
600a1b48e3aSEdgar E. Iglesias                 }
601aa28e6d4SRichard Henderson                 break;
602351527b7SEdgar E. Iglesias             case SR_ESR:
60341ba37c4SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
60441ba37c4SRichard Henderson                                cpu_env, offsetof(CPUMBState, esr));
605aa28e6d4SRichard Henderson                 break;
606351527b7SEdgar E. Iglesias             case SR_FSR:
60786017ccfSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
60886017ccfSRichard Henderson                                cpu_env, offsetof(CPUMBState, fsr));
609aa28e6d4SRichard Henderson                 break;
610351527b7SEdgar E. Iglesias             case SR_BTR:
611ccf628b7SRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
612ccf628b7SRichard Henderson                                cpu_env, offsetof(CPUMBState, btr));
613aa28e6d4SRichard Henderson                 break;
6147cdae31dSTong Ho             case SR_EDR:
61539db007eSRichard Henderson                 tcg_gen_ld_i32(cpu_R[dc->rd],
61639db007eSRichard Henderson                                cpu_env, offsetof(CPUMBState, edr));
617fcf5ef2aSThomas Huth                 break;
618fcf5ef2aSThomas Huth             case 0x800:
619cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
620cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, slr));
621fcf5ef2aSThomas Huth                 break;
622fcf5ef2aSThomas Huth             case 0x802:
623cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
624cfeea807SEdgar E. Iglesias                                cpu_env, offsetof(CPUMBState, shr));
625fcf5ef2aSThomas Huth                 break;
626351527b7SEdgar E. Iglesias             case 0x2000 ... 0x200c:
627fcf5ef2aSThomas Huth                 rn = sr & 0xf;
628cfeea807SEdgar E. Iglesias                 tcg_gen_ld_i32(cpu_R[dc->rd],
629fcf5ef2aSThomas Huth                               cpu_env, offsetof(CPUMBState, pvr.regs[rn]));
630fcf5ef2aSThomas Huth                 break;
631fcf5ef2aSThomas Huth             default:
632fcf5ef2aSThomas Huth                 cpu_abort(cs, "unknown mfs reg %x\n", sr);
633fcf5ef2aSThomas Huth                 break;
634fcf5ef2aSThomas Huth         }
635fcf5ef2aSThomas Huth     }
636fcf5ef2aSThomas Huth 
637fcf5ef2aSThomas Huth     if (dc->rd == 0) {
638cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[0], 0);
639fcf5ef2aSThomas Huth     }
640fcf5ef2aSThomas Huth }
641fcf5ef2aSThomas Huth 
642fcf5ef2aSThomas Huth /* Multiplier unit.  */
643fcf5ef2aSThomas Huth static void dec_mul(DisasContext *dc)
644fcf5ef2aSThomas Huth {
645cfeea807SEdgar E. Iglesias     TCGv_i32 tmp;
646fcf5ef2aSThomas Huth     unsigned int subcode;
647fcf5ef2aSThomas Huth 
6489ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_hw_mul)) {
649fcf5ef2aSThomas Huth         return;
650fcf5ef2aSThomas Huth     }
651fcf5ef2aSThomas Huth 
652fcf5ef2aSThomas Huth     subcode = dc->imm & 3;
653fcf5ef2aSThomas Huth 
654fcf5ef2aSThomas Huth     if (dc->type_b) {
655fcf5ef2aSThomas Huth         LOG_DIS("muli r%d r%d %x\n", dc->rd, dc->ra, dc->imm);
656cfeea807SEdgar E. Iglesias         tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], *(dec_alu_op_b(dc)));
657fcf5ef2aSThomas Huth         return;
658fcf5ef2aSThomas Huth     }
659fcf5ef2aSThomas Huth 
660fcf5ef2aSThomas Huth     /* mulh, mulhsu and mulhu are not available if C_USE_HW_MUL is < 2.  */
6619b964318SEdgar E. Iglesias     if (subcode >= 1 && subcode <= 3 && dc->cpu->cfg.use_hw_mul < 2) {
662fcf5ef2aSThomas Huth         /* nop??? */
663fcf5ef2aSThomas Huth     }
664fcf5ef2aSThomas Huth 
665cfeea807SEdgar E. Iglesias     tmp = tcg_temp_new_i32();
666fcf5ef2aSThomas Huth     switch (subcode) {
667fcf5ef2aSThomas Huth         case 0:
668fcf5ef2aSThomas Huth             LOG_DIS("mul r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
669cfeea807SEdgar E. Iglesias             tcg_gen_mul_i32(cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
670fcf5ef2aSThomas Huth             break;
671fcf5ef2aSThomas Huth         case 1:
672fcf5ef2aSThomas Huth             LOG_DIS("mulh r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
673cfeea807SEdgar E. Iglesias             tcg_gen_muls2_i32(tmp, cpu_R[dc->rd],
674cfeea807SEdgar E. Iglesias                               cpu_R[dc->ra], cpu_R[dc->rb]);
675fcf5ef2aSThomas Huth             break;
676fcf5ef2aSThomas Huth         case 2:
677fcf5ef2aSThomas Huth             LOG_DIS("mulhsu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
678cfeea807SEdgar E. Iglesias             tcg_gen_mulsu2_i32(tmp, cpu_R[dc->rd],
679cfeea807SEdgar E. Iglesias                                cpu_R[dc->ra], cpu_R[dc->rb]);
680fcf5ef2aSThomas Huth             break;
681fcf5ef2aSThomas Huth         case 3:
682fcf5ef2aSThomas Huth             LOG_DIS("mulhu r%d r%d r%d\n", dc->rd, dc->ra, dc->rb);
683cfeea807SEdgar E. Iglesias             tcg_gen_mulu2_i32(tmp, cpu_R[dc->rd], cpu_R[dc->ra], cpu_R[dc->rb]);
684fcf5ef2aSThomas Huth             break;
685fcf5ef2aSThomas Huth         default:
686fcf5ef2aSThomas Huth             cpu_abort(CPU(dc->cpu), "unknown MUL insn %x\n", subcode);
687fcf5ef2aSThomas Huth             break;
688fcf5ef2aSThomas Huth     }
689cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(tmp);
690fcf5ef2aSThomas Huth }
691fcf5ef2aSThomas Huth 
692fcf5ef2aSThomas Huth /* Div unit.  */
693fcf5ef2aSThomas Huth static void dec_div(DisasContext *dc)
694fcf5ef2aSThomas Huth {
695fcf5ef2aSThomas Huth     unsigned int u;
696fcf5ef2aSThomas Huth 
697fcf5ef2aSThomas Huth     u = dc->imm & 2;
698fcf5ef2aSThomas Huth     LOG_DIS("div\n");
699fcf5ef2aSThomas Huth 
7009ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_div)) {
7019ba8cd45SEdgar E. Iglesias         return;
702fcf5ef2aSThomas Huth     }
703fcf5ef2aSThomas Huth 
704fcf5ef2aSThomas Huth     if (u)
705fcf5ef2aSThomas Huth         gen_helper_divu(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
706fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
707fcf5ef2aSThomas Huth     else
708fcf5ef2aSThomas Huth         gen_helper_divs(cpu_R[dc->rd], cpu_env, *(dec_alu_op_b(dc)),
709fcf5ef2aSThomas Huth                         cpu_R[dc->ra]);
710fcf5ef2aSThomas Huth     if (!dc->rd)
711cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], 0);
712fcf5ef2aSThomas Huth }
713fcf5ef2aSThomas Huth 
714fcf5ef2aSThomas Huth static void dec_barrel(DisasContext *dc)
715fcf5ef2aSThomas Huth {
716cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
717faa48d74SEdgar E. Iglesias     unsigned int imm_w, imm_s;
718d09b2585SEdgar E. Iglesias     bool s, t, e = false, i = false;
719fcf5ef2aSThomas Huth 
7209ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_barrel)) {
721fcf5ef2aSThomas Huth         return;
722fcf5ef2aSThomas Huth     }
723fcf5ef2aSThomas Huth 
724faa48d74SEdgar E. Iglesias     if (dc->type_b) {
725faa48d74SEdgar E. Iglesias         /* Insert and extract are only available in immediate mode.  */
726d09b2585SEdgar E. Iglesias         i = extract32(dc->imm, 15, 1);
727faa48d74SEdgar E. Iglesias         e = extract32(dc->imm, 14, 1);
728faa48d74SEdgar E. Iglesias     }
729e3e84983SEdgar E. Iglesias     s = extract32(dc->imm, 10, 1);
730e3e84983SEdgar E. Iglesias     t = extract32(dc->imm, 9, 1);
731faa48d74SEdgar E. Iglesias     imm_w = extract32(dc->imm, 6, 5);
732faa48d74SEdgar E. Iglesias     imm_s = extract32(dc->imm, 0, 5);
733fcf5ef2aSThomas Huth 
734faa48d74SEdgar E. Iglesias     LOG_DIS("bs%s%s%s r%d r%d r%d\n",
735faa48d74SEdgar E. Iglesias             e ? "e" : "",
736fcf5ef2aSThomas Huth             s ? "l" : "r", t ? "a" : "l", dc->rd, dc->ra, dc->rb);
737fcf5ef2aSThomas Huth 
738faa48d74SEdgar E. Iglesias     if (e) {
739faa48d74SEdgar E. Iglesias         if (imm_w + imm_s > 32 || imm_w == 0) {
740faa48d74SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
741faa48d74SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsefi: Bad input w=%d s=%d\n",
742faa48d74SEdgar E. Iglesias                           imm_w, imm_s);
743faa48d74SEdgar E. Iglesias         } else {
744faa48d74SEdgar E. Iglesias             tcg_gen_extract_i32(cpu_R[dc->rd], cpu_R[dc->ra], imm_s, imm_w);
745faa48d74SEdgar E. Iglesias         }
746d09b2585SEdgar E. Iglesias     } else if (i) {
747d09b2585SEdgar E. Iglesias         int width = imm_w - imm_s + 1;
748d09b2585SEdgar E. Iglesias 
749d09b2585SEdgar E. Iglesias         if (imm_w < imm_s) {
750d09b2585SEdgar E. Iglesias             /* These inputs have an undefined behavior.  */
751d09b2585SEdgar E. Iglesias             qemu_log_mask(LOG_GUEST_ERROR, "bsifi: Bad input w=%d s=%d\n",
752d09b2585SEdgar E. Iglesias                           imm_w, imm_s);
753d09b2585SEdgar E. Iglesias         } else {
754d09b2585SEdgar E. Iglesias             tcg_gen_deposit_i32(cpu_R[dc->rd], cpu_R[dc->rd], cpu_R[dc->ra],
755d09b2585SEdgar E. Iglesias                                 imm_s, width);
756d09b2585SEdgar E. Iglesias         }
757faa48d74SEdgar E. Iglesias     } else {
758cfeea807SEdgar E. Iglesias         t0 = tcg_temp_new_i32();
759fcf5ef2aSThomas Huth 
760cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(t0, *(dec_alu_op_b(dc)));
761cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t0, t0, 31);
762fcf5ef2aSThomas Huth 
7632acf6d53SEdgar E. Iglesias         if (s) {
764cfeea807SEdgar E. Iglesias             tcg_gen_shl_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7652acf6d53SEdgar E. Iglesias         } else {
7662acf6d53SEdgar E. Iglesias             if (t) {
767cfeea807SEdgar E. Iglesias                 tcg_gen_sar_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
7682acf6d53SEdgar E. Iglesias             } else {
769cfeea807SEdgar E. Iglesias                 tcg_gen_shr_i32(cpu_R[dc->rd], cpu_R[dc->ra], t0);
770fcf5ef2aSThomas Huth             }
771fcf5ef2aSThomas Huth         }
772cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(t0);
7732acf6d53SEdgar E. Iglesias     }
774faa48d74SEdgar E. Iglesias }
775fcf5ef2aSThomas Huth 
776fcf5ef2aSThomas Huth static void dec_bit(DisasContext *dc)
777fcf5ef2aSThomas Huth {
778fcf5ef2aSThomas Huth     CPUState *cs = CPU(dc->cpu);
779cfeea807SEdgar E. Iglesias     TCGv_i32 t0;
780fcf5ef2aSThomas Huth     unsigned int op;
781fcf5ef2aSThomas Huth 
782fcf5ef2aSThomas Huth     op = dc->ir & ((1 << 9) - 1);
783fcf5ef2aSThomas Huth     switch (op) {
784fcf5ef2aSThomas Huth         case 0x21:
785fcf5ef2aSThomas Huth             /* src.  */
786cfeea807SEdgar E. Iglesias             t0 = tcg_temp_new_i32();
787fcf5ef2aSThomas Huth 
788fcf5ef2aSThomas Huth             LOG_DIS("src r%d r%d\n", dc->rd, dc->ra);
7893e0e16aeSRichard Henderson             tcg_gen_andi_i32(t0, cpu_msr, MSR_CC);
790fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
791fcf5ef2aSThomas Huth             if (dc->rd) {
792cfeea807SEdgar E. Iglesias                 tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
793cfeea807SEdgar E. Iglesias                 tcg_gen_or_i32(cpu_R[dc->rd], cpu_R[dc->rd], t0);
794fcf5ef2aSThomas Huth             }
795cfeea807SEdgar E. Iglesias             tcg_temp_free_i32(t0);
796fcf5ef2aSThomas Huth             break;
797fcf5ef2aSThomas Huth 
798fcf5ef2aSThomas Huth         case 0x1:
799fcf5ef2aSThomas Huth         case 0x41:
800fcf5ef2aSThomas Huth             /* srl.  */
801fcf5ef2aSThomas Huth             LOG_DIS("srl r%d r%d\n", dc->rd, dc->ra);
802fcf5ef2aSThomas Huth 
803fcf5ef2aSThomas Huth             /* Update carry. Note that write carry only looks at the LSB.  */
804fcf5ef2aSThomas Huth             write_carry(dc, cpu_R[dc->ra]);
805fcf5ef2aSThomas Huth             if (dc->rd) {
806fcf5ef2aSThomas Huth                 if (op == 0x41)
807cfeea807SEdgar E. Iglesias                     tcg_gen_shri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
808fcf5ef2aSThomas Huth                 else
809cfeea807SEdgar E. Iglesias                     tcg_gen_sari_i32(cpu_R[dc->rd], cpu_R[dc->ra], 1);
810fcf5ef2aSThomas Huth             }
811fcf5ef2aSThomas Huth             break;
812fcf5ef2aSThomas Huth         case 0x60:
813fcf5ef2aSThomas Huth             LOG_DIS("ext8s r%d r%d\n", dc->rd, dc->ra);
814fcf5ef2aSThomas Huth             tcg_gen_ext8s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
815fcf5ef2aSThomas Huth             break;
816fcf5ef2aSThomas Huth         case 0x61:
817fcf5ef2aSThomas Huth             LOG_DIS("ext16s r%d r%d\n", dc->rd, dc->ra);
818fcf5ef2aSThomas Huth             tcg_gen_ext16s_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
819fcf5ef2aSThomas Huth             break;
820fcf5ef2aSThomas Huth         case 0x64:
821fcf5ef2aSThomas Huth         case 0x66:
822fcf5ef2aSThomas Huth         case 0x74:
823fcf5ef2aSThomas Huth         case 0x76:
824fcf5ef2aSThomas Huth             /* wdc.  */
825fcf5ef2aSThomas Huth             LOG_DIS("wdc r%d\n", dc->ra);
826bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
827fcf5ef2aSThomas Huth             break;
828fcf5ef2aSThomas Huth         case 0x68:
829fcf5ef2aSThomas Huth             /* wic.  */
830fcf5ef2aSThomas Huth             LOG_DIS("wic r%d\n", dc->ra);
831bdfc1e88SEdgar E. Iglesias             trap_userspace(dc, true);
832fcf5ef2aSThomas Huth             break;
833fcf5ef2aSThomas Huth         case 0xe0:
8349ba8cd45SEdgar E. Iglesias             if (trap_illegal(dc, !dc->cpu->cfg.use_pcmp_instr)) {
8359ba8cd45SEdgar E. Iglesias                 return;
836fcf5ef2aSThomas Huth             }
8378fc5239eSEdgar E. Iglesias             if (dc->cpu->cfg.use_pcmp_instr) {
8385318420cSRichard Henderson                 tcg_gen_clzi_i32(cpu_R[dc->rd], cpu_R[dc->ra], 32);
839fcf5ef2aSThomas Huth             }
840fcf5ef2aSThomas Huth             break;
841fcf5ef2aSThomas Huth         case 0x1e0:
842fcf5ef2aSThomas Huth             /* swapb */
843fcf5ef2aSThomas Huth             LOG_DIS("swapb r%d r%d\n", dc->rd, dc->ra);
844fcf5ef2aSThomas Huth             tcg_gen_bswap32_i32(cpu_R[dc->rd], cpu_R[dc->ra]);
845fcf5ef2aSThomas Huth             break;
846fcf5ef2aSThomas Huth         case 0x1e2:
847fcf5ef2aSThomas Huth             /*swaph */
848fcf5ef2aSThomas Huth             LOG_DIS("swaph r%d r%d\n", dc->rd, dc->ra);
849fcf5ef2aSThomas Huth             tcg_gen_rotri_i32(cpu_R[dc->rd], cpu_R[dc->ra], 16);
850fcf5ef2aSThomas Huth             break;
851fcf5ef2aSThomas Huth         default:
852fcf5ef2aSThomas Huth             cpu_abort(cs, "unknown bit oc=%x op=%x rd=%d ra=%d rb=%d\n",
853fcf5ef2aSThomas Huth                       dc->pc, op, dc->rd, dc->ra, dc->rb);
854fcf5ef2aSThomas Huth             break;
855fcf5ef2aSThomas Huth     }
856fcf5ef2aSThomas Huth }
857fcf5ef2aSThomas Huth 
858fcf5ef2aSThomas Huth static inline void sync_jmpstate(DisasContext *dc)
859fcf5ef2aSThomas Huth {
860fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
861fcf5ef2aSThomas Huth         if (dc->jmp == JMP_DIRECT) {
862*9b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
863fcf5ef2aSThomas Huth         }
864fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
8650f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->jmp_pc);
866fcf5ef2aSThomas Huth     }
867fcf5ef2aSThomas Huth }
868fcf5ef2aSThomas Huth 
869fcf5ef2aSThomas Huth static void dec_imm(DisasContext *dc)
870fcf5ef2aSThomas Huth {
871fcf5ef2aSThomas Huth     LOG_DIS("imm %x\n", dc->imm << 16);
872*9b158558SRichard Henderson     tcg_gen_movi_i32(cpu_imm, (dc->imm << 16));
873fcf5ef2aSThomas Huth     dc->tb_flags |= IMM_FLAG;
874fcf5ef2aSThomas Huth     dc->clear_imm = 0;
875fcf5ef2aSThomas Huth }
876fcf5ef2aSThomas Huth 
877d248e1beSEdgar E. Iglesias static inline void compute_ldst_addr(DisasContext *dc, bool ea, TCGv t)
878fcf5ef2aSThomas Huth {
8790e9033c8SEdgar E. Iglesias     bool extimm = dc->tb_flags & IMM_FLAG;
8800e9033c8SEdgar E. Iglesias     /* Should be set to true if r1 is used by loadstores.  */
8810e9033c8SEdgar E. Iglesias     bool stackprot = false;
882403322eaSEdgar E. Iglesias     TCGv_i32 t32;
883fcf5ef2aSThomas Huth 
884fcf5ef2aSThomas Huth     /* All load/stores use ra.  */
885fcf5ef2aSThomas Huth     if (dc->ra == 1 && dc->cpu->cfg.stackprot) {
8860e9033c8SEdgar E. Iglesias         stackprot = true;
887fcf5ef2aSThomas Huth     }
888fcf5ef2aSThomas Huth 
889fcf5ef2aSThomas Huth     /* Treat the common cases first.  */
890fcf5ef2aSThomas Huth     if (!dc->type_b) {
891d248e1beSEdgar E. Iglesias         if (ea) {
892d248e1beSEdgar E. Iglesias             int addr_size = dc->cpu->cfg.addr_size;
893d248e1beSEdgar E. Iglesias 
894d248e1beSEdgar E. Iglesias             if (addr_size == 32) {
895d248e1beSEdgar E. Iglesias                 tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
896d248e1beSEdgar E. Iglesias                 return;
897d248e1beSEdgar E. Iglesias             }
898d248e1beSEdgar E. Iglesias 
899d248e1beSEdgar E. Iglesias             tcg_gen_concat_i32_i64(t, cpu_R[dc->rb], cpu_R[dc->ra]);
900d248e1beSEdgar E. Iglesias             if (addr_size < 64) {
901d248e1beSEdgar E. Iglesias                 /* Mask off out of range bits.  */
902d248e1beSEdgar E. Iglesias                 tcg_gen_andi_i64(t, t, MAKE_64BIT_MASK(0, addr_size));
903d248e1beSEdgar E. Iglesias             }
904d248e1beSEdgar E. Iglesias             return;
905d248e1beSEdgar E. Iglesias         }
906d248e1beSEdgar E. Iglesias 
9070dc4af5cSEdgar E. Iglesias         /* If any of the regs is r0, set t to the value of the other reg.  */
908fcf5ef2aSThomas Huth         if (dc->ra == 0) {
909403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->rb]);
9100dc4af5cSEdgar E. Iglesias             return;
911fcf5ef2aSThomas Huth         } else if (dc->rb == 0) {
912403322eaSEdgar E. Iglesias             tcg_gen_extu_i32_tl(t, cpu_R[dc->ra]);
9130dc4af5cSEdgar E. Iglesias             return;
914fcf5ef2aSThomas Huth         }
915fcf5ef2aSThomas Huth 
916fcf5ef2aSThomas Huth         if (dc->rb == 1 && dc->cpu->cfg.stackprot) {
9170e9033c8SEdgar E. Iglesias             stackprot = true;
918fcf5ef2aSThomas Huth         }
919fcf5ef2aSThomas Huth 
920403322eaSEdgar E. Iglesias         t32 = tcg_temp_new_i32();
921403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], cpu_R[dc->rb]);
922403322eaSEdgar E. Iglesias         tcg_gen_extu_i32_tl(t, t32);
923403322eaSEdgar E. Iglesias         tcg_temp_free_i32(t32);
924fcf5ef2aSThomas Huth 
925fcf5ef2aSThomas Huth         if (stackprot) {
9260a87e691SEdgar E. Iglesias             gen_helper_stackprot(cpu_env, t);
927fcf5ef2aSThomas Huth         }
9280dc4af5cSEdgar E. Iglesias         return;
929fcf5ef2aSThomas Huth     }
930fcf5ef2aSThomas Huth     /* Immediate.  */
931403322eaSEdgar E. Iglesias     t32 = tcg_temp_new_i32();
932fcf5ef2aSThomas Huth     if (!extimm) {
933f7a66e3aSEdgar E. Iglesias         tcg_gen_addi_i32(t32, cpu_R[dc->ra], (int16_t)dc->imm);
934403322eaSEdgar E. Iglesias     } else {
935403322eaSEdgar E. Iglesias         tcg_gen_add_i32(t32, cpu_R[dc->ra], *(dec_alu_op_b(dc)));
936403322eaSEdgar E. Iglesias     }
937403322eaSEdgar E. Iglesias     tcg_gen_extu_i32_tl(t, t32);
938403322eaSEdgar E. Iglesias     tcg_temp_free_i32(t32);
939fcf5ef2aSThomas Huth 
940fcf5ef2aSThomas Huth     if (stackprot) {
9410a87e691SEdgar E. Iglesias         gen_helper_stackprot(cpu_env, t);
942fcf5ef2aSThomas Huth     }
9430dc4af5cSEdgar E. Iglesias     return;
944fcf5ef2aSThomas Huth }
945fcf5ef2aSThomas Huth 
946fcf5ef2aSThomas Huth static void dec_load(DisasContext *dc)
947fcf5ef2aSThomas Huth {
948403322eaSEdgar E. Iglesias     TCGv_i32 v;
949403322eaSEdgar E. Iglesias     TCGv addr;
9508534063aSEdgar E. Iglesias     unsigned int size;
951d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
952d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
95314776ab5STony Nguyen     MemOp mop;
954fcf5ef2aSThomas Huth 
955fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
956fcf5ef2aSThomas Huth     size = 1 << mop;
957fcf5ef2aSThomas Huth     if (!dc->type_b) {
958d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
9598534063aSEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
9608534063aSEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
961fcf5ef2aSThomas Huth     }
962fcf5ef2aSThomas Huth     mop |= MO_TE;
963fcf5ef2aSThomas Huth     if (rev) {
964fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
965fcf5ef2aSThomas Huth     }
966fcf5ef2aSThomas Huth 
9679ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
968fcf5ef2aSThomas Huth         return;
969fcf5ef2aSThomas Huth     }
970fcf5ef2aSThomas Huth 
971d248e1beSEdgar E. Iglesias     if (trap_userspace(dc, ea)) {
972d248e1beSEdgar E. Iglesias         return;
973d248e1beSEdgar E. Iglesias     }
974d248e1beSEdgar E. Iglesias 
975d248e1beSEdgar E. Iglesias     LOG_DIS("l%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
976d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
977d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
978fcf5ef2aSThomas Huth 
979fcf5ef2aSThomas Huth     t_sync_flags(dc);
980403322eaSEdgar E. Iglesias     addr = tcg_temp_new();
981d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
982d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
983d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
984fcf5ef2aSThomas Huth 
985fcf5ef2aSThomas Huth     /*
986fcf5ef2aSThomas Huth      * When doing reverse accesses we need to do two things.
987fcf5ef2aSThomas Huth      *
988fcf5ef2aSThomas Huth      * 1. Reverse the address wrt endianness.
989fcf5ef2aSThomas Huth      * 2. Byteswap the data lanes on the way back into the CPU core.
990fcf5ef2aSThomas Huth      */
991fcf5ef2aSThomas Huth     if (rev && size != 4) {
992fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
993fcf5ef2aSThomas Huth         switch (size) {
994fcf5ef2aSThomas Huth             case 1:
995fcf5ef2aSThomas Huth             {
996a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
997fcf5ef2aSThomas Huth                 break;
998fcf5ef2aSThomas Huth             }
999fcf5ef2aSThomas Huth 
1000fcf5ef2aSThomas Huth             case 2:
1001fcf5ef2aSThomas Huth                 /* 00 -> 10
1002fcf5ef2aSThomas Huth                    10 -> 00.  */
1003403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1004fcf5ef2aSThomas Huth                 break;
1005fcf5ef2aSThomas Huth             default:
1006fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1007fcf5ef2aSThomas Huth                 break;
1008fcf5ef2aSThomas Huth         }
1009fcf5ef2aSThomas Huth     }
1010fcf5ef2aSThomas Huth 
1011fcf5ef2aSThomas Huth     /* lwx does not throw unaligned access errors, so force alignment */
1012fcf5ef2aSThomas Huth     if (ex) {
1013403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1014fcf5ef2aSThomas Huth     }
1015fcf5ef2aSThomas Huth 
1016fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1017fcf5ef2aSThomas Huth     sync_jmpstate(dc);
1018fcf5ef2aSThomas Huth 
1019fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
1020fcf5ef2aSThomas Huth     /*
1021fcf5ef2aSThomas Huth      * Microblaze gives MMU faults priority over faults due to
1022fcf5ef2aSThomas Huth      * unaligned addresses. That's why we speculatively do the load
1023fcf5ef2aSThomas Huth      * into v. If the load succeeds, we verify alignment of the
1024fcf5ef2aSThomas Huth      * address and if that succeeds we write into the destination reg.
1025fcf5ef2aSThomas Huth      */
1026cfeea807SEdgar E. Iglesias     v = tcg_temp_new_i32();
1027d248e1beSEdgar E. Iglesias     tcg_gen_qemu_ld_i32(v, addr, mem_index, mop);
1028fcf5ef2aSThomas Huth 
10291507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1030a6338015SEdgar E. Iglesias         TCGv_i32 t0 = tcg_const_i32(0);
1031a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1032a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1033a6338015SEdgar E. Iglesias 
10340f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
1035a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t0, tsize);
1036a6338015SEdgar E. Iglesias 
1037a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t0);
1038a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1039a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1040fcf5ef2aSThomas Huth     }
1041fcf5ef2aSThomas Huth 
1042fcf5ef2aSThomas Huth     if (ex) {
1043*9b158558SRichard Henderson         tcg_gen_mov_tl(cpu_res_addr, addr);
1044*9b158558SRichard Henderson         tcg_gen_mov_i32(cpu_res_val, v);
1045fcf5ef2aSThomas Huth     }
1046fcf5ef2aSThomas Huth     if (dc->rd) {
1047cfeea807SEdgar E. Iglesias         tcg_gen_mov_i32(cpu_R[dc->rd], v);
1048fcf5ef2aSThomas Huth     }
1049cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(v);
1050fcf5ef2aSThomas Huth 
1051fcf5ef2aSThomas Huth     if (ex) { /* lwx */
1052fcf5ef2aSThomas Huth         /* no support for AXI exclusive so always clear C */
1053fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1054fcf5ef2aSThomas Huth     }
1055fcf5ef2aSThomas Huth 
1056403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1057fcf5ef2aSThomas Huth }
1058fcf5ef2aSThomas Huth 
1059fcf5ef2aSThomas Huth static void dec_store(DisasContext *dc)
1060fcf5ef2aSThomas Huth {
1061403322eaSEdgar E. Iglesias     TCGv addr;
1062fcf5ef2aSThomas Huth     TCGLabel *swx_skip = NULL;
1063b51b3d43SEdgar E. Iglesias     unsigned int size;
1064d248e1beSEdgar E. Iglesias     bool rev = false, ex = false, ea = false;
1065d248e1beSEdgar E. Iglesias     int mem_index = cpu_mmu_index(&dc->cpu->env, false);
106614776ab5STony Nguyen     MemOp mop;
1067fcf5ef2aSThomas Huth 
1068fcf5ef2aSThomas Huth     mop = dc->opcode & 3;
1069fcf5ef2aSThomas Huth     size = 1 << mop;
1070fcf5ef2aSThomas Huth     if (!dc->type_b) {
1071d248e1beSEdgar E. Iglesias         ea = extract32(dc->ir, 7, 1);
1072b51b3d43SEdgar E. Iglesias         rev = extract32(dc->ir, 9, 1);
1073b51b3d43SEdgar E. Iglesias         ex = extract32(dc->ir, 10, 1);
1074fcf5ef2aSThomas Huth     }
1075fcf5ef2aSThomas Huth     mop |= MO_TE;
1076fcf5ef2aSThomas Huth     if (rev) {
1077fcf5ef2aSThomas Huth         mop ^= MO_BSWAP;
1078fcf5ef2aSThomas Huth     }
1079fcf5ef2aSThomas Huth 
10809ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, size > 4)) {
1081fcf5ef2aSThomas Huth         return;
1082fcf5ef2aSThomas Huth     }
1083fcf5ef2aSThomas Huth 
1084d248e1beSEdgar E. Iglesias     trap_userspace(dc, ea);
1085d248e1beSEdgar E. Iglesias 
1086d248e1beSEdgar E. Iglesias     LOG_DIS("s%d%s%s%s%s\n", size, dc->type_b ? "i" : "", rev ? "r" : "",
1087d248e1beSEdgar E. Iglesias                                                         ex ? "x" : "",
1088d248e1beSEdgar E. Iglesias                                                         ea ? "ea" : "");
1089fcf5ef2aSThomas Huth     t_sync_flags(dc);
1090fcf5ef2aSThomas Huth     /* If we get a fault on a dslot, the jmpstate better be in sync.  */
1091fcf5ef2aSThomas Huth     sync_jmpstate(dc);
10920dc4af5cSEdgar E. Iglesias     /* SWX needs a temp_local.  */
1093403322eaSEdgar E. Iglesias     addr = ex ? tcg_temp_local_new() : tcg_temp_new();
1094d248e1beSEdgar E. Iglesias     compute_ldst_addr(dc, ea, addr);
1095d248e1beSEdgar E. Iglesias     /* Extended addressing bypasses the MMU.  */
1096d248e1beSEdgar E. Iglesias     mem_index = ea ? MMU_NOMMU_IDX : mem_index;
1097fcf5ef2aSThomas Huth 
1098fcf5ef2aSThomas Huth     if (ex) { /* swx */
1099cfeea807SEdgar E. Iglesias         TCGv_i32 tval;
1100fcf5ef2aSThomas Huth 
1101fcf5ef2aSThomas Huth         /* swx does not throw unaligned access errors, so force alignment */
1102403322eaSEdgar E. Iglesias         tcg_gen_andi_tl(addr, addr, ~3);
1103fcf5ef2aSThomas Huth 
1104fcf5ef2aSThomas Huth         write_carryi(dc, 1);
1105fcf5ef2aSThomas Huth         swx_skip = gen_new_label();
1106*9b158558SRichard Henderson         tcg_gen_brcond_tl(TCG_COND_NE, cpu_res_addr, addr, swx_skip);
1107fcf5ef2aSThomas Huth 
1108071cdc67SEdgar E. Iglesias         /*
1109071cdc67SEdgar E. Iglesias          * Compare the value loaded at lwx with current contents of
1110071cdc67SEdgar E. Iglesias          * the reserved location.
1111071cdc67SEdgar E. Iglesias          */
1112cfeea807SEdgar E. Iglesias         tval = tcg_temp_new_i32();
1113071cdc67SEdgar E. Iglesias 
1114*9b158558SRichard Henderson         tcg_gen_atomic_cmpxchg_i32(tval, addr, cpu_res_val,
1115071cdc67SEdgar E. Iglesias                                    cpu_R[dc->rd], mem_index,
1116071cdc67SEdgar E. Iglesias                                    mop);
1117071cdc67SEdgar E. Iglesias 
1118*9b158558SRichard Henderson         tcg_gen_brcond_i32(TCG_COND_NE, cpu_res_val, tval, swx_skip);
1119fcf5ef2aSThomas Huth         write_carryi(dc, 0);
1120cfeea807SEdgar E. Iglesias         tcg_temp_free_i32(tval);
1121fcf5ef2aSThomas Huth     }
1122fcf5ef2aSThomas Huth 
1123fcf5ef2aSThomas Huth     if (rev && size != 4) {
1124fcf5ef2aSThomas Huth         /* Endian reverse the address. t is addr.  */
1125fcf5ef2aSThomas Huth         switch (size) {
1126fcf5ef2aSThomas Huth             case 1:
1127fcf5ef2aSThomas Huth             {
1128a6338015SEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 3);
1129fcf5ef2aSThomas Huth                 break;
1130fcf5ef2aSThomas Huth             }
1131fcf5ef2aSThomas Huth 
1132fcf5ef2aSThomas Huth             case 2:
1133fcf5ef2aSThomas Huth                 /* 00 -> 10
1134fcf5ef2aSThomas Huth                    10 -> 00.  */
1135fcf5ef2aSThomas Huth                 /* Force addr into the temp.  */
1136403322eaSEdgar E. Iglesias                 tcg_gen_xori_tl(addr, addr, 2);
1137fcf5ef2aSThomas Huth                 break;
1138fcf5ef2aSThomas Huth             default:
1139fcf5ef2aSThomas Huth                 cpu_abort(CPU(dc->cpu), "Invalid reverse size\n");
1140fcf5ef2aSThomas Huth                 break;
1141fcf5ef2aSThomas Huth         }
1142fcf5ef2aSThomas Huth     }
1143071cdc67SEdgar E. Iglesias 
1144071cdc67SEdgar E. Iglesias     if (!ex) {
1145d248e1beSEdgar E. Iglesias         tcg_gen_qemu_st_i32(cpu_R[dc->rd], addr, mem_index, mop);
1146071cdc67SEdgar E. Iglesias     }
1147fcf5ef2aSThomas Huth 
1148fcf5ef2aSThomas Huth     /* Verify alignment if needed.  */
11491507e5f6SEdgar E. Iglesias     if (dc->cpu->cfg.unaligned_exceptions && size > 1) {
1150a6338015SEdgar E. Iglesias         TCGv_i32 t1 = tcg_const_i32(1);
1151a6338015SEdgar E. Iglesias         TCGv_i32 treg = tcg_const_i32(dc->rd);
1152a6338015SEdgar E. Iglesias         TCGv_i32 tsize = tcg_const_i32(size - 1);
1153a6338015SEdgar E. Iglesias 
11540f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, dc->pc);
1155fcf5ef2aSThomas Huth         /* FIXME: if the alignment is wrong, we should restore the value
1156fcf5ef2aSThomas Huth          *        in memory. One possible way to achieve this is to probe
1157fcf5ef2aSThomas Huth          *        the MMU prior to the memaccess, thay way we could put
1158fcf5ef2aSThomas Huth          *        the alignment checks in between the probe and the mem
1159fcf5ef2aSThomas Huth          *        access.
1160fcf5ef2aSThomas Huth          */
1161a6338015SEdgar E. Iglesias         gen_helper_memalign(cpu_env, addr, treg, t1, tsize);
1162a6338015SEdgar E. Iglesias 
1163a6338015SEdgar E. Iglesias         tcg_temp_free_i32(t1);
1164a6338015SEdgar E. Iglesias         tcg_temp_free_i32(treg);
1165a6338015SEdgar E. Iglesias         tcg_temp_free_i32(tsize);
1166fcf5ef2aSThomas Huth     }
1167fcf5ef2aSThomas Huth 
1168fcf5ef2aSThomas Huth     if (ex) {
1169fcf5ef2aSThomas Huth         gen_set_label(swx_skip);
1170fcf5ef2aSThomas Huth     }
1171fcf5ef2aSThomas Huth 
1172403322eaSEdgar E. Iglesias     tcg_temp_free(addr);
1173fcf5ef2aSThomas Huth }
1174fcf5ef2aSThomas Huth 
1175fcf5ef2aSThomas Huth static inline void eval_cc(DisasContext *dc, unsigned int cc,
11769e6e1828SEdgar E. Iglesias                            TCGv_i32 d, TCGv_i32 a)
1177fcf5ef2aSThomas Huth {
1178d89b86e9SEdgar E. Iglesias     static const int mb_to_tcg_cc[] = {
1179d89b86e9SEdgar E. Iglesias         [CC_EQ] = TCG_COND_EQ,
1180d89b86e9SEdgar E. Iglesias         [CC_NE] = TCG_COND_NE,
1181d89b86e9SEdgar E. Iglesias         [CC_LT] = TCG_COND_LT,
1182d89b86e9SEdgar E. Iglesias         [CC_LE] = TCG_COND_LE,
1183d89b86e9SEdgar E. Iglesias         [CC_GE] = TCG_COND_GE,
1184d89b86e9SEdgar E. Iglesias         [CC_GT] = TCG_COND_GT,
1185d89b86e9SEdgar E. Iglesias     };
1186d89b86e9SEdgar E. Iglesias 
1187fcf5ef2aSThomas Huth     switch (cc) {
1188fcf5ef2aSThomas Huth     case CC_EQ:
1189fcf5ef2aSThomas Huth     case CC_NE:
1190fcf5ef2aSThomas Huth     case CC_LT:
1191fcf5ef2aSThomas Huth     case CC_LE:
1192fcf5ef2aSThomas Huth     case CC_GE:
1193fcf5ef2aSThomas Huth     case CC_GT:
11949e6e1828SEdgar E. Iglesias         tcg_gen_setcondi_i32(mb_to_tcg_cc[cc], d, a, 0);
1195fcf5ef2aSThomas Huth         break;
1196fcf5ef2aSThomas Huth     default:
1197fcf5ef2aSThomas Huth         cpu_abort(CPU(dc->cpu), "Unknown condition code %x.\n", cc);
1198fcf5ef2aSThomas Huth         break;
1199fcf5ef2aSThomas Huth     }
1200fcf5ef2aSThomas Huth }
1201fcf5ef2aSThomas Huth 
12020f96e96bSRichard Henderson static void eval_cond_jmp(DisasContext *dc, TCGv_i32 pc_true, TCGv_i32 pc_false)
1203fcf5ef2aSThomas Huth {
12040f96e96bSRichard Henderson     TCGv_i32 zero = tcg_const_i32(0);
1205e956caf2SEdgar E. Iglesias 
12060f96e96bSRichard Henderson     tcg_gen_movcond_i32(TCG_COND_NE, cpu_pc,
1207*9b158558SRichard Henderson                         cpu_btaken, zero,
1208e956caf2SEdgar E. Iglesias                         pc_true, pc_false);
1209e956caf2SEdgar E. Iglesias 
12100f96e96bSRichard Henderson     tcg_temp_free_i32(zero);
1211fcf5ef2aSThomas Huth }
1212fcf5ef2aSThomas Huth 
1213f91c60f0SEdgar E. Iglesias static void dec_setup_dslot(DisasContext *dc)
1214f91c60f0SEdgar E. Iglesias {
1215f91c60f0SEdgar E. Iglesias         TCGv_i32 tmp = tcg_const_i32(dc->type_b && (dc->tb_flags & IMM_FLAG));
1216f91c60f0SEdgar E. Iglesias 
1217f91c60f0SEdgar E. Iglesias         dc->delayed_branch = 2;
1218f91c60f0SEdgar E. Iglesias         dc->tb_flags |= D_FLAG;
1219f91c60f0SEdgar E. Iglesias 
1220f91c60f0SEdgar E. Iglesias         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUMBState, bimm));
1221f91c60f0SEdgar E. Iglesias         tcg_temp_free_i32(tmp);
1222f91c60f0SEdgar E. Iglesias }
1223f91c60f0SEdgar E. Iglesias 
1224fcf5ef2aSThomas Huth static void dec_bcc(DisasContext *dc)
1225fcf5ef2aSThomas Huth {
1226fcf5ef2aSThomas Huth     unsigned int cc;
1227fcf5ef2aSThomas Huth     unsigned int dslot;
1228fcf5ef2aSThomas Huth 
1229fcf5ef2aSThomas Huth     cc = EXTRACT_FIELD(dc->ir, 21, 23);
1230fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 25);
1231fcf5ef2aSThomas Huth     LOG_DIS("bcc%s r%d %x\n", dslot ? "d" : "", dc->ra, dc->imm);
1232fcf5ef2aSThomas Huth 
1233fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1234fcf5ef2aSThomas Huth     if (dslot) {
1235f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1236fcf5ef2aSThomas Huth     }
1237fcf5ef2aSThomas Huth 
1238fcf5ef2aSThomas Huth     if (dec_alu_op_b_is_small_imm(dc)) {
1239fcf5ef2aSThomas Huth         int32_t offset = (int32_t)((int16_t)dc->imm); /* sign-extend.  */
1240fcf5ef2aSThomas Huth 
12410f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_btarget, dc->pc + offset);
1242fcf5ef2aSThomas Huth         dc->jmp = JMP_DIRECT_CC;
1243fcf5ef2aSThomas Huth         dc->jmp_pc = dc->pc + offset;
1244fcf5ef2aSThomas Huth     } else {
1245fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
12460f96e96bSRichard Henderson         tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1247fcf5ef2aSThomas Huth     }
1248*9b158558SRichard Henderson     eval_cc(dc, cc, cpu_btaken, cpu_R[dc->ra]);
1249fcf5ef2aSThomas Huth }
1250fcf5ef2aSThomas Huth 
1251fcf5ef2aSThomas Huth static void dec_br(DisasContext *dc)
1252fcf5ef2aSThomas Huth {
1253fcf5ef2aSThomas Huth     unsigned int dslot, link, abs, mbar;
1254fcf5ef2aSThomas Huth 
1255fcf5ef2aSThomas Huth     dslot = dc->ir & (1 << 20);
1256fcf5ef2aSThomas Huth     abs = dc->ir & (1 << 19);
1257fcf5ef2aSThomas Huth     link = dc->ir & (1 << 18);
1258fcf5ef2aSThomas Huth 
1259fcf5ef2aSThomas Huth     /* Memory barrier.  */
1260fcf5ef2aSThomas Huth     mbar = (dc->ir >> 16) & 31;
1261fcf5ef2aSThomas Huth     if (mbar == 2 && dc->imm == 4) {
1262badcbf9dSEdgar E. Iglesias         uint16_t mbar_imm = dc->rd;
1263badcbf9dSEdgar E. Iglesias 
12646f3c458bSEdgar E. Iglesias         LOG_DIS("mbar %d\n", mbar_imm);
12656f3c458bSEdgar E. Iglesias 
12663f172744SEdgar E. Iglesias         /* Data access memory barrier.  */
12673f172744SEdgar E. Iglesias         if ((mbar_imm & 2) == 0) {
12683f172744SEdgar E. Iglesias             tcg_gen_mb(TCG_BAR_SC | TCG_MO_ALL);
12693f172744SEdgar E. Iglesias         }
12703f172744SEdgar E. Iglesias 
1271fcf5ef2aSThomas Huth         /* mbar IMM & 16 decodes to sleep.  */
1272badcbf9dSEdgar E. Iglesias         if (mbar_imm & 16) {
127341ba37c4SRichard Henderson             TCGv_i32 tmp_1;
1274fcf5ef2aSThomas Huth 
1275fcf5ef2aSThomas Huth             LOG_DIS("sleep\n");
1276fcf5ef2aSThomas Huth 
1277b4919e7dSEdgar E. Iglesias             if (trap_userspace(dc, true)) {
1278b4919e7dSEdgar E. Iglesias                 /* Sleep is a privileged instruction.  */
1279b4919e7dSEdgar E. Iglesias                 return;
1280b4919e7dSEdgar E. Iglesias             }
1281b4919e7dSEdgar E. Iglesias 
1282fcf5ef2aSThomas Huth             t_sync_flags(dc);
128341ba37c4SRichard Henderson 
128441ba37c4SRichard Henderson             tmp_1 = tcg_const_i32(1);
1285fcf5ef2aSThomas Huth             tcg_gen_st_i32(tmp_1, cpu_env,
1286fcf5ef2aSThomas Huth                            -offsetof(MicroBlazeCPU, env)
1287fcf5ef2aSThomas Huth                            +offsetof(CPUState, halted));
1288fcf5ef2aSThomas Huth             tcg_temp_free_i32(tmp_1);
128941ba37c4SRichard Henderson 
129041ba37c4SRichard Henderson             tcg_gen_movi_i32(cpu_pc, dc->pc + 4);
129141ba37c4SRichard Henderson 
129241ba37c4SRichard Henderson             gen_raise_exception(dc, EXCP_HLT);
1293fcf5ef2aSThomas Huth             return;
1294fcf5ef2aSThomas Huth         }
1295fcf5ef2aSThomas Huth         /* Break the TB.  */
1296fcf5ef2aSThomas Huth         dc->cpustate_changed = 1;
1297fcf5ef2aSThomas Huth         return;
1298fcf5ef2aSThomas Huth     }
1299fcf5ef2aSThomas Huth 
1300fcf5ef2aSThomas Huth     LOG_DIS("br%s%s%s%s imm=%x\n",
1301fcf5ef2aSThomas Huth              abs ? "a" : "", link ? "l" : "",
1302fcf5ef2aSThomas Huth              dc->type_b ? "i" : "", dslot ? "d" : "",
1303fcf5ef2aSThomas Huth              dc->imm);
1304fcf5ef2aSThomas Huth 
1305fcf5ef2aSThomas Huth     dc->delayed_branch = 1;
1306fcf5ef2aSThomas Huth     if (dslot) {
1307f91c60f0SEdgar E. Iglesias         dec_setup_dslot(dc);
1308fcf5ef2aSThomas Huth     }
1309fcf5ef2aSThomas Huth     if (link && dc->rd)
1310cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(cpu_R[dc->rd], dc->pc);
1311fcf5ef2aSThomas Huth 
1312fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1313fcf5ef2aSThomas Huth     if (abs) {
1314*9b158558SRichard Henderson         tcg_gen_movi_i32(cpu_btaken, 1);
13150f96e96bSRichard Henderson         tcg_gen_mov_i32(cpu_btarget, *(dec_alu_op_b(dc)));
1316fcf5ef2aSThomas Huth         if (link && !dslot) {
131741ba37c4SRichard Henderson             if (!(dc->tb_flags & IMM_FLAG) &&
131841ba37c4SRichard Henderson                 (dc->imm == 8 || dc->imm == 0x18)) {
131941ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_BREAK);
132041ba37c4SRichard Henderson             }
1321fcf5ef2aSThomas Huth             if (dc->imm == 0) {
1322bdfc1e88SEdgar E. Iglesias                 if (trap_userspace(dc, true)) {
1323fcf5ef2aSThomas Huth                     return;
1324fcf5ef2aSThomas Huth                 }
132541ba37c4SRichard Henderson                 gen_raise_exception_sync(dc, EXCP_DEBUG);
1326fcf5ef2aSThomas Huth             }
1327fcf5ef2aSThomas Huth         }
1328fcf5ef2aSThomas Huth     } else {
1329fcf5ef2aSThomas Huth         if (dec_alu_op_b_is_small_imm(dc)) {
1330fcf5ef2aSThomas Huth             dc->jmp = JMP_DIRECT;
1331fcf5ef2aSThomas Huth             dc->jmp_pc = dc->pc + (int32_t)((int16_t)dc->imm);
1332fcf5ef2aSThomas Huth         } else {
1333*9b158558SRichard Henderson             tcg_gen_movi_i32(cpu_btaken, 1);
13340f96e96bSRichard Henderson             tcg_gen_addi_i32(cpu_btarget, *dec_alu_op_b(dc), dc->pc);
1335fcf5ef2aSThomas Huth         }
1336fcf5ef2aSThomas Huth     }
1337fcf5ef2aSThomas Huth }
1338fcf5ef2aSThomas Huth 
1339fcf5ef2aSThomas Huth static inline void do_rti(DisasContext *dc)
1340fcf5ef2aSThomas Huth {
1341cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1342cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1343cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13443e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13450a22f8cfSEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
13460a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_IE);
1347cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1348fcf5ef2aSThomas Huth 
1349cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1350cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1351fcf5ef2aSThomas Huth     msr_write(dc, t1);
1352cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1353cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1354fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTI_FLAG;
1355fcf5ef2aSThomas Huth }
1356fcf5ef2aSThomas Huth 
1357fcf5ef2aSThomas Huth static inline void do_rtb(DisasContext *dc)
1358fcf5ef2aSThomas Huth {
1359cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1360cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1361cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
13623e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13630a22f8cfSEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_BIP);
1364cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1365cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1366fcf5ef2aSThomas Huth 
1367cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1368cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1369fcf5ef2aSThomas Huth     msr_write(dc, t1);
1370cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1371cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1372fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTB_FLAG;
1373fcf5ef2aSThomas Huth }
1374fcf5ef2aSThomas Huth 
1375fcf5ef2aSThomas Huth static inline void do_rte(DisasContext *dc)
1376fcf5ef2aSThomas Huth {
1377cfeea807SEdgar E. Iglesias     TCGv_i32 t0, t1;
1378cfeea807SEdgar E. Iglesias     t0 = tcg_temp_new_i32();
1379cfeea807SEdgar E. Iglesias     t1 = tcg_temp_new_i32();
1380fcf5ef2aSThomas Huth 
13813e0e16aeSRichard Henderson     tcg_gen_mov_i32(t1, cpu_msr);
13820a22f8cfSEdgar E. Iglesias     tcg_gen_ori_i32(t1, t1, MSR_EE);
1383cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~MSR_EIP);
1384cfeea807SEdgar E. Iglesias     tcg_gen_shri_i32(t0, t1, 1);
1385cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t0, t0, (MSR_VM | MSR_UM));
1386fcf5ef2aSThomas Huth 
1387cfeea807SEdgar E. Iglesias     tcg_gen_andi_i32(t1, t1, ~(MSR_VM | MSR_UM));
1388cfeea807SEdgar E. Iglesias     tcg_gen_or_i32(t1, t1, t0);
1389fcf5ef2aSThomas Huth     msr_write(dc, t1);
1390cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t1);
1391cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t0);
1392fcf5ef2aSThomas Huth     dc->tb_flags &= ~DRTE_FLAG;
1393fcf5ef2aSThomas Huth }
1394fcf5ef2aSThomas Huth 
1395fcf5ef2aSThomas Huth static void dec_rts(DisasContext *dc)
1396fcf5ef2aSThomas Huth {
1397fcf5ef2aSThomas Huth     unsigned int b_bit, i_bit, e_bit;
1398fcf5ef2aSThomas Huth 
1399fcf5ef2aSThomas Huth     i_bit = dc->ir & (1 << 21);
1400fcf5ef2aSThomas Huth     b_bit = dc->ir & (1 << 22);
1401fcf5ef2aSThomas Huth     e_bit = dc->ir & (1 << 23);
1402fcf5ef2aSThomas Huth 
1403bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, i_bit || b_bit || e_bit)) {
1404bdfc1e88SEdgar E. Iglesias         return;
1405bdfc1e88SEdgar E. Iglesias     }
1406bdfc1e88SEdgar E. Iglesias 
1407f91c60f0SEdgar E. Iglesias     dec_setup_dslot(dc);
1408fcf5ef2aSThomas Huth 
1409fcf5ef2aSThomas Huth     if (i_bit) {
1410fcf5ef2aSThomas Huth         LOG_DIS("rtid ir=%x\n", dc->ir);
1411fcf5ef2aSThomas Huth         dc->tb_flags |= DRTI_FLAG;
1412fcf5ef2aSThomas Huth     } else if (b_bit) {
1413fcf5ef2aSThomas Huth         LOG_DIS("rtbd ir=%x\n", dc->ir);
1414fcf5ef2aSThomas Huth         dc->tb_flags |= DRTB_FLAG;
1415fcf5ef2aSThomas Huth     } else if (e_bit) {
1416fcf5ef2aSThomas Huth         LOG_DIS("rted ir=%x\n", dc->ir);
1417fcf5ef2aSThomas Huth         dc->tb_flags |= DRTE_FLAG;
1418fcf5ef2aSThomas Huth     } else
1419fcf5ef2aSThomas Huth         LOG_DIS("rts ir=%x\n", dc->ir);
1420fcf5ef2aSThomas Huth 
1421fcf5ef2aSThomas Huth     dc->jmp = JMP_INDIRECT;
1422*9b158558SRichard Henderson     tcg_gen_movi_i32(cpu_btaken, 1);
14230f96e96bSRichard Henderson     tcg_gen_add_i32(cpu_btarget, cpu_R[dc->ra], *dec_alu_op_b(dc));
1424fcf5ef2aSThomas Huth }
1425fcf5ef2aSThomas Huth 
1426fcf5ef2aSThomas Huth static int dec_check_fpuv2(DisasContext *dc)
1427fcf5ef2aSThomas Huth {
1428fcf5ef2aSThomas Huth     if ((dc->cpu->cfg.use_fpu != 2) && (dc->tb_flags & MSR_EE_FLAG)) {
142941ba37c4SRichard Henderson         gen_raise_hw_excp(dc, ESR_EC_FPU);
1430fcf5ef2aSThomas Huth     }
14312016a6a7SJoe Komlodi     return (dc->cpu->cfg.use_fpu == 2) ? PVR2_USE_FPU2_MASK : 0;
1432fcf5ef2aSThomas Huth }
1433fcf5ef2aSThomas Huth 
1434fcf5ef2aSThomas Huth static void dec_fpu(DisasContext *dc)
1435fcf5ef2aSThomas Huth {
1436fcf5ef2aSThomas Huth     unsigned int fpu_insn;
1437fcf5ef2aSThomas Huth 
14389ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, !dc->cpu->cfg.use_fpu)) {
1439fcf5ef2aSThomas Huth         return;
1440fcf5ef2aSThomas Huth     }
1441fcf5ef2aSThomas Huth 
1442fcf5ef2aSThomas Huth     fpu_insn = (dc->ir >> 7) & 7;
1443fcf5ef2aSThomas Huth 
1444fcf5ef2aSThomas Huth     switch (fpu_insn) {
1445fcf5ef2aSThomas Huth         case 0:
1446fcf5ef2aSThomas Huth             gen_helper_fadd(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1447fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1448fcf5ef2aSThomas Huth             break;
1449fcf5ef2aSThomas Huth 
1450fcf5ef2aSThomas Huth         case 1:
1451fcf5ef2aSThomas Huth             gen_helper_frsub(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1452fcf5ef2aSThomas Huth                              cpu_R[dc->rb]);
1453fcf5ef2aSThomas Huth             break;
1454fcf5ef2aSThomas Huth 
1455fcf5ef2aSThomas Huth         case 2:
1456fcf5ef2aSThomas Huth             gen_helper_fmul(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1457fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1458fcf5ef2aSThomas Huth             break;
1459fcf5ef2aSThomas Huth 
1460fcf5ef2aSThomas Huth         case 3:
1461fcf5ef2aSThomas Huth             gen_helper_fdiv(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra],
1462fcf5ef2aSThomas Huth                             cpu_R[dc->rb]);
1463fcf5ef2aSThomas Huth             break;
1464fcf5ef2aSThomas Huth 
1465fcf5ef2aSThomas Huth         case 4:
1466fcf5ef2aSThomas Huth             switch ((dc->ir >> 4) & 7) {
1467fcf5ef2aSThomas Huth                 case 0:
1468fcf5ef2aSThomas Huth                     gen_helper_fcmp_un(cpu_R[dc->rd], cpu_env,
1469fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1470fcf5ef2aSThomas Huth                     break;
1471fcf5ef2aSThomas Huth                 case 1:
1472fcf5ef2aSThomas Huth                     gen_helper_fcmp_lt(cpu_R[dc->rd], cpu_env,
1473fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1474fcf5ef2aSThomas Huth                     break;
1475fcf5ef2aSThomas Huth                 case 2:
1476fcf5ef2aSThomas Huth                     gen_helper_fcmp_eq(cpu_R[dc->rd], cpu_env,
1477fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1478fcf5ef2aSThomas Huth                     break;
1479fcf5ef2aSThomas Huth                 case 3:
1480fcf5ef2aSThomas Huth                     gen_helper_fcmp_le(cpu_R[dc->rd], cpu_env,
1481fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1482fcf5ef2aSThomas Huth                     break;
1483fcf5ef2aSThomas Huth                 case 4:
1484fcf5ef2aSThomas Huth                     gen_helper_fcmp_gt(cpu_R[dc->rd], cpu_env,
1485fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1486fcf5ef2aSThomas Huth                     break;
1487fcf5ef2aSThomas Huth                 case 5:
1488fcf5ef2aSThomas Huth                     gen_helper_fcmp_ne(cpu_R[dc->rd], cpu_env,
1489fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1490fcf5ef2aSThomas Huth                     break;
1491fcf5ef2aSThomas Huth                 case 6:
1492fcf5ef2aSThomas Huth                     gen_helper_fcmp_ge(cpu_R[dc->rd], cpu_env,
1493fcf5ef2aSThomas Huth                                        cpu_R[dc->ra], cpu_R[dc->rb]);
1494fcf5ef2aSThomas Huth                     break;
1495fcf5ef2aSThomas Huth                 default:
1496fcf5ef2aSThomas Huth                     qemu_log_mask(LOG_UNIMP,
1497fcf5ef2aSThomas Huth                                   "unimplemented fcmp fpu_insn=%x pc=%x"
1498fcf5ef2aSThomas Huth                                   " opc=%x\n",
1499fcf5ef2aSThomas Huth                                   fpu_insn, dc->pc, dc->opcode);
1500fcf5ef2aSThomas Huth                     dc->abort_at_next_insn = 1;
1501fcf5ef2aSThomas Huth                     break;
1502fcf5ef2aSThomas Huth             }
1503fcf5ef2aSThomas Huth             break;
1504fcf5ef2aSThomas Huth 
1505fcf5ef2aSThomas Huth         case 5:
1506fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1507fcf5ef2aSThomas Huth                 return;
1508fcf5ef2aSThomas Huth             }
1509fcf5ef2aSThomas Huth             gen_helper_flt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1510fcf5ef2aSThomas Huth             break;
1511fcf5ef2aSThomas Huth 
1512fcf5ef2aSThomas Huth         case 6:
1513fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1514fcf5ef2aSThomas Huth                 return;
1515fcf5ef2aSThomas Huth             }
1516fcf5ef2aSThomas Huth             gen_helper_fint(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1517fcf5ef2aSThomas Huth             break;
1518fcf5ef2aSThomas Huth 
1519fcf5ef2aSThomas Huth         case 7:
1520fcf5ef2aSThomas Huth             if (!dec_check_fpuv2(dc)) {
1521fcf5ef2aSThomas Huth                 return;
1522fcf5ef2aSThomas Huth             }
1523fcf5ef2aSThomas Huth             gen_helper_fsqrt(cpu_R[dc->rd], cpu_env, cpu_R[dc->ra]);
1524fcf5ef2aSThomas Huth             break;
1525fcf5ef2aSThomas Huth 
1526fcf5ef2aSThomas Huth         default:
1527fcf5ef2aSThomas Huth             qemu_log_mask(LOG_UNIMP, "unimplemented FPU insn fpu_insn=%x pc=%x"
1528fcf5ef2aSThomas Huth                           " opc=%x\n",
1529fcf5ef2aSThomas Huth                           fpu_insn, dc->pc, dc->opcode);
1530fcf5ef2aSThomas Huth             dc->abort_at_next_insn = 1;
1531fcf5ef2aSThomas Huth             break;
1532fcf5ef2aSThomas Huth     }
1533fcf5ef2aSThomas Huth }
1534fcf5ef2aSThomas Huth 
1535fcf5ef2aSThomas Huth static void dec_null(DisasContext *dc)
1536fcf5ef2aSThomas Huth {
15379ba8cd45SEdgar E. Iglesias     if (trap_illegal(dc, true)) {
1538fcf5ef2aSThomas Huth         return;
1539fcf5ef2aSThomas Huth     }
1540fcf5ef2aSThomas Huth     qemu_log_mask(LOG_GUEST_ERROR, "unknown insn pc=%x opc=%x\n", dc->pc, dc->opcode);
1541fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 1;
1542fcf5ef2aSThomas Huth }
1543fcf5ef2aSThomas Huth 
1544fcf5ef2aSThomas Huth /* Insns connected to FSL or AXI stream attached devices.  */
1545fcf5ef2aSThomas Huth static void dec_stream(DisasContext *dc)
1546fcf5ef2aSThomas Huth {
1547fcf5ef2aSThomas Huth     TCGv_i32 t_id, t_ctrl;
1548fcf5ef2aSThomas Huth     int ctrl;
1549fcf5ef2aSThomas Huth 
1550fcf5ef2aSThomas Huth     LOG_DIS("%s%s imm=%x\n", dc->rd ? "get" : "put",
1551fcf5ef2aSThomas Huth             dc->type_b ? "" : "d", dc->imm);
1552fcf5ef2aSThomas Huth 
1553bdfc1e88SEdgar E. Iglesias     if (trap_userspace(dc, true)) {
1554fcf5ef2aSThomas Huth         return;
1555fcf5ef2aSThomas Huth     }
1556fcf5ef2aSThomas Huth 
1557cfeea807SEdgar E. Iglesias     t_id = tcg_temp_new_i32();
1558fcf5ef2aSThomas Huth     if (dc->type_b) {
1559cfeea807SEdgar E. Iglesias         tcg_gen_movi_i32(t_id, dc->imm & 0xf);
1560fcf5ef2aSThomas Huth         ctrl = dc->imm >> 10;
1561fcf5ef2aSThomas Huth     } else {
1562cfeea807SEdgar E. Iglesias         tcg_gen_andi_i32(t_id, cpu_R[dc->rb], 0xf);
1563fcf5ef2aSThomas Huth         ctrl = dc->imm >> 5;
1564fcf5ef2aSThomas Huth     }
1565fcf5ef2aSThomas Huth 
1566cfeea807SEdgar E. Iglesias     t_ctrl = tcg_const_i32(ctrl);
1567fcf5ef2aSThomas Huth 
1568fcf5ef2aSThomas Huth     if (dc->rd == 0) {
1569fcf5ef2aSThomas Huth         gen_helper_put(t_id, t_ctrl, cpu_R[dc->ra]);
1570fcf5ef2aSThomas Huth     } else {
1571fcf5ef2aSThomas Huth         gen_helper_get(cpu_R[dc->rd], t_id, t_ctrl);
1572fcf5ef2aSThomas Huth     }
1573cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_id);
1574cfeea807SEdgar E. Iglesias     tcg_temp_free_i32(t_ctrl);
1575fcf5ef2aSThomas Huth }
1576fcf5ef2aSThomas Huth 
1577fcf5ef2aSThomas Huth static struct decoder_info {
1578fcf5ef2aSThomas Huth     struct {
1579fcf5ef2aSThomas Huth         uint32_t bits;
1580fcf5ef2aSThomas Huth         uint32_t mask;
1581fcf5ef2aSThomas Huth     };
1582fcf5ef2aSThomas Huth     void (*dec)(DisasContext *dc);
1583fcf5ef2aSThomas Huth } decinfo[] = {
1584fcf5ef2aSThomas Huth     {DEC_ADD, dec_add},
1585fcf5ef2aSThomas Huth     {DEC_SUB, dec_sub},
1586fcf5ef2aSThomas Huth     {DEC_AND, dec_and},
1587fcf5ef2aSThomas Huth     {DEC_XOR, dec_xor},
1588fcf5ef2aSThomas Huth     {DEC_OR, dec_or},
1589fcf5ef2aSThomas Huth     {DEC_BIT, dec_bit},
1590fcf5ef2aSThomas Huth     {DEC_BARREL, dec_barrel},
1591fcf5ef2aSThomas Huth     {DEC_LD, dec_load},
1592fcf5ef2aSThomas Huth     {DEC_ST, dec_store},
1593fcf5ef2aSThomas Huth     {DEC_IMM, dec_imm},
1594fcf5ef2aSThomas Huth     {DEC_BR, dec_br},
1595fcf5ef2aSThomas Huth     {DEC_BCC, dec_bcc},
1596fcf5ef2aSThomas Huth     {DEC_RTS, dec_rts},
1597fcf5ef2aSThomas Huth     {DEC_FPU, dec_fpu},
1598fcf5ef2aSThomas Huth     {DEC_MUL, dec_mul},
1599fcf5ef2aSThomas Huth     {DEC_DIV, dec_div},
1600fcf5ef2aSThomas Huth     {DEC_MSR, dec_msr},
1601fcf5ef2aSThomas Huth     {DEC_STREAM, dec_stream},
1602fcf5ef2aSThomas Huth     {{0, 0}, dec_null}
1603fcf5ef2aSThomas Huth };
1604fcf5ef2aSThomas Huth 
1605fcf5ef2aSThomas Huth static inline void decode(DisasContext *dc, uint32_t ir)
1606fcf5ef2aSThomas Huth {
1607fcf5ef2aSThomas Huth     int i;
1608fcf5ef2aSThomas Huth 
1609fcf5ef2aSThomas Huth     dc->ir = ir;
1610fcf5ef2aSThomas Huth     LOG_DIS("%8.8x\t", dc->ir);
1611fcf5ef2aSThomas Huth 
1612462c2544SEdgar E. Iglesias     if (ir == 0) {
16131ee1bd28SEdgar E. Iglesias         trap_illegal(dc, dc->cpu->cfg.opcode_0_illegal);
1614462c2544SEdgar E. Iglesias         /* Don't decode nop/zero instructions any further.  */
1615462c2544SEdgar E. Iglesias         return;
1616462c2544SEdgar E. Iglesias     }
1617fcf5ef2aSThomas Huth 
1618fcf5ef2aSThomas Huth     /* bit 2 seems to indicate insn type.  */
1619fcf5ef2aSThomas Huth     dc->type_b = ir & (1 << 29);
1620fcf5ef2aSThomas Huth 
1621fcf5ef2aSThomas Huth     dc->opcode = EXTRACT_FIELD(ir, 26, 31);
1622fcf5ef2aSThomas Huth     dc->rd = EXTRACT_FIELD(ir, 21, 25);
1623fcf5ef2aSThomas Huth     dc->ra = EXTRACT_FIELD(ir, 16, 20);
1624fcf5ef2aSThomas Huth     dc->rb = EXTRACT_FIELD(ir, 11, 15);
1625fcf5ef2aSThomas Huth     dc->imm = EXTRACT_FIELD(ir, 0, 15);
1626fcf5ef2aSThomas Huth 
1627fcf5ef2aSThomas Huth     /* Large switch for all insns.  */
1628fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(decinfo); i++) {
1629fcf5ef2aSThomas Huth         if ((dc->opcode & decinfo[i].mask) == decinfo[i].bits) {
1630fcf5ef2aSThomas Huth             decinfo[i].dec(dc);
1631fcf5ef2aSThomas Huth             break;
1632fcf5ef2aSThomas Huth         }
1633fcf5ef2aSThomas Huth     }
1634fcf5ef2aSThomas Huth }
1635fcf5ef2aSThomas Huth 
1636fcf5ef2aSThomas Huth /* generate intermediate code for basic block 'tb'.  */
16378b86d6d2SRichard Henderson void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1638fcf5ef2aSThomas Huth {
16399c489ea6SLluís Vilanova     CPUMBState *env = cs->env_ptr;
1640f5c7e93aSRichard Henderson     MicroBlazeCPU *cpu = env_archcpu(env);
1641fcf5ef2aSThomas Huth     uint32_t pc_start;
1642fcf5ef2aSThomas Huth     struct DisasContext ctx;
1643fcf5ef2aSThomas Huth     struct DisasContext *dc = &ctx;
164456371527SEmilio G. Cota     uint32_t page_start, org_flags;
1645cfeea807SEdgar E. Iglesias     uint32_t npc;
1646fcf5ef2aSThomas Huth     int num_insns;
1647fcf5ef2aSThomas Huth 
1648fcf5ef2aSThomas Huth     pc_start = tb->pc;
1649fcf5ef2aSThomas Huth     dc->cpu = cpu;
1650fcf5ef2aSThomas Huth     dc->tb = tb;
1651fcf5ef2aSThomas Huth     org_flags = dc->synced_flags = dc->tb_flags = tb->flags;
1652fcf5ef2aSThomas Huth 
1653fcf5ef2aSThomas Huth     dc->is_jmp = DISAS_NEXT;
1654fcf5ef2aSThomas Huth     dc->jmp = 0;
1655fcf5ef2aSThomas Huth     dc->delayed_branch = !!(dc->tb_flags & D_FLAG);
1656fcf5ef2aSThomas Huth     if (dc->delayed_branch) {
1657fcf5ef2aSThomas Huth         dc->jmp = JMP_INDIRECT;
1658fcf5ef2aSThomas Huth     }
1659fcf5ef2aSThomas Huth     dc->pc = pc_start;
1660fcf5ef2aSThomas Huth     dc->singlestep_enabled = cs->singlestep_enabled;
1661fcf5ef2aSThomas Huth     dc->cpustate_changed = 0;
1662fcf5ef2aSThomas Huth     dc->abort_at_next_insn = 0;
1663fcf5ef2aSThomas Huth 
1664fcf5ef2aSThomas Huth     if (pc_start & 3) {
1665fcf5ef2aSThomas Huth         cpu_abort(cs, "Microblaze: unaligned PC=%x\n", pc_start);
1666fcf5ef2aSThomas Huth     }
1667fcf5ef2aSThomas Huth 
166856371527SEmilio G. Cota     page_start = pc_start & TARGET_PAGE_MASK;
1669fcf5ef2aSThomas Huth     num_insns = 0;
1670fcf5ef2aSThomas Huth 
1671fcf5ef2aSThomas Huth     gen_tb_start(tb);
1672fcf5ef2aSThomas Huth     do
1673fcf5ef2aSThomas Huth     {
1674fcf5ef2aSThomas Huth         tcg_gen_insn_start(dc->pc);
1675fcf5ef2aSThomas Huth         num_insns++;
1676fcf5ef2aSThomas Huth 
1677fcf5ef2aSThomas Huth         if (unlikely(cpu_breakpoint_test(cs, dc->pc, BP_ANY))) {
167841ba37c4SRichard Henderson             gen_raise_exception_sync(dc, EXCP_DEBUG);
1679fcf5ef2aSThomas Huth             /* The address covered by the breakpoint must be included in
1680fcf5ef2aSThomas Huth                [tb->pc, tb->pc + tb->size) in order to for it to be
1681fcf5ef2aSThomas Huth                properly cleared -- thus we increment the PC here so that
1682fcf5ef2aSThomas Huth                the logic setting tb->size below does the right thing.  */
1683fcf5ef2aSThomas Huth             dc->pc += 4;
1684fcf5ef2aSThomas Huth             break;
1685fcf5ef2aSThomas Huth         }
1686fcf5ef2aSThomas Huth 
1687fcf5ef2aSThomas Huth         /* Pretty disas.  */
1688fcf5ef2aSThomas Huth         LOG_DIS("%8.8x:\t", dc->pc);
1689fcf5ef2aSThomas Huth 
1690c5a49c63SEmilio G. Cota         if (num_insns == max_insns && (tb_cflags(tb) & CF_LAST_IO)) {
1691fcf5ef2aSThomas Huth             gen_io_start();
1692fcf5ef2aSThomas Huth         }
1693fcf5ef2aSThomas Huth 
1694fcf5ef2aSThomas Huth         dc->clear_imm = 1;
1695fcf5ef2aSThomas Huth         decode(dc, cpu_ldl_code(env, dc->pc));
1696fcf5ef2aSThomas Huth         if (dc->clear_imm)
1697fcf5ef2aSThomas Huth             dc->tb_flags &= ~IMM_FLAG;
1698fcf5ef2aSThomas Huth         dc->pc += 4;
1699fcf5ef2aSThomas Huth 
1700fcf5ef2aSThomas Huth         if (dc->delayed_branch) {
1701fcf5ef2aSThomas Huth             dc->delayed_branch--;
1702fcf5ef2aSThomas Huth             if (!dc->delayed_branch) {
1703fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTI_FLAG)
1704fcf5ef2aSThomas Huth                     do_rti(dc);
1705fcf5ef2aSThomas Huth                  if (dc->tb_flags & DRTB_FLAG)
1706fcf5ef2aSThomas Huth                     do_rtb(dc);
1707fcf5ef2aSThomas Huth                 if (dc->tb_flags & DRTE_FLAG)
1708fcf5ef2aSThomas Huth                     do_rte(dc);
1709fcf5ef2aSThomas Huth                 /* Clear the delay slot flag.  */
1710fcf5ef2aSThomas Huth                 dc->tb_flags &= ~D_FLAG;
1711fcf5ef2aSThomas Huth                 /* If it is a direct jump, try direct chaining.  */
1712fcf5ef2aSThomas Huth                 if (dc->jmp == JMP_INDIRECT) {
17130f96e96bSRichard Henderson                     TCGv_i32 tmp_pc = tcg_const_i32(dc->pc);
17140f96e96bSRichard Henderson                     eval_cond_jmp(dc, cpu_btarget, tmp_pc);
17150f96e96bSRichard Henderson                     tcg_temp_free_i32(tmp_pc);
1716fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_JUMP;
1717fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT) {
1718fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1719fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1720fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1721fcf5ef2aSThomas Huth                 } else if (dc->jmp == JMP_DIRECT_CC) {
1722fcf5ef2aSThomas Huth                     TCGLabel *l1 = gen_new_label();
1723fcf5ef2aSThomas Huth                     t_sync_flags(dc);
1724fcf5ef2aSThomas Huth                     /* Conditional jmp.  */
1725*9b158558SRichard Henderson                     tcg_gen_brcondi_i32(TCG_COND_NE, cpu_btaken, 0, l1);
1726fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 1, dc->pc);
1727fcf5ef2aSThomas Huth                     gen_set_label(l1);
1728fcf5ef2aSThomas Huth                     gen_goto_tb(dc, 0, dc->jmp_pc);
1729fcf5ef2aSThomas Huth 
1730fcf5ef2aSThomas Huth                     dc->is_jmp = DISAS_TB_JUMP;
1731fcf5ef2aSThomas Huth                 }
1732fcf5ef2aSThomas Huth                 break;
1733fcf5ef2aSThomas Huth             }
1734fcf5ef2aSThomas Huth         }
1735fcf5ef2aSThomas Huth         if (cs->singlestep_enabled) {
1736fcf5ef2aSThomas Huth             break;
1737fcf5ef2aSThomas Huth         }
1738fcf5ef2aSThomas Huth     } while (!dc->is_jmp && !dc->cpustate_changed
1739fcf5ef2aSThomas Huth              && !tcg_op_buf_full()
1740fcf5ef2aSThomas Huth              && !singlestep
174156371527SEmilio G. Cota              && (dc->pc - page_start < TARGET_PAGE_SIZE)
1742fcf5ef2aSThomas Huth              && num_insns < max_insns);
1743fcf5ef2aSThomas Huth 
1744fcf5ef2aSThomas Huth     npc = dc->pc;
1745fcf5ef2aSThomas Huth     if (dc->jmp == JMP_DIRECT || dc->jmp == JMP_DIRECT_CC) {
1746fcf5ef2aSThomas Huth         if (dc->tb_flags & D_FLAG) {
1747fcf5ef2aSThomas Huth             dc->is_jmp = DISAS_UPDATE;
17480f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1749fcf5ef2aSThomas Huth             sync_jmpstate(dc);
1750fcf5ef2aSThomas Huth         } else
1751fcf5ef2aSThomas Huth             npc = dc->jmp_pc;
1752fcf5ef2aSThomas Huth     }
1753fcf5ef2aSThomas Huth 
1754fcf5ef2aSThomas Huth     /* Force an update if the per-tb cpu state has changed.  */
1755fcf5ef2aSThomas Huth     if (dc->is_jmp == DISAS_NEXT
1756fcf5ef2aSThomas Huth         && (dc->cpustate_changed || org_flags != dc->tb_flags)) {
1757fcf5ef2aSThomas Huth         dc->is_jmp = DISAS_UPDATE;
17580f96e96bSRichard Henderson         tcg_gen_movi_i32(cpu_pc, npc);
1759fcf5ef2aSThomas Huth     }
1760fcf5ef2aSThomas Huth     t_sync_flags(dc);
1761fcf5ef2aSThomas Huth 
1762fcf5ef2aSThomas Huth     if (unlikely(cs->singlestep_enabled)) {
1763fcf5ef2aSThomas Huth         TCGv_i32 tmp = tcg_const_i32(EXCP_DEBUG);
1764fcf5ef2aSThomas Huth 
1765fcf5ef2aSThomas Huth         if (dc->is_jmp != DISAS_JUMP) {
17660f96e96bSRichard Henderson             tcg_gen_movi_i32(cpu_pc, npc);
1767fcf5ef2aSThomas Huth         }
1768fcf5ef2aSThomas Huth         gen_helper_raise_exception(cpu_env, tmp);
1769fcf5ef2aSThomas Huth         tcg_temp_free_i32(tmp);
1770fcf5ef2aSThomas Huth     } else {
1771fcf5ef2aSThomas Huth         switch(dc->is_jmp) {
1772fcf5ef2aSThomas Huth             case DISAS_NEXT:
1773fcf5ef2aSThomas Huth                 gen_goto_tb(dc, 1, npc);
1774fcf5ef2aSThomas Huth                 break;
1775fcf5ef2aSThomas Huth             default:
1776fcf5ef2aSThomas Huth             case DISAS_JUMP:
1777fcf5ef2aSThomas Huth             case DISAS_UPDATE:
1778fcf5ef2aSThomas Huth                 /* indicate that the hash table must be used
1779fcf5ef2aSThomas Huth                    to find the next TB */
178007ea28b4SRichard Henderson                 tcg_gen_exit_tb(NULL, 0);
1781fcf5ef2aSThomas Huth                 break;
1782fcf5ef2aSThomas Huth             case DISAS_TB_JUMP:
1783fcf5ef2aSThomas Huth                 /* nothing more to generate */
1784fcf5ef2aSThomas Huth                 break;
1785fcf5ef2aSThomas Huth         }
1786fcf5ef2aSThomas Huth     }
1787fcf5ef2aSThomas Huth     gen_tb_end(tb, num_insns);
1788fcf5ef2aSThomas Huth 
1789fcf5ef2aSThomas Huth     tb->size = dc->pc - pc_start;
1790fcf5ef2aSThomas Huth     tb->icount = num_insns;
1791fcf5ef2aSThomas Huth 
1792fcf5ef2aSThomas Huth #ifdef DEBUG_DISAS
1793fcf5ef2aSThomas Huth #if !SIM_COMPAT
1794fcf5ef2aSThomas Huth     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)
1795fcf5ef2aSThomas Huth         && qemu_log_in_addr_range(pc_start)) {
1796fc59d2d8SRobert Foley         FILE *logfile = qemu_log_lock();
1797fcf5ef2aSThomas Huth         qemu_log("--------------\n");
17981d48474dSRichard Henderson         log_target_disas(cs, pc_start, dc->pc - pc_start);
1799fc59d2d8SRobert Foley         qemu_log_unlock(logfile);
1800fcf5ef2aSThomas Huth     }
1801fcf5ef2aSThomas Huth #endif
1802fcf5ef2aSThomas Huth #endif
1803fcf5ef2aSThomas Huth     assert(!dc->abort_at_next_insn);
1804fcf5ef2aSThomas Huth }
1805fcf5ef2aSThomas Huth 
180690c84c56SMarkus Armbruster void mb_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1807fcf5ef2aSThomas Huth {
1808fcf5ef2aSThomas Huth     MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
1809fcf5ef2aSThomas Huth     CPUMBState *env = &cpu->env;
1810fcf5ef2aSThomas Huth     int i;
1811fcf5ef2aSThomas Huth 
181290c84c56SMarkus Armbruster     if (!env) {
1813fcf5ef2aSThomas Huth         return;
181490c84c56SMarkus Armbruster     }
1815fcf5ef2aSThomas Huth 
18160f96e96bSRichard Henderson     qemu_fprintf(f, "IN: PC=%x %s\n",
181776e8187dSRichard Henderson                  env->pc, lookup_symbol(env->pc));
18186efd5599SRichard Henderson     qemu_fprintf(f, "rmsr=%x resr=%x rear=%" PRIx64 " "
1819eb2022b7SRichard Henderson                  "imm=%x iflags=%x fsr=%x rbtr=%x\n",
182078e9caf2SRichard Henderson                  env->msr, env->esr, env->ear,
1821eb2022b7SRichard Henderson                  env->imm, env->iflags, env->fsr, env->btr);
18220f96e96bSRichard Henderson     qemu_fprintf(f, "btaken=%d btarget=%x mode=%s(saved=%s) eip=%d ie=%d\n",
1823fcf5ef2aSThomas Huth                  env->btaken, env->btarget,
18242e5282caSRichard Henderson                  (env->msr & MSR_UM) ? "user" : "kernel",
18252e5282caSRichard Henderson                  (env->msr & MSR_UMS) ? "user" : "kernel",
18262e5282caSRichard Henderson                  (bool)(env->msr & MSR_EIP),
18272e5282caSRichard Henderson                  (bool)(env->msr & MSR_IE));
18282ead1b18SJoe Komlodi     for (i = 0; i < 12; i++) {
18292ead1b18SJoe Komlodi         qemu_fprintf(f, "rpvr%2.2d=%8.8x ", i, env->pvr.regs[i]);
18302ead1b18SJoe Komlodi         if ((i + 1) % 4 == 0) {
18312ead1b18SJoe Komlodi             qemu_fprintf(f, "\n");
18322ead1b18SJoe Komlodi         }
18332ead1b18SJoe Komlodi     }
1834fcf5ef2aSThomas Huth 
18352ead1b18SJoe Komlodi     /* Registers that aren't modeled are reported as 0 */
183639db007eSRichard Henderson     qemu_fprintf(f, "redr=%x rpid=0 rzpr=0 rtlbx=0 rtlbsx=0 "
1837af20a93aSRichard Henderson                     "rtlblo=0 rtlbhi=0\n", env->edr);
18382ead1b18SJoe Komlodi     qemu_fprintf(f, "slr=%x shr=%x\n", env->slr, env->shr);
1839fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
184090c84c56SMarkus Armbruster         qemu_fprintf(f, "r%2.2d=%8.8x ", i, env->regs[i]);
1841fcf5ef2aSThomas Huth         if ((i + 1) % 4 == 0)
184290c84c56SMarkus Armbruster             qemu_fprintf(f, "\n");
1843fcf5ef2aSThomas Huth         }
184490c84c56SMarkus Armbruster     qemu_fprintf(f, "\n\n");
1845fcf5ef2aSThomas Huth }
1846fcf5ef2aSThomas Huth 
1847fcf5ef2aSThomas Huth void mb_tcg_init(void)
1848fcf5ef2aSThomas Huth {
1849fcf5ef2aSThomas Huth     int i;
1850fcf5ef2aSThomas Huth 
1851*9b158558SRichard Henderson     cpu_iflags = tcg_global_mem_new_i32(cpu_env,
1852fcf5ef2aSThomas Huth                     offsetof(CPUMBState, iflags),
1853fcf5ef2aSThomas Huth                     "iflags");
1854*9b158558SRichard Henderson     cpu_imm = tcg_global_mem_new_i32(cpu_env,
1855fcf5ef2aSThomas Huth                     offsetof(CPUMBState, imm),
1856fcf5ef2aSThomas Huth                     "imm");
18570f96e96bSRichard Henderson     cpu_btarget = tcg_global_mem_new_i32(cpu_env,
1858fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btarget),
1859fcf5ef2aSThomas Huth                      "btarget");
1860*9b158558SRichard Henderson     cpu_btaken = tcg_global_mem_new_i32(cpu_env,
1861fcf5ef2aSThomas Huth                      offsetof(CPUMBState, btaken),
1862fcf5ef2aSThomas Huth                      "btaken");
1863*9b158558SRichard Henderson     cpu_res_addr = tcg_global_mem_new(cpu_env,
1864fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_addr),
1865fcf5ef2aSThomas Huth                      "res_addr");
1866*9b158558SRichard Henderson     cpu_res_val = tcg_global_mem_new_i32(cpu_env,
1867fcf5ef2aSThomas Huth                      offsetof(CPUMBState, res_val),
1868fcf5ef2aSThomas Huth                      "res_val");
1869fcf5ef2aSThomas Huth     for (i = 0; i < ARRAY_SIZE(cpu_R); i++) {
1870cfeea807SEdgar E. Iglesias         cpu_R[i] = tcg_global_mem_new_i32(cpu_env,
1871fcf5ef2aSThomas Huth                           offsetof(CPUMBState, regs[i]),
1872fcf5ef2aSThomas Huth                           regnames[i]);
1873fcf5ef2aSThomas Huth     }
187476e8187dSRichard Henderson 
1875aa28e6d4SRichard Henderson     cpu_pc =
18760f96e96bSRichard Henderson         tcg_global_mem_new_i32(cpu_env, offsetof(CPUMBState, pc), "rpc");
1877aa28e6d4SRichard Henderson     cpu_msr =
18783e0e16aeSRichard Henderson         tcg_global_mem_new_i32(cpu_env, offsetof(CPUMBState, msr), "rmsr");
1879fcf5ef2aSThomas Huth }
1880fcf5ef2aSThomas Huth 
1881fcf5ef2aSThomas Huth void restore_state_to_opc(CPUMBState *env, TranslationBlock *tb,
1882fcf5ef2aSThomas Huth                           target_ulong *data)
1883fcf5ef2aSThomas Huth {
188476e8187dSRichard Henderson     env->pc = data[0];
1885fcf5ef2aSThomas Huth }
1886