/
kern_igfx.cpp
2091 lines (1770 loc) · 94.8 KB
/
kern_igfx.cpp
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
//
// kern_igfx.cpp
// WhateverGreen
//
// Copyright © 2018 vit9696. All rights reserved.
//
#include "kern_igfx.hpp"
#include "kern_fb.hpp"
#include "kern_guc.hpp"
#include "kern_agdc.hpp"
#include "kern_igfx_kexts.hpp"
#include <Headers/kern_api.hpp>
#include <Headers/kern_cpu.hpp>
#include <Headers/kern_file.hpp>
#include <Headers/kern_iokit.hpp>
#include <IOKit/pci/IOPCIDevice.h>
IGFX *IGFX::callbackIGFX;
void IGFX::init() {
callbackIGFX = this;
// Initialize each submodule
for (auto submodule : submodules)
submodule->init();
for (auto submodule : sharedSubmodules)
submodule->init();
auto &bdi = BaseDeviceInfo::get();
auto generation = bdi.cpuGeneration;
auto family = bdi.cpuFamily;
auto model = bdi.cpuModel;
switch (generation) {
case CPUInfo::CpuGeneration::Penryn:
case CPUInfo::CpuGeneration::Nehalem:
// Do not warn about legacy processors (e.g. Xeon).
break;
case CPUInfo::CpuGeneration::Westmere:
currentGraphics = &kextIntelHD;
currentFramebuffer = &kextIntelHDFb;
break;
case CPUInfo::CpuGeneration::SandyBridge: {
int tmp = 1;
PE_parse_boot_argn("igfxsnb", &tmp, sizeof(tmp));
moderniseAccelerator = tmp == 1;
currentGraphics = &kextIntelHD3000;
currentFramebuffer = &kextIntelSNBFb;
break;
}
case CPUInfo::CpuGeneration::IvyBridge:
currentGraphics = &kextIntelHD4000;
currentFramebuffer = &kextIntelCapriFb;
break;
case CPUInfo::CpuGeneration::Haswell:
currentGraphics = &kextIntelHD5000;
currentFramebuffer = &kextIntelAzulFb;
break;
case CPUInfo::CpuGeneration::Broadwell:
currentGraphics = &kextIntelBDW;
currentFramebuffer = &kextIntelBDWFb;
break;
case CPUInfo::CpuGeneration::Skylake:
// Fake SKL as KBL on 13.0+ due to the removal of SKL kexts
// Or KBL kext can be used on SKL with older versions as well with KBL `device-id' and `ig-platform-id' injected.
forceSKLAsKBL = getKernelVersion() >= KernelVersion::Ventura || checkKernelArgument("-igfxsklaskbl");
if (forceSKLAsKBL) {
DBGLOG("igfx", "enforcing KBL kexts and patches on Skylake");
supportsGuCFirmware = true;
currentGraphics = &kextIntelKBL;
currentFramebuffer = &kextIntelKBLFb;
modForceCompleteModeset.supported = modForceCompleteModeset.enabled = true;
modRPSControlPatch.available = true;
modForceWakeWorkaround.enabled = true;
modTypeCCheckDisabler.enabled = getKernelVersion() >= KernelVersion::BigSur;
modBlackScreenFix.available = true;
} else {
supportsGuCFirmware = true;
currentGraphics = &kextIntelSKL;
currentFramebuffer = &kextIntelSKLFb;
modForceCompleteModeset.supported = modForceCompleteModeset.legacy = true; // not enabled, as on legacy operating systems it casues crashes.
modTypeCCheckDisabler.enabled = getKernelVersion() >= KernelVersion::BigSur;
modBlackScreenFix.available = true;
}
break;
case CPUInfo::CpuGeneration::KabyLake:
supportsGuCFirmware = true;
currentGraphics = &kextIntelKBL;
currentFramebuffer = &kextIntelKBLFb;
modForceCompleteModeset.supported = modForceCompleteModeset.enabled = true;
modRPSControlPatch.available = true;
modForceWakeWorkaround.enabled = true;
modTypeCCheckDisabler.enabled = getKernelVersion() >= KernelVersion::BigSur;
modBlackScreenFix.available = true;
break;
case CPUInfo::CpuGeneration::CoffeeLake:
supportsGuCFirmware = true;
currentGraphics = &kextIntelKBL;
currentFramebuffer = &kextIntelCFLFb;
// Allow faking ask KBL
currentFramebufferOpt = &kextIntelKBLFb;
// Note, several CFL GPUs are completely broken. They freeze in IGMemoryManager::initCache due to incompatible
// configuration, supposedly due to Apple not supporting new MOCS table and forcing Skylake-based format.
// See: https://github.com/torvalds/linux/blob/135c5504a600ff9b06e321694fbcac78a9530cd4/drivers/gpu/drm/i915/intel_mocs.c#L181
modForceCompleteModeset.supported = modForceCompleteModeset.enabled = true;
modRPSControlPatch.available = true;
modForceWakeWorkaround.enabled = true;
modTypeCCheckDisabler.enabled = true;
modBlackScreenFix.available = true;
break;
case CPUInfo::CpuGeneration::CannonLake:
supportsGuCFirmware = true;
currentGraphics = &kextIntelCNL;
currentFramebuffer = &kextIntelCNLFb;
modForceCompleteModeset.supported = modForceCompleteModeset.enabled = true;
modTypeCCheckDisabler.enabled = true;
break;
case CPUInfo::CpuGeneration::IceLake:
supportsGuCFirmware = true;
currentGraphics = &kextIntelICL;
currentFramebuffer = &kextIntelICLLPFb;
currentFramebufferOpt = &kextIntelICLHPFb;
modDVMTCalcFix.available = true;
break;
case CPUInfo::CpuGeneration::CometLake:
supportsGuCFirmware = true;
currentGraphics = &kextIntelKBL;
currentFramebuffer = &kextIntelCFLFb;
// Allow faking ask KBL
currentFramebufferOpt = &kextIntelKBLFb;
// Note, several CFL GPUs are completely broken. They freeze in IGMemoryManager::initCache due to incompatible
// configuration, supposedly due to Apple not supporting new MOCS table and forcing Skylake-based format.
// See: https://github.com/torvalds/linux/blob/135c5504a600ff9b06e321694fbcac78a9530cd4/drivers/gpu/drm/i915/intel_mocs.c#L181
modForceCompleteModeset.supported = modForceCompleteModeset.enabled = true;
modRPSControlPatch.available = true;
modTypeCCheckDisabler.enabled = true;
modBlackScreenFix.available = true;
break;
case CPUInfo::CpuGeneration::RocketLake:
gPlatformGraphicsSupported = false;
break;
default:
SYSLOG("igfx", "found an unsupported processor 0x%X:0x%X, please report this!", family, model);
break;
}
if (currentGraphics)
lilu.onKextLoadForce(currentGraphics);
if (currentFramebuffer)
lilu.onKextLoadForce(currentFramebuffer);
if (currentFramebufferOpt)
lilu.onKextLoadForce(currentFramebufferOpt);
}
void IGFX::deinit() {
// Deinitialize each submodule
for (auto submodule : submodules)
submodule->deinit();
for (auto submodule : sharedSubmodules)
submodule->deinit();
}
void IGFX::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
bool switchOffGraphics = false;
bool switchOffFramebuffer = false;
framebufferPatch.framebufferId = info->reportedFramebufferId;
auto cpuGeneration = BaseDeviceInfo::get().cpuGeneration;
if (info->videoBuiltin && !gPlatformGraphicsSupported) {
switchOffGraphics = switchOffFramebuffer = true;
} else if (info->videoBuiltin) {
applyFramebufferPatch = loadPatchesFromDevice(info->videoBuiltin, info->reportedFramebufferId);
#ifdef DEBUG
dumpFramebufferToDisk = checkKernelArgument("-igfxdump");
dumpPlatformTable = checkKernelArgument("-igfxfbdump");
#endif
if (supportsGuCFirmware && getKernelVersion() >= KernelVersion::HighSierra) {
if (!PE_parse_boot_argn("igfxfw", &fwLoadMode, sizeof(fwLoadMode)))
WIOKit::getOSDataValue<int32_t>(info->videoBuiltin, "igfxfw", fwLoadMode);
if (fwLoadMode == FW_AUTO)
fwLoadMode = info->firmwareVendor == DeviceInfo::FirmwareVendor::Apple ? FW_APPLE : FW_DISABLE;
} else {
fwLoadMode = FW_APPLE; /* Do nothing, GuC is either unsupported due to low OS or Apple */
}
disableAccel = checkKernelArgument("-igfxvesa");
bool connectorLessFrame = info->reportedFramebufferIsConnectorLess;
int gl = info->videoBuiltin->getProperty("disable-metal") != nullptr;
PE_parse_boot_argn("igfxgl", &gl, sizeof(gl));
forceOpenGL = gl == 1;
int metal = info->videoBuiltin->getProperty("enable-metal") != nullptr;
PE_parse_boot_argn("igfxmetal", &metal, sizeof(metal));
forceMetal = metal == 1;
// Automatically enable HDMI -> DP patches
bool nohdmi = info->videoBuiltin->getProperty("disable-hdmi-patches") != nullptr;
hdmiAutopatch = !applyFramebufferPatch && !connectorLessFrame && getKernelVersion() >= Yosemite &&
cpuGeneration >= CPUInfo::CpuGeneration::SandyBridge && !checkKernelArgument("-igfxnohdmi") && !nohdmi;
// Set AAPL00,DualLink to zero on Westmere if applying single link patches.
if (cpuGeneration == CPUInfo::CpuGeneration::Westmere && framebufferWestmerePatches.SingleLink) {
uint8_t dualLinkBytes[] { 0x00, 0x00, 0x00, 0x00 };
info->videoBuiltin->setProperty("AAPL00,DualLink", dualLinkBytes, sizeof(dualLinkBytes));
}
// Note that the order does matter
// We first iterate through each submodule and redirect the request
for (auto submodule : submodules)
submodule->processKernel(patcher, info);
// Then process shared submodules
// A shared submodule will disable itself if no active submodule depends on it
for (auto submodule : sharedSubmodules)
submodule->processKernel(patcher, info);
// Iterate through each submodule and see if we need to patch the graphics and the framebuffer kext
auto submodulesRequiresFramebufferPatch = false;
auto submodulesRequiresGraphicsPatch = false;
for (auto submodule : submodules) {
submodulesRequiresFramebufferPatch |= submodule->requiresPatchingFramebuffer;
submodulesRequiresGraphicsPatch |= submodule->requiresPatchingGraphics;
}
// Ideally, we could get rid of these two lambda expressions
auto requiresFramebufferPatches = [this]() {
if (applyFramebufferPatch || hdmiAutopatch)
return true;
if (dumpFramebufferToDisk || dumpPlatformTable)
return true;
return false;
};
auto requiresGraphicsPatches = [this]() {
if (forceOpenGL)
return true;
if (forceMetal)
return true;
if (moderniseAccelerator)
return true;
if (fwLoadMode != FW_APPLE)
return true;
if (disableAccel)
return true;
return false;
};
// Disable kext patching if we have nothing to do.
switchOffFramebuffer = !requiresFramebufferPatches() && !submodulesRequiresFramebufferPatch;
switchOffGraphics = !requiresGraphicsPatches() && !submodulesRequiresGraphicsPatch;
} else {
switchOffGraphics = switchOffFramebuffer = true;
}
if (switchOffGraphics && currentGraphics)
currentGraphics->switchOff();
if (switchOffFramebuffer) {
if (currentFramebuffer)
currentFramebuffer->switchOff();
if (currentFramebufferOpt)
currentFramebufferOpt->switchOff();
}
if (moderniseAccelerator) {
KernelPatcher::RouteRequest request("__ZN9IOService20copyExistingServicesEP12OSDictionaryjj", wrapCopyExistingServices, orgCopyExistingServices);
patcher.routeMultiple(KernelPatcher::KernelID, &request, 1);
}
}
bool IGFX::processKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
auto cpuGeneration = BaseDeviceInfo::get().cpuGeneration;
if (currentGraphics && currentGraphics->loadIndex == index) {
if (forceOpenGL || forceMetal || forceSKLAsKBL || moderniseAccelerator || fwLoadMode != FW_APPLE || disableAccel) {
KernelPatcher::RouteRequest request("__ZN16IntelAccelerator5startEP9IOService", wrapAcceleratorStart, orgAcceleratorStart);
patcher.routeMultiple(index, &request, 1, address, size);
if (fwLoadMode == FW_GENERIC && getKernelVersion() <= KernelVersion::Mojave)
loadIGScheduler4Patches(patcher, index, address, size);
}
// Note that the order is reversed at this stage
// We first process shared submodules
// If a shared submodule fails to process the acceleration driver,
// all subdmoules that depend on it will be disabled automatically.
for (auto submodule : sharedSubmodules)
if (submodule->enabled)
submodule->processGraphicsKext(patcher, index, address, size);
// Then iterate through each submodule and redirect the request if and only if it is enabled
for (auto submodule : submodules)
if (submodule->enabled)
submodule->processGraphicsKext(patcher, index, address, size);
return true;
}
if ((currentFramebuffer && currentFramebuffer->loadIndex == index) ||
(currentFramebufferOpt && currentFramebufferOpt->loadIndex == index)) {
// Note that the order is reversed at this stage
// We first process shared submodules
// If a shared submodule fails to process the framebuffer driver,
// all subdmoules that depend on it will be disabled automatically.
for (auto submodule : sharedSubmodules)
if (submodule->enabled)
submodule->processFramebufferKext(patcher, index, address, size);
// Then iterate through each submodule and redirect the request if and only if it is enabled
for (auto submodule : submodules)
if (submodule->enabled)
submodule->processFramebufferKext(patcher, index, address, size);
if (applyFramebufferPatch || dumpFramebufferToDisk || dumpPlatformTable || hdmiAutopatch) {
framebufferStart = reinterpret_cast<uint8_t *>(address);
framebufferSize = size;
if (cpuGeneration >= CPUInfo::CpuGeneration::SandyBridge) {
if (cpuGeneration == CPUInfo::CpuGeneration::SandyBridge) {
gPlatformListIsSNB = true;
gPlatformInformationList = patcher.solveSymbol<void *>(index, "_PlatformInformationList", address, size);
} else {
gPlatformListIsSNB = false;
gPlatformInformationList = patcher.solveSymbol<void *>(index, "_gPlatformInformationList", address, size);
}
DBGLOG("igfx", "platform is snb %d and list " PRIKADDR, gPlatformListIsSNB, CASTKADDR(gPlatformInformationList));
}
if ((gPlatformInformationList && cpuGeneration >= CPUInfo::CpuGeneration::SandyBridge) || cpuGeneration == CPUInfo::CpuGeneration::Westmere) {
auto fbGetOSInformation = "__ZN31AppleIntelFramebufferController16getOSInformationEv";
if (cpuGeneration == CPUInfo::CpuGeneration::Westmere)
fbGetOSInformation = "__ZN22AppleIntelHDGraphicsFB16getOSInformationEv";
else if (cpuGeneration == CPUInfo::CpuGeneration::SandyBridge)
fbGetOSInformation = "__ZN23AppleIntelSNBGraphicsFB16getOSInformationEv";
else if (cpuGeneration == CPUInfo::CpuGeneration::IvyBridge)
fbGetOSInformation = "__ZN25AppleIntelCapriController16getOSInformationEv";
else if (cpuGeneration == CPUInfo::CpuGeneration::Haswell)
fbGetOSInformation = "__ZN24AppleIntelAzulController16getOSInformationEv";
else if (cpuGeneration == CPUInfo::CpuGeneration::Broadwell)
fbGetOSInformation = "__ZN22AppleIntelFBController16getOSInformationEv";
KernelPatcher::RouteRequest request(fbGetOSInformation, wrapGetOSInformation, orgGetOSInformation);
patcher.routeMultiple(index, &request, 1, address, size);
} else if (cpuGeneration >= CPUInfo::CpuGeneration::SandyBridge) {
SYSLOG("igfx", "failed to obtain gPlatformInformationList pointer with code %d", patcher.getError());
patcher.clearError();
}
if (applyFramebufferPatch && cpuGeneration == CPUInfo::CpuGeneration::Westmere) {
KernelPatcher::RouteRequest requestWestmere("__ZN22AppleIntelHDGraphicsFB8TrainFDIEiPNS_10CRTCParamsE", wrapTrainFDI, orgTrainFDI);
patcher.routeMultiple(index, &requestWestmere, 1, address, size);
applyWestmerePatches(patcher);
}
}
return true;
}
return false;
}
// MARK: - Global Framebuffer Controller Access Support
void IGFX::FramebufferControllerAccessSupport::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::FramebufferControllerAccessSupport::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
// Enable if at least one active submodule relies on this shared module
for (auto submodule : callbackIGFX->submodules)
if (submodule->enabled)
enabled |= submodule->requiresGlobalFramebufferControllersAccess;
DBGLOG("igfx", "FCA: Enabled = %d.", enabled);
}
void IGFX::FramebufferControllerAccessSupport::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
KernelPatcher::SolveRequest request("_gController", controllers);
if (!patcher.solveMultiple(index, &request, 1, address, size)) {
SYSLOG("igfx", "FCA: Failed to resolve the symbol of global controllers. Will disable all submodules that rely on this one.");
disableDependentSubmodules();
}
}
void IGFX::FramebufferControllerAccessSupport::disableDependentSubmodules() {
for (auto submodule : callbackIGFX->submodules)
if (submodule->requiresGlobalFramebufferControllersAccess)
submodule->enabled = false;
}
// MARK: - MMIO Registers Read Support
void IGFX::MMIORegistersReadSupport::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::MMIORegistersReadSupport::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
// Enable the verbose output if the boot argument is found
verbose = checkKernelArgument("-igfxvamregs");
enabled |= verbose;
// Enable if at least one active submodule relies on this shared module
for (auto submodule : callbackIGFX->submodules)
if (submodule->enabled)
enabled |= submodule->requiresMMIORegistersReadAccess;
DBGLOG("igfx", "RRS: Enabled = %d.", enabled);
}
void IGFX::MMIORegistersReadSupport::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
// Symbol Table:
// IVB*: "__ZN25AppleIntelCapriController14ReadRegister32Em"
// HSW*: "__ZN24AppleIntelAzulController14ReadRegister32Em"
// BDW*: "__ZNK22AppleIntelFBController14ReadRegister32Em"
// SKL+: "__ZN31AppleIntelFramebufferController14ReadRegister32Em"
const char* symbol = nullptr;
auto framebuffer = Value::of(callbackIGFX->getRealFramebuffer(index));
if (framebuffer.isOneOf(&kextIntelCapriFb)) {
symbol = "__ZN25AppleIntelCapriController14ReadRegister32Em";
DBGLOG("igfx", "RRS: Will setup the read register module for IVB platform.");
} else if (framebuffer.isOneOf(&kextIntelAzulFb)) {
symbol = "__ZN24AppleIntelAzulController14ReadRegister32Em";
DBGLOG("igfx", "RRS: Will setup the read register module for HSW platform.");
} else if (framebuffer.isOneOf(&kextIntelBDWFb)) {
symbol = "__ZNK22AppleIntelFBController14ReadRegister32Em";
DBGLOG("igfx", "RRS: Will setup the read register module for BDW platform.");
} else if (framebuffer.isOneOf(&kextIntelSKLFb, &kextIntelKBLFb, &kextIntelCFLFb, &kextIntelICLLPFb)) {
symbol = "__ZN31AppleIntelFramebufferController14ReadRegister32Em";
DBGLOG("igfx", "RRS: Will setup the read register module for SKL/KBL/CFL/ICL platform.");
} else {
SYSLOG("igfx", "RRS: Found an unsupported platform. Will disable all submodules that depend on RRS.");
return disableDependentSubmodules();
}
KernelPatcher::RouteRequest request(symbol, wrapReadRegister32, orgReadRegister32);
if (!patcher.routeMultiple(index, &request, 1, address, size)) {
SYSLOG("igfx", "RRS: Failed to resolve the symbol of ReadRegister32. Will disable all submodules that rely on this one.");
disableDependentSubmodules();
}
}
void IGFX::MMIORegistersReadSupport::disableDependentSubmodules() {
for (auto submodule : callbackIGFX->submodules)
if (submodule->requiresMMIORegistersReadAccess)
submodule->enabled = false;
}
uint32_t IGFX::MMIORegistersReadSupport::wrapReadRegister32(void *controller, uint32_t address) {
// Guard: Perform prologue injections
auto prologueInjector = callbackIGFX->modMMIORegistersReadSupport.prologueList.getInjector(address);
if (prologueInjector) {
DBGLOG("igfx", "RRS: Found a prologue injector triggered by the register address 0x%x.", address);
prologueInjector(controller, address);
}
// Guard: Perform replacer injections
auto replacerInjector = callbackIGFX->modMMIORegistersReadSupport.replacerList.getInjector(address);
if (replacerInjector) {
DBGLOG("igfx", "RRS: Found a replacer injector triggered by the register value 0x%x.", address);
return replacerInjector(controller, address);
}
// Invoke the original function
uint32_t retVal = callbackIGFX->modMMIORegistersReadSupport.orgReadRegister32(controller, address);
if (callbackIGFX->modMMIORegistersReadSupport.verbose)
DBGLOG("igfx", "RRS: Read MMIO Register = 0x%x; Value = 0x%x.", address, retVal);
// Guard: Perform epilogue injections
auto epilogueInjector = callbackIGFX->modMMIORegistersReadSupport.epilogueList.getInjector(address);
if (epilogueInjector) {
DBGLOG("igfx", "RRS: Found a epilogue injector triggered by the register value 0x%x.", address);
return epilogueInjector(controller, address, retVal);
}
// Return the register value retrieved from the original function
return retVal;
}
// MARK: - MMIO Registers Write Support
void IGFX::MMIORegistersWriteSupport::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::MMIORegistersWriteSupport::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
// Enable the verbose output if the boot argument is found
verbose = checkKernelArgument("-igfxvamregs");
enabled |= verbose;
// Enable if at least one active submodule relies on this shared module
for (auto submodule : callbackIGFX->submodules)
if (submodule->enabled)
enabled |= submodule->requiresMMIORegistersWriteAccess;
DBGLOG("igfx", "RWS: Enabled = %d.", enabled);
}
void IGFX::MMIORegistersWriteSupport::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
// Symbol Table:
// IVB*: "__ZN25AppleIntelCapriController15WriteRegister32Emj"
// HSW*: "__ZN24AppleIntelAzulController15WriteRegister32Emj"
// BDW*: "__ZN22AppleIntelFBController15WriteRegister32Emj"
// SKL+: "__ZN31AppleIntelFramebufferController15WriteRegister32Emj"
const char* symbol = nullptr;
auto framebuffer = Value::of(callbackIGFX->getRealFramebuffer(index));
if (framebuffer.isOneOf(&kextIntelCapriFb)) {
symbol = "__ZN25AppleIntelCapriController15WriteRegister32Emj";
DBGLOG("igfx", "RWS: Will setup the read register module for IVB platform.");
} else if (framebuffer.isOneOf(&kextIntelAzulFb)) {
symbol = "__ZN24AppleIntelAzulController15WriteRegister32Emj";
DBGLOG("igfx", "RWS: Will setup the read register module for HSW platform.");
} else if (framebuffer.isOneOf(&kextIntelBDWFb)) {
symbol = "__ZN22AppleIntelFBController15WriteRegister32Emj";
DBGLOG("igfx", "RWS: Will setup the read register module for BDW platform.");
} else if (framebuffer.isOneOf(&kextIntelSKLFb, &kextIntelKBLFb, &kextIntelCFLFb, &kextIntelICLLPFb)) {
symbol = "__ZN31AppleIntelFramebufferController15WriteRegister32Emj";
DBGLOG("igfx", "RWS: Will setup the read register module for SKL/KBL/CFL/ICL platform.");
} else {
SYSLOG("igfx", "RWS: Found an unsupported platform. Will disable all submodules that depend on RWS.");
return disableDependentSubmodules();
}
KernelPatcher::RouteRequest request(symbol, wrapWriteRegister32, orgWriteRegister32);
if (!patcher.routeMultiple(index, &request, 1, address, size)) {
SYSLOG("igfx", "RWS: Failed to resolve the symbol of WriteRegister32. Will disable all submodules that rely on this one.");
disableDependentSubmodules();
}
}
void IGFX::MMIORegistersWriteSupport::disableDependentSubmodules() {
for (auto submodule : callbackIGFX->submodules)
if (submodule->requiresMMIORegistersWriteAccess)
submodule->enabled = false;
}
void IGFX::MMIORegistersWriteSupport::wrapWriteRegister32(void *controller, uint32_t address, uint32_t value) {
// Guard: Perform prologue injections
auto prologueInjector = callbackIGFX->modMMIORegistersWriteSupport.prologueList.getInjector(address);
if (prologueInjector) {
DBGLOG("igfx", "RWS: Found a prologue injector triggered by the register address 0x%x.", address);
prologueInjector(controller, address, value);
}
// Guard: Perform replacer injections
auto replacerInjector = callbackIGFX->modMMIORegistersWriteSupport.replacerList.getInjector(address);
if (replacerInjector) {
DBGLOG("igfx", "RWS: Found a replacer injector triggered by the register value 0x%x.", address);
return replacerInjector(controller, address, value);
}
// Invoke the original function
callbackIGFX->modMMIORegistersWriteSupport.orgWriteRegister32(controller, address, value);
if (callbackIGFX->modMMIORegistersWriteSupport.verbose)
DBGLOG("igfx", "RWS: Write MMIO Register = 0x%x; Value = 0x%x.", address, value);
// Guard: Perform epilogue injections
auto epilogueInjector = callbackIGFX->modMMIORegistersWriteSupport.epilogueList.getInjector(address);
if (epilogueInjector) {
DBGLOG("igfx", "RWS: Found a epilogue injector triggered by the register value 0x%x.", address);
return epilogueInjector(controller, address, value);
}
}
// MARK: - Force Complete Modeset
void IGFX::ForceCompleteModeset::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::ForceCompleteModeset::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
uint32_t forceCompleteModeSet = 0;
if (PE_parse_boot_argn("igfxfcms", &forceCompleteModeSet, sizeof(forceCompleteModeSet))) {
enabled = supported && forceCompleteModeSet != 0;
DBGLOG("weg", "force complete-modeset overriden by boot-argument %u -> %d", forceCompleteModeSet, enabled);
} else if (WIOKit::getOSDataValue(info->videoBuiltin, "complete-modeset", forceCompleteModeSet)) {
enabled = supported && forceCompleteModeSet != 0;
DBGLOG("weg", "force complete-modeset overriden by device property %u -> %d", forceCompleteModeSet, enabled);
} else if (info->firmwareVendor == DeviceInfo::FirmwareVendor::Apple) {
enabled = false; // may interfere with FV2
DBGLOG("weg", "force complete-modeset overriden by Apple firmware -> %d", enabled);
}
if (enabled) {
uint64_t fbs;
if (PE_parse_boot_argn("igfxfcmsfbs", &fbs, sizeof(fbs)) ||
WIOKit::getOSDataValue(info->videoBuiltin, "complete-modeset-framebuffers", fbs)) {
for (size_t i = 0; i < arrsize(this->fbs); i++)
this->fbs[i] = (fbs >> (8 * i)) & 0xffU;
customised = true;
}
}
}
void IGFX::ForceCompleteModeset::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
KernelPatcher::RouteRequest request = {
legacy ?
"__ZN31AppleIntelFramebufferController16hwRegsNeedUpdateEP21AppleIntelFramebufferP21AppleIntelDisplayPathPNS_10CRTCParamsE" :
"__ZN31AppleIntelFramebufferController16hwRegsNeedUpdateEP21AppleIntelFramebufferP21AppleIntelDisplayPathPNS_10CRTCParamsEPK29IODetailedTimingInformationV2",
wrapHwRegsNeedUpdate,
orgHwRegsNeedUpdate
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "FCM: Failed to route the function hwRegsNeedUpdate.");
}
bool IGFX::ForceCompleteModeset::wrapHwRegsNeedUpdate(void *controller, IORegistryEntry *framebuffer, void *displayPath, void *crtParams, void *detailedInfo) {
// The framebuffer controller can perform panel fitter, partial, or a
// complete modeset (see AppleIntelFramebufferController::hwSetMode).
// In a dual-monitor CFL DVI+HDMI setup, only HDMI output was working after
// boot: it was observed that for HDMI framebuffer a complete modeset
// eventually occured, but for DVI it never did until after sleep and wake
// sequence.
//
// Function AppleIntelFramebufferController::hwRegsNeedUpdate checks
// whether a complete modeset needs to be issued. It does so by comparing
// sets of pipes and transcoder parameters. For some reason, the result was
// never true in the above scenario, so a complete modeset never occured.
// Consequently, AppleIntelFramebufferController::LightUpTMDS was never
// called for that framebuffer.
//
// Patching hwRegsNeedUpdate to always return true seems to be a rather
// safe solution to that. Note that the root cause of the problem is
// somewhere deeper.
// On older Skylake versions this function has no detailedInfo and does not use framebuffer argument.
// As a result the compiler does not pass framebuffer to the target function. Since the fix is disabled
// by default for Skylake, just force complete modeset on all framebuffers when actually requested.
if (callbackIGFX->modForceCompleteModeset.legacy)
return true;
// Either this framebuffer is in override list
if (callbackIGFX->modForceCompleteModeset.customised) {
return callbackIGFX->modForceCompleteModeset.inList(framebuffer) || callbackIGFX->modForceCompleteModeset.orgHwRegsNeedUpdate(controller, framebuffer, displayPath, crtParams, detailedInfo);
}
// Or it is not built-in, as indicated by AppleBacklightDisplay setting property "built-in" for
// this framebuffer.
// Note we need to check this at every invocation, as this property may reappear
return !framebuffer->getProperty("built-in") || callbackIGFX->modForceCompleteModeset.orgHwRegsNeedUpdate(controller, framebuffer, displayPath, crtParams, detailedInfo);
}
// MARK: - Force Online Display
void IGFX::ForceOnlineDisplay::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::ForceOnlineDisplay::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
uint32_t forceOnline = 0;
if (PE_parse_boot_argn("igfxonln", &forceOnline, sizeof(forceOnline))) {
enabled = forceOnline != 0;
DBGLOG("weg", "force online overriden by boot-argument %u", forceOnline);
} else if (WIOKit::getOSDataValue(info->videoBuiltin, "force-online", forceOnline)) {
enabled = forceOnline != 0;
DBGLOG("weg", "force online overriden by device property %u", forceOnline);
}
if (enabled) {
uint64_t fbs;
if (PE_parse_boot_argn("igfxonlnfbs", &fbs, sizeof(fbs)) ||
WIOKit::getOSDataValue(info->videoBuiltin, "force-online-framebuffers", fbs)) {
for (size_t i = 0; i < arrsize(this->fbs); i++)
this->fbs[i] = (fbs >> (8 * i)) & 0xffU;
customised = true;
}
}
}
void IGFX::ForceOnlineDisplay::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
KernelPatcher::RouteRequest request = {
"__ZN21AppleIntelFramebuffer16getDisplayStatusEP21AppleIntelDisplayPath",
wrapGetDisplayStatus,
orgGetDisplayStatus
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "FOD: Failed to route the function getDisplayStatus.");
}
uint32_t IGFX::ForceOnlineDisplay::wrapGetDisplayStatus(IORegistryEntry *framebuffer, void *displayPath) {
// 0 - offline, 1 - online, 2 - empty dongle.
uint32_t ret = callbackIGFX->modForceOnlineDisplay.orgGetDisplayStatus(framebuffer, displayPath);
if (ret != 1) {
if (callbackIGFX->modForceOnlineDisplay.customised)
ret = callbackIGFX->modForceOnlineDisplay.inList(framebuffer) ? 1 : ret;
else
ret = 1;
}
DBGLOG("igfx", "getDisplayStatus forces %u", ret);
return ret;
}
// MARK: - AGDC Disabler
void IGFX::AGDCDisabler::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::AGDCDisabler::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
int agdc = info->videoBuiltin->getProperty("disable-agdc") != nullptr ? 0 : 1;
PE_parse_boot_argn("igfxagdc", &agdc, sizeof(agdc));
enabled = agdc == 0;
}
void IGFX::AGDCDisabler::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
KernelPatcher::RouteRequest request = {
"__ZN20IntelFBClientControl11doAttributeEjPmmS0_S0_P25IOExternalMethodArguments",
wrapFBClientDoAttribute,
orgFBClientDoAttribute
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "AGDCD: Failed to route the function FBClientControl::doAttribute.");
}
IOReturn IGFX::AGDCDisabler::wrapFBClientDoAttribute(void *fbclient, uint32_t attribute, unsigned long *unk1, unsigned long unk2, unsigned long *unk3, unsigned long *unk4, void *externalMethodArguments) {
if (attribute == kAGDCRegisterCallback) {
DBGLOG("igfx", "AGDCD: Ignoring AGDC registration in FBClientControl::doAttribute.");
return kIOReturnUnsupported;
}
return callbackIGFX->modAGDCDisabler.orgFBClientDoAttribute(fbclient, attribute, unk1, unk2, unk3, unk4, externalMethodArguments);
}
// MARK: - Type-C Check Disabler
void IGFX::TypeCCheckDisabler::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::TypeCCheckDisabler::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
enabled &= !checkKernelArgument("-igfxtypec");
}
void IGFX::TypeCCheckDisabler::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
if (callbackIGFX->getRealFramebuffer(index) == &kextIntelCFLFb || getKernelVersion() >= KernelVersion::BigSur) {
KernelPatcher::RouteRequest request = {
"__ZN31AppleIntelFramebufferController17IsTypeCOnlySystemEv",
wrapIsTypeCOnlySystem
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "TCCD: Failed to route the function IsTypeCOnlySystem.");
}
}
bool IGFX::TypeCCheckDisabler::wrapIsTypeCOnlySystem(void *controller) {
DBGLOG("igfx", "TCCD: Forcing IsTypeCOnlySystem false.");
return false;
}
// MARK: - Black Screen Fix (HDMI/DVI Displays)
void IGFX::BlackScreenFix::init() {
// We only need to patch the framebuffer driver
requiresPatchingFramebuffer = true;
}
void IGFX::BlackScreenFix::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
// Black screen (ComputeLaneCount) happened from 10.12.4
// It only affects SKL, KBL, and CFL drivers with a frame with connectors.
if (!info->reportedFramebufferIsConnectorLess && available &&
((getKernelVersion() == KernelVersion::Sierra && getKernelMinorVersion() >= 5) ||
getKernelVersion() >= KernelVersion::HighSierra)) {
enabled = info->firmwareVendor != DeviceInfo::FirmwareVendor::Apple;
}
}
void IGFX::BlackScreenFix::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
bool foundSymbol = false;
// Currently it is 10.14.1 and Kaby+...
if (getKernelVersion() >= KernelVersion::Mojave &&
BaseDeviceInfo::get().cpuGeneration >= CPUInfo::CpuGeneration::KabyLake) {
KernelPatcher::RouteRequest request = {
"__ZN31AppleIntelFramebufferController16ComputeLaneCountEPK29IODetailedTimingInformationV2jPj",
wrapComputeLaneCountNouveau,
orgComputeLaneCountNouveau
};
foundSymbol = patcher.routeMultiple(index, &request, 1, address, size);
}
if (!foundSymbol) {
KernelPatcher::RouteRequest request = {
"__ZN31AppleIntelFramebufferController16ComputeLaneCountEPK29IODetailedTimingInformationV2jjPj",
wrapComputeLaneCount,
orgComputeLaneCount
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "BSF: Failed to route the function ComputeLaneCount.");
}
}
bool IGFX::BlackScreenFix::wrapComputeLaneCount(void *controller, void *detailedTiming, uint32_t bpp, int availableLanes, int *laneCount) {
DBGLOG("igfx", "BSF: ComputeLaneCount: bpp = %u, available lanes = %d", bpp, availableLanes);
// It seems that AGDP fails to properly detect external boot monitors. As a result computeLaneCount
// is mistakengly called for any boot monitor (e.g. HDMI/DVI), while it is only meant to be used for
// DP (eDP) displays. More details could be found at:
// https://github.com/vit9696/Lilu/issues/27#issuecomment-372103559
// Since the only problematic function is AppleIntelFramebuffer::validateDetailedTiming, there are
// multiple ways to workaround it.
// 1. In 10.13.4 Apple added an additional extended timing validation call, which happened to be
// guardded by a HDMI 2.0 enable boot-arg, which resulted in one bug fixing the other, and 10.13.5
// broke it again.
// 2. Another good way is to intercept AppleIntelFramebufferController::RegisterAGDCCallback and
// make sure AppleGraphicsDevicePolicy::_VendorEventHandler returns mode 2 (not 0) for event 10.
// 3. Disabling AGDC by nopping AppleIntelFramebufferController::RegisterAGDCCallback is also fine.
// Simply returning true from computeLaneCount and letting 0 to be compared against zero so far was
// least destructive and most reliable. Let's stick with it until we could solve more problems.
bool r = callbackIGFX->modBlackScreenFix.orgComputeLaneCount(controller, detailedTiming, bpp, availableLanes, laneCount);
if (!r && *laneCount == 0) {
DBGLOG("igfx", "BSF: Reporting worked lane count (legacy)");
r = true;
}
return r;
}
bool IGFX::BlackScreenFix::wrapComputeLaneCountNouveau(void *controller, void *detailedTiming, int availableLanes, int *laneCount) {
bool r = callbackIGFX->modBlackScreenFix.orgComputeLaneCountNouveau(controller, detailedTiming, availableLanes, laneCount);
if (!r && *laneCount == 0) {
DBGLOG("igfx", "reporting worked lane count (nouveau)");
r = true;
}
return r;
}
// MARK: - PAVP Disabler
void IGFX::PAVPDisabler::init() {
// We only need to patch the accelerator driver
requiresPatchingGraphics = true;
}
void IGFX::PAVPDisabler::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
int pavpMode = info->reportedFramebufferIsConnectorLess || info->firmwareVendor == DeviceInfo::FirmwareVendor::Apple;
if (!PE_parse_boot_argn("igfxpavp", &pavpMode, sizeof(pavpMode)))
WIOKit::getOSDataValue(info->videoBuiltin, "igfxpavp", pavpMode);
enabled = pavpMode == 0 && BaseDeviceInfo::get().cpuGeneration >= CPUInfo::CpuGeneration::SandyBridge;
}
void IGFX::PAVPDisabler::processGraphicsKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
const char* symbol;
switch (BaseDeviceInfo::get().cpuGeneration) {
case CPUInfo::CpuGeneration::SandyBridge:
symbol = "__ZN15Gen6Accelerator19PAVPCommandCallbackE22PAVPSessionCommandID_t18PAVPSessionAppID_tPjb";
break;
case CPUInfo::CpuGeneration::IvyBridge:
symbol = "__ZN16IntelAccelerator19PAVPCommandCallbackE22PAVPSessionCommandID_t18PAVPSessionAppID_tPjb";
break;
default:
symbol = "__ZN16IntelAccelerator19PAVPCommandCallbackE22PAVPSessionCommandID_tjPjb";
break;
}
KernelPatcher::RouteRequest request = {
symbol,
wrapPavpSessionCallback,
orgPavpSessionCallback
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "PAVP: Failed to route the function PAVPCommandCallback.");
}
IOReturn IGFX::PAVPDisabler::wrapPavpSessionCallback(void *intelAccelerator, int32_t sessionCommand, uint32_t sessionAppId, uint32_t *a4, bool flag) {
//DBGLOG("igfx, "pavpCallback: cmd = %d, flag = %d, app = %u, a4 = %s", sessionCommand, flag, sessionAppId, a4 == nullptr ? "null" : "not null");
if (sessionCommand == 4) {
DBGLOG("igfx", "PAVP: PavpSessionCallback: Enforcing error on cmd 4 (send to ring?)!");
return kIOReturnTimeout; // or kIOReturnSuccess
}
return callbackIGFX->modPAVPDisabler.orgPavpSessionCallback(intelAccelerator, sessionCommand, sessionAppId, a4, flag);
}
// MARK: - Read Descriptors Patch
void IGFX::ReadDescriptorPatch::init() {
// We only need to patch the accelerator driver
requiresPatchingGraphics = true;
}
void IGFX::ReadDescriptorPatch::processKernel(KernelPatcher &patcher, DeviceInfo *info) {
// Starting from 10.14.4b1 Skylake+ graphics randomly kernel panics on GPU usage
enabled = BaseDeviceInfo::get().cpuGeneration >= CPUInfo::CpuGeneration::Skylake && getKernelVersion() >= KernelVersion::Mojave;
}
void IGFX::ReadDescriptorPatch::processGraphicsKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) {
KernelPatcher::RouteRequest request = {
"__ZNK25IGHardwareGlobalPageTable4readEyRyS0_",
globalPageTableRead
};
if (!patcher.routeMultiple(index, &request, 1, address, size))
SYSLOG("igfx", "RDP: Failed to route the function IGHardwareGlobalPageTable::read.");
}
bool IGFX::ReadDescriptorPatch::globalPageTableRead(void *hardwareGlobalPageTable, uint64_t address, uint64_t &physAddress, uint64_t &flags) {
uint64_t pageNumber = address >> PAGE_SHIFT;
uint64_t pageEntry = getMember<uint64_t *>(hardwareGlobalPageTable, 0x28)[pageNumber];
// PTE: Page Table Entry for 4KB Page, page 82:
// https://01.org/sites/default/files/documentation/intel-gfx-prm-osrc-kbl-vol05-memory_views.pdf.
physAddress = pageEntry & 0x7FFFFFF000ULL; // HAW-1:12, where HAW is 39.
flags = pageEntry & PAGE_MASK; // 11:0
// Relevant flag bits are as follows:
// 2 Ignored Ignored (h/w does not care about values behind ignored registers)
// 1 R/W: Read/Write Write permission rights. If 0, write permission not granted for requests with user-level privilege
// (and requests with supervisor-level privilege, if WPE=1 in the relevant extended-context-entry) to
// the memory region controlled by this entry. See a later section for access rights.
// GPU does not support Supervisor mode contexts.
// 0 P: Present This bit must be “1” to point to a valid Page.
//
// In 10.14.4b1 the return condition changed from (flags & 7U) != 0 to (flags & 1U) != 0. The change makes good sense to me, but
// it results in crashes on 10.14.4b1 on many ASUS Z170 and Z270 boards with connector-less IGPU framebuffer.
// The reason for that is that __ZNK31IGHardwarePerProcessPageTable6422readDescriptorForRangeERK14IGAddressRangePPN10IGPagePool14PageDescriptorE
// unconditionally returns true but actually returns NULL PageDescriptor, which subsequently results in OSAddAtomic64(&this->counter)
// __ZN31IGHardwarePerProcessPageTable6421mapDescriptorForRangeERK14IGAddressRangePN10IGPagePool14PageDescriptorE writing to invalid address.
//
// Fault CR2: 0x0000000000000028, Error code: 0x0000000000000002, Fault CPU: 0x1, PL: 0, VF: 0
// 0xffffff821885b8f0 : 0xffffff801d3c7dc4 mach_kernel : _OSAddAtomic64 + 0x4
// 0xffffff821885b9e0 : 0xffffff7f9f3f1845 com.apple.driver.AppleIntelKBLGraphics :
// __ZN31IGHardwarePerProcessPageTable6421mapDescriptorForRangeERK14IGAddressRangePN10IGPagePool14PageDescriptorE + 0x6b
// 0xffffff821885ba20 : 0xffffff7f9f40a3c3 com.apple.driver.AppleIntelKBLGraphics :
// __ZN29IGHardwarePerProcessPageTable25synchronizePageDescriptorEPKS_RK14IGAddressRangeb + 0x51
// 0xffffff821885ba50 : 0xffffff7f9f40a36b com.apple.driver.AppleIntelKBLGraphics :
// __ZN29IGHardwarePerProcessPageTable15synchronizeWithIS_EEvPKT_RK14IGAddressRangeb + 0x37
// 0xffffff821885ba70 : 0xffffff7f9f3b1716 com.apple.driver.AppleIntelKBLGraphics : __ZN15IGMemoryManager19newPageTableForTaskEP11IGAccelTask + 0x98
// 0xffffff821885baa0 : 0xffffff7f9f40a716 com.apple.driver.AppleIntelKBLGraphics : __ZN11IGAccelTask15initWithOptionsEP16IntelAccelerator + 0x108
// 0xffffff821885bad0 : 0xffffff7f9f40a5f7 com.apple.driver.AppleIntelKBLGraphics : __ZN11IGAccelTask11withOptionsEP16IntelAccelerator + 0x31
// 0xffffff821885baf0 : 0xffffff7f9f3bfaf0 com.apple.driver.AppleIntelKBLGraphics : __ZN16IntelAccelerator17createUserGPUTaskEv + 0x30
// 0xffffff821885bb10 : 0xffffff7f9f2f7520 com.apple.iokit.IOAcceleratorFamily2 : __ZN14IOAccelShared24initEP22IOGraphicsAccelerator2P4task + 0x2e
// 0xffffff821885bb50 : 0xffffff7f9f30c157 com.apple.iokit.IOAcceleratorFamily2 : __ZN22IOGraphicsAccelerator212createSharedEP4task + 0x33
// 0xffffff821885bb80 : 0xffffff7f9f2faa95 com.apple.iokit.IOAcceleratorFamily2 : __ZN24IOAccelSharedUserClient211sharedStartEv + 0x2b
// 0xffffff821885bba0 : 0xffffff7f9f401ca2 com.apple.driver.AppleIntelKBLGraphics : __ZN23IGAccelSharedUserClient11sharedStartEv + 0x16
// 0xffffff821885bbc0 : 0xffffff7f9f2f8aaa com.apple.iokit.IOAcceleratorFamily2 : __ZN24IOAccelSharedUserClient25startEP9IOService + 0x9c
// 0xffffff821885bbf0 : 0xffffff7f9f30ba3c com.apple.iokit.IOAcceleratorFamily2 : __ZN22IOGraphicsAccelerator213newUserClientEP4taskPvjPP12IOUserClient + 0x43e
// 0xffffff821885bc80 : 0xffffff801d42a040 mach_kernel : __ZN9IOService13newUserClientEP4taskPvjP12OSDictionaryPP12IOUserClient + 0x30
// 0xffffff821885bcd0 : 0xffffff801d48ef9a mach_kernel : _is_io_service_open_extended + 0x10a
// 0xffffff821885bd30 : 0xffffff801ce96b52 mach_kernel : _iokit_server_routine + 0x58d2
// 0xffffff821885bd80 : 0xffffff801cdb506c mach_kernel : _ipc_kobject_server + 0x12c
// 0xffffff821885bdd0 : 0xffffff801cd8fa91 mach_kernel : _ipc_kmsg_send + 0xd1
// 0xffffff821885be50 : 0xffffff801cda42fe mach_kernel : _mach_msg_overwrite_trap + 0x3ce
// 0xffffff821885bef0 : 0xffffff801cec3e87 mach_kernel : _mach_call_munger64 + 0x257
// 0xffffff821885bfa0 : 0xffffff801cd5d2c6 mach_kernel : _hndl_mach_scall64 + 0x16
//
// Even so the change makes good sense to me, and most likely the real bug is elsewhere. The change workarounds the issue by also checking
// for the W (writeable) bit in addition to P (present). Presumably this works because some code misuses ::read method to iterate
// over page table instead of obtaining valid mapped physical address.
return (flags & 3U) != 0;
}
// MARK: - TODO
OSObject *IGFX::wrapCopyExistingServices(OSDictionary *matching, IOOptionBits inState, IOOptionBits options) {
if (matching && inState == kIOServiceMatchedState && options == 0) {
auto name = OSDynamicCast(OSString, matching->getObject(gIONameMatchKey));
if (name) {
DBGLOG("igfx", "found matching request by name %s", name->getCStringNoCopy());
if (name->isEqualTo("Gen6Accelerator")) {
DBGLOG("igfx", "found and fixed Gen6Accelerator request");
auto accel = OSString::withCString("IntelAccelerator");
if (accel) {
matching->setObject(gIONameMatchKey, accel);
accel->release();
}
}
}
}
return FunctionCast(wrapCopyExistingServices, callbackIGFX->orgCopyExistingServices)(matching, inState, options);
}
bool IGFX::wrapAcceleratorStart(IOService *that, IOService *provider) {
if (callbackIGFX->disableAccel)