16#define MAX_INSTRUCTION_LENGTH 15
110 InstructionData->DisplacementSize = Size;
111 InstructionData->Immediate += Size;
112 InstructionData->End += Size;
135 Ext = &InstructionData->Ext;
137 return ((InstructionData->Mode == LongMode64Bit) &&
138 (Ext->ModRm.Mod == 0) &&
139 (Ext->ModRm.Rm == 5) &&
140 (InstructionData->SibPresent ==
FALSE));
163 UINT64 EffectiveAddress;
165 Ext = &InstructionData->Ext;
166 EffectiveAddress = 0;
174 RipRelative = *(INT32 *)InstructionData->Displacement;
181 return Regs->Rip + (UINT64)RipRelative;
184 switch (Ext->ModRm.Mod) {
187 EffectiveAddress += (UINT64)(*(INT8 *)(InstructionData->Displacement));
190 switch (InstructionData->AddrSize) {
193 EffectiveAddress += (UINT64)(*(INT16 *)(InstructionData->Displacement));
197 EffectiveAddress += (UINT64)(*(INT32 *)(InstructionData->Displacement));
204 if (InstructionData->SibPresent) {
207 if (Ext->Sib.Index != 4) {
211 sizeof (Displacement)
213 Displacement *= (INT64)(1 << Ext->Sib.Scale);
218 EffectiveAddress += (UINT64)Displacement;
221 if ((Ext->Sib.Base != 5) || Ext->ModRm.Mod) {
225 EffectiveAddress += (UINT64)(*(INT32 *)(InstructionData->Displacement));
231 return EffectiveAddress;
255 RexPrefix = &InstructionData->RexPrefix;
256 Ext = &InstructionData->Ext;
257 ModRm = &InstructionData->ModRm;
258 Sib = &InstructionData->Sib;
260 InstructionData->ModRmPresent =
TRUE;
261 ModRm->Uint8 = *(InstructionData->End);
263 InstructionData->Displacement++;
264 InstructionData->Immediate++;
265 InstructionData->End++;
267 Ext->ModRm.Mod = ModRm->Bits.Mod;
268 Ext->ModRm.Reg = (RexPrefix->Bits.BitR << 3) | ModRm->Bits.Reg;
269 Ext->ModRm.Rm = (RexPrefix->Bits.BitB << 3) | ModRm->Bits.Rm;
273 if (Ext->ModRm.Mod == 3) {
276 if (ModRm->Bits.Rm == 4) {
277 InstructionData->SibPresent =
TRUE;
278 Sib->Uint8 = *(InstructionData->End);
280 InstructionData->Displacement++;
281 InstructionData->Immediate++;
282 InstructionData->End++;
284 Ext->Sib.Scale = Sib->Bits.Scale;
285 Ext->Sib.Index = (RexPrefix->Bits.BitX << 3) | Sib->Bits.Index;
286 Ext->Sib.Base = (RexPrefix->Bits.BitB << 3) | Sib->Bits.Base;
312 CC_INSTRUCTION_MODE Mode;
313 CC_INSTRUCTION_SIZE ModeDataSize;
314 CC_INSTRUCTION_SIZE ModeAddrSize;
323 Mode = LongMode64Bit;
324 ModeDataSize = Size32Bits;
325 ModeAddrSize = Size64Bits;
327 InstructionData->Mode = Mode;
328 InstructionData->DataSize = ModeDataSize;
329 InstructionData->AddrSize = ModeAddrSize;
331 InstructionData->Prefixes = InstructionData->Begin;
333 Byte = InstructionData->Prefixes;
334 for ( ; ParsedLength <= MAX_INSTRUCTION_LENGTH; Byte++, InstructionData->PrefixSize++, ParsedLength++) {
340 if ((*Byte >= REX_PREFIX_START) && (*Byte <= REX_PREFIX_STOP)) {
341 InstructionData->RexPrefix.Uint8 = *Byte;
342 if ((*Byte & REX_64BIT_OPERAND_SIZE_MASK) != 0) {
343 InstructionData->DataSize = Size64Bits;
350 case OVERRIDE_SEGMENT_CS:
351 case OVERRIDE_SEGMENT_DS:
352 case OVERRIDE_SEGMENT_ES:
353 case OVERRIDE_SEGMENT_SS:
354 if (Mode != LongMode64Bit) {
355 InstructionData->SegmentSpecified =
TRUE;
356 InstructionData->Segment = (*Byte >> 3) & 3;
361 case OVERRIDE_SEGMENT_FS:
362 case OVERRIDE_SEGMENT_GS:
363 InstructionData->SegmentSpecified =
TRUE;
364 InstructionData->Segment = *Byte & 7;
367 case OVERRIDE_OPERAND_SIZE:
368 if (InstructionData->RexPrefix.Uint8 == 0) {
369 InstructionData->DataSize =
370 (Mode == LongMode64Bit) ? Size16Bits :
371 (Mode == LongModeCompat32Bit) ? Size16Bits :
372 (Mode == LongModeCompat16Bit) ? Size32Bits : 0;
377 case OVERRIDE_ADDRESS_SIZE:
378 InstructionData->AddrSize =
379 (Mode == LongMode64Bit) ? Size32Bits :
380 (Mode == LongModeCompat32Bit) ? Size16Bits :
381 (Mode == LongModeCompat16Bit) ? Size32Bits : 0;
388 InstructionData->RepMode = RepZ;
392 InstructionData->RepMode = RepNZ;
396 InstructionData->OpCodes = Byte;
397 InstructionData->OpCodeSize = (*Byte == TWO_BYTE_OPCODE_ESCAPE) ? 2 : 1;
399 InstructionData->End = Byte + InstructionData->OpCodeSize;
400 InstructionData->Displacement = InstructionData->End;
401 InstructionData->Immediate = InstructionData->End;
424 return (UINT64)(InstructionData->End - InstructionData->Begin);
448 SetMem (InstructionData,
sizeof (*InstructionData), 0);
449 InstructionData->Ghcb = Ghcb;
450 InstructionData->Begin = (UINT8 *)Regs->Rip;
451 InstructionData->End = (UINT8 *)Regs->Rip;
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI SetMem(OUT VOID *Buffer, IN UINTN Length, IN UINT8 Value)
UINT64 * CcGetRegisterPointer(IN EFI_SYSTEM_CONTEXT_X64 *Regs, IN UINT8 Register)
STATIC BOOLEAN IsRipRelative(IN CC_INSTRUCTION_DATA *InstructionData)
VOID CcDecodeModRm(IN EFI_SYSTEM_CONTEXT_X64 *Regs, IN OUT CC_INSTRUCTION_DATA *InstructionData)
STATIC VOID UpdateForDisplacement(IN OUT CC_INSTRUCTION_DATA *InstructionData, IN UINTN Size)
EFI_STATUS CcInitInstructionData(IN OUT CC_INSTRUCTION_DATA *InstructionData, IN GHCB *Ghcb, IN EFI_SYSTEM_CONTEXT_X64 *Regs)
UINT64 CcInstructionLength(IN CC_INSTRUCTION_DATA *InstructionData)
STATIC EFI_STATUS DecodePrefixes(IN EFI_SYSTEM_CONTEXT_X64 *Regs, IN OUT CC_INSTRUCTION_DATA *InstructionData)
STATIC UINT64 GetEffectiveMemoryAddress(IN EFI_SYSTEM_CONTEXT_X64 *Regs, IN CC_INSTRUCTION_DATA *InstructionData)
EFI_STATUS EFIAPI Register(IN EFI_PEI_RSC_HANDLER_CALLBACK Callback)