-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathptlmon.cpp
More file actions
2021 lines (1625 loc) · 62.8 KB
/
ptlmon.cpp
File metadata and controls
2021 lines (1625 loc) · 62.8 KB
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
// PTLxen monitor and control program running in dom0
//
// Copyright 2005-2008 Matt T. Yourst <yourst@yourst.com>
//
#include <globals.h>
#include <superstl.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/epoll.h>
#include <elf.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
typedef W16 domid_t;
asmlinkage {
#include <xenctrl.h>
#include <xs.h>
};
#include <xen-types.h>
#include <xen/linux/privcmd.h>
#include <xen/linux/evtchn.h>
#include <xen/platform.h>
#define PTLSIM_IN_PTLMON
#include <ptlxen.h>
#include <xen/io/console.h>
#undef DEBUG
int domain = -1;
bool log_ptlsim_boot = 0;
static inline W64 do_syscall_64bit(W64 syscallid, W64 arg1, W64 arg2, W64 arg3, W64 arg4, W64 arg5, W64 arg6) {
W64 rc;
asm volatile ("movq %5,%%r10\n"
"movq %6,%%r8\n"
"movq %7,%%r9\n"
"syscall\n"
: "=a" (rc)
: "0" (syscallid),"D" ((W64)(arg1)),"S" ((W64)(arg2)),
"d" ((W64)(arg3)), "g" ((W64)(arg4)), "g" ((W64)(arg5)),
"g" ((W64)(arg6))
: "r11","rcx","memory" ,"r8", "r10", "r9" );
return rc;
}
static inline W32 do_syscall_32bit(W32 sysid, W32 arg1, W32 arg2, W32 arg3, W32 arg4, W32 arg5, W32 arg6) {
W32 rc;
asm volatile ("push %%rbp ; movl %[arg6],%%ebp ; int $0x80 ; pop %%rbp" : "=a" (rc) :
"a" (sysid), "b" (arg1), "c" (arg2), "d" (arg3),
"S" (arg4), "D" (arg5), [arg6] "r" (arg6));
return rc;
}
static int do_privcmd(int xc_handle, unsigned int cmd, unsigned long data) {
return ioctl(xc_handle, cmd, data);
}
int do_xen_hypercall(int xc_handle, privcmd_hypercall_t *hypercall) {
return do_privcmd(xc_handle, IOCTL_PRIVCMD_HYPERCALL, (unsigned long)hypercall);
}
static int do_evtchn_op(int xc_handle, int cmd, void *arg, size_t arg_size) {
int ret = -1;
privcmd_hypercall_t hypercall;
hypercall.op = __HYPERVISOR_event_channel_op;
hypercall.arg[0] = cmd;
hypercall.arg[1] = (unsigned long)arg;
if ( mlock(arg, arg_size) != 0 ) {
goto out;
}
if ((ret = do_xen_hypercall(xc_handle, &hypercall)) < 0) {
// cerr << "do_evtchn_op: HYPERVISOR_event_channel_op failed: ", ret, ", errno ", errno, " (", strerror(errno), ")", endl;
}
munlock(arg, arg_size);
out:
return ret;
}
static int xc_alloc_unbound(int xc, int domfrom, int domto) {
evtchn_alloc_unbound_t req;
req.dom = domto;
req.remote_dom = domfrom;
int rc = do_evtchn_op(xc, EVTCHNOP_alloc_unbound, &req, sizeof(req));
return (rc < 0) ? rc : req.port;
}
static int xc_bind_interdomain(int xc, int remotedom, int remoteport) {
evtchn_bind_interdomain_t req;
req.remote_dom = remotedom;
req.remote_port = remoteport;
int rc = do_evtchn_op(xc, EVTCHNOP_bind_interdomain, &req, sizeof(req));
return (rc < 0) ? rc : req.local_port;
}
static int xc_send(int xc, int localport) {
evtchn_send_t req;
req.port = localport;
int rc = do_evtchn_op(xc, EVTCHNOP_send, &req, sizeof(req));
return rc;
}
static int xc_evtchn_unmask(int xc, int localport) {
evtchn_unmask_t req;
req.port = localport;
int rc = do_evtchn_op(xc, EVTCHNOP_unmask, &req, sizeof(req));
return rc;
}
static int do_msr_op(int xc, int cmd, int cpu, W32 index, W64& value) {
int rc = -1;
privcmd_hypercall_t hypercall;
xen_platform_op_t op;
op.cmd = cmd;
op.interface_version = XENPF_INTERFACE_VERSION;
op.u.msr.cpu = cpu;
op.u.msr.index = index;
op.u.msr.value = value;
hypercall.op = __HYPERVISOR_platform_op;
hypercall.arg[0] = W64(&op);
if (mlock(&op, sizeof(op))) goto out;
rc = do_xen_hypercall(xc, &hypercall);
#if 0
cerr << "do_msr_op(", cmd, ", ", cpu, ", ", (void*)index, ", value ", (void*)value, "): "
"hypercall rc ", rc, ", msr rc ", op.u.msr.rc, ", new value ", (void*)op.u.msr.value, endl, flush;
#endif
value = op.u.msr.value;
if (!rc) rc = op.u.msr.rc;
munlock(&op, sizeof(op));
out:
return rc;
}
static W64 rdmsr(int xc, int cpu, W32 index) {
W64 value = 0;
int rc = do_msr_op(xc, XENPF_rdmsr, cpu, index, value);
return value;
}
static int wrmsr(int xc, int cpu, W32 index, W64 value) {
int rc = do_msr_op(xc, XENPF_wrmsr, cpu, index, value);
return rc;
}
#define barrier() asm volatile("": : :"memory")
#if defined(__i386__)
#define L1_PAGETABLE_SHIFT_PAE 12
#define L2_PAGETABLE_SHIFT_PAE 21
#define L3_PAGETABLE_SHIFT_PAE 30
#define L1_PAGETABLE_SHIFT 12
#define L2_PAGETABLE_SHIFT 22
#define L0_PAGETABLE_MASK_PAE 0x0000000ffffff000ULL
#define L1_PAGETABLE_MASK_PAE 0x1ffULL
#define L2_PAGETABLE_MASK_PAE 0x1ffULL
#define L3_PAGETABLE_MASK_PAE 0x3ULL
#define L0_PAGETABLE_MASK 0xfffff000ULL
#define L1_PAGETABLE_MASK 0x3ffULL
#define L2_PAGETABLE_MASK 0x3ffULL
#elif defined(__x86_64__)
#define L1_PAGETABLE_SHIFT_PAE 12
#define L2_PAGETABLE_SHIFT_PAE 21
#define L3_PAGETABLE_SHIFT_PAE 30
#define L4_PAGETABLE_SHIFT_PAE 39
#define L1_PAGETABLE_SHIFT L1_PAGETABLE_SHIFT_PAE
#define L2_PAGETABLE_SHIFT L2_PAGETABLE_SHIFT_PAE
#define L0_PAGETABLE_MASK_PAE 0x000000fffffff000ULL
#define L1_PAGETABLE_MASK_PAE 0x1ffULL
#define L2_PAGETABLE_MASK_PAE 0x1ffULL
#define L3_PAGETABLE_MASK_PAE 0x1ffULL
#define L4_PAGETABLE_MASK_PAE 0x1ffULL
#define L0_PAGETABLE_MASK L0_PAGETABLE_MASK_PAE
#define L1_PAGETABLE_MASK L1_PAGETABLE_MASK_PAE
#define L2_PAGETABLE_MASK L2_PAGETABLE_MASK_PAE
#endif
#define mmap_invalid(addr) (((W64)(addr) & 0xfffffffffffff000) == 0xfffffffffffff000)
#define mmap_valid(addr) (!mmap_invalid(addr))
#define PTL_PAGE_POOL_BASE 0x70000000LL
void* sys_mmap(void* start, W64 length, int prot, int flags, int fd, off_t offset) {
return mmap(start, length, prot, flags, fd, offset);
}
int sys_munmap(void* start, W64 length) {
return munmap((void*)start, length);
}
int sys_mprotect(void* start, W64 length, int prot) {
return mprotect(start, length, prot);
}
int sys_madvise(void* start, W64 length, int action) {
return madvise(start, length, action);
}
void* ptl_alloc_private_pages(Waddr bytecount, int prot, Waddr base = 0, int extraflags = 0) {
int flags = MAP_ANONYMOUS|MAP_NORESERVE | (base ? MAP_FIXED : 0);
//flags |= (inside_ptlsim) ? MAP_SHARED : MAP_PRIVATE;
flags |= MAP_PRIVATE;
flags |= extraflags;
if (base == 0) base = PTL_PAGE_POOL_BASE;
void* addr = sys_mmap((void*)base, ceil(bytecount, PAGE_SIZE), prot, flags, 0, 0);
return addr;
}
void* ptl_alloc_private_page() {
return ptl_alloc_private_pages(4096, PROT_READ|PROT_WRITE);
}
void* ptl_alloc_private_32bit_pages(Waddr bytecount, int prot, Waddr base) {
#ifdef __x86_64__
int flags = MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE | (base ? MAP_FIXED : MAP_32BIT);
#else
int flags = MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE | (base ? MAP_FIXED : 0);
#endif
return sys_mmap((void*)base, ceil(bytecount, PAGE_SIZE), prot, flags, 0, 0);
}
bool ptl_lock_private_pages(void* addr, Waddr bytecount) {
return (mlock(addr, bytecount) == 0);
}
bool ptl_unlock_private_pages(void* addr, Waddr bytecount) {
return (munlock(addr, bytecount) == 0);
}
void ptl_free_private_pages(void* addr, Waddr bytecount) {
sys_munmap(addr, bytecount);
}
void ptl_free_private_page(void* addr) {
ptl_free_private_pages(addr, 4096);
}
void ptl_zero_private_pages(void* addr, Waddr bytecount) {
sys_madvise((void*)floor((Waddr)addr, PAGE_SIZE), bytecount, MADV_DONTNEED);
}
// In dom0, PTLmon runs in userspace so it can't mmap at the real address.
#define PTLSIM_PSEUDO_VIRT_BASE 0x10000000
template <typename T>
static inline T ptlcore_ptr_to_ptlmon_ptr(T p) {
if (!p) return p;
return (T)((((Waddr)p) - PTLSIM_VIRT_BASE) + PTLSIM_PSEUDO_VIRT_BASE);
}
template <typename T>
static inline T ptlmon_ptr_to_ptlcore_ptr(T p) {
if (!p) return p;
return (T)((((Waddr)p) - PTLSIM_PSEUDO_VIRT_BASE) + PTLSIM_VIRT_BASE);
}
// This is from ptlxen.bin.o:
extern byte _binary_ptlxen_bin_start;
extern byte _binary_ptlxen_bin_end;
struct PendingRequest: public selflistlink {
W64 uuid;
int fd;
char* data;
};
struct PendingRequestLinkManager: public ObjectLinkManager<PendingRequest> {
static inline W64& keyof(PendingRequest* obj) {
return obj->uuid;
}
};
queue<PendingRequest, PendingRequestLinkManager> requestq;
typedef SelfHashtable<W64, PendingRequest, 16, PendingRequestLinkManager> PendingRequestTable;
PendingRequestTable pendingreqs;
struct XenController;
static inline bool thunk_ptr_valid(Waddr w) {
return (inrange(w, (Waddr)PTLSIM_XFER_PAGES_VIRT_BASE, PTLSIM_XFER_PAGES_VIRT_BASE+(PTLSIM_XFER_PAGES_SIZE-1)));
}
static ostream& operator <<(ostream& os, const page_type_t& pagetype) {
static const char* page_type_names[] = {"none", "L1", "L2", "L3", "L4", "GDT", "LDT", "write"};
const char* page_type_name =
(pagetype.out.type == PAGE_TYPE_INVALID_MFN) ? "inv" :
(pagetype.out.type == PAGE_TYPE_INACCESSIBLE) ? "inacc" :
(pagetype.out.type < lengthof(page_type_names)) ? page_type_names[pagetype.out.type] :
"???";
os << padstring(page_type_name, -5), " ", (pagetype.out.pinned ? "pin" : " "), " ",
intstring(pagetype.out.total_count, 5), " total, ", intstring(pagetype.out.type_count, 5), " by type";
return os;
}
struct XenController {
int xc;
int domain;
xc_domaininfo_t info;
shared_info* shinfo;
Level4PTE* toplevel_page_table;
byte* image;
int ptl_page_count;
int ptl_pagedir_mfn_count;
int shared_map_page_count;
mfn_t* ptl_mfns;
shared_info* shadow_shinfo;
PTLsimMonitorInfo* bootinfo;
int vcpu_count;
int vcpu_online_count;
int evtchnfd;
int ptlsim_hostcall_port;
int ptlsim_upcall_port;
int ptlsim_breakout_port;
W64 total_machine_pages;
W64 xen_hypervisor_start_va;
int page_table_levels;
Context* ctx;
W64 phys_cpu_affinity;
int cpu_type;
XenController() { reset(); }
void reset() {
xc = -1; domain = -1; ptlsim_hostcall_port = -1;
shinfo = null;
bootinfo = null;
ctx = null;
next_upcall_uuid = 1;
cpu_type = CPU_TYPE_UNKNOWN;
}
void* map_pages(mfn_t* mfns, size_t count, int prot = PROT_READ, void* base = null, int flags = 0) {
if (base) flags |= MAP_FIXED;
void* addr = mmap(base, count * PAGE_SIZE, prot, flags | MAP_SHARED, xc, 0);
if (mmap_invalid(addr)) return null;
privcmd_mmapbatch_t ioctlx;
ioctlx.num = count;
ioctlx.dom = domain;
ioctlx.addr = (unsigned long)addr;
ioctlx.arr = mfns;
int rc = ioctl(xc, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx);
if (rc < 0) {
cout << "ERROR: XenController::map_pages() failed: errno = ", strerror(errno), endl, flush;
munmap(addr, count * PAGE_SIZE);
return null;
}
return addr;
}
byte* map_page(mfn_t mfn, int prot = PROT_READ, void* addr = null) {
mfn_t mfns[1];
mfns[0] = mfn;
return (byte*)map_pages(mfns, 1, prot, addr);
}
void unmap_pages(void* virt, int count) {
int rc = munmap(virt, count * PAGE_SIZE);
if (rc < 0) {
cout << "unmap_pages(", virt, ", ", count, "): rc = ", rc, ", errno ", strerror(errno), endl, flush;
assert(false);
}
}
void unmap_page(void* virt) {
unmap_pages(virt, 1);
}
pfn_t ptl_virt_to_pfn(void* virt) {
Waddr offset = (Waddr)virt - (Waddr)image;
if (!inrange(offset, (Waddr)0, (Waddr)((ptl_page_count*PAGE_SIZE)-1))) {
cerr << "ptl_virt_to_pfn(", virt, "): in page ", (offset / 4096), ": not in range 0 to ", ptl_page_count, " pages", endl, flush;
assert(false);
}
return offset >> 12;
}
mfn_t ptl_virt_to_mfn(void* virt) {
return ptl_mfns[ptl_virt_to_pfn(virt)];
}
pfn_t ptl_level4_pfn, ptl_level3_pfn, phys_level3_pfn, ptl_level2_pfn, ptl_level1_pfn;
mfn_t ptl_level4_mfn, ptl_level3_mfn, phys_level3_mfn, ptl_level2_mfn;
byte* per_vcpu_stack_base;
shared_info* ptlsim_entry_shinfo;
char* log_buffer_base;
bool alloc_ptlsim_pages(mfn_t* mfns, int ptl_page_count) {
static const int MAX_ATTEMPTS = 4;
int pages_so_far = 0;
W64 sleep_time_nsec = 50 * (1000000);
if (log_ptlsim_boot) cerr << "Allocate ", ptl_page_count, " pages (", ((ptl_page_count * 4096) / 1024), " KB) for PTLsim", endl, flush;
foreach (i, MAX_ATTEMPTS) {
xen_memory_reservation_t res;
res.extent_start.p = mfns + pages_so_far;
res.nr_extents = ptl_page_count - pages_so_far;
res.extent_order = 0;
res.address_bits = 0;
res.domid = domain;
assert(pages_so_far <= ptl_page_count);
int rc = xc_memory_op(xc, XENMEM_increase_reservation, &res);
// foreach (i, rc) { cerr << " page ", intstring(i, 8), " -> mfn ", intstring(mfns[i], 8), endl; }
if (rc < 0) {
cerr << "PTLsim error: cannot increase domain reservation on attempt ", i, ": rc ", rc, endl, flush;
return false;
}
pages_so_far += rc;
if (pages_so_far == ptl_page_count) {
return true;
}
if (log_ptlsim_boot) cerr << " alloc_ptlsim_pages (attempt ", i, "): allocated ", rc, " pages; total so far: ", pages_so_far, " out of ", ptl_page_count, " pages", endl, flush;
W64 current_target = 0;
{
istream is("/proc/xen/balloon");
if (!is) {
cerr << "PTLsim error: alloc_ptlsim_pages: cannot open /proc/xen/balloon", endl, flush;
return false;
}
stringbuf line;
dynarray<char*> tok;
for (;;) {
line.reset();
is >> line;
if (!is) break;
tok.tokenize(line, " :");
if ((tok.length >= 4) && strequal(tok[0], "Current") && strequal(tok[1], "allocation") && strequal(tok[3], "kB")) {
current_target = atoll(tok[2]) * 1024;
break;
}
}
}
if (log_ptlsim_boot) cerr << " Current requested target: ", current_target, " bytes", endl, flush;
if (!current_target) {
cerr << "PTLsim error: cannot read current target reservation from /proc/xen/balloon! Is this the wrong Xen version?", endl, flush;
return false;
}
W64 pages_still_needed = ptl_page_count - pages_so_far;
W64 bytes_still_needed = pages_still_needed * 4096;
if (log_ptlsim_boot) cerr << " Still need ", pages_still_needed, " pages (", bytes_still_needed, " bytes)", endl, flush;
W64 new_target = current_target - (bytes_still_needed + 1048576);
if (log_ptlsim_boot) cerr << " New target: ", new_target, endl;
stringbuf sb;
sb << new_target, endl;
{
ostream os("/proc/xen/balloon");
os << sb;
os.flush();
if (!os.ok()) {
cerr << "PTLsim error: cannot write new target reservation (", new_target, ") to /proc/xen/balloon! Is there a permissions problem?", endl, flush;
return false;
}
}
if (log_ptlsim_boot) cerr << "Sleeping for ", (sleep_time_nsec / 1000000), " milliseconds while pages are freed...", endl, flush;
sys_nanosleep(sleep_time_nsec);
sleep_time_nsec *= 2; // exponential backoff
}
cerr << "PTLsim error: cannot allocate ", ptl_page_count, " pages (", ((ptl_page_count * 4096) / 1024), " KB) for PTLsim", endl;
cerr << " Tried ", MAX_ATTEMPTS, " attempts but only got ", pages_so_far, " pages (", ((pages_so_far * 4096) / 1024), " KB)", endl;
cerr << " Make sure no other domains are running, try to free up some memory in dom0 and try again.", endl;
cerr << endl;
cerr << flush;
return false;
}
void prep_initial_ptlsim_context(int vcpuid, Context& ptlctx) {
setzero(ptlctx);
ptlctx.cr3 = (ptl_virt_to_mfn(toplevel_page_table) << log2(PAGE_SIZE));
ptlctx.kernel_ptbase_mfn = ptlctx.cr3 >> 12;
ptlctx.user_ptbase_mfn = ptlctx.cr3 >> 12;
ptlctx.kernel_mode = 1;
ptlctx.seg[SEGID_CS].selector = FLAT_KERNEL_CS;
ptlctx.seg[SEGID_DS].selector = FLAT_KERNEL_DS;
ptlctx.seg[SEGID_SS].selector = FLAT_KERNEL_SS;
ptlctx.seg[SEGID_ES].selector = FLAT_KERNEL_DS;
ptlctx.seg[SEGID_FS].selector = 0;
ptlctx.seg[SEGID_GS].selector = 0;
ptlctx.commitarf[REG_flags] = 0; // interrupts initially off
ptlctx.saved_upcall_mask = 1;
Waddr per_vcpu_sp = ((Waddr)bootinfo->per_vcpu_stack_base) + (vcpuid * 4096) + 4096;
ptlctx.commitarf[REG_rsp] = (vcpuid > 0) ? per_vcpu_sp : (Waddr)bootinfo->stack_top;
ptlctx.commitarf[REG_rip] = PTLSIM_ENTRYPOINT_RIP;
// start info in %rdi (arg[0]):
ptlctx.commitarf[REG_rdi] = (Waddr)ptlmon_ptr_to_ptlcore_ptr(bootinfo);
// vcpuid is passed in rsi so we know whether or not we're the primary VCPU:
ptlctx.commitarf[REG_rsi] = vcpuid;
}
bool inject_ptlsim(int reserved_mbytes) {
bool DEBUG = log_ptlsim_boot;
static const int stacksize = 2048*1024;
ptl_page_count = (reserved_mbytes * 1024*1024) / 4096;
xen_domctl_t op;
op.cmd = XEN_DOMCTL_getdomaininfo;
op.domain = domain;
int rc = xc_domctl(xc, &op);
xen_domctl_getdomaininfo_t info;
memcpy(&info, &op.u.getdomaininfo, sizeof(info));
if (rc) {
cerr << endl;
cerr << "PTLsim error: cannot access domain ", domain, " (error ", rc, ")", endl,
"Please make sure the specified domain is running.", endl, endl;
return false;
}
W64 new_max_pages = info.tot_pages + ptl_page_count;
if (DEBUG) {
cerr << "Inject PTLsim into domain ", domain, ":", endl;
cerr << " Current allocation: ", intstring(info.tot_pages, 8), " pages", endl;
cerr << " Max allocation: ", intstring(info.max_pages, 8), " pages", endl;
cerr << " PTLsim reserved: ", intstring(ptl_page_count, 8), " pages", endl;
cerr << " Increase to: ", intstring(new_max_pages, 8), " pages", endl;
}
op.cmd = XEN_DOMCTL_max_mem;
op.domain = domain;
op.u.max_mem.max_memkb = (new_max_pages * 4096) / 1024;
rc = xc_domctl(xc, &op);
if (rc) {
cerr << "PTLsim error: cannot increase domain limit to ", new_max_pages, " pages (rc ", rc, ")", endl, flush;
return false;
}
ptl_mfns = new mfn_t[ptl_page_count];
memset(ptl_mfns, 0, ptl_page_count * sizeof(mfn_t));
rc = alloc_ptlsim_pages(ptl_mfns, ptl_page_count);
if (!rc) {
cerr << "PTLsim error: cannot increase domain reservation to ", new_max_pages, " pages (rc ", rc, ")", endl, flush;
return false;
}
image = (byte*)map_pages(ptl_mfns, ptl_page_count, PROT_READ|PROT_WRITE, (void*)PTLSIM_PSEUDO_VIRT_BASE, MAP_FIXED);
assert(image == (byte*)PTLSIM_PSEUDO_VIRT_BASE);
if (!image) {
cerr << "PTLsim error: cannot map ", ptl_page_count, " allocated pages!", endl, flush;
return false;
}
memset(image, 0, ptl_page_count * PAGE_SIZE);
int next_free_page_pfn = ptl_page_count;
#define alloc_page(T, virt, pfn, mfn) next_free_page_pfn--; mfn = ptl_mfns[next_free_page_pfn]; pfn = next_free_page_pfn; T* virt = (T*)(image + (next_free_page_pfn * PAGE_SIZE))
#define alloc_pages(T, virt, pfn, n) next_free_page_pfn -= (n); pfn = next_free_page_pfn; T* virt = (T*)(image + (next_free_page_pfn * PAGE_SIZE))
#define virt_to_pfn(virt) ((((byte*)(virt)) - image) / PAGE_SIZE)
int ptl_pagedir_mfn_count = (ptl_page_count + PTES_PER_PAGE-1) / PTES_PER_PAGE;
bootinfo = (PTLsimMonitorInfo*)(image + (PTLSIM_BOOT_PAGE_PFN * PAGE_SIZE));
mfn_t bootpage_mfn = ptl_mfns[PTLSIM_BOOT_PAGE_PFN];
bootinfo->mfn_count = ptl_page_count;
bootinfo->ptl_pagedir_mfn_count = ptl_pagedir_mfn_count;
bootinfo->shared_info_mfn = info.shared_info_frame;
bootinfo->total_machine_pages = total_machine_pages;
bootinfo->phys_cpu_affinity = phys_cpu_affinity;
alloc_pages(Level1PTE, ptl_level1, ptl_level1_pfn, ptl_pagedir_mfn_count);
memset(ptl_level1, 0, ptl_pagedir_mfn_count * PAGE_SIZE);
ptl_level1_pfn = virt_to_pfn(ptl_level1);
bootinfo->ptl_pagedir = (Level1PTE*)(PTLSIM_VIRT_BASE + ((byte*)ptl_level1 - image));
foreach (i, ptl_page_count) {
Level1PTE& pte = ptl_level1[i];
pte.p = 1;
pte.rw = 1; // writable (unless reset below for pinned page tables)
pte.us = 1; // supervisor only
pte.a = 1;
pte.mfn = ptl_mfns[i];
}
//
// Map shared info page in appropriate place
//
ptl_level1[PTLSIM_SHINFO_PAGE_PFN].mfn = bootinfo->shared_info_mfn;
bootinfo->shared_info = (shared_info*)PTLSIM_SHINFO_PAGE_VIRT_BASE;
//
// Allocate L2 PTE page
//
alloc_page(Level2PTE, ptl_level2, ptl_level2_pfn, ptl_level2_mfn);
memset(ptl_level2, 0, PAGE_SIZE);
foreach (i, ptl_pagedir_mfn_count) {
Level2PTE& pte = ptl_level2[i];
pte.p = 1;
pte.rw = 1; // sub-pages are writable unless overridden
pte.us = 1; // both user and supervisor
pte.a = 1;
pte.mfn = ptl_mfns[ptl_level1_pfn + i];
}
bootinfo->ptl_pagedir_map = (Level2PTE*)(PTLSIM_VIRT_BASE + ((byte*)ptl_level2 - image));
bootinfo->ptl_pagedir_map_mfn = ptl_level2_mfn;
//
// Allocate PTLsim L3 page
//
alloc_page(Level3PTE, ptl_level3, ptl_level3_pfn, ptl_level3_mfn);
memset(ptl_level3, 0, PAGE_SIZE);
ptl_level3->p = 1;
ptl_level3->rw = 1; // sub-pages are writable unless overridden
ptl_level3->us = 1;
ptl_level3->a = 1;
ptl_level3->mfn = ptl_level2_mfn;
bootinfo->ptl_level3_map = (Level3PTE*)(PTLSIM_VIRT_BASE + ((byte*)ptl_level3 - image));
bootinfo->ptl_level3_mfn = ptl_level3_mfn;
//
// Finish toplevel template
//
alloc_page(Level4PTE, ptl_level4, ptl_level4_pfn, ptl_level4_mfn);
memset(ptl_level4, 0, PAGE_SIZE);
{
Level4PTE& pte = ptl_level4[bits(PTLSIM_VIRT_BASE, (12+9+9+9), 9)];
pte.p = 1;
pte.rw = 1; // sub-pages are writable unless overridden
pte.us = 1; // both user and supervisor
pte.a = 1;
pte.mfn = ptl_level3_mfn;
}
//
// Finish up bootpage
//
bootinfo->toplevel_page_table = (Level4PTE*)(PTLSIM_VIRT_BASE + ((byte*)ptl_level4 - image));
bootinfo->toplevel_page_table_mfn = ptl_level4_mfn;
toplevel_page_table = ptlcore_ptr_to_ptlmon_ptr(bootinfo->toplevel_page_table);
if (DEBUG) cerr << "toplevel_page_table = ", toplevel_page_table, " (mfn ", ptl_virt_to_mfn(toplevel_page_table), ")", endl, flush;
if (DEBUG) cerr << "toplevel_page_table_mfn = ", bootinfo->toplevel_page_table_mfn, endl, flush;
if (DEBUG) cerr << "ptl_pagedir_map_mfn = ", bootinfo->ptl_pagedir_map_mfn, endl, flush;
assert(bootinfo->toplevel_page_table_mfn == ptl_virt_to_mfn(toplevel_page_table));
size_t bytes = &_binary_ptlxen_bin_end - &_binary_ptlxen_bin_start;
const byte* data = &_binary_ptlxen_bin_start;
const Elf64_Ehdr& ehdr = *(const Elf64_Ehdr*)data;
const Elf64_Phdr* phdr = (const Elf64_Phdr*)(((const byte*)&ehdr) + ehdr.e_phoff);
if (DEBUG) cerr << "Injecting PTLsim into domain ", domain, ":", endl;
if (DEBUG) cerr << " PTLcore is ", bytes, " bytes @ virt addr ", image, endl, flush;
Waddr real_code_start_offset = (ehdr.e_entry - PTLSIM_VIRT_BASE);
shared_map_page_count = real_code_start_offset / PAGE_SIZE;
int bytes_remaining = bytes - real_code_start_offset;
if (DEBUG) cerr << " Real code starts at offset ", real_code_start_offset, endl;
if (DEBUG) cerr << " Bytes to copy: ", bytes_remaining, endl, flush;
memcpy(image + real_code_start_offset, data + real_code_start_offset, bytes_remaining);
//
// Prepare the heap
//
byte* end_of_image = image + ceil(phdr[0].p_memsz, PAGE_SIZE);
Waddr bss_size = phdr[0].p_memsz - phdr[0].p_filesz;
if (DEBUG) cerr << " PTLsim has ", next_free_page_pfn, " pages left out of ", ptl_page_count, " pages allocated", endl;
if (DEBUG) cerr << " PTLxen file size ", phdr[0].p_filesz, ", virtual size ", phdr[0].p_memsz, ", end_of_image ", end_of_image, endl;
if (DEBUG) cerr << " Zero ", bss_size, " bss bytes starting at ", (image + phdr[0].p_filesz), endl, flush;
memset(image + phdr[0].p_filesz, 0, bss_size);
//
// Set up primary stack
//
byte* sp = image + (next_free_page_pfn * PAGE_SIZE);
bootinfo->stack_top = ptlmon_ptr_to_ptlcore_ptr(sp);
bootinfo->stack_size = stacksize;
next_free_page_pfn -= (stacksize / PAGE_SIZE);
if (DEBUG) cerr << " Setting up ", stacksize, "-byte stack starting at ", ptlmon_ptr_to_ptlcore_ptr(sp), endl, flush;
//
// Allocate up per-VCPU stacks
//
pfn_t per_vcpu_stack_base_pfn;
alloc_pages(byte, per_vcpu_stack_base, per_vcpu_stack_base_pfn, MAX_CONTEXTS);
bootinfo->per_vcpu_stack_base = ptlmon_ptr_to_ptlcore_ptr(per_vcpu_stack_base);
if (DEBUG) cerr << " Setting up ", vcpu_count, " stacks of ", 4096, " bytes each starting at ", bootinfo->per_vcpu_stack_base, endl, flush;
//
// Allocate virtual shinfo redirect page (for event recording and replay)
//
shadow_shinfo = ptlcore_ptr_to_ptlmon_ptr((shared_info*)PTLSIM_SHADOW_SHINFO_PAGE_VIRT_BASE);
// shadow_shinfo will be filled in by Xen during the context swap
if (DEBUG) cerr << " Shadow shared info page at ", shadow_shinfo, endl, flush;
//
// Allocate VCPU contexts
//
ctx = ptlcore_ptr_to_ptlmon_ptr((Context*)PTLSIM_CTX_PAGE_VIRT_BASE);
if (DEBUG) cerr << " Context array starts at ", ptlmon_ptr_to_ptlcore_ptr(ctx), " (", sizeof(Context), " bytes each)", endl, flush;
bootinfo->heap_start = ptlmon_ptr_to_ptlcore_ptr(end_of_image);
bootinfo->heap_end = ptlmon_ptr_to_ptlcore_ptr(image + ((next_free_page_pfn-1) * PAGE_SIZE));
bootinfo->avail_mfn_count = (next_free_page_pfn-1);
if (DEBUG) {
cerr << "PTLsim mapped at ", image, ":", endl;
cerr << "Page counts:", endl;
cerr << " Total pages: ", intstring(bootinfo->mfn_count, 8), endl;
cerr << " Remaining pages: ", intstring(bootinfo->avail_mfn_count, 8), endl;
cerr << "Addresses:", endl;
cerr << " Base: ", (void*)image, endl;
cerr << flush;
}
if (DEBUG) cerr << " Heap start ", bootinfo->heap_start, " to heap end ", bootinfo->heap_end, " (", ((bootinfo->heap_end - bootinfo->heap_start) / 1024), " kbytes)", endl;
if (DEBUG) cerr << " Per-VCPU stacks from ", bootinfo->heap_start, " to heap end ", bootinfo->heap_end, " (", ((bootinfo->heap_end - bootinfo->heap_start) / 1024), " kbytes)", endl;
bootinfo->vcpu_count = vcpu_count;
bootinfo->max_pages = info.max_pages;
bootinfo->total_machine_pages = total_machine_pages;
bootinfo->monitor_hostcall_port = ptlsim_hostcall_port;
bootinfo->hostcall_port = -1; // will be filled in by PTLsim on connect
bootinfo->monitor_upcall_port = ptlsim_upcall_port;
bootinfo->upcall_port = -1; // will be filled in by PTLsim on connect
bootinfo->monitor_breakout_port = ptlsim_breakout_port;
bootinfo->breakout_port = -1; // will be filled in by PTLsim on connect
bootinfo->hostreq.ready = 0;
bootinfo->hostreq.op = PTLSIM_HOST_NOP;
bootinfo->ptlsim_state = PTLSIM_STATE_INITIALIZING;
bootinfo->queued_upcall_count = 0;
bootinfo->hostreq.op = PTLSIM_HOST_NOP;
bootinfo->logbuf_tail = 0;
bootinfo->logbuf_spinlock.reset();
if (DEBUG) cerr << " Hostcall port: ", bootinfo->monitor_hostcall_port, endl;
if (DEBUG) cerr << " Upcall port: ", bootinfo->monitor_upcall_port, endl;
if (DEBUG) cerr << " Breakout port: ", bootinfo->monitor_breakout_port, endl;
//
// Set up hypercall page
//
{
xen_domctl_t dom0op;
dom0op.domain = domain;
dom0op.u.hypercall_init.gmfn = ptl_mfns[PTLSIM_HYPERCALL_PAGE_PFN];
dom0op.cmd = XEN_DOMCTL_hypercall_init;
assert(xc_domctl(xc, &dom0op) == 0);
}
//
// Set page protections correctly (can't have writable mappings to page table pages)
//
Level1PTE* l1ptes = ptlcore_ptr_to_ptlmon_ptr(bootinfo->ptl_pagedir);
if (DEBUG) cerr << " ", bootinfo->ptl_pagedir_mfn_count, " L1 ptes start at ", l1ptes, endl, flush;
int first_l1_page = ptl_virt_to_pfn(l1ptes);
foreach (i, bootinfo->ptl_pagedir_mfn_count) {
// if (DEBUG) cerr << " Make L1 pfn ", (first_l1_page + i), " read only", endl, flush;
l1ptes[first_l1_page + i].rw = 0;
}
int l2_page = ptl_virt_to_pfn(ptlcore_ptr_to_ptlmon_ptr(bootinfo->ptl_pagedir_map));
if (DEBUG) cerr << " Make L2 pfn ", l2_page, " read only", endl, flush;
l1ptes[l2_page].rw = 0;
int l3_page = ptl_virt_to_pfn(ptlcore_ptr_to_ptlmon_ptr(bootinfo->ptl_level3_map));
if (DEBUG) cerr << " Make L3 pfn ", l3_page, " read only", endl, flush;
l1ptes[l3_page].rw = 0;
int l4_page = ptl_virt_to_pfn(ptlcore_ptr_to_ptlmon_ptr(bootinfo->toplevel_page_table));
if (DEBUG) cerr << " Make L4 pfn ", l4_page, " read only", endl, flush;
l1ptes[l4_page].rw = 0;
if (DEBUG) cerr << " Make L4 mfn ", ptl_level4_mfn, " the L4 page table overlay page", endl, flush;
//
// Unmap the read/write middle region and remap as read only (except for stack, which gets mapped read/write)
//
if (DEBUG) cerr << "Unmap ", bootinfo->mfn_count, " pages at ", image, endl, flush;
unmap_pages(image, bootinfo->mfn_count);
//
// NOTE! bootpage is no longer accessible at this point!
//
rc = pin_page_table_mfns(ptl_mfns + ptl_level1_pfn, ptl_pagedir_mfn_count, 1);
assert(rc == 0);
rc = pin_page_table_mfn(ptl_level2_mfn, 2);
assert(rc == 0);
rc = pin_page_table_mfn(ptl_level3_mfn, 3);
assert(rc == 0);
rc = pin_page_table_mfn(ptl_level4_mfn, 4);
assert(rc == 0);
//
// Make DMA area re-accessible:
//
if (DEBUG) cerr << " Remap ", shared_map_page_count, " pages as read/write at ", image, " (page ", 0, ")", endl, flush;
assert((Waddr)map_pages(ptl_mfns, shared_map_page_count, PROT_READ|PROT_WRITE, (void*)PTLSIM_PSEUDO_VIRT_BASE, MAP_FIXED) == PTLSIM_PSEUDO_VIRT_BASE);
log_buffer_base = (char*)(PTLSIM_PSEUDO_VIRT_BASE + (PTLSIM_LOGBUF_PAGE_PFN * PAGE_SIZE));
memset(log_buffer_base, 0, PTLSIM_LOGBUF_SIZE);
foreach (i, vcpu_count) {
Context& ptlctx = ctx[i];
prep_initial_ptlsim_context(i, ptlctx);
}
//
// Prepare initial shinfo page.
// Upcalls must be disabled on every vcpu;
// PTLsim will re-enable them on startup.
//
ptlsim_entry_shinfo = (shared_info*)ptl_alloc_private_page();
memset(ptlsim_entry_shinfo, 0, 4096);
foreach (i, vcpu_count) {
ptlsim_entry_shinfo->vcpu_info[i].evtchn_upcall_mask = 1;
//
// Put in the initial CPU frequency since this will not be filled in until
// PTLsim has been scheduled for the first timeslice.
//
ptlsim_entry_shinfo->vcpu_info[i].time.tsc_to_system_mul = shinfo->vcpu_info[i].time.tsc_to_system_mul;
ptlsim_entry_shinfo->vcpu_info[i].time.tsc_shift = shinfo->vcpu_info[i].time.tsc_shift;
memset(ptlsim_entry_shinfo->evtchn_mask, 0xff, sizeof(ptlsim_entry_shinfo->evtchn_mask));
}
if (DEBUG) cerr << " PTLsim initial toplevel overlay cr3 = ", (void*)ctx[0].cr3, " (mfn ", (ctx[0].cr3 >> log2(PAGE_SIZE)), ")", endl;
if (DEBUG) cerr << " PTLsim entrypoint at ", (void*)ehdr.e_entry, endl;
return true;
}
//
// Print the early boot log buffer
//
void print_log_buffer(ostream& os) {
bootinfo->logbuf_spinlock.acquire();
int tail = bootinfo->logbuf_tail;
os << "=== Start of log buffer (tail ", tail, ") ===", endl; os.flush();
foreach (i, PTLSIM_LOGBUF_SIZE) {
int offset = (tail + i) % PTLSIM_LOGBUF_SIZE;
char c = log_buffer_base[offset];
if (c) os << c;
}
os << endl, "=== End of log buffer ==", endl; os.flush();
bootinfo->logbuf_spinlock.release();
}
int pin_page_table_page(void* virt, int level) {
assert(inrange(level, 0, 4));
// Was it in PTLsim space?
mfn_t mfn = ptl_virt_to_mfn(virt);
cerr << "pinning mfn ", mfn, " (virt ", virt, "): level ", level, endl, flush;
if (mfn == INVALID_MFN) return -1;
int level_to_function[5] = {MMUEXT_UNPIN_TABLE, MMUEXT_PIN_L1_TABLE, MMUEXT_PIN_L2_TABLE, MMUEXT_PIN_L3_TABLE, MMUEXT_PIN_L4_TABLE};
int func = level_to_function[level];
int rc = 0;
mmuext_op op;
op.cmd = func;
op.arg1.mfn = mfn;
// Pages can only be pinned once!
rc = xc_mmuext_op(xc, &op, 1, domain);
return rc;
}
page_type_t query_page_type(const mfn_t mfn) {
page_type_t pt;
pt.in.mfn = mfn;
mmuext_op_t op;
op.cmd = MMUEXT_QUERY_PAGES;
op.arg1.linear_addr = (Waddr)&pt;
op.arg2.nr_ents = 1;
int rc = xc_mmuext_op(xc, &op, 1, domain);
return pt;
}
int pin_page_table_mfns(const mfn_t* mfns, int count, int level) {
assert(inrange(level, 0, 4));
int level_to_function[5] = {MMUEXT_UNPIN_TABLE, MMUEXT_PIN_L1_TABLE, MMUEXT_PIN_L2_TABLE, MMUEXT_PIN_L3_TABLE, MMUEXT_PIN_L4_TABLE};
int func = level_to_function[level];
mmuext_op* ops = new mmuext_op[count];
foreach (i, count) {
// cerr << "Pin mfn ", mfns[i], " as level ", level, endl, flush;
ops[i].cmd = func;
ops[i].arg1.mfn = mfns[i];
}
// Pages can only be pinned once!
int rc = xc_mmuext_op(xc, ops, count, domain);
//cerr << "Pin rc = ", rc, endl, flush;
delete[] ops;
return rc;
}
int pin_page_table_mfn(mfn_t mfn, int level) {
return pin_page_table_mfns(&mfn, 1, level);
}
bool attach(int domain) {
static const bool DEBUG = log_ptlsim_boot;
int rc;
reset();
cpu_type = get_cpu_type();
this->domain = domain;
xc = xc_interface_open();
if (xc < 0) {
cerr << endl;
cerr << "PTLsim error: cannot connect to Xen hypervisor (xc_interface_open failed: rc ", xc, ")", endl,
endl,
"Please make sure you are running as root, the machine is running under Xen,", endl,
"and the appropriate Xen libraries are installed.", endl, endl;