Skip to content

Commit

Permalink
Build: Switch to BaseMemoryLibRepStr for optimised memory routines
Browse files Browse the repository at this point in the history
We cannot use BaseMemoryLibOptDxe since it uses SSE instructions,
and some firmwares fail to properly maintain MMX register contexts
across the timers. This results in exceptions when trying to execute
primitives like CopyMem in timers (e.g. AIKDataWriteEntry).
Reproduced on ASUS M5A97 with AMD FX8320 CPU.

closes acidanthera/bugtracker#754
  • Loading branch information
vit9696 committed May 31, 2020
1 parent ae5e1f4 commit 0fdae43
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 4 deletions.
102 changes: 102 additions & 0 deletions Application/VerifyMemOpt/VerifyMemOpt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/** @file
Check memory routine compatibility.
Copyright (c) 2018, vit9696. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include <Uefi.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseLib.h>
#include <Library/DebugLib.h>
#include <Library/OcMiscLib.h>
#include <Library/UefiLib.h>
#include <Library/UefiBootServicesTableLib.h>

STATIC UINT8 mSource[64];
STATIC UINT8 mDestination[64];

EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
UINT8 *Src;
UINT8 *Dst;
UINT32 Size;
IA32_CR0 Cr0;
IA32_CR4 Cr4;
IA32_EFLAGS32 Flags;

Src = ALIGN_POINTER (mSource, 16);
Dst = ALIGN_POINTER (mDestination, 16);
Size = 32;

gST->ConOut->OutputString (gST->ConOut, L"VerifyMemOpt\r\n");
gBS->Stall (SECONDS_TO_MICROSECONDS (1));

Cr0.UintN = AsmReadCr0 ();
Cr4.UintN = AsmReadCr4 ();
Flags.UintN = AsmReadEflags();

//
// CR0.MP bit (BIT1) must be set.
// CR0.EM bit (BIT2) must be cleared.
// CR4.OSFXSR bit (BIT9) must be set.
// CR4.OSXMMEXCPT bit (BIT10) must be set.
//
DEBUG ((DEBUG_WARN, "VMOPT: CR0 %08X CR4 %08X EFLAGS %08X\n", Cr0, Cr4, Flags));

if (Cr0.Bits.MP == 0) {
DEBUG ((DEBUG_WARN, "VMOPT: WARN CR0 MP is NOT set\n"));
}

if (Cr0.Bits.EM != 0) {
DEBUG ((DEBUG_WARN, "VMOPT: WARN CR0 EM is set\n"));
}

if (Cr4.Bits.OSFXSR == 0) {
DEBUG ((DEBUG_WARN, "VMOPT: WARN CR4 OSFXSR is NOT set\n"));
}

if (Cr4.Bits.OSXMMEXCPT == 0) {
DEBUG ((DEBUG_WARN, "VMOPT: WARN CR4 OSXMMEXCPT is NOT set\n"));
}

if (Flags.Bits.DF != 0) {
DEBUG ((DEBUG_WARN, "VMOPT: WARN EFLAGS DF is set\n"));
}

DEBUG ((DEBUG_WARN, "VMOPT: CopyMem aligned src %p/aligned dst %p/size %u\n", Src, Dst, Size));
gBS->Stall (SECONDS_TO_MICROSECONDS (1));
CopyMem (Dst, Src, Size);

Src += 6;
DEBUG ((DEBUG_WARN, "VMOPT: CopyMem unaligned src %p/aligned dst %p/size %u\n", Src, Dst, Size));
gBS->Stall (SECONDS_TO_MICROSECONDS (1));
CopyMem (Dst, Src, Size);

Size -= 12;
DEBUG ((DEBUG_WARN, "VMOPT: CopyMem unaligned src %p/aligned dst %p/size %u\n", Src, Dst, Size));
gBS->Stall (SECONDS_TO_MICROSECONDS (1));
CopyMem (Dst, Src, Size);

Dst += 6;
DEBUG ((DEBUG_WARN, "VMOPT: CopyMem unaligned src %p/unaligned dst %p/size %u\n", Src, Dst, Size));
gBS->Stall (SECONDS_TO_MICROSECONDS (1));
CopyMem (Dst, Src, Size);

DEBUG ((DEBUG_WARN, "VMOPT: Done testing\n"));
gBS->Stall (SECONDS_TO_MICROSECONDS (1));

return EFI_SUCCESS;
}
48 changes: 48 additions & 0 deletions Application/VerifyMemOpt/VerifyMemOpt.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## @file
# Check memory routine compatibility.
#
# Copyright (c) 2018, vit9696. All rights reserved.<BR>
#
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = VerifyMemOpt
FILE_GUID = 00697824-B09E-43D6-8A2A-1CD9E5C7BFBC
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 1.0
ENTRY_POINT = UefiMain

#
# This flag specifies whether HII resource section is generated into PE image.
#
UEFI_HII_RESOURCE_SECTION = TRUE

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 IPF EBC
#

[Sources]
VerifyMemOpt.c

[Packages]
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec
OpenCorePkg/OpenCorePkg.dec
UefiCpuPkg/UefiCpuPkg.dec

[LibraryClasses]
OcConsoleControlEntryModeGenericLib
UefiApplicationEntryPoint
UefiLib
PcdLib
IoLib
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ OpenCore Changelog
- Added prebuilt version of `CrScreenshotDxe` driver
- Fixed Hyper-V frequency detection compatibility
- Added `SysReport` option for DEBUG builds to dump system info
- Fixed crashes on some AMD firmwares when performing keyboard input

#### v0.5.8
- Fixed invalid CPU object reference in SSDT-PLUG
Expand Down
1 change: 1 addition & 0 deletions Docs/Configuration.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,7 @@ \subsection{Debug Properties}\label{miscdebugprops}
\item \texttt{OCRST} --- ResetSystem
\item \texttt{OCUI} --- OpenCanopy
\item \texttt{OC} --- OpenCore main
\item \texttt{VMOPT} --- VerifyMemOpt
\end{itemize}

\textbf{Libraries}:
Expand Down
16 changes: 13 additions & 3 deletions OpenCorePkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@

[LibraryClasses]
BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
BaseMemoryLib|MdePkg/Library/BaseMemoryLibOptDxe/BaseMemoryLibOptDxe.inf
# We cannot use BaseMemoryLibOptDxe since it uses SSE instructions,
# and some firmwares fail to properly maintain MMX register contexts
# across the timers. This results in exceptions when trying to execute
# primitives like CopyMem in timers (e.g. AIKDataWriteEntry).
# Reproduced on ASUS M5A97 with AMD FX8320 CPU.
# REF: https://github.com/acidanthera/bugtracker/issues/754
BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf
BaseRngLib|MdePkg/Library/BaseRngLib/BaseRngLib.inf
BcfgCommandLib|ShellPkg/Library/UefiShellBcfgCommandLib/UefiShellBcfgCommandLib.inf
CpuLib|MdePkg/Library/BaseCpuLib/BaseCpuLib.inf
Expand Down Expand Up @@ -127,13 +133,13 @@
!include NetworkPkg/NetworkLibs.dsc.inc

[Components]
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf{
MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressDxe.inf {
<LibraryClasses>
!if $(TARGET) == RELEASE
DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
!endif
}
MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf{
MdeModulePkg/Bus/Pci/XhciDxe/XhciDxe.inf {
<LibraryClasses>
!if $(TARGET) == RELEASE
DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
Expand All @@ -155,6 +161,10 @@
OpenCorePkg/Application/PavpProvision/PavpProvision.inf
OpenCorePkg/Application/ResetSystem/ResetSystem.inf
OpenCorePkg/Application/RtcRw/RtcRw.inf
OpenCorePkg/Application/VerifyMemOpt/VerifyMemOpt.inf {
<LibraryClasses>
BaseMemoryLib|MdePkg/Library/BaseMemoryLibOptDxe/BaseMemoryLibOptDxe.inf
}
OpenCorePkg/Application/VerifyMsrE2/VerifyMsrE2.inf
OpenCorePkg/Debug/GdbSyms/GdbSyms.inf
OpenCorePkg/Library/OcAcpiLib/OcAcpiLib.inf
Expand Down
2 changes: 1 addition & 1 deletion OpenDuetPkg.dsc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#
BaseLib|MdePkg/Library/BaseLib/BaseLib.inf
!if $(ARCH) == X64
BaseMemoryLib|MdePkg/Library/BaseMemoryLibOptDxe/BaseMemoryLibOptDxe.inf
BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf
!else
BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
!endif
Expand Down

0 comments on commit 0fdae43

Please sign in to comment.