Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp5289259yba; Wed, 8 May 2019 10:44:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqxBtYAQcu7fW6hTdbCmMlnfatNtQ9vdif0aSgZl8t26BPtUJSVj3cAOpEpkK1zjvDeT36Y4 X-Received: by 2002:a63:fc08:: with SMTP id j8mr27534072pgi.432.1557337476664; Wed, 08 May 2019 10:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557337476; cv=none; d=google.com; s=arc-20160816; b=de/DbY5+js/wbTW5GlQr+ohip/bGzYjkxrgKNAjXHI6hRk6PHrg9T3fbUodfOwTF4N FJhPYw1m6oxIBp93G0q/mXmQSL5pNYhj0lUonAbZsPTsqvTGJ7qNJN+H0MxsLXSektBc F5kau4T7jw/4IlFB3d1LRHYndnYFE0pr6d8Te8kQ9n0szcx8MoCJtlj3QyPuwOU/AlMB oEkCope2o44+y20mAOGYD2E+nWBjUpsWZgHVDsu8cAFapnAylnt65S9lvRIZGGV1R9KN 5zJWu26/m9hT0fYXMgCqASJOBi0ts9U0zHZcVSYu1TLsr66R86edXAJKmSqhktB8nXea w3bw== 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; bh=JpFf7qOa/lilxcHmFkzrT9z3goapRz8s5p1AyEi8zXs=; b=MvUlFoWU3nlJ4dpA82+V7iWTxR0lBgM1pAUrqeHA52j2aef72siu0zCjmFMt6SeThk d0P+ApRDBECYQ7YAHXvU0+Y9dq16eR22FUqfVyAyah1nM1x6IFcnwixfJIqwk9VDP0Qm 9+0q0Yorh/AMg7wkHdJxZSwipQwGdYcKlu4x2i0QcS084xaSVr2px2zsElk/q7jitIy4 vcU1fzQpNRhkH6q4Dbi+WsiZhfqv2A9OD8Y0X5cQUfUtF/INRwB8O5lfUAFoBVzFAspN EMLPuv2QalvjHHo5HdWs3bto85NFVL4M716ZUk1A6Y9DFLcKXBhEiGyMrDrUKt68etJR WcOg== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n2si22273433pgp.564.2019.05.08.10.44.21; Wed, 08 May 2019 10:44:36 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727673AbfEHOrU (ORCPT + 99 others); Wed, 8 May 2019 10:47:20 -0400 Received: from mga07.intel.com ([134.134.136.100]:33094 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728451AbfEHOow (ORCPT ); Wed, 8 May 2019 10:44:52 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 May 2019 07:44:50 -0700 X-ExtLoop1: 1 Received: from black.fi.intel.com ([10.237.72.28]) by orsmga007.jf.intel.com with ESMTP; 08 May 2019 07:44:46 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 3BB8410AA; Wed, 8 May 2019 17:44:31 +0300 (EEST) From: "Kirill A. Shutemov" 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: [PATCH, RFC 50/62] kvm, x86, mmu: setup MKTME keyID to spte for given PFN Date: Wed, 8 May 2019 17:44:10 +0300 Message-Id: <20190508144422.13171-51-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190508144422.13171-1-kirill.shutemov@linux.intel.com> References: <20190508144422.13171-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 d9c7b45d231f..bfee0c194161 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c @@ -2899,6 +2899,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, @@ -2945,7 +2961,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.20.1