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=LWS3Cy/F; spf=none, err=SPF record not found (domain: amd.com, ip: , mailfrom: thomas.lendacky@amd.com) Received: from NAM01-BN3-obe.outbound.protection.outlook.com (NAM01-BN3-obe.outbound.protection.outlook.com []) by groups.io with SMTP; Mon, 19 Aug 2019 14:36:03 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NcoQyiBd78bGSz76/3dI2uA9rPFmvhXqclsGBXur3tzl+nofnSmNWmBshBt0uANsq6dPaTCBNfVMlR6mFjjwyy3SfdoGROE1G7IE/XArWh5xjGpywUxizVngxEM+ti6vlpdmMnq6kKvoap01Gq5MsT7UpcAAC1/eNBG4L6UGnfcERcsGgx7EnBJV+t3NF5KGLpQAB0/fOScX3Oykux+em8VD2mlnHglZjNe153srIcRhV/1txyVitnQrPdledBvKihZJe2yku8aI28YeFb5/M5E90oB8hAddiusRPXAwPPBXJQmQgyC98Ax07tdx0q/p1V+XoLxouCTMpXTFAUHtuw== 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=DIVdaFrfnhkddBwWcMDPfcd4QLzcpfYr1GtSrPLf3QY=; b=g6R4wBWntI4EIA2ghj3NVfRI3/RTMkZPnJdcaEri2Ic8+eJPhjbcMfGFY6GK/r1VHRd64IzlTsN+gdO3FEbTVaQzPu3LsC+7YCqgpEuKJUxJW+fROQrf0jGkFoAPPdhbhkkLw0r+1bUBuD3Z59+/H4qcI5+P+F2YMycenIFJcXzA1KZklb9kftNgkrU3kqbefByE7DpTDZaYaASA9vO0Z9ZJGfvPG4AEkNl+vzQ2wBdNbFnbTLGkYhhgN/y+WHxfE/l++JRRv+77fEAzmWRyvxD8MDeTH9x9QFYp+UpDOe/I+5SmOL90b0PBXZ+EZRaJKmAtbnXLBEnAiwpCMTrolw== 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=DIVdaFrfnhkddBwWcMDPfcd4QLzcpfYr1GtSrPLf3QY=; b=LWS3Cy/FTfXq/l5eAkvXi9ldGhO/tl0sMShpf6vrs1F8gXyniLM5TRn3tDwC1dh/P+XkmDIOurDeC+Hy+1QLo9XGBqs9JQdq40417l6AxMiBUWDSOigNoaH0UPqS9l2kIs1Ffb+zymNac0rrBMVAjoLjKSrMlziZpvAIcgNqz2w= Received: from BYAPR12MB3158.namprd12.prod.outlook.com (20.179.92.19) by BYAPR12MB2965.namprd12.prod.outlook.com (20.178.52.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2178.18; Mon, 19 Aug 2019 21:36:02 +0000 Received: from BYAPR12MB3158.namprd12.prod.outlook.com ([fe80::39b9:76bd:a491:1f27]) by BYAPR12MB3158.namprd12.prod.outlook.com ([fe80::39b9:76bd:a491:1f27%6]) with mapi id 15.20.2157.022; Mon, 19 Aug 2019 21:36:02 +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 12/28] UefiCpuPkg/CpuExceptionHandler: Support string IO for IOIO_PROT NAE events Thread-Topic: [RFC PATCH 12/28] UefiCpuPkg/CpuExceptionHandler: Support string IO for IOIO_PROT NAE events Thread-Index: AQHVVtYZKQjhj8NYP0KajIGU36TiVA== Date: Mon, 19 Aug 2019 21:36:02 +0000 Message-ID: <43b23558db24d8f33039e2dbee59d369a20bda18.1566250534.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: SN2PR01CA0031.prod.exchangelabs.com (2603:10b6:804:2::41) To BYAPR12MB3158.namprd12.prod.outlook.com (2603:10b6:a03:132::19) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Thomas.Lendacky@amd.com; x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [165.204.77.1] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: e7b5eec2-28ef-44b8-6e95-08d724ed3b65 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020);SRVR:BYAPR12MB2965; x-ms-traffictypediagnostic: BYAPR12MB2965: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:7691; x-forefront-prvs: 0134AD334F x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(189003)(199004)(50226002)(53936002)(316002)(5660300002)(2351001)(52116002)(76176011)(36756003)(6436002)(71200400001)(19627235002)(6512007)(7736002)(256004)(86362001)(14454004)(478600001)(305945005)(2906002)(71190400001)(2501003)(64756008)(186003)(118296001)(4326008)(26005)(6486002)(99286004)(1730700003)(386003)(6506007)(66066001)(66476007)(66556008)(8676002)(102836004)(81166006)(25786009)(66446008)(6916009)(54906003)(3846002)(6116002)(486006)(5640700003)(476003)(2616005)(11346002)(446003)(81156014)(66946007)(8936002);DIR:OUT;SFP:1101;SCL:1;SRVR:BYAPR12MB2965;H:BYAPR12MB3158.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: xGqBB6e79hGyRde0A8pm8Dpy08dbIXsCnfDmi9rM+QUbmQ2DLQeLAnXq8bj+SYk+76tq9LT/gj4FZCISoiGhCIs1N8Vwjj9HMde4JWaZOqZFFawbhFLiZwdp2+FBAjN77sZUdwUZW/BmOwc/wDG2ujvO5RsEMTQeXFvJ2d7eacW2ub7U7LkWCIEx0V2GMZPDQPiwUuStcfU8ckF+g1xCt0p4n1auppHb36B+zCOfukbBDGkk5Cz/7A+tg2DWWy4/eue57WMPV0ou/+UGLbFNuu7VdeCII6uApDr5UuvjktEnflqp1LYx4VmcMolq2uLhZCECAyh24ejNBZXCtegNeT4LzcnLkmpIWwn2tLqqRraDglYZPyyLjZWBeoXH1bFz/FOTn+nmQVgtyoHRnU8KS2ioM/lrnPVyeKjfkEkUnK0= MIME-Version: 1.0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e7b5eec2-28ef-44b8-6e95-08d724ed3b65 X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Aug 2019 21:36:02.1055 (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: 5WxwRYGUZpskNzcH/aGfsGE3zm/C1P8IH2D5FhNd8kyquriegPkFcpkLPCbGiINzF+GOosCXKhTaCeim7HzGPg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB2965 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-ID: <59CD7F3C207828458A04B67A2CC1FD7F@namprd12.prod.outlook.com> Content-Transfer-Encoding: quoted-printable From: Tom Lendacky 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. Signed-off-by: Tom Lendacky --- .../X64/AMDSevVcCommon.c | 92 ++++++++++++++++--- 1 file changed, 80 insertions(+), 12 deletions(-) diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c= b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c index 3b69aca48055..2bc156840e74 100644 --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/AMDSevVcCommon.c @@ -278,6 +278,34 @@ IoioExitInfo ( UINT64 ExitInfo =3D 0; =20 switch (*(InstructionData->OpCodes)) { + case 0x6C: /* INSB / INS mem8, DX */ + ExitInfo |=3D IOIO_TYPE_INS; + ExitInfo |=3D IOIO_DATA_8; + ExitInfo |=3D IOIO_SEG_ES; + ExitInfo |=3D ((Regs->Rdx & 0xffff) << 16); + break; + case 0x6D: /* INSW / INS mem16, DX / INSD / INS mem32, DX */ + ExitInfo |=3D IOIO_TYPE_INS; + ExitInfo |=3D (InstructionData->DataSize =3D=3D Size16Bits) ? IOIO_DAT= A_16 + : IOIO_DATA_32; + ExitInfo |=3D IOIO_SEG_ES; + ExitInfo |=3D ((Regs->Rdx & 0xffff) << 16); + break; + + case 0x6E: /* OUTSB / OUTS DX, mem8 */ + ExitInfo |=3D IOIO_TYPE_OUTS; + ExitInfo |=3D IOIO_DATA_8; + ExitInfo |=3D IOIO_SEG_DS; + ExitInfo |=3D ((Regs->Rdx & 0xffff) << 16); + break; + case 0x6F: /* OUTSW / OUTS DX, mem16 / OUTSD / OUTS DX, mem32 */ + ExitInfo |=3D IOIO_TYPE_OUTS; + ExitInfo |=3D (InstructionData->DataSize =3D=3D Size16Bits) ? IOIO_DAT= A_16 + : IOIO_DATA_32; + ExitInfo |=3D IOIO_SEG_DS; + ExitInfo |=3D ((Regs->Rdx & 0xffff) << 16); + break; + case 0xE4: /* IN AL, imm8 */ InstructionData->ImmediateSize =3D 1; InstructionData->End++; @@ -370,8 +398,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) { @@ -389,17 +418,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