Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4117549rwb; Sun, 7 Aug 2022 15:33:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR7HJ6Z2dMT8tOZyPbL1f3oso149bKscnxpHL9aSWC0Kq961gSTkQ9OVJpgzB9dVYit25Hfz X-Received: by 2002:a17:90a:fe10:b0:1f3:1de7:fe1b with SMTP id ck16-20020a17090afe1000b001f31de7fe1bmr25693185pjb.189.1659911586243; Sun, 07 Aug 2022 15:33:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659911586; cv=none; d=google.com; s=arc-20160816; b=WHTNK1VZ3Ao42oB72y6bw7FRT/DvzqLRaC0HQ5u+CSJeGXcm23K/NLVQ38bpEC6TeY 9vg7VFd6m9gglxY3wBNLa4QIBraMHqSgX9DMsHyAqllPv+t9gNbwhh9BnfqPKueyGARl nYXJgpicXH4nuYUX1IOeyIr3B7dLt07eWBic2H4Vq721Eottu9UGE7mf4SDNKdFyUA6f YukATNLVIL67CvdS+TADByDQ+VbrvhVR3B+YXFQrQ6vz4tkAa43QhUFgSlY4wC+++kcX wfTMs/6SsBkwpTU+erEKBDKgQYkGW7b+mN9sOlCyV+1jiuHW6Ki1SWBGfZbqQ2uy7MD/ v8FQ== 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=45F9k2WOh+W6Q/p8aT0VcPAGSJFlkmuFJEJmYbZ8fKA=; b=Xk3omNWoVVEx9k7X5vHNxKUXULGkm+RLfHS9gENjqtJ23GZJSyx+qvordJv8G1yXHF cvF5kPDqWI/Z6RSU0cMadUXoM7I6g1g1jkjYa9tfD+cR295PRcSN/IpFRLbp8wwTqATF +Pc1Oa5GSziuPbrOA1D2CkL1x1yeTx/NrD33nmEHUmXmK1wQjrrstiMjVJMplHEEcqmz Lyik2Relyk7/zVdvp0YfnVLYO+b7i7s9+FC9sAzGEPfu+O+I/6kGVE6zfjaYyJvmNh9T eRKNTwtBh/3uiGEEOP116QNtqU+Tvx5YNK9IUcERUYYIXJWL6qULyyVa9BB51Lc7ETgi +8+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AUhJkDNC; 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 h5-20020a170902f70500b001708b184ff9si5546009plo.213.2022.08.07.15.32.52; Sun, 07 Aug 2022 15:33:06 -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=AUhJkDNC; 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 S241898AbiHGWKd (ORCPT + 99 others); Sun, 7 Aug 2022 18:10:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241800AbiHGWG6 (ORCPT ); Sun, 7 Aug 2022 18:06:58 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A846CB49D; Sun, 7 Aug 2022 15:03:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659909794; x=1691445794; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=69Qhug7uYKK7xc9nnBm5WUvzBfvk+zTqYYudVEDf8Rs=; b=AUhJkDNCC5y5pJmi7em8bwskTcVYAUZ0RtXMp4LShtmr8fSULanWzEQF /lxj3XqTl+CoTGfBkuFDwlra+yV53jXvR54CslRtp0Vz4HYPNz1rklNmj rfSZ1iD0EArVToGidVrj1qcPs9DV9yQLb7GZ8gzGZQlphSVx6S1W5c9gd b8L4Ii5oyD4gUjFsJAqKBhR0VrzdaaYzMEdwdd0CUusQ8+aOS3BnmCkvQ f3AxkrxSt8Rla3JIkEabOGv6zDRVv4qbaXoOHDNsMCjbBWqNZbNurXN7J kZrViuvNHFQxTAadaLQ3tzDtc9YQJ9G23HLnCWf39dcnLF3uxkeIW34I5 w==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="291695711" X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="291695711" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:40 -0700 X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="663682670" 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; 07 Aug 2022 15:02:39 -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 Subject: [PATCH v8 068/103] KVM: TDX: restore user ret MSRs Date: Sun, 7 Aug 2022 15:01:53 -0700 Message-Id: <707fce0d21cbfb2fe70af58c3b7570b458b238d9.1659854790.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=-5.0 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 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 0cf9ef4a1d2a..0aa52bf1c281 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -580,6 +580,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); @@ -619,6 +641,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; @@ -1454,6 +1477,26 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) return -ENODEV; } + 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)->registerd 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 alreays 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