summaryrefslogtreecommitdiffstats
path: root/test/kvm-eviction.c
blob: 0c04baad56ed612e9b8cafd400f29feb7b9df630 (plain) (blame)
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
#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 <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define SAMPLE_COUNT 64

#define TARGET_CORE 2
#define SECONDARY_CORE 3

extern uint8_t guest_with_start[];
extern uint8_t guest_with_stop[];
extern uint8_t guest_without_start[];
extern uint8_t guest_without_stop[];

static const char *vmtype;

uint64_t
vm_get_rip(struct kvm *kvm)
{
	struct kvm_regs regs;
	uint64_t rip;
	int ret;

	if (!strcmp(vmtype, "sev-snp")) {
		rip = snp_dbg_decrypt_rip(kvm->vmfd);
	} else if (!strcmp(vmtype, "sev-es")) {
		rip = sev_dbg_decrypt_rip(kvm->vmfd);
	} else {
		ret = ioctl(kvm->vcpufd, KVM_GET_REGS, &regs);
		if (ret == -1) err(1, "KVM_GET_REGS");
		rip = regs.rip;
	}

	return rip;
}

void
vm_init(struct kvm *kvm, void *code_start, void *code_end)
{
	size_t ramsize;

	ramsize = L1_SIZE * 2;
	if (!strcmp(vmtype, "kvm")) {
		kvm_init(kvm, ramsize, code_start, code_end);
	} else if (!strcmp(vmtype, "sev")) {
		sev_kvm_init(kvm, ramsize, code_start, code_end);
	} else if (!strcmp(vmtype, "sev-es")) {
		sev_es_kvm_init(kvm, ramsize, code_start, code_end);
	} else if (!strcmp(vmtype, "sev-snp")) {
		sev_snp_kvm_init(kvm, ramsize, code_start, code_end);
	} else {
		errx(1, "invalid version");
	}
}

void
vm_deinit(struct kvm *kvm)
{
	kvm_deinit(kvm);
}

void
collect(struct kvm *kvm, uint8_t *counts)
{
	int ret;

	ret = ioctl(kvm->vcpufd, KVM_RUN, NULL);
	if (ret == -1) err(1, "KVM_RUN");
	// warnx("rip:%lu code:%i", vm_get_rip(kvm), kvm->run->exit_reason);

	if (kvm->run->exit_reason != KVM_EXIT_HLT) {
		errx(1, "KVM died! rip:%lu code:%i",
			vm_get_rip(kvm), kvm->run->exit_reason);
	}

	ret = ioctl(kvm_dev, KVM_CPC_READ_COUNTS, counts);
	if (ret == -1) err(1, "ioctl KVM_CPC_READ_COUNTS");
}

int
main(int argc, const char **argv)
{
	struct kvm vms[2];
	uint8_t counts[2][SAMPLE_COUNT][L1_SETS];
	uint8_t baseline[L1_SETS];
	int i, k, 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);

	pin_process(0, TARGET_CORE, true);

	kvm_setup_init();

	vm_init(&vms[WITH], guest_with_start, guest_with_stop);
	vm_init(&vms[WITHOUT], guest_without_start, guest_without_stop);

	/* reset kernel module state */
	ret = ioctl(kvm_dev, KVM_CPC_RESET);
	if (ret == -1) err(1, "ioctl KVM_CPC_RESET");

	/* resolve page faults in advance (code only covers 1 page)..
	 * we want the read counts to apply between KVM_RUN and KVM_EXIT_HLT,
	 * any exits through PFs inbetween will influence our measurement */
	collect(&vms[WITH], counts[WITH][0]);
	collect(&vms[WITHOUT], counts[WITHOUT][0]);

	/* collect samples */
	for (i = 0; i < SAMPLE_COUNT; i++) {
		collect(&vms[WITH], counts[WITH][i]);
		collect(&vms[WITHOUT], counts[WITHOUT][i]);
	}

	/* calculate measurement baseline */
	memset(baseline, 0xff, L1_SETS);
	for (i = 0; i < SAMPLE_COUNT; i++) {
		for (k = 0; k < L1_SETS; k++) {
			if (counts[WITH][i][k] < baseline[k])
				baseline[k] = counts[WITH][i][k];
			if (counts[WITHOUT][i][k] < baseline[k])
				baseline[k] = counts[WITHOUT][i][k];
		}
	}

	printf("=== Baseline ===\n\n", i);
	print_counts(baseline);
	printf("\n");
	print_counts_raw(baseline);
	printf("\n");

	/* apply baseline and output samples */
	for (i = 0; i < SAMPLE_COUNT; i++) {
		for (k = 0; k < L1_SETS; k++) {
			counts[WITH][i][k] -= baseline[k];
			counts[WITHOUT][i][k] -= baseline[k];
		}

		printf("=== Sample %2i ===\n", i);

		printf("\nWith eviction:\n\n");
		print_counts(counts[WITH][i]);
		printf("\n");
		print_counts_raw(counts[WITH][i]);

		printf("\nWithout eviction:\n\n");
		print_counts(counts[WITHOUT][i]);
		printf("\n");
		print_counts_raw(counts[WITHOUT][i]);
		printf("\n");
	}

	/* check for measurment errors */
	for (i = 0; i < SAMPLE_COUNT; i++) {
		for (k = 0; k < L1_SETS; k++) {
			if (counts[WITH][i][k] + baseline[k] > L1_ASSOC)
				warnx("sample %i: With count OOB for set %i (=%i)",
					i, k, counts[WITH][i][k] + baseline[k]);

			if (counts[WITHOUT][i][k] + baseline[k] > L1_ASSOC)
				warnx("sample %i: Without count OOB for set %i (=%i)",
					i, k, counts[WITHOUT][i][k] + baseline[k]);
		}

		if (!counts[WITH][i][TARGET_SET])
			warnx("sample %i: Missing eviction in target set %i (=%i,%i)",
				i, TARGET_SET, counts[WITH][i][TARGET_SET],
				counts[WITH][i][TARGET_SET] + baseline[TARGET_SET]);
	}

	vm_deinit(&vms[WITH]);
	vm_deinit(&vms[WITHOUT]);

	kvm_setup_deinit();
}