1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net>
6 #include "blockcache.h"
8 #include "disassembler.h"
10 #include "interpreter.h"
11 #include "lightrec-config.h"
12 #include "lightning-wrapper.h"
14 #include "memmanager.h"
16 #include "recompiler.h"
18 #include "optimizer.h"
19 #include "tlsf/tlsf.h"
24 #if ENABLE_THREADED_COMPILER
25 #include <stdatomic.h>
31 #define GENMASK(h, l) \
32 (((uintptr_t)-1 << (l)) & ((uintptr_t)-1 >> (__WORDSIZE - 1 - (h))))
34 static struct block * lightrec_precompile_block(struct lightrec_state *state,
36 static bool lightrec_block_is_fully_tagged(const struct block *block);
38 static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data);
39 static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg);
41 static void lightrec_default_sb(struct lightrec_state *state, u32 opcode,
42 void *host, u32 addr, u8 data)
46 if (!state->invalidate_from_dma_only)
47 lightrec_invalidate(state, addr, 1);
50 static void lightrec_default_sh(struct lightrec_state *state, u32 opcode,
51 void *host, u32 addr, u16 data)
53 *(u16 *)host = HTOLE16(data);
55 if (!state->invalidate_from_dma_only)
56 lightrec_invalidate(state, addr, 2);
59 static void lightrec_default_sw(struct lightrec_state *state, u32 opcode,
60 void *host, u32 addr, u32 data)
62 *(u32 *)host = HTOLE32(data);
64 if (!state->invalidate_from_dma_only)
65 lightrec_invalidate(state, addr, 4);
68 static u8 lightrec_default_lb(struct lightrec_state *state,
69 u32 opcode, void *host, u32 addr)
74 static u16 lightrec_default_lh(struct lightrec_state *state,
75 u32 opcode, void *host, u32 addr)
77 return LE16TOH(*(u16 *)host);
80 static u32 lightrec_default_lw(struct lightrec_state *state,
81 u32 opcode, void *host, u32 addr)
83 return LE32TOH(*(u32 *)host);
86 static const struct lightrec_mem_map_ops lightrec_default_ops = {
87 .sb = lightrec_default_sb,
88 .sh = lightrec_default_sh,
89 .sw = lightrec_default_sw,
90 .lb = lightrec_default_lb,
91 .lh = lightrec_default_lh,
92 .lw = lightrec_default_lw,
95 static void __segfault_cb(struct lightrec_state *state, u32 addr,
96 const struct block *block)
98 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
99 pr_err("Segmentation fault in recompiled code: invalid "
100 "load/store at address 0x%08x\n", addr);
102 pr_err("Was executing block PC 0x%08x\n", block->pc);
105 static void lightrec_swl(struct lightrec_state *state,
106 const struct lightrec_mem_map_ops *ops,
107 u32 opcode, void *host, u32 addr, u32 data)
109 unsigned int shift = addr & 0x3;
110 unsigned int mask = GENMASK(31, (shift + 1) * 8);
113 /* Align to 32 bits */
115 host = (void *)((uintptr_t)host & ~3);
117 old_data = ops->lw(state, opcode, host, addr);
119 data = (data >> ((3 - shift) * 8)) | (old_data & mask);
121 ops->sw(state, opcode, host, addr, data);
124 static void lightrec_swr(struct lightrec_state *state,
125 const struct lightrec_mem_map_ops *ops,
126 u32 opcode, void *host, u32 addr, u32 data)
128 unsigned int shift = addr & 0x3;
129 unsigned int mask = (1 << (shift * 8)) - 1;
132 /* Align to 32 bits */
134 host = (void *)((uintptr_t)host & ~3);
136 old_data = ops->lw(state, opcode, host, addr);
138 data = (data << (shift * 8)) | (old_data & mask);
140 ops->sw(state, opcode, host, addr, data);
143 static void lightrec_swc2(struct lightrec_state *state, union code op,
144 const struct lightrec_mem_map_ops *ops,
145 void *host, u32 addr)
147 u32 data = lightrec_mfc2(state, op.i.rt);
149 ops->sw(state, op.opcode, host, addr, data);
152 static u32 lightrec_lwl(struct lightrec_state *state,
153 const struct lightrec_mem_map_ops *ops,
154 u32 opcode, void *host, u32 addr, u32 data)
156 unsigned int shift = addr & 0x3;
157 unsigned int mask = (1 << (24 - shift * 8)) - 1;
160 /* Align to 32 bits */
162 host = (void *)((uintptr_t)host & ~3);
164 old_data = ops->lw(state, opcode, host, addr);
166 return (data & mask) | (old_data << (24 - shift * 8));
169 static u32 lightrec_lwr(struct lightrec_state *state,
170 const struct lightrec_mem_map_ops *ops,
171 u32 opcode, void *host, u32 addr, u32 data)
173 unsigned int shift = addr & 0x3;
174 unsigned int mask = GENMASK(31, 32 - shift * 8);
177 /* Align to 32 bits */
179 host = (void *)((uintptr_t)host & ~3);
181 old_data = ops->lw(state, opcode, host, addr);
183 return (data & mask) | (old_data >> (shift * 8));
186 static void lightrec_lwc2(struct lightrec_state *state, union code op,
187 const struct lightrec_mem_map_ops *ops,
188 void *host, u32 addr)
190 u32 data = ops->lw(state, op.opcode, host, addr);
192 lightrec_mtc2(state, op.i.rt, data);
195 static void lightrec_invalidate_map(struct lightrec_state *state,
196 const struct lightrec_mem_map *map, u32 addr, u32 len)
198 if (map == &state->maps[PSX_MAP_KERNEL_USER_RAM]) {
199 memset(lut_address(state, lut_offset(addr)), 0,
200 ((len + 3) / 4) * lut_elm_size(state));
205 lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr)
207 const struct lightrec_mem_map *map;
210 for (i = 0; i < state->nb_maps; i++) {
211 map = &state->maps[i];
213 if (kaddr >= map->pc && kaddr < map->pc + map->length)
214 return (enum psx_map) i;
217 return PSX_MAP_UNKNOWN;
220 const struct lightrec_mem_map *
221 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr)
223 const struct lightrec_mem_map *map;
227 idx = lightrec_get_map_idx(state, kaddr);
228 if (idx == PSX_MAP_UNKNOWN)
231 map = &state->maps[idx];
232 addr = kaddr - map->pc;
234 while (map->mirror_of)
235 map = map->mirror_of;
238 *host = map->address + addr;
243 u32 lightrec_rw(struct lightrec_state *state, union code op,
244 u32 addr, u32 data, u16 *flags, struct block *block)
246 const struct lightrec_mem_map *map;
247 const struct lightrec_mem_map_ops *ops;
248 u32 opcode = op.opcode;
251 addr += (s16) op.i.imm;
253 map = lightrec_get_map(state, &host, kunseg(addr));
255 __segfault_cb(state, addr, block);
259 if (unlikely(map->ops)) {
260 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
261 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
265 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
266 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
268 ops = &lightrec_default_ops;
273 ops->sb(state, opcode, host, addr, (u8) data);
276 ops->sh(state, opcode, host, addr, (u16) data);
279 lightrec_swl(state, ops, opcode, host, addr, data);
282 lightrec_swr(state, ops, opcode, host, addr, data);
285 ops->sw(state, opcode, host, addr, data);
288 lightrec_swc2(state, op, ops, host, addr);
291 return (s32) (s8) ops->lb(state, opcode, host, addr);
293 return ops->lb(state, opcode, host, addr);
295 return (s32) (s16) ops->lh(state, opcode, host, addr);
297 return ops->lh(state, opcode, host, addr);
299 lightrec_lwc2(state, op, ops, host, addr);
302 return lightrec_lwl(state, ops, opcode, host, addr, data);
304 return lightrec_lwr(state, ops, opcode, host, addr, data);
307 return ops->lw(state, opcode, host, addr);
311 static void lightrec_rw_helper(struct lightrec_state *state,
312 union code op, u16 *flags,
315 u32 ret = lightrec_rw(state, op, state->regs.gpr[op.i.rs],
316 state->regs.gpr[op.i.rt], flags, block);
327 state->regs.gpr[op.i.rt] = ret;
334 static void lightrec_rw_cb(struct lightrec_state *state)
336 lightrec_rw_helper(state, (union code)state->c_wrapper_arg, NULL, NULL);
339 static void lightrec_rw_generic_cb(struct lightrec_state *state)
344 u32 arg = state->c_wrapper_arg;
345 u16 offset = (u16)arg;
347 block = lightrec_find_block_from_lut(state->block_cache,
348 arg >> 16, state->next_pc);
349 if (unlikely(!block)) {
350 pr_err("rw_generic: No block found in LUT for PC 0x%x offset 0x%x\n",
351 state->next_pc, offset);
355 op = &block->opcode_list[offset];
356 was_tagged = LIGHTREC_FLAGS_GET_IO_MODE(op->flags);
358 lightrec_rw_helper(state, op->c, &op->flags, block);
361 pr_debug("Opcode of block at PC 0x%08x has been tagged - flag "
362 "for recompilation\n", block->pc);
364 block->flags |= BLOCK_SHOULD_RECOMPILE;
368 static u32 clamp_s32(s32 val, s32 min, s32 max)
370 return val < min ? min : val > max ? max : val;
373 static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg)
375 s16 gteir1, gteir2, gteir3;
385 return (s32)(s16) state->regs.cp2d[reg];
391 return (u16) state->regs.cp2d[reg];
394 gteir1 = (s16) state->regs.cp2d[9];
395 gteir2 = (s16) state->regs.cp2d[10];
396 gteir3 = (s16) state->regs.cp2d[11];
398 return clamp_s32(gteir1 >> 7, 0, 0x1f) << 0 |
399 clamp_s32(gteir2 >> 7, 0, 0x1f) << 5 |
400 clamp_s32(gteir3 >> 7, 0, 0x1f) << 10;
405 return state->regs.cp2d[reg];
409 u32 lightrec_mfc(struct lightrec_state *state, union code op)
411 if (op.i.op == OP_CP0)
412 return state->regs.cp0[op.r.rd];
413 else if (op.r.rs == OP_CP2_BASIC_MFC2)
414 return lightrec_mfc2(state, op.r.rd);
416 return state->regs.cp2c[op.r.rd];
419 static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
421 u32 status, oldstatus, cause;
429 /* Those registers are read-only */
436 status = state->regs.cp0[12];
439 if (status & ~data & BIT(16)) {
440 state->ops.enable_ram(state, true);
441 lightrec_invalidate_all(state);
442 } else if (~status & data & BIT(16)) {
443 state->ops.enable_ram(state, false);
448 state->regs.cp0[13] &= ~0x300;
449 state->regs.cp0[13] |= data & 0x300;
451 state->regs.cp0[reg] = data;
454 if (reg == 12 || reg == 13) {
455 cause = state->regs.cp0[13];
456 status = state->regs.cp0[12];
458 /* Handle software interrupts */
459 if (!!(status & cause & 0x300) & status)
460 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
462 /* Handle hardware interrupts */
463 if (reg == 12 && !(~status & 0x401) && (~oldstatus & 0x401))
464 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
468 static u32 count_leading_bits(s32 data)
473 #if __has_builtin(__builtin_clrsb)
474 return 1 + __builtin_clrsb(data);
478 data = (data ^ (data >> 31)) << 1;
488 static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data)
492 state->regs.cp2d[12] = state->regs.cp2d[13];
493 state->regs.cp2d[13] = state->regs.cp2d[14];
494 state->regs.cp2d[14] = data;
497 state->regs.cp2d[9] = (data << 7) & 0xf80;
498 state->regs.cp2d[10] = (data << 2) & 0xf80;
499 state->regs.cp2d[11] = (data >> 3) & 0xf80;
504 state->regs.cp2d[31] = count_leading_bits((s32) data);
507 state->regs.cp2d[reg] = data;
512 static void lightrec_ctc2(struct lightrec_state *state, u8 reg, u32 data)
522 data = (s32)(s16) data;
525 data = (data & 0x7ffff000) | !!(data & 0x7f87e000) << 31;
531 state->regs.cp2c[reg] = data;
534 void lightrec_mtc(struct lightrec_state *state, union code op, u32 data)
536 if (op.i.op == OP_CP0)
537 lightrec_mtc0(state, op.r.rd, data);
538 else if (op.r.rs == OP_CP2_BASIC_CTC2)
539 lightrec_ctc2(state, op.r.rd, data);
541 lightrec_mtc2(state, op.r.rd, data);
544 static void lightrec_mtc_cb(struct lightrec_state *state)
546 union code op = (union code) state->c_wrapper_arg;
548 lightrec_mtc(state, op, state->regs.gpr[op.r.rt]);
551 void lightrec_rfe(struct lightrec_state *state)
555 /* Read CP0 Status register (r12) */
556 status = state->regs.cp0[12];
558 /* Switch the bits */
559 status = ((status & 0x3c) >> 2) | (status & ~0xf);
562 lightrec_mtc0(state, 12, status);
565 void lightrec_cp(struct lightrec_state *state, union code op)
567 if (op.i.op == OP_CP0) {
568 pr_err("Invalid CP opcode to coprocessor #0\n");
572 (*state->ops.cop2_op)(state, op.opcode);
575 static void lightrec_cp_cb(struct lightrec_state *state)
577 lightrec_cp(state, (union code) state->c_wrapper_arg);
580 static void lightrec_syscall_cb(struct lightrec_state *state)
582 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SYSCALL);
585 static void lightrec_break_cb(struct lightrec_state *state)
587 lightrec_set_exit_flags(state, LIGHTREC_EXIT_BREAK);
590 struct block * lightrec_get_block(struct lightrec_state *state, u32 pc)
592 struct block *block = lightrec_find_block(state->block_cache, pc);
594 if (block && lightrec_block_is_outdated(state, block)) {
595 pr_debug("Block at PC 0x%08x is outdated!\n", block->pc);
597 /* Make sure the recompiler isn't processing the block we'll
599 if (ENABLE_THREADED_COMPILER)
600 lightrec_recompiler_remove(state->rec, block);
602 lightrec_unregister_block(state->block_cache, block);
603 remove_from_code_lut(state->block_cache, block);
604 lightrec_free_block(state, block);
609 block = lightrec_precompile_block(state, pc);
611 pr_err("Unable to recompile block at PC 0x%x\n", pc);
612 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
616 lightrec_register_block(state->block_cache, block);
622 static void * get_next_block_func(struct lightrec_state *state, u32 pc)
625 bool should_recompile;
630 func = lut_read(state, lut_offset(pc));
631 if (func && func != state->get_next_block)
634 block = lightrec_get_block(state, pc);
636 if (unlikely(!block))
639 if (OPT_REPLACE_MEMSET && (block->flags & BLOCK_IS_MEMSET)) {
640 func = state->memset_func;
644 should_recompile = block->flags & BLOCK_SHOULD_RECOMPILE &&
645 !(block->flags & BLOCK_IS_DEAD);
647 if (unlikely(should_recompile)) {
648 pr_debug("Block at PC 0x%08x should recompile\n", pc);
650 lightrec_unregister(MEM_FOR_CODE, block->code_size);
652 if (ENABLE_THREADED_COMPILER) {
653 lightrec_recompiler_add(state->rec, block);
655 err = lightrec_compile_block(state->cstate, block);
657 state->exit_flags = LIGHTREC_EXIT_NOMEM;
663 if (ENABLE_THREADED_COMPILER && likely(!should_recompile))
664 func = lightrec_recompiler_run_first_pass(state, block, &pc);
666 func = block->function;
671 if (unlikely(block->flags & BLOCK_NEVER_COMPILE)) {
672 pc = lightrec_emulate_block(state, block, pc);
674 } else if (!ENABLE_THREADED_COMPILER) {
675 /* Block wasn't compiled yet - run the interpreter */
676 if (block->flags & BLOCK_FULLY_TAGGED)
677 pr_debug("Block fully tagged, skipping first pass\n");
678 else if (ENABLE_FIRST_PASS && likely(!should_recompile))
679 pc = lightrec_emulate_block(state, block, pc);
681 /* Then compile it using the profiled data */
682 err = lightrec_compile_block(state->cstate, block);
684 state->exit_flags = LIGHTREC_EXIT_NOMEM;
688 lightrec_recompiler_add(state->rec, block);
691 if (state->exit_flags != LIGHTREC_EXIT_NORMAL ||
692 state->current_cycle >= state->target_cycle)
700 static s32 c_function_wrapper(struct lightrec_state *state, s32 cycles_delta,
701 void (*f)(struct lightrec_state *))
703 state->current_cycle = state->target_cycle - cycles_delta;
707 return state->target_cycle - state->current_cycle;
710 static void * lightrec_alloc_code(struct lightrec_state *state, size_t size)
714 if (ENABLE_THREADED_COMPILER)
715 lightrec_code_alloc_lock(state);
717 code = tlsf_malloc(state->tlsf, size);
719 if (ENABLE_THREADED_COMPILER)
720 lightrec_code_alloc_unlock(state);
725 static void lightrec_realloc_code(struct lightrec_state *state,
726 void *ptr, size_t size)
728 /* NOTE: 'size' MUST be smaller than the size specified during
731 if (ENABLE_THREADED_COMPILER)
732 lightrec_code_alloc_lock(state);
734 tlsf_realloc(state->tlsf, ptr, size);
736 if (ENABLE_THREADED_COMPILER)
737 lightrec_code_alloc_unlock(state);
740 static void lightrec_free_code(struct lightrec_state *state, void *ptr)
742 if (ENABLE_THREADED_COMPILER)
743 lightrec_code_alloc_lock(state);
745 tlsf_free(state->tlsf, ptr);
747 if (ENABLE_THREADED_COMPILER)
748 lightrec_code_alloc_unlock(state);
751 static void * lightrec_emit_code(struct lightrec_state *state,
752 const struct block *block,
753 jit_state_t *_jit, unsigned int *size)
755 bool has_code_buffer = ENABLE_CODE_BUFFER && state->tlsf;
756 jit_word_t code_size, new_code_size;
761 if (!ENABLE_DISASSEMBLER)
762 jit_set_data(NULL, 0, JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
764 if (has_code_buffer) {
765 jit_get_code(&code_size);
766 code = lightrec_alloc_code(state, (size_t) code_size);
769 if (ENABLE_THREADED_COMPILER) {
770 /* If we're using the threaded compiler, return
771 * an allocation error here. The threaded
772 * compiler will then empty its job queue and
773 * request a code flush using the reaper. */
777 /* Remove outdated blocks, and try again */
778 lightrec_remove_outdated_blocks(state->block_cache, block);
780 pr_debug("Re-try to alloc %zu bytes...\n", code_size);
782 code = lightrec_alloc_code(state, code_size);
784 pr_err("Could not alloc even after removing old blocks!\n");
789 jit_set_code(code, code_size);
794 jit_get_code(&new_code_size);
795 lightrec_register(MEM_FOR_CODE, new_code_size);
797 if (has_code_buffer) {
798 lightrec_realloc_code(state, code, (size_t) new_code_size);
800 pr_debug("Creating code block at address 0x%" PRIxPTR ", "
801 "code size: %" PRIuPTR " new: %" PRIuPTR "\n",
802 (uintptr_t) code, code_size, new_code_size);
805 *size = (unsigned int) new_code_size;
810 static struct block * generate_wrapper(struct lightrec_state *state)
816 jit_node_t *to_tramp, *to_fn_epilog;
817 jit_node_t *addr[C_WRAPPERS_COUNT - 1];
819 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
823 _jit = jit_new_state();
827 jit_name("RW wrapper");
828 jit_note(__FILE__, __LINE__);
830 /* Wrapper entry point */
834 /* Add entry points; separate them by opcodes that increment
835 * LIGHTREC_REG_STATE (since we cannot touch other registers).
836 * The difference will then tell us which C function to call. */
837 for (i = C_WRAPPERS_COUNT - 1; i > 0; i--) {
838 jit_addi(LIGHTREC_REG_STATE, LIGHTREC_REG_STATE, __WORDSIZE / 8);
839 addr[i - 1] = jit_indirect();
845 stack_ptr = jit_allocai(sizeof(uintptr_t) * NUM_TEMPS);
847 /* Save all temporaries on stack */
848 for (i = 0; i < NUM_TEMPS; i++)
849 jit_stxi(stack_ptr + i * sizeof(uintptr_t), JIT_FP, JIT_R(i));
851 /* Jump to the trampoline */
852 to_tramp = jit_jmpi();
854 /* The trampoline will jump back here */
855 to_fn_epilog = jit_label();
857 /* Restore temporaries from stack */
858 for (i = 0; i < NUM_TEMPS; i++)
859 jit_ldxi(JIT_R(i), JIT_FP, stack_ptr + i * sizeof(uintptr_t));
864 /* Trampoline entry point.
865 * The sole purpose of the trampoline is to cheese Lightning not to
866 * save/restore the callee-saved register LIGHTREC_REG_CYCLE, since we
867 * do want to return to the caller with this register modified. */
872 /* Retrieve the wrapper function */
873 jit_ldxi(JIT_R0, LIGHTREC_REG_STATE,
874 offsetof(struct lightrec_state, c_wrappers));
876 /* Restore LIGHTREC_REG_STATE to its correct value */
877 jit_movi(LIGHTREC_REG_STATE, (uintptr_t) state);
880 jit_pushargr(LIGHTREC_REG_STATE);
881 jit_pushargr(LIGHTREC_REG_CYCLE);
882 jit_pushargr(JIT_R0);
883 jit_finishi(c_function_wrapper);
884 jit_retval_i(LIGHTREC_REG_CYCLE);
886 jit_patch_at(jit_jmpi(), to_fn_epilog);
890 block->opcode_list = NULL;
894 block->function = lightrec_emit_code(state, block, _jit,
896 if (!block->function)
899 state->wrappers_eps[C_WRAPPERS_COUNT - 1] = block->function;
901 for (i = 0; i < C_WRAPPERS_COUNT - 1; i++)
902 state->wrappers_eps[i] = jit_address(addr[i]);
904 if (ENABLE_DISASSEMBLER) {
905 pr_debug("Wrapper block:\n");
913 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
915 pr_err("Unable to compile wrapper: Out of memory\n");
919 static u32 lightrec_memset(struct lightrec_state *state)
921 u32 kunseg_pc = kunseg(state->regs.gpr[4]);
923 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg_pc);
924 u32 length = state->regs.gpr[5] * 4;
927 pr_err("Unable to find memory map for memset target address "
928 "0x%x\n", kunseg_pc);
932 pr_debug("Calling host memset, PC 0x%x (host address 0x%" PRIxPTR ") for %u bytes\n",
933 kunseg_pc, (uintptr_t)host, length);
934 memset(host, 0, length);
936 if (!state->invalidate_from_dma_only)
937 lightrec_invalidate_map(state, map, kunseg_pc, length);
939 /* Rough estimation of the number of cycles consumed */
940 return 8 + 5 * (length + 3 / 4);
943 static struct block * generate_dispatcher(struct lightrec_state *state)
947 jit_node_t *to_end, *loop, *addr, *addr2, *addr3;
951 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
955 _jit = jit_new_state();
959 jit_name("dispatcher");
960 jit_note(__FILE__, __LINE__);
965 jit_getarg(JIT_R0, jit_arg());
966 jit_getarg_i(LIGHTREC_REG_CYCLE, jit_arg());
968 /* Force all callee-saved registers to be pushed on the stack */
969 for (i = 0; i < NUM_REGS; i++)
970 jit_movr(JIT_V(i), JIT_V(i));
972 /* Pass lightrec_state structure to blocks, using the last callee-saved
973 * register that Lightning provides */
974 jit_movi(LIGHTREC_REG_STATE, (intptr_t) state);
978 /* Call the block's code */
981 if (OPT_REPLACE_MEMSET) {
982 /* Blocks will jump here when they need to call
983 * lightrec_memset() */
984 addr3 = jit_indirect();
987 jit_pushargr(LIGHTREC_REG_STATE);
988 jit_finishi(lightrec_memset);
990 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE,
991 offsetof(struct lightrec_state, regs.gpr[31]));
994 jit_subr(LIGHTREC_REG_CYCLE, LIGHTREC_REG_CYCLE, JIT_R0);
997 /* The block will jump here, with the number of cycles remaining in
998 * LIGHTREC_REG_CYCLE */
999 addr2 = jit_indirect();
1001 /* Store back the next_pc to the lightrec_state structure */
1002 offset = offsetof(struct lightrec_state, next_pc);
1003 jit_stxi_i(offset, LIGHTREC_REG_STATE, JIT_V0);
1005 /* Jump to end if state->target_cycle < state->current_cycle */
1006 to_end = jit_blei(LIGHTREC_REG_CYCLE, 0);
1008 /* Convert next PC to KUNSEG and avoid mirrors */
1009 jit_andi(JIT_R0, JIT_V0, 0x10000000 | (RAM_SIZE - 1));
1010 jit_rshi_u(JIT_R1, JIT_R0, 28);
1011 jit_andi(JIT_R2, JIT_V0, BIOS_SIZE - 1);
1012 jit_addi(JIT_R2, JIT_R2, RAM_SIZE);
1013 jit_movnr(JIT_R0, JIT_R2, JIT_R1);
1015 /* If possible, use the code LUT */
1016 if (!lut_is_32bit(state))
1017 jit_lshi(JIT_R0, JIT_R0, 1);
1018 jit_addr(JIT_R0, JIT_R0, LIGHTREC_REG_STATE);
1020 offset = offsetof(struct lightrec_state, code_lut);
1021 if (lut_is_32bit(state))
1022 jit_ldxi_ui(JIT_R0, JIT_R0, offset);
1024 jit_ldxi(JIT_R0, JIT_R0, offset);
1026 /* If we get non-NULL, loop */
1027 jit_patch_at(jit_bnei(JIT_R0, 0), loop);
1029 /* Slow path: call C function get_next_block_func() */
1031 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1032 /* We may call the interpreter - update state->current_cycle */
1033 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
1034 offsetof(struct lightrec_state, target_cycle));
1035 jit_subr(JIT_R1, JIT_R2, LIGHTREC_REG_CYCLE);
1036 jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
1037 LIGHTREC_REG_STATE, JIT_R1);
1040 /* The code LUT will be set to this address when the block at the target
1041 * PC has been preprocessed but not yet compiled by the threaded
1043 addr = jit_indirect();
1045 /* Get the next block */
1047 jit_pushargr(LIGHTREC_REG_STATE);
1048 jit_pushargr(JIT_V0);
1049 jit_finishi(&get_next_block_func);
1052 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1053 /* The interpreter may have updated state->current_cycle and
1054 * state->target_cycle - recalc the delta */
1055 jit_ldxi_i(JIT_R1, LIGHTREC_REG_STATE,
1056 offsetof(struct lightrec_state, current_cycle));
1057 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
1058 offsetof(struct lightrec_state, target_cycle));
1059 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, JIT_R1);
1062 /* If we get non-NULL, loop */
1063 jit_patch_at(jit_bnei(JIT_R0, 0), loop);
1065 /* When exiting, the recompiled code will jump to that address */
1066 jit_note(__FILE__, __LINE__);
1069 jit_retr(LIGHTREC_REG_CYCLE);
1073 block->opcode_list = NULL;
1077 block->function = lightrec_emit_code(state, block, _jit,
1079 if (!block->function)
1080 goto err_free_block;
1082 state->eob_wrapper_func = jit_address(addr2);
1083 if (OPT_REPLACE_MEMSET)
1084 state->memset_func = jit_address(addr3);
1085 state->get_next_block = jit_address(addr);
1087 if (ENABLE_DISASSEMBLER) {
1088 pr_debug("Dispatcher block:\n");
1097 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1099 pr_err("Unable to compile dispatcher: Out of memory\n");
1103 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc)
1107 lightrec_get_map(state, &host, kunseg(pc));
1109 const u32 *code = (u32 *)host;
1110 return (union code) LE32TOH(*code);
1113 unsigned int lightrec_cycles_of_opcode(union code code)
1118 void lightrec_free_opcode_list(struct lightrec_state *state, struct block *block)
1120 lightrec_free(state, MEM_FOR_IR,
1121 sizeof(*block->opcode_list) * block->nb_ops,
1122 block->opcode_list);
1125 static unsigned int lightrec_get_mips_block_len(const u32 *src)
1130 for (i = 1; ; i++) {
1131 c.opcode = LE32TOH(*src++);
1136 if (is_unconditional_jump(c))
1141 static struct opcode * lightrec_disassemble(struct lightrec_state *state,
1142 const u32 *src, unsigned int *len)
1144 struct opcode *list;
1145 unsigned int i, length;
1147 length = lightrec_get_mips_block_len(src);
1149 list = lightrec_malloc(state, MEM_FOR_IR, sizeof(*list) * length);
1151 pr_err("Unable to allocate memory\n");
1155 for (i = 0; i < length; i++) {
1156 list[i].opcode = LE32TOH(src[i]);
1160 *len = length * sizeof(u32);
1165 static struct block * lightrec_precompile_block(struct lightrec_state *state,
1168 struct opcode *list;
1169 struct block *block;
1171 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg(pc));
1172 const u32 *code = (u32 *) host;
1173 unsigned int length;
1179 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
1181 pr_err("Unable to recompile block: Out of memory\n");
1185 list = lightrec_disassemble(state, code, &length);
1187 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1193 block->function = NULL;
1194 block->opcode_list = list;
1198 block->code_size = 0;
1199 block->precompile_date = state->current_cycle;
1200 #if ENABLE_THREADED_COMPILER
1201 block->op_list_freed = (atomic_flag)ATOMIC_FLAG_INIT;
1203 block->nb_ops = length / sizeof(u32);
1205 lightrec_optimize(state, block);
1207 length = block->nb_ops * sizeof(u32);
1209 lightrec_register(MEM_FOR_MIPS_CODE, length);
1211 if (ENABLE_DISASSEMBLER) {
1212 pr_debug("Disassembled block at PC: 0x%08x\n", block->pc);
1213 lightrec_print_disassembly(block, code);
1216 pr_debug("Block size: %hu opcodes\n", block->nb_ops);
1218 /* If the first opcode is an 'impossible' branch, never compile the
1220 if (should_emulate(block->opcode_list))
1221 block->flags |= BLOCK_NEVER_COMPILE;
1223 fully_tagged = lightrec_block_is_fully_tagged(block);
1225 block->flags |= BLOCK_FULLY_TAGGED;
1227 if (OPT_REPLACE_MEMSET && (block->flags & BLOCK_IS_MEMSET))
1228 lut_write(state, lut_offset(pc), state->memset_func);
1230 block->hash = lightrec_calculate_block_hash(block);
1232 pr_debug("Recompile count: %u\n", state->nb_precompile++);
1237 static bool lightrec_block_is_fully_tagged(const struct block *block)
1239 const struct opcode *op;
1242 for (i = 0; i < block->nb_ops; i++) {
1243 op = &block->opcode_list[i];
1245 /* Verify that all load/stores of the opcode list
1246 * Check all loads/stores of the opcode list and mark the
1247 * block as fully compiled if they all have been tagged. */
1248 switch (op->c.i.op) {
1263 if (!LIGHTREC_FLAGS_GET_IO_MODE(op->flags))
1274 static void lightrec_reap_block(struct lightrec_state *state, void *data)
1276 struct block *block = data;
1278 pr_debug("Reap dead block at PC 0x%08x\n", block->pc);
1279 lightrec_unregister_block(state->block_cache, block);
1280 lightrec_free_block(state, block);
1283 static void lightrec_reap_jit(struct lightrec_state *state, void *data)
1285 _jit_destroy_state(data);
1288 static void lightrec_free_function(struct lightrec_state *state, void *fn)
1290 if (ENABLE_CODE_BUFFER && state->tlsf) {
1291 pr_debug("Freeing code block at 0x%" PRIxPTR "\n", (uintptr_t) fn);
1292 lightrec_free_code(state, fn);
1296 static void lightrec_reap_function(struct lightrec_state *state, void *data)
1298 lightrec_free_function(state, data);
1301 int lightrec_compile_block(struct lightrec_cstate *cstate,
1302 struct block *block)
1304 struct lightrec_state *state = cstate->state;
1305 struct lightrec_branch_target *target;
1306 bool op_list_freed = false, fully_tagged = false;
1307 struct block *block2;
1309 jit_state_t *_jit, *oldjit;
1310 jit_node_t *start_of_block;
1311 bool skip_next = false;
1312 void *old_fn, *new_fn;
1316 fully_tagged = lightrec_block_is_fully_tagged(block);
1318 block->flags |= BLOCK_FULLY_TAGGED;
1320 _jit = jit_new_state();
1324 oldjit = block->_jit;
1325 old_fn = block->function;
1328 lightrec_regcache_reset(cstate->reg_cache);
1330 cstate->nb_branches = 0;
1331 cstate->nb_local_branches = 0;
1332 cstate->nb_targets = 0;
1337 start_of_block = jit_label();
1339 for (i = 0; i < block->nb_ops; i++) {
1340 elm = &block->opcode_list[i];
1347 cstate->cycles += lightrec_cycles_of_opcode(elm->c);
1349 if (should_emulate(elm)) {
1350 pr_debug("Branch at offset 0x%x will be emulated\n",
1353 lightrec_emit_eob(cstate, block, i, false);
1354 skip_next = !(elm->flags & LIGHTREC_NO_DS);
1356 lightrec_rec_opcode(cstate, block, i);
1357 skip_next = has_delay_slot(elm->c) &&
1358 !(elm->flags & LIGHTREC_NO_DS);
1360 /* FIXME: GNU Lightning on Windows seems to use our
1361 * mapped registers as temporaries. Until the actual bug
1362 * is found and fixed, unconditionally mark our
1363 * registers as live here. */
1364 lightrec_regcache_mark_live(cstate->reg_cache, _jit);
1369 for (i = 0; i < cstate->nb_branches; i++)
1370 jit_patch(cstate->branches[i]);
1372 for (i = 0; i < cstate->nb_local_branches; i++) {
1373 struct lightrec_branch *branch = &cstate->local_branches[i];
1375 pr_debug("Patch local branch to offset 0x%x\n",
1376 branch->target << 2);
1378 if (branch->target == 0) {
1379 jit_patch_at(branch->branch, start_of_block);
1383 for (j = 0; j < cstate->nb_targets; j++) {
1384 if (cstate->targets[j].offset == branch->target) {
1385 jit_patch_at(branch->branch,
1386 cstate->targets[j].label);
1391 if (j == cstate->nb_targets)
1392 pr_err("Unable to find branch target\n");
1395 jit_ldxi(JIT_R0, LIGHTREC_REG_STATE,
1396 offsetof(struct lightrec_state, eob_wrapper_func));
1403 new_fn = lightrec_emit_code(state, block, _jit, &block->code_size);
1405 if (!ENABLE_THREADED_COMPILER)
1406 pr_err("Unable to compile block!\n");
1407 block->_jit = oldjit;
1408 _jit_destroy_state(_jit);
1412 block->function = new_fn;
1413 block->flags &= ~BLOCK_SHOULD_RECOMPILE;
1415 /* Add compiled function to the LUT */
1416 lut_write(state, lut_offset(block->pc), block->function);
1418 if (ENABLE_THREADED_COMPILER) {
1419 /* Since we might try to reap the same block multiple times,
1420 * we need the reaper to wait until everything has been
1421 * submitted, so that the duplicate entries can be dropped. */
1422 lightrec_reaper_pause(state->reaper);
1425 /* Detect old blocks that have been covered by the new one */
1426 for (i = 0; i < cstate->nb_targets; i++) {
1427 target = &cstate->targets[i];
1429 if (!target->offset)
1432 offset = block->pc + target->offset * sizeof(u32);
1433 block2 = lightrec_find_block(state->block_cache, offset);
1435 /* No need to check if block2 is compilable - it must
1436 * be, otherwise block wouldn't be compilable either */
1438 /* Set the "block dead" flag to prevent the dynarec from
1439 * recompiling this block */
1440 block2->flags |= BLOCK_IS_DEAD;
1442 /* If block2 was pending for compilation, cancel it.
1443 * If it's being compiled right now, wait until it
1445 if (ENABLE_THREADED_COMPILER)
1446 lightrec_recompiler_remove(state->rec, block2);
1449 /* We know from now on that block2 (if present) isn't going to
1450 * be compiled. We can override the LUT entry with our new
1451 * block's entry point. */
1452 offset = lut_offset(block->pc) + target->offset;
1453 lut_write(state, offset, jit_address(target->label));
1456 pr_debug("Reap block 0x%08x as it's covered by block "
1457 "0x%08x\n", block2->pc, block->pc);
1459 /* Finally, reap the block. */
1460 if (ENABLE_THREADED_COMPILER) {
1461 lightrec_reaper_add(state->reaper,
1462 lightrec_reap_block,
1465 lightrec_unregister_block(state->block_cache, block2);
1466 lightrec_free_block(state, block2);
1471 if (ENABLE_THREADED_COMPILER)
1472 lightrec_reaper_continue(state->reaper);
1474 if (ENABLE_DISASSEMBLER) {
1475 pr_debug("Compiling block at PC: 0x%08x\n", block->pc);
1481 #if ENABLE_THREADED_COMPILER
1483 op_list_freed = atomic_flag_test_and_set(&block->op_list_freed);
1485 if (fully_tagged && !op_list_freed) {
1486 pr_debug("Block PC 0x%08x is fully tagged"
1487 " - free opcode list\n", block->pc);
1488 lightrec_free_opcode_list(state, block);
1489 block->opcode_list = NULL;
1493 pr_debug("Block 0x%08x recompiled, reaping old jit context.\n",
1496 if (ENABLE_THREADED_COMPILER) {
1497 lightrec_reaper_add(state->reaper,
1498 lightrec_reap_jit, oldjit);
1499 lightrec_reaper_add(state->reaper,
1500 lightrec_reap_function, old_fn);
1502 _jit_destroy_state(oldjit);
1503 lightrec_free_function(state, old_fn);
1510 static void lightrec_print_info(struct lightrec_state *state)
1512 if ((state->current_cycle & ~0xfffffff) != state->old_cycle_counter) {
1513 pr_info("Lightrec RAM usage: IR %u KiB, CODE %u KiB, "
1514 "MIPS %u KiB, TOTAL %u KiB, avg. IPI %f\n",
1515 lightrec_get_mem_usage(MEM_FOR_IR) / 1024,
1516 lightrec_get_mem_usage(MEM_FOR_CODE) / 1024,
1517 lightrec_get_mem_usage(MEM_FOR_MIPS_CODE) / 1024,
1518 lightrec_get_total_mem_usage() / 1024,
1519 lightrec_get_average_ipi());
1520 state->old_cycle_counter = state->current_cycle & ~0xfffffff;
1524 u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
1526 s32 (*func)(void *, s32) = (void *)state->dispatcher->function;
1530 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1532 /* Handle the cycle counter overflowing */
1533 if (unlikely(target_cycle < state->current_cycle))
1534 target_cycle = UINT_MAX;
1536 state->target_cycle = target_cycle;
1537 state->next_pc = pc;
1539 block_trace = get_next_block_func(state, pc);
1541 cycles_delta = state->target_cycle - state->current_cycle;
1543 cycles_delta = (*func)(block_trace, cycles_delta);
1545 state->current_cycle = state->target_cycle - cycles_delta;
1548 if (ENABLE_THREADED_COMPILER)
1549 lightrec_reaper_reap(state->reaper);
1551 if (LOG_LEVEL >= INFO_L)
1552 lightrec_print_info(state);
1554 return state->next_pc;
1557 u32 lightrec_execute_one(struct lightrec_state *state, u32 pc)
1559 return lightrec_execute(state, pc, state->current_cycle);
1562 u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc)
1564 struct block *block = lightrec_get_block(state, pc);
1568 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1570 pc = lightrec_emulate_block(state, block, pc);
1572 if (LOG_LEVEL >= INFO_L)
1573 lightrec_print_info(state);
1578 void lightrec_free_block(struct lightrec_state *state, struct block *block)
1580 lightrec_unregister(MEM_FOR_MIPS_CODE, block->nb_ops * sizeof(u32));
1581 if (block->opcode_list)
1582 lightrec_free_opcode_list(state, block);
1584 _jit_destroy_state(block->_jit);
1585 if (block->function) {
1586 lightrec_free_function(state, block->function);
1587 lightrec_unregister(MEM_FOR_CODE, block->code_size);
1589 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1592 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state)
1594 struct lightrec_cstate *cstate;
1596 cstate = lightrec_malloc(state, MEM_FOR_LIGHTREC, sizeof(*cstate));
1600 cstate->reg_cache = lightrec_regcache_init(state);
1601 if (!cstate->reg_cache) {
1602 lightrec_free(state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
1606 cstate->state = state;
1611 void lightrec_free_cstate(struct lightrec_cstate *cstate)
1613 lightrec_free_regcache(cstate->reg_cache);
1614 lightrec_free(cstate->state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
1617 struct lightrec_state * lightrec_init(char *argv0,
1618 const struct lightrec_mem_map *map,
1620 const struct lightrec_ops *ops)
1622 const struct lightrec_mem_map *codebuf_map = &map[PSX_MAP_CODE_BUFFER];
1623 struct lightrec_state *state;
1626 bool with_32bit_lut = false;
1629 /* Sanity-check ops */
1630 if (!ops || !ops->cop2_op || !ops->enable_ram) {
1631 pr_err("Missing callbacks in lightrec_ops structure\n");
1635 if (ENABLE_CODE_BUFFER && nb > PSX_MAP_CODE_BUFFER
1636 && codebuf_map->address) {
1637 tlsf = tlsf_create_with_pool(codebuf_map->address,
1638 codebuf_map->length);
1640 pr_err("Unable to initialize code buffer\n");
1644 if (__WORDSIZE == 64) {
1645 addr = (uintptr_t) codebuf_map->address + codebuf_map->length - 1;
1646 with_32bit_lut = addr == (u32) addr;
1651 lut_size = CODE_LUT_SIZE * 4;
1653 lut_size = CODE_LUT_SIZE * sizeof(void *);
1657 state = calloc(1, sizeof(*state) + lut_size);
1659 goto err_finish_jit;
1661 lightrec_register(MEM_FOR_LIGHTREC, sizeof(*state) + lut_size);
1664 state->with_32bit_lut = with_32bit_lut;
1666 state->block_cache = lightrec_blockcache_init(state);
1667 if (!state->block_cache)
1668 goto err_free_state;
1670 if (ENABLE_THREADED_COMPILER) {
1671 state->rec = lightrec_recompiler_init(state);
1673 goto err_free_block_cache;
1675 state->reaper = lightrec_reaper_init(state);
1677 goto err_free_recompiler;
1679 state->cstate = lightrec_create_cstate(state);
1681 goto err_free_block_cache;
1684 state->nb_maps = nb;
1687 memcpy(&state->ops, ops, sizeof(*ops));
1689 state->dispatcher = generate_dispatcher(state);
1690 if (!state->dispatcher)
1691 goto err_free_reaper;
1693 state->c_wrapper_block = generate_wrapper(state);
1694 if (!state->c_wrapper_block)
1695 goto err_free_dispatcher;
1697 state->c_wrappers[C_WRAPPER_RW] = lightrec_rw_cb;
1698 state->c_wrappers[C_WRAPPER_RW_GENERIC] = lightrec_rw_generic_cb;
1699 state->c_wrappers[C_WRAPPER_MTC] = lightrec_mtc_cb;
1700 state->c_wrappers[C_WRAPPER_CP] = lightrec_cp_cb;
1701 state->c_wrappers[C_WRAPPER_SYSCALL] = lightrec_syscall_cb;
1702 state->c_wrappers[C_WRAPPER_BREAK] = lightrec_break_cb;
1704 map = &state->maps[PSX_MAP_BIOS];
1705 state->offset_bios = (uintptr_t)map->address - map->pc;
1707 map = &state->maps[PSX_MAP_SCRATCH_PAD];
1708 state->offset_scratch = (uintptr_t)map->address - map->pc;
1710 map = &state->maps[PSX_MAP_KERNEL_USER_RAM];
1711 state->offset_ram = (uintptr_t)map->address - map->pc;
1713 if (state->maps[PSX_MAP_MIRROR1].address == map->address + 0x200000 &&
1714 state->maps[PSX_MAP_MIRROR2].address == map->address + 0x400000 &&
1715 state->maps[PSX_MAP_MIRROR3].address == map->address + 0x600000)
1716 state->mirrors_mapped = true;
1718 if (state->offset_bios == 0 &&
1719 state->offset_scratch == 0 &&
1720 state->offset_ram == 0 &&
1721 state->mirrors_mapped) {
1722 pr_info("Memory map is perfect. Emitted code will be best.\n");
1724 pr_info("Memory map is sub-par. Emitted code will be slow.\n");
1727 if (state->with_32bit_lut)
1728 pr_info("Using 32-bit LUT\n");
1732 err_free_dispatcher:
1733 lightrec_free_block(state, state->dispatcher);
1735 if (ENABLE_THREADED_COMPILER)
1736 lightrec_reaper_destroy(state->reaper);
1737 err_free_recompiler:
1738 if (ENABLE_THREADED_COMPILER)
1739 lightrec_free_recompiler(state->rec);
1741 lightrec_free_cstate(state->cstate);
1742 err_free_block_cache:
1743 lightrec_free_block_cache(state->block_cache);
1745 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
1746 lut_elm_size(state) * CODE_LUT_SIZE);
1750 if (ENABLE_CODE_BUFFER && tlsf)
1755 void lightrec_destroy(struct lightrec_state *state)
1757 /* Force a print info on destroy*/
1758 state->current_cycle = ~state->current_cycle;
1759 lightrec_print_info(state);
1761 if (ENABLE_THREADED_COMPILER) {
1762 lightrec_free_recompiler(state->rec);
1763 lightrec_reaper_destroy(state->reaper);
1765 lightrec_free_cstate(state->cstate);
1768 lightrec_free_block_cache(state->block_cache);
1769 lightrec_free_block(state, state->dispatcher);
1770 lightrec_free_block(state, state->c_wrapper_block);
1772 if (ENABLE_CODE_BUFFER && state->tlsf)
1773 tlsf_destroy(state->tlsf);
1775 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
1776 lut_elm_size(state) * CODE_LUT_SIZE);
1780 void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len)
1782 u32 kaddr = kunseg(addr & ~0x3);
1783 enum psx_map idx = lightrec_get_map_idx(state, kaddr);
1786 case PSX_MAP_MIRROR1:
1787 case PSX_MAP_MIRROR2:
1788 case PSX_MAP_MIRROR3:
1789 /* Handle mirrors */
1790 kaddr &= RAM_SIZE - 1;
1792 case PSX_MAP_KERNEL_USER_RAM:
1798 memset(lut_address(state, lut_offset(kaddr)), 0,
1799 ((len + 3) / 4) * lut_elm_size(state));
1802 void lightrec_invalidate_all(struct lightrec_state *state)
1804 memset(state->code_lut, 0, lut_elm_size(state) * CODE_LUT_SIZE);
1807 void lightrec_set_invalidate_mode(struct lightrec_state *state, bool dma_only)
1809 if (state->invalidate_from_dma_only != dma_only)
1810 lightrec_invalidate_all(state);
1812 state->invalidate_from_dma_only = dma_only;
1815 void lightrec_set_exit_flags(struct lightrec_state *state, u32 flags)
1817 if (flags != LIGHTREC_EXIT_NORMAL) {
1818 state->exit_flags |= flags;
1819 state->target_cycle = state->current_cycle;
1823 u32 lightrec_exit_flags(struct lightrec_state *state)
1825 return state->exit_flags;
1828 u32 lightrec_current_cycle_count(const struct lightrec_state *state)
1830 return state->current_cycle;
1833 void lightrec_reset_cycle_count(struct lightrec_state *state, u32 cycles)
1835 state->current_cycle = cycles;
1837 if (state->target_cycle < cycles)
1838 state->target_cycle = cycles;
1841 void lightrec_set_target_cycle_count(struct lightrec_state *state, u32 cycles)
1843 if (state->exit_flags == LIGHTREC_EXIT_NORMAL) {
1844 if (cycles < state->current_cycle)
1845 cycles = state->current_cycle;
1847 state->target_cycle = cycles;
1851 struct lightrec_registers * lightrec_get_registers(struct lightrec_state *state)
1853 return &state->regs;