git subrepo pull --force deps/lightrec
[pcsx_rearmed.git] / deps / lightrec / lightrec.c
CommitLineData
98fa08a5 1// SPDX-License-Identifier: LGPL-2.1-or-later
d16005f8 2/*
98fa08a5 3 * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net>
d16005f8
PC
4 */
5
6#include "blockcache.h"
d16005f8
PC
7#include "debug.h"
8#include "disassembler.h"
9#include "emitter.h"
10#include "interpreter.h"
98fa08a5
PC
11#include "lightrec-config.h"
12#include "lightning-wrapper.h"
d16005f8
PC
13#include "lightrec.h"
14#include "memmanager.h"
a59e5536 15#include "reaper.h"
d16005f8
PC
16#include "recompiler.h"
17#include "regcache.h"
18#include "optimizer.h"
02487de7 19#include "tlsf/tlsf.h"
d16005f8
PC
20
21#include <errno.h>
98fa08a5 22#include <inttypes.h>
d16005f8
PC
23#include <limits.h>
24#if ENABLE_THREADED_COMPILER
25#include <stdatomic.h>
26#endif
27#include <stdbool.h>
28#include <stddef.h>
29#include <string.h>
d16005f8 30
d16005f8
PC
31static struct block * lightrec_precompile_block(struct lightrec_state *state,
32 u32 pc);
98fa08a5
PC
33static bool lightrec_block_is_fully_tagged(const struct block *block);
34
35static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data);
36static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg);
d16005f8 37
a59e5536 38static void lightrec_default_sb(struct lightrec_state *state, u32 opcode,
39 void *host, u32 addr, u8 data)
40{
41 *(u8 *)host = data;
42
43 if (!state->invalidate_from_dma_only)
44 lightrec_invalidate(state, addr, 1);
45}
46
47static void lightrec_default_sh(struct lightrec_state *state, u32 opcode,
48 void *host, u32 addr, u16 data)
49{
50 *(u16 *)host = HTOLE16(data);
51
52 if (!state->invalidate_from_dma_only)
53 lightrec_invalidate(state, addr, 2);
54}
55
56static void lightrec_default_sw(struct lightrec_state *state, u32 opcode,
57 void *host, u32 addr, u32 data)
58{
59 *(u32 *)host = HTOLE32(data);
60
61 if (!state->invalidate_from_dma_only)
62 lightrec_invalidate(state, addr, 4);
63}
64
65static u8 lightrec_default_lb(struct lightrec_state *state,
66 u32 opcode, void *host, u32 addr)
67{
68 return *(u8 *)host;
69}
70
71static u16 lightrec_default_lh(struct lightrec_state *state,
72 u32 opcode, void *host, u32 addr)
73{
74 return LE16TOH(*(u16 *)host);
75}
76
77static u32 lightrec_default_lw(struct lightrec_state *state,
78 u32 opcode, void *host, u32 addr)
79{
80 return LE32TOH(*(u32 *)host);
81}
82
83static 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,
90};
91
98fa08a5
PC
92static void __segfault_cb(struct lightrec_state *state, u32 addr,
93 const struct block *block)
d16005f8
PC
94{
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);
98fa08a5
PC
98 if (block)
99 pr_err("Was executing block PC 0x%08x\n", block->pc);
d16005f8
PC
100}
101
a59e5536 102static void lightrec_swl(struct lightrec_state *state,
103 const struct lightrec_mem_map_ops *ops,
104 u32 opcode, void *host, u32 addr, u32 data)
d16005f8 105{
a59e5536 106 unsigned int shift = addr & 0x3;
13b02197 107 unsigned int mask = shift < 3 ? GENMASK(31, (shift + 1) * 8) : 0;
a59e5536 108 u32 old_data;
109
110 /* Align to 32 bits */
111 addr &= ~3;
112 host = (void *)((uintptr_t)host & ~3);
113
114 old_data = ops->lw(state, opcode, host, addr);
115
116 data = (data >> ((3 - shift) * 8)) | (old_data & mask);
117
118 ops->sw(state, opcode, host, addr, data);
119}
120
121static void lightrec_swr(struct lightrec_state *state,
122 const struct lightrec_mem_map_ops *ops,
123 u32 opcode, void *host, u32 addr, u32 data)
124{
125 unsigned int shift = addr & 0x3;
126 unsigned int mask = (1 << (shift * 8)) - 1;
127 u32 old_data;
128
129 /* Align to 32 bits */
130 addr &= ~3;
131 host = (void *)((uintptr_t)host & ~3);
132
133 old_data = ops->lw(state, opcode, host, addr);
134
135 data = (data << (shift * 8)) | (old_data & mask);
136
137 ops->sw(state, opcode, host, addr, data);
138}
139
140static void lightrec_swc2(struct lightrec_state *state, union code op,
141 const struct lightrec_mem_map_ops *ops,
142 void *host, u32 addr)
143{
98fa08a5 144 u32 data = lightrec_mfc2(state, op.i.rt);
a59e5536 145
146 ops->sw(state, op.opcode, host, addr, data);
147}
148
149static u32 lightrec_lwl(struct lightrec_state *state,
150 const struct lightrec_mem_map_ops *ops,
151 u32 opcode, void *host, u32 addr, u32 data)
152{
153 unsigned int shift = addr & 0x3;
154 unsigned int mask = (1 << (24 - shift * 8)) - 1;
155 u32 old_data;
156
157 /* Align to 32 bits */
158 addr &= ~3;
159 host = (void *)((uintptr_t)host & ~3);
160
161 old_data = ops->lw(state, opcode, host, addr);
162
163 return (data & mask) | (old_data << (24 - shift * 8));
164}
165
166static u32 lightrec_lwr(struct lightrec_state *state,
167 const struct lightrec_mem_map_ops *ops,
168 u32 opcode, void *host, u32 addr, u32 data)
169{
170 unsigned int shift = addr & 0x3;
13b02197 171 unsigned int mask = shift ? GENMASK(31, 32 - shift * 8) : 0;
a59e5536 172 u32 old_data;
173
174 /* Align to 32 bits */
175 addr &= ~3;
176 host = (void *)((uintptr_t)host & ~3);
177
178 old_data = ops->lw(state, opcode, host, addr);
179
180 return (data & mask) | (old_data >> (shift * 8));
181}
182
183static void lightrec_lwc2(struct lightrec_state *state, union code op,
184 const struct lightrec_mem_map_ops *ops,
185 void *host, u32 addr)
186{
187 u32 data = ops->lw(state, op.opcode, host, addr);
188
98fa08a5 189 lightrec_mtc2(state, op.i.rt, data);
d16005f8
PC
190}
191
192static void lightrec_invalidate_map(struct lightrec_state *state,
98fa08a5 193 const struct lightrec_mem_map *map, u32 addr, u32 len)
d16005f8 194{
98fa08a5 195 if (map == &state->maps[PSX_MAP_KERNEL_USER_RAM]) {
02487de7
PC
196 memset(lut_address(state, lut_offset(addr)), 0,
197 ((len + 3) / 4) * lut_elm_size(state));
98fa08a5 198 }
d16005f8
PC
199}
200
02487de7
PC
201enum psx_map
202lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr)
d16005f8 203{
98fa08a5 204 const struct lightrec_mem_map *map;
d16005f8
PC
205 unsigned int i;
206
207 for (i = 0; i < state->nb_maps; i++) {
02487de7 208 map = &state->maps[i];
d16005f8 209
02487de7
PC
210 if (kaddr >= map->pc && kaddr < map->pc + map->length)
211 return (enum psx_map) i;
d16005f8
PC
212 }
213
02487de7
PC
214 return PSX_MAP_UNKNOWN;
215}
216
217const struct lightrec_mem_map *
218lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr)
219{
220 const struct lightrec_mem_map *map;
221 enum psx_map idx;
222 u32 addr;
223
224 idx = lightrec_get_map_idx(state, kaddr);
225 if (idx == PSX_MAP_UNKNOWN)
98fa08a5
PC
226 return NULL;
227
02487de7 228 map = &state->maps[idx];
98fa08a5
PC
229 addr = kaddr - map->pc;
230
231 while (map->mirror_of)
232 map = map->mirror_of;
233
234 if (host)
235 *host = map->address + addr;
236
237 return map;
d16005f8
PC
238}
239
240u32 lightrec_rw(struct lightrec_state *state, union code op,
03535202 241 u32 addr, u32 data, u32 *flags, struct block *block)
d16005f8
PC
242{
243 const struct lightrec_mem_map *map;
a59e5536 244 const struct lightrec_mem_map_ops *ops;
98fa08a5 245 u32 opcode = op.opcode;
a59e5536 246 void *host;
d16005f8
PC
247
248 addr += (s16) op.i.imm;
d16005f8 249
98fa08a5 250 map = lightrec_get_map(state, &host, kunseg(addr));
d16005f8 251 if (!map) {
98fa08a5 252 __segfault_cb(state, addr, block);
d16005f8
PC
253 return 0;
254 }
255
d16005f8 256
6ce0b00a 257 if (likely(!map->ops)) {
22eee2ac
PC
258 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
259 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
d16005f8 260
a59e5536 261 ops = &lightrec_default_ops;
6ce0b00a
PC
262 } else if (flags &&
263 LIGHTREC_FLAGS_GET_IO_MODE(*flags) == LIGHTREC_IO_DIRECT_HW) {
264 ops = &lightrec_default_ops;
265 } else {
266 if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
267 *flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
268
269 ops = map->ops;
a59e5536 270 }
d16005f8
PC
271
272 switch (op.i.op) {
273 case OP_SB:
a59e5536 274 ops->sb(state, opcode, host, addr, (u8) data);
d16005f8
PC
275 return 0;
276 case OP_SH:
a59e5536 277 ops->sh(state, opcode, host, addr, (u16) data);
d16005f8
PC
278 return 0;
279 case OP_SWL:
a59e5536 280 lightrec_swl(state, ops, opcode, host, addr, data);
d16005f8
PC
281 return 0;
282 case OP_SWR:
a59e5536 283 lightrec_swr(state, ops, opcode, host, addr, data);
d16005f8
PC
284 return 0;
285 case OP_SW:
a59e5536 286 ops->sw(state, opcode, host, addr, data);
d16005f8
PC
287 return 0;
288 case OP_SWC2:
a59e5536 289 lightrec_swc2(state, op, ops, host, addr);
d16005f8
PC
290 return 0;
291 case OP_LB:
a59e5536 292 return (s32) (s8) ops->lb(state, opcode, host, addr);
d16005f8 293 case OP_LBU:
a59e5536 294 return ops->lb(state, opcode, host, addr);
d16005f8 295 case OP_LH:
a59e5536 296 return (s32) (s16) ops->lh(state, opcode, host, addr);
d16005f8 297 case OP_LHU:
a59e5536 298 return ops->lh(state, opcode, host, addr);
d16005f8 299 case OP_LWC2:
a59e5536 300 lightrec_lwc2(state, op, ops, host, addr);
d16005f8 301 return 0;
a59e5536 302 case OP_LWL:
303 return lightrec_lwl(state, ops, opcode, host, addr, data);
304 case OP_LWR:
305 return lightrec_lwr(state, ops, opcode, host, addr, data);
d16005f8
PC
306 case OP_LW:
307 default:
a59e5536 308 return ops->lw(state, opcode, host, addr);
d16005f8
PC
309 }
310}
311
312static void lightrec_rw_helper(struct lightrec_state *state,
03535202 313 union code op, u32 *flags,
98fa08a5 314 struct block *block)
d16005f8 315{
98fa08a5
PC
316 u32 ret = lightrec_rw(state, op, state->regs.gpr[op.i.rs],
317 state->regs.gpr[op.i.rt], flags, block);
d16005f8
PC
318
319 switch (op.i.op) {
320 case OP_LB:
321 case OP_LBU:
322 case OP_LH:
323 case OP_LHU:
324 case OP_LWL:
325 case OP_LWR:
326 case OP_LW:
327 if (op.i.rt)
98fa08a5 328 state->regs.gpr[op.i.rt] = ret;
d8b04acd
PC
329 fallthrough;
330 default:
d16005f8
PC
331 break;
332 }
333}
334
03535202 335static void lightrec_rw_cb(struct lightrec_state *state, u32 arg)
d16005f8 336{
03535202 337 lightrec_rw_helper(state, (union code) arg, NULL, NULL);
d16005f8
PC
338}
339
03535202 340static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
d16005f8 341{
98fa08a5
PC
342 struct block *block;
343 struct opcode *op;
344 bool was_tagged;
345 u16 offset = (u16)arg;
ba3814c1 346 u16 old_flags;
98fa08a5
PC
347
348 block = lightrec_find_block_from_lut(state->block_cache,
349 arg >> 16, state->next_pc);
350 if (unlikely(!block)) {
351 pr_err("rw_generic: No block found in LUT for PC 0x%x offset 0x%x\n",
352 state->next_pc, offset);
ba3814c1 353 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
98fa08a5
PC
354 return;
355 }
356
357 op = &block->opcode_list[offset];
22eee2ac 358 was_tagged = LIGHTREC_FLAGS_GET_IO_MODE(op->flags);
d16005f8 359
98fa08a5 360 lightrec_rw_helper(state, op->c, &op->flags, block);
d16005f8
PC
361
362 if (!was_tagged) {
ba3814c1 363 old_flags = block_set_flags(block, BLOCK_SHOULD_RECOMPILE);
d16005f8 364
ba3814c1
PC
365 if (!(old_flags & BLOCK_SHOULD_RECOMPILE)) {
366 pr_debug("Opcode of block at PC 0x%08x has been tagged"
367 " - flag for recompilation\n", block->pc);
368
369 lut_write(state, lut_offset(block->pc), NULL);
370 }
d16005f8
PC
371 }
372}
373
98fa08a5 374static u32 clamp_s32(s32 val, s32 min, s32 max)
d16005f8 375{
98fa08a5
PC
376 return val < min ? min : val > max ? max : val;
377}
d16005f8 378
11357fef
PC
379static u16 load_u16(u32 *ptr)
380{
381 return ((struct u16x2 *) ptr)->l;
382}
383
384static void store_u16(u32 *ptr, u16 value)
385{
386 ((struct u16x2 *) ptr)->l = value;
387}
388
98fa08a5
PC
389static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg)
390{
391 s16 gteir1, gteir2, gteir3;
392
393 switch (reg) {
394 case 1:
395 case 3:
396 case 5:
397 case 8:
398 case 9:
399 case 10:
400 case 11:
11357fef 401 return (s32)(s16) load_u16(&state->regs.cp2d[reg]);
98fa08a5
PC
402 case 7:
403 case 16:
404 case 17:
405 case 18:
406 case 19:
11357fef 407 return load_u16(&state->regs.cp2d[reg]);
98fa08a5
PC
408 case 28:
409 case 29:
11357fef
PC
410 gteir1 = (s16) load_u16(&state->regs.cp2d[9]);
411 gteir2 = (s16) load_u16(&state->regs.cp2d[10]);
412 gteir3 = (s16) load_u16(&state->regs.cp2d[11]);
98fa08a5
PC
413
414 return clamp_s32(gteir1 >> 7, 0, 0x1f) << 0 |
415 clamp_s32(gteir2 >> 7, 0, 0x1f) << 5 |
416 clamp_s32(gteir3 >> 7, 0, 0x1f) << 10;
417 case 15:
418 reg = 14;
d8b04acd
PC
419 fallthrough;
420 default:
98fa08a5
PC
421 return state->regs.cp2d[reg];
422 }
423}
d16005f8 424
98fa08a5
PC
425u32 lightrec_mfc(struct lightrec_state *state, union code op)
426{
ba3814c1
PC
427 u32 val;
428
98fa08a5
PC
429 if (op.i.op == OP_CP0)
430 return state->regs.cp0[op.r.rd];
431 else if (op.r.rs == OP_CP2_BASIC_MFC2)
fdf33147
PC
432 val = lightrec_mfc2(state, op.r.rd);
433 else {
434 val = state->regs.cp2c[op.r.rd];
435
436 switch (op.r.rd) {
437 case 4:
438 case 12:
439 case 20:
440 case 26:
441 case 27:
442 case 29:
443 case 30:
444 val = (u32)(s16)val;
445 fallthrough;
446 default:
447 break;
448 }
449 }
ba3814c1 450
fdf33147
PC
451 if (state->ops.cop2_notify)
452 (*state->ops.cop2_notify)(state, op.opcode, val);
ba3814c1 453
fdf33147
PC
454 return val;
455}
456
457static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
458{
459 u32 rt = lightrec_mfc(state, op);
460
461 if (op.r.rt)
462 state->regs.gpr[op.r.rt] = rt;
d16005f8
PC
463}
464
98fa08a5 465static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
d16005f8 466{
fd58fa32 467 u32 status, oldstatus, cause;
98fa08a5
PC
468
469 switch (reg) {
470 case 1:
471 case 4:
472 case 8:
473 case 14:
474 case 15:
475 /* Those registers are read-only */
476 return;
fd58fa32 477 default:
98fa08a5
PC
478 break;
479 }
d16005f8 480
98fa08a5
PC
481 if (reg == 12) {
482 status = state->regs.cp0[12];
fd58fa32 483 oldstatus = status;
d16005f8 484
98fa08a5
PC
485 if (status & ~data & BIT(16)) {
486 state->ops.enable_ram(state, true);
487 lightrec_invalidate_all(state);
488 } else if (~status & data & BIT(16)) {
489 state->ops.enable_ram(state, false);
490 }
491 }
492
fd58fa32
PC
493 if (reg == 13) {
494 state->regs.cp0[13] &= ~0x300;
495 state->regs.cp0[13] |= data & 0x300;
496 } else {
497 state->regs.cp0[reg] = data;
498 }
98fa08a5
PC
499
500 if (reg == 12 || reg == 13) {
501 cause = state->regs.cp0[13];
502 status = state->regs.cp0[12];
503
fd58fa32 504 /* Handle software interrupts */
98fa08a5
PC
505 if (!!(status & cause & 0x300) & status)
506 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
fd58fa32
PC
507
508 /* Handle hardware interrupts */
509 if (reg == 12 && !(~status & 0x401) && (~oldstatus & 0x401))
510 lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
98fa08a5
PC
511 }
512}
513
514static u32 count_leading_bits(s32 data)
515{
98fa08a5
PC
516 u32 cnt = 33;
517
de742fa0
PC
518#ifdef __has_builtin
519#if __has_builtin(__builtin_clrsb)
520 return 1 + __builtin_clrsb(data);
521#endif
522#endif
523
98fa08a5
PC
524 data = (data ^ (data >> 31)) << 1;
525
526 do {
527 cnt -= 1;
528 data >>= 1;
529 } while (data);
530
531 return cnt;
98fa08a5
PC
532}
533
534static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data)
535{
536 switch (reg) {
537 case 15:
538 state->regs.cp2d[12] = state->regs.cp2d[13];
539 state->regs.cp2d[13] = state->regs.cp2d[14];
540 state->regs.cp2d[14] = data;
541 break;
542 case 28:
543 state->regs.cp2d[9] = (data << 7) & 0xf80;
544 state->regs.cp2d[10] = (data << 2) & 0xf80;
545 state->regs.cp2d[11] = (data >> 3) & 0xf80;
546 break;
547 case 31:
548 return;
549 case 30:
550 state->regs.cp2d[31] = count_leading_bits((s32) data);
d8b04acd
PC
551 fallthrough;
552 default:
98fa08a5
PC
553 state->regs.cp2d[reg] = data;
554 break;
555 }
556}
d16005f8 557
98fa08a5
PC
558static void lightrec_ctc2(struct lightrec_state *state, u8 reg, u32 data)
559{
560 switch (reg) {
561 case 4:
562 case 12:
563 case 20:
564 case 26:
565 case 27:
566 case 29:
567 case 30:
11357fef 568 store_u16(&state->regs.cp2c[reg], data);
98fa08a5
PC
569 break;
570 case 31:
571 data = (data & 0x7ffff000) | !!(data & 0x7f87e000) << 31;
d8b04acd
PC
572 fallthrough;
573 default:
11357fef 574 state->regs.cp2c[reg] = data;
98fa08a5
PC
575 break;
576 }
98fa08a5
PC
577}
578
579void lightrec_mtc(struct lightrec_state *state, union code op, u32 data)
580{
fdf33147 581 if (op.i.op == OP_CP0) {
98fa08a5 582 lightrec_mtc0(state, op.r.rd, data);
fdf33147
PC
583 } else {
584 if (op.r.rs == OP_CP2_BASIC_CTC2)
585 lightrec_ctc2(state, op.r.rd, data);
586 else
587 lightrec_mtc2(state, op.r.rd, data);
588
589 if (state->ops.cop2_notify)
590 (*state->ops.cop2_notify)(state, op.opcode, data);
591 }
d16005f8
PC
592}
593
03535202 594static void lightrec_mtc_cb(struct lightrec_state *state, u32 arg)
d16005f8 595{
03535202 596 union code op = (union code) arg;
22eee2ac 597
98fa08a5 598 lightrec_mtc(state, op, state->regs.gpr[op.r.rt]);
d16005f8
PC
599}
600
98fa08a5 601void lightrec_rfe(struct lightrec_state *state)
d16005f8
PC
602{
603 u32 status;
604
605 /* Read CP0 Status register (r12) */
98fa08a5 606 status = state->regs.cp0[12];
d16005f8
PC
607
608 /* Switch the bits */
609 status = ((status & 0x3c) >> 2) | (status & ~0xf);
610
611 /* Write it back */
98fa08a5 612 lightrec_mtc0(state, 12, status);
d16005f8
PC
613}
614
98fa08a5 615void lightrec_cp(struct lightrec_state *state, union code op)
d16005f8 616{
98fa08a5
PC
617 if (op.i.op == OP_CP0) {
618 pr_err("Invalid CP opcode to coprocessor #0\n");
619 return;
620 }
d16005f8 621
98fa08a5 622 (*state->ops.cop2_op)(state, op.opcode);
d16005f8
PC
623}
624
03535202 625static void lightrec_cp_cb(struct lightrec_state *state, u32 arg)
22eee2ac 626{
03535202 627 lightrec_cp(state, (union code) arg);
22eee2ac
PC
628}
629
03535202 630static struct block * lightrec_get_block(struct lightrec_state *state, u32 pc)
d16005f8
PC
631{
632 struct block *block = lightrec_find_block(state->block_cache, pc);
ba3814c1 633 u8 old_flags;
d16005f8 634
98fa08a5 635 if (block && lightrec_block_is_outdated(state, block)) {
d16005f8
PC
636 pr_debug("Block at PC 0x%08x is outdated!\n", block->pc);
637
ba3814c1
PC
638 old_flags = block_set_flags(block, BLOCK_IS_DEAD);
639 if (!(old_flags & BLOCK_IS_DEAD)) {
640 /* Make sure the recompiler isn't processing the block
641 * we'll destroy */
642 if (ENABLE_THREADED_COMPILER)
643 lightrec_recompiler_remove(state->rec, block);
644
645 lightrec_unregister_block(state->block_cache, block);
646 remove_from_code_lut(state->block_cache, block);
647 lightrec_free_block(state, block);
648 }
d16005f8 649
d16005f8
PC
650 block = NULL;
651 }
652
653 if (!block) {
654 block = lightrec_precompile_block(state, pc);
655 if (!block) {
656 pr_err("Unable to recompile block at PC 0x%x\n", pc);
657 lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
658 return NULL;
659 }
660
661 lightrec_register_block(state->block_cache, block);
662 }
663
664 return block;
665}
666
667static void * get_next_block_func(struct lightrec_state *state, u32 pc)
668{
669 struct block *block;
670 bool should_recompile;
671 void *func;
d8b04acd 672 int err;
d16005f8
PC
673
674 for (;;) {
d8b04acd 675 func = lut_read(state, lut_offset(pc));
d16005f8 676 if (func && func != state->get_next_block)
98fa08a5 677 break;
d16005f8
PC
678
679 block = lightrec_get_block(state, pc);
680
681 if (unlikely(!block))
98fa08a5
PC
682 break;
683
ba3814c1
PC
684 if (OPT_REPLACE_MEMSET &&
685 block_has_flag(block, BLOCK_IS_MEMSET)) {
98fa08a5
PC
686 func = state->memset_func;
687 break;
688 }
d16005f8 689
ba3814c1
PC
690 should_recompile = block_has_flag(block, BLOCK_SHOULD_RECOMPILE) &&
691 !block_has_flag(block, BLOCK_IS_DEAD);
d16005f8
PC
692
693 if (unlikely(should_recompile)) {
a59e5536 694 pr_debug("Block at PC 0x%08x should recompile\n", pc);
d16005f8 695
d8b04acd 696 if (ENABLE_THREADED_COMPILER) {
a59e5536 697 lightrec_recompiler_add(state->rec, block);
d8b04acd
PC
698 } else {
699 err = lightrec_compile_block(state->cstate, block);
700 if (err) {
701 state->exit_flags = LIGHTREC_EXIT_NOMEM;
702 return NULL;
703 }
704 }
d16005f8
PC
705 }
706
707 if (ENABLE_THREADED_COMPILER && likely(!should_recompile))
98fa08a5 708 func = lightrec_recompiler_run_first_pass(state, block, &pc);
d16005f8
PC
709 else
710 func = block->function;
711
712 if (likely(func))
98fa08a5 713 break;
d16005f8 714
ba3814c1 715 if (unlikely(block_has_flag(block, BLOCK_NEVER_COMPILE))) {
98fa08a5
PC
716 pc = lightrec_emulate_block(state, block, pc);
717
718 } else if (!ENABLE_THREADED_COMPILER) {
719 /* Block wasn't compiled yet - run the interpreter */
ba3814c1 720 if (block_has_flag(block, BLOCK_FULLY_TAGGED))
98fa08a5
PC
721 pr_debug("Block fully tagged, skipping first pass\n");
722 else if (ENABLE_FIRST_PASS && likely(!should_recompile))
723 pc = lightrec_emulate_block(state, block, pc);
d16005f8 724
d16005f8 725 /* Then compile it using the profiled data */
d8b04acd
PC
726 err = lightrec_compile_block(state->cstate, block);
727 if (err) {
728 state->exit_flags = LIGHTREC_EXIT_NOMEM;
729 return NULL;
730 }
ba3814c1
PC
731 } else if (unlikely(block_has_flag(block, BLOCK_IS_DEAD))) {
732 /*
733 * If the block is dead but has never been compiled,
734 * then its function pointer is NULL and we cannot
735 * execute the block. In that case, reap all the dead
736 * blocks now, and in the next loop we will create a
737 * new block.
738 */
739 lightrec_reaper_reap(state->reaper);
98fa08a5
PC
740 } else {
741 lightrec_recompiler_add(state->rec, block);
d16005f8
PC
742 }
743
744 if (state->exit_flags != LIGHTREC_EXIT_NORMAL ||
98fa08a5
PC
745 state->current_cycle >= state->target_cycle)
746 break;
d16005f8 747 }
d16005f8 748
98fa08a5
PC
749 state->next_pc = pc;
750 return func;
d16005f8
PC
751}
752
d8b04acd
PC
753static void * lightrec_alloc_code(struct lightrec_state *state, size_t size)
754{
755 void *code;
756
757 if (ENABLE_THREADED_COMPILER)
758 lightrec_code_alloc_lock(state);
759
760 code = tlsf_malloc(state->tlsf, size);
761
762 if (ENABLE_THREADED_COMPILER)
763 lightrec_code_alloc_unlock(state);
764
765 return code;
766}
767
768static void lightrec_realloc_code(struct lightrec_state *state,
769 void *ptr, size_t size)
770{
771 /* NOTE: 'size' MUST be smaller than the size specified during
772 * the allocation. */
773
774 if (ENABLE_THREADED_COMPILER)
775 lightrec_code_alloc_lock(state);
776
777 tlsf_realloc(state->tlsf, ptr, size);
778
779 if (ENABLE_THREADED_COMPILER)
780 lightrec_code_alloc_unlock(state);
781}
782
783static void lightrec_free_code(struct lightrec_state *state, void *ptr)
784{
785 if (ENABLE_THREADED_COMPILER)
786 lightrec_code_alloc_lock(state);
787
788 tlsf_free(state->tlsf, ptr);
789
790 if (ENABLE_THREADED_COMPILER)
791 lightrec_code_alloc_unlock(state);
792}
793
02487de7 794static void * lightrec_emit_code(struct lightrec_state *state,
d8b04acd 795 const struct block *block,
02487de7
PC
796 jit_state_t *_jit, unsigned int *size)
797{
798 bool has_code_buffer = ENABLE_CODE_BUFFER && state->tlsf;
799 jit_word_t code_size, new_code_size;
800 void *code;
801
802 jit_realize();
803
804 if (!ENABLE_DISASSEMBLER)
805 jit_set_data(NULL, 0, JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
806
807 if (has_code_buffer) {
808 jit_get_code(&code_size);
d8b04acd
PC
809 code = lightrec_alloc_code(state, (size_t) code_size);
810
811 if (!code) {
812 if (ENABLE_THREADED_COMPILER) {
813 /* If we're using the threaded compiler, return
814 * an allocation error here. The threaded
815 * compiler will then empty its job queue and
816 * request a code flush using the reaper. */
817 return NULL;
818 }
819
820 /* Remove outdated blocks, and try again */
821 lightrec_remove_outdated_blocks(state->block_cache, block);
822
823 pr_debug("Re-try to alloc %zu bytes...\n", code_size);
824
825 code = lightrec_alloc_code(state, code_size);
826 if (!code) {
827 pr_err("Could not alloc even after removing old blocks!\n");
828 return NULL;
829 }
830 }
02487de7
PC
831
832 jit_set_code(code, code_size);
833 }
834
835 code = jit_emit();
836
837 jit_get_code(&new_code_size);
838 lightrec_register(MEM_FOR_CODE, new_code_size);
839
840 if (has_code_buffer) {
d8b04acd 841 lightrec_realloc_code(state, code, (size_t) new_code_size);
02487de7
PC
842
843 pr_debug("Creating code block at address 0x%" PRIxPTR ", "
844 "code size: %" PRIuPTR " new: %" PRIuPTR "\n",
845 (uintptr_t) code, code_size, new_code_size);
846 }
847
848 *size = (unsigned int) new_code_size;
849
850 return code;
851}
852
98fa08a5 853static struct block * generate_wrapper(struct lightrec_state *state)
d16005f8
PC
854{
855 struct block *block;
856 jit_state_t *_jit;
857 unsigned int i;
fd58fa32 858 jit_node_t *addr[C_WRAPPERS_COUNT - 1];
ba3814c1 859 jit_node_t *to_end[C_WRAPPERS_COUNT - 1];
d16005f8
PC
860
861 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
862 if (!block)
863 goto err_no_mem;
864
865 _jit = jit_new_state();
866 if (!_jit)
867 goto err_free_block;
868
869 jit_name("RW wrapper");
870 jit_note(__FILE__, __LINE__);
871
872 /* Wrapper entry point */
873 jit_prolog();
fd58fa32
PC
874 jit_tramp(256);
875
ba3814c1 876 /* Add entry points */
fd58fa32 877 for (i = C_WRAPPERS_COUNT - 1; i > 0; i--) {
ba3814c1
PC
878 jit_ldxi(JIT_R1, LIGHTREC_REG_STATE,
879 offsetof(struct lightrec_state, c_wrappers[i]));
880 to_end[i - 1] = jit_b();
fd58fa32
PC
881 addr[i - 1] = jit_indirect();
882 }
883
ba3814c1
PC
884 jit_ldxi(JIT_R1, LIGHTREC_REG_STATE,
885 offsetof(struct lightrec_state, c_wrappers[0]));
886
887 for (i = 0; i < C_WRAPPERS_COUNT - 1; i++)
888 jit_patch(to_end[i]);
889
fd58fa32
PC
890 jit_epilog();
891 jit_prolog();
d16005f8 892
fd58fa32 893 /* Save all temporaries on stack */
ba3814c1
PC
894 for (i = 0; i < NUM_TEMPS; i++) {
895 if (i + FIRST_TEMP != 1) {
896 jit_stxi(offsetof(struct lightrec_state, wrapper_regs[i]),
897 LIGHTREC_REG_STATE, JIT_R(i + FIRST_TEMP));
898 }
899 }
d16005f8 900
ba3814c1 901 jit_getarg(JIT_R2, jit_arg());
03535202 902
ba3814c1
PC
903 jit_prepare();
904 jit_pushargr(LIGHTREC_REG_STATE);
905 jit_pushargr(JIT_R2);
d16005f8 906
ba3814c1
PC
907 jit_ldxi_ui(JIT_R2, LIGHTREC_REG_STATE,
908 offsetof(struct lightrec_state, target_cycle));
d16005f8 909
ba3814c1
PC
910 /* state->current_cycle = state->target_cycle - delta; */
911 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, LIGHTREC_REG_CYCLE);
912 jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
913 LIGHTREC_REG_STATE, LIGHTREC_REG_CYCLE);
d16005f8 914
ba3814c1
PC
915 /* Call the wrapper function */
916 jit_finishr(JIT_R1);
d16005f8 917
ba3814c1
PC
918 /* delta = state->target_cycle - state->current_cycle */;
919 jit_ldxi_ui(LIGHTREC_REG_CYCLE, LIGHTREC_REG_STATE,
920 offsetof(struct lightrec_state, current_cycle));
921 jit_ldxi_ui(JIT_R1, LIGHTREC_REG_STATE,
922 offsetof(struct lightrec_state, target_cycle));
923 jit_subr(LIGHTREC_REG_CYCLE, JIT_R1, LIGHTREC_REG_CYCLE);
fd58fa32 924
ba3814c1
PC
925 /* Restore temporaries from stack */
926 for (i = 0; i < NUM_TEMPS; i++) {
927 if (i + FIRST_TEMP != 1) {
928 jit_ldxi(JIT_R(i + FIRST_TEMP), LIGHTREC_REG_STATE,
929 offsetof(struct lightrec_state, wrapper_regs[i]));
930 }
931 }
d16005f8 932
ba3814c1 933 jit_ret();
d16005f8
PC
934 jit_epilog();
935
d16005f8 936 block->_jit = _jit;
d16005f8 937 block->opcode_list = NULL;
ba3814c1 938 block->flags = BLOCK_NO_OPCODE_LIST;
d16005f8
PC
939 block->nb_ops = 0;
940
d8b04acd 941 block->function = lightrec_emit_code(state, block, _jit,
02487de7
PC
942 &block->code_size);
943 if (!block->function)
944 goto err_free_block;
945
fd58fa32
PC
946 state->wrappers_eps[C_WRAPPERS_COUNT - 1] = block->function;
947
948 for (i = 0; i < C_WRAPPERS_COUNT - 1; i++)
949 state->wrappers_eps[i] = jit_address(addr[i]);
950
d16005f8
PC
951 if (ENABLE_DISASSEMBLER) {
952 pr_debug("Wrapper block:\n");
953 jit_disassemble();
954 }
955
956 jit_clear_state();
957 return block;
958
959err_free_block:
960 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
961err_no_mem:
962 pr_err("Unable to compile wrapper: Out of memory\n");
963 return NULL;
964}
965
98fa08a5
PC
966static u32 lightrec_memset(struct lightrec_state *state)
967{
968 u32 kunseg_pc = kunseg(state->regs.gpr[4]);
969 void *host;
970 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg_pc);
971 u32 length = state->regs.gpr[5] * 4;
972
973 if (!map) {
974 pr_err("Unable to find memory map for memset target address "
975 "0x%x\n", kunseg_pc);
976 return 0;
977 }
978
979 pr_debug("Calling host memset, PC 0x%x (host address 0x%" PRIxPTR ") for %u bytes\n",
980 kunseg_pc, (uintptr_t)host, length);
981 memset(host, 0, length);
982
983 if (!state->invalidate_from_dma_only)
984 lightrec_invalidate_map(state, map, kunseg_pc, length);
985
986 /* Rough estimation of the number of cycles consumed */
987 return 8 + 5 * (length + 3 / 4);
988}
989
d16005f8
PC
990static struct block * generate_dispatcher(struct lightrec_state *state)
991{
992 struct block *block;
993 jit_state_t *_jit;
02487de7 994 jit_node_t *to_end, *loop, *addr, *addr2, *addr3;
d16005f8 995 unsigned int i;
02487de7 996 u32 offset;
d16005f8
PC
997
998 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
999 if (!block)
1000 goto err_no_mem;
1001
1002 _jit = jit_new_state();
1003 if (!_jit)
1004 goto err_free_block;
1005
1006 jit_name("dispatcher");
1007 jit_note(__FILE__, __LINE__);
1008
1009 jit_prolog();
1010 jit_frame(256);
1011
ba3814c1 1012 jit_getarg(JIT_V1, jit_arg());
d16005f8 1013 jit_getarg_i(LIGHTREC_REG_CYCLE, jit_arg());
d16005f8
PC
1014
1015 /* Force all callee-saved registers to be pushed on the stack */
1016 for (i = 0; i < NUM_REGS; i++)
ba3814c1 1017 jit_movr(JIT_V(i + FIRST_REG), JIT_V(i + FIRST_REG));
d16005f8
PC
1018
1019 /* Pass lightrec_state structure to blocks, using the last callee-saved
1020 * register that Lightning provides */
1021 jit_movi(LIGHTREC_REG_STATE, (intptr_t) state);
1022
1023 loop = jit_label();
1024
1025 /* Call the block's code */
ba3814c1 1026 jit_jmpr(JIT_V1);
d16005f8 1027
98fa08a5
PC
1028 if (OPT_REPLACE_MEMSET) {
1029 /* Blocks will jump here when they need to call
1030 * lightrec_memset() */
1031 addr3 = jit_indirect();
1032
ba3814c1
PC
1033 jit_movr(JIT_V1, LIGHTREC_REG_CYCLE);
1034
98fa08a5
PC
1035 jit_prepare();
1036 jit_pushargr(LIGHTREC_REG_STATE);
1037 jit_finishi(lightrec_memset);
1038
1039 jit_ldxi_ui(JIT_V0, LIGHTREC_REG_STATE,
1040 offsetof(struct lightrec_state, regs.gpr[31]));
1041
ba3814c1
PC
1042 jit_retval(LIGHTREC_REG_CYCLE);
1043 jit_subr(LIGHTREC_REG_CYCLE, JIT_V1, LIGHTREC_REG_CYCLE);
98fa08a5
PC
1044 }
1045
d16005f8
PC
1046 /* The block will jump here, with the number of cycles remaining in
1047 * LIGHTREC_REG_CYCLE */
1048 addr2 = jit_indirect();
1049
98fa08a5
PC
1050 /* Store back the next_pc to the lightrec_state structure */
1051 offset = offsetof(struct lightrec_state, next_pc);
1052 jit_stxi_i(offset, LIGHTREC_REG_STATE, JIT_V0);
1053
d16005f8
PC
1054 /* Jump to end if state->target_cycle < state->current_cycle */
1055 to_end = jit_blei(LIGHTREC_REG_CYCLE, 0);
1056
1057 /* Convert next PC to KUNSEG and avoid mirrors */
ba3814c1
PC
1058 jit_andi(JIT_V1, JIT_V0, 0x10000000 | (RAM_SIZE - 1));
1059 jit_rshi_u(JIT_R1, JIT_V1, 28);
02487de7
PC
1060 jit_andi(JIT_R2, JIT_V0, BIOS_SIZE - 1);
1061 jit_addi(JIT_R2, JIT_R2, RAM_SIZE);
ba3814c1 1062 jit_movnr(JIT_V1, JIT_R2, JIT_R1);
02487de7
PC
1063
1064 /* If possible, use the code LUT */
1065 if (!lut_is_32bit(state))
ba3814c1
PC
1066 jit_lshi(JIT_V1, JIT_V1, 1);
1067 jit_addr(JIT_V1, JIT_V1, LIGHTREC_REG_STATE);
02487de7
PC
1068
1069 offset = offsetof(struct lightrec_state, code_lut);
1070 if (lut_is_32bit(state))
ba3814c1 1071 jit_ldxi_ui(JIT_V1, JIT_V1, offset);
02487de7 1072 else
ba3814c1 1073 jit_ldxi(JIT_V1, JIT_V1, offset);
d16005f8
PC
1074
1075 /* If we get non-NULL, loop */
ba3814c1
PC
1076 jit_patch_at(jit_bnei(JIT_V1, 0), loop);
1077
1078 /* The code LUT will be set to this address when the block at the target
1079 * PC has been preprocessed but not yet compiled by the threaded
1080 * recompiler */
1081 addr = jit_indirect();
d16005f8
PC
1082
1083 /* Slow path: call C function get_next_block_func() */
d16005f8 1084
98fa08a5 1085 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
d16005f8
PC
1086 /* We may call the interpreter - update state->current_cycle */
1087 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
1088 offsetof(struct lightrec_state, target_cycle));
ba3814c1 1089 jit_subr(JIT_V1, JIT_R2, LIGHTREC_REG_CYCLE);
d16005f8 1090 jit_stxi_i(offsetof(struct lightrec_state, current_cycle),
ba3814c1 1091 LIGHTREC_REG_STATE, JIT_V1);
d16005f8
PC
1092 }
1093
d16005f8
PC
1094 jit_prepare();
1095 jit_pushargr(LIGHTREC_REG_STATE);
1096 jit_pushargr(JIT_V0);
ba3814c1
PC
1097
1098 /* Save the cycles register if needed */
1099 if (!(ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES))
1100 jit_movr(JIT_V0, LIGHTREC_REG_CYCLE);
1101
1102 /* Get the next block */
d16005f8 1103 jit_finishi(&get_next_block_func);
ba3814c1 1104 jit_retval(JIT_V1);
d16005f8 1105
98fa08a5 1106 if (ENABLE_FIRST_PASS || OPT_DETECT_IMPOSSIBLE_BRANCHES) {
d16005f8
PC
1107 /* The interpreter may have updated state->current_cycle and
1108 * state->target_cycle - recalc the delta */
1109 jit_ldxi_i(JIT_R1, LIGHTREC_REG_STATE,
1110 offsetof(struct lightrec_state, current_cycle));
1111 jit_ldxi_i(JIT_R2, LIGHTREC_REG_STATE,
1112 offsetof(struct lightrec_state, target_cycle));
1113 jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, JIT_R1);
ba3814c1
PC
1114 } else {
1115 jit_movr(LIGHTREC_REG_CYCLE, JIT_V0);
d16005f8
PC
1116 }
1117
1118 /* If we get non-NULL, loop */
ba3814c1 1119 jit_patch_at(jit_bnei(JIT_V1, 0), loop);
d16005f8 1120
d16005f8
PC
1121 /* When exiting, the recompiled code will jump to that address */
1122 jit_note(__FILE__, __LINE__);
1123 jit_patch(to_end);
1124
d16005f8
PC
1125 jit_retr(LIGHTREC_REG_CYCLE);
1126 jit_epilog();
1127
d16005f8 1128 block->_jit = _jit;
d16005f8 1129 block->opcode_list = NULL;
ba3814c1 1130 block->flags = BLOCK_NO_OPCODE_LIST;
d16005f8
PC
1131 block->nb_ops = 0;
1132
d8b04acd 1133 block->function = lightrec_emit_code(state, block, _jit,
02487de7
PC
1134 &block->code_size);
1135 if (!block->function)
1136 goto err_free_block;
d16005f8
PC
1137
1138 state->eob_wrapper_func = jit_address(addr2);
98fa08a5
PC
1139 if (OPT_REPLACE_MEMSET)
1140 state->memset_func = jit_address(addr3);
d16005f8
PC
1141 state->get_next_block = jit_address(addr);
1142
1143 if (ENABLE_DISASSEMBLER) {
1144 pr_debug("Dispatcher block:\n");
1145 jit_disassemble();
1146 }
1147
1148 /* We're done! */
1149 jit_clear_state();
1150 return block;
1151
1152err_free_block:
1153 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1154err_no_mem:
1155 pr_err("Unable to compile dispatcher: Out of memory\n");
1156 return NULL;
1157}
1158
1159union code lightrec_read_opcode(struct lightrec_state *state, u32 pc)
1160{
fd58fa32 1161 void *host = NULL;
d16005f8 1162
98fa08a5 1163 lightrec_get_map(state, &host, kunseg(pc));
d16005f8 1164
98fa08a5 1165 const u32 *code = (u32 *)host;
02487de7 1166 return (union code) LE32TOH(*code);
98fa08a5 1167}
d16005f8 1168
98fa08a5
PC
1169unsigned int lightrec_cycles_of_opcode(union code code)
1170{
1171 return 2;
1172}
d16005f8 1173
ba3814c1 1174void lightrec_free_opcode_list(struct lightrec_state *state, struct opcode *ops)
98fa08a5 1175{
ba3814c1
PC
1176 struct opcode_list *list = container_of(ops, struct opcode_list, ops);
1177
98fa08a5 1178 lightrec_free(state, MEM_FOR_IR,
ba3814c1
PC
1179 sizeof(*list) + list->nb_ops * sizeof(struct opcode),
1180 list);
98fa08a5
PC
1181}
1182
1183static unsigned int lightrec_get_mips_block_len(const u32 *src)
1184{
1185 unsigned int i;
1186 union code c;
1187
1188 for (i = 1; ; i++) {
1189 c.opcode = LE32TOH(*src++);
1190
1191 if (is_syscall(c))
1192 return i;
1193
1194 if (is_unconditional_jump(c))
1195 return i + 1;
1196 }
1197}
1198
1199static struct opcode * lightrec_disassemble(struct lightrec_state *state,
1200 const u32 *src, unsigned int *len)
1201{
ba3814c1 1202 struct opcode_list *list;
98fa08a5
PC
1203 unsigned int i, length;
1204
1205 length = lightrec_get_mips_block_len(src);
1206
ba3814c1
PC
1207 list = lightrec_malloc(state, MEM_FOR_IR,
1208 sizeof(*list) + sizeof(struct opcode) * length);
98fa08a5
PC
1209 if (!list) {
1210 pr_err("Unable to allocate memory\n");
1211 return NULL;
1212 }
1213
ba3814c1
PC
1214 list->nb_ops = (u16) length;
1215
98fa08a5 1216 for (i = 0; i < length; i++) {
ba3814c1
PC
1217 list->ops[i].opcode = LE32TOH(src[i]);
1218 list->ops[i].flags = 0;
98fa08a5
PC
1219 }
1220
1221 *len = length * sizeof(u32);
1222
ba3814c1 1223 return list->ops;
d16005f8
PC
1224}
1225
1226static struct block * lightrec_precompile_block(struct lightrec_state *state,
1227 u32 pc)
1228{
1229 struct opcode *list;
1230 struct block *block;
ba3814c1 1231 void *host, *addr;
98fa08a5
PC
1232 const struct lightrec_mem_map *map = lightrec_get_map(state, &host, kunseg(pc));
1233 const u32 *code = (u32 *) host;
d16005f8 1234 unsigned int length;
98fa08a5 1235 bool fully_tagged;
ba3814c1 1236 u8 block_flags = 0;
d16005f8
PC
1237
1238 if (!map)
1239 return NULL;
1240
d16005f8
PC
1241 block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
1242 if (!block) {
1243 pr_err("Unable to recompile block: Out of memory\n");
1244 return NULL;
1245 }
1246
1247 list = lightrec_disassemble(state, code, &length);
1248 if (!list) {
1249 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1250 return NULL;
1251 }
1252
1253 block->pc = pc;
d16005f8
PC
1254 block->_jit = NULL;
1255 block->function = NULL;
1256 block->opcode_list = list;
98fa08a5 1257 block->code = code;
d16005f8
PC
1258 block->next = NULL;
1259 block->flags = 0;
1260 block->code_size = 0;
d8b04acd 1261 block->precompile_date = state->current_cycle;
d16005f8
PC
1262 block->nb_ops = length / sizeof(u32);
1263
98fa08a5 1264 lightrec_optimize(state, block);
d16005f8
PC
1265
1266 length = block->nb_ops * sizeof(u32);
1267
1268 lightrec_register(MEM_FOR_MIPS_CODE, length);
1269
1270 if (ENABLE_DISASSEMBLER) {
98fa08a5
PC
1271 pr_debug("Disassembled block at PC: 0x%08x\n", block->pc);
1272 lightrec_print_disassembly(block, code);
d16005f8
PC
1273 }
1274
98fa08a5 1275 pr_debug("Block size: %hu opcodes\n", block->nb_ops);
d16005f8
PC
1276
1277 /* If the first opcode is an 'impossible' branch, never compile the
1278 * block */
98fa08a5 1279 if (should_emulate(block->opcode_list))
ba3814c1 1280 block_flags |= BLOCK_NEVER_COMPILE;
d16005f8 1281
98fa08a5
PC
1282 fully_tagged = lightrec_block_is_fully_tagged(block);
1283 if (fully_tagged)
ba3814c1 1284 block_flags |= BLOCK_FULLY_TAGGED;
98fa08a5 1285
ba3814c1
PC
1286 if (block_flags)
1287 block_set_flags(block, block_flags);
98fa08a5 1288
d16005f8
PC
1289 block->hash = lightrec_calculate_block_hash(block);
1290
ba3814c1
PC
1291 if (OPT_REPLACE_MEMSET && block_has_flag(block, BLOCK_IS_MEMSET))
1292 addr = state->memset_func;
1293 else
1294 addr = state->get_next_block;
1295 lut_write(state, lut_offset(pc), addr);
1296
a59e5536 1297 pr_debug("Recompile count: %u\n", state->nb_precompile++);
1298
d16005f8
PC
1299 return block;
1300}
1301
98fa08a5 1302static bool lightrec_block_is_fully_tagged(const struct block *block)
d16005f8 1303{
98fa08a5
PC
1304 const struct opcode *op;
1305 unsigned int i;
1306
1307 for (i = 0; i < block->nb_ops; i++) {
1308 op = &block->opcode_list[i];
d16005f8 1309
d16005f8
PC
1310 /* Verify that all load/stores of the opcode list
1311 * Check all loads/stores of the opcode list and mark the
1312 * block as fully compiled if they all have been tagged. */
1313 switch (op->c.i.op) {
1314 case OP_LB:
1315 case OP_LH:
1316 case OP_LWL:
1317 case OP_LW:
1318 case OP_LBU:
1319 case OP_LHU:
1320 case OP_LWR:
1321 case OP_SB:
1322 case OP_SH:
1323 case OP_SWL:
1324 case OP_SW:
1325 case OP_SWR:
1326 case OP_LWC2:
1327 case OP_SWC2:
22eee2ac 1328 if (!LIGHTREC_FLAGS_GET_IO_MODE(op->flags))
d16005f8 1329 return false;
d8b04acd
PC
1330 fallthrough;
1331 default:
d16005f8
PC
1332 continue;
1333 }
1334 }
1335
1336 return true;
1337}
1338
98fa08a5 1339static void lightrec_reap_block(struct lightrec_state *state, void *data)
a59e5536 1340{
1341 struct block *block = data;
1342
1343 pr_debug("Reap dead block at PC 0x%08x\n", block->pc);
98fa08a5
PC
1344 lightrec_unregister_block(state->block_cache, block);
1345 lightrec_free_block(state, block);
a59e5536 1346}
1347
98fa08a5 1348static void lightrec_reap_jit(struct lightrec_state *state, void *data)
a59e5536 1349{
1350 _jit_destroy_state(data);
1351}
1352
02487de7
PC
1353static void lightrec_free_function(struct lightrec_state *state, void *fn)
1354{
1355 if (ENABLE_CODE_BUFFER && state->tlsf) {
1356 pr_debug("Freeing code block at 0x%" PRIxPTR "\n", (uintptr_t) fn);
d8b04acd 1357 lightrec_free_code(state, fn);
02487de7
PC
1358 }
1359}
1360
1361static void lightrec_reap_function(struct lightrec_state *state, void *data)
1362{
1363 lightrec_free_function(state, data);
1364}
1365
ba3814c1
PC
1366static void lightrec_reap_opcode_list(struct lightrec_state *state, void *data)
1367{
1368 lightrec_free_opcode_list(state, data);
1369}
1370
98fa08a5
PC
1371int lightrec_compile_block(struct lightrec_cstate *cstate,
1372 struct block *block)
d16005f8 1373{
98fa08a5 1374 struct lightrec_state *state = cstate->state;
a59e5536 1375 struct lightrec_branch_target *target;
ba3814c1 1376 bool fully_tagged = false;
a59e5536 1377 struct block *block2;
d16005f8 1378 struct opcode *elm;
a59e5536 1379 jit_state_t *_jit, *oldjit;
d16005f8
PC
1380 jit_node_t *start_of_block;
1381 bool skip_next = false;
d8b04acd 1382 void *old_fn, *new_fn;
ba3814c1 1383 size_t old_code_size;
d16005f8 1384 unsigned int i, j;
ba3814c1 1385 u8 old_flags;
98fa08a5 1386 u32 offset;
d16005f8
PC
1387
1388 fully_tagged = lightrec_block_is_fully_tagged(block);
1389 if (fully_tagged)
ba3814c1 1390 block_set_flags(block, BLOCK_FULLY_TAGGED);
d16005f8
PC
1391
1392 _jit = jit_new_state();
1393 if (!_jit)
1394 return -ENOMEM;
1395
a59e5536 1396 oldjit = block->_jit;
02487de7 1397 old_fn = block->function;
ba3814c1 1398 old_code_size = block->code_size;
d16005f8
PC
1399 block->_jit = _jit;
1400
98fa08a5
PC
1401 lightrec_regcache_reset(cstate->reg_cache);
1402 cstate->cycles = 0;
98fa08a5
PC
1403 cstate->nb_local_branches = 0;
1404 cstate->nb_targets = 0;
d16005f8
PC
1405
1406 jit_prolog();
1407 jit_tramp(256);
1408
1409 start_of_block = jit_label();
1410
98fa08a5
PC
1411 for (i = 0; i < block->nb_ops; i++) {
1412 elm = &block->opcode_list[i];
d16005f8
PC
1413
1414 if (skip_next) {
1415 skip_next = false;
1416 continue;
1417 }
1418
98fa08a5 1419 if (should_emulate(elm)) {
d16005f8 1420 pr_debug("Branch at offset 0x%x will be emulated\n",
98fa08a5
PC
1421 i << 2);
1422
13b02197 1423 lightrec_emit_eob(cstate, block, i);
03535202 1424 skip_next = !op_flag_no_ds(elm->flags);
98fa08a5
PC
1425 } else {
1426 lightrec_rec_opcode(cstate, block, i);
03535202 1427 skip_next = !op_flag_no_ds(elm->flags) && has_delay_slot(elm->c);
d16005f8
PC
1428#if _WIN32
1429 /* FIXME: GNU Lightning on Windows seems to use our
1430 * mapped registers as temporaries. Until the actual bug
1431 * is found and fixed, unconditionally mark our
1432 * registers as live here. */
98fa08a5 1433 lightrec_regcache_mark_live(cstate->reg_cache, _jit);
d16005f8
PC
1434#endif
1435 }
03535202
PC
1436
1437 cstate->cycles += lightrec_cycles_of_opcode(elm->c);
d16005f8
PC
1438 }
1439
98fa08a5
PC
1440 for (i = 0; i < cstate->nb_local_branches; i++) {
1441 struct lightrec_branch *branch = &cstate->local_branches[i];
d16005f8
PC
1442
1443 pr_debug("Patch local branch to offset 0x%x\n",
1444 branch->target << 2);
1445
1446 if (branch->target == 0) {
1447 jit_patch_at(branch->branch, start_of_block);
1448 continue;
1449 }
1450
98fa08a5
PC
1451 for (j = 0; j < cstate->nb_targets; j++) {
1452 if (cstate->targets[j].offset == branch->target) {
d16005f8 1453 jit_patch_at(branch->branch,
98fa08a5 1454 cstate->targets[j].label);
d16005f8
PC
1455 break;
1456 }
1457 }
1458
98fa08a5 1459 if (j == cstate->nb_targets)
d16005f8
PC
1460 pr_err("Unable to find branch target\n");
1461 }
1462
d16005f8
PC
1463 jit_ret();
1464 jit_epilog();
1465
d8b04acd
PC
1466 new_fn = lightrec_emit_code(state, block, _jit, &block->code_size);
1467 if (!new_fn) {
1468 if (!ENABLE_THREADED_COMPILER)
1469 pr_err("Unable to compile block!\n");
1470 block->_jit = oldjit;
ba3814c1 1471 jit_clear_state();
d8b04acd
PC
1472 _jit_destroy_state(_jit);
1473 return -ENOMEM;
02487de7
PC
1474 }
1475
ba3814c1
PC
1476 /* Pause the reaper, because lightrec_reset_lut_offset() may try to set
1477 * the old block->function pointer to the code LUT. */
1478 if (ENABLE_THREADED_COMPILER)
1479 lightrec_reaper_pause(state->reaper);
1480
d8b04acd 1481 block->function = new_fn;
ba3814c1 1482 block_clear_flags(block, BLOCK_SHOULD_RECOMPILE);
d16005f8
PC
1483
1484 /* Add compiled function to the LUT */
02487de7 1485 lut_write(state, lut_offset(block->pc), block->function);
d16005f8 1486
ba3814c1
PC
1487 if (ENABLE_THREADED_COMPILER)
1488 lightrec_reaper_continue(state->reaper);
a59e5536 1489
1490 /* Detect old blocks that have been covered by the new one */
98fa08a5
PC
1491 for (i = 0; i < cstate->nb_targets; i++) {
1492 target = &cstate->targets[i];
a59e5536 1493
1494 if (!target->offset)
1495 continue;
1496
1497 offset = block->pc + target->offset * sizeof(u32);
ba3814c1
PC
1498
1499 /* Pause the reaper while we search for the block until we set
1500 * the BLOCK_IS_DEAD flag, otherwise the block may be removed
1501 * under our feet. */
1502 if (ENABLE_THREADED_COMPILER)
1503 lightrec_reaper_pause(state->reaper);
1504
a59e5536 1505 block2 = lightrec_find_block(state->block_cache, offset);
1506 if (block2) {
1507 /* No need to check if block2 is compilable - it must
1508 * be, otherwise block wouldn't be compilable either */
1509
98fa08a5
PC
1510 /* Set the "block dead" flag to prevent the dynarec from
1511 * recompiling this block */
ba3814c1
PC
1512 old_flags = block_set_flags(block2, BLOCK_IS_DEAD);
1513 }
1514
1515 if (ENABLE_THREADED_COMPILER) {
1516 lightrec_reaper_continue(state->reaper);
a59e5536 1517
98fa08a5
PC
1518 /* If block2 was pending for compilation, cancel it.
1519 * If it's being compiled right now, wait until it
1520 * finishes. */
ba3814c1 1521 if (block2)
98fa08a5 1522 lightrec_recompiler_remove(state->rec, block2);
fd58fa32 1523 }
98fa08a5 1524
fd58fa32
PC
1525 /* We know from now on that block2 (if present) isn't going to
1526 * be compiled. We can override the LUT entry with our new
1527 * block's entry point. */
1528 offset = lut_offset(block->pc) + target->offset;
02487de7 1529 lut_write(state, offset, jit_address(target->label));
98fa08a5 1530
fd58fa32 1531 if (block2) {
a59e5536 1532 pr_debug("Reap block 0x%08x as it's covered by block "
1533 "0x%08x\n", block2->pc, block->pc);
1534
98fa08a5 1535 /* Finally, reap the block. */
ba3814c1
PC
1536 if (!ENABLE_THREADED_COMPILER) {
1537 lightrec_unregister_block(state->block_cache, block2);
1538 lightrec_free_block(state, block2);
1539 } else if (!(old_flags & BLOCK_IS_DEAD)) {
a59e5536 1540 lightrec_reaper_add(state->reaper,
1541 lightrec_reap_block,
1542 block2);
a59e5536 1543 }
1544 }
1545 }
1546
d16005f8 1547 if (ENABLE_DISASSEMBLER) {
98fa08a5 1548 pr_debug("Compiling block at PC: 0x%08x\n", block->pc);
d16005f8
PC
1549 jit_disassemble();
1550 }
1551
1552 jit_clear_state();
1553
d16005f8 1554 if (fully_tagged)
ba3814c1
PC
1555 old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
1556
1557 if (fully_tagged && !(old_flags & BLOCK_NO_OPCODE_LIST)) {
d16005f8
PC
1558 pr_debug("Block PC 0x%08x is fully tagged"
1559 " - free opcode list\n", block->pc);
ba3814c1
PC
1560
1561 if (ENABLE_THREADED_COMPILER) {
1562 lightrec_reaper_add(state->reaper,
1563 lightrec_reap_opcode_list,
1564 block->opcode_list);
1565 } else {
1566 lightrec_free_opcode_list(state, block->opcode_list);
1567 }
d16005f8
PC
1568 }
1569
a59e5536 1570 if (oldjit) {
1571 pr_debug("Block 0x%08x recompiled, reaping old jit context.\n",
1572 block->pc);
1573
02487de7 1574 if (ENABLE_THREADED_COMPILER) {
a59e5536 1575 lightrec_reaper_add(state->reaper,
1576 lightrec_reap_jit, oldjit);
02487de7
PC
1577 lightrec_reaper_add(state->reaper,
1578 lightrec_reap_function, old_fn);
1579 } else {
a59e5536 1580 _jit_destroy_state(oldjit);
02487de7
PC
1581 lightrec_free_function(state, old_fn);
1582 }
ba3814c1
PC
1583
1584 lightrec_unregister(MEM_FOR_CODE, old_code_size);
a59e5536 1585 }
1586
d16005f8
PC
1587 return 0;
1588}
1589
98fa08a5
PC
1590static void lightrec_print_info(struct lightrec_state *state)
1591{
1592 if ((state->current_cycle & ~0xfffffff) != state->old_cycle_counter) {
1593 pr_info("Lightrec RAM usage: IR %u KiB, CODE %u KiB, "
1594 "MIPS %u KiB, TOTAL %u KiB, avg. IPI %f\n",
1595 lightrec_get_mem_usage(MEM_FOR_IR) / 1024,
1596 lightrec_get_mem_usage(MEM_FOR_CODE) / 1024,
1597 lightrec_get_mem_usage(MEM_FOR_MIPS_CODE) / 1024,
1598 lightrec_get_total_mem_usage() / 1024,
1599 lightrec_get_average_ipi());
1600 state->old_cycle_counter = state->current_cycle & ~0xfffffff;
1601 }
1602}
1603
d16005f8
PC
1604u32 lightrec_execute(struct lightrec_state *state, u32 pc, u32 target_cycle)
1605{
1606 s32 (*func)(void *, s32) = (void *)state->dispatcher->function;
1607 void *block_trace;
1608 s32 cycles_delta;
1609
1610 state->exit_flags = LIGHTREC_EXIT_NORMAL;
1611
1612 /* Handle the cycle counter overflowing */
1613 if (unlikely(target_cycle < state->current_cycle))
1614 target_cycle = UINT_MAX;
1615
1616 state->target_cycle = target_cycle;
98fa08a5 1617 state->next_pc = pc;
d16005f8
PC
1618
1619 block_trace = get_next_block_func(state, pc);
1620 if (block_trace) {
1621 cycles_delta = state->target_cycle - state->current_cycle;
1622
1623 cycles_delta = (*func)(block_trace, cycles_delta);
1624
1625 state->current_cycle = state->target_cycle - cycles_delta;
1626 }
1627
a59e5536 1628 if (ENABLE_THREADED_COMPILER)
1629 lightrec_reaper_reap(state->reaper);
1630
98fa08a5
PC
1631 if (LOG_LEVEL >= INFO_L)
1632 lightrec_print_info(state);
1633
d16005f8
PC
1634 return state->next_pc;
1635}
1636
ba3814c1
PC
1637u32 lightrec_run_interpreter(struct lightrec_state *state, u32 pc,
1638 u32 target_cycle)
d16005f8 1639{
ba3814c1 1640 struct block *block;
d16005f8
PC
1641
1642 state->exit_flags = LIGHTREC_EXIT_NORMAL;
ba3814c1
PC
1643 state->target_cycle = target_cycle;
1644
1645 do {
1646 block = lightrec_get_block(state, pc);
1647 if (!block)
1648 break;
1649
1650 pc = lightrec_emulate_block(state, block, pc);
d16005f8 1651
ba3814c1
PC
1652 if (ENABLE_THREADED_COMPILER)
1653 lightrec_reaper_reap(state->reaper);
1654 } while (state->current_cycle < state->target_cycle);
98fa08a5
PC
1655
1656 if (LOG_LEVEL >= INFO_L)
1657 lightrec_print_info(state);
1658
1659 return pc;
d16005f8
PC
1660}
1661
98fa08a5 1662void lightrec_free_block(struct lightrec_state *state, struct block *block)
d16005f8 1663{
ba3814c1
PC
1664 u8 old_flags;
1665
d16005f8 1666 lightrec_unregister(MEM_FOR_MIPS_CODE, block->nb_ops * sizeof(u32));
ba3814c1
PC
1667 old_flags = block_set_flags(block, BLOCK_NO_OPCODE_LIST);
1668
1669 if (!(old_flags & BLOCK_NO_OPCODE_LIST))
1670 lightrec_free_opcode_list(state, block->opcode_list);
d16005f8
PC
1671 if (block->_jit)
1672 _jit_destroy_state(block->_jit);
d8b04acd
PC
1673 if (block->function) {
1674 lightrec_free_function(state, block->function);
1675 lightrec_unregister(MEM_FOR_CODE, block->code_size);
1676 }
98fa08a5
PC
1677 lightrec_free(state, MEM_FOR_IR, sizeof(*block), block);
1678}
1679
1680struct lightrec_cstate * lightrec_create_cstate(struct lightrec_state *state)
1681{
1682 struct lightrec_cstate *cstate;
1683
1684 cstate = lightrec_malloc(state, MEM_FOR_LIGHTREC, sizeof(*cstate));
1685 if (!cstate)
1686 return NULL;
1687
1688 cstate->reg_cache = lightrec_regcache_init(state);
1689 if (!cstate->reg_cache) {
1690 lightrec_free(state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
1691 return NULL;
1692 }
1693
1694 cstate->state = state;
1695
1696 return cstate;
1697}
1698
1699void lightrec_free_cstate(struct lightrec_cstate *cstate)
1700{
1701 lightrec_free_regcache(cstate->reg_cache);
1702 lightrec_free(cstate->state, MEM_FOR_LIGHTREC, sizeof(*cstate), cstate);
d16005f8
PC
1703}
1704
1705struct lightrec_state * lightrec_init(char *argv0,
1706 const struct lightrec_mem_map *map,
1707 size_t nb,
1708 const struct lightrec_ops *ops)
1709{
d8b04acd 1710 const struct lightrec_mem_map *codebuf_map = &map[PSX_MAP_CODE_BUFFER];
d16005f8 1711 struct lightrec_state *state;
02487de7
PC
1712 uintptr_t addr;
1713 void *tlsf = NULL;
1714 bool with_32bit_lut = false;
1715 size_t lut_size;
d16005f8
PC
1716
1717 /* Sanity-check ops */
98fa08a5 1718 if (!ops || !ops->cop2_op || !ops->enable_ram) {
d16005f8
PC
1719 pr_err("Missing callbacks in lightrec_ops structure\n");
1720 return NULL;
1721 }
1722
fdf33147
PC
1723 if (ops->cop2_notify)
1724 pr_debug("Optional cop2_notify callback in lightrec_ops\n");
1725 else
1726 pr_debug("No optional cop2_notify callback in lightrec_ops\n");
1727
d8b04acd
PC
1728 if (ENABLE_CODE_BUFFER && nb > PSX_MAP_CODE_BUFFER
1729 && codebuf_map->address) {
02487de7
PC
1730 tlsf = tlsf_create_with_pool(codebuf_map->address,
1731 codebuf_map->length);
1732 if (!tlsf) {
1733 pr_err("Unable to initialize code buffer\n");
1734 return NULL;
1735 }
1736
1737 if (__WORDSIZE == 64) {
1738 addr = (uintptr_t) codebuf_map->address + codebuf_map->length - 1;
1739 with_32bit_lut = addr == (u32) addr;
1740 }
1741 }
1742
1743 if (with_32bit_lut)
1744 lut_size = CODE_LUT_SIZE * 4;
1745 else
1746 lut_size = CODE_LUT_SIZE * sizeof(void *);
1747
d16005f8
PC
1748 init_jit(argv0);
1749
02487de7 1750 state = calloc(1, sizeof(*state) + lut_size);
d16005f8
PC
1751 if (!state)
1752 goto err_finish_jit;
1753
02487de7
PC
1754 lightrec_register(MEM_FOR_LIGHTREC, sizeof(*state) + lut_size);
1755
1756 state->tlsf = tlsf;
1757 state->with_32bit_lut = with_32bit_lut;
d16005f8 1758
d16005f8
PC
1759 state->block_cache = lightrec_blockcache_init(state);
1760 if (!state->block_cache)
d8b04acd 1761 goto err_free_state;
d16005f8 1762
d16005f8
PC
1763 if (ENABLE_THREADED_COMPILER) {
1764 state->rec = lightrec_recompiler_init(state);
1765 if (!state->rec)
98fa08a5 1766 goto err_free_block_cache;
a59e5536 1767
1768 state->reaper = lightrec_reaper_init(state);
1769 if (!state->reaper)
1770 goto err_free_recompiler;
98fa08a5
PC
1771 } else {
1772 state->cstate = lightrec_create_cstate(state);
1773 if (!state->cstate)
1774 goto err_free_block_cache;
d16005f8
PC
1775 }
1776
1777 state->nb_maps = nb;
1778 state->maps = map;
1779
1780 memcpy(&state->ops, ops, sizeof(*ops));
1781
1782 state->dispatcher = generate_dispatcher(state);
1783 if (!state->dispatcher)
a59e5536 1784 goto err_free_reaper;
d16005f8 1785
98fa08a5
PC
1786 state->c_wrapper_block = generate_wrapper(state);
1787 if (!state->c_wrapper_block)
d16005f8
PC
1788 goto err_free_dispatcher;
1789
98fa08a5
PC
1790 state->c_wrappers[C_WRAPPER_RW] = lightrec_rw_cb;
1791 state->c_wrappers[C_WRAPPER_RW_GENERIC] = lightrec_rw_generic_cb;
fdf33147 1792 state->c_wrappers[C_WRAPPER_MFC] = lightrec_mfc_cb;
98fa08a5 1793 state->c_wrappers[C_WRAPPER_MTC] = lightrec_mtc_cb;
22eee2ac 1794 state->c_wrappers[C_WRAPPER_CP] = lightrec_cp_cb;
d16005f8
PC
1795
1796 map = &state->maps[PSX_MAP_BIOS];
1797 state->offset_bios = (uintptr_t)map->address - map->pc;
1798
1799 map = &state->maps[PSX_MAP_SCRATCH_PAD];
1800 state->offset_scratch = (uintptr_t)map->address - map->pc;
1801
ba3814c1
PC
1802 map = &state->maps[PSX_MAP_HW_REGISTERS];
1803 state->offset_io = (uintptr_t)map->address - map->pc;
1804
d16005f8
PC
1805 map = &state->maps[PSX_MAP_KERNEL_USER_RAM];
1806 state->offset_ram = (uintptr_t)map->address - map->pc;
1807
1808 if (state->maps[PSX_MAP_MIRROR1].address == map->address + 0x200000 &&
1809 state->maps[PSX_MAP_MIRROR2].address == map->address + 0x400000 &&
1810 state->maps[PSX_MAP_MIRROR3].address == map->address + 0x600000)
1811 state->mirrors_mapped = true;
1812
98fa08a5
PC
1813 if (state->offset_bios == 0 &&
1814 state->offset_scratch == 0 &&
1815 state->offset_ram == 0 &&
ba3814c1 1816 state->offset_io == 0 &&
98fa08a5
PC
1817 state->mirrors_mapped) {
1818 pr_info("Memory map is perfect. Emitted code will be best.\n");
1819 } else {
1820 pr_info("Memory map is sub-par. Emitted code will be slow.\n");
1821 }
1822
02487de7
PC
1823 if (state->with_32bit_lut)
1824 pr_info("Using 32-bit LUT\n");
1825
d16005f8
PC
1826 return state;
1827
d16005f8 1828err_free_dispatcher:
98fa08a5 1829 lightrec_free_block(state, state->dispatcher);
a59e5536 1830err_free_reaper:
1831 if (ENABLE_THREADED_COMPILER)
1832 lightrec_reaper_destroy(state->reaper);
d16005f8
PC
1833err_free_recompiler:
1834 if (ENABLE_THREADED_COMPILER)
1835 lightrec_free_recompiler(state->rec);
98fa08a5
PC
1836 else
1837 lightrec_free_cstate(state->cstate);
d16005f8
PC
1838err_free_block_cache:
1839 lightrec_free_block_cache(state->block_cache);
d16005f8 1840err_free_state:
d16005f8 1841 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
02487de7 1842 lut_elm_size(state) * CODE_LUT_SIZE);
d16005f8
PC
1843 free(state);
1844err_finish_jit:
1845 finish_jit();
02487de7
PC
1846 if (ENABLE_CODE_BUFFER && tlsf)
1847 tlsf_destroy(tlsf);
d16005f8
PC
1848 return NULL;
1849}
1850
1851void lightrec_destroy(struct lightrec_state *state)
1852{
98fa08a5
PC
1853 /* Force a print info on destroy*/
1854 state->current_cycle = ~state->current_cycle;
1855 lightrec_print_info(state);
1856
03535202
PC
1857 lightrec_free_block_cache(state->block_cache);
1858 lightrec_free_block(state, state->dispatcher);
1859 lightrec_free_block(state, state->c_wrapper_block);
1860
a59e5536 1861 if (ENABLE_THREADED_COMPILER) {
d16005f8 1862 lightrec_free_recompiler(state->rec);
a59e5536 1863 lightrec_reaper_destroy(state->reaper);
98fa08a5
PC
1864 } else {
1865 lightrec_free_cstate(state->cstate);
a59e5536 1866 }
d16005f8 1867
d16005f8 1868 finish_jit();
02487de7
PC
1869 if (ENABLE_CODE_BUFFER && state->tlsf)
1870 tlsf_destroy(state->tlsf);
d16005f8 1871
d16005f8 1872 lightrec_unregister(MEM_FOR_LIGHTREC, sizeof(*state) +
02487de7 1873 lut_elm_size(state) * CODE_LUT_SIZE);
d16005f8
PC
1874 free(state);
1875}
1876
1877void lightrec_invalidate(struct lightrec_state *state, u32 addr, u32 len)
1878{
1879 u32 kaddr = kunseg(addr & ~0x3);
d8b04acd 1880 enum psx_map idx = lightrec_get_map_idx(state, kaddr);
d16005f8 1881
d8b04acd
PC
1882 switch (idx) {
1883 case PSX_MAP_MIRROR1:
1884 case PSX_MAP_MIRROR2:
1885 case PSX_MAP_MIRROR3:
d16005f8 1886 /* Handle mirrors */
d8b04acd
PC
1887 kaddr &= RAM_SIZE - 1;
1888 fallthrough;
1889 case PSX_MAP_KERNEL_USER_RAM:
1890 break;
1891 default:
1892 return;
d16005f8 1893 }
d8b04acd
PC
1894
1895 memset(lut_address(state, lut_offset(kaddr)), 0,
1896 ((len + 3) / 4) * lut_elm_size(state));
d16005f8
PC
1897}
1898
1899void lightrec_invalidate_all(struct lightrec_state *state)
1900{
02487de7 1901 memset(state->code_lut, 0, lut_elm_size(state) * CODE_LUT_SIZE);
d16005f8
PC
1902}
1903
1904void lightrec_set_invalidate_mode(struct lightrec_state *state, bool dma_only)
1905{
1906 if (state->invalidate_from_dma_only != dma_only)
1907 lightrec_invalidate_all(state);
1908
1909 state->invalidate_from_dma_only = dma_only;
1910}
1911
1912void lightrec_set_exit_flags(struct lightrec_state *state, u32 flags)
1913{
1914 if (flags != LIGHTREC_EXIT_NORMAL) {
1915 state->exit_flags |= flags;
1916 state->target_cycle = state->current_cycle;
1917 }
1918}
1919
1920u32 lightrec_exit_flags(struct lightrec_state *state)
1921{
1922 return state->exit_flags;
1923}
1924
d16005f8
PC
1925u32 lightrec_current_cycle_count(const struct lightrec_state *state)
1926{
1927 return state->current_cycle;
1928}
1929
1930void lightrec_reset_cycle_count(struct lightrec_state *state, u32 cycles)
1931{
1932 state->current_cycle = cycles;
1933
1934 if (state->target_cycle < cycles)
1935 state->target_cycle = cycles;
1936}
1937
1938void lightrec_set_target_cycle_count(struct lightrec_state *state, u32 cycles)
1939{
1940 if (state->exit_flags == LIGHTREC_EXIT_NORMAL) {
1941 if (cycles < state->current_cycle)
1942 cycles = state->current_cycle;
1943
1944 state->target_cycle = cycles;
1945 }
1946}
98fa08a5
PC
1947
1948struct lightrec_registers * lightrec_get_registers(struct lightrec_state *state)
1949{
1950 return &state->regs;
1951}