Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp550175rwb; Thu, 12 Jan 2023 09:12:34 -0800 (PST) X-Google-Smtp-Source: AMrXdXtQham+WiNbL9r1ACH3yD2g1E1LBrQNcZ1k6tyAvriRgZg6WsFwQUxQCX4S/OqRkf+TUitG X-Received: by 2002:a17:906:2816:b0:7c0:d452:2e74 with SMTP id r22-20020a170906281600b007c0d4522e74mr66412603ejc.4.1673543554575; Thu, 12 Jan 2023 09:12:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673543554; cv=none; d=google.com; s=arc-20160816; b=JifLD8yU9Uk4x050GsBFdHVpTHqTHT01zJyx5WWLqRcN5sQsn5KLbmaka36WH8QPtS jaabfoaV27zgkopE4vvHC6Rgi/bHF0TM0RlBWcxihw4xXEWHNdDEuwfcgXtUek9uWPPl f5ANqf01Hu/E7fr7OYET+Iy4G6XdpWM65NDaXVtt6CfeZK9y16HZ9XoWZdtMqgF5oOUA q8gxbEEBQ/72bjlNqyzL8h7CVSmNzY6XN0r3UJBSSLtt0YPVBG3bs396i7KXeobA9awL Ak1UGNLOtHqdFUVvuQjPbeXxPlCn5au+dPEz0Uq6vkk/VxaWnrF+45gJd3KtHA4s1S1O 4zUw== 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=K8d+NYacxb59wvjDMxR4nHi0tJwSTGWPChLEqvghNu8=; b=PQJ+nmBNKOyKQOSsNRs37GlPvCZA7wIenwWA7UAZnscR1d4wllUVkz71ILHZRgsww1 WnmPtklmmDvZ69BAw/OcymrWQRT0VlxxjbXamtEDIrFGKmaxHTShNBj387HqbU4Xox/9 YA6yyn87xMLRdFwic9tPMGFm62PXtaIAG4Huo47lhz64EY5D7PM/x8SXGfEd4Ebt9Y5E BiT+3FAkvX8zQCTk4xgunGt8DHgMdpMzBrklLnt+U50zV11VYH4o03OoeyCW5cBx5erE JcPpPW3SLWtlGLKcCUvwupq1JVFiib/0GyqGCpUSqjX7KGy0GtAuF4cY7wbIxUxP+IbF 2Zrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=iXY7hPKA; 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 qa9-20020a170907868900b007c10a0c6f44si18997156ejc.623.2023.01.12.09.12.22; Thu, 12 Jan 2023 09:12:34 -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=iXY7hPKA; 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 S241242AbjALQtb (ORCPT + 50 others); Thu, 12 Jan 2023 11:49:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240349AbjALQiu (ORCPT ); Thu, 12 Jan 2023 11:38:50 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86607271AA; Thu, 12 Jan 2023 08:34:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541249; x=1705077249; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XIBHMW1eyVfCY7IFUGtMBBW4XDJDqjOi2Ou1UwPM7wg=; b=iXY7hPKARGsYv9bvPp9TiJ8C5V1eXHVXfJQ8KStnOZ2RBV6GLHOY85cQ KDA8MZqpwRHvx3Z/1gPla8c48+gVbNj4SducGrz3T531mRi8Tb4YVm3iS iUgiL28B+maC7AD+e9BEJWwpREL7BbSwcgy9s/39lFa5o96uw3QBxty6e IUCGiSPNOfP3t642aAIbV7l2SX2z0YmbJtJg5Ql9pnOzynanUFQSWVHxk g/SZLETkrj6dKmFJUXGmaDvVfq+M8c/OMlYtsGxOhuL/fhMaV/pJC9nw0 dldqztMWQ3pDrL/+lfAW1WueS5p0VhBtjiYu7pXqLSwz4YGCri3sVy1oD A==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="323811996" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="323811996" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:32 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="721151877" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="721151877" 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; 12 Jan 2023 08:33:32 -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 069/113] KVM: TDX: restore user ret MSRs Date: Thu, 12 Jan 2023 08:32:17 -0800 Message-Id: <239f6056d8109c5e94eb4e8eda54e5649bd1976c.1673539699.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 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 Several user ret MSRs are clobbered on TD exit. Restore those values on TD exit and before returning to ring 3. 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 7bd47a76c96c..4bd651b31172 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -461,6 +461,28 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +struct tdx_uret_msr { + u32 msr; + unsigned int slot; + u64 defval; +}; + +static struct tdx_uret_msr tdx_uret_msrs[] = { + {.msr = MSR_SYSCALL_MASK,}, + {.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); @@ -500,6 +522,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) tdx_vcpu_enter_exit(vcpu, tdx); + tdx_user_return_update_cache(); tdx_restore_host_xsave_state(vcpu); tdx->host_state_need_restore = true; @@ -1581,6 +1604,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