GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / x86 / kvm / hyperv.c
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"
26 #include "ioapic.h"
27 #include "hyperv.h"
28
29 #include <linux/kvm_host.h>
30 #include <linux/highmem.h>
31 #include <linux/sched/cputime.h>
32
33 #include <asm/apicdef.h>
34 #include <trace/events/kvm.h>
35
36 #include "trace.h"
37
38 static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
39 {
40         return atomic64_read(&synic->sint[sint]);
41 }
42
43 static 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
50 static 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
62 static 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
77 static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
78                           u64 data, bool host)
79 {
80         int vector;
81
82         vector = data & HV_SYNIC_SINT_VECTOR_MASK;
83         if (vector < 16 && !host)
84                 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
109 static 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
124 static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
125 {
126         struct kvm_vcpu *vcpu;
127         struct kvm_vcpu_hv_synic *synic;
128
129         vcpu = get_vcpu_by_vpidx(kvm, vpidx);
130         if (!vcpu)
131                 return NULL;
132         synic = vcpu_to_synic(vcpu);
133         return (synic->active) ? synic : NULL;
134 }
135
136 static 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
162 static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
163 {
164         struct kvm *kvm = vcpu->kvm;
165         struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
166         struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
167         struct kvm_vcpu_hv_stimer *stimer;
168         int gsi, idx, stimers_pending;
169
170         trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
171
172         if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
173                 synic_clear_sint_msg_pending(synic, sint);
174
175         /* 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
190         idx = srcu_read_lock(&kvm->irq_srcu);
191         gsi = atomic_read(&synic->sint_to_gsi[sint]);
192         if (gsi != -1)
193                 kvm_notify_acked_gsi(kvm, gsi);
194         srcu_read_unlock(&kvm->irq_srcu, idx);
195 }
196
197 static 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
211 static 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
220         trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
221
222         ret = 0;
223         switch (msr) {
224         case HV_X64_MSR_SCONTROL:
225                 synic->control = data;
226                 if (!host)
227                         synic_exit(synic, msr);
228                 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:
237                 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
238                     !synic->dont_zero_synic_pages)
239                         if (kvm_clear_guest(vcpu->kvm,
240                                             data & PAGE_MASK, PAGE_SIZE)) {
241                                 ret = 1;
242                                 break;
243                         }
244                 synic->evt_page = data;
245                 if (!host)
246                         synic_exit(synic, msr);
247                 break;
248         case HV_X64_MSR_SIMP:
249                 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
250                     !synic->dont_zero_synic_pages)
251                         if (kvm_clear_guest(vcpu->kvm,
252                                             data & PAGE_MASK, PAGE_SIZE)) {
253                                 ret = 1;
254                                 break;
255                         }
256                 synic->msg_page = data;
257                 if (!host)
258                         synic_exit(synic, msr);
259                 break;
260         case HV_X64_MSR_EOM: {
261                 int i;
262
263                 if (!synic->active)
264                         break;
265
266                 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
267                         kvm_hv_notify_acked_sint(vcpu, i);
268                 break;
269         }
270         case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
271                 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
272                 break;
273         default:
274                 ret = 1;
275                 break;
276         }
277         return ret;
278 }
279
280 static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
281 {
282         int ret;
283
284         if (!synic->active)
285                 return 1;
286
287         ret = 0;
288         switch (msr) {
289         case HV_X64_MSR_SCONTROL:
290                 *pdata = synic->control;
291                 break;
292         case HV_X64_MSR_SVERSION:
293                 *pdata = synic->version;
294                 break;
295         case HV_X64_MSR_SIEFP:
296                 *pdata = synic->evt_page;
297                 break;
298         case HV_X64_MSR_SIMP:
299                 *pdata = synic->msg_page;
300                 break;
301         case HV_X64_MSR_EOM:
302                 *pdata = 0;
303                 break;
304         case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
305                 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
306                 break;
307         default:
308                 ret = 1;
309                 break;
310         }
311         return ret;
312 }
313
314 static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
315 {
316         struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
317         struct kvm_lapic_irq irq;
318         int ret, vector;
319
320         if (sint >= ARRAY_SIZE(synic->sint))
321                 return -EINVAL;
322
323         vector = synic_get_sint_vector(synic_read_sint(synic, sint));
324         if (vector < 0)
325                 return -ENOENT;
326
327         memset(&irq, 0, sizeof(irq));
328         irq.shorthand = APIC_DEST_SELF;
329         irq.dest_mode = APIC_DEST_PHYSICAL;
330         irq.delivery_mode = APIC_DM_FIXED;
331         irq.vector = vector;
332         irq.level = 1;
333
334         ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
335         trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
336         return ret;
337 }
338
339 int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
340 {
341         struct kvm_vcpu_hv_synic *synic;
342
343         synic = synic_get(kvm, vpidx);
344         if (!synic)
345                 return -EINVAL;
346
347         return synic_set_irq(synic, sint);
348 }
349
350 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
351 {
352         struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
353         int i;
354
355         trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
356
357         for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
358                 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
359                         kvm_hv_notify_acked_sint(vcpu, i);
360 }
361
362 static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
363 {
364         struct kvm_vcpu_hv_synic *synic;
365
366         synic = synic_get(kvm, vpidx);
367         if (!synic)
368                 return -EINVAL;
369
370         if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
371                 return -EINVAL;
372
373         atomic_set(&synic->sint_to_gsi[sint], gsi);
374         return 0;
375 }
376
377 void kvm_hv_irq_routing_update(struct kvm *kvm)
378 {
379         struct kvm_irq_routing_table *irq_rt;
380         struct kvm_kernel_irq_routing_entry *e;
381         u32 gsi;
382
383         irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
384                                         lockdep_is_held(&kvm->irq_lock));
385
386         for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
387                 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
388                         if (e->type == KVM_IRQ_ROUTING_HV_SINT)
389                                 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
390                                                     e->hv_sint.sint, gsi);
391                 }
392         }
393 }
394
395 static void synic_init(struct kvm_vcpu_hv_synic *synic)
396 {
397         int i;
398
399         memset(synic, 0, sizeof(*synic));
400         synic->version = HV_SYNIC_VERSION_1;
401         for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
402                 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
403                 atomic_set(&synic->sint_to_gsi[i], -1);
404         }
405 }
406
407 static u64 get_time_ref_counter(struct kvm *kvm)
408 {
409         struct kvm_hv *hv = &kvm->arch.hyperv;
410         struct kvm_vcpu *vcpu;
411         u64 tsc;
412
413         /*
414          * The guest has not set up the TSC page or the clock isn't
415          * stable, fall back to get_kvmclock_ns.
416          */
417         if (!hv->tsc_ref.tsc_sequence)
418                 return div_u64(get_kvmclock_ns(kvm), 100);
419
420         vcpu = kvm_get_vcpu(kvm, 0);
421         tsc = kvm_read_l1_tsc(vcpu, rdtsc());
422         return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
423                 + hv->tsc_ref.tsc_offset;
424 }
425
426 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
427                                 bool vcpu_kick)
428 {
429         struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
430
431         set_bit(stimer->index,
432                 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
433         kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
434         if (vcpu_kick)
435                 kvm_vcpu_kick(vcpu);
436 }
437
438 static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
439 {
440         struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
441
442         trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
443                                     stimer->index);
444
445         hrtimer_cancel(&stimer->timer);
446         clear_bit(stimer->index,
447                   vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
448         stimer->msg_pending = false;
449         stimer->exp_time = 0;
450 }
451
452 static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
453 {
454         struct kvm_vcpu_hv_stimer *stimer;
455
456         stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
457         trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
458                                      stimer->index);
459         stimer_mark_pending(stimer, true);
460
461         return HRTIMER_NORESTART;
462 }
463
464 /*
465  * stimer_start() assumptions:
466  * a) stimer->count is not equal to 0
467  * b) stimer->config has HV_STIMER_ENABLE flag
468  */
469 static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
470 {
471         u64 time_now;
472         ktime_t ktime_now;
473
474         time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
475         ktime_now = ktime_get();
476
477         if (stimer->config & HV_STIMER_PERIODIC) {
478                 if (stimer->exp_time) {
479                         if (time_now >= stimer->exp_time) {
480                                 u64 remainder;
481
482                                 div64_u64_rem(time_now - stimer->exp_time,
483                                               stimer->count, &remainder);
484                                 stimer->exp_time =
485                                         time_now + (stimer->count - remainder);
486                         }
487                 } else
488                         stimer->exp_time = time_now + stimer->count;
489
490                 trace_kvm_hv_stimer_start_periodic(
491                                         stimer_to_vcpu(stimer)->vcpu_id,
492                                         stimer->index,
493                                         time_now, stimer->exp_time);
494
495                 hrtimer_start(&stimer->timer,
496                               ktime_add_ns(ktime_now,
497                                            100 * (stimer->exp_time - time_now)),
498                               HRTIMER_MODE_ABS);
499                 return 0;
500         }
501         stimer->exp_time = stimer->count;
502         if (time_now >= stimer->count) {
503                 /*
504                  * Expire timer according to Hypervisor Top-Level Functional
505                  * specification v4(15.3.1):
506                  * "If a one shot is enabled and the specified count is in
507                  * the past, it will expire immediately."
508                  */
509                 stimer_mark_pending(stimer, false);
510                 return 0;
511         }
512
513         trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
514                                            stimer->index,
515                                            time_now, stimer->count);
516
517         hrtimer_start(&stimer->timer,
518                       ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
519                       HRTIMER_MODE_ABS);
520         return 0;
521 }
522
523 static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
524                              bool host)
525 {
526         struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
527         struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
528
529         if (!synic->active && (!host || config))
530                 return 1;
531
532         trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
533                                        stimer->index, config, host);
534
535         stimer_cleanup(stimer);
536         if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
537                 config &= ~HV_STIMER_ENABLE;
538         stimer->config = config;
539         stimer_mark_pending(stimer, false);
540         return 0;
541 }
542
543 static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
544                             bool host)
545 {
546         struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
547         struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
548
549         if (!synic->active && (!host || count))
550                 return 1;
551
552         trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
553                                       stimer->index, count, host);
554
555         stimer_cleanup(stimer);
556         stimer->count = count;
557         if (stimer->count == 0)
558                 stimer->config &= ~HV_STIMER_ENABLE;
559         else if (stimer->config & HV_STIMER_AUTOENABLE)
560                 stimer->config |= HV_STIMER_ENABLE;
561         stimer_mark_pending(stimer, false);
562         return 0;
563 }
564
565 static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
566 {
567         *pconfig = stimer->config;
568         return 0;
569 }
570
571 static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
572 {
573         *pcount = stimer->count;
574         return 0;
575 }
576
577 static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
578                              struct hv_message *src_msg)
579 {
580         struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
581         struct page *page;
582         gpa_t gpa;
583         struct hv_message *dst_msg;
584         int r;
585         struct hv_message_page *msg_page;
586
587         if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
588                 return -ENOENT;
589
590         gpa = synic->msg_page & PAGE_MASK;
591         page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
592         if (is_error_page(page))
593                 return -EFAULT;
594
595         msg_page = kmap_atomic(page);
596         dst_msg = &msg_page->sint_message[sint];
597         if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
598                          src_msg->header.message_type) != HVMSG_NONE) {
599                 dst_msg->header.message_flags.msg_pending = 1;
600                 r = -EAGAIN;
601         } else {
602                 memcpy(&dst_msg->u.payload, &src_msg->u.payload,
603                        src_msg->header.payload_size);
604                 dst_msg->header.message_type = src_msg->header.message_type;
605                 dst_msg->header.payload_size = src_msg->header.payload_size;
606                 r = synic_set_irq(synic, sint);
607                 if (r >= 1)
608                         r = 0;
609                 else if (r == 0)
610                         r = -EFAULT;
611         }
612         kunmap_atomic(msg_page);
613         kvm_release_page_dirty(page);
614         kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
615         return r;
616 }
617
618 static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
619 {
620         struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
621         struct hv_message *msg = &stimer->msg;
622         struct hv_timer_message_payload *payload =
623                         (struct hv_timer_message_payload *)&msg->u.payload;
624
625         payload->expiration_time = stimer->exp_time;
626         payload->delivery_time = get_time_ref_counter(vcpu->kvm);
627         return synic_deliver_msg(vcpu_to_synic(vcpu),
628                                  HV_STIMER_SINT(stimer->config), msg);
629 }
630
631 static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
632 {
633         int r;
634
635         stimer->msg_pending = true;
636         r = stimer_send_msg(stimer);
637         trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
638                                        stimer->index, r);
639         if (!r) {
640                 stimer->msg_pending = false;
641                 if (!(stimer->config & HV_STIMER_PERIODIC))
642                         stimer->config &= ~HV_STIMER_ENABLE;
643         }
644 }
645
646 void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
647 {
648         struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
649         struct kvm_vcpu_hv_stimer *stimer;
650         u64 time_now, exp_time;
651         int i;
652
653         for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
654                 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
655                         stimer = &hv_vcpu->stimer[i];
656                         if (stimer->config & HV_STIMER_ENABLE) {
657                                 exp_time = stimer->exp_time;
658
659                                 if (exp_time) {
660                                         time_now =
661                                                 get_time_ref_counter(vcpu->kvm);
662                                         if (time_now >= exp_time)
663                                                 stimer_expiration(stimer);
664                                 }
665
666                                 if ((stimer->config & HV_STIMER_ENABLE) &&
667                                     stimer->count) {
668                                         if (!stimer->msg_pending)
669                                                 stimer_start(stimer);
670                                 } else
671                                         stimer_cleanup(stimer);
672                         }
673                 }
674 }
675
676 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
677 {
678         struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
679         int i;
680
681         for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
682                 stimer_cleanup(&hv_vcpu->stimer[i]);
683 }
684
685 static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
686 {
687         struct hv_message *msg = &stimer->msg;
688         struct hv_timer_message_payload *payload =
689                         (struct hv_timer_message_payload *)&msg->u.payload;
690
691         memset(&msg->header, 0, sizeof(msg->header));
692         msg->header.message_type = HVMSG_TIMER_EXPIRED;
693         msg->header.payload_size = sizeof(*payload);
694
695         payload->timer_index = stimer->index;
696         payload->expiration_time = 0;
697         payload->delivery_time = 0;
698 }
699
700 static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
701 {
702         memset(stimer, 0, sizeof(*stimer));
703         stimer->index = timer_index;
704         hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
705         stimer->timer.function = stimer_timer_callback;
706         stimer_prepare_msg(stimer);
707 }
708
709 void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
710 {
711         struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
712         int i;
713
714         synic_init(&hv_vcpu->synic);
715
716         bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
717         for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
718                 stimer_init(&hv_vcpu->stimer[i], i);
719 }
720
721 void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
722 {
723         struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
724
725         hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
726 }
727
728 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
729 {
730         struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
731
732         /*
733          * Hyper-V SynIC auto EOI SINT's are
734          * not compatible with APICV, so deactivate APICV
735          */
736         kvm_vcpu_deactivate_apicv(vcpu);
737         synic->active = true;
738         synic->dont_zero_synic_pages = dont_zero_synic_pages;
739         return 0;
740 }
741
742 static bool kvm_hv_msr_partition_wide(u32 msr)
743 {
744         bool r = false;
745
746         switch (msr) {
747         case HV_X64_MSR_GUEST_OS_ID:
748         case HV_X64_MSR_HYPERCALL:
749         case HV_X64_MSR_REFERENCE_TSC:
750         case HV_X64_MSR_TIME_REF_COUNT:
751         case HV_X64_MSR_CRASH_CTL:
752         case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
753         case HV_X64_MSR_RESET:
754                 r = true;
755                 break;
756         }
757
758         return r;
759 }
760
761 static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
762                                      u32 index, u64 *pdata)
763 {
764         struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
765         size_t size = ARRAY_SIZE(hv->hv_crash_param);
766
767         if (WARN_ON_ONCE(index >= size))
768                 return -EINVAL;
769
770         *pdata = hv->hv_crash_param[array_index_nospec(index, size)];
771         return 0;
772 }
773
774 static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
775 {
776         struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
777
778         *pdata = hv->hv_crash_ctl;
779         return 0;
780 }
781
782 static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
783 {
784         struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
785
786         if (host)
787                 hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
788
789         if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
790
791                 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
792                           hv->hv_crash_param[0],
793                           hv->hv_crash_param[1],
794                           hv->hv_crash_param[2],
795                           hv->hv_crash_param[3],
796                           hv->hv_crash_param[4]);
797
798                 /* Send notification about crash to user space */
799                 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
800         }
801
802         return 0;
803 }
804
805 static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
806                                      u32 index, u64 data)
807 {
808         struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
809         size_t size = ARRAY_SIZE(hv->hv_crash_param);
810
811         if (WARN_ON_ONCE(index >= size))
812                 return -EINVAL;
813
814         hv->hv_crash_param[array_index_nospec(index, size)] = data;
815         return 0;
816 }
817
818 /*
819  * The kvmclock and Hyper-V TSC page use similar formulas, and converting
820  * between them is possible:
821  *
822  * kvmclock formula:
823  *    nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
824  *           + system_time
825  *
826  * Hyper-V formula:
827  *    nsec/100 = ticks * scale / 2^64 + offset
828  *
829  * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
830  * By dividing the kvmclock formula by 100 and equating what's left we get:
831  *    ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
832  *            scale / 2^64 =         tsc_to_system_mul * 2^(tsc_shift-32) / 100
833  *            scale        =         tsc_to_system_mul * 2^(32+tsc_shift) / 100
834  *
835  * Now expand the kvmclock formula and divide by 100:
836  *    nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
837  *           - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
838  *           + system_time
839  *    nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
840  *               - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
841  *               + system_time / 100
842  *
843  * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
844  *    nsec/100 = ticks * scale / 2^64
845  *               - tsc_timestamp * scale / 2^64
846  *               + system_time / 100
847  *
848  * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
849  *    offset = system_time / 100 - tsc_timestamp * scale / 2^64
850  *
851  * These two equivalencies are implemented in this function.
852  */
853 static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
854                                         HV_REFERENCE_TSC_PAGE *tsc_ref)
855 {
856         u64 max_mul;
857
858         if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
859                 return false;
860
861         /*
862          * check if scale would overflow, if so we use the time ref counter
863          *    tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
864          *    tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
865          *    tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
866          */
867         max_mul = 100ull << (32 - hv_clock->tsc_shift);
868         if (hv_clock->tsc_to_system_mul >= max_mul)
869                 return false;
870
871         /*
872          * Otherwise compute the scale and offset according to the formulas
873          * derived above.
874          */
875         tsc_ref->tsc_scale =
876                 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
877                                 hv_clock->tsc_to_system_mul,
878                                 100);
879
880         tsc_ref->tsc_offset = hv_clock->system_time;
881         do_div(tsc_ref->tsc_offset, 100);
882         tsc_ref->tsc_offset -=
883                 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
884         return true;
885 }
886
887 void kvm_hv_setup_tsc_page(struct kvm *kvm,
888                            struct pvclock_vcpu_time_info *hv_clock)
889 {
890         struct kvm_hv *hv = &kvm->arch.hyperv;
891         u32 tsc_seq;
892         u64 gfn;
893
894         BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
895         BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
896
897         if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
898                 return;
899
900         mutex_lock(&kvm->arch.hyperv.hv_lock);
901         if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
902                 goto out_unlock;
903
904         gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
905         /*
906          * Because the TSC parameters only vary when there is a
907          * change in the master clock, do not bother with caching.
908          */
909         if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
910                                     &tsc_seq, sizeof(tsc_seq))))
911                 goto out_unlock;
912
913         /*
914          * While we're computing and writing the parameters, force the
915          * guest to use the time reference count MSR.
916          */
917         hv->tsc_ref.tsc_sequence = 0;
918         if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
919                             &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
920                 goto out_unlock;
921
922         if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
923                 goto out_unlock;
924
925         /* Ensure sequence is zero before writing the rest of the struct.  */
926         smp_wmb();
927         if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
928                 goto out_unlock;
929
930         /*
931          * Now switch to the TSC page mechanism by writing the sequence.
932          */
933         tsc_seq++;
934         if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
935                 tsc_seq = 1;
936
937         /* Write the struct entirely before the non-zero sequence.  */
938         smp_wmb();
939
940         hv->tsc_ref.tsc_sequence = tsc_seq;
941         kvm_write_guest(kvm, gfn_to_gpa(gfn),
942                         &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
943 out_unlock:
944         mutex_unlock(&kvm->arch.hyperv.hv_lock);
945 }
946
947 static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
948                              bool host)
949 {
950         struct kvm *kvm = vcpu->kvm;
951         struct kvm_hv *hv = &kvm->arch.hyperv;
952
953         switch (msr) {
954         case HV_X64_MSR_GUEST_OS_ID:
955                 hv->hv_guest_os_id = data;
956                 /* setting guest os id to zero disables hypercall page */
957                 if (!hv->hv_guest_os_id)
958                         hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
959                 break;
960         case HV_X64_MSR_HYPERCALL: {
961                 u64 gfn;
962                 unsigned long addr;
963                 u8 instructions[4];
964
965                 /* if guest os id is not set hypercall should remain disabled */
966                 if (!hv->hv_guest_os_id)
967                         break;
968                 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
969                         hv->hv_hypercall = data;
970                         break;
971                 }
972                 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
973                 addr = gfn_to_hva(kvm, gfn);
974                 if (kvm_is_error_hva(addr))
975                         return 1;
976                 kvm_x86_ops->patch_hypercall(vcpu, instructions);
977                 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
978                 if (__copy_to_user((void __user *)addr, instructions, 4))
979                         return 1;
980                 hv->hv_hypercall = data;
981                 mark_page_dirty(kvm, gfn);
982                 break;
983         }
984         case HV_X64_MSR_REFERENCE_TSC:
985                 hv->hv_tsc_page = data;
986                 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
987                         kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
988                 break;
989         case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
990                 return kvm_hv_msr_set_crash_data(vcpu,
991                                                  msr - HV_X64_MSR_CRASH_P0,
992                                                  data);
993         case HV_X64_MSR_CRASH_CTL:
994                 return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
995         case HV_X64_MSR_RESET:
996                 if (data == 1) {
997                         vcpu_debug(vcpu, "hyper-v reset requested\n");
998                         kvm_make_request(KVM_REQ_HV_RESET, vcpu);
999                 }
1000                 break;
1001         default:
1002                 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1003                             msr, data);
1004                 return 1;
1005         }
1006         return 0;
1007 }
1008
1009 /* Calculate cpu time spent by current task in 100ns units */
1010 static u64 current_task_runtime_100ns(void)
1011 {
1012         u64 utime, stime;
1013
1014         task_cputime_adjusted(current, &utime, &stime);
1015
1016         return div_u64(utime + stime, 100);
1017 }
1018
1019 static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1020 {
1021         struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1022
1023         switch (msr) {
1024         case HV_X64_MSR_VP_INDEX:
1025                 if (!host)
1026                         return 1;
1027                 hv->vp_index = (u32)data;
1028                 break;
1029         case HV_X64_MSR_APIC_ASSIST_PAGE: {
1030                 u64 gfn;
1031                 unsigned long addr;
1032
1033                 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1034                         hv->hv_vapic = data;
1035                         if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1036                                 return 1;
1037                         break;
1038                 }
1039                 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
1040                 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1041                 if (kvm_is_error_hva(addr))
1042                         return 1;
1043                 if (__clear_user((void __user *)addr, PAGE_SIZE))
1044                         return 1;
1045                 hv->hv_vapic = data;
1046                 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1047                 if (kvm_lapic_enable_pv_eoi(vcpu,
1048                                             gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1049                         return 1;
1050                 break;
1051         }
1052         case HV_X64_MSR_EOI:
1053                 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1054         case HV_X64_MSR_ICR:
1055                 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1056         case HV_X64_MSR_TPR:
1057                 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1058         case HV_X64_MSR_VP_RUNTIME:
1059                 if (!host)
1060                         return 1;
1061                 hv->runtime_offset = data - current_task_runtime_100ns();
1062                 break;
1063         case HV_X64_MSR_SCONTROL:
1064         case HV_X64_MSR_SVERSION:
1065         case HV_X64_MSR_SIEFP:
1066         case HV_X64_MSR_SIMP:
1067         case HV_X64_MSR_EOM:
1068         case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1069                 return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
1070         case HV_X64_MSR_STIMER0_CONFIG:
1071         case HV_X64_MSR_STIMER1_CONFIG:
1072         case HV_X64_MSR_STIMER2_CONFIG:
1073         case HV_X64_MSR_STIMER3_CONFIG: {
1074                 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1075
1076                 return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
1077                                          data, host);
1078         }
1079         case HV_X64_MSR_STIMER0_COUNT:
1080         case HV_X64_MSR_STIMER1_COUNT:
1081         case HV_X64_MSR_STIMER2_COUNT:
1082         case HV_X64_MSR_STIMER3_COUNT: {
1083                 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1084
1085                 return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
1086                                         data, host);
1087         }
1088         default:
1089                 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1090                             msr, data);
1091                 return 1;
1092         }
1093
1094         return 0;
1095 }
1096
1097 static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1098 {
1099         u64 data = 0;
1100         struct kvm *kvm = vcpu->kvm;
1101         struct kvm_hv *hv = &kvm->arch.hyperv;
1102
1103         switch (msr) {
1104         case HV_X64_MSR_GUEST_OS_ID:
1105                 data = hv->hv_guest_os_id;
1106                 break;
1107         case HV_X64_MSR_HYPERCALL:
1108                 data = hv->hv_hypercall;
1109                 break;
1110         case HV_X64_MSR_TIME_REF_COUNT:
1111                 data = get_time_ref_counter(kvm);
1112                 break;
1113         case HV_X64_MSR_REFERENCE_TSC:
1114                 data = hv->hv_tsc_page;
1115                 break;
1116         case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1117                 return kvm_hv_msr_get_crash_data(vcpu,
1118                                                  msr - HV_X64_MSR_CRASH_P0,
1119                                                  pdata);
1120         case HV_X64_MSR_CRASH_CTL:
1121                 return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
1122         case HV_X64_MSR_RESET:
1123                 data = 0;
1124                 break;
1125         default:
1126                 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1127                 return 1;
1128         }
1129
1130         *pdata = data;
1131         return 0;
1132 }
1133
1134 static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1135 {
1136         u64 data = 0;
1137         struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1138
1139         switch (msr) {
1140         case HV_X64_MSR_VP_INDEX:
1141                 data = hv->vp_index;
1142                 break;
1143         case HV_X64_MSR_EOI:
1144                 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1145         case HV_X64_MSR_ICR:
1146                 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1147         case HV_X64_MSR_TPR:
1148                 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1149         case HV_X64_MSR_APIC_ASSIST_PAGE:
1150                 data = hv->hv_vapic;
1151                 break;
1152         case HV_X64_MSR_VP_RUNTIME:
1153                 data = current_task_runtime_100ns() + hv->runtime_offset;
1154                 break;
1155         case HV_X64_MSR_SCONTROL:
1156         case HV_X64_MSR_SVERSION:
1157         case HV_X64_MSR_SIEFP:
1158         case HV_X64_MSR_SIMP:
1159         case HV_X64_MSR_EOM:
1160         case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1161                 return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
1162         case HV_X64_MSR_STIMER0_CONFIG:
1163         case HV_X64_MSR_STIMER1_CONFIG:
1164         case HV_X64_MSR_STIMER2_CONFIG:
1165         case HV_X64_MSR_STIMER3_CONFIG: {
1166                 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1167
1168                 return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
1169                                          pdata);
1170         }
1171         case HV_X64_MSR_STIMER0_COUNT:
1172         case HV_X64_MSR_STIMER1_COUNT:
1173         case HV_X64_MSR_STIMER2_COUNT:
1174         case HV_X64_MSR_STIMER3_COUNT: {
1175                 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1176
1177                 return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
1178                                         pdata);
1179         }
1180         case HV_X64_MSR_TSC_FREQUENCY:
1181                 data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
1182                 break;
1183         case HV_X64_MSR_APIC_FREQUENCY:
1184                 data = APIC_BUS_FREQUENCY;
1185                 break;
1186         default:
1187                 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1188                 return 1;
1189         }
1190         *pdata = data;
1191         return 0;
1192 }
1193
1194 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1195 {
1196         if (kvm_hv_msr_partition_wide(msr)) {
1197                 int r;
1198
1199                 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1200                 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
1201                 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1202                 return r;
1203         } else
1204                 return kvm_hv_set_msr(vcpu, msr, data, host);
1205 }
1206
1207 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1208 {
1209         if (kvm_hv_msr_partition_wide(msr)) {
1210                 int r;
1211
1212                 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1213                 r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
1214                 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1215                 return r;
1216         } else
1217                 return kvm_hv_get_msr(vcpu, msr, pdata);
1218 }
1219
1220 bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1221 {
1222         return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
1223 }
1224
1225 static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
1226 {
1227         bool longmode;
1228
1229         longmode = is_64_bit_mode(vcpu);
1230         if (longmode)
1231                 kvm_register_write(vcpu, VCPU_REGS_RAX, result);
1232         else {
1233                 kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
1234                 kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
1235         }
1236 }
1237
1238 static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
1239 {
1240         struct kvm_run *run = vcpu->run;
1241
1242         kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result);
1243         return kvm_skip_emulated_instruction(vcpu);
1244 }
1245
1246 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1247 {
1248         u64 param, ingpa, outgpa, ret;
1249         uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
1250         bool fast, longmode;
1251
1252         /*
1253          * hypercall generates UD from non zero cpl and real mode
1254          * per HYPER-V spec
1255          */
1256         if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1257                 kvm_queue_exception(vcpu, UD_VECTOR);
1258                 return 1;
1259         }
1260
1261         longmode = is_64_bit_mode(vcpu);
1262
1263         if (!longmode) {
1264                 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1265                         (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1266                 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1267                         (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1268                 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1269                         (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1270         }
1271 #ifdef CONFIG_X86_64
1272         else {
1273                 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1274                 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1275                 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1276         }
1277 #endif
1278
1279         code = param & 0xffff;
1280         fast = (param >> 16) & 0x1;
1281         rep_cnt = (param >> 32) & 0xfff;
1282         rep_idx = (param >> 48) & 0xfff;
1283
1284         trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1285
1286         /* Hypercall continuation is not supported yet */
1287         if (rep_cnt || rep_idx) {
1288                 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1289                 goto set_result;
1290         }
1291
1292         switch (code) {
1293         case HVCALL_NOTIFY_LONG_SPIN_WAIT:
1294                 kvm_vcpu_on_spin(vcpu, true);
1295                 break;
1296         case HVCALL_POST_MESSAGE:
1297         case HVCALL_SIGNAL_EVENT:
1298                 /* don't bother userspace if it has no way to handle it */
1299                 if (!vcpu_to_synic(vcpu)->active) {
1300                         res = HV_STATUS_INVALID_HYPERCALL_CODE;
1301                         break;
1302                 }
1303                 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
1304                 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
1305                 vcpu->run->hyperv.u.hcall.input = param;
1306                 vcpu->run->hyperv.u.hcall.params[0] = ingpa;
1307                 vcpu->run->hyperv.u.hcall.params[1] = outgpa;
1308                 vcpu->arch.complete_userspace_io =
1309                                 kvm_hv_hypercall_complete_userspace;
1310                 return 0;
1311         default:
1312                 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1313                 break;
1314         }
1315
1316 set_result:
1317         ret = res | (((u64)rep_done & 0xfff) << 32);
1318         kvm_hv_hypercall_set_result(vcpu, ret);
1319         return 1;
1320 }