From patchwork Mon Feb 26 08:26:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 206417 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1946365dyb; Mon, 26 Feb 2024 00:56:25 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXI1kqibVmeB/MimHnb2JZwfyvML8oXI6Jh3E+pCq0oD7WheggWC3y4uDVt3nzVV9dxOKssKXNx/OeQcT7lX1T37gZqrg== X-Google-Smtp-Source: AGHT+IFYw27PdUA45rD258DW0YtP+bYb+AI+xadY3P9en6WcPh+j93vzMDx4L/hJObDibQMB3LoF X-Received: by 2002:a05:6a21:1743:b0:1a0:f5b9:bd88 with SMTP id nw3-20020a056a21174300b001a0f5b9bd88mr5579249pzb.55.1708937785170; Mon, 26 Feb 2024 00:56:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937785; cv=pass; d=google.com; s=arc-20160816; b=S+XGWN3N6zfsBRhnvVTvWj5IkCB2m6xV9v509HCUZYb0THXEqGtgEsmUKshbeswJRT iInep5s5xStjku1hlUgKpLWVaKW6UJzNr7pv7M0uLTR+nRHGVdbIhT4Ywk62qttUJ/JB TOkidZfU+ZjmLcRLprgQA397XLG7U3Ocmk0hI3wr51WHU3FFDBcOl+50uUhuUVosGUzn R7J4OnMcAK4FpJG1SGSy9MApEY6jmmZgGrgJedLyfU7PWkJ+wHSsmeFoGHwq8oG4xjXN aFjwi+CIn5mvEandzQA36sMZopqrP3sg6DI2KhqE+DBMLndcmKxm5qY6BabfKlpWP+71 cvlQ== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80845-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id n14-20020a170902f60e00b001da20c45118si3331948plg.470.2024.02.26.00.56.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:56:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80845-ouuuleilei=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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80845-ouuuleilei=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 E1865281F05 for ; Mon, 26 Feb 2024 08:56:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0B3AF79DB3; Mon, 26 Feb 2024 08:28:47 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791951146758184585 X-GMAIL-MSGID: 1791951146758184585 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);