blob: 337b6d2730fa15b14a8c411b9a77662e48f4a190 [file] [log] [blame]
Andrey Smetanine83d5882015-07-03 15:01:34 +03001/*
2 * KVM Microsoft Hyper-V emulation
3 *
4 * derived from arch/x86/kvm/x86.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
9 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
11 *
12 * Authors:
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
17 * Andrey Smetanin <asmetanin@virtuozzo.com>
18 *
19 * This work is licensed under the terms of the GNU GPL, version 2. See
20 * the COPYING file in the top-level directory.
21 *
22 */
23
24#include "x86.h"
25#include "lapic.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030026#include "ioapic.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030027#include "hyperv.h"
28
29#include <linux/kvm_host.h>
Andrey Smetanin765eaa02015-11-30 19:22:20 +030030#include <linux/highmem.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010031#include <linux/sched/cputime.h>
32
Andrey Smetanin5c9194122015-11-10 15:36:34 +030033#include <asm/apicdef.h>
Andrey Smetanine83d5882015-07-03 15:01:34 +030034#include <trace/events/kvm.h>
35
36#include "trace.h"
37
Andrey Smetanin5c9194122015-11-10 15:36:34 +030038static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
39{
40 return atomic64_read(&synic->sint[sint]);
41}
42
43static inline int synic_get_sint_vector(u64 sint_value)
44{
45 if (sint_value & HV_SYNIC_SINT_MASKED)
46 return -1;
47 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
48}
49
50static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
51 int vector)
52{
53 int i;
54
55 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
56 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
57 return true;
58 }
59 return false;
60}
61
62static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
63 int vector)
64{
65 int i;
66 u64 sint_value;
67
68 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
69 sint_value = synic_read_sint(synic, i);
70 if (synic_get_sint_vector(sint_value) == vector &&
71 sint_value & HV_SYNIC_SINT_AUTO_EOI)
72 return true;
73 }
74 return false;
75}
76
Andrey Smetanin7be58a62015-12-28 18:27:23 +030077static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
78 u64 data, bool host)
Andrey Smetanin5c9194122015-11-10 15:36:34 +030079{
80 int vector;
81
82 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
Andrey Smetanin7be58a62015-12-28 18:27:23 +030083 if (vector < 16 && !host)
Andrey Smetanin5c9194122015-11-10 15:36:34 +030084 return 1;
85 /*
86 * Guest may configure multiple SINTs to use the same vector, so
87 * we maintain a bitmap of vectors handled by synic, and a
88 * bitmap of vectors with auto-eoi behavior. The bitmaps are
89 * updated here, and atomically queried on fast paths.
90 */
91
92 atomic64_set(&synic->sint[sint], data);
93
94 if (synic_has_vector_connected(synic, vector))
95 __set_bit(vector, synic->vec_bitmap);
96 else
97 __clear_bit(vector, synic->vec_bitmap);
98
99 if (synic_has_vector_auto_eoi(synic, vector))
100 __set_bit(vector, synic->auto_eoi_bitmap);
101 else
102 __clear_bit(vector, synic->auto_eoi_bitmap);
103
104 /* Load SynIC vectors into EOI exit bitmap */
105 kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
106 return 0;
107}
108
Roman Kagand3457c82017-07-14 17:13:20 +0300109static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
110{
111 struct kvm_vcpu *vcpu = NULL;
112 int i;
113
114 if (vpidx < KVM_MAX_VCPUS)
115 vcpu = kvm_get_vcpu(kvm, vpidx);
116 if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
117 return vcpu;
118 kvm_for_each_vcpu(i, vcpu, kvm)
119 if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
120 return vcpu;
121 return NULL;
122}
123
124static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300125{
126 struct kvm_vcpu *vcpu;
127 struct kvm_vcpu_hv_synic *synic;
128
Roman Kagand3457c82017-07-14 17:13:20 +0300129 vcpu = get_vcpu_by_vpidx(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300130 if (!vcpu)
131 return NULL;
132 synic = vcpu_to_synic(vcpu);
133 return (synic->active) ? synic : NULL;
134}
135
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300136static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
137 u32 sint)
138{
139 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
140 struct page *page;
141 gpa_t gpa;
142 struct hv_message *msg;
143 struct hv_message_page *msg_page;
144
145 gpa = synic->msg_page & PAGE_MASK;
146 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
147 if (is_error_page(page)) {
148 vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
149 gpa);
150 return;
151 }
152 msg_page = kmap_atomic(page);
153
154 msg = &msg_page->sint_message[sint];
155 msg->header.message_flags.msg_pending = 0;
156
157 kunmap_atomic(msg_page);
158 kvm_release_page_dirty(page);
159 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
160}
161
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300162static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
163{
164 struct kvm *kvm = vcpu->kvm;
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300165 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300166 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
167 struct kvm_vcpu_hv_stimer *stimer;
168 int gsi, idx, stimers_pending;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300169
Andrey Smetanin18659a92015-12-23 16:53:59 +0300170 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300171
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300172 if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
173 synic_clear_sint_msg_pending(synic, sint);
174
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300175 /* Try to deliver pending Hyper-V SynIC timers messages */
176 stimers_pending = 0;
177 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
178 stimer = &hv_vcpu->stimer[idx];
179 if (stimer->msg_pending &&
180 (stimer->config & HV_STIMER_ENABLE) &&
181 HV_STIMER_SINT(stimer->config) == sint) {
182 set_bit(stimer->index,
183 hv_vcpu->stimer_pending_bitmap);
184 stimers_pending++;
185 }
186 }
187 if (stimers_pending)
188 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
189
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300190 idx = srcu_read_lock(&kvm->irq_srcu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300191 gsi = atomic_read(&synic->sint_to_gsi[sint]);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300192 if (gsi != -1)
193 kvm_notify_acked_gsi(kvm, gsi);
194 srcu_read_unlock(&kvm->irq_srcu, idx);
195}
196
Andrey Smetanindb3975712015-11-10 15:36:35 +0300197static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
198{
199 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
200 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
201
202 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
203 hv_vcpu->exit.u.synic.msr = msr;
204 hv_vcpu->exit.u.synic.control = synic->control;
205 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
206 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
207
208 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
209}
210
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300211static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
212 u32 msr, u64 data, bool host)
213{
214 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
215 int ret;
216
217 if (!synic->active)
218 return 1;
219
Andrey Smetanin18659a92015-12-23 16:53:59 +0300220 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
221
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300222 ret = 0;
223 switch (msr) {
224 case HV_X64_MSR_SCONTROL:
225 synic->control = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300226 if (!host)
227 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300228 break;
229 case HV_X64_MSR_SVERSION:
230 if (!host) {
231 ret = 1;
232 break;
233 }
234 synic->version = data;
235 break;
236 case HV_X64_MSR_SIEFP:
Roman Kaganefc479e2017-06-22 16:51:01 +0300237 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
238 !synic->dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300239 if (kvm_clear_guest(vcpu->kvm,
240 data & PAGE_MASK, PAGE_SIZE)) {
241 ret = 1;
242 break;
243 }
244 synic->evt_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300245 if (!host)
246 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300247 break;
248 case HV_X64_MSR_SIMP:
Roman Kaganefc479e2017-06-22 16:51:01 +0300249 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
250 !synic->dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300251 if (kvm_clear_guest(vcpu->kvm,
252 data & PAGE_MASK, PAGE_SIZE)) {
253 ret = 1;
254 break;
255 }
256 synic->msg_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300257 if (!host)
258 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300259 break;
260 case HV_X64_MSR_EOM: {
261 int i;
262
263 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
264 kvm_hv_notify_acked_sint(vcpu, i);
265 break;
266 }
267 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
Andrey Smetanin7be58a62015-12-28 18:27:23 +0300268 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300269 break;
270 default:
271 ret = 1;
272 break;
273 }
274 return ret;
275}
276
277static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
278{
279 int ret;
280
281 if (!synic->active)
282 return 1;
283
284 ret = 0;
285 switch (msr) {
286 case HV_X64_MSR_SCONTROL:
287 *pdata = synic->control;
288 break;
289 case HV_X64_MSR_SVERSION:
290 *pdata = synic->version;
291 break;
292 case HV_X64_MSR_SIEFP:
293 *pdata = synic->evt_page;
294 break;
295 case HV_X64_MSR_SIMP:
296 *pdata = synic->msg_page;
297 break;
298 case HV_X64_MSR_EOM:
299 *pdata = 0;
300 break;
301 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
302 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
303 break;
304 default:
305 ret = 1;
306 break;
307 }
308 return ret;
309}
310
Jiang Biaoecd8a8c2016-11-07 08:56:33 +0800311static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300312{
313 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
314 struct kvm_lapic_irq irq;
315 int ret, vector;
316
317 if (sint >= ARRAY_SIZE(synic->sint))
318 return -EINVAL;
319
320 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
321 if (vector < 0)
322 return -ENOENT;
323
324 memset(&irq, 0, sizeof(irq));
Radim Krčmářf98a3ef2016-12-15 18:06:45 +0100325 irq.shorthand = APIC_DEST_SELF;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300326 irq.dest_mode = APIC_DEST_PHYSICAL;
327 irq.delivery_mode = APIC_DM_FIXED;
328 irq.vector = vector;
329 irq.level = 1;
330
Radim Krčmářf98a3ef2016-12-15 18:06:45 +0100331 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
Andrey Smetanin18659a92015-12-23 16:53:59 +0300332 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300333 return ret;
334}
335
Roman Kagand3457c82017-07-14 17:13:20 +0300336int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300337{
338 struct kvm_vcpu_hv_synic *synic;
339
Roman Kagand3457c82017-07-14 17:13:20 +0300340 synic = synic_get(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300341 if (!synic)
342 return -EINVAL;
343
344 return synic_set_irq(synic, sint);
345}
346
347void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
348{
349 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
350 int i;
351
Andrey Smetanin18659a92015-12-23 16:53:59 +0300352 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300353
354 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
355 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
356 kvm_hv_notify_acked_sint(vcpu, i);
357}
358
Roman Kagand3457c82017-07-14 17:13:20 +0300359static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300360{
361 struct kvm_vcpu_hv_synic *synic;
362
Roman Kagand3457c82017-07-14 17:13:20 +0300363 synic = synic_get(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300364 if (!synic)
365 return -EINVAL;
366
367 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
368 return -EINVAL;
369
370 atomic_set(&synic->sint_to_gsi[sint], gsi);
371 return 0;
372}
373
374void kvm_hv_irq_routing_update(struct kvm *kvm)
375{
376 struct kvm_irq_routing_table *irq_rt;
377 struct kvm_kernel_irq_routing_entry *e;
378 u32 gsi;
379
380 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
381 lockdep_is_held(&kvm->irq_lock));
382
383 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
384 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
385 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
386 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
387 e->hv_sint.sint, gsi);
388 }
389 }
390}
391
392static void synic_init(struct kvm_vcpu_hv_synic *synic)
393{
394 int i;
395
396 memset(synic, 0, sizeof(*synic));
397 synic->version = HV_SYNIC_VERSION_1;
398 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
399 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
400 atomic_set(&synic->sint_to_gsi[i], -1);
401 }
402}
403
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300404static u64 get_time_ref_counter(struct kvm *kvm)
405{
Paolo Bonzini095cf552016-02-08 12:54:12 +0100406 struct kvm_hv *hv = &kvm->arch.hyperv;
407 struct kvm_vcpu *vcpu;
408 u64 tsc;
409
410 /*
411 * The guest has not set up the TSC page or the clock isn't
412 * stable, fall back to get_kvmclock_ns.
413 */
414 if (!hv->tsc_ref.tsc_sequence)
415 return div_u64(get_kvmclock_ns(kvm), 100);
416
417 vcpu = kvm_get_vcpu(kvm, 0);
418 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
419 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
420 + hv->tsc_ref.tsc_offset;
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300421}
422
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300423static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300424 bool vcpu_kick)
425{
426 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
427
428 set_bit(stimer->index,
429 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
430 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
431 if (vcpu_kick)
432 kvm_vcpu_kick(vcpu);
433}
434
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300435static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
436{
437 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
438
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300439 trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
440 stimer->index);
441
Andrey Smetanin019b9782015-12-28 18:27:19 +0300442 hrtimer_cancel(&stimer->timer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300443 clear_bit(stimer->index,
444 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
445 stimer->msg_pending = false;
Andrey Smetaninf8084952015-12-28 18:27:20 +0300446 stimer->exp_time = 0;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300447}
448
449static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
450{
451 struct kvm_vcpu_hv_stimer *stimer;
452
453 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300454 trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
455 stimer->index);
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300456 stimer_mark_pending(stimer, true);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300457
458 return HRTIMER_NORESTART;
459}
460
Andrey Smetaninf8084952015-12-28 18:27:20 +0300461/*
462 * stimer_start() assumptions:
463 * a) stimer->count is not equal to 0
464 * b) stimer->config has HV_STIMER_ENABLE flag
465 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300466static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
467{
468 u64 time_now;
469 ktime_t ktime_now;
470
471 time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
472 ktime_now = ktime_get();
473
474 if (stimer->config & HV_STIMER_PERIODIC) {
Andrey Smetaninf8084952015-12-28 18:27:20 +0300475 if (stimer->exp_time) {
476 if (time_now >= stimer->exp_time) {
477 u64 remainder;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300478
Andrey Smetaninf8084952015-12-28 18:27:20 +0300479 div64_u64_rem(time_now - stimer->exp_time,
480 stimer->count, &remainder);
481 stimer->exp_time =
482 time_now + (stimer->count - remainder);
483 }
484 } else
485 stimer->exp_time = time_now + stimer->count;
486
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300487 trace_kvm_hv_stimer_start_periodic(
488 stimer_to_vcpu(stimer)->vcpu_id,
489 stimer->index,
490 time_now, stimer->exp_time);
491
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300492 hrtimer_start(&stimer->timer,
Andrey Smetaninf8084952015-12-28 18:27:20 +0300493 ktime_add_ns(ktime_now,
494 100 * (stimer->exp_time - time_now)),
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300495 HRTIMER_MODE_ABS);
496 return 0;
497 }
498 stimer->exp_time = stimer->count;
499 if (time_now >= stimer->count) {
500 /*
501 * Expire timer according to Hypervisor Top-Level Functional
502 * specification v4(15.3.1):
503 * "If a one shot is enabled and the specified count is in
504 * the past, it will expire immediately."
505 */
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300506 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300507 return 0;
508 }
509
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300510 trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
511 stimer->index,
512 time_now, stimer->count);
513
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300514 hrtimer_start(&stimer->timer,
515 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
516 HRTIMER_MODE_ABS);
517 return 0;
518}
519
520static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
521 bool host)
522{
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300523 trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
524 stimer->index, config, host);
525
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300526 stimer_cleanup(stimer);
Andrey Smetanin23a3b202015-12-28 18:27:22 +0300527 if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300528 config &= ~HV_STIMER_ENABLE;
529 stimer->config = config;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300530 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300531 return 0;
532}
533
534static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
535 bool host)
536{
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300537 trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
538 stimer->index, count, host);
539
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300540 stimer_cleanup(stimer);
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300541 stimer->count = count;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300542 if (stimer->count == 0)
543 stimer->config &= ~HV_STIMER_ENABLE;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300544 else if (stimer->config & HV_STIMER_AUTOENABLE)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300545 stimer->config |= HV_STIMER_ENABLE;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300546 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300547 return 0;
548}
549
550static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
551{
552 *pconfig = stimer->config;
553 return 0;
554}
555
556static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
557{
558 *pcount = stimer->count;
559 return 0;
560}
561
562static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
563 struct hv_message *src_msg)
564{
565 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
566 struct page *page;
567 gpa_t gpa;
568 struct hv_message *dst_msg;
569 int r;
570 struct hv_message_page *msg_page;
571
572 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
573 return -ENOENT;
574
575 gpa = synic->msg_page & PAGE_MASK;
576 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
577 if (is_error_page(page))
578 return -EFAULT;
579
580 msg_page = kmap_atomic(page);
581 dst_msg = &msg_page->sint_message[sint];
582 if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
583 src_msg->header.message_type) != HVMSG_NONE) {
584 dst_msg->header.message_flags.msg_pending = 1;
585 r = -EAGAIN;
586 } else {
587 memcpy(&dst_msg->u.payload, &src_msg->u.payload,
588 src_msg->header.payload_size);
589 dst_msg->header.message_type = src_msg->header.message_type;
590 dst_msg->header.payload_size = src_msg->header.payload_size;
591 r = synic_set_irq(synic, sint);
592 if (r >= 1)
593 r = 0;
594 else if (r == 0)
595 r = -EFAULT;
596 }
597 kunmap_atomic(msg_page);
598 kvm_release_page_dirty(page);
599 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
600 return r;
601}
602
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300603static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300604{
605 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
606 struct hv_message *msg = &stimer->msg;
607 struct hv_timer_message_payload *payload =
608 (struct hv_timer_message_payload *)&msg->u.payload;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300609
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300610 payload->expiration_time = stimer->exp_time;
611 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300612 return synic_deliver_msg(vcpu_to_synic(vcpu),
613 HV_STIMER_SINT(stimer->config), msg);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300614}
615
616static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
617{
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300618 int r;
619
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300620 stimer->msg_pending = true;
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300621 r = stimer_send_msg(stimer);
622 trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
623 stimer->index, r);
624 if (!r) {
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300625 stimer->msg_pending = false;
626 if (!(stimer->config & HV_STIMER_PERIODIC))
627 stimer->config &= ~HV_STIMER_ENABLE;
628 }
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300629}
630
631void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
632{
633 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
634 struct kvm_vcpu_hv_stimer *stimer;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300635 u64 time_now, exp_time;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300636 int i;
637
638 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
639 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
640 stimer = &hv_vcpu->stimer[i];
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300641 if (stimer->config & HV_STIMER_ENABLE) {
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300642 exp_time = stimer->exp_time;
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300643
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300644 if (exp_time) {
645 time_now =
646 get_time_ref_counter(vcpu->kvm);
647 if (time_now >= exp_time)
648 stimer_expiration(stimer);
649 }
650
651 if ((stimer->config & HV_STIMER_ENABLE) &&
Roman Kaganf1ff89e2017-07-20 17:26:40 +0300652 stimer->count) {
653 if (!stimer->msg_pending)
654 stimer_start(stimer);
655 } else
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300656 stimer_cleanup(stimer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300657 }
658 }
659}
660
661void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
662{
663 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
664 int i;
665
666 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
667 stimer_cleanup(&hv_vcpu->stimer[i]);
668}
669
670static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
671{
672 struct hv_message *msg = &stimer->msg;
673 struct hv_timer_message_payload *payload =
674 (struct hv_timer_message_payload *)&msg->u.payload;
675
676 memset(&msg->header, 0, sizeof(msg->header));
677 msg->header.message_type = HVMSG_TIMER_EXPIRED;
678 msg->header.payload_size = sizeof(*payload);
679
680 payload->timer_index = stimer->index;
681 payload->expiration_time = 0;
682 payload->delivery_time = 0;
683}
684
685static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
686{
687 memset(stimer, 0, sizeof(*stimer));
688 stimer->index = timer_index;
689 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
690 stimer->timer.function = stimer_timer_callback;
691 stimer_prepare_msg(stimer);
692}
693
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300694void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
695{
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300696 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
697 int i;
698
699 synic_init(&hv_vcpu->synic);
700
701 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
702 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
703 stimer_init(&hv_vcpu->stimer[i], i);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300704}
705
Roman Kagand3457c82017-07-14 17:13:20 +0300706void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
707{
708 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
709
710 hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
711}
712
Roman Kaganefc479e2017-06-22 16:51:01 +0300713int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300714{
Roman Kaganefc479e2017-06-22 16:51:01 +0300715 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
716
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300717 /*
718 * Hyper-V SynIC auto EOI SINT's are
719 * not compatible with APICV, so deactivate APICV
720 */
721 kvm_vcpu_deactivate_apicv(vcpu);
Roman Kaganefc479e2017-06-22 16:51:01 +0300722 synic->active = true;
723 synic->dont_zero_synic_pages = dont_zero_synic_pages;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300724 return 0;
725}
726
Andrey Smetanine83d5882015-07-03 15:01:34 +0300727static bool kvm_hv_msr_partition_wide(u32 msr)
728{
729 bool r = false;
730
731 switch (msr) {
732 case HV_X64_MSR_GUEST_OS_ID:
733 case HV_X64_MSR_HYPERCALL:
734 case HV_X64_MSR_REFERENCE_TSC:
735 case HV_X64_MSR_TIME_REF_COUNT:
Andrey Smetanine7d95132015-07-03 15:01:37 +0300736 case HV_X64_MSR_CRASH_CTL:
737 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300738 case HV_X64_MSR_RESET:
Andrey Smetanine83d5882015-07-03 15:01:34 +0300739 r = true;
740 break;
741 }
742
743 return r;
744}
745
Andrey Smetanine7d95132015-07-03 15:01:37 +0300746static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
747 u32 index, u64 *pdata)
748{
749 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
750
751 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
752 return -EINVAL;
753
754 *pdata = hv->hv_crash_param[index];
755 return 0;
756}
757
758static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
759{
760 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
761
762 *pdata = hv->hv_crash_ctl;
763 return 0;
764}
765
766static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
767{
768 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
769
770 if (host)
771 hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
772
773 if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
774
775 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
776 hv->hv_crash_param[0],
777 hv->hv_crash_param[1],
778 hv->hv_crash_param[2],
779 hv->hv_crash_param[3],
780 hv->hv_crash_param[4]);
781
782 /* Send notification about crash to user space */
783 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
784 }
785
786 return 0;
787}
788
789static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
790 u32 index, u64 data)
791{
792 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
793
794 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
795 return -EINVAL;
796
797 hv->hv_crash_param[index] = data;
798 return 0;
799}
800
Paolo Bonzini095cf552016-02-08 12:54:12 +0100801/*
802 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
803 * between them is possible:
804 *
805 * kvmclock formula:
806 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
807 * + system_time
808 *
809 * Hyper-V formula:
810 * nsec/100 = ticks * scale / 2^64 + offset
811 *
812 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
813 * By dividing the kvmclock formula by 100 and equating what's left we get:
814 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
815 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
816 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
817 *
818 * Now expand the kvmclock formula and divide by 100:
819 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
820 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
821 * + system_time
822 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
823 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
824 * + system_time / 100
825 *
826 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
827 * nsec/100 = ticks * scale / 2^64
828 * - tsc_timestamp * scale / 2^64
829 * + system_time / 100
830 *
831 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
832 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
833 *
834 * These two equivalencies are implemented in this function.
835 */
836static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
837 HV_REFERENCE_TSC_PAGE *tsc_ref)
838{
839 u64 max_mul;
840
841 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
842 return false;
843
844 /*
845 * check if scale would overflow, if so we use the time ref counter
846 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
847 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
848 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
849 */
850 max_mul = 100ull << (32 - hv_clock->tsc_shift);
851 if (hv_clock->tsc_to_system_mul >= max_mul)
852 return false;
853
854 /*
855 * Otherwise compute the scale and offset according to the formulas
856 * derived above.
857 */
858 tsc_ref->tsc_scale =
859 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
860 hv_clock->tsc_to_system_mul,
861 100);
862
863 tsc_ref->tsc_offset = hv_clock->system_time;
864 do_div(tsc_ref->tsc_offset, 100);
865 tsc_ref->tsc_offset -=
866 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
867 return true;
868}
869
870void kvm_hv_setup_tsc_page(struct kvm *kvm,
871 struct pvclock_vcpu_time_info *hv_clock)
872{
873 struct kvm_hv *hv = &kvm->arch.hyperv;
874 u32 tsc_seq;
875 u64 gfn;
876
877 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
878 BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
879
880 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
881 return;
882
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100883 mutex_lock(&kvm->arch.hyperv.hv_lock);
884 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
885 goto out_unlock;
886
Paolo Bonzini095cf552016-02-08 12:54:12 +0100887 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
888 /*
889 * Because the TSC parameters only vary when there is a
890 * change in the master clock, do not bother with caching.
891 */
892 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
893 &tsc_seq, sizeof(tsc_seq))))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100894 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100895
896 /*
897 * While we're computing and writing the parameters, force the
898 * guest to use the time reference count MSR.
899 */
900 hv->tsc_ref.tsc_sequence = 0;
901 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
902 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100903 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100904
905 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100906 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100907
908 /* Ensure sequence is zero before writing the rest of the struct. */
909 smp_wmb();
910 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100911 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100912
913 /*
914 * Now switch to the TSC page mechanism by writing the sequence.
915 */
916 tsc_seq++;
917 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
918 tsc_seq = 1;
919
920 /* Write the struct entirely before the non-zero sequence. */
921 smp_wmb();
922
923 hv->tsc_ref.tsc_sequence = tsc_seq;
924 kvm_write_guest(kvm, gfn_to_gpa(gfn),
925 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100926out_unlock:
927 mutex_unlock(&kvm->arch.hyperv.hv_lock);
Paolo Bonzini095cf552016-02-08 12:54:12 +0100928}
929
Andrey Smetanine7d95132015-07-03 15:01:37 +0300930static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
931 bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +0300932{
933 struct kvm *kvm = vcpu->kvm;
934 struct kvm_hv *hv = &kvm->arch.hyperv;
935
936 switch (msr) {
937 case HV_X64_MSR_GUEST_OS_ID:
938 hv->hv_guest_os_id = data;
939 /* setting guest os id to zero disables hypercall page */
940 if (!hv->hv_guest_os_id)
941 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
942 break;
943 case HV_X64_MSR_HYPERCALL: {
944 u64 gfn;
945 unsigned long addr;
946 u8 instructions[4];
947
948 /* if guest os id is not set hypercall should remain disabled */
949 if (!hv->hv_guest_os_id)
950 break;
951 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
952 hv->hv_hypercall = data;
953 break;
954 }
955 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
956 addr = gfn_to_hva(kvm, gfn);
957 if (kvm_is_error_hva(addr))
958 return 1;
959 kvm_x86_ops->patch_hypercall(vcpu, instructions);
960 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
961 if (__copy_to_user((void __user *)addr, instructions, 4))
962 return 1;
963 hv->hv_hypercall = data;
964 mark_page_dirty(kvm, gfn);
965 break;
966 }
Paolo Bonzini095cf552016-02-08 12:54:12 +0100967 case HV_X64_MSR_REFERENCE_TSC:
Andrey Smetanine83d5882015-07-03 15:01:34 +0300968 hv->hv_tsc_page = data;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100969 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
970 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Andrey Smetanine83d5882015-07-03 15:01:34 +0300971 break;
Andrey Smetanine7d95132015-07-03 15:01:37 +0300972 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
973 return kvm_hv_msr_set_crash_data(vcpu,
974 msr - HV_X64_MSR_CRASH_P0,
975 data);
976 case HV_X64_MSR_CRASH_CTL:
977 return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300978 case HV_X64_MSR_RESET:
979 if (data == 1) {
980 vcpu_debug(vcpu, "hyper-v reset requested\n");
981 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
982 }
983 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +0300984 default:
985 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
986 msr, data);
987 return 1;
988 }
989 return 0;
990}
991
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300992/* Calculate cpu time spent by current task in 100ns units */
993static u64 current_task_runtime_100ns(void)
994{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100995 u64 utime, stime;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300996
997 task_cputime_adjusted(current, &utime, &stime);
Frederic Weisbecker5613fda2017-01-31 04:09:23 +0100998
999 return div_u64(utime + stime, 100);
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001000}
1001
1002static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001003{
1004 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1005
1006 switch (msr) {
Roman Kagand3457c82017-07-14 17:13:20 +03001007 case HV_X64_MSR_VP_INDEX:
1008 if (!host)
1009 return 1;
1010 hv->vp_index = (u32)data;
1011 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001012 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1013 u64 gfn;
1014 unsigned long addr;
1015
1016 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1017 hv->hv_vapic = data;
1018 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1019 return 1;
1020 break;
1021 }
1022 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
1023 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1024 if (kvm_is_error_hva(addr))
1025 return 1;
1026 if (__clear_user((void __user *)addr, PAGE_SIZE))
1027 return 1;
1028 hv->hv_vapic = data;
1029 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1030 if (kvm_lapic_enable_pv_eoi(vcpu,
1031 gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1032 return 1;
1033 break;
1034 }
1035 case HV_X64_MSR_EOI:
1036 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1037 case HV_X64_MSR_ICR:
1038 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1039 case HV_X64_MSR_TPR:
1040 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001041 case HV_X64_MSR_VP_RUNTIME:
1042 if (!host)
1043 return 1;
1044 hv->runtime_offset = data - current_task_runtime_100ns();
1045 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001046 case HV_X64_MSR_SCONTROL:
1047 case HV_X64_MSR_SVERSION:
1048 case HV_X64_MSR_SIEFP:
1049 case HV_X64_MSR_SIMP:
1050 case HV_X64_MSR_EOM:
1051 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1052 return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001053 case HV_X64_MSR_STIMER0_CONFIG:
1054 case HV_X64_MSR_STIMER1_CONFIG:
1055 case HV_X64_MSR_STIMER2_CONFIG:
1056 case HV_X64_MSR_STIMER3_CONFIG: {
1057 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1058
1059 return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
1060 data, host);
1061 }
1062 case HV_X64_MSR_STIMER0_COUNT:
1063 case HV_X64_MSR_STIMER1_COUNT:
1064 case HV_X64_MSR_STIMER2_COUNT:
1065 case HV_X64_MSR_STIMER3_COUNT: {
1066 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1067
1068 return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
1069 data, host);
1070 }
Andrey Smetanine83d5882015-07-03 15:01:34 +03001071 default:
1072 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1073 msr, data);
1074 return 1;
1075 }
1076
1077 return 0;
1078}
1079
1080static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1081{
1082 u64 data = 0;
1083 struct kvm *kvm = vcpu->kvm;
1084 struct kvm_hv *hv = &kvm->arch.hyperv;
1085
1086 switch (msr) {
1087 case HV_X64_MSR_GUEST_OS_ID:
1088 data = hv->hv_guest_os_id;
1089 break;
1090 case HV_X64_MSR_HYPERCALL:
1091 data = hv->hv_hypercall;
1092 break;
Andrey Smetanin93bf4172015-11-30 19:22:19 +03001093 case HV_X64_MSR_TIME_REF_COUNT:
1094 data = get_time_ref_counter(kvm);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001095 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001096 case HV_X64_MSR_REFERENCE_TSC:
1097 data = hv->hv_tsc_page;
1098 break;
Andrey Smetanine7d95132015-07-03 15:01:37 +03001099 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1100 return kvm_hv_msr_get_crash_data(vcpu,
1101 msr - HV_X64_MSR_CRASH_P0,
1102 pdata);
1103 case HV_X64_MSR_CRASH_CTL:
1104 return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001105 case HV_X64_MSR_RESET:
1106 data = 0;
1107 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001108 default:
1109 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1110 return 1;
1111 }
1112
1113 *pdata = data;
1114 return 0;
1115}
1116
1117static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1118{
1119 u64 data = 0;
1120 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1121
1122 switch (msr) {
Roman Kagand3457c82017-07-14 17:13:20 +03001123 case HV_X64_MSR_VP_INDEX:
1124 data = hv->vp_index;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001125 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001126 case HV_X64_MSR_EOI:
1127 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1128 case HV_X64_MSR_ICR:
1129 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1130 case HV_X64_MSR_TPR:
1131 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1132 case HV_X64_MSR_APIC_ASSIST_PAGE:
1133 data = hv->hv_vapic;
1134 break;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001135 case HV_X64_MSR_VP_RUNTIME:
1136 data = current_task_runtime_100ns() + hv->runtime_offset;
1137 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001138 case HV_X64_MSR_SCONTROL:
1139 case HV_X64_MSR_SVERSION:
1140 case HV_X64_MSR_SIEFP:
1141 case HV_X64_MSR_SIMP:
1142 case HV_X64_MSR_EOM:
1143 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1144 return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001145 case HV_X64_MSR_STIMER0_CONFIG:
1146 case HV_X64_MSR_STIMER1_CONFIG:
1147 case HV_X64_MSR_STIMER2_CONFIG:
1148 case HV_X64_MSR_STIMER3_CONFIG: {
1149 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1150
1151 return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
1152 pdata);
1153 }
1154 case HV_X64_MSR_STIMER0_COUNT:
1155 case HV_X64_MSR_STIMER1_COUNT:
1156 case HV_X64_MSR_STIMER2_COUNT:
1157 case HV_X64_MSR_STIMER3_COUNT: {
1158 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1159
1160 return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
1161 pdata);
1162 }
Andrey Smetanine83d5882015-07-03 15:01:34 +03001163 default:
1164 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1165 return 1;
1166 }
1167 *pdata = data;
1168 return 0;
1169}
1170
Andrey Smetanine7d95132015-07-03 15:01:37 +03001171int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001172{
1173 if (kvm_hv_msr_partition_wide(msr)) {
1174 int r;
1175
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001176 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine7d95132015-07-03 15:01:37 +03001177 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001178 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001179 return r;
1180 } else
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001181 return kvm_hv_set_msr(vcpu, msr, data, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001182}
1183
1184int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1185{
1186 if (kvm_hv_msr_partition_wide(msr)) {
1187 int r;
1188
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001189 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001190 r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001191 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001192 return r;
1193 } else
1194 return kvm_hv_get_msr(vcpu, msr, pdata);
1195}
1196
1197bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1198{
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001199 return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001200}
1201
Andrey Smetanin83326e42016-02-11 16:45:01 +03001202static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
1203{
1204 bool longmode;
1205
1206 longmode = is_64_bit_mode(vcpu);
1207 if (longmode)
1208 kvm_register_write(vcpu, VCPU_REGS_RAX, result);
1209 else {
1210 kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
1211 kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
1212 }
1213}
1214
1215static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
1216{
1217 struct kvm_run *run = vcpu->run;
1218
1219 kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result);
1220 return 1;
1221}
1222
Andrey Smetanine83d5882015-07-03 15:01:34 +03001223int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1224{
1225 u64 param, ingpa, outgpa, ret;
1226 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
1227 bool fast, longmode;
1228
1229 /*
1230 * hypercall generates UD from non zero cpl and real mode
1231 * per HYPER-V spec
1232 */
1233 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1234 kvm_queue_exception(vcpu, UD_VECTOR);
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03001235 return 1;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001236 }
1237
1238 longmode = is_64_bit_mode(vcpu);
1239
1240 if (!longmode) {
1241 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1242 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1243 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1244 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1245 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1246 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1247 }
1248#ifdef CONFIG_X86_64
1249 else {
1250 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1251 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1252 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1253 }
1254#endif
1255
1256 code = param & 0xffff;
1257 fast = (param >> 16) & 0x1;
1258 rep_cnt = (param >> 32) & 0xfff;
1259 rep_idx = (param >> 48) & 0xfff;
1260
1261 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1262
Andrey Smetaninb2fdc252016-02-11 16:45:00 +03001263 /* Hypercall continuation is not supported yet */
1264 if (rep_cnt || rep_idx) {
1265 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1266 goto set_result;
1267 }
1268
Andrey Smetanine83d5882015-07-03 15:01:34 +03001269 switch (code) {
Andrey Smetanin8ed6d762016-02-11 16:44:57 +03001270 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
Andrey Smetanine83d5882015-07-03 15:01:34 +03001271 kvm_vcpu_on_spin(vcpu);
1272 break;
Andrey Smetanin83326e42016-02-11 16:45:01 +03001273 case HVCALL_POST_MESSAGE:
1274 case HVCALL_SIGNAL_EVENT:
Paolo Bonzinia2b5c3c2016-03-29 11:23:25 +02001275 /* don't bother userspace if it has no way to handle it */
1276 if (!vcpu_to_synic(vcpu)->active) {
1277 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1278 break;
1279 }
Andrey Smetanin83326e42016-02-11 16:45:01 +03001280 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
1281 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
1282 vcpu->run->hyperv.u.hcall.input = param;
1283 vcpu->run->hyperv.u.hcall.params[0] = ingpa;
1284 vcpu->run->hyperv.u.hcall.params[1] = outgpa;
1285 vcpu->arch.complete_userspace_io =
1286 kvm_hv_hypercall_complete_userspace;
1287 return 0;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001288 default:
1289 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1290 break;
1291 }
1292
Andrey Smetaninb2fdc252016-02-11 16:45:00 +03001293set_result:
Andrey Smetanine83d5882015-07-03 15:01:34 +03001294 ret = res | (((u64)rep_done & 0xfff) << 32);
Andrey Smetanin83326e42016-02-11 16:45:01 +03001295 kvm_hv_hypercall_set_result(vcpu, ret);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001296 return 1;
1297}