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