-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
CpuidPatches.c
2265 lines (2006 loc) · 71.5 KB
/
CpuidPatches.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
CPUID kernel patches.
Copyright (c) 2018-2020, vit9696, Goldfish64. 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>
//
// CPUID EAX=0x2 Cache descriptor values
//
STATIC
CONST struct {
UINT8 value;
cache_type_t type;
UINT32 size;
UINT8 associativity;
UINT8 linesize;
UINT8 partitions;
} mCpuCacheDescriptorValues[] = {
{ 0x06, L1I, SIZE_8KB, 4, 32, 1 },
{ 0x08, L1I, SIZE_16KB, 4, 32, 1 },
{ 0x0A, L1D, SIZE_8KB, 2, 32, 1 },
{ 0x0C, L1D, SIZE_16KB, 4, 32, 1 },
{ 0x0E, L1D, 24*SIZE_1KB, 6, 64, 1 },
{ 0x22, L3U, SIZE_512KB, 4, 64, 2 },
{ 0x23, L3U, SIZE_1MB, 8, 64, 2 },
{ 0x25, L3U, SIZE_2MB, 8, 64, 2 },
{ 0x29, L3U, SIZE_4MB, 8, 64, 2 },
{ 0x2C, L1D, SIZE_32KB, 8, 64, 2 },
{ 0x30, L1I, SIZE_32KB, 8, 64, 1 },
{ 0x41, L2U, SIZE_128KB, 4, 32, 1 },
{ 0x42, L2U, SIZE_256KB, 4, 32, 1 },
{ 0x43, L2U, SIZE_512KB, 4, 32, 1 },
{ 0x44, L2U, SIZE_1MB, 4, 32, 1 },
{ 0x45, L2U, SIZE_2MB, 4, 32, 1 },
{ 0x46, L3U, SIZE_4MB, 4, 64, 1 },
{ 0x47, L3U, SIZE_8MB, 8, 64, 1 },
{ 0x48, L2U, 3*SIZE_1MB, 12, 64, 1 },
{ 0x49, L2U, SIZE_4MB, 16, 64, 1 }, // for Xeons family Fh model 6h it's L3U instead
{ 0x4A, L3U, 6*SIZE_1MB, 12, 64, 1 },
{ 0x4B, L3U, SIZE_8MB, 16, 64, 1 },
{ 0x4C, L3U, 12*SIZE_1MB, 12, 64, 1 },
{ 0x4D, L3U, SIZE_16MB, 16, 64, 1 },
{ 0x4E, L2U, 6*SIZE_1MB, 24, 64, 1 },
{ 0x60, L1D, SIZE_16KB, 8, 64, 1 },
{ 0x66, L1D, SIZE_8KB, 4, 64, 1 },
{ 0x67, L1D, SIZE_16KB, 4, 64, 1 },
{ 0x68, L1D, SIZE_32KB, 4, 64, 1 },
{ 0x78, L2U, SIZE_1MB, 4, 64, 1 },
{ 0x79, L2U, SIZE_128KB, 8, 64, 2 },
{ 0x7A, L2U, SIZE_256KB, 8, 64, 2 },
{ 0x7B, L2U, SIZE_512KB, 8, 64, 2 },
{ 0x7C, L2U, SIZE_1MB, 8, 64, 2 },
{ 0x7D, L2U, SIZE_2MB, 8, 64, 1 },
{ 0x7F, L2U, SIZE_512KB, 2, 64, 1 },
{ 0x80, L2U, SIZE_512KB, 8, 64, 1 },
{ 0x82, L2U, SIZE_256KB, 8, 32, 1 },
{ 0x83, L2U, SIZE_512KB, 8, 32, 1 },
{ 0x84, L2U, SIZE_1MB, 8, 32, 1 },
{ 0x85, L2U, SIZE_2MB, 8, 32, 1 },
{ 0x86, L2U, SIZE_512KB, 4, 64, 1 },
{ 0x87, L2U, SIZE_1MB, 8, 64, 1 },
{ 0xD0, L3U, SIZE_512KB, 4, 64, 1 },
{ 0xD1, L3U, SIZE_1MB, 4, 64, 1 },
{ 0xD2, L3U, SIZE_2MB, 4, 64, 1 },
{ 0xD6, L3U, SIZE_1MB, 8, 64, 1 },
{ 0xD7, L3U, SIZE_2MB, 8, 64, 1 },
{ 0xD8, L3U, SIZE_4MB, 8, 64, 1 },
{ 0xDC, L3U, (UINT32)(1.5*SIZE_1MB), 12, 64, 1 },
{ 0xDD, L3U, 3*SIZE_1MB, 12, 64, 1 },
{ 0xDE, L3U, 6*SIZE_1MB, 12, 64, 1 },
{ 0xE2, L3U, SIZE_2MB, 16, 64, 1 },
{ 0xE3, L3U, SIZE_4MB, 16, 64, 1 },
{ 0xE4, L3U, SIZE_8MB, 16, 64, 1 },
{ 0xEA, L3U, 12*SIZE_1MB, 24, 64, 1 },
{ 0xEB, L3U, 18*SIZE_1MB, 24, 64, 1 },
{ 0xEC, L3U, 24*SIZE_1MB, 24, 64, 1 }
};
STATIC
CONST UINT8
mKernelCpuIdFindRelNew[] = {
0xB9, 0x8B, 0x00, 0x00, 0x00, // mov ecx, 8Bh
0x31, 0xC0, // xor eax, eax
0x31, 0xD2, // xor edx, edx
0x0F, 0x30, // wrmsr
0xB8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1
0x31, 0xDB, // xor ebx, ebx
0x31, 0xC9, // xor ecx, ecx
0x31, 0xD2, // xor edx, edx
0x0F, 0xA2 // cpuid
};
STATIC
CONST UINT8
mKernelCpuIdFindRelOld[] = {
0xB9, 0x8B, 0x00, 0x00, 0x00, // mov ecx, 8Bh
0x31, 0xD2, // xor edx, edx
0x0F, 0x30, // wrmsr
0xB8, 0x01, 0x00, 0x00, 0x00, // mov eax, 1
0x31, 0xDB, // xor ebx, ebx
0x31, 0xC9, // xor ecx, ecx
0x31, 0xD2, // xor edx, edx
0x0F, 0xA2 // cpuid
};
STATIC
CONST UINT8
mKernelCpuidFindMcRel[] = {
0xB9, 0x8B, 0x00, 0x00, 0x00, // mov ecx, 8Bh
0x0F, 0x32 // rdmsr
};
/**
cpu->cpuid_signature = 0x11111111;
cpu->cpuid_stepping = 0x22;
cpu->cpuid_model = 0x33;
cpu->cpuid_family = 0x44;
cpu->cpuid_type = 0x55555555;
cpu->cpuid_extmodel = 0x66;
cpu->cpuid_extfamily = 0x77;
cpu->cpuid_features = 0x8888888888888888;
cpu->cpuid_logical_per_package = 0x99999999;
cpu->cpuid_cpufamily = 0xAAAAAAAA;
return 0xAAAAAAAA;
**/
STATIC
CONST UINT8
mKernelCpuidReplaceDbg[] = {
0xC7, 0x47, 0x68, 0x11, 0x11, 0x11, 0x11, // mov dword ptr [rdi+68h], 11111111h
0xC6, 0x47, 0x50, 0x22, // mov byte ptr [rdi+50h], 22h
0x48, 0xB8, 0x55, 0x55, 0x55, 0x55, 0x44, 0x33, 0x66, 0x77, // mov rax, 7766334455555555h
0x48, 0x89, 0x47, 0x48, // mov [rdi+48h], rax
0x48, 0xB8, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, // mov rax, 8888888888888888h
0x48, 0x89, 0x47, 0x58, // mov [rdi+58h], rax
0xC7, 0x87, 0xCC, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, // mov dword ptr [rdi+0CCh], 99999999h
0xC7, 0x87, 0x88, 0x01, 0x00, 0x00, 0xAA, 0xAA, 0xAA, 0xAA, // mov dword ptr [rdi+188h], AAAAAAAAh
0xB8, 0xAA, 0xAA, 0xAA, 0xAA, // mov eax, AAAAAAAAh
0xC3 // retn
};
#pragma pack(push, 1)
typedef struct {
UINT8 Code1[3];
UINT32 Signature;
UINT8 Code2[3];
UINT8 Stepping;
UINT8 Code3[2];
UINT32 Type;
UINT8 Family;
UINT8 Model;
UINT8 ExtModel;
UINT8 ExtFamily;
UINT8 Code4[6];
UINT64 Features;
UINT8 Code5[10];
UINT32 LogicalPerPkg;
UINT8 Code6[6];
UINT32 AppleFamily1;
UINT8 Code7;
UINT32 AppleFamily2;
UINT8 Code8;
} INTERNAL_CPUID_FN_PATCH;
STATIC_ASSERT (
sizeof (INTERNAL_CPUID_FN_PATCH) == sizeof (mKernelCpuidReplaceDbg),
"Check your CPUID patch layout"
);
typedef struct {
UINT8 EaxCmd;
UINT32 EaxVal;
UINT8 EbxCmd;
UINT32 EbxVal;
UINT8 EcxCmd;
UINT32 EcxVal;
UINT8 EdxCmd;
UINT32 EdxVal;
} INTERNAL_CPUID_PATCH;
typedef struct {
UINT8 EdxCmd;
UINT32 EdxVal;
} INTERNAL_MICROCODE_PATCH;
#pragma pack(pop)
STATIC
EFI_STATUS
PatchKernelCpuIdLegacy (
IN OUT PATCHER_CONTEXT *Patcher,
IN UINT32 KernelVersion,
IN OC_CPU_INFO *CpuInfo,
IN UINT32 *Data,
IN UINT32 *DataMask,
IN UINT8 *Start,
IN UINT8 *Last
)
{
EFI_STATUS Status;
UINT8 *Record;
UINT8 *BlockClearFunc;
UINT8 *StartPointer;
UINT8 *EndPointer;
UINT32 StructAddr;
UINT8 *Location;
UINT8 *LocationEnd;
UINT8 *LocationSnow32;
UINT8 *LocationTigerTsc;
UINT8 *LocationTigerTscEnd;
UINT32 Signature[3];
BOOLEAN IsTiger;
BOOLEAN IsTigerOld;
BOOLEAN IsLeopard;
BOOLEAN IsSnow;
BOOLEAN IsLion;
UINT32 Index;
UINT32 MaxExt;
INT32 Delta;
INTERNAL_CPUID_PATCH Patch;
//
// XNU 8.10.1 and older require an additional patch to _tsc_init.
//
IsTiger = OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_TIGER_MIN, KERNEL_VERSION_TIGER_MAX);
IsTigerOld = OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_TIGER_MIN, KERNEL_VERSION (8, 10, 1));
IsLeopard = OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_LEOPARD_MIN, KERNEL_VERSION_LEOPARD_MAX);
IsSnow = OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_SNOW_LEOPARD_MIN, KERNEL_VERSION_SNOW_LEOPARD_MAX);
IsLion = OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_LION_MIN, KERNEL_VERSION_LION_MAX);
StructAddr = 0;
LocationSnow32 = NULL;
LocationTigerTsc = NULL;
LocationTigerTscEnd = NULL;
//
// Locate _cpuid_set_info or _cpuid_get_info.
// _cpuid_set_info is also patched in 10.4, and is located below _cpuid_get_info.
//
Status = PatcherGetSymbolAddress (Patcher, IsTiger ? "_cpuid_get_info" : "_cpuid_set_info", (UINT8 **)&Record);
if (EFI_ERROR (Status) || (Record >= Last)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate _cpuid_%a_info (%p) - %r\n", IsTiger ? "get" : "set", Record, Status));
return EFI_NOT_FOUND;
}
//
// Start of patch area.
//
// The existing code here will be replaced with a hardcoded vendor string (CPUID (0)).
// We'll also hardcode the CPUID (1) data here, jumped to from the next patch location area.
//
// 10.4 is in _cpuid_get_info.
// Others are in _cpuid_set_info.
//
// 10.4 has the struct in esi.
// 10.5 and 32-bit 10.6 have the struct hardcoded. This can be
// pulled right below the call to _blkclr, as below.
// 64-bit 10.6 has the struct in rdi, a bit before lea rsi... below.
// 32-bit 10.7 has the struct hardcoded, and can be pulled right
// before the call to _bzero
//
STATIC CONST UINT8 mKernelCpuidFindPatchTigerStart[4] = {
0x8D, 0x45, 0xEC, // lea eax, dword [...]
0xC7 // mov from mov dword [...], 4
};
STATIC CONST UINT8 mKernelCpuidFindPatchLeoSnowLionStruct32[2] = {
0x00, // 0 from end of mov dword [...], [struct address]
0xE8 // call from call _blkclr or _bzero
};
STATIC CONST UINT8 mKernelCpuidFindPatchLeoSnowLionStart32[5] = {
0x04, 0x00, 0x00, 0x00, // 4 from mov dword [...], 4
0xC7 // mov...
};
STATIC CONST UINT8 mKernelCpuidFindPatchSnowStart64[8] = {
0xBA, 0x04, 0x00, 0x00, 0x00, // mov edx, 4
0x48, 0x8D, 0x35 // lea rsi, ...
};
if (Patcher->Is32Bit) {
if (IsTiger) {
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindPatchTigerStart[0])
&& (Record[1] == mKernelCpuidFindPatchTigerStart[1])
&& (Record[2] == mKernelCpuidFindPatchTigerStart[2])
&& (Record[3] == mKernelCpuidFindPatchTigerStart[3]))
{
break;
}
}
} else {
//
// We need to get the address of _blkclr or _bzero first for proper matching.
//
Status = PatcherGetSymbolAddress (Patcher, IsLion ? "_bzero" : "_blkclr", (UINT8 **)&BlockClearFunc);
if (EFI_ERROR (Status) || (Record >= Last)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate %a (%p) - %r\n", IsLion ? "_bzero" : "_blkclr", Record, Status));
return EFI_NOT_FOUND;
}
//
// Get struct address first.
//
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindPatchLeoSnowLionStruct32[0])
&& (Record[1] == mKernelCpuidFindPatchLeoSnowLionStruct32[1])
&& (*((INT32 *)&Record[2]) == (INT32)(BlockClearFunc - (Record + sizeof (mKernelCpuidFindPatchLeoSnowLionStruct32) + sizeof (UINT32)))))
{
break;
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
StructAddr = *((UINT32 *)(Record - 3));
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindPatchLeoSnowLionStart32[0])
&& (Record[1] == mKernelCpuidFindPatchLeoSnowLionStart32[1])
&& (Record[2] == mKernelCpuidFindPatchLeoSnowLionStart32[2])
&& (Record[3] == mKernelCpuidFindPatchLeoSnowLionStart32[3])
&& (Record[4] == mKernelCpuidFindPatchLeoSnowLionStart32[4]))
{
break;
}
}
}
} else {
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindPatchSnowStart64[0])
&& (Record[1] == mKernelCpuidFindPatchSnowStart64[1])
&& (Record[2] == mKernelCpuidFindPatchSnowStart64[2])
&& (Record[3] == mKernelCpuidFindPatchSnowStart64[3])
&& (Record[4] == mKernelCpuidFindPatchSnowStart64[4])
&& (Record[5] == mKernelCpuidFindPatchSnowStart64[5])
&& (Record[6] == mKernelCpuidFindPatchSnowStart64[6])
&& (Record[7] == mKernelCpuidFindPatchSnowStart64[7]))
{
break;
}
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
if (Patcher->Is32Bit) {
StartPointer = IsTiger ? Record : Record - 4;
} else {
StartPointer = Record + sizeof (mKernelCpuidFindPatchSnowStart64) + sizeof (UINT32);
}
//
// End of patch area.
//
STATIC CONST UINT8 mKernelCpuidFindPatchTigerEnd[4] = {
0x00, // 0 from mov byte [...], 0
0x31, 0xDB, // xor ebx, ebx
0x8B // mov ...
};
STATIC UINT8 mKernelCpuidFindPatchSnowLionEnd32[7] = {
0xC6, 0x05, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 // mov byte [struct_addr], 0
};
STATIC UINT32 *mKernelCpuidFindPatchSnowLionEndPtr32 = (UINT32 *)&mKernelCpuidFindPatchSnowLionEnd32[2];
*mKernelCpuidFindPatchSnowLionEndPtr32 = StructAddr + sizeof (Signature[0]) * 3;
STATIC CONST UINT8 mKernelCpuidFindPatchLeoEnd1[5] = {
0xB8, 0x00, 0x00, 0x00, 0x80 // mov eax/edx, 80000000h
};
STATIC CONST UINT8 mKernelCpuidFindPatchLeoEnd1Mask = 0xFD;
if (IsTiger) {
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindPatchTigerEnd[0])
&& (Record[1] == mKernelCpuidFindPatchTigerEnd[1])
&& (Record[2] == mKernelCpuidFindPatchTigerEnd[2])
&& (Record[3] == mKernelCpuidFindPatchTigerEnd[3]))
{
break;
}
}
} else if ((IsSnow || IsLion) && Patcher->Is32Bit) {
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindPatchSnowLionEnd32[0])
&& (Record[1] == mKernelCpuidFindPatchSnowLionEnd32[1])
&& (Record[2] == mKernelCpuidFindPatchSnowLionEnd32[2])
&& (Record[3] == mKernelCpuidFindPatchSnowLionEnd32[3])
&& (Record[4] == mKernelCpuidFindPatchSnowLionEnd32[4])
&& (Record[5] == mKernelCpuidFindPatchSnowLionEnd32[5])
&& (Record[6] == mKernelCpuidFindPatchSnowLionEnd32[6]))
{
break;
}
}
} else {
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( ((Record[0] & mKernelCpuidFindPatchLeoEnd1Mask) == mKernelCpuidFindPatchLeoEnd1[0])
&& (Record[1] == mKernelCpuidFindPatchLeoEnd1[1])
&& (Record[2] == mKernelCpuidFindPatchLeoEnd1[2])
&& (Record[3] == mKernelCpuidFindPatchLeoEnd1[3])
&& (Record[4] == mKernelCpuidFindPatchLeoEnd1[4]))
{
break;
}
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
if (IsSnow && !Patcher->Is32Bit) {
STATIC CONST UINT8 mKernelCpuidFindPatchLeoEnd2[3] = {
0x0F, 0xA2, // cpuid
0x89 // mov ...
};
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindPatchLeoEnd2[0])
&& (Record[1] == mKernelCpuidFindPatchLeoEnd2[1])
&& (Record[2] == mKernelCpuidFindPatchLeoEnd2[2]))
{
break;
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
}
EndPointer = IsTiger ? Record - 3 : Record;
//
// Start of CPUID location.
//
// We'll replace this with a call to the previous patched section to
// populate the CPUID (1) info.
//
// 32-bit 10.6 has two different mov eax, 0x1 pairs for 32-bit and 64-bit.
// The first is patched out with nops, and we'll use the second for the jmp.
//
// 32-bit 10.7 has a call to a function that is very similar to 32-bit 10.6, and calls
// cpuid in 64-bit mode if supported. We can simply replace this call with one to the patched area.
//
STATIC CONST UINT8 mKernelCpuidFindLocLeoTigerStart[7] = {
0xB9, 0x01, 0x00, 0x00, 0x00, // mov ecx, 1
0x89, 0xC8 // mov eax, ecx
};
STATIC CONST UINT8 mKernelCpuidFindLocSnowStart[5] = {
0xB8, 0x01, 0x00, 0x00, 0x00 // mov eax, 1
};
STATIC CONST UINT8 mKernelCpuidFindLocSnowStart32[6] = {
0xE8, 0xFF, 0xFF, 0xFF, 0xFF, // call _cpuid64
0xEB // jmp ...
};
STATIC CONST UINT8 mKernelCpuidFindLocLionStart32[8] = {
0xB9, 0x01, 0x00, 0x00, 0x00, // mov ecx, 1
0x8D, 0x55, 0xD0 // lea edx, dword [...]
};
if (IsTiger || IsLeopard) {
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindLocLeoTigerStart[0])
&& (Record[1] == mKernelCpuidFindLocLeoTigerStart[1])
&& (Record[2] == mKernelCpuidFindLocLeoTigerStart[2])
&& (Record[3] == mKernelCpuidFindLocLeoTigerStart[3])
&& (Record[4] == mKernelCpuidFindLocLeoTigerStart[4])
&& (Record[5] == mKernelCpuidFindLocLeoTigerStart[5])
&& (Record[6] == mKernelCpuidFindLocLeoTigerStart[6]))
{
break;
}
}
} else if (IsSnow) {
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindLocSnowStart[0])
&& (Record[1] == mKernelCpuidFindLocSnowStart[1])
&& (Record[2] == mKernelCpuidFindLocSnowStart[2])
&& (Record[3] == mKernelCpuidFindLocSnowStart[3])
&& (Record[4] == mKernelCpuidFindLocSnowStart[4]))
{
break;
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
//
// Find where call _cpuid64 is located.
// We'll then look for the second mov eax, 0x1 after that.
//
if (Patcher->Is32Bit) {
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindLocSnowStart32[0])
&& (Record[5] == mKernelCpuidFindLocSnowStart32[5]))
{
break;
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
LocationSnow32 = Record;
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindLocSnowStart[0])
&& (Record[1] == mKernelCpuidFindLocSnowStart[1])
&& (Record[2] == mKernelCpuidFindLocSnowStart[2])
&& (Record[3] == mKernelCpuidFindLocSnowStart[3])
&& (Record[4] == mKernelCpuidFindLocSnowStart[4]))
{
break;
}
}
}
} else {
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindLocLionStart32[0])
&& (Record[1] == mKernelCpuidFindLocLionStart32[1])
&& (Record[2] == mKernelCpuidFindLocLionStart32[2])
&& (Record[3] == mKernelCpuidFindLocLionStart32[3])
&& (Record[4] == mKernelCpuidFindLocLionStart32[4])
&& (Record[5] == mKernelCpuidFindLocLionStart32[5])
&& (Record[6] == mKernelCpuidFindLocLionStart32[6])
&& (Record[7] == mKernelCpuidFindLocLionStart32[7]))
{
break;
}
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
Location = IsLion ? Record + sizeof (mKernelCpuidFindLocLionStart32) : Record;
//
// End of CPUID location. Not applicable to 10.7.
//
STATIC CONST UINT8 mKernelCpuidFindLegacyLocEnd[3] = {
0x0F, 0xA2, // cpuid
0x89 // mov ...
};
if (!IsLion) {
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindLegacyLocEnd[0])
&& (Record[1] == mKernelCpuidFindLegacyLocEnd[1])
&& (Record[2] == mKernelCpuidFindLegacyLocEnd[2]))
{
break;
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
LocationEnd = Record + 2;
} else {
LocationEnd = Location;
}
//
// Locate _tsc_init on 10.4, as there is a CPUID (1) call that needs to be patched.
// This only applies to XNU 8.10.1 and older. Some recovery versions of
// 10.4.10 have a newer XNU 8.10.3 kernel with code changes to _tsc_init.
//
// It's possible 8.10.2 may require the patch, but there are no sources or kernels
// available to verify.
//
if (IsTigerOld) {
Status = PatcherGetSymbolAddress (Patcher, "_tsc_init", (UINT8 **)&Record);
if (EFI_ERROR (Status) || (Record >= Last)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate _tsc_init (%p) - %r\n", Record, Status));
return EFI_NOT_FOUND;
}
//
// Start of _tsc_init CPUID location.
//
// We'll replace this with a call to the previous patched section to
// populate the CPUID (1) info.
//
STATIC CONST UINT8 mKernelCpuidFindTscLocTigerStart[7] = {
0xBA, 0x01, 0x00, 0x00, 0x00, // mov edx, 1
0x89, 0xD0 // mov eax, edx
};
for (Index = 0; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindTscLocTigerStart[0])
&& (Record[1] == mKernelCpuidFindTscLocTigerStart[1])
&& (Record[2] == mKernelCpuidFindTscLocTigerStart[2])
&& (Record[3] == mKernelCpuidFindTscLocTigerStart[3])
&& (Record[4] == mKernelCpuidFindTscLocTigerStart[4])
&& (Record[5] == mKernelCpuidFindTscLocTigerStart[5])
&& (Record[6] == mKernelCpuidFindTscLocTigerStart[6]))
{
break;
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
LocationTigerTsc = Record;
//
// End of _tsc_init CPUID location.
//
for ( ; Index < EFI_PAGE_SIZE; Index++, Record++) {
if ( (Record[0] == mKernelCpuidFindLegacyLocEnd[0])
&& (Record[1] == mKernelCpuidFindLegacyLocEnd[1])
&& (Record[2] == mKernelCpuidFindLegacyLocEnd[2]))
{
break;
}
}
if (Index >= EFI_PAGE_SIZE) {
return EFI_NOT_FOUND;
}
LocationTigerTscEnd = Record + 2;
}
DEBUG ((
DEBUG_INFO,
"OCAK: Legacy CPUID patch %p:%p, loc %p:%p, tsc loc %p:%p, struct @ 0x%X\n",
StartPointer - Start,
EndPointer - Start,
Location - Start,
LocationEnd - Start,
IsTigerOld ? LocationTigerTsc - Start : 0,
IsTigerOld ? LocationTigerTscEnd - Start : 0,
StructAddr
));
//
// Free 2 more bytes in the end by assigning EAX directly.
//
if (IsSnow && !Patcher->Is32Bit) {
AsmCpuid (0x80000000, &MaxExt, NULL, NULL, NULL);
EndPointer[0] = 0xB8;
CopyMem (&EndPointer[1], &MaxExt, sizeof (MaxExt));
}
//
// Short-write CPU signature to create space for CPUID (1) patch.
//
// 29 bytes.
//
AsmCpuid (0, NULL, &Signature[0], &Signature[2], &Signature[1]);
for (Index = 0; Index < 3; ++Index) {
//
// mov eax, <signature>
//
*StartPointer++ = 0xB8;
CopyMem (StartPointer, &Signature[Index], sizeof (Signature[0]));
StartPointer += sizeof (Signature[0]);
if (IsLeopard || ((IsSnow || IsLion) && Patcher->Is32Bit)) {
//
// mov dword [struct_addr], eax
//
*StartPointer++ = 0xA3;
CopyMem (StartPointer, &StructAddr, sizeof (StructAddr));
StartPointer += sizeof (StructAddr);
StructAddr += sizeof (Signature[0]);
} else if (IsTiger) {
//
// mov [esi+offset], eax
//
*StartPointer++ = 0x89;
*StartPointer++ = 0x46;
*StartPointer++ = (UINT8)(Index * sizeof (Signature[0]));
} else {
//
// mov [rsi], eax
//
*StartPointer++ = 0x89;
*StartPointer++ = 0x06;
if (Index < 2) {
//
// add rsi, 4
//
*StartPointer++ = 0x48;
*StartPointer++ = 0x83;
*StartPointer++ = 0xC6;
*StartPointer++ = 0x04;
}
}
}
//
// Ensure that we still have room, which is within 2-byte jmp (127)
// and has at least 2-byte jmp and patch + 1-byte ret.
//
if ( (StartPointer >= EndPointer)
|| (EndPointer - StartPointer > 128)
|| ((UINTN)(EndPointer - StartPointer) < sizeof (INTERNAL_CPUID_PATCH) + 3))
{
return EFI_OUT_OF_RESOURCES;
}
//
// Short jmp to EndPointer.
//
StartPointer[0] = 0xEB;
StartPointer[1] = (UINT8)(EndPointer - StartPointer - 2);
StartPointer += 2;
//
// Workaround incorrect OSXSAVE handling, see below.
//
if ( (CpuInfo->CpuidVerEcx.Bits.XSAVE != 0)
&& (CpuInfo->CpuidVerEcx.Bits.OSXSAVE == 0)
&& (CpuInfo->CpuidVerEcx.Bits.AVX != 0))
{
CpuInfo->CpuidVerEcx.Bits.OSXSAVE = 1;
}
//
// NOP out call _cpuid64 in 32-bit 10.6.
//
if (IsSnow && Patcher->Is32Bit) {
while (LocationSnow32 < LocationEnd) {
*LocationSnow32++ = 0x90;
}
}
//
// Call from location to patch area.
//
Delta = (INT32)(StartPointer - (Location + 5));
*Location++ = 0xE8;
CopyMem (Location, &Delta, sizeof (Delta));
Location += sizeof (Delta);
while (Location < LocationEnd) {
*Location++ = 0x90;
}
//
// In 10.4, we need to replace a call to CPUID (1) with a call to
// the patch area like above in _tsc_init.
//
if (IsTigerOld) {
Delta = (INT32)(StartPointer - (LocationTigerTsc + 5));
*LocationTigerTsc++ = 0xE8;
CopyMem (LocationTigerTsc, &Delta, sizeof (Delta));
LocationTigerTsc += sizeof (Delta);
while (LocationTigerTsc < LocationTigerTscEnd) {
*LocationTigerTsc++ = 0x90;
}
}
//
// Write virtualised CPUID.
// 20 bytes.
// 10.7 requires the registers to be copied to a memory location in EDX, so we'll use ESI instead.
//
Patch.EaxCmd = 0xB8;
Patch.EaxVal = (Data[0] & DataMask[0]) | (CpuInfo->CpuidVerEax.Uint32 & ~DataMask[0]);
Patch.EbxCmd = 0xBB;
Patch.EbxVal = (Data[1] & DataMask[1]) | (CpuInfo->CpuidVerEbx.Uint32 & ~DataMask[1]);
Patch.EcxCmd = 0xB9;
Patch.EcxVal = (Data[2] & DataMask[2]) | (CpuInfo->CpuidVerEcx.Uint32 & ~DataMask[2]);
Patch.EdxCmd = IsLion ? 0xBE : 0xBA;
Patch.EdxVal = (Data[3] & DataMask[3]) | (CpuInfo->CpuidVerEdx.Uint32 & ~DataMask[3]);
CopyMem (StartPointer, &Patch, sizeof (Patch));
StartPointer += sizeof (Patch);
//
// Under 10.7, we need to copy registers to memory in EDX.
//
if (IsLion) {
//
// mov [edx], eax
//
*StartPointer++ = 0x89;
*StartPointer++ = 0x02;
//
// mov [edx+4], ebx
//
*StartPointer++ = 0x89;
*StartPointer++ = 0x5A;
*StartPointer++ = 0x04;
//
// mov [edx+8], ecx
//
*StartPointer++ = 0x89;
*StartPointer++ = 0x4A;
*StartPointer++ = 0x08;
//
// mov [edx+12], esi
//
*StartPointer++ = 0x89;
*StartPointer++ = 0x72;
*StartPointer++ = 0x0C;
}
//
// Return to the end of location.
//
*StartPointer++ = 0xC3;
DEBUG ((DEBUG_INFO, "OCAK: [OK] Legacy CPUID patch completed @ %p\n", StartPointer - Start));
return EFI_SUCCESS;
}
EFI_STATUS
PatchKernelCpuId (
IN OUT PATCHER_CONTEXT *Patcher,
IN OC_CPU_INFO *CpuInfo,
IN UINT32 *Data,
IN UINT32 *DataMask,
IN UINT32 KernelVersion
)
{
EFI_STATUS Status;
UINT8 *CpuidSetInfo;
UINT8 *Record;
UINT8 *Start;
UINT8 *Last;
UINT32 Index;
UINT32 FoundSize;
INTERNAL_CPUID_PATCH *CpuidPatch;
INTERNAL_MICROCODE_PATCH *McPatch;
INTERNAL_CPUID_FN_PATCH *FnPatch;
CPUID_VERSION_INFO_EAX Eax;
CPUID_VERSION_INFO_EBX Ebx;
CPUID_VERSION_INFO_ECX Ecx;
CPUID_VERSION_INFO_EDX Edx;
BOOLEAN FoundReleaseKernel;
ASSERT (Patcher != NULL);
STATIC_ASSERT (
sizeof (mKernelCpuIdFindRelNew) > sizeof (mKernelCpuIdFindRelOld),
"Kernel CPUID patch seems wrong"
);
ASSERT (
mKernelCpuIdFindRelNew[0] == mKernelCpuIdFindRelOld[0]
&& mKernelCpuIdFindRelNew[1] == mKernelCpuIdFindRelOld[1]
&& mKernelCpuIdFindRelNew[2] == mKernelCpuIdFindRelOld[2]
&& mKernelCpuIdFindRelNew[3] == mKernelCpuIdFindRelOld[3]
);
Start = ((UINT8 *)MachoGetMachHeader (&Patcher->MachContext));
Last = Start + MachoGetInnerSize (&Patcher->MachContext) - EFI_PAGE_SIZE * 2 - sizeof (mKernelCpuIdFindRelNew);
//
// Do legacy patching for 32-bit 10.7, and 10.6 and older.
//
if ( (OcMatchDarwinVersion (KernelVersion, KERNEL_VERSION_LION_MIN, KERNEL_VERSION_LION_MAX) && Patcher->Is32Bit)
|| OcMatchDarwinVersion (KernelVersion, 0, KERNEL_VERSION_SNOW_LEOPARD_MAX))
{
Status = PatchKernelCpuIdLegacy (Patcher, KernelVersion, CpuInfo, Data, DataMask, Start, Last);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to patch legacy CPUID - %r\n", Status));
}
return Status;
}
Status = PatcherGetSymbolAddress (Patcher, "_cpuid_set_info", (UINT8 **)&CpuidSetInfo);
if (EFI_ERROR (Status) || (CpuidSetInfo >= Last)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate _cpuid_set_info (%p) - %r\n", CpuidSetInfo, Status));
return EFI_NOT_FOUND;
}
Record = CpuidSetInfo;
FoundSize = 0;
for (Index = 0; Index < EFI_PAGE_SIZE; ++Index, ++Record) {
if ( (Record[0] == mKernelCpuIdFindRelNew[0])
&& (Record[1] == mKernelCpuIdFindRelNew[1])
&& (Record[2] == mKernelCpuIdFindRelNew[2])
&& (Record[3] == mKernelCpuIdFindRelNew[3]))
{
if (CompareMem (Record, mKernelCpuIdFindRelNew, sizeof (mKernelCpuIdFindRelNew)) == 0) {
FoundSize = sizeof (mKernelCpuIdFindRelNew);
break;
} else if (CompareMem (Record, mKernelCpuIdFindRelOld, sizeof (mKernelCpuIdFindRelOld)) == 0) {
FoundSize = sizeof (mKernelCpuIdFindRelOld);
break;
}
}
}
FoundReleaseKernel = FoundSize > 0;
//
// When patching the release kernel we do not allow reevaluating CPUID information,
// which is used to report OSXSAVE availability. This causes issues with some programs,
// like Docker using Hypervisor.framework, which rely on sysctl to track CPU feature.
//
// To workaround this we make sure to always report OSXSAVE bit when it is available
// regardless of the reevaluation performed by init_fpu in XNU.
//
// REF: https://github.com/acidanthera/bugtracker/issues/1035
//
if ( FoundReleaseKernel
&& (CpuInfo->CpuidVerEcx.Bits.XSAVE != 0)
&& (CpuInfo->CpuidVerEcx.Bits.OSXSAVE == 0)
&& (CpuInfo->CpuidVerEcx.Bits.AVX != 0))
{
CpuInfo->CpuidVerEcx.Bits.OSXSAVE = 1;
}
Eax.Uint32 = (Data[0] & DataMask[0]) | (CpuInfo->CpuidVerEax.Uint32 & ~DataMask[0]);
Ebx.Uint32 = (Data[1] & DataMask[1]) | (CpuInfo->CpuidVerEbx.Uint32 & ~DataMask[1]);
Ecx.Uint32 = (Data[2] & DataMask[2]) | (CpuInfo->CpuidVerEcx.Uint32 & ~DataMask[2]);
Edx.Uint32 = (Data[3] & DataMask[3]) | (CpuInfo->CpuidVerEdx.Uint32 & ~DataMask[3]);
if (FoundReleaseKernel) {
CpuidPatch = (INTERNAL_CPUID_PATCH *)Record;
CpuidPatch->EaxCmd = 0xB8;
CpuidPatch->EaxVal = Eax.Uint32;
CpuidPatch->EbxCmd = 0xBB;
CpuidPatch->EbxVal = Ebx.Uint32;
CpuidPatch->EcxCmd = 0xB9;
CpuidPatch->EcxVal = Ecx.Uint32;
CpuidPatch->EdxCmd = 0xBA;
CpuidPatch->EdxVal = Edx.Uint32;
SetMem (
Record + sizeof (INTERNAL_CPUID_PATCH),
FoundSize - sizeof (INTERNAL_CPUID_PATCH),
0x90
);
Record += FoundSize;
for (Index = 0; Index < EFI_PAGE_SIZE - sizeof (mKernelCpuidFindMcRel); ++Index, ++Record) {
if (CompareMem (Record, mKernelCpuidFindMcRel, sizeof (mKernelCpuidFindMcRel)) == 0) {
McPatch = (INTERNAL_MICROCODE_PATCH *)Record;
McPatch->EdxCmd = 0xBA;
McPatch->EdxVal = CpuInfo->MicrocodeRevision;
SetMem (
Record + sizeof (INTERNAL_MICROCODE_PATCH),
sizeof (mKernelCpuidFindMcRel) - sizeof (INTERNAL_MICROCODE_PATCH),
0x90
);
DEBUG ((DEBUG_INFO, "OCAK: [OK] Patch success CPUID release\n"));
return EFI_SUCCESS;
}
}
} else {
//
// Handle debug kernel here...
//
Status = PatcherGetSymbolAddress (Patcher, "_cpuid_set_cpufamily", (UINT8 **)&Record);
if (EFI_ERROR (Status) || (Record >= Last)) {
DEBUG ((DEBUG_WARN, "OCAK: [FAIL] Failed to locate _cpuid_set_cpufamily (%p) - %r\n", Record, Status));
return EFI_NOT_FOUND;
}
CopyMem (Record, mKernelCpuidReplaceDbg, sizeof (mKernelCpuidReplaceDbg));
FnPatch = (INTERNAL_CPUID_FN_PATCH *)Record;
FnPatch->Signature = Eax.Uint32;