From patchwork Sat Mar 9 01:27:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587460 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 B34091096F 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-60a0890f2cdso16924717b3.1 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=dMvgHU4ASKU/eUaaxYzXokDGhBDOz/yFhI57ZE6MpWufI6uFYwsJZZPKNfu6lGvTXH PgYSvtc7akOWCz9dV34JSyiV5CNQbjcQfCrpCG0N7gt3W78B5d52LJJdvzu1RGobSpbQ VhlBnDNJ5BmjOf3BmxiSsJnKN3InBxvegwpM0FpjmJ8jC5Ae1k8DQXZgTQNfcjZegIHB w6doW/+Lqa9lf85ZiApfbC/uyAfUZ3ZcsewTX9GE8EG+eigXNtKrUUNzhWm231PpWYcH x51pO93BB21VlfNfuQoLCuBuf/UYA/0aX6bIGHwegVO26MCpuqH/YswR65CNcnfNRj2u MIGA== X-Forwarded-Encrypted: i=1; AJvYcCUTQot0a5uPlG5/BrifjNSDzUeFDbLDe3fAmYHt8Jj0pQ6rVEbKYaUEG7PmfLxPsbcIJm2unfDwWeyfPzcK8kIdVsQx X-Gm-Message-State: AOJu0Yy31yP/dHOJa3gvr2R4lH36lsJAANHNOmLjyP9jgMxbp3frzSET 3Wy+4n9brvT7dSb3J4WOJURHt+P9UiMAMAKXq4BMadDgqzSoJBYemVrFoutc5s/YYTjfv/OgT2J CfQ== 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: kvm@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 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-index.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) +/* + * 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 */ #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 /* 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 shifted. */ #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/mtrr.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 @@ #include "mtrr.h" +static_assert(X86_MEMTYPE_UC == MTRR_TYPE_UNCACHABLE); +static_assert(X86_MEMTYPE_WC == MTRR_TYPE_WRCOMB); +static_assert(X86_MEMTYPE_WT == MTRR_TYPE_WRTHROUGH); +static_assert(X86_MEMTYPE_WP == MTRR_TYPE_WRPROT); +static_assert(X86_MEMTYPE_WB == MTRR_TYPE_WRBACK); + /* arch_phys_wc_add returns an MTRR register index plus this offset. */ #define MTRR_TO_PHYS_WC_OFFSET 1000 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); if (cpu_has_vmx_basic_inout()) msrs->basic |= 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 /* Require Write-Back (WB) memory type for VMCS accesses. */ - if (((vmx_msr_high >> 18) & 15) != 6) + if (((vmx_msr_high >> 18) & 15) != X86_MEMTYPE_WB) return -EIO; 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 -enum { - PAT_UC = 0, /* uncached */ - PAT_WC = 1, /* Write combining */ - PAT_WT = 4, /* Write Through */ - PAT_WP = 5, /* Write Protected */ - PAT_WB = 6, /* Write Back (default) */ - PAT_UC_MINUS = 7, /* UC, but can be overridden by MTRR */ -}; - #define CM(c) (_PAGE_CACHE_MODE_ ## c) 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; switch (pat_val) { - case PAT_UC: cache = CM(UC); cache_mode = "UC "; break; - case PAT_WC: cache = CM(WC); cache_mode = "WC "; break; - case PAT_WT: cache = CM(WT); cache_mode = "WT "; break; - case PAT_WP: cache = CM(WP); cache_mode = "WP "; break; - case PAT_WB: cache = CM(WB); cache_mode = "WB "; break; - case PAT_UC_MINUS: cache = CM(UC_MINUS); cache_mode = "UC- "; break; - default: cache = CM(WB); cache_mode = "WB "; break; + case X86_MEMTYPE_UC: cache = CM(UC); cache_mode = "UC "; break; + case X86_MEMTYPE_WC: cache = CM(WC); cache_mode = "WC "; break; + case X86_MEMTYPE_WT: cache = CM(WT); cache_mode = "WT "; break; + case X86_MEMTYPE_WP: cache = CM(WP); cache_mode = "WP "; break; + case X86_MEMTYPE_WB: cache = CM(WB); cache_mode = "WB "; break; + case X86_MEMTYPE_UC_MINUS: cache = CM(UC_MINUS); cache_mode = "UC- "; break; + default: cache = CM(WB); cache_mode = "WB "; break; } memcpy(msg, cache_mode, 4); @@ -254,11 +245,11 @@ void pat_cpu_init(void) void __init pat_bp_init(void) { struct cpuinfo_x86 *c = &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)) if (!IS_ENABLED(CONFIG_X86_PAT)) From patchwork Sat Mar 9 01:27:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587461 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 4677520B0F for ; Sat, 9 Mar 2024 01:27:35 +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=1709947656; cv=none; b=dfOPU+P6fm8gkS1zW1JGLzgYy6/8WYmnTrSjhbhqFrEQgrx+f6XJh43kYF9ym9P4VughC3055PL2IXrzeEgol9SkmLKyeDpuyJ2F/Bb9RC93Emo63uxbiTgLwB7fmPQ5ComcqBqhst6NPxBdZPpnx187gVZ9r3uawkI7MQWbb1I= 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.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="IFnnJqxw" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-6e672f1880fso915473b3a.1 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=SNfLsX/tZEy8/QQsMDzzXl0mVUIqKnJwypI1nZTxW27sdJ1/3aMmMtBxP+R4Np7xbt zQfEFrCUM0RY8uKiYtpz2AFlwG2U7FeV0ITYANhp+hWra03gsBuSfyn/jJt7uOyd/+sS 5gUpzbGtEx6VUZSzXK21f/ftF/jeVKObZKDbW/2Cri0ijUn98Jdtb7+dKBCScHIGQahP 0D0LcTjjPPmQmLIwsqvlLa8DlVOVitIBSxbo5LeweBwFN/G5pl15D3C6EfHzz8xE/ItA /wEvlfQZqA9LAgjWQerhu7wTexfmYtlMcLJ2hxtKtKL3L5HvPX70QhNLaaGOc70kL/qc M7qg== X-Forwarded-Encrypted: i=1; AJvYcCVrTf1VBjdBD8k2zhA2sx1vxjU+FCqgG5VcNQKagd2sgny4AfRU7QsnXlbfINVkUEDPII13ywCfBN+D1GExbemcX689 X-Gm-Message-State: AOJu0Ywa1qjlmXwb7c7yMZCiX+o7JDS7EzCeoVmP3SPm1thTikyvlUCp 0vxyaIPlnhjJ2hLCm7KNPASN3iNjUpAZy8LyIuwIFG9Yz6anHeFxT0UjkM4yKKVxX9JET/QQG9g 0rg== 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: kvm@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 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-index.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 @@ #define MSR_IA32_CR_PAT 0x00000277 +#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 int val, return max(val, min); } -#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) 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 = &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)) - if (!IS_ENABLED(CONFIG_X86_PAT)) pr_info_once("x86/PAT: PAT support disabled because CONFIG_X86_PAT is disabled 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 = PAT(WB, WT, UC_MINUS, UC, WB, WT, UC_MINUS, UC); + pat_msr_val = PAT_VALUE(WB, WT, UC_MINUS, UC, WB, WT, UC_MINUS, UC); } /* @@ -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 = PAT(WB, WC, UC_MINUS, UC, WB, WC, UC_MINUS, UC); + pat_msr_val = 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 = PAT(WB, WC, UC_MINUS, UC, WB, WP, UC_MINUS, WT); + pat_msr_val = PAT_VALUE(WB, WC, UC_MINUS, UC, WB, WP, UC_MINUS, WT); } memory_caching_control |= CACHE_PAT; From patchwork Sat Mar 9 01:27:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587462 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 505D429D0C 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-609f2920b53so38121367b3.0 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=dow74s/Eg+IT/w1NSJFI6B+ajjOpiuQT/c6RzJrRlFAi6nSJeXhzSGSR1IAukZkavp tBXh6F0aWD/PGj7av3ttWw8wF6rVyAqcrXnu7jgrlGThDEHTsMvlrISEFriQUrjfYkIC fQW0KQnYnoI3TLKJI+iS/SrT1vkk9OpTmVl18esy8iC4RmMgVKYdmvKYp20k3NxUKn/k 5GJjGdyf7MW9C9WSQxffZzNNq+Ns7emDXnQCeykzie+fG0LVM0ve4/o7CU3AbDtQtJ9W MYngHnvMWUwkyar1YoalNEdjFMg+rUEduyQCdfad2LHYOaNmIspFI2LYbmUEfSpLtZrO /HoQ== X-Forwarded-Encrypted: i=1; AJvYcCWaVlX+0V+kMaLTCY6QzVlJEI9iFXZh2Qkqv7nPwp1VBj7HA0FioxdybJA9qMcHTJqoBtR4wXuWnvSpfS/hYzvMNMiI X-Gm-Message-State: AOJu0YwPo5Ul1eGDeES/IcSBpcHC7UjuQcZOHHpU4Ero9v2EdV9zkFJE tIaoCW7ReMdQ7nkD7rHjKW3hXWDh3hQaVp+IVYXboRC9zS4wv+VVl+wz4KpnDLny2lWnaZyWZW/ JBA== 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: kvm@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 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 = cpuid_query_maxphyaddr(vcpu); vcpu->arch.reserved_gpa_bits = kvm_vcpu_reserved_gpa_bits_raw(vcpu); - vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; - kvm_async_pf_hash_reset(vcpu); vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; @@ -12302,6 +12300,8 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) if (!init_event) { vcpu->arch.smbase = 0x30000; + vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; + vcpu->arch.msr_misc_features_enables = 0; vcpu->arch.ia32_misc_enable_msr = MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL | MSR_IA32_MISC_ENABLE_BTS_UNAVAIL; From patchwork Sat Mar 9 01:27:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587463 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 5A60B2E644 for ; Sat, 9 Mar 2024 01:27:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947660; cv=none; b=WcNPdT/6Io3L24pyj57L4kP/2nZVtLT9UgDTz3EGBZRedqyZYN5VRLksMIwCCy8bD4l5O2sMCDFdlZ3I9FfPcM3iZQghZwZOT7A1m/dKwDGpeZfU8hKrpJRy/jCY8ySOyFqUqAvOCqwjk+KxaWPJ2YO53NTDobR9y/tc3rSU3vM= 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.219.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-yb1-f202.google.com with SMTP id 3f1490d57ef6-d9a541b720aso4750458276.0 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=Mh7X0YGkAVYBhLO4+PONXdtnnL4tcJ9OOtvV6XZW7z8kwlx1Z604LFTM7SR5AGwI53 K+cbVVh+2lTPpWXs/RO8SVaLsMA7WMZdBNpzL4LSoB3/FaNvIueACtE4il0ePztfRF35 3+1VhfyjDm+9xqjyK7SAv7TNSs05BB1RBhY9yPQDqKw2fEAXrB5p6lwvK8Q4+wFM14sv XAdn/m6sqLDI6eyGAeySloIWOlM4siyKY0OIUpssgM17VXCK7LhOckNMh9YAmujSuVZb ioHhkl/+b4JM8rJ5rZLncw4UrsnrymBO7XpOT37tP++nBGgL7Nd9RnMMYHmLz9fmSTHM tSDw== X-Forwarded-Encrypted: i=1; AJvYcCUt58yrOKpdjkLf6l4/XsNXQwBINXWLutubcPqMDvWbEbrluZi0NXOBX/LKQp4h/5RydIO4cDV3uuS2Hv1dbgEwjU7v X-Gm-Message-State: AOJu0YwQ07dgH+Hz2GLX2vMh+HauE1htnViVFIGWQiXbOcJljsyNP5DR Xdum07aDTms7Rpjem+cVFgzIB+4AqfV2THrMQhePhhmQRxpgzJoPaVD/MBXTOGZK9+5oV25AAFi NJQ== 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: kvm@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 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: Zhao Liu Reviewed-by: Kai Huang --- 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-index.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 -/* 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_SWITCHING) #define VMFUNC_EPTP_ENTRIES 512 +#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); From patchwork Sat Mar 9 01:27:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587464 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 DB992364A5 for ; Sat, 9 Mar 2024 01:27:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947662; cv=none; b=Ne+FQsuf12yBMDdQ0ACJdxjgox45+eegJ1gMUil1SGEuBW66CWhppB6I+y12yC2bnmOXtyN4sOYf5oga8QXPbs9mD1wXAljIyxeEAPMlgP368Zm4kvdQa6mdZiWsnhWeTMOUlb8v6ah4ywAT0tw0m1bA1aDWCOprOZyRCIDxHmk= 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.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="cFfeOdfH" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcc58cddb50so4401773276.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=bbkLJYQaa6ghTC08W31q3zXyPwbXPo65KkxyHpiLKZm+joaVHZ0lvZa+jJsOJgxYE2 d98S3AN2wk1n342tLcBIVnl+MKjGbUhXCAIUJhZt0ohObOXDDgCLqR+VhE1wYiHrzCOE xmbbcrpVC4j1l5hVXEwmB+hVDJ7gJxCE/49w+BGbqrsfJZHxblapE6dhXvTo8G+er71c zYb6JhCNj3/tNDc5fjgKZuQOUCWELO582W5WHubszopYhhKIK/YxfK+1U8D4xVjzMlS6 pBCUPxtAo0oWFyFLVBgrmjJ4t+C7W/EAJ2kY7HIK3ojOhSuKCKwYbnKNHiWJnhFjHp1I ulKA== X-Forwarded-Encrypted: i=1; AJvYcCV4voG4Y1NwfSxLV+rva2dbf8f3Y0ZOL6cjPi6z+UhR/+plGuzPgcw/KPcHg9xWUn4q+6PbLaWuuUSCIq2pxE6y7ITJ X-Gm-Message-State: AOJu0YxdcYXuKrh17Hp0u3maZ79fCkZBT0ZV6khbGX4B2rQla24iMXIv /GZcLxzOQAImkCKq/5upDkKK0uT6Mi9g+pP70WSSmGEMyOixw0Z1cByZMZQFtUXgVP4m7JfXwQZ kIA== 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: kvm@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 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; } +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/capabilities.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 { }; 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_init; 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; } 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 msr) 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 = 0; u32 _cpu_based_exec_control = 0; u32 _cpu_based_2nd_exec_control = 0; u64 _cpu_based_3rd_exec_control = 0; u32 _vmexit_control = 0; u32 _vmentry_control = 0; + u64 basic_msr; u64 misc_msr; int i; @@ -2679,29 +2679,29 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl; } - rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); + rdmsrl(MSR_IA32_VMX_BASIC, basic_msr); /* 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; #ifdef CONFIG_X86_64 - /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ - 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 /* Require Write-Back (WB) memory type for VMCS accesses. */ - if (((vmx_msr_high >> 18) & 15) != X86_MEMTYPE_WB) + if (vmx_basic_vmcs_mem_type(basic_msr) != X86_MEMTYPE_WB) return -EIO; rdmsrl(MSR_IA32_VMX_MISC, misc_msr); - vmcs_conf->size = vmx_msr_high & 0x1fff; - vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; - - vmcs_conf->revision_id = vmx_msr_low; - + vmcs_conf->basic = basic_msr; vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control; vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control; @@ -2851,13 +2851,13 @@ struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags) if (!pages) return NULL; vmcs = page_address(pages); - memset(vmcs, 0, vmcs_config.size); + memset(vmcs, 0, vmx_basic_vmcs_size(vmcs_config.basic)); /* KVM supports Enlightened VMCS v1 only */ if (kvm_is_using_evmcs()) vmcs->hdr.revision_id = KVM_EVMCS_VERSION; else - vmcs->hdr.revision_id = vmcs_config.revision_id; + vmcs->hdr.revision_id = vmx_basic_vmcs_revision_id(vmcs_config.basic); if (shadow) vmcs->hdr.shadow_vmcs = 1; @@ -2950,7 +2950,7 @@ static __init int alloc_kvm_area(void) * physical CPU. */ if (kvm_is_using_evmcs()) - vmcs->hdr.revision_id = vmcs_config.revision_id; + vmcs->hdr.revision_id = vmx_basic_vmcs_revision_id(vmcs_config.basic); per_cpu(vmxarea, cpu) = vmcs; } From patchwork Sat Mar 9 01:27:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587465 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 3A72E374F5 for ; Sat, 9 Mar 2024 01:27:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947664; cv=none; b=JVC2v9O8gcq7GGnj14cMzHy7bXbKRgiNxEYZPpWUPDJ9ZJF5RQB6fxo7okwn3xodHyvMwwlbyRXtac8cd1/OjTj0brn0BM69b4otdnCg8499H/77GDIUmO+Z/qsv7DlkEsfVUAl9aswawKembVsgfoQ0/LTUi0iGqivOFzDEnUk= 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.73 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-f73.google.com with SMTP id 98e67ed59e1d1-29b5af6b4dfso2095988a91.0 for ; Fri, 08 Mar 2024 17:27:43 -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=GYKwfaXomyG4Kk+NmXcxMxiu1W97qBJvHgF8sxlJXZa2eRuaghiKTG2qD2Bdh/QN8w h96GROYNd+sEwGF3HXyogbqtBdg63dJkVctrtjS4L4vYcMmytFXrJJHkcRgbo081+qZL i0iEr92nI2GaD+M4IvFYWXE6EPswJjdviw7Rzhx11gyqbbMPodvmmJNXDUTIasiW7tkU i/wlYMBU7jnaM0t7TMDDVmXwW8xp3rSgyuwoKXmUo3WnroXRe/VQ27vc/TsebPHhHa62 TLbwKzmydWhNcWb0Vh+2k7G2Emxgfz8CmWCcVkOht1eI/iDO3+qj7vgfOf89wKmRIJcd 6fhA== X-Forwarded-Encrypted: i=1; AJvYcCWgECsQAMjiMD9PXAl6ad/SyCvbvkc27A6LaguhvXDO244dXY3ZFKR1rKPcxEtk9I7Wq6naAGldtXUKNy/LFaayCAoZ X-Gm-Message-State: AOJu0YxlyfsmhybppYDDHW3vg0/z406tDZqq8+8qu7pW102peRyWAgVK dk48+IJUTQU4WmTDqlovthOTH1iF4Rq2D71HQSkXn0CEHUXeoMxDsPzFItSSFUty4maKQ447aR/ QzQ== 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: kvm@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 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: Zhao Liu Reviewed-by: Kai Huang Reviewed-by: Xiaoyao Li --- 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 subset, u64 mask) static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) { - const u64 feature_and_reserved = - /* 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 = VMX_BASIC_DUAL_MONITOR_TREATMENT | + VMX_BASIC_INOUT | + VMX_BASIC_TRUE_CTLS; + + const u64 reserved_bits = GENMASK_ULL(63, 56) | + GENMASK_ULL(47, 45) | + BIT_ULL(31); + u64 vmx_basic = vmcs_config.nested.basic; - 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; /* * 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; if (vmx_basic_vmcs_revision_id(vmx_basic) != From patchwork Sat Mar 9 01:27:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587466 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 1E95238382 for ; Sat, 9 Mar 2024 01:27:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947666; cv=none; b=qW0/3Nqa5D38UiXSYBh5br/xN2iDPajfj8WraotM/57xN4u1HR9sljDb6Bc+e4NgfwP3jHJWT0DHj5CQUV32MkpUhjXBao/BWYkwmRu9E7sLHLflvb9UiA22kER7ToN4AnmSIGELTlnh6Z47nRNUbAqKqofbO9p27AYHe042pf0= 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.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="uDqmOj71" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcd1779adbeso5022375276.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=VD2TScB4GQCNdn95daohQUc5zOP2kJry+8GmAPMcChfStR8J31mCTHOJ+KsbjwKE22 pIEfH0/zqFyHIk5mzQnh1pqjURGPOa0FKtaMDX7OAI8cfkPvO/KfFQ0Wjp9xeyNNVi8e NWuO2zqNjnUkNZ0hNet6hK7vkDogqiaKu+WXK2/d3Ctt68xq7VrayLvjZ/AvCHs3aB7J 0v+PVuE1YkScPmGCFkTCe0ET/a7FnaTVTpKM9Us1YJMGGfeEmYA0N8tLf+dgrFN+b/E2 yG6PCzkDq+aEIGC8KIHbppxCdeNr2s8gaCvpUpqV412LiCdpU9YHpNhejAbS3/vytLg2 OH1g== X-Forwarded-Encrypted: i=1; AJvYcCXG/9xJKM9ABWeBADfmuU+yqnDaCF7ltFFgPKyupSGgO1Y4NBt/xSVPjOBnbtWlkdNELBJTEn4ZdZhJLpHDEL0oP28n X-Gm-Message-State: AOJu0YzUF0PoIHb9rimatZY4A0KHe8JoHswrpWv5ucETlzPISQg2QBKo ZSS/FaK93kKhKIu5R7hAnrQqxOXJs9i4lre5n7a1DfPLebhYdgtw35I9TGfyjshT/6/4TxHkJMZ QlQ== 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: kvm@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 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: Zhao Liu Reviewed-by: Kai Huang --- 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-index.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 -/* 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 @@ #define VM_ENTRY_ALWAYSON_WITHOUT_TRUE_MSR 0x000011ff -#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) @@ -155,6 +148,18 @@ static inline u32 vmx_basic_vmcs_mem_type(u64 vmx_basic) return (vmx_basic & GENMASK_ULL(53, 50)) >> 50; } +#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/capabilities.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; return vmcs_config.cpu_based_2nd_exec_ctrl & @@ -365,7 +365,7 @@ static inline bool cpu_has_vmx_invvpid_global(void) 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_config *vmcs_conf, { msrs->misc_low = (u32)vmcs_conf->misc & VMX_MISC_SAVE_EFER_LMA; msrs->misc_low |= - 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(struct 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; } static inline bool nested_cpu_has_zero_length_injection(struct kvm_vcpu *vcpu) From patchwork Sat Mar 9 01:27:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587467 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 E6C7338FA6 for ; Sat, 9 Mar 2024 01:27:46 +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=1709947668; cv=none; b=bMa7r/tZUWz+Ki5af8MfLfqsP0+c6xAba8HnyFVzqi1yxq8Lp8HL90S6/YPXYrOVRDsC0FgpYUH9PPAGTNy512WKHdMg2QjNvfCTvElEvnEYwDX2qHiTgw1U6tq9Co/BOCJnW+wEs9eTKf0zYU7Ij/+HzdflBFzki1PVi6Q6n2g= 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.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="QcAq61iA" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-6e672f1880fso915562b3a.1 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=EDTxw9lKjZtlDLazAe/ek6smjwUIxx49J8FzlYn7F14ElHjqN3EyVQeYTqLQywtliy uZ9XXb5s6psXzk2t5rXQdzCUZN/qP044D1CA38T1Hkd/lTv4Netx6bMp8ze4HvGwt3Cv +TnFiexmdL6f+nb33DHPZxxLkquwPV9NrM5pIda6luX1LR4ko9+xfYZDDLSBZ/YPqkpE TRyLleWRyqdDM96EMgW5ypwRDe9cJjyvgLe3OwBQNRfyIzQO9plDBuEhoFHNLrE3OJ6R aBzHjWl447eWDUAGgF/v8BrG36bndylqHWXBage98q//KRkpdWMsUzsZ7MCyodWmiKcT 56ig== X-Forwarded-Encrypted: i=1; AJvYcCWQJdb3dcLcns8g/m+GWvIwqOM95ytBT9bvDx+kSrSKWx+lr0XfjlwmrkbcxABxgzn34BnYT6G3lqADTNqrWQP6j3mc X-Gm-Message-State: AOJu0YwkeMafMsQo6Z6PLvfjS29kxBPE+shgf/9uiUkBzNyTCrSf4V8e sPtw9a3PbJDkwbkHj4WLicP85x3IvoK4G61j17oHgH1/USTqa9YFSAK7HBhtmjmj5BR7CvO4281 PeA== 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: kvm@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 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; } -#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) 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); } 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 = 5000ULL * 1000 * 1000; cpu_preemption_timer_multi = - vmcs_config.misc & VMX_MISC_PREEMPTION_TIMER_RATE_MASK; + vmx_misc_preemption_timer_rate(vmcs_config.misc); if (tsc_khz) use_timer_freq = (u64)tsc_khz * 1000; From patchwork Sat Mar 9 01:27:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13587468 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 9BD673AC08 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=1709947670; cv=none; b=jw7ZTRuePhKKBjPPCT0NIjy2/BMMg6quz68jBoeI4ft/k6gWXpqqJIJ7fujBKiuwxlgRiIqknqVGZDmLDlpjgSI3aFi512RL9iF2Ad54L3caCg8Rt/APT7bqHcY8BV+qdr8LBkCNTm1oLLQMkJlCSbcsTzYOgWROJio88bHL0Iw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709947670; c=relaxed/simple; bh=3RB5ybb2eWpxvWqhePHFkoyZ4DVvi7cGA85H74hezaE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=scFlfIuYkF1sAzDaNWmWMcsH87t5lWttBqpw0p7EETnTaxGxgoNFGjWKClVuFUIEAZiXagJobOwA6NihUvdpXuQeWggUiAaVW6pO9MvMOPsxbAxyoQ/JNVJVWlhNskOT7N2Syl638IfUYDjFDtt9DGEmDNpyRnIRC7c0W3dYeWU= 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-6e62136860eso1259941b3a.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=JG3pcP2tXAhxjAX/atvSa6iHMKZz9z8YbvQ8TUJjserLko1gRWInrtJ2MCiIwluAll PuPFtMbqFJYlDrgFeJxy4G/ocAu3C2nF79zQ+nzg+AsiW8Y6GkvWpiLZv7UuqFrZDOV6 1VQ7yJh75zkqsPZT6cKEm2y0yGDihCPQPH1lDPM/do59KOfZ2v+hQKSVNTOEqPqd1g5D /yQGajQ3HZtXaivLQ/F4ma9LXbSqOTRKB4WGisjPgKu6H0gc9hExmzTzEgxiHkiRhVfb o1sSlDzw7nETBN/OE73JywAbazMuJDChWm2r5fRL0VXTT8CiLjl9z0e5LLcUwhdx2BZw UWZA== X-Forwarded-Encrypted: i=1; AJvYcCWuGkw5uniZ0fHF07ta/NetES9PwW5lsNLUnCkqis1CNMUoGSaSTLvwOhQWNyn5snZjnxkr9JJYMHEzr0oFNgmdwkFn X-Gm-Message-State: AOJu0Yw3cDHuMj11xCwGgA3v6CEcw/XKGp6FXzLN9LhNSeQ+QFIQ2cGx A7TSZ2aeXf/vP/XazVeg3k8flDud5qyVMZk+4mJym4s9EzMBKB28mLqIBLKsquu8JYfM2b9G5Vf A7Q== 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: kvm@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 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: Zhao Liu Reviewed-by: Kai Huang Reviewed-by: Xiaoyao Li --- 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 msr_index, u64 data) static int vmx_restore_vmx_misc(struct vcpu_vmx *vmx, u64 data) { - const u64 feature_and_reserved_bits = - /* 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 = 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 = BIT_ULL(31) | GENMASK_ULL(13, 9); + u64 vmx_misc = vmx_control_msr(vmcs_config.nested.misc_low, vmcs_config.nested.misc_high); - 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; if ((vmx->nested.msrs.pinbased_ctls_high &