Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeySlice committed Mar 13, 2022
2 parents 70b8a24 + f1e182f commit c1cd3ed
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
69 changes: 36 additions & 33 deletions Library/OcAppleKernelLib/CommonPatches.c
Original file line number Diff line number Diff line change
Expand Up @@ -1979,45 +1979,38 @@ mApfsTimeoutPatch = {
.Limit = 0
};

STATIC
UINT8
mApfsTimeoutV2Find[] = {
0x40, 0x42, 0x0F, 0x00
};
VOID
PatchSetApfsTimeout (
IN UINT32 Timeout
)
{
// FIXME: This is really ugly, make quirks take a context param.
DEBUG ((DEBUG_INFO, "OCAK: Registering %u APFS timeout\n", Timeout));
CopyMem (&mApfsTimeoutReplace[2], &Timeout, sizeof (Timeout));
}

STATIC
UINT8
mApfsTimeoutV2Replace[] = {
0x00, 0x02, 0x00, 0x00
mApfsDisableTrimReplace[] = {
0x31, 0xC0, ///< xor eax, eax
0xC3 ///< ret
};


STATIC
PATCHER_GENERIC_PATCH
mApfsTimeoutV2Patch = {
.Comment = DEBUG_POINTER ("ApfsTimeout V2"),
.Base = "_spaceman_scan_free_blocks",
.Find = mApfsTimeoutV2Find,
mApfsDisableTrimPatch = {
.Comment = DEBUG_POINTER ("ApfsTimeout disable trim"),
.Base = "_spaceman_iterate_free_extents_internal",
.Find = NULL,
.Mask = NULL,
.Replace = mApfsTimeoutV2Replace,
.Replace = mApfsDisableTrimReplace,
.ReplaceMask = NULL,
.Size = sizeof (mApfsTimeoutV2Find),
.Count = 2,
.Size = sizeof (mApfsDisableTrimReplace),
.Count = 1,
.Skip = 0,
.Limit = 4096
.Limit = 0
};

VOID
PatchSetApfsTimeout (
IN UINT32 Timeout
)
{
// FIXME: This is really ugly, make quirks take a context param.
DEBUG ((DEBUG_INFO, "OCAK: Registering %u APFS timeout\n", Timeout));
CopyMem (&mApfsTimeoutReplace[2], &Timeout, sizeof (Timeout));
CopyMem (&mApfsTimeoutV2Replace[0], &Timeout, sizeof (Timeout));
}

STATIC
EFI_STATUS
PatchSetApfsTrimTimeout (
Expand All @@ -2036,25 +2029,35 @@ PatchSetApfsTrimTimeout (
return EFI_NOT_FOUND;
}

if (KernelVersion >= KERNEL_VERSION_MONTEREY_MIN) {
Status = PatcherApplyGenericPatch (Patcher, &mApfsTimeoutV2Patch);
//
// Disable trim using another patch when timeout is 0.
//
if (mApfsTimeoutReplace[2] == 0) {
Status = PatcherApplyGenericPatch (Patcher, &mApfsDisableTrimPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: Failed to apply patch SetApfsTrimTimeoutV2 - %r\n", Status));
DEBUG ((DEBUG_INFO, "OCAK: Failed to apply patch ApfsDisableTrim - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: Patch success SetApfsTrimTimeoutV2\n"));
DEBUG ((DEBUG_INFO, "OCAK: Patch success ApfsDisableTrim\n"));
}
} else {

return Status;
}

if (KernelVersion < KERNEL_VERSION_MONTEREY_MIN) {
Status = PatcherApplyGenericPatch (Patcher, &mApfsTimeoutPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: Failed to apply patch SetApfsTrimTimeout - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: Patch success SetApfsTrimTimeout\n"));
}
}

return Status;
}

DEBUG ((DEBUG_INFO, "OCAK: Skipping patch SetApfsTrimTimeout on macOS 12.0+\n"));
return EFI_SUCCESS;
}

//
// Quirks table.
//
Expand Down
24 changes: 17 additions & 7 deletions Library/OcAppleKernelLib/CpuidPatches.c
Original file line number Diff line number Diff line change
Expand Up @@ -1215,14 +1215,15 @@ mProvideCurrentCpuInfoTopologyCorePerPackageV2Patch = {

STATIC
EFI_STATUS
PatchProvideCurrentCpuInfoForAmpCpu (
PatchProvideCurrentCpuInfoMSR35h (
IN OUT PATCHER_CONTEXT *Patcher,
IN OC_CPU_INFO *CpuInfo,
IN UINT32 KernelVersion
)
{
EFI_STATUS Status;
UINT32 CoreThreadCount;
BOOLEAN IsAmpCpu;

//
// TODO: We can support older, just there is no real need.
Expand All @@ -1233,8 +1234,20 @@ PatchProvideCurrentCpuInfoForAmpCpu (
return EFI_SUCCESS;
}

IsAmpCpu = (CpuInfo->ThreadCount % CpuInfo->CoreCount) != 0;

//
// Provide real values for normal CPUs.
// Provide Thread=Thread for AMP (ADL) CPUs.
//
if (IsAmpCpu) {
CoreThreadCount =
(((UINT32) CpuInfo->ThreadCount) << 16U) | ((UINT32) CpuInfo->ThreadCount);
} else {
CoreThreadCount =
(((UINT32) CpuInfo->CoreCount) << 16U) | ((UINT32) CpuInfo->ThreadCount);
}

CopyMem (
&mProvideCurrentCpuInfoTopologyCoreCountReplace[1],
&CoreThreadCount,
Expand All @@ -1248,7 +1261,7 @@ PatchProvideCurrentCpuInfoForAmpCpu (

DEBUG ((DEBUG_INFO, "OCAK: Patching MSR 35h to %08x - %r\n", CoreThreadCount, Status));

if (EFI_ERROR (Status)) {
if (EFI_ERROR (Status) || !IsAmpCpu) {
return Status;
}

Expand Down Expand Up @@ -1317,11 +1330,9 @@ PatchProvideCurrentCpuInfo (
UINT32 msrCoreThreadCount;

ASSERT (Patcher != NULL);
if (!Patcher) return EFI_INVALID_PARAMETER;

if (!CpuInfo->Hypervisor && CpuInfo->Vendor[0] == CPUID_VENDOR_INTEL) {
return PatchProvideCurrentCpuInfoForAmpCpu (Patcher, CpuInfo, KernelVersion);
}
Status = EFI_SUCCESS;
Status |= PatchProvideCurrentCpuInfoMSR35h (Patcher, CpuInfo, KernelVersion);

Start = ((UINT8 *) MachoGetMachHeader (&Patcher->MachContext));

Expand All @@ -1338,7 +1349,6 @@ PatchProvideCurrentCpuInfo (
//
// Pull required symbols.
//
Status = EFI_SUCCESS;
Status |= PatcherGetSymbolAddress (Patcher, "_tsc_init", (UINT8 **) &TscInitFunc);
Status |= PatcherGetSymbolAddress (Patcher, "_tmrCvt", (UINT8 **) &TmrCvtFunc);

Expand Down

0 comments on commit c1cd3ed

Please sign in to comment.