Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2622892rdg; Mon, 16 Oct 2023 09:39:52 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG+CfbwdkqhMagFF7Z6jkQEYse185mc6T09f68T4P3oohFzQm7TMz8re6POLqrU5LT3jBlO X-Received: by 2002:a05:6808:14d5:b0:3ae:bae2:fa76 with SMTP id f21-20020a05680814d500b003aebae2fa76mr44088656oiw.36.1697474391717; Mon, 16 Oct 2023 09:39:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697474391; cv=none; d=google.com; s=arc-20160816; b=Eud24Epv7tVYqRvb5+YFCu3cP6vP6IC8wld/OZP6bUXh3UHLpvuLSradlWBs+daofD g7YFmsxfrRKZHcbFjLdPyVToE98D8cHzp1Of7aFLI5IlNEz2wYA9HnQ8+zuMG/Nsr73p UZYOCeYQnwUFNZ5ORJ8lZYB70UaaGULa8tWeYf9g8OIvpV1UV39Gkm7Xx5mIg+U57uQ+ DxbMSNTGAq8eftVWsS/lJFJD8auI70kLHNGV2ulU8mgRJaocaqFgHwnwxiBznKaA/k7l jTWQru65j0RpMCvt8vvjcBwcm5Nf1Ex64Ky5YpOn9wM9iH2xq8PHSjNvmANwFrJE6rmk +0Cw== 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=b8jI+4wgvR/GtKovt0WsZ4Odzqx9qO1puDbxNwpraSQ=; fh=lRdU2Q/1zx5DcPdZuWBjshA5VT5Oc9cEhB1tCFiV0Nw=; b=QLzAZbPp9E3+i9bR0FWlDznX7aq6PSbjWJFgWD5mvUx7nEdT5ivjLkO7HQbVps8o1D PHpoCUOkxY8ZEYJS/813ZE95CrQ06k/Ie3xJpgXJfKV5qgbq/8byZ8QSFFXIMm2wk2py xMfTPCJ2zFLycgmS2SUNCe+ib6U/JcdkskfVjNXTp5rzZoRoz1inqqZW7kKLwdKh6QW5 bHbJGDUItSv4R3hP03rP2xAAfN94hf5wVxZAUkrgYBzVZwuudjCQ5U3eZJBhtleaa0fz TnzigxtiXUDtEEG+ou9S3Pwdntcr8oyHuBeHxocAh7tpjT1bQ/izi+qYfk5nHhgC39Wt 6nbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dXUyJ2o5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id w17-20020a056a0014d100b00690cc6f7598si165468pfu.247.2023.10.16.09.39.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:39:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dXUyJ2o5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 70269807B9AC; Mon, 16 Oct 2023 09:39:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234452AbjJPQik (ORCPT + 99 others); Mon, 16 Oct 2023 12:38:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234182AbjJPQgy (ORCPT ); Mon, 16 Oct 2023 12:36:54 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A146E468B; Mon, 16 Oct 2023 09:20:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473207; x=1729009207; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=06o7fT7NZNVcS6oNOxRJJ2NUP2CsC3WWbwRpBqTIQ4g=; b=dXUyJ2o5D53alGXMG15YC7qNSp3gtv77RN8svjkElLo9QNvZlRZP9eH4 GTNR65KQ8Ov+r4H6Wutio+nFnreSkHxJMlMqh4ECad/9aRFd0/D0LhhYu BOzDbZ4UBF3GnA3HBDsi2Y/U4yKRv3vM36e6l28RKXA17kWQ4TgmUr0MV wjBI/m6EaRw/Lodtpf5I1H7RYcHLLgzi7uOFbEDvOYqkvKhC+U9Zxp8t6 aGFyJXzqpJdobgAKLRChpUcOWjzhHaicpFiLb50xc3fYb1I4LPlK8Lcfh W3U+oTvahTFzBM08HNUqka60GPf/f6/sWS8vVgMKbdaUVRuku9gJZDEDC A==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364922005" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364922005" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:16:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="846448270" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="846448270" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:16:01 -0700 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 , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v16 085/116] KVM: TDX: handle ept violation/misconfig exit Date: Mon, 16 Oct 2023 09:14:37 -0700 Message-Id: <7c0c1bf160913eec9ae2f0c5fe32f67d49be34a2.1697471314.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=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:39:34 -0700 (PDT) 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 --- v14 -> v15: - use PFERR_GUEST_ENC_MASK to tell the fault is private --- arch/x86/kvm/vmx/common.h | 3 +++ arch/x86/kvm/vmx/tdx.c | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/arch/x86/kvm/vmx/common.h b/arch/x86/kvm/vmx/common.h index 632af7a76d0a..027aa4175d2c 100644 --- a/arch/x86/kvm/vmx/common.h +++ b/arch/x86/kvm/vmx/common.h @@ -87,6 +87,9 @@ static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa, error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) != 0 ? PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK; + if (kvm_is_private_gpa(vcpu->kvm, gpa)) + error_code |= PFERR_GUEST_ENC_MASK; + return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0); } diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 618c2ecec26a..f98b27cb9003 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1279,6 +1279,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; @@ -1339,6 +1381,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