Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6265050ybi; Wed, 31 Jul 2019 11:10:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqzpfqmr5INLutuEwW+QxaJll7gn+bHmEMEtCnsuqh/bSGoglduOIfcEw706HRyqmJnyZJ/2 X-Received: by 2002:a17:902:100a:: with SMTP id b10mr80476656pla.338.1564596642131; Wed, 31 Jul 2019 11:10:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564596642; cv=none; d=google.com; s=arc-20160816; b=KNwAq72LLLiCizB456wGHwfHvYlyIOHERpZAssdFsWdcC5P0BbRPmr+q6qr8TQ1t7d J7s4QxzorrownMWYP6qRKp0eMd4o4evC8/5sbFyq7QyU90g/9UvStoJuHynVlUYZIlx9 vsAXMvkJwVRXzjFn0IfB+mS/35iWEmMcpWpNrss0DDawwbk5ZwPq0+p6H1yI2QoarUSC ziX2zA7hO9ONPUQ1YSz77O57GZZdo0JAA1Mn37X98zL1hj8xXSIKP/QuTTlIs62/2Qo0 iKq5RwRDwKHWgcskwK3fE/Qum0N4afxHuiyB/AWcEhqeTBX+Zwcy9X1BnWWz2ZyQV3K7 tkag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2O79cZSevj0fRjj98yOOKrmJ0HJlarDgQ5rQqTlE+Vc=; b=uTDcz3pE9pMaADRgDNyNHufxBNESGtgGPPA0ZBOyw0EaBfyuMs3UK+4srN5F5qAixw 7gpqh9G4pgU0Jjis8bEz+5KsvQqFm/A8JJKRgr7uVWIVj47ZT2kZhITOBb2BxRVxplgq GHA5rpbkfFBaiXNZsptWwVjg3aayaxUTah/OeULsuzJViWsh3nhCh4q+fsl4Zk2hy1jI sjJhe7H/fQ+8sX+864Tf/jURzPVAOZ9yRJmj3g5N4vrxNTaKWhHg5xfHjxmXjIFev1Rd FvjanlpcOk1ExrKxpjarBxC6nWKgiGtuMuhHVWCR+rm9pjcfTVl4AFGr+UjOuX9gKOFI XnSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=owT4iRH2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w12si33221302pgs.364.2019.07.31.11.10.26; Wed, 31 Jul 2019 11:10:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=owT4iRH2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388561AbfGaPI1 (ORCPT + 99 others); Wed, 31 Jul 2019 11:08:27 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:35918 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388484AbfGaPIZ (ORCPT ); Wed, 31 Jul 2019 11:08:25 -0400 Received: by mail-ed1-f67.google.com with SMTP id k21so66032896edq.3 for ; Wed, 31 Jul 2019 08:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2O79cZSevj0fRjj98yOOKrmJ0HJlarDgQ5rQqTlE+Vc=; b=owT4iRH22mETHlgcegYuhOK9u2nliY5q2VeZyUqXWoHHX/n19x6syg9GnDrIywB6W7 APi/TtXQtb/nyf25clrFAdZFZGzagMUnv2Z+2cnjWF04cbPyOCM49Gksu1AvQeUUnvR7 XUl2UNWMozLrrjHv201FJREyJgY9narxPLb1H5CawrRm8y//HMtVsyE9d/mxsDfkAKPA enOpyJ4Rl3qA1WMr8I6NZp5PsirfuPoIKC32e6w353HdSR4LEILwTFl4IFSdfBVVRSfh STKHa9Io28Fnozh/5ryrT10P9Qc9Ssl4F2PLmi5mOWUpEijdd0/vEnnTb2ET+50iJgoQ 809g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2O79cZSevj0fRjj98yOOKrmJ0HJlarDgQ5rQqTlE+Vc=; b=T5ADEs84YjZaW8b2RZzxQnHZbwr2IJgJEEluhCmCMixNRkz4FC/DKv3hv4NTjANaB/ 1asDou0N9ndQIyILOJKj4uCmNl39b14vb+JnJCEHZ5DQyVepKQFfQcYt3rrloBpVePkP eguY0UDd3FilAk0KqDcc2JzmsEJmzNNEG8Z7AV+9nQZWLl8bHp7sbEoFm/O0WXkwX72g GZijqG8htZ/9limK1b+2ePJQR4WPMQRPzIn6blb7orMtH3KY/MJ1S2Gj/nWa/EXzMhav sZQNY9DuxtLLXhYRArdiATyJPt6ogh80m7f3Xijoe97cWPOhAvh7OP87czZyYt0GzxKs 6lBA== X-Gm-Message-State: APjAAAXQ34l9ealNZrYnda2XKsNudhRWYpDwl/uGSMcTx9SV7fgmzzZI LNBdETbjRSYUNBnqBDmUGSc= X-Received: by 2002:a17:906:1105:: with SMTP id h5mr26111047eja.53.1564585702584; Wed, 31 Jul 2019 08:08:22 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id k11sm16516389edq.54.2019.07.31.08.08.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:08:19 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 25CB810131F; Wed, 31 Jul 2019 18:08:16 +0300 (+03) To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" Subject: [PATCHv2 08/59] x86/mm: Introduce helpers to read number, shift and mask of KeyIDs Date: Wed, 31 Jul 2019 18:07:22 +0300 Message-Id: <20190731150813.26289-9-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> References: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org mktme_nr_keyids() returns the number of KeyIDs available for MKTME, excluding KeyID zero which used by TME. MKTME KeyIDs start from 1. mktme_keyid_shift() returns the shift of KeyID within physical address. mktme_keyid_mask() returns the mask to extract KeyID from physical address. Signed-off-by: Kirill A. Shutemov --- arch/x86/include/asm/mktme.h | 19 +++++++++++++++++++ arch/x86/kernel/cpu/intel.c | 15 ++++++++++++--- arch/x86/mm/Makefile | 2 ++ arch/x86/mm/mktme.c | 27 +++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 arch/x86/include/asm/mktme.h create mode 100644 arch/x86/mm/mktme.c diff --git a/arch/x86/include/asm/mktme.h b/arch/x86/include/asm/mktme.h new file mode 100644 index 000000000000..b9ba2ea5b600 --- /dev/null +++ b/arch/x86/include/asm/mktme.h @@ -0,0 +1,19 @@ +#ifndef _ASM_X86_MKTME_H +#define _ASM_X86_MKTME_H + +#include + +#ifdef CONFIG_X86_INTEL_MKTME +extern phys_addr_t __mktme_keyid_mask; +extern phys_addr_t mktme_keyid_mask(void); +extern int __mktme_keyid_shift; +extern int mktme_keyid_shift(void); +extern int __mktme_nr_keyids; +extern int mktme_nr_keyids(void); +#else +#define mktme_keyid_mask() ((phys_addr_t)0) +#define mktme_nr_keyids() 0 +#define mktme_keyid_shift() 0 +#endif + +#endif diff --git a/arch/x86/kernel/cpu/intel.c b/arch/x86/kernel/cpu/intel.c index f03eee666761..7ba44825be42 100644 --- a/arch/x86/kernel/cpu/intel.c +++ b/arch/x86/kernel/cpu/intel.c @@ -618,6 +618,9 @@ static void detect_tme(struct cpuinfo_x86 *c) #ifdef CONFIG_X86_INTEL_MKTME if (mktme_status == MKTME_ENABLED && nr_keyids) { + __mktme_nr_keyids = nr_keyids; + __mktme_keyid_shift = c->x86_phys_bits - keyid_bits; + /* * Mask out bits claimed from KeyID from physical address mask. * @@ -625,17 +628,23 @@ static void detect_tme(struct cpuinfo_x86 *c) * and number of bits claimed for KeyID is 6, bits 51:46 of * physical address is unusable. */ - phys_addr_t keyid_mask; + __mktme_keyid_mask = GENMASK_ULL(c->x86_phys_bits - 1, mktme_keyid_shift()); + physical_mask &= ~mktme_keyid_mask(); - keyid_mask = GENMASK_ULL(c->x86_phys_bits - 1, c->x86_phys_bits - keyid_bits); - physical_mask &= ~keyid_mask; } else { /* * Reset __PHYSICAL_MASK. * Maybe needed if there's inconsistent configuation * between CPUs. + * + * FIXME: broken for hotplug. + * We must not allow onlining secondary CPUs with non-matching + * configuration. */ physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1; + __mktme_keyid_mask = 0; + __mktme_keyid_shift = 0; + __mktme_nr_keyids = 0; } #endif diff --git a/arch/x86/mm/Makefile b/arch/x86/mm/Makefile index 84373dc9b341..600d18691876 100644 --- a/arch/x86/mm/Makefile +++ b/arch/x86/mm/Makefile @@ -53,3 +53,5 @@ obj-$(CONFIG_PAGE_TABLE_ISOLATION) += pti.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o + +obj-$(CONFIG_X86_INTEL_MKTME) += mktme.o diff --git a/arch/x86/mm/mktme.c b/arch/x86/mm/mktme.c new file mode 100644 index 000000000000..0f48ef2720cc --- /dev/null +++ b/arch/x86/mm/mktme.c @@ -0,0 +1,27 @@ +#include + +/* Mask to extract KeyID from physical address. */ +phys_addr_t __mktme_keyid_mask; +phys_addr_t mktme_keyid_mask(void) +{ + return __mktme_keyid_mask; +} +EXPORT_SYMBOL_GPL(mktme_keyid_mask); + +/* Shift of KeyID within physical address. */ +int __mktme_keyid_shift; +int mktme_keyid_shift(void) +{ + return __mktme_keyid_shift; +} +EXPORT_SYMBOL_GPL(mktme_keyid_shift); + +/* + * Number of KeyIDs available for MKTME. + * Excludes KeyID-0 which used by TME. MKTME KeyIDs start from 1. + */ +int __mktme_nr_keyids; +int mktme_nr_keyids(void) +{ + return __mktme_nr_keyids; +} -- 2.21.0