Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1694820lql; Wed, 13 Mar 2024 05:59:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUhJyr9cIrBQBkxTEN/fXIWB3/Ju9fuBhYXTz94hLSDKMXgmZ4L0nY8HckdM8dCHg2ytyqi7Ufa8r/oKabW6QxpwMU1xK4VKCW7VT5kYw== X-Google-Smtp-Source: AGHT+IFPhmjMh4zrhx/Ev41s7lVApitWQ8OPbTCqhu2vFg6q+171ri9PlHiVtZ6k2ePtOsPKEZ3h X-Received: by 2002:a05:6a00:1389:b0:6e6:5343:c165 with SMTP id t9-20020a056a00138900b006e65343c165mr3124273pfg.16.1710334743743; Wed, 13 Mar 2024 05:59:03 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710334743; cv=pass; d=google.com; s=arc-20160816; b=g47caQ+rukrkqItGdkUi6g5xJ2uDR9eStKWcu3bURf9a81jIUx0yQ4dy9/e34BJgi6 V9C7gk0up1o0Ce520NJza+JjoCpiwHNeGIVOLijDou7LYWX1/fAO8pK4ArGcEied7a0F rFWiyw3Bc5HNA5HK4GCtkp2bmOH5LFEDPAiWdNae3mORV8lp+r2pJapLiFb7Fnt+ppUx qxjdR0FNXck+3l/nQbDPNcji7DjzukwZPk4/R9R1rlVcof9NVQdzUbviwkXVRnPfemTu Y3bDbjvbjztyoqEiLNv8RpjomEEkvgZDclJ+TsB7vtIjq/JGWMNpM6lhvUWugJe6xAdU 9QwQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=20mUBcwllGx12FWQlFMvjVnIzYpvKcCQQXjfzRSGeyc=; fh=+0nuhWk4EeR9G54N0UU2EER7DXdpp14CTwi+HUVZv40=; b=nVnOE0dlGY/3VO63ks3aJCJbo5sz2VzunPglo6dpzmQ0WP09HqSzTI40kQPVQTjU4+ 7z9HEOnmFAnFL9MQBqZlDK8jZoYgk9PTExjmZ5DvfiSPFTfxA2IMF2L1cNIG6KPd7H8f uXC7gBaCoOvPauW6YngxCCjR+SflG3ECBbKgtXLj3UtWcrWSu5gpPpw/QVIImMwLxvsd 2zWhGdOzaseRe+GKm+W4yCPEA4GlQAr6RsT/2ERaS6MMsVysT84upqpqfg9pC6oo4+04 TuWhSUSg+0nmqczm1KQwz4bZllqTJ53YZKZbo5MteFvAr8I+u/BRflpxuZzr0oqNmcpI 7gsw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QyicdYdm; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-101501-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101501-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ln4-20020a056a003cc400b006e464571dcesi9158748pfb.310.2024.03.13.05.59.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 05:59:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-101501-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QyicdYdm; arc=pass (i=1 spf=pass spfdomain=redhat.com dkim=pass dkdomain=redhat.com dmarc=pass fromdomain=redhat.com); spf=pass (google.com: domain of linux-kernel+bounces-101501-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101501-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 6B464284C8F for ; Wed, 13 Mar 2024 12:59:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B0BBC4087C; Wed, 13 Mar 2024 12:58:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="QyicdYdm" Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E1F53FE46 for ; Wed, 13 Mar 2024 12:58:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710334732; cv=none; b=Tne/cCQOwD0ehX1Csu2cT2T4QVGTHhrRI95s1fqwEKgiS3hguuAV2ll0Fiw6GKI/Gs5Lw41HqKg0hyJE7R9/+xA0/s1JippWd7yBgO//BpBav4Y//JRWSBnQpzghPfgyrq0nwOOE3PCb2Al3YTtbWY9ntK1Ed+vqCl84inceAuU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710334732; c=relaxed/simple; bh=ds4LSmLFzqZm3/IqgZJ/IqJDdOGemJpXab4dvBlKsic=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FsT5kWfjhOsG1RglV4KrPhIBSbGemvnyJkQ0TkvYphvEKnaU6B0YcygW4Ri6EuaYKJe0QIvMiXk3ThANi+qUfkJRixVfc3lyJUCzPBzmVOJ/oJysyn3njxpVfr92myBlovXZSomIXJbO0Aiw3XRSQkS7HBiOMZWYQZ/zSmrPAq8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=QyicdYdm; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710334730; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=20mUBcwllGx12FWQlFMvjVnIzYpvKcCQQXjfzRSGeyc=; b=QyicdYdmLFausx94v1NKLLhuSVgsD0Vu7G1YUuwVWhXc9gP2iD3GrUMRPwzaWgtyNiprtF bmodcOZJha/S8ReVVboH2ktjRFV3aBC/QB/irSeczXM8hUUtkmecSK/mrRy8DNb99SLNn5 +iHD91S9vMSGSTehcXKGCb+irje/3Rc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-569-WnNgWH4XPSq_E3eQvcBrng-1; Wed, 13 Mar 2024 08:58:48 -0400 X-MC-Unique: WnNgWH4XPSq_E3eQvcBrng-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D0F1F80026F; Wed, 13 Mar 2024 12:58:47 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.192.160]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2EB6340C6CB1; Wed, 13 Mar 2024 12:58:47 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 262681801494; Wed, 13 Mar 2024 13:58:45 +0100 (CET) From: Gerd Hoffmann To: kvm@vger.kernel.org Cc: Tom Lendacky , Gerd Hoffmann , Xiaoyao Li , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), "H. Peter Anvin" , linux-kernel@vger.kernel.org (open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)) Subject: [PATCH v4 2/2] kvm/cpuid: set proper GuestPhysBits in CPUID.0x80000008 Date: Wed, 13 Mar 2024 13:58:43 +0100 Message-ID: <20240313125844.912415-3-kraxel@redhat.com> In-Reply-To: <20240313125844.912415-1-kraxel@redhat.com> References: <20240313125844.912415-1-kraxel@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 The AMD APM (3.35) defines GuestPhysBits (EAX[23:16]) as: Maximum guest physical address size in bits. This number applies only to guests using nested paging. When this field is zero, refer to the PhysAddrSize field for the maximum guest physical address size. Tom Lendacky confirmed that the purpose of GuestPhysBits is software use and KVM can use it as described below. Hardware always returns zero here. Use the GuestPhysBits field to communicate the max addressable GPA to the guest. Typically this is identical to the max effective GPA, except in case the CPU supports MAXPHYADDR > 48 but does not support 5-level TDP. GuestPhysBits is set only in case TDP is enabled, otherwise it is left at zero. GuestPhysBits will be used by the guest firmware to make sure resources like PCI bars are mapped into the addressable GPA. Signed-off-by: Gerd Hoffmann Reviewed-by: Xiaoyao Li --- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/cpuid.c | 28 +++++++++++++++++++++++++--- arch/x86/kvm/mmu/mmu.c | 5 +++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 60f21bb4c27b..b410a227c601 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -100,6 +100,8 @@ static inline u8 kvm_get_shadow_phys_bits(void) return boot_cpu_data.x86_phys_bits; } +u8 kvm_mmu_get_max_tdp_level(void); + void kvm_mmu_set_mmio_spte_mask(u64 mmio_value, u64 mmio_mask, u64 access_mask); void kvm_mmu_set_me_spte_mask(u64 me_value, u64 me_mask); void kvm_mmu_set_ept_masks(bool has_ad_bits, bool has_exec_only); diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 3235254724cf..b54a85d7387e 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1221,8 +1221,22 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) entry->eax = entry->ebx = entry->ecx = 0; break; case 0x80000008: { + /* + * GuestPhysAddrSize (EAX[23:16]) is intended for software + * use. + * + * KVM's ABI is to report the effective MAXPHYADDR for the + * guest in PhysAddrSize (phys_as), and the maximum + * *addressable* GPA in GuestPhysAddrSize (g_phys_as). + * + * GuestPhysAddrSize is valid if and only if TDP is enabled, + * in which case the max GPA that can be addressed by KVM may + * be less than the max GPA that can be legally generated by + * the guest, e.g. if MAXPHYADDR>48 but the CPU doesn't + * support 5-level TDP. + */ unsigned int virt_as = max((entry->eax >> 8) & 0xff, 48U); - unsigned int phys_as; + unsigned int phys_as, g_phys_as; /* * If TDP (NPT) is disabled use the adjusted host MAXPHYADDR as @@ -1231,15 +1245,23 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) * paging, too. * * If TDP is enabled, use the raw bare metal MAXPHYADDR as - * reductions to the HPAs do not affect GPAs. + * reductions to the HPAs do not affect GPAs. The max + * addressable GPA is the same as the max effective GPA, except + * that it's capped at 48 bits if 5-level TDP isn't supported + * (hardware processes bits 51:48 only when walking the fifth + * level page table). */ if (!tdp_enabled) { phys_as = boot_cpu_data.x86_phys_bits; + g_phys_as = 0; } else { phys_as = entry->eax & 0xff; + g_phys_as = phys_as; + if (kvm_mmu_get_max_tdp_level() < 5) + g_phys_as = min(g_phys_as, 48); } - entry->eax = phys_as | (virt_as << 8); + entry->eax = phys_as | (virt_as << 8) | (g_phys_as << 16); entry->ecx &= ~(GENMASK(31, 16) | GENMASK(11, 8)); entry->edx = 0; cpuid_entry_override(entry, CPUID_8000_0008_EBX); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 2b515acd8e72..74b9d0354bff 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5309,6 +5309,11 @@ static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu) return max_tdp_level; } +u8 kvm_mmu_get_max_tdp_level(void) +{ + return tdp_root_level ? tdp_root_level : max_tdp_level; +} + static union kvm_mmu_page_role kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, union kvm_cpu_role cpu_role) -- 2.44.0