Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp285627rwb; Tue, 25 Jul 2023 16:05:43 -0700 (PDT) X-Google-Smtp-Source: APBJJlGhbJFswmTLEKFH2WhVxkdr3lCf5hUOcqouKZSwTtaDGYPiMQG3pYhXBeRO9SWUbcNfql5L X-Received: by 2002:a17:907:1def:b0:994:54fd:31aa with SMTP id og47-20020a1709071def00b0099454fd31aamr169120ejc.15.1690326342887; Tue, 25 Jul 2023 16:05:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690326342; cv=none; d=google.com; s=arc-20160816; b=Ne3skjMYMNHU6UmG/YWEJahE/JuLdPwRzMfD4v8o7taSJNnDmbd2jBZoQDRxRr3cmu X0HGlsg7sf055lz/5StuY7l5iloyrN4BTB8bymEDGmsfwtcapQWCClhCCxcLW9swTM7S t98FggcBBvOOx/LIDU4yy9xC7cRGtv7Pfks+2qeXZB5UFKcTx7LZaP0cbKuz/ICwZQcN riTZIO755iTml9RUiLfUklDZOV7L6PAEYN900uRInz5qQfE9QNNJJgW5VObozqSEU7DD xnj66cDLg4nSRAN/rjA4hhANppF9EcaKGzm4Zr4q2P2T5lnGc0XyUqop8wjGyhzN3lue jTGw== 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=kXocbP/RDgLAC7j+b2dSBevFxw8IYbJ558z0i/dzH8g=; fh=juxwNcA6iKwLISFgUCNwdoIYC0NKLUseq3xZdq25RR4=; b=uU6sBOt2/VlQCj1B3yd8hnc9aGqPKd1XV8VvCNB+ZxJrFWsA8Rx8koejLF9pOR9k65 xzEd0txVEGU8OZckuAVJyqIJIB2vL2rcht6LuZXebe6/4wMQSNgY0ylt5yptJR+mhUM7 snQZ03aAVY2n/sgOG9jRGgz/wVu3FjUN0AKnekE3mBSp241dAvMGT96La6ee1sB8CzNa Tm7paMyve9RRYrzxKaCUjwp8lrBN5y0J81gdtK49oCXUeMZlYRp6MF7l3N5N1HyhYn8B s3H7Xtu8ESWZKqwceP40vVcRatOZkDPPBFyI8EKLy5nFNLkI8oX6o13Rl5PxqBf9RYuD ectA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AgC59eOS; 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 v1-20020a1709064e8100b00993689daad1si8952032eju.116.2023.07.25.16.05.16; Tue, 25 Jul 2023 16:05:42 -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=AgC59eOS; 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 S233004AbjGYWZF (ORCPT + 99 others); Tue, 25 Jul 2023 18:25:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232903AbjGYWXI (ORCPT ); Tue, 25 Jul 2023 18:23:08 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33D62525F; Tue, 25 Jul 2023 15:18:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323502; x=1721859502; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zAvts/lhrxcaq+89MhHSSlnL5ZiM8bhS++C5LhP2iVo=; b=AgC59eOS5/vPeYHRv2V5CC9bxLaFdF1NQOJPm+5jgmQDxLuXU76IeCzc yaVs4/RMfkbXP5tZXMreZiJEIZG8cmeFdqXvp0LzLNBvOeKSwp+CF0ZgR WPmhdrJ0Zvk5uUkgo4IlCPe6o1PWK47Es1dSxiPHrJFD1z4Vgcd29UXQS HiJtQPk6Kd3+HluN/tg76m6gfCOpesx89/nBCHJ6DE0dg7/10+e3OryZZ HWFCyXulPb43RKcy+XsY/0Phkfx5q3G3irEQRCiCZ67HXEycox5//hxUD EVjRzf34Njqft9voSFDulzVFTb0MOLEgOqKd+58SqLP/F3eDgUWcWHCs8 Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="367882705" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="367882705" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:16:03 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="840001899" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="840001899" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:16:03 -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 v15 093/115] KVM: TDX: Handle TDX PV HLT hypercall Date: Tue, 25 Jul 2023 15:14:44 -0700 Message-Id: <5b1c274d0a8d672a439e0c380e9d8965e711488f.1690322424.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,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 9760d592bc68..d3ea368db5c6 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -622,7 +622,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) @@ -1009,6 +1034,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)) @@ -1017,6 +1053,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; } @@ -1360,6 +1398,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 e03f7192dfab..c0cc09cb77ba 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -101,6 +101,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