From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (NAM04-DM6-obe.outbound.protection.outlook.com [40.107.102.44]) by mx.groups.io with SMTP id smtpd.web10.10201.1619716357662335255 for ; Thu, 29 Apr 2021 10:12:37 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amd.com header.s=selector1 header.b=YT28EQJz; 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.102.44, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Mu5dcIxQCojRQM6C6udLKeXSNLVR7gN5GX03LCTN7pReHBLFpiDjWPZ/ZR9pBZr7PyhYqNxbKOm3ELu7vvGf6MUQq7zOFtXJ+26SfpT33okQNaAfSHZIQRt8Mjr+tDb9OedDRGc/iOu9akH7bamw1ckCncDZ2QKgOci7LMLdBvTshUuVh2qao+d1ZrEYFox1V+5dcKWQ6QmB89exW/DZePp75keQ0nQL5zrTuVm8CXr4smrBgXwMcoGN++55uVdoY3PR0uNn3G62NFC+WXUbYMQX2tG+1ngCA9dVnV04I7fr4C3C1OmaBSuJEZQvM/2rU5VT84MeZqpKeFotDp9zqQ== 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=XWcpYLomQE9UgdSmnLbBdNso2tZYsHmhPG3l4ez3CRo=; b=WJnbQ92TDSMgOlnhM0aLFG+f6zXau9kwcvRDAWW6xj1USblY9/QJ8A4XTLrNQaJSoPU6wyVF4NlZpoppaWGrg3XXGPJGLUIXc0q17RUWh1OoD53Z5rF+ghxfi1a+haiadysmhoMgARkOnxToAKmsaUgHmMIOE6QVSKzZ37h4VOB3xivU5c+TI8rYjUMqC2voFr4wgd9MItPhSka0zyHItcH21ieeYyOztMw0rbYv0GOH3oDT67Xeqwic0TBsOiDsT01ndm9ZeUAogfpkQ21B08XIa5WiPP6eTTCE9DyudJkApQD35TBjzk1hb8IlvPscYfV/lE/dGJJj1Uwd7qY0lw== 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=XWcpYLomQE9UgdSmnLbBdNso2tZYsHmhPG3l4ez3CRo=; b=YT28EQJz/P3v4xr3fmDecHH9rRTjw1IbQzQyyxmbMjw7PJbzo1VJHtuEzsacr4jfhv3SUOj1DJIdU49bcm0nEajFnZmcR2BNy/gJWRH7Ky8RUJlZSB1I60R8jWSYFXmt/DpZWXtVANc+fwyP7yibjeJrk4RRZIlLsVRAJxvTiQA= 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 DM6PR12MB4218.namprd12.prod.outlook.com (2603:10b6:5:21b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21; Thu, 29 Apr 2021 17:12:36 +0000 Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::b914:4704:ad6f:aba9]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::b914:4704:ad6f:aba9%12]) with mapi id 15.20.4065.026; Thu, 29 Apr 2021 17:12:36 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Joerg Roedel , Borislav Petkov , Laszlo Ersek , Ard Biesheuvel , Jordan Justen , Brijesh Singh , Erdem Aktas , James Bottomley , Jiewen Yao , Min Xu Subject: [PATCH v3 1/5] OvfmPkg/VmgExitLib: Properly decode MMIO MOVZX and MOVSX opcodes Date: Thu, 29 Apr 2021 12:12:10 -0500 Message-ID: <5949d54cb2c9ab69256f67ed5654b32654c0501c.1619716333.git.thomas.lendacky@amd.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN7PR04CA0060.namprd04.prod.outlook.com (2603:10b6:806:120::35) 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 SN7PR04CA0060.namprd04.prod.outlook.com (2603:10b6:806:120::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4087.27 via Frontend Transport; Thu, 29 Apr 2021 17:12:35 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 31a397b3-bdad-40bf-f7d3-08d90b31fc13 X-MS-TrafficTypeDiagnostic: DM6PR12MB4218: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PgQLljd1nKJoInqv2j6YAi/Ab/99WgFw7b+0P828vNtKZGIba22+Yje7hLEPiMLXosTPU0MYOPs6KW+RG8X6hwyFs59pLBTNRCh46tik/u70Go8X9j1wDqLfF9S3a5Qe8Tv5mfF2cZ5zsQZHt70vimLUp5Jp4YdzNSwwKKLnFE/bLMBYfLHxjRa2TIEi4J1L3U0Xcpliv9OUun0iBovxMBeKaLEQbQP0Yv+x0deJU6CAriIQ865yreWB4AcDZNa4U7tbtOJqw9KPjBV1eTHAtb4TyNPd/S5itdOkFWk8PRz9suB+GBkt+weXJoyVZvO95feLG85dnGH4HyV2mFcZC7r+QsDOzVt4uZlDr8Hxpez8Mpcuzy+o++if2fgaT8Gm+hy5e+kaRKFI8slYqPw/wrOh0n2C8YdTJFp9MMoeAmlI9h0RrChCbbZkyGXwjK0IZkBOB4t5RzBvOiJKN9IVieUS7LLWD0WKWT36hUBGyfkr74+2gZMdKtqy96yRYpKyFMewWu8QDYEQsTwWeCTHeFqSc2btXdmicL95pFHw4b3rr3amieSj+mc1o87jtiaxR8b2/vwoSfSSsTja1J6pOnbbNaBw7F/KE73ck+PZgFy5B57EUKTT6VQIkMrq161vUMlLG5cYq5I+RCDurEbIe66Vd5BlcrMfVWQx3i8DBYrheFeQgfmMb22b/sTfS5ez3hW8NWFbj2ApozNa88Vych0Z9Yo3LKWQdv1GeUHFKTVBEn+01seDXncHzHQvdg2z 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)(396003)(136003)(376002)(366004)(346002)(39860400002)(5660300002)(52116002)(66556008)(478600001)(66476007)(16526019)(54906003)(66946007)(83380400001)(956004)(2906002)(8676002)(8936002)(186003)(6916009)(2616005)(38100700002)(6666004)(966005)(26005)(86362001)(7416002)(7696005)(4326008)(38350700002)(6486002)(36756003)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?vKa9KeWLIALum+/3majfCFERMetn7+2Gzxs1KJ+iwyDe6o6kkHUMofRdUYK9?= =?us-ascii?Q?K0JOB4va+OFfsgxtEtBVo9GZkXs+LZawWSUw6pFv99xeoQEk160erOzxRH/L?= =?us-ascii?Q?d+LCDsD0qhetVn0JGHnQnPR6pngPJgkyJ7ZMKEDFVBn8CZVhS9vWXWV7fdM2?= =?us-ascii?Q?TANHddoO3bbynGn/zZCjdPTkUonHBFIIFbH8DoixkazwU9zW9ssiFldVlhSS?= =?us-ascii?Q?vEoWbPV/3G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5E/ZcyJkzP4zqoGA9DKZ5?= =?us-ascii?Q?IVJqPc2EUcX3Dx+3z2vPe8f9sEFlbhSK32ODPNULVLe/HuKnUNMySAl7Fg+g?= =?us-ascii?Q?geAnlO4nl7PM0phBWs8vDOVUURLQWA30x4TBlogmbHefSASerR6JrbaQVoXp?= =?us-ascii?Q?EDum9o6qE/ioq2/hnSabVFg4pBOda+SfQHrKGUlKHV96AQwzSIqzwg3LV4Kc?= =?us-ascii?Q?2zBv0z8EwmvRV0o757tt/WKb?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 31a397b3-bdad-40bf-f7d3-08d90b31fc13 X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Apr 2021 17:12:36.1735 (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: LJSz4e3LTQE1zWrBFN2S069x/RIgXtLxmcca1xQggZTWG3xk1+DHSrbKuSuF3/ysAIZpgdA8pnrQ6ZaEWHLQXQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB4218 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3345 The MOVZX and MOVSX instructions use the ModRM byte in the instruction, but the instruction decoding support was not decoding it. This resulted in invalid decoding and failing of the MMIO operation. Also, when performing the zero-extend or sign-extend operation, the memory operation should be using the size, and not the size enumeration value. Add the ModRM byte decoding for the MOVZX and MOVSX opcodes and use the true data size to perform the extend operations. Additionally, add a DEBUG statement identifying the MMIO address being flagged as encrypted during the MMIO address validation. Fixes: c45f678a1ea2080344e125dc55b14e4b9f98483d Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Jordan Justen Cc: Brijesh Singh Cc: Erdem Aktas Cc: James Bottomley Cc: Jiewen Yao Cc: Min Xu Acked-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Librar= y/VmgExitLib/VmgExitVcHandler.c index 24259060fd65..b716541ad170 100644 --- a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c +++ b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c @@ -643,6 +643,9 @@ ValidateMmioMemory ( // // Any state other than unencrypted is an error, issue a #GP. // + DEBUG ((DEBUG_ERROR, + "MMIO using encrypted memory: %lx\n", + (UINT64) MemoryAddress)); GpEvent.Uint64 =3D 0; GpEvent.Elements.Vector =3D GP_EXCEPTION; GpEvent.Elements.Type =3D GHCB_EVENT_INJECTION_TYPE_EXCEPTION; @@ -817,6 +820,7 @@ MmioExit ( // fall through // case 0xB7: + DecodeModRm (Regs, InstructionData); Bytes =3D (Bytes !=3D 0) ? Bytes : 2; =20 Status =3D ValidateMmioMemory (Ghcb, InstructionData->Ext.RmData, Byte= s); @@ -835,7 +839,7 @@ MmioExit ( } =20 Register =3D GetRegisterPointer (Regs, InstructionData->Ext.ModRm.Reg)= ; - SetMem (Register, InstructionData->DataSize, 0); + SetMem (Register, (UINTN) (1 << InstructionData->DataSize), 0); CopyMem (Register, Ghcb->SharedBuffer, Bytes); break; =20 @@ -848,6 +852,7 @@ MmioExit ( // fall through // case 0xBF: + DecodeModRm (Regs, InstructionData); Bytes =3D (Bytes !=3D 0) ? Bytes : 2; =20 Status =3D ValidateMmioMemory (Ghcb, InstructionData->Ext.RmData, Byte= s); @@ -878,7 +883,7 @@ MmioExit ( } =20 Register =3D GetRegisterPointer (Regs, InstructionData->Ext.ModRm.Reg)= ; - SetMem (Register, InstructionData->DataSize, SignByte); + SetMem (Register, (UINTN) (1 << InstructionData->DataSize), SignByte); CopyMem (Register, Ghcb->SharedBuffer, Bytes); break; =20 --=20 2.31.0