From patchwork Tue Jun 8 18:05:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12307841 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,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 151CFC4743D for ; Tue, 8 Jun 2021 18:06:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0053E61377 for ; Tue, 8 Jun 2021 18:06:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232789AbhFHSH5 (ORCPT ); Tue, 8 Jun 2021 14:07:57 -0400 Received: from mail-bn8nam12on2043.outbound.protection.outlook.com ([40.107.237.43]:12544 "EHLO NAM12-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232461AbhFHSHt (ORCPT ); Tue, 8 Jun 2021 14:07:49 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eNXHzXY6+29IBP4MwAet8tzQfAaMIN+x8OgE5ND5zcgBOXSzAwD3IKFPTiEARcowLNtgVyIF1HPeJZJHYvJkB+8fPFY/hUVeuMq3HNQLRwybm+6d2KbofDpk5fy114ADcRqFSQ/lckoAPlwzzHfBE7DrDMFswZxrX2LwfbXIzJbZ/lxEB2l9liaUqDMbXmrMsE0rWGUdyd38JmvMklJ5R4mA2RIr72P4M5UyUgy5A8gXfGyWz+m3F4QEi6uvCh+08lBxIRFtTmIXunqzEZalkqlefmLrtQw5btshrFqR64XcR/9JUZOlSR+nR4STgtUlE3y97cZ910C7/ZSb6hyv8g== 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=l9DMx7yo7+oDg4SeCgH+wtAlhFopEjAswcNpItO+Njk=; b=XllHWPvUkMP2Y9OQ/G76WGzoGFmX6c/zf8ZrPk/rv6oKnefNRadvWVyBngs1QrUTLc5HH5fg1U7pGKnmkXMI2eQshOYLruW8xi1hRT+4FJdFGVjvlyPe7W68NSXiZSpEZPNISGKdgmni1B8wMAvfKA6cfULuAV9Xv5FdRF4flRDkcq13y8Y0a4oxlQfO5qJ40Jw2SXeyYb1lZUXsTPnq07vSk+qI4BjBFO1WJchVseSnXZp4kgaEeJS9weTzHjU2YXR3YU0OWQIV2bGt6Qj1NoQEODyprSCt/bVQw9H//DM4QI+bJLxo/T/rv7eYMtI9mfYysrWmb93tUhAx8gqhNA== 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=l9DMx7yo7+oDg4SeCgH+wtAlhFopEjAswcNpItO+Njk=; b=E4STk0QT0WjEEnLveWyaqxRLMnxsjn7mOb2fgCfLpmVHkHpo41fBTQK3e7cK3pWyrBIUGc7OIoWy+QN0AHDDYnkSdzkCj8/3yT7yPXnHgjw7wl/7zHNcBePdNEhh3iYKqSay9QgvhR2JNzkYG+nEL0UY6mBMP8WK53D4zjohBjY= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) by BY5PR12MB4321.namprd12.prod.outlook.com (2603:10b6:a03:204::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.24; Tue, 8 Jun 2021 18:05:54 +0000 Received: from BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a]) by BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a%5]) with mapi id 15.20.4195.030; Tue, 8 Jun 2021 18:05:54 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: seanjc@google.com, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, Thomas.Lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, brijesh.singh@amd.com, linux-efi@vger.kernel.org Subject: [PATCH v3 1/5] KVM: X86: Introduce KVM_HC_MAP_GPA_RANGE hypercall Date: Tue, 8 Jun 2021 18:05:43 +0000 Message-Id: <90778988e1ee01926ff9cac447aacb745f954c8c.1623174621.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: SA0PR11CA0077.namprd11.prod.outlook.com (2603:10b6:806:d2::22) To BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SA0PR11CA0077.namprd11.prod.outlook.com (2603:10b6:806:d2::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.23 via Frontend Transport; Tue, 8 Jun 2021 18:05:52 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7becc568-c572-4c50-92e5-08d92aa80e97 X-MS-TrafficTypeDiagnostic: BY5PR12MB4321: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: s8kPJxRbunAzfFGuEY2G8hhyDGRQZC6PD3R2Kvr58pLUwIHmdOz4AO7FjWH2jelZLzEsAydJTjN8JEawiZlCIJqXH/yR6mvKFFwSEAtSTpc5anjOl9FjWE5Kem8LqjlRNZ2okREH/XfFWz/J7z8kO9TQThin1Q9D2PvJ6qUvPzK8VnsB8Djl0MH+U38DWOQe+snC4ykJU2YzRQIVU1mRryXf/9L5kjjShUAoi88BjxbjAgFAdXVrEcDMqwBkY2eRPEtPmE6D29zOU2qb5ojw3zeb8NOenUujmEB+8w65nTKYJUz8qCnFcEH3gKyGDw+OyWWgTGtLPwZjr5jcE8tuxPQrJIKAkMrQHx78Of3bSBIZE1Zh755224SR3dKy0lDTLwBIncNxi/W2Ji/GwCzHWRuF1uamo+ZVJ9Kko5ZekZCcoKoLMtR5HHbSjJscQUdmby+aPg9TQMTCMWyclB7HDbxKxb1RBJyLayo7BOmAi6+4g85ArXbt8H9i+4/7pd8dZ6inslreDsPgKCYDIng1SRwBzRnX94zj5ghB+788u7hoDivEPnpSAwOE20199eghQkBSykUc6PomwVm+p2mvjAUjF7pwpW1phBMCiS0iIioUhnLc5Lv6bSie5NOtdYSUSx03oSpDG5f6HT2hfU833A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2759.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(346002)(39860400002)(396003)(136003)(376002)(66476007)(5660300002)(66556008)(38350700002)(956004)(38100700002)(2906002)(2616005)(66946007)(8936002)(36756003)(7696005)(7416002)(186003)(16526019)(6916009)(30864003)(86362001)(6666004)(316002)(6486002)(478600001)(26005)(83380400001)(4326008)(52116002)(8676002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 6M0jXS2bS8oEXI7dSBSG22Uugt6NME98TSxThNUDLaLjv8IX6Vw3wlvmzNQxEggijGEVSDMfzPJQpxhfevdmf1T0NzMb4KHiPf1smVELEOOyGk9EcTudwwd5RtG65rtRhSga8wPts+iFr5KeDLVw8gB4e1yd4wTpm21HMW6MLtnUTB5MNiiSH9f0m+ffjRcMDxzB9jENsT+JqonpbKq8nnY+lRdsyXQ7S7/qOJcW1qr3NA+ugakxXfPnKhPHzc35OImP0aaYBu+vSdZZtHS/GXjKvE+PTvAk1V0mNrQOFrh+Jc0YSj491BKfDsUUfT5u4F+djMhSkval+T0+yhiXpkL7zzJGu9isU/jise94Su050f0HoKUUgnToiQxpD8ChYzVfg5DHCOPqALYt7ewES1yV1fw8e9fRGraR+EpBrWPrOEt76NDwkN6XObZJhSyY2/vMB/E0HKbiFr7mUDz/R1A2hmBBviGZqYjnJ/M5w166PLOFtEoVZZJxYExqe1avzClIzWTQfEbrFfnthA0H7eCaMQJSO5HBebKuoSzW/VVqvzZfYUuakQ8Yzz2tKaPH8FKPgmYsOZulkPAqS8Op5kNCpGtac47KseeurXvzY08UbrL0I8yKz12doaWpNxQtSBTtpWGgu/b7ETkSFlKLJf5CWSHIWCrHWRVxF6PTrDbpkUtI9Hfa8uXLSSL//5ZY6Km8J0zDMCgVKTjJxZQAcFkXhx/u+U1RkSFtOe6r9HVUEHcT68jcybvypDW+Gj5VFeI6qBaXzxFe2RIxYXYK4+7CyrLKW7tl9AQxaLASS2i7hyMG9d1empdddtD6yB0Lt1xH5MxHzRzX4M5YSmncey9EeuYiekR2MJH5UdHPgvpGBJoM+9NRZtnpcKiFcdFpnLbG+M3yHbf0qUgDDwFYQtEVENA/Mc7UspG9P8yL/w30S7FN3pn7dmmF7hEIXn5dewLtWCIdsQneTALWwpiyQgACNrBdFJMXscw8IhVPdLCMX9P6E2QaZ8f7NEvvP/FARgF1+N5Yn+Edcy4EAPj7j0ImFm+4A96xwH6oMELYPIHz5n4rOK+FWjzY9thyml92uDuYppHQkzLvjRrX/yfnOSPSy2U5jr8wkYXO0ae3/rIVsGU8QRv+YIx1AbJE2JCOKTW+/vCxtyhD7V9TB2iy5+ns6gPCYQ0TV+lOiA9ZfaPiZOTuCj8xNc9KgoL1eRGJYtBbgSPxd4lsq8lCf+d4JQqcFfsYdoMFpZhzNDXMjW65ekbMQ91oLQ1KYhx/b8Sx1k0QgTsLr/iLHYqT5QpllHUAwsapfQ6vA+S75lXacQ04TELMkqH0qLdYncxCwIJc X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7becc568-c572-4c50-92e5-08d92aa80e97 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2759.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jun 2021 18:05:53.9711 (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: GHH4saVn+IWpqTJuAzzDxovWFcGmEIkSSxJrHHrj2lvLqg4VS0gqyNKLtvJvsbmXw2gAYS7c15/z+aDW9Opgiw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4321 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Ashish Kalra This hypercall is used by the SEV guest to notify a change in the page encryption status to the hypervisor. The hypercall should be invoked only when the encryption attribute is changed from encrypted -> decrypted and vice versa. By default all guest pages are considered encrypted. The hypercall exits to userspace to manage the guest shared regions and integrate with the userspace VMM's migration code. 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 Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Co-developed-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- Documentation/virt/kvm/api.rst | 19 +++++++++++ Documentation/virt/kvm/cpuid.rst | 7 ++++ Documentation/virt/kvm/hypercalls.rst | 21 ++++++++++++ Documentation/virt/kvm/msr.rst | 13 ++++++++ arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/include/uapi/asm/kvm_para.h | 13 ++++++++ arch/x86/kvm/x86.c | 46 +++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + include/uapi/linux/kvm_para.h | 1 + 9 files changed, 123 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 7fcb2fd38f42..6396ce8bfa44 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6891,3 +6891,22 @@ This capability is always enabled. This capability indicates that the KVM virtual PTP service is supported in the host. A VMM can check whether the service is available to the guest on migration. + +8.33 KVM_CAP_EXIT_HYPERCALL +--------------------------- + +:Capability: KVM_CAP_EXIT_HYPERCALL +:Architectures: x86 +:Type: vm + +This capability, if enabled, will cause KVM to exit to userspace +with KVM_EXIT_HYPERCALL exit reason to process some hypercalls. + +Calling KVM_CHECK_EXTENSION for this capability will return a bitmask +of hypercalls that can be configured to exit to userspace. +Right now, the only such hypercall is KVM_HC_MAP_GPA_RANGE. + +The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset +of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace +the hypercalls whose corresponding bit is in the argument, and return +ENOSYS for the others. diff --git a/Documentation/virt/kvm/cpuid.rst b/Documentation/virt/kvm/cpuid.rst index cf62162d4be2..bda3e3e737d7 100644 --- a/Documentation/virt/kvm/cpuid.rst +++ b/Documentation/virt/kvm/cpuid.rst @@ -96,6 +96,13 @@ KVM_FEATURE_MSI_EXT_DEST_ID 15 guest checks this feature bit before using extended destination ID bits in MSI address bits 11-5. +KVM_FEATURE_HC_MAP_GPA_RANGE 16 guest checks this feature bit before + using the map gpa range hypercall + to notify the page state change + +KVM_FEATURE_MIGRATION_CONTROL 17 guest checks this feature bit before + using MSR_KVM_MIGRATION_CONTROL + KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24 host will warn if no guest-side per-cpu warps are expected in kvmclock diff --git a/Documentation/virt/kvm/hypercalls.rst b/Documentation/virt/kvm/hypercalls.rst index ed4fddd364ea..e56fa8b9cfca 100644 --- a/Documentation/virt/kvm/hypercalls.rst +++ b/Documentation/virt/kvm/hypercalls.rst @@ -169,3 +169,24 @@ a0: destination APIC ID :Usage example: When sending a call-function IPI-many to vCPUs, yield if any of the IPI target vCPUs was preempted. + +8. KVM_HC_MAP_GPA_RANGE +------------------------- +:Architecture: x86 +:Status: active +:Purpose: Request KVM to map a GPA range with the specified attributes. + +a0: the guest physical address of the start page +a1: the number of (4kb) pages (must be contiguous in GPA space) +a2: attributes + + Where 'attributes' : + * bits 3:0 - preferred page size encoding 0 = 4kb, 1 = 2mb, 2 = 1gb, etc... + * bit 4 - plaintext = 0, encrypted = 1 + * bits 63:5 - reserved (must be zero) + +**Implementation note**: this hypercall is implemented in userspace via +the KVM_CAP_EXIT_HYPERCALL capability. Userspace must enable that capability +before advertising KVM_FEATURE_HC_MAP_GPA_RANGE in the guest CPUID. In +addition, if the guest supports KVM_FEATURE_MIGRATION_CONTROL, userspace +must also set up an MSR filter to process writes to MSR_KVM_MIGRATION_CONTROL. diff --git a/Documentation/virt/kvm/msr.rst b/Documentation/virt/kvm/msr.rst index e37a14c323d2..9315fc385fb0 100644 --- a/Documentation/virt/kvm/msr.rst +++ b/Documentation/virt/kvm/msr.rst @@ -376,3 +376,16 @@ data: write '1' to bit 0 of the MSR, this causes the host to re-scan its queue and check if there are more notifications pending. The MSR is available if KVM_FEATURE_ASYNC_PF_INT is present in CPUID. + +MSR_KVM_MIGRATION_CONTROL: + 0x4b564d08 + +data: + This MSR is available if KVM_FEATURE_MIGRATION_CONTROL is present in + CPUID. Bit 0 represents whether live migration of the guest is allowed. + + When a guest is started, bit 0 will be 0 if the guest has encrypted + memory and 1 if the guest does not have encrypted memory. If the + guest is communicating page encryption status to the host using the + ``KVM_HC_MAP_GPA_RANGE`` hypercall, it can set bit 0 in this MSR to + allow live migration of the guest. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 55efbacfc244..5b9bc8b3db20 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1067,6 +1067,8 @@ struct kvm_arch { u32 user_space_msr_mask; struct kvm_x86_msr_filter __rcu *msr_filter; + u32 hypercall_exit_enabled; + /* Guest can access the SGX PROVISIONKEY. */ bool sgx_provisioning_allowed; diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/asm/kvm_para.h index 950afebfba88..5146bbab84d4 100644 --- a/arch/x86/include/uapi/asm/kvm_para.h +++ b/arch/x86/include/uapi/asm/kvm_para.h @@ -33,6 +33,8 @@ #define KVM_FEATURE_PV_SCHED_YIELD 13 #define KVM_FEATURE_ASYNC_PF_INT 14 #define KVM_FEATURE_MSI_EXT_DEST_ID 15 +#define KVM_FEATURE_HC_MAP_GPA_RANGE 16 +#define KVM_FEATURE_MIGRATION_CONTROL 17 #define KVM_HINTS_REALTIME 0 @@ -54,6 +56,7 @@ #define MSR_KVM_POLL_CONTROL 0x4b564d05 #define MSR_KVM_ASYNC_PF_INT 0x4b564d06 #define MSR_KVM_ASYNC_PF_ACK 0x4b564d07 +#define MSR_KVM_MIGRATION_CONTROL 0x4b564d08 struct kvm_steal_time { __u64 steal; @@ -90,6 +93,16 @@ struct kvm_clock_pairing { /* MSR_KVM_ASYNC_PF_INT */ #define KVM_ASYNC_PF_VEC_MASK GENMASK(7, 0) +/* MSR_KVM_MIGRATION_CONTROL */ +#define KVM_MIGRATION_READY (1 << 0) + +/* KVM_HC_MAP_GPA_RANGE */ +#define KVM_MAP_GPA_RANGE_PAGE_SZ_4K 0 +#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M (1 << 0) +#define KVM_MAP_GPA_RANGE_PAGE_SZ_1G (1 << 1) +#define KVM_MAP_GPA_RANGE_ENC_STAT(n) (n << 4) +#define KVM_MAP_GPA_RANGE_ENCRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(1) +#define KVM_MAP_GPA_RANGE_DECRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(0) /* Operations for KVM_HC_MMU_OP */ #define KVM_MMU_OP_WRITE_PTE 1 diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9b6bca616929..6686d99b1d7b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -102,6 +102,8 @@ static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE); static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS; +#define KVM_EXIT_HYPERCALL_VALID_MASK (1 << KVM_HC_MAP_GPA_RANGE) + #define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \ KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) @@ -3894,6 +3896,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_VM_COPY_ENC_CONTEXT_FROM: r = 1; break; + case KVM_CAP_EXIT_HYPERCALL: + r = KVM_EXIT_HYPERCALL_VALID_MASK; + break; case KVM_CAP_SET_GUEST_DEBUG2: return KVM_GUESTDBG_VALID_MASK; #ifdef CONFIG_KVM_XEN @@ -5499,6 +5504,14 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, if (kvm_x86_ops.vm_copy_enc_context_from) r = kvm_x86_ops.vm_copy_enc_context_from(kvm, cap->args[0]); return r; + case KVM_CAP_EXIT_HYPERCALL: + if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) { + r = -EINVAL; + break; + } + kvm->arch.hypercall_exit_enabled = cap->args[0]; + r = 0; + break; default: r = -EINVAL; break; @@ -8384,6 +8397,17 @@ static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id) return; } +static int complete_hypercall_exit(struct kvm_vcpu *vcpu) +{ + u64 ret = vcpu->run->hypercall.ret; + + if (!is_64_bit_mode(vcpu)) + ret = (u32)ret; + kvm_rax_write(vcpu, ret); + ++vcpu->stat.hypercalls; + return kvm_skip_emulated_instruction(vcpu); +} + int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) { unsigned long nr, a0, a1, a2, a3, ret; @@ -8449,6 +8473,28 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) kvm_sched_yield(vcpu, a0); ret = 0; break; + case KVM_HC_MAP_GPA_RANGE: { + u64 gpa = a0, npages = a1, attrs = a2; + + ret = -KVM_ENOSYS; + if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) + break; + + if (!PAGE_ALIGNED(gpa) || !npages || + gpa_to_gfn(gpa) + npages <= gpa_to_gfn(gpa)) { + ret = -KVM_EINVAL; + break; + } + + vcpu->run->exit_reason = KVM_EXIT_HYPERCALL; + vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE; + vcpu->run->hypercall.args[0] = gpa; + vcpu->run->hypercall.args[1] = npages; + vcpu->run->hypercall.args[2] = attrs; + vcpu->run->hypercall.longmode = op_64_bit; + vcpu->arch.complete_userspace_io = complete_hypercall_exit; + return 0; + } default: ret = -KVM_ENOSYS; break; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 3fd9a7e9d90c..1fb4fd863324 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1082,6 +1082,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_SGX_ATTRIBUTE 196 #define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197 #define KVM_CAP_PTP_KVM 198 +#define KVM_CAP_EXIT_HYPERCALL 199 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/include/uapi/linux/kvm_para.h b/include/uapi/linux/kvm_para.h index 8b86609849b9..960c7e93d1a9 100644 --- a/include/uapi/linux/kvm_para.h +++ b/include/uapi/linux/kvm_para.h @@ -29,6 +29,7 @@ #define KVM_HC_CLOCK_PAIRING 9 #define KVM_HC_SEND_IPI 10 #define KVM_HC_SCHED_YIELD 11 +#define KVM_HC_MAP_GPA_RANGE 12 /* * hypercalls use architecture specific From patchwork Tue Jun 8 18:06:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12307843 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,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 2B303C4743E for ; Tue, 8 Jun 2021 18:06:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1084361376 for ; Tue, 8 Jun 2021 18:06:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234107AbhFHSIN (ORCPT ); Tue, 8 Jun 2021 14:08:13 -0400 Received: from mail-dm6nam08on2050.outbound.protection.outlook.com ([40.107.102.50]:36833 "EHLO NAM04-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S233996AbhFHSIM (ORCPT ); Tue, 8 Jun 2021 14:08:12 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WpAO0QT39p+77zMrggkj423NPPFKM1DoK9HbKzrP2KotyOKeppujxq4kmyvnUTYUwyA2tkU/CdTh96pARXfBP2Zki2p5T6x5FLA0a/6jUhD38tGXvVXRgbEc/rQ780cwOZQe7sg2emdHs3zGRrca8Py0NvMI+660JlgdOY84T+MfvqCIovjbM40HDE2Y2kiDTZNgYYnH0Ag+apbuIMu+sO0LFBeUPd7F4Q8gf47k3H8i/iQbemhxm1Wcp+wHfGo9Hn5WY5vVnvj8un0bQTgLVjt8B7MXUFwjsXMjIkvZpM3fONN9R5Lta/Fh9fG9wlnBK+YCa2Cr5LrQPmBK4OJHkg== 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=El3whwmkdfWMns+nsJ2zA7g76wDB9Lbs38DDSo8Hy5k=; b=kQ+yruKgKyt5+c/np6ldVlrwBFoTg8cBBc9vyCa2QiHJTVU5LBCVhaMU8c/Ix4HwHs5B2TUnl+hi77R/lSQgrvMByh78KuLuh2Nl2o2F/RS9T3GIfm23/PConL/X/NO+Mzr9VTQqhp/F5j5JagUmnJSUkNQKKE06s0mYVQBEvm4rmx+xofNj+WeQS9iv5fGaPV6jLaNDtPuoTZ/VKJksyhU/hXOWQK8t5kvtxEKHd4V/8l1rm29ayH0NOjuFKZMSibdHEWg56tjN0uqpBhzGFVPA9bcHkMfJkwpAOk4w5nCQ6RTd43VVLZTdSBSHydFkULrKEBj37AVsM+0zsXBcdQ== 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=El3whwmkdfWMns+nsJ2zA7g76wDB9Lbs38DDSo8Hy5k=; b=xFC+0gPaEa496spy1eU+dZGNSarS11eOcEGvm6RvfKh7ow/U/mVhqI0VMUbyWpYzPof1RddU48sAYc5GTFZmVUIxMD6wUjUVxvcCGjNd0oTxxzIbqIPutVRCOvyyFgL6AK1MOo5pBnHv34P8GyE1PAeS6dqRGwjNCUypZV3p1v0= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) by BYAPR12MB2936.namprd12.prod.outlook.com (2603:10b6:a03:12f::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Tue, 8 Jun 2021 18:06:17 +0000 Received: from BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a]) by BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a%5]) with mapi id 15.20.4195.030; Tue, 8 Jun 2021 18:06:17 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: seanjc@google.com, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, Thomas.Lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, brijesh.singh@amd.com, linux-efi@vger.kernel.org Subject: [PATCH v3 2/5] KVM: x86: invert KVM_HYPERCALL to default to VMMCALL Date: Tue, 8 Jun 2021 18:06:04 +0000 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0046.namprd11.prod.outlook.com (2603:10b6:806:d0::21) To BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SA0PR11CA0046.namprd11.prod.outlook.com (2603:10b6:806:d0::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4219.20 via Frontend Transport; Tue, 8 Jun 2021 18:06:15 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a73a46d5-8516-4c3c-cbf7-08d92aa81c4a X-MS-TrafficTypeDiagnostic: BYAPR12MB2936: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5516; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2xKU1DH87gf9QnTSFBMUuwXniqZHuVxQaqWMc8J1VTTFTJDSHdXy9RFvGNY3ZnXAgHz5kXuCr7LJs/tgAtVty+Hi8gswS4k1300mnwzEy4pe4/PYlPTMvyf3L7JRr/EC2XINDn6Y60Oq6xD+fixSWnqq0oBwOfgJhYrp6y9nQtyR7Wa8N1RYxdgTbJ6GXW0at/sP9ye7nwJ6fhnhhm3iExub00TsA31SXdSX4dLkDgjGhHxHuLabzDUaYl90jCSMfu9A7G4XdiHwI2qOUZM4h9ayXlaTHXQZJgu+jrEiadPZMZ18V7zi8pbqSIfORp0n9G4G2ajsj2lBiw5vG8HYwGfV/gr4CcGyEwHnVZoRhfx2ekZWv7l+4gL0wfX44+CBnd4NxUWBhp+nLZ4/a6CCN3jleLKdWSX1SD4yZJ9Nd3AZD/C5sg19tAJ0IT2maYQEGV84yhJqRWoV7ngRd0pScmfS8A1vHZpWzpEWE7IZM2SiFSGt5pkuw6etSvYO7699tBk0CpE+A4B+wq3pbOG01swWFsy/08L02RyBm2DI90EluFdOecyd3b7Uc/a820YRkj/OtB4wnGWO5DbWCLjSQtetS/FzBpz7TPWS+v7sjl6gyhIdRPjBMcHqcZMJ9npfU+3QGEl2wSKxacPaDvCZGzxoiT2eGQ7YCMta9WBBmK8= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2759.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(39860400002)(396003)(346002)(366004)(478600001)(36756003)(38100700002)(38350700002)(16526019)(2616005)(316002)(186003)(956004)(7416002)(5660300002)(8676002)(6486002)(26005)(4326008)(83380400001)(52116002)(8936002)(7696005)(6916009)(66946007)(6666004)(66556008)(66476007)(86362001)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: iFW3aI07L/t0EmRhuZBTz8QcZxUkuZgohDhIh1HqCQQjgMTNkiazNP+qy24e0JB2SuGiSP6+eAvVvOT0bkgH7Gs6zBO7qZ+sRNQvnjGUv54kB7k2ZZ2Q+pmj+DiLqb/jyDVU+H8lgKKV1Kl5kGpi0aM6LW/HEFKWnhLogOhG+ALAEE/jhMDWIjigc+RP2dZU9cMi8ORAncAhs/m2FI63a6UQ2aSABHpzrIJEGkeTb+xdGNXO4vUa3O7Q4+OZXMa25NGBF3ELJ08tDhLnmq35CRjqz91WaWK1eGrqwVqP935JJcwqEEoiHvsYWMkADVUgIxLhmt3hjj9lbvmlWPIoprWi8Jy1jJOAPW6M/NMDPZ9fbIM7irysO+VKVS66gY0obWR27WeDn4tcSaQL3gDkR9CE3YgFk9msHZPVTgDHioLeUGVXKsIoYTDwIgXhbaCTYSfFIR8Vc33LCpCA+AGm9HIeCA6cCivUgeuMiKX0Tv9pgZ+HgudQD0/OYqBK6fbhips5uYK2nVkcbTrCmJOAlXNeB/ZdLttuELx6bctT+C0wm3SGJGz3wbs+BMJJIgcyPbaL3LxJVxV3/XngVjr3kCdBjAQthqzT/7biqxOIaz9TeRAVhqm6/7vT/XmoNKSoat93pCdyHVqLLLLIV69kvoy5urpkXyHXJgIAVGRIcFzs0SMLop5RytoXSpywyas09v0jwLCltI13m5j4LKswfSCGX2QqiJbIpMCPpORt6bPBoqh2js+6kb9zHsFrid42td5m1UmvDxMmw4f/+yV0nxM5FGvDm382KgzNnLOUfzNIgEJiv9z+n/Rj0G4NwLtvKX4jQO9KyHFU8kiSvTwSYHoEpZIDm49GTgu0UYJ+oQyQN3N1kQO7hNje0erAyry1tqcOKiwkrBflQRfxl4+JjVIdZhvsveFM50WVbCkeQ/LiFTtVgOwVmzkZFhk0TjJZEO3jrmeacdmgs0mh4T1Z2GloLJ0v8cdT1SbiEsJ9gtFS5YS+Pnea4NFt9okICoQQnSgvKjFvPkoma8Wdi7NLr3CllPj9yTQJWothO5qZikld7CSzUsC8ZukX4DpYwapgMTV9SyDon8WQm7+NpDoWtI27ODX9w2+GxTUNqBPCJhwka97re5lWGee7wUAWbKDzYoxoaT/BKf5CKvvQ7vRBLRUweFbiyawQtuHvz4vdYW++QQOS2i2w0PAISJ1EI6hvq8MKowEou9ItsPH2nj1+6Hf3q9dYbn5iUNT84K/vyPlW53FD6UvLRPAXPC3WyZkF/zK3EvPWjPEz6krlyUjhksZLtlp1sm0CFlC+Dd+lXmm34ZYTNnkOg0UxRphr/keZ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: a73a46d5-8516-4c3c-cbf7-08d92aa81c4a X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2759.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jun 2021 18:06:16.8841 (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: GvrHb27/jwJN9KWRtI7nwcwz+sjfmFlPSabC3DeW5qProWfiq2N2DayFdc7VCjNJAg7H4zA5XxlFkZfClN+V/Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB2936 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Ashish Kalra 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. So invert KVM_HYPERCALL and X86_FEATURE_VMMCALL to default to VMMCALL and opt into VMCALL. 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 Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/kvm_para.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_para.h b/arch/x86/include/asm/kvm_para.h index 69299878b200..0267bebb0b0f 100644 --- a/arch/x86/include/asm/kvm_para.h +++ b/arch/x86/include/asm/kvm_para.h @@ -17,7 +17,7 @@ static inline bool kvm_check_and_clear_guest_paused(void) #endif /* CONFIG_KVM_GUEST */ #define KVM_HYPERCALL \ - ALTERNATIVE("vmcall", "vmmcall", X86_FEATURE_VMMCALL) + ALTERNATIVE("vmmcall", "vmcall", X86_FEATURE_VMCALL) /* For KVM hypercalls, a three-byte sequence of either the vmcall or the vmmcall * instruction. The hypervisor may replace it with something else but only the From patchwork Tue Jun 8 18:06:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12307851 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,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 BFA87C4743D for ; Tue, 8 Jun 2021 18:06:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A8F3C60FE5 for ; Tue, 8 Jun 2021 18:06:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234229AbhFHSIe (ORCPT ); Tue, 8 Jun 2021 14:08:34 -0400 Received: from mail-dm6nam08on2084.outbound.protection.outlook.com ([40.107.102.84]:30853 "EHLO NAM04-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234176AbhFHSIc (ORCPT ); Tue, 8 Jun 2021 14:08:32 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZVDri659r3ccsiWA/JI/JPHmpsIAMNVlgIc8B8R8gmEs61gW8DYxCqyrFHjFX1oSqPbaehmsKlwLybwzmdRXjt5NzoFkU0zCFZsrdFnAFaf5KPUFGIIxFVBYszHFh41+OQ/Ox0Gea6YCYwDZBp6Nh8sjnEUeJTDW4V+CwOEW4Ic2izDKdcVvuvZf96hClGC/sL9Rsxy0KQPkZ1i1ShMjRvqgZ/UyOm5uOXbfKRqc+fBTn4Tx1AC1EzAWiD14Gz0rWr7td98IlJyONRe7sV91/Ziy+XNGMk/m81wGMeACjLpZ39eCLNXCsOPT4vbPCZfcUksnMZnIbRQU4DVSjlYh6g== 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=8V/fgw8Zdb79Sp8j7durUwyS0eECfGQTGfDekcmCQyw=; b=HYGTWAXp1rlQlci6Y2C1VbbE5tZvG2b5R619iPYBgnlBI+HobStSPBqfqObAUf7aSQYc+DVvN0aprci27miHk9J2bEZmOB6hKbaguwScRkpVDHf4LRpKABTnBU4j20mkPsq+eEfU96UbD0vSie4u00NZY6BoM7gIwG/L7kE8iSA6eGleNt9Xuvecnz1EFTXWl5ZgLEZUZl2V3HAvNnTiVQN5ans6wG7QUm5QXLnOuIArjXCQlmJbziKJ7sZvaBPEE0gZg59ZQrAEIJ1vzI2cRuokU0cVAqOzeVTN6z8gxwWbD1J/mBcoBRrrjzpUzmK1JrZtXBwxqURlrBJOe72ADg== 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=8V/fgw8Zdb79Sp8j7durUwyS0eECfGQTGfDekcmCQyw=; b=K8HoWKV9PXBthgfPguZF2NkxUO4d3a4AWY0SMoWfzlLIAx2NB8UsOA1M1cdEp+TF0M7PG85CyZcxkN7Ggo2esm2LSWQoVeNHCTy/jjTPOmRa0LPab4ONcbXNCDU53iZw9/Nuf6t5BioC7n4OtRT/CliYaWOeb+mbQJGG2sg30vk= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) by BYAPR12MB2936.namprd12.prod.outlook.com (2603:10b6:a03:12f::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Tue, 8 Jun 2021 18:06:37 +0000 Received: from BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a]) by BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a%5]) with mapi id 15.20.4195.030; Tue, 8 Jun 2021 18:06:37 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: seanjc@google.com, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, Thomas.Lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, brijesh.singh@amd.com, linux-efi@vger.kernel.org Subject: [PATCH v3 3/5] mm: x86: Invoke hypercall when page encryption status is changed Date: Tue, 8 Jun 2021 18:06:26 +0000 Message-Id: <41f3cc3be60571ebe4d5c6d51f1ed27f32afd58c.1623174621.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: SN6PR2101CA0013.namprd21.prod.outlook.com (2603:10b6:805:106::23) To BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN6PR2101CA0013.namprd21.prod.outlook.com (2603:10b6:805:106::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4242.8 via Frontend Transport; Tue, 8 Jun 2021 18:06:36 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 05601bbe-0240-4b38-1768-08d92aa82876 X-MS-TrafficTypeDiagnostic: BYAPR12MB2936: 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: 2BJYK15s5DDw0/lgCwomtBxqQB5EklytLCoogOIfpnAFmBuugqHf4ufbCu8fIFaMnQEqQqosrZVWyDMJQC5TOywmCODLUnGRSZuU1QRO8olXjwzlq0yhy9/+8XR4mSNJhIHPjRJsKyUo/dP47UPyBweD562Q/gBxU97HSdszi/Xa7D/fWIDpaX0d9XKdG6GsqfFP9iKj/ICQol8WFEETOAaNB/qyUqNgG1UZILctGIa5W0Vgf1ZAMBY6zXNfgNfEGvG3sh4/8Jilrvb95F6DF5v9oqCT4VEIhXJjdiAilwfbjDJHJxw00/0zQRW3jzS3hFgtQYo/hYjDJdS7t5ZG9yX3oJhXpnkjtI0TKCwAAa3YzJiGFJPZWEBi8mZ0KD0SMKmD4cP1wwZpc42bJSnqpvTG8HdxByFip0M73BFCXto4RWQNY5+rLr/E0g2wTts/bx+t2xbtUgefFJbBn4zsv3WL7suNpsNjTosjn+YoeAtDeqNJo614E0yO8JIgTXHSsTtZ2zQm6itsDudhpD8mw7TjyvbE3kWfwpr8ZIFzUIalrJZVST2+mKPLGkPTV4KbvaUyToCG5hZQaWkQAIU4lpYYAqkLIXpQ+U1ddUkH7a5bwywAHGHwjta9tMrAp12a4mJz39wHikkgMGIBY92MW/Frj70qEltKbFBAk3ToiYU= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2759.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(136003)(39860400002)(396003)(346002)(366004)(478600001)(36756003)(38100700002)(38350700002)(16526019)(2616005)(316002)(186003)(956004)(7416002)(5660300002)(8676002)(6486002)(26005)(4326008)(83380400001)(52116002)(8936002)(7696005)(6916009)(66946007)(6666004)(66556008)(66476007)(86362001)(2906002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Lj8Vfv32CWlE6pyhAeoQKZ0+hdLvfd4wYWdcWOsrycO/V3o6enO9f5qYhDULneSAMIZ+IMi7jxv9ZJous2XxA3sxyzw3knJ1nC91TMnP4Oor/bT8RhW/VPWnAnjfyX+ScqIkeHPbHrnacKSnXH33AUYLeOgtBwJauStvE5JJ+UktPMXGru2Ca+PmImGaMqya47S+/Ldmb5w73X+yZCdpDFJqyHooUCy7kGbnWqe+0baFABQEoRAQLGu6CvCm8zd5Yx9lo3hAZyhnYQUNSXW83Vi9+iWqAmePga1chmlEi1xW/M62Sd0cBXEfRgbPyjnmo1mTRhNIit+i+wehmD/zTetKFH1ZgMtCmLv24W5o350EF5WXtE32hMzq2SXDJ4niHHG5F9a7jkCSN7O9MeP0hS0a23O1ILMP0srHD3L2p1SyedxdyuVrNRpLafqTkiQ7zCjdMYwELfwZp4nie53uq38/wXsNx6yL2qH9lYzHlG4MO1nIzzY0GX4388dzmTKcMy2KLM0SgZKsUZaDg0vw7kOq21zmuvVbQRA0/Fcaam9SGyw4lyURumaOreytjp9E7qnG+ukbcWF/zfQ5lLVFfDPB0BGxSQKM29fzd6DMr+pqDKZc5IEm2jtrN4OuRXUNNmhmTiESzyco8KzXfSjr3tiQU+hnlOjOHbgE6RvJJ+9JLmeCRulFUAsRE06pB43fhfwgJzKnYDvhsljkXW9cnVC/Cn+I0DmqZDe1stOnsFfI9GmjPjRJv49aq5pjMsQZZYy/NIx0cFotfct5e17syYaZE/2Yy/RxPyIBm2Sk5RMEDl3HCnp2r7Rdg6I8v7NS6J/O4nD78uwMdmBNW3RIZYuD6/fETx1IjyH/+TW09hkrGMO2d8FiwsbquglL2P69gOzkI84f/MDNFynknStulUgPCqlGU0Wuhkmjyq8gctskXYjLAZkFCNuiKiJZBGLswjRI42jQMj3kaEYMtKZc1xYCgDGoFCsN+flokX47rFMBK2GlWg0Aefym2Cm4TFSKvEIbR/CJBD9ilzEhOG7WBKPkGNhmmU+2As6+xtiKIKYV16kuGxuvupEqZSJG78d2WckEnVizjf/C6kGTKFi/SoaMC2YPCVN5iUX5EQcsoO59jalhrshV7Cj88XTu+cUXsZeu6cDKHOO44qsMqaUTpVHmxxtjaSgHvccBej35+paAt3Xe1T7/U5Rw4JbkQ3rfOmED8tuCBn5lsDcfIG2xVssJg5YsztGmCbzhXCr8UJtTjcRDmLzcidt/vNdMqQtwRuL5P7yAMgsskznZI8vWYJF+spmJSVJbRLBiCbAUqFLnam8FBKfNIkPDjEXRgphc X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 05601bbe-0240-4b38-1768-08d92aa82876 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2759.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jun 2021 18:06:37.2985 (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: 9SplhooJyuOfQ7Y9i3giQfdiNWJfx7QSXpKDIKQLGJut8UEiodZo6i/HaZ+7l+gbnIxJdxJz9U8lS1FrRXuH2w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB2936 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 Reviewed-by: Borislav Petkov --- arch/x86/include/asm/paravirt.h | 6 +++ arch/x86/include/asm/paravirt_types.h | 1 + arch/x86/include/asm/set_memory.h | 1 + arch/x86/kernel/paravirt.c | 1 + arch/x86/mm/mem_encrypt.c | 69 +++++++++++++++++++++++---- arch/x86/mm/pat/set_memory.c | 7 +++ 6 files changed, 76 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h index da3a1ac82be5..540bf8cb37db 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -97,6 +97,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 d9d6b0203ec4..664199820239 100644 --- a/arch/x86/include/asm/paravirt_types.h +++ b/arch/x86/include/asm/paravirt_types.h @@ -168,6 +168,7 @@ 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 43fa081a1adb..872617542bbc 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -83,6 +83,7 @@ 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_range_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 04cafc057bed..1cc20ac9a54f 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -296,6 +296,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 ff08dc463634..6b12620376a4 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -228,29 +228,77 @@ 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; + WARN_ONCE(1, "Invalid level for kpte\n"); + return 0; } + if (ret_prot) + *ret_prot = prot; + + return pfn; +} + +void notify_range_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)) { + WARN_ONCE(1, "kpte lookup for vaddr\n"); + 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; @@ -285,12 +333,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; @@ -345,6 +394,8 @@ static int __init early_set_memory_enc_dec(unsigned long vaddr, ret = 0; + notify_range_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 156cd235659f..9729cb0d99e3 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2020,6 +2020,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_range_enc_status_changed(addr, numpages, enc); + return ret; } From patchwork Tue Jun 8 18:06:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12307853 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,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 6582FC4743D for ; Tue, 8 Jun 2021 18:07:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 522B161246 for ; Tue, 8 Jun 2021 18:07:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234066AbhFHSIz (ORCPT ); Tue, 8 Jun 2021 14:08:55 -0400 Received: from mail-dm6nam12on2041.outbound.protection.outlook.com ([40.107.243.41]:49988 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234246AbhFHSIw (ORCPT ); Tue, 8 Jun 2021 14:08:52 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ji8pFvEk4pozRY3nCirAHytZuXNFZtClJNEOYD/Orr3/mj8eKhDhdXo0x71o/nxVBF7xKcjhZ1F/VDyL+5pyG8a5F4OpbhuzRz/f9GbRMBvoFsuMxkUh/ejz6wUeelTJ1tLTj0YkglGxmXzfUDRUP6TpZdHn9RVY6Wm/AfdR4O7/n89kK4MNxdjWHLzWMxR0ZYbz8cdNSZmqQYE9Z0eYAnQPViA+XTG9T/FpMLizJ+GGdtFq/oku3+WFlpkTSUAn3pRcbN43cilP9xRCvjlGKzy/AbSNjd0yzjhJPPtjz9EMtEyLicwcDkEEM1pisRM/iHTy5pAelePS/gTp6weCFg== 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=nEviQ1iNZ66KLAyhHDcKxcCB08Jy+gxynqCE6dn+Kas=; b=jX2asGcYvHzKLLxTrB8G1YBdmruIj03KS06ILM5nVO44PA0G8hv1vgiFKXN7XWd4m7MDU3mQjWatdnTcx+vBV+/1KaGM05SNCH710bvSyavEp0xgp+XHIcmdKfFzmYVcWYPrzBoKNlQD+qPEBzw1rIU498nxmRHVrR3JxT5aylKEwqKYL5xIJ1NKGVb5o8f5cDWWmxS8riy/sU8u1b4z38qh9WWnV2DAQVAhr2XupjUEoNqe3iFjBQTykxNWWUo5ZXJC37UhgAq5Biv5ruPMrlimq2xYDg5xY2K6VBn9IBvaoLpXhdeUtz4GYb69i4X0TgmTfb+yy5EokdFNAuV6Hg== 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=nEviQ1iNZ66KLAyhHDcKxcCB08Jy+gxynqCE6dn+Kas=; b=ZjgNuIWiAqiLG4/4fsWa+sQnfzPWtVq5vsW0n+w2LR6URhdre8MF2Y4IyT65umStXU1O02AmuLytk2x+5hJdUdR/azzHwZdGXPa9+Eb/bCFeKKpvKW2E+kr79KMbxdqmgiGazOGFtrvfuM3vpeqpQx4G+crh/KUkJ5h2lESOpFk= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) by BYAPR12MB2775.namprd12.prod.outlook.com (2603:10b6:a03:6b::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Tue, 8 Jun 2021 18:06:57 +0000 Received: from BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a]) by BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a%5]) with mapi id 15.20.4195.030; Tue, 8 Jun 2021 18:06:57 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: seanjc@google.com, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, Thomas.Lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, brijesh.singh@amd.com, linux-efi@vger.kernel.org Subject: [PATCH v3 4/5] EFI: Introduce the new AMD Memory Encryption GUID. Date: Tue, 8 Jun 2021 18:06:46 +0000 Message-Id: <13d4bdd5fc0cf9aa0ad81d43da975deb37f0d39c.1623174621.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: SN4PR0501CA0128.namprd05.prod.outlook.com (2603:10b6:803:42::45) To BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0501CA0128.namprd05.prod.outlook.com (2603:10b6:803:42::45) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4219.10 via Frontend Transport; Tue, 8 Jun 2021 18:06:56 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6ee3f55e-feb4-495c-9b0d-08d92aa83465 X-MS-TrafficTypeDiagnostic: BYAPR12MB2775: 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: vfuY/onWdiBmWSKV3PO5rKj8gTazjmY77WCHKrz4xAbl9W3BgqKVX1Bz2EGg0BGtMB/dTzuGtfeNudORTrNFskkdhDpxNzFqwLnw+bDTXmOKhIY/iCi6q87+oC7Dq+kJKvDzCy5inxCxLkRRLkhbHi55Xc08XH4uLgaXdMJ0g1/r5WrXwDdE+DGL61LRJRtdHVFqx8IoIaDoD3A/3QV3Yvm3LHXuWbIjBKj3FSb2RSDrcgHv7Nt5utT7YL2kz29fpwy4iCgmoGaKSwkBxhQkqT6xTRh+K0fRS0mlEJBid+76dmpRS0ctmuVZDKZtQzaioQfyutZ0K8lQwKQPcReWK/DQRhKkwcgi7SISGXe0NcCPVtAohuCokxBMhb+Pc0J2dqbGDNaKarXKn7m7xCsJ5vfaErztq5+sQS1I83bV/t2ou6Q922WHCZXgbl0ChcnNjD4iouhsKg6+nxGAa1s755kdtvQifPNKhEMB0hZrumpexQ7S2kIfg+M7YWsHt3Ux6CAa30mS68+YD4Q5O+t7EGjB9Ig7+Y2yRn+cKNvXHgfNzYK5TMyxi4VvGw2hivci1ZqXhCMRxf0H3uHttsalaISJWve/j1923u4iHE8Of4GRwzr/TJM4lQYP5BADncNf+mxZOKSPFV7oO3BxgryLJalIvJTFWvXr3Tsaamt6Ev8= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2759.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(39860400002)(366004)(346002)(478600001)(2616005)(36756003)(956004)(6666004)(6916009)(66476007)(26005)(4326008)(38350700002)(7416002)(7696005)(5660300002)(52116002)(86362001)(6486002)(316002)(66946007)(66556008)(16526019)(83380400001)(8936002)(8676002)(186003)(2906002)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: t5TNHJlN+eQ8z2Sbxs7hUeaJ2yg23WvEHyewaS/ypdvu6C9ZEBIhyRNppP9jJ5KvpQ6TIxPqfw9kJ5XbZMm4k/pqUfdH7WXj2LGHYgGf6/er/PokzS4dRJk8oSyerpJox9NWsW6UFnIe5NErGme9oQz71CaOXMamyAsfQRlWLidNiq4h/wd3WDs0hdsbFButImntbsKrPoX9T4LpaFncivQoj+J0g7DY2zHJCl1HNNe8/RlLbHa201ae1iStv36m60OHKc2+7QZgnkGiOdc/jLO2XjlsC/NVXdQnJMzE5NRbVbRzVkQ4axPo2wV9u5RCFQo5xt8qXcoDl64WzECQAvZJjkNWFrrXf/EdBhf1qqB7ytcD25QwAkuQUdFb8hSjR8FBiWP11d0IUOUuA4NoKVqDngcCLXqH/tj05P7t7mFJNLkwF0CfLWr4MsmK/BtL++GCvSBt1CqiD0LSc+rtOeXyQMXEN4h/OTWh0nthXyHxsVZaYa03ThftPBB+5Pioe3tAiMrUppMCoH/WzuA0bAslqU2yhqzft0fHHB2kx9xChTpYm3rAmNiMRQ1HbtEPkkyxYyXqOwupe/n17OdKTJ0rGwNnInXDoEaxWjqDatlKf1udiKSd0S1GqH31O9Kp9YIohoGHp8TABCO/kWJr3Ox2Nt7BcudU5wbG3nf65MqlEIwRokag+QGmma5fb0F+NQ3guhryPwR9XY37//XVHEVuw1x6EJnDHw9m8KvOWf9CRcvvVpnj6RRkmYSUfS6NHEyR9YkNCEHqvIht9FPQpbvUcITsNr8bDA+NCiLRWqPMKIcrZP1Xyqdo52oYYvXRmNaoF9e+1Z5SCUyk6NIEtKARzB3Ir8k/gPzzBavLhH0MfM+rNapoou1VXgt4F6NUoCAVBwERgnWnHAGa4MnQep6bfgg7R84Ix4lFYPpseeN8tgTw/mV2E/Sr6lEqaXQA6V+0PEzGwgUewSYld6X1Li/Xn4VBXq/Yhxks0aWmUzE3lUNc9Ze9jo6g6b7a2hgR+jrNBdO8aQvUeS650RfApv7VUmkMPWYROlDlKjN9hn3OVraDUH9POkx6AzMKW1I++ZiAJ3mkj7BpxrypmMqEcbPQG5Jb2JGFH/9hrtSotTExCyLuRsUXnv8M62OkFq1nfoFmcWh6wYPZ6mmS8/XolE2hxztwNkHXbwekJjNdm0ySlT1TW/UVIXDwLjsv4zR6k4Yo9vDhXAGSaL7mXqmOilhNheDY2QRfKMSPqb1drXLcLYjMuYDBt9igo5G2kj76Dr71NxqKU3MidCDrjURZHa+XoFZajknD1q93S618TdlcRFkS2ZS5oWuc1UfbtJGP X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6ee3f55e-feb4-495c-9b0d-08d92aa83465 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2759.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jun 2021 18:06:57.3800 (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: v3DwMxdFYJclJPrJx0oe8pVG3TmRW+RlyjTDNAquIAtff7LcpaGiMl+S/frWrqew4XuIQIumwHswlzbhxQi13w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB2775 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 Acked-by: Ard Biesheuvel --- include/linux/efi.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/linux/efi.h b/include/linux/efi.h index 6b5d36babfcc..dbd39b20e034 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -362,6 +362,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 AMD_SEV_MEM_ENCRYPT_GUID EFI_GUID(0x0cf29b71, 0x9e51, 0x433a, 0xa3, 0xb7, 0x81, 0xf3, 0xab, 0x16, 0xb8, 0x75) typedef struct { efi_guid_t guid; From patchwork Tue Jun 8 18:07:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Kalra, Ashish" X-Patchwork-Id: 12307855 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,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable 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 2FB3DC4743D for ; Tue, 8 Jun 2021 18:07:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1923661246 for ; Tue, 8 Jun 2021 18:07:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234271AbhFHSJM (ORCPT ); Tue, 8 Jun 2021 14:09:12 -0400 Received: from mail-dm6nam11on2076.outbound.protection.outlook.com ([40.107.223.76]:14369 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S234291AbhFHSJJ (ORCPT ); Tue, 8 Jun 2021 14:09:09 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SPokZuvCEMTM7FAr8X6v57oKIgBhkYZxUBz7l3LeB5ccGGnpVniLQ76kTJfw6k6xlkeBbBwiJ+2ZtnrUdI0KNJUuJLv9h+tybUSSnol+hPttB0FN2+Hy80wMSRD2xcmUGbcWXR0m5HIUsQGjzQlBPljoKJs3EdYHQRdPWUsPB5sVZdqePa5g/M7q8cFIfNI6vjcFPFjE1X2PPfR41aKMi2a0jgi72Z36OFdNGkLiXnag61EQ9Wrw0zPdI5hKeUhhDxg3YYw+9YZm5Hb2C28uchqAjWRJGaahoiUn9q/jYAmQ5biDpQmxTaWHJsj7J+5+ixdwrivVa6WAJIjRqD9X2g== 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=byPxPBVAaVcaJmF4W2HW9APZqi6drLzD62O/iiYnsEs=; b=ciF2qjaIVENbX9mDtfmThmmGor+7Em/gYCI+G3jwb+lmP5bUit/pWRnjZ+vjbXUoMDCLQbsZYqnGMXYi9yCUw/sMD5sedLNfC/NTsSOHqs27WWnt1HBmKibaiSJ6c6IIBw/Smuqa9ioDCFnPCHaNinkag4BgaQfwNr+mx4iVagtmxkYAKuEl+YdxbEcdREOWcbeA1xVIBRtVPWh/x261H1XrA4ib/l0rTAFGW0Z/vtAeSU3e7y2o78wAzR2VyNtsDHLlIQC4SuUivrqD97XSGEWQUd+Cneu8LMOy+0z+c308cE3YDk7XHbeU7CGh2hnlL3bFrEECdMDysa90XqsWtQ== 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=byPxPBVAaVcaJmF4W2HW9APZqi6drLzD62O/iiYnsEs=; b=u1wOV8wUJlgB2W+TLw8d0wgQdvWdQELkMpPBcrC8hh/bDduBmr8Re4+QXZRIppT0DmlzqT7eedO3efCyEbjsPxZiFnN0ymvcBMHqXp4pwbcly1D7evCvZs3Rha8yo8+PzF71qQL6KxM6b8+jZP4+NbQi74my7t113L1KhIO34AQ= Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=amd.com; Received: from BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) by BY5PR12MB4065.namprd12.prod.outlook.com (2603:10b6:a03:202::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.24; Tue, 8 Jun 2021 18:07:14 +0000 Received: from BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a]) by BYAPR12MB2759.namprd12.prod.outlook.com ([fe80::ed9c:d6cf:531c:731a%5]) with mapi id 15.20.4195.030; Tue, 8 Jun 2021 18:07:14 +0000 From: Ashish Kalra To: pbonzini@redhat.com Cc: seanjc@google.com, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, joro@8bytes.org, Thomas.Lendacky@amd.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, srutherford@google.com, brijesh.singh@amd.com, linux-efi@vger.kernel.org Subject: [PATCH v3 5/5] x86/kvm: Add guest support for detecting and enabling SEV Live Migration feature. Date: Tue, 8 Jun 2021 18:07:04 +0000 Message-Id: <951a07f3b9e25cb7dd8fbc8d1797f216008c139a.1623174621.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: SA0PR12CA0030.namprd12.prod.outlook.com (2603:10b6:806:6f::35) To BYAPR12MB2759.namprd12.prod.outlook.com (2603:10b6:a03:61::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SA0PR12CA0030.namprd12.prod.outlook.com (2603:10b6:806:6f::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4219.21 via Frontend Transport; Tue, 8 Jun 2021 18:07:13 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b72e0e05-0ac2-49e3-eb27-08d92aa83e94 X-MS-TrafficTypeDiagnostic: BY5PR12MB4065: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:252; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sbryhYLM7C9ksedU/Tq3RUbqyYprVpPph2fy3gm+W8joDNQg97uly2exti5X8rNVJow3yqsm+zr2KDX7F3iPEFns1YMYW6N0zawA1QA3uhheyfruqg909K3nQV/5kbZfE9Bz9TYHALM/MR03au0EVK0j8OUiDtTp+V98EztA2gquw9INYU8QcKf5yhhzN+j0qZm0jTp7a8gdSumdzHp29IEonGNN9u0gExytsis+0c2TlEGxc/aq1VqYBOhp5i5McWfhTR4ZrPdBO9cj6AD5urYAxwedqos/YVl7rEgbabI+8tljFHAkXtDR3qAjZ26/mJGKmiJQSyJ8ZQ4jF3WeZCh2wS5I9/kUSj2K1BAGB8zs1n3BKachY/HxgU/1jxt+qXNJHwDhZXwTpVqLWNISwP4K0z0jcCl39rGwqnRK6oUc0vRvJSOSWQbfW+2Yxr5UwvbH9GcDAxvzjuPuaDj/Jtdx+s2b6wuQhg8lEmpFG5V8Y0li9EXFpn9TZDZZAs73W4YE+GLVCDfws090plGE3CvbyCFgV8R96PP5xP6RWZJo+pSlmETKkzVNUW+poIKUOh/+0OaXnF17IHvExHjgexdpYycOZckVlSw7aiYob5Icb2+Ltq8MthCbhhzsABvDDWWTBQE1C4oxmG7300AL3fHOGy8GghfYafeJdZUphpc= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2759.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(376002)(346002)(366004)(39860400002)(396003)(6916009)(4326008)(38350700002)(38100700002)(83380400001)(6486002)(5660300002)(86362001)(8936002)(7416002)(66556008)(8676002)(66476007)(186003)(26005)(956004)(16526019)(2616005)(478600001)(52116002)(6666004)(7696005)(316002)(66946007)(2906002)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8UGd5xPr9V1O646fsiQUVthZCbAKOCkPTzpAmQtvN/dSyU0vaeeeHujQeXvcF3XELbcHUI4AKzKduDTfkFCNDzO/ZS5R9vaWCMymP9opeYucLEobU7PuGro+51K3+dMDy4HMPJoUdJ20cj4jpxjgR8B5nPGCmSvDsycD+D2Qev/1p8/OiyY1QJkOAtTMFgC+2bTKGUzmhciUbzyKPcAzsAlnw9LZaCNnIs6TllFnQ+yM1rfvtS9NXhoPuXiPnLVAGEJEpZA3P79Hs4Q1ryoV4VGN2OlPHVJwT2GF50v0Rn2fvxf9QB76JtK2sbqBVimCuq4eODbhkCZuZ/INpmpRZacQ29bJiHyUDpqKfmCVmqJjjSayJf9aWOfWkzgr2HODNgYQx4+XCn93PJbFjKZe477IcWt1wwc/IHDyn5lFEzUkPHK+8xEpum+vJPV9hr9b3dcA4J6ALaOUTD5CqVRfeD9BJ1kM4wtGtlKG4sw3uMl2JvWkLLiZFYBlaopcYAmusZ42ZZjJjt7HWhL7PRidggljz8GjyPO7xBzCJf7Iwxp5YpaEKIOVytIi+NgJB0CaojZgE1GXZ5UNCMfeVPRUPEBb/5Exwo0ag7Qc8ETzLkY9EQAwUBr1kOBjo3eAzTPkaeZmRafPv2zjdZGDKKfSjniOF4puaNDY7lDQNTRXLrcw4Tn/baOVA5bE6MdnXzzyKtidkS3bLyA7abuNMQp0rwaCCYRvAb6K6Pkr1GWwCG7973oaKQDymqRWDnIZ35OPlpdL7e2WJYcSRQH1+SyfCsp2PJMc8fb4GcKokzIm2gtv9FQ3z0etKmzD3ERDPVsxzgjvaTEz+1fjGEGgE3V+zGy4zGAhw55PT7jPG2PdRv4Sg0QdlKRg+RMt/Ctt+lfuvSOAwA055RDMAy1WFVqENd9zanV1rMPhAK7Y9lNvoG30kJIgW+EKNws4kZMqIfPFyGPIyXhe1Oes3iCo/62a6ei0abaQKn2u0P0e7tOe3P19PrX4mWZ/T6zgwPSWZ0VL8Ba1YlmnmquAYok4z4e4xMs3xDadqzMm3mxj05baEziOKWq/zYjVhlG1uyg4sJa4fdY8TUzdxfC6mi6LBYqYd0Xl4dR758osFHjUo//E8ZL1iqwHiaJgb0s+sAcdXjwu8ZhVQNZD1Z0kYk0dk1v0duC/LkcHtWWlbOrczpW8pMQKackUulIyR6ciaQ19gwBfLgRvhoptaGoxgjimJQZoPL9GN2dFBzKKyCQ6DhYBsAzkxc3MJT3uk7Wzv3hwkjEspAhhyvW/9fkNU1Rf0mgssVhm8/bKW76NtXqawCtWflPg4y6HYbISp4r54X9Nj2Zp X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: b72e0e05-0ac2-49e3-eb27-08d92aa83e94 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2759.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jun 2021 18:07:14.3784 (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: /plWAtxytF6pWuSwwATDdXkNcPkURPtnnh5obINmJ/O8vqK65B+mI5v2hbcbNfYRPTLsrKjjNMhMJ+WsvMbEAQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4065 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 | 107 +++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 6 ++ 3 files changed, 117 insertions(+) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 9c80c68d75b5..8dd373cc8b66 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -43,6 +43,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); @@ -83,6 +85,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 a26643dc6bd6..80a81de4c470 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,7 @@ #include #include #include +#include DEFINE_STATIC_KEY_FALSE(kvm_async_pf_enabled); @@ -433,6 +435,8 @@ static void kvm_guest_cpu_offline(bool shutdown) kvm_disable_steal_time(); 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(); if (!shutdown) apf_task_wake_all(); @@ -547,6 +551,55 @@ static void kvm_send_ipi_mask_allbutself(const struct cpumask *mask, int vector) __send_ipi_mask(local_mask, vector); } +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 = AMD_SEV_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_MIGRATION_READY); + + return true; +} + +late_initcall(setup_efi_kvm_sev_migration); + /* * Set the IPI entry points */ @@ -805,8 +858,62 @@ 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_hypercall3(KVM_HC_MAP_GPA_RANGE, pfn << PAGE_SHIFT, npages, + KVM_MAP_GPA_RANGE_ENC_STAT(enc) | KVM_MAP_GPA_RANGE_PAGE_SZ_4K); +} + 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_hypercall3(KVM_HC_MAP_GPA_RANGE, entry->addr, + nr_pages, + KVM_MAP_GPA_RANGE_ENCRYPTED | KVM_MAP_GPA_RANGE_PAGE_SZ_4K); + } + + /* + * 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_MIGRATION_READY); + } 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 6b12620376a4..3d6a906d125c 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -411,6 +411,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_range_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