From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (NAM02-SN1-obe.outbound.protection.outlook.com [40.107.96.71]) by mx.groups.io with SMTP id smtpd.web12.494.1624402207046820778 for ; Tue, 22 Jun 2021 15:50:07 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=IkBmg6Yr; 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.96.71, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nVPgYHoXKW2h4v0xYsslb4cVmgW9c4/y9kZqLKITmikLDd/a8vo7rWv6QUk46A9b0488H9tK8wYy9JKTtlGlCk8fHqrbSy/J58yRhaU4MlPFtDGWiYGiTXzRILg8r+ysmaYVOSv/lfvf0yGHsOJB9QEgtAznDP0OKDpoNQNom59VkYjUcJkFRYyy+6FEZVMDQRg20NUJ1ZQtkX89D18+nzshWIQ8kJmdWpoHd83IrWqI/PNc3mH90AZ9y3vXTZp+aWCqufU3R7KQ3LsxuB2+VXYWNbanUjKneFK/Al+Lxcj+LnTjmcbyEUrnYUTHak5vRvIV4hcB14UtiaoQ4IRb1g== 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=vjoZNzXDDEM1tpValSt/q4GmZ4Qhs5gA0nkt0NJGWjM=; b=mKb6loJZj4rKtsirMCvOmapzqm/e2vA8zrhinfZgQ6z64xbrlm7TdRZt0lVzCjoCbow8pjSSY1xEA2tDCVbi9t+9t5AAixqJwR1fIweem3NSYl62aSa0znKPQ/QSOjUOeZ9uZ1UOPXF1jqD//cEg6gskNdxaiaHyLfls8mYy/tkAI65N9Rv8mOHp1a1CFtEJlnMBWn6G/nD9MrAZyKfQXU/jSaeCYaHTwdxMMZRTNRYJcqtBFHfe1/JYn3QbcxMArOYSKE/zh1ga5rVte4iyRtx4w/oH8wSODcBQH/5ePNRmQOlBbphg3au+yzU682qY+dq50RFZCzShDTyx0gXHyg== 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=vjoZNzXDDEM1tpValSt/q4GmZ4Qhs5gA0nkt0NJGWjM=; b=IkBmg6YrBZ8MANJL7o+u/xEXybTkDkBTkQKdh3oAfbVYtUJ2IzkrMRD/MzK3xp1fPvjizN3JtUVZpbcL0kCVMLdS+Tv7qwerVyLWyYXmKpNNxMDVG0YsR8E4BrGH22p2Fq4dMdX+k4juJFaGf+CcYQ9VHTgm4ZbcAZR0lW3yCJ8= Authentication-Results: arm.com; dkim=none (message not signed) header.d=none;arm.com; dmarc=none action=none header.from=amd.com; Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM6PR12MB4812.namprd12.prod.outlook.com (2603:10b6:5:1ff::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4242.19; Tue, 22 Jun 2021 22:50:05 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::6437:2e87:f7dc:a686]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::6437:2e87:f7dc:a686%12]) with mapi id 15.20.4242.023; Tue, 22 Jun 2021 22:50:05 +0000 Subject: Re: [PATCH v4 2/4] OvmfPkg/BaseMemEncryptLib: Support to issue unencrypted hypercall To: Ashish Kalra , devel@edk2.groups.io Cc: brijesh.singh@amd.com, jejb@linux.ibm.com, erdemaktas@google.com, jiewen.yao@intel.com, min.m.xu@intel.com, lersek@redhat.com, jordan.l.justen@intel.com, ard.biesheuvel@arm.com References: <3bfdbe553d597f489a03c21fbc0f6c614f92c32f.1624281247.git.ashish.kalra@amd.com> From: "Lendacky, Thomas" Message-ID: <6fa3515f-81b4-ce03-f3c0-2358273b5dba@amd.com> Date: Tue, 22 Jun 2021 17:50:03 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 In-Reply-To: <3bfdbe553d597f489a03c21fbc0f6c614f92c32f.1624281247.git.ashish.kalra@amd.com> X-Originating-IP: [67.79.209.213] X-ClientProxiedBy: SN6PR08CA0035.namprd08.prod.outlook.com (2603:10b6:805:66::48) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from office-ryzen.texastahm.com (67.79.209.213) by SN6PR08CA0035.namprd08.prod.outlook.com (2603:10b6:805:66::48) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4242.19 via Frontend Transport; Tue, 22 Jun 2021 22:50:04 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 725a81a6-609d-4437-f413-08d935d013bd X-MS-TrafficTypeDiagnostic: DM6PR12MB4812: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8EEUuO3zWNFaIwx7L7VARTJtL9U7+vbatVY3FA4XcVxDkHZTfN2g+nPnPo/mz4m2yf/hdrdgiXdvZ5OU4w+5r8LzUovWSU3jX+VwrJysMgZ9GHOBP0Qks9Y/Dej7ZC62M4FaprbWw6KhfMxT30g3jyERQEaBByjO/aNpo+/jZKiCULD6SUQn+Y/usgqoNgfEQ4raxO+mO0WTX+0GSvNtS9O3G6Kj5OsH2kX2tiHgoz+G0Sd8K37N1ikaO8uqQDlUDSrFV5Iyj/ugMjy6knLjY3OCM7q/FBP4MlnvOhEboZ7CSjHx3YL23qVQZUxSWk57ZXlgifr74m+t82T1mgtte7ejaeyLdkWMbiMsWJHl5FVqp/dqI+B8kUZ8t+bOic/gjm32LoY+RHm1lFK5z2qbcN1Fi8OcJ9iVbHgc6FBphPo7ohd8KRg/n1W9J3qoydVtZJRuyNMx56Q2kNixdSPdSNAVCgfvpoKg0JX8sSo3VPXUpskNsBR23JB+5Cq4uZCgfI+XgaEYwuO5xmrLjxZ8q7um7y7yjmbDVPGuE+4wG12/7W7r0gF5yzQu533u2boJ/clk1NLgp96whLN7D94bJLBL+EPtdEM031Z0C7eQzy3/oD1tRDKZmg6y7b+C1O4110uWZt+nxYO+glsN99PVdYZxyxpugR0v2R00r/gDQgVrLlMhxur4042XuIMOHtbdmH8dUxWOeIgE6ittNRL7cpUZqU7q5mTxbDPFDvTI8wzLL7F3cGyRLIgXGa6yKs1L X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1355.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(39850400004)(396003)(376002)(136003)(366004)(2906002)(316002)(4326008)(83380400001)(36756003)(86362001)(8936002)(53546011)(66476007)(66556008)(31696002)(66946007)(8676002)(478600001)(19627235002)(31686004)(38100700002)(2616005)(6486002)(16526019)(26005)(186003)(956004)(5660300002)(6512007)(6506007)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?ZHVKdmhMRHJkL3pwVEFOdlNFR3doZlEzOTZ6U05RR2ZpbXBEN3hiSTdkMWNV?= =?utf-8?B?b1FSRkxkdkZpcWIzQzc4cStK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Tenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jun 2021 22:50:05.1910 (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: a+yDihH7jmILIXyx6OdA+ev0B2QU6Sy36ThVMcCoXbh7BLeA7JUipCB5o45gthopILBPaqsHuw8YJ2NUozw65A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4812 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 6/21/21 8:57 AM, Ashish Kalra wrote: > From: Brijesh Singh > > 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. > > Invoke hypercall via the new hypercall library. > > This hypercall is used to notify hypervisor when a page is marked as > 'decrypted' (i.e C-bit removed). > > Cc: Jordan Justen > Cc: Laszlo Ersek > Cc: Ard Biesheuvel > > Signed-off-by: Brijesh Singh > Signed-off-by: Ashish Kalra > --- > OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf | 1 + > OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf | 1 + > OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c | 22 ++++++++++++++++++++ > 3 files changed, 24 insertions(+) > > diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf > index f2e162d680..aefcd7c0f7 100644 > --- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf > +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf > @@ -49,6 +49,7 @@ > DebugLib > MemoryAllocationLib > PcdLib > + MemEncryptHypercallLib > > [FeaturePcd] > gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire > diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf > index 03a78c32df..7503f56a0b 100644 > --- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf > +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf > @@ -49,6 +49,7 @@ > DebugLib > MemoryAllocationLib > PcdLib > + MemEncryptHypercallLib > > [FeaturePcd] > gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire > diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c > index c696745f9d..12b3a9fcfb 100644 > --- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c > +++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c > @@ -15,6 +15,7 @@ > #include > #include > #include > +#include > > #include "VirtualMemory.h" > > @@ -585,6 +586,9 @@ SetMemoryEncDec ( > UINT64 AddressEncMask; > BOOLEAN IsWpEnabled; > RETURN_STATUS Status; > + UINTN Size; > + BOOLEAN CBitChanged; > + PHYSICAL_ADDRESS OrigPhysicalAddress; > > // > // Set PageMapLevel4Entry to suppress incorrect compiler/analyzer warnings. > @@ -636,6 +640,10 @@ SetMemoryEncDec ( > > Status = EFI_SUCCESS; > > + Size = Length; > + CBitChanged = FALSE; > + OrigPhysicalAddress = PhysicalAddress; > + > while (Length != 0) > { > // > @@ -695,6 +703,7 @@ SetMemoryEncDec ( > )); > PhysicalAddress += BIT30; > Length -= BIT30; > + CBitChanged = TRUE; > } else { > // > // We must split the page > @@ -749,6 +758,7 @@ SetMemoryEncDec ( > SetOrClearCBit (&PageDirectory2MEntry->Uint64, Mode); > PhysicalAddress += BIT21; > Length -= BIT21; > + CBitChanged = TRUE; > } else { > // > // We must split up this page into 4K pages > @@ -791,6 +801,7 @@ SetMemoryEncDec ( > SetOrClearCBit (&PageTableEntry->Uint64, Mode); > PhysicalAddress += EFI_PAGE_SIZE; > Length -= EFI_PAGE_SIZE; > + CBitChanged = TRUE; > } > } > } > @@ -808,6 +819,17 @@ SetMemoryEncDec ( > // > CpuFlushTlb(); > > + // > + // Notify Hypervisor on C-bit status > + // > + if (CBitChanged) { > + SetMemoryEncDecHypercall3 ( > + OrigPhysicalAddress, > + EFI_SIZE_TO_PAGES(Size), > + KVM_MAP_GPA_RANGE_ENC_STAT(!Mode) > + ); > + } Maybe add a comment here that the "Mode" doesn't care whether the change was for 4k vs 2mb vs 1gb. It is just tracking the total number of pages changed (should you just remove those #defines for the page sizes then?) which can be done by calculating everything at the 4k level. Thanks, Tom > + > Done: > // > // Restore page table write protection, if any. >