-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
CommonPatches.c
2995 lines (2617 loc) · 84.9 KB
/
CommonPatches.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
Commonly used kext and kernel patches.
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 <Base.h>
#include <IndustryStandard/AppleIntelCpuInfo.h>
#include <Library/BaseMemoryLib.h>
#include <Library/OcDebugLogLib.h>
#include <Library/OcAppleKernelLib.h>
#include <Library/PrintLib.h>
#include <Library/OcFileLib.h>
#include <Library/OcStringLib.h>
#include <Library/UefiLib.h>
STATIC
CONST UINT8
mMovEcxE2[] = {
0xB9, 0xE2, 0x00, 0x00, 0x00 ///< mov ecx, 0xE2
};
STATIC_ASSERT (sizeof (mMovEcxE2) == 5, "Unsupported mMovEcxE2");
STATIC
CONST UINT8
mMovCxE2[] = {
0x66, 0xB9, 0xE2, 0x00 ///< mov cx, 0xE2
};
STATIC_ASSERT (sizeof (mMovCxE2) == 4, "Unsupported mMovCxE2");
STATIC
CONST UINT8
mWrmsr[] = {
0x0F, 0x30 ///< wrmsr
};
STATIC_ASSERT (sizeof (mWrmsr) == 2, "Unsupported mWrmsr");
STATIC
CONST UINTN
mWrmsrMaxDistance = 32;
STATIC
EFI_STATUS
PatchAppleCpuPmCfgLock (
IN OUT PATCHER_CONTEXT *Patcher OPTIONAL,
IN UINT32 KernelVersion
)
{
UINTN Count;
UINT8 *Walker;
UINT8 *WalkerEnd;
UINT8 *WalkerTmp;
//
// NOTE: As of macOS 13.0 AICPUPM kext is removed.
// However, legacy version of this kext may be injected and patched,
// thus no need to perform system version check here.
//
if (Patcher == NULL) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping %a on NULL Patcher on kernel version %u\n", __func__, KernelVersion));
return OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_VENTURA_MIN, 0) ? EFI_SUCCESS : EFI_NOT_FOUND;
}
Count = 0;
Walker = (UINT8 *)MachoGetMachHeader (&Patcher->MachContext);
WalkerEnd = Walker + MachoGetInnerSize (&Patcher->MachContext) - mWrmsrMaxDistance;
//
// Thanks to Clover developers for the approach.
//
while (Walker < WalkerEnd) {
//
// Match (e)cx E2h assignment.
//
if ( (Walker[0] == mMovEcxE2[0])
&& (Walker[1] == mMovEcxE2[1])
&& (Walker[2] == mMovEcxE2[2])
&& (Walker[3] == mMovEcxE2[3])
&& (Walker[4] == mMovEcxE2[4]))
{
Walker += sizeof (mMovEcxE2);
} else if ( (Walker[0] == mMovCxE2[0])
&& (Walker[1] == mMovCxE2[1])
&& (Walker[2] == mMovCxE2[2])
&& (Walker[3] == mMovCxE2[3]))
{
Walker += sizeof (mMovCxE2);
} else {
++Walker;
continue;
}
WalkerTmp = Walker + mWrmsrMaxDistance;
while (Walker < WalkerTmp) {
if ( (Walker[0] == mWrmsr[0])
&& (Walker[1] == mWrmsr[1]))
{
++Count;
//
// Patch matched wrmsr with nop.
//
*Walker++ = 0x90;
*Walker++ = 0x90;
break;
}
if ( ((Walker[0] == 0xC9) && (Walker[1] == 0xC3)) ///< leave; ret
|| ((Walker[0] == 0x5D) && (Walker[1] == 0xC3))) ///< pop rbp; ret
//
// Stop searching upon matching return sequences.
//
{
Walker += 2;
break;
}
if ( ((Walker[0] == 0xB9) && (Walker[3] == 0) && (Walker[4] == 0)) ///< mov ecx, 00000xxxxh
|| ((Walker[0] == 0x66) && (Walker[1] == 0xB9) && (Walker[3] == 0))) ///< mov cx, 00xxh
//
// Stop searching upon matching reassign sequences.
//
{
break;
}
//
// Continue searching.
//
++Walker;
}
}
//
// At least one patch must be successful for this to work.
//
if (Count > 0) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Success AppleCpuPmCfgLock patch\n"));
return EFI_SUCCESS;
}
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply AppleCpuPmCfgLock patch\n"));
return EFI_NOT_FOUND;
}
#pragma pack(push, 1)
//
// XCPM record definition, extracted from XNU debug kernel.
//
typedef struct XCPM_MSR_RECORD_ {
UINT32 xcpm_msr_num;
UINT32 xcpm_msr_applicable_cpus;
UINT32 *xcpm_msr_flag_p;
UINT64 xcpm_msr_bits_clear;
UINT64 xcpm_msr_bits_set;
UINT64 xcpm_msr_initial_value;
UINT64 xcpm_msr_rb_value;
} XCPM_MSR_RECORD;
#pragma pack(pop)
STATIC
CONST UINT8
mXcpmCfgLockRelFind[] = {
0xB9, 0xE2, 0x00, 0x00, 0x00, ///< mov ecx, 0xE2
0x0F, 0x30 ///< wrmsr
};
STATIC
CONST UINT8
mXcpmCfgLockRelReplace[] = {
0xB9, 0xE2, 0x00, 0x00, 0x00, ///< mov ecx, 0xE2
0x90, 0x90 ///< nop
};
STATIC
PATCHER_GENERIC_PATCH
mXcpmCfgLockRelPatch = {
.Comment = DEBUG_POINTER ("XcpmCfgLockRel"),
.Base = "_xcpm_idle",
.Find = mXcpmCfgLockRelFind,
.Mask = NULL,
.Replace = mXcpmCfgLockRelReplace,
.ReplaceMask = NULL,
.Size = sizeof (mXcpmCfgLockRelFind),
.Count = 2,
.Skip = 0,
.Limit = 4096
};
STATIC
CONST UINT8
mXcpmCfgLockDbgFind[] = {
0xBF, 0xE2, 0x00, 0x00, 0x00, ///< mov edi, 0xE2
0xE8 ///< call (wrmsr64)
};
STATIC
CONST UINT8
mXcpmCfgLockDbgReplace[] = {
0xEB, 0x08, ///< jmp LBL ; Skip the 3x nops and 5x call
0x90, 0x90, 0x90, ///< nop
0xE8 ///< call (wrmsr64) ; Skipped by jmp LBL
};
STATIC
PATCHER_GENERIC_PATCH
mXcpmCfgLockDbgPatch = {
.Comment = DEBUG_POINTER ("XcpmCfgLockDbg"),
.Base = "_xcpm_cst_control_evaluate",
.Find = mXcpmCfgLockDbgFind,
.Mask = NULL,
.Replace = mXcpmCfgLockDbgReplace,
.ReplaceMask = NULL,
.Size = sizeof (mXcpmCfgLockDbgFind),
.Count = 2,
.Skip = 0,
.Limit = 4096
};
STATIC
EFI_STATUS
PatchAppleXcpmCfgLock (
IN OUT PATCHER_CONTEXT *Patcher,
IN UINT32 KernelVersion
)
{
EFI_STATUS Status;
XCPM_MSR_RECORD *Record;
XCPM_MSR_RECORD *Last;
UINT32 Replacements;
//
// This is a kernel patch, so Patcher cannot be NULL.
//
ASSERT (Patcher != NULL);
//
// XCPM is not available before macOS 10.8.5.
//
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION (KERNEL_VERSION_MOUNTAIN_LION, 5, 0), 0)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping XcpmCfgLock on %u\n", KernelVersion));
return EFI_SUCCESS;
}
Last = (XCPM_MSR_RECORD *)((UINT8 *)MachoGetMachHeader (&Patcher->MachContext)
+ MachoGetInnerSize (&Patcher->MachContext) - sizeof (XCPM_MSR_RECORD));
Replacements = 0;
Status = PatcherGetSymbolAddress (Patcher, "_xcpm_core_scope_msrs", (UINT8 **)&Record);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate _xcpm_core_scope_msrs for XcpmCfgLock patch - %r\n", Status));
return EFI_NOT_FOUND;
}
while (Record < Last) {
if (Record->xcpm_msr_num != 0xE2) {
break;
}
DEBUG ((
DEBUG_INFO,
"OCAK: Zeroing _xcpm_core_scope_msrs 0x%X applicable CPUs (%u)\n",
Record->xcpm_msr_num,
Record->xcpm_msr_applicable_cpus
));
Record->xcpm_msr_applicable_cpus = 0;
++Replacements;
++Record;
}
//
// Now the HWP patch at _xcpm_idle() for Release XNU.
//
Status = PatcherApplyGenericPatch (Patcher, &mXcpmCfgLockRelPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: Failed to apply _xcpm_idle release patch - %r, trying dbg\n", Status));
Status = PatcherApplyGenericPatch (Patcher, &mXcpmCfgLockDbgPatch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: Failed to apply dbg _xcpm_cst_control_evaluate patches - %r\n", Status));
}
}
if ((Replacements > 0) && !EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Success XcpmCfgLock patch\n"));
return EFI_SUCCESS;
}
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply XcpmCfgLock patch\n"));
return EFI_NOT_FOUND;
}
STATIC
CONST UINT8
mMiscPwrMgmtRelFind[] = {
0xB9, 0xAA, 0x01, 0x00, 0x00, ///< mov ecx, 0x1AA
0x0F, 0x30 ///< wrmsr
};
STATIC
CONST UINT8
mMiscPwrMgmtRelReplace[] = {
0xB9, 0xAA, 0x01, 0x00, 0x00, ///< mov ecx, 0x1AA
0x90, 0x90 ///< nop
};
STATIC
PATCHER_GENERIC_PATCH
mMiscPwrMgmtRelPatch = {
.Comment = DEBUG_POINTER ("MiscPwrMgmtRel"),
.Base = NULL,
.Find = mMiscPwrMgmtRelFind,
.Mask = NULL,
.Replace = mMiscPwrMgmtRelReplace,
.ReplaceMask = NULL,
.Size = sizeof (mMiscPwrMgmtRelFind),
.Count = 0,
.Skip = 0,
.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[] = {
0xBF, 0xAA, 0x01, 0x00, 0x00, ///< mov edi, 0x1AA
0xE8 ///< call (wrmsr64)
};
STATIC
CONST UINT8
mMiscPwrMgmtDbgReplace[] = {
0xEB, 0x08, ///< jmp LBL ; Skip the 3x nops and 5x call
0x90, 0x90, 0x90, ///< nop
0xE8 ///< call (wrmsr64) ; Skipped by jmp LBL
};
STATIC
PATCHER_GENERIC_PATCH
mMiscPwrMgmtDbgPatch = {
.Comment = DEBUG_POINTER ("MiscPwrMgmtDbg"),
.Base = NULL,
.Find = mMiscPwrMgmtDbgFind,
.Mask = NULL,
.Replace = mMiscPwrMgmtDbgReplace,
.ReplaceMask = NULL,
.Size = sizeof (mMiscPwrMgmtDbgFind),
.Count = 0,
.Skip = 0,
.Limit = 0
};
STATIC
EFI_STATUS
PatchAppleXcpmExtraMsrs (
IN OUT PATCHER_CONTEXT *Patcher,
IN UINT32 KernelVersion
)
{
EFI_STATUS Status;
XCPM_MSR_RECORD *Record;
XCPM_MSR_RECORD *Last;
UINT32 Replacements;
//
// This is a kernel patch, so Patcher cannot be NULL.
//
ASSERT (Patcher != NULL);
//
// XCPM is not available before macOS 10.8.5.
//
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION (KERNEL_VERSION_MOUNTAIN_LION, 5, 0), 0)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping XcpmExtraMsrs on %u\n", KernelVersion));
return EFI_SUCCESS;
}
Last = (XCPM_MSR_RECORD *)((UINT8 *)MachoGetMachHeader (&Patcher->MachContext)
+ MachoGetInnerSize (&Patcher->MachContext) - sizeof (XCPM_MSR_RECORD));
Replacements = 0;
Status = PatcherGetSymbolAddress (Patcher, "_xcpm_pkg_scope_msrs", (UINT8 **)&Record);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate _xcpm_pkg_scope_msrs for XcpmExtraMsrs patch - %r\n", Status));
return EFI_NOT_FOUND;
}
while (Record < Last) {
//
// Most Record->xcpm_msr_applicable_cpus has
// 0xDC or 0xDE in its lower 16-bit and thus here we
// AND 0xFF0000FDU in order to match both. (The result will be 0xDC)
//
if ((Record->xcpm_msr_applicable_cpus & 0xFF0000FDU) != 0xDC) {
break;
}
DEBUG ((
DEBUG_INFO,
"OCAK: Zeroing _xcpm_pkg_scope_msrs 0x%X applicable CPUs (%u)\n",
Record->xcpm_msr_num,
Record->xcpm_msr_applicable_cpus
));
Record->xcpm_msr_applicable_cpus = 0;
++Replacements;
++Record;
}
Status = PatcherGetSymbolAddress (Patcher, "_xcpm_SMT_scope_msrs", (UINT8 **)&Record);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate _xcpm_SMT_scope_msrs for XcpmExtraMsrs patch - %r\n", Status));
return EFI_NOT_FOUND;
}
while (Record < Last) {
if (Record->xcpm_msr_flag_p != NULL) {
break;
}
DEBUG ((
DEBUG_INFO,
"OCAK: Zeroing _xcpm_SMT_scope_msrs data 0x%X applicable CPUs (%u)\n",
Record->xcpm_msr_num,
Record->xcpm_msr_applicable_cpus
));
Record->xcpm_msr_applicable_cpus = 0;
++Replacements;
++Record;
}
//
// Now patch writes to MSR_MISC_PWR_MGMT.
//
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 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));
}
}
}
if ((Replacements > 0) && !EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Success XcpmExtraMsrs patch\n"));
return EFI_SUCCESS;
}
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply XcpmExtraMsrs patch\n"));
return EFI_NOT_FOUND;
}
STATIC
CONST UINT8
mPerfCtrlFind1[] = {
0xB9, 0x99, 0x01, 0x00, 0x00, ///< mov ecx, 0x199
0x0F, 0x30 ///< wrmsr
};
STATIC
CONST UINT8
mPerfCtrlFind2[] = {
0xB9, 0x99, 0x01, 0x00, 0x00, ///< mov ecx, 0x199
0x31, 0xD2, ///< xor edx, edx
0x0F, 0x30 ///< wrmsr
};
STATIC
CONST UINT8
mPerfCtrlFind3[] = {
0xB9, 0x99, 0x01, 0x00, 0x00, ///< mov ecx, 0x199
0x4C, 0x89, 0xF0, ///< mov rax, r14
0x0F, 0x30 ///< wrmsr
};
STATIC
CONST UINT8
mPerfCtrlFind4[] = {
0xB9, 0x99, 0x01, 0x00, 0x00, ///< mov ecx, 0x199
0x48, 0x89, 0xD8, ///< mov rax, rbx
0x0F, 0x30 ///< wrmsr
};
STATIC
CONST UINT8
mPerfCtrlMax[] = {
0xB9, 0x99, 0x01, 0x00, 0x00, ///< mov ecx, 0x199
0x31, 0xD2, ///< xor edx, edx
0xB8, 0x00, 0xFF, 0x00, 0x00, ///< mov eax, 0xFF00
0x0F, 0x30, ///< wrmsr
0xC3 ///< ret
};
STATIC
EFI_STATUS
PatchAppleXcpmForceBoost (
IN OUT PATCHER_CONTEXT *Patcher,
IN UINT32 KernelVersion
)
{
UINT8 *Start;
UINT8 *Last;
UINT8 *Current;
//
// This is a kernel patch, so Patcher cannot be NULL.
//
ASSERT (Patcher != NULL);
//
// XCPM is not available before macOS 10.8.5.
//
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION (KERNEL_VERSION_MOUNTAIN_LION, 5, 0), 0)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping XcpmForceBoost on %u\n", KernelVersion));
return EFI_SUCCESS;
}
Start = (UINT8 *)MachoGetMachHeader (&Patcher->MachContext);
Last = Start + MachoGetInnerSize (&Patcher->MachContext) - EFI_PAGE_SIZE * 2;
Start += EFI_PAGE_SIZE;
Current = Start;
while (Current < Last) {
//
// Compare <mov ecx, 0x199> in common.
//
if ( (Current[0] == mPerfCtrlFind1[0])
&& (Current[1] == mPerfCtrlFind1[1])
&& (Current[2] == mPerfCtrlFind1[2])
&& (Current[3] == mPerfCtrlFind1[3]))
{
if ( (CompareMem (&Current[4], &mPerfCtrlFind1[4], sizeof (mPerfCtrlFind1) - 4) == 0)
|| (CompareMem (&Current[4], &mPerfCtrlFind2[4], sizeof (mPerfCtrlFind2) - 4) == 0)
|| (CompareMem (&Current[4], &mPerfCtrlFind3[4], sizeof (mPerfCtrlFind3) - 4) == 0)
|| (CompareMem (&Current[4], &mPerfCtrlFind4[4], sizeof (mPerfCtrlFind4) - 4) == 0))
{
break;
}
}
++Current;
}
if (Current == Last) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate MSR_IA32_PERF_CONTROL write for XcpmForceBoost patch\n"));
return EFI_NOT_FOUND;
}
Start = Current - EFI_PAGE_SIZE;
Current -= 4;
while (Current >= Start) {
//
// Locate the beginning.
//
if ( (Current[0] == 0x55)
&& (Current[1] == 0x48)
&& (Current[2] == 0x89)
&& (Current[3] == 0xE5))
{
break;
}
--Current;
}
if (Current < Start) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate MSR_IA32_PERF_CONTROL prologue for XcpmForceBoost patch\n"));
return EFI_NOT_FOUND;
}
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch write max to MSR_IA32_PERF_CONTROL for XcpmForceBoost patch\n"));
CopyMem (Current, mPerfCtrlMax, sizeof (mPerfCtrlMax));
return EFI_SUCCESS;
}
STATIC
CONST UINT8
mRemoveUsbLimitV1Find[] = {
0xFF, 0xFF, 0x10
};
STATIC
CONST UINT8
mRemoveUsbLimitV1Replace[] = {
0xFF, 0xFF, 0x40
};
STATIC
PATCHER_GENERIC_PATCH
mRemoveUsbLimitV1Patch = {
.Comment = DEBUG_POINTER ("RemoveUsbLimitV1"),
.Base = "__ZN15AppleUSBXHCIPCI11createPortsEv",
.Find = mRemoveUsbLimitV1Find,
.Mask = NULL,
.Replace = mRemoveUsbLimitV1Replace,
.ReplaceMask = NULL,
.Size = sizeof (mRemoveUsbLimitV1Replace),
.Count = 1,
.Skip = 0,
.Limit = 8192
};
STATIC
CONST UINT8
mRemoveUsbLimitV2Find[] = {
0x0F, 0x0F, 0x83
};
STATIC
CONST UINT8
mRemoveUsbLimitV2Replace[] = {
0x40, 0x0F, 0x83
};
STATIC
PATCHER_GENERIC_PATCH
mRemoveUsbLimitV2Patch = {
.Comment = DEBUG_POINTER ("RemoveUsbLimitV2"),
.Base = "__ZN12AppleUSBXHCI11createPortsEv",
.Find = mRemoveUsbLimitV2Find,
.Mask = NULL,
.Replace = mRemoveUsbLimitV2Replace,
.ReplaceMask = NULL,
.Size = sizeof (mRemoveUsbLimitV2Replace),
.Count = 1,
.Skip = 0,
.Limit = 4096
};
STATIC
CONST UINT8
mRemoveUsbLimitIoP1Find1[] = {
0x0F, 0x0F, 0x87
};
STATIC
CONST UINT8
mRemoveUsbLimitIoP1Replace1[] = {
0x40, 0x0F, 0x87
};
STATIC
PATCHER_GENERIC_PATCH
mRemoveUsbLimitIoP1Patch1 = {
.Comment = DEBUG_POINTER ("RemoveUsbLimitIoP1 part 1"),
.Base = "__ZN16AppleUSBHostPort15setPortLocationEj",
.Find = mRemoveUsbLimitIoP1Find1,
.Mask = NULL,
.Replace = mRemoveUsbLimitIoP1Replace1,
.ReplaceMask = NULL,
.Size = sizeof (mRemoveUsbLimitIoP1Replace1),
.Count = 1,
.Skip = 0,
.Limit = 4096
};
STATIC
CONST UINT8
mRemoveUsbLimitIoP1Find2[] = {
0x41, 0x83, 0x00, 0x0F, ///< and whatever, 0x0Fh
0x41, 0xD3, 0x00, ///< shl whatever, cl
0x00, 0x09, 0x00 ///< or ebx, whatever
};
STATIC
CONST UINT8
mRemoveUsbLimitIoP1Mask2[] = {
0xFF, 0xFF, 0x00, 0xFF,
0xFF, 0xFF, 0x00,
0x00, 0xFF, 0x00
};
STATIC
CONST UINT8
mRemoveUsbLimitIoP1Replace2[] = {
0x00, 0x00, 0x00, 0x3F,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};
STATIC
CONST UINT8
mRemoveUsbLimitIoP1ReplaceMask2[] = {
0x00, 0x00, 0x00, 0xFF,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};
STATIC
PATCHER_GENERIC_PATCH
mRemoveUsbLimitIoP1Patch2 = {
.Comment = DEBUG_POINTER ("RemoveUsbLimitIoP1 part 2"),
.Base = "__ZN16AppleUSBHostPort15setPortLocationEj",
.Find = mRemoveUsbLimitIoP1Find2,
.Mask = mRemoveUsbLimitIoP1Mask2,
.Replace = mRemoveUsbLimitIoP1Replace2,
.ReplaceMask = mRemoveUsbLimitIoP1ReplaceMask2,
.Size = sizeof (mRemoveUsbLimitIoP1Replace2),
.Count = 1,
.Skip = 0,
.Limit = 4096
};
STATIC
EFI_STATUS
PatchUsbXhciPortLimit1 (
IN OUT PATCHER_CONTEXT *Patcher OPTIONAL,
IN UINT32 KernelVersion
)
{
EFI_STATUS Status;
//
// On 10.14.4 and newer IOUSBHostFamily also needs limit removal.
// Thanks to ydeng discovering this.
//
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION (KERNEL_VERSION_MOJAVE, 5, 0), 0)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping port patch IOUSBHostFamily on %u\n", KernelVersion));
return EFI_SUCCESS;
}
if (Patcher == NULL) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping %a on NULL Patcher on %u\n", __func__, KernelVersion));
return EFI_NOT_FOUND;
}
Status = PatcherApplyGenericPatch (Patcher, &mRemoveUsbLimitIoP1Patch1);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply port patch com.apple.iokit.IOUSBHostFamily part 1 - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success port com.apple.iokit.IOUSBHostFamily part 1\n"));
}
//
// The following patch is only needed on macOS 11.1 (Darwin 20.2.0) and above; skip it otherwise.
//
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION (KERNEL_VERSION_BIG_SUR, 2, 0), 0)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping port patch com.apple.iokit.IOUSBHostFamily part 2 on %u\n", KernelVersion));
return Status;
}
Status = PatcherApplyGenericPatch (Patcher, &mRemoveUsbLimitIoP1Patch2);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply port patch com.apple.iokit.IOUSBHostFamily part 2 - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success port com.apple.iokit.IOUSBHostFamily part 2\n"));
}
return Status;
}
STATIC
EFI_STATUS
PatchUsbXhciPortLimit2 (
IN OUT PATCHER_CONTEXT *Patcher OPTIONAL,
IN UINT32 KernelVersion
)
{
EFI_STATUS Status;
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_HIGH_SIERRA_MIN, 0)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping modern port patch AppleUSBXHCI on %u\n", KernelVersion));
return EFI_SUCCESS;
}
if (Patcher == NULL) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping %a on NULL Patcher on %u\n", __func__, KernelVersion));
return EFI_NOT_FOUND;
}
//
// TODO: Implement some locationID hack in IOUSBHostFamily.
// The location ID is a 32 bit number which is unique among all USB devices in the system,
// and which will not change on a system reboot unless the topology of the bus itself changes.
// See AppleUSBHostPort::setPortLocation():
// locationId = getLocationId();
// if (!(locationId & 0xF)) {
// int32_t shift = 20;
// while (locationId & (0xF << shift)) {
// shift -= 4;
// if (Shift < 0) { setLocationId(locationId); return; }
// }
// setLocationId(locationId | ((portNumber & 0xF) << shift));
// }
// The value (e.g. 0x14320000) is represented as follows: 0xAABCDEFG
// AA — Ctrl number 8 bits (e.g. 0x14, aka XHCI)
// B - Port number 4 bits (e.g. 0x3, aka SS03)
// C~F - Bus number 4 bits (e.g. 0x2, aka IOUSBHostHIDDevice)
//
// C~F are filled as many times as many USB Hubs are there on the port.
//
Status = PatcherApplyGenericPatch (Patcher, &mRemoveUsbLimitV2Patch);
if (!EFI_ERROR (Status)) {
//
// We do not need to patch com.apple.driver.usb.AppleUSBXHCI if this patch was successful.
// Only legacy systems require com.apple.driver.usb.AppleUSBXHCI to be patched.
//
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success com.apple.driver.usb.AppleUSBXHCI\n"));
} else {
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply patch com.apple.driver.usb.AppleUSBXHCI - %r\n", Status));
}
//
// TODO: Check when the patch changed actually.
//
if ( EFI_ERROR (Status)
&& OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_HIGH_SIERRA_MIN, KERNEL_VERSION_HIGH_SIERRA_MAX))
{
DEBUG ((DEBUG_INFO, "OCAK: [OK] Assuming success for AppleUSBXHCI on %u\n", KernelVersion));
return EFI_SUCCESS;
}
return Status;
}
STATIC
EFI_STATUS
PatchUsbXhciPortLimit3 (
IN OUT PATCHER_CONTEXT *Patcher OPTIONAL,
IN UINT32 KernelVersion
)
{
EFI_STATUS Status;
if (!OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_EL_CAPITAN_MIN, KERNEL_VERSION_HIGH_SIERRA_MAX)) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping legacy port patch AppleUSBXHCIPCI on %u\n", KernelVersion));
return EFI_SUCCESS;
}
if (Patcher == NULL) {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Skipping %a on NULL Patcher on %u\n", __func__, KernelVersion));
return EFI_NOT_FOUND;
}
//
// If we are here, we are on legacy 10.13 or below, try the oldest patch.
//
Status = PatcherApplyGenericPatch (Patcher, &mRemoveUsbLimitV1Patch);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_INFO, "OCAK: [FAIL] Failed to apply legacy port patch AppleUSBXHCIPCI - %r\n", Status));
} else {
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success apply legacy port AppleUSBXHCIPCI\n"));
}
//
// TODO: Check when the patch changed actually.
//
if ( EFI_ERROR (Status)
&& OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_HIGH_SIERRA_MIN, KERNEL_VERSION_HIGH_SIERRA_MAX))
{
DEBUG ((DEBUG_INFO, "OCAK: [OK] Assuming success for legacy port AppleUSBXHCIPCI on %u\n", KernelVersion));
return EFI_SUCCESS;
}
return Status;
}
STATIC
CONST UINT8
mIOAHCIBlockStoragePatchV1Find[] = {
0x41, 0x50, 0x50, 0x4C, 0x45, 0x20, 0x53, 0x53, 0x44, 0x00 ///< "APPLE SSD\0"
};
STATIC
CONST UINT8
mIOAHCIBlockStoragePatchV1Replace[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
STATIC
PATCHER_GENERIC_PATCH
mIOAHCIBlockStoragePatchV1 = {
.Comment = DEBUG_POINTER ("IOAHCIBlockStorageV1"),
.Base = NULL,
.Find = mIOAHCIBlockStoragePatchV1Find,
.Mask = NULL,
.Replace = mIOAHCIBlockStoragePatchV1Replace,
.ReplaceMask = NULL,
.Size = sizeof (mIOAHCIBlockStoragePatchV1Find),
.Count = 1,
.Skip = 0
};
STATIC
CONST UINT8
mIOAHCIBlockStoragePatchV2Find[] = {
0x41, 0x50, 0x50, 0x4C, 0x45, 0x00 ///< "APPLE\0"
};
STATIC
CONST UINT8
mIOAHCIBlockStoragePatchV2Replace[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
STATIC
PATCHER_GENERIC_PATCH
mIOAHCIBlockStoragePatchV2 = {
.Comment = DEBUG_POINTER ("IOAHCIBlockStorageV2"),
.Base = NULL,
.Find = mIOAHCIBlockStoragePatchV2Find,
.Mask = NULL,
.Replace = mIOAHCIBlockStoragePatchV2Replace,
.ReplaceMask = NULL,
.Size = sizeof (mIOAHCIBlockStoragePatchV2Find),
.Count = 1,
.Skip = 0
};
STATIC
CONST UINT8
mIOAHCIBlockStoragePatch133Find1[] = {
0x48, 0x8D, 0x3D, 0x00, 0x00, 0x00, 0x00,
0xBA, 0x09, 0x00, 0x00, 0x00
};
STATIC
CONST UINT8
mIOAHCIBlockStoragePatch133Find2[] = {