Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4119826rwb; Sun, 7 Aug 2022 15:37:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR7AWZzTBtL0hI/NLcUaUCTR6NhLc2AJQAkMFIsxn27vHnFiJRr1YPnNXzOSUfPZyr9zLMtb X-Received: by 2002:a17:902:aa48:b0:16f:1364:788b with SMTP id c8-20020a170902aa4800b0016f1364788bmr16466732plr.109.1659911819951; Sun, 07 Aug 2022 15:36:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659911819; cv=none; d=google.com; s=arc-20160816; b=p/9zKF0mAAZ04dD5JhW+POys83WZdsfs8fHKxx2BPTYbRKApl7oXEj4murXDGNA2/l YC5tx0Uwswe+C3tG48YMErlnUINjl19zGUjUt8wKj2S4lWj78SjvaxATIfaLE9BRY5Ge yJndRIXYtBVEKKsDOgqNLME1hVdYJCXkDRCmxj5Cr0DOkc0yLDrGWBJTqwrHFPnyGPz6 CV4BprmVzTjBMgvK+siJ2Qttje6tphtypq/Pfh2ctEe90bAdMJZRlUWwCBlH6sX16qoI bj4+j9A30oQQK+OSKg+c4vbnI8P7YYefKumq0ZhKETJgP88ukVTjxDZkjWlpGWAtCmZb WNpA== 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=o/h4w/hPLQ8v8FNjrGQKB8HW50C1qKissS5brP1usYw=; b=AY/+9LJ0CaZSxvutqfLNMjrkAL/ybIbdyxW/Y+OMwINXqBjEEUbE/KryMcwPA6SZHe FF5f7PIl1GMa4GXZMI4e39ZBHKya65cNia5DjL7Vn+o9jSrkLLaYut/79lIeZjklVUZU AGJvKA8HsuqovRaDOajl4RACD2+uMpnBWWY1yx4LbmqJ6bB9OjrHK4MfFKD/5l6EyqCi qgR23ukVb0fyHHbRXhT3r7xYJRzYC5/H6HdCunOvsZCvLtWxHRxFPLJM4oxZEijeACsX H4QZQ+lNhTTl3ZQkAF6/Q8nehHTa0vw1SkC6FKakzrLeEiiNOyzmLPa+KEmMIW+Np1sS 3oQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="SV/kZNCD"; 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 f1-20020a056a00228100b0052543a673a0si5775847pfe.93.2022.08.07.15.36.46; Sun, 07 Aug 2022 15:36:59 -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="SV/kZNCD"; 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 S236691AbiHGWEo (ORCPT + 99 others); Sun, 7 Aug 2022 18:04:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232666AbiHGWCh (ORCPT ); Sun, 7 Aug 2022 18:02:37 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5073464C2; Sun, 7 Aug 2022 15:02:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659909756; x=1691445756; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=geG5IFdM92uMkCmaqJr+2phpNb7HquNXFEIpPDeaO4s=; b=SV/kZNCDn01Cija0lYzBdfcdbHY0YMSHJ9fuvhZQzoKwMmwQTxXLCBCE T1+ijtQ6ibTlZUKqBv0x5Sx/caNttZYY+22y82f2+3SImBfpE7mF0VTXv 0AMEq4EVN6X9y8ZnJ41tFedEq1GuOayN/GtepJdqhYbepPDQ2ocmbenMb XBNZ5gk/iHd2s539ttujAJFPluhJrENtC0Ttt+V3+6LwnjTppOJHImyk9 elE4d25wsPm6sC5I3AdwcgyL7fiXU7lHbHa04rIMJuJ0vDNyrGFfiL9yO OkwF+6V9VcH/TpGVycRPhhjavBe4xJ2a3mkobbCcdPjpW60Vc2sX+DEOw Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="289224081" X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="289224081" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:31 -0700 X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="663682497" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:31 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar Subject: [PATCH v8 019/103] x86/cpu: Add helper functions to allocate/free TDX private host key id Date: Sun, 7 Aug 2022 15:01:04 -0700 Message-Id: X-Mailer: git-send-email 2.25.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 From: Isaku Yamahata TDX private host key id is assigned to guest TD. The memory controller encrypts guest TD memory with the assigned TDX private host key id (HIKD). Add helper functions to allocate/free TDX private host key id so that TDX KVM manage it. Also export the global TDX private host key id that is used to encrypt TDX module, its memory and some dynamic data (TDR). When VMM releasing encrypted page to reuse it, the page needs to be flushed with the used host key id. VMM needs the global TDX private host key id to flush such pages TDX module accesses with the global TDX private host key id. Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/tdx.h | 12 ++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 28 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index c887618e3cec..a32e8881e758 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -144,6 +144,16 @@ struct tdsysinfo_struct { bool platform_tdx_enabled(void); int tdx_init(void); const struct tdsysinfo_struct *tdx_get_sysinfo(void); +/* + * Key id globally used by TDX module: TDX module maps TDR with this TDX global + * key id. TDR includes key id assigned to the TD. Then TDX module maps other + * TD-related pages with the assigned key id. TDR requires this TDX global key + * id for cache flush unlike other TD-related pages. + */ +extern u32 tdx_global_keyid __read_mostly; +int tdx_keyid_alloc(void); +void tdx_keyid_free(int keyid); + u64 __seamcall(u64 op, u64 rcx, u64 rdx, u64 r8, u64 r9, struct tdx_module_output *out); #else /* !CONFIG_INTEL_TDX_HOST */ @@ -151,6 +161,8 @@ static inline bool platform_tdx_enabled(void) { return false; } static inline int tdx_init(void) { return -ENODEV; } struct tdsysinfo_struct; static inline const struct tdsysinfo_struct *tdx_get_sysinfo(void) { return NULL; } +static inline int tdx_keyid_alloc(void) { return -EOPNOTSUPP; } +static inline void tdx_keyid_free(int keyid) { } #endif /* CONFIG_INTEL_TDX_HOST */ #endif /* !__ASSEMBLY__ */ diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 918e79159bbf..2168e6133d45 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -56,7 +56,8 @@ static struct cmr_info tdx_cmr_array[MAX_CMRS] __aligned(CMR_INFO_ARRAY_ALIGNMEN static int tdx_cmr_num; /* TDX module global KeyID. Used in TDH.SYS.CONFIG ABI. */ -static u32 tdx_global_keyid; +u32 tdx_global_keyid __read_mostly; +EXPORT_SYMBOL_GPL(tdx_global_keyid); /* Detect whether CPU supports SEAM */ static int detect_seam(void) @@ -80,6 +81,31 @@ static int detect_seam(void) return 0; } +/* TDX KeyID pool */ +static DEFINE_IDA(tdx_keyid_pool); + +int tdx_keyid_alloc(void) +{ + if (WARN_ON_ONCE(!tdx_keyid_start || !tdx_keyid_num)) + return -EINVAL; + + /* The first keyID is reserved for the global key. */ + return ida_alloc_range(&tdx_keyid_pool, tdx_keyid_start + 1, + tdx_keyid_start + tdx_keyid_num - 1, + GFP_KERNEL); +} +EXPORT_SYMBOL_GPL(tdx_keyid_alloc); + +void tdx_keyid_free(int keyid) +{ + /* keyid = 0 is reserved. */ + if (!keyid || keyid <= 0) + return; + + ida_free(&tdx_keyid_pool, keyid); +} +EXPORT_SYMBOL_GPL(tdx_keyid_free); + static int detect_tdx_keyids(void) { u64 keyid_part; -- 2.25.1