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
|
#include "test/kvm-eviction.h"
#include "test/kvm.h"
#include "test/util.h"
#include "cachepc/uapi.h"
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
static int child;
uint64_t
monitor(struct kvm *kvm, bool baseline)
{
struct cpc_event event;
int ret;
ret = ioctl(kvm_dev, KVM_CPC_POLL_EVENT, &event);
if (ret && errno == EAGAIN) return 0;
if (ret) err(1, "KVM_CPC_POLL_EVENT");
if (event.type != CPC_EVENT_TRACK_PAGE)
errx(1, "unexpected event type %i", event.type);
printf("Event: rip:%08llx prev:%llu next:%llu ret:%llu\n",
vm_get_rip(), event.page.inst_gfn_prev,
event.page.inst_gfn, event.page.retinst);
printf("\n");
ret = ioctl(kvm_dev, KVM_CPC_ACK_EVENT, &event.id);
if (ret) err(1, "KVM_CPC_ACK_EVENT");
return 1;
}
int
main(int argc, const char **argv)
{
struct ipc *ipc;
struct guest guest;
struct kvm kvm;
struct cpc_track_cfg cfg;
uint64_t eventcnt;
int ret;
vmtype = "kvm";
if (argc > 1) vmtype = argv[1];
if (strcmp(vmtype, "kvm") && strcmp(vmtype, "sev")
&& strcmp(vmtype, "sev-es")
&& strcmp(vmtype, "sev-snp"))
errx(1, "invalid vm mode: %s", vmtype);
setvbuf(stdout, NULL, _IONBF, 0);
kvm_setup_init();
ipc = ipc_alloc();
child = fork();
if (child < 0) err(1, "fork");
if (child == 0) {
pin_process(0, TARGET_CORE, true);
guest_init(&guest, "test/kvm-pagestep_guest");
vm_init(&kvm, &guest);
guest_deinit(&guest);
ret = ioctl(kvm_dev, KVM_CPC_RESET, NULL);
if (ret < 0) err(1, "KVM_CPC_RESET");
ipc_signal_parent(ipc);
ipc_wait_parent(ipc);
printf("VM start\n");
do {
ret = ioctl(kvm.vcpufd, KVM_RUN, NULL);
if (ret < 0) err(1, "KVM_RUN");
if (kvm.run->exit_reason == KVM_EXIT_HLT)
printf("VM halt\n");
} while (kvm.run->exit_reason == KVM_EXIT_HLT);
printf("VM exit\n");
vm_deinit(&kvm);
} else {
pin_process(0, SECONDARY_CORE, true);
ipc_wait_child(ipc);
printf("Monitor start\n");
memset(&cfg, 0, sizeof(cfg));
cfg.mode = CPC_TRACK_PAGES;
ret = ioctl(kvm_dev, KVM_CPC_TRACK_MODE, &cfg);
if (ret) err(1, "KVM_CPC_TRACK_MODE");
ipc_signal_child(ipc);
eventcnt = 0;
while (eventcnt < 50) {
eventcnt += monitor(&kvm, true);
}
printf("Monitor exit\n");
}
ipc_free(ipc);
ret = ioctl(kvm_dev, KVM_CPC_RESET, NULL);
if (ret < 0) err(1, "KVM_CPC_RESET");
kvm_setup_deinit();
}
|