Skip to content

Commit c97b106

Browse files
xinli-intelsean-jc
authored andcommitted
KVM: nVMX: Use macros and #defines in vmx_restore_vmx_basic()
Use macros in vmx_restore_vmx_basic() instead of open coding everything using BIT_ULL() and GENMASK_ULL(). Opportunistically split feature bits and reserved bits into separate variables, and add a comment explaining the subset logic (it's not immediately obvious that the set of feature bits is NOT the set of _supported_ feature bits). Cc: Shan Kang <[email protected]> Cc: Kai Huang <[email protected]> Signed-off-by: Xin Li <[email protected]> [sean: split to separate patch, write changelog, drop #defines] Reviewed-by: Zhao Liu <[email protected]> Reviewed-by: Xiaoyao Li <[email protected]> Reviewed-by: Kai Huang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 9df398f commit c97b106

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

arch/x86/kvm/vmx/nested.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,21 +1251,32 @@ static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask)
12511251

12521252
static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data)
12531253
{
1254-
const u64 feature_and_reserved =
1255-
/* feature (except bit 48; see below) */
1256-
BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |
1257-
/* reserved */
1258-
BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56);
1254+
const u64 feature_bits = VMX_BASIC_DUAL_MONITOR_TREATMENT |
1255+
VMX_BASIC_INOUT |
1256+
VMX_BASIC_TRUE_CTLS;
1257+
1258+
const u64 reserved_bits = GENMASK_ULL(63, 56) |
1259+
GENMASK_ULL(47, 45) |
1260+
BIT_ULL(31);
1261+
12591262
u64 vmx_basic = vmcs_config.nested.basic;
12601263

1261-
if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved))
1264+
BUILD_BUG_ON(feature_bits & reserved_bits);
1265+
1266+
/*
1267+
* Except for 32BIT_PHYS_ADDR_ONLY, which is an anti-feature bit (has
1268+
* inverted polarity), the incoming value must not set feature bits or
1269+
* reserved bits that aren't allowed/supported by KVM. Fields, i.e.
1270+
* multi-bit values, are explicitly checked below.
1271+
*/
1272+
if (!is_bitwise_subset(vmx_basic, data, feature_bits | reserved_bits))
12621273
return -EINVAL;
12631274

12641275
/*
12651276
* KVM does not emulate a version of VMX that constrains physical
12661277
* addresses of VMX structures (e.g. VMCS) to 32-bits.
12671278
*/
1268-
if (data & BIT_ULL(48))
1279+
if (data & VMX_BASIC_32BIT_PHYS_ADDR_ONLY)
12691280
return -EINVAL;
12701281

12711282
if (vmx_basic_vmcs_revision_id(vmx_basic) !=

0 commit comments

Comments
 (0)