kvm/x86: Update SynIC timers on guest entry only
[deliverable/linux.git] / arch / x86 / kvm / hyperv.c
CommitLineData
e83d5887
AS
1/*
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"
5c919412 26#include "ioapic.h"
e83d5887
AS
27#include "hyperv.h"
28
29#include <linux/kvm_host.h>
765eaa0f 30#include <linux/highmem.h>
5c919412 31#include <asm/apicdef.h>
e83d5887
AS
32#include <trace/events/kvm.h>
33
34#include "trace.h"
35
5c919412
AS
36static 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
7be58a64
AS
75static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
76 u64 data, bool host)
5c919412
AS
77{
78 int vector;
79
80 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
7be58a64 81 if (vector < 16 && !host)
5c919412
AS
82 return 1;
83 /*
84 * Guest may configure multiple SINTs to use the same vector, so
85 * we maintain a bitmap of vectors handled by synic, and a
86 * bitmap of vectors with auto-eoi behavior. The bitmaps are
87 * updated here, and atomically queried on fast paths.
88 */
89
90 atomic64_set(&synic->sint[sint], data);
91
92 if (synic_has_vector_connected(synic, vector))
93 __set_bit(vector, synic->vec_bitmap);
94 else
95 __clear_bit(vector, synic->vec_bitmap);
96
97 if (synic_has_vector_auto_eoi(synic, vector))
98 __set_bit(vector, synic->auto_eoi_bitmap);
99 else
100 __clear_bit(vector, synic->auto_eoi_bitmap);
101
102 /* Load SynIC vectors into EOI exit bitmap */
103 kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
104 return 0;
105}
106
107static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
108{
109 struct kvm_vcpu *vcpu;
110 struct kvm_vcpu_hv_synic *synic;
111
112 if (vcpu_id >= atomic_read(&kvm->online_vcpus))
113 return NULL;
114 vcpu = kvm_get_vcpu(kvm, vcpu_id);
115 if (!vcpu)
116 return NULL;
117 synic = vcpu_to_synic(vcpu);
118 return (synic->active) ? synic : NULL;
119}
120
765eaa0f
AS
121static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
122 u32 sint)
123{
124 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
125 struct page *page;
126 gpa_t gpa;
127 struct hv_message *msg;
128 struct hv_message_page *msg_page;
129
130 gpa = synic->msg_page & PAGE_MASK;
131 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
132 if (is_error_page(page)) {
133 vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
134 gpa);
135 return;
136 }
137 msg_page = kmap_atomic(page);
138
139 msg = &msg_page->sint_message[sint];
140 msg->header.message_flags.msg_pending = 0;
141
142 kunmap_atomic(msg_page);
143 kvm_release_page_dirty(page);
144 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
145}
146
5c919412
AS
147static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
148{
149 struct kvm *kvm = vcpu->kvm;
765eaa0f 150 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
1f4b34f8
AS
151 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
152 struct kvm_vcpu_hv_stimer *stimer;
153 int gsi, idx, stimers_pending;
5c919412
AS
154
155 vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint);
156
765eaa0f
AS
157 if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
158 synic_clear_sint_msg_pending(synic, sint);
159
1f4b34f8
AS
160 /* Try to deliver pending Hyper-V SynIC timers messages */
161 stimers_pending = 0;
162 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
163 stimer = &hv_vcpu->stimer[idx];
164 if (stimer->msg_pending &&
165 (stimer->config & HV_STIMER_ENABLE) &&
166 HV_STIMER_SINT(stimer->config) == sint) {
167 set_bit(stimer->index,
168 hv_vcpu->stimer_pending_bitmap);
169 stimers_pending++;
170 }
171 }
172 if (stimers_pending)
173 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
174
5c919412 175 idx = srcu_read_lock(&kvm->irq_srcu);
1f4b34f8 176 gsi = atomic_read(&synic->sint_to_gsi[sint]);
5c919412
AS
177 if (gsi != -1)
178 kvm_notify_acked_gsi(kvm, gsi);
179 srcu_read_unlock(&kvm->irq_srcu, idx);
180}
181
db397571
AS
182static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
183{
184 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
185 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
186
187 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
188 hv_vcpu->exit.u.synic.msr = msr;
189 hv_vcpu->exit.u.synic.control = synic->control;
190 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
191 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
192
193 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
194}
195
5c919412
AS
196static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
197 u32 msr, u64 data, bool host)
198{
199 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
200 int ret;
201
202 if (!synic->active)
203 return 1;
204
205 vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n",
206 msr, data, host);
207 ret = 0;
208 switch (msr) {
209 case HV_X64_MSR_SCONTROL:
210 synic->control = data;
db397571
AS
211 if (!host)
212 synic_exit(synic, msr);
5c919412
AS
213 break;
214 case HV_X64_MSR_SVERSION:
215 if (!host) {
216 ret = 1;
217 break;
218 }
219 synic->version = data;
220 break;
221 case HV_X64_MSR_SIEFP:
222 if (data & HV_SYNIC_SIEFP_ENABLE)
223 if (kvm_clear_guest(vcpu->kvm,
224 data & PAGE_MASK, PAGE_SIZE)) {
225 ret = 1;
226 break;
227 }
228 synic->evt_page = data;
db397571
AS
229 if (!host)
230 synic_exit(synic, msr);
5c919412
AS
231 break;
232 case HV_X64_MSR_SIMP:
233 if (data & HV_SYNIC_SIMP_ENABLE)
234 if (kvm_clear_guest(vcpu->kvm,
235 data & PAGE_MASK, PAGE_SIZE)) {
236 ret = 1;
237 break;
238 }
239 synic->msg_page = data;
db397571
AS
240 if (!host)
241 synic_exit(synic, msr);
5c919412
AS
242 break;
243 case HV_X64_MSR_EOM: {
244 int i;
245
246 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
247 kvm_hv_notify_acked_sint(vcpu, i);
248 break;
249 }
250 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
7be58a64 251 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
5c919412
AS
252 break;
253 default:
254 ret = 1;
255 break;
256 }
257 return ret;
258}
259
260static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
261{
262 int ret;
263
264 if (!synic->active)
265 return 1;
266
267 ret = 0;
268 switch (msr) {
269 case HV_X64_MSR_SCONTROL:
270 *pdata = synic->control;
271 break;
272 case HV_X64_MSR_SVERSION:
273 *pdata = synic->version;
274 break;
275 case HV_X64_MSR_SIEFP:
276 *pdata = synic->evt_page;
277 break;
278 case HV_X64_MSR_SIMP:
279 *pdata = synic->msg_page;
280 break;
281 case HV_X64_MSR_EOM:
282 *pdata = 0;
283 break;
284 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
285 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
286 break;
287 default:
288 ret = 1;
289 break;
290 }
291 return ret;
292}
293
294int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
295{
296 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
297 struct kvm_lapic_irq irq;
298 int ret, vector;
299
300 if (sint >= ARRAY_SIZE(synic->sint))
301 return -EINVAL;
302
303 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
304 if (vector < 0)
305 return -ENOENT;
306
307 memset(&irq, 0, sizeof(irq));
308 irq.dest_id = kvm_apic_id(vcpu->arch.apic);
309 irq.dest_mode = APIC_DEST_PHYSICAL;
310 irq.delivery_mode = APIC_DM_FIXED;
311 irq.vector = vector;
312 irq.level = 1;
313
314 ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL);
315 vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret);
316 return ret;
317}
318
319int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint)
320{
321 struct kvm_vcpu_hv_synic *synic;
322
323 synic = synic_get(kvm, vcpu_id);
324 if (!synic)
325 return -EINVAL;
326
327 return synic_set_irq(synic, sint);
328}
329
330void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
331{
332 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
333 int i;
334
335 vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector);
336
337 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
338 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
339 kvm_hv_notify_acked_sint(vcpu, i);
340}
341
342static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi)
343{
344 struct kvm_vcpu_hv_synic *synic;
345
346 synic = synic_get(kvm, vcpu_id);
347 if (!synic)
348 return -EINVAL;
349
350 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
351 return -EINVAL;
352
353 atomic_set(&synic->sint_to_gsi[sint], gsi);
354 return 0;
355}
356
357void kvm_hv_irq_routing_update(struct kvm *kvm)
358{
359 struct kvm_irq_routing_table *irq_rt;
360 struct kvm_kernel_irq_routing_entry *e;
361 u32 gsi;
362
363 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
364 lockdep_is_held(&kvm->irq_lock));
365
366 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
367 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
368 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
369 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
370 e->hv_sint.sint, gsi);
371 }
372 }
373}
374
375static void synic_init(struct kvm_vcpu_hv_synic *synic)
376{
377 int i;
378
379 memset(synic, 0, sizeof(*synic));
380 synic->version = HV_SYNIC_VERSION_1;
381 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
382 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
383 atomic_set(&synic->sint_to_gsi[i], -1);
384 }
385}
386
93bf4172
AS
387static u64 get_time_ref_counter(struct kvm *kvm)
388{
389 return div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
390}
391
f3b138c5 392static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
1f4b34f8
AS
393 bool vcpu_kick)
394{
395 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
396
397 set_bit(stimer->index,
398 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
399 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
400 if (vcpu_kick)
401 kvm_vcpu_kick(vcpu);
402}
403
1f4b34f8
AS
404static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
405{
406 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
407
019b9781 408 hrtimer_cancel(&stimer->timer);
1f4b34f8
AS
409 clear_bit(stimer->index,
410 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
411 stimer->msg_pending = false;
f808495d 412 stimer->exp_time = 0;
1f4b34f8
AS
413}
414
415static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
416{
417 struct kvm_vcpu_hv_stimer *stimer;
418
419 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
f3b138c5 420 stimer_mark_pending(stimer, true);
1f4b34f8
AS
421
422 return HRTIMER_NORESTART;
423}
424
f808495d
AS
425/*
426 * stimer_start() assumptions:
427 * a) stimer->count is not equal to 0
428 * b) stimer->config has HV_STIMER_ENABLE flag
429 */
1f4b34f8
AS
430static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
431{
432 u64 time_now;
433 ktime_t ktime_now;
434
435 time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
436 ktime_now = ktime_get();
437
438 if (stimer->config & HV_STIMER_PERIODIC) {
f808495d
AS
439 if (stimer->exp_time) {
440 if (time_now >= stimer->exp_time) {
441 u64 remainder;
442
443 div64_u64_rem(time_now - stimer->exp_time,
444 stimer->count, &remainder);
445 stimer->exp_time =
446 time_now + (stimer->count - remainder);
447 }
448 } else
449 stimer->exp_time = time_now + stimer->count;
1f4b34f8 450
1f4b34f8 451 hrtimer_start(&stimer->timer,
f808495d
AS
452 ktime_add_ns(ktime_now,
453 100 * (stimer->exp_time - time_now)),
1f4b34f8
AS
454 HRTIMER_MODE_ABS);
455 return 0;
456 }
457 stimer->exp_time = stimer->count;
458 if (time_now >= stimer->count) {
459 /*
460 * Expire timer according to Hypervisor Top-Level Functional
461 * specification v4(15.3.1):
462 * "If a one shot is enabled and the specified count is in
463 * the past, it will expire immediately."
464 */
f3b138c5 465 stimer_mark_pending(stimer, false);
1f4b34f8
AS
466 return 0;
467 }
468
469 hrtimer_start(&stimer->timer,
470 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
471 HRTIMER_MODE_ABS);
472 return 0;
473}
474
475static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
476 bool host)
477{
f3b138c5 478 stimer_cleanup(stimer);
23a3b201 479 if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
1f4b34f8
AS
480 config &= ~HV_STIMER_ENABLE;
481 stimer->config = config;
f3b138c5 482 stimer_mark_pending(stimer, false);
1f4b34f8
AS
483 return 0;
484}
485
486static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
487 bool host)
488{
1f4b34f8 489 stimer_cleanup(stimer);
f3b138c5 490 stimer->count = count;
1f4b34f8
AS
491 if (stimer->count == 0)
492 stimer->config &= ~HV_STIMER_ENABLE;
f3b138c5 493 else if (stimer->config & HV_STIMER_AUTOENABLE)
1f4b34f8 494 stimer->config |= HV_STIMER_ENABLE;
f3b138c5 495 stimer_mark_pending(stimer, false);
1f4b34f8
AS
496 return 0;
497}
498
499static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
500{
501 *pconfig = stimer->config;
502 return 0;
503}
504
505static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
506{
507 *pcount = stimer->count;
508 return 0;
509}
510
511static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
512 struct hv_message *src_msg)
513{
514 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
515 struct page *page;
516 gpa_t gpa;
517 struct hv_message *dst_msg;
518 int r;
519 struct hv_message_page *msg_page;
520
521 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
522 return -ENOENT;
523
524 gpa = synic->msg_page & PAGE_MASK;
525 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
526 if (is_error_page(page))
527 return -EFAULT;
528
529 msg_page = kmap_atomic(page);
530 dst_msg = &msg_page->sint_message[sint];
531 if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
532 src_msg->header.message_type) != HVMSG_NONE) {
533 dst_msg->header.message_flags.msg_pending = 1;
534 r = -EAGAIN;
535 } else {
536 memcpy(&dst_msg->u.payload, &src_msg->u.payload,
537 src_msg->header.payload_size);
538 dst_msg->header.message_type = src_msg->header.message_type;
539 dst_msg->header.payload_size = src_msg->header.payload_size;
540 r = synic_set_irq(synic, sint);
541 if (r >= 1)
542 r = 0;
543 else if (r == 0)
544 r = -EFAULT;
545 }
546 kunmap_atomic(msg_page);
547 kvm_release_page_dirty(page);
548 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
549 return r;
550}
551
0cdeabb1 552static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
1f4b34f8
AS
553{
554 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
555 struct hv_message *msg = &stimer->msg;
556 struct hv_timer_message_payload *payload =
557 (struct hv_timer_message_payload *)&msg->u.payload;
1f4b34f8 558
1f4b34f8
AS
559 payload->expiration_time = stimer->exp_time;
560 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
0cdeabb1
AS
561 return synic_deliver_msg(vcpu_to_synic(vcpu),
562 HV_STIMER_SINT(stimer->config), msg);
1f4b34f8
AS
563}
564
565static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
566{
0cdeabb1
AS
567 stimer->msg_pending = true;
568 if (!stimer_send_msg(stimer)) {
569 stimer->msg_pending = false;
570 if (!(stimer->config & HV_STIMER_PERIODIC))
571 stimer->config &= ~HV_STIMER_ENABLE;
572 }
1f4b34f8
AS
573}
574
575void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
576{
577 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
578 struct kvm_vcpu_hv_stimer *stimer;
f3b138c5 579 u64 time_now, exp_time;
1f4b34f8
AS
580 int i;
581
582 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
583 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
584 stimer = &hv_vcpu->stimer[i];
1f4b34f8 585 if (stimer->config & HV_STIMER_ENABLE) {
f3b138c5
AS
586 exp_time = stimer->exp_time;
587
588 if (exp_time) {
589 time_now =
590 get_time_ref_counter(vcpu->kvm);
591 if (time_now >= exp_time)
592 stimer_expiration(stimer);
593 }
0cdeabb1 594
f3b138c5
AS
595 if ((stimer->config & HV_STIMER_ENABLE) &&
596 stimer->count)
0cdeabb1
AS
597 stimer_start(stimer);
598 else
599 stimer_cleanup(stimer);
1f4b34f8
AS
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
5c919412
AS
637void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
638{
1f4b34f8
AS
639 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);
5c919412
AS
647}
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
e83d5887
AS
660static 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:
e7d9513b
AS
669 case HV_X64_MSR_CRASH_CTL:
670 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
e516cebb 671 case HV_X64_MSR_RESET:
e83d5887
AS
672 r = true;
673 break;
674 }
675
676 return r;
677}
678
e7d9513b
AS
679static 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)
e83d5887
AS
736{
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 }
e7d9513b
AS
788 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);
e516cebb
AS
794 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;
e83d5887
AS
800 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
9eec50b8
AS
808/* 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)
e83d5887
AS
818{
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);
9eec50b8
AS
851 case HV_X64_MSR_VP_RUNTIME:
852 if (!host)
853 return 1;
854 hv->runtime_offset = data - current_task_runtime_100ns();
855 break;
5c919412
AS
856 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);
1f4b34f8
AS
863 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 }
e83d5887
AS
881 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;
93bf4172
AS
903 case HV_X64_MSR_TIME_REF_COUNT:
904 data = get_time_ref_counter(kvm);
e83d5887 905 break;
e83d5887
AS
906 case HV_X64_MSR_REFERENCE_TSC:
907 data = hv->hv_tsc_page;
908 break;
e7d9513b
AS
909 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);
e516cebb
AS
915 case HV_X64_MSR_RESET:
916 data = 0;
917 break;
e83d5887
AS
918 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;
9eec50b8
AS
954 case HV_X64_MSR_VP_RUNTIME:
955 data = current_task_runtime_100ns() + hv->runtime_offset;
956 break;
5c919412
AS
957 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);
1f4b34f8
AS
964 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 }
e83d5887
AS
982 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
e7d9513b 990int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
e83d5887
AS
991{
992 if (kvm_hv_msr_partition_wide(msr)) {
993 int r;
994
995 mutex_lock(&vcpu->kvm->lock);
e7d9513b 996 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
e83d5887
AS
997 mutex_unlock(&vcpu->kvm->lock);
998 return r;
999 } else
9eec50b8 1000 return kvm_hv_set_msr(vcpu, msr, data, host);
e83d5887
AS
1001}
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}
This page took 0.081436 seconds and 5 git commands to generate.