Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp457924pxb; Wed, 6 Oct 2021 08:31:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzCrr70Eys3k6s+9JRcKQh21iBzgHUpF6ZyO8U3PIBiHT8ZmjU8H6ZvQb5ZnpQhMuf55WIo X-Received: by 2002:a05:6402:50cc:: with SMTP id h12mr35998683edb.112.1633534264073; Wed, 06 Oct 2021 08:31:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633534264; cv=none; d=google.com; s=arc-20160816; b=I9gMvEojPJDFQFSeOGvsec4r9whVDZEO1cXrmkT2f6uezc0SRiDHovF5KnuIhtM0DC AkFIxwyqCliCoqu3usYR144kiki3x5n60yJFrA/b6PcDE7AtKSm0a+cPCB72Kvtbfefs pnF05XGdX6GfCntaayTUmdXRuP7/P8OOejpPivF+nveMyh1MPsV2uZq5msHt6UqpC3NS nAhXZBEsnaS4apzGUe7+1MgAwl8JSwhXQT+0eFAvdmt20MOTj/AHTN1iRuZdsCxuGrq1 w3egjMeghgh/4EQMaj5o3vs8UOYf3+DMRJ37wQ1yPre2BUXl6zL0p8gYPlpliNv859Fj Fv7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=aNd7PoUhak/3a48Mf+gIve1v0CrGJRlpCPmC8oGfq5o=; b=CCYAekMMCTYFFE5QGd4G8PWPVsfh3ZzV3kCzcgGvs8UMxN42oI2XdTKxwckhuWf6cj WPd90c0YzHkrxOXXcs6kilcj6SM1iqNEGpJ/BgukqUqPMk2rC2Mt2U8MGVUaFlz/p563 wERNBuZBrAifineqr74zzMRYMPXkUU049JSoW8qwxYPz5BLIFQe8+5ZPfRtBSELQjepx 5kdHc3z3luxEtm6lAXQIUKDtBRNesi2hnII7HuY98Ood7ABrJe8HG+5tvK98gGSwPmNR vQFVVw30Et0gPkrktKCI9KE8Q2yrPBrkE/iiwEltzgX2/+GPGPP+mIXjJwyHnyeV14c7 8GKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=UjEVimTo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c30si27802193ejj.786.2021.10.06.08.30.32; Wed, 06 Oct 2021 08:31:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@alien8.de header.s=dkim header.b=UjEVimTo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=alien8.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239175AbhJFP2c (ORCPT + 99 others); Wed, 6 Oct 2021 11:28:32 -0400 Received: from mail.skyhub.de ([5.9.137.197]:55542 "EHLO mail.skyhub.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231671AbhJFP2b (ORCPT ); Wed, 6 Oct 2021 11:28:31 -0400 Received: from zn.tnic (p200300ec2f0d3600bdb6aa8687ce684e.dip0.t-ipconnect.de [IPv6:2003:ec:2f0d:3600:bdb6:aa86:87ce:684e]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.skyhub.de (SuperMail on ZX Spectrum 128k) with ESMTPSA id 769691EC0409; Wed, 6 Oct 2021 17:26:37 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alien8.de; s=dkim; t=1633533997; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references; bh=aNd7PoUhak/3a48Mf+gIve1v0CrGJRlpCPmC8oGfq5o=; b=UjEVimTo/HVnt6sg3Pq2epKgy04bCmL8r+mloo7FtgZcph1HrHVP8FyYySdc6qgQCiT4n6 Mp2lFnB4DcsffwPXRpZrMGHrCup3QHU3Q3wg9i5pariyRsUZ5Y8NuZRRN+rkrulByv2GhP b5QKBtNLVwW4MzuKzlvEb8ykP9W73ko= Date: Wed, 6 Oct 2021 17:26:33 +0200 From: Borislav Petkov To: Kuppuswamy Sathyanarayanan Cc: Thomas Gleixner , Ingo Molnar , x86@kernel.org, Paolo Bonzini , David Hildenbrand , Andrea Arcangeli , Josh Poimboeuf , Juergen Gross , Deep Shah , VMware Inc , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Peter H Anvin , Dave Hansen , Tony Luck , Dan Williams , Andi Kleen , Kirill Shutemov , Sean Christopherson , Kuppuswamy Sathyanarayanan , linux-kernel@vger.kernel.org Subject: Re: [PATCH v8 03/11] x86/cpufeatures: Add TDX Guest CPU feature Message-ID: References: <20211005025205.1784480-1-sathyanarayanan.kuppuswamy@linux.intel.com> <20211005025205.1784480-4-sathyanarayanan.kuppuswamy@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20211005025205.1784480-4-sathyanarayanan.kuppuswamy@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 04, 2021 at 07:51:57PM -0700, Kuppuswamy Sathyanarayanan wrote: > diff --git a/arch/x86/kernel/tdx.c b/arch/x86/kernel/tdx.c > new file mode 100644 > index 000000000000..ad3ff5925153 > --- /dev/null > +++ b/arch/x86/kernel/tdx.c > @@ -0,0 +1,40 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (C) 2020 Intel Corporation */ > + > +#undef pr_fmt > +#define pr_fmt(fmt) "tdx: " fmt > + > +#include > + > +/* > + * Allocate it in the data region to avoid zeroing it during > + * BSS initialization. It is mainly used in cc_platform_has() > + * call during early boot call. > + */ > +u64 __section(".data") is_tdx_guest = 0; > + > +static void __init is_tdx_guest_init(void) > +{ > + u32 eax, sig[3]; > + > + if (cpuid_eax(0) < TDX_CPUID_LEAF_ID) { > + is_tdx_guest = 0; > + return; > + } > + > + cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]); > + > + is_tdx_guest = !memcmp("IntelTDX ", sig, 12); > +} > + > +void __init tdx_early_init(void) > +{ > + is_tdx_guest_init(); > + > + if (!is_tdx_guest) > + return; > + > + setup_force_cpu_cap(X86_FEATURE_TDX_GUEST); > + > + pr_info("Guest initialized\n"); > +} > -- What I meant was this (untested of course). is_tdx_guest() is the accessor external code queries and you cache the detected value in tdx_guest so that the one after the first one is cheap. /* * Allocate it in the data region to avoid zeroing it during * BSS initialization. It is mainly used in cc_platform_has() * call during early boot call. * * States whether the kernel is running as a TDX guest. */ static int tdx_guest __ro_after_init = -1; bool is_tdx_guest(void) { u32 eax, sig[3]; if (tdx_guest >= 0) return tdx_guest; if (cpuid_eax(0) < TDX_CPUID_LEAF_ID) { tdx_guest = 0; return false; } cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax, &sig[0], &sig[2], &sig[1]); tdx_guest = !memcmp("IntelTDX ", sig, 12); return tdx_guest; } void __init tdx_early_init(void) { if (!is_tdx_guest()) return; setup_force_cpu_cap(X86_FEATURE_TDX_GUEST); pr_info("Guest initialized\n"); } -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette