Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp864634rdb; Tue, 23 Jan 2024 18:51:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFWJyDBVctkToiKl1k9TKHinOnZXYTUgH/FWjIZrMq/rz7aQ8ISlkopbonI1HGjbms/G8ct X-Received: by 2002:a05:6808:ecc:b0:3bd:a8a4:3d46 with SMTP id q12-20020a0568080ecc00b003bda8a43d46mr773700oiv.107.1706064670618; Tue, 23 Jan 2024 18:51:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706064670; cv=pass; d=google.com; s=arc-20160816; b=RZmKNqXqGsuf4ShbDVG4Z9OePTvI2fsEyceARzcSwg6T8cNHWMukX710nfvDuZz1CO bpSG6VNWMTCVar1AJ4xphSd3QQ9l9oTv5+UrTD3gmVkSe1uzbmpkn8u5gSwbZTZlrJld zj2f3MsGlJT9rxQnc5gb16YyxQQMyV9hoB51MP7qxpicWaxnmuNurDH1dF6xzvz+XvvQ BFd/UbLd3SxUcSi0xoGBhZ8SWXiFOFxaXkD6Mlb90MlRw4B14vTAO6iRRPqJRO+5gmuJ Ms1krpmoIF33Skdzmezknub3k6reg1lWTdRTbpniUEF7QPca9TX3WVv0XJHgb4wI09Gv jcmQ== 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=2W6ua03p62wEHOmEXD3xHN445AXLBRMQoRwP2HjRjEI=; fh=D+u40KSnYgjUYLGDRdXdD62xsMHg3nFDmpf3sEnyojA=; b=wMaS4nkj4td0oBCG8sgDaQc3/QfHi4KRqRXL3fj12Lvce8Tefd/pXXpDVNOnYV+fI5 lkwsrKyk1cGj52zkYTdBM8WCtNdylB7PhyJzxdOqu6940sOG40S9Z7iigQbnnrWgPjLA gMtIVOCf4+U81SKdcjS+zH7jlWdhv0CdpvqmfUmKlI0YvYnXnHidqhk4O97D/ag2K+N6 ol4b1o/HeutpPJvF93ndEuHOeIZbDg3vpXhqL/c8P9UpA5Cy6IRxiVYDZo/Xf2FDRNNH KJdfa84hyY47QJtT6Ig58H+DRk2TTrdCOVUU/Ql8W4OcMZoUoxAlEJGNyW+M3odXSOGt Lmiw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VvPI2eDt; 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-36344-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36344-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 l3-20020a656803000000b005d39d96c0f4si1922076pgt.466.2024.01.23.18.51.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 18:51:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36344-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=VvPI2eDt; 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-36344-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36344-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 2AD17283FD6 for ; Wed, 24 Jan 2024 02:50:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D53BC32C6C; Wed, 24 Jan 2024 02:42:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="VvPI2eDt" 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 CDCBD1B5A0; Wed, 24 Jan 2024 02:42:48 +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=1706064170; cv=none; b=UfpFFHACyB3u8NZsvvOT+y0gYJa5dKjgQflEHyrMK8xFWWZgjCx7zuvc4vHssgiER3VgxcxkndCRaIA4m4N8WyXjoX7hCm8vR/7iO3TYsqzFZK1zfcnwxvXv+1pCic83jwsq2SMYfsZW9Iokl2q0W3UBWqk3Rn9Bg/X5Nu0FnaY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706064170; c=relaxed/simple; bh=cqA+46P9Nu0FwZGZRW9VynovBYFc0Tg+zn4fsIptNWI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=q8cjc186EP2mpok6CKsfqrPl3aBDgYIyak0H2zjl34L5w3cojEwsDIjMDhQ4qqjsCDYwc018bxolplJAfIq5fEhPSg14hl2rCjCk2oHjrX3MrX+lKvJN09geQ9Y6DiAK3iuZuehOXQ22Bttv9l5V40t7FgUJVH1n3zDrQHlUH0s= 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=VvPI2eDt; 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=1706064168; x=1737600168; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cqA+46P9Nu0FwZGZRW9VynovBYFc0Tg+zn4fsIptNWI=; b=VvPI2eDtwdL9H7REPO4CkUkoddW/L9teqGQjC701kAyUfH2pBuWLlWzi HHihqu3uO6x2NRgz/WI4ErsEbMRXqnakozyWNpIdmKJ/qrWgDnIxveKcb bWco01COp7xkPjSIJg7CuvDoYv+BO2QA19EUUN7drGZ31n9RCKmP0Y1gH 5mqjZ/UPjHsCtWPy0LOBETwST7U3AvBKnyxIzHfqJgPNCld56kMPknM18 63Gna38O7wTeTnetCztU53iLVMSznzff2Ena5WSYpz4nVTY6tC6Y9M0nt ICKY8rKWRVFfsHaz194eRO/bho+JfsH2IMwbKLjdrqjNkpgG+q7tzT/PW Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="400586570" X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="400586570" 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:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="1825918" 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:43 -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 23/27] KVM: VMX: Set host constant supervisor states to VMCS fields Date: Tue, 23 Jan 2024 18:41:56 -0800 Message-Id: <20240124024200.102792-24-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 constant values to HOST_{S_CET,SSP,INTR_SSP_TABLE} field explicitly. Kernel IBT is supported and the setting in MSR_IA32_S_CET is static after post-boot(The exception is BIOS call case but vCPU thread never across it) and KVM doesn't need to refresh HOST_S_CET field before every VM-Enter/ VM-Exit sequence. Host supervisor shadow stack is not enabled now and SSP is not accessible to kernel mode, thus it's safe to set host IA32_INT_SSP_TAB/SSP VMCS field to 0s. When shadow stack is enabled for CPL3, SSP is reloaded from PL3_SSP before it exits to userspace. Check SDM Vol 2A/B Chapter 3/4 for SYSCALL/ SYSRET/SYSENTER SYSEXIT/RDSSP/CALL etc. Prevent KVM module loading if host supervisor shadow stack SHSTK_EN is set in MSR_IA32_S_CET as KVM cannot co-exit with it correctly. Suggested-by: Sean Christopherson Suggested-by: Chao Gao Signed-off-by: Yang Weijiang Reviewed-by: Maxim Levitsky --- arch/x86/kvm/vmx/capabilities.h | 4 ++++ arch/x86/kvm/vmx/vmx.c | 15 +++++++++++++++ arch/x86/kvm/x86.c | 14 ++++++++++++++ arch/x86/kvm/x86.h | 1 + 4 files changed, 34 insertions(+) diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilities.h index 41a4533f9989..ee8938818c8a 100644 --- a/arch/x86/kvm/vmx/capabilities.h +++ b/arch/x86/kvm/vmx/capabilities.h @@ -106,6 +106,10 @@ static inline bool cpu_has_load_perf_global_ctrl(void) return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL; } +static inline bool cpu_has_load_cet_ctrl(void) +{ + return (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_CET_STATE); +} static inline bool cpu_has_vmx_mpx(void) { return vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_BNDCFGS; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 34e91dbbffed..3993afbacd51 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4371,6 +4371,21 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx) if (cpu_has_load_ia32_efer()) vmcs_write64(HOST_IA32_EFER, host_efer); + + /* + * Supervisor shadow stack is not enabled on host side, i.e., + * host IA32_S_CET.SHSTK_EN bit is guaranteed to 0 now, per SDM + * description(RDSSP instruction), SSP is not readable in CPL0, + * so resetting the two registers to 0s at VM-Exit does no harm + * to kernel execution. When execution flow exits to userspace, + * SSP is reloaded from IA32_PL3_SSP. Check SDM Vol.2A/B Chapter + * 3 and 4 for details. + */ + if (cpu_has_load_cet_ctrl()) { + vmcs_writel(HOST_S_CET, host_s_cet); + vmcs_writel(HOST_SSP, 0); + vmcs_writel(HOST_INTR_SSP_TABLE, 0); + } } void set_cr4_guest_host_mask(struct vcpu_vmx *vmx) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cf0f9e4474a4..9596763fae8d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -114,6 +114,8 @@ static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE); #endif static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS; +u64 __read_mostly host_s_cet; +EXPORT_SYMBOL_GPL(host_s_cet); #define KVM_EXIT_HYPERCALL_VALID_MASK (1 << KVM_HC_MAP_GPA_RANGE) @@ -9840,6 +9842,18 @@ static int __kvm_x86_vendor_init(struct kvm_x86_init_ops *ops) return -EIO; } + if (boot_cpu_has(X86_FEATURE_SHSTK)) { + rdmsrl(MSR_IA32_S_CET, host_s_cet); + /* + * Linux doesn't yet support supervisor shadow stacks (SSS), so + * KVM doesn't save/restore the associated MSRs, i.e. KVM may + * clobber the host values. Yell and refuse to load if SSS is + * unexpectedly enabled, e.g. to avoid crashing the host. + */ + if (WARN_ON_ONCE(host_s_cet & CET_SHSTK_EN)) + return -EIO; + } + x86_emulator_cache = kvm_alloc_emulator_cache(); if (!x86_emulator_cache) { pr_err("failed to allocate cache for x86 emulator\n"); diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 9c19dfb5011d..656107e64c93 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -325,6 +325,7 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu); extern u64 host_xcr0; extern u64 host_xss; extern u64 host_arch_capabilities; +extern u64 host_s_cet; extern struct kvm_caps kvm_caps; -- 2.39.3