Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp505550rwb; Thu, 12 Jan 2023 08:42:38 -0800 (PST) X-Google-Smtp-Source: AMrXdXsBQjl1ZewflFXNjHPTJRVkyCscAsnwjt8TMigb//2qVEJyAK7jmtFMAes1f6X74lxIjzfE X-Received: by 2002:a05:6402:299b:b0:499:b320:fc2d with SMTP id eq27-20020a056402299b00b00499b320fc2dmr13927576edb.33.1673541758377; Thu, 12 Jan 2023 08:42:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673541758; cv=none; d=google.com; s=arc-20160816; b=redR5kPl/2mH4/P67nr/esRtVKMFPe361D+qaFChMezwCD9yMCaCz327P0SVW/7dCm 1i/SmCK0RCtVWbOGm/B+hgixKeBlIX5UjXMqTM6Qh7ibDrguGMZiDjtap1Ktdcqpq4tl HQevJgkUsy5pizSMsLRHonf6Gbw7sLOA2THtDe5uNr4zRKdYgSwSC8SektLGaBwkHhP2 kAIAxEDGg7yRbxhuSddUfoTyspIQsVGv1+uKcFt508o9UITrx6N9C02SraQl6kO332m2 9jNFr3SI4qIQVAdbY5nxi2xi0UdMr3UJOVcpSNlc1mQSKzv9W6BJCdjoTnJoi7vE0oX6 XF+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gUSZizERejc7FDjV0cI18HPDiozisMR/tRxe7TxQpe8=; b=EugmHxx4wt2k23fJ6oWFjfcqGC6WtCmVY73pFqtm7euCU1cEqyUt8PdRGYQsP3xRgX 2Zav8lCjkBBJ26wQTAkzuNLoofrYSwpRKYZM9+7JgMs8Rci+xnP0aMJN9x8y4jZjwfPL Q23L8l2I3TTWfrKj/0+YeBFp3GVR47ATWOUQ6OAbb0HU9qtSzjnzX6grf/9BmnntXnUX qmKAP3MWwfqIoxqcoATkRRh16hIWRSlmqtEkg0clkKh09t1yTuwzgS7/Fg+60iE6m8jr aO/9nuvm1IR0IpEIAigBsB9EYD6fwTTKbI+h7JDU6bAo8Skwiy/cd35Tq1ceJBQ2t0Pf DEBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=m9C5Kj97; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r9-20020a05640251c900b0048afff70577si21443757edd.595.2023.01.12.08.42.26; Thu, 12 Jan 2023 08:42:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=m9C5Kj97; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232194AbjALQjh (ORCPT + 50 others); Thu, 12 Jan 2023 11:39:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236181AbjALQhW (ORCPT ); Thu, 12 Jan 2023 11:37:22 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 66B0711C01; Thu, 12 Jan 2023 08:33:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541223; x=1705077223; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DPQNPByrY2lPsww3gl59qroz4kG/Av+q17xVYdcZNoc=; b=m9C5Kj970z5q58smVX742UfPbZ0/1/CL8U460TQaT3PLtVlsvTn7l08N /G3SehRRXjwW69cWRYaBS5yuPPzpNlPNpqNN1qwelpOwpUeNVHjeeY+Y7 PWoeWSfK8t29qKOHVdM6h/BSGlgUjc2avXIFO3z90dnpkmYT+Tx54N6iJ 5R9Y55erDhY8Sqowg0KCA2i+Bz3OAp2mqgF8Ec7wbLneCO/kTZBdc+A/s eXdr/YXAYqXM1L8ovj64zvv4UnaBLQ7mgnLfCyjSfEveYZwfOk6C3Xi+7 FRFBRW4qtZeuycUEHS1qfZPhhsJj7yPt70uuVxWVYiW7dGLYZknw69Lur w==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="386089695" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="386089695" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:35 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="726372536" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="726372536" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:35 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack Subject: [PATCH v11 087/113] KVM: TDX: handle ept violation/misconfig exit Date: Thu, 12 Jan 2023 08:32:35 -0800 Message-Id: <83e6c6ff4cc14860678e6f1456a631d887a96526.1673539699.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Isaku Yamahata On EPT violation, call a common function, __vmx_handle_ept_violation() to trigger x86 MMU code. On EPT misconfiguration, exit to ring 3 with KVM_EXIT_UNKNOWN. because EPT misconfiguration can't happen as MMIO is trigged by TDG.VP.VMCALL. No point to set a misconfiguration value for the fast path. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 83df5673d5f2..2bea7c96536b 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1075,6 +1075,48 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, __vmx_deliver_posted_interrupt(vcpu, &tdx->pi_desc, vector); } +static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu) +{ + unsigned long exit_qual; + + if (kvm_is_private_gpa(vcpu->kvm, tdexit_gpa(vcpu))) { + /* + * Always treat SEPT violations as write faults. Ignore the + * EXIT_QUALIFICATION reported by TDX-SEAM for SEPT violations. + * TD private pages are always RWX in the SEPT tables, + * i.e. they're always mapped writable. Just as importantly, + * treating SEPT violations as write faults is necessary to + * avoid COW allocations, which will cause TDAUGPAGE failures + * due to aliasing a single HPA to multiple GPAs. + */ +#define TDX_SEPT_VIOLATION_EXIT_QUAL EPT_VIOLATION_ACC_WRITE + exit_qual = TDX_SEPT_VIOLATION_EXIT_QUAL; + } else { + exit_qual = tdexit_exit_qual(vcpu); + if (exit_qual & EPT_VIOLATION_ACC_INSTR) { + pr_warn("kvm: TDX instr fetch to shared GPA = 0x%lx @ RIP = 0x%lx\n", + tdexit_gpa(vcpu), kvm_rip_read(vcpu)); + vcpu->run->exit_reason = KVM_EXIT_EXCEPTION; + vcpu->run->ex.exception = PF_VECTOR; + vcpu->run->ex.error_code = exit_qual; + return 0; + } + } + + trace_kvm_page_fault(vcpu, tdexit_gpa(vcpu), exit_qual); + return __vmx_handle_ept_violation(vcpu, tdexit_gpa(vcpu), exit_qual); +} + +static int tdx_handle_ept_misconfig(struct kvm_vcpu *vcpu) +{ + WARN_ON_ONCE(1); + + vcpu->run->exit_reason = KVM_EXIT_UNKNOWN; + vcpu->run->hw.hardware_exit_reason = EXIT_REASON_EPT_MISCONFIG; + + return 0; +} + int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) { union tdx_exit_reason exit_reason = to_tdx(vcpu)->exit_reason; @@ -1119,6 +1161,10 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) WARN_ON_ONCE(fastpath != EXIT_FASTPATH_NONE); switch (exit_reason.basic) { + case EXIT_REASON_EPT_VIOLATION: + return tdx_handle_ept_violation(vcpu); + case EXIT_REASON_EPT_MISCONFIG: + return tdx_handle_ept_misconfig(vcpu); case EXIT_REASON_OTHER_SMI: /* * If reach here, it's not a Machine Check System Management -- 2.25.1