-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdecode-complex.cpp
2478 lines (2074 loc) · 79.8 KB
/
decode-complex.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 complex instructions
//
// Copyright 1999-2008 Matt T. Yourst <yourst@yourst.com>
//
#include <decode.h>
template <typename T> void assist_div(Context& ctx) {
Waddr rax = ctx.commitarf[REG_rax]; Waddr rdx = ctx.commitarf[REG_rdx];
asm("div %[divisor];" : "+a" (rax), "+d" (rdx) : [divisor] "q" ((T)ctx.commitarf[REG_ar1]));
ctx.commitarf[REG_rax] = rax; ctx.commitarf[REG_rdx] = rdx;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
template <typename T> void assist_idiv(Context& ctx) {
Waddr rax = ctx.commitarf[REG_rax]; Waddr rdx = ctx.commitarf[REG_rdx];
asm("idiv %[divisor];" : "+a" (rax), "+d" (rdx) : [divisor] "q" ((T)ctx.commitarf[REG_ar1]));
ctx.commitarf[REG_rax] = rax; ctx.commitarf[REG_rdx] = rdx;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
// Not possible in 64-bit mode
#ifndef __x86_64__
template <> void assist_div<W64>(Context& ctx) { assert(false); }
template <> void assist_idiv<W64>(Context& ctx) { assert(false); }
#endif
template void assist_div<byte>(Context& ctx);
template void assist_div<W16>(Context& ctx);
template void assist_div<W32>(Context& ctx);
template void assist_div<W64>(Context& ctx);
template void assist_idiv<byte>(Context& ctx);
template void assist_idiv<W16>(Context& ctx);
template void assist_idiv<W32>(Context& ctx);
template void assist_idiv<W64>(Context& ctx);
void assist_int(Context& ctx) {
byte intid = ctx.commitarf[REG_ar1];
#ifdef PTLSIM_HYPERVISOR
// The returned rip is nextrip for explicit intN instructions:
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
ctx.propagate_x86_exception(intid, 0);
#else
if (intid == 0x80) {
handle_syscall_32bit(SYSCALL_SEMANTICS_INT80);
} else if (intid == 0x01 || intid == 0x03) {
ctx.propagate_x86_exception(intid, 0);
} else {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault, 0);
}
#endif
}
#ifdef PTLSIM_HYPERVISOR
extern void handle_xen_hypercall_assist(Context& ctx);
extern void handle_syscall_assist(Context& ctx);
#endif
void assist_syscall(Context& ctx) {
#ifdef PTLSIM_HYPERVISOR
//
// SYSCALL has two distinct sets of semantics on Xen x86-64.
//
// When executed from user mode, it's a normal system call
// in the Linux sense, and Xen just relays control back
// to the guest domain's kernel.
//
if (ctx.kernel_mode) {
logfile << ctx, endl, flush;
assert(!ctx.kernel_mode);
}
handle_syscall_assist(ctx);
#else
if (ctx.use64) {
#ifdef __x86_64__
handle_syscall_64bit();
#endif
} else {
handle_syscall_32bit(SYSCALL_SEMANTICS_SYSCALL);
}
#endif
// REG_rip is filled out for us
}
void assist_hypercall(Context& ctx) {
#ifdef PTLSIM_HYPERVISOR
//
// SYSCALL has two distinct sets of semantics on Xen x86-64.
//
// When executed from kernel mode, it's interpreted as a
// hypercall into Xen itself.
//
if (!ctx.kernel_mode) {
logfile << ctx, endl, flush;
assert(ctx.kernel_mode);
}
handle_xen_hypercall_assist(ctx);
#endif
}
void assist_sysenter(Context& ctx) {
#ifdef PTLSIM_HYPERVISOR
//++MTY TODO
cerr << "assist_sysenter()", endl, flush;
assert(false);
#else
handle_syscall_32bit(SYSCALL_SEMANTICS_SYSENTER);
#endif
// REG_rip is filled out for us
}
static const char cpuid_vendor[12+1] = "GenuineIntel";
static const char cpuid_description[48+1] = "Intel(R) Xeon(TM) CPU 2.00 GHz ";
//static const char cpuid_vendor[12+1] = "PTLsimCPUx64";
//static const char cpuid_description[48+1] = "PTLsim Cycle Accurate x86-64 Simulator Model ";
//
// CPUID level 0x00000001, result in %edx
//
#define X86_FEATURE_FPU (1 << 0) // Onboard FPU
#define X86_FEATURE_VME (1 << 1) // Virtual Mode Extensions
#define X86_FEATURE_DE (1 << 2) // Debugging Extensions
#define X86_FEATURE_PSE (1 << 3) // Page Size Extensions
#define X86_FEATURE_TSC (1 << 4) // Time Stamp Counter
#define X86_FEATURE_MSR (1 << 5) // Model-Specific Registers, RDMSR, WRMSR
#define X86_FEATURE_PAE (1 << 6) // Physical Address Extensions
#define X86_FEATURE_MCE (1 << 7) // Machine Check Architecture
#define X86_FEATURE_CX8 (1 << 8) // CMPXCHG8 instruction
#define X86_FEATURE_APIC (1 << 9) // Onboard APIC
#define X86_FEATURE_BIT10 (1 << 10) // (undefined)
#define X86_FEATURE_SEP (1 << 11) // SYSENTER/SYSEXIT
#define X86_FEATURE_MTRR (1 << 12) // Memory Type Range Registers
#define X86_FEATURE_PGE (1 << 13) // Page Global Enable
#define X86_FEATURE_MCA (1 << 14) // Machine Check Architecture
#define X86_FEATURE_CMOV (1 << 15) // CMOV instruction (FCMOVCC and FCOMI too if FPU present)
#define X86_FEATURE_PAT (1 << 16) // Page Attribute Table
#define X86_FEATURE_PSE36 (1 << 17) // 36-bit PSEs
#define X86_FEATURE_PN (1 << 18) // Processor serial number
#define X86_FEATURE_CLFL (1 << 19) // Supports the CLFLUSH instruction
#define X86_FEATURE_NX (1 << 20) // No-Execute page attribute
#define X86_FEATURE_DTES (1 << 21) // Debug Trace Store
#define X86_FEATURE_ACPI (1 << 22) // ACPI via MSR
#define X86_FEATURE_MMX (1 << 23) // Multimedia Extensions
#define X86_FEATURE_FXSR (1 << 24) // FXSAVE and FXRSTOR instructions; CR4.OSFXSR available
#define X86_FEATURE_XMM (1 << 25) // Streaming SIMD Extensions
#define X86_FEATURE_XMM2 (1 << 26) // Streaming SIMD Extensions-2
#define X86_FEATURE_SNOOP (1 << 27) // CPU self snoop
#define X86_FEATURE_HT (1 << 28) // Hyper-Threading
#define X86_FEATURE_ACC (1 << 29) // Automatic clock control
#define X86_FEATURE_IA64 (1 << 30) // IA-64 processor
#define X86_FEATURE_BIT31 (1 << 31) // (undefined)
//
// Xen forces us to mask some features (vme, de, pse, pge, sep, mtrr)
// when returning the CPUID to a guest, since it uses these features itself.
//
#define PTLSIM_X86_FEATURE (\
X86_FEATURE_FPU | /*X86_FEATURE_VME | X86_FEATURE_DE | */ X86_FEATURE_PSE | \
X86_FEATURE_TSC | X86_FEATURE_MSR | X86_FEATURE_PAE | X86_FEATURE_MCE | \
X86_FEATURE_CX8 | X86_FEATURE_APIC | /*X86_FEATURE_BIT10 | X86_FEATURE_SEP | */ \
/*X86_FEATURE_MTRR | X86_FEATURE_PGE | */ X86_FEATURE_MCA | X86_FEATURE_CMOV | \
X86_FEATURE_PAT | X86_FEATURE_PSE36 | X86_FEATURE_PN | X86_FEATURE_CLFL | \
X86_FEATURE_NX | /*X86_FEATURE_DTES | */ X86_FEATURE_ACPI | X86_FEATURE_MMX | \
X86_FEATURE_FXSR | X86_FEATURE_XMM | X86_FEATURE_XMM2 | X86_FEATURE_SNOOP | \
X86_FEATURE_HT /* | X86_FEATURE_ACC | X86_FEATURE_IA64 | X86_FEATURE_BIT31*/)
//
// CPUID level 0x00000001, result in %ecx
//
#define X86_EXT_FEATURE_XMM3 (1 << 0) // Streaming SIMD Extensions-3
#define X86_EXT_FEATURE_MWAIT (1 << 3) // Monitor/Mwait support
#define X86_EXT_FEATURE_DSCPL (1 << 4) // CPL Qualified Debug Store
#define X86_EXT_FEATURE_EST (1 << 7) // Enhanced SpeedStep
#define X86_EXT_FEATURE_TM2 (1 << 8) // Thermal Monitor 2
#define X86_EXT_FEATURE_CID (1 << 10) // Context ID
#define X86_EXT_FEATURE_CX16 (1 << 13) // CMPXCHG16B
#define X86_EXT_FEATURE_XTPR (1 << 14) // Send Task Priority Messages
#define PTLSIM_X86_EXT_FEATURE (\
X86_EXT_FEATURE_XMM3 | X86_EXT_FEATURE_CX16)
//
// CPUID level 0x80000001, result in %edx
//
#define X86_VENDOR_FEATURE_SYSCALL (1 << 11) // SYSCALL/SYSRET
#define X86_VENDOR_FEATURE_MMXEXT (1 << 22) // AMD MMX extensions
#define X86_VENDOR_FEATURE_FXSR_OPT (1 << 25) // FXSR optimizations
#define X86_VENDOR_FEATURE_RDTSCP (1 << 27) // RDTSCP instruction
#define X86_VENDOR_FEATURE_LM (1 << 29) // Long Mode (x86-64)
#define X86_VENDOR_FEATURE_3DNOWEXT (1 << 30) // AMD 3DNow! extensions
#define X86_VENDOR_FEATURE_3DNOW (1 << 31) // 3DNow!
#define PTLSIM_X86_VENDOR_FEATURE \
(X86_VENDOR_FEATURE_FXSR_OPT | X86_VENDOR_FEATURE_LM | (PTLSIM_X86_FEATURE & 0x1ffffff))
//
// CPUID level 0x80000001, result in %ecx
//
#define X86_VENDOR_EXT_FEATURE_LAHF_LM (1 << 0) // LAHF/SAHF in long mode
#define X86_VENDOR_EXT_FEATURE_CMP_LEGACY (1 << 1) // If yes HyperThreading not valid
#define X86_VENDOR_EXT_FEATURE_SVM (1 << 2) // Secure Virtual Machine extensions
//
// Make sure we do NOT define CMP_LEGACY since PTLsim may have multiple threads
// per core enabled and the guest OS must optimize cache coherency as such.
//
#define PTLSIM_X86_VENDOR_EXT_FEATURE (X86_VENDOR_EXT_FEATURE_LAHF_LM)
union ProcessorModelInfo {
struct { W32 stepping:4, model:4, family:4, reserved1:4, extmodel:4, extfamily:8, reserved2:4; } fields;
W32 data;
};
union ProcessorMiscInfo {
struct { W32 brandid:8, clflush:8, reserved:8, apicid:8; } fields;
W32 data;
};
#define PTLSIM_X86_MODEL_INFO (\
(0 << 0) /* stepping */ | \
(0 << 4) /* model */ | \
(15 << 8) /* family */ | \
(0 << 12) /* reserved1 */ | \
(0 << 16) /* extmodel */ | \
(0 << 20) /* extfamily */ | \
(0 << 24))
#define PTLSIM_X86_MISC_INFO (\
(0 << 0) /* brandid */ | \
(8 << 8) /* line size (8 x 8 = 64) */ | \
(0 << 16) /* reserved */ | \
(0 << 24)) /* APIC ID (must be patched later!) */
void assist_cpuid(Context& ctx) {
W64& rax = ctx.commitarf[REG_rax];
W64& rbx = ctx.commitarf[REG_rbx];
W64& rcx = ctx.commitarf[REG_rcx];
W64& rdx = ctx.commitarf[REG_rdx];
W32 func = rax;
if (logable(4)) {
logfile << "assist_cpuid: func 0x", hexstring(func, 32), " called from ",
(void*)(Waddr)ctx.commitarf[REG_selfrip], ":", endl;
}
switch (func) {
case 0: {
// Max avail function spec and vendor ID:
const W32* vendor = (const W32*)&cpuid_vendor;
rax = 2; // two extended function
rbx = vendor[0];
rdx = vendor[1];
rcx = vendor[2];
break;
}
case 1: {
// Model and capability information
rax = PTLSIM_X86_MODEL_INFO; // model
rbx = PTLSIM_X86_MISC_INFO | (ctx.vcpuid << 24);
rcx = PTLSIM_X86_EXT_FEATURE;
rdx = PTLSIM_X86_FEATURE;
break;
}
case 2: {
// Dummy cache informations, required by glibc
rax = 0; // TODO
rbx = 0;
rcx = 0;
rdx = 0;
break;
}
case 0x80000000: {
// Max avail extended function spec and vendor ID:
const W32* vendor = (const W32*)&cpuid_vendor;
rax = 4;
rbx = vendor[0];
rdx = vendor[1];
rcx = vendor[2];
break;
}
case 0x80000001: {
// extended feature info
rax = PTLSIM_X86_MODEL_INFO;
rbx = 0; // brand ID
rcx = PTLSIM_X86_VENDOR_EXT_FEATURE;
rdx = PTLSIM_X86_VENDOR_FEATURE;
break;
}
case 0x80000002 ... 0x80000004: {
// processor name string
const W32* cpudesc = (const W32*)(&cpuid_description[(func - 0x80000002)*16]);
rax = cpudesc[0];
rbx = cpudesc[1];
rcx = cpudesc[2];
rdx = cpudesc[3];
break;
}
default: {
W32 eax, ebx, ecx, edx;
cpuid(func, eax, ebx, ecx, edx);
rax = eax;
rbx = ebx;
rcx = ecx;
rdx = edx;
break;
}
}
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_rdtsc(Context& ctx) {
W64& rax = ctx.commitarf[REG_rax];
W64& rdx = ctx.commitarf[REG_rdx];
#ifdef PTLSIM_HYPERVISOR
W64 tsc = ctx.base_tsc + sim_cycle;
#else
W64 tsc = sim_cycle;
#endif
rax = LO32(tsc);
rdx = HI32(tsc);
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
//
// Pop from stack into flags register, with checking for reserved bits
//
void assist_popf(Context& ctx) {
W32 flags = ctx.commitarf[REG_ar1];
// bit 1 is always '1', and bits {3, 5, 15} are always '0':
flags = (flags | (1 << 1)) & (~((1 << 3) | (1 << 5) | (1 << 15)));
ctx.internal_eflags = flags & ~(FLAG_ZAPS|FLAG_CF|FLAG_OF);
ctx.commitarf[REG_flags] = flags & (FLAG_ZAPS|FLAG_CF|FLAG_OF);
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
// Update internal flags too (only update non-standard flags in internal_flags_bits):
// Equivalent to these uops:
// this << TransOp(OP_and, REG_temp1, REG_temp0, REG_imm, REG_zero, 2, ~(FLAG_ZAPS|FLAG_CF|FLAG_OF));
// TransOp stp(OP_st, REG_mem, REG_ctx, REG_imm, REG_temp1, 2, offsetof(Context, internal_eflags)); stp.internal = 1; this << stp;
// this << TransOp(OP_movrcc, REG_temp0, REG_zero, REG_temp0, REG_zero, 3, 0, 0, FLAGS_DEFAULT_ALU);
}
//
// CLD and STD must be barrier assists since a new RIPVirtPhys
// context key may be active after the direction flag is altered.
//
void assist_cld(Context& ctx) {
ctx.internal_eflags &= ~FLAG_DF;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_std(Context& ctx) {
ctx.internal_eflags |= FLAG_DF;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
//
// PTL calls
//
extern void assist_ptlcall(Context& ctx);
void assist_write_segreg(Context& ctx) {
W16 selector = ctx.commitarf[REG_ar1];
byte segid = ctx.commitarf[REG_ar2];
int exception = ctx.write_segreg(segid, selector);
if unlikely (exception) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(exception, selector);
return;
}
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_ldmxcsr(Context& ctx) {
//
// LDMXCSR needs to flush the pipeline since future FP instructions will
// depend on its value and can't be issued out of order w.r.t the mxcsr.
//
W32 mxcsr = (W32)ctx.commitarf[REG_ar1];
// Top bit of mxcsr archreg doubles as direction flag and other misc flags: preserve it
ctx.mxcsr = (ctx.mxcsr & 0xffffffff00000000ULL) | mxcsr;
// We can't have exceptions going on inside PTLsim: virtualize this feature in uopimpl code
// Everything else will be used by real SSE insns inside uopimpls.
mxcsr |= MXCSR_EXCEPTION_DISABLE_MASK;
x86_set_mxcsr(mxcsr);
//
// Technically all FP uops should update the sticky exception bits in the mxcsr
// if marked as such (i.e. non-x87). Presently we don't do this, so hopefully
// no code checks for exception conditions in this manner. Otherwise each FP
// uopimpl would need to update a speculative version of the mxcsr.
//
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_fxsave(Context& ctx) {
FXSAVEStruct state;
ctx.fxsave(state);
Waddr target = ctx.commitarf[REG_ar1] & ctx.virt_addr_mask;
PageFaultErrorCode pfec;
Waddr faultaddr;
int bytes = ctx.copy_to_user(target, &state, sizeof(state), pfec, faultaddr);
if (bytes < sizeof(state)) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_page_fault, pfec, faultaddr);
return;
}
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_fxrstor(Context& ctx) {
FXSAVEStruct state;
Waddr target = ctx.commitarf[REG_ar1] & ctx.virt_addr_mask;
PageFaultErrorCode pfec;
Waddr faultaddr;
int bytes = ctx.copy_from_user(&state, target, sizeof(state), pfec, faultaddr);
if (bytes < sizeof(state)) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_page_fault, pfec, faultaddr);
return;
}
ctx.fxrstor(state);
// We can't have exceptions going on inside PTLsim: virtualize this feature in uopimpl code
// Everything else will be used by real SSE insns inside uopimpls.
W32 mxcsr = ctx.mxcsr | MXCSR_EXCEPTION_DISABLE_MASK;
x86_set_mxcsr(mxcsr);
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_wrmsr(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
#ifdef PTLSIM_HYPERVISOR
if (ctx.kernel_mode) {
W64 value = (ctx.commitarf[REG_rdx] << 32) | LO32(ctx.commitarf[REG_rax]);
W32 msr = ctx.commitarf[REG_rcx];
bool invalid = 0;
switch (msr) {
case 0xc0000100:
ctx.seg[SEGID_FS].base = value; break;
case 0xc0000101:
ctx.seg[SEGID_GS].base = value; break;
case 0xc0000102:
ctx.swapgs_base = value; break;
default:
invalid = 1; break;
}
if (invalid) {
logfile << "Warning: wrmsr: invalid MSR write (msr ", (void*)(Waddr)msr,
") with value ", (void*)(Waddr)value, " from rip ",
(void*)(Waddr)ctx.commitarf[REG_rip], endl;
// Invalid MSR writes are ignored by Xen by default
// ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
} else {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
} else {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
#else
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
#endif
}
void assist_rdmsr(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
#ifdef PTLSIM_HYPERVISOR
if (ctx.kernel_mode) {
W32 msr = ctx.commitarf[REG_rcx];
W64 rc = 0;
bool invalid = 0;
switch (msr) {
case 0xc0000100:
rc = ctx.seg[SEGID_FS].base; break;
case 0xc0000101:
rc = ctx.seg[SEGID_GS].base; break;
case 0xc0000102:
rc = ctx.swapgs_base; break;
case 0xc0000080:
rc = ctx.efer; break;
default:
invalid = 1; break;
}
if (invalid) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
} else {
ctx.commitarf[REG_rdx] = HI32(rc);
ctx.commitarf[REG_rax] = LO32(rc);
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
} else {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
#else
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
#endif
}
#ifdef PTLSIM_HYPERVISOR
void assist_write_cr0(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
if (!ctx.kernel_mode) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
W64 val = ctx.commitarf[REG_ar1];
if ((val ^ ctx.cr0) & ~(X86_CR0_TS)) {
// Only allowed to change TS flag
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
ctx.cr0 = val;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_write_cr2(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
if (!ctx.kernel_mode) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
W64 val = ctx.commitarf[REG_ar1];
sshinfo.vcpu_info[ctx.vcpuid].arch.cr2 = val;
ctx.cr2 = val;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void switch_page_table(mfn_t mfn);
void assist_write_cr3(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
if (!ctx.kernel_mode) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
ctx.cr3 = ctx.commitarf[REG_ar1] & 0xfffffffffffff000ULL;
ctx.flush_tlb();
switch_page_table(ctx.cr3 >> 12);
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_write_cr4(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
if (!ctx.kernel_mode) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
// (Ignore all writes to CR4 under Xen)
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_write_debug_reg(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
if (!ctx.kernel_mode) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
W64 value = ctx.commitarf[REG_ar1];
W64 regid = ctx.commitarf[REG_ar2];
switch (regid) {
case 0: ctx.dr0 = value; break;
case 1: ctx.dr1 = value; break;
case 2: ctx.dr2 = value; break;
case 3: ctx.dr3 = value; break;
case 4: ctx.dr4 = value; break;
case 5: ctx.dr5 = value; break;
case 6: ctx.dr6 = value; break;
case 7: ctx.dr7 = value; break;
};
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
#else
//
// Userspace PTLsim does not support these:
//
void assist_write_cr0(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
void assist_write_cr2(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
void assist_write_cr3(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
void assist_write_cr4(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
void assist_write_debug_reg(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
#endif
void assist_iret16(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_invalid_opcode);
}
void assist_iret32(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_invalid_opcode);
}
extern bool force_synchronous_streams;
struct IRETStackFrame {
W64 rip, cs, rflags, rsp, ss;
};
static inline ostream& operator <<(ostream& os, const IRETStackFrame& iretctx) {
os << "cs:rip ", (void*)iretctx.cs, ":", (void*)iretctx.rip,
", ss:rsp ", (void*)iretctx.ss, ":", (void*)iretctx.rsp,
", rflags ", (void*)iretctx.rflags;
return os;
}
void assist_iret64(Context& ctx) {
#ifdef PTLSIM_HYPERVISOR
IRETStackFrame frame;
PageFaultErrorCode pfec;
Waddr faultaddr;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
int n = ctx.copy_from_user(&frame, (Waddr)ctx.commitarf[REG_rsp], sizeof(frame), pfec, faultaddr);
if unlikely (n != sizeof(frame)) {
ctx.propagate_x86_exception(EXCEPTION_x86_page_fault, pfec, faultaddr);
return;
}
int exception;
if unlikely (exception = ctx.write_segreg(SEGID_SS, frame.ss)) {
ctx.propagate_x86_exception(exception, frame.ss & 0xfff8);
return;
}
if unlikely (exception = ctx.write_segreg(SEGID_CS, frame.cs)) {
ctx.propagate_x86_exception(exception, frame.cs & 0xfff8);
return;
}
if (logable(5)) {
logfile << "IRET64 from rip ", (void*)(Waddr)ctx.commitarf[REG_rip], ": iretctx @ ",
(void*)(Waddr)ctx.commitarf[REG_rsp], " = ", frame, " (", sim_cycle, " cycles, ",
total_user_insns_committed, " commits)", endl;
}
ctx.commitarf[REG_rip] = frame.rip;
ctx.commitarf[REG_rsp] = frame.rsp;
ctx.internal_eflags = frame.rflags & ~(FLAG_ZAPS|FLAG_CF|FLAG_OF);
ctx.commitarf[REG_flags] = frame.rflags & (FLAG_ZAPS|FLAG_CF|FLAG_OF);
#else
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_invalid_opcode);
#endif
}
static inline W64 x86_merge(W64 rd, W64 ra, int sizeshift) {
union {
W8 w8;
W16 w16;
W32 w32;
W64 w64;
} sizes;
switch (sizeshift) {
case 0: sizes.w64 = rd; sizes.w8 = ra; return sizes.w64;
case 1: sizes.w64 = rd; sizes.w16 = ra; return sizes.w64;
case 2: return LO32(ra);
case 3: return ra;
}
return rd;
}
#ifdef PTLSIM_HYPERVISOR
void assist_ioport_in(Context& ctx) {
// ar1 = 16-bit port number
// ar2 = sizeshift
// rax = output
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
if (!ctx.kernel_mode) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
W64 port = ctx.commitarf[REG_ar1];
W64 sizeshift = ctx.commitarf[REG_ar2];
W64 value = x86_merge(ctx.commitarf[REG_rax], 0xffffffffffffffffULL, sizeshift);
logfile << "assist_ioport_in from rip ", (void*)(Waddr)ctx.commitarf[REG_selfrip], "): ",
"in port 0x", hexstring(port, 16), " (size ", (1<<sizeshift), " bytes) => 0x",
hexstring(value, 64), endl;
ctx.commitarf[REG_rax] = value;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
void assist_ioport_out(Context& ctx) {
// ar1 = 16-bit port number
// ar2 = sizeshift
// rax = value to write
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
if (!ctx.kernel_mode) {
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
return;
}
W64 port = ctx.commitarf[REG_ar1];
W64 sizeshift = ctx.commitarf[REG_ar2];
W64 value = x86_merge(0, ctx.commitarf[REG_rax], sizeshift);
logfile << "assist_ioport_out from rip ", (void*)(Waddr)ctx.commitarf[REG_selfrip], "): ",
"out port 0x", hexstring(port, 16), " (size ", (1<<sizeshift), " bytes) <= 0x",
hexstring(value, 64), endl;
ctx.commitarf[REG_rip] = ctx.commitarf[REG_nextrip];
}
#else
void assist_ioport_in(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
void assist_ioport_out(Context& ctx) {
ctx.commitarf[REG_rip] = ctx.commitarf[REG_selfrip];
ctx.propagate_x86_exception(EXCEPTION_x86_gp_fault);
}
#endif
bool TraceDecoder::decode_complex() {
DecodedOperand rd;
DecodedOperand ra;
switch (op) {
case 0x60: {
// pusha
if (use64) {
// pusha is invalid in 64-bit mode
MakeInvalid();
break;
}
EndOfDecode();
int sizeshift = (opsize_prefix) ? 1 : 2;
int size = (1 << sizeshift);
int offset = 0;
#define PUSH(reg) \
this << TransOp(OP_st, REG_mem, REG_rsp, REG_imm, reg, sizeshift, offset);
offset -= size; PUSH(REG_rax);
offset -= size; PUSH(REG_rcx);
offset -= size; PUSH(REG_rdx);
offset -= size; PUSH(REG_rbx);
offset -= size; PUSH(REG_rsp);
offset -= size; PUSH(REG_rbp);
offset -= size; PUSH(REG_rsi);
offset -= size; PUSH(REG_rdi);
#undef PUSH
this << TransOp(OP_sub, REG_rsp, REG_rsp, REG_imm, REG_zero, sizeshift, -offset);
break;
}
case 0x61: {
// popa
if (use64) {
// popa is invalid in 64-bit mode
MakeInvalid();
break;
}
EndOfDecode();
int sizeshift = (opsize_prefix) ? 1 : 2;
int size = (1 << sizeshift);
int offset = 0;
#define POP(reg) \
this << TransOp(OP_ld, reg, REG_rsp, REG_imm, REG_zero, sizeshift, offset);
POP(REG_rdi); offset += size;
POP(REG_rsi); offset += size;
POP(REG_rbp); offset += size;
/* skip rsp */ offset += size;
POP(REG_rbx); offset += size;
POP(REG_rdx); offset += size;
POP(REG_rcx); offset += size;
POP(REG_rax); offset += size;
#undef POP
this << TransOp(OP_add, REG_rsp, REG_rsp, REG_imm, REG_zero, sizeshift, offset);
break;
}
case 0x62: {
// bound [not used by gcc]
MakeInvalid();
break;
}
case 0x64 ... 0x67: {
// invalid (prefixes)
MakeInvalid();
break;
}
case 0x6c ... 0x6f: {
// insb/insw/outsb/outsw: not supported
MakeInvalid();
break;
}
case 0x86 ... 0x87: {
// xchg
DECODE(eform, rd, bit(op, 0) ? v_mode : b_mode);
DECODE(gform, ra, bit(op, 0) ? v_mode : b_mode);
EndOfDecode();
/*
xchg [mem],ra
becomes:
mov t7 = ra
ld.acq t6 = [mem]
# create artificial data dependency on t6 -> t7 (always let t7 pass through)
sel.c t7 = t7,t6,(zero)
st.rel [mem] = t7
mov ra,t6
Notice that the st.rel is artificially forced to depend on the ld.acq
so as to guarantee we won't try to unlock before we lock should these
uops be reordered.
ld.acq and st.rel are always used for memory operands, regardless of LOCK prefix
*/
int sizeshift = reginfo[ra.reg.reg].sizeshift;
bool rahigh = reginfo[ra.reg.reg].hibyte;
int rareg = arch_pseudo_reg_to_arch_reg[ra.reg.reg];
if (rd.type == OPTYPE_REG) {
int rdreg = arch_pseudo_reg_to_arch_reg[rd.reg.reg];
bool rdhigh = reginfo[rd.reg.reg].hibyte;
this << TransOp(OP_mov, REG_temp0, REG_zero, rdreg, REG_zero, 3); // save old rdreg
bool moveonly = (!rdhigh && !rahigh);
int maskctl1 =
(rdhigh && !rahigh) ? MaskControlInfo(56, 8, 56) : // insert high byte
(!rdhigh && rahigh) ? MaskControlInfo(0, 8, 8) : // extract high byte
(rdhigh && rahigh) ? MaskControlInfo(56, 8, 0) : // move between high bytes
MaskControlInfo(0, 64, 0); // straight move (but cannot synthesize from mask uop)
int maskctl2 =
(rdhigh && !rahigh) ? MaskControlInfo(0, 8, 8) : // extract high byte
(!rdhigh && rahigh) ? MaskControlInfo(56, 8, 56) : // insert high byte
(rdhigh && rahigh) ? MaskControlInfo(56, 8, 0) : // move between high bytes
MaskControlInfo(0, 64, 0); // straight move (but cannot synthesize from mask uop)
if (moveonly) {
this << TransOp(OP_mov, rdreg, rdreg, rareg, REG_zero, sizeshift);
this << TransOp(OP_mov, rareg, rareg, REG_temp0, REG_zero, sizeshift);
} else {
this << TransOp(OP_maskb, rdreg, rdreg, rareg, REG_imm, 3, 0, maskctl1);
this << TransOp(OP_maskb, rareg, rareg, REG_temp0, REG_imm, 3, 0, maskctl2);
}
} else {
// xchg [mem],reg is always locked:
prefixes |= PFX_LOCK;
if (memory_fence_if_locked(0)) break;
if (rahigh)
this << TransOp(OP_maskb, REG_temp7, REG_zero, rareg, REG_imm, 3, 0, MaskControlInfo(0, 8, 8));
else this << TransOp(OP_mov, REG_temp7, REG_zero, rareg, REG_zero, 3);
//
// ld t6 = [mem]
//
int destreg = arch_pseudo_reg_to_arch_reg[ra.reg.reg];
int mergewith = arch_pseudo_reg_to_arch_reg[ra.reg.reg];
if (sizeshift >= 2) {
// zero extend 32-bit to 64-bit or just load as 64-bit:
operand_load(REG_temp6, rd);
} else {
// need to merge 8-bit or 16-bit data:
operand_load(REG_temp0, rd);
if (reginfo[rd.reg.reg].hibyte)
this << TransOp(OP_maskb, REG_temp6, destreg, REG_temp0, REG_imm, 3, 0, MaskControlInfo(56, 8, 56));
else this << TransOp(OP_mov, REG_temp6, destreg, REG_temp0, REG_zero, sizeshift);
}
//
// Create artificial data dependency:
//
// This is not on the critical path since the ld result is available
// immediately in an out of order machine.
//
// sel.c t7 = t7,t6,(zero) # ra always selected (passthrough)
//
TransOp dummyop(OP_sel, REG_temp7, REG_temp7, REG_temp6, REG_zero, 3);
dummyop.cond = COND_c;
this << dummyop;
//
// st [mem] = t0
//
result_store(REG_temp7, REG_temp0, rd);
//
// mov ra = zero,t6
// Always move the full size: the temporary was already merged above
//
this << TransOp(OP_mov, destreg, REG_zero, REG_temp6, REG_zero, 3);
if (memory_fence_if_locked(1)) break;
}
break;
}
case 0x8c: {
// mov Ev,segreg
DECODE(eform, rd, w_mode);
DECODE(gform, ra, w_mode);
EndOfDecode();
// Same encoding as order in SEGID_xxx: ES CS SS DS FS GS - - (last two are invalid)
if (modrm.reg >= 6) MakeInvalid();
int rdreg = (rd.type == OPTYPE_MEM) ? REG_temp0 : arch_pseudo_reg_to_arch_reg[rd.reg.reg];