2018-03-20 22:02:11 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2022-11-01 22:53:44 +08:00
|
|
|
#ifndef __KVM_X86_VMX_HYPERV_H
|
|
|
|
#define __KVM_X86_VMX_HYPERV_H
|
2018-03-20 22:02:11 +08:00
|
|
|
|
2018-12-04 05:53:06 +08:00
|
|
|
#include <linux/jump_label.h>
|
2018-03-20 22:02:11 +08:00
|
|
|
|
2018-12-04 05:53:06 +08:00
|
|
|
#include <asm/hyperv-tlfs.h>
|
|
|
|
#include <asm/mshyperv.h>
|
|
|
|
#include <asm/vmx.h>
|
|
|
|
|
2022-11-01 22:53:44 +08:00
|
|
|
#include "../hyperv.h"
|
|
|
|
|
2018-12-04 05:53:06 +08:00
|
|
|
#include "capabilities.h"
|
|
|
|
#include "vmcs.h"
|
2020-02-05 20:30:34 +08:00
|
|
|
#include "vmcs12.h"
|
2018-12-04 05:53:06 +08:00
|
|
|
|
|
|
|
struct vmcs_config;
|
|
|
|
|
|
|
|
#define current_evmcs ((struct hv_enlightened_vmcs *)this_cpu_read(current_vmcs))
|
|
|
|
|
|
|
|
#define KVM_EVMCS_VERSION 1
|
|
|
|
|
2018-03-20 22:02:11 +08:00
|
|
|
struct evmcs_field {
|
|
|
|
u16 offset;
|
|
|
|
u16 clean_field;
|
|
|
|
};
|
|
|
|
|
2018-12-04 05:53:06 +08:00
|
|
|
extern const struct evmcs_field vmcs_field_to_evmcs_1[];
|
|
|
|
extern const unsigned int nr_evmcs_1_fields;
|
|
|
|
|
2022-01-13 01:01:33 +08:00
|
|
|
static __always_inline int evmcs_field_offset(unsigned long field,
|
|
|
|
u16 *clean_field)
|
2018-03-20 22:02:11 +08:00
|
|
|
{
|
|
|
|
unsigned int index = ROL16(field, 6);
|
|
|
|
const struct evmcs_field *evmcs_field;
|
|
|
|
|
2022-01-13 01:01:33 +08:00
|
|
|
if (unlikely(index >= nr_evmcs_1_fields))
|
2018-03-20 22:02:11 +08:00
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
evmcs_field = &vmcs_field_to_evmcs_1[index];
|
|
|
|
|
2022-01-13 01:01:33 +08:00
|
|
|
/*
|
|
|
|
* Use offset=0 to detect holes in eVMCS. This offset belongs to
|
|
|
|
* 'revision_id' but this field has no encoding and is supposed to
|
|
|
|
* be accessed directly.
|
|
|
|
*/
|
|
|
|
if (unlikely(!evmcs_field->offset))
|
|
|
|
return -ENOENT;
|
|
|
|
|
2018-03-20 22:02:11 +08:00
|
|
|
if (clean_field)
|
|
|
|
*clean_field = evmcs_field->clean_field;
|
|
|
|
|
|
|
|
return evmcs_field->offset;
|
|
|
|
}
|
|
|
|
|
2022-01-13 01:01:34 +08:00
|
|
|
static inline u64 evmcs_read_any(struct hv_enlightened_vmcs *evmcs,
|
|
|
|
unsigned long field, u16 offset)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* vmcs12_read_any() doesn't care whether the supplied structure
|
|
|
|
* is 'struct vmcs12' or 'struct hv_enlightened_vmcs' as it takes
|
|
|
|
* the exact offset of the required field, use it for convenience
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
return vmcs12_read_any((void *)evmcs, field, offset);
|
|
|
|
}
|
|
|
|
|
2022-01-13 01:01:33 +08:00
|
|
|
#if IS_ENABLED(CONFIG_HYPERV)
|
|
|
|
|
2023-02-11 08:35:34 +08:00
|
|
|
DECLARE_STATIC_KEY_FALSE(__kvm_is_using_evmcs);
|
2023-02-11 08:35:33 +08:00
|
|
|
|
|
|
|
static __always_inline bool kvm_is_using_evmcs(void)
|
|
|
|
{
|
2023-02-11 08:35:34 +08:00
|
|
|
return static_branch_unlikely(&__kvm_is_using_evmcs);
|
2023-02-11 08:35:33 +08:00
|
|
|
}
|
|
|
|
|
2022-01-13 01:01:33 +08:00
|
|
|
static __always_inline int get_evmcs_offset(unsigned long field,
|
|
|
|
u16 *clean_field)
|
|
|
|
{
|
|
|
|
int offset = evmcs_field_offset(field, clean_field);
|
|
|
|
|
KVM: x86: Unify pr_fmt to use module name for all KVM modules
Define pr_fmt using KBUILD_MODNAME for all KVM x86 code so that printks
use consistent formatting across common x86, Intel, and AMD code. In
addition to providing consistent print formatting, using KBUILD_MODNAME,
e.g. kvm_amd and kvm_intel, allows referencing SVM and VMX (and SEV and
SGX and ...) as technologies without generating weird messages, and
without causing naming conflicts with other kernel code, e.g. "SEV: ",
"tdx: ", "sgx: " etc.. are all used by the kernel for non-KVM subsystems.
Opportunistically move away from printk() for prints that need to be
modified anyways, e.g. to drop a manual "kvm: " prefix.
Opportunistically convert a few SGX WARNs that are similarly modified to
WARN_ONCE; in the very unlikely event that the WARNs fire, odds are good
that they would fire repeatedly and spam the kernel log without providing
unique information in each print.
Note, defining pr_fmt yields undesirable results for code that uses KVM's
printk wrappers, e.g. vcpu_unimpl(). But, that's a pre-existing problem
as SVM/kvm_amd already defines a pr_fmt, and thankfully use of KVM's
wrappers is relatively limited in KVM x86 code.
Signed-off-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Paul Durrant <paul@xen.org>
Message-Id: <20221130230934.1014142-35-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2022-12-01 07:09:18 +08:00
|
|
|
WARN_ONCE(offset < 0, "accessing unsupported EVMCS field %lx\n", field);
|
2022-01-13 01:01:33 +08:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2021-06-24 17:41:07 +08:00
|
|
|
static __always_inline void evmcs_write64(unsigned long field, u64 value)
|
2018-12-04 05:53:06 +08:00
|
|
|
{
|
|
|
|
u16 clean_field;
|
|
|
|
int offset = get_evmcs_offset(field, &clean_field);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*(u64 *)((char *)current_evmcs + offset) = value;
|
|
|
|
|
|
|
|
current_evmcs->hv_clean_fields &= ~clean_field;
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:09:08 +08:00
|
|
|
static __always_inline void evmcs_write32(unsigned long field, u32 value)
|
2018-12-04 05:53:06 +08:00
|
|
|
{
|
|
|
|
u16 clean_field;
|
|
|
|
int offset = get_evmcs_offset(field, &clean_field);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*(u32 *)((char *)current_evmcs + offset) = value;
|
|
|
|
current_evmcs->hv_clean_fields &= ~clean_field;
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:09:08 +08:00
|
|
|
static __always_inline void evmcs_write16(unsigned long field, u16 value)
|
2018-12-04 05:53:06 +08:00
|
|
|
{
|
|
|
|
u16 clean_field;
|
|
|
|
int offset = get_evmcs_offset(field, &clean_field);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
*(u16 *)((char *)current_evmcs + offset) = value;
|
|
|
|
current_evmcs->hv_clean_fields &= ~clean_field;
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:09:08 +08:00
|
|
|
static __always_inline u64 evmcs_read64(unsigned long field)
|
2018-12-04 05:53:06 +08:00
|
|
|
{
|
|
|
|
int offset = get_evmcs_offset(field, NULL);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return *(u64 *)((char *)current_evmcs + offset);
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:09:08 +08:00
|
|
|
static __always_inline u32 evmcs_read32(unsigned long field)
|
2018-12-04 05:53:06 +08:00
|
|
|
{
|
|
|
|
int offset = get_evmcs_offset(field, NULL);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return *(u32 *)((char *)current_evmcs + offset);
|
|
|
|
}
|
|
|
|
|
2022-12-13 14:09:08 +08:00
|
|
|
static __always_inline u16 evmcs_read16(unsigned long field)
|
2018-12-04 05:53:06 +08:00
|
|
|
{
|
|
|
|
int offset = get_evmcs_offset(field, NULL);
|
|
|
|
|
|
|
|
if (offset < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return *(u16 *)((char *)current_evmcs + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void evmcs_load(u64 phys_addr)
|
|
|
|
{
|
|
|
|
struct hv_vp_assist_page *vp_ap =
|
|
|
|
hv_get_vp_assist_page(smp_processor_id());
|
|
|
|
|
2019-08-22 22:30:21 +08:00
|
|
|
if (current_evmcs->hv_enlightenments_control.nested_flush_hypercall)
|
|
|
|
vp_ap->nested_control.features.directhypercall = 1;
|
2018-12-04 05:53:06 +08:00
|
|
|
vp_ap->current_nested_vmcs = phys_addr;
|
|
|
|
vp_ap->enlighten_vmentry = 1;
|
|
|
|
}
|
|
|
|
|
2022-12-01 07:09:23 +08:00
|
|
|
void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf);
|
2018-12-04 05:53:06 +08:00
|
|
|
#else /* !IS_ENABLED(CONFIG_HYPERV) */
|
2023-02-11 08:35:33 +08:00
|
|
|
static __always_inline bool kvm_is_using_evmcs(void) { return false; }
|
2021-06-24 17:41:07 +08:00
|
|
|
static __always_inline void evmcs_write64(unsigned long field, u64 value) {}
|
2022-12-13 14:09:08 +08:00
|
|
|
static __always_inline void evmcs_write32(unsigned long field, u32 value) {}
|
|
|
|
static __always_inline void evmcs_write16(unsigned long field, u16 value) {}
|
|
|
|
static __always_inline u64 evmcs_read64(unsigned long field) { return 0; }
|
|
|
|
static __always_inline u32 evmcs_read32(unsigned long field) { return 0; }
|
|
|
|
static __always_inline u16 evmcs_read16(unsigned long field) { return 0; }
|
2018-12-04 05:53:06 +08:00
|
|
|
static inline void evmcs_load(u64 phys_addr) {}
|
|
|
|
#endif /* IS_ENABLED(CONFIG_HYPERV) */
|
|
|
|
|
2021-05-26 21:20:16 +08:00
|
|
|
#define EVMPTR_INVALID (-1ULL)
|
2021-05-26 21:20:20 +08:00
|
|
|
#define EVMPTR_MAP_PENDING (-2ULL)
|
2021-05-26 21:20:16 +08:00
|
|
|
|
|
|
|
static inline bool evmptr_is_valid(u64 evmptr)
|
|
|
|
{
|
2021-05-26 21:20:20 +08:00
|
|
|
return evmptr != EVMPTR_INVALID && evmptr != EVMPTR_MAP_PENDING;
|
2021-05-26 21:20:16 +08:00
|
|
|
}
|
|
|
|
|
2020-03-09 23:52:13 +08:00
|
|
|
enum nested_evmptrld_status {
|
|
|
|
EVMPTRLD_DISABLED,
|
|
|
|
EVMPTRLD_SUCCEEDED,
|
|
|
|
EVMPTRLD_VMFAIL,
|
|
|
|
EVMPTRLD_ERROR,
|
|
|
|
};
|
|
|
|
|
2022-11-01 22:54:03 +08:00
|
|
|
u64 nested_get_evmptr(struct kvm_vcpu *vcpu);
|
2018-12-11 01:21:55 +08:00
|
|
|
uint16_t nested_get_evmcs_version(struct kvm_vcpu *vcpu);
|
2018-12-04 05:53:06 +08:00
|
|
|
int nested_enable_evmcs(struct kvm_vcpu *vcpu,
|
|
|
|
uint16_t *vmcs_version);
|
2022-08-30 21:37:19 +08:00
|
|
|
void nested_evmcs_filter_control_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
|
2020-02-05 20:30:34 +08:00
|
|
|
int nested_evmcs_check_controls(struct vmcs12 *vmcs12);
|
2022-11-01 22:54:04 +08:00
|
|
|
bool nested_evmcs_l2_tlb_flush_enabled(struct kvm_vcpu *vcpu);
|
2022-11-01 22:53:59 +08:00
|
|
|
void vmx_hv_inject_synthetic_vmexit_post_tlb_flush(struct kvm_vcpu *vcpu);
|
2018-12-04 05:53:06 +08:00
|
|
|
|
2022-11-01 22:53:44 +08:00
|
|
|
#endif /* __KVM_X86_VMX_HYPERV_H */
|