From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (NAM11-BN8-obe.outbound.protection.outlook.com [40.107.236.82]) by mx.groups.io with SMTP id smtpd.web12.6554.1607040220312075004 for ; Thu, 03 Dec 2020 16:03:40 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=nypSz/tK; 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.236.82, mailfrom: ashish.kalra@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h4F0Fwh4arIvccpgOHOI+FcwLqocuEOa/Etwa3X9nOnuwx44+YQZpug1fz4wBZDGO3rOiYVqsOQjCQdKqcbJMztDoKRhECPzLenIOwrDoDVF9QvXaObLOkdo8VyW5XtKqg5kUAlqCXQPGhVcpD37hMZsQD6b5l0bwdHnWPAdNgNzaZSmOqor8lcfMwSM9tI6P1KdzGcmVWEZIHjnBRBljRz77cvmbTOJEVX/WYXNMnM76vLqjgXrPOF1Gs+tFzH+Fz4sIg5E9VzdsS0Va4HzuJF3gbDLufaonD5nM03ucuOVR5W9/KayWtZeFi/TJWes81UYPNmC5b4wkN84o6FbPQ== 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=1t6oC4lZ57xjNaS6GKs/9SY1UpL5VsqK5gUKYx1x8Y8=; b=nNTHgYLW9LXGqGeo5SWFkGLE/aNXi3KUnowEtOIYmttIkAzjzbu7KiGYt/wFcJ4E9yuF2xrpfGgXq7hMd12mZN6O2BTWzCuMNMM4bz8lPi87oxSv6C9msdZkQ4og6wCT7V9rcuLuxu6A7f1vvuH5jfMzgABerkT2xX8BvaaZiMQ80/rQJIOypTaH/kSu3uS0lsvaIq7UiB+I9Nmu1Q6zv0LovBkejTvPjRMmQkqMCmR/pLmPnjdBpp8KchtEdPxYEZxvjMcLF/01hD5CjEXxhv1ZnlqPWUxfiI4dC88IvVBReZUA8ssdOcueok3w+CWi2gfkozFS/yFxIN7jIXLzJA== 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=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1t6oC4lZ57xjNaS6GKs/9SY1UpL5VsqK5gUKYx1x8Y8=; b=nypSz/tK5CsBzxLMaHh2hFYuFKQ+nArjtBuFL2IjNdvn2U5wdJLB5eJhCTjJE6+z8ts7DFIEUY4NS2vHFqNs7WouHVmnQMuRp7+RK+yWe9nGef+5SEXYT1WQ20C8Q4E/Rsd11lipdtdyeGLC0C5oxKUC2Lyz3Y8M6ZRdo8xtLu8= 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 SN6PR12MB2783.namprd12.prod.outlook.com (2603:10b6:805:78::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3611.25; Fri, 4 Dec 2020 00:03:37 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::d8f2:fde4:5e1d:afec]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::d8f2:fde4:5e1d:afec%3]) with mapi id 15.20.3611.025; Fri, 4 Dec 2020 00:03:37 +0000 From: "Ashish Kalra" To: devel@edk2.groups.io Cc: dovmurik@linux.vnet.ibm.com, brijesh.singh@amd.com, tobin@ibm.com, Jon.Grimm@amd.com, Thomas.Lendacky@amd.com, jejb@linux.ibm.com, frankeh@us.ibm.com, dgilbert@redhat.com, lersek@redhat.com, jordan.l.justen@intel.com, ard.biesheuvel@arm.com Subject: [PATCH v3 1/3] OvmfPkg/MemEncryptHypercallLib: add library to support SEV hypercalls. Date: Fri, 4 Dec 2020 00:03:27 +0000 Message-Id: <5d84e29cb02eada513738fb4f0c54a6dfe35f416.1607038824.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: SN4PR0501CA0031.namprd05.prod.outlook.com (2603:10b6:803:40::44) 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 SN4PR0501CA0031.namprd05.prod.outlook.com (2603:10b6:803:40::44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3654.7 via Frontend Transport; Fri, 4 Dec 2020 00:03:36 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 0cdc504d-669e-4c8a-21ee-08d897e80c53 X-MS-TrafficTypeDiagnostic: SN6PR12MB2783: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PDy5MGXeRNlwlz0o+OOgK+NilzMvWuAjLeLWeUfigWYG4oNOdZTTp9G48wni2mmztUO9eJHaJ2VDKv1hAIEcuBoobon0nuYl5ooZy2ra24FuhWF+MZtHiqSjoCUHdRiGTXcw3fcaeKd+m19d6nTvYPzUgnegqmfquLp9l1c7+DXt9vVhfNU8+g91OqTR8yS6/LPxktr8QDVZOjtVCrw+UUw7qii/4XmQOSoxXpxwTbTYwspJfONuNNnBaxzoZrcAjSIg+kPzApBQ79en0SP2Rwwwyv7WQCKZujnl4FhjjMrf6GjXHbHAE4iepIA4z1jya/2assI+9tJhziySXV/r8A== 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)(376002)(346002)(136003)(396003)(366004)(478600001)(86362001)(66476007)(5660300002)(6666004)(66946007)(66556008)(956004)(19627235002)(2616005)(83380400001)(7696005)(316002)(52116002)(2906002)(16526019)(8676002)(186003)(6916009)(6486002)(26005)(8936002)(36756003)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?0J8to90XTGzTsCAeSLamUyhqdPa7pEgO0sy64pNiPduHSmEO/hdD1Gk0wLAj?= =?us-ascii?Q?HMT6G781vh2S66moBtdtd4mSLhlPd6PXHwC4UnyhoTUyImFNvDihLnosyPki?= =?us-ascii?Q?06wQcizPHL4WN86gj1X5nkjXbO2kuHGheGRgCz2OlCtzSogNPZX3iWRz8kIW?= =?us-ascii?Q?xpGvZymVHnag3VmPoX+HeXjXWZ+ihquaBtKQ+YBkw9Z5HiWENMzgoW/V2Xrk?= =?us-ascii?Q?ehiW/JVv7Z6yf1A/1pG1A/1mc0o5+kWbw8EtMuAdJcX6PUEadOYip9LdJ2FU?= =?us-ascii?Q?d9wP9k+sM1Jp0+6d+FsoDAgvyogMJPbD93yfVuB1qHstg0ypAgN1GsVy0OVo?= =?us-ascii?Q?z+PFFYx6g28vV8Y97+4G1WT2gd0aasvBWdsp+wyOIEfynzjySZXPrlED8pPf?= =?us-ascii?Q?G7aXJ0GDCt9buxWVLuMQKS/1Yu/bBGd7TDbL1yfREzuASmpdRK3ByrQcpLCX?= =?us-ascii?Q?KqPyHHHl/s7DDbnmrye9m+iZ3doyKjPXrW6Tdl9EXb5NK16abHO5kFt77M3x?= =?us-ascii?Q?PHtLTzacPv7kkguok6ucdbRV/mbtRmsKvAF014e/8ZvKwm67rGB8yvkqTtAN?= =?us-ascii?Q?PQQ2Le0Lf94ZXE6abciQ48bBPb5VSA/nXmSO9W82n0N5c2b8fwHPsgO9W9ej?= =?us-ascii?Q?IJWgMpHwSzB9agoVlP7ob+ltgf+GWn03B3Lsk9bbs6+cqjPGr8KR2Aq5v7d3?= =?us-ascii?Q?h38TcxDGs2Bk9ZoETkfGQ+7qnzIGs1Z2ohzT60+VC30zBZ3w9QNTxldADolT?= =?us-ascii?Q?b42fvvxN94J+jcb9Dkk8zIGZZNJWHLncxUDZ5i+S2xOQVmtKL7CwRBtATrAO?= =?us-ascii?Q?Ui/FDprVNK7+Ua5YcRb+fib7QsLThGLl8OSJt1SGvpND4T7PPrRIOTqCIFv/?= =?us-ascii?Q?Mag6I+jBuCYYcUe7tLoqSx8tG9QMTrEJAUhIDFImvf1XVnChuyINzVJOYvdZ?= =?us-ascii?Q?yQXMp4rV5WeWYBswHCZiaP1/U42IMovO4ic3Fcry5rs7XCjq9CVAGeiZFI9B?= =?us-ascii?Q?sd0H?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0cdc504d-669e-4c8a-21ee-08d897e80c53 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2020 00:03:36.9585 (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: QmalxMwPPg3kkcLaUqG9rSqFIDVrsqcVde0KK8CELiPgQiMZmQGSjpWDgY+FOKfeZLmr9qe1+JBhB+4xk1R8fg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2783 Content-Type: text/plain From: Ashish Kalra Add SEV and SEV-ES hypercall abstraction library to support SEV Page encryption/deceryption status hypercalls for SEV and SEV-ES guests. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Signed-off-by: Ashish Kalra --- OvmfPkg/Include/Library/MemEncryptHypercallLib.h | 37 +++++++ OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.c | 105 ++++++++++++++++++++ OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf | 39 ++++++++ OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm | 39 ++++++++ OvmfPkg/OvmfPkgX64.dsc | 1 + 5 files changed, 221 insertions(+) diff --git a/OvmfPkg/Include/Library/MemEncryptHypercallLib.h b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h new file mode 100644 index 0000000000..cd46a7f2b3 --- /dev/null +++ b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h @@ -0,0 +1,37 @@ +/** @file + + Define Secure Encrypted Virtualization (SEV) hypercall library. + + Copyright (c) 2020, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _MEM_ENCRYPT_HYPERCALL_LIB_H_ +#define _MEM_ENCRYPT_HYPERCALL_LIB_H_ + +#include + +#define SEV_PAGE_ENC_HYPERCALL 12 + +/** + This hyercall is used to notify hypervisor when a page is marked as + 'decrypted' (i.e C-bit removed). + + @param[in] PhysicalAddress The physical address that is the start address + of a memory region. + @param[in] Length The length of memory region + @param[in] Mode SetCBit or ClearCBit + +**/ + +VOID +EFIAPI +SetMemoryEncDecHypercall3 ( + IN UINTN PhysicalAddress, + IN UINTN Length, + IN UINTN Mode + ); + +#endif diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.c b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.c new file mode 100644 index 0000000000..f1136b7d36 --- /dev/null +++ b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.c @@ -0,0 +1,105 @@ +/** @file + + Secure Encrypted Virtualization (SEV) hypercall helper library + + Copyright (c) 2020, AMD Incorporated. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// +// Interface exposed by the ASM implementation of the core hypercall +// +// + +VOID +EFIAPI +SetMemoryEncDecHypercall3AsmStub ( + IN UINTN HypercallNum, + IN UINTN PhysicalAddress, + IN UINTN Length, + IN UINTN Mode + ); + +/** + This function returns the current CPU privilege level, implemented + in ASM helper stub. + +**/ + +UINT8 +EFIAPI +GetCurrentCpuPrivilegeLevel ( + VOID + ); + +STATIC +VOID +GhcbSetRegValid ( + IN OUT GHCB *Ghcb, + IN GHCB_REGISTER Reg + ) +{ + UINT32 RegIndex; + UINT32 RegBit; + + RegIndex = Reg / 8; + RegBit = Reg & 0x07; + + Ghcb->SaveArea.ValidBitmap[RegIndex] |= (1 << RegBit); +} + +VOID +EFIAPI +SetMemoryEncDecHypercall3 ( + IN PHYSICAL_ADDRESS PhysicalAddress, + IN UINTN Pages, + IN UINTN Mode + ) +{ + if (MemEncryptSevEsIsEnabled ()) { + MSR_SEV_ES_GHCB_REGISTER Msr; + GHCB *Ghcb; + BOOLEAN InterruptState; + UINT64 Status; + + Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB); + Ghcb = Msr.Ghcb; + + VmgInit (Ghcb, &InterruptState); + + Ghcb->SaveArea.Rax = SEV_PAGE_ENC_HYPERCALL; + GhcbSetRegValid (Ghcb, GhcbRax); + Ghcb->SaveArea.Rbx = PhysicalAddress; + GhcbSetRegValid (Ghcb, GhcbRbx); + Ghcb->SaveArea.Rcx = Pages; + GhcbSetRegValid (Ghcb, GhcbRcx); + Ghcb->SaveArea.Rdx = Mode; + GhcbSetRegValid (Ghcb, GhcbRdx); + Ghcb->SaveArea.Cpl = GetCurrentCpuPrivilegeLevel(); + GhcbSetRegValid (Ghcb, GhcbCpl); + + Status = VmgExit (Ghcb, SVM_EXIT_VMMCALL, 0, 0); + if (Status) { + DEBUG ((DEBUG_ERROR, "SVM_EXIT_VMMCALL failed %lx\n", Status)); + } + VmgDone (Ghcb, InterruptState); + } else { + SetMemoryEncDecHypercall3AsmStub ( + SEV_PAGE_ENC_HYPERCALL, + PhysicalAddress, + Pages, + Mode); + } +} diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf new file mode 100644 index 0000000000..1936fe5b37 --- /dev/null +++ b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf @@ -0,0 +1,39 @@ +## @file +# Library provides the hypervisor helper functions for SEV guest +# +# Copyright (c) 2020 Advanced Micro Devices. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 1.25 + BASE_NAME = MemEncryptHypercallLib + FILE_GUID = 86f2501e-f128-45f3-91c4-3cff31656ca8 + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = MemEncryptHypercallLib|SEC PEI_CORE PEIM DXE_DRIVER DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_DRIVER + +# +# The following information is for reference only and not required by the build +# tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Packages] + MdeModulePkg/MdeModulePkg.dec + MdePkg/MdePkg.dec + UefiCpuPkg/UefiCpuPkg.dec + OvmfPkg/OvmfPkg.dec + +[Sources.X64] + MemEncryptHypercallLib.c + X64/AsmHelperStub.nasm + +[LibraryClasses] + BaseLib + DebugLib + VmgExitLib diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm new file mode 100644 index 0000000000..5d8a7aa85a --- /dev/null +++ b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm @@ -0,0 +1,39 @@ +DEFAULT REL +SECTION .text + +; VOID +; EFIAPI +; SetMemoryEncDecHypercall3AsmStub ( +; IN UINT HypercallNum, +; IN INTN Arg1, +; IN INTN Arg2, +; IN INTN Arg3 +; ); +global ASM_PFX(SetMemoryEncDecHypercall3AsmStub) +ASM_PFX(SetMemoryEncDecHypercall3AsmStub): + ; UEFI calling conventions require RBX to + ; be nonvolatile/callee-saved. + push rbx + ; Copy HypercallNumber to rax + mov rax, rcx + ; Copy Arg1 to the register expected by KVM + mov rbx, rdx + ; Copy Arg2 to register expected by KVM + mov rcx, r8 + ; Copy Arg2 to register expected by KVM + mov rdx, r9 + ; Call VMMCALL + vmmcall + pop rbx + ret + +; UINT8 +; EFIAPI +; GetCurrentCpuPrivilegeLevel ( +; VOID +; ); +global ASM_PFX(GetCurrentCpuPrivilegeLevel) +ASM_PFX(GetCurrentCpuPrivilegeLevel): + mov ax, cs + and al, 0x3 + ret diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index e59ae05b73..97c31c7586 100644 --- a/OvmfPkg/OvmfPkgX64.dsc +++ b/OvmfPkg/OvmfPkgX64.dsc @@ -174,6 +174,7 @@ VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/BaseMemEncryptSevLib.inf + MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf !if $(SMM_REQUIRE) == FALSE LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf !endif -- 2.17.1