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