Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp470304pxb; Wed, 6 Oct 2021 08:46:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzeoSMnf197vHYz4NKhYGhcUt7MvhKY2x7d3S9m949tS6KACp30EkzGj5h6LqnGdjZbq2Jj X-Received: by 2002:a17:907:a061:: with SMTP id ia1mr31373134ejc.372.1633535174228; Wed, 06 Oct 2021 08:46:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633535174; cv=none; d=google.com; s=arc-20160816; b=pxS13wSVtxEcyQJo3zmhehLQynNV0p/AfWj4NfVRdHboy+E5R70aX78dn9ycj3s71J McL11rtRsonWAQHl7rdxIxL4xjEpl+wF6uAkCSRDEUV9V7u5tbaXQEksa5QAo+5YaOJS 8Zmd54/jHEmFHDUAVUU0e9B7gZIjKuutdxb6AFHhxUH/8DcxBxm5/yjGPfBfKwu9Ll0c QkhMccpZOiOejqeWapCDAqUPHmZ5XOozCkUY7q3ZOKfe2Jem09EL+NmbSNpBLjmO2MDM +g/GFAaU1UV196eqp+mn8C9ijPERzjA77f+QzMAf38Xv8rlNjjUjbnIgDcGTei9Lxuze 2tGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=TWdtQWcIf673X0SHndqTOwCb+dZ89S+sGkd+0+QDJVs=; b=XxiINQso09DsF5/TPJHY0B6Hvjk6Gdw0zL+sZhgh5PiDxGbf9i5HQnicZ18kaWzQ1+ 5h2B6+h63U8n6PZ6m60slO4fg4Bix9GpcTCCvO+JohKaHjLfwMBgpn9H59gJlpkphjlO AlXGtNW7sQh3BXegYhcoiseBUjElnQZA0O6+klCHgxUArhEHRe6GL85/NUg8OvSAnVM+ gjlM3Ca4t0NuG31FJAT69BKob81K+U4M57vzcQHg9psqvqYgzDjoMPkZBk3VuBKNlee3 FrKi0qjqctkkRwo/mhsh4/PozPdILJrWMZSDSGtiqq9OBIUiN7d0T2kuzasobEWxRMBX dJnQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p7si25400623ejl.82.2021.10.06.08.45.49; Wed, 06 Oct 2021 08:46:14 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238994AbhJFPpn (ORCPT + 99 others); Wed, 6 Oct 2021 11:45:43 -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