Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp940402lqc; Fri, 8 Mar 2024 17:29:44 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV7C7x7kcHQUZYpltwtnQ4WGXtHRobRvAgJS3fWBiOV8Xk7r4RayqZdCYUs6ed2KAAH270/Vc3jiywRVnUvu1Z6/KggXHj651A4UR90LQ== X-Google-Smtp-Source: AGHT+IGc0Q3ZtEwLJ3C4GCrpU5FcK58NhLjAMQyG7R2jPp+5iDTfLiNTUu2FHnSLxzmf1X4uprMC X-Received: by 2002:a05:620a:4493:b0:788:2aa6:8c4c with SMTP id x19-20020a05620a449300b007882aa68c4cmr1070839qkp.16.1709947784160; Fri, 08 Mar 2024 17:29:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709947784; cv=pass; d=google.com; s=arc-20160816; b=loqRBvtkWlB6CGtv95fgosGXTYsBgD7bCqaXxGCHHYWBd6/seu8YElWu+aJ00rccLU PRm5tJ/v0PCUoHASYjs506BgjgXXILgqfZLP/sL/zqyAZJTRlOYmH4tx+tf8RPEJImCJ kdF2bUN/TMvKmLP4PH9W0lNHZgq3bG7EYnAF84QRN0v1ncxnccUhw9VxV6ZnE2BD00W7 loU+AlbZTt6cX3B27Rd+1Lrlt2P57nZJx6vY/nokmIumKKTQYitbZsqzsZr50r4ms+l5 2whruDqMbYq9e5blBb1JHqWt98QnXfd4qPc+E7FOtjtpgQ0JKjdzAsn3jmWPRQstum1m DERQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=7zfSsZ0P3OmkAxzO4p6XnXAYwdNDLv4d4j/tO3LJoqY=; fh=NyWG4rD5t6h5r/4R5u0tCCJ3lWPqli6Lgx24ds/Q2bw=; b=HPB4k3jaMPQ+aFfGHM9+hCz6Xst8WWs4E+bmOP8I3gvPih9LjmR9785vYYqDJfDyAa z1sMLLnuCrxx99vm/ZvMx1Z6XD9Lx1lgV3VlBypJw286cj2KZHPe9ATLWmAhJ0YjQW9W YBVYY3M11tRDa5Om8qHqB6TuWjXIBoSbbmnbDMBLe3DVabDAqnrH+Zk2+5fxZz95i+nS 0xb2l28hm79yD7SMjBfe8zRwIrLSZVlZVswJo6pvxPA/uE7oM9F84903Xjcr4xCLXa/K PEEkw6OpVBZTDorszpFp7+cy1BO30c4dtUcJx0/Jc3ogJCUN1mwPpsBxnG/YtGMJq6rb 3aDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cFfeOdfH; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-97731-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97731-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id pz7-20020a05620a640700b0078830baaa2esi746290qkn.572.2024.03.08.17.29.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Mar 2024 17:29:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-97731-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=cFfeOdfH; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-97731-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-97731-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BE8411C20EC6 for ; Sat, 9 Mar 2024 01:29:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 654B9376E0; Sat, 9 Mar 2024 01:27:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cFfeOdfH" 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 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-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-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 --- 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; } -- 2.44.0.278.ge034bb2e1d-goog