From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (NAM10-DM6-obe.outbound.protection.outlook.com [40.107.93.72]) by mx.groups.io with SMTP id smtpd.web08.69853.1629381881025553253 for ; Thu, 19 Aug 2021 07:04:41 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=ZwvXS+vU; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.93.72, mailfrom: ashish.kalra@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DRujsJOR78kGzySyc1gx6eniIhHdQdIgyCHMYW8MnnG77hmz2Dfx0lVN4O+7VkZohClB/iDP4MtehWeLBOw04u4odgaY9fJZRKG5xDnuOC+TuiJp10Fn2RzNhJuWwxfsiWDjCA1o+AJoNYvebs5+r52O2/lpE73pbDldkOJkST44DeKK6LptK16CgsjIwPB6oUYLGoC2s8F9AZWCAIqgVRPGGaPvejfPaXT+csU3Mt+9WXT+ygHCX6goIa39PCfKR0K3jSuX6Q32zdyeputPd9pf7P7QsR1epE5ccGS+3trWhq/qUkjzeMpGl94HgKqXmN6WikH/5bXFp+PVf2b1Qw== 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=+MZ/BjRL4WRf7UwvhJVfGYQoAkIMBuH9EApnoYZ2CNw=; b=aGg0d2TMqaCu5C0lsg2onKqgcSDhqdItSdqV7scH9twr73PN/e8bBK+I1UI8fbTamlkusOdJhEqdUD4k26K/jROhAyNv8Nw1MOty8g/XYi79dgAdXzMIrLPbH3GVNiVNkg2bD4JWk9nEImv7FcCiGj1xCk7clEhgdX38jCwR9GeQfpmLmYevz0bUQWW4SEATqYGrGUnFdEnVeulOE9SUyEZFabZFmdUOBm/4P3B4sgWINfBWxVdNs4sT3VU5tbB//cYZvYcygyrt9EiIvG3nbxLzfuwUEy02Xxky1a6H93EmZOXttJOrexuFfceQaIb4UFScesnZgY5+htCDP0Oedw== 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=+MZ/BjRL4WRf7UwvhJVfGYQoAkIMBuH9EApnoYZ2CNw=; b=ZwvXS+vU0bjYawR7+9nvDhmcdEu+QEtK2q26k296kETs6eaG3XttRqli0r0Baq3hVi4u3vNqMsOk491hY1xAkODrnwY8Bs7/2vMuMDnON/Jyjq86bjhxg2OoAXQzgG9WkLWEQGmshCUpxe1l3bYjsdWDI2+zFZjSSBxd1Wd7Ni4= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SA0PR12MB4429.namprd12.prod.outlook.com (2603:10b6:806:73::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4436.19; Thu, 19 Aug 2021 14:04:39 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::491e:2642:bae2:8b73]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::491e:2642:bae2:8b73%7]) with mapi id 15.20.4415.024; Thu, 19 Aug 2021 14:04:39 +0000 From: "Ashish Kalra" To: devel@edk2.groups.io Cc: dovmurik@linux.vnet.ibm.com, brijesh.singh@amd.com, tobin@ibm.com, Thomas.Lendacky@amd.com, jejb@linux.ibm.com, erdemaktas@google.com, jiewen.yao@intel.com, min.m.xu@intel.com, jordan.l.justen@intel.com, ard.biesheuvel@arm.com Subject: [PATCH v7 2/6] OvmfPkg/BaseMemEncryptLib: Hypercall API for page encryption state change Date: Thu, 19 Aug 2021 14:04:29 +0000 Message-Id: <1073ba3a6a203d97192647ad3e8cade694407d7f.1629380011.git.ashish.kalra@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: SN4PR0501CA0123.namprd05.prod.outlook.com (2603:10b6:803:42::40) To SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) Return-Path: Ashish.Kalra@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from ashkalra_ubuntu_server.amd.com (165.204.77.1) by SN4PR0501CA0123.namprd05.prod.outlook.com (2603:10b6:803:42::40) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4436.9 via Frontend Transport; Thu, 19 Aug 2021 14:04:38 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3c3b67de-5fa7-41b1-82de-08d9631a48bb X-MS-TrafficTypeDiagnostic: SA0PR12MB4429: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8QeE7C59Ev1eZm1EtyQ7Ah8ha4KvAtUKtnVdWQM7bd2XQzltp3UrLfdtH0xtTFECP1ImiPNM6VKM+dV1ZUa7tEtWrkx7yUvxOQjpJBmwsaiU62rlRJcn1tM0llO77n3nyKFFMiy25mKh6qS7CUSTl6YHp1Ko3mQo7G11Zl6hzNknrwy/1mUTwy01FCMlGqP2XOpP2UvWZOp61OI9kzxp4mRf4BwSYB+GJRLWlo5GG6ATZ/SLPa9GILqtzAGu9fDOL9MLY9hAj5qTQ+YB6mXwWoYb8lLAoyc+J7sjGKC/Q178CznjdQJT/6DQJXxgKK2p/ne01tSdals93Nsm5zGFKH2B1rXgYttylLR6Mb3kixFhr3hNvCgSRU86EsjGSfMXd2Yrr8cp/WHV4kU+QSedDwU0+daPLoAevIQkw3itE7TnftdDGHxb7UZ+EBAlZqxxUDTR2gvH6ArZHkNcnBs2OdqBeFQ0rFTbHnXTC4vFG4PVKeS/Frm2EusbRchz4bwWyn6LSDFr3fjfAlP6FnH5XXjCFxd/wkIMQextzolbWTNRu6ZQy/oavXC5EGfD3jN8pujKF3pf5wVYrSkzPoUHr+fJDRRRUx+8v/mi5ke7N0phizxaGhVpgHHuUaBAZYjn2sG97eI7bIUEmXrU4or6F8slM8STqkWj9SKBYGoWVdeUbHwu4M7XmiwTOxs6Guqsjp29NNUWlblEzYUf25ypeg== 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)(346002)(366004)(396003)(376002)(39860400002)(136003)(36756003)(2906002)(66946007)(6916009)(83380400001)(6666004)(38100700002)(38350700002)(19627235002)(86362001)(5660300002)(316002)(52116002)(478600001)(7696005)(8936002)(4326008)(956004)(6486002)(186003)(66556008)(66476007)(26005)(8676002)(2616005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?WXvKFx3UomtzPIUeAm3uenkTqgPmW1LWYFwQeqN7berS6aYIi33ZPVBYP2TY?= =?us-ascii?Q?p/hU023GhlZDWjUcYfuVhAppUPJT5cAt2N/9OOVdOJSsGDmJ5M6qkw2d1eNn?= =?us-ascii?Q?2itQgMHXGsDrtGSiYcOGOgMAAzHmKf3G/sUM+BVnPMbztzxKEWUl43hOXb6P?= =?us-ascii?Q?gjNcJbTA6kNOSRsvHU/xVPGw6Lej2IcTsbdnFYEpoh91uT8fEdYUnEE8rJMK?= =?us-ascii?Q?3SuIBvwv+ynq2512pRVG2cwTmuZsLMzOn6ZycGsTkWs05y5wEadGEoYoGZTv?= =?us-ascii?Q?ZDqxPuPUB1h5E6XHM8ZVpSvGk3HMcOzmnZ8WodTjA6ppJrwWMX4ePUR2lBP5?= =?us-ascii?Q?Q7apc9IgNXeZP6/9akXqwU2tlwqB/ZfFyY2ryexTHJO8qcGyFLwgLt8lA6AA?= =?us-ascii?Q?V3xxXsaB152SWZ5xuAUD2HcnfqJdYpPLhqBnu/1nlmFLEFQqPsU0z5K2LejW?= =?us-ascii?Q?WnFvjnYk/6ywi9Hat4/zXmWurXxdZGGNXeJulN2XS54MrGs56UZDrsDSi8lJ?= =?us-ascii?Q?gM7lXiVZJJzKYQTU+pMWsSd1zf7njf+5AOzQvWRQFPmIVr3dnzcr1pC/nSkf?= =?us-ascii?Q?9I5rdYiXPgY9fnq86zywH+bLruh8jlDWzQZJw320wLzHNj43Q30W0oo7oLZC?= =?us-ascii?Q?+1EV/Q8FcnL3edhP3lsBr/EL7k5Fb4YtOgFkWhfjzba6i+1exza13vbJe7ty?= =?us-ascii?Q?vteI//PR8wGwkANFSvN48fbclMBXr5LVkcPlFDbIdm2pAC9jGGYSSaA/teUW?= =?us-ascii?Q?k35G7QMd82XxFt6+q9vkD9mhUUeyylhq80jq5OYpuTrZKip5+rQSe8l+3Dzj?= =?us-ascii?Q?uhIq7Dqx1/VW8dHDpvauj8PO/2IoIs1z7wq+SnHbFIrbVuK38PyZeo7P7tzi?= =?us-ascii?Q?NXU3UlJLs4LcEUuHk2ou34dCuhQftivQWTqqL6hxG/wh1ygsjNWvwtWqU7oV?= =?us-ascii?Q?EMoEvOx0HVq+AhAdB+5eCCrTWtfgPuSqLGfIjLLOKjCPZgs59Z39Dm3tE4mu?= =?us-ascii?Q?SpyJHcBWP+wqISHhyAhc9nelIKyZ8FO4/xPMjqAkVoltexR2lZGFNDx+WO4m?= =?us-ascii?Q?EIF5uH5BHtQhtplLdqGQ8nXnu4Q1KqcqFMmi9IxtBGeT4fw90amqiLgFEQMN?= =?us-ascii?Q?o02QuneNZ7oVTmbc6/6bzK9cFjiIgGgPJ1p1uVmIVWwWzDrsUDhoL61AJP3X?= =?us-ascii?Q?5YbTaq3UCxXaksOdfEYqIhglgu+ctpZjaUvsLs/HAEvi5XnEsXyDl3U77bEY?= =?us-ascii?Q?hWj7kkFUHi8ZnUUz/aQ8m4R6AHXkXPliuZfIInkYlAJmolYFspT26+vDiUbO?= =?us-ascii?Q?Imn8bAtjQ9zwEf8Qi+35/KqM?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3c3b67de-5fa7-41b1-82de-08d9631a48bb X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Aug 2021 14:04:39.2312 (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: NAdqOVdTyrZc2VqfXTGMrddCTTAdS0kG4Q8t/JmxfZihVXBf06e04pkasLQ6BkMKMQtdSlbdCMYypAjIbqoYQQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4429 Content-Type: text/plain From: Ashish Kalra Add API to issue hypercall on page encryption state change. By default all the SEV guest memory regions are considered encrypted, if a guest changes the encryption attribute of the page (e.g mark a page as decrypted) then notify hypervisor. Hypervisor will need to track the unencrypted pages. The information will be used during guest live migration, guest page migration and guest debugging. This hypercall is used to notify hypervisor when the page's encryption state changes. Cc: Jordan Justen Cc: Ard Biesheuvel Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- OvmfPkg/Include/Library/MemEncryptSevLib.h | 52 ++++++++++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf | 1 + OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c | 27 +++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf | 1 + OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c | 20 ++++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.nasm | 33 ++++++++++ OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c | 64 ++++++++++++++++++++ 7 files changed, 198 insertions(+) diff --git a/OvmfPkg/Include/Library/MemEncryptSevLib.h b/OvmfPkg/Include/Library/MemEncryptSevLib.h index b619cadf51..9d46c5f5d6 100644 --- a/OvmfPkg/Include/Library/MemEncryptSevLib.h +++ b/OvmfPkg/Include/Library/MemEncryptSevLib.h @@ -234,4 +234,56 @@ MemEncryptSevClearMmioPageEncMask ( IN UINTN NumPages ); +/** + This hypercall is used to notify hypervisor when the page's encryption + state changes. + + @param[in] PhysicalAddress The physical address that is the start address + of a memory region. + @param[in] Pages Number of pages in memory region. + @param[in] IsEncrypted Encrypted or Decrypted. + + @retval RETURN_SUCCESS Hypercall returned success. + @retval RETURN_UNSUPPORTED Hypercall not supported. + @retval RETURN_NO_MAPPING Hypercall returned error. +**/ +RETURN_STATUS +EFIAPI +SetMemoryEncDecHypercall3 ( + IN UINTN PhysicalAddress, + IN UINTN Pages, + IN BOOLEAN IsEncrypted + ); + +#define KVM_HC_MAP_GPA_RANGE 12 +#define KVM_MAP_GPA_RANGE_PAGE_SZ_4K 0 +#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M BIT0 +#define KVM_MAP_GPA_RANGE_PAGE_SZ_1G BIT1 +#define KVM_MAP_GPA_RANGE_ENC_STATE(n) ((n) << 4) +#define KVM_MAP_GPA_RANGE_ENCRYPTED KVM_MAP_GPA_RANGE_ENC_STATE(1) +#define KVM_MAP_GPA_RANGE_DECRYPTED KVM_MAP_GPA_RANGE_ENC_STATE(0) + +/** + Interface exposed by the ASM implementation of the core hypercall + + @param[in] HypercallNum KVM_HC_MAP_GPA_RANGE hypercall. + @param[in] PhysicalAddress The physical address that is the start address + of a memory region. + @param[in] Pages Number of pages in memory region. + @param[in] 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) + + @retval Hypercall returned status. +**/ +UINTN +EFIAPI +SetMemoryEncDecHypercall3AsmStub ( + IN UINTN HypercallNum, + IN UINTN PhysicalAddress, + IN UINTN Pages, + IN UINTN Attributes + ); + #endif // _MEM_ENCRYPT_SEV_LIB_H_ diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf index f2e162d680..0c28afadee 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf @@ -38,6 +38,7 @@ X64/PeiDxeVirtualMemory.c X64/VirtualMemory.c X64/VirtualMemory.h + X64/AsmHelperStub.nasm [Sources.IA32] Ia32/MemEncryptSevLib.c diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c b/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c index be260e0d10..516d639489 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/Ia32/MemEncryptSevLib.c @@ -136,3 +136,30 @@ MemEncryptSevClearMmioPageEncMask ( // return RETURN_UNSUPPORTED; } + +/** + This hyercall is used to notify hypervisor when the page's encryption + state changes. + + @param[in] PhysicalAddress The physical address that is the start address + of a memory region. + @param[in] Pages Number of Pages in the memory region. + @param[in] IsEncrypted Encrypted or Decrypted. + + @retval RETURN_SUCCESS Hypercall returned success. + @retval RETURN_UNSUPPORTED Hypercall not supported. + @retval RETURN_NO_MAPPING Hypercall returned error. +**/ +RETURN_STATUS +EFIAPI +SetMemoryEncDecHypercall3 ( + IN UINTN PhysicalAddress, + IN UINTN Pages, + IN BOOLEAN IsEncrypted + ) +{ + // + // Memory encryption bit is not accessible in 32-bit mode + // + return RETURN_UNSUPPORTED; +} diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf index 03a78c32df..3233ca7979 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf @@ -38,6 +38,7 @@ X64/PeiDxeVirtualMemory.c X64/VirtualMemory.c X64/VirtualMemory.h + X64/AsmHelperStub.nasm [Sources.IA32] Ia32/MemEncryptSevLib.c diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c b/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c index d9f7befcd2..ebb1c39319 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/SecMemEncryptSevLibInternal.c @@ -118,6 +118,26 @@ MemEncryptSevLiveMigrationIsEnabled ( return FALSE; } +/** + Interface exposed by the ASM implementation of the core hypercall + + @retval Hypercall returned status. +**/ +UINTN +EFIAPI +SetMemoryEncDecHypercall3AsmStub ( + IN UINTN HypercallNum, + IN UINTN PhysicalAddress, + IN UINTN Pages, + IN UINTN Attributes + ) +{ + // + // Not used in SEC phase. + // + return RETURN_UNSUPPORTED; +} + /** Returns the SEV encryption mask. diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.nasm b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.nasm new file mode 100644 index 0000000000..0ec35dd9b6 --- /dev/null +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/AsmHelperStub.nasm @@ -0,0 +1,33 @@ +/** @file + + ASM helper stub to invoke hypercall + + Copyright (c) 2021, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +DEFAULT REL +SECTION .text + +; UINTN +; EFIAPI +; SetMemoryEncDecHypercall3AsmStub ( +; IN UINTN HypercallNum, +; IN UINTN Arg1, +; IN UINTN Arg2, +; IN UINTN Arg3 +; ); +global ASM_PFX(SetMemoryEncDecHypercall3AsmStub) +ASM_PFX(SetMemoryEncDecHypercall3AsmStub): + ; UEFI calling conventions require RBX to + ; be nonvolatile/callee-saved. + push rbx + mov rax, rcx ; Copy HypercallNumber to rax + mov rbx, rdx ; Copy Arg1 to the register expected by KVM + mov rcx, r8 ; Copy Arg2 to register expected by KVM + mov rdx, r9 ; Copy Arg3 to register expected by KVM + vmmcall ; Call VMMCALL + pop rbx + ret diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c index a57e8fd37f..a6246d4312 100644 --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/MemEncryptSevLib.c @@ -143,3 +143,67 @@ MemEncryptSevClearMmioPageEncMask ( ); } + +/** + This hyercall is used to notify hypervisor when the page's encryption + state changes. + + @param[in] PhysicalAddress The physical address that is the start address + of a memory region. + @param[in] Pages Number of Pages in the memory region. + @param[in] IsEncrypted Encrypted or Decrypted. + + @retval RETURN_SUCCESS Hypercall returned success. + @retval RETURN_UNSUPPORTED Hypercall not supported. + @retval RETURN_NO_MAPPING Hypercall returned error. +**/ +RETURN_STATUS +EFIAPI +SetMemoryEncDecHypercall3 ( + IN UINTN PhysicalAddress, + IN UINTN Pages, + IN BOOLEAN IsEncrypted + ) +{ + RETURN_STATUS Ret; + UINTN Error; + UINTN EncryptState; + + Ret = RETURN_UNSUPPORTED; + + if (MemEncryptSevLiveMigrationIsEnabled ()) { + Ret = RETURN_SUCCESS; + // + // The encryption bit is set/clear on the smallest page size, hence + // use the 4k page size in MAP_GPA_RANGE hypercall below. + // + // Also, when the GCD map is being walked and the c-bit being cleared + // from MMIO and NonExistent memory spaces, the physical address + // range being passed may not be page-aligned and adding an assert + // here prevents booting. Hence, rounding it down when calling + // SetMemoryEncDecHypercall3AsmStub below. + // + + EncryptState = IsEncrypted ? KVM_MAP_GPA_RANGE_ENCRYPTED : + KVM_MAP_GPA_RANGE_DECRYPTED; + + Error = SetMemoryEncDecHypercall3AsmStub ( + KVM_HC_MAP_GPA_RANGE, + PhysicalAddress & ~EFI_PAGE_MASK, + Pages, + KVM_MAP_GPA_RANGE_PAGE_SZ_4K | EncryptState + ); + + if (Error != 0) { + DEBUG ((DEBUG_ERROR, + "SetMemoryEncDecHypercall3 failed, Phys = %x, Pages = %d, Err = %Ld\n", + PhysicalAddress, + Pages, + (INT64)Error)); + + Ret = RETURN_NO_MAPPING; + } + } + + return Ret; +} -- 2.17.1