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_reap_block(struct lightrec_state *state, void *data);
40 static void lightrec_default_sb(struct lightrec_state *state, u32 opcode,
41 void *host, u32 addr, u32 data)
43 *(u8 *)host = (u8)data;
45 if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
46 lightrec_invalidate(state, addr, 1);
49 static void lightrec_default_sh(struct lightrec_state *state, u32 opcode,
50 void *host, u32 addr, u32 data)
52 *(u16 *)host = HTOLE16((u16)data);
54 if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
55 lightrec_invalidate(state, addr, 2);
58 static void lightrec_default_sw(struct lightrec_state *state, u32 opcode,
59 void *host, u32 addr, u32 data)
61 *(u32 *)host = HTOLE32(data);
63 if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
64 lightrec_invalidate(state, addr, 4);
67 static u8 lightrec_default_lb(struct lightrec_state *state,
68 u32 opcode, void *host, u32 addr)
73 static u16 lightrec_default_lh(struct lightrec_state *state,
74 u32 opcode, void *host, u32 addr)
76 return LE16TOH(*(u16 *)host);
79 static u32 lightrec_default_lw(struct lightrec_state *state,
80 u32 opcode, void *host, u32 addr)
82 return LE32TOH(*(u32 *)host);
85 static u32 lightrec_default_lwu(struct lightrec_state *state,
86 u32 opcode, void *host, u32 addr)
90 memcpy(&val, host, 4);
95 static void lightrec_default_swu(struct lightrec_state *state, u32 opcode,
96 void *host, u32 addr, u32 data)
100 memcpy(host, &data, 4);
102 if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
103 lightrec_invalidate(state, addr & ~0x3, 8);
106 static const struct lightrec_mem_map_ops lightrec_default_ops = {
107 .sb = lightrec_default_sb,
108 .sh = lightrec_default_sh,
109 .sw = lightrec_default_sw,
110 .lb = lightrec_default_lb,
111 .lh = lightrec_default_lh,
112 .lw = lightrec_default_lw,
113 .lwu = lightrec_default_lwu,
114 .swu = lightrec_default_swu,
117 static void __segfault_cb(struct lightrec_state *state, u32 addr,
118 const struct block *block)
120 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
121 pr_err("Segmentation fault in recompiled code: invalid "
122 "load/store at address "PC_FMT"\n", addr);
124 pr_err("Was executing block "PC_FMT"\n", block->pc);
127 static void lightrec_swl(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 = shift < 3 ? GENMASK(31, (shift + 1) * 8) : 0;
135 /* Align to 32 bits */
137 host = (void *)((uintptr_t)host & ~3);
139 old_data = ops->lw(state, opcode, host, addr);
141 data = (data >> ((3 - shift) * 8)) | (old_data & mask);
143 ops->sw(state, opcode, host, addr, data);
146 static void lightrec_swr(struct lightrec_state *state,
147 const struct lightrec_mem_map_ops *ops,
148 u32 opcode, void *host, u32 addr, u32 data)
150 unsigned int shift = addr & 0x3;
151 unsigned int mask = (1 << (shift * 8)) - 1;
154 /* Align to 32 bits */
156 host = (void *)((uintptr_t)host & ~3);
158 old_data = ops->lw(state, opcode, host, addr);
160 data = (data << (shift * 8)) | (old_data & mask);
162 ops->sw(state, opcode, host, addr, data);
165 static void lightrec_swc2(struct lightrec_state *state, union code op,
166 const struct lightrec_mem_map_ops *ops,
167 void *host, u32 addr)
169 u32 data = lightrec_mfc2(state, op.i.rt);
171 ops->sw(state, op.opcode, host, addr, data);
174 static u32 lightrec_lwl(struct lightrec_state *state,
175 const struct lightrec_mem_map_ops *ops,
176 u32 opcode, void *host, u32 addr, u32 data)
178 unsigned int shift = addr & 0x3;
179 unsigned int mask = (1 << (24 - shift * 8)) - 1;
182 /* Align to 32 bits */
184 host = (void *)((uintptr_t)host & ~3);
186 old_data = ops->lw(state, opcode, host, addr);
188 return (data & mask) | (old_data << (24 - shift * 8));
191 static u32 lightrec_lwr(struct lightrec_state *state,
192 const struct lightrec_mem_map_ops *ops,
193 u32 opcode, void *host, u32 addr, u32 data)
195 unsigned int shift = addr & 0x3;
196 unsigned int mask = shift ? GENMASK(31, 32 - shift * 8) : 0;
199 /* Align to 32 bits */
201 host = (void *)((uintptr_t)host & ~3);
203 old_data = ops->lw(state, opcode, host, addr);
205 return (data & mask) | (old_data >> (shift * 8));
208 static void lightrec_lwc2(struct lightrec_state *state, union code op,
209 const struct lightrec_mem_map_ops *ops,
210 void *host, u32 addr)
212 u32 data = ops->lw(state, op.opcode, host, addr);
214 lightrec_mtc2(state, op.i.rt, data);
217 static void lightrec_invalidate_map(struct lightrec_state *state,
218 const struct lightrec_mem_map *map, u32 addr, u32 len)
220 if (map == &state->maps[PSX_MAP_KERNEL_USER_RAM]) {
221 memset(lut_address(state, lut_offset(addr)), 0,
222 ((len + 3) / 4) * lut_elm_size(state));
227 lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr)
229 const struct lightrec_mem_map *map;
232 for (i = 0; i < state->nb_maps; i++) {
233 map = &state->maps[i];
235 if (kaddr >= map->pc && kaddr < map->pc + map->length)
236 return (enum psx_map) i;
239 return PSX_MAP_UNKNOWN;
242 const struct lightrec_mem_map *
243 lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr)
245 const struct lightrec_mem_map *map;
249 idx = lightrec_get_map_idx(state, kaddr);
250 if (idx == PSX_MAP_UNKNOWN)
253 map = &state->maps[idx];
254 addr = kaddr - map->pc;
256 while (map->mirror_of)
257 map = map->mirror_of;
260 *host = map->address + addr;
265 u32 lightrec_rw(struct lightrec_state *state, union code op, u32 base,
266 u32 data, u32 *flags, struct block *block, u16 offset)
268 const struct lightrec_mem_map *map;
269 const struct lightrec_mem_map_ops *ops;
270 u32 opcode = op.opcode;
271 bool was_tagged = true;
276 addr = kunseg(base + (s16) op.i.imm);
278 map = lightrec_get_map(state, &host, addr);
280 __segfault_cb(state, addr, block);
285 was_tagged = LIGHTREC_FLAGS_GET_IO_MODE(*flags);
287 if (likely(!map->ops)) {
288 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags)) {
289 /* Force parallel port accesses as HW accesses, because
290 * the direct-I/O emitters can't differenciate it. */
291 if (unlikely(map == &state->maps[PSX_MAP_PARALLEL_PORT]))
292 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
293 /* If the base register is 0x0, be extra suspicious.
294 * Some games (e.g. Sled Storm) actually do segmentation
295 * faults by using uninitialized pointers, which are
296 * later initialized to point to hardware registers. */
297 else if (op.i.rs && base == 0x0)
298 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
300 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
303 ops = &lightrec_default_ops;
305 LIGHTREC_FLAGS_GET_IO_MODE(*flags) == LIGHTREC_IO_DIRECT_HW) {
306 ops = &lightrec_default_ops;
308 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
309 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
315 old_flags = block_set_flags(block, BLOCK_SHOULD_RECOMPILE);
317 if (!(old_flags & BLOCK_SHOULD_RECOMPILE)) {
318 pr_debug("Opcode of block at "PC_FMT" has been tagged"
319 " - flag for recompilation\n", block->pc);
321 lut_write(state, lut_offset(block->pc), NULL);
327 ops->sb(state, opcode, host, addr, data);
330 ops->sh(state, opcode, host, addr, data);
333 lightrec_swl(state, ops, opcode, host, addr, data);
336 lightrec_swr(state, ops, opcode, host, addr, data);
339 ops->sw(state, opcode, host, addr, data);
342 lightrec_swc2(state, op, ops, host, addr);
345 return (s32) (s8) ops->lb(state, opcode, host, addr);
347 return ops->lb(state, opcode, host, addr);
349 return (s32) (s16) ops->lh(state, opcode, host, addr);
351 return ops->lh(state, opcode, host, addr);
353 lightrec_lwc2(state, op, ops, host, addr);
356 return lightrec_lwl(state, ops, opcode, host, addr, data);
358 return lightrec_lwr(state, ops, opcode, host, addr, data);
360 return ops->lwu(state, opcode, host, addr);
362 ops->swu(state, opcode, host, addr, data);
366 return ops->lw(state, opcode, host, addr);
370 static void lightrec_rw_helper(struct lightrec_state *state,
371 union code op, u32 *flags,
372 struct block *block, u16 offset)
374 u32 ret = lightrec_rw(state, op, state->regs.gpr[op.i.rs],
375 state->regs.gpr[op.i.rt], flags, block, offset);
386 if (OPT_HANDLE_LOAD_DELAYS && unlikely(!state->in_delay_slot_n)) {
387 state->temp_reg = ret;
388 state->in_delay_slot_n = 0xff;
389 } else if (op.i.rt) {
390 state->regs.gpr[op.i.rt] = ret;
398 static void lightrec_rw_cb(struct lightrec_state *state, u32 arg)
400 lightrec_rw_helper(state, (union code) arg, NULL, NULL, 0);
403 static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
407 u16 offset = (u16)arg;
409 block = lightrec_find_block_from_lut(state->block_cache,
410 arg >> 16, state->curr_pc);
411 if (unlikely(!block)) {
412 pr_err("rw_generic: No block found in LUT for "PC_FMT" offset 0x%"PRIx16"\n",
413 state->curr_pc, offset);
414 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
418 op = &block->opcode_list[offset];
419 lightrec_rw_helper(state, op->c, &op->flags, block, offset);
422 static u32 clamp_s32(s32 val, s32 min, s32 max)
424 return val < min ? min : val > max ? max : val;
427 static u16 load_u16(u32 *ptr)
429 return ((struct u16x2 *) ptr)->l;
432 static void store_u16(u32 *ptr, u16 value)
434 ((struct u16x2 *) ptr)->l = value;
437 static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg)
439 s16 gteir1, gteir2, gteir3;
449 return (s32)(s16) load_u16(&state->regs.cp2d[reg]);
455 return load_u16(&state->regs.cp2d[reg]);
458 gteir1 = (s16) load_u16(&state->regs.cp2d[9]);
459 gteir2 = (s16) load_u16(&state->regs.cp2d[10]);
460 gteir3 = (s16) load_u16(&state->regs.cp2d[11]);
462 return clamp_s32(gteir1 >> 7, 0, 0x1f) << 0 |
463 clamp_s32(gteir2 >> 7, 0, 0x1f) << 5 |
464 clamp_s32(gteir3 >> 7, 0, 0x1f) << 10;
469 return state->regs.cp2d[reg];
473 u32 lightrec_mfc(struct lightrec_state *state, union code op)
477 if (op.i.op == OP_CP0)
478 return state->regs.cp0[op.r.rd];
480 if (op.i.op == OP_SWC2) {
481 val = lightrec_mfc2(state, op.i.rt);
482 } else if (op.r.rs == OP_CP2_BASIC_MFC2)
483 val = lightrec_mfc2(state, op.r.rd);
485 val = state->regs.cp2c[op.r.rd];
502 if (state->ops.cop2_notify)
503 (*state->ops.cop2_notify)(state, op.opcode, val);
508 static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
510 u32 rt = lightrec_mfc(state, op);
512 if (op.i.op == OP_SWC2)
513 state->temp_reg = rt;
515 state->regs.gpr[op.r.rt] = rt;
518 static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
520 u32 status, oldstatus, cause;
528 /* Those registers are read-only */
535 status = state->regs.cp0[12];
538 if (status & ~data & BIT(16)) {
539 state->ops.enable_ram(state, true);
540 lightrec_invalidate_all(state);
541 } else if (~status & data & BIT(16)) {
542 state->ops.enable_ram(state, false);
547 state->regs.cp0[13] &= ~0x300;
548 state->regs.cp0[13] |= data & 0x300;
550 state->regs.cp0[reg] = data;
553 if (reg == 12 || reg == 13) {
554 cause = state->regs.cp0[13];
555 status = state->regs.cp0[12];
557 /* Handle software interrupts */
558 if ((!!(status & cause & 0x300)) & status)
559 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
561 /* Handle hardware interrupts */
562 if (reg == 12 && !(~status & 0x401) && (~oldstatus & 0x401))
563 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
567 static u32 count_leading_bits(s32 data)
572 #if __has_builtin(__builtin_clrsb)
573 return 1 + __builtin_clrsb(data);
577 data = (data ^ (data >> 31)) << 1;
587 static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data)
591 state->regs.cp2d[12] = state->regs.cp2d[13];
592 state->regs.cp2d[13] = state->regs.cp2d[14];
593 state->regs.cp2d[14] = data;
596 state->regs.cp2d[9] = (data << 7) & 0xf80;
597 state->regs.cp2d[10] = (data << 2) & 0xf80;
598 state->regs.cp2d[11] = (data >> 3) & 0xf80;
603 state->regs.cp2d[31] = count_leading_bits((s32) data);
606 state->regs.cp2d[reg] = data;
611 static void lightrec_ctc2(struct lightrec_state *state, u8 reg, u32 data)
621 store_u16(&state->regs.cp2c[reg], data);
624 data = (data & 0x7ffff000) | !!(data & 0x7f87e000) << 31;
627 state->regs.cp2c[reg] = data;
632 void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data)
634 if (op.i.op == OP_CP0) {
635 lightrec_mtc0(state, reg, data);
637 if (op.i.op == OP_LWC2 || op.r.rs != OP_CP2_BASIC_CTC2)
638 lightrec_mtc2(state, reg, data);
640 lightrec_ctc2(state, reg, data);
642 if (state->ops.cop2_notify)
643 (*state->ops.cop2_notify)(state, op.opcode, data);
647 static void lightrec_mtc_cb(struct lightrec_state *state, u32 arg)
649 union code op = (union code) arg;
653 if (op.i.op == OP_LWC2) {
654 data = state->temp_reg;
657 data = state->regs.gpr[op.r.rt];
661 lightrec_mtc(state, op, reg, data);
664 void lightrec_rfe(struct lightrec_state *state)
668 /* Read CP0 Status register (r12) */
669 status = state->regs.cp0[12];
671 /* Switch the bits */
672 status = ((status & 0x3c) >> 2) | (status & ~0xf);
675 lightrec_mtc0(state, 12, status);
678 void lightrec_cp(struct lightrec_state *state, union code op)
680 if (op.i.op == OP_CP0) {
681 pr_err("Invalid CP opcode to coprocessor #0\n");
685 (*state->ops.cop2_op)(state, op.opcode);
688 static void lightrec_cp_cb(struct lightrec_state *state, u32 arg)
690 lightrec_cp(state, (union code) arg);
693 static struct block * lightrec_get_block(struct lightrec_state *state, u32 pc)
695 struct block *block = lightrec_find_block(state->block_cache, pc);
698 if (block && lightrec_block_is_outdated(state, block)) {
699 pr_debug("Block at "PC_FMT" is outdated!\n", block->pc);
701 old_flags = block_set_flags(block, BLOCK_IS_DEAD);
702 if (!(old_flags & BLOCK_IS_DEAD)) {
703 /* Make sure the recompiler isn't processing the block
705 if (ENABLE_THREADED_COMPILER)
706 lightrec_recompiler_remove(state->rec, block);
708 remove_from_code_lut(state->block_cache, block);
710 if (ENABLE_THREADED_COMPILER) {
711 lightrec_reaper_add(state->reaper,
712 lightrec_reap_block, block);
714 lightrec_unregister_block(state->block_cache, block);
715 lightrec_free_block(state, block);
723 block = lightrec_precompile_block(state, pc);
725 pr_err("Unable to recompile block at "PC_FMT"\n", pc);
726 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
730 lightrec_register_block(state->block_cache, block);
736 static void * get_next_block_func(struct lightrec_state *state, u32 pc)
739 bool should_recompile;
744 func = lut_read(state, lut_offset(pc));
745 if (func && func != state->get_next_block)
748 block = lightrec_get_block(state, pc);
750 if (unlikely(!block))
753 if (OPT_REPLACE_MEMSET &&
754 block_has_flag(block, BLOCK_IS_MEMSET)) {
755 func = state->memset_func;
759 should_recompile = block_has_flag(block, BLOCK_SHOULD_RECOMPILE) &&
760 !block_has_flag(block, BLOCK_NEVER_COMPILE) &&
761 !block_has_flag(block, BLOCK_IS_DEAD);
763 if (unlikely(should_recompile)) {
764 pr_debug("Block at "PC_FMT" should recompile\n", pc);
766 if (ENABLE_THREADED_COMPILER) {
767 lightrec_recompiler_add(state->rec, block);
769 err = lightrec_compile_block(state->cstate, block);
771 state->exit_flags = LIGHTREC_EXIT_NOMEM;
777 if (ENABLE_THREADED_COMPILER && likely(!should_recompile))
778 func = lightrec_recompiler_run_first_pass(state, block, &pc);
780 func = block->function;
785 if (unlikely(block_has_flag(block, BLOCK_NEVER_COMPILE))) {
786 pc = lightrec_emulate_block(state, block, pc);
788 } else if (!ENABLE_THREADED_COMPILER) {
789 /* Block wasn't compiled yet - run the interpreter */
790 if (block_has_flag(block, BLOCK_FULLY_TAGGED))
791 pr_debug("Block fully tagged, skipping first pass\n");
792 else if (ENABLE_FIRST_PASS && likely(!should_recompile))
793 pc = lightrec_emulate_block(state, block, pc);
795 /* Then compile it using the profiled data */
796 err = lightrec_compile_block(state->cstate, block);
798 state->exit_flags = LIGHTREC_EXIT_NOMEM;
801 } else if (unlikely(block_has_flag(block, BLOCK_IS_DEAD))) {
803 * If the block is dead but has never been compiled,
804 * then its function pointer is NULL and we cannot
805 * execute the block. In that case, reap all the dead
806 * blocks now, and in the next loop we will create a
809 lightrec_reaper_reap(state->reaper);
811 lightrec_recompiler_add(state->rec, block);
813 } while (state->exit_flags == LIGHTREC_EXIT_NORMAL
814 && state->current_cycle < state->target_cycle);
820 static void * lightrec_alloc_code(struct lightrec_state *state, size_t size)
824 if (ENABLE_THREADED_COMPILER)
825 lightrec_code_alloc_lock(state);
827 code = tlsf_malloc(state->tlsf, size);
829 if (ENABLE_THREADED_COMPILER)
830 lightrec_code_alloc_unlock(state);
835 static void lightrec_realloc_code(struct lightrec_state *state,
836 void *ptr, size_t size)
838 /* NOTE: 'size' MUST be smaller than the size specified during
841 if (ENABLE_THREADED_COMPILER)
842 lightrec_code_alloc_lock(state);
844 tlsf_realloc(state->tlsf, ptr, size);
846 if (ENABLE_THREADED_COMPILER)
847 lightrec_code_alloc_unlock(state);
850 static void lightrec_free_code(struct lightrec_state *state, void *ptr)
852 if (ENABLE_THREADED_COMPILER)
853 lightrec_code_alloc_lock(state);
855 tlsf_free(state->tlsf, ptr);
857 if (ENABLE_THREADED_COMPILER)
858 lightrec_code_alloc_unlock(state);
861 static char lightning_code_data[0x80000];
863 static void * lightrec_emit_code(struct lightrec_state *state,
864 const struct block *block,
865 jit_state_t *_jit, unsigned int *size)
867 bool has_code_buffer = ENABLE_CODE_BUFFER && state->tlsf;
868 jit_word_t code_size, new_code_size;
873 if (ENABLE_DISASSEMBLER)
874 jit_set_data(lightning_code_data, sizeof(lightning_code_data), 0);
876 jit_set_data(NULL, 0, JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
878 if (has_code_buffer) {
879 jit_get_code(&code_size);
882 /* Lightning's code size estimation routine is buggy on x86 and
883 * will return a value that's too small. */
887 code = lightrec_alloc_code(state, (size_t) code_size);
890 if (ENABLE_THREADED_COMPILER) {
891 /* If we're using the threaded compiler, return
892 * an allocation error here. The threaded
893 * compiler will then empty its job queue and
894 * request a code flush using the reaper. */
898 /* Remove outdated blocks, and try again */
899 lightrec_remove_outdated_blocks(state->block_cache, block);
901 pr_debug("Re-try to alloc %zu bytes...\n", code_size);
903 code = lightrec_alloc_code(state, code_size);
905 pr_err("Could not alloc even after removing old blocks!\n");
910 jit_set_code(code, code_size);
916 lightrec_free_code(state, code);
921 jit_get_code(&new_code_size);
922 lightrec_register(MEM_FOR_CODE, new_code_size);
924 if (has_code_buffer) {
925 lightrec_realloc_code(state, code, (size_t) new_code_size);
927 pr_debug("Creating code block at address 0x%" PRIxPTR ", "
928 "code size: %" PRIuPTR " new: %" PRIuPTR "\n",
929 (uintptr_t) code, code_size, new_code_size);
932 *size = (unsigned int) new_code_size;
934 if (state->ops.code_inv)
935 state->ops.code_inv(code, new_code_size);
940 static struct block * generate_wrapper(struct lightrec_state *state)
946 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
950 _jit = jit_new_state();
954 jit_name("RW wrapper");
955 jit_note(__FILE__, __LINE__);
957 /* Wrapper entry point */
961 /* Load pointer to C wrapper */
962 jit_addr(JIT_R1, JIT_R1, LIGHTREC_REG_STATE);
963 jit_ldxi(JIT_R1, JIT_R1, lightrec_offset(c_wrappers));
968 /* Save all temporaries on stack */
969 for (i = 0; i < NUM_TEMPS; i++) {
970 if (i + FIRST_TEMP != 1) {
971 jit_stxi(lightrec_offset(wrapper_regs[i]),
972 LIGHTREC_REG_STATE, JIT_R(i + FIRST_TEMP));
976 jit_getarg(JIT_R2, jit_arg());
979 jit_pushargr(LIGHTREC_REG_STATE);
980 jit_pushargr(JIT_R2);
982 jit_ldxi_ui(JIT_R2, LIGHTREC_REG_STATE, lightrec_offset(target_cycle));
984 /* state->current_cycle = state->target_cycle - delta; */
985 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, LIGHTREC_REG_CYCLE);
986 jit_stxi_i(lightrec_offset(current_cycle), LIGHTREC_REG_STATE, LIGHTREC_REG_CYCLE);
988 /* Call the wrapper function */
991 /* delta = state->target_cycle - state->current_cycle */;
992 jit_ldxi_ui(LIGHTREC_REG_CYCLE, LIGHTREC_REG_STATE, lightrec_offset(current_cycle));
993 jit_ldxi_ui(JIT_R1, LIGHTREC_REG_STATE, lightrec_offset(target_cycle));
994 jit_subr(LIGHTREC_REG_CYCLE, JIT_R1, LIGHTREC_REG_CYCLE);
996 /* Restore temporaries from stack */
997 for (i = 0; i < NUM_TEMPS; i++) {
998 if (i + FIRST_TEMP != 1) {
999 jit_ldxi(JIT_R(i + FIRST_TEMP), LIGHTREC_REG_STATE,
1000 lightrec_offset(wrapper_regs[i]));
1008 block->opcode_list = NULL;
1009 block->flags = BLOCK_NO_OPCODE_LIST;
1012 block->function = lightrec_emit_code(state, block, _jit,
1014 if (!block->function)
1017 state->c_wrapper = block->function;
1019 if (ENABLE_DISASSEMBLER) {
1020 pr_debug("Wrapper block:\n");
1028 jit_destroy_state();
1030 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1032 pr_err("Unable to compile wrapper: Out of memory\n");
1036 static u32 lightrec_memset(struct lightrec_state *state)
1038 u32 kunseg_pc = kunseg(state->regs.gpr[4]);
1040 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg_pc);
1041 u32 length = state->regs.gpr[5] * 4;
1044 pr_err("Unable to find memory map for memset target address "PC_FMT"\n",
1049 pr_debug("Calling host memset, "PC_FMT" (host address 0x%"PRIxPTR") for %u bytes\n",
1050 kunseg_pc, (uintptr_t)host, length);
1051 memset(host, 0, length);
1053 if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
1054 lightrec_invalidate_map(state, map, kunseg_pc, length);
1056 /* Rough estimation of the number of cycles consumed */
1057 return 8 + 5 * (length + 3 / 4);
1060 static u32 lightrec_check_load_delay(struct lightrec_state *state, u32 pc, u8 reg)
1062 struct block *block;
1063 union code first_op;
1065 first_op = lightrec_read_opcode(state, pc);
1067 if (likely(!opcode_reads_register(first_op, reg))) {
1068 state->regs.gpr[reg] = state->temp_reg;
1070 block = lightrec_get_block(state, pc);
1071 if (unlikely(!block)) {
1072 pr_err("Unable to get block at "PC_FMT"\n", pc);
1073 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
1076 pc = lightrec_handle_load_delay(state, block, pc, reg);
1083 static void update_cycle_counter_before_c(jit_state_t *_jit)
1085 /* update state->current_cycle */
1086 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE, lightrec_offset(target_cycle));
1087 jit_subr(JIT_R1, JIT_R2, LIGHTREC_REG_CYCLE);
1088 jit_stxi_i(lightrec_offset(current_cycle), LIGHTREC_REG_STATE, JIT_R1);
1091 static void update_cycle_counter_after_c(jit_state_t *_jit)
1093 /* Recalc the delta */
1094 jit_ldxi_i(JIT_R1, LIGHTREC_REG_STATE, lightrec_offset(current_cycle));
1095 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE, lightrec_offset(target_cycle));
1096 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, JIT_R1);
1099 static void sync_next_pc(jit_state_t *_jit)
1101 if (lightrec_store_next_pc()) {
1102 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE,
1103 lightrec_offset(next_pc));
1107 static struct block * generate_dispatcher(struct lightrec_state *state)
1109 struct block *block;
1111 jit_node_t *to_end, *loop, *loop2,
1112 *addr, *addr2, *addr3, *addr4, *addr5;
1116 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
1120 _jit = jit_new_state();
1122 goto err_free_block;
1124 jit_name("dispatcher");
1125 jit_note(__FILE__, __LINE__);
1130 jit_getarg(LIGHTREC_REG_STATE, jit_arg());
1131 jit_getarg(JIT_V0, jit_arg());
1132 jit_getarg(JIT_V1, jit_arg());
1133 jit_getarg_i(LIGHTREC_REG_CYCLE, jit_arg());
1135 /* Force all callee-saved registers to be pushed on the stack */
1136 for (i = 0; i < NUM_REGS; i++)
1137 jit_movr(JIT_V(i + FIRST_REG), JIT_V(i + FIRST_REG));
1141 /* Call the block's code */
1144 /* The block will jump here, with the number of cycles remaining in
1145 * LIGHTREC_REG_CYCLE */
1146 addr2 = jit_indirect();
1150 loop2 = jit_label();
1152 /* Jump to end if state->target_cycle < state->current_cycle */
1153 to_end = jit_blei(LIGHTREC_REG_CYCLE, 0);
1155 /* Convert next PC to KUNSEG and avoid mirrors */
1156 jit_andi(JIT_V1, JIT_V0, RAM_SIZE - 1);
1157 jit_andi(JIT_R2, JIT_V0, BIOS_SIZE - 1);
1158 jit_andi(JIT_R1, JIT_V0, BIT(28));
1159 jit_addi(JIT_R2, JIT_R2, RAM_SIZE);
1160 jit_movnr(JIT_V1, JIT_R2, JIT_R1);
1162 /* If possible, use the code LUT */
1163 if (!lut_is_32bit(state))
1164 jit_lshi(JIT_V1, JIT_V1, 1);
1165 jit_add_state(JIT_V1, JIT_V1);
1167 offset = lightrec_offset(code_lut);
1168 if (lut_is_32bit(state))
1169 jit_ldxi_ui(JIT_V1, JIT_V1, offset);
1171 jit_ldxi(JIT_V1, JIT_V1, offset);
1173 /* Store back the current PC to the lightrec_state structure */
1174 jit_stxi_i(lightrec_offset(curr_pc), LIGHTREC_REG_STATE, JIT_V0);
1176 /* If we get non-NULL, loop */
1177 jit_patch_at(jit_bnei(JIT_V1, 0), loop);
1179 /* The code LUT will be set to this address when the block at the target
1180 * PC has been preprocessed but not yet compiled by the threaded
1182 addr = jit_indirect();
1184 /* Slow path: call C function get_next_block_func() */
1186 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1187 /* We may call the interpreter - update state->current_cycle */
1188 update_cycle_counter_before_c(_jit);
1192 jit_pushargr(LIGHTREC_REG_STATE);
1193 jit_pushargr(JIT_V0);
1195 /* Save the cycles register if needed */
1196 if (!(ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES))
1197 jit_movr(JIT_V0, LIGHTREC_REG_CYCLE);
1199 /* Get the next block */
1200 jit_finishi(&get_next_block_func);
1203 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1204 /* The interpreter may have updated state->current_cycle and
1205 * state->target_cycle - recalc the delta */
1206 update_cycle_counter_after_c(_jit);
1208 jit_movr(LIGHTREC_REG_CYCLE, JIT_V0);
1211 /* Reset JIT_V0 to the next PC */
1212 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE, lightrec_offset(curr_pc));
1214 /* If we get non-NULL, loop */
1215 jit_patch_at(jit_bnei(JIT_V1, 0), loop);
1217 /* When exiting, the recompiled code will jump to that address */
1218 jit_note(__FILE__, __LINE__);
1221 /* Store back the current PC to the lightrec_state structure */
1222 jit_stxi_i(lightrec_offset(curr_pc), LIGHTREC_REG_STATE, JIT_V0);
1224 jit_retr(LIGHTREC_REG_CYCLE);
1226 if (OPT_REPLACE_MEMSET) {
1227 /* Blocks will jump here when they need to call
1228 * lightrec_memset() */
1229 addr3 = jit_indirect();
1231 jit_movr(JIT_V1, LIGHTREC_REG_CYCLE);
1234 jit_pushargr(LIGHTREC_REG_STATE);
1236 jit_finishi(lightrec_memset);
1237 jit_retval(LIGHTREC_REG_CYCLE);
1239 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE, lightrec_offset(regs.gpr[31]));
1241 jit_subr(LIGHTREC_REG_CYCLE, JIT_V1, LIGHTREC_REG_CYCLE);
1243 jit_patch_at(jit_b(), loop2);
1246 if (OPT_DETECT_IMPOSSIBLE_BRANCHES) {
1247 /* Blocks will jump here when they reach a branch that should
1248 * be executed with the interpreter, passing the branch's PC
1249 * in JIT_V0 and the address of the block in JIT_V1. */
1250 addr4 = jit_indirect();
1253 update_cycle_counter_before_c(_jit);
1256 jit_pushargr(LIGHTREC_REG_STATE);
1257 jit_pushargr(JIT_V1);
1258 jit_pushargr(JIT_V0);
1259 jit_finishi(lightrec_emulate_block);
1263 update_cycle_counter_after_c(_jit);
1265 jit_patch_at(jit_b(), loop2);
1269 if (OPT_HANDLE_LOAD_DELAYS) {
1270 /* Blocks will jump here when they reach a branch with a load
1271 * opcode in its delay slot. The delay slot has already been
1272 * executed; the load value is in (state->temp_reg), and the
1273 * register number is in JIT_V1.
1274 * Jump to a C function which will evaluate the branch target's
1275 * first opcode, to make sure that it does not read the register
1276 * in question; and if it does, handle it accordingly. */
1277 addr5 = jit_indirect();
1280 update_cycle_counter_before_c(_jit);
1283 jit_pushargr(LIGHTREC_REG_STATE);
1284 jit_pushargr(JIT_V0);
1285 jit_pushargr(JIT_V1);
1286 jit_finishi(lightrec_check_load_delay);
1290 update_cycle_counter_after_c(_jit);
1292 jit_patch_at(jit_b(), loop2);
1298 block->opcode_list = NULL;
1299 block->flags = BLOCK_NO_OPCODE_LIST;
1302 block->function = lightrec_emit_code(state, block, _jit,
1304 if (!block->function)
1307 state->eob_wrapper_func = jit_address(addr2);
1308 if (OPT_DETECT_IMPOSSIBLE_BRANCHES)
1309 state->interpreter_func = jit_address(addr4);
1310 if (OPT_HANDLE_LOAD_DELAYS)
1311 state->ds_check_func = jit_address(addr5);
1312 if (OPT_REPLACE_MEMSET)
1313 state->memset_func = jit_address(addr3);
1314 state->get_next_block = jit_address(addr);
1316 if (ENABLE_DISASSEMBLER) {
1317 pr_debug("Dispatcher block:\n");
1326 jit_destroy_state();
1328 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1330 pr_err("Unable to compile dispatcher: Out of memory\n");
1334 union code lightrec_read_opcode(struct lightrec_state *state, u32 pc)
1338 lightrec_get_map(state, &host, kunseg(pc));
1340 const u32 *code = (u32 *)host;
1341 return (union code) LE32TOH(*code);
1344 unsigned int lightrec_cycles_of_opcode(const struct lightrec_state *state,
1347 return state->cycles_per_op;
1350 void lightrec_free_opcode_list(struct lightrec_state *state, struct opcode *ops)
1352 struct opcode_list *list = container_of(ops, struct opcode_list, ops);
1354 lightrec_free(state, MEM_FOR_IR,
1355 sizeof(*list) + list->nb_ops * sizeof(struct opcode),
1359 static unsigned int lightrec_get_mips_block_len(const u32 *src)
1364 for (i = 1; ; i++) {
1365 c.opcode = LE32TOH(*src++);
1370 if (is_unconditional_jump(c))
1375 static struct opcode * lightrec_disassemble(struct lightrec_state *state,
1376 const u32 *src, unsigned int *len)
1378 struct opcode_list *list;
1379 unsigned int i, length;
1381 length = lightrec_get_mips_block_len(src);
1383 list = lightrec_malloc(state, MEM_FOR_IR,
1384 sizeof(*list) + sizeof(struct opcode) * length);
1386 pr_err("Unable to allocate memory\n");
1390 list->nb_ops = (u16) length;
1392 for (i = 0; i < length; i++) {
1393 list->ops[i].opcode = LE32TOH(src[i]);
1394 list->ops[i].flags = 0;
1397 *len = length * sizeof(u32);
1402 static struct block * lightrec_precompile_block(struct lightrec_state *state,
1405 struct opcode *list;
1406 struct block *block;
1408 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg(pc));
1409 const u32 *code = (u32 *) host;
1410 unsigned int length;
1417 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
1419 pr_err("Unable to recompile block: Out of memory\n");
1423 list = lightrec_disassemble(state, code, &length);
1425 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1431 block->function = NULL;
1432 block->opcode_list = list;
1436 block->code_size = 0;
1437 block->precompile_date = state->current_cycle;
1438 block->nb_ops = length / sizeof(u32);
1440 lightrec_optimize(state, block);
1442 length = block->nb_ops * sizeof(u32);
1444 lightrec_register(MEM_FOR_MIPS_CODE, length);
1446 if (ENABLE_DISASSEMBLER) {
1447 pr_debug("Disassembled block at "PC_FMT"\n", block->pc);
1448 lightrec_print_disassembly(block, code);
1451 pr_debug("Block size: %hu opcodes\n", block->nb_ops);
1453 fully_tagged = lightrec_block_is_fully_tagged(block);
1455 block_flags |= BLOCK_FULLY_TAGGED;
1458 block_set_flags(block, block_flags);
1460 block->hash = lightrec_calculate_block_hash(block);
1462 if (OPT_REPLACE_MEMSET && block_has_flag(block, BLOCK_IS_MEMSET))
1463 addr = state->memset_func;
1465 addr = state->get_next_block;
1466 lut_write(state, lut_offset(pc), addr);
1468 pr_debug("Blocks created: %u\n", ++state->nb_precompile);
1473 static bool lightrec_block_is_fully_tagged(const struct block *block)
1475 const struct opcode *op;
1478 for (i = 0; i < block->nb_ops; i++) {
1479 op = &block->opcode_list[i];
1481 /* If we have one branch that must be emulated, we cannot trash
1482 * the opcode list. */
1483 if (should_emulate(op))
1486 /* Check all loads/stores of the opcode list and mark the
1487 * block as fully compiled if they all have been tagged. */
1488 switch (op->c.i.op) {
1505 if (!LIGHTREC_FLAGS_GET_IO_MODE(op->flags))
1516 static void lightrec_reap_block(struct lightrec_state *state, void *data)
1518 struct block *block = data;
1520 pr_debug("Reap dead block at "PC_FMT"\n", block->pc);
1521 lightrec_unregister_block(state->block_cache, block);
1522 lightrec_free_block(state, block);
1525 static void lightrec_reap_jit(struct lightrec_state *state, void *data)
1527 _jit_destroy_state(data);
1530 static void lightrec_free_function(struct lightrec_state *state, void *fn)
1532 if (ENABLE_CODE_BUFFER && state->tlsf) {
1533 pr_debug("Freeing code block at 0x%" PRIxPTR "\n", (uintptr_t) fn);
1534 lightrec_free_code(state, fn);
1538 static void lightrec_reap_function(struct lightrec_state *state, void *data)
1540 lightrec_free_function(state, data);
1543 static void lightrec_reap_opcode_list(struct lightrec_state *state, void *data)
1545 lightrec_free_opcode_list(state, data);
1548 int lightrec_compile_block(struct lightrec_cstate *cstate,
1549 struct block *block)
1551 struct block *dead_blocks[ARRAY_SIZE(cstate->targets)];
1552 u32 was_dead[ARRAY_SIZE(cstate->targets) / 8];
1553 struct lightrec_state *state = cstate->state;
1554 struct lightrec_branch_target *target;
1555 bool fully_tagged = false;
1556 struct block *block2;
1558 jit_state_t *_jit, *oldjit;
1559 jit_node_t *start_of_block;
1560 bool skip_next = false;
1561 void *old_fn, *new_fn;
1562 size_t old_code_size;
1567 fully_tagged = lightrec_block_is_fully_tagged(block);
1569 block_set_flags(block, BLOCK_FULLY_TAGGED);
1571 _jit = jit_new_state();
1575 oldjit = block->_jit;
1576 old_fn = block->function;
1577 old_code_size = block->code_size;
1580 lightrec_regcache_reset(cstate->reg_cache);
1582 if (OPT_PRELOAD_PC && (block->flags & BLOCK_PRELOAD_PC))
1583 lightrec_preload_pc(cstate->reg_cache, _jit);
1586 cstate->nb_local_branches = 0;
1587 cstate->nb_targets = 0;
1588 cstate->no_load_delay = false;
1593 start_of_block = jit_label();
1595 for (i = 0; i < block->nb_ops; i++) {
1596 elm = &block->opcode_list[i];
1603 if (should_emulate(elm)) {
1604 pr_debug("Branch at offset 0x%x will be emulated\n",
1607 lightrec_emit_jump_to_interpreter(cstate, block, i);
1608 skip_next = !op_flag_no_ds(elm->flags);
1610 lightrec_rec_opcode(cstate, block, i);
1611 skip_next = !op_flag_no_ds(elm->flags) && has_delay_slot(elm->c);
1613 /* FIXME: GNU Lightning on Windows seems to use our
1614 * mapped registers as temporaries. Until the actual bug
1615 * is found and fixed, unconditionally mark our
1616 * registers as live here. */
1617 lightrec_regcache_mark_live(cstate->reg_cache, _jit);
1621 cstate->cycles += lightrec_cycles_of_opcode(state, elm->c);
1624 for (i = 0; i < cstate->nb_local_branches; i++) {
1625 struct lightrec_branch *branch = &cstate->local_branches[i];
1627 pr_debug("Patch local branch to offset 0x%x\n",
1628 branch->target << 2);
1630 if (branch->target == 0) {
1631 jit_patch_at(branch->branch, start_of_block);
1635 for (j = 0; j < cstate->nb_targets; j++) {
1636 if (cstate->targets[j].offset == branch->target) {
1637 jit_patch_at(branch->branch,
1638 cstate->targets[j].label);
1643 if (j == cstate->nb_targets)
1644 pr_err("Unable to find branch target\n");
1650 new_fn = lightrec_emit_code(state, block, _jit, &block->code_size);
1652 if (!ENABLE_THREADED_COMPILER)
1653 pr_err("Unable to compile block!\n");
1654 block->_jit = oldjit;
1656 _jit_destroy_state(_jit);
1660 /* Pause the reaper, because lightrec_reset_lut_offset() may try to set
1661 * the old block->function pointer to the code LUT. */
1662 if (ENABLE_THREADED_COMPILER)
1663 lightrec_reaper_pause(state->reaper);
1665 block->function = new_fn;
1666 block_clear_flags(block, BLOCK_SHOULD_RECOMPILE);
1668 /* Add compiled function to the LUT */
1669 lut_write(state, lut_offset(block->pc), block->function);
1671 /* Detect old blocks that have been covered by the new one */
1672 for (i = 0; ENABLE_THREADED_COMPILER && i < cstate->nb_targets; i++) {
1673 target = &cstate->targets[i];
1675 if (!target->offset)
1678 offset = block->pc + target->offset * sizeof(u32);
1680 block2 = lightrec_find_block(state->block_cache, offset);
1682 /* No need to check if block2 is compilable - it must
1683 * be, otherwise block wouldn't be compilable either */
1685 /* Set the "block dead" flag to prevent the dynarec from
1686 * recompiling this block */
1687 old_flags = block_set_flags(block2, BLOCK_IS_DEAD);
1689 if (old_flags & BLOCK_IS_DEAD)
1690 was_dead[i / 32] |= BIT(i % 32);
1692 was_dead[i / 32] &= ~BIT(i % 32);
1695 dead_blocks[i] = block2;
1697 /* If block2 was pending for compilation, cancel it.
1698 * If it's being compiled right now, wait until it finishes. */
1700 lightrec_recompiler_remove(state->rec, block2);
1703 for (i = 0; i < cstate->nb_targets; i++) {
1704 target = &cstate->targets[i];
1706 if (!target->offset)
1709 /* We know from now on that block2 (if present) isn't going to
1710 * be compiled. We can override the LUT entry with our new
1711 * block's entry point. */
1712 offset = lut_offset(block->pc) + target->offset;
1713 lut_write(state, offset, jit_address(target->label));
1715 if (ENABLE_THREADED_COMPILER) {
1716 block2 = dead_blocks[i];
1718 offset = block->pc + target->offset * sizeof(u32);
1719 block2 = lightrec_find_block(state->block_cache, offset);
1722 pr_debug("Reap block "X32_FMT" as it's covered by block "
1723 X32_FMT"\n", block2->pc, block->pc);
1725 /* Finally, reap the block. */
1726 if (!ENABLE_THREADED_COMPILER) {
1727 lightrec_unregister_block(state->block_cache, block2);
1728 lightrec_free_block(state, block2);
1729 } else if (!(was_dead[i / 32] & BIT(i % 32))) {
1730 lightrec_reaper_add(state->reaper,
1731 lightrec_reap_block,
1737 if (ENABLE_THREADED_COMPILER)
1738 lightrec_reaper_continue(state->reaper);
1740 if (ENABLE_DISASSEMBLER) {
1741 pr_debug("Compiling block at "PC_FMT"\n", block->pc);
1748 old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
1750 if (fully_tagged && !(old_flags & BLOCK_NO_OPCODE_LIST)) {
1751 pr_debug("Block "PC_FMT" is fully tagged"
1752 " - free opcode list\n", block->pc);
1754 if (ENABLE_THREADED_COMPILER) {
1755 lightrec_reaper_add(state->reaper,
1756 lightrec_reap_opcode_list,
1757 block->opcode_list);
1759 lightrec_free_opcode_list(state, block->opcode_list);
1764 pr_debug("Block "X32_FMT" recompiled, reaping old jit context.\n",
1767 if (ENABLE_THREADED_COMPILER) {
1768 lightrec_reaper_add(state->reaper,
1769 lightrec_reap_jit, oldjit);
1770 lightrec_reaper_add(state->reaper,
1771 lightrec_reap_function, old_fn);
1773 _jit_destroy_state(oldjit);
1774 lightrec_free_function(state, old_fn);
1777 lightrec_unregister(MEM_FOR_CODE, old_code_size);
1780 pr_debug("Blocks compiled: %u\n", ++state->nb_compile);
1785 static void lightrec_print_info(struct lightrec_state *state)
1787 if ((state->current_cycle & ~0xfffffff) != state->old_cycle_counter) {
1788 pr_info("Lightrec RAM usage: IR %u KiB, CODE %u KiB, "
1789 "MIPS %u KiB, TOTAL %u KiB, avg. IPI %f\n",
1790 lightrec_get_mem_usage(MEM_FOR_IR) / 1024,
1791 lightrec_get_mem_usage(MEM_FOR_CODE) / 1024,
1792 lightrec_get_mem_usage(MEM_FOR_MIPS_CODE) / 1024,
1793 lightrec_get_total_mem_usage() / 1024,
1794 lightrec_get_average_ipi());
1795 state->old_cycle_counter = state->current_cycle & ~0xfffffff;
1799 u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
1801 s32 (*func)(struct lightrec_state *, u32, void *, s32) = (void *)state->dispatcher->function;
1805 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1807 /* Handle the cycle counter overflowing */
1808 if (unlikely(target_cycle < state->current_cycle))
1809 target_cycle = UINT_MAX;
1811 state->target_cycle = target_cycle;
1812 state->curr_pc = pc;
1814 block_trace = get_next_block_func(state, pc);
1816 cycles_delta = state->target_cycle - state->current_cycle;
1818 cycles_delta = (*func)(state, state->curr_pc,
1819 block_trace, cycles_delta);
1821 state->current_cycle = state->target_cycle - cycles_delta;
1824 if (ENABLE_THREADED_COMPILER)
1825 lightrec_reaper_reap(state->reaper);
1827 if (LOG_LEVEL >= INFO_L)
1828 lightrec_print_info(state);
1830 return state->curr_pc;
1833 u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc,
1836 struct block *block;
1838 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1839 state->target_cycle = target_cycle;
1842 block = lightrec_get_block(state, pc);
1846 pc = lightrec_emulate_block(state, block, pc);
1848 if (ENABLE_THREADED_COMPILER)
1849 lightrec_reaper_reap(state->reaper);
1850 } while (state->current_cycle < state->target_cycle);
1852 if (LOG_LEVEL >= INFO_L)
1853 lightrec_print_info(state);
1858 void lightrec_free_block(struct lightrec_state *state, struct block *block)
1862 lightrec_unregister(MEM_FOR_MIPS_CODE, block->nb_ops * sizeof(u32));
1863 old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
1865 if (!(old_flags & BLOCK_NO_OPCODE_LIST))
1866 lightrec_free_opcode_list(state, block->opcode_list);
1868 _jit_destroy_state(block->_jit);
1869 if (block->function) {
1870 lightrec_free_function(state, block->function);
1871 lightrec_unregister(MEM_FOR_CODE, block->code_size);
1873 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1876 struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state)
1878 struct lightrec_cstate *cstate;
1880 cstate = lightrec_malloc(state, MEM_FOR_LIGHTREC, sizeof(*cstate));
1884 cstate->reg_cache = lightrec_regcache_init(state);
1885 if (!cstate->reg_cache) {
1886 lightrec_free(state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
1890 cstate->state = state;
1895 void lightrec_free_cstate(struct lightrec_cstate *cstate)
1897 lightrec_free_regcache(cstate->reg_cache);
1898 lightrec_free(cstate->state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
1901 struct lightrec_state * lightrec_init(char *argv0,
1902 const struct lightrec_mem_map *maps,
1904 const struct lightrec_ops *ops)
1906 const struct lightrec_mem_map *codebuf_map = &maps[PSX_MAP_CODE_BUFFER];
1907 const struct lightrec_mem_map *map;
1908 struct lightrec_state *state;
1911 bool with_32bit_lut = false;
1914 /* Sanity-check ops */
1915 if (!ops || !ops->cop2_op || !ops->enable_ram) {
1916 pr_err("Missing callbacks in lightrec_ops structure\n");
1920 if (ops->cop2_notify)
1921 pr_debug("Optional cop2_notify callback in lightrec_ops\n");
1923 pr_debug("No optional cop2_notify callback in lightrec_ops\n");
1925 if (ENABLE_CODE_BUFFER && nb > PSX_MAP_CODE_BUFFER
1926 && codebuf_map->address) {
1927 tlsf = tlsf_create_with_pool(codebuf_map->address,
1928 codebuf_map->length);
1930 pr_err("Unable to initialize code buffer\n");
1934 if (__WORDSIZE == 64) {
1935 addr = (uintptr_t) codebuf_map->address + codebuf_map->length - 1;
1936 with_32bit_lut = addr == (u32) addr;
1941 lut_size = CODE_LUT_SIZE * 4;
1943 lut_size = CODE_LUT_SIZE * sizeof(void *);
1945 init_jit_with_debug(argv0, stdout);
1947 state = calloc(1, sizeof(*state) + lut_size);
1949 goto err_finish_jit;
1951 lightrec_register(MEM_FOR_LIGHTREC, sizeof(*state) + lut_size);
1954 state->with_32bit_lut = with_32bit_lut;
1955 state->in_delay_slot_n = 0xff;
1956 state->cycles_per_op = 2;
1958 state->block_cache = lightrec_blockcache_init(state);
1959 if (!state->block_cache)
1960 goto err_free_state;
1962 if (ENABLE_THREADED_COMPILER) {
1963 state->rec = lightrec_recompiler_init(state);
1965 goto err_free_block_cache;
1967 state->reaper = lightrec_reaper_init(state);
1969 goto err_free_recompiler;
1971 state->cstate = lightrec_create_cstate(state);
1973 goto err_free_block_cache;
1976 state->nb_maps = nb;
1979 memcpy(&state->ops, ops, sizeof(*ops));
1981 state->dispatcher = generate_dispatcher(state);
1982 if (!state->dispatcher)
1983 goto err_free_reaper;
1985 state->c_wrapper_block = generate_wrapper(state);
1986 if (!state->c_wrapper_block)
1987 goto err_free_dispatcher;
1989 state->c_wrappers[C_WRAPPER_RW] = lightrec_rw_cb;
1990 state->c_wrappers[C_WRAPPER_RW_GENERIC] = lightrec_rw_generic_cb;
1991 state->c_wrappers[C_WRAPPER_MFC] = lightrec_mfc_cb;
1992 state->c_wrappers[C_WRAPPER_MTC] = lightrec_mtc_cb;
1993 state->c_wrappers[C_WRAPPER_CP] = lightrec_cp_cb;
1995 map = &maps[PSX_MAP_BIOS];
1996 state->offset_bios = (uintptr_t)map->address - map->pc;
1998 map = &maps[PSX_MAP_SCRATCH_PAD];
1999 state->offset_scratch = (uintptr_t)map->address - map->pc;
2001 map = &maps[PSX_MAP_HW_REGISTERS];
2002 state->offset_io = (uintptr_t)map->address - map->pc;
2004 map = &maps[PSX_MAP_KERNEL_USER_RAM];
2005 state->offset_ram = (uintptr_t)map->address - map->pc;
2007 if (maps[PSX_MAP_MIRROR1].address == map->address + 0x200000 &&
2008 maps[PSX_MAP_MIRROR2].address == map->address + 0x400000 &&
2009 maps[PSX_MAP_MIRROR3].address == map->address + 0x600000)
2010 state->mirrors_mapped = true;
2012 if (state->offset_bios == 0 &&
2013 state->offset_scratch == 0 &&
2014 state->offset_ram == 0 &&
2015 state->offset_io == 0 &&
2016 state->mirrors_mapped) {
2017 pr_info("Memory map is perfect. Emitted code will be best.\n");
2019 pr_info("Memory map is sub-par. Emitted code will be slow.\n");
2022 if (state->with_32bit_lut)
2023 pr_info("Using 32-bit LUT\n");
2027 err_free_dispatcher:
2028 lightrec_free_block(state, state->dispatcher);
2030 if (ENABLE_THREADED_COMPILER)
2031 lightrec_reaper_destroy(state->reaper);
2032 err_free_recompiler:
2033 if (ENABLE_THREADED_COMPILER)
2034 lightrec_free_recompiler(state->rec);
2036 lightrec_free_cstate(state->cstate);
2037 err_free_block_cache:
2038 lightrec_free_block_cache(state->block_cache);
2040 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
2041 lut_elm_size(state) * CODE_LUT_SIZE);
2045 if (ENABLE_CODE_BUFFER && tlsf)
2050 void lightrec_destroy(struct lightrec_state *state)
2052 /* Force a print info on destroy*/
2053 state->current_cycle = ~state->current_cycle;
2054 lightrec_print_info(state);
2056 lightrec_free_block_cache(state->block_cache);
2057 lightrec_free_block(state, state->dispatcher);
2058 lightrec_free_block(state, state->c_wrapper_block);
2060 if (ENABLE_THREADED_COMPILER) {
2061 lightrec_free_recompiler(state->rec);
2062 lightrec_reaper_destroy(state->reaper);
2064 lightrec_free_cstate(state->cstate);
2068 if (ENABLE_CODE_BUFFER && state->tlsf)
2069 tlsf_destroy(state->tlsf);
2071 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
2072 lut_elm_size(state) * CODE_LUT_SIZE);
2076 void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len)
2078 u32 kaddr = kunseg(addr & ~0x3);
2079 enum psx_map idx = lightrec_get_map_idx(state, kaddr);
2082 case PSX_MAP_MIRROR1:
2083 case PSX_MAP_MIRROR2:
2084 case PSX_MAP_MIRROR3:
2085 /* Handle mirrors */
2086 kaddr &= RAM_SIZE - 1;
2088 case PSX_MAP_KERNEL_USER_RAM:
2094 memset(lut_address(state, lut_offset(kaddr)), 0,
2095 ((len + 3) / 4) * lut_elm_size(state));
2098 void lightrec_invalidate_all(struct lightrec_state *state)
2100 memset(state->code_lut, 0, lut_elm_size(state) * CODE_LUT_SIZE);
2103 void lightrec_set_unsafe_opt_flags(struct lightrec_state *state, u32 flags)
2105 if ((flags ^ state->opt_flags) & LIGHTREC_OPT_INV_DMA_ONLY)
2106 lightrec_invalidate_all(state);
2108 state->opt_flags = flags;
2111 void lightrec_set_exit_flags(struct lightrec_state *state, u32 flags)
2113 if (flags != LIGHTREC_EXIT_NORMAL) {
2114 state->exit_flags |= flags;
2115 state->target_cycle = state->current_cycle;
2119 u32 lightrec_exit_flags(struct lightrec_state *state)
2121 return state->exit_flags;
2124 u32 lightrec_current_cycle_count(const struct lightrec_state *state)
2126 return state->current_cycle;
2129 void lightrec_reset_cycle_count(struct lightrec_state *state, u32 cycles)
2131 state->current_cycle = cycles;
2133 if (state->target_cycle < cycles)
2134 state->target_cycle = cycles;
2137 void lightrec_set_target_cycle_count(struct lightrec_state *state, u32 cycles)
2139 if (state->exit_flags == LIGHTREC_EXIT_NORMAL) {
2140 if (cycles < state->current_cycle)
2141 cycles = state->current_cycle;
2143 state->target_cycle = cycles;
2147 struct lightrec_registers * lightrec_get_registers(struct lightrec_state *state)
2149 return &state->regs;
2152 void lightrec_set_cycles_per_opcode(struct lightrec_state *state, u32 cycles)
2154 if (state->cycles_per_op == cycles)
2157 state->cycles_per_op = cycles;
2159 if (ENABLE_THREADED_COMPILER) {
2160 lightrec_recompiler_pause(state->rec);
2161 lightrec_reaper_reap(state->reaper);
2164 lightrec_invalidate_all(state);
2165 lightrec_free_all_blocks(state->block_cache);
2167 if (ENABLE_THREADED_COMPILER)
2168 lightrec_recompiler_unpause(state->rec);