Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp310486lqs; Tue, 5 Mar 2024 02:46:32 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVXE4Cs2PTTqynnZZyt6N22GWP9mcXEbWNtRFNg+N7LuSjAiadoh3t2ALxX0Q3b3IWjZ4Md8D4pvjnoU1xPuy6kFxoYboXKPuNTl2NJjw== X-Google-Smtp-Source: AGHT+IEc4qJ5kvisGhH9H72SZwbyHk9rNgC1cdH/cP27IX4xrg26CkZBGxXV1Hl7aO0tZ3jHFgK1 X-Received: by 2002:a05:6808:f04:b0:3c2:ffb:3365 with SMTP id m4-20020a0568080f0400b003c20ffb3365mr1490716oiw.25.1709635592655; Tue, 05 Mar 2024 02:46:32 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709635592; cv=pass; d=google.com; s=arc-20160816; b=NhIDFPRdIGhKLHjj3Zo9AZ3/O4s8RGCKt0SRvbZGF9vp3Ufb/Gxqfp9008UyDWl4jM n03sxrKO+02nGnOqGSUkcAkn2Vd2sHW1tACN6gyVtNNJ24FZ757Tek5IsM89wu+KqmpS YOQjQRuo557aVtU4dbhzqOugE04/qT6SqQjZpaxEg78TgvqSla1elwcZWtmHks+6kCU5 evD1Ce59CRyNxL2rrIUJd1zqWpkl8VcghRDB27KwThQFt+d/JTAAEwUeaNSMe7iBDYbO hr7RPlGymSA416fF+T88MbEo7ITCZ6TeXuym0luPZPFdfFoAyBxrE/DCj/9ypx9OovIj uI1g== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=NbpRvhegxzAibgmjukvrlXm7mMqQtfg7UO/OQspIw34=; fh=xIEQLB2inP02AVT4siOgme+TLzVja6G/aeC4gwiWfh8=; b=f8lpEQdAVLILuw/+05PQNFfU3Bt30y+d+yFyltTHKPhFPa2hebKp1vOeC+G6dnYlsX JmLQIA4AIyUiC1U0uqmjDHDh8JqxBEd7S+IJUvnutUFWjKg7GMDu+Yop6NTkRt0oiYui ATFKKvas/am/I0Tc/R6xLsjg7kceSHzCPKvmxKIH2iMdhngNtumuU+VNq0HMGqHgMWta PdTEOLQoFgdpeCJOT5XOn4miJ3OJcLNqYH0VFR4KDKcwzygUIzofk4lLgMiNcZDsV3gT tuNPlybq9LQpYp/2lFQ8jNwwHZQQQ+tl8pUFT97bThRrJ/hFGPsVURRf0ApY9a9qEF6e HoUw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=hhg1UTGm; 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-92150-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92150-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 f22-20020a631f16000000b005dc8762dce5si9839507pgf.51.2024.03.05.02.46.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 02:46:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-92150-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=hhg1UTGm; 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-92150-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92150-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 4DB1E284EBB for ; Tue, 5 Mar 2024 10:46:32 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7CF55548F5; Tue, 5 Mar 2024 10:35:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hhg1UTGm" 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 EAE3C33D5 for ; Tue, 5 Mar 2024 10:35:13 +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=1709634915; cv=none; b=SnBSxfyi48EOkuZIh+1K4r32Rd3K1hxXcYjD/NqWy3Lh1qRlYzRUVc9E5pNMhaRU4gl2tChI7CH79T/KeQdjFGBO5rrlvLziNuhMnqxpjmNSnTDYPNn0sKXDYPivfEym88FTonAHs1QWzwuxplYQ9O3MYMPiU3kdIPb9B+FZkc8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709634915; c=relaxed/simple; bh=rD7LNIXQgytgk1SuBjjFtl3xeLRotb12aTcaosK398I=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=MbKsXHa35yHpFNoWAaolicvSxlU+utEDSB9dO6JiwFMbMPp3Yq6Pj3wg4XoZG9bgKHwmXvb7nkaNy6y4xhpiu/Cx4pkk3s8v/r+Eozn91DRjX2MVNUF6m4ZsqHbPitphltF5Urt6NsVXkoWd6dnivKFFquW37DDed5D8wMvm0QU= 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=hhg1UTGm; 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=1709634912; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=NbpRvhegxzAibgmjukvrlXm7mMqQtfg7UO/OQspIw34=; b=hhg1UTGm2xyc56N4Qgt7oOfIZbIMcbrAfzoAanwCwlVq896cBpxWgB3/0zMqCf93+Fr3np KCGcegYMddUqplwRp/jeFzGaYIkjtv+8EhOPZIGq61P3/e/fZcIvV/evip68IzOrFK0P19 1ppPQfKxIS5lfQQ7p6ODm1ctYI/rquk= 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-691-TVz_IhSJNWKUXLei6GDKIQ-1; Tue, 05 Mar 2024 05:35:09 -0500 X-MC-Unique: TVz_IhSJNWKUXLei6GDKIQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 B7A45803F60; Tue, 5 Mar 2024 10:35:08 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.193.36]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2F53D111FE; Tue, 5 Mar 2024 10:35:08 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id F363218000B2; Tue, 5 Mar 2024 11:35:06 +0100 (CET) From: Gerd Hoffmann To: kvm@vger.kernel.org Cc: Tom Lendacky , Gerd Hoffmann , 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 v2] kvm: set guest physical bits in CPUID.0x80000008 Date: Tue, 5 Mar 2024 11:35:06 +0100 Message-ID: <20240305103506.613950-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-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Set CPUID.0x80000008:EAX[23:16] to guest phys bits, i.e. the bits which are actually addressable. In most cases this is identical to the host phys bits, but tdp restrictions (no 5-level paging) can limit this to 48. Quoting AMD APM (revision 3.35): 23:16 GuestPhysAddrSize 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. See “Secure Virtual Machine” in APM Volume 2. Tom Lendacky confirmed the purpose of this field is software use, hardware always returns zero here. Signed-off-by: Gerd Hoffmann --- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/cpuid.c | 3 ++- arch/x86/kvm/mmu/mmu.c | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 60f21bb4c27b..42b5212561c8 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; } +int kvm_mmu_get_guest_phys_bits(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 adba49afb5fe..12037f1b017e 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1240,7 +1240,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) else if (!g_phys_as) g_phys_as = phys_as; - entry->eax = g_phys_as | (virt_as << 8); + entry->eax = g_phys_as | (virt_as << 8) + | kvm_mmu_get_guest_phys_bits() << 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 2d6cdeab1f8a..8bebb3e96c8a 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5267,6 +5267,21 @@ static inline int kvm_mmu_get_tdp_level(struct kvm_vcpu *vcpu) return max_tdp_level; } +/* + * return the actually addressable guest phys bits, which might be + * less than host phys bits due to tdp restrictions. + */ +int kvm_mmu_get_guest_phys_bits(void) +{ + if (tdp_enabled && shadow_phys_bits > 48) { + if (tdp_root_level && tdp_root_level != PT64_ROOT_5LEVEL) + return 48; + if (max_tdp_level != PT64_ROOT_5LEVEL) + return 48; + } + return shadow_phys_bits; +} + 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