2 * Copyright (C) 2014-2020 Paul Cercueil <paul@crapouillou.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
15 #include "blockcache.h"
18 #include "disassembler.h"
20 #include "interpreter.h"
22 #include "memmanager.h"
24 #include "recompiler.h"
26 #include "optimizer.h"
29 #include <lightning.h>
31 #if ENABLE_THREADED_COMPILER
32 #include <stdatomic.h>
41 #define GENMASK(h, l) \
42 (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
44 static struct block * lightrec_precompile_block(struct lightrec_state *state,
47 static void lightrec_default_sb(struct lightrec_state *state, u32 opcode,
48 void *host, u32 addr, u8 data)
52 if (!state->invalidate_from_dma_only)
53 lightrec_invalidate(state, addr, 1);
56 static void lightrec_default_sh(struct lightrec_state *state, u32 opcode,
57 void *host, u32 addr, u16 data)
59 *(u16 *)host = HTOLE16(data);
61 if (!state->invalidate_from_dma_only)
62 lightrec_invalidate(state, addr, 2);
65 static void lightrec_default_sw(struct lightrec_state *state, u32 opcode,
66 void *host, u32 addr, u32 data)
68 *(u32 *)host = HTOLE32(data);
70 if (!state->invalidate_from_dma_only)
71 lightrec_invalidate(state, addr, 4);
74 static u8 lightrec_default_lb(struct lightrec_state *state,
75 u32 opcode, void *host, u32 addr)
80 static u16 lightrec_default_lh(struct lightrec_state *state,
81 u32 opcode, void *host, u32 addr)
83 return LE16TOH(*(u16 *)host);
86 static u32 lightrec_default_lw(struct lightrec_state *state,
87 u32 opcode, void *host, u32 addr)
89 return LE32TOH(*(u32 *)host);
92 static const struct lightrec_mem_map_ops lightrec_default_ops = {
93 .sb = lightrec_default_sb,
94 .sh = lightrec_default_sh,
95 .sw = lightrec_default_sw,
96 .lb = lightrec_default_lb,
97 .lh = lightrec_default_lh,
98 .lw = lightrec_default_lw,
101 static void __segfault_cb(struct lightrec_state *state, u32 addr)
103 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
104 pr_err("Segmentation fault in recompiled code: invalid "
105 "load/store at address 0x%08x\n", addr);
108 static void lightrec_swl(struct lightrec_state *state,
109 const struct lightrec_mem_map_ops *ops,
110 u32 opcode, void *host, u32 addr, u32 data)
112 unsigned int shift = addr & 0x3;
113 unsigned int mask = GENMASK(31, (shift + 1) * 8);
116 /* Align to 32 bits */
118 host = (void *)((uintptr_t)host & ~3);
120 old_data = ops->lw(state, opcode, host, addr);
122 data = (data >> ((3 - shift) * 8)) | (old_data & mask);
124 ops->sw(state, opcode, host, addr, data);
127 static void lightrec_swr(struct lightrec_state *state,
128 const struct lightrec_mem_map_ops *ops,
129 u32 opcode, void *host, u32 addr, u32 data)
131 unsigned int shift = addr & 0x3;
132 unsigned int mask = (1 << (shift * 8)) - 1;
135 /* Align to 32 bits */
137 host = (void *)((uintptr_t)host & ~3);
139 old_data = ops->lw(state, opcode, host, addr);
141 data = (data << (shift * 8)) | (old_data & mask);
143 ops->sw(state, opcode, host, addr, data);
146 static void lightrec_swc2(struct lightrec_state *state, union code op,
147 const struct lightrec_mem_map_ops *ops,
148 void *host, u32 addr)
150 u32 data = state->ops.cop2_ops.mfc(state, op.opcode, op.i.rt);
152 ops->sw(state, op.opcode, host, addr, data);
155 static u32 lightrec_lwl(struct lightrec_state *state,
156 const struct lightrec_mem_map_ops *ops,
157 u32 opcode, void *host, u32 addr, u32 data)
159 unsigned int shift = addr & 0x3;
160 unsigned int mask = (1 << (24 - shift * 8)) - 1;
163 /* Align to 32 bits */
165 host = (void *)((uintptr_t)host & ~3);
167 old_data = ops->lw(state, opcode, host, addr);
169 return (data & mask) | (old_data << (24 - shift * 8));
172 static u32 lightrec_lwr(struct lightrec_state *state,
173 const struct lightrec_mem_map_ops *ops,
174 u32 opcode, void *host, u32 addr, u32 data)
176 unsigned int shift = addr & 0x3;
177 unsigned int mask = GENMASK(31, 32 - shift * 8);
180 /* Align to 32 bits */
182 host = (void *)((uintptr_t)host & ~3);
184 old_data = ops->lw(state, opcode, host, addr);
186 return (data & mask) | (old_data >> (shift * 8));
189 static void lightrec_lwc2(struct lightrec_state *state, union code op,
190 const struct lightrec_mem_map_ops *ops,
191 void *host, u32 addr)
193 u32 data = ops->lw(state, op.opcode, host, addr);
195 state->ops.cop2_ops.mtc(state, op.opcode, op.i.rt, data);
198 static void lightrec_invalidate_map(struct lightrec_state *state,
199 const struct lightrec_mem_map *map, u32 addr)
201 if (map == &state->maps[PSX_MAP_KERNEL_USER_RAM])
202 state->code_lut[lut_offset(addr)] = NULL;
205 static const struct lightrec_mem_map *
206 lightrec_get_map(struct lightrec_state *state, u32 kaddr)
210 for (i = 0; i < state->nb_maps; i++) {
211 const struct lightrec_mem_map *map = &state->maps[i];
213 if (kaddr >= map->pc && kaddr < map->pc + map->length)
220 u32 lightrec_rw(struct lightrec_state *state, union code op,
221 u32 addr, u32 data, u16 *flags)
223 const struct lightrec_mem_map *map;
224 const struct lightrec_mem_map_ops *ops;
225 u32 kaddr, pc, opcode = op.opcode;
228 addr += (s16) op.i.imm;
229 kaddr = kunseg(addr);
231 map = lightrec_get_map(state, kaddr);
233 __segfault_cb(state, addr);
239 while (map->mirror_of)
240 map = map->mirror_of;
242 host = (void *)((uintptr_t)map->address + kaddr - pc);
244 if (unlikely(map->ops)) {
246 *flags |= LIGHTREC_HW_IO;
251 *flags |= LIGHTREC_DIRECT_IO;
253 ops = &lightrec_default_ops;
258 ops->sb(state, opcode, host, addr, (u8) data);
261 ops->sh(state, opcode, host, addr, (u16) data);
264 lightrec_swl(state, ops, opcode, host, addr, data);
267 lightrec_swr(state, ops, opcode, host, addr, data);
270 ops->sw(state, opcode, host, addr, data);
273 lightrec_swc2(state, op, ops, host, addr);
276 return (s32) (s8) ops->lb(state, opcode, host, addr);
278 return ops->lb(state, opcode, host, addr);
280 return (s32) (s16) ops->lh(state, opcode, host, addr);
282 return ops->lh(state, opcode, host, addr);
284 lightrec_lwc2(state, op, ops, host, addr);
287 return lightrec_lwl(state, ops, opcode, host, addr, data);
289 return lightrec_lwr(state, ops, opcode, host, addr, data);
292 return ops->lw(state, opcode, host, addr);
296 static void lightrec_rw_helper(struct lightrec_state *state,
297 union code op, u16 *flags)
299 u32 ret = lightrec_rw(state, op,
300 state->native_reg_cache[op.i.rs],
301 state->native_reg_cache[op.i.rt], flags);
312 state->native_reg_cache[op.i.rt] = ret;
313 default: /* fall-through */
318 static void lightrec_rw_cb(struct lightrec_state *state, union code op)
320 lightrec_rw_helper(state, op, NULL);
323 static void lightrec_rw_generic_cb(struct lightrec_state *state,
324 struct opcode *op, struct block *block)
326 bool was_tagged = op->flags & (LIGHTREC_HW_IO | LIGHTREC_DIRECT_IO);
328 lightrec_rw_helper(state, op->c, &op->flags);
331 pr_debug("Opcode of block at PC 0x%08x offset 0x%x has been "
332 "tagged - flag for recompilation\n",
333 block->pc, op->offset << 2);
335 block->flags |= BLOCK_SHOULD_RECOMPILE;
339 u32 lightrec_mfc(struct lightrec_state *state, union code op)
341 bool is_cfc = (op.i.op == OP_CP0 && op.r.rs == OP_CP0_CFC0) ||
342 (op.i.op == OP_CP2 && op.r.rs == OP_CP2_BASIC_CFC2);
343 u32 (*func)(struct lightrec_state *, u32, u8);
344 const struct lightrec_cop_ops *ops;
346 if (op.i.op == OP_CP0)
347 ops = &state->ops.cop0_ops;
349 ops = &state->ops.cop2_ops;
356 return (*func)(state, op.opcode, op.r.rd);
359 static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
361 u32 rt = lightrec_mfc(state, op);
364 state->native_reg_cache[op.r.rt] = rt;
367 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data)
369 bool is_ctc = (op.i.op == OP_CP0 && op.r.rs == OP_CP0_CTC0) ||
370 (op.i.op == OP_CP2 && op.r.rs == OP_CP2_BASIC_CTC2);
371 void (*func)(struct lightrec_state *, u32, u8, u32);
372 const struct lightrec_cop_ops *ops;
374 if (op.i.op == OP_CP0)
375 ops = &state->ops.cop0_ops;
377 ops = &state->ops.cop2_ops;
384 (*func)(state, op.opcode, op.r.rd, data);
387 static void lightrec_mtc_cb(struct lightrec_state *state, union code op)
389 lightrec_mtc(state, op, state->native_reg_cache[op.r.rt]);
392 static void lightrec_rfe_cb(struct lightrec_state *state, union code op)
396 /* Read CP0 Status register (r12) */
397 status = state->ops.cop0_ops.mfc(state, op.opcode, 12);
399 /* Switch the bits */
400 status = ((status & 0x3c) >> 2) | (status & ~0xf);
403 state->ops.cop0_ops.ctc(state, op.opcode, 12, status);
406 static void lightrec_cp_cb(struct lightrec_state *state, union code op)
408 void (*func)(struct lightrec_state *, u32);
410 if ((op.opcode >> 25) & 1)
411 func = state->ops.cop2_ops.op;
413 func = state->ops.cop0_ops.op;
415 (*func)(state, op.opcode);
418 static void lightrec_syscall_cb(struct lightrec_state *state, union code op)
420 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SYSCALL);
423 static void lightrec_break_cb(struct lightrec_state *state, union code op)
425 lightrec_set_exit_flags(state, LIGHTREC_EXIT_BREAK);
428 struct block * lightrec_get_block(struct lightrec_state *state, u32 pc)
430 struct block *block = lightrec_find_block(state->block_cache, pc);
432 if (block && lightrec_block_is_outdated(block)) {
433 pr_debug("Block at PC 0x%08x is outdated!\n", block->pc);
435 /* Make sure the recompiler isn't processing the block we'll
437 if (ENABLE_THREADED_COMPILER)
438 lightrec_recompiler_remove(state->rec, block);
440 lightrec_unregister_block(state->block_cache, block);
441 remove_from_code_lut(state->block_cache, block);
442 lightrec_free_block(block);
447 block = lightrec_precompile_block(state, pc);
449 pr_err("Unable to recompile block at PC 0x%x\n", pc);
450 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
454 lightrec_register_block(state->block_cache, block);
460 static void * get_next_block_func(struct lightrec_state *state, u32 pc)
463 bool should_recompile;
467 func = state->code_lut[lut_offset(pc)];
468 if (func && func != state->get_next_block)
471 block = lightrec_get_block(state, pc);
473 if (unlikely(!block))
476 should_recompile = block->flags & BLOCK_SHOULD_RECOMPILE &&
477 !(block->flags & BLOCK_IS_DEAD);
479 if (unlikely(should_recompile)) {
480 pr_debug("Block at PC 0x%08x should recompile\n", pc);
482 lightrec_unregister(MEM_FOR_CODE, block->code_size);
484 if (ENABLE_THREADED_COMPILER)
485 lightrec_recompiler_add(state->rec, block);
487 lightrec_compile_block(block);
490 if (ENABLE_THREADED_COMPILER && likely(!should_recompile))
491 func = lightrec_recompiler_run_first_pass(block, &pc);
493 func = block->function;
498 /* Block wasn't compiled yet - run the interpreter */
499 if (!ENABLE_THREADED_COMPILER &&
500 ((ENABLE_FIRST_PASS && likely(!should_recompile)) ||
501 unlikely(block->flags & BLOCK_NEVER_COMPILE)))
502 pc = lightrec_emulate_block(block, pc);
504 if (likely(!(block->flags & BLOCK_NEVER_COMPILE))) {
505 /* Then compile it using the profiled data */
506 if (ENABLE_THREADED_COMPILER)
507 lightrec_recompiler_add(state->rec, block);
509 lightrec_compile_block(block);
512 if (state->exit_flags != LIGHTREC_EXIT_NORMAL ||
513 state->current_cycle >= state->target_cycle) {
520 static s32 c_generic_function_wrapper(struct lightrec_state *state,
522 void (*f)(struct lightrec_state *,
525 struct opcode *op, struct block *block)
527 state->current_cycle = state->target_cycle - cycles_delta;
529 (*f)(state, op, block);
531 return state->target_cycle - state->current_cycle;
534 static s32 c_function_wrapper(struct lightrec_state *state, s32 cycles_delta,
535 void (*f)(struct lightrec_state *, union code),
538 state->current_cycle = state->target_cycle - cycles_delta;
542 return state->target_cycle - state->current_cycle;
545 static struct block * generate_wrapper(struct lightrec_state *state,
546 void *f, bool generic)
552 jit_word_t code_size;
553 jit_node_t *to_tramp, *to_fn_epilog;
555 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
559 _jit = jit_new_state();
563 jit_name("RW wrapper");
564 jit_note(__FILE__, __LINE__);
566 /* Wrapper entry point */
569 stack_ptr = jit_allocai(sizeof(uintptr_t) * NUM_TEMPS);
571 for (i = 0; i < NUM_TEMPS; i++)
572 jit_stxi(stack_ptr + i * sizeof(uintptr_t), JIT_FP, JIT_R(i));
574 /* Jump to the trampoline */
575 to_tramp = jit_jmpi();
577 /* The trampoline will jump back here */
578 to_fn_epilog = jit_label();
580 for (i = 0; i < NUM_TEMPS; i++)
581 jit_ldxi(JIT_R(i), JIT_FP, stack_ptr + i * sizeof(uintptr_t));
586 /* Trampoline entry point.
587 * The sole purpose of the trampoline is to cheese Lightning not to
588 * save/restore the callee-saved register LIGHTREC_REG_CYCLE, since we
589 * do want to return to the caller with this register modified. */
595 jit_pushargr(LIGHTREC_REG_STATE);
596 jit_pushargr(LIGHTREC_REG_CYCLE);
597 jit_pushargi((uintptr_t)f);
598 jit_pushargr(JIT_R0);
600 jit_pushargr(JIT_R1);
601 jit_finishi(c_generic_function_wrapper);
603 jit_finishi(c_function_wrapper);
607 jit_retval_i(LIGHTREC_REG_CYCLE);
609 jit_retval(LIGHTREC_REG_CYCLE);
612 jit_patch_at(jit_jmpi(), to_fn_epilog);
615 block->state = state;
617 block->function = jit_emit();
618 block->opcode_list = NULL;
622 jit_get_code(&code_size);
623 lightrec_register(MEM_FOR_CODE, code_size);
625 block->code_size = code_size;
627 if (ENABLE_DISASSEMBLER) {
628 pr_debug("Wrapper block:\n");
636 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
638 pr_err("Unable to compile wrapper: Out of memory\n");
642 static struct block * generate_dispatcher(struct lightrec_state *state)
646 jit_node_t *to_end, *to_end2, *to_c, *loop, *addr, *addr2;
649 jit_word_t code_size;
651 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
655 _jit = jit_new_state();
659 jit_name("dispatcher");
660 jit_note(__FILE__, __LINE__);
665 jit_getarg(JIT_R0, jit_arg());
667 jit_getarg_i(LIGHTREC_REG_CYCLE, jit_arg());
669 jit_getarg(LIGHTREC_REG_CYCLE, jit_arg());
672 /* Force all callee-saved registers to be pushed on the stack */
673 for (i = 0; i < NUM_REGS; i++)
674 jit_movr(JIT_V(i), JIT_V(i));
676 /* Pass lightrec_state structure to blocks, using the last callee-saved
677 * register that Lightning provides */
678 jit_movi(LIGHTREC_REG_STATE, (intptr_t) state);
682 /* Call the block's code */
685 /* The block will jump here, with the number of cycles remaining in
686 * LIGHTREC_REG_CYCLE */
687 addr2 = jit_indirect();
689 /* Jump to end if state->target_cycle < state->current_cycle */
690 to_end = jit_blei(LIGHTREC_REG_CYCLE, 0);
692 /* Convert next PC to KUNSEG and avoid mirrors */
693 ram_len = state->maps[PSX_MAP_KERNEL_USER_RAM].length;
694 jit_andi(JIT_R0, JIT_V0, 0x10000000 | (ram_len - 1));
695 to_c = jit_bgei(JIT_R0, ram_len);
697 /* Fast path: code is running from RAM, use the code LUT */
699 jit_lshi(JIT_R0, JIT_R0, 1);
701 jit_addr(JIT_R0, JIT_R0, LIGHTREC_REG_STATE);
702 jit_ldxi(JIT_R0, JIT_R0, offsetof(struct lightrec_state, code_lut));
704 /* If we get non-NULL, loop */
705 jit_patch_at(jit_bnei(JIT_R0, 0), loop);
707 /* Slow path: call C function get_next_block_func() */
710 if (ENABLE_FIRST_PASS) {
711 /* We may call the interpreter - update state->current_cycle */
712 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
713 offsetof(struct lightrec_state, target_cycle));
714 jit_subr(JIT_R1, JIT_R2, LIGHTREC_REG_CYCLE);
715 jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
716 LIGHTREC_REG_STATE, JIT_R1);
719 /* The code LUT will be set to this address when the block at the target
720 * PC has been preprocessed but not yet compiled by the threaded
722 addr = jit_indirect();
724 /* Get the next block */
726 jit_pushargr(LIGHTREC_REG_STATE);
727 jit_pushargr(JIT_V0);
728 jit_finishi(&get_next_block_func);
731 if (ENABLE_FIRST_PASS) {
732 /* The interpreter may have updated state->current_cycle and
733 * state->target_cycle - recalc the delta */
734 jit_ldxi_i(JIT_R1, LIGHTREC_REG_STATE,
735 offsetof(struct lightrec_state, current_cycle));
736 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
737 offsetof(struct lightrec_state, target_cycle));
738 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, JIT_R1);
741 /* If we get non-NULL, loop */
742 jit_patch_at(jit_bnei(JIT_R0, 0), loop);
744 to_end2 = jit_jmpi();
746 /* When exiting, the recompiled code will jump to that address */
747 jit_note(__FILE__, __LINE__);
750 /* Store back the next_pc to the lightrec_state structure */
751 offset = offsetof(struct lightrec_state, next_pc);
752 jit_stxi_i(offset, LIGHTREC_REG_STATE, JIT_V0);
756 jit_retr(LIGHTREC_REG_CYCLE);
759 block->state = state;
761 block->function = jit_emit();
762 block->opcode_list = NULL;
766 jit_get_code(&code_size);
767 lightrec_register(MEM_FOR_CODE, code_size);
769 block->code_size = code_size;
771 state->eob_wrapper_func = jit_address(addr2);
772 state->get_next_block = jit_address(addr);
774 if (ENABLE_DISASSEMBLER) {
775 pr_debug("Dispatcher block:\n");
784 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
786 pr_err("Unable to compile dispatcher: Out of memory\n");
790 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc)
792 u32 addr, kunseg_pc = kunseg(pc);
794 const struct lightrec_mem_map *map = lightrec_get_map(state, kunseg_pc);
796 addr = kunseg_pc - map->pc;
798 while (map->mirror_of)
799 map = map->mirror_of;
801 code = map->address + addr;
803 return (union code) *code;
806 static struct block * lightrec_precompile_block(struct lightrec_state *state,
812 u32 addr, kunseg_pc = kunseg(pc);
813 const struct lightrec_mem_map *map = lightrec_get_map(state, kunseg_pc);
819 addr = kunseg_pc - map->pc;
821 while (map->mirror_of)
822 map = map->mirror_of;
824 code = map->address + addr;
826 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
828 pr_err("Unable to recompile block: Out of memory\n");
832 list = lightrec_disassemble(state, code, &length);
834 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
839 block->state = state;
841 block->function = NULL;
842 block->opcode_list = list;
846 block->code_size = 0;
847 #if ENABLE_THREADED_COMPILER
848 block->op_list_freed = (atomic_flag)ATOMIC_FLAG_INIT;
850 block->nb_ops = length / sizeof(u32);
852 lightrec_optimize(block);
854 length = block->nb_ops * sizeof(u32);
856 lightrec_register(MEM_FOR_MIPS_CODE, length);
858 if (ENABLE_DISASSEMBLER) {
859 pr_debug("Disassembled block at PC: 0x%x\n", block->pc);
860 lightrec_print_disassembly(block, code, length);
863 pr_debug("Block size: %lu opcodes\n", block->nb_ops);
865 /* If the first opcode is an 'impossible' branch, never compile the
867 if (list->flags & LIGHTREC_EMULATE_BRANCH)
868 block->flags |= BLOCK_NEVER_COMPILE;
870 block->hash = lightrec_calculate_block_hash(block);
872 pr_debug("Recompile count: %u\n", state->nb_precompile++);
877 static bool lightrec_block_is_fully_tagged(struct block *block)
881 for (op = block->opcode_list; op; op = op->next) {
882 /* Verify that all load/stores of the opcode list
883 * Check all loads/stores of the opcode list and mark the
884 * block as fully compiled if they all have been tagged. */
885 switch (op->c.i.op) {
900 if (!(op->flags & (LIGHTREC_DIRECT_IO |
903 default: /* fall-through */
911 static void lightrec_reap_block(void *data)
913 struct block *block = data;
915 pr_debug("Reap dead block at PC 0x%08x\n", block->pc);
916 lightrec_free_block(block);
919 static void lightrec_reap_jit(void *data)
921 _jit_destroy_state(data);
924 int lightrec_compile_block(struct block *block)
926 struct lightrec_state *state = block->state;
927 struct lightrec_branch_target *target;
928 bool op_list_freed = false, fully_tagged = false;
929 struct block *block2;
931 jit_state_t *_jit, *oldjit;
932 jit_node_t *start_of_block;
933 bool skip_next = false;
934 jit_word_t code_size;
938 fully_tagged = lightrec_block_is_fully_tagged(block);
940 block->flags |= BLOCK_FULLY_TAGGED;
942 _jit = jit_new_state();
946 oldjit = block->_jit;
949 lightrec_regcache_reset(state->reg_cache);
951 state->nb_branches = 0;
952 state->nb_local_branches = 0;
953 state->nb_targets = 0;
958 start_of_block = jit_label();
960 for (elm = block->opcode_list; elm; elm = elm->next) {
961 next_pc = block->pc + elm->offset * sizeof(u32);
968 state->cycles += lightrec_cycles_of_opcode(elm->c);
970 if (elm->flags & LIGHTREC_EMULATE_BRANCH) {
971 pr_debug("Branch at offset 0x%x will be emulated\n",
973 lightrec_emit_eob(block, elm, next_pc);
974 skip_next = !(elm->flags & LIGHTREC_NO_DS);
975 } else if (elm->opcode) {
976 lightrec_rec_opcode(block, elm, next_pc);
977 skip_next = has_delay_slot(elm->c) &&
978 !(elm->flags & LIGHTREC_NO_DS);
980 /* FIXME: GNU Lightning on Windows seems to use our
981 * mapped registers as temporaries. Until the actual bug
982 * is found and fixed, unconditionally mark our
983 * registers as live here. */
984 lightrec_regcache_mark_live(state->reg_cache, _jit);
989 for (i = 0; i < state->nb_branches; i++)
990 jit_patch(state->branches[i]);
992 for (i = 0; i < state->nb_local_branches; i++) {
993 struct lightrec_branch *branch = &state->local_branches[i];
995 pr_debug("Patch local branch to offset 0x%x\n",
996 branch->target << 2);
998 if (branch->target == 0) {
999 jit_patch_at(branch->branch, start_of_block);
1003 for (j = 0; j < state->nb_targets; j++) {
1004 if (state->targets[j].offset == branch->target) {
1005 jit_patch_at(branch->branch,
1006 state->targets[j].label);
1011 if (j == state->nb_targets)
1012 pr_err("Unable to find branch target\n");
1015 jit_ldxi(JIT_R0, LIGHTREC_REG_STATE,
1016 offsetof(struct lightrec_state, eob_wrapper_func));
1023 block->function = jit_emit();
1024 block->flags &= ~BLOCK_SHOULD_RECOMPILE;
1026 /* Add compiled function to the LUT */
1027 state->code_lut[lut_offset(block->pc)] = block->function;
1029 /* Fill code LUT with the block's entry points */
1030 for (i = 0; i < state->nb_targets; i++) {
1031 target = &state->targets[i];
1033 if (target->offset) {
1034 offset = lut_offset(block->pc) + target->offset;
1035 state->code_lut[offset] = jit_address(target->label);
1039 /* Detect old blocks that have been covered by the new one */
1040 for (i = 0; i < state->nb_targets; i++) {
1041 target = &state->targets[i];
1043 if (!target->offset)
1046 offset = block->pc + target->offset * sizeof(u32);
1047 block2 = lightrec_find_block(state->block_cache, offset);
1049 /* No need to check if block2 is compilable - it must
1050 * be, otherwise block wouldn't be compilable either */
1052 block2->flags |= BLOCK_IS_DEAD;
1054 pr_debug("Reap block 0x%08x as it's covered by block "
1055 "0x%08x\n", block2->pc, block->pc);
1057 lightrec_unregister_block(state->block_cache, block2);
1059 if (ENABLE_THREADED_COMPILER) {
1060 lightrec_recompiler_remove(state->rec, block2);
1061 lightrec_reaper_add(state->reaper,
1062 lightrec_reap_block,
1065 lightrec_free_block(block2);
1070 jit_get_code(&code_size);
1071 lightrec_register(MEM_FOR_CODE, code_size);
1073 block->code_size = code_size;
1075 if (ENABLE_DISASSEMBLER) {
1076 pr_debug("Compiling block at PC: 0x%x\n", block->pc);
1082 #if ENABLE_THREADED_COMPILER
1084 op_list_freed = atomic_flag_test_and_set(&block->op_list_freed);
1086 if (fully_tagged && !op_list_freed) {
1087 pr_debug("Block PC 0x%08x is fully tagged"
1088 " - free opcode list\n", block->pc);
1089 lightrec_free_opcode_list(state, block->opcode_list);
1090 block->opcode_list = NULL;
1094 pr_debug("Block 0x%08x recompiled, reaping old jit context.\n",
1097 if (ENABLE_THREADED_COMPILER)
1098 lightrec_reaper_add(state->reaper,
1099 lightrec_reap_jit, oldjit);
1101 _jit_destroy_state(oldjit);
1107 u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
1109 s32 (*func)(void *, s32) = (void *)state->dispatcher->function;
1113 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1115 /* Handle the cycle counter overflowing */
1116 if (unlikely(target_cycle < state->current_cycle))
1117 target_cycle = UINT_MAX;
1119 state->target_cycle = target_cycle;
1121 block_trace = get_next_block_func(state, pc);
1123 cycles_delta = state->target_cycle - state->current_cycle;
1125 cycles_delta = (*func)(block_trace, cycles_delta);
1127 state->current_cycle = state->target_cycle - cycles_delta;
1130 if (ENABLE_THREADED_COMPILER)
1131 lightrec_reaper_reap(state->reaper);
1133 return state->next_pc;
1136 u32 lightrec_execute_one(struct lightrec_state *state, u32 pc)
1138 return lightrec_execute(state, pc, state->current_cycle);
1141 u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc)
1143 struct block *block = lightrec_get_block(state, pc);
1147 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1149 return lightrec_emulate_block(block, pc);
1152 void lightrec_free_block(struct block *block)
1154 lightrec_unregister(MEM_FOR_MIPS_CODE, block->nb_ops * sizeof(u32));
1155 if (block->opcode_list)
1156 lightrec_free_opcode_list(block->state, block->opcode_list);
1158 _jit_destroy_state(block->_jit);
1159 lightrec_unregister(MEM_FOR_CODE, block->code_size);
1160 lightrec_free(block->state, MEM_FOR_IR, sizeof(*block), block);
1163 struct lightrec_state * lightrec_init(char *argv0,
1164 const struct lightrec_mem_map *map,
1166 const struct lightrec_ops *ops)
1168 struct lightrec_state *state;
1170 /* Sanity-check ops */
1172 !ops->cop0_ops.mfc || !ops->cop0_ops.cfc || !ops->cop0_ops.mtc ||
1173 !ops->cop0_ops.ctc || !ops->cop0_ops.op ||
1174 !ops->cop2_ops.mfc || !ops->cop2_ops.cfc || !ops->cop2_ops.mtc ||
1175 !ops->cop2_ops.ctc || !ops->cop2_ops.op) {
1176 pr_err("Missing callbacks in lightrec_ops structure\n");
1182 state = calloc(1, sizeof(*state) +
1183 sizeof(*state->code_lut) * CODE_LUT_SIZE);
1185 goto err_finish_jit;
1187 lightrec_register(MEM_FOR_LIGHTREC, sizeof(*state) +
1188 sizeof(*state->code_lut) * CODE_LUT_SIZE);
1191 state->tinymm = tinymm_init(malloc, free, 4096);
1193 goto err_free_state;
1196 state->block_cache = lightrec_blockcache_init(state);
1197 if (!state->block_cache)
1198 goto err_free_tinymm;
1200 state->reg_cache = lightrec_regcache_init(state);
1201 if (!state->reg_cache)
1202 goto err_free_block_cache;
1204 if (ENABLE_THREADED_COMPILER) {
1205 state->rec = lightrec_recompiler_init(state);
1207 goto err_free_reg_cache;
1209 state->reaper = lightrec_reaper_init(state);
1211 goto err_free_recompiler;
1214 state->nb_maps = nb;
1217 memcpy(&state->ops, ops, sizeof(*ops));
1219 state->dispatcher = generate_dispatcher(state);
1220 if (!state->dispatcher)
1221 goto err_free_reaper;
1223 state->rw_generic_wrapper = generate_wrapper(state,
1224 lightrec_rw_generic_cb,
1226 if (!state->rw_generic_wrapper)
1227 goto err_free_dispatcher;
1229 state->rw_wrapper = generate_wrapper(state, lightrec_rw_cb, false);
1230 if (!state->rw_wrapper)
1231 goto err_free_generic_rw_wrapper;
1233 state->mfc_wrapper = generate_wrapper(state, lightrec_mfc_cb, false);
1234 if (!state->mfc_wrapper)
1235 goto err_free_rw_wrapper;
1237 state->mtc_wrapper = generate_wrapper(state, lightrec_mtc_cb, false);
1238 if (!state->mtc_wrapper)
1239 goto err_free_mfc_wrapper;
1241 state->rfe_wrapper = generate_wrapper(state, lightrec_rfe_cb, false);
1242 if (!state->rfe_wrapper)
1243 goto err_free_mtc_wrapper;
1245 state->cp_wrapper = generate_wrapper(state, lightrec_cp_cb, false);
1246 if (!state->cp_wrapper)
1247 goto err_free_rfe_wrapper;
1249 state->syscall_wrapper = generate_wrapper(state, lightrec_syscall_cb,
1251 if (!state->syscall_wrapper)
1252 goto err_free_cp_wrapper;
1254 state->break_wrapper = generate_wrapper(state, lightrec_break_cb,
1256 if (!state->break_wrapper)
1257 goto err_free_syscall_wrapper;
1259 state->rw_generic_func = state->rw_generic_wrapper->function;
1260 state->rw_func = state->rw_wrapper->function;
1261 state->mfc_func = state->mfc_wrapper->function;
1262 state->mtc_func = state->mtc_wrapper->function;
1263 state->rfe_func = state->rfe_wrapper->function;
1264 state->cp_func = state->cp_wrapper->function;
1265 state->syscall_func = state->syscall_wrapper->function;
1266 state->break_func = state->break_wrapper->function;
1268 map = &state->maps[PSX_MAP_BIOS];
1269 state->offset_bios = (uintptr_t)map->address - map->pc;
1271 map = &state->maps[PSX_MAP_SCRATCH_PAD];
1272 state->offset_scratch = (uintptr_t)map->address - map->pc;
1274 map = &state->maps[PSX_MAP_KERNEL_USER_RAM];
1275 state->offset_ram = (uintptr_t)map->address - map->pc;
1277 if (state->maps[PSX_MAP_MIRROR1].address == map->address + 0x200000 &&
1278 state->maps[PSX_MAP_MIRROR2].address == map->address + 0x400000 &&
1279 state->maps[PSX_MAP_MIRROR3].address == map->address + 0x600000)
1280 state->mirrors_mapped = true;
1284 err_free_syscall_wrapper:
1285 lightrec_free_block(state->syscall_wrapper);
1286 err_free_cp_wrapper:
1287 lightrec_free_block(state->cp_wrapper);
1288 err_free_rfe_wrapper:
1289 lightrec_free_block(state->rfe_wrapper);
1290 err_free_mtc_wrapper:
1291 lightrec_free_block(state->mtc_wrapper);
1292 err_free_mfc_wrapper:
1293 lightrec_free_block(state->mfc_wrapper);
1294 err_free_rw_wrapper:
1295 lightrec_free_block(state->rw_wrapper);
1296 err_free_generic_rw_wrapper:
1297 lightrec_free_block(state->rw_generic_wrapper);
1298 err_free_dispatcher:
1299 lightrec_free_block(state->dispatcher);
1301 if (ENABLE_THREADED_COMPILER)
1302 lightrec_reaper_destroy(state->reaper);
1303 err_free_recompiler:
1304 if (ENABLE_THREADED_COMPILER)
1305 lightrec_free_recompiler(state->rec);
1307 lightrec_free_regcache(state->reg_cache);
1308 err_free_block_cache:
1309 lightrec_free_block_cache(state->block_cache);
1312 tinymm_shutdown(state->tinymm);
1315 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
1316 sizeof(*state->code_lut) * CODE_LUT_SIZE);
1323 void lightrec_destroy(struct lightrec_state *state)
1325 if (ENABLE_THREADED_COMPILER) {
1326 lightrec_free_recompiler(state->rec);
1327 lightrec_reaper_destroy(state->reaper);
1330 lightrec_free_regcache(state->reg_cache);
1331 lightrec_free_block_cache(state->block_cache);
1332 lightrec_free_block(state->dispatcher);
1333 lightrec_free_block(state->rw_generic_wrapper);
1334 lightrec_free_block(state->rw_wrapper);
1335 lightrec_free_block(state->mfc_wrapper);
1336 lightrec_free_block(state->mtc_wrapper);
1337 lightrec_free_block(state->rfe_wrapper);
1338 lightrec_free_block(state->cp_wrapper);
1339 lightrec_free_block(state->syscall_wrapper);
1340 lightrec_free_block(state->break_wrapper);
1344 tinymm_shutdown(state->tinymm);
1346 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
1347 sizeof(*state->code_lut) * CODE_LUT_SIZE);
1351 void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len)
1353 u32 kaddr = kunseg(addr & ~0x3);
1354 const struct lightrec_mem_map *map = lightrec_get_map(state, kaddr);
1357 while (map->mirror_of)
1358 map = map->mirror_of;
1360 if (map != &state->maps[PSX_MAP_KERNEL_USER_RAM])
1363 /* Handle mirrors */
1364 kaddr &= (state->maps[PSX_MAP_KERNEL_USER_RAM].length - 1);
1366 for (; len > 4; len -= 4, kaddr += 4)
1367 lightrec_invalidate_map(state, map, kaddr);
1369 lightrec_invalidate_map(state, map, kaddr);
1373 void lightrec_invalidate_all(struct lightrec_state *state)
1375 memset(state->code_lut, 0, sizeof(*state->code_lut) * CODE_LUT_SIZE);
1378 void lightrec_set_invalidate_mode(struct lightrec_state *state, bool dma_only)
1380 if (state->invalidate_from_dma_only != dma_only)
1381 lightrec_invalidate_all(state);
1383 state->invalidate_from_dma_only = dma_only;
1386 void lightrec_set_exit_flags(struct lightrec_state *state, u32 flags)
1388 if (flags != LIGHTREC_EXIT_NORMAL) {
1389 state->exit_flags |= flags;
1390 state->target_cycle = state->current_cycle;
1394 u32 lightrec_exit_flags(struct lightrec_state *state)
1396 return state->exit_flags;
1399 void lightrec_dump_registers(struct lightrec_state *state, u32 regs[34])
1401 memcpy(regs, state->native_reg_cache, sizeof(state->native_reg_cache));
1404 void lightrec_restore_registers(struct lightrec_state *state, u32 regs[34])
1406 memcpy(state->native_reg_cache, regs, sizeof(state->native_reg_cache));
1409 u32 lightrec_current_cycle_count(const struct lightrec_state *state)
1411 return state->current_cycle;
1414 void lightrec_reset_cycle_count(struct lightrec_state *state, u32 cycles)
1416 state->current_cycle = cycles;
1418 if (state->target_cycle < cycles)
1419 state->target_cycle = cycles;
1422 void lightrec_set_target_cycle_count(struct lightrec_state *state, u32 cycles)
1424 if (state->exit_flags == LIGHTREC_EXIT_NORMAL) {
1425 if (cycles < state->current_cycle)
1426 cycles = state->current_cycle;
1428 state->target_cycle = cycles;