Skip to content

Commit

Permalink
OcAppleKernelLib: Fix XcpmExtraMsrs on macOS 15
Browse files Browse the repository at this point in the history
  • Loading branch information
PMheart committed Sep 24, 2024
1 parent 5afe3a6 commit be7e552
Showing 1 changed file with 70 additions and 9 deletions.
79 changes: 70 additions & 9 deletions Library/OcAppleKernelLib/CommonPatches.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,61 @@ PATCHER_GENERIC_PATCH
.Limit = 0
};

STATIC
CONST UINT8
mMiscPwrMgmtRelFind15[] = {
0xB9, 0xAA, 0x01, 0x00, 0x00, ///< mov ecx, 0x1AA
0x0F, 0x32, ///< rdmsr
0x89, 0xD2, ///< mov edx, edx
0x83, 0x00, 0x00, ///< and/or, whatever
0x0F, 0x30 ///< wrmsr
};

STATIC
CONST UINT8
mMiscPwrMgmtRelMask15[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0xFF,
0xFF, 0x00, 0x00,
0xFF, 0xFF
};

STATIC
CONST UINT8
mMiscPwrMgmtRelReplace15[] = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00, 0x00,
0x90, 0x90 ///< nop
};

STATIC
CONST UINT8
mMiscPwrMgmtRelReplaceMask15[] = {
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00,
0x00, 0x00,
0x00, 0x00, 0x00,
0xFF, 0xFF
};

STATIC
PATCHER_GENERIC_PATCH
mMiscPwrMgmtRel15Patch = {
.Comment = DEBUG_POINTER ("MiscPwrMgmtRel Sequoia"),
.Base = NULL,
.Find = mMiscPwrMgmtRelFind15,
.Mask = mMiscPwrMgmtRelMask15,
.Replace = mMiscPwrMgmtRelReplace15,
.ReplaceMask = mMiscPwrMgmtRelReplaceMask15,
.Size = sizeof (mMiscPwrMgmtRelFind15),
.Count = 0,
.Skip = 0,
.Limit = 0
};

STATIC
CONST UINT8
mMiscPwrMgmtDbgFind[] = {
Expand Down Expand Up @@ -446,17 +501,23 @@ PatchAppleXcpmExtraMsrs (

//
// Now patch writes to MSR_MISC_PWR_MGMT.
// On macOS Monterey (12) and above, in theory this no longer exists in XNU,
// yet without it macOS will not boot correctly on certain X299 builds.
//
// Ref: https://github.com/acidanthera/bugtracker/issues/2410#issuecomment-2368925597
//
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtRelPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: Failed to patch writes to XcpmExtraMsrs MSR_MISC_PWR_MGMT - %r, trying dbg\n", Status));
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtDbgPatch);
if (OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_SEQUOIA_MIN, 0)) {
//
// TODO: Find dbg patch on macOS 15+.
//
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtRel15Patch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: Failed to patch writes to XcpmExtraMsrs MSR_MISC_PWR_MGMT - %r\n", Status));
DEBUG ((DEBUG_WARN, "OCAK: Failed to patch writes to XcpmExtraMsrs MSR_MISC_PWR_MGMT macOS 15+ - %r\n", Status));
}
} else {
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtRelPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: Failed to patch writes to XcpmExtraMsrs MSR_MISC_PWR_MGMT old - %r, trying dbg\n", Status));
Status = PatcherApplyGenericPatch (Patcher, &mMiscPwrMgmtDbgPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: Failed to patch writes to XcpmExtraMsrs MSR_MISC_PWR_MGMT old - %r\n", Status));
}
}
}

Expand Down

0 comments on commit be7e552

Please sign in to comment.