Skip to content

Commit

Permalink
Add CPU MP Debug Protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorBeebe authored and kenlautner committed May 4, 2023
1 parent 55b5c27 commit 00f90c1
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 3 deletions.
2 changes: 0 additions & 2 deletions UefiCpuPkg/CpuDxe/CpuMp.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ InitializeExceptionStackSwitchHandlers (

MpInitLibWhoAmI (&Index);
SwitchStackData = (EXCEPTION_STACK_SWITCH_CONTEXT *)Buffer;

//
// This may be called twice for each Cpu. Only run InitializeSeparateExceptionStacks
// if this is the first call or the first call failed because of size too small.
Expand Down Expand Up @@ -709,7 +708,6 @@ InitializeMpExceptionStackSwitchHandlers (
ASSERT (SwitchStackData[Index].Status == EFI_SUCCESS);
}
}

FreePool (SwitchStackData);
}

Expand Down
21 changes: 21 additions & 0 deletions UefiCpuPkg/Include/Library/MpInitLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,25 @@ MpInitLibStartupAllCPUs (
IN VOID *ProcedureArgument OPTIONAL
);

// MU_CHANGE START: Support for protocol for reporting multi-processor debug info

/**
Add CPU_MP_DEBUG_PROTOCOL entry to the global list
@param[in] StackBuffer Start of AP stack buffer
@param[in] StackSize Size of the stack
@param[in] CpuNumber AP CPU number
@param[in] IsSwitchStack If the input buffer is the CPU switch stack
**/
VOID
EFIAPI
AppendCpuMpDebugProtocolEntry (
UINTN StackBuffer,
UINTN StackSize,
UINTN CpuNumber,
BOOLEAN IsSwitchStack
);

// MU_CHANGE END

#endif
31 changes: 31 additions & 0 deletions UefiCpuPkg/Include/Protocol/CpuMpDebug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @file -- CpuMpDebug.h
This protocol provides debug access to AP buffer information for validation and testing.
Copyright (c) Microsoft Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef __CPU_MP_DEBUG_PROTOCOL__
#define __CPU_MP_DEBUG_PROTOCOL__

#define CPU_MP_DEBUG_PROTOCOL_GUID \
{ \
0xce05eb65, 0x9416, 0x4197, {0x98, 0x4b, 0xa2, 0x8b, 0x62, 0x9c, 0x64, 0x4d } \
}

#define CPU_MP_DEBUG_SIGNATURE SIGNATURE_32 ('C','M','P','S')

typedef struct _CPU_MP_DEBUG_PROTOCOL {
UINT32 Signature;
UINTN ApStackBuffer;
UINTN ApStackSize;
UINTN CpuNumber;
BOOLEAN IsSwitchStack;
LIST_ENTRY Link;
} CPU_MP_DEBUG_PROTOCOL;

extern EFI_GUID gCpuMpDebugProtocolGuid;

#endif
1 change: 1 addition & 0 deletions UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
[Protocols]
gEfiTimerArchProtocolGuid ## SOMETIMES_CONSUMES
gEfiMemoryAttributeProtocolGuid ## CONSUMES ## MU_CHANGE
gCpuMpDebugProtocolGuid ## PRODUCES ## MU_CHANGE

[Guids]
gEfiEventExitBootServicesGuid ## CONSUMES ## Event
Expand Down
93 changes: 92 additions & 1 deletion UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
#include <Protocol/Timer.h>

#include <Library/DxeMemoryProtectionHobLib.h> // MU_CHANGE
// MU_CHANGE: Add protocol for reporting multi-processor debug info
#include <Protocol/CpuMpDebug.h>
CPU_MP_DEBUG_PROTOCOL mCpuMpDebugProtocol = {
CPU_MP_DEBUG_SIGNATURE,
0,
0,
0,
FALSE,
INITIALIZE_LIST_HEAD_VARIABLE (mCpuMpDebugProtocol.Link)
};

// MU_CHANGE END

#define AP_SAFE_STACK_SIZE 128

CPU_MP_DATA *mCpuMpData = NULL;
Expand All @@ -34,6 +47,65 @@ volatile UINT32 mNumberToFinish = 0;
// Begin wakeup buffer allocation below 0x88000
//
STATIC EFI_PHYSICAL_ADDRESS mSevEsDxeWakeupBuffer = 0x88000;
// MU_CHANGE START: Install protocol for reporting multi-processor debug info

/**
Add CPU_MP_DEBUG_PROTOCOL entry to the global list
@param[in] StackBuffer Start of AP stack buffer
@param[in] StackSize Size of the stack
@param[in] CpuNumber AP CPU number
@param[in] IsSwitchStack If the input buffer is the CPU switch stack
**/
VOID
EFIAPI
AppendCpuMpDebugProtocolEntry (
UINTN StackBuffer,
UINTN StackSize,
UINTN CpuNumber,
BOOLEAN IsSwitchStack
)
{
CPU_MP_DEBUG_PROTOCOL *Entry;

Entry = AllocatePool (sizeof (CPU_MP_DEBUG_PROTOCOL));
if (Entry == NULL) {
DEBUG ((DEBUG_ERROR, "%a - Failed to allocate memory!\n", __FUNCTION__));
return;
}

Entry->Signature = CPU_MP_DEBUG_SIGNATURE;
Entry->ApStackBuffer = StackBuffer;
Entry->ApStackSize = StackSize;
Entry->CpuNumber = CpuNumber;
Entry->IsSwitchStack = IsSwitchStack;
InsertTailList (&mCpuMpDebugProtocol.Link, &Entry->Link);
}

