blob: 6b2ed930bf186bb6aa48bd680d93eeba90c88145 [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>
Andrey Smetanin5c9194122015-11-10 15:36:34 +030031#include <asm/apicdef.h>
Andrey Smetanine83d5882015-07-03 15:01:34 +030032#include <trace/events/kvm.h>
33
34#include "trace.h"
35
Andrey Smetanin5c9194122015-11-10 15:36:34 +030036static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
37{
38 return atomic64_read(&synic->sint[sint]);
39}
40
41static inline int synic_get_sint_vector(u64 sint_value)
42{
43 if (sint_value & HV_SYNIC_SINT_MASKED)
44 return -1;
45 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
46}
47
48static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
49 int vector)
50{
51 int i;
52
53 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
54 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
55 return true;
56 }
57 return false;
58}
59
60static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
61 int vector)
62{
63 int i;
64 u64 sint_value;
65
66 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
67 sint_value = synic_read_sint(synic, i);
68 if (synic_get_sint_vector(sint_value) == vector &&
69 sint_value & HV_SYNIC_SINT_AUTO_EOI)
70 return true;
71 }
72 return false;
73}
74
75static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data)
76{
77 int vector;
78
79 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
80 if (vector < 16)
81 return 1;
82 /*
83 * Guest may configure multiple SINTs to use the same vector, so
84 * we maintain a bitmap of vectors handled by synic, and a
85 * bitmap of vectors with auto-eoi behavior. The bitmaps are
86 * updated here, and atomically queried on fast paths.
87 */
88
89 atomic64_set(&synic->sint[sint], data);
90
91 if (synic_has_vector_connected(synic, vector))
92 __set_bit(vector, synic->vec_bitmap);
93 else
94 __clear_bit(vector, synic->vec_bitmap);
95
96 if (synic_has_vector_auto_eoi(synic, vector))
97 __set_bit(vector, synic->auto_eoi_bitmap);
98 else
99 __clear_bit(vector, synic->auto_eoi_bitmap);
100
101 /* Load SynIC vectors into EOI exit bitmap */
102 kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
103 return 0;
104}
105
106static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
107{
108 struct kvm_vcpu *vcpu;
109 struct kvm_vcpu_hv_synic *synic;
110
111 if (vcpu_id >= atomic_read(&kvm->online_vcpus))
112 return NULL;
113 vcpu = kvm_get_vcpu(kvm, vcpu_id);
114 if (!vcpu)
115 return NULL;
116 synic = vcpu_to_synic(vcpu);
117 return (synic->active) ? synic : NULL;
118}
119
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300120static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
121 u32 sint)
122{
123 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
124 struct page *page;
125 gpa_t gpa;
126 struct hv_message *msg;
127 struct hv_message_page *msg_page;
128
129 gpa = synic->msg_page & PAGE_MASK;
130 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
131 if (is_error_page(page)) {
132 vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
133 gpa);
134 return;
135 }
136 msg_page = kmap_atomic(page);
137
138 msg = &msg_page->sint_message[sint];
139 msg->header.message_flags.msg_pending = 0;
140
141 kunmap_atomic(msg_page);
142 kvm_release_page_dirty(page);
143 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
144}
145
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300146static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
147{
148 struct kvm *kvm = vcpu->kvm;
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300149 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300150 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
151 struct kvm_vcpu_hv_stimer *stimer;
152 int gsi, idx, stimers_pending;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300153
154 vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint);
155
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300156 if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
157 synic_clear_sint_msg_pending(synic, sint);
158
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300159 /* Try to deliver pending Hyper-V SynIC timers messages */
160 stimers_pending = 0;
161 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
162 stimer = &hv_vcpu->stimer[idx];
163 if (stimer->msg_pending &&
164 (stimer->config & HV_STIMER_ENABLE) &&
165 HV_STIMER_SINT(stimer->config) == sint) {
166 set_bit(stimer->index,
167 hv_vcpu->stimer_pending_bitmap);
168 stimers_pending++;
169 }
170 }
171 if (stimers_pending)
172 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
173
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300174 idx = srcu_read_lock(&kvm->irq_srcu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300175 gsi = atomic_read(&synic->sint_to_gsi[sint]);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300176 if (gsi != -1)
177 kvm_notify_acked_gsi(kvm, gsi);
178 srcu_read_unlock(&kvm->irq_srcu, idx);
179}
180
Andrey Smetanindb3975712015-11-10 15:36:35 +0300181static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
182{
183 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
184 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
185
186 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
187 hv_vcpu->exit.u.synic.msr = msr;
188 hv_vcpu->exit.u.synic.control = synic->control;
189 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
190 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
191
192 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
193}
194
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300195static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
196 u32 msr, u64 data, bool host)
197{
198 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
199 int ret;
200
201 if (!synic->active)
202 return 1;
203
204 vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n",
205 msr, data, host);
206 ret = 0;
207 switch (msr) {
208 case HV_X64_MSR_SCONTROL:
209 synic->control = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300210 if (!host)
211 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300212 break;
213 case HV_X64_MSR_SVERSION:
214 if (!host) {
215 ret = 1;
216 break;
217 }
218 synic->version = data;
219 break;
220 case HV_X64_MSR_SIEFP:
221 if (data & HV_SYNIC_SIEFP_ENABLE)
222 if (kvm_clear_guest(vcpu->kvm,
223 data & PAGE_MASK, PAGE_SIZE)) {
224 ret = 1;
225 break;
226 }
227 synic->evt_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300228 if (!host)
229 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300230 break;
231 case HV_X64_MSR_SIMP:
232 if (data & HV_SYNIC_SIMP_ENABLE)
233 if (kvm_clear_guest(vcpu->kvm,
234 data & PAGE_MASK, PAGE_SIZE)) {
235 ret = 1;
236 break;
237 }
238 synic->msg_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300239 if (!host)
240 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300241 break;
242 case HV_X64_MSR_EOM: {
243 int i;
244
245 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
246 kvm_hv_notify_acked_sint(vcpu, i);
247 break;
248 }
249 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
250 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data);
251 break;
252 default:
253 ret = 1;
254 break;
255 }
256 return ret;
257}
258
259static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
260{
261 int ret;
262
263 if (!synic->active)
264 return 1;
265
266 ret = 0;
267 switch (msr) {
268 case HV_X64_MSR_SCONTROL:
269 *pdata = synic->control;
270 break;
271 case HV_X64_MSR_SVERSION:
272 *pdata = synic->version;
273 break;
274 case HV_X64_MSR_SIEFP:
275 *pdata = synic->evt_page;
276 break;
277 case HV_X64_MSR_SIMP:
278 *pdata = synic->msg_page;
279 break;
280 case HV_X64_MSR_EOM:
281 *pdata = 0;
282 break;
283 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
284 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
285 break;
286 default:
287 ret = 1;
288 break;
289 }
290 return ret;
291}
292
293int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
294{
295 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
296 struct kvm_lapic_irq irq;
297 int ret, vector;
298
299 if (sint >= ARRAY_SIZE(synic->sint))
300 return -EINVAL;
301
302 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
303 if (vector < 0)
304 return -ENOENT;
305
306 memset(&irq, 0, sizeof(irq));
307 irq.dest_id = kvm_apic_id(vcpu->arch.apic);
308 irq.dest_mode = APIC_DEST_PHYSICAL;
309 irq.delivery_mode = APIC_DM_FIXED;
310 irq.vector = vector;
311 irq.level = 1;
312
313 ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL);
314 vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret);
315 return ret;
316}
317
318int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint)
319{
320 struct kvm_vcpu_hv_synic *synic;
321
322 synic = synic_get(kvm, vcpu_id);
323 if (!synic)
324 return -EINVAL;
325
326 return synic_set_irq(synic, sint);
327}
328
329void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
330{
331 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
332 int i;
333
334 vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector);
335
336 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
337 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
338 kvm_hv_notify_acked_sint(vcpu, i);
339}
340
341static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi)
342{
343 struct kvm_vcpu_hv_synic *synic;
344
345 synic = synic_get(kvm, vcpu_id);
346 if (!synic)
347 return -EINVAL;
348
349 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
350 return -EINVAL;
351
352 atomic_set(&synic->sint_to_gsi[sint], gsi);
353 return 0;
354}
355
356void kvm_hv_irq_routing_update(struct kvm *kvm)
357{
358 struct kvm_irq_routing_table *irq_rt;
359 struct kvm_kernel_irq_routing_entry *e;
360 u32 gsi;
361
362 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
363 lockdep_is_held(&kvm->irq_lock));
364
365 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
366 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
367 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
368 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
369 e->hv_sint.sint, gsi);
370 }
371 }
372}
373
374static void synic_init(struct kvm_vcpu_hv_synic *synic)
375{
376 int i;
377
378 memset(synic, 0, sizeof(*synic));
379 synic->version = HV_SYNIC_VERSION_1;
380 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
381 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
382 atomic_set(&synic->sint_to_gsi[i], -1);
383 }
384}
385
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300386static u64 get_time_ref_counter(struct kvm *kvm)
387{
388 return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
389}
390
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300391static void stimer_mark_expired(struct kvm_vcpu_hv_stimer *stimer,
392 bool vcpu_kick)
393{
394 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
395
396 set_bit(stimer->index,
397 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
398 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
399 if (vcpu_kick)
400 kvm_vcpu_kick(vcpu);
401}
402
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300403static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
404{
405 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
406
Andrey Smetanin019b9782015-12-28 18:27:19 +0300407 hrtimer_cancel(&stimer->timer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300408 clear_bit(stimer->index,
409 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
410 stimer->msg_pending = false;
411}
412
413static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
414{
415 struct kvm_vcpu_hv_stimer *stimer;
416
417 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
418 stimer_mark_expired(stimer, true);
419
420 return HRTIMER_NORESTART;
421}
422
423static void stimer_restart(struct kvm_vcpu_hv_stimer *stimer)
424{
425 u64 time_now;
426 ktime_t ktime_now;
427 u64 remainder;
428
429 time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
430 ktime_now = ktime_get();
431
432 div64_u64_rem(time_now - stimer->exp_time, stimer->count, &remainder);
433 stimer->exp_time = time_now + (stimer->count - remainder);
434
435 hrtimer_start(&stimer->timer,
436 ktime_add_ns(ktime_now,
437 100 * (stimer->exp_time - time_now)),
438 HRTIMER_MODE_ABS);
439}
440
441static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
442{
443 u64 time_now;
444 ktime_t ktime_now;
445
446 time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
447 ktime_now = ktime_get();
448
449 if (stimer->config & HV_STIMER_PERIODIC) {
450 if (stimer->count == 0)
451 return -EINVAL;
452
453 stimer->exp_time = time_now + stimer->count;
454 hrtimer_start(&stimer->timer,
455 ktime_add_ns(ktime_now, 100 * stimer->count),
456 HRTIMER_MODE_ABS);
457 return 0;
458 }
459 stimer->exp_time = stimer->count;
460 if (time_now >= stimer->count) {
461 /*
462 * Expire timer according to Hypervisor Top-Level Functional
463 * specification v4(15.3.1):
464 * "If a one shot is enabled and the specified count is in
465 * the past, it will expire immediately."
466 */
467 stimer_mark_expired(stimer, false);
468 return 0;
469 }
470
471 hrtimer_start(&stimer->timer,
472 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
473 HRTIMER_MODE_ABS);
474 return 0;
475}
476
477static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
478 bool host)
479{
480 if (stimer->count == 0 || HV_STIMER_SINT(config) == 0)
481 config &= ~HV_STIMER_ENABLE;
482 stimer->config = config;
483 stimer_cleanup(stimer);
484 if (stimer->config & HV_STIMER_ENABLE)
485 if (stimer_start(stimer))
486 return 1;
487 return 0;
488}
489
490static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
491 bool host)
492{
493 stimer->count = count;
494
495 stimer_cleanup(stimer);
496 if (stimer->count == 0)
497 stimer->config &= ~HV_STIMER_ENABLE;
498 else if (stimer->config & HV_STIMER_AUTOENABLE) {
499 stimer->config |= HV_STIMER_ENABLE;
500 if (stimer_start(stimer))
501 return 1;
502 }
503
504 return 0;
505}
506
507static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
508{
509 *pconfig = stimer->config;
510 return 0;
511}
512
513static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
514{
515 *pcount = stimer->count;
516 return 0;
517}
518
519static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
520 struct hv_message *src_msg)
521{
522 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
523 struct page *page;
524 gpa_t gpa;
525 struct hv_message *dst_msg;
526 int r;
527 struct hv_message_page *msg_page;
528
529 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
530 return -ENOENT;
531
532 gpa = synic->msg_page & PAGE_MASK;
533 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
534 if (is_error_page(page))
535 return -EFAULT;
536
537 msg_page = kmap_atomic(page);
538 dst_msg = &msg_page->sint_message[sint];
539 if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
540 src_msg->header.message_type) != HVMSG_NONE) {
541 dst_msg->header.message_flags.msg_pending = 1;
542 r = -EAGAIN;
543 } else {
544 memcpy(&dst_msg->u.payload, &src_msg->u.payload,
545 src_msg->header.payload_size);
546 dst_msg->header.message_type = src_msg->header.message_type;
547 dst_msg->header.payload_size = src_msg->header.payload_size;
548 r = synic_set_irq(synic, sint);
549 if (r >= 1)
550 r = 0;
551 else if (r == 0)
552 r = -EFAULT;
553 }
554 kunmap_atomic(msg_page);
555 kvm_release_page_dirty(page);
556 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
557 return r;
558}
559
560static void stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
561{
562 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
563 struct hv_message *msg = &stimer->msg;
564 struct hv_timer_message_payload *payload =
565 (struct hv_timer_message_payload *)&msg->u.payload;
566 int r;
567
568 stimer->msg_pending = true;
569 payload->expiration_time = stimer->exp_time;
570 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
571 r = synic_deliver_msg(vcpu_to_synic(vcpu),
572 HV_STIMER_SINT(stimer->config), msg);
573 if (!r)
574 stimer->msg_pending = false;
575}
576
577static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
578{
579 stimer_send_msg(stimer);
580 if (!(stimer->config & HV_STIMER_PERIODIC))
Andrey Smetanin1ac1b652015-12-28 18:27:18 +0300581 stimer->config &= ~HV_STIMER_ENABLE;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300582 else
583 stimer_restart(stimer);
584}
585
586void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
587{
588 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
589 struct kvm_vcpu_hv_stimer *stimer;
590 u64 time_now;
591 int i;
592
593 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
594 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
595 stimer = &hv_vcpu->stimer[i];
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300596 if (stimer->config & HV_STIMER_ENABLE) {
597 time_now = get_time_ref_counter(vcpu->kvm);
598 if (time_now >= stimer->exp_time)
599 stimer_expiration(stimer);
600 }
601 }
602}
603
604void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
605{
606 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
607 int i;
608
609 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
610 stimer_cleanup(&hv_vcpu->stimer[i]);
611}
612
613static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
614{
615 struct hv_message *msg = &stimer->msg;
616 struct hv_timer_message_payload *payload =
617 (struct hv_timer_message_payload *)&msg->u.payload;
618
619 memset(&msg->header, 0, sizeof(msg->header));
620 msg->header.message_type = HVMSG_TIMER_EXPIRED;
621 msg->header.payload_size = sizeof(*payload);
622
623 payload->timer_index = stimer->index;
624 payload->expiration_time = 0;
625 payload->delivery_time = 0;
626}
627
628static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
629{
630 memset(stimer, 0, sizeof(*stimer));
631 stimer->index = timer_index;
632 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
633 stimer->timer.function = stimer_timer_callback;
634 stimer_prepare_msg(stimer);
635}
636
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300637void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
638{
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300639 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
640 int i;
641
642 synic_init(&hv_vcpu->synic);
643
644 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
645 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
646 stimer_init(&hv_vcpu->stimer[i], i);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300647}
648
649int kvm_hv_activate_synic(struct kvm_vcpu *vcpu)
650{
651 /*
652 * Hyper-V SynIC auto EOI SINT's are
653 * not compatible with APICV, so deactivate APICV
654 */
655 kvm_vcpu_deactivate_apicv(vcpu);
656 vcpu_to_synic(vcpu)->active = true;
657 return 0;
658}
659
Andrey Smetanine83d5882015-07-03 15:01:34 +0300660static bool kvm_hv_msr_partition_wide(u32 msr)
661{
662 bool r = false;
663
664 switch (msr) {
665 case HV_X64_MSR_GUEST_OS_ID:
666 case HV_X64_MSR_HYPERCALL:
667 case HV_X64_MSR_REFERENCE_TSC:
668 case HV_X64_MSR_TIME_REF_COUNT:
Andrey Smetanine7d95132015-07-03 15:01:37 +0300669 case HV_X64_MSR_CRASH_CTL:
670 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300671 case HV_X64_MSR_RESET:
Andrey Smetanine83d5882015-07-03 15:01:34 +0300672 r = true;
673 break;
674 }
675
676 return r;
677}
678
Andrey Smetanine7d95132015-07-03 15:01:37 +0300679static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
680 u32 index, u64 *pdata)
681{
682 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
683
684 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
685 return -EINVAL;
686
687 *pdata = hv->hv_crash_param[index];
688 return 0;
689}
690
691static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
692{
693 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
694
695 *pdata = hv->hv_crash_ctl;
696 return 0;
697}
698
699static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
700{
701 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
702
703 if (host)
704 hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
705
706 if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
707
708 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
709 hv->hv_crash_param[0],
710 hv->hv_crash_param[1],
711 hv->hv_crash_param[2],
712 hv->hv_crash_param[3],
713 hv->hv_crash_param[4]);
714
715 /* Send notification about crash to user space */
716 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
717 }
718
719 return 0;
720}
721
722static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
723 u32 index, u64 data)
724{
725 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
726
727 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
728 return -EINVAL;
729
730 hv->hv_crash_param[index] = data;
731 return 0;
732}
733
734static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
735 bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +0300736{
737 struct kvm *kvm = vcpu->kvm;
738 struct kvm_hv *hv = &kvm->arch.hyperv;
739
740 switch (msr) {
741 case HV_X64_MSR_GUEST_OS_ID:
742 hv->hv_guest_os_id = data;
743 /* setting guest os id to zero disables hypercall page */
744 if (!hv->hv_guest_os_id)
745 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
746 break;
747 case HV_X64_MSR_HYPERCALL: {
748 u64 gfn;
749 unsigned long addr;
750 u8 instructions[4];
751
752 /* if guest os id is not set hypercall should remain disabled */
753 if (!hv->hv_guest_os_id)
754 break;
755 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
756 hv->hv_hypercall = data;
757 break;
758 }
759 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
760 addr = gfn_to_hva(kvm, gfn);
761 if (kvm_is_error_hva(addr))
762 return 1;
763 kvm_x86_ops->patch_hypercall(vcpu, instructions);
764 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
765 if (__copy_to_user((void __user *)addr, instructions, 4))
766 return 1;
767 hv->hv_hypercall = data;
768 mark_page_dirty(kvm, gfn);
769 break;
770 }
771 case HV_X64_MSR_REFERENCE_TSC: {
772 u64 gfn;
773 HV_REFERENCE_TSC_PAGE tsc_ref;
774
775 memset(&tsc_ref, 0, sizeof(tsc_ref));
776 hv->hv_tsc_page = data;
777 if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
778 break;
779 gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
780 if (kvm_write_guest(
781 kvm,
782 gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
783 &tsc_ref, sizeof(tsc_ref)))
784 return 1;
785 mark_page_dirty(kvm, gfn);
786 break;
787 }
Andrey Smetanine7d95132015-07-03 15:01:37 +0300788 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
789 return kvm_hv_msr_set_crash_data(vcpu,
790 msr - HV_X64_MSR_CRASH_P0,
791 data);
792 case HV_X64_MSR_CRASH_CTL:
793 return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300794 case HV_X64_MSR_RESET:
795 if (data == 1) {
796 vcpu_debug(vcpu, "hyper-v reset requested\n");
797 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
798 }
799 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +0300800 default:
801 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
802 msr, data);
803 return 1;
804 }
805 return 0;
806}
807
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300808/* Calculate cpu time spent by current task in 100ns units */
809static u64 current_task_runtime_100ns(void)
810{
811 cputime_t utime, stime;
812
813 task_cputime_adjusted(current, &utime, &stime);
814 return div_u64(cputime_to_nsecs(utime + stime), 100);
815}
816
817static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +0300818{
819 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
820
821 switch (msr) {
822 case HV_X64_MSR_APIC_ASSIST_PAGE: {
823 u64 gfn;
824 unsigned long addr;
825
826 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
827 hv->hv_vapic = data;
828 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
829 return 1;
830 break;
831 }
832 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
833 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
834 if (kvm_is_error_hva(addr))
835 return 1;
836 if (__clear_user((void __user *)addr, PAGE_SIZE))
837 return 1;
838 hv->hv_vapic = data;
839 kvm_vcpu_mark_page_dirty(vcpu, gfn);
840 if (kvm_lapic_enable_pv_eoi(vcpu,
841 gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
842 return 1;
843 break;
844 }
845 case HV_X64_MSR_EOI:
846 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
847 case HV_X64_MSR_ICR:
848 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
849 case HV_X64_MSR_TPR:
850 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300851 case HV_X64_MSR_VP_RUNTIME:
852 if (!host)
853 return 1;
854 hv->runtime_offset = data - current_task_runtime_100ns();
855 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300856 case HV_X64_MSR_SCONTROL:
857 case HV_X64_MSR_SVERSION:
858 case HV_X64_MSR_SIEFP:
859 case HV_X64_MSR_SIMP:
860 case HV_X64_MSR_EOM:
861 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
862 return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300863 case HV_X64_MSR_STIMER0_CONFIG:
864 case HV_X64_MSR_STIMER1_CONFIG:
865 case HV_X64_MSR_STIMER2_CONFIG:
866 case HV_X64_MSR_STIMER3_CONFIG: {
867 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
868
869 return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
870 data, host);
871 }
872 case HV_X64_MSR_STIMER0_COUNT:
873 case HV_X64_MSR_STIMER1_COUNT:
874 case HV_X64_MSR_STIMER2_COUNT:
875 case HV_X64_MSR_STIMER3_COUNT: {
876 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
877
878 return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
879 data, host);
880 }
Andrey Smetanine83d5882015-07-03 15:01:34 +0300881 default:
882 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
883 msr, data);
884 return 1;
885 }
886
887 return 0;
888}
889
890static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
891{
892 u64 data = 0;
893 struct kvm *kvm = vcpu->kvm;
894 struct kvm_hv *hv = &kvm->arch.hyperv;
895
896 switch (msr) {
897 case HV_X64_MSR_GUEST_OS_ID:
898 data = hv->hv_guest_os_id;
899 break;
900 case HV_X64_MSR_HYPERCALL:
901 data = hv->hv_hypercall;
902 break;
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300903 case HV_X64_MSR_TIME_REF_COUNT:
904 data = get_time_ref_counter(kvm);
Andrey Smetanine83d5882015-07-03 15:01:34 +0300905 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +0300906 case HV_X64_MSR_REFERENCE_TSC:
907 data = hv->hv_tsc_page;
908 break;
Andrey Smetanine7d95132015-07-03 15:01:37 +0300909 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
910 return kvm_hv_msr_get_crash_data(vcpu,
911 msr - HV_X64_MSR_CRASH_P0,
912 pdata);
913 case HV_X64_MSR_CRASH_CTL:
914 return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300915 case HV_X64_MSR_RESET:
916 data = 0;
917 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +0300918 default:
919 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
920 return 1;
921 }
922
923 *pdata = data;
924 return 0;
925}
926
927static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
928{
929 u64 data = 0;
930 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
931
932 switch (msr) {
933 case HV_X64_MSR_VP_INDEX: {
934 int r;
935 struct kvm_vcpu *v;
936
937 kvm_for_each_vcpu(r, v, vcpu->kvm) {
938 if (v == vcpu) {
939 data = r;
940 break;
941 }
942 }
943 break;
944 }
945 case HV_X64_MSR_EOI:
946 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
947 case HV_X64_MSR_ICR:
948 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
949 case HV_X64_MSR_TPR:
950 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
951 case HV_X64_MSR_APIC_ASSIST_PAGE:
952 data = hv->hv_vapic;
953 break;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +0300954 case HV_X64_MSR_VP_RUNTIME:
955 data = current_task_runtime_100ns() + hv->runtime_offset;
956 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300957 case HV_X64_MSR_SCONTROL:
958 case HV_X64_MSR_SVERSION:
959 case HV_X64_MSR_SIEFP:
960 case HV_X64_MSR_SIMP:
961 case HV_X64_MSR_EOM:
962 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
963 return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300964 case HV_X64_MSR_STIMER0_CONFIG:
965 case HV_X64_MSR_STIMER1_CONFIG:
966 case HV_X64_MSR_STIMER2_CONFIG:
967 case HV_X64_MSR_STIMER3_CONFIG: {
968 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
969
970 return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
971 pdata);
972 }
973 case HV_X64_MSR_STIMER0_COUNT:
974 case HV_X64_MSR_STIMER1_COUNT:
975 case HV_X64_MSR_STIMER2_COUNT:
976 case HV_X64_MSR_STIMER3_COUNT: {
977 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
978
979 return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
980 pdata);
981 }
Andrey Smetanine83d5882015-07-03 15:01:34 +0300982 default:
983 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
984 return 1;
985 }
986 *pdata = data;
987 return 0;
988}
989
Andrey Smetanine7d95132015-07-03 15:01:37 +0300990int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +0300991{
992 if (kvm_hv_msr_partition_wide(msr)) {
993 int r;
994
995 mutex_lock(&vcpu->kvm->lock);
Andrey Smetanine7d95132015-07-03 15:01:37 +0300996 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +0300997 mutex_unlock(&vcpu->kvm->lock);
998 return r;
999 } else
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001000 return kvm_hv_set_msr(vcpu, msr, data, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001001}
1002
1003int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1004{
1005 if (kvm_hv_msr_partition_wide(msr)) {
1006 int r;
1007
1008 mutex_lock(&vcpu->kvm->lock);
1009 r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
1010 mutex_unlock(&vcpu->kvm->lock);
1011 return r;
1012 } else
1013 return kvm_hv_get_msr(vcpu, msr, pdata);
1014}
1015
1016bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1017{
1018 return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
1019}
1020
1021int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1022{
1023 u64 param, ingpa, outgpa, ret;
1024 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
1025 bool fast, longmode;
1026
1027 /*
1028 * hypercall generates UD from non zero cpl and real mode
1029 * per HYPER-V spec
1030 */
1031 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1032 kvm_queue_exception(vcpu, UD_VECTOR);
1033 return 0;
1034 }
1035
1036 longmode = is_64_bit_mode(vcpu);
1037
1038 if (!longmode) {
1039 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1040 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1041 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1042 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1043 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1044 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1045 }
1046#ifdef CONFIG_X86_64
1047 else {
1048 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1049 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1050 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1051 }
1052#endif
1053
1054 code = param & 0xffff;
1055 fast = (param >> 16) & 0x1;
1056 rep_cnt = (param >> 32) & 0xfff;
1057 rep_idx = (param >> 48) & 0xfff;
1058
1059 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1060
1061 switch (code) {
1062 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
1063 kvm_vcpu_on_spin(vcpu);
1064 break;
1065 default:
1066 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1067 break;
1068 }
1069
1070 ret = res | (((u64)rep_done & 0xfff) << 32);
1071 if (longmode) {
1072 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
1073 } else {
1074 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
1075 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
1076 }
1077
1078 return 1;
1079}