From nobody Sun Feb 8 13:09:17 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E31DB20328 for ; Sat, 9 Mar 2024 01:27:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947655; cv=none; b=QLdnaj16ASeP9kggW+Y8OwHy2UW83CD7zZMXumZqgx8OhRgTA/t9/pWhPzRYA0vD6NQdBpa9aM6LLUTSI/iurWVWgE0bXGssYMmlY6dScnpZG+kvKyXFEYQ+ZPeeUkCTrb/LGBbh0VwC98ecdxitGYaaeucPfXyZY6+UJ2pOUUs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947655; c=relaxed/simple; bh=pSKQNm5JvNwxiEm9TrDvsr+6vXSmj4xUeeMJYJlK4/0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZaZ3cBvsZ6ClwZHd33axEYR6cnF/JjA1VBeawHj1NmZNJYFRmc2mBIjZmClze4UPIvSheXP90vnNFS9pTZh2Tg2XTFtlS0fHaGkEKvt4VE2s5mQ1qrOcokE2hByxbF/pg2Wmyeh4UpnsXc6LprKL+B/80yKv9+dzVhrn6BdMQAo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=zvlTCoei; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="zvlTCoei" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-609c800881fso45767047b3.0 for ; Fri, 08 Mar 2024 17:27:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947653; x=1710552453; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=EaBCMidXAkgUGM8CxSfXfUhRT077xv0MmYJnGKo2lwk=; b=zvlTCoeiFrJkV81ymSGjQ/gtJ1ZNhPXFfRaAgG4SD6pib8VRDxpNy99RLk48A94G1i H5xqVfFStXlo0uApFPkhekFN1aiiLjBMs2mVT1qOULEIPZYBYYizLHPQyVrk8P/7tgJh s/RE1qYRZ4DXO7jc0BaMN7OhctKxuBAe3NusOTOI0I4J+/B+mBXIuRtuof2GxAd9o9Bf 5QHJvBisj6DloAIiR8LSMwLdfOSXIhpUmMJnGGfUsXTEl1OzSRZL8U1HDoEjvP3y+nGv 3meDgiAu3fmklRH7tufq5YO+WiFbagQwuq4JtuL+W1yzjP5X61oou4B2AwLYJPc3mPqU pE5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947653; x=1710552453; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=EaBCMidXAkgUGM8CxSfXfUhRT077xv0MmYJnGKo2lwk=; b=OvnOIeFIEonoyj6luc2hd/G9SB1BQIquIHm3TfODl7kks25rAwoq4tzlaYQ86TS9hj 6NNqhFG6D7yw4NkWv6/rVCI0eQAcKgfOLRngh44efIZVFlUe93jTGC65soYhaCdl3feZ CcXYN+HCv+LC4sYJu4/Rme8Hv6om999U+Ab958BDaycIPsFsCss2OiiWi+xmr2d2dLt4 OXRVI7ooihznsJxsob9uEEpy7h4GVTcQ/TfJk8/yEOL7c1mLqHTgrD20brWceNe89nqj gBvG1BqU811gp0t9vHWVsBKlsYM0UG0TuLSkfpmTzaDZDRwNEbGsmxMF66zP8mfawYdy lbCQ== X-Gm-Message-State: AOJu0YxofuOV/pKl/nIuG6VRa0T5rVM7Ryv3W4sgavDFVtrAJw+rT2Q6 E+XZEWzkJohetS3mu4IX7WFHI2gKl5iWpZmxMPZKv7lZYkGKtZ/EaD2wEIAgXCSXjoe10gOu1hs C4Q== X-Google-Smtp-Source: AGHT+IGJKBjd+vklKwLu3ypQCI2MgbZgIC4pHjjoZgBjV4khc/YSZk3awEL+tQe1YSQxZ3H38MuL2HIekWI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:690d:0:b0:dc6:e5e9:f3af with SMTP id e13-20020a25690d000000b00dc6e5e9f3afmr108780ybc.9.1709947652925; Fri, 08 Mar 2024 17:27:32 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:17 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-2-seanjc@google.com> Subject: [PATCH v6 1/9] x86/cpu: KVM: Add common defines for architectural memory types (PAT, MTRRs, etc.) From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add defines for the architectural memory types that can be shoved into various MSRs and registers, e.g. MTRRs, PAT, VMX capabilities MSRs, EPTPs, etc. While most MSRs/registers support only a subset of all memory types, the values themselves are architectural and identical across all users. Leave the goofy MTRR_TYPE_* definitions as-is since they are in a uapi header, but add compile-time assertions to connect the dots (and sanity check that the msr-index.h values didn't get fat-fingered). Keep the VMX_EPTP_MT_* defines so that it's slightly more obvious that the EPTP holds a single memory type in 3 of its 64 bits; those bits just happen to be 2:0, i.e. don't need to be shifted. Opportunistically use X86_MEMTYPE_WB instead of an open coded '6' in setup_vmcs_config(). No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/msr-index.h | 15 ++++++++++++++- arch/x86/include/asm/vmx.h | 5 +++-- arch/x86/kernel/cpu/mtrr/mtrr.c | 6 ++++++ arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/mm/pat/memtype.c | 33 ++++++++++++-------------------- 6 files changed, 37 insertions(+), 26 deletions(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-in= dex.h index f1bd7b91b3c6..29f0ea78e41c 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -36,6 +36,20 @@ #define EFER_FFXSR (1<<_EFER_FFXSR) #define EFER_AUTOIBRS (1<<_EFER_AUTOIBRS) =20 +/* + * Architectural memory types that are common to MTRRs, PAT, VMX MSRs, etc. + * Most MSRs support/allow only a subset of memory types, but the values + * themselves are common across all relevant MSRs. + */ +#define X86_MEMTYPE_UC 0ull /* Uncacheable, a.k.a. Strong Uncacheable */ +#define X86_MEMTYPE_WC 1ull /* Write Combining */ +/* RESERVED 2 */ +/* RESERVED 3 */ +#define X86_MEMTYPE_WT 4ull /* Write Through */ +#define X86_MEMTYPE_WP 5ull /* Write Protected */ +#define X86_MEMTYPE_WB 6ull /* Write Back */ +#define X86_MEMTYPE_UC_MINUS 7ull /* Weak Uncacheabled (PAT only) */ + /* Intel MSRs. Some also available on other CPUs */ =20 #define MSR_TEST_CTRL 0x00000033 @@ -1108,7 +1122,6 @@ #define VMX_BASIC_64 0x0001000000000000LLU #define VMX_BASIC_MEM_TYPE_SHIFT 50 #define VMX_BASIC_MEM_TYPE_MASK 0x003c000000000000LLU -#define VMX_BASIC_MEM_TYPE_WB 6LLU #define VMX_BASIC_INOUT 0x0040000000000000LLU =20 /* Resctrl MSRs: */ diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index 0e73616b82f3..4fdc76263066 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -504,9 +504,10 @@ enum vmcs_field { #define VMX_EPTP_PWL_4 0x18ull #define VMX_EPTP_PWL_5 0x20ull #define VMX_EPTP_AD_ENABLE_BIT (1ull << 6) +/* The EPTP memtype is encoded in bits 2:0, i.e. doesn't need to be shifte= d. */ #define VMX_EPTP_MT_MASK 0x7ull -#define VMX_EPTP_MT_WB 0x6ull -#define VMX_EPTP_MT_UC 0x0ull +#define VMX_EPTP_MT_WB X86_MEMTYPE_WB +#define VMX_EPTP_MT_UC X86_MEMTYPE_UC #define VMX_EPT_READABLE_MASK 0x1ull #define VMX_EPT_WRITABLE_MASK 0x2ull #define VMX_EPT_EXECUTABLE_MASK 0x4ull diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtr= r.c index 767bf1c71aad..125e36010b82 100644 --- a/arch/x86/kernel/cpu/mtrr/mtrr.c +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c @@ -55,6 +55,12 @@ =20 #include "mtrr.h" =20 +static_assert(X86_MEMTYPE_UC =3D=3D MTRR_TYPE_UNCACHABLE); +static_assert(X86_MEMTYPE_WC =3D=3D MTRR_TYPE_WRCOMB); +static_assert(X86_MEMTYPE_WT =3D=3D MTRR_TYPE_WRTHROUGH); +static_assert(X86_MEMTYPE_WP =3D=3D MTRR_TYPE_WRPROT); +static_assert(X86_MEMTYPE_WB =3D=3D MTRR_TYPE_WRBACK); + /* arch_phys_wc_add returns an MTRR register index plus this offset. */ #define MTRR_TO_PHYS_WC_OFFSET 1000 =20 diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index d05ddf751491..82a35aba7d2b 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -7006,7 +7006,7 @@ static void nested_vmx_setup_basic(struct nested_vmx_= msrs *msrs) VMCS12_REVISION | VMX_BASIC_TRUE_CTLS | ((u64)VMCS12_SIZE << VMX_BASIC_VMCS_SIZE_SHIFT) | - (VMX_BASIC_MEM_TYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); + (X86_MEMTYPE_WB << VMX_BASIC_MEM_TYPE_SHIFT); =20 if (cpu_has_vmx_basic_inout()) msrs->basic |=3D VMX_BASIC_INOUT; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 7a74388f9ecf..71cc6e3b3221 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2692,7 +2692,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs= _conf, #endif =20 /* Require Write-Back (WB) memory type for VMCS accesses. */ - if (((vmx_msr_high >> 18) & 15) !=3D 6) + if (((vmx_msr_high >> 18) & 15) !=3D X86_MEMTYPE_WB) return -EIO; =20 rdmsrl(MSR_IA32_VMX_MISC, misc_msr); diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index 0904d7e8e126..3e0ba044925f 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -175,15 +175,6 @@ static inline void set_page_memtype(struct page *pg, } #endif =20 -enum { - PAT_UC =3D 0, /* uncached */ - PAT_WC =3D 1, /* Write combining */ - PAT_WT =3D 4, /* Write Through */ - PAT_WP =3D 5, /* Write Protected */ - PAT_WB =3D 6, /* Write Back (default) */ - PAT_UC_MINUS =3D 7, /* UC, but can be overridden by MTRR */ -}; - #define CM(c) (_PAGE_CACHE_MODE_ ## c) =20 static enum page_cache_mode __init pat_get_cache_mode(unsigned int pat_val, @@ -193,13 +184,13 @@ static enum page_cache_mode __init pat_get_cache_mode= (unsigned int pat_val, char *cache_mode; =20 switch (pat_val) { - case PAT_UC: cache =3D CM(UC); cache_mode =3D "UC "; break; - case PAT_WC: cache =3D CM(WC); cache_mode =3D "WC "; break; - case PAT_WT: cache =3D CM(WT); cache_mode =3D "WT "; break; - case PAT_WP: cache =3D CM(WP); cache_mode =3D "WP "; break; - case PAT_WB: cache =3D CM(WB); cache_mode =3D "WB "; break; - case PAT_UC_MINUS: cache =3D CM(UC_MINUS); cache_mode =3D "UC- "; break; - default: cache =3D CM(WB); cache_mode =3D "WB "; break; + case X86_MEMTYPE_UC: cache =3D CM(UC); cache_mode =3D "UC ";= break; + case X86_MEMTYPE_WC: cache =3D CM(WC); cache_mode =3D "WC ";= break; + case X86_MEMTYPE_WT: cache =3D CM(WT); cache_mode =3D "WT ";= break; + case X86_MEMTYPE_WP: cache =3D CM(WP); cache_mode =3D "WP ";= break; + case X86_MEMTYPE_WB: cache =3D CM(WB); cache_mode =3D "WB ";= break; + case X86_MEMTYPE_UC_MINUS: cache =3D CM(UC_MINUS); cache_mode =3D "UC- ";= break; + default: cache =3D CM(WB); cache_mode =3D "WB ";= break; } =20 memcpy(msg, cache_mode, 4); @@ -254,11 +245,11 @@ void pat_cpu_init(void) void __init pat_bp_init(void) { struct cpuinfo_x86 *c =3D &boot_cpu_data; -#define PAT(p0, p1, p2, p3, p4, p5, p6, p7) \ - (((u64)PAT_ ## p0) | ((u64)PAT_ ## p1 << 8) | \ - ((u64)PAT_ ## p2 << 16) | ((u64)PAT_ ## p3 << 24) | \ - ((u64)PAT_ ## p4 << 32) | ((u64)PAT_ ## p5 << 40) | \ - ((u64)PAT_ ## p6 << 48) | ((u64)PAT_ ## p7 << 56)) +#define PAT(p0, p1, p2, p3, p4, p5, p6, p7) \ + ((X86_MEMTYPE_ ## p0) | (X86_MEMTYPE_ ## p1 << 8) | \ + (X86_MEMTYPE_ ## p2 << 16) | (X86_MEMTYPE_ ## p3 << 24) | \ + (X86_MEMTYPE_ ## p4 << 32) | (X86_MEMTYPE_ ## p5 << 40) | \ + (X86_MEMTYPE_ ## p6 << 48) | (X86_MEMTYPE_ ## p7 << 56)) =20 =20 if (!IS_ENABLED(CONFIG_X86_PAT)) --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D00B20B12 for ; Sat, 9 Mar 2024 01:27:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947656; cv=none; b=D0LPzqc+UqdDhN54754wL8i+SZILAHWsOROvZMBc6rHpNLjboUBKOXMCjbPLU1jP6e+mXpg3EivNCmg/nyq3OreVNIitolPApIZ9FqeY+GXUYcpSZ3RVVKoQpaEO3uAoVhTwxOaL2tt54qiYEmprqg6xXGl+6SQLLhl6pX/5OLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947656; c=relaxed/simple; bh=rUvlDX/garBo7FQGCqJMZMq6R0UBQe0xv8vny/tefDc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TR2OD2K7ZvrN2P1KVeXMOVbW5/7qb1xGG3eFr9/rURqz7hnm/8VHxIMfqMPVn6RLbSs1CcNE7kOpB1NF8Zvq6ARmjXaNWWdJj0Mg+bcUCryv54P4EePEXlcx8y4/s6VmYnoOIkueemxH2Bi+czWaTnxO5veEJTxwZBBsGZXZF7w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IFnnJqxw; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IFnnJqxw" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-5c66a69ec8eso2307320a12.3 for ; Fri, 08 Mar 2024 17:27:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947654; x=1710552454; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=bOeR0l241/HNIRQwF4+765+GZlbUNLMWslpjXSL9lcI=; b=IFnnJqxw3UnRCEpHPAQiTz6fMulfyA4wnvtPnQmxT29Fp1oAxQcvorE8w8h/zqDIfW vnmnh4VlUoZ0LFjkNTmEsp5nPlmwfulaEsgC8b8HFhwXZHM6n735jLOZK+l6W9NegVcz 3QG3nWg7qodbYvwpV9Tvdp0E9NpWEvSb3kTzmMDSFpLoqaKMbvD4q1GLUaM8+g5ShKU8 FVMHYlQj529mRu2nE6o6uasLxDJ85P/KPydhJpdrA4A6Ahpu1BAXp2+iIbgrO5Q3riU3 qgeoM+RTf6431QyG+9To7NczxCDqmumcBL1/tR3HJmK3923UpxnZo4vfm73XzvBUaapt XlyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947654; x=1710552454; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bOeR0l241/HNIRQwF4+765+GZlbUNLMWslpjXSL9lcI=; b=jnPz+ljEGs8CfZ7DogjpJM2uVq+QF7Fd7mHt003qU/djF5uh0mt3KKRuavAGcIsuPM zleGkv3xrCWfB0Cs0Iu6SVcZeqikbgxX2g6j7DTii8UxkQ9MHMxtjVd3JR5Vkf/cxKuH Q+aJgDwvFtOtnvhh5mtI21VcEsVVqiX5iOOzyqV/QU5TDCNL5m8/7xONHbaKQkrBY/EX AYgGAJkm8dLWJc87sgypmGLKZkU26RUM1DUjVpCfXo5kL3hh7xYuHsDF0CT/Tp5SdBE5 xhALr0pmifMPjwbG+kqGH1bAxpYzwSY7g0gkRWtgBERSwd/Wmmv/5+SQo64CLkWgM75j PsJQ== X-Gm-Message-State: AOJu0YwFUzffiJShYadEoM6fchlGZoXRXtLK5QOQKtwkR+/l2QGwwj17 KW+K0qYQ4C4XoFjRTWPAm/iyKslTdQzpcyDkF+4cx2ADaN3B47Sh4FhSHgnQhNncWGBvHyaVnIY KMg== X-Google-Smtp-Source: AGHT+IEwL6cOqFwCHrQ/97euZuNN3v/7TvrgfRqK8reM7VfQa8XJ7pZXhuIV3Bnnoq/uRQOxrC2CEMSby7g= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1741:b0:6e6:2a08:ba90 with SMTP id j1-20020a056a00174100b006e62a08ba90mr60420pfc.3.1709947654671; Fri, 08 Mar 2024 17:27:34 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:18 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-3-seanjc@google.com> Subject: [PATCH v6 2/9] x86/cpu: KVM: Move macro to encode PAT value to common header From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move pat/memtype.c's PAT() macro to msr-index.h as PAT_VALUE(), and use it in KVM to define the default (Power-On / RESET) PAT value instead of open coding an inscrutable magic number. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Reviewed-by: Xiaoyao Li --- arch/x86/include/asm/msr-index.h | 6 ++++++ arch/x86/kvm/x86.h | 3 ++- arch/x86/mm/pat/memtype.c | 12 +++--------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-in= dex.h index 29f0ea78e41c..af71f8bb76ae 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -352,6 +352,12 @@ =20 #define MSR_IA32_CR_PAT 0x00000277 =20 +#define PAT_VALUE(p0, p1, p2, p3, p4, p5, p6, p7) \ + ((X86_MEMTYPE_ ## p0) | (X86_MEMTYPE_ ## p1 << 8) | \ + (X86_MEMTYPE_ ## p2 << 16) | (X86_MEMTYPE_ ## p3 << 24) | \ + (X86_MEMTYPE_ ## p4 << 32) | (X86_MEMTYPE_ ## p5 << 40) | \ + (X86_MEMTYPE_ ## p6 << 48) | (X86_MEMTYPE_ ## p7 << 56)) + #define MSR_IA32_DEBUGCTLMSR 0x000001d9 #define MSR_IA32_LASTBRANCHFROMIP 0x000001db #define MSR_IA32_LASTBRANCHTOIP 0x000001dc diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index a8b71803777b..753403639e72 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -87,7 +87,8 @@ static inline unsigned int __shrink_ple_window(unsigned i= nt val, return max(val, min); } =20 -#define MSR_IA32_CR_PAT_DEFAULT 0x0007040600070406ULL +#define MSR_IA32_CR_PAT_DEFAULT \ + PAT_VALUE(WB, WT, UC_MINUS, UC, WB, WT, UC_MINUS, UC) =20 void kvm_service_local_tlb_flush_requests(struct kvm_vcpu *vcpu); int kvm_check_nested_events(struct kvm_vcpu *vcpu); diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c index 3e0ba044925f..f2dedddfbaf2 100644 --- a/arch/x86/mm/pat/memtype.c +++ b/arch/x86/mm/pat/memtype.c @@ -245,12 +245,6 @@ void pat_cpu_init(void) void __init pat_bp_init(void) { struct cpuinfo_x86 *c =3D &boot_cpu_data; -#define PAT(p0, p1, p2, p3, p4, p5, p6, p7) \ - ((X86_MEMTYPE_ ## p0) | (X86_MEMTYPE_ ## p1 << 8) | \ - (X86_MEMTYPE_ ## p2 << 16) | (X86_MEMTYPE_ ## p3 << 24) | \ - (X86_MEMTYPE_ ## p4 << 32) | (X86_MEMTYPE_ ## p5 << 40) | \ - (X86_MEMTYPE_ ## p6 << 48) | (X86_MEMTYPE_ ## p7 << 56)) - =20 if (!IS_ENABLED(CONFIG_X86_PAT)) pr_info_once("x86/PAT: PAT support disabled because CONFIG_X86_PAT is di= sabled in the kernel.\n"); @@ -281,7 +275,7 @@ void __init pat_bp_init(void) * NOTE: When WC or WP is used, it is redirected to UC- per * the default setup in __cachemode2pte_tbl[]. */ - pat_msr_val =3D PAT(WB, WT, UC_MINUS, UC, WB, WT, UC_MINUS, UC); + pat_msr_val =3D PAT_VALUE(WB, WT, UC_MINUS, UC, WB, WT, UC_MINUS, UC); } =20 /* @@ -321,7 +315,7 @@ void __init pat_bp_init(void) * NOTE: When WT or WP is used, it is redirected to UC- per * the default setup in __cachemode2pte_tbl[]. */ - pat_msr_val =3D PAT(WB, WC, UC_MINUS, UC, WB, WC, UC_MINUS, UC); + pat_msr_val =3D PAT_VALUE(WB, WC, UC_MINUS, UC, WB, WC, UC_MINUS, UC); } else { /* * Full PAT support. We put WT in slot 7 to improve @@ -349,7 +343,7 @@ void __init pat_bp_init(void) * The reserved slots are unused, but mapped to their * corresponding types in the presence of PAT errata. */ - pat_msr_val =3D PAT(WB, WC, UC_MINUS, UC, WB, WP, UC_MINUS, WT); + pat_msr_val =3D PAT_VALUE(WB, WC, UC_MINUS, UC, WB, WP, UC_MINUS, WT); } =20 memory_caching_control |=3D CACHE_PAT; --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 505FB1400D for ; Sat, 9 Mar 2024 01:27:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947658; cv=none; b=ZrNHX7C69+VJ9drIFaGaucWXWOHiPKhAl14FkkyC+/O+8fCht7w6ZMSxoT16UOG9BRRQ8ea0Dxq3ObylNW9DAjJ+CiRe+r7AJnOPTclT1kvMhE/X7mzE9L7czWebeJEencG7jdXHLlie+xwdua5TFpno2ispx2o1d8wLOvUOsRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947658; c=relaxed/simple; bh=kUoFvFEil2iu64BSiHLlDTpL8qYLtqcLQvQtmS8GmB0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jRb8M5XOXKtC0ZyrDZNK66+G5tH0Xqbf8xdK/bU4hSmyhZCJRGZ6Rl43UIpv4lvdK8+M+WX+gWpaJP2Knt7rG9iiiEKlBZLs1bHbZtbUq4LhddSKybnTg6a+J24EzLpOUcbYluMVILvuSMGfuxHd4sSQ0wda83pgL6BxoYsuQqM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=2xOjhaAf; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2xOjhaAf" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-60a08eb0956so14901877b3.3 for ; Fri, 08 Mar 2024 17:27:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947656; x=1710552456; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Md5c39yBFZ+eeG7bGl1mI/mZMLWg13mNVu69FRgG8aI=; b=2xOjhaAfc1z1Jw/3GgwXy6hXA9G8W6WqKHzO546huC1nyOJFICpAIeDDE8qDFJ3hjm etArolfgYj7yHgFMlq4zmV83FaR2bZz62S+xNCb1UfRM9m1p3P0OyE3cf+FFYLsxcQQP lvZFhblrh5gq5ACOTjdjb9KswJTUqbGhCr8XIqti7c/fVRcCdvPK1k7TGEpnEo+m4/O9 kTWlWsUzFSK6UNgq4+huVg07RD5kc2fVQeBGbUCKR1Bk0cG9LTAeW4ha1ccvHwy7YwSo rD1kOis9e3N+t5WqR6UTKXjSRKSZhDo53GirVfH4W43K1HLfC5FlE+cmTCHntea3hlKo uuhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947656; x=1710552456; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Md5c39yBFZ+eeG7bGl1mI/mZMLWg13mNVu69FRgG8aI=; b=VAYRWbpJTffAfM0XTYVbWiJsVZdwjuaJxDLiNnBbdmV4Cl01jGmAhfdtY6ZXzKaQ/A KGdwvLZyMwV1MgCv2i1qB3btCimDHHwYsXd0yOUSBDGawRYiNACXYZ78fLhlnhV6R5Sx MB4yXmGK0D7ALFTai+O6jB2a2YjoIRWyqB2mu6Ke0+x8yNLelETl+5gRo9CktOGe329H m3moOHzJCM+kJAp/slGot6aoNuwmDuVI+PsJrv5zLItWL0HPTbZPFgIJCLVlswhA/MF9 aPf4UrsQrZTcuHZF6HOw4+CIXkCxT+yGnPvpXmCLKWGd5tAfPKpwozSvl2iG8yXs9RpL mxNQ== X-Gm-Message-State: AOJu0YwU1ZucXMEz4uc3+l51rz7+xyDqaNsQD6aqA9WJbgGOC+Nx6aZ9 neNokTLAHYl23XH9AWaJugLBhPiTzTxRCoaRJbKH4/bTAvOmSo85T6mBPLA1yPWAJG38EpbrSP5 TqA== X-Google-Smtp-Source: AGHT+IEQBKUUFqxmrGlfhL/slMpOGh3HPM1r8v1t/uJ9PxnoA13LOPBkXNE4KXZ/Gn2H3jPcL40LDG7/Sv8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:d84f:0:b0:609:e1f:5a42 with SMTP id a76-20020a0dd84f000000b006090e1f5a42mr178693ywe.2.1709947656474; Fri, 08 Mar 2024 17:27:36 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:19 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-4-seanjc@google.com> Subject: [PATCH v6 3/9] KVM: x86: Stuff vCPU's PAT with default value at RESET, not creation From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the stuffing of the vCPU's PAT to the architectural "default" value from kvm_arch_vcpu_create() to kvm_vcpu_reset(), guarded by !init_event, to better capture that the default value is the value "Following Power-up or Reset". E.g. setting PAT only during creation would break if KVM were to expose a RESET ioctl() to userspace (which is unlikely, but that's not a good reason to have unintuitive code). No functional change. Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Reviewed-by: Xiaoyao Li --- arch/x86/kvm/x86.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 66c4381460dc..eac97b1b8379 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12134,8 +12134,6 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.maxphyaddr =3D cpuid_query_maxphyaddr(vcpu); vcpu->arch.reserved_gpa_bits =3D kvm_vcpu_reserved_gpa_bits_raw(vcpu); =20 - vcpu->arch.pat =3D MSR_IA32_CR_PAT_DEFAULT; - kvm_async_pf_hash_reset(vcpu); =20 vcpu->arch.perf_capabilities =3D kvm_caps.supported_perf_cap; @@ -12302,6 +12300,8 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool ini= t_event) if (!init_event) { vcpu->arch.smbase =3D 0x30000; =20 + vcpu->arch.pat =3D MSR_IA32_CR_PAT_DEFAULT; + vcpu->arch.msr_misc_features_enables =3D 0; vcpu->arch.ia32_misc_enable_msr =3D MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | MSR_IA32_MISC_ENABLE_BTS_UNAVAIL; --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4AF1B2E631 for ; Sat, 9 Mar 2024 01:27:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947660; cv=none; b=LhkjXnqIRCrf3JJ0aTJ1XNJwVKbSZliW7vN4CKAHYji130e78GmowGfbePllrTdyaF0Xw09LB16aGM55F9gKGhnOyKw9AK/pYFQsMOdqBqw+BPcsHuxhj5wVGUdhEafIy2GRiWDgZdz/vDypBCwqZFdulESgM+Qw62tifKF4rGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947660; c=relaxed/simple; bh=cSo/9H4LV+ugQDK8WH64MT/A8kylvoerz+EThSRDTZ0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tZspA6pzvPLX6sdTybKaXgGRiiK4f+JLO7wLTx9oV5aOqz7/jonitz+NqUvlFxzsfzqMwBH9ojxeSiNdhXpShhFuxzzrEmiCAmOzgy1t3ca+bUhNhFOeScwo64JUUHW20WzCeHgUBPLo5VlzHByLzjc4wqSlsPOIPVr1C1KS7Co= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=s85dU/Wv; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="s85dU/Wv" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-609ff3206c3so32385927b3.2 for ; Fri, 08 Mar 2024 17:27:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947658; x=1710552458; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=J2mk3yjINFLzpjFivjrb5N/1XLkVh6F5pa6/JTyW+Xg=; b=s85dU/WvNT6ambJeTz55S2TJcT8bgzf2wWEEFHu0dKcS3Qv6uzfwJAbFAgJSxuuLYo EchOugvVQNajRruIO3ECwj6usk2UZ5X+H1T0BffxnsCm4Irs0Pxi+c72NdTC4y6ViRwq 2xZ7ZoWgyBp/u0zLjKDXSG1rUBU3jhk5d67edHlWz20p95ID/dXyB7RJsn4vj7mDbCBs v94oKwCfjaYeAa1mRxKfMA40cGFFwFHnmbq5eqJwcMMnbyaeaSndEXWPCjIgTIUbUFXd i05LAOZpzKVt4Dzv+fJvAGoxILN1zvTBkEa+aPDQAQ3PU9f9q22RmFbnOevnoERbpwiL uC7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947658; x=1710552458; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=J2mk3yjINFLzpjFivjrb5N/1XLkVh6F5pa6/JTyW+Xg=; b=tfrv1qcJHHBlZNWkxV9wVM+1UIseFTEx4NGaHnYoJUUbR8LJVXYJi07/DAuEoYCqpz t257vf68akpRwg4WKklnmwPWvnMO8da4W1NieNRdo9sK9BBG5DDW993SKpmdGKVyAj/M eXreLXj6trUOddlqVCqve5yho7rEsNwyadbEWCr/owMfBCLJpCz0dnuZdenv93sWAM6j qjPVXiN95/jBI0eie5Fy+Rtbwzz9pzAdwVfwUMnfG8+RXfz5/hKjZz5JWvSNaoOmHBZP dJIwphlKE/4xMLlMocNoIN9RX7tnO0WP/qJoCUSj3o5vWvKUUsX9TPdBZ0OkydWgMGSv OYXw== X-Gm-Message-State: AOJu0YzKg79P+Eu26yVJigXYBlH0CesayDBP+WfuPOUTx7Op8XCPq0tI g5r8jPQ7INYhDSyVm/ZdaF1Fzl1TXUIN+w4Kvi3vFE+3z08wGWxD/Nb42wJImqA3LRape/roTfx yew== X-Google-Smtp-Source: AGHT+IHrI5FDrUBoUl0r6JtHEuthwtAy7BhRUu74r7xvlQts2botI/ZyPUrf7LiPIj+ypGRGHDyvqTmpyOU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:ef46:0:b0:dcd:88e9:e508 with SMTP id w6-20020a25ef46000000b00dcd88e9e508mr185467ybm.5.1709947658336; Fri, 08 Mar 2024 17:27:38 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:20 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-5-seanjc@google.com> Subject: [PATCH v6 4/9] KVM: VMX: Move MSR_IA32_VMX_BASIC bit defines to asm/vmx.h From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Xin Li Move the bit defines for MSR_IA32_VMX_BASIC from msr-index.h to vmx.h so that they are colocated with other VMX MSR bit defines, and with the helpers that extract specific information from an MSR_IA32_VMX_BASIC value. Opportunistically use BIT_ULL() instead of open coding hex values. Opportunistically rename VMX_BASIC_64 to VMX_BASIC_32BIT_PHYS_ADDR_ONLY, as "VMX_BASIC_64" is widly misleading. The flag enumerates that addresses are limited to 32 bits, not that 64-bit addresses are allowed. Cc: Shan Kang Cc: Kai Huang Signed-off-by: Xin Li [sean: split to separate patch, write changelog] Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Reviewed-by: Zhao Liu --- arch/x86/include/asm/msr-index.h | 8 -------- arch/x86/include/asm/vmx.h | 7 +++++++ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-in= dex.h index af71f8bb76ae..5ca81ad509b5 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -1122,14 +1122,6 @@ #define MSR_IA32_VMX_VMFUNC 0x00000491 #define MSR_IA32_VMX_PROCBASED_CTLS3 0x00000492 =20 -/* VMX_BASIC bits and bitmasks */ -#define VMX_BASIC_VMCS_SIZE_SHIFT 32 -#define VMX_BASIC_TRUE_CTLS (1ULL << 55) -#define VMX_BASIC_64 0x0001000000000000LLU -#define VMX_BASIC_MEM_TYPE_SHIFT 50 -#define VMX_BASIC_MEM_TYPE_MASK 0x003c000000000000LLU -#define VMX_BASIC_INOUT 0x0040000000000000LLU - /* Resctrl MSRs: */ /* - Intel: */ #define MSR_IA32_L3_QOS_CFG 0xc81 diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index 4fdc76263066..c3a97dca4a33 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -133,6 +133,13 @@ #define VMX_VMFUNC_EPTP_SWITCHING VMFUNC_CONTROL_BIT(EPTP_SW= ITCHING) #define VMFUNC_EPTP_ENTRIES 512 =20 +#define VMX_BASIC_VMCS_SIZE_SHIFT 32 +#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY BIT_ULL(48) +#define VMX_BASIC_DUAL_MONITOR_TREATMENT BIT_ULL(49) +#define VMX_BASIC_MEM_TYPE_SHIFT 50 +#define VMX_BASIC_INOUT BIT_ULL(54) +#define VMX_BASIC_TRUE_CTLS BIT_ULL(55) + static inline u32 vmx_basic_vmcs_revision_id(u64 vmx_basic) { return vmx_basic & GENMASK_ULL(30, 0); --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3F0A364A4 for ; Sat, 9 Mar 2024 01:27:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947662; cv=none; b=pQpEaKpbJ7ejIJYuh/NFau2/nXi1rL6bi52o1rzmdTBlbTKp26VAdqbDVXn/xw1fs6/13aLi6mAFEVvsUI37145pAi/8IEmUZNkmy860cs16YaGrF4ad4aXiCPO0bLefuEophSGisbtMI12VqPndSudygS/8q9kZu7GkVZUopyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947662; c=relaxed/simple; bh=asA2Q64cYEdVnPGwxvGxEdtiJ+4njYAq1P8HIi+00oU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=U9ag6WHWKYIfPdHqY9F43cDmjxsmlSVl21ZebV/CxjQZbxKBwF8M3R8ddRqEnSN5V8pn1LMfMTjkQYq8sHn2S1o/kaUbf8Q12FWXBGD6IOQc5OM21x2y+GQloSfZWyuIP1959dAvPJivnqU5lGxQd2L3l6u2YRQasVWLHfKslx0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cFfeOdfH; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cFfeOdfH" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcc58cddb50so4401772276.0 for ; Fri, 08 Mar 2024 17:27:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947660; x=1710552460; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=7zfSsZ0P3OmkAxzO4p6XnXAYwdNDLv4d4j/tO3LJoqY=; b=cFfeOdfHuHVcK2WWbInVR/NCHRniyKHTKGOEmI+/qlMx5l7PySK6cczYdfNxUMLEm5 4L4Nl2sb+PI9TGt9muRci4ZIldxj2kN6ao/9kQjXryX63bd3unbhobzQai35FIz/KFlO 3ReUMEGy8j9RF8tY5IBJe84g/ETCAgE+Ss2Byue9rrI41A5HTucoZSoeBQuIJsjKK2In LhBbzx3H3loWYhp0cnobrkdxqKJjnChXH2Taa799sBs/YSLIhjKmrvQ4tpqyebH0GTXX Ccpbh07pfqFEPvWGi7qYgj3dH+OczKI2H/q3MVsYuEu/67RVYePhAcYf+x7p3jCUtY/L 8IPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947660; x=1710552460; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=7zfSsZ0P3OmkAxzO4p6XnXAYwdNDLv4d4j/tO3LJoqY=; b=sJ8DZ8hdZ//xE5dR7NL8uX+5vRghny4DYLjSA/KFPDu1x5Ajwrowj7Y6nQB0CgRQ7E WpZThQNyJ9HpfnPxApj+zyHB3G7ppbn0LtNkLz6skbTYT5MtaZDNaEpiv+X30RtzHGku /cmvHqkRyjV+E52AQyS8OkVr1qklPWIM6AoeJWbVOzpOR/gnYdI2vlH15I8UpCofDGGf Wzxej9D9dla5tKwlUYheKbuWR2JuFoVV7qQPvhAYJCRz7ZJkFUtTW/LwT1VpGjNV0jMt umIdEWvPeK2fS6sFdosO05sXtAMmoHITVjhBCmSBrxodnhpaXjlYBzIennJxJr60Bsrr k8bg== X-Gm-Message-State: AOJu0Yx3kWV5yJBXAyBG4WkgmiKoE3ENVCA1tFtKM1+Ll+AEp42tDVEX cVcfcdE3aJFk3KGbMlmQXEy+mgN0uHnefm/uE7RD8XtrUX0UGI0uG+MO5R5NoocRKGQqCeUTQ8M 4YQ== X-Google-Smtp-Source: AGHT+IFcW7KzgO/BpgyMqKPfaZjTeUEzLTWFoMFPJK2iqgZo0BYHsKQoyclB2VWqfLuEY3nnCdYV1aYmGj8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:aa83:0:b0:dc2:3441:897f with SMTP id t3-20020a25aa83000000b00dc23441897fmr185240ybi.6.1709947659928; Fri, 08 Mar 2024 17:27:39 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:21 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-6-seanjc@google.com> Subject: [PATCH v6 5/9] KVM: VMX: Track CPU's MSR_IA32_VMX_BASIC as a single 64-bit value From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Track the "basic" capabilities VMX MSR as a single u64 in vmcs_config instead of splitting it across three fields, that obviously don't combine into a single 64-bit value, so that KVM can use the macros that define MSR bits using their absolute position. Replace all open coded shifts and masks, many of which are relative to the "high" half, with the appropriate macro. Opportunistically use VMX_BASIC_32BIT_PHYS_ADDR_ONLY instead of an open coded equivalent, and clean up the related comment to not reference a specific SDM section (to the surprise of no one, the comment is stale). No functional change intended (though obviously the code generation will be quite different). Cc: Shan Kang Cc: Kai Huang Signed-off-by: Xin Li [sean: split to separate patch, write changelog] Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Reviewed-by: Xiaoyao Li --- arch/x86/include/asm/vmx.h | 5 +++++ arch/x86/kvm/vmx/capabilities.h | 6 ++---- arch/x86/kvm/vmx/vmx.c | 28 ++++++++++++++-------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index c3a97dca4a33..ce6d166fc3c5 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -150,6 +150,11 @@ static inline u32 vmx_basic_vmcs_size(u64 vmx_basic) return (vmx_basic & GENMASK_ULL(44, 32)) >> 32; } =20 +static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basic) +{ + return (vmx_basic & GENMASK_ULL(53, 50)) >> 50; +} + static inline int vmx_misc_preemption_timer_rate(u64 vmx_misc) { return vmx_misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilitie= s.h index 41a4533f9989..86ce8bb96bed 100644 --- a/arch/x86/kvm/vmx/capabilities.h +++ b/arch/x86/kvm/vmx/capabilities.h @@ -54,9 +54,7 @@ struct nested_vmx_msrs { }; =20 struct vmcs_config { - int size; - u32 basic_cap; - u32 revision_id; + u64 basic; u32 pin_based_exec_ctrl; u32 cpu_based_exec_ctrl; u32 cpu_based_2nd_exec_ctrl; @@ -76,7 +74,7 @@ extern struct vmx_capability vmx_capability __ro_after_in= it; =20 static inline bool cpu_has_vmx_basic_inout(void) { - return (((u64)vmcs_config.basic_cap << 32) & VMX_BASIC_INOUT); + return vmcs_config.basic & VMX_BASIC_INOUT; } =20 static inline bool cpu_has_virtual_nmis(void) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 71cc6e3b3221..e312c48f542f 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2554,13 +2554,13 @@ static u64 adjust_vmx_controls64(u64 ctl_opt, u32 m= sr) static int setup_vmcs_config(struct vmcs_config *vmcs_conf, struct vmx_capability *vmx_cap) { - u32 vmx_msr_low, vmx_msr_high; u32 _pin_based_exec_control =3D 0; u32 _cpu_based_exec_control =3D 0; u32 _cpu_based_2nd_exec_control =3D 0; u64 _cpu_based_3rd_exec_control =3D 0; u32 _vmexit_control =3D 0; u32 _vmentry_control =3D 0; + u64 basic_msr; u64 misc_msr; int i; =20 @@ -2679,29 +2679,29 @@ static int setup_vmcs_config(struct vmcs_config *vm= cs_conf, _vmexit_control &=3D ~x_ctrl; } =20 - rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); + rdmsrl(MSR_IA32_VMX_BASIC, basic_msr); =20 /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ - if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) + if (vmx_basic_vmcs_size(basic_msr) > PAGE_SIZE) return -EIO; =20 #ifdef CONFIG_X86_64 - /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]=3D=3D0. */ - if (vmx_msr_high & (1u<<16)) + /* + * KVM expects to be able to shove all legal physical addresses into + * VMCS fields for 64-bit kernels, and per the SDM, "This bit is always + * 0 for processors that support Intel 64 architecture". + */ + if (basic_msr & VMX_BASIC_32BIT_PHYS_ADDR_ONLY) return -EIO; #endif =20 /* Require Write-Back (WB) memory type for VMCS accesses. */ - if (((vmx_msr_high >> 18) & 15) !=3D X86_MEMTYPE_WB) + if (vmx_basic_vmcs_mem_type(basic_msr) !=3D X86_MEMTYPE_WB) return -EIO; =20 rdmsrl(MSR_IA32_VMX_MISC, misc_msr); =20 - vmcs_conf->size =3D vmx_msr_high & 0x1fff; - vmcs_conf->basic_cap =3D vmx_msr_high & ~0x1fff; - - vmcs_conf->revision_id =3D vmx_msr_low; - + vmcs_conf->basic =3D basic_msr; vmcs_conf->pin_based_exec_ctrl =3D _pin_based_exec_control; vmcs_conf->cpu_based_exec_ctrl =3D _cpu_based_exec_control; vmcs_conf->cpu_based_2nd_exec_ctrl =3D _cpu_based_2nd_exec_control; @@ -2851,13 +2851,13 @@ struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, g= fp_t flags) if (!pages) return NULL; vmcs =3D page_address(pages); - memset(vmcs, 0, vmcs_config.size); + memset(vmcs, 0, vmx_basic_vmcs_size(vmcs_config.basic)); =20 /* KVM supports Enlightened VMCS v1 only */ if (kvm_is_using_evmcs()) vmcs->hdr.revision_id =3D KVM_EVMCS_VERSION; else - vmcs->hdr.revision_id =3D vmcs_config.revision_id; + vmcs->hdr.revision_id =3D vmx_basic_vmcs_revision_id(vmcs_config.basic); =20 if (shadow) vmcs->hdr.shadow_vmcs =3D 1; @@ -2950,7 +2950,7 @@ static __init int alloc_kvm_area(void) * physical CPU. */ if (kvm_is_using_evmcs()) - vmcs->hdr.revision_id =3D vmcs_config.revision_id; + vmcs->hdr.revision_id =3D vmx_basic_vmcs_revision_id(vmcs_config.basic); =20 per_cpu(vmxarea, cpu) =3D vmcs; } --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24379374EF for ; Sat, 9 Mar 2024 01:27:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947664; cv=none; b=Fkd4YHT7htE3oW0hf0Jpj2R4dGtU3IiW5T8JbTKJXE4Nkk9YogsBIdjTXvYAMWFksNVAW48cNfj/1zeVivHdzUvhvaqw34MZWkz6VPM7riAq3IJIJ+IEgzn2IypN+RQacUUmQy6gH01923j/VX4RQfyJMAfhvbN/TqnQLejVcpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947664; c=relaxed/simple; bh=0Bygn6FGfK7hczQ7MYvkTd3Y6tG5a7KkroAmIU0tSQ4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=quQH1zbEuQMnqFfClmO7Xhev3304hM7peooSuL3PyvXEpL105SLMSj6qkEQ76hOoSdzdQn14i9QMlVlr9ZQ0thXfspBYyqwrrw1wxS8rtTh55uxB/AWp8FDsg2WqeZ9nQ2bmjszN2Wqz73IPSshXvL3pxZLZ9GgWzhJWN/VLRoY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=plY1J/gm; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="plY1J/gm" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-29baacab848so1381304a91.1 for ; Fri, 08 Mar 2024 17:27:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947662; x=1710552462; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=mjAr6s0HGaO3+5h8Ie2u9Yey208VqmWXwMYkWSzg22A=; b=plY1J/gmNz6wZzKK2ZA1+jU0sa+jwCVQHQDpuenOf/sgRWPlCVno6kGzlo/iK3J1we N6jfA6o+GxzXbAl5Y6bjj7moO8N58i3jE7qj6azR0loNGPTq1Yh7Dy4nSHZ6vG47okLD vVc0lxmSMoQxELo+weYjvDAYIg0WnoUHsGlNpf2EMkGN0s8qm9L17PRYbpLZowxnukp4 teHYPb/YAiXO0EwlFdtEcWPrp/3PguZrMW/qKRaSMeIZADGaDFwqecRL9EK/s6Az/28a qP66Dj7TcqDiwTjGlf5PL2TgPVMcbPoQKZoW/xrHv9JNQmLR3Ji7YlwpUkFsdjfMitet PC3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947662; x=1710552462; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=mjAr6s0HGaO3+5h8Ie2u9Yey208VqmWXwMYkWSzg22A=; b=nOF/An3LeF+01Jjt7oyoB4HMnwVBzQ9x4k0mgvetMF+y4ZbvHZovRxt6dQ4H2oz9z1 npxaL9vSELk2ToHDDupOziOfRj1rzUSMTdGLCK1YO/mDPQWCafMs78iBtsK05p7WYpDp ZDFx/2Fesb7iyysYq+nmvQOvrPG/E5aPSZalGLd4WeL5EKDa4lkuIzz8zEfIbW6eooi+ poz36kZBfJ0DvtKru9pWhZZalPcU7lH2xbtp+wuETO8a6ktwjFAYkEtGiJa9iQQib1zi oj6wiEFxwD87mxR9C/+57ScP7vKkjF579b/7sJD3yn2KapxgGp3VIh9txO2nYDABIMzY JGug== X-Gm-Message-State: AOJu0Yx75dbtyUlIDDUkddvgXq8CA3OsPzfYYd2t94A1dedHHhbJvbg7 LYAq/tLbcVvkDDnS34sptRK3bUcZfctlrzefKa4nFrvE4iaiMjBt7H2jR3bMK+t11MolagmfUzX oyA== X-Google-Smtp-Source: AGHT+IGsNNKjWe5wisyM7/b0FQKlpNvDtTNfaGIHuq9cW8kK/Cxbk+EhF7GpV5gmkbYL414F+r8ZBDlG/hY= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:38cc:b0:29b:c58e:8a53 with SMTP id nn12-20020a17090b38cc00b0029bc58e8a53mr15336pjb.0.1709947661557; Fri, 08 Mar 2024 17:27:41 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:22 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-7-seanjc@google.com> Subject: [PATCH v6 6/9] KVM: nVMX: Use macros and #defines in vmx_restore_vmx_basic() From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Xin Li 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 Cc: Kai Huang Signed-off-by: Xin Li [sean: split to separate patch, write changelog, drop #defines] Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Reviewed-by: Xiaoyao Li Reviewed-by: Zhao Liu --- arch/x86/kvm/vmx/nested.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 82a35aba7d2b..4ad8696c25af 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -1228,21 +1228,32 @@ static bool is_bitwise_subset(u64 superset, u64 sub= set, u64 mask) =20 static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) { - const u64 feature_and_reserved =3D - /* feature (except bit 48; see below) */ - BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | - /* reserved */ - BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); + const u64 feature_bits =3D VMX_BASIC_DUAL_MONITOR_TREATMENT | + VMX_BASIC_INOUT | + VMX_BASIC_TRUE_CTLS; + + const u64 reserved_bits =3D GENMASK_ULL(63, 56) | + GENMASK_ULL(47, 45) | + BIT_ULL(31); + u64 vmx_basic =3D vmcs_config.nested.basic; =20 - if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) + BUILD_BUG_ON(feature_bits & reserved_bits); + + /* + * Except for 32BIT_PHYS_ADDR_ONLY, which is an anti-feature bit (has + * inverted polarity), the incoming value must not set feature bits or + * reserved bits that aren't allowed/supported by KVM. Fields, i.e. + * multi-bit values, are explicitly checked below. + */ + if (!is_bitwise_subset(vmx_basic, data, feature_bits | reserved_bits)) return -EINVAL; =20 /* * KVM does not emulate a version of VMX that constrains physical * addresses of VMX structures (e.g. VMCS) to 32-bits. */ - if (data & BIT_ULL(48)) + if (data & VMX_BASIC_32BIT_PHYS_ADDR_ONLY) return -EINVAL; =20 if (vmx_basic_vmcs_revision_id(vmx_basic) !=3D --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E920381DD for ; Sat, 9 Mar 2024 01:27:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947666; cv=none; b=A5okO52YWu/ER0y0keWyx+KmL49nG9fk2z/x4vWxnb90/LUAgz33z1npAZVfi/XefUV2g0IZug+Fhv1M/aEZWVAj9A/xrw8Wdgbua38gBsKLargePOO4nYaIh5eGzPQqwM/5n7rQd0aU90dAO2EWjDZfhOZy29nxh3dm6qJk0FY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947666; c=relaxed/simple; bh=ZEiLrY1KoQ4Fx0NTSZKVxy7T5TZLTSRB54wG8b69QkY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=M/OaxFoXQ2kpJGpClCfxYmFMyYK2At8jSVonMNXEEp7zez2Ss6nbR63G1BPewhjf4kh96IY657VFj6whPpqUjtJhE6RVJYUJKCeMF/9CFDj3LHRiT+MabJAaqNcYbcQ2UoAQQQdqUEYOgaxt6g5fKXqG9xMz24bOkZJKqT0KN4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uDqmOj71; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uDqmOj71" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dcd1779adbeso5022374276.3 for ; Fri, 08 Mar 2024 17:27:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947664; x=1710552464; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Z0hXUDRVrnWiVd/XkZxxPle06FSyDaHVj3M6N2xEb8c=; b=uDqmOj716Fl0bSLgUENe4S9NFy3CDMg3ukvdx57UFjGrLDQmDoBY4RQj4WgJe8HNJz teWxMDuybiGcWvccuHGgl4Ix+DyW0hy3I5RMVrwid7HM1p8fldmz03Rd3tIWZ7NDGCUy igrncfJcgcTizvqXPHD3PzRfs6+ICFAqfNxeXVXuwcv4DO2Gh9oXF64zlegxbWVk2VYc 7VFNe+j0xQQBzvUJzCWRPzO3bPlHJ9vT8cC0I+1YaMINLMwGzkmFzO3THgT31SKuTLWM Y0QUyezCVxaia0i0VmHAYsup2o14RA1rSGv3XAmUmCMwQ5/6L7aT4LwJx0/MhJ4bz5/k 5NAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947664; x=1710552464; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Z0hXUDRVrnWiVd/XkZxxPle06FSyDaHVj3M6N2xEb8c=; b=IWk3dwaTyMtQx6LwxJ6gufJUYmkHcgEU77ueLFlgAjmAz+hat38JO0qXC5ko7AcloJ esmEQZN00bea+LKPjyLY6I+mSmEHbDCfSKM68QL4YXt1qr609riBwCBujiEYWxovrSUi BPIYEq50MSIGqARRerIYbV9jH24B/IAczebPEIhD1dzr6MfsxY5r/M6+Elyro2+mzjlO 2cbzMBbZuTsUd9cgnmgGp1Rjuy/AeSRGCx1pIoYcbMR81js0TkqWcYIzOvv/o8jHzOcN cTKta9L9/ps4nhqa+U3oOMSzsQ4FZZDzlr+iffgcy2EVIbOguWHITEOgH7ou+CVC9MJ1 KFig== X-Gm-Message-State: AOJu0YxsJ36Hd/Ax5v/PAf8xjjxsXk6XkDH8jv298oQ/NYl+l4f/IZ+y h0fPxCLUD3XnfFysSG0BD08t+1jzLkXYN4Sty7hdGKBpp9GxEXX2f02zt5qWkJY/okubM2aGz1J GIg== X-Google-Smtp-Source: AGHT+IHmflw34lq1WAlNZ9yNOJPtmcj0Q5muJ8m3UkhtRdxvaNlP5bAOSFjzOKKe2zkVBXxtgGtliF3ZaUU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:110a:b0:dcc:50ca:e153 with SMTP id o10-20020a056902110a00b00dcc50cae153mr132642ybu.7.1709947664231; Fri, 08 Mar 2024 17:27:44 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:23 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-8-seanjc@google.com> Subject: [PATCH v6 7/9] KVM VMX: Move MSR_IA32_VMX_MISC bit defines to asm/vmx.h From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the handful of MSR_IA32_VMX_MISC bit defines that are currently in msr-indx.h to vmx.h so that all of the VMX_MISC defines and wrappers can be found in a single location. Opportunistically use BIT_ULL() instead of open coding hex values, add defines for feature bits that are architectural defined, and move the defines down in the file so that they are colocated with the helpers for getting fields from VMX_MISC. No functional change intended. Cc: Shan Kang Cc: Kai Huang Signed-off-by: Xin Li [sean: split to separate patch, write changelog] Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Reviewed-by: Zhao Liu --- arch/x86/include/asm/msr-index.h | 5 ----- arch/x86/include/asm/vmx.h | 19 ++++++++++++------- arch/x86/kvm/vmx/capabilities.h | 4 ++-- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/nested.h | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-in= dex.h index 5ca81ad509b5..3531856def3d 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -1138,11 +1138,6 @@ #define MSR_IA32_SMBA_BW_BASE 0xc0000280 #define MSR_IA32_EVT_CFG_BASE 0xc0000400 =20 -/* MSR_IA32_VMX_MISC bits */ -#define MSR_IA32_VMX_MISC_INTEL_PT (1ULL << 14) -#define MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS (1ULL << 29) -#define MSR_IA32_VMX_MISC_PREEMPTION_TIMER_SCALE 0x1F - /* AMD-V MSRs */ #define MSR_VM_CR 0xc0010114 #define MSR_VM_IGNNE 0xc0010115 diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index ce6d166fc3c5..6ff179b11235 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -120,13 +120,6 @@ =20 #define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff =20 -#define VMX_MISC_PREEMPTION_TIMER_RATE_MASK 0x0000001f -#define VMX_MISC_SAVE_EFER_LMA 0x00000020 -#define VMX_MISC_ACTIVITY_HLT 0x00000040 -#define VMX_MISC_ACTIVITY_WAIT_SIPI 0x00000100 -#define VMX_MISC_ZERO_LEN_INS 0x40000000 -#define VMX_MISC_MSR_LIST_MULTIPLIER 512 - /* VMFUNC functions */ #define VMFUNC_CONTROL_BIT(x) BIT((VMX_FEATURE_##x & 0x1f) - 28) =20 @@ -155,6 +148,18 @@ static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basi= c) return (vmx_basic & GENMASK_ULL(53, 50)) >> 50; } =20 +#define VMX_MISC_PREEMPTION_TIMER_RATE_MASK GENMASK_ULL(4, 0) +#define VMX_MISC_SAVE_EFER_LMA BIT_ULL(5) +#define VMX_MISC_ACTIVITY_HLT BIT_ULL(6) +#define VMX_MISC_ACTIVITY_SHUTDOWN BIT_ULL(7) +#define VMX_MISC_ACTIVITY_WAIT_SIPI BIT_ULL(8) +#define VMX_MISC_INTEL_PT BIT_ULL(14) +#define VMX_MISC_RDMSR_IN_SMM BIT_ULL(15) +#define VMX_MISC_VMXOFF_BLOCK_SMI BIT_ULL(28) +#define VMX_MISC_VMWRITE_SHADOW_RO_FIELDS BIT_ULL(29) +#define VMX_MISC_ZERO_LEN_INS BIT_ULL(30) +#define VMX_MISC_MSR_LIST_MULTIPLIER 512 + static inline int vmx_misc_preemption_timer_rate(u64 vmx_misc) { return vmx_misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilitie= s.h index 86ce8bb96bed..cb6588238f46 100644 --- a/arch/x86/kvm/vmx/capabilities.h +++ b/arch/x86/kvm/vmx/capabilities.h @@ -223,7 +223,7 @@ static inline bool cpu_has_vmx_vmfunc(void) static inline bool cpu_has_vmx_shadow_vmcs(void) { /* check if the cpu supports writing r/o exit information fields */ - if (!(vmcs_config.misc & MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) + if (!(vmcs_config.misc & VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) return false; =20 return vmcs_config.cpu_based_2nd_exec_ctrl & @@ -365,7 +365,7 @@ static inline bool cpu_has_vmx_invvpid_global(void) =20 static inline bool cpu_has_vmx_intel_pt(void) { - return (vmcs_config.misc & MSR_IA32_VMX_MISC_INTEL_PT) && + return (vmcs_config.misc & VMX_MISC_INTEL_PT) && (vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_PT_USE_GPA) && (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_RTIT_CTL); } diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 4ad8696c25af..06512ee7a5c4 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -6998,7 +6998,7 @@ static void nested_vmx_setup_misc_data(struct vmcs_co= nfig *vmcs_conf, { msrs->misc_low =3D (u32)vmcs_conf->misc & VMX_MISC_SAVE_EFER_LMA; msrs->misc_low |=3D - MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | + VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | VMX_MISC_EMULATED_PREEMPTION_TIMER_RATE | VMX_MISC_ACTIVITY_HLT | VMX_MISC_ACTIVITY_WAIT_SIPI; diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h index cce4e2aa30fb..0782fe599757 100644 --- a/arch/x86/kvm/vmx/nested.h +++ b/arch/x86/kvm/vmx/nested.h @@ -109,7 +109,7 @@ static inline unsigned nested_cpu_vmx_misc_cr3_count(st= ruct kvm_vcpu *vcpu) static inline bool nested_cpu_has_vmwrite_any_field(struct kvm_vcpu *vcpu) { return to_vmx(vcpu)->nested.msrs.misc_low & - MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; + VMX_MISC_VMWRITE_SHADOW_RO_FIELDS; } =20 static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *v= cpu) --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E986E3984D for ; Sat, 9 Mar 2024 01:27:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947668; cv=none; b=csnzb8xScvqWfN1S3QNLSV4A8/9PMzCwnUbwXYXGC5KJXHavJboyvKNzzacI5W0lWRrPCYQ2jwft4mQIAoZt6JBD7wPs2qoVZvYRlIhE8RMitvJQlIUgcZoK5OLE62ZlTSVBwwnAp9cXNbiMMHTLPkBIY3n1tYGEYoJxow+vW0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947668; c=relaxed/simple; bh=vq2K+5LHK83RpKnCGkTT6ZhKAFoSxkbWDGsV0tgPWLw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BSGqMrSBfYfEACv6LIBygDhJjTbwi4WE6mhRLYHa8VEAEfTZ76OwMrynIw+d9I3+2ZnwS+jSg0aqLnYH6f4bCSWgG5b42QAadA08NuIRs40OiUryxu7thIlv7JSak20an+1vLctjEu6/EEFdvEiLSiEcvkiNXrF0evWg/G4YMvo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QcAq61iA; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QcAq61iA" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6e678cd2f25so299021b3a.3 for ; Fri, 08 Mar 2024 17:27:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947666; x=1710552466; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=yWRHC3fowN32chz4V0nbkaLENfnprXFQBmDkxVMLWD0=; b=QcAq61iAMFmUZPo2SP05UsgzMOsOSL+fOoY/MQPpU9GBNB/eEN0YmaA90GG4Xm44HK r75Ws8AiCyaMJqBxWFjh46tfR4cdAmSj66p3QMsl1WxI3eL1FtPjJFgqExlY/FrHxFFs 6XKUgLlQBKzsfiMTuAFiC7GLpxarcdpMBu7lo5rYnFQQ66jCNwfU8WJzfUPSqv3n+K+q JaGocGEFKa30mAQhaP1OF9q7MZ4mfVYWwYl37fDLkboQRFsiA45iaBbD5V2ptNaEZ/ZG zfvinBA7ZwyiGCos0fpLyczajx3m07cxmpMpQewrmhn8cm/22JebFRSfirJBgy40FOkb sx2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947666; x=1710552466; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yWRHC3fowN32chz4V0nbkaLENfnprXFQBmDkxVMLWD0=; b=RhyK7Da+9Y7t80uf3qFoIrrl8Qrq9pLz5WDVZxERQZxx2ZUEAc5yFSr+8+u9hxM7QM 3gXOZLbRwttB8cRunouIyWB1fLJIBLTW59wlHij7LMl4s7WNK01nFqjz2hLTIq5aSO9u hxt/XAwRWBZ+j5/eHmYLDkEwJKhYsdLydPPv1D6ICgTuABhU+UEGmzEJ1EZS6TlsM/w7 McLfNUrQei4KtxONEhPXKakOHPx6T7pHzZd+nUSLmzdBgsO1pQ9ZHSdyrf7s0inZH46/ bjRDjTmeQMXc0etwrGjAhJ8epOAap29W4RlaqhYXltdIOvXeUpOBMm5nyYsXAnCes9ie kqiw== X-Gm-Message-State: AOJu0YzE1Wvpb2SgcsCW6VpMqB1xyKnmRM+cGHRTw2WegCJuidsk2Tmw ntCp+FELl8TF4xTuZzxvbo/URJkmtj2d7+jbWS+rQqMumk7qnMCHE89G1nMIQ77pGsukvFvQXyq Dbg== X-Google-Smtp-Source: AGHT+IFQJluLhAiF5eQqXz2PPCgiEuZQssxJpXXlfeXxfqSOPWP/vG50rZro2Xy/23igZND4o1xXOR4e3G8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:9289:b0:6e5:8053:af92 with SMTP id jw9-20020a056a00928900b006e58053af92mr63785pfb.4.1709947666389; Fri, 08 Mar 2024 17:27:46 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:24 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-9-seanjc@google.com> Subject: [PATCH v6 8/9] KVM: VMX: Open code VMX preemption timer rate mask in its accessor From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Xin Li Use vmx_misc_preemption_timer_rate() to get the rate in hardware_setup(), and open code the rate's bitmask in vmx_misc_preemption_timer_rate() so that the function looks like all the helpers that grab values from VMX_BASIC and VMX_MISC MSR values. No functional change intended. Cc: Shan Kang Cc: Kai Huang Signed-off-by: Xin Li [sean: split to separate patch, write changelog] Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang --- arch/x86/include/asm/vmx.h | 3 +-- arch/x86/kvm/vmx/vmx.c | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index 6ff179b11235..90ed559076d7 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -148,7 +148,6 @@ static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basic) return (vmx_basic & GENMASK_ULL(53, 50)) >> 50; } =20 -#define VMX_MISC_PREEMPTION_TIMER_RATE_MASK GENMASK_ULL(4, 0) #define VMX_MISC_SAVE_EFER_LMA BIT_ULL(5) #define VMX_MISC_ACTIVITY_HLT BIT_ULL(6) #define VMX_MISC_ACTIVITY_SHUTDOWN BIT_ULL(7) @@ -162,7 +161,7 @@ static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basic) =20 static inline int vmx_misc_preemption_timer_rate(u64 vmx_misc) { - return vmx_misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; + return vmx_misc & GENMASK_ULL(4, 0); } =20 static inline int vmx_misc_cr3_count(u64 vmx_misc) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e312c48f542f..4fcfa1a213fa 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -8613,7 +8613,7 @@ static __init int hardware_setup(void) u64 use_timer_freq =3D 5000ULL * 1000 * 1000; =20 cpu_preemption_timer_multi =3D - vmcs_config.misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; + vmx_misc_preemption_timer_rate(vmcs_config.misc); =20 if (tsc_khz) use_timer_freq =3D (u64)tsc_khz * 1000; --=20 2.44.0.278.ge034bb2e1d-goog From nobody Sun Feb 8 13:09:17 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D1AC3A8CC for ; Sat, 9 Mar 2024 01:27:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947669; cv=none; b=Eri9WTnWaI3EThKLZVsjZZWlw8g9aqx8ekjOLlfcd159L5CHv1EGGxl/Q5JwnVS6c1JJIt3Mp37JCfIZtXcO0HM+8iBSsGVpcuNCLKxpW9CqOr8Ei+RZIZXjzzdGbVBI3/Y0eL9F2r8aLhW8VLqHa0EM34qtkNeC/6MVInAN8rw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947669; c=relaxed/simple; bh=3RB5ybb2eWpxvWqhePHFkoyZ4DVvi7cGA85H74hezaE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FFkPdX2pQexz8Es71dgaFtBeQQX7qXwxooFbncQnF94iNkUXG7BdWZJxZz+YQo+afVv9jCPcis+ywVf9S0jyq4O5yDtfM21Y7FwxI4lJ2LJKMfTeI0H2ywFEW1LI6O2HZWyLSgYoSaCGL6NS2dkghsj3xLfi7+3z4yz1KbkgFY0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YK90p5J5; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YK90p5J5" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-6e62136860eso1259939b3a.3 for ; Fri, 08 Mar 2024 17:27:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709947668; x=1710552468; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=PaASkSwAWfMjl4NBiq05b1lXThMlN3vSRBX1XjjeH98=; b=YK90p5J5vNtDrnM0FcqgUm4DVGGNSX2ISZ9v8TsPAGelJTpKv6H7wWyWT9haJFvRMN SJ20rVLUPaWXboYVMXl9nvKXCPWXiyI6yKWcS+Rdm/d9sm9RD8XGLraae+4tMRPfaoWv 1mkz0S8imx37mTaSjlo45m2JScfQ9DowpwF+J182eLATqzUa8aG/s8dLHrrgPRTdN8xy fKahl9V9jXo/x2rzh4qwSHDwQuLDWP7adiSLpo2rRiJZB+ccDxAZ2VPWO5ax9FuQXvF7 9UNa7PIVE0goTmyB0me0rEvNQBNeV2uAkIr7CgiD+U6kmdEGiZqUKmy6voqT3W7OC/Wt 3aqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709947668; x=1710552468; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PaASkSwAWfMjl4NBiq05b1lXThMlN3vSRBX1XjjeH98=; b=RcPPM/yscWJHjYqH7WGpSVvF6gN/GL3ex7Y1hpcO06wxPKYDQonOgnWNrNUUPe4GoL 2+u7yZY5gjvxrto57dc3w8fybGfuoXA3JNCzvqnUE4bAhMTuKQcG0CpZij6lQjLs3K09 /K4lkfOZnQhTFAQ936fnBz6ytykgXFWiWU/fDCx/HtbENuhwqqmtv2wS8xexKkd4Fjwt jZ9gBvFWXWETEvZlPyZd3JXDoYqJNlxthMZiQM7usiyZ5lY4+MiS+0UcLIweAt/Y71G3 6FUR9GX0n55lA/pvYFdJzesEovhJEx9jO2rXW3mKxHAkEndSv3QN7P74r2jqAidyy+lf vnNg== X-Gm-Message-State: AOJu0Ywj4iWp/TCIRtH2UpdhT2+pnw+jJ2S6hCeOAJKga3+pMtYDiOTv AIr/J5xAS2asCzq3Fjg6N329xSR6mt8kMTviBnn1AN9dAU8DNce8vxX9xu+r1YX6U5J/tXMRksU llA== X-Google-Smtp-Source: AGHT+IECrHtNs6BgHJfPn5W88AEGOjcbjO3YQOnM3DErPQrq0yT7XiAUw/mEaCIWPLmtk4712WYOVZyywRg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1389:b0:6e6:691f:42f5 with SMTP id t9-20020a056a00138900b006e6691f42f5mr47078pfg.0.1709947667900; Fri, 08 Mar 2024 17:27:47 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 8 Mar 2024 17:27:25 -0800 In-Reply-To: <20240309012725.1409949-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240309012725.1409949-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240309012725.1409949-10-seanjc@google.com> Subject: [PATCH v6 9/9] KVM: nVMX: Use macros and #defines in vmx_restore_vmx_misc() From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Andy Lutomirski , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Shan Kang , Kai Huang , Xin Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Xin Li Use macros in vmx_restore_vmx_misc() 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 Cc: Kai Huang Signed-off-by: Xin Li [sean: split to separate patch, write changelog, drop #defines] Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang Reviewed-by: Xiaoyao Li Reviewed-by: Zhao Liu --- arch/x86/kvm/vmx/nested.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 06512ee7a5c4..6610d258c680 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -1322,16 +1322,29 @@ vmx_restore_control_msr(struct vcpu_vmx *vmx, u32 m= sr_index, u64 data) =20 static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) { - const u64 feature_and_reserved_bits =3D - /* feature */ - BIT_ULL(5) | GENMASK_ULL(8, 6) | BIT_ULL(14) | BIT_ULL(15) | - BIT_ULL(28) | BIT_ULL(29) | BIT_ULL(30) | - /* reserved */ - GENMASK_ULL(13, 9) | BIT_ULL(31); + const u64 feature_bits =3D VMX_MISC_SAVE_EFER_LMA | + VMX_MISC_ACTIVITY_HLT | + VMX_MISC_ACTIVITY_SHUTDOWN | + VMX_MISC_ACTIVITY_WAIT_SIPI | + VMX_MISC_INTEL_PT | + VMX_MISC_RDMSR_IN_SMM | + VMX_MISC_VMWRITE_SHADOW_RO_FIELDS | + VMX_MISC_VMXOFF_BLOCK_SMI | + VMX_MISC_ZERO_LEN_INS; + + const u64 reserved_bits =3D BIT_ULL(31) | GENMASK_ULL(13, 9); + u64 vmx_misc =3D vmx_control_msr(vmcs_config.nested.misc_low, vmcs_config.nested.misc_high); =20 - if (!is_bitwise_subset(vmx_misc, data, feature_and_reserved_bits)) + BUILD_BUG_ON(feature_bits & reserved_bits); + + /* + * The incoming value must not set feature bits or reserved bits that + * aren't allowed/supported by KVM. Fields, i.e. multi-bit values, are + * explicitly checked below. + */ + if (!is_bitwise_subset(vmx_misc, data, feature_bits | reserved_bits)) return -EINVAL; =20 if ((vmx->nested.msrs.pinbased_ctls_high & --=20 2.44.0.278.ge034bb2e1d-goog