Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2612027rdg; Mon, 16 Oct 2023 09:21:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGT8EGHktxO+UVKfhQEhj2is7aa8GdMSWL4fcJqFv6Wi2U9bNw7UZgrUpxE9GaPglxio6cg X-Received: by 2002:a17:902:e550:b0:1c6:2f59:8c4d with SMTP id n16-20020a170902e55000b001c62f598c4dmr35931921plf.28.1697473292988; Mon, 16 Oct 2023 09:21:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697473292; cv=none; d=google.com; s=arc-20160816; b=hYIIgV9FEx0uQZNq4eKei2I0Hn6W8fqCmaIb47taE4IMvfagaarj0b9d3BrYvncIJA jYOwRujqoZ9KkJWgNHBaH6woLn6co4S7cfk61SmbqDr/DusQ4T7dEVf82BnpcqbLS4lh RheJhYW0odZ49VRNcyuzA1EnrYdqqWrwD0d93Gz6IGFEV8FnjZaMoWIaQ+yd8DZ2FN6i KLbdxhGXxDiQaLZUpRowRj7SxU0L6PsQN7rezlde+fDQsab3/Akz61+uA+HhmVl4AiHJ BmKXesfSuZgdEXyjn9ENDO/VBMvAvQYcYnp2RZDCPxJhEOkGlt5WsqxyqeZu69asewc6 gJ3A== 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=z5ROr0njB5vnJYhA5CYfdj7V/zWUGLh6qAdM/bguZ3o=; fh=lRdU2Q/1zx5DcPdZuWBjshA5VT5Oc9cEhB1tCFiV0Nw=; b=TuCmKXNR5LZo03uxo3QbMZhl6dvGsikkM/K8L3LeOev9qQwFfiGZujdoE9PdksJXQd ErkS0De8VeMvSvzft9crfXm1lfiVnoFQ24610WC7jj5QO6cXnpXpG/DErHlzTMygh/Lw UhqMcXt78sCRbQr0g6QCXmjPkHt5QFMEGEJbmjbzBspYcVqvAaXADsfkTW/sIEtUurV5 0Njaao8kOlm2I+v7smFtWYby+MMs9GAtSfoDyifc1XLSwrOGeim6tQzUFfpFHURCtqPs 0cu2yamty6KjPcUldK2ikFNEDz1E7ZsdurGKRwGtx9YWSQwC8+xuoImDs36zmKzhh7fc n32g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HoakumhZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id r24-20020a170902be1800b001c9bdaa81e3si10394940pls.633.2023.10.16.09.21.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:21:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HoakumhZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 8B4E48022C84; Mon, 16 Oct 2023 09:21:16 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233924AbjJPQTP (ORCPT + 99 others); Mon, 16 Oct 2023 12:19:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234212AbjJPQSz (ORCPT ); Mon, 16 Oct 2023 12:18:55 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 950411FE6; Mon, 16 Oct 2023 09:17:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473076; x=1729009076; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zcWluIS22FAvjw6XlxPPD1KcHJW/qHe+l7RuWr14cLA=; b=HoakumhZ/ORj63yqXyjbgneYfZfXuUUqk+54n5bAYQzgZZrSdLrkhn+m 0h3SkDzQzRhXvO0CZoAemf5egWte4fsE3dz0bA1BvRViSJPTkNc8CQCK+ 982taR+yD3KNhJPZPLTEtWLaBh/rmZrKP4Zf+CNTpt3iQpsUM+Th3nASh 595EtHFoxIx+2xBfn16jREVpsK6sZpGrgNvTnUnb/I5hulkf+6VT3zgom LPJb3uXOR9cVMBfY+d0HWpQMcOo4qcj90eCYEbJwsYM9v+mY9lpT6QNg4 PCDC02YbpqVb+tZy7Iuyk3Gd0IYJFQBXYwcdQN3TRZ9S+b1fCKr/T/aMh A==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="364921875" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="364921875" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="846448196" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="846448196" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:52 -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 v16 066/116] KVM: TDX: restore user ret MSRs Date: Mon, 16 Oct 2023 09:14:18 -0700 Message-Id: <51bbd590d25bca0769084b9064711ccb91dac989.1697471314.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=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:21:16 -0700 (PDT) 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 98827160c6f9..87ed1a255c3b 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -509,6 +509,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); @@ -601,6 +623,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; @@ -1833,6 +1856,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