Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp558813rwb; Thu, 12 Jan 2023 09:18:13 -0800 (PST) X-Google-Smtp-Source: AMrXdXsRioMjU9POcl9LLGEi8xd/GmKLFVgeumT49+y3T2jGSvLJNQiAusV3zZYWk+4LKTxZ+1oS X-Received: by 2002:a17:907:180c:b0:7c1:67ca:56f5 with SMTP id lg12-20020a170907180c00b007c167ca56f5mr210121ejc.15.1673543893092; Thu, 12 Jan 2023 09:18:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673543893; cv=none; d=google.com; s=arc-20160816; b=t+FlFaIUz9kz9arUdkwpQwqJiPitCRnbpWAeOq/aIVyODMGWChR165qmw99b+Efam8 qu7W6nz9lnETzg/O4F45Zt9uAIAazi9TDgIXQQN7SmCM9AESjMAuVoel/tW/Q5Us3mHG qkXc25/cVlfvDUBK9ZjUj5E2mlH2CPHXsMGmV32k3gM6LZrJrKu5cGY1Rqzdf2knvMRg YvfEn21JBEI1sYSky3d+LtcA2bR1OTix8sBwBmQMZKwGNPt0LYrhBHABHkTN0YNYJqgg rKblde8P4dUHgfDjXf3n42Pr/v21r6dTPlDFpZwCyUMMV6yWDkMaPuqtYNWi8d/34uv9 Ne+Q== 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=E7RZzlL3jQAarjo1f1Y0RdpEgzDba15fTA6ccvg5OW8=; b=KtWGeUbAyxmaKOdta6h4AyDK7TNgzYVfikLWNUXW047xC9zDuxgLbLNmTS+HbzTVxS hnFAZUdlsN7Oz0NxDpxkUK7vc76pSkg+WklQQujRcvKcg+hL/z1S6N8tRIlaj7EgYHRZ EHSR8EA4fAGocslULj3ygfwhVj8QJQFB/k7cCSAH0JVGV5tQeRa48zbVnbYvvVU6U6t7 n+t5VLxyCxHf3WYfNgB+xZ1V376ADo8bKr6ru/WyNQfYd4sh6Y/vkLR8RoLnUP145sJd oENCip/3iG543AyPEOcA4jYOn1hL3L7hSE4At+6K/wPO1SX2l85RmUoptxfvM6FDkqj0 +KVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HgcXasWt; 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 xc2-20020a170907074200b00780636887fesi19129112ejb.797.2023.01.12.09.18.00; Thu, 12 Jan 2023 09:18:13 -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=HgcXasWt; 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 S240494AbjALQkK (ORCPT + 52 others); Thu, 12 Jan 2023 11:40:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231433AbjALQhZ (ORCPT ); Thu, 12 Jan 2023 11:37:25 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4989BD2E7; Thu, 12 Jan 2023 08:33:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541225; x=1705077225; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ry2mM8Gx+J1Y5WAEaUxrJuOu6ddVbWqrX7U7oiz143U=; b=HgcXasWtd6/7JxpI51UgnIxxJphm+1giw2VA2PPn/snJ65EEeTLftRWZ U43EycTlSkPf1QWiCQSWaEkbYVe25KSPw1/EuAXC/WkM4QJ6oX3+LKtfQ FvNMwrlfDpDyyUnYtvF6Q5GqaaoIbF4UcBIZLCFFv6+MPUMN+jQEqynXg aSUZXmiuDpdnZMvP+AYNJH0k3/W1oNIC4ukLjsKVwfV77EHOfgWz23o/3 B7796lID8oybRivOXASQnxmjwYSD7DHvbCf3rkHujWRW/Rh+dWhdkBNXK M8DoTAytzPxKIn+iDTeaGkHg84O+HtRYHDu5NNTZFcUGTb2iX5s3v3brP g==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="386089724" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="386089724" 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:36 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="726372561" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="726372561" 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:36 -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 093/113] KVM: TDX: Handle TDX PV HLT hypercall Date: Thu, 12 Jan 2023 08:32:41 -0800 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=-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 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 6419cb741996..3dcfbdd37579 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -588,7 +588,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) @@ -943,6 +968,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)) @@ -951,6 +987,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; } @@ -1284,6 +1322,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 272980d9605c..01e97d6886d5 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -93,6 +93,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