Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3419814iog; Mon, 27 Jun 2022 16:00:45 -0700 (PDT) X-Google-Smtp-Source: AGRyM1utoJs8n5dHpOpI/+v0upETx1SYXPU6f7kdDFrkLVvlJ917ER8Y2Dvy+c+c1Euiv63IldwQ X-Received: by 2002:a50:cb8b:0:b0:435:68a3:4d40 with SMTP id k11-20020a50cb8b000000b0043568a34d40mr19997567edi.394.1656370845560; Mon, 27 Jun 2022 16:00:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656370845; cv=none; d=google.com; s=arc-20160816; b=SZQ1mPHb9kI8UFqOg44YMbZFu+UIrlEbt139Lcn9xYZLEz3BFR7P2LYIJUofg6H0Xi FlyFb+S7phjGlLjJlsZz6lIYiwWbfvBHoxewuE77ur61FSf4tAUl7RSjGNczqgbcLFzI dMQ9tuct8A79nvSZRLcwMnFi9ShqU+Ox86KlSeoN1dGbvV3R9t8TOygRvgwlIdySGy/w 8tBhyAjuxDES0x4+QTHb2VQ7AF2WjGfvJIHUhSfUL4+VtJ454W+bH/tOEpOKLXF6sep/ IY81LsZ21RL5oZgImDYtjD5tjDj1A1zHV8+bxNfAux4hqEoT03lmNYudSX+LTzGSbIO+ AYsw== 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=Xqr6kRguVthCEHrv1kUzx5rFpDqQ0A3Sh+Hop6EkOiQ=; b=YfEiChFpBSMP3yJTffpvzJxNFPsGKdqP6sTpLFHxqZFfr8y6DVj5fFEqK530McFdmR 9s5Z3Bv6948K8AFmZZha3AKWEn271esuEqS5b0PRbgXqIa1fTEt5Q/xIrSeKOI0+mDAI WvoXIZgDqd4aHBFABzCd42r47kmgb8S3voXzuRp/+rs86s36fw86M1V48+8twqsjzeLZ wF2blu7+cktF7oJyJoS0W2+tqXOuDf1/dK/Z0BtBcTbFguCfvIEOhZzZ3rpQk7oswvSQ 8Wq2JA2bgvzG/+L6rkKXoEpJ5HNZwF+R/x3AhkWmKDUgiAyD4BuSX1Qxvzgt0LdZhkVD EBrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=KopfRjWi; 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 hd41-20020a17090796a900b00726be5b3befsi3029136ejc.156.2022.06.27.16.00.20; Mon, 27 Jun 2022 16:00:45 -0700 (PDT) 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=KopfRjWi; 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 S242407AbiF0WBr (ORCPT + 99 others); Mon, 27 Jun 2022 18:01:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241278AbiF0Vzy (ORCPT ); Mon, 27 Jun 2022 17:55:54 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F25A15739; Mon, 27 Jun 2022 14:55:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656366908; x=1687902908; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nvOKhWUwY4ixdp7Mo14HTOcmCjwpZM07BwLwUn5o6l0=; b=KopfRjWil1HZmbSSjU8CcF2PO5dc3lqiZSQTMDWVg7490QcaQBkl0NIK HkItUGxx7Lbhf/nNbeI0SY9jgBuZYLjyp/mGlO3LEfwhv23K9C6aNflD/ jPzK442KAlSY8kS3qie93YAHWerSJIxCVSEZNV70K3GTUg3O5mwcUl13T QRZyZs2Vko4QzzqkqcU61fnldnd3XL13HxPOMOJXN/ggEx1Yu+PR6HSK3 NakDOsii4yhgh0PYNNtWrGzg0N3/Jyu3J9vaEkV47E4GIWo+CQG4jfosE Gz49ASU0795JGCJErGM7a62U+UXzP6ohzx89T/8NkIiXj9YQpqd2aDG06 Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10391"; a="279116140" X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="279116140" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:55:01 -0700 X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="657863735" 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; 27 Jun 2022 14:55: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 Subject: [PATCH v7 090/102] KVM: TDX: Handle TDX PV HLT hypercall Date: Mon, 27 Jun 2022 14:54:22 -0700 Message-Id: <57238c84c7956ac8003692474a584233cc7c5eab.1656366338.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=-7.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Wire up TDX PV HLT hypercall to the KVM backend function. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 42 +++++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 3 +++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 96e41602125b..15dc0ae61e0f 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -654,7 +654,32 @@ void tdx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) bool tdx_protected_apic_has_interrupt(struct kvm_vcpu *vcpu) { - return pi_has_pending_interrupt(vcpu); + bool ret = pi_has_pending_interrupt(vcpu); + struct vcpu_tdx *tdx = to_tdx(vcpu); + + if (ret || vcpu->arch.mp_state != KVM_MP_STATE_HALTED) + return true; + + if (tdx->interrupt_disabled_hlt) + return false; + + /* + * This is for the case where the virtual interrupt is recognized, + * i.e. set in vmcs.RVI, between the STI and "HLT". KVM doesn't have + * access to RVI and the interrupt is no longer in the PID (because it + * was "recognized". It doesn't get delivered in the guest because the + * TDCALL completes before interrupts are enabled. + * + * TDX modules sets RVI while in an STI interrupt shadow. + * - TDExit(typically TDG.VP.VMCALL) from the guest to TDX module. + * The interrupt shadow at this point is gone. + * - It knows that there is an interrupt that can be delivered + * (RVI > PPR && EFLAGS.IF=1, the other conditions of 29.2.2 don't + * matter) + * - It forwards the TDExit nevertheless, to a clueless hypervisor that + * has no way to glean either RVI or PPR. + */ + return !!xchg(&tdx->buggy_hlt_workaround, 0); } void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) @@ -967,6 +992,17 @@ static int tdx_emulate_cpuid(struct kvm_vcpu *vcpu) return 1; } +static int tdx_emulate_hlt(struct kvm_vcpu *vcpu) +{ + struct vcpu_tdx *tdx = to_tdx(vcpu); + + /* See tdx_protected_apic_has_interrupt() to avoid heavy seamcall */ + tdx->interrupt_disabled_hlt = tdvmcall_a0_read(vcpu);; + + tdvmcall_set_return_code(vcpu, TDG_VP_VMCALL_SUCCESS); + return kvm_emulate_halt_noskip(vcpu); +} + static int handle_tdvmcall(struct kvm_vcpu *vcpu) { if (tdvmcall_exit_type(vcpu)) @@ -975,6 +1011,8 @@ static int handle_tdvmcall(struct kvm_vcpu *vcpu) switch (tdvmcall_leaf(vcpu)) { case EXIT_REASON_CPUID: return tdx_emulate_cpuid(vcpu); + case EXIT_REASON_HLT: + return tdx_emulate_hlt(vcpu); default: break; } @@ -1311,6 +1349,8 @@ void tdx_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode, struct kvm_vcpu *vcpu = apic->vcpu; struct vcpu_tdx *tdx = to_tdx(vcpu); + /* See comment in tdx_protected_apic_has_interrupt(). */ + tdx->buggy_hlt_workaround = 1; /* TDX supports only posted interrupt. No lapic emulation. */ __vmx_deliver_posted_interrupt(vcpu, &tdx->pi_desc, vector); } diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index b0bb239b51bf..a456ca6ec187 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -116,6 +116,9 @@ struct vcpu_tdx { bool host_state_need_restore; u64 msr_host_kernel_gs_base; + bool interrupt_disabled_hlt; + unsigned int buggy_hlt_workaround; + /* * Dummy to make pmu_intel not corrupt memory. * TODO: Support PMU for TDX. Future work. -- 2.25.1