Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp864425rdb; Tue, 23 Jan 2024 18:50:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IF3qiCPQDeWwlekUQrx5bHsBv9CAtA02XCZPjLJgHT8oizn2VpISADGst1x1zyVC3outbrC X-Received: by 2002:a05:6214:2682:b0:681:30e5:1c5a with SMTP id gm2-20020a056214268200b0068130e51c5amr2021283qvb.110.1706064627426; Tue, 23 Jan 2024 18:50:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706064627; cv=pass; d=google.com; s=arc-20160816; b=qpp75YLtMNwW45J1dm99C+QM8ifecPlEUcZdDQvSR8mRUjzsIlN6TmVD3BFQLFMAml UzGOuvBbsHV3rU4chGlF8AUJ4z+JuYLH55np7MifTGb0VAKYiGibOvT2AZf+hOVayuK/ zO8t5L6547JpWT+7mg8AOHKJUUd+hbwjT8bSLUKTNKeAj+9kcZ/HiKSlrBZ4xcUrWBa3 ln4YAAqFhHOGDENrqq+RxEpcStwUBhN/2OrUluah+ixJDDoDqvMcxfBbiHGxAp4/M5UE mwgxX5BxrAJ1ykYBF2wJbwHVhd6Gbdv5BXhRo4sQVV2nwSwSwdlzWpIfR8GTc7zXw6ak Ne3Q== 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=TOOmarXRC/HYV68iH4YrIq9T0UM9SEhHLPCSwT2bv/4=; fh=D+u40KSnYgjUYLGDRdXdD62xsMHg3nFDmpf3sEnyojA=; b=EHnlgd26KBltWcZDya2h6OL20Fs6E75RRhfLKZYDRlJmBHDpY6EDcClk/ALYXb+vns nG0lucTRMeJ2p2/wk/8CD+/FAMsyVOzos2CxgjEJpQc/L45u38/GiSno3Lh1iopOK1fV uMJ8GNXg0uSEvy0d+s62g7WVYTxP5pmXZw/4gH7epORIPniHWybeifuEQRHF5CaC7E4q iDPgjxEYIaxVqd4QBcYWaJGRNrT50E0d/Gry39weMgUz36bitte2fzaYBu7D0n2BgtIL 5slqqKe9R88AJsFVzlCj5JjaqTRbelDfJZoPNrmvduGnmf6Sv3siGDn99CbRvLRXwYD9 DQQQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RD+YoJYk; 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-36342-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36342-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id a23-20020a0ca997000000b006819435d7d4si9379479qvb.571.2024.01.23.18.50.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 18:50:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36342-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=RD+YoJYk; 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-36342-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36342-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 363211C21AFC for ; Wed, 24 Jan 2024 02:50:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 99FE8282D6; Wed, 24 Jan 2024 02:42:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="RD+YoJYk" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) (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 D52B718E1E; Wed, 24 Jan 2024 02:42:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.120 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706064168; cv=none; b=EgUyayNWxWgdk1JOfRUn0J79pc+00N9V9gOYnCjvPLInly/cLWCv9X3OqiOQrOLgtwveD/PHhq5b3JeBfShuiVwldOTMEnLqgON9e46KLfFX9+QePjQSd7WP9Z82Ux+r5EapKXu5QtaF4m868ofnzbDGlgUAdkeuDlrAOpqsByc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706064168; c=relaxed/simple; bh=h8um0cUpGOSDzpshStMJ7pABEvMbvAz9TJHO3NVRyXM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N7wwVHQbkDRfOobNLs2MEL6Kxvs5hg03q/4Z1attXQ+Fja/maePuui0wuKd6s/lAOV8ySLKN8DFX0lQzIYz/ClgL8UCnkCj4qUNVseRzZ22vB+BHdKi09+hJC2fMS/sK80LHA20UZTw2JHyjwOXU4srxxkNBtSROOeqNqWwRhbc= 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=RD+YoJYk; arc=none smtp.client-ip=192.55.52.120 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=1706064166; x=1737600166; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=h8um0cUpGOSDzpshStMJ7pABEvMbvAz9TJHO3NVRyXM=; b=RD+YoJYksDxfDmMEAVbos7KH/+l5xlDjF0Z+Rd323aJSSO4ok4bzjcev QXU3mpVb9vqRxnu+0YZNcAv2lahXIL4QzIb+UlvzGCt8ef4ATyUOxk3c+ emEst1PpeTr++i7cSwloMa3ye6jb74XQPrn5WEL8kn+O6sDSokwPB26Pe loA2hxRdYrLZsN3ghT+9mScAPPmXZN6xM4OSjvlxDrRX5ug5ZTkb+q25c 0Mluy8WpTxuwOtdM9FWfP+D2VRnmBnADJb+P+aFa6mjbpW8udwCxjxNa1 YkjHbTesc3R1ubnWH1fug1MeymsCN1hX8JuSkekQBmmmTlGf9fqOJN0qI w==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="400586556" X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="400586556" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 18:42:42 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="1825909" Received: from 984fee00a5ca.jf.intel.com ([10.165.9.183]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 18:42:42 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, yuan.yao@linux.intel.com Cc: peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, mlevitsk@redhat.com, john.allen@amd.com, weijiang.yang@intel.com Subject: [PATCH v9 21/27] KVM: x86: Save and reload SSP to/from SMRAM Date: Tue, 23 Jan 2024 18:41:54 -0800 Message-Id: <20240124024200.102792-22-weijiang.yang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240124024200.102792-1-weijiang.yang@intel.com> References: <20240124024200.102792-1-weijiang.yang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Save CET SSP to SMRAM on SMI and reload it on RSM. KVM emulates HW arch behavior when guest enters/leaves SMM mode,i.e., save registers to SMRAM at the entry of SMM and reload them at the exit to SMM. Per SDM, SSP is one of such registers on 64-bit Arch, and add the support for SSP. Note, on 32-bit Arch, SSP is not defined in SMRAM, so fail 32-bit CET guest launch. Suggested-by: Sean Christopherson Suggested-by: Chao Gao Signed-off-by: Yang Weijiang Reviewed-by: Maxim Levitsky --- arch/x86/kvm/cpuid.c | 11 +++++++++++ arch/x86/kvm/smm.c | 8 ++++++++ arch/x86/kvm/smm.h | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 3ab133530573..95233b0879a3 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -149,6 +149,17 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu, if (vaddr_bits != 48 && vaddr_bits != 57 && vaddr_bits != 0) return -EINVAL; } + /* + * Prevent 32-bit guest launch if shadow stack is exposed as SSP + * state is not defined for 32-bit SMRAM. + */ + best = cpuid_entry2_find(entries, nent, 0x80000001, + KVM_CPUID_INDEX_NOT_SIGNIFICANT); + if (best && !(best->edx & F(LM))) { + best = cpuid_entry2_find(entries, nent, 0x7, 0); + if (best && (best->ecx & F(SHSTK))) + return -EINVAL; + } /* * Exposing dynamic xfeatures to the guest requires additional diff --git a/arch/x86/kvm/smm.c b/arch/x86/kvm/smm.c index 45c855389ea7..7aac9c54c353 100644 --- a/arch/x86/kvm/smm.c +++ b/arch/x86/kvm/smm.c @@ -275,6 +275,10 @@ static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, enter_smm_save_seg_64(vcpu, &smram->gs, VCPU_SREG_GS); smram->int_shadow = static_call(kvm_x86_get_interrupt_shadow)(vcpu); + + if (guest_can_use(vcpu, X86_FEATURE_SHSTK)) + KVM_BUG_ON(kvm_msr_read(vcpu, MSR_KVM_SSP, &smram->ssp), + vcpu->kvm); } #endif @@ -564,6 +568,10 @@ static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, static_call(kvm_x86_set_interrupt_shadow)(vcpu, 0); ctxt->interruptibility = (u8)smstate->int_shadow; + if (guest_can_use(vcpu, X86_FEATURE_SHSTK)) + KVM_BUG_ON(kvm_msr_write(vcpu, MSR_KVM_SSP, smstate->ssp), + vcpu->kvm); + return X86EMUL_CONTINUE; } #endif diff --git a/arch/x86/kvm/smm.h b/arch/x86/kvm/smm.h index a1cf2ac5bd78..1e2a3e18207f 100644 --- a/arch/x86/kvm/smm.h +++ b/arch/x86/kvm/smm.h @@ -116,8 +116,8 @@ struct kvm_smram_state_64 { u32 smbase; u32 reserved4[5]; - /* ssp and svm_* fields below are not implemented by KVM */ u64 ssp; + /* svm_* fields below are not implemented by KVM */ u64 svm_guest_pat; u64 svm_host_efer; u64 svm_host_cr4; -- 2.39.3