Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6108298ybi; Wed, 31 Jul 2019 08:28:30 -0700 (PDT) X-Google-Smtp-Source: APXvYqyW0fKruOV8+7DP+VPb7eC6M7n96SB5qSxZui2CJ/ay37aEGmPuX5PR5/KdHdgvrHIXfyWr X-Received: by 2002:aa7:9514:: with SMTP id b20mr49188675pfp.223.1564586910816; Wed, 31 Jul 2019 08:28:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564586910; cv=none; d=google.com; s=arc-20160816; b=f0whlq9o/XxQ6Kzct84pRc7w9ezPR6hay7EQ7uEXiovSJtU0xYakJ1OOPtfBLeDQa7 OO5zQKvXmXYainvnDu+Zx3tt5+TH1SCIz5MFniJmpRESdJ4X2OJ6oVZewiUvBAjGd1PD /mJO+aZq4e7mSrpqAlp03TVXiGDUbopU0T6lMHlns9OhK/i7RI79EdxIa6mVMhlKYEUE vi4KdG3LRrfMdSy6ZtyzExLfCy+vYLGKNtQQeVQg5XVY9vUOmk4AhEP9g9USQBy8Rti/ +WAvbVUB7yLbOI6gCfISR/EUo7tNT96tOBHZOUyp7iNama7G5ReNWXvTs5s4+KM/xvpw PujA== 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=k+U6VIHmZ/fZHykv9hSZy3hTFPe14uQwWZPNGVQf9Wg=; b=wTZQmkgF1eAAmqRTfJl2lJAzzBXjxoIB0RB+OVJsRUvw4+yT16yOVtjvsGHmMRKyYi iXL6PD5WKoqIbrYUQK/f4ZwAHIVyMzzSqqr8ZaIwkYCmyIy4ErcWVA1idsd8JaX+w6sw DM/25mMI59JDwLDB2O6RaeS1V4o/4KFyoOvAWJebWR2VH16ppP6Joh0vkO0CoFdD2Q2o 1U083eXyB2FmKyuiDwwNJbVVl0VmDPNegja9RgU3dmqkvc2FZo0zoUAoHstRtntdQrh3 36x+CCJvhJw9n5XCAYYstxvjEPigr9k13zjQBgtKsGbRiPWXSbFFmYd5cKsIazhPDSK3 Ksrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=bRpitEYF; 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 a15si34420576pfi.48.2019.07.31.08.28.15; Wed, 31 Jul 2019 08:28:30 -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=bRpitEYF; 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 S1729834AbfGaPUE (ORCPT + 99 others); Wed, 31 Jul 2019 11:20:04 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:35262 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729528AbfGaPUA (ORCPT ); Wed, 31 Jul 2019 11:20:00 -0400 Received: by mail-ed1-f65.google.com with SMTP id w20so66030493edd.2 for ; Wed, 31 Jul 2019 08:19:59 -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=k+U6VIHmZ/fZHykv9hSZy3hTFPe14uQwWZPNGVQf9Wg=; b=bRpitEYFAw0iw9F4lWLWOaj8uAivovFwyv4AWNZf1NY1sGLxSiCLjNDzw6evzISUMa GHJXwwWF7WOifVXGeInd0STe8yx3ZYQIBXHrbJkyF+Rm71XJH0Uqn7KhiVn6RKSVi/NH GnIdsyLQWF+U1/Hz7RYNsNvOWkWTR+mduMkRXQK/n0AYcfSL5nkUiUeRr88V6rdddNZa NnYZWfhk4GFsXaFANiQNQQqJaMiCdDw2sC/yGDEkK8d34cXt5u306IrMfDI46uU39c0H ulh3gI4vz6iIiS8U4HduYh7aIdHho4vvV2rbd58qJxpeTNRRIu4peFugSAhril4FyF28 DzHw== 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=k+U6VIHmZ/fZHykv9hSZy3hTFPe14uQwWZPNGVQf9Wg=; b=ujEQJS87rTveHUdAZoC7CaKFWUBlAiBiIQswQZzqYF4O8UlzQLGkxkvN1+LP3kT5mH FsCovaymBpLsPSJU6+u/X2p2ma23vE5rBkQAmZMZkDFDT31++z1CBjwx2zrSbI7rYcyq ub/Krwr8qQNh5md8pheYXGn2gYhbE0mYFfBHGrrpNpw68bcI8m0yycPOlJMOgo28alhZ M69O8cgeTb7kicRj1/UQ9omqpJWsGOYAglG3mU1+it3qsvCIYBCaLMWGNeGZPxqOwEXa Flx38mgLC7HXVTP0alpgQU5NMs+usD5wewexQxKcA2PmOrZCGRA1llgtYjkSS6Jc15SC vUHQ== X-Gm-Message-State: APjAAAUNBbZR9BVskN9GUR2wkKwY2G5T1Ak+qviA3uM3tZC9gs/NcBGp fLP7MhU3zXTjI3kxk8ZWnXs= X-Received: by 2002:a50:9468:: with SMTP id q37mr106511363eda.163.1564586038381; Wed, 31 Jul 2019 08:13:58 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id e43sm17445027ede.62.2019.07.31.08.13.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:13:57 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id 41659104605; Wed, 31 Jul 2019 18:08:17 +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 47/59] kvm, x86, mmu: setup MKTME keyID to spte for given PFN Date: Wed, 31 Jul 2019 18:08:01 +0300 Message-Id: <20190731150813.26289-48-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 From: Kai Huang Setup keyID to SPTE, which will be eventually programmed to shadow MMU or EPT table, according to page's associated keyID, so that guest is able to use correct keyID to access guest memory. Note current shadow_me_mask doesn't suit MKTME's needs, since for MKTME there's no fixed memory encryption mask, but can vary from keyID 1 to maximum keyID, therefore shadow_me_mask remains 0 for MKTME. Signed-off-by: Kai Huang Signed-off-by: Kirill A. Shutemov --- arch/x86/kvm/mmu.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index 8f72526e2f68..b8742e6219f6 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -2936,6 +2936,22 @@ static bool kvm_is_mmio_pfn(kvm_pfn_t pfn) #define SET_SPTE_WRITE_PROTECTED_PT BIT(0) #define SET_SPTE_NEED_REMOTE_TLB_FLUSH BIT(1) +static u64 get_phys_encryption_mask(kvm_pfn_t pfn) +{ +#ifdef CONFIG_X86_INTEL_MKTME + struct page *page; + + if (!pfn_valid(pfn)) + return 0; + + page = pfn_to_page(pfn); + + return ((u64)page_keyid(page)) << mktme_keyid_shift(); +#else + return shadow_me_mask; +#endif +} + static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, unsigned pte_access, int level, gfn_t gfn, kvm_pfn_t pfn, bool speculative, @@ -2982,7 +2998,7 @@ static int set_spte(struct kvm_vcpu *vcpu, u64 *sptep, pte_access &= ~ACC_WRITE_MASK; if (!kvm_is_mmio_pfn(pfn)) - spte |= shadow_me_mask; + spte |= get_phys_encryption_mask(pfn); spte |= (u64)pfn << PAGE_SHIFT; -- 2.21.0