Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2240788rbb; Tue, 27 Feb 2024 15:57:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXruis6w2RC/namXdRdGU0SgBNCz4jTWemxLgVYAo2G28N05XukBcI8PGwTzsd3x/GkKHYl/3r2KOQFtQnVpxuPo6GBn67k2NuMIaHgwg== X-Google-Smtp-Source: AGHT+IGRM16feJnzqrkP5EZIQi3uQsPpaGz7oUQqrVI260yGY3JG0lXbnt1fSREW3ExIBUGOJZvM X-Received: by 2002:a05:6a21:1743:b0:1a0:dfdf:2b16 with SMTP id nw3-20020a056a21174300b001a0dfdf2b16mr4000558pzb.43.1709078262937; Tue, 27 Feb 2024 15:57:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709078262; cv=pass; d=google.com; s=arc-20160816; b=VjfYqXErwDHOlbtFfTW6H9HdkqgAFO/kFuTx+GdAawJ7Y8e5RCxT5A55BgGYu4OCIO em3q9A3xZ1v7KWtyCJMeoO8X6EJKIqr64qm2o8owSJy+UWipCIoaNug8C5QSfIQ1Tiw2 LIdJHc5nRFt82QFTISCYlUmRIXtNchtp7gJSCd7hqxATUoH3VeIsF4J8HhpLCZfg7rj7 RdoUQl3sSn8G1JoOnyUxxbAbMHiXScSTGFYr9pzn+6uqn9mPzfCD1dxHxVHaBzgs8zOp yw5O2R/9aUi50X0lFNQKclSTjv75Gc9sYHY6C4zDznCLY/ViXbe+hN0z4X5l7Mw+BXcn z0/Q== 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=ItA4hweoRhWLy+GIQ/7oXHP94HCpIoNmoXorWKs0+NI=; fh=miuXyzfw5j8qsOURKCmYrSiXirUlKyDkR37pPJ+5+/s=; b=xK2Nadb+35IHgpG1Ccvaf00AWLqqA68hW86mLlmodtZ/cUvyOWXuasa2w5tF37r+lw Q1zmwT2lWCeXefd9SOVpLa1DX4SbfN4/DETWjRkcf/ezl2TvCH8lclaKSZvE0TWn4n8P Ezndq4HtDGHyRVsjh6gcslpFMqHdpagkO7POPj0np9aKExeylsYLPIRakTWgvObkpT5G qFGVW7uD7IdCAgyv9w+muIof20uTSiGryESEEgCMqdUbnbLT8rHBKSS6Off9RHRsBXCa qGP/q7Nzjrp3UHvDGW9Q/bhYqMOA0GNHFtwmhtPv0lwg9F3AKuf00usXQwEmSTl7dRwB SeXg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ww8BEP2F; 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-84209-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84209-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id j13-20020a63594d000000b005dcd64e8467si6162975pgm.59.2024.02.27.15.57.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 15:57:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-84209-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ww8BEP2F; 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-84209-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-84209-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 9CCFFB30A87 for ; Tue, 27 Feb 2024 23:27:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EF7FB5F486; Tue, 27 Feb 2024 23:21:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Ww8BEP2F" 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 737C858ACB for ; Tue, 27 Feb 2024 23:21:12 +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=1709076074; cv=none; b=H8uQYsAgCl7WdTPLcnBui7s7A1dAQk1iNWHvd5Jf2XmC4Pglx0WTN3m6Te677J5GoelQDc9plApVObvmdZLKTb7fp8S5hiA9IpGot5VSF6MLHR1X9nJqo8Tw+a9mDE5wz+08Wdio5rCqW4ckJHzv13vI9xoWCxsSNxWPZu3PG3s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709076074; c=relaxed/simple; bh=kAGvldVSSgs/cuGA2UhZHAoRzsM3NNWHqjWVGuiXJcU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=A172QhQKXwx3Jb3vNpdFhI40kw2kOCx4wocNurD+g/xIY4kwv9gC1B7epC62gc0TBMo2ByW3YpT4j4+9S1ALgS0gmV9zQnRXGhC5irag8Uq69HvjdPqPE2wYY6YCI3NqY2jyvjrHOc31/dRhvAVnFP5eKVZ7BrtdkYx9HSuP5zY= 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=Ww8BEP2F; 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=1709076071; 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: in-reply-to:in-reply-to:references:references; bh=ItA4hweoRhWLy+GIQ/7oXHP94HCpIoNmoXorWKs0+NI=; b=Ww8BEP2FciCV76Bk0uT/M71Iy5AlMvauX1wG+XHZbduVUqZsUyE2P6PyzdJU4QlwZ83Z+P /pFHKWuSl9e5KADxAOfhDAzRlQVJDA7CNVDGqKUzLzVdR2xm+xY/UlYZs0XEV6w41sdiSW c5FUelE+JcANPWjojMXAIR5om7f0tbI= 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-169-tuhU7xeHM2uf3DRJImU8EQ-1; Tue, 27 Feb 2024 18:21:04 -0500 X-MC-Unique: tuhU7xeHM2uf3DRJImU8EQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 65E90800074; Tue, 27 Feb 2024 23:21:04 +0000 (UTC) Received: from virtlab511.virt.lab.eng.bos.redhat.com (virtlab511.virt.lab.eng.bos.redhat.com [10.19.152.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 395F3C0348F; Tue, 27 Feb 2024 23:21:04 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, michael.roth@amd.com, isaku.yamahata@intel.com, thomas.lendacky@amd.com Subject: [PATCH 15/21] KVM: x86/mmu: Use PFERR_GUEST_ENC_MASK to indicate fault is private Date: Tue, 27 Feb 2024 18:20:54 -0500 Message-Id: <20240227232100.478238-16-pbonzini@redhat.com> In-Reply-To: <20240227232100.478238-1-pbonzini@redhat.com> References: <20240227232100.478238-1-pbonzini@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 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 SEV-SNP defines PFERR_GUEST_ENC_MASK (bit 34) in page-fault error bits to represent the guest page is encrypted. Use the bit to designate that the page fault is private and that it requires looking up memory attributes. The vendor kvm page fault handler should set PFERR_GUEST_ENC_MASK bit based on their fault information. It may or may not use the hardware value directly or parse the hardware value to set the bit. Based on a patch by Isaku Yamahata. Signed-off-by: Paolo Bonzini --- Compared to what is in the Intel TDX tree, I am dropping the if (vcpu->kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM) return RET_PF_RETRY; change in __kvm_faultin_pfn(). It is not well documented why it is needed and selftests seem to pass. Also, checking has_private_mem is needed so as not to break SEV-ES. arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 9 +++++++++ arch/x86/kvm/mmu/mmu_internal.h | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 24e30ca2ca8f..7de8a3f2a118 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -264,6 +264,7 @@ enum x86_intercept_stage; #define PFERR_SGX_BIT 15 #define PFERR_GUEST_FINAL_BIT 32 #define PFERR_GUEST_PAGE_BIT 33 +#define PFERR_GUEST_ENC_BIT 34 #define PFERR_IMPLICIT_ACCESS_BIT 48 #define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT) @@ -275,6 +276,7 @@ enum x86_intercept_stage; #define PFERR_SGX_MASK BIT(PFERR_SGX_BIT) #define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT) #define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_ENC_MASK BIT_ULL(PFERR_GUEST_ENC_BIT) #define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c9890e5b6e4c..6b4cb71668df 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5846,6 +5846,7 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err { int r, emulation_type = EMULTYPE_PF; bool direct = vcpu->arch.mmu->root_role.direct; + struct kvm *kvm = vcpu->kvm; /* * IMPLICIT_ACCESS is a KVM-defined flag used to correctly perform SMAP @@ -5861,6 +5862,14 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err if (WARN_ON_ONCE(!VALID_PAGE(vcpu->arch.mmu->root.hpa))) return RET_PF_RETRY; + /* + * There is no vendor code that can set PFERR_GUEST_ENC_MASK for + * software-protected VMs. Compute it here. + */ + if (kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM && + kvm_mem_is_private(kvm, cr2_or_gpa >> PAGE_SHIFT)) + error_code |= PFERR_GUEST_ENC_MASK; + r = RET_PF_INVALID; if (unlikely(error_code & PFERR_RSVD_MASK)) { r = handle_mmio_page_fault(vcpu, cr2_or_gpa, direct); diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h index 21f55e8b4dc6..154aa44eeb33 100644 --- a/arch/x86/kvm/mmu/mmu_internal.h +++ b/arch/x86/kvm/mmu/mmu_internal.h @@ -290,6 +290,7 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .present = err & PFERR_PRESENT_MASK, .rsvd = err & PFERR_RSVD_MASK, .user = err & PFERR_USER_MASK, + .is_private = vcpu->kvm->arch.has_private_mem && (err & PFERR_GUEST_ENC_MASK), .prefetch = prefetch, .is_tdp = likely(vcpu->arch.mmu->page_fault == kvm_tdp_page_fault), .nx_huge_page_workaround_enabled = @@ -298,7 +299,6 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, .max_level = KVM_MAX_HUGEPAGE_LEVEL, .req_level = PG_LEVEL_4K, .goal_level = PG_LEVEL_4K, - .is_private = kvm_mem_is_private(vcpu->kvm, cr2_or_gpa >> PAGE_SHIFT), }; int r; -- 2.39.0