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 static struct block * lightrec_precompile_block(struct lightrec_state *state,
33 static bool lightrec_block_is_fully_tagged(const struct block *block);
35 static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data);
36 static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg);
38 static void lightrec_default_sb(struct lightrec_state *state, u32 opcode,
39 void *host, u32 addr, u8 data)
43 if (!state->invalidate_from_dma_only)
44 lightrec_invalidate(state, addr, 1);
47 static void lightrec_default_sh(struct lightrec_state *state, u32 opcode,
48 void *host, u32 addr, u16 data)
50 *(u16 *)host = HTOLE16(data);
52 if (!state->invalidate_from_dma_only)
53 lightrec_invalidate(state, addr, 2);
56 static void lightrec_default_sw(struct lightrec_state *state, u32 opcode,
57 void *host, u32 addr, u32 data)
59 *(u32 *)host = HTOLE32(data);
61 if (!state->invalidate_from_dma_only)
62 lightrec_invalidate(state, addr, 4);
65 static u8 lightrec_default_lb(struct lightrec_state *state,
66 u32 opcode, void *host, u32 addr)
71 static u16 lightrec_default_lh(struct lightrec_state *state,
72 u32 opcode, void *host, u32 addr)
74 return LE16TOH(*(u16 *)host);
77 static u32 lightrec_default_lw(struct lightrec_state *state,
78 u32 opcode, void *host, u32 addr)
80 return LE32TOH(*(u32 *)host);
83 static const struct lightrec_mem_map_ops lightrec_default_ops = {
84 .sb = lightrec_default_sb,
85 .sh = lightrec_default_sh,
86 .sw = lightrec_default_sw,
87 .lb = lightrec_default_lb,
88 .lh = lightrec_default_lh,
89 .lw = lightrec_default_lw,
92 static void __segfault_cb(struct lightrec_state *state, u32 addr,
93 const struct block *block)
95 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
96 pr_err("Segmentation fault in recompiled code: invalid "
97 "load/store at address 0x%08x\n", addr);
99 pr_err("Was executing block PC 0x%08x\n", block->pc);
102 static void lightrec_swl(struct lightrec_state *state,
103 const struct lightrec_mem_map_ops *ops,
104 u32 opcode, void *host, u32 addr, u32 data)
106 unsigned int shift = addr & 0x3;
107 unsigned int mask = shift < 3 ? GENMASK(31, (shift + 1) * 8) : 0;
110 /* Align to 32 bits */
112 host = (void *)((uintptr_t)host & ~3);
114 old_data = ops->lw(state, opcode, host, addr);
116 data = (data >> ((3 - shift) * 8)) | (old_data & mask);
118 ops->sw(state, opcode, host, addr, data);
121 static void lightrec_swr(struct lightrec_state *state,
122 const struct lightrec_mem_map_ops *ops,
123 u32 opcode, void *host, u32 addr, u32 data)
125 unsigned int shift = addr & 0x3;
126 unsigned int mask = (1 << (shift * 8)) - 1;
129 /* Align to 32 bits */
131 host = (void *)((uintptr_t)host & ~3);
133 old_data = ops->lw(state, opcode, host, addr);
135 data = (data << (shift * 8)) | (old_data & mask);
137 ops->sw(state, opcode, host, addr, data);
140 static void lightrec_swc2(struct lightrec_state *state, union code op,
141 const struct lightrec_mem_map_ops *ops,
142 void *host, u32 addr)
144 u32 data = lightrec_mfc2(state, op.i.rt);
146 ops->sw(state, op.opcode, host, addr, data);
149 static u32 lightrec_lwl(struct lightrec_state *state,
150 const struct lightrec_mem_map_ops *ops,
151 u32 opcode, void *host, u32 addr, u32 data)
153 unsigned int shift = addr & 0x3;
154 unsigned int mask = (1 << (24 - shift * 8)) - 1;
157 /* Align to 32 bits */
159 host = (void *)((uintptr_t)host & ~3);
161 old_data = ops->lw(state, opcode, host, addr);
163 return (data & mask) | (old_data << (24 - shift * 8));
166 static u32 lightrec_lwr(struct lightrec_state *state,
167 const struct lightrec_mem_map_ops *ops,
168 u32 opcode, void *host, u32 addr, u32 data)
170 unsigned int shift = addr & 0x3;
171 unsigned int mask = shift ? GENMASK(31, 32 - shift * 8) : 0;
174 /* Align to 32 bits */
176 host = (void *)((uintptr_t)host & ~3);
178 old_data = ops->lw(state, opcode, host, addr);
180 return (data & mask) | (old_data >> (shift * 8));
183 static void lightrec_lwc2(struct lightrec_state *state, union code op,
184 const struct lightrec_mem_map_ops *ops,
185 void *host, u32 addr)
187 u32 data = ops->lw(state, op.opcode, host, addr);
189 lightrec_mtc2(state, op.i.rt, data);
192 static void lightrec_invalidate_map(struct lightrec_state *state,
193 const struct lightrec_mem_map *map, u32 addr, u32 len)
195 if (map == &state->maps[PSX_MAP_KERNEL_USER_RAM]) {
196 memset(lut_address(state, lut_offset(addr)), 0,
197 ((len + 3) / 4) * lut_elm_size(state));
202 lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr)
204 const struct lightrec_mem_map *map;
207 for (i = 0; i < state->nb_maps; i++) {
208 map = &state->maps[i];
210 if (kaddr >= map->pc && kaddr < map->pc + map->length)
211 return (enum psx_map) i;
214 return PSX_MAP_UNKNOWN;
217 const struct lightrec_mem_map *
218 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr)
220 const struct lightrec_mem_map *map;
224 idx = lightrec_get_map_idx(state, kaddr);
225 if (idx == PSX_MAP_UNKNOWN)
228 map = &state->maps[idx];
229 addr = kaddr - map->pc;
231 while (map->mirror_of)
232 map = map->mirror_of;
235 *host = map->address + addr;
240 u32 lightrec_rw(struct lightrec_state *state, union code op, u32 base,
241 u32 data, u32 *flags, struct block *block, u16 offset)
243 const struct lightrec_mem_map *map;
244 const struct lightrec_mem_map_ops *ops;
245 u32 opcode = op.opcode;
246 bool was_tagged = true;
251 addr = kunseg(base + (s16) op.i.imm);
253 map = lightrec_get_map(state, &host, addr);
255 __segfault_cb(state, addr, block);
260 was_tagged = LIGHTREC_FLAGS_GET_IO_MODE(*flags);
262 if (likely(!map->ops)) {
263 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags)) {
264 /* Force parallel port accesses as HW accesses, because
265 * the direct-I/O emitters can't differenciate it. */
266 if (unlikely(map == &state->maps[PSX_MAP_PARALLEL_PORT]))
267 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
268 /* If the base register is 0x0, be extra suspicious.
269 * Some games (e.g. Sled Storm) actually do segmentation
270 * faults by using uninitialized pointers, which are
271 * later initialized to point to hardware registers. */
272 else if (op.i.rs && base == 0x0)
273 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
275 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
278 ops = &lightrec_default_ops;
280 LIGHTREC_FLAGS_GET_IO_MODE(*flags) == LIGHTREC_IO_DIRECT_HW) {
281 ops = &lightrec_default_ops;
283 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
284 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
290 old_flags = block_set_flags(block, BLOCK_SHOULD_RECOMPILE);
292 if (!(old_flags & BLOCK_SHOULD_RECOMPILE)) {
293 pr_debug("Opcode of block at PC 0x%08x has been tagged"
294 " - flag for recompilation\n", block->pc);
296 lut_write(state, lut_offset(block->pc), NULL);
302 ops->sb(state, opcode, host, addr, (u8) data);
305 ops->sh(state, opcode, host, addr, (u16) data);
308 lightrec_swl(state, ops, opcode, host, addr, data);
311 lightrec_swr(state, ops, opcode, host, addr, data);
314 ops->sw(state, opcode, host, addr, data);
317 lightrec_swc2(state, op, ops, host, addr);
320 return (s32) (s8) ops->lb(state, opcode, host, addr);
322 return ops->lb(state, opcode, host, addr);
324 return (s32) (s16) ops->lh(state, opcode, host, addr);
326 return ops->lh(state, opcode, host, addr);
328 lightrec_lwc2(state, op, ops, host, addr);
331 return lightrec_lwl(state, ops, opcode, host, addr, data);
333 return lightrec_lwr(state, ops, opcode, host, addr, data);
336 return ops->lw(state, opcode, host, addr);
340 static void lightrec_rw_helper(struct lightrec_state *state,
341 union code op, u32 *flags,
342 struct block *block, u16 offset)
344 u32 ret = lightrec_rw(state, op, state->regs.gpr[op.i.rs],
345 state->regs.gpr[op.i.rt], flags, block, offset);
355 if (OPT_HANDLE_LOAD_DELAYS && unlikely(!state->in_delay_slot_n)) {
356 state->temp_reg = ret;
357 state->in_delay_slot_n = 0xff;
358 } else if (op.i.rt) {
359 state->regs.gpr[op.i.rt] = ret;
367 static void lightrec_rw_cb(struct lightrec_state *state, u32 arg)
369 lightrec_rw_helper(state, (union code) arg, NULL, NULL, 0);
372 static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
376 u16 offset = (u16)arg;
378 block = lightrec_find_block_from_lut(state->block_cache,
379 arg >> 16, state->next_pc);
380 if (unlikely(!block)) {
381 pr_err("rw_generic: No block found in LUT for PC 0x%x offset 0x%x\n",
382 state->next_pc, offset);
383 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
387 op = &block->opcode_list[offset];
388 lightrec_rw_helper(state, op->c, &op->flags, block, offset);
391 static u32 clamp_s32(s32 val, s32 min, s32 max)
393 return val < min ? min : val > max ? max : val;
396 static u16 load_u16(u32 *ptr)
398 return ((struct u16x2 *) ptr)->l;
401 static void store_u16(u32 *ptr, u16 value)
403 ((struct u16x2 *) ptr)->l = value;
406 static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg)
408 s16 gteir1, gteir2, gteir3;
418 return (s32)(s16) load_u16(&state->regs.cp2d[reg]);
424 return load_u16(&state->regs.cp2d[reg]);
427 gteir1 = (s16) load_u16(&state->regs.cp2d[9]);
428 gteir2 = (s16) load_u16(&state->regs.cp2d[10]);
429 gteir3 = (s16) load_u16(&state->regs.cp2d[11]);
431 return clamp_s32(gteir1 >> 7, 0, 0x1f) << 0 |
432 clamp_s32(gteir2 >> 7, 0, 0x1f) << 5 |
433 clamp_s32(gteir3 >> 7, 0, 0x1f) << 10;
438 return state->regs.cp2d[reg];
442 u32 lightrec_mfc(struct lightrec_state *state, union code op)
446 if (op.i.op == OP_CP0)
447 return state->regs.cp0[op.r.rd];
449 if (op.i.op == OP_SWC2) {
450 val = lightrec_mfc2(state, op.i.rt);
451 } else if (op.r.rs == OP_CP2_BASIC_MFC2)
452 val = lightrec_mfc2(state, op.r.rd);
454 val = state->regs.cp2c[op.r.rd];
471 if (state->ops.cop2_notify)
472 (*state->ops.cop2_notify)(state, op.opcode, val);
477 static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
479 u32 rt = lightrec_mfc(state, op);
481 if (op.i.op == OP_SWC2)
482 state->temp_reg = rt;
484 state->regs.gpr[op.r.rt] = rt;
487 static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
489 u32 status, oldstatus, cause;
497 /* Those registers are read-only */
504 status = state->regs.cp0[12];
507 if (status & ~data & BIT(16)) {
508 state->ops.enable_ram(state, true);
509 lightrec_invalidate_all(state);
510 } else if (~status & data & BIT(16)) {
511 state->ops.enable_ram(state, false);
516 state->regs.cp0[13] &= ~0x300;
517 state->regs.cp0[13] |= data & 0x300;
519 state->regs.cp0[reg] = data;
522 if (reg == 12 || reg == 13) {
523 cause = state->regs.cp0[13];
524 status = state->regs.cp0[12];
526 /* Handle software interrupts */
527 if (!!(status & cause & 0x300) & status)
528 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
530 /* Handle hardware interrupts */
531 if (reg == 12 && !(~status & 0x401) && (~oldstatus & 0x401))
532 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
536 static u32 count_leading_bits(s32 data)
541 #if __has_builtin(__builtin_clrsb)
542 return 1 + __builtin_clrsb(data);
546 data = (data ^ (data >> 31)) << 1;
556 static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data)
560 state->regs.cp2d[12] = state->regs.cp2d[13];
561 state->regs.cp2d[13] = state->regs.cp2d[14];
562 state->regs.cp2d[14] = data;
565 state->regs.cp2d[9] = (data << 7) & 0xf80;
566 state->regs.cp2d[10] = (data << 2) & 0xf80;
567 state->regs.cp2d[11] = (data >> 3) & 0xf80;
572 state->regs.cp2d[31] = count_leading_bits((s32) data);
575 state->regs.cp2d[reg] = data;
580 static void lightrec_ctc2(struct lightrec_state *state, u8 reg, u32 data)
590 store_u16(&state->regs.cp2c[reg], data);
593 data = (data & 0x7ffff000) | !!(data & 0x7f87e000) << 31;
596 state->regs.cp2c[reg] = data;
601 void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data)
603 if (op.i.op == OP_CP0) {
604 lightrec_mtc0(state, reg, data);
606 if (op.i.op == OP_LWC2 || op.r.rs != OP_CP2_BASIC_CTC2)
607 lightrec_mtc2(state, reg, data);
609 lightrec_ctc2(state, reg, data);
611 if (state->ops.cop2_notify)
612 (*state->ops.cop2_notify)(state, op.opcode, data);
616 static void lightrec_mtc_cb(struct lightrec_state *state, u32 arg)
618 union code op = (union code) arg;
622 if (op.i.op == OP_LWC2) {
623 data = state->temp_reg;
626 data = state->regs.gpr[op.r.rt];
630 lightrec_mtc(state, op, reg, data);
633 void lightrec_rfe(struct lightrec_state *state)
637 /* Read CP0 Status register (r12) */
638 status = state->regs.cp0[12];
640 /* Switch the bits */
641 status = ((status & 0x3c) >> 2) | (status & ~0xf);
644 lightrec_mtc0(state, 12, status);
647 void lightrec_cp(struct lightrec_state *state, union code op)
649 if (op.i.op == OP_CP0) {
650 pr_err("Invalid CP opcode to coprocessor #0\n");
654 (*state->ops.cop2_op)(state, op.opcode);
657 static void lightrec_cp_cb(struct lightrec_state *state, u32 arg)
659 lightrec_cp(state, (union code) arg);
662 static struct block * lightrec_get_block(struct lightrec_state *state, u32 pc)
664 struct block *block = lightrec_find_block(state->block_cache, pc);
667 if (block && lightrec_block_is_outdated(state, block)) {
668 pr_debug("Block at PC 0x%08x is outdated!\n", block->pc);
670 old_flags = block_set_flags(block, BLOCK_IS_DEAD);
671 if (!(old_flags & BLOCK_IS_DEAD)) {
672 /* Make sure the recompiler isn't processing the block
674 if (ENABLE_THREADED_COMPILER)
675 lightrec_recompiler_remove(state->rec, block);
677 lightrec_unregister_block(state->block_cache, block);
678 remove_from_code_lut(state->block_cache, block);
679 lightrec_free_block(state, block);
686 block = lightrec_precompile_block(state, pc);
688 pr_err("Unable to recompile block at PC 0x%x\n", pc);
689 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
693 lightrec_register_block(state->block_cache, block);
699 static void * get_next_block_func(struct lightrec_state *state, u32 pc)
702 bool should_recompile;
707 func = lut_read(state, lut_offset(pc));
708 if (func && func != state->get_next_block)
711 block = lightrec_get_block(state, pc);
713 if (unlikely(!block))
716 if (OPT_REPLACE_MEMSET &&
717 block_has_flag(block, BLOCK_IS_MEMSET)) {
718 func = state->memset_func;
722 should_recompile = block_has_flag(block, BLOCK_SHOULD_RECOMPILE) &&
723 !block_has_flag(block, BLOCK_NEVER_COMPILE) &&
724 !block_has_flag(block, BLOCK_IS_DEAD);
726 if (unlikely(should_recompile)) {
727 pr_debug("Block at PC 0x%08x should recompile\n", pc);
729 if (ENABLE_THREADED_COMPILER) {
730 lightrec_recompiler_add(state->rec, block);
732 err = lightrec_compile_block(state->cstate, block);
734 state->exit_flags = LIGHTREC_EXIT_NOMEM;
740 if (ENABLE_THREADED_COMPILER && likely(!should_recompile))
741 func = lightrec_recompiler_run_first_pass(state, block, &pc);
743 func = block->function;
748 if (unlikely(block_has_flag(block, BLOCK_NEVER_COMPILE))) {
749 pc = lightrec_emulate_block(state, block, pc);
751 } else if (!ENABLE_THREADED_COMPILER) {
752 /* Block wasn't compiled yet - run the interpreter */
753 if (block_has_flag(block, BLOCK_FULLY_TAGGED))
754 pr_debug("Block fully tagged, skipping first pass\n");
755 else if (ENABLE_FIRST_PASS && likely(!should_recompile))
756 pc = lightrec_emulate_block(state, block, pc);
758 /* Then compile it using the profiled data */
759 err = lightrec_compile_block(state->cstate, block);
761 state->exit_flags = LIGHTREC_EXIT_NOMEM;
764 } else if (unlikely(block_has_flag(block, BLOCK_IS_DEAD))) {
766 * If the block is dead but has never been compiled,
767 * then its function pointer is NULL and we cannot
768 * execute the block. In that case, reap all the dead
769 * blocks now, and in the next loop we will create a
772 lightrec_reaper_reap(state->reaper);
774 lightrec_recompiler_add(state->rec, block);
776 } while (state->exit_flags == LIGHTREC_EXIT_NORMAL
777 && state->current_cycle < state->target_cycle);
783 static void * lightrec_alloc_code(struct lightrec_state *state, size_t size)
787 if (ENABLE_THREADED_COMPILER)
788 lightrec_code_alloc_lock(state);
790 code = tlsf_malloc(state->tlsf, size);
792 if (ENABLE_THREADED_COMPILER)
793 lightrec_code_alloc_unlock(state);
798 static void lightrec_realloc_code(struct lightrec_state *state,
799 void *ptr, size_t size)
801 /* NOTE: 'size' MUST be smaller than the size specified during
804 if (ENABLE_THREADED_COMPILER)
805 lightrec_code_alloc_lock(state);
807 tlsf_realloc(state->tlsf, ptr, size);
809 if (ENABLE_THREADED_COMPILER)
810 lightrec_code_alloc_unlock(state);
813 static void lightrec_free_code(struct lightrec_state *state, void *ptr)
815 if (ENABLE_THREADED_COMPILER)
816 lightrec_code_alloc_lock(state);
818 tlsf_free(state->tlsf, ptr);
820 if (ENABLE_THREADED_COMPILER)
821 lightrec_code_alloc_unlock(state);
824 static char lightning_code_data[0x80000];
826 static void * lightrec_emit_code(struct lightrec_state *state,
827 const struct block *block,
828 jit_state_t *_jit, unsigned int *size)
830 bool has_code_buffer = ENABLE_CODE_BUFFER && state->tlsf;
831 jit_word_t code_size, new_code_size;
836 if (ENABLE_DISASSEMBLER)
837 jit_set_data(lightning_code_data, sizeof(lightning_code_data), 0);
839 jit_set_data(NULL, 0, JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
841 if (has_code_buffer) {
842 jit_get_code(&code_size);
843 code = lightrec_alloc_code(state, (size_t) code_size);
846 if (ENABLE_THREADED_COMPILER) {
847 /* If we're using the threaded compiler, return
848 * an allocation error here. The threaded
849 * compiler will then empty its job queue and
850 * request a code flush using the reaper. */
854 /* Remove outdated blocks, and try again */
855 lightrec_remove_outdated_blocks(state->block_cache, block);
857 pr_debug("Re-try to alloc %zu bytes...\n", code_size);
859 code = lightrec_alloc_code(state, code_size);
861 pr_err("Could not alloc even after removing old blocks!\n");
866 jit_set_code(code, code_size);
871 jit_get_code(&new_code_size);
872 lightrec_register(MEM_FOR_CODE, new_code_size);
874 if (has_code_buffer) {
875 lightrec_realloc_code(state, code, (size_t) new_code_size);
877 pr_debug("Creating code block at address 0x%" PRIxPTR ", "
878 "code size: %" PRIuPTR " new: %" PRIuPTR "\n",
879 (uintptr_t) code, code_size, new_code_size);
882 *size = (unsigned int) new_code_size;
884 if (state->ops.code_inv)
885 state->ops.code_inv(code, new_code_size);
890 static struct block * generate_wrapper(struct lightrec_state *state)
895 jit_node_t *addr[C_WRAPPERS_COUNT - 1];
896 jit_node_t *to_end[C_WRAPPERS_COUNT - 1];
900 /* On SH, GBR-relative loads target the r0 register.
901 * Use it as the temporary register to factorize the move to
903 if (LIGHTREC_REG_STATE == _GBR)
907 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
911 _jit = jit_new_state();
915 jit_name("RW wrapper");
916 jit_note(__FILE__, __LINE__);
918 /* Wrapper entry point */
922 /* Add entry points */
923 for (i = C_WRAPPERS_COUNT - 1; i > 0; i--) {
924 jit_ldxi(tmp, LIGHTREC_REG_STATE,
925 offsetof(struct lightrec_state, c_wrappers[i]));
926 to_end[i - 1] = jit_b();
927 addr[i - 1] = jit_indirect();
930 jit_ldxi(tmp, LIGHTREC_REG_STATE,
931 offsetof(struct lightrec_state, c_wrappers[0]));
933 for (i = 0; i < C_WRAPPERS_COUNT - 1; i++)
934 jit_patch(to_end[i]);
935 jit_movr(JIT_R1, tmp);
940 /* Save all temporaries on stack */
941 for (i = 0; i < NUM_TEMPS; i++) {
942 if (i + FIRST_TEMP != 1) {
943 jit_stxi(offsetof(struct lightrec_state, wrapper_regs[i]),
944 LIGHTREC_REG_STATE, JIT_R(i + FIRST_TEMP));
948 jit_getarg(JIT_R2, jit_arg());
951 jit_pushargr(LIGHTREC_REG_STATE);
952 jit_pushargr(JIT_R2);
954 jit_ldxi_ui(JIT_R2, LIGHTREC_REG_STATE,
955 offsetof(struct lightrec_state, target_cycle));
957 /* state->current_cycle = state->target_cycle - delta; */
958 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, LIGHTREC_REG_CYCLE);
959 jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
960 LIGHTREC_REG_STATE, LIGHTREC_REG_CYCLE);
962 /* Call the wrapper function */
965 /* delta = state->target_cycle - state->current_cycle */;
966 jit_ldxi_ui(LIGHTREC_REG_CYCLE, LIGHTREC_REG_STATE,
967 offsetof(struct lightrec_state, current_cycle));
968 jit_ldxi_ui(JIT_R1, LIGHTREC_REG_STATE,
969 offsetof(struct lightrec_state, target_cycle));
970 jit_subr(LIGHTREC_REG_CYCLE, JIT_R1, LIGHTREC_REG_CYCLE);
972 /* Restore temporaries from stack */
973 for (i = 0; i < NUM_TEMPS; i++) {
974 if (i + FIRST_TEMP != 1) {
975 jit_ldxi(JIT_R(i + FIRST_TEMP), LIGHTREC_REG_STATE,
976 offsetof(struct lightrec_state, wrapper_regs[i]));
984 block->opcode_list = NULL;
985 block->flags = BLOCK_NO_OPCODE_LIST;
988 block->function = lightrec_emit_code(state, block, _jit,
990 if (!block->function)
993 state->wrappers_eps[C_WRAPPERS_COUNT - 1] = block->function;
995 for (i = 0; i < C_WRAPPERS_COUNT - 1; i++)
996 state->wrappers_eps[i] = jit_address(addr[i]);
998 if (ENABLE_DISASSEMBLER) {
999 pr_debug("Wrapper block:\n");
1007 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1009 pr_err("Unable to compile wrapper: Out of memory\n");
1013 static u32 lightrec_memset(struct lightrec_state *state)
1015 u32 kunseg_pc = kunseg(state->regs.gpr[4]);
1017 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg_pc);
1018 u32 length = state->regs.gpr[5] * 4;
1021 pr_err("Unable to find memory map for memset target address "
1022 "0x%x\n", kunseg_pc);
1026 pr_debug("Calling host memset, PC 0x%x (host address 0x%" PRIxPTR ") for %u bytes\n",
1027 kunseg_pc, (uintptr_t)host, length);
1028 memset(host, 0, length);
1030 if (!state->invalidate_from_dma_only)
1031 lightrec_invalidate_map(state, map, kunseg_pc, length);
1033 /* Rough estimation of the number of cycles consumed */
1034 return 8 + 5 * (length + 3 / 4);
1037 static u32 lightrec_check_load_delay(struct lightrec_state *state, u32 pc, u8 reg)
1039 struct block *block;
1040 union code first_op;
1042 first_op = lightrec_read_opcode(state, pc);
1044 if (likely(!opcode_reads_register(first_op, reg))) {
1045 state->regs.gpr[reg] = state->temp_reg;
1047 block = lightrec_get_block(state, pc);
1048 if (unlikely(!block)) {
1049 pr_err("Unable to get block at PC 0x%08x\n", pc);
1050 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
1053 pc = lightrec_handle_load_delay(state, block, pc, reg);
1060 static void update_cycle_counter_before_c(jit_state_t *_jit)
1062 /* update state->current_cycle */
1063 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
1064 offsetof(struct lightrec_state, target_cycle));
1065 jit_subr(JIT_R1, JIT_R2, LIGHTREC_REG_CYCLE);
1066 jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
1067 LIGHTREC_REG_STATE, JIT_R1);
1070 static void update_cycle_counter_after_c(jit_state_t *_jit)
1072 /* Recalc the delta */
1073 jit_ldxi_i(JIT_R1, LIGHTREC_REG_STATE,
1074 offsetof(struct lightrec_state, current_cycle));
1075 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
1076 offsetof(struct lightrec_state, target_cycle));
1077 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, JIT_R1);
1080 static struct block * generate_dispatcher(struct lightrec_state *state)
1082 struct block *block;
1084 jit_node_t *to_end, *loop, *addr, *addr2, *addr3, *addr4, *addr5, *jmp, *jmp2;
1088 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
1092 _jit = jit_new_state();
1094 goto err_free_block;
1096 jit_name("dispatcher");
1097 jit_note(__FILE__, __LINE__);
1102 jit_getarg(LIGHTREC_REG_STATE, jit_arg());
1103 jit_getarg(JIT_V0, jit_arg());
1104 jit_getarg(JIT_V1, jit_arg());
1105 jit_getarg_i(LIGHTREC_REG_CYCLE, jit_arg());
1107 /* Force all callee-saved registers to be pushed on the stack */
1108 for (i = 0; i < NUM_REGS; i++)
1109 jit_movr(JIT_V(i + FIRST_REG), JIT_V(i + FIRST_REG));
1113 /* Call the block's code */
1116 if (OPT_REPLACE_MEMSET) {
1117 /* Blocks will jump here when they need to call
1118 * lightrec_memset() */
1119 addr3 = jit_indirect();
1121 jit_movr(JIT_V1, LIGHTREC_REG_CYCLE);
1124 jit_pushargr(LIGHTREC_REG_STATE);
1126 jit_finishi(lightrec_memset);
1127 jit_retval(LIGHTREC_REG_CYCLE);
1129 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE,
1130 offsetof(struct lightrec_state, regs.gpr[31]));
1131 jit_subr(LIGHTREC_REG_CYCLE, JIT_V1, LIGHTREC_REG_CYCLE);
1133 if (OPT_DETECT_IMPOSSIBLE_BRANCHES || OPT_HANDLE_LOAD_DELAYS)
1137 if (OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1138 /* Blocks will jump here when they reach a branch that should
1139 * be executed with the interpreter, passing the branch's PC
1140 * in JIT_V0 and the address of the block in JIT_V1. */
1141 addr4 = jit_indirect();
1143 update_cycle_counter_before_c(_jit);
1146 jit_pushargr(LIGHTREC_REG_STATE);
1147 jit_pushargr(JIT_V1);
1148 jit_pushargr(JIT_V0);
1149 jit_finishi(lightrec_emulate_block);
1153 update_cycle_counter_after_c(_jit);
1155 if (OPT_HANDLE_LOAD_DELAYS)
1160 if (OPT_HANDLE_LOAD_DELAYS) {
1161 /* Blocks will jump here when they reach a branch with a load
1162 * opcode in its delay slot. The delay slot has already been
1163 * executed; the load value is in (state->temp_reg), and the
1164 * register number is in JIT_V1.
1165 * Jump to a C function which will evaluate the branch target's
1166 * first opcode, to make sure that it does not read the register
1167 * in question; and if it does, handle it accordingly. */
1168 addr5 = jit_indirect();
1170 update_cycle_counter_before_c(_jit);
1173 jit_pushargr(LIGHTREC_REG_STATE);
1174 jit_pushargr(JIT_V0);
1175 jit_pushargr(JIT_V1);
1176 jit_finishi(lightrec_check_load_delay);
1180 update_cycle_counter_after_c(_jit);
1182 if (OPT_DETECT_IMPOSSIBLE_BRANCHES)
1186 if (OPT_REPLACE_MEMSET
1187 && (OPT_DETECT_IMPOSSIBLE_BRANCHES || OPT_HANDLE_LOAD_DELAYS)) {
1191 /* The block will jump here, with the number of cycles remaining in
1192 * LIGHTREC_REG_CYCLE */
1193 addr2 = jit_indirect();
1195 /* Store back the next_pc to the lightrec_state structure */
1196 offset = offsetof(struct lightrec_state, next_pc);
1197 jit_stxi_i(offset, LIGHTREC_REG_STATE, JIT_V0);
1199 /* Jump to end if state->target_cycle < state->current_cycle */
1200 to_end = jit_blei(LIGHTREC_REG_CYCLE, 0);
1202 /* Convert next PC to KUNSEG and avoid mirrors */
1203 jit_andi(JIT_V1, JIT_V0, 0x10000000 | (RAM_SIZE - 1));
1204 jit_rshi_u(JIT_R1, JIT_V1, 28);
1205 jit_andi(JIT_R2, JIT_V0, BIOS_SIZE - 1);
1206 jit_addi(JIT_R2, JIT_R2, RAM_SIZE);
1207 jit_movnr(JIT_V1, JIT_R2, JIT_R1);
1209 /* If possible, use the code LUT */
1210 if (!lut_is_32bit(state))
1211 jit_lshi(JIT_V1, JIT_V1, 1);
1212 jit_add_state(JIT_V1, JIT_V1);
1214 offset = offsetof(struct lightrec_state, code_lut);
1215 if (lut_is_32bit(state))
1216 jit_ldxi_ui(JIT_V1, JIT_V1, offset);
1218 jit_ldxi(JIT_V1, JIT_V1, offset);
1220 /* If we get non-NULL, loop */
1221 jit_patch_at(jit_bnei(JIT_V1, 0), loop);
1223 /* The code LUT will be set to this address when the block at the target
1224 * PC has been preprocessed but not yet compiled by the threaded
1226 addr = jit_indirect();
1228 /* Slow path: call C function get_next_block_func() */
1230 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1231 /* We may call the interpreter - update state->current_cycle */
1232 update_cycle_counter_before_c(_jit);
1236 jit_pushargr(LIGHTREC_REG_STATE);
1237 jit_pushargr(JIT_V0);
1239 /* Save the cycles register if needed */
1240 if (!(ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES))
1241 jit_movr(JIT_V0, LIGHTREC_REG_CYCLE);
1243 /* Get the next block */
1244 jit_finishi(&get_next_block_func);
1247 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1248 /* The interpreter may have updated state->current_cycle and
1249 * state->target_cycle - recalc the delta */
1250 update_cycle_counter_after_c(_jit);
1252 jit_movr(LIGHTREC_REG_CYCLE, JIT_V0);
1255 /* Reset JIT_V0 to the next PC */
1256 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE,
1257 offsetof(struct lightrec_state, next_pc));
1259 /* If we get non-NULL, loop */
1260 jit_patch_at(jit_bnei(JIT_V1, 0), loop);
1262 /* When exiting, the recompiled code will jump to that address */
1263 jit_note(__FILE__, __LINE__);
1266 jit_retr(LIGHTREC_REG_CYCLE);
1270 block->opcode_list = NULL;
1271 block->flags = BLOCK_NO_OPCODE_LIST;
1274 block->function = lightrec_emit_code(state, block, _jit,
1276 if (!block->function)
1277 goto err_free_block;
1279 state->eob_wrapper_func = jit_address(addr2);
1280 if (OPT_DETECT_IMPOSSIBLE_BRANCHES)
1281 state->interpreter_func = jit_address(addr4);
1282 if (OPT_HANDLE_LOAD_DELAYS)
1283 state->ds_check_func = jit_address(addr5);
1284 if (OPT_REPLACE_MEMSET)
1285 state->memset_func = jit_address(addr3);
1286 state->get_next_block = jit_address(addr);
1288 if (ENABLE_DISASSEMBLER) {
1289 pr_debug("Dispatcher block:\n");
1298 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1300 pr_err("Unable to compile dispatcher: Out of memory\n");
1304 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc)
1308 lightrec_get_map(state, &host, kunseg(pc));
1310 const u32 *code = (u32 *)host;
1311 return (union code) LE32TOH(*code);
1314 __cnst unsigned int lightrec_cycles_of_opcode(union code code)
1319 void lightrec_free_opcode_list(struct lightrec_state *state, struct opcode *ops)
1321 struct opcode_list *list = container_of(ops, struct opcode_list, ops);
1323 lightrec_free(state, MEM_FOR_IR,
1324 sizeof(*list) + list->nb_ops * sizeof(struct opcode),
1328 static unsigned int lightrec_get_mips_block_len(const u32 *src)
1333 for (i = 1; ; i++) {
1334 c.opcode = LE32TOH(*src++);
1339 if (is_unconditional_jump(c))
1344 static struct opcode * lightrec_disassemble(struct lightrec_state *state,
1345 const u32 *src, unsigned int *len)
1347 struct opcode_list *list;
1348 unsigned int i, length;
1350 length = lightrec_get_mips_block_len(src);
1352 list = lightrec_malloc(state, MEM_FOR_IR,
1353 sizeof(*list) + sizeof(struct opcode) * length);
1355 pr_err("Unable to allocate memory\n");
1359 list->nb_ops = (u16) length;
1361 for (i = 0; i < length; i++) {
1362 list->ops[i].opcode = LE32TOH(src[i]);
1363 list->ops[i].flags = 0;
1366 *len = length * sizeof(u32);
1371 static struct block * lightrec_precompile_block(struct lightrec_state *state,
1374 struct opcode *list;
1375 struct block *block;
1377 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg(pc));
1378 const u32 *code = (u32 *) host;
1379 unsigned int length;
1386 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
1388 pr_err("Unable to recompile block: Out of memory\n");
1392 list = lightrec_disassemble(state, code, &length);
1394 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1400 block->function = NULL;
1401 block->opcode_list = list;
1405 block->code_size = 0;
1406 block->precompile_date = state->current_cycle;
1407 block->nb_ops = length / sizeof(u32);
1409 lightrec_optimize(state, block);
1411 length = block->nb_ops * sizeof(u32);
1413 lightrec_register(MEM_FOR_MIPS_CODE, length);
1415 if (ENABLE_DISASSEMBLER) {
1416 pr_debug("Disassembled block at PC: 0x%08x\n", block->pc);
1417 lightrec_print_disassembly(block, code);
1420 pr_debug("Block size: %hu opcodes\n", block->nb_ops);
1422 fully_tagged = lightrec_block_is_fully_tagged(block);
1424 block_flags |= BLOCK_FULLY_TAGGED;
1427 block_set_flags(block, block_flags);
1429 block->hash = lightrec_calculate_block_hash(block);
1431 if (OPT_REPLACE_MEMSET && block_has_flag(block, BLOCK_IS_MEMSET))
1432 addr = state->memset_func;
1434 addr = state->get_next_block;
1435 lut_write(state, lut_offset(pc), addr);
1437 pr_debug("Blocks created: %u\n", ++state->nb_precompile);
1442 static bool lightrec_block_is_fully_tagged(const struct block *block)
1444 const struct opcode *op;
1447 for (i = 0; i < block->nb_ops; i++) {
1448 op = &block->opcode_list[i];
1450 /* If we have one branch that must be emulated, we cannot trash
1451 * the opcode list. */
1452 if (should_emulate(op))
1455 /* Check all loads/stores of the opcode list and mark the
1456 * block as fully compiled if they all have been tagged. */
1457 switch (op->c.i.op) {
1472 if (!LIGHTREC_FLAGS_GET_IO_MODE(op->flags))
1483 static void lightrec_reap_block(struct lightrec_state *state, void *data)
1485 struct block *block = data;
1487 pr_debug("Reap dead block at PC 0x%08x\n", block->pc);
1488 lightrec_unregister_block(state->block_cache, block);
1489 lightrec_free_block(state, block);
1492 static void lightrec_reap_jit(struct lightrec_state *state, void *data)
1494 _jit_destroy_state(data);
1497 static void lightrec_free_function(struct lightrec_state *state, void *fn)
1499 if (ENABLE_CODE_BUFFER && state->tlsf) {
1500 pr_debug("Freeing code block at 0x%" PRIxPTR "\n", (uintptr_t) fn);
1501 lightrec_free_code(state, fn);
1505 static void lightrec_reap_function(struct lightrec_state *state, void *data)
1507 lightrec_free_function(state, data);
1510 static void lightrec_reap_opcode_list(struct lightrec_state *state, void *data)
1512 lightrec_free_opcode_list(state, data);
1515 int lightrec_compile_block(struct lightrec_cstate *cstate,
1516 struct block *block)
1518 struct lightrec_state *state = cstate->state;
1519 struct lightrec_branch_target *target;
1520 bool fully_tagged = false;
1521 struct block *block2;
1523 jit_state_t *_jit, *oldjit;
1524 jit_node_t *start_of_block;
1525 bool skip_next = false;
1526 void *old_fn, *new_fn;
1527 size_t old_code_size;
1532 fully_tagged = lightrec_block_is_fully_tagged(block);
1534 block_set_flags(block, BLOCK_FULLY_TAGGED);
1536 _jit = jit_new_state();
1540 oldjit = block->_jit;
1541 old_fn = block->function;
1542 old_code_size = block->code_size;
1545 lightrec_regcache_reset(cstate->reg_cache);
1546 lightrec_preload_pc(cstate->reg_cache);
1549 cstate->nb_local_branches = 0;
1550 cstate->nb_targets = 0;
1551 cstate->no_load_delay = false;
1556 start_of_block = jit_label();
1558 for (i = 0; i < block->nb_ops; i++) {
1559 elm = &block->opcode_list[i];
1566 if (should_emulate(elm)) {
1567 pr_debug("Branch at offset 0x%x will be emulated\n",
1570 lightrec_emit_jump_to_interpreter(cstate, block, i);
1571 skip_next = !op_flag_no_ds(elm->flags);
1573 lightrec_rec_opcode(cstate, block, i);
1574 skip_next = !op_flag_no_ds(elm->flags) && has_delay_slot(elm->c);
1576 /* FIXME: GNU Lightning on Windows seems to use our
1577 * mapped registers as temporaries. Until the actual bug
1578 * is found and fixed, unconditionally mark our
1579 * registers as live here. */
1580 lightrec_regcache_mark_live(cstate->reg_cache, _jit);
1584 cstate->cycles += lightrec_cycles_of_opcode(elm->c);
1587 for (i = 0; i < cstate->nb_local_branches; i++) {
1588 struct lightrec_branch *branch = &cstate->local_branches[i];
1590 pr_debug("Patch local branch to offset 0x%x\n",
1591 branch->target << 2);
1593 if (branch->target == 0) {
1594 jit_patch_at(branch->branch, start_of_block);
1598 for (j = 0; j < cstate->nb_targets; j++) {
1599 if (cstate->targets[j].offset == branch->target) {
1600 jit_patch_at(branch->branch,
1601 cstate->targets[j].label);
1606 if (j == cstate->nb_targets)
1607 pr_err("Unable to find branch target\n");
1613 new_fn = lightrec_emit_code(state, block, _jit, &block->code_size);
1615 if (!ENABLE_THREADED_COMPILER)
1616 pr_err("Unable to compile block!\n");
1617 block->_jit = oldjit;
1619 _jit_destroy_state(_jit);
1623 /* Pause the reaper, because lightrec_reset_lut_offset() may try to set
1624 * the old block->function pointer to the code LUT. */
1625 if (ENABLE_THREADED_COMPILER)
1626 lightrec_reaper_pause(state->reaper);
1628 block->function = new_fn;
1629 block_clear_flags(block, BLOCK_SHOULD_RECOMPILE);
1631 /* Add compiled function to the LUT */
1632 lut_write(state, lut_offset(block->pc), block->function);
1634 if (ENABLE_THREADED_COMPILER)
1635 lightrec_reaper_continue(state->reaper);
1637 /* Detect old blocks that have been covered by the new one */
1638 for (i = 0; i < cstate->nb_targets; i++) {
1639 target = &cstate->targets[i];
1641 if (!target->offset)
1644 offset = block->pc + target->offset * sizeof(u32);
1646 /* Pause the reaper while we search for the block until we set
1647 * the BLOCK_IS_DEAD flag, otherwise the block may be removed
1648 * under our feet. */
1649 if (ENABLE_THREADED_COMPILER)
1650 lightrec_reaper_pause(state->reaper);
1652 block2 = lightrec_find_block(state->block_cache, offset);
1654 /* No need to check if block2 is compilable - it must
1655 * be, otherwise block wouldn't be compilable either */
1657 /* Set the "block dead" flag to prevent the dynarec from
1658 * recompiling this block */
1659 old_flags = block_set_flags(block2, BLOCK_IS_DEAD);
1662 if (ENABLE_THREADED_COMPILER) {
1663 lightrec_reaper_continue(state->reaper);
1665 /* If block2 was pending for compilation, cancel it.
1666 * If it's being compiled right now, wait until it
1669 lightrec_recompiler_remove(state->rec, block2);
1672 /* We know from now on that block2 (if present) isn't going to
1673 * be compiled. We can override the LUT entry with our new
1674 * block's entry point. */
1675 offset = lut_offset(block->pc) + target->offset;
1676 lut_write(state, offset, jit_address(target->label));
1679 pr_debug("Reap block 0x%08x as it's covered by block "
1680 "0x%08x\n", block2->pc, block->pc);
1682 /* Finally, reap the block. */
1683 if (!ENABLE_THREADED_COMPILER) {
1684 lightrec_unregister_block(state->block_cache, block2);
1685 lightrec_free_block(state, block2);
1686 } else if (!(old_flags & BLOCK_IS_DEAD)) {
1687 lightrec_reaper_add(state->reaper,
1688 lightrec_reap_block,
1694 if (ENABLE_DISASSEMBLER) {
1695 pr_debug("Compiling block at PC: 0x%08x\n", block->pc);
1702 old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
1704 if (fully_tagged && !(old_flags & BLOCK_NO_OPCODE_LIST)) {
1705 pr_debug("Block PC 0x%08x is fully tagged"
1706 " - free opcode list\n", block->pc);
1708 if (ENABLE_THREADED_COMPILER) {
1709 lightrec_reaper_add(state->reaper,
1710 lightrec_reap_opcode_list,
1711 block->opcode_list);
1713 lightrec_free_opcode_list(state, block->opcode_list);
1718 pr_debug("Block 0x%08x recompiled, reaping old jit context.\n",
1721 if (ENABLE_THREADED_COMPILER) {
1722 lightrec_reaper_add(state->reaper,
1723 lightrec_reap_jit, oldjit);
1724 lightrec_reaper_add(state->reaper,
1725 lightrec_reap_function, old_fn);
1727 _jit_destroy_state(oldjit);
1728 lightrec_free_function(state, old_fn);
1731 lightrec_unregister(MEM_FOR_CODE, old_code_size);
1734 pr_debug("Blocks compiled: %u\n", ++state->nb_compile);
1739 static void lightrec_print_info(struct lightrec_state *state)
1741 if ((state->current_cycle & ~0xfffffff) != state->old_cycle_counter) {
1742 pr_info("Lightrec RAM usage: IR %u KiB, CODE %u KiB, "
1743 "MIPS %u KiB, TOTAL %u KiB, avg. IPI %f\n",
1744 lightrec_get_mem_usage(MEM_FOR_IR) / 1024,
1745 lightrec_get_mem_usage(MEM_FOR_CODE) / 1024,
1746 lightrec_get_mem_usage(MEM_FOR_MIPS_CODE) / 1024,
1747 lightrec_get_total_mem_usage() / 1024,
1748 lightrec_get_average_ipi());
1749 state->old_cycle_counter = state->current_cycle & ~0xfffffff;
1753 u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
1755 s32 (*func)(struct lightrec_state *, u32, void *, s32) = (void *)state->dispatcher->function;
1759 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1761 /* Handle the cycle counter overflowing */
1762 if (unlikely(target_cycle < state->current_cycle))
1763 target_cycle = UINT_MAX;
1765 state->target_cycle = target_cycle;
1766 state->next_pc = pc;
1768 block_trace = get_next_block_func(state, pc);
1770 cycles_delta = state->target_cycle - state->current_cycle;
1772 cycles_delta = (*func)(state, state->next_pc,
1773 block_trace, cycles_delta);
1775 state->current_cycle = state->target_cycle - cycles_delta;
1778 if (ENABLE_THREADED_COMPILER)
1779 lightrec_reaper_reap(state->reaper);
1781 if (LOG_LEVEL >= INFO_L)
1782 lightrec_print_info(state);
1784 return state->next_pc;
1787 u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc,
1790 struct block *block;
1792 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1793 state->target_cycle = target_cycle;
1796 block = lightrec_get_block(state, pc);
1800 pc = lightrec_emulate_block(state, block, pc);
1802 if (ENABLE_THREADED_COMPILER)
1803 lightrec_reaper_reap(state->reaper);
1804 } while (state->current_cycle < state->target_cycle);
1806 if (LOG_LEVEL >= INFO_L)
1807 lightrec_print_info(state);
1812 void lightrec_free_block(struct lightrec_state *state, struct block *block)
1816 lightrec_unregister(MEM_FOR_MIPS_CODE, block->nb_ops * sizeof(u32));
1817 old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
1819 if (!(old_flags & BLOCK_NO_OPCODE_LIST))
1820 lightrec_free_opcode_list(state, block->opcode_list);
1822 _jit_destroy_state(block->_jit);
1823 if (block->function) {
1824 lightrec_free_function(state, block->function);
1825 lightrec_unregister(MEM_FOR_CODE, block->code_size);
1827 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1830 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state)
1832 struct lightrec_cstate *cstate;
1834 cstate = lightrec_malloc(state, MEM_FOR_LIGHTREC, sizeof(*cstate));
1838 cstate->reg_cache = lightrec_regcache_init(state);
1839 if (!cstate->reg_cache) {
1840 lightrec_free(state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
1844 cstate->state = state;
1849 void lightrec_free_cstate(struct lightrec_cstate *cstate)
1851 lightrec_free_regcache(cstate->reg_cache);
1852 lightrec_free(cstate->state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
1855 struct lightrec_state * lightrec_init(char *argv0,
1856 const struct lightrec_mem_map *map,
1858 const struct lightrec_ops *ops)
1860 const struct lightrec_mem_map *codebuf_map = &map[PSX_MAP_CODE_BUFFER];
1861 struct lightrec_state *state;
1864 bool with_32bit_lut = false;
1867 /* Sanity-check ops */
1868 if (!ops || !ops->cop2_op || !ops->enable_ram) {
1869 pr_err("Missing callbacks in lightrec_ops structure\n");
1873 if (ops->cop2_notify)
1874 pr_debug("Optional cop2_notify callback in lightrec_ops\n");
1876 pr_debug("No optional cop2_notify callback in lightrec_ops\n");
1878 if (ENABLE_CODE_BUFFER && nb > PSX_MAP_CODE_BUFFER
1879 && codebuf_map->address) {
1880 tlsf = tlsf_create_with_pool(codebuf_map->address,
1881 codebuf_map->length);
1883 pr_err("Unable to initialize code buffer\n");
1887 if (__WORDSIZE == 64) {
1888 addr = (uintptr_t) codebuf_map->address + codebuf_map->length - 1;
1889 with_32bit_lut = addr == (u32) addr;
1894 lut_size = CODE_LUT_SIZE * 4;
1896 lut_size = CODE_LUT_SIZE * sizeof(void *);
1900 state = calloc(1, sizeof(*state) + lut_size);
1902 goto err_finish_jit;
1904 lightrec_register(MEM_FOR_LIGHTREC, sizeof(*state) + lut_size);
1907 state->with_32bit_lut = with_32bit_lut;
1908 state->in_delay_slot_n = 0xff;
1910 state->block_cache = lightrec_blockcache_init(state);
1911 if (!state->block_cache)
1912 goto err_free_state;
1914 if (ENABLE_THREADED_COMPILER) {
1915 state->rec = lightrec_recompiler_init(state);
1917 goto err_free_block_cache;
1919 state->reaper = lightrec_reaper_init(state);
1921 goto err_free_recompiler;
1923 state->cstate = lightrec_create_cstate(state);
1925 goto err_free_block_cache;
1928 state->nb_maps = nb;
1931 memcpy(&state->ops, ops, sizeof(*ops));
1933 state->dispatcher = generate_dispatcher(state);
1934 if (!state->dispatcher)
1935 goto err_free_reaper;
1937 state->c_wrapper_block = generate_wrapper(state);
1938 if (!state->c_wrapper_block)
1939 goto err_free_dispatcher;
1941 state->c_wrappers[C_WRAPPER_RW] = lightrec_rw_cb;
1942 state->c_wrappers[C_WRAPPER_RW_GENERIC] = lightrec_rw_generic_cb;
1943 state->c_wrappers[C_WRAPPER_MFC] = lightrec_mfc_cb;
1944 state->c_wrappers[C_WRAPPER_MTC] = lightrec_mtc_cb;
1945 state->c_wrappers[C_WRAPPER_CP] = lightrec_cp_cb;
1947 map = &state->maps[PSX_MAP_BIOS];
1948 state->offset_bios = (uintptr_t)map->address - map->pc;
1950 map = &state->maps[PSX_MAP_SCRATCH_PAD];
1951 state->offset_scratch = (uintptr_t)map->address - map->pc;
1953 map = &state->maps[PSX_MAP_HW_REGISTERS];
1954 state->offset_io = (uintptr_t)map->address - map->pc;
1956 map = &state->maps[PSX_MAP_KERNEL_USER_RAM];
1957 state->offset_ram = (uintptr_t)map->address - map->pc;
1959 if (state->maps[PSX_MAP_MIRROR1].address == map->address + 0x200000 &&
1960 state->maps[PSX_MAP_MIRROR2].address == map->address + 0x400000 &&
1961 state->maps[PSX_MAP_MIRROR3].address == map->address + 0x600000)
1962 state->mirrors_mapped = true;
1964 if (state->offset_bios == 0 &&
1965 state->offset_scratch == 0 &&
1966 state->offset_ram == 0 &&
1967 state->offset_io == 0 &&
1968 state->mirrors_mapped) {
1969 pr_info("Memory map is perfect. Emitted code will be best.\n");
1971 pr_info("Memory map is sub-par. Emitted code will be slow.\n");
1974 if (state->with_32bit_lut)
1975 pr_info("Using 32-bit LUT\n");
1979 err_free_dispatcher:
1980 lightrec_free_block(state, state->dispatcher);
1982 if (ENABLE_THREADED_COMPILER)
1983 lightrec_reaper_destroy(state->reaper);
1984 err_free_recompiler:
1985 if (ENABLE_THREADED_COMPILER)
1986 lightrec_free_recompiler(state->rec);
1988 lightrec_free_cstate(state->cstate);
1989 err_free_block_cache:
1990 lightrec_free_block_cache(state->block_cache);
1992 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
1993 lut_elm_size(state) * CODE_LUT_SIZE);
1997 if (ENABLE_CODE_BUFFER && tlsf)
2002 void lightrec_destroy(struct lightrec_state *state)
2004 /* Force a print info on destroy*/
2005 state->current_cycle = ~state->current_cycle;
2006 lightrec_print_info(state);
2008 lightrec_free_block_cache(state->block_cache);
2009 lightrec_free_block(state, state->dispatcher);
2010 lightrec_free_block(state, state->c_wrapper_block);
2012 if (ENABLE_THREADED_COMPILER) {
2013 lightrec_free_recompiler(state->rec);
2014 lightrec_reaper_destroy(state->reaper);
2016 lightrec_free_cstate(state->cstate);
2020 if (ENABLE_CODE_BUFFER && state->tlsf)
2021 tlsf_destroy(state->tlsf);
2023 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
2024 lut_elm_size(state) * CODE_LUT_SIZE);
2028 void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len)
2030 u32 kaddr = kunseg(addr & ~0x3);
2031 enum psx_map idx = lightrec_get_map_idx(state, kaddr);
2034 case PSX_MAP_MIRROR1:
2035 case PSX_MAP_MIRROR2:
2036 case PSX_MAP_MIRROR3:
2037 /* Handle mirrors */
2038 kaddr &= RAM_SIZE - 1;
2040 case PSX_MAP_KERNEL_USER_RAM:
2046 memset(lut_address(state, lut_offset(kaddr)), 0,
2047 ((len + 3) / 4) * lut_elm_size(state));
2050 void lightrec_invalidate_all(struct lightrec_state *state)
2052 memset(state->code_lut, 0, lut_elm_size(state) * CODE_LUT_SIZE);
2055 void lightrec_set_invalidate_mode(struct lightrec_state *state, bool dma_only)
2057 if (state->invalidate_from_dma_only != dma_only)
2058 lightrec_invalidate_all(state);
2060 state->invalidate_from_dma_only = dma_only;
2063 void lightrec_set_exit_flags(struct lightrec_state *state, u32 flags)
2065 if (flags != LIGHTREC_EXIT_NORMAL) {
2066 state->exit_flags |= flags;
2067 state->target_cycle = state->current_cycle;
2071 u32 lightrec_exit_flags(struct lightrec_state *state)
2073 return state->exit_flags;
2076 u32 lightrec_current_cycle_count(const struct lightrec_state *state)
2078 return state->current_cycle;
2081 void lightrec_reset_cycle_count(struct lightrec_state *state, u32 cycles)
2083 state->current_cycle = cycles;
2085 if (state->target_cycle < cycles)
2086 state->target_cycle = cycles;
2089 void lightrec_set_target_cycle_count(struct lightrec_state *state, u32 cycles)
2091 if (state->exit_flags == LIGHTREC_EXIT_NORMAL) {
2092 if (cycles < state->current_cycle)
2093 cycles = state->current_cycle;
2095 state->target_cycle = cycles;
2099 struct lightrec_registers * lightrec_get_registers(struct lightrec_state *state)
2101 return &state->regs;