// MU_CHANGE START: Install protocol for reporting multi-processor debug info

/**
Install the CPU MP debug protocol
**/
STATIC
VOID
InstallCpuMpDebugProtocol (
VOID
)
{
EFI_HANDLE MpDebugHandle = NULL;
EFI_STATUS Status;

Status = gBS->InstallMultipleProtocolInterfaces (
&MpDebugHandle,
&gCpuMpDebugProtocolGuid,
&mCpuMpDebugProtocol,
NULL
);
DEBUG ((DEBUG_INFO, "Installed gCpuMpDebugProtocolGuid - Status: %r\n", Status));
}

// MU_CHANGE END

/**
Enable Debug Agent to support source debugging on AP function.
Expand Down Expand Up @@ -519,16 +591,35 @@ InitMpGlobalData (
MemDesc.Attributes | EFI_MEMORY_RP
);
ASSERT_EFI_ERROR (Status);

AppendCpuMpDebugProtocolEntry (StackBase, CpuMpData->CpuApStackSize, Index, FALSE); // MU_CHANGE
DEBUG ((
DEBUG_INFO,
"Stack Guard set at %lx [cpu%lu]!\n",
(UINT64)StackBase,
(UINT64)Index
));
}

InstallCpuMpDebugProtocol (); // MU_CHANGE
}
// MU_CHANGE START: Add the Debug Protocol in the case that CpuStackGuard is not active
else {
CpuInfoInHob = (CPU_INFO_IN_HOB *)(UINTN)CpuMpData->CpuInfoInHob;
for (Index = 0; Index < CpuMpData->CpuCount; ++Index) {
if ((CpuInfoInHob != NULL) && (CpuInfoInHob[Index].ApTopOfStack != 0)) {
StackBase = (UINTN)CpuInfoInHob[Index].ApTopOfStack - CpuMpData->CpuApStackSize;
} else {
StackBase = CpuMpData->Buffer + Index * CpuMpData->CpuApStackSize;
}

AppendCpuMpDebugProtocolEntry (StackBase, CpuMpData->CpuApStackSize, Index, FALSE);
}

InstallCpuMpDebugProtocol ();
}

// MU_CHANGE END

//
// Avoid APs access invalid buffer data which allocated by BootServices,
// so we will allocate reserved data for AP loop code. We also need to
Expand Down
24 changes: 24 additions & 0 deletions UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,27 @@ PlatformShadowMicrocode (

return EFI_SUCCESS;
}

// MU_CHANGE START: Support for protocol for reporting multi-processor debug info

/**
Add CPU_MP_DEBUG_PROTOCOL entry to the global list
@param[in] StackBuffer Start of AP stack buffer
@param[in] StackSize Size of the stack
@param[in] CpuNumber AP CPU number
@param[in] IsSwitchStack If the input buffer is the CPU switch stack
**/
VOID
EFIAPI
AppendCpuMpDebugProtocolEntry (
UINTN StackBuffer,
UINTN StackSize,
UINTN CpuNumber,
BOOLEAN IsSwitchStack
)
{
return;
}

// MU_CHANGE END
7 changes: 7 additions & 0 deletions UefiCpuPkg/UefiCpuPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,17 @@

## Include/Protocol/SmMonitorInit.h
gEfiSmMonitorInitProtocolGuid = { 0x228f344d, 0xb3de, 0x43bb, { 0xa4, 0xd7, 0xea, 0x20, 0xb, 0x1b, 0x14, 0x82 }}

#MSCHANGE - Add support for testing Smm protections
#
## Include/Protocol/SmmExceptionTestProtocol.h
gSmmExceptionTestProtocolGuid = { 0xb76383a1, 0x0e70, 0x4a3f, { 0x86, 0xb4, 0xc6, 0x13, 0x4c, 0x8e, 0x57, 0x23 }}

# MU_CHANGE - Add protocol for reporting multi-processor debug info
#
## Include/Protocol/CpuMpDebug.h
gCpuMpDebugProtocolGuid = { 0xce05eb65, 0x9416, 0x4197, {0x98, 0x4b, 0xa2, 0x8b, 0x62, 0x9c, 0x64, 0x4d }}

#
# [Error.gUefiCpuPkgTokenSpaceGuid]
# 0x80000001 | Invalid value provided.
Expand Down Expand Up @@ -201,6 +207,7 @@
## Size of teh area of memory where the SEV-ES work area block lives.
# @Prompt Configure the SEV-ES work area base
gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaSize|0x0|UINT32|0x30002006

## MS_CHANGE
## If TRUE, the default Cpu Exception Handler in SMM will reboot
# This handler state can be changed by using the Exception Test Protocol
Expand Down

0 comments on commit 00f90c1

Please sign in to comment.