-
Notifications
You must be signed in to change notification settings - Fork 16
/
jit.c
988 lines (843 loc) · 33.2 KB
/
jit.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
#include "jit.h"
#include "bbc_options.h"
#include "cpu_driver.h"
#include "debug.h"
#include "defs_6502.h"
#include "interp.h"
#include "inturbo.h"
#include "memory_access.h"
#include "os_alloc.h"
#include "os_fault.h"
#include "jit_compiler.h"
#include "jit_metadata.h"
#include "log.h"
#include "state_6502.h"
#include "timing.h"
#include "util.h"
#include "asm/asm_common.h"
#include "asm/asm_defs_host.h"
#include "asm/asm_inturbo.h"
#include "asm/asm_inturbo_defs.h"
#include "asm/asm_jit.h"
#include "asm/asm_jit_defs.h"
#include <assert.h>
#include <inttypes.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void* g_p_jit_base = (void*) NULL;
struct jit_struct {
/* Fields referenced by the JIT code. */
struct cpu_driver driver;
/* C callbacks called by JIT code. */
void* p_compile_callback;
/* C pointers used by JIT code. */
struct inturbo_struct* p_inturbo;
/* 6502 address -> JIT code pointers.
* These are stored as 32-bit even when the mapping is hosted in 64-bit
* space. It's hoped to be more cache efficient.
*/
uint32_t jit_ptrs[k_6502_addr_space_size];
/* Context pointer for JIT-defined custom callbacks. */
void* p_jit_callback_context;
/* Fields not referenced by JIT code. */
struct asm_jit_struct* p_asm;
struct jit_metadata* p_metadata;
struct os_alloc_mapping* p_mapping_jit;
struct os_alloc_mapping* p_mapping_no_code_ptr;
uint8_t* p_jit_base;
struct jit_compiler* p_compiler;
struct util_buffer* p_temp_buf;
struct interp_struct* p_interp;
uint8_t* p_opcode_types;
uint8_t* p_opcode_modes;
uint8_t* p_opcode_mem;
uint8_t* p_opcode_cycles;
uint64_t last_housekeeping_cycles;
int log_compile;
int log_fault;
uint64_t counter_num_compiles;
uint64_t counter_num_interps;
uint64_t counter_num_faults;
int do_fault_log;
};
static int
jit_interp_instruction_callback(void* p,
uint16_t next_pc,
uint8_t done_opcode,
uint16_t done_addr,
int next_is_irq,
int irq_pending) {
int32_t next_block;
int32_t next_block_prev;
uint8_t opmem;
struct jit_metadata* p_metadata;
struct jit_struct* p_jit = (struct jit_struct*) p;
p_metadata = p_jit->p_metadata;
opmem = p_jit->p_opcode_mem[done_opcode];
/* Any memory writes executed by the interpreter need to invalidate
* compiled JIT code if they're self-modifying writes.
*/
if ((opmem & k_opmem_write_flag) &&
jit_metadata_is_pc_in_code_block(p_metadata, done_addr)) {
void* p_jit_ptr = jit_metadata_get_host_jit_ptr(p_metadata, done_addr);
if (!jit_metadata_is_jit_ptr_no_code(p_metadata, p_jit_ptr) &&
!jit_metadata_is_jit_ptr_dynamic(p_metadata, p_jit_ptr)) {
asm_jit_start_code_updates(p_jit->p_asm, p_jit_ptr, 4);
asm_jit_invalidate_code_at(p_jit_ptr);
asm_jit_finish_code_updates(p_jit->p_asm);
}
}
if (next_is_irq || irq_pending) {
/* Keep interpreting to handle the IRQ. */
return 0;
}
/* We stay in interp indefinitely if we're syncing the 6502 writes to video
* 6845 reads. This is denoted by the presence of a memory written handler.
*/
if (interp_has_memory_written_callback(p_jit->p_interp)) {
return 0;
}
next_block = jit_metadata_get_code_block(p_metadata, next_pc);
if (next_block == -1) {
/* Always consider an address with no JIT code to be a new block
* boundary. Without this, an RTI to an uncompiled region will stay stuck
* in the interpreter.
*/
return 1;
}
next_block_prev = jit_metadata_get_code_block(p_metadata, (next_pc - 1));
if (next_block != next_block_prev) {
/* If the instruction we're about to execute is at the start of a JIT
* block, bounce back into JIT at this clean boundary.
*/
return 1;
}
/* Keep interpreting. */
return 0;
}
struct jit_enter_interp_ret {
int64_t countdown;
int64_t exited;
};
static void
jit_enter_interp(struct jit_struct* p_jit,
struct jit_enter_interp_ret* p_ret,
int64_t countdown,
uint64_t host_flags) {
uint32_t cpu_driver_flags;
struct cpu_driver* p_jit_cpu_driver = &p_jit->driver;
struct jit_compiler* p_compiler = p_jit->p_compiler;
struct interp_struct* p_interp = p_jit->p_interp;
struct state_6502* p_state_6502 = p_jit_cpu_driver->abi.p_state_6502;
p_jit->counter_num_interps++;
/* Take care of any deferred fault logging. */
if (p_jit->do_fault_log) {
p_jit->do_fault_log = 0;
log_do_log(k_log_jit, k_log_info, "JIT handled fault (log every 10k)");
}
/* Bouncing out of the JIT is quite jarring. We need to fixup up any state
* that was temporarily stale due to optimizations.
*/
countdown = jit_compiler_fixup_state(p_compiler,
p_state_6502,
countdown,
host_flags,
0);
countdown = interp_enter_with_countdown(p_interp, countdown);
cpu_driver_flags = p_jit_cpu_driver->p_funcs->get_flags(p_jit_cpu_driver);
p_ret->countdown = countdown;
p_ret->exited = !!(cpu_driver_flags & k_cpu_flag_exited);
}
static void
jit_destroy(struct cpu_driver* p_cpu_driver) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct cpu_driver* p_inturbo_cpu_driver =
(struct cpu_driver*) p_jit->p_inturbo;
struct cpu_driver* p_interp_cpu_driver = (struct cpu_driver*) p_jit->p_interp;
jit_metadata_destroy(p_jit->p_metadata);
asm_jit_destroy(p_jit->p_asm);
os_alloc_free_mapping(p_jit->p_mapping_no_code_ptr);
if (p_inturbo_cpu_driver != NULL) {
p_inturbo_cpu_driver->p_funcs->destroy(p_inturbo_cpu_driver);
}
p_interp_cpu_driver->p_funcs->destroy(p_interp_cpu_driver);
util_buffer_destroy(p_jit->p_temp_buf);
jit_compiler_destroy(p_jit->p_compiler);
os_alloc_free_mapping(p_jit->p_mapping_jit);
os_alloc_free_aligned(p_cpu_driver);
}
static int
jit_enter(struct cpu_driver* p_cpu_driver) {
int exited;
int64_t countdown;
struct timing_struct* p_timing = p_cpu_driver->p_extra->p_timing;
struct state_6502* p_state_6502 = p_cpu_driver->abi.p_state_6502;
uint16_t addr_6502 = state_6502_get_pc(p_state_6502);
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct jit_metadata* p_metadata = p_jit->p_metadata;
void* p_start_addr = jit_metadata_get_host_block_address(p_metadata,
addr_6502);
void* p_mem_base = (void*) K_BBC_MEM_READ_IND_ADDR;
countdown = timing_get_countdown(p_timing);
/* The memory must be aligned to at least 0x100 so that our register access
* tricks work.
*/
assert(((uintptr_t) p_mem_base & 0xff) == 0);
exited = asm_jit_enter(p_jit, p_start_addr, countdown, p_mem_base);
assert(exited == 1);
return exited;
}
static void
jit_set_reset_callback(struct cpu_driver* p_cpu_driver,
void (*do_reset_callback)(void* p, uint32_t flags),
void* p_do_reset_callback_object) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct cpu_driver* p_interp_driver = (struct cpu_driver*) p_jit->p_interp;
p_interp_driver->p_funcs->set_reset_callback(p_interp_driver,
do_reset_callback,
p_do_reset_callback_object);
}
static void
jit_set_memory_written_callback(struct cpu_driver* p_cpu_driver,
void (*memory_written_callback)(void* p),
void* p_memory_written_callback_object) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct cpu_driver* p_interp_driver = (struct cpu_driver*) p_jit->p_interp;
p_interp_driver->p_funcs->set_memory_written_callback(
p_interp_driver,
memory_written_callback,
p_memory_written_callback_object);
}
static void
jit_apply_flags(struct cpu_driver* p_cpu_driver,
uint32_t flags_set,
uint32_t flags_clear) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct cpu_driver* p_interp_driver = (struct cpu_driver*) p_jit->p_interp;
p_interp_driver->p_funcs->apply_flags(p_interp_driver,
flags_set,
flags_clear);
}
static uint32_t
jit_get_flags(struct cpu_driver* p_cpu_driver) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct cpu_driver* p_interp_driver = (struct cpu_driver*) p_jit->p_interp;
return p_interp_driver->p_funcs->get_flags(p_interp_driver);
}
static uint32_t
jit_get_exit_value(struct cpu_driver* p_cpu_driver) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct cpu_driver* p_interp_driver = (struct cpu_driver*) p_jit->p_interp;
return p_interp_driver->p_funcs->get_exit_value(p_interp_driver);
}
static void
jit_set_exit_value(struct cpu_driver* p_cpu_driver, uint32_t exit_value) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct cpu_driver* p_interp_driver = (struct cpu_driver*) p_jit->p_interp;
p_interp_driver->p_funcs->set_exit_value(p_interp_driver, exit_value);
}
static void
jit_memory_range_invalidate(struct cpu_driver* p_cpu_driver,
uint16_t addr_6502,
uint32_t len) {
uint32_t i;
void* p_block_ptr;
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct jit_metadata* p_metadata = p_jit->p_metadata;
uint32_t addr_end_6502 = (addr_6502 + len);
assert(len <= k_6502_addr_space_size);
assert(addr_end_6502 <= k_6502_addr_space_size);
if (p_jit->log_compile) {
log_do_log(k_log_jit,
k_log_info,
"invalidate range $%.4X-$%.4X",
addr_6502,
(addr_end_6502 - 1));
}
assert(addr_end_6502 >= addr_6502);
p_block_ptr = jit_metadata_get_host_block_address(p_metadata, addr_6502);
asm_jit_start_code_updates(p_jit->p_asm,
p_block_ptr,
(len * K_JIT_BYTES_PER_BYTE));
for (i = addr_6502; i < addr_end_6502; ++i) {
int32_t code_block = jit_metadata_get_code_block(p_metadata, i);
/* We assume we're not executing in the middle of a JIT block. Therefore,
* we can invalidate the entire range simply by making sure the very
* start of every code block is invalidated. This is possible now that
* the invalidation pointer is inclusive of any block countdown prefix.
*/
if (code_block == (int32_t) i) {
void* p_jit_ptr = jit_metadata_get_host_jit_ptr(p_metadata, i);
asm_jit_invalidate_code_at(p_jit_ptr);
}
if (code_block != -1) {
jit_metadata_set_code_block(p_metadata, i, -1);
}
jit_metadata_make_jit_ptr_no_code(p_metadata, i);
}
asm_jit_finish_code_updates(p_jit->p_asm);
jit_compiler_memory_range_invalidate(p_jit->p_compiler, addr_6502, len);
}
static char*
jit_get_address_info(struct cpu_driver* p_cpu_driver, uint16_t addr) {
static char block_addr_buf[5];
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct jit_metadata* p_metadata = p_jit->p_metadata;
int32_t block_addr_6502 = jit_metadata_get_code_block(p_metadata, addr);
(void) snprintf(block_addr_buf,
sizeof(block_addr_buf),
"%.4X",
(uint16_t) block_addr_6502);
return block_addr_buf;
}
static void
jit_get_custom_counters(struct cpu_driver* p_cpu_driver,
uint64_t* p_c1,
uint64_t* p_c2) {
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
*p_c1 = p_jit->counter_num_compiles;
*p_c2 = p_jit->counter_num_interps;
}
static void
jit_check_code_block(struct jit_struct* p_jit, uint16_t block_addr_6502) {
struct jit_metadata* p_metadata = p_jit->p_metadata;
int32_t code_block = jit_metadata_get_code_block(p_metadata, block_addr_6502);
int has_invalidations = 0;
uint16_t addr_6502;
void* p_block_ptr;
assert(code_block == block_addr_6502);
addr_6502 = block_addr_6502;
while (code_block == block_addr_6502) {
void* p_jit_ptr = jit_metadata_get_host_jit_ptr(p_metadata, addr_6502);
assert(!jit_metadata_is_jit_ptr_no_code(p_metadata, p_jit_ptr));
if (jit_metadata_is_jit_ptr_dynamic(p_metadata, p_jit_ptr)) {
/* No action. */
} else if (asm_jit_is_invalidated_code_at(p_jit_ptr)) {
has_invalidations = 1;
break;
}
addr_6502++;
code_block = jit_metadata_get_code_block(p_metadata, addr_6502);
}
if (!has_invalidations) {
return;
}
if (p_jit->log_compile) {
log_do_log(k_log_jit,
k_log_info,
"stale code block at $%.4X",
block_addr_6502);
}
/* Clear out jit pointers and the code block metadata. */
addr_6502 = block_addr_6502;
code_block = jit_metadata_get_code_block(p_metadata, addr_6502);
while (code_block == block_addr_6502) {
void* p_jit_ptr = jit_metadata_get_host_jit_ptr(p_metadata, addr_6502);
assert(!jit_metadata_is_jit_ptr_no_code(p_metadata, p_jit_ptr));
if (jit_metadata_is_jit_ptr_dynamic(p_metadata, p_jit_ptr)) {
/* No action. */
} else if (asm_jit_is_invalidated_code_at(p_jit_ptr)) {
/* This stopgap measure attempts to prevent write invalidation faults on
* ARM64. If we get here, and there's an invalidated code address, it's
* probably because that code isn't ever being executed. If it was
* being executed, it would typically get compiled to a dynamic operand
* or dynamic opcode.
* We tag the address so that a dynamic opcode will be compiled, and this
* will prevent write invalidation faults.
* Examples: Thurst, Galaforce.
*/
jit_compiler_tag_address_as_dynamic(p_jit->p_compiler, addr_6502);
}
jit_metadata_make_jit_ptr_no_code(p_metadata, addr_6502);
jit_metadata_set_code_block(p_metadata, addr_6502, -1);
/* TODO: clear compiler metadata? */
addr_6502++;
code_block = jit_metadata_get_code_block(p_metadata, addr_6502);
}
/* Disable the code block itself. */
p_block_ptr = jit_metadata_get_host_block_address(p_metadata,
block_addr_6502);
asm_jit_start_code_updates(p_jit->p_asm, p_block_ptr, 4);
asm_jit_invalidate_code_at(p_block_ptr);
asm_jit_finish_code_updates(p_jit->p_asm);
}
static void
jit_cleanup_stale_code(struct jit_struct* p_jit) {
uint32_t i;
struct jit_metadata* p_metadata = p_jit->p_metadata;
int32_t curr_code_block = -1;
log_do_log(k_log_jit, k_log_info, "starting stale code sweep");
for (i = 0; i < k_6502_addr_space_size; ++i) {
int32_t next_code_block = jit_metadata_get_code_block(p_metadata, i);
if (next_code_block == curr_code_block) {
continue;
}
if (next_code_block != -1) {
jit_check_code_block(p_jit, i);
}
curr_code_block = next_code_block;
}
}
static void
jit_housekeeping_tick(struct cpu_driver* p_cpu_driver) {
static const uint64_t k_cycles_threshold = (2000000 * 60 * 5);
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct state_6502* p_state_6502 = p_cpu_driver->abi.p_state_6502;
uint64_t cycles = state_6502_get_cycles(p_state_6502);
/* Some time after 6502 reset (currently 5 minutes of virtual time), sweep
* the JIT code space and clear out any code blocks containing invalidations.
* Such code blocks are likely no longer active, but contribute an overhead,
* especially on ARM64, where writing a code invalidation pointer faults.
*/
if (p_jit->last_housekeeping_cycles < k_cycles_threshold) {
if (cycles >= k_cycles_threshold) {
jit_cleanup_stale_code(p_jit);
}
}
p_jit->last_housekeeping_cycles = cycles;
}
static int64_t
jit_compile(struct jit_struct* p_jit,
uint8_t* p_host_pc,
int64_t countdown,
uint64_t host_flags) {
uint32_t bytes_6502_compiled;
uint16_t addr_6502;
uint16_t addr_6502_end;
uint16_t addr_6502_last;
void* p_jit_block;
void* p_jit_block_end;
struct state_6502* p_state_6502 = p_jit->driver.abi.p_state_6502;
struct jit_compiler* p_compiler = p_jit->p_compiler;
struct jit_metadata* p_metadata = p_jit->p_metadata;
int32_t code_block_6502;
int is_invalidation = 0;
int has_6502_code = 0;
int is_block_continuation = 0;
int do_redo_prepare = 0;
p_jit->counter_num_compiles++;
addr_6502 = jit_metadata_get_6502_pc_from_host_pc(p_metadata, p_host_pc);
code_block_6502 = jit_metadata_get_code_block(p_metadata, addr_6502);
if (asm_jit_is_invalidated_code_at(p_host_pc)) {
if (((uintptr_t) p_host_pc & (K_JIT_BYTES_PER_BYTE - 1)) != 0) {
/* Middle of a code block. Must be an invalidation. */
is_invalidation = 1;
} else if (addr_6502 == code_block_6502) {
/* Very beginning of code block. Must be an invalidation. */
is_invalidation = 1;
}
}
/* Bouncing out of the JIT is quite jarring. We need to fixup up any state
* that was temporarily stale due to optimizations.
*/
p_state_6502->abi_state.reg_pc = addr_6502;
if (is_invalidation) {
countdown = jit_compiler_fixup_state(p_compiler,
p_state_6502,
countdown,
host_flags,
1);
}
if (p_jit->log_compile) {
has_6502_code = jit_metadata_is_pc_in_code_block(p_metadata, addr_6502);
is_block_continuation = jit_compiler_is_block_continuation(p_compiler,
addr_6502);
}
/* Get the compile bounds. */
bytes_6502_compiled = jit_compiler_prepare_compile_block(p_compiler,
is_invalidation,
addr_6502);
addr_6502_end = (addr_6502 + bytes_6502_compiled);
if ((addr_6502 < 0xFF) &&
!jit_compiler_is_compiling_for_code_in_zero_page(p_compiler)) {
log_do_log(k_log_jit,
k_log_unusual,
"compiling zero page code @$%.2X",
addr_6502);
/* Invalidate all existing compiled code because if it writes to the zero
* page, it isn't doing self-modified code correctly.
*/
jit_memory_range_invalidate(&p_jit->driver,
0,
(k_6502_addr_space_size - 1));
jit_compiler_set_compiling_for_code_in_zero_page(p_compiler, 1);
do_redo_prepare = 1;
}
if ((addr_6502 <= 0x1FF) && (addr_6502_end >= 0x100)) {
log_do_log(k_log_jit,
k_log_unimplemented,
"compiling stack page code @$%.4X; self-modify not handled",
addr_6502);
}
if (do_redo_prepare) {
/* Some compilation options changed, so-generate the compiler structures. */
bytes_6502_compiled = jit_compiler_prepare_compile_block(p_compiler,
is_invalidation,
addr_6502);
addr_6502_end = (addr_6502 + bytes_6502_compiled);
}
/* Prepare for and write out the new binary code. */
p_jit_block = jit_metadata_get_host_block_address(p_metadata, addr_6502);
p_jit_block_end =
((uint8_t*) p_jit_block + (bytes_6502_compiled * K_JIT_BYTES_PER_BYTE));
assert(p_jit_block_end <= (void*) K_JIT_ADDR_END);
asm_jit_start_code_updates(
p_jit->p_asm,
p_jit_block,
((uint8_t*) p_jit_block_end - (uint8_t*) p_jit_block));
jit_compiler_execute_compile_block(p_compiler);
asm_jit_finish_code_updates(p_jit->p_asm);
/* Handle any overlap with existing code blocks. */
if ((code_block_6502 != -1) && (code_block_6502 != addr_6502)) {
/* We're splitting a code block before, so invalidate it. */
void* p_jit_ptr = jit_metadata_get_host_block_address(p_metadata,
code_block_6502);
asm_jit_start_code_updates(p_jit->p_asm, p_jit_ptr, 4);
asm_jit_invalidate_code_at(p_jit_ptr);
asm_jit_finish_code_updates(p_jit->p_asm);
jit_metadata_clear_block(p_metadata, code_block_6502);
}
addr_6502_end = (addr_6502 + bytes_6502_compiled);
addr_6502_last = (addr_6502_end - 1);
code_block_6502 = jit_metadata_get_code_block(p_metadata, addr_6502_end);
if ((code_block_6502 != -1) && (code_block_6502 <= addr_6502_last)) {
/* We're splitting a code block after, so invalidate it. */
jit_metadata_clear_block(p_metadata, addr_6502_end);
}
if (p_jit->log_compile) {
const char* p_text;
if (is_invalidation) {
p_text = "inval";
} else if (is_block_continuation) {
p_text = "cont";
} else if (has_6502_code) {
p_text = "split";
} else {
p_text = "new";
}
log_do_log(k_log_jit,
k_log_info,
"compile @$%.4X-$%.4X [host 0x%"PRIx64"], %s at ticks %"PRIu64,
addr_6502,
addr_6502_last,
(uint64_t) p_host_pc,
p_text,
timing_get_total_timer_ticks(p_jit->driver.p_extra->p_timing));
}
return countdown;
}
static void
jit_safe_hex_convert(char* p_buf, void* p_ptr) {
size_t i;
size_t val = (size_t) p_ptr;
for (i = 0; i < 8; ++i) {
char c1 = (val & 0x0f);
char c2 = ((val & 0xf0) >> 4);
if (c1 < 10) {
c1 = '0' + c1;
} else {
c1 = 'a' + (c1 - 10);
}
if (c2 < 10) {
c2 = '0' + c2;
} else {
c2 = 'a' + (c2 - 10);
}
p_buf[16 - 2 - (i * 2) + 1] = c1;
p_buf[16 - 2 - (i * 2) ] = c2;
val >>= 8;
}
}
static void
fault_reraise(void* p_pc,
void* p_addr,
int is_illegal,
int is_write,
int is_exec) {
int ret;
char hex_buf[16];
char digit;
static const char* p_msg = "FAULT: pc ";
static const char* p_msg2 = ", addr ";
static const char* p_msg3 = ", write ";
static const char* p_msg4 = ", exec ";
static const char* p_msg5 = ", illegal ";
static const char* p_msg6 = "\n";
ret = write(2, p_msg, strlen(p_msg));
jit_safe_hex_convert(&hex_buf[0], p_pc);
ret = write(2, hex_buf, sizeof(hex_buf));
ret = write(2, p_msg2, strlen(p_msg2));
jit_safe_hex_convert(&hex_buf[0], p_addr);
ret = write(2, hex_buf, sizeof(hex_buf));
ret = write(2, p_msg3, strlen(p_msg3));
if (is_write == -1) {
digit = '?';
} else {
digit = ('0' + is_write);
}
ret = write(2, &digit, 1);
ret = write(2, p_msg4, strlen(p_msg4));
if (is_exec == -1) {
digit = '?';
} else {
digit = ('0' + is_exec);
}
ret = write(2, &digit, 1);
ret = write(2, p_msg5, strlen(p_msg5));
digit = ('0' + is_illegal);
ret = write(2, &digit, 1);
ret = write(2, p_msg6, strlen(p_msg6));
(void) ret;
os_fault_bail();
}
static void
jit_handle_fault(uintptr_t* p_host_pc,
uintptr_t host_fault_addr,
int is_illegal,
int is_exec,
int is_write,
uintptr_t host_context) {
struct jit_struct* p_jit;
int32_t addr_6502 = -1;
uintptr_t new_pc = *p_host_pc;
int is_inturbo = 0;
void* p_fault_pc = (void*) *p_host_pc;
void* p_fault_addr = (void*) host_fault_addr;
/* Illegal instructions are not expected! */
if (is_illegal) {
fault_reraise(p_fault_pc, p_fault_addr, 1, 0, 0);
}
/* Fail unless the faulting instruction is in the JIT or inturbo region. */
if ((p_fault_pc >= (void*) K_JIT_ADDR) &&
(p_fault_pc < (void*) K_JIT_ADDR_END)) {
/* JIT code. Continue. */
} else if ((p_fault_pc >= (void*) K_INTURBO_ADDR) &&
(p_fault_pc < (void*) K_INTURBO_ADDR_END)) {
/* Inturbo code. Continue. */
is_inturbo = 1;
} else {
fault_reraise(p_fault_pc, p_fault_addr, 0, is_write, is_exec);
}
/* Fault in instruction fetch would be bad! */
if (is_exec == 1) {
fault_reraise(p_fault_pc, p_fault_addr, 0, is_write, is_exec);
}
if (!is_inturbo) {
p_jit = (struct jit_struct*) host_context;
} else {
/* Inturbo stores a pointer to JIT pointers in it's private member. */
void** p_inturbo = (void**) host_context;
void* p_jit_ptrs = *p_inturbo;
p_jit = (struct jit_struct*) ((uint8_t*) p_jit_ptrs -
K_JIT_CONTEXT_OFFSET_JIT_PTRS);
}
/* Sanity check it is really a jit struct. */
if (p_jit->p_compile_callback != jit_compile) {
fault_reraise(p_fault_pc, p_fault_addr, 0, is_write, is_exec);
}
if (!is_inturbo) {
/* NOTE -- may call assert() which isn't async safe but faulting context is
* raw asm, shouldn't be a disaster.
*/
assert(((uintptr_t) p_fault_pc & (K_JIT_BYTES_PER_BYTE - 1)) != 0);
addr_6502 = jit_metadata_get_6502_pc_from_host_pc(p_jit->p_metadata,
p_fault_pc);
}
/* Bail unless it's a clearly recognized fault. */
if (!asm_jit_handle_fault(p_jit->p_asm,
&new_pc,
is_inturbo,
addr_6502,
p_fault_addr,
is_write)) {
fault_reraise(p_fault_pc, p_fault_addr, 0, is_write, is_exec);
}
if (p_jit->log_fault) {
/* Not the cleverest thing to do in a fault context, but it should be ok
* because it's a fault in JIT code; it's also a debug option that needs to
* be turned on.
*/
log_do_log(k_log_jit,
k_log_info,
"JIT faulting at 6502 $%.4X pc 0x%"PRIx64" fault addr 0x%"PRIx64,
addr_6502,
(uint64_t) p_fault_pc,
(uint64_t) p_fault_addr);
}
if ((p_jit->counter_num_faults % 10000) == 0) {
/* We shouldn't call logging in the fault context (re-entrancy etc.) so set
* a flag to take care of it later.
*/
p_jit->do_fault_log = 1;
}
p_jit->counter_num_faults++;
*p_host_pc = new_pc;
}
static void
jit_init(struct cpu_driver* p_cpu_driver) {
struct interp_struct* p_interp;
uint8_t* p_jit_base;
struct util_buffer* p_temp_buf;
void* p_no_code_mapping_addr;
struct jit_metadata* p_metadata;
uint32_t i;
struct jit_struct* p_jit = (struct jit_struct*) p_cpu_driver;
struct state_6502* p_state_6502 = p_cpu_driver->abi.p_state_6502;
struct memory_access* p_memory_access =
p_cpu_driver->p_extra->p_memory_access;
struct timing_struct* p_timing = p_cpu_driver->p_extra->p_timing;
struct bbc_options* p_options = p_cpu_driver->p_extra->p_options;
struct debug_struct* p_debug = p_options->p_debug_object;
int debug = debug_subsystem_active(p_debug);
struct cpu_driver_funcs* p_funcs = p_cpu_driver->p_funcs;
struct inturbo_struct* p_inturbo = NULL;
p_jit->log_compile = util_has_option(p_options->p_log_flags, "jit:compile");
p_jit->log_fault = util_has_option(p_options->p_log_flags, "jit:fault");
p_funcs->get_opcode_maps(p_cpu_driver,
&p_jit->p_opcode_types,
&p_jit->p_opcode_modes,
&p_jit->p_opcode_mem,
&p_jit->p_opcode_cycles);
p_funcs->destroy = jit_destroy;
p_funcs->set_reset_callback = jit_set_reset_callback;
p_funcs->set_memory_written_callback = jit_set_memory_written_callback;
p_funcs->enter = jit_enter;
p_funcs->apply_flags = jit_apply_flags;
p_funcs->get_flags = jit_get_flags;
p_funcs->get_exit_value = jit_get_exit_value;
p_funcs->set_exit_value = jit_set_exit_value;
p_funcs->memory_range_invalidate = jit_memory_range_invalidate;
p_funcs->get_address_info = jit_get_address_info;
p_funcs->get_custom_counters = jit_get_custom_counters;
p_funcs->housekeeping_tick = jit_housekeeping_tick;
p_jit->p_compile_callback = jit_compile;
p_jit->p_jit_callback_context = p_memory_access->p_callback_obj;
p_cpu_driver->abi.p_debug_asm = asm_debug_trampoline;
p_cpu_driver->abi.p_interp_asm = asm_jit_interp_trampoline;
/* The JIT mode uses an interpreter to handle complicated situations,
* such as IRQs, hardware accesses, etc.
*/
p_interp = (struct interp_struct*) cpu_driver_alloc(k_cpu_mode_interp,
0,
p_state_6502,
p_memory_access,
p_timing,
p_options);
assert(((struct cpu_driver*) p_interp)->p_extra->type == k_cpu_mode_interp);
cpu_driver_init((struct cpu_driver*) p_interp);
p_jit->p_interp = p_interp;
interp_set_instruction_callback(p_interp,
jit_interp_instruction_callback,
p_jit);
/* The JIT mode uses an inturbo to handle opcodes that are self-modified
* continually.
*/
if (asm_inturbo_is_enabled()) {
struct cpu_driver* p_inturbo_driver;
p_inturbo = (struct inturbo_struct*) cpu_driver_alloc(k_cpu_mode_inturbo,
0,
p_state_6502,
p_memory_access,
p_timing,
p_options);
p_inturbo_driver = (struct cpu_driver*) p_inturbo;
assert(p_inturbo_driver->p_extra->type == k_cpu_mode_inturbo);
inturbo_set_interp(p_inturbo, p_interp);
/* Enable inturbo ret mode, which means we can call it to interpret a single
* instruction and it will ret right back to us after every instruction.
*/
inturbo_set_ret_mode(p_inturbo);
inturbo_set_do_write_invalidation(p_inturbo, &p_jit->jit_ptrs[0]);
/* RTI has to go through interp so as not to break apart code blocks
* arbitrarily.
*/
inturbo_set_use_interp_for_opcode(p_inturbo, 0x40);
cpu_driver_init(p_inturbo_driver);
}
p_jit->p_inturbo = p_inturbo;
p_jit->driver.abi.p_interp_callback = jit_enter_interp;
p_jit->driver.abi.p_interp_object = p_jit;
/* Little dance to avoid GCC 11 bug with -Werror=stringop-overflow. */
if (g_p_jit_base == NULL) {
g_p_jit_base = (void*) K_JIT_ADDR;
}
/* This is the mapping that holds the dynamically JIT'ed code.
* Directly after creation, it will be read-write.
*/
p_jit->p_mapping_jit = os_alloc_get_mapping(g_p_jit_base, K_JIT_SIZE);
p_jit_base = os_alloc_get_mapping_addr(p_jit->p_mapping_jit);
p_temp_buf = util_buffer_create();
p_jit->p_temp_buf = p_temp_buf;
util_buffer_setup(p_temp_buf, p_jit_base, K_JIT_SIZE);
asm_fill_with_trap(p_temp_buf);
p_jit->p_jit_base = p_jit_base;
p_jit->p_mapping_no_code_ptr =
os_alloc_get_mapping((void*) K_JIT_NO_CODE_JIT_PTR_PAGE, 4096);
p_no_code_mapping_addr =
os_alloc_get_mapping_addr(p_jit->p_mapping_no_code_ptr);
/* Ah the horrors, a fault / SIGSEGV handler! This actually enables a ton of
* optimizations by using faults for very uncommon conditions, such that the
* fast path doesn't need certain checks.
*/
os_fault_register_handler(jit_handle_fault);
p_metadata = jit_metadata_create(p_jit_base,
p_no_code_mapping_addr,
((uint8_t*) p_no_code_mapping_addr + 4),
&p_jit->jit_ptrs[0]);
p_jit->p_metadata = p_metadata;
/* Set up the invalidation markers. jit_memory_range_invalidate() only
* invalidates existing blocks, and here we're starting from a clean slate.
*/
for (i = 0; i < k_6502_addr_space_size; ++i) {
void* p_jit_ptr = jit_metadata_get_host_block_address(p_metadata, i);
asm_jit_invalidate_code_at(p_jit_ptr);
}
/* Anything the specific asm driver (x64 or ARM64) needs to get its job
* done. This includes setting up initial mapping permissions on the JIT
* mapping. Any setup requiring writing to the code area must occur above
* this call.
*/
p_jit->p_asm = asm_jit_create(p_jit_base,
p_memory_access->memory_is_always_ram,
p_memory_access->p_callback_obj);
p_cpu_driver->abi.p_util_private = asm_jit_get_private(p_jit->p_asm);
p_jit->p_compiler = jit_compiler_create(
p_jit->p_asm,
p_timing,
p_memory_access,
p_jit->p_metadata,
p_options,
debug,
p_jit->p_opcode_types,
p_jit->p_opcode_modes,
p_jit->p_opcode_mem,
p_jit->p_opcode_cycles);
/* NOTE: the JIT code space hasn't been set up with the invalidation markers.
* Power-on reset has the responsibility of marking the entire address space
* as invalidated.
*/
}
struct cpu_driver*
jit_create(struct cpu_driver_funcs* p_funcs) {
struct cpu_driver* p_cpu_driver;
size_t alignment;
if (!asm_jit_is_enabled()) {
return NULL;
}
asm_jit_test_preconditions();
/* Align the structure to a multiple of the L1 DTLB bucket stride. This is
* because the structure contains pointers read by JIT code and we want
* deterministic performance.
*/
alignment = (4096 * (64 / 4));
p_cpu_driver =
(struct cpu_driver*) os_alloc_get_aligned(alignment,
sizeof(struct jit_struct));
(void) memset(p_cpu_driver, '\0', sizeof(struct jit_struct));
p_funcs->init = jit_init;
return p_cpu_driver;
}
#include "test-jit.c"