1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2019-2021 Paul Cercueil <paul@crapouillou.net>
6 #include "disassembler.h"
7 #include "interpreter.h"
8 #include "lightrec-private.h"
16 static u32 int_CP0(struct interpreter *inter);
17 static u32 int_CP2(struct interpreter *inter);
18 static u32 int_SPECIAL(struct interpreter *inter);
19 static u32 int_META(struct interpreter *inter);
20 static u32 int_REGIMM(struct interpreter *inter);
21 static u32 int_branch(struct interpreter *inter, u32 pc,
22 union code code, bool branch);
24 typedef u32 (*lightrec_int_func_t)(struct interpreter *inter);
26 static const lightrec_int_func_t int_standard[64];
29 struct lightrec_state *state;
37 static u32 int_get_branch_pc(const struct interpreter *inter)
39 return get_branch_pc(inter->block, inter->offset, 0);
42 static inline u32 int_get_ds_pc(const struct interpreter *inter, s16 imm)
44 return get_ds_pc(inter->block, inter->offset, imm);
47 static inline struct opcode *next_op(const struct interpreter *inter)
52 static inline u32 execute(lightrec_int_func_t func, struct interpreter *inter)
54 return (*func)(inter);
57 static inline u32 lightrec_int_op(struct interpreter *inter)
59 return execute(int_standard[inter->op->i.op], inter);
62 static inline u32 jump_skip(struct interpreter *inter)
64 inter->op = next_op(inter);
67 if (op_flag_sync(inter->op->flags)) {
68 inter->state->current_cycle += inter->cycles;
72 return lightrec_int_op(inter);
75 static inline u32 jump_next(struct interpreter *inter)
77 inter->cycles += lightrec_cycles_of_opcode(inter->state, inter->op->c);
79 if (unlikely(inter->delay_slot))
82 return jump_skip(inter);
85 static inline u32 jump_after_branch(struct interpreter *inter)
87 inter->cycles += lightrec_cycles_of_opcode(inter->state, inter->op->c);
89 if (unlikely(inter->delay_slot))
92 inter->op = next_op(inter);
95 return jump_skip(inter);
98 static void update_cycles_before_branch(struct interpreter *inter)
102 if (!inter->delay_slot) {
103 cycles = lightrec_cycles_of_opcode(inter->state, inter->op->c);
105 if (!op_flag_no_ds(inter->op->flags) &&
106 has_delay_slot(inter->op->c))
107 cycles += lightrec_cycles_of_opcode(inter->state, next_op(inter)->c);
109 inter->cycles += cycles;
110 inter->state->current_cycle += inter->cycles;
111 inter->cycles = -cycles;
115 static bool is_branch_taken(const u32 *reg_cache, union code op)
119 return op.r.op == OP_SPECIAL_JR || op.r.op == OP_SPECIAL_JALR;
124 return reg_cache[op.r.rs] == reg_cache[op.r.rt];
126 return reg_cache[op.r.rs] != reg_cache[op.r.rt];
130 case OP_REGIMM_BLTZAL:
131 return (s32)reg_cache[op.r.rs] < 0;
133 case OP_REGIMM_BGEZAL:
134 return (s32)reg_cache[op.r.rs] >= 0;
143 static u32 int_delay_slot(struct interpreter *inter, u32 pc, bool branch)
145 struct lightrec_state *state = inter->state;
146 u32 *reg_cache = state->regs.gpr;
147 struct opcode new_op, *op = next_op(inter);
149 struct interpreter inter2 = {
151 .cycles = inter->cycles,
155 bool run_first_op = false, dummy_ld = false, save_rs = false,
156 load_in_ds, branch_in_ds = false, branch_at_addr = false,
158 u32 new_rt, old_rs = 0, new_rs = 0;
159 u32 next_pc, ds_next_pc;
162 if (op->i.op == OP_CP0 && op->r.rs == OP_CP0_RFE) {
163 /* When an IRQ happens, the PSX exception handlers (when done)
164 * will jump back to the instruction that was executed right
165 * before the IRQ, unless it was a GTE opcode; in that case, it
166 * jumps to the instruction right after.
167 * Since we will never handle the IRQ right after a GTE opcode,
168 * but on branch boundaries, we need to adjust the return
169 * address so that the GTE opcode is effectively executed.
171 cause = state->regs.cp0[13];
172 epc = state->regs.cp0[14];
174 if (!(cause & 0x7c) && epc == pc - 4)
178 if (inter->delay_slot) {
179 /* The branch opcode was in a delay slot of another branch
180 * opcode. Just return the target address of the second
185 /* An opcode located in the delay slot performing a delayed read
186 * requires special handling; we will always resort to using the
187 * interpreter in that case.
188 * Same goes for when we have a branch in a delay slot of another
190 load_in_ds = opcode_is_load(op->c) || opcode_is_mfc(op->c);
191 branch_in_ds = has_delay_slot(op->c);
194 if (load_in_ds || branch_in_ds)
195 op_next = lightrec_read_opcode(state, pc);
198 /* Verify that the next block actually reads the
199 * destination register of the delay slot opcode. */
200 run_first_op = opcode_reads_register(op_next, op->r.rt);
208 if (load_in_ds && run_first_op) {
211 /* If the first opcode of the next block writes the
212 * regiser used as the address for the load, we need to
213 * reset to the old value after it has been executed,
214 * then restore the new value after the delay slot
215 * opcode has been executed. */
216 save_rs = opcode_reads_register(op->c, op->r.rs) &&
217 opcode_writes_register(op_next, op->r.rs);
219 old_rs = reg_cache[op->r.rs];
221 /* If both the first opcode of the next block and the
222 * delay slot opcode write to the same register, the
223 * value written by the delay slot opcode is
225 dummy_ld = opcode_writes_register(op_next, op->r.rt);
230 } else if (has_delay_slot(op_next)) {
231 /* The first opcode of the next block is a branch, so we
232 * cannot execute it here, because of the load delay.
233 * Just check whether or not the branch would be taken,
234 * and save that info into the interpreter struct. */
235 branch_at_addr = true;
236 branch_taken = is_branch_taken(reg_cache, op_next);
237 pr_debug("Target of impossible branch is a branch, "
238 "%staken.\n", branch_taken ? "" : "not ");
239 inter->cycles += lightrec_cycles_of_opcode(inter->state, op_next);
240 old_rs = reg_cache[op_next.r.rs];
247 /* Execute the first opcode of the next block */
248 lightrec_int_op(&inter2);
251 new_rs = reg_cache[op->r.rs];
252 reg_cache[op->r.rs] = old_rs;
255 inter->cycles += lightrec_cycles_of_opcode(inter->state, op_next);
258 next_pc = int_get_ds_pc(inter, 2);
261 inter2.block = inter->block;
263 inter2.cycles = inter->cycles;
264 inter2.offset = inter->offset + 1;
267 new_rt = reg_cache[op->r.rt];
269 /* Execute delay slot opcode */
270 ds_next_pc = lightrec_int_op(&inter2);
272 if (branch_at_addr) {
273 if (op_next.i.op == OP_SPECIAL)
274 /* TODO: Handle JALR setting $ra */
276 else if (op_next.i.op == OP_J || op_next.i.op == OP_JAL)
277 /* TODO: Handle JAL setting $ra */
278 ds_next_pc = (pc & 0xf0000000) | (op_next.j.imm << 2);
280 ds_next_pc = pc + 4 + ((s16)op_next.i.imm << 2);
283 if (branch_at_addr && !branch_taken) {
284 /* If the branch at the target of the branch opcode is not
285 * taken, we jump to its delay slot */
286 next_pc = pc + sizeof(u32);
287 } else if (branch_at_addr || (!branch && branch_in_ds)) {
288 next_pc = ds_next_pc;
292 reg_cache[op->r.rs] = new_rs;
294 reg_cache[op->r.rt] = new_rt;
296 inter->cycles += lightrec_cycles_of_opcode(inter->state, op->c);
298 if (branch_at_addr && branch_taken) {
299 /* If the branch at the target of the branch opcode is taken,
300 * we execute its delay slot here, and jump to its target
302 op_next = lightrec_read_opcode(state, pc + 4);
309 inter->cycles += lightrec_cycles_of_opcode(inter->state, op_next);
311 pr_debug("Running delay slot of branch at target of impossible "
313 lightrec_int_op(&inter2);
319 static u32 int_unimplemented(struct interpreter *inter)
321 lightrec_set_exit_flags(inter->state, LIGHTREC_EXIT_UNKNOWN_OP);
323 return inter->block->pc + (inter->offset << 2);
326 static u32 int_jump(struct interpreter *inter, bool link)
328 struct lightrec_state *state = inter->state;
329 u32 old_pc = int_get_branch_pc(inter);
330 u32 pc = (old_pc & 0xf0000000) | (inter->op->j.imm << 2);
333 state->regs.gpr[31] = old_pc + 8;
335 if (op_flag_no_ds(inter->op->flags))
338 return int_delay_slot(inter, pc, true);
341 static u32 int_J(struct interpreter *inter)
343 return int_jump(inter, false);
346 static u32 int_JAL(struct interpreter *inter)
348 return int_jump(inter, true);
351 static u32 int_jumpr(struct interpreter *inter, u8 link_reg)
353 struct lightrec_state *state = inter->state;
354 u32 old_pc = int_get_branch_pc(inter);
355 u32 next_pc = state->regs.gpr[inter->op->r.rs];
358 state->regs.gpr[link_reg] = old_pc + 8;
360 if (op_flag_no_ds(inter->op->flags))
363 return int_delay_slot(inter, next_pc, true);
366 static u32 int_special_JR(struct interpreter *inter)
368 return int_jumpr(inter, 0);
371 static u32 int_special_JALR(struct interpreter *inter)
373 return int_jumpr(inter, inter->op->r.rd);
376 static u32 int_do_branch(struct interpreter *inter, u32 old_pc, u32 next_pc)
378 if (!inter->delay_slot && op_flag_local_branch(inter->op->flags) &&
379 (s16)inter->op->c.i.imm >= 0) {
380 next_pc = old_pc + ((1 + (s16)inter->op->c.i.imm) << 2);
381 next_pc = lightrec_emulate_block(inter->state, inter->block, next_pc);
387 static u32 int_branch(struct interpreter *inter, u32 pc,
388 union code code, bool branch)
390 u32 next_pc = pc + 4 + ((s16)code.i.imm << 2);
392 update_cycles_before_branch(inter);
394 if (op_flag_no_ds(inter->op->flags)) {
396 return int_do_branch(inter, pc, next_pc);
398 return jump_next(inter);
401 if (!inter->delay_slot)
402 next_pc = int_delay_slot(inter, next_pc, branch);
405 return int_do_branch(inter, pc, next_pc);
407 if (op_flag_emulate_branch(inter->op->flags))
410 return jump_after_branch(inter);
413 static u32 int_beq(struct interpreter *inter, bool bne)
415 u32 rs, rt, old_pc = int_get_branch_pc(inter);
417 rs = inter->state->regs.gpr[inter->op->i.rs];
418 rt = inter->state->regs.gpr[inter->op->i.rt];
420 return int_branch(inter, old_pc, inter->op->c, (rs == rt) ^ bne);
423 static u32 int_BEQ(struct interpreter *inter)
425 return int_beq(inter, false);
428 static u32 int_BNE(struct interpreter *inter)
430 return int_beq(inter, true);
433 static u32 int_bgez(struct interpreter *inter, bool link, bool lt, bool regimm)
435 u32 old_pc = int_get_branch_pc(inter);
439 inter->state->regs.gpr[31] = old_pc + 8;
441 rs = (s32)inter->state->regs.gpr[inter->op->i.rs];
443 return int_branch(inter, old_pc, inter->op->c,
444 ((regimm && !rs) || rs > 0) ^ lt);
447 static u32 int_regimm_BLTZ(struct interpreter *inter)
449 return int_bgez(inter, false, true, true);
452 static u32 int_regimm_BGEZ(struct interpreter *inter)
454 return int_bgez(inter, false, false, true);
457 static u32 int_regimm_BLTZAL(struct interpreter *inter)
459 return int_bgez(inter, true, true, true);
462 static u32 int_regimm_BGEZAL(struct interpreter *inter)
464 return int_bgez(inter, true, false, true);
467 static u32 int_BLEZ(struct interpreter *inter)
469 return int_bgez(inter, false, true, false);
472 static u32 int_BGTZ(struct interpreter *inter)
474 return int_bgez(inter, false, false, false);
477 static u32 int_cfc(struct interpreter *inter)
479 struct lightrec_state *state = inter->state;
480 const struct opcode *op = inter->op;
483 val = lightrec_mfc(state, op->c);
485 if (likely(op->r.rt))
486 state->regs.gpr[op->r.rt] = val;
488 return jump_next(inter);
491 static u32 int_ctc(struct interpreter *inter)
493 struct lightrec_state *state = inter->state;
494 const struct opcode *op = inter->op;
496 lightrec_mtc(state, op->c, op->r.rd, state->regs.gpr[op->r.rt]);
498 /* If we have a MTC0 or CTC0 to CP0 register 12 (Status) or 13 (Cause),
499 * return early so that the emulator will be able to check software
500 * interrupt status. */
501 if (!op_flag_no_ds(inter->op->flags) &&
502 op->i.op == OP_CP0 && (op->r.rd == 12 || op->r.rd == 13))
503 return int_get_ds_pc(inter, 1);
505 return jump_next(inter);
508 static u32 int_cp0_RFE(struct interpreter *inter)
510 lightrec_rfe(inter->state);
512 return jump_next(inter);
515 static u32 int_CP(struct interpreter *inter)
517 lightrec_cp(inter->state, inter->op->c);
519 return jump_next(inter);
522 static u32 int_ADDI(struct interpreter *inter)
524 u32 *reg_cache = inter->state->regs.gpr;
525 struct opcode_i *op = &inter->op->i;
528 reg_cache[op->rt] = reg_cache[op->rs] + (s32)(s16)op->imm;
530 return jump_next(inter);
533 static u32 int_SLTI(struct interpreter *inter)
535 u32 *reg_cache = inter->state->regs.gpr;
536 struct opcode_i *op = &inter->op->i;
539 reg_cache[op->rt] = (s32)reg_cache[op->rs] < (s32)(s16)op->imm;
541 return jump_next(inter);
544 static u32 int_SLTIU(struct interpreter *inter)
546 u32 *reg_cache = inter->state->regs.gpr;
547 struct opcode_i *op = &inter->op->i;
550 reg_cache[op->rt] = reg_cache[op->rs] < (u32)(s32)(s16)op->imm;
552 return jump_next(inter);
555 static u32 int_ANDI(struct interpreter *inter)
557 u32 *reg_cache = inter->state->regs.gpr;
558 struct opcode_i *op = &inter->op->i;
561 reg_cache[op->rt] = reg_cache[op->rs] & op->imm;
563 return jump_next(inter);
566 static u32 int_ORI(struct interpreter *inter)
568 u32 *reg_cache = inter->state->regs.gpr;
569 struct opcode_i *op = &inter->op->i;
572 reg_cache[op->rt] = reg_cache[op->rs] | op->imm;
574 return jump_next(inter);
577 static u32 int_XORI(struct interpreter *inter)
579 u32 *reg_cache = inter->state->regs.gpr;
580 struct opcode_i *op = &inter->op->i;
583 reg_cache[op->rt] = reg_cache[op->rs] ^ op->imm;
585 return jump_next(inter);
588 static u32 int_LUI(struct interpreter *inter)
590 struct opcode_i *op = &inter->op->i;
592 inter->state->regs.gpr[op->rt] = op->imm << 16;
594 return jump_next(inter);
597 static u32 int_io(struct interpreter *inter, bool is_load)
599 struct opcode_i *op = &inter->op->i;
600 u32 *reg_cache = inter->state->regs.gpr;
601 u32 val, *flags = NULL;
604 flags = &inter->op->flags;
606 val = lightrec_rw(inter->state, inter->op->c,
607 reg_cache[op->rs], reg_cache[op->rt],
608 flags, inter->block, inter->offset);
610 if (is_load && op->rt)
611 reg_cache[op->rt] = val;
613 return jump_next(inter);
616 static u32 int_load(struct interpreter *inter)
618 return int_io(inter, true);
621 static u32 int_store(struct interpreter *inter)
625 if (likely(!op_flag_smc(inter->op->flags)))
626 return int_io(inter, false);
628 lightrec_rw(inter->state, inter->op->c,
629 inter->state->regs.gpr[inter->op->i.rs],
630 inter->state->regs.gpr[inter->op->i.rt],
631 &inter->op->flags, inter->block, inter->offset);
633 next_pc = int_get_ds_pc(inter, 1);
635 /* Invalidate next PC, to force the rest of the block to be rebuilt */
636 lightrec_invalidate(inter->state, next_pc, 4);
641 static u32 int_LWC2(struct interpreter *inter)
643 return int_io(inter, false);
646 static u32 int_special_SLL(struct interpreter *inter)
648 struct opcode *op = inter->op;
651 if (op->opcode) { /* Handle NOPs */
652 rt = inter->state->regs.gpr[op->r.rt];
653 inter->state->regs.gpr[op->r.rd] = rt << op->r.imm;
656 return jump_next(inter);
659 static u32 int_special_SRL(struct interpreter *inter)
661 struct opcode *op = inter->op;
662 u32 rt = inter->state->regs.gpr[op->r.rt];
664 inter->state->regs.gpr[op->r.rd] = rt >> op->r.imm;
666 return jump_next(inter);
669 static u32 int_special_SRA(struct interpreter *inter)
671 struct opcode *op = inter->op;
672 s32 rt = inter->state->regs.gpr[op->r.rt];
674 inter->state->regs.gpr[op->r.rd] = rt >> op->r.imm;
676 return jump_next(inter);
679 static u32 int_special_SLLV(struct interpreter *inter)
681 struct opcode *op = inter->op;
682 u32 rs = inter->state->regs.gpr[op->r.rs];
683 u32 rt = inter->state->regs.gpr[op->r.rt];
685 inter->state->regs.gpr[op->r.rd] = rt << (rs & 0x1f);
687 return jump_next(inter);
690 static u32 int_special_SRLV(struct interpreter *inter)
692 struct opcode *op = inter->op;
693 u32 rs = inter->state->regs.gpr[op->r.rs];
694 u32 rt = inter->state->regs.gpr[op->r.rt];
696 inter->state->regs.gpr[op->r.rd] = rt >> (rs & 0x1f);
698 return jump_next(inter);
701 static u32 int_special_SRAV(struct interpreter *inter)
703 struct opcode *op = inter->op;
704 u32 rs = inter->state->regs.gpr[op->r.rs];
705 s32 rt = inter->state->regs.gpr[op->r.rt];
707 inter->state->regs.gpr[op->r.rd] = rt >> (rs & 0x1f);
709 return jump_next(inter);
712 static u32 int_syscall_break(struct interpreter *inter)
715 if (inter->op->r.op == OP_SPECIAL_BREAK)
716 lightrec_set_exit_flags(inter->state, LIGHTREC_EXIT_BREAK);
718 lightrec_set_exit_flags(inter->state, LIGHTREC_EXIT_SYSCALL);
720 return int_get_ds_pc(inter, 0);
723 static u32 int_special_MFHI(struct interpreter *inter)
725 u32 *reg_cache = inter->state->regs.gpr;
726 struct opcode_r *op = &inter->op->r;
729 reg_cache[op->rd] = reg_cache[REG_HI];
731 return jump_next(inter);
734 static u32 int_special_MTHI(struct interpreter *inter)
736 u32 *reg_cache = inter->state->regs.gpr;
738 reg_cache[REG_HI] = reg_cache[inter->op->r.rs];
740 return jump_next(inter);
743 static u32 int_special_MFLO(struct interpreter *inter)
745 u32 *reg_cache = inter->state->regs.gpr;
746 struct opcode_r *op = &inter->op->r;
749 reg_cache[op->rd] = reg_cache[REG_LO];
751 return jump_next(inter);
754 static u32 int_special_MTLO(struct interpreter *inter)
756 u32 *reg_cache = inter->state->regs.gpr;
758 reg_cache[REG_LO] = reg_cache[inter->op->r.rs];
760 return jump_next(inter);
763 static u32 int_special_MULT(struct interpreter *inter)
765 u32 *reg_cache = inter->state->regs.gpr;
766 s32 rs = reg_cache[inter->op->r.rs];
767 s32 rt = reg_cache[inter->op->r.rt];
768 u8 reg_lo = get_mult_div_lo(inter->op->c);
769 u8 reg_hi = get_mult_div_hi(inter->op->c);
770 u64 res = (s64)rs * (s64)rt;
772 if (!op_flag_no_hi(inter->op->flags))
773 reg_cache[reg_hi] = res >> 32;
774 if (!op_flag_no_lo(inter->op->flags))
775 reg_cache[reg_lo] = res;
777 return jump_next(inter);
780 static u32 int_special_MULTU(struct interpreter *inter)
782 u32 *reg_cache = inter->state->regs.gpr;
783 u32 rs = reg_cache[inter->op->r.rs];
784 u32 rt = reg_cache[inter->op->r.rt];
785 u8 reg_lo = get_mult_div_lo(inter->op->c);
786 u8 reg_hi = get_mult_div_hi(inter->op->c);
787 u64 res = (u64)rs * (u64)rt;
789 if (!op_flag_no_hi(inter->op->flags))
790 reg_cache[reg_hi] = res >> 32;
791 if (!op_flag_no_lo(inter->op->flags))
792 reg_cache[reg_lo] = res;
794 return jump_next(inter);
797 static u32 int_special_DIV(struct interpreter *inter)
799 u32 *reg_cache = inter->state->regs.gpr;
800 s32 rs = reg_cache[inter->op->r.rs];
801 s32 rt = reg_cache[inter->op->r.rt];
802 u8 reg_lo = get_mult_div_lo(inter->op->c);
803 u8 reg_hi = get_mult_div_hi(inter->op->c);
808 lo = (rs < 0) * 2 - 1;
814 if (!op_flag_no_hi(inter->op->flags))
815 reg_cache[reg_hi] = hi;
816 if (!op_flag_no_lo(inter->op->flags))
817 reg_cache[reg_lo] = lo;
819 return jump_next(inter);
822 static u32 int_special_DIVU(struct interpreter *inter)
824 u32 *reg_cache = inter->state->regs.gpr;
825 u32 rs = reg_cache[inter->op->r.rs];
826 u32 rt = reg_cache[inter->op->r.rt];
827 u8 reg_lo = get_mult_div_lo(inter->op->c);
828 u8 reg_hi = get_mult_div_hi(inter->op->c);
839 if (!op_flag_no_hi(inter->op->flags))
840 reg_cache[reg_hi] = hi;
841 if (!op_flag_no_lo(inter->op->flags))
842 reg_cache[reg_lo] = lo;
844 return jump_next(inter);
847 static u32 int_special_ADD(struct interpreter *inter)
849 u32 *reg_cache = inter->state->regs.gpr;
850 struct opcode_r *op = &inter->op->r;
851 s32 rs = reg_cache[op->rs];
852 s32 rt = reg_cache[op->rt];
855 reg_cache[op->rd] = rs + rt;
857 return jump_next(inter);
860 static u32 int_special_SUB(struct interpreter *inter)
862 u32 *reg_cache = inter->state->regs.gpr;
863 struct opcode_r *op = &inter->op->r;
864 u32 rs = reg_cache[op->rs];
865 u32 rt = reg_cache[op->rt];
868 reg_cache[op->rd] = rs - rt;
870 return jump_next(inter);
873 static u32 int_special_AND(struct interpreter *inter)
875 u32 *reg_cache = inter->state->regs.gpr;
876 struct opcode_r *op = &inter->op->r;
877 u32 rs = reg_cache[op->rs];
878 u32 rt = reg_cache[op->rt];
881 reg_cache[op->rd] = rs & rt;
883 return jump_next(inter);
886 static u32 int_special_OR(struct interpreter *inter)
888 u32 *reg_cache = inter->state->regs.gpr;
889 struct opcode_r *op = &inter->op->r;
890 u32 rs = reg_cache[op->rs];
891 u32 rt = reg_cache[op->rt];
894 reg_cache[op->rd] = rs | rt;
896 return jump_next(inter);
899 static u32 int_special_XOR(struct interpreter *inter)
901 u32 *reg_cache = inter->state->regs.gpr;
902 struct opcode_r *op = &inter->op->r;
903 u32 rs = reg_cache[op->rs];
904 u32 rt = reg_cache[op->rt];
907 reg_cache[op->rd] = rs ^ rt;
909 return jump_next(inter);
912 static u32 int_special_NOR(struct interpreter *inter)
914 u32 *reg_cache = inter->state->regs.gpr;
915 struct opcode_r *op = &inter->op->r;
916 u32 rs = reg_cache[op->rs];
917 u32 rt = reg_cache[op->rt];
920 reg_cache[op->rd] = ~(rs | rt);
922 return jump_next(inter);
925 static u32 int_special_SLT(struct interpreter *inter)
927 u32 *reg_cache = inter->state->regs.gpr;
928 struct opcode_r *op = &inter->op->r;
929 s32 rs = reg_cache[op->rs];
930 s32 rt = reg_cache[op->rt];
933 reg_cache[op->rd] = rs < rt;
935 return jump_next(inter);
938 static u32 int_special_SLTU(struct interpreter *inter)
940 u32 *reg_cache = inter->state->regs.gpr;
941 struct opcode_r *op = &inter->op->r;
942 u32 rs = reg_cache[op->rs];
943 u32 rt = reg_cache[op->rt];
946 reg_cache[op->rd] = rs < rt;
948 return jump_next(inter);
951 static u32 int_META_MOV(struct interpreter *inter)
953 u32 *reg_cache = inter->state->regs.gpr;
954 struct opcode_m *op = &inter->op->m;
957 reg_cache[op->rd] = reg_cache[op->rs];
959 return jump_next(inter);
962 static u32 int_META_EXTC(struct interpreter *inter)
964 u32 *reg_cache = inter->state->regs.gpr;
965 struct opcode_m *op = &inter->op->m;
968 reg_cache[op->rd] = (u32)(s32)(s8)reg_cache[op->rs];
970 return jump_next(inter);
973 static u32 int_META_EXTS(struct interpreter *inter)
975 u32 *reg_cache = inter->state->regs.gpr;
976 struct opcode_m *op = &inter->op->m;
979 reg_cache[op->rd] = (u32)(s32)(s16)reg_cache[op->rs];
981 return jump_next(inter);
984 static u32 int_META_MULT2(struct interpreter *inter)
986 u32 *reg_cache = inter->state->regs.gpr;
987 union code c = inter->op->c;
988 u32 rs = reg_cache[c.r.rs];
989 u8 reg_lo = get_mult_div_lo(c);
990 u8 reg_hi = get_mult_div_hi(c);
992 if (!op_flag_no_lo(inter->op->flags)) {
994 reg_cache[reg_lo] = rs << c.r.op;
996 reg_cache[reg_lo] = 0;
999 if (!op_flag_no_hi(inter->op->flags)) {
1001 reg_cache[reg_hi] = rs << (c.r.op - 32);
1003 else if (c.i.op == OP_META_MULT2) {
1005 reg_cache[reg_hi] = (s32) rs >> (32 - c.r.op);
1007 reg_cache[reg_hi] = (s32) rs >> 31;
1010 reg_cache[reg_hi] = rs >> (32 - c.r.op);
1012 reg_cache[reg_hi] = 0;
1016 return jump_next(inter);
1019 static u32 int_META_COM(struct interpreter *inter)
1021 u32 *reg_cache = inter->state->regs.gpr;
1022 union code c = inter->op->c;
1025 reg_cache[c.m.rd] = ~reg_cache[c.m.rs];
1027 return jump_next(inter);
1030 static const lightrec_int_func_t int_standard[64] = {
1031 SET_DEFAULT_ELM(int_standard, int_unimplemented),
1032 [OP_SPECIAL] = int_SPECIAL,
1033 [OP_REGIMM] = int_REGIMM,
1038 [OP_BLEZ] = int_BLEZ,
1039 [OP_BGTZ] = int_BGTZ,
1040 [OP_ADDI] = int_ADDI,
1041 [OP_ADDIU] = int_ADDI,
1042 [OP_SLTI] = int_SLTI,
1043 [OP_SLTIU] = int_SLTIU,
1044 [OP_ANDI] = int_ANDI,
1046 [OP_XORI] = int_XORI,
1052 [OP_LWL] = int_load,
1054 [OP_LBU] = int_load,
1055 [OP_LHU] = int_load,
1056 [OP_LWR] = int_load,
1057 [OP_SB] = int_store,
1058 [OP_SH] = int_store,
1059 [OP_SWL] = int_store,
1060 [OP_SW] = int_store,
1061 [OP_SWR] = int_store,
1062 [OP_LWC2] = int_LWC2,
1063 [OP_SWC2] = int_store,
1065 [OP_META] = int_META,
1066 [OP_META_MULT2] = int_META_MULT2,
1067 [OP_META_MULTU2] = int_META_MULT2,
1068 [OP_META_LWU] = int_load,
1069 [OP_META_SWU] = int_store,
1072 static const lightrec_int_func_t int_special[64] = {
1073 SET_DEFAULT_ELM(int_special, int_unimplemented),
1074 [OP_SPECIAL_SLL] = int_special_SLL,
1075 [OP_SPECIAL_SRL] = int_special_SRL,
1076 [OP_SPECIAL_SRA] = int_special_SRA,
1077 [OP_SPECIAL_SLLV] = int_special_SLLV,
1078 [OP_SPECIAL_SRLV] = int_special_SRLV,
1079 [OP_SPECIAL_SRAV] = int_special_SRAV,
1080 [OP_SPECIAL_JR] = int_special_JR,
1081 [OP_SPECIAL_JALR] = int_special_JALR,
1082 [OP_SPECIAL_SYSCALL] = int_syscall_break,
1083 [OP_SPECIAL_BREAK] = int_syscall_break,
1084 [OP_SPECIAL_MFHI] = int_special_MFHI,
1085 [OP_SPECIAL_MTHI] = int_special_MTHI,
1086 [OP_SPECIAL_MFLO] = int_special_MFLO,
1087 [OP_SPECIAL_MTLO] = int_special_MTLO,
1088 [OP_SPECIAL_MULT] = int_special_MULT,
1089 [OP_SPECIAL_MULTU] = int_special_MULTU,
1090 [OP_SPECIAL_DIV] = int_special_DIV,
1091 [OP_SPECIAL_DIVU] = int_special_DIVU,
1092 [OP_SPECIAL_ADD] = int_special_ADD,
1093 [OP_SPECIAL_ADDU] = int_special_ADD,
1094 [OP_SPECIAL_SUB] = int_special_SUB,
1095 [OP_SPECIAL_SUBU] = int_special_SUB,
1096 [OP_SPECIAL_AND] = int_special_AND,
1097 [OP_SPECIAL_OR] = int_special_OR,
1098 [OP_SPECIAL_XOR] = int_special_XOR,
1099 [OP_SPECIAL_NOR] = int_special_NOR,
1100 [OP_SPECIAL_SLT] = int_special_SLT,
1101 [OP_SPECIAL_SLTU] = int_special_SLTU,
1104 static const lightrec_int_func_t int_regimm[64] = {
1105 SET_DEFAULT_ELM(int_regimm, int_unimplemented),
1106 [OP_REGIMM_BLTZ] = int_regimm_BLTZ,
1107 [OP_REGIMM_BGEZ] = int_regimm_BGEZ,
1108 [OP_REGIMM_BLTZAL] = int_regimm_BLTZAL,
1109 [OP_REGIMM_BGEZAL] = int_regimm_BGEZAL,
1112 static const lightrec_int_func_t int_cp0[64] = {
1113 SET_DEFAULT_ELM(int_cp0, int_CP),
1114 [OP_CP0_MFC0] = int_cfc,
1115 [OP_CP0_CFC0] = int_cfc,
1116 [OP_CP0_MTC0] = int_ctc,
1117 [OP_CP0_CTC0] = int_ctc,
1118 [OP_CP0_RFE] = int_cp0_RFE,
1121 static const lightrec_int_func_t int_cp2_basic[64] = {
1122 SET_DEFAULT_ELM(int_cp2_basic, int_CP),
1123 [OP_CP2_BASIC_MFC2] = int_cfc,
1124 [OP_CP2_BASIC_CFC2] = int_cfc,
1125 [OP_CP2_BASIC_MTC2] = int_ctc,
1126 [OP_CP2_BASIC_CTC2] = int_ctc,
1129 static const lightrec_int_func_t int_meta[64] = {
1130 SET_DEFAULT_ELM(int_meta, int_unimplemented),
1131 [OP_META_MOV] = int_META_MOV,
1132 [OP_META_EXTC] = int_META_EXTC,
1133 [OP_META_EXTS] = int_META_EXTS,
1134 [OP_META_COM] = int_META_COM,
1137 static u32 int_SPECIAL(struct interpreter *inter)
1139 lightrec_int_func_t f = int_special[inter->op->r.op];
1141 if (!HAS_DEFAULT_ELM && unlikely(!f))
1142 return int_unimplemented(inter);
1144 return execute(f, inter);
1147 static u32 int_REGIMM(struct interpreter *inter)
1149 lightrec_int_func_t f = int_regimm[inter->op->r.rt];
1151 if (!HAS_DEFAULT_ELM && unlikely(!f))
1152 return int_unimplemented(inter);
1154 return execute(f, inter);
1157 static u32 int_CP0(struct interpreter *inter)
1159 lightrec_int_func_t f = int_cp0[inter->op->r.rs];
1161 if (!HAS_DEFAULT_ELM && unlikely(!f))
1162 return int_CP(inter);
1164 return execute(f, inter);
1167 static u32 int_CP2(struct interpreter *inter)
1169 if (inter->op->r.op == OP_CP2_BASIC) {
1170 lightrec_int_func_t f = int_cp2_basic[inter->op->r.rs];
1171 if (HAS_DEFAULT_ELM || likely(f))
1172 return execute(f, inter);
1175 return int_CP(inter);
1178 static u32 int_META(struct interpreter *inter)
1180 lightrec_int_func_t f = int_meta[inter->op->m.op];
1182 if (!HAS_DEFAULT_ELM && unlikely(!f))
1183 return int_unimplemented(inter);
1185 return execute(f, inter);
1188 static u32 lightrec_emulate_block_list(struct lightrec_state *state,
1189 struct block *block, u32 offset)
1191 struct interpreter inter;
1194 inter.block = block;
1195 inter.state = state;
1196 inter.offset = offset;
1197 inter.op = &block->opcode_list[offset];
1199 inter.delay_slot = false;
1201 pc = lightrec_int_op(&inter);
1203 /* Add the cycles of the last branch */
1204 inter.cycles += lightrec_cycles_of_opcode(inter.state, inter.op->c);
1206 state->current_cycle += inter.cycles;
1211 u32 lightrec_emulate_block(struct lightrec_state *state, struct block *block, u32 pc)
1213 u32 offset = (kunseg(pc) - kunseg(block->pc)) >> 2;
1215 if (offset < block->nb_ops)
1216 return lightrec_emulate_block_list(state, block, offset);
1218 pr_err(PC_FMT" is outside block at "PC_FMT"\n", pc, block->pc);
1220 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
1225 static u32 branch_get_next_pc(struct lightrec_state *state, union code c, u32 pc)
1230 return state->regs.gpr[c.r.rs];
1233 return (pc & 0xf0000000) | (c.j.imm << 2);
1235 /* Branch opcodes */
1236 return pc + 4 + ((s16)c.i.imm << 2);
1240 u32 lightrec_handle_load_delay(struct lightrec_state *state,
1241 struct block *block, u32 pc, u32 reg)
1243 union code c = lightrec_read_opcode(state, pc);
1244 struct opcode op[2] = {
1253 struct interpreter inter = {
1261 u32 reg_mask, next_pc;
1263 if (has_delay_slot(c)) {
1264 op[1].c = lightrec_read_opcode(state, pc + 4);
1266 branch_taken = is_branch_taken(state->regs.gpr, c);
1267 next_pc = branch_get_next_pc(state, c, pc);
1269 /* Branch was evaluated, we can write the load opcode's target
1271 state->regs.gpr[reg] = state->temp_reg;
1273 /* Handle JALR / regimm opcodes setting $ra (or any other
1274 * register in the case of JALR) */
1275 reg_mask = (u32)opcode_write_mask(c);
1277 state->regs.gpr[ctz32(reg_mask)] = pc + 8;
1279 /* Handle delay slot of the branch opcode */
1280 pc = int_delay_slot(&inter, next_pc, branch_taken);
1282 /* Make sure we only run one instruction */
1283 inter.delay_slot = true;
1285 lightrec_int_op(&inter);
1288 if (!opcode_writes_register(c, reg))
1289 state->regs.gpr[reg] = state->temp_reg;
1292 state->current_cycle += inter.cycles;