-
Notifications
You must be signed in to change notification settings - Fork 9
/
lightrec.c
2176 lines (1745 loc) · 52.8 KB
/
lightrec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* Copyright (C) 2014-2021 Paul Cercueil <[email protected]>
*/
#include "arch.h"
#include "blockcache.h"
#include "debug.h"
#include "disassembler.h"
#include "emitter.h"
#include "interpreter.h"
#include "lightrec-config.h"
#include "lightning-wrapper.h"
#include "lightrec.h"
#include "memmanager.h"
#include "reaper.h"
#include "recompiler.h"
#include "regcache.h"
#include "optimizer.h"
#include "tlsf/tlsf.h"
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#if ENABLE_THREADED_COMPILER
#include <stdatomic.h>
#endif
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
static struct block * lightrec_precompile_block(struct lightrec_state *state,
u32 pc);
static bool lightrec_block_is_fully_tagged(const struct block *block);
static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data);
static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg);
static void lightrec_reap_block(struct lightrec_state *state, void *data);
static void lightrec_default_sb(struct lightrec_state *state, u32 opcode,
void *host, u32 addr, u32 data)
{
*(u8 *)host = (u8)data;
if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
lightrec_invalidate(state, addr, 1);
}
static void lightrec_default_sh(struct lightrec_state *state, u32 opcode,
void *host, u32 addr, u32 data)
{
*(u16 *)host = HTOLE16((u16)data);
if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
lightrec_invalidate(state, addr, 2);
}
static void lightrec_default_sw(struct lightrec_state *state, u32 opcode,
void *host, u32 addr, u32 data)
{
*(u32 *)host = HTOLE32(data);
if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
lightrec_invalidate(state, addr, 4);
}
static u8 lightrec_default_lb(struct lightrec_state *state,
u32 opcode, void *host, u32 addr)
{
return *(u8 *)host;
}
static u16 lightrec_default_lh(struct lightrec_state *state,
u32 opcode, void *host, u32 addr)
{
return LE16TOH(*(u16 *)host);
}
static u32 lightrec_default_lw(struct lightrec_state *state,
u32 opcode, void *host, u32 addr)
{
return LE32TOH(*(u32 *)host);
}
static u32 lightrec_default_lwu(struct lightrec_state *state,
u32 opcode, void *host, u32 addr)
{
u32 val;
memcpy(&val, host, 4);
return LE32TOH(val);
}
static void lightrec_default_swu(struct lightrec_state *state, u32 opcode,
void *host, u32 addr, u32 data)
{
data = HTOLE32(data);
memcpy(host, &data, 4);
if (!(state->opt_flags & LIGHTREC_OPT_INV_DMA_ONLY))
lightrec_invalidate(state, addr & ~0x3, 8);
}
static const struct lightrec_mem_map_ops lightrec_default_ops = {
.sb = lightrec_default_sb,
.sh = lightrec_default_sh,
.sw = lightrec_default_sw,
.lb = lightrec_default_lb,
.lh = lightrec_default_lh,
.lw = lightrec_default_lw,
.lwu = lightrec_default_lwu,
.swu = lightrec_default_swu,
};
static void __segfault_cb(struct lightrec_state *state, u32 addr,
const struct block *block)
{
lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
pr_err("Segmentation fault in recompiled code: invalid "
"load/store at address "PC_FMT"\n", addr);
if (block)
pr_err("Was executing block "PC_FMT"\n", block->pc);
}
static void lightrec_swl(struct lightrec_state *state,
const struct lightrec_mem_map_ops *ops,
u32 opcode, void *host, u32 addr, u32 data)
{
unsigned int shift = addr & 0x3;
unsigned int mask = shift < 3 ? GENMASK(31, (shift + 1) * 8) : 0;
u32 old_data;
/* Align to 32 bits */
addr &= ~3;
host = (void *)((uintptr_t)host & ~3);
old_data = ops->lw(state, opcode, host, addr);
data = (data >> ((3 - shift) * 8)) | (old_data & mask);
ops->sw(state, opcode, host, addr, data);
}
static void lightrec_swr(struct lightrec_state *state,
const struct lightrec_mem_map_ops *ops,
u32 opcode, void *host, u32 addr, u32 data)
{
unsigned int shift = addr & 0x3;
unsigned int mask = (1 << (shift * 8)) - 1;
u32 old_data;
/* Align to 32 bits */
addr &= ~3;
host = (void *)((uintptr_t)host & ~3);
old_data = ops->lw(state, opcode, host, addr);
data = (data << (shift * 8)) | (old_data & mask);
ops->sw(state, opcode, host, addr, data);
}
static void lightrec_swc2(struct lightrec_state *state, union code op,
const struct lightrec_mem_map_ops *ops,
void *host, u32 addr)
{
u32 data = lightrec_mfc2(state, op.i.rt);
ops->sw(state, op.opcode, host, addr, data);
}
static u32 lightrec_lwl(struct lightrec_state *state,
const struct lightrec_mem_map_ops *ops,
u32 opcode, void *host, u32 addr, u32 data)
{
unsigned int shift = addr & 0x3;
unsigned int mask = (1 << (24 - shift * 8)) - 1;
u32 old_data;
/* Align to 32 bits */
addr &= ~3;
host = (void *)((uintptr_t)host & ~3);
old_data = ops->lw(state, opcode, host, addr);
return (data & mask) | (old_data << (24 - shift * 8));
}
static u32 lightrec_lwr(struct lightrec_state *state,
const struct lightrec_mem_map_ops *ops,
u32 opcode, void *host, u32 addr, u32 data)
{
unsigned int shift = addr & 0x3;
unsigned int mask = shift ? GENMASK(31, 32 - shift * 8) : 0;
u32 old_data;
/* Align to 32 bits */
addr &= ~3;
host = (void *)((uintptr_t)host & ~3);
old_data = ops->lw(state, opcode, host, addr);
return (data & mask) | (old_data >> (shift * 8));
}
static void lightrec_lwc2(struct lightrec_state *state, union code op,
const struct lightrec_mem_map_ops *ops,
void *host, u32 addr)
{
u32 data = ops->lw(state, op.opcode, host, addr);
lightrec_mtc2(state, op.i.rt, data);
}
static void lightrec_invalidate_map(struct lightrec_state *state,
const struct lightrec_mem_map *map, u32 addr, u32 len)
{
if (map == &state->maps[PSX_MAP_KERNEL_USER_RAM]) {
memset(lut_address(state, lut_offset(addr)), 0,
((len + 3) / 4) * lut_elm_size(state));
}
}
static enum psx_map
lightrec_get_map_idx(struct lightrec_state *state, u32 kaddr)
{
const struct lightrec_mem_map *map;
unsigned int i;
for (i = 0; i < state->nb_maps; i++) {
map = &state->maps[i];
if (kaddr >= map->pc && kaddr < map->pc + map->length)
return (enum psx_map) i;
}
return PSX_MAP_UNKNOWN;
}
const struct lightrec_mem_map *
lightrec_get_map(struct lightrec_state *state, void **host, u32 kaddr)
{
const struct lightrec_mem_map *map;
enum psx_map idx;
u32 addr;
idx = lightrec_get_map_idx(state, kaddr);
if (idx == PSX_MAP_UNKNOWN)
return NULL;
map = &state->maps[idx];
addr = kaddr - map->pc;
while (map->mirror_of)
map = map->mirror_of;
if (host)
*host = map->address + addr;
return map;
}
u32 lightrec_rw(struct lightrec_state *state, union code op, u32 base,
u32 data, u32 *flags, struct block *block, u16 offset)
{
const struct lightrec_mem_map *map;
const struct lightrec_mem_map_ops *ops;
u32 opcode = op.opcode;
bool was_tagged = true;
u16 old_flags;
u32 addr;
void *host;
addr = kunseg(base + (s16) op.i.imm);
map = lightrec_get_map(state, &host, addr);
if (!map) {
__segfault_cb(state, addr, block);
return 0;
}
if (flags)
was_tagged = LIGHTREC_FLAGS_GET_IO_MODE(*flags);
if (likely(!map->ops)) {
if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags)) {
/* Force parallel port accesses as HW accesses, because
* the direct-I/O emitters can't differenciate it. */
if (unlikely(map == &state->maps[PSX_MAP_PARALLEL_PORT]))
*flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
/* If the base register is 0x0, be extra suspicious.
* Some games (e.g. Sled Storm) actually do segmentation
* faults by using uninitialized pointers, which are
* later initialized to point to hardware registers. */
else if (op.i.rs && base == 0x0)
*flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
else
*flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_DIRECT);
}
ops = &lightrec_default_ops;
} else if (flags &&
LIGHTREC_FLAGS_GET_IO_MODE(*flags) == LIGHTREC_IO_DIRECT_HW) {
ops = &lightrec_default_ops;
} else {
if (flags && !LIGHTREC_FLAGS_GET_IO_MODE(*flags))
*flags |= LIGHTREC_IO_MODE(LIGHTREC_IO_HW);
ops = map->ops;
}
if (!was_tagged) {
old_flags = block_set_flags(block, BLOCK_SHOULD_RECOMPILE);
if (!(old_flags & BLOCK_SHOULD_RECOMPILE)) {
pr_debug("Opcode of block at "PC_FMT" has been tagged"
" - flag for recompilation\n", block->pc);
lut_write(state, lut_offset(block->pc), NULL);
}
}
switch (op.i.op) {
case OP_SB:
ops->sb(state, opcode, host, addr, data);
return 0;
case OP_SH:
ops->sh(state, opcode, host, addr, data);
return 0;
case OP_SWL:
lightrec_swl(state, ops, opcode, host, addr, data);
return 0;
case OP_SWR:
lightrec_swr(state, ops, opcode, host, addr, data);
return 0;
case OP_SW:
ops->sw(state, opcode, host, addr, data);
return 0;
case OP_SWC2:
lightrec_swc2(state, op, ops, host, addr);
return 0;
case OP_LB:
return (s32) (s8) ops->lb(state, opcode, host, addr);
case OP_LBU:
return ops->lb(state, opcode, host, addr);
case OP_LH:
return (s32) (s16) ops->lh(state, opcode, host, addr);
case OP_LHU:
return ops->lh(state, opcode, host, addr);
case OP_LWC2:
lightrec_lwc2(state, op, ops, host, addr);
return 0;
case OP_LWL:
return lightrec_lwl(state, ops, opcode, host, addr, data);
case OP_LWR:
return lightrec_lwr(state, ops, opcode, host, addr, data);
case OP_META_LWU:
return ops->lwu(state, opcode, host, addr);
case OP_META_SWU:
ops->swu(state, opcode, host, addr, data);
return 0;
case OP_LW:
default:
return ops->lw(state, opcode, host, addr);
}
}
static void lightrec_rw_helper(struct lightrec_state *state,
union code op, u32 *flags,
struct block *block, u16 offset)
{
u32 ret = lightrec_rw(state, op, state->regs.gpr[op.i.rs],
state->regs.gpr[op.i.rt], flags, block, offset);
switch (op.i.op) {
case OP_LB:
case OP_LBU:
case OP_LH:
case OP_LHU:
case OP_LWL:
case OP_LWR:
case OP_LW:
case OP_META_LWU:
if (OPT_HANDLE_LOAD_DELAYS && unlikely(!state->in_delay_slot_n)) {
state->temp_reg = ret;
state->in_delay_slot_n = 0xff;
} else if (op.i.rt) {
state->regs.gpr[op.i.rt] = ret;
}
fallthrough;
default:
break;
}
}
static void lightrec_rw_cb(struct lightrec_state *state, u32 arg)
{
lightrec_rw_helper(state, (union code) arg, NULL, NULL, 0);
}
static void lightrec_rw_generic_cb(struct lightrec_state *state, u32 arg)
{
struct block *block;
struct opcode *op;
u16 offset = (u16)arg;
block = lightrec_find_block_from_lut(state->block_cache,
arg >> 16, state->curr_pc);
if (unlikely(!block)) {
pr_err("rw_generic: No block found in LUT for "PC_FMT" offset 0x%"PRIx16"\n",
state->curr_pc, offset);
lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
return;
}
op = &block->opcode_list[offset];
lightrec_rw_helper(state, op->c, &op->flags, block, offset);
}
static u32 clamp_s32(s32 val, s32 min, s32 max)
{
return val < min ? min : val > max ? max : val;
}
static u16 load_u16(u32 *ptr)
{
return ((struct u16x2 *) ptr)->l;
}
static void store_u16(u32 *ptr, u16 value)
{
((struct u16x2 *) ptr)->l = value;
}
static u32 lightrec_mfc2(struct lightrec_state *state, u8 reg)
{
s16 gteir1, gteir2, gteir3;
switch (reg) {
case 1:
case 3:
case 5:
case 8:
case 9:
case 10:
case 11:
return (s32)(s16) load_u16(&state->regs.cp2d[reg]);
case 7:
case 16:
case 17:
case 18:
case 19:
return load_u16(&state->regs.cp2d[reg]);
case 28:
case 29:
gteir1 = (s16) load_u16(&state->regs.cp2d[9]);
gteir2 = (s16) load_u16(&state->regs.cp2d[10]);
gteir3 = (s16) load_u16(&state->regs.cp2d[11]);
return clamp_s32(gteir1 >> 7, 0, 0x1f) << 0 |
clamp_s32(gteir2 >> 7, 0, 0x1f) << 5 |
clamp_s32(gteir3 >> 7, 0, 0x1f) << 10;
case 15:
reg = 14;
fallthrough;
default:
return state->regs.cp2d[reg];
}
}
u32 lightrec_mfc(struct lightrec_state *state, union code op)
{
u32 val;
if (op.i.op == OP_CP0)
return state->regs.cp0[op.r.rd];
if (op.i.op == OP_SWC2) {
val = lightrec_mfc2(state, op.i.rt);
} else if (op.r.rs == OP_CP2_BASIC_MFC2)
val = lightrec_mfc2(state, op.r.rd);
else {
val = state->regs.cp2c[op.r.rd];
switch (op.r.rd) {
case 4:
case 12:
case 20:
case 26:
case 27:
case 29:
case 30:
val = (u32)(s16)val;
fallthrough;
default:
break;
}
}
if (state->ops.cop2_notify)
(*state->ops.cop2_notify)(state, op.opcode, val);
return val;
}
static void lightrec_mfc_cb(struct lightrec_state *state, union code op)
{
u32 rt = lightrec_mfc(state, op);
if (op.i.op == OP_SWC2)
state->temp_reg = rt;
else if (op.r.rt)
state->regs.gpr[op.r.rt] = rt;
}
static void lightrec_mtc0(struct lightrec_state *state, u8 reg, u32 data)
{
u32 status, oldstatus, cause;
switch (reg) {
case 1:
case 4:
case 8:
case 14:
case 15:
/* Those registers are read-only */
return;
default:
break;
}
if (reg == 12) {
status = state->regs.cp0[12];
oldstatus = status;
if (status & ~data & BIT(16)) {
state->ops.enable_ram(state, true);
lightrec_invalidate_all(state);
} else if (~status & data & BIT(16)) {
state->ops.enable_ram(state, false);
}
}
if (reg == 13) {
state->regs.cp0[13] &= ~0x300;
state->regs.cp0[13] |= data & 0x300;
} else {
state->regs.cp0[reg] = data;
}
if (reg == 12 || reg == 13) {
cause = state->regs.cp0[13];
status = state->regs.cp0[12];
/* Handle software interrupts */
if ((!!(status & cause & 0x300)) & status)
lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
/* Handle hardware interrupts */
if (reg == 12 && !(~status & 0x401) && (~oldstatus & 0x401))
lightrec_set_exit_flags(state, LIGHTREC_EXIT_CHECK_INTERRUPT);
}
}
static u32 count_leading_bits(s32 data)
{
u32 cnt = 33;
#ifdef __has_builtin
#if __has_builtin(__builtin_clrsb)
return 1 + __builtin_clrsb(data);
#endif
#endif
data = (data ^ (data >> 31)) << 1;
do {
cnt -= 1;
data >>= 1;
} while (data);
return cnt;
}
static void lightrec_mtc2(struct lightrec_state *state, u8 reg, u32 data)
{
switch (reg) {
case 15:
state->regs.cp2d[12] = state->regs.cp2d[13];
state->regs.cp2d[13] = state->regs.cp2d[14];
state->regs.cp2d[14] = data;
break;
case 28:
state->regs.cp2d[9] = (data << 7) & 0xf80;
state->regs.cp2d[10] = (data << 2) & 0xf80;
state->regs.cp2d[11] = (data >> 3) & 0xf80;
break;
case 31:
return;
case 30:
state->regs.cp2d[31] = count_leading_bits((s32) data);
fallthrough;
default:
state->regs.cp2d[reg] = data;
break;
}
}
static void lightrec_ctc2(struct lightrec_state *state, u8 reg, u32 data)
{
switch (reg) {
case 4:
case 12:
case 20:
case 26:
case 27:
case 29:
case 30:
store_u16(&state->regs.cp2c[reg], data);
break;
case 31:
data = (data & 0x7ffff000) | !!(data & 0x7f87e000) << 31;
fallthrough;
default:
state->regs.cp2c[reg] = data;
break;
}
}
void lightrec_mtc(struct lightrec_state *state, union code op, u8 reg, u32 data)
{
if (op.i.op == OP_CP0) {
lightrec_mtc0(state, reg, data);
} else {
if (op.i.op == OP_LWC2 || op.r.rs != OP_CP2_BASIC_CTC2)
lightrec_mtc2(state, reg, data);
else
lightrec_ctc2(state, reg, data);
if (state->ops.cop2_notify)
(*state->ops.cop2_notify)(state, op.opcode, data);
}
}
static void lightrec_mtc_cb(struct lightrec_state *state, u32 arg)
{
union code op = (union code) arg;
u32 data;
u8 reg;
if (op.i.op == OP_LWC2) {
data = state->temp_reg;
reg = op.i.rt;
} else {
data = state->regs.gpr[op.r.rt];
reg = op.r.rd;
}
lightrec_mtc(state, op, reg, data);
}
void lightrec_rfe(struct lightrec_state *state)
{
u32 status;
/* Read CP0 Status register (r12) */
status = state->regs.cp0[12];
/* Switch the bits */
status = ((status & 0x3c) >> 2) | (status & ~0xf);
/* Write it back */
lightrec_mtc0(state, 12, status);
}
void lightrec_cp(struct lightrec_state *state, union code op)
{
if (op.i.op == OP_CP0) {
pr_err("Invalid CP opcode to coprocessor #0\n");
return;
}
(*state->ops.cop2_op)(state, op.opcode);
}
static void lightrec_cp_cb(struct lightrec_state *state, u32 arg)
{
lightrec_cp(state, (union code) arg);
}
static struct block * lightrec_get_block(struct lightrec_state *state, u32 pc)
{
struct block *block = lightrec_find_block(state->block_cache, pc);
u8 old_flags;
if (block && lightrec_block_is_outdated(state, block)) {
pr_debug("Block at "PC_FMT" is outdated!\n", block->pc);
old_flags = block_set_flags(block, BLOCK_IS_DEAD);
if (!(old_flags & BLOCK_IS_DEAD)) {
/* Make sure the recompiler isn't processing the block
* we'll destroy */
if (ENABLE_THREADED_COMPILER)
lightrec_recompiler_remove(state->rec, block);
remove_from_code_lut(state->block_cache, block);
if (ENABLE_THREADED_COMPILER) {
lightrec_reaper_add(state->reaper,
lightrec_reap_block, block);
} else {
lightrec_unregister_block(state->block_cache, block);
lightrec_free_block(state, block);
}
}
block = NULL;
}
if (!block) {
block = lightrec_precompile_block(state, pc);
if (!block) {
pr_err("Unable to recompile block at "PC_FMT"\n", pc);
lightrec_set_exit_flags(state, LIGHTREC_EXIT_SEGFAULT);
return NULL;
}
lightrec_register_block(state->block_cache, block);
}
return block;
}
static void * get_next_block_func(struct lightrec_state *state, u32 pc)
{
struct block *block;
bool should_recompile;
void *func;
int err;
do {
func = lut_read(state, lut_offset(pc));
if (func && func != state->get_next_block)
break;
block = lightrec_get_block(state, pc);
if (unlikely(!block))
break;
if (OPT_REPLACE_MEMSET &&
block_has_flag(block, BLOCK_IS_MEMSET)) {
func = state->memset_func;
break;
}
should_recompile = block_has_flag(block, BLOCK_SHOULD_RECOMPILE) &&
!block_has_flag(block, BLOCK_NEVER_COMPILE) &&
!block_has_flag(block, BLOCK_IS_DEAD);
if (unlikely(should_recompile)) {
pr_debug("Block at "PC_FMT" should recompile\n", pc);
if (ENABLE_THREADED_COMPILER) {
lightrec_recompiler_add(state->rec, block);
} else {
err = lightrec_compile_block(state->cstate, block);
if (err) {
state->exit_flags = LIGHTREC_EXIT_NOMEM;
return NULL;
}
}
}
if (ENABLE_THREADED_COMPILER && likely(!should_recompile))
func = lightrec_recompiler_run_first_pass(state, block, &pc);
else
func = block->function;
if (likely(func))
break;
if (unlikely(block_has_flag(block, BLOCK_NEVER_COMPILE))) {
pc = lightrec_emulate_block(state, block, pc);
} else if (!ENABLE_THREADED_COMPILER) {
/* Block wasn't compiled yet - run the interpreter */
if (block_has_flag(block, BLOCK_FULLY_TAGGED))
pr_debug("Block fully tagged, skipping first pass\n");
else if (ENABLE_FIRST_PASS && likely(!should_recompile))
pc = lightrec_emulate_block(state, block, pc);
/* Then compile it using the profiled data */
err = lightrec_compile_block(state->cstate, block);
if (err) {
state->exit_flags = LIGHTREC_EXIT_NOMEM;
return NULL;
}
} else if (unlikely(block_has_flag(block, BLOCK_IS_DEAD))) {
/*
* If the block is dead but has never been compiled,
* then its function pointer is NULL and we cannot
* execute the block. In that case, reap all the dead
* blocks now, and in the next loop we will create a
* new block.
*/
lightrec_reaper_reap(state->reaper);
} else {
lightrec_recompiler_add(state->rec, block);
}
} while (state->exit_flags == LIGHTREC_EXIT_NORMAL
&& state->current_cycle < state->target_cycle);
state->curr_pc = pc;
return func;
}
static void * lightrec_alloc_code(struct lightrec_state *state, size_t size)
{
void *code;
if (ENABLE_THREADED_COMPILER)
lightrec_code_alloc_lock(state);
code = tlsf_malloc(state->tlsf, size);
if (ENABLE_THREADED_COMPILER)
lightrec_code_alloc_unlock(state);
return code;
}
static void lightrec_realloc_code(struct lightrec_state *state,
void *ptr, size_t size)
{
/* NOTE: 'size' MUST be smaller than the size specified during
* the allocation. */
if (ENABLE_THREADED_COMPILER)
lightrec_code_alloc_lock(state);
tlsf_realloc(state->tlsf, ptr, size);
if (ENABLE_THREADED_COMPILER)
lightrec_code_alloc_unlock(state);
}
static void lightrec_free_code(struct lightrec_state *state, void *ptr)
{
if (ENABLE_THREADED_COMPILER)
lightrec_code_alloc_lock(state);
tlsf_free(state->tlsf, ptr);
if (ENABLE_THREADED_COMPILER)
lightrec_code_alloc_unlock(state);
}
static char lightning_code_data[0x80000];
static void * lightrec_emit_code(struct lightrec_state *state,
const struct block *block,
jit_state_t *_jit, unsigned int *size)
{
bool has_code_buffer = ENABLE_CODE_BUFFER && state->tlsf;
jit_word_t code_size, new_code_size;
void *code;
jit_realize();
if (ENABLE_DISASSEMBLER)
jit_set_data(lightning_code_data, sizeof(lightning_code_data), 0);
else
jit_set_data(NULL, 0, JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
if (has_code_buffer) {
jit_get_code(&code_size);
#ifdef __i386__
/* Lightning's code size estimation routine is buggy on x86 and
* will return a value that's too small. */
code_size *= 2;
#endif
code = lightrec_alloc_code(state, (size_t) code_size);
if (!code) {
if (ENABLE_THREADED_COMPILER) {
/* If we're using the threaded compiler, return
* an allocation error here. The threaded
* compiler will then empty its job queue and
* request a code flush using the reaper. */
return NULL;
}
/* Remove outdated blocks, and try again */
lightrec_remove_outdated_blocks(state->block_cache, block);
pr_debug("Re-try to alloc %zu bytes...\n", code_size);
code = lightrec_alloc_code(state, code_size);
if (!code) {
pr_err("Could not alloc even after removing old blocks!\n");
return NULL;
}
}
jit_set_code(code, code_size);
}
code = jit_emit();
if (!code) {
if (has_code_buffer)
lightrec_free_code(state, code);
return NULL;
}
jit_get_code(&new_code_size);
lightrec_register(MEM_FOR_CODE, new_code_size);
if (has_code_buffer) {
lightrec_realloc_code(state, code, (size_t) new_code_size);
pr_debug("Creating code block at address 0x%" PRIxPTR ", "
"code size: %" PRIuPTR " new: %" PRIuPTR "\n",
(uintptr_t) code, code_size, new_code_size);
}
*size = (unsigned int) new_code_size;
if (state->ops.code_inv)
state->ops.code_inv(code, new_code_size);
return code;
}
static struct block * generate_wrapper(struct lightrec_state *state)
{
struct block *block;
jit_state_t *_jit;
unsigned int i;
block = lightrec_malloc(state, MEM_FOR_IR, sizeof(*block));
if (!block)
goto err_no_mem;
_jit = jit_new_state();
if (!_jit)
goto err_free_block;
jit_name("RW wrapper");
jit_note(__FILE__, __LINE__);
/* Wrapper entry point */
jit_prolog();
jit_tramp(256);
/* Load pointer to C wrapper */
jit_add_state(JIT_R1, JIT_R1);
jit_ldxi(JIT_R1, JIT_R1, lightrec_offset(c_wrappers));
jit_epilog();
jit_prolog();
/* Save all temporaries on stack */
for (i = 0; i < NUM_TEMPS; i++) {
if (i + FIRST_TEMP != 1) {
jit_stxi(lightrec_offset(wrapper_regs[i]),
LIGHTREC_REG_STATE, JIT_R(i + FIRST_TEMP));
}
}
jit_getarg(JIT_R2, jit_arg());
jit_prepare();
jit_pushargr(LIGHTREC_REG_STATE);
jit_pushargr(JIT_R2);
jit_ldxi_ui(JIT_R2, LIGHTREC_REG_STATE, lightrec_offset(target_cycle));
/* state->current_cycle = state->target_cycle - delta; */
jit_subr(LIGHTREC_REG_CYCLE, JIT_R2, LIGHTREC_REG_CYCLE);
jit_stxi_i(lightrec_offset(current_cycle), LIGHTREC_REG_STATE, LIGHTREC_REG_CYCLE);
/* Call the wrapper function */
jit_finishr(JIT_R1);
/* delta = state->target_cycle - state->current_cycle */;
jit_ldxi_ui(LIGHTREC_REG_CYCLE, LIGHTREC_REG_STATE, lightrec_offset(current_cycle));
jit_ldxi_ui(JIT_R1, LIGHTREC_REG_STATE, lightrec_offset(target_cycle));
jit_subr(LIGHTREC_REG_CYCLE, JIT_R1, LIGHTREC_REG_CYCLE);
/* Restore temporaries from stack */
for (i = 0; i < NUM_TEMPS; i++) {
if (i + FIRST_TEMP != 1) {
jit_ldxi(JIT_R(i + FIRST_TEMP), LIGHTREC_REG_STATE,