From 75d1a7903db171d80f9728b24274852ab613ed2b Mon Sep 17 00:00:00 2001 From: "Lendacky, Thomas" Date: Thu, 29 Apr 2021 12:12:10 -0500 Subject: [PATCH] OvfmPkg/VmgExitLib: Properly decode MMIO MOVZX and MOVSX opcodes BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3345 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 Message-Id: <5949d54cb2c9ab69256f67ed5654b32654c0501c.1619716333.git.thomas.lendacky@amd.com> --- OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c b/OvmfPkg/Library/VmgExitLib/VmgExitVcHandler.c index 24259060fd..b716541ad1 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 = 0; GpEvent.Elements.Vector = GP_EXCEPTION; GpEvent.Elements.Type = GHCB_EVENT_INJECTION_TYPE_EXCEPTION; @@ -817,6 +820,7 @@ MmioExit ( // fall through // case 0xB7: + DecodeModRm (Regs, InstructionData); Bytes = (Bytes != 0) ? Bytes : 2; Status = ValidateMmioMemory (Ghcb, InstructionData->Ext.RmData, Bytes); @@ -835,7 +839,7 @@ MmioExit ( } Register = GetRegisterPointer (Regs, InstructionData->Ext.ModRm.Reg); - SetMem (Register, InstructionData->DataSize, 0); + SetMem (Register, (UINTN) (1 << InstructionData->DataSize), 0); CopyMem (Register, Ghcb->SharedBuffer, Bytes); break; @@ -848,6 +852,7 @@ MmioExit ( // fall through // case 0xBF: + DecodeModRm (Regs, InstructionData); Bytes = (Bytes != 0) ? Bytes : 2; Status = ValidateMmioMemory (Ghcb, InstructionData->Ext.RmData, Bytes); @@ -878,7 +883,7 @@ MmioExit ( } Register = GetRegisterPointer (Regs, InstructionData->Ext.ModRm.Reg); - SetMem (Register, InstructionData->DataSize, SignByte); + SetMem (Register, (UINTN) (1 << InstructionData->DataSize), SignByte); CopyMem (Register, Ghcb->SharedBuffer, Bytes); break;