-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcodegen.rs
2659 lines (2392 loc) · 87.6 KB
/
codegen.rs
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
use cpu::cpu::{
tlb_data, FLAG_CARRY, FLAG_OVERFLOW, FLAG_SIGN, FLAG_ZERO, OPSIZE_16, OPSIZE_32, OPSIZE_8,
TLB_GLOBAL, TLB_HAS_CODE, TLB_NO_USER, TLB_READONLY, TLB_VALID,
};
use cpu::global_pointers;
use cpu::memory;
use jit::{Instruction, InstructionOperand, InstructionOperandDest, JitContext};
use modrm;
use modrm::ModrmByte;
use profiler;
use regs;
use wasmgen::wasm_builder::{WasmBuilder, WasmLocal, WasmLocalI64};
pub fn gen_add_cs_offset(ctx: &mut JitContext) {
if !ctx.cpu.has_flat_segmentation() {
ctx.builder
.load_fixed_i32(global_pointers::get_seg_offset(regs::CS));
ctx.builder.add_i32();
}
}
pub fn gen_get_eip(builder: &mut WasmBuilder) {
builder.load_fixed_i32(global_pointers::instruction_pointer as u32);
}
pub fn gen_set_eip_to_after_current_instruction(ctx: &mut JitContext) {
ctx.builder
.const_i32(global_pointers::instruction_pointer as i32);
gen_get_eip(ctx.builder);
ctx.builder.const_i32(!0xFFF);
ctx.builder.and_i32();
ctx.builder.const_i32(ctx.cpu.eip as i32 & 0xFFF);
ctx.builder.or_i32();
ctx.builder.store_aligned_i32(0);
}
pub fn gen_set_previous_eip_offset_from_eip_with_low_bits(
builder: &mut WasmBuilder,
low_bits: i32,
) {
// previous_ip = instruction_pointer & ~0xFFF | low_bits;
dbg_assert!(low_bits & !0xFFF == 0);
builder.const_i32(global_pointers::previous_ip as i32);
gen_get_eip(builder);
builder.const_i32(!0xFFF);
builder.and_i32();
builder.const_i32(low_bits);
builder.or_i32();
builder.store_aligned_i32(0);
}
pub fn gen_set_eip_low_bits(builder: &mut WasmBuilder, low_bits: i32) {
// instruction_pointer = instruction_pointer & ~0xFFF | low_bits;
dbg_assert!(low_bits & !0xFFF == 0);
builder.const_i32(global_pointers::instruction_pointer as i32);
gen_get_eip(builder);
builder.const_i32(!0xFFF);
builder.and_i32();
builder.const_i32(low_bits);
builder.or_i32();
builder.store_aligned_i32(0);
}
pub fn gen_set_eip_low_bits_and_jump_rel32(builder: &mut WasmBuilder, low_bits: i32, n: i32) {
// instruction_pointer = (instruction_pointer & ~0xFFF | low_bits) + n;
dbg_assert!(low_bits & !0xFFF == 0);
builder.const_i32(global_pointers::instruction_pointer as i32);
gen_get_eip(builder);
builder.const_i32(!0xFFF);
builder.and_i32();
builder.const_i32(low_bits);
builder.or_i32();
if n != 0 {
builder.const_i32(n);
builder.add_i32();
}
builder.store_aligned_i32(0);
}
pub fn gen_relative_jump(builder: &mut WasmBuilder, n: i32) {
// add n to instruction_pointer
if n != 0 {
builder.const_i32(global_pointers::instruction_pointer as i32);
gen_get_eip(builder);
builder.const_i32(n);
builder.add_i32();
builder.store_aligned_i32(0);
}
}
pub fn gen_page_switch_check(
ctx: &mut JitContext,
next_block_addr: u32,
last_instruction_addr: u32,
) {
// After switching a page while in jitted code, check if the page mapping still holds
gen_get_eip(ctx.builder);
let address_local = ctx.builder.set_new_local();
gen_get_phys_eip_plus_mem(ctx, &address_local);
ctx.builder.free_local(address_local);
ctx.builder
.const_i32(next_block_addr as i32 + unsafe { memory::mem8 } as i32);
ctx.builder.ne_i32();
if cfg!(debug_assertions) {
ctx.builder.if_void();
gen_profiler_stat_increment(ctx.builder, profiler::stat::FAILED_PAGE_CHANGE);
gen_debug_track_jit_exit(ctx.builder, last_instruction_addr);
ctx.builder.br(ctx.exit_label);
ctx.builder.block_end();
}
else {
ctx.builder.br_if(ctx.exit_label);
}
}
pub fn gen_update_instruction_counter(ctx: &mut JitContext) {
ctx.builder
.const_i32(global_pointers::instruction_counter as i32);
ctx.builder
.load_fixed_i32(global_pointers::instruction_counter as u32);
ctx.builder.get_local(&ctx.instruction_counter);
ctx.builder.add_i32();
ctx.builder.store_aligned_i32(0);
}
pub fn gen_get_reg8(ctx: &mut JitContext, r: u32) {
match r {
regs::AL | regs::CL | regs::DL | regs::BL => {
ctx.builder.get_local(&ctx.register_locals[r as usize]);
ctx.builder.const_i32(0xFF);
ctx.builder.and_i32();
},
regs::AH | regs::CH | regs::DH | regs::BH => {
ctx.builder
.get_local(&ctx.register_locals[(r - 4) as usize]);
ctx.builder.const_i32(8);
ctx.builder.shr_u_i32();
ctx.builder.const_i32(0xFF);
ctx.builder.and_i32();
},
_ => assert!(false),
}
}
/// Return a new local referencing one of the 8 bit registers or a direct reference to one of the
/// register locals. Higher bits might be garbage (suitable for gen_cmp8 etc.). Must be freed with
/// gen_free_reg8_or_alias.
pub fn gen_get_reg8_or_alias_to_reg32(ctx: &mut JitContext, r: u32) -> WasmLocal {
match r {
regs::AL | regs::CL | regs::DL | regs::BL => ctx.register_locals[r as usize].unsafe_clone(),
regs::AH | regs::CH | regs::DH | regs::BH => {
ctx.builder
.get_local(&ctx.register_locals[(r - 4) as usize]);
ctx.builder.const_i32(8);
ctx.builder.shr_u_i32();
ctx.builder.set_new_local()
},
_ => panic!(),
}
}
pub fn gen_free_reg8_or_alias(ctx: &mut JitContext, r: u32, local: WasmLocal) {
match r {
regs::AL | regs::CL | regs::DL | regs::BL => {},
regs::AH | regs::CH | regs::DH | regs::BH => ctx.builder.free_local(local),
_ => panic!(),
}
}
pub fn gen_get_reg16(ctx: &mut JitContext, r: u32) {
ctx.builder.get_local(&ctx.register_locals[r as usize]);
ctx.builder.const_i32(0xFFFF);
ctx.builder.and_i32();
}
pub fn gen_get_reg32(ctx: &mut JitContext, r: u32) {
ctx.builder.get_local(&ctx.register_locals[r as usize]);
}
pub fn gen_set_reg8(ctx: &mut JitContext, r: u32) {
match r {
regs::AL | regs::CL | regs::DL | regs::BL => {
// reg32[r] = stack_value & 0xFF | reg32[r] & ~0xFF
ctx.builder.const_i32(0xFF);
ctx.builder.and_i32();
ctx.builder.get_local(&ctx.register_locals[r as usize]);
ctx.builder.const_i32(!0xFF);
ctx.builder.and_i32();
ctx.builder.or_i32();
ctx.builder.set_local(&ctx.register_locals[r as usize]);
},
regs::AH | regs::CH | regs::DH | regs::BH => {
// reg32[r] = stack_value << 8 & 0xFF00 | reg32[r] & ~0xFF00
ctx.builder.const_i32(8);
ctx.builder.shl_i32();
ctx.builder.const_i32(0xFF00);
ctx.builder.and_i32();
ctx.builder
.get_local(&ctx.register_locals[(r - 4) as usize]);
ctx.builder.const_i32(!0xFF00);
ctx.builder.and_i32();
ctx.builder.or_i32();
ctx.builder
.set_local(&ctx.register_locals[(r - 4) as usize]);
},
_ => assert!(false),
}
}
pub fn gen_set_reg8_unmasked(ctx: &mut JitContext, r: u32) {
if cfg!(debug_assertions) {
let val = ctx.builder.set_new_local();
ctx.builder.get_local(&val);
ctx.builder.const_i32(!0xFF);
ctx.builder.and_i32();
ctx.builder.if_void();
ctx.builder.unreachable();
ctx.builder.block_end();
ctx.builder.get_local(&val);
ctx.builder.free_local(val);
}
match r {
regs::AL | regs::CL | regs::DL | regs::BL => {
// reg32[r] = stack_value | reg32[r] & ~0xFF
ctx.builder.get_local(&ctx.register_locals[r as usize]);
ctx.builder.const_i32(!0xFF);
ctx.builder.and_i32();
ctx.builder.or_i32();
ctx.builder.set_local(&ctx.register_locals[r as usize]);
},
regs::AH | regs::CH | regs::DH | regs::BH => {
// reg32[r] = stack_value << 8 | reg32[r] & ~0xFF00
ctx.builder.const_i32(8);
ctx.builder.shl_i32();
ctx.builder.const_i32(0xFF00);
ctx.builder.and_i32();
ctx.builder
.get_local(&ctx.register_locals[(r - 4) as usize]);
ctx.builder.const_i32(!0xFF00);
ctx.builder.and_i32();
ctx.builder.or_i32();
ctx.builder
.set_local(&ctx.register_locals[(r - 4) as usize]);
},
_ => assert!(false),
}
}
pub fn gen_set_reg16(ctx: &mut JitContext, r: u32) {
gen_set_reg16_local(ctx.builder, &ctx.register_locals[r as usize]);
}
pub fn gen_set_reg16_unmasked(ctx: &mut JitContext, r: u32) {
if cfg!(debug_assertions) {
let val = ctx.builder.set_new_local();
ctx.builder.get_local(&val);
ctx.builder.const_i32(!0xFFFF);
ctx.builder.and_i32();
ctx.builder.if_void();
ctx.builder.unreachable();
ctx.builder.block_end();
ctx.builder.get_local(&val);
ctx.builder.free_local(val);
}
ctx.builder.get_local(&ctx.reg(r));
ctx.builder.const_i32(!0xFFFF);
ctx.builder.and_i32();
ctx.builder.or_i32();
ctx.builder.set_local(&ctx.reg(r));
}
pub fn gen_set_reg16_local(builder: &mut WasmBuilder, local: &WasmLocal) {
// reg32[r] = v & 0xFFFF | reg32[r] & ~0xFFFF
builder.const_i32(0xFFFF);
builder.and_i32();
builder.get_local(local);
builder.const_i32(!0xFFFF);
builder.and_i32();
builder.or_i32();
builder.set_local(local);
}
pub fn gen_set_reg32(ctx: &mut JitContext, r: u32) {
ctx.builder.set_local(&ctx.register_locals[r as usize]);
}
pub fn decr_exc_asize(ctx: &mut JitContext) {
gen_get_reg32(ctx, regs::ECX);
ctx.builder.const_i32(1);
ctx.builder.sub_i32();
if ctx.cpu.asize_32() {
gen_set_reg32(ctx, regs::ECX);
}
else {
gen_set_reg16(ctx, regs::CX);
}
}
pub fn gen_read_reg_xmm128_into_scratch(ctx: &mut JitContext, r: u32) {
ctx.builder
.const_i32(global_pointers::sse_scratch_register as i32);
let dest = global_pointers::get_reg_xmm_offset(r);
ctx.builder.const_i32(dest as i32);
ctx.builder.load_aligned_i64(0);
ctx.builder.store_aligned_i64(0);
ctx.builder
.const_i32(global_pointers::sse_scratch_register as i32 + 8);
let dest = global_pointers::get_reg_xmm_offset(r) + 8;
ctx.builder.const_i32(dest as i32);
ctx.builder.load_aligned_i64(0);
ctx.builder.store_aligned_i64(0);
}
pub fn gen_get_sreg(ctx: &mut JitContext, r: u32) {
ctx.builder
.load_fixed_u16(global_pointers::get_sreg_offset(r))
}
pub fn gen_get_ss_offset(ctx: &mut JitContext) {
ctx.builder
.load_fixed_i32(global_pointers::get_seg_offset(regs::SS));
}
pub fn gen_get_flags(builder: &mut WasmBuilder) {
builder.load_fixed_i32(global_pointers::flags as u32);
}
fn gen_get_flags_changed(builder: &mut WasmBuilder) {
builder.load_fixed_i32(global_pointers::flags_changed as u32);
}
fn gen_get_last_result(builder: &mut WasmBuilder, previous_instruction: &Instruction) {
match previous_instruction {
Instruction::Add {
dest: InstructionOperandDest::WasmLocal(l),
opsize: OPSIZE_32,
..
}
| Instruction::AdcSbb {
dest: InstructionOperandDest::WasmLocal(l),
opsize: OPSIZE_32,
..
}
| Instruction::Sub {
dest: InstructionOperandDest::WasmLocal(l),
opsize: OPSIZE_32,
..
}
| Instruction::Bitwise {
dest: InstructionOperandDest::WasmLocal(l),
opsize: OPSIZE_32,
}
| Instruction::NonZeroShift {
dest: InstructionOperandDest::WasmLocal(l),
opsize: OPSIZE_32,
} => builder.get_local(&l),
Instruction::Cmp {
dest: InstructionOperandDest::WasmLocal(l),
source,
opsize: OPSIZE_32,
} => {
if source.is_zero() {
builder.get_local(&l)
}
else {
builder.load_fixed_i32(global_pointers::last_result as u32)
}
},
_ => builder.load_fixed_i32(global_pointers::last_result as u32),
}
}
fn gen_get_last_op_size(builder: &mut WasmBuilder) {
builder.load_fixed_i32(global_pointers::last_op_size as u32);
}
fn gen_get_last_op1(builder: &mut WasmBuilder, previous_instruction: &Instruction) {
match previous_instruction {
Instruction::Cmp {
dest: InstructionOperandDest::WasmLocal(l),
source: _,
opsize: OPSIZE_32,
} => builder.get_local(&l),
_ => builder.load_fixed_i32(global_pointers::last_op1 as u32),
}
}
pub fn gen_get_page_fault(builder: &mut WasmBuilder) {
builder.load_fixed_u8(global_pointers::page_fault as u32);
}
/// sign-extend a byte value on the stack and leave it on the stack
pub fn sign_extend_i8(builder: &mut WasmBuilder) {
builder.const_i32(24);
builder.shl_i32();
builder.const_i32(24);
builder.shr_s_i32();
}
/// sign-extend a two byte value on the stack and leave it on the stack
pub fn sign_extend_i16(builder: &mut WasmBuilder) {
builder.const_i32(16);
builder.shl_i32();
builder.const_i32(16);
builder.shr_s_i32();
}
pub fn gen_fn0_const(builder: &mut WasmBuilder, name: &str) { builder.call_fn0(name) }
pub fn gen_fn1_const(builder: &mut WasmBuilder, name: &str, arg0: u32) {
builder.const_i32(arg0 as i32);
builder.call_fn1(name);
}
pub fn gen_fn2_const(builder: &mut WasmBuilder, name: &str, arg0: u32, arg1: u32) {
builder.const_i32(arg0 as i32);
builder.const_i32(arg1 as i32);
builder.call_fn2(name);
}
// helper functions for gen/generate_jit.js
pub fn gen_modrm_fn0(builder: &mut WasmBuilder, name: &str) {
// generates: fn( _ )
builder.call_fn1(name);
}
pub fn gen_modrm_fn1(builder: &mut WasmBuilder, name: &str, arg0: u32) {
// generates: fn( _, arg0 )
builder.const_i32(arg0 as i32);
builder.call_fn2(name);
}
pub fn gen_modrm_resolve(ctx: &mut JitContext, modrm_byte: ModrmByte) {
modrm::gen(ctx, modrm_byte, 0)
}
pub fn gen_modrm_resolve_with_local(
ctx: &mut JitContext,
modrm_byte: ModrmByte,
gen: &dyn Fn(&mut JitContext, &WasmLocal),
) {
if let Some(r) = modrm::get_as_reg_index_if_possible(ctx, &modrm_byte) {
gen(ctx, &ctx.reg(r));
}
else {
gen_modrm_resolve(ctx, modrm_byte);
let address = ctx.builder.set_new_local();
gen(ctx, &address);
ctx.builder.free_local(address);
}
}
pub fn gen_modrm_resolve_with_esp_offset(
ctx: &mut JitContext,
modrm_byte: ModrmByte,
esp_offset: i32,
) {
modrm::gen(ctx, modrm_byte, esp_offset)
}
pub fn gen_set_reg8_r(ctx: &mut JitContext, dest: u32, src: u32) {
// generates: reg8[r_dest] = reg8[r_src]
if src != dest {
gen_get_reg8(ctx, src);
gen_set_reg8_unmasked(ctx, dest);
}
}
pub fn gen_set_reg16_r(ctx: &mut JitContext, dest: u32, src: u32) {
// generates: reg16[r_dest] = reg16[r_src]
if src != dest {
gen_get_reg16(ctx, src);
gen_set_reg16_unmasked(ctx, dest);
}
}
pub fn gen_set_reg32_r(ctx: &mut JitContext, dest: u32, src: u32) {
// generates: reg32[r_dest] = reg32[r_src]
if src != dest {
gen_get_reg32(ctx, src);
gen_set_reg32(ctx, dest);
}
}
pub fn gen_modrm_resolve_safe_read8(ctx: &mut JitContext, modrm_byte: ModrmByte) {
gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read8(ctx, addr));
}
pub fn gen_modrm_resolve_safe_read16(ctx: &mut JitContext, modrm_byte: ModrmByte) {
gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read16(ctx, addr));
}
pub fn gen_modrm_resolve_safe_read32(ctx: &mut JitContext, modrm_byte: ModrmByte) {
gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read32(ctx, addr));
}
pub fn gen_modrm_resolve_safe_read64(ctx: &mut JitContext, modrm_byte: ModrmByte) {
gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| gen_safe_read64(ctx, addr));
}
pub fn gen_modrm_resolve_safe_read128(
ctx: &mut JitContext,
modrm_byte: ModrmByte,
where_to_write: u32,
) {
gen_modrm_resolve_with_local(ctx, modrm_byte, &|ctx, addr| {
gen_safe_read128(ctx, addr, where_to_write)
});
}
pub fn gen_safe_read8(ctx: &mut JitContext, address_local: &WasmLocal) {
gen_safe_read(ctx, BitSize::BYTE, address_local, None);
}
pub fn gen_safe_read16(ctx: &mut JitContext, address_local: &WasmLocal) {
gen_safe_read(ctx, BitSize::WORD, address_local, None);
}
pub fn gen_safe_read32(ctx: &mut JitContext, address_local: &WasmLocal) {
gen_safe_read(ctx, BitSize::DWORD, address_local, None);
}
pub fn gen_safe_read64(ctx: &mut JitContext, address_local: &WasmLocal) {
gen_safe_read(ctx, BitSize::QWORD, &address_local, None);
}
pub fn gen_safe_read128(ctx: &mut JitContext, address_local: &WasmLocal, where_to_write: u32) {
gen_safe_read(ctx, BitSize::DQWORD, &address_local, Some(where_to_write));
}
// only used internally for gen_safe_write
enum GenSafeWriteValue<'a> {
I32(&'a WasmLocal),
I64(&'a WasmLocalI64),
TwoI64s(&'a WasmLocalI64, &'a WasmLocalI64),
}
enum GenSafeReadWriteValue {
I32(WasmLocal),
I64(WasmLocalI64),
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum BitSize {
BYTE,
WORD,
DWORD,
QWORD,
DQWORD,
}
impl BitSize {
pub fn bytes(&self) -> u32 {
match self {
BitSize::BYTE => 1,
BitSize::WORD => 2,
BitSize::DWORD => 4,
BitSize::QWORD => 8,
BitSize::DQWORD => 16,
}
}
}
pub fn gen_safe_write8(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {
gen_safe_write(
ctx,
BitSize::BYTE,
address_local,
GenSafeWriteValue::I32(value_local),
)
}
pub fn gen_safe_write16(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {
gen_safe_write(
ctx,
BitSize::WORD,
address_local,
GenSafeWriteValue::I32(value_local),
)
}
pub fn gen_safe_write32(ctx: &mut JitContext, address_local: &WasmLocal, value_local: &WasmLocal) {
gen_safe_write(
ctx,
BitSize::DWORD,
address_local,
GenSafeWriteValue::I32(value_local),
)
}
pub fn gen_safe_write64(
ctx: &mut JitContext,
address_local: &WasmLocal,
value_local: &WasmLocalI64,
) {
gen_safe_write(
ctx,
BitSize::QWORD,
address_local,
GenSafeWriteValue::I64(value_local),
)
}
pub fn gen_safe_write128(
ctx: &mut JitContext,
address_local: &WasmLocal,
value_local_low: &WasmLocalI64,
value_local_high: &WasmLocalI64,
) {
gen_safe_write(
ctx,
BitSize::DQWORD,
address_local,
GenSafeWriteValue::TwoI64s(value_local_low, value_local_high),
)
}
fn gen_safe_read(
ctx: &mut JitContext,
bits: BitSize,
address_local: &WasmLocal,
where_to_write: Option<u32>,
) {
// Execute a virtual memory read. All slow paths (memory-mapped IO, tlb miss, page fault and
// read across page boundary are handled in safe_read_jit_slow
// entry <- tlb_data[addr >> 12 << 2]
// if entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes: goto fast
// entry <- safe_read_jit_slow(addr, instruction_pointer)
// if page_fault: goto exit-with-pagefault
// fast: mem[(entry & ~0xFFF) ^ addr]
let cont = ctx.builder.block_void();
ctx.builder.get_local(&address_local);
ctx.builder.const_i32(12);
ctx.builder.shr_u_i32();
ctx.builder.const_i32(2);
ctx.builder.shl_i32();
ctx.builder
.load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
let entry_local = ctx.builder.tee_new_local();
ctx.builder.const_i32(
(0xFFF
& !TLB_READONLY
& !TLB_GLOBAL
& !TLB_HAS_CODE
& !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32,
);
ctx.builder.and_i32();
ctx.builder.const_i32(TLB_VALID as i32);
ctx.builder.eq_i32();
if bits != BitSize::BYTE {
ctx.builder.get_local(&address_local);
ctx.builder.const_i32(0xFFF);
ctx.builder.and_i32();
ctx.builder.const_i32(0x1000 - bits.bytes() as i32);
ctx.builder.le_i32();
ctx.builder.and_i32();
}
ctx.builder.br_if(cont);
if cfg!(feature = "profiler") {
ctx.builder.get_local(&address_local);
ctx.builder.get_local(&entry_local);
ctx.builder.call_fn2("report_safe_read_jit_slow");
}
ctx.builder.get_local(&address_local);
ctx.builder
.const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);
match bits {
BitSize::BYTE => {
ctx.builder.call_fn2_ret("safe_read8_slow_jit");
},
BitSize::WORD => {
ctx.builder.call_fn2_ret("safe_read16_slow_jit");
},
BitSize::DWORD => {
ctx.builder.call_fn2_ret("safe_read32s_slow_jit");
},
BitSize::QWORD => {
ctx.builder.call_fn2_ret("safe_read64s_slow_jit");
},
BitSize::DQWORD => {
ctx.builder.call_fn2_ret("safe_read128s_slow_jit");
},
}
ctx.builder.tee_local(&entry_local);
ctx.builder.const_i32(1);
ctx.builder.and_i32();
if cfg!(feature = "profiler") {
ctx.builder.if_void();
gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
ctx.builder.block_end();
ctx.builder.get_local(&entry_local);
ctx.builder.const_i32(1);
ctx.builder.and_i32();
}
ctx.builder.br_if(ctx.exit_with_fault_label);
ctx.builder.block_end();
gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_FAST); // XXX: Both fast and slow
ctx.builder.get_local(&entry_local);
ctx.builder.const_i32(!0xFFF);
ctx.builder.and_i32();
ctx.builder.get_local(&address_local);
ctx.builder.xor_i32();
// where_to_write is only used by dqword
dbg_assert!((where_to_write != None) == (bits == BitSize::DQWORD));
match bits {
BitSize::BYTE => {
ctx.builder.load_u8(0);
},
BitSize::WORD => {
ctx.builder.load_unaligned_u16(0);
},
BitSize::DWORD => {
ctx.builder.load_unaligned_i32(0);
},
BitSize::QWORD => {
ctx.builder.load_unaligned_i64(0);
},
BitSize::DQWORD => {
let where_to_write = where_to_write.unwrap();
let virt_address_local = ctx.builder.set_new_local();
ctx.builder.const_i32(0);
ctx.builder.get_local(&virt_address_local);
ctx.builder.load_unaligned_i64(0);
ctx.builder.store_unaligned_i64(where_to_write);
ctx.builder.const_i32(0);
ctx.builder.get_local(&virt_address_local);
ctx.builder.load_unaligned_i64(8);
ctx.builder.store_unaligned_i64(where_to_write + 8);
ctx.builder.free_local(virt_address_local);
},
}
ctx.builder.free_local(entry_local);
}
pub fn gen_get_phys_eip_plus_mem(ctx: &mut JitContext, address_local: &WasmLocal) {
// Similar to gen_safe_read, but return the physical eip + memory::mem rather than reading from memory
// In functions that need to use this value we need to fix it by substracting memory::mem
// this is done in order to remove one instruction from the fast path of memory accesses (no need to add
// memory::mem anymore ).
// We need to account for this in gen_page_switch_check and we compare with next_block_addr + memory::mem8
// We cannot the same while processing an AbsoluteEip flow control change so there we need to fix the value
// by subscracting memory::mem. Overall, since AbsoluteEip is encountered less often than memory accesses so
// this ends up improving perf.
// Does not (need to) handle mapped memory
// XXX: Currently does not use ctx.start_of_current_instruction, but rather assumes that eip is
// already correct (pointing at the current instruction)
let cont = ctx.builder.block_void();
ctx.builder.get_local(&address_local);
ctx.builder.const_i32(12);
ctx.builder.shr_u_i32();
ctx.builder.const_i32(2);
ctx.builder.shl_i32();
ctx.builder
.load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
let entry_local = ctx.builder.tee_new_local();
ctx.builder.const_i32(
(0xFFF
& !TLB_READONLY
& !TLB_GLOBAL
& !TLB_HAS_CODE
& !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32,
);
ctx.builder.and_i32();
ctx.builder.const_i32(TLB_VALID as i32);
ctx.builder.eq_i32();
ctx.builder.br_if(cont);
if cfg!(feature = "profiler") {
ctx.builder.get_local(&address_local);
ctx.builder.get_local(&entry_local);
ctx.builder.call_fn2("report_safe_read_jit_slow");
}
ctx.builder.get_local(&address_local);
ctx.builder.call_fn1_ret("get_phys_eip_slow_jit");
ctx.builder.tee_local(&entry_local);
ctx.builder.const_i32(1);
ctx.builder.and_i32();
if cfg!(feature = "profiler") {
ctx.builder.if_void();
gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction); // XXX
ctx.builder.block_end();
ctx.builder.get_local(&entry_local);
ctx.builder.const_i32(1);
ctx.builder.and_i32();
}
ctx.builder.br_if(ctx.exit_with_fault_label);
ctx.builder.block_end();
gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_READ_FAST); // XXX: Both fast and slow
ctx.builder.get_local(&entry_local);
ctx.builder.const_i32(!0xFFF);
ctx.builder.and_i32();
ctx.builder.get_local(&address_local);
ctx.builder.xor_i32();
ctx.builder.free_local(entry_local);
}
fn gen_safe_write(
ctx: &mut JitContext,
bits: BitSize,
address_local: &WasmLocal,
value_local: GenSafeWriteValue,
) {
// Execute a virtual memory write. All slow paths (memory-mapped IO, tlb miss, page fault,
// write across page boundary and page containing jitted code are handled in safe_write_jit_slow
// entry <- tlb_data[addr >> 12 << 2]
// if entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes: goto fast
// entry <- safe_write_jit_slow(addr, value, instruction_pointer)
// if page_fault: goto exit-with-pagefault
// fast: mem[(entry & ~0xFFF) ^ addr] <- value
let cont = ctx.builder.block_void();
ctx.builder.get_local(&address_local);
ctx.builder.const_i32(12);
ctx.builder.shr_u_i32();
ctx.builder.const_i32(2);
ctx.builder.shl_i32();
ctx.builder
.load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
let entry_local = ctx.builder.tee_new_local();
ctx.builder
.const_i32((0xFFF & !TLB_GLOBAL & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32);
ctx.builder.and_i32();
ctx.builder.const_i32(TLB_VALID as i32);
ctx.builder.eq_i32();
if bits != BitSize::BYTE {
ctx.builder.get_local(&address_local);
ctx.builder.const_i32(0xFFF);
ctx.builder.and_i32();
ctx.builder.const_i32(0x1000 - bits.bytes() as i32);
ctx.builder.le_i32();
ctx.builder.and_i32();
}
ctx.builder.br_if(cont);
if cfg!(feature = "profiler") {
ctx.builder.get_local(&address_local);
ctx.builder.get_local(&entry_local);
ctx.builder.call_fn2("report_safe_write_jit_slow");
}
ctx.builder.get_local(&address_local);
match value_local {
GenSafeWriteValue::I32(local) => ctx.builder.get_local(local),
GenSafeWriteValue::I64(local) => ctx.builder.get_local_i64(local),
GenSafeWriteValue::TwoI64s(local1, local2) => {
ctx.builder.get_local_i64(local1);
ctx.builder.get_local_i64(local2)
},
}
ctx.builder
.const_i32(ctx.start_of_current_instruction as i32 & 0xFFF);
match bits {
BitSize::BYTE => {
ctx.builder.call_fn3_ret("safe_write8_slow_jit");
},
BitSize::WORD => {
ctx.builder.call_fn3_ret("safe_write16_slow_jit");
},
BitSize::DWORD => {
ctx.builder.call_fn3_ret("safe_write32_slow_jit");
},
BitSize::QWORD => {
ctx.builder
.call_fn3_i32_i64_i32_ret("safe_write64_slow_jit");
},
BitSize::DQWORD => {
ctx.builder
.call_fn4_i32_i64_i64_i32_ret("safe_write128_slow_jit");
},
}
ctx.builder.tee_local(&entry_local);
ctx.builder.const_i32(1);
ctx.builder.and_i32();
if cfg!(feature = "profiler") {
ctx.builder.if_void();
gen_debug_track_jit_exit(ctx.builder, ctx.start_of_current_instruction);
ctx.builder.block_end();
ctx.builder.get_local(&entry_local);
ctx.builder.const_i32(1);
ctx.builder.and_i32();
}
ctx.builder.br_if(ctx.exit_with_fault_label);
ctx.builder.block_end();
gen_profiler_stat_increment(ctx.builder, profiler::stat::SAFE_WRITE_FAST); // XXX: Both fast and slow
ctx.builder.get_local(&entry_local);
ctx.builder.const_i32(!0xFFF);
ctx.builder.and_i32();
ctx.builder.get_local(&address_local);
ctx.builder.xor_i32();
match value_local {
GenSafeWriteValue::I32(local) => ctx.builder.get_local(local),
GenSafeWriteValue::I64(local) => ctx.builder.get_local_i64(local),
GenSafeWriteValue::TwoI64s(local1, local2) => {
assert!(bits == BitSize::DQWORD);
let virt_address_local = ctx.builder.tee_new_local();
ctx.builder.get_local_i64(local1);
ctx.builder.store_unaligned_i64(0);
ctx.builder.get_local(&virt_address_local);
ctx.builder.get_local_i64(local2);
ctx.builder.store_unaligned_i64(8);
ctx.builder.free_local(virt_address_local);
},
}
match bits {
BitSize::BYTE => {
ctx.builder.store_u8(0);
},
BitSize::WORD => {
ctx.builder.store_unaligned_u16(0);
},
BitSize::DWORD => {
ctx.builder.store_unaligned_i32(0);
},
BitSize::QWORD => {
ctx.builder.store_unaligned_i64(0);
},
BitSize::DQWORD => {}, // handled above
}
ctx.builder.free_local(entry_local);
}
pub fn gen_safe_read_write(
ctx: &mut JitContext,
bits: BitSize,
address_local: &WasmLocal,
f: &dyn Fn(&mut JitContext),
) {
// Execute a virtual memory read+write. All slow paths (memory-mapped IO, tlb miss, page fault,
// write across page boundary and page containing jitted code are handled in
// safe_read_write_jit_slow
// entry <- tlb_data[addr >> 12 << 2]
// can_use_fast_path <- entry & MASK == TLB_VALID && (addr & 0xFFF) <= 0x1000 - bytes
// if can_use_fast_path: goto fast
// entry <- safe_read_write_jit_slow(addr, instruction_pointer)
// if page_fault: goto exit-with-pagefault
// fast: value <- f(mem[(entry & ~0xFFF) ^ addr])
// if !can_use_fast_path { safe_write_jit_slow(addr, value, instruction_pointer) }
// mem[(entry & ~0xFFF) ^ addr] <- value
let cont = ctx.builder.block_void();
ctx.builder.get_local(address_local);
ctx.builder.const_i32(12);
ctx.builder.shr_u_i32();
ctx.builder.const_i32(2);
ctx.builder.shl_i32();
ctx.builder
.load_aligned_i32(unsafe { &tlb_data[0] as *const i32 as u32 });
let entry_local = ctx.builder.tee_new_local();
ctx.builder
.const_i32((0xFFF & !TLB_GLOBAL & !(if ctx.cpu.cpl3() { 0 } else { TLB_NO_USER })) as i32);
ctx.builder.and_i32();