2019-06-04 16:11:32 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-07-03 20:01:34 +08:00
|
|
|
/*
|
|
|
|
* KVM Microsoft Hyper-V emulation
|
|
|
|
*
|
|
|
|
* derived from arch/x86/kvm/x86.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Qumranet, Inc.
|
|
|
|
* Copyright (C) 2008 Qumranet, Inc.
|
|
|
|
* Copyright IBM Corporation, 2008
|
|
|
|
* Copyright 2010 Red Hat, Inc. and/or its affiliates.
|
|
|
|
* Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Avi Kivity <avi@qumranet.com>
|
|
|
|
* Yaniv Kamay <yaniv@qumranet.com>
|
|
|
|
* Amit Shah <amit.shah@qumranet.com>
|
|
|
|
* Ben-Ami Yassour <benami@il.ibm.com>
|
|
|
|
* Andrey Smetanin <asmetanin@virtuozzo.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "x86.h"
|
|
|
|
#include "lapic.h"
|
2015-11-10 20:36:34 +08:00
|
|
|
#include "ioapic.h"
|
2020-05-29 21:45:40 +08:00
|
|
|
#include "cpuid.h"
|
2015-07-03 20:01:34 +08:00
|
|
|
#include "hyperv.h"
|
2018-06-13 21:55:44 +08:00
|
|
|
#include "xen.h"
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2019-09-17 00:22:57 +08:00
|
|
|
#include <linux/cpu.h>
|
2015-07-03 20:01:34 +08:00
|
|
|
#include <linux/kvm_host.h>
|
2015-12-01 00:22:20 +08:00
|
|
|
#include <linux/highmem.h>
|
2017-02-05 18:48:36 +08:00
|
|
|
#include <linux/sched/cputime.h>
|
2018-02-01 21:48:32 +08:00
|
|
|
#include <linux/eventfd.h>
|
2017-02-05 18:48:36 +08:00
|
|
|
|
2015-11-10 20:36:34 +08:00
|
|
|
#include <asm/apicdef.h>
|
2015-07-03 20:01:34 +08:00
|
|
|
#include <trace/events/kvm.h>
|
|
|
|
|
|
|
|
#include "trace.h"
|
2019-12-05 03:07:17 +08:00
|
|
|
#include "irq.h"
|
2021-05-26 16:56:10 +08:00
|
|
|
#include "fpu.h"
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2021-01-26 21:48:14 +08:00
|
|
|
/* "Hv#1" signature */
|
|
|
|
#define HYPERV_CPUID_SIGNATURE_EAX 0x31237648
|
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
#define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
|
|
|
|
|
2018-11-26 23:47:31 +08:00
|
|
|
static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
|
|
|
|
bool vcpu_kick);
|
|
|
|
|
2015-11-10 20:36:34 +08:00
|
|
|
static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
|
|
|
|
{
|
|
|
|
return atomic64_read(&synic->sint[sint]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int synic_get_sint_vector(u64 sint_value)
|
|
|
|
{
|
|
|
|
if (sint_value & HV_SYNIC_SINT_MASKED)
|
|
|
|
return -1;
|
|
|
|
return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
|
|
|
|
int vector)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
|
|
|
|
if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
|
|
|
|
int vector)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u64 sint_value;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
|
|
|
|
sint_value = synic_read_sint(synic, i);
|
|
|
|
if (synic_get_sint_vector(sint_value) == vector &&
|
|
|
|
sint_value & HV_SYNIC_SINT_AUTO_EOI)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:15:13 +08:00
|
|
|
static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
|
|
|
|
int vector)
|
|
|
|
{
|
2021-08-11 04:52:46 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
|
|
|
|
struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
|
|
|
|
int auto_eoi_old, auto_eoi_new;
|
|
|
|
|
2018-12-05 23:36:21 +08:00
|
|
|
if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
|
|
|
|
return;
|
|
|
|
|
2018-03-01 22:15:13 +08:00
|
|
|
if (synic_has_vector_connected(synic, vector))
|
|
|
|
__set_bit(vector, synic->vec_bitmap);
|
|
|
|
else
|
|
|
|
__clear_bit(vector, synic->vec_bitmap);
|
|
|
|
|
2021-08-11 04:52:46 +08:00
|
|
|
auto_eoi_old = bitmap_weight(synic->auto_eoi_bitmap, 256);
|
|
|
|
|
2018-03-01 22:15:13 +08:00
|
|
|
if (synic_has_vector_auto_eoi(synic, vector))
|
|
|
|
__set_bit(vector, synic->auto_eoi_bitmap);
|
|
|
|
else
|
|
|
|
__clear_bit(vector, synic->auto_eoi_bitmap);
|
2021-08-11 04:52:46 +08:00
|
|
|
|
|
|
|
auto_eoi_new = bitmap_weight(synic->auto_eoi_bitmap, 256);
|
|
|
|
|
|
|
|
if (!!auto_eoi_old == !!auto_eoi_new)
|
|
|
|
return;
|
|
|
|
|
2021-12-08 09:52:34 +08:00
|
|
|
if (!enable_apicv)
|
|
|
|
return;
|
|
|
|
|
2021-10-22 08:49:27 +08:00
|
|
|
down_write(&vcpu->kvm->arch.apicv_update_lock);
|
2021-08-11 04:52:46 +08:00
|
|
|
|
|
|
|
if (auto_eoi_new)
|
|
|
|
hv->synic_auto_eoi_used++;
|
|
|
|
else
|
|
|
|
hv->synic_auto_eoi_used--;
|
|
|
|
|
|
|
|
__kvm_request_apicv_update(vcpu->kvm,
|
|
|
|
!hv->synic_auto_eoi_used,
|
|
|
|
APICV_INHIBIT_REASON_HYPERV);
|
|
|
|
|
2021-10-22 08:49:27 +08:00
|
|
|
up_write(&vcpu->kvm->arch.apicv_update_lock);
|
2018-03-01 22:15:13 +08:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:27:23 +08:00
|
|
|
static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
|
|
|
|
u64 data, bool host)
|
2015-11-10 20:36:34 +08:00
|
|
|
{
|
2018-03-01 22:15:13 +08:00
|
|
|
int vector, old_vector;
|
2018-03-01 22:15:14 +08:00
|
|
|
bool masked;
|
2015-11-10 20:36:34 +08:00
|
|
|
|
|
|
|
vector = data & HV_SYNIC_SINT_VECTOR_MASK;
|
2018-03-01 22:15:14 +08:00
|
|
|
masked = data & HV_SYNIC_SINT_MASKED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Valid vectors are 16-255, however, nested Hyper-V attempts to write
|
|
|
|
* default '0x10000' value on boot and this should not #GP. We need to
|
|
|
|
* allow zero-initing the register from host as well.
|
|
|
|
*/
|
|
|
|
if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
|
2015-11-10 20:36:34 +08:00
|
|
|
return 1;
|
|
|
|
/*
|
|
|
|
* Guest may configure multiple SINTs to use the same vector, so
|
|
|
|
* we maintain a bitmap of vectors handled by synic, and a
|
|
|
|
* bitmap of vectors with auto-eoi behavior. The bitmaps are
|
|
|
|
* updated here, and atomically queried on fast paths.
|
|
|
|
*/
|
2018-03-01 22:15:13 +08:00
|
|
|
old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
|
2015-11-10 20:36:34 +08:00
|
|
|
|
|
|
|
atomic64_set(&synic->sint[sint], data);
|
|
|
|
|
2018-03-01 22:15:13 +08:00
|
|
|
synic_update_vector(synic, old_vector);
|
2015-11-10 20:36:34 +08:00
|
|
|
|
2018-03-01 22:15:13 +08:00
|
|
|
synic_update_vector(synic, vector);
|
2015-11-10 20:36:34 +08:00
|
|
|
|
|
|
|
/* Load SynIC vectors into EOI exit bitmap */
|
2021-01-26 21:48:06 +08:00
|
|
|
kvm_make_request(KVM_REQ_SCAN_IOAPIC, hv_synic_to_vcpu(synic));
|
2015-11-10 20:36:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:13:20 +08:00
|
|
|
static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
|
|
|
|
{
|
|
|
|
struct kvm_vcpu *vcpu = NULL;
|
2021-11-17 00:04:02 +08:00
|
|
|
unsigned long i;
|
2017-07-14 22:13:20 +08:00
|
|
|
|
2018-08-22 18:18:28 +08:00
|
|
|
if (vpidx >= KVM_MAX_VCPUS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vcpu = kvm_get_vcpu(kvm, vpidx);
|
2021-01-26 21:48:12 +08:00
|
|
|
if (vcpu && kvm_hv_get_vpindex(vcpu) == vpidx)
|
2017-07-14 22:13:20 +08:00
|
|
|
return vcpu;
|
|
|
|
kvm_for_each_vcpu(i, vcpu, kvm)
|
2021-01-26 21:48:12 +08:00
|
|
|
if (kvm_hv_get_vpindex(vcpu) == vpidx)
|
2017-07-14 22:13:20 +08:00
|
|
|
return vcpu;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
|
2015-11-10 20:36:34 +08:00
|
|
|
{
|
|
|
|
struct kvm_vcpu *vcpu;
|
|
|
|
struct kvm_vcpu_hv_synic *synic;
|
|
|
|
|
2017-07-14 22:13:20 +08:00
|
|
|
vcpu = get_vcpu_by_vpidx(kvm, vpidx);
|
2021-02-26 15:59:59 +08:00
|
|
|
if (!vcpu || !to_hv_vcpu(vcpu))
|
2015-11-10 20:36:34 +08:00
|
|
|
return NULL;
|
2021-01-26 21:48:06 +08:00
|
|
|
synic = to_hv_synic(vcpu);
|
2015-11-10 20:36:34 +08:00
|
|
|
return (synic->active) ? synic : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
|
|
|
|
{
|
|
|
|
struct kvm *kvm = vcpu->kvm;
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
|
2021-01-26 21:48:05 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2015-12-01 00:22:21 +08:00
|
|
|
struct kvm_vcpu_hv_stimer *stimer;
|
2018-11-26 23:47:32 +08:00
|
|
|
int gsi, idx;
|
2015-11-10 20:36:34 +08:00
|
|
|
|
2015-12-23 21:53:59 +08:00
|
|
|
trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
|
2015-11-10 20:36:34 +08:00
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
/* Try to deliver pending Hyper-V SynIC timers messages */
|
|
|
|
for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
|
|
|
|
stimer = &hv_vcpu->stimer[idx];
|
2018-11-26 23:47:30 +08:00
|
|
|
if (stimer->msg_pending && stimer->config.enable &&
|
2018-11-26 23:47:31 +08:00
|
|
|
!stimer->config.direct_mode &&
|
2018-11-26 23:47:32 +08:00
|
|
|
stimer->config.sintx == sint)
|
|
|
|
stimer_mark_pending(stimer, false);
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
|
|
|
|
2015-11-10 20:36:34 +08:00
|
|
|
idx = srcu_read_lock(&kvm->irq_srcu);
|
2015-12-01 00:22:21 +08:00
|
|
|
gsi = atomic_read(&synic->sint_to_gsi[sint]);
|
2015-11-10 20:36:34 +08:00
|
|
|
if (gsi != -1)
|
|
|
|
kvm_notify_acked_gsi(kvm, gsi);
|
|
|
|
srcu_read_unlock(&kvm->irq_srcu, idx);
|
|
|
|
}
|
|
|
|
|
2015-11-10 20:36:35 +08:00
|
|
|
static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
|
|
|
|
{
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
|
2021-01-26 21:48:11 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2015-11-10 20:36:35 +08:00
|
|
|
|
|
|
|
hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
|
|
|
|
hv_vcpu->exit.u.synic.msr = msr;
|
|
|
|
hv_vcpu->exit.u.synic.control = synic->control;
|
|
|
|
hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
|
|
|
|
hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
|
|
|
|
|
|
|
|
kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
|
|
|
|
}
|
|
|
|
|
2015-11-10 20:36:34 +08:00
|
|
|
static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
|
|
|
|
u32 msr, u64 data, bool host)
|
|
|
|
{
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
|
2015-11-10 20:36:34 +08:00
|
|
|
int ret;
|
|
|
|
|
2018-07-26 19:01:52 +08:00
|
|
|
if (!synic->active && !host)
|
2015-11-10 20:36:34 +08:00
|
|
|
return 1;
|
|
|
|
|
2015-12-23 21:53:59 +08:00
|
|
|
trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
|
|
|
|
|
2015-11-10 20:36:34 +08:00
|
|
|
ret = 0;
|
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_SCONTROL:
|
|
|
|
synic->control = data;
|
2015-11-10 20:36:35 +08:00
|
|
|
if (!host)
|
|
|
|
synic_exit(synic, msr);
|
2015-11-10 20:36:34 +08:00
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SVERSION:
|
|
|
|
if (!host) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
synic->version = data;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SIEFP:
|
kvm: x86: hyperv: add KVM_CAP_HYPERV_SYNIC2
There is a flaw in the Hyper-V SynIC implementation in KVM: when message
page or event flags page is enabled by setting the corresponding msr,
KVM zeroes it out. This is problematic because on migration the
corresponding MSRs are loaded on the destination, so the content of
those pages is lost.
This went unnoticed so far because the only user of those pages was
in-KVM hyperv synic timers, which could continue working despite that
zeroing.
Newer QEMU uses those pages for Hyper-V VMBus implementation, and
zeroing them breaks the migration.
Besides, in newer QEMU the content of those pages is fully managed by
QEMU, so zeroing them is undesirable even when writing the MSRs from the
guest side.
To support this new scheme, introduce a new capability,
KVM_CAP_HYPERV_SYNIC2, which, when enabled, makes sure that the synic
pages aren't zeroed out in KVM.
Signed-off-by: Roman Kagan <rkagan@virtuozzo.com>
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
2017-06-22 21:51:01 +08:00
|
|
|
if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
|
|
|
|
!synic->dont_zero_synic_pages)
|
2015-11-10 20:36:34 +08:00
|
|
|
if (kvm_clear_guest(vcpu->kvm,
|
|
|
|
data & PAGE_MASK, PAGE_SIZE)) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
synic->evt_page = data;
|
2015-11-10 20:36:35 +08:00
|
|
|
if (!host)
|
|
|
|
synic_exit(synic, msr);
|
2015-11-10 20:36:34 +08:00
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SIMP:
|
kvm: x86: hyperv: add KVM_CAP_HYPERV_SYNIC2
There is a flaw in the Hyper-V SynIC implementation in KVM: when message
page or event flags page is enabled by setting the corresponding msr,
KVM zeroes it out. This is problematic because on migration the
corresponding MSRs are loaded on the destination, so the content of
those pages is lost.
This went unnoticed so far because the only user of those pages was
in-KVM hyperv synic timers, which could continue working despite that
zeroing.
Newer QEMU uses those pages for Hyper-V VMBus implementation, and
zeroing them breaks the migration.
Besides, in newer QEMU the content of those pages is fully managed by
QEMU, so zeroing them is undesirable even when writing the MSRs from the
guest side.
To support this new scheme, introduce a new capability,
KVM_CAP_HYPERV_SYNIC2, which, when enabled, makes sure that the synic
pages aren't zeroed out in KVM.
Signed-off-by: Roman Kagan <rkagan@virtuozzo.com>
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
2017-06-22 21:51:01 +08:00
|
|
|
if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
|
|
|
|
!synic->dont_zero_synic_pages)
|
2015-11-10 20:36:34 +08:00
|
|
|
if (kvm_clear_guest(vcpu->kvm,
|
|
|
|
data & PAGE_MASK, PAGE_SIZE)) {
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
synic->msg_page = data;
|
2015-11-10 20:36:35 +08:00
|
|
|
if (!host)
|
|
|
|
synic_exit(synic, msr);
|
2015-11-10 20:36:34 +08:00
|
|
|
break;
|
|
|
|
case HV_X64_MSR_EOM: {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
|
|
|
|
kvm_hv_notify_acked_sint(vcpu, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
|
2015-12-28 23:27:23 +08:00
|
|
|
ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
|
2015-11-10 20:36:34 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-05-29 21:45:40 +08:00
|
|
|
static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2021-05-21 17:51:37 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2020-05-29 21:45:40 +08:00
|
|
|
|
2021-05-21 17:51:37 +08:00
|
|
|
return hv_vcpu->cpuid_cache.syndbg_cap_eax &
|
|
|
|
HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
|
2020-05-29 21:45:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
|
2020-05-29 21:45:40 +08:00
|
|
|
|
|
|
|
if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL)
|
|
|
|
hv->hv_syndbg.control.status =
|
|
|
|
vcpu->run->hyperv.u.syndbg.status;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr)
|
|
|
|
{
|
2021-01-26 21:48:08 +08:00
|
|
|
struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
|
2021-01-26 21:48:11 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2020-05-29 21:45:40 +08:00
|
|
|
|
|
|
|
hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG;
|
|
|
|
hv_vcpu->exit.u.syndbg.msr = msr;
|
|
|
|
hv_vcpu->exit.u.syndbg.control = syndbg->control.control;
|
|
|
|
hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page;
|
|
|
|
hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page;
|
|
|
|
hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page;
|
|
|
|
vcpu->arch.complete_userspace_io =
|
|
|
|
kvm_hv_syndbg_complete_userspace;
|
|
|
|
|
|
|
|
kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
|
|
|
|
{
|
2021-01-26 21:48:08 +08:00
|
|
|
struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
|
2020-05-29 21:45:40 +08:00
|
|
|
|
|
|
|
if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id,
|
2021-01-26 21:48:05 +08:00
|
|
|
to_hv_vcpu(vcpu)->vp_index, msr, data);
|
2020-05-29 21:45:40 +08:00
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_SYNDBG_CONTROL:
|
|
|
|
syndbg->control.control = data;
|
|
|
|
if (!host)
|
|
|
|
syndbg_exit(vcpu, msr);
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_STATUS:
|
|
|
|
syndbg->control.status = data;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_SEND_BUFFER:
|
|
|
|
syndbg->control.send_page = data;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_RECV_BUFFER:
|
|
|
|
syndbg->control.recv_page = data;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
|
|
|
|
syndbg->control.pending_page = data;
|
|
|
|
if (!host)
|
|
|
|
syndbg_exit(vcpu, msr);
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_OPTIONS:
|
|
|
|
syndbg->options = data;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
|
|
|
|
{
|
2021-01-26 21:48:08 +08:00
|
|
|
struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
|
2020-05-29 21:45:40 +08:00
|
|
|
|
|
|
|
if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_SYNDBG_CONTROL:
|
|
|
|
*pdata = syndbg->control.control;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_STATUS:
|
|
|
|
*pdata = syndbg->control.status;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_SEND_BUFFER:
|
|
|
|
*pdata = syndbg->control.send_page;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_RECV_BUFFER:
|
|
|
|
*pdata = syndbg->control.recv_page;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
|
|
|
|
*pdata = syndbg->control.pending_page;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SYNDBG_OPTIONS:
|
|
|
|
*pdata = syndbg->options;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:12 +08:00
|
|
|
trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id, kvm_hv_get_vpindex(vcpu), msr, *pdata);
|
2020-05-29 21:45:40 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-26 19:01:52 +08:00
|
|
|
static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
|
|
|
|
bool host)
|
2015-11-10 20:36:34 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-07-26 19:01:52 +08:00
|
|
|
if (!synic->active && !host)
|
2015-11-10 20:36:34 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_SCONTROL:
|
|
|
|
*pdata = synic->control;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SVERSION:
|
|
|
|
*pdata = synic->version;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SIEFP:
|
|
|
|
*pdata = synic->evt_page;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SIMP:
|
|
|
|
*pdata = synic->msg_page;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_EOM:
|
|
|
|
*pdata = 0;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
|
|
|
|
*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:56:33 +08:00
|
|
|
static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
|
2015-11-10 20:36:34 +08:00
|
|
|
{
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
|
2015-11-10 20:36:34 +08:00
|
|
|
struct kvm_lapic_irq irq;
|
|
|
|
int ret, vector;
|
|
|
|
|
|
|
|
if (sint >= ARRAY_SIZE(synic->sint))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
vector = synic_get_sint_vector(synic_read_sint(synic, sint));
|
|
|
|
if (vector < 0)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
memset(&irq, 0, sizeof(irq));
|
2016-12-16 01:06:45 +08:00
|
|
|
irq.shorthand = APIC_DEST_SELF;
|
2015-11-10 20:36:34 +08:00
|
|
|
irq.dest_mode = APIC_DEST_PHYSICAL;
|
|
|
|
irq.delivery_mode = APIC_DM_FIXED;
|
|
|
|
irq.vector = vector;
|
|
|
|
irq.level = 1;
|
|
|
|
|
2016-12-16 01:06:45 +08:00
|
|
|
ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
|
2015-12-23 21:53:59 +08:00
|
|
|
trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
|
2015-11-10 20:36:34 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:13:20 +08:00
|
|
|
int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
|
2015-11-10 20:36:34 +08:00
|
|
|
{
|
|
|
|
struct kvm_vcpu_hv_synic *synic;
|
|
|
|
|
2017-07-14 22:13:20 +08:00
|
|
|
synic = synic_get(kvm, vpidx);
|
2015-11-10 20:36:34 +08:00
|
|
|
if (!synic)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return synic_set_irq(synic, sint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
|
|
|
|
{
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
|
2015-11-10 20:36:34 +08:00
|
|
|
int i;
|
|
|
|
|
2015-12-23 21:53:59 +08:00
|
|
|
trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
|
2015-11-10 20:36:34 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
|
|
|
|
if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
|
|
|
|
kvm_hv_notify_acked_sint(vcpu, i);
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:13:20 +08:00
|
|
|
static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
|
2015-11-10 20:36:34 +08:00
|
|
|
{
|
|
|
|
struct kvm_vcpu_hv_synic *synic;
|
|
|
|
|
2017-07-14 22:13:20 +08:00
|
|
|
synic = synic_get(kvm, vpidx);
|
2015-11-10 20:36:34 +08:00
|
|
|
if (!synic)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
atomic_set(&synic->sint_to_gsi[sint], gsi);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_hv_irq_routing_update(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
struct kvm_irq_routing_table *irq_rt;
|
|
|
|
struct kvm_kernel_irq_routing_entry *e;
|
|
|
|
u32 gsi;
|
|
|
|
|
|
|
|
irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
|
|
|
|
lockdep_is_held(&kvm->irq_lock));
|
|
|
|
|
|
|
|
for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
|
|
|
|
hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
|
|
|
|
if (e->type == KVM_IRQ_ROUTING_HV_SINT)
|
|
|
|
kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
|
|
|
|
e->hv_sint.sint, gsi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void synic_init(struct kvm_vcpu_hv_synic *synic)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(synic, 0, sizeof(*synic));
|
|
|
|
synic->version = HV_SYNIC_VERSION_1;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
|
|
|
|
atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
|
|
|
|
atomic_set(&synic->sint_to_gsi[i], -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-01 00:22:19 +08:00
|
|
|
static u64 get_time_ref_counter(struct kvm *kvm)
|
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2016-02-08 19:54:12 +08:00
|
|
|
struct kvm_vcpu *vcpu;
|
|
|
|
u64 tsc;
|
|
|
|
|
|
|
|
/*
|
2021-03-16 22:37:35 +08:00
|
|
|
* Fall back to get_kvmclock_ns() when TSC page hasn't been set up,
|
|
|
|
* is broken, disabled or being updated.
|
2016-02-08 19:54:12 +08:00
|
|
|
*/
|
2021-03-16 22:37:35 +08:00
|
|
|
if (hv->hv_tsc_page_status != HV_TSC_PAGE_SET)
|
2016-02-08 19:54:12 +08:00
|
|
|
return div_u64(get_kvmclock_ns(kvm), 100);
|
|
|
|
|
|
|
|
vcpu = kvm_get_vcpu(kvm, 0);
|
|
|
|
tsc = kvm_read_l1_tsc(vcpu, rdtsc());
|
|
|
|
return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
|
|
|
|
+ hv->tsc_ref.tsc_offset;
|
2015-12-01 00:22:19 +08:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:27:24 +08:00
|
|
|
static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
|
2015-12-01 00:22:21 +08:00
|
|
|
bool vcpu_kick)
|
|
|
|
{
|
2021-01-26 21:48:07 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
|
2015-12-01 00:22:21 +08:00
|
|
|
|
|
|
|
set_bit(stimer->index,
|
2021-01-26 21:48:05 +08:00
|
|
|
to_hv_vcpu(vcpu)->stimer_pending_bitmap);
|
2015-12-01 00:22:21 +08:00
|
|
|
kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
|
|
|
|
if (vcpu_kick)
|
|
|
|
kvm_vcpu_kick(vcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
|
|
|
|
{
|
2021-01-26 21:48:07 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
|
2015-12-01 00:22:21 +08:00
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
trace_kvm_hv_stimer_cleanup(hv_stimer_to_vcpu(stimer)->vcpu_id,
|
2015-12-23 21:54:00 +08:00
|
|
|
stimer->index);
|
|
|
|
|
2015-12-28 23:27:19 +08:00
|
|
|
hrtimer_cancel(&stimer->timer);
|
2015-12-01 00:22:21 +08:00
|
|
|
clear_bit(stimer->index,
|
2021-01-26 21:48:05 +08:00
|
|
|
to_hv_vcpu(vcpu)->stimer_pending_bitmap);
|
2015-12-01 00:22:21 +08:00
|
|
|
stimer->msg_pending = false;
|
2015-12-28 23:27:20 +08:00
|
|
|
stimer->exp_time = 0;
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
|
|
|
|
{
|
|
|
|
struct kvm_vcpu_hv_stimer *stimer;
|
|
|
|
|
|
|
|
stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
|
2021-01-26 21:48:07 +08:00
|
|
|
trace_kvm_hv_stimer_callback(hv_stimer_to_vcpu(stimer)->vcpu_id,
|
2015-12-23 21:54:00 +08:00
|
|
|
stimer->index);
|
2015-12-28 23:27:24 +08:00
|
|
|
stimer_mark_pending(stimer, true);
|
2015-12-01 00:22:21 +08:00
|
|
|
|
|
|
|
return HRTIMER_NORESTART;
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:27:20 +08:00
|
|
|
/*
|
|
|
|
* stimer_start() assumptions:
|
|
|
|
* a) stimer->count is not equal to 0
|
|
|
|
* b) stimer->config has HV_STIMER_ENABLE flag
|
|
|
|
*/
|
2015-12-01 00:22:21 +08:00
|
|
|
static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
|
|
|
|
{
|
|
|
|
u64 time_now;
|
|
|
|
ktime_t ktime_now;
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
time_now = get_time_ref_counter(hv_stimer_to_vcpu(stimer)->kvm);
|
2015-12-01 00:22:21 +08:00
|
|
|
ktime_now = ktime_get();
|
|
|
|
|
2018-11-26 23:47:30 +08:00
|
|
|
if (stimer->config.periodic) {
|
2015-12-28 23:27:20 +08:00
|
|
|
if (stimer->exp_time) {
|
|
|
|
if (time_now >= stimer->exp_time) {
|
|
|
|
u64 remainder;
|
|
|
|
|
|
|
|
div64_u64_rem(time_now - stimer->exp_time,
|
|
|
|
stimer->count, &remainder);
|
|
|
|
stimer->exp_time =
|
|
|
|
time_now + (stimer->count - remainder);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
stimer->exp_time = time_now + stimer->count;
|
2015-12-01 00:22:21 +08:00
|
|
|
|
2015-12-23 21:54:00 +08:00
|
|
|
trace_kvm_hv_stimer_start_periodic(
|
2021-01-26 21:48:07 +08:00
|
|
|
hv_stimer_to_vcpu(stimer)->vcpu_id,
|
2015-12-23 21:54:00 +08:00
|
|
|
stimer->index,
|
|
|
|
time_now, stimer->exp_time);
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
hrtimer_start(&stimer->timer,
|
2015-12-28 23:27:20 +08:00
|
|
|
ktime_add_ns(ktime_now,
|
|
|
|
100 * (stimer->exp_time - time_now)),
|
2015-12-01 00:22:21 +08:00
|
|
|
HRTIMER_MODE_ABS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
stimer->exp_time = stimer->count;
|
|
|
|
if (time_now >= stimer->count) {
|
|
|
|
/*
|
|
|
|
* Expire timer according to Hypervisor Top-Level Functional
|
|
|
|
* specification v4(15.3.1):
|
|
|
|
* "If a one shot is enabled and the specified count is in
|
|
|
|
* the past, it will expire immediately."
|
|
|
|
*/
|
2015-12-28 23:27:24 +08:00
|
|
|
stimer_mark_pending(stimer, false);
|
2015-12-01 00:22:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
trace_kvm_hv_stimer_start_one_shot(hv_stimer_to_vcpu(stimer)->vcpu_id,
|
2015-12-23 21:54:00 +08:00
|
|
|
stimer->index,
|
|
|
|
time_now, stimer->count);
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
hrtimer_start(&stimer->timer,
|
|
|
|
ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
|
|
|
|
HRTIMER_MODE_ABS);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
|
|
|
|
bool host)
|
|
|
|
{
|
2018-11-26 23:47:31 +08:00
|
|
|
union hv_stimer_config new_config = {.as_uint64 = config},
|
|
|
|
old_config = {.as_uint64 = stimer->config.as_uint64};
|
2021-01-26 21:48:07 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
|
2021-05-21 17:51:53 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
|
2020-09-24 22:57:52 +08:00
|
|
|
|
|
|
|
if (!synic->active && !host)
|
|
|
|
return 1;
|
2018-11-26 23:47:30 +08:00
|
|
|
|
2021-05-21 17:51:53 +08:00
|
|
|
if (unlikely(!host && hv_vcpu->enforce_cpuid && new_config.direct_mode &&
|
|
|
|
!(hv_vcpu->cpuid_cache.features_edx &
|
|
|
|
HV_STIMER_DIRECT_MODE_AVAILABLE)))
|
|
|
|
return 1;
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
trace_kvm_hv_stimer_set_config(hv_stimer_to_vcpu(stimer)->vcpu_id,
|
2015-12-23 21:54:00 +08:00
|
|
|
stimer->index, config, host);
|
|
|
|
|
2015-12-28 23:27:24 +08:00
|
|
|
stimer_cleanup(stimer);
|
2018-11-26 23:47:31 +08:00
|
|
|
if (old_config.enable &&
|
|
|
|
!new_config.direct_mode && new_config.sintx == 0)
|
2018-11-26 23:47:30 +08:00
|
|
|
new_config.enable = 0;
|
|
|
|
stimer->config.as_uint64 = new_config.as_uint64;
|
2018-11-26 23:47:31 +08:00
|
|
|
|
2019-03-14 01:13:42 +08:00
|
|
|
if (stimer->config.enable)
|
|
|
|
stimer_mark_pending(stimer, false);
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
|
|
|
|
bool host)
|
|
|
|
{
|
2021-01-26 21:48:07 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
|
2020-09-24 22:57:52 +08:00
|
|
|
|
|
|
|
if (!synic->active && !host)
|
|
|
|
return 1;
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id,
|
2015-12-23 21:54:00 +08:00
|
|
|
stimer->index, count, host);
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
stimer_cleanup(stimer);
|
2015-12-28 23:27:24 +08:00
|
|
|
stimer->count = count;
|
2015-12-01 00:22:21 +08:00
|
|
|
if (stimer->count == 0)
|
2018-11-26 23:47:30 +08:00
|
|
|
stimer->config.enable = 0;
|
|
|
|
else if (stimer->config.auto_enable)
|
|
|
|
stimer->config.enable = 1;
|
2019-03-14 01:13:42 +08:00
|
|
|
|
|
|
|
if (stimer->config.enable)
|
|
|
|
stimer_mark_pending(stimer, false);
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
|
|
|
|
{
|
2018-11-26 23:47:30 +08:00
|
|
|
*pconfig = stimer->config.as_uint64;
|
2015-12-01 00:22:21 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
|
|
|
|
{
|
|
|
|
*pcount = stimer->count;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
|
2018-12-11 02:47:27 +08:00
|
|
|
struct hv_message *src_msg, bool no_retry)
|
2015-12-01 00:22:21 +08:00
|
|
|
{
|
2021-01-26 21:48:06 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
|
2018-12-11 02:47:26 +08:00
|
|
|
int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
|
|
|
|
gfn_t msg_page_gfn;
|
|
|
|
struct hv_message_header hv_hdr;
|
2015-12-01 00:22:21 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
|
|
|
|
return -ENOENT;
|
|
|
|
|
2018-12-11 02:47:26 +08:00
|
|
|
msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
|
2015-12-01 00:22:21 +08:00
|
|
|
|
2018-12-11 02:47:26 +08:00
|
|
|
/*
|
|
|
|
* Strictly following the spec-mandated ordering would assume setting
|
|
|
|
* .msg_pending before checking .message_type. However, this function
|
|
|
|
* is only called in vcpu context so the entire update is atomic from
|
|
|
|
* guest POV and thus the exact order here doesn't matter.
|
|
|
|
*/
|
|
|
|
r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
|
|
|
|
msg_off + offsetof(struct hv_message,
|
|
|
|
header.message_type),
|
|
|
|
sizeof(hv_hdr.message_type));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (hv_hdr.message_type != HVMSG_NONE) {
|
2018-12-11 02:47:27 +08:00
|
|
|
if (no_retry)
|
|
|
|
return 0;
|
|
|
|
|
2018-12-11 02:47:26 +08:00
|
|
|
hv_hdr.message_flags.msg_pending = 1;
|
|
|
|
r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
|
|
|
|
&hv_hdr.message_flags,
|
|
|
|
msg_off +
|
|
|
|
offsetof(struct hv_message,
|
|
|
|
header.message_flags),
|
|
|
|
sizeof(hv_hdr.message_flags));
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
return -EAGAIN;
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
2018-12-11 02:47:26 +08:00
|
|
|
|
|
|
|
r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
|
|
|
|
sizeof(src_msg->header) +
|
|
|
|
src_msg->header.payload_size);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = synic_set_irq(synic, sint);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
|
|
|
|
2015-12-28 23:27:21 +08:00
|
|
|
static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
|
2015-12-01 00:22:21 +08:00
|
|
|
{
|
2021-01-26 21:48:07 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
|
2015-12-01 00:22:21 +08:00
|
|
|
struct hv_message *msg = &stimer->msg;
|
|
|
|
struct hv_timer_message_payload *payload =
|
|
|
|
(struct hv_timer_message_payload *)&msg->u.payload;
|
|
|
|
|
2018-12-11 02:47:27 +08:00
|
|
|
/*
|
|
|
|
* To avoid piling up periodic ticks, don't retry message
|
|
|
|
* delivery for them (within "lazy" lost ticks policy).
|
|
|
|
*/
|
2018-11-26 23:47:30 +08:00
|
|
|
bool no_retry = stimer->config.periodic;
|
2018-12-11 02:47:27 +08:00
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
payload->expiration_time = stimer->exp_time;
|
|
|
|
payload->delivery_time = get_time_ref_counter(vcpu->kvm);
|
2021-01-26 21:48:06 +08:00
|
|
|
return synic_deliver_msg(to_hv_synic(vcpu),
|
2018-11-26 23:47:30 +08:00
|
|
|
stimer->config.sintx, msg,
|
2018-12-11 02:47:27 +08:00
|
|
|
no_retry);
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
|
|
|
|
2018-11-26 23:47:31 +08:00
|
|
|
static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
|
|
|
|
{
|
2021-01-26 21:48:07 +08:00
|
|
|
struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
|
2018-11-26 23:47:31 +08:00
|
|
|
struct kvm_lapic_irq irq = {
|
|
|
|
.delivery_mode = APIC_DM_FIXED,
|
|
|
|
.vector = stimer->config.apic_vector
|
|
|
|
};
|
|
|
|
|
2019-09-16 15:42:32 +08:00
|
|
|
if (lapic_in_kernel(vcpu))
|
|
|
|
return !kvm_apic_set_irq(vcpu, &irq, NULL);
|
|
|
|
return 0;
|
2018-11-26 23:47:31 +08:00
|
|
|
}
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
|
|
|
|
{
|
2018-11-26 23:47:31 +08:00
|
|
|
int r, direct = stimer->config.direct_mode;
|
2015-12-23 21:54:00 +08:00
|
|
|
|
2015-12-28 23:27:21 +08:00
|
|
|
stimer->msg_pending = true;
|
2018-11-26 23:47:31 +08:00
|
|
|
if (!direct)
|
|
|
|
r = stimer_send_msg(stimer);
|
|
|
|
else
|
|
|
|
r = stimer_notify_direct(stimer);
|
2021-01-26 21:48:07 +08:00
|
|
|
trace_kvm_hv_stimer_expiration(hv_stimer_to_vcpu(stimer)->vcpu_id,
|
2018-11-26 23:47:31 +08:00
|
|
|
stimer->index, direct, r);
|
2015-12-23 21:54:00 +08:00
|
|
|
if (!r) {
|
2015-12-28 23:27:21 +08:00
|
|
|
stimer->msg_pending = false;
|
2018-11-26 23:47:30 +08:00
|
|
|
if (!(stimer->config.periodic))
|
|
|
|
stimer->config.enable = 0;
|
2015-12-28 23:27:21 +08:00
|
|
|
}
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2021-01-26 21:48:05 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2015-12-01 00:22:21 +08:00
|
|
|
struct kvm_vcpu_hv_stimer *stimer;
|
2015-12-28 23:27:24 +08:00
|
|
|
u64 time_now, exp_time;
|
2015-12-01 00:22:21 +08:00
|
|
|
int i;
|
|
|
|
|
2021-01-26 21:48:12 +08:00
|
|
|
if (!hv_vcpu)
|
|
|
|
return;
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
|
|
|
|
if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
|
|
|
|
stimer = &hv_vcpu->stimer[i];
|
2018-11-26 23:47:30 +08:00
|
|
|
if (stimer->config.enable) {
|
2015-12-28 23:27:24 +08:00
|
|
|
exp_time = stimer->exp_time;
|
|
|
|
|
|
|
|
if (exp_time) {
|
|
|
|
time_now =
|
|
|
|
get_time_ref_counter(vcpu->kvm);
|
|
|
|
if (time_now >= exp_time)
|
|
|
|
stimer_expiration(stimer);
|
|
|
|
}
|
2015-12-28 23:27:21 +08:00
|
|
|
|
2018-11-26 23:47:30 +08:00
|
|
|
if ((stimer->config.enable) &&
|
2017-07-20 22:26:40 +08:00
|
|
|
stimer->count) {
|
|
|
|
if (!stimer->msg_pending)
|
|
|
|
stimer_start(stimer);
|
|
|
|
} else
|
2015-12-28 23:27:21 +08:00
|
|
|
stimer_cleanup(stimer);
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2021-01-26 21:48:05 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2015-12-01 00:22:21 +08:00
|
|
|
int i;
|
|
|
|
|
2021-01-26 21:48:15 +08:00
|
|
|
if (!hv_vcpu)
|
|
|
|
return;
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
|
|
|
|
stimer_cleanup(&hv_vcpu->stimer[i]);
|
2021-01-26 21:48:13 +08:00
|
|
|
|
|
|
|
kfree(hv_vcpu);
|
|
|
|
vcpu->arch.hyperv = NULL;
|
2015-12-01 00:22:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-17 00:49:59 +08:00
|
|
|
bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2021-01-26 21:48:11 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
|
|
|
|
2021-01-26 21:48:12 +08:00
|
|
|
if (!hv_vcpu)
|
|
|
|
return false;
|
|
|
|
|
2021-01-26 21:48:11 +08:00
|
|
|
if (!(hv_vcpu->hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
|
2018-10-17 00:49:59 +08:00
|
|
|
return false;
|
|
|
|
return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled);
|
|
|
|
|
|
|
|
bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu,
|
|
|
|
struct hv_vp_assist_page *assist_page)
|
|
|
|
{
|
|
|
|
if (!kvm_hv_assist_page_enabled(vcpu))
|
|
|
|
return false;
|
|
|
|
return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
|
|
|
|
assist_page, sizeof(*assist_page));
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page);
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
|
|
|
|
{
|
|
|
|
struct hv_message *msg = &stimer->msg;
|
|
|
|
struct hv_timer_message_payload *payload =
|
|
|
|
(struct hv_timer_message_payload *)&msg->u.payload;
|
|
|
|
|
|
|
|
memset(&msg->header, 0, sizeof(msg->header));
|
|
|
|
msg->header.message_type = HVMSG_TIMER_EXPIRED;
|
|
|
|
msg->header.payload_size = sizeof(*payload);
|
|
|
|
|
|
|
|
payload->timer_index = stimer->index;
|
|
|
|
payload->expiration_time = 0;
|
|
|
|
payload->delivery_time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
|
|
|
|
{
|
|
|
|
memset(stimer, 0, sizeof(*stimer));
|
|
|
|
stimer->index = timer_index;
|
|
|
|
hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
|
|
|
|
stimer->timer.function = stimer_timer_callback;
|
|
|
|
stimer_prepare_msg(stimer);
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:15 +08:00
|
|
|
static int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
|
2015-11-10 20:36:34 +08:00
|
|
|
{
|
2021-01-26 21:48:13 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu;
|
2015-12-01 00:22:21 +08:00
|
|
|
int i;
|
|
|
|
|
2021-01-26 21:48:13 +08:00
|
|
|
hv_vcpu = kzalloc(sizeof(struct kvm_vcpu_hv), GFP_KERNEL_ACCOUNT);
|
|
|
|
if (!hv_vcpu)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
vcpu->arch.hyperv = hv_vcpu;
|
|
|
|
hv_vcpu->vcpu = vcpu;
|
|
|
|
|
2015-12-01 00:22:21 +08:00
|
|
|
synic_init(&hv_vcpu->synic);
|
|
|
|
|
|
|
|
bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
|
|
|
|
stimer_init(&hv_vcpu->stimer[i], i);
|
2021-01-26 21:48:13 +08:00
|
|
|
|
2021-09-11 02:32:19 +08:00
|
|
|
hv_vcpu->vp_index = vcpu->vcpu_idx;
|
2021-01-26 21:48:15 +08:00
|
|
|
|
2021-01-26 21:48:13 +08:00
|
|
|
return 0;
|
2015-11-10 20:36:34 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:15 +08:00
|
|
|
int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
|
2017-07-14 22:13:20 +08:00
|
|
|
{
|
2021-01-26 21:48:15 +08:00
|
|
|
struct kvm_vcpu_hv_synic *synic;
|
|
|
|
int r;
|
2017-07-14 22:13:20 +08:00
|
|
|
|
2021-01-26 21:48:15 +08:00
|
|
|
if (!to_hv_vcpu(vcpu)) {
|
|
|
|
r = kvm_hv_vcpu_init(vcpu);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
}
|
2017-07-14 22:13:20 +08:00
|
|
|
|
2021-01-26 21:48:15 +08:00
|
|
|
synic = to_hv_synic(vcpu);
|
kvm: x86: hyperv: add KVM_CAP_HYPERV_SYNIC2
There is a flaw in the Hyper-V SynIC implementation in KVM: when message
page or event flags page is enabled by setting the corresponding msr,
KVM zeroes it out. This is problematic because on migration the
corresponding MSRs are loaded on the destination, so the content of
those pages is lost.
This went unnoticed so far because the only user of those pages was
in-KVM hyperv synic timers, which could continue working despite that
zeroing.
Newer QEMU uses those pages for Hyper-V VMBus implementation, and
zeroing them breaks the migration.
Besides, in newer QEMU the content of those pages is fully managed by
QEMU, so zeroing them is undesirable even when writing the MSRs from the
guest side.
To support this new scheme, introduce a new capability,
KVM_CAP_HYPERV_SYNIC2, which, when enabled, makes sure that the synic
pages aren't zeroed out in KVM.
Signed-off-by: Roman Kagan <rkagan@virtuozzo.com>
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
2017-06-22 21:51:01 +08:00
|
|
|
|
|
|
|
synic->active = true;
|
|
|
|
synic->dont_zero_synic_pages = dont_zero_synic_pages;
|
2020-07-17 20:52:38 +08:00
|
|
|
synic->control = HV_SYNIC_CONTROL_ENABLE;
|
2015-11-10 20:36:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
static bool kvm_hv_msr_partition_wide(u32 msr)
|
|
|
|
{
|
|
|
|
bool r = false;
|
|
|
|
|
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_GUEST_OS_ID:
|
|
|
|
case HV_X64_MSR_HYPERCALL:
|
|
|
|
case HV_X64_MSR_REFERENCE_TSC:
|
|
|
|
case HV_X64_MSR_TIME_REF_COUNT:
|
2015-07-03 20:01:37 +08:00
|
|
|
case HV_X64_MSR_CRASH_CTL:
|
|
|
|
case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
|
2015-09-16 17:29:48 +08:00
|
|
|
case HV_X64_MSR_RESET:
|
2018-03-01 22:15:12 +08:00
|
|
|
case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_CONTROL:
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_STATUS:
|
2020-05-29 21:45:40 +08:00
|
|
|
case HV_X64_MSR_SYNDBG_OPTIONS:
|
|
|
|
case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
|
2015-07-03 20:01:34 +08:00
|
|
|
r = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
static int kvm_hv_msr_get_crash_data(struct kvm *kvm, u32 index, u64 *pdata)
|
2015-07-03 20:01:37 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2019-12-12 04:47:42 +08:00
|
|
|
size_t size = ARRAY_SIZE(hv->hv_crash_param);
|
2015-07-03 20:01:37 +08:00
|
|
|
|
2019-12-12 04:47:42 +08:00
|
|
|
if (WARN_ON_ONCE(index >= size))
|
2015-07-03 20:01:37 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-12-12 04:47:42 +08:00
|
|
|
*pdata = hv->hv_crash_param[array_index_nospec(index, size)];
|
2015-07-03 20:01:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
static int kvm_hv_msr_get_crash_ctl(struct kvm *kvm, u64 *pdata)
|
2015-07-03 20:01:37 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2015-07-03 20:01:37 +08:00
|
|
|
|
|
|
|
*pdata = hv->hv_crash_ctl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
static int kvm_hv_msr_set_crash_ctl(struct kvm *kvm, u64 data)
|
2015-07-03 20:01:37 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2015-07-03 20:01:37 +08:00
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
|
2015-07-03 20:01:37 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
static int kvm_hv_msr_set_crash_data(struct kvm *kvm, u32 index, u64 data)
|
2015-07-03 20:01:37 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2019-12-12 04:47:42 +08:00
|
|
|
size_t size = ARRAY_SIZE(hv->hv_crash_param);
|
2015-07-03 20:01:37 +08:00
|
|
|
|
2019-12-12 04:47:42 +08:00
|
|
|
if (WARN_ON_ONCE(index >= size))
|
2015-07-03 20:01:37 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-12-12 04:47:42 +08:00
|
|
|
hv->hv_crash_param[array_index_nospec(index, size)] = data;
|
2015-07-03 20:01:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:54:12 +08:00
|
|
|
/*
|
|
|
|
* The kvmclock and Hyper-V TSC page use similar formulas, and converting
|
|
|
|
* between them is possible:
|
|
|
|
*
|
|
|
|
* kvmclock formula:
|
|
|
|
* nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
|
|
|
|
* + system_time
|
|
|
|
*
|
|
|
|
* Hyper-V formula:
|
|
|
|
* nsec/100 = ticks * scale / 2^64 + offset
|
|
|
|
*
|
|
|
|
* When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
|
|
|
|
* By dividing the kvmclock formula by 100 and equating what's left we get:
|
|
|
|
* ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
|
|
|
|
* scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
|
|
|
|
* scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
|
|
|
|
*
|
|
|
|
* Now expand the kvmclock formula and divide by 100:
|
|
|
|
* nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
|
|
|
|
* - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
|
|
|
|
* + system_time
|
|
|
|
* nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
|
|
|
|
* - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
|
|
|
|
* + system_time / 100
|
|
|
|
*
|
|
|
|
* Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
|
|
|
|
* nsec/100 = ticks * scale / 2^64
|
|
|
|
* - tsc_timestamp * scale / 2^64
|
|
|
|
* + system_time / 100
|
|
|
|
*
|
|
|
|
* Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
|
|
|
|
* offset = system_time / 100 - tsc_timestamp * scale / 2^64
|
|
|
|
*
|
|
|
|
* These two equivalencies are implemented in this function.
|
|
|
|
*/
|
|
|
|
static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
|
2020-04-23 03:57:34 +08:00
|
|
|
struct ms_hyperv_tsc_page *tsc_ref)
|
2016-02-08 19:54:12 +08:00
|
|
|
{
|
|
|
|
u64 max_mul;
|
|
|
|
|
|
|
|
if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if scale would overflow, if so we use the time ref counter
|
|
|
|
* tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
|
|
|
|
* tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
|
|
|
|
* tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
|
|
|
|
*/
|
|
|
|
max_mul = 100ull << (32 - hv_clock->tsc_shift);
|
|
|
|
if (hv_clock->tsc_to_system_mul >= max_mul)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise compute the scale and offset according to the formulas
|
|
|
|
* derived above.
|
|
|
|
*/
|
|
|
|
tsc_ref->tsc_scale =
|
|
|
|
mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
|
|
|
|
hv_clock->tsc_to_system_mul,
|
|
|
|
100);
|
|
|
|
|
|
|
|
tsc_ref->tsc_offset = hv_clock->system_time;
|
|
|
|
do_div(tsc_ref->tsc_offset, 100);
|
|
|
|
tsc_ref->tsc_offset -=
|
|
|
|
mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
KVM: x86: hyper-v: Don't touch TSC page values when guest opted for re-enlightenment
When guest opts for re-enlightenment notifications upon migration, it is
in its right to assume that TSC page values never change (as they're only
supposed to change upon migration and the host has to keep things as they
are before it receives confirmation from the guest). This is mostly true
until the guest is migrated somewhere. KVM userspace (e.g. QEMU) will
trigger masterclock update by writing to HV_X64_MSR_REFERENCE_TSC, by
calling KVM_SET_CLOCK,... and as TSC value and kvmclock reading drift
apart (even slightly), the update causes TSC page values to change.
The issue at hand is that when Hyper-V is migrated, it uses stale (cached)
TSC page values to compute the difference between its own clocksource
(provided by KVM) and its guests' TSC pages to program synthetic timers
and in some cases, when TSC page is updated, this puts all stimer
expirations in the past. This, in its turn, causes an interrupt storm
and L2 guests not making much forward progress.
Note, KVM doesn't fully implement re-enlightenment notification. Basically,
the support for reenlightenment MSRs is just a stub and userspace is only
expected to expose the feature when TSC scaling on the expected destination
hosts is available. With TSC scaling, no real re-enlightenment is needed
as TSC frequency doesn't change. With TSC scaling becoming ubiquitous, it
likely makes little sense to fully implement re-enlightenment in KVM.
Prevent TSC page from being updated after migration. In case it's not the
guest who's initiating the change and when TSC page is already enabled,
just keep it as it is: TSC value is supposed to be preserved across
migration and TSC frequency can't change with re-enlightenment enabled.
The guest is doomed anyway if any of this is not true.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Message-Id: <20210316143736.964151-5-vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-03-16 22:37:36 +08:00
|
|
|
/*
|
|
|
|
* Don't touch TSC page values if the guest has opted for TSC emulation after
|
|
|
|
* migration. KVM doesn't fully support reenlightenment notifications and TSC
|
|
|
|
* access emulation and Hyper-V is known to expect the values in TSC page to
|
|
|
|
* stay constant before TSC access emulation is disabled from guest side
|
|
|
|
* (HV_X64_MSR_TSC_EMULATION_STATUS). KVM userspace is expected to preserve TSC
|
|
|
|
* frequency and guest visible TSC value across migration (and prevent it when
|
|
|
|
* TSC scaling is unsupported).
|
|
|
|
*/
|
|
|
|
static inline bool tsc_page_update_unsafe(struct kvm_hv *hv)
|
|
|
|
{
|
|
|
|
return (hv->hv_tsc_page_status != HV_TSC_PAGE_GUEST_CHANGED) &&
|
|
|
|
hv->hv_tsc_emulation_control;
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:54:12 +08:00
|
|
|
void kvm_hv_setup_tsc_page(struct kvm *kvm,
|
|
|
|
struct pvclock_vcpu_time_info *hv_clock)
|
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2016-02-08 19:54:12 +08:00
|
|
|
u32 tsc_seq;
|
|
|
|
u64 gfn;
|
|
|
|
|
|
|
|
BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
|
2020-04-23 03:57:34 +08:00
|
|
|
BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0);
|
2016-02-08 19:54:12 +08:00
|
|
|
|
2021-03-16 22:37:35 +08:00
|
|
|
if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN ||
|
|
|
|
hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET)
|
2016-02-08 19:54:12 +08:00
|
|
|
return;
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
mutex_lock(&hv->hv_lock);
|
2016-12-12 17:12:53 +08:00
|
|
|
if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
|
|
|
|
goto out_unlock;
|
|
|
|
|
2016-02-08 19:54:12 +08:00
|
|
|
gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
|
|
|
|
/*
|
|
|
|
* Because the TSC parameters only vary when there is a
|
|
|
|
* change in the master clock, do not bother with caching.
|
|
|
|
*/
|
|
|
|
if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
|
|
|
|
&tsc_seq, sizeof(tsc_seq))))
|
2021-03-16 22:37:35 +08:00
|
|
|
goto out_err;
|
2016-02-08 19:54:12 +08:00
|
|
|
|
KVM: x86: hyper-v: Don't touch TSC page values when guest opted for re-enlightenment
When guest opts for re-enlightenment notifications upon migration, it is
in its right to assume that TSC page values never change (as they're only
supposed to change upon migration and the host has to keep things as they
are before it receives confirmation from the guest). This is mostly true
until the guest is migrated somewhere. KVM userspace (e.g. QEMU) will
trigger masterclock update by writing to HV_X64_MSR_REFERENCE_TSC, by
calling KVM_SET_CLOCK,... and as TSC value and kvmclock reading drift
apart (even slightly), the update causes TSC page values to change.
The issue at hand is that when Hyper-V is migrated, it uses stale (cached)
TSC page values to compute the difference between its own clocksource
(provided by KVM) and its guests' TSC pages to program synthetic timers
and in some cases, when TSC page is updated, this puts all stimer
expirations in the past. This, in its turn, causes an interrupt storm
and L2 guests not making much forward progress.
Note, KVM doesn't fully implement re-enlightenment notification. Basically,
the support for reenlightenment MSRs is just a stub and userspace is only
expected to expose the feature when TSC scaling on the expected destination
hosts is available. With TSC scaling, no real re-enlightenment is needed
as TSC frequency doesn't change. With TSC scaling becoming ubiquitous, it
likely makes little sense to fully implement re-enlightenment in KVM.
Prevent TSC page from being updated after migration. In case it's not the
guest who's initiating the change and when TSC page is already enabled,
just keep it as it is: TSC value is supposed to be preserved across
migration and TSC frequency can't change with re-enlightenment enabled.
The guest is doomed anyway if any of this is not true.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Message-Id: <20210316143736.964151-5-vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-03-16 22:37:36 +08:00
|
|
|
if (tsc_seq && tsc_page_update_unsafe(hv)) {
|
|
|
|
if (kvm_read_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2016-02-08 19:54:12 +08:00
|
|
|
/*
|
|
|
|
* While we're computing and writing the parameters, force the
|
|
|
|
* guest to use the time reference count MSR.
|
|
|
|
*/
|
|
|
|
hv->tsc_ref.tsc_sequence = 0;
|
|
|
|
if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
|
|
|
|
&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
|
2021-03-16 22:37:35 +08:00
|
|
|
goto out_err;
|
2016-02-08 19:54:12 +08:00
|
|
|
|
|
|
|
if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
|
2021-03-16 22:37:35 +08:00
|
|
|
goto out_err;
|
2016-02-08 19:54:12 +08:00
|
|
|
|
|
|
|
/* Ensure sequence is zero before writing the rest of the struct. */
|
|
|
|
smp_wmb();
|
|
|
|
if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
|
2021-03-16 22:37:35 +08:00
|
|
|
goto out_err;
|
2016-02-08 19:54:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now switch to the TSC page mechanism by writing the sequence.
|
|
|
|
*/
|
|
|
|
tsc_seq++;
|
|
|
|
if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
|
|
|
|
tsc_seq = 1;
|
|
|
|
|
|
|
|
/* Write the struct entirely before the non-zero sequence. */
|
|
|
|
smp_wmb();
|
|
|
|
|
|
|
|
hv->tsc_ref.tsc_sequence = tsc_seq;
|
2021-03-16 22:37:35 +08:00
|
|
|
if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
|
|
|
|
&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
|
|
|
|
goto out_unlock;
|
|
|
|
|
|
|
|
out_err:
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN;
|
2016-12-12 17:12:53 +08:00
|
|
|
out_unlock:
|
2021-01-26 21:48:09 +08:00
|
|
|
mutex_unlock(&hv->hv_lock);
|
2016-02-08 19:54:12 +08:00
|
|
|
}
|
|
|
|
|
2021-03-16 22:37:34 +08:00
|
|
|
void kvm_hv_invalidate_tsc_page(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
|
|
|
u64 gfn;
|
2021-05-18 20:00:34 +08:00
|
|
|
int idx;
|
2021-03-16 22:37:34 +08:00
|
|
|
|
2021-03-16 22:37:35 +08:00
|
|
|
if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN ||
|
KVM: x86: hyper-v: Don't touch TSC page values when guest opted for re-enlightenment
When guest opts for re-enlightenment notifications upon migration, it is
in its right to assume that TSC page values never change (as they're only
supposed to change upon migration and the host has to keep things as they
are before it receives confirmation from the guest). This is mostly true
until the guest is migrated somewhere. KVM userspace (e.g. QEMU) will
trigger masterclock update by writing to HV_X64_MSR_REFERENCE_TSC, by
calling KVM_SET_CLOCK,... and as TSC value and kvmclock reading drift
apart (even slightly), the update causes TSC page values to change.
The issue at hand is that when Hyper-V is migrated, it uses stale (cached)
TSC page values to compute the difference between its own clocksource
(provided by KVM) and its guests' TSC pages to program synthetic timers
and in some cases, when TSC page is updated, this puts all stimer
expirations in the past. This, in its turn, causes an interrupt storm
and L2 guests not making much forward progress.
Note, KVM doesn't fully implement re-enlightenment notification. Basically,
the support for reenlightenment MSRs is just a stub and userspace is only
expected to expose the feature when TSC scaling on the expected destination
hosts is available. With TSC scaling, no real re-enlightenment is needed
as TSC frequency doesn't change. With TSC scaling becoming ubiquitous, it
likely makes little sense to fully implement re-enlightenment in KVM.
Prevent TSC page from being updated after migration. In case it's not the
guest who's initiating the change and when TSC page is already enabled,
just keep it as it is: TSC value is supposed to be preserved across
migration and TSC frequency can't change with re-enlightenment enabled.
The guest is doomed anyway if any of this is not true.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Message-Id: <20210316143736.964151-5-vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2021-03-16 22:37:36 +08:00
|
|
|
hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET ||
|
|
|
|
tsc_page_update_unsafe(hv))
|
2021-03-16 22:37:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&hv->hv_lock);
|
|
|
|
|
|
|
|
if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
|
|
|
|
goto out_unlock;
|
|
|
|
|
2021-03-16 22:37:35 +08:00
|
|
|
/* Preserve HV_TSC_PAGE_GUEST_CHANGED/HV_TSC_PAGE_HOST_CHANGED states */
|
|
|
|
if (hv->hv_tsc_page_status == HV_TSC_PAGE_SET)
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_UPDATING;
|
|
|
|
|
2021-03-16 22:37:34 +08:00
|
|
|
gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
|
|
|
|
|
|
|
|
hv->tsc_ref.tsc_sequence = 0;
|
2021-05-18 20:00:34 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Take the srcu lock as memslots will be accessed to check the gfn
|
|
|
|
* cache generation against the memslots generation.
|
|
|
|
*/
|
|
|
|
idx = srcu_read_lock(&kvm->srcu);
|
2021-03-16 22:37:35 +08:00
|
|
|
if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
|
|
|
|
&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN;
|
2021-05-18 20:00:34 +08:00
|
|
|
srcu_read_unlock(&kvm->srcu, idx);
|
2021-03-16 22:37:34 +08:00
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&hv->hv_lock);
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:51:38 +08:00
|
|
|
|
|
|
|
static bool hv_check_msr_access(struct kvm_vcpu_hv *hv_vcpu, u32 msr)
|
|
|
|
{
|
2021-05-21 17:51:39 +08:00
|
|
|
if (!hv_vcpu->enforce_cpuid)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_GUEST_OS_ID:
|
|
|
|
case HV_X64_MSR_HYPERCALL:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_HYPERCALL_AVAILABLE;
|
2021-05-21 17:51:40 +08:00
|
|
|
case HV_X64_MSR_VP_RUNTIME:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_VP_RUNTIME_AVAILABLE;
|
2021-05-21 17:51:41 +08:00
|
|
|
case HV_X64_MSR_TIME_REF_COUNT:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_TIME_REF_COUNT_AVAILABLE;
|
2021-05-21 17:51:42 +08:00
|
|
|
case HV_X64_MSR_VP_INDEX:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_VP_INDEX_AVAILABLE;
|
2021-05-21 17:51:43 +08:00
|
|
|
case HV_X64_MSR_RESET:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_RESET_AVAILABLE;
|
2021-05-21 17:51:44 +08:00
|
|
|
case HV_X64_MSR_REFERENCE_TSC:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_REFERENCE_TSC_AVAILABLE;
|
2021-05-21 17:51:45 +08:00
|
|
|
case HV_X64_MSR_SCONTROL:
|
|
|
|
case HV_X64_MSR_SVERSION:
|
|
|
|
case HV_X64_MSR_SIEFP:
|
|
|
|
case HV_X64_MSR_SIMP:
|
|
|
|
case HV_X64_MSR_EOM:
|
|
|
|
case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_SYNIC_AVAILABLE;
|
2021-05-21 17:51:46 +08:00
|
|
|
case HV_X64_MSR_STIMER0_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER1_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER2_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER3_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER0_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER1_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER2_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER3_COUNT:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_SYNTIMER_AVAILABLE;
|
2021-05-21 17:51:47 +08:00
|
|
|
case HV_X64_MSR_EOI:
|
|
|
|
case HV_X64_MSR_ICR:
|
|
|
|
case HV_X64_MSR_TPR:
|
|
|
|
case HV_X64_MSR_VP_ASSIST_PAGE:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_MSR_APIC_ACCESS_AVAILABLE;
|
|
|
|
break;
|
2021-05-21 17:51:48 +08:00
|
|
|
case HV_X64_MSR_TSC_FREQUENCY:
|
|
|
|
case HV_X64_MSR_APIC_FREQUENCY:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_ACCESS_FREQUENCY_MSRS;
|
2021-05-21 17:51:49 +08:00
|
|
|
case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_CONTROL:
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_STATUS:
|
|
|
|
return hv_vcpu->cpuid_cache.features_eax &
|
|
|
|
HV_ACCESS_REENLIGHTENMENT;
|
2021-05-21 17:51:50 +08:00
|
|
|
case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
|
|
|
|
case HV_X64_MSR_CRASH_CTL:
|
|
|
|
return hv_vcpu->cpuid_cache.features_edx &
|
|
|
|
HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
|
2021-05-21 17:51:51 +08:00
|
|
|
case HV_X64_MSR_SYNDBG_OPTIONS:
|
|
|
|
case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
|
|
|
|
return hv_vcpu->cpuid_cache.features_edx &
|
|
|
|
HV_FEATURE_DEBUG_MSRS_AVAILABLE;
|
2021-05-21 17:51:39 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:51:52 +08:00
|
|
|
return false;
|
2021-05-21 17:51:38 +08:00
|
|
|
}
|
|
|
|
|
2015-07-03 20:01:37 +08:00
|
|
|
static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
|
|
|
|
bool host)
|
2015-07-03 20:01:34 +08:00
|
|
|
{
|
|
|
|
struct kvm *kvm = vcpu->kvm;
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2021-05-21 17:51:38 +08:00
|
|
|
if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
|
|
|
|
return 1;
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_GUEST_OS_ID:
|
|
|
|
hv->hv_guest_os_id = data;
|
|
|
|
/* setting guest os id to zero disables hypercall page */
|
|
|
|
if (!hv->hv_guest_os_id)
|
|
|
|
hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_HYPERCALL: {
|
2018-06-13 21:55:44 +08:00
|
|
|
u8 instructions[9];
|
|
|
|
int i = 0;
|
|
|
|
u64 addr;
|
2015-07-03 20:01:34 +08:00
|
|
|
|
|
|
|
/* if guest os id is not set hypercall should remain disabled */
|
|
|
|
if (!hv->hv_guest_os_id)
|
|
|
|
break;
|
|
|
|
if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
|
|
|
|
hv->hv_hypercall = data;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-13 21:55:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If Xen and Hyper-V hypercalls are both enabled, disambiguate
|
|
|
|
* the same way Xen itself does, by setting the bit 31 of EAX
|
|
|
|
* which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just
|
|
|
|
* going to be clobbered on 64-bit.
|
|
|
|
*/
|
|
|
|
if (kvm_xen_hypercall_enabled(kvm)) {
|
|
|
|
/* orl $0x80000000, %eax */
|
|
|
|
instructions[i++] = 0x0d;
|
|
|
|
instructions[i++] = 0x00;
|
|
|
|
instructions[i++] = 0x00;
|
|
|
|
instructions[i++] = 0x00;
|
|
|
|
instructions[i++] = 0x80;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vmcall/vmmcall */
|
|
|
|
static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i);
|
|
|
|
i += 3;
|
|
|
|
|
|
|
|
/* ret */
|
|
|
|
((unsigned char *)instructions)[i++] = 0xc3;
|
|
|
|
|
|
|
|
addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK;
|
|
|
|
if (kvm_vcpu_write_guest(vcpu, addr, instructions, i))
|
2015-07-03 20:01:34 +08:00
|
|
|
return 1;
|
|
|
|
hv->hv_hypercall = data;
|
|
|
|
break;
|
|
|
|
}
|
2016-02-08 19:54:12 +08:00
|
|
|
case HV_X64_MSR_REFERENCE_TSC:
|
2015-07-03 20:01:34 +08:00
|
|
|
hv->hv_tsc_page = data;
|
2021-03-16 22:37:35 +08:00
|
|
|
if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) {
|
|
|
|
if (!host)
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_GUEST_CHANGED;
|
|
|
|
else
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED;
|
2016-02-08 19:54:12 +08:00
|
|
|
kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
|
2021-03-16 22:37:35 +08:00
|
|
|
} else {
|
|
|
|
hv->hv_tsc_page_status = HV_TSC_PAGE_UNSET;
|
|
|
|
}
|
2015-07-03 20:01:34 +08:00
|
|
|
break;
|
2015-07-03 20:01:37 +08:00
|
|
|
case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
|
2021-01-26 21:48:09 +08:00
|
|
|
return kvm_hv_msr_set_crash_data(kvm,
|
2015-07-03 20:01:37 +08:00
|
|
|
msr - HV_X64_MSR_CRASH_P0,
|
|
|
|
data);
|
|
|
|
case HV_X64_MSR_CRASH_CTL:
|
2021-01-26 21:48:09 +08:00
|
|
|
if (host)
|
|
|
|
return kvm_hv_msr_set_crash_ctl(kvm, data);
|
|
|
|
|
|
|
|
if (data & HV_CRASH_CTL_CRASH_NOTIFY) {
|
|
|
|
vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
|
|
|
|
hv->hv_crash_param[0],
|
|
|
|
hv->hv_crash_param[1],
|
|
|
|
hv->hv_crash_param[2],
|
|
|
|
hv->hv_crash_param[3],
|
|
|
|
hv->hv_crash_param[4]);
|
|
|
|
|
|
|
|
/* Send notification about crash to user space */
|
|
|
|
kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
|
|
|
|
}
|
|
|
|
break;
|
2015-09-16 17:29:48 +08:00
|
|
|
case HV_X64_MSR_RESET:
|
|
|
|
if (data == 1) {
|
|
|
|
vcpu_debug(vcpu, "hyper-v reset requested\n");
|
|
|
|
kvm_make_request(KVM_REQ_HV_RESET, vcpu);
|
|
|
|
}
|
|
|
|
break;
|
2018-03-01 22:15:12 +08:00
|
|
|
case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
|
|
|
|
hv->hv_reenlightenment_control = data;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_CONTROL:
|
|
|
|
hv->hv_tsc_emulation_control = data;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_STATUS:
|
2021-03-16 22:37:33 +08:00
|
|
|
if (data && !host)
|
|
|
|
return 1;
|
|
|
|
|
2018-03-01 22:15:12 +08:00
|
|
|
hv->hv_tsc_emulation_status = data;
|
|
|
|
break;
|
2018-07-26 19:01:52 +08:00
|
|
|
case HV_X64_MSR_TIME_REF_COUNT:
|
|
|
|
/* read-only, but still ignore it if host-initiated */
|
|
|
|
if (!host)
|
|
|
|
return 1;
|
|
|
|
break;
|
2020-05-29 21:45:40 +08:00
|
|
|
case HV_X64_MSR_SYNDBG_OPTIONS:
|
|
|
|
case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
|
|
|
|
return syndbg_set_msr(vcpu, msr, data, host);
|
2015-07-03 20:01:34 +08:00
|
|
|
default:
|
2019-12-11 14:26:24 +08:00
|
|
|
vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
|
2015-07-03 20:01:34 +08:00
|
|
|
msr, data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-16 17:29:50 +08:00
|
|
|
/* Calculate cpu time spent by current task in 100ns units */
|
|
|
|
static u64 current_task_runtime_100ns(void)
|
|
|
|
{
|
2017-01-31 11:09:23 +08:00
|
|
|
u64 utime, stime;
|
2015-09-16 17:29:50 +08:00
|
|
|
|
|
|
|
task_cputime_adjusted(current, &utime, &stime);
|
2017-01-31 11:09:23 +08:00
|
|
|
|
|
|
|
return div_u64(utime + stime, 100);
|
2015-09-16 17:29:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
|
2015-07-03 20:01:34 +08:00
|
|
|
{
|
2021-01-26 21:48:11 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2021-05-21 17:51:38 +08:00
|
|
|
if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
|
|
|
|
return 1;
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
switch (msr) {
|
2018-09-27 01:02:56 +08:00
|
|
|
case HV_X64_MSR_VP_INDEX: {
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
|
2018-09-27 01:02:56 +08:00
|
|
|
u32 new_vp_index = (u32)data;
|
|
|
|
|
|
|
|
if (!host || new_vp_index >= KVM_MAX_VCPUS)
|
2017-07-14 22:13:20 +08:00
|
|
|
return 1;
|
2018-09-27 01:02:56 +08:00
|
|
|
|
|
|
|
if (new_vp_index == hv_vcpu->vp_index)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The VP index is initialized to vcpu_index by
|
|
|
|
* kvm_hv_vcpu_postcreate so they initially match. Now the
|
|
|
|
* VP index is changing, adjust num_mismatched_vp_indexes if
|
|
|
|
* it now matches or no longer matches vcpu_idx.
|
|
|
|
*/
|
2021-09-11 02:32:19 +08:00
|
|
|
if (hv_vcpu->vp_index == vcpu->vcpu_idx)
|
2018-09-27 01:02:56 +08:00
|
|
|
atomic_inc(&hv->num_mismatched_vp_indexes);
|
2021-09-11 02:32:19 +08:00
|
|
|
else if (new_vp_index == vcpu->vcpu_idx)
|
2018-09-27 01:02:56 +08:00
|
|
|
atomic_dec(&hv->num_mismatched_vp_indexes);
|
|
|
|
|
|
|
|
hv_vcpu->vp_index = new_vp_index;
|
2017-07-14 22:13:20 +08:00
|
|
|
break;
|
2018-09-27 01:02:56 +08:00
|
|
|
}
|
2018-03-20 22:02:07 +08:00
|
|
|
case HV_X64_MSR_VP_ASSIST_PAGE: {
|
2015-07-03 20:01:34 +08:00
|
|
|
u64 gfn;
|
|
|
|
unsigned long addr;
|
|
|
|
|
2018-03-20 22:02:07 +08:00
|
|
|
if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
|
2018-09-27 01:02:55 +08:00
|
|
|
hv_vcpu->hv_vapic = data;
|
2021-11-08 23:28:18 +08:00
|
|
|
if (kvm_lapic_set_pv_eoi(vcpu, 0, 0))
|
2015-07-03 20:01:34 +08:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
2018-03-20 22:02:07 +08:00
|
|
|
gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
|
2015-07-03 20:01:34 +08:00
|
|
|
addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
|
|
|
|
if (kvm_is_error_hva(addr))
|
|
|
|
return 1;
|
2018-10-17 00:50:05 +08:00
|
|
|
|
|
|
|
/*
|
2019-12-11 14:26:22 +08:00
|
|
|
* Clear apic_assist portion of struct hv_vp_assist_page
|
2018-10-17 00:50:05 +08:00
|
|
|
* only, there can be valuable data in the rest which needs
|
|
|
|
* to be preserved e.g. on migration.
|
|
|
|
*/
|
2020-02-19 06:32:46 +08:00
|
|
|
if (__put_user(0, (u32 __user *)addr))
|
2015-07-03 20:01:34 +08:00
|
|
|
return 1;
|
2018-09-27 01:02:55 +08:00
|
|
|
hv_vcpu->hv_vapic = data;
|
2015-07-03 20:01:34 +08:00
|
|
|
kvm_vcpu_mark_page_dirty(vcpu, gfn);
|
2021-11-08 23:28:18 +08:00
|
|
|
if (kvm_lapic_set_pv_eoi(vcpu,
|
2018-10-17 00:49:59 +08:00
|
|
|
gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
|
|
|
|
sizeof(struct hv_vp_assist_page)))
|
2015-07-03 20:01:34 +08:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HV_X64_MSR_EOI:
|
|
|
|
return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
|
|
|
|
case HV_X64_MSR_ICR:
|
|
|
|
return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
|
|
|
|
case HV_X64_MSR_TPR:
|
|
|
|
return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
|
2015-09-16 17:29:50 +08:00
|
|
|
case HV_X64_MSR_VP_RUNTIME:
|
|
|
|
if (!host)
|
|
|
|
return 1;
|
2018-09-27 01:02:55 +08:00
|
|
|
hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
|
2015-09-16 17:29:50 +08:00
|
|
|
break;
|
2015-11-10 20:36:34 +08:00
|
|
|
case HV_X64_MSR_SCONTROL:
|
|
|
|
case HV_X64_MSR_SVERSION:
|
|
|
|
case HV_X64_MSR_SIEFP:
|
|
|
|
case HV_X64_MSR_SIMP:
|
|
|
|
case HV_X64_MSR_EOM:
|
|
|
|
case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
|
2021-01-26 21:48:06 +08:00
|
|
|
return synic_set_msr(to_hv_synic(vcpu), msr, data, host);
|
2015-12-01 00:22:21 +08:00
|
|
|
case HV_X64_MSR_STIMER0_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER1_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER2_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER3_CONFIG: {
|
|
|
|
int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
return stimer_set_config(to_hv_stimer(vcpu, timer_index),
|
2015-12-01 00:22:21 +08:00
|
|
|
data, host);
|
|
|
|
}
|
|
|
|
case HV_X64_MSR_STIMER0_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER1_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER2_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER3_COUNT: {
|
|
|
|
int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
return stimer_set_count(to_hv_stimer(vcpu, timer_index),
|
2015-12-01 00:22:21 +08:00
|
|
|
data, host);
|
|
|
|
}
|
2018-07-26 19:01:52 +08:00
|
|
|
case HV_X64_MSR_TSC_FREQUENCY:
|
|
|
|
case HV_X64_MSR_APIC_FREQUENCY:
|
|
|
|
/* read-only, but still ignore it if host-initiated */
|
|
|
|
if (!host)
|
|
|
|
return 1;
|
|
|
|
break;
|
2015-07-03 20:01:34 +08:00
|
|
|
default:
|
2019-12-11 14:26:24 +08:00
|
|
|
vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
|
2015-07-03 20:01:34 +08:00
|
|
|
msr, data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-29 21:45:40 +08:00
|
|
|
static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
|
|
|
|
bool host)
|
2015-07-03 20:01:34 +08:00
|
|
|
{
|
|
|
|
u64 data = 0;
|
|
|
|
struct kvm *kvm = vcpu->kvm;
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2021-05-21 17:51:38 +08:00
|
|
|
if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
|
|
|
|
return 1;
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
switch (msr) {
|
|
|
|
case HV_X64_MSR_GUEST_OS_ID:
|
|
|
|
data = hv->hv_guest_os_id;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_HYPERCALL:
|
|
|
|
data = hv->hv_hypercall;
|
|
|
|
break;
|
2015-12-01 00:22:19 +08:00
|
|
|
case HV_X64_MSR_TIME_REF_COUNT:
|
|
|
|
data = get_time_ref_counter(kvm);
|
2015-07-03 20:01:34 +08:00
|
|
|
break;
|
|
|
|
case HV_X64_MSR_REFERENCE_TSC:
|
|
|
|
data = hv->hv_tsc_page;
|
|
|
|
break;
|
2015-07-03 20:01:37 +08:00
|
|
|
case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
|
2021-01-26 21:48:09 +08:00
|
|
|
return kvm_hv_msr_get_crash_data(kvm,
|
2015-07-03 20:01:37 +08:00
|
|
|
msr - HV_X64_MSR_CRASH_P0,
|
|
|
|
pdata);
|
|
|
|
case HV_X64_MSR_CRASH_CTL:
|
2021-01-26 21:48:09 +08:00
|
|
|
return kvm_hv_msr_get_crash_ctl(kvm, pdata);
|
2015-09-16 17:29:48 +08:00
|
|
|
case HV_X64_MSR_RESET:
|
|
|
|
data = 0;
|
|
|
|
break;
|
2018-03-01 22:15:12 +08:00
|
|
|
case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
|
|
|
|
data = hv->hv_reenlightenment_control;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_CONTROL:
|
|
|
|
data = hv->hv_tsc_emulation_control;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_TSC_EMULATION_STATUS:
|
|
|
|
data = hv->hv_tsc_emulation_status;
|
|
|
|
break;
|
2020-05-29 21:45:40 +08:00
|
|
|
case HV_X64_MSR_SYNDBG_OPTIONS:
|
|
|
|
case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
|
|
|
|
return syndbg_get_msr(vcpu, msr, pdata, host);
|
2015-07-03 20:01:34 +08:00
|
|
|
default:
|
|
|
|
vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pdata = data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-26 19:01:52 +08:00
|
|
|
static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
|
|
|
|
bool host)
|
2015-07-03 20:01:34 +08:00
|
|
|
{
|
|
|
|
u64 data = 0;
|
2021-01-26 21:48:11 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2021-05-21 17:51:38 +08:00
|
|
|
if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
|
|
|
|
return 1;
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
switch (msr) {
|
2017-07-14 22:13:20 +08:00
|
|
|
case HV_X64_MSR_VP_INDEX:
|
2018-09-27 01:02:55 +08:00
|
|
|
data = hv_vcpu->vp_index;
|
2015-07-03 20:01:34 +08:00
|
|
|
break;
|
|
|
|
case HV_X64_MSR_EOI:
|
|
|
|
return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
|
|
|
|
case HV_X64_MSR_ICR:
|
|
|
|
return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
|
|
|
|
case HV_X64_MSR_TPR:
|
|
|
|
return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
|
2018-03-20 22:02:07 +08:00
|
|
|
case HV_X64_MSR_VP_ASSIST_PAGE:
|
2018-09-27 01:02:55 +08:00
|
|
|
data = hv_vcpu->hv_vapic;
|
2015-07-03 20:01:34 +08:00
|
|
|
break;
|
2015-09-16 17:29:50 +08:00
|
|
|
case HV_X64_MSR_VP_RUNTIME:
|
2018-09-27 01:02:55 +08:00
|
|
|
data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
|
2015-09-16 17:29:50 +08:00
|
|
|
break;
|
2015-11-10 20:36:34 +08:00
|
|
|
case HV_X64_MSR_SCONTROL:
|
|
|
|
case HV_X64_MSR_SVERSION:
|
|
|
|
case HV_X64_MSR_SIEFP:
|
|
|
|
case HV_X64_MSR_SIMP:
|
|
|
|
case HV_X64_MSR_EOM:
|
|
|
|
case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
|
2021-01-26 21:48:06 +08:00
|
|
|
return synic_get_msr(to_hv_synic(vcpu), msr, pdata, host);
|
2015-12-01 00:22:21 +08:00
|
|
|
case HV_X64_MSR_STIMER0_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER1_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER2_CONFIG:
|
|
|
|
case HV_X64_MSR_STIMER3_CONFIG: {
|
|
|
|
int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
return stimer_get_config(to_hv_stimer(vcpu, timer_index),
|
2015-12-01 00:22:21 +08:00
|
|
|
pdata);
|
|
|
|
}
|
|
|
|
case HV_X64_MSR_STIMER0_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER1_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER2_COUNT:
|
|
|
|
case HV_X64_MSR_STIMER3_COUNT: {
|
|
|
|
int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
|
|
|
|
|
2021-01-26 21:48:07 +08:00
|
|
|
return stimer_get_count(to_hv_stimer(vcpu, timer_index),
|
2015-12-01 00:22:21 +08:00
|
|
|
pdata);
|
|
|
|
}
|
2017-07-26 19:32:59 +08:00
|
|
|
case HV_X64_MSR_TSC_FREQUENCY:
|
|
|
|
data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
|
|
|
|
break;
|
|
|
|
case HV_X64_MSR_APIC_FREQUENCY:
|
|
|
|
data = APIC_BUS_FREQUENCY;
|
|
|
|
break;
|
2015-07-03 20:01:34 +08:00
|
|
|
default:
|
|
|
|
vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*pdata = data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-03 20:01:37 +08:00
|
|
|
int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
|
2015-07-03 20:01:34 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
|
|
|
|
|
2021-01-26 21:48:14 +08:00
|
|
|
if (!host && !vcpu->arch.hyperv_enabled)
|
|
|
|
return 1;
|
|
|
|
|
2021-01-26 21:48:15 +08:00
|
|
|
if (!to_hv_vcpu(vcpu)) {
|
|
|
|
if (kvm_hv_vcpu_init(vcpu))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
if (kvm_hv_msr_partition_wide(msr)) {
|
|
|
|
int r;
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
mutex_lock(&hv->hv_lock);
|
2015-07-03 20:01:37 +08:00
|
|
|
r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
|
2021-01-26 21:48:09 +08:00
|
|
|
mutex_unlock(&hv->hv_lock);
|
2015-07-03 20:01:34 +08:00
|
|
|
return r;
|
|
|
|
} else
|
2015-09-16 17:29:50 +08:00
|
|
|
return kvm_hv_set_msr(vcpu, msr, data, host);
|
2015-07-03 20:01:34 +08:00
|
|
|
}
|
|
|
|
|
2018-07-26 19:01:52 +08:00
|
|
|
int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
|
2015-07-03 20:01:34 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
|
|
|
|
|
2021-01-26 21:48:14 +08:00
|
|
|
if (!host && !vcpu->arch.hyperv_enabled)
|
|
|
|
return 1;
|
|
|
|
|
2021-01-26 21:48:15 +08:00
|
|
|
if (!to_hv_vcpu(vcpu)) {
|
|
|
|
if (kvm_hv_vcpu_init(vcpu))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
if (kvm_hv_msr_partition_wide(msr)) {
|
|
|
|
int r;
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
mutex_lock(&hv->hv_lock);
|
2020-05-29 21:45:40 +08:00
|
|
|
r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host);
|
2021-01-26 21:48:09 +08:00
|
|
|
mutex_unlock(&hv->hv_lock);
|
2015-07-03 20:01:34 +08:00
|
|
|
return r;
|
|
|
|
} else
|
2018-07-26 19:01:52 +08:00
|
|
|
return kvm_hv_get_msr(vcpu, msr, pdata, host);
|
2015-07-03 20:01:34 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
static __always_inline unsigned long *sparse_set_to_vcpu_mask(
|
|
|
|
struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask,
|
|
|
|
u64 *vp_bitmap, unsigned long *vcpu_bitmap)
|
2018-05-16 23:21:30 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2018-10-10 23:14:38 +08:00
|
|
|
struct kvm_vcpu *vcpu;
|
2021-11-17 00:04:02 +08:00
|
|
|
int bank, sbank = 0;
|
|
|
|
unsigned long i;
|
2018-05-16 23:21:30 +08:00
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
memset(vp_bitmap, 0,
|
|
|
|
KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap));
|
|
|
|
for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
|
|
|
|
KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
|
|
|
|
vp_bitmap[bank] = sparse_banks[sbank++];
|
2018-05-16 23:21:30 +08:00
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) {
|
|
|
|
/* for all vcpus vp_index == vcpu_idx */
|
|
|
|
return (unsigned long *)vp_bitmap;
|
|
|
|
}
|
2018-09-27 01:02:58 +08:00
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
|
|
|
|
kvm_for_each_vcpu(i, vcpu, kvm) {
|
2021-01-26 21:48:12 +08:00
|
|
|
if (test_bit(kvm_hv_get_vpindex(vcpu), (unsigned long *)vp_bitmap))
|
2018-10-10 23:14:38 +08:00
|
|
|
__set_bit(i, vcpu_bitmap);
|
|
|
|
}
|
|
|
|
return vcpu_bitmap;
|
2018-05-16 23:21:30 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
struct kvm_hv_hcall {
|
|
|
|
u64 param;
|
|
|
|
u64 ingpa;
|
|
|
|
u64 outgpa;
|
|
|
|
u16 code;
|
2021-12-08 06:09:20 +08:00
|
|
|
u16 var_cnt;
|
2021-05-26 16:56:09 +08:00
|
|
|
u16 rep_cnt;
|
|
|
|
u16 rep_idx;
|
|
|
|
bool fast;
|
|
|
|
bool rep;
|
2021-05-26 16:56:10 +08:00
|
|
|
sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS];
|
2021-05-26 16:56:09 +08:00
|
|
|
};
|
|
|
|
|
2021-12-08 06:09:22 +08:00
|
|
|
static u64 kvm_get_sparse_vp_set(struct kvm *kvm, struct kvm_hv_hcall *hc,
|
|
|
|
u64 *sparse_banks, gpa_t offset)
|
|
|
|
{
|
|
|
|
if (hc->var_cnt > 64)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return kvm_read_guest(kvm, hc->ingpa + offset, sparse_banks,
|
|
|
|
hc->var_cnt * sizeof(*sparse_banks));
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex)
|
2018-05-16 23:21:29 +08:00
|
|
|
{
|
2021-05-26 16:56:10 +08:00
|
|
|
int i;
|
2021-01-26 21:48:10 +08:00
|
|
|
struct kvm *kvm = vcpu->kvm;
|
2018-05-16 23:21:30 +08:00
|
|
|
struct hv_tlb_flush_ex flush_ex;
|
2018-05-16 23:21:29 +08:00
|
|
|
struct hv_tlb_flush flush;
|
2018-10-10 23:14:38 +08:00
|
|
|
u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
|
|
|
|
DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
|
|
|
|
unsigned long *vcpu_mask;
|
2018-09-27 01:02:58 +08:00
|
|
|
u64 valid_bank_mask;
|
2018-05-16 23:21:30 +08:00
|
|
|
u64 sparse_banks[64];
|
|
|
|
bool all_cpus;
|
2018-05-16 23:21:29 +08:00
|
|
|
|
2018-05-16 23:21:30 +08:00
|
|
|
if (!ex) {
|
2021-05-26 16:56:10 +08:00
|
|
|
if (hc->fast) {
|
|
|
|
flush.address_space = hc->ingpa;
|
|
|
|
flush.flags = hc->outgpa;
|
|
|
|
flush.processor_mask = sse128_lo(hc->xmm[0]);
|
|
|
|
} else {
|
|
|
|
if (unlikely(kvm_read_guest(kvm, hc->ingpa,
|
|
|
|
&flush, sizeof(flush))))
|
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
}
|
2018-05-16 23:21:29 +08:00
|
|
|
|
2018-05-16 23:21:30 +08:00
|
|
|
trace_kvm_hv_flush_tlb(flush.processor_mask,
|
|
|
|
flush.address_space, flush.flags);
|
|
|
|
|
2018-09-27 01:02:58 +08:00
|
|
|
valid_bank_mask = BIT_ULL(0);
|
2018-05-16 23:21:30 +08:00
|
|
|
sparse_banks[0] = flush.processor_mask;
|
2019-03-21 01:43:20 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Work around possible WS2012 bug: it sends hypercalls
|
|
|
|
* with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
|
|
|
|
* while also expecting us to flush something and crashing if
|
|
|
|
* we don't. Let's treat processor_mask == 0 same as
|
|
|
|
* HV_FLUSH_ALL_PROCESSORS.
|
|
|
|
*/
|
|
|
|
all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
|
|
|
|
flush.processor_mask == 0;
|
2018-05-16 23:21:30 +08:00
|
|
|
} else {
|
2021-05-26 16:56:10 +08:00
|
|
|
if (hc->fast) {
|
|
|
|
flush_ex.address_space = hc->ingpa;
|
|
|
|
flush_ex.flags = hc->outgpa;
|
|
|
|
memcpy(&flush_ex.hv_vp_set,
|
|
|
|
&hc->xmm[0], sizeof(hc->xmm[0]));
|
|
|
|
} else {
|
|
|
|
if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex,
|
|
|
|
sizeof(flush_ex))))
|
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
}
|
2018-05-16 23:21:30 +08:00
|
|
|
|
|
|
|
trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
|
|
|
|
flush_ex.hv_vp_set.format,
|
|
|
|
flush_ex.address_space,
|
|
|
|
flush_ex.flags);
|
|
|
|
|
|
|
|
valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
|
|
|
|
all_cpus = flush_ex.hv_vp_set.format !=
|
|
|
|
HV_GENERIC_SET_SPARSE_4K;
|
|
|
|
|
2021-12-08 06:09:20 +08:00
|
|
|
if (hc->var_cnt != bitmap_weight((unsigned long *)&valid_bank_mask, 64))
|
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
2018-05-16 23:21:30 +08:00
|
|
|
|
2021-12-08 06:09:21 +08:00
|
|
|
if (all_cpus)
|
|
|
|
goto do_flush;
|
|
|
|
|
|
|
|
if (!hc->var_cnt)
|
2018-05-16 23:21:30 +08:00
|
|
|
goto ret_success;
|
|
|
|
|
2021-12-08 06:09:21 +08:00
|
|
|
if (hc->fast) {
|
|
|
|
if (hc->var_cnt > HV_HYPERCALL_MAX_XMM_REGISTERS - 1)
|
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
for (i = 0; i < hc->var_cnt; i += 2) {
|
|
|
|
sparse_banks[i] = sse128_lo(hc->xmm[i / 2 + 1]);
|
|
|
|
sparse_banks[i + 1] = sse128_hi(hc->xmm[i / 2 + 1]);
|
2021-05-26 16:56:10 +08:00
|
|
|
}
|
2021-12-08 06:09:21 +08:00
|
|
|
goto do_flush;
|
2021-05-26 16:56:10 +08:00
|
|
|
}
|
2021-12-08 06:09:21 +08:00
|
|
|
|
2021-12-08 06:09:22 +08:00
|
|
|
if (kvm_get_sparse_vp_set(kvm, hc, sparse_banks,
|
|
|
|
offsetof(struct hv_tlb_flush_ex,
|
|
|
|
hv_vp_set.bank_contents)))
|
2021-12-08 06:09:21 +08:00
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
2018-05-16 23:21:30 +08:00
|
|
|
}
|
2018-05-16 23:21:29 +08:00
|
|
|
|
2021-12-08 06:09:21 +08:00
|
|
|
do_flush:
|
2018-09-27 01:02:58 +08:00
|
|
|
/*
|
2018-10-10 23:14:38 +08:00
|
|
|
* vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
|
|
|
|
* analyze it here, flush TLB regardless of the specified address space.
|
2018-09-27 01:02:58 +08:00
|
|
|
*/
|
2021-09-03 15:51:36 +08:00
|
|
|
if (all_cpus) {
|
|
|
|
kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH_GUEST);
|
|
|
|
} else {
|
|
|
|
vcpu_mask = sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
|
|
|
|
vp_bitmap, vcpu_bitmap);
|
|
|
|
|
|
|
|
kvm_make_vcpus_request_mask(kvm, KVM_REQ_TLB_FLUSH_GUEST,
|
2021-09-03 15:51:41 +08:00
|
|
|
vcpu_mask);
|
2021-09-03 15:51:36 +08:00
|
|
|
}
|
2018-05-16 23:21:29 +08:00
|
|
|
|
2018-05-16 23:21:30 +08:00
|
|
|
ret_success:
|
2021-05-26 16:56:09 +08:00
|
|
|
/* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */
|
2018-05-16 23:21:29 +08:00
|
|
|
return (u64)HV_STATUS_SUCCESS |
|
2021-05-26 16:56:09 +08:00
|
|
|
((u64)hc->rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
|
2018-05-16 23:21:29 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector,
|
|
|
|
unsigned long *vcpu_bitmap)
|
|
|
|
{
|
|
|
|
struct kvm_lapic_irq irq = {
|
|
|
|
.delivery_mode = APIC_DM_FIXED,
|
|
|
|
.vector = vector
|
|
|
|
};
|
|
|
|
struct kvm_vcpu *vcpu;
|
2021-11-17 00:04:02 +08:00
|
|
|
unsigned long i;
|
2018-10-10 23:14:38 +08:00
|
|
|
|
|
|
|
kvm_for_each_vcpu(i, vcpu, kvm) {
|
|
|
|
if (vcpu_bitmap && !test_bit(i, vcpu_bitmap))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We fail only when APIC is disabled */
|
|
|
|
kvm_apic_set_irq(vcpu, &irq, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc, bool ex)
|
2018-09-27 01:02:59 +08:00
|
|
|
{
|
2021-01-26 21:48:10 +08:00
|
|
|
struct kvm *kvm = vcpu->kvm;
|
2018-09-27 01:02:59 +08:00
|
|
|
struct hv_send_ipi_ex send_ipi_ex;
|
|
|
|
struct hv_send_ipi send_ipi;
|
2018-10-10 23:14:38 +08:00
|
|
|
u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
|
|
|
|
DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
|
|
|
|
unsigned long *vcpu_mask;
|
2018-09-27 01:02:59 +08:00
|
|
|
unsigned long valid_bank_mask;
|
|
|
|
u64 sparse_banks[64];
|
2018-10-10 23:14:38 +08:00
|
|
|
u32 vector;
|
2018-09-27 01:02:59 +08:00
|
|
|
bool all_cpus;
|
|
|
|
|
|
|
|
if (!ex) {
|
2021-05-26 16:56:09 +08:00
|
|
|
if (!hc->fast) {
|
|
|
|
if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi,
|
2018-09-27 01:02:59 +08:00
|
|
|
sizeof(send_ipi))))
|
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
sparse_banks[0] = send_ipi.cpu_mask;
|
2018-10-10 23:14:38 +08:00
|
|
|
vector = send_ipi.vector;
|
2018-09-27 01:02:59 +08:00
|
|
|
} else {
|
|
|
|
/* 'reserved' part of hv_send_ipi should be 0 */
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(hc->ingpa >> 32 != 0))
|
2018-09-27 01:02:59 +08:00
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
2021-05-26 16:56:09 +08:00
|
|
|
sparse_banks[0] = hc->outgpa;
|
|
|
|
vector = (u32)hc->ingpa;
|
2018-09-27 01:02:59 +08:00
|
|
|
}
|
|
|
|
all_cpus = false;
|
|
|
|
valid_bank_mask = BIT_ULL(0);
|
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
|
2018-09-27 01:02:59 +08:00
|
|
|
} else {
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi_ex,
|
2018-09-27 01:02:59 +08:00
|
|
|
sizeof(send_ipi_ex))))
|
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
|
|
|
|
trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
|
|
|
|
send_ipi_ex.vp_set.format,
|
|
|
|
send_ipi_ex.vp_set.valid_bank_mask);
|
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
vector = send_ipi_ex.vector;
|
2018-09-27 01:02:59 +08:00
|
|
|
valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
|
|
|
|
all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
|
|
|
|
|
2021-12-08 06:09:20 +08:00
|
|
|
if (hc->var_cnt != bitmap_weight(&valid_bank_mask, 64))
|
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
|
2021-12-08 06:09:19 +08:00
|
|
|
if (all_cpus)
|
|
|
|
goto check_and_send_ipi;
|
|
|
|
|
2021-12-08 06:09:20 +08:00
|
|
|
if (!hc->var_cnt)
|
2018-09-27 01:02:59 +08:00
|
|
|
goto ret_success;
|
|
|
|
|
2021-12-08 06:09:22 +08:00
|
|
|
if (kvm_get_sparse_vp_set(kvm, hc, sparse_banks,
|
|
|
|
offsetof(struct hv_send_ipi_ex,
|
|
|
|
vp_set.bank_contents)))
|
2018-09-27 01:02:59 +08:00
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
}
|
|
|
|
|
2021-12-08 06:09:19 +08:00
|
|
|
check_and_send_ipi:
|
2018-10-10 23:14:38 +08:00
|
|
|
if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
|
2018-09-27 01:02:59 +08:00
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
vcpu_mask = all_cpus ? NULL :
|
|
|
|
sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
|
|
|
|
vp_bitmap, vcpu_bitmap);
|
2018-09-27 01:02:59 +08:00
|
|
|
|
2018-10-10 23:14:38 +08:00
|
|
|
kvm_send_ipi_to_many(kvm, vector, vcpu_mask);
|
2018-09-27 01:02:59 +08:00
|
|
|
|
|
|
|
ret_success:
|
|
|
|
return HV_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:14 +08:00
|
|
|
void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
struct kvm_cpuid_entry2 *entry;
|
2021-08-09 19:00:58 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu;
|
2021-01-26 21:48:14 +08:00
|
|
|
|
|
|
|
entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_INTERFACE, 0);
|
2021-05-21 17:51:37 +08:00
|
|
|
if (entry && entry->eax == HYPERV_CPUID_SIGNATURE_EAX) {
|
2021-01-26 21:48:14 +08:00
|
|
|
vcpu->arch.hyperv_enabled = true;
|
2021-05-21 17:51:37 +08:00
|
|
|
} else {
|
2021-01-26 21:48:14 +08:00
|
|
|
vcpu->arch.hyperv_enabled = false;
|
2021-05-21 17:51:37 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!to_hv_vcpu(vcpu) && kvm_hv_vcpu_init(vcpu))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hv_vcpu = to_hv_vcpu(vcpu);
|
|
|
|
|
|
|
|
entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_FEATURES, 0);
|
|
|
|
if (entry) {
|
|
|
|
hv_vcpu->cpuid_cache.features_eax = entry->eax;
|
|
|
|
hv_vcpu->cpuid_cache.features_ebx = entry->ebx;
|
|
|
|
hv_vcpu->cpuid_cache.features_edx = entry->edx;
|
|
|
|
} else {
|
|
|
|
hv_vcpu->cpuid_cache.features_eax = 0;
|
|
|
|
hv_vcpu->cpuid_cache.features_ebx = 0;
|
|
|
|
hv_vcpu->cpuid_cache.features_edx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_ENLIGHTMENT_INFO, 0);
|
|
|
|
if (entry) {
|
|
|
|
hv_vcpu->cpuid_cache.enlightenments_eax = entry->eax;
|
|
|
|
hv_vcpu->cpuid_cache.enlightenments_ebx = entry->ebx;
|
|
|
|
} else {
|
|
|
|
hv_vcpu->cpuid_cache.enlightenments_eax = 0;
|
|
|
|
hv_vcpu->cpuid_cache.enlightenments_ebx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES, 0);
|
|
|
|
if (entry)
|
|
|
|
hv_vcpu->cpuid_cache.syndbg_cap_eax = entry->eax;
|
|
|
|
else
|
|
|
|
hv_vcpu->cpuid_cache.syndbg_cap_eax = 0;
|
2021-01-26 21:48:14 +08:00
|
|
|
}
|
|
|
|
|
2021-05-21 17:51:36 +08:00
|
|
|
int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce)
|
|
|
|
{
|
|
|
|
struct kvm_vcpu_hv *hv_vcpu;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!to_hv_vcpu(vcpu)) {
|
|
|
|
if (enforce) {
|
|
|
|
ret = kvm_hv_vcpu_init(vcpu);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hv_vcpu = to_hv_vcpu(vcpu);
|
|
|
|
hv_vcpu->enforce_cpuid = enforce;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:48:14 +08:00
|
|
|
bool kvm_hv_hypercall_enabled(struct kvm_vcpu *vcpu)
|
2015-07-03 20:01:34 +08:00
|
|
|
{
|
2021-01-26 21:48:14 +08:00
|
|
|
return vcpu->arch.hyperv_enabled && to_kvm_hv(vcpu->kvm)->hv_guest_os_id;
|
2015-07-03 20:01:34 +08:00
|
|
|
}
|
|
|
|
|
2016-02-11 21:45:01 +08:00
|
|
|
static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
|
|
|
|
{
|
|
|
|
bool longmode;
|
|
|
|
|
2021-05-25 01:48:57 +08:00
|
|
|
longmode = is_64_bit_hypercall(vcpu);
|
2016-02-11 21:45:01 +08:00
|
|
|
if (longmode)
|
2019-05-01 01:36:17 +08:00
|
|
|
kvm_rax_write(vcpu, result);
|
2016-02-11 21:45:01 +08:00
|
|
|
else {
|
2019-05-01 01:36:17 +08:00
|
|
|
kvm_rdx_write(vcpu, result >> 32);
|
|
|
|
kvm_rax_write(vcpu, result & 0xffffffff);
|
2016-02-11 21:45:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 23:50:56 +08:00
|
|
|
static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
|
2016-02-11 21:45:01 +08:00
|
|
|
{
|
2021-07-30 20:26:23 +08:00
|
|
|
trace_kvm_hv_hypercall_done(result);
|
2018-05-24 23:50:56 +08:00
|
|
|
kvm_hv_hypercall_set_result(vcpu, result);
|
|
|
|
++vcpu->stat.hypercalls;
|
2018-04-30 17:23:01 +08:00
|
|
|
return kvm_skip_emulated_instruction(vcpu);
|
2016-02-11 21:45:01 +08:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:50:56 +08:00
|
|
|
static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
|
2018-02-01 21:48:32 +08:00
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
|
2018-02-01 21:48:32 +08:00
|
|
|
struct eventfd_ctx *eventfd;
|
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(!hc->fast)) {
|
2018-02-01 21:48:32 +08:00
|
|
|
int ret;
|
2021-05-26 16:56:09 +08:00
|
|
|
gpa_t gpa = hc->ingpa;
|
2018-02-01 21:48:32 +08:00
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
if ((gpa & (__alignof__(hc->ingpa) - 1)) ||
|
|
|
|
offset_in_page(gpa) + sizeof(hc->ingpa) > PAGE_SIZE)
|
2018-02-01 21:48:32 +08:00
|
|
|
return HV_STATUS_INVALID_ALIGNMENT;
|
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_vcpu_read_guest(vcpu, gpa,
|
|
|
|
&hc->ingpa, sizeof(hc->ingpa));
|
2018-02-01 21:48:32 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return HV_STATUS_INVALID_ALIGNMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Per spec, bits 32-47 contain the extra "flag number". However, we
|
|
|
|
* have no use for it, and in all known usecases it is zero, so just
|
|
|
|
* report lookup failure if it isn't.
|
|
|
|
*/
|
2021-05-26 16:56:09 +08:00
|
|
|
if (hc->ingpa & 0xffff00000000ULL)
|
2018-02-01 21:48:32 +08:00
|
|
|
return HV_STATUS_INVALID_PORT_ID;
|
|
|
|
/* remaining bits are reserved-zero */
|
2021-05-26 16:56:09 +08:00
|
|
|
if (hc->ingpa & ~KVM_HYPERV_CONN_ID_MASK)
|
2018-02-01 21:48:32 +08:00
|
|
|
return HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
|
2018-05-08 01:24:34 +08:00
|
|
|
/* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
|
|
|
|
rcu_read_lock();
|
2021-05-26 16:56:09 +08:00
|
|
|
eventfd = idr_find(&hv->conn_to_evt, hc->ingpa);
|
2018-05-08 01:24:34 +08:00
|
|
|
rcu_read_unlock();
|
2018-02-01 21:48:32 +08:00
|
|
|
if (!eventfd)
|
|
|
|
return HV_STATUS_INVALID_PORT_ID;
|
|
|
|
|
|
|
|
eventfd_signal(eventfd, 1);
|
|
|
|
return HV_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-05-26 16:56:10 +08:00
|
|
|
static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc)
|
|
|
|
{
|
|
|
|
switch (hc->code) {
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc)
|
|
|
|
{
|
|
|
|
int reg;
|
|
|
|
|
|
|
|
kvm_fpu_get();
|
|
|
|
for (reg = 0; reg < HV_HYPERCALL_MAX_XMM_REGISTERS; reg++)
|
|
|
|
_kvm_read_sse_reg(reg, &hc->xmm[reg]);
|
|
|
|
kvm_fpu_put();
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:51:54 +08:00
|
|
|
static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code)
|
|
|
|
{
|
2021-05-21 17:51:55 +08:00
|
|
|
if (!hv_vcpu->enforce_cpuid)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case HVCALL_NOTIFY_LONG_SPIN_WAIT:
|
|
|
|
return hv_vcpu->cpuid_cache.enlightenments_ebx &&
|
|
|
|
hv_vcpu->cpuid_cache.enlightenments_ebx != U32_MAX;
|
2021-05-21 17:51:56 +08:00
|
|
|
case HVCALL_POST_MESSAGE:
|
|
|
|
return hv_vcpu->cpuid_cache.features_ebx & HV_POST_MESSAGES;
|
2021-05-21 17:51:57 +08:00
|
|
|
case HVCALL_SIGNAL_EVENT:
|
|
|
|
return hv_vcpu->cpuid_cache.features_ebx & HV_SIGNAL_EVENTS;
|
2021-05-21 17:51:58 +08:00
|
|
|
case HVCALL_POST_DEBUG_DATA:
|
|
|
|
case HVCALL_RETRIEVE_DEBUG_DATA:
|
|
|
|
case HVCALL_RESET_DEBUG_SESSION:
|
|
|
|
/*
|
|
|
|
* Return 'true' when SynDBG is disabled so the resulting code
|
|
|
|
* will be HV_STATUS_INVALID_HYPERCALL_CODE.
|
|
|
|
*/
|
|
|
|
return !kvm_hv_is_syndbg_enabled(hv_vcpu->vcpu) ||
|
|
|
|
hv_vcpu->cpuid_cache.features_ebx & HV_DEBUGGING;
|
2021-05-21 17:51:59 +08:00
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
|
2021-05-21 17:52:01 +08:00
|
|
|
if (!(hv_vcpu->cpuid_cache.enlightenments_eax &
|
|
|
|
HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
|
|
|
|
return false;
|
|
|
|
fallthrough;
|
2021-05-21 17:51:59 +08:00
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
|
|
|
|
return hv_vcpu->cpuid_cache.enlightenments_eax &
|
|
|
|
HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
|
2021-05-21 17:52:00 +08:00
|
|
|
case HVCALL_SEND_IPI_EX:
|
2021-05-21 17:52:01 +08:00
|
|
|
if (!(hv_vcpu->cpuid_cache.enlightenments_eax &
|
|
|
|
HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
|
|
|
|
return false;
|
|
|
|
fallthrough;
|
2021-05-21 17:52:00 +08:00
|
|
|
case HVCALL_SEND_IPI:
|
|
|
|
return hv_vcpu->cpuid_cache.enlightenments_eax &
|
|
|
|
HV_X64_CLUSTER_IPI_RECOMMENDED;
|
2021-05-21 17:51:55 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:51:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-03 20:01:34 +08:00
|
|
|
int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2021-07-30 20:26:24 +08:00
|
|
|
struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
|
2021-05-26 16:56:09 +08:00
|
|
|
struct kvm_hv_hcall hc;
|
|
|
|
u64 ret = HV_STATUS_SUCCESS;
|
2015-07-03 20:01:34 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* hypercall generates UD from non zero cpl and real mode
|
|
|
|
* per HYPER-V spec
|
|
|
|
*/
|
2021-01-15 11:27:56 +08:00
|
|
|
if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || !is_protmode(vcpu)) {
|
2015-07-03 20:01:34 +08:00
|
|
|
kvm_queue_exception(vcpu, UD_VECTOR);
|
2016-02-11 21:44:59 +08:00
|
|
|
return 1;
|
2015-07-03 20:01:34 +08:00
|
|
|
}
|
|
|
|
|
x86: kvm: avoid -Wsometimes-uninitized warning
Clang notices a code path in which some variables are never
initialized, but fails to figure out that this can never happen
on i386 because is_64_bit_mode() always returns false.
arch/x86/kvm/hyperv.c:1610:6: error: variable 'ingpa' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
if (!longmode) {
^~~~~~~~~
arch/x86/kvm/hyperv.c:1632:55: note: uninitialized use occurs here
trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
^~~~~
arch/x86/kvm/hyperv.c:1610:2: note: remove the 'if' if its condition is always true
if (!longmode) {
^~~~~~~~~~~~~~~
arch/x86/kvm/hyperv.c:1595:18: note: initialize the variable 'ingpa' to silence this warning
u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS;
^
= 0
arch/x86/kvm/hyperv.c:1610:6: error: variable 'outgpa' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
arch/x86/kvm/hyperv.c:1610:6: error: variable 'param' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
Flip the condition around to avoid the conditional execution on i386.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2019-07-12 22:13:09 +08:00
|
|
|
#ifdef CONFIG_X86_64
|
2021-05-25 01:48:57 +08:00
|
|
|
if (is_64_bit_hypercall(vcpu)) {
|
2021-05-26 16:56:09 +08:00
|
|
|
hc.param = kvm_rcx_read(vcpu);
|
|
|
|
hc.ingpa = kvm_rdx_read(vcpu);
|
|
|
|
hc.outgpa = kvm_r8_read(vcpu);
|
x86: kvm: avoid -Wsometimes-uninitized warning
Clang notices a code path in which some variables are never
initialized, but fails to figure out that this can never happen
on i386 because is_64_bit_mode() always returns false.
arch/x86/kvm/hyperv.c:1610:6: error: variable 'ingpa' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
if (!longmode) {
^~~~~~~~~
arch/x86/kvm/hyperv.c:1632:55: note: uninitialized use occurs here
trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
^~~~~
arch/x86/kvm/hyperv.c:1610:2: note: remove the 'if' if its condition is always true
if (!longmode) {
^~~~~~~~~~~~~~~
arch/x86/kvm/hyperv.c:1595:18: note: initialize the variable 'ingpa' to silence this warning
u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS;
^
= 0
arch/x86/kvm/hyperv.c:1610:6: error: variable 'outgpa' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
arch/x86/kvm/hyperv.c:1610:6: error: variable 'param' is used uninitialized whenever 'if' condition is false [-Werror,-Wsometimes-uninitialized]
Flip the condition around to avoid the conditional execution on i386.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2019-07-12 22:13:09 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2021-05-26 16:56:09 +08:00
|
|
|
hc.param = ((u64)kvm_rdx_read(vcpu) << 32) |
|
|
|
|
(kvm_rax_read(vcpu) & 0xffffffff);
|
|
|
|
hc.ingpa = ((u64)kvm_rbx_read(vcpu) << 32) |
|
|
|
|
(kvm_rcx_read(vcpu) & 0xffffffff);
|
|
|
|
hc.outgpa = ((u64)kvm_rdi_read(vcpu) << 32) |
|
|
|
|
(kvm_rsi_read(vcpu) & 0xffffffff);
|
2015-07-03 20:01:34 +08:00
|
|
|
}
|
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
hc.code = hc.param & 0xffff;
|
2021-12-08 06:09:20 +08:00
|
|
|
hc.var_cnt = (hc.param & HV_HYPERCALL_VARHEAD_MASK) >> HV_HYPERCALL_VARHEAD_OFFSET;
|
2021-05-26 16:56:09 +08:00
|
|
|
hc.fast = !!(hc.param & HV_HYPERCALL_FAST_BIT);
|
|
|
|
hc.rep_cnt = (hc.param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
|
|
|
|
hc.rep_idx = (hc.param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
|
|
|
|
hc.rep = !!(hc.rep_cnt || hc.rep_idx);
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2021-12-08 06:09:20 +08:00
|
|
|
trace_kvm_hv_hypercall(hc.code, hc.fast, hc.var_cnt, hc.rep_cnt,
|
|
|
|
hc.rep_idx, hc.ingpa, hc.outgpa);
|
2015-07-03 20:01:34 +08:00
|
|
|
|
2021-07-30 20:26:24 +08:00
|
|
|
if (unlikely(!hv_check_hypercall_access(hv_vcpu, hc.code))) {
|
2021-05-21 17:51:54 +08:00
|
|
|
ret = HV_STATUS_ACCESS_DENIED;
|
|
|
|
goto hypercall_complete;
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:26:24 +08:00
|
|
|
if (hc.fast && is_xmm_fast_hypercall(&hc)) {
|
|
|
|
if (unlikely(hv_vcpu->enforce_cpuid &&
|
|
|
|
!(hv_vcpu->cpuid_cache.features_edx &
|
|
|
|
HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE))) {
|
|
|
|
kvm_queue_exception(vcpu, UD_VECTOR);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:26:22 +08:00
|
|
|
kvm_hv_hypercall_read_xmm(&hc);
|
2021-07-30 20:26:24 +08:00
|
|
|
}
|
2021-07-30 20:26:22 +08:00
|
|
|
|
2021-05-26 16:56:09 +08:00
|
|
|
switch (hc.code) {
|
2016-02-11 21:44:57 +08:00
|
|
|
case HVCALL_NOTIFY_LONG_SPIN_WAIT:
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(hc.rep)) {
|
2018-05-16 23:21:27 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-08 12:05:33 +08:00
|
|
|
kvm_vcpu_on_spin(vcpu, true);
|
2015-07-03 20:01:34 +08:00
|
|
|
break;
|
2016-02-11 21:45:01 +08:00
|
|
|
case HVCALL_SIGNAL_EVENT:
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(hc.rep)) {
|
2018-05-16 23:21:27 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_hvcall_signal_event(vcpu, &hc);
|
2018-03-17 19:48:27 +08:00
|
|
|
if (ret != HV_STATUS_INVALID_PORT_ID)
|
2018-02-01 21:48:32 +08:00
|
|
|
break;
|
2020-08-24 06:36:59 +08:00
|
|
|
fallthrough; /* maybe userspace knows this conn_id */
|
2018-02-01 21:48:32 +08:00
|
|
|
case HVCALL_POST_MESSAGE:
|
2016-03-29 17:23:25 +08:00
|
|
|
/* don't bother userspace if it has no way to handle it */
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(hc.rep || !to_hv_synic(vcpu)->active)) {
|
2018-05-16 23:21:27 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
2016-03-29 17:23:25 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-02-11 21:45:01 +08:00
|
|
|
vcpu->run->exit_reason = KVM_EXIT_HYPERV;
|
|
|
|
vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
|
2021-05-26 16:56:09 +08:00
|
|
|
vcpu->run->hyperv.u.hcall.input = hc.param;
|
|
|
|
vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
|
|
|
|
vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
|
2016-02-11 21:45:01 +08:00
|
|
|
vcpu->arch.complete_userspace_io =
|
|
|
|
kvm_hv_hypercall_complete_userspace;
|
|
|
|
return 0;
|
2018-05-16 23:21:29 +08:00
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
|
2021-05-26 16:56:10 +08:00
|
|
|
if (unlikely(!hc.rep_cnt || hc.rep_idx)) {
|
2018-05-16 23:21:29 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_hv_flush_tlb(vcpu, &hc, false);
|
2018-05-16 23:21:29 +08:00
|
|
|
break;
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
|
2021-05-26 16:56:10 +08:00
|
|
|
if (unlikely(hc.rep)) {
|
2018-05-16 23:21:29 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_hv_flush_tlb(vcpu, &hc, false);
|
2018-05-16 23:21:30 +08:00
|
|
|
break;
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
|
2021-05-26 16:56:10 +08:00
|
|
|
if (unlikely(!hc.rep_cnt || hc.rep_idx)) {
|
2018-05-16 23:21:30 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_hv_flush_tlb(vcpu, &hc, true);
|
2018-05-16 23:21:30 +08:00
|
|
|
break;
|
|
|
|
case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
|
2021-05-26 16:56:10 +08:00
|
|
|
if (unlikely(hc.rep)) {
|
2018-05-16 23:21:30 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_hv_flush_tlb(vcpu, &hc, true);
|
2018-05-16 23:21:29 +08:00
|
|
|
break;
|
2018-09-27 01:02:59 +08:00
|
|
|
case HVCALL_SEND_IPI:
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(hc.rep)) {
|
2018-09-27 01:02:59 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_hv_send_ipi(vcpu, &hc, false);
|
2018-09-27 01:02:59 +08:00
|
|
|
break;
|
|
|
|
case HVCALL_SEND_IPI_EX:
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(hc.fast || hc.rep)) {
|
2018-09-27 01:02:59 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-26 16:56:09 +08:00
|
|
|
ret = kvm_hv_send_ipi(vcpu, &hc, true);
|
2018-09-27 01:02:59 +08:00
|
|
|
break;
|
2020-05-29 21:45:42 +08:00
|
|
|
case HVCALL_POST_DEBUG_DATA:
|
|
|
|
case HVCALL_RETRIEVE_DEBUG_DATA:
|
2021-05-26 16:56:09 +08:00
|
|
|
if (unlikely(hc.fast)) {
|
2020-05-29 21:45:42 +08:00
|
|
|
ret = HV_STATUS_INVALID_PARAMETER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fallthrough;
|
|
|
|
case HVCALL_RESET_DEBUG_SESSION: {
|
2021-01-26 21:48:08 +08:00
|
|
|
struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
|
2020-05-29 21:45:42 +08:00
|
|
|
|
|
|
|
if (!kvm_hv_is_syndbg_enabled(vcpu)) {
|
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_CODE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) {
|
|
|
|
ret = HV_STATUS_OPERATION_DENIED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vcpu->run->exit_reason = KVM_EXIT_HYPERV;
|
|
|
|
vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
|
2021-05-26 16:56:09 +08:00
|
|
|
vcpu->run->hyperv.u.hcall.input = hc.param;
|
|
|
|
vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
|
|
|
|
vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
|
2020-05-29 21:45:42 +08:00
|
|
|
vcpu->arch.complete_userspace_io =
|
|
|
|
kvm_hv_hypercall_complete_userspace;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-07-03 20:01:34 +08:00
|
|
|
default:
|
2018-03-17 19:48:27 +08:00
|
|
|
ret = HV_STATUS_INVALID_HYPERCALL_CODE;
|
2015-07-03 20:01:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-21 17:51:54 +08:00
|
|
|
hypercall_complete:
|
2018-05-24 23:50:56 +08:00
|
|
|
return kvm_hv_hypercall_complete(vcpu, ret);
|
2015-07-03 20:01:34 +08:00
|
|
|
}
|
2018-02-01 21:48:31 +08:00
|
|
|
|
|
|
|
void kvm_hv_init_vm(struct kvm *kvm)
|
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
|
|
|
|
|
|
|
mutex_init(&hv->hv_lock);
|
|
|
|
idr_init(&hv->conn_to_evt);
|
2018-02-01 21:48:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_hv_destroy_vm(struct kvm *kvm)
|
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2018-02-01 21:48:32 +08:00
|
|
|
struct eventfd_ctx *eventfd;
|
|
|
|
int i;
|
|
|
|
|
2021-01-26 21:48:09 +08:00
|
|
|
idr_for_each_entry(&hv->conn_to_evt, eventfd, i)
|
2018-02-01 21:48:32 +08:00
|
|
|
eventfd_ctx_put(eventfd);
|
2021-01-26 21:48:09 +08:00
|
|
|
idr_destroy(&hv->conn_to_evt);
|
2018-02-01 21:48:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
|
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2018-02-01 21:48:32 +08:00
|
|
|
struct eventfd_ctx *eventfd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
eventfd = eventfd_ctx_fdget(fd);
|
|
|
|
if (IS_ERR(eventfd))
|
|
|
|
return PTR_ERR(eventfd);
|
|
|
|
|
|
|
|
mutex_lock(&hv->hv_lock);
|
|
|
|
ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
|
2019-02-12 03:02:50 +08:00
|
|
|
GFP_KERNEL_ACCOUNT);
|
2018-02-01 21:48:32 +08:00
|
|
|
mutex_unlock(&hv->hv_lock);
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ret == -ENOSPC)
|
|
|
|
ret = -EEXIST;
|
|
|
|
eventfd_ctx_put(eventfd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
|
|
|
|
{
|
2021-01-26 21:48:09 +08:00
|
|
|
struct kvm_hv *hv = to_kvm_hv(kvm);
|
2018-02-01 21:48:32 +08:00
|
|
|
struct eventfd_ctx *eventfd;
|
|
|
|
|
|
|
|
mutex_lock(&hv->hv_lock);
|
|
|
|
eventfd = idr_remove(&hv->conn_to_evt, conn_id);
|
|
|
|
mutex_unlock(&hv->hv_lock);
|
|
|
|
|
|
|
|
if (!eventfd)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
synchronize_srcu(&kvm->srcu);
|
|
|
|
eventfd_ctx_put(eventfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
|
|
|
|
{
|
|
|
|
if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
|
|
|
|
(args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
|
|
|
|
return kvm_hv_eventfd_deassign(kvm, args->conn_id);
|
|
|
|
return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
|
2018-02-01 21:48:31 +08:00
|
|
|
}
|
2018-12-11 01:21:56 +08:00
|
|
|
|
2020-09-29 23:09:43 +08:00
|
|
|
int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
|
|
|
|
struct kvm_cpuid_entry2 __user *entries)
|
2018-12-11 01:21:56 +08:00
|
|
|
{
|
2019-08-28 00:04:02 +08:00
|
|
|
uint16_t evmcs_ver = 0;
|
2018-12-11 01:21:56 +08:00
|
|
|
struct kvm_cpuid_entry2 cpuid_entries[] = {
|
|
|
|
{ .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
|
|
|
|
{ .function = HYPERV_CPUID_INTERFACE },
|
|
|
|
{ .function = HYPERV_CPUID_VERSION },
|
|
|
|
{ .function = HYPERV_CPUID_FEATURES },
|
|
|
|
{ .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
|
|
|
|
{ .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
|
2020-05-29 21:45:40 +08:00
|
|
|
{ .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS },
|
|
|
|
{ .function = HYPERV_CPUID_SYNDBG_INTERFACE },
|
|
|
|
{ .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES },
|
2018-12-11 01:21:56 +08:00
|
|
|
{ .function = HYPERV_CPUID_NESTED_FEATURES },
|
|
|
|
};
|
|
|
|
int i, nent = ARRAY_SIZE(cpuid_entries);
|
|
|
|
|
2020-04-17 22:24:18 +08:00
|
|
|
if (kvm_x86_ops.nested_ops->get_evmcs_version)
|
|
|
|
evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu);
|
2019-08-28 00:04:02 +08:00
|
|
|
|
2018-12-11 01:21:56 +08:00
|
|
|
/* Skip NESTED_FEATURES if eVMCS is not supported */
|
|
|
|
if (!evmcs_ver)
|
|
|
|
--nent;
|
|
|
|
|
|
|
|
if (cpuid->nent < nent)
|
|
|
|
return -E2BIG;
|
|
|
|
|
|
|
|
if (cpuid->nent > nent)
|
|
|
|
cpuid->nent = nent;
|
|
|
|
|
|
|
|
for (i = 0; i < nent; i++) {
|
|
|
|
struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
|
|
|
|
u32 signature[3];
|
|
|
|
|
|
|
|
switch (ent->function) {
|
|
|
|
case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
|
|
|
|
memcpy(signature, "Linux KVM Hv", 12);
|
|
|
|
|
2020-05-29 21:45:40 +08:00
|
|
|
ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES;
|
2018-12-11 01:21:56 +08:00
|
|
|
ent->ebx = signature[0];
|
|
|
|
ent->ecx = signature[1];
|
|
|
|
ent->edx = signature[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_INTERFACE:
|
2021-01-26 21:48:14 +08:00
|
|
|
ent->eax = HYPERV_CPUID_SIGNATURE_EAX;
|
2018-12-11 01:21:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_VERSION:
|
|
|
|
/*
|
|
|
|
* We implement some Hyper-V 2016 functions so let's use
|
|
|
|
* this version.
|
|
|
|
*/
|
|
|
|
ent->eax = 0x00003839;
|
|
|
|
ent->ebx = 0x000A0000;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_FEATURES:
|
2020-09-26 22:26:26 +08:00
|
|
|
ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE;
|
2018-12-11 01:21:56 +08:00
|
|
|
ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
|
2020-09-26 22:26:26 +08:00
|
|
|
ent->eax |= HV_MSR_SYNIC_AVAILABLE;
|
2018-12-11 01:21:56 +08:00
|
|
|
ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
|
2020-09-26 22:26:26 +08:00
|
|
|
ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE;
|
|
|
|
ent->eax |= HV_MSR_HYPERCALL_AVAILABLE;
|
|
|
|
ent->eax |= HV_MSR_VP_INDEX_AVAILABLE;
|
|
|
|
ent->eax |= HV_MSR_RESET_AVAILABLE;
|
2018-12-11 01:21:56 +08:00
|
|
|
ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
|
2020-09-26 22:26:26 +08:00
|
|
|
ent->eax |= HV_ACCESS_FREQUENCY_MSRS;
|
|
|
|
ent->eax |= HV_ACCESS_REENLIGHTENMENT;
|
2018-12-11 01:21:56 +08:00
|
|
|
|
2020-09-26 22:26:26 +08:00
|
|
|
ent->ebx |= HV_POST_MESSAGES;
|
|
|
|
ent->ebx |= HV_SIGNAL_EVENTS;
|
2018-12-11 01:21:56 +08:00
|
|
|
|
2021-05-26 17:03:56 +08:00
|
|
|
ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE;
|
2018-12-11 01:21:56 +08:00
|
|
|
ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
|
|
|
|
ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
|
2019-09-16 15:42:32 +08:00
|
|
|
|
ARM:
- Move the arch-specific code into arch/arm64/kvm
- Start the post-32bit cleanup
- Cherry-pick a few non-invasive pre-NV patches
x86:
- Rework of TLB flushing
- Rework of event injection, especially with respect to nested virtualization
- Nested AMD event injection facelift, building on the rework of generic code
and fixing a lot of corner cases
- Nested AMD live migration support
- Optimization for TSC deadline MSR writes and IPIs
- Various cleanups
- Asynchronous page fault cleanups (from tglx, common topic branch with tip tree)
- Interrupt-based delivery of asynchronous "page ready" events (host side)
- Hyper-V MSRs and hypercalls for guest debugging
- VMX preemption timer fixes
s390:
- Cleanups
Generic:
- switch vCPU thread wakeup from swait to rcuwait
The other architectures, and the guest side of the asynchronous page fault
work, will come next week.
-----BEGIN PGP SIGNATURE-----
iQFIBAABCAAyFiEE8TM4V0tmI4mGbHaCv/vSX3jHroMFAl7VJcYUHHBib256aW5p
QHJlZGhhdC5jb20ACgkQv/vSX3jHroPf6QgAq4wU5wdd1lTGz/i3DIhNVJNJgJlp
ozLzRdMaJbdbn5RpAK6PEBd9+pt3+UlojpFB3gpJh2Nazv2OzV4yLQgXXXyyMEx1
5Hg7b4UCJYDrbkCiegNRv7f/4FWDkQ9dx++RZITIbxeskBBCEI+I7GnmZhGWzuC4
7kj4ytuKAySF2OEJu0VQF6u0CvrNYfYbQIRKBXjtOwuRK4Q6L63FGMJpYo159MBQ
asg3B1jB5TcuGZ9zrjL5LkuzaP4qZZHIRs+4kZsH9I6MODHGUxKonrkablfKxyKy
CFK+iaHCuEXXty5K0VmWM3nrTfvpEjVjbMc7e1QGBQ5oXsDM0pqn84syRg==
=v7Wn
-----END PGP SIGNATURE-----
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm updates from Paolo Bonzini:
"ARM:
- Move the arch-specific code into arch/arm64/kvm
- Start the post-32bit cleanup
- Cherry-pick a few non-invasive pre-NV patches
x86:
- Rework of TLB flushing
- Rework of event injection, especially with respect to nested
virtualization
- Nested AMD event injection facelift, building on the rework of
generic code and fixing a lot of corner cases
- Nested AMD live migration support
- Optimization for TSC deadline MSR writes and IPIs
- Various cleanups
- Asynchronous page fault cleanups (from tglx, common topic branch
with tip tree)
- Interrupt-based delivery of asynchronous "page ready" events (host
side)
- Hyper-V MSRs and hypercalls for guest debugging
- VMX preemption timer fixes
s390:
- Cleanups
Generic:
- switch vCPU thread wakeup from swait to rcuwait
The other architectures, and the guest side of the asynchronous page
fault work, will come next week"
* tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (256 commits)
KVM: selftests: fix rdtsc() for vmx_tsc_adjust_test
KVM: check userspace_addr for all memslots
KVM: selftests: update hyperv_cpuid with SynDBG tests
x86/kvm/hyper-v: Add support for synthetic debugger via hypercalls
x86/kvm/hyper-v: enable hypercalls regardless of hypercall page
x86/kvm/hyper-v: Add support for synthetic debugger interface
x86/hyper-v: Add synthetic debugger definitions
KVM: selftests: VMX preemption timer migration test
KVM: nVMX: Fix VMX preemption timer migration
x86/kvm/hyper-v: Explicitly align hcall param for kvm_hyperv_exit
KVM: x86/pmu: Support full width counting
KVM: x86/pmu: Tweak kvm_pmu_get_msr to pass 'struct msr_data' in
KVM: x86: announce KVM_FEATURE_ASYNC_PF_INT
KVM: x86: acknowledgment mechanism for async pf page ready notifications
KVM: x86: interrupt based APF 'page ready' event delivery
KVM: introduce kvm_read_guest_offset_cached()
KVM: rename kvm_arch_can_inject_async_page_present() to kvm_arch_can_dequeue_async_page_present()
KVM: x86: extend struct kvm_vcpu_pv_apf_data with token info
Revert "KVM: async_pf: Fix #DF due to inject "Page not Present" and "Page Ready" exceptions simultaneously"
KVM: VMX: Replace zero-length array with flexible-array
...
2020-06-04 06:13:47 +08:00
|
|
|
ent->ebx |= HV_DEBUGGING;
|
2020-05-29 21:45:40 +08:00
|
|
|
ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE;
|
|
|
|
ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE;
|
|
|
|
|
2019-09-16 15:42:32 +08:00
|
|
|
/*
|
|
|
|
* Direct Synthetic timers only make sense with in-kernel
|
|
|
|
* LAPIC
|
|
|
|
*/
|
2020-09-29 23:09:43 +08:00
|
|
|
if (!vcpu || lapic_in_kernel(vcpu))
|
2019-09-16 15:42:32 +08:00
|
|
|
ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
|
2018-12-11 01:21:56 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_ENLIGHTMENT_INFO:
|
|
|
|
ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
|
|
|
|
ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
|
|
|
|
ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
|
|
|
|
ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
|
|
|
|
ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
|
2019-01-25 19:19:34 +08:00
|
|
|
if (evmcs_ver)
|
|
|
|
ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
|
2019-09-17 00:22:57 +08:00
|
|
|
if (!cpu_smt_possible())
|
|
|
|
ent->eax |= HV_X64_NO_NONARCH_CORESHARING;
|
2021-08-11 04:52:46 +08:00
|
|
|
|
|
|
|
ent->eax |= HV_DEPRECATING_AEOI_RECOMMENDED;
|
2018-12-11 01:21:56 +08:00
|
|
|
/*
|
|
|
|
* Default number of spinlock retry attempts, matches
|
|
|
|
* HyperV 2016.
|
|
|
|
*/
|
|
|
|
ent->ebx = 0x00000FFF;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_IMPLEMENT_LIMITS:
|
|
|
|
/* Maximum number of virtual processors */
|
|
|
|
ent->eax = KVM_MAX_VCPUS;
|
|
|
|
/*
|
|
|
|
* Maximum number of logical processors, matches
|
|
|
|
* HyperV 2016.
|
|
|
|
*/
|
|
|
|
ent->ebx = 64;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_NESTED_FEATURES:
|
|
|
|
ent->eax = evmcs_ver;
|
2021-11-29 17:47:04 +08:00
|
|
|
if (evmcs_ver)
|
|
|
|
ent->eax |= HV_X64_NESTED_MSR_BITMAP;
|
2018-12-11 01:21:56 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
2020-05-29 21:45:40 +08:00
|
|
|
case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS:
|
|
|
|
memcpy(signature, "Linux KVM Hv", 12);
|
|
|
|
|
|
|
|
ent->eax = 0;
|
|
|
|
ent->ebx = signature[0];
|
|
|
|
ent->ecx = signature[1];
|
|
|
|
ent->edx = signature[2];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_SYNDBG_INTERFACE:
|
|
|
|
memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12);
|
|
|
|
ent->eax = signature[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES:
|
|
|
|
ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
|
|
|
|
break;
|
|
|
|
|
2018-12-11 01:21:56 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_to_user(entries, cpuid_entries,
|
|
|
|
nent * sizeof(struct kvm_cpuid_entry2)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|