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
|
diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile
index b804444e16d4..c94f8c4460f1 100644
--- a/arch/x86/kvm/Makefile
+++ b/arch/x86/kvm/Makefile
@@ -10,7 +10,9 @@ endif
KVM := ../../../virt/kvm
kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \
- $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o
+ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o \
+ svm/cachepc/cachepc.o svm/cachepc/util.o
+
kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \
@@ -20,7 +22,8 @@ kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \
kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o \
vmx/evmcs.o vmx/nested.o vmx/posted_intr.o
-kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.o
+kvm-amd-y += svm/svm.o svm/vmenter.o svm/pmu.o svm/nested.o svm/avic.o svm/sev.o \
+ svm/cachepc/cachepc.o svm/cachepc/util.o
obj-$(CONFIG_KVM) += kvm.o
obj-$(CONFIG_KVM_INTEL) += kvm-intel.o
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index 7b3cfbe8f7e3..f9a6b37eb36a 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -2,6 +2,8 @@
#include <linux/kvm_host.h>
+#include "cachepc/cachepc.h"
+
#include "irq.h"
#include "mmu.h"
#include "kvm_cache_regs.h"
@@ -3785,8 +3787,19 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu,
static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
{
- struct vcpu_svm *svm = to_svm(vcpu);
+ static struct cache_ctx *ctx = NULL;
+ static struct cacheline *ds = NULL;
+ static struct cacheline *head = NULL;
+ static int run_index = 0;
+ struct vcpu_svm *svm;
+
+ printk(KERN_WARNING "CachePC: svm_cpu_enter_exit()\n");
+ printk(KERN_WARNING "Vincent CachePC: svm_cpu_enter_exit()\n");
+ cachepc_init_counters();
+ if (!ctx) ctx = cachepc_get_ctx(L1);
+ if (!ds) ds = cachepc_prepare_ds(ctx);
+ svm = to_svm(vcpu);
svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
@@ -3835,8 +3848,15 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
*/
x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
+ head = cachepc_prime(ds);
+
svm_vcpu_enter_exit(vcpu, svm);
+ cachepc_probe(head);
+ //cachepc_print_msrmts(head);
+ printk(KERN_WARNING "Vincent: Saving measurements\n");
+ cachepc_save_msrmts(head);
+
/*
* We do not use IBRS in the kernel. If this vCPU has used the
* SPEC_CTRL MSR it may have left it on; save the value and
@@ -3912,6 +3932,8 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
if (is_guest_mode(vcpu))
return EXIT_FASTPATH_NONE;
+ run_index += 1;
+
return svm_exit_handlers_fastpath(vcpu);
}
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 2541a17ff1c4..1c3c3b63baba 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -51,6 +51,9 @@
#include <linux/io.h>
#include <linux/lockdep.h>
#include <linux/kthread.h>
+#include <linux/proc_fs.h>
+#include <linux/init.h>
+#include <asm/uaccess.h>
#include <asm/processor.h>
#include <asm/ioctl.h>
@@ -66,6 +69,8 @@
/* Worst case buffer size needed for holding an integer. */
#define ITOA_MAX_LEN 12
+#include "../../arch/x86/kvm/svm/cachepc/cachepc.h"
+
MODULE_AUTHOR("Qumranet");
MODULE_LICENSE("GPL");
@@ -143,6 +148,18 @@ static void hardware_disable_all(void);
static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
+struct proc_ops cachepc_proc_ops;
+
+uint16_t *cachepc_msrmts;
+size_t cachepc_msrmts_count;
+EXPORT_SYMBOL(cachepc_msrmts);
+EXPORT_SYMBOL(cachepc_msrmts_count);
+
+cache_ctx *cachepc_ctx;
+cacheline *cachepc_ds;
+EXPORT_SYMBOL(cachepc_ctx);
+EXPORT_SYMBOL(cachepc_ds);
+
__visible bool kvm_rebooting;
EXPORT_SYMBOL_GPL(kvm_rebooting);
@@ -4765,12 +4782,95 @@ static void check_processor_compat(void *data)
*c->ret = kvm_arch_check_processor_compat(c->opaque);
}
+int
+kvm_cachepc_open(struct inode *inode, struct file *file)
+{
+ try_module_get(THIS_MODULE);
+
+ return 0;
+}
+
+int
+kvm_cachepc_close(struct inode *inode, struct file *file)
+{
+ module_put(THIS_MODULE);
+
+ return 0;
+}
+
+ssize_t
+kvm_cachepc_read(struct file *file, char *buf, size_t buflen, loff_t *off)
+{
+ size_t len, left;
+ size_t size;
+
+ printk(KERN_WARNING "CacheSC: Reading entries (%lu:%lli)\n",
+ buflen, off ? *off : 0);
+
+ size = cachepc_msrmts_count * sizeof(uint16_t);
+ if (!off || *off >= size || *off < 0)
+ return 0;
+
+ len = size - *off;
+ if (len > buflen) len = buflen;
+
+ left = copy_to_user(buf, cachepc_msrmts + *off, len);
+
+ len -= left;
+ *off += len;
+
+ return len;
+}
+
+ssize_t
+kvm_cachepc_write(struct file *file, const char *buf, size_t buflen, loff_t *off)
+{
+ return 0;
+}
+
+void
+kvm_cachepc_single_eviction_test(void *p)
+{
+ cacheline *head;
+ cacheline *ptr;
+
+ ptr = cachepc_prepare_victim(cachepc_ctx, 48);
+ head = cachepc_prime(cachepc_ds);
+ cachepc_victim(ptr);
+ cachepc_probe(head);
+
+ printk(KERN_WARNING "CachePC: Test done, results:");
+ cachepc_print_msrmts(head);
+ cachepc_save_msrmts(head);
+
+ cachepc_release_victim(cachepc_ctx, ptr);
+}
+
+void
+kvm_cachepc_init(void *p)
+{
+ int cpu;
+
+ cpu = get_cpu();
+
+ printk(KERN_WARNING "CachePC: Running on core %i\n", cpu);
+
+ cachepc_init_counters();
+
+ cachepc_ctx = cachepc_get_ctx(L1);
+ cachepc_ds = cachepc_prepare_ds(cachepc_ctx);
+
+ kvm_cachepc_single_eviction_test(p);
+
+ put_cpu();
+}
+
int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
struct module *module)
{
+ printk(KERN_WARNING "Vincent: KVM Init called\n");
struct kvm_cpu_compat_check c;
- int r;
- int cpu;
+ int r, cpu;
r = kvm_arch_init(opaque);
if (r)
@@ -4848,6 +4948,20 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
r = kvm_vfio_ops_init();
WARN_ON(r);
+ cachepc_msrmts_count = L1_SETS;
+ cachepc_msrmts = kzalloc(cachepc_msrmts_count * sizeof(uint16_t), GFP_KERNEL);
+ BUG_ON(cachepc_msrmts == NULL);
+
+ r = smp_call_function_single(2, kvm_cachepc_init, NULL, true);
+ WARN_ON(r != 0);
+
+ memset(&cachepc_proc_ops, 0, sizeof(cachepc_proc_ops));
+ cachepc_proc_ops.proc_open = kvm_cachepc_open;
+ cachepc_proc_ops.proc_read = kvm_cachepc_read;
+ cachepc_proc_ops.proc_write = kvm_cachepc_write;
+ cachepc_proc_ops.proc_release = kvm_cachepc_close;
+ proc_create("cachepc", 0644, NULL, &cachepc_proc_ops);
+
return 0;
out_unreg:
@@ -4872,6 +4986,12 @@ EXPORT_SYMBOL_GPL(kvm_init);
void kvm_exit(void)
{
+ remove_proc_entry("cachepc", NULL);
+ kfree(cachepc_msrmts);
+
+ cachepc_release_ds(cachepc_ctx, cachepc_ds);
+ cachepc_release_ctx(cachepc_ctx);
+
debugfs_remove_recursive(kvm_debugfs_dir);
misc_deregister(&kvm_dev);
kmem_cache_destroy(kvm_vcpu_cache);
|