From patchwork Fri Mar 1 10:14:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 13578282 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 68ABF6BB21 for ; Fri, 1 Mar 2024 10:14:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709288061; cv=none; b=sFGBu92khSuZhiSc2JcrI4TTEUY6vsJWBCsyn5B9VXktsd6tS1FwkKCOoANBd54uiGmOgm5EKyXQvzotuG9gzrE41er9qp/tRHOrQ/CtcOuZLjHn+a/ixjoj1W7TC4T5IAXgMW47vSEaWMRTnp2x4zM/XXU9tzycEXpJ5ITK5eo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709288061; c=relaxed/simple; bh=uWxgVAOGbOSnxDWiId7krxLynUZio8/ykxEd9RkbEQI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=m+RGglA5ZnFp60IdXPanCfkx3ANh9mvBx0VEvg0WKiJG7Gq0VjSe4HYZg76F2sU+o3WQr9RFJlf8m00WZyMJFHWStnUb1hsHS1vOHkW5ijXjYdjm11aO8XoovovSRh3vGTQxYh1SKxxu84dUvxuBtBYVs/vRt5YFxdeAc7MfYbI= 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=Yt7OxJe4; arc=none smtp.client-ip=170.10.133.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Yt7OxJe4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709288057; 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=EQfnpdN6UNMUhl10QWdlY0XPgUMsy+RQDz3+hBDJ3Y0=; b=Yt7OxJe4BEgxKF3604ciNL18KJNVNQwvLGiraoco6KcKOlckEgGiq2PcKO7meP+BeQkMeh SR8mJTZMzX1Ay7YsSyjZL02dgpvz5JCmCqxVVWDWR3LexJNaOFmM2BBix/3s2gYL+Sayci 1NzmwFsohj7u6SIPYQzzy3fxpsHUFEs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-534--BHjQmXgMBqh-ay5ggpBlw-1; Fri, 01 Mar 2024 05:14:14 -0500 X-MC-Unique: -BHjQmXgMBqh-ay5ggpBlw-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 8FDC638062C0; Fri, 1 Mar 2024 10:14:13 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.192.121]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DD32763A69; Fri, 1 Mar 2024 10:14:11 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id AE62C180148E; Fri, 1 Mar 2024 11:14:10 +0100 (CET) From: Gerd Hoffmann To: kvm@vger.kernel.org Cc: 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 1/3] kvm: wire up KVM_CAP_VM_GPA_BITS for x86 Date: Fri, 1 Mar 2024 11:14:07 +0100 Message-ID: <20240301101410.356007-2-kraxel@redhat.com> In-Reply-To: <20240301101410.356007-1-kraxel@redhat.com> References: <20240301101410.356007-1-kraxel@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 Add new guest_phys_bits field to kvm_caps, return the value to userspace when asked for KVM_CAP_VM_GPA_BITS capability. Initialize guest_phys_bits with boot_cpu_data.x86_phys_bits. Vendor modules (i.e. vmx and svm) can adjust this field in case additional restrictions apply, for example in case EPT has no support for 5-level paging. Signed-off-by: Gerd Hoffmann --- arch/x86/kvm/x86.h | 2 ++ arch/x86/kvm/x86.c | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 2f7e19166658..e03aec3527f8 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -24,6 +24,8 @@ struct kvm_caps { bool has_bus_lock_exit; /* notify VM exit supported? */ bool has_notify_vmexit; + /* usable guest phys bits */ + u32 guest_phys_bits; u64 supported_mce_cap; u64 supported_xcr0; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 48a61d283406..e270b9b708d1 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4784,6 +4784,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) if (kvm_is_vm_type_supported(KVM_X86_SW_PROTECTED_VM)) r |= BIT(KVM_X86_SW_PROTECTED_VM); break; + case KVM_CAP_VM_GPA_BITS: + r = kvm_caps.guest_phys_bits; + break; default: break; } @@ -9706,6 +9709,8 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) rdmsrl(MSR_IA32_ARCH_CAPABILITIES, host_arch_capabilities); + kvm_caps.guest_phys_bits = boot_cpu_data.x86_phys_bits; + r = ops->hardware_setup(); if (r != 0) goto out_mmu_exit; From patchwork Fri Mar 1 10:14:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 13578281 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 6B2A16BB22 for ; Fri, 1 Mar 2024 10:14:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709288060; cv=none; b=UVK1fyaGkpBGlNkhoYizRpQT+gNJ0mHs4QJciUqLwpuM0zf2V6QYqmfJxk18KfqDj69v2pyNhk6lKHiHNP80vHPAbAvZBhs5T/BinXiNddm+foyHY6++4PV0MOuQkTzUjoCvs60P6YqCUNKLwC9hlmAgN8Qsa80RDFy5TJL9O1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709288060; c=relaxed/simple; bh=x+nTTwmEfaKEpgrbbMiWdsUHJXOKVRsj2CPeKkTwj3I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oX+0h6VaV5NrCqalCspY/cEO/PQa8zYYSi9+dVRR086VkGhuxUVgr9vF+3xoLN35ty3w34hoTKnTfACEnMLJWl15a3chruig/nGkP5WgC1T5AFUom4ydaOL+VLaGLNX+nkEe3ovnaBfh9DUl2dr1trme1SZDYVTsS57J78MudW4= 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=bY23dM0X; arc=none smtp.client-ip=170.10.133.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="bY23dM0X" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709288057; 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=R+QSb4VJ1jHAbQmwW/SLNpPZK+rj9N/eRYzdT8VP+cg=; b=bY23dM0XM/ODIqZLVt4gKlh1SYkVhCEPvFgGnowKo2Dk6TNobzOXzWp1nUypnNw1V3Gu0w gtioZ3UUSJ5IDfyLJFBPftiCArYZ7GgYf9RPDl9r0auC3Yz5Cp+vqy14VRRIU77Y9Ozb+D yq5/iPwsZhdCiVunCr0MlCslVCPtHjI= 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-662-QWOY9T-_N0yzeq7pYgncJQ-1; Fri, 01 Mar 2024 05:14:16 -0500 X-MC-Unique: QWOY9T-_N0yzeq7pYgncJQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 58473108BD87; Fri, 1 Mar 2024 10:14:15 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.192.121]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5C1F02166B33; Fri, 1 Mar 2024 10:14:14 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id DAB481801491; Fri, 1 Mar 2024 11:14:10 +0100 (CET) From: Gerd Hoffmann To: kvm@vger.kernel.org Cc: 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 2/3] kvm/vmx: limit guest_phys_bits to 48 without 5-level ept Date: Fri, 1 Mar 2024 11:14:08 +0100 Message-ID: <20240301101410.356007-3-kraxel@redhat.com> In-Reply-To: <20240301101410.356007-1-kraxel@redhat.com> References: <20240301101410.356007-1-kraxel@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 If EPT has no support for 5-level paging the guest physical address space is limited to 48 bits. Adjust kvm_caps.guest_phys_bits accordingly. Signed-off-by: Gerd Hoffmann --- arch/x86/kvm/vmx/vmx.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 1111d9d08903..8bd644a5022d 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7899,6 +7899,11 @@ static __init void vmx_set_cpu_caps(void) if (cpu_has_vmx_waitpkg()) kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG); + + if (enable_ept && + !cpu_has_vmx_ept_5levels() && + kvm_caps.guest_phys_bits > 48) + kvm_caps.guest_phys_bits = 48; } static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) From patchwork Fri Mar 1 10:14:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gerd Hoffmann X-Patchwork-Id: 13578283 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 AE0F56AF99 for ; Fri, 1 Mar 2024 10:14:19 +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=1709288061; cv=none; b=gB+XOLhVQMVMsVA/e3mf6/j2OduSNyQPRPR7qesyIuJM0/JHF7XImT2iVNYiZx13d6iHk3Qb8B/Jd0ZXAkmxUKo5kP/CSavGNLBRdzmqIhPjh3y5BanY2EmrlfQlTafz+kudlsMvPKgIrwU9D4fSQv75T8FYex67li0xxoAdekc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709288061; c=relaxed/simple; bh=sND8jhV+K31Cz2knDCDV+qPQDZGZA9Xo2gOcAqB/Ihk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qYSNNtrQppQy0vjnu/Vqqr4g8YXCOgXLmvYyhHtyP1+7oUIq+cISDlnXl7TuPPz3aPYSQ31/3KCwI0vYYkwUsW7+gzizsKuVnPT9p4RfkCWZ4Mc7zNhBOQyTkKcvJxwdBu21bU/6Jo1Gp5LekfFfLwnIuyccv2ctMdjxxqGA594= 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=hv+/9qb5; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hv+/9qb5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709288058; 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=8s8i3ItjA9wi3+gOCrDxEDREoSwFyWHMRuQO27hhEhI=; b=hv+/9qb54HIyVq0lp2yDFueE5t5WSrDILWwqFzhir0JEJ7e70ggPj9YMmknfnSZSYR8D4d zVX/0DvmQr5MxMsKuy8Zn3FCZKpCvPhiDDiG999KooNkDC6J6glr/G6kQvrRff/4HQ0Z0x 2qfuTiRbBe93WIW2daBzbBvA5pkKJDQ= 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-629-c4D56yyKOrai0fd5slYOSg-1; Fri, 01 Mar 2024 05:14:15 -0500 X-MC-Unique: c4D56yyKOrai0fd5slYOSg-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 EDBD68B39A1; Fri, 1 Mar 2024 10:14:14 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.192.121]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8859920229D6; Fri, 1 Mar 2024 10:14:14 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 11E471801492; Fri, 1 Mar 2024 11:14:11 +0100 (CET) From: Gerd Hoffmann To: kvm@vger.kernel.org Cc: 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 3/3] kvm/svm: limit guest_phys_bits to 48 in 4-level paging mode Date: Fri, 1 Mar 2024 11:14:09 +0100 Message-ID: <20240301101410.356007-4-kraxel@redhat.com> In-Reply-To: <20240301101410.356007-1-kraxel@redhat.com> References: <20240301101410.356007-1-kraxel@redhat.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 If the host runs in 4-level paging mode NPT is restricted to 4 paging levels too. Adjust kvm_caps.guest_phys_bits accordingly. Signed-off-by: Gerd Hoffmann --- arch/x86/kvm/svm/svm.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e90b429c84f1..8c3e2e3bd468 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5229,6 +5229,11 @@ static __init int svm_hardware_setup(void) get_npt_level(), PG_LEVEL_1G); pr_info("Nested Paging %sabled\n", npt_enabled ? "en" : "dis"); + if (npt_enabled && + get_npt_level() == PT64_ROOT_4LEVEL && + kvm_caps.guest_phys_bits > 48) + kvm_caps.guest_phys_bits = 48; + /* Setup shadow_me_value and shadow_me_mask */ kvm_mmu_set_me_spte_mask(sme_me_mask, sme_me_mask);