Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp4721052iog; Wed, 22 Jun 2022 04:40:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1u8Iw6X7JpesqxlTsEW4VuQ3nMYskUxXlRd64NQkvBN41zB+NCqcKjr+qVbOEgfcj3hcpun X-Received: by 2002:a17:906:749b:b0:722:d9ce:fac3 with SMTP id e27-20020a170906749b00b00722d9cefac3mr2671987ejl.699.1655898041299; Wed, 22 Jun 2022 04:40:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655898041; cv=none; d=google.com; s=arc-20160816; b=JPmqpr+25LvfIOtG0WWRQ+MVw7wyoT4X1dEFktrx1qOo4E+2Ud4nTuPpAhgrMjc8yJ qpd/ddEg13D2fgThvh4QlgD4uvD1wBb1fHRk0o/pcbNMT35v9ROILeGFlnn6Lr5dokty i+mGupmgsdJVBN+upYX5+JOazdEDICjnHTj//KaouS9EBL0oKmwsnb6VC2HBmnLHplXF Lt/+EPodKQzWBGav3ughBPxbeW1aoH+niW3zWg0trr+NrKe6MM/ZPoiiTuTyYJzqheko 6W2nO9DqVm9hnkhQWaCqOv5fBiew5AW2lImHk7Fb7aEXAYBtT2flZU4HHHoZZ9LiRbrf 19Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=x6hSQ1RECc78I5TmvxwAvnUGp8q2hiuht97FUFvXBRg=; b=DKyZ8tcBGI/aCu9/7aijIUlJgG5rS230SC5FCA8Z3z376YteJid4P16Ry4el4duNgS fFsyQq/v6Fwq+5FRQQ//0pCWGAwQ50anl1NWY35KlZk83S0TonKUzTmPSZyG1w785YGR RFYAJwcNtZFm3vf+V83m4FZlNr8rqRdSFht27ad1IB0e+ZKyFpf46bsZwz2ni7iE5k0C YAWxxdkWor93kbuLGc9paSz3GdMSthkTMUB4F4qbCSByq120tpDF+iqACg5zqaosswkU o18zlAANhQlaXoQI8KEnQK/GFF+AygRR7yAUSw2pcbFITLPCW4CXgp77c7A8ArZ7ucqb V1PQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=afAakwIk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t30-20020a056402241e00b0043572af468asi3281651eda.522.2022.06.22.04.40.16; Wed, 22 Jun 2022 04:40:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=afAakwIk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357389AbiFVLRj (ORCPT + 99 others); Wed, 22 Jun 2022 07:17:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357396AbiFVLRM (ORCPT ); Wed, 22 Jun 2022 07:17:12 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D5863C736; Wed, 22 Jun 2022 04:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1655896628; x=1687432628; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AQ+lrHitEV8+b6N1UB3Rz8HQXBcp9HIoxbqfRTWJ56w=; b=afAakwIkQSYZvhGheI+E9H/d9pPY/jdMIhrO7Fhf+wFsPxcetVMPStCk aE/BKHB8ptUGXSeSBJxNOWeGrB3CkdtzMc4wuqCP5L2G4/qexJ46UQeXt OOLAzTTrHSFEfT924TYNoRCeNTXkfj/iZ8ktahRFqIqH//zIGPNEIUnXv gnD3jNDRCR+bzFJ0sTqnqfVwS36W/+rFyCu949UA/6if5W+UW2DS0LKFA 8IJwwvPFVGbdxkddCgv6Shr0ZyEltBvpj0UTCkzlIv06PHBXQox5a9UZB XBoInMXLy3ZvcKjDXnPbxQRurlqMScxOXhbrObSy3UGkdd6mmYSLouRo2 A==; X-IronPort-AV: E=McAfee;i="6400,9594,10385"; a="344380030" X-IronPort-AV: E=Sophos;i="5.92,212,1650956400"; d="scan'208";a="344380030" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2022 04:17:08 -0700 X-IronPort-AV: E=Sophos;i="5.92,212,1650956400"; d="scan'208";a="834065780" Received: from jmatsis-mobl.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.209.178.197]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jun 2022 04:17:04 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, len.brown@intel.com, tony.luck@intel.com, rafael.j.wysocki@intel.com, reinette.chatre@intel.com, dan.j.williams@intel.com, peterz@infradead.org, ak@linux.intel.com, kirill.shutemov@linux.intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, isaku.yamahata@intel.com, kai.huang@intel.com Subject: [PATCH v5 09/22] x86/virt/tdx: Detect TDX module by doing module global initialization Date: Wed, 22 Jun 2022 23:16:32 +1200 Message-Id: <168253372035629fda418628af278a1c3044cda6.1655894131.git.kai.huang@intel.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org So far the TDX module hasn't been detected yet. __seamcall() returns TDX_SEAMCALL_VMFAILINVALID when the target SEAM software module is not loaded. Just use __seamcall() to the TDX module to detect the TDX module. The first step of initializing the module is to call TDH.SYS.INIT once on any logical cpu to do module global initialization. Just use it to detect the module since it needs to be done anyway. Signed-off-by: Kai Huang --- - v3 -> v5 (no feedback on v4): - Add detecting TDX module. --- arch/x86/virt/vmx/tdx/tdx.c | 39 +++++++++++++++++++++++++++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 1 + 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 31ce4522100a..de4efc16ed45 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -180,6 +180,21 @@ static void seamcall_on_each_cpu(struct seamcall_ctx *sc) on_each_cpu(seamcall_smp_call_function, sc, true); } +/* + * Do TDX module global initialization. It also detects whether the + * module has been loaded or not. + */ +static int tdx_module_init_global(void) +{ + u64 ret; + + ret = seamcall(TDH_SYS_INIT, 0, 0, 0, 0, NULL); + if (ret == TDX_SEAMCALL_VMFAILINVALID) + return -ENODEV; + + return ret ? -EFAULT : 0; +} + /* * Detect and initialize the TDX module. * @@ -189,8 +204,28 @@ static void seamcall_on_each_cpu(struct seamcall_ctx *sc) */ static int init_tdx_module(void) { - /* The TDX module hasn't been detected */ - return -ENODEV; + int ret; + + /* + * Whether the TDX module is loaded is still unknown. SEAMCALL + * instruction fails with VMfailInvalid if the target SEAM + * software module is not loaded, so it can be used to detect the + * module. + * + * The first step of initializing the TDX module is module global + * initialization. Just use it to detect the module. + */ + ret = tdx_module_init_global(); + if (ret) + goto out; + + /* + * Return -EINVAL until all steps of TDX module initialization + * process are done. + */ + ret = -EINVAL; +out: + return ret; } static void shutdown_tdx_module(void) diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index 95d4eb884134..9e694789eb91 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -49,6 +49,7 @@ /* * TDX module SEAMCALL leaf functions */ +#define TDH_SYS_INIT 33 #define TDH_SYS_LP_SHUTDOWN 44 /* -- 2.36.1