From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-SN1-obe.outbound.protection.outlook.com (NAM04-SN1-obe.outbound.protection.outlook.com [40.107.70.89]) by mx.groups.io with SMTP id smtpd.web12.58137.1595863807184112307 for ; Mon, 27 Jul 2020 08:30:07 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=QbYNZEYS; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.70.89, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=A0wnWaXSU5vZmOjxJlNRRaE174ur18+xcAAPxLPqCocaHbF8qXG43W6uo5Vg31IGQy3JbKTTkd6azTJxjpkLho6NHpt/KOdoeEGHO8i80amkdkJCHCrN3KlyAc9nwqb4NF+f0k/g8R0p88AIBu7PqjmzszSdd38yxUPlbExb9xvHeqV1sWzhTCjQgEaaLcEfqhxznvGimptLgT+RrS8SxnJiIvIJqoFQYXomVMYl+wkL/tjhkw2c02k/TZE+4grHJSyysy6gI4IxcD2SEtQI2Vnmk3/YfVnHi/1AYAlwQiCF4NH7yxE01bksomXgguLcd1b0bDbtAGVgYpv57EEyRA== 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=eqxKEl1jkDkoQSy0tcmI5Mo0oyJK/DNNn+LhL12PTF0=; b=LpneVv7wYkbabHmrM/eab3cgDW29ogqqqMSFxxCSjWQ57iODi0MH2iSH3L+tmo0/5oFm6vPYb86d/6K3ne2VHYVQp+9p+VG2FMND+Eo92KFK2pGFHNIilNlZErM2FwTlsvGxORXLaCsePxr+mFt8VkJhsMKsI0tPJBco8YUpt8D/EPDizoIFZTgQN4FjKdkzJDi91XxTwIY1/23JliJI6rgDkvx9pz9756F0awmT0EcsR5nZioR4ohAG6jxeXJndk+Fp+mLDzZoYwahp5RkVJgU0xqJV6KvNyqrDh6AEk0+UMtJL9ZRcjT5k/nAt/h6tQ7+zP/8DBY+xrpqO5zBzJQ== 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=eqxKEl1jkDkoQSy0tcmI5Mo0oyJK/DNNn+LhL12PTF0=; b=QbYNZEYSxDxEYCnvpHr0HbeflLQbe83Lqypa8SGMO7XZ1I64XOEft2jHegs7RGnP5S5DhP5s88riKwnBOumzmhrcQ/R8jopyHvg4PLMVvTgXjLsx34+5AEK8nuNOywhxVduUdKSOb2T0+FEeZcIFFrjmovIohNURLgMIf/BY5W8= 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 DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM5PR12MB1163.namprd12.prod.outlook.com (2603:10b6:3:7a::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.25; Mon, 27 Jul 2020 15:30:05 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::25ec:e6ba:197c:4eb0]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::25ec:e6ba:197c:4eb0%8]) with mapi id 15.20.3216.031; Mon, 27 Jul 2020 15:30:05 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , Ard Biesheuvel , Eric Dong , Jordan Justen , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni Subject: [PATCH v12 26/46] OvmfPkg/VmgExitLib: Add support for DR7 Read/Write NAE events Date: Mon, 27 Jul 2020 10:26:07 -0500 Message-ID: <7779e6d2ed997244e6f6bd71b9fcbe1bfe0520b2.1595863587.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: X-ClientProxiedBy: SN4PR0401CA0014.namprd04.prod.outlook.com (2603:10b6:803:21::24) 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 tlendack-t1.amd.com (165.204.77.1) by SN4PR0401CA0014.namprd04.prod.outlook.com (2603:10b6:803:21::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.22 via Frontend Transport; Mon, 27 Jul 2020 15:30:04 +0000 X-Mailer: git-send-email 2.27.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 54957aca-fc35-4aaa-020f-08d83241f00e X-MS-TrafficTypeDiagnostic: DM5PR12MB1163: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AmBarF59dlIXoIc0WZLLJI9afMbqVLL8CS+qADshugrIxeJ+HiRuUaDG06mGLWZY1Uy0hPXKykyyZ5xVvE4BuDaMozyrXLSuXCa9z9rrJZ4yf14slVf6mmjo/S5I6oDfQqPlKlQKapUdN8/gqXhPgoN2Ts8GJq+6FHsfh2iON8DzhgAfX135Q9fDPDLtO7TgapzA50krdqNrHHpOxz8mxFhmxuJrFlwCJnuP41Le3evNFhKetFHfOgttqqSC93V/fElJEjPujLruGIxXAFaFnHDHf7X9zImKHaNIaAcV8EYy0JHJCc1K9oLxhfEppgu/b84Mr7orQdy5itAnhkAuh6UEZi7OeSZJ7tTfVL8AkVN9Ucd0rZKvxVr8oFYn95UHfHb4gbmDTEkd8e16H1bjEUYa8ms8PPAT74AOdMbspCuCOgmTliPJk+6I5XUjBUQUliSJY0jNBjt72fQdvY+sEQ== 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;SFTY:;SFS:(4636009)(376002)(39860400002)(346002)(396003)(136003)(366004)(26005)(7696005)(16526019)(52116002)(186003)(66946007)(2616005)(66556008)(316002)(36756003)(956004)(966005)(5660300002)(66476007)(54906003)(478600001)(2906002)(4326008)(6486002)(8676002)(8936002)(6916009)(83380400001)(86362001)(136400200001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: djfioq6Xy0H/LYqIhhY8D1NBUs1qL+i2gMkeLfNU5O6erHqzh1CqIFObmyg2OCgDboheaS9kIyYMKS11M2JhxaBpVgwwemi0c5SOmG3i8CoeQ9L5T/r3E1R1KtPLQT3Gjnfo6Uim7ILGEsRGYxjBBePtHw3ADTNxXAxFk34ij/jwZw+OiEtyQxjvtsy9Jz6uKQGl4JHqFpkQhhw8vvDz+HotVaVVr9AXclKidcY+Jhd2i4Yw4/T4wJkKFOdv3O2Fzub03VQ1R75ajB+6aep63Ok/mSos3XlbpfQcBPI0Wt5nI6p78zo563z/wxDpg08CoGje/0lQ3qEbmKk2DOtR43ePBSm3V8IqJ97sq862A9DO4p4N1UucKA0SSIJHRh8du82S7cHR+Nz/jmRg25s+opsZN1Ld5/7vSsME/hMd5BIC6D4S0/NbTR2ZMHab53jAECeJsFhuKabNoqq9gKo0VPTGgJKNFH9cm8K91qHJkQY= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 54957aca-fc35-4aaa-020f-08d83241f00e X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2020 15:30:05.6647 (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: U7iB2Dr+8go+t1UbAe1Um8455KV3jCWj28wKK1rpTnSDlQIhHWo7mXKWSv/9FFdY5PzGWMHP9QSjfHj/78mOwA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1163 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2198 Under SEV-ES, a DR7 read or write intercept generates a #VC exception. The #VC handler must provide special support to the guest for this. On a DR7 write, the #VC handler must cache the value and issue a VMGEXIT to notify the hypervisor of the write. However, the #VC handler must not actually set the value of the DR7 register. On a DR7 read, the #VC handler must return the cached value of the DR7 register to the guest. VMGEXIT is not invoked for a DR7 register read. The caching of the DR7 values will make use of the per-CPU data pages that are allocated along with the GHCB pages. The per-CPU page for a vCPU is the page that immediately follows the vCPU's GHCB page. Since each GHCB page is unique for a vCPU, the page that follows becomes unique for that vCPU. The SEC phase will reserves an area of memory for a single GHCB and per-CPU page for use by the BSP. After transitioning to the PEI phase, new GHCB and per-CPU pages are allocated for the BSP and all APs. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Acked-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.c index e70e0ef82f68..c57c8c4ba203 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c @@ -126,6 +126,14 @@ UINT64 SEV_ES_INSTRUCTION_DATA *InstructionData ); =20 +// +// Per-CPU data mapping structure +// +typedef struct { + BOOLEAN Dr7Cached; + UINT64 Dr7; +} SEV_ES_PER_CPU_DATA; + =20 /** Checks the GHCB to determine if the specified register has been marked v= alid. @@ -1480,6 +1488,104 @@ RdtscExit ( return 0; } =20 +/** + Handle a DR7 register write event. + + Use the VMGEXIT instruction to handle a DR7 write event. + + @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communi= cation + Block + @param[in, out] Regs x64 processor context + @param[in] InstructionData Instruction parsing context + + @retval 0 Event handled successfully + @return New exception value to propagate + +**/ +STATIC +UINT64 +Dr7WriteExit ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT_X64 *Regs, + IN SEV_ES_INSTRUCTION_DATA *InstructionData + ) +{ + SEV_ES_INSTRUCTION_OPCODE_EXT *Ext; + SEV_ES_PER_CPU_DATA *SevEsData; + UINT64 *Register; + UINT64 Status; + + Ext =3D &InstructionData->Ext; + SevEsData =3D (SEV_ES_PER_CPU_DATA *) (Ghcb + 1); + + DecodeModRm (Regs, InstructionData); + + // + // MOV DRn always treats MOD =3D=3D 3 no matter how encoded + // + Register =3D GetRegisterPointer (Regs, Ext->ModRm.Rm); + + // + // Using a value of 0 for ExitInfo1 means RAX holds the value + // + Ghcb->SaveArea.Rax =3D *Register; + GhcbSetRegValid (Ghcb, GhcbRax); + + Status =3D VmgExit (Ghcb, SVM_EXIT_DR7_WRITE, 0, 0); + if (Status !=3D 0) { + return Status; + } + + SevEsData->Dr7 =3D *Register; + SevEsData->Dr7Cached =3D TRUE; + + return 0; +} + +/** + Handle a DR7 register read event. + + Use the VMGEXIT instruction to handle a DR7 read event. + + @param[in, out] Ghcb Pointer to the Guest-Hypervisor Communi= cation + Block + @param[in, out] Regs x64 processor context + @param[in] InstructionData Instruction parsing context + + @retval 0 Event handled successfully + +**/ +STATIC +UINT64 +Dr7ReadExit ( + IN OUT GHCB *Ghcb, + IN OUT EFI_SYSTEM_CONTEXT_X64 *Regs, + IN SEV_ES_INSTRUCTION_DATA *InstructionData + ) +{ + SEV_ES_INSTRUCTION_OPCODE_EXT *Ext; + SEV_ES_PER_CPU_DATA *SevEsData; + UINT64 *Register; + + Ext =3D &InstructionData->Ext; + SevEsData =3D (SEV_ES_PER_CPU_DATA *) (Ghcb + 1); + + DecodeModRm (Regs, InstructionData); + + // + // MOV DRn always treats MOD =3D=3D 3 no matter how encoded + // + Register =3D GetRegisterPointer (Regs, Ext->ModRm.Rm); + + // + // If there is a cached valued for DR7, return that. Otherwise return th= e + // DR7 standard reset value of 0x400 (no debug breakpoints set). + // + *Register =3D (SevEsData->Dr7Cached) ? SevEsData->Dr7 : 0x400; + + return 0; +} + /** Handle a #VC exception. =20 @@ -1524,6 +1630,14 @@ VmgExitHandleVc ( =20 ExitCode =3D Regs->ExceptionData; switch (ExitCode) { + case SVM_EXIT_DR7_READ: + NaeExit =3D Dr7ReadExit; + break; + + case SVM_EXIT_DR7_WRITE: + NaeExit =3D Dr7WriteExit; + break; + case SVM_EXIT_RDTSC: NaeExit =3D RdtscExit; break; --=20 2.27.0