From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=PyES9soS; spf=none, err=SPF record not found (domain: amd.com, ip: 40.107.80.58, mailfrom: thomas.lendacky@amd.com) Received: from NAM03-DM3-obe.outbound.protection.outlook.com (NAM03-DM3-obe.outbound.protection.outlook.com [40.107.80.58]) by groups.io with SMTP; Thu, 19 Sep 2019 12:52:48 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B276WVOq/h+dymrPBzf8brotqpCdcdRQzeoyIm+15LGPgODc/HPHf+P9mc6KMs/0mal7qN88qEEm7Ul10bvDJWMell+ngNKag00BgTff2fZMkUNobZjQMcqDFeU03Xkuk8p8kcpsKWuuLTjSs0RhjdRdTLXSS/5NCPQgRCU6uRUQCkMwx3VM+O7CVC+MRRCNNpyJeoU2r7B9M+vvg14A7d3BcXOwYmHmz8CVQQ/o630MwEolpG9jrchVNOVogxix40YQCBIg+e1cnWOS10tLWTZ2M/ngwWlgtOa3dGCBOtjR5iWwutcHxNJoEN7vwlryWIzLlmWn9K/hByD3YpJiVg== 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=1BiXoz/mKHdBOV+PwpjoGiso67enKYubp2tWAHLPNM4=; b=DeDT4k1/a4DrPF4Tpg+V+66HMLK8qqyizIJlaLI1ytyxRlJE7Kfer7pjijpcuf1QnrvFyYx8IpZfVUfj2+vVrv4sHw46Cbogy5l5x6zBglVz7nxZISGQmCAImiGgtG6zlZV/uKFHuz0XNVBCgBl30c4SE2XJ+0XfSoL+hREpyYdQDHNnzd6wiXGfX+eKbkLNLyR4HymNVZzz/rWf3JadQbE2+mZvHlaY8KPw3/jGTXz/Wh/zbI+qAdqyy4ekJpmmu5LgBBXSuHyu4FanGlWsCicrQcbhopL+QmLwwCuI4obwMOo2tHlcKniiSPDpUNajtejBOKeYDkogpUFo3kKsaw== 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=1BiXoz/mKHdBOV+PwpjoGiso67enKYubp2tWAHLPNM4=; b=PyES9soSONNRW/bcEO5OefmHvA/ne1Wegq0WiepYiLrw1m7l/Y+Hogu8QjJs70KXjTIHmgCEljG6FTZKmQIaLoQiKC35H15p1PPtPIVSirvh3QtO2jHERXeV7sRqnImGh7YqzapicTPN4ZW3tlkCCoqUGTiHLJRpzPzWgtxOzv0= Received: from DM6PR12MB3163.namprd12.prod.outlook.com (20.179.104.150) by DM6PR12MB2890.namprd12.prod.outlook.com (20.179.71.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2263.23; Thu, 19 Sep 2019 19:52:47 +0000 Received: from DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::400e:f0c3:7ca:2fcc]) by DM6PR12MB3163.namprd12.prod.outlook.com ([fe80::400e:f0c3:7ca:2fcc%6]) with mapi id 15.20.2284.009; Thu, 19 Sep 2019 19:52:47 +0000 From: "Lendacky, Thomas" To: "devel@edk2.groups.io" CC: Jordan Justen , Laszlo Ersek , Ard Biesheuvel , Michael D Kinney , Liming Gao , Eric Dong , Ray Ni , "Singh, Brijesh" Subject: [RFC PATCH v2 20/44] UefiCpuPkg/CpuExceptionHandler: Support string IO for IOIO_PROT NAE events Thread-Topic: [RFC PATCH v2 20/44] UefiCpuPkg/CpuExceptionHandler: Support string IO for IOIO_PROT NAE events Thread-Index: AQHVbyPPqAY2l7HhN0OoT/MjuqYE9w== Date: Thu, 19 Sep 2019 19:52:47 +0000 Message-ID: <95667186978d220e5703c10d98d28cda35e6701f.1568922729.git.thomas.lendacky@amd.com> References: In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.17.1 x-clientproxiedby: SN4PR0501CA0146.namprd05.prod.outlook.com (2603:10b6:803:2c::24) To DM6PR12MB3163.namprd12.prod.outlook.com (2603:10b6:5:182::22) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [165.204.78.1] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 31cb9e42-4c89-4e80-b05d-08d73d3af1cb x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600167)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:DM6PR12MB2890; x-ms-traffictypediagnostic: DM6PR12MB2890: x-ms-exchange-purlcount: 1 x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:7691; x-forefront-prvs: 016572D96D x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(4636009)(346002)(376002)(366004)(396003)(136003)(39860400002)(189003)(199004)(2501003)(99286004)(6486002)(86362001)(476003)(52116002)(11346002)(446003)(54906003)(2616005)(8936002)(19627235002)(3846002)(6436002)(64756008)(5660300002)(66556008)(66476007)(8676002)(66446008)(76176011)(66946007)(256004)(2351001)(486006)(6512007)(5640700003)(6116002)(50226002)(6916009)(6306002)(118296001)(71200400001)(71190400001)(2906002)(386003)(6506007)(966005)(14454004)(478600001)(4326008)(305945005)(66066001)(36756003)(26005)(102836004)(316002)(7736002)(81166006)(186003)(1730700003)(25786009)(81156014);DIR:OUT;SFP:1101;SCL:1;SRVR:DM6PR12MB2890;H:DM6PR12MB3163.namprd12.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; received-spf: None (protection.outlook.com: amd.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: cNCe77zdbqryESMFBHehDdchNhDw/3ofq5QN1OV3YmGO/OQK4wngHQXPPBAaTTV09nw6RlKW7oK33eS3EGWVxxEPV95WRz5TWdFdMrvMVIRgT2UmhwHkLVmmezFB0i7fBY4n6xWtkEsBsypra/4lkmvOl3+jlkgbGDhVfXp+eeg20zPI95X9XR4NwAiKMD00W8zkannSRDUjJPBvMo6vvJZFCa2otnPp/0bAZos9hJ4RXpPsRXDWv73+iJE7YXnvAXIrTOwp1iYHzQsf9ZIjVg4YX0m78fK4AfHtc1s1YSXIMvUREgeWVUynLrfqm+0haRRSXtuRWsgpBacPSspImbtznUcUY7/DOqqM6r+P5YLyHpUstNtnBc9QfYVSRaYmweN2P6X28/r0YYOSLV5/e8nPUkuP3Lzp8FmdqpZ+0Zo= MIME-Version: 1.0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 31cb9e42-4c89-4e80-b05d-08d73d3af1cb X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Sep 2019 19:52:47.3218 (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: UnWykx13Zgn1aTaKcw1m7scDdUEaNuxq8+JOwVeEOp99vOYYL3ssz4xAwSsMl0+CBaGtMGCVDRZVFqwcRwihrg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB2890 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-ID: <22301F714477CF4AAB090D677E330D8B@namprd12.prod.outlook.com> Content-Transfer-Encoding: quoted-printable From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D2198 Add support to the #VC exception handler to handle string IO. This requires expanding the IO instruction parsing to recognize string based IO instructions as well as preparing an un-encrypted buffer to be used to transfer (either to or from the guest) the string contents for the IO operation. The SW_EXITINFO2 and SW_SCRATCH fields of the GHCB are set appropriately for the operation. Multiple VMGEXIT invocations may be needed to complete the string IO operation. Cc: Eric Dong Cc: Ray Ni Cc: Laszlo Ersek Signed-off-by: Tom Lendacky --- .../X64/AMDSevVcCommon.c | 82 ++++++++++++++++--- 1 file changed, 70 insertions(+), 12 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c index 16091fe38f83..ce5514fd0fb6 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c @@ -305,6 +305,22 @@ IoioExitInfo ( UINT64 ExitInfo =3D 0; =20 switch (*(InstructionData->OpCodes)) { + // INS opcodes + case 0x6C: + case 0x6D: + ExitInfo |=3D IOIO_TYPE_INS; + ExitInfo |=3D IOIO_SEG_ES; + ExitInfo |=3D ((Regs->Rdx & 0xffff) << 16); + break; + + // OUTS opcodes + case 0x6E: + case 0x6F: + ExitInfo |=3D IOIO_TYPE_OUTS; + ExitInfo |=3D IOIO_SEG_DS; + ExitInfo |=3D ((Regs->Rdx & 0xffff) << 16); + break; + // IN immediate opcodes case 0xE4: case 0xE5: @@ -342,6 +358,8 @@ IoioExitInfo ( } =20 switch (*(InstructionData->OpCodes)) { + case 0x6C: + case 0x6E: case 0xE4: case 0xE6: case 0xEC: @@ -388,8 +406,9 @@ IoioExit ( SEV_ES_INSTRUCTION_DATA *InstructionData ) { - UINT64 ExitInfo1; - UINTN Status; + UINT64 ExitInfo1, ExitInfo2; + UINTN Status; + BOOLEAN String; =20 ExitInfo1 =3D IoioExitInfo (Regs, InstructionData); if (!ExitInfo1) { @@ -407,17 +426,56 @@ IoioExit ( Ghcb->SaveArea.Rax =3D Regs->Rax; GhcbSetRegValid (Ghcb, GhcbRax); =20 - Status =3D VmgExit (Ghcb, SvmExitIoioProt, ExitInfo1, 0); - if (Status) { - return Status; - } - - if (ExitInfo1 & IOIO_TYPE_IN) { - if (!GhcbIsRegValid (Ghcb, GhcbRax)) { - VmgExit (Ghcb, SvmExitUnsupported, SvmExitIoioProt, 0); - ASSERT (0); + String =3D (ExitInfo1 & IOIO_TYPE_STR) ? TRUE : FALSE; + if (String) { + UINTN IoBytes, VmgExitBytes; + UINTN GhcbCount, OpCount; + + Status =3D 0; + + IoBytes =3D (ExitInfo1 >> 4) & 0x7; + GhcbCount =3D sizeof (Ghcb->SharedBuffer) / IoBytes; + + OpCount =3D (ExitInfo1 & IOIO_REP) ? Regs->Rcx : 1; + while (OpCount) { + ExitInfo2 =3D MIN (OpCount, GhcbCount); + VmgExitBytes =3D ExitInfo2 * IoBytes; + + if (!(ExitInfo1 & IOIO_TYPE_IN)) { + CopyMem (Ghcb->SharedBuffer, (VOID *) Regs->Rsi, VmgExitBytes); + Regs->Rsi +=3D VmgExitBytes; + } + + Ghcb->SaveArea.SwScratch =3D (UINT64) Ghcb->SharedBuffer; + Status =3D VmgExit (Ghcb, SvmExitIoioProt, ExitInfo1, ExitInfo2); + if (Status) { + return Status; + } + + if (ExitInfo1 & IOIO_TYPE_IN) { + CopyMem ((VOID *) Regs->Rdi, Ghcb->SharedBuffer, VmgExitBytes); + Regs->Rdi +=3D VmgExitBytes; + } + + if (ExitInfo1 & IOIO_REP) { + Regs->Rcx -=3D ExitInfo2; + } + + OpCount -=3D ExitInfo2; + } + } else { + Status =3D VmgExit (Ghcb, SvmExitIoioProt, ExitInfo1, 0); + if (Status) { + return Status; + } + + if (ExitInfo1 & IOIO_TYPE_IN) { + if (!GhcbIsRegValid (Ghcb, GhcbRax)) { + VmgExit (Ghcb, SvmExitUnsupported, SvmExitIoioProt, 0); + ASSERT (0); + } + Regs->Rax =3D Ghcb->SaveArea.Rax; } - Regs->Rax =3D Ghcb->SaveArea.Rax; } =20 return 0; --=20 2.17.1