2 * MIPS32 emulation for qemu: main translation routines.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define MIPS_DEBUG_DISAS
32 //#define MIPS_SINGLE_STEP
35 #define DEF(s, n, copy_size) INDEX_op_ ## s,
41 static uint16_t *gen_opc_ptr;
42 static uint32_t *gen_opparam_ptr;
47 #define EXT_SPECIAL 0x100
48 #define EXT_SPECIAL2 0x200
49 #define EXT_REGIMM 0x300
56 /* indirect opcode tables */
64 /* arithmetic with immediate */
73 /* Jump and branches */
76 OPC_BEQ = 0x04, /* Unconditional if rs = rt = 0 (B) */
84 OPC_JALX = 0x1D, /* MIPS 16 only */
100 /* Floating point load/store */
109 /* Cache and prefetch */
114 /* MIPS special opcodes */
117 OPC_SLL = 0x00 | EXT_SPECIAL,
118 /* NOP is SLL r0, r0, 0 */
119 /* SSNOP is SLL r0, r0, 1 */
120 OPC_SRL = 0x02 | EXT_SPECIAL,
121 OPC_SRA = 0x03 | EXT_SPECIAL,
122 OPC_SLLV = 0x04 | EXT_SPECIAL,
123 OPC_SRLV = 0x06 | EXT_SPECIAL,
124 OPC_SRAV = 0x07 | EXT_SPECIAL,
125 /* Multiplication / division */
126 OPC_MULT = 0x18 | EXT_SPECIAL,
127 OPC_MULTU = 0x19 | EXT_SPECIAL,
128 OPC_DIV = 0x1A | EXT_SPECIAL,
129 OPC_DIVU = 0x1B | EXT_SPECIAL,
130 /* 2 registers arithmetic / logic */
131 OPC_ADD = 0x20 | EXT_SPECIAL,
132 OPC_ADDU = 0x21 | EXT_SPECIAL,
133 OPC_SUB = 0x22 | EXT_SPECIAL,
134 OPC_SUBU = 0x23 | EXT_SPECIAL,
135 OPC_AND = 0x24 | EXT_SPECIAL,
136 OPC_OR = 0x25 | EXT_SPECIAL,
137 OPC_XOR = 0x26 | EXT_SPECIAL,
138 OPC_NOR = 0x27 | EXT_SPECIAL,
139 OPC_SLT = 0x2A | EXT_SPECIAL,
140 OPC_SLTU = 0x2B | EXT_SPECIAL,
142 OPC_JR = 0x08 | EXT_SPECIAL,
143 OPC_JALR = 0x09 | EXT_SPECIAL,
145 OPC_TGE = 0x30 | EXT_SPECIAL,
146 OPC_TGEU = 0x31 | EXT_SPECIAL,
147 OPC_TLT = 0x32 | EXT_SPECIAL,
148 OPC_TLTU = 0x33 | EXT_SPECIAL,
149 OPC_TEQ = 0x34 | EXT_SPECIAL,
150 OPC_TNE = 0x36 | EXT_SPECIAL,
151 /* HI / LO registers load & stores */
152 OPC_MFHI = 0x10 | EXT_SPECIAL,
153 OPC_MTHI = 0x11 | EXT_SPECIAL,
154 OPC_MFLO = 0x12 | EXT_SPECIAL,
155 OPC_MTLO = 0x13 | EXT_SPECIAL,
156 /* Conditional moves */
157 OPC_MOVZ = 0x0A | EXT_SPECIAL,
158 OPC_MOVN = 0x0B | EXT_SPECIAL,
160 OPC_MOVCI = 0x01 | EXT_SPECIAL,
163 OPC_PMON = 0x05 | EXT_SPECIAL,
164 OPC_SYSCALL = 0x0C | EXT_SPECIAL,
165 OPC_BREAK = 0x0D | EXT_SPECIAL,
166 OPC_SYNC = 0x0F | EXT_SPECIAL,
170 /* Mutiply & xxx operations */
171 OPC_MADD = 0x00 | EXT_SPECIAL2,
172 OPC_MADDU = 0x01 | EXT_SPECIAL2,
173 OPC_MUL = 0x02 | EXT_SPECIAL2,
174 OPC_MSUB = 0x04 | EXT_SPECIAL2,
175 OPC_MSUBU = 0x05 | EXT_SPECIAL2,
177 OPC_CLZ = 0x20 | EXT_SPECIAL2,
178 OPC_CLO = 0x21 | EXT_SPECIAL2,
180 OPC_SDBBP = 0x3F | EXT_SPECIAL2,
185 OPC_BLTZ = 0x00 | EXT_REGIMM,
186 OPC_BLTZL = 0x02 | EXT_REGIMM,
187 OPC_BGEZ = 0x01 | EXT_REGIMM,
188 OPC_BGEZL = 0x03 | EXT_REGIMM,
189 OPC_BLTZAL = 0x10 | EXT_REGIMM,
190 OPC_BLTZALL = 0x12 | EXT_REGIMM,
191 OPC_BGEZAL = 0x11 | EXT_REGIMM,
192 OPC_BGEZALL = 0x13 | EXT_REGIMM,
193 OPC_TGEI = 0x08 | EXT_REGIMM,
194 OPC_TGEIU = 0x09 | EXT_REGIMM,
195 OPC_TLTI = 0x0A | EXT_REGIMM,
196 OPC_TLTIU = 0x0B | EXT_REGIMM,
197 OPC_TEQI = 0x0C | EXT_REGIMM,
198 OPC_TNEI = 0x0E | EXT_REGIMM,
202 /* Coprocessor 0 (MMU) */
203 OPC_MFC0 = 0x00 | EXT_CP0,
204 OPC_MTC0 = 0x04 | EXT_CP0,
205 OPC_TLBR = 0x01 | EXT_CP0,
206 OPC_TLBWI = 0x02 | EXT_CP0,
207 OPC_TLBWR = 0x06 | EXT_CP0,
208 OPC_TLBP = 0x08 | EXT_CP0,
209 OPC_ERET = 0x18 | EXT_CP0,
210 OPC_DERET = 0x1F | EXT_CP0,
211 OPC_WAIT = 0x20 | EXT_CP0,
214 const unsigned char *regnames[] =
215 { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
216 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
217 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
218 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
220 /* Warning: no function for r0 register (hard wired to zero) */
221 #define GEN32(func, NAME) \
222 static GenOpFunc *NAME ## _table [32] = { \
223 NULL, NAME ## 1, NAME ## 2, NAME ## 3, \
224 NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \
225 NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \
226 NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \
227 NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19, \
228 NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23, \
229 NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27, \
230 NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31, \
232 static inline void func(int n) \
234 NAME ## _table[n](); \
237 /* General purpose registers moves */
238 GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
239 GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
240 GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);
242 GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
243 GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
245 typedef struct DisasContext {
246 struct TranslationBlock *tb;
247 target_ulong pc, saved_pc;
249 /* Routine used to access memory */
251 uint32_t hflags, saved_hflags;
254 target_ulong btarget;
258 BS_NONE = 0, /* We go out of the TB without reaching a branch or an
259 * exception condition
261 BS_STOP = 1, /* We want to stop translation for any reason */
262 BS_BRANCH = 2, /* We reached a branch condition */
263 BS_EXCP = 3, /* We reached an exception condition */
266 #if defined MIPS_DEBUG_DISAS
267 #define MIPS_DEBUG(fmt, args...) \
269 if (loglevel & CPU_LOG_TB_IN_ASM) { \
270 fprintf(logfile, "%08x: %08x " fmt "\n", \
271 ctx->pc, ctx->opcode , ##args); \
275 #define MIPS_DEBUG(fmt, args...) do { } while(0)
278 #define MIPS_INVAL(op) \
280 MIPS_DEBUG("Invalid %s %03x %03x %03x", op, ctx->opcode >> 26, \
281 ctx->opcode & 0x3F, ((ctx->opcode >> 16) & 0x1F)); \
284 #define GEN_LOAD_REG_TN(Tn, Rn) \
287 glue(gen_op_reset_, Tn)(); \
289 glue(gen_op_load_gpr_, Tn)(Rn); \
293 #define GEN_LOAD_IMM_TN(Tn, Imm) \
296 glue(gen_op_reset_, Tn)(); \
298 glue(gen_op_set_, Tn)(Imm); \
302 #define GEN_STORE_TN_REG(Rn, Tn) \
305 glue(glue(gen_op_store_, Tn),_gpr)(Rn); \
309 static inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
311 #if defined MIPS_DEBUG_DISAS
312 if (loglevel & CPU_LOG_TB_IN_ASM) {
313 fprintf(logfile, "hflags %08x saved %08x\n",
314 ctx->hflags, ctx->saved_hflags);
317 if (do_save_pc && ctx->pc != ctx->saved_pc) {
318 gen_op_save_pc(ctx->pc);
319 ctx->saved_pc = ctx->pc;
321 if (ctx->hflags != ctx->saved_hflags) {
322 gen_op_save_state(ctx->hflags);
323 ctx->saved_hflags = ctx->hflags;
324 if (ctx->hflags & MIPS_HFLAG_BR) {
325 gen_op_save_breg_target();
326 } else if (ctx->hflags & MIPS_HFLAG_B) {
327 gen_op_save_btarget(ctx->btarget);
328 } else if (ctx->hflags & MIPS_HFLAG_BMASK) {
330 gen_op_save_btarget(ctx->btarget);
335 static inline void generate_exception (DisasContext *ctx, int excp)
337 #if defined MIPS_DEBUG_DISAS
338 if (loglevel & CPU_LOG_TB_IN_ASM)
339 fprintf(logfile, "%s: raise exception %d\n", __func__, excp);
341 save_cpu_state(ctx, 1);
342 gen_op_raise_exception(excp);
343 ctx->bstate = BS_EXCP;
346 #if defined(CONFIG_USER_ONLY)
347 #define op_ldst(name) gen_op_##name##_raw()
348 #define OP_LD_TABLE(width)
349 #define OP_ST_TABLE(width)
351 #define op_ldst(name) (*gen_op_##name[ctx->mem_idx])()
352 #define OP_LD_TABLE(width) \
353 static GenOpFunc *gen_op_l##width[] = { \
354 &gen_op_l##width##_user, \
355 &gen_op_l##width##_kernel, \
357 #define OP_ST_TABLE(width) \
358 static GenOpFunc *gen_op_s##width[] = { \
359 &gen_op_s##width##_user, \
360 &gen_op_s##width##_kernel, \
388 static void gen_ldst (DisasContext *ctx, uint16_t opc, int rt,
389 int base, int16_t offset)
391 const unsigned char *opn = "unk";
394 GEN_LOAD_IMM_TN(T0, offset);
395 } else if (offset == 0) {
396 gen_op_load_gpr_T0(base);
398 gen_op_load_gpr_T0(base);
399 gen_op_set_T1(offset);
402 /* Don't do NOP if destination is zero: we must perform the actual
406 #if defined(TARGET_MIPS64)
408 #if defined (MIPS_HAS_UNALIGNED_LS)
412 GEN_STORE_TN_REG(rt, T0);
416 #if defined (MIPS_HAS_UNALIGNED_LS)
419 GEN_LOAD_REG_TN(T1, rt);
425 GEN_STORE_TN_REG(rt, T0);
429 GEN_LOAD_REG_TN(T1, rt);
435 GEN_STORE_TN_REG(rt, T0);
439 GEN_LOAD_REG_TN(T1, rt);
445 #if defined (MIPS_HAS_UNALIGNED_LS)
449 GEN_STORE_TN_REG(rt, T0);
453 #if defined (MIPS_HAS_UNALIGNED_LS)
456 GEN_LOAD_REG_TN(T1, rt);
461 #if defined (MIPS_HAS_UNALIGNED_LS)
465 GEN_STORE_TN_REG(rt, T0);
469 #if defined (MIPS_HAS_UNALIGNED_LS)
472 GEN_LOAD_REG_TN(T1, rt);
477 #if defined (MIPS_HAS_UNALIGNED_LS)
481 GEN_STORE_TN_REG(rt, T0);
486 GEN_STORE_TN_REG(rt, T0);
490 GEN_LOAD_REG_TN(T1, rt);
496 GEN_STORE_TN_REG(rt, T0);
500 GEN_LOAD_REG_TN(T1, rt);
502 GEN_STORE_TN_REG(rt, T0);
506 GEN_LOAD_REG_TN(T1, rt);
511 GEN_LOAD_REG_TN(T1, rt);
513 GEN_STORE_TN_REG(rt, T0);
517 GEN_LOAD_REG_TN(T1, rt);
523 GEN_STORE_TN_REG(rt, T0);
527 GEN_LOAD_REG_TN(T1, rt);
529 GEN_STORE_TN_REG(rt, T0);
533 MIPS_INVAL("load/store");
534 generate_exception(ctx, EXCP_RI);
537 MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
540 /* Arithmetic with immediate operand */
541 static void gen_arith_imm (DisasContext *ctx, uint16_t opc, int rt,
545 const unsigned char *opn = "unk";
547 if (rt == 0 && opc != OPC_ADDI) {
548 /* if no destination, treat it as a NOP
549 * For addi, we must generate the overflow exception when needed.
554 if (opc == OPC_ADDI || opc == OPC_ADDIU ||
555 opc == OPC_SLTI || opc == OPC_SLTIU)
556 uimm = (int32_t)imm; /* Sign extent to 32 bits */
558 uimm = (uint16_t)imm;
559 if (opc != OPC_LUI) {
560 GEN_LOAD_REG_TN(T0, rs);
561 GEN_LOAD_IMM_TN(T1, uimm);
564 GEN_LOAD_IMM_TN(T0, uimm);
568 save_cpu_state(ctx, 1);
612 MIPS_INVAL("imm arith");
613 generate_exception(ctx, EXCP_RI);
616 GEN_STORE_TN_REG(rt, T0);
617 MIPS_DEBUG("%s %s, %s, %x", opn, regnames[rt], regnames[rs], uimm);
621 static void gen_arith (DisasContext *ctx, uint16_t opc,
622 int rd, int rs, int rt)
624 const unsigned char *opn = "unk";
626 if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB) {
627 /* if no destination, treat it as a NOP
628 * For add & sub, we must generate the overflow exception when needed.
633 GEN_LOAD_REG_TN(T0, rs);
634 GEN_LOAD_REG_TN(T1, rt);
637 save_cpu_state(ctx, 1);
646 save_cpu_state(ctx, 1);
704 generate_exception(ctx, EXCP_RI);
707 GEN_STORE_TN_REG(rd, T0);
709 MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
712 /* Arithmetic on HI/LO registers */
713 static void gen_HILO (DisasContext *ctx, uint16_t opc, int reg)
715 const unsigned char *opn = "unk";
717 if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
725 GEN_STORE_TN_REG(reg, T0);
730 GEN_STORE_TN_REG(reg, T0);
734 GEN_LOAD_REG_TN(T0, reg);
739 GEN_LOAD_REG_TN(T0, reg);
745 generate_exception(ctx, EXCP_RI);
748 MIPS_DEBUG("%s %s", opn, regnames[reg]);
751 static void gen_muldiv (DisasContext *ctx, uint16_t opc,
754 const unsigned char *opn = "unk";
756 GEN_LOAD_REG_TN(T0, rs);
757 GEN_LOAD_REG_TN(T1, rt);
792 MIPS_INVAL("mul/div");
793 generate_exception(ctx, EXCP_RI);
796 MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
799 static void gen_cl (DisasContext *ctx, uint16_t opc,
802 const unsigned char *opn = "unk";
808 GEN_LOAD_REG_TN(T0, rs);
822 generate_exception(ctx, EXCP_RI);
825 gen_op_store_T0_gpr(rd);
826 MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
830 static void gen_trap (DisasContext *ctx, uint16_t opc,
831 int rs, int rt, int16_t imm)
836 /* Load needed operands */
844 /* Compare two registers */
846 GEN_LOAD_REG_TN(T0, rs);
847 GEN_LOAD_REG_TN(T1, rt);
856 /* Compare register to immediate */
857 if (rs != 0 || imm != 0) {
858 GEN_LOAD_REG_TN(T0, rs);
859 GEN_LOAD_IMM_TN(T1, (int32_t)imm);
866 case OPC_TEQ: /* rs == rs */
867 case OPC_TEQI: /* r0 == 0 */
868 case OPC_TGE: /* rs >= rs */
869 case OPC_TGEI: /* r0 >= 0 */
870 case OPC_TGEU: /* rs >= rs unsigned */
871 case OPC_TGEIU: /* r0 >= 0 unsigned */
875 case OPC_TLT: /* rs < rs */
876 case OPC_TLTI: /* r0 < 0 */
877 case OPC_TLTU: /* rs < rs unsigned */
878 case OPC_TLTIU: /* r0 < 0 unsigned */
879 case OPC_TNE: /* rs != rs */
880 case OPC_TNEI: /* r0 != 0 */
881 /* Never trap: treat as NOP */
885 generate_exception(ctx, EXCP_RI);
916 generate_exception(ctx, EXCP_RI);
920 save_cpu_state(ctx, 1);
922 ctx->bstate = BS_STOP;
925 /* Branches (before delay slot) */
926 static void gen_compute_branch (DisasContext *ctx, uint16_t opc,
927 int rs, int rt, int32_t offset)
929 target_ulong btarget;
935 /* Load needed operands */
941 /* Compare two registers */
943 GEN_LOAD_REG_TN(T0, rs);
944 GEN_LOAD_REG_TN(T1, rt);
947 btarget = ctx->pc + 4 + offset;
961 /* Compare to zero */
963 gen_op_load_gpr_T0(rs);
966 btarget = ctx->pc + 4 + offset;
970 /* Jump to immediate */
971 btarget = ((ctx->pc + 4) & 0xF0000000) | offset;
975 /* Jump to register */
977 /* Only hint = 0 is valid */
978 generate_exception(ctx, EXCP_RI);
981 GEN_LOAD_REG_TN(T2, rs);
984 MIPS_INVAL("branch/jump");
985 generate_exception(ctx, EXCP_RI);
989 /* No condition to be computed */
991 case OPC_BEQ: /* rx == rx */
992 case OPC_BEQL: /* rx == rx likely */
993 case OPC_BGEZ: /* 0 >= 0 */
994 case OPC_BGEZL: /* 0 >= 0 likely */
995 case OPC_BLEZ: /* 0 <= 0 */
996 case OPC_BLEZL: /* 0 <= 0 likely */
998 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
999 MIPS_DEBUG("balways");
1001 case OPC_BGEZAL: /* 0 >= 0 */
1002 case OPC_BGEZALL: /* 0 >= 0 likely */
1003 /* Always take and link */
1005 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
1006 MIPS_DEBUG("balways and link");
1008 case OPC_BNE: /* rx != rx */
1009 case OPC_BGTZ: /* 0 > 0 */
1010 case OPC_BLTZ: /* 0 < 0 */
1011 case OPC_BLTZAL: /* 0 < 0 */
1012 /* Treated as NOP */
1013 MIPS_DEBUG("bnever (NOP)");
1015 case OPC_BNEL: /* rx != rx likely */
1016 case OPC_BGTZL: /* 0 > 0 likely */
1017 case OPC_BLTZALL: /* 0 < 0 likely */
1018 case OPC_BLTZL: /* 0 < 0 likely */
1019 /* Skip the instruction in the delay slot */
1020 MIPS_DEBUG("bnever and skip");
1021 gen_op_branch((long)ctx->tb, ctx->pc + 4);
1024 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
1025 MIPS_DEBUG("j %08x", btarget);
1029 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
1030 MIPS_DEBUG("jal %08x", btarget);
1033 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BR;
1034 MIPS_DEBUG("jr %s", regnames[rs]);
1038 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BR;
1039 MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
1042 MIPS_INVAL("branch/jump");
1043 generate_exception(ctx, EXCP_RI);
1050 MIPS_DEBUG("beq %s, %s, %08x",
1051 regnames[rs], regnames[rt], btarget);
1055 MIPS_DEBUG("beql %s, %s, %08x",
1056 regnames[rs], regnames[rt], btarget);
1060 MIPS_DEBUG("bne %s, %s, %08x",
1061 regnames[rs], regnames[rt], btarget);
1065 MIPS_DEBUG("bnel %s, %s, %08x",
1066 regnames[rs], regnames[rt], btarget);
1070 MIPS_DEBUG("bgez %s, %08x", regnames[rs], btarget);
1074 MIPS_DEBUG("bgezl %s, %08x", regnames[rs], btarget);
1078 MIPS_DEBUG("bgezal %s, %08x", regnames[rs], btarget);
1084 MIPS_DEBUG("bgezall %s, %08x", regnames[rs], btarget);
1088 MIPS_DEBUG("bgtz %s, %08x", regnames[rs], btarget);
1092 MIPS_DEBUG("bgtzl %s, %08x", regnames[rs], btarget);
1096 MIPS_DEBUG("blez %s, %08x", regnames[rs], btarget);
1100 MIPS_DEBUG("blezl %s, %08x", regnames[rs], btarget);
1104 MIPS_DEBUG("bltz %s, %08x", regnames[rs], btarget);
1108 MIPS_DEBUG("bltzl %s, %08x", regnames[rs], btarget);
1113 MIPS_DEBUG("bltzal %s, %08x", regnames[rs], btarget);
1115 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BC;
1120 MIPS_DEBUG("bltzall %s, %08x", regnames[rs], btarget);
1122 ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BL;
1127 MIPS_DEBUG("enter ds: link %d cond %02x target %08x",
1128 blink, ctx->hflags, btarget);
1129 ctx->btarget = btarget;
1131 gen_op_set_T0(ctx->pc + 8);
1132 gen_op_store_T0_gpr(blink);
1137 /* CP0 (MMU and control) */
1138 static void gen_cp0 (DisasContext *ctx, uint16_t opc, int rt, int rd)
1140 const unsigned char *opn = "unk";
1142 if (!(ctx->CP0_Status & (1 << CP0St_CU0)) &&
1143 !(ctx->hflags & MIPS_HFLAG_UM) &&
1144 !(ctx->hflags & MIPS_HFLAG_ERL) &&
1145 !(ctx->hflags & MIPS_HFLAG_EXL)) {
1146 if (loglevel & CPU_LOG_TB_IN_ASM) {
1147 fprintf(logfile, "CP0 is not usable\n");
1149 gen_op_raise_exception_err(EXCP_CpU, 0);
1158 gen_op_mfc0(rd, ctx->opcode & 0x7);
1159 gen_op_store_T0_gpr(rt);
1163 /* If we get an exception, we want to restart at next instruction */
1165 save_cpu_state(ctx, 1);
1167 GEN_LOAD_REG_TN(T0, rt);
1168 gen_op_mtc0(rd, ctx->opcode & 0x7);
1169 /* Stop translation as we may have switched the execution mode */
1170 ctx->bstate = BS_STOP;
1173 #if defined(MIPS_USES_R4K_TLB)
1193 save_cpu_state(ctx, 0);
1195 ctx->bstate = BS_EXCP;
1199 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
1200 generate_exception(ctx, EXCP_RI);
1202 save_cpu_state(ctx, 0);
1204 ctx->bstate = BS_EXCP;
1207 /* XXX: TODO: WAIT */
1209 if (loglevel & CPU_LOG_TB_IN_ASM) {
1210 fprintf(logfile, "Invalid CP0 opcode: %08x %03x %03x %03x\n",
1211 ctx->opcode, ctx->opcode >> 26, ctx->opcode & 0x3F,
1212 ((ctx->opcode >> 16) & 0x1F));
1214 generate_exception(ctx, EXCP_RI);
1217 MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
1220 /* Coprocessor 1 (FPU) */
1222 /* ISA extensions */
1223 /* MIPS16 extension to MIPS32 */
1224 /* SmartMIPS extension to MIPS32 */
1226 #ifdef TARGET_MIPS64
1227 static void gen_arith64 (DisasContext *ctx, uint16_t opc)
1229 if (func == 0x02 && rd == 0) {
1233 if (rs == 0 || rt == 0) {
1237 gen_op_load_gpr_T0(rs);
1238 gen_op_load_gpr_T1(rt);
1251 /* Coprocessor 3 (FPU) */
1253 /* MDMX extension to MIPS64 */
1254 /* MIPS-3D extension to MIPS64 */
1258 static void decode_opc (DisasContext *ctx)
1265 if ((ctx->hflags & MIPS_HFLAG_DS) &&
1266 (ctx->hflags & MIPS_HFLAG_BL)) {
1267 /* Handle blikely not taken case */
1268 MIPS_DEBUG("blikely condition (%08x)", ctx->pc + 4);
1269 gen_op_blikely((long)ctx->tb, ctx->pc + 4,
1270 ctx->hflags & ~(MIPS_HFLAG_BMASK | MIPS_HFLAG_DS));
1272 op = ctx->opcode >> 26;
1273 rs = ((ctx->opcode >> 21) & 0x1F);
1274 rt = ((ctx->opcode >> 16) & 0x1F);
1275 rd = ((ctx->opcode >> 11) & 0x1F);
1276 sa = ((ctx->opcode >> 6) & 0x1F);
1277 imm = (int16_t)ctx->opcode;
1279 case 0x00: /* Special opcode */
1280 op1 = ctx->opcode & 0x3F;
1282 case 0x00: /* Arithmetic with immediate */
1284 gen_arith_imm(ctx, op1 | EXT_SPECIAL, rd, rt, sa);
1286 case 0x04: /* Arithmetic */
1291 gen_arith(ctx, op1 | EXT_SPECIAL, rd, rs, rt);
1293 case 0x18 ... 0x1B: /* MULT / DIV */
1294 gen_muldiv(ctx, op1 | EXT_SPECIAL, rs, rt);
1296 case 0x08 ... 0x09: /* Jumps */
1297 gen_compute_branch(ctx, op1 | EXT_SPECIAL, rs, rd, sa);
1299 case 0x30 ... 0x34: /* Traps */
1301 gen_trap(ctx, op1 | EXT_SPECIAL, rs, rt, -1);
1303 case 0x10: /* Move from HI/LO */
1305 gen_HILO(ctx, op1 | EXT_SPECIAL, rd);
1308 case 0x13: /* Move to HI/LO */
1309 gen_HILO(ctx, op1 | EXT_SPECIAL, rs);
1311 case 0x0C: /* SYSCALL */
1312 generate_exception(ctx, EXCP_SYSCALL);
1314 case 0x0D: /* BREAK */
1315 generate_exception(ctx, EXCP_BREAK);
1317 case 0x0F: /* SYNC */
1318 /* Treat as a noop */
1320 case 0x05: /* Pmon entry point */
1321 gen_op_pmon((ctx->opcode >> 6) & 0x1F);
1323 #if defined (MIPS_HAS_MOVCI)
1324 case 0x01: /* MOVCI */
1326 #if defined (TARGET_MIPS64)
1327 case 0x14: /* MIPS64 specific opcodes */
1336 default: /* Invalid */
1337 MIPS_INVAL("special");
1338 generate_exception(ctx, EXCP_RI);
1342 case 0x1C: /* Special2 opcode */
1343 op1 = ctx->opcode & 0x3F;
1345 #if defined (MIPS_USES_R4K_EXT)
1346 /* Those instructions are not part of MIPS32 core */
1347 case 0x00 ... 0x01: /* Multiply and add/sub */
1349 gen_muldiv(ctx, op1 | EXT_SPECIAL2, rs, rt);
1351 case 0x02: /* MUL */
1352 gen_arith(ctx, op1 | EXT_SPECIAL2, rd, rs, rt);
1354 case 0x20 ... 0x21: /* CLO / CLZ */
1355 gen_cl(ctx, op1 | EXT_SPECIAL2, rd, rs);
1358 case 0x3F: /* SDBBP */
1359 /* XXX: not clear which exception should be raised
1360 * when in debug mode...
1362 if (!(ctx->hflags & MIPS_HFLAG_DM)) {
1363 generate_exception(ctx, EXCP_DBp);
1365 generate_exception(ctx, EXCP_DBp);
1367 /* Treat as a noop */
1369 default: /* Invalid */
1370 MIPS_INVAL("special2");
1371 generate_exception(ctx, EXCP_RI);
1375 case 0x01: /* B REGIMM opcode */
1376 op1 = ((ctx->opcode >> 16) & 0x1F);
1378 case 0x00 ... 0x03: /* REGIMM branches */
1380 gen_compute_branch(ctx, op1 | EXT_REGIMM, rs, -1, imm << 2);
1382 case 0x08 ... 0x0C: /* Traps */
1384 gen_trap(ctx, op1 | EXT_REGIMM, rs, -1, imm);
1386 default: /* Invalid */
1387 MIPS_INVAL("REGIMM");
1388 generate_exception(ctx, EXCP_RI);
1392 case 0x10: /* CP0 opcode */
1393 op1 = ((ctx->opcode >> 21) & 0x1F);
1397 gen_cp0(ctx, op1 | EXT_CP0, rt, rd);
1400 gen_cp0(ctx, (ctx->opcode & 0x1F) | EXT_CP0, rt, rd);
1404 case 0x08 ... 0x0F: /* Arithmetic with immediate opcode */
1405 gen_arith_imm(ctx, op, rt, rs, imm);
1407 case 0x02 ... 0x03: /* Jump */
1408 offset = (int32_t)(ctx->opcode & 0x03FFFFFF) << 2;
1409 gen_compute_branch(ctx, op, rs, rt, offset);
1411 case 0x04 ... 0x07: /* Branch */
1413 gen_compute_branch(ctx, op, rs, rt, imm << 2);
1415 case 0x20 ... 0x26: /* Load and stores */
1419 gen_ldst(ctx, op, rt, rs, imm);
1421 case 0x2F: /* Cache operation */
1422 /* Treat as a noop */
1424 case 0x33: /* Prefetch */
1425 /* Treat as a noop */
1427 case 0x3F: /* HACK */
1429 #if defined(MIPS_USES_FPU)
1430 case 0x31 ... 0x32: /* Floating point load/store */
1434 /* Not implemented */
1435 /* XXX: not correct */
1437 case 0x11: /* CP1 opcode */
1438 /* Not implemented */
1439 /* XXX: not correct */
1440 case 0x12: /* CP2 opcode */
1441 /* Not implemented */
1442 /* XXX: not correct */
1443 case 0x13: /* CP3 opcode */
1444 /* Not implemented */
1445 /* XXX: not correct */
1446 #if defined (TARGET_MIPS64)
1451 /* MIPS64 opcodes */
1453 #if defined (MIPS_HAS_JALX)
1455 /* JALX: not implemented */
1459 #if defined (MIPS_HAS_LSC)
1460 case 0x31: /* LWC1 */
1461 case 0x32: /* LWC2 */
1462 case 0x35: /* SDC1 */
1463 case 0x36: /* SDC2 */
1465 default: /* Invalid */
1467 generate_exception(ctx, EXCP_RI);
1470 if (ctx->hflags & MIPS_HFLAG_DS) {
1471 int hflags = ctx->hflags;
1472 /* Branches completion */
1473 ctx->hflags &= ~(MIPS_HFLAG_BMASK | MIPS_HFLAG_DS);
1474 ctx->bstate = BS_BRANCH;
1475 save_cpu_state(ctx, 0);
1476 switch (hflags & MIPS_HFLAG_BMASK) {
1478 /* unconditional branch */
1479 MIPS_DEBUG("unconditional branch");
1480 gen_op_branch((long)ctx->tb, ctx->btarget);
1483 /* blikely taken case */
1484 MIPS_DEBUG("blikely branch taken");
1485 gen_op_branch((long)ctx->tb, ctx->btarget);
1488 /* Conditional branch */
1489 MIPS_DEBUG("conditional branch");
1490 gen_op_bcond((long)ctx->tb, ctx->btarget, ctx->pc + 4);
1493 /* unconditional branch to register */
1494 MIPS_DEBUG("branch to register");
1498 MIPS_DEBUG("unknown branch");
1504 int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
1507 DisasContext ctx, *ctxp = &ctx;
1508 target_ulong pc_start;
1509 uint16_t *gen_opc_end;
1513 gen_opc_ptr = gen_opc_buf;
1514 gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
1515 gen_opparam_ptr = gen_opparam_buf;
1518 ctx.bstate = BS_NONE;
1519 /* Restore delay slot state */
1520 ctx.hflags = env->hflags;
1521 ctx.saved_hflags = ctx.hflags;
1522 if (ctx.hflags & MIPS_HFLAG_BR) {
1523 gen_op_restore_breg_target();
1524 } else if (ctx.hflags & MIPS_HFLAG_B) {
1525 ctx.btarget = env->btarget;
1526 } else if (ctx.hflags & MIPS_HFLAG_BMASK) {
1527 /* If we are in the delay slot of a conditional branch,
1528 * restore the branch condition from env->bcond to T2
1530 ctx.btarget = env->btarget;
1531 gen_op_restore_bcond();
1533 #if defined(CONFIG_USER_ONLY)
1536 ctx.mem_idx = (ctx.hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM ? 0 : 1;
1538 ctx.CP0_Status = env->CP0_Status;
1540 if (loglevel & CPU_LOG_TB_CPU) {
1541 fprintf(logfile, "------------------------------------------------\n");
1542 cpu_dump_state(env, logfile, fprintf, 0);
1545 #if defined MIPS_DEBUG_DISAS
1546 if (loglevel & CPU_LOG_TB_IN_ASM)
1547 fprintf(logfile, "\ntb %p super %d cond %04x %04x\n",
1548 tb, ctx.mem_idx, ctx.hflags, env->hflags);
1550 while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
1552 j = gen_opc_ptr - gen_opc_buf;
1553 save_cpu_state(ctxp, 1);
1557 gen_opc_instr_start[lj++] = 0;
1558 gen_opc_pc[lj] = ctx.pc;
1559 gen_opc_instr_start[lj] = 1;
1562 ctx.opcode = ldl_code(ctx.pc);
1565 if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
1567 #if defined (MIPS_SINGLE_STEP)
1571 if (ctx.bstate != BS_BRANCH && ctx.bstate != BS_EXCP) {
1572 save_cpu_state(ctxp, 0);
1573 gen_op_branch((long)ctx.tb, ctx.pc);
1576 /* Generate the return instruction */
1578 *gen_opc_ptr = INDEX_op_end;
1580 j = gen_opc_ptr - gen_opc_buf;
1583 gen_opc_instr_start[lj++] = 0;
1586 tb->size = ctx.pc - pc_start;
1589 #if defined MIPS_DEBUG_DISAS
1590 if (loglevel & CPU_LOG_TB_IN_ASM)
1591 fprintf(logfile, "\n");
1593 if (loglevel & CPU_LOG_TB_IN_ASM) {
1594 fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
1595 target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
1596 fprintf(logfile, "\n");
1598 if (loglevel & CPU_LOG_TB_OP) {
1599 fprintf(logfile, "OP:\n");
1600 dump_ops(gen_opc_buf, gen_opparam_buf);
1601 fprintf(logfile, "\n");
1603 if (loglevel & CPU_LOG_TB_CPU) {
1604 fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
1611 int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
1613 return gen_intermediate_code_internal(env, tb, 0);
1616 int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
1618 return gen_intermediate_code_internal(env, tb, 1);
1621 void cpu_dump_state (CPUState *env, FILE *f,
1622 int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1628 cpu_fprintf(f, "pc=0x%08x HI=0x%08x LO=0x%08x ds %04x %08x %d\n",
1629 env->PC, env->HI, env->LO, env->hflags, env->btarget, env->bcond);
1630 for (i = 0; i < 32; i++) {
1632 cpu_fprintf(f, "GPR%02d:", i);
1633 cpu_fprintf(f, " %s %08x", regnames[i], env->gpr[i]);
1635 cpu_fprintf(f, "\n");
1638 c0_status = env->CP0_Status;
1639 if (env->hflags & MIPS_HFLAG_UM)
1640 c0_status |= (1 << CP0St_UM);
1641 if (env->hflags & MIPS_HFLAG_ERL)
1642 c0_status |= (1 << CP0St_ERL);
1643 if (env->hflags & MIPS_HFLAG_EXL)
1644 c0_status |= (1 << CP0St_EXL);
1646 cpu_fprintf(f, "CP0 Status 0x%08x Cause 0x%08x EPC 0x%08x\n",
1647 c0_status, env->CP0_Cause, env->CP0_EPC);
1648 cpu_fprintf(f, " Config0 0x%08x Config1 0x%08x LLAddr 0x%08x\n",
1649 env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
1652 CPUMIPSState *cpu_mips_init (void)
1657 env = qemu_mallocz(sizeof(CPUMIPSState));
1662 env->PC = 0xBFC00000;
1663 #if defined (MIPS_USES_R4K_TLB)
1664 env->CP0_random = MIPS_TLB_NB - 1;
1667 env->CP0_Config0 = MIPS_CONFIG0;
1668 #if defined (MIPS_CONFIG1)
1669 env->CP0_Config1 = MIPS_CONFIG1;
1671 #if defined (MIPS_CONFIG2)
1672 env->CP0_Config2 = MIPS_CONFIG2;
1674 #if defined (MIPS_CONFIG3)
1675 env->CP0_Config3 = MIPS_CONFIG3;
1677 env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV);
1678 env->CP0_WatchLo = 0;
1679 env->hflags = MIPS_HFLAG_ERL;
1680 /* Count register increments in debug mode, EJTAG version 1 */
1681 env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
1682 env->CP0_PRid = MIPS_CPU;
1683 env->exception_index = EXCP_NONE;
1685 cpu_single_env = env;