From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D2A7CC433EF for ; Wed, 6 Oct 2021 15:43:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B60AF61056 for ; Wed, 6 Oct 2021 15:43:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239094AbhJFPpo (ORCPT ); Wed, 6 Oct 2021 11:45:44 -0400 Received: from mga17.intel.com ([192.55.52.151]:23453 "EHLO mga17.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230014AbhJFPpn (ORCPT ); Wed, 6 Oct 2021 11:45:43 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10129"; a="206833843" X-IronPort-AV: E=Sophos;i="5.85,352,1624345200"; d="scan'208";a="206833843" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Oct 2021 08:43:50 -0700 X-IronPort-AV: E=Sophos;i="5.85,352,1624345200"; d="scan'208";a="589806021" Received: from ptcotton-mobl1.amr.corp.intel.com (HELO skuppusw-mobl5.amr.corp.intel.com) ([10.212.211.164]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Oct 2021 08:43:50 -0700 Subject: Re: [PATCH v8 03/11] x86/cpufeatures: Add TDX Guest CPU feature To: Borislav Petkov 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 References: <20211005025205.1784480-1-sathyanarayanan.kuppuswamy@linux.intel.com> <20211005025205.1784480-4-sathyanarayanan.kuppuswamy@linux.intel.com> From: "Kuppuswamy, Sathyanarayanan" Message-ID: <328cc0e7-89e7-a1b2-f798-fe758c2c1f4e@linux.intel.com> Date: Wed, 6 Oct 2021 08:43:48 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/6/21 8:26 AM, Borislav Petkov wrote: > 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. Yes. But, Joerg Roedel in his review recommended using variable similar to sme_me_mask to avoid function call in Intel platform in cc_platform_has(). " This causes a function call on every Intel machine this code runs. is there an easier to check whether TDX is enabled, like the sme_me_mask check on AMD? " That's why I have introduced is_tdx_guest global variable in this version. > > /* > * 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"); > } > -- Sathyanarayanan Kuppuswamy Linux Kernel Developer