Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp165289rdb; Mon, 22 Jan 2024 16:03:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IFmiHB01Zg58uhCWxvy2SLbB84bGO3YvEfGUDHuGMO4wgc1cXAWvVY+DZuaJ5f8cJxbmciw X-Received: by 2002:a05:6a00:4f81:b0:6d9:c201:66f0 with SMTP id ld1-20020a056a004f8100b006d9c20166f0mr6895386pfb.27.1705968219778; Mon, 22 Jan 2024 16:03:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705968219; cv=pass; d=google.com; s=arc-20160816; b=QRAWb6u75myYty11+ySF+h9U7ftb/CLH+i45F224iMZEV5K+KqzwgzmT6rRN+LSc+f OsBRL38FLZEqRXoRvtgchxELxWASuT6AxW9ZASRJTCdiipIu+hIPbAs/2kzxX21hTrz1 fG4R9QjRWWFiImHtTFHIlAhZSdr0CGBI5Qbcvl5DGV1p45VUaIwM9moYq2eV/JUI5jAR Z3F2H3w5MlNrZJ4Zg5RcwfhK1K4Unqjn/w/GVY3zcOcnf+QK8d5okTVZoJuYw1yA/YGF FIrO+oDBqFejc1u9S1NOyYityfF/1VqMVGdqelVdj99X4f6xP9fSWW5Tx512xBGKIUOh dP4w== 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=fKBHYNSWBdql+cPyswC96K1+GLwItIub7LKsP6FY5sg=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=pXAX72oiOXcqOMoGZWuJ/7fSzVnh+p6pVddlRpBcdsldRbtwaRtMxaA1S/sb0JXgmK nBWj04svYXAnD3qRTBhyGzqZBJzIcrl5WPhtDHy6p5g0T38Y5CB8I4X7A1Eek5Nffx3X DFIDQWBmKAh2mi0AY/64WtMslMsysgTOk1LMsz2xXJSnJ8tRSbpLZ/J3ATfnx/ISR8G5 pIRWGIZtOCA4BK3PkaOLCNeaqTxL2nYKdewJdavbvQwt+mY0ClgPhZ4cE0+sL+M1KuZ7 7/cDcBEn0nFtw3hGeISGUA8A85P5qLa/+9aQJN6xxE8td+3zglTkAezPDaKBEYpk1OTO rXpA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bbgcwRJA; 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-34362-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34362-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id le12-20020a056a004fcc00b006d9906df4ccsi11110753pfb.162.2024.01.22.16.03.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 16:03:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34362-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=bbgcwRJA; 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-34362-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34362-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id BEB8728BACE for ; Tue, 23 Jan 2024 00:02:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC5A460DF1; Mon, 22 Jan 2024 23:55:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="bbgcwRJA" 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 6161B60893; Mon, 22 Jan 2024 23:55:41 +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=1705967742; cv=none; b=AQxim8nPri66vycZs2Rt5YknIVylJqZyAbw3hdahY2k2AHtu8ZiBq2fmQgbS6T0Ssy7moim6fCu++KW08Au//PC8zXvYgzrGVuErqkcasjVCKU+TX2R0of3rJZJK006JnFw2Z4cTq+DTgmqjNn9wGEgur8xZQV82u66QGOU4wkk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967742; c=relaxed/simple; bh=dUE8DxTtpIopl2l29eWxcZPjexOwf0PC7nhgiUfm1q4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CCZBmEpSsNmKHWy0CUwhiEcs2+3RVszNIhqNGmqXJIpG7Us7sMkpmVAIumSw2ki9N6MQKV+p3fo9/DZwVp7x/UlbleHF9GMRcIs3tNy8Y3KpI2ZOCxOAbe8cUjTRx5pUuNUjyPPw1Wjk6X7XZXHxCC+KYhMmBtmd24YtLBOLpUk= 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=bbgcwRJA; 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=1705967741; x=1737503741; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=dUE8DxTtpIopl2l29eWxcZPjexOwf0PC7nhgiUfm1q4=; b=bbgcwRJATmbV/+pgqJPws9U4wQQgl5tpUuoGWn8XA+ITfi0TLvO3/pHm BAbNuUgCc+PfwEtnH6lY+lxXRyOkX56Xyr61iwtbQVJzzh4RfvYikAoQB KXR+ILVqE5x7B0OYQUwwD9f9AecpQ2TvceATO+cL8b1A/YVYugGnpzMuC 9Vjf50MTcPLHCNOt0v03AyjVjAiIbh8EY7wbcXjdChH/EM9hZEN8BQQQX bFJBXZcDbslI5eWPF8a590zuGZXSE0fOdFTpcdDlBAftbQF2TnQ6vakk6 YWYdxuzQVZv2JLpK6mikVx1wGbDeKsDDZDXatje4es8uHF72HU6uT67JE g==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="400217740" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="400217740" 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:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="27817890" 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:40 -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 071/121] KVM: TDX: restore user ret MSRs Date: Mon, 22 Jan 2024 15:53:47 -0800 Message-Id: <65e96a61c497c78b41fa670e20fbeb3593f56bfe.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 Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3. Because TSX_CTRL requires special treat, this patch doesn't address it. Signed-off-by: Isaku Yamahata Reviewed-by: Paolo Bonzini --- arch/x86/kvm/vmx/tdx.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index fe818cfde9e7..4685ff6aa5f8 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -585,6 +585,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ } +struct tdx_uret_msr { + u32 msr; + unsigned int slot; + u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr = MSR_SYSCALL_MASK, .defval = 0x20200 }, + {.msr = MSR_STAR,}, + {.msr = MSR_LSTAR,}, + {.msr = MSR_TSC_AUX,}, +}; + +static void tdx_user_return_update_cache(void) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) + kvm_user_return_update_cache(tdx_uret_msrs[i].slot, + tdx_uret_msrs[i].defval); +} + static void tdx_restore_host_xsave_state(struct kvm_vcpu *vcpu) { struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm); @@ -677,6 +699,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) tdx_vcpu_enter_exit(tdx); + tdx_user_return_update_cache(); tdx_restore_host_xsave_state(vcpu); tdx->host_state_need_restore = true; @@ -1936,6 +1959,26 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) return -EINVAL; } + for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) { + /* + * Here it checks if MSRs (tdx_uret_msrs) can be saved/restored + * before returning to user space. + * + * this_cpu_ptr(user_return_msrs)->registered isn't checked + * because the registration is done at vcpu runtime by + * kvm_set_user_return_msr(). + * Here is setting up cpu feature before running vcpu, + * registered is already false. + */ + tdx_uret_msrs[i].slot = kvm_find_user_return_msr(tdx_uret_msrs[i].msr); + if (tdx_uret_msrs[i].slot == -1) { + /* If any MSR isn't supported, it is a KVM bug */ + pr_err("MSR %x isn't included by kvm_find_user_return_msr\n", + tdx_uret_msrs[i].msr); + return -EIO; + } + } + max_pkgs = topology_max_packages(); tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock), GFP_KERNEL); -- 2.25.1