-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdecode-core.cpp
2232 lines (1938 loc) · 75.6 KB
/
decode-core.cpp
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
//
// PTLsim: Cycle Accurate x86-64 Simulator
// Decoder for x86 and x86-64 to PTL transops
//
// Copyright 1999-2008 Matt T. Yourst <yourst@yourst.com>
//
#include <globals.h>
#include <ptlsim.h>
#include <decode.h>
#include <stats.h>
BasicBlockCache bbcache;
struct BasicBlockChunkListHashtableLinkManager {
static inline BasicBlockChunkList* objof(selflistlink* link) {
return baseof(BasicBlockChunkList, hashlink, link);
}
static inline W64& keyof(BasicBlockChunkList* obj) {
return obj->mfn;
}
static inline selflistlink* linkof(BasicBlockChunkList* obj) {
return &obj->hashlink;
}
};
typedef SelfHashtable<W64, BasicBlockChunkList, 16384, BasicBlockChunkListHashtableLinkManager> BasicBlockPageCache;
BasicBlockPageCache bbpages;
odstream bbcache_dump_file;
//
// Calling convention:
// rip = return RIP after insn
// sr0 = RIP of insn
// sr1 = RIP after insn
// sr2 = argument
//
const assist_func_t assistid_to_func[ASSIST_COUNT] = {
// Forced assists based on decode context
assist_invalid_opcode,
assist_exec_page_fault,
assist_gp_fault,
// Integer arithmetic
assist_div<byte>,
assist_div<W16>,
assist_div<W32>,
assist_div<W64>,
assist_idiv<byte>,
assist_idiv<W16>,
assist_idiv<W32>,
assist_idiv<W64>,
// x87
assist_x87_fist,
assist_x87_fldcw,
assist_x87_fprem,
assist_x87_fyl2xp1,
assist_x87_fsqrt,
assist_x87_fsincos,
assist_x87_frndint,
assist_x87_fscale,
assist_x87_fsin,
assist_x87_fcos,
assist_x87_fxam,
assist_x87_f2xm1,
assist_x87_fyl2x,
assist_x87_fptan,
assist_x87_fpatan,
assist_x87_fxtract,
assist_x87_fprem1,
assist_x87_fld80,
assist_x87_fstp80,
assist_x87_fsave,
assist_x87_frstor,
assist_x87_finit,
assist_x87_fclex,
// SSE save/restore
assist_ldmxcsr,
assist_fxsave,
assist_fxrstor,
// Interrupts, system calls, etc.
assist_int,
assist_syscall,
assist_hypercall,
assist_ptlcall,
assist_sysenter,
assist_iret16,
assist_iret32,
assist_iret64,
// Control register updates
assist_cpuid,
assist_rdtsc,
assist_cld,
assist_std,
assist_popf,
assist_write_segreg,
assist_wrmsr,
assist_rdmsr,
assist_write_cr0,
assist_write_cr2,
assist_write_cr3,
assist_write_cr4,
assist_write_debug_reg,
// I/O and legacy
assist_ioport_in,
assist_ioport_out,
};
int assist_index(assist_func_t assist) {
foreach (i, ASSIST_COUNT) {
if (assistid_to_func[i] == assist) {
return i;
}
}
return -1;
}
const char* assist_name(assist_func_t assist) {
foreach (i, ASSIST_COUNT) {
if (assistid_to_func[i] == assist) {
return assist_names[i];
}
}
return "unknown";
}
void update_assist_stats(assist_func_t assist) {
int idx = assist_index(assist);
assert(inrange(idx, 0, ASSIST_COUNT-1));
stats.external.assists[idx]++;
}
void split_unaligned(const TransOp& transop, TransOpBuffer& buf) {
assert(transop.unaligned);
bool ld = isload(transop.opcode);
bool st = isstore(transop.opcode);
assert(ld|st);
buf.reset();
int idx;
idx = buf.put();
TransOp& ag = buf.uops[idx];
ag = transop;
ag.opcode = OP_add;
ag.size = 3;
ag.cond = 0;
ag.eom = 0;
ag.internal = 0;
ag.unaligned = 0;
ag.rd = REG_temp9;
ag.rc = REG_zero;
buf.synthops[idx] = get_synthcode_for_uop(OP_add, 3, 0, 0, 0, 0, 0);
idx = buf.put();
TransOp& lo = buf.uops[idx];
lo = transop;
lo.ra = REG_temp9;
lo.rb = REG_imm;
lo.rbimm = 0;
lo.cond = LDST_ALIGN_LO;
lo.unaligned = 0;
lo.eom = 0;
buf.synthops[idx] = null; // loads and stores are not synthesized
idx = buf.put();
TransOp& hi = buf.uops[idx];
hi = transop;
hi.ra = REG_temp9;
hi.rb = REG_imm;
hi.rbimm = 0;
hi.cond = LDST_ALIGN_HI;
hi.unaligned = 0;
hi.som = 0;
buf.synthops[idx] = null; // loads and stores are not synthesized
if (ld) {
// ld rd = [ra+rb] => ld.lo rd = [rt] and ld.hi rd = [rt],rd
lo.rd = REG_temp4;
lo.size = 3; // always load 64-bit word
hi.rb = REG_temp4;
} else {
//
// For stores, expand st sfrd = [ra+rb],rc => st.lo sfrd1 = [rt],rc and st.hi sfrd2 = [rt],rc
//
// Make sure high part issues first in program order, so if there is
// a page fault on the high page it overlaps, this will be triggered
// before the low part overwrites memory.
//
lo.cond = LDST_ALIGN_HI;
hi.cond = LDST_ALIGN_LO;
}
}
static const W16 prefix_map_x86_64[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, PFX_ES, 0, 0, 0, 0, 0, 0, 0, PFX_CS, 0,
0, 0, 0, 0, 0, 0, PFX_SS, 0, 0, 0, 0, 0, 0, 0, PFX_DS, 0,
PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX, PFX_REX,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, PFX_FS, PFX_GS, PFX_DATA, PFX_ADDR, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, PFX_FWAIT, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
PFX_LOCK, 0, PFX_REPNZ, PFX_REPZ, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static const W16 prefix_map_x86[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, PFX_ES, 0, 0, 0, 0, 0, 0, 0, PFX_CS, 0,
0, 0, 0, 0, 0, 0, PFX_SS, 0, 0, 0, 0, 0, 0, 0, PFX_DS, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, PFX_FS, PFX_GS, PFX_DATA, PFX_ADDR, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, PFX_FWAIT, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
PFX_LOCK, 0, PFX_REPNZ, PFX_REPZ, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
const char* prefix_names[PFX_count] = {"repz", "repnz", "lock", "cs", "ss", "ds", "es", "fs", "gs", "datasz", "addrsz", "rex", "fwait"};
const char* uniform_arch_reg_names[APR_COUNT] = {
// 64-bit
"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
// 32-bit
"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
// 16-bit
"ax", "cx", "dx", "bx", "sp", "bp", "si", "di", "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w",
// 8-bit
"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh",
// 8-bit with REX:
"spl", "bpl", "sil", "dil",
"r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b", "r15b",
// SSE registers
"xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
// segments
"es", "cs", "ss", "ds", "fs", "gs",
// special:
"rip", "zero",
};
const ArchPseudoRegInfo reginfo[APR_COUNT] = {
// 64-bit
{3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0},
// 32-bit
{2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0}, {2, 0},
// 16-bit
{1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
// 8-bit
{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1},
// 8-bit with REX, not double-counting the regular 8-bit regs:
{0, 0}, {0, 0}, {0, 0}, {0, 0},
{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
// SSE registers
{3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0}, {3, 0},
// segments:
{1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
// special:
{3, 0}, {3, 0},
};
const byte reg64_to_uniform_reg[16] = { APR_rax, APR_rcx, APR_rdx, APR_rbx, APR_rsp, APR_rbp, APR_rsi, APR_rdi, APR_r8, APR_r9, APR_r10, APR_r11, APR_r12, APR_r13, APR_r14, APR_r15 };
const byte xmmreg_to_uniform_reg[16] = { APR_xmm0, APR_xmm1, APR_xmm2, APR_xmm3, APR_xmm4, APR_xmm5, APR_xmm6, APR_xmm7, APR_xmm8, APR_xmm9, APR_xmm10, APR_xmm11, APR_xmm12, APR_xmm13, APR_xmm14, APR_xmm15 };
const byte reg32_to_uniform_reg[16] = { APR_eax, APR_ecx, APR_edx, APR_ebx, APR_esp, APR_ebp, APR_esi, APR_edi, APR_r8d, APR_r9d, APR_r10d, APR_r11d, APR_r12d, APR_r13d, APR_r14d, APR_r15d };
const byte reg16_to_uniform_reg[16] = { APR_ax, APR_cx, APR_dx, APR_bx, APR_sp, APR_bp, APR_si, APR_di, APR_r8w, APR_r9w, APR_r10w, APR_r11w, APR_r12w, APR_r13w, APR_r14w, APR_r15w };
const byte reg8_to_uniform_reg[8] = { APR_al, APR_cl, APR_dl, APR_bl, APR_ah, APR_ch, APR_dh, APR_bh };
const byte reg8x_to_uniform_reg[16] = { APR_al, APR_cl, APR_dl, APR_bl, APR_spl, APR_bpl, APR_sil, APR_dil, APR_r8b, APR_r9b, APR_r10b, APR_r11b, APR_r12b, APR_r13b, APR_r14b, APR_r15b };
const byte segreg_to_uniform_reg[16] = { APR_es, APR_cs, APR_ss, APR_ds, APR_fs, APR_zero, APR_zero };
const byte arch_pseudo_reg_to_arch_reg[APR_COUNT] = {
// 64-bit
REG_rax, REG_rcx, REG_rdx, REG_rbx, REG_rsp, REG_rbp, REG_rsi, REG_rdi, REG_r8, REG_r9, REG_r10, REG_r11, REG_r12, REG_r13, REG_r14, REG_r15,
// 32-bit
REG_rax, REG_rcx, REG_rdx, REG_rbx, REG_rsp, REG_rbp, REG_rsi, REG_rdi, REG_r8, REG_r9, REG_r10, REG_r11, REG_r12, REG_r13, REG_r14, REG_r15,
// 16-bit
REG_rax, REG_rcx, REG_rdx, REG_rbx, REG_rsp, REG_rbp, REG_rsi, REG_rdi, REG_r8, REG_r9, REG_r10, REG_r11, REG_r12, REG_r13, REG_r14, REG_r15,
// 8-bit
REG_rax, REG_rcx, REG_rdx, REG_rbx, REG_rax, REG_rcx, REG_rdx, REG_rbx,
// 8-bit with REX, not double-counting the regular 8-bit regs:
REG_rsp, REG_rbp, REG_rsi, REG_rdi,
REG_r8, REG_r9, REG_r10, REG_r11, REG_r12, REG_r13, REG_r14, REG_r15,
// SSE registers
REG_xmml0, REG_xmml1, REG_xmml2, REG_xmml3, REG_xmml4, REG_xmml5, REG_xmml6, REG_xmml7, REG_xmml8, REG_xmml9, REG_xmml10, REG_xmml11, REG_xmml12, REG_xmml13, REG_xmml14, REG_xmml15,
// segments:
REG_zero, REG_zero, REG_zero, REG_zero, REG_zero, REG_zero,
// special:
REG_rip, REG_zero
};
static const byte onebyte_has_modrm[256] = {
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* ------------------------------- */
/* 00 */ 1,1,1,1,_,_,_,_,1,1,1,1,_,_,_,_, /* 00 */
/* 10 */ 1,1,1,1,_,_,_,_,1,1,1,1,_,_,_,_, /* 10 */
/* 20 */ 1,1,1,1,_,_,_,_,1,1,1,1,_,_,_,_, /* 20 */
/* 30 */ 1,1,1,1,_,_,_,_,1,1,1,1,_,_,_,_, /* 30 */
/* 40 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 40 */
/* 50 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 50 */
/* 60 */ _,_,1,1,_,_,_,_,_,1,_,1,_,_,_,_, /* 60 */
/* 70 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 70 */
/* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
/* 90 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 90 */
/* a0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* a0 */
/* b0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* b0 */
/* c0 */ 1,1,_,_,1,1,1,1,_,_,_,_,_,_,_,_, /* c0 */
/* d0 */ 1,1,1,1,_,_,_,_,1,1,1,1,1,1,1,1, /* d0 */
/* e0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* e0 */
/* f0 */ _,_,_,_,_,_,1,1,_,_,_,_,_,_,1,1 /* f0 */
/* ------------------------------- */
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
};
static const byte twobyte_has_modrm[256] = {
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* ------------------------------- */
/* 00 */ 1,1,1,1,_,_,_,_,_,_,_,_,_,1,_,1, /* 0f */
/* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
/* 20 */ 1,1,1,1,1,_,1,_,1,1,1,1,1,1,1,1, /* 2f */
/* 30 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 3f */
/* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
/* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
/* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
/* 70 */ 1,1,1,1,1,1,1,_,_,_,_,_,1,1,1,1, /* 7f */
/* 80 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 8f */
/* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
/* a0 */ _,_,_,1,1,1,1,1,_,_,_,1,1,1,1,1, /* af */
/* b0 */ 1,1,1,1,1,1,1,1,_,_,1,1,1,1,1,1, /* bf */
/* c0 */ 1,1,1,1,1,1,1,1,_,_,_,_,_,_,_,_, /* cf */
/* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
/* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
/* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,_ /* ff */
/* ------------------------------- */
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
};
static const byte twobyte_uses_SSE_prefix[256] = {
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* ------------------------------- */
/* 00 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 0f */
/* 10 */ 1,1,1,1,1,1,1,1,_,_,_,_,_,_,_,_, /* 1f */
/* 20 */ _,_,_,_,_,_,_,_,1,1,1,1,1,1,1,1, /* 2f */
/* 30 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 3f */
/* 40 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 4f */
/* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
/* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
/* 70 */ 1,1,1,1,1,1,1,_,_,_,_,_,1,1,1,1, /* 7f */
/* 80 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 8f */
/* 90 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* 9f */
/* a0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* af */
/* b0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /* bf */
/* c0 */ _,_,1,_,1,1,1,_,_,_,_,_,_,_,_,_, /* cf */
/* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
/* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
/* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,_ /* ff */
/* ------------------------------- */
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
};
//
// This determines if the insn is handled by the
// fast decoder or the complex microcode decoder.
// The expanded x86 opcodes are from 0x000 to 0x1ff,
// i.e. normal ones and those with the 0x0f prefix:
//
static const byte insn_is_simple[512] = {
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
/* ------------------------------- */
/* 00 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,_, /* 0f */
/* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
/* 20 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 2f */
/* 30 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 3f */
/* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
/* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
/* 60 */ _,_,_,1,_,_,_,_,1,1,1,1,_,_,_,_, /* 6f */
/* 70 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 7f */
/* 80 */ 1,1,1,1,1,1,_,_,1,1,1,1,_,1,_,1, /* 8f */
/* 90 */ 1,_,_,_,_,_,_,_,1,1,_,_,_,_,1,1, /* 9f */
/* a0 */ 1,1,1,1,_,_,_,_,1,1,_,_,_,_,_,_, /* af */
/* b0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* bf */
/* c0 */ 1,1,1,1,_,_,1,1,1,1,_,_,_,_,_,_, /* cf */
/* d0 */ 1,1,1,1,_,_,_,_,_,_,_,_,_,_,_,_, /* df */
/* e0 */ _,_,_,_,_,_,_,_,1,1,_,1,_,_,_,_, /* ef */
/* f0 */ _,_,_,_,_,1,1,1,1,1,_,_,0,0,1,1, /* ff */
/*100 */ _,_,_,_,_,_,_,_,_,_,_,_,_,1,_,_, /*10f */
/*110 */ _,_,_,_,_,_,_,_,1,_,_,_,_,_,_,_, /*11f */
/*120 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*12f */
/*130 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*13f */
/*140 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*14f */
/*150 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*15f */
/*160 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*16f */
/*170 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*17f */
/*180 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*18f */
/*190 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /*19f */
/*1a0 */ _,_,_,1,_,_,_,_,_,_,_,1,_,_,_,1, /*1af */
/*1b0 */ _,_,_,1,_,_,1,1,_,_,1,1,1,1,1,1, /*1bf */
/*1c0 */ _,_,_,_,_,_,_,_,1,1,1,1,1,1,1,1, /*1cf */
/*1d0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*1df */
/*1e0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*1ef */
/*1f0 */ _,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_, /*1ff */
/* ------------------------------- */
/* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
};
#undef _
static int transop_histogram[MAX_TRANSOPS_PER_USER_INSN+1];
void TraceDecoder::reset() {
byteoffset = 0;
transbufcount = 0;
pfec = 0;
faultaddr = 0;
prefixes = 0;
rex = 0;
modrm = 0;
user_insn_count = 0;
// Note(AE): commit d278b597 changed this to zero, previously this was always
// reset to 1 from the very beginning. The commit mentions that this was done
// to ensure that every basic block updates flags at least once, but does not
// give any reason/indication of *why* this was done. I'm unsure whether
// anything depends on this behavior now, but at least theoretically, there
// should be no harm in setting this to one.
last_flags_update_was_atomic = 1;
invalid = 0;
some_insns_complex = 0;
used_microcode_assist = 0;
end_of_block = 0;
first_uop_in_insn = 1;
split_basic_block_at_locks_and_fences = 0;
split_invalid_basic_blocks = 0;
no_partial_flag_updates_per_insn = 0;
fast_length_decode_only = 0;
join_with_prev_insn = 0;
outcome = DECODE_OUTCOME_OK;
stop_at_rip = limits<W64>::max;
}
TraceDecoder::TraceDecoder(const RIPVirtPhys& rvp) {
reset();
bb.reset(rvp);
rip = rvp;
ripstart = rvp;
use64 = rvp.use64;
kernel = rvp.kernel;
dirflag = rvp.df;
}
TraceDecoder::TraceDecoder(Waddr rip, bool use64, bool kernel, bool df) {
reset();
bb.reset();
setzero(bb.rip);
bb.rip.rip = rip;
bb.rip.use64 = use64;
bb.rip.kernel = kernel;
bb.rip.df = df;
this->rip = rip;
this->ripstart = rip;
this->use64 = use64;
this->kernel = kernel;
this->dirflag = df;
}
TraceDecoder::TraceDecoder(Context& ctx, Waddr rip) {
reset();
use64 = ctx.use64;
#ifdef PTLSIM_HYPERVISOR
kernel = ctx.kernel_mode;
#else
kernel = 0;
#endif
dirflag = ((ctx.internal_eflags & FLAG_DF) != 0);
bb.reset();
setzero(bb.rip);
bb.rip.rip = rip;
bb.rip.use64 = use64;
bb.rip.kernel = kernel;
bb.rip.df = dirflag;
this->rip = rip;
ripstart = rip;
}
void TraceDecoder::put(const TransOp& transop) {
assert(transbufcount < MAX_TRANSOPS_PER_USER_INSN);
transbuf[transbufcount++] = transop;
if (transop.setflags)
last_flags_update_was_atomic = (transop.setflags == 0x7);
}
bool TraceDecoder::flush() {
if unlikely (!transbufcount) {
return true;
}
//
// Can we fit all the uops in this x86 insn?
//
// If not, remove the pending uops and split
// the BB with an uncond branch.
//
// We reserve 2 uops for the splitting branch
// and a possible collcc if needed.
//
bool overflow = (transbufcount >= ((MAX_BB_UOPS-2) - bb.count));
if unlikely (overflow) {
if (logable(5)) {
logfile << "Basic block overflowed (too many uops) during decode of ", bb.rip, " (ripstart ", (void*)ripstart,
"): req ", transbufcount, " uops but only have ", ((MAX_BB_UOPS-2) - bb.count), " free", endl;
}
assert(!first_insn_in_bb());
transbufcount = 0;
// This will set join_with_prev_insn and fill in the (collcc, bru) ops
split_before();
}
if unlikely (join_with_prev_insn) {
//
// Reopen the previous instruction
//
assert(bb.count > 0);
int i = bb.count-1;
while (i >= 0) {
TransOp& prevop = bb.transops[i];
prevop.eom = 0;
prevop.final_flags_in_insn = 0;
i--;
if unlikely (prevop.som) {
rip = ripstart;
ripstart -= prevop.bytes;
break;
}
}
}
int bytes = (rip - ripstart);
assert(bytes <= 15);
TransOp& first = transbuf[0];
TransOp& last = transbuf[transbufcount-1];
first.som = (!join_with_prev_insn);
last.eom = 1;
bool contains_branch = isbranch(last.opcode);
W8s final_archreg_writer[ARCHREG_COUNT];
memset(final_archreg_writer, 0xff, sizeof(final_archreg_writer));
W8s final_flags_writer = -1;
byte flag_sets_set = 0;
foreach (i, transbufcount) {
TransOp& transop = transbuf[i];
if likely (transop.rd < ARCHREG_COUNT) { final_archreg_writer[transop.rd] = i; }
bool sets_all_flags = ((transop.setflags == 7) && (!transop.nouserflags));
if unlikely (sets_all_flags) final_flags_writer = i;
if likely (!transop.nouserflags) flag_sets_set |= transop.setflags;
}
if unlikely (no_partial_flag_updates_per_insn) {
if unlikely ((flag_sets_set != 0) && (flag_sets_set != (SETFLAG_ZF|SETFLAG_CF|SETFLAG_OF))) {
logfile << "Invalid partial flag sets at rip ", (void*)ripstart, " (flag sets ", flag_sets_set, ")", endl;
assert(false);
}
}
foreach (i, transbufcount) {
TransOp& transop = transbuf[i];
if unlikely (bb.count >= MAX_BB_UOPS) {
logfile << "ERROR: Too many transops (", bb.count, ") in basic block ", bb.rip, " (current RIP: ", (void*)ripstart, ") (max ", MAX_BB_UOPS, " allowed)", endl;
logfile << bb;
assert(bb.count < MAX_BB_UOPS);
}
bool ld = isload(transop.opcode);
bool st = isstore(transop.opcode);
bool br = isbranch(transop.opcode);
if unlikely (br) {
switch (transop.opcode) {
case OP_br: bb.type = BB_TYPE_COND; break;
case OP_bru: bb.type = BB_TYPE_UNCOND; break;
case OP_jmp: bb.type = BB_TYPE_INDIR; break;
case OP_brp: bb.type = BB_TYPE_ASSIST; break;
default: assert(false); break;
}
bb.call = ((transop.extshift & BRANCH_HINT_PUSH_RAS) != 0);
bb.ret = ((transop.extshift & BRANCH_HINT_POP_RAS) != 0);
bb.rip_taken = transop.riptaken;
bb.rip_not_taken = transop.ripseq;
}
transop.unaligned = 0;
transop.bytes = bytes;
transop.bbindex = bb.count;
transop.is_x87 = is_x87;
transop.is_sse = is_sse;
transop.final_insn_in_bb = contains_branch;
transop.final_arch_in_insn = (transop.rd < ARCHREG_COUNT) && (final_archreg_writer[transop.rd] == i);
transop.final_flags_in_insn = (final_flags_writer == i);
transop.any_flags_in_insn = (flag_sets_set != 0);
bb.x87 |= is_x87;
bb.sse |= is_sse;
bb.transops[bb.count++] = transop;
if (ld|st) bb.memcount++;
if (st) bb.storecount++;
bb.tagcount++;
if (transop.rd < ARCHREG_COUNT) setbit(bb.usedregs, transop.rd);
if (transop.ra < ARCHREG_COUNT) setbit(bb.usedregs, transop.ra);
if (transop.rb < ARCHREG_COUNT) setbit(bb.usedregs, transop.rb);
if (transop.rc < ARCHREG_COUNT) setbit(bb.usedregs, transop.rc);
}
stats.decoder.throughput.uops += transbufcount;
if (!join_with_prev_insn) {
bb.user_insn_count++;
bb.bytes += bytes;
stats.decoder.throughput.x86_insns++;
stats.decoder.throughput.bytes += bytes;
}
transbufcount = 0;
return (!overflow);
}
ostream& DecodedOperand::print(ostream& os) const {
switch (type) {
case OPTYPE_REG:
os << uniform_arch_reg_names[reg.reg]; break;
case OPTYPE_IMM:
os << hexstring(imm.imm, 64); break;
case OPTYPE_MEM:
os << "mem", (1<<mem.size), " [", uniform_arch_reg_names[mem.basereg], " + ", uniform_arch_reg_names[mem.indexreg], "*", (1 << mem.scale), " + ", hexstring(mem.offset, 64),
(mem.riprel) ? " riprel" : "", "]";
break;
default:
break;
}
return os;
}
bool DecodedOperand::gform_ext(TraceDecoder& state, int bytemode, int regfield, bool def64, bool in_rex_base) {
int add = ((in_rex_base) ? state.rex.extbase : state.rex.extreg) ? 8 : 0;
this->type = OPTYPE_REG;
switch (bytemode) {
case b_mode: this->reg.reg = (state.rex) ? reg8x_to_uniform_reg[regfield + add] : reg8_to_uniform_reg[regfield]; break;
case w_mode: this->reg.reg = reg16_to_uniform_reg[regfield + add]; break;
case d_mode: this->reg.reg = reg32_to_uniform_reg[regfield + add]; break;
case q_mode: this->reg.reg = reg64_to_uniform_reg[regfield + add]; break;
case v_mode:
case dq_mode:
this->reg.reg = (state.rex.mode64 | (def64 & (!state.opsize_prefix))) ? reg64_to_uniform_reg[regfield + add] :
((!state.opsize_prefix) | (bytemode == dq_mode)) ? reg32_to_uniform_reg[regfield + add] :
reg16_to_uniform_reg[regfield + add];
break;
case x_mode: this->reg.reg = xmmreg_to_uniform_reg[regfield + add]; break;
default: return false;
}
return true;
}
bool DecodedOperand::gform(TraceDecoder& state, int bytemode) {
return gform_ext(state, bytemode, state.modrm.reg);
}
bool DecodedOperand::iform(TraceDecoder& state, int bytemode) {
this->type = OPTYPE_IMM;
this->imm.imm = 0;
switch (bytemode) {
case b_mode:
this->imm.imm = (W8s)state.fetch1(); break;
case q_mode:
this->imm.imm = (W64s)state.fetch8(); break;
case v_mode:
// NOTE: Even if rex.mode64 is specified, immediates are never longer than 32 bits (except for mov):
if (state.rex.mode64 | (!state.opsize_prefix)) {
this->imm.imm = (W32s)state.fetch4();
} else {
this->imm.imm = (W16s)state.fetch2();
}
break;
case w_mode:
this->imm.imm = (W16s)state.fetch2(); break;
default:
return false;
}
return true;
}
bool DecodedOperand::iform64(TraceDecoder& state, int bytemode) {
this->type = OPTYPE_IMM;
this->imm.imm = 0;
switch (bytemode) {
case b_mode:
this->imm.imm = (W8s)state.fetch1(); break;
case q_mode:
this->imm.imm = (W64s)state.fetch8(); break;
case v_mode:
if (state.rex.mode64) {
this->imm.imm = (W64s)state.fetch8();
} else if (state.opsize_prefix) {
this->imm.imm = (W16s)state.fetch2();
} else {
this->imm.imm = (W32s)state.fetch4();
}
break;
case w_mode:
this->imm.imm = (W16s)state.fetch2(); break;
case d_mode:
this->imm.imm = (W32s)state.fetch4(); break;
default:
return false;
}
return true;
}
bool DecodedOperand::eform(TraceDecoder& state, int bytemode) {
if (state.modrm.mod == 3) {
return gform_ext(state, bytemode, state.modrm.rm, false, true);
}
type = OPTYPE_MEM;
mem.basereg = APR_zero;
mem.indexreg = APR_zero;
mem.offset = 0;
mem.scale = 0;
mem.riprel = 0;
mem.size = 0;
const int mod_and_rexextbase_and_rm_to_basereg_x86_64[4][2][8] = {
{
// mod = 00
{APR_rax, APR_rcx, APR_rdx, APR_rbx, -1, APR_rip, APR_rsi, APR_rdi}, // rex.extbase = 0
{APR_r8, APR_r9, APR_r10, APR_r11, -1, APR_rip, APR_r14, APR_r15}, // rex.extbase = 1
}, {
// mod = 01
{APR_rax, APR_rcx, APR_rdx, APR_rbx, -1, APR_rbp, APR_rsi, APR_rdi}, // rex.extbase = 0
{APR_r8, APR_r9, APR_r10, APR_r11, -1, APR_r13, APR_r14, APR_r15}, // rex.extbase = 1
}, {
// mod = 10
{APR_rax, APR_rcx, APR_rdx, APR_rbx, -1, APR_rbp, APR_rsi, APR_rdi}, // rex.extbase = 0
{APR_r8, APR_r9, APR_r10, APR_r11, -1, APR_r13, APR_r14, APR_r15}, // rex.extbase = 1
}, {
// mod = 11: not possible since this is g-form
{-1, -1, -1, -1, -1, -1, -1, -1},
{-1, -1, -1, -1, -1, -1, -1, -1},
}
};
const int mod_and_rm_to_basereg_x86[4][8] = {
{APR_eax, APR_ecx, APR_edx, APR_ebx, -1, APR_zero, APR_esi, APR_edi},
{APR_eax, APR_ecx, APR_edx, APR_ebx, -1, APR_ebp, APR_esi, APR_edi},
{APR_eax, APR_ecx, APR_edx, APR_ebx, -1, APR_ebp, APR_esi, APR_edi},
{-1, -1, -1, -1, -1, -1, -1, -1}, // mod = 11: not possible since this is g-form
};
mem.basereg = (state.use64)
? mod_and_rexextbase_and_rm_to_basereg_x86_64[state.modrm.mod][state.rex.extbase][state.modrm.rm]
: mod_and_rm_to_basereg_x86[state.modrm.mod][state.modrm.rm];
SIBByte sib;
if (state.modrm.rm == 4) {
sib = SIBByte(state.fetch1());
}
const byte mod_and_rm_to_immsize[4][8] = {
{0, 0, 0, 0, 0, 4, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{4, 4, 4, 4, 4, 4, 4, 4},
{0, 0, 0, 0, 0, 0, 0, 0},
};
byte immsize = mod_and_rm_to_immsize[state.modrm.mod][state.modrm.rm];
mem.offset = (immsize) ? signext32((W32s)state.fetch(immsize), immsize*8) : 0;
mem.riprel = (mem.basereg == APR_rip);
if (mem.basereg < 0) {
// Have sib
const int rexextbase_and_base_to_basereg[2][8] = {
{APR_rax, APR_rcx, APR_rdx, APR_rbx, APR_rsp, -1, APR_rsi, APR_rdi}, // rex.extbase = 0
{APR_r8, APR_r9, APR_r10, APR_r11, APR_r12, -1, APR_r14, APR_r15}, // rex.extbase = 1
};
mem.basereg = rexextbase_and_base_to_basereg[state.rex.extbase][sib.base];
if (mem.basereg < 0) {
const int rexextbase_and_mod_to_basereg[2][4] = {
{APR_zero, APR_rbp, APR_rbp, -1}, // rex.extbase = 0
{APR_zero, APR_r13, APR_r13, -1}, // rex.extbase = 1
};
mem.basereg = rexextbase_and_mod_to_basereg[state.rex.extbase][state.modrm.mod];
if (!immsize) {
switch (state.modrm.mod) {
case 0:
case 2:
assert(!immsize);
mem.offset = (W32s)state.fetch4();
break;
case 1:
assert(!immsize);
mem.offset = (W8s)state.fetch1();
break;
}
}
}
const int rexextindex_and_index_to_indexreg[2][8] = {
{APR_rax, APR_rcx, APR_rdx, APR_rbx, APR_zero, APR_rbp, APR_rsi, APR_rdi}, // rex.extindex = 0
{APR_r8, APR_r9, APR_r10, APR_r11, APR_r12, APR_r13, APR_r14, APR_r15}, // rex.extindex = 1
};
mem.indexreg = rexextindex_and_index_to_indexreg[state.rex.extindex][sib.index];
mem.scale = sib.scale;
}
switch (bytemode) {
case b_mode: mem.size = 0; break;
case w_mode: mem.size = 1; break;
case d_mode: mem.size = 2; break;
case q_mode: mem.size = 3; break;
// case m_mode: mem.size = (state.use64) ? 3 : 2; break;
case v_mode: case dq_mode: mem.size = (state.rex.mode64) ? 3 : ((!state.opsize_prefix) | (bytemode == dq_mode)) ? 2 : 1; break; // See table 1.2 (p35) of AMD64 ISA manual
case x_mode: mem.size = 3; break;
default: return false;
}
return true;
}
bool DecodedOperand::varreg(TraceDecoder& state, int regcode, bool def64) {
this->type = OPTYPE_REG;
// push and pop default to 64 bits in 64-bit mode, while all others default to 32 bit mode and need the REX prefix to make them 64-bit:
// assert(mode_64bit);
if (def64) {
// Always a 64-bit operation
this->reg.reg = reg64_to_uniform_reg[regcode + (state.rex.extbase * 8)];
} else {
this->reg.reg = (state.rex.mode64) ? reg64_to_uniform_reg[regcode + (state.rex.extbase * 8)] :
(state.opsize_prefix) ? reg16_to_uniform_reg[regcode + (state.rex.extbase * 8)]
: reg32_to_uniform_reg[regcode + (state.rex.extbase * 8)];
}
return true;
}
bool DecodedOperand::varreg_def64(TraceDecoder& state, int regcode) {
return DecodedOperand::varreg(state, regcode, true);
}
bool DecodedOperand::varreg_def32(TraceDecoder& state, int regcode) {
return DecodedOperand::varreg(state, regcode, false);
}
void TraceDecoder::immediate(int rdreg, int sizeshift, W64s imm, bool issigned) {
int totalbits = (sizeshift == 3) ? 64 : (8 * (1 << sizeshift));
if (totalbits < 64) imm = (issigned) ? signext64(imm, totalbits) : bits(imm, 0, totalbits);
// Only byte and word sized immediates need to be merged with the previous value:
this << TransOp(OP_mov, rdreg, REG_zero, REG_imm, REG_zero, 3, imm);
}
void TraceDecoder::abs_code_addr_immediate(int rdreg, int sizeshift, W64 imm) {
this << TransOp(OP_add, rdreg, REG_trace, REG_imm, REG_zero, sizeshift, signext64(imm, 48));
}
int TraceDecoder::bias_by_segreg(int basereg) {
if (prefixes & (PFX_CS|PFX_DS|PFX_ES|PFX_FS|PFX_GS|PFX_SS)) {
int segid =
(prefixes & PFX_FS) ? SEGID_FS :
(prefixes & PFX_GS) ? SEGID_GS :
(prefixes & PFX_DS) ? SEGID_DS :
(prefixes & PFX_SS) ? SEGID_SS :
(prefixes & PFX_ES) ? SEGID_ES :
(prefixes & PFX_CS) ? SEGID_CS : -1;
assert(segid >= 0);
int varoffs = offsetof_(Context, seg[segid].base);
TransOp ldp(OP_ld, REG_temp6, REG_ctx, REG_imm, REG_zero, 3, varoffs); ldp.internal = 1; this << ldp;
this << TransOp(OP_add, REG_temp6, REG_temp6, basereg, REG_zero, 3);
return REG_temp6;
}
return basereg;
}
void TraceDecoder::address_generate_and_load_or_store(int destreg, int srcreg, const DecodedOperand& memref, int opcode, int datatype, int cachelevel, bool force_seg_bias, bool rmw) {
//
// In the address generation form used by internally generated
// uops, we need the full virtual address, including the segment base
// bias. However, technically LEA is not supposed to add the segment
// base, even if prefixes implying a base are applied.
//
// Therefore, any internal uses that need the full virtual address
// can use the special hint force_seg_bias to generate it,
// but not actually include any load or store uops.
//
bool memop = isload(opcode) | isstore(opcode) | (opcode == OP_ld_pre);
force_seg_bias |= memop;
int imm_bits = (memop) ? 32 : 64;
int basereg = arch_pseudo_reg_to_arch_reg[memref.mem.basereg];
int indexreg = arch_pseudo_reg_to_arch_reg[memref.mem.indexreg];
// ld rd = ra,rb,rc
// ra = base
// rb = offset or imm8
// rc = reg to merge low bytes with
//
// Encoding rules for loads:
//
// Loads have only one addressing mode: basereg + immN, where
// N is a small number of bits. The immediate is shifted left
// by ld.size to allow for a greater range, assuming the immN
// is always a multiple of the load data size. If immN is not
// properly aligned, or it exceeds the field width, it cannot
// be represented in the load and must be encoded outside as
// a separate move immediate uop.
//
// Note that the rbimm field in the load uop is not actually
// modified; this is for simulation purposes only, since real
// microprocessors do not have unlimited immediate lengths.
//
bool imm_is_not_encodable =
(lowbits(memref.mem.offset, memref.mem.size) != 0) |
(!fits_in_signed_nbit(memref.mem.offset >> memref.mem.size, imm_bits));
if unlikely (opcode == OP_add) {
// LEA and the like are always encodable since it's just an ADD uop:
imm_is_not_encodable = 0;
}
W64s offset = memref.mem.offset;
bool locked = ((prefixes & PFX_LOCK) != 0);
if (basereg == REG_rip) {
// [rip + imm32]: index always is zero and scale is 1
// This mode is only possible in x86-64 code
basereg = REG_zero;
if (force_seg_bias) basereg = bias_by_segreg(basereg);
if (memop) {
abs_code_addr_immediate(REG_temp8, 3, Waddr(rip) + offset);
TransOp ldst(opcode, destreg, REG_temp8, REG_imm, srcreg, memref.mem.size, 0);
ldst.datatype = datatype;
ldst.cachelevel = cachelevel;
ldst.locked = locked;
ldst.extshift = 0;
this << ldst;
} else {
assert(opcode == OP_add);
abs_code_addr_immediate(destreg, 3, Waddr(rip) + offset);
}
} else if (indexreg == REG_zero) {
// [ra + imm32] or [ra]
if (force_seg_bias) basereg = bias_by_segreg(basereg);
if (imm_is_not_encodable) {
this << TransOp(OP_add, REG_temp8, basereg, REG_imm, REG_zero, 3, offset);
basereg = REG_temp8;
offset = 0;
}