blob: 51fc74ea773f30b72a04a35776d1782203026d10 [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrey Smetanine83d5882015-07-03 15:01:34 +03002/*
3 * KVM Microsoft Hyper-V emulation
4 *
5 * derived from arch/x86/kvm/x86.c
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
10 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
12 *
13 * Authors:
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 * Amit Shah <amit.shah@qumranet.com>
17 * Ben-Ami Yassour <benami@il.ibm.com>
18 * Andrey Smetanin <asmetanin@virtuozzo.com>
Andrey Smetanine83d5882015-07-03 15:01:34 +030019 */
20
21#include "x86.h"
22#include "lapic.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030023#include "ioapic.h"
Jon Doronf97f5a52020-05-29 16:45:40 +030024#include "cpuid.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030025#include "hyperv.h"
Joao Martins79033be2018-06-13 09:55:44 -040026#include "xen.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030027
Vitaly Kuznetsovb2d8b162019-09-16 18:22:57 +020028#include <linux/cpu.h>
Andrey Smetanine83d5882015-07-03 15:01:34 +030029#include <linux/kvm_host.h>
Andrey Smetanin765eaa02015-11-30 19:22:20 +030030#include <linux/highmem.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010031#include <linux/sched/cputime.h>
Roman Kaganfaeb7832018-02-01 16:48:32 +030032#include <linux/eventfd.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010033
Andrey Smetanin5c9194122015-11-10 15:36:34 +030034#include <asm/apicdef.h>
Andrey Smetanine83d5882015-07-03 15:01:34 +030035#include <trace/events/kvm.h>
36
37#include "trace.h"
Peter Xu59508b32019-12-04 20:07:17 +010038#include "irq.h"
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +020039#include "fpu.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030040
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +010041/* "Hv#1" signature */
42#define HYPERV_CPUID_SIGNATURE_EAX 0x31237648
43
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +020044#define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
45
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +010046static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
47 bool vcpu_kick);
48
Andrey Smetanin5c9194122015-11-10 15:36:34 +030049static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
50{
51 return atomic64_read(&synic->sint[sint]);
52}
53
54static inline int synic_get_sint_vector(u64 sint_value)
55{
56 if (sint_value & HV_SYNIC_SINT_MASKED)
57 return -1;
58 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
59}
60
61static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
62 int vector)
63{
64 int i;
65
66 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
67 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
68 return true;
69 }
70 return false;
71}
72
73static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
74 int vector)
75{
76 int i;
77 u64 sint_value;
78
79 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
80 sint_value = synic_read_sint(synic, i);
81 if (synic_get_sint_vector(sint_value) == vector &&
82 sint_value & HV_SYNIC_SINT_AUTO_EOI)
83 return true;
84 }
85 return false;
86}
87
Vitaly Kuznetsov98f65ad2018-03-01 15:15:13 +010088static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
89 int vector)
Andrey Smetanin5c9194122015-11-10 15:36:34 +030090{
Vitaly Kuznetsov87a8d792018-12-05 16:36:21 +010091 if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
92 return;
93
Andrey Smetanin5c9194122015-11-10 15:36:34 +030094 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);
Vitaly Kuznetsov98f65ad2018-03-01 15:15:13 +0100103}
104
105static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
106 u64 data, bool host)
107{
108 int vector, old_vector;
Vitaly Kuznetsov915e6f72018-03-01 15:15:14 +0100109 bool masked;
Vitaly Kuznetsov98f65ad2018-03-01 15:15:13 +0100110
111 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
Vitaly Kuznetsov915e6f72018-03-01 15:15:14 +0100112 masked = data & HV_SYNIC_SINT_MASKED;
113
114 /*
115 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
116 * default '0x10000' value on boot and this should not #GP. We need to
117 * allow zero-initing the register from host as well.
118 */
119 if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
Vitaly Kuznetsov98f65ad2018-03-01 15:15:13 +0100120 return 1;
121 /*
122 * Guest may configure multiple SINTs to use the same vector, so
123 * we maintain a bitmap of vectors handled by synic, and a
124 * bitmap of vectors with auto-eoi behavior. The bitmaps are
125 * updated here, and atomically queried on fast paths.
126 */
127 old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
128
129 atomic64_set(&synic->sint[sint], data);
130
131 synic_update_vector(synic, old_vector);
132
133 synic_update_vector(synic, vector);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300134
135 /* Load SynIC vectors into EOI exit bitmap */
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100136 kvm_make_request(KVM_REQ_SCAN_IOAPIC, hv_synic_to_vcpu(synic));
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300137 return 0;
138}
139
Roman Kagand3457c82017-07-14 17:13:20 +0300140static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
141{
142 struct kvm_vcpu *vcpu = NULL;
143 int i;
144
Vitaly Kuznetsov91702002018-08-22 12:18:28 +0200145 if (vpidx >= KVM_MAX_VCPUS)
146 return NULL;
147
148 vcpu = kvm_get_vcpu(kvm, vpidx);
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +0100149 if (vcpu && kvm_hv_get_vpindex(vcpu) == vpidx)
Roman Kagand3457c82017-07-14 17:13:20 +0300150 return vcpu;
151 kvm_for_each_vcpu(i, vcpu, kvm)
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +0100152 if (kvm_hv_get_vpindex(vcpu) == vpidx)
Roman Kagand3457c82017-07-14 17:13:20 +0300153 return vcpu;
154 return NULL;
155}
156
157static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300158{
159 struct kvm_vcpu *vcpu;
160 struct kvm_vcpu_hv_synic *synic;
161
Roman Kagand3457c82017-07-14 17:13:20 +0300162 vcpu = get_vcpu_by_vpidx(kvm, vpidx);
Wanpeng Li919f4eb2021-02-26 15:59:59 +0800163 if (!vcpu || !to_hv_vcpu(vcpu))
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300164 return NULL;
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100165 synic = to_hv_synic(vcpu);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300166 return (synic->active) ? synic : NULL;
167}
168
169static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
170{
171 struct kvm *kvm = vcpu->kvm;
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100172 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
Vitaly Kuznetsovef3f3982021-01-26 14:48:05 +0100173 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300174 struct kvm_vcpu_hv_stimer *stimer;
Vitaly Kuznetsov08a800a2018-11-26 16:47:32 +0100175 int gsi, idx;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300176
Andrey Smetanin18659a92015-12-23 16:53:59 +0300177 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300178
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300179 /* Try to deliver pending Hyper-V SynIC timers messages */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300180 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
181 stimer = &hv_vcpu->stimer[idx];
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100182 if (stimer->msg_pending && stimer->config.enable &&
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100183 !stimer->config.direct_mode &&
Vitaly Kuznetsov08a800a2018-11-26 16:47:32 +0100184 stimer->config.sintx == sint)
185 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300186 }
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300187
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300188 idx = srcu_read_lock(&kvm->irq_srcu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300189 gsi = atomic_read(&synic->sint_to_gsi[sint]);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300190 if (gsi != -1)
191 kvm_notify_acked_gsi(kvm, gsi);
192 srcu_read_unlock(&kvm->irq_srcu, idx);
193}
194
Andrey Smetanindb3975712015-11-10 15:36:35 +0300195static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
196{
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100197 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +0100198 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Andrey Smetanindb3975712015-11-10 15:36:35 +0300199
200 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
201 hv_vcpu->exit.u.synic.msr = msr;
202 hv_vcpu->exit.u.synic.control = synic->control;
203 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
204 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
205
206 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
207}
208
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300209static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
210 u32 msr, u64 data, bool host)
211{
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100212 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300213 int ret;
214
Paolo Bonzini44883f02018-07-26 13:01:52 +0200215 if (!synic->active && !host)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300216 return 1;
217
Andrey Smetanin18659a92015-12-23 16:53:59 +0300218 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
219
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300220 ret = 0;
221 switch (msr) {
222 case HV_X64_MSR_SCONTROL:
223 synic->control = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300224 if (!host)
225 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300226 break;
227 case HV_X64_MSR_SVERSION:
228 if (!host) {
229 ret = 1;
230 break;
231 }
232 synic->version = data;
233 break;
234 case HV_X64_MSR_SIEFP:
Roman Kaganefc479e2017-06-22 16:51:01 +0300235 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
236 !synic->dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300237 if (kvm_clear_guest(vcpu->kvm,
238 data & PAGE_MASK, PAGE_SIZE)) {
239 ret = 1;
240 break;
241 }
242 synic->evt_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300243 if (!host)
244 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300245 break;
246 case HV_X64_MSR_SIMP:
Roman Kaganefc479e2017-06-22 16:51:01 +0300247 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
248 !synic->dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300249 if (kvm_clear_guest(vcpu->kvm,
250 data & PAGE_MASK, PAGE_SIZE)) {
251 ret = 1;
252 break;
253 }
254 synic->msg_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300255 if (!host)
256 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300257 break;
258 case HV_X64_MSR_EOM: {
259 int i;
260
261 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
262 kvm_hv_notify_acked_sint(vcpu, i);
263 break;
264 }
265 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
Andrey Smetanin7be58a62015-12-28 18:27:23 +0300266 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300267 break;
268 default:
269 ret = 1;
270 break;
271 }
272 return ret;
273}
274
Jon Doronf97f5a52020-05-29 16:45:40 +0300275static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu)
276{
Vitaly Kuznetsov10d7bf12021-05-21 11:51:37 +0200277 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Jon Doronf97f5a52020-05-29 16:45:40 +0300278
Vitaly Kuznetsov10d7bf12021-05-21 11:51:37 +0200279 return hv_vcpu->cpuid_cache.syndbg_cap_eax &
280 HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
Jon Doronf97f5a52020-05-29 16:45:40 +0300281}
282
283static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu)
284{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100285 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
Jon Doronf97f5a52020-05-29 16:45:40 +0300286
287 if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL)
288 hv->hv_syndbg.control.status =
289 vcpu->run->hyperv.u.syndbg.status;
290 return 1;
291}
292
293static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr)
294{
Vitaly Kuznetsovf69b55e2021-01-26 14:48:08 +0100295 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +0100296 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Jon Doronf97f5a52020-05-29 16:45:40 +0300297
298 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG;
299 hv_vcpu->exit.u.syndbg.msr = msr;
300 hv_vcpu->exit.u.syndbg.control = syndbg->control.control;
301 hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page;
302 hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page;
303 hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page;
304 vcpu->arch.complete_userspace_io =
305 kvm_hv_syndbg_complete_userspace;
306
307 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
308}
309
310static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
311{
Vitaly Kuznetsovf69b55e2021-01-26 14:48:08 +0100312 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
Jon Doronf97f5a52020-05-29 16:45:40 +0300313
314 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
315 return 1;
316
317 trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id,
Vitaly Kuznetsovef3f3982021-01-26 14:48:05 +0100318 to_hv_vcpu(vcpu)->vp_index, msr, data);
Jon Doronf97f5a52020-05-29 16:45:40 +0300319 switch (msr) {
320 case HV_X64_MSR_SYNDBG_CONTROL:
321 syndbg->control.control = data;
322 if (!host)
323 syndbg_exit(vcpu, msr);
324 break;
325 case HV_X64_MSR_SYNDBG_STATUS:
326 syndbg->control.status = data;
327 break;
328 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
329 syndbg->control.send_page = data;
330 break;
331 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
332 syndbg->control.recv_page = data;
333 break;
334 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
335 syndbg->control.pending_page = data;
336 if (!host)
337 syndbg_exit(vcpu, msr);
338 break;
339 case HV_X64_MSR_SYNDBG_OPTIONS:
340 syndbg->options = data;
341 break;
342 default:
343 break;
344 }
345
346 return 0;
347}
348
349static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
350{
Vitaly Kuznetsovf69b55e2021-01-26 14:48:08 +0100351 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
Jon Doronf97f5a52020-05-29 16:45:40 +0300352
353 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
354 return 1;
355
356 switch (msr) {
357 case HV_X64_MSR_SYNDBG_CONTROL:
358 *pdata = syndbg->control.control;
359 break;
360 case HV_X64_MSR_SYNDBG_STATUS:
361 *pdata = syndbg->control.status;
362 break;
363 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
364 *pdata = syndbg->control.send_page;
365 break;
366 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
367 *pdata = syndbg->control.recv_page;
368 break;
369 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
370 *pdata = syndbg->control.pending_page;
371 break;
372 case HV_X64_MSR_SYNDBG_OPTIONS:
373 *pdata = syndbg->options;
374 break;
375 default:
376 break;
377 }
378
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +0100379 trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id, kvm_hv_get_vpindex(vcpu), msr, *pdata);
Jon Doronf97f5a52020-05-29 16:45:40 +0300380
381 return 0;
382}
383
Paolo Bonzini44883f02018-07-26 13:01:52 +0200384static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
385 bool host)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300386{
387 int ret;
388
Paolo Bonzini44883f02018-07-26 13:01:52 +0200389 if (!synic->active && !host)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300390 return 1;
391
392 ret = 0;
393 switch (msr) {
394 case HV_X64_MSR_SCONTROL:
395 *pdata = synic->control;
396 break;
397 case HV_X64_MSR_SVERSION:
398 *pdata = synic->version;
399 break;
400 case HV_X64_MSR_SIEFP:
401 *pdata = synic->evt_page;
402 break;
403 case HV_X64_MSR_SIMP:
404 *pdata = synic->msg_page;
405 break;
406 case HV_X64_MSR_EOM:
407 *pdata = 0;
408 break;
409 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
410 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
411 break;
412 default:
413 ret = 1;
414 break;
415 }
416 return ret;
417}
418
Jiang Biaoecd8a8c2016-11-07 08:56:33 +0800419static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300420{
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100421 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300422 struct kvm_lapic_irq irq;
423 int ret, vector;
424
425 if (sint >= ARRAY_SIZE(synic->sint))
426 return -EINVAL;
427
428 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
429 if (vector < 0)
430 return -ENOENT;
431
432 memset(&irq, 0, sizeof(irq));
Radim Krčmářf98a3ef2016-12-15 18:06:45 +0100433 irq.shorthand = APIC_DEST_SELF;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300434 irq.dest_mode = APIC_DEST_PHYSICAL;
435 irq.delivery_mode = APIC_DM_FIXED;
436 irq.vector = vector;
437 irq.level = 1;
438
Radim Krčmářf98a3ef2016-12-15 18:06:45 +0100439 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
Andrey Smetanin18659a92015-12-23 16:53:59 +0300440 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300441 return ret;
442}
443
Roman Kagand3457c82017-07-14 17:13:20 +0300444int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300445{
446 struct kvm_vcpu_hv_synic *synic;
447
Roman Kagand3457c82017-07-14 17:13:20 +0300448 synic = synic_get(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300449 if (!synic)
450 return -EINVAL;
451
452 return synic_set_irq(synic, sint);
453}
454
455void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
456{
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100457 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300458 int i;
459
Andrey Smetanin18659a92015-12-23 16:53:59 +0300460 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300461
462 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
463 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
464 kvm_hv_notify_acked_sint(vcpu, i);
465}
466
Roman Kagand3457c82017-07-14 17:13:20 +0300467static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300468{
469 struct kvm_vcpu_hv_synic *synic;
470
Roman Kagand3457c82017-07-14 17:13:20 +0300471 synic = synic_get(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300472 if (!synic)
473 return -EINVAL;
474
475 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
476 return -EINVAL;
477
478 atomic_set(&synic->sint_to_gsi[sint], gsi);
479 return 0;
480}
481
482void kvm_hv_irq_routing_update(struct kvm *kvm)
483{
484 struct kvm_irq_routing_table *irq_rt;
485 struct kvm_kernel_irq_routing_entry *e;
486 u32 gsi;
487
488 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
489 lockdep_is_held(&kvm->irq_lock));
490
491 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
492 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
493 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
494 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
495 e->hv_sint.sint, gsi);
496 }
497 }
498}
499
500static void synic_init(struct kvm_vcpu_hv_synic *synic)
501{
502 int i;
503
504 memset(synic, 0, sizeof(*synic));
505 synic->version = HV_SYNIC_VERSION_1;
506 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
507 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
508 atomic_set(&synic->sint_to_gsi[i], -1);
509 }
510}
511
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300512static u64 get_time_ref_counter(struct kvm *kvm)
513{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100514 struct kvm_hv *hv = to_kvm_hv(kvm);
Paolo Bonzini095cf552016-02-08 12:54:12 +0100515 struct kvm_vcpu *vcpu;
516 u64 tsc;
517
518 /*
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +0100519 * Fall back to get_kvmclock_ns() when TSC page hasn't been set up,
520 * is broken, disabled or being updated.
Paolo Bonzini095cf552016-02-08 12:54:12 +0100521 */
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +0100522 if (hv->hv_tsc_page_status != HV_TSC_PAGE_SET)
Paolo Bonzini095cf552016-02-08 12:54:12 +0100523 return div_u64(get_kvmclock_ns(kvm), 100);
524
525 vcpu = kvm_get_vcpu(kvm, 0);
526 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
527 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
528 + hv->tsc_ref.tsc_offset;
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300529}
530
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300531static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300532 bool vcpu_kick)
533{
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100534 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300535
536 set_bit(stimer->index,
Vitaly Kuznetsovef3f3982021-01-26 14:48:05 +0100537 to_hv_vcpu(vcpu)->stimer_pending_bitmap);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300538 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
539 if (vcpu_kick)
540 kvm_vcpu_kick(vcpu);
541}
542
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300543static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
544{
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100545 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300546
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100547 trace_kvm_hv_stimer_cleanup(hv_stimer_to_vcpu(stimer)->vcpu_id,
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300548 stimer->index);
549
Andrey Smetanin019b9782015-12-28 18:27:19 +0300550 hrtimer_cancel(&stimer->timer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300551 clear_bit(stimer->index,
Vitaly Kuznetsovef3f3982021-01-26 14:48:05 +0100552 to_hv_vcpu(vcpu)->stimer_pending_bitmap);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300553 stimer->msg_pending = false;
Andrey Smetaninf8084952015-12-28 18:27:20 +0300554 stimer->exp_time = 0;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300555}
556
557static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
558{
559 struct kvm_vcpu_hv_stimer *stimer;
560
561 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100562 trace_kvm_hv_stimer_callback(hv_stimer_to_vcpu(stimer)->vcpu_id,
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300563 stimer->index);
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300564 stimer_mark_pending(stimer, true);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300565
566 return HRTIMER_NORESTART;
567}
568
Andrey Smetaninf8084952015-12-28 18:27:20 +0300569/*
570 * stimer_start() assumptions:
571 * a) stimer->count is not equal to 0
572 * b) stimer->config has HV_STIMER_ENABLE flag
573 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300574static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
575{
576 u64 time_now;
577 ktime_t ktime_now;
578
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100579 time_now = get_time_ref_counter(hv_stimer_to_vcpu(stimer)->kvm);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300580 ktime_now = ktime_get();
581
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100582 if (stimer->config.periodic) {
Andrey Smetaninf8084952015-12-28 18:27:20 +0300583 if (stimer->exp_time) {
584 if (time_now >= stimer->exp_time) {
585 u64 remainder;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300586
Andrey Smetaninf8084952015-12-28 18:27:20 +0300587 div64_u64_rem(time_now - stimer->exp_time,
588 stimer->count, &remainder);
589 stimer->exp_time =
590 time_now + (stimer->count - remainder);
591 }
592 } else
593 stimer->exp_time = time_now + stimer->count;
594
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300595 trace_kvm_hv_stimer_start_periodic(
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100596 hv_stimer_to_vcpu(stimer)->vcpu_id,
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300597 stimer->index,
598 time_now, stimer->exp_time);
599
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300600 hrtimer_start(&stimer->timer,
Andrey Smetaninf8084952015-12-28 18:27:20 +0300601 ktime_add_ns(ktime_now,
602 100 * (stimer->exp_time - time_now)),
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300603 HRTIMER_MODE_ABS);
604 return 0;
605 }
606 stimer->exp_time = stimer->count;
607 if (time_now >= stimer->count) {
608 /*
609 * Expire timer according to Hypervisor Top-Level Functional
610 * specification v4(15.3.1):
611 * "If a one shot is enabled and the specified count is in
612 * the past, it will expire immediately."
613 */
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300614 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300615 return 0;
616 }
617
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100618 trace_kvm_hv_stimer_start_one_shot(hv_stimer_to_vcpu(stimer)->vcpu_id,
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300619 stimer->index,
620 time_now, stimer->count);
621
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300622 hrtimer_start(&stimer->timer,
623 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
624 HRTIMER_MODE_ABS);
625 return 0;
626}
627
628static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
629 bool host)
630{
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100631 union hv_stimer_config new_config = {.as_uint64 = config},
632 old_config = {.as_uint64 = stimer->config.as_uint64};
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100633 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
Vitaly Kuznetsov1aa8a412021-05-21 11:51:53 +0200634 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100635 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
Vitaly Kuznetsovdbcf3f92020-09-24 16:57:52 +0200636
637 if (!synic->active && !host)
638 return 1;
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100639
Vitaly Kuznetsov1aa8a412021-05-21 11:51:53 +0200640 if (unlikely(!host && hv_vcpu->enforce_cpuid && new_config.direct_mode &&
641 !(hv_vcpu->cpuid_cache.features_edx &
642 HV_STIMER_DIRECT_MODE_AVAILABLE)))
643 return 1;
644
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100645 trace_kvm_hv_stimer_set_config(hv_stimer_to_vcpu(stimer)->vcpu_id,
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300646 stimer->index, config, host);
647
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300648 stimer_cleanup(stimer);
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100649 if (old_config.enable &&
650 !new_config.direct_mode && new_config.sintx == 0)
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100651 new_config.enable = 0;
652 stimer->config.as_uint64 = new_config.as_uint64;
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100653
Vitaly Kuznetsov013cc6e2019-03-13 18:13:42 +0100654 if (stimer->config.enable)
655 stimer_mark_pending(stimer, false);
656
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300657 return 0;
658}
659
660static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
661 bool host)
662{
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100663 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100664 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
Vitaly Kuznetsovdbcf3f92020-09-24 16:57:52 +0200665
666 if (!synic->active && !host)
667 return 1;
668
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100669 trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id,
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300670 stimer->index, count, host);
671
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300672 stimer_cleanup(stimer);
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300673 stimer->count = count;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300674 if (stimer->count == 0)
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100675 stimer->config.enable = 0;
676 else if (stimer->config.auto_enable)
677 stimer->config.enable = 1;
Vitaly Kuznetsov013cc6e2019-03-13 18:13:42 +0100678
679 if (stimer->config.enable)
680 stimer_mark_pending(stimer, false);
681
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300682 return 0;
683}
684
685static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
686{
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100687 *pconfig = stimer->config.as_uint64;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300688 return 0;
689}
690
691static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
692{
693 *pcount = stimer->count;
694 return 0;
695}
696
697static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
Roman Kagan7deec5e2018-12-10 18:47:27 +0000698 struct hv_message *src_msg, bool no_retry)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300699{
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100700 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
Roman Kagan3a0e7732018-12-10 18:47:26 +0000701 int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
702 gfn_t msg_page_gfn;
703 struct hv_message_header hv_hdr;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300704 int r;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300705
706 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
707 return -ENOENT;
708
Roman Kagan3a0e7732018-12-10 18:47:26 +0000709 msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300710
Roman Kagan3a0e7732018-12-10 18:47:26 +0000711 /*
712 * Strictly following the spec-mandated ordering would assume setting
713 * .msg_pending before checking .message_type. However, this function
714 * is only called in vcpu context so the entire update is atomic from
715 * guest POV and thus the exact order here doesn't matter.
716 */
717 r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
718 msg_off + offsetof(struct hv_message,
719 header.message_type),
720 sizeof(hv_hdr.message_type));
721 if (r < 0)
722 return r;
723
724 if (hv_hdr.message_type != HVMSG_NONE) {
Roman Kagan7deec5e2018-12-10 18:47:27 +0000725 if (no_retry)
726 return 0;
727
Roman Kagan3a0e7732018-12-10 18:47:26 +0000728 hv_hdr.message_flags.msg_pending = 1;
729 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
730 &hv_hdr.message_flags,
731 msg_off +
732 offsetof(struct hv_message,
733 header.message_flags),
734 sizeof(hv_hdr.message_flags));
735 if (r < 0)
736 return r;
737 return -EAGAIN;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300738 }
Roman Kagan3a0e7732018-12-10 18:47:26 +0000739
740 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
741 sizeof(src_msg->header) +
742 src_msg->header.payload_size);
743 if (r < 0)
744 return r;
745
746 r = synic_set_irq(synic, sint);
747 if (r < 0)
748 return r;
749 if (r == 0)
750 return -EFAULT;
751 return 0;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300752}
753
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300754static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300755{
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100756 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300757 struct hv_message *msg = &stimer->msg;
758 struct hv_timer_message_payload *payload =
759 (struct hv_timer_message_payload *)&msg->u.payload;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300760
Roman Kagan7deec5e2018-12-10 18:47:27 +0000761 /*
762 * To avoid piling up periodic ticks, don't retry message
763 * delivery for them (within "lazy" lost ticks policy).
764 */
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100765 bool no_retry = stimer->config.periodic;
Roman Kagan7deec5e2018-12-10 18:47:27 +0000766
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300767 payload->expiration_time = stimer->exp_time;
768 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +0100769 return synic_deliver_msg(to_hv_synic(vcpu),
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100770 stimer->config.sintx, msg,
Roman Kagan7deec5e2018-12-10 18:47:27 +0000771 no_retry);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300772}
773
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100774static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
775{
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100776 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100777 struct kvm_lapic_irq irq = {
778 .delivery_mode = APIC_DM_FIXED,
779 .vector = stimer->config.apic_vector
780 };
781
Wanpeng Lia073d7e2019-09-16 15:42:32 +0800782 if (lapic_in_kernel(vcpu))
783 return !kvm_apic_set_irq(vcpu, &irq, NULL);
784 return 0;
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100785}
786
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300787static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
788{
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100789 int r, direct = stimer->config.direct_mode;
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300790
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300791 stimer->msg_pending = true;
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100792 if (!direct)
793 r = stimer_send_msg(stimer);
794 else
795 r = stimer_notify_direct(stimer);
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +0100796 trace_kvm_hv_stimer_expiration(hv_stimer_to_vcpu(stimer)->vcpu_id,
Vitaly Kuznetsov8644f772018-11-26 16:47:31 +0100797 stimer->index, direct, r);
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300798 if (!r) {
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300799 stimer->msg_pending = false;
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100800 if (!(stimer->config.periodic))
801 stimer->config.enable = 0;
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300802 }
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300803}
804
805void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
806{
Vitaly Kuznetsovef3f3982021-01-26 14:48:05 +0100807 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300808 struct kvm_vcpu_hv_stimer *stimer;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300809 u64 time_now, exp_time;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300810 int i;
811
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +0100812 if (!hv_vcpu)
813 return;
814
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300815 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
816 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
817 stimer = &hv_vcpu->stimer[i];
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100818 if (stimer->config.enable) {
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300819 exp_time = stimer->exp_time;
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300820
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300821 if (exp_time) {
822 time_now =
823 get_time_ref_counter(vcpu->kvm);
824 if (time_now >= exp_time)
825 stimer_expiration(stimer);
826 }
827
Vitaly Kuznetsov6a058a1e2018-11-26 16:47:30 +0100828 if ((stimer->config.enable) &&
Roman Kaganf1ff89e2017-07-20 17:26:40 +0300829 stimer->count) {
830 if (!stimer->msg_pending)
831 stimer_start(stimer);
832 } else
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300833 stimer_cleanup(stimer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300834 }
835 }
836}
837
838void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
839{
Vitaly Kuznetsovef3f3982021-01-26 14:48:05 +0100840 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300841 int i;
842
Vitaly Kuznetsovfc08b622021-01-26 14:48:15 +0100843 if (!hv_vcpu)
844 return;
845
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300846 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
847 stimer_cleanup(&hv_vcpu->stimer[i]);
Vitaly Kuznetsov4592b7e2021-01-26 14:48:13 +0100848
849 kfree(hv_vcpu);
850 vcpu->arch.hyperv = NULL;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300851}
852
Ladi Prosek72bbf932018-10-16 18:49:59 +0200853bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
854{
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +0100855 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
856
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +0100857 if (!hv_vcpu)
858 return false;
859
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +0100860 if (!(hv_vcpu->hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
Ladi Prosek72bbf932018-10-16 18:49:59 +0200861 return false;
862 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
863}
864EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled);
865
866bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu,
867 struct hv_vp_assist_page *assist_page)
868{
869 if (!kvm_hv_assist_page_enabled(vcpu))
870 return false;
871 return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
872 assist_page, sizeof(*assist_page));
873}
874EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page);
875
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300876static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
877{
878 struct hv_message *msg = &stimer->msg;
879 struct hv_timer_message_payload *payload =
880 (struct hv_timer_message_payload *)&msg->u.payload;
881
882 memset(&msg->header, 0, sizeof(msg->header));
883 msg->header.message_type = HVMSG_TIMER_EXPIRED;
884 msg->header.payload_size = sizeof(*payload);
885
886 payload->timer_index = stimer->index;
887 payload->expiration_time = 0;
888 payload->delivery_time = 0;
889}
890
891static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
892{
893 memset(stimer, 0, sizeof(*stimer));
894 stimer->index = timer_index;
895 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
896 stimer->timer.function = stimer_timer_callback;
897 stimer_prepare_msg(stimer);
898}
899
Vitaly Kuznetsovfc08b622021-01-26 14:48:15 +0100900static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300901{
Vitaly Kuznetsov4592b7e2021-01-26 14:48:13 +0100902 struct kvm_vcpu_hv *hv_vcpu;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300903 int i;
904
Vitaly Kuznetsov4592b7e2021-01-26 14:48:13 +0100905 hv_vcpu = kzalloc(sizeof(struct kvm_vcpu_hv), GFP_KERNEL_ACCOUNT);
906 if (!hv_vcpu)
907 return -ENOMEM;
908
909 vcpu->arch.hyperv = hv_vcpu;
910 hv_vcpu->vcpu = vcpu;
911
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300912 synic_init(&hv_vcpu->synic);
913
914 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
915 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
916 stimer_init(&hv_vcpu->stimer[i], i);
Vitaly Kuznetsov4592b7e2021-01-26 14:48:13 +0100917
Roman Kagand3457c82017-07-14 17:13:20 +0300918 hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
Vitaly Kuznetsovfc08b622021-01-26 14:48:15 +0100919
920 return 0;
Roman Kagand3457c82017-07-14 17:13:20 +0300921}
922
Roman Kaganefc479e2017-06-22 16:51:01 +0300923int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300924{
Vitaly Kuznetsovfc08b622021-01-26 14:48:15 +0100925 struct kvm_vcpu_hv_synic *synic;
926 int r;
927
928 if (!to_hv_vcpu(vcpu)) {
929 r = kvm_hv_vcpu_init(vcpu);
930 if (r)
931 return r;
932 }
933
934 synic = to_hv_synic(vcpu);
Roman Kaganefc479e2017-06-22 16:51:01 +0300935
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300936 /*
937 * Hyper-V SynIC auto EOI SINT's are
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -0600938 * not compatible with APICV, so request
939 * to deactivate APICV permanently.
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300940 */
Suravee Suthikulpanitf4fdc0a2019-11-14 14:15:13 -0600941 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_HYPERV);
Roman Kaganefc479e2017-06-22 16:51:01 +0300942 synic->active = true;
943 synic->dont_zero_synic_pages = dont_zero_synic_pages;
Jon Doron99b48ec2020-07-17 15:52:38 +0300944 synic->control = HV_SYNIC_CONTROL_ENABLE;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300945 return 0;
946}
947
Andrey Smetanine83d5882015-07-03 15:01:34 +0300948static bool kvm_hv_msr_partition_wide(u32 msr)
949{
950 bool r = false;
951
952 switch (msr) {
953 case HV_X64_MSR_GUEST_OS_ID:
954 case HV_X64_MSR_HYPERCALL:
955 case HV_X64_MSR_REFERENCE_TSC:
956 case HV_X64_MSR_TIME_REF_COUNT:
Andrey Smetanine7d95132015-07-03 15:01:37 +0300957 case HV_X64_MSR_CRASH_CTL:
958 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300959 case HV_X64_MSR_RESET:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +0100960 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
961 case HV_X64_MSR_TSC_EMULATION_CONTROL:
962 case HV_X64_MSR_TSC_EMULATION_STATUS:
Jon Doronf97f5a52020-05-29 16:45:40 +0300963 case HV_X64_MSR_SYNDBG_OPTIONS:
964 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
Andrey Smetanine83d5882015-07-03 15:01:34 +0300965 r = true;
966 break;
967 }
968
969 return r;
970}
971
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100972static int kvm_hv_msr_get_crash_data(struct kvm *kvm, u32 index, u64 *pdata)
Andrey Smetanine7d95132015-07-03 15:01:37 +0300973{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100974 struct kvm_hv *hv = to_kvm_hv(kvm);
Marios Pomonis86187932019-12-11 12:47:42 -0800975 size_t size = ARRAY_SIZE(hv->hv_crash_param);
Andrey Smetanine7d95132015-07-03 15:01:37 +0300976
Marios Pomonis86187932019-12-11 12:47:42 -0800977 if (WARN_ON_ONCE(index >= size))
Andrey Smetanine7d95132015-07-03 15:01:37 +0300978 return -EINVAL;
979
Marios Pomonis86187932019-12-11 12:47:42 -0800980 *pdata = hv->hv_crash_param[array_index_nospec(index, size)];
Andrey Smetanine7d95132015-07-03 15:01:37 +0300981 return 0;
982}
983
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100984static int kvm_hv_msr_get_crash_ctl(struct kvm *kvm, u64 *pdata)
Andrey Smetanine7d95132015-07-03 15:01:37 +0300985{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100986 struct kvm_hv *hv = to_kvm_hv(kvm);
Andrey Smetanine7d95132015-07-03 15:01:37 +0300987
988 *pdata = hv->hv_crash_ctl;
989 return 0;
990}
991
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100992static int kvm_hv_msr_set_crash_ctl(struct kvm *kvm, u64 data)
Andrey Smetanine7d95132015-07-03 15:01:37 +0300993{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100994 struct kvm_hv *hv = to_kvm_hv(kvm);
Andrey Smetanine7d95132015-07-03 15:01:37 +0300995
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +0100996 hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
Andrey Smetanine7d95132015-07-03 15:01:37 +0300997
998 return 0;
999}
1000
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001001static int kvm_hv_msr_set_crash_data(struct kvm *kvm, u32 index, u64 data)
Andrey Smetanine7d95132015-07-03 15:01:37 +03001002{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001003 struct kvm_hv *hv = to_kvm_hv(kvm);
Marios Pomonis86187932019-12-11 12:47:42 -08001004 size_t size = ARRAY_SIZE(hv->hv_crash_param);
Andrey Smetanine7d95132015-07-03 15:01:37 +03001005
Marios Pomonis86187932019-12-11 12:47:42 -08001006 if (WARN_ON_ONCE(index >= size))
Andrey Smetanine7d95132015-07-03 15:01:37 +03001007 return -EINVAL;
1008
Marios Pomonis86187932019-12-11 12:47:42 -08001009 hv->hv_crash_param[array_index_nospec(index, size)] = data;
Andrey Smetanine7d95132015-07-03 15:01:37 +03001010 return 0;
1011}
1012
Paolo Bonzini095cf552016-02-08 12:54:12 +01001013/*
1014 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
1015 * between them is possible:
1016 *
1017 * kvmclock formula:
1018 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
1019 * + system_time
1020 *
1021 * Hyper-V formula:
1022 * nsec/100 = ticks * scale / 2^64 + offset
1023 *
1024 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
1025 * By dividing the kvmclock formula by 100 and equating what's left we get:
1026 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1027 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
1028 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
1029 *
1030 * Now expand the kvmclock formula and divide by 100:
1031 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
1032 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
1033 * + system_time
1034 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1035 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1036 * + system_time / 100
1037 *
1038 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
1039 * nsec/100 = ticks * scale / 2^64
1040 * - tsc_timestamp * scale / 2^64
1041 * + system_time / 100
1042 *
1043 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
1044 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
1045 *
1046 * These two equivalencies are implemented in this function.
1047 */
1048static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
Michael Kelley7357b1d2020-04-22 12:57:34 -07001049 struct ms_hyperv_tsc_page *tsc_ref)
Paolo Bonzini095cf552016-02-08 12:54:12 +01001050{
1051 u64 max_mul;
1052
1053 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
1054 return false;
1055
1056 /*
1057 * check if scale would overflow, if so we use the time ref counter
1058 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
1059 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
1060 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
1061 */
1062 max_mul = 100ull << (32 - hv_clock->tsc_shift);
1063 if (hv_clock->tsc_to_system_mul >= max_mul)
1064 return false;
1065
1066 /*
1067 * Otherwise compute the scale and offset according to the formulas
1068 * derived above.
1069 */
1070 tsc_ref->tsc_scale =
1071 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
1072 hv_clock->tsc_to_system_mul,
1073 100);
1074
1075 tsc_ref->tsc_offset = hv_clock->system_time;
1076 do_div(tsc_ref->tsc_offset, 100);
1077 tsc_ref->tsc_offset -=
1078 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
1079 return true;
1080}
1081
Vitaly Kuznetsov0469f2f2021-03-16 15:37:36 +01001082/*
1083 * Don't touch TSC page values if the guest has opted for TSC emulation after
1084 * migration. KVM doesn't fully support reenlightenment notifications and TSC
1085 * access emulation and Hyper-V is known to expect the values in TSC page to
1086 * stay constant before TSC access emulation is disabled from guest side
1087 * (HV_X64_MSR_TSC_EMULATION_STATUS). KVM userspace is expected to preserve TSC
1088 * frequency and guest visible TSC value across migration (and prevent it when
1089 * TSC scaling is unsupported).
1090 */
1091static inline bool tsc_page_update_unsafe(struct kvm_hv *hv)
1092{
1093 return (hv->hv_tsc_page_status != HV_TSC_PAGE_GUEST_CHANGED) &&
1094 hv->hv_tsc_emulation_control;
1095}
1096
Paolo Bonzini095cf552016-02-08 12:54:12 +01001097void kvm_hv_setup_tsc_page(struct kvm *kvm,
1098 struct pvclock_vcpu_time_info *hv_clock)
1099{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001100 struct kvm_hv *hv = to_kvm_hv(kvm);
Paolo Bonzini095cf552016-02-08 12:54:12 +01001101 u32 tsc_seq;
1102 u64 gfn;
1103
1104 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
Michael Kelley7357b1d2020-04-22 12:57:34 -07001105 BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0);
Paolo Bonzini095cf552016-02-08 12:54:12 +01001106
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001107 if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN ||
1108 hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET)
Paolo Bonzini095cf552016-02-08 12:54:12 +01001109 return;
1110
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001111 mutex_lock(&hv->hv_lock);
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001112 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1113 goto out_unlock;
1114
Paolo Bonzini095cf552016-02-08 12:54:12 +01001115 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
1116 /*
1117 * Because the TSC parameters only vary when there is a
1118 * change in the master clock, do not bother with caching.
1119 */
1120 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
1121 &tsc_seq, sizeof(tsc_seq))))
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001122 goto out_err;
Paolo Bonzini095cf552016-02-08 12:54:12 +01001123
Vitaly Kuznetsov0469f2f2021-03-16 15:37:36 +01001124 if (tsc_seq && tsc_page_update_unsafe(hv)) {
1125 if (kvm_read_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
1126 goto out_err;
1127
1128 hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
1129 goto out_unlock;
1130 }
1131
Paolo Bonzini095cf552016-02-08 12:54:12 +01001132 /*
1133 * While we're computing and writing the parameters, force the
1134 * guest to use the time reference count MSR.
1135 */
1136 hv->tsc_ref.tsc_sequence = 0;
1137 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1138 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001139 goto out_err;
Paolo Bonzini095cf552016-02-08 12:54:12 +01001140
1141 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001142 goto out_err;
Paolo Bonzini095cf552016-02-08 12:54:12 +01001143
1144 /* Ensure sequence is zero before writing the rest of the struct. */
1145 smp_wmb();
1146 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001147 goto out_err;
Paolo Bonzini095cf552016-02-08 12:54:12 +01001148
1149 /*
1150 * Now switch to the TSC page mechanism by writing the sequence.
1151 */
1152 tsc_seq++;
1153 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
1154 tsc_seq = 1;
1155
1156 /* Write the struct entirely before the non-zero sequence. */
1157 smp_wmb();
1158
1159 hv->tsc_ref.tsc_sequence = tsc_seq;
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001160 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1161 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
1162 goto out_err;
1163
1164 hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
1165 goto out_unlock;
1166
1167out_err:
1168 hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN;
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001169out_unlock:
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001170 mutex_unlock(&hv->hv_lock);
Paolo Bonzini095cf552016-02-08 12:54:12 +01001171}
1172
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01001173void kvm_hv_invalidate_tsc_page(struct kvm *kvm)
1174{
1175 struct kvm_hv *hv = to_kvm_hv(kvm);
1176 u64 gfn;
Wanpeng Lida6d63a2021-05-18 05:00:34 -07001177 int idx;
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01001178
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001179 if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN ||
Vitaly Kuznetsov0469f2f2021-03-16 15:37:36 +01001180 hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET ||
1181 tsc_page_update_unsafe(hv))
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01001182 return;
1183
1184 mutex_lock(&hv->hv_lock);
1185
1186 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1187 goto out_unlock;
1188
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001189 /* Preserve HV_TSC_PAGE_GUEST_CHANGED/HV_TSC_PAGE_HOST_CHANGED states */
1190 if (hv->hv_tsc_page_status == HV_TSC_PAGE_SET)
1191 hv->hv_tsc_page_status = HV_TSC_PAGE_UPDATING;
1192
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01001193 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
1194
1195 hv->tsc_ref.tsc_sequence = 0;
Wanpeng Lida6d63a2021-05-18 05:00:34 -07001196
1197 /*
1198 * Take the srcu lock as memslots will be accessed to check the gfn
1199 * cache generation against the memslots generation.
1200 */
1201 idx = srcu_read_lock(&kvm->srcu);
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001202 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1203 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
1204 hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN;
Wanpeng Lida6d63a2021-05-18 05:00:34 -07001205 srcu_read_unlock(&kvm->srcu, idx);
Vitaly Kuznetsove880c6e2021-03-16 15:37:34 +01001206
1207out_unlock:
1208 mutex_unlock(&hv->hv_lock);
1209}
1210
Vitaly Kuznetsovb4128002021-05-21 11:51:38 +02001211
1212static bool hv_check_msr_access(struct kvm_vcpu_hv *hv_vcpu, u32 msr)
1213{
Vitaly Kuznetsov1561c2c2021-05-21 11:51:39 +02001214 if (!hv_vcpu->enforce_cpuid)
1215 return true;
1216
1217 switch (msr) {
1218 case HV_X64_MSR_GUEST_OS_ID:
1219 case HV_X64_MSR_HYPERCALL:
1220 return hv_vcpu->cpuid_cache.features_eax &
1221 HV_MSR_HYPERCALL_AVAILABLE;
Vitaly Kuznetsovb80a92f2021-05-21 11:51:40 +02001222 case HV_X64_MSR_VP_RUNTIME:
1223 return hv_vcpu->cpuid_cache.features_eax &
1224 HV_MSR_VP_RUNTIME_AVAILABLE;
Vitaly Kuznetsovc2b32862021-05-21 11:51:41 +02001225 case HV_X64_MSR_TIME_REF_COUNT:
1226 return hv_vcpu->cpuid_cache.features_eax &
1227 HV_MSR_TIME_REF_COUNT_AVAILABLE;
Vitaly Kuznetsovd2ac25d2021-05-21 11:51:42 +02001228 case HV_X64_MSR_VP_INDEX:
1229 return hv_vcpu->cpuid_cache.features_eax &
1230 HV_MSR_VP_INDEX_AVAILABLE;
Vitaly Kuznetsov679008e2021-05-21 11:51:43 +02001231 case HV_X64_MSR_RESET:
1232 return hv_vcpu->cpuid_cache.features_eax &
1233 HV_MSR_RESET_AVAILABLE;
Vitaly Kuznetsova1ec6612021-05-21 11:51:44 +02001234 case HV_X64_MSR_REFERENCE_TSC:
1235 return hv_vcpu->cpuid_cache.features_eax &
1236 HV_MSR_REFERENCE_TSC_AVAILABLE;
Vitaly Kuznetsov9e2715c2021-05-21 11:51:45 +02001237 case HV_X64_MSR_SCONTROL:
1238 case HV_X64_MSR_SVERSION:
1239 case HV_X64_MSR_SIEFP:
1240 case HV_X64_MSR_SIMP:
1241 case HV_X64_MSR_EOM:
1242 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1243 return hv_vcpu->cpuid_cache.features_eax &
1244 HV_MSR_SYNIC_AVAILABLE;
Vitaly Kuznetsoveba60dd2021-05-21 11:51:46 +02001245 case HV_X64_MSR_STIMER0_CONFIG:
1246 case HV_X64_MSR_STIMER1_CONFIG:
1247 case HV_X64_MSR_STIMER2_CONFIG:
1248 case HV_X64_MSR_STIMER3_CONFIG:
1249 case HV_X64_MSR_STIMER0_COUNT:
1250 case HV_X64_MSR_STIMER1_COUNT:
1251 case HV_X64_MSR_STIMER2_COUNT:
1252 case HV_X64_MSR_STIMER3_COUNT:
1253 return hv_vcpu->cpuid_cache.features_eax &
1254 HV_MSR_SYNTIMER_AVAILABLE;
Vitaly Kuznetsov978b5742021-05-21 11:51:47 +02001255 case HV_X64_MSR_EOI:
1256 case HV_X64_MSR_ICR:
1257 case HV_X64_MSR_TPR:
1258 case HV_X64_MSR_VP_ASSIST_PAGE:
1259 return hv_vcpu->cpuid_cache.features_eax &
1260 HV_MSR_APIC_ACCESS_AVAILABLE;
1261 break;
Vitaly Kuznetsov9442f3b2021-05-21 11:51:48 +02001262 case HV_X64_MSR_TSC_FREQUENCY:
1263 case HV_X64_MSR_APIC_FREQUENCY:
1264 return hv_vcpu->cpuid_cache.features_eax &
1265 HV_ACCESS_FREQUENCY_MSRS;
Vitaly Kuznetsov234d01b2021-05-21 11:51:49 +02001266 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1267 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1268 case HV_X64_MSR_TSC_EMULATION_STATUS:
1269 return hv_vcpu->cpuid_cache.features_eax &
1270 HV_ACCESS_REENLIGHTENMENT;
Vitaly Kuznetsov0a19c892021-05-21 11:51:50 +02001271 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1272 case HV_X64_MSR_CRASH_CTL:
1273 return hv_vcpu->cpuid_cache.features_edx &
1274 HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
Vitaly Kuznetsov17b6d512021-05-21 11:51:51 +02001275 case HV_X64_MSR_SYNDBG_OPTIONS:
1276 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1277 return hv_vcpu->cpuid_cache.features_edx &
1278 HV_FEATURE_DEBUG_MSRS_AVAILABLE;
Vitaly Kuznetsov1561c2c2021-05-21 11:51:39 +02001279 default:
1280 break;
1281 }
1282
Vitaly Kuznetsovd66bfa32021-05-21 11:51:52 +02001283 return false;
Vitaly Kuznetsovb4128002021-05-21 11:51:38 +02001284}
1285
Andrey Smetanine7d95132015-07-03 15:01:37 +03001286static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
1287 bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001288{
1289 struct kvm *kvm = vcpu->kvm;
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001290 struct kvm_hv *hv = to_kvm_hv(kvm);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001291
Vitaly Kuznetsovb4128002021-05-21 11:51:38 +02001292 if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
1293 return 1;
1294
Andrey Smetanine83d5882015-07-03 15:01:34 +03001295 switch (msr) {
1296 case HV_X64_MSR_GUEST_OS_ID:
1297 hv->hv_guest_os_id = data;
1298 /* setting guest os id to zero disables hypercall page */
1299 if (!hv->hv_guest_os_id)
1300 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1301 break;
1302 case HV_X64_MSR_HYPERCALL: {
Joao Martins79033be2018-06-13 09:55:44 -04001303 u8 instructions[9];
1304 int i = 0;
1305 u64 addr;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001306
1307 /* if guest os id is not set hypercall should remain disabled */
1308 if (!hv->hv_guest_os_id)
1309 break;
1310 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1311 hv->hv_hypercall = data;
1312 break;
1313 }
Joao Martins79033be2018-06-13 09:55:44 -04001314
1315 /*
1316 * If Xen and Hyper-V hypercalls are both enabled, disambiguate
1317 * the same way Xen itself does, by setting the bit 31 of EAX
1318 * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just
1319 * going to be clobbered on 64-bit.
1320 */
1321 if (kvm_xen_hypercall_enabled(kvm)) {
1322 /* orl $0x80000000, %eax */
1323 instructions[i++] = 0x0d;
1324 instructions[i++] = 0x00;
1325 instructions[i++] = 0x00;
1326 instructions[i++] = 0x00;
1327 instructions[i++] = 0x80;
1328 }
1329
1330 /* vmcall/vmmcall */
1331 static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i);
1332 i += 3;
1333
1334 /* ret */
1335 ((unsigned char *)instructions)[i++] = 0xc3;
1336
1337 addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK;
1338 if (kvm_vcpu_write_guest(vcpu, addr, instructions, i))
Andrey Smetanine83d5882015-07-03 15:01:34 +03001339 return 1;
1340 hv->hv_hypercall = data;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001341 break;
1342 }
Paolo Bonzini095cf552016-02-08 12:54:12 +01001343 case HV_X64_MSR_REFERENCE_TSC:
Andrey Smetanine83d5882015-07-03 15:01:34 +03001344 hv->hv_tsc_page = data;
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001345 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) {
1346 if (!host)
1347 hv->hv_tsc_page_status = HV_TSC_PAGE_GUEST_CHANGED;
1348 else
1349 hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED;
Paolo Bonzini095cf552016-02-08 12:54:12 +01001350 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Vitaly Kuznetsovcc9cfdd2021-03-16 15:37:35 +01001351 } else {
1352 hv->hv_tsc_page_status = HV_TSC_PAGE_UNSET;
1353 }
Andrey Smetanine83d5882015-07-03 15:01:34 +03001354 break;
Andrey Smetanine7d95132015-07-03 15:01:37 +03001355 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001356 return kvm_hv_msr_set_crash_data(kvm,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001357 msr - HV_X64_MSR_CRASH_P0,
1358 data);
1359 case HV_X64_MSR_CRASH_CTL:
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001360 if (host)
1361 return kvm_hv_msr_set_crash_ctl(kvm, data);
1362
1363 if (data & HV_CRASH_CTL_CRASH_NOTIFY) {
1364 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
1365 hv->hv_crash_param[0],
1366 hv->hv_crash_param[1],
1367 hv->hv_crash_param[2],
1368 hv->hv_crash_param[3],
1369 hv->hv_crash_param[4]);
1370
1371 /* Send notification about crash to user space */
1372 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
1373 }
1374 break;
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001375 case HV_X64_MSR_RESET:
1376 if (data == 1) {
1377 vcpu_debug(vcpu, "hyper-v reset requested\n");
1378 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
1379 }
1380 break;
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001381 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1382 hv->hv_reenlightenment_control = data;
1383 break;
1384 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1385 hv->hv_tsc_emulation_control = data;
1386 break;
1387 case HV_X64_MSR_TSC_EMULATION_STATUS:
Vitaly Kuznetsovd2547cf2021-03-16 15:37:33 +01001388 if (data && !host)
1389 return 1;
1390
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001391 hv->hv_tsc_emulation_status = data;
1392 break;
Paolo Bonzini44883f02018-07-26 13:01:52 +02001393 case HV_X64_MSR_TIME_REF_COUNT:
1394 /* read-only, but still ignore it if host-initiated */
1395 if (!host)
1396 return 1;
1397 break;
Jon Doronf97f5a52020-05-29 16:45:40 +03001398 case HV_X64_MSR_SYNDBG_OPTIONS:
1399 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1400 return syndbg_set_msr(vcpu, msr, data, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001401 default:
Miaohe Lin2f9f5cd2019-12-11 14:26:24 +08001402 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
Andrey Smetanine83d5882015-07-03 15:01:34 +03001403 msr, data);
1404 return 1;
1405 }
1406 return 0;
1407}
1408
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001409/* Calculate cpu time spent by current task in 100ns units */
1410static u64 current_task_runtime_100ns(void)
1411{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001412 u64 utime, stime;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001413
1414 task_cputime_adjusted(current, &utime, &stime);
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001415
1416 return div_u64(utime + stime, 100);
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001417}
1418
1419static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001420{
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01001421 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001422
Vitaly Kuznetsovb4128002021-05-21 11:51:38 +02001423 if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
1424 return 1;
1425
Andrey Smetanine83d5882015-07-03 15:01:34 +03001426 switch (msr) {
Vitaly Kuznetsov87ee6132018-09-26 19:02:56 +02001427 case HV_X64_MSR_VP_INDEX: {
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001428 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
Vitaly Kuznetsov87ee6132018-09-26 19:02:56 +02001429 int vcpu_idx = kvm_vcpu_get_idx(vcpu);
1430 u32 new_vp_index = (u32)data;
1431
1432 if (!host || new_vp_index >= KVM_MAX_VCPUS)
Roman Kagand3457c82017-07-14 17:13:20 +03001433 return 1;
Vitaly Kuznetsov87ee6132018-09-26 19:02:56 +02001434
1435 if (new_vp_index == hv_vcpu->vp_index)
1436 return 0;
1437
1438 /*
1439 * The VP index is initialized to vcpu_index by
1440 * kvm_hv_vcpu_postcreate so they initially match. Now the
1441 * VP index is changing, adjust num_mismatched_vp_indexes if
1442 * it now matches or no longer matches vcpu_idx.
1443 */
1444 if (hv_vcpu->vp_index == vcpu_idx)
1445 atomic_inc(&hv->num_mismatched_vp_indexes);
1446 else if (new_vp_index == vcpu_idx)
1447 atomic_dec(&hv->num_mismatched_vp_indexes);
1448
1449 hv_vcpu->vp_index = new_vp_index;
Roman Kagand3457c82017-07-14 17:13:20 +03001450 break;
Vitaly Kuznetsov87ee6132018-09-26 19:02:56 +02001451 }
Ladi Prosekd4abc572018-03-20 15:02:07 +01001452 case HV_X64_MSR_VP_ASSIST_PAGE: {
Andrey Smetanine83d5882015-07-03 15:01:34 +03001453 u64 gfn;
1454 unsigned long addr;
1455
Ladi Prosekd4abc572018-03-20 15:02:07 +01001456 if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
Vitaly Kuznetsov1779a392018-09-26 19:02:55 +02001457 hv_vcpu->hv_vapic = data;
Ladi Prosek72bbf932018-10-16 18:49:59 +02001458 if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0))
Andrey Smetanine83d5882015-07-03 15:01:34 +03001459 return 1;
1460 break;
1461 }
Ladi Prosekd4abc572018-03-20 15:02:07 +01001462 gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001463 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1464 if (kvm_is_error_hva(addr))
1465 return 1;
Vitaly Kuznetsov12e0c612018-10-16 18:50:05 +02001466
1467 /*
Miaohe Lin67b0ae42019-12-11 14:26:22 +08001468 * Clear apic_assist portion of struct hv_vp_assist_page
Vitaly Kuznetsov12e0c612018-10-16 18:50:05 +02001469 * only, there can be valuable data in the rest which needs
1470 * to be preserved e.g. on migration.
1471 */
Al Viro9eb41c52020-02-18 17:32:46 -05001472 if (__put_user(0, (u32 __user *)addr))
Andrey Smetanine83d5882015-07-03 15:01:34 +03001473 return 1;
Vitaly Kuznetsov1779a392018-09-26 19:02:55 +02001474 hv_vcpu->hv_vapic = data;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001475 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1476 if (kvm_lapic_enable_pv_eoi(vcpu,
Ladi Prosek72bbf932018-10-16 18:49:59 +02001477 gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
1478 sizeof(struct hv_vp_assist_page)))
Andrey Smetanine83d5882015-07-03 15:01:34 +03001479 return 1;
1480 break;
1481 }
1482 case HV_X64_MSR_EOI:
1483 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1484 case HV_X64_MSR_ICR:
1485 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1486 case HV_X64_MSR_TPR:
1487 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001488 case HV_X64_MSR_VP_RUNTIME:
1489 if (!host)
1490 return 1;
Vitaly Kuznetsov1779a392018-09-26 19:02:55 +02001491 hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001492 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001493 case HV_X64_MSR_SCONTROL:
1494 case HV_X64_MSR_SVERSION:
1495 case HV_X64_MSR_SIEFP:
1496 case HV_X64_MSR_SIMP:
1497 case HV_X64_MSR_EOM:
1498 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +01001499 return synic_set_msr(to_hv_synic(vcpu), msr, data, host);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001500 case HV_X64_MSR_STIMER0_CONFIG:
1501 case HV_X64_MSR_STIMER1_CONFIG:
1502 case HV_X64_MSR_STIMER2_CONFIG:
1503 case HV_X64_MSR_STIMER3_CONFIG: {
1504 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1505
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +01001506 return stimer_set_config(to_hv_stimer(vcpu, timer_index),
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001507 data, host);
1508 }
1509 case HV_X64_MSR_STIMER0_COUNT:
1510 case HV_X64_MSR_STIMER1_COUNT:
1511 case HV_X64_MSR_STIMER2_COUNT:
1512 case HV_X64_MSR_STIMER3_COUNT: {
1513 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1514
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +01001515 return stimer_set_count(to_hv_stimer(vcpu, timer_index),
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001516 data, host);
1517 }
Paolo Bonzini44883f02018-07-26 13:01:52 +02001518 case HV_X64_MSR_TSC_FREQUENCY:
1519 case HV_X64_MSR_APIC_FREQUENCY:
1520 /* read-only, but still ignore it if host-initiated */
1521 if (!host)
1522 return 1;
1523 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001524 default:
Miaohe Lin2f9f5cd2019-12-11 14:26:24 +08001525 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
Andrey Smetanine83d5882015-07-03 15:01:34 +03001526 msr, data);
1527 return 1;
1528 }
1529
1530 return 0;
1531}
1532
Jon Doronf97f5a52020-05-29 16:45:40 +03001533static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1534 bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001535{
1536 u64 data = 0;
1537 struct kvm *kvm = vcpu->kvm;
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001538 struct kvm_hv *hv = to_kvm_hv(kvm);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001539
Vitaly Kuznetsovb4128002021-05-21 11:51:38 +02001540 if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
1541 return 1;
1542
Andrey Smetanine83d5882015-07-03 15:01:34 +03001543 switch (msr) {
1544 case HV_X64_MSR_GUEST_OS_ID:
1545 data = hv->hv_guest_os_id;
1546 break;
1547 case HV_X64_MSR_HYPERCALL:
1548 data = hv->hv_hypercall;
1549 break;
Andrey Smetanin93bf4172015-11-30 19:22:19 +03001550 case HV_X64_MSR_TIME_REF_COUNT:
1551 data = get_time_ref_counter(kvm);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001552 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001553 case HV_X64_MSR_REFERENCE_TSC:
1554 data = hv->hv_tsc_page;
1555 break;
Andrey Smetanine7d95132015-07-03 15:01:37 +03001556 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001557 return kvm_hv_msr_get_crash_data(kvm,
Andrey Smetanine7d95132015-07-03 15:01:37 +03001558 msr - HV_X64_MSR_CRASH_P0,
1559 pdata);
1560 case HV_X64_MSR_CRASH_CTL:
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001561 return kvm_hv_msr_get_crash_ctl(kvm, pdata);
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001562 case HV_X64_MSR_RESET:
1563 data = 0;
1564 break;
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001565 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1566 data = hv->hv_reenlightenment_control;
1567 break;
1568 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1569 data = hv->hv_tsc_emulation_control;
1570 break;
1571 case HV_X64_MSR_TSC_EMULATION_STATUS:
1572 data = hv->hv_tsc_emulation_status;
1573 break;
Jon Doronf97f5a52020-05-29 16:45:40 +03001574 case HV_X64_MSR_SYNDBG_OPTIONS:
1575 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1576 return syndbg_get_msr(vcpu, msr, pdata, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001577 default:
1578 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1579 return 1;
1580 }
1581
1582 *pdata = data;
1583 return 0;
1584}
1585
Paolo Bonzini44883f02018-07-26 13:01:52 +02001586static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1587 bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001588{
1589 u64 data = 0;
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01001590 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001591
Vitaly Kuznetsovb4128002021-05-21 11:51:38 +02001592 if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
1593 return 1;
1594
Andrey Smetanine83d5882015-07-03 15:01:34 +03001595 switch (msr) {
Roman Kagand3457c82017-07-14 17:13:20 +03001596 case HV_X64_MSR_VP_INDEX:
Vitaly Kuznetsov1779a392018-09-26 19:02:55 +02001597 data = hv_vcpu->vp_index;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001598 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001599 case HV_X64_MSR_EOI:
1600 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1601 case HV_X64_MSR_ICR:
1602 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1603 case HV_X64_MSR_TPR:
1604 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
Ladi Prosekd4abc572018-03-20 15:02:07 +01001605 case HV_X64_MSR_VP_ASSIST_PAGE:
Vitaly Kuznetsov1779a392018-09-26 19:02:55 +02001606 data = hv_vcpu->hv_vapic;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001607 break;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001608 case HV_X64_MSR_VP_RUNTIME:
Vitaly Kuznetsov1779a392018-09-26 19:02:55 +02001609 data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001610 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001611 case HV_X64_MSR_SCONTROL:
1612 case HV_X64_MSR_SVERSION:
1613 case HV_X64_MSR_SIEFP:
1614 case HV_X64_MSR_SIMP:
1615 case HV_X64_MSR_EOM:
1616 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
Vitaly Kuznetsove0121fa2021-01-26 14:48:06 +01001617 return synic_get_msr(to_hv_synic(vcpu), msr, pdata, host);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001618 case HV_X64_MSR_STIMER0_CONFIG:
1619 case HV_X64_MSR_STIMER1_CONFIG:
1620 case HV_X64_MSR_STIMER2_CONFIG:
1621 case HV_X64_MSR_STIMER3_CONFIG: {
1622 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1623
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +01001624 return stimer_get_config(to_hv_stimer(vcpu, timer_index),
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001625 pdata);
1626 }
1627 case HV_X64_MSR_STIMER0_COUNT:
1628 case HV_X64_MSR_STIMER1_COUNT:
1629 case HV_X64_MSR_STIMER2_COUNT:
1630 case HV_X64_MSR_STIMER3_COUNT: {
1631 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1632
Vitaly Kuznetsovaafa97f2021-01-26 14:48:07 +01001633 return stimer_get_count(to_hv_stimer(vcpu, timer_index),
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001634 pdata);
1635 }
Ladi Prosek72c139b2017-07-26 13:32:59 +02001636 case HV_X64_MSR_TSC_FREQUENCY:
1637 data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
1638 break;
1639 case HV_X64_MSR_APIC_FREQUENCY:
1640 data = APIC_BUS_FREQUENCY;
1641 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001642 default:
1643 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1644 return 1;
1645 }
1646 *pdata = data;
1647 return 0;
1648}
1649
Andrey Smetanine7d95132015-07-03 15:01:37 +03001650int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001651{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001652 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1653
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001654 if (!host && !vcpu->arch.hyperv_enabled)
1655 return 1;
1656
Vitaly Kuznetsovfc08b622021-01-26 14:48:15 +01001657 if (!to_hv_vcpu(vcpu)) {
1658 if (kvm_hv_vcpu_init(vcpu))
1659 return 1;
1660 }
1661
Andrey Smetanine83d5882015-07-03 15:01:34 +03001662 if (kvm_hv_msr_partition_wide(msr)) {
1663 int r;
1664
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001665 mutex_lock(&hv->hv_lock);
Andrey Smetanine7d95132015-07-03 15:01:37 +03001666 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001667 mutex_unlock(&hv->hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001668 return r;
1669 } else
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001670 return kvm_hv_set_msr(vcpu, msr, data, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001671}
1672
Paolo Bonzini44883f02018-07-26 13:01:52 +02001673int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001674{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001675 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1676
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001677 if (!host && !vcpu->arch.hyperv_enabled)
1678 return 1;
1679
Vitaly Kuznetsovfc08b622021-01-26 14:48:15 +01001680 if (!to_hv_vcpu(vcpu)) {
1681 if (kvm_hv_vcpu_init(vcpu))
1682 return 1;
1683 }
1684
Andrey Smetanine83d5882015-07-03 15:01:34 +03001685 if (kvm_hv_msr_partition_wide(msr)) {
1686 int r;
1687
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001688 mutex_lock(&hv->hv_lock);
Jon Doronf97f5a52020-05-29 16:45:40 +03001689 r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host);
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001690 mutex_unlock(&hv->hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001691 return r;
1692 } else
Paolo Bonzini44883f02018-07-26 13:01:52 +02001693 return kvm_hv_get_msr(vcpu, msr, pdata, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001694}
1695
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001696static __always_inline unsigned long *sparse_set_to_vcpu_mask(
1697 struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask,
1698 u64 *vp_bitmap, unsigned long *vcpu_bitmap)
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001699{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01001700 struct kvm_hv *hv = to_kvm_hv(kvm);
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001701 struct kvm_vcpu *vcpu;
1702 int i, bank, sbank = 0;
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001703
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001704 memset(vp_bitmap, 0,
1705 KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap));
1706 for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
1707 KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
1708 vp_bitmap[bank] = sparse_banks[sbank++];
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001709
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001710 if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) {
1711 /* for all vcpus vp_index == vcpu_idx */
1712 return (unsigned long *)vp_bitmap;
1713 }
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001714
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001715 bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
1716 kvm_for_each_vcpu(i, vcpu, kvm) {
Vitaly Kuznetsovf2bc14b2021-01-26 14:48:12 +01001717 if (test_bit(kvm_hv_get_vpindex(vcpu), (unsigned long *)vp_bitmap))
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001718 __set_bit(i, vcpu_bitmap);
1719 }
1720 return vcpu_bitmap;
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001721}
1722
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001723struct kvm_hv_hcall {
1724 u64 param;
1725 u64 ingpa;
1726 u64 outgpa;
1727 u16 code;
1728 u16 rep_cnt;
1729 u16 rep_idx;
1730 bool fast;
1731 bool rep;
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02001732 sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS];
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001733};
1734
1735static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex)
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001736{
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02001737 int i;
1738 gpa_t gpa;
Vitaly Kuznetsov72167a92021-01-26 14:48:10 +01001739 struct kvm *kvm = vcpu->kvm;
Vitaly Kuznetsov9ff5e032021-01-26 14:48:11 +01001740 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001741 struct hv_tlb_flush_ex flush_ex;
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001742 struct hv_tlb_flush flush;
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001743 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1744 DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1745 unsigned long *vcpu_mask;
Vitaly Kuznetsov2cefc5f2018-09-26 19:02:58 +02001746 u64 valid_bank_mask;
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001747 u64 sparse_banks[64];
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001748 int sparse_banks_len;
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001749 bool all_cpus;
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001750
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001751 if (!ex) {
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02001752 if (hc->fast) {
1753 flush.address_space = hc->ingpa;
1754 flush.flags = hc->outgpa;
1755 flush.processor_mask = sse128_lo(hc->xmm[0]);
1756 } else {
1757 if (unlikely(kvm_read_guest(kvm, hc->ingpa,
1758 &flush, sizeof(flush))))
1759 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1760 }
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001761
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001762 trace_kvm_hv_flush_tlb(flush.processor_mask,
1763 flush.address_space, flush.flags);
1764
Vitaly Kuznetsov2cefc5f2018-09-26 19:02:58 +02001765 valid_bank_mask = BIT_ULL(0);
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001766 sparse_banks[0] = flush.processor_mask;
Vitaly Kuznetsovda667612019-03-20 18:43:20 +01001767
1768 /*
1769 * Work around possible WS2012 bug: it sends hypercalls
1770 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
1771 * while also expecting us to flush something and crashing if
1772 * we don't. Let's treat processor_mask == 0 same as
1773 * HV_FLUSH_ALL_PROCESSORS.
1774 */
1775 all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
1776 flush.processor_mask == 0;
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001777 } else {
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02001778 if (hc->fast) {
1779 flush_ex.address_space = hc->ingpa;
1780 flush_ex.flags = hc->outgpa;
1781 memcpy(&flush_ex.hv_vp_set,
1782 &hc->xmm[0], sizeof(hc->xmm[0]));
1783 } else {
1784 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex,
1785 sizeof(flush_ex))))
1786 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1787 }
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001788
1789 trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
1790 flush_ex.hv_vp_set.format,
1791 flush_ex.address_space,
1792 flush_ex.flags);
1793
1794 valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
1795 all_cpus = flush_ex.hv_vp_set.format !=
1796 HV_GENERIC_SET_SPARSE_4K;
1797
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02001798 sparse_banks_len = bitmap_weight((unsigned long *)&valid_bank_mask, 64);
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001799
1800 if (!sparse_banks_len && !all_cpus)
1801 goto ret_success;
1802
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02001803 if (!all_cpus) {
1804 if (hc->fast) {
1805 if (sparse_banks_len > HV_HYPERCALL_MAX_XMM_REGISTERS - 1)
1806 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1807 for (i = 0; i < sparse_banks_len; i += 2) {
1808 sparse_banks[i] = sse128_lo(hc->xmm[i / 2 + 1]);
1809 sparse_banks[i + 1] = sse128_hi(hc->xmm[i / 2 + 1]);
1810 }
1811 } else {
1812 gpa = hc->ingpa + offsetof(struct hv_tlb_flush_ex,
1813 hv_vp_set.bank_contents);
1814 if (unlikely(kvm_read_guest(kvm, gpa, sparse_banks,
1815 sparse_banks_len *
1816 sizeof(sparse_banks[0]))))
1817 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1818 }
1819 }
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001820 }
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001821
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001822 cpumask_clear(&hv_vcpu->tlb_flush);
1823
1824 vcpu_mask = all_cpus ? NULL :
1825 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1826 vp_bitmap, vcpu_bitmap);
1827
Vitaly Kuznetsov2cefc5f2018-09-26 19:02:58 +02001828 /*
1829 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
1830 * analyze it here, flush TLB regardless of the specified address space.
1831 */
Vitaly Kuznetsov0baedd72020-03-25 12:28:24 -04001832 kvm_make_vcpus_request_mask(kvm, KVM_REQ_HV_TLB_FLUSH,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -05001833 NULL, vcpu_mask, &hv_vcpu->tlb_flush);
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001834
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02001835ret_success:
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001836 /* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001837 return (u64)HV_STATUS_SUCCESS |
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001838 ((u64)hc->rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02001839}
1840
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001841static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector,
1842 unsigned long *vcpu_bitmap)
1843{
1844 struct kvm_lapic_irq irq = {
1845 .delivery_mode = APIC_DM_FIXED,
1846 .vector = vector
1847 };
1848 struct kvm_vcpu *vcpu;
1849 int i;
1850
1851 kvm_for_each_vcpu(i, vcpu, kvm) {
1852 if (vcpu_bitmap && !test_bit(i, vcpu_bitmap))
1853 continue;
1854
1855 /* We fail only when APIC is disabled */
1856 kvm_apic_set_irq(vcpu, &irq, NULL);
1857 }
1858}
1859
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001860static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex)
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001861{
Vitaly Kuznetsov72167a92021-01-26 14:48:10 +01001862 struct kvm *kvm = vcpu->kvm;
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001863 struct hv_send_ipi_ex send_ipi_ex;
1864 struct hv_send_ipi send_ipi;
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001865 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1866 DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1867 unsigned long *vcpu_mask;
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001868 unsigned long valid_bank_mask;
1869 u64 sparse_banks[64];
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001870 int sparse_banks_len;
1871 u32 vector;
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001872 bool all_cpus;
1873
1874 if (!ex) {
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001875 if (!hc->fast) {
1876 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi,
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001877 sizeof(send_ipi))))
1878 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1879 sparse_banks[0] = send_ipi.cpu_mask;
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001880 vector = send_ipi.vector;
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001881 } else {
1882 /* 'reserved' part of hv_send_ipi should be 0 */
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001883 if (unlikely(hc->ingpa >> 32 != 0))
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001884 return HV_STATUS_INVALID_HYPERCALL_INPUT;
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001885 sparse_banks[0] = hc->outgpa;
1886 vector = (u32)hc->ingpa;
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001887 }
1888 all_cpus = false;
1889 valid_bank_mask = BIT_ULL(0);
1890
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001891 trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001892 } else {
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001893 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi_ex,
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001894 sizeof(send_ipi_ex))))
1895 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1896
1897 trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
1898 send_ipi_ex.vp_set.format,
1899 send_ipi_ex.vp_set.valid_bank_mask);
1900
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001901 vector = send_ipi_ex.vector;
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001902 valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
1903 sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) *
1904 sizeof(sparse_banks[0]);
1905
1906 all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
1907
1908 if (!sparse_banks_len)
1909 goto ret_success;
1910
1911 if (!all_cpus &&
1912 kvm_read_guest(kvm,
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02001913 hc->ingpa + offsetof(struct hv_send_ipi_ex,
1914 vp_set.bank_contents),
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001915 sparse_banks,
1916 sparse_banks_len))
1917 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1918 }
1919
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001920 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001921 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1922
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001923 vcpu_mask = all_cpus ? NULL :
1924 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1925 vp_bitmap, vcpu_bitmap);
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001926
Vitaly Kuznetsovf21dd492018-10-10 17:14:38 +02001927 kvm_send_ipi_to_many(kvm, vector, vcpu_mask);
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02001928
1929ret_success:
1930 return HV_STATUS_SUCCESS;
1931}
1932
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001933void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001934{
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001935 struct kvm_cpuid_entry2 *entry;
Vitaly Kuznetsov10d7bf12021-05-21 11:51:37 +02001936 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001937
1938 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_INTERFACE, 0);
Vitaly Kuznetsov10d7bf12021-05-21 11:51:37 +02001939 if (entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX) {
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001940 vcpu->arch.hyperv_enabled = true;
Vitaly Kuznetsov10d7bf12021-05-21 11:51:37 +02001941 } else {
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001942 vcpu->arch.hyperv_enabled = false;
Vitaly Kuznetsov10d7bf12021-05-21 11:51:37 +02001943 return;
1944 }
1945
1946 if (!to_hv_vcpu(vcpu) && kvm_hv_vcpu_init(vcpu))
1947 return;
1948
1949 hv_vcpu = to_hv_vcpu(vcpu);
1950
1951 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_FEATURES, 0);
1952 if (entry) {
1953 hv_vcpu->cpuid_cache.features_eax = entry->eax;
1954 hv_vcpu->cpuid_cache.features_ebx = entry->ebx;
1955 hv_vcpu->cpuid_cache.features_edx = entry->edx;
1956 } else {
1957 hv_vcpu->cpuid_cache.features_eax = 0;
1958 hv_vcpu->cpuid_cache.features_ebx = 0;
1959 hv_vcpu->cpuid_cache.features_edx = 0;
1960 }
1961
1962 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_ENLIGHTMENT_INFO, 0);
1963 if (entry) {
1964 hv_vcpu->cpuid_cache.enlightenments_eax = entry->eax;
1965 hv_vcpu->cpuid_cache.enlightenments_ebx = entry->ebx;
1966 } else {
1967 hv_vcpu->cpuid_cache.enlightenments_eax = 0;
1968 hv_vcpu->cpuid_cache.enlightenments_ebx = 0;
1969 }
1970
1971 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES, 0);
1972 if (entry)
1973 hv_vcpu->cpuid_cache.syndbg_cap_eax = entry->eax;
1974 else
1975 hv_vcpu->cpuid_cache.syndbg_cap_eax = 0;
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001976}
1977
Vitaly Kuznetsov644f7062021-05-21 11:51:36 +02001978int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce)
1979{
1980 struct kvm_vcpu_hv *hv_vcpu;
1981 int ret = 0;
1982
1983 if (!to_hv_vcpu(vcpu)) {
1984 if (enforce) {
1985 ret = kvm_hv_vcpu_init(vcpu);
1986 if (ret)
1987 return ret;
1988 } else {
1989 return 0;
1990 }
1991 }
1992
1993 hv_vcpu = to_hv_vcpu(vcpu);
1994 hv_vcpu->enforce_cpuid = enforce;
1995
1996 return ret;
1997}
1998
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01001999bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu)
2000{
2001 return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id;
Andrey Smetanine83d5882015-07-03 15:01:34 +03002002}
2003
Andrey Smetanin83326e42016-02-11 16:45:01 +03002004static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
2005{
2006 bool longmode;
2007
2008 longmode = is_64_bit_mode(vcpu);
2009 if (longmode)
Sean Christophersonde3cd112019-04-30 10:36:17 -07002010 kvm_rax_write(vcpu, result);
Andrey Smetanin83326e42016-02-11 16:45:01 +03002011 else {
Sean Christophersonde3cd112019-04-30 10:36:17 -07002012 kvm_rdx_write(vcpu, result >> 32);
2013 kvm_rax_write(vcpu, result & 0xffffffff);
Andrey Smetanin83326e42016-02-11 16:45:01 +03002014 }
2015}
2016
Radim Krčmář696ca772018-05-24 17:50:56 +02002017static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
2018{
2019 kvm_hv_hypercall_set_result(vcpu, result);
2020 ++vcpu->stat.hypercalls;
2021 return kvm_skip_emulated_instruction(vcpu);
2022}
2023
Andrey Smetanin83326e42016-02-11 16:45:01 +03002024static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
2025{
Radim Krčmář696ca772018-05-24 17:50:56 +02002026 return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
Andrey Smetanin83326e42016-02-11 16:45:01 +03002027}
2028
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002029static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
Roman Kaganfaeb7832018-02-01 16:48:32 +03002030{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01002031 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
Roman Kaganfaeb7832018-02-01 16:48:32 +03002032 struct eventfd_ctx *eventfd;
2033
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002034 if (unlikely(!hc->fast)) {
Roman Kaganfaeb7832018-02-01 16:48:32 +03002035 int ret;
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002036 gpa_t gpa = hc->ingpa;
Roman Kaganfaeb7832018-02-01 16:48:32 +03002037
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002038 if ((gpa & (__alignof__(hc->ingpa) - 1)) ||
2039 offset_in_page(gpa) + sizeof(hc->ingpa) > PAGE_SIZE)
Roman Kaganfaeb7832018-02-01 16:48:32 +03002040 return HV_STATUS_INVALID_ALIGNMENT;
2041
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002042 ret = kvm_vcpu_read_guest(vcpu, gpa,
2043 &hc->ingpa, sizeof(hc->ingpa));
Roman Kaganfaeb7832018-02-01 16:48:32 +03002044 if (ret < 0)
2045 return HV_STATUS_INVALID_ALIGNMENT;
2046 }
2047
2048 /*
2049 * Per spec, bits 32-47 contain the extra "flag number". However, we
2050 * have no use for it, and in all known usecases it is zero, so just
2051 * report lookup failure if it isn't.
2052 */
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002053 if (hc->ingpa & 0xffff00000000ULL)
Roman Kaganfaeb7832018-02-01 16:48:32 +03002054 return HV_STATUS_INVALID_PORT_ID;
2055 /* remaining bits are reserved-zero */
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002056 if (hc->ingpa & ~KVM_HYPERV_CONN_ID_MASK)
Roman Kaganfaeb7832018-02-01 16:48:32 +03002057 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2058
Paolo Bonzini452a68d2018-05-07 19:24:34 +02002059 /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
2060 rcu_read_lock();
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002061 eventfd = idr_find(&hv->conn_to_evt, hc->ingpa);
Paolo Bonzini452a68d2018-05-07 19:24:34 +02002062 rcu_read_unlock();
Roman Kaganfaeb7832018-02-01 16:48:32 +03002063 if (!eventfd)
2064 return HV_STATUS_INVALID_PORT_ID;
2065
2066 eventfd_signal(eventfd, 1);
2067 return HV_STATUS_SUCCESS;
2068}
2069
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02002070static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc)
2071{
2072 switch (hc->code) {
2073 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
2074 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
2075 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
2076 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
2077 return true;
2078 }
2079
2080 return false;
2081}
2082
2083static void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc)
2084{
2085 int reg;
2086
2087 kvm_fpu_get();
2088 for (reg = 0; reg < HV_HYPERCALL_MAX_XMM_REGISTERS; reg++)
2089 _kvm_read_sse_reg(reg, &hc->xmm[reg]);
2090 kvm_fpu_put();
2091}
2092
Vitaly Kuznetsov4ad81a92021-05-21 11:51:54 +02002093static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code)
2094{
2095 return true;
2096}
2097
Andrey Smetanine83d5882015-07-03 15:01:34 +03002098int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
2099{
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002100 struct kvm_hv_hcall hc;
2101 u64 ret = HV_STATUS_SUCCESS;
Andrey Smetanine83d5882015-07-03 15:01:34 +03002102
2103 /*
2104 * hypercall generates UD from non zero cpl and real mode
2105 * per HYPER-V spec
2106 */
Jason Baronb36464772021-01-14 22:27:56 -05002107 if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || !is_protmode(vcpu)) {
Andrey Smetanine83d5882015-07-03 15:01:34 +03002108 kvm_queue_exception(vcpu, UD_VECTOR);
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002109 return 1;
Andrey Smetanine83d5882015-07-03 15:01:34 +03002110 }
2111
Arnd Bergmannf4e4805e2019-07-12 16:13:09 +02002112#ifdef CONFIG_X86_64
2113 if (is_64_bit_mode(vcpu)) {
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002114 hc.param = kvm_rcx_read(vcpu);
2115 hc.ingpa = kvm_rdx_read(vcpu);
2116 hc.outgpa = kvm_r8_read(vcpu);
Arnd Bergmannf4e4805e2019-07-12 16:13:09 +02002117 } else
2118#endif
2119 {
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002120 hc.param = ((u64)kvm_rdx_read(vcpu) << 32) |
2121 (kvm_rax_read(vcpu) & 0xffffffff);
2122 hc.ingpa = ((u64)kvm_rbx_read(vcpu) << 32) |
2123 (kvm_rcx_read(vcpu) & 0xffffffff);
2124 hc.outgpa = ((u64)kvm_rdi_read(vcpu) << 32) |
2125 (kvm_rsi_read(vcpu) & 0xffffffff);
Andrey Smetanine83d5882015-07-03 15:01:34 +03002126 }
Andrey Smetanine83d5882015-07-03 15:01:34 +03002127
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002128 hc.code = hc.param & 0xffff;
2129 hc.fast = !!(hc.param & HV_HYPERCALL_FAST_BIT);
2130 hc.rep_cnt = (hc.param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
2131 hc.rep_idx = (hc.param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
2132 hc.rep = !!(hc.rep_cnt || hc.rep_idx);
Andrey Smetanine83d5882015-07-03 15:01:34 +03002133
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02002134 if (hc.fast && is_xmm_fast_hypercall(&hc))
2135 kvm_hv_hypercall_read_xmm(&hc);
2136
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002137 trace_kvm_hv_hypercall(hc.code, hc.fast, hc.rep_cnt, hc.rep_idx,
2138 hc.ingpa, hc.outgpa);
Andrey Smetanine83d5882015-07-03 15:01:34 +03002139
Vitaly Kuznetsov4ad81a92021-05-21 11:51:54 +02002140 if (unlikely(!hv_check_hypercall_access(to_hv_vcpu(vcpu), hc.code))) {
2141 ret = HV_STATUS_ACCESS_DENIED;
2142 goto hypercall_complete;
2143 }
2144
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002145 switch (hc.code) {
Andrey Smetanin8ed6d762016-02-11 16:44:57 +03002146 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002147 if (unlikely(hc.rep)) {
Vitaly Kuznetsov56b9ae72018-05-16 17:21:27 +02002148 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2149 break;
2150 }
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08002151 kvm_vcpu_on_spin(vcpu, true);
Andrey Smetanine83d5882015-07-03 15:01:34 +03002152 break;
Andrey Smetanin83326e42016-02-11 16:45:01 +03002153 case HVCALL_SIGNAL_EVENT:
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002154 if (unlikely(hc.rep)) {
Vitaly Kuznetsov56b9ae72018-05-16 17:21:27 +02002155 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2156 break;
2157 }
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002158 ret = kvm_hvcall_signal_event(vcpu, &hc);
Dan Carpenterd32ef542018-03-17 14:48:27 +03002159 if (ret != HV_STATUS_INVALID_PORT_ID)
Roman Kaganfaeb7832018-02-01 16:48:32 +03002160 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002161 fallthrough; /* maybe userspace knows this conn_id */
Roman Kaganfaeb7832018-02-01 16:48:32 +03002162 case HVCALL_POST_MESSAGE:
Paolo Bonzinia2b5c3c2016-03-29 11:23:25 +02002163 /* don't bother userspace if it has no way to handle it */
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002164 if (unlikely(hc.rep || !to_hv_synic(vcpu)->active)) {
Vitaly Kuznetsov56b9ae72018-05-16 17:21:27 +02002165 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
Paolo Bonzinia2b5c3c2016-03-29 11:23:25 +02002166 break;
2167 }
Andrey Smetanin83326e42016-02-11 16:45:01 +03002168 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
2169 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002170 vcpu->run->hyperv.u.hcall.input = hc.param;
2171 vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
2172 vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
Andrey Smetanin83326e42016-02-11 16:45:01 +03002173 vcpu->arch.complete_userspace_io =
2174 kvm_hv_hypercall_complete_userspace;
2175 return 0;
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02002176 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02002177 if (unlikely(!hc.rep_cnt || hc.rep_idx)) {
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02002178 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2179 break;
2180 }
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002181 ret = kvm_hv_flush_tlb(vcpu, &hc, false);
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02002182 break;
2183 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02002184 if (unlikely(hc.rep)) {
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02002185 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2186 break;
2187 }
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002188 ret = kvm_hv_flush_tlb(vcpu, &hc, false);
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02002189 break;
2190 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02002191 if (unlikely(!hc.rep_cnt || hc.rep_idx)) {
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02002192 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2193 break;
2194 }
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002195 ret = kvm_hv_flush_tlb(vcpu, &hc, true);
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02002196 break;
2197 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
Siddharth Chandrasekaran59745652021-05-26 10:56:10 +02002198 if (unlikely(hc.rep)) {
Vitaly Kuznetsovc7012672018-05-16 17:21:30 +02002199 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2200 break;
2201 }
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002202 ret = kvm_hv_flush_tlb(vcpu, &hc, true);
Vitaly Kuznetsove2f11f42018-05-16 17:21:29 +02002203 break;
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02002204 case HVCALL_SEND_IPI:
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002205 if (unlikely(hc.rep)) {
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02002206 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2207 break;
2208 }
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002209 ret = kvm_hv_send_ipi(vcpu, &hc, false);
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02002210 break;
2211 case HVCALL_SEND_IPI_EX:
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002212 if (unlikely(hc.fast || hc.rep)) {
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02002213 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2214 break;
2215 }
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002216 ret = kvm_hv_send_ipi(vcpu, &hc, true);
Vitaly Kuznetsov214ff832018-09-26 19:02:59 +02002217 break;
Jon Doronb1870382020-05-29 16:45:42 +03002218 case HVCALL_POST_DEBUG_DATA:
2219 case HVCALL_RETRIEVE_DEBUG_DATA:
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002220 if (unlikely(hc.fast)) {
Jon Doronb1870382020-05-29 16:45:42 +03002221 ret = HV_STATUS_INVALID_PARAMETER;
2222 break;
2223 }
2224 fallthrough;
2225 case HVCALL_RESET_DEBUG_SESSION: {
Vitaly Kuznetsovf69b55e2021-01-26 14:48:08 +01002226 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
Jon Doronb1870382020-05-29 16:45:42 +03002227
2228 if (!kvm_hv_is_syndbg_enabled(vcpu)) {
2229 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
2230 break;
2231 }
2232
2233 if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) {
2234 ret = HV_STATUS_OPERATION_DENIED;
2235 break;
2236 }
2237 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
2238 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
Siddharth Chandrasekaranbd38b322021-05-26 10:56:09 +02002239 vcpu->run->hyperv.u.hcall.input = hc.param;
2240 vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
2241 vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
Jon Doronb1870382020-05-29 16:45:42 +03002242 vcpu->arch.complete_userspace_io =
2243 kvm_hv_hypercall_complete_userspace;
2244 return 0;
2245 }
Andrey Smetanine83d5882015-07-03 15:01:34 +03002246 default:
Dan Carpenterd32ef542018-03-17 14:48:27 +03002247 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
Andrey Smetanine83d5882015-07-03 15:01:34 +03002248 break;
2249 }
2250
Vitaly Kuznetsov4ad81a92021-05-21 11:51:54 +02002251hypercall_complete:
Radim Krčmář696ca772018-05-24 17:50:56 +02002252 return kvm_hv_hypercall_complete(vcpu, ret);
Andrey Smetanine83d5882015-07-03 15:01:34 +03002253}
Roman Kagancbc02362018-02-01 16:48:31 +03002254
2255void kvm_hv_init_vm(struct kvm *kvm)
2256{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01002257 struct kvm_hv *hv = to_kvm_hv(kvm);
2258
2259 mutex_init(&hv->hv_lock);
2260 idr_init(&hv->conn_to_evt);
Roman Kagancbc02362018-02-01 16:48:31 +03002261}
2262
2263void kvm_hv_destroy_vm(struct kvm *kvm)
2264{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01002265 struct kvm_hv *hv = to_kvm_hv(kvm);
Roman Kaganfaeb7832018-02-01 16:48:32 +03002266 struct eventfd_ctx *eventfd;
2267 int i;
2268
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01002269 idr_for_each_entry(&hv->conn_to_evt, eventfd, i)
Roman Kaganfaeb7832018-02-01 16:48:32 +03002270 eventfd_ctx_put(eventfd);
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01002271 idr_destroy(&hv->conn_to_evt);
Roman Kaganfaeb7832018-02-01 16:48:32 +03002272}
2273
2274static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
2275{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01002276 struct kvm_hv *hv = to_kvm_hv(kvm);
Roman Kaganfaeb7832018-02-01 16:48:32 +03002277 struct eventfd_ctx *eventfd;
2278 int ret;
2279
2280 eventfd = eventfd_ctx_fdget(fd);
2281 if (IS_ERR(eventfd))
2282 return PTR_ERR(eventfd);
2283
2284 mutex_lock(&hv->hv_lock);
2285 ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
Ben Gardon254272c2019-02-11 11:02:50 -08002286 GFP_KERNEL_ACCOUNT);
Roman Kaganfaeb7832018-02-01 16:48:32 +03002287 mutex_unlock(&hv->hv_lock);
2288
2289 if (ret >= 0)
2290 return 0;
2291
2292 if (ret == -ENOSPC)
2293 ret = -EEXIST;
2294 eventfd_ctx_put(eventfd);
2295 return ret;
2296}
2297
2298static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
2299{
Vitaly Kuznetsov05f04ae2021-01-26 14:48:09 +01002300 struct kvm_hv *hv = to_kvm_hv(kvm);
Roman Kaganfaeb7832018-02-01 16:48:32 +03002301 struct eventfd_ctx *eventfd;
2302
2303 mutex_lock(&hv->hv_lock);
2304 eventfd = idr_remove(&hv->conn_to_evt, conn_id);
2305 mutex_unlock(&hv->hv_lock);
2306
2307 if (!eventfd)
2308 return -ENOENT;
2309
2310 synchronize_srcu(&kvm->srcu);
2311 eventfd_ctx_put(eventfd);
2312 return 0;
2313}
2314
2315int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
2316{
2317 if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
2318 (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
2319 return -EINVAL;
2320
2321 if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
2322 return kvm_hv_eventfd_deassign(kvm, args->conn_id);
2323 return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
Roman Kagancbc02362018-02-01 16:48:31 +03002324}
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002325
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02002326int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
2327 struct kvm_cpuid_entry2 __user *entries)
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002328{
Vitaly Kuznetsovea152982019-08-27 18:04:02 +02002329 uint16_t evmcs_ver = 0;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002330 struct kvm_cpuid_entry2 cpuid_entries[] = {
2331 { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
2332 { .function = HYPERV_CPUID_INTERFACE },
2333 { .function = HYPERV_CPUID_VERSION },
2334 { .function = HYPERV_CPUID_FEATURES },
2335 { .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
2336 { .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
Jon Doronf97f5a52020-05-29 16:45:40 +03002337 { .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS },
2338 { .function = HYPERV_CPUID_SYNDBG_INTERFACE },
2339 { .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES },
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002340 { .function = HYPERV_CPUID_NESTED_FEATURES },
2341 };
2342 int i, nent = ARRAY_SIZE(cpuid_entries);
2343
Paolo Bonzini33b22172020-04-17 10:24:18 -04002344 if (kvm_x86_ops.nested_ops->get_evmcs_version)
2345 evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu);
Vitaly Kuznetsovea152982019-08-27 18:04:02 +02002346
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002347 /* Skip NESTED_FEATURES if eVMCS is not supported */
2348 if (!evmcs_ver)
2349 --nent;
2350
2351 if (cpuid->nent < nent)
2352 return -E2BIG;
2353
2354 if (cpuid->nent > nent)
2355 cpuid->nent = nent;
2356
2357 for (i = 0; i < nent; i++) {
2358 struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
2359 u32 signature[3];
2360
2361 switch (ent->function) {
2362 case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
2363 memcpy(signature, "Linux KVM Hv", 12);
2364
Jon Doronf97f5a52020-05-29 16:45:40 +03002365 ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002366 ent->ebx = signature[0];
2367 ent->ecx = signature[1];
2368 ent->edx = signature[2];
2369 break;
2370
2371 case HYPERV_CPUID_INTERFACE:
Vitaly Kuznetsov8f014552021-01-26 14:48:14 +01002372 ent->eax = HYPERV_CPUID_SIGNATURE_EAX;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002373 break;
2374
2375 case HYPERV_CPUID_VERSION:
2376 /*
2377 * We implement some Hyper-V 2016 functions so let's use
2378 * this version.
2379 */
2380 ent->eax = 0x00003839;
2381 ent->ebx = 0x000A0000;
2382 break;
2383
2384 case HYPERV_CPUID_FEATURES:
Joseph Salisburydfc53ba2020-09-26 07:26:26 -07002385 ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002386 ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
Joseph Salisburydfc53ba2020-09-26 07:26:26 -07002387 ent->eax |= HV_MSR_SYNIC_AVAILABLE;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002388 ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
Joseph Salisburydfc53ba2020-09-26 07:26:26 -07002389 ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE;
2390 ent->eax |= HV_MSR_HYPERCALL_AVAILABLE;
2391 ent->eax |= HV_MSR_VP_INDEX_AVAILABLE;
2392 ent->eax |= HV_MSR_RESET_AVAILABLE;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002393 ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
Joseph Salisburydfc53ba2020-09-26 07:26:26 -07002394 ent->eax |= HV_ACCESS_FREQUENCY_MSRS;
2395 ent->eax |= HV_ACCESS_REENLIGHTENMENT;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002396
Joseph Salisburydfc53ba2020-09-26 07:26:26 -07002397 ent->ebx |= HV_POST_MESSAGES;
2398 ent->ebx |= HV_SIGNAL_EVENTS;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002399
Siddharth Chandrasekarand8f55372021-05-26 11:03:56 +02002400 ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002401 ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
2402 ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
Wanpeng Lia073d7e2019-09-16 15:42:32 +08002403
Linus Torvalds039aeb92020-06-03 15:13:47 -07002404 ent->ebx |= HV_DEBUGGING;
Jon Doronf97f5a52020-05-29 16:45:40 +03002405 ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE;
2406 ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE;
2407
Wanpeng Lia073d7e2019-09-16 15:42:32 +08002408 /*
2409 * Direct Synthetic timers only make sense with in-kernel
2410 * LAPIC
2411 */
Vitaly Kuznetsovc21d54f2020-09-29 17:09:43 +02002412 if (!vcpu || lapic_in_kernel(vcpu))
Wanpeng Lia073d7e2019-09-16 15:42:32 +08002413 ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002414
2415 break;
2416
2417 case HYPERV_CPUID_ENLIGHTMENT_INFO:
2418 ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
2419 ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002420 ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
2421 ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
2422 ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
Vitaly Kuznetsovf1adcea2019-01-25 12:19:34 +01002423 if (evmcs_ver)
2424 ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
Vitaly Kuznetsovb2d8b162019-09-16 18:22:57 +02002425 if (!cpu_smt_possible())
2426 ent->eax |= HV_X64_NO_NONARCH_CORESHARING;
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002427 /*
2428 * Default number of spinlock retry attempts, matches
2429 * HyperV 2016.
2430 */
2431 ent->ebx = 0x00000FFF;
2432
2433 break;
2434
2435 case HYPERV_CPUID_IMPLEMENT_LIMITS:
2436 /* Maximum number of virtual processors */
2437 ent->eax = KVM_MAX_VCPUS;
2438 /*
2439 * Maximum number of logical processors, matches
2440 * HyperV 2016.
2441 */
2442 ent->ebx = 64;
2443
2444 break;
2445
2446 case HYPERV_CPUID_NESTED_FEATURES:
2447 ent->eax = evmcs_ver;
2448
2449 break;
2450
Jon Doronf97f5a52020-05-29 16:45:40 +03002451 case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS:
2452 memcpy(signature, "Linux KVM Hv", 12);
2453
2454 ent->eax = 0;
2455 ent->ebx = signature[0];
2456 ent->ecx = signature[1];
2457 ent->edx = signature[2];
2458 break;
2459
2460 case HYPERV_CPUID_SYNDBG_INTERFACE:
2461 memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12);
2462 ent->eax = signature[0];
2463 break;
2464
2465 case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES:
2466 ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
2467 break;
2468
Vitaly Kuznetsov2bc39972018-12-10 18:21:56 +01002469 default:
2470 break;
2471 }
2472 }
2473
2474 if (copy_to_user(entries, cpuid_entries,
2475 nent * sizeof(struct kvm_cpuid_entry2)))
2476 return -EFAULT;
2477
2478 return 0;
2479}