From patchwork Wed Mar 24 17:04:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 408310 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61EFFC4321A for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 420E6619B4 for ; Wed, 24 Mar 2021 17:06:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237185AbhCXRFo (ORCPT ); Wed, 24 Mar 2021 13:05:44 -0400 Received: from mail-dm6nam11on2071.outbound.protection.outlook.com ([40.107.223.71]:41848 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S237063AbhCXRFM (ORCPT ); Wed, 24 Mar 2021 13:05:12 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=d+/2k2gb3mWVNdfDrfABA2eYD0UKZfuJ8t6g4B6dyAJWiZ6WLJGBoOI1Fe1TZov7KJWtro4fk2WfFiEHfej/kKZ2jcx8EVsHAghJhhZxLL7WXTnQs4cuP8Bq2wKrHJgNFBil2TE125SGjXknLnBTASkLGK6J+ips2304XRJjXbFqSkZJSZYFsybdDAmjvG3+wcmw7/2kTZO89dyC+Ewm2Wbs0znNlJ+K5knnndpnBySyyJBWBfgf2/6h7MwQZjZfkT/my63iTAYVQS9aGVJ0ft3q7lJIgdzScALA0LyUkUsIcIr3vKGlMmxcT73nKp0FFMfU9SUzXWoSZbnz4PrpwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Zs710fNLbqRp3/buPF9DtwxvV6+8m/Tb/z97SaRom2U=; b=By8vuOuoNNZlzP67In6kQ13x/NOA+O4dEllQafoJcmm1R775QinMQm8BY+XVoWD7KOAZ0G1EMPolG91r5HXw47lsqnP8a9kyA7Ym1Y8/B7TNI9HnTIABGA3dStqbPAH9txiKwsqasXu2rLI439lgR8DnKdmkTnFaazkWisLfJap7EXJIeh3mylNHOkdxaw8+rGsFhiHkPZejRMkeRaRWmAvZYiyG2Am2Wrwu7bE53A4AzxkprgLMInP/09dNj4W7Ghp7qKqw04wm+HW0iLyVDSxirXKCzFP1ErLHT61gFhNkktoKr4+caZVMcrxu8mJg1Ck/WNmlZkz6AIaGjVtUiQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Zs710fNLbqRp3/buPF9DtwxvV6+8m/Tb/z97SaRom2U=; b=XDLnRY1hToMW5S3NWf5z84gO/9/QUpQL58aGy7xecJTm70wQsYiCbBavlxvSODYnZDBD1Z+nr7pHY6tWcyfbxUdPd85qhnYshQvSPmNRaa1vbZlImGm4W9qEPjfmeeLVcwUGg8ZjYZyrRcGNpKTVwYKjxwgxF+SA5nk8lc2UYaI= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4382.namprd12.prod.outlook.com (2603:10b6:806:9a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25; Wed, 24 Mar 2021 17:05:10 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::30fb:2d6c:a0bf:2f1d%3]) with mapi id 15.20.3955.027; Wed, 24 Mar 2021 17:05:10 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org, linux-crypto@vger.kernel.org Cc: ak@linux.intel.com, herbert@gondor.apana.org.au, Brijesh Singh , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Joerg Roedel , "H. Peter Anvin" , Tony Luck , Dave Hansen , "Peter Zijlstra (Intel)" , Paolo Bonzini , Tom Lendacky , David Rientjes , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson Subject: [RFC Part2 PATCH 23/30] KVM: X86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use Date: Wed, 24 Mar 2021 12:04:29 -0500 Message-Id: <20210324170436.31843-24-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210324170436.31843-1-brijesh.singh@amd.com> References: <20210324170436.31843-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0210.namprd11.prod.outlook.com (2603:10b6:806:1bc::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3977.25 via Frontend Transport; Wed, 24 Mar 2021 17:05:09 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: dde0f0c5-b118-4612-6212-08d8eee6fb4e X-MS-TrafficTypeDiagnostic: SA0PR12MB4382: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nFTVFu+Hm2tfpSIKax+V9qs3u6ZOm5Qrkpz76f6f7X0cE0nti/vm8Tof95aoRYcSlhV+RU/AbTdYsBqoCfNgNehA8MD3ra1FClmXCJR49BNbjjvdFFnvB+ihWqOpkRcvemRHmz/dtLKKFZPt4++323s0zfaFbhWc5QcNwUGy2BRnU8ccukm8itwyoUwZInRl3CH2qYZfNOdJwdr01a9ipZTl4tyLnPov+RaCRvNFljwyK8ypVkA/IrLArppBtnuYEcKYT/2GX0DvJzccObOHd+jWnET5yJcCGH9xSiOsxoYexLnv4vQUz7ccajJ+CRrF3QRSLPiTK/VjfGbeJ180xgPGOtqXwZjpErkNrR/e64h79o+a7prAUYSyBvgQn1idYosyUnLhsLwdlWvqZ5mWo4NbAL4Wee8uwdtM7epFhOiciYt+oKkdcjKvkC2F6UKVA3JAvjmp2VzeKH7CiMk4NS8uuN9b3cI1aLsghJkD7bKMZuGC+hi3sfQQ5Z8Yv2KC62j7Asj6UPWBUIqAqiJLajBqApaHvKPA/LFjfBtCmOLCEkNPnPvZoIR8142b8Gz4jbnnYXyj/vOfO8QatnAf4Zehc7oiIN5S9kGGyQfRqf/jLSduusS9zj5dfk01/dHoX9nuFcbruOiPN5Oi07a4bQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SN6PR12MB2718.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(6666004)(44832011)(66946007)(1076003)(66556008)(66476007)(956004)(36756003)(478600001)(2616005)(83380400001)(8676002)(2906002)(8936002)(26005)(86362001)(5660300002)(52116002)(7696005)(38100700001)(4326008)(54906003)(7416002)(16526019)(6486002)(316002)(186003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 75UguT9FVoQRHZUhxLfC9hiRsnR9DUUGM/xpTG4sbYimyyrVkwpl3fHHqrXee0lZEyHJEMeXDbUBhtNTPd5Y5xcUZfqFdaxTGO0q/hkTBaMkha7dD0F0hdnxlZHbZCaw0zPh7dTg4WsFmDApQXXTguvM/5cal+G50PY8Ak++ygqjfqoXfXONKmMhSQUSbm45iLGJW80fBVdZBsVmoxXSfKLPTnuZmvhgXFvWJ4VvsPCjGrcJFGD3Vnc5Q4yCHbfrnIcvSjj90pWriBOYVWVpubG7b10SOCUIOm7faaEdZ9suU98kOGT9Y5Icu8xdClh0FIg1GWYIEQMRYodJdbJAjKv6y+0gDc+ghSYJN6+yN4P4OrIdbSwlJJcvxsmn4uIqVTKErrSTtyEfWxFD6Iq41R+vhLLLD+KtEmbEZvAe79FeuMabH7rgOVyJoDSKxcQcSbk45yUT96SEHsXG3qcKMGSZUdd7KQDdPojPR5L+9gWxcJWfueSbUuA+LxNRhPBjPBUaE3sv9QHWuVXbtjyqnqXN9kNT/TeERkYIJugU4zVc6BEwd/PeDshC8WSAiUtSmFcC2cpXsgDQHlKnPrv2vFqwWrBF0FVmXrD0FKGKyh+YgiE0gWgu5+l3p1hY1dHuXgvtUa5Klm3ZRkXlGDBNJ5mXskD6XOHSNvTfqps32T4rvmZUMvZcgYny4MbICNxehvMq7RTc2/2e+zq643Vjl6Ywsz6rMK5521RYS8t4jMVZTkyOOw6uEDbXYBxoszhDnEHW7uVZoP93o/juXcrVxs7ve6AElfSsxNaw9f/p826+dlOWI/u80rWZkZYWRl6llHMJaSyzi7XdW41G/LS+HWpCqDhDbJVqVB4ywN8LaYuI7lnVBVu712RUfzAOeYSiDcjhlaOD7zo62GYNNdPk0B4Ba21PKqkv/pCvgz++8dsJ65XDfXItyYFVPRjWinpDcor96EPhzr2x3HARULGHnDnsgU7HpfrIufa26E+n/vuuExqkntaFhqc/Z1Yn6nCsPNOxvPCQXGXx/W3mpKx2/OsWjbKRuWgRuOaZQUWIlcHdbtuM00K53smva4gmKy1USEJAukL2jOF+YM5N3PeHY7mmVZQIbMISd4QkpgJuDLOUBBJCyDE0cdHbjUzi/cJE7MP9yenrSfhg/hRMGalhArbUFVIvWX6/OJxXCWQU1n7D/nCMAszPyijGXqsKxxcex9xOXpQpC8cTNN3RN/OBKpn9kAFHQVjTAW25Cwvp2ri1k65w9ZbASd7HVPfiZ95eG9jP8n2LLdGVlgnWIAzFK6r53QvZZQM/SnFxA4LFaiEBVEZiOctVt5elulbUQ1Jb X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: dde0f0c5-b118-4612-6212-08d8eee6fb4e X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Mar 2021 17:05:10.1010 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: J0pVuxeA1o2eF/Zz4RBpJCm+WnNxASHpTnAGZysMMdx66R0HPBdhS2q0KKbnjSrDX4gCkl9V/P0iO8XUO9g3RQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4382 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The SEV-SNP VMs may call the page state change VMGEXIT to add the GPA as private or shared in the RMP table. The page state change VMGEXIT will contain the RMP page level to be used in the RMP entry. If the page level between the TDP and RMP does not match then, it will result in nested-page-fault (RMP violation). The SEV-SNP VMGEXIT handler will use the kvm_mmu_get_tdp_walk() to get the current page-level in the TDP for the given GPA and calculate a workable page level. If a GPA is mapped as a 4K-page in the TDP, but the guest requested to add the GPA as a 2M in the RMP entry then the 2M request will be broken into 4K-pages to keep the RMP and TDP page-levels in sync. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Joerg Roedel Cc: "H. Peter Anvin" Cc: Tony Luck Cc: Dave Hansen Cc: "Peter Zijlstra (Intel)" Cc: Paolo Bonzini Cc: Tom Lendacky Cc: David Rientjes Cc: Sean Christopherson Cc: Vitaly Kuznetsov Cc: Wanpeng Li Cc: Jim Mattson Cc: x86@kernel.org Cc: kvm@vger.kernel.org Signed-off-by: Brijesh Singh --- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 70dce26a5882..e7c4e55215bf 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -110,6 +110,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, bool prefault); int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int max_level); +bool kvm_mmu_get_tdp_walk(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t *pfn, int *level); static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 err, bool prefault) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 33104943904b..147f22bda6e7 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3828,6 +3828,35 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int m } EXPORT_SYMBOL_GPL(kvm_mmu_map_tdp_page); +bool kvm_mmu_get_tdp_walk(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t *pfn, int *level) +{ + u64 sptes[PT64_ROOT_MAX_LEVEL + 1]; + int leaf, root; + + if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa)) + leaf = kvm_tdp_mmu_get_walk(vcpu, gpa, sptes, &root); + else + leaf = get_walk(vcpu, gpa, sptes, &root); + + if (unlikely(leaf < 0)) + return false; + + /* Check if the leaf SPTE is present */ + if (!is_shadow_present_pte(sptes[leaf])) + return false; + + *pfn = spte_to_pfn(sptes[leaf]); + if (leaf > PG_LEVEL_4K) { + u64 page_mask = KVM_PAGES_PER_HPAGE(leaf) - KVM_PAGES_PER_HPAGE(leaf - 1); + *pfn |= (gpa_to_gfn(gpa) & page_mask); + } + + *level = leaf; + + return true; +} +EXPORT_SYMBOL_GPL(kvm_mmu_get_tdp_walk); + static void nonpaging_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context) {