Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1180136rbb; Mon, 26 Feb 2024 00:55:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUlRdPrOWZh1RUSZgNrJzYF7bX/DB+jkEkhq9xWiCwnjmm+FXa6njTWXvX7XPp2Eu+y2ef0Wqu2aFbTmv+exGltnRI4/VHM5CrReNuDow== X-Google-Smtp-Source: AGHT+IGs4U/eMnwron+MPQNncSvjRzs2Pio8rYh/azrZyX4mQAGYVsdO0CZvK2s898wvHlns+26f X-Received: by 2002:a17:90a:780f:b0:299:951f:e7de with SMTP id w15-20020a17090a780f00b00299951fe7demr5291083pjk.14.1708937704232; Mon, 26 Feb 2024 00:55:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937704; cv=pass; d=google.com; s=arc-20160816; b=dA2TYBqiB52BrMH0KWB14/kCOnCd2GXjnLJ6WkPl2R4aLzwrBHPWpBT51zBR7sQeZR +ztqL8vjUHYB8zWQckIhTuG9o5EfALwD+ZOdI0g+kuSFHX6MjPuLfQjSW36eevX0uBQK 0Boox9ebaBfvmc78YAGZpQEN+pV2/hpe00RKnaNgw3EIu7Hprmf23rvZAUCOyYHjlmUW MI3ASvOyHYYzFwAt5Yc0OwxPZzLqtpQtSPbrev3XfG6S2j3WxgvuPfP9VnVm6vg+KcfW atp3+RDS/scvedMoUnZrCgOQfMG6uMEFJcUtedBrg9Q1dZ6o39SVhonvIbWpKEfditX1 9BKw== 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=AjnoZoN5Xnu6L0lXB/VPo1e41oZKOII0TafSUYkMY+E=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=gIGbG0Ptt5m8o1It63lroQ4DqDLaUnVascVHyvnXqj5hnZLpLe5qfcj6Dys60Xv0xE irqMUYFf16ODpDkOsa3J4agNY9BdVQw/wZUj+l3hujorLgJPTKZMs+UaEbjb7slSjT+0 dzrUIKLHXtQoQpgaAQ0jvWCpx1/V+PGV7/fFhS/dcPSduzFnXK1PxFbsFcnLWpYnd2HG t0781PGCmQAkjQMPEqoGu9UrVFSzsoVdwU9UgVbsvk3pOh8KPg7Le/RQ2brFmKBZ/Few 3osEd/VEPqZvcyp84qpU7paukXLtROhzrjlF/SMgLS89wC3N2DeFA1Du0JN9F1V8MyEd 0lVA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZwdUl8DL; 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-80845-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80845-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id qj14-20020a17090b28ce00b0029acacc4fddsi960140pjb.186.2024.02.26.00.55.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:55:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80845-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZwdUl8DL; 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-80845-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80845-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 E9BF8283C56 for ; Mon, 26 Feb 2024 08:55:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 11EE577F34; Mon, 26 Feb 2024 08:28:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ZwdUl8DL" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (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 F030671B3D; Mon, 26 Feb 2024 08:28:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936122; cv=none; b=VRAXNSrOb/zHSyJKZkcqj3unPqAJDpJU+Mlg5bsJ/1lfA5MJBNZVFganAgN4HWDiYGjgPnpPSDAKYhbjczJ1OawcvrKKiFMnxlV6X+uzMxkUPV405AfrZl3oshkAl84XJl8LpsXEO16C/g6/mRrzkPHGTdZMmw3KXz2QrpdvcG0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936122; c=relaxed/simple; bh=AYp+IzNByZIZ5yaCRPVPJqWoxGQPDva5mwiKOWh6qJY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=J0qGi+96ZUxh4xeolL50qgHwLmzw9BXuuA7rmhrF/D2Yz9zujt/thWsqBcMYowoFi8zfAPihP5fvBqzN2WDVHugLdHvF7RrqKNAtcZLOfzyd8QMEmSBBdaCNmW6REnfGDl26oCRTcS9eCLIDUu6W1JQy7eGboeUCdbEl70nb5MM= 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=ZwdUl8DL; arc=none smtp.client-ip=198.175.65.19 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=1708936121; x=1740472121; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AYp+IzNByZIZ5yaCRPVPJqWoxGQPDva5mwiKOWh6qJY=; b=ZwdUl8DLWqXC+5Ivn/kaGFe0bVgBV0MWFnZJzKAHahMj22HzrwtPD7sI N45c8gTncLaqNEwchhkReZMDa9yNJCLUhubBHZr13tgVwozqKh9jIJsRR whd27qlGqZWMXq9n0ILYY78y73W2XueH0S2qmLuo4PhhyH8yLq2ARnbyC ZQRwByi9h/tjhwamGq4tHMjYrqVCak6hcHtZ1p8rO/wU2aP1o8RucCw3G nIoZRzo0Q92kWrimJSCzmvYDFO4jr65nWDbYaGn/hT4ooI/NlG0YUVa2w KgT22B9OetDHphYpbv/lk0YHG6anDAkznpXHxUQmQ+mgUCcAZQU6oiuGv A==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3069517" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3069517" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="11272555" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:39 -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 v19 082/130] KVM: TDX: restore user ret MSRs Date: Mon, 26 Feb 2024 00:26:24 -0800 Message-Id: <8ba41a08c98034fd4f3886791d1d068b0d390f86.1708933498.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 199226c6cf55..7e2b1e554246 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -535,6 +535,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); @@ -627,6 +649,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; @@ -1972,6 +1995,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