From patchwork Thu Apr 22 20:55:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12219185 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.8 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,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 9E1E4C433B4 for ; Thu, 22 Apr 2021 20:55:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C6FE6141C for ; Thu, 22 Apr 2021 20:55:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239429AbhDVUz7 (ORCPT ); Thu, 22 Apr 2021 16:55:59 -0400 Received: from mail-mw2nam10on2083.outbound.protection.outlook.com ([40.107.94.83]:4386 "EHLO NAM10-MW2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236851AbhDVUz6 (ORCPT ); Thu, 22 Apr 2021 16:55:58 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ihYzj1KyF+R2qIlmdy+Cbn0r3XvoN/6UGy5Jh5PmRiWAWyaaQ5Tbh8GCbCQmk8y0T7xgtzfHri5zpYL8zZyDmn3Sk/UCunTFGbMNTx/u20dSe2USuADR66lGf9/veKNHSHn1M2PO1hm8JOY4hMVJzKVmv4hRalOGKs0UAG3V09LdMlRWqUYjck60lJ+O02dZDT+LRj2knWgUi2QMeIgv0yQdGVt7t5j4gqK/ojdny7XLPLTFKohg15UrUoLfy41jHJFhk9ZyaRjklSGXNFePMn/wbZpMiCO9gMk1t81ibKjVuZatChn78B6DhfVY0KrOBqftDcpRL3XwHr5KE56r1Q== 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=djYOIZIAe+UUpzeBZtr/RMxOzG5cL+NruPXwZsscQuI=; b=iUr+YNq2lGi2pVeDjwZUXD6aNHOaz84tJEA4ZB6ts3V4+PTMUrzNPqL1B2PF2azGVpeq8or+q1jtJw9/QnEQJ+SPTUWhU0HosJJkVOUkGq9d2iAbhCxqwAnlD5oIj/qm6UiI75D7w23yjUGojIKNMGHuzieLhviHjCm7dTePqY8WgAVdj7W2V9XPdOnih9kYgjaLkh+KkoPoLUJ8wElNR5l1bVc/IVLPImkyrWyA3Fnu9zmVTfMZLPHxJWSvrinrI1Xndy1t0L6n+xGnOCEGFo5sgDtKBTurhcag3PoA+eV9BqP6nJUBr8SRfvk4J9cxoIEyi+h+X4jBBAw80BDpeA== 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=djYOIZIAe+UUpzeBZtr/RMxOzG5cL+NruPXwZsscQuI=; b=rty4HZjtL7o6H++9pS1E3JxG9eRKjgKk08oP3yCxRYUdJHZ0se0cV9tPgkww8fCr9gO+ax8NmTMcJJ2BR7ZNctEr3a76I0kVGn7C8OZ46oDHJMh6vLa/Ozf4/L2++O0+bmJNjLrEF+3G4nl5QP37Vrqi/oTw7o8mJMplUqq/dZs= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SA0PR12MB4510.namprd12.prod.outlook.com (2603:10b6:806:94::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.16; Thu, 22 Apr 2021 20:55:18 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e%7]) with mapi id 15.20.4065.021; Thu, 22 Apr 2021 20:55:18 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, seanjc@google.com, venu.busireddy@oracle.com, brijesh.singh@amd.com Subject: [PATCH 1/4] KVM: x86: Add AMD SEV specific Hypercall3 Date: Thu, 22 Apr 2021 20:55:08 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN4PR0601CA0019.namprd06.prod.outlook.com (2603:10b6:803:2f::29) To SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0601CA0019.namprd06.prod.outlook.com (2603:10b6:803:2f::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21 via Frontend Transport; Thu, 22 Apr 2021 20:55:17 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a707d165-cdba-4cdc-a5c4-08d905d0efa0 X-MS-TrafficTypeDiagnostic: SA0PR12MB4510: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: b+9nIYVDp0t5JD7gkDr9gOFY+McH20zJbHw8juyiHlftpA9d8VKZH1l5AqmO123IyIdzokAax4fF+se7edefGU5PQuEiYc164NmANgcNO2vEuPryCHoycB3hzfoxBxX18VQuz6iJWBAYz6wyjR2Eycph/Dh2lclH/EJjPu99ApWX6lLtsj+cjYJaaMVwRGEjt0y18M4eQQ1C0BzPg4DQKFHH8nSQOTi6wD9j5ppHyh61wn6unp0+3UKHAIxQUd0adChbQRqm2W56xSntphuOT3k74Xex7CBbrffJI2tyaCJpC7huc+NDQnNIWjDv7RP6QRIBPCT8ErdP2MM90kglNdu/Ch3tPfKlQEWE3pgZdOqPkWerOSMKdj0/WfhUru0033z88tnUrSKtExb0Pj+7XaspT4AuCFqARBdGwKhMfVe2dksInO6YChOkRB/Qz7nF/JKBKdnS3t7nBREXlBcZmIA78j+D4zsC9Ibu3CZmhifcNGp5DnCA1YAn3bNK/KkGLf79r18vYzg7tKenkn5mbie5Oybslu4niCIsfI6WksvyhIzmL2dP8t+Lziuhwr7jRW7DKQISskf9JwQLxLnGw6L2FdxggkOzFcma5o82+UmpgqnYO+sndoyZnz0gG2g+hlcwQJ4tW9PLTwka91flFg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2767.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(39860400002)(366004)(346002)(6916009)(66476007)(36756003)(38100700002)(8936002)(956004)(5660300002)(38350700002)(4326008)(6486002)(26005)(16526019)(8676002)(478600001)(186003)(2616005)(7416002)(86362001)(2906002)(66946007)(52116002)(7696005)(316002)(6666004)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?utf-8?q?GmkBqaYFkUISy4aYVGFSYZatUd42DO?= =?utf-8?q?hVjscL7cvHvhVYrcBAGCezpL3D796g7zVFzE5VDH98oQ9kQeUElh3EycMJe1EVsgE?= =?utf-8?q?kHBJKMbO98aSNm/tgpyJ+4FkQrcuPCDyDXt+r0cQEZBQura+ISpF97SeHlJFxoVKq?= =?utf-8?q?bykuLzdruKgHNMbcpOzq6HPAkAeP3f/RmtFFoO1sgzB2E7FtC4/cSouCtoRjLqm0A?= =?utf-8?q?JXh74NARwT6i8eGYjDzukZS1hcOZzqe9qXINBoCMcXfVvxbc16yT821L0VBJTKqm3?= =?utf-8?q?bV3b0Zjfys6gNiNDV84qcT09KwqDe5wgsvsc8RbXNosZABiACd+VtzbyhOZ0xi2FM?= =?utf-8?q?uN4C+mAnNrGE7zndKh7oZhhuWpJhaSD85bhCQiJFimwa2Nm7c4oncyR3vNsvQJdti?= =?utf-8?q?bYRurUhLLTjfBiwEuQ+/aMgRapedPU62PGf8XkRw2B2nk+S/RZmM/KMLTJpTL23ju?= =?utf-8?q?UJcvl5EqX3/XPaf2lC/1t2bU1GbUvW7RywSEP72qisEZNgmo0c6H96TZZVoazt+uW?= =?utf-8?q?DA7UXQfwp1Nm30mKd5i76PkeMS1x0keHUS6AfpVuL63soSS+CfKcUHFlq8skwfQy1?= =?utf-8?q?FbTZ5rA866SYA2g1qQ+EUEqkSqYFBQ33calPdP+GMwaKvTddklydPFCXG9ZOHXCqO?= =?utf-8?q?8YPcT3VYkN+qdEn8jUgym40+OTvox5tXYLQ2f6WkVyvmtz2t0NcOn8Wbiah6bR0yZ?= =?utf-8?q?cv/abpqGWXtDHEoWYzWX3TrJiytfjiES/JkoE0vgAqnKNnmy/b79v+Vig1v4tByuT?= =?utf-8?q?qRsQ6UzsKisV6cTvAYjYvPaCdYYXnbIzBJxH2Au0Qx/25TPR60aD0wSCpItciviIb?= =?utf-8?q?T0NDOaZFofLjB5fjXkj2illmce2hJSq3SMoo8rWP6l9Jcqw9IYSowdjK/UVTiYPdi?= =?utf-8?q?kXhubNwng5C5LAPNuVTLJfE+k6cuQ5/BUPGQRApMejZ5sKGNcwSvhzdyLXhsN+plN?= =?utf-8?q?6TFPvg4Xk9syFMd1L9mRZR/Fa2NgZGTJup56/R6aRO418uZv8cgoTSYXOc3dahDWy?= =?utf-8?q?sISU06qYRu0ldNuhk+nmWXVaNAOFKGW/mAiWvCgZWvAOfqm812Zrhj2jA/RwqhXIo?= =?utf-8?q?I0jQxIq8uWbDiPi0WSYNTEhPywbs6uHzJ/7RGhDwItTx6cYhJZHczBZJuwzapsKgN?= =?utf-8?q?i7SG38popjRbIr2TjtO5SSp+fATtEyedGdqBq/vHhZDW3EZpwqZFRlUnyTZ0HjcLh?= =?utf-8?q?BFsFv9q9bfUaGvNzWGFctcTqT37PmXEwg2qdx+//XH3gfckEOG4JyvWdF0wMViy3z?= =?utf-8?q?CF99AQD2fh7re/?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: a707d165-cdba-4cdc-a5c4-08d905d0efa0 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2021 20:55:18.2514 (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: D0qh8AaygUgNY91Y8mwP1dS7vLYDeW9SC8HHWvik6YxGQSNfMdLvxjjOxyq6luKw7WQXmHrkEY+XTZGLlw/7Sg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4510 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh KVM hypercall framework relies on alternative framework to patch the VMCALL -> VMMCALL on AMD platform. If a hypercall is made before apply_alternative() is called then it defaults to VMCALL. The approach works fine on non SEV guest. A VMCALL would causes #UD, and hypervisor will be able to decode the instruction and do the right things. But when SEV is active, guest memory is encrypted with guest key and hypervisor will not be able to decode the instruction bytes. Add SEV specific hypercall3, it unconditionally uses VMMCALL. The hypercall will be used by the SEV guest to notify encrypted pages to the hypervisor. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Steve Rutherford Reviewed-by: Venu Busireddy Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/kvm_para.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h index 338119852512..bc1b11d057fc 100644 --- a/arch/x86/include/asm/kvm_para.h +++ b/arch/x86/include/asm/kvm_para.h @@ -85,6 +85,18 @@ static inline long kvm_hypercall4(unsigned int nr, unsigned long p1, return ret; } +static inline long kvm_sev_hypercall3(unsigned int nr, unsigned long p1, + unsigned long p2, unsigned long p3) +{ + long ret; + + asm volatile("vmmcall" + : "=a"(ret) + : "a"(nr), "b"(p1), "c"(p2), "d"(p3) + : "memory"); + return ret; +} + #ifdef CONFIG_KVM_GUEST bool kvm_para_available(void); unsigned int kvm_arch_para_features(void); From patchwork Thu Apr 22 20:55:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12219187 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.8 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,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 EE829C43460 for ; Thu, 22 Apr 2021 20:55:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BB08A61417 for ; Thu, 22 Apr 2021 20:55:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239528AbhDVU4O (ORCPT ); Thu, 22 Apr 2021 16:56:14 -0400 Received: from mail-mw2nam10on2075.outbound.protection.outlook.com ([40.107.94.75]:4961 "EHLO NAM10-MW2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236851AbhDVU4N (ORCPT ); Thu, 22 Apr 2021 16:56:13 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TtdfrRDrtSPfTKraQuZXVk9Qztu40JemGRA/WY/sJwF104VtG7M4ilHnJ81Z+tgtZO8X35ULG3eDo9UGZYXTFuVKOd8vONd0nhuHGTRyiZR/kxy8BhawaJJuSJr/HXgAlX++OBMQiJfZiPtXRBRp+4LTFmlptslEivwQwAskn+wbgnPM/PZDrIx8tjUVFGxr33FHfi2ohf0Ai5Pp+KNl5mOvyMDoNQv0LY/N2a+DT6+SOfdiAYlNbCzIw1puJOI4llPZPJ8lhoAz7Wb6zmTGXQH3Z/zGhpZV0FSwvZw/68S+fRnQzDYZ6Lj3TFtmvOkgEeIVHteV7Bne3B2U42K+Qg== 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=V519WsdDsLhQcSitVWrmZPwA4RVtfnxNsUoCkWKP0qw=; b=hhIIvlTcsrg8RdIfC2TBY0CLqULuPWc0vjd/skop7j0vXUNxuQL03iZPlyD1Qn8D5RQkg+DbcaDEIBtISODkscQx2rirbvhvFDVw+A6TaU5zRZAoh/PC1xWMMmkg4mG2zgpUiA3le5jZ/lyZoVdTf/EtohU1cZ7Ww15kX240GFNqLmgD7q0cAfDpLe4Lm9jZF2oVlSbEUP16BmIzCiphYDOyXe+cIvhEuuOOuZC2r4xmmpY/KAMxZuvVU5YcJZgBZClc5c6gU8ixYWKnJIQ3lztCm+3sI/9UsyTcToVCDrAOI8lFzwtlJex+x81RsD5KDLSgzZ+L3DqGFYrgfM4AXg== 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=V519WsdDsLhQcSitVWrmZPwA4RVtfnxNsUoCkWKP0qw=; b=UvOBdf7c3dOI0wR2jqErBASO7Vd6oZ5nJg0gU9/BUIWxIb3xQPIgll5bXCpURRGNFpAkk8m8+GqltZBWJ5jJvLnRgDzksagwnUKEkQlgDv7BryOHEssUnjqcbQZch5cTn1Y2mjWP040+yWAVY4TJh/q7sHzeN9SE5SpNUyM/s0w= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SA0PR12MB4510.namprd12.prod.outlook.com (2603:10b6:806:94::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.16; Thu, 22 Apr 2021 20:55:35 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e%7]) with mapi id 15.20.4065.021; Thu, 22 Apr 2021 20:55:35 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, seanjc@google.com, venu.busireddy@oracle.com, brijesh.singh@amd.com Subject: [PATCH 2/4] mm: x86: Invoke hypercall when page encryption status is changed Date: Thu, 22 Apr 2021 20:55:25 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR13CA0022.namprd13.prod.outlook.com (2603:10b6:806:130::27) To SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SA0PR13CA0022.namprd13.prod.outlook.com (2603:10b6:806:130::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.16 via Frontend Transport; Thu, 22 Apr 2021 20:55:34 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d21adc52-70f0-4b3c-5cc6-08d905d0f9fc X-MS-TrafficTypeDiagnostic: SA0PR12MB4510: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fWDFCkiBbF7ln5wE2hx0lups+YSq9KuRafYU5/6MmQp13/kQISAc5BAjEhbVWsffYsUWm9HecFSQpxLhtlU/IjDXNQPtBGsraDcOJliZYIDRG3ChxtcuSQZ3jNrhz1QIsNyGMwWGJuPO4Zb28n9xRVUZiuysMI1jNYVNzhBjT0sGaVwzLY7UV7qJcT+dx2C/UUpJmVer9i/1W2E1XeB2IIHsRPgGKJbg+VRwkv6DIRH1ueMrLTSAR2HHiLTqdf1gwPqU6TUAiO5AjS9rYSi+1Jl0frnJfAXigC1evvSRjaVVvkidEpzVFnJb62POiA6BjT4DckzAt2bM/15Q80V+OE2pKkaCcn4XuAWYOk7JBiF2i2WFyPxoZ7L3J3A8C7Mv0FWsFZGzdBzvyW3VsrKy6fHYpXey70cDlwsyw4B0w0C0HAZL9ty8b2lUDSfq1L+LdlcMSzT6Flg20wFsnGZsiGeDOD1A/TYd7Ewkl8jL9/GULVSUQth5IvSbsKCi/x3caThrnKJszaQAN6F0zJlC0ul0H+2URMlgzxuxGBxD8TevXQN327mCbkes9wXF7ZYMbXeDh+BGQgEeGVOcVUwWBHY1hn/isKC7u5zbGTC/Hl9mQRTEFDJSwmhJGF2gEDyxUnKfVW83nZdabdeEd8Bp4g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2767.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(39860400002)(346002)(366004)(376002)(396003)(136003)(316002)(7696005)(6666004)(86362001)(2906002)(52116002)(66946007)(66556008)(956004)(8936002)(38350700002)(4326008)(6486002)(5660300002)(66476007)(36756003)(6916009)(38100700002)(186003)(7416002)(2616005)(26005)(16526019)(478600001)(83380400001)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 4DU7N7YTOW4EPj5aeQwfwCWd0KCtrO70bkoJX8gbknWWPU0ogEpHHFNGjdrJCTxdPehDtLKeB3CbUnLspSIdjzHW2+V6iecqE1PZXgMXGENXPU1fWS5bj8jWo3LDUCxJOB99xra+mt2ZyHf7c0dMiGv5kdXje4UfqMJXqk51x6mXYbZlhyFwX/OCLYGv3dTBvqGT7wbdYk+FSS7YbCTXCjG3YdQfBnZY2oNSvCO8oeo0JxfwkmEe2d1XjOs0SOKVpGGUX81juey3EZ7ATf9qm2AuykpH07OK2hsKwSAAj3rn9B5sF15paDUgnG30N2kG6cQLGtF2y8aMy0mYCsfk4+WVIGOnwZ8vHpZBTyI+pnvU3vdZohSn2KCdWqZE5EaUlyGzXqPZkNwc8G7sZG8SQ1abzopW38Xn42Gb8brLt/2n/reRO3GDMm0P2c9dvvLgq1VfRXRna5tO0jheW7Pw1YzQkWmgdp7XpJV7Q/nSs0ge9xhVcNRxNEhNWxCD8NFOhPrZxuCerNiWGJ6+hFqCAalpOyWhRT/etebOp8WRtyvtYfZO1QajWr0kt0+3lTnnp33hdwp2fid7/u362NvtlP0FBk+MMrykTLecgR24ZCU8Hi5DPX/ohvUENijwOtRCerUSCvLq6bp+mgIFjdj8avFnzK/U8xe4MGq1JP+z5+feUbxfyTVD/516vf5ASw2QgQXH4GXfpOgNSLTnQn2LtIPQqwITnLVnnAAK6Di8D6dOMn3y1shIxyWiEUD6Hw7WC58yBJkx+SrDTGw40IH9fmrYL89226jaTndbOZxmhP6UcxIIcUBD9+a0rRJJt3zQJisQmo41IBvRpG3chAAA4tsieCONzRVEUA8zdiXr+uhLGyUZhhjt8DYGbSN8qh4EocI2jDTqHELDMs3MyT5eqGD8oZKBU9xrOI7bUyVR590+Ihdjj2zC+WquTmWthlnrEEuWymjBuoEpV2EIC2CE2xrDaZeaoN2Pfl09OkRc3JesUf7VjxN1kvIIEZKrw+Ye8Ii3+sgvBfOBdd7sfdN4cnBIDhAK5/ibp1aQ4XGaxTU9ajVA2npQQ/T8hNkD+aonm2UfM6yFn0EPCOTFyPPY3r/XG/mvN5iWhGRJ+gXINA0+75kQp0osm7tpLPquqnhKzGMGI3BSdzLcbgxTX7eeOVG+zJ1vgzrq/FZ7e/8epbdryy5sYnxPygGo2ev/NQS5xFHGMEH63CtBUMYDMvPtZEYlMSPa0MOAnJCW4d7HxDdLY5+zFIVw0pin7wxjc5HhSULj6KlRYLcTCADkHgZqpzCJ+u1uZu3kfsSxzbX324Ttugmr0O+lxTT/1uZvoXTr X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d21adc52-70f0-4b3c-5cc6-08d905d0f9fc X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2021 20:55:35.6277 (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: oXdzX9JhXOBl9NxP8GRtzTDJf2MRx9xx8xRbDiI1/DRuf59yBG3fNp2CqBlBPstY1+SfS+ADgGTwQg9WwRCmKQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4510 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Brijesh Singh Invoke a hypercall when a memory region is changed from encrypted -> decrypted and vice versa. Hypervisor needs to know the page encryption status during the guest migration. Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: Paolo Bonzini Cc: Joerg Roedel Cc: Borislav Petkov Cc: Tom Lendacky Cc: x86@kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Reviewed-by: Steve Rutherford Reviewed-by: Venu Busireddy Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/paravirt.h | 6 +++ arch/x86/include/asm/paravirt_types.h | 2 + arch/x86/include/asm/set_memory.h | 2 + arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/mem_encrypt.c | 66 +++++++++++++++++++++++---- arch/x86/mm/pat/set_memory.c | 7 +++ 6 files changed, 75 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h index 4abf110e2243..c0ef13e97f5a 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -84,6 +84,12 @@ static inline void paravirt_arch_exit_mmap(struct mm_struct *mm) PVOP_VCALL1(mmu.exit_mmap, mm); } +static inline void notify_page_enc_status_changed(unsigned long pfn, + int npages, bool enc) +{ + PVOP_VCALL3(mmu.notify_page_enc_status_changed, pfn, npages, enc); +} + #ifdef CONFIG_PARAVIRT_XXL static inline void load_sp0(unsigned long sp0) { diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h index de87087d3bde..7245d08f5500 100644 --- a/arch/x86/include/asm/paravirt_types.h +++ b/arch/x86/include/asm/paravirt_types.h @@ -195,6 +195,8 @@ struct pv_mmu_ops { /* Hook for intercepting the destruction of an mm_struct. */ void (*exit_mmap)(struct mm_struct *mm); + void (*notify_page_enc_status_changed)(unsigned long pfn, int npages, + bool enc); #ifdef CONFIG_PARAVIRT_XXL struct paravirt_callee_save read_cr2; diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index 4352f08bfbb5..ed9cfe062634 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -83,6 +83,8 @@ int set_pages_rw(struct page *page, int numpages); int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); bool kernel_page_present(struct page *page); +void notify_addr_enc_status_changed(unsigned long vaddr, int npages, + bool enc); extern int kernel_set_to_readonly; diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c index c60222ab8ab9..192230247ad7 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -335,6 +335,7 @@ struct paravirt_patch_template pv_ops = { (void (*)(struct mmu_gather *, void *))tlb_remove_page, .mmu.exit_mmap = paravirt_nop, + .mmu.notify_page_enc_status_changed = paravirt_nop, #ifdef CONFIG_PARAVIRT_XXL .mmu.read_cr2 = __PV_IS_CALLEE_SAVE(native_read_cr2), diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 4b01f7dbaf30..e4b94099645b 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -229,29 +229,74 @@ void __init sev_setup_arch(void) swiotlb_adjust_size(size); } -static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc) +static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot) { - pgprot_t old_prot, new_prot; - unsigned long pfn, pa, size; - pte_t new_pte; + unsigned long pfn = 0; + pgprot_t prot; switch (level) { case PG_LEVEL_4K: pfn = pte_pfn(*kpte); - old_prot = pte_pgprot(*kpte); + prot = pte_pgprot(*kpte); break; case PG_LEVEL_2M: pfn = pmd_pfn(*(pmd_t *)kpte); - old_prot = pmd_pgprot(*(pmd_t *)kpte); + prot = pmd_pgprot(*(pmd_t *)kpte); break; case PG_LEVEL_1G: pfn = pud_pfn(*(pud_t *)kpte); - old_prot = pud_pgprot(*(pud_t *)kpte); + prot = pud_pgprot(*(pud_t *)kpte); break; default: - return; + return 0; } + if (ret_prot) + *ret_prot = prot; + + return pfn; +} + +void notify_addr_enc_status_changed(unsigned long vaddr, int npages, + bool enc) +{ +#ifdef CONFIG_PARAVIRT + unsigned long sz = npages << PAGE_SHIFT; + unsigned long vaddr_end = vaddr + sz; + + while (vaddr < vaddr_end) { + int psize, pmask, level; + unsigned long pfn; + pte_t *kpte; + + kpte = lookup_address(vaddr, &level); + if (!kpte || pte_none(*kpte)) + return; + + pfn = pg_level_to_pfn(level, kpte, NULL); + if (!pfn) + continue; + + psize = page_level_size(level); + pmask = page_level_mask(level); + + notify_page_enc_status_changed(pfn, psize >> PAGE_SHIFT, enc); + + vaddr = (vaddr & pmask) + psize; + } +#endif +} + +static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc) +{ + pgprot_t old_prot, new_prot; + unsigned long pfn, pa, size; + pte_t new_pte; + + pfn = pg_level_to_pfn(level, kpte, &old_prot); + if (!pfn) + return; + new_prot = old_prot; if (enc) pgprot_val(new_prot) |= _PAGE_ENC; @@ -286,12 +331,13 @@ static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc) static int __init early_set_memory_enc_dec(unsigned long vaddr, unsigned long size, bool enc) { - unsigned long vaddr_end, vaddr_next; + unsigned long vaddr_end, vaddr_next, start; unsigned long psize, pmask; int split_page_size_mask; int level, ret; pte_t *kpte; + start = vaddr; vaddr_next = vaddr; vaddr_end = vaddr + size; @@ -346,6 +392,8 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr, ret = 0; + notify_addr_enc_status_changed(start, PAGE_ALIGN(size) >> PAGE_SHIFT, + enc); out: __flush_tlb_all(); return ret; diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 16f878c26667..45e65517405a 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2012,6 +2012,13 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) */ cpa_flush(&cpa, 0); + /* + * Notify hypervisor that a given memory range is mapped encrypted + * or decrypted. The hypervisor will use this information during the + * VM migration. + */ + notify_addr_enc_status_changed(addr, numpages, enc); + return ret; } From patchwork Thu Apr 22 20:56:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12219189 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.8 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,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 6F747C433ED for ; Thu, 22 Apr 2021 20:56:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 416D36141C for ; Thu, 22 Apr 2021 20:56:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239584AbhDVU50 (ORCPT ); Thu, 22 Apr 2021 16:57:26 -0400 Received: from mail-bn7nam10on2075.outbound.protection.outlook.com ([40.107.92.75]:36448 "EHLO NAM10-BN7-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S239575AbhDVU5Z (ORCPT ); Thu, 22 Apr 2021 16:57:25 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DiQGfO+XMnMXlBCnoc4Zn057dR6WUY1gvB1LSbZamfcOnXETjiOTt1K68zqB+iy5IdixEEg4xBi8nEkAsPWQskVF3ndA16rQ8Er4eFZmo5AbkP0a1EZ4v3eCKWvXvplXgSf39j+FLZ68gU/76O14mkoxdH/cnyi5BPY0Gb9l6Ga5I6LWKUSOGEbV292+BCDCjdhHlQlgXD9LJI3DAZOD+QHd/MJw6l6Xw/+7yAhDh2JQyfFSKuLmMMhoVbRXlFys3MKN01Kh8+fy40Vk6O2MkgJG2b9MSVwBUXxKr+t0hRiJ25D8xYG/RLFg/vcY+DYaXBDMzfcvs2+vAaKq9u9yoQ== 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=kP40PdU4Z8HHa6wnZVnRT/zhr1u8HNB3yWt0AcQt+JM=; b=g2I66d1BYWz6S1+mhN7QoGgaP5iMisGjFTDxm/7h2XG9QWwuvZG+UYrXEla80sbmjVz7U0ccnjeKvC1KJcgMSRbcySA+cX8hQOKMY871SQUMawMZdip6EPf2vhHA3rap1j/sBwUpzcscBm7ekx2Q7WeIl1byJ0Qp1OmQZTR8TgCUX3IyntzhLGsOCS0dOeD18QVT3uWmZpsw9Br9PnHHr+3dvFKbdv6Qn1EFlmHqm+D0UW6ERyx6ISp5WKpZ8XDYqvBAaXACOklUN9dqJGkhz+y8hdL/VHeljQe4na6EUJ4sExMsjg+f48xqWL3MNzCXcL/4tEJPCt2aIsjnuvWnsw== 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=kP40PdU4Z8HHa6wnZVnRT/zhr1u8HNB3yWt0AcQt+JM=; b=rvaIkUk7wx1SOk7scAiK71P9inIVF3QOiJoV6oBMq6z7iKOW5TGO8rea0FkQ6r46s4+GbL4c1QoinoR+Gpvh1RQaT+iW9K5QIem52ijd5a7OxAzpGIAeigtbC3NTqMTJjWn9ZtLEU0U1VMwpGkdy6frCjOlwZ86/6dKAnk2QNwM= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SA0PR12MB4509.namprd12.prod.outlook.com (2603:10b6:806:9e::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.16; Thu, 22 Apr 2021 20:56:48 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e%7]) with mapi id 15.20.4065.021; Thu, 22 Apr 2021 20:56:48 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, seanjc@google.com, venu.busireddy@oracle.com, brijesh.singh@amd.com Subject: [PATCH 3/4] EFI: Introduce the new AMD Memory Encryption GUID. Date: Thu, 22 Apr 2021 20:56:39 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN4PR0801CA0021.namprd08.prod.outlook.com (2603:10b6:803:29::31) To SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0801CA0021.namprd08.prod.outlook.com (2603:10b6:803:29::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.22 via Frontend Transport; Thu, 22 Apr 2021 20:56:48 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2caacb1a-ec9f-4eba-be95-08d905d125ac X-MS-TrafficTypeDiagnostic: SA0PR12MB4509: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MAhMdgjj9TLObeUyAkXTB2A4PARxou6oE89ClQ3OKKbqphVosleihY+lsKbX/6Tg62NqZ/bpAR/u8VHdA+UkQtIjlMYOtd33JPajByI/PuzPCRAA3DJFx0otNk8mnPkS2nbO3i0NO2vn+lfiJev6/wJaKkVqKOr53z82w/WwHng3LG5hHh3fMNGz05Egeb5DpvWheM7/cbmXEWcJmu6MySOFiKYdts5Wh//hU+KYL1qukADYT33YVLCSRAefmLkZWLtJvO0jKau7jX5ilJlIWFIs5WqeEgvL4E5dFFLV/3USeMuw8tVh58upzIOeSE4RhuddDWtdGQTecDQeHJKvATsedMz5MHyjpaceMwkZB6FOUUXrVGRKKQJCtRiCdx7f1Xamjq3lgMkblmaX9UsOSMOPE9llg8O07VUuCWRF/EX2SMF0phcKByMQi3+5UXvljWivQBsu6QDfKH9kcwn7WvRhqVSIpC3jdYLknxtM+ULLy1ha9IlyskcUPVqxWvol599T0dkFCO5OQ49ZtGbXzgSG5nArQnSSGjaDCmpQXaaPdDKbtRuf6wfd/Gk4Hd/qd+Xsl/TTCTLSFAOxb12n+1jvk39VlhgapF3J/JHfa+DBMq9pjEQSx19uoFYOX78zE4ym063FwU+87iMC1BcALg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2767.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(136003)(346002)(376002)(39860400002)(396003)(8936002)(7696005)(26005)(8676002)(2906002)(186003)(66946007)(86362001)(38100700002)(83380400001)(6666004)(6486002)(7416002)(16526019)(66556008)(36756003)(6916009)(52116002)(4326008)(5660300002)(956004)(478600001)(66476007)(38350700002)(2616005)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: frq3yls1anxbELJrSvA8UaF30UsrYd5uw1Yh/C3Dlhuw1TE/jmKfFWgRKq8q2PmfzrMEntSyDHPtefot4RwPO7Mvi7Nwi1pCRewuhqbLYIJk/ecFpNWUotDEzXp37q/qfLvYVLItfsgo14+qJksUrQH7I23fEOYMgyz/kmplWtihg9KMcyA+O/Qqf/ISXPiw7dB7nnbGxgFHPe+tUVjY/poDr9idPwJIG7pPbGmwSoAiQRJ11YzTGnfZGS4wl9bnVkxru7uB8UzevW1jmGn2BauZ5jmeb9tYF5C84lXEMu6udFVRgC0fyDtnglg7dfjOk8WzMrcSh2TlAkjlrBYz+9BE7z0u4n3s2Wm/vzAfsfAbDP8DdgCqtajN4EYMT9nLTCgd4ndIrmiUW6tIrpaIHuPfE6AONgrB2H0LcX7DW+KVtpdMQQu4bbald4rIrHjZJbYqNWHXsnT1EMB7eFMMTPlcRKxJVN2RhYjIhYhoqSd27uz4iBm7/ssPckJ+/DIgvmqmMl4RLuhwy3YaLiRFAKxVbpwNL4WtXXi1EZpR89MEYNnZRruzvNZ+z/EG4PcSEKSDKWIpZ8P5MiWKLs7wVx4gIll8c78UthMXYXQjygrJJ49ChX+JgkzqnvgqMFF/+OMeiknEo8QurG4tBzIbv492uMu8QsbQFGdU3tiNjsg65jevuZp3p/2hbJtleYNFVdTGTSuuOrhJ0jEKR2iUX1VDQPHs2SNKS02IE+TVhFpqgWPoPh6qOIxQ7Me3UIpby1dK9TrTIWjEVznZ16SpSS/8BUn2yjGcg6ERKL/FVbrFDBijKf5k+1R36NczGWl/saf0tIOvvkKU9MwaidWNqBTyH3FHNsqZwi+5EHdTv5t7/OjjWvzqo0tlzS9buHqFxlQiUHrQHr2/1g7MT2dczR8Y1/MGDvidXCbFrOdDryiJZSwjaf5TC4z7wwSAxOyMAh/xT9/ahq0ungQsaAGKXXLbxpFJmtxUBvQynhz18EG92U6c3bssfbqAZ10GXVgOOQiWrvoYkD+6TOVlP+U0at4DPiE5AiuN107E8FBp6FxrF70dT07WFGIQDv+yR6jhrBag2x27LN0h0xayvSt7RyANQ95faq1rL3p3y4F0mSXouVsWbWva40AGcVsarWotCaQkhkbHNhX7t2Otwj1x5OirJQZqU7TEPKzPAMLLpMT63j+6C9oyzN339NdJnQA9O/E+LxrCNzjCrmKt/aSwSMiShMrq/P/Zyc1jia4O63YpJxpriLCm1eg/6uIiriExsc1Y6V7g83hJMndzUFACumXGH0xj/BvcyH/JRl39EQggdbX3hN8f9hrHLty9eiGo X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2caacb1a-ec9f-4eba-be95-08d905d125ac X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2021 20:56:48.9106 (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: AwmQ1xedRhQj7755Z6eOomt7ub8NLVfQaWh5zFHqrINRqSa4QCs1+ZcYXyQ1SDjGyYvOywXr/y8hE0QFZZK2Iw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4509 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Ashish Kalra Introduce a new AMD Memory Encryption GUID which is currently used for defining a new UEFI environment variable which indicates UEFI/OVMF support for the SEV live migration feature. This variable is setup when UEFI/OVMF detects host/hypervisor support for SEV live migration and later this variable is read by the kernel using EFI runtime services to verify if OVMF supports the live migration feature. Signed-off-by: Ashish Kalra --- include/linux/efi.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/linux/efi.h b/include/linux/efi.h index 8710f5710c1d..e95c144d1d02 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -360,6 +360,7 @@ void efi_native_runtime_setup(void); /* OEM GUIDs */ #define DELLEMC_EFI_RCI2_TABLE_GUID EFI_GUID(0x2d9f28a2, 0xa886, 0x456a, 0x97, 0xa8, 0xf1, 0x1e, 0xf2, 0x4f, 0xf4, 0x55) +#define MEM_ENCRYPT_GUID EFI_GUID(0x0cf29b71, 0x9e51, 0x433a, 0xa3, 0xb7, 0x81, 0xf3, 0xab, 0x16, 0xb8, 0x75) typedef struct { efi_guid_t guid; From patchwork Thu Apr 22 20:56:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12219191 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.8 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,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 3C59DC433ED for ; Thu, 22 Apr 2021 20:57:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F188E6141C for ; Thu, 22 Apr 2021 20:57:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239400AbhDVU5q (ORCPT ); Thu, 22 Apr 2021 16:57:46 -0400 Received: from mail-eopbgr680056.outbound.protection.outlook.com ([40.107.68.56]:4846 "EHLO NAM04-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S236851AbhDVU5q (ORCPT ); Thu, 22 Apr 2021 16:57:46 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I5nWpWHry7HyDa7mLjXaKsqXySnnn+Y9XfwAqcDT9JoNRg8mDU5Ja1NhKu60ri/7Sim7l9Rv12IMmGYi7x8ouEWVxVO1mPaE6Y4sDapTpN0nAVU4TXocBoRBtSZGPSGJ4suXqxbhq16UZVlTN45o25OUCphXA/yBHpN5mhmVae0eJSlNi9ZgpHjkPu/54PiVu80Px14TNXN3xRYCFAhqH8YWRIxycmDep4Z2hDuLMwzU81pTkeRmHljs6hzBE0GEpUR0rONEbDB3KnaccGiE8phe2J3NF+d4MztojjfgU7Xp0PtGq4P9yfXDCT2bgSF2XXGmrg26+l5Kg1z9clDK9w== 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=T43XMQxcjTRanl7RjRTeaaoKed3PlEQ8eSjDbix8YVM=; b=Bh9aECHpgOHeDFjBA11ysd8+xyBq1ll9Dd6ixE5JfpuMECU9yPTJ5GgPVhk7/pyMw5rAAmkMcT9vBYBCxN6MMM84tY/JyCk1vP6X4k2bOOJAeT7ngkooEBEQ/NNqhy3hSTLCi4ZqzmKVaABQBfALm4mGNnOjORzJSXB4LQxVo5SoVZ5SXF83XdzfukRH/eruna31erlK3oF1ACDR1zw3VP4OgKS+LwyHa/WOTku0tQttwa8x5h9jHZyb8saIZ1nScojfkQsfFnL3cZ5A1wdoVntSxbS1GeeToSWIw/wjCKQpsH7snGoqmq2e9nnferjzVLGMYxKJcYFGyj3CmpYr1Q== 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=T43XMQxcjTRanl7RjRTeaaoKed3PlEQ8eSjDbix8YVM=; b=cSykLrt+bwDNc9Rmr9W7YKAzztXKYg7bbzIIB7Rfsahz67l8FormubxRzscCvKLgV9WDaIQ1QfPRMvOksbrgsP1HtC5ckOevrXbZK/xopLrPclzE624H5Y5fIc27Bk/FfLVSj84o4wcK/33U7M5oI3MCQnrtK4vCs+Asyku2Duo= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SA0PR12MB4510.namprd12.prod.outlook.com (2603:10b6:806:94::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.16; Thu, 22 Apr 2021 20:57:07 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::1fb:7d59:2c24:615e%7]) with mapi id 15.20.4065.021; Thu, 22 Apr 2021 20:57:07 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, bp@suse.de, thomas.lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, seanjc@google.com, venu.busireddy@oracle.com, brijesh.singh@amd.com Subject: [PATCH 4/4] x86/kvm: Add guest support for detecting and enabling SEV Live Migration feature. Date: Thu, 22 Apr 2021 20:56:57 +0000 Message-Id: <8c69ca218ab223916dfac7e773d0329030e16f95.1619124613.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN6PR01CA0024.prod.exchangelabs.com (2603:10b6:805:b6::37) To SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN6PR01CA0024.prod.exchangelabs.com (2603:10b6:805:b6::37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21 via Frontend Transport; Thu, 22 Apr 2021 20:57:07 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d2e0708c-3b2f-4d10-9b03-08d905d130da X-MS-TrafficTypeDiagnostic: SA0PR12MB4510: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:305; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OchIQLZlPfQYtxnREynBsU7UMwznz5PT8Ol/FPLm8JlQJ5IxPrSha3o1/W1oNYwRr+G3K7hZJR18Tt2Wi95X5nHbgagqmE+F991dMeFXvxxltuswiA5/Czy43wkF+x003oEMH7res+TjIjcYE+UoIP04BjNYa+wEHFWhH65/0bVUvSJzbQIwZ6YgVI08bKtk418GTEIAOfmLNckoIHwOhAE9JReFTSsuWuI4c9keOSVXCQygPj1Xft7EKGkkXoidwQqfc6ygWGPsqYZx59AOACSWLBmKuEE0TUBCP+dE4w+gx6PswVKJwxJrmaZF4/HTBrEdxwvKjsUbPnY2mFzDo3mE3JDP8SxEo/zRBuqD5DZkkOxdNY1GOjHcrhb7nJ9u8Geb/29xXfM5Nlo/hsleaPLCZXQnaAP5tgxE/xxTgvmCZ8eiXECKzSYyMKoOsyuqJ65yZRn2iDKuMF3XAk74rmqKfvdXckqZyeOcUJQT9i2KgpZCUYvGzb5Yg7m1jW6CNBJj/JfIVuGY3aCQ4lUqqr/PFBAUdXxwM9Q8IcgqXfksBz6vm+eCAQGa+9bvVwU1vF8BCxuV9vXyZ7RHjQYO76yA8SWwOtYPJqVkUJ9TUaTGMGzirwt8fNg6nQLrVvAYhXLG+uq2MtKFmw+PxoaGEg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2767.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(39860400002)(346002)(366004)(376002)(396003)(136003)(316002)(7696005)(6666004)(86362001)(2906002)(52116002)(66946007)(66556008)(956004)(8936002)(38350700002)(4326008)(6486002)(5660300002)(66476007)(36756003)(6916009)(38100700002)(186003)(7416002)(2616005)(26005)(16526019)(478600001)(83380400001)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: mphqHatjEG56tmUo5gRqLwvJHgq6euZ5/tXPR1AVYw3NqNuCVw+59kVW56EeRcQyBs+kYVGYH4uwAFjWX5IFwodgog4iDkMf7n9lP0068+k4RrRKnnNY7Sye1Ko/4/eLmuz77HPWgtD4OBGtyfQH/hyerHhSoQ12tVQ6vY5io3ks9ey2ldlADLx501zyYYqFZ7uwmmCmDm5Ajczgf0IH/y4zThsTg9AifA9SneFUBi31in37f4AY6o+yhQqlgH8tkEG/rAfWIhtFcirW973ze6XxzwQQdw3xdLzsa3AbbE7tfjmtUvfhoNwDcvu7mRFESCUJpplaHqxA3CiI21ng6zC9gwQAjBzocegWyz6t6vWd+Q1ijFwtbPDtM6eGf6ThWoBosas8kBXgbwM9wALFPqnTHEQrk3TIvfgYjZuFjvHWhExhQN6kLAu24zTl/JxCNTu6H0XhrBdw7nIIpTYUhSQOef60Hq2J6cjwMCRHGApqG6aFdDSkC67XrI6ExyL1rjPu58TE8Oap9ZSsrp8qTRmjlJ01lAXCxjPvc7WpnPvoCM9iqAIltKoYGtQYldnCb9UvIn2pfP0107X5ZO0qS45GZ3Om3OWWD5FMGTNb2JwsI+f8AznFH5Ow01nHpwZcfWSVQx2HkcVcoQb5dIE1AhodlMH/7MAoSGCF5qYcztHTuNxCSMvRz7M11JjfSXCUMyjyfuzhERIidFe1IYU8+mYtc3BP38SVw4i1/BOL8EGHzOF1py+gSFKv8aSCZBH6z3tNH9t2zen2e17nWJut9z8UWUZ+AnR2xhst4Zt+sEyhrtvJ7MtI2NLTmIi3fUn+Qmd2AuY2+FWbbpAvdHY3D8Cruup4iKx5JELqwWLbWomlwW0jPmTtQdaXesTwz++JvIX8Ux3gU5d8sNT59XKCdLtBRLy8nJwb9ej4rJc9dkbhn+2XdgaaeB5jod50pU3ImU5MsgUZwqjGI8xMxJrTWJstQpc+zsRu45Zmi8qvxdpFOXI84w0/gYLVhPWQ0Wocd5y/iY/uuaVSmGUHVEDcSVemvADqjsxpZhYeRpdH+4ta1pYkKo66PO0IB+7AX2zPIgXgvO4Qt+FtzTmXVAY3iN3HQncwUY2eEv1rKye5g95wBWl4KcASSSTt4w466EuU9Hy2RovTYTdZkVpKYhB0upJ+HkJDFJ/B3CkMYO7818iUG0glqpB+za32Un7p6IhAyE3Oj4iuJdC/6GvQcZrVjN4RAHRGAPZlVxkuE7VtBZkU8S1M6eaPuh2HMapxFWrKj8BzAfOicdQetipg3JKtmYKzKbc47jP9fa06rfUrH8JOkytatXwceQFca5shVBhg X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d2e0708c-3b2f-4d10-9b03-08d905d130da X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2021 20:57:07.6501 (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: MPT5z1Zvm0+qz64kBjmQHiU+abzn64QHw2t1XNkZKA5PzsCgYKF/x53e/yAS9sWKoo+6zFQ4n9FF50jfEJneRg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4510 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Ashish Kalra The guest support for detecting and enabling SEV Live migration feature uses the following logic : - kvm_init_plaform() checks if its booted under the EFI - If not EFI, i) if kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL), issue a wrmsrl() to enable the SEV live migration support - If EFI, i) If kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL), read the UEFI variable which indicates OVMF support for live migration ii) the variable indicates live migration is supported, issue a wrmsrl() to enable the SEV live migration support The EFI live migration check is done using a late_initcall() callback. Also, ensure that _bss_decrypted section is marked as decrypted in the shared pages list. Also adds kexec support for SEV Live Migration. Reset the host's shared pages list related to kernel specific page encryption status settings before we load a new kernel by kexec. We cannot reset the complete shared pages list here as we need to retain the UEFI/OVMF firmware specific settings. The host's shared pages list is maintained for the guest to keep track of all unencrypted guest memory regions, therefore we need to explicitly mark all shared pages as encrypted again before rebooting into the new guest kernel. Signed-off-by: Ashish Kalra --- arch/x86/include/asm/mem_encrypt.h | 4 ++ arch/x86/kernel/kvm.c | 106 +++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 6 ++ 3 files changed, 116 insertions(+) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 31c4df123aa0..776e3378a782 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -44,6 +44,8 @@ void __init sme_enable(struct boot_params *bp); int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size); int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size); +void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages, + bool enc); void __init mem_encrypt_free_decrypted_mem(void); @@ -84,6 +86,8 @@ static inline int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size) { return 0; } static inline int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 0; } +static inline void __init +early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages, bool enc) {} static inline void mem_encrypt_free_decrypted_mem(void) { } diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 224a7a1ed6c3..85f991135daa 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,7 @@ #include #include #include +#include DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled); @@ -383,6 +385,8 @@ static void kvm_pv_guest_cpu_reboot(void *unused) */ if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) wrmsrl(MSR_KVM_PV_EOI_EN, 0); + if (kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL)) + wrmsrl(MSR_KVM_MIGRATION_CONTROL, 0); kvm_pv_disable_apf(); kvm_disable_steal_time(); } @@ -429,6 +433,55 @@ static inline void __set_percpu_decrypted(void *ptr, unsigned long size) early_set_memory_decrypted((unsigned long) ptr, size); } +static int __init setup_efi_kvm_sev_migration(void) +{ + efi_char16_t efi_sev_live_migration_enabled[] = L"SevLiveMigrationEnabled"; + efi_guid_t efi_variable_guid = MEM_ENCRYPT_GUID; + efi_status_t status; + unsigned long size; + bool enabled; + + if (!sev_active() || + !kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL)) + return 0; + + if (!efi_enabled(EFI_BOOT)) + return 0; + + if (!efi_enabled(EFI_RUNTIME_SERVICES)) { + pr_info("%s : EFI runtime services are not enabled\n", __func__); + return 0; + } + + size = sizeof(enabled); + + /* Get variable contents into buffer */ + status = efi.get_variable(efi_sev_live_migration_enabled, + &efi_variable_guid, NULL, &size, &enabled); + + if (status == EFI_NOT_FOUND) { + pr_info("%s : EFI live migration variable not found\n", __func__); + return 0; + } + + if (status != EFI_SUCCESS) { + pr_info("%s : EFI variable retrieval failed\n", __func__); + return 0; + } + + if (enabled == 0) { + pr_info("%s: live migration disabled in EFI\n", __func__); + return 0; + } + + pr_info("%s : live migration enabled in EFI\n", __func__); + wrmsrl(MSR_KVM_MIGRATION_CONTROL, KVM_PAGE_ENC_STATUS_UPTODATE); + + return true; +} + +late_initcall(setup_efi_kvm_sev_migration); + /* * Iterate through all possible CPUs and map the memory region pointed * by apf_reason, steal_time and kvm_apic_eoi as decrypted at once. @@ -763,8 +816,61 @@ static bool __init kvm_msi_ext_dest_id(void) return kvm_para_has_feature(KVM_FEATURE_MSI_EXT_DEST_ID); } +static void kvm_sev_hc_page_enc_status(unsigned long pfn, int npages, bool enc) +{ + kvm_sev_hypercall3(KVM_HC_PAGE_ENC_STATUS, pfn << PAGE_SHIFT, npages, + enc); +} + static void __init kvm_init_platform(void) { + if (sev_active() && + kvm_para_has_feature(KVM_FEATURE_MIGRATION_CONTROL)) { + unsigned long nr_pages; + int i; + + pv_ops.mmu.notify_page_enc_status_changed = + kvm_sev_hc_page_enc_status; + + /* + * Reset the host's shared pages list related to kernel + * specific page encryption status settings before we load a + * new kernel by kexec. Reset the page encryption status + * during early boot intead of just before kexec to avoid SMP + * races during kvm_pv_guest_cpu_reboot(). + * NOTE: We cannot reset the complete shared pages list + * here as we need to retain the UEFI/OVMF firmware + * specific settings. + */ + + for (i = 0; i < e820_table->nr_entries; i++) { + struct e820_entry *entry = &e820_table->entries[i]; + + if (entry->type != E820_TYPE_RAM) + continue; + + nr_pages = DIV_ROUND_UP(entry->size, PAGE_SIZE); + + kvm_sev_hypercall3(KVM_HC_PAGE_ENC_STATUS, entry->addr, + nr_pages, 1); + } + + /* + * Ensure that _bss_decrypted section is marked as decrypted in the + * shared pages list. + */ + nr_pages = DIV_ROUND_UP(__end_bss_decrypted - __start_bss_decrypted, + PAGE_SIZE); + early_set_mem_enc_dec_hypercall((unsigned long)__start_bss_decrypted, + nr_pages, 0); + + /* + * If not booted using EFI, enable Live migration support. + */ + if (!efi_enabled(EFI_BOOT)) + wrmsrl(MSR_KVM_MIGRATION_CONTROL, + KVM_PAGE_ENC_STATUS_UPTODATE); + } kvmclock_init(); x86_platform.apic_post_init = kvm_apic_init; } diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index e4b94099645b..5294e7057ce1 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -409,6 +409,12 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size) return early_set_memory_enc_dec(vaddr, size, true); } +void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages, + bool enc) +{ + notify_addr_enc_status_changed(vaddr, npages, enc); +} + /* * SME and SEV are very similar but they are not the same, so there are * times that the kernel will need to distinguish between SME and SEV. The