Skip to content

Commit

Permalink
KVM: nVMX: Implement evmcs_field_offset() suitable for handle_vmread()
Browse files Browse the repository at this point in the history
In preparation to allowing reads from Enlightened VMCS from
handle_vmread(), implement evmcs_field_offset() to get the correct
read offset. get_evmcs_offset(), which is being used by KVM-on-Hyper-V,
is almost what's needed but a few things need to be adjusted. First,
WARN_ON() is unacceptable for handle_vmread() as any field can (in
theory) be supplied by the guest and not all fields are defined in
eVMCS v1. Second, we need to handle 'holes' in eVMCS (missing fields).
It also sounds like a good idea to WARN_ON() if such fields are ever
accessed by KVM-on-Hyper-V.

Implement dedicated evmcs_field_offset() helper.

No functional change intended.

Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Message-Id: <20220112170134.1904308-5-vkuznets@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
Vitaly Kuznetsov authored and Paolo Bonzini committed Jan 28, 2022
1 parent 2423a4c commit 892a42c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
3 changes: 1 addition & 2 deletions arch/x86/kvm/vmx/evmcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

DEFINE_STATIC_KEY_FALSE(enable_evmcs);

#if IS_ENABLED(CONFIG_HYPERV)

#define EVMCS1_OFFSET(x) offsetof(struct hv_enlightened_vmcs, x)
#define EVMCS1_FIELD(number, name, clean_field)[ROL16(number, 6)] = \
{EVMCS1_OFFSET(name), clean_field}
Expand Down Expand Up @@ -296,6 +294,7 @@ const struct evmcs_field vmcs_field_to_evmcs_1[] = {
};
const unsigned int nr_evmcs_1_fields = ARRAY_SIZE(vmcs_field_to_evmcs_1);

#if IS_ENABLED(CONFIG_HYPERV)
__init void evmcs_sanitize_exec_ctrls(struct vmcs_config *vmcs_conf)
{
vmcs_conf->pin_based_exec_ctrl &= ~EVMCS1_UNSUPPORTED_PINCTRL;
Expand Down
32 changes: 24 additions & 8 deletions arch/x86/kvm/vmx/evmcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ DECLARE_STATIC_KEY_FALSE(enable_evmcs);
#define EVMCS1_UNSUPPORTED_VMENTRY_CTRL (VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
#define EVMCS1_UNSUPPORTED_VMFUNC (VMX_VMFUNC_EPTP_SWITCHING)

#if IS_ENABLED(CONFIG_HYPERV)

struct evmcs_field {
u16 offset;
u16 clean_field;
Expand All @@ -75,26 +73,44 @@ struct evmcs_field {
extern const struct evmcs_field vmcs_field_to_evmcs_1[];
extern const unsigned int nr_evmcs_1_fields;

static __always_inline int get_evmcs_offset(unsigned long field,
u16 *clean_field)
static __always_inline int evmcs_field_offset(unsigned long field,
u16 *clean_field)
{
unsigned int index = ROL16(field, 6);
const struct evmcs_field *evmcs_field;

if (unlikely(index >= nr_evmcs_1_fields)) {
WARN_ONCE(1, "KVM: accessing unsupported EVMCS field %lx\n",
field);
if (unlikely(index >= nr_evmcs_1_fields))
return -ENOENT;
}

evmcs_field = &vmcs_field_to_evmcs_1[index];

/*
* 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;

if (clean_field)
*clean_field = evmcs_field->clean_field;

return evmcs_field->offset;
}

#if IS_ENABLED(CONFIG_HYPERV)

static __always_inline int get_evmcs_offset(unsigned long field,
u16 *clean_field)
{
int offset = evmcs_field_offset(field, clean_field);

WARN_ONCE(offset < 0, "KVM: accessing unsupported EVMCS field %lx\n",
field);

return offset;
}

static __always_inline void evmcs_write64(unsigned long field, u64 value)
{
u16 clean_field;
Expand Down

0 comments on commit 892a42c

Please sign in to comment.