Skip to content

Commit

Permalink
Wrap advanced logger buffer cursor when the logging area is full (mic…
Browse files Browse the repository at this point in the history
…rosoft#408)

## Description

The advanced logging at runtime is lack of usage. However, this becomes
increasingly important for server platforms, where the system could
potentially stay up for months, or even years. This will make the
advanced logger fill up the buffer during runtime/MM logging well ahead
of a reset event, making the buffer content stale when being reviewed.

This change added a feature PCD guarded feature that, when enabled, will
automatically wrap the LogCurrent cursor to the beginning and continue
to log. This will effectively create a circular buffer as the stale
content will remain in place. The tooling update is still under
development.

- [x] Impacts functionality?
- **Functionality** - Does the change ultimately impact how firmware
functions?
- Examples: Add a new library, publish a new PPI, update an algorithm,
...
- [ ] Impacts security?
- **Security** - Does the change have a direct security impact on an
application,
    flow, or firmware?
  - Examples: Crypto algorithm change, buffer overflow fix, parameter
    validation improvement, ...
- [ ] Breaking change?
- **Breaking change** - Will anyone consuming this change experience a
break
    in build or boot behavior?
- Examples: Add a new library class, move a module to a different repo,
call
    a function in a new library class in a pre-existing module, ...
- [x] Includes tests?
  - **Tests** - Does the change include any explicit test code?
  - Examples: Unit tests, integration tests, robot tests, ...
- [ ] Includes documentation?
- **Documentation** - Does the change contain explicit documentation
additions
    outside direct code modifications (and comments)?
- Examples: Update readme file, add feature readme file, link to
documentation
    on an a separate Web page, ...

## How This Was Tested

This was tested on QEMU Q35 along with the UEFI shell based test app to
verify that the entry after wrapping is correct and is multi-threading
safe.

## Integration Instructions

Platforms can enable this feature by setting
`gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoClearEnable|TRUE`
  • Loading branch information
kuqin12 authored Jan 18, 2024
1 parent de34c20 commit d269a99
Show file tree
Hide file tree
Showing 14 changed files with 621 additions and 14 deletions.
5 changes: 5 additions & 0 deletions AdvLoggerPkg/AdvLoggerPkg.dec
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
#
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedFileLoggerForceEnable|TRUE|BOOLEAN|0x00010184

## PcdAdvancedLoggerAutoWrapEnable - Automatically wrap around the LogCurrent cursor when it reaches the end
# of the log. This feature only activates at runtime (after exit boot service event).
#
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable|FALSE|BOOLEAN|0x00010188


[PcdsFixedAtBuild]
## Advanced Logger Base - NULL = UEFI starts with PEI or DXE, and there is no SEC, or SEC
Expand Down
1 change: 1 addition & 0 deletions AdvLoggerPkg/AdvLoggerPkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
AdvLoggerPkg/Library/AssertLib/AssertLib.inf
AdvLoggerPkg/Library/AssertTelemetryLib/AssertLib.inf
AdvLoggerPkg/UnitTests/LineParser/LineParserTestApp.inf
AdvLoggerPkg/UnitTests/AdvancedLoggerWrapper/AdvancedLoggerWrapperTestApp.inf

[BuildOptions]
#force deprecated interfaces off
Expand Down
52 changes: 38 additions & 14 deletions AdvLoggerPkg/Library/AdvancedLoggerLib/AdvancedLoggerCommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,44 @@ AdvancedLoggerMemoryLoggerWrite (
if ((UsedSize >= LoggerInfo->LogBufferSize) ||
((LoggerInfo->LogBufferSize - UsedSize) < EntrySize))
{
//
// Update the number of bytes of log that have not been captured
//
do {
CurrentSize = LoggerInfo->DiscardedSize;
NewSize = CurrentSize + (UINT32)NumberOfBytes;
OldSize = InterlockedCompareExchange32 (
(UINT32 *)&LoggerInfo->DiscardedSize,
(UINT32)CurrentSize,
(UINT32)NewSize
);
} while (OldSize != CurrentSize);

return LoggerInfo;
if (FeaturePcdGet (PcdAdvancedLoggerAutoWrapEnable) && (LoggerInfo->AtRuntime)) {
//
// Wrap around the current cursor when auto wrap is enabled on buffer full during runtime.
//
NewBuffer = LoggerInfo->LogBuffer;
OldValue = InterlockedCompareExchange64 (
(UINT64 *)&LoggerInfo->LogCurrent,
(UINT64)CurrentBuffer,
(UINT64)NewBuffer
);
if (OldValue != CurrentBuffer) {
//
// Another thread has updated the buffer, we should retry the logging.
//
continue;
}

// Now that we have a buffer that starts from the beginning, proceed to log the current message, from the beginning.
// Note that in this case, if there are other threads in the middle of logging a message, they will continue to write
// to the end of the buffer as it fits.
// If there is another clearing attempt on the other thread, i.e. another thread also try to fill up the buffer, the
// first clear will take effect and the other log entries will fail to update and proceed with a normal retry.
} else {
//
// Update the number of bytes of log that have not been captured
//
do {
CurrentSize = LoggerInfo->DiscardedSize;
NewSize = CurrentSize + (UINT32)NumberOfBytes;
OldSize = InterlockedCompareExchange32 (
(UINT32 *)&LoggerInfo->DiscardedSize,
(UINT32)CurrentSize,
(UINT32)NewSize
);
} while (OldSize != CurrentSize);

return LoggerInfo;
}
}

NewBuffer = PA_FROM_PTR ((CHAR8_FROM_PA (CurrentBuffer) + EntrySize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerBase ## CONSUMES
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## SOMETIMES_CONSUMES

[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable

[Depex]
TRUE
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@
[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerLocator
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerFixedInRAM
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@
[Pcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## SOMETIMES_CONSUMES

[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable

[Depex]
TRUE
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerFixedInRAM
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable

[FixedPcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
[Pcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerBase ## CONSUMES
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## SOMETIMES_CONSUMES
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable ## SOMETIMES_CONSUMES
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerPeiInRAM ## CONSUMES
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerFixedInRAM ## CONSUMES
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable

[FixedPcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerBase ## CONSUMES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@

[Pcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## SOMETIMES_CONSUMES

[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerPages ## CONSUMES
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## CONSUMES

[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable

[BuildOptions]
*_*_*_CC_FLAGS = -D ADVANCED_LOGGER_SEC=1
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@

[Pcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerHdwPortDebugPrintErrorLevel ## SOMETIMES_CONSUMES

[FeaturePcd]
gAdvLoggerPkgTokenSpaceGuid.PcdAdvancedLoggerAutoWrapEnable
Loading

0 comments on commit d269a99

Please sign in to comment.