From patchwork Wed Feb 7 17:26:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 198006 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2408767dyb; Wed, 7 Feb 2024 10:05:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IFrRx9404vd8E5HwuDYs0EBQQ/b8pmWUJzOd7OYlBduZn0hKRQgEjZgYeW8xdDeFP5Di3Q3 X-Received: by 2002:a92:d5cf:0:b0:363:8440:94af with SMTP id d15-20020a92d5cf000000b00363844094afmr6517003ilq.4.1707329145770; Wed, 07 Feb 2024 10:05:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707329145; cv=pass; d=google.com; s=arc-20160816; b=mNewRMOIthZ+vdmAVbt9fKUvKRT8f1ujbxYSCgdmjninRIb26aruDZv+Hwqs4VL8ma 88fqYNRhdVpDjWKCxgnvFwP+luCYT4YRiCnE2B54FZcMO3cY4GCWBJM33WMZO8O887t2 59Lm5ppG8iG/3TK33/GnxwJHCViZjpTrOZesafNwMtBGLuNSxkFVTGZIsgcA99ADO7k6 gCuo7NagnAgfrNmLgoNT5Dtpo/Cs39hB9A5tuHXa/+npW5Ykz14m/54/koxIFfoLEIEm MTJ2tvtsLLNap89Dt1o5epJ5nZChnNA7n2o9wXUDE5g09uhxkXoHKR2E4svJFrRvKBEs tpFw== 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=ItYiO89XXonjbBMGkjDbXnR4McxkibpxA64VrR7S6jg=; fh=9q6zzg5mP9AgQlwhw5vFElFklKMmOQcV0LHONnp9NoY=; b=qlSC5oSOgFWS280MrWZFe7+MjmAbKwSfOtHucwoubjwKCpkkFXFGZoEX/eqQwJD39e BLPT0TXD2/m5U+Xdm/hhL+ixk+qM7WPLRcF7H7Sg843Sldsv6fHf495dWAeaOs+tX6Tu aRSK8BRByE8nkL697M7ZBFJsWTgHJ1FYWy1us9snHjyzCEybh2hQKwf7p7/LYfV5U+G9 Kw3o9ktQ06nJ0WwM/3r1vzw+o9ttmfAGocaJeomiqQaEV9CTxcblQru6e0KD3GzJsUGd VfLQzR8LwW3sIVXArxeu3eRzG1uXegJmjBanmtMsA7wsL7PwEPfNd/O79aqFBYvzMluS r+yA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=OCB9h80F; 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-56866-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56866-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Forwarded-Encrypted: i=2; AJvYcCXPXYGtzToRcwGCxN9bMXu7HqeRmAbSKRoHY/GXwH7ZTmCfGaGViYXlDmpqkzDEIAoUdrBWC49e0EkEV3iV2S/pR3gFpg== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id x29-20020a63b21d000000b005d8c06e15e6si1953281pge.533.2024.02.07.10.05.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 10:05:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-56866-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=OCB9h80F; 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-56866-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-56866-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 1102B2831F5 for ; Wed, 7 Feb 2024 18:05:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8F755131E36; Wed, 7 Feb 2024 17:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="OCB9h80F" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (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 0D10E127B40; Wed, 7 Feb 2024 17:59:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.11 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707328754; cv=none; b=NhpX6zDodKTMSnKnai/ywGt+We0mUna8mztBlNTB1Pwjwsykf6FU96a/uFf88XzvRadnkCA35VbF/FjiXSbzPJRzxXs608/FN5Hk3wifXjycZLmoTdt/whdKisAWWf1UIprwK6eqlhOPSq2MH4o5cWr9US3Y7HBVpjsmvqNgCcI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707328754; c=relaxed/simple; bh=yTi4kr3PlzXNPrwqknVaKsYqhBkec/CNGRO3VuNenIU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D128TCLDPMq5OzUb+E8n/Nz0S6jcS2Kv9j6EOygWpihi5cpIMSfwMLzoPksuDe/un7MgeaZmXZYtGvmdiUpA3a39hP0jNZjosR+15MzmVPocLhOcqr3o1IDykYnRL8egcKSwqJG0jte/dO8VhlO81zbTphxjwNABvaC6hgYhxUY= 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=OCB9h80F; arc=none smtp.client-ip=198.175.65.11 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=1707328749; x=1738864749; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yTi4kr3PlzXNPrwqknVaKsYqhBkec/CNGRO3VuNenIU=; b=OCB9h80F7FAanD2ydW1HwX83AWeHVomZP1XI5gtKVGBrJ6QMXttwHb2+ Hed4Wl0a2WKc+5Kvf74/AsOfCe76wej4ZwEOPCqdNUk14GIljTH2GAXy0 wYhbpIb9sS7eD0W/hC1QtunqirRyCuGqbp+ZtpXpz7d0DI1g8WU97wc++ OCqNNm5+oaBk10c/CiwZiM54hjf4aUzKGFh2gyJLHC7kZQnTMSPXPHPiG vxrMGHwg3Dla5aS2vR68X2F7FQsGonot5rNn9DQse3lFLguRlhI0QC44t qnZWsrdftvuP7K9o84sqA89HvThib+nmZ3tSgCwX6l1cNKen5s/zk+zM1 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10977"; a="11622567" X-IronPort-AV: E=Sophos;i="6.05,251,1701158400"; d="scan'208";a="11622567" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2024 09:58:56 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,251,1701158400"; d="scan'208";a="6020727" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmviesa004.fm.intel.com with ESMTP; 07 Feb 2024 09:58:56 -0800 From: Xin Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, corbet@lwn.net, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, shuah@kernel.org, vkuznets@redhat.com, peterz@infradead.org, ravi.v.shankar@intel.com, xin@zytor.com Subject: [PATCH v2 11/25] KVM: x86: Add kvm_is_fred_enabled() Date: Wed, 7 Feb 2024 09:26:31 -0800 Message-ID: <20240207172646.3981-12-xin3.li@intel.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240207172646.3981-1-xin3.li@intel.com> References: <20240207172646.3981-1-xin3.li@intel.com> 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: 1790264366441302551 X-GMAIL-MSGID: 1790264366441302551 Add kvm_is_fred_enabled() to get if FRED is enabled on a vCPU. Signed-off-by: Xin Li Tested-by: Shan Kang --- Change since v1: * Explain why it is ok to only check CR4.FRED (Chao Gao). --- arch/x86/kvm/kvm_cache_regs.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h index 75eae9c4998a..1d431c703fdf 100644 --- a/arch/x86/kvm/kvm_cache_regs.h +++ b/arch/x86/kvm/kvm_cache_regs.h @@ -187,6 +187,23 @@ static __always_inline bool kvm_is_cr4_bit_set(struct kvm_vcpu *vcpu, return !!kvm_read_cr4_bits(vcpu, cr4_bit); } +/* + * It's enough to check just CR4.FRED (X86_CR4_FRED) to tell if + * a vCPU is running with FRED enabled, because: + * 1) CR4.FRED can be set to 1 only _after_ IA32_EFER.LMA = 1. + * 2) To leave IA-32e mode, CR4.FRED must be cleared first. + * + * More details at FRED Spec 6.0 Section 4.2 Enabling in CR4. + */ +static __always_inline bool kvm_is_fred_enabled(struct kvm_vcpu *vcpu) +{ +#ifdef CONFIG_X86_64 + return kvm_is_cr4_bit_set(vcpu, X86_CR4_FRED); +#else + return false; +#endif +} + static inline ulong kvm_read_cr3(struct kvm_vcpu *vcpu) { if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))