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