Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4118705rwb; Sun, 7 Aug 2022 15:35:03 -0700 (PDT) X-Google-Smtp-Source: AA6agR4XG8At0FiAZ6ygyIFDV54BOd9ApKx3MQzxv92nvW7V2luPrBANwZDH62CBStXkMFLMMNqL X-Received: by 2002:a17:90b:3850:b0:1f5:609a:3ac6 with SMTP id nl16-20020a17090b385000b001f5609a3ac6mr19667260pjb.125.1659911703687; Sun, 07 Aug 2022 15:35:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659911703; cv=none; d=google.com; s=arc-20160816; b=IrU55SgIV0DSO6M8kLOC2PcsDmaZLLp0vlDdkbEf61cfGlX+pXK7y3MoF7fj1D0n0D ltoowrOz+h94Nuj5+IPZVX2PW3YbplEsbht9yqgn6k7XRtwZgKTYpNvSRzuy7djMbxat avA5KEMfo9LoyC7PGcuKoalsMCId9/6E3X6eJbFM+EsUflfbWf3sYkHpNoEDN5H+Xnln r/LobkKrVj0d1TwVPKewcShlrnh+DAk18v3WWZIYOqNdiE4OOOcNKfOWjHiQt0fnVMje vLswvldAyj/WlJyg2B4s+cRV70enaGL9eoQfIu50IG5NVe3hokgQ7lrmmIcSKG2yTnI8 PPtw== 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=qMgO7DwQK/vqdoD3xPgvC4H5uzEnYdUkTHxQUyWJsmk=; b=VgWe08RRjoMp8xqcae/0+YH8vSN1geDrHBNHuGfKaFaHFozojyx/MX0Fyc524S1m6+ cXXxb9qc6kBtI2w0gmTjcmjvmWksgQaBQKlRwyXTrqmzgog+KfQtdo68i+0OGa2HXJSn 2ou928kKhCjyoX37WcD1WZRFO0E2OTUt7vP+k3pdbCDr2Pugl/kQ4feuH+17x4nbdJ9M AwtRfRKm8oLvqfvB7S3mNDiWfFxSzXARNu4ORwswQkoz89bjBLj95b00w7csJtdt3e1y wswM4T7u4RDHBOI2dmU7/PsgXN/z6/L+Dd/RLn+JQeVTgWh11E396gM3PkAMnlFc5eAZ Fmow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XnxNepwz; 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 lb15-20020a17090b4a4f00b001f244c4f9b3si12105447pjb.111.2022.08.07.15.34.50; Sun, 07 Aug 2022 15:35:03 -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=XnxNepwz; 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 S242472AbiHGWLO (ORCPT + 99 others); Sun, 7 Aug 2022 18:11:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237133AbiHGWHU (ORCPT ); Sun, 7 Aug 2022 18:07:20 -0400 Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EEF9BC80; Sun, 7 Aug 2022 15:03:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659909801; x=1691445801; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rSbqV/Q88ZHvumnSiQpg6Nqx4jLIYwqcOPb1LpLI8/c=; b=XnxNepwzs9deNzMLD7asnu5VSts5Cvpn29jjSpuhOlbHFXN973E2JnwG 51frXlwaw0Y4CqYMqn1jgHsX5FOXKfqV3HnbZWvfKOaQQGLu8YxebHGYR 5m9F5WsPQK9VcqwflawQGCSlqamPscpjKDtvOV2wZoARFf3wSZSDJbgK9 CpZZb/0eTViHwj0P0ps/GcsQxNiZwcGqe0o03rPI2aowPjapRWKTRiQMg mP/MvzBUHtRFdbe/n1tjza6ZaXylgK5viNkTNKMqe6UUqaGn7888HKdEf 338CRPGvXjwo95Gxq6gvRk2imxA3u/aYgVBAec+uJ1r8ENgy/7K5GCFk4 g==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="270240404" X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="270240404" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:43 -0700 X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="663682743" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:43 -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 Subject: [PATCH v8 091/103] KVM: TDX: Handle TDX PV HLT hypercall Date: Sun, 7 Aug 2022 15:02:16 -0700 Message-Id: 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=-5.0 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_PASS,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 93c4ef209a73..aa3b569318bb 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -641,7 +641,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) @@ -954,6 +979,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)) @@ -962,6 +998,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; } @@ -1304,6 +1342,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 15010358f6ab..8284cce0d385 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -115,6 +115,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