From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (NAM11-CO1-obe.outbound.protection.outlook.com [40.107.220.63]) by mx.groups.io with SMTP id smtpd.web11.448.1624405107991902344 for ; Tue, 22 Jun 2021 16:38:28 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=2VNHBE29; 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.220.63, mailfrom: brijesh.singh@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YC8iXHogs9EJATWAuRiFfXXWcMqAm18YYa3Q8eFewYBvWb+yu8LRsuyhbCfQYPbwBwU2ejOglluZjkOsE+1iwPHjhma0OKwoghX1U5GP0b542xI46+/JVl93Ciyls8VtaHrtqoLDVa3x2TsxUXL6Na0ob2Kabw9mFlEsXNQQRxiz7Ab+77Q+GoJ8csNy9hh8lRazKtZKvNAZVMTrNBMWDPhc/rF7TbmBqoWLecojBIUTl7IbTn9jdv2ohUc2puq15N2pouSvt+wqmJb808VAhiDsO23GdG8XQwiLo+4O+ACxyS/cf/ho5zeRQRQ67Wfk7XZesZwaPWPVR6egWMX5MA== 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=93NgaWOuCFDPccnpBBQFYFs6wi7QZLVjOlsdIrheUyk=; b=eg05CtK1nkbb8uKR8OWRIT+TGZObplvPXg48bGiq0oRs2tywu/2tkyku+iyY2Oc/H8C/ZulLAPOuGoUMhOBm6aibGG309HuwcoVHlAD5nx5NxbadcsjWZkQ6ZKTow/WpQ4EmyA8ftklWCQ/DEh/m6tO9hAm/KwKDEoUle608HPeMXBCNk/9fi3EJQcveIL9e0v3iFUunTxqo1I/22/u3SXGB5DfRIvyNp5jvF4Ps1PxoZXHxcxwgFUQnm+yJJRQc96v/eE9l3hlQDz2A5+miLGZRNXvNMEmv1mF/h/hsfKwK1WNo7QwOUnBt/Bk+Okb5tkwbMYBN0RDWXmoJWBtTWw== 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=93NgaWOuCFDPccnpBBQFYFs6wi7QZLVjOlsdIrheUyk=; b=2VNHBE29CepO/RpJeC/RO6qIF/TYYoBh1xf00sbVaIntVoBBOyUjs7g+CE5akiqStjDknh4riCBQSjWjs+l62IrrhbKdhM/7NHEqN2vxIDsGk4kVUOdhFJVB1Gaq6nkwjiNHYJDXHKLiDXTIYyV50cIHlDUYm+jVH/mp+KgEIIk= Authentication-Results: arm.com; dkim=none (message not signed) header.d=none;arm.com; dmarc=none action=none header.from=amd.com; Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3827.namprd12.prod.outlook.com (2603:10b6:a03:1ab::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4264.18; Tue, 22 Jun 2021 23:38:25 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4242.023; Tue, 22 Jun 2021 23:38:25 +0000 Cc: brijesh.singh@amd.com, devel@edk2.groups.io, 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 Subject: Re: [PATCH v4 1/4] OvmfPkg/MemEncryptHypercallLib: add library to support SEV hypercalls. To: Ashish Kalra , Tom Lendacky References: <7d0a30a022a7d3d3e056af8f79b87ed9991d2f52.1624281247.git.ashish.kalra@amd.com> <20210622232020.GA6843@ashkalra_ubuntu_server> From: "Brijesh Singh" Message-ID: <02600e09-bc55-73e7-971a-c7301f69db37@amd.com> Date: Tue, 22 Jun 2021 18:38:20 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 In-Reply-To: <20210622232020.GA6843@ashkalra_ubuntu_server> X-Originating-IP: [165.204.77.11] X-ClientProxiedBy: SA0PR13CA0022.namprd13.prod.outlook.com (2603:10b6:806:130::27) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) Return-Path: brijesh.singh@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [172.31.11.236] (165.204.77.11) by SA0PR13CA0022.namprd13.prod.outlook.com (2603:10b6:806:130::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4264.9 via Frontend Transport; Tue, 22 Jun 2021 23:38:23 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d44df6af-b8ac-4257-ecba-08d935d6d41b X-MS-TrafficTypeDiagnostic: BY5PR12MB3827: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yZ+zUwIl9B6eIHO9A4IAB4gnO/KdbA1O0Jj+G476e7De/clpW4HSIUsdd8Hvf8mL2ngsIzkeT3BHfpHf3LbDM6ooi2T6GvsEoh/Q/4XXkoQb5DP0Uhbn65OLCV0ZqWH/TDz9uu64Sef3anHpPsU9iOQzVBhsiq6BeN+iDmnQa74J7eSqXglMiGVKX6jKnirAzeIZvI+2+RswP2nGvFVlJ5/WbSszPxs3tn0l6FmW/0UKq63ZJLQRyRRbLpGb7M40x/BAx+CGkrWiHpzl6WaLNe2b1CBkEHd4Y1nvPLa1VvM0/EY6WBfJpus6QiwJDin/AapOnZmjaZ0aa/k1DCzRV5ljIq2z35C4BoOd+ULknPpjPLBez2ksrlMWtiOuMKDAldf/fo7/A2HE7RH9AzL/ZryAnvMh2znkm8VmTgvL7p4FHr4xnFMK3oCGiXxW0KUSYMhevNZiUSBzWwZSwoo9TNGhvDqlTfEY3gsuo+M0NwMHribFh8v47EG5s21aEUs2OCq/sivu5cefhyCI2y8CPR66ObJaLduBfaZX85nZPMQx2kf9kcbIpjhByyVwWxG3WII1tbqJOBoQNAiIP7OY6xhiHheM4nPNYB3aUCwD1AVgD4EBOQ7UJ10kfyr3jDcRaQqbdZz9uCcN/LFgnztKWZwhudqkDbr/1ZHC4WHms3roBM9HyhRyyUP981fzNrJ5EctfDHQixBbLyqMqapjnunDzy4XgnRbAuwhfPcweAYE= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(376002)(39850400004)(396003)(366004)(346002)(66946007)(53546011)(16526019)(66476007)(66556008)(186003)(83380400001)(8676002)(31686004)(19627235002)(86362001)(4326008)(316002)(16576012)(110136005)(26005)(52116002)(8936002)(36756003)(2616005)(2906002)(478600001)(956004)(44832011)(31696002)(6486002)(38100700002)(6636002)(5660300002)(38350700002)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?QVBjUmMzQWlFblZpUVNZR21ZUFgwUDEwUldrdnZOdk1BNDJBc1plYkRBTDc5?= =?utf-8?B?b1d4VzVEcktWYUxBT1gwTXp5ZHFnMHpqeVdiWl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12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jun 2021 23:38:24.9957 (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: aV9CIB+tAjmobJ5RM9etW19/6L7/hwZ+8AViLySFyXAf/YLrXpsHmwMyoy18SadaqihD5UMrWJi0S8RwQMvVUQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3827 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 6/22/2021 6:20 PM, Ashish Kalra wrote: > Hello Tom, > > On Tue, Jun 22, 2021 at 05:47:48PM -0500, Tom Lendacky wrote: > ... >>> +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 = KVM_HC_MAP_GPA_RANGE; >>> + 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 = AsmReadCs() & 0x3; >>> + 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 ( >>> + KVM_HC_MAP_GPA_RANGE, >>> + PhysicalAddress, >>> + Pages, >>> + Mode >>> + ); >>> + } >>> +} >> >> You could just issue the VMMCALL and, for SEV-ES, let the VC handler take >> care of this. You would just have to add some smarts to the VC handler to >> compare the hypercall number and add the additional register values. You >> could probably get rid of a level of function calls that way. Thoughts? >> > > IIRC, we have already discussed this internally. > > Letting the VC handler do it was making it too complicated to add hooks > inside the VmgExitLib, and corresponding updation of MdePkg and UefiCpuPkg > (as described in the email thread below), and at that time > Brijesh had suggested the use of this alternative VmgExit() approach. > A lot has changed in the OVMF code since you last submitted the patch. IIRC, in ES wip patches Tom was implementing the VC handling library in the EDK2 core. But in the final version, all the VC handling is done in the OVMF. The EDK2 core provides a Null library that get override from the OVMF. In other words, you no longer need to touch the MdePkg and UefiCpuPkg etc for this change. I agree with Tom that it would be nice if we can add smarts in the VC handler. My previous comment was mainly focuses around how you can avoid touching the EDK2 core. > Email thread copied below : > > ... > Well, I does not mean that you should literally use VMMCALL instruction instead you use its corresponding VMGEXIT number. > Something like this: > > Status = VmgExit (Ghcb, SVM_EXIT_VMMCALL, 0, 0); > > This way, a #VC will not be kicked in and there is no need to hook anything inside the VmgExitLib. Maybe Tom can correct me if that is not acceptable. > > -Brijesh > >>> I am not able to follow your OVMF patches, could you provide a very high level overview of what exactly you are trying to achieve? Its possible that I am missing something fundamental but why do we care of Hypercall inside the bare metal pkg (e.g MdePkg, UefiCpuPkg)? Why we are needing a Hob etc ? IMO, a Hypercall implementation should be straight forward like what I did the SEV live migration. In case of ES, all you need to use the Ghcb instance to save the register values (rax, rbx, rcx etc) then invoke vmmcall instruction. >> > I need to do this hypercall validation and setup as part of VC# exception's VMMCALL handling, > i.e, in the VmgExitLib code. I need to use a HOB to store/cache hypercalls invoked during SEC > and PEI phase and flush them later at DXE IPL phase. As VmgExitLib code is invoked in SEC and > PEI phases and references MdePkg and UefiCpuPkg, i need to add HypercallLib references in UefiCpuPkg. > > Also, i need to verify if Hypercall library interfaces are being invoked during SEC and/or PEI phase, > currently i do it by checking PcdOvmfSecGhcbBase and for accessing that i had to add reference to > it in MdePkg/MdePkg.dec and UefiCpuPkg/UefiCpuPkg.dec, if i can check that the Hypercall library > interfaces are being invoked in SEC/PEI phase using some other mechanism then i can drop this > reference to PcdOvmfSecGhcbBase. > ... > > > Thanks, > Ashish >