Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp176537rdb; Mon, 22 Jan 2024 16:30:35 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6qhERKfnLyFyTSUyBi7y7l3XuaNsTM9Dlo4XVoiyiDsiiSdWHMwK8Sj43kIONYzyto6n8 X-Received: by 2002:a05:6e02:11a8:b0:361:acb4:3f85 with SMTP id 8-20020a056e0211a800b00361acb43f85mr6737421ilj.63.1705969835078; Mon, 22 Jan 2024 16:30:35 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705969835; cv=pass; d=google.com; s=arc-20160816; b=MBJKZkkcDBh/ANtvz285NsxKZvpi+tWO+6HWXldlwz6NmxzFU4LFTWwH3iEVBvK06L po5jMqi7FDPtGUUWnhSHRFwIbTB2+BecbEbqR5qjznIlzaCesYpAt9X8CzJJ2Qo7E8fF gn4HdAFTW+WnTgaJKsCMh3vrH5H5r3AkydOQYf/tHkND3Q1dqV8n1prVHjjVInZLqKhz RLTcK9THkuVLmAWfwE63EjYIgwjHGxK0OtXyIf1DQiiyWuVogQDHowsHRSKhpBwakTlR 51ToPPFuxxj5HpbLIDawsj7jCMEalR3pCOvIEPPxGDXIJx5bgFcYJcCU1iTTxnLYcU9f 1sYg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=J8puvmJyCdCBJdUM+LYosbrG/Jff6sTHGycq2GPOA9Y=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=hIX7Jq/4XTH5rA8zs8RcWo8SJLpkZy/6PD2yXUqxCpMj+RALDRyHxNJfvIKfkox5kz aQ/OYCEIi7y27gRFFDY49NByWBmkeX61jIgz2CJqJcPzc3F2cXwC2e1CNlHLGwWJhL1e wbReK6TDVsMQraEHAR0r5GdhPhjQnwqxGj+N7cLkwQPQnFYtPWa2MyKlig57JF0WeDiR kmUeJOV0cM0vFwNkUuWbS7WBv4Mgn1QgLJ/dII+DzEIFlkxyxhFaWXd9kePqCmDrDNUe WqfUAGUQqnnwcBE4w9GkWfXUfpERjCXsfjU5Uvq5RkSInEhFAL8Aa44I985JjGSlK8I1 +PBQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gGt7wZox; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34406-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34406-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id x27-20020a634a1b000000b005b95ee3edc6si8976184pga.628.2024.01.22.16.30.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 16:30:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34406-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gGt7wZox; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34406-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34406-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 62F1DB25FA4 for ; Tue, 23 Jan 2024 00:15:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 09F8777633; Mon, 22 Jan 2024 23:56:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="gGt7wZox" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C85056DD01; Mon, 22 Jan 2024 23:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.120 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967764; cv=none; b=O/Gk97tmmrEzQ+i97ndbtR7cX9Tr9qhgoRKHjV5d1UM8x0BgHdUgzBde9/VgrjSxG2EkdMr7BvR3BkcAvzdPHQEa+PkohvuG4NNC6Vp0953TywX+kCxF5RZLq6WMiO3ArIt71lzloSuha2JyqKQ0R6W1Sz/SH9yK09rrHSPSPRk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967764; c=relaxed/simple; bh=ecW1HuB58giFSejI1GX+Wd1u8QBXoYN9OtMr5VLrR8c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rtR4pDS7Jjl+ejdF8oUOFTkXGwsuAn6aJmKgmcWxcpxUeAX3Y9EQb/wOR+LMzKTW5tNEbnieEhkSVCoP4sAa8/6LzoqV/czTbRwuFqLoQYNXN0sByG0jPazPkPJkGg2UNj9xwzx76KJn0QoQ/V3vDevjmgXAnD3/msSryCSIJbw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=gGt7wZox; arc=none smtp.client-ip=192.55.52.120 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705967761; x=1737503761; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ecW1HuB58giFSejI1GX+Wd1u8QBXoYN9OtMr5VLrR8c=; b=gGt7wZoxhUCEmMNHDptYDeR5jpq/o0vKosbQaHvjaQy3JKsAcCELwJX0 GODs/czvrh8d3YLRxtr1taWaKbanB9fasVZoAi+bmk1MSzVo1xa8bY/sQ 8nv3uxueczazcDgN+EoQyBbejEX4bOdwor5Ek+el6vq4hAhsi1cJQZW2e BgVL0x8xZucQeVZYo6QfEVeUp2G7aUwmfJhBaobtRouO+SmR7oZcIE4cz mHGbcQ1jVmnouh2lIalGnNc3KjEDtptMs2fXExOekcWfyUlpzINm+dHhL cyL0elhDsaMUdHerXCgCWWw832krJGd24fs8302N7zDciYr3G1vKsmLL4 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="400217859" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="400217859" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="27817977" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:51 -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 , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v18 097/121] KVM: TDX: Handle TDX PV HLT hypercall Date: Mon, 22 Jan 2024 15:54:13 -0800 Message-Id: <5682a09f9a81e612e88cc7021d4741f180742d53.1705965635.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Isaku Yamahata Wire up TDX PV HLT hypercall to the KVM backend function. Signed-off-by: Isaku Yamahata --- v18: - drop buggy_hlt_workaround and use TDH.VP.RD(TD_VCPU_STATE_DETAILS) --- arch/x86/kvm/vmx/tdx.c | 26 +++++++++++++++++++++++++- arch/x86/kvm/vmx/tdx.h | 3 +++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index f952a95e493d..4628c7eb3002 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -738,7 +738,18 @@ 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); + union tdx_vcpu_state_details details; + 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; + + details.full = td_state_non_arch_read64(tdx, TD_VCPU_STATE_DETAILS_NON_ARCH); + return !!details.vmxip; } void tdx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) @@ -1180,6 +1191,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)) @@ -1188,6 +1210,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; } diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index 14926394f0a5..21cf9cafdf69 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -102,6 +102,8 @@ struct vcpu_tdx { bool host_state_need_restore; u64 msr_host_kernel_gs_base; + bool interrupt_disabled_hlt; + /* * Dummy to make pmu_intel not corrupt memory. * TODO: Support PMU for TDX. Future work. @@ -225,6 +227,7 @@ TDX_BUILD_TDVPS_ACCESSORS(32, VMCS, vmcs); TDX_BUILD_TDVPS_ACCESSORS(64, VMCS, vmcs); TDX_BUILD_TDVPS_ACCESSORS(8, MANAGEMENT, management); +TDX_BUILD_TDVPS_ACCESSORS(64, STATE_NON_ARCH, state_non_arch); static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) { -- 2.25.1