From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (NAM10-BN7-obe.outbound.protection.outlook.com [40.107.92.70]) by mx.groups.io with SMTP id smtpd.web09.305.1624404028115122660 for ; Tue, 22 Jun 2021 16:20:28 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@amd.com header.s=selector1 header.b=e/P9tfOd; 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.92.70, mailfrom: ashish.kalra@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nrvW/+rKqvHlbeT1dBbJMm3SLEqTjSxoJesnX7g6B5rgLj3GCawXSV8ssvowd7QwDeoB7sOO3wjqR4MmEx3sFlf7MpIuHdGYo8AzBke49hMPDbNW+FlNOPMh5XkNvmf7KhfncW5nYyxXunHVHcbyMG0aRppa8NoLMuwEOaEyQOg3PttL2pNlMAJIGPZ59if08xkB24RhZBF99ZwzCRuuhdCpvTHtSSKhOCrbDhjymA/as6qS4JrxeroZYHGHvfxkpOPvm3uKqCVktHqYB9KOpk+J5mV3P+BGyB3zeW4MKANrB5hNgAG202A5m7DZyqKx9pH92qWz6PyB+lMPRRx/QA== 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=dKCY16PDVsOXP8cW5X7grrsBm64Y9oGKTPnqCLsLXEE=; b=eMW3SsMYQML+RXHSDolpaO0Do2IxS73vG316J+0oD9TMVCeVbWrMfqmjvxuJwfzXMg3oUPSYmlt9whFH6ZQmqmdPRencuSiMjKsQnAafrDQzVflwF4Gkoz7TZojVpGd6c2cv6OpuBsMV+fJ4k0vmmilkMqOjzu8deH9JTKy2YC1RIjJcdHGmBnTMIRgMh0zj7ES6gsgwB9HNqly5IjldsTxwd2H9oXVkQGq+a3plQoqQu8N2kygfuR5oWHKINUZKXz/BBhe7bhyPlUePl8EiQSzbV9DmI0bmA7aryOC063QUP9RpmzuCJ+ElLoKuJgybmU/QmJw/5AMBDBSlkd13Ew== 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=dKCY16PDVsOXP8cW5X7grrsBm64Y9oGKTPnqCLsLXEE=; b=e/P9tfOdeoW3bECrbES4h2Rdvm7uyNNVgeLG3nZ3fG+aMsfCXomhpYNTFw49on1LqPY+ESxqk1n6ba1qTdXGfwN7k7ZQXMCglQfNMKtCn0fFXrGzOpthhnuh+rjR0eWEQGQF6pPXUnxQmSXAiLSUVEFG+rBpCqMtAd7fHXAuXMY= Authentication-Results: amd.com; dkim=none (message not signed) header.d=none;amd.com; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SN1PR12MB2448.namprd12.prod.outlook.com (2603:10b6:802:28::23) 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:20:26 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::958d:2e44:518c:744c]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::958d:2e44:518c:744c%7]) with mapi id 15.20.4242.023; Tue, 22 Jun 2021 23:20:26 +0000 Date: Tue, 22 Jun 2021 23:20:20 +0000 From: "Ashish Kalra" To: Tom Lendacky Cc: devel@edk2.groups.io, 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 Subject: Re: [PATCH v4 1/4] OvmfPkg/MemEncryptHypercallLib: add library to support SEV hypercalls. Message-ID: <20210622232020.GA6843@ashkalra_ubuntu_server> References: <7d0a30a022a7d3d3e056af8f79b87ed9991d2f52.1624281247.git.ashish.kalra@amd.com> In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0095.namprd11.prod.outlook.com (2603:10b6:806:d1::10) 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 (165.204.77.1) by SA0PR11CA0095.namprd11.prod.outlook.com (2603:10b6:806:d1::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4264.18 via Frontend Transport; Tue, 22 Jun 2021 23:20:25 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d0f6fe39-40bf-4361-0ae5-08d935d4510f X-MS-TrafficTypeDiagnostic: SN1PR12MB2448: 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: rRyL83aJVuGM4ujKSh4fhF+sxA55b5m9mZMjdP7g+Hu7R8JrHcIxWB+BzgZxw/OM81TWc/QfoBKvGplP3X5J6wKwsH2d9WdjKUAmluVjZDHxlceJG6srClVQWeSvZqMgFDWmfI+0GC/4LDZ9O5wzmrTlISSPVno88ASCMVPDUl39TEBwxZ+WuXr4W4Hi14tZOHNJoM/9qbUxG1a6Ap/RNpVqBBxbC9dfh6KvexJteTctWsObT0vi/4KVPPbgHsDOfBngTylcY2ZUtp0aFg7sYLEnmZusX5cn29ADzGVRuth/awqKdSKssMRYPH83nG9szC9kB1wHaUqrFEZ7SI0Zj8tFWcxx3nk18a3b3LOfIDXXWRCy1hLgaG5ZOVwXGwrpS/sU2WA0rb8Q3ZL8K3/uLaDQsVRdq8pm5OLQsFAtV/XjKpPODPV6WAuVi6BQcsJhQSs7elXNgPyN6y9NEDbhctAh+qU26KXMxBA3TujN4j0Iq+LIyMiyQmjBbkyHdEytUVjMx/rCofq9DmCgn9K2UesvCQ5Tg8kWEJYy8DZP6E/67rlPVAn1H9w0by6phXtvSrPFdzLQnuivWY6g+N6aJz/BwQMCpYr6WjW9x9uUSa8gk3mcWyH5bU62s9iKyVIEnLLIlwDUNM0CvaPlebhZKg== 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)(376002)(366004)(136003)(39850400004)(346002)(396003)(66476007)(66946007)(956004)(66556008)(44832011)(2906002)(8936002)(8676002)(316002)(6666004)(5660300002)(86362001)(19627235002)(186003)(33716001)(16526019)(9686003)(55016002)(26005)(6496006)(83380400001)(1076003)(52116002)(38350700002)(38100700002)(33656002)(4326008)(6636002)(478600001)(6862004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?0P+5iOUnRB4fHIZU3RPHJihNelNNGZOvNPD7p77UHxQnfAZBmPCgnHREPvhL?= =?us-ascii?Q?r3DFDLZz7K/nqiZo/tEnXbouetuy9MiXxbawERHlkCDiuMTBJAx9EzqrTHGE?= =?us-ascii?Q?ngZGmUvyu9exQ27mTbmYhuApkUIljXGD0d8fO4q1k7JBZhGT1ucS4FSH/Mtc?= =?us-ascii?Q?Pn6qEvkoZZxdqwTgrpKiJt2PBPBVWLNJ5KoFNA1SPnJRos9No4RMl83q22OR?= =?us-ascii?Q?56E4mcckiE/Dqkw1U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sXZzEELo+/?= =?us-ascii?Q?ubjAPqemr2KTM+wf+VZa9AIN1eg25j2diXqCZtZDx7zr0p5lQyw2TgyWmtDe?= =?us-ascii?Q?CS4n8M3cfp+PvpGVDMof5Qe0M8xBKMs8Ivt2tX9ZvHUBFieh94DA0riLNCqR?= =?us-ascii?Q?ANn9cdOjPnr5ThFcddTFoSObDHA4kWqNNN1ek/nfFxmJypSO8VucudEHmLA6?= =?us-ascii?Q?WgwMR4Xbsz0aYJZc+a28Mz2v?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d0f6fe39-40bf-4361-0ae5-08d935d4510f X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jun 2021 23:20:26.0494 (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: 59GfsYf2nmszL5gQQsJSgCndQE8LdO5WT8RsMTIylPZJUESTFyWAJ5D56YMlgnn7ufU7ksDL3yNcxrp2c5NPfA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2448 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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. 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